1884d179dSJan Kara /* 2884d179dSJan Kara * Quota code necessary even when VFS quota support is not compiled 3884d179dSJan Kara * into the kernel. The interesting stuff is over in dquot.c, here 4884d179dSJan Kara * we have symbols for initial quotactl(2) handling, the sysctl(2) 5884d179dSJan Kara * variables, etc - things needed even when quota support disabled. 6884d179dSJan Kara */ 7884d179dSJan Kara 8884d179dSJan Kara #include <linux/fs.h> 9884d179dSJan Kara #include <linux/namei.h> 10884d179dSJan Kara #include <linux/slab.h> 11884d179dSJan Kara #include <asm/current.h> 12884d179dSJan Kara #include <asm/uaccess.h> 13884d179dSJan Kara #include <linux/kernel.h> 14884d179dSJan Kara #include <linux/security.h> 15884d179dSJan Kara #include <linux/syscalls.h> 16884d179dSJan Kara #include <linux/buffer_head.h> 17884d179dSJan Kara #include <linux/capability.h> 18884d179dSJan Kara #include <linux/quotaops.h> 19884d179dSJan Kara #include <linux/types.h> 208c4e4acdSChristoph Hellwig #include <linux/writeback.h> 21884d179dSJan Kara 22c988afb5SChristoph Hellwig static int check_quotactl_permission(struct super_block *sb, int type, int cmd, 23268157baSJan Kara qid_t id) 24884d179dSJan Kara { 25c988afb5SChristoph Hellwig switch (cmd) { 26c988afb5SChristoph Hellwig /* these commands do not require any special privilegues */ 27c988afb5SChristoph Hellwig case Q_GETFMT: 28c988afb5SChristoph Hellwig case Q_SYNC: 29c988afb5SChristoph Hellwig case Q_GETINFO: 30c988afb5SChristoph Hellwig case Q_XGETQSTAT: 31c988afb5SChristoph Hellwig case Q_XQUOTASYNC: 32c988afb5SChristoph Hellwig break; 33c988afb5SChristoph Hellwig /* allow to query information for dquots we "own" */ 34c988afb5SChristoph Hellwig case Q_GETQUOTA: 35c988afb5SChristoph Hellwig case Q_XGETQUOTA: 36c988afb5SChristoph Hellwig if ((type == USRQUOTA && current_euid() == id) || 37c988afb5SChristoph Hellwig (type == GRPQUOTA && in_egroup_p(id))) 38c988afb5SChristoph Hellwig break; 39c988afb5SChristoph Hellwig /*FALLTHROUGH*/ 40c988afb5SChristoph Hellwig default: 41884d179dSJan Kara if (!capable(CAP_SYS_ADMIN)) 42884d179dSJan Kara return -EPERM; 43884d179dSJan Kara } 44884d179dSJan Kara 45c988afb5SChristoph Hellwig return security_quotactl(cmd, type, id, sb); 46884d179dSJan Kara } 47884d179dSJan Kara 4801a05b33SAl Viro static void quota_sync_one(struct super_block *sb, void *arg) 4901a05b33SAl Viro { 5001a05b33SAl Viro if (sb->s_qcop && sb->s_qcop->quota_sync) 5101a05b33SAl Viro sb->s_qcop->quota_sync(sb, *(int *)arg, 1); 5201a05b33SAl Viro } 5301a05b33SAl Viro 546ae09575SChristoph Hellwig static int quota_sync_all(int type) 55884d179dSJan Kara { 566ae09575SChristoph Hellwig int ret; 576ae09575SChristoph Hellwig 586ae09575SChristoph Hellwig if (type >= MAXQUOTAS) 596ae09575SChristoph Hellwig return -EINVAL; 606ae09575SChristoph Hellwig ret = security_quotactl(Q_SYNC, type, 0, NULL); 6101a05b33SAl Viro if (!ret) 6201a05b33SAl Viro iterate_supers(quota_sync_one, &type); 636ae09575SChristoph Hellwig return ret; 64884d179dSJan Kara } 65884d179dSJan Kara 66c411e5f6SChristoph Hellwig static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id, 67*f00c9e44SJan Kara struct path *path) 68884d179dSJan Kara { 69*f00c9e44SJan Kara if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta) 70*f00c9e44SJan Kara return -ENOSYS; 71*f00c9e44SJan Kara if (sb->s_qcop->quota_on_meta) 72*f00c9e44SJan Kara return sb->s_qcop->quota_on_meta(sb, type, id); 73*f00c9e44SJan Kara if (IS_ERR(path)) 74*f00c9e44SJan Kara return PTR_ERR(path); 75*f00c9e44SJan Kara return sb->s_qcop->quota_on(sb, type, id, path); 76884d179dSJan Kara } 77884d179dSJan Kara 78c411e5f6SChristoph Hellwig static int quota_getfmt(struct super_block *sb, int type, void __user *addr) 79c411e5f6SChristoph Hellwig { 80884d179dSJan Kara __u32 fmt; 81884d179dSJan Kara 82884d179dSJan Kara down_read(&sb_dqopt(sb)->dqptr_sem); 83884d179dSJan Kara if (!sb_has_quota_active(sb, type)) { 84884d179dSJan Kara up_read(&sb_dqopt(sb)->dqptr_sem); 85884d179dSJan Kara return -ESRCH; 86884d179dSJan Kara } 87884d179dSJan Kara fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; 88884d179dSJan Kara up_read(&sb_dqopt(sb)->dqptr_sem); 89884d179dSJan Kara if (copy_to_user(addr, &fmt, sizeof(fmt))) 90884d179dSJan Kara return -EFAULT; 91884d179dSJan Kara return 0; 92884d179dSJan Kara } 93c411e5f6SChristoph Hellwig 94c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr) 95c411e5f6SChristoph Hellwig { 96884d179dSJan Kara struct if_dqinfo info; 97c411e5f6SChristoph Hellwig int ret; 98884d179dSJan Kara 99f450d4feSChristoph Hellwig if (!sb->s_qcop->get_info) 100f450d4feSChristoph Hellwig return -ENOSYS; 101268157baSJan Kara ret = sb->s_qcop->get_info(sb, type, &info); 102c411e5f6SChristoph Hellwig if (!ret && copy_to_user(addr, &info, sizeof(info))) 103884d179dSJan Kara return -EFAULT; 104c411e5f6SChristoph Hellwig return ret; 105884d179dSJan Kara } 106c411e5f6SChristoph Hellwig 107c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr) 108c411e5f6SChristoph Hellwig { 109884d179dSJan Kara struct if_dqinfo info; 110884d179dSJan Kara 111884d179dSJan Kara if (copy_from_user(&info, addr, sizeof(info))) 112884d179dSJan Kara return -EFAULT; 113f450d4feSChristoph Hellwig if (!sb->s_qcop->set_info) 114f450d4feSChristoph Hellwig return -ENOSYS; 115884d179dSJan Kara return sb->s_qcop->set_info(sb, type, &info); 116884d179dSJan Kara } 117c411e5f6SChristoph Hellwig 118b9b2dd36SChristoph Hellwig static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src) 119b9b2dd36SChristoph Hellwig { 120b9b2dd36SChristoph Hellwig dst->dqb_bhardlimit = src->d_blk_hardlimit; 121b9b2dd36SChristoph Hellwig dst->dqb_bsoftlimit = src->d_blk_softlimit; 122b9b2dd36SChristoph Hellwig dst->dqb_curspace = src->d_bcount; 123b9b2dd36SChristoph Hellwig dst->dqb_ihardlimit = src->d_ino_hardlimit; 124b9b2dd36SChristoph Hellwig dst->dqb_isoftlimit = src->d_ino_softlimit; 125b9b2dd36SChristoph Hellwig dst->dqb_curinodes = src->d_icount; 126b9b2dd36SChristoph Hellwig dst->dqb_btime = src->d_btimer; 127b9b2dd36SChristoph Hellwig dst->dqb_itime = src->d_itimer; 128b9b2dd36SChristoph Hellwig dst->dqb_valid = QIF_ALL; 129b9b2dd36SChristoph Hellwig } 130b9b2dd36SChristoph Hellwig 131c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id, 132c411e5f6SChristoph Hellwig void __user *addr) 133c411e5f6SChristoph Hellwig { 134b9b2dd36SChristoph Hellwig struct fs_disk_quota fdq; 135884d179dSJan Kara struct if_dqblk idq; 136c411e5f6SChristoph Hellwig int ret; 137884d179dSJan Kara 138f450d4feSChristoph Hellwig if (!sb->s_qcop->get_dqblk) 139f450d4feSChristoph Hellwig return -ENOSYS; 140b9b2dd36SChristoph Hellwig ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); 141268157baSJan Kara if (ret) 142884d179dSJan Kara return ret; 143b9b2dd36SChristoph Hellwig copy_to_if_dqblk(&idq, &fdq); 144884d179dSJan Kara if (copy_to_user(addr, &idq, sizeof(idq))) 145884d179dSJan Kara return -EFAULT; 146884d179dSJan Kara return 0; 147884d179dSJan Kara } 148c411e5f6SChristoph Hellwig 149c472b432SChristoph Hellwig static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src) 150c472b432SChristoph Hellwig { 151c472b432SChristoph Hellwig dst->d_blk_hardlimit = src->dqb_bhardlimit; 152c472b432SChristoph Hellwig dst->d_blk_softlimit = src->dqb_bsoftlimit; 153c472b432SChristoph Hellwig dst->d_bcount = src->dqb_curspace; 154c472b432SChristoph Hellwig dst->d_ino_hardlimit = src->dqb_ihardlimit; 155c472b432SChristoph Hellwig dst->d_ino_softlimit = src->dqb_isoftlimit; 156c472b432SChristoph Hellwig dst->d_icount = src->dqb_curinodes; 157c472b432SChristoph Hellwig dst->d_btimer = src->dqb_btime; 158c472b432SChristoph Hellwig dst->d_itimer = src->dqb_itime; 159c472b432SChristoph Hellwig 160c472b432SChristoph Hellwig dst->d_fieldmask = 0; 161c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BLIMITS) 162c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD; 163c472b432SChristoph Hellwig if (src->dqb_valid & QIF_SPACE) 164c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_BCOUNT; 165c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ILIMITS) 166c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD; 167c472b432SChristoph Hellwig if (src->dqb_valid & QIF_INODES) 168c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_ICOUNT; 169c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BTIME) 170c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_BTIMER; 171c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ITIME) 172c472b432SChristoph Hellwig dst->d_fieldmask |= FS_DQ_ITIMER; 173c472b432SChristoph Hellwig } 174c472b432SChristoph Hellwig 175c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id, 176c411e5f6SChristoph Hellwig void __user *addr) 177c411e5f6SChristoph Hellwig { 178c472b432SChristoph Hellwig struct fs_disk_quota fdq; 179884d179dSJan Kara struct if_dqblk idq; 180884d179dSJan Kara 181884d179dSJan Kara if (copy_from_user(&idq, addr, sizeof(idq))) 182884d179dSJan Kara return -EFAULT; 183f450d4feSChristoph Hellwig if (!sb->s_qcop->set_dqblk) 184f450d4feSChristoph Hellwig return -ENOSYS; 185c472b432SChristoph Hellwig copy_from_if_dqblk(&fdq, &idq); 186c472b432SChristoph Hellwig return sb->s_qcop->set_dqblk(sb, type, id, &fdq); 187884d179dSJan Kara } 188884d179dSJan Kara 189c411e5f6SChristoph Hellwig static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr) 190c411e5f6SChristoph Hellwig { 191884d179dSJan Kara __u32 flags; 192884d179dSJan Kara 193884d179dSJan Kara if (copy_from_user(&flags, addr, sizeof(flags))) 194884d179dSJan Kara return -EFAULT; 195f450d4feSChristoph Hellwig if (!sb->s_qcop->set_xstate) 196f450d4feSChristoph Hellwig return -ENOSYS; 197884d179dSJan Kara return sb->s_qcop->set_xstate(sb, flags, cmd); 198884d179dSJan Kara } 199884d179dSJan Kara 200c411e5f6SChristoph Hellwig static int quota_getxstate(struct super_block *sb, void __user *addr) 201c411e5f6SChristoph Hellwig { 202c411e5f6SChristoph Hellwig struct fs_quota_stat fqs; 203c411e5f6SChristoph Hellwig int ret; 204c411e5f6SChristoph Hellwig 205f450d4feSChristoph Hellwig if (!sb->s_qcop->get_xstate) 206f450d4feSChristoph Hellwig return -ENOSYS; 207c411e5f6SChristoph Hellwig ret = sb->s_qcop->get_xstate(sb, &fqs); 208c411e5f6SChristoph Hellwig if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 209884d179dSJan Kara return -EFAULT; 210c411e5f6SChristoph Hellwig return ret; 211884d179dSJan Kara } 212c411e5f6SChristoph Hellwig 213c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id, 214c411e5f6SChristoph Hellwig void __user *addr) 215c411e5f6SChristoph Hellwig { 216884d179dSJan Kara struct fs_disk_quota fdq; 217884d179dSJan Kara 218884d179dSJan Kara if (copy_from_user(&fdq, addr, sizeof(fdq))) 219884d179dSJan Kara return -EFAULT; 220c472b432SChristoph Hellwig if (!sb->s_qcop->set_dqblk) 221f450d4feSChristoph Hellwig return -ENOSYS; 222c472b432SChristoph Hellwig return sb->s_qcop->set_dqblk(sb, type, id, &fdq); 223884d179dSJan Kara } 224c411e5f6SChristoph Hellwig 225c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id, 226c411e5f6SChristoph Hellwig void __user *addr) 227c411e5f6SChristoph Hellwig { 228884d179dSJan Kara struct fs_disk_quota fdq; 229c411e5f6SChristoph Hellwig int ret; 230884d179dSJan Kara 231b9b2dd36SChristoph Hellwig if (!sb->s_qcop->get_dqblk) 232f450d4feSChristoph Hellwig return -ENOSYS; 233b9b2dd36SChristoph Hellwig ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); 234c411e5f6SChristoph Hellwig if (!ret && copy_to_user(addr, &fdq, sizeof(fdq))) 235884d179dSJan Kara return -EFAULT; 236c411e5f6SChristoph Hellwig return ret; 237884d179dSJan Kara } 238c411e5f6SChristoph Hellwig 239c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */ 240c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, 241*f00c9e44SJan Kara void __user *addr, struct path *path) 242c411e5f6SChristoph Hellwig { 243c988afb5SChristoph Hellwig int ret; 244c988afb5SChristoph Hellwig 245c988afb5SChristoph Hellwig if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS)) 246c988afb5SChristoph Hellwig return -EINVAL; 247c988afb5SChristoph Hellwig if (!sb->s_qcop) 248c988afb5SChristoph Hellwig return -ENOSYS; 249c988afb5SChristoph Hellwig 250c988afb5SChristoph Hellwig ret = check_quotactl_permission(sb, type, cmd, id); 251c988afb5SChristoph Hellwig if (ret < 0) 252c988afb5SChristoph Hellwig return ret; 253c988afb5SChristoph Hellwig 254c411e5f6SChristoph Hellwig switch (cmd) { 255c411e5f6SChristoph Hellwig case Q_QUOTAON: 256*f00c9e44SJan Kara return quota_quotaon(sb, type, cmd, id, path); 257c411e5f6SChristoph Hellwig case Q_QUOTAOFF: 258f450d4feSChristoph Hellwig if (!sb->s_qcop->quota_off) 259f450d4feSChristoph Hellwig return -ENOSYS; 260307ae18aSChristoph Hellwig return sb->s_qcop->quota_off(sb, type); 261c411e5f6SChristoph Hellwig case Q_GETFMT: 262c411e5f6SChristoph Hellwig return quota_getfmt(sb, type, addr); 263c411e5f6SChristoph Hellwig case Q_GETINFO: 264c411e5f6SChristoph Hellwig return quota_getinfo(sb, type, addr); 265c411e5f6SChristoph Hellwig case Q_SETINFO: 266c411e5f6SChristoph Hellwig return quota_setinfo(sb, type, addr); 267c411e5f6SChristoph Hellwig case Q_GETQUOTA: 268c411e5f6SChristoph Hellwig return quota_getquota(sb, type, id, addr); 269c411e5f6SChristoph Hellwig case Q_SETQUOTA: 270c411e5f6SChristoph Hellwig return quota_setquota(sb, type, id, addr); 271c411e5f6SChristoph Hellwig case Q_SYNC: 272f450d4feSChristoph Hellwig if (!sb->s_qcop->quota_sync) 273f450d4feSChristoph Hellwig return -ENOSYS; 2745fb324adSChristoph Hellwig return sb->s_qcop->quota_sync(sb, type, 1); 275c411e5f6SChristoph Hellwig case Q_XQUOTAON: 276c411e5f6SChristoph Hellwig case Q_XQUOTAOFF: 277c411e5f6SChristoph Hellwig case Q_XQUOTARM: 278c411e5f6SChristoph Hellwig return quota_setxstate(sb, cmd, addr); 279c411e5f6SChristoph Hellwig case Q_XGETQSTAT: 280c411e5f6SChristoph Hellwig return quota_getxstate(sb, addr); 281c411e5f6SChristoph Hellwig case Q_XSETQLIM: 282c411e5f6SChristoph Hellwig return quota_setxquota(sb, type, id, addr); 283c411e5f6SChristoph Hellwig case Q_XGETQUOTA: 284c411e5f6SChristoph Hellwig return quota_getxquota(sb, type, id, addr); 285884d179dSJan Kara case Q_XQUOTASYNC: 2868c4e4acdSChristoph Hellwig /* caller already holds s_umount */ 2878c4e4acdSChristoph Hellwig if (sb->s_flags & MS_RDONLY) 2888c4e4acdSChristoph Hellwig return -EROFS; 2898c4e4acdSChristoph Hellwig writeback_inodes_sb(sb); 2908c4e4acdSChristoph Hellwig return 0; 291884d179dSJan Kara default: 292f450d4feSChristoph Hellwig return -EINVAL; 293884d179dSJan Kara } 294884d179dSJan Kara } 295884d179dSJan Kara 296884d179dSJan Kara /* 297884d179dSJan Kara * look up a superblock on which quota ops will be performed 298884d179dSJan Kara * - use the name of a block device to find the superblock thereon 299884d179dSJan Kara */ 3007a2435d8SJan Kara static struct super_block *quotactl_block(const char __user *special) 301884d179dSJan Kara { 302884d179dSJan Kara #ifdef CONFIG_BLOCK 303884d179dSJan Kara struct block_device *bdev; 304884d179dSJan Kara struct super_block *sb; 305884d179dSJan Kara char *tmp = getname(special); 306884d179dSJan Kara 307884d179dSJan Kara if (IS_ERR(tmp)) 308884d179dSJan Kara return ERR_CAST(tmp); 309884d179dSJan Kara bdev = lookup_bdev(tmp); 310884d179dSJan Kara putname(tmp); 311884d179dSJan Kara if (IS_ERR(bdev)) 312884d179dSJan Kara return ERR_CAST(bdev); 313884d179dSJan Kara sb = get_super(bdev); 314884d179dSJan Kara bdput(bdev); 315884d179dSJan Kara if (!sb) 316884d179dSJan Kara return ERR_PTR(-ENODEV); 317884d179dSJan Kara 318884d179dSJan Kara return sb; 319884d179dSJan Kara #else 320884d179dSJan Kara return ERR_PTR(-ENODEV); 321884d179dSJan Kara #endif 322884d179dSJan Kara } 323884d179dSJan Kara 324884d179dSJan Kara /* 325884d179dSJan Kara * This is the system call interface. This communicates with 326884d179dSJan Kara * the user-level programs. Currently this only supports diskquota 327884d179dSJan Kara * calls. Maybe we need to add the process quotas etc. in the future, 328884d179dSJan Kara * but we probably should use rlimits for that. 329884d179dSJan Kara */ 330884d179dSJan Kara SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, 331884d179dSJan Kara qid_t, id, void __user *, addr) 332884d179dSJan Kara { 333884d179dSJan Kara uint cmds, type; 334884d179dSJan Kara struct super_block *sb = NULL; 335*f00c9e44SJan Kara struct path path, *pathp = NULL; 336884d179dSJan Kara int ret; 337884d179dSJan Kara 338884d179dSJan Kara cmds = cmd >> SUBCMDSHIFT; 339884d179dSJan Kara type = cmd & SUBCMDMASK; 340884d179dSJan Kara 3416ae09575SChristoph Hellwig /* 3426ae09575SChristoph Hellwig * As a special case Q_SYNC can be called without a specific device. 3436ae09575SChristoph Hellwig * It will iterate all superblocks that have quota enabled and call 3446ae09575SChristoph Hellwig * the sync action on each of them. 3456ae09575SChristoph Hellwig */ 3466ae09575SChristoph Hellwig if (!special) { 3476ae09575SChristoph Hellwig if (cmds == Q_SYNC) 3486ae09575SChristoph Hellwig return quota_sync_all(type); 3496ae09575SChristoph Hellwig return -ENODEV; 3506ae09575SChristoph Hellwig } 3516ae09575SChristoph Hellwig 352*f00c9e44SJan Kara /* 353*f00c9e44SJan Kara * Path for quotaon has to be resolved before grabbing superblock 354*f00c9e44SJan Kara * because that gets s_umount sem which is also possibly needed by path 355*f00c9e44SJan Kara * resolution (think about autofs) and thus deadlocks could arise. 356*f00c9e44SJan Kara */ 357*f00c9e44SJan Kara if (cmds == Q_QUOTAON) { 358*f00c9e44SJan Kara ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW, &path); 359*f00c9e44SJan Kara if (ret) 360*f00c9e44SJan Kara pathp = ERR_PTR(ret); 361*f00c9e44SJan Kara else 362*f00c9e44SJan Kara pathp = &path; 363*f00c9e44SJan Kara } 364*f00c9e44SJan Kara 365884d179dSJan Kara sb = quotactl_block(special); 366884d179dSJan Kara if (IS_ERR(sb)) 367884d179dSJan Kara return PTR_ERR(sb); 368884d179dSJan Kara 369*f00c9e44SJan Kara ret = do_quotactl(sb, type, cmds, id, addr, pathp); 370884d179dSJan Kara 3716ae09575SChristoph Hellwig drop_super(sb); 372*f00c9e44SJan Kara if (pathp && !IS_ERR(pathp)) 373*f00c9e44SJan Kara path_put(pathp); 374884d179dSJan Kara return ret; 375884d179dSJan Kara } 376