1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * by Cluster File Systems, Inc.
10 * authors, Peter Braam <braam@clusterfs.com> &
11 * Phil Schwan <phil@clusterfs.com>
14 #define DEBUG_SUBSYSTEM S_LDLM
16 #include <linux/lustre_dlm.h>
18 /* This function will be called to judge if the granted queue of another child
19 * (read: another extent) is conflicting and needs its granted queue walked to
22 * This helps to find conflicts between read and write locks on overlapping
24 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
26 if (MAX(a->l_extent.start, b->l_extent.start) <=
27 MIN(a->l_extent.end, b->l_extent.end))
33 /* The purpose of this function is to return:
34 * - the maximum extent
35 * - containing the requested extent
36 * - and not overlapping existing extents outside the requested one
38 * An alternative policy is to not shrink the new extent when conflicts exist.
40 * To reconstruct our formulas, take a deep breath. */
41 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
42 struct ldlm_extent *new_ex, ldlm_mode_t mode)
44 struct list_head *tmp;
46 list_for_each(tmp, queue) {
47 struct ldlm_lock *lock;
48 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
50 if (lock->l_extent.end < req_ex->start)
51 new_ex->start = MIN(lock->l_extent.end, new_ex->start);
53 if (lock->l_extent.start < req_ex->start &&
54 !lockmode_compat(lock->l_req_mode, mode))
55 /* Policy: minimize conflict overlap */
56 new_ex->start = req_ex->start;
58 if (lock->l_extent.start > req_ex->end)
59 new_ex->end = MAX(lock->l_extent.start, new_ex->end);
61 if (lock->l_extent.end > req_ex->end &&
62 !lockmode_compat(lock->l_req_mode, mode))
63 /* Policy: minimize conflict overlap */
64 new_ex->end = req_ex->end;
69 /* apply the internal policy by walking all the lists */
70 int ldlm_extent_policy(struct ldlm_lock *lock, void *req_cookie,
71 ldlm_mode_t mode, void *data)
73 struct ldlm_resource *res = lock->l_resource;
74 struct ldlm_extent *req_ex = req_cookie;
75 struct ldlm_extent new_ex;
82 policy_internal(&res->lr_granted, req_ex, &new_ex, mode);
83 policy_internal(&res->lr_converting, req_ex, &new_ex, mode);
84 policy_internal(&res->lr_waiting, req_ex, &new_ex, mode);
86 memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
88 if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
89 return ELDLM_LOCK_CHANGED;