Motr  M0
domain.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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_STOB
24 #include "lib/trace.h"
25 
26 #include "stob/domain.h"
27 
28 #include "lib/memory.h" /* m0_alloc */
29 #include "lib/string.h" /* m0_strdup */
30 #include "lib/errno.h" /* EINVAL */
31 
32 #include "stob/type.h" /* m0_stob_type__dom_add */
33 #include "stob/cache.h" /* m0_stob_cache */
34 #include "stob/stob_internal.h" /* m0_stob__cache_evict */
35 #include "stob/stob.h" /* m0_stob_fid_dom_id_get */
36 
43 enum {
52 };
53 
54 static int stob_domain_type(const char *location,
55  struct m0_stob_type **type)
56 {
57  char *colon;
58  char *type_str;
59  int rc;
60 
61  M0_ENTRY("location=%s", location);
62 
63  rc = location == NULL ? -EINVAL : 0;
64  if (location != NULL) {
65  colon = strchr(location, ':');
66  type_str = m0_strdup(location);
67  rc = colon == NULL ? -EINVAL : 0;
68  rc = rc ?: type_str == NULL ? -ENOMEM : 0;
69  if (colon != NULL && type_str != NULL) {
70  type_str[colon - location] = '\0';
71  *type = m0_stob_type_by_name(type_str);
72  }
73  m0_free(type_str);
74  }
75  M0_POST(ergo(rc == 0, *type != NULL));
76  return M0_RC(rc);
77 }
78 
79 static char *stob_domain_location_data(const char *location)
80 {
81  char *location_data = NULL;
82  char *colon;
83 
84  if (location != NULL) {
85  colon = strchr(location, ':');
86  location_data = colon == NULL ? NULL : m0_strdup(colon + 1);
87  }
88  return location_data;
89 }
90 
92  struct m0_stob *stob)
93 {
95 }
96 
98  const char *location_data,
99  uint64_t dom_key,
100  const char *str_cfg_create)
101 {
102  void *cfg_create = NULL;
103  bool cfg_parsed;
104  int rc;
105 
106  rc = type->st_ops->sto_domain_cfg_create_parse(str_cfg_create,
107  &cfg_create);
108  cfg_parsed = rc == 0;
109  rc = rc ?: type->st_ops->sto_domain_create(type, location_data,
110  dom_key, cfg_create);
111  if (cfg_parsed)
112  type->st_ops->sto_domain_cfg_create_free(cfg_create);
113  return M0_RC(rc);
114 }
115 
116 static int stob_domain_init(struct m0_stob_type *type,
117  const char *location_data,
118  const char *str_cfg_init,
119  struct m0_stob_domain **out)
120 {
121  void *cfg_init = NULL;
122  bool cfg_parsed;
123  int rc;
124 
125  M0_ENTRY("location_data=%s str_cfg_init=%s",
126  location_data, str_cfg_init);
127  rc = type->st_ops->sto_domain_cfg_init_parse(str_cfg_init, &cfg_init);
128  cfg_parsed = rc == 0;
129  rc = rc ?: type->st_ops->sto_domain_init(type, location_data,
130  cfg_init, out);
131  if (cfg_parsed)
132  type->st_ops->sto_domain_cfg_init_free(cfg_init);
133  return M0_RC(rc);
134 }
135 
136 static int stob_domain_init_create(const char *location,
137  const char *str_cfg_init,
138  uint64_t dom_key,
139  const char *str_cfg_create,
140  struct m0_stob_domain **out,
141  bool init)
142 {
143  struct m0_stob_type *type = NULL;
144  struct m0_stob_domain *dom;
145  char *location_data;
146  int rc;
147  int rc1;
148 
149  M0_ENTRY();
150 
151  M0_LOG(M0_INFO, "location=%s str_cfg_init=%s dom_key=%" PRIu64 " "
152  "str_cfg_create=%s init=%d",
153  location, str_cfg_init, dom_key, str_cfg_create, !!init);
154 
156  rc = dom != NULL ? -EEXIST : 0;
158  location_data = rc == 0 ? stob_domain_location_data(location) : NULL;
159  rc = rc ?: location_data == NULL ? -ENOMEM : 0;
160  rc = rc ?: init ? 0 : stob_domain_create(type, location_data, dom_key,
161  str_cfg_create);
162  if (rc == 0) {
163  rc = stob_domain_init(type, location_data, str_cfg_init, out);
164  if (!init && rc != 0) {
165  M0_LOG(M0_WARN,
166  "init() after create() failed: rc = %d, "
167  "location = %s", rc, location);
169  if (rc1 != 0) {
171  "destroy() failed: rc = %d, "
172  "location = %s", rc1, location);
173  }
174  /* rc1 is lost here */
175  }
176  }
177  M0_ASSERT(ergo(rc == 0, *out != NULL));
178  if (rc == 0) {
179  dom = *out;
181  dom->sd_location_data = location_data;
182  dom->sd_type = type;
186  NULL);
188  } else {
189  m0_free(location_data);
190  }
192  return M0_RC(rc);
193 }
194 
195 M0_INTERNAL int m0_stob_domain_init(const char *location,
196  const char *str_cfg_init,
197  struct m0_stob_domain **out)
198 {
199  M0_LOG(M0_DEBUG, "location=%s str_cfg_init=%s", location, str_cfg_init);
200  return stob_domain_init_create(location, str_cfg_init, 0,
201  NULL, out, true);
202 }
203 
204 M0_INTERNAL void m0_stob_domain_fini(struct m0_stob_domain *dom)
205 {
206  const struct m0_fid *dom_id = m0_stob_domain_id_get(dom);
207  struct m0_stob_type *type = m0_stob_type_by_dom_id(dom_id);
208 
209  M0_ASSERT(type != NULL);
214  dom->sd_ops->sdo_fini(dom);
215 }
216 
217 M0_INTERNAL int m0_stob_domain_create(const char *location,
218  const char *str_cfg_init,
219  uint64_t dom_key,
220  const char *str_cfg_create,
221  struct m0_stob_domain **out)
222 {
223  return stob_domain_init_create(location, str_cfg_init, dom_key,
224  str_cfg_create, out, false);
225 }
226 
227 M0_INTERNAL int m0_stob_domain_destroy(struct m0_stob_domain *dom)
228 {
229  const char *location_const = m0_stob_domain_location_get(dom);
230  char *location;
231  int rc;
232 
233  M0_ENTRY("location=%s", location_const);
234  location = location_const == NULL ? NULL : m0_strdup(location_const);
235  rc = location == NULL ? -ENOMEM : 0;
238  m0_free(location);
239  return M0_RC(rc);
240 }
241 
242 M0_INTERNAL int m0_stob_domain_destroy_location(const char *location)
243 {
244  struct m0_stob_type *type;
245  char *location_data;
246  int rc;
247 
248  M0_ENTRY("location=%s", location);
249 
250  rc = location == NULL ? -EINVAL : 0;
252  location_data = rc == 0 ? stob_domain_location_data(location) : NULL;
253  rc = rc ?: location_data == NULL ? -ENOMEM : 0;
254  rc = rc ?: type->st_ops->sto_domain_destroy(type, location_data);
255  m0_free(location_data);
256 
257  if (!M0_IN(rc, (0, -ENOENT)))
258  return M0_ERR(rc);
259  return M0_RC(rc);
260 }
261 
262 M0_INTERNAL int m0_stob_domain_create_or_init(const char *location,
263  const char *str_cfg_init,
264  uint64_t dom_key,
265  const char *str_cfg_create,
266  struct m0_stob_domain **out)
267 {
268  int rc;
269 
270  rc = m0_stob_domain_init(location, str_cfg_init, out);
271  if (rc != 0)
272  rc = m0_stob_domain_create(location, str_cfg_init,
273  dom_key, str_cfg_create, out);
274  return M0_RC(rc);
275 }
276 
277 M0_INTERNAL struct m0_stob_domain *
278 m0_stob_domain_find(const struct m0_fid *dom_id)
279 {
280  return m0_stob_type__dom_find(m0_stob_type_by_dom_id(dom_id), dom_id);
281 }
282 
283 M0_INTERNAL struct m0_stob_domain *
285 {
286  struct m0_stob_type *type;
287  int rc = stob_domain_type(location, &type);
288 
289  return rc != 0 ? NULL :
291 }
292 
293 M0_INTERNAL struct m0_stob_domain *
295 {
296  return m0_stob_domain_find(&stob_id->si_domain_fid);
297 }
298 
299 M0_INTERNAL const struct m0_fid *
301 {
302  return &dom->sd_id;
303 }
304 
305 M0_INTERNAL const char *
307 {
308  return dom->sd_location;
309 }
310 
311 M0_INTERNAL void m0_stob_domain__id_set(struct m0_stob_domain *dom,
312  struct m0_fid *dom_id)
313 {
314  dom->sd_id = *dom_id;
315 }
316 
317 M0_INTERNAL uint8_t m0_stob_domain__type_id(const struct m0_fid *dom_id)
318 {
319  return m0_fid_tget(dom_id);
320 }
321 
322 M0_INTERNAL uint64_t m0_stob_domain__dom_key(const struct m0_fid *dom_id)
323 {
324  return dom_id->f_key;
325 }
326 
327 M0_INTERNAL void m0_stob_domain__dom_id_make(struct m0_fid *dom_id,
328  uint8_t type_id,
329  uint64_t dom_container,
330  uint64_t dom_key)
331 {
332  m0_fid_tset(dom_id, type_id, dom_container, dom_key);
333 }
334 
336 {
337  const struct m0_fid *dom_id = m0_stob_domain_id_get(dom);
338  struct m0_stob_type *type = dom->sd_type;
339 
340  return _0C(type != NULL) &&
342 }
343 
344 M0_INTERNAL bool m0_stob_domain__dom_key_is_valid(uint64_t dom_key)
345 {
346  return (dom_key & 0xFFULL << 56) == 0;
347 }
348 
349 M0_INTERNAL bool m0_stob_domain_is_of_type(const struct m0_stob_domain *dom,
350  const struct m0_stob_type *dt)
351 {
353 }
356 /*
357  * Local variables:
358  * c-indentation-style: "K&R"
359  * c-basic-offset: 8
360  * tab-width: 8
361  * fill-column: 80
362  * scroll-step: 1
363  * End:
364  */
365 /*
366  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
367  */
static int stob_domain_init(struct m0_stob_type *type, const char *location_data, const char *str_cfg_init, struct m0_stob_domain **out)
Definition: domain.c:116
Definition: beck.c:235
M0_INTERNAL struct m0_stob_type * m0_stob_type_by_dom_id(const struct m0_fid *id)
Definition: type.c:122
static int stob_domain_init_create(const char *location, const char *str_cfg_init, uint64_t dom_key, const char *str_cfg_create, struct m0_stob_domain **out, bool init)
Definition: domain.c:136
M0_INTERNAL uint64_t m0_stob_domain__dom_key(const struct m0_fid *dom_id)
Definition: domain.c:322
#define m0_strdup(s)
Definition: string.h:43
M0_INTERNAL struct m0_stob_domain * m0_stob_domain_find(const struct m0_fid *dom_id)
Definition: domain.c:278
M0_INTERNAL void m0_stob_domain__dom_id_make(struct m0_fid *dom_id, uint8_t type_id, uint64_t dom_container, uint64_t dom_key)
Definition: domain.c:327
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_fid_tset(struct m0_fid *fid, uint8_t tid, uint64_t container, uint64_t key)
Definition: fid.c:126
struct m0_fid si_domain_fid
Definition: stob.h:103
#define ergo(a, b)
Definition: misc.h:293
static void stob_domain_cache_evict_cb(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: domain.c:91
#define M0_LOG(level,...)
Definition: trace.h:167
M0_INTERNAL const struct m0_fid * m0_stob_domain_id_get(const struct m0_stob_domain *dom)
Definition: domain.c:300
M0_INTERNAL int m0_stob_domain_destroy(struct m0_stob_domain *dom)
Definition: domain.c:227
M0_INTERNAL bool m0_stob_domain_is_of_type(const struct m0_stob_domain *dom, const struct m0_stob_type *dt)
Definition: domain.c:349
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
void(* sdo_fini)(struct m0_stob_domain *dom)
Definition: domain.h:111
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
char * sd_location
Definition: domain.h:97
M0_INTERNAL int m0_stob_cache_init(struct m0_stob_cache *cache, uint64_t idle_size, m0_stob_cache_eviction_cb_t eviction_cb)
Definition: cache.c:42
const char * location
Definition: storage.c:50
M0_INTERNAL void m0_stob__cache_evict(struct m0_stob *stob)
Definition: stob.c:323
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)
#define M0_ASSERT_EX(cond)
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL struct m0_stob_domain * m0_stob_domain_find_by_location(const char *location)
Definition: domain.c:284
M0_INTERNAL bool m0_stob_domain__dom_key_is_valid(uint64_t dom_key)
Definition: domain.c:344
M0_INTERNAL int m0_stob_domain_create(const char *location, const char *str_cfg_init, uint64_t dom_key, const char *str_cfg_create, struct m0_stob_domain **out)
Definition: domain.c:217
#define PRIu64
Definition: types.h:58
static int stob_domain_create(struct m0_stob_type *type, const char *location_data, uint64_t dom_key, const char *str_cfg_create)
Definition: domain.c:97
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL void m0_stob_type__dom_add(struct m0_stob_type *type, struct m0_stob_domain *dom)
Definition: type.c:174
Definition: trace.h:482
Definition: stob.h:163
static struct m0_stob * stob
Definition: storage.c:39
#define M0_ASSERT(cond)
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
struct m0_stob_cache sd_cache
Definition: domain.h:103
static struct m0_stob_domain * dom
Definition: storage.c:38
M0_INTERNAL void m0_stob_cache_fini(struct m0_stob_cache *cache)
Definition: cache.c:61
#define M0_POST(cond)
M0_INTERNAL struct m0_stob_domain * m0_stob_domain_find_by_stob_id(const struct m0_stob_id *stob_id)
Definition: domain.c:294
char * sd_location_data
Definition: domain.h:98
M0_INTERNAL void m0_stob_domain_fini(struct m0_stob_domain *dom)
Definition: domain.c:204
int init(struct workload *w)
M0_INTERNAL int m0_stob_domain_destroy_location(const char *location)
Definition: domain.c:242
struct m0_fid sd_id
Definition: domain.h:96
struct m0_stob_type * sd_type
Definition: domain.h:95
M0_INTERNAL struct m0_stob_type * m0_stob_type_by_name(const char *name)
Definition: type.c:131
M0_INTERNAL int m0_stob_domain_init(const char *location, const char *str_cfg_init, struct m0_stob_domain **out)
Definition: domain.c:195
Definition: fid.h:38
M0_INTERNAL int m0_stob_domain_create_or_init(const char *location, const char *str_cfg_init, uint64_t dom_key, const char *str_cfg_create, struct m0_stob_domain **out)
Definition: domain.c:262
uint64_t f_key
Definition: fid.h:40
M0_INTERNAL const char * m0_stob_domain_location_get(const struct m0_stob_domain *dom)
Definition: domain.c:306
M0_INTERNAL void m0_stob_domain__id_set(struct m0_stob_domain *dom, struct m0_fid *dom_id)
Definition: domain.c:311
M0_INTERNAL bool m0_stob_domain__invariant(struct m0_stob_domain *dom)
Definition: domain.c:335
#define _0C(exp)
Definition: assert.h:311
static char * stob_domain_location_data(const char *location)
Definition: domain.c:79
const struct m0_stob_domain_ops * sd_ops
Definition: domain.h:94
#define out(...)
Definition: gen.c:41
int type
Definition: dir.c:1031
static int stob_domain_type(const char *location, struct m0_stob_type **type)
Definition: domain.c:54
void m0_free(void *data)
Definition: memory.c:146
M0_INTERNAL uint8_t m0_stob_domain__type_id(const struct m0_fid *dom_id)
Definition: domain.c:317
int32_t rc
Definition: trigger_fop.h:47
Definition: trace.h:478