Whamcloud - gitweb
Introduce .gitignore files.
[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  2008 Sun Microsystems, Inc. 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 <obd_class.h>
50 #include "ldlm_internal.h"
51
52 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
53
54 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
55 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
56
57 struct semaphore ldlm_srv_namespace_lock;
58 CFS_LIST_HEAD(ldlm_srv_namespace_list);
59
60 struct semaphore ldlm_cli_namespace_lock;
61 CFS_LIST_HEAD(ldlm_cli_namespace_list);
62
63 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
64 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
65 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
66
67 #ifdef LPROCFS
68 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
69                              unsigned long count, void *data)
70 {
71         ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
72         ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
73         RETURN(count);
74 }
75
76 int ldlm_proc_setup(void)
77 {
78         int rc;
79         struct lprocfs_vars list[] = {
80                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
81                 { NULL }};
82         ENTRY;
83         LASSERT(ldlm_ns_proc_dir == NULL);
84
85         ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
86                                               proc_lustre_root,
87                                               NULL, NULL);
88         if (IS_ERR(ldlm_type_proc_dir)) {
89                 CERROR("LProcFS failed in ldlm-init\n");
90                 rc = PTR_ERR(ldlm_type_proc_dir);
91                 GOTO(err, rc);
92         }
93
94         ldlm_ns_proc_dir = lprocfs_register("namespaces",
95                                             ldlm_type_proc_dir,
96                                             NULL, NULL);
97         if (IS_ERR(ldlm_ns_proc_dir)) {
98                 CERROR("LProcFS failed in ldlm-init\n");
99                 rc = PTR_ERR(ldlm_ns_proc_dir);
100                 GOTO(err_type, rc);
101         }
102
103         ldlm_svc_proc_dir = lprocfs_register("services",
104                                             ldlm_type_proc_dir,
105                                             NULL, NULL);
106         if (IS_ERR(ldlm_svc_proc_dir)) {
107                 CERROR("LProcFS failed in ldlm-init\n");
108                 rc = PTR_ERR(ldlm_svc_proc_dir);
109                 GOTO(err_ns, rc);
110         }
111
112         rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
113
114         RETURN(0);
115
116 err_ns:
117         lprocfs_remove(&ldlm_ns_proc_dir);
118 err_type:
119         lprocfs_remove(&ldlm_type_proc_dir);
120 err:
121         ldlm_svc_proc_dir = NULL;
122         RETURN(rc);
123 }
124
125 void ldlm_proc_cleanup(void)
126 {
127         if (ldlm_svc_proc_dir)
128                 lprocfs_remove(&ldlm_svc_proc_dir);
129
130         if (ldlm_ns_proc_dir)
131                 lprocfs_remove(&ldlm_ns_proc_dir);
132
133         if (ldlm_type_proc_dir)
134                 lprocfs_remove(&ldlm_type_proc_dir);
135 }
136
137 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
138                                int count, int *eof, void *data)
139 {
140         struct ldlm_namespace *ns = data;
141         __u32 *nr = &ns->ns_max_unused;
142
143         if (ns_connect_lru_resize(ns))
144                 nr = &ns->ns_nr_unused;
145         return lprocfs_rd_uint(page, start, off, count, eof, nr);
146 }
147
148 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
149                                unsigned long count, void *data)
150 {
151         struct ldlm_namespace *ns = data;
152         char dummy[MAX_STRING_SIZE + 1], *end;
153         unsigned long tmp;
154         int lru_resize;
155
156         dummy[MAX_STRING_SIZE] = '\0';
157         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
158                 return -EFAULT;
159
160         if (strncmp(dummy, "clear", 5) == 0) {
161                 CDEBUG(D_DLMTRACE,
162                        "dropping all unused locks from namespace %s\n",
163                        ns->ns_name);
164                 if (ns_connect_lru_resize(ns)) {
165                         int canceled, unused  = ns->ns_nr_unused;
166
167                         /* Try to cancel all @ns_nr_unused locks. */
168                         canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
169                                                    LDLM_CANCEL_PASSED);
170                         if (canceled < unused) {
171                                 CERROR("not all requested locks are canceled, "
172                                        "requested: %d, canceled: %d\n", unused,
173                                        canceled);
174                                 return -EINVAL;
175                         }
176                 } else {
177                         tmp = ns->ns_max_unused;
178                         ns->ns_max_unused = 0;
179                         ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
180                         ns->ns_max_unused = tmp;
181                 }
182                 return count;
183         }
184
185         tmp = simple_strtoul(dummy, &end, 0);
186         if (dummy == end) {
187                 CERROR("invalid value written\n");
188                 return -EINVAL;
189         }
190         lru_resize = (tmp == 0);
191
192         if (ns_connect_lru_resize(ns)) {
193                 if (!lru_resize)
194                         ns->ns_max_unused = (unsigned int)tmp;
195
196                 if (tmp > ns->ns_nr_unused)
197                         tmp = ns->ns_nr_unused;
198                 tmp = ns->ns_nr_unused - tmp;
199
200                 CDEBUG(D_DLMTRACE,
201                        "changing namespace %s unused locks from %u to %u\n",
202                        ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
203                 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
204
205                 if (!lru_resize) {
206                         CDEBUG(D_DLMTRACE,
207                                "disable lru_resize for namespace %s\n",
208                                ns->ns_name);
209                         ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
210                 }
211         } else {
212                 CDEBUG(D_DLMTRACE,
213                        "changing namespace %s max_unused from %u to %u\n",
214                        ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
215                 ns->ns_max_unused = (unsigned int)tmp;
216                 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
217
218                 /* Make sure that originally lru resize was supported before
219                  * turning it on here. */
220                 if (lru_resize &&
221                     (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
222                         CDEBUG(D_DLMTRACE,
223                                "enable lru_resize for namespace %s\n",
224                                ns->ns_name);
225                         ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
226                 }
227         }
228
229         return count;
230 }
231
232 void ldlm_proc_namespace(struct ldlm_namespace *ns)
233 {
234         struct lprocfs_vars lock_vars[2];
235         char lock_name[MAX_STRING_SIZE + 1];
236
237         LASSERT(ns != NULL);
238         LASSERT(ns->ns_name != NULL);
239
240         lock_name[MAX_STRING_SIZE] = '\0';
241
242         memset(lock_vars, 0, sizeof(lock_vars));
243         lock_vars[0].name = lock_name;
244
245         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
246         lock_vars[0].data = &ns->ns_refcount;
247         lock_vars[0].read_fptr = lprocfs_rd_atomic;
248         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
249
250         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
251         lock_vars[0].data = &ns->ns_locks;
252         lock_vars[0].read_fptr = lprocfs_rd_atomic;
253         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
254
255         if (ns_is_client(ns)) {
256                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
257                          ns->ns_name);
258                 lock_vars[0].data = &ns->ns_nr_unused;
259                 lock_vars[0].read_fptr = lprocfs_rd_uint;
260                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
261
262                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
263                          ns->ns_name);
264                 lock_vars[0].data = ns;
265                 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
266                 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
267                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
268
269                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
270                          ns->ns_name);
271                 lock_vars[0].data = &ns->ns_max_age;
272                 lock_vars[0].read_fptr = lprocfs_rd_uint;
273                 lock_vars[0].write_fptr = lprocfs_wr_uint;
274                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
275         } else {
276                 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
277                          ns->ns_name);
278                 lock_vars[0].data = &ns->ns_ctime_age_limit;
279                 lock_vars[0].read_fptr = lprocfs_rd_uint;
280                 lock_vars[0].write_fptr = lprocfs_wr_uint;
281                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
282
283                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
284                          ns->ns_name);
285                 lock_vars[0].data = &ns->ns_timeouts;
286                 lock_vars[0].read_fptr = lprocfs_rd_uint;
287                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
288
289                 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
290                          ns->ns_name);
291                 lock_vars[0].data = &ns->ns_max_nolock_size;
292                 lock_vars[0].read_fptr = lprocfs_rd_uint;
293                 lock_vars[0].write_fptr = lprocfs_wr_uint;
294                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
295
296                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
297                          ns->ns_name);
298                 lock_vars[0].data = &ns->ns_contention_time;
299                 lock_vars[0].read_fptr = lprocfs_rd_uint;
300                 lock_vars[0].write_fptr = lprocfs_wr_uint;
301                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
302
303                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
304                          ns->ns_name);
305                 lock_vars[0].data = &ns->ns_contended_locks;
306                 lock_vars[0].read_fptr = lprocfs_rd_uint;
307                 lock_vars[0].write_fptr = lprocfs_wr_uint;
308                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
309         }
310 }
311 #undef MAX_STRING_SIZE
312 #else
313 #define ldlm_proc_namespace(ns) do {} while (0)
314 #endif /* LPROCFS */
315
316 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
317                                           ldlm_side_t client, ldlm_appetite_t apt)
318 {
319         struct ldlm_namespace *ns = NULL;
320         struct list_head *bucket;
321         int rc, idx, namelen;
322         ENTRY;
323
324         rc = ldlm_get_ref();
325         if (rc) {
326                 CERROR("ldlm_get_ref failed: %d\n", rc);
327                 RETURN(NULL);
328         }
329
330         OBD_ALLOC_PTR(ns);
331         if (!ns)
332                 GOTO(out_ref, NULL);
333
334         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
335         if (!ns->ns_hash)
336                 GOTO(out_ns, NULL);
337
338         ns->ns_appetite = apt;
339
340         LASSERT(obd != NULL);
341         ns->ns_obd = obd;
342
343         namelen = strlen(name);
344         OBD_ALLOC(ns->ns_name, namelen + 1);
345         if (!ns->ns_name)
346                 GOTO(out_hash, NULL);
347
348         strcpy(ns->ns_name, name);
349
350         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
351         CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
352         ns->ns_refcount = 0;
353         ns->ns_client = client;
354         spin_lock_init(&ns->ns_hash_lock);
355         atomic_set(&ns->ns_locks, 0);
356         ns->ns_resources = 0;
357         cfs_waitq_init(&ns->ns_waitq);
358         ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
359         ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
360         ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
361
362         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
363              bucket--)
364                 CFS_INIT_LIST_HEAD(bucket);
365
366         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
367         ns->ns_nr_unused = 0;
368         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
369         ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
370         ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
371         ns->ns_timeouts = 0;
372         spin_lock_init(&ns->ns_unused_lock);
373         ns->ns_orig_connect_flags = 0;
374         ns->ns_connect_flags = 0;
375         ldlm_proc_namespace(ns);
376
377         idx = atomic_read(ldlm_namespace_nr(client));
378         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
379         if (rc) {
380                 CERROR("Can't initialize lock pool, rc %d\n", rc);
381                 GOTO(out_proc, rc);
382         }
383
384         at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
385
386         ldlm_namespace_register(ns, client);
387         RETURN(ns);
388 out_proc:
389         ldlm_namespace_cleanup(ns, 0);
390         OBD_FREE(ns->ns_name, namelen + 1);
391 out_hash:
392         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
393 out_ns:
394         OBD_FREE_PTR(ns);
395 out_ref:
396         ldlm_put_ref();
397         RETURN(NULL);
398 }
399
400 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
401
402 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
403  * This is currently only used for recovery, and we make certain assumptions
404  * as a result--notably, that we shouldn't cancel locks with refs. -phil
405  *
406  * Called with the ns_lock held. */
407 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
408                              int flags)
409 {
410         struct list_head *tmp;
411         int rc = 0, client = ns_is_client(res->lr_namespace);
412         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
413         ENTRY;
414
415
416         do {
417                 struct ldlm_lock *lock = NULL;
418
419                 /* first, we look for non-cleaned-yet lock
420                  * all cleaned locks are marked by CLEANED flag */
421                 lock_res(res);
422                 list_for_each(tmp, q) {
423                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
424                         if (lock->l_flags & LDLM_FL_CLEANED) {
425                                 lock = NULL;
426                                 continue;
427                         }
428                         LDLM_LOCK_GET(lock);
429                         lock->l_flags |= LDLM_FL_CLEANED;
430                         break;
431                 }
432
433                 if (lock == NULL) {
434                         unlock_res(res);
435                         break;
436                 }
437
438                 /* Set CBPENDING so nothing in the cancellation path
439                  * can match this lock */
440                 lock->l_flags |= LDLM_FL_CBPENDING;
441                 lock->l_flags |= LDLM_FL_FAILED;
442                 lock->l_flags |= flags;
443
444                 /* ... without sending a CANCEL message for local_only. */
445                 if (local_only)
446                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
447
448                 if (local_only && (lock->l_readers || lock->l_writers)) {
449                         /* This is a little bit gross, but much better than the
450                          * alternative: pretend that we got a blocking AST from
451                          * the server, so that when the lock is decref'd, it
452                          * will go away ... */
453                         unlock_res(res);
454                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
455                         if (lock->l_completion_ast)
456                                 lock->l_completion_ast(lock, 0, NULL);
457                         LDLM_LOCK_RELEASE(lock);
458                         continue;
459                 }
460
461                 if (client) {
462                         struct lustre_handle lockh;
463
464                         unlock_res(res);
465                         ldlm_lock2handle(lock, &lockh);
466                         rc = ldlm_cli_cancel(&lockh);
467                         if (rc)
468                                 CERROR("ldlm_cli_cancel: %d\n", rc);
469                 } else {
470                         ldlm_resource_unlink_lock(lock);
471                         unlock_res(res);
472                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
473                                    "client node");
474                         ldlm_lock_destroy(lock);
475                 }
476                 LDLM_LOCK_RELEASE(lock);
477         } while (1);
478
479         EXIT;
480 }
481
482 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
483 {
484         struct list_head *tmp;
485         int i;
486
487         if (ns == NULL) {
488                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
489                 return ELDLM_OK;
490         }
491
492         for (i = 0; i < RES_HASH_SIZE; i++) {
493                 spin_lock(&ns->ns_hash_lock);
494                 tmp = ns->ns_hash[i].next;
495                 while (tmp != &(ns->ns_hash[i])) {
496                         struct ldlm_resource *res;
497                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
498                         ldlm_resource_getref(res);
499                         spin_unlock(&ns->ns_hash_lock);
500                         LDLM_RESOURCE_ADDREF(res);
501
502                         cleanup_resource(res, &res->lr_granted, flags);
503                         cleanup_resource(res, &res->lr_converting, flags);
504                         cleanup_resource(res, &res->lr_waiting, flags);
505
506                         spin_lock(&ns->ns_hash_lock);
507                         tmp = tmp->next;
508
509                         /* XXX: former stuff caused issues in case of race
510                          * between ldlm_namespace_cleanup() and lockd() when
511                          * client gets blocking ast when lock gets distracted by
512                          * server. This is 1_4 branch solution, let's see how
513                          * will it behave. */
514                         LDLM_RESOURCE_DELREF(res);
515                         if (!ldlm_resource_putref_locked(res)) {
516                                 CERROR("Namespace %s resource refcount nonzero "
517                                        "(%d) after lock cleanup; forcing cleanup.\n",
518                                        ns->ns_name, atomic_read(&res->lr_refcount));
519                                 CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
520                                        LPU64") (rc: %d)\n", res,
521                                        res->lr_name.name[0], res->lr_name.name[1],
522                                        res->lr_name.name[2], res->lr_name.name[3],
523                                        atomic_read(&res->lr_refcount));
524                         }
525                 }
526                 spin_unlock(&ns->ns_hash_lock);
527         }
528
529         return ELDLM_OK;
530 }
531
532 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
533 {
534         ENTRY;
535
536         /* At shutdown time, don't call the cancellation callback */
537         ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
538
539         if (ns->ns_refcount > 0) {
540                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
541                 int rc;
542                 CDEBUG(D_DLMTRACE,
543                        "dlm namespace %s free waiting on refcount %d\n",
544                        ns->ns_name, ns->ns_refcount);
545 force_wait:
546                 if (force)
547                         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
548
549                 rc = l_wait_event(ns->ns_waitq,
550                                   ns->ns_refcount == 0, &lwi);
551
552                 /* Forced cleanups should be able to reclaim all references,
553                  * so it's safe to wait forever... we can't leak locks... */
554                 if (force && rc == -ETIMEDOUT) {
555                         LCONSOLE_ERROR("Forced cleanup waiting for %s "
556                                        "namespace with %d resources in use, "
557                                        "(rc=%d)\n", ns->ns_name,
558                                        ns->ns_refcount, rc);
559                         GOTO(force_wait, rc);
560                 }
561
562                 if (ns->ns_refcount) {
563                         LCONSOLE_ERROR("Cleanup waiting for %s namespace "
564                                        "with %d resources in use, (rc=%d)\n",
565                                        ns->ns_name,
566                                        ns->ns_refcount, rc);
567                         RETURN(ELDLM_NAMESPACE_EXISTS);
568                 }
569                 CDEBUG(D_DLMTRACE,
570                        "dlm namespace %s free done waiting\n", ns->ns_name);
571         }
572
573         RETURN(ELDLM_OK);
574 }
575
576 /**
577  * Performs various cleanups for passed \a ns to make it drop refc and be ready
578  * for freeing. Waits for refc == 0.
579  *
580  * The following is done:
581  * (0) Unregister \a ns from its list to make inaccessible for potential users
582  * like pools thread and others;
583  * (1) Clear all locks in \a ns.
584  */
585 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
586                                struct obd_import *imp,
587                                int force)
588 {
589         int rc;
590         ENTRY;
591         if (!ns) {
592                 EXIT;
593                 return;
594         }
595
596
597         /*
598          * Can fail with -EINTR when force == 0 in which case try harder.
599          */
600         rc = __ldlm_namespace_free(ns, force);
601         if (rc != ELDLM_OK) {
602                 if (imp) {
603                         ptlrpc_disconnect_import(imp, 0);
604                         ptlrpc_invalidate_import(imp);
605                 }
606
607                 /*
608                  * With all requests dropped and the import inactive
609                  * we are gaurenteed all reference will be dropped.
610                  */
611                 rc = __ldlm_namespace_free(ns, 1);
612                 LASSERT(rc == 0);
613         }
614         EXIT;
615 }
616
617 /**
618  * Performs freeing memory structures related to \a ns. This is only done when
619  * ldlm_namespce_free_prior() successfully removed all resources referencing
620  * \a ns and its refc == 0.
621  */
622 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
623 {
624         ENTRY;
625         if (!ns) {
626                 EXIT;
627                 return;
628         }
629
630
631         /*
632          * Make sure that nobody can find this ns in its list.
633          */
634         ldlm_namespace_unregister(ns, ns->ns_client);
635         /*
636          * Fini pool _before_ parent proc dir is removed. This is important as
637          * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
638          * it after @dir may cause oops.
639          */
640         ldlm_pool_fini(&ns->ns_pool);
641
642 #ifdef LPROCFS
643         {
644                 struct proc_dir_entry *dir;
645                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
646                 if (dir == NULL) {
647                         CERROR("dlm namespace %s has no procfs dir?\n",
648                                ns->ns_name);
649                 } else {
650                         lprocfs_remove(&dir);
651                 }
652         }
653 #endif
654
655         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
656         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
657
658         /*
659          * Namespace \a ns should be not on list in this time, otherwise this
660          * will cause issues realted to using freed \a ns in pools thread.
661          */
662         LASSERT(list_empty(&ns->ns_list_chain));
663         OBD_FREE_PTR(ns);
664         ldlm_put_ref();
665         EXIT;
666 }
667
668
669 /* Cleanup the resource, and free namespace.
670  * bug 12864:
671  * Deadlock issue:
672  * proc1: destroy import
673  *        class_disconnect_export(grab cl_sem) ->
674  *              -> ldlm_namespace_free ->
675  *              -> lprocfs_remove(grab _lprocfs_lock).
676  * proc2: read proc info
677  *        lprocfs_fops_read(grab _lprocfs_lock) ->
678  *              -> osc_rd_active, etc(grab cl_sem).
679  *
680  * So that I have to split the ldlm_namespace_free into two parts - the first
681  * part ldlm_namespace_free_prior is used to cleanup the resource which is
682  * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
683  * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
684  * held.
685  */
686 void ldlm_namespace_free(struct ldlm_namespace *ns,
687                          struct obd_import *imp,
688                          int force)
689 {
690         ldlm_namespace_free_prior(ns, imp, force);
691         ldlm_namespace_free_post(ns);
692 }
693
694
695 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
696 {
697         ns->ns_refcount++;
698 }
699
700 void ldlm_namespace_get(struct ldlm_namespace *ns)
701 {
702         spin_lock(&ns->ns_hash_lock);
703         ldlm_namespace_get_locked(ns);
704         spin_unlock(&ns->ns_hash_lock);
705 }
706
707 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
708 {
709         LASSERT(ns->ns_refcount > 0);
710         ns->ns_refcount--;
711         if (ns->ns_refcount == 0 && wakeup)
712                 wake_up(&ns->ns_waitq);
713 }
714
715 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
716 {
717         spin_lock(&ns->ns_hash_lock);
718         ldlm_namespace_put_locked(ns, wakeup);
719         spin_unlock(&ns->ns_hash_lock);
720 }
721
722 /* Register @ns in the list of namespaces */
723 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
724 {
725         mutex_down(ldlm_namespace_lock(client));
726         LASSERT(list_empty(&ns->ns_list_chain));
727         list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
728         atomic_inc(ldlm_namespace_nr(client));
729         mutex_up(ldlm_namespace_lock(client));
730 }
731
732 /* Unregister @ns from the list of namespaces */
733 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
734 {
735         mutex_down(ldlm_namespace_lock(client));
736         LASSERT(!list_empty(&ns->ns_list_chain));
737         /*
738          * Some asserts and possibly other parts of code still using
739          * list_empty(&ns->ns_list_chain). This is why it is important
740          * to use list_del_init() here.
741          */
742         list_del_init(&ns->ns_list_chain);
743         atomic_dec(ldlm_namespace_nr(client));
744         mutex_up(ldlm_namespace_lock(client));
745 }
746
747 /* Should be called under ldlm_namespace_lock(client) taken */
748 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
749 {
750         LASSERT(!list_empty(&ns->ns_list_chain));
751         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
752         list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
753 }
754
755 /* Should be called under ldlm_namespace_lock(client) taken */
756 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
757 {
758         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
759         LASSERT(!list_empty(ldlm_namespace_list(client)));
760         return container_of(ldlm_namespace_list(client)->next,
761                 struct ldlm_namespace, ns_list_chain);
762 }
763 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
764                           const struct ldlm_res_id *name)
765 {
766         __u32 hash = 0;
767         int i;
768
769         for (i = 0; i < RES_NAME_SIZE; i++)
770                 hash += name->name[i];
771
772         hash += (__u32)((unsigned long)parent >> 4);
773
774         return (hash & RES_HASH_MASK);
775 }
776
777 static struct ldlm_resource *ldlm_resource_new(void)
778 {
779         struct ldlm_resource *res;
780         int idx;
781
782         OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
783         if (res == NULL)
784                 return NULL;
785
786         memset(res, 0, sizeof(*res));
787
788         CFS_INIT_LIST_HEAD(&res->lr_children);
789         CFS_INIT_LIST_HEAD(&res->lr_childof);
790         CFS_INIT_LIST_HEAD(&res->lr_granted);
791         CFS_INIT_LIST_HEAD(&res->lr_converting);
792         CFS_INIT_LIST_HEAD(&res->lr_waiting);
793
794         /* initialize interval trees for each lock mode*/
795         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
796                 res->lr_itree[idx].lit_size = 0;
797                 res->lr_itree[idx].lit_mode = 1 << idx;
798                 res->lr_itree[idx].lit_root = NULL;
799         }
800
801         atomic_set(&res->lr_refcount, 1);
802         spin_lock_init(&res->lr_lock);
803         lu_ref_init(&res->lr_reference);
804
805         /* one who creates the resource must unlock
806          * the semaphore after lvb initialization */
807         init_MUTEX_LOCKED(&res->lr_lvb_sem);
808
809         return res;
810 }
811
812 /* must be called with hash lock held */
813 static struct ldlm_resource *
814 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
815                    __u32 hash)
816 {
817         struct list_head *bucket, *tmp;
818         struct ldlm_resource *res;
819
820         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
821         bucket = ns->ns_hash + hash;
822
823         list_for_each(tmp, bucket) {
824                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
825                 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
826                         return res;
827         }
828
829         return NULL;
830 }
831
832 /* Args: locked namespace
833  * Returns: newly-allocated, referenced, unlocked resource */
834 static struct ldlm_resource *
835 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
836                   const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
837 {
838         struct list_head *bucket;
839         struct ldlm_resource *res, *old_res;
840         ENTRY;
841
842         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
843                  "type: %d\n", type);
844
845         res = ldlm_resource_new();
846         if (!res)
847                 RETURN(NULL);
848
849         res->lr_name = *name;
850         res->lr_namespace = ns;
851         res->lr_type = type;
852         res->lr_most_restr = LCK_NL;
853
854         spin_lock(&ns->ns_hash_lock);
855         old_res = ldlm_resource_find(ns, name, hash);
856         if (old_res) {
857                 /* someone won the race and added the resource before */
858                 ldlm_resource_getref(old_res);
859                 spin_unlock(&ns->ns_hash_lock);
860                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
861                 /* synchronize WRT resource creation */
862                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
863                         down(&old_res->lr_lvb_sem);
864                         up(&old_res->lr_lvb_sem);
865                 }
866                 RETURN(old_res);
867         }
868
869         /* we won! let's add the resource */
870         bucket = ns->ns_hash + hash;
871         list_add(&res->lr_hash, bucket);
872         ns->ns_resources++;
873         ldlm_namespace_get_locked(ns);
874
875         if (parent == NULL) {
876                 list_add(&res->lr_childof, &ns->ns_root_list);
877         } else {
878                 res->lr_parent = parent;
879                 list_add(&res->lr_childof, &parent->lr_children);
880         }
881         spin_unlock(&ns->ns_hash_lock);
882
883         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
884                 int rc;
885
886                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
887                 rc = ns->ns_lvbo->lvbo_init(res);
888                 if (rc)
889                         CERROR("lvbo_init failed for resource "
890                                LPU64": rc %d\n", name->name[0], rc);
891                 /* we create resource with locked lr_lvb_sem */
892                 up(&res->lr_lvb_sem);
893         }
894
895         RETURN(res);
896 }
897
898 /* Args: unlocked namespace
899  * Locks: takes and releases ns->ns_lock and res->lr_lock
900  * Returns: referenced, unlocked ldlm_resource or NULL */
901 struct ldlm_resource *
902 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
903                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
904 {
905         __u32 hash = ldlm_hash_fn(parent, name);
906         struct ldlm_resource *res = NULL;
907         ENTRY;
908
909         LASSERT(ns != NULL);
910         LASSERT(ns->ns_hash != NULL);
911         LASSERT(name->name[0] != 0);
912
913         spin_lock(&ns->ns_hash_lock);
914         res = ldlm_resource_find(ns, name, hash);
915         if (res) {
916                 ldlm_resource_getref(res);
917                 spin_unlock(&ns->ns_hash_lock);
918                 /* synchronize WRT resource creation */
919                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
920                         down(&res->lr_lvb_sem);
921                         up(&res->lr_lvb_sem);
922                 }
923                 RETURN(res);
924         }
925         spin_unlock(&ns->ns_hash_lock);
926
927         if (create == 0)
928                 RETURN(NULL);
929
930         res = ldlm_resource_add(ns, parent, name, hash, type);
931         RETURN(res);
932 }
933
934 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
935 {
936         LASSERT(res != NULL);
937         LASSERT(res != LP_POISON);
938         atomic_inc(&res->lr_refcount);
939         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
940                atomic_read(&res->lr_refcount));
941         return res;
942 }
943
944 void __ldlm_resource_putref_final(struct ldlm_resource *res)
945 {
946         struct ldlm_namespace *ns = res->lr_namespace;
947
948         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
949
950         if (!list_empty(&res->lr_granted)) {
951                 ldlm_resource_dump(D_ERROR, res);
952                 LBUG();
953         }
954
955         if (!list_empty(&res->lr_converting)) {
956                 ldlm_resource_dump(D_ERROR, res);
957                 LBUG();
958         }
959
960         if (!list_empty(&res->lr_waiting)) {
961                 ldlm_resource_dump(D_ERROR, res);
962                 LBUG();
963         }
964
965         if (!list_empty(&res->lr_children)) {
966                 ldlm_resource_dump(D_ERROR, res);
967                 LBUG();
968         }
969
970         /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
971          * lines below when all children are freed. */
972         ldlm_namespace_put_locked(ns, 0);
973         list_del_init(&res->lr_hash);
974         list_del_init(&res->lr_childof);
975         lu_ref_fini(&res->lr_reference);
976
977         ns->ns_resources--;
978         if (ns->ns_resources == 0)
979                 wake_up(&ns->ns_waitq);
980 }
981
982 /* Returns 1 if the resource was freed, 0 if it remains. */
983 int ldlm_resource_putref(struct ldlm_resource *res)
984 {
985         struct ldlm_namespace *ns = res->lr_namespace;
986         int rc = 0;
987         ENTRY;
988
989         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
990                atomic_read(&res->lr_refcount) - 1);
991         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
992                  atomic_read(&res->lr_refcount));
993         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
994                  atomic_read(&res->lr_refcount));
995
996         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
997                 __ldlm_resource_putref_final(res);
998                 spin_unlock(&ns->ns_hash_lock);
999                 if (res->lr_lvb_data)
1000                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1001                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1002                 rc = 1;
1003         }
1004
1005         RETURN(rc);
1006 }
1007
1008 /* Returns 1 if the resource was freed, 0 if it remains. */
1009 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1010 {
1011         int rc = 0;
1012         ENTRY;
1013
1014         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
1015                atomic_read(&res->lr_refcount) - 1);
1016         LASSERT(atomic_read(&res->lr_refcount) > 0);
1017         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
1018
1019         LASSERT(atomic_read(&res->lr_refcount) >= 0);
1020         if (atomic_dec_and_test(&res->lr_refcount)) {
1021                 __ldlm_resource_putref_final(res);
1022                 if (res->lr_lvb_data)
1023                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1024                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1025                 rc = 1;
1026         }
1027
1028         RETURN(rc);
1029 }
1030
1031 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1032                             struct ldlm_lock *lock)
1033 {
1034         check_res_locked(res);
1035
1036         ldlm_resource_dump(D_INFO, res);
1037         CDEBUG(D_OTHER, "About to add this lock:\n");
1038         ldlm_lock_dump(D_OTHER, lock, 0);
1039
1040         if (lock->l_destroyed) {
1041                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1042                 return;
1043         }
1044
1045         LASSERT(list_empty(&lock->l_res_link));
1046
1047         list_add_tail(&lock->l_res_link, head);
1048 }
1049
1050 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1051                                      struct ldlm_lock *new)
1052 {
1053         struct ldlm_resource *res = original->l_resource;
1054
1055         check_res_locked(res);
1056
1057         ldlm_resource_dump(D_INFO, res);
1058         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1059         ldlm_lock_dump(D_OTHER, new, 0);
1060
1061         if (new->l_destroyed) {
1062                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1063                 goto out;
1064         }
1065
1066         LASSERT(list_empty(&new->l_res_link));
1067
1068         list_add(&new->l_res_link, &original->l_res_link);
1069  out:;
1070 }
1071
1072 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1073 {
1074         int type = lock->l_resource->lr_type;
1075
1076         check_res_locked(lock->l_resource);
1077         if (type == LDLM_IBITS || type == LDLM_PLAIN)
1078                 ldlm_unlink_lock_skiplist(lock);
1079         else if (type == LDLM_EXTENT)
1080                 ldlm_extent_unlink_lock(lock);
1081         list_del_init(&lock->l_res_link);
1082 }
1083
1084 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1085 {
1086         desc->lr_type = res->lr_type;
1087         desc->lr_name = res->lr_name;
1088 }
1089
1090 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1091 {
1092         struct list_head *tmp;
1093
1094         if (!((libcfs_debug | D_ERROR) & level))
1095                 return;
1096
1097         mutex_down(ldlm_namespace_lock(client));
1098
1099         list_for_each(tmp, ldlm_namespace_list(client)) {
1100                 struct ldlm_namespace *ns;
1101                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1102                 ldlm_namespace_dump(level, ns);
1103         }
1104
1105         mutex_up(ldlm_namespace_lock(client));
1106 }
1107
1108 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1109 {
1110         struct list_head *tmp;
1111
1112         if (!((libcfs_debug | D_ERROR) & level))
1113                 return;
1114
1115         CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1116                ns->ns_name, ns->ns_refcount,
1117                ns_is_client(ns) ? "client" : "server");
1118
1119         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1120                 return;
1121
1122         spin_lock(&ns->ns_hash_lock);
1123         tmp = ns->ns_root_list.next;
1124         while (tmp != &ns->ns_root_list) {
1125                 struct ldlm_resource *res;
1126                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1127
1128                 ldlm_resource_getref(res);
1129                 spin_unlock(&ns->ns_hash_lock);
1130                 LDLM_RESOURCE_ADDREF(res);
1131
1132                 lock_res(res);
1133                 ldlm_resource_dump(level, res);
1134                 unlock_res(res);
1135
1136                 LDLM_RESOURCE_DELREF(res);
1137                 spin_lock(&ns->ns_hash_lock);
1138                 tmp = tmp->next;
1139                 ldlm_resource_putref_locked(res);
1140         }
1141         ns->ns_next_dump = cfs_time_shift(10);
1142         spin_unlock(&ns->ns_hash_lock);
1143 }
1144
1145 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1146 {
1147         struct list_head *tmp;
1148         int pos;
1149
1150         CLASSERT(RES_NAME_SIZE == 4);
1151
1152         if (!((libcfs_debug | D_ERROR) & level))
1153                 return;
1154
1155         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1156                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1157                res->lr_name.name[2], res->lr_name.name[3],
1158                atomic_read(&res->lr_refcount));
1159
1160         if (!list_empty(&res->lr_granted)) {
1161                 pos = 0;
1162                 CDEBUG(level, "Granted locks:\n");
1163                 list_for_each(tmp, &res->lr_granted) {
1164                         struct ldlm_lock *lock;
1165                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1166                         ldlm_lock_dump(level, lock, ++pos);
1167                 }
1168         }
1169         if (!list_empty(&res->lr_converting)) {
1170                 pos = 0;
1171                 CDEBUG(level, "Converting locks:\n");
1172                 list_for_each(tmp, &res->lr_converting) {
1173                         struct ldlm_lock *lock;
1174                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1175                         ldlm_lock_dump(level, lock, ++pos);
1176                 }
1177         }
1178         if (!list_empty(&res->lr_waiting)) {
1179                 pos = 0;
1180                 CDEBUG(level, "Waiting locks:\n");
1181                 list_for_each(tmp, &res->lr_waiting) {
1182                         struct ldlm_lock *lock;
1183                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1184                         ldlm_lock_dump(level, lock, ++pos);
1185                 }
1186         }
1187 }