blob: 66d6c16bf440b480c57b11ca8cf4ada22f7c65fc [file] [log] [blame]
David Teigland597d0ca2006-07-12 16:44:04 -05001/*
David Teigland7fe2b312010-02-24 11:08:18 -06002 * Copyright (C) 2006-2010 Red Hat, Inc. All rights reserved.
David Teigland597d0ca2006-07-12 16:44:04 -05003 *
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/init.h>
11#include <linux/wait.h>
12#include <linux/module.h>
13#include <linux/file.h>
14#include <linux/fs.h>
15#include <linux/poll.h>
16#include <linux/signal.h>
17#include <linux/spinlock.h>
18#include <linux/dlm.h>
19#include <linux/dlm_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
David Teigland597d0ca2006-07-12 16:44:04 -050021
22#include "dlm_internal.h"
23#include "lockspace.h"
24#include "lock.h"
25#include "lvb_table.h"
Adrian Bunk84c6e8c2007-02-26 00:18:42 +010026#include "user.h"
David Teigland597d0ca2006-07-12 16:44:04 -050027
Denis Cheng0fe410d2008-01-29 13:50:16 +080028static const char name_prefix[] = "dlm";
Arjan van de Ven00977a52007-02-12 00:55:34 -080029static const struct file_operations device_fops;
David Teiglanddc68c7e2008-08-18 11:43:30 -050030static atomic_t dlm_monitor_opened;
31static int dlm_monitor_unused = 1;
David Teigland597d0ca2006-07-12 16:44:04 -050032
33#ifdef CONFIG_COMPAT
34
35struct dlm_lock_params32 {
36 __u8 mode;
37 __u8 namelen;
David Teiglandd7db9232007-05-18 09:00:32 -050038 __u16 unused;
39 __u32 flags;
David Teigland597d0ca2006-07-12 16:44:04 -050040 __u32 lkid;
41 __u32 parent;
David Teiglandd7db9232007-05-18 09:00:32 -050042 __u64 xid;
43 __u64 timeout;
David Teigland597d0ca2006-07-12 16:44:04 -050044 __u32 castparam;
45 __u32 castaddr;
46 __u32 bastparam;
47 __u32 bastaddr;
48 __u32 lksb;
David Teigland597d0ca2006-07-12 16:44:04 -050049 char lvb[DLM_USER_LVB_LEN];
50 char name[0];
51};
52
53struct dlm_write_request32 {
54 __u32 version[3];
55 __u8 cmd;
56 __u8 is64bit;
57 __u8 unused[2];
58
59 union {
60 struct dlm_lock_params32 lock;
61 struct dlm_lspace_params lspace;
David Teigland72c2be72007-03-30 15:06:16 -050062 struct dlm_purge_params purge;
David Teigland597d0ca2006-07-12 16:44:04 -050063 } i;
64};
65
66struct dlm_lksb32 {
67 __u32 sb_status;
68 __u32 sb_lkid;
69 __u8 sb_flags;
70 __u32 sb_lvbptr;
71};
72
73struct dlm_lock_result32 {
David Teiglandd7db9232007-05-18 09:00:32 -050074 __u32 version[3];
David Teigland597d0ca2006-07-12 16:44:04 -050075 __u32 length;
76 __u32 user_astaddr;
77 __u32 user_astparam;
78 __u32 user_lksb;
79 struct dlm_lksb32 lksb;
80 __u8 bast_mode;
81 __u8 unused[3];
82 /* Offsets may be zero if no data is present */
83 __u32 lvb_offset;
84};
85
86static void compat_input(struct dlm_write_request *kb,
Patrick Caulfeld2a792892008-01-17 10:25:28 +000087 struct dlm_write_request32 *kb32,
David Teigland1fecb1c2009-03-04 11:17:23 -060088 int namelen)
David Teigland597d0ca2006-07-12 16:44:04 -050089{
90 kb->version[0] = kb32->version[0];
91 kb->version[1] = kb32->version[1];
92 kb->version[2] = kb32->version[2];
93
94 kb->cmd = kb32->cmd;
95 kb->is64bit = kb32->is64bit;
96 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
97 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
98 kb->i.lspace.flags = kb32->i.lspace.flags;
99 kb->i.lspace.minor = kb32->i.lspace.minor;
David Teigland1fecb1c2009-03-04 11:17:23 -0600100 memcpy(kb->i.lspace.name, kb32->i.lspace.name, namelen);
David Teigland72c2be72007-03-30 15:06:16 -0500101 } else if (kb->cmd == DLM_USER_PURGE) {
102 kb->i.purge.nodeid = kb32->i.purge.nodeid;
103 kb->i.purge.pid = kb32->i.purge.pid;
David Teigland597d0ca2006-07-12 16:44:04 -0500104 } else {
105 kb->i.lock.mode = kb32->i.lock.mode;
106 kb->i.lock.namelen = kb32->i.lock.namelen;
107 kb->i.lock.flags = kb32->i.lock.flags;
108 kb->i.lock.lkid = kb32->i.lock.lkid;
109 kb->i.lock.parent = kb32->i.lock.parent;
David Teiglandd7db9232007-05-18 09:00:32 -0500110 kb->i.lock.xid = kb32->i.lock.xid;
111 kb->i.lock.timeout = kb32->i.lock.timeout;
David Teigland597d0ca2006-07-12 16:44:04 -0500112 kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
113 kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
114 kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
115 kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
116 kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
117 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
David Teigland1fecb1c2009-03-04 11:17:23 -0600118 memcpy(kb->i.lock.name, kb32->i.lock.name, namelen);
David Teigland597d0ca2006-07-12 16:44:04 -0500119 }
120}
121
122static void compat_output(struct dlm_lock_result *res,
123 struct dlm_lock_result32 *res32)
124{
David Teiglandd7db9232007-05-18 09:00:32 -0500125 res32->version[0] = res->version[0];
126 res32->version[1] = res->version[1];
127 res32->version[2] = res->version[2];
128
David Teigland597d0ca2006-07-12 16:44:04 -0500129 res32->user_astaddr = (__u32)(long)res->user_astaddr;
130 res32->user_astparam = (__u32)(long)res->user_astparam;
131 res32->user_lksb = (__u32)(long)res->user_lksb;
132 res32->bast_mode = res->bast_mode;
133
134 res32->lvb_offset = res->lvb_offset;
135 res32->length = res->length;
136
137 res32->lksb.sb_status = res->lksb.sb_status;
138 res32->lksb.sb_flags = res->lksb.sb_flags;
139 res32->lksb.sb_lkid = res->lksb.sb_lkid;
140 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
141}
142#endif
143
David Teigland84d8cd62007-05-29 08:44:23 -0500144/* Figure out if this lock is at the end of its life and no longer
145 available for the application to use. The lkb still exists until
146 the final ast is read. A lock becomes EOL in three situations:
147 1. a noqueue request fails with EAGAIN
148 2. an unlock completes with EUNLOCK
149 3. a cancel of a waiting request completes with ECANCEL/EDEADLK
150 An EOL lock needs to be removed from the process's list of locks.
151 And we can't allow any new operation on an EOL lock. This is
152 not related to the lifetime of the lkb struct which is managed
153 entirely by refcount. */
154
155static int lkb_is_endoflife(struct dlm_lkb *lkb, int sb_status, int type)
156{
157 switch (sb_status) {
158 case -DLM_EUNLOCK:
159 return 1;
160 case -DLM_ECANCEL:
161 case -ETIMEDOUT:
David Teigland8b4021f2007-05-29 08:46:00 -0500162 case -EDEADLK:
David Teigland84d8cd62007-05-29 08:44:23 -0500163 if (lkb->lkb_grmode == DLM_LOCK_IV)
164 return 1;
165 break;
166 case -EAGAIN:
167 if (type == AST_COMP && lkb->lkb_grmode == DLM_LOCK_IV)
168 return 1;
169 break;
170 }
171 return 0;
172}
173
David Teiglandef0c2bb2007-03-28 09:56:46 -0500174/* we could possibly check if the cancel of an orphan has resulted in the lkb
175 being removed and then remove that lkb from the orphans list and free it */
David Teigland597d0ca2006-07-12 16:44:04 -0500176
David Teigland7fe2b312010-02-24 11:08:18 -0600177void dlm_user_add_ast(struct dlm_lkb *lkb, int type, int mode)
David Teigland597d0ca2006-07-12 16:44:04 -0500178{
179 struct dlm_ls *ls;
180 struct dlm_user_args *ua;
181 struct dlm_user_proc *proc;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500182 int eol = 0, ast_type;
David Teigland597d0ca2006-07-12 16:44:04 -0500183
David Teiglandef0c2bb2007-03-28 09:56:46 -0500184 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
David Teigland597d0ca2006-07-12 16:44:04 -0500185 return;
David Teigland597d0ca2006-07-12 16:44:04 -0500186
187 ls = lkb->lkb_resource->res_ls;
188 mutex_lock(&ls->ls_clear_proc_locks);
189
190 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
191 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
David Teiglandef0c2bb2007-03-28 09:56:46 -0500192 lkb->ua so we can't try to use it. This second check is necessary
193 for cases where a completion ast is received for an operation that
194 began before clear_proc_locks did its cancel/unlock. */
David Teigland597d0ca2006-07-12 16:44:04 -0500195
David Teiglandef0c2bb2007-03-28 09:56:46 -0500196 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
David Teigland597d0ca2006-07-12 16:44:04 -0500197 goto out;
David Teigland597d0ca2006-07-12 16:44:04 -0500198
David Teiglandd292c0c2008-02-06 23:27:04 -0600199 DLM_ASSERT(lkb->lkb_ua, dlm_print_lkb(lkb););
200 ua = lkb->lkb_ua;
David Teigland597d0ca2006-07-12 16:44:04 -0500201 proc = ua->proc;
202
203 if (type == AST_BAST && ua->bastaddr == NULL)
204 goto out;
205
206 spin_lock(&proc->asts_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500207
208 ast_type = lkb->lkb_ast_type;
209 lkb->lkb_ast_type |= type;
David Teigland7fe2b312010-02-24 11:08:18 -0600210 if (type == AST_BAST)
211 lkb->lkb_bastmode = mode;
212 else
213 lkb->lkb_castmode = mode;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500214
215 if (!ast_type) {
David Teigland597d0ca2006-07-12 16:44:04 -0500216 kref_get(&lkb->lkb_ref);
217 list_add_tail(&lkb->lkb_astqueue, &proc->asts);
David Teigland89d799d2010-03-25 14:23:13 -0500218 lkb->lkb_ast_first = type;
David Teigland597d0ca2006-07-12 16:44:04 -0500219 wake_up_interruptible(&proc->wait);
220 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500221 if (type == AST_COMP && (ast_type & AST_COMP))
222 log_debug(ls, "ast overlap %x status %x %x",
223 lkb->lkb_id, ua->lksb.sb_status, lkb->lkb_flags);
David Teigland597d0ca2006-07-12 16:44:04 -0500224
David Teigland84d8cd62007-05-29 08:44:23 -0500225 eol = lkb_is_endoflife(lkb, ua->lksb.sb_status, type);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500226 if (eol) {
David Teiglandef0c2bb2007-03-28 09:56:46 -0500227 lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
228 }
David Teiglanda1bc86e2007-01-15 10:34:52 -0600229
David Teigland597d0ca2006-07-12 16:44:04 -0500230 /* We want to copy the lvb to userspace when the completion
231 ast is read if the status is 0, the lock has an lvb and
232 lvb_ops says we should. We could probably have set_lvb_lock()
233 set update_user_lvb instead and not need old_mode */
234
235 if ((lkb->lkb_ast_type & AST_COMP) &&
236 (lkb->lkb_lksb->sb_status == 0) &&
237 lkb->lkb_lksb->sb_lvbptr &&
238 dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
239 ua->update_user_lvb = 1;
240 else
241 ua->update_user_lvb = 0;
242
243 spin_unlock(&proc->asts_spin);
David Teigland34e22be2006-07-18 11:24:04 -0500244
David Teiglandef0c2bb2007-03-28 09:56:46 -0500245 if (eol) {
David Teiglandce5246b2008-01-14 15:48:58 -0600246 spin_lock(&proc->locks_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500247 if (!list_empty(&lkb->lkb_ownqueue)) {
248 list_del_init(&lkb->lkb_ownqueue);
249 dlm_put_lkb(lkb);
250 }
David Teiglandce5246b2008-01-14 15:48:58 -0600251 spin_unlock(&proc->locks_spin);
David Teigland34e22be2006-07-18 11:24:04 -0500252 }
David Teigland597d0ca2006-07-12 16:44:04 -0500253 out:
254 mutex_unlock(&ls->ls_clear_proc_locks);
255}
256
257static int device_user_lock(struct dlm_user_proc *proc,
258 struct dlm_lock_params *params)
259{
260 struct dlm_ls *ls;
261 struct dlm_user_args *ua;
262 int error = -ENOMEM;
263
264 ls = dlm_find_lockspace_local(proc->lockspace);
265 if (!ls)
266 return -ENOENT;
267
268 if (!params->castaddr || !params->lksb) {
269 error = -EINVAL;
270 goto out;
271 }
272
David Teigland573c24c2009-11-30 16:34:43 -0600273 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -0500274 if (!ua)
275 goto out;
276 ua->proc = proc;
277 ua->user_lksb = params->lksb;
278 ua->castparam = params->castparam;
279 ua->castaddr = params->castaddr;
280 ua->bastparam = params->bastparam;
281 ua->bastaddr = params->bastaddr;
David Teiglandd7db9232007-05-18 09:00:32 -0500282 ua->xid = params->xid;
David Teigland597d0ca2006-07-12 16:44:04 -0500283
284 if (params->flags & DLM_LKF_CONVERT)
285 error = dlm_user_convert(ls, ua,
286 params->mode, params->flags,
David Teiglandd7db9232007-05-18 09:00:32 -0500287 params->lkid, params->lvb,
288 (unsigned long) params->timeout);
David Teigland597d0ca2006-07-12 16:44:04 -0500289 else {
290 error = dlm_user_request(ls, ua,
291 params->mode, params->flags,
292 params->name, params->namelen,
David Teiglandd7db9232007-05-18 09:00:32 -0500293 (unsigned long) params->timeout);
David Teigland597d0ca2006-07-12 16:44:04 -0500294 if (!error)
295 error = ua->lksb.sb_lkid;
296 }
297 out:
298 dlm_put_lockspace(ls);
299 return error;
300}
301
302static int device_user_unlock(struct dlm_user_proc *proc,
303 struct dlm_lock_params *params)
304{
305 struct dlm_ls *ls;
306 struct dlm_user_args *ua;
307 int error = -ENOMEM;
308
309 ls = dlm_find_lockspace_local(proc->lockspace);
310 if (!ls)
311 return -ENOENT;
312
David Teigland573c24c2009-11-30 16:34:43 -0600313 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -0500314 if (!ua)
315 goto out;
316 ua->proc = proc;
317 ua->user_lksb = params->lksb;
318 ua->castparam = params->castparam;
319 ua->castaddr = params->castaddr;
320
321 if (params->flags & DLM_LKF_CANCEL)
322 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
323 else
324 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
325 params->lvb);
326 out:
327 dlm_put_lockspace(ls);
328 return error;
329}
330
David Teigland8b4021f2007-05-29 08:46:00 -0500331static int device_user_deadlock(struct dlm_user_proc *proc,
332 struct dlm_lock_params *params)
333{
334 struct dlm_ls *ls;
335 int error;
336
337 ls = dlm_find_lockspace_local(proc->lockspace);
338 if (!ls)
339 return -ENOENT;
340
341 error = dlm_user_deadlock(ls, params->flags, params->lkid);
342
343 dlm_put_lockspace(ls);
344 return error;
345}
346
David Teigland0f8e0d92008-08-06 13:30:24 -0500347static int dlm_device_register(struct dlm_ls *ls, char *name)
Patrick Caulfield254da032007-03-21 09:23:53 +0000348{
349 int error, len;
350
David Teigland0f8e0d92008-08-06 13:30:24 -0500351 /* The device is already registered. This happens when the
352 lockspace is created multiple times from userspace. */
353 if (ls->ls_device.name)
354 return 0;
355
Patrick Caulfield254da032007-03-21 09:23:53 +0000356 error = -ENOMEM;
357 len = strlen(name) + strlen(name_prefix) + 2;
David Teigland573c24c2009-11-30 16:34:43 -0600358 ls->ls_device.name = kzalloc(len, GFP_NOFS);
Patrick Caulfield254da032007-03-21 09:23:53 +0000359 if (!ls->ls_device.name)
360 goto fail;
361
362 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
363 name);
364 ls->ls_device.fops = &device_fops;
365 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
366
367 error = misc_register(&ls->ls_device);
368 if (error) {
369 kfree(ls->ls_device.name);
370 }
371fail:
372 return error;
373}
374
David Teigland0f8e0d92008-08-06 13:30:24 -0500375int dlm_device_deregister(struct dlm_ls *ls)
376{
377 int error;
378
379 /* The device is not registered. This happens when the lockspace
380 was never used from userspace, or when device_create_lockspace()
381 calls dlm_release_lockspace() after the register fails. */
382 if (!ls->ls_device.name)
383 return 0;
384
385 error = misc_deregister(&ls->ls_device);
386 if (!error)
387 kfree(ls->ls_device.name);
388 return error;
389}
390
David Teigland72c2be72007-03-30 15:06:16 -0500391static int device_user_purge(struct dlm_user_proc *proc,
392 struct dlm_purge_params *params)
393{
394 struct dlm_ls *ls;
395 int error;
396
397 ls = dlm_find_lockspace_local(proc->lockspace);
398 if (!ls)
399 return -ENOENT;
400
401 error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
402
403 dlm_put_lockspace(ls);
404 return error;
405}
406
David Teigland597d0ca2006-07-12 16:44:04 -0500407static int device_create_lockspace(struct dlm_lspace_params *params)
408{
409 dlm_lockspace_t *lockspace;
410 struct dlm_ls *ls;
Patrick Caulfield254da032007-03-21 09:23:53 +0000411 int error;
David Teigland597d0ca2006-07-12 16:44:04 -0500412
413 if (!capable(CAP_SYS_ADMIN))
414 return -EPERM;
415
416 error = dlm_new_lockspace(params->name, strlen(params->name),
David Teigland3ae1acf2007-05-18 08:59:31 -0500417 &lockspace, params->flags, DLM_USER_LVB_LEN);
David Teigland597d0ca2006-07-12 16:44:04 -0500418 if (error)
419 return error;
420
421 ls = dlm_find_lockspace_local(lockspace);
422 if (!ls)
423 return -ENOENT;
424
David Teigland0f8e0d92008-08-06 13:30:24 -0500425 error = dlm_device_register(ls, params->name);
David Teigland597d0ca2006-07-12 16:44:04 -0500426 dlm_put_lockspace(ls);
David Teigland597d0ca2006-07-12 16:44:04 -0500427
Patrick Caulfield254da032007-03-21 09:23:53 +0000428 if (error)
429 dlm_release_lockspace(lockspace, 0);
430 else
431 error = ls->ls_device.minor;
432
David Teigland597d0ca2006-07-12 16:44:04 -0500433 return error;
434}
435
436static int device_remove_lockspace(struct dlm_lspace_params *params)
437{
438 dlm_lockspace_t *lockspace;
439 struct dlm_ls *ls;
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500440 int error, force = 0;
David Teigland597d0ca2006-07-12 16:44:04 -0500441
442 if (!capable(CAP_SYS_ADMIN))
443 return -EPERM;
444
445 ls = dlm_find_lockspace_device(params->minor);
446 if (!ls)
447 return -ENOENT;
448
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500449 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
450 force = 2;
451
David Teigland597d0ca2006-07-12 16:44:04 -0500452 lockspace = ls->ls_local_handle;
David Teigland597d0ca2006-07-12 16:44:04 -0500453 dlm_put_lockspace(ls);
David Teigland0f8e0d92008-08-06 13:30:24 -0500454
455 /* The final dlm_release_lockspace waits for references to go to
456 zero, so all processes will need to close their device for the
457 ls before the release will proceed. release also calls the
458 device_deregister above. Converting a positive return value
459 from release to zero means that userspace won't know when its
460 release was the final one, but it shouldn't need to know. */
461
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500462 error = dlm_release_lockspace(lockspace, force);
David Teigland0f8e0d92008-08-06 13:30:24 -0500463 if (error > 0)
464 error = 0;
David Teigland597d0ca2006-07-12 16:44:04 -0500465 return error;
466}
467
468/* Check the user's version matches ours */
469static int check_version(struct dlm_write_request *req)
470{
471 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
472 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
473 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
474
475 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
476 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
477 current->comm,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700478 task_pid_nr(current),
David Teigland597d0ca2006-07-12 16:44:04 -0500479 req->version[0],
480 req->version[1],
481 req->version[2],
482 DLM_DEVICE_VERSION_MAJOR,
483 DLM_DEVICE_VERSION_MINOR,
484 DLM_DEVICE_VERSION_PATCH);
485 return -EINVAL;
486 }
487 return 0;
488}
489
490/*
491 * device_write
492 *
493 * device_user_lock
494 * dlm_user_request -> request_lock
495 * dlm_user_convert -> convert_lock
496 *
497 * device_user_unlock
498 * dlm_user_unlock -> unlock_lock
499 * dlm_user_cancel -> cancel_lock
500 *
501 * device_create_lockspace
502 * dlm_new_lockspace
503 *
504 * device_remove_lockspace
505 * dlm_release_lockspace
506 */
507
508/* a write to a lockspace device is a lock or unlock request, a write
509 to the control device is to create/remove a lockspace */
510
511static ssize_t device_write(struct file *file, const char __user *buf,
512 size_t count, loff_t *ppos)
513{
514 struct dlm_user_proc *proc = file->private_data;
515 struct dlm_write_request *kbuf;
516 sigset_t tmpsig, allsigs;
517 int error;
518
519#ifdef CONFIG_COMPAT
520 if (count < sizeof(struct dlm_write_request32))
521#else
522 if (count < sizeof(struct dlm_write_request))
523#endif
524 return -EINVAL;
525
David Teigland573c24c2009-11-30 16:34:43 -0600526 kbuf = kzalloc(count + 1, GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -0500527 if (!kbuf)
528 return -ENOMEM;
529
530 if (copy_from_user(kbuf, buf, count)) {
531 error = -EFAULT;
532 goto out_free;
533 }
534
535 if (check_version(kbuf)) {
536 error = -EBADE;
537 goto out_free;
538 }
539
540#ifdef CONFIG_COMPAT
541 if (!kbuf->is64bit) {
542 struct dlm_write_request32 *k32buf;
David Teigland1fecb1c2009-03-04 11:17:23 -0600543 int namelen = 0;
544
545 if (count > sizeof(struct dlm_write_request32))
546 namelen = count - sizeof(struct dlm_write_request32);
547
David Teigland597d0ca2006-07-12 16:44:04 -0500548 k32buf = (struct dlm_write_request32 *)kbuf;
David Teigland1fecb1c2009-03-04 11:17:23 -0600549
550 /* add 1 after namelen so that the name string is terminated */
551 kbuf = kzalloc(sizeof(struct dlm_write_request) + namelen + 1,
David Teigland573c24c2009-11-30 16:34:43 -0600552 GFP_NOFS);
David Teiglandcb980d92008-07-29 15:21:19 -0500553 if (!kbuf) {
554 kfree(k32buf);
David Teigland597d0ca2006-07-12 16:44:04 -0500555 return -ENOMEM;
David Teiglandcb980d92008-07-29 15:21:19 -0500556 }
David Teigland597d0ca2006-07-12 16:44:04 -0500557
558 if (proc)
559 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
David Teigland1fecb1c2009-03-04 11:17:23 -0600560
561 compat_input(kbuf, k32buf, namelen);
David Teigland597d0ca2006-07-12 16:44:04 -0500562 kfree(k32buf);
563 }
564#endif
565
566 /* do we really need this? can a write happen after a close? */
567 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
David Teiglandcb980d92008-07-29 15:21:19 -0500568 (proc && test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))) {
569 error = -EINVAL;
570 goto out_free;
571 }
David Teigland597d0ca2006-07-12 16:44:04 -0500572
573 sigfillset(&allsigs);
574 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
575
576 error = -EINVAL;
577
578 switch (kbuf->cmd)
579 {
580 case DLM_USER_LOCK:
581 if (!proc) {
582 log_print("no locking on control device");
583 goto out_sig;
584 }
585 error = device_user_lock(proc, &kbuf->i.lock);
586 break;
587
588 case DLM_USER_UNLOCK:
589 if (!proc) {
590 log_print("no locking on control device");
591 goto out_sig;
592 }
593 error = device_user_unlock(proc, &kbuf->i.lock);
594 break;
595
David Teigland8b4021f2007-05-29 08:46:00 -0500596 case DLM_USER_DEADLOCK:
597 if (!proc) {
598 log_print("no locking on control device");
599 goto out_sig;
600 }
601 error = device_user_deadlock(proc, &kbuf->i.lock);
602 break;
603
David Teigland597d0ca2006-07-12 16:44:04 -0500604 case DLM_USER_CREATE_LOCKSPACE:
605 if (proc) {
606 log_print("create/remove only on control device");
607 goto out_sig;
608 }
609 error = device_create_lockspace(&kbuf->i.lspace);
610 break;
611
612 case DLM_USER_REMOVE_LOCKSPACE:
613 if (proc) {
614 log_print("create/remove only on control device");
615 goto out_sig;
616 }
617 error = device_remove_lockspace(&kbuf->i.lspace);
618 break;
619
David Teigland72c2be72007-03-30 15:06:16 -0500620 case DLM_USER_PURGE:
621 if (!proc) {
622 log_print("no locking on control device");
623 goto out_sig;
624 }
625 error = device_user_purge(proc, &kbuf->i.purge);
626 break;
627
David Teigland597d0ca2006-07-12 16:44:04 -0500628 default:
629 log_print("Unknown command passed to DLM device : %d\n",
630 kbuf->cmd);
631 }
632
633 out_sig:
634 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
635 recalc_sigpending();
636 out_free:
637 kfree(kbuf);
638 return error;
639}
640
641/* Every process that opens the lockspace device has its own "proc" structure
642 hanging off the open file that's used to keep track of locks owned by the
643 process and asts that need to be delivered to the process. */
644
645static int device_open(struct inode *inode, struct file *file)
646{
647 struct dlm_user_proc *proc;
648 struct dlm_ls *ls;
649
650 ls = dlm_find_lockspace_device(iminor(inode));
David Teiglandf9f2ed42008-09-04 12:51:20 -0500651 if (!ls)
David Teigland597d0ca2006-07-12 16:44:04 -0500652 return -ENOENT;
653
David Teigland573c24c2009-11-30 16:34:43 -0600654 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_NOFS);
David Teigland597d0ca2006-07-12 16:44:04 -0500655 if (!proc) {
656 dlm_put_lockspace(ls);
657 return -ENOMEM;
658 }
659
660 proc->lockspace = ls->ls_local_handle;
661 INIT_LIST_HEAD(&proc->asts);
662 INIT_LIST_HEAD(&proc->locks);
David Teiglanda1bc86e2007-01-15 10:34:52 -0600663 INIT_LIST_HEAD(&proc->unlocking);
David Teigland597d0ca2006-07-12 16:44:04 -0500664 spin_lock_init(&proc->asts_spin);
665 spin_lock_init(&proc->locks_spin);
666 init_waitqueue_head(&proc->wait);
667 file->private_data = proc;
668
669 return 0;
670}
671
672static int device_close(struct inode *inode, struct file *file)
673{
674 struct dlm_user_proc *proc = file->private_data;
675 struct dlm_ls *ls;
676 sigset_t tmpsig, allsigs;
677
678 ls = dlm_find_lockspace_local(proc->lockspace);
679 if (!ls)
680 return -ENOENT;
681
682 sigfillset(&allsigs);
683 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
684
685 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
686
687 dlm_clear_proc_locks(ls, proc);
688
689 /* at this point no more lkb's should exist for this lockspace,
690 so there's no chance of dlm_user_add_ast() being called and
691 looking for lkb->ua->proc */
692
693 kfree(proc);
694 file->private_data = NULL;
695
696 dlm_put_lockspace(ls);
697 dlm_put_lockspace(ls); /* for the find in device_open() */
698
699 /* FIXME: AUTOFREE: if this ls is no longer used do
700 device_remove_lockspace() */
701
702 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
703 recalc_sigpending();
704
705 return 0;
706}
707
708static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
David Teigland89d799d2010-03-25 14:23:13 -0500709 int mode, char __user *buf, size_t count)
David Teigland597d0ca2006-07-12 16:44:04 -0500710{
711#ifdef CONFIG_COMPAT
712 struct dlm_lock_result32 result32;
713#endif
714 struct dlm_lock_result result;
715 void *resultptr;
716 int error=0;
717 int len;
718 int struct_len;
719
720 memset(&result, 0, sizeof(struct dlm_lock_result));
David Teiglandd7db9232007-05-18 09:00:32 -0500721 result.version[0] = DLM_DEVICE_VERSION_MAJOR;
722 result.version[1] = DLM_DEVICE_VERSION_MINOR;
723 result.version[2] = DLM_DEVICE_VERSION_PATCH;
David Teigland597d0ca2006-07-12 16:44:04 -0500724 memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
725 result.user_lksb = ua->user_lksb;
726
727 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
728 in a conversion unless the conversion is successful. See code
729 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
730 notes that a new blocking AST address and parameter are set even if
731 the conversion fails, so maybe we should just do that. */
732
733 if (type == AST_BAST) {
734 result.user_astaddr = ua->bastaddr;
735 result.user_astparam = ua->bastparam;
David Teigland89d799d2010-03-25 14:23:13 -0500736 result.bast_mode = mode;
David Teigland597d0ca2006-07-12 16:44:04 -0500737 } else {
738 result.user_astaddr = ua->castaddr;
739 result.user_astparam = ua->castparam;
740 }
741
742#ifdef CONFIG_COMPAT
743 if (compat)
744 len = sizeof(struct dlm_lock_result32);
745 else
746#endif
747 len = sizeof(struct dlm_lock_result);
748 struct_len = len;
749
750 /* copy lvb to userspace if there is one, it's been updated, and
751 the user buffer has space for it */
752
753 if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
754 count >= len + DLM_USER_LVB_LEN) {
755 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
756 DLM_USER_LVB_LEN)) {
757 error = -EFAULT;
758 goto out;
759 }
760
761 result.lvb_offset = len;
762 len += DLM_USER_LVB_LEN;
763 }
764
765 result.length = len;
766 resultptr = &result;
767#ifdef CONFIG_COMPAT
768 if (compat) {
769 compat_output(&result, &result32);
770 resultptr = &result32;
771 }
772#endif
773
774 if (copy_to_user(buf, resultptr, struct_len))
775 error = -EFAULT;
776 else
777 error = len;
778 out:
779 return error;
780}
781
David Teiglandd7db9232007-05-18 09:00:32 -0500782static int copy_version_to_user(char __user *buf, size_t count)
783{
784 struct dlm_device_version ver;
785
786 memset(&ver, 0, sizeof(struct dlm_device_version));
787 ver.version[0] = DLM_DEVICE_VERSION_MAJOR;
788 ver.version[1] = DLM_DEVICE_VERSION_MINOR;
789 ver.version[2] = DLM_DEVICE_VERSION_PATCH;
790
791 if (copy_to_user(buf, &ver, sizeof(struct dlm_device_version)))
792 return -EFAULT;
793 return sizeof(struct dlm_device_version);
794}
795
David Teigland597d0ca2006-07-12 16:44:04 -0500796/* a read returns a single ast described in a struct dlm_lock_result */
797
798static ssize_t device_read(struct file *file, char __user *buf, size_t count,
799 loff_t *ppos)
800{
801 struct dlm_user_proc *proc = file->private_data;
802 struct dlm_lkb *lkb;
David Teigland597d0ca2006-07-12 16:44:04 -0500803 DECLARE_WAITQUEUE(wait, current);
David Teigland89d799d2010-03-25 14:23:13 -0500804 int error = 0, removed;
805 int ret_type, ret_mode;
806 int bastmode, castmode, do_bast, do_cast;
David Teigland597d0ca2006-07-12 16:44:04 -0500807
David Teiglandd7db9232007-05-18 09:00:32 -0500808 if (count == sizeof(struct dlm_device_version)) {
809 error = copy_version_to_user(buf, count);
810 return error;
811 }
812
813 if (!proc) {
814 log_print("non-version read from control device %zu", count);
815 return -EINVAL;
816 }
817
David Teigland597d0ca2006-07-12 16:44:04 -0500818#ifdef CONFIG_COMPAT
819 if (count < sizeof(struct dlm_lock_result32))
820#else
821 if (count < sizeof(struct dlm_lock_result))
822#endif
823 return -EINVAL;
824
David Teigland89d799d2010-03-25 14:23:13 -0500825 try_another:
826
David Teigland597d0ca2006-07-12 16:44:04 -0500827 /* do we really need this? can a read happen after a close? */
828 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
829 return -EINVAL;
830
831 spin_lock(&proc->asts_spin);
832 if (list_empty(&proc->asts)) {
833 if (file->f_flags & O_NONBLOCK) {
834 spin_unlock(&proc->asts_spin);
835 return -EAGAIN;
836 }
837
838 add_wait_queue(&proc->wait, &wait);
839
840 repeat:
841 set_current_state(TASK_INTERRUPTIBLE);
842 if (list_empty(&proc->asts) && !signal_pending(current)) {
843 spin_unlock(&proc->asts_spin);
844 schedule();
845 spin_lock(&proc->asts_spin);
846 goto repeat;
847 }
848 set_current_state(TASK_RUNNING);
849 remove_wait_queue(&proc->wait, &wait);
850
851 if (signal_pending(current)) {
852 spin_unlock(&proc->asts_spin);
853 return -ERESTARTSYS;
854 }
855 }
856
David Teigland597d0ca2006-07-12 16:44:04 -0500857 /* there may be both completion and blocking asts to return for
858 the lkb, don't remove lkb from asts list unless no asts remain */
859
860 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
861
David Teigland89d799d2010-03-25 14:23:13 -0500862 removed = 0;
863 ret_type = 0;
864 ret_mode = 0;
865 do_bast = lkb->lkb_ast_type & AST_BAST;
866 do_cast = lkb->lkb_ast_type & AST_COMP;
867 bastmode = lkb->lkb_bastmode;
868 castmode = lkb->lkb_castmode;
869
870 /* when both are queued figure out which to do first and
871 switch first so the other goes in the next read */
872
873 if (do_cast && do_bast) {
874 if (lkb->lkb_ast_first == AST_COMP) {
875 ret_type = AST_COMP;
876 ret_mode = castmode;
877 lkb->lkb_ast_type &= ~AST_COMP;
878 lkb->lkb_ast_first = AST_BAST;
879 } else {
880 ret_type = AST_BAST;
881 ret_mode = bastmode;
882 lkb->lkb_ast_type &= ~AST_BAST;
883 lkb->lkb_ast_first = AST_COMP;
884 }
885 } else {
886 ret_type = lkb->lkb_ast_first;
887 ret_mode = (ret_type == AST_COMP) ? castmode : bastmode;
888 lkb->lkb_ast_type &= ~ret_type;
889 lkb->lkb_ast_first = 0;
890 }
891
892 /* if we're doing a bast but the bast is unnecessary, then
893 switch to do nothing or do a cast if that was needed next */
894
895 if ((ret_type == AST_BAST) &&
896 dlm_modes_compat(bastmode, lkb->lkb_castmode_done)) {
897 ret_type = 0;
898 ret_mode = 0;
899
900 if (do_cast) {
901 ret_type = AST_COMP;
902 ret_mode = castmode;
903 lkb->lkb_ast_type &= ~AST_COMP;
904 lkb->lkb_ast_first = 0;
905 }
906 }
907
908 if (lkb->lkb_ast_first != lkb->lkb_ast_type) {
909 log_print("device_read %x ast_first %x ast_type %x",
910 lkb->lkb_id, lkb->lkb_ast_first, lkb->lkb_ast_type);
David Teigland597d0ca2006-07-12 16:44:04 -0500911 }
912
913 if (!lkb->lkb_ast_type) {
914 list_del(&lkb->lkb_astqueue);
915 removed = 1;
916 }
917 spin_unlock(&proc->asts_spin);
918
David Teigland89d799d2010-03-25 14:23:13 -0500919 if (ret_type) {
920 error = copy_result_to_user(lkb->lkb_ua,
921 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
922 ret_type, ret_mode, buf, count);
923
924 if (ret_type == AST_COMP)
925 lkb->lkb_castmode_done = castmode;
926 if (ret_type == AST_BAST)
927 lkb->lkb_bastmode_done = bastmode;
928 }
David Teigland597d0ca2006-07-12 16:44:04 -0500929
930 /* removes reference for the proc->asts lists added by
931 dlm_user_add_ast() and may result in the lkb being freed */
David Teigland89d799d2010-03-25 14:23:13 -0500932
David Teigland597d0ca2006-07-12 16:44:04 -0500933 if (removed)
934 dlm_put_lkb(lkb);
935
David Teigland89d799d2010-03-25 14:23:13 -0500936 /* the bast that was queued was eliminated (see unnecessary above),
937 leaving nothing to return */
938
939 if (!ret_type)
940 goto try_another;
941
David Teigland597d0ca2006-07-12 16:44:04 -0500942 return error;
943}
944
945static unsigned int device_poll(struct file *file, poll_table *wait)
946{
947 struct dlm_user_proc *proc = file->private_data;
948
949 poll_wait(file, &proc->wait, wait);
950
951 spin_lock(&proc->asts_spin);
952 if (!list_empty(&proc->asts)) {
953 spin_unlock(&proc->asts_spin);
954 return POLLIN | POLLRDNORM;
955 }
956 spin_unlock(&proc->asts_spin);
957 return 0;
958}
959
David Teiglanddc68c7e2008-08-18 11:43:30 -0500960int dlm_user_daemon_available(void)
961{
962 /* dlm_controld hasn't started (or, has started, but not
963 properly populated configfs) */
964
965 if (!dlm_our_nodeid())
966 return 0;
967
968 /* This is to deal with versions of dlm_controld that don't
969 know about the monitor device. We assume that if the
970 dlm_controld was started (above), but the monitor device
971 was never opened, that it's an old version. dlm_controld
972 should open the monitor device before populating configfs. */
973
974 if (dlm_monitor_unused)
975 return 1;
976
977 return atomic_read(&dlm_monitor_opened) ? 1 : 0;
978}
979
David Teigland597d0ca2006-07-12 16:44:04 -0500980static int ctl_device_open(struct inode *inode, struct file *file)
981{
982 file->private_data = NULL;
983 return 0;
984}
985
986static int ctl_device_close(struct inode *inode, struct file *file)
987{
988 return 0;
989}
990
David Teiglanddc68c7e2008-08-18 11:43:30 -0500991static int monitor_device_open(struct inode *inode, struct file *file)
992{
993 atomic_inc(&dlm_monitor_opened);
994 dlm_monitor_unused = 0;
995 return 0;
996}
997
998static int monitor_device_close(struct inode *inode, struct file *file)
999{
1000 if (atomic_dec_and_test(&dlm_monitor_opened))
1001 dlm_stop_lockspaces();
1002 return 0;
1003}
1004
Arjan van de Ven00977a52007-02-12 00:55:34 -08001005static const struct file_operations device_fops = {
David Teigland597d0ca2006-07-12 16:44:04 -05001006 .open = device_open,
1007 .release = device_close,
1008 .read = device_read,
1009 .write = device_write,
1010 .poll = device_poll,
1011 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001012 .llseek = noop_llseek,
David Teigland597d0ca2006-07-12 16:44:04 -05001013};
1014
Arjan van de Ven00977a52007-02-12 00:55:34 -08001015static const struct file_operations ctl_device_fops = {
David Teigland597d0ca2006-07-12 16:44:04 -05001016 .open = ctl_device_open,
1017 .release = ctl_device_close,
David Teiglandd7db9232007-05-18 09:00:32 -05001018 .read = device_read,
David Teigland597d0ca2006-07-12 16:44:04 -05001019 .write = device_write,
1020 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001021 .llseek = noop_llseek,
David Teigland597d0ca2006-07-12 16:44:04 -05001022};
1023
Denis Cheng0fe410d2008-01-29 13:50:16 +08001024static struct miscdevice ctl_device = {
1025 .name = "dlm-control",
1026 .fops = &ctl_device_fops,
1027 .minor = MISC_DYNAMIC_MINOR,
1028};
1029
David Teiglanddc68c7e2008-08-18 11:43:30 -05001030static const struct file_operations monitor_device_fops = {
1031 .open = monitor_device_open,
1032 .release = monitor_device_close,
1033 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001034 .llseek = noop_llseek,
David Teiglanddc68c7e2008-08-18 11:43:30 -05001035};
1036
1037static struct miscdevice monitor_device = {
1038 .name = "dlm-monitor",
1039 .fops = &monitor_device_fops,
1040 .minor = MISC_DYNAMIC_MINOR,
1041};
1042
Denis Cheng30727172008-02-02 01:53:46 +08001043int __init dlm_user_init(void)
David Teigland597d0ca2006-07-12 16:44:04 -05001044{
1045 int error;
1046
David Teiglanddc68c7e2008-08-18 11:43:30 -05001047 atomic_set(&dlm_monitor_opened, 0);
David Teigland597d0ca2006-07-12 16:44:04 -05001048
David Teiglanddc68c7e2008-08-18 11:43:30 -05001049 error = misc_register(&ctl_device);
1050 if (error) {
1051 log_print("misc_register failed for control device");
1052 goto out;
1053 }
1054
1055 error = misc_register(&monitor_device);
1056 if (error) {
1057 log_print("misc_register failed for monitor device");
1058 misc_deregister(&ctl_device);
1059 }
1060 out:
David Teigland597d0ca2006-07-12 16:44:04 -05001061 return error;
1062}
1063
1064void dlm_user_exit(void)
1065{
1066 misc_deregister(&ctl_device);
David Teiglanddc68c7e2008-08-18 11:43:30 -05001067 misc_deregister(&monitor_device);
David Teigland597d0ca2006-07-12 16:44:04 -05001068}
1069