1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003 Hewlett-Packard Development Company LP.
30 * Developed under the sponsorship of the US Government under
31 * Subcontract No. B514193
33 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
34 * Use is subject to license terms.
36 * Copyright (c) 2010, 2011, Whamcloud, Inc.
39 * This file is part of Lustre, http://www.lustre.org/
40 * Lustre is a trademark of Sun Microsystems, Inc.
43 #define DEBUG_SUBSYSTEM S_LDLM
46 #include <lustre_dlm.h>
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include <lustre_lib.h>
50 #include <libcfs/list.h>
52 #include <liblustre.h>
53 #include <obd_class.h>
56 #include "ldlm_internal.h"
58 #define l_flock_waitq l_lru
60 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
61 void *data, int flag);
64 * list_for_remaining_safe - iterate over the remaining entries in a list
65 * and safeguard against removal of a list entry.
66 * \param pos the &struct list_head to use as a loop counter. pos MUST
67 * have been initialized prior to using it in this macro.
68 * \param n another &struct list_head to use as temporary storage
69 * \param head the head for your list.
71 #define list_for_remaining_safe(pos, n, head) \
72 for (n = pos->next; pos != (head); pos = n, n = pos->next)
75 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
77 return((new->l_policy_data.l_flock.owner ==
78 lock->l_policy_data.l_flock.owner) &&
79 (new->l_export == lock->l_export));
83 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
85 return((new->l_policy_data.l_flock.start <=
86 lock->l_policy_data.l_flock.end) &&
87 (new->l_policy_data.l_flock.end >=
88 lock->l_policy_data.l_flock.start));
91 static inline void ldlm_flock_blocking_link(struct ldlm_lock *req,
92 struct ldlm_lock *lock)
95 if (req->l_export == NULL)
98 LASSERT(cfs_list_empty(&req->l_flock_waitq));
99 cfs_write_lock(&req->l_export->exp_flock_wait_lock);
101 req->l_policy_data.l_flock.blocking_owner =
102 lock->l_policy_data.l_flock.owner;
103 req->l_policy_data.l_flock.blocking_export =
104 class_export_get(lock->l_export);
106 cfs_list_add_tail(&req->l_flock_waitq,
107 &req->l_export->exp_flock_wait_list);
108 cfs_write_unlock(&req->l_export->exp_flock_wait_lock);
111 static inline void ldlm_flock_blocking_unlink(struct ldlm_lock *req)
113 /* For server only */
114 if (req->l_export == NULL)
117 cfs_write_lock(&req->l_export->exp_flock_wait_lock);
118 if (!cfs_list_empty(&req->l_flock_waitq)) {
119 cfs_list_del_init(&req->l_flock_waitq);
121 class_export_put(req->l_policy_data.l_flock.blocking_export);
122 req->l_policy_data.l_flock.blocking_owner = 0;
123 req->l_policy_data.l_flock.blocking_export = NULL;
125 cfs_write_unlock(&req->l_export->exp_flock_wait_lock);
129 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
133 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
136 /* Safe to not lock here, since it should be empty anyway */
137 LASSERT(cfs_list_empty(&lock->l_flock_waitq));
139 cfs_list_del_init(&lock->l_res_link);
140 if (flags == LDLM_FL_WAIT_NOREPROC &&
141 !(lock->l_flags & LDLM_FL_FAILED)) {
142 /* client side - set a flag to prevent sending a CANCEL */
143 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
145 /* when reaching here, it is under lock_res_and_lock(). Thus,
146 need call the nolock version of ldlm_lock_decref_internal*/
147 ldlm_lock_decref_internal_nolock(lock, mode);
150 ldlm_lock_destroy_nolock(lock);
155 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *bl_lock)
157 struct obd_export *req_exp = req->l_export;
158 struct obd_export *bl_exp = bl_lock->l_export;
159 struct obd_export *bl_exp_new;
160 __u64 req_owner = req->l_policy_data.l_flock.owner;
161 __u64 bl_owner = bl_lock->l_policy_data.l_flock.owner;
162 struct ldlm_lock *lock;
164 /* For server only */
168 class_export_get(bl_exp);
170 cfs_read_lock(&bl_exp->exp_flock_wait_lock);
171 cfs_list_for_each_entry(lock, &bl_exp->exp_flock_wait_list,
173 struct ldlm_flock *flock = &lock->l_policy_data.l_flock;
175 /* want to find something from same client and same process */
176 if (flock->owner != bl_owner)
179 bl_owner = flock->blocking_owner;
180 bl_exp_new = class_export_get(flock->blocking_export);
181 cfs_read_unlock(&bl_exp->exp_flock_wait_lock);
182 class_export_put(bl_exp);
185 if (bl_owner == req_owner && bl_exp == req_exp) {
186 class_export_put(bl_exp);
192 cfs_read_unlock(&bl_exp->exp_flock_wait_lock);
193 class_export_put(bl_exp);
199 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
200 ldlm_error_t *err, cfs_list_t *work_list)
202 struct ldlm_resource *res = req->l_resource;
203 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
205 cfs_list_t *ownlocks = NULL;
206 struct ldlm_lock *lock = NULL;
207 struct ldlm_lock *new = req;
208 struct ldlm_lock *new2 = NULL;
209 ldlm_mode_t mode = req->l_req_mode;
210 int local = ns_is_client(ns);
211 int added = (mode == LCK_NL);
214 const struct ldlm_callback_suite null_cbs = { NULL };
217 CDEBUG(D_DLMTRACE, "flags %#x owner "LPU64" pid %u mode %u start "LPU64
218 " end "LPU64"\n", *flags, new->l_policy_data.l_flock.owner,
219 new->l_policy_data.l_flock.pid, mode,
220 req->l_policy_data.l_flock.start,
221 req->l_policy_data.l_flock.end);
226 /* No blocking ASTs are sent to the clients for
227 * Posix file & record locks */
228 req->l_blocking_ast = NULL;
230 /* Called on the server for lock cancels. */
231 req->l_blocking_ast = ldlm_flock_blocking_ast;
235 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
236 /* This loop determines where this processes locks start
237 * in the resource lr_granted list. */
238 cfs_list_for_each(tmp, &res->lr_granted) {
239 lock = cfs_list_entry(tmp, struct ldlm_lock,
241 if (ldlm_same_flock_owner(lock, req)) {
247 lockmode_verify(mode);
249 /* This loop determines if there are existing locks
250 * that conflict with the new lock request. */
251 cfs_list_for_each(tmp, &res->lr_granted) {
252 lock = cfs_list_entry(tmp, struct ldlm_lock,
255 if (ldlm_same_flock_owner(lock, req)) {
261 /* locks are compatible, overlap doesn't matter */
262 if (lockmode_compat(lock->l_granted_mode, mode))
265 if (!ldlm_flocks_overlap(lock, req))
269 RETURN(LDLM_ITER_CONTINUE);
271 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
272 ldlm_flock_destroy(req, mode, *flags);
274 RETURN(LDLM_ITER_STOP);
277 if (*flags & LDLM_FL_TEST_LOCK) {
278 ldlm_flock_destroy(req, mode, *flags);
279 req->l_req_mode = lock->l_granted_mode;
280 req->l_policy_data.l_flock.pid =
281 lock->l_policy_data.l_flock.pid;
282 req->l_policy_data.l_flock.start =
283 lock->l_policy_data.l_flock.start;
284 req->l_policy_data.l_flock.end =
285 lock->l_policy_data.l_flock.end;
286 *flags |= LDLM_FL_LOCK_CHANGED;
287 RETURN(LDLM_ITER_STOP);
290 if (ldlm_flock_deadlock(req, lock)) {
291 ldlm_flock_destroy(req, mode, *flags);
293 RETURN(LDLM_ITER_STOP);
297 ldlm_flock_blocking_link(req, lock);
298 ldlm_resource_add_lock(res, &res->lr_waiting, req);
299 *flags |= LDLM_FL_BLOCK_GRANTED;
300 RETURN(LDLM_ITER_STOP);
304 if (*flags & LDLM_FL_TEST_LOCK) {
305 ldlm_flock_destroy(req, mode, *flags);
306 req->l_req_mode = LCK_NL;
307 *flags |= LDLM_FL_LOCK_CHANGED;
308 RETURN(LDLM_ITER_STOP);
311 /* In case we had slept on this lock request take it off of the
312 * deadlock detection waitq. */
313 ldlm_flock_blocking_unlink(req);
315 /* Scan the locks owned by this process that overlap this request.
316 * We may have to merge or split existing locks. */
319 ownlocks = &res->lr_granted;
321 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
322 lock = cfs_list_entry(ownlocks, struct ldlm_lock, l_res_link);
324 if (!ldlm_same_flock_owner(lock, new))
327 if (lock->l_granted_mode == mode) {
328 /* If the modes are the same then we need to process
329 * locks that overlap OR adjoin the new lock. The extra
330 * logic condition is necessary to deal with arithmetic
331 * overflow and underflow. */
332 if ((new->l_policy_data.l_flock.start >
333 (lock->l_policy_data.l_flock.end + 1))
334 && (lock->l_policy_data.l_flock.end !=
338 if ((new->l_policy_data.l_flock.end <
339 (lock->l_policy_data.l_flock.start - 1))
340 && (lock->l_policy_data.l_flock.start != 0))
343 if (new->l_policy_data.l_flock.start <
344 lock->l_policy_data.l_flock.start) {
345 lock->l_policy_data.l_flock.start =
346 new->l_policy_data.l_flock.start;
348 new->l_policy_data.l_flock.start =
349 lock->l_policy_data.l_flock.start;
352 if (new->l_policy_data.l_flock.end >
353 lock->l_policy_data.l_flock.end) {
354 lock->l_policy_data.l_flock.end =
355 new->l_policy_data.l_flock.end;
357 new->l_policy_data.l_flock.end =
358 lock->l_policy_data.l_flock.end;
362 ldlm_flock_destroy(lock, mode, *flags);
370 if (new->l_policy_data.l_flock.start >
371 lock->l_policy_data.l_flock.end)
374 if (new->l_policy_data.l_flock.end <
375 lock->l_policy_data.l_flock.start)
380 if (new->l_policy_data.l_flock.start <=
381 lock->l_policy_data.l_flock.start) {
382 if (new->l_policy_data.l_flock.end <
383 lock->l_policy_data.l_flock.end) {
384 lock->l_policy_data.l_flock.start =
385 new->l_policy_data.l_flock.end + 1;
388 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
391 if (new->l_policy_data.l_flock.end >=
392 lock->l_policy_data.l_flock.end) {
393 lock->l_policy_data.l_flock.end =
394 new->l_policy_data.l_flock.start - 1;
398 /* split the existing lock into two locks */
400 /* if this is an F_UNLCK operation then we could avoid
401 * allocating a new lock and use the req lock passed in
402 * with the request but this would complicate the reply
403 * processing since updates to req get reflected in the
404 * reply. The client side replays the lock request so
405 * it must see the original lock data in the reply. */
407 /* XXX - if ldlm_lock_new() can sleep we should
408 * release the lr_lock, allocate the new lock,
409 * and restart processing this lock. */
411 unlock_res_and_lock(req);
412 new2 = ldlm_lock_create(ns, &res->lr_name, LDLM_FLOCK,
413 lock->l_granted_mode, &null_cbs,
415 lock_res_and_lock(req);
417 ldlm_flock_destroy(req, lock->l_granted_mode,
420 RETURN(LDLM_ITER_STOP);
427 new2->l_granted_mode = lock->l_granted_mode;
428 new2->l_policy_data.l_flock.pid =
429 new->l_policy_data.l_flock.pid;
430 new2->l_policy_data.l_flock.owner =
431 new->l_policy_data.l_flock.owner;
432 new2->l_policy_data.l_flock.start =
433 lock->l_policy_data.l_flock.start;
434 new2->l_policy_data.l_flock.end =
435 new->l_policy_data.l_flock.start - 1;
436 lock->l_policy_data.l_flock.start =
437 new->l_policy_data.l_flock.end + 1;
438 new2->l_conn_export = lock->l_conn_export;
439 if (lock->l_export != NULL) {
440 new2->l_export = class_export_lock_get(lock->l_export, new2);
441 if (new2->l_export->exp_lock_hash &&
442 cfs_hlist_unhashed(&new2->l_exp_hash))
443 cfs_hash_add(new2->l_export->exp_lock_hash,
444 &new2->l_remote_handle,
447 if (*flags == LDLM_FL_WAIT_NOREPROC)
448 ldlm_lock_addref_internal_nolock(new2,
449 lock->l_granted_mode);
451 /* insert new2 at lock */
452 ldlm_resource_add_lock(res, ownlocks, new2);
453 LDLM_LOCK_RELEASE(new2);
457 /* if new2 is created but never used, destroy it*/
458 if (splitted == 0 && new2 != NULL)
459 ldlm_lock_destroy_nolock(new2);
461 /* At this point we're granting the lock request. */
462 req->l_granted_mode = req->l_req_mode;
464 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
466 cfs_list_del_init(&req->l_res_link);
467 /* insert new lock before ownlocks in list. */
468 ldlm_resource_add_lock(res, ownlocks, req);
471 if (*flags != LDLM_FL_WAIT_NOREPROC) {
473 /* If this is an unlock, reprocess the waitq and
474 * send completions ASTs for locks that can now be
475 * granted. The only problem with doing this
476 * reprocessing here is that the completion ASTs for
477 * newly granted locks will be sent before the unlock
478 * completion is sent. It shouldn't be an issue. Also
479 * note that ldlm_process_flock_lock() will recurse,
480 * but only once because first_enq will be false from
481 * ldlm_reprocess_queue. */
482 if ((mode == LCK_NL) && overlaps) {
483 CFS_LIST_HEAD(rpc_list);
486 ldlm_reprocess_queue(res, &res->lr_waiting,
489 unlock_res_and_lock(req);
490 rc = ldlm_run_ast_work(ns, &rpc_list,
492 lock_res_and_lock(req);
494 GOTO(restart, -ERESTART);
497 LASSERT(req->l_completion_ast);
498 ldlm_add_ast_work_item(req, NULL, work_list);
502 /* In case we're reprocessing the requested lock we can't destroy
503 * it until after calling ldlm_ast_work_item() above so that lawi()
504 * can bump the reference count on req. Otherwise req could be freed
505 * before the completion AST can be sent. */
507 ldlm_flock_destroy(req, mode, *flags);
509 ldlm_resource_dump(D_INFO, res);
510 RETURN(LDLM_ITER_CONTINUE);
513 struct ldlm_flock_wait_data {
514 struct ldlm_lock *fwd_lock;
519 ldlm_flock_interrupted_wait(void *data)
521 struct ldlm_lock *lock;
524 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
526 /* take lock off the deadlock detection waitq. */
527 ldlm_flock_blocking_unlink(lock);
529 /* client side - set flag to prevent lock from being put on lru list */
530 lock->l_flags |= LDLM_FL_CBPENDING;
536 * Flock completion calback function.
538 * \param lock [in,out]: A lock to be handled
539 * \param flags [in]: flags
540 * \param *data [in]: ldlm_work_cp_ast_lock() will use ldlm_cb_set_arg
542 * \retval 0 : success
543 * \retval <0 : failure
546 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
548 cfs_flock_t *getlk = lock->l_ast_data;
549 struct obd_device *obd;
550 struct obd_import *imp = NULL;
551 struct ldlm_flock_wait_data fwd;
552 struct l_wait_info lwi;
557 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
560 /* Import invalidation. We need to actually release the lock
561 * references being held, so that it can go away. No point in
562 * holding the lock even if app still believes it has it, since
563 * server already dropped it anyway. Only for granted locks too. */
564 if ((lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_LOCAL_ONLY)) ==
565 (LDLM_FL_FAILED|LDLM_FL_LOCAL_ONLY)) {
566 if (lock->l_req_mode == lock->l_granted_mode &&
567 lock->l_granted_mode != LCK_NL &&
569 ldlm_lock_decref_internal(lock, lock->l_req_mode);
571 /* Need to wake up the waiter if we were evicted */
572 cfs_waitq_signal(&lock->l_waitq);
576 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
578 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
579 LDLM_FL_BLOCK_CONV))) {
581 /* mds granted the lock in the reply */
583 /* CP AST RPC: lock get granted, wake it up */
584 cfs_waitq_signal(&lock->l_waitq);
588 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
591 obd = class_exp2obd(lock->l_conn_export);
593 /* if this is a local lock, there is no import */
595 imp = obd->u.cli.cl_import;
598 cfs_spin_lock(&imp->imp_lock);
599 fwd.fwd_generation = imp->imp_generation;
600 cfs_spin_unlock(&imp->imp_lock);
603 lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
605 /* Go to sleep until the lock is granted. */
606 rc = l_wait_event(lock->l_waitq, is_granted_or_cancelled(lock), &lwi);
609 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
615 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT, 10);
617 if (lock->l_destroyed) {
618 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
622 if (lock->l_flags & LDLM_FL_FAILED) {
623 LDLM_DEBUG(lock, "client-side enqueue waking up: failed");
628 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
633 LDLM_DEBUG(lock, "client-side enqueue granted");
635 /* take lock off the deadlock detection waitq. */
636 ldlm_flock_blocking_unlink(lock);
638 lock_res_and_lock(lock);
639 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
640 cfs_list_del_init(&lock->l_res_link);
642 if (flags & LDLM_FL_TEST_LOCK) {
643 /* fcntl(F_GETLK) request */
644 /* The old mode was saved in getlk->fl_type so that if the mode
645 * in the lock changes we can decref the appropriate refcount.*/
646 ldlm_flock_destroy(lock, cfs_flock_type(getlk),
647 LDLM_FL_WAIT_NOREPROC);
648 switch (lock->l_granted_mode) {
650 cfs_flock_set_type(getlk, F_RDLCK);
653 cfs_flock_set_type(getlk, F_WRLCK);
656 cfs_flock_set_type(getlk, F_UNLCK);
658 cfs_flock_set_pid(getlk,
659 (pid_t)lock->l_policy_data.l_flock.pid);
660 cfs_flock_set_start(getlk,
661 (loff_t)lock->l_policy_data.l_flock.start);
662 cfs_flock_set_end(getlk,
663 (loff_t)lock->l_policy_data.l_flock.end);
665 int noreproc = LDLM_FL_WAIT_NOREPROC;
667 /* We need to reprocess the lock to do merges or splits
668 * with existing locks owned by this process. */
669 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
671 unlock_res_and_lock(lock);
674 EXPORT_SYMBOL(ldlm_flock_completion_ast);
676 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
677 void *data, int flag)
682 LASSERT(flag == LDLM_CB_CANCELING);
684 /* take lock off the deadlock detection waitq. */
685 ldlm_flock_blocking_unlink(lock);
689 void ldlm_flock_policy_wire18_to_local(const ldlm_wire_policy_data_t *wpolicy,
690 ldlm_policy_data_t *lpolicy)
692 memset(lpolicy, 0, sizeof(*lpolicy));
693 lpolicy->l_flock.start = wpolicy->l_flock.lfw_start;
694 lpolicy->l_flock.end = wpolicy->l_flock.lfw_end;
695 lpolicy->l_flock.pid = wpolicy->l_flock.lfw_pid;
696 /* Compat code, old clients had no idea about owner field and
697 * relied solely on pid for ownership. Introduced in LU-104, 2.1,
699 lpolicy->l_flock.owner = wpolicy->l_flock.lfw_pid;
703 void ldlm_flock_policy_wire21_to_local(const ldlm_wire_policy_data_t *wpolicy,
704 ldlm_policy_data_t *lpolicy)
706 memset(lpolicy, 0, sizeof(*lpolicy));
707 lpolicy->l_flock.start = wpolicy->l_flock.lfw_start;
708 lpolicy->l_flock.end = wpolicy->l_flock.lfw_end;
709 lpolicy->l_flock.pid = wpolicy->l_flock.lfw_pid;
710 lpolicy->l_flock.owner = wpolicy->l_flock.lfw_owner;
713 void ldlm_flock_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
714 ldlm_wire_policy_data_t *wpolicy)
716 memset(wpolicy, 0, sizeof(*wpolicy));
717 wpolicy->l_flock.lfw_start = lpolicy->l_flock.start;
718 wpolicy->l_flock.lfw_end = lpolicy->l_flock.end;
719 wpolicy->l_flock.lfw_pid = lpolicy->l_flock.pid;
720 wpolicy->l_flock.lfw_owner = lpolicy->l_flock.owner;