Whamcloud - gitweb
b=1971
[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 one extent overlaps with another */
34 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
35 {
36         if ((a->l_extent.start <= b->l_extent.end) &&
37             (a->l_extent.end >=  b->l_extent.start))
38                 RETURN(0);
39
40         RETURN(1);
41 }
42
43 #include "ldlm_internal.h"
44
45 /* The purpose of this function is to return:
46  * - the maximum extent
47  * - containing the requested extent
48  * - and not overlapping existing conflicting extents outside the requested one
49  *
50  * An alternative policy is to not shrink the new extent when conflicts exist.
51  *
52  * To reconstruct our formulas, take a deep breath. */
53 static void policy_internal(struct list_head *queue, struct ldlm_extent *req_ex,
54                             struct ldlm_extent *new_ex, ldlm_mode_t mode)
55 {
56         struct list_head *tmp;
57
58         list_for_each(tmp, queue) {
59                 struct ldlm_lock *lock;
60                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
61
62                 /* if lock doesn't overlap new_ex, skip it. */
63                 if (lock->l_extent.end < new_ex->start ||
64                     lock->l_extent.start > new_ex->end)
65                         continue;
66
67                 /* Locks are compatible, overlap doesn't matter */
68                 if (lockmode_compat(lock->l_req_mode, mode))
69                         continue;
70
71                 if (lock->l_extent.start < req_ex->start) {
72                         if (lock->l_extent.end == ~0) {
73                                 new_ex->start = req_ex->start;
74                                 new_ex->end = req_ex->end;
75                                 return;
76                         }
77                         new_ex->start = MIN(lock->l_extent.end + 1,
78                                             req_ex->start);
79                 }
80
81                 if (lock->l_extent.end > req_ex->end) {
82                         if (lock->l_extent.start == 0) {
83                                 new_ex->start = req_ex->start;
84                                 new_ex->end = req_ex->end;
85                                 return;
86                         }
87                         new_ex->end = MAX(lock->l_extent.start - 1,
88                                           req_ex->end);
89                 }
90         }
91 }
92
93 /* apply the internal policy by walking all the lists */
94 int ldlm_extent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
95                        void *req_cookie, ldlm_mode_t mode, int flags,
96                        void *data)
97 {
98         struct ldlm_lock *lock = *lockp;
99         struct ldlm_resource *res = lock->l_resource;
100         struct ldlm_extent *req_ex = req_cookie;
101         struct ldlm_extent new_ex;
102         new_ex.start = 0;
103         new_ex.end = ~0;
104
105         if (!res)
106                 LBUG();
107
108         l_lock(&ns->ns_lock);
109         policy_internal(&res->lr_granted, req_ex, &new_ex, mode);
110         policy_internal(&res->lr_converting, req_ex, &new_ex, mode);
111         policy_internal(&res->lr_waiting, req_ex, &new_ex, mode);
112         l_unlock(&ns->ns_lock);
113
114         memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
115
116         LDLM_DEBUG(lock, "requested extent ["LPU64"->"LPU64"], new extent ["
117                    LPU64"->"LPU64"]",
118                    req_ex->start, req_ex->end, new_ex.start, new_ex.end);
119
120         if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
121                 return ELDLM_LOCK_CHANGED;
122         else 
123                 return 0;
124 }