Motr  M0
wlock_helper.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-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_RM
24 #include "lib/trace.h"
25 #include "lib/string.h"
26 #include "rm/rm.h"
27 #include "rm/rm_rwlock.h"
28 #include "rpc/rpclib.h"
29 
30 struct wlock_ctx {
34  char *wlc_rm_addr;
35  struct m0_fid wlc_rm_fid;
54  int32_t wlc_rc;
55 } wlx;
56 
57 static void write_lock_complete(struct m0_rm_incoming *in,
58  int32_t rc)
59 {
60  M0_ENTRY("incoming %p, rc %d", in, rc);
61  wlx.wlc_rc = rc;
63  M0_LEAVE();
64 }
65 
66 static void write_lock_conflict(struct m0_rm_incoming *in)
67 {
68  /* Do nothing */
69 }
70 
71 static struct m0_rm_incoming_ops ri_ops = {
73  .rio_conflict = write_lock_conflict,
74 };
75 
76 static int wlock_ctx_create(struct m0_rpc_machine *rpc_mach, const char *rm_ep)
77 {
78  int rc;
79 
80  wlx.wlc_rmach = rpc_mach;
85  &wlx.wlc_rwlock, NULL);
86  wlx.wlc_rm_addr = m0_strdup(rm_ep);
88  return M0_RC(rc);
89 }
90 
91 static int wlock_ctx_connect(struct wlock_ctx *wlx)
92 {
93  enum { MAX_RPCS_IN_FLIGHT = 15 };
94 
95  M0_PRE(wlx != NULL);
99 }
100 
102 {
103  M0_ENTRY("wlx=%p", wlx);
104  M0_PRE(wlx != NULL);
108  M0_LEAVE();
109 }
110 
111 static void _write_lock_get(struct wlock_ctx *wlx)
112 {
113  M0_ENTRY("wlx=%p", wlx);
114  M0_PRE(wlx != NULL);
119  M0_LEAVE();
120 }
121 
122 static void wlock_ctx_destroy(struct wlock_ctx *wlx)
123 {
124  M0_ENTRY("wlx=%p", wlx);
125  M0_PRE(wlx != NULL);
126  M0_LOG(M0_DEBUG, "owner wound up");
130  M0_LEAVE();
131 }
132 
133 static void wlock_ctx_disconnect(struct wlock_ctx *wlx)
134 {
135  int rc;
136 
137  M0_ENTRY("wlx=%p", wlx);
138  M0_PRE(_0C(wlx != NULL) && _0C(!M0_IS0(&wlx->wlc_sess)));
140  if (rc != 0)
141  M0_LOG(M0_ERROR, "Failed to destroy wlock session");
143  if (rc != 0)
144  M0_LOG(M0_ERROR, "Failed to destroy wlock connection");
145  M0_LEAVE();
146 
147 }
148 
150 {
151  int rc;
152 
153  M0_ENTRY("wlx=%p", wlx);
157  M0_TIME_NEVER);
158  M0_ASSERT(rc == 0);
159  M0_LEAVE();
160 }
161 
163 {
164  M0_ENTRY("wlx=%p", wlx);
165  M0_PRE(wlx != NULL);
169  M0_LEAVE();
170 }
171 
172 static void _write_lock_put(struct wlock_ctx *wlx)
173 {
174  struct m0_rm_incoming *req;
175 
176  M0_ENTRY("wlx=%p", wlx);
177  M0_PRE(wlx != NULL);
178  req = &wlx->wlc_req;
183  M0_LEAVE();
184 }
185 
186 M0_INTERNAL void rm_write_lock_put()
187 {
192  M0_LEAVE();
193 }
194 
195 M0_INTERNAL int rm_write_lock_get(struct m0_rpc_machine *rpc_mach,
196  const char *rm_ep)
197 {
198  int rc;
199 
200  rc = wlock_ctx_create(rpc_mach, rm_ep);
201  if (rc != 0) {
202  M0_ERR(rc);
203  goto fail;
204  }
206  if (rc != 0) {
207  M0_ERR_INFO(rc, "ep=%s", rm_ep);
208  goto ctx_free;
209  }
213  rc = wlx.wlc_rc;
214  if (rc != 0) {
215  M0_ERR(rc);
216  goto ctx_destroy;
217  }
218  return M0_RC(rc);
219 ctx_destroy:
224 ctx_free:
226 fail:
227  return M0_ERR(rc);
228 }
229 
230 #undef M0_TRACE_SUBSYSTEM
231 
232 /*
233  * Local variables:
234  * c-indentation-style: "K&R"
235  * c-basic-offset: 8
236  * tab-width: 8
237  * fill-column: 80
238  * scroll-step: 1
239  * End:
240  */
M0_INTERNAL int rm_write_lock_get(struct m0_rpc_machine *rpc_mach, const char *rm_ep)
Definition: wlock_helper.c:195
#define M0_PRE(cond)
static void wlock_ctx_owner_windup(struct wlock_ctx *wlx)
Definition: wlock_helper.c:149
#define m0_strdup(s)
Definition: string.h:43
int32_t wlc_rc
Definition: wlock_helper.c:54
struct m0_rw_lockable wlc_rwlock
Definition: wlock_helper.c:36
#define NULL
Definition: misc.h:38
struct m0_rm_remote * ro_creditor
Definition: rm.h:1026
void(* rio_complete)(struct m0_rm_incoming *in, int32_t rc)
Definition: rm.h:1493
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
static struct io_request req
Definition: file.c:100
static void write_lock_complete(struct m0_rm_incoming *in, int32_t rc)
Definition: wlock_helper.c:57
static int wlock_ctx_connect(struct wlock_ctx *wlx)
Definition: wlock_helper.c:91
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
M0_INTERNAL void m0_rm_rwlock_req_init(struct m0_rm_incoming *req, struct m0_rm_owner *owner, const struct m0_rm_incoming_ops *ops, enum m0_rm_incoming_flags flags, enum m0_rm_rwlock_req_type type)
Definition: rm_rwlock.c:548
int m0_rpc_session_destroy(struct m0_rpc_session *session, m0_time_t abs_timeout)
Definition: session.c:559
#define M0_BITS(...)
Definition: misc.h:236
M0_INTERNAL void m0_rm_remote_init(struct m0_rm_remote *rem, struct m0_rm_resource *res)
Definition: rm.c:1411
#define M0_SET0(obj)
Definition: misc.h:64
struct m0_rpc_session * rem_session
Definition: rm.h:755
M0_INTERNAL void m0_rw_lockable_fini(struct m0_rw_lockable *lockable)
Definition: rm_rwlock.c:523
M0_INTERNAL int m0_rm_owner_timedwait(struct m0_rm_owner *owner, uint64_t state, const m0_time_t abs_timeout)
Definition: rm.c:892
return M0_RC(rc)
struct m0_rpc_machine * wlc_rmach
Definition: wlock_helper.c:31
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL void m0_rm_rwlock_owner_init(struct m0_rm_owner *owner, struct m0_fid *fid, struct m0_rw_lockable *lockable, struct m0_rm_remote *creditor)
Definition: rm_rwlock.c:532
static struct m0_rm_incoming_ops ri_ops
Definition: wlock_helper.c:71
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL int m0_rpc_client_connect(struct m0_rpc_conn *conn, struct m0_rpc_session *session, struct m0_rpc_machine *rpc_mach, const char *remote_addr, struct m0_fid *svc_fid, uint64_t max_rpcs_in_flight, m0_time_t abs_timeout)
Definition: rpclib.c:102
struct wlock_ctx wlx
M0_INTERNAL void m0_rm_rwlock_owner_fini(struct m0_rm_owner *owner)
Definition: rm_rwlock.c:542
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
struct m0_semaphore wlc_sem
Definition: wlock_helper.c:42
M0_INTERNAL void m0_fid_tgenerate(struct m0_fid *fid, const uint8_t tid)
Definition: fid.c:155
M0_INTERNAL void m0_rw_lockable_init(struct m0_rw_lockable *lockable, const struct m0_fid *fid, struct m0_rm_domain *dom)
Definition: rm_rwlock.c:508
int m0_rpc_conn_destroy(struct m0_rpc_conn *conn, m0_time_t abs_timeout)
Definition: conn.c:974
M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore, unsigned value)
Definition: semaphore.c:38
static void _write_lock_put(struct wlock_ctx *wlx)
Definition: wlock_helper.c:172
static void _write_lock_get(struct wlock_ctx *wlx)
Definition: wlock_helper.c:111
const struct m0_fid M0_RWLOCK_FID
Definition: rm_rwlock.c:596
static void write_lock_conflict(struct m0_rm_incoming *in)
Definition: wlock_helper.c:66
struct m0_rm_owner wlc_owner
Definition: wlock_helper.c:37
struct m0_rm_incoming wlc_req
Definition: wlock_helper.c:40
M0_INTERNAL int m0_rwlockable_domain_type_init(struct m0_rm_domain *rwl_dom, struct m0_rm_resource_type *rwl_rt)
Definition: rm_rwlock.c:256
struct m0_fid wlc_owner_fid
Definition: wlock_helper.c:38
static int wlock_ctx_create(struct m0_rpc_machine *rpc_mach, const char *rm_ep)
Definition: wlock_helper.c:76
M0_INTERNAL void m0_rm_credit_put(struct m0_rm_incoming *in)
Definition: rm.c:1797
struct m0_rpc_session wlc_sess
Definition: wlock_helper.c:33
static uint8_t fail[DATA_UNIT_COUNT_MAX+PARITY_UNIT_COUNT_MAX]
struct m0_fid wlc_rm_fid
Definition: wlock_helper.c:35
static void wlock_ctx_destroy(struct wlock_ctx *wlx)
Definition: wlock_helper.c:122
Definition: fid.h:38
M0_INTERNAL void m0_rm_remote_fini(struct m0_rm_remote *rem)
Definition: rm.c:1431
#define M0_IS0(obj)
Definition: misc.h:70
M0_INTERNAL void rm_write_lock_put()
Definition: wlock_helper.c:186
Definition: rm.h:863
M0_INTERNAL void m0_rm_owner_windup(struct m0_rm_owner *owner)
Definition: rm.c:930
#define _0C(exp)
Definition: assert.h:311
struct m0_rm_resource_type wlc_rt
Definition: wlock_helper.c:52
M0_INTERNAL void m0_rm_incoming_fini(struct m0_rm_incoming *in)
Definition: rm.c:1099
M0_INTERNAL void m0_rm_owner_creditor_reset(struct m0_rm_owner *owner, struct m0_rm_remote *creditor)
Definition: rm.c:904
struct m0_rm_domain wlc_dom
Definition: wlock_helper.c:47
struct m0_rm_remote wlc_creditor
Definition: wlock_helper.c:39
M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
Definition: semaphore.c:49
static void wlock_ctx_creditor_unset(struct wlock_ctx *wlx)
Definition: wlock_helper.c:162
static void wlock_ctx_creditor_setup(struct wlock_ctx *wlx)
Definition: wlock_helper.c:101
M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
Definition: semaphore.c:65
static void wlock_ctx_disconnect(struct wlock_ctx *wlx)
Definition: wlock_helper.c:133
M0_INTERNAL void m0_rwlockable_domain_type_fini(struct m0_rm_domain *rwl_dom, struct m0_rm_resource_type *rwl_rt)
Definition: rm_rwlock.c:267
struct m0_rpc_conn wlc_conn
Definition: wlock_helper.c:32
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL void m0_rm_credit_get(struct m0_rm_incoming *in)
Definition: rm.c:1758
struct m0_rm_resource * ro_resource
Definition: rm.h:1015
char * wlc_rm_addr
Definition: wlock_helper.c:34