GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / dlm / plock.c
blob32781fd9c5299946d18256b4184001237454ea5e
1 /*
2 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
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 version 2.
7 */
9 #include <linux/fs.h>
10 #include <linux/miscdevice.h>
11 #include <linux/poll.h>
12 #include <linux/dlm.h>
13 #include <linux/dlm_plock.h>
14 #include <linux/slab.h>
16 #include "dlm_internal.h"
17 #include "lockspace.h"
19 static spinlock_t ops_lock;
20 static struct list_head send_list;
21 static struct list_head recv_list;
22 static wait_queue_head_t send_wq;
23 static wait_queue_head_t recv_wq;
25 struct plock_op {
26 struct list_head list;
27 int done;
28 struct dlm_plock_info info;
31 struct plock_xop {
32 struct plock_op xop;
33 void *callback;
34 void *fl;
35 void *file;
36 struct file_lock flc;
40 static inline void set_version(struct dlm_plock_info *info)
42 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
43 info->version[1] = DLM_PLOCK_VERSION_MINOR;
44 info->version[2] = DLM_PLOCK_VERSION_PATCH;
47 static int check_version(struct dlm_plock_info *info)
49 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
50 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
51 log_print("plock device version mismatch: "
52 "kernel (%u.%u.%u), user (%u.%u.%u)",
53 DLM_PLOCK_VERSION_MAJOR,
54 DLM_PLOCK_VERSION_MINOR,
55 DLM_PLOCK_VERSION_PATCH,
56 info->version[0],
57 info->version[1],
58 info->version[2]);
59 return -EINVAL;
61 return 0;
64 static void send_op(struct plock_op *op)
66 set_version(&op->info);
67 INIT_LIST_HEAD(&op->list);
68 spin_lock(&ops_lock);
69 list_add_tail(&op->list, &send_list);
70 spin_unlock(&ops_lock);
71 wake_up(&send_wq);
74 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
75 int cmd, struct file_lock *fl)
77 struct dlm_ls *ls;
78 struct plock_op *op;
79 struct plock_xop *xop;
80 int rv;
82 ls = dlm_find_lockspace_local(lockspace);
83 if (!ls)
84 return -EINVAL;
86 xop = kzalloc(sizeof(*xop), GFP_NOFS);
87 if (!xop) {
88 rv = -ENOMEM;
89 goto out;
92 op = &xop->xop;
93 op->info.optype = DLM_PLOCK_OP_LOCK;
94 op->info.pid = fl->fl_pid;
95 op->info.ex = (fl->fl_type == F_WRLCK);
96 op->info.wait = IS_SETLKW(cmd);
97 op->info.fsid = ls->ls_global_id;
98 op->info.number = number;
99 op->info.start = fl->fl_start;
100 op->info.end = fl->fl_end;
101 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
102 /* fl_owner is lockd which doesn't distinguish
103 processes on the nfs client */
104 op->info.owner = (__u64) fl->fl_pid;
105 xop->callback = fl->fl_lmops->fl_grant;
106 locks_init_lock(&xop->flc);
107 locks_copy_lock(&xop->flc, fl);
108 xop->fl = fl;
109 xop->file = file;
110 } else {
111 op->info.owner = (__u64)(long) fl->fl_owner;
112 xop->callback = NULL;
115 send_op(op);
117 if (xop->callback == NULL)
118 wait_event(recv_wq, (op->done != 0));
119 else {
120 rv = FILE_LOCK_DEFERRED;
121 goto out;
124 spin_lock(&ops_lock);
125 if (!list_empty(&op->list)) {
126 log_error(ls, "dlm_posix_lock: op on list %llx",
127 (unsigned long long)number);
128 list_del(&op->list);
130 spin_unlock(&ops_lock);
132 rv = op->info.rv;
134 if (!rv) {
135 if (posix_lock_file_wait(file, fl) < 0)
136 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
137 (unsigned long long)number);
140 kfree(xop);
141 out:
142 dlm_put_lockspace(ls);
143 return rv;
145 EXPORT_SYMBOL_GPL(dlm_posix_lock);
147 /* Returns failure iff a successful lock operation should be canceled */
148 static int dlm_plock_callback(struct plock_op *op)
150 struct file *file;
151 struct file_lock *fl;
152 struct file_lock *flc;
153 int (*notify)(void *, void *, int) = NULL;
154 struct plock_xop *xop = (struct plock_xop *)op;
155 int rv = 0;
157 spin_lock(&ops_lock);
158 if (!list_empty(&op->list)) {
159 log_print("dlm_plock_callback: op on list %llx",
160 (unsigned long long)op->info.number);
161 list_del(&op->list);
163 spin_unlock(&ops_lock);
165 /* check if the following 2 are still valid or make a copy */
166 file = xop->file;
167 flc = &xop->flc;
168 fl = xop->fl;
169 notify = xop->callback;
171 if (op->info.rv) {
172 notify(fl, NULL, op->info.rv);
173 goto out;
176 /* got fs lock; bookkeep locally as well: */
177 flc->fl_flags &= ~FL_SLEEP;
178 if (posix_lock_file(file, flc, NULL)) {
180 * This can only happen in the case of kmalloc() failure.
181 * The filesystem's own lock is the authoritative lock,
182 * so a failure to get the lock locally is not a disaster.
183 * As long as the fs cannot reliably cancel locks (especially
184 * in a low-memory situation), we're better off ignoring
185 * this failure than trying to recover.
187 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
188 (unsigned long long)op->info.number, file, fl);
191 rv = notify(fl, NULL, 0);
192 if (rv) {
193 log_print("dlm_plock_callback: lock granted after lock request "
194 "failed; dangling lock!\n");
195 goto out;
198 out:
199 kfree(xop);
200 return rv;
203 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
204 struct file_lock *fl)
206 struct dlm_ls *ls;
207 struct plock_op *op;
208 int rv;
210 ls = dlm_find_lockspace_local(lockspace);
211 if (!ls)
212 return -EINVAL;
214 op = kzalloc(sizeof(*op), GFP_NOFS);
215 if (!op) {
216 rv = -ENOMEM;
217 goto out;
220 if (posix_lock_file_wait(file, fl) < 0)
221 log_error(ls, "dlm_posix_unlock: vfs unlock error %llx",
222 (unsigned long long)number);
224 op->info.optype = DLM_PLOCK_OP_UNLOCK;
225 op->info.pid = fl->fl_pid;
226 op->info.fsid = ls->ls_global_id;
227 op->info.number = number;
228 op->info.start = fl->fl_start;
229 op->info.end = fl->fl_end;
230 if (fl->fl_lmops && fl->fl_lmops->fl_grant)
231 op->info.owner = (__u64) fl->fl_pid;
232 else
233 op->info.owner = (__u64)(long) fl->fl_owner;
235 send_op(op);
236 wait_event(recv_wq, (op->done != 0));
238 spin_lock(&ops_lock);
239 if (!list_empty(&op->list)) {
240 log_error(ls, "dlm_posix_unlock: op on list %llx",
241 (unsigned long long)number);
242 list_del(&op->list);
244 spin_unlock(&ops_lock);
246 rv = op->info.rv;
248 if (rv == -ENOENT)
249 rv = 0;
251 kfree(op);
252 out:
253 dlm_put_lockspace(ls);
254 return rv;
256 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
258 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
259 struct file_lock *fl)
261 struct dlm_ls *ls;
262 struct plock_op *op;
263 int rv;
265 ls = dlm_find_lockspace_local(lockspace);
266 if (!ls)
267 return -EINVAL;
269 op = kzalloc(sizeof(*op), GFP_NOFS);
270 if (!op) {
271 rv = -ENOMEM;
272 goto out;
275 op->info.optype = DLM_PLOCK_OP_GET;
276 op->info.pid = fl->fl_pid;
277 op->info.ex = (fl->fl_type == F_WRLCK);
278 op->info.fsid = ls->ls_global_id;
279 op->info.number = number;
280 op->info.start = fl->fl_start;
281 op->info.end = fl->fl_end;
282 if (fl->fl_lmops && fl->fl_lmops->fl_grant)
283 op->info.owner = (__u64) fl->fl_pid;
284 else
285 op->info.owner = (__u64)(long) fl->fl_owner;
287 send_op(op);
288 wait_event(recv_wq, (op->done != 0));
290 spin_lock(&ops_lock);
291 if (!list_empty(&op->list)) {
292 log_error(ls, "dlm_posix_get: op on list %llx",
293 (unsigned long long)number);
294 list_del(&op->list);
296 spin_unlock(&ops_lock);
298 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
299 -ENOENT if there are no locks on the file */
301 rv = op->info.rv;
303 fl->fl_type = F_UNLCK;
304 if (rv == -ENOENT)
305 rv = 0;
306 else if (rv > 0) {
307 locks_init_lock(fl);
308 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
309 fl->fl_flags = FL_POSIX;
310 fl->fl_pid = op->info.pid;
311 fl->fl_start = op->info.start;
312 fl->fl_end = op->info.end;
313 rv = 0;
316 kfree(op);
317 out:
318 dlm_put_lockspace(ls);
319 return rv;
321 EXPORT_SYMBOL_GPL(dlm_posix_get);
323 /* a read copies out one plock request from the send list */
324 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
325 loff_t *ppos)
327 struct dlm_plock_info info;
328 struct plock_op *op = NULL;
330 if (count < sizeof(info))
331 return -EINVAL;
333 spin_lock(&ops_lock);
334 if (!list_empty(&send_list)) {
335 op = list_entry(send_list.next, struct plock_op, list);
336 list_move(&op->list, &recv_list);
337 memcpy(&info, &op->info, sizeof(info));
339 spin_unlock(&ops_lock);
341 if (!op)
342 return -EAGAIN;
344 if (copy_to_user(u, &info, sizeof(info)))
345 return -EFAULT;
346 return sizeof(info);
349 /* a write copies in one plock result that should match a plock_op
350 on the recv list */
351 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
352 loff_t *ppos)
354 struct dlm_plock_info info;
355 struct plock_op *op;
356 int found = 0, do_callback = 0;
358 if (count != sizeof(info))
359 return -EINVAL;
361 if (copy_from_user(&info, u, sizeof(info)))
362 return -EFAULT;
364 if (check_version(&info))
365 return -EINVAL;
367 spin_lock(&ops_lock);
368 list_for_each_entry(op, &recv_list, list) {
369 if (op->info.fsid == info.fsid &&
370 op->info.number == info.number &&
371 op->info.owner == info.owner) {
372 struct plock_xop *xop = (struct plock_xop *)op;
373 list_del_init(&op->list);
374 memcpy(&op->info, &info, sizeof(info));
375 if (xop->callback)
376 do_callback = 1;
377 else
378 op->done = 1;
379 found = 1;
380 break;
383 spin_unlock(&ops_lock);
385 if (found) {
386 if (do_callback)
387 dlm_plock_callback(op);
388 else
389 wake_up(&recv_wq);
390 } else
391 log_print("dev_write no op %x %llx", info.fsid,
392 (unsigned long long)info.number);
393 return count;
396 static unsigned int dev_poll(struct file *file, poll_table *wait)
398 unsigned int mask = 0;
400 poll_wait(file, &send_wq, wait);
402 spin_lock(&ops_lock);
403 if (!list_empty(&send_list))
404 mask = POLLIN | POLLRDNORM;
405 spin_unlock(&ops_lock);
407 return mask;
410 static const struct file_operations dev_fops = {
411 .read = dev_read,
412 .write = dev_write,
413 .poll = dev_poll,
414 .owner = THIS_MODULE
417 static struct miscdevice plock_dev_misc = {
418 .minor = MISC_DYNAMIC_MINOR,
419 .name = DLM_PLOCK_MISC_NAME,
420 .fops = &dev_fops
423 int dlm_plock_init(void)
425 int rv;
427 spin_lock_init(&ops_lock);
428 INIT_LIST_HEAD(&send_list);
429 INIT_LIST_HEAD(&recv_list);
430 init_waitqueue_head(&send_wq);
431 init_waitqueue_head(&recv_wq);
433 rv = misc_register(&plock_dev_misc);
434 if (rv)
435 log_print("dlm_plock_init: misc_register failed %d", rv);
436 return rv;
439 void dlm_plock_exit(void)
441 if (misc_deregister(&plock_dev_misc) < 0)
442 log_print("dlm_plock_exit: misc_deregister failed");