1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 #define DEBUG_SUBSYSTEM S_LDLM
26 # include <liblustre.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/obd_support.h>
31 #include <linux/lustre_lib.h>
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
37 * This helps to find conflicts between read and write locks on overlapping
39 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
41 if (MAX(a->l_extent.start, b->l_extent.start) <=
42 MIN(a->l_extent.end, b->l_extent.end))
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
53 * An alternative policy is to not shrink the new extent when conflicts exist.
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)
59 struct list_head *tmp;
61 list_for_each(tmp, queue) {
62 struct ldlm_lock *lock;
63 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
65 if (lock->l_extent.end < req_ex->start) {
66 new_ex->start = MIN(lock->l_extent.end, new_ex->start);
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;
73 if (lock->l_extent.start > req_ex->end) {
74 new_ex->end = MAX(lock->l_extent.start, new_ex->end);
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;
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,
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;
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);
105 memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
107 LDLM_DEBUG(lock, "new extent "LPU64" -> "LPU64, new_ex.start,
110 if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
111 return ELDLM_LOCK_CHANGED;