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