Motr  M0
isc_service.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 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_ISCS
24 #include "lib/trace.h"
25 
26 #include "reqh/reqh_service.h"
27 #include "reqh/reqh.h"
28 #include "iscservice/isc_service.h"
29 #include "iscservice/isc_fops.h"
30 #include "lib/hash.h"
31 #include "module/instance.h" /* m0_get() */
32 #include "lib/memory.h"
33 #include "fid/fid.h"
34 #include "motr/magic.h"
35 #include "iscservice/isc.h"
36 
37 static int iscs_allocate(struct m0_reqh_service **service,
38  const struct m0_reqh_service_type *stype);
39 static void iscs_fini(struct m0_reqh_service *service);
40 
41 static int iscs_start(struct m0_reqh_service *service);
42 static void iscs_stop(struct m0_reqh_service *service);
43 
44 static bool comp_key_eq(const void *key1, const void *key2)
45 {
46  return m0_fid_eq(key1, key2);
47 }
48 
49 static uint64_t comp_hash_func(const struct m0_htable *htable, const void *k)
50 {
51  return m0_fid_hash(k) % htable->h_bucket_nr;
52 }
53 
54 M0_HT_DESCR_DEFINE(m0_isc, "Hash table for compute functions", M0_INTERNAL,
55  struct m0_isc_comp, ic_hlink, ic_magic,
57  ic_fid, comp_hash_func, comp_key_eq);
58 
59 M0_HT_DEFINE(m0_isc, M0_INTERNAL, struct m0_isc_comp, struct m0_fid);
60 
61 enum {
63 };
64 
65 static const struct m0_reqh_service_type_ops iscs_type_ops = {
67 };
68 
69 static const struct m0_reqh_service_ops iscs_ops = {
71  .rso_start_async = m0_reqh_service_async_start_simple,
72  .rso_stop = iscs_stop,
73  .rso_fini = iscs_fini
74 };
75 
77  .rst_name = "M0_CST_ISCS",
78  .rst_ops = &iscs_type_ops,
79  .rst_level = M0_RS_LEVEL_NORMAL,
80  .rst_typecode = M0_CST_ISCS,
81 };
82 
83 M0_INTERNAL struct m0_htable *m0_isc_htable_get(void)
84 {
85  return m0_get()->i_moddata[M0_MODULE_ISC];
86 }
87 
88 M0_INTERNAL int m0_isc_mod_init(void)
89 {
90  struct m0_htable *isc_htable;
91 
92  M0_ALLOC_PTR(isc_htable);
93  if (isc_htable == NULL)
94  return M0_ERR(-ENOMEM);
95  m0_get()->i_moddata[M0_MODULE_ISC] = isc_htable;
96  return 0;
97 }
98 
99 M0_INTERNAL void m0_isc_mod_fini(void)
100 {
102 }
103 
104 M0_INTERNAL int m0_iscs_register(void)
105 {
106  int rc;
107 
108  M0_ENTRY();
110  M0_ASSERT(rc == 0);
112  if (rc != 0)
113  return M0_ERR_INFO(rc, "Fop initialization failed");
115  M0_LEAVE();
116  return M0_RC(0);
117 }
118 
119 M0_INTERNAL void m0_iscs_unregister(void)
120 {
123 }
124 
126  const struct m0_reqh_service_type *stype)
127 {
128  struct m0_reqh_isc_service *isc_svc;
129 
130  M0_PRE(service != NULL && stype != NULL);
131 
132  M0_ALLOC_PTR(isc_svc);
133  if (isc_svc == NULL)
134  return M0_ERR(-ENOMEM);
135 
137 
138  *service = &isc_svc->riscs_gen;
139  (*service)->rs_ops = &iscs_ops;
140 
141  return 0;
142 }
143 
144 static int iscs_start(struct m0_reqh_service *service)
145 {
146  int rc = 0;
147 
148  M0_ENTRY();
149  rc = m0_isc_htable_init(m0_isc_htable_get(), ISC_HT_BUCKET_NR);
150  if (rc != 0)
151  return M0_ERR(rc);
152  M0_LEAVE();
153  return rc;
154 }
155 
156 static void iscs_stop(struct m0_reqh_service *service)
157 {
158  struct m0_isc_comp *isc_comp;
159 
160  M0_ENTRY();
161  m0_htable_for(m0_isc, isc_comp, m0_isc_htable_get()) {
162  M0_ASSERT(isc_comp->ic_ref_count == 0);
163  m0_isc_comp_unregister(&isc_comp->ic_fid);
165  m0_isc_htable_fini(m0_isc_htable_get());
166  M0_LEAVE();
167 }
168 
169 static void iscs_fini(struct m0_reqh_service *service)
170 {
171  struct m0_reqh_isc_service *isc_svc;
172 
173  M0_PRE(service != NULL);
174 
175  isc_svc = M0_AMB(isc_svc, service, riscs_gen);
176  m0_free(isc_svc);
177 }
178 
179 #undef M0_TRACE_SUBSYSTEM
180 
181 /*
182  * Local variables:
183  * c-indentation-style: "K&R"
184  * c-basic-offset: 8
185  * tab-width: 8
186  * fill-column: 80
187  * scroll-step: 1
188  * End:
189  */
uint64_t riscs_magic
Definition: isc_service.h:79
static uint64_t comp_hash_func(const struct m0_htable *htable, const void *k)
Definition: isc_service.c:49
uint32_t ic_ref_count
Definition: isc_service.h:69
M0_INTERNAL void m0_iscs_unregister(void)
Definition: isc_service.c:119
#define M0_PRE(cond)
int(* rso_start)(struct m0_reqh_service *service)
Definition: reqh_service.h:360
#define m0_htable_for(name, var, htable)
Definition: hash.h:483
M0_INTERNAL uint64_t m0_fid_hash(const struct m0_fid *fid)
Definition: fid.c:295
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_isc_mod_fini(void)
Definition: isc_service.c:99
M0_LEAVE()
M0_INTERNAL struct m0 * m0_get(void)
Definition: instance.c:41
static void iscs_stop(struct m0_reqh_service *service)
Definition: isc_service.c:156
int m0_reqh_service_type_register(struct m0_reqh_service_type *rstype)
Definition: reqh_service.c:473
return M0_RC(rc)
static bool comp_key_eq(const void *key1, const void *key2)
Definition: isc_service.c:44
#define M0_ENTRY(...)
Definition: trace.h:170
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
return M0_ERR(-EOPNOTSUPP)
struct m0_reqh_service riscs_gen
Definition: isc_service.h:78
M0_HT_DEFINE(m0_isc, M0_INTERNAL, struct m0_isc_comp, struct m0_fid)
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
static const struct socktype stype[]
Definition: sock.c:1156
M0_INTERNAL int m0_iscservice_fop_init(void)
Definition: isc_fops.c:41
#define M0_ASSERT(cond)
M0_INTERNAL int m0_isc_mod_init(void)
Definition: isc_service.c:88
M0_INTERNAL void m0_isc_fom_type_init(void)
Definition: isc.c:480
const char * rst_name
Definition: reqh_service.h:447
M0_INTERNAL void m0_iscservice_fop_fini(void)
Definition: isc_fops.c:65
struct m0_fid ic_fid
Definition: isc_service.h:51
static const struct m0_reqh_service_type_ops iscs_type_ops
Definition: isc_service.c:65
M0_INTERNAL struct m0_htable * m0_isc_htable_get(void)
Definition: isc_service.c:83
M0_INTERNAL void m0_isc_comp_unregister(const struct m0_fid *fid)
Definition: isc.c:652
int m0_reqh_service_async_start_simple(struct m0_reqh_service_start_async_ctx *asc)
Definition: reqh_service.c:601
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
int(* rsto_service_allocate)(struct m0_reqh_service **service, const struct m0_reqh_service_type *stype)
Definition: reqh_service.h:435
Definition: fid.h:38
struct m0_reqh_service_type m0_iscs_type
Definition: isc_service.c:76
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
void * i_moddata[M0_MODULE_NR]
Definition: instance.h:94
static int iscs_allocate(struct m0_reqh_service **service, const struct m0_reqh_service_type *stype)
Definition: isc_service.c:125
static const struct m0_reqh_service_ops iscs_ops
Definition: isc_service.c:69
void m0_free(void *data)
Definition: memory.c:146
#define m0_htable_endfor
Definition: hash.h:491
static void iscs_fini(struct m0_reqh_service *service)
Definition: isc_service.c:169
void m0_reqh_service_type_unregister(struct m0_reqh_service_type *rstype)
Definition: reqh_service.c:490
static struct m0_reqh_service * service[REQH_IN_UT_MAX]
Definition: long_lock_ut.c:46
int32_t rc
Definition: trigger_fop.h:47
uint64_t h_bucket_nr
Definition: hash.h:178
M0_HT_DESCR_DEFINE(m0_isc, "Hash table for compute functions", M0_INTERNAL, struct m0_isc_comp, ic_hlink, ic_magic, M0_ISC_COMP_MAGIC, M0_ISC_TLIST_HEAD_MAGIC, ic_fid, comp_hash_func, comp_key_eq)
const struct m0_reqh_service_ops * rs_ops
Definition: reqh_service.h:254
M0_INTERNAL int m0_iscs_register(void)
Definition: isc_service.c:104
static int iscs_start(struct m0_reqh_service *service)
Definition: isc_service.c:144