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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/ldlm/ldlm_extent.c
33 * Author: Peter Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
38 * This file contains implementation of EXTENT lock type
40 * EXTENT lock type is for locking a contiguous range of values, represented
41 * by 64-bit starting and ending offsets (inclusive). There are several extent
42 * lock modes, some of which may be mutually incompatible. Extent locks are
43 * considered incompatible if their modes are incompatible and their extents
44 * intersect. See the lock mode compatibility matrix in lustre_dlm.h.
47 #define DEBUG_SUBSYSTEM S_LDLM
49 #include <libcfs/libcfs.h>
50 #include <lustre_dlm.h>
51 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_lib.h>
56 #include "ldlm_internal.h"
58 #ifdef HAVE_SERVER_SUPPORT
59 # define LDLM_MAX_GROWN_EXTENT (32 * 1024 * 1024 - 1)
62 * Fix up the ldlm_extent after expanding it.
64 * After expansion has been done, we might still want to do certain adjusting
65 * based on overall contention of the resource and the like to avoid granting
68 static void ldlm_extent_internal_policy_fixup(struct ldlm_lock *req,
69 struct ldlm_extent *new_ex,
72 enum ldlm_mode req_mode = req->l_req_mode;
73 __u64 req_start = req->l_req_extent.start;
74 __u64 req_end = req->l_req_extent.end;
75 __u64 req_align, mask;
77 if (conflicting > 32 && (req_mode == LCK_PW || req_mode == LCK_CW)) {
78 if (req_end < req_start + LDLM_MAX_GROWN_EXTENT)
79 new_ex->end = min(req_start + LDLM_MAX_GROWN_EXTENT,
83 if (new_ex->start == 0 && new_ex->end == OBD_OBJECT_EOF) {
88 /* we need to ensure that the lock extent is properly aligned to what
89 * the client requested. Also we need to make sure it's also server
90 * page size aligned otherwise a server page can be covered by two
94 req_align = (req_end + 1) | req_start;
95 if (req_align != 0 && (req_align & (mask - 1)) == 0) {
96 while ((req_align & mask) == 0)
100 /* We can only shrink the lock, not grow it.
101 * This should never cause lock to be smaller than requested,
102 * since requested lock was already aligned on these boundaries.
104 new_ex->start = ((new_ex->start - 1) | mask) + 1;
105 new_ex->end = ((new_ex->end + 1) & ~mask) - 1;
106 LASSERTF(new_ex->start <= req_start,
107 "mask %#llx grant start %llu req start %llu\n",
108 mask, new_ex->start, req_start);
109 LASSERTF(new_ex->end >= req_end,
110 "mask %#llx grant end %llu req end %llu\n",
111 mask, new_ex->end, req_end);
115 * Return the maximum extent that:
116 * - contains the requested extent
117 * - does not overlap existing conflicting extents outside the requested one
119 * This allows clients to request a small required extent range, but if there
120 * is no contention on the lock the full lock can be granted to the client.
121 * This avoids the need for many smaller lock requests to be granted in the
122 * common (uncontended) case.
124 * Use interval tree to expand the lock extent for granted lock.
126 static void ldlm_extent_internal_policy_granted(struct ldlm_lock *req,
127 struct ldlm_extent *new_ex)
129 struct ldlm_resource *res = req->l_resource;
130 enum ldlm_mode req_mode = req->l_req_mode;
131 __u64 req_start = req->l_req_extent.start;
132 __u64 req_end = req->l_req_extent.end;
133 struct ldlm_interval_tree *tree;
134 struct interval_node_extent limiter = {
135 .start = new_ex->start,
143 lockmode_verify(req_mode);
145 /* Using interval tree to handle the LDLM extent granted locks. */
146 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
147 struct interval_node_extent ext = {
152 tree = &res->lr_itree[idx];
153 if (lockmode_compat(tree->lit_mode, req_mode))
156 conflicting += tree->lit_size;
158 limiter.start = req_start;
160 if (interval_is_overlapped(tree->lit_root, &ext))
162 "req_mode = %d, tree->lit_mode = %d, tree->lit_size = %d\n",
163 req_mode, tree->lit_mode, tree->lit_size);
164 interval_expand(tree->lit_root, &ext, &limiter);
165 limiter.start = max(limiter.start, ext.start);
166 limiter.end = min(limiter.end, ext.end);
167 if (limiter.start == req_start && limiter.end == req_end)
171 new_ex->start = limiter.start;
172 new_ex->end = limiter.end;
173 LASSERT(new_ex->start <= req_start);
174 LASSERT(new_ex->end >= req_end);
176 ldlm_extent_internal_policy_fixup(req, new_ex, conflicting);
180 /* The purpose of this function is to return:
181 * - the maximum extent
182 * - containing the requested extent
183 * - and not overlapping existing conflicting extents outside the requested one
186 ldlm_extent_internal_policy_waiting(struct ldlm_lock *req,
187 struct ldlm_extent *new_ex)
189 struct ldlm_resource *res = req->l_resource;
190 enum ldlm_mode req_mode = req->l_req_mode;
191 __u64 req_start = req->l_req_extent.start;
192 __u64 req_end = req->l_req_extent.end;
193 struct ldlm_lock *lock;
198 lockmode_verify(req_mode);
200 /* for waiting locks */
201 list_for_each_entry(lock, &res->lr_waiting, l_res_link) {
202 struct ldlm_extent *l_extent = &lock->l_policy_data.l_extent;
204 /* We already hit the minimum requested size, search no more */
205 if (new_ex->start == req_start && new_ex->end == req_end) {
210 /* Don't conflict with ourselves */
214 /* Locks are compatible, overlap doesn't matter
215 * Until bug 20 is fixed, try to avoid granting overlapping
216 * locks on one client (they take a long time to cancel)
218 if (lockmode_compat(lock->l_req_mode, req_mode) &&
219 lock->l_export != req->l_export)
222 /* If this is a high-traffic lock, don't grow downwards at all
223 * or grow upwards too much
227 new_ex->start = req_start;
229 /* If lock doesn't overlap new_ex, skip it. */
230 if (!ldlm_extent_overlap(l_extent, new_ex))
233 /* Locks conflicting in requested extents and we can't satisfy
234 * both locks, so ignore it. Either we will ping-pong this
235 * extent (we would regardless of what extent we granted) or
236 * lock is unused and it shouldn't limit our extent growth.
238 if (ldlm_extent_overlap(&lock->l_req_extent,
242 /* We grow extents downwards only as far as they don't overlap
243 * with already-granted locks, on the assumption that clients
244 * will be writing beyond the initial requested end and would
245 * then need to enqueue a new lock beyond previous request.
246 * l_req_extent->end strictly < req_start, checked above.
248 if (l_extent->start < req_start && new_ex->start != req_start) {
249 if (l_extent->end >= req_start)
250 new_ex->start = req_start;
252 new_ex->start = min(l_extent->end+1, req_start);
255 /* If we need to cancel this lock anyways because our request
256 * overlaps the granted lock, we grow up to its requested
257 * extent start instead of limiting this extent, assuming that
258 * clients are writing forwards and the lock had over grown
259 * its extent downwards before we enqueued our request.
261 if (l_extent->end > req_end) {
262 if (l_extent->start <= req_end)
263 new_ex->end = max(lock->l_req_extent.start - 1,
266 new_ex->end = max(l_extent->start - 1, req_end);
270 ldlm_extent_internal_policy_fixup(req, new_ex, conflicting);
275 /* In order to determine the largest possible extent we can grant, we need
276 * to scan all of the queues.
278 static void ldlm_extent_policy(struct ldlm_resource *res,
279 struct ldlm_lock *lock, __u64 *flags)
281 struct ldlm_extent new_ex = { .start = 0, .end = OBD_OBJECT_EOF };
283 if (lock->l_export == NULL)
284 /* this is a local lock taken by server (e.g., as a part of
285 * OST-side locking, or unlink handling). Expansion doesn't
286 * make a lot of sense for local locks, because they are
287 * dropped immediately on operation completion and would only
288 * conflict with other threads.
292 if (lock->l_policy_data.l_extent.start == 0 &&
293 lock->l_policy_data.l_extent.end == OBD_OBJECT_EOF)
294 /* fast-path whole file locks */
297 /* Because reprocess_queue zeroes flags and uses it to return
298 * LDLM_FL_LOCK_CHANGED, we must check for the NO_EXPANSION flag
299 * in the lock flags rather than the 'flags' argument
301 if (likely(!(lock->l_flags & LDLM_FL_NO_EXPANSION))) {
302 ldlm_extent_internal_policy_granted(lock, &new_ex);
303 ldlm_extent_internal_policy_waiting(lock, &new_ex);
305 LDLM_DEBUG(lock, "Not expanding manually requested lock");
306 new_ex.start = lock->l_policy_data.l_extent.start;
307 new_ex.end = lock->l_policy_data.l_extent.end;
308 /* In case the request is not on correct boundaries, we call
309 * fixup. (normally called in ldlm_extent_internal_policy_*)
311 ldlm_extent_internal_policy_fixup(lock, &new_ex, 0);
314 if (!ldlm_extent_equal(&new_ex, &lock->l_policy_data.l_extent)) {
315 *flags |= LDLM_FL_LOCK_CHANGED;
316 lock->l_policy_data.l_extent.start = new_ex.start;
317 lock->l_policy_data.l_extent.end = new_ex.end;
321 static bool ldlm_check_contention(struct ldlm_lock *lock, int contended_locks)
323 struct ldlm_resource *res = lock->l_resource;
324 time64_t now = ktime_get_seconds();
326 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_SET_CONTENTION))
329 CDEBUG(D_DLMTRACE, "contended locks = %d\n", contended_locks);
330 if (contended_locks > ldlm_res_to_ns(res)->ns_contended_locks)
331 res->lr_contention_time = now;
333 return now < res->lr_contention_time +
334 ldlm_res_to_ns(res)->ns_contention_time;
337 struct ldlm_extent_compat_args {
338 struct list_head *work_list;
339 struct ldlm_lock *lock;
345 static enum interval_iter ldlm_extent_compat_cb(struct interval_node *n,
348 struct ldlm_lock *lock = container_of(n, struct ldlm_lock, l_tree_node);
349 struct ldlm_extent_compat_args *priv = data;
350 struct list_head *work_list = priv->work_list;
351 struct ldlm_lock *enq = priv->lock;
352 enum ldlm_mode mode = priv->mode;
355 /* interval tree is for granted lock */
356 LASSERTF(mode == lock->l_granted_mode,
357 "mode = %s, lock->l_granted_mode = %s\n",
359 ldlm_lockname[lock->l_granted_mode]);
360 if (lock->l_blocking_ast && lock->l_granted_mode != LCK_GROUP)
361 ldlm_add_ast_work_item(lock, enq, work_list);
363 /* don't count conflicting glimpse locks */
364 if (!(mode == LCK_PR && lock->l_policy_data.l_extent.start == 0 &&
365 lock->l_policy_data.l_extent.end == OBD_OBJECT_EOF))
371 RETURN(INTERVAL_ITER_CONT);
375 * Determine if the lock is compatible with all locks on the queue.
377 * If \a work_list is provided, conflicting locks are linked there.
378 * If \a work_list is not provided, we exit this function on first conflict.
380 * \retval 0 if the lock is not compatible
381 * \retval 1 if the lock is compatible
382 * \retval 2 if \a req is a group lock and it is compatible and requires
383 * no further checking
384 * \retval negative error, such as EAGAIN for group locks
387 ldlm_extent_compat_queue(struct list_head *queue, struct ldlm_lock *req,
388 __u64 *flags, struct list_head *work_list,
389 int *contended_locks)
391 struct ldlm_resource *res = req->l_resource;
392 enum ldlm_mode req_mode = req->l_req_mode;
393 __u64 req_start = req->l_req_extent.start;
394 __u64 req_end = req->l_req_extent.end;
395 struct ldlm_lock *lock;
396 int check_contention;
401 lockmode_verify(req_mode);
403 /* Using interval tree for granted lock */
404 if (queue == &res->lr_granted) {
405 struct ldlm_interval_tree *tree;
406 struct ldlm_extent_compat_args data = {
407 .work_list = work_list,
409 .locks = contended_locks,
411 struct interval_node_extent ex = {
416 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
417 tree = &res->lr_itree[idx];
418 if (tree->lit_root == NULL) /* empty tree, skipped */
421 data.mode = tree->lit_mode;
422 if (lockmode_compat(req_mode, tree->lit_mode)) {
423 struct ldlm_lock *lock;
425 if (req_mode != LCK_GROUP)
428 /* group lock, grant it immediately if
431 lock = container_of(tree->lit_root,
434 if (req->l_policy_data.l_extent.gid ==
435 lock->l_policy_data.l_extent.gid)
439 if (tree->lit_mode == LCK_GROUP) {
440 if (*flags & (LDLM_FL_BLOCK_NOWAIT |
441 LDLM_FL_SPECULATIVE)) {
449 /* if work list is not NULL,add all
450 * locks in the tree to work list
453 interval_iterate(tree->lit_root,
454 ldlm_extent_compat_cb, &data);
458 /* We've found a potentially blocking lock, check
459 * compatibility. This handles locks other than GROUP
460 * locks, which are handled separately above.
462 * Locks with FL_SPECULATIVE are asynchronous requests
463 * which must never wait behind another lock, so they
464 * fail if any conflicting lock is found.
466 if (!work_list || (*flags & LDLM_FL_SPECULATIVE)) {
467 rc = interval_is_overlapped(tree->lit_root,
478 interval_search(tree->lit_root, &ex,
479 ldlm_extent_compat_cb, &data);
480 if (!list_empty(work_list) && compat)
484 } else { /* for waiting queue */
485 list_for_each_entry(lock, queue, l_res_link) {
486 check_contention = 1;
488 /* We stop walking the queue if we hit ourselves so
489 * we don't take conflicting locks enqueued after us
490 * into account, or we'd wait forever.
495 /* locks are compatible, overlap doesn't matter */
496 if (lockmode_compat(lock->l_req_mode, req_mode)) {
497 if (req_mode == LCK_PR &&
498 ((lock->l_policy_data.l_extent.start <=
499 req->l_policy_data.l_extent.start) &&
500 (lock->l_policy_data.l_extent.end >=
501 req->l_policy_data.l_extent.end))) {
502 /* If we met a PR lock just like us or
503 * wider, and nobody down the list
504 * conflicted with it, that means we
505 * can skip processing of the rest of
506 * the list and safely place ourselves
507 * at the end of the list, or grant
508 * (dependent if we met an conflicting
509 * locks before in the list). In case
510 * of 1st enqueue only we continue
511 * traversing if there is something
512 * conflicting down the list because
513 * we need to make sure that something
514 * is marked as AST_SENT as well, in
515 * case of empy worklist we would exit
516 * on first conflict met.
519 /* There IS a case where such flag is
520 * not set for a lock, yet it blocks
521 * something. Luckily for us this is
522 * only during destroy, so lock is
523 * exclusive. So here we are safe
525 if (!ldlm_is_ast_sent(lock))
529 /* non-group locks are compatible,
530 * overlap doesn't matter
532 if (likely(req_mode != LCK_GROUP))
535 /* If we are trying to get a GROUP lock and
536 * there is another one of this kind, we need
539 if (req->l_policy_data.l_extent.gid ==
540 lock->l_policy_data.l_extent.gid) {
541 /* If existing lock with matched gid
542 * is granted, we grant new one too.
544 if (ldlm_is_granted(lock))
547 /* Otherwise we are scanning queue of
548 * waiting locks and it means current
549 * request would block along with
550 * existing lock (that is already
551 * blocked. If we are in nonblocking
552 * mode - return immediately
554 if (*flags & (LDLM_FL_BLOCK_NOWAIT |
555 LDLM_FL_SPECULATIVE)) {
559 /* If this group lock is compatible with
560 * another group lock on the waiting
561 * list, they must be together in the
562 * list, so they can be granted at the
563 * same time. Otherwise the later lock
564 * can get stuck behind another,
565 * incompatible, lock.
567 ldlm_resource_insert_lock_after(lock,
569 /* Because 'lock' is not granted, we can
570 * stop processing this queue and return
571 * immediately. There is no need to
572 * check the rest of the list.
578 if (unlikely(req_mode == LCK_GROUP &&
579 !ldlm_is_granted(lock))) {
581 if (lock->l_req_mode != LCK_GROUP) {
582 /* Ok, we hit non-GROUP lock, there
583 * should be no more GROUP locks later
584 * on, queue in front of first
588 ldlm_resource_insert_lock_before(lock,
592 LASSERT(req->l_policy_data.l_extent.gid !=
593 lock->l_policy_data.l_extent.gid);
597 if (unlikely(lock->l_req_mode == LCK_GROUP)) {
598 /* If compared lock is GROUP, then requested is
599 * PR/PW so this is not compatible; extent
600 * range does not matter
602 if (*flags & (LDLM_FL_BLOCK_NOWAIT |
603 LDLM_FL_SPECULATIVE)) {
607 } else if (lock->l_policy_data.l_extent.end < req_start ||
608 lock->l_policy_data.l_extent.start > req_end) {
609 /* if non group lock doesn't overlap skip it */
611 } else if (lock->l_req_extent.end < req_start ||
612 lock->l_req_extent.start > req_end) {
613 /* false contention, the requests doesn't
615 check_contention = 0;
621 if (*flags & LDLM_FL_SPECULATIVE) {
626 /* don't count conflicting glimpse locks */
627 if (lock->l_req_mode == LCK_PR &&
628 lock->l_policy_data.l_extent.start == 0 &&
629 lock->l_policy_data.l_extent.end == OBD_OBJECT_EOF)
630 check_contention = 0;
632 *contended_locks += check_contention;
635 if (lock->l_blocking_ast &&
636 lock->l_req_mode != LCK_GROUP)
637 ldlm_add_ast_work_item(lock, req, work_list);
641 if (ldlm_check_contention(req, *contended_locks) &&
642 compat == 0 && (*flags & LDLM_FL_DENY_ON_CONTENTION) &&
643 req->l_req_mode != LCK_GROUP && req_end - req_start <=
644 ldlm_res_to_ns(req->l_resource)->ns_max_nolock_size)
645 GOTO(destroylock, compat = -EUSERS);
649 list_del_init(&req->l_res_link);
650 ldlm_lock_destroy_nolock(req);
655 * This function refresh eviction timer for cancelled lock.
656 * \param[in] lock ldlm lock for refresh
657 * \param[in] arg ldlm prolong arguments, timeout, export, extent
658 * and counter are used
660 void ldlm_lock_prolong_one(struct ldlm_lock *lock,
661 struct ldlm_prolong_args *arg)
665 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PROLONG_PAUSE, 3);
667 if (arg->lpa_export != lock->l_export ||
668 lock->l_flags & LDLM_FL_DESTROYED)
669 /* ignore unrelated locks */
672 arg->lpa_locks_cnt++;
674 if (!(lock->l_flags & LDLM_FL_AST_SENT))
675 /* ignore locks not being cancelled */
678 arg->lpa_blocks_cnt++;
680 /* OK. this is a possible lock the user holds doing I/O
681 * let's refresh eviction timer for it.
683 timeout = ldlm_bl_timeout_by_rpc(arg->lpa_req);
684 LDLM_DEBUG(lock, "refreshed to %ds", timeout);
685 ldlm_refresh_waiting_lock(lock, timeout);
687 EXPORT_SYMBOL(ldlm_lock_prolong_one);
689 static enum interval_iter ldlm_resource_prolong_cb(struct interval_node *n,
692 struct ldlm_lock *lock = container_of(n, struct ldlm_lock, l_tree_node);
693 struct ldlm_prolong_args *arg = data;
696 ldlm_lock_prolong_one(lock, arg);
697 RETURN(INTERVAL_ITER_CONT);
701 * Walk through granted tree and prolong locks if they overlaps extent.
703 * \param[in] arg prolong args
705 void ldlm_resource_prolong(struct ldlm_prolong_args *arg)
707 struct ldlm_interval_tree *tree;
708 struct ldlm_resource *res;
709 struct interval_node_extent ex = { .start = arg->lpa_extent.start,
710 .end = arg->lpa_extent.end };
715 res = ldlm_resource_get(arg->lpa_export->exp_obd->obd_namespace,
716 &arg->lpa_resid, LDLM_EXTENT, 0);
718 CDEBUG(D_DLMTRACE, "Failed to get resource for resid %llu/%llu\n",
719 arg->lpa_resid.name[0], arg->lpa_resid.name[1]);
724 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
725 tree = &res->lr_itree[idx];
726 if (tree->lit_root == NULL) /* empty tree, skipped */
729 /* There is no possibility to check for the groupID
730 * so all the group locks are considered as valid
731 * here, especially because the client is supposed
732 * to check it has such a lock before sending an RPC.
734 if (!(tree->lit_mode & arg->lpa_mode))
737 interval_search(tree->lit_root, &ex,
738 ldlm_resource_prolong_cb, arg);
741 ldlm_resource_putref(res);
745 EXPORT_SYMBOL(ldlm_resource_prolong);
748 * Process a granting attempt for extent lock.
749 * Must be called with ns lock held.
751 * This function looks for any conflicts for \a lock in the granted or
752 * waiting queues. The lock is granted if no conflicts are found in
755 int ldlm_process_extent_lock(struct ldlm_lock *lock, __u64 *flags,
756 enum ldlm_process_intention intention,
757 enum ldlm_error *err, struct list_head *work_list)
759 struct ldlm_resource *res = lock->l_resource;
761 int contended_locks = 0;
762 struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
766 LASSERT(!ldlm_is_granted(lock));
767 LASSERT(!(*flags & LDLM_FL_DENY_ON_CONTENTION) ||
768 !ldlm_is_ast_discard_data(lock));
769 check_res_locked(res);
772 if (intention == LDLM_PROCESS_RESCAN) {
773 /* Careful observers will note that we don't handle -EAGAIN
774 * here, but it's ok for a non-obvious reason -- compat_queue
775 * can only return -EAGAIN if (flags & BLOCK_NOWAIT |
776 * SPECULATIVE). flags should always be zero here, and if that
777 * ever stops being true, we want to find out. */
778 LASSERT(*flags == 0);
779 rc = ldlm_extent_compat_queue(&res->lr_granted, lock, flags,
780 NULL, &contended_locks);
782 rc = ldlm_extent_compat_queue(&res->lr_waiting, lock,
787 RETURN(LDLM_ITER_STOP);
789 ldlm_resource_unlink_lock(lock);
791 if (!CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_EVICT_RACE))
792 ldlm_extent_policy(res, lock, flags);
793 ldlm_grant_lock(lock, grant_work);
794 RETURN(LDLM_ITER_CONTINUE);
798 rc = ldlm_extent_compat_queue(&res->lr_granted, lock, flags,
799 work_list, &contended_locks);
801 GOTO(out, *err = rc);
804 rc2 = ldlm_extent_compat_queue(&res->lr_waiting, lock,
808 GOTO(out, *err = rc = rc2);
812 ldlm_extent_policy(res, lock, flags);
813 ldlm_resource_unlink_lock(lock);
814 ldlm_grant_lock(lock, grant_work);
816 /* Adding LDLM_FL_NO_TIMEOUT flag to granted lock to
817 * force client to wait for the lock endlessly once
818 * the lock is enqueued -bzzz */
819 *flags |= LDLM_FL_NO_TIMEOUT;
822 RETURN(LDLM_ITER_CONTINUE);
826 #endif /* HAVE_SERVER_SUPPORT */
828 struct ldlm_kms_shift_args {
834 /* Callback for interval_iterate functions, used by ldlm_extent_shift_Kms */
835 static enum interval_iter ldlm_kms_shift_cb(struct interval_node *n,
838 struct ldlm_lock *lock = container_of(n, struct ldlm_lock, l_tree_node);
839 struct ldlm_kms_shift_args *arg = args;
842 /* Since all locks in an interval have the same extent, we can just
843 * use the lock without kms_ignore set.
845 if (ldlm_is_kms_ignore(lock))
846 RETURN(INTERVAL_ITER_CONT);
848 /* If we find a lock with a greater or equal kms, we are not the
849 * highest lock (or we share that distinction with another lock), and
850 * don't need to update KMS. Return old_kms and stop looking.
852 if (lock->l_policy_data.l_extent.end == OBD_OBJECT_EOF ||
853 lock->l_policy_data.l_extent.end + 1 >= arg->old_kms) {
854 arg->kms = arg->old_kms;
855 arg->complete = true;
856 RETURN(INTERVAL_ITER_STOP);
859 if (lock->l_policy_data.l_extent.end + 1 > arg->kms)
860 arg->kms = lock->l_policy_data.l_extent.end + 1;
862 /* Since interval_iterate_reverse starts with the highest lock and
863 * works down, for PW locks, we only need to check if we should update
864 * the kms, then stop walking the tree. PR locks are not exclusive, so
865 * the highest start does not imply the highest end and we must
866 * continue. (Only one group lock is allowed per resource, so this is
867 * irrelevant for group locks.)*/
868 if (lock->l_granted_mode == LCK_PW)
869 RETURN(INTERVAL_ITER_STOP);
871 RETURN(INTERVAL_ITER_CONT);
874 /* When a lock is cancelled by a client, the KMS may undergo change if this
875 * is the "highest lock". This function returns the new KMS value, updating
876 * it only if we were the highest lock.
878 * Caller must hold lr_lock already.
880 * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes!
882 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
884 struct ldlm_resource *res = lock->l_resource;
885 struct ldlm_interval_tree *tree;
886 struct ldlm_kms_shift_args args;
891 args.old_kms = old_kms;
893 args.complete = false;
895 /* don't let another thread in ldlm_extent_shift_kms race in
896 * just after we finish and take our lock into account in its
897 * calculation of the kms */
898 ldlm_set_kms_ignore(lock);
900 /* We iterate over the lock trees, looking for the largest kms smaller
901 * than the current one. */
902 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
903 tree = &res->lr_itree[idx];
905 /* If our already known kms is >= than the highest 'end' in
906 * this tree, we don't need to check this tree, because
907 * the kms from a tree can be lower than in_max_high (due to
908 * kms_ignore), but it can never be higher. */
909 if (!tree->lit_root || args.kms >= tree->lit_root->in_max_high)
912 interval_iterate_reverse(tree->lit_root, ldlm_kms_shift_cb,
915 /* this tells us we're not the highest lock, so we don't need
916 * to check the remaining trees */
921 LASSERTF(args.kms <= args.old_kms, "kms %llu old_kms %llu\n", args.kms,
926 EXPORT_SYMBOL(ldlm_extent_shift_kms);
928 static inline int ldlm_mode_to_index(enum ldlm_mode mode)
933 LASSERT(is_power_of_2(mode));
935 LASSERT(index < LCK_MODE_NUM);
939 /** Add newly granted lock into interval tree for the resource. */
940 void ldlm_extent_add_lock(struct ldlm_resource *res,
941 struct ldlm_lock *lock)
943 struct interval_node **root;
944 struct ldlm_extent *extent;
947 LASSERT(ldlm_is_granted(lock));
949 LASSERT(!interval_is_intree(&lock->l_tree_node));
951 idx = ldlm_mode_to_index(lock->l_granted_mode);
952 LASSERT(lock->l_granted_mode == BIT(idx));
953 LASSERT(lock->l_granted_mode == res->lr_itree[idx].lit_mode);
955 /* node extent initialize */
956 extent = &lock->l_policy_data.l_extent;
958 rc = interval_set(&lock->l_tree_node, extent->start, extent->end);
961 root = &res->lr_itree[idx].lit_root;
962 interval_insert(&lock->l_tree_node, root);
963 res->lr_itree[idx].lit_size++;
965 /* even though we use interval tree to manage the extent lock, we also
966 * add the locks into grant list, for debug purpose, .. */
967 ldlm_resource_add_lock(res, &res->lr_granted, lock);
969 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_GRANT_CHECK)) {
970 struct ldlm_lock *lck;
972 list_for_each_entry_reverse(lck, &res->lr_granted,
976 if (lockmode_compat(lck->l_granted_mode,
977 lock->l_granted_mode))
979 if (ldlm_extent_overlap(&lck->l_req_extent,
980 &lock->l_req_extent)) {
982 "granting conflicting lock %p %p\n",
984 ldlm_resource_dump(D_ERROR, res);
991 /** Remove cancelled lock from resource interval tree. */
992 void ldlm_extent_unlink_lock(struct ldlm_lock *lock)
994 struct ldlm_resource *res = lock->l_resource;
995 struct ldlm_interval_tree *tree;
998 if (!interval_is_intree(&lock->l_tree_node)) /* duplicate unlink */
1001 idx = ldlm_mode_to_index(lock->l_granted_mode);
1002 LASSERT(lock->l_granted_mode == BIT(idx));
1003 tree = &res->lr_itree[idx];
1005 LASSERT(tree->lit_root != NULL); /* assure the tree is not null */
1008 interval_erase(&lock->l_tree_node, &tree->lit_root);
1011 void ldlm_extent_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
1012 union ldlm_policy_data *lpolicy)
1014 lpolicy->l_extent.start = wpolicy->l_extent.start;
1015 lpolicy->l_extent.end = wpolicy->l_extent.end;
1016 lpolicy->l_extent.gid = wpolicy->l_extent.gid;
1019 void ldlm_extent_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
1020 union ldlm_wire_policy_data *wpolicy)
1022 memset(wpolicy, 0, sizeof(*wpolicy));
1023 wpolicy->l_extent.start = lpolicy->l_extent.start;
1024 wpolicy->l_extent.end = lpolicy->l_extent.end;
1025 wpolicy->l_extent.gid = lpolicy->l_extent.gid;