1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LDLM
27 #include <linux/lustre_dlm.h>
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_lib.h>
31 #include <portals/list.h>
33 #include <liblustre.h>
36 #include "ldlm_internal.h"
38 #define l_flock_waitq l_lru
40 static struct list_head ldlm_flock_waitq = LIST_HEAD_INIT(ldlm_flock_waitq);
42 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
43 void *data, int flag);
46 * list_for_remaining_safe - iterate over the remaining entries in a list
47 * and safeguard against removal of a list entry.
48 * @pos: the &struct list_head to use as a loop counter. pos MUST
49 * have been initialized prior to using it in this macro.
50 * @n: another &struct list_head to use as temporary storage
51 * @head: the head for your list.
53 #define list_for_remaining_safe(pos, n, head) \
54 for (n = pos->next; pos != (head); pos = n, n = pos->next)
57 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
59 return((new->l_policy_data.l_flock.pid ==
60 lock->l_policy_data.l_flock.pid) &&
61 (new->l_export == lock->l_export));
65 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
67 return((new->l_policy_data.l_flock.start <=
68 lock->l_policy_data.l_flock.end) &&
69 (new->l_policy_data.l_flock.end >=
70 lock->l_policy_data.l_flock.start));
74 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
78 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
81 LASSERT(list_empty(&lock->l_flock_waitq));
83 list_del_init(&lock->l_res_link);
84 if (flags == LDLM_FL_WAIT_NOREPROC) {
85 /* client side - set a flag to prevent sending a CANCEL */
86 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
87 ldlm_lock_decref_internal(lock, mode);
90 ldlm_lock_destroy(lock);
95 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
97 struct obd_export *req_export = req->l_export;
98 struct obd_export *blocking_export = blocking_lock->l_export;
99 pid_t req_pid = req->l_policy_data.l_flock.pid;
100 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
101 struct ldlm_lock *lock;
104 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
105 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
106 (lock->l_export != blocking_export))
109 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
110 blocking_export = (struct obd_export *)(long)
111 lock->l_policy_data.l_flock.blocking_export;
112 if (blocking_pid == req_pid && blocking_export == req_export)
122 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
125 struct ldlm_resource *res = req->l_resource;
126 struct ldlm_namespace *ns = res->lr_namespace;
127 struct list_head *tmp;
128 struct list_head *ownlocks = NULL;
129 struct ldlm_lock *lock = NULL;
130 struct ldlm_lock *new = req;
131 struct ldlm_lock *new2 = NULL;
132 ldlm_mode_t mode = req->l_req_mode;
133 int local = ns->ns_client;
134 int added = (mode == LCK_NL);
138 CDEBUG(D_DLMTRACE, "flags %#x pid "LPU64" mode %u start "LPU64" end "
139 LPU64"\n", *flags, new->l_policy_data.l_flock.pid, mode,
140 req->l_policy_data.l_flock.start,
141 req->l_policy_data.l_flock.end);
146 /* No blocking ASTs are sent to the clients for
147 * Posix file & record locks */
148 req->l_blocking_ast = NULL;
150 /* Called on the server for lock cancels. */
151 req->l_blocking_ast = ldlm_flock_blocking_ast;
154 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
155 /* This loop determines where this processes locks start
156 * in the resource lr_granted list. */
157 list_for_each(tmp, &res->lr_granted) {
158 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
159 if (ldlm_same_flock_owner(lock, req)) {
165 lockmode_verify(mode);
167 /* This loop determines if there are existing locks
168 * that conflict with the new lock request. */
169 list_for_each(tmp, &res->lr_granted) {
170 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
172 if (ldlm_same_flock_owner(lock, req)) {
178 /* locks are compatible, overlap doesn't matter */
179 if (lockmode_compat(lock->l_granted_mode, mode))
182 if (!ldlm_flocks_overlap(lock, req))
186 RETURN(LDLM_ITER_CONTINUE);
188 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
189 ldlm_flock_destroy(req, mode, *flags);
191 RETURN(LDLM_ITER_STOP);
194 if (*flags & LDLM_FL_TEST_LOCK) {
195 ldlm_flock_destroy(req, mode, *flags);
196 req->l_req_mode = lock->l_granted_mode;
197 req->l_policy_data.l_flock.pid =
198 lock->l_policy_data.l_flock.pid;
199 req->l_policy_data.l_flock.start =
200 lock->l_policy_data.l_flock.start;
201 req->l_policy_data.l_flock.end =
202 lock->l_policy_data.l_flock.end;
203 *flags |= LDLM_FL_LOCK_CHANGED;
204 RETURN(LDLM_ITER_STOP);
207 if (ldlm_flock_deadlock(req, lock)) {
208 ldlm_flock_destroy(req, mode, *flags);
210 RETURN(LDLM_ITER_STOP);
213 req->l_policy_data.l_flock.blocking_pid =
214 lock->l_policy_data.l_flock.pid;
215 req->l_policy_data.l_flock.blocking_export =
216 (long)(void *)lock->l_export;
218 LASSERT(list_empty(&req->l_flock_waitq));
219 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
221 ldlm_resource_add_lock(res, &res->lr_waiting, req);
222 *flags |= LDLM_FL_BLOCK_GRANTED;
223 RETURN(LDLM_ITER_STOP);
227 if (*flags & LDLM_FL_TEST_LOCK) {
228 ldlm_flock_destroy(req, mode, *flags);
229 req->l_req_mode = LCK_NL;
230 *flags |= LDLM_FL_LOCK_CHANGED;
231 RETURN(LDLM_ITER_STOP);
234 /* In case we had slept on this lock request take it off of the
235 * deadlock detection waitq. */
236 list_del_init(&req->l_flock_waitq);
238 /* Scan the locks owned by this process that overlap this request.
239 * We may have to merge or split existing locks. */
242 ownlocks = &res->lr_granted;
244 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
245 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
247 if (!ldlm_same_flock_owner(lock, new))
250 if (lock->l_granted_mode == mode) {
251 /* If the modes are the same then we need to process
252 * locks that overlap OR adjoin the new lock. The extra
253 * logic condition is necessary to deal with arithmetic
254 * overflow and underflow. */
255 if ((new->l_policy_data.l_flock.start >
256 (lock->l_policy_data.l_flock.end + 1))
257 && (lock->l_policy_data.l_flock.end != ~0))
260 if ((new->l_policy_data.l_flock.end <
261 (lock->l_policy_data.l_flock.start - 1))
262 && (lock->l_policy_data.l_flock.start != 0))
265 if (new->l_policy_data.l_flock.start <
266 lock->l_policy_data.l_flock.start) {
267 lock->l_policy_data.l_flock.start =
268 new->l_policy_data.l_flock.start;
270 new->l_policy_data.l_flock.start =
271 lock->l_policy_data.l_flock.start;
274 if (new->l_policy_data.l_flock.end >
275 lock->l_policy_data.l_flock.end) {
276 lock->l_policy_data.l_flock.end =
277 new->l_policy_data.l_flock.end;
279 new->l_policy_data.l_flock.end =
280 lock->l_policy_data.l_flock.end;
284 ldlm_flock_destroy(lock, mode, *flags);
292 if (new->l_policy_data.l_flock.start >
293 lock->l_policy_data.l_flock.end)
296 if (new->l_policy_data.l_flock.end <
297 lock->l_policy_data.l_flock.start)
302 if (new->l_policy_data.l_flock.start <=
303 lock->l_policy_data.l_flock.start) {
304 if (new->l_policy_data.l_flock.end <
305 lock->l_policy_data.l_flock.end) {
306 lock->l_policy_data.l_flock.start =
307 new->l_policy_data.l_flock.end + 1;
310 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
313 if (new->l_policy_data.l_flock.end >=
314 lock->l_policy_data.l_flock.end) {
315 lock->l_policy_data.l_flock.end =
316 new->l_policy_data.l_flock.start - 1;
320 /* split the existing lock into two locks */
322 /* if this is an F_UNLCK operation then we could avoid
323 * allocating a new lock and use the req lock passed in
324 * with the request but this would complicate the reply
325 * processing since updates to req get reflected in the
326 * reply. The client side replays the lock request so
327 * it must see the original lock data in the reply. */
329 /* XXX - if ldlm_lock_new() can sleep we should
330 * release the ns_lock, allocate the new lock,
331 * and restart processing this lock. */
332 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
333 lock->l_granted_mode, NULL, NULL, NULL,
336 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
338 RETURN(LDLM_ITER_STOP);
341 new2->l_granted_mode = lock->l_granted_mode;
342 new2->l_policy_data.l_flock.pid =
343 new->l_policy_data.l_flock.pid;
344 new2->l_policy_data.l_flock.start =
345 lock->l_policy_data.l_flock.start;
346 new2->l_policy_data.l_flock.end =
347 new->l_policy_data.l_flock.start - 1;
348 lock->l_policy_data.l_flock.start =
349 new->l_policy_data.l_flock.end + 1;
350 new2->l_conn_export = lock->l_conn_export;
351 if (lock->l_export != NULL) {
352 new2->l_export = class_export_get(lock->l_export);
353 list_add(&new2->l_export_chain,
354 &new2->l_export->exp_ldlm_data.led_held_locks);
356 if (*flags == LDLM_FL_WAIT_NOREPROC)
357 ldlm_lock_addref_internal(new2, lock->l_granted_mode);
359 /* insert new2 at lock */
360 ldlm_resource_add_lock(res, ownlocks, new2);
365 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
367 req->l_granted_mode = req->l_req_mode;
368 list_del_init(&req->l_res_link);
369 /* insert new lock before ownlocks in list. */
370 ldlm_resource_add_lock(res, ownlocks, req);
373 if (*flags != LDLM_FL_WAIT_NOREPROC) {
375 /* If this is an unlock, reprocess the waitq and
376 * send completions ASTs for locks that can now be
377 * granted. The only problem with doing this
378 * reprocessing here is that the completion ASTs for
379 * newly granted locks will be sent before the unlock
380 * completion is sent. It shouldn't be an issue. Also
381 * note that ldlm_process_flock_lock() will recurse,
382 * but only once because first_enq will be false from
383 * ldlm_reprocess_queue. */
384 if ((mode == LCK_NL) && overlaps) {
385 struct list_head rpc_list
386 = LIST_HEAD_INIT(rpc_list);
389 res->lr_tmp = &rpc_list;
390 ldlm_reprocess_queue(res, &res->lr_waiting);
393 l_unlock(&ns->ns_lock);
394 rc = ldlm_run_ast_work(res->lr_namespace,
396 l_lock(&ns->ns_lock);
398 GOTO(restart, -ERESTART);
401 LASSERT(req->l_completion_ast);
402 ldlm_add_ast_work_item(req, NULL, NULL, 0);
406 /* In case we're reprocessing the requested lock we can't destroy
407 * it until after calling ldlm_ast_work_item() above so that lawi()
408 * can bump the reference count on req. Otherwise req could be freed
409 * before the completion AST can be sent. */
411 ldlm_flock_destroy(req, mode, *flags);
413 ldlm_resource_dump(res);
414 RETURN(LDLM_ITER_CONTINUE);
417 struct ldlm_flock_wait_data {
418 struct ldlm_lock *fwd_lock;
423 ldlm_flock_interrupted_wait(void *data)
425 struct ldlm_lock *lock;
426 struct lustre_handle lockh;
430 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
432 /* take lock off the deadlock detection waitq. */
433 list_del_init(&lock->l_flock_waitq);
435 ldlm_lock_decref_internal(lock, lock->l_req_mode);
436 ldlm_lock2handle(lock, &lockh);
437 rc = ldlm_cli_cancel(&lockh);
442 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
444 struct ldlm_namespace *ns;
445 struct file_lock *getlk = lock->l_ast_data;
446 struct ldlm_flock_wait_data fwd;
447 unsigned long irqflags;
448 struct obd_device *obd;
449 struct obd_import *imp = NULL;
452 struct l_wait_info lwi;
455 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
458 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
461 wake_up(&lock->l_waitq);
465 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
466 LDLM_FL_BLOCK_CONV)))
469 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
472 ldlm_lock_dump(D_DLMTRACE, lock, 0);
475 obd = class_exp2obd(lock->l_conn_export);
477 /* if this is a local lock, then there is no import */
479 imp = obd->u.cli.cl_import;
482 spin_lock_irqsave(&imp->imp_lock, irqflags);
483 fwd.fwd_generation = imp->imp_generation;
484 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
487 lwi = LWI_TIMEOUT_INTR(0,NULL,ldlm_flock_interrupted_wait,&fwd);
489 /* Go to sleep until the lock is granted. */
490 rc = l_wait_event(lock->l_waitq,
491 ((lock->l_req_mode == lock->l_granted_mode) ||
492 lock->l_destroyed), &lwi);
495 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
500 LASSERT(!(lock->l_destroyed));
504 LDLM_DEBUG(lock, "client-side enqueue waking up");
505 ns = lock->l_resource->lr_namespace;
506 l_lock(&ns->ns_lock);
508 /* take lock off the deadlock detection waitq. */
509 list_del_init(&lock->l_flock_waitq);
511 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
512 list_del_init(&lock->l_res_link);
514 if (flags & LDLM_FL_TEST_LOCK) {
515 /* fcntl(F_GETLK) request */
516 /* The old mode was saved in getlk->fl_type so that if the mode
517 * in the lock changes we can decref the approprate refcount. */
518 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
519 switch (lock->l_granted_mode) {
521 getlk->fl_type = F_RDLCK;
524 getlk->fl_type = F_WRLCK;
527 getlk->fl_type = F_UNLCK;
529 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
530 getlk->fl_start = lock->l_policy_data.l_flock.start;
531 getlk->fl_end = lock->l_policy_data.l_flock.end;
533 /* We need to reprocess the lock to do merges or splits
534 * with existing locks owned by this process. */
535 flags = LDLM_FL_WAIT_NOREPROC;
536 ldlm_process_flock_lock(lock, &flags, 1, &err);
538 l_unlock(&ns->ns_lock);
542 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
543 void *data, int flag)
545 struct ldlm_namespace *ns;
549 LASSERT(flag == LDLM_CB_CANCELING);
551 ns = lock->l_resource->lr_namespace;
553 /* take lock off the deadlock detection waitq. */
554 l_lock(&ns->ns_lock);
555 list_del_init(&lock->l_flock_waitq);
556 l_unlock(&ns->ns_lock);