Motr  M0
linear_enum.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 
29 #include "lib/errno.h"
30 #include "lib/tlist.h" /* struct m0_tl */
31 #include "lib/vec.h" /* m0_bufvec_cursor_step(), m0_bufvec_cursor_addr() */
32 #include "lib/memory.h" /* M0_ALLOC_PTR() */
33 #include "lib/misc.h" /* M0_IN() */
34 #include "lib/bob.h"
35 #include "lib/finject.h"
36 
37 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LAYOUT
38 #include "lib/trace.h"
39 
40 #include "motr/magic.h"
41 #include "fid/fid.h" /* m0_fid_set(), m0_fid_is_valid() */
42 #include "layout/layout_internal.h"
43 #include "layout/linear_enum.h"
44 #include "ioservice/fid_convert.h" /* m0_fid_convert_gob2cob */
45 
46 static const struct m0_bob_type linear_bob = {
47  .bt_name = "linear_enum",
48  .bt_magix_offset = offsetof(struct m0_layout_linear_enum, lla_magic),
49  .bt_magix = M0_LAYOUT_LINEAR_ENUM_MAGIC,
50  .bt_check = NULL
51 };
52 
54 
56 {
57  return
58  m0_layout_linear_enum_bob_check(le) &&
59  le->lle_attr.lla_nr == 0 &&
60  le->lle_attr.lla_B == 0;
61 }
62 
67 static bool linear_invariant(const struct m0_layout_linear_enum *le)
68 {
69  return
70  m0_layout_linear_enum_bob_check(le) &&
71  le->lle_attr.lla_nr != 0 &&
72  le->lle_attr.lla_B != 0 &&
74 }
75 
76 static const struct m0_layout_enum_ops linear_enum_ops;
77 
80  struct m0_layout_enum **out)
81 {
82  struct m0_layout_linear_enum *lin_enum;
83 
85  M0_PRE(out != NULL);
86 
87  M0_ENTRY();
88 
89  if (M0_FI_ENABLED("mem_err")) { lin_enum = NULL; goto err1_injected; }
90  M0_ALLOC_PTR(lin_enum);
91 err1_injected:
92  if (lin_enum == NULL) {
93  m0_layout__log("linear_allocate", "M0_ALLOC_PTR() failed",
94  LID_NONE, -ENOMEM);
95  return M0_ERR(-ENOMEM);
96  }
97  m0_layout__enum_init(dom, &lin_enum->lle_base,
99  m0_layout_linear_enum_bob_init(lin_enum);
101  *out = &lin_enum->lle_base;
102  M0_LEAVE("linear enum pointer %p", lin_enum);
103  return 0;
104 }
105 
106 static void linear_delete(struct m0_layout_enum *e)
107 {
108  struct m0_layout_linear_enum *lin_enum;
109 
110  lin_enum = bob_of(e, struct m0_layout_linear_enum,
111  lle_base, &linear_bob);
113 
114  M0_ENTRY("enum_pointer %p", e);
115  m0_layout_linear_enum_bob_fini(lin_enum);
117  m0_free(lin_enum);
118  M0_LEAVE();
119 }
120 
121 static int linear_populate(struct m0_layout_linear_enum *lin_enum,
122  const struct m0_layout_linear_attr *attr)
123 {
125  M0_PRE(attr != NULL);
126 
127  if (attr->lla_nr == 0 || attr->lla_B == 0) {
129  "lin_enum %p, attr %p, Invalid attributes, rc %d",
130  lin_enum, attr, -EPROTO);
131  return M0_ERR(-EPROTO);
132  }
133  lin_enum->lle_attr = *attr;
134  M0_POST(linear_invariant(lin_enum));
135  return 0;
136 }
137 
138 M0_INTERNAL int m0_linear_enum_build(struct m0_layout_domain *dom,
139  const struct m0_layout_linear_attr *attr,
140  struct m0_layout_linear_enum **out)
141 {
142  struct m0_layout_enum *e;
143  struct m0_layout_linear_enum *lin_enum;
144  int rc;
145 
146  M0_PRE(out != NULL);
147  M0_ENTRY("domain %p", dom);
148 
149  rc = linear_allocate(dom, &e);
150  if (rc == 0) {
151  lin_enum = bob_of(e, struct m0_layout_linear_enum,
152  lle_base, &linear_bob);
153  rc = linear_populate(lin_enum, attr);
154  if (rc == 0) {
155  M0_POST(linear_invariant(lin_enum));
156  *out = lin_enum;
157  } else {
158  linear_delete(e);
159  }
160  }
161 
162  M0_LEAVE("domain %p, rc %d", dom, rc);
163  return M0_RC(rc);
164 }
165 
166 static struct m0_layout_linear_enum *
168 {
169  struct m0_layout_linear_enum *lin_enum;
170 
171  lin_enum = bob_of(e, struct m0_layout_linear_enum,
172  lle_base, &linear_bob);
173  M0_ASSERT(linear_invariant(lin_enum));
174  return lin_enum;
175 }
176 
178 static void linear_fini(struct m0_layout_enum *e)
179 {
180  struct m0_layout_linear_enum *lin_enum;
181  uint64_t lid;
182 
184 
185  lid = e->le_sl_is_set ? e->le_sl->sl_base.l_id : 0;
186  M0_ENTRY("lid %llu, enum_pointer %p", (unsigned long long)lid, e);
187  lin_enum = enum_to_linear_enum(e);
188  m0_layout_linear_enum_bob_fini(lin_enum);
190  m0_free(lin_enum);
191  M0_LEAVE();
192 }
193 
199  const struct m0_layout_enum_type *et)
200 {
201  return 0;
202 }
203 
206  const struct m0_layout_enum_type *et)
207 {
208 }
209 
212 {
213  return sizeof(struct m0_layout_linear_attr);
214 }
215 
217 static int linear_decode(struct m0_layout_enum *e,
218  struct m0_bufvec_cursor *cur,
219  enum m0_layout_xcode_op op,
220  struct m0_be_tx *tx,
221  struct m0_striped_layout *stl)
222 {
223  uint64_t lid;
224  struct m0_layout_linear_enum *lin_enum;
225  struct m0_layout_linear_attr *lin_attr;
226  int rc;
227 
228  M0_PRE(e != NULL);
229  M0_PRE(cur != NULL);
230  M0_PRE(m0_bufvec_cursor_step(cur) >= sizeof *lin_attr);
232  M0_PRE(ergo(op == M0_LXO_DB_LOOKUP, tx != NULL));
234 
235  lid = stl->sl_base.l_id;
236  M0_ENTRY("lid %llu", (unsigned long long)lid);
237  lin_enum = bob_of(e, struct m0_layout_linear_enum,
238  lle_base, &linear_bob);
240 
241  lin_attr = m0_bufvec_cursor_addr(cur);
242  m0_bufvec_cursor_move(cur, sizeof *lin_attr);
243 
244  if (M0_FI_ENABLED("attr_err")) { lin_attr->lla_nr = 0; }
245  rc = linear_populate(lin_enum, lin_attr);
246  if (rc != 0)
247  M0_LOG(M0_ERROR, "linear_populate() failed");
248  M0_POST(ergo(rc == 0, linear_invariant(lin_enum)));
249  M0_POST(ergo(rc != 0, linear_allocated_invariant(lin_enum)));
250  M0_LEAVE("lid %llu, rc %d", (unsigned long long)lid, rc);
251  return M0_RC(rc);
252 }
253 
255 static int linear_encode(const struct m0_layout_enum *e,
256  enum m0_layout_xcode_op op,
257  struct m0_be_tx *tx,
258  struct m0_bufvec_cursor *out)
259 {
260  struct m0_layout_linear_enum *lin_enum;
261  m0_bcount_t nbytes;
262  uint64_t lid;
263 
264  M0_PRE(e != NULL);
267  M0_PRE(ergo(op != M0_LXO_BUFFER_OP, tx != NULL));
268  M0_PRE(out != NULL);
269  M0_PRE(m0_bufvec_cursor_step(out) >= sizeof lin_enum->lle_attr);
270 
271  lin_enum = enum_to_linear_enum(e);
272  lid = lin_enum->lle_base.le_sl->sl_base.l_id;
273  M0_ENTRY("lid %llu", (unsigned long long)lid);
274  nbytes = m0_bufvec_cursor_copyto(out, &lin_enum->lle_attr,
275  sizeof lin_enum->lle_attr);
276  M0_ASSERT(nbytes == sizeof lin_enum->lle_attr);
277  M0_LEAVE("lid %llu", (unsigned long long)lid);
278  return 0;
279 }
280 
282 static uint32_t linear_nr(const struct m0_layout_enum *e)
283 {
284  struct m0_layout_linear_enum *lin_enum;
285 
286  M0_PRE(e != NULL);
287 
288  M0_ENTRY("lid %llu, enum_pointer %p",
289  (unsigned long long)e->le_sl->sl_base.l_id, e);
290  lin_enum = enum_to_linear_enum(e);
291  M0_LEAVE("lid %llu, enum_pointer %p, nr %lu",
292  (unsigned long long)e->le_sl->sl_base.l_id, e,
293  (unsigned long)lin_enum->lle_attr.lla_nr);
294  return lin_enum->lle_attr.lla_nr;
295 }
296 
298 static void linear_get(const struct m0_layout_enum *e, uint32_t idx,
299  const struct m0_fid *gfid, struct m0_fid *out)
300 {
301  struct m0_layout_linear_enum *lin_enum;
302 
303  M0_PRE(e != NULL);
304  M0_PRE(gfid != NULL);
305  M0_PRE(out != NULL);
306 
307  M0_ENTRY("lid %llu, enum_pointer %p",
308  (unsigned long long)e->le_sl->sl_base.l_id, e);
309  lin_enum = enum_to_linear_enum(e);
310  M0_ASSERT(idx < lin_enum->lle_attr.lla_nr);
312  idx * lin_enum->lle_attr.lla_B);
313 
314  M0_LEAVE("lid %llu, enum_pointer %p, fid_pointer %p",
315  (unsigned long long)e->le_sl->sl_base.l_id, e, out);
317 }
318 
321 {
322  return sizeof(struct m0_layout_linear_attr);
323 }
324 
325 static const struct m0_layout_enum_ops linear_enum_ops = {
326  .leo_nr = linear_nr,
327  .leo_get = linear_get,
328  .leo_recsize = linear_recsize,
329  .leo_fini = linear_fini,
330  .leo_delete = linear_delete,
331  .leo_decode = linear_decode,
332  .leo_encode = linear_encode
333 };
334 
337  .leto_unregister = linear_unregister,
338  .leto_max_recsize = linear_max_recsize,
339  .leto_allocate = linear_allocate
340 };
341 
343  .let_name = "linear",
344  .let_id = 1,
345  .let_ref_count = 0,
346  .let_ops = &linear_type_ops
347 };
348 
349 #undef M0_TRACE_SUBSYSTEM
350 
353 /*
354  * Local variables:
355  * c-indentation-style: "K&R"
356  * c-basic-offset: 8
357  * tab-width: 8
358  * fill-column: 80
359  * scroll-step: 1
360  * End:
361  */
static int linear_populate(struct m0_layout_linear_enum *lin_enum, const struct m0_layout_linear_attr *attr)
Definition: linear_enum.c:121
static bool linear_invariant(const struct m0_layout_linear_enum *le)
Definition: linear_enum.c:67
int(* leto_register)(struct m0_layout_domain *dom, const struct m0_layout_enum_type *et)
Definition: layout.h:548
static struct m0_layout_linear_enum * enum_to_linear_enum(const struct m0_layout_enum *e)
Definition: linear_enum.c:167
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
static struct buffer * cur(struct m0_addb2_mach *mach, m0_bcount_t space)
Definition: addb2.c:791
#define ergo(a, b)
Definition: misc.h:293
static m0_bcount_t linear_recsize(struct m0_layout_enum *e)
Definition: linear_enum.c:320
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
M0_INTERNAL int m0_linear_enum_build(struct m0_layout_domain *dom, const struct m0_layout_linear_attr *attr, struct m0_layout_linear_enum **out)
Definition: linear_enum.c:138
static int linear_register(struct m0_layout_domain *dom, const struct m0_layout_enum_type *et)
Definition: linear_enum.c:198
M0_INTERNAL void * m0_bufvec_cursor_addr(struct m0_bufvec_cursor *cur)
Definition: vec.c:597
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL bool m0_layout__striped_allocated_invariant(const struct m0_striped_layout *stl)
Definition: layout.c:181
const char * bt_name
Definition: bob.h:73
static const struct m0_bob_type linear_bob
Definition: linear_enum.c:46
static bool linear_allocated_invariant(const struct m0_layout_linear_enum *le)
Definition: linear_enum.c:55
return M0_RC(rc)
op
Definition: libdemo.c:64
static int linear_allocate(struct m0_layout_domain *dom, struct m0_layout_enum **out)
Definition: linear_enum.c:79
static void linear_get(const struct m0_layout_enum *e, uint32_t idx, const struct m0_fid *gfid, struct m0_fid *out)
Definition: linear_enum.c:298
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL bool m0_layout__enum_invariant(const struct m0_layout_enum *e)
Definition: layout.c:170
M0_INTERNAL bool m0_bufvec_cursor_move(struct m0_bufvec_cursor *cur, m0_bcount_t count)
Definition: vec.c:574
struct m0_layout_linear_attr lle_attr
Definition: linear_enum.h:71
M0_INTERNAL void m0_layout__enum_fini(struct m0_layout_enum *le)
Definition: layout.c:507
return M0_ERR(-EOPNOTSUPP)
struct m0_layout_enum lle_base
Definition: linear_enum.h:69
static const struct m0_layout_enum_type_ops linear_type_ops
Definition: linear_enum.c:335
static void attr(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:949
M0_INTERNAL m0_bcount_t m0_bufvec_cursor_step(const struct m0_bufvec_cursor *cur)
Definition: vec.c:581
static void linear_fini(struct m0_layout_enum *e)
Definition: linear_enum.c:178
static int linear_encode(const struct m0_layout_enum *e, enum m0_layout_xcode_op op, struct m0_be_tx *tx, struct m0_bufvec_cursor *out)
Definition: linear_enum.c:255
#define M0_ASSERT(cond)
M0_INTERNAL void m0_layout__enum_init(struct m0_layout_domain *dom, struct m0_layout_enum *le, struct m0_layout_enum_type *let, const struct m0_layout_enum_ops *ops)
Definition: layout.c:480
#define bob_of(ptr, type, field, bt)
Definition: bob.h:140
static struct m0_stob_domain * dom
Definition: storage.c:38
#define M0_POST(cond)
static int linear_decode(struct m0_layout_enum *e, struct m0_bufvec_cursor *cur, enum m0_layout_xcode_op op, struct m0_be_tx *tx, struct m0_striped_layout *stl)
Definition: linear_enum.c:217
M0_INTERNAL bool m0_layout__domain_invariant(const struct m0_layout_domain *dom)
Definition: layout.c:131
M0_INTERNAL void m0_layout__log(const char *fn_name, const char *err_msg, uint64_t lid, int rc)
Definition: layout.c:586
const char * let_name
Definition: layout.h:527
static uint32_t linear_nr(const struct m0_layout_enum *e)
Definition: linear_enum.c:282
static void linear_delete(struct m0_layout_enum *e)
Definition: linear_enum.c:106
bool le_sl_is_set
Definition: layout.h:417
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
Definition: fid.h:38
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
M0_INTERNAL m0_bcount_t m0_bufvec_cursor_copyto(struct m0_bufvec_cursor *dcur, void *sdata, m0_bcount_t num_bytes)
Definition: vec.c:677
struct m0_layout sl_base
Definition: layout.h:574
static const struct m0_layout_enum_ops linear_enum_ops
Definition: linear_enum.c:76
struct m0_striped_layout * le_sl
Definition: layout.h:420
uint32_t(* leo_nr)(const struct m0_layout_enum *e)
Definition: layout.h:431
M0_INTERNAL void m0_fid_convert_gob2cob(const struct m0_fid *gob_fid, struct m0_fid *cob_fid, uint32_t device_id)
Definition: fid_convert.c:55
#define out(...)
Definition: gen.c:41
struct m0_fid gfid
Definition: dir.c:626
M0_BOB_DEFINE(static, &linear_bob, m0_layout_linear_enum)
static m0_bcount_t linear_max_recsize(void)
Definition: linear_enum.c:211
M0_INTERNAL bool m0_fid_is_valid(const struct m0_fid *fid)
Definition: fid.c:96
static void linear_unregister(struct m0_layout_domain *dom, const struct m0_layout_enum_type *et)
Definition: linear_enum.c:205
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
#define offsetof(typ, memb)
Definition: misc.h:29
struct m0_layout_enum_type m0_linear_enum_type
Definition: linear_enum.c:342
m0_layout_xcode_op
Definition: layout.h:161
Definition: tx.h:280
uint64_t l_id
Definition: layout.h:225