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