Whamcloud - gitweb
0d34e4080d567238261c6407489f7f48e361aab1
[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, void *key, unsigned mask)
380 {
381         struct ldlm_res_id     *id  = key;
382         unsigned                val = 0;
383         unsigned                i;
384
385         for (i = 0; i < RES_NAME_SIZE; i++)
386                 val += id->name[i];
387         return val & mask;
388 }
389
390 static unsigned ldlm_res_hop_fid_hash(cfs_hash_t *hs, void *key, unsigned mask)
391 {
392         struct ldlm_res_id *id = key;
393         struct lu_fid       fid;
394         __u64               hash;
395
396         fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
397         fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_OID_OFF];
398         fid.f_ver = (__u32)id->name[LUSTRE_RES_ID_VER_OFF];
399
400         hash = fid_flatten(&fid);
401         hash = cfs_hash_long(hash, hs->hs_bkt_bits);
402         /* ignore a few low bits */
403         if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0)
404                 hash += id->name[LUSTRE_RES_ID_HSH_OFF] >> 5;
405         else
406                 hash = hash >> 5;
407         hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
408         hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
409
410         return hash & mask;
411 }
412
413 static void *ldlm_res_hop_key(cfs_hlist_node_t *hnode)
414 {
415         struct ldlm_resource   *res;
416
417         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
418         return &res->lr_name;
419 }
420
421 static int ldlm_res_eq(const struct ldlm_res_id *res0,
422                        const struct ldlm_res_id *res1)
423 {
424         return !memcmp(res0, res1, sizeof(*res0));
425 }
426
427 static int ldlm_res_hop_keycmp(void *key, cfs_hlist_node_t *hnode)
428 {
429         struct ldlm_resource   *res;
430
431         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
432         return ldlm_res_eq((const struct ldlm_res_id *)key,
433                            (const struct ldlm_res_id *)&res->lr_name);
434 }
435
436 static void *ldlm_res_hop_object(cfs_hlist_node_t *hnode)
437 {
438         return cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
439 }
440
441 static void ldlm_res_hop_get_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
442 {
443         struct ldlm_resource *res;
444
445         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
446         ldlm_resource_getref(res);
447         LDLM_RESOURCE_ADDREF(res);
448 }
449
450 static void ldlm_res_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
451 {
452         struct ldlm_resource *res;
453
454         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
455         /* cfs_hash_for_each_nolock is the only chance we call it */
456         LDLM_RESOURCE_DELREF(res);
457         ldlm_resource_putref_locked(res);
458 }
459
460 static void ldlm_res_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
461 {
462         struct ldlm_resource *res;
463
464         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
465         LDLM_RESOURCE_DELREF(res);
466         ldlm_resource_putref(res);
467 }
468
469 cfs_hash_ops_t ldlm_ns_hash_ops = {
470         .hs_hash        = ldlm_res_hop_hash,
471         .hs_key         = ldlm_res_hop_key,
472         .hs_keycmp      = ldlm_res_hop_keycmp,
473         .hs_keycpy      = NULL,
474         .hs_object      = ldlm_res_hop_object,
475         .hs_get         = ldlm_res_hop_get_locked,
476         .hs_put_locked  = ldlm_res_hop_put_locked,
477         .hs_put         = ldlm_res_hop_put
478 };
479
480 cfs_hash_ops_t ldlm_ns_fid_hash_ops = {
481         .hs_hash        = ldlm_res_hop_fid_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 typedef struct {
492         ldlm_ns_type_t  nsd_type;
493         /** hash bucket bits */
494         unsigned        nsd_bkt_bits;
495         /** hash bits */
496         unsigned        nsd_all_bits;
497         /** hash operations */
498         cfs_hash_ops_t *nsd_hops;
499 } ldlm_ns_hash_def_t;
500
501 ldlm_ns_hash_def_t ldlm_ns_hash_defs[] =
502 {
503         {
504                 .nsd_type       = LDLM_NS_TYPE_MDC,
505                 .nsd_bkt_bits   = 11,
506                 .nsd_all_bits   = 15,
507                 .nsd_hops       = &ldlm_ns_fid_hash_ops,
508         },
509         {
510                 .nsd_type       = LDLM_NS_TYPE_MDT,
511                 .nsd_bkt_bits   = 14,
512                 .nsd_all_bits   = 21,
513                 .nsd_hops       = &ldlm_ns_fid_hash_ops,
514         },
515         {
516                 .nsd_type       = LDLM_NS_TYPE_OSC,
517                 .nsd_bkt_bits   = 8,
518                 .nsd_all_bits   = 12,
519                 .nsd_hops       = &ldlm_ns_hash_ops,
520         },
521         {
522                 .nsd_type       = LDLM_NS_TYPE_OST,
523                 .nsd_bkt_bits   = 11,
524                 .nsd_all_bits   = 17,
525                 .nsd_hops       = &ldlm_ns_hash_ops,
526         },
527         {
528                 .nsd_type       = LDLM_NS_TYPE_MGC,
529                 .nsd_bkt_bits   = 4,
530                 .nsd_all_bits   = 4,
531                 .nsd_hops       = &ldlm_ns_hash_ops,
532         },
533         {
534                 .nsd_type       = LDLM_NS_TYPE_MGT,
535                 .nsd_bkt_bits   = 4,
536                 .nsd_all_bits   = 4,
537                 .nsd_hops       = &ldlm_ns_hash_ops,
538         },
539         {
540                 .nsd_type       = LDLM_NS_TYPE_UNKNOWN,
541         },
542 };
543
544 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
545                                           ldlm_side_t client,
546                                           ldlm_appetite_t apt,
547                                           ldlm_ns_type_t ns_type)
548 {
549         struct ldlm_namespace *ns = NULL;
550         struct ldlm_ns_bucket *nsb;
551         ldlm_ns_hash_def_t    *nsd;
552         cfs_hash_bd_t          bd;
553         int                    idx;
554         int                    rc;
555         ENTRY;
556
557         LASSERT(obd != NULL);
558
559         rc = ldlm_get_ref();
560         if (rc) {
561                 CERROR("ldlm_get_ref failed: %d\n", rc);
562                 RETURN(NULL);
563         }
564
565         for (idx = 0;;idx++) {
566                 nsd = &ldlm_ns_hash_defs[idx];
567                 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
568                         CERROR("Unknown type %d for ns %s\n", ns_type, name);
569                         GOTO(out_ref, NULL);
570                 }
571
572                 if (nsd->nsd_type == ns_type)
573                         break;
574         }
575
576         OBD_ALLOC_PTR(ns);
577         if (!ns)
578                 GOTO(out_ref, NULL);
579
580         ns->ns_rs_hash = cfs_hash_create(name,
581                                          nsd->nsd_all_bits, nsd->nsd_all_bits,
582                                          nsd->nsd_bkt_bits, sizeof(*nsb),
583                                          CFS_HASH_MIN_THETA,
584                                          CFS_HASH_MAX_THETA,
585                                          nsd->nsd_hops,
586                                          CFS_HASH_DEPTH |
587                                          CFS_HASH_BIGNAME |
588                                          CFS_HASH_SPIN_BKTLOCK |
589                                          CFS_HASH_NO_ITEMREF);
590         if (ns->ns_rs_hash == NULL)
591                 GOTO(out_ns, NULL);
592
593         cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
594                 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
595                 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
596                 nsb->nsb_namespace = ns;
597         }
598
599         ns->ns_obd      = obd;
600         ns->ns_appetite = apt;
601         ns->ns_client   = client;
602
603         CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
604         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
605         cfs_spin_lock_init(&ns->ns_lock);
606         cfs_atomic_set(&ns->ns_bref, 0);
607         cfs_waitq_init(&ns->ns_waitq);
608
609         ns->ns_max_nolock_size    = NS_DEFAULT_MAX_NOLOCK_BYTES;
610         ns->ns_contention_time    = NS_DEFAULT_CONTENTION_SECONDS;
611         ns->ns_contended_locks    = NS_DEFAULT_CONTENDED_LOCKS;
612
613         ns->ns_nr_unused          = 0;
614         ns->ns_max_unused         = LDLM_DEFAULT_LRU_SIZE;
615         ns->ns_max_age            = LDLM_DEFAULT_MAX_ALIVE;
616         ns->ns_ctime_age_limit    = LDLM_CTIME_AGE_LIMIT;
617         ns->ns_timeouts           = 0;
618         ns->ns_orig_connect_flags = 0;
619         ns->ns_connect_flags      = 0;
620         rc = ldlm_namespace_proc_register(ns);
621         if (rc != 0) {
622                 CERROR("Can't initialize ns proc, rc %d\n", rc);
623                 GOTO(out_hash, rc);
624         }
625
626         idx = cfs_atomic_read(ldlm_namespace_nr(client));
627         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
628         if (rc) {
629                 CERROR("Can't initialize lock pool, rc %d\n", rc);
630                 GOTO(out_proc, rc);
631         }
632
633         ldlm_namespace_register(ns, client);
634         RETURN(ns);
635 out_proc:
636         ldlm_namespace_proc_unregister(ns);
637         ldlm_namespace_cleanup(ns, 0);
638 out_hash:
639         cfs_hash_putref(ns->ns_rs_hash);
640 out_ns:
641         OBD_FREE_PTR(ns);
642 out_ref:
643         ldlm_put_ref();
644         RETURN(NULL);
645 }
646
647 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
648
649 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
650  * This is currently only used for recovery, and we make certain assumptions
651  * as a result--notably, that we shouldn't cancel locks with refs. -phil */
652 static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
653                              int flags)
654 {
655         cfs_list_t *tmp;
656         int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
657         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
658
659         do {
660                 struct ldlm_lock *lock = NULL;
661
662                 /* first, we look for non-cleaned-yet lock
663                  * all cleaned locks are marked by CLEANED flag */
664                 lock_res(res);
665                 cfs_list_for_each(tmp, q) {
666                         lock = cfs_list_entry(tmp, struct ldlm_lock,
667                                               l_res_link);
668                         if (lock->l_flags & LDLM_FL_CLEANED) {
669                                 lock = NULL;
670                                 continue;
671                         }
672                         LDLM_LOCK_GET(lock);
673                         lock->l_flags |= LDLM_FL_CLEANED;
674                         break;
675                 }
676
677                 if (lock == NULL) {
678                         unlock_res(res);
679                         break;
680                 }
681
682                 /* Set CBPENDING so nothing in the cancellation path
683                  * can match this lock */
684                 lock->l_flags |= LDLM_FL_CBPENDING;
685                 lock->l_flags |= LDLM_FL_FAILED;
686                 lock->l_flags |= flags;
687
688                 /* ... without sending a CANCEL message for local_only. */
689                 if (local_only)
690                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
691
692                 if (local_only && (lock->l_readers || lock->l_writers)) {
693                         /* This is a little bit gross, but much better than the
694                          * alternative: pretend that we got a blocking AST from
695                          * the server, so that when the lock is decref'd, it
696                          * will go away ... */
697                         unlock_res(res);
698                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
699                         if (lock->l_completion_ast)
700                                 lock->l_completion_ast(lock, 0, NULL);
701                         LDLM_LOCK_RELEASE(lock);
702                         continue;
703                 }
704
705                 if (client) {
706                         struct lustre_handle lockh;
707
708                         unlock_res(res);
709                         ldlm_lock2handle(lock, &lockh);
710                         rc = ldlm_cli_cancel(&lockh);
711                         if (rc)
712                                 CERROR("ldlm_cli_cancel: %d\n", rc);
713                 } else {
714                         ldlm_resource_unlink_lock(lock);
715                         unlock_res(res);
716                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
717                                    "client node");
718                         ldlm_lock_destroy(lock);
719                 }
720                 LDLM_LOCK_RELEASE(lock);
721         } while (1);
722 }
723
724 static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
725                                cfs_hlist_node_t *hnode, void *arg)
726 {
727         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
728         int    flags = (int)(unsigned long)arg;
729
730         cleanup_resource(res, &res->lr_granted, flags);
731         cleanup_resource(res, &res->lr_converting, flags);
732         cleanup_resource(res, &res->lr_waiting, flags);
733
734         return 0;
735 }
736
737 static int ldlm_resource_complain(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
742         CERROR("Namespace %s resource refcount nonzero "
743                "(%d) after lock cleanup; forcing "
744                "cleanup.\n",
745                ldlm_ns_name(ldlm_res_to_ns(res)),
746                cfs_atomic_read(&res->lr_refcount) - 1);
747
748         CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
749                LPU64") (rc: %d)\n", res,
750                res->lr_name.name[0], res->lr_name.name[1],
751                res->lr_name.name[2], res->lr_name.name[3],
752                cfs_atomic_read(&res->lr_refcount) - 1);
753         return 0;
754 }
755
756 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
757 {
758         if (ns == NULL) {
759                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
760                 return ELDLM_OK;
761         }
762
763         cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean,
764                                  (void *)(unsigned long)flags);
765         cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
766         return ELDLM_OK;
767 }
768
769 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
770 {
771         ENTRY;
772
773         /* At shutdown time, don't call the cancellation callback */
774         ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
775
776         if (cfs_atomic_read(&ns->ns_bref) > 0) {
777                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
778                 int rc;
779                 CDEBUG(D_DLMTRACE,
780                        "dlm namespace %s free waiting on refcount %d\n",
781                        ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref));
782 force_wait:
783                 if (force)
784                         lwi = LWI_TIMEOUT(obd_timeout * CFS_HZ / 4, NULL, NULL);
785
786                 rc = l_wait_event(ns->ns_waitq,
787                                   cfs_atomic_read(&ns->ns_bref) == 0, &lwi);
788
789                 /* Forced cleanups should be able to reclaim all references,
790                  * so it's safe to wait forever... we can't leak locks... */
791                 if (force && rc == -ETIMEDOUT) {
792                         LCONSOLE_ERROR("Forced cleanup waiting for %s "
793                                        "namespace with %d resources in use, "
794                                        "(rc=%d)\n", ldlm_ns_name(ns),
795                                        cfs_atomic_read(&ns->ns_bref), rc);
796                         GOTO(force_wait, rc);
797                 }
798
799                 if (cfs_atomic_read(&ns->ns_bref)) {
800                         LCONSOLE_ERROR("Cleanup waiting for %s namespace "
801                                        "with %d resources in use, (rc=%d)\n",
802                                        ldlm_ns_name(ns),
803                                        cfs_atomic_read(&ns->ns_bref), rc);
804                         RETURN(ELDLM_NAMESPACE_EXISTS);
805                 }
806                 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
807                        ldlm_ns_name(ns));
808         }
809
810         RETURN(ELDLM_OK);
811 }
812
813 /**
814  * Performs various cleanups for passed \a ns to make it drop refc and be ready
815  * for freeing. Waits for refc == 0.
816  *
817  * The following is done:
818  * (0) Unregister \a ns from its list to make inaccessible for potential users
819  * like pools thread and others;
820  * (1) Clear all locks in \a ns.
821  */
822 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
823                                struct obd_import *imp,
824                                int force)
825 {
826         int rc;
827         ENTRY;
828         if (!ns) {
829                 EXIT;
830                 return;
831         }
832
833
834         /*
835          * Can fail with -EINTR when force == 0 in which case try harder.
836          */
837         rc = __ldlm_namespace_free(ns, force);
838         if (rc != ELDLM_OK) {
839                 if (imp) {
840                         ptlrpc_disconnect_import(imp, 0);
841                         ptlrpc_invalidate_import(imp);
842                 }
843
844                 /*
845                  * With all requests dropped and the import inactive
846                  * we are gaurenteed all reference will be dropped.
847                  */
848                 rc = __ldlm_namespace_free(ns, 1);
849                 LASSERT(rc == 0);
850         }
851         EXIT;
852 }
853
854 /**
855  * Performs freeing memory structures related to \a ns. This is only done when
856  * ldlm_namespce_free_prior() successfully removed all resources referencing
857  * \a ns and its refc == 0.
858  */
859 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
860 {
861         ENTRY;
862         if (!ns) {
863                 EXIT;
864                 return;
865         }
866
867
868         /*
869          * Make sure that nobody can find this ns in its list.
870          */
871         ldlm_namespace_unregister(ns, ns->ns_client);
872         /*
873          * Fini pool _before_ parent proc dir is removed. This is important as
874          * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
875          * it after @dir may cause oops.
876          */
877         ldlm_pool_fini(&ns->ns_pool);
878
879         ldlm_namespace_proc_unregister(ns);
880         cfs_hash_putref(ns->ns_rs_hash);
881         /*
882          * Namespace \a ns should be not on list in this time, otherwise this
883          * will cause issues realted to using freed \a ns in pools thread.
884          */
885         LASSERT(cfs_list_empty(&ns->ns_list_chain));
886         OBD_FREE_PTR(ns);
887         ldlm_put_ref();
888         EXIT;
889 }
890
891
892 /* Cleanup the resource, and free namespace.
893  * bug 12864:
894  * Deadlock issue:
895  * proc1: destroy import
896  *        class_disconnect_export(grab cl_sem) ->
897  *              -> ldlm_namespace_free ->
898  *              -> lprocfs_remove(grab _lprocfs_lock).
899  * proc2: read proc info
900  *        lprocfs_fops_read(grab _lprocfs_lock) ->
901  *              -> osc_rd_active, etc(grab cl_sem).
902  *
903  * So that I have to split the ldlm_namespace_free into two parts - the first
904  * part ldlm_namespace_free_prior is used to cleanup the resource which is
905  * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
906  * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
907  * held.
908  */
909 void ldlm_namespace_free(struct ldlm_namespace *ns,
910                          struct obd_import *imp,
911                          int force)
912 {
913         ldlm_namespace_free_prior(ns, imp, force);
914         ldlm_namespace_free_post(ns);
915 }
916
917 void ldlm_namespace_get(struct ldlm_namespace *ns)
918 {
919         cfs_atomic_inc(&ns->ns_bref);
920 }
921
922 void ldlm_namespace_put(struct ldlm_namespace *ns)
923 {
924         if (cfs_atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
925                 cfs_waitq_signal(&ns->ns_waitq);
926                 cfs_spin_unlock(&ns->ns_lock);
927         }
928 }
929
930 /* Register @ns in the list of namespaces */
931 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
932 {
933         cfs_mutex_down(ldlm_namespace_lock(client));
934         LASSERT(cfs_list_empty(&ns->ns_list_chain));
935         cfs_list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
936         cfs_atomic_inc(ldlm_namespace_nr(client));
937         cfs_mutex_up(ldlm_namespace_lock(client));
938 }
939
940 /* Unregister @ns from the list of namespaces */
941 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
942 {
943         cfs_mutex_down(ldlm_namespace_lock(client));
944         LASSERT(!cfs_list_empty(&ns->ns_list_chain));
945         /*
946          * Some asserts and possibly other parts of code still using
947          * list_empty(&ns->ns_list_chain). This is why it is important
948          * to use list_del_init() here.
949          */
950         cfs_list_del_init(&ns->ns_list_chain);
951         cfs_atomic_dec(ldlm_namespace_nr(client));
952         cfs_mutex_up(ldlm_namespace_lock(client));
953 }
954
955 /* Should be called under ldlm_namespace_lock(client) taken */
956 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
957 {
958         LASSERT(!cfs_list_empty(&ns->ns_list_chain));
959         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
960         cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
961 }
962
963 /* Should be called under ldlm_namespace_lock(client) taken */
964 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
965 {
966         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
967         LASSERT(!cfs_list_empty(ldlm_namespace_list(client)));
968         return container_of(ldlm_namespace_list(client)->next,
969                 struct ldlm_namespace, ns_list_chain);
970 }
971
972 static struct ldlm_resource *ldlm_resource_new(void)
973 {
974         struct ldlm_resource *res;
975         int idx;
976
977         OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
978         if (res == NULL)
979                 return NULL;
980
981         CFS_INIT_LIST_HEAD(&res->lr_granted);
982         CFS_INIT_LIST_HEAD(&res->lr_converting);
983         CFS_INIT_LIST_HEAD(&res->lr_waiting);
984
985         /* initialize interval trees for each lock mode*/
986         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
987                 res->lr_itree[idx].lit_size = 0;
988                 res->lr_itree[idx].lit_mode = 1 << idx;
989                 res->lr_itree[idx].lit_root = NULL;
990         }
991
992         cfs_atomic_set(&res->lr_refcount, 1);
993         cfs_spin_lock_init(&res->lr_lock);
994         lu_ref_init(&res->lr_reference);
995
996         /* one who creates the resource must unlock
997          * the semaphore after lvb initialization */
998         cfs_init_mutex_locked(&res->lr_lvb_sem);
999
1000         return res;
1001 }
1002
1003 /* Args: unlocked namespace
1004  *  * Locks: takes and releases NS hash-lock and res->lr_lock
1005  *   * Returns: referenced, unlocked ldlm_resource or NULL */
1006 struct ldlm_resource *
1007 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1008                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
1009 {
1010         cfs_hlist_node_t     *hnode;
1011         struct ldlm_resource *res;
1012         cfs_hash_bd_t         bd;
1013         __u64                 version;
1014
1015         LASSERT(ns != NULL);
1016         LASSERT(parent == NULL);
1017         LASSERT(ns->ns_rs_hash != NULL);
1018         LASSERT(name->name[0] != 0);
1019
1020         cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1021         hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1022         if (hnode != NULL) {
1023                 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1024                 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1025                 /* synchronize WRT resource creation */
1026                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1027                         cfs_down(&res->lr_lvb_sem);
1028                         cfs_up(&res->lr_lvb_sem);
1029                 }
1030                 return res;
1031         }
1032
1033         version = cfs_hash_bd_version_get(&bd);
1034         cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1035
1036         if (create == 0)
1037                 return NULL;
1038
1039         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1040                  "type: %d\n", type);
1041         res = ldlm_resource_new();
1042         if (!res)
1043                 return NULL;
1044
1045         res->lr_ns_bucket  = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1046         res->lr_name       = *name;
1047         res->lr_type       = type;
1048         res->lr_most_restr = LCK_NL;
1049
1050         cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1051         hnode = (version == cfs_hash_bd_version_get(&bd)) ?  NULL :
1052                 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1053
1054         if (hnode != NULL) {
1055                 /* someone won the race and added the resource before */
1056                 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1057                 /* clean lu_ref for failed resource */
1058                 lu_ref_fini(&res->lr_reference);
1059                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1060
1061                 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1062                 /* synchronize WRT resource creation */
1063                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1064                         cfs_down(&res->lr_lvb_sem);
1065                         cfs_up(&res->lr_lvb_sem);
1066                 }
1067                 return res;
1068         }
1069         /* we won! let's add the resource */
1070         cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1071         if (cfs_hash_bd_count_get(&bd) == 1)
1072                 ldlm_namespace_get(ns);
1073
1074         cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1075         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1076                 int rc;
1077
1078                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1079                 rc = ns->ns_lvbo->lvbo_init(res);
1080                 if (rc)
1081                         CERROR("lvbo_init failed for resource "
1082                                LPU64": rc %d\n", name->name[0], rc);
1083                 /* we create resource with locked lr_lvb_sem */
1084                 cfs_up(&res->lr_lvb_sem);
1085         }
1086
1087         return res;
1088 }
1089
1090 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1091 {
1092         LASSERT(res != NULL);
1093         LASSERT(res != LP_POISON);
1094         cfs_atomic_inc(&res->lr_refcount);
1095         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1096                cfs_atomic_read(&res->lr_refcount));
1097         return res;
1098 }
1099
1100 static void __ldlm_resource_putref_final(cfs_hash_bd_t *bd,
1101                                          struct ldlm_resource *res)
1102 {
1103         struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1104
1105         if (!cfs_list_empty(&res->lr_granted)) {
1106                 ldlm_resource_dump(D_ERROR, res);
1107                 LBUG();
1108         }
1109
1110         if (!cfs_list_empty(&res->lr_converting)) {
1111                 ldlm_resource_dump(D_ERROR, res);
1112                 LBUG();
1113         }
1114
1115         if (!cfs_list_empty(&res->lr_waiting)) {
1116                 ldlm_resource_dump(D_ERROR, res);
1117                 LBUG();
1118         }
1119
1120         cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1121                                bd, &res->lr_hash);
1122         lu_ref_fini(&res->lr_reference);
1123         if (cfs_hash_bd_count_get(bd) == 0)
1124                 ldlm_namespace_put(nsb->nsb_namespace);
1125 }
1126
1127 /* Returns 1 if the resource was freed, 0 if it remains. */
1128 int ldlm_resource_putref(struct ldlm_resource *res)
1129 {
1130         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1131         int ref = cfs_atomic_read(&res->lr_refcount);
1132         cfs_hash_bd_t   bd;
1133
1134         CDEBUG(D_INFO, "putref res: %p count: %d\n", res, ref - 1);
1135         LASSERTF(ref > 0 && ref < LI_POISON, "%d", ref);
1136         cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1137         if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1138                 __ldlm_resource_putref_final(&bd, res);
1139                 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1140                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1141                         ns->ns_lvbo->lvbo_free(res);
1142                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1143                 return 1;
1144         }
1145         return 0;
1146 }
1147
1148 /* Returns 1 if the resource was freed, 0 if it remains. */
1149 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1150 {
1151         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1152         int ref = cfs_atomic_read(&res->lr_refcount);
1153
1154         CDEBUG(D_INFO, "putref res: %p count: %d\n", res, ref - 1);
1155         LASSERTF(ref > 0 && ref < LI_POISON, "%d", ref);
1156         if (cfs_atomic_dec_and_test(&res->lr_refcount)) {
1157                 cfs_hash_bd_t bd;
1158
1159                 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1160                                 &res->lr_name, &bd);
1161                 __ldlm_resource_putref_final(&bd, res);
1162                 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1163                 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1164                  * so we should never be here while calling cfs_hash_del,
1165                  * cfs_hash_for_each_nolock is the only case we can get
1166                  * here, which is safe to release cfs_hash_bd_lock.
1167                  */
1168                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1169                         ns->ns_lvbo->lvbo_free(res);
1170                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1171
1172                 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1173                 return 1;
1174         }
1175         return 0;
1176 }
1177
1178 void ldlm_resource_add_lock(struct ldlm_resource *res, cfs_list_t *head,
1179                             struct ldlm_lock *lock)
1180 {
1181         check_res_locked(res);
1182
1183         ldlm_resource_dump(D_INFO, res);
1184         CDEBUG(D_OTHER, "About to add this lock:\n");
1185         ldlm_lock_dump(D_OTHER, lock, 0);
1186
1187         if (lock->l_destroyed) {
1188                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1189                 return;
1190         }
1191
1192         LASSERT(cfs_list_empty(&lock->l_res_link));
1193
1194         cfs_list_add_tail(&lock->l_res_link, head);
1195 }
1196
1197 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1198                                      struct ldlm_lock *new)
1199 {
1200         struct ldlm_resource *res = original->l_resource;
1201
1202         check_res_locked(res);
1203
1204         ldlm_resource_dump(D_INFO, res);
1205         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1206         ldlm_lock_dump(D_OTHER, new, 0);
1207
1208         if (new->l_destroyed) {
1209                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1210                 goto out;
1211         }
1212
1213         LASSERT(cfs_list_empty(&new->l_res_link));
1214
1215         cfs_list_add(&new->l_res_link, &original->l_res_link);
1216  out:;
1217 }
1218
1219 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1220 {
1221         int type = lock->l_resource->lr_type;
1222
1223         check_res_locked(lock->l_resource);
1224         if (type == LDLM_IBITS || type == LDLM_PLAIN)
1225                 ldlm_unlink_lock_skiplist(lock);
1226         else if (type == LDLM_EXTENT)
1227                 ldlm_extent_unlink_lock(lock);
1228         cfs_list_del_init(&lock->l_res_link);
1229 }
1230
1231 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1232 {
1233         desc->lr_type = res->lr_type;
1234         desc->lr_name = res->lr_name;
1235 }
1236
1237 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1238 {
1239         cfs_list_t *tmp;
1240
1241         if (!((libcfs_debug | D_ERROR) & level))
1242                 return;
1243
1244         cfs_mutex_down(ldlm_namespace_lock(client));
1245
1246         cfs_list_for_each(tmp, ldlm_namespace_list(client)) {
1247                 struct ldlm_namespace *ns;
1248                 ns = cfs_list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1249                 ldlm_namespace_dump(level, ns);
1250         }
1251
1252         cfs_mutex_up(ldlm_namespace_lock(client));
1253 }
1254
1255 static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1256                               cfs_hlist_node_t *hnode, void *arg)
1257 {
1258         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1259         int    level = (int)(unsigned long)arg;
1260
1261         lock_res(res);
1262         ldlm_resource_dump(level, res);
1263         unlock_res(res);
1264
1265         return 0;
1266 }
1267
1268 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1269 {
1270         if (!((libcfs_debug | D_ERROR) & level))
1271                 return;
1272
1273         CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1274                ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref),
1275                ns_is_client(ns) ? "client" : "server");
1276
1277         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1278                 return;
1279
1280         cfs_hash_for_each_nolock(ns->ns_rs_hash,
1281                                  ldlm_res_hash_dump,
1282                                  (void *)(unsigned long)level);
1283         cfs_spin_lock(&ns->ns_lock);
1284         ns->ns_next_dump = cfs_time_shift(10);
1285         cfs_spin_unlock(&ns->ns_lock);
1286 }
1287
1288 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1289 {
1290         cfs_list_t *tmp;
1291         int pos;
1292
1293         CLASSERT(RES_NAME_SIZE == 4);
1294
1295         if (!((libcfs_debug | D_ERROR) & level))
1296                 return;
1297
1298         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1299                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1300                res->lr_name.name[2], res->lr_name.name[3],
1301                cfs_atomic_read(&res->lr_refcount));
1302
1303         if (!cfs_list_empty(&res->lr_granted)) {
1304                 pos = 0;
1305                 CDEBUG(level, "Granted locks:\n");
1306                 cfs_list_for_each(tmp, &res->lr_granted) {
1307                         struct ldlm_lock *lock;
1308                         lock = cfs_list_entry(tmp, struct ldlm_lock,
1309                                               l_res_link);
1310                         ldlm_lock_dump(level, lock, ++pos);
1311                 }
1312         }
1313         if (!cfs_list_empty(&res->lr_converting)) {
1314                 pos = 0;
1315                 CDEBUG(level, "Converting locks:\n");
1316                 cfs_list_for_each(tmp, &res->lr_converting) {
1317                         struct ldlm_lock *lock;
1318                         lock = cfs_list_entry(tmp, struct ldlm_lock,
1319                                               l_res_link);
1320                         ldlm_lock_dump(level, lock, ++pos);
1321                 }
1322         }
1323         if (!cfs_list_empty(&res->lr_waiting)) {
1324                 pos = 0;
1325                 CDEBUG(level, "Waiting locks:\n");
1326                 cfs_list_for_each(tmp, &res->lr_waiting) {
1327                         struct ldlm_lock *lock;
1328                         lock = cfs_list_entry(tmp, struct ldlm_lock,
1329                                               l_res_link);
1330                         ldlm_lock_dump(level, lock, ++pos);
1331                 }
1332         }
1333 }