Motr  M0
filterc.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 #include "lib/finject.h" /* MO_FI_ENABLED */
26 
27 #include "filterc.h"
28 
29 #ifndef __KERNEL__
30 static int m0_filterc_start(struct m0_filterc_ctx *ctx,
31  struct m0_reqh *reqh);
32 static void m0_filterc_stop(struct m0_filterc_ctx *ctx);
33 static int m0_filterc_open(struct m0_filterc_ctx *ctx,
34  enum m0_fdmi_rec_type_id rec_type_id,
35  struct m0_filterc_iter *iter);
36 static int m0_filterc_get_next(struct m0_filterc_iter *iter,
37  struct m0_conf_fdmi_filter **out);
38 static void m0_filterc_close(struct m0_filterc_iter *iter);
39 
40 
43  .fco_stop = m0_filterc_stop,
44  .fco_open = m0_filterc_open,
45  .fco_get_next = m0_filterc_get_next,
46  .fco_close = m0_filterc_close
47 };
48 
49 M0_INTERNAL void m0_filterc_ctx_init(struct m0_filterc_ctx *ctx,
50  const struct m0_filterc_ops *ops)
51 
52 {
53  M0_ENTRY();
54  M0_PRE(M0_IS0(ctx));
55  ctx->fcc_ops = ops;
56  M0_LEAVE();
57 }
58 
60 static int m0_filterc_start(struct m0_filterc_ctx *ctx,
61  struct m0_reqh *reqh)
62 {
63  M0_ENTRY();
64  M0_PRE(reqh != NULL);
65 
66  ctx->fcc_confc = m0_reqh2confc(reqh);
67  if (ctx->fcc_confc == NULL)
68  return M0_RC(-EINVAL);
69  return M0_RC(0);
70 
71 }
72 
73 static void m0_filterc_stop(struct m0_filterc_ctx *ctx)
74 {
75  M0_ENTRY();
76  ctx->fcc_confc = NULL;
77  M0_LEAVE();
78 }
79 
80 M0_INTERNAL void m0_filterc_ctx_fini(struct m0_filterc_ctx *ctx)
81 {
82  M0_ENTRY();
83  M0_LEAVE();
84 }
85 
87  enum m0_fdmi_rec_type_id rec_type_id,
88  struct m0_conf_obj **out)
89 {
90  int rc;
91  struct m0_conf_obj *flt_grp = NULL;
92  struct m0_conf_obj *flt_grp_tmp;
93  struct m0_conf_obj *flt_grp_dir;
94  struct m0_conf_obj *flts_dir;
95 
96  M0_ENTRY();
97 
99  if (ctx->fcc_confc == NULL || ctx->fcc_confc->cc_root == NULL)
100  return M0_RC(-EINVAL);
101 
102  rc = m0_confc_open_sync(&flt_grp_dir, ctx->fcc_confc->cc_root,
103  M0_CONF_ROOT_FDMI_FLT_GRPS_FID);
104  if (rc != 0)
105  goto open_err;
106  for (flt_grp_tmp = NULL;
107  (rc = m0_confc_readdir_sync(flt_grp_dir, &flt_grp_tmp)) > 0; ) {
108  struct m0_conf_fdmi_flt_grp *grp =
109  M0_CONF_CAST(flt_grp_tmp, m0_conf_fdmi_flt_grp);
110 
111  if (grp->ffg_rec_type == rec_type_id) {
112  flt_grp = flt_grp_tmp;
113  break;
114  }
116  }
117 
118  if (flt_grp != NULL) {
119  rc = m0_confc_open_sync(&flts_dir, flt_grp,
120  M0_CONF_FDMI_FGRP_FILTERS_FID);
121  if (rc == 0)
122  *out = flts_dir;
123  } else {
124  rc = (rc < 0) ? rc : -ENOENT;
125  }
126 
127  m0_confc_close(flt_grp_tmp);
128  m0_confc_close(flt_grp_dir);
129 open_err:
130  return M0_RC(rc);
131 }
132 
133 static int m0_filterc_open(struct m0_filterc_ctx *ctx,
134  enum m0_fdmi_rec_type_id rec_type_id,
135  struct m0_filterc_iter *iter)
136 {
137  int rc;
138 
139  M0_ENTRY();
140  rc = open_filter_group(ctx, rec_type_id, &iter->fci_dir);
141 
142  if (rc != 0)
143  goto find_err;
144 
145  iter->fci_filterc_ctx = ctx;
146  iter->fci_cur_flt = NULL;
147 find_err:
148  return M0_RC(rc);
149 }
150 
151 static int m0_filterc_get_next(struct m0_filterc_iter *iter,
152  struct m0_conf_fdmi_filter **out)
153 {
154  int rc;
155 
156  M0_ENTRY();
157  rc = m0_confc_readdir_sync((struct m0_conf_obj *)iter->fci_dir,
158  (struct m0_conf_obj **)&iter->fci_cur_flt);
159 
160  if (rc > 0) {
161  *out = iter->fci_cur_flt;
162  }
163 
164  return M0_RC(rc);
165 }
166 
167 static void m0_filterc_close(struct m0_filterc_iter *iter)
168 {
169  M0_ENTRY();
170  m0_confc_close((struct m0_conf_obj *)iter->fci_cur_flt);
171  m0_confc_close((struct m0_conf_obj *)iter->fci_dir);
172  M0_LEAVE();
173 }
174 
175 #endif /* __KERNEL__ */
176 
177 #undef M0_TRACE_SUBSYSTEM
178 
179 /*
180  * Local variables:
181  * c-indentation-style: "K&R"
182  * c-basic-offset: 8
183  * tab-width: 8
184  * fill-column: 80
185  * scroll-step: 1
186  * End:
187  */
188 /*
189  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
190  */
#define M0_PRE(cond)
struct m0_conf_fdmi_filter * fci_cur_flt
Definition: filterc.h:124
const struct m0_filterc_ops filterc_def_ops
Definition: filterc.c:41
#define NULL
Definition: misc.h:38
static struct m0_sm_group * grp
Definition: bytecount.c:38
M0_LEAVE()
static int m0_filterc_open(struct m0_filterc_ctx *ctx, enum m0_fdmi_rec_type_id rec_type_id, struct m0_filterc_iter *iter)
Definition: filterc.c:133
struct m0_filterc_ctx * fci_filterc_ctx
Definition: filterc.h:122
static int open_filter_group(struct m0_filterc_ctx *ctx, enum m0_fdmi_rec_type_id rec_type_id, struct m0_conf_obj **out)
Definition: filterc.c:86
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL struct m0_confc * m0_reqh2confc(struct m0_reqh *reqh)
Definition: reqh.c:753
M0_INTERNAL void m0_filterc_ctx_init(struct m0_filterc_ctx *ctx, const struct m0_filterc_ops *ops)
Definition: filterc.c:49
M0_INTERNAL void m0_filterc_ctx_fini(struct m0_filterc_ctx *ctx)
Definition: filterc.c:80
Definition: reqh.h:94
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
m0_fdmi_rec_type_id
Definition: fdmi.h:234
#define m0_confc_open_sync(result, origin,...)
Definition: confc.h:707
static struct fdmi_ctx ctx
Definition: main.c:80
struct m0_reqh reqh
Definition: rm_foms.c:48
#define M0_IS0(obj)
Definition: misc.h:70
int(* fco_start)(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: filterc.h:56
M0_INTERNAL void m0_confc_close(struct m0_conf_obj *obj)
Definition: confc.c:921
Definition: nucleus.c:42
#define out(...)
Definition: gen.c:41
static void m0_filterc_close(struct m0_filterc_iter *iter)
Definition: filterc.c:167
struct m0_fom_ops ops
Definition: io_foms.c:623
struct m0_conf_obj * fci_dir
Definition: filterc.h:123
M0_INTERNAL int m0_confc_readdir_sync(struct m0_conf_obj *dir, struct m0_conf_obj **pptr)
Definition: confc.c:1020
int32_t rc
Definition: trigger_fop.h:47
static void m0_filterc_stop(struct m0_filterc_ctx *ctx)
Definition: filterc.c:73
static int m0_filterc_get_next(struct m0_filterc_iter *iter, struct m0_conf_fdmi_filter **out)
Definition: filterc.c:151
static int m0_filterc_start(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: filterc.c:60