Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25 #ifndef __KERNEL__
26 # include <liblustre.h>
27 #endif
28
29 #include <linux/lustre_dlm.h>
30 #include <linux/obd_support.h>
31 #include <linux/lustre_lib.h>
32
33 /* This function will be called to judge if the granted queue of another child
34  * (read: another extent) is conflicting and needs its granted queue walked to
35  * issue callbacks.
36  *
37  * This helps to find conflicts between read and write locks on overlapping
38  * extents. */
39 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
40 {
41         if (MAX(a->l_extent.start, b->l_extent.start) <=
42             MIN(a->l_extent.end, b->l_extent.end))
43                 RETURN(0);
44
45         RETURN(1);
46 }
47
48 /* The purpose of this function is to return:
49  * - the maximum extent
50  * - containing the requested extent
51  * - and not overlapping existing extents outside the requested one
52  *
53  * An alternative policy is to not shrink the new extent when conflicts exist.
54  *
55  * To reconstruct our formulas, take a deep breath. */
56 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
57                             struct ldlm_extent *new_ex, ldlm_mode_t mode)
58 {
59         struct list_head *tmp;
60
61         list_for_each(tmp, queue) {
62                 struct ldlm_lock *lock;
63                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
64
65                 if (lock->l_extent.end < req_ex->start) {
66                         new_ex->start = MIN(lock->l_extent.end, new_ex->start);
67                 } else {
68                         if (lock->l_extent.start < req_ex->start &&
69                             !lockmode_compat(lock->l_req_mode, mode))
70                                 /* Policy: minimize conflict overlap */
71                                 new_ex->start = req_ex->start;
72                 }
73                 if (lock->l_extent.start > req_ex->end) {
74                         new_ex->end = MAX(lock->l_extent.start, new_ex->end);
75                 } else {
76                         if (lock->l_extent.end > req_ex->end &&
77                             !lockmode_compat(lock->l_req_mode, mode))
78                                 /* Policy: minimize conflict overlap */
79                                 new_ex->end = req_ex->end;
80                 }
81         }
82 }
83
84 /* apply the internal policy by walking all the lists */
85 int ldlm_extent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
86                        void *req_cookie, ldlm_mode_t mode, int flags,
87                        void *data)
88 {
89         struct ldlm_lock *lock = *lockp;
90         struct ldlm_resource *res = lock->l_resource;
91         struct ldlm_extent *req_ex = req_cookie;
92         struct ldlm_extent new_ex;
93         new_ex.start = 0;
94         new_ex.end = ~0;
95
96         if (!res)
97                 LBUG();
98
99         l_lock(&ns->ns_lock);
100         policy_internal(&res->lr_granted, req_ex, &new_ex, mode);
101         policy_internal(&res->lr_converting, req_ex, &new_ex, mode);
102         policy_internal(&res->lr_waiting, req_ex, &new_ex, mode);
103         l_unlock(&ns->ns_lock);
104
105         memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
106
107         LDLM_DEBUG(lock, "new extent "LPU64" -> "LPU64, new_ex.start,
108                    new_ex.end);
109
110         if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
111                 return ELDLM_LOCK_CHANGED;
112         else 
113                 return 0;
114 }