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