Whamcloud - gitweb
- first fixes to get things working again: current state:
[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 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
19 {
20         struct ldlm_namespace *ns = NULL;
21         struct list_head *bucket;
22
23         OBD_ALLOC(ns, sizeof(*ns));
24         if (!ns) {
25                 LBUG();
26                 GOTO(out, NULL);
27         }
28
29         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
30         if (!ns->ns_hash) {
31                 LBUG();
32                 GOTO(out, ns);
33         }
34
35         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
36         if (!ns->ns_name) {
37                 LBUG();
38                 GOTO(out, ns);
39         }
40         strcpy(ns->ns_name, name);
41
42         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
43                            &ns->ns_rpc_client);
44
45         INIT_LIST_HEAD(&ns->ns_root_list);
46         l_lock_init(&ns->ns_lock);
47         ns->ns_refcount = 0;
48         ns->ns_client = client;
49
50         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
51              bucket--)
52                 INIT_LIST_HEAD(bucket);
53         RETURN(ns);
54
55  out:
56         if (ns && ns->ns_hash)
57                 vfree(ns->ns_hash);
58         if (ns && ns->ns_name)
59                 OBD_FREE(ns->ns_name, strlen(name) + 1);
60         if (ns)
61                 OBD_FREE(ns, sizeof(*ns));
62         return NULL;
63 }
64
65 static int cleanup_resource(struct ldlm_resource *res, struct list_head *q)
66 {
67         struct list_head *tmp, *pos;
68         int rc = 0, client = res->lr_namespace->ns_client;
69         ENTRY;
70
71         list_for_each_safe(tmp, pos, q) {
72                 struct ldlm_lock *lock;
73                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
74
75                 if (client) {
76                         struct lustre_handle lockh;
77                         ldlm_lock2handle(lock, &lockh);
78                         rc = ldlm_cli_cancel(&lockh);
79                         if (rc < 0) {
80                                 CERROR("ldlm_cli_cancel: %d\n", rc);
81                                 LBUG();
82                         }
83                         if (rc == ELDLM_RESOURCE_FREED)
84                                 rc = 1;
85                 } else {
86                         CERROR("Freeing a lock still held by a client node.\n");
87
88                         ldlm_resource_unlink_lock(lock);
89                         ldlm_lock_destroy(lock);
90
91                         rc = ldlm_resource_put(res);
92                 }
93         }
94
95         RETURN(rc);
96 }
97
98 int ldlm_namespace_free(struct ldlm_namespace *ns)
99 {
100         struct list_head *tmp, *pos;
101         int i, rc;
102
103         if (!ns)
104                 RETURN(ELDLM_OK);
105
106         l_lock(&ns->ns_lock);
107
108         for (i = 0; i < RES_HASH_SIZE; i++) {
109                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
110                         struct ldlm_resource *res;
111                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
112
113                         rc = cleanup_resource(res, &res->lr_granted);
114                         if (!rc)
115                                 rc = cleanup_resource(res, &res->lr_converting);
116                         if (!rc)
117                                 rc = cleanup_resource(res, &res->lr_waiting);
118
119                         if (rc == 0) {
120                                 CERROR("Resource refcount nonzero (%d) after "
121                                        "lock cleanup; forcing cleanup.\n",
122                                        atomic_read(&res->lr_refcount));
123                                 ldlm_resource_dump(res);
124                                 atomic_set(&res->lr_refcount, 1);
125                                 rc = ldlm_resource_put(res);
126                         }
127                 }
128         }
129
130         vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
131         ptlrpc_cleanup_client(&ns->ns_rpc_client);
132         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
133         OBD_FREE(ns, sizeof(*ns));
134
135         return ELDLM_OK;
136 }
137
138 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
139 {
140         __u32 hash = 0;
141         int i;
142
143         for (i = 0; i < RES_NAME_SIZE; i++)
144                 hash += name[i];
145
146         hash += (__u32)((unsigned long)parent >> 4);
147
148         return (hash & RES_HASH_MASK);
149 }
150
151 static struct ldlm_resource *ldlm_resource_new(void)
152 {
153         struct ldlm_resource *res;
154
155         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
156         if (res == NULL) {
157                 LBUG();
158                 return NULL;
159         }
160         memset(res, 0, sizeof(*res));
161
162         INIT_LIST_HEAD(&res->lr_children);
163         INIT_LIST_HEAD(&res->lr_childof);
164         INIT_LIST_HEAD(&res->lr_granted);
165         INIT_LIST_HEAD(&res->lr_converting);
166         INIT_LIST_HEAD(&res->lr_waiting);
167
168         atomic_set(&res->lr_refcount, 1);
169
170         return res;
171 }
172
173 /* Args: locked namespace
174  * Returns: newly-allocated, referenced, unlocked resource */
175 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
176                                                struct ldlm_resource *parent,
177                                                __u64 *name, __u32 type)
178 {
179         struct list_head *bucket;
180         struct ldlm_resource *res;
181         ENTRY;
182
183         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
184                 LBUG();
185                 RETURN(NULL);
186         }
187
188         res = ldlm_resource_new();
189         if (!res) {
190                 LBUG();
191                 RETURN(NULL);
192         }
193
194         memcpy(res->lr_name, name, sizeof(res->lr_name));
195         res->lr_namespace = ns;
196         ns->ns_refcount++;
197
198         res->lr_type = type;
199         res->lr_most_restr = LCK_NL;
200
201         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
202         list_add(&res->lr_hash, bucket);
203
204         if (parent == NULL) {
205                 res->lr_parent = res;
206                 list_add(&res->lr_rootlink, &ns->ns_root_list);
207         } else {
208                 res->lr_parent = parent;
209                 list_add(&res->lr_childof, &parent->lr_children);
210         }
211
212         RETURN(res);
213 }
214
215 /* Args: unlocked namespace
216  * Locks: takes and releases ns->ns_lock and res->lr_lock
217  * Returns: referenced, unlocked ldlm_resource or NULL */
218 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
219                                         struct ldlm_resource *parent,
220                                         __u64 *name, __u32 type, int create)
221 {
222         struct list_head *bucket;
223         struct list_head *tmp = bucket;
224         struct ldlm_resource *res = NULL;
225         ENTRY;
226
227         if (ns == NULL || ns->ns_hash == NULL) {
228                 LBUG();
229                 RETURN(NULL);
230         }
231
232         l_lock(&ns->ns_lock);
233         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
234
235         list_for_each(tmp, bucket) {
236                 struct ldlm_resource *chk;
237                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
238
239                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
240                         res = chk;
241                         atomic_inc(&res->lr_refcount);
242                         EXIT;
243                         break;
244                 }
245         }
246
247         if (res == NULL && create)
248                 res = ldlm_resource_add(ns, parent, name, type);
249         l_unlock(&ns->ns_lock);
250
251         RETURN(res);
252 }
253
254 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
255 {
256         atomic_inc(&res->lr_refcount);
257         return res;
258 }
259
260 /* Returns 1 if the resource was freed, 0 if it remains. */
261 int ldlm_resource_put(struct ldlm_resource *res)
262 {
263         int rc = 0;
264
265         if (atomic_dec_and_test(&res->lr_refcount)) {
266                 struct ldlm_namespace *ns = res->lr_namespace;
267                 ENTRY;
268
269                 l_lock(&ns->ns_lock);
270
271                 if (atomic_read(&res->lr_refcount) != 0) {
272                         /* We lost the race. */
273                         l_unlock(&ns->ns_lock);
274                         goto out;
275                 }
276
277                 if (!list_empty(&res->lr_granted))
278                         LBUG();
279
280                 if (!list_empty(&res->lr_converting))
281                         LBUG();
282
283                 if (!list_empty(&res->lr_waiting))
284                         LBUG();
285
286                 if (!list_empty(&res->lr_children))
287                         LBUG();
288
289                 ns->ns_refcount--;
290                 list_del(&res->lr_hash);
291                 list_del(&res->lr_rootlink);
292                 list_del(&res->lr_childof);
293
294                 kmem_cache_free(ldlm_resource_slab, res);
295                 l_unlock(&ns->ns_lock);
296                 rc = 1;
297         } else {
298                 ENTRY;
299         out:
300                 if (atomic_read(&res->lr_refcount) < 0)
301                         LBUG();
302         }
303
304         RETURN(rc);
305 }
306
307 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
308                             struct ldlm_lock *lock)
309 {
310         l_lock(&res->lr_namespace->ns_lock);
311
312         ldlm_resource_dump(res);
313         ldlm_lock_dump(lock);
314
315         if (!list_empty(&lock->l_res_link))
316                 LBUG();
317
318         list_add(&lock->l_res_link, head);
319         l_unlock(&res->lr_namespace->ns_lock);
320 }
321
322 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
323 {
324         l_lock(&lock->l_resource->lr_namespace->ns_lock);
325         list_del_init(&lock->l_res_link);
326         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
327 }
328
329 int ldlm_get_resource_handle(struct ldlm_resource *res, struct lustre_handle *h)
330 {
331         LBUG();
332         return 0;
333 }
334
335 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
336 {
337         desc->lr_type = res->lr_type;
338         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
339         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
340 }
341
342 void ldlm_resource_dump(struct ldlm_resource *res)
343 {
344         struct list_head *tmp;
345         char name[256];
346
347         if (RES_NAME_SIZE != 3)
348                 LBUG();
349
350         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
351                  (unsigned long long)res->lr_name[0],
352                  (unsigned long long)res->lr_name[1],
353                  (unsigned long long)res->lr_name[2]);
354
355         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
356                atomic_read(&res->lr_refcount));
357         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
358                res->lr_namespace->ns_name);
359         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
360
361         CDEBUG(D_OTHER, "Granted locks:\n");
362         list_for_each(tmp, &res->lr_granted) {
363                 struct ldlm_lock *lock;
364                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
365                 ldlm_lock_dump(lock);
366         }
367
368         CDEBUG(D_OTHER, "Converting locks:\n");
369         list_for_each(tmp, &res->lr_converting) {
370                 struct ldlm_lock *lock;
371                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
372                 ldlm_lock_dump(lock);
373         }
374
375         CDEBUG(D_OTHER, "Waiting locks:\n");
376         list_for_each(tmp, &res->lr_waiting) {
377                 struct ldlm_lock *lock;
378                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
379                 ldlm_lock_dump(lock);
380         }
381 }