Motr  M0
type.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-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 #include "stob/type.h"
24 
25 #include "lib/errno.h" /* -ENOMEM */
26 #include "lib/memory.h" /* M0_ALLOC_PTR */
27 #include "lib/tlist.h" /* M0_TL_DESCR_DEFINE */
28 #include "motr/magic.h" /* M0_STOB_DOMAINS_MAGIC */
29 
30 #include "stob/domain.h" /* m0_stob_domain */
31 #include "stob/module.h" /* m0_stob_module */
32 #include "stob/null.h" /* m0_stob_null_type */
33 #include "lib/string.h" /* m0_streq */
34 
35 #ifndef __KERNEL__
36 #include "stob/linux.h" /* m0_stob_linux_type */
37 #include "stob/ad.h" /* m0_stob_ad_type */
38 #include "stob/perf.h" /* m0_stob_perf_type */
39 #endif
40 
41 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_STOB
42 #include "lib/trace.h"
43 
50 static const struct m0_stob_type *stob_types_reg[] = {
52 #ifndef __KERNEL__
56 #endif
57 };
58 
59 M0_TL_DESCR_DEFINE(domains, "stob domains", static, struct m0_stob_domain,
60  sd_domain_linkage, sd_magic, M0_STOB_DOMAINS_MAGIC,
62 M0_TL_DEFINE(domains, static, struct m0_stob_domain);
63 
64 M0_TL_DESCR_DEFINE(types, "stob types", static, struct m0_stob_type,
67 M0_TL_DEFINE(types, static, struct m0_stob_type);
68 
69 static int stob_type_copy(const struct m0_stob_type *type,
70  struct m0_stob_type **copy)
71 {
73  return *copy == NULL ?
74  -ENOMEM : (memcpy(*copy, type, sizeof(*type)), 0);
75 }
76 
77 static void stob_types_destroy_list(struct m0_stob_types *types)
78 {
79  struct m0_stob_type *type;
80  while (!types_tlist_is_empty(&types->sts_stypes)) {
81  type = types_tlist_head(&types->sts_stypes);
83  /* allocated by stob_type_copy() */
84  m0_free(type);
85  }
86  types_tlist_fini(&types->sts_stypes);
87 }
88 
89 static struct m0_stob_types *stob_types_get(void)
90 {
91  return &m0_stob_module__get()->stm_types;
92 }
93 
94 M0_INTERNAL int m0_stob_types_init(void)
95 {
96  struct m0_stob_types *types = stob_types_get();
97  struct m0_stob_type *type;
98  int i;
99  int rc = 0;
100 
101  types_tlist_init(&types->sts_stypes);
102 
103  for (i = 0; i < ARRAY_SIZE(stob_types_reg); ++i) {
105  if (rc != 0)
106  break;
108  }
109  if (rc != 0)
111 
112  return M0_RC(rc);
113 }
114 
115 M0_INTERNAL void m0_stob_types_fini(void)
116 {
117  struct m0_stob_types *types = stob_types_get();
118 
120 }
121 
122 M0_INTERNAL struct m0_stob_type *m0_stob_type_by_dom_id(const struct m0_fid *id)
123 {
124  const struct m0_fid_type *fidt = m0_fid_type_get(m0_fid_tget(id));
125 
126  /* XXX cast from const to non-const here */
127  return fidt == NULL ? NULL :
128  container_of(fidt, struct m0_stob_type, st_fidt);
129 }
130 
131 M0_INTERNAL struct m0_stob_type *m0_stob_type_by_name(const char *name)
132 {
133  struct m0_stob_types *types = stob_types_get();
134 
135  return m0_tl_find(types, type, &types->sts_stypes,
137 }
138 
139 M0_INTERNAL uint8_t m0_stob_type_id_by_name(const char *name)
140 {
142 }
143 
144 M0_INTERNAL void m0_stob_type_register(struct m0_stob_type *type)
145 {
146  struct m0_stob_types *types = stob_types_get();
147 
148  m0_mutex_init(&type->st_domains_lock);
149  domains_tlist_init(&type->st_domains);
150  m0_fid_type_register(&type->st_fidt);
151  types_tlink_init_at_tail(type, &types->sts_stypes);
152  type->st_ops->sto_register(type);
153 }
154 
155 M0_INTERNAL void m0_stob_type_deregister(struct m0_stob_type *type)
156 {
157  type->st_ops->sto_deregister(type);
158  types_tlink_del_fini(type);
159  m0_fid_type_unregister(&type->st_fidt);
160  domains_tlist_fini(&type->st_domains);
161  m0_mutex_fini(&type->st_domains_lock);
162 }
163 
164 M0_INTERNAL uint8_t m0_stob_type_id_get(const struct m0_stob_type *type)
165 {
166  return type->st_fidt.ft_id;
167 }
168 
169 M0_INTERNAL const char *m0_stob_type_name_get(struct m0_stob_type *type)
170 {
171  return type->st_fidt.ft_name;
172 }
173 
174 M0_INTERNAL void m0_stob_type__dom_add(struct m0_stob_type *type,
175  struct m0_stob_domain *dom)
176 {
177  m0_mutex_lock(&type->st_domains_lock);
178  domains_tlink_init_at_tail(dom, &type->st_domains);
179  m0_mutex_unlock(&type->st_domains_lock);
180 }
181 
182 M0_INTERNAL void m0_stob_type__dom_del(struct m0_stob_type *type,
183  struct m0_stob_domain *dom)
184 {
185  m0_mutex_lock(&type->st_domains_lock);
186  domains_tlink_del_fini(dom);
187  m0_mutex_unlock(&type->st_domains_lock);
188 }
189 
190 M0_INTERNAL struct m0_stob_domain *
191 m0_stob_type__dom_find(struct m0_stob_type *type, const struct m0_fid *dom_id)
192 {
193  struct m0_stob_domain *dom;
194 
195  m0_mutex_lock(&type->st_domains_lock);
196  dom = m0_tl_find(domains, dom, &type->st_domains,
197  m0_fid_cmp(m0_stob_domain_id_get(dom), dom_id) == 0);
198  m0_mutex_unlock(&type->st_domains_lock);
199 
200  return dom;
201 }
202 
203 M0_INTERNAL struct m0_stob_domain *
205  const char *location)
206 {
207  struct m0_stob_domain *dom;
208 
209  m0_mutex_lock(&type->st_domains_lock);
210  dom = m0_tl_find(domains, dom, &type->st_domains,
212  m0_mutex_unlock(&type->st_domains_lock);
213  return dom;
214 }
215 
216 #undef M0_TRACE_SUBSYSTEM
217 
220 /*
221  * Local variables:
222  * c-indentation-style: "K&R"
223  * c-basic-offset: 8
224  * tab-width: 8
225  * fill-column: 80
226  * scroll-step: 1
227  * End:
228  */
229 /*
230  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
231  */
const struct m0_stob_type m0_stob_perf_type
Definition: perf.c:924
M0_INTERNAL struct m0_stob_type * m0_stob_type_by_dom_id(const struct m0_fid *id)
Definition: type.c:122
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
static int stob_type_copy(const struct m0_stob_type *type, struct m0_stob_type **copy)
Definition: type.c:69
#define NULL
Definition: misc.h:38
struct m0_stob_types stm_types
Definition: module.h:44
M0_INTERNAL const struct m0_fid * m0_stob_domain_id_get(const struct m0_stob_domain *dom)
Definition: domain.c:300
uint8_t ft_id
Definition: fid.h:101
M0_INTERNAL void m0_stob_type_register(struct m0_stob_type *type)
Definition: type.c:144
static struct m0t1fs_fsync_interactions copy
Definition: fsync.c:75
static void stob_types_destroy_list(struct m0_stob_types *types)
Definition: type.c:77
const struct m0_stob_type m0_stob_ad_type
Definition: ad.c:1003
M0_INTERNAL uint8_t m0_stob_type_id_get(const struct m0_stob_type *type)
Definition: type.c:164
M0_INTERNAL uint8_t m0_fid_tget(const struct m0_fid *fid)
Definition: fid.c:133
M0_INTERNAL const struct m0_fid_type * m0_fid_type_get(uint8_t id)
Definition: fid.c:64
static const struct m0_stob_type * stob_types_reg[]
Definition: type.c:50
M0_TL_DEFINE(domains, static, struct m0_stob_domain)
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
M0_INTERNAL int m0_fid_cmp(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:170
M0_INTERNAL struct m0_stob_domain * m0_stob_type__dom_find(struct m0_stob_type *type, const struct m0_fid *dom_id)
Definition: type.c:191
const char * location
Definition: storage.c:50
M0_INTERNAL void m0_stob_type__dom_del(struct m0_stob_type *type, struct m0_stob_domain *dom)
Definition: type.c:182
return M0_RC(rc)
int i
Definition: dir.c:1033
M0_INTERNAL void m0_fid_type_register(const struct m0_fid_type *fidt)
Definition: fid.c:46
M0_INTERNAL void m0_stob_type__dom_add(struct m0_stob_type *type, struct m0_stob_domain *dom)
Definition: type.c:174
const char * name
Definition: trace.c:110
struct m0_tl sts_stypes
Definition: type.h:45
M0_INTERNAL struct m0_stob_domain * m0_stob_type__dom_find_by_location(struct m0_stob_type *type, const char *location)
Definition: type.c:204
#define m0_streq(a, b)
Definition: string.h:34
static struct m0_stob_domain * dom
Definition: storage.c:38
const struct m0_stob_type m0_stob_null_type
Definition: null.c:87
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
M0_INTERNAL uint8_t m0_stob_type_id_by_name(const char *name)
Definition: type.c:139
M0_INTERNAL void m0_stob_types_fini(void)
Definition: type.c:115
M0_INTERNAL void m0_fid_type_unregister(const struct m0_fid_type *fidt)
Definition: fid.c:55
M0_INTERNAL const char * m0_stob_type_name_get(struct m0_stob_type *type)
Definition: type.c:169
uint64_t st_magic
Definition: type.h:66
static struct m0_stob_types * stob_types_get(void)
Definition: type.c:89
M0_INTERNAL int m0_stob_types_init(void)
Definition: type.c:94
M0_INTERNAL struct m0_stob_type * m0_stob_type_by_name(const char *name)
Definition: type.c:131
Definition: fid.h:38
M0_INTERNAL const char * m0_stob_domain_location_get(const struct m0_stob_domain *dom)
Definition: domain.c:306
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_TL_DESCR_DEFINE(domains, "stob domains", static, struct m0_stob_domain, sd_domain_linkage, sd_magic, M0_STOB_DOMAINS_MAGIC, M0_STOB_DOMAINS_HEAD_MAGIC)
struct m0_tlink st_type_linkage
Definition: type.h:65
int type
Definition: dir.c:1031
M0_INTERNAL struct m0_stob_module * m0_stob_module__get(void)
Definition: module.c:49
#define m0_tl_find(name, var, head,...)
Definition: tlist.h:757
void m0_free(void *data)
Definition: memory.c:146
const struct m0_stob_type m0_stob_linux_type
Definition: linux.c:86
M0_INTERNAL void m0_stob_type_deregister(struct m0_stob_type *type)
Definition: type.c:155
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
struct m0_fid_type st_fidt
Definition: type.h:61