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> |
| 19 | |
| 20 | #include "lm_interface.h" |
| 21 | |
| 22 | struct lmh_wrapper { |
| 23 | struct list_head lw_list; |
| 24 | struct lm_lockops *lw_ops; |
| 25 | }; |
| 26 | |
| 27 | /* List of registered low-level locking protocols. A file system selects one |
| 28 | of them by name at mount time, e.g. lock_nolock, lock_dlm. */ |
| 29 | |
| 30 | static struct list_head lmh_list; |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 31 | static struct mutex lmh_lock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 34 | * gfs2_register_lockproto - Register a low-level locking protocol |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | * @proto: the protocol definition |
| 36 | * |
| 37 | * Returns: 0 on success, -EXXX on failure |
| 38 | */ |
| 39 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 40 | int gfs2_register_lockproto(struct lm_lockops *proto) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | { |
| 42 | struct lmh_wrapper *lw; |
| 43 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 44 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 45 | |
| 46 | list_for_each_entry(lw, &lmh_list, lw_list) { |
| 47 | if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 48 | mutex_unlock(&lmh_lock); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 49 | printk(KERN_INFO "GFS2: protocol %s already exists\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 50 | proto->lm_proto_name); |
| 51 | return -EEXIST; |
| 52 | } |
| 53 | } |
| 54 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 55 | lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | if (!lw) { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 57 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | return -ENOMEM; |
| 59 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 60 | |
| 61 | lw->lw_ops = proto; |
| 62 | list_add(&lw->lw_list, &lmh_list); |
| 63 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 64 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /** |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 70 | * gfs2_unregister_lockproto - Unregister a low-level locking protocol |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 71 | * @proto: the protocol definition |
| 72 | * |
| 73 | */ |
| 74 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 75 | void gfs2_unregister_lockproto(struct lm_lockops *proto) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 76 | { |
| 77 | struct lmh_wrapper *lw; |
| 78 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 79 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 80 | |
| 81 | list_for_each_entry(lw, &lmh_list, lw_list) { |
| 82 | if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) { |
| 83 | list_del(&lw->lw_list); |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 84 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 85 | kfree(lw); |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 90 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 91 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 92 | printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 93 | proto->lm_proto_name); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * gfs2_mount_lockproto - Mount a lock protocol |
| 98 | * @proto_name - the name of the protocol |
| 99 | * @table_name - the name of the lock space |
| 100 | * @host_data - data specific to this host |
| 101 | * @cb - the callback to the code using the lock module |
| 102 | * @fsdata - data to pass back with the callback |
| 103 | * @min_lvb_size - the mininum LVB size that the caller can deal with |
| 104 | * @flags - LM_MFLAG_* |
| 105 | * @lockstruct - a structure returned describing the mount |
| 106 | * |
| 107 | * Returns: 0 on success, -EXXX on failure |
| 108 | */ |
| 109 | |
| 110 | int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data, |
| 111 | lm_callback_t cb, lm_fsdata_t *fsdata, |
| 112 | unsigned int min_lvb_size, int flags, |
| 113 | struct lm_lockstruct *lockstruct, |
| 114 | struct kobject *fskobj) |
| 115 | { |
| 116 | struct lmh_wrapper *lw = NULL; |
| 117 | int try = 0; |
| 118 | int error, found; |
| 119 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 120 | retry: |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 121 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 122 | |
| 123 | found = 0; |
| 124 | list_for_each_entry(lw, &lmh_list, lw_list) { |
| 125 | if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) { |
| 126 | found = 1; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!found) { |
| 132 | if (!try && capable(CAP_SYS_MODULE)) { |
| 133 | try = 1; |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 134 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | request_module(proto_name); |
| 136 | goto retry; |
| 137 | } |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 138 | printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | error = -ENOENT; |
| 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | if (!try_module_get(lw->lw_ops->lm_owner)) { |
| 144 | try = 0; |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 145 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 146 | msleep(1000); |
| 147 | goto retry; |
| 148 | } |
| 149 | |
| 150 | error = lw->lw_ops->lm_mount(table_name, host_data, cb, fsdata, |
| 151 | min_lvb_size, flags, lockstruct, fskobj); |
| 152 | if (error) |
| 153 | module_put(lw->lw_ops->lm_owner); |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 154 | out: |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 155 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | return error; |
| 157 | } |
| 158 | |
| 159 | void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct) |
| 160 | { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 161 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 162 | lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace); |
| 163 | if (lockstruct->ls_ops->lm_owner) |
| 164 | module_put(lockstruct->ls_ops->lm_owner); |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 165 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * gfs2_withdraw_lockproto - abnormally unmount a lock module |
| 170 | * @lockstruct: the lockstruct passed into mount |
| 171 | * |
| 172 | */ |
| 173 | |
| 174 | void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct) |
| 175 | { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 176 | mutex_lock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 177 | lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace); |
| 178 | if (lockstruct->ls_ops->lm_owner) |
| 179 | module_put(lockstruct->ls_ops->lm_owner); |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 180 | mutex_unlock(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void __init gfs2_init_lmh(void) |
| 184 | { |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 185 | mutex_init(&lmh_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | INIT_LIST_HEAD(&lmh_list); |
| 187 | } |
| 188 | |
Steven Whitehouse | 2b557f6 | 2006-08-07 11:12:30 -0400 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(gfs2_register_lockproto); |
| 190 | EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 191 | |