blob: fba1f1d87e4fbe92cceb66fd44eb7ee40a8a6d84 [file] [log] [blame]
David Teigland869d81d2006-01-17 08:47:12 +00001/*
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 Whitehousee9fc2aa2006-09-01 11:05:15 -04006 * of the GNU General Public License version 2.
David Teigland869d81d2006-01-17 08:47:12 +00007 */
8
9#include <linux/miscdevice.h>
10#include <linux/lock_dlm_plock.h>
Al Virobd01f842006-10-19 17:23:57 -040011#include <linux/poll.h>
David Teigland869d81d2006-01-17 08:47:12 +000012
13#include "lock_dlm.h"
14
15
16static spinlock_t ops_lock;
17static struct list_head send_list;
18static struct list_head recv_list;
19static wait_queue_head_t send_wq;
20static wait_queue_head_t recv_wq;
21
22struct plock_op {
23 struct list_head list;
24 int done;
25 struct gdlm_plock_info info;
26};
27
Marc Eshel586759f2006-11-14 16:37:25 -050028struct plock_xop {
29 struct plock_op xop;
30 void *callback;
31 void *fl;
32 void *file;
33 struct file_lock flc;
34};
35
36
David Teigland869d81d2006-01-17 08:47:12 +000037static inline void set_version(struct gdlm_plock_info *info)
38{
39 info->version[0] = GDLM_PLOCK_VERSION_MAJOR;
40 info->version[1] = GDLM_PLOCK_VERSION_MINOR;
41 info->version[2] = GDLM_PLOCK_VERSION_PATCH;
42}
43
44static int check_version(struct gdlm_plock_info *info)
45{
46 if ((GDLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
47 (GDLM_PLOCK_VERSION_MINOR < info->version[1])) {
48 log_error("plock device version mismatch: "
49 "kernel (%u.%u.%u), user (%u.%u.%u)",
50 GDLM_PLOCK_VERSION_MAJOR,
51 GDLM_PLOCK_VERSION_MINOR,
52 GDLM_PLOCK_VERSION_PATCH,
53 info->version[0],
54 info->version[1],
55 info->version[2]);
56 return -EINVAL;
57 }
58 return 0;
59}
60
61static void send_op(struct plock_op *op)
62{
63 set_version(&op->info);
64 INIT_LIST_HEAD(&op->list);
65 spin_lock(&ops_lock);
66 list_add_tail(&op->list, &send_list);
67 spin_unlock(&ops_lock);
68 wake_up(&send_wq);
69}
70
Steven Whitehouse9b47c112006-09-08 10:17:58 -040071int gdlm_plock(void *lockspace, struct lm_lockname *name,
David Teigland869d81d2006-01-17 08:47:12 +000072 struct file *file, int cmd, struct file_lock *fl)
73{
Steven Whitehouse9b47c112006-09-08 10:17:58 -040074 struct gdlm_ls *ls = lockspace;
David Teigland869d81d2006-01-17 08:47:12 +000075 struct plock_op *op;
Marc Eshel586759f2006-11-14 16:37:25 -050076 struct plock_xop *xop;
David Teigland869d81d2006-01-17 08:47:12 +000077 int rv;
78
Marc Eshel586759f2006-11-14 16:37:25 -050079 xop = kzalloc(sizeof(*xop), GFP_KERNEL);
80 if (!xop)
David Teigland869d81d2006-01-17 08:47:12 +000081 return -ENOMEM;
82
Marc Eshel586759f2006-11-14 16:37:25 -050083 op = &xop->xop;
David Teigland869d81d2006-01-17 08:47:12 +000084 op->info.optype = GDLM_PLOCK_OP_LOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -040085 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +000086 op->info.ex = (fl->fl_type == F_WRLCK);
87 op->info.wait = IS_SETLKW(cmd);
88 op->info.fsid = ls->id;
89 op->info.number = name->ln_number;
90 op->info.start = fl->fl_start;
91 op->info.end = fl->fl_end;
David Teiglandde9b75d2006-07-28 14:00:20 -050092 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -050093 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
94 xop->callback = fl->fl_lmops->fl_grant;
95 locks_init_lock(&xop->flc);
96 locks_copy_lock(&xop->flc, fl);
97 xop->fl = fl;
98 xop->file = file;
99 } else
100 xop->callback = NULL;
David Teigland869d81d2006-01-17 08:47:12 +0000101
102 send_op(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500103
104 if (xop->callback == NULL)
105 wait_event(recv_wq, (op->done != 0));
106 else
107 return -EINPROGRESS;
David Teigland869d81d2006-01-17 08:47:12 +0000108
109 spin_lock(&ops_lock);
110 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500111 printk(KERN_INFO "plock op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +0000112 list_del(&op->list);
113 }
114 spin_unlock(&ops_lock);
115
116 rv = op->info.rv;
117
118 if (!rv) {
119 if (posix_lock_file_wait(file, fl) < 0)
120 log_error("gdlm_plock: vfs lock error %x,%llx",
David Teigland9229f012006-05-24 09:21:30 -0400121 name->ln_type,
122 (unsigned long long)name->ln_number);
David Teigland869d81d2006-01-17 08:47:12 +0000123 }
124
Marc Eshel586759f2006-11-14 16:37:25 -0500125 kfree(xop);
126 return rv;
127}
128
129/* Returns failure iff a succesful lock operation should be canceled */
130static int gdlm_plock_callback(struct plock_op *op)
131{
132 struct file *file;
133 struct file_lock *fl;
134 struct file_lock *flc;
135 int (*notify)(void *, void *, int) = NULL;
136 struct plock_xop *xop = (struct plock_xop *)op;
137 int rv = 0;
138
139 spin_lock(&ops_lock);
140 if (!list_empty(&op->list)) {
141 printk(KERN_INFO "plock op on list\n");
142 list_del(&op->list);
143 }
144 spin_unlock(&ops_lock);
145
146 /* check if the following 2 are still valid or make a copy */
147 file = xop->file;
148 flc = &xop->flc;
149 fl = xop->fl;
150 notify = xop->callback;
151
152 if (op->info.rv) {
153 notify(flc, NULL, op->info.rv);
154 goto out;
155 }
156
157 /* got fs lock; bookkeep locally as well: */
158 flc->fl_flags &= ~FL_SLEEP;
159 if (posix_lock_file(file, flc, NULL)) {
160 /*
161 * This can only happen in the case of kmalloc() failure.
162 * The filesystem's own lock is the authoritative lock,
163 * so a failure to get the lock locally is not a disaster.
164 * As long as GFS cannot reliably cancel locks (especially
165 * in a low-memory situation), we're better off ignoring
166 * this failure than trying to recover.
167 */
168 log_error("gdlm_plock: vfs lock error file %p fl %p",
169 file, fl);
170 }
171
172 rv = notify(flc, NULL, 0);
173 if (rv) {
174 /* XXX: We need to cancel the fs lock here: */
175 printk("gfs2 lock granted after lock request failed;"
176 " dangling lock!\n");
177 goto out;
178 }
179
180out:
181 kfree(xop);
David Teigland869d81d2006-01-17 08:47:12 +0000182 return rv;
183}
184
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400185int gdlm_punlock(void *lockspace, struct lm_lockname *name,
David Teigland869d81d2006-01-17 08:47:12 +0000186 struct file *file, struct file_lock *fl)
187{
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400188 struct gdlm_ls *ls = lockspace;
David Teigland869d81d2006-01-17 08:47:12 +0000189 struct plock_op *op;
190 int rv;
191
192 op = kzalloc(sizeof(*op), GFP_KERNEL);
193 if (!op)
194 return -ENOMEM;
195
196 if (posix_lock_file_wait(file, fl) < 0)
197 log_error("gdlm_punlock: vfs unlock error %x,%llx",
David Teigland9229f012006-05-24 09:21:30 -0400198 name->ln_type, (unsigned long long)name->ln_number);
David Teigland869d81d2006-01-17 08:47:12 +0000199
200 op->info.optype = GDLM_PLOCK_OP_UNLOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400201 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000202 op->info.fsid = ls->id;
203 op->info.number = name->ln_number;
204 op->info.start = fl->fl_start;
205 op->info.end = fl->fl_end;
David Teiglandde9b75d2006-07-28 14:00:20 -0500206 op->info.owner = (__u64)(long) fl->fl_owner;
David Teigland869d81d2006-01-17 08:47:12 +0000207
208 send_op(op);
209 wait_event(recv_wq, (op->done != 0));
210
211 spin_lock(&ops_lock);
212 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500213 printk(KERN_INFO "punlock op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +0000214 list_del(&op->list);
215 }
216 spin_unlock(&ops_lock);
217
218 rv = op->info.rv;
219
Marc Eshel586759f2006-11-14 16:37:25 -0500220 if (rv == -ENOENT)
221 rv = 0;
222
David Teigland869d81d2006-01-17 08:47:12 +0000223 kfree(op);
224 return rv;
225}
226
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400227int gdlm_plock_get(void *lockspace, struct lm_lockname *name,
David Teigland869d81d2006-01-17 08:47:12 +0000228 struct file *file, struct file_lock *fl)
229{
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400230 struct gdlm_ls *ls = lockspace;
David Teigland869d81d2006-01-17 08:47:12 +0000231 struct plock_op *op;
232 int rv;
233
234 op = kzalloc(sizeof(*op), GFP_KERNEL);
235 if (!op)
236 return -ENOMEM;
237
238 op->info.optype = GDLM_PLOCK_OP_GET;
David Teigland3a2a9c92006-04-25 15:45:51 -0400239 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000240 op->info.ex = (fl->fl_type == F_WRLCK);
241 op->info.fsid = ls->id;
242 op->info.number = name->ln_number;
243 op->info.start = fl->fl_start;
244 op->info.end = fl->fl_end;
David Teiglandd88101d2007-06-08 16:00:22 -0500245 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -0500246
David Teigland869d81d2006-01-17 08:47:12 +0000247 send_op(op);
248 wait_event(recv_wq, (op->done != 0));
249
250 spin_lock(&ops_lock);
251 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500252 printk(KERN_INFO "plock_get op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +0000253 list_del(&op->list);
254 }
255 spin_unlock(&ops_lock);
256
David Teiglanda7a2ff82007-06-08 17:01:40 -0500257 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
258 -ENOENT if there are no locks on the file */
259
David Teigland869d81d2006-01-17 08:47:12 +0000260 rv = op->info.rv;
261
Marc Eshel586759f2006-11-14 16:37:25 -0500262 fl->fl_type = F_UNLCK;
263 if (rv == -ENOENT)
264 rv = 0;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500265 else if (rv > 0) {
David Teigland869d81d2006-01-17 08:47:12 +0000266 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
267 fl->fl_pid = op->info.pid;
268 fl->fl_start = op->info.start;
269 fl->fl_end = op->info.end;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500270 rv = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000271 }
272
273 kfree(op);
274 return rv;
275}
276
277/* a read copies out one plock request from the send list */
278static ssize_t dev_read(struct file *file, char __user *u, size_t count,
279 loff_t *ppos)
280{
281 struct gdlm_plock_info info;
282 struct plock_op *op = NULL;
283
284 if (count < sizeof(info))
285 return -EINVAL;
286
287 spin_lock(&ops_lock);
288 if (!list_empty(&send_list)) {
289 op = list_entry(send_list.next, struct plock_op, list);
290 list_move(&op->list, &recv_list);
291 memcpy(&info, &op->info, sizeof(info));
292 }
293 spin_unlock(&ops_lock);
294
295 if (!op)
296 return -EAGAIN;
297
298 if (copy_to_user(u, &info, sizeof(info)))
299 return -EFAULT;
300 return sizeof(info);
301}
302
303/* a write copies in one plock result that should match a plock_op
304 on the recv list */
305static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
306 loff_t *ppos)
307{
308 struct gdlm_plock_info info;
309 struct plock_op *op;
310 int found = 0;
311
312 if (count != sizeof(info))
313 return -EINVAL;
314
315 if (copy_from_user(&info, u, sizeof(info)))
316 return -EFAULT;
317
318 if (check_version(&info))
319 return -EINVAL;
320
321 spin_lock(&ops_lock);
322 list_for_each_entry(op, &recv_list, list) {
Steven Whitehouse9b47c112006-09-08 10:17:58 -0400323 if (op->info.fsid == info.fsid && op->info.number == info.number &&
David Teigland08eac932006-08-04 16:19:20 -0500324 op->info.owner == info.owner) {
David Teigland869d81d2006-01-17 08:47:12 +0000325 list_del_init(&op->list);
326 found = 1;
327 op->done = 1;
328 memcpy(&op->info, &info, sizeof(info));
329 break;
330 }
331 }
332 spin_unlock(&ops_lock);
333
Marc Eshel586759f2006-11-14 16:37:25 -0500334 if (found) {
335 struct plock_xop *xop;
336 xop = (struct plock_xop *)op;
337 if (xop->callback)
338 count = gdlm_plock_callback(op);
339 else
340 wake_up(&recv_wq);
341 } else
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500342 printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
David Teigland9229f012006-05-24 09:21:30 -0400343 (unsigned long long)info.number);
David Teigland869d81d2006-01-17 08:47:12 +0000344 return count;
345}
346
347static unsigned int dev_poll(struct file *file, poll_table *wait)
348{
349 poll_wait(file, &send_wq, wait);
350
351 spin_lock(&ops_lock);
352 if (!list_empty(&send_list)) {
353 spin_unlock(&ops_lock);
354 return POLLIN | POLLRDNORM;
355 }
356 spin_unlock(&ops_lock);
357 return 0;
358}
359
Arjan van de Ven00977a52007-02-12 00:55:34 -0800360static const struct file_operations dev_fops = {
David Teigland869d81d2006-01-17 08:47:12 +0000361 .read = dev_read,
362 .write = dev_write,
363 .poll = dev_poll,
364 .owner = THIS_MODULE
365};
366
367static struct miscdevice plock_dev_misc = {
368 .minor = MISC_DYNAMIC_MINOR,
369 .name = GDLM_PLOCK_MISC_NAME,
370 .fops = &dev_fops
371};
372
373int gdlm_plock_init(void)
374{
375 int rv;
376
377 spin_lock_init(&ops_lock);
378 INIT_LIST_HEAD(&send_list);
379 INIT_LIST_HEAD(&recv_list);
380 init_waitqueue_head(&send_wq);
381 init_waitqueue_head(&recv_wq);
382
383 rv = misc_register(&plock_dev_misc);
384 if (rv)
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500385 printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
386 rv);
David Teigland869d81d2006-01-17 08:47:12 +0000387 return rv;
388}
389
390void gdlm_plock_exit(void)
391{
392 if (misc_deregister(&plock_dev_misc) < 0)
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500393 printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
David Teigland869d81d2006-01-17 08:47:12 +0000394}
395