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