Motr  M0
seg0.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 <stdio.h>
24 #include "be/alloc.h"
25 #include "be/domain.h"
26 #include "be/op.h"
27 #include "be/seg0.h"
28 #include "be/seg.h"
29 
30 #include "lib/mutex.h"
31 #include "lib/buf.h"
32 
33 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_BE
34 #include "lib/trace.h"
35 
36 static bool be_0type_invariant(const struct m0_be_0type *zt)
37 {
38  return zt->b0_name != NULL &&
39  zt->b0_init != NULL &&
40  zt->b0_fini != NULL;
41 }
42 
43 static bool dom_is_locked(const struct m0_be_domain *dom)
44 {
46 }
47 
48 static void keyname_format(const struct m0_be_0type *zt, const char *suffix,
49  char *keyname, size_t keyname_len)
50 {
51  snprintf(keyname, keyname_len, "%s%s", zt->b0_name, suffix);
52 }
53 
55 {
56  struct m0_be_seg *seg;
57 
60  "BE domain should be mkfs'ed first. "
61  "See m0_be_domain_cfg::bc_mkfs_mode.");
62  return seg;
63 }
64 
66 {
68 
70 }
71 
73 {
75 
77 }
78 
80  struct m0_be_domain *dom,
81  struct m0_be_tx *tx,
82  const char *suffix,
83  const struct m0_buf *data)
84 {
85  struct m0_be_seg *seg;
86  struct m0_buf *opt;
87  char keyname[256] = {};
88  int rc;
89 
93  // add PRE 0type registered
94 
96  keyname_format(zt, suffix, keyname, ARRAY_SIZE(keyname));
97 
98  M0_BE_ALLOC_PTR_SYNC(opt, seg, tx);
99  opt->b_nob = data->b_nob;
100  M0_BE_ALLOC_BUF_SYNC(opt, seg, tx);
101  memcpy(opt->b_addr, data->b_addr, data->b_nob);
102  M0_BE_TX_CAPTURE_PTR(seg, tx, opt);
103  M0_BE_TX_CAPTURE_BUF(seg, tx, opt);
104  rc = m0_be_seg_dict_insert(seg, tx, keyname, (void*)opt);
105  if (rc != 0)
106  return M0_RC(rc);
107 
108  /* XXX error handling is missing here: what if b0_init() fails? */
109  return zt->b0_init(dom, suffix, opt);
110 }
111 
113  struct m0_be_domain *dom,
114  struct m0_be_tx *tx,
115  const char *suffix)
116 {
117  struct m0_be_seg *seg;
118  struct m0_buf *opt;
119  char keyname[256] = {};
120  int rc;
121 
125 
127  keyname_format(zt, suffix, keyname, ARRAY_SIZE(keyname));
128 
129  rc = m0_be_seg_dict_lookup(seg, keyname, (void**)&opt);
130  if (rc != 0)
131  return M0_RC(rc); /* keyname is not found -- nothing to delete */
132 
133  zt->b0_fini(dom, suffix, opt);
134  M0_BE_FREE_PTR_SYNC(opt->b_addr, seg, tx);
135  M0_BE_FREE_PTR_SYNC(opt, seg, tx);
136  return m0_be_seg_dict_delete(seg, tx, keyname);
137 }
138 
140  const struct m0_be_0type *zt,
141  const char *suffix,
142  const struct m0_buf *data,
143  struct m0_be_tx_credit *credit)
144 {
145  struct m0_be_seg *seg = be_0type_seg0_get(dom);
146  char keyname[256] = {};
147 
148  keyname_format(zt, suffix, keyname, ARRAY_SIZE(keyname));
149  M0_BE_ALLOC_CREDIT_PTR(data, seg, credit);
150  M0_BE_ALLOC_CREDIT_BUF(data, seg, credit);
153  m0_be_seg_dict_insert_credit(seg, keyname, credit);
154 }
155 
157  const struct m0_be_0type *zt,
158  const char *suffix,
159  struct m0_be_tx_credit *credit)
160 {
161  struct m0_be_seg *seg = be_0type_seg0_get(dom);
162  char keyname[256] = {};
163 
164  keyname_format(zt, suffix, keyname, ARRAY_SIZE(keyname));
165  /* to free data */
167  0, 0, credit);
168  /* to free m0_buf pointing to data */
170  0, 0, credit);
171  m0_be_seg_dict_delete_credit(seg, keyname, credit);
172 }
173 
174 #undef M0_TRACE_SUBSYSTEM
175 
176 /*
177  * Local variables:
178  * c-indentation-style: "K&R"
179  * c-basic-offset: 8
180  * tab-width: 8
181  * fill-column: 80
182  * scroll-step: 1
183  * End:
184  */
185 /*
186  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
187  */
#define M0_BE_TX_CREDIT_PTR(ptr)
Definition: tx_credit.h:98
#define M0_BE_ALLOC_CREDIT_PTR(ptr, seg, accum)
Definition: alloc.h:355
#define M0_PRE(cond)
#define M0_BE_ALLOC_PTR_SYNC(ptr, seg, tx)
Definition: alloc.h:339
M0_INTERNAL void m0_be_domain__0type_unregister(struct m0_be_domain *dom, struct m0_be_0type *type)
Definition: domain.c:655
M0_INTERNAL int m0_be_seg_dict_insert(struct m0_be_seg *seg, struct m0_be_tx *tx, const char *name, void *value)
Definition: seg_dict.c:255
void m0_be_0type_add_credit(struct m0_be_domain *dom, const struct m0_be_0type *zt, const char *suffix, const struct m0_buf *data, struct m0_be_tx_credit *credit)
Definition: seg0.c:139
int(* b0_init)(struct m0_be_domain *dom, const char *suffix, const struct m0_buf *data)
Definition: seg0.h:62
#define NULL
Definition: misc.h:38
const char * b0_name
Definition: seg0.h:44
M0_INTERNAL bool m0_be_domain_is_locked(const struct m0_be_domain *dom)
Definition: domain.c:641
void * b_addr
Definition: buf.h:39
void m0_be_0type_del_credit(struct m0_be_domain *dom, const struct m0_be_0type *zt, const char *suffix, struct m0_be_tx_credit *credit)
Definition: seg0.c:156
struct m0_bufvec data
Definition: di.c:40
void m0_be_0type_unregister(struct m0_be_domain *dom, struct m0_be_0type *zt)
Definition: seg0.c:72
#define M0_BE_ALLOC_BUF_SYNC(buf, seg, tx)
Definition: alloc.h:352
M0_INTERNAL struct m0_be_seg * m0_be_domain_seg0_get(struct m0_be_domain *dom)
Definition: domain.c:466
M0_INTERNAL void m0_be_domain__0type_register(struct m0_be_domain *dom, struct m0_be_0type *type)
Definition: domain.c:647
#define M0_BE_TX_CAPTURE_PTR(seg, tx, ptr)
Definition: tx.h:505
static bool dom_is_locked(const struct m0_be_domain *dom)
Definition: seg0.c:43
return M0_RC(rc)
Definition: buf.h:37
M0_INTERNAL int m0_be_seg_dict_delete(struct m0_be_seg *seg, struct m0_be_tx *tx, const char *name)
Definition: seg_dict.c:316
#define M0_BE_TX_CAPTURE_BUF(seg, tx, buf)
Definition: tx.h:509
m0_bcount_t b_nob
Definition: buf.h:38
void m0_be_0type_register(struct m0_be_domain *dom, struct m0_be_0type *zt)
Definition: seg0.c:65
static bool be_0type_invariant(const struct m0_be_0type *zt)
Definition: seg0.c:36
M0_INTERNAL struct m0_be_allocator * m0_be_seg_allocator(struct m0_be_seg *seg)
Definition: stubs.c:113
M0_INTERNAL void m0_be_tx_credit_add(struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:44
static struct m0_stob_domain * dom
Definition: storage.c:38
M0_INTERNAL bool m0_be_tx__is_exclusive(const struct m0_be_tx *tx)
Definition: tx.c:649
#define M0_BE_FREE_PTR_SYNC(ptr, seg, tx)
Definition: alloc.h:345
Definition: seg.h:66
void(* b0_fini)(struct m0_be_domain *dom, const char *suffix, const struct m0_buf *data)
Definition: seg0.h:65
int m0_be_0type_add(struct m0_be_0type *zt, struct m0_be_domain *dom, struct m0_be_tx *tx, const char *suffix, const struct m0_buf *data)
Definition: seg0.c:79
M0_INTERNAL void m0_be_seg_dict_delete_credit(struct m0_be_seg *seg, const char *name, struct m0_be_tx_credit *accum)
Definition: seg_dict.c:131
int m0_be_0type_del(struct m0_be_0type *zt, struct m0_be_domain *dom, struct m0_be_tx *tx, const char *suffix)
Definition: seg0.c:112
static struct m0_be_seg * be_0type_seg0_get(struct m0_be_domain *dom)
Definition: seg0.c:54
static struct m0_be_seg * seg
Definition: btree.c:40
#define M0_ASSERT_INFO(cond, fmt,...)
#define M0_BE_TX_CREDIT_BUF(buf)
Definition: tx_credit.h:99
M0_INTERNAL void m0_be_allocator_credit(struct m0_be_allocator *a, enum m0_be_allocator_op optype, m0_bcount_t size, unsigned shift, struct m0_be_tx_credit *accum)
Definition: alloc.c:900
M0_INTERNAL void m0_be_seg_dict_insert_credit(struct m0_be_seg *seg, const char *name, struct m0_be_tx_credit *accum)
Definition: seg_dict.c:114
M0_INTERNAL int m0_be_seg_dict_lookup(struct m0_be_seg *seg, const char *name, void **out)
Definition: seg_dict.c:180
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
static void keyname_format(const struct m0_be_0type *zt, const char *suffix, char *keyname, size_t keyname_len)
Definition: seg0.c:48
Definition: tx.h:280
#define M0_BE_ALLOC_CREDIT_BUF(buf, seg, accum)
Definition: alloc.h:371