GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / staging / lustre / lustre / llite / range_lock.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
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
25  * file.
26  *
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
29  *
30  * This file could later replaced by the upstream kernel version.
31  */
32 /*
33  * Author: Prakash Surya <surya1@llnl.gov>
34  * Author: Bobi Jam <bobijam.xu@intel.com>
35  */
36 #include "range_lock.h"
37 #include "../include/lustre/lustre_user.h"
38
39 /**
40  * Initialize a range lock tree
41  *
42  * \param tree [in]     an empty range lock tree
43  *
44  * Pre:  Caller should have allocated the range lock tree.
45  * Post: The range lock tree is ready to function.
46  */
47 void range_lock_tree_init(struct range_lock_tree *tree)
48 {
49         tree->rlt_root = NULL;
50         tree->rlt_sequence = 0;
51         spin_lock_init(&tree->rlt_lock);
52 }
53
54 /**
55  * Initialize a range lock node
56  *
57  * \param lock  [in]    an empty range lock node
58  * \param start [in]    start of the covering region
59  * \param end   [in]    end of the covering region
60  *
61  * Pre:  Caller should have allocated the range lock node.
62  * Post: The range lock node is meant to cover [start, end] region
63  */
64 void range_lock_init(struct range_lock *lock, __u64 start, __u64 end)
65 {
66         memset(&lock->rl_node, 0, sizeof(lock->rl_node));
67         if (end != LUSTRE_EOF)
68                 end >>= PAGE_SHIFT;
69         interval_set(&lock->rl_node, start >> PAGE_SHIFT, end);
70         INIT_LIST_HEAD(&lock->rl_next_lock);
71         lock->rl_task = NULL;
72         lock->rl_lock_count = 0;
73         lock->rl_blocking_ranges = 0;
74         lock->rl_sequence = 0;
75 }
76
77 static inline struct range_lock *next_lock(struct range_lock *lock)
78 {
79         return list_entry(lock->rl_next_lock.next, typeof(*lock), rl_next_lock);
80 }
81
82 /**
83  * Helper function of range_unlock()
84  *
85  * \param node [in]     a range lock found overlapped during interval node
86  *                      search
87  * \param arg [in]      the range lock to be tested
88  *
89  * \retval INTERVAL_ITER_CONT   indicate to continue the search for next
90  *                              overlapping range node
91  * \retval INTERVAL_ITER_STOP   indicate to stop the search
92  */
93 static enum interval_iter range_unlock_cb(struct interval_node *node, void *arg)
94 {
95         struct range_lock *lock = arg;
96         struct range_lock *overlap = node2rangelock(node);
97         struct range_lock *iter;
98
99         list_for_each_entry(iter, &overlap->rl_next_lock, rl_next_lock) {
100                 if (iter->rl_sequence > lock->rl_sequence) {
101                         --iter->rl_blocking_ranges;
102                         LASSERT(iter->rl_blocking_ranges > 0);
103                 }
104         }
105         if (overlap->rl_sequence > lock->rl_sequence) {
106                 --overlap->rl_blocking_ranges;
107                 if (overlap->rl_blocking_ranges == 0)
108                         wake_up_process(overlap->rl_task);
109         }
110         return INTERVAL_ITER_CONT;
111 }
112
113 /**
114  * Unlock a range lock, wake up locks blocked by this lock.
115  *
116  * \param tree [in]     range lock tree
117  * \param lock [in]     range lock to be deleted
118  *
119  * If this lock has been granted, relase it; if not, just delete it from
120  * the tree or the same region lock list. Wake up those locks only blocked
121  * by this lock through range_unlock_cb().
122  */
123 void range_unlock(struct range_lock_tree *tree, struct range_lock *lock)
124 {
125         spin_lock(&tree->rlt_lock);
126         if (!list_empty(&lock->rl_next_lock)) {
127                 struct range_lock *next;
128
129                 if (interval_is_intree(&lock->rl_node)) { /* first lock */
130                         /* Insert the next same range lock into the tree */
131                         next = next_lock(lock);
132                         next->rl_lock_count = lock->rl_lock_count - 1;
133                         interval_erase(&lock->rl_node, &tree->rlt_root);
134                         interval_insert(&next->rl_node, &tree->rlt_root);
135                 } else {
136                         /* find the first lock in tree */
137                         list_for_each_entry(next, &lock->rl_next_lock,
138                                             rl_next_lock) {
139                                 if (!interval_is_intree(&next->rl_node))
140                                         continue;
141
142                                 LASSERT(next->rl_lock_count > 0);
143                                 next->rl_lock_count--;
144                                 break;
145                         }
146                 }
147                 list_del_init(&lock->rl_next_lock);
148         } else {
149                 LASSERT(interval_is_intree(&lock->rl_node));
150                 interval_erase(&lock->rl_node, &tree->rlt_root);
151         }
152
153         interval_search(tree->rlt_root, &lock->rl_node.in_extent,
154                         range_unlock_cb, lock);
155         spin_unlock(&tree->rlt_lock);
156 }
157
158 /**
159  * Helper function of range_lock()
160  *
161  * \param node [in]     a range lock found overlapped during interval node
162  *                      search
163  * \param arg [in]      the range lock to be tested
164  *
165  * \retval INTERVAL_ITER_CONT   indicate to continue the search for next
166  *                              overlapping range node
167  * \retval INTERVAL_ITER_STOP   indicate to stop the search
168  */
169 static enum interval_iter range_lock_cb(struct interval_node *node, void *arg)
170 {
171         struct range_lock *lock = (struct range_lock *)arg;
172         struct range_lock *overlap = node2rangelock(node);
173
174         lock->rl_blocking_ranges += overlap->rl_lock_count + 1;
175         return INTERVAL_ITER_CONT;
176 }
177
178 /**
179  * Lock a region
180  *
181  * \param tree [in]     range lock tree
182  * \param lock [in]     range lock node containing the region span
183  *
184  * \retval 0    get the range lock
185  * \retval <0   error code while not getting the range lock
186  *
187  * If there exists overlapping range lock, the new lock will wait and
188  * retry, if later it find that it is not the chosen one to wake up,
189  * it wait again.
190  */
191 int range_lock(struct range_lock_tree *tree, struct range_lock *lock)
192 {
193         struct interval_node *node;
194         int rc = 0;
195
196         spin_lock(&tree->rlt_lock);
197         /*
198          * We need to check for all conflicting intervals
199          * already in the tree.
200          */
201         interval_search(tree->rlt_root, &lock->rl_node.in_extent,
202                         range_lock_cb, lock);
203         /*
204          * Insert to the tree if I am unique, otherwise I've been linked to
205          * the rl_next_lock of another lock which has the same range as mine
206          * in range_lock_cb().
207          */
208         node = interval_insert(&lock->rl_node, &tree->rlt_root);
209         if (node) {
210                 struct range_lock *tmp = node2rangelock(node);
211
212                 list_add_tail(&lock->rl_next_lock, &tmp->rl_next_lock);
213                 tmp->rl_lock_count++;
214         }
215         lock->rl_sequence = ++tree->rlt_sequence;
216
217         while (lock->rl_blocking_ranges > 0) {
218                 lock->rl_task = current;
219                 __set_current_state(TASK_INTERRUPTIBLE);
220                 spin_unlock(&tree->rlt_lock);
221                 schedule();
222
223                 if (signal_pending(current)) {
224                         range_unlock(tree, lock);
225                         rc = -EINTR;
226                         goto out;
227                 }
228                 spin_lock(&tree->rlt_lock);
229         }
230         spin_unlock(&tree->rlt_lock);
231 out:
232         return rc;
233 }