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 %u mode %u start "LPU64" end "LPU64
133 "\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 /* This loop determines if there are existing locks
154 * that conflict with the new lock request. */
155 list_for_each(tmp, &res->lr_granted) {
156 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
158 if (ldlm_same_flock_owner(lock, req)) {
164 /* locks are compatible, overlap doesn't matter */
165 if (lockmode_compat(lock->l_granted_mode, mode))
168 if (!ldlm_flocks_overlap(lock, req))
172 RETURN(LDLM_ITER_CONTINUE);
174 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
175 ldlm_flock_destroy(req, mode, *flags);
177 RETURN(LDLM_ITER_STOP);
180 if (*flags & LDLM_FL_TEST_LOCK) {
181 ldlm_flock_destroy(req, mode, *flags);
182 req->l_req_mode = lock->l_granted_mode;
183 req->l_policy_data.l_flock.pid =
184 lock->l_policy_data.l_flock.pid;
185 req->l_policy_data.l_flock.start =
186 lock->l_policy_data.l_flock.start;
187 req->l_policy_data.l_flock.end =
188 lock->l_policy_data.l_flock.end;
189 *flags |= LDLM_FL_LOCK_CHANGED;
190 RETURN(LDLM_ITER_STOP);
193 if (ldlm_flock_deadlock(req, lock)) {
194 ldlm_flock_destroy(req, mode, *flags);
196 RETURN(LDLM_ITER_STOP);
199 req->l_policy_data.l_flock.blocking_pid =
200 lock->l_policy_data.l_flock.pid;
201 req->l_policy_data.l_flock.blocking_export =
202 (long)(void *)lock->l_export;
204 LASSERT(list_empty(&req->l_flock_waitq));
205 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
207 ldlm_resource_add_lock(res, &res->lr_waiting, req);
208 *flags |= LDLM_FL_BLOCK_GRANTED;
209 RETURN(LDLM_ITER_STOP);
213 if (*flags & LDLM_FL_TEST_LOCK) {
214 ldlm_flock_destroy(req, mode, *flags);
215 req->l_req_mode = LCK_NL;
216 *flags |= LDLM_FL_LOCK_CHANGED;
217 RETURN(LDLM_ITER_STOP);
220 /* Scan the locks owned by this process that overlap this request.
221 * We may have to merge or split existing locks. */
224 ownlocks = &res->lr_granted;
226 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
227 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
229 if (!ldlm_same_flock_owner(lock, new))
232 if (lock->l_granted_mode == mode) {
233 /* If the modes are the same then we need to process
234 * locks that overlap OR adjoin the new lock. The extra
235 * logic condition is necessary to deal with arithmetic
236 * overflow and underflow. */
237 if ((new->l_policy_data.l_flock.start >
238 (lock->l_policy_data.l_flock.end + 1))
239 && (lock->l_policy_data.l_flock.end != ~0))
242 if ((new->l_policy_data.l_flock.end <
243 (lock->l_policy_data.l_flock.start - 1))
244 && (lock->l_policy_data.l_flock.start != 0))
247 if (new->l_policy_data.l_flock.start <
248 lock->l_policy_data.l_flock.start) {
249 lock->l_policy_data.l_flock.start =
250 new->l_policy_data.l_flock.start;
252 new->l_policy_data.l_flock.start =
253 lock->l_policy_data.l_flock.start;
256 if (new->l_policy_data.l_flock.end >
257 lock->l_policy_data.l_flock.end) {
258 lock->l_policy_data.l_flock.end =
259 new->l_policy_data.l_flock.end;
261 new->l_policy_data.l_flock.end =
262 lock->l_policy_data.l_flock.end;
266 ldlm_flock_destroy(lock, mode, *flags);
274 if (new->l_policy_data.l_flock.start >
275 lock->l_policy_data.l_flock.end)
278 if (new->l_policy_data.l_flock.end <
279 lock->l_policy_data.l_flock.start)
284 if (new->l_policy_data.l_flock.start <=
285 lock->l_policy_data.l_flock.start) {
286 if (new->l_policy_data.l_flock.end <
287 lock->l_policy_data.l_flock.end) {
288 lock->l_policy_data.l_flock.start =
289 new->l_policy_data.l_flock.end + 1;
292 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
295 if (new->l_policy_data.l_flock.end >=
296 lock->l_policy_data.l_flock.end) {
297 lock->l_policy_data.l_flock.end =
298 new->l_policy_data.l_flock.start - 1;
302 /* split the existing lock into two locks */
304 /* if this is an F_UNLCK operation then we could avoid
305 * allocating a new lock and use the req lock passed in
306 * with the request but this would complicate the reply
307 * processing since updates to req get reflected in the
308 * reply. The client side replays the lock request so
309 * it must see the original lock data in the reply. */
311 /* XXX - if ldlm_lock_new() can sleep we should
312 * release the ns_lock, allocate the new lock,
313 * and restart processing this lock. */
314 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
315 lock->l_granted_mode, NULL, NULL, NULL);
317 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
319 RETURN(LDLM_ITER_STOP);
322 new2->l_granted_mode = lock->l_granted_mode;
323 new2->l_policy_data.l_flock.pid =
324 new->l_policy_data.l_flock.pid;
325 new2->l_policy_data.l_flock.start =
326 lock->l_policy_data.l_flock.start;
327 new2->l_policy_data.l_flock.end =
328 new->l_policy_data.l_flock.start - 1;
329 lock->l_policy_data.l_flock.start =
330 new->l_policy_data.l_flock.end + 1;
331 new2->l_conn_export = lock->l_conn_export;
332 if (lock->l_export != NULL) {
333 new2->l_export = class_export_get(lock->l_export);
334 list_add(&new2->l_export_chain,
335 &new2->l_export->exp_ldlm_data.led_held_locks);
337 if (*flags == LDLM_FL_WAIT_NOREPROC)
338 ldlm_lock_addref_internal(new2, lock->l_granted_mode);
340 /* insert new2 at lock */
341 ldlm_resource_add_lock(res, ownlocks, new2);
346 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
348 req->l_granted_mode = req->l_req_mode;
349 list_del_init(&req->l_res_link);
350 /* insert new lock before ownlocks in list. */
351 ldlm_resource_add_lock(res, ownlocks, req);
354 if (*flags != LDLM_FL_WAIT_NOREPROC) {
356 /* If this is an unlock, reprocess the waitq and
357 * send completions ASTs for locks that can now be
358 * granted. The only problem with doing this
359 * reprocessing here is that the completion ASTs for
360 * newly granted locks will be sent before the unlock
361 * completion is sent. It shouldn't be an issue. Also
362 * note that ldlm_process_flock_lock() will recurse,
363 * but only once because first_enq will be false from
364 * ldlm_reprocess_queue. */
365 if ((mode == LCK_NL) && overlaps) {
366 struct list_head rpc_list
367 = LIST_HEAD_INIT(rpc_list);
370 res->lr_tmp = &rpc_list;
371 ldlm_reprocess_queue(res, &res->lr_waiting);
374 l_unlock(&ns->ns_lock);
375 rc = ldlm_run_ast_work(res->lr_namespace,
377 l_lock(&ns->ns_lock);
379 GOTO(restart, -ERESTART);
382 LASSERT(req->l_completion_ast);
383 ldlm_add_ast_work_item(req, NULL, NULL, 0);
387 /* In case we're reprocessing the requested lock we can't destroy
388 * it until after calling ldlm_ast_work_item() above so that lawi()
389 * can bump the reference count on req. Otherwise req could be freed
390 * before the completion AST can be sent. */
392 ldlm_flock_destroy(req, mode, *flags);
394 ldlm_resource_dump(res);
395 RETURN(LDLM_ITER_CONTINUE);
398 struct ldlm_flock_wait_data {
399 struct ldlm_lock *fwd_lock;
404 ldlm_flock_interrupted_wait(void *data)
406 struct ldlm_lock *lock;
407 struct lustre_handle lockh;
411 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
412 ldlm_lock_decref_internal(lock, lock->l_req_mode);
413 ldlm_lock2handle(lock, &lockh);
414 rc = ldlm_cli_cancel(&lockh);
415 CDEBUG(D_DLMTRACE, "ldlm_cli_cancel: %d\n", rc);
420 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
422 struct ldlm_namespace *ns;
423 struct file_lock *getlk = lock->l_ast_data;
424 struct ldlm_flock_wait_data fwd;
425 unsigned long irqflags;
426 struct obd_device *obd;
427 struct obd_import *imp = NULL;
430 struct l_wait_info lwi;
433 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
436 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
439 wake_up(&lock->l_waitq);
443 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
444 LDLM_FL_BLOCK_CONV)))
447 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
450 ldlm_lock_dump(D_OTHER, lock, 0);
453 obd = class_exp2obd(lock->l_conn_export);
455 /* if this is a local lock, then there is no import */
457 imp = obd->u.cli.cl_import;
460 spin_lock_irqsave(&imp->imp_lock, irqflags);
461 fwd.fwd_generation = imp->imp_generation;
462 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
465 lwi = LWI_TIMEOUT_INTR(0,NULL,ldlm_flock_interrupted_wait,&fwd);
467 /* Go to sleep until the lock is granted. */
468 rc = l_wait_event(lock->l_waitq,
469 ((lock->l_req_mode == lock->l_granted_mode) ||
470 lock->l_destroyed), &lwi);
473 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
478 LASSERT(!(lock->l_destroyed));
482 LDLM_DEBUG(lock, "client-side enqueue waking up");
483 ns = lock->l_resource->lr_namespace;
484 l_lock(&ns->ns_lock);
486 /* take data off of deadlock detection waitq. */
487 list_del_init(&lock->l_flock_waitq);
489 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
490 list_del_init(&lock->l_res_link);
492 if (flags & LDLM_FL_TEST_LOCK) {
493 /* fcntl(F_GETLK) request */
494 /* The old mode was saved in getlk->fl_type so that if the mode
495 * in the lock changes we can decref the approprate refcount. */
496 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
497 switch (lock->l_granted_mode) {
499 getlk->fl_type = F_RDLCK;
502 getlk->fl_type = F_WRLCK;
505 getlk->fl_type = F_UNLCK;
507 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
508 getlk->fl_start = lock->l_policy_data.l_flock.start;
509 getlk->fl_end = lock->l_policy_data.l_flock.end;
511 /* We need to reprocess the lock to do merges or splits
512 * with existing locks owne by this process. */
513 flags = LDLM_FL_WAIT_NOREPROC;
514 ldlm_process_flock_lock(lock, &flags, 1, &err);
516 l_unlock(&ns->ns_lock);