1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 #define DEBUG_SUBSYSTEM S_LDLM
26 # include <liblustre.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/obd_support.h>
31 #include <linux/lustre_lib.h>
33 #include "ldlm_internal.h"
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
41 ldlm_extent_internal_policy(struct list_head *queue, struct ldlm_lock *req,
42 struct ldlm_extent *new_ex)
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;
51 lockmode_verify(req_mode);
53 list_for_each(tmp, queue) {
54 struct ldlm_lock *lock;
55 struct ldlm_extent *l_extent;
57 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
58 l_extent = &lock->l_policy_data.l_extent;
60 if (new_ex->start == req_start && new_ex->end == req_end) {
65 /* Don't conflict with ourselves */
69 /* Locks are compatible, overlap doesn't matter */
70 if (lockmode_compat(lock->l_req_mode, req_mode))
73 /* If this is a high-traffic lock, don't grow downwards at all
74 * or grow upwards too much */
77 new_ex->start = req_start;
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)
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)
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;
101 new_ex->start = min(l_extent->end+1, req_start);
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,
114 new_ex->end = max(l_extent->start - 1, req_end);
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,
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)
132 struct ldlm_extent new_ex = { .start = 0, .end = ~0};
134 ldlm_extent_internal_policy(&res->lr_granted, lock, &new_ex);
135 ldlm_extent_internal_policy(&res->lr_waiting, lock, &new_ex);
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;
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. */
149 ldlm_extent_compat_queue(struct list_head *queue, struct ldlm_lock *req,
152 struct list_head *tmp;
153 struct ldlm_lock *lock;
154 ldlm_mode_t req_mode = req->l_req_mode;
155 __u64 req_start = req->l_req_extent.start;
156 __u64 req_end = req->l_req_extent.end;
160 lockmode_verify(req_mode);
162 list_for_each(tmp, queue) {
163 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
168 /* locks are compatible, overlap doesn't matter */
169 if (lockmode_compat(lock->l_req_mode, req_mode))
172 /* if lock doesn't overlap skip it */
173 if (lock->l_policy_data.l_extent.end < req_start ||
174 lock->l_policy_data.l_extent.start > req_end)
181 if (lock->l_blocking_ast)
182 ldlm_add_ast_work_item(lock, req, NULL, 0);
188 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
189 * - blocking ASTs have already been sent
190 * - the caller has already initialized req->lr_tmp
191 * - must call this function with the ns lock held
193 * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
194 * - blocking ASTs have not been sent
195 * - the caller has NOT initialized req->lr_tmp, so we must
196 * - must call this function with the ns lock held once */
197 int ldlm_process_extent_lock(struct ldlm_lock *lock, int *flags, int first_enq,
200 struct ldlm_resource *res = lock->l_resource;
201 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
205 LASSERT(list_empty(&res->lr_converting));
208 LASSERT(res->lr_tmp != NULL);
209 rc = ldlm_extent_compat_queue(&res->lr_granted, lock, 0);
211 RETURN(LDLM_ITER_STOP);
212 rc = ldlm_extent_compat_queue(&res->lr_waiting, lock, 0);
214 RETURN(LDLM_ITER_STOP);
216 ldlm_resource_unlink_lock(lock);
218 ldlm_extent_policy(res, lock, flags);
219 ldlm_grant_lock(lock, NULL, 0, 1);
220 RETURN(LDLM_ITER_CONTINUE);
224 LASSERT(res->lr_tmp == NULL);
225 res->lr_tmp = &rpc_list;
226 rc = ldlm_extent_compat_queue(&res->lr_granted, lock, 1);
227 rc += ldlm_extent_compat_queue(&res->lr_waiting, lock, 1);
231 /* If either of the compat_queue()s returned 0, then we
232 * have ASTs to send and must go onto the waiting list.
234 * bug 2322: we used to unlink and re-add here, which was a
235 * terrible folly -- if we goto restart, we could get
236 * re-ordered! Causes deadlock, because ASTs aren't sent! */
237 if (list_empty(&lock->l_res_link))
238 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
239 l_unlock(&res->lr_namespace->ns_lock);
240 rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
241 l_lock(&res->lr_namespace->ns_lock);
243 GOTO(restart, -ERESTART);
244 *flags |= LDLM_FL_BLOCK_GRANTED;
246 ldlm_extent_policy(res, lock, flags);
247 ldlm_resource_unlink_lock(lock);
248 ldlm_grant_lock(lock, NULL, 0, 0);
253 /* When a lock is cancelled by a client, the KMS may undergo change if this
254 * is the "highest lock". This function returns the new KMS value.
256 * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes! */
257 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
259 struct ldlm_resource *res = lock->l_resource;
260 struct list_head *tmp;
261 struct ldlm_lock *lck;
265 /* don't let another thread in ldlm_extent_shift_kms race in
266 * just after we finish and take our lock into account in its
267 * calculation of the kms */
268 lock->l_flags |= LDLM_FL_KMS_IGNORE;
270 l_lock(&res->lr_namespace->ns_lock);
271 list_for_each(tmp, &res->lr_granted) {
272 lck = list_entry(tmp, struct ldlm_lock, l_res_link);
274 if (lck->l_flags & LDLM_FL_KMS_IGNORE)
277 if (lck->l_policy_data.l_extent.end >= old_kms)
278 GOTO(out, kms = old_kms);
279 kms = lck->l_policy_data.l_extent.end + 1;
284 l_unlock(&res->lr_namespace->ns_lock);