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