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 <libcfs/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));
82 list_del_init(&lock->l_res_link);
83 if (flags == LDLM_FL_WAIT_NOREPROC) {
84 /* client side - set a flag to prevent sending a CANCEL */
85 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
86 ldlm_lock_decref_internal(lock, mode);
89 ldlm_lock_destroy(lock);
94 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
96 struct obd_export *req_export = req->l_export;
97 struct obd_export *blocking_export = blocking_lock->l_export;
98 pid_t req_pid = req->l_policy_data.l_flock.pid;
99 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
100 struct ldlm_lock *lock;
103 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
104 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
105 (lock->l_export != blocking_export))
108 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
109 blocking_export = (struct obd_export *)(long)
110 lock->l_policy_data.l_flock.blocking_export;
111 if (blocking_pid == req_pid && blocking_export == req_export)
121 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
122 ldlm_error_t *err, struct list_head *work_list)
124 struct ldlm_resource *res = req->l_resource;
125 struct ldlm_namespace *ns = res->lr_namespace;
126 struct list_head *tmp;
127 struct list_head *ownlocks = NULL;
128 struct ldlm_lock *lock = NULL;
129 struct ldlm_lock *new = req;
130 struct ldlm_lock *new2 = NULL;
131 ldlm_mode_t mode = req->l_req_mode;
132 int local = ns->ns_client;
133 int added = (mode == LCK_NL);
137 CDEBUG(D_DLMTRACE, "flags %#x pid %u mode %u start "LPU64" end "
138 LPU64"\n", *flags, (unsigned int)new->l_policy_data.l_flock.pid,
139 mode, req->l_policy_data.l_flock.start,
140 req->l_policy_data.l_flock.end);
145 /* No blocking ASTs are sent to the clients for
146 * Posix file & record locks */
147 req->l_blocking_ast = NULL;
149 /* Called on the server for lock cancels. */
150 req->l_blocking_ast = ldlm_flock_blocking_ast;
153 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
154 /* This loop determines where this processes locks start
155 * in the resource lr_granted list. */
156 list_for_each(tmp, &res->lr_granted) {
157 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
158 if (ldlm_same_flock_owner(lock, req)) {
164 lockmode_verify(mode);
166 /* This loop determines if there are existing locks
167 * that conflict with the new lock request. */
168 list_for_each(tmp, &res->lr_granted) {
169 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
171 if (ldlm_same_flock_owner(lock, req)) {
177 /* locks are compatible, overlap doesn't matter */
178 if (lockmode_compat(lock->l_granted_mode, mode))
181 if (!ldlm_flocks_overlap(lock, req))
185 RETURN(LDLM_ITER_CONTINUE);
187 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
188 ldlm_flock_destroy(req, mode, *flags);
190 RETURN(LDLM_ITER_STOP);
193 if (*flags & LDLM_FL_TEST_LOCK) {
194 ldlm_flock_destroy(req, mode, *flags);
195 req->l_req_mode = lock->l_granted_mode;
196 req->l_policy_data.l_flock.pid =
197 lock->l_policy_data.l_flock.pid;
198 req->l_policy_data.l_flock.start =
199 lock->l_policy_data.l_flock.start;
200 req->l_policy_data.l_flock.end =
201 lock->l_policy_data.l_flock.end;
202 *flags |= LDLM_FL_LOCK_CHANGED;
203 RETURN(LDLM_ITER_STOP);
206 if (ldlm_flock_deadlock(req, lock)) {
207 ldlm_flock_destroy(req, mode, *flags);
209 RETURN(LDLM_ITER_STOP);
212 req->l_policy_data.l_flock.blocking_pid =
213 lock->l_policy_data.l_flock.pid;
214 req->l_policy_data.l_flock.blocking_export =
215 (long)(void *)lock->l_export;
217 LASSERT(list_empty(&req->l_flock_waitq));
218 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
220 ldlm_resource_add_lock(res, &res->lr_waiting, req);
221 *flags |= LDLM_FL_BLOCK_GRANTED;
222 RETURN(LDLM_ITER_STOP);
226 if (*flags & LDLM_FL_TEST_LOCK) {
227 ldlm_flock_destroy(req, mode, *flags);
228 req->l_req_mode = LCK_NL;
229 *flags |= LDLM_FL_LOCK_CHANGED;
230 RETURN(LDLM_ITER_STOP);
233 /* In case we had slept on this lock request take it off of the
234 * deadlock detection waitq. */
235 list_del_init(&req->l_flock_waitq);
237 /* Scan the locks owned by this process that overlap this request.
238 * We may have to merge or split existing locks. */
241 ownlocks = &res->lr_granted;
243 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
244 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
246 if (!ldlm_same_flock_owner(lock, new))
249 if (lock->l_granted_mode == mode) {
250 /* If the modes are the same then we need to process
251 * locks that overlap OR adjoin the new lock. The extra
252 * logic condition is necessary to deal with arithmetic
253 * overflow and underflow. */
254 if ((new->l_policy_data.l_flock.start >
255 (lock->l_policy_data.l_flock.end + 1))
256 && (lock->l_policy_data.l_flock.end != ~0))
259 if ((new->l_policy_data.l_flock.end <
260 (lock->l_policy_data.l_flock.start - 1))
261 && (lock->l_policy_data.l_flock.start != 0))
264 if (new->l_policy_data.l_flock.start <
265 lock->l_policy_data.l_flock.start) {
266 lock->l_policy_data.l_flock.start =
267 new->l_policy_data.l_flock.start;
269 new->l_policy_data.l_flock.start =
270 lock->l_policy_data.l_flock.start;
273 if (new->l_policy_data.l_flock.end >
274 lock->l_policy_data.l_flock.end) {
275 lock->l_policy_data.l_flock.end =
276 new->l_policy_data.l_flock.end;
278 new->l_policy_data.l_flock.end =
279 lock->l_policy_data.l_flock.end;
283 ldlm_flock_destroy(lock, mode, *flags);
291 if (new->l_policy_data.l_flock.start >
292 lock->l_policy_data.l_flock.end)
295 if (new->l_policy_data.l_flock.end <
296 lock->l_policy_data.l_flock.start)
301 if (new->l_policy_data.l_flock.start <=
302 lock->l_policy_data.l_flock.start) {
303 if (new->l_policy_data.l_flock.end <
304 lock->l_policy_data.l_flock.end) {
305 lock->l_policy_data.l_flock.start =
306 new->l_policy_data.l_flock.end + 1;
309 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
312 if (new->l_policy_data.l_flock.end >=
313 lock->l_policy_data.l_flock.end) {
314 lock->l_policy_data.l_flock.end =
315 new->l_policy_data.l_flock.start - 1;
319 /* split the existing lock into two locks */
321 /* if this is an F_UNLCK operation then we could avoid
322 * allocating a new lock and use the req lock passed in
323 * with the request but this would complicate the reply
324 * processing since updates to req get reflected in the
325 * reply. The client side replays the lock request so
326 * it must see the original lock data in the reply. */
328 /* XXX - if ldlm_lock_new() can sleep we should
329 * release the ns_lock, allocate the new lock,
330 * and restart processing this lock. */
331 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
332 lock->l_granted_mode, NULL, NULL, NULL,
335 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
337 RETURN(LDLM_ITER_STOP);
340 new2->l_granted_mode = lock->l_granted_mode;
341 new2->l_policy_data.l_flock.pid =
342 new->l_policy_data.l_flock.pid;
343 new2->l_policy_data.l_flock.start =
344 lock->l_policy_data.l_flock.start;
345 new2->l_policy_data.l_flock.end =
346 new->l_policy_data.l_flock.start - 1;
347 lock->l_policy_data.l_flock.start =
348 new->l_policy_data.l_flock.end + 1;
349 new2->l_conn_export = lock->l_conn_export;
350 if (lock->l_export != NULL) {
351 new2->l_export = class_export_get(lock->l_export);
352 list_add(&new2->l_export_chain,
353 &new2->l_export->exp_ldlm_data.led_held_locks);
355 if (*flags == LDLM_FL_WAIT_NOREPROC)
356 ldlm_lock_addref_internal_nolock(new2, lock->l_granted_mode);
358 /* insert new2 at lock */
359 ldlm_resource_add_lock(res, ownlocks, new2);
364 /* At this point we're granting the lock request. */
365 req->l_granted_mode = req->l_req_mode;
367 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
369 list_del_init(&req->l_res_link);
370 /* insert new lock before ownlocks in list. */
371 ldlm_resource_add_lock(res, ownlocks, req);
374 if (*flags != LDLM_FL_WAIT_NOREPROC) {
376 /* If this is an unlock, reprocess the waitq and
377 * send completions ASTs for locks that can now be
378 * granted. The only problem with doing this
379 * reprocessing here is that the completion ASTs for
380 * newly granted locks will be sent before the unlock
381 * completion is sent. It shouldn't be an issue. Also
382 * note that ldlm_process_flock_lock() will recurse,
383 * but only once because first_enq will be false from
384 * ldlm_reprocess_queue. */
385 if ((mode == LCK_NL) && overlaps) {
386 struct list_head rpc_list
387 = LIST_HEAD_INIT(rpc_list);
390 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
392 rc = ldlm_run_cp_ast_work(&rpc_list);
395 GOTO(restart, -ERESTART);
398 LASSERT(req->l_completion_ast);
399 ldlm_add_ast_work_item(req, NULL, NULL);
403 /* In case we're reprocessing the requested lock we can't destroy
404 * it until after calling ldlm_ast_work_item() above so that lawi()
405 * can bump the reference count on req. Otherwise req could be freed
406 * before the completion AST can be sent. */
408 ldlm_flock_destroy(req, mode, *flags);
410 ldlm_resource_dump(D_OTHER, res);
411 RETURN(LDLM_ITER_CONTINUE);
414 struct ldlm_flock_wait_data {
415 struct ldlm_lock *fwd_lock;
420 ldlm_flock_interrupted_wait(void *data)
422 struct ldlm_lock *lock;
423 struct lustre_handle lockh;
426 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
428 /* take lock off the deadlock detection waitq. */
429 list_del_init(&lock->l_flock_waitq);
431 /* client side - set flag to prevent lock from being put on lru list */
432 lock->l_flags |= LDLM_FL_CBPENDING;
434 ldlm_lock_decref_internal(lock, lock->l_req_mode);
435 ldlm_lock2handle(lock, &lockh);
436 ldlm_cli_cancel(&lockh);
441 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
443 struct ldlm_namespace *ns;
444 struct file_lock *getlk = lock->l_ast_data;
445 struct ldlm_flock_wait_data fwd;
446 unsigned long irqflags;
447 struct obd_device *obd;
448 struct obd_import *imp = NULL;
451 struct l_wait_info lwi;
454 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
457 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
459 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
460 LDLM_FL_BLOCK_CONV)))
463 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
466 ldlm_lock_dump(D_DLMTRACE, lock, 0);
468 obd = class_exp2obd(lock->l_conn_export);
470 /* if this is a local lock, then there is no import */
472 imp = obd->u.cli.cl_import;
475 spin_lock_irqsave(&imp->imp_lock, irqflags);
476 fwd.fwd_generation = imp->imp_generation;
477 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
480 lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
482 /* Go to sleep until the lock is granted. */
483 rc = l_wait_event(lock->l_waitq,
484 ((lock->l_req_mode == lock->l_granted_mode) ||
485 lock->l_destroyed), &lwi);
487 LDLM_DEBUG(lock, "client-side enqueue waking up: rc = %d", rc);
492 LDLM_DEBUG(lock, "client-side enqueue granted");
493 ns = lock->l_resource->lr_namespace;
494 lock_res(lock->l_resource);
496 /* take lock off the deadlock detection waitq. */
497 list_del_init(&lock->l_flock_waitq);
499 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
500 list_del_init(&lock->l_res_link);
502 if (flags & LDLM_FL_TEST_LOCK) {
503 /* fcntl(F_GETLK) request */
504 /* The old mode was saved in getlk->fl_type so that if the mode
505 * in the lock changes we can decref the approprate refcount. */
506 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
507 switch (lock->l_granted_mode) {
509 getlk->fl_type = F_RDLCK;
512 getlk->fl_type = F_WRLCK;
515 getlk->fl_type = F_UNLCK;
517 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
518 getlk->fl_start = lock->l_policy_data.l_flock.start;
519 getlk->fl_end = lock->l_policy_data.l_flock.end;
521 int noreproc = LDLM_FL_WAIT_NOREPROC;
523 /* We need to reprocess the lock to do merges or splits
524 * with existing locks owned by this process. */
525 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
527 wake_up(&lock->l_waitq);
529 unlock_res(lock->l_resource);
533 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
534 void *data, int flag)
539 LASSERT(flag == LDLM_CB_CANCELING);
541 /* take lock off the deadlock detection waitq. */
542 lock_res(lock->l_resource);
543 list_del_init(&lock->l_flock_waitq);
544 unlock_res(lock->l_resource);