Whamcloud - gitweb
Landing the ldlm_testing branch; now the only difference is that the locking
[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 Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * by Cluster File Systems, Inc.
10  * authors, Peter Braam <braam@clusterfs.com> & 
11  * Phil Schwan <phil@clusterfs.com>
12  */
13
14 #define EXPORT_SYMTAB
15 #define DEBUG_SUBSYSTEM S_LDLM
16
17 #include <linux/lustre_dlm.h>
18
19 /* This function will be called to judge if the granted queue of another child
20  * (read: another extent) is conflicting and needs its granted queue walked to
21  * issue callbacks.
22  *
23  * This helps to find conflicts between read and write locks on overlapping
24  * extents. */
25 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
26 {
27         if (MAX(a->l_extent.start, b->l_extent.start) <=
28             MIN(a->l_extent.end, b->l_extent.end))
29                 RETURN(0);
30
31         RETURN(1);
32 }
33
34 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
35                             struct ldlm_extent *new_ex, ldlm_mode_t mode)
36 {
37         struct list_head *tmp;
38
39         list_for_each(tmp, queue) {
40                 struct ldlm_lock *lock;
41                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
42
43                 if (lock->l_extent.end < req_ex->start)
44                         new_ex->start = MIN(lock->l_extent.end, new_ex->start);
45                 else {
46                         if (lock->l_extent.start < req_ex->start &&
47                             !lockmode_compat(lock->l_req_mode, mode))
48                                 /* Policy: minimize conflict overlap */
49                                 new_ex->start = req_ex->start;
50                 }
51                 if (lock->l_extent.start > req_ex->end)
52                         new_ex->end = MAX(lock->l_extent.start, new_ex->end);
53                 else {
54                         if (lock->l_extent.end > req_ex->end &&
55                             !lockmode_compat(lock->l_req_mode, mode))
56                                 /* Policy: minimize conflict overlap */
57                                 new_ex->end = req_ex->end;
58                 }
59         }
60 }
61
62 /* The purpose of this function is to return:
63  * - the maximum extent
64  * - containing the requested extent
65  * - and not overlapping existing extents outside the requested one
66  *
67  * An alternative policy is to not shrink the new extent when conflicts exist.
68  *
69  * To reconstruct our formulas, take a deep breath. */
70 int ldlm_extent_policy(struct ldlm_resource *res, struct ldlm_extent *req_ex,
71                        struct ldlm_extent *new_ex, ldlm_mode_t mode, void *data)
72 {
73         new_ex->start = 0;
74         new_ex->end = ~0;
75
76         if (!res)
77                 LBUG();
78
79         policy_internal(&res->lr_granted, req_ex, new_ex, mode);
80         policy_internal(&res->lr_converting, req_ex, new_ex, mode);
81         policy_internal(&res->lr_waiting, req_ex, new_ex, mode);
82
83         if (new_ex->end != req_ex->end || new_ex->start != req_ex->start)
84                 return ELDLM_LOCK_CHANGED;
85         return 0;
86 }