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