4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_extent.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 * This file contains implementation of EXTENT lock type
45 * EXTENT lock type is for locking a contiguous range of values, represented
46 * by 64-bit starting and ending offsets (inclusive). There are several extent
47 * lock modes, some of which may be mutually incompatible. Extent locks are
48 * considered incompatible if their modes are incompatible and their extents
49 * intersect. See the lock mode compatibility matrix in lustre_dlm.h.
52 #define DEBUG_SUBSYSTEM S_LDLM
53 # include <linux/libcfs/libcfs.h>
55 #include <lustre_dlm.h>
56 #include <obd_support.h>
58 #include <obd_class.h>
59 #include <lustre_lib.h>
61 #include "ldlm_internal.h"
64 /* When a lock is cancelled by a client, the KMS may undergo change if this
65 * is the "highest lock". This function returns the new KMS value.
66 * Caller must hold lr_lock already.
68 * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes! */
69 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
71 struct ldlm_resource *res = lock->l_resource;
72 struct list_head *tmp;
73 struct ldlm_lock *lck;
77 /* don't let another thread in ldlm_extent_shift_kms race in
78 * just after we finish and take our lock into account in its
79 * calculation of the kms */
80 lock->l_flags |= LDLM_FL_KMS_IGNORE;
82 list_for_each(tmp, &res->lr_granted) {
83 lck = list_entry(tmp, struct ldlm_lock, l_res_link);
85 if (lck->l_flags & LDLM_FL_KMS_IGNORE)
88 if (lck->l_policy_data.l_extent.end >= old_kms)
91 /* This extent _has_ to be smaller than old_kms (checked above)
92 * so kms can only ever be smaller or the same as old_kms. */
93 if (lck->l_policy_data.l_extent.end + 1 > kms)
94 kms = lck->l_policy_data.l_extent.end + 1;
96 LASSERTF(kms <= old_kms, "kms "LPU64" old_kms "LPU64"\n", kms, old_kms);
100 EXPORT_SYMBOL(ldlm_extent_shift_kms);
102 struct kmem_cache *ldlm_interval_slab;
103 struct ldlm_interval *ldlm_interval_alloc(struct ldlm_lock *lock)
105 struct ldlm_interval *node;
108 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
109 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, __GFP_IO);
113 INIT_LIST_HEAD(&node->li_group);
114 ldlm_interval_attach(node, lock);
118 void ldlm_interval_free(struct ldlm_interval *node)
121 LASSERT(list_empty(&node->li_group));
122 LASSERT(!interval_is_intree(&node->li_node));
123 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
127 /* interval tree, for LDLM_EXTENT. */
128 void ldlm_interval_attach(struct ldlm_interval *n,
131 LASSERT(l->l_tree_node == NULL);
132 LASSERT(l->l_resource->lr_type == LDLM_EXTENT);
134 list_add_tail(&l->l_sl_policy, &n->li_group);
138 struct ldlm_interval *ldlm_interval_detach(struct ldlm_lock *l)
140 struct ldlm_interval *n = l->l_tree_node;
145 LASSERT(!list_empty(&n->li_group));
146 l->l_tree_node = NULL;
147 list_del_init(&l->l_sl_policy);
149 return (list_empty(&n->li_group) ? n : NULL);
152 static inline int lock_mode_to_index(ldlm_mode_t mode)
157 LASSERT(IS_PO2(mode));
158 for (index = -1; mode; index++, mode >>= 1) ;
159 LASSERT(index < LCK_MODE_NUM);
163 /** Add newly granted lock into interval tree for the resource. */
164 void ldlm_extent_add_lock(struct ldlm_resource *res,
165 struct ldlm_lock *lock)
167 struct interval_node *found, **root;
168 struct ldlm_interval *node;
169 struct ldlm_extent *extent;
172 LASSERT(lock->l_granted_mode == lock->l_req_mode);
174 node = lock->l_tree_node;
175 LASSERT(node != NULL);
176 LASSERT(!interval_is_intree(&node->li_node));
178 idx = lock_mode_to_index(lock->l_granted_mode);
179 LASSERT(lock->l_granted_mode == 1 << idx);
180 LASSERT(lock->l_granted_mode == res->lr_itree[idx].lit_mode);
182 /* node extent initialize */
183 extent = &lock->l_policy_data.l_extent;
184 interval_set(&node->li_node, extent->start, extent->end);
186 root = &res->lr_itree[idx].lit_root;
187 found = interval_insert(&node->li_node, root);
188 if (found) { /* The policy group found. */
189 struct ldlm_interval *tmp = ldlm_interval_detach(lock);
190 LASSERT(tmp != NULL);
191 ldlm_interval_free(tmp);
192 ldlm_interval_attach(to_ldlm_interval(found), lock);
194 res->lr_itree[idx].lit_size++;
196 /* even though we use interval tree to manage the extent lock, we also
197 * add the locks into grant list, for debug purpose, .. */
198 ldlm_resource_add_lock(res, &res->lr_granted, lock);
201 /** Remove cancelled lock from resource interval tree. */
202 void ldlm_extent_unlink_lock(struct ldlm_lock *lock)
204 struct ldlm_resource *res = lock->l_resource;
205 struct ldlm_interval *node = lock->l_tree_node;
206 struct ldlm_interval_tree *tree;
209 if (!node || !interval_is_intree(&node->li_node)) /* duplicate unlink */
212 idx = lock_mode_to_index(lock->l_granted_mode);
213 LASSERT(lock->l_granted_mode == 1 << idx);
214 tree = &res->lr_itree[idx];
216 LASSERT(tree->lit_root != NULL); /* assure the tree is not null */
219 node = ldlm_interval_detach(lock);
221 interval_erase(&node->li_node, &tree->lit_root);
222 ldlm_interval_free(node);
226 void ldlm_extent_policy_wire_to_local(const ldlm_wire_policy_data_t *wpolicy,
227 ldlm_policy_data_t *lpolicy)
229 memset(lpolicy, 0, sizeof(*lpolicy));
230 lpolicy->l_extent.start = wpolicy->l_extent.start;
231 lpolicy->l_extent.end = wpolicy->l_extent.end;
232 lpolicy->l_extent.gid = wpolicy->l_extent.gid;
235 void ldlm_extent_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
236 ldlm_wire_policy_data_t *wpolicy)
238 memset(wpolicy, 0, sizeof(*wpolicy));
239 wpolicy->l_extent.start = lpolicy->l_extent.start;
240 wpolicy->l_extent.end = lpolicy->l_extent.end;
241 wpolicy->l_extent.gid = lpolicy->l_extent.gid;