Motr  M0
policy.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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_POOL
23 #include "lib/trace.h"
24 
25 #include "lib/errno.h"
26 #include "lib/memory.h"
27 #include "lib/tlist.h"
28 #include "module/instance.h"
29 #include "module/module.h"
30 #include "motr/magic.h"
31 #include "conf/confc.h" /* m0_confc_fini */
32 #include "conf/helpers.h" /* m0_conf_pver_get */
33 #include "pool/pool.h"
34 #include "pool/policy.h"
35 
42 M0_TL_DESCR_DEFINE(pver_policy_types, "pver policy type list", static,
43  struct m0_pver_policy_type, ppt_link, ppt_magic,
45 M0_TL_DEFINE(pver_policy_types, static, struct m0_pver_policy_type);
46 
47 /*
48  * Pool version selection policies.
49  */
50 
52 static int pver_first_available_init(struct m0_pver_policy *policy);
54  const struct m0_pool *pool,
55  struct m0_pool_version **pver);
56 static void pver_first_available_fini(struct m0_pver_policy *policy);
57 
58 static const struct m0_pver_policy_type_ops
61 };
62 
66 };
67 
69  .ppt_name = "pver_policy_first_available",
72 };
73 
76  .ppo_fini = pver_first_available_fini,
77  .ppo_get = pver_first_available_get
78 };
79 
81 {
83 
84  M0_ALLOC_PTR(fa);
85  if (fa == NULL)
86  return M0_ERR(-ENOMEM);
87 
88  *out = &fa->fcp_policy;
90  return M0_RC(0);
91 }
92 
93 static int pver_first_available_init(struct m0_pver_policy *policy)
94 {
95  M0_ENTRY();
96  return M0_RC(0);
97 }
98 
99 static void pver_first_available_fini(struct m0_pver_policy *policy)
100 {
101  M0_ENTRY();
103  fcp_policy));
104  M0_LEAVE();
105 }
106 
108  const struct m0_pool *pool,
109  struct m0_pool_version **pv)
110 {
111  struct m0_conf_pver *pver;
112  int rc;
113 
114  M0_ENTRY("pool="FID_F, FID_P(&pool->po_id));
115 
116  /* Check pool version cache */
118  if (*pv != NULL)
119  return M0_RC(0);
120 
121  /* Derive new pver using formulae */
123  if (rc != 0)
124  return M0_ERR(rc);
125 
126  /* Cache derived if not present */
127  *pv = m0_pool_version_lookup(pc, &pver->pv_obj.co_id);
128  if (*pv == NULL)
130 
131  m0_confc_close(&pver->pv_obj);
132  return M0_RC(rc);
133 }
134 
135 static struct m0_pver_policies *pver_policies(void)
136 {
137  return m0_get()->i_moddata[M0_MODULE_POOL];
138 }
139 
140 M0_INTERNAL struct m0_pver_policy_type *
142 {
143  return m0_tl_find(pver_policy_types, pvpt, &pver_policies()->pp_types,
144  pvpt->ppt_code == code);
145 }
146 
147 M0_INTERNAL int m0_pver_policy_types_nr(void)
148 {
149  return pver_policy_types_tlist_length(&pver_policies()->pp_types);
150 }
151 
152 M0_INTERNAL
154 {
155  pver_policy_types_tlink_init_at_tail(type, &pver_policies()->pp_types);
156  return M0_RC(0);
157 }
158 
159 M0_INTERNAL
161 {
162  pver_policy_types_tlink_del_fini(type);
163 }
164 
165 M0_INTERNAL int m0_pver_policies_init(void)
166 {
167  struct m0_pver_policies *policies;
168 
169  M0_ALLOC_PTR(policies);
170  if (policies == NULL)
171  return M0_ERR(-ENOMEM);
172 
173  m0_get()->i_moddata[M0_MODULE_POOL] = policies;
174  pver_policy_types_tlist_init(&policies->pp_types);
175 
178 }
179 
180 M0_INTERNAL void m0_pver_policies_fini(void)
181 {
183  pver_policy_types_tlist_fini(&pver_policies()->pp_types);
186 }
187 
189 #undef M0_TRACE_SUBSYSTEM
190 
191 /*
192  * Local variables:
193  * c-indentation-style: "K&R"
194  * c-basic-offset: 8
195  * tab-width: 8
196  * fill-column: 80
197  * scroll-step: 1
198  * End:
199  */
static const struct m0_pver_policy_ops first_available_pver_policy_ops
Definition: policy.c:74
M0_INTERNAL int m0_pool_version_append(struct m0_pools_common *pc, struct m0_conf_pver *pver, struct m0_pool_version **pv)
Definition: pool.c:1725
#define NULL
Definition: misc.h:38
static struct m0_pver_policy_type first_available_pver_policy_type
Definition: policy.c:68
static int pver_first_available_get(struct m0_pools_common *pc, const struct m0_pool *pool, struct m0_pool_version **pver)
Definition: policy.c:107
M0_INTERNAL int m0_pver_policy_types_nr(void)
Definition: policy.c:147
struct m0_pool_version * pv
Definition: dir.c:629
M0_LEAVE()
const struct m0_pver_policy_ops * pp_ops
Definition: policy.h:48
M0_INTERNAL struct m0 * m0_get(void)
Definition: instance.c:41
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_TL_DEFINE(pver_policy_types, static, struct m0_pver_policy_type)
static struct m0_pools_common pc
Definition: iter_ut.c:59
static int pver_first_available_init(struct m0_pver_policy *policy)
Definition: policy.c:93
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL int m0_conf_pver_get(struct m0_confc *confc, const struct m0_fid *pool, struct m0_conf_pver **out)
Definition: helpers.c:71
return M0_ERR(-EOPNOTSUPP)
static void pver_first_available_fini(struct m0_pver_policy *policy)
Definition: policy.c:99
M0_INTERNAL struct m0_pver_policy_type * m0_pver_policy_type_find(enum m0_pver_policy_code code)
Definition: policy.c:141
int(* ppto_create)(struct m0_pver_policy **out)
Definition: policy.h:53
m0_pver_policy_code
Definition: policy.h:40
struct m0_fid pver
Definition: idx_dix.c:74
struct m0_tl pp_types
Definition: policy.h:85
static const struct m0_pver_policy_type_ops first_vailable_pver_policy_type_ops
Definition: policy.c:59
M0_INTERNAL int m0_pver_policy_type_register(struct m0_pver_policy_type *type)
Definition: policy.c:153
struct m0_confc * pc_confc
Definition: pool.h:164
#define FID_P(f)
Definition: fid.h:77
static struct m0_pool pool
Definition: iter_ut.c:58
static struct m0_pver_policies * pver_policies(void)
Definition: policy.c:135
int(* ppo_init)(struct m0_pver_policy *pver_policy)
Definition: policy.h:67
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
void * i_moddata[M0_MODULE_NR]
Definition: instance.h:94
M0_INTERNAL void m0_pver_policy_type_deregister(struct m0_pver_policy_type *type)
Definition: policy.c:160
struct m0_fid po_id
Definition: pool.h:81
M0_INTERNAL void m0_confc_close(struct m0_conf_obj *obj)
Definition: confc.c:921
Definition: pool.h:80
#define out(...)
Definition: gen.c:41
struct m0_pver_policy fcp_policy
Definition: policy.c:65
int type
Definition: dir.c:1031
M0_INTERNAL struct m0_pool_version * m0_pool_clean_pver_find(const struct m0_pool *pool)
Definition: pool.c:556
M0_TL_DESCR_DEFINE(pver_policy_types, "pver policy type list", static, struct m0_pver_policy_type, ppt_link, ppt_magic, M0_PVER_POLICY_MAGIC, M0_PVER_POLICY_HEAD_MAGIC)
#define m0_tl_find(name, var, head,...)
Definition: tlist.h:757
static int pver_first_available_create(struct m0_pver_policy **out)
Definition: policy.c:80
M0_INTERNAL struct m0_pool_version * m0_pool_version_lookup(const struct m0_pools_common *pc, const struct m0_fid *id)
Definition: pool.c:568
void m0_free(void *data)
Definition: memory.c:146
M0_INTERNAL void m0_pver_policies_fini(void)
Definition: policy.c:180
int32_t rc
Definition: trigger_fop.h:47
const char * ppt_name
Definition: policy.h:58
#define FID_F
Definition: fid.h:75
M0_INTERNAL int m0_pver_policies_init(void)
Definition: policy.c:165