1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Hewlett-Packard Development Company LP.
5 * Developed under the sponsorship of the US Government under
6 * Subcontract No. B514193
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
30 #include <lustre_dlm.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_lib.h>
34 #include <libcfs/list.h>
36 #include <liblustre.h>
37 #include <obd_class.h>
40 #include "ldlm_internal.h"
42 #define l_flock_waitq l_lru
44 static CFS_LIST_HEAD(ldlm_flock_waitq);
45 spinlock_t ldlm_flock_waitq_lock = SPIN_LOCK_UNLOCKED;
47 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
48 void *data, int flag);
51 * list_for_remaining_safe - iterate over the remaining entries in a list
52 * and safeguard against removal of a list entry.
53 * @pos: the &struct list_head to use as a loop counter. pos MUST
54 * have been initialized prior to using it in this macro.
55 * @n: another &struct list_head to use as temporary storage
56 * @head: the head for your list.
58 #define list_for_remaining_safe(pos, n, head) \
59 for (n = pos->next; pos != (head); pos = n, n = pos->next)
62 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
64 return((new->l_policy_data.l_flock.pid ==
65 lock->l_policy_data.l_flock.pid) &&
66 (new->l_export == lock->l_export));
70 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
72 return((new->l_policy_data.l_flock.start <=
73 lock->l_policy_data.l_flock.end) &&
74 (new->l_policy_data.l_flock.end >=
75 lock->l_policy_data.l_flock.start));
79 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
83 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
86 /* Safe to not lock here, since it should be empty anyway */
87 LASSERT(list_empty(&lock->l_flock_waitq));
89 list_del_init(&lock->l_res_link);
90 if (flags == LDLM_FL_WAIT_NOREPROC) {
91 /* client side - set a flag to prevent sending a CANCEL */
92 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
93 ldlm_lock_decref_internal(lock, mode);
96 ldlm_lock_destroy_nolock(lock);
101 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
103 struct obd_export *req_export = req->l_export;
104 struct obd_export *blocking_export = blocking_lock->l_export;
105 pid_t req_pid = req->l_policy_data.l_flock.pid;
106 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
107 struct ldlm_lock *lock;
109 spin_lock(&ldlm_flock_waitq_lock);
111 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
112 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
113 (lock->l_export != blocking_export))
116 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
117 blocking_export = (struct obd_export *)(long)
118 lock->l_policy_data.l_flock.blocking_export;
119 if (blocking_pid == req_pid && blocking_export == req_export) {
120 spin_unlock(&ldlm_flock_waitq_lock);
126 spin_unlock(&ldlm_flock_waitq_lock);
132 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
133 ldlm_error_t *err, struct list_head *work_list)
135 struct ldlm_resource *res = req->l_resource;
136 struct ldlm_namespace *ns = res->lr_namespace;
137 struct list_head *tmp;
138 struct list_head *ownlocks = NULL;
139 struct ldlm_lock *lock = NULL;
140 struct ldlm_lock *new = req;
141 struct ldlm_lock *new2 = NULL;
142 ldlm_mode_t mode = req->l_req_mode;
143 int local = ns_is_client(ns);
144 int added = (mode == LCK_NL);
148 CDEBUG(D_DLMTRACE, "flags %#x pid %u mode %u start "LPU64" end "LPU64
149 "\n", *flags, new->l_policy_data.l_flock.pid, mode,
150 req->l_policy_data.l_flock.start,
151 req->l_policy_data.l_flock.end);
156 /* No blocking ASTs are sent to the clients for
157 * Posix file & record locks */
158 req->l_blocking_ast = NULL;
160 /* Called on the server for lock cancels. */
161 req->l_blocking_ast = ldlm_flock_blocking_ast;
164 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
165 /* This loop determines where this processes locks start
166 * in the resource lr_granted list. */
167 list_for_each(tmp, &res->lr_granted) {
168 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
169 if (ldlm_same_flock_owner(lock, req)) {
175 lockmode_verify(mode);
177 /* This loop determines if there are existing locks
178 * that conflict with the new lock request. */
179 list_for_each(tmp, &res->lr_granted) {
180 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
182 if (ldlm_same_flock_owner(lock, req)) {
188 /* locks are compatible, overlap doesn't matter */
189 if (lockmode_compat(lock->l_granted_mode, mode))
192 if (!ldlm_flocks_overlap(lock, req))
196 RETURN(LDLM_ITER_CONTINUE);
198 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
199 ldlm_flock_destroy(req, mode, *flags);
201 RETURN(LDLM_ITER_STOP);
204 if (*flags & LDLM_FL_TEST_LOCK) {
205 ldlm_flock_destroy(req, mode, *flags);
206 req->l_req_mode = lock->l_granted_mode;
207 req->l_policy_data.l_flock.pid =
208 lock->l_policy_data.l_flock.pid;
209 req->l_policy_data.l_flock.start =
210 lock->l_policy_data.l_flock.start;
211 req->l_policy_data.l_flock.end =
212 lock->l_policy_data.l_flock.end;
213 *flags |= LDLM_FL_LOCK_CHANGED;
214 RETURN(LDLM_ITER_STOP);
217 if (ldlm_flock_deadlock(req, lock)) {
218 ldlm_flock_destroy(req, mode, *flags);
220 RETURN(LDLM_ITER_STOP);
223 req->l_policy_data.l_flock.blocking_pid =
224 lock->l_policy_data.l_flock.pid;
225 req->l_policy_data.l_flock.blocking_export =
226 (long)(void *)lock->l_export;
228 LASSERT(list_empty(&req->l_flock_waitq));
229 spin_lock(&ldlm_flock_waitq_lock);
230 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
231 spin_unlock(&ldlm_flock_waitq_lock);
233 ldlm_resource_add_lock(res, &res->lr_waiting, req);
234 *flags |= LDLM_FL_BLOCK_GRANTED;
235 RETURN(LDLM_ITER_STOP);
239 if (*flags & LDLM_FL_TEST_LOCK) {
240 ldlm_flock_destroy(req, mode, *flags);
241 req->l_req_mode = LCK_NL;
242 *flags |= LDLM_FL_LOCK_CHANGED;
243 RETURN(LDLM_ITER_STOP);
246 /* In case we had slept on this lock request take it off of the
247 * deadlock detection waitq. */
248 spin_lock(&ldlm_flock_waitq_lock);
249 list_del_init(&req->l_flock_waitq);
250 spin_unlock(&ldlm_flock_waitq_lock);
252 /* Scan the locks owned by this process that overlap this request.
253 * We may have to merge or split existing locks. */
256 ownlocks = &res->lr_granted;
258 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
259 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
261 if (!ldlm_same_flock_owner(lock, new))
264 if (lock->l_granted_mode == mode) {
265 /* If the modes are the same then we need to process
266 * locks that overlap OR adjoin the new lock. The extra
267 * logic condition is necessary to deal with arithmetic
268 * overflow and underflow. */
269 if ((new->l_policy_data.l_flock.start >
270 (lock->l_policy_data.l_flock.end + 1))
271 && (lock->l_policy_data.l_flock.end !=
275 if ((new->l_policy_data.l_flock.end <
276 (lock->l_policy_data.l_flock.start - 1))
277 && (lock->l_policy_data.l_flock.start != 0))
280 if (new->l_policy_data.l_flock.start <
281 lock->l_policy_data.l_flock.start) {
282 lock->l_policy_data.l_flock.start =
283 new->l_policy_data.l_flock.start;
285 new->l_policy_data.l_flock.start =
286 lock->l_policy_data.l_flock.start;
289 if (new->l_policy_data.l_flock.end >
290 lock->l_policy_data.l_flock.end) {
291 lock->l_policy_data.l_flock.end =
292 new->l_policy_data.l_flock.end;
294 new->l_policy_data.l_flock.end =
295 lock->l_policy_data.l_flock.end;
299 ldlm_flock_destroy(lock, mode, *flags);
307 if (new->l_policy_data.l_flock.start >
308 lock->l_policy_data.l_flock.end)
311 if (new->l_policy_data.l_flock.end <
312 lock->l_policy_data.l_flock.start)
317 if (new->l_policy_data.l_flock.start <=
318 lock->l_policy_data.l_flock.start) {
319 if (new->l_policy_data.l_flock.end <
320 lock->l_policy_data.l_flock.end) {
321 lock->l_policy_data.l_flock.start =
322 new->l_policy_data.l_flock.end + 1;
325 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
328 if (new->l_policy_data.l_flock.end >=
329 lock->l_policy_data.l_flock.end) {
330 lock->l_policy_data.l_flock.end =
331 new->l_policy_data.l_flock.start - 1;
335 /* split the existing lock into two locks */
337 /* if this is an F_UNLCK operation then we could avoid
338 * allocating a new lock and use the req lock passed in
339 * with the request but this would complicate the reply
340 * processing since updates to req get reflected in the
341 * reply. The client side replays the lock request so
342 * it must see the original lock data in the reply. */
344 /* XXX - if ldlm_lock_new() can sleep we should
345 * release the ns_lock, allocate the new lock,
346 * and restart processing this lock. */
347 new2 = ldlm_lock_create(ns, &res->lr_name, LDLM_FLOCK,
348 lock->l_granted_mode, NULL, NULL, NULL,
351 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
353 RETURN(LDLM_ITER_STOP);
356 new2->l_granted_mode = lock->l_granted_mode;
357 new2->l_policy_data.l_flock.pid =
358 new->l_policy_data.l_flock.pid;
359 new2->l_policy_data.l_flock.start =
360 lock->l_policy_data.l_flock.start;
361 new2->l_policy_data.l_flock.end =
362 new->l_policy_data.l_flock.start - 1;
363 lock->l_policy_data.l_flock.start =
364 new->l_policy_data.l_flock.end + 1;
365 new2->l_conn_export = lock->l_conn_export;
366 if (lock->l_export != NULL) {
367 new2->l_export = class_export_get(lock->l_export);
368 spin_lock(&new2->l_export->exp_ldlm_data.led_lock);
369 list_add(&new2->l_export_chain,
370 &new2->l_export->exp_ldlm_data.led_held_locks);
371 spin_unlock(&new2->l_export->exp_ldlm_data.led_lock);
373 if (*flags == LDLM_FL_WAIT_NOREPROC)
374 ldlm_lock_addref_internal(new2, lock->l_granted_mode);
376 /* insert new2 at lock */
377 ldlm_resource_add_lock(res, ownlocks, new2);
382 /* At this point we're granting the lock request. */
383 req->l_granted_mode = req->l_req_mode;
385 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
387 list_del_init(&req->l_res_link);
388 /* insert new lock before ownlocks in list. */
389 ldlm_resource_add_lock(res, ownlocks, req);
392 if (*flags != LDLM_FL_WAIT_NOREPROC) {
394 /* If this is an unlock, reprocess the waitq and
395 * send completions ASTs for locks that can now be
396 * granted. The only problem with doing this
397 * reprocessing here is that the completion ASTs for
398 * newly granted locks will be sent before the unlock
399 * completion is sent. It shouldn't be an issue. Also
400 * note that ldlm_process_flock_lock() will recurse,
401 * but only once because first_enq will be false from
402 * ldlm_reprocess_queue. */
403 if ((mode == LCK_NL) && overlaps) {
404 CFS_LIST_HEAD(rpc_list);
407 ldlm_reprocess_queue(res, &res->lr_waiting,
411 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_BL_AST);
414 GOTO(restart, -ERESTART);
417 LASSERT(req->l_completion_ast);
418 ldlm_add_ast_work_item(req, NULL, work_list);
422 /* In case we're reprocessing the requested lock we can't destroy
423 * it until after calling ldlm_ast_work_item() above so that lawi()
424 * can bump the reference count on req. Otherwise req could be freed
425 * before the completion AST can be sent. */
427 ldlm_flock_destroy(req, mode, *flags);
429 ldlm_resource_dump(D_OTHER, res);
430 RETURN(LDLM_ITER_CONTINUE);
433 struct ldlm_flock_wait_data {
434 struct ldlm_lock *fwd_lock;
439 ldlm_flock_interrupted_wait(void *data)
441 struct ldlm_lock *lock;
442 struct lustre_handle lockh;
446 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
448 /* take lock off the deadlock detection waitq. */
449 spin_lock(&ldlm_flock_waitq_lock);
450 list_del_init(&lock->l_flock_waitq);
451 spin_unlock(&ldlm_flock_waitq_lock);
453 /* client side - set flag to prevent lock from being put on lru list */
454 lock->l_flags |= LDLM_FL_CBPENDING;
456 ldlm_lock_decref_internal(lock, lock->l_req_mode);
457 ldlm_lock2handle(lock, &lockh);
458 rc = ldlm_cli_cancel(&lockh);
460 CERROR("ldlm_cli_cancel: %d\n", rc);
466 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
468 struct ldlm_namespace *ns;
469 cfs_flock_t *getlk = lock->l_ast_data;
470 struct ldlm_flock_wait_data fwd;
471 struct obd_device *obd;
472 struct obd_import *imp = NULL;
475 struct l_wait_info lwi;
478 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
481 /* Import invalidation. We need to actually release the lock
482 * references being held, so that it can go away. No point in
483 * holding the lock even if app still believes it has it, since
484 * server already dropped it anyway. Only for granted locks too. */
485 lock_res_and_lock(lock);
486 if ((lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_LOCAL_ONLY)) ==
487 (LDLM_FL_FAILED|LDLM_FL_LOCAL_ONLY)) {
488 unlock_res_and_lock(lock);
489 if (lock->l_req_mode == lock->l_granted_mode &&
490 lock->l_granted_mode != LCK_NL)
491 ldlm_lock_decref_internal(lock, lock->l_req_mode);
494 unlock_res_and_lock(lock);
496 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
498 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
499 LDLM_FL_BLOCK_CONV)))
502 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
506 obd = class_exp2obd(lock->l_conn_export);
508 /* if this is a local lock, then there is no import */
510 imp = obd->u.cli.cl_import;
513 spin_lock(&imp->imp_lock);
514 fwd.fwd_generation = imp->imp_generation;
515 spin_unlock(&imp->imp_lock);
518 lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
520 /* Go to sleep until the lock is granted. */
521 rc = l_wait_event(lock->l_waitq,
522 ((lock->l_req_mode == lock->l_granted_mode) ||
523 lock->l_destroyed), &lwi);
525 LDLM_DEBUG(lock, "client-side enqueue waking up: rc = %d", rc);
530 LDLM_DEBUG(lock, "client-side enqueue granted");
531 ns = lock->l_resource->lr_namespace;
532 lock_res(lock->l_resource);
534 /* take lock off the deadlock detection waitq. */
535 spin_lock(&ldlm_flock_waitq_lock);
536 list_del_init(&lock->l_flock_waitq);
537 spin_unlock(&ldlm_flock_waitq_lock);
539 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
540 list_del_init(&lock->l_res_link);
542 if (flags & LDLM_FL_TEST_LOCK) {
543 /* fcntl(F_GETLK) request */
544 /* The old mode was saved in getlk->fl_type so that if the mode
545 * in the lock changes we can decref the approprate refcount. */
546 ldlm_flock_destroy(lock, cfs_flock_type(getlk), LDLM_FL_WAIT_NOREPROC);
547 switch (lock->l_granted_mode) {
549 cfs_flock_set_type(getlk, F_RDLCK);
552 cfs_flock_set_type(getlk, F_WRLCK);
555 cfs_flock_set_type(getlk, F_UNLCK);
557 cfs_flock_set_pid(getlk, (pid_t)lock->l_policy_data.l_flock.pid);
558 cfs_flock_set_start(getlk, (loff_t)lock->l_policy_data.l_flock.start);
559 cfs_flock_set_end(getlk, (loff_t)lock->l_policy_data.l_flock.end);
561 int noreproc = LDLM_FL_WAIT_NOREPROC;
563 /* We need to reprocess the lock to do merges or splits
564 * with existing locks owned by this process. */
565 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
567 cfs_waitq_signal(&lock->l_waitq);
569 unlock_res(lock->l_resource);
572 EXPORT_SYMBOL(ldlm_flock_completion_ast);
574 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
575 void *data, int flag)
577 struct ldlm_namespace *ns;
581 LASSERT(flag == LDLM_CB_CANCELING);
583 ns = lock->l_resource->lr_namespace;
585 /* take lock off the deadlock detection waitq. */
586 spin_lock(&ldlm_flock_waitq_lock);
587 list_del_init(&lock->l_flock_waitq);
588 spin_unlock(&ldlm_flock_waitq_lock);