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