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