staging/lustre/ldlm: move namespaces/early_lock_cancel to sysfs
[linux-2.6-block.git] / drivers / staging / lustre / lustre / ldlm / ldlm_resource.c
CommitLineData
d7e09d03
PT
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_resource.c
37 *
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Peter Braam <braam@clusterfs.com>
40 */
41
42#define DEBUG_SUBSYSTEM S_LDLM
e27db149
GKH
43#include "../include/lustre_dlm.h"
44#include "../include/lustre_fid.h"
45#include "../include/obd_class.h"
d7e09d03
PT
46#include "ldlm_internal.h"
47
48struct kmem_cache *ldlm_resource_slab, *ldlm_lock_slab;
49
91a50030
OD
50int ldlm_srv_namespace_nr = 0;
51int ldlm_cli_namespace_nr = 0;
d7e09d03
PT
52
53struct mutex ldlm_srv_namespace_lock;
54LIST_HEAD(ldlm_srv_namespace_list);
55
56struct mutex ldlm_cli_namespace_lock;
91a50030
OD
57/* Client Namespaces that have active resources in them.
58 * Once all resources go away, ldlm_poold moves such namespaces to the
59 * inactive list */
60LIST_HEAD(ldlm_cli_active_namespace_list);
61/* Client namespaces that don't have any locks in them */
62LIST_HEAD(ldlm_cli_inactive_namespace_list);
d7e09d03 63
b59fe845 64struct proc_dir_entry *ldlm_type_proc_dir = NULL;
381ca677 65static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
b59fe845 66struct proc_dir_entry *ldlm_svc_proc_dir = NULL;
d7e09d03 67
d7e09d03
PT
68/* during debug dump certain amount of granted locks for one resource to avoid
69 * DDOS. */
70unsigned int ldlm_dump_granted_max = 256;
71
4d089bb4 72#if defined(CONFIG_PROC_FS)
e84962e3 73static ssize_t lprocfs_wr_dump_ns(struct file *file, const char __user *buffer,
73bb1da6 74 size_t count, loff_t *off)
d7e09d03
PT
75{
76 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
77 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
0a3bdb00 78 return count;
d7e09d03 79}
73bb1da6
PT
80LPROC_SEQ_FOPS_WR_ONLY(ldlm, dump_ns);
81
82LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint);
83LPROC_SEQ_FOPS_RO_TYPE(ldlm, uint);
d7e09d03
PT
84
85int ldlm_proc_setup(void)
86{
87 int rc;
88 struct lprocfs_vars list[] = {
5bcfab13 89 { "dump_namespaces", &ldlm_dump_ns_fops, NULL, 0222 },
73bb1da6
PT
90 { "dump_granted_max", &ldlm_rw_uint_fops,
91 &ldlm_dump_granted_max },
43ee4160 92 { NULL } };
d7e09d03
PT
93 LASSERT(ldlm_ns_proc_dir == NULL);
94
95 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
96 proc_lustre_root,
97 NULL, NULL);
98 if (IS_ERR(ldlm_type_proc_dir)) {
99 CERROR("LProcFS failed in ldlm-init\n");
100 rc = PTR_ERR(ldlm_type_proc_dir);
d1c0d446 101 goto err;
d7e09d03
PT
102 }
103
104 ldlm_ns_proc_dir = lprocfs_register("namespaces",
105 ldlm_type_proc_dir,
106 NULL, NULL);
107 if (IS_ERR(ldlm_ns_proc_dir)) {
108 CERROR("LProcFS failed in ldlm-init\n");
109 rc = PTR_ERR(ldlm_ns_proc_dir);
d1c0d446 110 goto err_type;
d7e09d03
PT
111 }
112
113 ldlm_svc_proc_dir = lprocfs_register("services",
114 ldlm_type_proc_dir,
115 NULL, NULL);
116 if (IS_ERR(ldlm_svc_proc_dir)) {
117 CERROR("LProcFS failed in ldlm-init\n");
118 rc = PTR_ERR(ldlm_svc_proc_dir);
d1c0d446 119 goto err_ns;
d7e09d03
PT
120 }
121
122 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
123
0a3bdb00 124 return 0;
d7e09d03
PT
125
126err_ns:
127 lprocfs_remove(&ldlm_ns_proc_dir);
128err_type:
129 lprocfs_remove(&ldlm_type_proc_dir);
130err:
131 ldlm_svc_proc_dir = NULL;
1e4db2b3
PT
132 ldlm_type_proc_dir = NULL;
133 ldlm_ns_proc_dir = NULL;
0a3bdb00 134 return rc;
d7e09d03
PT
135}
136
137void ldlm_proc_cleanup(void)
138{
139 if (ldlm_svc_proc_dir)
140 lprocfs_remove(&ldlm_svc_proc_dir);
141
142 if (ldlm_ns_proc_dir)
143 lprocfs_remove(&ldlm_ns_proc_dir);
144
145 if (ldlm_type_proc_dir)
146 lprocfs_remove(&ldlm_type_proc_dir);
1e4db2b3
PT
147
148 ldlm_svc_proc_dir = NULL;
149 ldlm_type_proc_dir = NULL;
150 ldlm_ns_proc_dir = NULL;
d7e09d03
PT
151}
152
61d4a2e4
OD
153static ssize_t resource_count_show(struct kobject *kobj, struct attribute *attr,
154 char *buf)
d7e09d03 155{
61d4a2e4
OD
156 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
157 ns_kobj);
d7e09d03 158 __u64 res = 0;
6ea510c1 159 struct cfs_hash_bd bd;
d7e09d03
PT
160 int i;
161
6e3dd654 162 /* result is not strictly consistent */
d7e09d03
PT
163 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
164 res += cfs_hash_bd_count_get(&bd);
61d4a2e4 165 return sprintf(buf, "%lld\n", res);
d7e09d03 166}
61d4a2e4 167LUSTRE_RO_ATTR(resource_count);
d7e09d03 168
63af1f57
OD
169static ssize_t lock_count_show(struct kobject *kobj, struct attribute *attr,
170 char *buf)
d7e09d03 171{
63af1f57
OD
172 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
173 ns_kobj);
d7e09d03
PT
174 __u64 locks;
175
176 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
177 LPROCFS_FIELDS_FLAGS_SUM);
63af1f57 178 return sprintf(buf, "%lld\n", locks);
d7e09d03 179}
63af1f57 180LUSTRE_RO_ATTR(lock_count);
d7e09d03 181
6784096b
OD
182static ssize_t lru_size_show(struct kobject *kobj, struct attribute *attr,
183 char *buf)
d7e09d03 184{
6784096b
OD
185 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
186 ns_kobj);
d7e09d03
PT
187 __u32 *nr = &ns->ns_max_unused;
188
189 if (ns_connect_lru_resize(ns))
190 nr = &ns->ns_nr_unused;
6784096b 191 return sprintf(buf, "%u", *nr);
d7e09d03
PT
192}
193
6784096b
OD
194static ssize_t lru_size_store(struct kobject *kobj, struct attribute *attr,
195 const char *buffer, size_t count)
d7e09d03 196{
6784096b
OD
197 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
198 ns_kobj);
d7e09d03
PT
199 unsigned long tmp;
200 int lru_resize;
ae59720b 201 int err;
d7e09d03 202
6784096b 203 if (strncmp(buffer, "clear", 5) == 0) {
d7e09d03
PT
204 CDEBUG(D_DLMTRACE,
205 "dropping all unused locks from namespace %s\n",
206 ldlm_ns_name(ns));
207 if (ns_connect_lru_resize(ns)) {
208 int canceled, unused = ns->ns_nr_unused;
209
210 /* Try to cancel all @ns_nr_unused locks. */
211 canceled = ldlm_cancel_lru(ns, unused, 0,
212 LDLM_CANCEL_PASSED);
213 if (canceled < unused) {
214 CDEBUG(D_DLMTRACE,
2d00bd17
JP
215 "not all requested locks are canceled, requested: %d, canceled: %d\n",
216 unused,
d7e09d03
PT
217 canceled);
218 return -EINVAL;
219 }
220 } else {
221 tmp = ns->ns_max_unused;
222 ns->ns_max_unused = 0;
223 ldlm_cancel_lru(ns, 0, 0, LDLM_CANCEL_PASSED);
224 ns->ns_max_unused = tmp;
225 }
226 return count;
227 }
228
6784096b 229 err = kstrtoul(buffer, 10, &tmp);
ae59720b 230 if (err != 0) {
6784096b 231 CERROR("lru_size: invalid value written\n");
d7e09d03
PT
232 return -EINVAL;
233 }
234 lru_resize = (tmp == 0);
235
236 if (ns_connect_lru_resize(ns)) {
237 if (!lru_resize)
238 ns->ns_max_unused = (unsigned int)tmp;
239
240 if (tmp > ns->ns_nr_unused)
241 tmp = ns->ns_nr_unused;
242 tmp = ns->ns_nr_unused - tmp;
243
244 CDEBUG(D_DLMTRACE,
245 "changing namespace %s unused locks from %u to %u\n",
246 ldlm_ns_name(ns), ns->ns_nr_unused,
247 (unsigned int)tmp);
248 ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_CANCEL_PASSED);
249
250 if (!lru_resize) {
251 CDEBUG(D_DLMTRACE,
252 "disable lru_resize for namespace %s\n",
253 ldlm_ns_name(ns));
254 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
255 }
256 } else {
257 CDEBUG(D_DLMTRACE,
258 "changing namespace %s max_unused from %u to %u\n",
259 ldlm_ns_name(ns), ns->ns_max_unused,
260 (unsigned int)tmp);
261 ns->ns_max_unused = (unsigned int)tmp;
262 ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_CANCEL_PASSED);
263
264 /* Make sure that LRU resize was originally supported before
265 * turning it on here. */
266 if (lru_resize &&
267 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
268 CDEBUG(D_DLMTRACE,
269 "enable lru_resize for namespace %s\n",
270 ldlm_ns_name(ns));
271 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
272 }
273 }
274
275 return count;
276}
6784096b 277LUSTRE_RW_ATTR(lru_size);
d7e09d03 278
87d32094
OD
279static ssize_t early_lock_cancel_show(struct kobject *kobj,
280 struct attribute *attr,
281 char *buf)
d7e09d03 282{
87d32094
OD
283 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
284 ns_kobj);
d7e09d03 285
87d32094 286 return sprintf(buf, "%d\n", ns_connect_cancelset(ns));
d7e09d03
PT
287}
288
87d32094
OD
289static ssize_t early_lock_cancel_store(struct kobject *kobj,
290 struct attribute *attr,
291 const char *buffer,
292 size_t count)
d7e09d03 293{
87d32094
OD
294 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
295 ns_kobj);
296 unsigned long supp = -1;
d7e09d03
PT
297 int rc;
298
87d32094 299 rc = kstrtoul(buffer, 10, &supp);
d7e09d03
PT
300 if (rc < 0)
301 return rc;
302
303 if (supp == 0)
304 ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
305 else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
306 ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
307 return count;
308}
87d32094 309LUSTRE_RW_ATTR(early_lock_cancel);
d7e09d03 310
18fd8850
OD
311/* These are for namespaces in /sys/fs/lustre/ldlm/namespaces/ */
312static struct attribute *ldlm_ns_attrs[] = {
61d4a2e4 313 &lustre_attr_resource_count.attr,
63af1f57 314 &lustre_attr_lock_count.attr,
6784096b 315 &lustre_attr_lru_size.attr,
87d32094 316 &lustre_attr_early_lock_cancel.attr,
18fd8850
OD
317 NULL,
318};
319
320static void ldlm_ns_release(struct kobject *kobj)
321{
322 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
323 ns_kobj);
324 complete(&ns->ns_kobj_unregister);
325}
326
327static struct kobj_type ldlm_ns_ktype = {
328 .default_attrs = ldlm_ns_attrs,
329 .sysfs_ops = &lustre_sysfs_ops,
330 .release = ldlm_ns_release,
331};
332
d7e09d03
PT
333void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
334{
73bb1da6 335 if (ns->ns_proc_dir_entry == NULL)
d7e09d03
PT
336 CERROR("dlm namespace %s has no procfs dir?\n",
337 ldlm_ns_name(ns));
73bb1da6
PT
338 else
339 lprocfs_remove(&ns->ns_proc_dir_entry);
d7e09d03
PT
340
341 if (ns->ns_stats != NULL)
342 lprocfs_free_stats(&ns->ns_stats);
343}
344
18fd8850
OD
345void ldlm_namespace_sysfs_unregister(struct ldlm_namespace *ns)
346{
347 kobject_put(&ns->ns_kobj);
348 wait_for_completion(&ns->ns_kobj_unregister);
349}
350
73bb1da6
PT
351#define LDLM_NS_ADD_VAR(name, var, ops) \
352 do { \
353 snprintf(lock_name, MAX_STRING_SIZE, name); \
354 lock_vars[0].data = var; \
355 lock_vars[0].fops = ops; \
5bcfab13 356 lprocfs_add_vars(ns_pde, lock_vars, NULL); \
73bb1da6
PT
357 } while (0)
358
18fd8850
OD
359
360int ldlm_namespace_sysfs_register(struct ldlm_namespace *ns)
361{
362 int err;
363
364 ns->ns_kobj.kset = ldlm_ns_kset;
365 init_completion(&ns->ns_kobj_unregister);
366 err = kobject_init_and_add(&ns->ns_kobj, &ldlm_ns_ktype, NULL,
367 "%s", ldlm_ns_name(ns));
368
63af1f57
OD
369 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
370 if (ns->ns_stats == NULL) {
371 kobject_put(&ns->ns_kobj);
372 return -ENOMEM;
373 }
374
375 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
376 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
377
18fd8850
OD
378 return err;
379}
380
d7e09d03
PT
381int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
382{
383 struct lprocfs_vars lock_vars[2];
384 char lock_name[MAX_STRING_SIZE + 1];
b59fe845 385 struct proc_dir_entry *ns_pde;
d7e09d03
PT
386
387 LASSERT(ns != NULL);
388 LASSERT(ns->ns_rs_hash != NULL);
389
73bb1da6
PT
390 if (ns->ns_proc_dir_entry != NULL) {
391 ns_pde = ns->ns_proc_dir_entry;
392 } else {
393 ns_pde = proc_mkdir(ldlm_ns_name(ns), ldlm_ns_proc_dir);
394 if (ns_pde == NULL)
395 return -ENOMEM;
396 ns->ns_proc_dir_entry = ns_pde;
397 }
398
d7e09d03
PT
399 lock_name[MAX_STRING_SIZE] = '\0';
400
401 memset(lock_vars, 0, sizeof(lock_vars));
402 lock_vars[0].name = lock_name;
403
d7e09d03 404 if (ns_is_client(ns)) {
73bb1da6
PT
405 LDLM_NS_ADD_VAR("lock_unused_count", &ns->ns_nr_unused,
406 &ldlm_uint_fops);
73bb1da6
PT
407 LDLM_NS_ADD_VAR("lru_max_age", &ns->ns_max_age,
408 &ldlm_rw_uint_fops);
d7e09d03 409 } else {
73bb1da6
PT
410 LDLM_NS_ADD_VAR("ctime_age_limit", &ns->ns_ctime_age_limit,
411 &ldlm_rw_uint_fops);
412 LDLM_NS_ADD_VAR("lock_timeouts", &ns->ns_timeouts,
413 &ldlm_uint_fops);
414 LDLM_NS_ADD_VAR("max_nolock_bytes", &ns->ns_max_nolock_size,
415 &ldlm_rw_uint_fops);
416 LDLM_NS_ADD_VAR("contention_seconds", &ns->ns_contention_time,
417 &ldlm_rw_uint_fops);
418 LDLM_NS_ADD_VAR("contended_locks", &ns->ns_contended_locks,
419 &ldlm_rw_uint_fops);
420 LDLM_NS_ADD_VAR("max_parallel_ast", &ns->ns_max_parallel_ast,
421 &ldlm_rw_uint_fops);
d7e09d03
PT
422 }
423 return 0;
424}
425#undef MAX_STRING_SIZE
f267cdb4 426#else /* CONFIG_PROC_FS */
d7e09d03 427
43ee4160
AR
428#define ldlm_namespace_proc_unregister(ns) ({; })
429#define ldlm_namespace_proc_register(ns) ({0; })
d7e09d03 430
f267cdb4 431#endif /* CONFIG_PROC_FS */
d7e09d03 432
6da6eabe 433static unsigned ldlm_res_hop_hash(struct cfs_hash *hs,
d7e09d03
PT
434 const void *key, unsigned mask)
435{
436 const struct ldlm_res_id *id = key;
437 unsigned val = 0;
438 unsigned i;
439
440 for (i = 0; i < RES_NAME_SIZE; i++)
441 val += id->name[i];
442 return val & mask;
443}
444
6da6eabe 445static unsigned ldlm_res_hop_fid_hash(struct cfs_hash *hs,
d7e09d03
PT
446 const void *key, unsigned mask)
447{
448 const struct ldlm_res_id *id = key;
449 struct lu_fid fid;
450 __u32 hash;
451 __u32 val;
452
453 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
454 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
455 fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
456
457 hash = fid_flatten32(&fid);
458 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
459 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
460 val = id->name[LUSTRE_RES_ID_HSH_OFF];
461 hash += (val >> 5) + (val << 11);
462 } else {
463 val = fid_oid(&fid);
464 }
72c0824a 465 hash = hash_long(hash, hs->hs_bkt_bits);
d7e09d03 466 /* give me another random factor */
72c0824a 467 hash -= hash_long((unsigned long)hs, val % 11 + 3);
d7e09d03
PT
468
469 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
470 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
471
472 return hash & mask;
473}
474
475static void *ldlm_res_hop_key(struct hlist_node *hnode)
476{
477 struct ldlm_resource *res;
478
479 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
480 return &res->lr_name;
481}
482
483static int ldlm_res_hop_keycmp(const void *key, struct hlist_node *hnode)
484{
485 struct ldlm_resource *res;
486
487 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
488 return ldlm_res_eq((const struct ldlm_res_id *)key,
489 (const struct ldlm_res_id *)&res->lr_name);
490}
491
492static void *ldlm_res_hop_object(struct hlist_node *hnode)
493{
494 return hlist_entry(hnode, struct ldlm_resource, lr_hash);
495}
496
e7ddc48c
AR
497static void ldlm_res_hop_get_locked(struct cfs_hash *hs,
498 struct hlist_node *hnode)
d7e09d03
PT
499{
500 struct ldlm_resource *res;
501
502 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
503 ldlm_resource_getref(res);
504}
505
e7ddc48c
AR
506static void ldlm_res_hop_put_locked(struct cfs_hash *hs,
507 struct hlist_node *hnode)
d7e09d03
PT
508{
509 struct ldlm_resource *res;
510
511 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
512 /* cfs_hash_for_each_nolock is the only chance we call it */
513 ldlm_resource_putref_locked(res);
514}
515
6da6eabe 516static void ldlm_res_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
d7e09d03
PT
517{
518 struct ldlm_resource *res;
519
520 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
521 ldlm_resource_putref(res);
522}
523
524cfs_hash_ops_t ldlm_ns_hash_ops = {
525 .hs_hash = ldlm_res_hop_hash,
526 .hs_key = ldlm_res_hop_key,
527 .hs_keycmp = ldlm_res_hop_keycmp,
528 .hs_keycpy = NULL,
529 .hs_object = ldlm_res_hop_object,
530 .hs_get = ldlm_res_hop_get_locked,
531 .hs_put_locked = ldlm_res_hop_put_locked,
532 .hs_put = ldlm_res_hop_put
533};
534
535cfs_hash_ops_t ldlm_ns_fid_hash_ops = {
536 .hs_hash = ldlm_res_hop_fid_hash,
537 .hs_key = ldlm_res_hop_key,
538 .hs_keycmp = ldlm_res_hop_keycmp,
539 .hs_keycpy = NULL,
540 .hs_object = ldlm_res_hop_object,
541 .hs_get = ldlm_res_hop_get_locked,
542 .hs_put_locked = ldlm_res_hop_put_locked,
543 .hs_put = ldlm_res_hop_put
544};
545
38d676da 546struct ldlm_ns_hash_def {
d7e09d03
PT
547 ldlm_ns_type_t nsd_type;
548 /** hash bucket bits */
549 unsigned nsd_bkt_bits;
550 /** hash bits */
551 unsigned nsd_all_bits;
552 /** hash operations */
553 cfs_hash_ops_t *nsd_hops;
38d676da 554};
d7e09d03 555
38d676da 556struct ldlm_ns_hash_def ldlm_ns_hash_defs[] = {
d7e09d03
PT
557 {
558 .nsd_type = LDLM_NS_TYPE_MDC,
559 .nsd_bkt_bits = 11,
560 .nsd_all_bits = 16,
561 .nsd_hops = &ldlm_ns_fid_hash_ops,
562 },
563 {
564 .nsd_type = LDLM_NS_TYPE_MDT,
565 .nsd_bkt_bits = 14,
566 .nsd_all_bits = 21,
567 .nsd_hops = &ldlm_ns_fid_hash_ops,
568 },
569 {
570 .nsd_type = LDLM_NS_TYPE_OSC,
571 .nsd_bkt_bits = 8,
572 .nsd_all_bits = 12,
573 .nsd_hops = &ldlm_ns_hash_ops,
574 },
575 {
576 .nsd_type = LDLM_NS_TYPE_OST,
577 .nsd_bkt_bits = 11,
578 .nsd_all_bits = 17,
579 .nsd_hops = &ldlm_ns_hash_ops,
580 },
581 {
582 .nsd_type = LDLM_NS_TYPE_MGC,
583 .nsd_bkt_bits = 4,
584 .nsd_all_bits = 4,
585 .nsd_hops = &ldlm_ns_hash_ops,
586 },
587 {
588 .nsd_type = LDLM_NS_TYPE_MGT,
589 .nsd_bkt_bits = 4,
590 .nsd_all_bits = 4,
591 .nsd_hops = &ldlm_ns_hash_ops,
592 },
593 {
594 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
595 },
596};
597
598/**
599 * Create and initialize new empty namespace.
600 */
601struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
602 ldlm_side_t client,
603 ldlm_appetite_t apt,
604 ldlm_ns_type_t ns_type)
605{
606 struct ldlm_namespace *ns = NULL;
607 struct ldlm_ns_bucket *nsb;
38d676da 608 struct ldlm_ns_hash_def *nsd;
6ea510c1 609 struct cfs_hash_bd bd;
d7e09d03
PT
610 int idx;
611 int rc;
d7e09d03
PT
612
613 LASSERT(obd != NULL);
614
615 rc = ldlm_get_ref();
616 if (rc) {
617 CERROR("ldlm_get_ref failed: %d\n", rc);
0a3bdb00 618 return NULL;
d7e09d03
PT
619 }
620
43ee4160 621 for (idx = 0;; idx++) {
d7e09d03
PT
622 nsd = &ldlm_ns_hash_defs[idx];
623 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
624 CERROR("Unknown type %d for ns %s\n", ns_type, name);
d1c0d446 625 goto out_ref;
d7e09d03
PT
626 }
627
628 if (nsd->nsd_type == ns_type)
629 break;
630 }
631
352f7891 632 ns = kzalloc(sizeof(*ns), GFP_NOFS);
d7e09d03 633 if (!ns)
d1c0d446 634 goto out_ref;
d7e09d03
PT
635
636 ns->ns_rs_hash = cfs_hash_create(name,
637 nsd->nsd_all_bits, nsd->nsd_all_bits,
638 nsd->nsd_bkt_bits, sizeof(*nsb),
639 CFS_HASH_MIN_THETA,
640 CFS_HASH_MAX_THETA,
641 nsd->nsd_hops,
642 CFS_HASH_DEPTH |
643 CFS_HASH_BIGNAME |
644 CFS_HASH_SPIN_BKTLOCK |
645 CFS_HASH_NO_ITEMREF);
646 if (ns->ns_rs_hash == NULL)
d1c0d446 647 goto out_ns;
d7e09d03
PT
648
649 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
650 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
651 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
652 nsb->nsb_namespace = ns;
653 }
654
655 ns->ns_obd = obd;
656 ns->ns_appetite = apt;
657 ns->ns_client = client;
658
659 INIT_LIST_HEAD(&ns->ns_list_chain);
660 INIT_LIST_HEAD(&ns->ns_unused_list);
661 spin_lock_init(&ns->ns_lock);
662 atomic_set(&ns->ns_bref, 0);
663 init_waitqueue_head(&ns->ns_waitq);
664
665 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
666 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
667 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
668
669 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
670 ns->ns_nr_unused = 0;
671 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
672 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
673 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
674 ns->ns_timeouts = 0;
675 ns->ns_orig_connect_flags = 0;
676 ns->ns_connect_flags = 0;
677 ns->ns_stopping = 0;
18fd8850
OD
678
679 rc = ldlm_namespace_sysfs_register(ns);
680 if (rc != 0) {
681 CERROR("Can't initialize ns sysfs, rc %d\n", rc);
682 goto out_hash;
683 }
684
d7e09d03
PT
685 rc = ldlm_namespace_proc_register(ns);
686 if (rc != 0) {
687 CERROR("Can't initialize ns proc, rc %d\n", rc);
18fd8850 688 goto out_sysfs;
d7e09d03
PT
689 }
690
91a50030 691 idx = ldlm_namespace_nr_read(client);
d7e09d03
PT
692 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
693 if (rc) {
694 CERROR("Can't initialize lock pool, rc %d\n", rc);
d1c0d446 695 goto out_proc;
d7e09d03
PT
696 }
697
698 ldlm_namespace_register(ns, client);
0a3bdb00 699 return ns;
d7e09d03
PT
700out_proc:
701 ldlm_namespace_proc_unregister(ns);
18fd8850
OD
702out_sysfs:
703 ldlm_namespace_sysfs_unregister(ns);
d7e09d03
PT
704 ldlm_namespace_cleanup(ns, 0);
705out_hash:
706 cfs_hash_putref(ns->ns_rs_hash);
707out_ns:
352f7891 708 kfree(ns);
d7e09d03
PT
709out_ref:
710 ldlm_put_ref();
0a3bdb00 711 return NULL;
d7e09d03
PT
712}
713EXPORT_SYMBOL(ldlm_namespace_new);
714
715extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
716
717/**
718 * Cancel and destroy all locks on a resource.
719 *
720 * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
721 * clean up. This is currently only used for recovery, and we make
722 * certain assumptions as a result--notably, that we shouldn't cancel
723 * locks with refs.
724 */
725static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
726 __u64 flags)
727{
728 struct list_head *tmp;
729 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
730 bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
731
732 do {
733 struct ldlm_lock *lock = NULL;
734
735 /* First, we look for non-cleaned-yet lock
736 * all cleaned locks are marked by CLEANED flag. */
737 lock_res(res);
738 list_for_each(tmp, q) {
739 lock = list_entry(tmp, struct ldlm_lock,
740 l_res_link);
741 if (lock->l_flags & LDLM_FL_CLEANED) {
742 lock = NULL;
743 continue;
744 }
745 LDLM_LOCK_GET(lock);
746 lock->l_flags |= LDLM_FL_CLEANED;
747 break;
748 }
749
750 if (lock == NULL) {
751 unlock_res(res);
752 break;
753 }
754
755 /* Set CBPENDING so nothing in the cancellation path
756 * can match this lock. */
757 lock->l_flags |= LDLM_FL_CBPENDING;
758 lock->l_flags |= LDLM_FL_FAILED;
759 lock->l_flags |= flags;
760
761 /* ... without sending a CANCEL message for local_only. */
762 if (local_only)
763 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
764
765 if (local_only && (lock->l_readers || lock->l_writers)) {
766 /* This is a little bit gross, but much better than the
767 * alternative: pretend that we got a blocking AST from
768 * the server, so that when the lock is decref'd, it
769 * will go away ... */
770 unlock_res(res);
771 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
772 if (lock->l_completion_ast)
773 lock->l_completion_ast(lock, 0, NULL);
774 LDLM_LOCK_RELEASE(lock);
775 continue;
776 }
777
778 if (client) {
779 struct lustre_handle lockh;
780
781 unlock_res(res);
782 ldlm_lock2handle(lock, &lockh);
783 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
784 if (rc)
785 CERROR("ldlm_cli_cancel: %d\n", rc);
786 } else {
787 ldlm_resource_unlink_lock(lock);
788 unlock_res(res);
2d00bd17 789 LDLM_DEBUG(lock, "Freeing a lock still held by a client node");
d7e09d03
PT
790 ldlm_lock_destroy(lock);
791 }
792 LDLM_LOCK_RELEASE(lock);
793 } while (1);
794}
795
6da6eabe 796static int ldlm_resource_clean(struct cfs_hash *hs, struct cfs_hash_bd *bd,
d7e09d03
PT
797 struct hlist_node *hnode, void *arg)
798{
799 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
800 __u64 flags = *(__u64 *)arg;
801
802 cleanup_resource(res, &res->lr_granted, flags);
803 cleanup_resource(res, &res->lr_converting, flags);
804 cleanup_resource(res, &res->lr_waiting, flags);
805
806 return 0;
807}
808
6da6eabe 809static int ldlm_resource_complain(struct cfs_hash *hs, struct cfs_hash_bd *bd,
d7e09d03
PT
810 struct hlist_node *hnode, void *arg)
811{
812 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
813
814 lock_res(res);
6d95e048
AD
815 CERROR("%s: namespace resource "DLDLMRES
816 " (%p) refcount nonzero (%d) after lock cleanup; forcing cleanup.\n",
817 ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
d7e09d03
PT
818 atomic_read(&res->lr_refcount) - 1);
819
820 ldlm_resource_dump(D_ERROR, res);
821 unlock_res(res);
822 return 0;
823}
824
825/**
826 * Cancel and destroy all locks in the namespace.
827 *
828 * Typically used during evictions when server notified client that it was
829 * evicted and all of its state needs to be destroyed.
830 * Also used during shutdown.
831 */
832int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
833{
834 if (ns == NULL) {
835 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
836 return ELDLM_OK;
837 }
838
839 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean, &flags);
840 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
841 return ELDLM_OK;
842}
843EXPORT_SYMBOL(ldlm_namespace_cleanup);
844
845/**
846 * Attempts to free namespace.
847 *
848 * Only used when namespace goes away, like during an unmount.
849 */
850static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
851{
d7e09d03
PT
852 /* At shutdown time, don't call the cancellation callback */
853 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
854
855 if (atomic_read(&ns->ns_bref) > 0) {
856 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
857 int rc;
902f3bb1 858
d7e09d03
PT
859 CDEBUG(D_DLMTRACE,
860 "dlm namespace %s free waiting on refcount %d\n",
861 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
862force_wait:
863 if (force)
864 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
865
866 rc = l_wait_event(ns->ns_waitq,
867 atomic_read(&ns->ns_bref) == 0, &lwi);
868
869 /* Forced cleanups should be able to reclaim all references,
870 * so it's safe to wait forever... we can't leak locks... */
871 if (force && rc == -ETIMEDOUT) {
2d00bd17
JP
872 LCONSOLE_ERROR("Forced cleanup waiting for %s namespace with %d resources in use, (rc=%d)\n",
873 ldlm_ns_name(ns),
d7e09d03 874 atomic_read(&ns->ns_bref), rc);
d1c0d446 875 goto force_wait;
d7e09d03
PT
876 }
877
878 if (atomic_read(&ns->ns_bref)) {
2d00bd17 879 LCONSOLE_ERROR("Cleanup waiting for %s namespace with %d resources in use, (rc=%d)\n",
d7e09d03
PT
880 ldlm_ns_name(ns),
881 atomic_read(&ns->ns_bref), rc);
0a3bdb00 882 return ELDLM_NAMESPACE_EXISTS;
d7e09d03
PT
883 }
884 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
885 ldlm_ns_name(ns));
886 }
887
0a3bdb00 888 return ELDLM_OK;
d7e09d03
PT
889}
890
891/**
892 * Performs various cleanups for passed \a ns to make it drop refc and be
893 * ready for freeing. Waits for refc == 0.
894 *
895 * The following is done:
896 * (0) Unregister \a ns from its list to make inaccessible for potential
897 * users like pools thread and others;
898 * (1) Clear all locks in \a ns.
899 */
900void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
901 struct obd_import *imp,
902 int force)
903{
904 int rc;
29aaf496 905
bf050e55 906 if (!ns)
d7e09d03 907 return;
d7e09d03
PT
908
909 spin_lock(&ns->ns_lock);
910 ns->ns_stopping = 1;
911 spin_unlock(&ns->ns_lock);
912
913 /*
914 * Can fail with -EINTR when force == 0 in which case try harder.
915 */
916 rc = __ldlm_namespace_free(ns, force);
917 if (rc != ELDLM_OK) {
918 if (imp) {
919 ptlrpc_disconnect_import(imp, 0);
920 ptlrpc_invalidate_import(imp);
921 }
922
923 /*
924 * With all requests dropped and the import inactive
6e3dd654 925 * we are guaranteed all reference will be dropped.
d7e09d03
PT
926 */
927 rc = __ldlm_namespace_free(ns, 1);
928 LASSERT(rc == 0);
929 }
d7e09d03
PT
930}
931
932/**
933 * Performs freeing memory structures related to \a ns. This is only done
934 * when ldlm_namespce_free_prior() successfully removed all resources
935 * referencing \a ns and its refc == 0.
936 */
937void ldlm_namespace_free_post(struct ldlm_namespace *ns)
938{
bf050e55 939 if (!ns)
d7e09d03 940 return;
d7e09d03
PT
941
942 /* Make sure that nobody can find this ns in its list. */
943 ldlm_namespace_unregister(ns, ns->ns_client);
944 /* Fini pool _before_ parent proc dir is removed. This is important as
945 * ldlm_pool_fini() removes own proc dir which is child to @dir.
946 * Removing it after @dir may cause oops. */
947 ldlm_pool_fini(&ns->ns_pool);
948
949 ldlm_namespace_proc_unregister(ns);
950 cfs_hash_putref(ns->ns_rs_hash);
951 /* Namespace \a ns should be not on list at this time, otherwise
952 * this will cause issues related to using freed \a ns in poold
953 * thread. */
954 LASSERT(list_empty(&ns->ns_list_chain));
352f7891 955 kfree(ns);
d7e09d03 956 ldlm_put_ref();
d7e09d03
PT
957}
958
959/**
960 * Cleanup the resource, and free namespace.
961 * bug 12864:
962 * Deadlock issue:
963 * proc1: destroy import
964 * class_disconnect_export(grab cl_sem) ->
965 * -> ldlm_namespace_free ->
966 * -> lprocfs_remove(grab _lprocfs_lock).
967 * proc2: read proc info
968 * lprocfs_fops_read(grab _lprocfs_lock) ->
969 * -> osc_rd_active, etc(grab cl_sem).
970 *
971 * So that I have to split the ldlm_namespace_free into two parts - the first
972 * part ldlm_namespace_free_prior is used to cleanup the resource which is
973 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
974 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
975 * held.
976 */
977void ldlm_namespace_free(struct ldlm_namespace *ns,
978 struct obd_import *imp,
979 int force)
980{
981 ldlm_namespace_free_prior(ns, imp, force);
982 ldlm_namespace_free_post(ns);
983}
984EXPORT_SYMBOL(ldlm_namespace_free);
985
986void ldlm_namespace_get(struct ldlm_namespace *ns)
987{
988 atomic_inc(&ns->ns_bref);
989}
990EXPORT_SYMBOL(ldlm_namespace_get);
991
91a50030
OD
992/* This is only for callers that care about refcount */
993int ldlm_namespace_get_return(struct ldlm_namespace *ns)
994{
995 return atomic_inc_return(&ns->ns_bref);
996}
997
d7e09d03
PT
998void ldlm_namespace_put(struct ldlm_namespace *ns)
999{
1000 if (atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
1001 wake_up(&ns->ns_waitq);
1002 spin_unlock(&ns->ns_lock);
1003 }
1004}
1005EXPORT_SYMBOL(ldlm_namespace_put);
1006
1007/** Register \a ns in the list of namespaces */
1008void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
1009{
1010 mutex_lock(ldlm_namespace_lock(client));
1011 LASSERT(list_empty(&ns->ns_list_chain));
91a50030
OD
1012 list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
1013 ldlm_namespace_nr_inc(client);
d7e09d03
PT
1014 mutex_unlock(ldlm_namespace_lock(client));
1015}
1016
1017/** Unregister \a ns from the list of namespaces. */
1018void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
1019{
1020 mutex_lock(ldlm_namespace_lock(client));
1021 LASSERT(!list_empty(&ns->ns_list_chain));
1022 /* Some asserts and possibly other parts of the code are still
1023 * using list_empty(&ns->ns_list_chain). This is why it is
1024 * important to use list_del_init() here. */
1025 list_del_init(&ns->ns_list_chain);
91a50030 1026 ldlm_namespace_nr_dec(client);
d7e09d03
PT
1027 mutex_unlock(ldlm_namespace_lock(client));
1028}
1029
1030/** Should be called with ldlm_namespace_lock(client) taken. */
91a50030
OD
1031void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
1032 ldlm_side_t client)
d7e09d03
PT
1033{
1034 LASSERT(!list_empty(&ns->ns_list_chain));
1035 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1036 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
1037}
1038
91a50030
OD
1039/** Should be called with ldlm_namespace_lock(client) taken. */
1040void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
1041 ldlm_side_t client)
1042{
1043 LASSERT(!list_empty(&ns->ns_list_chain));
1044 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1045 list_move_tail(&ns->ns_list_chain,
1046 ldlm_namespace_inactive_list(client));
1047}
1048
d7e09d03
PT
1049/** Should be called with ldlm_namespace_lock(client) taken. */
1050struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
1051{
1052 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1053 LASSERT(!list_empty(ldlm_namespace_list(client)));
1054 return container_of(ldlm_namespace_list(client)->next,
1055 struct ldlm_namespace, ns_list_chain);
1056}
1057
1058/** Create and initialize new resource. */
1059static struct ldlm_resource *ldlm_resource_new(void)
1060{
1061 struct ldlm_resource *res;
1062 int idx;
1063
0be19afa 1064 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, GFP_NOFS);
d7e09d03
PT
1065 if (res == NULL)
1066 return NULL;
1067
1068 INIT_LIST_HEAD(&res->lr_granted);
1069 INIT_LIST_HEAD(&res->lr_converting);
1070 INIT_LIST_HEAD(&res->lr_waiting);
1071
1072 /* Initialize interval trees for each lock mode. */
1073 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1074 res->lr_itree[idx].lit_size = 0;
1075 res->lr_itree[idx].lit_mode = 1 << idx;
1076 res->lr_itree[idx].lit_root = NULL;
1077 }
1078
1079 atomic_set(&res->lr_refcount, 1);
1080 spin_lock_init(&res->lr_lock);
1081 lu_ref_init(&res->lr_reference);
1082
1083 /* The creator of the resource must unlock the mutex after LVB
1084 * initialization. */
1085 mutex_init(&res->lr_lvb_mutex);
1086 mutex_lock(&res->lr_lvb_mutex);
1087
1088 return res;
1089}
1090
1091/**
1092 * Return a reference to resource with given name, creating it if necessary.
1093 * Args: namespace with ns_lock unlocked
1094 * Locks: takes and releases NS hash-lock and res->lr_lock
1095 * Returns: referenced, unlocked ldlm_resource or NULL
1096 */
1097struct ldlm_resource *
1098ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1099 const struct ldlm_res_id *name, ldlm_type_t type, int create)
1100{
1101 struct hlist_node *hnode;
1102 struct ldlm_resource *res;
6ea510c1 1103 struct cfs_hash_bd bd;
d7e09d03 1104 __u64 version;
91a50030 1105 int ns_refcount = 0;
d7e09d03
PT
1106
1107 LASSERT(ns != NULL);
1108 LASSERT(parent == NULL);
1109 LASSERT(ns->ns_rs_hash != NULL);
1110 LASSERT(name->name[0] != 0);
1111
1112 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1113 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1114 if (hnode != NULL) {
1115 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1116 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1117 /* Synchronize with regard to resource creation. */
1118 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1119 mutex_lock(&res->lr_lvb_mutex);
1120 mutex_unlock(&res->lr_lvb_mutex);
1121 }
1122
1123 if (unlikely(res->lr_lvb_len < 0)) {
1124 ldlm_resource_putref(res);
1125 res = NULL;
1126 }
1127 return res;
1128 }
1129
1130 version = cfs_hash_bd_version_get(&bd);
1131 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1132
1133 if (create == 0)
1134 return NULL;
1135
1136 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1137 "type: %d\n", type);
1138 res = ldlm_resource_new();
1139 if (!res)
1140 return NULL;
1141
1142 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1143 res->lr_name = *name;
1144 res->lr_type = type;
1145 res->lr_most_restr = LCK_NL;
1146
1147 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1148 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1149 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1150
1151 if (hnode != NULL) {
1152 /* Someone won the race and already added the resource. */
1153 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1154 /* Clean lu_ref for failed resource. */
1155 lu_ref_fini(&res->lr_reference);
1156 /* We have taken lr_lvb_mutex. Drop it. */
1157 mutex_unlock(&res->lr_lvb_mutex);
ec83e611 1158 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
d7e09d03
PT
1159
1160 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1161 /* Synchronize with regard to resource creation. */
1162 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1163 mutex_lock(&res->lr_lvb_mutex);
1164 mutex_unlock(&res->lr_lvb_mutex);
1165 }
1166
1167 if (unlikely(res->lr_lvb_len < 0)) {
1168 ldlm_resource_putref(res);
1169 res = NULL;
1170 }
1171 return res;
1172 }
1173 /* We won! Let's add the resource. */
1174 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1175 if (cfs_hash_bd_count_get(&bd) == 1)
91a50030 1176 ns_refcount = ldlm_namespace_get_return(ns);
d7e09d03
PT
1177
1178 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1179 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1180 int rc;
1181
1182 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1183 rc = ns->ns_lvbo->lvbo_init(res);
1184 if (rc < 0) {
55f5a824
GKH
1185 CERROR("%s: lvbo_init failed for resource %#llx:%#llx: rc = %d\n",
1186 ns->ns_obd->obd_name, name->name[0],
1187 name->name[1], rc);
62b497d8
JL
1188 kfree(res->lr_lvb_data);
1189 res->lr_lvb_data = NULL;
d7e09d03
PT
1190 res->lr_lvb_len = rc;
1191 mutex_unlock(&res->lr_lvb_mutex);
1192 ldlm_resource_putref(res);
1193 return NULL;
1194 }
1195 }
1196
1197 /* We create resource with locked lr_lvb_mutex. */
1198 mutex_unlock(&res->lr_lvb_mutex);
1199
91a50030
OD
1200 /* Let's see if we happened to be the very first resource in this
1201 * namespace. If so, and this is a client namespace, we need to move
1202 * the namespace into the active namespaces list to be patrolled by
df9fcbeb 1203 * the ldlm_poold. */
91a50030
OD
1204 if (ns_is_client(ns) && ns_refcount == 1) {
1205 mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1206 ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
1207 mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1208 }
1209
d7e09d03
PT
1210 return res;
1211}
1212EXPORT_SYMBOL(ldlm_resource_get);
1213
1214struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1215{
1216 LASSERT(res != NULL);
1217 LASSERT(res != LP_POISON);
1218 atomic_inc(&res->lr_refcount);
1219 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1220 atomic_read(&res->lr_refcount));
1221 return res;
1222}
1223
6ea510c1 1224static void __ldlm_resource_putref_final(struct cfs_hash_bd *bd,
d7e09d03
PT
1225 struct ldlm_resource *res)
1226{
1227 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1228
1229 if (!list_empty(&res->lr_granted)) {
1230 ldlm_resource_dump(D_ERROR, res);
1231 LBUG();
1232 }
1233
1234 if (!list_empty(&res->lr_converting)) {
1235 ldlm_resource_dump(D_ERROR, res);
1236 LBUG();
1237 }
1238
1239 if (!list_empty(&res->lr_waiting)) {
1240 ldlm_resource_dump(D_ERROR, res);
1241 LBUG();
1242 }
1243
1244 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1245 bd, &res->lr_hash);
1246 lu_ref_fini(&res->lr_reference);
1247 if (cfs_hash_bd_count_get(bd) == 0)
1248 ldlm_namespace_put(nsb->nsb_namespace);
1249}
1250
1251/* Returns 1 if the resource was freed, 0 if it remains. */
1252int ldlm_resource_putref(struct ldlm_resource *res)
1253{
1254 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
6ea510c1 1255 struct cfs_hash_bd bd;
d7e09d03
PT
1256
1257 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1258 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1259 res, atomic_read(&res->lr_refcount) - 1);
1260
1261 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1262 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1263 __ldlm_resource_putref_final(&bd, res);
1264 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1265 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1266 ns->ns_lvbo->lvbo_free(res);
ec83e611 1267 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
d7e09d03
PT
1268 return 1;
1269 }
1270 return 0;
1271}
1272EXPORT_SYMBOL(ldlm_resource_putref);
1273
1274/* Returns 1 if the resource was freed, 0 if it remains. */
1275int ldlm_resource_putref_locked(struct ldlm_resource *res)
1276{
1277 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1278
1279 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1280 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1281 res, atomic_read(&res->lr_refcount) - 1);
1282
1283 if (atomic_dec_and_test(&res->lr_refcount)) {
6ea510c1 1284 struct cfs_hash_bd bd;
d7e09d03
PT
1285
1286 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1287 &res->lr_name, &bd);
1288 __ldlm_resource_putref_final(&bd, res);
1289 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1290 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1291 * so we should never be here while calling cfs_hash_del,
1292 * cfs_hash_for_each_nolock is the only case we can get
1293 * here, which is safe to release cfs_hash_bd_lock.
1294 */
1295 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1296 ns->ns_lvbo->lvbo_free(res);
ec83e611 1297 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
d7e09d03
PT
1298
1299 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1300 return 1;
1301 }
1302 return 0;
1303}
1304
1305/**
1306 * Add a lock into a given resource into specified lock list.
1307 */
1308void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1309 struct ldlm_lock *lock)
1310{
1311 check_res_locked(res);
1312
1313 LDLM_DEBUG(lock, "About to add this lock:\n");
1314
f2145eae 1315 if (lock->l_flags & LDLM_FL_DESTROYED) {
d7e09d03
PT
1316 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1317 return;
1318 }
1319
1320 LASSERT(list_empty(&lock->l_res_link));
1321
1322 list_add_tail(&lock->l_res_link, head);
1323}
1324
1325/**
1326 * Insert a lock into resource after specified lock.
1327 *
1328 * Obtain resource description from the lock we are inserting after.
1329 */
1330void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1331 struct ldlm_lock *new)
1332{
1333 struct ldlm_resource *res = original->l_resource;
1334
1335 check_res_locked(res);
1336
1337 ldlm_resource_dump(D_INFO, res);
1338 LDLM_DEBUG(new, "About to insert this lock after %p:\n", original);
1339
f2145eae 1340 if (new->l_flags & LDLM_FL_DESTROYED) {
d7e09d03
PT
1341 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1342 goto out;
1343 }
1344
1345 LASSERT(list_empty(&new->l_res_link));
1346
1347 list_add(&new->l_res_link, &original->l_res_link);
1348 out:;
1349}
1350
1351void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1352{
1353 int type = lock->l_resource->lr_type;
1354
1355 check_res_locked(lock->l_resource);
1356 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1357 ldlm_unlink_lock_skiplist(lock);
1358 else if (type == LDLM_EXTENT)
1359 ldlm_extent_unlink_lock(lock);
1360 list_del_init(&lock->l_res_link);
1361}
1362EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1363
1364void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1365{
1366 desc->lr_type = res->lr_type;
1367 desc->lr_name = res->lr_name;
1368}
1369
1370/**
1371 * Print information about all locks in all namespaces on this node to debug
1372 * log.
1373 */
1374void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1375{
1376 struct list_head *tmp;
1377
1378 if (!((libcfs_debug | D_ERROR) & level))
1379 return;
1380
1381 mutex_lock(ldlm_namespace_lock(client));
1382
1383 list_for_each(tmp, ldlm_namespace_list(client)) {
1384 struct ldlm_namespace *ns;
902f3bb1 1385
d7e09d03
PT
1386 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1387 ldlm_namespace_dump(level, ns);
1388 }
1389
1390 mutex_unlock(ldlm_namespace_lock(client));
1391}
1392EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1393
6da6eabe 1394static int ldlm_res_hash_dump(struct cfs_hash *hs, struct cfs_hash_bd *bd,
d7e09d03
PT
1395 struct hlist_node *hnode, void *arg)
1396{
1397 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1398 int level = (int)(unsigned long)arg;
1399
1400 lock_res(res);
1401 ldlm_resource_dump(level, res);
1402 unlock_res(res);
1403
1404 return 0;
1405}
1406
1407/**
1408 * Print information about all locks in this namespace on this node to debug
1409 * log.
1410 */
1411void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1412{
1413 if (!((libcfs_debug | D_ERROR) & level))
1414 return;
1415
1416 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1417 ldlm_ns_name(ns), atomic_read(&ns->ns_bref),
1418 ns_is_client(ns) ? "client" : "server");
1419
699503bc 1420 if (time_before(cfs_time_current(), ns->ns_next_dump))
d7e09d03
PT
1421 return;
1422
1423 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1424 ldlm_res_hash_dump,
1425 (void *)(unsigned long)level);
1426 spin_lock(&ns->ns_lock);
1427 ns->ns_next_dump = cfs_time_shift(10);
1428 spin_unlock(&ns->ns_lock);
1429}
1430EXPORT_SYMBOL(ldlm_namespace_dump);
1431
1432/**
1433 * Print information about all locks in this resource to debug log.
1434 */
1435void ldlm_resource_dump(int level, struct ldlm_resource *res)
1436{
1437 struct ldlm_lock *lock;
1438 unsigned int granted = 0;
1439
1440 CLASSERT(RES_NAME_SIZE == 4);
1441
1442 if (!((libcfs_debug | D_ERROR) & level))
1443 return;
1444
6d95e048
AD
1445 CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
1446 PLDLMRES(res), res, atomic_read(&res->lr_refcount));
d7e09d03
PT
1447
1448 if (!list_empty(&res->lr_granted)) {
1449 CDEBUG(level, "Granted locks (in reverse order):\n");
1450 list_for_each_entry_reverse(lock, &res->lr_granted,
1451 l_res_link) {
1452 LDLM_DEBUG_LIMIT(level, lock, "###");
1453 if (!(level & D_CANTMASK) &&
1454 ++granted > ldlm_dump_granted_max) {
2d00bd17
JP
1455 CDEBUG(level, "only dump %d granted locks to avoid DDOS.\n",
1456 granted);
d7e09d03
PT
1457 break;
1458 }
1459 }
1460 }
1461 if (!list_empty(&res->lr_converting)) {
1462 CDEBUG(level, "Converting locks:\n");
1463 list_for_each_entry(lock, &res->lr_converting, l_res_link)
1464 LDLM_DEBUG_LIMIT(level, lock, "###");
1465 }
1466 if (!list_empty(&res->lr_waiting)) {
1467 CDEBUG(level, "Waiting locks:\n");
1468 list_for_each_entry(lock, &res->lr_waiting, l_res_link)
1469 LDLM_DEBUG_LIMIT(level, lock, "###");
1470 }
1471}