Motr  M0
common.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CONF
23 #include "lib/trace.h"
24 
25 #include "conf/objs/common.h"
26 
27 #define X_CONF(name, key) \
28 const struct m0_fid M0_CONF_ ## name ## _FID = M0_FID_TINIT('/', 0, (key));
29 
31 #undef X_CONF
32 
33 M0_INTERNAL int dir_create_and_populate(struct m0_conf_dir **result,
34  const struct conf_dir_entries *de,
35  struct m0_conf_obj *dir_parent)
36 {
37  return M0_RC(m0_conf_dir_new(dir_parent, de->de_relfid,
38  de->de_entry_type, de->de_entries,
39  result));
40 }
41 
42 M0_INTERNAL int
43 conf_dirs_encode(const struct conf_dir_encoding_pair *how, size_t how_nr)
44 {
45  const struct conf_dir_encoding_pair *p;
46  size_t i;
47  int rc = 0;
48 
49  for (i = 0; i < how_nr; ++i) {
50  p = &how[i];
51  M0_ASSERT(_0C(p->dep_dest->af_count == 0) &&
52  _0C(p->dep_dest->af_elems == NULL));
53  rc = arrfid_from_dir(p->dep_dest, p->dep_src);
54  if (rc != 0)
55  break;
56  }
57  if (rc != 0) {
58  while (i > 0)
59  arrfid_free(how[--i].dep_dest);
60  }
61  return M0_RC(rc);
62 }
63 
64 M0_INTERNAL int conf_dirs_lookup(struct m0_conf_obj **out,
65  const struct m0_fid *name,
66  const struct conf_dir_relation *rels,
67  size_t nr_rels)
68 {
69  size_t i;
70 
71  for (i = 0; i < nr_rels; ++i) {
72  if (m0_fid_eq(rels[i].dr_relfid, name)) {
73  *out = &rels[i].dr_dir->cd_obj;
75  return 0;
76  }
77  }
78  return -ENOENT;
79 }
80 
81 M0_INTERNAL int
83 {
84  struct m0_conf_obj *obj;
85  struct m0_fid *fid;
86 
87  *dest = (struct m0_fid_arr){
88  .af_count = m0_conf_dir_len(dir)
89  };
90  if (dest->af_count == 0)
91  return 0;
92 
93  M0_ALLOC_ARR(dest->af_elems, dest->af_count);
94  if (dest->af_elems == NULL)
95  return M0_ERR(-ENOMEM);
96 
97  fid = dest->af_elems;
98  m0_tl_for (m0_conf_dir, &dir->cd_items, obj) {
99  *fid++ = obj->co_id;
100  } m0_tl_endfor;
101  return 0;
102 }
103 
104 M0_INTERNAL void arrfid_free(struct m0_fid_arr *arr)
105 {
106  m0_free0(&arr->af_elems);
107 }
108 
109 M0_INTERNAL void
111 {
112  dest->xo_u.u_header.ch_id = src->co_id;
113  dest->xo_type = m0_conf_obj_type(src)->cot_ftype.ft_id;
114 }
115 
116 M0_INTERNAL int u32arr_decode(const struct arr_u32 *src, uint32_t **dest)
117 {
118  M0_PRE(src->au_count != 0 && src->au_elems != NULL);
119 
120  M0_ALLOC_ARR(*dest, src->au_count);
121  if (*dest == NULL)
122  return M0_ERR(-ENOMEM);
123 
124  memcpy(*dest, src->au_elems, src->au_count * sizeof *dest[0]);
125  return 0;
126 }
127 
128 M0_INTERNAL int
129 u32arr_encode(struct arr_u32 *dest, const uint32_t *src, uint32_t src_nr)
130 {
131  M0_PRE((src == NULL) == (src_nr == 0));
132 
133  if (src != NULL) {
134  M0_ALLOC_ARR(dest->au_elems, src_nr);
135  if (dest->au_elems == NULL)
136  return M0_ERR(-ENOMEM);
137  dest->au_count = src_nr;
138  memcpy(dest->au_elems, src,
139  dest->au_count * sizeof dest->au_elems[0]);
140  }
141  return 0;
142 }
143 
144 M0_INTERNAL bool
145 u32arr_cmp(const struct arr_u32 *a1, const uint32_t *a2, uint32_t a2_nr)
146 {
147  return a1->au_count == a2_nr &&
148  m0_forall(i, a2_nr, a1->au_elems[i] == a2[i]);
149 }
150 
151 M0_INTERNAL void u32arr_free(struct arr_u32 *arr)
152 {
153  m0_free0(&arr->au_elems);
154  arr->au_count = 0;
155 }
156 
157 /*
158  * XXX REFACTORME
159  * This code resembles that of profile_decode() and m0_bufs_to_strings().
160  */
161 M0_INTERNAL int conf_pvers_decode(struct m0_conf_pver ***dest,
162  const struct m0_fid_arr *src,
163  struct m0_conf_cache *cache)
164 {
165  uint32_t i;
166  struct m0_conf_obj *obj;
167 
168  M0_PRE(*dest == NULL);
169  M0_PRE((src->af_count == 0) == (src->af_elems == NULL));
170 
171  if (src->af_count == 0)
172  return M0_RC(0);
173 
174  M0_ALLOC_ARR(*dest, src->af_count + 1);
175  if (*dest == NULL)
176  return M0_ERR(-ENOMEM);
177 
178  for (i = 0; i < src->af_count; ++i) {
179  int rc = m0_conf_obj_find(cache, &src->af_elems[i], &obj);
180  if (rc != 0) {
181  m0_free0(dest);
182  return M0_ERR(rc);
183  }
184  (*dest)[i] = M0_CONF_CAST(obj, m0_conf_pver);
185  }
186  return M0_RC(0);
187 }
188 
189 /*
190  * XXX REFACTORME
191  * This code resembles that of profile_encode() and m0_bufs_from_strings().
192  */
193 M0_INTERNAL int
195 {
196  uint32_t i;
197 
198  M0_SET0(dest);
199 
200  if (src == NULL)
201  return M0_RC(0);
202 
203  while (src[dest->af_count] != NULL)
204  ++dest->af_count;
205  if (dest->af_count == 0)
206  return M0_RC(0);
207 
208  M0_ALLOC_ARR(dest->af_elems, dest->af_count);
209  if (dest->af_elems == NULL)
210  return M0_ERR(-ENOMEM);
211 
212  for (i = 0; i < dest->af_count; ++i)
213  dest->af_elems[i] = src[i]->pv_obj.co_id;
214  return M0_RC(0);
215 }
216 
217 M0_INTERNAL int conf_obj_lookup_denied(const struct m0_conf_obj *parent,
218  const struct m0_fid *name,
219  struct m0_conf_obj **out)
220 {
221  M0_IMPOSSIBLE("Leaf object");
222  return M0_ERR(-EPERM);
223 }
224 
225 M0_INTERNAL const struct m0_fid **
227 {
228  static const struct m0_fid *rels[] = { NULL };
229  return rels;
230 }
231 
232 #undef M0_TRACE_SUBSYSTEM
233 
234 /*
235  * Local variables:
236  * c-indentation-style: "K&R"
237  * c-basic-offset: 8
238  * tab-width: 8
239  * fill-column: 80
240  * scroll-step: 1
241  * End:
242  */
243 /*
244  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
245  */
const struct m0_conf_obj_type * m0_conf_obj_type(const struct m0_conf_obj *obj)
Definition: obj.c:363
M0_INTERNAL int conf_dirs_encode(const struct conf_dir_encoding_pair *how, size_t how_nr)
Definition: common.c:43
Definition: beck.c:235
M0_INTERNAL const struct m0_fid ** conf_obj_downlinks_none(const struct m0_conf_obj *obj)
Definition: common.c:226
static struct m0_addb2_philter p
Definition: consumer.c:40
M0_INTERNAL void arrfid_free(struct m0_fid_arr *arr)
Definition: common.c:104
M0_INTERNAL int conf_dirs_lookup(struct m0_conf_obj **out, const struct m0_fid *name, const struct conf_dir_relation *rels, size_t nr_rels)
Definition: common.c:64
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define NULL
Definition: misc.h:38
struct m0_conf_obj cd_obj
Definition: obj.h:358
const struct m0_fid_arr * de_entries
Definition: common.h:86
uint8_t ft_id
Definition: fid.h:101
struct m0_fid * af_elems
Definition: fid.h:45
const struct m0_conf_obj_type * de_entry_type
Definition: common.h:85
Definition: onwire.h:44
M0_INTERNAL int conf_obj_lookup_denied(const struct m0_conf_obj *parent, const struct m0_fid *name, struct m0_conf_obj **out)
Definition: common.c:217
struct m0_fid_arr * dep_dest
Definition: common.h:97
uint32_t * au_elems
Definition: onwire.h:46
#define M0_SET0(obj)
Definition: misc.h:64
static struct foo * obj
Definition: tlist.c:302
#define m0_tl_endfor
Definition: tlist.h:700
struct m0_fid fid
Definition: di.c:46
return M0_RC(rc)
M0_INTERNAL int conf_pvers_decode(struct m0_conf_pver ***dest, const struct m0_fid_arr *src, struct m0_conf_cache *cache)
Definition: common.c:161
M0_INTERNAL int m0_conf_dir_new(struct m0_conf_obj *parent, const struct m0_fid *relfid, const struct m0_conf_obj_type *children_type, const struct m0_fid_arr *children_ids, struct m0_conf_dir **out)
Definition: dir.c:159
M0_INTERNAL int u32arr_decode(const struct arr_u32 *src, uint32_t **dest)
Definition: common.c:116
int i
Definition: dir.c:1033
return M0_ERR(-EOPNOTSUPP)
const char * name
Definition: trace.c:110
const struct m0_fid_type cot_ftype
Definition: obj.h:314
#define m0_free0(pptr)
Definition: memory.h:77
const struct m0_fid * de_relfid
Definition: common.h:84
#define M0_ASSERT(cond)
M0_INTERNAL int conf_pvers_encode(struct m0_fid_arr *dest, const struct m0_conf_pver **src)
Definition: common.c:194
static uint32_t m0_conf_dir_len(const struct m0_conf_dir *dir)
Definition: dir.h:59
M0_INTERNAL void confx_encode(struct m0_confx_obj *dest, const struct m0_conf_obj *src)
Definition: common.c:110
Definition: fid.h:43
#define M0_POST(cond)
M0_INTERNAL int m0_conf_obj_find(struct m0_conf_cache *cache, const struct m0_fid *id, struct m0_conf_obj **out)
Definition: obj_ops.c:136
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
M0_INTERNAL int arrfid_from_dir(struct m0_fid_arr *dest, const struct m0_conf_dir *dir)
Definition: common.c:82
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
#define m0_forall(var, nr,...)
Definition: misc.h:112
M0_INTERNAL void u32arr_free(struct arr_u32 *arr)
Definition: common.c:151
struct m0_conf_dir * dr_dir
Definition: common.h:104
Definition: fid.h:38
M0_INTERNAL int u32arr_encode(struct arr_u32 *dest, const uint32_t *src, uint32_t src_nr)
Definition: common.c:129
#define _0C(exp)
Definition: assert.h:311
M0_CONF_REL_FIDS M0_INTERNAL int dir_create_and_populate(struct m0_conf_dir **result, const struct conf_dir_entries *de, struct m0_conf_obj *dir_parent)
Definition: common.c:33
struct inode * dir
Definition: dir.c:1028
#define out(...)
Definition: gen.c:41
#define M0_CONF_REL_FIDS
Definition: obj.h:831
M0_INTERNAL bool u32arr_cmp(const struct arr_u32 *a1, const uint32_t *a2, uint32_t a2_nr)
Definition: common.c:145
#define m0_tl_for(name, head, obj)
Definition: tlist.h:695
struct m0_pdclust_src_addr src
Definition: fd.c:108
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL bool m0_conf_obj_invariant(const struct m0_conf_obj *obj)
Definition: obj_ops.c:52
uint32_t au_count
Definition: onwire.h:45
#define M0_IMPOSSIBLE(fmt,...)