patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / ipc / mqueue.c
1 /*
2  * POSIX message queues filesystem for Linux.
3  *
4  * Copyright (C) 2003,2004  Krzysztof Benedyczak    (golbi@mat.uni.torun.pl)
5  *                          Michal Wronski          (wrona@mat.uni.torun.pl)
6  *
7  * Spinlocks:               Mohamed Abbas           (abbas.mohamed@intel.com)
8  * Lockless receive & send, fd based notify:
9  *                          Manfred Spraul          (manfred@colorfullife.com)
10  *
11  * This file is released under the GPL.
12  */
13
14 #include <linux/init.h>
15 #include <linux/pagemap.h>
16 #include <linux/file.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/sysctl.h>
20 #include <linux/poll.h>
21 #include <linux/mqueue.h>
22 #include <linux/msg.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <net/sock.h>
26 #include "util.h"
27
28 #define MQUEUE_MAGIC    0x19800202
29 #define DIRENT_SIZE     20
30 #define FILENT_SIZE     80
31
32 #define SEND            0
33 #define RECV            1
34
35 #define STATE_NONE      0
36 #define STATE_PENDING   1
37 #define STATE_READY     2
38
39 /* used by sysctl */
40 #define FS_MQUEUE       1
41 #define CTL_QUEUESMAX   2
42 #define CTL_MSGMAX      3
43 #define CTL_MSGSIZEMAX  4
44
45 /* default values */
46 #define DFLT_QUEUESMAX  64      /* max number of message queues */
47 #define DFLT_MSGMAX     40      /* max number of messages in each queue */
48 #define HARD_MSGMAX     (131072/sizeof(void*))
49 #define DFLT_MSGSIZEMAX 16384   /* max message size */
50
51 #define NOTIFY_COOKIE_LEN       32
52
53 struct ext_wait_queue {         /* queue of sleeping tasks */
54         struct task_struct *task;
55         struct list_head list;
56         struct msg_msg *msg;    /* ptr of loaded message */
57         int state;              /* one of STATE_* values */
58 };
59
60 struct mqueue_inode_info {
61         spinlock_t lock;
62         struct inode vfs_inode;
63         wait_queue_head_t wait_q;
64
65         struct msg_msg **messages;
66         struct mq_attr attr;
67
68         struct sigevent notify;
69         pid_t notify_owner;
70         struct sock *notify_sock;
71         struct sk_buff *notify_cookie;
72
73         /* for tasks waiting for free space and messages, respectively */
74         struct ext_wait_queue e_wait_q[2];
75
76         unsigned long qsize; /* size of queue in memory (sum of all msgs) */
77 };
78
79 static struct inode_operations mqueue_dir_inode_operations;
80 static struct file_operations mqueue_file_operations;
81 static struct super_operations mqueue_super_ops;
82 static void remove_notification(struct mqueue_inode_info *info);
83
84 static spinlock_t mq_lock;
85 static kmem_cache_t *mqueue_inode_cachep;
86 static struct vfsmount *mqueue_mnt;
87
88 static unsigned int queues_count;
89 static unsigned int queues_max  = DFLT_QUEUESMAX;
90 static unsigned int msg_max     = DFLT_MSGMAX;
91 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
92
93 static struct ctl_table_header * mq_sysctl_table;
94
95 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
96 {
97         return container_of(inode, struct mqueue_inode_info, vfs_inode);
98 }
99
100 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
101                                                         struct mq_attr *attr)
102 {
103         struct inode *inode;
104
105         inode = new_inode(sb);
106         if (inode) {
107                 inode->i_mode = mode;
108                 inode->i_uid = current->fsuid;
109                 inode->i_gid = current->fsgid;
110                 inode->i_blksize = PAGE_CACHE_SIZE;
111                 inode->i_blocks = 0;
112                 inode->i_mtime = inode->i_ctime = inode->i_atime =
113                                 CURRENT_TIME;
114
115                 if (S_ISREG(mode)) {
116                         struct mqueue_inode_info *info;
117
118                         inode->i_fop = &mqueue_file_operations;
119                         inode->i_size = FILENT_SIZE;
120                         /* mqueue specific info */
121                         info = MQUEUE_I(inode);
122                         spin_lock_init(&info->lock);
123                         init_waitqueue_head(&info->wait_q);
124                         INIT_LIST_HEAD(&info->e_wait_q[0].list);
125                         INIT_LIST_HEAD(&info->e_wait_q[1].list);
126                         info->notify_owner = 0;
127                         info->qsize = 0;
128                         memset(&info->attr, 0, sizeof(info->attr));
129                         info->attr.mq_maxmsg = DFLT_MSGMAX;
130                         info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
131                         if (attr) {
132                                 info->attr.mq_maxmsg = attr->mq_maxmsg;
133                                 info->attr.mq_msgsize = attr->mq_msgsize;
134                         }
135                         info->messages = kmalloc(info->attr.mq_maxmsg * sizeof(struct msg_msg *), GFP_KERNEL);
136                         if (!info->messages) {
137                                 make_bad_inode(inode);
138                                 iput(inode);
139                                 inode = NULL;
140                         }
141                 } else if (S_ISDIR(mode)) {
142                         inode->i_nlink++;
143                         /* Some things misbehave if size == 0 on a directory */
144                         inode->i_size = 2 * DIRENT_SIZE;
145                         inode->i_op = &mqueue_dir_inode_operations;
146                         inode->i_fop = &simple_dir_operations;
147                 }
148         }
149         return inode;
150 }
151
152 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
153 {
154         struct inode *inode;
155
156         sb->s_blocksize = PAGE_CACHE_SIZE;
157         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
158         sb->s_magic = MQUEUE_MAGIC;
159         sb->s_op = &mqueue_super_ops;
160
161         inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
162         if (!inode)
163                 return -ENOMEM;
164
165         sb->s_root = d_alloc_root(inode);
166         if (!sb->s_root) {
167                 iput(inode);
168                 return -ENOMEM;
169         }
170
171         return 0;
172 }
173
174 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
175                                          int flags, const char *dev_name,
176                                          void *data)
177 {
178         return get_sb_single(fs_type, flags, data, mqueue_fill_super);
179 }
180
181 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
182 {
183         struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
184
185         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
186                 SLAB_CTOR_CONSTRUCTOR)
187                 inode_init_once(&p->vfs_inode);
188 }
189
190 static struct inode *mqueue_alloc_inode(struct super_block *sb)
191 {
192         struct mqueue_inode_info *ei;
193
194         ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
195         if (!ei)
196                 return NULL;
197         return &ei->vfs_inode;
198 }
199
200 static void mqueue_destroy_inode(struct inode *inode)
201 {
202         kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
203 }
204
205 static void mqueue_delete_inode(struct inode *inode)
206 {
207         struct mqueue_inode_info *info;
208         int i;
209
210         if (S_ISDIR(inode->i_mode)) {
211                 clear_inode(inode);
212                 return;
213         }
214         info = MQUEUE_I(inode);
215         spin_lock(&info->lock);
216         for (i = 0; i < info->attr.mq_curmsgs; i++)
217                 free_msg(info->messages[i]);
218         kfree(info->messages);
219         spin_unlock(&info->lock);
220
221         clear_inode(inode);
222
223         if (info->messages) {
224                 spin_lock(&mq_lock);
225                 queues_count--;
226                 spin_unlock(&mq_lock);
227         }
228 }
229
230 static int mqueue_create(struct inode *dir, struct dentry *dentry,
231                                 int mode, struct nameidata *nd)
232 {
233         struct inode *inode;
234         struct mq_attr *attr = dentry->d_fsdata;
235         int error;
236
237         spin_lock(&mq_lock);
238         if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
239                 error = -ENOSPC;
240                 goto out_lock;
241         }
242         queues_count++;
243         spin_unlock(&mq_lock);
244
245         inode = mqueue_get_inode(dir->i_sb, mode, attr);
246         if (!inode) {
247                 error = -ENOMEM;
248                 spin_lock(&mq_lock);
249                 queues_count--;
250                 goto out_lock;
251         }
252
253         dir->i_size += DIRENT_SIZE;
254         dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
255
256         d_instantiate(dentry, inode);
257         dget(dentry);
258         return 0;
259 out_lock:
260         spin_unlock(&mq_lock);
261         return error;
262 }
263
264 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
265 {
266         struct inode *inode = dentry->d_inode;
267
268         dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
269         dir->i_size -= DIRENT_SIZE;
270         inode->i_nlink--;
271         dput(dentry);
272         return 0;
273 }
274
275 /*
276 *       This is routine for system read from queue file.
277 *       To avoid mess with doing here some sort of mq_receive we allow
278 *       to read only queue size & notification info (the only values
279 *       that are interesting from user point of view and aren't accessible
280 *       through std routines)
281 */
282 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
283                                 size_t count, loff_t * off)
284 {
285         struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
286         char buffer[FILENT_SIZE];
287         size_t slen;
288         loff_t o;
289
290         if (!count)
291                 return 0;
292
293         spin_lock(&info->lock);
294         snprintf(buffer, sizeof(buffer),
295                         "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
296                         info->qsize,
297                         info->notify_owner ? info->notify.sigev_notify : 0,
298                         (info->notify_owner &&
299                          info->notify.sigev_notify == SIGEV_SIGNAL) ?
300                                 info->notify.sigev_signo : 0,
301                         info->notify_owner);
302         spin_unlock(&info->lock);
303         buffer[sizeof(buffer)-1] = '\0';
304         slen = strlen(buffer)+1;
305
306         o = *off;
307         if (o > slen)
308                 return 0;
309
310         if (o + count > slen)
311                 count = slen - o;
312
313         if (copy_to_user(u_data, buffer + o, count))
314                 return -EFAULT;
315
316         *off = o + count;
317         filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
318         return count;
319 }
320
321 static int mqueue_flush_file(struct file *filp)
322 {
323         struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
324
325         spin_lock(&info->lock);
326         if (current->tgid == info->notify_owner)
327                 remove_notification(info);
328
329         spin_unlock(&info->lock);
330         return 0;
331 }
332
333 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
334 {
335         struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
336         int retval = 0;
337
338         poll_wait(filp, &info->wait_q, poll_tab);
339
340         spin_lock(&info->lock);
341         if (info->attr.mq_curmsgs)
342                 retval = POLLIN | POLLRDNORM;
343
344         if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
345                 retval |= POLLOUT | POLLWRNORM;
346         spin_unlock(&info->lock);
347
348         return retval;
349 }
350
351 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
352 static void wq_add(struct mqueue_inode_info *info, int sr,
353                         struct ext_wait_queue *ewp)
354 {
355         struct ext_wait_queue *walk;
356
357         ewp->task = current;
358
359         list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
360                 if (walk->task->static_prio <= current->static_prio) {
361                         list_add_tail(&ewp->list, &walk->list);
362                         return;
363                 }
364         }
365         list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
366 }
367
368 /*
369  * Puts current task to sleep. Caller must hold queue lock. After return
370  * lock isn't held.
371  * sr: SEND or RECV
372  */
373 static int wq_sleep(struct mqueue_inode_info *info, int sr,
374                         long timeout, struct ext_wait_queue *ewp)
375 {
376         int retval;
377         signed long time;
378
379         wq_add(info, sr, ewp);
380
381         for (;;) {
382                 set_current_state(TASK_INTERRUPTIBLE);
383
384                 spin_unlock(&info->lock);
385                 time = schedule_timeout(timeout);
386
387                 while (ewp->state == STATE_PENDING)
388                         cpu_relax();
389
390                 if (ewp->state == STATE_READY) {
391                         retval = 0;
392                         goto out;
393                 }
394                 spin_lock(&info->lock);
395                 if (ewp->state == STATE_READY) {
396                         retval = 0;
397                         goto out_unlock;
398                 }
399                 if (signal_pending(current)) {
400                         retval = -ERESTARTSYS;
401                         break;
402                 }
403                 if (time == 0) {
404                         retval = -ETIMEDOUT;
405                         break;
406                 }
407         }
408         list_del(&ewp->list);
409 out_unlock:
410         spin_unlock(&info->lock);
411 out:
412         return retval;
413 }
414
415 /*
416  * Returns waiting task that should be serviced first or NULL if none exists
417  */
418 static struct ext_wait_queue *wq_get_first_waiter(
419                 struct mqueue_inode_info *info, int sr)
420 {
421         struct list_head *ptr;
422
423         ptr = info->e_wait_q[sr].list.prev;
424         if (ptr == &info->e_wait_q[sr].list)
425                 return NULL;
426         return list_entry(ptr, struct ext_wait_queue, list);
427 }
428
429 /* Auxiliary functions to manipulate messages' list */
430 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
431 {
432         int k;
433
434         k = info->attr.mq_curmsgs - 1;
435         while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
436                 info->messages[k + 1] = info->messages[k];
437                 k--;
438         }
439         info->attr.mq_curmsgs++;
440         info->qsize += ptr->m_ts;
441         info->messages[k + 1] = ptr;
442 }
443
444 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
445 {
446         info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
447         return info->messages[info->attr.mq_curmsgs];
448 }
449
450 static inline void set_cookie(struct sk_buff *skb, char code)
451 {
452         ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
453 }
454
455 /*
456  * The next function is only to split too long sys_mq_timedsend
457  */
458 static void __do_notify(struct mqueue_inode_info *info)
459 {
460         /* notification
461          * invoked when there is registered process and there isn't process
462          * waiting synchronously for message AND state of queue changed from
463          * empty to not empty. Here we are sure that no one is waiting
464          * synchronously. */
465         if (info->notify_owner &&
466             info->attr.mq_curmsgs == 1) {
467                 struct siginfo sig_i;
468                 switch (info->notify.sigev_notify) {
469                 case SIGEV_NONE:
470                         break;
471                 case SIGEV_SIGNAL:
472                         /* sends signal */
473
474                         sig_i.si_signo = info->notify.sigev_signo;
475                         sig_i.si_errno = 0;
476                         sig_i.si_code = SI_MESGQ;
477                         sig_i.si_value = info->notify.sigev_value;
478                         sig_i.si_pid = current->tgid;
479                         sig_i.si_uid = current->uid;
480
481                         kill_proc_info(info->notify.sigev_signo,
482                                        &sig_i, info->notify_owner);
483                         break;
484                 case SIGEV_THREAD:
485                         set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
486                         netlink_sendskb(info->notify_sock,
487                                         info->notify_cookie, 0);
488                         break;
489                 }
490                 /* after notification unregisters process */
491                 info->notify_owner = 0;
492         }
493         wake_up(&info->wait_q);
494 }
495
496 static long prepare_timeout(const struct timespec __user *u_arg)
497 {
498         struct timespec ts, nowts;
499         long timeout;
500
501         if (u_arg) {
502                 if (unlikely(copy_from_user(&ts, u_arg,
503                                         sizeof(struct timespec))))
504                         return -EFAULT;
505
506                 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
507                         || ts.tv_nsec >= NSEC_PER_SEC))
508                         return -EINVAL;
509                 nowts = CURRENT_TIME;
510                 /* first subtract as jiffies can't be too big */
511                 ts.tv_sec -= nowts.tv_sec;
512                 if (ts.tv_nsec < nowts.tv_nsec) {
513                         ts.tv_nsec += NSEC_PER_SEC;
514                         ts.tv_sec--;
515                 }
516                 ts.tv_nsec -= nowts.tv_nsec;
517                 if (ts.tv_sec < 0)
518                         return 0;
519
520                 timeout = timespec_to_jiffies(&ts) + 1;
521         } else
522                 return MAX_SCHEDULE_TIMEOUT;
523
524         return timeout;
525 }
526
527 static void remove_notification(struct mqueue_inode_info *info)
528 {
529         if (info->notify_owner != 0 &&
530             info->notify.sigev_notify == SIGEV_THREAD) {
531                 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
532                 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
533         }
534         info->notify_owner = 0;
535 }
536
537 /*
538  * Invoked when creating a new queue via sys_mq_open
539  */
540 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
541                         int oflag, mode_t mode, struct mq_attr __user *u_attr)
542 {
543         struct file *filp;
544         struct mq_attr attr;
545         int ret;
546
547         if (u_attr != NULL) {
548                 if (copy_from_user(&attr, u_attr, sizeof(attr)))
549                         return ERR_PTR(-EFAULT);
550
551                 if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0)
552                         return ERR_PTR(-EINVAL);
553                 if (capable(CAP_SYS_RESOURCE)) {
554                         if (attr.mq_maxmsg > HARD_MSGMAX)
555                                 return ERR_PTR(-EINVAL);
556                 } else {
557                         if (attr.mq_maxmsg > msg_max ||
558                                         attr.mq_msgsize > msgsize_max)
559                                 return ERR_PTR(-EINVAL);
560                 }
561                 /* store for use during create */
562                 dentry->d_fsdata = &attr;
563         }
564
565         ret = vfs_create(dir->d_inode, dentry, mode, NULL);
566         dentry->d_fsdata = NULL;
567         if (ret)
568                 return ERR_PTR(ret);
569
570         filp = dentry_open(dentry, mqueue_mnt, oflag);
571         if (!IS_ERR(filp))
572                 dget(dentry);
573
574         return filp;
575 }
576
577 /* Opens existing queue */
578 static struct file *do_open(struct dentry *dentry, int oflag)
579 {
580 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
581                                         MAY_READ | MAY_WRITE };
582         struct file *filp;
583
584         if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
585                 return ERR_PTR(-EINVAL);
586
587         if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL))
588                 return ERR_PTR(-EACCES);
589
590         filp = dentry_open(dentry, mqueue_mnt, oflag);
591
592         if (!IS_ERR(filp))
593                 dget(dentry);
594
595         return filp;
596 }
597
598 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
599                                 struct mq_attr __user *u_attr)
600 {
601         struct dentry *dentry;
602         struct file *filp;
603         char *name;
604         int fd, error;
605
606         if (IS_ERR(name = getname(u_name)))
607                 return PTR_ERR(name);
608
609         fd = get_unused_fd();
610         if (fd < 0)
611                 goto out_putname;
612
613         down(&mqueue_mnt->mnt_root->d_inode->i_sem);
614         dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
615         if (IS_ERR(dentry)) {
616                 error = PTR_ERR(dentry);
617                 goto out_err;
618         }
619         mntget(mqueue_mnt);
620
621         if (oflag & O_CREAT) {
622                 if (dentry->d_inode) {  /* entry already exists */
623                         filp = (oflag & O_EXCL) ? ERR_PTR(-EEXIST) :
624                                         do_open(dentry, oflag);
625                 } else {
626                         filp = do_create(mqueue_mnt->mnt_root, dentry,
627                                                 oflag, mode, u_attr);
628                 }
629         } else
630                 filp = (dentry->d_inode) ? do_open(dentry, oflag) :
631                                         ERR_PTR(-ENOENT);
632
633         dput(dentry);
634
635         if (IS_ERR(filp)) {
636                 error = PTR_ERR(filp);
637                 goto out_putfd;
638         }
639
640         set_close_on_exec(fd, 1);
641         fd_install(fd, filp);
642         goto out_upsem;
643
644 out_putfd:
645         mntput(mqueue_mnt);
646         put_unused_fd(fd);
647 out_err:
648         fd = error;
649 out_upsem:
650         up(&mqueue_mnt->mnt_root->d_inode->i_sem);
651 out_putname:
652         putname(name);
653         return fd;
654 }
655
656 asmlinkage long sys_mq_unlink(const char __user *u_name)
657 {
658         int err;
659         char *name;
660         struct dentry *dentry;
661         struct inode *inode = NULL;
662
663         name = getname(u_name);
664         if (IS_ERR(name))
665                 return PTR_ERR(name);
666
667         down(&mqueue_mnt->mnt_root->d_inode->i_sem);
668         dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
669         if (IS_ERR(dentry)) {
670                 err = PTR_ERR(dentry);
671                 goto out_unlock;
672         }
673
674         if (!dentry->d_inode) {
675                 err = -ENOENT;
676                 goto out_err;
677         }
678
679         inode = dentry->d_inode;
680         if (inode)
681                 atomic_inc(&inode->i_count);
682
683         err = vfs_unlink(dentry->d_parent->d_inode, dentry);
684 out_err:
685         dput(dentry);
686
687 out_unlock:
688         up(&mqueue_mnt->mnt_root->d_inode->i_sem);
689         putname(name);
690         if (inode)
691                 iput(inode);
692
693         return err;
694 }
695
696 /* Pipelined send and receive functions.
697  *
698  * If a receiver finds no waiting message, then it registers itself in the
699  * list of waiting receivers. A sender checks that list before adding the new
700  * message into the message array. If there is a waiting receiver, then it
701  * bypasses the message array and directly hands the message over to the
702  * receiver.
703  * The receiver accepts the message and returns without grabbing the queue
704  * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
705  * are necessary. The same algorithm is used for sysv semaphores, see
706  * ipc/sem.c fore more details.
707  *
708  * The same algorithm is used for senders.
709  */
710
711 /* pipelined_send() - send a message directly to the task waiting in
712  * sys_mq_timedreceive() (without inserting message into a queue).
713  */
714 static inline void pipelined_send(struct mqueue_inode_info *info,
715                                   struct msg_msg *message,
716                                   struct ext_wait_queue *receiver)
717 {
718         receiver->msg = message;
719         list_del(&receiver->list);
720         receiver->state = STATE_PENDING;
721         wake_up_process(receiver->task);
722         wmb();
723         receiver->state = STATE_READY;
724 }
725
726 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
727  * gets its message and put to the queue (we have one free place for sure). */
728 static inline void pipelined_receive(struct mqueue_inode_info *info)
729 {
730         struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
731
732         if (!sender) {
733                 /* for poll */
734                 wake_up_interruptible(&info->wait_q);
735                 return;
736         }
737         msg_insert(sender->msg, info);
738         list_del(&sender->list);
739         sender->state = STATE_PENDING;
740         wake_up_process(sender->task);
741         wmb();
742         sender->state = STATE_READY;
743 }
744
745 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
746         size_t msg_len, unsigned int msg_prio,
747         const struct timespec __user *u_abs_timeout)
748 {
749         struct file *filp;
750         struct inode *inode;
751         struct ext_wait_queue wait;
752         struct ext_wait_queue *receiver;
753         struct msg_msg *msg_ptr;
754         struct mqueue_inode_info *info;
755         long timeout;
756         int ret;
757
758         if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
759                 return -EINVAL;
760
761         timeout = prepare_timeout(u_abs_timeout);
762
763         ret = -EBADF;
764         filp = fget(mqdes);
765         if (unlikely(!filp))
766                 goto out;
767
768         inode = filp->f_dentry->d_inode;
769         if (unlikely(filp->f_op != &mqueue_file_operations))
770                 goto out_fput;
771         info = MQUEUE_I(inode);
772
773         if (unlikely(!(filp->f_mode & FMODE_WRITE)))
774                 goto out_fput;
775
776         if (unlikely(msg_len > info->attr.mq_msgsize)) {
777                 ret = -EMSGSIZE;
778                 goto out_fput;
779         }
780
781         /* First try to allocate memory, before doing anything with
782          * existing queues. */
783         msg_ptr = load_msg(u_msg_ptr, msg_len);
784         if (unlikely(IS_ERR(msg_ptr))) {
785                 ret = PTR_ERR(msg_ptr);
786                 goto out_fput;
787         }
788         msg_ptr->m_ts = msg_len;
789         msg_ptr->m_type = msg_prio;
790
791         spin_lock(&info->lock);
792
793         if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
794                 if (filp->f_flags & O_NONBLOCK) {
795                         spin_unlock(&info->lock);
796                         ret = -EAGAIN;
797                 } else if (unlikely(timeout < 0)) {
798                         spin_unlock(&info->lock);
799                         ret = timeout;
800                 } else {
801                         wait.task = current;
802                         wait.msg = (void *) msg_ptr;
803                         wait.state = STATE_NONE;
804                         ret = wq_sleep(info, SEND, timeout, &wait);
805                 }
806                 if (ret < 0)
807                         free_msg(msg_ptr);
808         } else {
809                 receiver = wq_get_first_waiter(info, RECV);
810                 if (receiver) {
811                         pipelined_send(info, msg_ptr, receiver);
812                 } else {
813                         /* adds message to the queue */
814                         msg_insert(msg_ptr, info);
815                         __do_notify(info);
816                 }
817                 inode->i_atime = inode->i_mtime = inode->i_ctime =
818                                 CURRENT_TIME;
819                 spin_unlock(&info->lock);
820                 ret = 0;
821         }
822 out_fput:
823         fput(filp);
824 out:
825         return ret;
826 }
827
828 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
829         size_t msg_len, unsigned int __user *u_msg_prio,
830         const struct timespec __user *u_abs_timeout)
831 {
832         long timeout;
833         ssize_t ret;
834         struct msg_msg *msg_ptr;
835         struct file *filp;
836         struct inode *inode;
837         struct mqueue_inode_info *info;
838         struct ext_wait_queue wait;
839
840         timeout = prepare_timeout(u_abs_timeout);
841
842         ret = -EBADF;
843         filp = fget(mqdes);
844         if (unlikely(!filp))
845                 goto out;
846
847         inode = filp->f_dentry->d_inode;
848         if (unlikely(filp->f_op != &mqueue_file_operations))
849                 goto out_fput;
850         info = MQUEUE_I(inode);
851
852         if (unlikely(!(filp->f_mode & FMODE_READ)))
853                 goto out_fput;
854
855         /* checks if buffer is big enough */
856         if (unlikely(msg_len < info->attr.mq_msgsize)) {
857                 ret = -EMSGSIZE;
858                 goto out_fput;
859         }
860
861         spin_lock(&info->lock);
862         if (info->attr.mq_curmsgs == 0) {
863                 if (filp->f_flags & O_NONBLOCK) {
864                         spin_unlock(&info->lock);
865                         ret = -EAGAIN;
866                         msg_ptr = NULL;
867                 } else if (unlikely(timeout < 0)) {
868                         spin_unlock(&info->lock);
869                         ret = timeout;
870                         msg_ptr = NULL;
871                 } else {
872                         wait.task = current;
873                         wait.state = STATE_NONE;
874                         ret = wq_sleep(info, RECV, timeout, &wait);
875                         msg_ptr = wait.msg;
876                 }
877         } else {
878                 msg_ptr = msg_get(info);
879
880                 inode->i_atime = inode->i_mtime = inode->i_ctime =
881                                 CURRENT_TIME;
882
883                 /* There is now free space in queue. */
884                 pipelined_receive(info);
885                 spin_unlock(&info->lock);
886                 ret = 0;
887         }
888         if (ret == 0) {
889                 ret = msg_ptr->m_ts;
890
891                 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
892                         store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
893                         ret = -EFAULT;
894                 }
895                 free_msg(msg_ptr);
896         }
897 out_fput:
898         fput(filp);
899 out:
900         return ret;
901 }
902
903 /*
904  * Notes: the case when user wants us to deregister (with NULL as pointer)
905  * and he isn't currently owner of notification, will be silently discarded.
906  * It isn't explicitly defined in the POSIX.
907  */
908 asmlinkage long sys_mq_notify(mqd_t mqdes,
909                                 const struct sigevent __user *u_notification)
910 {
911         int ret;
912         struct file *filp;
913         struct sock *sock;
914         struct inode *inode;
915         struct sigevent notification;
916         struct mqueue_inode_info *info;
917         struct sk_buff *nc;
918
919         nc = NULL;
920         sock = NULL;
921         if (u_notification != NULL) {
922                 if (copy_from_user(&notification, u_notification,
923                                         sizeof(struct sigevent)))
924                         return -EFAULT;
925
926                 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
927                              notification.sigev_notify != SIGEV_SIGNAL &&
928                              notification.sigev_notify != SIGEV_THREAD))
929                         return -EINVAL;
930                 if (notification.sigev_notify == SIGEV_SIGNAL &&
931                         (notification.sigev_signo < 0 ||
932                          notification.sigev_signo > _NSIG)) {
933                         return -EINVAL;
934                 }
935                 if (notification.sigev_notify == SIGEV_THREAD) {
936                         /* create the notify skb */
937                         nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
938                         ret = -ENOMEM;
939                         if (!nc)
940                                 goto out;
941                         ret = -EFAULT;
942                         if (copy_from_user(nc->data,
943                                         notification.sigev_value.sival_ptr,
944                                         NOTIFY_COOKIE_LEN)) {
945                                 goto out;
946                         }
947
948                         /* TODO: add a header? */
949                         skb_put(nc, NOTIFY_COOKIE_LEN);
950                         /* and attach it to the socket */
951 retry:
952                         filp = fget(notification.sigev_signo);
953                         ret = -EBADF;
954                         if (!filp)
955                                 goto out;
956                         sock = netlink_getsockbyfilp(filp);
957                         fput(filp);
958                         if (IS_ERR(sock)) {
959                                 ret = PTR_ERR(sock);
960                                 sock = NULL;
961                                 goto out;
962                         }
963
964                         ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
965                         if (ret == 1)
966                                 goto retry;
967                         if (ret) {
968                                 sock = NULL;
969                                 nc = NULL;
970                                 goto out;
971                         }
972                 }
973         }
974
975         ret = -EBADF;
976         filp = fget(mqdes);
977         if (!filp)
978                 goto out;
979
980         inode = filp->f_dentry->d_inode;
981         if (unlikely(filp->f_op != &mqueue_file_operations))
982                 goto out_fput;
983         info = MQUEUE_I(inode);
984
985         ret = 0;
986         spin_lock(&info->lock);
987         if (u_notification == NULL) {
988                 if (info->notify_owner == current->tgid) {
989                         remove_notification(info);
990                         inode->i_atime = inode->i_ctime = CURRENT_TIME;
991                 }
992         } else if (info->notify_owner != 0) {
993                 ret = -EBUSY;
994         } else {
995                 switch (notification.sigev_notify) {
996                 case SIGEV_NONE:
997                         info->notify.sigev_notify = SIGEV_NONE;
998                         break;
999                 case SIGEV_THREAD:
1000                         info->notify_sock = sock;
1001                         info->notify_cookie = nc;
1002                         sock = NULL;
1003                         nc = NULL;
1004                         info->notify.sigev_notify = SIGEV_THREAD;
1005                         break;
1006                 case SIGEV_SIGNAL:
1007                         info->notify.sigev_signo = notification.sigev_signo;
1008                         info->notify.sigev_value = notification.sigev_value;
1009                         info->notify.sigev_notify = SIGEV_SIGNAL;
1010                         break;
1011                 }
1012                 info->notify_owner = current->tgid;
1013                 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1014         }
1015         spin_unlock(&info->lock);
1016 out_fput:
1017         fput(filp);
1018 out:
1019         if (sock) {
1020                 netlink_detachskb(sock, nc);
1021         } else if (nc) {
1022                 dev_kfree_skb(nc);
1023         }
1024         return ret;
1025 }
1026
1027 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1028                         const struct mq_attr __user *u_mqstat,
1029                         struct mq_attr __user *u_omqstat)
1030 {
1031         int ret;
1032         struct mq_attr mqstat, omqstat;
1033         struct file *filp;
1034         struct inode *inode;
1035         struct mqueue_inode_info *info;
1036
1037         if (u_mqstat != NULL) {
1038                 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1039                         return -EFAULT;
1040                 if (mqstat.mq_flags & (~O_NONBLOCK))
1041                         return -EINVAL;
1042         }
1043
1044         ret = -EBADF;
1045         filp = fget(mqdes);
1046         if (!filp)
1047                 goto out;
1048
1049         inode = filp->f_dentry->d_inode;
1050         if (unlikely(filp->f_op != &mqueue_file_operations))
1051                 goto out_fput;
1052         info = MQUEUE_I(inode);
1053
1054         spin_lock(&info->lock);
1055
1056         omqstat = info->attr;
1057         omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1058         if (u_mqstat) {
1059                 if (mqstat.mq_flags & O_NONBLOCK)
1060                         filp->f_flags |= O_NONBLOCK;
1061                 else
1062                         filp->f_flags &= ~O_NONBLOCK;
1063
1064                 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1065         }
1066
1067         spin_unlock(&info->lock);
1068
1069         ret = 0;
1070         if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1071                                                 sizeof(struct mq_attr)))
1072                 ret = -EFAULT;
1073
1074 out_fput:
1075         fput(filp);
1076 out:
1077         return ret;
1078 }
1079
1080 static struct inode_operations mqueue_dir_inode_operations = {
1081         .lookup = simple_lookup,
1082         .create = mqueue_create,
1083         .unlink = mqueue_unlink,
1084 };
1085
1086 static struct file_operations mqueue_file_operations = {
1087         .flush = mqueue_flush_file,
1088         .poll = mqueue_poll_file,
1089         .read = mqueue_read_file,
1090 };
1091
1092 static struct super_operations mqueue_super_ops = {
1093         .alloc_inode = mqueue_alloc_inode,
1094         .destroy_inode = mqueue_destroy_inode,
1095         .statfs = simple_statfs,
1096         .delete_inode = mqueue_delete_inode,
1097         .drop_inode = generic_delete_inode,
1098 };
1099
1100 static struct file_system_type mqueue_fs_type = {
1101         .name = "mqueue",
1102         .get_sb = mqueue_get_sb,
1103         .kill_sb = kill_litter_super,
1104 };
1105
1106 static int msg_max_limit_min = DFLT_MSGMAX;
1107 static int msg_max_limit_max = HARD_MSGMAX;
1108
1109 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1110 static int msg_maxsize_limit_max = INT_MAX;
1111
1112 static ctl_table mq_sysctls[] = {
1113         {
1114                 .ctl_name       = CTL_QUEUESMAX,
1115                 .procname       = "queues_max",
1116                 .data           = &queues_max,
1117                 .maxlen         = sizeof(int),
1118                 .mode           = 0644,
1119                 .proc_handler   = &proc_dointvec,
1120         },
1121         {
1122                 .ctl_name       = CTL_MSGMAX,
1123                 .procname       = "msg_max",
1124                 .data           = &msg_max,
1125                 .maxlen         = sizeof(int),
1126                 .mode           = 0644,
1127                 .proc_handler   = &proc_dointvec_minmax,
1128                 .extra1         = &msg_max_limit_min,
1129                 .extra2         = &msg_max_limit_max,
1130         },
1131         {
1132                 .ctl_name       = CTL_MSGSIZEMAX,
1133                 .procname       = "msgsize_max",
1134                 .data           = &msgsize_max,
1135                 .maxlen         = sizeof(int),
1136                 .mode           = 0644,
1137                 .proc_handler   = &proc_dointvec_minmax,
1138                 .extra1         = &msg_maxsize_limit_min,
1139                 .extra2         = &msg_maxsize_limit_max,
1140         },
1141         { .ctl_name = 0 }
1142 };
1143
1144 static ctl_table mq_sysctl_dir[] = {
1145         {
1146                 .ctl_name       = FS_MQUEUE,
1147                 .procname       = "mqueue",
1148                 .mode           = 0555,
1149                 .child          = mq_sysctls,
1150         },
1151         { .ctl_name = 0 }
1152 };
1153
1154 static ctl_table mq_sysctl_root[] = {
1155         {
1156                 .ctl_name       = CTL_FS,
1157                 .procname       = "fs",
1158                 .mode           = 0555,
1159                 .child          = mq_sysctl_dir,
1160         },
1161         { .ctl_name = 0 }
1162 };
1163
1164 static int __init init_mqueue_fs(void)
1165 {
1166         int error;
1167
1168         mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1169                                 sizeof(struct mqueue_inode_info), 0,
1170                                 SLAB_HWCACHE_ALIGN, init_once, NULL);
1171         if (mqueue_inode_cachep == NULL)
1172                 return -ENOMEM;
1173
1174         mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1175         if (!mq_sysctl_table) {
1176                 error = -ENOMEM;
1177                 goto out_cache;
1178         }
1179
1180         error = register_filesystem(&mqueue_fs_type);
1181         if (error)
1182                 goto out_sysctl;
1183
1184         if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1185                 error = PTR_ERR(mqueue_mnt);
1186                 goto out_filesystem;
1187         }
1188
1189         /* internal initialization - not common for vfs */
1190         queues_count = 0;
1191         spin_lock_init(&mq_lock);
1192
1193         return 0;
1194
1195 out_filesystem:
1196         unregister_filesystem(&mqueue_fs_type);
1197 out_sysctl:
1198         unregister_sysctl_table(mq_sysctl_table);
1199 out_cache:
1200         if (kmem_cache_destroy(mqueue_inode_cachep)) {
1201                 printk(KERN_INFO
1202                         "mqueue_inode_cache: not all structures were freed\n");
1203         }
1204         return error;
1205 }
1206
1207 __initcall(init_mqueue_fs);