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