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>
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <asm/unistd.h>
21 #define DEBUG_SUBSYSTEM S_LDLM
23 #include <linux/obd_support.h>
24 #include <linux/obd_class.h>
25 #include <linux/lustre_dlm.h>
27 /* This function will be called to judge if the granted queue of another child
28 * (read: another extent) is conflicting and needs its granted queue walked to
31 * This helps to find conflicts between read and write locks on overlapping
33 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
35 if (MAX(a->l_extent.start, b->l_extent.start) <=
36 MIN(a->l_extent.end, b->l_extent.end))
42 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
43 struct ldlm_extent *new_ex, ldlm_mode_t mode)
45 struct list_head *tmp;
47 list_for_each(tmp, queue) {
48 struct ldlm_lock *lock;
49 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
51 if (lock->l_extent.end < req_ex->start)
52 new_ex->start = MIN(lock->l_extent.end, new_ex->start);
54 if (lock->l_extent.start < req_ex->start &&
55 !lockmode_compat(lock->l_req_mode, mode))
56 /* Policy: minimize conflict overlap */
57 new_ex->start = req_ex->start;
59 if (lock->l_extent.start > req_ex->end)
60 new_ex->end = MAX(lock->l_extent.start, new_ex->end);
62 if (lock->l_extent.end > req_ex->end &&
63 !lockmode_compat(lock->l_req_mode, mode))
64 /* Policy: minimize conflict overlap */
65 new_ex->end = req_ex->end;
70 /* The purpose of this function is to return:
71 * - the maximum extent
72 * - containing the requested extent
73 * - and not overlapping existing extents outside the requested one
75 * An alternative policy is to not shrink the new extent when conflicts exist.
77 * To reconstruct our formulas, take a deep breath. */
78 int ldlm_extent_policy(struct ldlm_resource *res, struct ldlm_extent *req_ex,
79 struct ldlm_extent *new_ex, ldlm_mode_t mode, void *data)
87 policy_internal(&res->lr_granted, req_ex, new_ex, mode);
88 policy_internal(&res->lr_converting, req_ex, new_ex, mode);
89 policy_internal(&res->lr_waiting, req_ex, new_ex, mode);
91 if (new_ex->end != req_ex->end || new_ex->start != req_ex->start)
92 return ELDLM_LOCK_CHANGED;