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