blob: b5f89aef3b29c88a1447201504a797eb16e93dcb [file] [log] [blame]
David Teigland869d81d2006-01-17 08:47:12 +00001/*
David Teigland24022112008-03-14 15:09:15 -05002 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
David Teigland869d81d2006-01-17 08:47:12 +00003 *
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
David Teigland24022112008-03-14 15:09:15 -05009#include <linux/fs.h>
David Teigland869d81d2006-01-17 08:47:12 +000010#include <linux/miscdevice.h>
Al Virobd01f842006-10-19 17:23:57 -040011#include <linux/poll.h>
David Teigland24022112008-03-14 15:09:15 -050012#include <linux/dlm.h>
13#include <linux/dlm_plock.h>
David Teigland869d81d2006-01-17 08:47:12 +000014
David Teigland24022112008-03-14 15:09:15 -050015#include "dlm_internal.h"
16#include "lockspace.h"
David Teigland869d81d2006-01-17 08:47:12 +000017
18static spinlock_t ops_lock;
19static struct list_head send_list;
20static struct list_head recv_list;
21static wait_queue_head_t send_wq;
22static wait_queue_head_t recv_wq;
23
24struct plock_op {
25 struct list_head list;
26 int done;
David Teigland24022112008-03-14 15:09:15 -050027 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +000028};
29
Marc Eshel586759f2006-11-14 16:37:25 -050030struct plock_xop {
31 struct plock_op xop;
32 void *callback;
33 void *fl;
34 void *file;
35 struct file_lock flc;
36};
37
38
David Teigland24022112008-03-14 15:09:15 -050039static inline void set_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000040{
David Teigland24022112008-03-14 15:09:15 -050041 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
42 info->version[1] = DLM_PLOCK_VERSION_MINOR;
43 info->version[2] = DLM_PLOCK_VERSION_PATCH;
David Teigland869d81d2006-01-17 08:47:12 +000044}
45
David Teigland24022112008-03-14 15:09:15 -050046static int check_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000047{
David Teigland24022112008-03-14 15:09:15 -050048 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
49 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
50 log_print("plock device version mismatch: "
David Teigland869d81d2006-01-17 08:47:12 +000051 "kernel (%u.%u.%u), user (%u.%u.%u)",
David Teigland24022112008-03-14 15:09:15 -050052 DLM_PLOCK_VERSION_MAJOR,
53 DLM_PLOCK_VERSION_MINOR,
54 DLM_PLOCK_VERSION_PATCH,
David Teigland869d81d2006-01-17 08:47:12 +000055 info->version[0],
56 info->version[1],
57 info->version[2]);
58 return -EINVAL;
59 }
60 return 0;
61}
62
63static void send_op(struct plock_op *op)
64{
65 set_version(&op->info);
66 INIT_LIST_HEAD(&op->list);
67 spin_lock(&ops_lock);
68 list_add_tail(&op->list, &send_list);
69 spin_unlock(&ops_lock);
70 wake_up(&send_wq);
71}
72
David Teigland24022112008-03-14 15:09:15 -050073int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
74 int cmd, struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +000075{
David Teigland24022112008-03-14 15:09:15 -050076 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +000077 struct plock_op *op;
Marc Eshel586759f2006-11-14 16:37:25 -050078 struct plock_xop *xop;
David Teigland869d81d2006-01-17 08:47:12 +000079 int rv;
80
David Teigland24022112008-03-14 15:09:15 -050081 ls = dlm_find_lockspace_local(lockspace);
82 if (!ls)
83 return -EINVAL;
84
David Teigland573c24c2009-11-30 16:34:43 -060085 xop = kzalloc(sizeof(*xop), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -050086 if (!xop) {
87 rv = -ENOMEM;
88 goto out;
89 }
David Teigland869d81d2006-01-17 08:47:12 +000090
Marc Eshel586759f2006-11-14 16:37:25 -050091 op = &xop->xop;
David Teigland24022112008-03-14 15:09:15 -050092 op->info.optype = DLM_PLOCK_OP_LOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -040093 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +000094 op->info.ex = (fl->fl_type == F_WRLCK);
95 op->info.wait = IS_SETLKW(cmd);
David Teigland24022112008-03-14 15:09:15 -050096 op->info.fsid = ls->ls_global_id;
97 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +000098 op->info.start = fl->fl_start;
99 op->info.end = fl->fl_end;
Marc Eshel586759f2006-11-14 16:37:25 -0500100 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
David Teigland2066b582007-12-06 09:35:25 -0600101 /* fl_owner is lockd which doesn't distinguish
102 processes on the nfs client */
103 op->info.owner = (__u64) fl->fl_pid;
Marc Eshel586759f2006-11-14 16:37:25 -0500104 xop->callback = fl->fl_lmops->fl_grant;
105 locks_init_lock(&xop->flc);
106 locks_copy_lock(&xop->flc, fl);
107 xop->fl = fl;
108 xop->file = file;
David Teigland2066b582007-12-06 09:35:25 -0600109 } else {
110 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -0500111 xop->callback = NULL;
David Teigland2066b582007-12-06 09:35:25 -0600112 }
David Teigland869d81d2006-01-17 08:47:12 +0000113
114 send_op(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500115
116 if (xop->callback == NULL)
117 wait_event(recv_wq, (op->done != 0));
David Teigland24022112008-03-14 15:09:15 -0500118 else {
Miklos Szeredibde74e42008-07-25 01:48:57 -0700119 rv = FILE_LOCK_DEFERRED;
David Teigland24022112008-03-14 15:09:15 -0500120 goto out;
121 }
David Teigland869d81d2006-01-17 08:47:12 +0000122
123 spin_lock(&ops_lock);
124 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500125 log_error(ls, "dlm_posix_lock: op on list %llx",
126 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000127 list_del(&op->list);
128 }
129 spin_unlock(&ops_lock);
130
131 rv = op->info.rv;
132
133 if (!rv) {
134 if (posix_lock_file_wait(file, fl) < 0)
David Teigland24022112008-03-14 15:09:15 -0500135 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
136 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000137 }
138
Marc Eshel586759f2006-11-14 16:37:25 -0500139 kfree(xop);
David Teigland24022112008-03-14 15:09:15 -0500140out:
141 dlm_put_lockspace(ls);
Marc Eshel586759f2006-11-14 16:37:25 -0500142 return rv;
143}
David Teigland24022112008-03-14 15:09:15 -0500144EXPORT_SYMBOL_GPL(dlm_posix_lock);
Marc Eshel586759f2006-11-14 16:37:25 -0500145
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146/* Returns failure iff a successful lock operation should be canceled */
David Teigland24022112008-03-14 15:09:15 -0500147static int dlm_plock_callback(struct plock_op *op)
Marc Eshel586759f2006-11-14 16:37:25 -0500148{
149 struct file *file;
150 struct file_lock *fl;
151 struct file_lock *flc;
152 int (*notify)(void *, void *, int) = NULL;
153 struct plock_xop *xop = (struct plock_xop *)op;
154 int rv = 0;
155
156 spin_lock(&ops_lock);
157 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500158 log_print("dlm_plock_callback: op on list %llx",
159 (unsigned long long)op->info.number);
Marc Eshel586759f2006-11-14 16:37:25 -0500160 list_del(&op->list);
161 }
162 spin_unlock(&ops_lock);
163
164 /* check if the following 2 are still valid or make a copy */
165 file = xop->file;
166 flc = &xop->flc;
167 fl = xop->fl;
168 notify = xop->callback;
169
170 if (op->info.rv) {
David Teigland24179f42009-01-19 13:13:33 -0600171 notify(fl, NULL, op->info.rv);
Marc Eshel586759f2006-11-14 16:37:25 -0500172 goto out;
173 }
174
175 /* got fs lock; bookkeep locally as well: */
176 flc->fl_flags &= ~FL_SLEEP;
177 if (posix_lock_file(file, flc, NULL)) {
178 /*
179 * This can only happen in the case of kmalloc() failure.
180 * The filesystem's own lock is the authoritative lock,
181 * so a failure to get the lock locally is not a disaster.
David Teigland24022112008-03-14 15:09:15 -0500182 * As long as the fs cannot reliably cancel locks (especially
Marc Eshel586759f2006-11-14 16:37:25 -0500183 * in a low-memory situation), we're better off ignoring
184 * this failure than trying to recover.
185 */
David Teigland24022112008-03-14 15:09:15 -0500186 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
187 (unsigned long long)op->info.number, file, fl);
Marc Eshel586759f2006-11-14 16:37:25 -0500188 }
189
David Teigland24179f42009-01-19 13:13:33 -0600190 rv = notify(fl, NULL, 0);
Marc Eshel586759f2006-11-14 16:37:25 -0500191 if (rv) {
192 /* XXX: We need to cancel the fs lock here: */
David Teigland24022112008-03-14 15:09:15 -0500193 log_print("dlm_plock_callback: lock granted after lock request "
194 "failed; dangling lock!\n");
Marc Eshel586759f2006-11-14 16:37:25 -0500195 goto out;
196 }
197
198out:
199 kfree(xop);
David Teigland869d81d2006-01-17 08:47:12 +0000200 return rv;
201}
202
David Teigland24022112008-03-14 15:09:15 -0500203int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
204 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000205{
David Teigland24022112008-03-14 15:09:15 -0500206 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000207 struct plock_op *op;
208 int rv;
209
David Teigland24022112008-03-14 15:09:15 -0500210 ls = dlm_find_lockspace_local(lockspace);
211 if (!ls)
212 return -EINVAL;
213
David Teigland573c24c2009-11-30 16:34:43 -0600214 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500215 if (!op) {
216 rv = -ENOMEM;
217 goto out;
218 }
David Teigland869d81d2006-01-17 08:47:12 +0000219
220 if (posix_lock_file_wait(file, fl) < 0)
David Teigland24022112008-03-14 15:09:15 -0500221 log_error(ls, "dlm_posix_unlock: vfs unlock error %llx",
222 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000223
David Teigland24022112008-03-14 15:09:15 -0500224 op->info.optype = DLM_PLOCK_OP_UNLOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400225 op->info.pid = fl->fl_pid;
David Teigland24022112008-03-14 15:09:15 -0500226 op->info.fsid = ls->ls_global_id;
227 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000228 op->info.start = fl->fl_start;
229 op->info.end = fl->fl_end;
David Teigland2066b582007-12-06 09:35:25 -0600230 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;
David Teigland869d81d2006-01-17 08:47:12 +0000234
235 send_op(op);
236 wait_event(recv_wq, (op->done != 0));
237
238 spin_lock(&ops_lock);
239 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500240 log_error(ls, "dlm_posix_unlock: op on list %llx",
241 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000242 list_del(&op->list);
243 }
244 spin_unlock(&ops_lock);
245
246 rv = op->info.rv;
247
Marc Eshel586759f2006-11-14 16:37:25 -0500248 if (rv == -ENOENT)
249 rv = 0;
250
David Teigland869d81d2006-01-17 08:47:12 +0000251 kfree(op);
David Teigland24022112008-03-14 15:09:15 -0500252out:
253 dlm_put_lockspace(ls);
David Teigland869d81d2006-01-17 08:47:12 +0000254 return rv;
255}
David Teigland24022112008-03-14 15:09:15 -0500256EXPORT_SYMBOL_GPL(dlm_posix_unlock);
David Teigland869d81d2006-01-17 08:47:12 +0000257
David Teigland24022112008-03-14 15:09:15 -0500258int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
259 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000260{
David Teigland24022112008-03-14 15:09:15 -0500261 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000262 struct plock_op *op;
263 int rv;
264
David Teigland24022112008-03-14 15:09:15 -0500265 ls = dlm_find_lockspace_local(lockspace);
266 if (!ls)
267 return -EINVAL;
David Teigland869d81d2006-01-17 08:47:12 +0000268
David Teigland573c24c2009-11-30 16:34:43 -0600269 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500270 if (!op) {
271 rv = -ENOMEM;
272 goto out;
273 }
274
275 op->info.optype = DLM_PLOCK_OP_GET;
David Teigland3a2a9c92006-04-25 15:45:51 -0400276 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000277 op->info.ex = (fl->fl_type == F_WRLCK);
David Teigland24022112008-03-14 15:09:15 -0500278 op->info.fsid = ls->ls_global_id;
279 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000280 op->info.start = fl->fl_start;
281 op->info.end = fl->fl_end;
David Teigland2066b582007-12-06 09:35:25 -0600282 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;
Marc Eshel586759f2006-11-14 16:37:25 -0500286
David Teigland869d81d2006-01-17 08:47:12 +0000287 send_op(op);
288 wait_event(recv_wq, (op->done != 0));
289
290 spin_lock(&ops_lock);
291 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500292 log_error(ls, "dlm_posix_get: op on list %llx",
293 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000294 list_del(&op->list);
295 }
296 spin_unlock(&ops_lock);
297
David Teiglanda7a2ff82007-06-08 17:01:40 -0500298 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
299 -ENOENT if there are no locks on the file */
300
David Teigland869d81d2006-01-17 08:47:12 +0000301 rv = op->info.rv;
302
Marc Eshel586759f2006-11-14 16:37:25 -0500303 fl->fl_type = F_UNLCK;
304 if (rv == -ENOENT)
305 rv = 0;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500306 else if (rv > 0) {
Jeff Layton20d5a392009-01-21 11:34:50 -0500307 locks_init_lock(fl);
David Teigland869d81d2006-01-17 08:47:12 +0000308 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
Jeff Layton20d5a392009-01-21 11:34:50 -0500309 fl->fl_flags = FL_POSIX;
David Teigland869d81d2006-01-17 08:47:12 +0000310 fl->fl_pid = op->info.pid;
311 fl->fl_start = op->info.start;
312 fl->fl_end = op->info.end;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500313 rv = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000314 }
315
316 kfree(op);
David Teigland24022112008-03-14 15:09:15 -0500317out:
318 dlm_put_lockspace(ls);
David Teigland869d81d2006-01-17 08:47:12 +0000319 return rv;
320}
David Teigland24022112008-03-14 15:09:15 -0500321EXPORT_SYMBOL_GPL(dlm_posix_get);
David Teigland869d81d2006-01-17 08:47:12 +0000322
323/* a read copies out one plock request from the send list */
324static ssize_t dev_read(struct file *file, char __user *u, size_t count,
325 loff_t *ppos)
326{
David Teigland24022112008-03-14 15:09:15 -0500327 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000328 struct plock_op *op = NULL;
329
330 if (count < sizeof(info))
331 return -EINVAL;
332
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));
338 }
339 spin_unlock(&ops_lock);
340
341 if (!op)
342 return -EAGAIN;
343
344 if (copy_to_user(u, &info, sizeof(info)))
345 return -EFAULT;
346 return sizeof(info);
347}
348
349/* a write copies in one plock result that should match a plock_op
350 on the recv list */
351static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
352 loff_t *ppos)
353{
David Teigland24022112008-03-14 15:09:15 -0500354 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000355 struct plock_op *op;
David Teiglandc78a87d2009-06-18 13:20:24 -0500356 int found = 0, do_callback = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000357
358 if (count != sizeof(info))
359 return -EINVAL;
360
361 if (copy_from_user(&info, u, sizeof(info)))
362 return -EFAULT;
363
364 if (check_version(&info))
365 return -EINVAL;
366
367 spin_lock(&ops_lock);
368 list_for_each_entry(op, &recv_list, list) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500369 if (op->info.fsid == info.fsid &&
370 op->info.number == info.number &&
David Teigland08eac932006-08-04 16:19:20 -0500371 op->info.owner == info.owner) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500372 struct plock_xop *xop = (struct plock_xop *)op;
David Teigland869d81d2006-01-17 08:47:12 +0000373 list_del_init(&op->list);
David Teigland869d81d2006-01-17 08:47:12 +0000374 memcpy(&op->info, &info, sizeof(info));
David Teiglandc78a87d2009-06-18 13:20:24 -0500375 if (xop->callback)
376 do_callback = 1;
377 else
378 op->done = 1;
379 found = 1;
David Teigland869d81d2006-01-17 08:47:12 +0000380 break;
381 }
382 }
383 spin_unlock(&ops_lock);
384
Marc Eshel586759f2006-11-14 16:37:25 -0500385 if (found) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500386 if (do_callback)
David Teigland817d10b2008-05-13 14:28:26 -0500387 dlm_plock_callback(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500388 else
389 wake_up(&recv_wq);
390 } else
David Teigland24022112008-03-14 15:09:15 -0500391 log_print("dev_write no op %x %llx", info.fsid,
392 (unsigned long long)info.number);
David Teigland869d81d2006-01-17 08:47:12 +0000393 return count;
394}
395
396static unsigned int dev_poll(struct file *file, poll_table *wait)
397{
Denis Chengcee23c72007-07-25 17:53:58 +0800398 unsigned int mask = 0;
399
David Teigland869d81d2006-01-17 08:47:12 +0000400 poll_wait(file, &send_wq, wait);
401
402 spin_lock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800403 if (!list_empty(&send_list))
404 mask = POLLIN | POLLRDNORM;
David Teigland869d81d2006-01-17 08:47:12 +0000405 spin_unlock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800406
407 return mask;
David Teigland869d81d2006-01-17 08:47:12 +0000408}
409
Arjan van de Ven00977a52007-02-12 00:55:34 -0800410static const struct file_operations dev_fops = {
David Teigland869d81d2006-01-17 08:47:12 +0000411 .read = dev_read,
412 .write = dev_write,
413 .poll = dev_poll,
414 .owner = THIS_MODULE
415};
416
417static struct miscdevice plock_dev_misc = {
418 .minor = MISC_DYNAMIC_MINOR,
David Teigland24022112008-03-14 15:09:15 -0500419 .name = DLM_PLOCK_MISC_NAME,
David Teigland869d81d2006-01-17 08:47:12 +0000420 .fops = &dev_fops
421};
422
David Teigland24022112008-03-14 15:09:15 -0500423int dlm_plock_init(void)
David Teigland869d81d2006-01-17 08:47:12 +0000424{
425 int rv;
426
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);
432
433 rv = misc_register(&plock_dev_misc);
434 if (rv)
David Teigland24022112008-03-14 15:09:15 -0500435 log_print("dlm_plock_init: misc_register failed %d", rv);
David Teigland869d81d2006-01-17 08:47:12 +0000436 return rv;
437}
438
David Teigland24022112008-03-14 15:09:15 -0500439void dlm_plock_exit(void)
David Teigland869d81d2006-01-17 08:47:12 +0000440{
441 if (misc_deregister(&plock_dev_misc) < 0)
David Teigland24022112008-03-14 15:09:15 -0500442 log_print("dlm_plock_exit: misc_deregister failed");
David Teigland869d81d2006-01-17 08:47:12 +0000443}
444