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