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);
43 * list_for_remaining_safe - iterate over the remaining entries in a list
44 * and safeguard against removal of a list entry.
45 * @pos: the &struct list_head to use as a loop counter. pos MUST
46 * have been initialized prior to using it in this macro.
47 * @n: another &struct list_head to use as temporary storage
48 * @head: the head for your list.
50 #define list_for_remaining_safe(pos, n, head) \
51 for (n = pos->next; pos != (head); pos = n, n = pos->next)
54 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
56 return((new->l_policy_data.l_flock.pid ==
57 lock->l_policy_data.l_flock.pid) &&
58 (new->l_export == lock->l_export));
62 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
64 return((new->l_policy_data.l_flock.start <=
65 lock->l_policy_data.l_flock.end) &&
66 (new->l_policy_data.l_flock.end >=
67 lock->l_policy_data.l_flock.start));
71 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
75 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
78 list_del_init(&lock->l_res_link);
79 if (flags == LDLM_FL_WAIT_NOREPROC) {
80 /* client side - set a flag to prevent sending a CANCEL */
81 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
82 ldlm_lock_decref_internal(lock, mode);
85 ldlm_lock_destroy(lock);
90 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
92 struct obd_export *req_export = req->l_export;
93 struct obd_export *blocking_export = blocking_lock->l_export;
94 pid_t req_pid = req->l_policy_data.l_flock.pid;
95 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
96 struct ldlm_lock *lock;
99 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
100 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
101 (lock->l_export != blocking_export))
104 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
105 blocking_export = (struct obd_export *)(long)
106 lock->l_policy_data.l_flock.blocking_export;
107 if (blocking_pid == req_pid && blocking_export == req_export)
117 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
120 struct ldlm_resource *res = req->l_resource;
121 struct ldlm_namespace *ns = res->lr_namespace;
122 struct list_head *tmp;
123 struct list_head *ownlocks = NULL;
124 struct ldlm_lock *lock = NULL;
125 struct ldlm_lock *new = req;
126 struct ldlm_lock *new2 = NULL;
127 ldlm_mode_t mode = req->l_req_mode;
128 int added = (mode == LCK_NL);
132 CDEBUG(D_DLMTRACE, "flags %#x pid "LPU64" mode %u start "LPU64" end "
133 LPU64"\n", *flags, new->l_policy_data.l_flock.pid, mode,
134 req->l_policy_data.l_flock.start,
135 req->l_policy_data.l_flock.end);
139 /* No blocking ASTs are sent for Posix file & record locks */
140 req->l_blocking_ast = NULL;
142 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
143 /* This loop determines where this processes locks start
144 * in the resource lr_granted list. */
145 list_for_each(tmp, &res->lr_granted) {
146 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
147 if (ldlm_same_flock_owner(lock, req)) {
153 lockmode_verify(mode);
155 /* This loop determines if there are existing locks
156 * that conflict with the new lock request. */
157 list_for_each(tmp, &res->lr_granted) {
158 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
160 if (ldlm_same_flock_owner(lock, req)) {
166 /* locks are compatible, overlap doesn't matter */
167 if (lockmode_compat(lock->l_granted_mode, mode))
170 if (!ldlm_flocks_overlap(lock, req))
174 RETURN(LDLM_ITER_CONTINUE);
176 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
177 ldlm_flock_destroy(req, mode, *flags);
179 RETURN(LDLM_ITER_STOP);
182 if (*flags & LDLM_FL_TEST_LOCK) {
183 ldlm_flock_destroy(req, mode, *flags);
184 req->l_req_mode = lock->l_granted_mode;
185 req->l_policy_data.l_flock.pid =
186 lock->l_policy_data.l_flock.pid;
187 req->l_policy_data.l_flock.start =
188 lock->l_policy_data.l_flock.start;
189 req->l_policy_data.l_flock.end =
190 lock->l_policy_data.l_flock.end;
191 *flags |= LDLM_FL_LOCK_CHANGED;
192 RETURN(LDLM_ITER_STOP);
195 if (ldlm_flock_deadlock(req, lock)) {
196 ldlm_flock_destroy(req, mode, *flags);
198 RETURN(LDLM_ITER_STOP);
201 req->l_policy_data.l_flock.blocking_pid =
202 lock->l_policy_data.l_flock.pid;
203 req->l_policy_data.l_flock.blocking_export =
204 (long)(void *)lock->l_export;
206 LASSERT(list_empty(&req->l_flock_waitq));
207 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
209 ldlm_resource_add_lock(res, &res->lr_waiting, req);
210 *flags |= LDLM_FL_BLOCK_GRANTED;
211 RETURN(LDLM_ITER_STOP);
215 if (*flags & LDLM_FL_TEST_LOCK) {
216 ldlm_flock_destroy(req, mode, *flags);
217 req->l_req_mode = LCK_NL;
218 *flags |= LDLM_FL_LOCK_CHANGED;
219 RETURN(LDLM_ITER_STOP);
222 /* Scan the locks owned by this process that overlap this request.
223 * We may have to merge or split existing locks. */
226 ownlocks = &res->lr_granted;
228 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
229 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
231 if (!ldlm_same_flock_owner(lock, new))
234 if (lock->l_granted_mode == mode) {
235 /* If the modes are the same then we need to process
236 * locks that overlap OR adjoin the new lock. The extra
237 * logic condition is necessary to deal with arithmetic
238 * overflow and underflow. */
239 if ((new->l_policy_data.l_flock.start >
240 (lock->l_policy_data.l_flock.end + 1))
241 && (lock->l_policy_data.l_flock.end != ~0))
244 if ((new->l_policy_data.l_flock.end <
245 (lock->l_policy_data.l_flock.start - 1))
246 && (lock->l_policy_data.l_flock.start != 0))
249 if (new->l_policy_data.l_flock.start <
250 lock->l_policy_data.l_flock.start) {
251 lock->l_policy_data.l_flock.start =
252 new->l_policy_data.l_flock.start;
254 new->l_policy_data.l_flock.start =
255 lock->l_policy_data.l_flock.start;
258 if (new->l_policy_data.l_flock.end >
259 lock->l_policy_data.l_flock.end) {
260 lock->l_policy_data.l_flock.end =
261 new->l_policy_data.l_flock.end;
263 new->l_policy_data.l_flock.end =
264 lock->l_policy_data.l_flock.end;
268 ldlm_flock_destroy(lock, mode, *flags);
276 if (new->l_policy_data.l_flock.start >
277 lock->l_policy_data.l_flock.end)
280 if (new->l_policy_data.l_flock.end <
281 lock->l_policy_data.l_flock.start)
286 if (new->l_policy_data.l_flock.start <=
287 lock->l_policy_data.l_flock.start) {
288 if (new->l_policy_data.l_flock.end <
289 lock->l_policy_data.l_flock.end) {
290 lock->l_policy_data.l_flock.start =
291 new->l_policy_data.l_flock.end + 1;
294 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
297 if (new->l_policy_data.l_flock.end >=
298 lock->l_policy_data.l_flock.end) {
299 lock->l_policy_data.l_flock.end =
300 new->l_policy_data.l_flock.start - 1;
304 /* split the existing lock into two locks */
306 /* if this is an F_UNLCK operation then we could avoid
307 * allocating a new lock and use the req lock passed in
308 * with the request but this would complicate the reply
309 * processing since updates to req get reflected in the
310 * reply. The client side replays the lock request so
311 * it must see the original lock data in the reply. */
313 /* XXX - if ldlm_lock_new() can sleep we should
314 * release the ns_lock, allocate the new lock,
315 * and restart processing this lock. */
316 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
317 lock->l_granted_mode, NULL, NULL, NULL,
320 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
322 RETURN(LDLM_ITER_STOP);
325 new2->l_granted_mode = lock->l_granted_mode;
326 new2->l_policy_data.l_flock.pid =
327 new->l_policy_data.l_flock.pid;
328 new2->l_policy_data.l_flock.start =
329 lock->l_policy_data.l_flock.start;
330 new2->l_policy_data.l_flock.end =
331 new->l_policy_data.l_flock.start - 1;
332 lock->l_policy_data.l_flock.start =
333 new->l_policy_data.l_flock.end + 1;
334 new2->l_conn_export = lock->l_conn_export;
335 if (lock->l_export != NULL) {
336 new2->l_export = class_export_get(lock->l_export);
337 list_add(&new2->l_export_chain,
338 &new2->l_export->exp_ldlm_data.led_held_locks);
340 if (*flags == LDLM_FL_WAIT_NOREPROC)
341 ldlm_lock_addref_internal(new2, lock->l_granted_mode);
343 /* insert new2 at lock */
344 ldlm_resource_add_lock(res, ownlocks, new2);
349 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
351 req->l_granted_mode = req->l_req_mode;
352 list_del_init(&req->l_res_link);
353 /* insert new lock before ownlocks in list. */
354 ldlm_resource_add_lock(res, ownlocks, req);
357 if (*flags != LDLM_FL_WAIT_NOREPROC) {
359 /* If this is an unlock, reprocess the waitq and
360 * send completions ASTs for locks that can now be
361 * granted. The only problem with doing this
362 * reprocessing here is that the completion ASTs for
363 * newly granted locks will be sent before the unlock
364 * completion is sent. It shouldn't be an issue. Also
365 * note that ldlm_process_flock_lock() will recurse,
366 * but only once because first_enq will be false from
367 * ldlm_reprocess_queue. */
368 if ((mode == LCK_NL) && overlaps) {
369 struct list_head rpc_list
370 = LIST_HEAD_INIT(rpc_list);
373 res->lr_tmp = &rpc_list;
374 ldlm_reprocess_queue(res, &res->lr_waiting);
377 l_unlock(&ns->ns_lock);
378 rc = ldlm_run_ast_work(res->lr_namespace,
380 l_lock(&ns->ns_lock);
382 GOTO(restart, -ERESTART);
385 LASSERT(req->l_completion_ast);
386 ldlm_add_ast_work_item(req, NULL, NULL, 0);
390 /* In case we're reprocessing the requested lock we can't destroy
391 * it until after calling ldlm_ast_work_item() above so that lawi()
392 * can bump the reference count on req. Otherwise req could be freed
393 * before the completion AST can be sent. */
395 ldlm_flock_destroy(req, mode, *flags);
397 ldlm_resource_dump(res);
398 RETURN(LDLM_ITER_CONTINUE);
401 struct ldlm_flock_wait_data {
402 struct ldlm_lock *fwd_lock;
407 ldlm_flock_interrupted_wait(void *data)
409 struct ldlm_lock *lock;
410 struct lustre_handle lockh;
414 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
415 ldlm_lock_decref_internal(lock, lock->l_req_mode);
416 ldlm_lock2handle(lock, &lockh);
417 rc = ldlm_cli_cancel(&lockh);
418 CDEBUG(D_DLMTRACE, "ldlm_cli_cancel: %d\n", rc);
423 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
425 struct ldlm_namespace *ns;
426 struct file_lock *getlk = lock->l_ast_data;
427 struct ldlm_flock_wait_data fwd;
428 unsigned long irqflags;
429 struct obd_device *obd;
430 struct obd_import *imp = NULL;
433 struct l_wait_info lwi;
436 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
439 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
442 wake_up(&lock->l_waitq);
446 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
447 LDLM_FL_BLOCK_CONV)))
450 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
453 ldlm_lock_dump(D_OTHER, lock, 0);
456 obd = class_exp2obd(lock->l_conn_export);
458 /* if this is a local lock, then there is no import */
460 imp = obd->u.cli.cl_import;
463 spin_lock_irqsave(&imp->imp_lock, irqflags);
464 fwd.fwd_generation = imp->imp_generation;
465 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
468 lwi = LWI_TIMEOUT_INTR(0,NULL,ldlm_flock_interrupted_wait,&fwd);
470 /* Go to sleep until the lock is granted. */
471 rc = l_wait_event(lock->l_waitq,
472 ((lock->l_req_mode == lock->l_granted_mode) ||
473 lock->l_destroyed), &lwi);
476 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
481 LASSERT(!(lock->l_destroyed));
485 LDLM_DEBUG(lock, "client-side enqueue waking up");
486 ns = lock->l_resource->lr_namespace;
487 l_lock(&ns->ns_lock);
489 /* take data off of deadlock detection waitq. */
490 list_del_init(&lock->l_flock_waitq);
492 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
493 list_del_init(&lock->l_res_link);
495 if (flags & LDLM_FL_TEST_LOCK) {
496 /* fcntl(F_GETLK) request */
497 /* The old mode was saved in getlk->fl_type so that if the mode
498 * in the lock changes we can decref the approprate refcount. */
499 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
500 switch (lock->l_granted_mode) {
502 getlk->fl_type = F_RDLCK;
505 getlk->fl_type = F_WRLCK;
508 getlk->fl_type = F_UNLCK;
510 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
511 getlk->fl_start = lock->l_policy_data.l_flock.start;
512 getlk->fl_end = lock->l_policy_data.l_flock.end;
514 /* We need to reprocess the lock to do merges or splits
515 * with existing locks owned by this process. */
516 flags = LDLM_FL_WAIT_NOREPROC;
517 ldlm_process_flock_lock(lock, &flags, 1, &err);
519 l_unlock(&ns->ns_lock);