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 * Range lock is used to allow multiple threads writing a single shared
24 * file given each thread is writing to a non-overlapping portion of the
27 * Refer to the possible upstream kernel version of range lock by
28 * Jan Kara <jack@suse.cz>: https://lkml.org/lkml/2013/1/31/480
30 * This file could later replaced by the upstream kernel version.
33 * Author: Prakash Surya <surya1@llnl.gov>
34 * Author: Bobi Jam <bobijam.xu@intel.com>
36 #ifdef HAVE_SCHED_HEADERS
37 #include <linux/sched/signal.h>
39 #include "range_lock.h"
40 #include <uapi/linux/lustre/lustre_user.h>
43 * Initialize a range lock tree
45 * \param tree [in] an empty range lock tree
47 * Pre: Caller should have allocated the range lock tree.
48 * Post: The range lock tree is ready to function.
50 void range_lock_tree_init(struct range_lock_tree *tree)
52 tree->rlt_root = NULL;
53 tree->rlt_sequence = 0;
54 spin_lock_init(&tree->rlt_lock);
58 * Intialize a range lock node
60 * \param lock [in] an empty range lock node
61 * \param start [in] start of the covering region
62 * \param end [in] end of the covering region
64 * Pre: Caller should have allocated the range lock node.
65 * Post: The range lock node is meant to cover [start, end] region
67 int range_lock_init(struct range_lock *lock, __u64 start, __u64 end)
71 interval_init(&lock->rl_node);
72 if (end != LUSTRE_EOF)
74 rc = interval_set(&lock->rl_node, start >> PAGE_SHIFT, end);
78 INIT_LIST_HEAD(&lock->rl_next_lock);
80 lock->rl_lock_count = 0;
81 lock->rl_blocking_ranges = 0;
82 lock->rl_sequence = 0;
86 static inline struct range_lock *next_lock(struct range_lock *lock)
88 return list_entry(lock->rl_next_lock.next, typeof(*lock), rl_next_lock);
92 * Helper function of range_unlock()
94 * \param node [in] a range lock found overlapped during interval node
96 * \param arg [in] the range lock to be tested
98 * \retval INTERVAL_ITER_CONT indicate to continue the search for next
99 * overlapping range node
100 * \retval INTERVAL_ITER_STOP indicate to stop the search
102 static enum interval_iter range_unlock_cb(struct interval_node *node, void *arg)
104 struct range_lock *lock = arg;
105 struct range_lock *overlap = node2rangelock(node);
106 struct range_lock *iter;
109 list_for_each_entry(iter, &overlap->rl_next_lock, rl_next_lock) {
110 if (iter->rl_sequence > lock->rl_sequence) {
111 --iter->rl_blocking_ranges;
112 LASSERT(iter->rl_blocking_ranges > 0);
115 if (overlap->rl_sequence > lock->rl_sequence) {
116 --overlap->rl_blocking_ranges;
117 if (overlap->rl_blocking_ranges == 0)
118 wake_up_process(overlap->rl_task);
120 RETURN(INTERVAL_ITER_CONT);
124 * Unlock a range lock, wake up locks blocked by this lock.
126 * \param tree [in] range lock tree
127 * \param lock [in] range lock to be deleted
129 * If this lock has been granted, relase it; if not, just delete it from
130 * the tree or the same region lock list. Wake up those locks only blocked
131 * by this lock through range_unlock_cb().
133 void range_unlock(struct range_lock_tree *tree, struct range_lock *lock)
137 spin_lock(&tree->rlt_lock);
138 if (!list_empty(&lock->rl_next_lock)) {
139 struct range_lock *next;
141 if (interval_is_intree(&lock->rl_node)) { /* first lock */
142 /* Insert the next same range lock into the tree */
143 next = next_lock(lock);
144 next->rl_lock_count = lock->rl_lock_count - 1;
145 interval_erase(&lock->rl_node, &tree->rlt_root);
146 interval_insert(&next->rl_node, &tree->rlt_root);
148 /* find the first lock in tree */
149 list_for_each_entry(next, &lock->rl_next_lock,
151 if (!interval_is_intree(&next->rl_node))
154 LASSERT(next->rl_lock_count > 0);
155 next->rl_lock_count--;
159 list_del_init(&lock->rl_next_lock);
161 LASSERT(interval_is_intree(&lock->rl_node));
162 interval_erase(&lock->rl_node, &tree->rlt_root);
165 interval_search(tree->rlt_root, &lock->rl_node.in_extent,
166 range_unlock_cb, lock);
167 spin_unlock(&tree->rlt_lock);
173 * Helper function of range_lock()
175 * \param node [in] a range lock found overlapped during interval node
177 * \param arg [in] the range lock to be tested
179 * \retval INTERVAL_ITER_CONT indicate to continue the search for next
180 * overlapping range node
181 * \retval INTERVAL_ITER_STOP indicate to stop the search
183 static enum interval_iter range_lock_cb(struct interval_node *node, void *arg)
185 struct range_lock *lock = (struct range_lock *)arg;
186 struct range_lock *overlap = node2rangelock(node);
188 lock->rl_blocking_ranges += overlap->rl_lock_count + 1;
189 RETURN(INTERVAL_ITER_CONT);
195 * \param tree [in] range lock tree
196 * \param lock [in] range lock node containing the region span
198 * \retval 0 get the range lock
199 * \retval <0 error code while not getting the range lock
201 * If there exists overlapping range lock, the new lock will wait and
202 * retry, if later it find that it is not the chosen one to wake up,
205 int range_lock(struct range_lock_tree *tree, struct range_lock *lock)
207 struct interval_node *node;
211 spin_lock(&tree->rlt_lock);
213 * We need to check for all conflicting intervals
214 * already in the tree.
216 interval_search(tree->rlt_root, &lock->rl_node.in_extent,
217 range_lock_cb, lock);
219 * Insert to the tree if I am unique, otherwise I've been linked to
220 * the rl_next_lock of another lock which has the same range as mine
221 * in range_lock_cb().
223 node = interval_insert(&lock->rl_node, &tree->rlt_root);
225 struct range_lock *tmp = node2rangelock(node);
227 list_add_tail(&lock->rl_next_lock, &tmp->rl_next_lock);
228 tmp->rl_lock_count++;
230 lock->rl_sequence = ++tree->rlt_sequence;
232 while (lock->rl_blocking_ranges > 0) {
233 lock->rl_task = current;
234 __set_current_state(TASK_INTERRUPTIBLE);
235 spin_unlock(&tree->rlt_lock);
238 if (signal_pending(current)) {
239 range_unlock(tree, lock);
240 GOTO(out, rc = -ERESTARTSYS);
242 spin_lock(&tree->rlt_lock);
244 spin_unlock(&tree->rlt_lock);