Motr  M0
fd_tree.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 #include "fd/fd_internal.h"
24 #include "lib/assert.h" /* _0C */
25 #include "lib/memory.h" /* m0_alloc m0_free */
26 #include "lib/errno.h" /* EINVAL ENOMEM */
27 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_FD
28 #include "lib/trace.h" /* M0_ERR */
29 
30 
31 M0_INTERNAL int m0_fd__tree_node_init(struct m0_fd_tree *tree,
32  struct m0_fd_tree_node *node,
33  uint16_t child_nr,
34  const struct m0_fd__tree_cursor *cursor)
35 {
36  M0_PRE(tree != NULL && node != NULL && cursor != NULL);
37  M0_PRE(ergo(cursor->ftc_depth != 0, cursor->ftc_node != NULL));
38  M0_PRE(ergo(cursor->ftc_depth != tree->ft_depth, child_nr > 0));
39 
40  node->ftn_parent = cursor->ftc_node;
41  node->ftn_depth = cursor->ftc_depth;
42  node->ftn_rel_idx = cursor->ftc_child_idx;
43  node->ftn_abs_idx = cursor->ftc_child_abs_idx;
44  node->ftn_ha_state = M0_NC_ONLINE;
45  ++tree->ft_cnt;
46  node->ftn_child_nr = child_nr;
47  node->ftn_children = m0_alloc(child_nr * sizeof node->ftn_children[0]);
48  if (node->ftn_children == NULL)
49  return M0_ERR(-ENOMEM);
50  return M0_RC(0);
51 }
52 
53 M0_INTERNAL void m0_fd__tree_node_fini(struct m0_fd_tree *tree,
54  struct m0_fd_tree_node *node)
55 {
56  M0_PRE(tree != NULL && node != NULL);
57  if (node->ftn_children != NULL)
58  m0_free0(&node->ftn_children);
59  --tree->ft_cnt;
60 }
61 
62 M0_INTERNAL int m0_fd__tree_cursor_init(struct m0_fd__tree_cursor *cursor,
63  const struct m0_fd_tree *tree,
64  uint16_t depth)
65 {
66  struct m0_fd__tree_cursor coarse_cursor;
67 
68  M0_PRE(cursor != NULL);
69  M0_PRE(tree != NULL);
70  M0_PRE(depth <= tree->ft_depth);
71 
72  M0_SET0(&coarse_cursor);
73  coarse_cursor.ftc_tree = (struct m0_fd_tree *)tree;
74  /* Specially treat the root node. */
75  if (depth == 0) {
76  coarse_cursor.ftc_node = NULL;
77  coarse_cursor.ftc_depth = 0;
78  } else {
79  coarse_cursor.ftc_node = tree->ft_root;
80  coarse_cursor.ftc_depth = 1;
81  coarse_cursor.ftc_path[0] = 0;
82  }
83  while (coarse_cursor.ftc_depth < depth) {
85  coarse_cursor.ftc_node));
86  coarse_cursor.ftc_node =
87  coarse_cursor.ftc_node->ftn_children[0];
88  ++coarse_cursor.ftc_depth;
89  }
90  coarse_cursor.ftc_child_idx = 0;
91  coarse_cursor.ftc_child_abs_idx = 0;
92  coarse_cursor.ftc_cnt = 0;
93  *cursor = coarse_cursor;
94  return M0_RC(0);
95 }
96 
97 M0_INTERNAL int m0_fd__tree_cursor_init_at(struct m0_fd__tree_cursor *cursor,
98  const struct m0_fd_tree *tree,
99  const struct m0_fd_tree_node *node,
100  uint32_t child_idx)
101 {
102  struct m0_fd__tree_cursor fine_cursor;
103  struct m0_fd_tree_node *parent;
104  uint32_t depth;
105 
106  M0_PRE(cursor != NULL);
109  M0_PRE(child_idx < node->ftn_child_nr);
110 
111  M0_SET0(&fine_cursor);
112  fine_cursor.ftc_tree = (struct m0_fd_tree *)tree;
113  fine_cursor.ftc_node = (struct m0_fd_tree_node *)node;
114  fine_cursor.ftc_depth = node->ftn_depth + 1;
115  fine_cursor.ftc_child_idx = child_idx;
116  fine_cursor.ftc_cnt = 0;
117  if (!m0_fd__tree_node_invariant(tree, node->ftn_children[child_idx]))
118  return M0_ERR(-EINVAL);
119  fine_cursor.ftc_child_abs_idx =
120  node->ftn_children[child_idx]->ftn_abs_idx;
121  fine_cursor.ftc_path[node->ftn_depth + 1] =
122  fine_cursor.ftc_child_abs_idx;
123  parent = (struct m0_fd_tree_node *)node;
124  depth = parent->ftn_depth;
125  while (parent != NULL) {
126  fine_cursor.ftc_path[depth] = parent->ftn_abs_idx;
127  parent = parent->ftn_parent;
128  --depth;
129  }
130  *cursor = fine_cursor;
131  memcpy(cursor, &fine_cursor, sizeof fine_cursor);
132  return M0_RC(0);
133 }
134 
135 M0_INTERNAL struct m0_fd_tree_node **
137 {
138  return cursor->ftc_depth == 0 ? &cursor->ftc_tree->ft_root :
139  &(cursor->ftc_node->ftn_children[cursor->ftc_child_idx]);
140 }
141 
142 M0_INTERNAL int m0_fd__tree_cursor_next(struct m0_fd__tree_cursor *cursor)
143 {
144  struct m0_fd_tree_node *parent;
145  struct m0_fd_tree_node *child;
146  uint16_t depth;
147  uint16_t child_idx;
148 
149  if (cursor->ftc_depth == 0)
150  goto end;
151  parent = cursor->ftc_node;
152  child_idx = cursor->ftc_child_idx;
153  if (parent->ftn_child_nr > child_idx + 1) {
154  ++cursor->ftc_child_idx;
155  } else {
156  depth = cursor->ftc_depth;
157  while (parent != NULL &&
158  parent->ftn_child_nr <= child_idx + 1) {
159  child_idx = parent->ftn_rel_idx;
160  parent = parent->ftn_parent;
161  --depth;
162  }
163  if (parent == NULL)
164  goto end;
165  child = parent->ftn_children[child_idx + 1];
166  ++depth;
167  while (depth < cursor->ftc_depth) {
168  child = child->ftn_children[0];
169  ++depth;
170  }
171  cursor->ftc_node = child;
172  cursor->ftc_child_idx = 0;
173  }
174  ++cursor->ftc_child_abs_idx;
175  cursor->ftc_path[cursor->ftc_depth] = cursor->ftc_child_abs_idx;
176  return 1;
177 end:
178  return 0;
179 }
180 
181 M0_INTERNAL int m0_fd__tree_root_create(struct m0_fd_tree *tree,
182  uint64_t root_children)
183 {
184  struct m0_fd__tree_cursor cursor;
185  int rc;
186 
187  M0_PRE(tree != NULL && tree->ft_root != NULL);
188  /* Initialize the root node. */
189  rc = m0_fd__tree_cursor_init(&cursor, tree, 0);
190  if (rc != 0)
191  return M0_RC(rc);
192  rc = m0_fd__tree_node_init(tree, tree->ft_root, root_children,
193  &cursor);
194  if (rc != 0) {
195  tree->ft_depth = 0;
196  m0_fd__tree_node_fini(tree, tree->ft_root);
197  m0_free0(&tree->ft_root);
198  }
199  return M0_RC(rc);
200 }
201 
202 M0_INTERNAL bool m0_fd__tree_invariant(const struct m0_fd_tree *tree)
203 {
204  return _0C(tree != NULL) && _0C(tree->ft_depth > 0) &&
205  _0C(tree->ft_root != NULL);
206 }
207 
208 M0_INTERNAL bool m0_fd__tree_node_invariant(const struct m0_fd_tree *tree,
209  const struct m0_fd_tree_node *node)
210 {
211  return _0C(node != NULL) && _0C(ergo(node->ftn_depth < tree->ft_depth,
212  node->ftn_child_nr > 0 &&
213  node->ftn_children != NULL));
214 }
215 
216 #undef M0_TRACE_SUBSYSTEM
M0_INTERNAL struct m0_fd_tree_node ** m0_fd__tree_cursor_get(struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:136
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
uint32_t ftn_child_nr
Definition: fd.h:218
M0_INTERNAL int m0_fd__tree_cursor_init(struct m0_fd__tree_cursor *cursor, const struct m0_fd_tree *tree, uint16_t depth)
Definition: fd_tree.c:62
#define ergo(a, b)
Definition: misc.h:293
M0_INTERNAL void m0_fd__tree_node_fini(struct m0_fd_tree *tree, struct m0_fd_tree_node *node)
Definition: fd_tree.c:53
static struct net_test_cmd_node * node
Definition: commands.c:72
uint16_t ft_depth
Definition: fd.h:201
int32_t ftc_child_abs_idx
Definition: fd_internal.h:41
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL int m0_fd__tree_cursor_next(struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:142
return M0_RC(rc)
struct m0_fd_tree_node * ftn_parent
Definition: fd.h:214
static unsigned depth
Definition: base.c:377
return M0_ERR(-EOPNOTSUPP)
uint32_t ftn_abs_idx
Definition: fd.h:222
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
uint16_t ftc_depth
Definition: fd_internal.h:35
struct m0_fd_tree_node * ftc_node
Definition: fd_internal.h:37
M0_INTERNAL bool m0_fd__tree_invariant(const struct m0_fd_tree *tree)
Definition: fd_tree.c:202
void * m0_alloc(size_t size)
Definition: memory.c:126
struct m0_fd_tree * ftc_tree
Definition: fd_internal.h:33
int32_t ftc_child_idx
Definition: fd_internal.h:39
uint64_t ft_cnt
Definition: fd.h:207
struct m0_fd_tree_node * ft_root
Definition: fd.h:203
struct m0_fd_tree_node ** ftn_children
Definition: fd.h:224
uint32_t ftn_rel_idx
Definition: fd.h:220
#define _0C(exp)
Definition: assert.h:311
uint64_t ftc_path[M0_CONF_PVER_HEIGHT+1]
Definition: fd_internal.h:46
uint16_t ftn_depth
Definition: fd.h:216
M0_INTERNAL int m0_fd__tree_node_init(struct m0_fd_tree *tree, struct m0_fd_tree_node *node, uint16_t child_nr, const struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:31
Definition: fd.h:199
M0_INTERNAL bool m0_fd__tree_node_invariant(const struct m0_fd_tree *tree, const struct m0_fd_tree_node *node)
Definition: fd_tree.c:208
M0_INTERNAL int m0_fd__tree_root_create(struct m0_fd_tree *tree, uint64_t root_children)
Definition: fd_tree.c:181
M0_INTERNAL int m0_fd__tree_cursor_init_at(struct m0_fd__tree_cursor *cursor, const struct m0_fd_tree *tree, const struct m0_fd_tree_node *node, uint32_t child_idx)
Definition: fd_tree.c:97
int32_t rc
Definition: trigger_fop.h:47