Whamcloud - gitweb
Build properly with lprocfs disabled.
[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 #ifndef LPROC_SNMP
45 static int lprocfs_ll_rd(char *page, char **start, off_t off,
46                          int count, int *eof, void *data)
47 {
48         int len;
49         __u64 *temp = (__u64 *)data;
50
51         len = snprintf(page, count, "%Lu\n", *temp);
52
53         return len;
54 }
55 #endif
56
57 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
58 {
59         struct ldlm_namespace *ns = NULL;
60         struct list_head *bucket;
61         struct proc_dir_entry *proc_entry;
62
63         OBD_ALLOC(ns, sizeof(*ns));
64         if (!ns) {
65                 LBUG();
66                 GOTO(out, NULL);
67         }
68
69         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
70         if (!ns->ns_hash) {
71                 LBUG();
72                 GOTO(out, ns);
73         }
74         obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
75
76         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
77         if (!ns->ns_name) {
78                 LBUG();
79                 GOTO(out, ns);
80         }
81         strcpy(ns->ns_name, name);
82
83         INIT_LIST_HEAD(&ns->ns_root_list);
84         l_lock_init(&ns->ns_lock);
85         ns->ns_refcount = 0;
86         ns->ns_client = client;
87         spin_lock_init(&ns->ns_counter_lock);
88         ns->ns_locks = 0;
89         ns->ns_resources = 0;
90
91         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
92              bucket--)
93                 INIT_LIST_HEAD(bucket);
94
95         spin_lock(&ldlm_namespace_lock);
96         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
97         spin_unlock(&ldlm_namespace_lock);
98
99         ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
100         if (ns->ns_proc_dir == NULL)
101                 CERROR("Unable to create proc directory for namespace.\n");
102         proc_entry = create_proc_entry("resource_count", 0444, ns->ns_proc_dir);
103         proc_entry->read_proc = lprocfs_ll_rd;
104         proc_entry->data = &ns->ns_resources;
105         proc_entry = create_proc_entry("lock_count", 0444, ns->ns_proc_dir);
106         proc_entry->read_proc = lprocfs_ll_rd;
107         proc_entry->data = &ns->ns_locks;
108
109         RETURN(ns);
110
111  out:
112         if (ns && ns->ns_hash) {
113                 vfree(ns->ns_hash);
114                 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
115         }
116         if (ns && ns->ns_name)
117                 OBD_FREE(ns->ns_name, strlen(name) + 1);
118         if (ns)
119                 OBD_FREE(ns, sizeof(*ns));
120         return NULL;
121 }
122
123 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
124
125 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
126 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
127                              int local_only)
128 {
129         struct list_head *tmp, *pos;
130         int rc = 0, client = res->lr_namespace->ns_client;
131         ENTRY;
132
133         list_for_each_safe(tmp, pos, q) {
134                 struct ldlm_lock *lock;
135                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
136                 LDLM_LOCK_GET(lock);
137
138                 /* At shutdown time, don't call the cancellation callback */
139                 lock->l_flags |= LDLM_FL_CANCEL;
140
141                 if (client) {
142                         struct lustre_handle lockh;
143                         ldlm_lock2handle(lock, &lockh);
144                         if (!local_only) {
145                                 rc = ldlm_cli_cancel(&lockh);
146                                 if (rc)
147                                         CERROR("ldlm_cli_cancel: %d\n", rc);
148                         }
149                         /* Force local cleanup on errors, too. */
150                         if (local_only || rc != ELDLM_OK)
151                                 ldlm_lock_cancel(lock);
152                 } else {
153                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
154                                    "client node.\n");
155
156                         ldlm_resource_unlink_lock(lock);
157                         ldlm_lock_destroy(lock);
158                 }
159                 LDLM_LOCK_PUT(lock);
160         }
161 }
162
163 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
164 {
165         int i;
166
167         l_lock(&ns->ns_lock);
168         for (i = 0; i < RES_HASH_SIZE; i++) {
169                 struct list_head *tmp, *pos;
170                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
171                         struct ldlm_resource *res;
172                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
173                         ldlm_resource_getref(res);
174
175                         cleanup_resource(res, &res->lr_granted, local_only);
176                         cleanup_resource(res, &res->lr_converting, local_only);
177                         cleanup_resource(res, &res->lr_waiting, local_only);
178
179                         /* XXX this is a bit counter-intuitive and should
180                          * probably be cleaner: don't force cleanup if we're
181                          * local_only (which is only used by recovery).  We
182                          * probably still have outstanding lock refs which
183                          * reference these resources. -phil */
184                         if (!ldlm_resource_put(res) && !local_only) {
185                                 CERROR("Resource refcount nonzero (%d) after "
186                                        "lock cleanup; forcing cleanup.\n",
187                                        atomic_read(&res->lr_refcount));
188                                 ldlm_resource_dump(res);
189                                 atomic_set(&res->lr_refcount, 1);
190                                 ldlm_resource_put(res);
191                         }
192                 }
193         }
194         l_unlock(&ns->ns_lock);
195
196         return ELDLM_OK;
197 }
198
199 /* Cleanup, but also free, the namespace */
200 int ldlm_namespace_free(struct ldlm_namespace *ns)
201 {
202         if (!ns)
203                 RETURN(ELDLM_OK);
204
205         spin_lock(&ldlm_namespace_lock);
206         list_del(&ns->ns_list_chain);
207         remove_proc_entry("resource_count", ns->ns_proc_dir);
208         remove_proc_entry("lock_count", ns->ns_proc_dir);
209         remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
210         spin_unlock(&ldlm_namespace_lock);
211
212         ldlm_namespace_cleanup(ns, 0);
213
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         memcpy(res->lr_name, name, sizeof(res->lr_name));
290         res->lr_namespace = ns;
291         ns->ns_refcount++;
292
293         res->lr_type = type;
294         res->lr_most_restr = LCK_NL;
295
296         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
297         list_add(&res->lr_hash, bucket);
298
299         if (parent == NULL)
300                 list_add(&res->lr_childof, &ns->ns_root_list);
301         else {
302                 res->lr_parent = parent;
303                 list_add(&res->lr_childof, &parent->lr_children);
304         }
305
306         RETURN(res);
307 }
308
309 /* Args: unlocked namespace
310  * Locks: takes and releases ns->ns_lock and res->lr_lock
311  * Returns: referenced, unlocked ldlm_resource or NULL */
312 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
313                                         struct ldlm_resource *parent,
314                                         __u64 *name, __u32 type, int create)
315 {
316         struct list_head *bucket;
317         struct list_head *tmp = bucket;
318         struct ldlm_resource *res = NULL;
319         ENTRY;
320
321         if (ns == NULL || ns->ns_hash == NULL) {
322                 LBUG();
323                 RETURN(NULL);
324         }
325
326         l_lock(&ns->ns_lock);
327         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
328
329         list_for_each(tmp, bucket) {
330                 struct ldlm_resource *chk;
331                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
332
333                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
334                         res = chk;
335                         atomic_inc(&res->lr_refcount);
336                         EXIT;
337                         break;
338                 }
339         }
340
341         if (res == NULL && create)
342                 res = ldlm_resource_add(ns, parent, name, type);
343         l_unlock(&ns->ns_lock);
344
345         RETURN(res);
346 }
347
348 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
349 {
350         atomic_inc(&res->lr_refcount);
351         return res;
352 }
353
354 /* Returns 1 if the resource was freed, 0 if it remains. */
355 int ldlm_resource_put(struct ldlm_resource *res)
356 {
357         int rc = 0;
358
359         if (atomic_dec_and_test(&res->lr_refcount)) {
360                 struct ldlm_namespace *ns = res->lr_namespace;
361                 ENTRY;
362
363                 l_lock(&ns->ns_lock);
364
365                 if (atomic_read(&res->lr_refcount) != 0) {
366                         /* We lost the race. */
367                         l_unlock(&ns->ns_lock);
368                         goto out;
369                 }
370
371                 if (!list_empty(&res->lr_granted))
372                         LBUG();
373
374                 if (!list_empty(&res->lr_converting))
375                         LBUG();
376
377                 if (!list_empty(&res->lr_waiting))
378                         LBUG();
379
380                 if (!list_empty(&res->lr_children))
381                         LBUG();
382
383                 ns->ns_refcount--;
384                 list_del(&res->lr_hash);
385                 list_del(&res->lr_childof);
386
387                 kmem_cache_free(ldlm_resource_slab, res);
388                 l_unlock(&ns->ns_lock);
389
390                 spin_lock(&ns->ns_counter_lock);
391                 ns->ns_resources--;
392                 spin_unlock(&ns->ns_counter_lock);
393
394                 rc = 1;
395         } else {
396                 ENTRY;
397         out:
398                 if (atomic_read(&res->lr_refcount) < 0)
399                         LBUG();
400         }
401
402         RETURN(rc);
403 }
404
405 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
406                             struct ldlm_lock *lock)
407 {
408         l_lock(&res->lr_namespace->ns_lock);
409
410         ldlm_resource_dump(res);
411         ldlm_lock_dump(lock);
412
413         if (!list_empty(&lock->l_res_link))
414                 LBUG();
415
416         list_add(&lock->l_res_link, head);
417         l_unlock(&res->lr_namespace->ns_lock);
418 }
419
420 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
421 {
422         l_lock(&lock->l_resource->lr_namespace->ns_lock);
423         list_del_init(&lock->l_res_link);
424         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
425 }
426
427 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
428 {
429         desc->lr_type = res->lr_type;
430         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
431         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
432 }
433
434 void ldlm_dump_all_namespaces(void)
435 {
436         struct list_head *tmp;
437
438         spin_lock(&ldlm_namespace_lock);
439
440         list_for_each(tmp, &ldlm_namespace_list) {
441                 struct ldlm_namespace *ns;
442                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
443                 ldlm_namespace_dump(ns);
444         }
445
446         spin_unlock(&ldlm_namespace_lock);
447 }
448
449 void ldlm_namespace_dump(struct ldlm_namespace *ns)
450 {
451         struct list_head *tmp;
452
453         l_lock(&ns->ns_lock);
454         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
455                ns->ns_refcount, ns->ns_client);
456
457         list_for_each(tmp, &ns->ns_root_list) {
458                 struct ldlm_resource *res;
459                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
460
461                 /* Once we have resources with children, this should really dump
462                  * them recursively. */
463                 ldlm_resource_dump(res);
464         }
465         l_unlock(&ns->ns_lock);
466 }
467
468 void ldlm_resource_dump(struct ldlm_resource *res)
469 {
470         struct list_head *tmp;
471         char name[256];
472
473         if (RES_NAME_SIZE != 3)
474                 LBUG();
475
476         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
477                  (unsigned long long)res->lr_name[0],
478                  (unsigned long long)res->lr_name[1],
479                  (unsigned long long)res->lr_name[2]);
480
481         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
482                atomic_read(&res->lr_refcount));
483         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
484                res->lr_namespace->ns_name);
485         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
486
487         CDEBUG(D_OTHER, "Granted locks:\n");
488         list_for_each(tmp, &res->lr_granted) {
489                 struct ldlm_lock *lock;
490                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
491                 ldlm_lock_dump(lock);
492         }
493
494         CDEBUG(D_OTHER, "Converting locks:\n");
495         list_for_each(tmp, &res->lr_converting) {
496                 struct ldlm_lock *lock;
497                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
498                 ldlm_lock_dump(lock);
499         }
500
501         CDEBUG(D_OTHER, "Waiting locks:\n");
502         list_for_each(tmp, &res->lr_waiting) {
503                 struct ldlm_lock *lock;
504                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
505                 ldlm_lock_dump(lock);
506         }
507 }