]> err.no Git - linux-2.6/blob - fs/dlm/device.c
[DLM] compat patch
[linux-2.6] / fs / dlm / device.c
1 /******************************************************************************
2 *******************************************************************************
3 **
4 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5 **  Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
6 **
7 **  This copyrighted material is made available to anyone wishing to use,
8 **  modify, copy, or redistribute it subject to the terms and conditions
9 **  of the GNU General Public License v.2.
10 **
11 *******************************************************************************
12 ******************************************************************************/
13
14 /*
15  * device.c
16  *
17  * This is the userland interface to the DLM.
18  *
19  * The locking is done via a misc char device (find the
20  * registered minor number in /proc/misc).
21  *
22  * User code should not use this interface directly but
23  * call the library routines in libdlm.a instead.
24  *
25  */
26
27 #include <linux/miscdevice.h>
28 #include <linux/init.h>
29 #include <linux/wait.h>
30 #include <linux/module.h>
31 #include <linux/file.h>
32 #include <linux/fs.h>
33 #include <linux/poll.h>
34 #include <linux/signal.h>
35 #include <linux/spinlock.h>
36 #include <linux/idr.h>
37
38 #include <linux/dlm.h>
39 #include <linux/dlm_device.h>
40
41 #include "lvb_table.h"
42
43 static struct file_operations _dlm_fops;
44 static const char *name_prefix="dlm";
45 static struct list_head user_ls_list;
46 static struct mutex user_ls_lock;
47
48 /* Lock infos are stored in here indexed by lock ID */
49 static DEFINE_IDR(lockinfo_idr);
50 static rwlock_t lockinfo_lock;
51
52 /* Flags in li_flags */
53 #define LI_FLAG_COMPLETE   1
54 #define LI_FLAG_FIRSTLOCK  2
55 #define LI_FLAG_PERSISTENT 3
56 #define LI_FLAG_ONLIST     4
57
58 /* flags in ls_flags*/
59 #define LS_FLAG_DELETED   1
60 #define LS_FLAG_AUTOFREE  2
61
62 /* flags in ls_flags*/
63 #define FI_FLAG_OPEN      1
64 #define FI_FLAG_COMPAT    2
65
66 #define LOCKINFO_MAGIC 0x53595324
67
68 struct lock_info {
69         uint32_t li_magic;
70         uint8_t li_cmd;
71         int8_t  li_grmode;
72         int8_t  li_rqmode;
73         struct dlm_lksb li_lksb;
74         wait_queue_head_t li_waitq;
75         unsigned long li_flags;
76         void __user *li_castparam;
77         void __user *li_castaddr;
78         void __user *li_bastparam;
79         void __user *li_bastaddr;
80         void __user *li_pend_bastparam;
81         void __user *li_pend_bastaddr;
82         struct list_head li_ownerqueue;
83         struct file_info *li_file;
84         struct dlm_lksb __user *li_user_lksb;
85         struct completion li_firstcomp;
86 };
87
88 /* A queued AST no less */
89 struct ast_info {
90         struct dlm_lock_result result;
91         struct list_head list;
92         uint32_t lvb_updated;
93         uint32_t progress;      /* How much has been read */
94 };
95
96 /* One of these per userland lockspace */
97 struct user_ls {
98         void    *ls_lockspace;
99         atomic_t ls_refcnt;
100         long     ls_flags;
101
102         /* Passed into misc_register() */
103         struct miscdevice ls_miscinfo;
104         struct list_head  ls_list;
105 };
106
107 /* misc_device info for the control device */
108 static struct miscdevice ctl_device;
109
110 /*
111  * Stuff we hang off the file struct.
112  * The first two are to cope with unlocking all the
113  * locks help by a process when it dies.
114  */
115 struct file_info {
116         struct list_head    fi_li_list;  /* List of active lock_infos */
117         spinlock_t          fi_li_lock;
118         struct list_head    fi_ast_list; /* Queue of ASTs to be delivered */
119         spinlock_t          fi_ast_lock;
120         wait_queue_head_t   fi_wait;
121         struct user_ls     *fi_ls;
122         atomic_t            fi_refcnt;   /* Number of users */
123         unsigned long       fi_flags;
124 };
125
126 #ifdef CONFIG_COMPAT
127
128 struct dlm_lock_params32 {
129         __u8 mode;
130         __u8 namelen;
131         __u16 flags;
132         __u32 lkid;
133         __u32 parent;
134
135         __u32 castparam;
136         __u32 castaddr;
137         __u32 bastparam;
138         __u32 bastaddr;
139         __u32 lksb;
140
141         char lvb[DLM_USER_LVB_LEN];
142         char name[0];
143 };
144
145 struct dlm_write_request32 {
146         __u32 version[3];
147         __u8 cmd;
148         __u8 is64bit;
149         __u8 unused[2];
150
151         union  {
152                 struct dlm_lock_params32 lock;
153                 struct dlm_lspace_params lspace;
154         } i;
155 };
156
157 struct dlm_lksb32 {
158         __u32    sb_status;
159         __u32    sb_lkid;
160         __u8     sb_flags;
161         __u32    sb_lvbptr;
162 };
163
164 struct dlm_lock_result32 {
165         __u32 length;
166         __u32 user_astaddr;
167         __u32 user_astparam;
168         __u32 user_lksb;
169         struct dlm_lksb32 lksb;
170         __u8 bast_mode;
171         __u8 unused[3];
172         /* Offsets may be zero if no data is present */
173         __u32 lvb_offset;
174 };
175
176
177 static void compat_input(struct dlm_write_request *kparams, struct dlm_write_request32 *k32params)
178 {
179
180         kparams->version[0] = k32params->version[0];
181         kparams->version[1] = k32params->version[1];
182         kparams->version[2] = k32params->version[2];
183
184         kparams->cmd = k32params->cmd;
185         kparams->is64bit = k32params->is64bit;
186         if (kparams->cmd == DLM_USER_CREATE_LOCKSPACE ||
187             kparams->cmd == DLM_USER_REMOVE_LOCKSPACE) {
188
189                 kparams->i.lspace.flags = k32params->i.lspace.flags;
190                 kparams->i.lspace.minor = k32params->i.lspace.minor;
191                 strcpy(kparams->i.lspace.name, k32params->i.lspace.name);
192         }
193         else {
194                 kparams->i.lock.mode = k32params->i.lock.mode;
195                 kparams->i.lock.namelen = k32params->i.lock.namelen;
196                 kparams->i.lock.flags = k32params->i.lock.flags;
197                 kparams->i.lock.lkid = k32params->i.lock.lkid;
198                 kparams->i.lock.parent = k32params->i.lock.parent;
199                 kparams->i.lock.castparam = (void *)(long)k32params->i.lock.castparam;
200                 kparams->i.lock.castaddr = (void *)(long)k32params->i.lock.castaddr;
201                 kparams->i.lock.bastparam = (void *)(long)k32params->i.lock.bastparam;
202                 kparams->i.lock.bastaddr = (void *)(long)k32params->i.lock.bastaddr;
203                 kparams->i.lock.lksb = (void *)(long)k32params->i.lock.lksb;
204                 memcpy(kparams->i.lock.lvb, k32params->i.lock.lvb, DLM_USER_LVB_LEN);
205                 memcpy(kparams->i.lock.name, k32params->i.lock.name, kparams->i.lock.namelen);
206         }
207 }
208
209 void compat_output(struct dlm_lock_result *res, struct dlm_lock_result32 *res32)
210 {
211         res32->length = res->length - (sizeof(struct dlm_lock_result) - sizeof(struct dlm_lock_result32));
212         res32->user_astaddr = (__u32)(long)res->user_astaddr;
213         res32->user_astparam = (__u32)(long)res->user_astparam;
214         res32->user_lksb = (__u32)(long)res->user_lksb;
215         res32->bast_mode = res->bast_mode;
216
217         res32->lvb_offset = res->lvb_offset;
218         res32->length = res->length;
219
220         res32->lksb.sb_status = res->lksb.sb_status;
221         res32->lksb.sb_flags = res->lksb.sb_flags;
222         res32->lksb.sb_lkid = res->lksb.sb_lkid;
223         res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
224 }
225 #endif
226
227
228 /* get and put ops for file_info.
229    Actually I don't really like "get" and "put", but everyone
230    else seems to use them and I can't think of anything
231    nicer at the moment */
232 static void get_file_info(struct file_info *f)
233 {
234         atomic_inc(&f->fi_refcnt);
235 }
236
237 static void put_file_info(struct file_info *f)
238 {
239         if (atomic_dec_and_test(&f->fi_refcnt))
240                 kfree(f);
241 }
242
243 static void release_lockinfo(struct lock_info *li)
244 {
245         put_file_info(li->li_file);
246
247         write_lock(&lockinfo_lock);
248         idr_remove(&lockinfo_idr, li->li_lksb.sb_lkid);
249         write_unlock(&lockinfo_lock);
250
251         if (li->li_lksb.sb_lvbptr)
252                 kfree(li->li_lksb.sb_lvbptr);
253         kfree(li);
254
255         module_put(THIS_MODULE);
256 }
257
258 static struct lock_info *get_lockinfo(uint32_t lockid)
259 {
260         struct lock_info *li;
261
262         read_lock(&lockinfo_lock);
263         li = idr_find(&lockinfo_idr, lockid);
264         read_unlock(&lockinfo_lock);
265
266         return li;
267 }
268
269 static int add_lockinfo(struct lock_info *li)
270 {
271         int n;
272         int r;
273         int ret = -EINVAL;
274
275         write_lock(&lockinfo_lock);
276
277         if (idr_find(&lockinfo_idr, li->li_lksb.sb_lkid))
278                 goto out_up;
279
280         ret = -ENOMEM;
281         r = idr_pre_get(&lockinfo_idr, GFP_KERNEL);
282         if (!r)
283                 goto out_up;
284
285         r = idr_get_new_above(&lockinfo_idr, li, li->li_lksb.sb_lkid, &n);
286         if (r)
287                 goto out_up;
288
289         if (n != li->li_lksb.sb_lkid) {
290                 idr_remove(&lockinfo_idr, n);
291                 goto out_up;
292         }
293
294         ret = 0;
295
296  out_up:
297         write_unlock(&lockinfo_lock);
298
299         return ret;
300 }
301
302
303 static struct user_ls *__find_lockspace(int minor)
304 {
305         struct user_ls *lsinfo;
306
307         list_for_each_entry(lsinfo, &user_ls_list, ls_list) {
308                 if (lsinfo->ls_miscinfo.minor == minor)
309                         return lsinfo;
310         }
311         return NULL;
312 }
313
314 /* Find a lockspace struct given the device minor number */
315 static struct user_ls *find_lockspace(int minor)
316 {
317         struct user_ls *lsinfo;
318
319         mutex_lock(&user_ls_lock);
320         lsinfo = __find_lockspace(minor);
321         mutex_unlock(&user_ls_lock);
322
323         return lsinfo;
324 }
325
326 static void add_lockspace_to_list(struct user_ls *lsinfo)
327 {
328         mutex_lock(&user_ls_lock);
329         list_add(&lsinfo->ls_list, &user_ls_list);
330         mutex_unlock(&user_ls_lock);
331 }
332
333 /* Register a lockspace with the DLM and create a misc
334    device for userland to access it */
335 static int register_lockspace(char *name, struct user_ls **ls, int flags)
336 {
337         struct user_ls *newls;
338         int status;
339         int namelen;
340
341         namelen = strlen(name)+strlen(name_prefix)+2;
342
343         newls = kzalloc(sizeof(struct user_ls), GFP_KERNEL);
344         if (!newls)
345                 return -ENOMEM;
346
347         newls->ls_miscinfo.name = kzalloc(namelen, GFP_KERNEL);
348         if (!newls->ls_miscinfo.name) {
349                 kfree(newls);
350                 return -ENOMEM;
351         }
352
353         status = dlm_new_lockspace(name, strlen(name), &newls->ls_lockspace, 0,
354                                    DLM_USER_LVB_LEN);
355         if (status != 0) {
356                 kfree(newls->ls_miscinfo.name);
357                 kfree(newls);
358                 return status;
359         }
360
361         snprintf((char*)newls->ls_miscinfo.name, namelen, "%s_%s",
362                  name_prefix, name);
363
364         newls->ls_miscinfo.fops = &_dlm_fops;
365         newls->ls_miscinfo.minor = MISC_DYNAMIC_MINOR;
366
367         status = misc_register(&newls->ls_miscinfo);
368         if (status) {
369                 printk(KERN_ERR "dlm: misc register failed for %s\n", name);
370                 dlm_release_lockspace(newls->ls_lockspace, 0);
371                 kfree(newls->ls_miscinfo.name);
372                 kfree(newls);
373                 return status;
374         }
375
376         if (flags & DLM_USER_LSFLG_AUTOFREE)
377                 set_bit(LS_FLAG_AUTOFREE, &newls->ls_flags);
378
379         add_lockspace_to_list(newls);
380         *ls = newls;
381         return 0;
382 }
383
384 /* Called with the user_ls_lock mutex held */
385 static int unregister_lockspace(struct user_ls *lsinfo, int force)
386 {
387         int status;
388
389         status = dlm_release_lockspace(lsinfo->ls_lockspace, force);
390         if (status)
391                 return status;
392
393         status = misc_deregister(&lsinfo->ls_miscinfo);
394         if (status)
395                 return status;
396
397         list_del(&lsinfo->ls_list);
398         set_bit(LS_FLAG_DELETED, &lsinfo->ls_flags);
399         lsinfo->ls_lockspace = NULL;
400         if (atomic_read(&lsinfo->ls_refcnt) == 0) {
401                 kfree(lsinfo->ls_miscinfo.name);
402                 kfree(lsinfo);
403         }
404
405         return 0;
406 }
407
408 /* Add it to userland's AST queue */
409 static void add_to_astqueue(struct lock_info *li, void *astaddr, void *astparam,
410                             int lvb_updated)
411 {
412         struct ast_info *ast = kzalloc(sizeof(struct ast_info), GFP_KERNEL);
413         if (!ast)
414                 return;
415
416         ast->result.user_astparam = astparam;
417         ast->result.user_astaddr  = astaddr;
418         ast->result.user_lksb     = li->li_user_lksb;
419         memcpy(&ast->result.lksb, &li->li_lksb, sizeof(struct dlm_lksb));
420         ast->lvb_updated = lvb_updated;
421
422         spin_lock(&li->li_file->fi_ast_lock);
423         list_add_tail(&ast->list, &li->li_file->fi_ast_list);
424         spin_unlock(&li->li_file->fi_ast_lock);
425         wake_up_interruptible(&li->li_file->fi_wait);
426 }
427
428 static void bast_routine(void *param, int mode)
429 {
430         struct lock_info *li = param;
431
432         if (li && li->li_bastaddr)
433                 add_to_astqueue(li, li->li_bastaddr, li->li_bastparam, 0);
434 }
435
436 /*
437  * This is the kernel's AST routine.
438  * All lock, unlock & query operations complete here.
439  * The only syncronous ops are those done during device close.
440  */
441 static void ast_routine(void *param)
442 {
443         struct lock_info *li = param;
444
445         /* Param may be NULL if a persistent lock is unlocked by someone else */
446         if (!li)
447                 return;
448
449         /* If this is a succesful conversion then activate the blocking ast
450          * args from the conversion request */
451         if (!test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
452             li->li_lksb.sb_status == 0) {
453
454                 li->li_bastparam = li->li_pend_bastparam;
455                 li->li_bastaddr = li->li_pend_bastaddr;
456                 li->li_pend_bastaddr = NULL;
457         }
458
459         /* If it's an async request then post data to the user's AST queue. */
460         if (li->li_castaddr) {
461                 int lvb_updated = 0;
462
463                 /* See if the lvb has been updated */
464                 if (dlm_lvb_operations[li->li_grmode+1][li->li_rqmode+1] == 1)
465                         lvb_updated = 1;
466
467                 if (li->li_lksb.sb_status == 0)
468                         li->li_grmode = li->li_rqmode;
469
470                 /* Only queue AST if the device is still open */
471                 if (test_bit(FI_FLAG_OPEN, &li->li_file->fi_flags))
472                         add_to_astqueue(li, li->li_castaddr, li->li_castparam,
473                                         lvb_updated);
474
475                 /* If it's a new lock operation that failed, then
476                  * remove it from the owner queue and free the
477                  * lock_info.
478                  */
479                 if (test_and_clear_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
480                     li->li_lksb.sb_status != 0) {
481
482                         /* Wait till dlm_lock() has finished */
483                         wait_for_completion(&li->li_firstcomp);
484
485                         spin_lock(&li->li_file->fi_li_lock);
486                         list_del(&li->li_ownerqueue);
487                         clear_bit(LI_FLAG_ONLIST, &li->li_flags);
488                         spin_unlock(&li->li_file->fi_li_lock);
489                         release_lockinfo(li);
490                         return;
491                 }
492                 /* Free unlocks & queries */
493                 if (li->li_lksb.sb_status == -DLM_EUNLOCK ||
494                     li->li_cmd == DLM_USER_QUERY) {
495                         release_lockinfo(li);
496                 }
497         } else {
498                 /* Synchronous request, just wake up the caller */
499                 set_bit(LI_FLAG_COMPLETE, &li->li_flags);
500                 wake_up_interruptible(&li->li_waitq);
501         }
502 }
503
504 /*
505  * Wait for the lock op to complete and return the status.
506  */
507 static int wait_for_ast(struct lock_info *li)
508 {
509         /* Wait for the AST routine to complete */
510         set_task_state(current, TASK_INTERRUPTIBLE);
511         while (!test_bit(LI_FLAG_COMPLETE, &li->li_flags))
512                 schedule();
513
514         set_task_state(current, TASK_RUNNING);
515
516         return li->li_lksb.sb_status;
517 }
518
519
520 /* Open on control device */
521 static int dlm_ctl_open(struct inode *inode, struct file *file)
522 {
523         file->private_data = NULL;
524         return 0;
525 }
526
527 /* Close on control device */
528 static int dlm_ctl_close(struct inode *inode, struct file *file)
529 {
530         return 0;
531 }
532
533 /* Open on lockspace device */
534 static int dlm_open(struct inode *inode, struct file *file)
535 {
536         struct file_info *f;
537         struct user_ls *lsinfo;
538
539         lsinfo = find_lockspace(iminor(inode));
540         if (!lsinfo)
541                 return -ENOENT;
542
543         f = kzalloc(sizeof(struct file_info), GFP_KERNEL);
544         if (!f)
545                 return -ENOMEM;
546
547         atomic_inc(&lsinfo->ls_refcnt);
548         INIT_LIST_HEAD(&f->fi_li_list);
549         INIT_LIST_HEAD(&f->fi_ast_list);
550         spin_lock_init(&f->fi_li_lock);
551         spin_lock_init(&f->fi_ast_lock);
552         init_waitqueue_head(&f->fi_wait);
553         f->fi_ls = lsinfo;
554         f->fi_flags = 0;
555         get_file_info(f);
556         set_bit(FI_FLAG_OPEN, &f->fi_flags);
557
558         file->private_data = f;
559
560         return 0;
561 }
562
563 /* Check the user's version matches ours */
564 static int check_version(struct dlm_write_request *req)
565 {
566         if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
567             (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
568              req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
569
570                 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
571                        "user (%d.%d.%d) kernel (%d.%d.%d)\n",
572                        current->comm,
573                        current->pid,
574                        req->version[0],
575                        req->version[1],
576                        req->version[2],
577                        DLM_DEVICE_VERSION_MAJOR,
578                        DLM_DEVICE_VERSION_MINOR,
579                        DLM_DEVICE_VERSION_PATCH);
580                 return -EINVAL;
581         }
582         return 0;
583 }
584
585 /* Close on lockspace device */
586 static int dlm_close(struct inode *inode, struct file *file)
587 {
588         struct file_info *f = file->private_data;
589         struct lock_info li;
590         struct lock_info *old_li, *safe;
591         sigset_t tmpsig;
592         sigset_t allsigs;
593         struct user_ls *lsinfo;
594         DECLARE_WAITQUEUE(wq, current);
595
596         lsinfo = find_lockspace(iminor(inode));
597         if (!lsinfo)
598                 return -ENOENT;
599
600         /* Mark this closed so that ASTs will not be delivered any more */
601         clear_bit(FI_FLAG_OPEN, &f->fi_flags);
602
603         /* Block signals while we are doing this */
604         sigfillset(&allsigs);
605         sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
606
607         /* We use our own lock_info struct here, so that any
608          * outstanding "real" ASTs will be delivered with the
609          * corresponding "real" params, thus freeing the lock_info
610          * that belongs the lock. This catches the corner case where
611          * a lock is BUSY when we try to unlock it here
612          */
613         memset(&li, 0, sizeof(li));
614         clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
615         init_waitqueue_head(&li.li_waitq);
616         add_wait_queue(&li.li_waitq, &wq);
617
618         /*
619          * Free any outstanding locks, they are on the
620          * list in LIFO order so there should be no problems
621          * about unlocking parents before children.
622          */
623         list_for_each_entry_safe(old_li, safe, &f->fi_li_list, li_ownerqueue) {
624                 int status;
625                 int flags = 0;
626
627                 /* Don't unlock persistent locks, just mark them orphaned */
628                 if (test_bit(LI_FLAG_PERSISTENT, &old_li->li_flags)) {
629                         list_del(&old_li->li_ownerqueue);
630
631                         /* Update master copy */
632                         /* TODO: Check locking core updates the local and
633                            remote ORPHAN flags */
634                         li.li_lksb.sb_lkid = old_li->li_lksb.sb_lkid;
635                         status = dlm_lock(f->fi_ls->ls_lockspace,
636                                           old_li->li_grmode, &li.li_lksb,
637                                           DLM_LKF_CONVERT|DLM_LKF_ORPHAN,
638                                           NULL, 0, 0, ast_routine, NULL, NULL);
639                         if (status != 0)
640                                 printk("dlm: Error orphaning lock %x: %d\n",
641                                        old_li->li_lksb.sb_lkid, status);
642
643                         /* But tidy our references in it */
644                         release_lockinfo(old_li);
645                         continue;
646                 }
647
648                 clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
649
650                 flags = DLM_LKF_FORCEUNLOCK;
651                 if (old_li->li_grmode >= DLM_LOCK_PW)
652                         flags |= DLM_LKF_IVVALBLK;
653
654                 status = dlm_unlock(f->fi_ls->ls_lockspace,
655                                     old_li->li_lksb.sb_lkid, flags,
656                                     &li.li_lksb, &li);
657
658                 /* Must wait for it to complete as the next lock could be its
659                  * parent */
660                 if (status == 0)
661                         wait_for_ast(&li);
662
663                 /* Unlock suceeded, free the lock_info struct. */
664                 if (status == 0)
665                         release_lockinfo(old_li);
666         }
667
668         remove_wait_queue(&li.li_waitq, &wq);
669
670         /*
671          * If this is the last reference to the lockspace
672          * then free the struct. If it's an AUTOFREE lockspace
673          * then free the whole thing.
674          */
675         mutex_lock(&user_ls_lock);
676         if (atomic_dec_and_test(&lsinfo->ls_refcnt)) {
677
678                 if (lsinfo->ls_lockspace) {
679                         if (test_bit(LS_FLAG_AUTOFREE, &lsinfo->ls_flags)) {
680                                 unregister_lockspace(lsinfo, 1);
681                         }
682                 } else {
683                         kfree(lsinfo->ls_miscinfo.name);
684                         kfree(lsinfo);
685                 }
686         }
687         mutex_unlock(&user_ls_lock);
688         put_file_info(f);
689
690         /* Restore signals */
691         sigprocmask(SIG_SETMASK, &tmpsig, NULL);
692         recalc_sigpending();
693
694         return 0;
695 }
696
697 static int do_user_create_lockspace(struct file_info *fi, uint8_t cmd,
698                                     struct dlm_lspace_params *kparams)
699 {
700         int status;
701         struct user_ls *lsinfo;
702
703         if (!capable(CAP_SYS_ADMIN))
704                 return -EPERM;
705
706         status = register_lockspace(kparams->name, &lsinfo, kparams->flags);
707
708         /* If it succeeded then return the minor number */
709         if (status == 0)
710                 status = lsinfo->ls_miscinfo.minor;
711
712         return status;
713 }
714
715 static int do_user_remove_lockspace(struct file_info *fi, uint8_t cmd,
716                                     struct dlm_lspace_params *kparams)
717 {
718         int status;
719         int force = 1;
720         struct user_ls *lsinfo;
721
722         if (!capable(CAP_SYS_ADMIN))
723                 return -EPERM;
724
725         mutex_lock(&user_ls_lock);
726         lsinfo = __find_lockspace(kparams->minor);
727         if (!lsinfo) {
728                 mutex_unlock(&user_ls_lock);
729                 return -EINVAL;
730         }
731
732         if (kparams->flags & DLM_USER_LSFLG_FORCEFREE)
733                 force = 3;
734
735         status = unregister_lockspace(lsinfo, force);
736         mutex_unlock(&user_ls_lock);
737
738         return status;
739 }
740
741 /* Read call, might block if no ASTs are waiting.
742  * It will only ever return one message at a time, regardless
743  * of how many are pending.
744  */
745 static ssize_t dlm_read(struct file *file, char __user *buffer, size_t count,
746                         loff_t *ppos)
747 {
748         struct file_info *fi = file->private_data;
749         struct ast_info *ast;
750         void *data;
751         int data_size;
752         int struct_size;
753         int offset;
754         DECLARE_WAITQUEUE(wait, current);
755 #ifdef CONFIG_COMPAT
756         struct dlm_lock_result32 result32;
757
758         if (count < sizeof(struct dlm_lock_result32))
759 #else
760         if (count < sizeof(struct dlm_lock_result))
761 #endif
762                 return -EINVAL;
763
764         spin_lock(&fi->fi_ast_lock);
765         if (list_empty(&fi->fi_ast_list)) {
766
767                 /* No waiting ASTs.
768                  * Return EOF if the lockspace been deleted.
769                  */
770                 if (test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
771                         return 0;
772
773                 if (file->f_flags & O_NONBLOCK) {
774                         spin_unlock(&fi->fi_ast_lock);
775                         return -EAGAIN;
776                 }
777
778                 add_wait_queue(&fi->fi_wait, &wait);
779
780         repeat:
781                 set_current_state(TASK_INTERRUPTIBLE);
782                 if (list_empty(&fi->fi_ast_list) &&
783                     !signal_pending(current)) {
784
785                         spin_unlock(&fi->fi_ast_lock);
786                         schedule();
787                         spin_lock(&fi->fi_ast_lock);
788                         goto repeat;
789                 }
790
791                 current->state = TASK_RUNNING;
792                 remove_wait_queue(&fi->fi_wait, &wait);
793
794                 if (signal_pending(current)) {
795                         spin_unlock(&fi->fi_ast_lock);
796                         return -ERESTARTSYS;
797                 }
798         }
799
800         ast = list_entry(fi->fi_ast_list.next, struct ast_info, list);
801         list_del(&ast->list);
802         spin_unlock(&fi->fi_ast_lock);
803
804         /* Work out the size of the returned data */
805 #ifdef CONFIG_COMPAT
806         if (test_bit(FI_FLAG_COMPAT, &fi->fi_flags)) {
807                 data_size = struct_size = sizeof(struct dlm_lock_result32);
808                 data = &result32;
809         }
810         else
811 #endif
812         {
813                 data_size = struct_size = sizeof(struct dlm_lock_result);
814                 data = &ast->result;
815         }
816         if (ast->lvb_updated && ast->result.lksb.sb_lvbptr)
817                 data_size += DLM_USER_LVB_LEN;
818
819         offset = struct_size;
820
821         /* Room for the extended data ? */
822         if (count >= data_size) {
823
824                 if (ast->lvb_updated && ast->result.lksb.sb_lvbptr) {
825                         if (copy_to_user(buffer+offset,
826                                          ast->result.lksb.sb_lvbptr,
827                                          DLM_USER_LVB_LEN))
828                                 return -EFAULT;
829                         ast->result.lvb_offset = offset;
830                         offset += DLM_USER_LVB_LEN;
831                 }
832         }
833
834         ast->result.length = data_size;
835
836 #ifdef CONFIG_COMPAT
837         compat_output(&ast->result, &result32);
838 #endif
839
840         /* Copy the header now it has all the offsets in it */
841         if (copy_to_user(buffer, data, struct_size))
842                 offset = -EFAULT;
843
844         /* If we only returned a header and there's more to come then put it
845            back on the list */
846         if (count < data_size) {
847                 spin_lock(&fi->fi_ast_lock);
848                 list_add(&ast->list, &fi->fi_ast_list);
849                 spin_unlock(&fi->fi_ast_lock);
850         } else
851                 kfree(ast);
852         return offset;
853 }
854
855 static unsigned int dlm_poll(struct file *file, poll_table *wait)
856 {
857         struct file_info *fi = file->private_data;
858
859         poll_wait(file, &fi->fi_wait, wait);
860
861         spin_lock(&fi->fi_ast_lock);
862         if (!list_empty(&fi->fi_ast_list)) {
863                 spin_unlock(&fi->fi_ast_lock);
864                 return POLLIN | POLLRDNORM;
865         }
866
867         spin_unlock(&fi->fi_ast_lock);
868         return 0;
869 }
870
871 static struct lock_info *allocate_lockinfo(struct file_info *fi, uint8_t cmd,
872                                            struct dlm_lock_params *kparams)
873 {
874         struct lock_info *li;
875
876         if (!try_module_get(THIS_MODULE))
877                 return NULL;
878
879         li = kzalloc(sizeof(struct lock_info), GFP_KERNEL);
880         if (li) {
881                 li->li_magic     = LOCKINFO_MAGIC;
882                 li->li_file      = fi;
883                 li->li_cmd       = cmd;
884                 li->li_flags     = 0;
885                 li->li_grmode    = -1;
886                 li->li_rqmode    = -1;
887                 li->li_pend_bastparam = NULL;
888                 li->li_pend_bastaddr  = NULL;
889                 li->li_castaddr   = NULL;
890                 li->li_castparam  = NULL;
891                 li->li_lksb.sb_lvbptr = NULL;
892                 li->li_bastaddr  = kparams->bastaddr;
893                 li->li_bastparam = kparams->bastparam;
894
895                 get_file_info(fi);
896         }
897         return li;
898 }
899
900 static int do_user_lock(struct file_info *fi, uint8_t cmd,
901                         struct dlm_lock_params *kparams)
902 {
903         struct lock_info *li;
904         int status;
905
906         /*
907          * Validate things that we need to have correct.
908          */
909         if (!kparams->castaddr)
910                 return -EINVAL;
911
912         if (!kparams->lksb)
913                 return -EINVAL;
914
915         /* Persistent child locks are not available yet */
916         if ((kparams->flags & DLM_LKF_PERSISTENT) && kparams->parent)
917                 return -EINVAL;
918
919         /* For conversions, there should already be a lockinfo struct,
920            unless we are adopting an orphaned persistent lock */
921         if (kparams->flags & DLM_LKF_CONVERT) {
922
923                 li = get_lockinfo(kparams->lkid);
924
925                 /* If this is a persistent lock we will have to create a
926                    lockinfo again */
927                 if (!li && (kparams->flags & DLM_LKF_PERSISTENT)) {
928                         li = allocate_lockinfo(fi, cmd, kparams);
929                         if (!li)
930                                 return -ENOMEM;
931
932                         li->li_lksb.sb_lkid = kparams->lkid;
933                         li->li_castaddr  = kparams->castaddr;
934                         li->li_castparam = kparams->castparam;
935
936                         /* OK, this isn't exactly a FIRSTLOCK but it is the
937                            first time we've used this lockinfo, and if things
938                            fail we want rid of it */
939                         init_completion(&li->li_firstcomp);
940                         set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
941                         add_lockinfo(li);
942
943                         /* TODO: do a query to get the current state ?? */
944                 }
945                 if (!li)
946                         return -EINVAL;
947
948                 if (li->li_magic != LOCKINFO_MAGIC)
949                         return -EINVAL;
950
951                 /* For conversions don't overwrite the current blocking AST
952                    info so that:
953                    a) if a blocking AST fires before the conversion is queued
954                       it runs the current handler
955                    b) if the conversion is cancelled, the original blocking AST
956                       declaration is active
957                    The pend_ info is made active when the conversion
958                    completes.
959                 */
960                 li->li_pend_bastaddr  = kparams->bastaddr;
961                 li->li_pend_bastparam = kparams->bastparam;
962         } else {
963                 li = allocate_lockinfo(fi, cmd, kparams);
964                 if (!li)
965                         return -ENOMEM;
966
967                 /* Allow us to complete our work before
968                    the AST routine runs. In fact we only need (and use) this
969                    when the initial lock fails */
970                 init_completion(&li->li_firstcomp);
971                 set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
972         }
973
974         li->li_user_lksb = kparams->lksb;
975         li->li_castaddr  = kparams->castaddr;
976         li->li_castparam = kparams->castparam;
977         li->li_lksb.sb_lkid = kparams->lkid;
978         li->li_rqmode    = kparams->mode;
979         if (kparams->flags & DLM_LKF_PERSISTENT)
980                 set_bit(LI_FLAG_PERSISTENT, &li->li_flags);
981
982         /* Copy in the value block */
983         if (kparams->flags & DLM_LKF_VALBLK) {
984                 if (!li->li_lksb.sb_lvbptr) {
985                         li->li_lksb.sb_lvbptr = kmalloc(DLM_USER_LVB_LEN,
986                                                         GFP_KERNEL);
987                         if (!li->li_lksb.sb_lvbptr) {
988                                 status = -ENOMEM;
989                                 goto out_err;
990                         }
991                 }
992
993                 memcpy(li->li_lksb.sb_lvbptr, kparams->lvb, DLM_USER_LVB_LEN);
994         }
995
996         /* Lock it ... */
997         status = dlm_lock(fi->fi_ls->ls_lockspace,
998                           kparams->mode, &li->li_lksb,
999                           kparams->flags,
1000                           kparams->name, kparams->namelen,
1001                           kparams->parent,
1002                           ast_routine,
1003                           li,
1004                           (li->li_pend_bastaddr || li->li_bastaddr) ?
1005                            bast_routine : NULL);
1006         if (status)
1007                 goto out_err;
1008
1009         /* If it succeeded (this far) with a new lock then keep track of
1010            it on the file's lockinfo list */
1011         if (!status && test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags)) {
1012
1013                 spin_lock(&fi->fi_li_lock);
1014                 list_add(&li->li_ownerqueue, &fi->fi_li_list);
1015                 set_bit(LI_FLAG_ONLIST, &li->li_flags);
1016                 spin_unlock(&fi->fi_li_lock);
1017                 if (add_lockinfo(li))
1018                         printk(KERN_WARNING "Add lockinfo failed\n");
1019
1020                 complete(&li->li_firstcomp);
1021         }
1022
1023         /* Return the lockid as the user needs it /now/ */
1024         return li->li_lksb.sb_lkid;
1025
1026  out_err:
1027         if (test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags))
1028                 release_lockinfo(li);
1029         return status;
1030
1031 }
1032
1033 static int do_user_unlock(struct file_info *fi, uint8_t cmd,
1034                           struct dlm_lock_params *kparams)
1035 {
1036         struct lock_info *li;
1037         int status;
1038         int convert_cancel = 0;
1039
1040         li = get_lockinfo(kparams->lkid);
1041         if (!li) {
1042                 li = allocate_lockinfo(fi, cmd, kparams);
1043                 if (!li)
1044                         return -ENOMEM;
1045                 spin_lock(&fi->fi_li_lock);
1046                 list_add(&li->li_ownerqueue, &fi->fi_li_list);
1047                 set_bit(LI_FLAG_ONLIST, &li->li_flags);
1048                 spin_unlock(&fi->fi_li_lock);
1049         }
1050
1051         if (li->li_magic != LOCKINFO_MAGIC)
1052                 return -EINVAL;
1053
1054         li->li_user_lksb = kparams->lksb;
1055         li->li_castparam = kparams->castparam;
1056         li->li_cmd       = cmd;
1057
1058         /* Cancelling a conversion doesn't remove the lock...*/
1059         if (kparams->flags & DLM_LKF_CANCEL && li->li_grmode != -1)
1060                 convert_cancel = 1;
1061
1062         /* Wait until dlm_lock() has completed */
1063         if (!test_bit(LI_FLAG_ONLIST, &li->li_flags)) {
1064                 wait_for_completion(&li->li_firstcomp);
1065         }
1066
1067         /* dlm_unlock() passes a 0 for castaddr which means don't overwrite
1068            the existing li_castaddr as that's the completion routine for
1069            unlocks. dlm_unlock_wait() specifies a new AST routine to be
1070            executed when the unlock completes. */
1071         if (kparams->castaddr)
1072                 li->li_castaddr = kparams->castaddr;
1073
1074         /* Use existing lksb & astparams */
1075         status = dlm_unlock(fi->fi_ls->ls_lockspace,
1076                              kparams->lkid,
1077                              kparams->flags, &li->li_lksb, li);
1078
1079         if (!status && !convert_cancel) {
1080                 spin_lock(&fi->fi_li_lock);
1081                 list_del(&li->li_ownerqueue);
1082                 clear_bit(LI_FLAG_ONLIST, &li->li_flags);
1083                 spin_unlock(&fi->fi_li_lock);
1084         }
1085
1086         return status;
1087 }
1088
1089 /* Write call, submit a locking request */
1090 static ssize_t dlm_write(struct file *file, const char __user *buffer,
1091                          size_t count, loff_t *ppos)
1092 {
1093         struct file_info *fi = file->private_data;
1094         struct dlm_write_request *kparams;
1095         sigset_t tmpsig;
1096         sigset_t allsigs;
1097         int status;
1098
1099 #ifdef CONFIG_COMPAT
1100         if (count < sizeof(struct dlm_write_request32))
1101 #else
1102         if (count < sizeof(struct dlm_write_request))
1103 #endif
1104                 return -EINVAL;
1105
1106         if (count > sizeof(struct dlm_write_request) + DLM_RESNAME_MAXLEN)
1107                 return -EINVAL;
1108
1109         /* Has the lockspace been deleted */
1110         if (fi && test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
1111                 return -ENOENT;
1112
1113         kparams = kmalloc(count, GFP_KERNEL);
1114         if (!kparams)
1115                 return -ENOMEM;
1116
1117         status = -EFAULT;
1118         /* Get the command info */
1119         if (copy_from_user(kparams, buffer, count))
1120                 goto out_free;
1121
1122         status = -EBADE;
1123         if (check_version(kparams))
1124                 goto out_free;
1125
1126 #ifdef CONFIG_COMPAT
1127         if (!kparams->is64bit) {
1128                 struct dlm_write_request32 *k32params = (struct dlm_write_request32 *)kparams;
1129                 kparams = kmalloc(count + (sizeof(struct dlm_write_request) - sizeof(struct dlm_write_request32)), GFP_KERNEL);
1130                 if (!kparams)
1131                         return -ENOMEM;
1132
1133                 if (fi)
1134                         set_bit(FI_FLAG_COMPAT, &fi->fi_flags);
1135                 compat_input(kparams, k32params);
1136                 kfree(k32params);
1137         }
1138 #endif
1139
1140         /* Block signals while we are doing this */
1141         sigfillset(&allsigs);
1142         sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
1143
1144         status = -EINVAL;
1145         switch (kparams->cmd)
1146         {
1147         case DLM_USER_LOCK:
1148                 if (!fi) goto out_sig;
1149                 status = do_user_lock(fi, kparams->cmd, &kparams->i.lock);
1150                 break;
1151
1152         case DLM_USER_UNLOCK:
1153                 if (!fi) goto out_sig;
1154                 status = do_user_unlock(fi, kparams->cmd, &kparams->i.lock);
1155                 break;
1156
1157         case DLM_USER_CREATE_LOCKSPACE:
1158                 if (fi) goto out_sig;
1159                 status = do_user_create_lockspace(fi, kparams->cmd,
1160                                                   &kparams->i.lspace);
1161                 break;
1162
1163         case DLM_USER_REMOVE_LOCKSPACE:
1164                 if (fi) goto out_sig;
1165                 status = do_user_remove_lockspace(fi, kparams->cmd,
1166                                                   &kparams->i.lspace);
1167                 break;
1168         default:
1169                 printk("Unknown command passed to DLM device : %d\n",
1170                         kparams->cmd);
1171                 break;
1172         }
1173
1174  out_sig:
1175         /* Restore signals */
1176         sigprocmask(SIG_SETMASK, &tmpsig, NULL);
1177         recalc_sigpending();
1178
1179  out_free:
1180         kfree(kparams);
1181         if (status == 0)
1182                 return count;
1183         else
1184                 return status;
1185 }
1186
1187 static struct file_operations _dlm_fops = {
1188       .open    = dlm_open,
1189       .release = dlm_close,
1190       .read    = dlm_read,
1191       .write   = dlm_write,
1192       .poll    = dlm_poll,
1193       .owner   = THIS_MODULE,
1194 };
1195
1196 static struct file_operations _dlm_ctl_fops = {
1197       .open    = dlm_ctl_open,
1198       .release = dlm_ctl_close,
1199       .write   = dlm_write,
1200       .owner   = THIS_MODULE,
1201 };
1202
1203 /*
1204  * Create control device
1205  */
1206 static int __init dlm_device_init(void)
1207 {
1208         int r;
1209
1210         INIT_LIST_HEAD(&user_ls_list);
1211         mutex_init(&user_ls_lock);
1212         rwlock_init(&lockinfo_lock);
1213
1214         ctl_device.name = "dlm-control";
1215         ctl_device.fops = &_dlm_ctl_fops;
1216         ctl_device.minor = MISC_DYNAMIC_MINOR;
1217
1218         r = misc_register(&ctl_device);
1219         if (r) {
1220                 printk(KERN_ERR "dlm: misc_register failed for control dev\n");
1221                 return r;
1222         }
1223
1224         return 0;
1225 }
1226
1227 static void __exit dlm_device_exit(void)
1228 {
1229         misc_deregister(&ctl_device);
1230 }
1231
1232 MODULE_DESCRIPTION("Distributed Lock Manager device interface");
1233 MODULE_AUTHOR("Red Hat, Inc.");
1234 MODULE_LICENSE("GPL");
1235
1236 module_init(dlm_device_init);
1237 module_exit(dlm_device_exit);