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