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/
31 * lustre/ldlm/ldlm_inodebits.c
33 * Author: Peter Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
38 * This file contains implementation of IBITS lock type
40 * IBITS lock type contains a bit mask determining various properties of an
41 * object. The meanings of specific bits are specific to the caller and are
42 * opaque to LDLM code.
44 * Locks with intersecting bitmasks and conflicting lock modes (e.g. LCK_PW)
45 * are considered conflicting. See the lock mode compatibility matrix
49 #define DEBUG_SUBSYSTEM S_LDLM
51 #include <lustre_dlm.h>
52 #include <obd_support.h>
53 #include <lustre_lib.h>
54 #include <obd_class.h>
56 #include "ldlm_internal.h"
58 #ifdef HAVE_SERVER_SUPPORT
61 * It should iterate through all waiting locks on a given resource queue and
62 * attempt to grant them. An optimization is to check only heads waitintg
63 * locks for each inodebit type.
65 * Must be called with resource lock held.
67 int ldlm_reprocess_inodebits_queue(struct ldlm_resource *res,
68 struct list_head *queue,
69 struct list_head *work_list,
70 enum ldlm_process_intention intention,
74 int rc = LDLM_ITER_CONTINUE;
76 LIST_HEAD(bl_ast_list);
77 struct ldlm_ibits_queues *queues = res->lr_ibits_queues;
82 check_res_locked(res);
84 LASSERT(res->lr_type == LDLM_IBITS);
85 LASSERT(intention == LDLM_PROCESS_RESCAN ||
86 intention == LDLM_PROCESS_RECOVERY);
88 if (intention == LDLM_PROCESS_RECOVERY)
89 return ldlm_reprocess_queue(res, queue, work_list, intention,
93 CDEBUG(D_DLMTRACE, "--- Reprocess resource "DLDLMRES" (%p)\n",
96 CDEBUG(D_DLMTRACE, "Hint %llx\n", mask);
98 mask = MDS_INODELOCK_FULL;
100 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
102 struct list_head *head = &queues->liq_waiting[i];
103 struct ldlm_lock *pending;
104 struct ldlm_ibits_node *node;
106 if (list_empty(head) || !(mask & (1 << i)))
109 node = list_entry(head->next, struct ldlm_ibits_node,
112 pending = node->lock;
113 LDLM_DEBUG(pending, "Reprocessing lock from queue %d", i);
116 rc = ldlm_process_inodebits_lock(pending, &flags, intention,
118 if (ldlm_is_granted(pending)) {
119 list_splice(&rpc_list, work_list);
120 mask |= pending->l_policy_data.l_inodebits.bits;
121 i = ffs(pending->l_policy_data.l_inodebits.bits) - 2;
123 list_splice(&rpc_list, &bl_ast_list);
127 if (!list_empty(&bl_ast_list)) {
130 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &bl_ast_list,
134 if (rc == -ERESTART) {
140 if (!list_empty(&bl_ast_list))
141 ldlm_discard_bl_list(&bl_ast_list);
147 * Determine if the lock is compatible with all locks on the queue.
149 * If \a work_list is provided, conflicting locks are linked there.
150 * If \a work_list is not provided, we exit this function on first conflict.
152 * \retval 0 if there are conflicting locks in the \a queue
153 * \retval 1 if the lock is compatible to all locks in \a queue
155 * IBITS locks in granted queue are organized in bunches of
156 * same-mode/same-bits locks called "skip lists". The First lock in the
157 * bunch contains a pointer to the end of the bunch. This allows us to
158 * skip an entire bunch when iterating the list in search for conflicting
159 * locks if first lock of the bunch is not conflicting with us.
162 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
163 __u64 *ldlm_flags, struct list_head *work_list)
165 enum ldlm_mode req_mode = req->l_req_mode;
166 struct list_head *tmp;
167 struct ldlm_lock *lock;
168 __u64 req_bits = req->l_policy_data.l_inodebits.bits;
169 __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
174 lockmode_verify(req_mode);
176 /* There is no sense in lock with no bits set. Also such a lock
177 * would be compatible with any other bit lock.
178 * Meanwhile that can be true if there were just try_bits and all
179 * are failed, so just exit gracefully and let the caller to care.
181 if ((req_bits | *try_bits) == 0)
184 /* Group lock could be only DOM */
185 if (unlikely(req_mode == LCK_GROUP &&
186 (req_bits | *try_bits) != MDS_INODELOCK_DOM))
189 list_for_each(tmp, queue) {
190 struct list_head *mode_tail;
192 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
194 /* We stop walking the queue if we hit ourselves so we don't
195 * take conflicting locks enqueued after us into account,
196 * or we'd wait forever. */
200 /* last lock in mode group */
201 LASSERT(lock->l_sl_mode.prev != NULL);
202 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
203 l_sl_mode)->l_res_link;
205 /* if request lock is not COS_INCOMPAT and COS is disabled,
206 * they are compatible, IOW this request is from a local
207 * transaction on a DNE system. */
208 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
209 !ldlm_is_cos_enabled(req)) {
210 /* jump to last lock in mode group */
215 if (lockmode_compat(lock->l_req_mode, req_mode)) {
216 /* non group locks are compatible, bits don't matter */
217 if (likely(req_mode != LCK_GROUP)) {
218 /* jump to last lock in mode group */
223 if (req->l_policy_data.l_inodebits.li_gid ==
224 lock->l_policy_data.l_inodebits.li_gid) {
225 if (ldlm_is_granted(lock))
228 if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
229 RETURN(-EWOULDBLOCK);
231 /* Place the same group together */
232 ldlm_resource_insert_lock_after(lock, req);
237 /* GROUP locks are placed to a head of the waiting list, but
239 if (unlikely(req_mode == LCK_GROUP && !ldlm_is_granted(lock))) {
241 if (lock->l_req_mode != LCK_GROUP) {
242 /* Already not a GROUP lock, insert before. */
243 ldlm_resource_insert_lock_before(lock, req);
246 /* Still GROUP but a different gid(the same gid would
247 * be handled above). Keep searching for the same gid */
248 LASSERT(req->l_policy_data.l_inodebits.li_gid !=
249 lock->l_policy_data.l_inodebits.li_gid);
254 struct list_head *head;
256 /* Advance loop cursor to last lock in policy group. */
257 tmp = &list_entry(lock->l_sl_policy.prev,
259 l_sl_policy)->l_res_link;
261 /* New lock's try_bits are filtered out by ibits
262 * of all locks in both granted and waiting queues.
264 *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
265 lock->l_policy_data.l_inodebits.try_bits);
267 if ((req_bits | *try_bits) == 0)
270 /* The new lock ibits is more preferable than try_bits
271 * of waiting locks so drop conflicting try_bits in
273 * Notice that try_bits of granted locks must be zero.
275 lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
277 /* Locks with overlapping bits conflict. */
278 if (lock->l_policy_data.l_inodebits.bits & req_bits) {
279 /* COS lock mode has a special compatibility
280 * requirement: it is only compatible with
281 * locks from the same client. */
282 if (lock->l_req_mode == LCK_COS &&
283 !ldlm_is_cos_incompat(req) &&
284 ldlm_is_cos_enabled(req) &&
285 lock->l_client_cookie == req->l_client_cookie)
290 if (unlikely(lock->l_req_mode == LCK_GROUP)) {
291 LASSERT(ldlm_has_dom(lock));
293 if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
294 RETURN(-EWOULDBLOCK);
296 /* Local combined DOM lock came across
297 * GROUP DOM lock, it makes the thread
298 * to be blocked for a long time, not
299 * allowed, the trybits to be used
302 if (!req->l_export &&
303 (req_bits & MDS_INODELOCK_DOM) &&
304 (req_bits & ~MDS_INODELOCK_DOM))
310 /* Found a conflicting policy group. */
314 /* Add locks of the policy group to @work_list
315 * as blocking locks for @req */
316 if (lock->l_blocking_ast)
317 ldlm_add_ast_work_item(lock, req,
319 head = &lock->l_sl_policy;
320 list_for_each_entry(lock, head, l_sl_policy)
321 if (lock->l_blocking_ast)
322 ldlm_add_ast_work_item(lock,
326 if (tmp == mode_tail)
330 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
331 } /* Loop over policy groups within one mode group. */
332 } /* Loop over mode groups within @queue. */
338 * Process a granting attempt for IBITS lock.
339 * Must be called with ns lock held
341 * This function looks for any conflicts for \a lock in the granted or
342 * waiting queues. The lock is granted if no conflicts are found in
345 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *ldlm_flags,
346 enum ldlm_process_intention intention,
347 enum ldlm_error *err,
348 struct list_head *work_list)
350 struct ldlm_resource *res = lock->l_resource;
351 struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
356 *err = ELDLM_LOCK_ABORTED;
357 LASSERT(!ldlm_is_granted(lock));
358 check_res_locked(res);
360 if (intention == LDLM_PROCESS_RESCAN) {
361 struct list_head *bl_list =
362 *ldlm_flags & LDLM_FL_BLOCK_NOWAIT ? NULL : work_list;
364 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
366 /* It is possible that some of granted locks was not canceled
367 * but converted and is kept in granted queue. So there is
368 * a window where lock with 'ast_sent' might become granted
369 * again. Meanwhile a new lock may appear in that window and
370 * conflicts with the converted lock so the following scenario
373 * 1) lock1 conflicts with lock2
374 * 2) bl_ast was sent for lock2
375 * 3) lock3 comes and conflicts with lock2 too
376 * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
377 * 5) lock2 was converted for lock1 but not for lock3
378 * 6) lock1 granted, lock3 still is waiting for lock2, but
379 * there will never be another bl_ast for that
381 * To avoid this scenario the work_list is used below to collect
382 * any blocked locks from granted queue during every reprocess
383 * and bl_ast will be sent if needed.
386 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
387 ldlm_flags, bl_list);
389 RETURN(LDLM_ITER_STOP);
390 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
393 RETURN(LDLM_ITER_STOP);
395 /* grant also try_bits if any */
396 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
397 lock->l_policy_data.l_inodebits.bits |=
398 lock->l_policy_data.l_inodebits.try_bits;
399 lock->l_policy_data.l_inodebits.try_bits = 0;
400 *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
402 ldlm_resource_unlink_lock(lock);
403 ldlm_grant_lock(lock, grant_work);
406 RETURN(LDLM_ITER_CONTINUE);
409 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
410 ldlm_flags, work_list);
412 GOTO(out, *err = rc);
415 rc2 = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
416 ldlm_flags, work_list);
418 GOTO(out, *err = rc = rc2);
422 /* if there were only bits to try and all are conflicting */
423 if ((lock->l_policy_data.l_inodebits.bits |
424 lock->l_policy_data.l_inodebits.try_bits)) {
425 /* There is no sense to set LDLM_FL_NO_TIMEOUT to
426 * @ldlm_flags for DOM lock while they are enqueued
427 * through intents, i.e. @lock here is local which does
432 /* grant also all remaining try_bits */
433 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
434 lock->l_policy_data.l_inodebits.bits |=
435 lock->l_policy_data.l_inodebits.try_bits;
436 lock->l_policy_data.l_inodebits.try_bits = 0;
437 *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
439 LASSERT(lock->l_policy_data.l_inodebits.bits);
440 ldlm_resource_unlink_lock(lock);
441 ldlm_grant_lock(lock, grant_work);
445 RETURN(LDLM_ITER_CONTINUE);
449 #endif /* HAVE_SERVER_SUPPORT */
451 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
452 union ldlm_policy_data *lpolicy)
454 lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
456 * try_bits and li_gid are to be handled outside of generic
457 * write_to_local due to different behavior on a server and client.
461 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
462 union ldlm_wire_policy_data *wpolicy)
464 memset(wpolicy, 0, sizeof(*wpolicy));
465 wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
466 wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
467 wpolicy->l_inodebits.li_gid = lpolicy->l_inodebits.li_gid;
471 * Attempt to convert already granted IBITS lock with several bits set to
472 * a lock with less bits (downgrade).
474 * Such lock conversion is used to keep lock with non-blocking bits instead of
475 * cancelling it, introduced for better support of DoM files.
477 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
481 check_res_locked(lock->l_resource);
483 /* Just return if there are no conflicting bits */
484 if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
485 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
486 lock->l_policy_data.l_inodebits.bits, to_drop);
491 /* remove lock from a skiplist and put in the new place
492 * according with new inodebits */
493 ldlm_resource_unlink_lock(lock);
494 lock->l_policy_data.l_inodebits.bits &= ~to_drop;
495 ldlm_grant_lock_with_skiplist(lock);
498 EXPORT_SYMBOL(ldlm_inodebits_drop);
500 /* convert single lock */
501 int ldlm_cli_inodebits_convert(struct ldlm_lock *lock,
502 enum ldlm_cancel_flags cancel_flags)
504 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
505 struct ldlm_lock_desc ld = { { 0 } };
506 __u64 drop_bits, new_bits;
512 check_res_locked(lock->l_resource);
514 /* Lock is being converted already */
515 if (ldlm_is_converting(lock)) {
516 if (!(cancel_flags & LCF_ASYNC)) {
517 unlock_res_and_lock(lock);
518 wait_event_idle(lock->l_waitq,
519 is_lock_converted(lock));
520 lock_res_and_lock(lock);
525 /* lru_cancel may happen in parallel and call ldlm_cli_cancel_list()
528 if (ldlm_is_canceling(lock))
531 /* no need in only local convert */
532 if (lock->l_flags & (LDLM_FL_LOCAL_ONLY | LDLM_FL_CANCEL_ON_BLOCK))
535 drop_bits = lock->l_policy_data.l_inodebits.cancel_bits;
536 /* no cancel bits - means that caller needs full cancel */
540 new_bits = lock->l_policy_data.l_inodebits.bits & ~drop_bits;
541 /* check if all lock bits are dropped, proceed with cancel */
545 /* check if no dropped bits, consider this as successful convert */
546 if (lock->l_policy_data.l_inodebits.bits == new_bits)
549 ldlm_set_converting(lock);
550 /* Finally call cancel callback for remaining bits only.
551 * It is important to have converting flag during that
552 * so blocking_ast callback can distinguish convert from
555 ld.l_policy_data.l_inodebits.cancel_bits = drop_bits;
556 unlock_res_and_lock(lock);
557 lock->l_blocking_ast(lock, &ld, lock->l_ast_data, LDLM_CB_CANCELING);
558 /* now notify server about convert */
559 rc = ldlm_cli_convert_req(lock, &flags, new_bits);
560 lock_res_and_lock(lock);
562 GOTO(full_cancel, rc);
564 /* Finally clear these bits in lock ibits */
565 ldlm_inodebits_drop(lock, drop_bits);
567 /* Being locked again check if lock was canceled, it is important
568 * to do and don't drop cbpending below
570 if (ldlm_is_canceling(lock))
571 GOTO(full_cancel, rc = -EINVAL);
573 /* also check again if more bits to be cancelled appeared */
574 if (drop_bits != lock->l_policy_data.l_inodebits.cancel_bits)
575 GOTO(clear_converting, rc = -EAGAIN);
577 /* clear cbpending flag early, it is safe to match lock right after
578 * client convert because it is downgrade always.
580 ldlm_clear_cbpending(lock);
581 ldlm_clear_bl_ast(lock);
582 spin_lock(&ns->ns_lock);
583 if (list_empty(&lock->l_lru))
584 ldlm_lock_add_to_lru_nolock(lock);
585 spin_unlock(&ns->ns_lock);
587 /* the job is done, zero the cancel_bits. If more conflicts appear,
588 * it will result in another cycle of ldlm_cli_inodebits_convert().
591 lock->l_policy_data.l_inodebits.cancel_bits = 0;
593 ldlm_clear_converting(lock);
597 int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock)
599 if (ldlm_is_ns_srv(lock)) {
602 OBD_SLAB_ALLOC_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
603 if (lock->l_ibits_node == NULL)
605 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
606 INIT_LIST_HEAD(&lock->l_ibits_node->lin_link[i]);
607 lock->l_ibits_node->lock = lock;
609 lock->l_ibits_node = NULL;
614 void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
615 struct ldlm_lock *lock, bool tail)
619 if (!ldlm_is_ns_srv(lock))
622 if (head == &res->lr_waiting) {
623 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
624 if (!(lock->l_policy_data.l_inodebits.bits & BIT(i)))
627 list_add_tail(&lock->l_ibits_node->lin_link[i],
628 &res->lr_ibits_queues->liq_waiting[i]);
630 list_add(&lock->l_ibits_node->lin_link[i],
631 &res->lr_ibits_queues->liq_waiting[i]);
633 } else if (head == &res->lr_granted && lock->l_ibits_node != NULL) {
634 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
635 LASSERT(list_empty(&lock->l_ibits_node->lin_link[i]));
636 OBD_SLAB_FREE_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
637 lock->l_ibits_node = NULL;
638 } else if (head != &res->lr_granted) {
639 /* we are inserting in a middle of a list, after @head */
640 struct ldlm_lock *orig = list_entry(head, struct ldlm_lock,
642 LASSERT(orig->l_policy_data.l_inodebits.bits ==
643 lock->l_policy_data.l_inodebits.bits);
644 /* The is no a use case to insert before with exactly matched
646 LASSERT(tail == false);
648 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
649 if (!(lock->l_policy_data.l_inodebits.bits & (1 << i)))
651 list_add(&lock->l_ibits_node->lin_link[i],
652 &orig->l_ibits_node->lin_link[i]);
657 void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock)
661 ldlm_unlink_lock_skiplist(lock);
662 if (!ldlm_is_ns_srv(lock))
665 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
666 list_del_init(&lock->l_ibits_node->lin_link[i]);