quota: clean up Q_XQUOTASYNC
[linux-2.6.git] / fs / quota / quota.c
index 4d7fdc4..3d31228 100644 (file)
 #include <linux/capability.h>
 #include <linux/quotaops.h>
 #include <linux/types.h>
+#include <linux/writeback.h>
 #include <net/netlink.h>
 #include <net/genetlink.h>
 
-/* Check validity of generic quotactl commands */
-static int generic_quotactl_valid(struct super_block *sb, int type, int cmd,
-                                 qid_t id)
+static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
+                                    qid_t id)
 {
-       if (type >= MAXQUOTAS)
-               return -EINVAL;
-       if (!sb && cmd != Q_SYNC)
-               return -ENODEV;
-       /* Is operation supported? */
-       if (sb && !sb->s_qcop)
-               return -ENOSYS;
-
-       switch (cmd) {
-               case Q_GETFMT:
-                       break;
-               case Q_QUOTAON:
-                       if (!sb->s_qcop->quota_on)
-                               return -ENOSYS;
-                       break;
-               case Q_QUOTAOFF:
-                       if (!sb->s_qcop->quota_off)
-                               return -ENOSYS;
-                       break;
-               case Q_SETINFO:
-                       if (!sb->s_qcop->set_info)
-                               return -ENOSYS;
-                       break;
-               case Q_GETINFO:
-                       if (!sb->s_qcop->get_info)
-                               return -ENOSYS;
-                       break;
-               case Q_SETQUOTA:
-                       if (!sb->s_qcop->set_dqblk)
-                               return -ENOSYS;
-                       break;
-               case Q_GETQUOTA:
-                       if (!sb->s_qcop->get_dqblk)
-                               return -ENOSYS;
-                       break;
-               case Q_SYNC:
-                       if (sb && !sb->s_qcop->quota_sync)
-                               return -ENOSYS;
-                       break;
-               default:
-                       return -EINVAL;
-       }
-
-       /* Is quota turned on for commands which need it? */
        switch (cmd) {
-               case Q_GETFMT:
-               case Q_GETINFO:
-               case Q_SETINFO:
-               case Q_SETQUOTA:
-               case Q_GETQUOTA:
-                       /* This is just an informative test so we are satisfied
-                        * without the lock */
-                       if (!sb_has_quota_active(sb, type))
-                               return -ESRCH;
-       }
-
-       /* Check privileges */
-       if (cmd == Q_GETQUOTA) {
-               if (((type == USRQUOTA && current_euid() != id) ||
-                    (type == GRPQUOTA && !in_egroup_p(id))) &&
-                   !capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-       }
-       else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
-       return 0;
-}
-
-/* Check validity of XFS Quota Manager commands */
-static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
-                             qid_t id)
-{
-       if (type >= XQM_MAXQUOTAS)
-               return -EINVAL;
-       if (!sb)
-               return -ENODEV;
-       if (!sb->s_qcop)
-               return -ENOSYS;
-
-       switch (cmd) {
-               case Q_XQUOTAON:
-               case Q_XQUOTAOFF:
-               case Q_XQUOTARM:
-                       if (!sb->s_qcop->set_xstate)
-                               return -ENOSYS;
-                       break;
-               case Q_XGETQSTAT:
-                       if (!sb->s_qcop->get_xstate)
-                               return -ENOSYS;
-                       break;
-               case Q_XSETQLIM:
-                       if (!sb->s_qcop->set_xquota)
-                               return -ENOSYS;
-                       break;
-               case Q_XGETQUOTA:
-                       if (!sb->s_qcop->get_xquota)
-                               return -ENOSYS;
-                       break;
-               case Q_XQUOTASYNC:
-                       if (!sb->s_qcop->quota_sync)
-                               return -ENOSYS;
+       /* these commands do not require any special privilegues */
+       case Q_GETFMT:
+       case Q_SYNC:
+       case Q_GETINFO:
+       case Q_XGETQSTAT:
+       case Q_XQUOTASYNC:
+               break;
+       /* allow to query information for dquots we "own" */
+       case Q_GETQUOTA:
+       case Q_XGETQUOTA:
+               if ((type == USRQUOTA && current_euid() == id) ||
+                   (type == GRPQUOTA && in_egroup_p(id)))
                        break;
-               default:
-                       return -EINVAL;
-       }
-
-       /* Check privileges */
-       if (cmd == Q_XGETQUOTA) {
-               if (((type == XQM_USRQUOTA && current_euid() != id) ||
-                    (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
-                    !capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-       } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
+               /*FALLTHROUGH*/
+       default:
                if (!capable(CAP_SYS_ADMIN))
                        return -EPERM;
        }
 
-       return 0;
-}
-
-static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
-                               qid_t id)
-{
-       int error;
-
-       if (XQM_COMMAND(cmd))
-               error = xqm_quotactl_valid(sb, type, cmd, id);
-       else
-               error = generic_quotactl_valid(sb, type, cmd, id);
-       if (!error)
-               error = security_quotactl(cmd, type, id, sb);
-       return error;
+       return security_quotactl(cmd, type, id, sb);
 }
 
 #ifdef CONFIG_QUOTA
@@ -166,7 +53,7 @@ void sync_quota_sb(struct super_block *sb, int type)
 {
        int cnt;
 
-       if (!sb->s_qcop->quota_sync)
+       if (!sb->s_qcop || !sb->s_qcop->quota_sync)
                return;
 
        sb->s_qcop->quota_sync(sb, type);
@@ -199,10 +86,17 @@ void sync_quota_sb(struct super_block *sb, int type)
 }
 #endif
 
-static void sync_dquots(int type)
+static int quota_sync_all(int type)
 {
        struct super_block *sb;
        int cnt;
+       int ret;
+
+       if (type >= MAXQUOTAS)
+               return -EINVAL;
+       ret = security_quotactl(Q_SYNC, type, 0, NULL);
+       if (ret)
+               return ret;
 
        spin_lock(&sb_lock);
 restart:
@@ -232,18 +126,21 @@ restart:
                        goto restart;
        }
        spin_unlock(&sb_lock);
+
+       return 0;
 }
 
 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
                         void __user *addr)
 {
        char *pathname;
-       int ret;
+       int ret = -ENOSYS;
 
        pathname = getname(addr);
        if (IS_ERR(pathname))
                return PTR_ERR(pathname);
-       ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
+       if (sb->s_qcop->quota_on)
+               ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
        putname(pathname);
        return ret;
 }
@@ -269,6 +166,10 @@ static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
        struct if_dqinfo info;
        int ret;
 
+       if (!sb_has_quota_active(sb, type))
+               return -ESRCH;
+       if (!sb->s_qcop->get_info)
+               return -ENOSYS;
        ret = sb->s_qcop->get_info(sb, type, &info);
        if (!ret && copy_to_user(addr, &info, sizeof(info)))
                return -EFAULT;
@@ -281,6 +182,10 @@ static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
 
        if (copy_from_user(&info, addr, sizeof(info)))
                return -EFAULT;
+       if (!sb_has_quota_active(sb, type))
+               return -ESRCH;
+       if (!sb->s_qcop->set_info)
+               return -ENOSYS;
        return sb->s_qcop->set_info(sb, type, &info);
 }
 
@@ -290,6 +195,10 @@ static int quota_getquota(struct super_block *sb, int type, qid_t id,
        struct if_dqblk idq;
        int ret;
 
+       if (!sb_has_quota_active(sb, type))
+               return -ESRCH;
+       if (!sb->s_qcop->get_dqblk)
+               return -ENOSYS;
        ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
        if (ret)
                return ret;
@@ -305,6 +214,10 @@ static int quota_setquota(struct super_block *sb, int type, qid_t id,
 
        if (copy_from_user(&idq, addr, sizeof(idq)))
                return -EFAULT;
+       if (!sb_has_quota_active(sb, type))
+               return -ESRCH;
+       if (!sb->s_qcop->set_dqblk)
+               return -ENOSYS;
        return sb->s_qcop->set_dqblk(sb, type, id, &idq);
 }
 
@@ -314,6 +227,8 @@ static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
 
        if (copy_from_user(&flags, addr, sizeof(flags)))
                return -EFAULT;
+       if (!sb->s_qcop->set_xstate)
+               return -ENOSYS;
        return sb->s_qcop->set_xstate(sb, flags, cmd);
 }
 
@@ -321,7 +236,9 @@ static int quota_getxstate(struct super_block *sb, void __user *addr)
 {
        struct fs_quota_stat fqs;
        int ret;
-               
+
+       if (!sb->s_qcop->get_xstate)
+               return -ENOSYS;
        ret = sb->s_qcop->get_xstate(sb, &fqs);
        if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
                return -EFAULT;
@@ -335,6 +252,8 @@ static int quota_setxquota(struct super_block *sb, int type, qid_t id,
 
        if (copy_from_user(&fdq, addr, sizeof(fdq)))
                return -EFAULT;
+       if (!sb->s_qcop->set_xquota)
+               return -ENOSYS;
        return sb->s_qcop->set_xquota(sb, type, id, &fdq);
 }
 
@@ -344,6 +263,8 @@ static int quota_getxquota(struct super_block *sb, int type, qid_t id,
        struct fs_disk_quota fdq;
        int ret;
 
+       if (!sb->s_qcop->get_xquota)
+               return -ENOSYS;
        ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
        if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
                return -EFAULT;
@@ -354,10 +275,23 @@ static int quota_getxquota(struct super_block *sb, int type, qid_t id,
 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
                       void __user *addr)
 {
+       int ret;
+
+       if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
+               return -EINVAL;
+       if (!sb->s_qcop)
+               return -ENOSYS;
+
+       ret = check_quotactl_permission(sb, type, cmd, id);
+       if (ret < 0)
+               return ret;
+
        switch (cmd) {
        case Q_QUOTAON:
                return quota_quotaon(sb, type, cmd, id, addr);
        case Q_QUOTAOFF:
+               if (!sb->s_qcop->quota_off)
+                       return -ENOSYS;
                return sb->s_qcop->quota_off(sb, type, 0);
        case Q_GETFMT:
                return quota_getfmt(sb, type, addr);
@@ -370,10 +304,9 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
        case Q_SETQUOTA:
                return quota_setquota(sb, type, id, addr);
        case Q_SYNC:
-               if (sb)
-                       sync_quota_sb(sb, type);
-               else
-                       sync_dquots(type);
+               if (!sb->s_qcop->quota_sync)
+                       return -ENOSYS;
+               sync_quota_sb(sb, type);
                return 0;
        case Q_XQUOTAON:
        case Q_XQUOTAOFF:
@@ -386,13 +319,14 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
        case Q_XGETQUOTA:
                return quota_getxquota(sb, type, id, addr);
        case Q_XQUOTASYNC:
-               return sb->s_qcop->quota_sync(sb, type);
-       /* We never reach here unless validity check is broken */
+               /* caller already holds s_umount */
+               if (sb->s_flags & MS_RDONLY)
+                       return -EROFS;
+               writeback_inodes_sb(sb);
+               return 0;
        default:
-               BUG();
+               return -EINVAL;
        }
-
-       return 0;
 }
 
 /*
@@ -439,18 +373,24 @@ SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
        cmds = cmd >> SUBCMDSHIFT;
        type = cmd & SUBCMDMASK;
 
-       if (cmds != Q_SYNC || special) {
-               sb = quotactl_block(special);
-               if (IS_ERR(sb))
-                       return PTR_ERR(sb);
+       /*
+        * As a special case Q_SYNC can be called without a specific device.
+        * It will iterate all superblocks that have quota enabled and call
+        * the sync action on each of them.
+        */
+       if (!special) {
+               if (cmds == Q_SYNC)
+                       return quota_sync_all(type);
+               return -ENODEV;
        }
 
-       ret = check_quotactl_valid(sb, type, cmds, id);
-       if (ret >= 0)
-               ret = do_quotactl(sb, type, cmds, id, addr);
-       if (sb)
-               drop_super(sb);
+       sb = quotactl_block(special);
+       if (IS_ERR(sb))
+               return PTR_ERR(sb);
+
+       ret = do_quotactl(sb, type, cmds, id, addr);
 
+       drop_super(sb);
        return ret;
 }