Motr  M0
pool_foms.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_POOL
23 #include "lib/trace.h"
24 
25 #include "lib/errno.h"
26 #include "lib/memory.h"
27 #include "lib/assert.h"
28 #include "lib/misc.h" /* M0_BITS */
29 #include "fop/fop.h"
30 #include "fop/fom_generic.h"
31 #include "reqh/reqh_service.h"
32 #include "pool/pool.h"
33 #include "pool/pool_foms.h"
34 #include "pool/pool_fops.h"
35 #include "rpc/rpc_opcodes.h"
36 #include "motr/setup.h"
37 #include "conf/diter.h"
38 #include "conf/helpers.h" /* m0_conf_drive_get */
39 
40 static const struct m0_fom_ops poolmach_ops;
41 
42 static int poolmach_fom_create(struct m0_fop *fop, struct m0_fom **out,
43  struct m0_reqh *reqh)
44 {
45  struct m0_fop *rep_fop;
46  struct m0_fom *fom;
47  int rc = 0;
48 
49  M0_PRE(fop != NULL);
50  M0_PRE(out != NULL);
51 
53  if (fom == NULL)
54  return M0_ERR(-ENOMEM);
55 
59  M0_LOG(M0_DEBUG, "create Query fop");
63  M0_LOG(M0_DEBUG, "create set fop");
64  } else {
65  m0_free(fom);
66  return M0_ERR(-EINVAL);
67  }
68  if (rep_fop == NULL) {
69  m0_free(fom);
70  return M0_ERR(-ENOMEM);
71  }
72 
74  reqh);
75  *out = fom;
76 
77  return M0_RC(rc);
78 }
79 
80 static void poolmach_set_op(struct m0_fom *fom)
81 {
82  int rc = 0;
83  int i;
84  int j;
85  struct m0_fop *req_fop = fom->fo_fop;
86  struct m0_fop *rep_fop = fom->fo_rep_fop;
87  struct m0_reqh *reqh = m0_fom_reqh(fom);
88  struct m0_motr *motr = m0_cs_ctx_get(reqh);
89  struct m0_fid *dev_fid;
90  struct m0_conf_drive *drive;
91  struct m0_conf_pver **conf_pver;
92  struct m0_pool_version *pv;
93  struct m0_poolmach *pm;
94 
95  struct m0_fop_poolmach_set *set_fop = m0_fop_data(req_fop);
96  struct m0_fop_poolmach_set_rep *set_fop_rep = m0_fop_data(rep_fop);
97  struct m0_poolmach_event pme = {0};
98 
99  for (i = 0; i < set_fop->fps_dev_info.fpi_nr; ++i) {
100  pme.pe_type = set_fop->fps_type;
101  pme.pe_state = set_fop->fps_dev_info.fpi_dev[i].fpd_state;
102  /*
103  * Update pool-machines per pool-version to which device
104  * is associated.
105  */
106  dev_fid = &set_fop->fps_dev_info.fpi_dev[i].fpd_fid;
107  rc = m0_conf_drive_get(m0_reqh2confc(reqh), dev_fid, &drive);
108  if (rc != 0)
109  break;
110  conf_pver = drive->ck_pvers;
111  for (j = 0; conf_pver[j] != NULL; ++j) {
113  &conf_pver[j]->pv_obj.co_id);
114  pm = &pv->pv_mach;
115  rc = m0_poolmach_fid_to_idx(pm, dev_fid, &pme.pe_index);
116  M0_ASSERT(rc == 0);
117  rc = m0_poolmach_state_transit(pm, &pme);
118  if (rc != 0)
119  break;
120  }
121  m0_confc_close(&drive->ck_obj);
122  }
123  set_fop_rep->fps_rc = rc;
124 }
125 
126 static void poolmach_query_op(struct m0_fom *fom)
127 {
128  uint32_t idx = 0;
129  int rc = 0;
130  int i;
131  int j;
132  struct m0_fop *req_fop = fom->fo_fop;
133  struct m0_fop *rep_fop = fom->fo_rep_fop;
134  struct m0_reqh *reqh = m0_fom_reqh(fom);
135  struct m0_motr *motr = m0_cs_ctx_get(reqh);
136  struct m0_conf_drive *drive;
137  struct m0_conf_pver **conf_pver;
138  struct m0_pool_version *pv;
139  struct m0_poolmach *pm;
140  struct m0_fid *dev_fid;
141 
142  struct m0_fop_poolmach_query *query_fop = m0_fop_data(req_fop);
143  struct m0_fop_poolmach_query_rep *query_fop_rep = m0_fop_data(rep_fop);
144 
145  M0_ALLOC_ARR(query_fop_rep->fqr_dev_info.fpi_dev,
146  query_fop->fpq_dev_idx.fpx_nr);
147  if (query_fop_rep->fqr_dev_info.fpi_dev == NULL) {
148  query_fop_rep->fqr_rc = -ENOMEM;
149  return;
150  }
151 
152  query_fop_rep->fqr_dev_info.fpi_nr = query_fop->fpq_dev_idx.fpx_nr;
153 
154  for (i = 0; i < query_fop->fpq_dev_idx.fpx_nr; ++i) {
155  dev_fid = &query_fop->fpq_dev_idx.fpx_fid[i];
156  rc = m0_conf_drive_get(m0_reqh2confc(reqh), dev_fid, &drive);
157  if (rc != 0)
158  break;
159  conf_pver = drive->ck_pvers;
160  /*
161  * Search the first pool version for the given drive fid
162  * and return the corresponding state.
163  */
164  for (j = 0; conf_pver[j] != NULL; ++j) {
166  &conf_pver[j]->pv_obj.co_id);
167  pm = &pv->pv_mach;
168  M0_ASSERT(query_fop->fpq_type == M0_POOL_DEVICE);
169  rc = m0_poolmach_fid_to_idx(pm, dev_fid, &idx);
170  if (rc == -ENOENT)
171  continue;
172  m0_poolmach_device_state(pm, idx,
173  &query_fop_rep->fqr_dev_info.fpi_dev[i].fpd_state);
174  break;
175  }
176  query_fop_rep->fqr_dev_info.fpi_dev[i].fpd_fid = *dev_fid;
177  m0_confc_close(&drive->ck_obj);
178  }
179  query_fop_rep->fqr_rc = rc;
180 }
181 
182 static int poolmach_fom_tick(struct m0_fom *fom)
183 {
184  struct m0_fop *req_fop = fom->fo_fop;
185 
186  /* first handle generic phase */
187  if (m0_fom_phase(fom) < M0_FOPH_NR)
188  return m0_fom_tick_generic(fom);
189 
190  switch (m0_fop_opcode(req_fop)) {
193  break;
194  }
197  break;
198  }
199  default:
200  M0_IMPOSSIBLE("Invalid opcode");
202  return M0_FSO_AGAIN;
203  }
204 
206  return M0_FSO_AGAIN;
207 }
208 
209 static size_t poolmach_fom_home_locality(const struct m0_fom *fom)
210 {
211  M0_PRE(fom != NULL);
212 
213  return m0_fop_opcode(fom->fo_fop);
214 }
215 
216 static void poolmach_fom_fini(struct m0_fom *fom)
217 {
218  m0_fom_fini(fom);
219  m0_free(fom);
220 }
221 
225 static const struct m0_fom_ops poolmach_ops = {
227  .fo_tick = poolmach_fom_tick,
228  .fo_home_locality = poolmach_fom_home_locality
229 };
230 
236 };
237 
240  .sd_name = "Pool Machine query/set",
241  .sd_allowed = M0_BITS(M0_FOPH_SUCCESS, M0_FOPH_FAILURE)
242  }
243 };
244 
245 const struct m0_sm_conf poolmach_conf = {
246  .scf_name = "poolmach",
247  .scf_nr_states = ARRAY_SIZE(poolmach_phases),
248  .scf_state = poolmach_phases
249 };
250 
251 #undef M0_TRACE_SUBSYSTEM
252 
253 /*
254  * Local variables:
255  * c-indentation-style: "K&R"
256  * c-basic-offset: 8
257  * tab-width: 8
258  * fill-column: 80
259  * scroll-step: 1
260  * End:
261  */
struct m0_fid co_id
Definition: obj.h:208
uint32_t m0_fop_opcode(const struct m0_fop *fop)
Definition: fop.c:226
struct m0_fop_poolmach_dev_idx fpq_dev_idx
Definition: pool_fops.h:72
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
M0_INTERNAL int m0_poolmach_state_transit(struct m0_poolmach *pm, const struct m0_poolmach_event *event)
Definition: pool_machine.c:554
#define NULL
Definition: misc.h:38
static void poolmach_set_op(struct m0_fom *fom)
Definition: pool_foms.c:80
Definition: sm.h:350
M0_INTERNAL struct m0_pool_version * m0_pool_version_find(struct m0_pools_common *pc, const struct m0_fid *id)
Definition: pool.c:586
struct m0_pool_version * pv
Definition: dir.c:629
struct m0_poolmach pv_mach
Definition: pool.h:133
#define M0_LOG(level,...)
Definition: trace.h:167
static int poolmach_fom_tick(struct m0_fom *fom)
Definition: pool_foms.c:182
int(* fto_create)(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: fom.h:650
struct m0_fid fpd_fid
Definition: pool_fops.h:52
M0_INTERNAL int m0_poolmach_fid_to_idx(struct m0_poolmach *pm, struct m0_fid *fid, uint32_t *idx)
Definition: pool_machine.c:968
const struct m0_sm_conf poolmach_conf
Definition: pool_foms.c:245
struct m0_fop_poolmach_dev * fpi_dev
Definition: pool_fops.h:47
void * m0_fop_data(const struct m0_fop *fop)
Definition: fop.c:220
struct m0_conf_obj pv_obj
Definition: obj.h:533
#define M0_BITS(...)
Definition: misc.h:236
M0_INTERNAL int m0_poolmach_device_state(struct m0_poolmach *pm, uint32_t device_index, enum m0_pool_nd_state *state_out)
Definition: pool_machine.c:816
static void poolmach_query_op(struct m0_fom *fom)
Definition: pool_foms.c:126
struct m0_conf_pver ** ck_pvers
Definition: obj.h:715
uint32_t fps_type
Definition: pool_fops.h:56
m0_fom_phase
Definition: fom.h:372
uint32_t fpd_state
Definition: pool_fops.h:51
struct m0_fom_type ft_fom_type
Definition: fop.h:232
struct m0_fop_type m0_fop_poolmach_set_rep_fopt
Definition: pool_fops.c:40
static const struct m0_fom_ops poolmach_ops
Definition: pool_foms.c:40
return M0_RC(rc)
void m0_fom_init(struct m0_fom *fom, const struct m0_fom_type *fom_type, const struct m0_fom_ops *ops, struct m0_fop *fop, struct m0_fop *reply, struct m0_reqh *reqh)
Definition: fom.c:1372
int i
Definition: dir.c:1033
struct m0_fop_type * f_type
Definition: fop.h:81
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL struct m0_confc * m0_reqh2confc(struct m0_reqh *reqh)
Definition: reqh.c:753
int m0_fom_tick_generic(struct m0_fom *fom)
Definition: fom_generic.c:848
void m0_fom_fini(struct m0_fom *fom)
Definition: fom.c:1324
#define M0_ASSERT(cond)
const char * scf_name
Definition: sm.h:352
struct m0_fop_poolmach_dev_info fps_dev_info
Definition: pool_fops.h:57
void m0_fom_phase_move(struct m0_fom *fom, int32_t rc, int phase)
Definition: fom.c:1699
static void poolmach_fom_fini(struct m0_fom *fom)
Definition: pool_foms.c:216
struct m0_fop_poolmach_dev_info fqr_dev_info
Definition: pool_fops.h:62
Definition: reqh.h:94
Definition: dump.c:103
const struct m0_fom_type_ops poolmach_fom_type_ops
Definition: pool_foms.c:234
struct m0_fop * m0_fop_reply_alloc(struct m0_fop *req, struct m0_fop_type *rept)
Definition: fop.c:129
Definition: fom.h:481
Definition: setup.h:354
struct m0_reqh reqh
Definition: rm_foms.c:48
const char * sd_name
Definition: sm.h:383
Definition: fid.h:38
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
struct m0_motr motr
struct m0_sm_state_descr poolmach_phases[]
Definition: pool_foms.c:238
struct m0_fid * fpx_fid
Definition: pool_fops.h:67
static int poolmach_fom_create(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: pool_foms.c:42
static struct m0_fop * fop
Definition: item.c:57
M0_INTERNAL void m0_confc_close(struct m0_conf_obj *obj)
Definition: confc.c:921
void(* fo_fini)(struct m0_fom *fom)
Definition: fom.h:657
#define out(...)
Definition: gen.c:41
static size_t poolmach_fom_home_locality(const struct m0_fom *fom)
Definition: pool_foms.c:209
struct m0_fop_type m0_fop_poolmach_query_rep_fopt
Definition: pool_fops.c:38
struct m0_pools_common cc_pools_common
Definition: setup.h:356
enum m0_pool_nd_state pe_state
Definition: pool_machine.h:199
void m0_free(void *data)
Definition: memory.c:146
M0_INTERNAL struct m0_motr * m0_cs_ctx_get(struct m0_reqh *reqh)
Definition: setup.c:1778
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: fop.h:79
struct m0_conf_obj ck_obj
Definition: obj.h:707
M0_INTERNAL struct m0_reqh * m0_fom_reqh(const struct m0_fom *fom)
Definition: fom.c:283
struct m0_fop * rep_fop
Definition: dir.c:334
#define M0_IMPOSSIBLE(fmt,...)