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