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 one extent overlaps with another */
34 int ldlm_extent_compat(struct ldlm_lock *a, struct ldlm_lock *b)
36 if ((a->l_extent.start <= b->l_extent.end) &&
37 (a->l_extent.end >= b->l_extent.start))
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
48 * An alternative policy is to not shrink the new extent when conflicts exist.
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)
54 struct list_head *tmp;
56 list_for_each(tmp, queue) {
57 struct ldlm_lock *lock;
58 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
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)
65 /* Locks are compatible, overlap doesn't matter */
66 if (lockmode_compat(lock->l_req_mode, mode))
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;
75 new_ex->start = MIN(lock->l_extent.end + 1,
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;
85 new_ex->end = MAX(lock->l_extent.start - 1,
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,
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;
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);
112 memcpy(&lock->l_extent, &new_ex, sizeof(new_ex));
114 LDLM_DEBUG(lock, "requested extent ["LPU64"->"LPU64"], new extent ["
116 req_ex->start, req_ex->end, new_ex.start, new_ex.end);
118 if (new_ex.end != req_ex->end || new_ex.start != req_ex->start)
119 return ELDLM_LOCK_CHANGED;