David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 19 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | |
| 21 | struct lmh_wrapper { |
| 22 | struct list_head lw_list; |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 23 | const struct lm_lockops *lw_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 26 | static 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | /* 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 Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 35 | static const struct lm_lockops nolock_ops = { |
| 36 | .lm_proto_name = "lock_nolock", |
| 37 | .lm_mount = nolock_mount, |
| 38 | }; |
| 39 | |
| 40 | static struct lmh_wrapper nolock_proto = { |
| 41 | .lw_list = LIST_HEAD_INIT(nolock_proto.lw_list), |
| 42 | .lw_ops = &nolock_ops, |
| 43 | }; |
| 44 | |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 45 | static LIST_HEAD(lmh_list); |
| 46 | static DEFINE_MUTEX(lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 47 | |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 48 | static 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 74 | /** |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 75 | * gfs2_register_lockproto - Register a low-level locking protocol |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 76 | * @proto: the protocol definition |
| 77 | * |
| 78 | * Returns: 0 on success, -EXXX on failure |
| 79 | */ |
| 80 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 81 | int gfs2_register_lockproto(const struct lm_lockops *proto) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 82 | { |
| 83 | struct lmh_wrapper *lw; |
| 84 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 85 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 86 | |
| 87 | list_for_each_entry(lw, &lmh_list, lw_list) { |
| 88 | if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 89 | mutex_unlock(&lmh_lock); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 90 | printk(KERN_INFO "GFS2: protocol %s already exists\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 91 | proto->lm_proto_name); |
| 92 | return -EEXIST; |
| 93 | } |
| 94 | } |
| 95 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 96 | lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 97 | if (!lw) { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 98 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 99 | return -ENOMEM; |
| 100 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 101 | |
| 102 | lw->lw_ops = proto; |
| 103 | list_add(&lw->lw_list, &lmh_list); |
| 104 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 105 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /** |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 111 | * gfs2_unregister_lockproto - Unregister a low-level locking protocol |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 112 | * @proto: the protocol definition |
| 113 | * |
| 114 | */ |
| 115 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 116 | void gfs2_unregister_lockproto(const struct lm_lockops *proto) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | { |
| 118 | struct lmh_wrapper *lw; |
| 119 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 120 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | |
| 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 Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 125 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | kfree(lw); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 131 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 132 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 133 | printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 134 | 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 Whitehouse | 1c089c3 | 2006-09-07 15:50:20 -0400 | [diff] [blame] | 143 | * @sdp - The GFS2 superblock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 144 | * @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 | |
| 151 | int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data, |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 152 | lm_callback_t cb, void *cb_data, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 153 | 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 Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 161 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 162 | retry: |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 163 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 164 | |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 165 | if (list_empty(&nolock_proto.lw_list)) |
Steven Whitehouse | 8027473 | 2008-06-02 09:08:47 +0100 | [diff] [blame] | 166 | list_add(&nolock_proto.lw_list, &lmh_list); |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 167 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 168 | 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 Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 179 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 180 | request_module(proto_name); |
| 181 | goto retry; |
| 182 | } |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 183 | printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 184 | error = -ENOENT; |
| 185 | goto out; |
| 186 | } |
| 187 | |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 188 | if (lw->lw_ops->lm_owner && |
| 189 | !try_module_get(lw->lw_ops->lm_owner)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | try = 0; |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 191 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 192 | msleep(1000); |
| 193 | goto retry; |
| 194 | } |
| 195 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 196 | error = lw->lw_ops->lm_mount(table_name, host_data, cb, cb_data, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 197 | min_lvb_size, flags, lockstruct, fskobj); |
| 198 | if (error) |
| 199 | module_put(lw->lw_ops->lm_owner); |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 200 | out: |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 201 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | return error; |
| 203 | } |
| 204 | |
| 205 | void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct) |
| 206 | { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 207 | mutex_lock(&lmh_lock); |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 208 | if (lockstruct->ls_ops->lm_unmount) |
| 209 | lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 210 | if (lockstruct->ls_ops->lm_owner) |
| 211 | module_put(lockstruct->ls_ops->lm_owner); |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 212 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * gfs2_withdraw_lockproto - abnormally unmount a lock module |
| 217 | * @lockstruct: the lockstruct passed into mount |
| 218 | * |
| 219 | */ |
| 220 | |
| 221 | void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct) |
| 222 | { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 223 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace); |
| 225 | if (lockstruct->ls_ops->lm_owner) |
| 226 | module_put(lockstruct->ls_ops->lm_owner); |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 227 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(gfs2_register_lockproto); |
| 231 | EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 232 | |