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> 12f3da9310SJeff Liu #include <linux/uaccess.h> 13884d179dSJan Kara #include <linux/kernel.h> 14884d179dSJan Kara #include <linux/security.h> 15884d179dSJan Kara #include <linux/syscalls.h> 16884d179dSJan Kara #include <linux/capability.h> 17884d179dSJan Kara #include <linux/quotaops.h> 18884d179dSJan Kara #include <linux/types.h> 198c4e4acdSChristoph Hellwig #include <linux/writeback.h> 20884d179dSJan Kara 21c988afb5SChristoph Hellwig static int check_quotactl_permission(struct super_block *sb, int type, int cmd, 22268157baSJan Kara qid_t id) 23884d179dSJan Kara { 24c988afb5SChristoph Hellwig switch (cmd) { 25c988afb5SChristoph Hellwig /* these commands do not require any special privilegues */ 26c988afb5SChristoph Hellwig case Q_GETFMT: 27c988afb5SChristoph Hellwig case Q_SYNC: 28c988afb5SChristoph Hellwig case Q_GETINFO: 29c988afb5SChristoph Hellwig case Q_XGETQSTAT: 30af30cb44SChandra Seetharaman case Q_XGETQSTATV: 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: 3674a8a103SEric W. Biederman if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) || 3774a8a103SEric W. Biederman (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), 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 { 502c5f648aSJan Kara int type = *(int *)arg; 512c5f648aSJan Kara 522c5f648aSJan Kara if (sb->s_qcop && sb->s_qcop->quota_sync && 532c5f648aSJan Kara (sb->s_quota_types & (1 << type))) 542c5f648aSJan Kara sb->s_qcop->quota_sync(sb, type); 5501a05b33SAl Viro } 5601a05b33SAl Viro 576ae09575SChristoph Hellwig static int quota_sync_all(int type) 58884d179dSJan Kara { 596ae09575SChristoph Hellwig int ret; 606ae09575SChristoph Hellwig 616ae09575SChristoph Hellwig if (type >= MAXQUOTAS) 626ae09575SChristoph Hellwig return -EINVAL; 636ae09575SChristoph Hellwig ret = security_quotactl(Q_SYNC, type, 0, NULL); 6401a05b33SAl Viro if (!ret) 6501a05b33SAl Viro iterate_supers(quota_sync_one, &type); 666ae09575SChristoph Hellwig return ret; 67884d179dSJan Kara } 68884d179dSJan Kara 69d3b86324SJan Kara unsigned int qtype_enforce_flag(int type) 70d3b86324SJan Kara { 71d3b86324SJan Kara switch (type) { 72d3b86324SJan Kara case USRQUOTA: 73d3b86324SJan Kara return FS_QUOTA_UDQ_ENFD; 74d3b86324SJan Kara case GRPQUOTA: 75d3b86324SJan Kara return FS_QUOTA_GDQ_ENFD; 76d3b86324SJan Kara case PRJQUOTA: 77d3b86324SJan Kara return FS_QUOTA_PDQ_ENFD; 78d3b86324SJan Kara } 79d3b86324SJan Kara return 0; 80d3b86324SJan Kara } 81d3b86324SJan Kara 823218a3ecSEric Sandeen static int quota_quotaon(struct super_block *sb, int type, qid_t id, 83f00c9e44SJan Kara struct path *path) 84884d179dSJan Kara { 85aaa3daedSJan Kara if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable) 86f00c9e44SJan Kara return -ENOSYS; 87d3b86324SJan Kara if (sb->s_qcop->quota_enable) 88d3b86324SJan Kara return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type)); 89f00c9e44SJan Kara if (IS_ERR(path)) 90f00c9e44SJan Kara return PTR_ERR(path); 91f00c9e44SJan Kara return sb->s_qcop->quota_on(sb, type, id, path); 92884d179dSJan Kara } 93884d179dSJan Kara 94d3b86324SJan Kara static int quota_quotaoff(struct super_block *sb, int type) 95d3b86324SJan Kara { 96d3b86324SJan Kara if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable) 97d3b86324SJan Kara return -ENOSYS; 98d3b86324SJan Kara if (sb->s_qcop->quota_disable) 99d3b86324SJan Kara return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type)); 100d3b86324SJan Kara return sb->s_qcop->quota_off(sb, type); 101d3b86324SJan Kara } 102d3b86324SJan Kara 103c411e5f6SChristoph Hellwig static int quota_getfmt(struct super_block *sb, int type, void __user *addr) 104c411e5f6SChristoph Hellwig { 105884d179dSJan Kara __u32 fmt; 106884d179dSJan Kara 107606cdccaSNiu Yawei mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); 108884d179dSJan Kara if (!sb_has_quota_active(sb, type)) { 109606cdccaSNiu Yawei mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); 110884d179dSJan Kara return -ESRCH; 111884d179dSJan Kara } 112884d179dSJan Kara fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; 113606cdccaSNiu Yawei mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); 114884d179dSJan Kara if (copy_to_user(addr, &fmt, sizeof(fmt))) 115884d179dSJan Kara return -EFAULT; 116884d179dSJan Kara return 0; 117884d179dSJan Kara } 118c411e5f6SChristoph Hellwig 119c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr) 120c411e5f6SChristoph Hellwig { 1210a240339SJan Kara struct qc_state state; 1220a240339SJan Kara struct qc_type_state *tstate; 1230a240339SJan Kara struct if_dqinfo uinfo; 124c411e5f6SChristoph Hellwig int ret; 125884d179dSJan Kara 1260a240339SJan Kara /* This checks whether qc_state has enough entries... */ 1270a240339SJan Kara BUILD_BUG_ON(MAXQUOTAS > XQM_MAXQUOTAS); 1280a240339SJan Kara if (!sb->s_qcop->get_state) 129f450d4feSChristoph Hellwig return -ENOSYS; 1300a240339SJan Kara ret = sb->s_qcop->get_state(sb, &state); 1310a240339SJan Kara if (ret) 1320a240339SJan Kara return ret; 1330a240339SJan Kara tstate = state.s_state + type; 1340a240339SJan Kara if (!(tstate->flags & QCI_ACCT_ENABLED)) 1350a240339SJan Kara return -ESRCH; 1360a240339SJan Kara memset(&uinfo, 0, sizeof(uinfo)); 1370a240339SJan Kara uinfo.dqi_bgrace = tstate->spc_timelimit; 1380a240339SJan Kara uinfo.dqi_igrace = tstate->ino_timelimit; 1390a240339SJan Kara if (tstate->flags & QCI_SYSFILE) 1400a240339SJan Kara uinfo.dqi_flags |= DQF_SYS_FILE; 1410a240339SJan Kara if (tstate->flags & QCI_ROOT_SQUASH) 1420a240339SJan Kara uinfo.dqi_flags |= DQF_ROOT_SQUASH; 1430a240339SJan Kara uinfo.dqi_valid = IIF_ALL; 14472d4d0e4SDan Carpenter if (copy_to_user(addr, &uinfo, sizeof(uinfo))) 145884d179dSJan Kara return -EFAULT; 14672d4d0e4SDan Carpenter return 0; 147884d179dSJan Kara } 148c411e5f6SChristoph Hellwig 149c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr) 150c411e5f6SChristoph Hellwig { 151884d179dSJan Kara struct if_dqinfo info; 1525eacb2acSJan Kara struct qc_info qinfo; 153884d179dSJan Kara 154884d179dSJan Kara if (copy_from_user(&info, addr, sizeof(info))) 155884d179dSJan Kara return -EFAULT; 156f450d4feSChristoph Hellwig if (!sb->s_qcop->set_info) 157f450d4feSChristoph Hellwig return -ENOSYS; 1585eacb2acSJan Kara if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE)) 1595eacb2acSJan Kara return -EINVAL; 1605eacb2acSJan Kara memset(&qinfo, 0, sizeof(qinfo)); 1615eacb2acSJan Kara if (info.dqi_valid & IIF_FLAGS) { 1625eacb2acSJan Kara if (info.dqi_flags & ~DQF_SETINFO_MASK) 1635eacb2acSJan Kara return -EINVAL; 1645eacb2acSJan Kara if (info.dqi_flags & DQF_ROOT_SQUASH) 1655eacb2acSJan Kara qinfo.i_flags |= QCI_ROOT_SQUASH; 1665eacb2acSJan Kara qinfo.i_fieldmask |= QC_FLAGS; 1675eacb2acSJan Kara } 1685eacb2acSJan Kara if (info.dqi_valid & IIF_BGRACE) { 1695eacb2acSJan Kara qinfo.i_spc_timelimit = info.dqi_bgrace; 1705eacb2acSJan Kara qinfo.i_fieldmask |= QC_SPC_TIMER; 1715eacb2acSJan Kara } 1725eacb2acSJan Kara if (info.dqi_valid & IIF_IGRACE) { 1735eacb2acSJan Kara qinfo.i_ino_timelimit = info.dqi_igrace; 1745eacb2acSJan Kara qinfo.i_fieldmask |= QC_INO_TIMER; 1755eacb2acSJan Kara } 1765eacb2acSJan Kara return sb->s_qcop->set_info(sb, type, &qinfo); 177884d179dSJan Kara } 178c411e5f6SChristoph Hellwig 17914bf61ffSJan Kara static inline qsize_t qbtos(qsize_t blocks) 18014bf61ffSJan Kara { 18114bf61ffSJan Kara return blocks << QIF_DQBLKSIZE_BITS; 18214bf61ffSJan Kara } 18314bf61ffSJan Kara 18414bf61ffSJan Kara static inline qsize_t stoqb(qsize_t space) 18514bf61ffSJan Kara { 18614bf61ffSJan Kara return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS; 18714bf61ffSJan Kara } 18814bf61ffSJan Kara 18914bf61ffSJan Kara static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src) 190b9b2dd36SChristoph Hellwig { 19118da65e7SDan Carpenter memset(dst, 0, sizeof(*dst)); 19214bf61ffSJan Kara dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit); 19314bf61ffSJan Kara dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit); 19414bf61ffSJan Kara dst->dqb_curspace = src->d_space; 195b9b2dd36SChristoph Hellwig dst->dqb_ihardlimit = src->d_ino_hardlimit; 196b9b2dd36SChristoph Hellwig dst->dqb_isoftlimit = src->d_ino_softlimit; 19714bf61ffSJan Kara dst->dqb_curinodes = src->d_ino_count; 19814bf61ffSJan Kara dst->dqb_btime = src->d_spc_timer; 19914bf61ffSJan Kara dst->dqb_itime = src->d_ino_timer; 200b9b2dd36SChristoph Hellwig dst->dqb_valid = QIF_ALL; 201b9b2dd36SChristoph Hellwig } 202b9b2dd36SChristoph Hellwig 203c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id, 204c411e5f6SChristoph Hellwig void __user *addr) 205c411e5f6SChristoph Hellwig { 20674a8a103SEric W. Biederman struct kqid qid; 20714bf61ffSJan Kara struct qc_dqblk fdq; 208884d179dSJan Kara struct if_dqblk idq; 209c411e5f6SChristoph Hellwig int ret; 210884d179dSJan Kara 211f450d4feSChristoph Hellwig if (!sb->s_qcop->get_dqblk) 212f450d4feSChristoph Hellwig return -ENOSYS; 21374a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 214d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 21574a8a103SEric W. Biederman return -EINVAL; 21674a8a103SEric W. Biederman ret = sb->s_qcop->get_dqblk(sb, qid, &fdq); 217268157baSJan Kara if (ret) 218884d179dSJan Kara return ret; 219b9b2dd36SChristoph Hellwig copy_to_if_dqblk(&idq, &fdq); 220884d179dSJan Kara if (copy_to_user(addr, &idq, sizeof(idq))) 221884d179dSJan Kara return -EFAULT; 222884d179dSJan Kara return 0; 223884d179dSJan Kara } 224c411e5f6SChristoph Hellwig 225926132c0SEric Sandeen /* 226926132c0SEric Sandeen * Return quota for next active quota >= this id, if any exists, 227ba58148bSEric Sandeen * otherwise return -ENOENT via ->get_nextdqblk 228926132c0SEric Sandeen */ 229926132c0SEric Sandeen static int quota_getnextquota(struct super_block *sb, int type, qid_t id, 230926132c0SEric Sandeen void __user *addr) 231926132c0SEric Sandeen { 232926132c0SEric Sandeen struct kqid qid; 233926132c0SEric Sandeen struct qc_dqblk fdq; 234926132c0SEric Sandeen struct if_nextdqblk idq; 235926132c0SEric Sandeen int ret; 236926132c0SEric Sandeen 237926132c0SEric Sandeen if (!sb->s_qcop->get_nextdqblk) 238926132c0SEric Sandeen return -ENOSYS; 239926132c0SEric Sandeen qid = make_kqid(current_user_ns(), type, id); 240d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 241926132c0SEric Sandeen return -EINVAL; 242926132c0SEric Sandeen ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq); 243926132c0SEric Sandeen if (ret) 244926132c0SEric Sandeen return ret; 245926132c0SEric Sandeen /* struct if_nextdqblk is a superset of struct if_dqblk */ 246926132c0SEric Sandeen copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq); 247926132c0SEric Sandeen idq.dqb_id = from_kqid(current_user_ns(), qid); 248926132c0SEric Sandeen if (copy_to_user(addr, &idq, sizeof(idq))) 249926132c0SEric Sandeen return -EFAULT; 250926132c0SEric Sandeen return 0; 251926132c0SEric Sandeen } 252926132c0SEric Sandeen 25314bf61ffSJan Kara static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src) 254c472b432SChristoph Hellwig { 25514bf61ffSJan Kara dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit); 25614bf61ffSJan Kara dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit); 25714bf61ffSJan Kara dst->d_space = src->dqb_curspace; 258c472b432SChristoph Hellwig dst->d_ino_hardlimit = src->dqb_ihardlimit; 259c472b432SChristoph Hellwig dst->d_ino_softlimit = src->dqb_isoftlimit; 26014bf61ffSJan Kara dst->d_ino_count = src->dqb_curinodes; 26114bf61ffSJan Kara dst->d_spc_timer = src->dqb_btime; 26214bf61ffSJan Kara dst->d_ino_timer = src->dqb_itime; 263c472b432SChristoph Hellwig 264c472b432SChristoph Hellwig dst->d_fieldmask = 0; 265c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BLIMITS) 26614bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD; 267c472b432SChristoph Hellwig if (src->dqb_valid & QIF_SPACE) 26814bf61ffSJan Kara dst->d_fieldmask |= QC_SPACE; 269c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ILIMITS) 27014bf61ffSJan Kara dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD; 271c472b432SChristoph Hellwig if (src->dqb_valid & QIF_INODES) 27214bf61ffSJan Kara dst->d_fieldmask |= QC_INO_COUNT; 273c472b432SChristoph Hellwig if (src->dqb_valid & QIF_BTIME) 27414bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_TIMER; 275c472b432SChristoph Hellwig if (src->dqb_valid & QIF_ITIME) 27614bf61ffSJan Kara dst->d_fieldmask |= QC_INO_TIMER; 277c472b432SChristoph Hellwig } 278c472b432SChristoph Hellwig 279c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id, 280c411e5f6SChristoph Hellwig void __user *addr) 281c411e5f6SChristoph Hellwig { 28214bf61ffSJan Kara struct qc_dqblk fdq; 283884d179dSJan Kara struct if_dqblk idq; 28474a8a103SEric W. Biederman struct kqid qid; 285884d179dSJan Kara 286884d179dSJan Kara if (copy_from_user(&idq, addr, sizeof(idq))) 287884d179dSJan Kara return -EFAULT; 288f450d4feSChristoph Hellwig if (!sb->s_qcop->set_dqblk) 289f450d4feSChristoph Hellwig return -ENOSYS; 29074a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 291d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 29274a8a103SEric W. Biederman return -EINVAL; 293c472b432SChristoph Hellwig copy_from_if_dqblk(&fdq, &idq); 29474a8a103SEric W. Biederman return sb->s_qcop->set_dqblk(sb, qid, &fdq); 295884d179dSJan Kara } 296884d179dSJan Kara 29738e478c4SJan Kara static int quota_enable(struct super_block *sb, void __user *addr) 298c411e5f6SChristoph Hellwig { 299884d179dSJan Kara __u32 flags; 300884d179dSJan Kara 301884d179dSJan Kara if (copy_from_user(&flags, addr, sizeof(flags))) 302884d179dSJan Kara return -EFAULT; 30338e478c4SJan Kara if (!sb->s_qcop->quota_enable) 304f450d4feSChristoph Hellwig return -ENOSYS; 30538e478c4SJan Kara return sb->s_qcop->quota_enable(sb, flags); 30638e478c4SJan Kara } 30738e478c4SJan Kara 30838e478c4SJan Kara static int quota_disable(struct super_block *sb, void __user *addr) 30938e478c4SJan Kara { 31038e478c4SJan Kara __u32 flags; 31138e478c4SJan Kara 31238e478c4SJan Kara if (copy_from_user(&flags, addr, sizeof(flags))) 31338e478c4SJan Kara return -EFAULT; 31438e478c4SJan Kara if (!sb->s_qcop->quota_disable) 31538e478c4SJan Kara return -ENOSYS; 31638e478c4SJan Kara return sb->s_qcop->quota_disable(sb, flags); 317884d179dSJan Kara } 318884d179dSJan Kara 319bc230e4aSJan Kara static int quota_state_to_flags(struct qc_state *state) 320bc230e4aSJan Kara { 321bc230e4aSJan Kara int flags = 0; 322bc230e4aSJan Kara 323bc230e4aSJan Kara if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) 324bc230e4aSJan Kara flags |= FS_QUOTA_UDQ_ACCT; 325bc230e4aSJan Kara if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED) 326bc230e4aSJan Kara flags |= FS_QUOTA_UDQ_ENFD; 327bc230e4aSJan Kara if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) 328bc230e4aSJan Kara flags |= FS_QUOTA_GDQ_ACCT; 329bc230e4aSJan Kara if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED) 330bc230e4aSJan Kara flags |= FS_QUOTA_GDQ_ENFD; 331bc230e4aSJan Kara if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) 332bc230e4aSJan Kara flags |= FS_QUOTA_PDQ_ACCT; 333bc230e4aSJan Kara if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED) 334bc230e4aSJan Kara flags |= FS_QUOTA_PDQ_ENFD; 335bc230e4aSJan Kara return flags; 336bc230e4aSJan Kara } 337bc230e4aSJan Kara 338bc230e4aSJan Kara static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs) 339bc230e4aSJan Kara { 340bc230e4aSJan Kara int type; 341bc230e4aSJan Kara struct qc_state state; 342bc230e4aSJan Kara int ret; 343bc230e4aSJan Kara 3443cd0126dSEric Sandeen memset(&state, 0, sizeof (struct qc_state)); 345bc230e4aSJan Kara ret = sb->s_qcop->get_state(sb, &state); 346bc230e4aSJan Kara if (ret < 0) 347bc230e4aSJan Kara return ret; 348bc230e4aSJan Kara 349bc230e4aSJan Kara memset(fqs, 0, sizeof(*fqs)); 350bc230e4aSJan Kara fqs->qs_version = FS_QSTAT_VERSION; 351bc230e4aSJan Kara fqs->qs_flags = quota_state_to_flags(&state); 352bc230e4aSJan Kara /* No quota enabled? */ 353bc230e4aSJan Kara if (!fqs->qs_flags) 354bc230e4aSJan Kara return -ENOSYS; 355bc230e4aSJan Kara fqs->qs_incoredqs = state.s_incoredqs; 356bc230e4aSJan Kara /* 357bc230e4aSJan Kara * GETXSTATE quotactl has space for just one set of time limits so 358bc230e4aSJan Kara * report them for the first enabled quota type 359bc230e4aSJan Kara */ 360bc230e4aSJan Kara for (type = 0; type < XQM_MAXQUOTAS; type++) 361bc230e4aSJan Kara if (state.s_state[type].flags & QCI_ACCT_ENABLED) 362bc230e4aSJan Kara break; 363bc230e4aSJan Kara BUG_ON(type == XQM_MAXQUOTAS); 364bc230e4aSJan Kara fqs->qs_btimelimit = state.s_state[type].spc_timelimit; 365bc230e4aSJan Kara fqs->qs_itimelimit = state.s_state[type].ino_timelimit; 366bc230e4aSJan Kara fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; 367bc230e4aSJan Kara fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; 368bc230e4aSJan Kara fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; 3693cd0126dSEric Sandeen 3703cd0126dSEric Sandeen /* Inodes may be allocated even if inactive; copy out if present */ 3713cd0126dSEric Sandeen if (state.s_state[USRQUOTA].ino) { 372bc230e4aSJan Kara fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; 373bc230e4aSJan Kara fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; 374bc230e4aSJan Kara fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; 375bc230e4aSJan Kara } 3763cd0126dSEric Sandeen if (state.s_state[GRPQUOTA].ino) { 377bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; 378bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; 379bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; 380bc230e4aSJan Kara } 3813cd0126dSEric Sandeen if (state.s_state[PRJQUOTA].ino) { 382bc230e4aSJan Kara /* 383bc230e4aSJan Kara * Q_XGETQSTAT doesn't have room for both group and project 384bc230e4aSJan Kara * quotas. So, allow the project quota values to be copied out 385bc230e4aSJan Kara * only if there is no group quota information available. 386bc230e4aSJan Kara */ 387bc230e4aSJan Kara if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) { 388bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino; 389bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = 390bc230e4aSJan Kara state.s_state[PRJQUOTA].blocks; 391bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = 392bc230e4aSJan Kara state.s_state[PRJQUOTA].nextents; 393bc230e4aSJan Kara } 394bc230e4aSJan Kara } 395bc230e4aSJan Kara return 0; 396bc230e4aSJan Kara } 397bc230e4aSJan Kara 398c411e5f6SChristoph Hellwig static int quota_getxstate(struct super_block *sb, void __user *addr) 399c411e5f6SChristoph Hellwig { 400c411e5f6SChristoph Hellwig struct fs_quota_stat fqs; 401c411e5f6SChristoph Hellwig int ret; 402c411e5f6SChristoph Hellwig 40359b6ba69SJan Kara if (!sb->s_qcop->get_state) 404f450d4feSChristoph Hellwig return -ENOSYS; 405bc230e4aSJan Kara ret = quota_getstate(sb, &fqs); 406c411e5f6SChristoph Hellwig if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 407884d179dSJan Kara return -EFAULT; 408c411e5f6SChristoph Hellwig return ret; 409884d179dSJan Kara } 410c411e5f6SChristoph Hellwig 411bc230e4aSJan Kara static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs) 412bc230e4aSJan Kara { 413bc230e4aSJan Kara int type; 414bc230e4aSJan Kara struct qc_state state; 415bc230e4aSJan Kara int ret; 416bc230e4aSJan Kara 4173cd0126dSEric Sandeen memset(&state, 0, sizeof (struct qc_state)); 418bc230e4aSJan Kara ret = sb->s_qcop->get_state(sb, &state); 419bc230e4aSJan Kara if (ret < 0) 420bc230e4aSJan Kara return ret; 421bc230e4aSJan Kara 422bc230e4aSJan Kara memset(fqs, 0, sizeof(*fqs)); 423bc230e4aSJan Kara fqs->qs_version = FS_QSTAT_VERSION; 424bc230e4aSJan Kara fqs->qs_flags = quota_state_to_flags(&state); 425bc230e4aSJan Kara /* No quota enabled? */ 426bc230e4aSJan Kara if (!fqs->qs_flags) 427bc230e4aSJan Kara return -ENOSYS; 428bc230e4aSJan Kara fqs->qs_incoredqs = state.s_incoredqs; 429bc230e4aSJan Kara /* 430bc230e4aSJan Kara * GETXSTATV quotactl has space for just one set of time limits so 431bc230e4aSJan Kara * report them for the first enabled quota type 432bc230e4aSJan Kara */ 433bc230e4aSJan Kara for (type = 0; type < XQM_MAXQUOTAS; type++) 434bc230e4aSJan Kara if (state.s_state[type].flags & QCI_ACCT_ENABLED) 435bc230e4aSJan Kara break; 436bc230e4aSJan Kara BUG_ON(type == XQM_MAXQUOTAS); 437bc230e4aSJan Kara fqs->qs_btimelimit = state.s_state[type].spc_timelimit; 438bc230e4aSJan Kara fqs->qs_itimelimit = state.s_state[type].ino_timelimit; 439bc230e4aSJan Kara fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; 440bc230e4aSJan Kara fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; 441bc230e4aSJan Kara fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; 4423cd0126dSEric Sandeen 4433cd0126dSEric Sandeen /* Inodes may be allocated even if inactive; copy out if present */ 4443cd0126dSEric Sandeen if (state.s_state[USRQUOTA].ino) { 445bc230e4aSJan Kara fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; 446bc230e4aSJan Kara fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; 447bc230e4aSJan Kara fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; 448bc230e4aSJan Kara } 4493cd0126dSEric Sandeen if (state.s_state[GRPQUOTA].ino) { 450bc230e4aSJan Kara fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; 451bc230e4aSJan Kara fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; 452bc230e4aSJan Kara fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; 453bc230e4aSJan Kara } 4543cd0126dSEric Sandeen if (state.s_state[PRJQUOTA].ino) { 455bc230e4aSJan Kara fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino; 456bc230e4aSJan Kara fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks; 457bc230e4aSJan Kara fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents; 458bc230e4aSJan Kara } 459bc230e4aSJan Kara return 0; 460bc230e4aSJan Kara } 461bc230e4aSJan Kara 462af30cb44SChandra Seetharaman static int quota_getxstatev(struct super_block *sb, void __user *addr) 463af30cb44SChandra Seetharaman { 464af30cb44SChandra Seetharaman struct fs_quota_statv fqs; 465af30cb44SChandra Seetharaman int ret; 466af30cb44SChandra Seetharaman 46759b6ba69SJan Kara if (!sb->s_qcop->get_state) 468af30cb44SChandra Seetharaman return -ENOSYS; 469af30cb44SChandra Seetharaman 470af30cb44SChandra Seetharaman memset(&fqs, 0, sizeof(fqs)); 471af30cb44SChandra Seetharaman if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */ 472af30cb44SChandra Seetharaman return -EFAULT; 473af30cb44SChandra Seetharaman 474af30cb44SChandra Seetharaman /* If this kernel doesn't support user specified version, fail */ 475af30cb44SChandra Seetharaman switch (fqs.qs_version) { 476af30cb44SChandra Seetharaman case FS_QSTATV_VERSION1: 477af30cb44SChandra Seetharaman break; 478af30cb44SChandra Seetharaman default: 479af30cb44SChandra Seetharaman return -EINVAL; 480af30cb44SChandra Seetharaman } 481bc230e4aSJan Kara ret = quota_getstatev(sb, &fqs); 482af30cb44SChandra Seetharaman if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 483af30cb44SChandra Seetharaman return -EFAULT; 484af30cb44SChandra Seetharaman return ret; 485af30cb44SChandra Seetharaman } 486af30cb44SChandra Seetharaman 48714bf61ffSJan Kara /* 48814bf61ffSJan Kara * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them 48914bf61ffSJan Kara * out of there as xfsprogs rely on definitions being in that header file. So 49014bf61ffSJan Kara * just define same functions here for quota purposes. 49114bf61ffSJan Kara */ 49214bf61ffSJan Kara #define XFS_BB_SHIFT 9 49314bf61ffSJan Kara 49414bf61ffSJan Kara static inline u64 quota_bbtob(u64 blocks) 49514bf61ffSJan Kara { 49614bf61ffSJan Kara return blocks << XFS_BB_SHIFT; 49714bf61ffSJan Kara } 49814bf61ffSJan Kara 49914bf61ffSJan Kara static inline u64 quota_btobb(u64 bytes) 50014bf61ffSJan Kara { 50114bf61ffSJan Kara return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT; 50214bf61ffSJan Kara } 50314bf61ffSJan Kara 50414bf61ffSJan Kara static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src) 50514bf61ffSJan Kara { 50614bf61ffSJan Kara dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit); 50714bf61ffSJan Kara dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit); 50814bf61ffSJan Kara dst->d_ino_hardlimit = src->d_ino_hardlimit; 50914bf61ffSJan Kara dst->d_ino_softlimit = src->d_ino_softlimit; 51014bf61ffSJan Kara dst->d_space = quota_bbtob(src->d_bcount); 51114bf61ffSJan Kara dst->d_ino_count = src->d_icount; 51214bf61ffSJan Kara dst->d_ino_timer = src->d_itimer; 51314bf61ffSJan Kara dst->d_spc_timer = src->d_btimer; 51414bf61ffSJan Kara dst->d_ino_warns = src->d_iwarns; 51514bf61ffSJan Kara dst->d_spc_warns = src->d_bwarns; 51614bf61ffSJan Kara dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit); 51714bf61ffSJan Kara dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit); 51814bf61ffSJan Kara dst->d_rt_space = quota_bbtob(src->d_rtbcount); 51914bf61ffSJan Kara dst->d_rt_spc_timer = src->d_rtbtimer; 52014bf61ffSJan Kara dst->d_rt_spc_warns = src->d_rtbwarns; 52114bf61ffSJan Kara dst->d_fieldmask = 0; 52214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ISOFT) 52314bf61ffSJan Kara dst->d_fieldmask |= QC_INO_SOFT; 52414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_IHARD) 52514bf61ffSJan Kara dst->d_fieldmask |= QC_INO_HARD; 52614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BSOFT) 52714bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_SOFT; 52814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BHARD) 52914bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_HARD; 53014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBSOFT) 53114bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_SOFT; 53214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBHARD) 53314bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_HARD; 53414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BTIMER) 53514bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_TIMER; 53614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ITIMER) 53714bf61ffSJan Kara dst->d_fieldmask |= QC_INO_TIMER; 53814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBTIMER) 53914bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_TIMER; 54014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BWARNS) 54114bf61ffSJan Kara dst->d_fieldmask |= QC_SPC_WARNS; 54214bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_IWARNS) 54314bf61ffSJan Kara dst->d_fieldmask |= QC_INO_WARNS; 54414bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBWARNS) 54514bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPC_WARNS; 54614bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_BCOUNT) 54714bf61ffSJan Kara dst->d_fieldmask |= QC_SPACE; 54814bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_ICOUNT) 54914bf61ffSJan Kara dst->d_fieldmask |= QC_INO_COUNT; 55014bf61ffSJan Kara if (src->d_fieldmask & FS_DQ_RTBCOUNT) 55114bf61ffSJan Kara dst->d_fieldmask |= QC_RT_SPACE; 55214bf61ffSJan Kara } 55314bf61ffSJan Kara 554c39fb53bSJan Kara static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst, 555c39fb53bSJan Kara struct fs_disk_quota *src) 556c39fb53bSJan Kara { 557c39fb53bSJan Kara memset(dst, 0, sizeof(*dst)); 558c39fb53bSJan Kara dst->i_spc_timelimit = src->d_btimer; 559c39fb53bSJan Kara dst->i_ino_timelimit = src->d_itimer; 560c39fb53bSJan Kara dst->i_rt_spc_timelimit = src->d_rtbtimer; 561c39fb53bSJan Kara dst->i_ino_warnlimit = src->d_iwarns; 562c39fb53bSJan Kara dst->i_spc_warnlimit = src->d_bwarns; 563c39fb53bSJan Kara dst->i_rt_spc_warnlimit = src->d_rtbwarns; 564c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_BWARNS) 565c39fb53bSJan Kara dst->i_fieldmask |= QC_SPC_WARNS; 566c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_IWARNS) 567c39fb53bSJan Kara dst->i_fieldmask |= QC_INO_WARNS; 568c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_RTBWARNS) 569c39fb53bSJan Kara dst->i_fieldmask |= QC_RT_SPC_WARNS; 570c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_BTIMER) 571c39fb53bSJan Kara dst->i_fieldmask |= QC_SPC_TIMER; 572c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_ITIMER) 573c39fb53bSJan Kara dst->i_fieldmask |= QC_INO_TIMER; 574c39fb53bSJan Kara if (src->d_fieldmask & FS_DQ_RTBTIMER) 575c39fb53bSJan Kara dst->i_fieldmask |= QC_RT_SPC_TIMER; 576c39fb53bSJan Kara } 577c39fb53bSJan Kara 578c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id, 579c411e5f6SChristoph Hellwig void __user *addr) 580c411e5f6SChristoph Hellwig { 581884d179dSJan Kara struct fs_disk_quota fdq; 58214bf61ffSJan Kara struct qc_dqblk qdq; 58374a8a103SEric W. Biederman struct kqid qid; 584884d179dSJan Kara 585884d179dSJan Kara if (copy_from_user(&fdq, addr, sizeof(fdq))) 586884d179dSJan Kara return -EFAULT; 587c472b432SChristoph Hellwig if (!sb->s_qcop->set_dqblk) 588f450d4feSChristoph Hellwig return -ENOSYS; 58974a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 590d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 59174a8a103SEric W. Biederman return -EINVAL; 592c39fb53bSJan Kara /* Are we actually setting timer / warning limits for all users? */ 593cfd4c70aSEric W. Biederman if (from_kqid(sb->s_user_ns, qid) == 0 && 594c39fb53bSJan Kara fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) { 595c39fb53bSJan Kara struct qc_info qinfo; 596c39fb53bSJan Kara int ret; 597c39fb53bSJan Kara 598c39fb53bSJan Kara if (!sb->s_qcop->set_info) 599c39fb53bSJan Kara return -EINVAL; 600c39fb53bSJan Kara copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq); 601c39fb53bSJan Kara ret = sb->s_qcop->set_info(sb, type, &qinfo); 602c39fb53bSJan Kara if (ret) 603c39fb53bSJan Kara return ret; 604c39fb53bSJan Kara /* These are already done */ 605c39fb53bSJan Kara fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK); 606c39fb53bSJan Kara } 60714bf61ffSJan Kara copy_from_xfs_dqblk(&qdq, &fdq); 60814bf61ffSJan Kara return sb->s_qcop->set_dqblk(sb, qid, &qdq); 60914bf61ffSJan Kara } 61014bf61ffSJan Kara 61114bf61ffSJan Kara static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src, 61214bf61ffSJan Kara int type, qid_t id) 61314bf61ffSJan Kara { 61414bf61ffSJan Kara memset(dst, 0, sizeof(*dst)); 61514bf61ffSJan Kara dst->d_version = FS_DQUOT_VERSION; 61614bf61ffSJan Kara dst->d_id = id; 61714bf61ffSJan Kara if (type == USRQUOTA) 61814bf61ffSJan Kara dst->d_flags = FS_USER_QUOTA; 61914bf61ffSJan Kara else if (type == PRJQUOTA) 62014bf61ffSJan Kara dst->d_flags = FS_PROJ_QUOTA; 62114bf61ffSJan Kara else 62214bf61ffSJan Kara dst->d_flags = FS_GROUP_QUOTA; 62314bf61ffSJan Kara dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit); 62414bf61ffSJan Kara dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit); 62514bf61ffSJan Kara dst->d_ino_hardlimit = src->d_ino_hardlimit; 62614bf61ffSJan Kara dst->d_ino_softlimit = src->d_ino_softlimit; 62714bf61ffSJan Kara dst->d_bcount = quota_btobb(src->d_space); 62814bf61ffSJan Kara dst->d_icount = src->d_ino_count; 62914bf61ffSJan Kara dst->d_itimer = src->d_ino_timer; 63014bf61ffSJan Kara dst->d_btimer = src->d_spc_timer; 63114bf61ffSJan Kara dst->d_iwarns = src->d_ino_warns; 63214bf61ffSJan Kara dst->d_bwarns = src->d_spc_warns; 63314bf61ffSJan Kara dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit); 63414bf61ffSJan Kara dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit); 63514bf61ffSJan Kara dst->d_rtbcount = quota_btobb(src->d_rt_space); 63614bf61ffSJan Kara dst->d_rtbtimer = src->d_rt_spc_timer; 63714bf61ffSJan Kara dst->d_rtbwarns = src->d_rt_spc_warns; 638884d179dSJan Kara } 639c411e5f6SChristoph Hellwig 640c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id, 641c411e5f6SChristoph Hellwig void __user *addr) 642c411e5f6SChristoph Hellwig { 643884d179dSJan Kara struct fs_disk_quota fdq; 64414bf61ffSJan Kara struct qc_dqblk qdq; 64574a8a103SEric W. Biederman struct kqid qid; 646c411e5f6SChristoph Hellwig int ret; 647884d179dSJan Kara 648b9b2dd36SChristoph Hellwig if (!sb->s_qcop->get_dqblk) 649f450d4feSChristoph Hellwig return -ENOSYS; 65074a8a103SEric W. Biederman qid = make_kqid(current_user_ns(), type, id); 651d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 65274a8a103SEric W. Biederman return -EINVAL; 65314bf61ffSJan Kara ret = sb->s_qcop->get_dqblk(sb, qid, &qdq); 65414bf61ffSJan Kara if (ret) 65514bf61ffSJan Kara return ret; 65614bf61ffSJan Kara copy_to_xfs_dqblk(&fdq, &qdq, type, id); 65714bf61ffSJan Kara if (copy_to_user(addr, &fdq, sizeof(fdq))) 658884d179dSJan Kara return -EFAULT; 659c411e5f6SChristoph Hellwig return ret; 660884d179dSJan Kara } 661c411e5f6SChristoph Hellwig 6628b375249SEric Sandeen /* 6638b375249SEric Sandeen * Return quota for next active quota >= this id, if any exists, 664ba58148bSEric Sandeen * otherwise return -ENOENT via ->get_nextdqblk. 6658b375249SEric Sandeen */ 6668b375249SEric Sandeen static int quota_getnextxquota(struct super_block *sb, int type, qid_t id, 6678b375249SEric Sandeen void __user *addr) 6688b375249SEric Sandeen { 6698b375249SEric Sandeen struct fs_disk_quota fdq; 6708b375249SEric Sandeen struct qc_dqblk qdq; 6718b375249SEric Sandeen struct kqid qid; 6728b375249SEric Sandeen qid_t id_out; 6738b375249SEric Sandeen int ret; 6748b375249SEric Sandeen 6758b375249SEric Sandeen if (!sb->s_qcop->get_nextdqblk) 6768b375249SEric Sandeen return -ENOSYS; 6778b375249SEric Sandeen qid = make_kqid(current_user_ns(), type, id); 678d49d3762SEric W. Biederman if (!qid_has_mapping(sb->s_user_ns, qid)) 6798b375249SEric Sandeen return -EINVAL; 6808b375249SEric Sandeen ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq); 6818b375249SEric Sandeen if (ret) 6828b375249SEric Sandeen return ret; 6838b375249SEric Sandeen id_out = from_kqid(current_user_ns(), qid); 6848b375249SEric Sandeen copy_to_xfs_dqblk(&fdq, &qdq, type, id_out); 6858b375249SEric Sandeen if (copy_to_user(addr, &fdq, sizeof(fdq))) 6868b375249SEric Sandeen return -EFAULT; 6878b375249SEric Sandeen return ret; 6888b375249SEric Sandeen } 6898b375249SEric Sandeen 6909da93f9bSEric Sandeen static int quota_rmxquota(struct super_block *sb, void __user *addr) 6919da93f9bSEric Sandeen { 6929da93f9bSEric Sandeen __u32 flags; 6939da93f9bSEric Sandeen 6949da93f9bSEric Sandeen if (copy_from_user(&flags, addr, sizeof(flags))) 6959da93f9bSEric Sandeen return -EFAULT; 6969da93f9bSEric Sandeen if (!sb->s_qcop->rm_xquota) 6979da93f9bSEric Sandeen return -ENOSYS; 6989da93f9bSEric Sandeen return sb->s_qcop->rm_xquota(sb, flags); 6999da93f9bSEric Sandeen } 7009da93f9bSEric Sandeen 701c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */ 702c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, 703f00c9e44SJan Kara void __user *addr, struct path *path) 704c411e5f6SChristoph Hellwig { 705c988afb5SChristoph Hellwig int ret; 706c988afb5SChristoph Hellwig 707c988afb5SChristoph Hellwig if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS)) 708c988afb5SChristoph Hellwig return -EINVAL; 7092c5f648aSJan Kara /* 7102c5f648aSJan Kara * Quota not supported on this fs? Check this before s_quota_types 7112c5f648aSJan Kara * since they needn't be set if quota is not supported at all. 7122c5f648aSJan Kara */ 713c988afb5SChristoph Hellwig if (!sb->s_qcop) 714c988afb5SChristoph Hellwig return -ENOSYS; 7152c5f648aSJan Kara if (!(sb->s_quota_types & (1 << type))) 7162c5f648aSJan Kara return -EINVAL; 717c988afb5SChristoph Hellwig 718c988afb5SChristoph Hellwig ret = check_quotactl_permission(sb, type, cmd, id); 719c988afb5SChristoph Hellwig if (ret < 0) 720c988afb5SChristoph Hellwig return ret; 721c988afb5SChristoph Hellwig 722c411e5f6SChristoph Hellwig switch (cmd) { 723c411e5f6SChristoph Hellwig case Q_QUOTAON: 7243218a3ecSEric Sandeen return quota_quotaon(sb, type, id, path); 725c411e5f6SChristoph Hellwig case Q_QUOTAOFF: 726d3b86324SJan Kara return quota_quotaoff(sb, type); 727c411e5f6SChristoph Hellwig case Q_GETFMT: 728c411e5f6SChristoph Hellwig return quota_getfmt(sb, type, addr); 729c411e5f6SChristoph Hellwig case Q_GETINFO: 730c411e5f6SChristoph Hellwig return quota_getinfo(sb, type, addr); 731c411e5f6SChristoph Hellwig case Q_SETINFO: 732c411e5f6SChristoph Hellwig return quota_setinfo(sb, type, addr); 733c411e5f6SChristoph Hellwig case Q_GETQUOTA: 734c411e5f6SChristoph Hellwig return quota_getquota(sb, type, id, addr); 735926132c0SEric Sandeen case Q_GETNEXTQUOTA: 736926132c0SEric Sandeen return quota_getnextquota(sb, type, id, addr); 737c411e5f6SChristoph Hellwig case Q_SETQUOTA: 738c411e5f6SChristoph Hellwig return quota_setquota(sb, type, id, addr); 739c411e5f6SChristoph Hellwig case Q_SYNC: 740f450d4feSChristoph Hellwig if (!sb->s_qcop->quota_sync) 741f450d4feSChristoph Hellwig return -ENOSYS; 742ceed1723SJan Kara return sb->s_qcop->quota_sync(sb, type); 743c411e5f6SChristoph Hellwig case Q_XQUOTAON: 74438e478c4SJan Kara return quota_enable(sb, addr); 745c411e5f6SChristoph Hellwig case Q_XQUOTAOFF: 74638e478c4SJan Kara return quota_disable(sb, addr); 7479da93f9bSEric Sandeen case Q_XQUOTARM: 7489da93f9bSEric Sandeen return quota_rmxquota(sb, addr); 749c411e5f6SChristoph Hellwig case Q_XGETQSTAT: 750c411e5f6SChristoph Hellwig return quota_getxstate(sb, addr); 751af30cb44SChandra Seetharaman case Q_XGETQSTATV: 752af30cb44SChandra Seetharaman return quota_getxstatev(sb, addr); 753c411e5f6SChristoph Hellwig case Q_XSETQLIM: 754c411e5f6SChristoph Hellwig return quota_setxquota(sb, type, id, addr); 755c411e5f6SChristoph Hellwig case Q_XGETQUOTA: 756c411e5f6SChristoph Hellwig return quota_getxquota(sb, type, id, addr); 7578b375249SEric Sandeen case Q_XGETNEXTQUOTA: 7588b375249SEric Sandeen return quota_getnextxquota(sb, type, id, addr); 759884d179dSJan Kara case Q_XQUOTASYNC: 7608c4e4acdSChristoph Hellwig if (sb->s_flags & MS_RDONLY) 7618c4e4acdSChristoph Hellwig return -EROFS; 7624b217ed9SChristoph Hellwig /* XFS quotas are fully coherent now, making this call a noop */ 7638c4e4acdSChristoph Hellwig return 0; 764884d179dSJan Kara default: 765f450d4feSChristoph Hellwig return -EINVAL; 766884d179dSJan Kara } 767884d179dSJan Kara } 768884d179dSJan Kara 76956df1278SLee Jones #ifdef CONFIG_BLOCK 77056df1278SLee Jones 771dcdbed85SJan Kara /* Return 1 if 'cmd' will block on frozen filesystem */ 772dcdbed85SJan Kara static int quotactl_cmd_write(int cmd) 773dcdbed85SJan Kara { 774ccf370e4SJan Kara /* 775ccf370e4SJan Kara * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access 776ccf370e4SJan Kara * as dquot_acquire() may allocate space for new structure and OCFS2 777ccf370e4SJan Kara * needs to increment on-disk use count. 778ccf370e4SJan Kara */ 779dcdbed85SJan Kara switch (cmd) { 780dcdbed85SJan Kara case Q_GETFMT: 781dcdbed85SJan Kara case Q_GETINFO: 782dcdbed85SJan Kara case Q_SYNC: 783dcdbed85SJan Kara case Q_XGETQSTAT: 784af30cb44SChandra Seetharaman case Q_XGETQSTATV: 785dcdbed85SJan Kara case Q_XGETQUOTA: 7868b375249SEric Sandeen case Q_XGETNEXTQUOTA: 787dcdbed85SJan Kara case Q_XQUOTASYNC: 788dcdbed85SJan Kara return 0; 789dcdbed85SJan Kara } 790dcdbed85SJan Kara return 1; 791dcdbed85SJan Kara } 79256df1278SLee Jones #endif /* CONFIG_BLOCK */ 79356df1278SLee Jones 794*7d6cd73dSJan Kara /* Return true if quotactl command is manipulating quota on/off state */ 795*7d6cd73dSJan Kara static bool quotactl_cmd_onoff(int cmd) 796*7d6cd73dSJan Kara { 797*7d6cd73dSJan Kara return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF); 798*7d6cd73dSJan Kara } 799*7d6cd73dSJan Kara 800884d179dSJan Kara /* 801884d179dSJan Kara * look up a superblock on which quota ops will be performed 802884d179dSJan Kara * - use the name of a block device to find the superblock thereon 803884d179dSJan Kara */ 804dcdbed85SJan Kara static struct super_block *quotactl_block(const char __user *special, int cmd) 805884d179dSJan Kara { 806884d179dSJan Kara #ifdef CONFIG_BLOCK 807884d179dSJan Kara struct block_device *bdev; 808884d179dSJan Kara struct super_block *sb; 80991a27b2aSJeff Layton struct filename *tmp = getname(special); 810884d179dSJan Kara 811884d179dSJan Kara if (IS_ERR(tmp)) 812884d179dSJan Kara return ERR_CAST(tmp); 81391a27b2aSJeff Layton bdev = lookup_bdev(tmp->name); 814884d179dSJan Kara putname(tmp); 815884d179dSJan Kara if (IS_ERR(bdev)) 816884d179dSJan Kara return ERR_CAST(bdev); 817*7d6cd73dSJan Kara if (quotactl_cmd_onoff(cmd)) 818*7d6cd73dSJan Kara sb = get_super_exclusive_thawed(bdev); 819*7d6cd73dSJan Kara else if (quotactl_cmd_write(cmd)) 820dcdbed85SJan Kara sb = get_super_thawed(bdev); 821dcdbed85SJan Kara else 822884d179dSJan Kara sb = get_super(bdev); 823884d179dSJan Kara bdput(bdev); 824884d179dSJan Kara if (!sb) 825884d179dSJan Kara return ERR_PTR(-ENODEV); 826884d179dSJan Kara 827884d179dSJan Kara return sb; 828884d179dSJan Kara #else 829884d179dSJan Kara return ERR_PTR(-ENODEV); 830884d179dSJan Kara #endif 831884d179dSJan Kara } 832884d179dSJan Kara 833884d179dSJan Kara /* 834884d179dSJan Kara * This is the system call interface. This communicates with 835884d179dSJan Kara * the user-level programs. Currently this only supports diskquota 836884d179dSJan Kara * calls. Maybe we need to add the process quotas etc. in the future, 837884d179dSJan Kara * but we probably should use rlimits for that. 838884d179dSJan Kara */ 839884d179dSJan Kara SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, 840884d179dSJan Kara qid_t, id, void __user *, addr) 841884d179dSJan Kara { 842884d179dSJan Kara uint cmds, type; 843884d179dSJan Kara struct super_block *sb = NULL; 844f00c9e44SJan Kara struct path path, *pathp = NULL; 845884d179dSJan Kara int ret; 846884d179dSJan Kara 847884d179dSJan Kara cmds = cmd >> SUBCMDSHIFT; 848884d179dSJan Kara type = cmd & SUBCMDMASK; 849884d179dSJan Kara 8506ae09575SChristoph Hellwig /* 8516ae09575SChristoph Hellwig * As a special case Q_SYNC can be called without a specific device. 8526ae09575SChristoph Hellwig * It will iterate all superblocks that have quota enabled and call 8536ae09575SChristoph Hellwig * the sync action on each of them. 8546ae09575SChristoph Hellwig */ 8556ae09575SChristoph Hellwig if (!special) { 8566ae09575SChristoph Hellwig if (cmds == Q_SYNC) 8576ae09575SChristoph Hellwig return quota_sync_all(type); 8586ae09575SChristoph Hellwig return -ENODEV; 8596ae09575SChristoph Hellwig } 8606ae09575SChristoph Hellwig 861f00c9e44SJan Kara /* 862f00c9e44SJan Kara * Path for quotaon has to be resolved before grabbing superblock 863f00c9e44SJan Kara * because that gets s_umount sem which is also possibly needed by path 864f00c9e44SJan Kara * resolution (think about autofs) and thus deadlocks could arise. 865f00c9e44SJan Kara */ 866f00c9e44SJan Kara if (cmds == Q_QUOTAON) { 867815d405cSTrond Myklebust ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 868f00c9e44SJan Kara if (ret) 869f00c9e44SJan Kara pathp = ERR_PTR(ret); 870f00c9e44SJan Kara else 871f00c9e44SJan Kara pathp = &path; 872f00c9e44SJan Kara } 873f00c9e44SJan Kara 874dcdbed85SJan Kara sb = quotactl_block(special, cmds); 8750aaa6188SJan Kara if (IS_ERR(sb)) { 8760aaa6188SJan Kara ret = PTR_ERR(sb); 8770aaa6188SJan Kara goto out; 8780aaa6188SJan Kara } 879884d179dSJan Kara 880f00c9e44SJan Kara ret = do_quotactl(sb, type, cmds, id, addr, pathp); 881884d179dSJan Kara 882*7d6cd73dSJan Kara if (!quotactl_cmd_onoff(cmds)) 8836ae09575SChristoph Hellwig drop_super(sb); 884*7d6cd73dSJan Kara else 885*7d6cd73dSJan Kara drop_super_exclusive(sb); 8860aaa6188SJan Kara out: 887f00c9e44SJan Kara if (pathp && !IS_ERR(pathp)) 888f00c9e44SJan Kara path_put(pathp); 889884d179dSJan Kara return ret; 890884d179dSJan Kara } 891