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