Whamcloud - gitweb
* Landing fix on HEAD, originally committed to b_devel
[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 Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25 #ifdef __KERNEL__
26 # include <linux/lustre_dlm.h>
27 #else
28 # include <liblustre.h>
29 #endif
30
31 #include <linux/obd_class.h>
32 #include "ldlm_internal.h"
33
34 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
35
36 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
37 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
38 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
39
40 int ldlm_proc_setup(struct obd_device *obd)
41 {
42         int rc;
43         ENTRY;
44         LASSERT(ldlm_ns_proc_dir == NULL);
45         LASSERT(obd != NULL);
46         rc = lprocfs_obd_attach(obd, 0);
47         if (rc) {
48                 CERROR("LProcFS failed in ldlm-init\n");
49                 RETURN(rc);
50         }
51         ldlm_ns_proc_dir = obd->obd_proc_entry;
52         RETURN(0);
53 }
54
55 void ldlm_proc_cleanup(struct obd_device *obd)
56 {
57         if (ldlm_ns_proc_dir) {
58                 lprocfs_obd_detach(obd);
59                 ldlm_ns_proc_dir = NULL;
60         }
61 }
62
63 #ifdef __KERNEL__
64 static int lprocfs_uint_rd(char *page, char **start, off_t off,
65                            int count, int *eof, void *data)
66 {
67         unsigned int *temp = (unsigned int *)data;
68         return snprintf(page, count, "%u\n", *temp);
69 }
70
71 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
72                                  int count, int *eof, void *data)
73 {
74         struct ldlm_namespace *ns = data;
75         return lprocfs_uint_rd(page, start, off, count, eof,
76                                &ns->ns_max_unused);
77 }
78
79 #define MAX_STRING_SIZE 128
80 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
81                                   unsigned long count, void *data)
82 {
83         struct ldlm_namespace *ns = data;
84         char dummy[MAX_STRING_SIZE + 1];
85         unsigned long tmp;
86
87         dummy[MAX_STRING_SIZE] = '\0';
88         copy_from_user(dummy, buffer, MAX_STRING_SIZE);
89
90         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
91                 CDEBUG(D_DLMTRACE,
92                        "dropping all unused locks from namespace %s\n",
93                        ns->ns_name);
94                 tmp = ns->ns_max_unused;
95                 ns->ns_max_unused = 0;
96                 ldlm_cancel_lru(ns);
97                 ns->ns_max_unused = tmp;
98                 return count;
99         }
100
101         tmp = simple_strtoul(dummy, NULL, 0);
102         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
103                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
104         ns->ns_max_unused = (unsigned int)tmp;
105
106         ldlm_cancel_lru(ns);
107
108         return count;
109 }
110
111 void ldlm_proc_namespace(struct ldlm_namespace *ns)
112 {
113         struct lprocfs_vars lock_vars[2];
114         char lock_name[MAX_STRING_SIZE + 1];
115
116         LASSERT(ns != NULL);
117         LASSERT(ns->ns_name != NULL);
118
119         lock_name[MAX_STRING_SIZE] = '\0';
120
121         memset(lock_vars, 0, sizeof(lock_vars));
122         lock_vars[0].read_fptr = lprocfs_rd_u64;
123         lock_vars[0].name = lock_name;
124
125         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
126         lock_vars[0].data = &ns->ns_resources;
127         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
128
129         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
130         lock_vars[0].data = &ns->ns_locks;
131         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
132
133         if (ns->ns_client) {
134                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
135                          ns->ns_name);
136                 lock_vars[0].data = &ns->ns_nr_unused;
137                 lock_vars[0].read_fptr = lprocfs_uint_rd;
138                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
139
140                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
141                          ns->ns_name);
142                 lock_vars[0].data = ns;
143                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
144                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
145                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
146         }
147 }
148 #endif
149 #undef MAX_STRING_SIZE
150
151 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
152 {
153         struct ldlm_namespace *ns = NULL;
154         struct list_head *bucket;
155         ENTRY;
156
157         OBD_ALLOC(ns, sizeof(*ns));
158         if (!ns)
159                 RETURN(NULL);
160
161         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
162         if (!ns->ns_hash)
163                 GOTO(out_ns, NULL);
164
165         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
166         if (!ns->ns_name)
167                 GOTO(out_hash, NULL);
168
169         strcpy(ns->ns_name, name);
170
171         INIT_LIST_HEAD(&ns->ns_root_list);
172         l_lock_init(&ns->ns_lock);
173         ns->ns_refcount = 0;
174         ns->ns_client = client;
175         spin_lock_init(&ns->ns_counter_lock);
176         ns->ns_locks = 0;
177         ns->ns_resources = 0;
178
179         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
180              bucket--)
181                 INIT_LIST_HEAD(bucket);
182
183         INIT_LIST_HEAD(&ns->ns_unused_list);
184         ns->ns_nr_unused = 0;
185         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
186
187         spin_lock(&ldlm_namespace_lock);
188         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
189         spin_unlock(&ldlm_namespace_lock);
190 #ifdef __KERNEL__
191         ldlm_proc_namespace(ns);
192 #endif
193         RETURN(ns);
194
195 out_hash:
196         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
197         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
198 out_ns:
199         OBD_FREE(ns, sizeof(*ns));
200         return NULL;
201 }
202
203 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
204
205 /* If 'local_only' is true, don't try to tell the server, just cleanup.
206  * This is currently only used for recovery, and we make certain assumptions
207  * as a result--notably, that we shouldn't cancel locks with refs. -phil
208  *
209  * Called with the ns_lock held. */
210 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
211                              int local_only)
212 {
213         struct list_head *tmp, *pos;
214         int rc = 0, client = res->lr_namespace->ns_client;
215         ENTRY;
216
217         list_for_each_safe(tmp, pos, q) {
218                 struct ldlm_lock *lock;
219                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
220                 LDLM_LOCK_GET(lock);
221
222                 if (local_only && (lock->l_readers || lock->l_writers)) {
223                         /* This is a little bit gross, but much better than the
224                          * alternative: pretend that we got a blocking AST from
225                          * the server, so that when the lock is decref'd, it
226                          * will go away ... */
227                         lock->l_flags |= LDLM_FL_CBPENDING;
228                         /* ... without sending a CANCEL message. */
229                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
230                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
231                         /* ... and without calling the cancellation callback */
232                         lock->l_flags |= LDLM_FL_CANCEL;
233                         LDLM_LOCK_PUT(lock);
234                         continue;
235                 }
236
237                 /* At shutdown time, don't call the cancellation callback */
238                 lock->l_flags |= LDLM_FL_CANCEL;
239
240                 if (client) {
241                         struct lustre_handle lockh;
242                         ldlm_lock2handle(lock, &lockh);
243                         if (!local_only) {
244                                 rc = ldlm_cli_cancel(&lockh);
245                                 if (rc)
246                                         CERROR("ldlm_cli_cancel: %d\n", rc);
247                         }
248                         /* Force local cleanup on errors, too. */
249                         if (local_only || rc != ELDLM_OK)
250                                 ldlm_lock_cancel(lock);
251                 } else {
252                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
253                                    "client node");
254
255                         ldlm_resource_unlink_lock(lock);
256                         ldlm_lock_destroy(lock);
257                 }
258                 LDLM_LOCK_PUT(lock);
259         }
260         EXIT;
261 }
262
263 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
264 {
265         int i;
266
267         if (ns == NULL) {
268                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
269                 return ELDLM_OK;
270         }
271
272         l_lock(&ns->ns_lock);
273         for (i = 0; i < RES_HASH_SIZE; i++) {
274                 struct list_head *tmp, *pos;
275                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
276                         struct ldlm_resource *res;
277                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
278                         ldlm_resource_getref(res);
279
280                         cleanup_resource(res, &res->lr_granted, local_only);
281                         cleanup_resource(res, &res->lr_converting, local_only);
282                         cleanup_resource(res, &res->lr_waiting, local_only);
283
284                         /* XXX what a mess: don't force cleanup if we're
285                          * local_only (which is only used by recovery).  In that
286                          * case, we probably still have outstanding lock refs
287                          * which reference these resources. -phil */
288                         if (!ldlm_resource_putref(res) && !local_only) {
289                                 CERROR("Resource refcount nonzero (%d) after "
290                                        "lock cleanup; forcing cleanup.\n",
291                                        atomic_read(&res->lr_refcount));
292                                 ldlm_resource_dump(res);
293                                 atomic_set(&res->lr_refcount, 1);
294                                 ldlm_resource_putref(res);
295                         }
296                 }
297         }
298         l_unlock(&ns->ns_lock);
299
300         return ELDLM_OK;
301 }
302
303 /* Cleanup, but also free, the namespace */
304 int ldlm_namespace_free(struct ldlm_namespace *ns)
305 {
306         if (!ns)
307                 RETURN(ELDLM_OK);
308
309         spin_lock(&ldlm_namespace_lock);
310         list_del(&ns->ns_list_chain);
311
312         spin_unlock(&ldlm_namespace_lock);
313
314         ldlm_namespace_cleanup(ns, 0);
315
316         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
317         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
318         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
319         OBD_FREE(ns, sizeof(*ns));
320
321         return ELDLM_OK;
322 }
323
324 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
325 {
326         __u32 hash = 0;
327         int i;
328
329         for (i = 0; i < RES_NAME_SIZE; i++)
330                 hash += name.name[i];
331
332         hash += (__u32)((unsigned long)parent >> 4);
333
334         return (hash & RES_HASH_MASK);
335 }
336
337 static struct ldlm_resource *ldlm_resource_new(void)
338 {
339         struct ldlm_resource *res;
340
341         OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_KERNEL, sizeof *res);
342         if (res == NULL) {
343                 LBUG();
344                 return NULL;
345         }
346         memset(res, 0, sizeof(*res));
347
348         INIT_LIST_HEAD(&res->lr_children);
349         INIT_LIST_HEAD(&res->lr_childof);
350         INIT_LIST_HEAD(&res->lr_granted);
351         INIT_LIST_HEAD(&res->lr_converting);
352         INIT_LIST_HEAD(&res->lr_waiting);
353
354         atomic_set(&res->lr_refcount, 1);
355
356         return res;
357 }
358
359 /* Args: locked namespace
360  * Returns: newly-allocated, referenced, unlocked resource */
361 static struct ldlm_resource *
362 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
363                   struct ldlm_res_id name, __u32 type)
364 {
365         struct list_head *bucket;
366         struct ldlm_resource *res;
367         ENTRY;
368
369         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
370                 LBUG();
371                 RETURN(NULL);
372         }
373
374         res = ldlm_resource_new();
375         if (!res) {
376                 LBUG();
377                 RETURN(NULL);
378         }
379
380         spin_lock(&ns->ns_counter_lock);
381         ns->ns_resources++;
382         spin_unlock(&ns->ns_counter_lock);
383
384         l_lock(&ns->ns_lock);
385         memcpy(&res->lr_name, &name, sizeof(res->lr_name));
386         res->lr_namespace = ns;
387         ns->ns_refcount++;
388
389         res->lr_type = type;
390         res->lr_most_restr = LCK_NL;
391
392         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
393         list_add(&res->lr_hash, bucket);
394
395         if (parent == NULL) {
396                 list_add(&res->lr_childof, &ns->ns_root_list);
397         } else {
398                 res->lr_parent = parent;
399                 list_add(&res->lr_childof, &parent->lr_children);
400         }
401         l_unlock(&ns->ns_lock);
402
403         RETURN(res);
404 }
405
406 /* Args: unlocked namespace
407  * Locks: takes and releases ns->ns_lock and res->lr_lock
408  * Returns: referenced, unlocked ldlm_resource or NULL */
409 struct ldlm_resource *
410 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
411                   struct ldlm_res_id name, __u32 type, int create)
412 {
413         struct list_head *bucket, *tmp;
414         struct ldlm_resource *res = NULL;
415         ENTRY;
416
417         LASSERT(ns != NULL);
418         LASSERT(ns->ns_hash != NULL);
419
420         l_lock(&ns->ns_lock);
421         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
422
423         list_for_each(tmp, bucket) {
424                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
425
426                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
427                         ldlm_resource_getref(res);
428                         l_unlock(&ns->ns_lock);
429                         RETURN(res);
430                 }
431         }
432
433         if (create)
434                 res = ldlm_resource_add(ns, parent, name, type);
435         else
436                 res = NULL;
437
438         l_unlock(&ns->ns_lock);
439
440         RETURN(res);
441 }
442
443 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
444 {
445         atomic_inc(&res->lr_refcount);
446         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
447                atomic_read(&res->lr_refcount));
448         return res;
449 }
450
451 /* Returns 1 if the resource was freed, 0 if it remains. */
452 int ldlm_resource_putref(struct ldlm_resource *res)
453 {
454         int rc = 0;
455         ENTRY;
456
457         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
458                atomic_read(&res->lr_refcount) - 1);
459         LASSERT(atomic_read(&res->lr_refcount) > 0);
460         LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
461
462         if (atomic_dec_and_test(&res->lr_refcount)) {
463                 struct ldlm_namespace *ns = res->lr_namespace;
464                 ENTRY;
465
466                 l_lock(&ns->ns_lock);
467
468                 if (atomic_read(&res->lr_refcount) != 0) {
469                         /* We lost the race. */
470                         l_unlock(&ns->ns_lock);
471                         RETURN(rc);
472                 }
473
474                 if (!list_empty(&res->lr_granted)) {
475                         ldlm_resource_dump(res);
476                         LBUG();
477                 }
478
479                 if (!list_empty(&res->lr_converting)) {
480                         ldlm_resource_dump(res);
481                         LBUG();
482                 }
483
484                 if (!list_empty(&res->lr_waiting)) {
485                         ldlm_resource_dump(res);
486                         LBUG();
487                 }
488
489                 if (!list_empty(&res->lr_children)) {
490                         ldlm_resource_dump(res);
491                         LBUG();
492                 }
493
494                 ns->ns_refcount--;
495                 list_del_init(&res->lr_hash);
496                 list_del_init(&res->lr_childof);
497
498                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
499                 l_unlock(&ns->ns_lock);
500
501                 spin_lock(&ns->ns_counter_lock);
502                 ns->ns_resources--;
503                 spin_unlock(&ns->ns_counter_lock);
504
505                 rc = 1;
506                 EXIT;
507         }
508
509         RETURN(rc);
510 }
511
512 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
513                             struct ldlm_lock *lock)
514 {
515         l_lock(&res->lr_namespace->ns_lock);
516
517         ldlm_resource_dump(res);
518         CDEBUG(D_OTHER, "About to add this lock:\n");
519         ldlm_lock_dump(D_OTHER, lock);
520
521         if (lock->l_destroyed) {
522                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
523                 return;
524         }
525
526         LASSERT(list_empty(&lock->l_res_link));
527
528         list_add_tail(&lock->l_res_link, head);
529         l_unlock(&res->lr_namespace->ns_lock);
530 }
531
532 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
533 {
534         l_lock(&lock->l_resource->lr_namespace->ns_lock);
535         list_del_init(&lock->l_res_link);
536         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
537 }
538
539 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
540 {
541         desc->lr_type = res->lr_type;
542         memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
543         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
544 }
545
546 void ldlm_dump_all_namespaces(void)
547 {
548         struct list_head *tmp;
549
550         spin_lock(&ldlm_namespace_lock);
551
552         list_for_each(tmp, &ldlm_namespace_list) {
553                 struct ldlm_namespace *ns;
554                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
555                 ldlm_namespace_dump(ns);
556         }
557
558         spin_unlock(&ldlm_namespace_lock);
559 }
560
561 void ldlm_namespace_dump(struct ldlm_namespace *ns)
562 {
563         struct list_head *tmp;
564
565         l_lock(&ns->ns_lock);
566         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
567                ns->ns_refcount, ns->ns_client);
568
569         list_for_each(tmp, &ns->ns_root_list) {
570                 struct ldlm_resource *res;
571                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
572
573                 /* Once we have resources with children, this should really dump
574                  * them recursively. */
575                 ldlm_resource_dump(res);
576         }
577         l_unlock(&ns->ns_lock);
578 }
579
580 void ldlm_resource_dump(struct ldlm_resource *res)
581 {
582         struct list_head *tmp;
583         char name[256];
584
585         if (RES_NAME_SIZE != 3)
586                 LBUG();
587
588         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
589                  (unsigned long long)res->lr_name.name[0],
590                  (unsigned long long)res->lr_name.name[1],
591                  (unsigned long long)res->lr_name.name[2]);
592
593         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
594                atomic_read(&res->lr_refcount));
595         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
596                res->lr_namespace->ns_name);
597         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
598
599         CDEBUG(D_OTHER, "Granted locks:\n");
600         list_for_each(tmp, &res->lr_granted) {
601                 struct ldlm_lock *lock;
602                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
603                 ldlm_lock_dump(D_OTHER, lock);
604         }
605
606         CDEBUG(D_OTHER, "Converting locks:\n");
607         list_for_each(tmp, &res->lr_converting) {
608                 struct ldlm_lock *lock;
609                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
610                 ldlm_lock_dump(D_OTHER, lock);
611         }
612
613         CDEBUG(D_OTHER, "Waiting locks:\n");
614         list_for_each(tmp, &res->lr_waiting) {
615                 struct ldlm_lock *lock;
616                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
617                 ldlm_lock_dump(D_OTHER, lock);
618         }
619 }