1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * by Cluster File Systems, Inc.
12 #define DEBUG_SUBSYSTEM S_LDLM
14 #include <linux/lustre_dlm.h>
16 int ldlm_cli_enqueue(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
17 struct ptlrpc_request *req,
18 struct ldlm_namespace *ns,
19 struct lustre_handle *parent_lock_handle,
22 void *cookie, int cookielen,
25 ldlm_lock_callback callback,
28 struct lustre_handle *lockh)
30 struct ldlm_lock *lock;
31 struct ldlm_request *body;
32 struct ldlm_reply *reply;
33 int rc, size = sizeof(*body), req_passed_in = 1;
37 lock = ldlm_local_lock_create(ns, parent_lock_handle, res_id, type,
38 mode, data, data_len);
40 GOTO(out, rc = -ENOMEM);
42 LDLM_DEBUG(lock, "client-side enqueue START");
45 req = ptlrpc_prep_req(cl, conn, LDLM_ENQUEUE, 1, &size, NULL);
47 GOTO(out, rc = -ENOMEM);
49 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
52 /* Dump all of this data into the request buffer */
53 body = lustre_msg_buf(req->rq_reqmsg, 0);
54 body->lock_desc.l_resource.lr_type = type;
55 memcpy(body->lock_desc.l_resource.lr_name, res_id,
56 sizeof(body->lock_desc.l_resource.lr_name));
58 body->lock_desc.l_req_mode = mode;
59 if (type == LDLM_EXTENT)
60 memcpy(&body->lock_desc.l_extent, cookie,
61 sizeof(body->lock_desc.l_extent));
62 body->lock_flags = *flags;
64 ldlm_lock2handle(lock, &body->lock_handle1);
65 if (parent_lock_handle)
66 memcpy(&body->lock_handle2, parent_lock_handle,
67 sizeof(body->lock_handle2));
69 /* Continue as normal. */
71 size = sizeof(*reply);
72 req->rq_replen = lustre_msg_size(1, &size);
75 lock->l_connection = conn;
78 rc = ptlrpc_queue_wait(req);
79 rc = ptlrpc_check_status(req, rc);
82 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
83 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
88 reply = lustre_msg_buf(req->rq_repmsg, 0);
89 memcpy(&lock->l_remote_handle, &reply->lock_handle,
90 sizeof(lock->l_remote_handle));
91 if (type == LDLM_EXTENT)
92 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
93 *flags = reply->lock_flags;
95 CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
96 (void *)(unsigned long)reply->lock_handle.addr, *flags);
97 CDEBUG(D_INFO, "extent: %Lu -> %Lu\n",
98 (unsigned long long)reply->lock_extent.start,
99 (unsigned long long)reply->lock_extent.end);
101 /* If enqueue returned a blocked lock but the completion handler has
102 * already run, then it fixed up the resource and we don't need to do it
104 if (*flags & LDLM_FL_LOCK_CHANGED &&
105 lock->l_req_mode != lock->l_granted_mode) {
106 CDEBUG(D_INFO, "remote intent success, locking %ld instead of"
107 "%ld\n", (long)reply->lock_resource_name[0],
108 (long)lock->l_resource->lr_name[0]);
110 ldlm_lock_change_resource(lock, reply->lock_resource_name);
111 if (lock->l_resource == NULL) {
115 LDLM_DEBUG(lock, "client-side enqueue, new resource");
119 ptlrpc_free_req(req);
121 rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, callback,
124 LDLM_DEBUG(lock, "client-side enqueue END");
125 if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
126 LDLM_FL_BLOCK_CONV)) {
127 /* Go to sleep until the lock is granted. */
128 /* FIXME: or cancelled. */
129 CDEBUG(D_NET, "enqueue returned a blocked lock (%p), "
130 "going to sleep.\n", lock);
131 ldlm_lock_dump(lock);
132 wait_event_interruptible(lock->l_waitq, lock->l_req_mode ==
133 lock->l_granted_mode);
134 CDEBUG(D_NET, "waking up, the lock must be granted.\n");
142 int ldlm_cli_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
143 void *data, __u32 data_len, struct ptlrpc_request **reqp)
145 struct ldlm_request *body;
146 struct ptlrpc_request *req;
147 struct ptlrpc_client *cl =
148 &lock->l_resource->lr_namespace->ns_rpc_client;
149 int rc = 0, size = sizeof(*body);
152 req = ptlrpc_prep_req(cl, lock->l_connection, LDLM_CALLBACK, 1,
155 GOTO(out, rc = -ENOMEM);
157 body = lustre_msg_buf(req->rq_reqmsg, 0);
158 memcpy(&body->lock_handle1, &lock->l_remote_handle,
159 sizeof(body->lock_handle1));
162 CDEBUG(D_NET, "Sending granted AST\n");
163 ldlm_lock2desc(lock, &body->lock_desc);
165 CDEBUG(D_NET, "Sending blocked AST\n");
166 ldlm_lock2desc(new, &body->lock_desc);
167 ldlm_object2handle(new, &body->lock_handle2);
170 LDLM_DEBUG(lock, "server preparing %s AST",
171 new == NULL ? "completion" : "blocked");
173 req->rq_replen = lustre_msg_size(0, NULL);
177 rc = ptlrpc_queue_wait(req);
178 rc = ptlrpc_check_status(req, rc);
179 ptlrpc_free_req(req);
188 int ldlm_cli_convert(struct ptlrpc_client *cl, struct lustre_handle *lockh,
189 int new_mode, int *flags)
191 struct ldlm_request *body;
192 struct ldlm_reply *reply;
193 struct ldlm_lock *lock;
194 struct ldlm_resource *res;
195 struct ptlrpc_request *req;
196 int rc, size = sizeof(*body);
199 lock = ldlm_handle2lock(lockh);
204 LDLM_DEBUG(lock, "client-side convert");
206 req = ptlrpc_prep_req(cl, lock->l_connection, LDLM_CONVERT, 1, &size,
209 GOTO(out, rc = -ENOMEM);
211 body = lustre_msg_buf(req->rq_reqmsg, 0);
212 memcpy(&body->lock_handle1, &lock->l_remote_handle,
213 sizeof(body->lock_handle1));
215 body->lock_desc.l_req_mode = new_mode;
216 body->lock_flags = *flags;
218 size = sizeof(*reply);
219 req->rq_replen = lustre_msg_size(1, &size);
221 rc = ptlrpc_queue_wait(req);
222 rc = ptlrpc_check_status(req, rc);
226 reply = lustre_msg_buf(req->rq_repmsg, 0);
227 res = ldlm_local_lock_convert(lockh, new_mode, &reply->lock_flags);
229 ldlm_reprocess_all(res);
230 if (lock->l_req_mode != lock->l_granted_mode) {
231 /* Go to sleep until the lock is granted. */
232 /* FIXME: or cancelled. */
233 CDEBUG(D_NET, "convert returned a blocked lock, "
234 "going to sleep.\n");
235 wait_event_interruptible(lock->l_waitq, lock->l_req_mode ==
236 lock->l_granted_mode);
237 CDEBUG(D_NET, "waking up, the lock must be granted.\n");
242 ptlrpc_free_req(req);
246 int ldlm_cli_cancel(struct ptlrpc_client *cl, struct lustre_handle *lockh)
248 struct ptlrpc_request *req;
249 struct ldlm_lock *lock;
250 struct ldlm_request *body;
251 struct ldlm_resource *res;
252 int rc, size = sizeof(*body);
255 lock = ldlm_handle2lock(lockh);
259 LDLM_DEBUG(lock, "client-side cancel");
260 req = ptlrpc_prep_req(cl, lock->l_connection, LDLM_CANCEL, 1, &size,
263 GOTO(out, rc = -ENOMEM);
265 body = lustre_msg_buf(req->rq_reqmsg, 0);
266 memcpy(&body->lock_handle1, &lock->l_remote_handle,
267 sizeof(body->lock_handle1));
269 req->rq_replen = lustre_msg_size(0, NULL);
271 rc = ptlrpc_queue_wait(req);
272 rc = ptlrpc_check_status(req, rc);
273 ptlrpc_free_req(req);
277 ldlm_lock_cancel(lock);
278 ldlm_reprocess_all(res);