GNU Linux-libre 4.9.337-gnu1
[releases.git] / fs / dlm / plock.c
1 /*
2  * Copyright (C) 2005-2008 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 version 2.
7  */
8
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>
15
16 #include "dlm_internal.h"
17 #include "lockspace.h"
18
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;
24
25 struct plock_op {
26         struct list_head list;
27         int done;
28         struct dlm_plock_info info;
29         int (*callback)(struct file_lock *fl, int result);
30 };
31
32 struct plock_xop {
33         struct plock_op xop;
34         void *fl;
35         void *file;
36         struct file_lock flc;
37 };
38
39
40 static inline void set_version(struct dlm_plock_info *info)
41 {
42         info->version[0] = DLM_PLOCK_VERSION_MAJOR;
43         info->version[1] = DLM_PLOCK_VERSION_MINOR;
44         info->version[2] = DLM_PLOCK_VERSION_PATCH;
45 }
46
47 static int check_version(struct dlm_plock_info *info)
48 {
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;
60         }
61         return 0;
62 }
63
64 static void send_op(struct plock_op *op)
65 {
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);
72 }
73
74 /* If a process was killed while waiting for the only plock on a file,
75    locks_remove_posix will not see any lock on the file so it won't
76    send an unlock-close to us to pass on to userspace to clean up the
77    abandoned waiter.  So, we have to insert the unlock-close when the
78    lock call is interrupted. */
79
80 static void do_unlock_close(struct dlm_ls *ls, u64 number,
81                             struct file *file, struct file_lock *fl)
82 {
83         struct plock_op *op;
84
85         op = kzalloc(sizeof(*op), GFP_NOFS);
86         if (!op)
87                 return;
88
89         op->info.optype         = DLM_PLOCK_OP_UNLOCK;
90         op->info.pid            = fl->fl_pid;
91         op->info.fsid           = ls->ls_global_id;
92         op->info.number         = number;
93         op->info.start          = 0;
94         op->info.end            = OFFSET_MAX;
95         if (fl->fl_lmops && fl->fl_lmops->lm_grant)
96                 op->info.owner  = (__u64) fl->fl_pid;
97         else
98                 op->info.owner  = (__u64)(long) fl->fl_owner;
99
100         op->info.flags |= DLM_PLOCK_FL_CLOSE;
101         send_op(op);
102 }
103
104 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
105                    int cmd, struct file_lock *fl)
106 {
107         struct dlm_ls *ls;
108         struct plock_op *op;
109         struct plock_xop *xop;
110         int rv;
111
112         ls = dlm_find_lockspace_local(lockspace);
113         if (!ls)
114                 return -EINVAL;
115
116         xop = kzalloc(sizeof(*xop), GFP_NOFS);
117         if (!xop) {
118                 rv = -ENOMEM;
119                 goto out;
120         }
121
122         op = &xop->xop;
123         op->info.optype         = DLM_PLOCK_OP_LOCK;
124         op->info.pid            = fl->fl_pid;
125         op->info.ex             = (fl->fl_type == F_WRLCK);
126         op->info.wait           = IS_SETLKW(cmd);
127         op->info.fsid           = ls->ls_global_id;
128         op->info.number         = number;
129         op->info.start          = fl->fl_start;
130         op->info.end            = fl->fl_end;
131         if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
132                 /* fl_owner is lockd which doesn't distinguish
133                    processes on the nfs client */
134                 op->info.owner  = (__u64) fl->fl_pid;
135                 op->callback    = fl->fl_lmops->lm_grant;
136                 locks_init_lock(&xop->flc);
137                 locks_copy_lock(&xop->flc, fl);
138                 xop->fl         = fl;
139                 xop->file       = file;
140         } else {
141                 op->info.owner  = (__u64)(long) fl->fl_owner;
142         }
143
144         send_op(op);
145
146         if (!op->callback) {
147                 rv = wait_event_interruptible(recv_wq, (op->done != 0));
148                 if (rv == -ERESTARTSYS) {
149                         log_debug(ls, "dlm_posix_lock: wait killed %llx",
150                                   (unsigned long long)number);
151                         spin_lock(&ops_lock);
152                         list_del(&op->list);
153                         spin_unlock(&ops_lock);
154                         kfree(xop);
155                         do_unlock_close(ls, number, file, fl);
156                         goto out;
157                 }
158         } else {
159                 rv = FILE_LOCK_DEFERRED;
160                 goto out;
161         }
162
163         spin_lock(&ops_lock);
164         if (!list_empty(&op->list)) {
165                 log_error(ls, "dlm_posix_lock: op on list %llx",
166                           (unsigned long long)number);
167                 list_del(&op->list);
168         }
169         spin_unlock(&ops_lock);
170
171         rv = op->info.rv;
172
173         if (!rv) {
174                 if (locks_lock_file_wait(file, fl) < 0)
175                         log_error(ls, "dlm_posix_lock: vfs lock error %llx",
176                                   (unsigned long long)number);
177         }
178
179         kfree(xop);
180 out:
181         dlm_put_lockspace(ls);
182         return rv;
183 }
184 EXPORT_SYMBOL_GPL(dlm_posix_lock);
185
186 /* Returns failure iff a successful lock operation should be canceled */
187 static int dlm_plock_callback(struct plock_op *op)
188 {
189         struct file *file;
190         struct file_lock *fl;
191         struct file_lock *flc;
192         int (*notify)(struct file_lock *fl, int result) = NULL;
193         struct plock_xop *xop = (struct plock_xop *)op;
194         int rv = 0;
195
196         spin_lock(&ops_lock);
197         if (!list_empty(&op->list)) {
198                 log_print("dlm_plock_callback: op on list %llx",
199                           (unsigned long long)op->info.number);
200                 list_del(&op->list);
201         }
202         spin_unlock(&ops_lock);
203
204         /* check if the following 2 are still valid or make a copy */
205         file = xop->file;
206         flc = &xop->flc;
207         fl = xop->fl;
208         notify = op->callback;
209
210         if (op->info.rv) {
211                 notify(fl, op->info.rv);
212                 goto out;
213         }
214
215         /* got fs lock; bookkeep locally as well: */
216         flc->fl_flags &= ~FL_SLEEP;
217         if (posix_lock_file(file, flc, NULL)) {
218                 /*
219                  * This can only happen in the case of kmalloc() failure.
220                  * The filesystem's own lock is the authoritative lock,
221                  * so a failure to get the lock locally is not a disaster.
222                  * As long as the fs cannot reliably cancel locks (especially
223                  * in a low-memory situation), we're better off ignoring
224                  * this failure than trying to recover.
225                  */
226                 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
227                           (unsigned long long)op->info.number, file, fl);
228         }
229
230         rv = notify(fl, 0);
231         if (rv) {
232                 /* XXX: We need to cancel the fs lock here: */
233                 log_print("dlm_plock_callback: lock granted after lock request "
234                           "failed; dangling lock!\n");
235                 goto out;
236         }
237
238 out:
239         kfree(xop);
240         return rv;
241 }
242
243 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
244                      struct file_lock *fl)
245 {
246         struct dlm_ls *ls;
247         struct plock_op *op;
248         int rv;
249         unsigned char fl_flags = fl->fl_flags;
250
251         ls = dlm_find_lockspace_local(lockspace);
252         if (!ls)
253                 return -EINVAL;
254
255         op = kzalloc(sizeof(*op), GFP_NOFS);
256         if (!op) {
257                 rv = -ENOMEM;
258                 goto out;
259         }
260
261         /* cause the vfs unlock to return ENOENT if lock is not found */
262         fl->fl_flags |= FL_EXISTS;
263
264         rv = locks_lock_file_wait(file, fl);
265         if (rv == -ENOENT) {
266                 rv = 0;
267                 goto out_free;
268         }
269         if (rv < 0) {
270                 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
271                           rv, (unsigned long long)number);
272         }
273
274         op->info.optype         = DLM_PLOCK_OP_UNLOCK;
275         op->info.pid            = fl->fl_pid;
276         op->info.fsid           = ls->ls_global_id;
277         op->info.number         = number;
278         op->info.start          = fl->fl_start;
279         op->info.end            = fl->fl_end;
280         if (fl->fl_lmops && fl->fl_lmops->lm_grant)
281                 op->info.owner  = (__u64) fl->fl_pid;
282         else
283                 op->info.owner  = (__u64)(long) fl->fl_owner;
284
285         if (fl->fl_flags & FL_CLOSE) {
286                 op->info.flags |= DLM_PLOCK_FL_CLOSE;
287                 send_op(op);
288                 rv = 0;
289                 goto out;
290         }
291
292         send_op(op);
293         wait_event(recv_wq, (op->done != 0));
294
295         spin_lock(&ops_lock);
296         if (!list_empty(&op->list)) {
297                 log_error(ls, "dlm_posix_unlock: op on list %llx",
298                           (unsigned long long)number);
299                 list_del(&op->list);
300         }
301         spin_unlock(&ops_lock);
302
303         rv = op->info.rv;
304
305         if (rv == -ENOENT)
306                 rv = 0;
307
308 out_free:
309         kfree(op);
310 out:
311         dlm_put_lockspace(ls);
312         fl->fl_flags = fl_flags;
313         return rv;
314 }
315 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
316
317 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
318                   struct file_lock *fl)
319 {
320         struct dlm_ls *ls;
321         struct plock_op *op;
322         int rv;
323
324         ls = dlm_find_lockspace_local(lockspace);
325         if (!ls)
326                 return -EINVAL;
327
328         op = kzalloc(sizeof(*op), GFP_NOFS);
329         if (!op) {
330                 rv = -ENOMEM;
331                 goto out;
332         }
333
334         op->info.optype         = DLM_PLOCK_OP_GET;
335         op->info.pid            = fl->fl_pid;
336         op->info.ex             = (fl->fl_type == F_WRLCK);
337         op->info.fsid           = ls->ls_global_id;
338         op->info.number         = number;
339         op->info.start          = fl->fl_start;
340         op->info.end            = fl->fl_end;
341         if (fl->fl_lmops && fl->fl_lmops->lm_grant)
342                 op->info.owner  = (__u64) fl->fl_pid;
343         else
344                 op->info.owner  = (__u64)(long) fl->fl_owner;
345
346         send_op(op);
347         wait_event(recv_wq, (op->done != 0));
348
349         spin_lock(&ops_lock);
350         if (!list_empty(&op->list)) {
351                 log_error(ls, "dlm_posix_get: op on list %llx",
352                           (unsigned long long)number);
353                 list_del(&op->list);
354         }
355         spin_unlock(&ops_lock);
356
357         /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
358            -ENOENT if there are no locks on the file */
359
360         rv = op->info.rv;
361
362         fl->fl_type = F_UNLCK;
363         if (rv == -ENOENT)
364                 rv = 0;
365         else if (rv > 0) {
366                 locks_init_lock(fl);
367                 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
368                 fl->fl_flags = FL_POSIX;
369                 fl->fl_pid = op->info.pid;
370                 fl->fl_start = op->info.start;
371                 fl->fl_end = op->info.end;
372                 rv = 0;
373         }
374
375         kfree(op);
376 out:
377         dlm_put_lockspace(ls);
378         return rv;
379 }
380 EXPORT_SYMBOL_GPL(dlm_posix_get);
381
382 /* a read copies out one plock request from the send list */
383 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
384                         loff_t *ppos)
385 {
386         struct dlm_plock_info info;
387         struct plock_op *op = NULL;
388
389         if (count < sizeof(info))
390                 return -EINVAL;
391
392         spin_lock(&ops_lock);
393         if (!list_empty(&send_list)) {
394                 op = list_entry(send_list.next, struct plock_op, list);
395                 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
396                         list_del(&op->list);
397                 else
398                         list_move(&op->list, &recv_list);
399                 memcpy(&info, &op->info, sizeof(info));
400         }
401         spin_unlock(&ops_lock);
402
403         if (!op)
404                 return -EAGAIN;
405
406         /* there is no need to get a reply from userspace for unlocks
407            that were generated by the vfs cleaning up for a close
408            (the process did not make an unlock call). */
409
410         if (op->info.flags & DLM_PLOCK_FL_CLOSE)
411                 kfree(op);
412
413         if (copy_to_user(u, &info, sizeof(info)))
414                 return -EFAULT;
415         return sizeof(info);
416 }
417
418 /* a write copies in one plock result that should match a plock_op
419    on the recv list */
420 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
421                          loff_t *ppos)
422 {
423         struct dlm_plock_info info;
424         struct plock_op *op;
425         int found = 0, do_callback = 0;
426
427         if (count != sizeof(info))
428                 return -EINVAL;
429
430         if (copy_from_user(&info, u, sizeof(info)))
431                 return -EFAULT;
432
433         if (check_version(&info))
434                 return -EINVAL;
435
436         spin_lock(&ops_lock);
437         list_for_each_entry(op, &recv_list, list) {
438                 if (op->info.fsid == info.fsid &&
439                     op->info.number == info.number &&
440                     op->info.owner == info.owner) {
441                         list_del_init(&op->list);
442                         memcpy(&op->info, &info, sizeof(info));
443                         if (op->callback)
444                                 do_callback = 1;
445                         else
446                                 op->done = 1;
447                         found = 1;
448                         break;
449                 }
450         }
451         spin_unlock(&ops_lock);
452
453         if (found) {
454                 if (do_callback)
455                         dlm_plock_callback(op);
456                 else
457                         wake_up(&recv_wq);
458         } else
459                 log_print("dev_write no op %x %llx", info.fsid,
460                           (unsigned long long)info.number);
461         return count;
462 }
463
464 static unsigned int dev_poll(struct file *file, poll_table *wait)
465 {
466         unsigned int mask = 0;
467
468         poll_wait(file, &send_wq, wait);
469
470         spin_lock(&ops_lock);
471         if (!list_empty(&send_list))
472                 mask = POLLIN | POLLRDNORM;
473         spin_unlock(&ops_lock);
474
475         return mask;
476 }
477
478 static const struct file_operations dev_fops = {
479         .read    = dev_read,
480         .write   = dev_write,
481         .poll    = dev_poll,
482         .owner   = THIS_MODULE,
483         .llseek  = noop_llseek,
484 };
485
486 static struct miscdevice plock_dev_misc = {
487         .minor = MISC_DYNAMIC_MINOR,
488         .name = DLM_PLOCK_MISC_NAME,
489         .fops = &dev_fops
490 };
491
492 int dlm_plock_init(void)
493 {
494         int rv;
495
496         spin_lock_init(&ops_lock);
497         INIT_LIST_HEAD(&send_list);
498         INIT_LIST_HEAD(&recv_list);
499         init_waitqueue_head(&send_wq);
500         init_waitqueue_head(&recv_wq);
501
502         rv = misc_register(&plock_dev_misc);
503         if (rv)
504                 log_print("dlm_plock_init: misc_register failed %d", rv);
505         return rv;
506 }
507
508 void dlm_plock_exit(void)
509 {
510         misc_deregister(&plock_dev_misc);
511 }
512