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