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