1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2884d179dSJan Kara /* 3884d179dSJan Kara * Quota code necessary even when VFS quota support is not compiled 4884d179dSJan Kara * into the kernel. The interesting stuff is over in dquot.c, here 5884d179dSJan Kara * we have symbols for initial quotactl(2) handling, the sysctl(2) 6884d179dSJan Kara * variables, etc - things needed even when quota support disabled. 7884d179dSJan Kara */ 8884d179dSJan Kara 9884d179dSJan Kara #include <linux/fs.h> 10884d179dSJan Kara #include <linux/namei.h> 11884d179dSJan Kara #include <linux/slab.h> 12884d179dSJan Kara #include <asm/current.h> 13f3da9310SJeff Liu #include <linux/uaccess.h> 14884d179dSJan Kara #include <linux/kernel.h> 15884d179dSJan Kara #include <linux/security.h> 16884d179dSJan Kara #include <linux/syscalls.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> 217b6924d9SJeremy Cline #include <linux/nospec.h> 22884d179dSJan Kara 23c988afb5SChristoph Hellwig static int check_quotactl_permission(struct super_block *sb, int type, int cmd, 24268157baSJan Kara qid_t id) 25884d179dSJan Kara { 26c988afb5SChristoph Hellwig switch (cmd) { 27c988afb5SChristoph Hellwig /* these commands do not require any special privilegues */ 28c988afb5SChristoph Hellwig case Q_GETFMT: 29c988afb5SChristoph Hellwig case Q_SYNC: 30c988afb5SChristoph Hellwig case Q_GETINFO: 31c988afb5SChristoph Hellwig case Q_XGETQSTAT: 32af30cb44SChandra Seetharaman case Q_XGETQSTATV: 33c988afb5SChristoph Hellwig case Q_XQUOTASYNC: 34c988afb5SChristoph Hellwig break; 35c988afb5SChristoph Hellwig /* allow to query information for dquots we "own" */ 36c988afb5SChristoph Hellwig case Q_GETQUOTA: 37c988afb5SChristoph Hellwig case Q_XGETQUOTA: 3874a8a103SEric W. Biederman if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) || 3974a8a103SEric W. Biederman (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id)))) 40c988afb5SChristoph Hellwig break; 41c988afb5SChristoph Hellwig /*FALLTHROUGH*/ 42c988afb5SChristoph Hellwig default: 43884d179dSJan Kara if (!capable(CAP_SYS_ADMIN)) 44884d179dSJan Kara return -EPERM; 45884d179dSJan Kara } 46884d179dSJan Kara 47c988afb5SChristoph Hellwig return security_quotactl(cmd, type, id, sb); 48884d179dSJan Kara } 49884d179dSJan Kara 5001a05b33SAl Viro static void quota_sync_one(struct super_block *sb, void *arg) 5101a05b33SAl Viro { 522c5f648aSJan Kara int type = *(int *)arg; 532c5f648aSJan Kara 542c5f648aSJan Kara if (sb->s_qcop && sb->s_qcop->quota_sync && 552c5f648aSJan Kara (sb->s_quota_types & (1 << type))) 562c5f648aSJan Kara sb->s_qcop->quota_sync(sb, type); 5701a05b33SAl Viro } 5801a05b33SAl Viro 596ae09575SChristoph Hellwig static int quota_sync_all(int type) 60884d179dSJan Kara { 616ae09575SChristoph Hellwig int ret; 626ae09575SChristoph Hellwig 636ae09575SChristoph Hellwig if (type >= MAXQUOTAS) 646ae09575SChristoph Hellwig return -EINVAL; 656ae09575SChristoph Hellwig ret = security_quotactl(Q_SYNC, type, 0, NULL); 6601a05b33SAl Viro if (!ret) 6701a05b33SAl Viro iterate_supers(quota_sync_one, &type); 686ae09575SChristoph Hellwig return ret; 69884d179dSJan Kara } 70884d179dSJan Kara 71d3b86324SJan Kara unsigned int qtype_enforce_flag(int type) 72d3b86324SJan Kara { 73d3b86324SJan Kara switch (type) { 74d3b86324SJan Kara case USRQUOTA: 75d3b86324SJan Kara return FS_QUOTA_UDQ_ENFD; 76d3b86324SJan Kara case GRPQUOTA: 77d3b86324SJan Kara return FS_QUOTA_GDQ_ENFD; 78d3b86324SJan Kara case PRJQUOTA: 79d3b86324SJan Kara return FS_QUOTA_PDQ_ENFD; 80d3b86324SJan Kara } 81d3b86324SJan Kara return 0; 82d3b86324SJan Kara } 83d3b86324SJan Kara 843218a3ecSEric Sandeen static int quota_quotaon(struct super_block *sb, int type, qid_t id, 858c54ca9cSAl Viro const struct path *path) 86884d179dSJan Kara { 87aaa3daedSJan Kara if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable) 88f00c9e44SJan Kara return -ENOSYS; 89d3b86324SJan Kara if (sb->s_qcop->quota_enable) 90d3b86324SJan Kara return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type)); 91f00c9e44SJan Kara if (IS_ERR(path)) 92f00c9e44SJan Kara return PTR_ERR(path); 93f00c9e44SJan Kara return sb->s_qcop->quota_on(sb, type, id, path); 94884d179dSJan Kara } 95884d179dSJan Kara 96d3b86324SJan Kara static int quota_quotaoff(struct super_block *sb, int type) 97d3b86324SJan Kara { 98d3b86324SJan Kara if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable) 99d3b86324SJan Kara return -ENOSYS; 100d3b86324SJan Kara if (sb->s_qcop->quota_disable) 101d3b86324SJan Kara return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type)); 102d3b86324SJan Kara return sb->s_qcop->quota_off(sb, type); 103d3b86324SJan Kara } 104d3b86324SJan Kara 105c411e5f6SChristoph Hellwig static int quota_getfmt(struct super_block *sb, int type, void __user *addr) 106c411e5f6SChristoph Hellwig { 107884d179dSJan Kara __u32 fmt; 108884d179dSJan Kara 1099d1ccbe7SJan Kara if (!sb_has_quota_active(sb, type)) 110884d179dSJan Kara return -ESRCH; 111884d179dSJan Kara fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; 112884d179dSJan Kara if (copy_to_user(addr, &fmt, sizeof(fmt))) 113884d179dSJan Kara return -EFAULT; 114884d179dSJan Kara return 0; 115884d179dSJan Kara } 116c411e5f6SChristoph Hellwig 117c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr) 118c411e5f6SChristoph Hellwig { 1190a240339SJan Kara struct qc_state state; 1200a240339SJan Kara struct qc_type_state *tstate; 1210a240339SJan Kara struct if_dqinfo uinfo; 122c411e5f6SChristoph Hellwig int ret; 123884d179dSJan Kara 1240a240339SJan Kara if (!sb->s_qcop->get_state) 125f450d4feSChristoph Hellwig return -ENOSYS; 1260a240339SJan Kara ret = sb->s_qcop->get_state(sb, &state); 1270a240339SJan Kara if (ret) 1280a240339SJan Kara return ret; 1290a240339SJan Kara tstate = state.s_state + type; 1300a240339SJan Kara if (!(tstate->flags & QCI_ACCT_ENABLED)) 1310a240339SJan Kara return -ESRCH; 1320a240339SJan Kara memset(&uinfo, 0, sizeof(uinfo)); 1330a240339SJan Kara uinfo.dqi_bgrace = tstate->spc_timelimit; 1340a240339SJan Kara uinfo.dqi_igrace = tstate->ino_timelimit; 1350a240339SJan Kara if (tstate->flags & QCI_SYSFILE) 1360a240339SJan Kara uinfo.dqi_flags |= DQF_SYS_FILE; 1370a240339SJan Kara if (tstate->flags & QCI_ROOT_SQUASH) 1380a240339SJan Kara uinfo.dqi_flags |= DQF_ROOT_SQUASH; 1390a240339SJan Kara uinfo.dqi_valid = IIF_ALL; 14072d4d0e4SDan Carpenter if (copy_to_user(addr, &uinfo, sizeof(uinfo))) 141884d179dSJan Kara return -EFAULT; 14272d4d0e4SDan Carpenter return 0; 143884d179dSJan Kara } 144c411e5f6SChristoph Hellwig 145c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr) 146c411e5f6SChristoph Hellwig { 147884d179dSJan Kara struct if_dqinfo info; 1485eacb2acSJan Kara struct qc_info qinfo; 149884d179dSJan Kara 150884d179dSJan Kara if (copy_from_user(&info, addr, sizeof(info))) 151884d179dSJan Kara return -EFAULT; 152f450d4feSChristoph Hellwig if (!sb->s_qcop->set_info) 153f450d4feSChristoph Hellwig return -ENOSYS; 1545eacb2acSJan Kara if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE)) 1555eacb2acSJan Kara return -EINVAL; 1565eacb2acSJan Kara memset(&qinfo, 0, sizeof(qinfo)); 1575eacb2acSJan Kara if (info.dqi_valid & IIF_FLAGS) { 1585eacb2acSJan Kara if (info.dqi_flags & ~DQF_SETINFO_MASK) 1595eacb2acSJan Kara return -EINVAL; 1605eacb2acSJan Kara if (info.dqi_flags & DQF_ROOT_SQUASH) 1615eacb2acSJan Kara qinfo.i_flags |= QCI_ROOT_SQUASH; 1625eacb2acSJan Kara qinfo.i_fieldmask |= QC_FLAGS; 1635eacb2acSJan Kara } 1645eacb2acSJan Kara if (info.dqi_valid & IIF_BGRACE) { 1655eacb2acSJan Kara qinfo.i_spc_timelimit = info.dqi_bgrace; 1665eacb2acSJan Kara qinfo.i_fieldmask |= QC_SPC_TIMER; 1675eacb2acSJan Kara } 1685eacb2acSJan Kara if (info.dqi_valid & IIF_IGRACE) { 1695eacb2acSJan Kara qinfo.i_ino_timelimit = info.dqi_igrace; 1705eacb2acSJan Kara qinfo.i_fieldmask |= QC_INO_TIMER; 1715eacb2acSJan Kara } 1725eacb2acSJan Kara return sb->s_qcop->set_info(sb, type, &qinfo); 173884d179dSJan Kara } 174c411e5f6SChristoph Hellwig 17514bf61ffSJan Kara static inline qsize_t qbtos(qsize_t blocks) 17614bf61ffSJan Kara { 17714bf61ffSJan Kara return blocks << QIF_DQBLKSIZE_BITS; 17814bf61ffSJan Kara } 17914bf61ffSJan Kara 18014bf61ffSJan Kara static inline qsize_t stoqb(qsize_t space) 18114bf61ffSJan Kara { 18214bf61ffSJan Kara return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS; 18314bf61ffSJan Kara } 18414bf61ffSJan Kara 18514bf61ffSJan Kara static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src) 186b9b2dd36SChristoph Hellwig { 18718da65e7SDan Carpenter memset(dst, 0, sizeof(*dst)); 18814bf61ffSJan Kara dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit); 18914bf61ffSJan Kara dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit); 19014bf61ffSJan Kara dst->dqb_curspace = src->d_space; 191b9b2dd36SChristoph Hellwig dst->dqb_ihardlimit = src->d_ino_hardlimit; 192b9b2dd36SChristoph Hellwig dst->dqb_isoftlimit = src->d_ino_softlimit; 19314bf61ffSJan Kara dst->dqb_curinodes = src->d_ino_count; 19414bf61ffSJan Kara dst->dqb_btime = src->d_spc_timer; 19514bf61ffSJan Kara dst->dqb_itime = src->d_ino_timer; 196b9b2dd36SChristoph Hellwig dst->dqb_valid = QIF_ALL; 197b9b2dd36SChristoph Hellwig } 198b9b2dd36SChristoph Hellwig 199c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id, 200c411e5f6SChristoph Hellwig void __user *addr) 201c411e5f6SChristoph Hellwig { 20274a8a103SEric W. Biederman struct kqid qid; 20314bf61ffSJan Kara struct qc_dqblk fdq; 204884d179dSJan Kara struct if_dqblk idq; 205c411e5f6SChristoph Hellwig int ret; 206884d179dSJan Kara 207f450d4feSChristoph Hellwig if (!sb->s_qcop->get_dqblk) 208f450d4feSChristoph Hellwig return -ENOSYS; 20974a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 210d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 21174a8a103SEric W. Biederman return -EINVAL; 21274a8a103SEric W. Biederman ret = sb->s_qcop->get_dqblk(sb, qid, &fdq); 213268157baSJan Kara if (ret) 214884d179dSJan Kara return ret; 215b9b2dd36SChristoph Hellwig copy_to_if_dqblk(&idq, &fdq); 216884d179dSJan Kara if (copy_to_user(addr, &idq, sizeof(idq))) 217884d179dSJan Kara return -EFAULT; 218884d179dSJan Kara return 0; 219884d179dSJan Kara } 220c411e5f6SChristoph Hellwig 221926132c0SEric Sandeen /* 222926132c0SEric Sandeen * Return quota for next active quota >= this id, if any exists, 223ba58148bSEric Sandeen * otherwise return -ENOENT via ->get_nextdqblk 224926132c0SEric Sandeen */ 225926132c0SEric Sandeen static int quota_getnextquota(struct super_block *sb, int type, qid_t id, 226926132c0SEric Sandeen void __user *addr) 227926132c0SEric Sandeen { 228926132c0SEric Sandeen struct kqid qid; 229926132c0SEric Sandeen struct qc_dqblk fdq; 230926132c0SEric Sandeen struct if_nextdqblk idq; 231926132c0SEric Sandeen int ret; 232926132c0SEric Sandeen 233926132c0SEric Sandeen if (!sb->s_qcop->get_nextdqblk) 234926132c0SEric Sandeen return -ENOSYS; 235926132c0SEric Sandeen qid = make_kqid(current_user_ns(), type, id); 236d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 237926132c0SEric Sandeen return -EINVAL; 238926132c0SEric Sandeen ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq); 239926132c0SEric Sandeen if (ret) 240926132c0SEric Sandeen return ret; 241926132c0SEric Sandeen /* struct if_nextdqblk is a superset of struct if_dqblk */ 242926132c0SEric Sandeen copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq); 243926132c0SEric Sandeen idq.dqb_id = from_kqid(current_user_ns(), qid); 244926132c0SEric Sandeen if (copy_to_user(addr, &idq, sizeof(idq))) 245926132c0SEric Sandeen return -EFAULT; 246926132c0SEric Sandeen return 0; 247926132c0SEric Sandeen } 248926132c0SEric Sandeen 24914bf61ffSJan Kara static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src) 250c472b432SChristoph Hellwig { 25114bf61ffSJan Kara dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit); 25214bf61ffSJan Kara dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit); 25314bf61ffSJan Kara dst->d_space = src->dqb_curspace; 254c472b432SChristoph Hellwig dst->d_ino_hardlimit = src->dqb_ihardlimit; 255c472b432SChristoph Hellwig dst->d_ino_softlimit = src->dqb_isoftlimit; 25614bf61ffSJan Kara dst->d_ino_count = src->dqb_curinodes; 25714bf61ffSJan Kara dst->d_spc_timer = src->dqb_btime; 25814bf61ffSJan Kara dst->d_ino_timer = src->dqb_itime; 259c472b432SChristoph Hellwig 260c472b432SChristoph Hellwig dst->d_fieldmask = 0; 261c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BLIMITS) 26214bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD; 263c472b432SChristoph Hellwig if (src->dqb_valid & QIF_SPACE) 26414bf61ffSJan Kara dst->d_fieldmask |= QC_SPACE; 265c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ILIMITS) 26614bf61ffSJan Kara dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD; 267c472b432SChristoph Hellwig if (src->dqb_valid & QIF_INODES) 26814bf61ffSJan Kara dst->d_fieldmask |= QC_INO_COUNT; 269c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BTIME) 27014bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_TIMER; 271c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ITIME) 27214bf61ffSJan Kara dst->d_fieldmask |= QC_INO_TIMER; 273c472b432SChristoph Hellwig } 274c472b432SChristoph Hellwig 275c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id, 276c411e5f6SChristoph Hellwig void __user *addr) 277c411e5f6SChristoph Hellwig { 27814bf61ffSJan Kara struct qc_dqblk fdq; 279884d179dSJan Kara struct if_dqblk idq; 28074a8a103SEric W. Biederman struct kqid qid; 281884d179dSJan Kara 282884d179dSJan Kara if (copy_from_user(&idq, addr, sizeof(idq))) 283884d179dSJan Kara return -EFAULT; 284f450d4feSChristoph Hellwig if (!sb->s_qcop->set_dqblk) 285f450d4feSChristoph Hellwig return -ENOSYS; 28674a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 287d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 28874a8a103SEric W. Biederman return -EINVAL; 289c472b432SChristoph Hellwig copy_from_if_dqblk(&fdq, &idq); 29074a8a103SEric W. Biederman return sb->s_qcop->set_dqblk(sb, qid, &fdq); 291884d179dSJan Kara } 292884d179dSJan Kara 29338e478c4SJan Kara static int quota_enable(struct super_block *sb, void __user *addr) 294c411e5f6SChristoph Hellwig { 295884d179dSJan Kara __u32 flags; 296884d179dSJan Kara 297884d179dSJan Kara if (copy_from_user(&flags, addr, sizeof(flags))) 298884d179dSJan Kara return -EFAULT; 29938e478c4SJan Kara if (!sb->s_qcop->quota_enable) 300f450d4feSChristoph Hellwig return -ENOSYS; 30138e478c4SJan Kara return sb->s_qcop->quota_enable(sb, flags); 30238e478c4SJan Kara } 30338e478c4SJan Kara 30438e478c4SJan Kara static int quota_disable(struct super_block *sb, void __user *addr) 30538e478c4SJan Kara { 30638e478c4SJan Kara __u32 flags; 30738e478c4SJan Kara 30838e478c4SJan Kara if (copy_from_user(&flags, addr, sizeof(flags))) 30938e478c4SJan Kara return -EFAULT; 31038e478c4SJan Kara if (!sb->s_qcop->quota_disable) 31138e478c4SJan Kara return -ENOSYS; 31238e478c4SJan Kara return sb->s_qcop->quota_disable(sb, flags); 313884d179dSJan Kara } 314884d179dSJan Kara 315bc230e4aSJan Kara static int quota_state_to_flags(struct qc_state *state) 316bc230e4aSJan Kara { 317bc230e4aSJan Kara int flags = 0; 318bc230e4aSJan Kara 319bc230e4aSJan Kara if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) 320bc230e4aSJan Kara flags |= FS_QUOTA_UDQ_ACCT; 321bc230e4aSJan Kara if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED) 322bc230e4aSJan Kara flags |= FS_QUOTA_UDQ_ENFD; 323bc230e4aSJan Kara if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) 324bc230e4aSJan Kara flags |= FS_QUOTA_GDQ_ACCT; 325bc230e4aSJan Kara if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED) 326bc230e4aSJan Kara flags |= FS_QUOTA_GDQ_ENFD; 327bc230e4aSJan Kara if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) 328bc230e4aSJan Kara flags |= FS_QUOTA_PDQ_ACCT; 329bc230e4aSJan Kara if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED) 330bc230e4aSJan Kara flags |= FS_QUOTA_PDQ_ENFD; 331bc230e4aSJan Kara return flags; 332bc230e4aSJan Kara } 333bc230e4aSJan Kara 334bc230e4aSJan Kara static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs) 335bc230e4aSJan Kara { 336bc230e4aSJan Kara int type; 337bc230e4aSJan Kara struct qc_state state; 338bc230e4aSJan Kara int ret; 339bc230e4aSJan Kara 3403cd0126dSEric Sandeen memset(&state, 0, sizeof (struct qc_state)); 341bc230e4aSJan Kara ret = sb->s_qcop->get_state(sb, &state); 342bc230e4aSJan Kara if (ret < 0) 343bc230e4aSJan Kara return ret; 344bc230e4aSJan Kara 345bc230e4aSJan Kara memset(fqs, 0, sizeof(*fqs)); 346bc230e4aSJan Kara fqs->qs_version = FS_QSTAT_VERSION; 347bc230e4aSJan Kara fqs->qs_flags = quota_state_to_flags(&state); 348bc230e4aSJan Kara /* No quota enabled? */ 349bc230e4aSJan Kara if (!fqs->qs_flags) 350bc230e4aSJan Kara return -ENOSYS; 351bc230e4aSJan Kara fqs->qs_incoredqs = state.s_incoredqs; 352bc230e4aSJan Kara /* 353bc230e4aSJan Kara * GETXSTATE quotactl has space for just one set of time limits so 354bc230e4aSJan Kara * report them for the first enabled quota type 355bc230e4aSJan Kara */ 35664d9d138SJeremy Cline for (type = 0; type < MAXQUOTAS; type++) 357bc230e4aSJan Kara if (state.s_state[type].flags & QCI_ACCT_ENABLED) 358bc230e4aSJan Kara break; 35964d9d138SJeremy Cline BUG_ON(type == MAXQUOTAS); 360bc230e4aSJan Kara fqs->qs_btimelimit = state.s_state[type].spc_timelimit; 361bc230e4aSJan Kara fqs->qs_itimelimit = state.s_state[type].ino_timelimit; 362bc230e4aSJan Kara fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; 363bc230e4aSJan Kara fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; 364bc230e4aSJan Kara fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; 3653cd0126dSEric Sandeen 3663cd0126dSEric Sandeen /* Inodes may be allocated even if inactive; copy out if present */ 3673cd0126dSEric Sandeen if (state.s_state[USRQUOTA].ino) { 368bc230e4aSJan Kara fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; 369bc230e4aSJan Kara fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; 370bc230e4aSJan Kara fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; 371bc230e4aSJan Kara } 3723cd0126dSEric Sandeen if (state.s_state[GRPQUOTA].ino) { 373bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; 374bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; 375bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; 376bc230e4aSJan Kara } 3773cd0126dSEric Sandeen if (state.s_state[PRJQUOTA].ino) { 378bc230e4aSJan Kara /* 379bc230e4aSJan Kara * Q_XGETQSTAT doesn't have room for both group and project 380bc230e4aSJan Kara * quotas. So, allow the project quota values to be copied out 381bc230e4aSJan Kara * only if there is no group quota information available. 382bc230e4aSJan Kara */ 383bc230e4aSJan Kara if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) { 384bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino; 385bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = 386bc230e4aSJan Kara state.s_state[PRJQUOTA].blocks; 387bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = 388bc230e4aSJan Kara state.s_state[PRJQUOTA].nextents; 389bc230e4aSJan Kara } 390bc230e4aSJan Kara } 391bc230e4aSJan Kara return 0; 392bc230e4aSJan Kara } 393bc230e4aSJan Kara 394c411e5f6SChristoph Hellwig static int quota_getxstate(struct super_block *sb, void __user *addr) 395c411e5f6SChristoph Hellwig { 396c411e5f6SChristoph Hellwig struct fs_quota_stat fqs; 397c411e5f6SChristoph Hellwig int ret; 398c411e5f6SChristoph Hellwig 39959b6ba69SJan Kara if (!sb->s_qcop->get_state) 400f450d4feSChristoph Hellwig return -ENOSYS; 401bc230e4aSJan Kara ret = quota_getstate(sb, &fqs); 402c411e5f6SChristoph Hellwig if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 403884d179dSJan Kara return -EFAULT; 404c411e5f6SChristoph Hellwig return ret; 405884d179dSJan Kara } 406c411e5f6SChristoph Hellwig 407bc230e4aSJan Kara static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs) 408bc230e4aSJan Kara { 409bc230e4aSJan Kara int type; 410bc230e4aSJan Kara struct qc_state state; 411bc230e4aSJan Kara int ret; 412bc230e4aSJan Kara 4133cd0126dSEric Sandeen memset(&state, 0, sizeof (struct qc_state)); 414bc230e4aSJan Kara ret = sb->s_qcop->get_state(sb, &state); 415bc230e4aSJan Kara if (ret < 0) 416bc230e4aSJan Kara return ret; 417bc230e4aSJan Kara 418bc230e4aSJan Kara memset(fqs, 0, sizeof(*fqs)); 419bc230e4aSJan Kara fqs->qs_version = FS_QSTAT_VERSION; 420bc230e4aSJan Kara fqs->qs_flags = quota_state_to_flags(&state); 421bc230e4aSJan Kara /* No quota enabled? */ 422bc230e4aSJan Kara if (!fqs->qs_flags) 423bc230e4aSJan Kara return -ENOSYS; 424bc230e4aSJan Kara fqs->qs_incoredqs = state.s_incoredqs; 425bc230e4aSJan Kara /* 426bc230e4aSJan Kara * GETXSTATV quotactl has space for just one set of time limits so 427bc230e4aSJan Kara * report them for the first enabled quota type 428bc230e4aSJan Kara */ 42964d9d138SJeremy Cline for (type = 0; type < MAXQUOTAS; type++) 430bc230e4aSJan Kara if (state.s_state[type].flags & QCI_ACCT_ENABLED) 431bc230e4aSJan Kara break; 43264d9d138SJeremy Cline BUG_ON(type == MAXQUOTAS); 433bc230e4aSJan Kara fqs->qs_btimelimit = state.s_state[type].spc_timelimit; 434bc230e4aSJan Kara fqs->qs_itimelimit = state.s_state[type].ino_timelimit; 435bc230e4aSJan Kara fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; 436bc230e4aSJan Kara fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; 437bc230e4aSJan Kara fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; 4383cd0126dSEric Sandeen 4393cd0126dSEric Sandeen /* Inodes may be allocated even if inactive; copy out if present */ 4403cd0126dSEric Sandeen if (state.s_state[USRQUOTA].ino) { 441bc230e4aSJan Kara fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; 442bc230e4aSJan Kara fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; 443bc230e4aSJan Kara fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; 444bc230e4aSJan Kara } 4453cd0126dSEric Sandeen if (state.s_state[GRPQUOTA].ino) { 446bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; 447bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; 448bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; 449bc230e4aSJan Kara } 4503cd0126dSEric Sandeen if (state.s_state[PRJQUOTA].ino) { 451bc230e4aSJan Kara fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino; 452bc230e4aSJan Kara fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks; 453bc230e4aSJan Kara fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents; 454bc230e4aSJan Kara } 455bc230e4aSJan Kara return 0; 456bc230e4aSJan Kara } 457bc230e4aSJan Kara 458af30cb44SChandra Seetharaman static int quota_getxstatev(struct super_block *sb, void __user *addr) 459af30cb44SChandra Seetharaman { 460af30cb44SChandra Seetharaman struct fs_quota_statv fqs; 461af30cb44SChandra Seetharaman int ret; 462af30cb44SChandra Seetharaman 46359b6ba69SJan Kara if (!sb->s_qcop->get_state) 464af30cb44SChandra Seetharaman return -ENOSYS; 465af30cb44SChandra Seetharaman 466af30cb44SChandra Seetharaman memset(&fqs, 0, sizeof(fqs)); 467af30cb44SChandra Seetharaman if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */ 468af30cb44SChandra Seetharaman return -EFAULT; 469af30cb44SChandra Seetharaman 470af30cb44SChandra Seetharaman /* If this kernel doesn't support user specified version, fail */ 471af30cb44SChandra Seetharaman switch (fqs.qs_version) { 472af30cb44SChandra Seetharaman case FS_QSTATV_VERSION1: 473af30cb44SChandra Seetharaman break; 474af30cb44SChandra Seetharaman default: 475af30cb44SChandra Seetharaman return -EINVAL; 476af30cb44SChandra Seetharaman } 477bc230e4aSJan Kara ret = quota_getstatev(sb, &fqs); 478af30cb44SChandra Seetharaman if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 479af30cb44SChandra Seetharaman return -EFAULT; 480af30cb44SChandra Seetharaman return ret; 481af30cb44SChandra Seetharaman } 482af30cb44SChandra Seetharaman 48314bf61ffSJan Kara /* 48414bf61ffSJan Kara * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them 48514bf61ffSJan Kara * out of there as xfsprogs rely on definitions being in that header file. So 48614bf61ffSJan Kara * just define same functions here for quota purposes. 48714bf61ffSJan Kara */ 48814bf61ffSJan Kara #define XFS_BB_SHIFT 9 48914bf61ffSJan Kara 49014bf61ffSJan Kara static inline u64 quota_bbtob(u64 blocks) 49114bf61ffSJan Kara { 49214bf61ffSJan Kara return blocks << XFS_BB_SHIFT; 49314bf61ffSJan Kara } 49414bf61ffSJan Kara 49514bf61ffSJan Kara static inline u64 quota_btobb(u64 bytes) 49614bf61ffSJan Kara { 49714bf61ffSJan Kara return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT; 49814bf61ffSJan Kara } 49914bf61ffSJan Kara 50014bf61ffSJan Kara static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src) 50114bf61ffSJan Kara { 50214bf61ffSJan Kara dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit); 50314bf61ffSJan Kara dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit); 50414bf61ffSJan Kara dst->d_ino_hardlimit = src->d_ino_hardlimit; 50514bf61ffSJan Kara dst->d_ino_softlimit = src->d_ino_softlimit; 50614bf61ffSJan Kara dst->d_space = quota_bbtob(src->d_bcount); 50714bf61ffSJan Kara dst->d_ino_count = src->d_icount; 50814bf61ffSJan Kara dst->d_ino_timer = src->d_itimer; 50914bf61ffSJan Kara dst->d_spc_timer = src->d_btimer; 51014bf61ffSJan Kara dst->d_ino_warns = src->d_iwarns; 51114bf61ffSJan Kara dst->d_spc_warns = src->d_bwarns; 51214bf61ffSJan Kara dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit); 51314bf61ffSJan Kara dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit); 51414bf61ffSJan Kara dst->d_rt_space = quota_bbtob(src->d_rtbcount); 51514bf61ffSJan Kara dst->d_rt_spc_timer = src->d_rtbtimer; 51614bf61ffSJan Kara dst->d_rt_spc_warns = src->d_rtbwarns; 51714bf61ffSJan Kara dst->d_fieldmask = 0; 51814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ISOFT) 51914bf61ffSJan Kara dst->d_fieldmask |= QC_INO_SOFT; 52014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_IHARD) 52114bf61ffSJan Kara dst->d_fieldmask |= QC_INO_HARD; 52214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BSOFT) 52314bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_SOFT; 52414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BHARD) 52514bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_HARD; 52614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBSOFT) 52714bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_SOFT; 52814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBHARD) 52914bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_HARD; 53014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BTIMER) 53114bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_TIMER; 53214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ITIMER) 53314bf61ffSJan Kara dst->d_fieldmask |= QC_INO_TIMER; 53414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBTIMER) 53514bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_TIMER; 53614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BWARNS) 53714bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_WARNS; 53814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_IWARNS) 53914bf61ffSJan Kara dst->d_fieldmask |= QC_INO_WARNS; 54014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBWARNS) 54114bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_WARNS; 54214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BCOUNT) 54314bf61ffSJan Kara dst->d_fieldmask |= QC_SPACE; 54414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ICOUNT) 54514bf61ffSJan Kara dst->d_fieldmask |= QC_INO_COUNT; 54614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBCOUNT) 54714bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPACE; 54814bf61ffSJan Kara } 54914bf61ffSJan Kara 550c39fb53bSJan Kara static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst, 551c39fb53bSJan Kara struct fs_disk_quota *src) 552c39fb53bSJan Kara { 553c39fb53bSJan Kara memset(dst, 0, sizeof(*dst)); 554c39fb53bSJan Kara dst->i_spc_timelimit = src->d_btimer; 555c39fb53bSJan Kara dst->i_ino_timelimit = src->d_itimer; 556c39fb53bSJan Kara dst->i_rt_spc_timelimit = src->d_rtbtimer; 557c39fb53bSJan Kara dst->i_ino_warnlimit = src->d_iwarns; 558c39fb53bSJan Kara dst->i_spc_warnlimit = src->d_bwarns; 559c39fb53bSJan Kara dst->i_rt_spc_warnlimit = src->d_rtbwarns; 560c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_BWARNS) 561c39fb53bSJan Kara dst->i_fieldmask |= QC_SPC_WARNS; 562c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_IWARNS) 563c39fb53bSJan Kara dst->i_fieldmask |= QC_INO_WARNS; 564c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_RTBWARNS) 565c39fb53bSJan Kara dst->i_fieldmask |= QC_RT_SPC_WARNS; 566c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_BTIMER) 567c39fb53bSJan Kara dst->i_fieldmask |= QC_SPC_TIMER; 568c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_ITIMER) 569c39fb53bSJan Kara dst->i_fieldmask |= QC_INO_TIMER; 570c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_RTBTIMER) 571c39fb53bSJan Kara dst->i_fieldmask |= QC_RT_SPC_TIMER; 572c39fb53bSJan Kara } 573c39fb53bSJan Kara 574c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id, 575c411e5f6SChristoph Hellwig void __user *addr) 576c411e5f6SChristoph Hellwig { 577884d179dSJan Kara struct fs_disk_quota fdq; 57814bf61ffSJan Kara struct qc_dqblk qdq; 57974a8a103SEric W. Biederman struct kqid qid; 580884d179dSJan Kara 581884d179dSJan Kara if (copy_from_user(&fdq, addr, sizeof(fdq))) 582884d179dSJan Kara return -EFAULT; 583c472b432SChristoph Hellwig if (!sb->s_qcop->set_dqblk) 584f450d4feSChristoph Hellwig return -ENOSYS; 58574a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 586d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 58774a8a103SEric W. Biederman return -EINVAL; 588c39fb53bSJan Kara /* Are we actually setting timer / warning limits for all users? */ 589cfd4c70aSEric W. Biederman if (from_kqid(sb->s_user_ns, qid) == 0 && 590c39fb53bSJan Kara fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) { 591c39fb53bSJan Kara struct qc_info qinfo; 592c39fb53bSJan Kara int ret; 593c39fb53bSJan Kara 594c39fb53bSJan Kara if (!sb->s_qcop->set_info) 595c39fb53bSJan Kara return -EINVAL; 596c39fb53bSJan Kara copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq); 597c39fb53bSJan Kara ret = sb->s_qcop->set_info(sb, type, &qinfo); 598c39fb53bSJan Kara if (ret) 599c39fb53bSJan Kara return ret; 600c39fb53bSJan Kara /* These are already done */ 601c39fb53bSJan Kara fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK); 602c39fb53bSJan Kara } 60314bf61ffSJan Kara copy_from_xfs_dqblk(&qdq, &fdq); 60414bf61ffSJan Kara return sb->s_qcop->set_dqblk(sb, qid, &qdq); 60514bf61ffSJan Kara } 60614bf61ffSJan Kara 60714bf61ffSJan Kara static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src, 60814bf61ffSJan Kara int type, qid_t id) 60914bf61ffSJan Kara { 61014bf61ffSJan Kara memset(dst, 0, sizeof(*dst)); 61114bf61ffSJan Kara dst->d_version = FS_DQUOT_VERSION; 61214bf61ffSJan Kara dst->d_id = id; 61314bf61ffSJan Kara if (type == USRQUOTA) 61414bf61ffSJan Kara dst->d_flags = FS_USER_QUOTA; 61514bf61ffSJan Kara else if (type == PRJQUOTA) 61614bf61ffSJan Kara dst->d_flags = FS_PROJ_QUOTA; 61714bf61ffSJan Kara else 61814bf61ffSJan Kara dst->d_flags = FS_GROUP_QUOTA; 61914bf61ffSJan Kara dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit); 62014bf61ffSJan Kara dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit); 62114bf61ffSJan Kara dst->d_ino_hardlimit = src->d_ino_hardlimit; 62214bf61ffSJan Kara dst->d_ino_softlimit = src->d_ino_softlimit; 62314bf61ffSJan Kara dst->d_bcount = quota_btobb(src->d_space); 62414bf61ffSJan Kara dst->d_icount = src->d_ino_count; 62514bf61ffSJan Kara dst->d_itimer = src->d_ino_timer; 62614bf61ffSJan Kara dst->d_btimer = src->d_spc_timer; 62714bf61ffSJan Kara dst->d_iwarns = src->d_ino_warns; 62814bf61ffSJan Kara dst->d_bwarns = src->d_spc_warns; 62914bf61ffSJan Kara dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit); 63014bf61ffSJan Kara dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit); 63114bf61ffSJan Kara dst->d_rtbcount = quota_btobb(src->d_rt_space); 63214bf61ffSJan Kara dst->d_rtbtimer = src->d_rt_spc_timer; 63314bf61ffSJan Kara dst->d_rtbwarns = src->d_rt_spc_warns; 634884d179dSJan Kara } 635c411e5f6SChristoph Hellwig 636c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id, 637c411e5f6SChristoph Hellwig void __user *addr) 638c411e5f6SChristoph Hellwig { 639884d179dSJan Kara struct fs_disk_quota fdq; 64014bf61ffSJan Kara struct qc_dqblk qdq; 64174a8a103SEric W. Biederman struct kqid qid; 642c411e5f6SChristoph Hellwig int ret; 643884d179dSJan Kara 644b9b2dd36SChristoph Hellwig if (!sb->s_qcop->get_dqblk) 645f450d4feSChristoph Hellwig return -ENOSYS; 64674a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 647d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 64874a8a103SEric W. Biederman return -EINVAL; 64914bf61ffSJan Kara ret = sb->s_qcop->get_dqblk(sb, qid, &qdq); 65014bf61ffSJan Kara if (ret) 65114bf61ffSJan Kara return ret; 65214bf61ffSJan Kara copy_to_xfs_dqblk(&fdq, &qdq, type, id); 65314bf61ffSJan Kara if (copy_to_user(addr, &fdq, sizeof(fdq))) 654884d179dSJan Kara return -EFAULT; 655c411e5f6SChristoph Hellwig return ret; 656884d179dSJan Kara } 657c411e5f6SChristoph Hellwig 6588b375249SEric Sandeen /* 6598b375249SEric Sandeen * Return quota for next active quota >= this id, if any exists, 660ba58148bSEric Sandeen * otherwise return -ENOENT via ->get_nextdqblk. 6618b375249SEric Sandeen */ 6628b375249SEric Sandeen static int quota_getnextxquota(struct super_block *sb, int type, qid_t id, 6638b375249SEric Sandeen void __user *addr) 6648b375249SEric Sandeen { 6658b375249SEric Sandeen struct fs_disk_quota fdq; 6668b375249SEric Sandeen struct qc_dqblk qdq; 6678b375249SEric Sandeen struct kqid qid; 6688b375249SEric Sandeen qid_t id_out; 6698b375249SEric Sandeen int ret; 6708b375249SEric Sandeen 6718b375249SEric Sandeen if (!sb->s_qcop->get_nextdqblk) 6728b375249SEric Sandeen return -ENOSYS; 6738b375249SEric Sandeen qid = make_kqid(current_user_ns(), type, id); 674d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 6758b375249SEric Sandeen return -EINVAL; 6768b375249SEric Sandeen ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq); 6778b375249SEric Sandeen if (ret) 6788b375249SEric Sandeen return ret; 6798b375249SEric Sandeen id_out = from_kqid(current_user_ns(), qid); 6808b375249SEric Sandeen copy_to_xfs_dqblk(&fdq, &qdq, type, id_out); 6818b375249SEric Sandeen if (copy_to_user(addr, &fdq, sizeof(fdq))) 6828b375249SEric Sandeen return -EFAULT; 6838b375249SEric Sandeen return ret; 6848b375249SEric Sandeen } 6858b375249SEric Sandeen 6869da93f9bSEric Sandeen static int quota_rmxquota(struct super_block *sb, void __user *addr) 6879da93f9bSEric Sandeen { 6889da93f9bSEric Sandeen __u32 flags; 6899da93f9bSEric Sandeen 6909da93f9bSEric Sandeen if (copy_from_user(&flags, addr, sizeof(flags))) 6919da93f9bSEric Sandeen return -EFAULT; 6929da93f9bSEric Sandeen if (!sb->s_qcop->rm_xquota) 6939da93f9bSEric Sandeen return -ENOSYS; 6949da93f9bSEric Sandeen return sb->s_qcop->rm_xquota(sb, flags); 6959da93f9bSEric Sandeen } 6969da93f9bSEric Sandeen 697c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */ 698c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, 6998c54ca9cSAl Viro void __user *addr, const struct path *path) 700c411e5f6SChristoph Hellwig { 701c988afb5SChristoph Hellwig int ret; 702c988afb5SChristoph Hellwig 70364d9d138SJeremy Cline if (type >= MAXQUOTAS) 704c988afb5SChristoph Hellwig return -EINVAL; 7057b6924d9SJeremy Cline type = array_index_nospec(type, MAXQUOTAS); 7062c5f648aSJan Kara /* 7072c5f648aSJan Kara * Quota not supported on this fs? Check this before s_quota_types 7082c5f648aSJan Kara * since they needn't be set if quota is not supported at all. 7092c5f648aSJan Kara */ 710c988afb5SChristoph Hellwig if (!sb->s_qcop) 711c988afb5SChristoph Hellwig return -ENOSYS; 7122c5f648aSJan Kara if (!(sb->s_quota_types & (1 << type))) 7132c5f648aSJan Kara return -EINVAL; 714c988afb5SChristoph Hellwig 715c988afb5SChristoph Hellwig ret = check_quotactl_permission(sb, type, cmd, id); 716c988afb5SChristoph Hellwig if (ret < 0) 717c988afb5SChristoph Hellwig return ret; 718c988afb5SChristoph Hellwig 719c411e5f6SChristoph Hellwig switch (cmd) { 720c411e5f6SChristoph Hellwig case Q_QUOTAON: 7213218a3ecSEric Sandeen return quota_quotaon(sb, type, id, path); 722c411e5f6SChristoph Hellwig case Q_QUOTAOFF: 723d3b86324SJan Kara return quota_quotaoff(sb, type); 724c411e5f6SChristoph Hellwig case Q_GETFMT: 725c411e5f6SChristoph Hellwig return quota_getfmt(sb, type, addr); 726c411e5f6SChristoph Hellwig case Q_GETINFO: 727c411e5f6SChristoph Hellwig return quota_getinfo(sb, type, addr); 728c411e5f6SChristoph Hellwig case Q_SETINFO: 729c411e5f6SChristoph Hellwig return quota_setinfo(sb, type, addr); 730c411e5f6SChristoph Hellwig case Q_GETQUOTA: 731c411e5f6SChristoph Hellwig return quota_getquota(sb, type, id, addr); 732926132c0SEric Sandeen case Q_GETNEXTQUOTA: 733926132c0SEric Sandeen return quota_getnextquota(sb, type, id, addr); 734c411e5f6SChristoph Hellwig case Q_SETQUOTA: 735c411e5f6SChristoph Hellwig return quota_setquota(sb, type, id, addr); 736c411e5f6SChristoph Hellwig case Q_SYNC: 737f450d4feSChristoph Hellwig if (!sb->s_qcop->quota_sync) 738f450d4feSChristoph Hellwig return -ENOSYS; 739ceed1723SJan Kara return sb->s_qcop->quota_sync(sb, type); 740c411e5f6SChristoph Hellwig case Q_XQUOTAON: 74138e478c4SJan Kara return quota_enable(sb, addr); 742c411e5f6SChristoph Hellwig case Q_XQUOTAOFF: 74338e478c4SJan Kara return quota_disable(sb, addr); 7449da93f9bSEric Sandeen case Q_XQUOTARM: 7459da93f9bSEric Sandeen return quota_rmxquota(sb, addr); 746c411e5f6SChristoph Hellwig case Q_XGETQSTAT: 747c411e5f6SChristoph Hellwig return quota_getxstate(sb, addr); 748af30cb44SChandra Seetharaman case Q_XGETQSTATV: 749af30cb44SChandra Seetharaman return quota_getxstatev(sb, addr); 750c411e5f6SChristoph Hellwig case Q_XSETQLIM: 751c411e5f6SChristoph Hellwig return quota_setxquota(sb, type, id, addr); 752c411e5f6SChristoph Hellwig case Q_XGETQUOTA: 753c411e5f6SChristoph Hellwig return quota_getxquota(sb, type, id, addr); 7548b375249SEric Sandeen case Q_XGETNEXTQUOTA: 7558b375249SEric Sandeen return quota_getnextxquota(sb, type, id, addr); 756884d179dSJan Kara case Q_XQUOTASYNC: 757bc98a42cSDavid Howells if (sb_rdonly(sb)) 7588c4e4acdSChristoph Hellwig return -EROFS; 7594b217ed9SChristoph Hellwig /* XFS quotas are fully coherent now, making this call a noop */ 7608c4e4acdSChristoph Hellwig return 0; 761884d179dSJan Kara default: 762f450d4feSChristoph Hellwig return -EINVAL; 763884d179dSJan Kara } 764884d179dSJan Kara } 765884d179dSJan Kara 76656df1278SLee Jones #ifdef CONFIG_BLOCK 76756df1278SLee Jones 768dcdbed85SJan Kara /* Return 1 if 'cmd' will block on frozen filesystem */ 769dcdbed85SJan Kara static int quotactl_cmd_write(int cmd) 770dcdbed85SJan Kara { 771ccf370e4SJan Kara /* 772ccf370e4SJan Kara * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access 773ccf370e4SJan Kara * as dquot_acquire() may allocate space for new structure and OCFS2 774ccf370e4SJan Kara * needs to increment on-disk use count. 775ccf370e4SJan Kara */ 776dcdbed85SJan Kara switch (cmd) { 777dcdbed85SJan Kara case Q_GETFMT: 778dcdbed85SJan Kara case Q_GETINFO: 779dcdbed85SJan Kara case Q_SYNC: 780dcdbed85SJan Kara case Q_XGETQSTAT: 781af30cb44SChandra Seetharaman case Q_XGETQSTATV: 782dcdbed85SJan Kara case Q_XGETQUOTA: 7838b375249SEric Sandeen case Q_XGETNEXTQUOTA: 784dcdbed85SJan Kara case Q_XQUOTASYNC: 785dcdbed85SJan Kara return 0; 786dcdbed85SJan Kara } 787dcdbed85SJan Kara return 1; 788dcdbed85SJan Kara } 78956df1278SLee Jones #endif /* CONFIG_BLOCK */ 79056df1278SLee Jones 7917d6cd73dSJan Kara /* Return true if quotactl command is manipulating quota on/off state */ 7927d6cd73dSJan Kara static bool quotactl_cmd_onoff(int cmd) 7937d6cd73dSJan Kara { 794*41c4f85cSJavier Barrio return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) || 795*41c4f85cSJavier Barrio (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF); 7967d6cd73dSJan Kara } 7977d6cd73dSJan Kara 798884d179dSJan Kara /* 799884d179dSJan Kara * look up a superblock on which quota ops will be performed 800884d179dSJan Kara * - use the name of a block device to find the superblock thereon 801884d179dSJan Kara */ 802dcdbed85SJan Kara static struct super_block *quotactl_block(const char __user *special, int cmd) 803884d179dSJan Kara { 804884d179dSJan Kara #ifdef CONFIG_BLOCK 805884d179dSJan Kara struct block_device *bdev; 806884d179dSJan Kara struct super_block *sb; 80791a27b2aSJeff Layton struct filename *tmp = getname(special); 808884d179dSJan Kara 809884d179dSJan Kara if (IS_ERR(tmp)) 810884d179dSJan Kara return ERR_CAST(tmp); 81191a27b2aSJeff Layton bdev = lookup_bdev(tmp->name); 812884d179dSJan Kara putname(tmp); 813884d179dSJan Kara if (IS_ERR(bdev)) 814884d179dSJan Kara return ERR_CAST(bdev); 8157d6cd73dSJan Kara if (quotactl_cmd_onoff(cmd)) 8167d6cd73dSJan Kara sb = get_super_exclusive_thawed(bdev); 8177d6cd73dSJan Kara else if (quotactl_cmd_write(cmd)) 818dcdbed85SJan Kara sb = get_super_thawed(bdev); 819dcdbed85SJan Kara else 820884d179dSJan Kara sb = get_super(bdev); 821884d179dSJan Kara bdput(bdev); 822884d179dSJan Kara if (!sb) 823884d179dSJan Kara return ERR_PTR(-ENODEV); 824884d179dSJan Kara 825884d179dSJan Kara return sb; 826884d179dSJan Kara #else 827884d179dSJan Kara return ERR_PTR(-ENODEV); 828884d179dSJan Kara #endif 829884d179dSJan Kara } 830884d179dSJan Kara 831884d179dSJan Kara /* 832884d179dSJan Kara * This is the system call interface. This communicates with 833884d179dSJan Kara * the user-level programs. Currently this only supports diskquota 834884d179dSJan Kara * calls. Maybe we need to add the process quotas etc. in the future, 835884d179dSJan Kara * but we probably should use rlimits for that. 836884d179dSJan Kara */ 837cb0b476aSDominik Brodowski int kernel_quotactl(unsigned int cmd, const char __user *special, 838cb0b476aSDominik Brodowski qid_t id, void __user *addr) 839884d179dSJan Kara { 840884d179dSJan Kara uint cmds, type; 841884d179dSJan Kara struct super_block *sb = NULL; 842f00c9e44SJan Kara struct path path, *pathp = NULL; 843884d179dSJan Kara int ret; 844884d179dSJan Kara 845884d179dSJan Kara cmds = cmd >> SUBCMDSHIFT; 846884d179dSJan Kara type = cmd & SUBCMDMASK; 847884d179dSJan Kara 8486ae09575SChristoph Hellwig /* 8496ae09575SChristoph Hellwig * As a special case Q_SYNC can be called without a specific device. 8506ae09575SChristoph Hellwig * It will iterate all superblocks that have quota enabled and call 8516ae09575SChristoph Hellwig * the sync action on each of them. 8526ae09575SChristoph Hellwig */ 8536ae09575SChristoph Hellwig if (!special) { 8546ae09575SChristoph Hellwig if (cmds == Q_SYNC) 8556ae09575SChristoph Hellwig return quota_sync_all(type); 8566ae09575SChristoph Hellwig return -ENODEV; 8576ae09575SChristoph Hellwig } 8586ae09575SChristoph Hellwig 859f00c9e44SJan Kara /* 860f00c9e44SJan Kara * Path for quotaon has to be resolved before grabbing superblock 861f00c9e44SJan Kara * because that gets s_umount sem which is also possibly needed by path 862f00c9e44SJan Kara * resolution (think about autofs) and thus deadlocks could arise. 863f00c9e44SJan Kara */ 864f00c9e44SJan Kara if (cmds == Q_QUOTAON) { 865815d405cSTrond Myklebust ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 866f00c9e44SJan Kara if (ret) 867f00c9e44SJan Kara pathp = ERR_PTR(ret); 868f00c9e44SJan Kara else 869f00c9e44SJan Kara pathp = &path; 870f00c9e44SJan Kara } 871f00c9e44SJan Kara 872dcdbed85SJan Kara sb = quotactl_block(special, cmds); 8730aaa6188SJan Kara if (IS_ERR(sb)) { 8740aaa6188SJan Kara ret = PTR_ERR(sb); 8750aaa6188SJan Kara goto out; 8760aaa6188SJan Kara } 877884d179dSJan Kara 878f00c9e44SJan Kara ret = do_quotactl(sb, type, cmds, id, addr, pathp); 879884d179dSJan Kara 8807d6cd73dSJan Kara if (!quotactl_cmd_onoff(cmds)) 8816ae09575SChristoph Hellwig drop_super(sb); 8827d6cd73dSJan Kara else 8837d6cd73dSJan Kara drop_super_exclusive(sb); 8840aaa6188SJan Kara out: 885f00c9e44SJan Kara if (pathp && !IS_ERR(pathp)) 886f00c9e44SJan Kara path_put(pathp); 887884d179dSJan Kara return ret; 888884d179dSJan Kara } 889cb0b476aSDominik Brodowski 890cb0b476aSDominik Brodowski SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, 891cb0b476aSDominik Brodowski qid_t, id, void __user *, addr) 892cb0b476aSDominik Brodowski { 893cb0b476aSDominik Brodowski return kernel_quotactl(cmd, special, id, addr); 894cb0b476aSDominik Brodowski } 895