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