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);
52 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
54 struct l_wait_info lwi =
55 LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_completion_wait,
56 interrupted_completion_wait, lock);
60 if (flags == LDLM_FL_WAIT_NOREPROC)
64 wake_up(&lock->l_waitq);
68 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
72 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
75 ldlm_reprocess_all(lock->l_resource);
78 /* Go to sleep until the lock is granted or cancelled. */
79 rc = l_wait_event(lock->l_waitq,
80 ((lock->l_req_mode == lock->l_granted_mode) ||
81 (lock->l_flags & LDLM_FL_DESTROYED)), &lwi);
83 if (lock->l_flags & LDLM_FL_DESTROYED) {
84 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
89 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
94 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
98 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
99 struct lustre_handle *parent_lockh,
102 void *cookie, int cookielen,
105 ldlm_completion_callback completion,
106 ldlm_blocking_callback blocking,
109 struct lustre_handle *lockh)
111 struct ldlm_lock *lock;
116 CERROR("Trying to enqueue local lock in a shadow namespace\n");
120 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data,
123 GOTO(out_nolock, err = -ENOMEM);
124 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
126 ldlm_lock_addref_internal(lock, mode);
127 ldlm_lock2handle(lock, lockh);
128 lock->l_connh = NULL;
130 err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
135 if (type == LDLM_EXTENT)
136 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
137 if ((*flags) & LDLM_FL_LOCK_CHANGED)
138 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
140 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
143 if (lock->l_completion_ast)
144 lock->l_completion_ast(lock, *flags);
146 LDLM_DEBUG(lock, "client-side local enqueue END");
154 int ldlm_cli_enqueue(struct lustre_handle *connh,
155 struct ptlrpc_request *req,
156 struct ldlm_namespace *ns,
157 struct lustre_handle *parent_lock_handle,
160 void *cookie, int cookielen,
163 ldlm_completion_callback completion,
164 ldlm_blocking_callback blocking,
167 struct lustre_handle *lockh)
169 struct ldlm_lock *lock;
170 struct ldlm_request *body;
171 struct ldlm_reply *reply;
172 int rc, size = sizeof(*body), req_passed_in = 1;
176 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
177 type, cookie, cookielen, mode,
178 flags, completion, blocking, data,
182 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
185 GOTO(out_nolock, rc = -ENOMEM);
186 LDLM_DEBUG(lock, "client-side enqueue START");
187 /* for the local lock, add the reference */
188 ldlm_lock_addref_internal(lock, mode);
189 ldlm_lock2handle(lock, lockh);
192 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
195 GOTO(out, rc = -ENOMEM);
197 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
200 /* Dump all of this data into the request buffer */
201 body = lustre_msg_buf(req->rq_reqmsg, 0);
202 ldlm_lock2desc(lock, &body->lock_desc);
203 /* Phil: make this part of ldlm_lock2desc */
204 if (type == LDLM_EXTENT) {
205 memcpy(&body->lock_desc.l_extent, cookie,
206 sizeof(body->lock_desc.l_extent));
207 CDEBUG(D_INFO, "extent in body: "LPU64" -> "LPU64"\n",
208 body->lock_desc.l_extent.start,
209 body->lock_desc.l_extent.end);
211 body->lock_flags = *flags;
213 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
214 if (parent_lock_handle)
215 memcpy(&body->lock_handle2, parent_lock_handle,
216 sizeof(body->lock_handle2));
218 /* Continue as normal. */
219 if (!req_passed_in) {
220 size = sizeof(*reply);
221 req->rq_replen = lustre_msg_size(1, &size);
223 lock->l_connh = connh;
224 lock->l_export = NULL;
226 rc = ptlrpc_queue_wait(req);
227 /* FIXME: status check here? */
228 rc = ptlrpc_check_status(req, rc);
230 if (rc != ELDLM_OK) {
231 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
232 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
233 ldlm_lock_decref(lockh, mode);
234 /* FIXME: if we've already received a completion AST, this will
236 ldlm_lock_destroy(lock);
240 reply = lustre_msg_buf(req->rq_repmsg, 0);
241 memcpy(&lock->l_remote_handle, &reply->lock_handle,
242 sizeof(lock->l_remote_handle));
243 if (type == LDLM_EXTENT)
244 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
245 *flags = reply->lock_flags;
247 CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
248 (void *)(unsigned long)reply->lock_handle.addr, *flags);
249 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got extent "
250 LPU64" -> "LPU64"\n",
251 body->lock_desc.l_extent.start, body->lock_desc.l_extent.end,
252 reply->lock_extent.start, reply->lock_extent.end);
254 /* If enqueue returned a blocked lock but the completion handler has
255 * already run, then it fixed up the resource and we don't need to do it
257 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
258 int newmode = reply->lock_mode;
259 if (newmode && newmode != lock->l_req_mode) {
260 LDLM_DEBUG(lock, "server returned different mode %s",
261 ldlm_lockname[newmode]);
262 lock->l_req_mode = newmode;
265 if (reply->lock_resource_name[0] !=
266 lock->l_resource->lr_name[0]) {
267 CDEBUG(D_INFO, "remote intent success, locking %ld "
269 (long)reply->lock_resource_name[0],
270 (long)lock->l_resource->lr_name[0]);
272 ldlm_lock_change_resource(lock,
273 reply->lock_resource_name);
274 if (lock->l_resource == NULL) {
278 LDLM_DEBUG(lock, "client-side enqueue, new resource");
283 ptlrpc_req_finished(req);
285 rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
287 if (lock->l_completion_ast)
288 lock->l_completion_ast(lock, *flags);
290 LDLM_DEBUG(lock, "client-side enqueue END");
298 int ldlm_match_or_enqueue(struct lustre_handle *connh,
299 struct ptlrpc_request *req,
300 struct ldlm_namespace *ns,
301 struct lustre_handle *parent_lock_handle,
304 void *cookie, int cookielen,
307 ldlm_completion_callback completion,
308 ldlm_blocking_callback blocking,
311 struct lustre_handle *lockh)
315 rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
317 rc = ldlm_cli_enqueue(connh, req, ns,
318 parent_lock_handle, res_id, type, cookie,
319 cookielen, mode, flags, completion,
320 blocking, data, data_len, lockh);
322 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
328 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
332 if (lock->l_resource->lr_namespace->ns_client) {
333 CERROR("Trying to cancel local lock\n");
336 LDLM_DEBUG(lock, "client-side local convert");
338 ldlm_lock_convert(lock, new_mode, flags);
339 ldlm_reprocess_all(lock->l_resource);
341 LDLM_DEBUG(lock, "client-side local convert handler END");
346 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
347 * conversion of locks which are on the waiting or converting queue */
348 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
350 struct ldlm_request *body;
351 struct lustre_handle *connh;
352 struct ldlm_reply *reply;
353 struct ldlm_lock *lock;
354 struct ldlm_resource *res;
355 struct ptlrpc_request *req;
356 int rc, size = sizeof(*body);
359 lock = ldlm_handle2lock(lockh);
365 connh = lock->l_connh;
368 return ldlm_cli_convert_local(lock, new_mode, flags);
370 LDLM_DEBUG(lock, "client-side convert");
372 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
375 GOTO(out, rc = -ENOMEM);
377 body = lustre_msg_buf(req->rq_reqmsg, 0);
378 memcpy(&body->lock_handle1, &lock->l_remote_handle,
379 sizeof(body->lock_handle1));
381 body->lock_desc.l_req_mode = new_mode;
382 body->lock_flags = *flags;
384 size = sizeof(*reply);
385 req->rq_replen = lustre_msg_size(1, &size);
387 rc = ptlrpc_queue_wait(req);
388 rc = ptlrpc_check_status(req, rc);
392 reply = lustre_msg_buf(req->rq_repmsg, 0);
393 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
395 ldlm_reprocess_all(res);
396 /* Go to sleep until the lock is granted. */
397 /* FIXME: or cancelled. */
398 if (lock->l_completion_ast)
399 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
403 ptlrpc_req_finished(req);
407 int ldlm_cli_cancel(struct lustre_handle *lockh)
409 struct ptlrpc_request *req;
410 struct ldlm_lock *lock;
411 struct ldlm_request *body;
412 int rc = 0, size = sizeof(*body);
415 lock = ldlm_handle2lock(lockh);
417 /* It's possible that the decref that we did just before this
418 * cancel was the last reader/writer, and caused a cancel before
419 * we could call this function. If we want to make this
420 * impossible (by adding a dec_and_cancel() or similar), then
421 * we can put the LBUG back. */
427 LDLM_DEBUG(lock, "client-side cancel");
428 /* Set this flag to prevent others from getting new references*/
429 l_lock(&ldlm_everything_lock);
430 lock->l_flags |= LDLM_FL_CBPENDING;
431 ldlm_cancel_callback(lock);
432 l_unlock(&ldlm_everything_lock);
434 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
435 LDLM_CANCEL, 1, &size, NULL);
437 GOTO(out, rc = -ENOMEM);
439 body = lustre_msg_buf(req->rq_reqmsg, 0);
440 memcpy(&body->lock_handle1, &lock->l_remote_handle,
441 sizeof(body->lock_handle1));
443 req->rq_replen = lustre_msg_size(0, NULL);
445 rc = ptlrpc_queue_wait(req);
446 rc = ptlrpc_check_status(req, rc);
447 ptlrpc_req_finished(req);
451 ldlm_lock_cancel(lock);
453 LDLM_DEBUG(lock, "client-side local cancel");
454 if (lock->l_resource->lr_namespace->ns_client) {
455 CERROR("Trying to cancel local lock\n");
458 ldlm_lock_cancel(lock);
459 ldlm_reprocess_all(lock->l_resource);
460 LDLM_DEBUG(lock, "client-side local cancel handler END");
469 /* Cancel all locks on a given resource that have 0 readers/writers.
471 * If 'local_only' is true, throw the locks away without trying to notify the
473 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
476 struct ldlm_resource *res;
477 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
478 struct ldlm_ast_work *w;
481 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
483 /* This is not a problem. */
484 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id[0]);
488 l_lock(&ldlm_everything_lock);
489 list_for_each(tmp, &res->lr_granted) {
490 struct ldlm_lock *lock;
491 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
493 if (lock->l_readers || lock->l_writers)
496 /* Setting the CBPENDING flag is a little misleading, but
497 * prevents an important race; namely, once CBPENDING is set,
498 * the lock can accumulate no more readers/writers. Since
499 * readers and writers are already zero here, ldlm_lock_decref
500 * won't see this flag and call l_blocking_ast */
501 lock->l_flags |= LDLM_FL_CBPENDING;
503 OBD_ALLOC(w, sizeof(*w));
506 w->w_lock = LDLM_LOCK_GET(lock);
507 list_add(&w->w_list, &list);
509 l_unlock(&ldlm_everything_lock);
511 list_for_each_safe(tmp, next, &list) {
512 struct lustre_handle lockh;
514 w = list_entry(tmp, struct ldlm_ast_work, w_list);
517 ldlm_lock_cancel(w->w_lock);
519 ldlm_lock2handle(w->w_lock, &lockh);
520 rc = ldlm_cli_cancel(&lockh);
522 CERROR("ldlm_cli_cancel: %d\n", rc);
524 LDLM_LOCK_PUT(w->w_lock);
525 list_del(&w->w_list);
526 OBD_FREE(w, sizeof(*w));
529 ldlm_resource_put(res);