Merge branch 'master'
[linux-2.6] / fs / gfs2 / locking / dlm / plock.c
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
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
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
61 int 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;
73         op->info.pid            = fl->fl_pid;
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;
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)) {
86                 printk(KERN_INFO "plock op on list\n");
87                 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",
96                                   name->ln_type,
97                                   (unsigned long long)name->ln_number);
98         }
99
100         kfree(op);
101         return rv;
102 }
103
104 int 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",
117                           name->ln_type, (unsigned long long)name->ln_number);
118
119         op->info.optype         = GDLM_PLOCK_OP_UNLOCK;
120         op->info.pid            = fl->fl_pid;
121         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)) {
131                 printk(KERN_INFO "punlock op on list\n");
132                 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
142 int 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;
154         op->info.pid            = fl->fl_pid;
155         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)) {
166                 printk(KERN_INFO "plock_get op on list\n");
167                 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 */
187 static 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 */
214 static 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
246                 printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
247                         (unsigned long long)info.number);
248         return count;
249 }
250
251 static 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
264 static struct file_operations dev_fops = {
265         .read    = dev_read,
266         .write   = dev_write,
267         .poll    = dev_poll,
268         .owner   = THIS_MODULE
269 };
270
271 static struct miscdevice plock_dev_misc = {
272         .minor = MISC_DYNAMIC_MINOR,
273         .name = GDLM_PLOCK_MISC_NAME,
274         .fops = &dev_fops
275 };
276
277 int 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)
289                 printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
290                        rv);
291         return rv;
292 }
293
294 void gdlm_plock_exit(void)
295 {
296         if (misc_deregister(&plock_dev_misc) < 0)
297                 printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
298 }
299