David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Red Hat, Inc. All rights reserved. |
| 3 | * |
| 4 | * This copyrighted material is made available to anyone wishing to use, |
| 5 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 6 | * of the GNU General Public License version 2. |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/miscdevice.h> |
| 10 | #include <linux/lock_dlm_plock.h> |
Al Viro | bd01f84 | 2006-10-19 17:23:57 -0400 | [diff] [blame^] | 11 | #include <linux/poll.h> |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 12 | |
| 13 | #include "lock_dlm.h" |
| 14 | |
| 15 | |
| 16 | static spinlock_t ops_lock; |
| 17 | static struct list_head send_list; |
| 18 | static struct list_head recv_list; |
| 19 | static wait_queue_head_t send_wq; |
| 20 | static wait_queue_head_t recv_wq; |
| 21 | |
| 22 | struct plock_op { |
| 23 | struct list_head list; |
| 24 | int done; |
| 25 | struct gdlm_plock_info info; |
| 26 | }; |
| 27 | |
| 28 | static inline void set_version(struct gdlm_plock_info *info) |
| 29 | { |
| 30 | info->version[0] = GDLM_PLOCK_VERSION_MAJOR; |
| 31 | info->version[1] = GDLM_PLOCK_VERSION_MINOR; |
| 32 | info->version[2] = GDLM_PLOCK_VERSION_PATCH; |
| 33 | } |
| 34 | |
| 35 | static int check_version(struct gdlm_plock_info *info) |
| 36 | { |
| 37 | if ((GDLM_PLOCK_VERSION_MAJOR != info->version[0]) || |
| 38 | (GDLM_PLOCK_VERSION_MINOR < info->version[1])) { |
| 39 | log_error("plock device version mismatch: " |
| 40 | "kernel (%u.%u.%u), user (%u.%u.%u)", |
| 41 | GDLM_PLOCK_VERSION_MAJOR, |
| 42 | GDLM_PLOCK_VERSION_MINOR, |
| 43 | GDLM_PLOCK_VERSION_PATCH, |
| 44 | info->version[0], |
| 45 | info->version[1], |
| 46 | info->version[2]); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static void send_op(struct plock_op *op) |
| 53 | { |
| 54 | set_version(&op->info); |
| 55 | INIT_LIST_HEAD(&op->list); |
| 56 | spin_lock(&ops_lock); |
| 57 | list_add_tail(&op->list, &send_list); |
| 58 | spin_unlock(&ops_lock); |
| 59 | wake_up(&send_wq); |
| 60 | } |
| 61 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 62 | int gdlm_plock(void *lockspace, struct lm_lockname *name, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 63 | struct file *file, int cmd, struct file_lock *fl) |
| 64 | { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 65 | struct gdlm_ls *ls = lockspace; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 66 | struct plock_op *op; |
| 67 | int rv; |
| 68 | |
| 69 | op = kzalloc(sizeof(*op), GFP_KERNEL); |
| 70 | if (!op) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | op->info.optype = GDLM_PLOCK_OP_LOCK; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 74 | op->info.pid = fl->fl_pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 75 | op->info.ex = (fl->fl_type == F_WRLCK); |
| 76 | op->info.wait = IS_SETLKW(cmd); |
| 77 | op->info.fsid = ls->id; |
| 78 | op->info.number = name->ln_number; |
| 79 | op->info.start = fl->fl_start; |
| 80 | op->info.end = fl->fl_end; |
David Teigland | de9b75d | 2006-07-28 14:00:20 -0500 | [diff] [blame] | 81 | op->info.owner = (__u64)(long) fl->fl_owner; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 82 | |
| 83 | send_op(op); |
| 84 | wait_event(recv_wq, (op->done != 0)); |
| 85 | |
| 86 | spin_lock(&ops_lock); |
| 87 | if (!list_empty(&op->list)) { |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 88 | printk(KERN_INFO "plock op on list\n"); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 89 | list_del(&op->list); |
| 90 | } |
| 91 | spin_unlock(&ops_lock); |
| 92 | |
| 93 | rv = op->info.rv; |
| 94 | |
| 95 | if (!rv) { |
| 96 | if (posix_lock_file_wait(file, fl) < 0) |
| 97 | log_error("gdlm_plock: vfs lock error %x,%llx", |
David Teigland | 9229f01 | 2006-05-24 09:21:30 -0400 | [diff] [blame] | 98 | name->ln_type, |
| 99 | (unsigned long long)name->ln_number); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | kfree(op); |
| 103 | return rv; |
| 104 | } |
| 105 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 106 | int gdlm_punlock(void *lockspace, struct lm_lockname *name, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 107 | struct file *file, struct file_lock *fl) |
| 108 | { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 109 | struct gdlm_ls *ls = lockspace; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 110 | struct plock_op *op; |
| 111 | int rv; |
| 112 | |
| 113 | op = kzalloc(sizeof(*op), GFP_KERNEL); |
| 114 | if (!op) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | if (posix_lock_file_wait(file, fl) < 0) |
| 118 | log_error("gdlm_punlock: vfs unlock error %x,%llx", |
David Teigland | 9229f01 | 2006-05-24 09:21:30 -0400 | [diff] [blame] | 119 | name->ln_type, (unsigned long long)name->ln_number); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 120 | |
| 121 | op->info.optype = GDLM_PLOCK_OP_UNLOCK; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 122 | op->info.pid = fl->fl_pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 123 | op->info.fsid = ls->id; |
| 124 | op->info.number = name->ln_number; |
| 125 | op->info.start = fl->fl_start; |
| 126 | op->info.end = fl->fl_end; |
David Teigland | de9b75d | 2006-07-28 14:00:20 -0500 | [diff] [blame] | 127 | op->info.owner = (__u64)(long) fl->fl_owner; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 128 | |
| 129 | send_op(op); |
| 130 | wait_event(recv_wq, (op->done != 0)); |
| 131 | |
| 132 | spin_lock(&ops_lock); |
| 133 | if (!list_empty(&op->list)) { |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 134 | printk(KERN_INFO "punlock op on list\n"); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 135 | list_del(&op->list); |
| 136 | } |
| 137 | spin_unlock(&ops_lock); |
| 138 | |
| 139 | rv = op->info.rv; |
| 140 | |
| 141 | kfree(op); |
| 142 | return rv; |
| 143 | } |
| 144 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 145 | int gdlm_plock_get(void *lockspace, struct lm_lockname *name, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 146 | struct file *file, struct file_lock *fl) |
| 147 | { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 148 | struct gdlm_ls *ls = lockspace; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 149 | struct plock_op *op; |
| 150 | int rv; |
| 151 | |
| 152 | op = kzalloc(sizeof(*op), GFP_KERNEL); |
| 153 | if (!op) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | op->info.optype = GDLM_PLOCK_OP_GET; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 157 | op->info.pid = fl->fl_pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 158 | op->info.ex = (fl->fl_type == F_WRLCK); |
| 159 | op->info.fsid = ls->id; |
| 160 | op->info.number = name->ln_number; |
| 161 | op->info.start = fl->fl_start; |
| 162 | op->info.end = fl->fl_end; |
| 163 | |
| 164 | send_op(op); |
| 165 | wait_event(recv_wq, (op->done != 0)); |
| 166 | |
| 167 | spin_lock(&ops_lock); |
| 168 | if (!list_empty(&op->list)) { |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 169 | printk(KERN_INFO "plock_get op on list\n"); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 170 | list_del(&op->list); |
| 171 | } |
| 172 | spin_unlock(&ops_lock); |
| 173 | |
| 174 | rv = op->info.rv; |
| 175 | |
| 176 | if (rv == 0) |
| 177 | fl->fl_type = F_UNLCK; |
| 178 | else if (rv > 0) { |
| 179 | fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK; |
| 180 | fl->fl_pid = op->info.pid; |
| 181 | fl->fl_start = op->info.start; |
| 182 | fl->fl_end = op->info.end; |
| 183 | } |
| 184 | |
| 185 | kfree(op); |
| 186 | return rv; |
| 187 | } |
| 188 | |
| 189 | /* a read copies out one plock request from the send list */ |
| 190 | static ssize_t dev_read(struct file *file, char __user *u, size_t count, |
| 191 | loff_t *ppos) |
| 192 | { |
| 193 | struct gdlm_plock_info info; |
| 194 | struct plock_op *op = NULL; |
| 195 | |
| 196 | if (count < sizeof(info)) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | spin_lock(&ops_lock); |
| 200 | if (!list_empty(&send_list)) { |
| 201 | op = list_entry(send_list.next, struct plock_op, list); |
| 202 | list_move(&op->list, &recv_list); |
| 203 | memcpy(&info, &op->info, sizeof(info)); |
| 204 | } |
| 205 | spin_unlock(&ops_lock); |
| 206 | |
| 207 | if (!op) |
| 208 | return -EAGAIN; |
| 209 | |
| 210 | if (copy_to_user(u, &info, sizeof(info))) |
| 211 | return -EFAULT; |
| 212 | return sizeof(info); |
| 213 | } |
| 214 | |
| 215 | /* a write copies in one plock result that should match a plock_op |
| 216 | on the recv list */ |
| 217 | static ssize_t dev_write(struct file *file, const char __user *u, size_t count, |
| 218 | loff_t *ppos) |
| 219 | { |
| 220 | struct gdlm_plock_info info; |
| 221 | struct plock_op *op; |
| 222 | int found = 0; |
| 223 | |
| 224 | if (count != sizeof(info)) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | if (copy_from_user(&info, u, sizeof(info))) |
| 228 | return -EFAULT; |
| 229 | |
| 230 | if (check_version(&info)) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | spin_lock(&ops_lock); |
| 234 | list_for_each_entry(op, &recv_list, list) { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 235 | if (op->info.fsid == info.fsid && op->info.number == info.number && |
David Teigland | 08eac93 | 2006-08-04 16:19:20 -0500 | [diff] [blame] | 236 | op->info.owner == info.owner) { |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 237 | list_del_init(&op->list); |
| 238 | found = 1; |
| 239 | op->done = 1; |
| 240 | memcpy(&op->info, &info, sizeof(info)); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | spin_unlock(&ops_lock); |
| 245 | |
| 246 | if (found) |
| 247 | wake_up(&recv_wq); |
| 248 | else |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 249 | printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid, |
David Teigland | 9229f01 | 2006-05-24 09:21:30 -0400 | [diff] [blame] | 250 | (unsigned long long)info.number); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 251 | return count; |
| 252 | } |
| 253 | |
| 254 | static unsigned int dev_poll(struct file *file, poll_table *wait) |
| 255 | { |
| 256 | poll_wait(file, &send_wq, wait); |
| 257 | |
| 258 | spin_lock(&ops_lock); |
| 259 | if (!list_empty(&send_list)) { |
| 260 | spin_unlock(&ops_lock); |
| 261 | return POLLIN | POLLRDNORM; |
| 262 | } |
| 263 | spin_unlock(&ops_lock); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static struct file_operations dev_fops = { |
| 268 | .read = dev_read, |
| 269 | .write = dev_write, |
| 270 | .poll = dev_poll, |
| 271 | .owner = THIS_MODULE |
| 272 | }; |
| 273 | |
| 274 | static struct miscdevice plock_dev_misc = { |
| 275 | .minor = MISC_DYNAMIC_MINOR, |
| 276 | .name = GDLM_PLOCK_MISC_NAME, |
| 277 | .fops = &dev_fops |
| 278 | }; |
| 279 | |
| 280 | int gdlm_plock_init(void) |
| 281 | { |
| 282 | int rv; |
| 283 | |
| 284 | spin_lock_init(&ops_lock); |
| 285 | INIT_LIST_HEAD(&send_list); |
| 286 | INIT_LIST_HEAD(&recv_list); |
| 287 | init_waitqueue_head(&send_wq); |
| 288 | init_waitqueue_head(&recv_wq); |
| 289 | |
| 290 | rv = misc_register(&plock_dev_misc); |
| 291 | if (rv) |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 292 | printk(KERN_INFO "gdlm_plock_init: misc_register failed %d", |
| 293 | rv); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 294 | return rv; |
| 295 | } |
| 296 | |
| 297 | void gdlm_plock_exit(void) |
| 298 | { |
| 299 | if (misc_deregister(&plock_dev_misc) < 0) |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 300 | printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed"); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 301 | } |
| 302 | |