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
62 * It should iterate through all waiting locks on a given resource queue and
63 * attempt to grant them. An optimization is to check only heads waitintg
64 * locks for each inodebit type.
66 * Must be called with resource lock held.
68 int ldlm_reprocess_inodebits_queue(struct ldlm_resource *res,
69 struct list_head *queue,
70 struct list_head *work_list,
71 enum ldlm_process_intention intention,
72 struct ldlm_lock *hint)
75 int rc = LDLM_ITER_CONTINUE;
77 LIST_HEAD(bl_ast_list);
78 struct ldlm_ibits_queues *queues = res->lr_ibits_queues;
83 check_res_locked(res);
85 LASSERT(res->lr_type == LDLM_IBITS);
86 LASSERT(intention == LDLM_PROCESS_RESCAN ||
87 intention == LDLM_PROCESS_RECOVERY);
89 if (intention == LDLM_PROCESS_RECOVERY)
90 return ldlm_reprocess_queue(res, queue, work_list, intention,
94 CDEBUG(D_DLMTRACE, "--- Reprocess resource "DLDLMRES" (%p)\n",
97 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
99 struct list_head *head = &queues->liq_waiting[i];
100 struct ldlm_lock *pending;
101 struct ldlm_ibits_node *node;
103 if (list_empty(head))
105 if (hint && !(hint->l_policy_data.l_inodebits.bits & BIT(i)))
108 node = list_entry(head->next, struct ldlm_ibits_node,
111 pending = node->lock;
112 LDLM_DEBUG(pending, "Reprocessing lock from queue %d", i);
115 rc = ldlm_process_inodebits_lock(pending, &flags, intention,
117 if (ldlm_is_granted(pending)) {
118 list_splice(&rpc_list, work_list);
119 /* Try to grant more locks from current queue */
122 list_splice(&rpc_list, &bl_ast_list);
126 if (!list_empty(&bl_ast_list)) {
129 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &bl_ast_list,
137 if (!list_empty(&bl_ast_list))
138 ldlm_discard_bl_list(&bl_ast_list);
144 * Determine if the lock is compatible with all locks on the queue.
146 * If \a work_list is provided, conflicting locks are linked there.
147 * If \a work_list is not provided, we exit this function on first conflict.
149 * \retval 0 if there are conflicting locks in the \a queue
150 * \retval 1 if the lock is compatible to all locks in \a queue
152 * IBITS locks in granted queue are organized in bunches of
153 * same-mode/same-bits locks called "skip lists". The First lock in the
154 * bunch contains a pointer to the end of the bunch. This allows us to
155 * skip an entire bunch when iterating the list in search for conflicting
156 * locks if first lock of the bunch is not conflicting with us.
159 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
160 __u64 *ldlm_flags, struct list_head *work_list)
162 enum ldlm_mode req_mode = req->l_req_mode;
163 struct list_head *tmp;
164 struct ldlm_lock *lock;
165 __u64 req_bits = req->l_policy_data.l_inodebits.bits;
166 __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
171 lockmode_verify(req_mode);
173 /* There is no sense in lock with no bits set. Also such a lock
174 * would be compatible with any other bit lock.
175 * Meanwhile that can be true if there were just try_bits and all
176 * are failed, so just exit gracefully and let the caller to care.
178 if ((req_bits | *try_bits) == 0)
181 list_for_each(tmp, queue) {
182 struct list_head *mode_tail;
184 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
186 /* We stop walking the queue if we hit ourselves so we don't
187 * take conflicting locks enqueued after us into account,
188 * or we'd wait forever. */
192 /* last lock in mode group */
193 LASSERT(lock->l_sl_mode.prev != NULL);
194 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
195 l_sl_mode)->l_res_link;
197 /* if request lock is not COS_INCOMPAT and COS is disabled,
198 * they are compatible, IOW this request is from a local
199 * transaction on a DNE system. */
200 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
201 !ldlm_is_cos_enabled(req)) {
202 /* jump to last lock in mode group */
207 if (lockmode_compat(lock->l_req_mode, req_mode)) {
208 /* non group locks are compatible, bits don't matter */
209 if (likely(req_mode != LCK_GROUP)) {
210 /* jump to last lock in mode group */
215 if (req->l_policy_data.l_inodebits.li_gid ==
216 lock->l_policy_data.l_inodebits.li_gid) {
217 if (ldlm_is_granted(lock))
220 if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
221 RETURN(-EWOULDBLOCK);
223 /* Place the same group together */
224 ldlm_resource_insert_lock_after(lock, req);
229 /* GROUP locks are placed to a head of the waiting list, but
231 if (unlikely(req_mode == LCK_GROUP && !ldlm_is_granted(lock))) {
233 if (lock->l_req_mode != LCK_GROUP) {
234 /* Already not a GROUP lock, insert before. */
235 ldlm_resource_insert_lock_before(lock, req);
238 /* Still GROUP but a different gid(the same gid would
239 * be handled above). Keep searching for the same gid */
240 LASSERT(req->l_policy_data.l_inodebits.li_gid !=
241 lock->l_policy_data.l_inodebits.li_gid);
246 struct list_head *head;
248 /* Advance loop cursor to last lock in policy group. */
249 tmp = &list_entry(lock->l_sl_policy.prev,
251 l_sl_policy)->l_res_link;
253 /* New lock's try_bits are filtered out by ibits
254 * of all locks in both granted and waiting queues.
256 *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
257 lock->l_policy_data.l_inodebits.try_bits);
259 if ((req_bits | *try_bits) == 0)
262 /* The new lock ibits is more preferable than try_bits
263 * of waiting locks so drop conflicting try_bits in
265 * Notice that try_bits of granted locks must be zero.
267 lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
269 /* Locks with overlapping bits conflict. */
270 if (lock->l_policy_data.l_inodebits.bits & req_bits) {
271 /* COS lock mode has a special compatibility
272 * requirement: it is only compatible with
273 * locks from the same client. */
274 if (lock->l_req_mode == LCK_COS &&
275 !ldlm_is_cos_incompat(req) &&
276 ldlm_is_cos_enabled(req) &&
277 lock->l_client_cookie == req->l_client_cookie)
282 if (unlikely(lock->l_req_mode == LCK_GROUP)) {
283 LASSERT(ldlm_has_dom(lock));
285 if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
286 RETURN(-EWOULDBLOCK);
291 /* Found a conflicting policy group. */
295 /* Add locks of the policy group to @work_list
296 * as blocking locks for @req */
297 if (lock->l_blocking_ast)
298 ldlm_add_ast_work_item(lock, req,
300 head = &lock->l_sl_policy;
301 list_for_each_entry(lock, head, l_sl_policy)
302 if (lock->l_blocking_ast)
303 ldlm_add_ast_work_item(lock,
307 if (tmp == mode_tail)
311 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
312 } /* Loop over policy groups within one mode group. */
313 } /* Loop over mode groups within @queue. */
319 * Process a granting attempt for IBITS lock.
320 * Must be called with ns lock held
322 * This function looks for any conflicts for \a lock in the granted or
323 * waiting queues. The lock is granted if no conflicts are found in
326 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *ldlm_flags,
327 enum ldlm_process_intention intention,
328 enum ldlm_error *err,
329 struct list_head *work_list)
331 struct ldlm_resource *res = lock->l_resource;
332 struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
337 *err = ELDLM_LOCK_ABORTED;
338 LASSERT(!ldlm_is_granted(lock));
339 check_res_locked(res);
341 if (intention == LDLM_PROCESS_RESCAN) {
342 struct list_head *bl_list =
343 *ldlm_flags & LDLM_FL_BLOCK_NOWAIT ? NULL : work_list;
345 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
347 /* It is possible that some of granted locks was not canceled
348 * but converted and is kept in granted queue. So there is
349 * a window where lock with 'ast_sent' might become granted
350 * again. Meanwhile a new lock may appear in that window and
351 * conflicts with the converted lock so the following scenario
354 * 1) lock1 conflicts with lock2
355 * 2) bl_ast was sent for lock2
356 * 3) lock3 comes and conflicts with lock2 too
357 * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
358 * 5) lock2 was converted for lock1 but not for lock3
359 * 6) lock1 granted, lock3 still is waiting for lock2, but
360 * there will never be another bl_ast for that
362 * To avoid this scenario the work_list is used below to collect
363 * any blocked locks from granted queue during every reprocess
364 * and bl_ast will be sent if needed.
367 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
368 ldlm_flags, bl_list);
370 RETURN(LDLM_ITER_STOP);
371 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
374 RETURN(LDLM_ITER_STOP);
376 /* grant also try_bits if any */
377 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
378 lock->l_policy_data.l_inodebits.bits |=
379 lock->l_policy_data.l_inodebits.try_bits;
380 lock->l_policy_data.l_inodebits.try_bits = 0;
381 *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
383 ldlm_resource_unlink_lock(lock);
384 ldlm_grant_lock(lock, grant_work);
387 RETURN(LDLM_ITER_CONTINUE);
390 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
391 ldlm_flags, work_list);
393 GOTO(out, *err = rc);
396 rc2 = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
397 ldlm_flags, work_list);
399 GOTO(out, *err = rc = rc2);
403 /* if there were only bits to try and all are conflicting */
404 if ((lock->l_policy_data.l_inodebits.bits |
405 lock->l_policy_data.l_inodebits.try_bits)) {
406 /* There is no sense to set LDLM_FL_NO_TIMEOUT to
407 * @ldlm_flags for DOM lock while they are enqueued
408 * through intents, i.e. @lock here is local which does
413 /* grant also all remaining try_bits */
414 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
415 lock->l_policy_data.l_inodebits.bits |=
416 lock->l_policy_data.l_inodebits.try_bits;
417 lock->l_policy_data.l_inodebits.try_bits = 0;
418 *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
420 LASSERT(lock->l_policy_data.l_inodebits.bits);
421 ldlm_resource_unlink_lock(lock);
422 ldlm_grant_lock(lock, grant_work);
426 RETURN(LDLM_ITER_CONTINUE);
430 #endif /* HAVE_SERVER_SUPPORT */
432 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
433 union ldlm_policy_data *lpolicy)
435 lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
436 lpolicy->l_inodebits.li_gid = wpolicy->l_inodebits.li_gid;
438 * try_bits are to be handled outside of generic write_to_local due
439 * to different behavior on a server and client.
443 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
444 union ldlm_wire_policy_data *wpolicy)
446 memset(wpolicy, 0, sizeof(*wpolicy));
447 wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
448 wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
449 wpolicy->l_inodebits.li_gid = lpolicy->l_inodebits.li_gid;
453 * Attempt to convert already granted IBITS lock with several bits set to
454 * a lock with less bits (downgrade).
456 * Such lock conversion is used to keep lock with non-blocking bits instead of
457 * cancelling it, introduced for better support of DoM files.
459 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
463 check_res_locked(lock->l_resource);
465 /* Just return if there are no conflicting bits */
466 if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
467 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
468 lock->l_policy_data.l_inodebits.bits, to_drop);
473 /* remove lock from a skiplist and put in the new place
474 * according with new inodebits */
475 ldlm_resource_unlink_lock(lock);
476 lock->l_policy_data.l_inodebits.bits &= ~to_drop;
477 ldlm_grant_lock_with_skiplist(lock);
480 EXPORT_SYMBOL(ldlm_inodebits_drop);
482 /* convert single lock */
483 int ldlm_cli_inodebits_convert(struct ldlm_lock *lock,
484 enum ldlm_cancel_flags cancel_flags)
486 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
487 struct ldlm_lock_desc ld = { { 0 } };
488 __u64 drop_bits, new_bits;
494 check_res_locked(lock->l_resource);
496 /* Lock is being converted already */
497 if (ldlm_is_converting(lock)) {
498 if (!(cancel_flags & LCF_ASYNC)) {
499 unlock_res_and_lock(lock);
500 wait_event_idle(lock->l_waitq,
501 is_lock_converted(lock));
502 lock_res_and_lock(lock);
507 /* lru_cancel may happen in parallel and call ldlm_cli_cancel_list()
510 if (ldlm_is_canceling(lock))
513 /* no need in only local convert */
514 if (lock->l_flags & (LDLM_FL_LOCAL_ONLY | LDLM_FL_CANCEL_ON_BLOCK))
517 drop_bits = lock->l_policy_data.l_inodebits.cancel_bits;
518 /* no cancel bits - means that caller needs full cancel */
522 new_bits = lock->l_policy_data.l_inodebits.bits & ~drop_bits;
523 /* check if all lock bits are dropped, proceed with cancel */
527 /* check if no dropped bits, consider this as successful convert */
528 if (lock->l_policy_data.l_inodebits.bits == new_bits)
531 ldlm_set_converting(lock);
532 /* Finally call cancel callback for remaining bits only.
533 * It is important to have converting flag during that
534 * so blocking_ast callback can distinguish convert from
537 ld.l_policy_data.l_inodebits.cancel_bits = drop_bits;
538 unlock_res_and_lock(lock);
539 lock->l_blocking_ast(lock, &ld, lock->l_ast_data, LDLM_CB_CANCELING);
540 /* now notify server about convert */
541 rc = ldlm_cli_convert_req(lock, &flags, new_bits);
542 lock_res_and_lock(lock);
544 GOTO(full_cancel, rc);
546 /* Finally clear these bits in lock ibits */
547 ldlm_inodebits_drop(lock, drop_bits);
549 /* Being locked again check if lock was canceled, it is important
550 * to do and don't drop cbpending below
552 if (ldlm_is_canceling(lock))
553 GOTO(full_cancel, rc = -EINVAL);
555 /* also check again if more bits to be cancelled appeared */
556 if (drop_bits != lock->l_policy_data.l_inodebits.cancel_bits)
557 GOTO(clear_converting, rc = -EAGAIN);
559 /* clear cbpending flag early, it is safe to match lock right after
560 * client convert because it is downgrade always.
562 ldlm_clear_cbpending(lock);
563 ldlm_clear_bl_ast(lock);
564 spin_lock(&ns->ns_lock);
565 if (list_empty(&lock->l_lru))
566 ldlm_lock_add_to_lru_nolock(lock);
567 spin_unlock(&ns->ns_lock);
569 /* the job is done, zero the cancel_bits. If more conflicts appear,
570 * it will result in another cycle of ldlm_cli_inodebits_convert().
573 lock->l_policy_data.l_inodebits.cancel_bits = 0;
575 ldlm_clear_converting(lock);
579 int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock)
581 if (ldlm_is_ns_srv(lock)) {
584 OBD_SLAB_ALLOC_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
585 if (lock->l_ibits_node == NULL)
587 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
588 INIT_LIST_HEAD(&lock->l_ibits_node->lin_link[i]);
589 lock->l_ibits_node->lock = lock;
591 lock->l_ibits_node = NULL;
596 void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
597 struct ldlm_lock *lock, bool tail)
601 if (!ldlm_is_ns_srv(lock))
604 if (head == &res->lr_waiting) {
605 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
606 if (!(lock->l_policy_data.l_inodebits.bits & BIT(i)))
609 list_add_tail(&lock->l_ibits_node->lin_link[i],
610 &res->lr_ibits_queues->liq_waiting[i]);
612 list_add(&lock->l_ibits_node->lin_link[i],
613 &res->lr_ibits_queues->liq_waiting[i]);
615 } else if (head == &res->lr_granted && lock->l_ibits_node != NULL) {
616 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
617 LASSERT(list_empty(&lock->l_ibits_node->lin_link[i]));
618 OBD_SLAB_FREE_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
619 lock->l_ibits_node = NULL;
620 } else if (head != &res->lr_granted) {
621 /* we are inserting in a middle of a list, after @head */
622 struct ldlm_lock *orig = list_entry(head, struct ldlm_lock,
624 LASSERT(orig->l_policy_data.l_inodebits.bits ==
625 lock->l_policy_data.l_inodebits.bits);
626 /* The is no a use case to insert before with exactly matched
628 LASSERT(tail == false);
630 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
631 if (!(lock->l_policy_data.l_inodebits.bits & (1 << i)))
633 list_add(&lock->l_ibits_node->lin_link[i],
634 &orig->l_ibits_node->lin_link[i]);
639 void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock)
643 ldlm_unlink_lock_skiplist(lock);
644 if (!ldlm_is_ns_srv(lock))
647 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
648 list_del_init(&lock->l_ibits_node->lin_link[i]);