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