Motr  M0
common_service.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/arith.h"
24 #include "lib/errno.h"
25 #include "cm/ut/common_service.h"
26 #include "rpc/rpc_opcodes.h" /* M0_CM_UT_OPCODE */
27 #include "motr/setup.h"
28 #include "ha/msg.h"
29 
34 
35 const char lfname[] = "cm_ut.errlog";
36 FILE *lf;
37 
38 /* Copy machine replica identifier for multiple copy machine replicas. */
39 uint64_t ut_cm_id;
40 
41 struct m0_ut_cm *cm2utcm(struct m0_cm *cm)
42 {
43  return container_of(cm, struct m0_ut_cm, ut_cm);
44 }
45 
47 {
48  struct m0_cm *cm;
49 
51  return m0_cm_setup(cm);
52 }
53 
55 {
56  struct m0_cm *cm = container_of(service, struct m0_cm, cm_service);
57 
58  m0_cm_fini(cm);
59 }
60 
62 {
63  struct m0_cm *cm = m0_cmsvc2cm(service);
64  struct m0_ut_cm *ut_cm;
65 
67  ut_cm = cm2utcm(cm);
68 
69  m0_chan_fini_lock(&ut_cm->ut_cm_wait);
70  m0_mutex_fini(&ut_cm->ut_cm_wait_mutex);
71  M0_SET0(&ut_cm->ut_cm);
73 }
74 
75 static const struct m0_reqh_service_ops cm_ut_service_ops = {
77  .rso_stop = cm_ut_service_stop,
78  .rso_fini = cm_ut_service_fini
79 };
80 
81 static void cm_cp_ut_free(struct m0_cm_cp *cp)
82 {
83 }
84 
85 static bool cm_cp_ut_invariant(const struct m0_cm_cp *cp)
86 {
87  return true;
88 }
89 
90 static const struct m0_cm_cp_ops cm_cp_ut_ops = {
92  .co_free = cm_cp_ut_free
93 };
94 
95 static struct m0_cm_cp* cm_ut_cp_alloc(struct m0_cm *cm)
96 {
98  return &cm_ut_cp;
99 }
100 
101 static int cm_ut_setup(struct m0_cm *cm)
102 {
103  return 0;
104 }
105 
106 static int cm_ut_prepare(struct m0_cm *cm)
107 {
108  return 0;
109 }
110 
111 static int cm_ut_start(struct m0_cm *cm)
112 {
113  return 0;
114 }
115 
116 static void cm_ut_stop(struct m0_cm *cm)
117 {
118 }
119 
120 static int cm_ut_data_next(struct m0_cm *cm, struct m0_cm_cp *cp)
121 {
122  return -ENODATA;
123 }
124 
125 static void cm_ag_ut_fini(struct m0_cm_aggr_group *ag)
126 {
127 }
128 
129 static uint64_t cm_ag_ut_local_cp_nr(const struct m0_cm_aggr_group *ag)
130 {
131  return CM_UT_LOCAL_CP_NR;
132 }
133 
136  .cago_local_cp_nr = cm_ag_ut_local_cp_nr,
137 };
138 
139 /* Set of aggregation groups per copy machine replica. */
144 
145 static int cm_ut_ag_alloc(struct m0_cm *cm, const struct m0_cm_ag_id *id,
146  bool has_incoming, struct m0_cm_aggr_group **out)
147 {
148  struct m0_ut_cm *ut_cm = cm2utcm(cm);
149 
150  M0_SET0(&aggr_grps[ut_cm->ut_cm_id][id->ai_lo.u_hi]);
151  m0_cm_aggr_group_init(&aggr_grps[ut_cm->ut_cm_id][id->ai_lo.u_hi],
152  cm, id, true, &cm_ag_ut_ops);
153  *out = &aggr_grps[ut_cm->ut_cm_id][id->ai_lo.u_hi];
154 
155  return 0;
156 }
157 
158 static int cm_ut_ag_next(struct m0_cm *cm, const struct m0_cm_ag_id *id_curr,
159  struct m0_cm_ag_id *id_next)
160 {
161  struct m0_fid gfid = {0, 4};
162  struct m0_ut_cm *ut_cm = cm2utcm(cm);
163  uint64_t cid = ut_cm->ut_cm_id;
164 
165  if (ag_id_cnt[cid] == MAX_CM_NR || !test_ready_fop) {
166  ag_id_cnt[cid] = 0;
167  return -ENOENT;
168  }
169 
170  if (test_ready_fop) {
171  M0_SET0(&ag_ids[cid][ag_id_cnt[cid]]);
172  ag_ids[cid][ag_id_cnt[cid]].ai_hi.u_hi = gfid.f_container;
173  ag_ids[cid][ag_id_cnt[cid]].ai_hi.u_lo = gfid.f_key;
174  ag_ids[cid][ag_id_cnt[cid]].ai_lo.u_hi = 0;
175  ag_ids[cid][ag_id_cnt[cid]].ai_lo.u_hi = ag_id_cnt[cid];
176  }
177 
178  *id_next = ag_ids[cid][ag_id_cnt[cid]];
179  ++ag_id_cnt[cid];
180 
181  return 0;
182 }
183 
184 static void cm_ut_ha_msg(struct m0_cm *cm, struct m0_ha_msg *msg, int rc)
185 {
186 }
187 static void cm_ut_fini(struct m0_cm *cm)
188 {
189 }
190 
191 static const struct m0_cm_ops cm_ut_ops = {
193  .cmo_prepare = cm_ut_prepare,
194  .cmo_start = cm_ut_start,
195  .cmo_stop = cm_ut_stop,
196  .cmo_ag_alloc = cm_ut_ag_alloc,
197  .cmo_cp_alloc = cm_ut_cp_alloc,
198  .cmo_data_next = cm_ut_data_next,
199  .cmo_ag_next = cm_ut_ag_next,
200  .cmo_ha_msg = cm_ut_ha_msg,
201  .cmo_fini = cm_ut_fini
202 };
203 
205  const struct m0_reqh_service_type *stype)
206 {
207  struct m0_cm *cm = &cm_ut[ut_cm_id].ut_cm;
208 
209  *service = &cm->cm_service;
210  (*service)->rs_ops = &cm_ut_service_ops;
211  (*service)->rs_sm.sm_state = M0_RST_INITIALISING;
213  m0_mutex_init(&cm_ut[ut_cm_id].ut_cm_wait_mutex);
214  m0_chan_init(&cm_ut[ut_cm_id].ut_cm_wait,
215  &cm_ut[ut_cm_id].ut_cm_wait_mutex);
217 
218  return m0_cm_init(cm, container_of(stype, struct m0_cm_type, ct_stype),
219  &cm_ut_ops);
220 }
221 
224 };
225 
227 
228 struct m0_motr motr = { .cc_pool_width = 10 };
229 struct m0_reqh_context rctx = { .rc_motr = &motr };
230 
232 {
233  int rc;
234  /* Internally calls m0_cm_init(). */
237  NULL);
238  M0_ASSERT(rc == 0);
240 }
241 
243 {
247 }
248 
249 /*
250  * Local variables:
251  * c-indentation-style: "K&R"
252  * c-basic-offset: 8
253  * tab-width: 8
254  * fill-column: 80
255  * scroll-step: 1
256  * End:
257  */
bool(* co_invariant)(const struct m0_cm_cp *cp)
Definition: cp.h:235
uint64_t id
Definition: cob.h:2380
uint64_t ut_cm_id
static void cm_ut_service_fini(struct m0_reqh_service *service)
int(* rso_start)(struct m0_reqh_service *service)
Definition: reqh_service.h:360
static void cm_ut_stop(struct m0_cm *cm)
M0_INTERNAL void m0_reqh_service_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:402
#define NULL
Definition: misc.h:38
bool test_ready_fop
void cm_ut_service_alloc_init(struct m0_reqh *reqh)
static void cm_ag_ut_fini(struct m0_cm_aggr_group *ag)
struct m0_ut_rpc_mach_ctx cmut_rmach_ctx
Definition: cp.h:160
M0_INTERNAL void m0_reqh_service_prepare_to_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:375
static bool cm_cp_ut_invariant(const struct m0_cm_cp *cp)
void(* cago_fini)(struct m0_cm_aggr_group *ag)
Definition: ag.h:140
struct m0_cm ut_cm
static void cm_ut_ha_msg(struct m0_cm *cm, struct m0_ha_msg *msg, int rc)
struct m0_ut_cm cm_ut[MAX_CM_NR]
static int cm_ut_prepare(struct m0_cm *cm)
uint32_t cc_pool_width
Definition: setup.h:409
static const struct m0_reqh_service_type_ops cm_ut_service_type_ops
#define container_of(ptr, type, member)
Definition: misc.h:33
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL void m0_cm_fini(struct m0_cm *cm)
Definition: cm.c:964
struct m0_motr * rc_motr
Definition: setup.h:318
M0_INTERNAL void m0_cm_aggr_group_init(struct m0_cm_aggr_group *ag, struct m0_cm *cm, const struct m0_cm_ag_id *id, bool has_incoming, const struct m0_cm_aggr_group_ops *ag_ops)
Definition: ag.c:153
struct m0_cm_type cm_ut_cmt
struct m0_reqh_service * cm_ut_service
const char lfname[]
static int cm_ut_ag_alloc(struct m0_cm *cm, const struct m0_cm_ag_id *id, bool has_incoming, struct m0_cm_aggr_group **out)
M0_INTERNAL int m0_cm_init(struct m0_cm *cm, struct m0_cm_type *cm_type, const struct m0_cm_ops *cm_ops)
Definition: cm.c:925
static struct m0_cm * cm
Definition: cm.c:63
struct m0_ut_cm * cm2utcm(struct m0_cm *cm)
struct m0_reqh_context rctx
static const struct socktype stype[]
Definition: sock.c:1156
static int cm_ut_data_next(struct m0_cm *cm, struct m0_cm_cp *cp)
M0_INTERNAL void m0_chan_init(struct m0_chan *chan, struct m0_mutex *ch_guard)
Definition: chan.c:96
#define M0_ASSERT(cond)
struct m0_cm_aggr_group aggr_grps[MAX_CM_NR][MAX_CM_NR]
M0_INTERNAL void m0_reqh_service_fini(struct m0_reqh_service *service)
Definition: reqh_service.c:457
const struct m0_cm_cp_ops * c_ops
Definition: cp.h:169
static int cm_ut_service_start(struct m0_reqh_service *service)
uint64_t u_hi
Definition: types.h:36
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
Definition: msg.h:115
uint64_t f_container
Definition: fid.h:39
M0_INTERNAL int m0_reqh_service_allocate(struct m0_reqh_service **out, const struct m0_reqh_service_type *stype, struct m0_reqh_context *rctx)
Definition: reqh_service.c:185
static void cm_ut_fini(struct m0_cm *cm)
Definition: reqh.h:94
static const struct m0_reqh_service_ops cm_ut_service_ops
Definition: cm.h:286
M0_INTERNAL void m0_reqh_service_init(struct m0_reqh_service *service, struct m0_reqh *reqh, const struct m0_fid *fid)
Definition: reqh_service.c:428
void cm_ut_service_cleanup()
static int cm_ut_service_allocate(struct m0_reqh_service **service, const struct m0_reqh_service_type *stype)
M0_INTERNAL int m0_cm_setup(struct m0_cm *cm)
Definition: cm.c:595
uint64_t ut_cm_id
M0_INTERNAL struct m0_cm * m0_cmsvc2cm(struct m0_reqh_service *cmsvc)
Definition: cm.c:497
Definition: cm.h:143
Definition: setup.h:354
struct m0_reqh reqh
Definition: rm_foms.c:48
int(* rsto_service_allocate)(struct m0_reqh_service **service, const struct m0_reqh_service_type *stype)
Definition: reqh_service.h:435
FILE * lf
#define M0_CNT_INC(cnt)
Definition: arith.h:226
Definition: fid.h:38
uint64_t f_key
Definition: fid.h:40
struct m0_cm_ag_id ag_ids[MAX_CM_NR][MAX_CM_NR]
M0_CM_TYPE_DECLARE(cm_ut, M0_CM_UT_OPCODE, &cm_ut_service_type_ops, "cm_ut", 0)
uint64_t ag_id_cnt[MAX_CM_NR]
struct m0_motr motr
struct m0_reqh_service cm_service
Definition: cm.h:191
Definition: cm.h:166
static uint64_t cm_ag_ut_local_cp_nr(const struct m0_cm_aggr_group *ag)
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
int(* cmo_setup)(struct m0_cm *cm)
Definition: cm.h:292
struct m0_uint128 ai_hi
Definition: ag.h:50
struct m0_uint128 ai_lo
Definition: ag.h:51
struct m0_reqh_service_type ct_stype
Definition: cm.h:145
#define M0_CNT_DEC(cnt)
Definition: arith.h:219
static int cm_ut_ag_next(struct m0_cm *cm, const struct m0_cm_ag_id *id_curr, struct m0_cm_ag_id *id_next)
#define out(...)
Definition: gen.c:41
static int cm_ut_setup(struct m0_cm *cm)
struct m0_fid gfid
Definition: dir.c:626
struct m0_cm_cp cm_ut_cp
uint64_t u_lo
Definition: types.h:37
static void cm_ut_service_stop(struct m0_reqh_service *service)
static const struct m0_cm_cp_ops cm_cp_ut_ops
M0_INTERNAL void m0_chan_fini_lock(struct m0_chan *chan)
Definition: chan.c:112
static struct m0_cm_cp * cm_ut_cp_alloc(struct m0_cm *cm)
static struct m0_reqh_service * service[REQH_IN_UT_MAX]
Definition: long_lock_ut.c:46
static const struct m0_cm_ops cm_ut_ops
int32_t rc
Definition: trigger_fop.h:47
Definition: ag.h:49
static void cm_cp_ut_free(struct m0_cm_cp *cp)
const struct m0_reqh_service_ops * rs_ops
Definition: reqh_service.h:254
static int cm_ut_start(struct m0_cm *cm)
const struct m0_cm_aggr_group_ops cm_ag_ut_ops