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