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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ldlm/ldlm_inodebits.c
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
39 * This file contains implementation of IBITS lock type
41 * IBITS lock type contains a bit mask determining various properties of an
42 * object. The meanings of specific bits are specific to the caller and are
43 * opaque to LDLM code.
45 * Locks with intersecting bitmasks and conflicting lock modes (e.g. LCK_PW)
46 * are considered conflicting. See the lock mode compatibility matrix
50 #define DEBUG_SUBSYSTEM S_LDLM
52 #include <lustre_dlm.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <obd_class.h>
57 #include "ldlm_internal.h"
59 #ifdef HAVE_SERVER_SUPPORT
61 * Determine if the lock is compatible with all locks on the queue.
63 * If \a work_list is provided, conflicting locks are linked there.
64 * If \a work_list is not provided, we exit this function on first conflict.
66 * \retval 0 if there are conflicting locks in the \a queue
67 * \retval 1 if the lock is compatible to all locks in \a queue
69 * IBITS locks in granted queue are organized in bunches of
70 * same-mode/same-bits locks called "skip lists". The First lock in the
71 * bunch contains a pointer to the end of the bunch. This allows us to
72 * skip an entire bunch when iterating the list in search for conflicting
73 * locks if first lock of the bunch is not conflicting with us.
76 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
77 struct list_head *work_list)
79 struct list_head *tmp;
80 struct ldlm_lock *lock;
81 __u64 req_bits = req->l_policy_data.l_inodebits.bits;
82 __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
87 /* There is no sense in lock with no bits set. Also such a lock
88 * would be compatible with any other bit lock.
89 * Meanwhile that can be true if there were just try_bits and all
90 * are failed, so just exit gracefully and let the caller to care.
92 if ((req_bits | *try_bits) == 0)
95 list_for_each(tmp, queue) {
96 struct list_head *mode_tail;
98 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
100 /* We stop walking the queue if we hit ourselves so we don't
101 * take conflicting locks enqueued after us into account,
102 * or we'd wait forever. */
106 /* last lock in mode group */
107 LASSERT(lock->l_sl_mode.prev != NULL);
108 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
109 l_sl_mode)->l_res_link;
111 /* if request lock is not COS_INCOMPAT and COS is disabled,
112 * they are compatible, IOW this request is from a local
113 * transaction on a DNE system. */
114 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
115 !ldlm_is_cos_enabled(req)) {
116 /* jump to last lock in mode group */
121 /* locks' mode are compatible, bits don't matter */
122 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
123 /* jump to last lock in mode group */
129 struct list_head *head;
131 /* Advance loop cursor to last lock in policy group. */
132 tmp = &list_entry(lock->l_sl_policy.prev,
134 l_sl_policy)->l_res_link;
136 /* New lock's try_bits are filtered out by ibits
137 * of all locks in both granted and waiting queues.
139 *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
140 lock->l_policy_data.l_inodebits.try_bits);
142 if ((req_bits | *try_bits) == 0)
145 /* The new lock ibits is more preferable than try_bits
146 * of waiting locks so drop conflicting try_bits in
148 * Notice that try_bits of granted locks must be zero.
150 lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
152 /* Locks with overlapping bits conflict. */
153 if (lock->l_policy_data.l_inodebits.bits & req_bits) {
154 /* COS lock mode has a special compatibility
155 * requirement: it is only compatible with
156 * locks from the same client. */
157 if (lock->l_req_mode == LCK_COS &&
158 !ldlm_is_cos_incompat(req) &&
159 ldlm_is_cos_enabled(req) &&
160 lock->l_client_cookie == req->l_client_cookie)
161 goto not_conflicting;
163 /* Found a conflicting policy group. */
169 /* Add locks of the policy group to @work_list
170 * as blocking locks for @req */
171 if (lock->l_blocking_ast)
172 ldlm_add_ast_work_item(lock, req,
174 head = &lock->l_sl_policy;
175 list_for_each_entry(lock, head, l_sl_policy)
176 if (lock->l_blocking_ast)
177 ldlm_add_ast_work_item(lock,
181 if (tmp == mode_tail)
185 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
186 } /* Loop over policy groups within one mode group. */
187 } /* Loop over mode groups within @queue. */
193 * Process a granting attempt for IBITS lock.
194 * Must be called with ns lock held
196 * This function looks for any conflicts for \a lock in the granted or
197 * waiting queues. The lock is granted if no conflicts are found in
200 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
201 enum ldlm_process_intention intention,
202 enum ldlm_error *err,
203 struct list_head *work_list)
205 struct ldlm_resource *res = lock->l_resource;
206 struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
212 LASSERT(lock->l_granted_mode != lock->l_req_mode);
213 check_res_locked(res);
215 if (intention == LDLM_PROCESS_RESCAN) {
216 struct list_head *bl_list;
218 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
220 *err = ELDLM_LOCK_WOULDBLOCK;
223 *err = ELDLM_LOCK_ABORTED;
226 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
228 /* It is possible that some of granted locks was not canceled
229 * but converted and is kept in granted queue. So there is
230 * a window where lock with 'ast_sent' might become granted
231 * again. Meanwhile a new lock may appear in that window and
232 * conflicts with the converted lock so the following scenario
235 * 1) lock1 conflicts with lock2
236 * 2) bl_ast was sent for lock2
237 * 3) lock3 comes and conflicts with lock2 too
238 * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
239 * 5) lock2 was converted for lock1 but not for lock3
240 * 6) lock1 granted, lock3 still is waiting for lock2, but
241 * there will never be another bl_ast for that
243 * To avoid this scenario the work_list is used below to collect
244 * any blocked locks from granted queue during every reprocess
245 * and bl_ast will be sent if needed.
247 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
250 RETURN(LDLM_ITER_STOP);
251 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
253 RETURN(LDLM_ITER_STOP);
255 /* grant also try_bits if any */
256 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
257 lock->l_policy_data.l_inodebits.bits |=
258 lock->l_policy_data.l_inodebits.try_bits;
259 lock->l_policy_data.l_inodebits.try_bits = 0;
260 *flags |= LDLM_FL_LOCK_CHANGED;
262 ldlm_resource_unlink_lock(lock);
263 ldlm_grant_lock(lock, grant_work);
266 RETURN(LDLM_ITER_CONTINUE);
269 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, work_list);
270 rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, work_list);
273 /* if there were only bits to try and all are conflicting */
274 if ((lock->l_policy_data.l_inodebits.bits |
275 lock->l_policy_data.l_inodebits.try_bits) == 0) {
276 *err = ELDLM_LOCK_WOULDBLOCK;
281 /* grant also all remaining try_bits */
282 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
283 lock->l_policy_data.l_inodebits.bits |=
284 lock->l_policy_data.l_inodebits.try_bits;
285 lock->l_policy_data.l_inodebits.try_bits = 0;
286 *flags |= LDLM_FL_LOCK_CHANGED;
288 LASSERT(lock->l_policy_data.l_inodebits.bits);
289 ldlm_resource_unlink_lock(lock);
290 ldlm_grant_lock(lock, grant_work);
294 RETURN(LDLM_ITER_CONTINUE);
296 #endif /* HAVE_SERVER_SUPPORT */
298 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
299 union ldlm_policy_data *lpolicy)
301 lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
302 lpolicy->l_inodebits.try_bits = wpolicy->l_inodebits.try_bits;
305 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
306 union ldlm_wire_policy_data *wpolicy)
308 memset(wpolicy, 0, sizeof(*wpolicy));
309 wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
310 wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
314 * Attempt to convert already granted IBITS lock with several bits set to
315 * a lock with less bits (downgrade).
317 * Such lock conversion is used to keep lock with non-blocking bits instead of
318 * cancelling it, introduced for better support of DoM files.
320 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
324 check_res_locked(lock->l_resource);
326 /* Just return if there are no conflicting bits */
327 if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
328 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
329 lock->l_policy_data.l_inodebits.bits, to_drop);
334 /* remove lock from a skiplist and put in the new place
335 * according with new inodebits */
336 ldlm_resource_unlink_lock(lock);
337 lock->l_policy_data.l_inodebits.bits &= ~to_drop;
338 ldlm_grant_lock_with_skiplist(lock);
341 EXPORT_SYMBOL(ldlm_inodebits_drop);
343 /* convert single lock */
344 int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
346 struct lustre_handle lockh;
353 LASSERT(!lock->l_readers && !lock->l_writers);
355 LDLM_DEBUG(lock, "client lock convert START");
357 ldlm_lock2handle(lock, &lockh);
358 lock_res_and_lock(lock);
359 /* check if all bits are blocked */
360 if (!(lock->l_policy_data.l_inodebits.bits & ~drop_bits)) {
361 unlock_res_and_lock(lock);
362 /* return error to continue with cancel */
363 GOTO(exit, rc = -EINVAL);
366 /* check if no common bits, consider this as successful convert */
367 if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
368 unlock_res_and_lock(lock);
372 /* check if there is race with cancel */
373 if (ldlm_is_canceling(lock) || ldlm_is_cancel(lock)) {
374 unlock_res_and_lock(lock);
375 GOTO(exit, rc = -EINVAL);
378 /* clear cbpending flag early, it is safe to match lock right after
379 * client convert because it is downgrade always.
381 ldlm_clear_cbpending(lock);
382 ldlm_clear_bl_ast(lock);
384 /* If lock is being converted already, check drop bits first */
385 if (ldlm_is_converting(lock)) {
386 /* raced lock convert, lock inodebits are remaining bits
387 * so check if they are conflicting with new convert or not.
389 if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
390 unlock_res_and_lock(lock);
393 /* Otherwise drop new conflicting bits in new convert */
395 ldlm_set_converting(lock);
396 /* from all bits of blocking lock leave only conflicting */
397 drop_bits &= lock->l_policy_data.l_inodebits.bits;
398 /* save them in cancel_bits, so l_blocking_ast will know
399 * which bits from the current lock were dropped. */
400 lock->l_policy_data.l_inodebits.cancel_bits = drop_bits;
401 /* Finally clear these bits in lock ibits */
402 ldlm_inodebits_drop(lock, drop_bits);
403 unlock_res_and_lock(lock);
404 /* Finally call cancel callback for remaining bits only.
405 * It is important to have converting flag during that
406 * so blocking_ast callback can distinguish convert from
409 if (lock->l_blocking_ast)
410 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
413 /* now notify server about convert */
414 rc = ldlm_cli_convert(lock, &flags);
416 lock_res_and_lock(lock);
417 if (ldlm_is_converting(lock)) {
418 ldlm_clear_converting(lock);
419 ldlm_set_cbpending(lock);
420 ldlm_set_bl_ast(lock);
422 unlock_res_and_lock(lock);
427 LDLM_DEBUG(lock, "client lock convert END");