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