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