Whamcloud - gitweb
the first try at lock LRU
[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 extern struct proc_dir_entry proc_root;
23
24 int ldlm_proc_setup(struct obd_device *obd)
25 {
26         ENTRY;
27
28         LASSERT(ldlm_ns_proc_dir == NULL);
29
30         ldlm_ns_proc_dir = proc_mkdir("ldlm", &proc_root);
31         if (ldlm_ns_proc_dir == NULL) {
32                 CERROR("Couldn't create /proc/ldlm\n");
33                 RETURN(-EPERM);
34         }
35         RETURN(0);
36 }
37
38 void ldlm_proc_cleanup(struct obd_device *obd)
39 {
40         remove_proc_entry("ldlm", &proc_root);
41 }
42
43 /* FIXME: This can go away when we start to really use lprocfs */
44 static int ldlm_proc_ll_rd(char *page, char **start, off_t off,
45                          int count, int *eof, void *data)
46 {
47         int len;
48         __u64 *temp = (__u64 *)data;
49
50         len = snprintf(page, count, "%Lu\n", *temp);
51
52         return len;
53 }
54
55 #define LDLM_MAX_UNUSED 20
56 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
57 {
58         struct ldlm_namespace *ns = NULL;
59         struct list_head *bucket;
60         struct proc_dir_entry *proc_entry;
61
62         OBD_ALLOC(ns, sizeof(*ns));
63         if (!ns) {
64                 LBUG();
65                 GOTO(out, NULL);
66         }
67
68         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
69         if (!ns->ns_hash) {
70                 LBUG();
71                 GOTO(out, ns);
72         }
73         obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
74
75         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
76         if (!ns->ns_name) {
77                 LBUG();
78                 GOTO(out, ns);
79         }
80         strcpy(ns->ns_name, name);
81
82         INIT_LIST_HEAD(&ns->ns_root_list);
83         l_lock_init(&ns->ns_lock);
84         ns->ns_refcount = 0;
85         ns->ns_client = client;
86         spin_lock_init(&ns->ns_counter_lock);
87         ns->ns_locks = 0;
88         ns->ns_resources = 0;
89
90         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
91              bucket--)
92                 INIT_LIST_HEAD(bucket);
93
94         INIT_LIST_HEAD(&ns->ns_unused_list);
95         ns->ns_nr_unused = 0;
96         ns->ns_max_unused = LDLM_MAX_UNUSED;
97
98         spin_lock(&ldlm_namespace_lock);
99         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
100         spin_unlock(&ldlm_namespace_lock);
101
102         ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
103         if (ns->ns_proc_dir == NULL)
104                 CERROR("Unable to create proc directory for namespace.\n");
105         proc_entry = create_proc_entry("resource_count", 0444, ns->ns_proc_dir);
106         proc_entry->read_proc = ldlm_proc_ll_rd;
107         proc_entry->data = &ns->ns_resources;
108         proc_entry = create_proc_entry("lock_count", 0444, ns->ns_proc_dir);
109         proc_entry->read_proc = ldlm_proc_ll_rd;
110         proc_entry->data = &ns->ns_locks;
111
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         remove_proc_entry("resource_count", ns->ns_proc_dir);
211         remove_proc_entry("lock_count", ns->ns_proc_dir);
212         remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
213         spin_unlock(&ldlm_namespace_lock);
214
215         ldlm_namespace_cleanup(ns, 0);
216
217         vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
218         obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
219         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
220         OBD_FREE(ns, sizeof(*ns));
221
222         return ELDLM_OK;
223 }
224
225 int ldlm_client_free(struct obd_export *exp)
226 {
227         struct ldlm_export_data *led = &exp->exp_ldlm_data;
228         ptlrpc_cleanup_client(&led->led_import);
229         RETURN(0);
230 }
231
232 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
233 {
234         __u32 hash = 0;
235         int i;
236
237         for (i = 0; i < RES_NAME_SIZE; i++)
238                 hash += name[i];
239
240         hash += (__u32)((unsigned long)parent >> 4);
241
242         return (hash & RES_HASH_MASK);
243 }
244
245 static struct ldlm_resource *ldlm_resource_new(void)
246 {
247         struct ldlm_resource *res;
248
249         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
250         if (res == NULL) {
251                 LBUG();
252                 return NULL;
253         }
254         memset(res, 0, sizeof(*res));
255
256         INIT_LIST_HEAD(&res->lr_children);
257         INIT_LIST_HEAD(&res->lr_childof);
258         INIT_LIST_HEAD(&res->lr_granted);
259         INIT_LIST_HEAD(&res->lr_converting);
260         INIT_LIST_HEAD(&res->lr_waiting);
261
262         atomic_set(&res->lr_refcount, 1);
263
264         return res;
265 }
266
267 /* Args: locked namespace
268  * Returns: newly-allocated, referenced, unlocked resource */
269 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
270                                                struct ldlm_resource *parent,
271                                                __u64 *name, __u32 type)
272 {
273         struct list_head *bucket;
274         struct ldlm_resource *res;
275         ENTRY;
276
277         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
278                 LBUG();
279                 RETURN(NULL);
280         }
281
282         res = ldlm_resource_new();
283         if (!res) {
284                 LBUG();
285                 RETURN(NULL);
286         }
287
288         spin_lock(&ns->ns_counter_lock);
289         ns->ns_resources++;
290         spin_unlock(&ns->ns_counter_lock);
291
292         memcpy(res->lr_name, name, sizeof(res->lr_name));
293         res->lr_namespace = ns;
294         ns->ns_refcount++;
295
296         res->lr_type = type;
297         res->lr_most_restr = LCK_NL;
298
299         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
300         list_add(&res->lr_hash, bucket);
301
302         if (parent == NULL)
303                 list_add(&res->lr_childof, &ns->ns_root_list);
304         else {
305                 res->lr_parent = parent;
306                 list_add(&res->lr_childof, &parent->lr_children);
307         }
308
309         RETURN(res);
310 }
311
312 /* Args: unlocked namespace
313  * Locks: takes and releases ns->ns_lock and res->lr_lock
314  * Returns: referenced, unlocked ldlm_resource or NULL */
315 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
316                                         struct ldlm_resource *parent,
317                                         __u64 *name, __u32 type, int create)
318 {
319         struct list_head *bucket;
320         struct list_head *tmp = bucket;
321         struct ldlm_resource *res = NULL;
322         ENTRY;
323
324         if (ns == NULL || ns->ns_hash == NULL) {
325                 LBUG();
326                 RETURN(NULL);
327         }
328
329         l_lock(&ns->ns_lock);
330         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
331
332         list_for_each(tmp, bucket) {
333                 struct ldlm_resource *chk;
334                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
335
336                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
337                         res = chk;
338                         atomic_inc(&res->lr_refcount);
339                         EXIT;
340                         break;
341                 }
342         }
343
344         if (res == NULL && create)
345                 res = ldlm_resource_add(ns, parent, name, type);
346         l_unlock(&ns->ns_lock);
347
348         RETURN(res);
349 }
350
351 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
352 {
353         atomic_inc(&res->lr_refcount);
354         return res;
355 }
356
357 /* Returns 1 if the resource was freed, 0 if it remains. */
358 int ldlm_resource_put(struct ldlm_resource *res)
359 {
360         int rc = 0;
361
362         if (atomic_dec_and_test(&res->lr_refcount)) {
363                 struct ldlm_namespace *ns = res->lr_namespace;
364                 ENTRY;
365
366                 l_lock(&ns->ns_lock);
367
368                 if (atomic_read(&res->lr_refcount) != 0) {
369                         /* We lost the race. */
370                         l_unlock(&ns->ns_lock);
371                         goto out;
372                 }
373
374                 if (!list_empty(&res->lr_granted))
375                         LBUG();
376
377                 if (!list_empty(&res->lr_converting))
378                         LBUG();
379
380                 if (!list_empty(&res->lr_waiting))
381                         LBUG();
382
383                 if (!list_empty(&res->lr_children))
384                         LBUG();
385
386                 ns->ns_refcount--;
387                 list_del(&res->lr_hash);
388                 list_del(&res->lr_childof);
389
390                 kmem_cache_free(ldlm_resource_slab, res);
391                 l_unlock(&ns->ns_lock);
392
393                 spin_lock(&ns->ns_counter_lock);
394                 ns->ns_resources--;
395                 spin_unlock(&ns->ns_counter_lock);
396
397                 rc = 1;
398         } else {
399                 ENTRY;
400         out:
401                 if (atomic_read(&res->lr_refcount) < 0)
402                         LBUG();
403         }
404
405         RETURN(rc);
406 }
407
408 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
409                             struct ldlm_lock *lock)
410 {
411         l_lock(&res->lr_namespace->ns_lock);
412
413         ldlm_resource_dump(res);
414         ldlm_lock_dump(lock);
415
416         if (!list_empty(&lock->l_res_link))
417                 LBUG();
418
419         list_add(&lock->l_res_link, head);
420         l_unlock(&res->lr_namespace->ns_lock);
421 }
422
423 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
424 {
425         l_lock(&lock->l_resource->lr_namespace->ns_lock);
426         list_del_init(&lock->l_res_link);
427         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
428 }
429
430 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
431 {
432         desc->lr_type = res->lr_type;
433         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
434         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
435 }
436
437 void ldlm_dump_all_namespaces(void)
438 {
439         struct list_head *tmp;
440
441         spin_lock(&ldlm_namespace_lock);
442
443         list_for_each(tmp, &ldlm_namespace_list) {
444                 struct ldlm_namespace *ns;
445                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
446                 ldlm_namespace_dump(ns);
447         }
448
449         spin_unlock(&ldlm_namespace_lock);
450 }
451
452 void ldlm_namespace_dump(struct ldlm_namespace *ns)
453 {
454         struct list_head *tmp;
455
456         l_lock(&ns->ns_lock);
457         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
458                ns->ns_refcount, ns->ns_client);
459
460         list_for_each(tmp, &ns->ns_root_list) {
461                 struct ldlm_resource *res;
462                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
463
464                 /* Once we have resources with children, this should really dump
465                  * them recursively. */
466                 ldlm_resource_dump(res);
467         }
468         l_unlock(&ns->ns_lock);
469 }
470
471 void ldlm_resource_dump(struct ldlm_resource *res)
472 {
473         struct list_head *tmp;
474         char name[256];
475
476         if (RES_NAME_SIZE != 3)
477                 LBUG();
478
479         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
480                  (unsigned long long)res->lr_name[0],
481                  (unsigned long long)res->lr_name[1],
482                  (unsigned long long)res->lr_name[2]);
483
484         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
485                atomic_read(&res->lr_refcount));
486         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
487                res->lr_namespace->ns_name);
488         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
489
490         CDEBUG(D_OTHER, "Granted locks:\n");
491         list_for_each(tmp, &res->lr_granted) {
492                 struct ldlm_lock *lock;
493                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
494                 ldlm_lock_dump(lock);
495         }
496
497         CDEBUG(D_OTHER, "Converting locks:\n");
498         list_for_each(tmp, &res->lr_converting) {
499                 struct ldlm_lock *lock;
500                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
501                 ldlm_lock_dump(lock);
502         }
503
504         CDEBUG(D_OTHER, "Waiting locks:\n");
505         list_for_each(tmp, &res->lr_waiting) {
506                 struct ldlm_lock *lock;
507                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
508                 ldlm_lock_dump(lock);
509         }
510 }