Motr  M0
null.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-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 "stob/null.h"
24 
25 #include "lib/errno.h"
26 #include "lib/memory.h" /* M0_ALLOC_PTR */
27 #include "lib/tlist.h" /* m0_tl */
28 #include "lib/mutex.h" /* m0_mutex */
29 #include "lib/misc.h" /* M0_SET0 */
30 #include "lib/string.h" /* m0_strdup */
31 
32 #include "fid/fid.h"
33 
34 #include "stob/type.h"
35 #include "stob/domain.h"
36 #include "stob/stob.h"
37 
38 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_STOB
39 #include "lib/trace.h"
40 
46 enum {
48 };
49 
50 struct stob_null_lists;
51 struct stob_null_domain;
52 
53 struct stob_null {
56  struct m0_tlink sn_link;
57  uint64_t sn_magic;
58 };
59 
63  char *snd_path;
64  uint64_t snd_dom_key;
66  uint64_t snd_magic;
67  struct m0_tl snd_stobs;
69 };
70 
74 };
75 
76 M0_TL_DESCR_DEFINE(stob_null_stobs, "list of null stobs in the domain",
77  static, struct stob_null, sn_link, sn_magic,
79 M0_TL_DEFINE(stob_null_stobs, static, struct stob_null);
80 
81 M0_TL_DESCR_DEFINE(stob_null_domains, "list of null stob domains",
82  static, struct stob_null_domain, snd_link, snd_magic,
84 M0_TL_DEFINE(stob_null_domains, static, struct stob_null_domain);
85 
86 /* export */
88 
91 static struct m0_stob_ops stob_null_ops;
92 
94 {
95  struct stob_null_lists *snl;
96 
97  M0_ALLOC_PTR(snl);
98  if (snl != NULL) {
99  stob_null_domains_tlist_init(&snl->snl_domains);
100  m0_mutex_init(&snl->snl_lock);
101  }
102  type->st_private = snl;
103 }
104 
106 {
107  struct stob_null_lists *snl = type->st_private;
108 
109  m0_mutex_fini(&snl->snl_lock);
110  stob_null_domains_tlist_fini(&snl->snl_domains);
111  m0_free0(&type->st_private);
112 }
113 
114 static struct stob_null_domain *
116  const char *path,
117  bool take_lock)
118 {
119  struct stob_null_domain *snd;
120 
121  if (take_lock)
122  m0_mutex_lock(&snl->snl_lock);
123  snd = m0_tl_find(stob_null_domains, snd, &snl->snl_domains,
124  strcmp(snd->snd_path, path) == 0);
125  if (take_lock)
126  m0_mutex_unlock(&snl->snl_lock);
127  return snd;
128 }
129 
130 static int stob_null_domain_add(struct stob_null_domain *snd,
131  struct stob_null_lists *snl)
132 {
133  struct stob_null_domain *snd1;
134  int rc;
135 
136  m0_mutex_lock(&snl->snl_lock);
137  snd1 = stob_null_domain_find(snl, snd->snd_path, false);
138  rc = snd1 != NULL ? -EEXIST : 0;
139  if (rc == 0)
140  stob_null_domains_tlink_init_at(snd, &snl->snl_domains);
141  m0_mutex_unlock(&snl->snl_lock);
142  return M0_RC(rc);
143 }
144 
145 static void stob_null_domain_del(struct stob_null_domain *snd,
146  struct stob_null_lists *snl)
147 {
148  m0_mutex_lock(&snl->snl_lock);
149  stob_null_domains_tlink_del_fini(snd);
150  m0_mutex_unlock(&snl->snl_lock);
151 }
152 
153 static struct stob_null_domain *
155 {
156  return container_of(dom, struct stob_null_domain, snd_dom);
157 }
158 
159 static int stob_null_domain_cfg_init_parse(const char *str_cfg_init,
160  void **cfg_init)
161 {
162  return 0;
163 }
164 
165 static void stob_null_domain_cfg_init_free(void *cfg_init)
166 {
167 }
168 
169 static int stob_null_domain_cfg_create_parse(const char *str_cfg_create,
170  void **cfg_create)
171 {
172  return 0;
173 }
174 
175 static void stob_null_domain_cfg_create_free(void *cfg_create)
176 {
177 }
178 
180  const char *location_data,
181  void *cfg_init,
182  struct m0_stob_domain **out)
183 {
184  struct stob_null_domain *snd;
185  struct stob_null_lists *snl = type->st_private;
186  struct m0_fid dom_id;
187  uint8_t type_id;
188  int rc;
189 
190  rc = snl == NULL ? -ENOMEM : 0;
191  snd = rc == 0 ? stob_null_domain_find(snl, location_data, true) : NULL;
192  rc = rc ?: snd == NULL ? -ENOENT : 0;
193  if (rc == 0) {
194  M0_SET0(&snd->snd_dom);
196 
197  type_id = m0_stob_type_id_get(type);
198  m0_stob_domain__dom_id_make(&dom_id, type_id,
199  0, snd->snd_dom_key);
200  m0_stob_domain__id_set(&snd->snd_dom, &dom_id);
201  }
202  *out = rc == 0 ? &snd->snd_dom : NULL;
203  return M0_RC(rc);
204 }
205 
207 {
208 }
209 
211  const char *location_data,
212  uint64_t dom_key,
213  void *cfg_create)
214 {
215  struct stob_null_domain *snd;
216  struct stob_null_lists *snl = type->st_private;
217  int rc;
218 
219  rc = snl == NULL ? -ENOMEM : 0;
220  snd = rc == 0 ? stob_null_domain_find(snl, location_data, true) : NULL;
221  rc = rc ?: snd != NULL ? -EEXIST : 0;
222  if (rc == 0)
223  M0_ALLOC_PTR(snd);
224  rc = rc ?: snd == NULL ? -ENOMEM : 0;
225  if (rc == 0) {
226  m0_mutex_init(&snd->snd_lock);
227  snd->snd_dom_key = dom_key,
230  0, dom_key);
231  snd->snd_path = m0_strdup(location_data);
232  snd->snd_lists = snl;
233  stob_null_stobs_tlist_init(&snd->snd_stobs);
234 
235  rc = stob_null_domain_add(snd, snl);
236  if (rc != 0) {
237  stob_null_stobs_tlist_fini(&snd->snd_stobs);
238  m0_free(snd->snd_path);
239  m0_mutex_fini(&snd->snd_lock);
240  m0_free(snd);
241  }
242  }
243 
244  return M0_RC(rc);
245 }
246 
248  const char *location_data)
249 {
250  struct stob_null_lists *snl = type->st_private;
251  struct stob_null_domain *snd;
252 
253  snd = stob_null_domain_find(snl, location_data, true);
254  if (snd != NULL) {
255  stob_null_domain_del(snd, snd->snd_lists);
256  stob_null_stobs_tlist_fini(&snd->snd_stobs);
257  m0_free(snd->snd_path);
258  m0_mutex_fini(&snd->snd_lock);
259  m0_free(snd);
260  }
261  return 0;
262 }
263 
264 static struct m0_stob *stob_null_alloc(struct m0_stob_domain *dom,
265  const struct m0_fid *stob_fid)
266 {
267  return m0_alloc(sizeof(struct m0_stob));
268 }
269 
270 static void stob_null_free(struct m0_stob_domain *dom,
271  struct m0_stob *stob)
272 {
273  m0_free(stob);
274 }
275 
276 static int stob_null_cfg_parse(const char *str_cfg_create,
277  void **cfg_create)
278 {
279  return 0;
280 }
281 
282 static void stob_null_cfg_free(void *cfg_create)
283 {
284 }
285 
286 static struct stob_null *stob_null_find(struct stob_null_domain *snd,
287  const struct m0_fid *stob_fid,
288  bool take_lock)
289 {
290  struct stob_null *sn;
291 
292  if (take_lock)
293  m0_mutex_lock(&snd->snd_lock);
294  sn = m0_tl_find(stob_null_stobs, sn, &snd->snd_stobs,
295  m0_fid_cmp(&sn->sn_stob_fid, stob_fid) == 0);
296  if (take_lock)
297  m0_mutex_unlock(&snd->snd_lock);
298  return sn;
299 }
300 
301 static int stob_null_add(struct stob_null *sn, struct stob_null_domain *snd)
302 {
303  struct stob_null *sn1;
304  int rc;
305 
306  m0_mutex_lock(&snd->snd_lock);
307  sn1 = stob_null_find(snd, &sn->sn_stob_fid, false);
308  rc = sn1 != NULL ? -EEXIST : 0;
309  if (sn1 == NULL)
310  stob_null_stobs_tlink_init_at(sn, &snd->snd_stobs);
311  m0_mutex_unlock(&snd->snd_lock);
312  return M0_RC(rc);
313 }
314 
315 static void stob_null_del(struct stob_null *sn, struct stob_null_domain *snd)
316 {
317  m0_mutex_lock(&snd->snd_lock);
318  stob_null_stobs_tlink_del_fini(sn);
319  m0_mutex_unlock(&snd->snd_lock);
320 }
321 
322 static int stob_null_init(struct m0_stob *stob,
323  struct m0_stob_domain *dom,
324  const struct m0_fid *stob_fid)
325 {
327  struct stob_null *sn = stob_null_find(snd, stob_fid, true);
328 
329  stob->so_private = sn;
331 
332  if (sn != NULL)
333  sn->sn_dom = snd;
334 
335  return sn == NULL ? -ENOENT : 0;
336 }
337 
338 static void stob_null_fini(struct m0_stob *stob)
339 {
340 }
341 
343  struct m0_be_tx_credit *accum)
344 {
345 }
346 
347 static int stob_null_create(struct m0_stob *stob,
348  struct m0_stob_domain *dom,
349  struct m0_dtx *dtx,
350  const struct m0_fid *stob_fid,
351  void *cfg)
352 {
354  struct stob_null *sn;
355  int rc;
356 
357  M0_ALLOC_PTR(sn);
358  rc = sn == NULL ? -ENOMEM : 0;
359  if (sn != NULL) {
360  sn->sn_stob_fid = *stob_fid;
361  rc = stob_null_add(sn, snd);
362  if (rc != 0) {
363  m0_free(sn);
364  } else {
365  stob_null_init(stob, dom, stob_fid);
366  }
367  }
368  /* TODO allocate memory for stob-io */
369  return M0_RC(rc);
370 }
371 
373  struct m0_be_tx_credit *accum)
374 {
375 }
376 
377 static int stob_null_destroy(struct m0_stob *stob, struct m0_dtx *dtx)
378 {
379  struct stob_null *sn = stob->so_private;
380 
381  stob_null_del(sn, sn->sn_dom);
382  m0_free(sn);
383  return 0;
384 }
385 
386 static int stob_null_punch(struct m0_stob *stob,
387  struct m0_indexvec *range,
388  struct m0_dtx *dtx)
389 {
390  return 0;
391 }
392 
393 static uint32_t stob_null_block_shift(struct m0_stob *stob)
394 {
395  return 0;
396 }
397 
398 static struct m0_stob_type_ops stob_null_type_ops = {
400  .sto_deregister = &stob_null_type_deregister,
401  .sto_domain_cfg_init_parse = &stob_null_domain_cfg_init_parse,
402  .sto_domain_cfg_init_free = &stob_null_domain_cfg_init_free,
403  .sto_domain_cfg_create_parse = &stob_null_domain_cfg_create_parse,
404  .sto_domain_cfg_create_free = &stob_null_domain_cfg_create_free,
405  .sto_domain_init = &stob_null_domain_init,
406  .sto_domain_create = &stob_null_domain_create,
407  .sto_domain_destroy = &stob_null_domain_destroy,
408 };
409 
412  .sdo_stob_alloc = &stob_null_alloc,
413  .sdo_stob_free = &stob_null_free,
414  .sdo_stob_cfg_parse = &stob_null_cfg_parse,
415  .sdo_stob_cfg_free = &stob_null_cfg_free,
416  .sdo_stob_init = &stob_null_init,
417  .sdo_stob_create_credit = &stob_null_create_credit,
418  .sdo_stob_create = &stob_null_create,
419 };
420 
421 static struct m0_stob_ops stob_null_ops = {
423  .sop_destroy_credit = &stob_null_destroy_credit,
424  .sop_destroy = &stob_null_destroy,
425  .sop_punch = &stob_null_punch,
426  .sop_block_shift = &stob_null_block_shift,
427 };
428 
429 const struct m0_stob_type m0_stob_null_type = {
431  .st_fidt = {
432  .ft_id = STOB_TYPE_NULL,
433  .ft_name = "nullstob",
434  },
435 };
436 
437 #undef M0_TRACE_SUBSYSTEM
438 
439 /*
440  * Local variables:
441  * c-indentation-style: "K&R"
442  * c-basic-offset: 8
443  * tab-width: 8
444  * fill-column: 80
445  * scroll-step: 1
446  * End:
447  */
448 /*
449  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
450  */
struct m0_mutex snd_lock
Definition: null.c:68
static struct m0_stob_type_ops stob_null_type_ops
Definition: null.c:89
Definition: dtm.h:554
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
#define m0_strdup(s)
Definition: string.h:43
void(* sto_register)(struct m0_stob_type *type)
Definition: type.h:73
static void stob_null_free(struct m0_stob_domain *dom, struct m0_stob *stob)
Definition: null.c:270
M0_INTERNAL void m0_stob_domain__dom_id_make(struct m0_fid *dom_id, uint8_t type_id, uint64_t dom_container, uint64_t dom_key)
Definition: domain.c:327
#define NULL
Definition: misc.h:38
struct m0_tl snd_stobs
Definition: null.c:67
const struct m0_stob_ops * so_ops
Definition: stob.h:164
struct m0_tlink snd_link
Definition: null.c:65
static int stob_null_create(struct m0_stob *stob, struct m0_stob_domain *dom, struct m0_dtx *dtx, const struct m0_fid *stob_fid, void *cfg)
Definition: null.c:347
static uint32_t stob_null_block_shift(struct m0_stob *stob)
Definition: null.c:393
static int stob_null_domain_add(struct stob_null_domain *snd, struct stob_null_lists *snl)
Definition: null.c:130
struct m0_tl snl_domains
Definition: null.c:72
static void stob_null_create_credit(struct m0_stob_domain *dom, struct m0_be_tx_credit *accum)
Definition: null.c:342
M0_INTERNAL uint8_t m0_stob_type_id_get(const struct m0_stob_type *type)
Definition: type.c:164
char * snd_path
Definition: null.c:63
void * so_private
Definition: stob.h:175
#define container_of(ptr, type, member)
Definition: misc.h:33
#define M0_SET0(obj)
Definition: misc.h:64
void(* sdo_fini)(struct m0_stob_domain *dom)
Definition: domain.h:111
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
M0_INTERNAL int m0_fid_cmp(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:170
static struct m0_stob * stob_null_alloc(struct m0_stob_domain *dom, const struct m0_fid *stob_fid)
Definition: null.c:264
return M0_RC(rc)
uint64_t sn_magic
Definition: null.c:57
static void stob_null_domain_del(struct stob_null_domain *snd, struct stob_null_lists *snl)
Definition: null.c:145
static void stob_null_del(struct stob_null *sn, struct stob_null_domain *snd)
Definition: null.c:315
Definition: null.c:53
static int stob_null_destroy(struct m0_stob *stob, struct m0_dtx *dtx)
Definition: null.c:377
static int stob_null_domain_destroy(struct m0_stob_type *type, const char *location_data)
Definition: null.c:247
struct stob_null_lists * snd_lists
Definition: null.c:62
Definition: stob.h:163
static void stob_null_destroy_credit(struct m0_stob *stob, struct m0_be_tx_credit *accum)
Definition: null.c:372
static struct m0_stob * stob
Definition: storage.c:39
#define m0_free0(pptr)
Definition: memory.h:77
static int stob_null_domain_init(struct m0_stob_type *type, const char *location_data, void *cfg_init, struct m0_stob_domain **out)
Definition: null.c:179
struct m0_mutex snl_lock
Definition: null.c:73
static void stob_null_fini(struct m0_stob *stob)
Definition: null.c:338
Definition: tlist.h:251
static struct m0_stob_domain * dom
Definition: storage.c:38
static int stob_null_punch(struct m0_stob *stob, struct m0_indexvec *range, struct m0_dtx *dtx)
Definition: null.c:386
M0_TL_DEFINE(stob_null_stobs, static, struct stob_null)
const struct m0_stob_type m0_stob_null_type
Definition: null.c:87
void * m0_alloc(size_t size)
Definition: memory.c:126
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
static int stob_null_domain_cfg_create_parse(const char *str_cfg_create, void **cfg_create)
Definition: null.c:169
static struct m0_stob_domain_ops stob_null_domain_ops
Definition: null.c:90
static void stob_null_type_register(struct m0_stob_type *type)
Definition: null.c:93
static struct stob_null_domain * stob_null_domain_find(struct stob_null_lists *snl, const char *path, bool take_lock)
Definition: null.c:115
static struct stob_null * stob_null_find(struct stob_null_domain *snd, const struct m0_fid *stob_fid, bool take_lock)
Definition: null.c:286
struct m0_tlink sn_link
Definition: null.c:56
struct m0_stob_domain snd_dom
Definition: null.c:61
struct m0_fid sd_id
Definition: domain.h:96
void(* sop_fini)(struct m0_stob *stob)
Definition: stob.h:187
static void stob_null_cfg_free(void *cfg_create)
Definition: null.c:282
static int stob_null_add(struct stob_null *sn, struct stob_null_domain *snd)
Definition: null.c:301
Definition: fid.h:38
M0_INTERNAL void m0_stob_domain__id_set(struct m0_stob_domain *dom, struct m0_fid *dom_id)
Definition: domain.c:311
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
static void stob_null_domain_fini(struct m0_stob_domain *dom)
Definition: null.c:206
struct m0_fid sn_stob_fid
Definition: null.c:55
uint64_t snd_dom_key
Definition: null.c:64
static int stob_null_init(struct m0_stob *stob, struct m0_stob_domain *dom, const struct m0_fid *stob_fid)
Definition: null.c:322
M0_TL_DESCR_DEFINE(stob_null_stobs, "list of null stobs in the domain", static, struct stob_null, sn_link, sn_magic, M0_STOB_NULL_MAGIC, M0_STOB_NULL_HEAD_MAGIC)
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
struct stob_null_domain * sn_dom
Definition: null.c:54
const struct m0_stob_domain_ops * sd_ops
Definition: domain.h:94
static void stob_null_type_deregister(struct m0_stob_type *type)
Definition: null.c:105
static void stob_null_domain_cfg_init_free(void *cfg_init)
Definition: null.c:165
#define out(...)
Definition: gen.c:41
static int stob_null_domain_cfg_init_parse(const char *str_cfg_init, void **cfg_init)
Definition: null.c:159
int type
Definition: dir.c:1031
const struct m0_stob_type_ops * st_ops
Definition: type.h:60
static int stob_null_domain_create(struct m0_stob_type *type, const char *location_data, uint64_t dom_key, void *cfg_create)
Definition: null.c:210
uint64_t snd_magic
Definition: null.c:66
static struct stob_null_domain * stob_null_domain_container(struct m0_stob_domain *dom)
Definition: null.c:154
#define m0_tl_find(name, var, head,...)
Definition: tlist.h:757
static int stob_null_cfg_parse(const char *str_cfg_create, void **cfg_create)
Definition: null.c:276
void m0_free(void *data)
Definition: memory.c:146
Definition: mutex.h:47
static struct m0_stob_ops stob_null_ops
Definition: null.c:91
int32_t rc
Definition: trigger_fop.h:47
static void stob_null_domain_cfg_create_free(void *cfg_create)
Definition: null.c:175