Motr  M0
obj.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CONF
24 #include "lib/trace.h"
25 
26 #include "lib/misc.h" /* M0_IN */
27 #include "xcode/xcode.h" /* m0_xcode_union_add, M0_CONF_TYPE_MAX */
28 #include "conf/obj.h"
29 #include "conf/onwire.h" /* m0_confx_obj_xc */
30 #include "conf/onwire_xc.h" /* m0_confx_header_xc */
31 
301 
302 M0_INTERNAL bool m0_conf_obj_is_stub(const struct m0_conf_obj *obj)
303 {
304  M0_PRE(M0_IN(obj->co_status,
306  return obj->co_status != M0_CS_READY;
307 }
308 
309 static const struct m0_conf_obj_type *obj_types[256];
310 
312 
314 {
315  uint8_t id = otype->cot_ftype.ft_id;
316 
318  M0_PRE(obj_types[id] == NULL);
319 
321  obj_types[id] = otype;
322 
323  if (otype->cot_xt != NULL) {
324  otype->cot_xc_init();
327  M0_BITS(M0_XA_ATOM)));
328  /* Onwire representation must start with the header. */
329  M0_PRE((*otype->cot_xt)->xct_child[0].xf_type ==
330  m0_confx_header_xc);
331 
333  *otype->cot_xt, id);
334  }
336  ergo(obj_types[i] != NULL,
337  (obj_types[i]->cot_magic == otype->cot_magic) == (i == id))));
338 }
339 
341 {
342  uint8_t id = otype->cot_ftype.ft_id;
343 
345  M0_PRE(obj_types[id] == otype);
347  obj_types[id] = NULL;
348 }
349 
350 M0_INTERNAL const struct m0_conf_obj_type *
352 {
353  int idx;
354 
355  idx = otype == NULL ? 0 : otype->cot_ftype.ft_id + 1;
356  for (; idx < ARRAY_SIZE(obj_types); ++idx) {
357  if (obj_types[idx] != NULL)
358  return obj_types[idx];
359  }
360  return NULL;
361 }
362 
363 const struct m0_conf_obj_type *m0_conf_obj_type(const struct m0_conf_obj *obj)
364 {
365  return m0_conf_fid_type(&obj->co_id);
366 }
367 
368 const struct m0_conf_obj_type *m0_conf_fid_type(const struct m0_fid *fid)
369 {
370  uint8_t id = m0_fid_type_getfid(fid)->ft_id;
371 
373  M0_PRE(obj_types[id] != NULL);
374 
375  return obj_types[id];
376 }
377 
378 bool m0_conf_fid_is_valid(const struct m0_fid *fid)
379 {
380  return m0_fid_is_valid(fid) &&
382 }
383 
385 {
386  struct m0_conf_obj *result;
387 
389  M0_PRE(m0_conf_obj_type(obj->co_parent) == &M0_CONF_DIR_TYPE);
390 
391  result = obj->co_parent->co_parent;
392 
393  M0_POST(!m0_conf_obj_is_stub(result));
394  return result;
395 }
396 
397 M0_INTERNAL int m0_conf_obj_init(void)
398 {
399  m0_xcode_union_init(m0_confx_obj_xc, "m0_confx_obj",
400  "xo_type", M0_CONF_OBJ_TYPE_MAX);
403 #define X_CONF(_, NAME, ...) \
404  m0_conf_obj_type_register(&M0_CONF_ ## NAME ## _TYPE);
405 
407 #undef X_CONF
410  return 0;
411 }
412 
413 M0_INTERNAL void m0_conf_obj_fini(void)
414 {
415 #define X_CONF(_, NAME, ...) \
416  m0_conf_obj_type_unregister(&M0_CONF_ ## NAME ## _TYPE);
417 
419 #undef X_CONF
422 }
423 
424 M0_INTERNAL void
425 m0_conf_child_adopt(struct m0_conf_obj *parent, struct m0_conf_obj *child)
426 {
427  /* Root cannot be a child, because it is the topmost object. */
429  M0_PRE(child->co_cache == parent->co_cache);
430 
431  if (child->co_parent != child)
432  child->co_parent = parent;
433 }
434 
435 #undef M0_TRACE_SUBSYSTEM
const struct m0_conf_obj_type * m0_conf_obj_type(const struct m0_conf_obj *obj)
Definition: obj.c:363
void m0_conf_obj_type_unregister(const struct m0_conf_obj_type *otype)
Definition: obj.c:340
uint64_t id
Definition: cob.h:2380
void m0_conf_obj_type_register(const struct m0_conf_obj_type *otype)
Definition: obj.c:313
#define M0_PRE(cond)
void(* cot_xc_init)(void)
Definition: obj.h:327
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_xcode_union_close(struct m0_xcode_type *un)
Definition: xcode.c:805
M0_BASSERT(M0_CONF_OBJ_TYPE_MAX<=ARRAY_SIZE(obj_types))
#define ergo(a, b)
Definition: misc.h:293
M0_CONF_REL_FIDS const struct m0_fid_type M0_CONF_RELFID_TYPE
Definition: dir.c:215
const struct m0_conf_obj_type * m0_conf_fid_type(const struct m0_fid *fid)
Definition: obj.c:368
uint8_t ft_id
Definition: fid.h:101
const struct m0_conf_obj_type M0_CONF_ROOT_TYPE
Definition: root.c:214
#define M0_BITS(...)
Definition: misc.h:236
M0_INTERNAL struct m0_xcode_type * m0_confx_obj_xc
Definition: db.c:53
struct m0_conf_cache * co_cache
Definition: obj.h:251
struct m0_conf_obj * co_parent
Definition: obj.h:223
M0_INTERNAL bool m0_conf_obj_is_stub(const struct m0_conf_obj *obj)
Definition: obj.c:302
static struct foo * obj
Definition: tlist.c:302
struct m0_fid fid
Definition: di.c:46
M0_INTERNAL void m0_conf_obj_fini(void)
Definition: obj.c:413
const char * cot_branch
Definition: obj.h:326
M0_INTERNAL void m0_xcode_union_fini(struct m0_xcode_type *un)
Definition: xcode.c:768
int i
Definition: dir.c:1033
M0_INTERNAL void m0_conf_child_adopt(struct m0_conf_obj *parent, struct m0_conf_obj *child)
Definition: obj.c:425
M0_INTERNAL void m0_fid_type_register(const struct m0_fid_type *fidt)
Definition: fid.c:46
uint64_t cot_magic
Definition: obj.h:316
const struct m0_fid_type cot_ftype
Definition: obj.h:314
M0_INTERNAL void m0_xcode_union_add(struct m0_xcode_type *un, const char *name, const struct m0_xcode_type *xt, uint64_t tag)
Definition: xcode.c:780
struct m0_conf_obj * m0_conf_obj_grandparent(const struct m0_conf_obj *obj)
Definition: obj.c:384
static const struct m0_conf_obj_type * obj_types[256]
Definition: obj.c:309
M0_INTERNAL const struct m0_fid_type * m0_fid_type_getfid(const struct m0_fid *fid)
Definition: fid.c:76
struct m0_xcode_type ** cot_xt
Definition: obj.h:321
M0_INTERNAL void m0_xcode_union_init(struct m0_xcode_type *un, const char *name, const char *discriminator, size_t maxbranches)
Definition: xcode.c:751
#define M0_POST(cond)
m0_xcode_type_flags
Definition: xcode.h:300
M0_INTERNAL void m0_fid_type_unregister(const struct m0_fid_type *fidt)
Definition: fid.c:55
M0_INTERNAL const struct m0_conf_obj_type * m0_conf_obj_type_next(const struct m0_conf_obj_type *otype)
Definition: obj.c:351
bool m0_conf_fid_is_valid(const struct m0_fid *fid)
Definition: obj.c:378
#define m0_forall(var, nr,...)
Definition: misc.h:112
Definition: fid.h:38
#define IS_IN_ARRAY(idx, array)
Definition: misc.h:311
M0_INTERNAL int m0_conf_obj_init(void)
Definition: obj.c:397
M0_INTERNAL bool m0_fid_is_valid(const struct m0_fid *fid)
Definition: fid.c:96
#define M0_PRE_EX(cond)
const struct m0_conf_obj_type M0_CONF_DIR_TYPE
Definition: dir.c:206
#define ARRAY_SIZE(a)
Definition: misc.h:45
uint32_t xct_flags
Definition: xcode.h:331