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