Motr  M0
stob.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_STOB
23 #include "lib/trace.h"
24 
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "lib/arith.h" /* M0_3WAY */
27 #include "lib/errno.h"
28 #include "lib/assert.h"
29 #include "lib/memory.h"
30 #include "lib/string.h" /* m0_strdup */
31 #include "lib/types.h" /* PRIu64 */
32 #include "motr/magic.h"
33 
34 #include "stob/type.h"
35 #include "stob/domain.h"
36 #include "stob/stob.h"
37 #include "stob/stob_internal.h"
38 #include "stob/stob_xc.h"
39 
45 M0_INTERNAL struct m0_stob_cache *
47 {
48  return &dom->sd_cache;
49 }
50 
51 M0_INTERNAL struct m0_stob *
53  const struct m0_fid *stob_fid)
54 {
55  return dom->sd_ops->sdo_stob_alloc(dom, stob_fid);
56 }
57 
58 M0_INTERNAL void m0_stob_domain__stob_free(struct m0_stob_domain *dom,
59  struct m0_stob *stob)
60 {
62 }
63 
65 M0_INTERNAL int m0_stob_find_by_key(struct m0_stob_domain *dom,
66  const struct m0_fid *stob_fid,
67  struct m0_stob **out)
68 {
70  struct m0_stob *stob;
71 
73  stob = m0_stob_cache_lookup(cache, stob_fid);
74  if (stob != NULL) {
76  } else {
77  stob = m0_stob_domain__stob_alloc(dom, stob_fid);
78  if (stob != NULL) {
79  stob->so_domain = dom;
80  m0_stob__id_set(stob, stob_fid);
82  stob->so_ref = 1;
84  }
85  }
87 
88  *out = stob;
89  return stob == NULL ? M0_ERR(-ENOMEM) : M0_RC(0);
90 }
91 
92 M0_INTERNAL int m0_stob_find(const struct m0_stob_id *id, struct m0_stob **out)
93 {
95 
96  *out = NULL; /* XXX Workaround for gcc warning. */
97 
98  return dom == NULL ? M0_ERR(-EINVAL) :
99  m0_stob_find_by_key(dom, &id->si_fid, out);
100 }
101 
102 M0_INTERNAL int m0_stob_lookup_by_key(struct m0_stob_domain *dom,
103  const struct m0_fid *stob_fid,
104  struct m0_stob **out)
105 {
107  struct m0_stob *stob;
108 
110  stob = m0_stob_cache_lookup(cache, stob_fid);
111  if (stob != NULL)
114 
115  *out = stob;
116  return stob == NULL ? -ENOENT : 0;
117 }
118 
119 M0_INTERNAL int m0_stob_lookup(const struct m0_stob_id *id,
120  struct m0_stob **out)
121 {
123 
124  return dom == NULL ? -EINVAL :
125  m0_stob_lookup_by_key(dom, &id->si_fid, out);
126 }
127 
128 M0_INTERNAL int m0_stob_locate(struct m0_stob *stob)
129 {
131  int rc;
132 
133  M0_ENTRY();
134  M0_PRE(stob->so_ref > 0);
136 
138  if (rc == 0) {
142  } else if (rc == -ENOENT)
144 
145  return M0_RC(M0_IN(rc, (0, -ENOENT)) ? 0 : rc);
146 }
147 
148 M0_INTERNAL void m0_stob_create_credit(struct m0_stob_domain *dom,
149  struct m0_be_tx_credit *accum)
150 {
152 }
153 
154 M0_INTERNAL int m0_stob_create(struct m0_stob *stob,
155  struct m0_dtx *dtx,
156  const char *str_cfg)
157 {
159  const struct m0_stob_domain_ops *dom_ops = dom->sd_ops;
160  void *cfg;
161  int rc;
162 
163  M0_ENTRY("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
166  M0_PRE(stob->so_ref > 0);
167 
168  rc = dom_ops->sdo_stob_cfg_parse(str_cfg, &cfg);
169  if (rc == 0) {
170  rc = m0_stob_state_get(stob) == CSS_EXISTS ? -EEXIST :
171  dom_ops->sdo_stob_create(stob, dom, dtx,
172  m0_stob_fid_get(stob), cfg);
173  dom_ops->sdo_stob_cfg_free(cfg);
174  if (rc == 0) {
177  }
178  }
180  rc == 0 ? CSS_EXISTS : m0_stob_state_get(stob));
181  if (rc == -ENOENT)
182  stob->so_ops->sop_destroy(stob, dtx);
183 
184  return M0_RC(rc);
185 }
186 
187 M0_INTERNAL void m0_stob_destroy_credit(struct m0_stob *stob,
188  struct m0_be_tx_credit *accum)
189 {
191 }
192 
193 M0_INTERNAL void m0_stob_delete_mark(struct m0_stob *stob)
194 {
196 
198 }
199 
200 M0_INTERNAL int m0_stob_destroy(struct m0_stob *stob, struct m0_dtx *dtx)
201 {
202  int rc;
203 
204  M0_ENTRY("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
206  /*
207  * ioservice ensures stob existence.
208  * @see cob_ops_fom_tick().
209  */
211  M0_ASSERT_INFO(stob->so_ref == 1, "so_ref=%"PRIu64, stob->so_ref);
212 
213  rc = stob->so_ops->sop_destroy(stob, dtx);
214  if (rc == 0) {
218  m0_stob_put(stob);
219  }
220  return M0_RC(rc);
221 }
222 
223 M0_INTERNAL int m0_stob_punch_credit(struct m0_stob *stob,
224  struct m0_indexvec *want,
225  struct m0_indexvec *got,
226  struct m0_be_tx_credit *accum)
227 {
229  return stob->so_ops->sop_punch_credit(stob, want, got, accum);
230 }
231 
232 M0_INTERNAL int m0_stob_punch(struct m0_stob *stob,
233  struct m0_indexvec *range,
234  struct m0_dtx *dtx)
235 {
236  int rc;
237 
238  M0_ENTRY("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
241  rc = stob->so_ops->sop_punch(stob, range, dtx);
242  return M0_RC(rc);
243 }
244 
245 M0_INTERNAL uint64_t m0_stob_dom_id_get(struct m0_stob *stob)
246 {
248 }
249 
250 M0_INTERNAL const struct m0_stob_id *m0_stob_id_get(struct m0_stob *stob)
251 {
252  return &stob->so_id;
253 }
254 
255 M0_INTERNAL const struct m0_fid *m0_stob_fid_get(struct m0_stob *stob)
256 {
257  return &stob->so_id.si_fid;
258 }
259 
260 M0_INTERNAL uint64_t m0_stob_id_dom_id_get(const struct m0_stob_id *stob_id)
261 {
262  return m0_fid_tget(&stob_id->si_domain_fid);
263 }
264 
265 M0_INTERNAL enum m0_stob_state m0_stob_state_get(struct m0_stob *stob)
266 {
267  return stob->so_state;
268 }
269 
270 M0_INTERNAL uint32_t m0_stob_block_shift(struct m0_stob *stob)
271 {
272  return stob->so_ops->sop_block_shift(stob);
273 }
274 
275 M0_INTERNAL void m0_stob_get(struct m0_stob *stob)
276 {
277  struct m0_stob_cache *cache;
278 
280 
282  M0_ENTRY("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
284  M0_ASSERT(stob->so_ref > 0);
286  M0_LEAVE("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
289 }
290 
291 M0_INTERNAL void m0_stob_put(struct m0_stob *stob)
292 {
293  struct m0_stob_cache *cache;
294 
296 
298  M0_ENTRY("stob=%p so_id="STOB_ID_F" so_ref=%"PRIu64,
301  if (stob->so_ref == 0)
304 
305  M0_LOG(M0_DEBUG, "stob %p, fid="FID_F" so_ref %" PRIu64 ", released ref, "
306  "chan_waiters %"PRIu32, stob, FID_P(&stob->so_id.si_fid),
309  M0_ASSERT(stob->so_ref >= 1);
310  if (stob->so_ref == 1)
312  }
313 }
314 
315 M0_INTERNAL void m0_stob__id_set(struct m0_stob *stob,
316  const struct m0_fid *stob_fid)
317 {
318  M0_LOG(M0_DEBUG, FID_F, FID_P(stob_fid));
320  stob->so_id.si_fid = *stob_fid;
321 }
322 
323 M0_INTERNAL void m0_stob__cache_evict(struct m0_stob *stob)
324 {
326 
330 }
331 
332 M0_INTERNAL void m0_stob__state_set(struct m0_stob *stob,
333  enum m0_stob_state state)
334 {
335  stob->so_state = state;
336 }
337 
338 M0_INTERNAL struct m0_stob_domain *m0_stob_dom_get(struct m0_stob *stob)
339 {
340  return stob->so_domain;
341 }
342 
343 M0_INTERNAL void m0_stob_id_make(uint64_t container,
344  uint64_t key,
345  const struct m0_fid *dom_id,
346  struct m0_stob_id *stob_id)
347 {
348  stob_id->si_domain_fid = *dom_id;
349  m0_fid_tset(&stob_id->si_fid, m0_fid_tget(dom_id), container, key);
350 }
351 
352 M0_INTERNAL bool m0_stob_id_eq(const struct m0_stob_id *stob_id0,
353  const struct m0_stob_id *stob_id1)
354 {
355  return m0_fid_eq(&stob_id0->si_domain_fid, &stob_id1->si_domain_fid) &&
356  m0_fid_eq(&stob_id0->si_fid, &stob_id1->si_fid);
357 
358 }
359 
360 M0_INTERNAL int m0_stob_fd(struct m0_stob *stob)
361 {
362  M0_PRE(stob->so_ops != NULL && stob->so_ops->sop_fd != NULL);
363 
364  return stob->so_ops->sop_fd(stob);
365 }
366 
367 M0_INTERNAL int m0_stob_mod_init(void)
368 {
369  m0_xc_stob_stob_init();
370  return 0;
371 }
372 
373 M0_INTERNAL void m0_stob_mod_fini(void)
374 {
375  m0_xc_stob_stob_fini();
376 }
377 
379 #undef M0_TRACE_SUBSYSTEM
380 
381 /*
382  * Local variables:
383  * c-indentation-style: "K&R"
384  * c-basic-offset: 8
385  * tab-width: 8
386  * fill-column: 80
387  * scroll-step: 1
388  * End:
389  */
Definition: beck.c:235
uint64_t id
Definition: cob.h:2380
M0_INTERNAL struct m0_stob_domain * m0_stob_dom_get(struct m0_stob *stob)
Definition: stob.c:338
M0_INTERNAL void m0_stob_domain__stob_free(struct m0_stob_domain *dom, struct m0_stob *stob)
Definition: stob.c:58
#define M0_PRE(cond)
Definition: dtm.h:554
int(* sop_punch)(struct m0_stob *stob, struct m0_indexvec *range, struct m0_dtx *dtx)
Definition: stob.h:199
void(* sop_destroy_credit)(struct m0_stob *stob, struct m0_be_tx_credit *accum)
Definition: stob.h:189
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_stob_cache_add(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: cache.c:120
M0_INTERNAL int m0_stob_locate(struct m0_stob *stob)
Definition: stob.c:128
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
const struct m0_stob_ops * so_ops
Definition: stob.h:164
M0_INTERNAL int m0_stob_mod_init(void)
Definition: stob.c:367
M0_INTERNAL bool m0_chan_has_waiters(struct m0_chan *chan)
Definition: chan.c:185
#define M0_LOG(level,...)
Definition: trace.h:167
M0_INTERNAL int m0_stob_find_by_key(struct m0_stob_domain *dom, const struct m0_fid *stob_fid, struct m0_stob **out)
Definition: stob.c:65
M0_LEAVE()
struct m0_container container
void(* sdo_stob_free)(struct m0_stob_domain *dom, struct m0_stob *stob)
Definition: domain.h:123
int(* sop_destroy)(struct m0_stob *stob, struct m0_dtx *dtx)
Definition: stob.h:192
M0_INTERNAL uint8_t m0_fid_tget(const struct m0_fid *fid)
Definition: fid.c:133
M0_INTERNAL const struct m0_fid * m0_stob_fid_get(struct m0_stob *stob)
Definition: stob.c:255
struct m0_mutex so_ref_mutex
Definition: stob.h:172
M0_INTERNAL void m0_stob__id_set(struct m0_stob *stob, const struct m0_fid *stob_fid)
Definition: stob.c:315
M0_INTERNAL void m0_stob__cache_evict(struct m0_stob *stob)
Definition: stob.c:323
struct m0_stob_domain * so_domain
Definition: stob.h:165
return M0_RC(rc)
M0_INTERNAL uint32_t m0_stob_block_shift(struct m0_stob *stob)
Definition: stob.c:270
void(* sdo_stob_create_credit)(struct m0_stob_domain *dom, struct m0_be_tx_credit *accum)
Definition: domain.h:138
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL int m0_stob_lookup(const struct m0_stob_id *id, struct m0_stob **out)
Definition: stob.c:119
void(* sdo_stob_cfg_free)(void *cfg_create)
Definition: domain.h:132
uint32_t(* sop_block_shift)(struct m0_stob *stob)
Definition: stob.h:204
#define PRIu64
Definition: types.h:58
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL struct m0_stob_cache * m0_stob_domain__cache(struct m0_stob_domain *dom)
Definition: stob.c:46
M0_INTERNAL const struct m0_stob_id * m0_stob_id_get(struct m0_stob *stob)
Definition: stob.c:250
Definition: stob.h:163
enum m0_stob_state so_state
Definition: stob.h:167
static struct m0_stob * stob
Definition: storage.c:39
M0_INTERNAL void m0_chan_init(struct m0_chan *chan, struct m0_mutex *ch_guard)
Definition: chan.c:96
#define M0_ASSERT(cond)
M0_INTERNAL void m0_stob_mod_fini(void)
Definition: stob.c:373
#define STOB_ID_P(si)
Definition: stob.h:109
struct m0_stob_cache sd_cache
Definition: domain.h:103
M0_INTERNAL void m0_stob_destroy_credit(struct m0_stob *stob, struct m0_be_tx_credit *accum)
Definition: stob.c:187
M0_INTERNAL void m0_stob_cache_lock(struct m0_stob_cache *cache)
Definition: cache.c:185
M0_INTERNAL uint64_t m0_stob_dom_id_get(struct m0_stob *stob)
Definition: stob.c:245
int(* sop_punch_credit)(struct m0_stob *stob, struct m0_indexvec *want, struct m0_indexvec *got, struct m0_be_tx_credit *accum)
Definition: stob.h:194
static struct m0_stob_domain * dom
Definition: storage.c:38
M0_INTERNAL void m0_stob_id_make(uint64_t container, uint64_t key, const struct m0_fid *dom_id, struct m0_stob_id *stob_id)
Definition: stob.c:343
M0_INTERNAL int m0_stob_lookup_by_key(struct m0_stob_domain *dom, const struct m0_fid *stob_fid, struct m0_stob **out)
Definition: stob.c:102
struct m0_fid si_fid
Definition: stob.h:105
struct m0_chan so_ref_chan
Definition: stob.h:170
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
int(* sdo_stob_create)(struct m0_stob *stob, struct m0_stob_domain *dom, struct m0_dtx *dtx, const struct m0_fid *stob_fid, void *cfg)
Definition: domain.h:141
M0_INTERNAL void m0_stob_cache_unlock(struct m0_stob_cache *cache)
Definition: cache.c:190
uint64_t so_ref
Definition: stob.h:168
#define FID_P(f)
Definition: fid.h:77
M0_INTERNAL void m0_chan_signal_lock(struct m0_chan *chan)
Definition: chan.c:165
M0_INTERNAL struct m0_stob_domain * m0_stob_domain_find_by_stob_id(const struct m0_stob_id *stob_id)
Definition: domain.c:294
M0_INTERNAL void m0_stob__state_set(struct m0_stob *stob, enum m0_stob_state state)
Definition: stob.c:332
struct m0_stob *(* sdo_stob_alloc)(struct m0_stob_domain *dom, const struct m0_fid *stob_fid)
Definition: domain.h:116
M0_INTERNAL int m0_stob_create(struct m0_stob *stob, struct m0_dtx *dtx, const char *str_cfg)
Definition: stob.c:154
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
M0_INTERNAL int m0_stob_punch_credit(struct m0_stob *stob, struct m0_indexvec *want, struct m0_indexvec *got, struct m0_be_tx_credit *accum)
Definition: stob.c:223
struct m0_fid sd_id
Definition: domain.h:96
#define PRIu32
Definition: types.h:66
M0_INTERNAL int m0_stob_destroy(struct m0_stob *stob, struct m0_dtx *dtx)
Definition: stob.c:200
M0_INTERNAL enum m0_stob_state m0_stob_state_get(struct m0_stob *stob)
Definition: stob.c:265
void(* sop_fini)(struct m0_stob *stob)
Definition: stob.h:187
Definition: stob.h:91
uint32_t ch_waiters
Definition: chan.h:236
#define M0_CNT_INC(cnt)
Definition: arith.h:226
Definition: fid.h:38
M0_INTERNAL struct m0_stob * m0_stob_domain__stob_alloc(struct m0_stob_domain *dom, const struct m0_fid *stob_fid)
Definition: stob.c:52
M0_INTERNAL int m0_stob_fd(struct m0_stob *stob)
Definition: stob.c:360
m0_stob_state
Definition: stob.h:81
M0_INTERNAL uint64_t m0_stob_id_dom_id_get(const struct m0_stob_id *stob_id)
Definition: stob.c:260
int(* sdo_stob_cfg_parse)(const char *str_cfg_create, void **cfg_create)
Definition: domain.h:126
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_INTERNAL bool m0_stob_id_eq(const struct m0_stob_id *stob_id0, const struct m0_stob_id *stob_id1)
Definition: stob.c:352
int(* sdo_stob_init)(struct m0_stob *stob, struct m0_stob_domain *dom, const struct m0_fid *stob_fid)
Definition: domain.h:134
M0_INTERNAL int m0_stob_find(const struct m0_stob_id *id, struct m0_stob **out)
Definition: stob.c:92
#define M0_CNT_DEC(cnt)
Definition: arith.h:219
const struct m0_stob_domain_ops * sd_ops
Definition: domain.h:94
#define M0_ASSERT_INFO(cond, fmt,...)
int(* sop_fd)(struct m0_stob *stob)
Definition: stob.h:206
#define STOB_ID_F
Definition: stob.h:108
M0_INTERNAL void m0_stob_delete_mark(struct m0_stob *stob)
Definition: stob.c:193
#define out(...)
Definition: gen.c:41
M0_INTERNAL int m0_stob_punch(struct m0_stob *stob, struct m0_indexvec *range, struct m0_dtx *dtx)
Definition: stob.c:232
M0_INTERNAL void m0_stob_create_credit(struct m0_stob_domain *dom, struct m0_be_tx_credit *accum)
Definition: stob.c:148
M0_INTERNAL struct m0_stob * m0_stob_cache_lookup(struct m0_stob_cache *cache, const struct m0_fid *stob_fid)
Definition: cache.c:137
M0_INTERNAL void m0_chan_fini_lock(struct m0_chan *chan)
Definition: chan.c:112
M0_INTERNAL void m0_stob_get(struct m0_stob *stob)
Definition: stob.c:275
int32_t rc
Definition: trigger_fop.h:47
struct m0_stob_id so_id
Definition: stob.h:166
M0_INTERNAL void m0_stob_put(struct m0_stob *stob)
Definition: stob.c:291
#define FID_F
Definition: fid.h:75
M0_INTERNAL void m0_stob_cache_idle(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: cache.c:129
Definition: idx_mock.c:47