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