blob: 523243a13a2184a0fcbe02ecc10df012af3c77f8 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020019#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000020
21struct lmh_wrapper {
22 struct list_head lw_list;
Steven Whitehouse9b47c112006-09-08 10:17:58 -040023 const struct lm_lockops *lw_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +000024};
25
Steven Whitehouse048bca22008-05-23 14:46:04 +010026static int nolock_mount(char *table_name, char *host_data,
27 lm_callback_t cb, void *cb_data,
28 unsigned int min_lvb_size, int flags,
29 struct lm_lockstruct *lockstruct,
30 struct kobject *fskobj);
31
David Teiglandb3b94fa2006-01-16 16:50:04 +000032/* List of registered low-level locking protocols. A file system selects one
33 of them by name at mount time, e.g. lock_nolock, lock_dlm. */
34
Steven Whitehouse048bca22008-05-23 14:46:04 +010035static const struct lm_lockops nolock_ops = {
36 .lm_proto_name = "lock_nolock",
37 .lm_mount = nolock_mount,
38};
39
40static struct lmh_wrapper nolock_proto = {
41 .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list),
42 .lw_ops = &nolock_ops,
43};
44
Steven Whitehouse50299962006-09-04 09:49:55 -040045static LIST_HEAD(lmh_list);
46static DEFINE_MUTEX(lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000047
Steven Whitehouse048bca22008-05-23 14:46:04 +010048static int nolock_mount(char *table_name, char *host_data,
49 lm_callback_t cb, void *cb_data,
50 unsigned int min_lvb_size, int flags,
51 struct lm_lockstruct *lockstruct,
52 struct kobject *fskobj)
53{
54 char *c;
55 unsigned int jid;
56
57 c = strstr(host_data, "jid=");
58 if (!c)
59 jid = 0;
60 else {
61 c += 4;
62 sscanf(c, "%u", &jid);
63 }
64
65 lockstruct->ls_jid = jid;
66 lockstruct->ls_first = 1;
67 lockstruct->ls_lvb_size = min_lvb_size;
68 lockstruct->ls_ops = &nolock_ops;
69 lockstruct->ls_flags = LM_LSFLAG_LOCAL;
70
71 return 0;
72}
73
David Teiglandb3b94fa2006-01-16 16:50:04 +000074/**
Steven Whitehouse2b557f62006-08-07 11:12:30 -040075 * gfs2_register_lockproto - Register a low-level locking protocol
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 * @proto: the protocol definition
77 *
78 * Returns: 0 on success, -EXXX on failure
79 */
80
Steven Whitehouse9b47c112006-09-08 10:17:58 -040081int gfs2_register_lockproto(const struct lm_lockops *proto)
David Teiglandb3b94fa2006-01-16 16:50:04 +000082{
83 struct lmh_wrapper *lw;
84
Steven Whitehousea74604b2006-04-21 15:10:46 -040085 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086
87 list_for_each_entry(lw, &lmh_list, lw_list) {
88 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
Steven Whitehousea74604b2006-04-21 15:10:46 -040089 mutex_unlock(&lmh_lock);
Steven Whitehoused92a8d42006-02-27 10:57:14 -050090 printk(KERN_INFO "GFS2: protocol %s already exists\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 proto->lm_proto_name);
92 return -EEXIST;
93 }
94 }
95
Steven Whitehoused92a8d42006-02-27 10:57:14 -050096 lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000097 if (!lw) {
Steven Whitehousea74604b2006-04-21 15:10:46 -040098 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 return -ENOMEM;
100 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101
102 lw->lw_ops = proto;
103 list_add(&lw->lw_list, &lmh_list);
104
Steven Whitehousea74604b2006-04-21 15:10:46 -0400105 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106
107 return 0;
108}
109
110/**
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400111 * gfs2_unregister_lockproto - Unregister a low-level locking protocol
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 * @proto: the protocol definition
113 *
114 */
115
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400116void gfs2_unregister_lockproto(const struct lm_lockops *proto)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117{
118 struct lmh_wrapper *lw;
119
Steven Whitehousea74604b2006-04-21 15:10:46 -0400120 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121
122 list_for_each_entry(lw, &lmh_list, lw_list) {
123 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
124 list_del(&lw->lw_list);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400125 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 kfree(lw);
127 return;
128 }
129 }
130
Steven Whitehousea74604b2006-04-21 15:10:46 -0400131 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500133 printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 proto->lm_proto_name);
135}
136
137/**
138 * gfs2_mount_lockproto - Mount a lock protocol
139 * @proto_name - the name of the protocol
140 * @table_name - the name of the lock space
141 * @host_data - data specific to this host
142 * @cb - the callback to the code using the lock module
Steven Whitehouse1c089c32006-09-07 15:50:20 -0400143 * @sdp - The GFS2 superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 * @min_lvb_size - the mininum LVB size that the caller can deal with
145 * @flags - LM_MFLAG_*
146 * @lockstruct - a structure returned describing the mount
147 *
148 * Returns: 0 on success, -EXXX on failure
149 */
150
151int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data,
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400152 lm_callback_t cb, void *cb_data,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 unsigned int min_lvb_size, int flags,
154 struct lm_lockstruct *lockstruct,
155 struct kobject *fskobj)
156{
157 struct lmh_wrapper *lw = NULL;
158 int try = 0;
159 int error, found;
160
Steven Whitehouse048bca22008-05-23 14:46:04 +0100161
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400162retry:
Steven Whitehousea74604b2006-04-21 15:10:46 -0400163 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
Steven Whitehouse048bca22008-05-23 14:46:04 +0100165 if (list_empty(&nolock_proto.lw_list))
Steven Whitehouse80274732008-06-02 09:08:47 +0100166 list_add(&nolock_proto.lw_list, &lmh_list);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100167
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 found = 0;
169 list_for_each_entry(lw, &lmh_list, lw_list) {
170 if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
171 found = 1;
172 break;
173 }
174 }
175
176 if (!found) {
177 if (!try && capable(CAP_SYS_MODULE)) {
178 try = 1;
Steven Whitehousea74604b2006-04-21 15:10:46 -0400179 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 request_module(proto_name);
181 goto retry;
182 }
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500183 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 error = -ENOENT;
185 goto out;
186 }
187
Steven Whitehouse048bca22008-05-23 14:46:04 +0100188 if (lw->lw_ops->lm_owner &&
189 !try_module_get(lw->lw_ops->lm_owner)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 try = 0;
Steven Whitehousea74604b2006-04-21 15:10:46 -0400191 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192 msleep(1000);
193 goto retry;
194 }
195
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400196 error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 min_lvb_size, flags, lockstruct, fskobj);
198 if (error)
199 module_put(lw->lw_ops->lm_owner);
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400200out:
Steven Whitehousea74604b2006-04-21 15:10:46 -0400201 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 return error;
203}
204
205void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
206{
Steven Whitehousea74604b2006-04-21 15:10:46 -0400207 mutex_lock(&lmh_lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100208 if (lockstruct->ls_ops->lm_unmount)
209 lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 if (lockstruct->ls_ops->lm_owner)
211 module_put(lockstruct->ls_ops->lm_owner);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400212 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213}
214
215/**
216 * gfs2_withdraw_lockproto - abnormally unmount a lock module
217 * @lockstruct: the lockstruct passed into mount
218 *
219 */
220
221void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
222{
Steven Whitehousea74604b2006-04-21 15:10:46 -0400223 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
225 if (lockstruct->ls_ops->lm_owner)
226 module_put(lockstruct->ls_ops->lm_owner);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400227 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228}
229
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400230EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
231EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232