4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003 Hewlett-Packard Development Company LP.
24 * Developed under the sponsorship of the US Government under
25 * Subcontract No. B514193
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2017, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
37 * This file implements POSIX lock type for Lustre.
38 * Its policy properties are start and end of extent and PID.
40 * These locks are only done through MDS due to POSIX semantics requiring
41 * e.g. that locks could be only partially released and as such split into
42 * two parts, and also that two adjacent locks from the same process may be
43 * merged into a single wider lock.
45 * Lock modes are mapped like this:
46 * PR and PW for READ and WRITE locks
47 * NL to request a releasing of a portion of the lock
49 * These flock locks never timeout.
52 #define DEBUG_SUBSYSTEM S_LDLM
54 #include <linux/list.h>
55 #include <lustre_dlm.h>
56 #include <obd_support.h>
57 #include <obd_class.h>
58 #include <lustre_lib.h>
60 #include "ldlm_internal.h"
62 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
63 void *data, int flag);
66 * list_for_remaining_safe - iterate over the remaining entries in a list
67 * and safeguard against removal of a list entry.
68 * \param pos the &struct list_head to use as a loop counter. pos MUST
69 * have been initialized prior to using it in this macro.
70 * \param n another &struct list_head to use as temporary storage
71 * \param head the head for your list.
73 #define list_for_remaining_safe(pos, n, head) \
74 for (n = pos->next; pos != (head); pos = n, n = pos->next)
77 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
79 return ((new->l_policy_data.l_flock.owner ==
80 lock->l_policy_data.l_flock.owner) &&
81 (new->l_export == lock->l_export));
85 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
87 return ((new->l_policy_data.l_flock.start <=
88 lock->l_policy_data.l_flock.end) &&
89 (new->l_policy_data.l_flock.end >=
90 lock->l_policy_data.l_flock.start));
93 static inline void ldlm_flock_blocking_link(struct ldlm_lock *req,
94 struct ldlm_lock *lock)
97 if (req->l_export == NULL)
100 LASSERT(hlist_unhashed(&req->l_exp_flock_hash));
102 req->l_policy_data.l_flock.blocking_owner =
103 lock->l_policy_data.l_flock.owner;
104 req->l_policy_data.l_flock.blocking_export =
106 atomic_set(&req->l_policy_data.l_flock.blocking_refs, 0);
108 cfs_hash_add(req->l_export->exp_flock_hash,
109 &req->l_policy_data.l_flock.owner,
110 &req->l_exp_flock_hash);
113 static inline void ldlm_flock_blocking_unlink(struct ldlm_lock *req)
115 /* For server only */
116 if (req->l_export == NULL)
119 check_res_locked(req->l_resource);
120 if (req->l_export->exp_flock_hash != NULL &&
121 !hlist_unhashed(&req->l_exp_flock_hash))
122 cfs_hash_del(req->l_export->exp_flock_hash,
123 &req->l_policy_data.l_flock.owner,
124 &req->l_exp_flock_hash);
128 ldlm_flock_destroy(struct ldlm_lock *lock, enum ldlm_mode mode, __u64 flags)
132 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: %#llx)",
135 /* Safe to not lock here, since it should be empty anyway */
136 LASSERT(hlist_unhashed(&lock->l_exp_flock_hash));
138 list_del_init(&lock->l_res_link);
139 if (flags == LDLM_FL_WAIT_NOREPROC) {
140 /* client side - set a flag to prevent sending a CANCEL */
141 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
143 /* when reaching here, it is under lock_res_and_lock(). Thus,
144 * need call the nolock version of ldlm_lock_decref_internal
146 ldlm_lock_decref_internal_nolock(lock, mode);
149 ldlm_lock_destroy_nolock(lock);
153 #ifdef HAVE_SERVER_SUPPORT
155 * POSIX locks deadlock detection code.
157 * Given a new lock \a req and an existing lock \a bl_lock it conflicts
158 * with, we need to iterate through all blocked POSIX locks for this
159 * export and see if there is a deadlock condition arising. (i.e. when
160 * one client holds a lock on something and want a lock on something
161 * else and at the same time another client has the opposite situation).
164 struct ldlm_flock_lookup_cb_data {
166 struct ldlm_lock *lock;
167 struct obd_export *exp;
170 static int ldlm_flock_lookup_cb(struct obd_export *exp, void *data)
172 struct ldlm_flock_lookup_cb_data *cb_data = data;
173 struct ldlm_lock *lock;
178 lock = cfs_hash_lookup(exp->exp_flock_hash, cb_data->bl_owner);
182 /* Stop on first found lock. Same process can't sleep twice */
183 cb_data->lock = lock;
184 cb_data->exp = class_export_get(exp);
190 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *bl_lock)
192 struct obd_export *req_exp = req->l_export;
193 struct obd_export *bl_exp = bl_lock->l_export;
194 __u64 req_owner = req->l_policy_data.l_flock.owner;
195 __u64 bl_owner = bl_lock->l_policy_data.l_flock.owner;
197 /* For server only */
201 class_export_get(bl_exp);
203 struct ldlm_flock_lookup_cb_data cb_data = {
204 .bl_owner = &bl_owner,
208 struct ptlrpc_connection *bl_exp_conn;
209 struct obd_export *bl_exp_new;
210 struct ldlm_lock *lock = NULL;
211 struct ldlm_flock *flock;
213 bl_exp_conn = bl_exp->exp_connection;
214 if (bl_exp->exp_flock_hash != NULL) {
217 found = obd_nid_export_for_each(bl_exp->exp_obd,
218 &bl_exp_conn->c_peer.nid,
219 ldlm_flock_lookup_cb,
227 class_export_put(bl_exp);
228 bl_exp = cb_data.exp;
230 LASSERT(req != lock);
231 flock = &lock->l_policy_data.l_flock;
232 LASSERT(flock->owner == bl_owner);
233 bl_owner = flock->blocking_owner;
234 bl_exp_new = class_export_get(flock->blocking_export);
235 class_export_put(bl_exp);
237 cfs_hash_put(bl_exp->exp_flock_hash, &lock->l_exp_flock_hash);
240 if (bl_exp->exp_failed)
243 if (bl_owner == req_owner &&
244 nid_same(&bl_exp_conn->c_peer.nid,
245 &req_exp->exp_connection->c_peer.nid)) {
246 class_export_put(bl_exp);
250 class_export_put(bl_exp);
255 static void ldlm_flock_cancel_on_deadlock(struct ldlm_lock *lock,
256 struct list_head *work_list)
258 CDEBUG(D_INFO, "reprocess deadlock req=%p\n", lock);
260 if ((exp_connect_flags(lock->l_export) &
261 OBD_CONNECT_FLOCK_DEAD) == 0) {
262 CERROR("deadlock found, but client doesn't support flock canceliation\n");
264 LASSERT(lock->l_completion_ast);
265 LASSERT(!ldlm_is_ast_sent(lock));
266 lock->l_flags |= (LDLM_FL_AST_SENT | LDLM_FL_CANCEL_ON_BLOCK |
267 LDLM_FL_FLOCK_DEADLOCK);
268 ldlm_flock_blocking_unlink(lock);
269 ldlm_resource_unlink_lock(lock);
270 ldlm_add_ast_work_item(lock, NULL, work_list);
273 #endif /* HAVE_SERVER_SUPPORT */
276 * Process a granting attempt for flock lock.
277 * Must be called under ns lock held.
279 * This function looks for any conflicts for \a lock in the granted or
280 * waiting queues. The lock is granted if no conflicts are found in
284 ldlm_process_flock_lock(struct ldlm_lock *req, __u64 *flags,
285 enum ldlm_process_intention intention,
286 enum ldlm_error *err, struct list_head *work_list)
288 struct ldlm_resource *res = req->l_resource;
289 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
290 struct list_head *tmp;
291 struct list_head *ownlocks = NULL;
292 struct ldlm_lock *lock = NULL;
293 struct ldlm_lock *new = req;
294 struct ldlm_lock *new2 = NULL;
295 enum ldlm_mode mode = req->l_req_mode;
296 int local = ns_is_client(ns);
297 int added = (mode == LCK_NL);
300 const struct ldlm_callback_suite null_cbs = { NULL };
301 #ifdef HAVE_SERVER_SUPPORT
302 struct list_head *grant_work = (intention == LDLM_PROCESS_ENQUEUE ?
307 CDEBUG(D_DLMTRACE, "flags %#llx owner %llu pid %u mode %u start "
308 "%llu end %llu\n", *flags,
309 new->l_policy_data.l_flock.owner,
310 new->l_policy_data.l_flock.pid, mode,
311 req->l_policy_data.l_flock.start,
312 req->l_policy_data.l_flock.end);
317 /* No blocking ASTs are sent to the clients for
318 * Posix file & record locks
320 req->l_blocking_ast = NULL;
322 /* Called on the server for lock cancels. */
323 req->l_blocking_ast = ldlm_flock_blocking_ast;
327 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
328 /* This loop determines where this processes locks start
329 * in the resource lr_granted list.
331 list_for_each(tmp, &res->lr_granted) {
332 lock = list_entry(tmp, struct ldlm_lock,
334 if (ldlm_same_flock_owner(lock, req)) {
340 #ifdef HAVE_SERVER_SUPPORT
342 int reprocess_failed = 0;
343 lockmode_verify(mode);
345 /* This loop determines if there are existing locks
346 * that conflict with the new lock request.
348 list_for_each(tmp, &res->lr_granted) {
349 lock = list_entry(tmp, struct ldlm_lock,
352 if (ldlm_same_flock_owner(lock, req)) {
358 if (req->l_req_mode == LCK_PR &&
359 lock->l_granted_mode == LCK_PR &&
360 lock->l_policy_data.l_flock.start <=
361 req->l_policy_data.l_flock.start &&
362 lock->l_policy_data.l_flock.end >=
363 req->l_policy_data.l_flock.end) {
364 /* there can't be granted WR lock */
367 /* locks are compatible, overlap doesn't matter */
368 if (lockmode_compat(lock->l_granted_mode, mode))
371 if (!ldlm_flocks_overlap(lock, req))
374 if (intention != LDLM_PROCESS_ENQUEUE) {
375 if (ldlm_flock_deadlock(req, lock)) {
376 ldlm_flock_cancel_on_deadlock(
378 RETURN(LDLM_ITER_CONTINUE);
380 reprocess_failed = 1;
384 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
385 ldlm_flock_destroy(req, mode, *flags);
387 RETURN(LDLM_ITER_STOP);
390 if (*flags & LDLM_FL_TEST_LOCK) {
391 ldlm_flock_destroy(req, mode, *flags);
392 req->l_req_mode = lock->l_granted_mode;
393 req->l_policy_data.l_flock.pid =
394 lock->l_policy_data.l_flock.pid;
395 req->l_policy_data.l_flock.start =
396 lock->l_policy_data.l_flock.start;
397 req->l_policy_data.l_flock.end =
398 lock->l_policy_data.l_flock.end;
399 *flags |= LDLM_FL_LOCK_CHANGED;
400 RETURN(LDLM_ITER_STOP);
403 /* add lock to blocking list before deadlock
404 * check to prevent race
406 ldlm_flock_blocking_link(req, lock);
408 if (ldlm_flock_deadlock(req, lock)) {
409 ldlm_flock_blocking_unlink(req);
410 ldlm_flock_destroy(req, mode, *flags);
412 RETURN(LDLM_ITER_STOP);
415 ldlm_resource_add_lock(res, &res->lr_waiting, req);
416 *flags |= LDLM_FL_BLOCK_GRANTED;
417 RETURN(LDLM_ITER_STOP);
419 if (reprocess_failed)
420 RETURN(LDLM_ITER_CONTINUE);
423 if (*flags & LDLM_FL_TEST_LOCK) {
424 ldlm_flock_destroy(req, mode, *flags);
425 req->l_req_mode = LCK_NL;
426 *flags |= LDLM_FL_LOCK_CHANGED;
427 RETURN(LDLM_ITER_STOP);
430 /* In case we had slept on this lock request take it off of the
431 * deadlock detection hash list.
433 ldlm_flock_blocking_unlink(req);
434 #endif /* HAVE_SERVER_SUPPORT */
436 /* Scan the locks owned by this process that overlap this request.
437 * We may have to merge or split existing locks.
440 ownlocks = &res->lr_granted;
442 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
443 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
445 if (!ldlm_same_flock_owner(lock, new))
448 if (lock->l_granted_mode == mode) {
449 /* If the modes are the same then we need to process
450 * locks that overlap OR adjoin the new lock. The extra
451 * logic condition is necessary to deal with arithmetic
452 * overflow and underflow.
454 if ((new->l_policy_data.l_flock.start >
455 (lock->l_policy_data.l_flock.end + 1))
456 && (lock->l_policy_data.l_flock.end !=
460 if ((new->l_policy_data.l_flock.end <
461 (lock->l_policy_data.l_flock.start - 1))
462 && (lock->l_policy_data.l_flock.start != 0))
465 if (new->l_policy_data.l_flock.start <
466 lock->l_policy_data.l_flock.start) {
467 lock->l_policy_data.l_flock.start =
468 new->l_policy_data.l_flock.start;
470 new->l_policy_data.l_flock.start =
471 lock->l_policy_data.l_flock.start;
474 if (new->l_policy_data.l_flock.end >
475 lock->l_policy_data.l_flock.end) {
476 lock->l_policy_data.l_flock.end =
477 new->l_policy_data.l_flock.end;
479 new->l_policy_data.l_flock.end =
480 lock->l_policy_data.l_flock.end;
484 ldlm_flock_destroy(lock, mode, *flags);
492 if (new->l_policy_data.l_flock.start >
493 lock->l_policy_data.l_flock.end)
496 if (new->l_policy_data.l_flock.end <
497 lock->l_policy_data.l_flock.start)
502 if (new->l_policy_data.l_flock.start <=
503 lock->l_policy_data.l_flock.start) {
504 if (new->l_policy_data.l_flock.end <
505 lock->l_policy_data.l_flock.end) {
506 lock->l_policy_data.l_flock.start =
507 new->l_policy_data.l_flock.end + 1;
510 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
513 if (new->l_policy_data.l_flock.end >=
514 lock->l_policy_data.l_flock.end) {
515 lock->l_policy_data.l_flock.end =
516 new->l_policy_data.l_flock.start - 1;
520 /* split the existing lock into two locks */
522 /* if this is an F_UNLCK operation then we could avoid
523 * allocating a new lock and use the req lock passed in
524 * with the request but this would complicate the reply
525 * processing since updates to req get reflected in the
526 * reply. The client side replays the lock request so
527 * it must see the original lock data in the reply.
530 /* XXX - if ldlm_lock_new() can sleep we should
531 * release the lr_lock, allocate the new lock,
532 * and restart processing this lock.
535 unlock_res_and_lock(req);
536 new2 = ldlm_lock_create(ns, &res->lr_name, LDLM_FLOCK,
537 lock->l_granted_mode, &null_cbs,
538 NULL, 0, LVB_T_NONE);
539 lock_res_and_lock(req);
541 ldlm_flock_destroy(req, lock->l_granted_mode,
543 *err = PTR_ERR(new2);
544 RETURN(LDLM_ITER_STOP);
551 new2->l_granted_mode = lock->l_granted_mode;
552 new2->l_policy_data.l_flock.pid =
553 new->l_policy_data.l_flock.pid;
554 new2->l_policy_data.l_flock.owner =
555 new->l_policy_data.l_flock.owner;
556 new2->l_policy_data.l_flock.start =
557 lock->l_policy_data.l_flock.start;
558 new2->l_policy_data.l_flock.end =
559 new->l_policy_data.l_flock.start - 1;
560 lock->l_policy_data.l_flock.start =
561 new->l_policy_data.l_flock.end + 1;
562 new2->l_conn_export = lock->l_conn_export;
563 if (lock->l_export != NULL) {
564 new2->l_export = class_export_lock_get(lock->l_export,
566 if (new2->l_export->exp_lock_hash &&
567 hlist_unhashed(&new2->l_exp_hash))
568 cfs_hash_add(new2->l_export->exp_lock_hash,
569 &new2->l_remote_handle,
572 if (*flags == LDLM_FL_WAIT_NOREPROC)
573 ldlm_lock_addref_internal_nolock(new2,
574 lock->l_granted_mode);
576 /* insert new2 at lock */
577 ldlm_resource_add_lock(res, ownlocks, new2);
578 LDLM_LOCK_RELEASE(new2);
582 /* if new2 is created but never used, destroy it*/
583 if (splitted == 0 && new2 != NULL)
584 ldlm_lock_destroy_nolock(new2);
586 /* At this point we're granting the lock request. */
587 req->l_granted_mode = req->l_req_mode;
589 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
591 list_del_init(&req->l_res_link);
592 /* insert new lock before ownlocks in list. */
593 ldlm_resource_add_lock(res, ownlocks, req);
596 if (*flags != LDLM_FL_WAIT_NOREPROC) {
597 #ifdef HAVE_SERVER_SUPPORT
598 if (intention == LDLM_PROCESS_ENQUEUE) {
599 /* If this is an unlock, reprocess the waitq and
600 * send completions ASTs for locks that can now be
601 * granted. The only problem with doing this
602 * reprocessing here is that the completion ASTs for
603 * newly granted locks will be sent before the unlock
604 * completion is sent. It shouldn't be an issue. Also
605 * note that ldlm_process_flock_lock() will recurse,
606 * but only once because 'intention' won't be
607 * LDLM_PROCESS_ENQUEUE from ldlm_reprocess_queue.
609 if ((mode == LCK_NL) && overlaps) {
614 ldlm_reprocess_queue(res, &res->lr_waiting,
616 LDLM_PROCESS_RESCAN, 0);
618 unlock_res_and_lock(req);
619 rc = ldlm_run_ast_work(ns, &rpc_list,
621 lock_res_and_lock(req);
626 LASSERT(req->l_completion_ast);
627 ldlm_add_ast_work_item(req, NULL, grant_work);
629 #else /* !HAVE_SERVER_SUPPORT */
630 /* The only one possible case for client-side calls flock
631 * policy function is ldlm_flock_completion_ast inside which
632 * carries LDLM_FL_WAIT_NOREPROC flag.
634 CERROR("Illegal parameter for client-side-only module.\n");
636 #endif /* HAVE_SERVER_SUPPORT */
639 /* In case we're reprocessing the requested lock we can't destroy
640 * it until after calling ldlm_add_ast_work_item() above so that laawi()
641 * can bump the reference count on \a req. Otherwise \a req
642 * could be freed before the completion AST can be sent.
645 ldlm_flock_destroy(req, mode, *flags);
647 ldlm_resource_dump(D_INFO, res);
648 RETURN(LDLM_ITER_CONTINUE);
652 * Flock completion callback function.
654 * \param lock [in,out]: A lock to be handled
655 * \param flags [in]: flags
656 * \param *data [in]: ldlm_work_cp_ast_lock() will use ldlm_cb_set_arg
658 * \retval 0 : success
659 * \retval <0 : failure
662 ldlm_flock_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
664 struct file_lock *getlk = lock->l_ast_data;
665 struct obd_device *obd;
670 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT2, 4);
671 if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT3)) {
672 lock_res_and_lock(lock);
673 lock->l_flags |= LDLM_FL_FAIL_LOC;
674 unlock_res_and_lock(lock);
675 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT3, 4);
677 CDEBUG(D_DLMTRACE, "flags: %#llx data: %p getlk: %p\n",
680 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
682 if (flags & LDLM_FL_FAILED)
685 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
687 /* mds granted the lock in the reply */
689 /* CP AST RPC: lock get granted, wake it up */
690 wake_up(&lock->l_waitq);
695 "client-side enqueue returned a blocked lock, sleeping");
696 obd = class_exp2obd(lock->l_conn_export);
698 /* Go to sleep until the lock is granted. */
699 rc = l_wait_event_abortable(lock->l_waitq,
700 is_granted_or_cancelled(lock));
702 /* take lock off the deadlock detection hash list. */
703 lock_res_and_lock(lock);
704 ldlm_flock_blocking_unlink(lock);
706 /* client side - set flag to prevent lock from being
709 ldlm_set_cbpending(lock);
710 unlock_res_and_lock(lock);
712 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
718 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT, 10);
720 if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT4)) {
721 lock_res_and_lock(lock);
722 /* DEADLOCK is always set with CBPENDING */
723 lock->l_flags |= LDLM_FL_FLOCK_DEADLOCK | LDLM_FL_CBPENDING;
724 unlock_res_and_lock(lock);
725 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT4, 4);
727 if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT5)) {
728 lock_res_and_lock(lock);
729 /* DEADLOCK is always set with CBPENDING */
730 lock->l_flags |= (LDLM_FL_FAIL_LOC |
731 LDLM_FL_FLOCK_DEADLOCK | LDLM_FL_CBPENDING);
732 unlock_res_and_lock(lock);
733 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT5, 4);
736 lock_res_and_lock(lock);
739 /* Protect against race where lock could have been just destroyed
740 * due to overlap in ldlm_process_flock_lock().
742 if (ldlm_is_destroyed(lock)) {
743 unlock_res_and_lock(lock);
744 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
746 /* An error is still to be returned, to propagate it up to
747 * ldlm_cli_enqueue_fini() caller. */
751 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
752 ldlm_resource_unlink_lock(lock);
754 /* Import invalidation. We need to actually release the lock
755 * references being held, so that it can go away. No point in
756 * holding the lock even if app still believes it has it, since
757 * server already dropped it anyway. Only for granted locks too.
759 /* Do the same for DEADLOCK'ed locks. */
760 if (ldlm_is_failed(lock) || ldlm_is_flock_deadlock(lock)) {
763 if (flags & LDLM_FL_TEST_LOCK)
764 LASSERT(ldlm_is_test_lock(lock));
766 if (ldlm_is_test_lock(lock) || ldlm_is_flock_deadlock(lock))
767 mode = getlk->fl_type;
769 mode = lock->l_req_mode;
771 if (ldlm_is_flock_deadlock(lock)) {
772 LDLM_DEBUG(lock, "client-side enqueue deadlock "
776 ldlm_flock_destroy(lock, mode, LDLM_FL_WAIT_NOREPROC);
777 unlock_res_and_lock(lock);
779 /* Need to wake up the waiter if we were evicted */
780 wake_up(&lock->l_waitq);
782 /* An error is still to be returned, to propagate it up to
783 * ldlm_cli_enqueue_fini() caller.
788 LDLM_DEBUG(lock, "client-side enqueue granted");
790 if (flags & LDLM_FL_TEST_LOCK) {
792 * fcntl(F_GETLK) request
793 * The old mode was saved in getlk->fl_type so that if the mode
794 * in the lock changes we can decref the appropriate refcount.
796 LASSERT(ldlm_is_test_lock(lock));
797 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
798 switch (lock->l_granted_mode) {
800 getlk->fl_type = F_RDLCK;
803 getlk->fl_type = F_WRLCK;
806 getlk->fl_type = F_UNLCK;
808 getlk->fl_pid = (pid_t)lock->l_policy_data.l_flock.pid;
809 getlk->fl_start = (loff_t)lock->l_policy_data.l_flock.start;
810 getlk->fl_end = (loff_t)lock->l_policy_data.l_flock.end;
812 __u64 noreproc = LDLM_FL_WAIT_NOREPROC;
814 /* We need to reprocess the lock to do merges or splits
815 * with existing locks owned by this process.
817 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
819 unlock_res_and_lock(lock);
822 EXPORT_SYMBOL(ldlm_flock_completion_ast);
824 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
825 void *data, int flag)
830 LASSERT(flag == LDLM_CB_CANCELING);
832 /* take lock off the deadlock detection hash list. */
833 lock_res_and_lock(lock);
834 ldlm_flock_blocking_unlink(lock);
835 unlock_res_and_lock(lock);
839 void ldlm_flock_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
840 union ldlm_policy_data *lpolicy)
842 lpolicy->l_flock.start = wpolicy->l_flock.lfw_start;
843 lpolicy->l_flock.end = wpolicy->l_flock.lfw_end;
844 lpolicy->l_flock.pid = wpolicy->l_flock.lfw_pid;
845 lpolicy->l_flock.owner = wpolicy->l_flock.lfw_owner;
848 void ldlm_flock_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
849 union ldlm_wire_policy_data *wpolicy)
851 memset(wpolicy, 0, sizeof(*wpolicy));
852 wpolicy->l_flock.lfw_start = lpolicy->l_flock.start;
853 wpolicy->l_flock.lfw_end = lpolicy->l_flock.end;
854 wpolicy->l_flock.lfw_pid = lpolicy->l_flock.pid;
855 wpolicy->l_flock.lfw_owner = lpolicy->l_flock.owner;
859 * Export handle<->flock hash operations.
862 ldlm_export_flock_hash(struct cfs_hash *hs, const void *key, unsigned mask)
864 return cfs_hash_u64_hash(*(__u64 *)key, mask);
868 ldlm_export_flock_key(struct hlist_node *hnode)
870 struct ldlm_lock *lock;
872 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
873 return &lock->l_policy_data.l_flock.owner;
877 ldlm_export_flock_keycmp(const void *key, struct hlist_node *hnode)
879 return !memcmp(ldlm_export_flock_key(hnode), key, sizeof(__u64));
883 ldlm_export_flock_object(struct hlist_node *hnode)
885 return hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
889 ldlm_export_flock_get(struct cfs_hash *hs, struct hlist_node *hnode)
891 struct ldlm_lock *lock;
892 struct ldlm_flock *flock;
894 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
897 flock = &lock->l_policy_data.l_flock;
898 LASSERT(flock->blocking_export != NULL);
899 class_export_get(flock->blocking_export);
900 atomic_inc(&flock->blocking_refs);
904 ldlm_export_flock_put(struct cfs_hash *hs, struct hlist_node *hnode)
906 struct ldlm_lock *lock;
907 struct ldlm_flock *flock;
909 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
911 flock = &lock->l_policy_data.l_flock;
912 LASSERT(flock->blocking_export != NULL);
913 class_export_put(flock->blocking_export);
914 if (atomic_dec_and_test(&flock->blocking_refs)) {
915 flock->blocking_owner = 0;
916 flock->blocking_export = NULL;
918 LDLM_LOCK_RELEASE(lock);
921 static struct cfs_hash_ops ldlm_export_flock_ops = {
922 .hs_hash = ldlm_export_flock_hash,
923 .hs_key = ldlm_export_flock_key,
924 .hs_keycmp = ldlm_export_flock_keycmp,
925 .hs_object = ldlm_export_flock_object,
926 .hs_get = ldlm_export_flock_get,
927 .hs_put = ldlm_export_flock_put,
928 .hs_put_locked = ldlm_export_flock_put,
931 int ldlm_init_flock_export(struct obd_export *exp)
933 if( strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDT_NAME) != 0)
936 exp->exp_flock_hash =
937 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
938 HASH_EXP_LOCK_CUR_BITS,
939 HASH_EXP_LOCK_MAX_BITS,
940 HASH_EXP_LOCK_BKT_BITS, 0,
941 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
942 &ldlm_export_flock_ops,
943 CFS_HASH_DEFAULT | CFS_HASH_NBLK_CHANGE);
944 if (!exp->exp_flock_hash)
950 void ldlm_destroy_flock_export(struct obd_export *exp)
953 if (exp->exp_flock_hash) {
954 cfs_hash_putref(exp->exp_flock_hash);
955 exp->exp_flock_hash = NULL;