Whamcloud - gitweb
- add more infrastructure to handle extents and debug.
[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 EXPORT_SYMTAB
15
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <asm/unistd.h>
20
21 #define DEBUG_SUBSYSTEM S_LDLM
22
23 #include <linux/obd_support.h>
24 #include <linux/obd_class.h>
25 #include <linux/lustre_dlm.h>
26
27 /* This function will be called to judge if the granted queue of another child
28  * (read: another extent) is conflicting and needs its granted queue walked to
29  * issue callbacks.
30  *
31  * This helps to find conflicts between read and write locks on overlapping
32  * extents. */
33 int ldlm_extent_compat(struct ldlm_resource *child, struct ldlm_resource *new)
34 {
35         struct ldlm_extent *child_ex, *new_ex;
36
37         child_ex = ldlm_res2extent(child);
38         new_ex = ldlm_res2extent(new);
39
40         if (MAX(child_ex->start, new_ex->start) <=
41             MIN(child_ex->end, new_ex->end))
42                 return 0;
43
44         return 1;
45 }
46
47 /* The purpose of this function is to return:
48  * - the maximum extent
49  * - containing the requested extent
50  * - and not overlapping existing extents outside the requested one
51  *
52  * An alternative policy is to not shrink the new extent when conflicts exist.
53  *
54  * To reconstruct our formulas, take a deep breath. */
55 int ldlm_extent_policy(struct ldlm_resource *parent,
56                        __u64 *res_id_in, __u64 *res_id_out,
57                        ldlm_mode_t mode, void *data)
58 {
59         struct ldlm_extent *new_ex, *req_ex;
60         struct list_head *tmp;
61         int rc = 0;
62
63         req_ex = (struct ldlm_extent *)res_id_in;
64
65         new_ex = (struct ldlm_extent *)res_id_out;
66         new_ex->start = 0;
67         new_ex->end = ~0;
68
69         list_for_each(tmp, &parent->lr_children) {
70                 struct ldlm_resource *res;
71                 struct ldlm_extent *exist_ex;
72                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
73
74                 exist_ex = ldlm_res2extent(res);
75
76                 if (exist_ex->end < req_ex->start)
77                         new_ex->start = MIN(exist_ex->end, new_ex->start);
78                 else {
79                         if (exist_ex->start < req_ex->start &&
80                             !lockmode_compat(res->lr_most_restr, mode))
81                                 /* Policy: minimize conflict overlap */
82                                 new_ex->start = req_ex->start;
83                 }
84                 if (exist_ex->start > req_ex->end)
85                         new_ex->end = MAX(exist_ex->start, new_ex->end);
86                 else {
87                         if (exist_ex->end > req_ex->end &&
88                             !lockmode_compat(res->lr_most_restr, mode))
89                                 /* Policy: minimize conflict overlap */
90                                 new_ex->end = req_ex->end;
91                 }
92         }
93
94         if (new_ex->end != req_ex->end || new_ex->start != req_ex->start)
95                 rc = ELDLM_RES_CHANGED;
96
97         return rc;
98 }
99