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