Whamcloud - gitweb
A bunch of minor cleanups when using sizeof(). This even appears to have
[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 EXPORT_SYMTAB
13
14 #include <linux/version.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <asm/unistd.h>
18
19 #define DEBUG_SUBSYSTEM S_LDLM
20
21 #include <linux/obd_support.h>
22 #include <linux/obd_class.h>
23
24 #include <linux/lustre_dlm.h>
25
26 kmem_cache_t *ldlm_resource_slab;
27 kmem_cache_t *ldlm_lock_slab;
28
29 struct ldlm_namespace *ldlm_namespace_find(struct obd_device *obddev, __u32 id)
30 {
31         struct list_head *tmp;
32         struct ldlm_namespace *res;
33
34         res = NULL;
35         list_for_each(tmp, &obddev->u.ldlm.ldlm_namespaces) { 
36                 struct ldlm_namespace *chk;
37                 chk = list_entry(tmp, struct ldlm_namespace, ns_link);
38                 
39                 if ( chk->ns_id == id ) {
40                         res = chk;
41                         break;
42                 }
43         }
44
45         return res;
46 }
47
48 /* this must be called with ldlm_lock(obddev) held */
49 static void res_hash_init(struct ldlm_namespace *ns)
50 {
51         struct list_head *res_hash;
52         struct list_head *bucket;
53
54         if (ns->ns_hash != NULL)
55                 return;
56
57         /* FIXME: this memory appears to be leaked */
58         OBD_ALLOC(res_hash, sizeof(*res_hash) * RES_HASH_SIZE);
59         if (!res_hash)
60                 LBUG();
61
62         for (bucket = res_hash + RES_HASH_SIZE - 1; bucket >= res_hash;
63              bucket--)
64                 INIT_LIST_HEAD(bucket);
65
66         ns->ns_hash = res_hash;
67 }
68
69 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obddev, __u32 id)
70 {
71         struct ldlm_namespace *ns;
72
73         if (ldlm_namespace_find(obddev, id))
74                 LBUG();
75
76         OBD_ALLOC(ns, sizeof(*ns));
77         if (!ns)
78                 LBUG();
79
80         ns->ns_id = id;
81         INIT_LIST_HEAD(&ns->ns_root_list);
82
83         list_add(&ns->ns_link, &obddev->u.ldlm.ldlm_namespaces);
84
85         res_hash_init(ns); 
86         atomic_set(&ns->ns_refcount, 0);
87
88         return ns;
89 }
90
91 int ldlm_namespace_free(struct ldlm_namespace *ns)
92 {
93         if (atomic_read(&ns->ns_refcount))
94                 return -EBUSY;
95
96         list_del(&ns->ns_link);
97         OBD_FREE(ns->ns_hash, sizeof(struct list_head) * RES_HASH_SIZE);
98         OBD_FREE(ns, sizeof(*ns));
99
100         return 0;
101 }
102
103 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
104 {
105         __u32 hash = 0;
106         int i;
107
108         for (i = 0; i < RES_NAME_SIZE; i++)
109                 hash += name[i];
110
111         hash += (__u32)((unsigned long)parent >> 4);
112
113         return (hash & RES_HASH_MASK);
114 }
115
116 static struct ldlm_resource *ldlm_resource_new(void)
117 {
118         struct ldlm_resource *res;
119
120         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
121         if (res == NULL)
122                 LBUG();
123         memset(res, 0, sizeof(*res));
124
125         INIT_LIST_HEAD(&res->lr_children);
126         INIT_LIST_HEAD(&res->lr_childof);
127         INIT_LIST_HEAD(&res->lr_granted);
128         INIT_LIST_HEAD(&res->lr_converting);
129         INIT_LIST_HEAD(&res->lr_waiting);
130
131         res->lr_lock = SPIN_LOCK_UNLOCKED;
132
133         atomic_set(&res->lr_refcount, 1);
134
135         return res;
136 }
137
138 /* ldlm_lock(obddev) must be taken before calling resource_add */
139 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
140                                                struct ldlm_resource *parent,
141                                                __u64 *name, __u32 type)
142 {
143         struct list_head *bucket;
144         struct ldlm_resource *res;
145
146         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
147
148         res = ldlm_resource_new();
149         if (!res)
150                 LBUG();
151
152         memcpy(res->lr_name, name, sizeof(res->lr_name));
153         res->lr_namespace = ns;
154         if (type < 0 || type > LDLM_MAX_TYPE) 
155                 LBUG();
156
157         res->lr_type = type; 
158         res->lr_most_restr = LCK_NL;
159         list_add(&res->lr_hash, bucket);
160         atomic_inc(&ns->ns_refcount);
161         if (parent == NULL) {
162                 res->lr_parent = res;
163                 list_add(&res->lr_rootlink, &ns->ns_root_list);
164         } else {
165                 res->lr_parent = parent;
166                 list_add(&res->lr_childof, &parent->lr_children);
167         }
168
169         return res;
170 }
171
172 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
173                                         struct ldlm_resource *parent,
174                                         __u64 *name, __u32 type, int create)
175 {
176         struct list_head *bucket;
177         struct list_head *tmp = bucket;
178         struct ldlm_resource *res;
179
180         ENTRY;
181
182         if (ns->ns_hash == NULL)
183                 RETURN(NULL);
184         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
185
186         res = NULL;
187         list_for_each(tmp, bucket) {
188                 struct ldlm_resource *chk;
189                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
190
191                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
192                         res = chk;
193                         atomic_inc(&res->lr_refcount);
194                         break;
195                 }
196         }
197
198         if (res == NULL && create)
199                 res = ldlm_resource_add(ns, parent, name, type);
200
201         RETURN(res);
202 }
203
204 int ldlm_resource_put(struct ldlm_resource *res)
205 {
206         int rc = 0; 
207
208         if (atomic_read(&res->lr_refcount) <= 0)
209                 LBUG();
210
211         if (atomic_dec_and_test(&res->lr_refcount)) {
212                 if (!list_empty(&res->lr_granted))
213                         LBUG();
214
215                 if (!list_empty(&res->lr_converting))
216                         LBUG();
217
218                 if (!list_empty(&res->lr_waiting))
219                         LBUG();
220
221                 atomic_dec(&res->lr_namespace->ns_refcount);
222                 list_del(&res->lr_hash);
223                 list_del(&res->lr_rootlink);
224                 list_del(&res->lr_childof);
225
226                 kmem_cache_free(ldlm_resource_slab, res);
227                 rc = 1;
228         }
229
230         return rc; 
231 }
232
233 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
234                             struct ldlm_lock *lock)
235 {
236         list_add(&lock->l_res_link, head);
237         atomic_inc(&res->lr_refcount);
238 }
239
240 void ldlm_resource_del_lock(struct ldlm_lock *lock)
241 {
242         list_del(&lock->l_res_link);
243         atomic_dec(&lock->l_resource->lr_refcount);
244 }
245
246 int ldlm_get_resource_handle(struct ldlm_resource *res, struct ldlm_handle *h)
247 {
248         LBUG();
249         return 0;
250 }
251
252 void ldlm_resource_dump(struct ldlm_resource *res)
253 {
254         struct list_head *tmp;
255         char name[256];
256
257         if (RES_NAME_SIZE != 3)
258                 LBUG();
259
260         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
261                  res->lr_name[0], res->lr_name[1], res->lr_name[2]);
262
263         CDEBUG(D_OTHER, "--- Resource: %p (%s)\n", res, name);
264         CDEBUG(D_OTHER, "Namespace: %p (%u)\n", res->lr_namespace,
265                res->lr_namespace->ns_id);
266         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
267
268         CDEBUG(D_OTHER, "Granted locks:\n");
269         list_for_each(tmp, &res->lr_granted) {
270                 struct ldlm_lock *lock;
271                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
272                 ldlm_lock_dump(lock);
273         }
274
275         CDEBUG(D_OTHER, "Converting locks:\n");
276         list_for_each(tmp, &res->lr_converting) {
277                 struct ldlm_lock *lock;
278                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
279                 ldlm_lock_dump(lock);
280         }
281
282         CDEBUG(D_OTHER, "Waiting locks:\n");
283         list_for_each(tmp, &res->lr_waiting) {
284                 struct ldlm_lock *lock;
285                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
286                 ldlm_lock_dump(lock);
287         }
288 }
289