blob: 329da1b5285f39a201b9835e86dd5eecf2426072 [file] [log] [blame]
David Teigland597d0ca2006-07-12 16:44:04 -05001/*
David Teiglandef0c2bb2007-03-28 09:56:46 -05002 * Copyright (C) 2006-2007 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>
20
21#include "dlm_internal.h"
22#include "lockspace.h"
23#include "lock.h"
24#include "lvb_table.h"
Adrian Bunk84c6e8c2007-02-26 00:18:42 +010025#include "user.h"
David Teigland597d0ca2006-07-12 16:44:04 -050026
27static const char *name_prefix="dlm";
28static struct miscdevice ctl_device;
Arjan van de Ven00977a52007-02-12 00:55:34 -080029static const struct file_operations device_fops;
David Teigland597d0ca2006-07-12 16:44:04 -050030
31#ifdef CONFIG_COMPAT
32
33struct dlm_lock_params32 {
34 __u8 mode;
35 __u8 namelen;
David Teiglandd7db9232007-05-18 09:00:32 -050036 __u16 unused;
37 __u32 flags;
David Teigland597d0ca2006-07-12 16:44:04 -050038 __u32 lkid;
39 __u32 parent;
David Teiglandd7db9232007-05-18 09:00:32 -050040 __u64 xid;
41 __u64 timeout;
David Teigland597d0ca2006-07-12 16:44:04 -050042 __u32 castparam;
43 __u32 castaddr;
44 __u32 bastparam;
45 __u32 bastaddr;
46 __u32 lksb;
David Teigland597d0ca2006-07-12 16:44:04 -050047 char lvb[DLM_USER_LVB_LEN];
48 char name[0];
49};
50
51struct dlm_write_request32 {
52 __u32 version[3];
53 __u8 cmd;
54 __u8 is64bit;
55 __u8 unused[2];
56
57 union {
58 struct dlm_lock_params32 lock;
59 struct dlm_lspace_params lspace;
David Teigland72c2be72007-03-30 15:06:16 -050060 struct dlm_purge_params purge;
David Teigland597d0ca2006-07-12 16:44:04 -050061 } i;
62};
63
64struct dlm_lksb32 {
65 __u32 sb_status;
66 __u32 sb_lkid;
67 __u8 sb_flags;
68 __u32 sb_lvbptr;
69};
70
71struct dlm_lock_result32 {
David Teiglandd7db9232007-05-18 09:00:32 -050072 __u32 version[3];
David Teigland597d0ca2006-07-12 16:44:04 -050073 __u32 length;
74 __u32 user_astaddr;
75 __u32 user_astparam;
76 __u32 user_lksb;
77 struct dlm_lksb32 lksb;
78 __u8 bast_mode;
79 __u8 unused[3];
80 /* Offsets may be zero if no data is present */
81 __u32 lvb_offset;
82};
83
84static void compat_input(struct dlm_write_request *kb,
85 struct dlm_write_request32 *kb32)
86{
87 kb->version[0] = kb32->version[0];
88 kb->version[1] = kb32->version[1];
89 kb->version[2] = kb32->version[2];
90
91 kb->cmd = kb32->cmd;
92 kb->is64bit = kb32->is64bit;
93 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
94 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
95 kb->i.lspace.flags = kb32->i.lspace.flags;
96 kb->i.lspace.minor = kb32->i.lspace.minor;
97 strcpy(kb->i.lspace.name, kb32->i.lspace.name);
David Teigland72c2be72007-03-30 15:06:16 -050098 } else if (kb->cmd == DLM_USER_PURGE) {
99 kb->i.purge.nodeid = kb32->i.purge.nodeid;
100 kb->i.purge.pid = kb32->i.purge.pid;
David Teigland597d0ca2006-07-12 16:44:04 -0500101 } else {
102 kb->i.lock.mode = kb32->i.lock.mode;
103 kb->i.lock.namelen = kb32->i.lock.namelen;
104 kb->i.lock.flags = kb32->i.lock.flags;
105 kb->i.lock.lkid = kb32->i.lock.lkid;
106 kb->i.lock.parent = kb32->i.lock.parent;
David Teiglandd7db9232007-05-18 09:00:32 -0500107 kb->i.lock.xid = kb32->i.lock.xid;
108 kb->i.lock.timeout = kb32->i.lock.timeout;
David Teigland597d0ca2006-07-12 16:44:04 -0500109 kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
110 kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
111 kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
112 kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
113 kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
114 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
115 memcpy(kb->i.lock.name, kb32->i.lock.name, kb->i.lock.namelen);
116 }
117}
118
119static void compat_output(struct dlm_lock_result *res,
120 struct dlm_lock_result32 *res32)
121{
David Teiglandd7db9232007-05-18 09:00:32 -0500122 res32->version[0] = res->version[0];
123 res32->version[1] = res->version[1];
124 res32->version[2] = res->version[2];
125
David Teigland597d0ca2006-07-12 16:44:04 -0500126 res32->user_astaddr = (__u32)(long)res->user_astaddr;
127 res32->user_astparam = (__u32)(long)res->user_astparam;
128 res32->user_lksb = (__u32)(long)res->user_lksb;
129 res32->bast_mode = res->bast_mode;
130
131 res32->lvb_offset = res->lvb_offset;
132 res32->length = res->length;
133
134 res32->lksb.sb_status = res->lksb.sb_status;
135 res32->lksb.sb_flags = res->lksb.sb_flags;
136 res32->lksb.sb_lkid = res->lksb.sb_lkid;
137 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
138}
139#endif
140
David Teigland84d8cd62007-05-29 08:44:23 -0500141/* Figure out if this lock is at the end of its life and no longer
142 available for the application to use. The lkb still exists until
143 the final ast is read. A lock becomes EOL in three situations:
144 1. a noqueue request fails with EAGAIN
145 2. an unlock completes with EUNLOCK
146 3. a cancel of a waiting request completes with ECANCEL/EDEADLK
147 An EOL lock needs to be removed from the process's list of locks.
148 And we can't allow any new operation on an EOL lock. This is
149 not related to the lifetime of the lkb struct which is managed
150 entirely by refcount. */
151
152static int lkb_is_endoflife(struct dlm_lkb *lkb, int sb_status, int type)
153{
154 switch (sb_status) {
155 case -DLM_EUNLOCK:
156 return 1;
157 case -DLM_ECANCEL:
158 case -ETIMEDOUT:
159 if (lkb->lkb_grmode == DLM_LOCK_IV)
160 return 1;
161 break;
162 case -EAGAIN:
163 if (type == AST_COMP && lkb->lkb_grmode == DLM_LOCK_IV)
164 return 1;
165 break;
166 }
167 return 0;
168}
169
David Teiglandef0c2bb2007-03-28 09:56:46 -0500170/* we could possibly check if the cancel of an orphan has resulted in the lkb
171 being removed and then remove that lkb from the orphans list and free it */
David Teigland597d0ca2006-07-12 16:44:04 -0500172
173void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
174{
175 struct dlm_ls *ls;
176 struct dlm_user_args *ua;
177 struct dlm_user_proc *proc;
David Teiglandef0c2bb2007-03-28 09:56:46 -0500178 int eol = 0, ast_type;
David Teigland597d0ca2006-07-12 16:44:04 -0500179
David Teiglandef0c2bb2007-03-28 09:56:46 -0500180 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
David Teigland597d0ca2006-07-12 16:44:04 -0500181 return;
David Teigland597d0ca2006-07-12 16:44:04 -0500182
183 ls = lkb->lkb_resource->res_ls;
184 mutex_lock(&ls->ls_clear_proc_locks);
185
186 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
187 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
David Teiglandef0c2bb2007-03-28 09:56:46 -0500188 lkb->ua so we can't try to use it. This second check is necessary
189 for cases where a completion ast is received for an operation that
190 began before clear_proc_locks did its cancel/unlock. */
David Teigland597d0ca2006-07-12 16:44:04 -0500191
David Teiglandef0c2bb2007-03-28 09:56:46 -0500192 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
David Teigland597d0ca2006-07-12 16:44:04 -0500193 goto out;
David Teigland597d0ca2006-07-12 16:44:04 -0500194
195 DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
196 ua = (struct dlm_user_args *)lkb->lkb_astparam;
197 proc = ua->proc;
198
199 if (type == AST_BAST && ua->bastaddr == NULL)
200 goto out;
201
202 spin_lock(&proc->asts_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500203
204 ast_type = lkb->lkb_ast_type;
205 lkb->lkb_ast_type |= type;
206
207 if (!ast_type) {
David Teigland597d0ca2006-07-12 16:44:04 -0500208 kref_get(&lkb->lkb_ref);
209 list_add_tail(&lkb->lkb_astqueue, &proc->asts);
David Teigland597d0ca2006-07-12 16:44:04 -0500210 wake_up_interruptible(&proc->wait);
211 }
David Teiglandef0c2bb2007-03-28 09:56:46 -0500212 if (type == AST_COMP && (ast_type & AST_COMP))
213 log_debug(ls, "ast overlap %x status %x %x",
214 lkb->lkb_id, ua->lksb.sb_status, lkb->lkb_flags);
David Teigland597d0ca2006-07-12 16:44:04 -0500215
David Teigland84d8cd62007-05-29 08:44:23 -0500216 eol = lkb_is_endoflife(lkb, ua->lksb.sb_status, type);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500217 if (eol) {
218 lkb->lkb_ast_type &= ~AST_BAST;
219 lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
220 }
David Teiglanda1bc86e2007-01-15 10:34:52 -0600221
David Teigland597d0ca2006-07-12 16:44:04 -0500222 /* We want to copy the lvb to userspace when the completion
223 ast is read if the status is 0, the lock has an lvb and
224 lvb_ops says we should. We could probably have set_lvb_lock()
225 set update_user_lvb instead and not need old_mode */
226
227 if ((lkb->lkb_ast_type & AST_COMP) &&
228 (lkb->lkb_lksb->sb_status == 0) &&
229 lkb->lkb_lksb->sb_lvbptr &&
230 dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
231 ua->update_user_lvb = 1;
232 else
233 ua->update_user_lvb = 0;
234
235 spin_unlock(&proc->asts_spin);
David Teigland34e22be2006-07-18 11:24:04 -0500236
David Teiglandef0c2bb2007-03-28 09:56:46 -0500237 if (eol) {
David Teigland34e22be2006-07-18 11:24:04 -0500238 spin_lock(&ua->proc->locks_spin);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500239 if (!list_empty(&lkb->lkb_ownqueue)) {
240 list_del_init(&lkb->lkb_ownqueue);
241 dlm_put_lkb(lkb);
242 }
David Teigland34e22be2006-07-18 11:24:04 -0500243 spin_unlock(&ua->proc->locks_spin);
David Teigland34e22be2006-07-18 11:24:04 -0500244 }
David Teigland597d0ca2006-07-12 16:44:04 -0500245 out:
246 mutex_unlock(&ls->ls_clear_proc_locks);
247}
248
249static int device_user_lock(struct dlm_user_proc *proc,
250 struct dlm_lock_params *params)
251{
252 struct dlm_ls *ls;
253 struct dlm_user_args *ua;
254 int error = -ENOMEM;
255
256 ls = dlm_find_lockspace_local(proc->lockspace);
257 if (!ls)
258 return -ENOENT;
259
260 if (!params->castaddr || !params->lksb) {
261 error = -EINVAL;
262 goto out;
263 }
264
265 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
266 if (!ua)
267 goto out;
268 ua->proc = proc;
269 ua->user_lksb = params->lksb;
270 ua->castparam = params->castparam;
271 ua->castaddr = params->castaddr;
272 ua->bastparam = params->bastparam;
273 ua->bastaddr = params->bastaddr;
David Teiglandd7db9232007-05-18 09:00:32 -0500274 ua->xid = params->xid;
David Teigland597d0ca2006-07-12 16:44:04 -0500275
276 if (params->flags & DLM_LKF_CONVERT)
277 error = dlm_user_convert(ls, ua,
278 params->mode, params->flags,
David Teiglandd7db9232007-05-18 09:00:32 -0500279 params->lkid, params->lvb,
280 (unsigned long) params->timeout);
David Teigland597d0ca2006-07-12 16:44:04 -0500281 else {
282 error = dlm_user_request(ls, ua,
283 params->mode, params->flags,
284 params->name, params->namelen,
David Teiglandd7db9232007-05-18 09:00:32 -0500285 (unsigned long) params->timeout);
David Teigland597d0ca2006-07-12 16:44:04 -0500286 if (!error)
287 error = ua->lksb.sb_lkid;
288 }
289 out:
290 dlm_put_lockspace(ls);
291 return error;
292}
293
294static int device_user_unlock(struct dlm_user_proc *proc,
295 struct dlm_lock_params *params)
296{
297 struct dlm_ls *ls;
298 struct dlm_user_args *ua;
299 int error = -ENOMEM;
300
301 ls = dlm_find_lockspace_local(proc->lockspace);
302 if (!ls)
303 return -ENOENT;
304
305 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
306 if (!ua)
307 goto out;
308 ua->proc = proc;
309 ua->user_lksb = params->lksb;
310 ua->castparam = params->castparam;
311 ua->castaddr = params->castaddr;
312
313 if (params->flags & DLM_LKF_CANCEL)
314 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
315 else
316 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
317 params->lvb);
318 out:
319 dlm_put_lockspace(ls);
320 return error;
321}
322
Patrick Caulfield254da032007-03-21 09:23:53 +0000323static int create_misc_device(struct dlm_ls *ls, char *name)
324{
325 int error, len;
326
327 error = -ENOMEM;
328 len = strlen(name) + strlen(name_prefix) + 2;
329 ls->ls_device.name = kzalloc(len, GFP_KERNEL);
330 if (!ls->ls_device.name)
331 goto fail;
332
333 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
334 name);
335 ls->ls_device.fops = &device_fops;
336 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
337
338 error = misc_register(&ls->ls_device);
339 if (error) {
340 kfree(ls->ls_device.name);
341 }
342fail:
343 return error;
344}
345
David Teigland72c2be72007-03-30 15:06:16 -0500346static int device_user_purge(struct dlm_user_proc *proc,
347 struct dlm_purge_params *params)
348{
349 struct dlm_ls *ls;
350 int error;
351
352 ls = dlm_find_lockspace_local(proc->lockspace);
353 if (!ls)
354 return -ENOENT;
355
356 error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
357
358 dlm_put_lockspace(ls);
359 return error;
360}
361
David Teigland597d0ca2006-07-12 16:44:04 -0500362static int device_create_lockspace(struct dlm_lspace_params *params)
363{
364 dlm_lockspace_t *lockspace;
365 struct dlm_ls *ls;
Patrick Caulfield254da032007-03-21 09:23:53 +0000366 int error;
David Teigland597d0ca2006-07-12 16:44:04 -0500367
368 if (!capable(CAP_SYS_ADMIN))
369 return -EPERM;
370
371 error = dlm_new_lockspace(params->name, strlen(params->name),
David Teigland3ae1acf2007-05-18 08:59:31 -0500372 &lockspace, params->flags, DLM_USER_LVB_LEN);
David Teigland597d0ca2006-07-12 16:44:04 -0500373 if (error)
374 return error;
375
376 ls = dlm_find_lockspace_local(lockspace);
377 if (!ls)
378 return -ENOENT;
379
Patrick Caulfield254da032007-03-21 09:23:53 +0000380 error = create_misc_device(ls, params->name);
David Teigland597d0ca2006-07-12 16:44:04 -0500381 dlm_put_lockspace(ls);
David Teigland597d0ca2006-07-12 16:44:04 -0500382
Patrick Caulfield254da032007-03-21 09:23:53 +0000383 if (error)
384 dlm_release_lockspace(lockspace, 0);
385 else
386 error = ls->ls_device.minor;
387
David Teigland597d0ca2006-07-12 16:44:04 -0500388 return error;
389}
390
391static int device_remove_lockspace(struct dlm_lspace_params *params)
392{
393 dlm_lockspace_t *lockspace;
394 struct dlm_ls *ls;
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500395 int error, force = 0;
David Teigland597d0ca2006-07-12 16:44:04 -0500396
397 if (!capable(CAP_SYS_ADMIN))
398 return -EPERM;
399
400 ls = dlm_find_lockspace_device(params->minor);
401 if (!ls)
402 return -ENOENT;
403
Patrick Caulfield254da032007-03-21 09:23:53 +0000404 /* Deregister the misc device first, so we don't have
405 * a device that's not attached to a lockspace. If
406 * dlm_release_lockspace fails then we can recreate it
407 */
David Teigland597d0ca2006-07-12 16:44:04 -0500408 error = misc_deregister(&ls->ls_device);
409 if (error) {
410 dlm_put_lockspace(ls);
411 goto out;
412 }
413 kfree(ls->ls_device.name);
414
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500415 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
416 force = 2;
417
David Teigland597d0ca2006-07-12 16:44:04 -0500418 lockspace = ls->ls_local_handle;
419
420 /* dlm_release_lockspace waits for references to go to zero,
421 so all processes will need to close their device for the ls
422 before the release will procede */
423
424 dlm_put_lockspace(ls);
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500425 error = dlm_release_lockspace(lockspace, force);
Patrick Caulfield254da032007-03-21 09:23:53 +0000426 if (error)
427 create_misc_device(ls, ls->ls_name);
David Teiglandc6e6f0b2006-08-30 10:50:18 -0500428 out:
David Teigland597d0ca2006-07-12 16:44:04 -0500429 return error;
430}
431
432/* Check the user's version matches ours */
433static int check_version(struct dlm_write_request *req)
434{
435 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
436 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
437 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
438
439 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
440 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
441 current->comm,
442 current->pid,
443 req->version[0],
444 req->version[1],
445 req->version[2],
446 DLM_DEVICE_VERSION_MAJOR,
447 DLM_DEVICE_VERSION_MINOR,
448 DLM_DEVICE_VERSION_PATCH);
449 return -EINVAL;
450 }
451 return 0;
452}
453
454/*
455 * device_write
456 *
457 * device_user_lock
458 * dlm_user_request -> request_lock
459 * dlm_user_convert -> convert_lock
460 *
461 * device_user_unlock
462 * dlm_user_unlock -> unlock_lock
463 * dlm_user_cancel -> cancel_lock
464 *
465 * device_create_lockspace
466 * dlm_new_lockspace
467 *
468 * device_remove_lockspace
469 * dlm_release_lockspace
470 */
471
472/* a write to a lockspace device is a lock or unlock request, a write
473 to the control device is to create/remove a lockspace */
474
475static ssize_t device_write(struct file *file, const char __user *buf,
476 size_t count, loff_t *ppos)
477{
478 struct dlm_user_proc *proc = file->private_data;
479 struct dlm_write_request *kbuf;
480 sigset_t tmpsig, allsigs;
481 int error;
482
483#ifdef CONFIG_COMPAT
484 if (count < sizeof(struct dlm_write_request32))
485#else
486 if (count < sizeof(struct dlm_write_request))
487#endif
488 return -EINVAL;
489
490 kbuf = kmalloc(count, GFP_KERNEL);
491 if (!kbuf)
492 return -ENOMEM;
493
494 if (copy_from_user(kbuf, buf, count)) {
495 error = -EFAULT;
496 goto out_free;
497 }
498
499 if (check_version(kbuf)) {
500 error = -EBADE;
501 goto out_free;
502 }
503
504#ifdef CONFIG_COMPAT
505 if (!kbuf->is64bit) {
506 struct dlm_write_request32 *k32buf;
507 k32buf = (struct dlm_write_request32 *)kbuf;
508 kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
509 sizeof(struct dlm_write_request32)), GFP_KERNEL);
510 if (!kbuf)
511 return -ENOMEM;
512
513 if (proc)
514 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
515 compat_input(kbuf, k32buf);
516 kfree(k32buf);
517 }
518#endif
519
520 /* do we really need this? can a write happen after a close? */
521 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
522 test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
523 return -EINVAL;
524
525 sigfillset(&allsigs);
526 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
527
528 error = -EINVAL;
529
530 switch (kbuf->cmd)
531 {
532 case DLM_USER_LOCK:
533 if (!proc) {
534 log_print("no locking on control device");
535 goto out_sig;
536 }
537 error = device_user_lock(proc, &kbuf->i.lock);
538 break;
539
540 case DLM_USER_UNLOCK:
541 if (!proc) {
542 log_print("no locking on control device");
543 goto out_sig;
544 }
545 error = device_user_unlock(proc, &kbuf->i.lock);
546 break;
547
548 case DLM_USER_CREATE_LOCKSPACE:
549 if (proc) {
550 log_print("create/remove only on control device");
551 goto out_sig;
552 }
553 error = device_create_lockspace(&kbuf->i.lspace);
554 break;
555
556 case DLM_USER_REMOVE_LOCKSPACE:
557 if (proc) {
558 log_print("create/remove only on control device");
559 goto out_sig;
560 }
561 error = device_remove_lockspace(&kbuf->i.lspace);
562 break;
563
David Teigland72c2be72007-03-30 15:06:16 -0500564 case DLM_USER_PURGE:
565 if (!proc) {
566 log_print("no locking on control device");
567 goto out_sig;
568 }
569 error = device_user_purge(proc, &kbuf->i.purge);
570 break;
571
David Teigland597d0ca2006-07-12 16:44:04 -0500572 default:
573 log_print("Unknown command passed to DLM device : %d\n",
574 kbuf->cmd);
575 }
576
577 out_sig:
578 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
579 recalc_sigpending();
580 out_free:
581 kfree(kbuf);
582 return error;
583}
584
585/* Every process that opens the lockspace device has its own "proc" structure
586 hanging off the open file that's used to keep track of locks owned by the
587 process and asts that need to be delivered to the process. */
588
589static int device_open(struct inode *inode, struct file *file)
590{
591 struct dlm_user_proc *proc;
592 struct dlm_ls *ls;
593
594 ls = dlm_find_lockspace_device(iminor(inode));
595 if (!ls)
596 return -ENOENT;
597
598 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
599 if (!proc) {
600 dlm_put_lockspace(ls);
601 return -ENOMEM;
602 }
603
604 proc->lockspace = ls->ls_local_handle;
605 INIT_LIST_HEAD(&proc->asts);
606 INIT_LIST_HEAD(&proc->locks);
David Teiglanda1bc86e2007-01-15 10:34:52 -0600607 INIT_LIST_HEAD(&proc->unlocking);
David Teigland597d0ca2006-07-12 16:44:04 -0500608 spin_lock_init(&proc->asts_spin);
609 spin_lock_init(&proc->locks_spin);
610 init_waitqueue_head(&proc->wait);
611 file->private_data = proc;
612
613 return 0;
614}
615
616static int device_close(struct inode *inode, struct file *file)
617{
618 struct dlm_user_proc *proc = file->private_data;
619 struct dlm_ls *ls;
620 sigset_t tmpsig, allsigs;
621
622 ls = dlm_find_lockspace_local(proc->lockspace);
623 if (!ls)
624 return -ENOENT;
625
626 sigfillset(&allsigs);
627 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
628
629 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
630
631 dlm_clear_proc_locks(ls, proc);
632
633 /* at this point no more lkb's should exist for this lockspace,
634 so there's no chance of dlm_user_add_ast() being called and
635 looking for lkb->ua->proc */
636
637 kfree(proc);
638 file->private_data = NULL;
639
640 dlm_put_lockspace(ls);
641 dlm_put_lockspace(ls); /* for the find in device_open() */
642
643 /* FIXME: AUTOFREE: if this ls is no longer used do
644 device_remove_lockspace() */
645
646 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
647 recalc_sigpending();
648
649 return 0;
650}
651
652static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
653 int bmode, char __user *buf, size_t count)
654{
655#ifdef CONFIG_COMPAT
656 struct dlm_lock_result32 result32;
657#endif
658 struct dlm_lock_result result;
659 void *resultptr;
660 int error=0;
661 int len;
662 int struct_len;
663
664 memset(&result, 0, sizeof(struct dlm_lock_result));
David Teiglandd7db9232007-05-18 09:00:32 -0500665 result.version[0] = DLM_DEVICE_VERSION_MAJOR;
666 result.version[1] = DLM_DEVICE_VERSION_MINOR;
667 result.version[2] = DLM_DEVICE_VERSION_PATCH;
David Teigland597d0ca2006-07-12 16:44:04 -0500668 memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
669 result.user_lksb = ua->user_lksb;
670
671 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
672 in a conversion unless the conversion is successful. See code
673 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
674 notes that a new blocking AST address and parameter are set even if
675 the conversion fails, so maybe we should just do that. */
676
677 if (type == AST_BAST) {
678 result.user_astaddr = ua->bastaddr;
679 result.user_astparam = ua->bastparam;
680 result.bast_mode = bmode;
681 } else {
682 result.user_astaddr = ua->castaddr;
683 result.user_astparam = ua->castparam;
684 }
685
686#ifdef CONFIG_COMPAT
687 if (compat)
688 len = sizeof(struct dlm_lock_result32);
689 else
690#endif
691 len = sizeof(struct dlm_lock_result);
692 struct_len = len;
693
694 /* copy lvb to userspace if there is one, it's been updated, and
695 the user buffer has space for it */
696
697 if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
698 count >= len + DLM_USER_LVB_LEN) {
699 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
700 DLM_USER_LVB_LEN)) {
701 error = -EFAULT;
702 goto out;
703 }
704
705 result.lvb_offset = len;
706 len += DLM_USER_LVB_LEN;
707 }
708
709 result.length = len;
710 resultptr = &result;
711#ifdef CONFIG_COMPAT
712 if (compat) {
713 compat_output(&result, &result32);
714 resultptr = &result32;
715 }
716#endif
717
718 if (copy_to_user(buf, resultptr, struct_len))
719 error = -EFAULT;
720 else
721 error = len;
722 out:
723 return error;
724}
725
David Teiglandd7db9232007-05-18 09:00:32 -0500726static int copy_version_to_user(char __user *buf, size_t count)
727{
728 struct dlm_device_version ver;
729
730 memset(&ver, 0, sizeof(struct dlm_device_version));
731 ver.version[0] = DLM_DEVICE_VERSION_MAJOR;
732 ver.version[1] = DLM_DEVICE_VERSION_MINOR;
733 ver.version[2] = DLM_DEVICE_VERSION_PATCH;
734
735 if (copy_to_user(buf, &ver, sizeof(struct dlm_device_version)))
736 return -EFAULT;
737 return sizeof(struct dlm_device_version);
738}
739
David Teigland597d0ca2006-07-12 16:44:04 -0500740/* a read returns a single ast described in a struct dlm_lock_result */
741
742static ssize_t device_read(struct file *file, char __user *buf, size_t count,
743 loff_t *ppos)
744{
745 struct dlm_user_proc *proc = file->private_data;
746 struct dlm_lkb *lkb;
747 struct dlm_user_args *ua;
748 DECLARE_WAITQUEUE(wait, current);
749 int error, type=0, bmode=0, removed = 0;
750
David Teiglandd7db9232007-05-18 09:00:32 -0500751 if (count == sizeof(struct dlm_device_version)) {
752 error = copy_version_to_user(buf, count);
753 return error;
754 }
755
756 if (!proc) {
757 log_print("non-version read from control device %zu", count);
758 return -EINVAL;
759 }
760
David Teigland597d0ca2006-07-12 16:44:04 -0500761#ifdef CONFIG_COMPAT
762 if (count < sizeof(struct dlm_lock_result32))
763#else
764 if (count < sizeof(struct dlm_lock_result))
765#endif
766 return -EINVAL;
767
768 /* do we really need this? can a read happen after a close? */
769 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
770 return -EINVAL;
771
772 spin_lock(&proc->asts_spin);
773 if (list_empty(&proc->asts)) {
774 if (file->f_flags & O_NONBLOCK) {
775 spin_unlock(&proc->asts_spin);
776 return -EAGAIN;
777 }
778
779 add_wait_queue(&proc->wait, &wait);
780
781 repeat:
782 set_current_state(TASK_INTERRUPTIBLE);
783 if (list_empty(&proc->asts) && !signal_pending(current)) {
784 spin_unlock(&proc->asts_spin);
785 schedule();
786 spin_lock(&proc->asts_spin);
787 goto repeat;
788 }
789 set_current_state(TASK_RUNNING);
790 remove_wait_queue(&proc->wait, &wait);
791
792 if (signal_pending(current)) {
793 spin_unlock(&proc->asts_spin);
794 return -ERESTARTSYS;
795 }
796 }
797
David Teigland597d0ca2006-07-12 16:44:04 -0500798 /* there may be both completion and blocking asts to return for
799 the lkb, don't remove lkb from asts list unless no asts remain */
800
801 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
802
803 if (lkb->lkb_ast_type & AST_COMP) {
804 lkb->lkb_ast_type &= ~AST_COMP;
805 type = AST_COMP;
806 } else if (lkb->lkb_ast_type & AST_BAST) {
807 lkb->lkb_ast_type &= ~AST_BAST;
808 type = AST_BAST;
809 bmode = lkb->lkb_bastmode;
810 }
811
812 if (!lkb->lkb_ast_type) {
813 list_del(&lkb->lkb_astqueue);
814 removed = 1;
815 }
816 spin_unlock(&proc->asts_spin);
817
818 ua = (struct dlm_user_args *)lkb->lkb_astparam;
819 error = copy_result_to_user(ua,
820 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
821 type, bmode, buf, count);
822
823 /* removes reference for the proc->asts lists added by
824 dlm_user_add_ast() and may result in the lkb being freed */
825 if (removed)
826 dlm_put_lkb(lkb);
827
828 return error;
829}
830
831static unsigned int device_poll(struct file *file, poll_table *wait)
832{
833 struct dlm_user_proc *proc = file->private_data;
834
835 poll_wait(file, &proc->wait, wait);
836
837 spin_lock(&proc->asts_spin);
838 if (!list_empty(&proc->asts)) {
839 spin_unlock(&proc->asts_spin);
840 return POLLIN | POLLRDNORM;
841 }
842 spin_unlock(&proc->asts_spin);
843 return 0;
844}
845
846static int ctl_device_open(struct inode *inode, struct file *file)
847{
848 file->private_data = NULL;
849 return 0;
850}
851
852static int ctl_device_close(struct inode *inode, struct file *file)
853{
854 return 0;
855}
856
Arjan van de Ven00977a52007-02-12 00:55:34 -0800857static const struct file_operations device_fops = {
David Teigland597d0ca2006-07-12 16:44:04 -0500858 .open = device_open,
859 .release = device_close,
860 .read = device_read,
861 .write = device_write,
862 .poll = device_poll,
863 .owner = THIS_MODULE,
864};
865
Arjan van de Ven00977a52007-02-12 00:55:34 -0800866static const struct file_operations ctl_device_fops = {
David Teigland597d0ca2006-07-12 16:44:04 -0500867 .open = ctl_device_open,
868 .release = ctl_device_close,
David Teiglandd7db9232007-05-18 09:00:32 -0500869 .read = device_read,
David Teigland597d0ca2006-07-12 16:44:04 -0500870 .write = device_write,
871 .owner = THIS_MODULE,
872};
873
874int dlm_user_init(void)
875{
876 int error;
877
878 ctl_device.name = "dlm-control";
879 ctl_device.fops = &ctl_device_fops;
880 ctl_device.minor = MISC_DYNAMIC_MINOR;
881
882 error = misc_register(&ctl_device);
883 if (error)
884 log_print("misc_register failed for control device");
885
886 return error;
887}
888
889void dlm_user_exit(void)
890{
891 misc_deregister(&ctl_device);
892}
893