Motr  M0
encdec.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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 
32 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_DIX
33 #include "lib/trace.h"
34 
35 #include "lib/memory.h" /* m0_alloc */
36 #include "lib/errno.h" /* ENOMEM */
37 #include "lib/vec.h"
38 #include "xcode/xcode.h"
39 #include "dix/layout.h"
40 #include "dix/encdec.h"
41 #include "dix/encdec_xc.h"
42 
43 #define DIX_META_VAL_XCODE_OBJ(ptr) M0_XCODE_OBJ(dix_meta_val_xc, ptr)
44 #define DIX_LAYOUT_VAL_XCODE_OBJ(ptr) M0_XCODE_OBJ(m0_dix_layout_xc, ptr)
45 #define DIX_LDESC_VAL_XCODE_OBJ(ptr) M0_XCODE_OBJ(m0_dix_ldesc_xc, ptr)
46 #define DIX_FID_VAL_XCODE_OBJ(ptr) M0_XCODE_OBJ(m0_fid_xc, ptr)
47 
48 M0_INTERNAL int m0_dix__meta_val_enc(const struct m0_fid *fid,
49  const struct m0_dix_ldesc *dld,
50  uint32_t nr,
51  struct m0_bufvec *vals)
52 {
53  int rc;
54  uint32_t i;
55 
56  M0_PRE(fid != NULL);
57  M0_PRE(dld != NULL);
58  M0_PRE(vals != NULL);
59  rc = m0_bufvec_empty_alloc(vals, nr);
60  if (rc != 0)
61  return M0_ERR(rc);
62  for (i = 0; rc == 0 && i < nr; i++) {
63  struct dix_meta_val x = { .mv_fid = fid[i], .mv_dld = dld[i] };
64 
66  &vals->ov_buf[i],
67  &vals->ov_vec.v_count[i]);
68  }
69  if (rc != 0)
70  m0_bufvec_free(vals);
71  return rc;
72 }
73 
74 M0_INTERNAL int m0_dix__meta_val_dec(const struct m0_bufvec *vals,
75  struct m0_fid *out_fid,
76  struct m0_dix_ldesc *out_dld,
77  uint32_t nr)
78 {
79  int rc = 0;
80  uint32_t i;
81 
82  M0_ENTRY();
83  M0_PRE(vals != NULL);
84  M0_PRE(out_fid != NULL);
85  M0_PRE(out_dld != NULL);
86  for (i = 0; i < nr; i++) {
87  struct dix_meta_val x;
88 
89  M0_SET0(&x);
91  vals->ov_buf[i],
92  vals->ov_vec.v_count[i]);
93  if (rc == 0) {
94  out_fid[i] = x.mv_fid;
95  rc = m0_dix_ldesc_copy(&out_dld[i], &x.mv_dld);
96  m0_dix_ldesc_fini(&x.mv_dld);
97  }
98  if (rc != 0)
99  break;
100  }
101  if (rc != 0)
102  /*
103  * We've got an error at step i. Finalise already created
104  * descriptors.
105  */
106  while(i != 0)
107  m0_dix_ldesc_fini(&out_dld[--i]);
108  return M0_RC(rc);
109 }
110 
111 M0_INTERNAL int m0_dix__layout_vals_enc(const struct m0_fid *fid,
112  const struct m0_dix_layout *dlay,
113  uint32_t nr,
114  struct m0_bufvec *keys,
115  struct m0_bufvec *vals)
116 {
117  int rc = 0;
118  uint32_t i;
119  bool enc_keys = keys != NULL;
120  bool enc_vals = vals != NULL;
121 
122  M0_PRE((fid != NULL) == (keys != NULL));
123  M0_PRE((dlay != NULL) == (vals != NULL));
124  M0_PRE(enc_keys || enc_vals);
125  if (enc_keys) {
126  rc = m0_bufvec_empty_alloc(keys, nr);
127  if (rc != 0)
128  return M0_ERR(rc);
129  }
130  if (enc_vals) {
131  rc = m0_bufvec_empty_alloc(vals, nr);
132  if (rc != 0)
133  return M0_ERR(-ENOMEM);
134  }
135  for (i = 0; rc == 0 && i < nr; i++) {
136  if (enc_keys)
139  (struct m0_fid *)&fid[i]),
140  &keys->ov_buf[i],
141  &keys->ov_vec.v_count[i]);
142  if (enc_vals)
145  (struct m0_dix_layout *)dlay),
146  &vals->ov_buf[i],
147  &vals->ov_vec.v_count[i]);
148  }
149  if (rc != 0) {
150  m0_bufvec_free(keys);
151  m0_bufvec_free(vals);
152  }
153  return rc;
154 }
155 
156 /* Decoding key and val bufvecs and returns fid and dix_layout. */
157 M0_INTERNAL int m0_dix__layout_vals_dec(const struct m0_bufvec *keys,
158  const struct m0_bufvec *vals,
159  struct m0_fid *out_fid,
160  struct m0_dix_layout *out_dlay,
161  uint32_t nr)
162 {
163  int rc = 0;
164  uint32_t i;
165  bool dec_keys = out_fid != NULL;
166  bool dec_vals = out_dlay != NULL;
167 
168  M0_PRE((out_fid != NULL) == (keys != NULL));
169  M0_PRE((out_dlay != NULL) == (vals != NULL));
170  M0_PRE(dec_keys || dec_vals);
171  for (i = 0; rc == 0 && i < nr; i++) {
172  if (dec_keys)
174  &DIX_FID_VAL_XCODE_OBJ(&out_fid[i]),
175  keys->ov_buf[i], keys->ov_vec.v_count[i]);
176  if (dec_vals) {
177  M0_SET0(out_dlay);
179  &DIX_LAYOUT_VAL_XCODE_OBJ(out_dlay),
180  vals->ov_buf[i], vals->ov_vec.v_count[i]);
181  if (rc != 0)
182  break;
183  }
184  }
185  /*
186  * We've got an error at i'th step. Already created layout descriptors
187  * should be finalised.
188  */
189  if (rc != 0)
190  while(i != 0)
191  m0_dix_ldesc_fini(&out_dlay[--i].u.dl_desc);
192  return rc;
193 }
194 
195 M0_INTERNAL int m0_dix__ldesc_vals_enc(const uint64_t *lid,
196  const struct m0_dix_ldesc *ldesc,
197  uint32_t nr,
198  struct m0_bufvec *keys,
199  struct m0_bufvec *vals)
200 {
201  int rc = 0;
202  uint32_t i;
203  bool enc_keys = keys != NULL;
204  bool enc_vals = vals != NULL;
205 
206  M0_PRE((lid != NULL) == (keys != NULL));
207  M0_PRE((ldesc != NULL) == (vals != NULL));
208  M0_PRE(enc_keys || enc_vals);
209  if (enc_keys) {
210  rc = m0_bufvec_alloc(keys, nr, sizeof *lid);
211  if (rc != 0)
212  return M0_ERR(rc);
213  }
214  if (enc_vals)
215  rc = m0_bufvec_empty_alloc(vals, nr);
216  for (i = 0; rc == 0 && i < nr; i++) {
217  if (enc_keys)
218  *(uint64_t *)keys->ov_buf[i] = lid[i];
219  if (enc_vals) {
220  struct m0_dix_ldesc *x = (struct m0_dix_ldesc *)ldesc;
223  &vals->ov_buf[i],
224  &vals->ov_vec.v_count[i]);
225  }
226  }
227  if (rc != 0) {
228  m0_bufvec_free(keys);
229  m0_bufvec_free(vals);
230  }
231  return rc;
232 }
233 
234 M0_INTERNAL int m0_dix__ldesc_vals_dec(const struct m0_bufvec *keys,
235  const struct m0_bufvec *vals,
236  uint64_t *out_lid,
237  struct m0_dix_ldesc *out_ldesc,
238  uint32_t nr)
239 {
240  int rc = 0;
241  uint32_t i;
242  bool dec_keys = out_lid != NULL;
243  bool dec_vals = out_ldesc != NULL;
244 
245  M0_PRE((out_lid != NULL) == (keys != NULL));
246  M0_PRE((out_ldesc != NULL) == (vals != NULL));
247  M0_PRE(dec_keys || dec_vals);
248  for (i = 0; rc == 0 && i < nr; i++) {
249  if (dec_keys)
250  out_lid[i] = *(uint64_t *)keys->ov_buf[i];
251  if (dec_vals) {
252  struct m0_dix_ldesc x;
253 
254  M0_SET0(&x);
257  vals->ov_buf[i], vals->ov_vec.v_count[i]);
258  if (rc == 0) {
259  rc = m0_dix_ldesc_copy(&out_ldesc[i], &x);
261  }
262  }
263  }
264  /*
265  * We've got an error for in step i. In case i != 0 we must fini
266  * already created ldesc.
267  */
268  if (rc != 0)
269  while(i != 0)
270  m0_dix_ldesc_fini(&out_ldesc[--i]);
271  return rc;
272 }
273 #undef M0_TRACE_SUBSYSTEM
274 
277 /*
278  * Local variables:
279  * c-indentation-style: "K&R"
280  * c-basic-offset: 8
281  * tab-width: 8
282  * fill-column: 80
283  * scroll-step: 1
284  * End:
285  */
286 /*
287  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
288  */
static size_t nr
Definition: dump.c:1505
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
static bool x
Definition: sm.c:168
union @126 u
struct m0_vec ov_vec
Definition: vec.h:147
#define DIX_META_VAL_XCODE_OBJ(ptr)
Definition: encdec.c:43
#define DIX_LDESC_VAL_XCODE_OBJ(ptr)
Definition: encdec.c:45
#define M0_SET0(obj)
Definition: misc.h:64
void ** ov_buf
Definition: vec.h:149
M0_INTERNAL int m0_dix_ldesc_copy(struct m0_dix_ldesc *dst, const struct m0_dix_ldesc *src)
Definition: layout.c:189
M0_INTERNAL int m0_dix__layout_vals_dec(const struct m0_bufvec *keys, const struct m0_bufvec *vals, struct m0_fid *out_fid, struct m0_dix_layout *out_dlay, uint32_t nr)
Definition: encdec.c:157
struct m0_fid fid
Definition: di.c:46
M0_INTERNAL int m0_bufvec_alloc(struct m0_bufvec *bufvec, uint32_t num_segs, m0_bcount_t seg_size)
Definition: vec.c:220
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL void m0_bufvec_free(struct m0_bufvec *bufvec)
Definition: vec.c:395
int i
Definition: dir.c:1033
M0_INTERNAL int m0_dix__ldesc_vals_enc(const uint64_t *lid, const struct m0_dix_ldesc *ldesc, uint32_t nr, struct m0_bufvec *keys, struct m0_bufvec *vals)
Definition: encdec.c:195
M0_INTERNAL int m0_dix__layout_vals_enc(const struct m0_fid *fid, const struct m0_dix_layout *dlay, uint32_t nr, struct m0_bufvec *keys, struct m0_bufvec *vals)
Definition: encdec.c:111
return M0_ERR(-EOPNOTSUPP)
if(value==NULL)
Definition: dir.c:350
M0_INTERNAL int m0_xcode_obj_enc_to_buf(struct m0_xcode_obj *obj, void **buf, m0_bcount_t *len)
Definition: xcode.c:832
M0_INTERNAL int m0_dix__ldesc_vals_dec(const struct m0_bufvec *keys, const struct m0_bufvec *vals, uint64_t *out_lid, struct m0_dix_ldesc *out_ldesc, uint32_t nr)
Definition: encdec.c:234
m0_bcount_t * v_count
Definition: vec.h:53
M0_INTERNAL void m0_dix_ldesc_fini(struct m0_dix_ldesc *ld)
Definition: layout.c:197
M0_INTERNAL int m0_dix__meta_val_dec(const struct m0_bufvec *vals, struct m0_fid *out_fid, struct m0_dix_ldesc *out_dld, uint32_t nr)
Definition: encdec.c:74
Definition: fid.h:38
M0_INTERNAL int m0_xcode_obj_dec_from_buf(struct m0_xcode_obj *obj, void *buf, m0_bcount_t len)
Definition: xcode.c:850
#define DIX_LAYOUT_VAL_XCODE_OBJ(ptr)
Definition: encdec.c:44
#define DIX_FID_VAL_XCODE_OBJ(ptr)
Definition: encdec.c:46
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL int m0_dix__meta_val_enc(const struct m0_fid *fid, const struct m0_dix_ldesc *dld, uint32_t nr, struct m0_bufvec *vals)
Definition: encdec.c:48
Definition: vec.h:145
M0_INTERNAL int m0_bufvec_empty_alloc(struct m0_bufvec *bufvec, uint32_t num_segs)
Definition: vec.c:213