• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_extent.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41 
42 /**
43  * This file contains implementation of EXTENT lock type
44  *
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.
50  */
51 
52 #define DEBUG_SUBSYSTEM S_LDLM
53 #include "../../include/linux/libcfs/libcfs.h"
54 #include "../include/lustre_dlm.h"
55 #include "../include/obd_support.h"
56 #include "../include/obd.h"
57 #include "../include/obd_class.h"
58 #include "../include/lustre_lib.h"
59 #include "ldlm_internal.h"
60 
61 /* When a lock is cancelled by a client, the KMS may undergo change if this
62  * is the "highest lock".  This function returns the new KMS value.
63  * Caller must hold lr_lock already.
64  *
65  * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes! */
ldlm_extent_shift_kms(struct ldlm_lock * lock,__u64 old_kms)66 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
67 {
68 	struct ldlm_resource *res = lock->l_resource;
69 	struct list_head *tmp;
70 	struct ldlm_lock *lck;
71 	__u64 kms = 0;
72 
73 	/* don't let another thread in ldlm_extent_shift_kms race in
74 	 * just after we finish and take our lock into account in its
75 	 * calculation of the kms */
76 	lock->l_flags |= LDLM_FL_KMS_IGNORE;
77 
78 	list_for_each(tmp, &res->lr_granted) {
79 		lck = list_entry(tmp, struct ldlm_lock, l_res_link);
80 
81 		if (lck->l_flags & LDLM_FL_KMS_IGNORE)
82 			continue;
83 
84 		if (lck->l_policy_data.l_extent.end >= old_kms)
85 			return old_kms;
86 
87 		/* This extent _has_ to be smaller than old_kms (checked above)
88 		 * so kms can only ever be smaller or the same as old_kms. */
89 		if (lck->l_policy_data.l_extent.end + 1 > kms)
90 			kms = lck->l_policy_data.l_extent.end + 1;
91 	}
92 	LASSERTF(kms <= old_kms, "kms %llu old_kms %llu\n", kms, old_kms);
93 
94 	return kms;
95 }
96 EXPORT_SYMBOL(ldlm_extent_shift_kms);
97 
98 struct kmem_cache *ldlm_interval_slab;
99 
100 /* interval tree, for LDLM_EXTENT. */
ldlm_interval_attach(struct ldlm_interval * n,struct ldlm_lock * l)101 static void ldlm_interval_attach(struct ldlm_interval *n, struct ldlm_lock *l)
102 {
103 	LASSERT(!l->l_tree_node);
104 	LASSERT(l->l_resource->lr_type == LDLM_EXTENT);
105 
106 	list_add_tail(&l->l_sl_policy, &n->li_group);
107 	l->l_tree_node = n;
108 }
109 
ldlm_interval_alloc(struct ldlm_lock * lock)110 struct ldlm_interval *ldlm_interval_alloc(struct ldlm_lock *lock)
111 {
112 	struct ldlm_interval *node;
113 
114 	LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
115 	node = kmem_cache_alloc(ldlm_interval_slab, GFP_NOFS | __GFP_ZERO);
116 	if (node == NULL)
117 		return NULL;
118 
119 	INIT_LIST_HEAD(&node->li_group);
120 	ldlm_interval_attach(node, lock);
121 	return node;
122 }
123 
ldlm_interval_free(struct ldlm_interval * node)124 void ldlm_interval_free(struct ldlm_interval *node)
125 {
126 	if (node) {
127 		LASSERT(list_empty(&node->li_group));
128 		LASSERT(!interval_is_intree(&node->li_node));
129 		kmem_cache_free(ldlm_interval_slab, node);
130 	}
131 }
132 
ldlm_interval_detach(struct ldlm_lock * l)133 struct ldlm_interval *ldlm_interval_detach(struct ldlm_lock *l)
134 {
135 	struct ldlm_interval *n = l->l_tree_node;
136 
137 	if (n == NULL)
138 		return NULL;
139 
140 	LASSERT(!list_empty(&n->li_group));
141 	l->l_tree_node = NULL;
142 	list_del_init(&l->l_sl_policy);
143 
144 	return list_empty(&n->li_group) ? n : NULL;
145 }
146 
lock_mode_to_index(ldlm_mode_t mode)147 static inline int lock_mode_to_index(ldlm_mode_t mode)
148 {
149 	int index;
150 
151 	LASSERT(mode != 0);
152 	LASSERT(IS_PO2(mode));
153 	for (index = -1; mode; index++)
154 		mode >>= 1;
155 	LASSERT(index < LCK_MODE_NUM);
156 	return index;
157 }
158 
159 /** Add newly granted lock into interval tree for the resource. */
ldlm_extent_add_lock(struct ldlm_resource * res,struct ldlm_lock * lock)160 void ldlm_extent_add_lock(struct ldlm_resource *res,
161 			  struct ldlm_lock *lock)
162 {
163 	struct interval_node *found, **root;
164 	struct ldlm_interval *node;
165 	struct ldlm_extent *extent;
166 	int idx;
167 
168 	LASSERT(lock->l_granted_mode == lock->l_req_mode);
169 
170 	node = lock->l_tree_node;
171 	LASSERT(node != NULL);
172 	LASSERT(!interval_is_intree(&node->li_node));
173 
174 	idx = lock_mode_to_index(lock->l_granted_mode);
175 	LASSERT(lock->l_granted_mode == 1 << idx);
176 	LASSERT(lock->l_granted_mode == res->lr_itree[idx].lit_mode);
177 
178 	/* node extent initialize */
179 	extent = &lock->l_policy_data.l_extent;
180 	interval_set(&node->li_node, extent->start, extent->end);
181 
182 	root = &res->lr_itree[idx].lit_root;
183 	found = interval_insert(&node->li_node, root);
184 	if (found) { /* The policy group found. */
185 		struct ldlm_interval *tmp;
186 
187 		tmp = ldlm_interval_detach(lock);
188 		LASSERT(tmp != NULL);
189 		ldlm_interval_free(tmp);
190 		ldlm_interval_attach(to_ldlm_interval(found), lock);
191 	}
192 	res->lr_itree[idx].lit_size++;
193 
194 	/* even though we use interval tree to manage the extent lock, we also
195 	 * add the locks into grant list, for debug purpose, .. */
196 	ldlm_resource_add_lock(res, &res->lr_granted, lock);
197 }
198 
199 /** Remove cancelled lock from resource interval tree. */
ldlm_extent_unlink_lock(struct ldlm_lock * lock)200 void ldlm_extent_unlink_lock(struct ldlm_lock *lock)
201 {
202 	struct ldlm_resource *res = lock->l_resource;
203 	struct ldlm_interval *node = lock->l_tree_node;
204 	struct ldlm_interval_tree *tree;
205 	int idx;
206 
207 	if (!node || !interval_is_intree(&node->li_node)) /* duplicate unlink */
208 		return;
209 
210 	idx = lock_mode_to_index(lock->l_granted_mode);
211 	LASSERT(lock->l_granted_mode == 1 << idx);
212 	tree = &res->lr_itree[idx];
213 
214 	LASSERT(tree->lit_root != NULL); /* assure the tree is not null */
215 
216 	tree->lit_size--;
217 	node = ldlm_interval_detach(lock);
218 	if (node) {
219 		interval_erase(&node->li_node, &tree->lit_root);
220 		ldlm_interval_free(node);
221 	}
222 }
223 
ldlm_extent_policy_wire_to_local(const ldlm_wire_policy_data_t * wpolicy,ldlm_policy_data_t * lpolicy)224 void ldlm_extent_policy_wire_to_local(const ldlm_wire_policy_data_t *wpolicy,
225 				     ldlm_policy_data_t *lpolicy)
226 {
227 	memset(lpolicy, 0, sizeof(*lpolicy));
228 	lpolicy->l_extent.start = wpolicy->l_extent.start;
229 	lpolicy->l_extent.end = wpolicy->l_extent.end;
230 	lpolicy->l_extent.gid = wpolicy->l_extent.gid;
231 }
232 
ldlm_extent_policy_local_to_wire(const ldlm_policy_data_t * lpolicy,ldlm_wire_policy_data_t * wpolicy)233 void ldlm_extent_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
234 				     ldlm_wire_policy_data_t *wpolicy)
235 {
236 	memset(wpolicy, 0, sizeof(*wpolicy));
237 	wpolicy->l_extent.start = lpolicy->l_extent.start;
238 	wpolicy->l_extent.end = lpolicy->l_extent.end;
239 	wpolicy->l_extent.gid = lpolicy->l_extent.gid;
240 }
241