blob: e2b87800436427ab2783118876ddeacfa2ace9cd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Teigland869d81d2006-01-17 08:47:12 +000015
David Teigland24022112008-03-14 15:09:15 -050016#include "dlm_internal.h"
17#include "lockspace.h"
David Teigland869d81d2006-01-17 08:47:12 +000018
19static spinlock_t ops_lock;
20static struct list_head send_list;
21static struct list_head recv_list;
22static wait_queue_head_t send_wq;
23static wait_queue_head_t recv_wq;
24
25struct plock_op {
26 struct list_head list;
27 int done;
David Teigland24022112008-03-14 15:09:15 -050028 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +000029};
30
Marc Eshel586759f2006-11-14 16:37:25 -050031struct plock_xop {
32 struct plock_op xop;
33 void *callback;
34 void *fl;
35 void *file;
36 struct file_lock flc;
37};
38
39
David Teigland24022112008-03-14 15:09:15 -050040static inline void set_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000041{
David Teigland24022112008-03-14 15:09:15 -050042 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
43 info->version[1] = DLM_PLOCK_VERSION_MINOR;
44 info->version[2] = DLM_PLOCK_VERSION_PATCH;
David Teigland869d81d2006-01-17 08:47:12 +000045}
46
David Teigland24022112008-03-14 15:09:15 -050047static int check_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000048{
David Teigland24022112008-03-14 15:09:15 -050049 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
50 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
51 log_print("plock device version mismatch: "
David Teigland869d81d2006-01-17 08:47:12 +000052 "kernel (%u.%u.%u), user (%u.%u.%u)",
David Teigland24022112008-03-14 15:09:15 -050053 DLM_PLOCK_VERSION_MAJOR,
54 DLM_PLOCK_VERSION_MINOR,
55 DLM_PLOCK_VERSION_PATCH,
David Teigland869d81d2006-01-17 08:47:12 +000056 info->version[0],
57 info->version[1],
58 info->version[2]);
59 return -EINVAL;
60 }
61 return 0;
62}
63
64static 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
David Teigland901025d2011-03-02 14:20:04 -060074/* 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
80static 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->fl_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
David Teigland24022112008-03-14 15:09:15 -0500104int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
105 int cmd, struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000106{
David Teigland24022112008-03-14 15:09:15 -0500107 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000108 struct plock_op *op;
Marc Eshel586759f2006-11-14 16:37:25 -0500109 struct plock_xop *xop;
David Teigland869d81d2006-01-17 08:47:12 +0000110 int rv;
111
David Teigland24022112008-03-14 15:09:15 -0500112 ls = dlm_find_lockspace_local(lockspace);
113 if (!ls)
114 return -EINVAL;
115
David Teigland573c24c2009-11-30 16:34:43 -0600116 xop = kzalloc(sizeof(*xop), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500117 if (!xop) {
118 rv = -ENOMEM;
119 goto out;
120 }
David Teigland869d81d2006-01-17 08:47:12 +0000121
Marc Eshel586759f2006-11-14 16:37:25 -0500122 op = &xop->xop;
David Teigland24022112008-03-14 15:09:15 -0500123 op->info.optype = DLM_PLOCK_OP_LOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400124 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000125 op->info.ex = (fl->fl_type == F_WRLCK);
126 op->info.wait = IS_SETLKW(cmd);
David Teigland24022112008-03-14 15:09:15 -0500127 op->info.fsid = ls->ls_global_id;
128 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000129 op->info.start = fl->fl_start;
130 op->info.end = fl->fl_end;
Marc Eshel586759f2006-11-14 16:37:25 -0500131 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
David Teigland2066b582007-12-06 09:35:25 -0600132 /* fl_owner is lockd which doesn't distinguish
133 processes on the nfs client */
134 op->info.owner = (__u64) fl->fl_pid;
Marc Eshel586759f2006-11-14 16:37:25 -0500135 xop->callback = fl->fl_lmops->fl_grant;
136 locks_init_lock(&xop->flc);
137 locks_copy_lock(&xop->flc, fl);
138 xop->fl = fl;
139 xop->file = file;
David Teigland2066b582007-12-06 09:35:25 -0600140 } else {
141 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -0500142 xop->callback = NULL;
David Teigland2066b582007-12-06 09:35:25 -0600143 }
David Teigland869d81d2006-01-17 08:47:12 +0000144
145 send_op(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500146
David Teigland901025d2011-03-02 14:20:04 -0600147 if (xop->callback == NULL) {
148 rv = wait_event_killable(recv_wq, (op->done != 0));
149 if (rv == -ERESTARTSYS) {
150 log_debug(ls, "dlm_posix_lock: wait killed %llx",
151 (unsigned long long)number);
152 spin_lock(&ops_lock);
153 list_del(&op->list);
154 spin_unlock(&ops_lock);
155 kfree(xop);
156 do_unlock_close(ls, number, file, fl);
157 goto out;
158 }
159 } else {
Miklos Szeredibde74e42008-07-25 01:48:57 -0700160 rv = FILE_LOCK_DEFERRED;
David Teigland24022112008-03-14 15:09:15 -0500161 goto out;
162 }
David Teigland869d81d2006-01-17 08:47:12 +0000163
164 spin_lock(&ops_lock);
165 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500166 log_error(ls, "dlm_posix_lock: op on list %llx",
167 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000168 list_del(&op->list);
169 }
170 spin_unlock(&ops_lock);
171
172 rv = op->info.rv;
173
174 if (!rv) {
175 if (posix_lock_file_wait(file, fl) < 0)
David Teigland24022112008-03-14 15:09:15 -0500176 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
177 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000178 }
179
Marc Eshel586759f2006-11-14 16:37:25 -0500180 kfree(xop);
David Teigland24022112008-03-14 15:09:15 -0500181out:
182 dlm_put_lockspace(ls);
Marc Eshel586759f2006-11-14 16:37:25 -0500183 return rv;
184}
David Teigland24022112008-03-14 15:09:15 -0500185EXPORT_SYMBOL_GPL(dlm_posix_lock);
Marc Eshel586759f2006-11-14 16:37:25 -0500186
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200187/* Returns failure iff a successful lock operation should be canceled */
David Teigland24022112008-03-14 15:09:15 -0500188static int dlm_plock_callback(struct plock_op *op)
Marc Eshel586759f2006-11-14 16:37:25 -0500189{
190 struct file *file;
191 struct file_lock *fl;
192 struct file_lock *flc;
193 int (*notify)(void *, void *, int) = NULL;
194 struct plock_xop *xop = (struct plock_xop *)op;
195 int rv = 0;
196
197 spin_lock(&ops_lock);
198 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500199 log_print("dlm_plock_callback: op on list %llx",
200 (unsigned long long)op->info.number);
Marc Eshel586759f2006-11-14 16:37:25 -0500201 list_del(&op->list);
202 }
203 spin_unlock(&ops_lock);
204
205 /* check if the following 2 are still valid or make a copy */
206 file = xop->file;
207 flc = &xop->flc;
208 fl = xop->fl;
209 notify = xop->callback;
210
211 if (op->info.rv) {
David Teigland24179f42009-01-19 13:13:33 -0600212 notify(fl, NULL, op->info.rv);
Marc Eshel586759f2006-11-14 16:37:25 -0500213 goto out;
214 }
215
216 /* got fs lock; bookkeep locally as well: */
217 flc->fl_flags &= ~FL_SLEEP;
218 if (posix_lock_file(file, flc, NULL)) {
219 /*
220 * This can only happen in the case of kmalloc() failure.
221 * The filesystem's own lock is the authoritative lock,
222 * so a failure to get the lock locally is not a disaster.
David Teigland24022112008-03-14 15:09:15 -0500223 * As long as the fs cannot reliably cancel locks (especially
Marc Eshel586759f2006-11-14 16:37:25 -0500224 * in a low-memory situation), we're better off ignoring
225 * this failure than trying to recover.
226 */
David Teigland24022112008-03-14 15:09:15 -0500227 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
228 (unsigned long long)op->info.number, file, fl);
Marc Eshel586759f2006-11-14 16:37:25 -0500229 }
230
David Teigland24179f42009-01-19 13:13:33 -0600231 rv = notify(fl, NULL, 0);
Marc Eshel586759f2006-11-14 16:37:25 -0500232 if (rv) {
233 /* XXX: We need to cancel the fs lock here: */
David Teigland24022112008-03-14 15:09:15 -0500234 log_print("dlm_plock_callback: lock granted after lock request "
235 "failed; dangling lock!\n");
Marc Eshel586759f2006-11-14 16:37:25 -0500236 goto out;
237 }
238
239out:
240 kfree(xop);
David Teigland869d81d2006-01-17 08:47:12 +0000241 return rv;
242}
243
David Teigland24022112008-03-14 15:09:15 -0500244int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
245 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000246{
David Teigland24022112008-03-14 15:09:15 -0500247 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000248 struct plock_op *op;
249 int rv;
250
David Teigland24022112008-03-14 15:09:15 -0500251 ls = dlm_find_lockspace_local(lockspace);
252 if (!ls)
253 return -EINVAL;
254
David Teigland573c24c2009-11-30 16:34:43 -0600255 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500256 if (!op) {
257 rv = -ENOMEM;
258 goto out;
259 }
David Teigland869d81d2006-01-17 08:47:12 +0000260
261 if (posix_lock_file_wait(file, fl) < 0)
David Teigland24022112008-03-14 15:09:15 -0500262 log_error(ls, "dlm_posix_unlock: vfs unlock error %llx",
263 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000264
David Teigland24022112008-03-14 15:09:15 -0500265 op->info.optype = DLM_PLOCK_OP_UNLOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400266 op->info.pid = fl->fl_pid;
David Teigland24022112008-03-14 15:09:15 -0500267 op->info.fsid = ls->ls_global_id;
268 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000269 op->info.start = fl->fl_start;
270 op->info.end = fl->fl_end;
David Teigland2066b582007-12-06 09:35:25 -0600271 if (fl->fl_lmops && fl->fl_lmops->fl_grant)
272 op->info.owner = (__u64) fl->fl_pid;
273 else
274 op->info.owner = (__u64)(long) fl->fl_owner;
David Teigland869d81d2006-01-17 08:47:12 +0000275
David Teigland901025d2011-03-02 14:20:04 -0600276 if (fl->fl_flags & FL_CLOSE) {
277 op->info.flags |= DLM_PLOCK_FL_CLOSE;
278 send_op(op);
279 rv = 0;
280 goto out;
281 }
282
David Teigland869d81d2006-01-17 08:47:12 +0000283 send_op(op);
284 wait_event(recv_wq, (op->done != 0));
285
286 spin_lock(&ops_lock);
287 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500288 log_error(ls, "dlm_posix_unlock: op on list %llx",
289 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000290 list_del(&op->list);
291 }
292 spin_unlock(&ops_lock);
293
294 rv = op->info.rv;
295
Marc Eshel586759f2006-11-14 16:37:25 -0500296 if (rv == -ENOENT)
297 rv = 0;
298
David Teigland869d81d2006-01-17 08:47:12 +0000299 kfree(op);
David Teigland24022112008-03-14 15:09:15 -0500300out:
301 dlm_put_lockspace(ls);
David Teigland869d81d2006-01-17 08:47:12 +0000302 return rv;
303}
David Teigland24022112008-03-14 15:09:15 -0500304EXPORT_SYMBOL_GPL(dlm_posix_unlock);
David Teigland869d81d2006-01-17 08:47:12 +0000305
David Teigland24022112008-03-14 15:09:15 -0500306int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
307 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000308{
David Teigland24022112008-03-14 15:09:15 -0500309 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000310 struct plock_op *op;
311 int rv;
312
David Teigland24022112008-03-14 15:09:15 -0500313 ls = dlm_find_lockspace_local(lockspace);
314 if (!ls)
315 return -EINVAL;
David Teigland869d81d2006-01-17 08:47:12 +0000316
David Teigland573c24c2009-11-30 16:34:43 -0600317 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500318 if (!op) {
319 rv = -ENOMEM;
320 goto out;
321 }
322
323 op->info.optype = DLM_PLOCK_OP_GET;
David Teigland3a2a9c92006-04-25 15:45:51 -0400324 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000325 op->info.ex = (fl->fl_type == F_WRLCK);
David Teigland24022112008-03-14 15:09:15 -0500326 op->info.fsid = ls->ls_global_id;
327 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000328 op->info.start = fl->fl_start;
329 op->info.end = fl->fl_end;
David Teigland2066b582007-12-06 09:35:25 -0600330 if (fl->fl_lmops && fl->fl_lmops->fl_grant)
331 op->info.owner = (__u64) fl->fl_pid;
332 else
333 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -0500334
David Teigland869d81d2006-01-17 08:47:12 +0000335 send_op(op);
336 wait_event(recv_wq, (op->done != 0));
337
338 spin_lock(&ops_lock);
339 if (!list_empty(&op->list)) {
David Teigland24022112008-03-14 15:09:15 -0500340 log_error(ls, "dlm_posix_get: op on list %llx",
341 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000342 list_del(&op->list);
343 }
344 spin_unlock(&ops_lock);
345
David Teiglanda7a2ff82007-06-08 17:01:40 -0500346 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
347 -ENOENT if there are no locks on the file */
348
David Teigland869d81d2006-01-17 08:47:12 +0000349 rv = op->info.rv;
350
Marc Eshel586759f2006-11-14 16:37:25 -0500351 fl->fl_type = F_UNLCK;
352 if (rv == -ENOENT)
353 rv = 0;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500354 else if (rv > 0) {
Jeff Layton20d5a392009-01-21 11:34:50 -0500355 locks_init_lock(fl);
David Teigland869d81d2006-01-17 08:47:12 +0000356 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
Jeff Layton20d5a392009-01-21 11:34:50 -0500357 fl->fl_flags = FL_POSIX;
David Teigland869d81d2006-01-17 08:47:12 +0000358 fl->fl_pid = op->info.pid;
359 fl->fl_start = op->info.start;
360 fl->fl_end = op->info.end;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500361 rv = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000362 }
363
364 kfree(op);
David Teigland24022112008-03-14 15:09:15 -0500365out:
366 dlm_put_lockspace(ls);
David Teigland869d81d2006-01-17 08:47:12 +0000367 return rv;
368}
David Teigland24022112008-03-14 15:09:15 -0500369EXPORT_SYMBOL_GPL(dlm_posix_get);
David Teigland869d81d2006-01-17 08:47:12 +0000370
371/* a read copies out one plock request from the send list */
372static ssize_t dev_read(struct file *file, char __user *u, size_t count,
373 loff_t *ppos)
374{
David Teigland24022112008-03-14 15:09:15 -0500375 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000376 struct plock_op *op = NULL;
377
378 if (count < sizeof(info))
379 return -EINVAL;
380
381 spin_lock(&ops_lock);
382 if (!list_empty(&send_list)) {
383 op = list_entry(send_list.next, struct plock_op, list);
David Teigland901025d2011-03-02 14:20:04 -0600384 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
385 list_del(&op->list);
386 else
387 list_move(&op->list, &recv_list);
David Teigland869d81d2006-01-17 08:47:12 +0000388 memcpy(&info, &op->info, sizeof(info));
389 }
390 spin_unlock(&ops_lock);
391
392 if (!op)
393 return -EAGAIN;
394
David Teigland901025d2011-03-02 14:20:04 -0600395 /* there is no need to get a reply from userspace for unlocks
396 that were generated by the vfs cleaning up for a close
397 (the process did not make an unlock call). */
398
399 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
400 kfree(op);
401
David Teigland869d81d2006-01-17 08:47:12 +0000402 if (copy_to_user(u, &info, sizeof(info)))
403 return -EFAULT;
404 return sizeof(info);
405}
406
407/* a write copies in one plock result that should match a plock_op
408 on the recv list */
409static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
410 loff_t *ppos)
411{
David Teigland24022112008-03-14 15:09:15 -0500412 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000413 struct plock_op *op;
David Teiglandc78a87d2009-06-18 13:20:24 -0500414 int found = 0, do_callback = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000415
416 if (count != sizeof(info))
417 return -EINVAL;
418
419 if (copy_from_user(&info, u, sizeof(info)))
420 return -EFAULT;
421
422 if (check_version(&info))
423 return -EINVAL;
424
425 spin_lock(&ops_lock);
426 list_for_each_entry(op, &recv_list, list) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500427 if (op->info.fsid == info.fsid &&
428 op->info.number == info.number &&
David Teigland08eac932006-08-04 16:19:20 -0500429 op->info.owner == info.owner) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500430 struct plock_xop *xop = (struct plock_xop *)op;
David Teigland869d81d2006-01-17 08:47:12 +0000431 list_del_init(&op->list);
David Teigland869d81d2006-01-17 08:47:12 +0000432 memcpy(&op->info, &info, sizeof(info));
David Teiglandc78a87d2009-06-18 13:20:24 -0500433 if (xop->callback)
434 do_callback = 1;
435 else
436 op->done = 1;
437 found = 1;
David Teigland869d81d2006-01-17 08:47:12 +0000438 break;
439 }
440 }
441 spin_unlock(&ops_lock);
442
Marc Eshel586759f2006-11-14 16:37:25 -0500443 if (found) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500444 if (do_callback)
David Teigland817d10b2008-05-13 14:28:26 -0500445 dlm_plock_callback(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500446 else
447 wake_up(&recv_wq);
448 } else
David Teigland24022112008-03-14 15:09:15 -0500449 log_print("dev_write no op %x %llx", info.fsid,
450 (unsigned long long)info.number);
David Teigland869d81d2006-01-17 08:47:12 +0000451 return count;
452}
453
454static unsigned int dev_poll(struct file *file, poll_table *wait)
455{
Denis Chengcee23c72007-07-25 17:53:58 +0800456 unsigned int mask = 0;
457
David Teigland869d81d2006-01-17 08:47:12 +0000458 poll_wait(file, &send_wq, wait);
459
460 spin_lock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800461 if (!list_empty(&send_list))
462 mask = POLLIN | POLLRDNORM;
David Teigland869d81d2006-01-17 08:47:12 +0000463 spin_unlock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800464
465 return mask;
David Teigland869d81d2006-01-17 08:47:12 +0000466}
467
Arjan van de Ven00977a52007-02-12 00:55:34 -0800468static const struct file_operations dev_fops = {
David Teigland869d81d2006-01-17 08:47:12 +0000469 .read = dev_read,
470 .write = dev_write,
471 .poll = dev_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200472 .owner = THIS_MODULE,
473 .llseek = noop_llseek,
David Teigland869d81d2006-01-17 08:47:12 +0000474};
475
476static struct miscdevice plock_dev_misc = {
477 .minor = MISC_DYNAMIC_MINOR,
David Teigland24022112008-03-14 15:09:15 -0500478 .name = DLM_PLOCK_MISC_NAME,
David Teigland869d81d2006-01-17 08:47:12 +0000479 .fops = &dev_fops
480};
481
David Teigland24022112008-03-14 15:09:15 -0500482int dlm_plock_init(void)
David Teigland869d81d2006-01-17 08:47:12 +0000483{
484 int rv;
485
486 spin_lock_init(&ops_lock);
487 INIT_LIST_HEAD(&send_list);
488 INIT_LIST_HEAD(&recv_list);
489 init_waitqueue_head(&send_wq);
490 init_waitqueue_head(&recv_wq);
491
492 rv = misc_register(&plock_dev_misc);
493 if (rv)
David Teigland24022112008-03-14 15:09:15 -0500494 log_print("dlm_plock_init: misc_register failed %d", rv);
David Teigland869d81d2006-01-17 08:47:12 +0000495 return rv;
496}
497
David Teigland24022112008-03-14 15:09:15 -0500498void dlm_plock_exit(void)
David Teigland869d81d2006-01-17 08:47:12 +0000499{
500 if (misc_deregister(&plock_dev_misc) < 0)
David Teigland24022112008-03-14 15:09:15 -0500501 log_print("dlm_plock_exit: misc_deregister failed");
David Teigland869d81d2006-01-17 08:47:12 +0000502}
503