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