Whamcloud - gitweb
- Fixed some 64bit warnings
[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 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
34                             struct ldlm_extent *new_ex, ldlm_mode_t mode)
35 {
36         struct list_head *tmp;
37
38         list_for_each(tmp, queue) {
39                 struct ldlm_lock *lock;
40                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
41
42                 if (lock->l_extent.end < req_ex->start)
43                         new_ex->start = MIN(lock->l_extent.end, new_ex->start);
44                 else {
45                         if (lock->l_extent.start < req_ex->start &&
46                             !lockmode_compat(lock->l_req_mode, mode))
47                                 /* Policy: minimize conflict overlap */
48                                 new_ex->start = req_ex->start;
49                 }
50                 if (lock->l_extent.start > req_ex->end)
51                         new_ex->end = MAX(lock->l_extent.start, new_ex->end);
52                 else {
53                         if (lock->l_extent.end > req_ex->end &&
54                             !lockmode_compat(lock->l_req_mode, mode))
55                                 /* Policy: minimize conflict overlap */
56                                 new_ex->end = req_ex->end;
57                 }
58         }
59 }
60
61 /* The purpose of this function is to return:
62  * - the maximum extent
63  * - containing the requested extent
64  * - and not overlapping existing extents outside the requested one
65  *
66  * An alternative policy is to not shrink the new extent when conflicts exist.
67  *
68  * To reconstruct our formulas, take a deep breath. */
69 int ldlm_extent_policy(struct ldlm_resource *res, struct ldlm_extent *req_ex,
70                        struct ldlm_extent *new_ex, ldlm_mode_t mode, void *data)
71 {
72         new_ex->start = 0;
73         new_ex->end = ~0;
74
75         if (!res)
76                 LBUG();
77
78         policy_internal(&res->lr_granted, req_ex, new_ex, mode);
79         policy_internal(&res->lr_converting, req_ex, new_ex, mode);
80         policy_internal(&res->lr_waiting, req_ex, new_ex, mode);
81
82         if (new_ex->end != req_ex->end || new_ex->start != req_ex->start)
83                 return ELDLM_LOCK_CHANGED;
84         return 0;
85 }