Motr  M0
entrypoint_fops.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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 
32 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_HA
33 #include "lib/trace.h"
34 
35 #include "ha/entrypoint_fops.h"
36 #include "ha/entrypoint_fops_xc.h"
37 
38 #include "lib/memory.h" /* M0_ALLOC_ARR */
39 #include "lib/string.h" /* memcpy */
40 
41 #include "fop/fop.h" /* M0_FOP_TYPE_INIT */
42 #include "rpc/rpc_opcodes.h" /* M0_HA_ENTRYPOINT_REQ_OPCODE */
43 #include "rpc/item.h" /* M0_RPC_ITEM_TYPE_REQUEST */
44 
45 #include "ha/entrypoint.h" /* m0_ha_entrypoint_service_type */
46 
49 
50 M0_INTERNAL void m0_ha_entrypoint_fops_init(void)
51 {
53  .name = "HA Cluster Entry Point Get Req",
55  .xt = m0_ha_entrypoint_req_fop_xc,
58  .svc_type = &m0_ha_entrypoint_service_type,
59  .rpc_flags = M0_RPC_ITEM_TYPE_REQUEST);
61  .name = "HA Cluster Entry Point Get Reply",
63  .xt = m0_ha_entrypoint_rep_fop_xc,
64  .svc_type = &m0_ha_entrypoint_service_type,
65  .rpc_flags = M0_RPC_ITEM_TYPE_REPLY);
66 }
67 
68 M0_INTERNAL void m0_ha_entrypoint_fops_fini(void)
69 {
72 }
73 
74 M0_INTERNAL int
76  struct m0_ha_entrypoint_req_fop *req_fop)
77 {
78  char *git_rev_id_dup = m0_strdup(req->heq_git_rev_id);
79 
80  if (git_rev_id_dup == NULL)
81  return M0_ERR(-ENOMEM);
82  *req_fop = (struct m0_ha_entrypoint_req_fop){
83  .erf_first_request = req->heq_first_request ? 1 : 0,
84  .erf_generation = req->heq_generation,
85  .erf_process_fid = req->heq_process_fid,
86  .erf_link_params = req->heq_link_params,
87  .erf_git_rev_id = M0_BUF_INITS(git_rev_id_dup),
88  .erf_pid = req->heq_pid,
89  };
90  m0_ha_cookie_to_xc(&req->heq_cookie_expected,
91  &req_fop->erf_cookie_expected);
92  return 0;
93 }
94 
95 M0_INTERNAL int
97  const char *rpc_endpoint,
98  struct m0_ha_entrypoint_req *req)
99 {
100  char *rpc_endpoint_dup = m0_strdup(rpc_endpoint);
101  char *git_rev_id_dup = m0_buf_strdup(&req_fop->erf_git_rev_id);
102 
103  if (rpc_endpoint_dup == NULL || git_rev_id_dup == NULL) {
104  m0_free(rpc_endpoint_dup);
105  m0_free(git_rev_id_dup);
106  return M0_ERR(-ENOMEM);
107  }
108  *req = (struct m0_ha_entrypoint_req){
109  .heq_first_request = req_fop->erf_first_request != 0,
110  .heq_generation = req_fop->erf_generation,
111  .heq_process_fid = req_fop->erf_process_fid,
112  .heq_rpc_endpoint = rpc_endpoint_dup,
113  .heq_link_params = req_fop->erf_link_params,
114  .heq_git_rev_id = git_rev_id_dup,
115  .heq_pid = req_fop->erf_pid,
116  };
117  m0_ha_cookie_from_xc(&req->heq_cookie_expected,
118  &req_fop->erf_cookie_expected);
119  return M0_RC(0);
120 }
121 
122 M0_INTERNAL int
124  struct m0_ha_entrypoint_rep *rep)
125 {
126  uint32_t i;
127  int rc;
128 
129  *rep = (struct m0_ha_entrypoint_rep){
130  .hae_quorum = rep_fop->hbp_quorum,
131  .hae_confd_fids = {
132  .af_count = rep_fop->hbp_confd_fids.af_count,
133  },
134  .hae_active_rm_fid = rep_fop->hbp_active_rm_fid,
135  .hae_active_rm_ep = m0_buf_strdup(&rep_fop->hbp_active_rm_ep),
136  .hae_link_params = rep_fop->hbp_link_params,
137  .hae_link_do_reconnect = !!rep_fop->hbp_link_do_reconnect,
138  .hae_disconnected_previously =
139  !!rep_fop->hbp_disconnected_previously,
140  .hae_control = rep_fop->hbp_control,
141  };
142  M0_ASSERT(rep->hae_active_rm_ep != NULL);
143  M0_ASSERT(rep->hae_confd_fids.af_count ==
144  rep_fop->hbp_confd_eps.ab_count);
145  rc = m0_bufs_to_strings(&rep->hae_confd_eps, &rep_fop->hbp_confd_eps);
146  M0_ASSERT(rc == 0);
147  M0_ALLOC_ARR(rep->hae_confd_fids.af_elems,
148  rep->hae_confd_fids.af_count);
149  M0_ASSERT(rep->hae_confd_fids.af_elems != NULL);
150  for (i = 0; i < rep->hae_confd_fids.af_count; ++i) {
151  rep->hae_confd_fids.af_elems[i] =
152  rep_fop->hbp_confd_fids.af_elems[i];
153  }
154  m0_ha_cookie_from_xc(&rep->hae_cookie_actual,
155  &rep_fop->hbp_cookie_actual);
156  return M0_RC(0);
157 }
158 
159 M0_INTERNAL int
162 {
163  uint32_t i;
164  char *rm_ep;
165  int rc;
166 
167  rm_ep = rep->hae_active_rm_ep == NULL ?
168  m0_strdup("") : m0_strdup(rep->hae_active_rm_ep);
169  *rep_fop = (struct m0_ha_entrypoint_rep_fop){
170  .hbp_quorum = rep->hae_quorum,
171  .hbp_confd_fids = {
172  .af_count = rep->hae_confd_fids.af_count,
173  },
174  .hbp_confd_eps = {
175  .ab_count = rep->hae_confd_fids.af_count,
176  },
177  .hbp_active_rm_fid = rep->hae_active_rm_fid,
178  .hbp_active_rm_ep = M0_BUF_INITS(rm_ep),
179  .hbp_link_params = rep->hae_link_params,
180  .hbp_link_do_reconnect = !!rep->hae_link_do_reconnect,
181  .hbp_disconnected_previously =
182  !!rep->hae_disconnected_previously,
183  .hbp_control = rep->hae_control,
184  };
185  M0_ALLOC_ARR(rep_fop->hbp_confd_eps.ab_elems,
186  rep_fop->hbp_confd_eps.ab_count);
187  M0_ASSERT(rep_fop->hbp_confd_eps.ab_elems != NULL);
188  for (i = 0; i < rep_fop->hbp_confd_eps.ab_count; ++i) {
189  rc = m0_buf_copy(&rep_fop->hbp_confd_eps.ab_elems[i],
190  &M0_BUF_INITS((char *)rep->hae_confd_eps[i]));
191  M0_ASSERT(rc == 0);
192  }
193  M0_ALLOC_ARR(rep_fop->hbp_confd_fids.af_elems,
194  rep_fop->hbp_confd_fids.af_count);
195  M0_ASSERT(rep_fop->hbp_confd_fids.af_elems != NULL);
196  for (i = 0; i < rep_fop->hbp_confd_fids.af_count; ++i) {
197  rep_fop->hbp_confd_fids.af_elems[i] =
198  rep->hae_confd_fids.af_elems[i];
199  }
200  m0_ha_cookie_to_xc(&rep->hae_cookie_actual,
201  &rep_fop->hbp_cookie_actual);
202  return 0;
203 }
204 
206 {
207  m0_free0(&rep->hae_confd_fids.af_elems);
208  m0_strings_free(rep->hae_confd_eps);
209  m0_free0(&rep->hae_active_rm_ep);
210 }
211 
213 {
214  m0_free(req->heq_rpc_endpoint);
215  /* It should be allocated by m0_alloc() in m0_ha_entrypoint_fop2req */
216  m0_free((char *)req->heq_git_rev_id);
217 }
218 
220  struct m0_ha_entrypoint_rep *from)
221 {
222  int rc;
223 
224  if (from->hae_confd_fids.af_count == 0 || from->hae_confd_eps == NULL)
225  return M0_RC(-EINVAL);
226 
227  *to = (struct m0_ha_entrypoint_rep) {
228  .hae_quorum = from->hae_quorum,
229  .hae_active_rm_fid = from->hae_active_rm_fid,
230  .hae_link_params = from->hae_link_params,
231  .hae_active_rm_ep = m0_strdup(from->hae_active_rm_ep),
232  .hae_control = from->hae_control,
233  };
234  if (to->hae_active_rm_ep == NULL) {
235  rc = -ENOMEM;
236  goto out;
237  }
238 
240  if (rc != 0)
241  goto out;
242 
244  if (to->hae_confd_eps == NULL) {
245  rc = -ENOMEM;
246  goto out;
247  }
248 
249  return M0_RC(0);
250 out:
252  return M0_RC(rc);
253 }
254 
255 #undef M0_TRACE_SUBSYSTEM
256 
259 /*
260  * Local variables:
261  * c-indentation-style: "K&R"
262  * c-basic-offset: 8
263  * tab-width: 8
264  * fill-column: 80
265  * scroll-step: 1
266  * End:
267  */
268 /*
269  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
270  */
struct m0_fid hae_active_rm_fid
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
M0_INTERNAL const char ** m0_strings_dup(const char **src)
Definition: string.c:55
#define m0_strdup(s)
Definition: string.h:43
M0_INTERNAL int m0_ha_entrypoint_rep_copy(struct m0_ha_entrypoint_rep *to, struct m0_ha_entrypoint_rep *from)
#define NULL
Definition: misc.h:38
#define M0_FOP_TYPE_INIT(ft,...)
Definition: fop.h:307
static struct io_request req
Definition: file.c:100
const char ** hae_confd_eps
struct m0_ha_link_params hae_link_params
M0_INTERNAL int m0_ha_entrypoint_fop2req(const struct m0_ha_entrypoint_req_fop *req_fop, const char *rpc_endpoint, struct m0_ha_entrypoint_req *req)
void m0_fop_type_fini(struct m0_fop_type *fopt)
Definition: fop.c:232
M0_INTERNAL void m0_ha_cookie_to_xc(const struct m0_ha_cookie *hc, struct m0_ha_cookie_xc *hc_xc)
Definition: cookie.c:101
static struct m0_xcode_type ** xt[]
Definition: protocol.c:64
struct m0_fop_getxattr_rep * rep
Definition: dir.c:455
struct m0_fop_type m0_ha_entrypoint_rep_fopt
return M0_RC(rc)
struct m0_fop_type m0_ha_entrypoint_req_fopt
int opcode
Definition: crate.c:301
int i
Definition: dir.c:1033
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL int m0_ha_entrypoint_req2fop(const struct m0_ha_entrypoint_req *req, struct m0_ha_entrypoint_req_fop *req_fop)
const char * name
Definition: trace.c:110
M0_INTERNAL void m0_ha_entrypoint_fops_init(void)
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
#define M0_BUF_INITS(str)
Definition: buf.h:70
M0_INTERNAL int m0_ha_entrypoint_rep2fop(const struct m0_ha_entrypoint_rep *rep, struct m0_ha_entrypoint_rep_fop *rep_fop)
M0_INTERNAL int m0_buf_copy(struct m0_buf *dest, const struct m0_buf *src)
Definition: buf.c:104
M0_INTERNAL int m0_ha_entrypoint_fop2rep(const struct m0_ha_entrypoint_rep_fop *rep_fop, struct m0_ha_entrypoint_rep *rep)
struct m0_reqh_service_type m0_ha_entrypoint_service_type
Definition: entrypoint.c:135
enum m0_ha_entrypoint_control hae_control
M0_INTERNAL char * m0_buf_strdup(const struct m0_buf *buf)
Definition: buf.c:140
struct m0_ha_cookie_xc erf_cookie_expected
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
M0_INTERNAL void m0_ha_entrypoint_req_free(struct m0_ha_entrypoint_req *req)
M0_INTERNAL int m0_fid_arr_copy(struct m0_fid_arr *to, const struct m0_fid_arr *from)
Definition: fid.c:303
M0_INTERNAL void m0_ha_entrypoint_rep_free(struct m0_ha_entrypoint_rep *rep)
M0_INTERNAL int m0_bufs_to_strings(const char ***dest, const struct m0_bufs *src)
Definition: buf.c:188
M0_INTERNAL void m0_ha_entrypoint_fops_fini(void)
#define out(...)
Definition: gen.c:41
M0_INTERNAL void m0_ha_cookie_from_xc(struct m0_ha_cookie *hc, const struct m0_ha_cookie_xc *hc_xc)
Definition: cookie.c:90
const struct m0_fom_type_ops m0_ha_entrypoint_fom_type_ops
Definition: entrypoint.c:959
struct m0_fid_arr hae_confd_fids
struct m0_ha_link_params erf_link_params
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
struct m0_sm_conf m0_ha_entrypoint_server_fom_states_conf
Definition: entrypoint.c:188
uint32_t af_count
Definition: fid.h:44
struct m0_fop * rep_fop
Definition: dir.c:334