blob: 6adfb2d4fd8c5e966fa5c0933ae8a17b7e96f7ef [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
6 * of the GNU General Public License v.2.
7 */
8
9#include <linux/miscdevice.h>
10#include <linux/lock_dlm_plock.h>
11
12#include "lock_dlm.h"
13
14
15static spinlock_t ops_lock;
16static struct list_head send_list;
17static struct list_head recv_list;
18static wait_queue_head_t send_wq;
19static wait_queue_head_t recv_wq;
20
21struct plock_op {
22 struct list_head list;
23 int done;
24 struct gdlm_plock_info info;
25};
26
27static 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
34static 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
51static 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
61int gdlm_plock(lm_lockspace_t *lockspace, struct lm_lockname *name,
62 struct file *file, int cmd, struct file_lock *fl)
63{
64 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
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 Teigland3a2a9c92006-04-25 15:45:51 -040073 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +000074 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;
80
81 send_op(op);
82 wait_event(recv_wq, (op->done != 0));
83
84 spin_lock(&ops_lock);
85 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -050086 printk(KERN_INFO "plock op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +000087 list_del(&op->list);
88 }
89 spin_unlock(&ops_lock);
90
91 rv = op->info.rv;
92
93 if (!rv) {
94 if (posix_lock_file_wait(file, fl) < 0)
95 log_error("gdlm_plock: vfs lock error %x,%llx",
David Teigland9229f012006-05-24 09:21:30 -040096 name->ln_type,
97 (unsigned long long)name->ln_number);
David Teigland869d81d2006-01-17 08:47:12 +000098 }
99
100 kfree(op);
101 return rv;
102}
103
104int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name,
105 struct file *file, struct file_lock *fl)
106{
107 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
108 struct plock_op *op;
109 int rv;
110
111 op = kzalloc(sizeof(*op), GFP_KERNEL);
112 if (!op)
113 return -ENOMEM;
114
115 if (posix_lock_file_wait(file, fl) < 0)
116 log_error("gdlm_punlock: vfs unlock error %x,%llx",
David Teigland9229f012006-05-24 09:21:30 -0400117 name->ln_type, (unsigned long long)name->ln_number);
David Teigland869d81d2006-01-17 08:47:12 +0000118
119 op->info.optype = GDLM_PLOCK_OP_UNLOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400120 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000121 op->info.fsid = ls->id;
122 op->info.number = name->ln_number;
123 op->info.start = fl->fl_start;
124 op->info.end = fl->fl_end;
125
126 send_op(op);
127 wait_event(recv_wq, (op->done != 0));
128
129 spin_lock(&ops_lock);
130 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500131 printk(KERN_INFO "punlock op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +0000132 list_del(&op->list);
133 }
134 spin_unlock(&ops_lock);
135
136 rv = op->info.rv;
137
138 kfree(op);
139 return rv;
140}
141
142int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name,
143 struct file *file, struct file_lock *fl)
144{
145 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
146 struct plock_op *op;
147 int rv;
148
149 op = kzalloc(sizeof(*op), GFP_KERNEL);
150 if (!op)
151 return -ENOMEM;
152
153 op->info.optype = GDLM_PLOCK_OP_GET;
David Teigland3a2a9c92006-04-25 15:45:51 -0400154 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000155 op->info.ex = (fl->fl_type == F_WRLCK);
156 op->info.fsid = ls->id;
157 op->info.number = name->ln_number;
158 op->info.start = fl->fl_start;
159 op->info.end = fl->fl_end;
160
161 send_op(op);
162 wait_event(recv_wq, (op->done != 0));
163
164 spin_lock(&ops_lock);
165 if (!list_empty(&op->list)) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500166 printk(KERN_INFO "plock_get op on list\n");
David Teigland869d81d2006-01-17 08:47:12 +0000167 list_del(&op->list);
168 }
169 spin_unlock(&ops_lock);
170
171 rv = op->info.rv;
172
173 if (rv == 0)
174 fl->fl_type = F_UNLCK;
175 else if (rv > 0) {
176 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
177 fl->fl_pid = op->info.pid;
178 fl->fl_start = op->info.start;
179 fl->fl_end = op->info.end;
180 }
181
182 kfree(op);
183 return rv;
184}
185
186/* a read copies out one plock request from the send list */
187static ssize_t dev_read(struct file *file, char __user *u, size_t count,
188 loff_t *ppos)
189{
190 struct gdlm_plock_info info;
191 struct plock_op *op = NULL;
192
193 if (count < sizeof(info))
194 return -EINVAL;
195
196 spin_lock(&ops_lock);
197 if (!list_empty(&send_list)) {
198 op = list_entry(send_list.next, struct plock_op, list);
199 list_move(&op->list, &recv_list);
200 memcpy(&info, &op->info, sizeof(info));
201 }
202 spin_unlock(&ops_lock);
203
204 if (!op)
205 return -EAGAIN;
206
207 if (copy_to_user(u, &info, sizeof(info)))
208 return -EFAULT;
209 return sizeof(info);
210}
211
212/* a write copies in one plock result that should match a plock_op
213 on the recv list */
214static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
215 loff_t *ppos)
216{
217 struct gdlm_plock_info info;
218 struct plock_op *op;
219 int found = 0;
220
221 if (count != sizeof(info))
222 return -EINVAL;
223
224 if (copy_from_user(&info, u, sizeof(info)))
225 return -EFAULT;
226
227 if (check_version(&info))
228 return -EINVAL;
229
230 spin_lock(&ops_lock);
231 list_for_each_entry(op, &recv_list, list) {
232 if (op->info.fsid == info.fsid &&
233 op->info.number == info.number) {
234 list_del_init(&op->list);
235 found = 1;
236 op->done = 1;
237 memcpy(&op->info, &info, sizeof(info));
238 break;
239 }
240 }
241 spin_unlock(&ops_lock);
242
243 if (found)
244 wake_up(&recv_wq);
245 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500246 printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
David Teigland9229f012006-05-24 09:21:30 -0400247 (unsigned long long)info.number);
David Teigland869d81d2006-01-17 08:47:12 +0000248 return count;
249}
250
251static unsigned int dev_poll(struct file *file, poll_table *wait)
252{
253 poll_wait(file, &send_wq, wait);
254
255 spin_lock(&ops_lock);
256 if (!list_empty(&send_list)) {
257 spin_unlock(&ops_lock);
258 return POLLIN | POLLRDNORM;
259 }
260 spin_unlock(&ops_lock);
261 return 0;
262}
263
264static struct file_operations dev_fops = {
265 .read = dev_read,
266 .write = dev_write,
267 .poll = dev_poll,
268 .owner = THIS_MODULE
269};
270
271static struct miscdevice plock_dev_misc = {
272 .minor = MISC_DYNAMIC_MINOR,
273 .name = GDLM_PLOCK_MISC_NAME,
274 .fops = &dev_fops
275};
276
277int gdlm_plock_init(void)
278{
279 int rv;
280
281 spin_lock_init(&ops_lock);
282 INIT_LIST_HEAD(&send_list);
283 INIT_LIST_HEAD(&recv_list);
284 init_waitqueue_head(&send_wq);
285 init_waitqueue_head(&recv_wq);
286
287 rv = misc_register(&plock_dev_misc);
288 if (rv)
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500289 printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
290 rv);
David Teigland869d81d2006-01-17 08:47:12 +0000291 return rv;
292}
293
294void gdlm_plock_exit(void)
295{
296 if (misc_deregister(&plock_dev_misc) < 0)
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500297 printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
David Teigland869d81d2006-01-17 08:47:12 +0000298}
299