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