GFS2: Bring back lvb-related stuff to lock_nolock to support quotas
[safe/jmp/linux-2.6] / fs / gfs2 / locking.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/kmod.h>
17 #include <linux/fs.h>
18 #include <linux/delay.h>
19 #include <linux/lm_interface.h>
20
21 struct lmh_wrapper {
22         struct list_head lw_list;
23         const struct lm_lockops *lw_ops;
24 };
25
26 struct nolock_lockspace {
27         unsigned int nl_lvb_size;
28 };
29
30 /**
31  * nolock_get_lock - get a lm_lock_t given a descripton of the lock
32  * @lockspace: the lockspace the lock lives in
33  * @name: the name of the lock
34  * @lockp: return the lm_lock_t here
35  *
36  * Returns: 0 on success, -EXXX on failure
37  */
38
39 static int nolock_get_lock(void *lockspace, struct lm_lockname *name,
40                            void **lockp)
41 {
42         *lockp = lockspace;
43         return 0;
44 }
45
46 /**
47  * nolock_put_lock - get rid of a lock structure
48  * @lock: the lock to throw away
49  *
50  */
51
52 static void nolock_put_lock(void *lock)
53 {
54 }
55
56 /**
57  * nolock_hold_lvb - hold on to a lock value block
58  * @lock: the lock the LVB is associated with
59  * @lvbp: return the lm_lvb_t here
60  *
61  * Returns: 0 on success, -EXXX on failure
62  */
63
64 static int nolock_hold_lvb(void *lock, char **lvbp)
65 {
66         struct nolock_lockspace *nl = lock;
67         int error = 0;
68
69         *lvbp = kzalloc(nl->nl_lvb_size, GFP_KERNEL);
70         if (!*lvbp)
71                 error = -ENOMEM;
72
73         return error;
74 }
75
76 /**
77  * nolock_unhold_lvb - release a LVB
78  * @lock: the lock the LVB is associated with
79  * @lvb: the lock value block
80  *
81  */
82
83 static void nolock_unhold_lvb(void *lock, char *lvb)
84 {
85         kfree(lvb);
86 }
87
88 static int nolock_mount(char *table_name, char *host_data,
89                         lm_callback_t cb, void *cb_data,
90                         unsigned int min_lvb_size, int flags,
91                         struct lm_lockstruct *lockstruct,
92                         struct kobject *fskobj);
93 static void nolock_unmount(void *lockspace);
94
95 /* List of registered low-level locking protocols.  A file system selects one
96    of them by name at mount time, e.g. lock_nolock, lock_dlm. */
97
98 static const struct lm_lockops nolock_ops = {
99         .lm_proto_name = "lock_nolock",
100         .lm_mount = nolock_mount,
101         .lm_unmount = nolock_unmount,
102         .lm_get_lock = nolock_get_lock,
103         .lm_put_lock = nolock_put_lock,
104         .lm_hold_lvb = nolock_hold_lvb,
105         .lm_unhold_lvb = nolock_unhold_lvb,
106 };
107
108 static struct lmh_wrapper nolock_proto  = {
109         .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list),
110         .lw_ops = &nolock_ops,
111 };
112
113 static LIST_HEAD(lmh_list);
114 static DEFINE_MUTEX(lmh_lock);
115
116 static int nolock_mount(char *table_name, char *host_data,
117                         lm_callback_t cb, void *cb_data,
118                         unsigned int min_lvb_size, int flags,
119                         struct lm_lockstruct *lockstruct,
120                         struct kobject *fskobj)
121 {
122         char *c;
123         unsigned int jid;
124         struct nolock_lockspace *nl;
125
126         c = strstr(host_data, "jid=");
127         if (!c)
128                 jid = 0;
129         else {
130                 c += 4;
131                 sscanf(c, "%u", &jid);
132         }
133
134         nl = kzalloc(sizeof(struct nolock_lockspace), GFP_KERNEL);
135         if (!nl)
136                 return -ENOMEM;
137
138         nl->nl_lvb_size = min_lvb_size;
139
140         lockstruct->ls_jid = jid;
141         lockstruct->ls_first = 1;
142         lockstruct->ls_lvb_size = min_lvb_size;
143         lockstruct->ls_lockspace = nl;
144         lockstruct->ls_ops = &nolock_ops;
145         lockstruct->ls_flags = LM_LSFLAG_LOCAL;
146
147         return 0;
148 }
149
150 static void nolock_unmount(void *lockspace)
151 {
152         struct nolock_lockspace *nl = lockspace;
153         kfree(nl);
154 }
155
156 /**
157  * gfs2_register_lockproto - Register a low-level locking protocol
158  * @proto: the protocol definition
159  *
160  * Returns: 0 on success, -EXXX on failure
161  */
162
163 int gfs2_register_lockproto(const struct lm_lockops *proto)
164 {
165         struct lmh_wrapper *lw;
166
167         mutex_lock(&lmh_lock);
168
169         list_for_each_entry(lw, &lmh_list, lw_list) {
170                 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
171                         mutex_unlock(&lmh_lock);
172                         printk(KERN_INFO "GFS2: protocol %s already exists\n",
173                                proto->lm_proto_name);
174                         return -EEXIST;
175                 }
176         }
177
178         lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
179         if (!lw) {
180                 mutex_unlock(&lmh_lock);
181                 return -ENOMEM;
182         }
183
184         lw->lw_ops = proto;
185         list_add(&lw->lw_list, &lmh_list);
186
187         mutex_unlock(&lmh_lock);
188
189         return 0;
190 }
191
192 /**
193  * gfs2_unregister_lockproto - Unregister a low-level locking protocol
194  * @proto: the protocol definition
195  *
196  */
197
198 void gfs2_unregister_lockproto(const struct lm_lockops *proto)
199 {
200         struct lmh_wrapper *lw;
201
202         mutex_lock(&lmh_lock);
203
204         list_for_each_entry(lw, &lmh_list, lw_list) {
205                 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
206                         list_del(&lw->lw_list);
207                         mutex_unlock(&lmh_lock);
208                         kfree(lw);
209                         return;
210                 }
211         }
212
213         mutex_unlock(&lmh_lock);
214
215         printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
216                proto->lm_proto_name);
217 }
218
219 /**
220  * gfs2_mount_lockproto - Mount a lock protocol
221  * @proto_name - the name of the protocol
222  * @table_name - the name of the lock space
223  * @host_data - data specific to this host
224  * @cb - the callback to the code using the lock module
225  * @sdp - The GFS2 superblock
226  * @min_lvb_size - the mininum LVB size that the caller can deal with
227  * @flags - LM_MFLAG_*
228  * @lockstruct - a structure returned describing the mount
229  *
230  * Returns: 0 on success, -EXXX on failure
231  */
232
233 int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data,
234                          lm_callback_t cb, void *cb_data,
235                          unsigned int min_lvb_size, int flags,
236                          struct lm_lockstruct *lockstruct,
237                          struct kobject *fskobj)
238 {
239         struct lmh_wrapper *lw = NULL;
240         int try = 0;
241         int error, found;
242
243
244 retry:
245         mutex_lock(&lmh_lock);
246
247         if (list_empty(&nolock_proto.lw_list))
248                 list_add(&nolock_proto.lw_list, &lmh_list);
249
250         found = 0;
251         list_for_each_entry(lw, &lmh_list, lw_list) {
252                 if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
253                         found = 1;
254                         break;
255                 }
256         }
257
258         if (!found) {
259                 if (!try && capable(CAP_SYS_MODULE)) {
260                         try = 1;
261                         mutex_unlock(&lmh_lock);
262                         request_module(proto_name);
263                         goto retry;
264                 }
265                 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
266                 error = -ENOENT;
267                 goto out;
268         }
269
270         if (lw->lw_ops->lm_owner &&
271             !try_module_get(lw->lw_ops->lm_owner)) {
272                 try = 0;
273                 mutex_unlock(&lmh_lock);
274                 msleep(1000);
275                 goto retry;
276         }
277
278         error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data,
279                                      min_lvb_size, flags, lockstruct, fskobj);
280         if (error)
281                 module_put(lw->lw_ops->lm_owner);
282 out:
283         mutex_unlock(&lmh_lock);
284         return error;
285 }
286
287 void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
288 {
289         mutex_lock(&lmh_lock);
290         if (lockstruct->ls_ops->lm_unmount)
291                 lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
292         if (lockstruct->ls_ops->lm_owner)
293                 module_put(lockstruct->ls_ops->lm_owner);
294         mutex_unlock(&lmh_lock);
295 }
296
297 /**
298  * gfs2_withdraw_lockproto - abnormally unmount a lock module
299  * @lockstruct: the lockstruct passed into mount
300  *
301  */
302
303 void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
304 {
305         mutex_lock(&lmh_lock);
306         lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
307         if (lockstruct->ls_ops->lm_owner)
308                 module_put(lockstruct->ls_ops->lm_owner);
309         mutex_unlock(&lmh_lock);
310 }
311
312 EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
313 EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);
314