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_completion_ast(struct ldlm_lock *lock, int flags)
21 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
22 LDLM_FL_BLOCK_CONV)) {
23 /* Go to sleep until the lock is granted. */
24 /* FIXME: or cancelled. */
25 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock,"
28 ldlm_reprocess_all(lock->l_resource);
29 wait_event(lock->l_waitq, lock->l_req_mode == lock->l_granted_mode);
30 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
31 } else if (flags == LDLM_FL_WAIT_NOREPROC) {
32 wait_event(lock->l_waitq, lock->l_req_mode == lock->l_granted_mode);
33 } else if (flags == 0) {
34 wake_up(&lock->l_waitq);
42 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
43 struct lustre_handle *parent_lockh,
46 void *cookie, int cookielen,
49 ldlm_completion_callback completion,
50 ldlm_lock_callback callback,
53 struct lustre_handle *lockh)
55 struct ldlm_lock *lock;
59 CERROR("Trying to cancel local lock\n");
63 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, NULL, 0);
65 GOTO(out_nolock, err = -ENOMEM);
66 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
68 ldlm_lock_addref_internal(lock, mode);
69 ldlm_lock2handle(lock, lockh);
72 err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion, callback);
76 if (type == LDLM_EXTENT)
77 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
78 if ((*flags) & LDLM_FL_LOCK_CHANGED)
79 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
81 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)", lock);
83 if (lock->l_completion_ast)
84 lock->l_completion_ast(lock, *flags);
86 LDLM_DEBUG(lock, "client-side local enqueue END");
95 int ldlm_cli_enqueue(struct lustre_handle *connh,
96 struct ptlrpc_request *req,
97 struct ldlm_namespace *ns,
98 struct lustre_handle *parent_lock_handle,
101 void *cookie, int cookielen,
104 ldlm_completion_callback completion,
105 ldlm_lock_callback callback,
108 struct lustre_handle *lockh)
110 struct ptlrpc_connection *connection;
111 struct ldlm_lock *lock;
112 struct ldlm_request *body;
113 struct ldlm_reply *reply;
114 int rc, size = sizeof(*body), req_passed_in = 1;
118 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
119 type, cookie, cookielen, mode,
120 flags, completion, callback, data, data_len, lockh);
121 connection = client_conn2cli(connh)->cl_conn;
124 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
127 GOTO(out_nolock, rc = -ENOMEM);
128 LDLM_DEBUG(lock, "client-side enqueue START");
129 /* for the local lock, add the reference */
130 ldlm_lock_addref_internal(lock, mode);
131 ldlm_lock2handle(lock, lockh);
134 req = ptlrpc_prep_req2(connh, LDLM_ENQUEUE, 1, &size, NULL);
136 GOTO(out, rc = -ENOMEM);
138 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
141 /* Dump all of this data into the request buffer */
142 body = lustre_msg_buf(req->rq_reqmsg, 0);
143 ldlm_lock2desc(lock, &body->lock_desc);
144 /* Phil: make this part of ldlm_lock2desc */
145 if (type == LDLM_EXTENT)
146 memcpy(&body->lock_desc.l_extent, cookie,
147 sizeof(body->lock_desc.l_extent));
148 body->lock_flags = *flags;
150 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
151 if (parent_lock_handle)
152 memcpy(&body->lock_handle2, parent_lock_handle,
153 sizeof(body->lock_handle2));
155 /* Continue as normal. */
156 if (!req_passed_in) {
157 size = sizeof(*reply);
158 req->rq_replen = lustre_msg_size(1, &size);
160 lock->l_connh = connh;
161 lock->l_connection = ptlrpc_connection_addref(connection);
162 lock->l_client = client_conn2cli(connh)->cl_client;
164 rc = ptlrpc_queue_wait(req);
165 /* FIXME: status check here? */
166 rc = ptlrpc_check_status(req, rc);
168 if (rc != ELDLM_OK) {
169 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
170 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
171 ldlm_lock_decref(lockh, mode);
172 /* FIXME: if we've already received a completion AST, this will
174 ldlm_lock_destroy(lock);
178 reply = lustre_msg_buf(req->rq_repmsg, 0);
179 memcpy(&lock->l_remote_handle, &reply->lock_handle,
180 sizeof(lock->l_remote_handle));
181 if (type == LDLM_EXTENT)
182 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
183 *flags = reply->lock_flags;
185 CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
186 (void *)(unsigned long)reply->lock_handle.addr, *flags);
187 CDEBUG(D_INFO, "extent: %Lu -> %Lu\n",
188 (unsigned long long)reply->lock_extent.start,
189 (unsigned long long)reply->lock_extent.end);
191 /* If enqueue returned a blocked lock but the completion handler has
192 * already run, then it fixed up the resource and we don't need to do it
194 if ((*flags) & LDLM_FL_LOCK_CHANGED &&
195 lock->l_req_mode != lock->l_granted_mode) {
196 CDEBUG(D_INFO, "remote intent success, locking %ld instead of"
197 "%ld\n", (long)reply->lock_resource_name[0],
198 (long)lock->l_resource->lr_name[0]);
200 ldlm_lock_change_resource(lock, reply->lock_resource_name);
201 if (lock->l_resource == NULL) {
205 LDLM_DEBUG(lock, "client-side enqueue, new resource");
209 ptlrpc_free_req(req);
211 rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
213 if (lock->l_completion_ast)
214 lock->l_completion_ast(lock, *flags);
216 LDLM_DEBUG(lock, "client-side enqueue END");
224 int ldlm_match_or_enqueue(struct lustre_handle *connh,
225 struct ptlrpc_request *req,
226 struct ldlm_namespace *ns,
227 struct lustre_handle *parent_lock_handle,
230 void *cookie, int cookielen,
233 ldlm_completion_callback completion,
234 ldlm_lock_callback callback,
237 struct lustre_handle *lockh)
241 rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
243 rc = ldlm_cli_enqueue(connh, req, ns,
244 parent_lock_handle, res_id, type, cookie,
245 cookielen, mode, flags, completion, callback, data,
248 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
254 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *desc,
255 void *data, __u32 data_len)
257 struct ldlm_lock *lock;
258 struct ldlm_request *body;
259 struct ptlrpc_request *req;
260 struct ptlrpc_client *cl;
261 int rc = 0, size = sizeof(*body);
264 lock = ldlm_handle2lock(lockh);
269 cl = &lock->l_resource->lr_namespace->ns_rpc_client;
270 req = ptlrpc_prep_req(cl, lock->l_connection, LDLM_CALLBACK, 1,
273 GOTO(out, rc = -ENOMEM);
275 body = lustre_msg_buf(req->rq_reqmsg, 0);
276 memcpy(&body->lock_handle1, &lock->l_remote_handle,
277 sizeof(body->lock_handle1));
280 CDEBUG(D_NET, "Sending granted AST\n");
281 ldlm_lock2desc(lock, &body->lock_desc);
283 CDEBUG(D_NET, "Sending blocked AST\n");
284 memcpy(&body->lock_desc, desc, sizeof(*desc));
287 LDLM_DEBUG(lock, "server preparing %s AST",
288 desc == 0 ? "completion" : "blocked");
290 req->rq_replen = lustre_msg_size(0, NULL);
292 rc = ptlrpc_queue_wait(req);
293 rc = ptlrpc_check_status(req, rc);
294 ptlrpc_free_req(req);
304 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode, int *flags)
307 if (lock->l_resource->lr_namespace->ns_client) {
308 CERROR("Trying to cancel local lock\n");
311 LDLM_DEBUG(lock, "client-side local convert");
313 ldlm_lock_convert(lock, new_mode, flags);
314 ldlm_reprocess_all(lock->l_resource);
316 LDLM_DEBUG(lock, "client-side local convert handler END");
321 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
323 struct ldlm_request *body;
324 struct lustre_handle *connh;
325 struct ldlm_reply *reply;
326 struct ldlm_lock *lock;
327 struct ldlm_resource *res;
328 struct ptlrpc_request *req;
329 int rc, size = sizeof(*body);
332 lock = ldlm_handle2lock(lockh);
338 connh = lock->l_connh;
341 return ldlm_cli_convert_local(lock, new_mode, flags);
343 LDLM_DEBUG(lock, "client-side convert");
345 req = ptlrpc_prep_req2(connh, LDLM_CONVERT, 1, &size, NULL);
347 GOTO(out, rc = -ENOMEM);
349 body = lustre_msg_buf(req->rq_reqmsg, 0);
350 memcpy(&body->lock_handle1, &lock->l_remote_handle,
351 sizeof(body->lock_handle1));
353 body->lock_desc.l_req_mode = new_mode;
354 body->lock_flags = *flags;
356 size = sizeof(*reply);
357 req->rq_replen = lustre_msg_size(1, &size);
359 rc = ptlrpc_queue_wait(req);
360 rc = ptlrpc_check_status(req, rc);
364 reply = lustre_msg_buf(req->rq_repmsg, 0);
365 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
367 ldlm_reprocess_all(res);
368 /* Go to sleep until the lock is granted. */
369 /* FIXME: or cancelled. */
370 if (lock->l_completion_ast)
371 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
375 ptlrpc_free_req(req);
379 int ldlm_cli_cancel(struct lustre_handle *lockh)
381 struct ptlrpc_request *req;
382 struct ldlm_lock *lock;
383 struct ldlm_request *body;
384 int rc = 0, size = sizeof(*body);
387 lock = ldlm_handle2lock(lockh);
389 /* It's possible that the decref that we did just before this
390 * cancel was the last reader/writer, and caused a cancel before
391 * we could call this function. If we want to make this
392 * impossible (by adding a dec_and_cancel() or similar), then
393 * we can put the LBUG back. */
399 LDLM_DEBUG(lock, "client-side cancel");
400 req = ptlrpc_prep_req2(lock->l_connh, LDLM_CANCEL, 1, &size, NULL);
402 GOTO(out, rc = -ENOMEM);
404 body = lustre_msg_buf(req->rq_reqmsg, 0);
405 memcpy(&body->lock_handle1, &lock->l_remote_handle,
406 sizeof(body->lock_handle1));
408 req->rq_replen = lustre_msg_size(0, NULL);
410 rc = ptlrpc_queue_wait(req);
411 rc = ptlrpc_check_status(req, rc);
412 ptlrpc_free_req(req);
416 ldlm_lock_cancel(lock);
418 LDLM_DEBUG(lock, "client-side local cancel");
419 if (lock->l_resource->lr_namespace->ns_client) {
420 CERROR("Trying to cancel local lock\n");
423 ldlm_lock_cancel(lock);
424 ldlm_reprocess_all(lock->l_resource);
425 LDLM_DEBUG(lock, "client-side local cancel handler END");