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 file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_LDLM
24 #include <linux/lustre_dlm.h>
25 #include <linux/obd_class.h>
26 #include <linux/obd.h>
28 static int interrupted_completion_wait(void *data)
33 static int expired_completion_wait(void *data)
35 struct ldlm_lock *lock = data;
36 struct ptlrpc_connection *conn;
37 struct obd_device *obd;
40 CERROR("NULL lock\n");
41 else if (!lock->l_connh)
42 CERROR("lock %p has NULL connh\n", lock);
43 else if (!(obd = class_conn2obd(lock->l_connh)))
44 CERROR("lock %p has NULL obd\n", lock);
45 else if (!(conn = obd->u.cli.cl_import.imp_connection))
46 CERROR("lock %p has NULL connection\n", lock);
48 class_signal_connection_failure(conn);
54 static int expired_completion_wait(void *data)
56 struct ldlm_lock *lock = data;
57 struct ptlrpc_connection *conn =
58 class_conn2cliimp(lock->l_connh)->imp_connection;
61 CERROR("lock %p has NULL import connection\n", lock);
65 class_signal_connection_failure(conn);
70 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
72 struct l_wait_info lwi =
73 LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_completion_wait,
74 interrupted_completion_wait, lock);
78 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
79 LDLM_FL_BLOCK_CONV)) {
80 /* Go to sleep until the lock is granted. */
81 /* FIXME: or cancelled. */
82 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock,"
85 ldlm_reprocess_all(lock->l_resource);
86 rc = l_wait_event(lock->l_waitq,
87 (lock->l_req_mode == lock->l_granted_mode),
91 "client-side enqueue waking up: failed (%d)",
95 "client-side enqueue waking up: granted");
97 } else if (flags == LDLM_FL_WAIT_NOREPROC) {
98 rc = l_wait_event(lock->l_waitq,
99 (lock->l_req_mode == lock->l_granted_mode),
101 } else if (flags == 0) {
102 wake_up(&lock->l_waitq);
108 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
109 struct lustre_handle *parent_lockh,
112 void *cookie, int cookielen,
115 ldlm_completion_callback completion,
116 ldlm_blocking_callback blocking,
119 struct lustre_handle *lockh)
121 struct ldlm_lock *lock;
126 CERROR("Trying to enqueue local lock in a shadow namespace\n");
130 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data,
133 GOTO(out_nolock, err = -ENOMEM);
134 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
136 ldlm_lock_addref_internal(lock, mode);
137 ldlm_lock2handle(lock, lockh);
138 lock->l_connh = NULL;
140 err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
145 if (type == LDLM_EXTENT)
146 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
147 if ((*flags) & LDLM_FL_LOCK_CHANGED)
148 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
150 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
153 if (lock->l_completion_ast)
154 lock->l_completion_ast(lock, *flags);
156 LDLM_DEBUG(lock, "client-side local enqueue END");
164 int ldlm_cli_enqueue(struct lustre_handle *connh,
165 struct ptlrpc_request *req,
166 struct ldlm_namespace *ns,
167 struct lustre_handle *parent_lock_handle,
170 void *cookie, int cookielen,
173 ldlm_completion_callback completion,
174 ldlm_blocking_callback blocking,
177 struct lustre_handle *lockh)
179 struct ldlm_lock *lock;
180 struct ldlm_request *body;
181 struct ldlm_reply *reply;
182 int rc, size = sizeof(*body), req_passed_in = 1;
186 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
187 type, cookie, cookielen, mode,
188 flags, completion, blocking, data,
192 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
195 GOTO(out_nolock, rc = -ENOMEM);
196 LDLM_DEBUG(lock, "client-side enqueue START");
197 /* for the local lock, add the reference */
198 ldlm_lock_addref_internal(lock, mode);
199 ldlm_lock2handle(lock, lockh);
202 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
205 GOTO(out, rc = -ENOMEM);
207 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
210 /* Dump all of this data into the request buffer */
211 body = lustre_msg_buf(req->rq_reqmsg, 0);
212 ldlm_lock2desc(lock, &body->lock_desc);
213 /* Phil: make this part of ldlm_lock2desc */
214 if (type == LDLM_EXTENT)
215 memcpy(&body->lock_desc.l_extent, cookie,
216 sizeof(body->lock_desc.l_extent));
217 body->lock_flags = *flags;
219 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
220 if (parent_lock_handle)
221 memcpy(&body->lock_handle2, parent_lock_handle,
222 sizeof(body->lock_handle2));
224 /* Continue as normal. */
225 if (!req_passed_in) {
226 size = sizeof(*reply);
227 req->rq_replen = lustre_msg_size(1, &size);
229 lock->l_connh = connh;
230 lock->l_export = NULL;
232 rc = ptlrpc_queue_wait(req);
233 /* FIXME: status check here? */
234 rc = ptlrpc_check_status(req, rc);
236 if (rc != ELDLM_OK) {
237 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
238 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
239 ldlm_lock_decref(lockh, mode);
240 /* FIXME: if we've already received a completion AST, this will
242 ldlm_lock_destroy(lock);
246 reply = lustre_msg_buf(req->rq_repmsg, 0);
247 memcpy(&lock->l_remote_handle, &reply->lock_handle,
248 sizeof(lock->l_remote_handle));
249 if (type == LDLM_EXTENT)
250 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
251 *flags = reply->lock_flags;
253 CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
254 (void *)(unsigned long)reply->lock_handle.addr, *flags);
255 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got extent "
256 LPU64" -> "LPU64"\n",
257 body->lock_desc.l_extent.start, body->lock_desc.l_extent.end,
258 reply->lock_extent.start, reply->lock_extent.end);
260 /* If enqueue returned a blocked lock but the completion handler has
261 * already run, then it fixed up the resource and we don't need to do it
263 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
264 int newmode = reply->lock_mode;
265 if (newmode && newmode != lock->l_req_mode) {
266 LDLM_DEBUG(lock, "server returned different mode %s",
267 ldlm_lockname[newmode]);
268 lock->l_req_mode = newmode;
271 if (reply->lock_resource_name[0] !=
272 lock->l_resource->lr_name[0]) {
273 CDEBUG(D_INFO, "remote intent success, locking %ld "
275 (long)reply->lock_resource_name[0],
276 (long)lock->l_resource->lr_name[0]);
278 ldlm_lock_change_resource(lock,
279 reply->lock_resource_name);
280 if (lock->l_resource == NULL) {
284 LDLM_DEBUG(lock, "client-side enqueue, new resource");
289 ptlrpc_free_req(req);
291 rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
293 if (lock->l_completion_ast)
294 lock->l_completion_ast(lock, *flags);
296 LDLM_DEBUG(lock, "client-side enqueue END");
304 int ldlm_match_or_enqueue(struct lustre_handle *connh,
305 struct ptlrpc_request *req,
306 struct ldlm_namespace *ns,
307 struct lustre_handle *parent_lock_handle,
310 void *cookie, int cookielen,
313 ldlm_completion_callback completion,
314 ldlm_blocking_callback blocking,
317 struct lustre_handle *lockh)
321 rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
323 rc = ldlm_cli_enqueue(connh, req, ns,
324 parent_lock_handle, res_id, type, cookie,
325 cookielen, mode, flags, completion,
326 blocking, data, data_len, lockh);
328 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
334 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
338 if (lock->l_resource->lr_namespace->ns_client) {
339 CERROR("Trying to cancel local lock\n");
342 LDLM_DEBUG(lock, "client-side local convert");
344 ldlm_lock_convert(lock, new_mode, flags);
345 ldlm_reprocess_all(lock->l_resource);
347 LDLM_DEBUG(lock, "client-side local convert handler END");
352 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
353 * conversion of locks which are on the waiting or converting queue */
354 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
356 struct ldlm_request *body;
357 struct lustre_handle *connh;
358 struct ldlm_reply *reply;
359 struct ldlm_lock *lock;
360 struct ldlm_resource *res;
361 struct ptlrpc_request *req;
362 int rc, size = sizeof(*body);
365 lock = ldlm_handle2lock(lockh);
371 connh = lock->l_connh;
374 return ldlm_cli_convert_local(lock, new_mode, flags);
376 LDLM_DEBUG(lock, "client-side convert");
378 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
381 GOTO(out, rc = -ENOMEM);
383 body = lustre_msg_buf(req->rq_reqmsg, 0);
384 memcpy(&body->lock_handle1, &lock->l_remote_handle,
385 sizeof(body->lock_handle1));
387 body->lock_desc.l_req_mode = new_mode;
388 body->lock_flags = *flags;
390 size = sizeof(*reply);
391 req->rq_replen = lustre_msg_size(1, &size);
393 rc = ptlrpc_queue_wait(req);
394 rc = ptlrpc_check_status(req, rc);
398 reply = lustre_msg_buf(req->rq_repmsg, 0);
399 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
401 ldlm_reprocess_all(res);
402 /* Go to sleep until the lock is granted. */
403 /* FIXME: or cancelled. */
404 if (lock->l_completion_ast)
405 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
409 ptlrpc_free_req(req);
413 int ldlm_cli_cancel(struct lustre_handle *lockh)
415 struct ptlrpc_request *req;
416 struct ldlm_lock *lock;
417 struct ldlm_request *body;
418 int rc = 0, size = sizeof(*body);
421 lock = ldlm_handle2lock(lockh);
423 /* It's possible that the decref that we did just before this
424 * cancel was the last reader/writer, and caused a cancel before
425 * we could call this function. If we want to make this
426 * impossible (by adding a dec_and_cancel() or similar), then
427 * we can put the LBUG back. */
433 LDLM_DEBUG(lock, "client-side cancel");
434 /* Set this flag to prevent others from getting new references*/
435 l_lock(&lock->l_resource->lr_namespace->ns_lock);
436 lock->l_flags |= LDLM_FL_CBPENDING;
437 ldlm_cancel_callback(lock);
438 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
440 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
441 LDLM_CANCEL, 1, &size, NULL);
443 GOTO(out, rc = -ENOMEM);
445 body = lustre_msg_buf(req->rq_reqmsg, 0);
446 memcpy(&body->lock_handle1, &lock->l_remote_handle,
447 sizeof(body->lock_handle1));
449 req->rq_replen = lustre_msg_size(0, NULL);
451 rc = ptlrpc_queue_wait(req);
452 rc = ptlrpc_check_status(req, rc);
453 ptlrpc_free_req(req);
457 ldlm_lock_cancel(lock);
459 LDLM_DEBUG(lock, "client-side local cancel");
460 if (lock->l_resource->lr_namespace->ns_client) {
461 CERROR("Trying to cancel local lock\n");
464 ldlm_lock_cancel(lock);
465 ldlm_reprocess_all(lock->l_resource);
466 LDLM_DEBUG(lock, "client-side local cancel handler END");
475 /* Cancel all locks on a given resource that have 0 readers/writers.
477 * If 'local_only' is true, throw the locks away without trying to notify the
479 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
482 struct ldlm_resource *res;
483 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
484 struct ldlm_ast_work *w;
487 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
489 /* This is not a problem. */
490 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id[0]);
494 l_lock(&ns->ns_lock);
495 list_for_each(tmp, &res->lr_granted) {
496 struct ldlm_lock *lock;
497 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
499 if (lock->l_readers || lock->l_writers)
502 /* Setting the CBPENDING flag is a little misleading, but
503 * prevents an important race; namely, once CBPENDING is set,
504 * the lock can accumulate no more readers/writers. Since
505 * readers and writers are already zero here, ldlm_lock_decref
506 * won't see this flag and call l_blocking_ast */
507 lock->l_flags |= LDLM_FL_CBPENDING;
509 OBD_ALLOC(w, sizeof(*w));
512 w->w_lock = LDLM_LOCK_GET(lock);
513 list_add(&w->w_list, &list);
515 l_unlock(&ns->ns_lock);
517 list_for_each_safe(tmp, next, &list) {
518 struct lustre_handle lockh;
520 w = list_entry(tmp, struct ldlm_ast_work, w_list);
523 ldlm_lock_cancel(w->w_lock);
525 ldlm_lock2handle(w->w_lock, &lockh);
526 rc = ldlm_cli_cancel(&lockh);
528 CERROR("ldlm_cli_cancel: %d\n", rc);
530 LDLM_LOCK_PUT(w->w_lock);
531 list_del(&w->w_list);
532 OBD_FREE(w, sizeof(*w));
535 ldlm_resource_put(res);