Motr  M0
mdstore.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 <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 
28 #include "lib/ub.h"
29 #include "lib/memory.h"
30 #include "lib/errno.h" /* ENOENT */
31 #include "lib/misc.h" /* M0_SET0 */
32 #include "lib/bitstring.h"
33 #include "lib/processor.h"
34 #include "fop/fop.h"
35 #include "cob/cob.h"
36 #include "reqh/reqh.h"
37 #include "mdstore/mdstore.h"
38 #include "net/net.h"
39 #include "ut/ut.h"
40 #include "ut/be.h"
41 #include "ut/stob.h" /* m0_ut_stob_linux_get_by_key */
42 #include "be/ut/helper.h"
43 #include "mdservice/fsync_foms.c"
44 
45 #include "mdservice/ut/mdstore.h"
46 #include "mdservice/ut/lustre.h"
47 
48 static struct m0_cob_domain_id id = { 42 };
49 
50 static struct m0_sm_group *grp;
51 static struct m0_be_ut_backend ut_be;
52 static struct m0_be_seg *be_seg;
53 
54 static struct m0_mdstore md;
55 static struct m0_reqh reqh;
56 static struct m0_reqh_service *mdservice;
57 extern struct m0_reqh_service_type m0_mds_type;
59 
60 int m0_md_lustre_fop_alloc(struct m0_fop **fop, void *data,
61  struct m0_rpc_machine *mach);
62 
63 static struct m0_semaphore inflight;
64 static int error = 0;
65 
66 static void (*orig_fom_fini)(struct m0_fom *fom) = NULL;
67 static int (*orig_fom_create)(struct m0_fop *fop, struct m0_fom **m,
68  struct m0_reqh *reqh);
69 
70 static void fom_fini(struct m0_fom *fom)
71 {
73 
74  if (error == 0)
75  error = m0_fom_rc(fom);
78 }
79 
80 static int fom_create(struct m0_fop *fop, struct m0_fom **m,
81  struct m0_reqh *reqh)
82 {
83  int rc;
84 
86  if (rc == 0) {
87  M0_UT_ASSERT(*m != NULL);
88  (*m)->fo_local = true;
89  }
90  return rc;
91 }
92 
93 static void test_mkfs(void)
94 {
95  struct m0_md_lustre_fid testroot;
96  struct m0_fid rootfid;
97  struct m0_be_tx tx;
98  struct m0_be_tx_credit cred = {};
99  int fd;
100  int rc;
101 
102  M0_SET0(&ut_be);
104  rc = M0_REQH_INIT(&reqh,
105  .rhia_db = be_seg,
106  .rhia_mdstore = &md,
107  .rhia_fid = &g_process_fid,
108  );
109  M0_UT_ASSERT(rc == 0);
112 
114 
115  fd = open(M0_MDSTORE_OPS_DUMP_PATH, O_RDONLY);
116  M0_UT_ASSERT(fd > 0);
117 
118  rc = read(fd, &testroot, sizeof(testroot));
119  M0_UT_ASSERT(rc == sizeof(testroot));
120  close(fd);
121 
122  md.md_dom = NULL;
123  rc = m0_mdstore_init(&md, be_seg, false);
124  M0_UT_ASSERT(rc == -ENOENT);
126  M0_UT_ASSERT(rc == 0);
127 
128  /* Create root and other structures */
130  m0_ut_be_tx_begin(&tx, &ut_be, &cred);
131  m0_fid_set(&rootfid, testroot.f_seq, testroot.f_oid);
132  rc = m0_cob_domain_mkfs(md.md_dom, &rootfid, &tx);
133  M0_UT_ASSERT(rc == 0);
134  m0_ut_be_tx_end(&tx);
135 
136  /* Fini old mdstore */
138 
139  /* Init mdstore with root init flag set to 1 */
140  rc = m0_mdstore_init(&md, be_seg, true);
141  M0_UT_ASSERT(rc == 0);
142 
143  /* Fini everything */
145 }
146 
147 extern void (*m0_md_req_fom_fini_func)(struct m0_fom *fom);
148 extern struct m0_fom_type_ops m0_md_fom_ops;
149 
150 static void test_init(void)
151 {
152  int rc;
153 
154  /* Patch md fom operations vector to overwrite finaliser. */
157  /* Patch fom creation routine. */
160  rc = m0_mdstore_init(&md, be_seg, true);
161  M0_UT_ASSERT(rc == 0);
162 
164  M0_UT_ASSERT(rc == 0);
167 
169 }
170 
171 static void test_fini(void)
172 {
173  int rc;
174 
176  M0_UT_ASSERT(rc == 0);
177 
181 
184 
186  M0_REQH_ST_INIT)))
188 
191 
194  m0_reqh_fini(&reqh);
195 }
196 
197 enum { REC_NR = 128 };
198 
199 static void test_mdops(void)
200 {
201  struct m0_md_lustre_logrec *rec;
202  struct m0_md_lustre_fid root;
203  int fd;
204  int result;
205  int size;
206  struct m0_fop *fop;
207  int i;
208 
209  fd = open(M0_MDSTORE_OPS_DUMP_PATH, O_RDONLY);
210  M0_UT_ASSERT(fd > 0);
211 
212  result = read(fd, &root, sizeof(root));
213  M0_UT_ASSERT(result == sizeof(root));
214  error = 0;
215 
217  for (i = 0; i < REC_NR; ++i) {
218  fop = NULL;
219  do {
220  result = read(fd, &size, sizeof(size));
221  if (result < sizeof(size))
222  goto end;
223  rec = m0_alloc(size);
224  M0_UT_ASSERT(rec != NULL);
225  result = read(fd, rec, size);
226  M0_UT_ASSERT(result == size);
227  result = m0_md_lustre_fop_alloc(&fop, rec, &machine);
228  m0_free(rec);
229  /* Let's get second part of rename fop. */
230  } while (result == -EAGAIN);
231  if (result == -EOPNOTSUPP)
232  continue;
235 
236  /* Process fops one by one sequentially. */
238  }
239  end:
240  close(fd);
241  /* Make sure that all fops are handled. */
243  M0_UT_ASSERT(error == 0);
245 }
246 
247 static void ut_fsync_create_fom(void)
248 {
249  struct m0_fop_fsync *ffop;
250  struct m0_fop *fop;
251  struct m0_fop *rep;
252  struct m0_fom *fom;
253  int rc;
254 
255  /* make up an rpc machine to use */
257 
259  M0_UT_ASSERT(fop != NULL);
260 
261  ffop = m0_fop_data(fop);
262  M0_UT_ASSERT(ffop != NULL);
263  ffop->ff_be_remid.tri_txid = 666;
264  ffop->ff_be_remid.tri_locality = 1;
265 
266  /* the type for the request is right */
268 
269  /* check reqh has is associated with the mds service */
272 
273  /* create a fom for the fsync fop request */
277  M0_UT_ASSERT(rc == 0);
278  M0_UT_ASSERT(fom != NULL);
279 
280  /* check the fom returned */
281  rep = fom->fo_rep_fop;
282  M0_UT_ASSERT(rep != NULL);
283  M0_UT_ASSERT(fom->fo_fop == fop);
284  /* check the types of the fop request and reply */
285  M0_UT_ASSERT(fom->fo_fop->f_type == &m0_fop_fsync_mds_fopt);
286  M0_UT_ASSERT(fom->fo_ops == &fsync_fom_ops);
288 
289  /* test fom's ops */
291 
292  /* release (the kraken!) */
293  m0_free(fop);
294 }
295 
297  .ts_name = "mdservice-ut",
298  .ts_tests = {
299  { "mdservice-mkfs", test_mkfs },
300  { "mdservice-init", test_init },
301  { "mdservice-ops", test_mdops },
302  { "mdservice-fsync", ut_fsync_create_fom},
303  { "mdservice-fini", test_fini },
304  { NULL, NULL }
305  }
306 };
307 
308 /*
309  * Local variables:
310  * c-indentation-style: "K&R"
311  * c-basic-offset: 8
312  * tab-width: 8
313  * fill-column: 80
314  * scroll-step: 1
315  * End:
316  */
M0_INTERNAL void m0_mdstore_fini(struct m0_mdstore *md)
Definition: mdstore.c:130
M0_INTERNAL int m0_reqh_service_start(struct m0_reqh_service *service)
Definition: reqh_service.c:343
struct m0_cob_domain * md_dom
Definition: mdstore.h:58
static struct m0_semaphore inflight
Definition: mdstore.c:63
M0_INTERNAL int m0_cob_domain_mkfs(struct m0_cob_domain *dom, const struct m0_fid *rootfid, struct m0_be_tx *tx)
Definition: cob.c:980
M0_INTERNAL void m0_reqh_service_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:402
static void fom_fini(struct m0_fom *fom)
Definition: mdstore.c:70
#define NULL
Definition: misc.h:38
static struct m0_be_seg * be_seg
Definition: mdstore.c:52
static struct m0_addb2_mach * m
Definition: consumer.c:38
uint32_t f_oid
Definition: lustre.h:35
struct m0_fop_type m0_fop_fsync_rep_fopt
Definition: fsync_fops.c:39
M0_INTERNAL int m0_mdstore_init(struct m0_mdstore *md, struct m0_be_seg *db, bool init_root)
Definition: mdstore.c:78
#define M0_REQH_INIT(reqh,...)
Definition: reqh.h:262
M0_INTERNAL int m0_reqh_fop_handle(struct m0_reqh *reqh, struct m0_fop *fop)
Definition: reqh.c:546
M0_INTERNAL void m0_reqh_service_prepare_to_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:375
uint64_t f_seq
Definition: lustre.h:34
static void test_mkfs(void)
Definition: mdstore.c:93
static const struct m0_fom_ops fsync_fom_ops
Definition: fsync_foms.c:51
int(* fto_create)(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: fom.h:650
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL struct m0_be_seg * m0_be_domain_seg0_get(struct m0_be_domain *dom)
Definition: domain.c:466
M0_INTERNAL void m0_ut_be_tx_end(struct m0_be_tx *tx)
Definition: be.c:82
static struct m0_cob_domain_id id
Definition: mdstore.c:48
void * m0_fop_data(const struct m0_fop *fop)
Definition: fop.c:220
struct m0_reqh_service_type m0_mds_type
Definition: md_service.c:72
static int error
Definition: mdstore.c:64
static struct m0_addb2_mach * mach
Definition: storage.c:42
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
M0_INTERNAL void m0_reqh_fini(struct m0_reqh *reqh)
Definition: reqh.c:320
struct m0_fop_type m0_fop_fsync_mds_fopt
Definition: fsync_fops.c:33
struct m0_fop_getxattr_rep * rep
Definition: dir.c:455
M0_INTERNAL void m0_sm_group_fini(struct m0_sm_group *grp)
Definition: sm.c:65
struct m0_fom_type ft_fom_type
Definition: fop.h:232
static void test_init(void)
Definition: mdstore.c:150
struct m0_sm_group rm_sm_grp
Definition: rpc_machine.h:82
M0_INTERNAL void m0_reqh_shutdown_wait(struct m0_reqh *reqh)
Definition: reqh.c:647
M0_INTERNAL int m0_fsync_req_fom_create(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: fsync_foms.c:330
M0_INTERNAL void m0_cob_tx_credit(struct m0_cob_domain *dom, enum m0_cob_op optype, struct m0_be_tx_credit *accum)
Definition: cob.c:2281
static struct m0_reqh_service * mdservice
Definition: mdstore.c:56
struct m0_reqh * bec_reqh
Definition: engine.h:84
M0_INTERNAL void m0_sm_group_unlock(struct m0_sm_group *grp)
Definition: sm.c:96
int i
Definition: dir.c:1033
static void(* orig_fom_fini)(struct m0_fom *fom)
Definition: mdstore.c:66
struct m0_fop_type * f_type
Definition: fop.h:81
void(* m0_md_req_fom_fini_func)(struct m0_fom *fom)
Definition: md_foms.c:1326
struct m0_ut_suite mdservice_ut
Definition: mdstore.c:296
M0_INTERNAL void m0_fid_set(struct m0_fid *fid, uint64_t container, uint64_t key)
Definition: fid.c:116
struct m0_conf_root * root
Definition: note.c:50
struct m0_fom_type_ops m0_md_fom_ops
Definition: md_fops.c:44
static void ut_fsync_create_fom(void)
Definition: mdstore.c:247
M0_INTERNAL void m0_ut_be_tx_begin(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be, struct m0_be_tx_credit *cred)
Definition: be.c:56
static int(* orig_fom_create)(struct m0_fop *fop, struct m0_fom **m, struct m0_reqh *reqh)
Definition: mdstore.c:67
#define M0_ASSERT(cond)
M0_INTERNAL void m0_sm_group_init(struct m0_sm_group *grp)
Definition: sm.c:53
M0_INTERNAL void m0_reqh_service_fini(struct m0_reqh_service *service)
Definition: reqh_service.c:457
void m0_be_ut_backend_init(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:238
#define M0_MDSTORE_OPS_DUMP_PATH
Definition: mdstore.h:5
void * m0_alloc(size_t size)
Definition: memory.c:126
static struct m0_sm_group * grp
Definition: mdstore.c:50
static void test_fini(void)
Definition: mdstore.c:171
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
Definition: reqh.h:94
struct m0_sm_group * m0_be_ut_backend_sm_group_lookup(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:277
Definition: dump.c:103
M0_INTERNAL void m0_reqh_post_storage_fini_svcs_stop(struct m0_reqh *reqh)
Definition: reqh.c:704
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
uint64_t tri_txid
Definition: tx.h:431
struct m0_rpc_machine machine
Definition: mdstore.c:58
Definition: seg.h:66
struct m0_be_domain but_dom
Definition: helper.h:47
uint64_t tri_locality
Definition: tx.h:432
static size_t fsync_fom_locality_get(const struct m0_fom *fom)
Definition: fsync_foms.c:308
M0_INTERNAL int m0_reqh_state_get(struct m0_reqh *reqh)
Definition: reqh.c:398
Definition: fom.h:481
struct m0_be_domain_cfg but_dom_cfg
Definition: helper.h:53
const char * ts_name
Definition: ut.h:99
static int fom_create(struct m0_fop *fop, struct m0_fom **m, struct m0_reqh *reqh)
Definition: mdstore.c:80
M0_INTERNAL void m0_reqh_start(struct m0_reqh *reqh)
Definition: reqh.c:711
static struct m0_mdstore md
Definition: mdstore.c:54
M0_INTERNAL struct m0_reqh_service * m0_reqh_service_find(const struct m0_reqh_service_type *st, const struct m0_reqh *reqh)
Definition: reqh_service.c:538
Definition: fid.h:38
M0_INTERNAL int m0_mdstore_create(struct m0_mdstore *md, struct m0_sm_group *grp, struct m0_cob_domain_id *id, struct m0_be_domain *bedom, struct m0_be_seg *db)
Definition: mdstore.c:135
struct m0_be_tx_remid ff_be_remid
Definition: fsync_fops.h:48
M0_INTERNAL int m0_fom_rc(const struct m0_fom *fom)
Definition: fom.c:1727
m0_bcount_t size
Definition: di.c:39
void m0_fop_put_lock(struct m0_fop *fop)
Definition: fop.c:199
M0_INTERNAL void m0_reqh_pre_storage_fini_svcs_stop(struct m0_reqh *reqh)
Definition: reqh.c:681
static struct m0_fop * fop
Definition: item.c:57
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
static struct m0_be_ut_backend ut_be
Definition: mdstore.c:51
M0_INTERNAL void m0_reqh_idle_wait_for(struct m0_reqh *reqh, struct m0_reqh_service *service)
Definition: reqh.c:591
M0_INTERNAL void m0_sm_group_lock(struct m0_sm_group *grp)
Definition: sm.c:83
static void test_mdops(void)
Definition: mdstore.c:199
struct m0t1fs_filedata * fd
Definition: dir.c:1030
M0_INTERNAL int m0_mdstore_destroy(struct m0_mdstore *md, struct m0_sm_group *grp, struct m0_be_domain *bedom)
Definition: mdstore.c:146
M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
Definition: semaphore.c:49
int m0_md_lustre_fop_alloc(struct m0_fop **fop, void *data, struct m0_rpc_machine *mach)
Definition: lustre.c:205
M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
Definition: semaphore.c:65
void m0_free(void *data)
Definition: memory.c:146
const struct m0_reqh_service_type * ft_rstype
Definition: fom.h:617
struct m0_be_engine_cfg bc_engine
Definition: domain.h:79
int32_t rc
Definition: trigger_fop.h:47
static struct m0_reqh reqh
Definition: mdstore.c:55
struct m0_fid g_process_fid
Definition: ut.c:689
#define M0_UT_ASSERT(a)
Definition: ut.h:46
Definition: fop.h:79
Definition: tx.h:280
struct m0_fop * m0_fop_alloc(struct m0_fop_type *fopt, void *data, struct m0_rpc_machine *mach)
Definition: fop.c:96