Motr  M0
cob.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 "ut/ut.h"
24 #include "lib/ub.h"
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "lib/memory.h"
27 #include "lib/bitstring.h"
28 
29 #include "be/ut/helper.h"
30 #include "be/seg.h"
31 #include "cob/cob.h"
32 #include "lib/locality.h"
33 
34 static const char test_name[] = "hello_world";
35 static const char add_name[] = "add_name";
36 static const char wrong_name[] = "wrong_name";
37 static struct m0_cob_domain_id id = { 42 };
38 static struct m0_be_ut_backend ut_be;
39 static struct m0_sm_group *grp;
40 static struct m0_cob_domain *dom;
41 static struct m0_cob *cob;
42 
43 static int ut_init(void)
44 {
45  return 0;
46 }
47 
48 static int ut_fini(void)
49 {
50  return 0;
51 }
52 
53 static void ut_tx_open(struct m0_be_tx *tx, struct m0_be_tx_credit *credit)
54 {
55  int rc;
56 
57  m0_be_ut_tx_init(tx, &ut_be);
58  m0_be_tx_prep(tx, credit);
59  rc = m0_be_tx_open_sync(tx);
60  M0_UT_ASSERT(rc == 0);
62 }
63 
64 static int _locate(int c, int k)
65 {
66  struct m0_fid fid;
67  struct m0_cob_oikey oikey;
68  int rc;
69 
70  m0_fid_set(&fid, c, k);
71 
72  oikey.cok_fid = fid;
73  oikey.cok_linkno = 0;
74 
75  rc = m0_cob_locate(dom, &oikey, 0, &cob);
76 
77  return rc;
78 }
79 
80 static void test_mkfs(void)
81 {
82  struct m0_be_tx tx_;
83  struct m0_be_tx *tx = &tx_;
84  struct m0_be_tx_credit accum = {};
85  int rc;
86  struct m0_be_seg *seg0;
87 
88  M0_SET0(&ut_be);
91 
93  rc = m0_cob_domain_create(&dom, grp, &id, &ut_be.but_dom, seg0); /*XXX*/
94  M0_UT_ASSERT(rc == 0);
95 
97  ut_tx_open(tx, &accum);
98 
99  /* Create root and other structures */
101  M0_UT_ASSERT(rc == 0);
102 
104  M0_MDSERVICE_SLASH_FID.f_key); /* slash */
105  M0_UT_ASSERT(rc == 0);
107  M0_COB_ROOT_FID.f_key); /* root */
108  M0_UT_ASSERT(rc != 0);
109 
111  m0_be_tx_fini(tx);
112 
114 
116 }
117 
118 static void test_init(void)
119 {
120  struct m0_be_seg *seg0;
121  int rc;
122 
123  M0_SET0(&ut_be);
125 
127 
129  M0_UT_ASSERT(rc == 0);
130 }
131 
132 static void test_fini(void)
133 {
134  int rc;
135 
138  M0_UT_ASSERT(rc == 0);
139 
141 }
142 
143 static void test_create(void)
144 {
145  struct m0_be_tx_credit accum = {};
146  struct m0_cob_nskey *key;
147  struct m0_cob_nsrec nsrec;
148  struct m0_cob_fabrec *fabrec;
149  struct m0_cob_omgrec omgrec;
150  struct m0_fid pfid;
151  struct m0_be_tx tx_;
152  struct m0_be_tx *tx = &tx_;
153  int rc;
154 
155  M0_SET0(&nsrec);
156  M0_SET0(&omgrec);
157 
158  /* pfid, filename */
159  m0_fid_set(&pfid, 0x123, 0x456);
160  m0_cob_nskey_make(&key, &pfid, test_name, strlen(test_name));
161 
162  m0_cob_nsrec_init(&nsrec);
163  m0_fid_set(&nsrec.cnr_fid, 0xabc, 0xdef);
164  nsrec.cnr_nlink = 0;
165 
166  rc = m0_cob_alloc(dom, &cob);
167  M0_UT_ASSERT(rc == 0);
168  m0_cob_fabrec_make(&fabrec, NULL, 0);
169  /* Set pool version. */
170  fabrec->cfb_pver = M0_FID_TINIT('v', 1, 24);
174  ut_tx_open(tx, &accum);
175  rc = m0_cob_create(cob, key, &nsrec, fabrec, &omgrec, tx);
176  M0_UT_ASSERT(rc == 0);
177  rc = m0_cob_create(cob, key, &nsrec, fabrec, &omgrec, tx);
178  M0_UT_ASSERT(rc == -EEXIST);
179 
180  ++nsrec.cnr_nlink;
181  rc = m0_cob_update(cob, &nsrec, NULL, NULL, tx);
182  M0_UT_ASSERT(rc == 0);
183  m0_cob_put(cob);
184 
186  m0_be_tx_fini(tx);
187 }
188 
189 static void test_add_name(void)
190 {
191  struct m0_cob_nskey *nskey;
192  struct m0_fid pfid;
193  struct m0_be_tx tx_;
194  struct m0_be_tx *tx = &tx_;
195  int rc;
196  struct m0_be_tx_credit accum = {};
197 
198  /* pfid, filename */
199  m0_fid_set(&pfid, 0x123, 0x456);
200 
202  ut_tx_open(tx, &accum);
203 
204  /* lookup for cob created before using @test_name. */
205  m0_cob_nskey_make(&nskey, &pfid, test_name, strlen(test_name));
206  rc = m0_cob_lookup(dom, nskey, M0_CA_NSKEY_FREE, &cob);
207  M0_UT_ASSERT(rc == 0);
208 
209  /* add new name to existing cob */
210  m0_cob_nskey_make(&nskey, &pfid, add_name, strlen(add_name));
212  rc = m0_cob_name_add(cob, nskey, &cob->co_nsrec, tx);
213  M0_UT_ASSERT(rc == 0);
214  m0_cob_put(cob);
215 
216  /* lookup for new name */
217  rc = m0_cob_lookup(dom, nskey, 0, &cob);
218  M0_UT_ASSERT(rc == 0);
219  m0_cob_put(cob);
220  m0_free(nskey);
221 
222  /* lookup for wrong name, should fail. */
223  m0_cob_nskey_make(&nskey, &pfid, wrong_name, strlen(wrong_name));
224  rc = m0_cob_lookup(dom, nskey, 0, &cob);
225  M0_UT_ASSERT(rc != 0);
226  m0_free(nskey);
227 
229  m0_be_tx_fini(tx);
230 }
231 
232 static void test_del_name(void)
233 {
234  struct m0_cob_nskey *nskey;
235  struct m0_fid pfid;
236  struct m0_be_tx tx_;
237  struct m0_be_tx *tx = &tx_;
238  int rc;
239  struct m0_be_tx_credit accum = {};
240 
241  /* pfid, filename */
242  m0_fid_set(&pfid, 0x123, 0x456);
243 
245  ut_tx_open(tx, &accum);
246 
247  /* lookup for cob created before using @test_name. */
248  m0_cob_nskey_make(&nskey, &pfid, test_name, strlen(test_name));
249  rc = m0_cob_lookup(dom, nskey, M0_CA_NSKEY_FREE, &cob);
250  M0_UT_ASSERT(rc == 0);
251 
252  /* del name that we created in prev test */
253  m0_cob_nskey_make(&nskey, &pfid, add_name, strlen(add_name));
254  rc = m0_cob_name_del(cob, nskey, tx);
255  M0_UT_ASSERT(rc == 0);
256  m0_cob_put(cob);
257 
258  /* lookup for new name */
259  rc = m0_cob_lookup(dom, nskey, 0, &cob);
260  M0_UT_ASSERT(rc != 0);
261  m0_free(nskey);
262 
264  m0_be_tx_fini(tx);
265 }
266 
268 static void test_lookup(void)
269 {
270  struct m0_cob_nskey *nskey;
271  struct m0_fid pfid;
272  int rc;
273 
274  m0_fid_set(&pfid, 0x123, 0x456);
275  m0_cob_nskey_make(&nskey, &pfid, test_name, strlen(test_name));
276  rc = m0_cob_lookup(dom, nskey, M0_CA_NSKEY_FREE, &cob);
277  M0_UT_ASSERT(rc == 0);
278  M0_UT_ASSERT(cob != NULL);
279  M0_UT_ASSERT(cob->co_dom == dom);
283 
284  /* We should have cached the key also, unless oom */
286 
287  m0_cob_put(cob);
288 }
289 
291 static void test_locate(void)
292 {
293  int rc;
294 
295  rc = _locate(0xabc, 0xdef);
296  M0_UT_ASSERT(rc == 0);
297 
298  M0_UT_ASSERT(cob != NULL);
299  M0_UT_ASSERT(cob->co_dom == dom);
300 
301  /* We should have saved the NSKEY */
304  M0_UT_ASSERT(cob->co_nskey->cnk_pfid.f_key == 0x456);
305 
306  /* Assuming we looked up the NSREC at the same time */
308 
309  m0_cob_put(cob);
310 
311  /* We should fail here, since there is no such cob */
312  rc = _locate(0x123, 0x456);
313  M0_UT_ASSERT(rc != 0);
314 }
315 
316 static void test_delete(void)
317 {
318  struct m0_be_tx tx_;
319  struct m0_be_tx *tx = &tx_;
320  int rc;
321  struct m0_be_tx_credit accum = {};
322 
323  /* gets ref */
324  rc = _locate(0xabc, 0xdef);
325  M0_UT_ASSERT(rc == 0);
326 
328  ut_tx_open(tx, &accum);
329  rc = m0_cob_delete_put(cob, tx);
331  m0_be_tx_fini(tx);
332 
333  M0_UT_ASSERT(rc == 0);
334 
335  /* should fail now */
336  rc = _locate(0xabc, 0xdef);
337  M0_UT_ASSERT(rc != 0);
338 }
339 
340 struct m0_ut_suite cob_ut = {
341  .ts_name = "cob-ut",
342  .ts_init = ut_init,
343  .ts_fini = ut_fini,
344  .ts_tests = {
345  { "cob-mkfs", test_mkfs },
346  { "cob-init", test_init },
347  { "cob-create", test_create },
348  { "cob-lookup", test_lookup },
349  { "cob-locate", test_locate },
350  { "cob-add-name", test_add_name },
351  { "cob-del-name", test_del_name },
352  { "cob-delete", test_delete },
353  { "cob-fini", test_fini },
354  { NULL, NULL }
355  }
356 };
357 
358 
359 /*
360  * Local variables:
361  * c-indentation-style: "K&R"
362  * c-basic-offset: 8
363  * tab-width: 8
364  * fill-column: 80
365  * scroll-step: 1
366  * End:
367  */
struct m0_cob_nsrec co_nsrec
Definition: cob.h:590
uint32_t cnr_nlink
Definition: cob.h:426
static void test_init(void)
Definition: cob.c:118
static struct m0_be_ut_backend ut_be
Definition: cob.c:38
Definition: cob.h:581
M0_INTERNAL int m0_be_ut_backend_init_cfg(struct m0_be_ut_backend *ut_be, const struct m0_be_domain_cfg *cfg, bool mkfs)
Definition: stubs.c:231
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
static void test_create(void)
Definition: cob.c:143
#define NULL
Definition: misc.h:38
uint64_t co_flags
Definition: cob.h:585
m0_be_tx_state
Definition: tx.h:214
static const char add_name[]
Definition: cob.c:35
static const char test_name[]
Definition: cob.c:34
struct m0_cob_domain * co_dom
Definition: cob.h:582
M0_INTERNAL void m0_be_tx_fini(struct m0_be_tx *tx)
Definition: stubs.c:163
void m0_cob_domain_fini(struct m0_cob_domain *dom)
Definition: cob.c:726
static int _locate(int c, int k)
Definition: cob.c:64
M0_INTERNAL struct m0_be_seg * m0_be_domain_seg0_get(struct m0_be_domain *dom)
Definition: domain.c:466
M0_INTERNAL void m0_cob_put(struct m0_cob *cob)
Definition: cob.c:1095
M0_INTERNAL void m0_be_tx_prep(struct m0_be_tx *tx, const struct m0_be_tx_credit *credit)
Definition: stubs.c:175
int m0_cob_domain_create(struct m0_cob_domain **dom, struct m0_sm_group *grp, const struct m0_cob_domain_id *cdid, struct m0_be_domain *bedom, struct m0_be_seg *seg)
Definition: cob.c:840
static const char wrong_name[]
Definition: cob.c:36
M0_INTERNAL int m0_cob_fabrec_make(struct m0_cob_fabrec **rech, const char *link, size_t linklen)
Definition: cob.c:262
#define M0_SET0(obj)
Definition: misc.h:64
static void test_locate(void)
Definition: cob.c:291
Definition: ut.h:77
uint32_t cnr_cntr
Definition: cob.h:427
M0_INTERNAL int m0_cob_nskey_make(struct m0_cob_nskey **keyh, const struct m0_fid *pfid, const char *name, size_t namelen)
Definition: cob.c:148
struct m0_fid fid
Definition: di.c:46
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
struct m0_cob_nskey * co_nskey
Definition: cob.h:588
uint32_t cnr_linkno
Definition: cob.h:420
M0_INTERNAL void m0_fid_set(struct m0_fid *fid, uint64_t container, uint64_t key)
Definition: fid.c:116
M0_INTERNAL int m0_cob_delete_put(struct m0_cob *cob, struct m0_be_tx *tx)
Definition: cob.c:1853
M0_INTERNAL const struct m0_fid M0_MDSERVICE_SLASH_FID
Definition: md_fid.c:63
uint32_t cok_linkno
Definition: cob.h:458
struct m0_ut_suite cob_ut
Definition: cob.c:340
static int key
Definition: locality.c:283
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
static int ut_init(void)
Definition: cob.c:43
M0_INTERNAL int m0_cob_name_add(struct m0_cob *cob, struct m0_cob_nskey *nskey, struct m0_cob_nsrec *nsrec, struct m0_be_tx *tx)
Definition: cob.c:1924
static void test_mkfs(void)
Definition: cob.c:80
struct m0_fid cok_fid
Definition: cob.h:457
static struct m0_cob_domain * dom
Definition: cob.c:40
static struct m0_addb2_callback c
Definition: consumer.c:41
static int ut_fini(void)
Definition: cob.c:48
M0_INTERNAL void m0_cob_nsrec_init(struct m0_cob_nsrec *nsrec)
Definition: cob.c:2058
int m0_cob_domain_destroy(struct m0_cob_domain *dom, struct m0_sm_group *grp, struct m0_be_domain *bedom)
Definition: cob.c:871
M0_INTERNAL int m0_cob_create(struct m0_cob *cob, struct m0_cob_nskey *nskey, struct m0_cob_nsrec *nsrec, struct m0_cob_fabrec *fabrec, struct m0_cob_omgrec *omgrec, struct m0_be_tx *tx)
Definition: cob.c:1681
M0_INTERNAL int m0_be_tx_open_sync(struct m0_be_tx *tx)
Definition: stubs.c:199
static void test_del_name(void)
Definition: cob.c:232
uint64_t f_container
Definition: fid.h:39
M0_INTERNAL int m0_cob_lookup(struct m0_cob_domain *dom, struct m0_cob_nskey *nskey, uint64_t flags, struct m0_cob **out)
Definition: cob.c:1371
struct m0_sm_group * m0_be_ut_backend_sm_group_lookup(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:277
Definition: seg.h:66
struct m0_be_domain but_dom
Definition: helper.h:47
int m0_cob_domain_init(struct m0_cob_domain *dom, struct m0_be_seg *seg)
Definition: cob.c:708
const char * ts_name
Definition: ut.h:99
struct m0_fid cfb_pver
Definition: cob.h:478
Definition: fid.h:38
static struct m0_cob_domain_id id
Definition: cob.c:37
uint64_t f_key
Definition: fid.h:40
void m0_be_ut_tx_init(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be)
Definition: stubs.c:286
static struct m0_be_seg * seg0
Definition: active_record.c:31
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
M0_INTERNAL int m0_cob_update(struct m0_cob *cob, struct m0_cob_nsrec *nsrec, struct m0_cob_fabrec *fabrec, struct m0_cob_omgrec *omgrec, struct m0_be_tx *tx)
Definition: cob.c:1860
static void ut_tx_open(struct m0_be_tx *tx, struct m0_be_tx_credit *credit)
Definition: cob.c:53
static void test_add_name(void)
Definition: cob.c:189
M0_INTERNAL int m0_cob_locate(struct m0_cob_domain *dom, struct m0_cob_oikey *oikey, uint64_t flags, struct m0_cob **out)
Definition: cob.c:1407
static void test_lookup(void)
Definition: cob.c:268
M0_INTERNAL int m0_cob_name_del(struct m0_cob *cob, struct m0_cob_nskey *nskey, struct m0_be_tx *tx)
Definition: cob.c:1966
M0_INTERNAL int m0_cob_alloc(struct m0_cob_domain *dom, struct m0_cob **out)
Definition: cob.c:1100
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
struct m0_fid cnk_pfid
Definition: cob.h:391
static void test_delete(void)
Definition: cob.c:316
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static struct m0_cob * cob
Definition: cob.c:41
M0_INTERNAL const struct m0_fid M0_COB_ROOT_FID
Definition: md_fid.c:39
static struct m0_sm_group * grp
Definition: cob.c:39
M0_INTERNAL void m0_be_tx_close_sync(struct m0_be_tx *tx)
Definition: stubs.c:205
Definition: tx.h:280
static void test_fini(void)
Definition: cob.c:132
Definition: idx_mock.c:47
struct m0_fid cnr_fid
Definition: cob.h:419