2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
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>
18 #include <linux/delay.h>
19 #include <linux/lm_interface.h>
22 struct list_head lw_list;
23 const struct lm_lockops *lw_ops;
26 struct nolock_lockspace {
27 unsigned int nl_lvb_size;
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
36 * Returns: 0 on success, -EXXX on failure
39 static int nolock_get_lock(void *lockspace, struct lm_lockname *name,
47 * nolock_put_lock - get rid of a lock structure
48 * @lock: the lock to throw away
52 static void nolock_put_lock(void *lock)
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
61 * Returns: 0 on success, -EXXX on failure
64 static int nolock_hold_lvb(void *lock, char **lvbp)
66 struct nolock_lockspace *nl = lock;
69 *lvbp = kzalloc(nl->nl_lvb_size, GFP_KERNEL);
77 * nolock_unhold_lvb - release a LVB
78 * @lock: the lock the LVB is associated with
79 * @lvb: the lock value block
83 static void nolock_unhold_lvb(void *lock, char *lvb)
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);
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. */
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,
108 static struct lmh_wrapper nolock_proto = {
109 .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list),
110 .lw_ops = &nolock_ops,
113 static LIST_HEAD(lmh_list);
114 static DEFINE_MUTEX(lmh_lock);
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)
124 struct nolock_lockspace *nl;
126 c = strstr(host_data, "jid=");
131 sscanf(c, "%u", &jid);
134 nl = kzalloc(sizeof(struct nolock_lockspace), GFP_KERNEL);
138 nl->nl_lvb_size = min_lvb_size;
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;
150 static void nolock_unmount(void *lockspace)
152 struct nolock_lockspace *nl = lockspace;
157 * gfs2_register_lockproto - Register a low-level locking protocol
158 * @proto: the protocol definition
160 * Returns: 0 on success, -EXXX on failure
163 int gfs2_register_lockproto(const struct lm_lockops *proto)
165 struct lmh_wrapper *lw;
167 mutex_lock(&lmh_lock);
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);
178 lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
180 mutex_unlock(&lmh_lock);
185 list_add(&lw->lw_list, &lmh_list);
187 mutex_unlock(&lmh_lock);
193 * gfs2_unregister_lockproto - Unregister a low-level locking protocol
194 * @proto: the protocol definition
198 void gfs2_unregister_lockproto(const struct lm_lockops *proto)
200 struct lmh_wrapper *lw;
202 mutex_lock(&lmh_lock);
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);
213 mutex_unlock(&lmh_lock);
215 printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
216 proto->lm_proto_name);
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
230 * Returns: 0 on success, -EXXX on failure
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)
239 struct lmh_wrapper *lw = NULL;
245 mutex_lock(&lmh_lock);
247 if (list_empty(&nolock_proto.lw_list))
248 list_add(&nolock_proto.lw_list, &lmh_list);
251 list_for_each_entry(lw, &lmh_list, lw_list) {
252 if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
259 if (!try && capable(CAP_SYS_MODULE)) {
261 mutex_unlock(&lmh_lock);
262 request_module(proto_name);
265 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
270 if (lw->lw_ops->lm_owner &&
271 !try_module_get(lw->lw_ops->lm_owner)) {
273 mutex_unlock(&lmh_lock);
278 error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data,
279 min_lvb_size, flags, lockstruct, fskobj);
281 module_put(lw->lw_ops->lm_owner);
283 mutex_unlock(&lmh_lock);
287 void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
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);
298 * gfs2_withdraw_lockproto - abnormally unmount a lock module
299 * @lockstruct: the lockstruct passed into mount
303 void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
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);
312 EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
313 EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);