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