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