Whamcloud - gitweb
Handle SF IOR better by working harder to avoid granting conflicting locks.
[fs/lustre-release.git] / lustre / ldlm / ldlm_extent.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25 #ifndef __KERNEL__
26 # include <liblustre.h>
27 #endif
28
29 #include <linux/lustre_dlm.h>
30 #include <linux/obd_support.h>
31 #include <linux/lustre_lib.h>
32
33 #include "ldlm_internal.h"
34
35 /* The purpose of this function is to return:
36  * - the maximum extent
37  * - containing the requested extent
38  * - and not overlapping existing conflicting extents outside the requested one
39  */
40 static void
41 ldlm_extent_internal_policy(struct list_head *queue, struct ldlm_lock *req,
42                             struct ldlm_extent *new_ex)
43 {
44         struct list_head *tmp;
45         ldlm_mode_t req_mode = req->l_req_mode;
46         __u64 req_start = req->l_req_extent.start;
47         __u64 req_end = req->l_req_extent.end;
48         int conflicting = 0;
49         ENTRY;
50
51         lockmode_verify(req_mode);
52
53         list_for_each(tmp, queue) {
54                 struct ldlm_lock *lock;
55                 struct ldlm_extent *l_extent;
56
57                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
58                 l_extent = &lock->l_policy_data.l_extent;
59
60                 if (new_ex->start == req_start && new_ex->end == req_end) {
61                         EXIT;
62                         return;
63                 }
64
65                 /* Don't conflict with ourselves */
66                 if (req == lock)
67                         continue;
68
69                 /* Locks are compatible, overlap doesn't matter */
70                 if (lockmode_compat(lock->l_req_mode, req_mode))
71                         continue;
72
73                 /* If this is a high-traffic lock, don't grow downwards at all
74                  * or grow upwards too much */
75                 ++conflicting;
76                 if (conflicting > 4)
77                         new_ex->start = req_start;
78
79                 /* If lock doesn't overlap new_ex, skip it. */
80                 if (l_extent->end < new_ex->start ||
81                     l_extent->start > new_ex->end)
82                         continue;
83
84                 /* Locks conflicting in requested extents and we can't satisfy
85                  * both locks, so ignore it.  Either we will ping-pong this
86                  * extent (we would regardless of what extent we granted) or
87                  * lock is unused and it shouldn't limit our extent growth. */
88                 if (lock->l_req_extent.end >= req_start &&
89                     lock->l_req_extent.start <= req_end)
90                         continue;
91
92                 /* We grow extents downwards only as far as they don't overlap
93                  * with already-granted locks, on the assumtion that clients
94                  * will be writing beyond the initial requested end and would
95                  * then need to enqueue a new lock beyond previous request.
96                  * l_req_extent->end strictly < req_start, checked above. */
97                 if (l_extent->start < req_start && new_ex->start != req_start) {
98                         if (l_extent->end >= req_start)
99                                 new_ex->start = req_start;
100                         else
101                                 new_ex->start = min(l_extent->end+1, req_start);
102                 }
103
104                 /* If we need to cancel this lock anyways because our request
105                  * overlaps the granted lock, we grow up to its requested
106                  * extent start instead of limiting this extent, assuming that
107                  * clients are writing forwards and the lock had over grown
108                  * its extent downwards before we enqueued our request. */
109                 if (l_extent->end > req_end) {
110                         if (l_extent->start <= req_end)
111                                 new_ex->end = max(lock->l_req_extent.start - 1,
112                                                   req_end);
113                         else
114                                 new_ex->end = max(l_extent->start - 1, req_end);
115                 }
116         }
117
118 #define LDLM_MAX_GROWN_EXTENT (32 * 1024 * 1024 - 1)
119         if (conflicting > 32 && (req_mode == LCK_PW || req_mode == LCK_CW)) {
120                 if (req_end < req_start + LDLM_MAX_GROWN_EXTENT)
121                         new_ex->end = min(req_start + LDLM_MAX_GROWN_EXTENT,
122                                           new_ex->end);
123         }
124         EXIT;
125 }
126
127 /* In order to determine the largest possible extent we can grant, we need
128  * to scan all of the queues. */
129 static void ldlm_extent_policy(struct ldlm_resource *res,
130                                struct ldlm_lock *lock, int *flags)
131 {
132         struct ldlm_extent new_ex = { .start = 0, .end = ~0};
133
134         ldlm_extent_internal_policy(&res->lr_granted, lock, &new_ex);
135         ldlm_extent_internal_policy(&res->lr_waiting, lock, &new_ex);
136
137         if (new_ex.start != lock->l_policy_data.l_extent.start ||
138             new_ex.end != lock->l_policy_data.l_extent.end) {
139                 *flags |= LDLM_FL_LOCK_CHANGED;
140                 lock->l_policy_data.l_extent.start = new_ex.start;
141                 lock->l_policy_data.l_extent.end = new_ex.end;
142         }
143 }
144
145 /* Determine if the lock is compatible with all locks on the queue.
146  * We stop walking the queue if we hit ourselves so we don't take
147  * conflicting locks enqueued after us into accound, or we'd wait forever.
148  *
149  * 0 if the lock is not compatible
150  * 1 if the lock is compatible
151  * 2 if this group lock is compatible and requires no further checking
152  * negative error, such as EWOULDBLOCK for group locks
153  */
154 static int
155 ldlm_extent_compat_queue(struct list_head *queue, struct ldlm_lock *req,
156                          int send_cbs, int *flags, ldlm_error_t *err)
157 {
158         struct list_head *tmp;
159         struct ldlm_lock *lock;
160         ldlm_mode_t req_mode = req->l_req_mode;
161         __u64 req_start = req->l_req_extent.start;
162         __u64 req_end = req->l_req_extent.end;
163         int compat = 1;
164         ENTRY;
165
166         lockmode_verify(req_mode);
167
168         list_for_each(tmp, queue) {
169                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
170
171                 if (req == lock)
172                         RETURN(compat);
173
174                 /* locks are compatible, overlap doesn't matter */
175                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
176                         /* non-group locks are compatible, overlap doesn't
177                            matter */
178                         if (req_mode != LCK_GROUP)
179                                 continue;
180                                 
181                         /* If we are trying to get a GROUP lock and there is
182                            another one of this kind, we need to compare gid */
183                         if (req->l_policy_data.l_extent.gid ==
184                             lock->l_policy_data.l_extent.gid) {
185                                 if (lock->l_req_mode == lock->l_granted_mode)
186                                         RETURN(2);
187
188                                 /* If we are in nonblocking mode - return
189                                    immediately */
190                                 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
191                                         compat = -EWOULDBLOCK;
192                                         goto destroylock;
193                                 }
194                                 /* If this group lock is compatible with another
195                                  * group lock on the waiting list, they must be
196                                  * together in the list, so they can be granted
197                                  * at the same time.  Otherwise the later lock
198                                  * can get stuck behind another, incompatible,
199                                  * lock. */
200                                 ldlm_resource_insert_lock_after(lock, req);
201                                 /* Because 'lock' is not granted, we can stop
202                                  * processing this queue and return immediately.
203                                  * There is no need to check the rest of the
204                                  * list. */
205                                 RETURN(0);
206                         }
207                 }
208
209                 if (lock->l_req_mode == LCK_GROUP) {
210                         /* If compared lock is GROUP, then requested is PR/PW/=>
211                          * this is not compatible; extent range does not
212                          * matter */
213                         if (*flags & LDLM_FL_BLOCK_NOWAIT) {
214                                 compat = -EWOULDBLOCK;
215                                 goto destroylock;
216                         } else {
217                                 *flags |= LDLM_FL_NO_TIMEOUT;
218                         }
219                 } else if (lock->l_policy_data.l_extent.end < req_start ||
220                            lock->l_policy_data.l_extent.start > req_end) {
221                         /* if a non grouplock doesn't overlap skip it */
222                         continue;
223                 }
224
225                 if (!send_cbs)
226                         RETURN(0);
227
228                 compat = 0;
229                 if (lock->l_blocking_ast)
230                         ldlm_add_ast_work_item(lock, req, NULL, 0);
231         }
232
233         return(compat);
234 destroylock:
235         list_del_init(&req->l_res_link);
236         ldlm_lock_destroy(req);
237         *err = compat;
238         RETURN(compat);
239 }
240
241 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
242   *   - blocking ASTs have already been sent
243   *   - the caller has already initialized req->lr_tmp
244   *   - must call this function with the ns lock held
245   *
246   * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
247   *   - blocking ASTs have not been sent
248   *   - the caller has NOT initialized req->lr_tmp, so we must
249   *   - must call this function with the ns lock held once */
250 int ldlm_process_extent_lock(struct ldlm_lock *lock, int *flags, int first_enq,
251                              ldlm_error_t *err)
252 {
253         struct ldlm_resource *res = lock->l_resource;
254         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
255         int rc, rc2;
256         ENTRY;
257
258         LASSERT(list_empty(&res->lr_converting));
259         *err = ELDLM_OK;
260
261         if (!first_enq) {
262                 /* Careful observers will note that we don't handle -EWOULDBLOCK
263                  * here, but it's ok for a non-obvious reason -- compat_queue
264                  * can only return -EWOULDBLOCK if (flags & BLOCK_NOWAIT).
265                  * flags should always be zero here, and if that ever stops
266                  * being true, we want to find out. */
267                 LASSERT(*flags == 0);
268                 LASSERT(res->lr_tmp != NULL);
269                 rc = ldlm_extent_compat_queue(&res->lr_granted, lock, 0, flags,
270                                               err);
271                 if (rc == 1) {
272                         rc = ldlm_extent_compat_queue(&res->lr_waiting, lock, 0,
273                                                       flags, err);
274                 }
275                 if (rc == 0)
276                         RETURN(LDLM_ITER_STOP);
277
278                 ldlm_resource_unlink_lock(lock);
279
280                 ldlm_extent_policy(res, lock, flags);
281                 ldlm_grant_lock(lock, NULL, 0, 1);
282                 RETURN(LDLM_ITER_CONTINUE);
283         }
284
285  restart:
286         LASSERT(res->lr_tmp == NULL);
287         res->lr_tmp = &rpc_list;
288         rc = ldlm_extent_compat_queue(&res->lr_granted, lock, 1, flags, err);
289         if (rc < 0)
290                 RETURN(rc); /* lock was destroyed */
291         if (rc == 2)
292                 goto grant;
293
294         rc2 = ldlm_extent_compat_queue(&res->lr_waiting, lock, 1, flags, err);
295         if (rc2 < 0)
296                 RETURN(rc2); /* lock was destroyed */
297         res->lr_tmp = NULL;
298
299         if (rc + rc2 == 2) {
300         grant:
301                 ldlm_extent_policy(res, lock, flags);
302                 ldlm_resource_unlink_lock(lock);
303                 ldlm_grant_lock(lock, NULL, 0, 0);
304         } else {
305                 /* If either of the compat_queue()s returned failure, then we
306                  * have ASTs to send and must go onto the waiting list.
307                  *
308                  * bug 2322: we used to unlink and re-add here, which was a
309                  * terrible folly -- if we goto restart, we could get
310                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
311                 if (list_empty(&lock->l_res_link))
312                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
313                 l_unlock(&res->lr_namespace->ns_lock);
314                 rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
315                 l_lock(&res->lr_namespace->ns_lock);
316                 if (rc == -ERESTART)
317                         GOTO(restart, -ERESTART);
318                 *flags |= LDLM_FL_BLOCK_GRANTED;
319         }
320         RETURN(0);
321 }
322
323 /* When a lock is cancelled by a client, the KMS may undergo change if this
324  * is the "highest lock".  This function returns the new KMS value.
325  *
326  * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes! */
327 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
328 {
329         struct ldlm_resource *res = lock->l_resource;
330         struct list_head *tmp;
331         struct ldlm_lock *lck;
332         __u64 kms = 0;
333         ENTRY;
334
335         l_lock(&res->lr_namespace->ns_lock);
336         list_for_each(tmp, &res->lr_granted) {
337                 lck = list_entry(tmp, struct ldlm_lock, l_res_link);
338
339                 if (lock == lck)
340                         continue;
341                 if (lck->l_policy_data.l_extent.end >= old_kms)
342                         GOTO(out, kms = old_kms);
343                 kms = lck->l_policy_data.l_extent.end + 1;
344         }
345
346         GOTO(out, kms);
347  out:
348         l_unlock(&res->lr_namespace->ns_lock);
349         return kms;
350 }