backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[linux-2.6.git] / ipc / mqueue.c
index cb0cd3c..247d3a1 100644 (file)
@@ -2,15 +2,18 @@
  * POSIX message queues filesystem for Linux.
  *
  * Copyright (C) 2003,2004  Krzysztof Benedyczak    (golbi@mat.uni.torun.pl)
- *                          Michal Wronski          (wrona@mat.uni.torun.pl)
+ *                          Michal Wronski          (Michal.Wronski@motorola.com)
  *
  * Spinlocks:               Mohamed Abbas           (abbas.mohamed@intel.com)
  * Lockless receive & send, fd based notify:
  *                         Manfred Spraul          (manfred@colorfullife.com)
  *
+ * Audit:                   George Wilson           (ltcgcw@us.ibm.com)
+ *
  * This file is released under the GPL.
  */
 
+#include <linux/capability.h>
 #include <linux/init.h>
 #include <linux/pagemap.h>
 #include <linux/file.h>
 #include <linux/skbuff.h>
 #include <linux/netlink.h>
 #include <linux/syscalls.h>
+#include <linux/audit.h>
+#include <linux/signal.h>
+#include <linux/mutex.h>
+#include <linux/vs_base.h>
+#include <linux/vs_context.h>
+#include <linux/vs_limit.h>
+
 #include <net/sock.h>
 #include "util.h"
 
@@ -49,7 +59,6 @@
 #define HARD_MSGMAX    (131072/sizeof(void*))
 #define DFLT_MSGSIZEMAX 8192   /* max message size */
 
-#define NOTIFY_COOKIE_LEN      32
 
 struct ext_wait_queue {                /* queue of sleeping tasks */
        struct task_struct *task;
@@ -68,7 +77,7 @@ struct mqueue_inode_info {
 
        struct sigevent notify;
        pid_t notify_owner;
-       struct user_struct *user;       /* user who created, for accouting */
+       struct user_struct *user;       /* user who created, for accounting */
        struct sock *notify_sock;
        struct sk_buff *notify_cookie;
 
@@ -109,7 +118,6 @@ static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
                inode->i_mode = mode;
                inode->i_uid = current->fsuid;
                inode->i_gid = current->fsgid;
-               inode->i_blksize = PAGE_CACHE_SIZE;
                inode->i_blocks = 0;
                inode->i_mtime = inode->i_ctime = inode->i_atime =
                                CURRENT_TIME;
@@ -146,17 +154,20 @@ static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
                        spin_lock(&mq_lock);
                        if (u->mq_bytes + mq_bytes < u->mq_bytes ||
                            u->mq_bytes + mq_bytes >
-                           p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur) {
+                           p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur ||
+                           !vx_ipcmsg_avail(p->vx_info, mq_bytes)) {
                                spin_unlock(&mq_lock);
                                goto out_inode;
                        }
                        u->mq_bytes += mq_bytes;
+                       vx_ipcmsg_add(p->vx_info, u, mq_bytes);
                        spin_unlock(&mq_lock);
 
                        info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
                        if (!info->messages) {
                                spin_lock(&mq_lock);
                                u->mq_bytes -= mq_bytes;
+                               vx_ipcmsg_sub(p->vx_info, u, mq_bytes);
                                spin_unlock(&mq_lock);
                                goto out_inode;
                        }
@@ -199,11 +210,11 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
        return 0;
 }
 
-static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
-                                        int flags, const char *dev_name,
-                                        void *data)
+static int mqueue_get_sb(struct file_system_type *fs_type,
+                        int flags, const char *dev_name,
+                        void *data, struct vfsmount *mnt)
 {
-       return get_sb_single(fs_type, flags, data, mqueue_fill_super);
+       return get_sb_single(fs_type, flags, data, mqueue_fill_super, mnt);
 }
 
 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
@@ -254,10 +265,14 @@ static void mqueue_delete_inode(struct inode *inode)
                   (info->attr.mq_maxmsg * info->attr.mq_msgsize));
        user = info->user;
        if (user) {
+               struct vx_info *vxi = lookup_vx_info(user->xid);
+
                spin_lock(&mq_lock);
                user->mq_bytes -= mq_bytes;
+               vx_ipcmsg_sub(vxi, user, mq_bytes);
                queues_count--;
                spin_unlock(&mq_lock);
+               put_vx_info(vxi);
                free_uid(user);
        }
 }
@@ -353,7 +368,7 @@ static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
        return count;
 }
 
-static int mqueue_flush_file(struct file *filp)
+static int mqueue_flush_file(struct file *filp, fl_owner_t id)
 {
        struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
 
@@ -597,29 +612,32 @@ static int mq_attr_ok(struct mq_attr *attr)
 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
                        int oflag, mode_t mode, struct mq_attr __user *u_attr)
 {
-       struct file *filp;
        struct mq_attr attr;
        int ret;
 
-       if (u_attr != NULL) {
+       if (u_attr) {
+               ret = -EFAULT;
                if (copy_from_user(&attr, u_attr, sizeof(attr)))
-                       return ERR_PTR(-EFAULT);
+                       goto out;
+               ret = -EINVAL;
                if (!mq_attr_ok(&attr))
-                       return ERR_PTR(-EINVAL);
+                       goto out;
                /* store for use during create */
                dentry->d_fsdata = &attr;
        }
 
+       mode &= ~current->fs->umask;
        ret = vfs_create(dir->d_inode, dentry, mode, NULL);
        dentry->d_fsdata = NULL;
        if (ret)
-               return ERR_PTR(ret);
+               goto out;
 
-       filp = dentry_open(dentry, mqueue_mnt, oflag);
-       if (!IS_ERR(filp))
-               dget(dentry);
+       return dentry_open(dentry, mqueue_mnt, oflag);
 
-       return filp;
+out:
+       dput(dentry);
+       mntput(mqueue_mnt);
+       return ERR_PTR(ret);
 }
 
 /* Opens existing queue */
@@ -627,20 +645,20 @@ static struct file *do_open(struct dentry *dentry, int oflag)
 {
 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
                                        MAY_READ | MAY_WRITE };
-       struct file *filp;
 
-       if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
+       if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
+               dput(dentry);
+               mntput(mqueue_mnt);
                return ERR_PTR(-EINVAL);
+       }
 
-       if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL))
+       if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
+               dput(dentry);
+               mntput(mqueue_mnt);
                return ERR_PTR(-EACCES);
+       }
 
-       filp = dentry_open(dentry, mqueue_mnt, oflag);
-
-       if (!IS_ERR(filp))
-               dget(dentry);
-
-       return filp;
+       return dentry_open(dentry, mqueue_mnt, oflag);
 }
 
 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
@@ -651,6 +669,10 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
        char *name;
        int fd, error;
 
+       error = audit_mq_open(oflag, mode, u_attr);
+       if (error != 0)
+               return error;
+
        if (IS_ERR(name = getname(u_name)))
                return PTR_ERR(name);
 
@@ -658,7 +680,7 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
        if (fd < 0)
                goto out_putname;
 
-       down(&mqueue_mnt->mnt_root->d_inode->i_sem);
+       mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
        dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
        if (IS_ERR(dentry)) {
                error = PTR_ERR(dentry);
@@ -668,17 +690,20 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
 
        if (oflag & O_CREAT) {
                if (dentry->d_inode) {  /* entry already exists */
-                       filp = (oflag & O_EXCL) ? ERR_PTR(-EEXIST) :
-                                       do_open(dentry, oflag);
+                       error = -EEXIST;
+                       if (oflag & O_EXCL)
+                               goto out;
+                       filp = do_open(dentry, oflag);
                } else {
                        filp = do_create(mqueue_mnt->mnt_root, dentry,
                                                oflag, mode, u_attr);
                }
-       } else
-               filp = (dentry->d_inode) ? do_open(dentry, oflag) :
-                                       ERR_PTR(-ENOENT);
-
-       dput(dentry);
+       } else {
+               error = -ENOENT;
+               if (!dentry->d_inode)
+                       goto out;
+               filp = do_open(dentry, oflag);
+       }
 
        if (IS_ERR(filp)) {
                error = PTR_ERR(filp);
@@ -689,13 +714,15 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
        fd_install(fd, filp);
        goto out_upsem;
 
-out_putfd:
+out:
+       dput(dentry);
        mntput(mqueue_mnt);
+out_putfd:
        put_unused_fd(fd);
 out_err:
        fd = error;
 out_upsem:
-       up(&mqueue_mnt->mnt_root->d_inode->i_sem);
+       mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
 out_putname:
        putname(name);
        return fd;
@@ -712,7 +739,7 @@ asmlinkage long sys_mq_unlink(const char __user *u_name)
        if (IS_ERR(name))
                return PTR_ERR(name);
 
-       down(&mqueue_mnt->mnt_root->d_inode->i_sem);
+       mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
        dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
        if (IS_ERR(dentry)) {
                err = PTR_ERR(dentry);
@@ -728,12 +755,12 @@ asmlinkage long sys_mq_unlink(const char __user *u_name)
        if (inode)
                atomic_inc(&inode->i_count);
 
-       err = vfs_unlink(dentry->d_parent->d_inode, dentry);
+       err = vfs_unlink(dentry->d_parent->d_inode, dentry, NULL);
 out_err:
        dput(dentry);
 
 out_unlock:
-       up(&mqueue_mnt->mnt_root->d_inode->i_sem);
+       mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
        putname(name);
        if (inode)
                iput(inode);
@@ -751,7 +778,7 @@ out_unlock:
  * The receiver accepts the message and returns without grabbing the queue
  * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  * are necessary. The same algorithm is used for sysv semaphores, see
- * ipc/sem.c fore more details.
+ * ipc/sem.c for more details.
  *
  * The same algorithm is used for senders.
  */
@@ -767,7 +794,7 @@ static inline void pipelined_send(struct mqueue_inode_info *info,
        list_del(&receiver->list);
        receiver->state = STATE_PENDING;
        wake_up_process(receiver->task);
-       wmb();
+       smp_wmb();
        receiver->state = STATE_READY;
 }
 
@@ -786,7 +813,7 @@ static inline void pipelined_receive(struct mqueue_inode_info *info)
        list_del(&sender->list);
        sender->state = STATE_PENDING;
        wake_up_process(sender->task);
-       wmb();
+       smp_wmb();
        sender->state = STATE_READY;
 }
 
@@ -803,6 +830,10 @@ asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
        long timeout;
        int ret;
 
+       ret = audit_mq_timedsend(mqdes, msg_len, msg_prio, u_abs_timeout);
+       if (ret != 0)
+               return ret;
+
        if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
                return -EINVAL;
 
@@ -885,6 +916,10 @@ asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
        struct mqueue_inode_info *info;
        struct ext_wait_queue wait;
 
+       ret = audit_mq_timedreceive(mqdes, msg_len, u_msg_prio, u_abs_timeout);
+       if (ret != 0)
+               return ret;
+
        timeout = prepare_timeout(u_abs_timeout);
 
        ret = -EBADF;
@@ -964,6 +999,10 @@ asmlinkage long sys_mq_notify(mqd_t mqdes,
        struct mqueue_inode_info *info;
        struct sk_buff *nc;
 
+       ret = audit_mq_notify(mqdes, u_notification);
+       if (ret != 0)
+               return ret;
+
        nc = NULL;
        sock = NULL;
        if (u_notification != NULL) {
@@ -976,8 +1015,7 @@ asmlinkage long sys_mq_notify(mqd_t mqdes,
                             notification.sigev_notify != SIGEV_THREAD))
                        return -EINVAL;
                if (notification.sigev_notify == SIGEV_SIGNAL &&
-                       (notification.sigev_signo < 0 ||
-                        notification.sigev_signo > _NSIG)) {
+                       !valid_signal(notification.sigev_signo)) {
                        return -EINVAL;
                }
                if (notification.sigev_notify == SIGEV_THREAD) {
@@ -1009,7 +1047,8 @@ retry:
                                goto out;
                        }
 
-                       ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
+                       ret = netlink_attachskb(sock, nc, 0,
+                                       MAX_SCHEDULE_TIMEOUT, NULL);
                        if (ret == 1)
                                goto retry;
                        if (ret) {
@@ -1104,6 +1143,9 @@ asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
        omqstat = info->attr;
        omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
        if (u_mqstat) {
+               ret = audit_mq_getsetattr(mqdes, &mqstat);
+               if (ret != 0)
+                       goto out;
                if (mqstat.mq_flags & O_NONBLOCK)
                        filp->f_flags |= O_NONBLOCK;
                else