0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/fs.h>
0010 #include <linux/namei.h>
0011 #include <linux/slab.h>
0012 #include <asm/current.h>
0013 #include <linux/blkdev.h>
0014 #include <linux/uaccess.h>
0015 #include <linux/kernel.h>
0016 #include <linux/security.h>
0017 #include <linux/syscalls.h>
0018 #include <linux/capability.h>
0019 #include <linux/quotaops.h>
0020 #include <linux/types.h>
0021 #include <linux/mount.h>
0022 #include <linux/writeback.h>
0023 #include <linux/nospec.h>
0024 #include "compat.h"
0025 #include "../internal.h"
0026
0027 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
0028 qid_t id)
0029 {
0030 switch (cmd) {
0031
0032 case Q_GETFMT:
0033 case Q_SYNC:
0034 case Q_GETINFO:
0035 case Q_XGETQSTAT:
0036 case Q_XGETQSTATV:
0037 case Q_XQUOTASYNC:
0038 break;
0039
0040 case Q_GETQUOTA:
0041 case Q_XGETQUOTA:
0042 if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
0043 (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
0044 break;
0045 fallthrough;
0046 default:
0047 if (!capable(CAP_SYS_ADMIN))
0048 return -EPERM;
0049 }
0050
0051 return security_quotactl(cmd, type, id, sb);
0052 }
0053
0054 static void quota_sync_one(struct super_block *sb, void *arg)
0055 {
0056 int type = *(int *)arg;
0057
0058 if (sb->s_qcop && sb->s_qcop->quota_sync &&
0059 (sb->s_quota_types & (1 << type)))
0060 sb->s_qcop->quota_sync(sb, type);
0061 }
0062
0063 static int quota_sync_all(int type)
0064 {
0065 int ret;
0066
0067 ret = security_quotactl(Q_SYNC, type, 0, NULL);
0068 if (!ret)
0069 iterate_supers(quota_sync_one, &type);
0070 return ret;
0071 }
0072
0073 unsigned int qtype_enforce_flag(int type)
0074 {
0075 switch (type) {
0076 case USRQUOTA:
0077 return FS_QUOTA_UDQ_ENFD;
0078 case GRPQUOTA:
0079 return FS_QUOTA_GDQ_ENFD;
0080 case PRJQUOTA:
0081 return FS_QUOTA_PDQ_ENFD;
0082 }
0083 return 0;
0084 }
0085
0086 static int quota_quotaon(struct super_block *sb, int type, qid_t id,
0087 const struct path *path)
0088 {
0089 if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
0090 return -ENOSYS;
0091 if (sb->s_qcop->quota_enable)
0092 return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
0093 if (IS_ERR(path))
0094 return PTR_ERR(path);
0095 return sb->s_qcop->quota_on(sb, type, id, path);
0096 }
0097
0098 static int quota_quotaoff(struct super_block *sb, int type)
0099 {
0100 if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
0101 return -ENOSYS;
0102 if (sb->s_qcop->quota_disable)
0103 return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type));
0104 return sb->s_qcop->quota_off(sb, type);
0105 }
0106
0107 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
0108 {
0109 __u32 fmt;
0110
0111 if (!sb_has_quota_active(sb, type))
0112 return -ESRCH;
0113 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
0114 if (copy_to_user(addr, &fmt, sizeof(fmt)))
0115 return -EFAULT;
0116 return 0;
0117 }
0118
0119 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
0120 {
0121 struct qc_state state;
0122 struct qc_type_state *tstate;
0123 struct if_dqinfo uinfo;
0124 int ret;
0125
0126 if (!sb->s_qcop->get_state)
0127 return -ENOSYS;
0128 ret = sb->s_qcop->get_state(sb, &state);
0129 if (ret)
0130 return ret;
0131 tstate = state.s_state + type;
0132 if (!(tstate->flags & QCI_ACCT_ENABLED))
0133 return -ESRCH;
0134 memset(&uinfo, 0, sizeof(uinfo));
0135 uinfo.dqi_bgrace = tstate->spc_timelimit;
0136 uinfo.dqi_igrace = tstate->ino_timelimit;
0137 if (tstate->flags & QCI_SYSFILE)
0138 uinfo.dqi_flags |= DQF_SYS_FILE;
0139 if (tstate->flags & QCI_ROOT_SQUASH)
0140 uinfo.dqi_flags |= DQF_ROOT_SQUASH;
0141 uinfo.dqi_valid = IIF_ALL;
0142 if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
0143 return -EFAULT;
0144 return 0;
0145 }
0146
0147 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
0148 {
0149 struct if_dqinfo info;
0150 struct qc_info qinfo;
0151
0152 if (copy_from_user(&info, addr, sizeof(info)))
0153 return -EFAULT;
0154 if (!sb->s_qcop->set_info)
0155 return -ENOSYS;
0156 if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
0157 return -EINVAL;
0158 memset(&qinfo, 0, sizeof(qinfo));
0159 if (info.dqi_valid & IIF_FLAGS) {
0160 if (info.dqi_flags & ~DQF_SETINFO_MASK)
0161 return -EINVAL;
0162 if (info.dqi_flags & DQF_ROOT_SQUASH)
0163 qinfo.i_flags |= QCI_ROOT_SQUASH;
0164 qinfo.i_fieldmask |= QC_FLAGS;
0165 }
0166 if (info.dqi_valid & IIF_BGRACE) {
0167 qinfo.i_spc_timelimit = info.dqi_bgrace;
0168 qinfo.i_fieldmask |= QC_SPC_TIMER;
0169 }
0170 if (info.dqi_valid & IIF_IGRACE) {
0171 qinfo.i_ino_timelimit = info.dqi_igrace;
0172 qinfo.i_fieldmask |= QC_INO_TIMER;
0173 }
0174 return sb->s_qcop->set_info(sb, type, &qinfo);
0175 }
0176
0177 static inline qsize_t qbtos(qsize_t blocks)
0178 {
0179 return blocks << QIF_DQBLKSIZE_BITS;
0180 }
0181
0182 static inline qsize_t stoqb(qsize_t space)
0183 {
0184 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
0185 }
0186
0187 static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
0188 {
0189 memset(dst, 0, sizeof(*dst));
0190 dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
0191 dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
0192 dst->dqb_curspace = src->d_space;
0193 dst->dqb_ihardlimit = src->d_ino_hardlimit;
0194 dst->dqb_isoftlimit = src->d_ino_softlimit;
0195 dst->dqb_curinodes = src->d_ino_count;
0196 dst->dqb_btime = src->d_spc_timer;
0197 dst->dqb_itime = src->d_ino_timer;
0198 dst->dqb_valid = QIF_ALL;
0199 }
0200
0201 static int quota_getquota(struct super_block *sb, int type, qid_t id,
0202 void __user *addr)
0203 {
0204 struct kqid qid;
0205 struct qc_dqblk fdq;
0206 struct if_dqblk idq;
0207 int ret;
0208
0209 if (!sb->s_qcop->get_dqblk)
0210 return -ENOSYS;
0211 qid = make_kqid(current_user_ns(), type, id);
0212 if (!qid_has_mapping(sb->s_user_ns, qid))
0213 return -EINVAL;
0214 ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
0215 if (ret)
0216 return ret;
0217 copy_to_if_dqblk(&idq, &fdq);
0218
0219 if (compat_need_64bit_alignment_fixup()) {
0220 struct compat_if_dqblk __user *compat_dqblk = addr;
0221
0222 if (copy_to_user(compat_dqblk, &idq, sizeof(*compat_dqblk)))
0223 return -EFAULT;
0224 if (put_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
0225 return -EFAULT;
0226 } else {
0227 if (copy_to_user(addr, &idq, sizeof(idq)))
0228 return -EFAULT;
0229 }
0230 return 0;
0231 }
0232
0233
0234
0235
0236
0237 static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
0238 void __user *addr)
0239 {
0240 struct kqid qid;
0241 struct qc_dqblk fdq;
0242 struct if_nextdqblk idq;
0243 int ret;
0244
0245 if (!sb->s_qcop->get_nextdqblk)
0246 return -ENOSYS;
0247 qid = make_kqid(current_user_ns(), type, id);
0248 if (!qid_has_mapping(sb->s_user_ns, qid))
0249 return -EINVAL;
0250 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
0251 if (ret)
0252 return ret;
0253
0254 copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq);
0255 idq.dqb_id = from_kqid(current_user_ns(), qid);
0256 if (copy_to_user(addr, &idq, sizeof(idq)))
0257 return -EFAULT;
0258 return 0;
0259 }
0260
0261 static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
0262 {
0263 dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
0264 dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
0265 dst->d_space = src->dqb_curspace;
0266 dst->d_ino_hardlimit = src->dqb_ihardlimit;
0267 dst->d_ino_softlimit = src->dqb_isoftlimit;
0268 dst->d_ino_count = src->dqb_curinodes;
0269 dst->d_spc_timer = src->dqb_btime;
0270 dst->d_ino_timer = src->dqb_itime;
0271
0272 dst->d_fieldmask = 0;
0273 if (src->dqb_valid & QIF_BLIMITS)
0274 dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
0275 if (src->dqb_valid & QIF_SPACE)
0276 dst->d_fieldmask |= QC_SPACE;
0277 if (src->dqb_valid & QIF_ILIMITS)
0278 dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
0279 if (src->dqb_valid & QIF_INODES)
0280 dst->d_fieldmask |= QC_INO_COUNT;
0281 if (src->dqb_valid & QIF_BTIME)
0282 dst->d_fieldmask |= QC_SPC_TIMER;
0283 if (src->dqb_valid & QIF_ITIME)
0284 dst->d_fieldmask |= QC_INO_TIMER;
0285 }
0286
0287 static int quota_setquota(struct super_block *sb, int type, qid_t id,
0288 void __user *addr)
0289 {
0290 struct qc_dqblk fdq;
0291 struct if_dqblk idq;
0292 struct kqid qid;
0293
0294 if (compat_need_64bit_alignment_fixup()) {
0295 struct compat_if_dqblk __user *compat_dqblk = addr;
0296
0297 if (copy_from_user(&idq, compat_dqblk, sizeof(*compat_dqblk)) ||
0298 get_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
0299 return -EFAULT;
0300 } else {
0301 if (copy_from_user(&idq, addr, sizeof(idq)))
0302 return -EFAULT;
0303 }
0304 if (!sb->s_qcop->set_dqblk)
0305 return -ENOSYS;
0306 qid = make_kqid(current_user_ns(), type, id);
0307 if (!qid_has_mapping(sb->s_user_ns, qid))
0308 return -EINVAL;
0309 copy_from_if_dqblk(&fdq, &idq);
0310 return sb->s_qcop->set_dqblk(sb, qid, &fdq);
0311 }
0312
0313 static int quota_enable(struct super_block *sb, void __user *addr)
0314 {
0315 __u32 flags;
0316
0317 if (copy_from_user(&flags, addr, sizeof(flags)))
0318 return -EFAULT;
0319 if (!sb->s_qcop->quota_enable)
0320 return -ENOSYS;
0321 return sb->s_qcop->quota_enable(sb, flags);
0322 }
0323
0324 static int quota_disable(struct super_block *sb, void __user *addr)
0325 {
0326 __u32 flags;
0327
0328 if (copy_from_user(&flags, addr, sizeof(flags)))
0329 return -EFAULT;
0330 if (!sb->s_qcop->quota_disable)
0331 return -ENOSYS;
0332 return sb->s_qcop->quota_disable(sb, flags);
0333 }
0334
0335 static int quota_state_to_flags(struct qc_state *state)
0336 {
0337 int flags = 0;
0338
0339 if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
0340 flags |= FS_QUOTA_UDQ_ACCT;
0341 if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
0342 flags |= FS_QUOTA_UDQ_ENFD;
0343 if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
0344 flags |= FS_QUOTA_GDQ_ACCT;
0345 if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
0346 flags |= FS_QUOTA_GDQ_ENFD;
0347 if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
0348 flags |= FS_QUOTA_PDQ_ACCT;
0349 if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
0350 flags |= FS_QUOTA_PDQ_ENFD;
0351 return flags;
0352 }
0353
0354 static int quota_getstate(struct super_block *sb, int type,
0355 struct fs_quota_stat *fqs)
0356 {
0357 struct qc_state state;
0358 int ret;
0359
0360 memset(&state, 0, sizeof (struct qc_state));
0361 ret = sb->s_qcop->get_state(sb, &state);
0362 if (ret < 0)
0363 return ret;
0364
0365 memset(fqs, 0, sizeof(*fqs));
0366 fqs->qs_version = FS_QSTAT_VERSION;
0367 fqs->qs_flags = quota_state_to_flags(&state);
0368
0369 if (!fqs->qs_flags)
0370 return -ENOSYS;
0371 fqs->qs_incoredqs = state.s_incoredqs;
0372
0373 fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
0374 fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
0375 fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
0376 fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
0377 fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
0378
0379
0380 if (state.s_state[USRQUOTA].ino) {
0381 fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
0382 fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
0383 fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
0384 }
0385 if (state.s_state[GRPQUOTA].ino) {
0386 fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
0387 fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
0388 fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
0389 }
0390 if (state.s_state[PRJQUOTA].ino) {
0391
0392
0393
0394
0395
0396 if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
0397 fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
0398 fqs->qs_gquota.qfs_nblks =
0399 state.s_state[PRJQUOTA].blocks;
0400 fqs->qs_gquota.qfs_nextents =
0401 state.s_state[PRJQUOTA].nextents;
0402 }
0403 }
0404 return 0;
0405 }
0406
0407 static int compat_copy_fs_qfilestat(struct compat_fs_qfilestat __user *to,
0408 struct fs_qfilestat *from)
0409 {
0410 if (copy_to_user(to, from, sizeof(*to)) ||
0411 put_user(from->qfs_nextents, &to->qfs_nextents))
0412 return -EFAULT;
0413 return 0;
0414 }
0415
0416 static int compat_copy_fs_quota_stat(struct compat_fs_quota_stat __user *to,
0417 struct fs_quota_stat *from)
0418 {
0419 if (put_user(from->qs_version, &to->qs_version) ||
0420 put_user(from->qs_flags, &to->qs_flags) ||
0421 put_user(from->qs_pad, &to->qs_pad) ||
0422 compat_copy_fs_qfilestat(&to->qs_uquota, &from->qs_uquota) ||
0423 compat_copy_fs_qfilestat(&to->qs_gquota, &from->qs_gquota) ||
0424 put_user(from->qs_incoredqs, &to->qs_incoredqs) ||
0425 put_user(from->qs_btimelimit, &to->qs_btimelimit) ||
0426 put_user(from->qs_itimelimit, &to->qs_itimelimit) ||
0427 put_user(from->qs_rtbtimelimit, &to->qs_rtbtimelimit) ||
0428 put_user(from->qs_bwarnlimit, &to->qs_bwarnlimit) ||
0429 put_user(from->qs_iwarnlimit, &to->qs_iwarnlimit))
0430 return -EFAULT;
0431 return 0;
0432 }
0433
0434 static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
0435 {
0436 struct fs_quota_stat fqs;
0437 int ret;
0438
0439 if (!sb->s_qcop->get_state)
0440 return -ENOSYS;
0441 ret = quota_getstate(sb, type, &fqs);
0442 if (ret)
0443 return ret;
0444
0445 if (compat_need_64bit_alignment_fixup())
0446 return compat_copy_fs_quota_stat(addr, &fqs);
0447 if (copy_to_user(addr, &fqs, sizeof(fqs)))
0448 return -EFAULT;
0449 return 0;
0450 }
0451
0452 static int quota_getstatev(struct super_block *sb, int type,
0453 struct fs_quota_statv *fqs)
0454 {
0455 struct qc_state state;
0456 int ret;
0457
0458 memset(&state, 0, sizeof (struct qc_state));
0459 ret = sb->s_qcop->get_state(sb, &state);
0460 if (ret < 0)
0461 return ret;
0462
0463 memset(fqs, 0, sizeof(*fqs));
0464 fqs->qs_version = FS_QSTAT_VERSION;
0465 fqs->qs_flags = quota_state_to_flags(&state);
0466
0467 if (!fqs->qs_flags)
0468 return -ENOSYS;
0469 fqs->qs_incoredqs = state.s_incoredqs;
0470
0471 fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
0472 fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
0473 fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
0474 fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
0475 fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
0476 fqs->qs_rtbwarnlimit = state.s_state[type].rt_spc_warnlimit;
0477
0478
0479 if (state.s_state[USRQUOTA].ino) {
0480 fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
0481 fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
0482 fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
0483 }
0484 if (state.s_state[GRPQUOTA].ino) {
0485 fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
0486 fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
0487 fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
0488 }
0489 if (state.s_state[PRJQUOTA].ino) {
0490 fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
0491 fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
0492 fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
0493 }
0494 return 0;
0495 }
0496
0497 static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
0498 {
0499 struct fs_quota_statv fqs;
0500 int ret;
0501
0502 if (!sb->s_qcop->get_state)
0503 return -ENOSYS;
0504
0505 memset(&fqs, 0, sizeof(fqs));
0506 if (copy_from_user(&fqs, addr, 1))
0507 return -EFAULT;
0508
0509
0510 switch (fqs.qs_version) {
0511 case FS_QSTATV_VERSION1:
0512 break;
0513 default:
0514 return -EINVAL;
0515 }
0516 ret = quota_getstatev(sb, type, &fqs);
0517 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
0518 return -EFAULT;
0519 return ret;
0520 }
0521
0522
0523
0524
0525
0526
0527 #define XFS_BB_SHIFT 9
0528
0529 static inline u64 quota_bbtob(u64 blocks)
0530 {
0531 return blocks << XFS_BB_SHIFT;
0532 }
0533
0534 static inline u64 quota_btobb(u64 bytes)
0535 {
0536 return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
0537 }
0538
0539 static inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d,
0540 __s32 timer, __s8 timer_hi)
0541 {
0542 if (d->d_fieldmask & FS_DQ_BIGTIME)
0543 return (u32)timer | (s64)timer_hi << 32;
0544 return timer;
0545 }
0546
0547 static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
0548 {
0549 dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
0550 dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
0551 dst->d_ino_hardlimit = src->d_ino_hardlimit;
0552 dst->d_ino_softlimit = src->d_ino_softlimit;
0553 dst->d_space = quota_bbtob(src->d_bcount);
0554 dst->d_ino_count = src->d_icount;
0555 dst->d_ino_timer = copy_from_xfs_dqblk_ts(src, src->d_itimer,
0556 src->d_itimer_hi);
0557 dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer,
0558 src->d_btimer_hi);
0559 dst->d_ino_warns = src->d_iwarns;
0560 dst->d_spc_warns = src->d_bwarns;
0561 dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
0562 dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
0563 dst->d_rt_space = quota_bbtob(src->d_rtbcount);
0564 dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer,
0565 src->d_rtbtimer_hi);
0566 dst->d_rt_spc_warns = src->d_rtbwarns;
0567 dst->d_fieldmask = 0;
0568 if (src->d_fieldmask & FS_DQ_ISOFT)
0569 dst->d_fieldmask |= QC_INO_SOFT;
0570 if (src->d_fieldmask & FS_DQ_IHARD)
0571 dst->d_fieldmask |= QC_INO_HARD;
0572 if (src->d_fieldmask & FS_DQ_BSOFT)
0573 dst->d_fieldmask |= QC_SPC_SOFT;
0574 if (src->d_fieldmask & FS_DQ_BHARD)
0575 dst->d_fieldmask |= QC_SPC_HARD;
0576 if (src->d_fieldmask & FS_DQ_RTBSOFT)
0577 dst->d_fieldmask |= QC_RT_SPC_SOFT;
0578 if (src->d_fieldmask & FS_DQ_RTBHARD)
0579 dst->d_fieldmask |= QC_RT_SPC_HARD;
0580 if (src->d_fieldmask & FS_DQ_BTIMER)
0581 dst->d_fieldmask |= QC_SPC_TIMER;
0582 if (src->d_fieldmask & FS_DQ_ITIMER)
0583 dst->d_fieldmask |= QC_INO_TIMER;
0584 if (src->d_fieldmask & FS_DQ_RTBTIMER)
0585 dst->d_fieldmask |= QC_RT_SPC_TIMER;
0586 if (src->d_fieldmask & FS_DQ_BWARNS)
0587 dst->d_fieldmask |= QC_SPC_WARNS;
0588 if (src->d_fieldmask & FS_DQ_IWARNS)
0589 dst->d_fieldmask |= QC_INO_WARNS;
0590 if (src->d_fieldmask & FS_DQ_RTBWARNS)
0591 dst->d_fieldmask |= QC_RT_SPC_WARNS;
0592 if (src->d_fieldmask & FS_DQ_BCOUNT)
0593 dst->d_fieldmask |= QC_SPACE;
0594 if (src->d_fieldmask & FS_DQ_ICOUNT)
0595 dst->d_fieldmask |= QC_INO_COUNT;
0596 if (src->d_fieldmask & FS_DQ_RTBCOUNT)
0597 dst->d_fieldmask |= QC_RT_SPACE;
0598 }
0599
0600 static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
0601 struct fs_disk_quota *src)
0602 {
0603 memset(dst, 0, sizeof(*dst));
0604 dst->i_spc_timelimit = src->d_btimer;
0605 dst->i_ino_timelimit = src->d_itimer;
0606 dst->i_rt_spc_timelimit = src->d_rtbtimer;
0607 dst->i_ino_warnlimit = src->d_iwarns;
0608 dst->i_spc_warnlimit = src->d_bwarns;
0609 dst->i_rt_spc_warnlimit = src->d_rtbwarns;
0610 if (src->d_fieldmask & FS_DQ_BWARNS)
0611 dst->i_fieldmask |= QC_SPC_WARNS;
0612 if (src->d_fieldmask & FS_DQ_IWARNS)
0613 dst->i_fieldmask |= QC_INO_WARNS;
0614 if (src->d_fieldmask & FS_DQ_RTBWARNS)
0615 dst->i_fieldmask |= QC_RT_SPC_WARNS;
0616 if (src->d_fieldmask & FS_DQ_BTIMER)
0617 dst->i_fieldmask |= QC_SPC_TIMER;
0618 if (src->d_fieldmask & FS_DQ_ITIMER)
0619 dst->i_fieldmask |= QC_INO_TIMER;
0620 if (src->d_fieldmask & FS_DQ_RTBTIMER)
0621 dst->i_fieldmask |= QC_RT_SPC_TIMER;
0622 }
0623
0624 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
0625 void __user *addr)
0626 {
0627 struct fs_disk_quota fdq;
0628 struct qc_dqblk qdq;
0629 struct kqid qid;
0630
0631 if (copy_from_user(&fdq, addr, sizeof(fdq)))
0632 return -EFAULT;
0633 if (!sb->s_qcop->set_dqblk)
0634 return -ENOSYS;
0635 qid = make_kqid(current_user_ns(), type, id);
0636 if (!qid_has_mapping(sb->s_user_ns, qid))
0637 return -EINVAL;
0638
0639 if (from_kqid(sb->s_user_ns, qid) == 0 &&
0640 fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
0641 struct qc_info qinfo;
0642 int ret;
0643
0644 if (!sb->s_qcop->set_info)
0645 return -EINVAL;
0646 copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
0647 ret = sb->s_qcop->set_info(sb, type, &qinfo);
0648 if (ret)
0649 return ret;
0650
0651 fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
0652 }
0653 copy_from_xfs_dqblk(&qdq, &fdq);
0654 return sb->s_qcop->set_dqblk(sb, qid, &qdq);
0655 }
0656
0657 static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d,
0658 __s32 *timer_lo, __s8 *timer_hi, s64 timer)
0659 {
0660 *timer_lo = timer;
0661 if (d->d_fieldmask & FS_DQ_BIGTIME)
0662 *timer_hi = timer >> 32;
0663 }
0664
0665 static inline bool want_bigtime(s64 timer)
0666 {
0667 return timer > S32_MAX || timer < S32_MIN;
0668 }
0669
0670 static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
0671 int type, qid_t id)
0672 {
0673 memset(dst, 0, sizeof(*dst));
0674 if (want_bigtime(src->d_ino_timer) || want_bigtime(src->d_spc_timer) ||
0675 want_bigtime(src->d_rt_spc_timer))
0676 dst->d_fieldmask |= FS_DQ_BIGTIME;
0677 dst->d_version = FS_DQUOT_VERSION;
0678 dst->d_id = id;
0679 if (type == USRQUOTA)
0680 dst->d_flags = FS_USER_QUOTA;
0681 else if (type == PRJQUOTA)
0682 dst->d_flags = FS_PROJ_QUOTA;
0683 else
0684 dst->d_flags = FS_GROUP_QUOTA;
0685 dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
0686 dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
0687 dst->d_ino_hardlimit = src->d_ino_hardlimit;
0688 dst->d_ino_softlimit = src->d_ino_softlimit;
0689 dst->d_bcount = quota_btobb(src->d_space);
0690 dst->d_icount = src->d_ino_count;
0691 copy_to_xfs_dqblk_ts(dst, &dst->d_itimer, &dst->d_itimer_hi,
0692 src->d_ino_timer);
0693 copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi,
0694 src->d_spc_timer);
0695 dst->d_iwarns = src->d_ino_warns;
0696 dst->d_bwarns = src->d_spc_warns;
0697 dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
0698 dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
0699 dst->d_rtbcount = quota_btobb(src->d_rt_space);
0700 copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi,
0701 src->d_rt_spc_timer);
0702 dst->d_rtbwarns = src->d_rt_spc_warns;
0703 }
0704
0705 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
0706 void __user *addr)
0707 {
0708 struct fs_disk_quota fdq;
0709 struct qc_dqblk qdq;
0710 struct kqid qid;
0711 int ret;
0712
0713 if (!sb->s_qcop->get_dqblk)
0714 return -ENOSYS;
0715 qid = make_kqid(current_user_ns(), type, id);
0716 if (!qid_has_mapping(sb->s_user_ns, qid))
0717 return -EINVAL;
0718 ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
0719 if (ret)
0720 return ret;
0721 copy_to_xfs_dqblk(&fdq, &qdq, type, id);
0722 if (copy_to_user(addr, &fdq, sizeof(fdq)))
0723 return -EFAULT;
0724 return ret;
0725 }
0726
0727
0728
0729
0730
0731 static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
0732 void __user *addr)
0733 {
0734 struct fs_disk_quota fdq;
0735 struct qc_dqblk qdq;
0736 struct kqid qid;
0737 qid_t id_out;
0738 int ret;
0739
0740 if (!sb->s_qcop->get_nextdqblk)
0741 return -ENOSYS;
0742 qid = make_kqid(current_user_ns(), type, id);
0743 if (!qid_has_mapping(sb->s_user_ns, qid))
0744 return -EINVAL;
0745 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
0746 if (ret)
0747 return ret;
0748 id_out = from_kqid(current_user_ns(), qid);
0749 copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
0750 if (copy_to_user(addr, &fdq, sizeof(fdq)))
0751 return -EFAULT;
0752 return ret;
0753 }
0754
0755 static int quota_rmxquota(struct super_block *sb, void __user *addr)
0756 {
0757 __u32 flags;
0758
0759 if (copy_from_user(&flags, addr, sizeof(flags)))
0760 return -EFAULT;
0761 if (!sb->s_qcop->rm_xquota)
0762 return -ENOSYS;
0763 return sb->s_qcop->rm_xquota(sb, flags);
0764 }
0765
0766
0767 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
0768 void __user *addr, const struct path *path)
0769 {
0770 int ret;
0771
0772 type = array_index_nospec(type, MAXQUOTAS);
0773
0774
0775
0776
0777 if (!sb->s_qcop)
0778 return -ENOSYS;
0779 if (!(sb->s_quota_types & (1 << type)))
0780 return -EINVAL;
0781
0782 ret = check_quotactl_permission(sb, type, cmd, id);
0783 if (ret < 0)
0784 return ret;
0785
0786 switch (cmd) {
0787 case Q_QUOTAON:
0788 return quota_quotaon(sb, type, id, path);
0789 case Q_QUOTAOFF:
0790 return quota_quotaoff(sb, type);
0791 case Q_GETFMT:
0792 return quota_getfmt(sb, type, addr);
0793 case Q_GETINFO:
0794 return quota_getinfo(sb, type, addr);
0795 case Q_SETINFO:
0796 return quota_setinfo(sb, type, addr);
0797 case Q_GETQUOTA:
0798 return quota_getquota(sb, type, id, addr);
0799 case Q_GETNEXTQUOTA:
0800 return quota_getnextquota(sb, type, id, addr);
0801 case Q_SETQUOTA:
0802 return quota_setquota(sb, type, id, addr);
0803 case Q_SYNC:
0804 if (!sb->s_qcop->quota_sync)
0805 return -ENOSYS;
0806 return sb->s_qcop->quota_sync(sb, type);
0807 case Q_XQUOTAON:
0808 return quota_enable(sb, addr);
0809 case Q_XQUOTAOFF:
0810 return quota_disable(sb, addr);
0811 case Q_XQUOTARM:
0812 return quota_rmxquota(sb, addr);
0813 case Q_XGETQSTAT:
0814 return quota_getxstate(sb, type, addr);
0815 case Q_XGETQSTATV:
0816 return quota_getxstatev(sb, type, addr);
0817 case Q_XSETQLIM:
0818 return quota_setxquota(sb, type, id, addr);
0819 case Q_XGETQUOTA:
0820 return quota_getxquota(sb, type, id, addr);
0821 case Q_XGETNEXTQUOTA:
0822 return quota_getnextxquota(sb, type, id, addr);
0823 case Q_XQUOTASYNC:
0824 if (sb_rdonly(sb))
0825 return -EROFS;
0826
0827 return 0;
0828 default:
0829 return -EINVAL;
0830 }
0831 }
0832
0833
0834 static int quotactl_cmd_write(int cmd)
0835 {
0836
0837
0838
0839
0840
0841 switch (cmd) {
0842 case Q_GETFMT:
0843 case Q_GETINFO:
0844 case Q_SYNC:
0845 case Q_XGETQSTAT:
0846 case Q_XGETQSTATV:
0847 case Q_XGETQUOTA:
0848 case Q_XGETNEXTQUOTA:
0849 case Q_XQUOTASYNC:
0850 return 0;
0851 }
0852 return 1;
0853 }
0854
0855
0856 static bool quotactl_cmd_onoff(int cmd)
0857 {
0858 return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
0859 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
0860 }
0861
0862
0863
0864
0865
0866 static struct super_block *quotactl_block(const char __user *special, int cmd)
0867 {
0868 #ifdef CONFIG_BLOCK
0869 struct super_block *sb;
0870 struct filename *tmp = getname(special);
0871 bool excl = false, thawed = false;
0872 int error;
0873 dev_t dev;
0874
0875 if (IS_ERR(tmp))
0876 return ERR_CAST(tmp);
0877 error = lookup_bdev(tmp->name, &dev);
0878 putname(tmp);
0879 if (error)
0880 return ERR_PTR(error);
0881
0882 if (quotactl_cmd_onoff(cmd)) {
0883 excl = true;
0884 thawed = true;
0885 } else if (quotactl_cmd_write(cmd)) {
0886 thawed = true;
0887 }
0888
0889 retry:
0890 sb = user_get_super(dev, excl);
0891 if (!sb)
0892 return ERR_PTR(-ENODEV);
0893 if (thawed && sb->s_writers.frozen != SB_UNFROZEN) {
0894 if (excl)
0895 up_write(&sb->s_umount);
0896 else
0897 up_read(&sb->s_umount);
0898 wait_event(sb->s_writers.wait_unfrozen,
0899 sb->s_writers.frozen == SB_UNFROZEN);
0900 put_super(sb);
0901 goto retry;
0902 }
0903 return sb;
0904
0905 #else
0906 return ERR_PTR(-ENODEV);
0907 #endif
0908 }
0909
0910
0911
0912
0913
0914
0915
0916 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
0917 qid_t, id, void __user *, addr)
0918 {
0919 uint cmds, type;
0920 struct super_block *sb = NULL;
0921 struct path path, *pathp = NULL;
0922 int ret;
0923
0924 cmds = cmd >> SUBCMDSHIFT;
0925 type = cmd & SUBCMDMASK;
0926
0927 if (type >= MAXQUOTAS)
0928 return -EINVAL;
0929
0930
0931
0932
0933
0934
0935 if (!special) {
0936 if (cmds == Q_SYNC)
0937 return quota_sync_all(type);
0938 return -ENODEV;
0939 }
0940
0941
0942
0943
0944
0945
0946 if (cmds == Q_QUOTAON) {
0947 ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
0948 if (ret)
0949 pathp = ERR_PTR(ret);
0950 else
0951 pathp = &path;
0952 }
0953
0954 sb = quotactl_block(special, cmds);
0955 if (IS_ERR(sb)) {
0956 ret = PTR_ERR(sb);
0957 goto out;
0958 }
0959
0960 ret = do_quotactl(sb, type, cmds, id, addr, pathp);
0961
0962 if (!quotactl_cmd_onoff(cmds))
0963 drop_super(sb);
0964 else
0965 drop_super_exclusive(sb);
0966 out:
0967 if (pathp && !IS_ERR(pathp))
0968 path_put(pathp);
0969 return ret;
0970 }
0971
0972 SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
0973 qid_t, id, void __user *, addr)
0974 {
0975 struct super_block *sb;
0976 unsigned int cmds = cmd >> SUBCMDSHIFT;
0977 unsigned int type = cmd & SUBCMDMASK;
0978 struct fd f;
0979 int ret;
0980
0981 f = fdget_raw(fd);
0982 if (!f.file)
0983 return -EBADF;
0984
0985 ret = -EINVAL;
0986 if (type >= MAXQUOTAS)
0987 goto out;
0988
0989 if (quotactl_cmd_write(cmds)) {
0990 ret = mnt_want_write(f.file->f_path.mnt);
0991 if (ret)
0992 goto out;
0993 }
0994
0995 sb = f.file->f_path.mnt->mnt_sb;
0996 if (quotactl_cmd_onoff(cmds))
0997 down_write(&sb->s_umount);
0998 else
0999 down_read(&sb->s_umount);
1000
1001 ret = do_quotactl(sb, type, cmds, id, addr, ERR_PTR(-EINVAL));
1002
1003 if (quotactl_cmd_onoff(cmds))
1004 up_write(&sb->s_umount);
1005 else
1006 up_read(&sb->s_umount);
1007
1008 if (quotactl_cmd_write(cmds))
1009 mnt_drop_write(f.file->f_path.mnt);
1010 out:
1011 fdput(f);
1012 return ret;
1013 }