Motr  M0
filter.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-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_FDMI
24 #include "lib/trace.h"
25 
26 #include "lib/memory.h"
27 #include "lib/errno.h"
28 #include "lib/finject.h"
29 #include "fdmi/filter.h"
30 #include "fdmi/filter_xc.h"
31 
32 M0_INTERNAL int m0_fdmi_flt_node_xc_type(const struct m0_xcode_obj *par,
33  const struct m0_xcode_type **out)
34 {
35  *out = m0_fdmi_flt_node_xc;
36  return 0;
37 }
38 
39 M0_INTERNAL void m0_fdmi_filter_init(struct m0_fdmi_filter *flt)
40 {
41  M0_ENTRY();
42  M0_PRE(flt != NULL);
43 
44  flt->ff_root = NULL;
45 
46  M0_LEAVE();
47 }
48 
49 M0_INTERNAL void m0_fdmi_filter_root_set(struct m0_fdmi_filter *flt,
50  struct m0_fdmi_flt_node *root)
51 {
52  M0_ENTRY();
53  M0_PRE(flt != NULL);
54  M0_PRE(root != NULL);
55  flt->ff_root = root;
56  M0_LEAVE();
57 }
58 
60 {
61  struct m0_fdmi_flt_operand *opnd = &node->ffn_u.ffn_operand;
62 
63  M0_ENTRY("node %p", node);
64 
65  if (opnd->ffo_data.fpl_type == M0_FF_OPND_PLD_BUF) {
66  m0_buf_free(&opnd->ffo_data.fpl_pld.fpl_buf);
67  }
68 
69  m0_free(node);
70  M0_LEAVE();
71 }
72 
74 {
75  struct m0_fdmi_flt_var_node *var_node = &node->ffn_u.ffn_var;
76 
77  M0_ENTRY("node %p", node);
78  m0_buf_free(&var_node->ffvn_data);
79  m0_free(node);
80  M0_LEAVE();
81 }
82 
83 static void free_flt_node(struct m0_fdmi_flt_node *node)
84 {
85  int i;
86 
87  M0_PRE(node != NULL);
88 
89  switch (node->ffn_type) {
91  for (i = 0; i < node->ffn_u.ffn_oper.ffon_opnds.fno_cnt; i ++) {
93  node->ffn_u.ffn_oper.ffon_opnds.
94  fno_opnds[i].ffnp_ptr);
95  }
96  m0_free(node->ffn_u.ffn_oper.ffon_opnds.fno_opnds);
97  m0_free(node);
98  break;
99 
100  case M0_FLT_OPERAND_NODE:
102  break;
103 
106  break;
107 
108  default:
109  M0_ASSERT(false);
110  }
111 }
112 
113 M0_INTERNAL void m0_fdmi_filter_fini(struct m0_fdmi_filter *flt)
114 {
115  M0_ENTRY("flt=%p", flt);
116 
117  if (flt->ff_root != NULL) {
118  free_flt_node(flt->ff_root);
119  flt->ff_root = NULL;
120  }
121 
122  M0_LEAVE();
123 }
124 
125 M0_INTERNAL void m0_fdmi_flt_bool_opnd_fill(
126  struct m0_fdmi_flt_operand *opnd, bool value)
127 {
128  M0_ENTRY("opnd %p, value %d", opnd, value);
129  M0_PRE(opnd != NULL);
130  opnd->ffo_type = M0_FF_OPND_BOOL;
132  opnd->ffo_data.fpl_pld.fpl_boolean = value;
133  M0_LEAVE();
134 }
135 
137 {
138  struct m0_fdmi_flt_node *ret;
139 
140  M0_ENTRY("value=%d", value);
141 
142  M0_ALLOC_PTR(ret);
143 
144  if (ret != NULL) {
146  m0_fdmi_flt_bool_opnd_fill(&ret->ffn_u.ffn_operand, value);
147  }
148 
149  M0_LEAVE("ret=%p", ret);
150  return ret;
151 }
152 
153 M0_INTERNAL void m0_fdmi_flt_int_opnd_fill(
154  struct m0_fdmi_flt_operand *opnd, int64_t value)
155 {
156  M0_ENTRY("opnd %p, value %"PRId64, opnd, value);
157  M0_PRE(opnd != NULL);
158  opnd->ffo_type = M0_FF_OPND_INT;
160  opnd->ffo_data.fpl_pld.fpl_integer = value;
161  M0_LEAVE();
162 }
163 
165 {
166  struct m0_fdmi_flt_node *ret;
167 
168  M0_ENTRY("value=%"PRId64, value);
169 
170  M0_ALLOC_PTR(ret);
171 
172  if (ret != NULL) {
174  m0_fdmi_flt_int_opnd_fill(&ret->ffn_u.ffn_operand, value);
175  }
176 
177  M0_LEAVE("ret=%p", ret);
178  return ret;
179 }
180 
181 M0_INTERNAL void m0_fdmi_flt_uint_opnd_fill(
182  struct m0_fdmi_flt_operand *opnd, uint64_t value)
183 {
184  M0_ENTRY("opnd %p, value %"PRIu64, opnd, value);
185  M0_PRE(opnd != NULL);
186  opnd->ffo_type = M0_FF_OPND_UINT;
188  opnd->ffo_data.fpl_pld.fpl_uinteger = value;
189  M0_LEAVE();
190 }
191 
193 {
194  struct m0_fdmi_flt_node *ret;
195 
196  M0_ENTRY("value=%"PRIu64, value);
197 
198  M0_ALLOC_PTR(ret);
199 
200  if (ret != NULL) {
202  m0_fdmi_flt_uint_opnd_fill(&ret->ffn_u.ffn_operand, value);
203  }
204 
205  M0_LEAVE("ret=%p", ret);
206  return ret;
207 }
208 
210 {
211  struct m0_fdmi_flt_node *ret;
212 
213  M0_ENTRY("data="BUF_F, BUF_P(data));
214 
215  M0_ALLOC_PTR(ret);
216 
217  if (ret != NULL) {
219  ret->ffn_u.ffn_var.ffvn_data = *data;
220  }
221 
222  M0_LEAVE("ret=%p", ret);
223  return ret;
224 }
225 
226 struct
228  struct m0_fdmi_flt_node *left,
229  struct m0_fdmi_flt_node *right)
230 {
231  struct m0_fdmi_flt_node *ret;
232 
233  M0_ENTRY("op_code=%d", op_code);
234 
238  M0_ALLOC_PTR(ret);
239  M0_ALLOC_ARR(ret->ffn_u.ffn_oper.ffon_opnds.fno_opnds,
241 
243  ret->ffn_u.ffn_oper.ffon_op_code = op_code;
244  ret->ffn_u.ffn_oper.ffon_opnds.fno_cnt = !!left + !!right;
245  ret->ffn_u.ffn_oper.ffon_opnds.fno_opnds[0].ffnp_ptr = left;
246  ret->ffn_u.ffn_oper.ffon_opnds.fno_opnds[1].ffnp_ptr = right;
247 
248  M0_LEAVE("ret=%p", ret);
249  return ret;
250 }
251 
253  char **out)
254 {
255  enum {
256  FIRST_SIZE_GUESS = 256,
257  };
258 
259  int rc;
260  char *str;
261 
262  M0_ENTRY();
263 
264  M0_ALLOC_ARR(str, FIRST_SIZE_GUESS);
265 
266  if (str == NULL) {
267  rc = -ENOMEM;
268  goto fail;
269  }
270 
271  rc = m0_xcode_print(&M0_XCODE_OBJ(m0_fdmi_flt_node_xc, node),
272  str, FIRST_SIZE_GUESS);
273 
274  if (rc >= FIRST_SIZE_GUESS || M0_FI_ENABLED("rc_bigger_than_size_guess")) {
275  m0_free(str);
276  M0_ALLOC_ARR(str, rc + 1);
277 
278  if (str == NULL) {
279  rc = -ENOMEM;
280  } else {
281  rc = m0_xcode_print(
282  &M0_XCODE_OBJ(m0_fdmi_flt_node_xc, node),
283  str, rc);
284  }
285  }
286 
287  if (rc > 0)
288  rc = 0;
289  else
290  m0_free0(&str);
291 
292 fail:
293  *out = str;
294 
295  return M0_RC(rc);
296 }
297 
298 M0_INTERNAL int m0_fdmi_flt_node_parse(const char *str,
299  struct m0_fdmi_flt_node *node)
300 {
301  return M0_RC(
302  m0_xcode_read(&M0_XCODE_OBJ(m0_fdmi_flt_node_xc, node), str));
303 }
304 
305 #undef M0_TRACE_SUBSYSTEM
306 
307 /*
308  * Local variables:
309  * c-indentation-style: "K&R"
310  * c-basic-offset: 8
311  * tab-width: 8
312  * fill-column: 80
313  * scroll-step: 1
314  * End:
315  */
316 /*
317  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
318  */
static void free_flt_node(struct m0_fdmi_flt_node *node)
Definition: filter.c:83
M0_INTERNAL int m0_xcode_print(const struct m0_xcode_obj *obj, char *str, int nr)
Definition: string.c:278
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_fdmi_filter_init(struct m0_fdmi_filter *flt)
Definition: filter.c:39
struct m0_fdmi_flt_node * m0_fdmi_flt_uint_node_create(uint64_t value)
Definition: filter.c:192
M0_INTERNAL void m0_fdmi_flt_bool_opnd_fill(struct m0_fdmi_flt_operand *opnd, bool value)
Definition: filter.c:125
M0_LEAVE()
M0_INTERNAL int m0_xcode_read(struct m0_xcode_obj *obj, const char *str)
Definition: string.c:162
m0_fdmi_flt_op_code
Definition: filter.h:96
static struct net_test_cmd_node * node
Definition: commands.c:72
struct m0_fdmi_flt_opnd_pld ffo_data
Definition: filter.h:177
struct m0_bufvec data
Definition: di.c:40
int const char const void * value
Definition: dir.c:325
uint32_t ffo_type
Definition: filter.h:175
static int left
Definition: locality.c:280
uint32_t ffn_type
Definition: filter.h:237
#define BUF_F
Definition: buf.h:75
return M0_RC(rc)
struct m0_fdmi_flt_node * m0_fdmi_flt_bool_node_create(bool value)
Definition: filter.c:136
#define M0_ENTRY(...)
Definition: trace.h:170
Definition: buf.h:37
struct m0_fdmi_flt_node * m0_fdmi_flt_int_node_create(int64_t value)
Definition: filter.c:164
int i
Definition: dir.c:1033
#define PRIu64
Definition: types.h:58
union m0_fdmi_flt_opnd_pld::@169 fpl_pld
struct m0_conf_root * root
Definition: note.c:50
struct m0_fdmi_flt_node * m0_fdmi_flt_op_node_create(enum m0_fdmi_flt_op_code op_code, struct m0_fdmi_flt_node *left, struct m0_fdmi_flt_node *right)
Definition: filter.c:227
M0_INTERNAL void m0_fdmi_flt_uint_opnd_fill(struct m0_fdmi_flt_operand *opnd, uint64_t value)
Definition: filter.c:181
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
struct m0_fdmi_flt_node * m0_fdmi_flt_var_node_create(struct m0_buf *data)
Definition: filter.c:209
M0_INTERNAL int m0_fdmi_flt_node_parse(const char *str, struct m0_fdmi_flt_node *node)
Definition: filter.c:298
static void free_operand_node(struct m0_fdmi_flt_node *node)
Definition: filter.c:59
static void free_variable_node(struct m0_fdmi_flt_node *node)
Definition: filter.c:73
struct m0_buf ffvn_data
Definition: filter.h:193
M0_INTERNAL void m0_fdmi_filter_fini(struct m0_fdmi_filter *flt)
Definition: filter.c:113
M0_INTERNAL void m0_buf_free(struct m0_buf *buf)
Definition: buf.c:55
#define PRId64
Definition: types.h:57
M0_INTERNAL int m0_fdmi_flt_node_print(struct m0_fdmi_flt_node *node, char **out)
Definition: filter.c:252
M0_INTERNAL void m0_fdmi_filter_root_set(struct m0_fdmi_filter *flt, struct m0_fdmi_flt_node *root)
Definition: filter.c:49
struct m0_fdmi_flt_node * ff_root
Definition: filter.h:120
static uint8_t fail[DATA_UNIT_COUNT_MAX+PARITY_UNIT_COUNT_MAX]
union m0_fdmi_flt_node::@170 ffn_u
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
#define BUF_P(p)
Definition: buf.h:76
M0_INTERNAL void m0_fdmi_flt_int_opnd_fill(struct m0_fdmi_flt_operand *opnd, int64_t value)
Definition: filter.c:153
#define M0_XCODE_OBJ(type, ptr)
Definition: xcode.h:962
M0_INTERNAL int m0_fdmi_flt_node_xc_type(const struct m0_xcode_obj *par, const struct m0_xcode_type **out)
Definition: filter.c:32
#define out(...)
Definition: gen.c:41
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
uint32_t fpl_type
Definition: filter.h:158