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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_inodebits.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 * This file contains implementation of IBITS lock type
45 * IBITS lock type contains a bit mask determining various properties of an
46 * object. The meanings of specific bits are specific to the caller and are
47 * opaque to LDLM code.
49 * Locks with intersecting bitmasks and conflicting lock modes (e.g. LCK_PW)
50 * are considered conflicting. See the lock mode compatibility matrix
54 #define DEBUG_SUBSYSTEM S_LDLM
56 #include <lustre_dlm.h>
57 #include <obd_support.h>
58 #include <lustre_lib.h>
59 #include <obd_class.h>
61 #include "ldlm_internal.h"
63 #ifdef HAVE_SERVER_SUPPORT
65 * Determine if the lock is compatible with all locks on the queue.
67 * If \a work_list is provided, conflicting locks are linked there.
68 * If \a work_list is not provided, we exit this function on first conflict.
70 * \retval 0 if there are conflicting locks in the \a queue
71 * \retval 1 if the lock is compatible to all locks in \a queue
73 * IBITS locks in granted queue are organized in bunches of
74 * same-mode/same-bits locks called "skip lists". The First lock in the
75 * bunch contains a pointer to the end of the bunch. This allows us to
76 * skip an entire bunch when iterating the list in search for conflicting
77 * locks if first lock of the bunch is not conflicting with us.
80 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
81 struct list_head *work_list)
83 struct list_head *tmp;
84 struct ldlm_lock *lock;
85 __u64 req_bits = req->l_policy_data.l_inodebits.bits;
89 /* There is no sense in lock with no bits set, I think.
90 * Also, such a lock would be compatible with any other bit lock */
91 LASSERT(req_bits != 0);
93 list_for_each(tmp, queue) {
94 struct list_head *mode_tail;
96 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
98 /* We stop walking the queue if we hit ourselves so we don't
99 * take conflicting locks enqueued after us into account,
100 * or we'd wait forever. */
104 /* last lock in mode group */
105 LASSERT(lock->l_sl_mode.prev != NULL);
106 mode_tail = &list_entry(lock->l_sl_mode.prev,
108 l_sl_mode)->l_res_link;
110 /* if reqest lock is not COS_INCOMPAT and COS is disabled,
111 * they are compatible, IOW this request is from a local
112 * transaction on a DNE system. */
113 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
114 !ldlm_is_cos_enabled(req)) {
115 /* jump to last lock in mode group */
120 /* locks' mode are compatible, bits don't matter */
121 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
122 /* jump to last lock in mode group */
128 struct list_head *head;
130 /* Advance loop cursor to last lock in policy group. */
131 tmp = &list_entry(lock->l_sl_policy.prev,
133 l_sl_policy)->l_res_link;
135 /* Locks with overlapping bits conflict. */
136 if (lock->l_policy_data.l_inodebits.bits & req_bits) {
137 /* COS lock mode has a special compatibility
138 * requirement: it is only compatible with
139 * locks from the same client. */
140 if (lock->l_req_mode == LCK_COS &&
141 !ldlm_is_cos_incompat(req) &&
142 ldlm_is_cos_enabled(req) &&
143 lock->l_client_cookie == req->l_client_cookie)
144 goto not_conflicting;
145 /* Found a conflicting policy group. */
151 /* Add locks of the policy group to @work_list
152 * as blocking locks for @req */
153 if (lock->l_blocking_ast)
154 ldlm_add_ast_work_item(lock, req,
156 head = &lock->l_sl_policy;
157 list_for_each_entry(lock, head, l_sl_policy)
158 if (lock->l_blocking_ast)
159 ldlm_add_ast_work_item(lock, req,
163 if (tmp == mode_tail)
167 lock = list_entry(tmp, struct ldlm_lock,
169 } /* Loop over policy groups within one mode group. */
170 } /* Loop over mode groups within @queue. */
176 * Process a granting attempt for IBITS lock.
177 * Must be called with ns lock held
179 * This function looks for any conflicts for \a lock in the granted or
180 * waiting queues. The lock is granted if no conflicts are found in
183 * If \a first_enq is 0 (ie, called from ldlm_reprocess_queue):
184 * - blocking ASTs have already been sent
186 * If \a first_enq is 1 (ie, called from ldlm_lock_enqueue):
187 * - blocking ASTs have not been sent yet, so list of conflicting locks
188 * would be collected and ASTs sent.
190 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
191 int first_enq, enum ldlm_error *err,
192 struct list_head *work_list)
194 struct ldlm_resource *res = lock->l_resource;
195 struct list_head rpc_list;
199 LASSERT(lock->l_granted_mode != lock->l_req_mode);
200 LASSERT(list_empty(&res->lr_converting));
201 INIT_LIST_HEAD(&rpc_list);
202 check_res_locked(res);
204 /* (*flags & LDLM_FL_BLOCK_NOWAIT) is for layout lock right now. */
205 if (!first_enq || (*flags & LDLM_FL_BLOCK_NOWAIT)) {
206 *err = ELDLM_LOCK_ABORTED;
207 if (*flags & LDLM_FL_BLOCK_NOWAIT)
208 *err = ELDLM_LOCK_WOULDBLOCK;
210 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
212 RETURN(LDLM_ITER_STOP);
213 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
215 RETURN(LDLM_ITER_STOP);
217 ldlm_resource_unlink_lock(lock);
218 ldlm_grant_lock(lock, work_list);
221 RETURN(LDLM_ITER_CONTINUE);
225 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
226 rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
229 /* If either of the compat_queue()s returned 0, then we
230 * have ASTs to send and must go onto the waiting list.
232 * bug 2322: we used to unlink and re-add here, which was a
233 * terrible folly -- if we goto restart, we could get
234 * re-ordered! Causes deadlock, because ASTs aren't sent! */
235 if (list_empty(&lock->l_res_link))
236 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
238 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
243 *flags |= LDLM_FL_BLOCK_GRANTED;
245 ldlm_resource_unlink_lock(lock);
246 ldlm_grant_lock(lock, NULL);
250 #endif /* HAVE_SERVER_SUPPORT */
252 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
253 union ldlm_policy_data *lpolicy)
255 memset(lpolicy, 0, sizeof(*lpolicy));
256 lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
259 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
260 union ldlm_wire_policy_data *wpolicy)
262 memset(wpolicy, 0, sizeof(*wpolicy));
263 wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;