Whamcloud - gitweb
Landing the mds_lock_devel branch on the trunk. Notables:
[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 DEBUG_SUBSYSTEM S_LDLM
15
16 #include <linux/lustre_dlm.h>
17
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
20  * issue callbacks.
21  *
22  * This helps to find conflicts between read and write locks on overlapping
23  * extents. */
24 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
25 {
26         if (MAX(a->l_extent.start, b->l_extent.start) <=
27             MIN(a->l_extent.end, b->l_extent.end))
28                 RETURN(0);
29
30         RETURN(1);
31 }
32
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
37  *
38  * An alternative policy is to not shrink the new extent when conflicts exist.
39  *
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)
43 {
44         struct list_head *tmp;
45
46         list_for_each(tmp, queue) {
47                 struct ldlm_lock *lock;
48                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
49
50                 if (lock->l_extent.end < req_ex->start)
51                         new_ex->start = MIN(lock->l_extent.end, new_ex->start);
52                 else {
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;
57                 }
58                 if (lock->l_extent.start > req_ex->end)
59                         new_ex->end = MAX(lock->l_extent.start, new_ex->end);
60                 else {
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;
65                 }
66         }
67 }
68
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)
72 {
73         struct ldlm_resource *res = lock->l_resource;
74         struct ldlm_extent *req_ex = req_cookie;
75         struct ldlm_extent new_ex;
76         new_ex.start = 0;
77         new_ex.end = ~0;
78
79         if (!res)
80                 LBUG();
81
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);
85
86         memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
87
88         if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
89                 return ELDLM_LOCK_CHANGED;
90         else 
91                 return 0;
92 }