Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Quota code necessary even when VFS quota support is not compiled |
| 4 | * into the kernel. The interesting stuff is over in dquot.c, here |
| 5 | * we have symbols for initial quotactl(2) handling, the sysctl(2) |
| 6 | * variables, etc - things needed even when quota support disabled. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/namei.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <asm/current.h> |
Jeff Liu | f3da931 | 2012-05-28 23:40:17 +0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/security.h> |
| 16 | #include <linux/syscalls.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Adrian Bunk | be586ba | 2005-11-07 00:59:35 -0800 | [diff] [blame] | 18 | #include <linux/quotaops.h> |
Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 19 | #include <linux/types.h> |
Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 20 | #include <linux/writeback.h> |
Jeremy Cline | 7b6924d | 2018-07-31 01:37:31 +0000 | [diff] [blame] | 21 | #include <linux/nospec.h> |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 22 | #include "compat.h" |
Christoph Hellwig | 60b4988 | 2020-11-16 15:21:18 +0100 | [diff] [blame] | 23 | #include "../internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 25 | static int check_quotactl_permission(struct super_block *sb, int type, int cmd, |
| 26 | qid_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 28 | switch (cmd) { |
| 29 | /* these commands do not require any special privilegues */ |
| 30 | case Q_GETFMT: |
| 31 | case Q_SYNC: |
| 32 | case Q_GETINFO: |
| 33 | case Q_XGETQSTAT: |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 34 | case Q_XGETQSTATV: |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 35 | case Q_XQUOTASYNC: |
| 36 | break; |
| 37 | /* allow to query information for dquots we "own" */ |
| 38 | case Q_GETQUOTA: |
| 39 | case Q_XGETQUOTA: |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 40 | if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) || |
| 41 | (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id)))) |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 42 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 43 | fallthrough; |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 44 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (!capable(CAP_SYS_ADMIN)) |
| 46 | return -EPERM; |
| 47 | } |
| 48 | |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 49 | return security_quotactl(cmd, type, id, sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 52 | static void quota_sync_one(struct super_block *sb, void *arg) |
| 53 | { |
Jan Kara | 2c5f648 | 2014-09-30 10:43:09 +0200 | [diff] [blame] | 54 | int type = *(int *)arg; |
| 55 | |
| 56 | if (sb->s_qcop && sb->s_qcop->quota_sync && |
| 57 | (sb->s_quota_types & (1 << type))) |
| 58 | sb->s_qcop->quota_sync(sb, type); |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 61 | static int quota_sync_all(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 63 | int ret; |
| 64 | |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 65 | ret = security_quotactl(Q_SYNC, type, 0, NULL); |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 66 | if (!ret) |
| 67 | iterate_supers(quota_sync_one, &type); |
| 68 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jan Kara | d3b8632 | 2014-10-08 16:07:12 +0200 | [diff] [blame] | 71 | unsigned int qtype_enforce_flag(int type) |
| 72 | { |
| 73 | switch (type) { |
| 74 | case USRQUOTA: |
| 75 | return FS_QUOTA_UDQ_ENFD; |
| 76 | case GRPQUOTA: |
| 77 | return FS_QUOTA_GDQ_ENFD; |
| 78 | case PRJQUOTA: |
| 79 | return FS_QUOTA_PDQ_ENFD; |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
Eric Sandeen | 3218a3e | 2016-02-08 11:21:24 +1100 | [diff] [blame] | 84 | static int quota_quotaon(struct super_block *sb, int type, qid_t id, |
Al Viro | 8c54ca9 | 2016-11-20 19:49:34 -0500 | [diff] [blame] | 85 | const struct path *path) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 86 | { |
Jan Kara | aaa3dae | 2014-10-08 18:35:31 +0200 | [diff] [blame] | 87 | if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable) |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 88 | return -ENOSYS; |
Jan Kara | d3b8632 | 2014-10-08 16:07:12 +0200 | [diff] [blame] | 89 | if (sb->s_qcop->quota_enable) |
| 90 | return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type)); |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 91 | if (IS_ERR(path)) |
| 92 | return PTR_ERR(path); |
| 93 | return sb->s_qcop->quota_on(sb, type, id, path); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Jan Kara | d3b8632 | 2014-10-08 16:07:12 +0200 | [diff] [blame] | 96 | static int quota_quotaoff(struct super_block *sb, int type) |
| 97 | { |
| 98 | if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable) |
| 99 | return -ENOSYS; |
| 100 | if (sb->s_qcop->quota_disable) |
| 101 | return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type)); |
| 102 | return sb->s_qcop->quota_off(sb, type); |
| 103 | } |
| 104 | |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 105 | static int quota_getfmt(struct super_block *sb, int type, void __user *addr) |
| 106 | { |
| 107 | __u32 fmt; |
| 108 | |
Jan Kara | 9d1ccbe | 2016-11-23 13:35:14 +0100 | [diff] [blame] | 109 | if (!sb_has_quota_active(sb, type)) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 110 | return -ESRCH; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 111 | fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 112 | if (copy_to_user(addr, &fmt, sizeof(fmt))) |
| 113 | return -EFAULT; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int quota_getinfo(struct super_block *sb, int type, void __user *addr) |
| 118 | { |
Jan Kara | 0a240339 | 2014-11-19 00:42:09 +0100 | [diff] [blame] | 119 | struct qc_state state; |
| 120 | struct qc_type_state *tstate; |
| 121 | struct if_dqinfo uinfo; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 122 | int ret; |
| 123 | |
Jan Kara | 0a240339 | 2014-11-19 00:42:09 +0100 | [diff] [blame] | 124 | if (!sb->s_qcop->get_state) |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 125 | return -ENOSYS; |
Jan Kara | 0a240339 | 2014-11-19 00:42:09 +0100 | [diff] [blame] | 126 | ret = sb->s_qcop->get_state(sb, &state); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | tstate = state.s_state + type; |
| 130 | if (!(tstate->flags & QCI_ACCT_ENABLED)) |
| 131 | return -ESRCH; |
| 132 | memset(&uinfo, 0, sizeof(uinfo)); |
| 133 | uinfo.dqi_bgrace = tstate->spc_timelimit; |
| 134 | uinfo.dqi_igrace = tstate->ino_timelimit; |
| 135 | if (tstate->flags & QCI_SYSFILE) |
| 136 | uinfo.dqi_flags |= DQF_SYS_FILE; |
| 137 | if (tstate->flags & QCI_ROOT_SQUASH) |
| 138 | uinfo.dqi_flags |= DQF_ROOT_SQUASH; |
| 139 | uinfo.dqi_valid = IIF_ALL; |
Dan Carpenter | 72d4d0e | 2015-08-11 00:29:55 +0300 | [diff] [blame] | 140 | if (copy_to_user(addr, &uinfo, sizeof(uinfo))) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 141 | return -EFAULT; |
Dan Carpenter | 72d4d0e | 2015-08-11 00:29:55 +0300 | [diff] [blame] | 142 | return 0; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int quota_setinfo(struct super_block *sb, int type, void __user *addr) |
| 146 | { |
| 147 | struct if_dqinfo info; |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 148 | struct qc_info qinfo; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 149 | |
| 150 | if (copy_from_user(&info, addr, sizeof(info))) |
| 151 | return -EFAULT; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 152 | if (!sb->s_qcop->set_info) |
| 153 | return -ENOSYS; |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 154 | if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE)) |
| 155 | return -EINVAL; |
| 156 | memset(&qinfo, 0, sizeof(qinfo)); |
| 157 | if (info.dqi_valid & IIF_FLAGS) { |
| 158 | if (info.dqi_flags & ~DQF_SETINFO_MASK) |
| 159 | return -EINVAL; |
| 160 | if (info.dqi_flags & DQF_ROOT_SQUASH) |
| 161 | qinfo.i_flags |= QCI_ROOT_SQUASH; |
| 162 | qinfo.i_fieldmask |= QC_FLAGS; |
| 163 | } |
| 164 | if (info.dqi_valid & IIF_BGRACE) { |
| 165 | qinfo.i_spc_timelimit = info.dqi_bgrace; |
| 166 | qinfo.i_fieldmask |= QC_SPC_TIMER; |
| 167 | } |
| 168 | if (info.dqi_valid & IIF_IGRACE) { |
| 169 | qinfo.i_ino_timelimit = info.dqi_igrace; |
| 170 | qinfo.i_fieldmask |= QC_INO_TIMER; |
| 171 | } |
| 172 | return sb->s_qcop->set_info(sb, type, &qinfo); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 175 | static inline qsize_t qbtos(qsize_t blocks) |
| 176 | { |
| 177 | return blocks << QIF_DQBLKSIZE_BITS; |
| 178 | } |
| 179 | |
| 180 | static inline qsize_t stoqb(qsize_t space) |
| 181 | { |
| 182 | return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS; |
| 183 | } |
| 184 | |
| 185 | static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src) |
Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 186 | { |
Dan Carpenter | 18da65e | 2013-11-01 13:21:54 +0300 | [diff] [blame] | 187 | memset(dst, 0, sizeof(*dst)); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 188 | dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit); |
| 189 | dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit); |
| 190 | dst->dqb_curspace = src->d_space; |
Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 191 | dst->dqb_ihardlimit = src->d_ino_hardlimit; |
| 192 | dst->dqb_isoftlimit = src->d_ino_softlimit; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 193 | dst->dqb_curinodes = src->d_ino_count; |
| 194 | dst->dqb_btime = src->d_spc_timer; |
| 195 | dst->dqb_itime = src->d_ino_timer; |
Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 196 | dst->dqb_valid = QIF_ALL; |
| 197 | } |
| 198 | |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 199 | static int quota_getquota(struct super_block *sb, int type, qid_t id, |
| 200 | void __user *addr) |
| 201 | { |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 202 | struct kqid qid; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 203 | struct qc_dqblk fdq; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 204 | struct if_dqblk idq; |
| 205 | int ret; |
| 206 | |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 207 | if (!sb->s_qcop->get_dqblk) |
| 208 | return -ENOSYS; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 209 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 210 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | ret = sb->s_qcop->get_dqblk(sb, qid, &fdq); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 213 | if (ret) |
| 214 | return ret; |
Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 215 | copy_to_if_dqblk(&idq, &fdq); |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 216 | |
| 217 | if (compat_need_64bit_alignment_fixup()) { |
| 218 | struct compat_if_dqblk __user *compat_dqblk = addr; |
| 219 | |
| 220 | if (copy_to_user(compat_dqblk, &idq, sizeof(*compat_dqblk))) |
| 221 | return -EFAULT; |
| 222 | if (put_user(idq.dqb_valid, &compat_dqblk->dqb_valid)) |
| 223 | return -EFAULT; |
| 224 | } else { |
| 225 | if (copy_to_user(addr, &idq, sizeof(idq))) |
| 226 | return -EFAULT; |
| 227 | } |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Eric Sandeen | 926132c | 2016-02-08 11:22:21 +1100 | [diff] [blame] | 231 | /* |
| 232 | * Return quota for next active quota >= this id, if any exists, |
Eric Sandeen | ba58148 | 2016-01-22 12:25:32 -0600 | [diff] [blame] | 233 | * otherwise return -ENOENT via ->get_nextdqblk |
Eric Sandeen | 926132c | 2016-02-08 11:22:21 +1100 | [diff] [blame] | 234 | */ |
| 235 | static int quota_getnextquota(struct super_block *sb, int type, qid_t id, |
| 236 | void __user *addr) |
| 237 | { |
| 238 | struct kqid qid; |
| 239 | struct qc_dqblk fdq; |
| 240 | struct if_nextdqblk idq; |
| 241 | int ret; |
| 242 | |
| 243 | if (!sb->s_qcop->get_nextdqblk) |
| 244 | return -ENOSYS; |
| 245 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 246 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric Sandeen | 926132c | 2016-02-08 11:22:21 +1100 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | /* struct if_nextdqblk is a superset of struct if_dqblk */ |
| 252 | copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq); |
| 253 | idq.dqb_id = from_kqid(current_user_ns(), qid); |
| 254 | if (copy_to_user(addr, &idq, sizeof(idq))) |
| 255 | return -EFAULT; |
| 256 | return 0; |
| 257 | } |
| 258 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 259 | static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src) |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 260 | { |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 261 | dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit); |
| 262 | dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit); |
| 263 | dst->d_space = src->dqb_curspace; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 264 | dst->d_ino_hardlimit = src->dqb_ihardlimit; |
| 265 | dst->d_ino_softlimit = src->dqb_isoftlimit; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 266 | dst->d_ino_count = src->dqb_curinodes; |
| 267 | dst->d_spc_timer = src->dqb_btime; |
| 268 | dst->d_ino_timer = src->dqb_itime; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 269 | |
| 270 | dst->d_fieldmask = 0; |
| 271 | if (src->dqb_valid & QIF_BLIMITS) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 272 | dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 273 | if (src->dqb_valid & QIF_SPACE) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 274 | dst->d_fieldmask |= QC_SPACE; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 275 | if (src->dqb_valid & QIF_ILIMITS) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 276 | dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 277 | if (src->dqb_valid & QIF_INODES) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 278 | dst->d_fieldmask |= QC_INO_COUNT; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 279 | if (src->dqb_valid & QIF_BTIME) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 280 | dst->d_fieldmask |= QC_SPC_TIMER; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 281 | if (src->dqb_valid & QIF_ITIME) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 282 | dst->d_fieldmask |= QC_INO_TIMER; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 285 | static int quota_setquota(struct super_block *sb, int type, qid_t id, |
| 286 | void __user *addr) |
| 287 | { |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 288 | struct qc_dqblk fdq; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 289 | struct if_dqblk idq; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 290 | struct kqid qid; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 291 | |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 292 | if (compat_need_64bit_alignment_fixup()) { |
| 293 | struct compat_if_dqblk __user *compat_dqblk = addr; |
| 294 | |
| 295 | if (copy_from_user(&idq, compat_dqblk, sizeof(*compat_dqblk)) || |
| 296 | get_user(idq.dqb_valid, &compat_dqblk->dqb_valid)) |
| 297 | return -EFAULT; |
| 298 | } else { |
| 299 | if (copy_from_user(&idq, addr, sizeof(idq))) |
| 300 | return -EFAULT; |
| 301 | } |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 302 | if (!sb->s_qcop->set_dqblk) |
| 303 | return -ENOSYS; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 304 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 305 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 306 | return -EINVAL; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 307 | copy_from_if_dqblk(&fdq, &idq); |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 308 | return sb->s_qcop->set_dqblk(sb, qid, &fdq); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 311 | static int quota_enable(struct super_block *sb, void __user *addr) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 312 | { |
| 313 | __u32 flags; |
| 314 | |
| 315 | if (copy_from_user(&flags, addr, sizeof(flags))) |
| 316 | return -EFAULT; |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 317 | if (!sb->s_qcop->quota_enable) |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 318 | return -ENOSYS; |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 319 | return sb->s_qcop->quota_enable(sb, flags); |
| 320 | } |
| 321 | |
| 322 | static int quota_disable(struct super_block *sb, void __user *addr) |
| 323 | { |
| 324 | __u32 flags; |
| 325 | |
| 326 | if (copy_from_user(&flags, addr, sizeof(flags))) |
| 327 | return -EFAULT; |
| 328 | if (!sb->s_qcop->quota_disable) |
| 329 | return -ENOSYS; |
| 330 | return sb->s_qcop->quota_disable(sb, flags); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 333 | static int quota_state_to_flags(struct qc_state *state) |
| 334 | { |
| 335 | int flags = 0; |
| 336 | |
| 337 | if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) |
| 338 | flags |= FS_QUOTA_UDQ_ACCT; |
| 339 | if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED) |
| 340 | flags |= FS_QUOTA_UDQ_ENFD; |
| 341 | if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) |
| 342 | flags |= FS_QUOTA_GDQ_ACCT; |
| 343 | if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED) |
| 344 | flags |= FS_QUOTA_GDQ_ENFD; |
| 345 | if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) |
| 346 | flags |= FS_QUOTA_PDQ_ACCT; |
| 347 | if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED) |
| 348 | flags |= FS_QUOTA_PDQ_ENFD; |
| 349 | return flags; |
| 350 | } |
| 351 | |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 352 | static int quota_getstate(struct super_block *sb, int type, |
| 353 | struct fs_quota_stat *fqs) |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 354 | { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 355 | struct qc_state state; |
| 356 | int ret; |
| 357 | |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 358 | memset(&state, 0, sizeof (struct qc_state)); |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 359 | ret = sb->s_qcop->get_state(sb, &state); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
| 363 | memset(fqs, 0, sizeof(*fqs)); |
| 364 | fqs->qs_version = FS_QSTAT_VERSION; |
| 365 | fqs->qs_flags = quota_state_to_flags(&state); |
| 366 | /* No quota enabled? */ |
| 367 | if (!fqs->qs_flags) |
| 368 | return -ENOSYS; |
| 369 | fqs->qs_incoredqs = state.s_incoredqs; |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 370 | |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 371 | fqs->qs_btimelimit = state.s_state[type].spc_timelimit; |
| 372 | fqs->qs_itimelimit = state.s_state[type].ino_timelimit; |
| 373 | fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; |
| 374 | fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; |
| 375 | fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 376 | |
| 377 | /* Inodes may be allocated even if inactive; copy out if present */ |
| 378 | if (state.s_state[USRQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 379 | fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; |
| 380 | fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; |
| 381 | fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; |
| 382 | } |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 383 | if (state.s_state[GRPQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 384 | fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; |
| 385 | fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; |
| 386 | fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; |
| 387 | } |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 388 | if (state.s_state[PRJQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 389 | /* |
| 390 | * Q_XGETQSTAT doesn't have room for both group and project |
| 391 | * quotas. So, allow the project quota values to be copied out |
| 392 | * only if there is no group quota information available. |
| 393 | */ |
| 394 | if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) { |
| 395 | fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino; |
| 396 | fqs->qs_gquota.qfs_nblks = |
| 397 | state.s_state[PRJQUOTA].blocks; |
| 398 | fqs->qs_gquota.qfs_nextents = |
| 399 | state.s_state[PRJQUOTA].nextents; |
| 400 | } |
| 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 405 | static int compat_copy_fs_qfilestat(struct compat_fs_qfilestat __user *to, |
| 406 | struct fs_qfilestat *from) |
| 407 | { |
| 408 | if (copy_to_user(to, from, sizeof(*to)) || |
| 409 | put_user(from->qfs_nextents, &to->qfs_nextents)) |
| 410 | return -EFAULT; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int compat_copy_fs_quota_stat(struct compat_fs_quota_stat __user *to, |
| 415 | struct fs_quota_stat *from) |
| 416 | { |
| 417 | if (put_user(from->qs_version, &to->qs_version) || |
| 418 | put_user(from->qs_flags, &to->qs_flags) || |
| 419 | put_user(from->qs_pad, &to->qs_pad) || |
| 420 | compat_copy_fs_qfilestat(&to->qs_uquota, &from->qs_uquota) || |
| 421 | compat_copy_fs_qfilestat(&to->qs_gquota, &from->qs_gquota) || |
| 422 | put_user(from->qs_incoredqs, &to->qs_incoredqs) || |
| 423 | put_user(from->qs_btimelimit, &to->qs_btimelimit) || |
| 424 | put_user(from->qs_itimelimit, &to->qs_itimelimit) || |
| 425 | put_user(from->qs_rtbtimelimit, &to->qs_rtbtimelimit) || |
| 426 | put_user(from->qs_bwarnlimit, &to->qs_bwarnlimit) || |
| 427 | put_user(from->qs_iwarnlimit, &to->qs_iwarnlimit)) |
| 428 | return -EFAULT; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 432 | static int quota_getxstate(struct super_block *sb, int type, void __user *addr) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 433 | { |
| 434 | struct fs_quota_stat fqs; |
| 435 | int ret; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 436 | |
Jan Kara | 59b6ba6 | 2014-11-19 16:44:58 +0100 | [diff] [blame] | 437 | if (!sb->s_qcop->get_state) |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 438 | return -ENOSYS; |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 439 | ret = quota_getstate(sb, type, &fqs); |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 440 | if (ret) |
| 441 | return ret; |
| 442 | |
| 443 | if (compat_need_64bit_alignment_fixup()) |
| 444 | return compat_copy_fs_quota_stat(addr, &fqs); |
| 445 | if (copy_to_user(addr, &fqs, sizeof(fqs))) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 446 | return -EFAULT; |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 447 | return 0; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 450 | static int quota_getstatev(struct super_block *sb, int type, |
| 451 | struct fs_quota_statv *fqs) |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 452 | { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 453 | struct qc_state state; |
| 454 | int ret; |
| 455 | |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 456 | memset(&state, 0, sizeof (struct qc_state)); |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 457 | ret = sb->s_qcop->get_state(sb, &state); |
| 458 | if (ret < 0) |
| 459 | return ret; |
| 460 | |
| 461 | memset(fqs, 0, sizeof(*fqs)); |
| 462 | fqs->qs_version = FS_QSTAT_VERSION; |
| 463 | fqs->qs_flags = quota_state_to_flags(&state); |
| 464 | /* No quota enabled? */ |
| 465 | if (!fqs->qs_flags) |
| 466 | return -ENOSYS; |
| 467 | fqs->qs_incoredqs = state.s_incoredqs; |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 468 | |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 469 | fqs->qs_btimelimit = state.s_state[type].spc_timelimit; |
| 470 | fqs->qs_itimelimit = state.s_state[type].ino_timelimit; |
| 471 | fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit; |
| 472 | fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit; |
| 473 | fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit; |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 474 | |
| 475 | /* Inodes may be allocated even if inactive; copy out if present */ |
| 476 | if (state.s_state[USRQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 477 | fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino; |
| 478 | fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks; |
| 479 | fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents; |
| 480 | } |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 481 | if (state.s_state[GRPQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 482 | fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino; |
| 483 | fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks; |
| 484 | fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents; |
| 485 | } |
Eric Sandeen | 3cd0126 | 2016-08-12 17:40:09 -0500 | [diff] [blame] | 486 | if (state.s_state[PRJQUOTA].ino) { |
Jan Kara | bc230e4 | 2014-11-19 16:17:45 +0100 | [diff] [blame] | 487 | fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino; |
| 488 | fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks; |
| 489 | fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents; |
| 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 494 | static int quota_getxstatev(struct super_block *sb, int type, void __user *addr) |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 495 | { |
| 496 | struct fs_quota_statv fqs; |
| 497 | int ret; |
| 498 | |
Jan Kara | 59b6ba6 | 2014-11-19 16:44:58 +0100 | [diff] [blame] | 499 | if (!sb->s_qcop->get_state) |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 500 | return -ENOSYS; |
| 501 | |
| 502 | memset(&fqs, 0, sizeof(fqs)); |
| 503 | if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */ |
| 504 | return -EFAULT; |
| 505 | |
| 506 | /* If this kernel doesn't support user specified version, fail */ |
| 507 | switch (fqs.qs_version) { |
| 508 | case FS_QSTATV_VERSION1: |
| 509 | break; |
| 510 | default: |
| 511 | return -EINVAL; |
| 512 | } |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 513 | ret = quota_getstatev(sb, type, &fqs); |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 514 | if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) |
| 515 | return -EFAULT; |
| 516 | return ret; |
| 517 | } |
| 518 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 519 | /* |
| 520 | * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them |
| 521 | * out of there as xfsprogs rely on definitions being in that header file. So |
| 522 | * just define same functions here for quota purposes. |
| 523 | */ |
| 524 | #define XFS_BB_SHIFT 9 |
| 525 | |
| 526 | static inline u64 quota_bbtob(u64 blocks) |
| 527 | { |
| 528 | return blocks << XFS_BB_SHIFT; |
| 529 | } |
| 530 | |
| 531 | static inline u64 quota_btobb(u64 bytes) |
| 532 | { |
| 533 | return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT; |
| 534 | } |
| 535 | |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 536 | static inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d, |
| 537 | __s32 timer, __s8 timer_hi) |
| 538 | { |
| 539 | if (d->d_fieldmask & FS_DQ_BIGTIME) |
| 540 | return (u32)timer | (s64)timer_hi << 32; |
| 541 | return timer; |
| 542 | } |
| 543 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 544 | static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src) |
| 545 | { |
| 546 | dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit); |
| 547 | dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit); |
| 548 | dst->d_ino_hardlimit = src->d_ino_hardlimit; |
| 549 | dst->d_ino_softlimit = src->d_ino_softlimit; |
| 550 | dst->d_space = quota_bbtob(src->d_bcount); |
| 551 | dst->d_ino_count = src->d_icount; |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 552 | dst->d_ino_timer = copy_from_xfs_dqblk_ts(src, src->d_itimer, |
| 553 | src->d_itimer_hi); |
| 554 | dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer, |
| 555 | src->d_btimer_hi); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 556 | dst->d_ino_warns = src->d_iwarns; |
| 557 | dst->d_spc_warns = src->d_bwarns; |
| 558 | dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit); |
| 559 | dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit); |
| 560 | dst->d_rt_space = quota_bbtob(src->d_rtbcount); |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 561 | dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer, |
| 562 | src->d_rtbtimer_hi); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 563 | dst->d_rt_spc_warns = src->d_rtbwarns; |
| 564 | dst->d_fieldmask = 0; |
| 565 | if (src->d_fieldmask & FS_DQ_ISOFT) |
| 566 | dst->d_fieldmask |= QC_INO_SOFT; |
| 567 | if (src->d_fieldmask & FS_DQ_IHARD) |
| 568 | dst->d_fieldmask |= QC_INO_HARD; |
| 569 | if (src->d_fieldmask & FS_DQ_BSOFT) |
| 570 | dst->d_fieldmask |= QC_SPC_SOFT; |
| 571 | if (src->d_fieldmask & FS_DQ_BHARD) |
| 572 | dst->d_fieldmask |= QC_SPC_HARD; |
| 573 | if (src->d_fieldmask & FS_DQ_RTBSOFT) |
| 574 | dst->d_fieldmask |= QC_RT_SPC_SOFT; |
| 575 | if (src->d_fieldmask & FS_DQ_RTBHARD) |
| 576 | dst->d_fieldmask |= QC_RT_SPC_HARD; |
| 577 | if (src->d_fieldmask & FS_DQ_BTIMER) |
| 578 | dst->d_fieldmask |= QC_SPC_TIMER; |
| 579 | if (src->d_fieldmask & FS_DQ_ITIMER) |
| 580 | dst->d_fieldmask |= QC_INO_TIMER; |
| 581 | if (src->d_fieldmask & FS_DQ_RTBTIMER) |
| 582 | dst->d_fieldmask |= QC_RT_SPC_TIMER; |
| 583 | if (src->d_fieldmask & FS_DQ_BWARNS) |
| 584 | dst->d_fieldmask |= QC_SPC_WARNS; |
| 585 | if (src->d_fieldmask & FS_DQ_IWARNS) |
| 586 | dst->d_fieldmask |= QC_INO_WARNS; |
| 587 | if (src->d_fieldmask & FS_DQ_RTBWARNS) |
| 588 | dst->d_fieldmask |= QC_RT_SPC_WARNS; |
| 589 | if (src->d_fieldmask & FS_DQ_BCOUNT) |
| 590 | dst->d_fieldmask |= QC_SPACE; |
| 591 | if (src->d_fieldmask & FS_DQ_ICOUNT) |
| 592 | dst->d_fieldmask |= QC_INO_COUNT; |
| 593 | if (src->d_fieldmask & FS_DQ_RTBCOUNT) |
| 594 | dst->d_fieldmask |= QC_RT_SPACE; |
| 595 | } |
| 596 | |
Jan Kara | c39fb53 | 2014-12-16 16:12:27 +0100 | [diff] [blame] | 597 | static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst, |
| 598 | struct fs_disk_quota *src) |
| 599 | { |
| 600 | memset(dst, 0, sizeof(*dst)); |
| 601 | dst->i_spc_timelimit = src->d_btimer; |
| 602 | dst->i_ino_timelimit = src->d_itimer; |
| 603 | dst->i_rt_spc_timelimit = src->d_rtbtimer; |
| 604 | dst->i_ino_warnlimit = src->d_iwarns; |
| 605 | dst->i_spc_warnlimit = src->d_bwarns; |
| 606 | dst->i_rt_spc_warnlimit = src->d_rtbwarns; |
| 607 | if (src->d_fieldmask & FS_DQ_BWARNS) |
| 608 | dst->i_fieldmask |= QC_SPC_WARNS; |
| 609 | if (src->d_fieldmask & FS_DQ_IWARNS) |
| 610 | dst->i_fieldmask |= QC_INO_WARNS; |
| 611 | if (src->d_fieldmask & FS_DQ_RTBWARNS) |
| 612 | dst->i_fieldmask |= QC_RT_SPC_WARNS; |
| 613 | if (src->d_fieldmask & FS_DQ_BTIMER) |
| 614 | dst->i_fieldmask |= QC_SPC_TIMER; |
| 615 | if (src->d_fieldmask & FS_DQ_ITIMER) |
| 616 | dst->i_fieldmask |= QC_INO_TIMER; |
| 617 | if (src->d_fieldmask & FS_DQ_RTBTIMER) |
| 618 | dst->i_fieldmask |= QC_RT_SPC_TIMER; |
| 619 | } |
| 620 | |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 621 | static int quota_setxquota(struct super_block *sb, int type, qid_t id, |
| 622 | void __user *addr) |
| 623 | { |
| 624 | struct fs_disk_quota fdq; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 625 | struct qc_dqblk qdq; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 626 | struct kqid qid; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 627 | |
| 628 | if (copy_from_user(&fdq, addr, sizeof(fdq))) |
| 629 | return -EFAULT; |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 630 | if (!sb->s_qcop->set_dqblk) |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 631 | return -ENOSYS; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 632 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 633 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 634 | return -EINVAL; |
Jan Kara | c39fb53 | 2014-12-16 16:12:27 +0100 | [diff] [blame] | 635 | /* Are we actually setting timer / warning limits for all users? */ |
Eric W. Biederman | cfd4c70 | 2016-07-05 11:10:57 -0500 | [diff] [blame] | 636 | if (from_kqid(sb->s_user_ns, qid) == 0 && |
Jan Kara | c39fb53 | 2014-12-16 16:12:27 +0100 | [diff] [blame] | 637 | fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) { |
| 638 | struct qc_info qinfo; |
| 639 | int ret; |
| 640 | |
| 641 | if (!sb->s_qcop->set_info) |
| 642 | return -EINVAL; |
| 643 | copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq); |
| 644 | ret = sb->s_qcop->set_info(sb, type, &qinfo); |
| 645 | if (ret) |
| 646 | return ret; |
| 647 | /* These are already done */ |
| 648 | fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK); |
| 649 | } |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 650 | copy_from_xfs_dqblk(&qdq, &fdq); |
| 651 | return sb->s_qcop->set_dqblk(sb, qid, &qdq); |
| 652 | } |
| 653 | |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 654 | static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d, |
| 655 | __s32 *timer_lo, __s8 *timer_hi, s64 timer) |
| 656 | { |
| 657 | *timer_lo = timer; |
| 658 | if (d->d_fieldmask & FS_DQ_BIGTIME) |
| 659 | *timer_hi = timer >> 32; |
| 660 | } |
| 661 | |
| 662 | static inline bool want_bigtime(s64 timer) |
| 663 | { |
| 664 | return timer > S32_MAX || timer < S32_MIN; |
| 665 | } |
| 666 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 667 | static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src, |
| 668 | int type, qid_t id) |
| 669 | { |
| 670 | memset(dst, 0, sizeof(*dst)); |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 671 | if (want_bigtime(src->d_ino_timer) || want_bigtime(src->d_spc_timer) || |
| 672 | want_bigtime(src->d_rt_spc_timer)) |
| 673 | dst->d_fieldmask |= FS_DQ_BIGTIME; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 674 | dst->d_version = FS_DQUOT_VERSION; |
| 675 | dst->d_id = id; |
| 676 | if (type == USRQUOTA) |
| 677 | dst->d_flags = FS_USER_QUOTA; |
| 678 | else if (type == PRJQUOTA) |
| 679 | dst->d_flags = FS_PROJ_QUOTA; |
| 680 | else |
| 681 | dst->d_flags = FS_GROUP_QUOTA; |
| 682 | dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit); |
| 683 | dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit); |
| 684 | dst->d_ino_hardlimit = src->d_ino_hardlimit; |
| 685 | dst->d_ino_softlimit = src->d_ino_softlimit; |
| 686 | dst->d_bcount = quota_btobb(src->d_space); |
| 687 | dst->d_icount = src->d_ino_count; |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 688 | copy_to_xfs_dqblk_ts(dst, &dst->d_itimer, &dst->d_itimer_hi, |
| 689 | src->d_ino_timer); |
| 690 | copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi, |
| 691 | src->d_spc_timer); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 692 | dst->d_iwarns = src->d_ino_warns; |
| 693 | dst->d_bwarns = src->d_spc_warns; |
| 694 | dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit); |
| 695 | dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit); |
| 696 | dst->d_rtbcount = quota_btobb(src->d_rt_space); |
Darrick J. Wong | ad47ff33 | 2020-09-09 09:34:13 -0700 | [diff] [blame] | 697 | copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi, |
| 698 | src->d_rt_spc_timer); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 699 | dst->d_rtbwarns = src->d_rt_spc_warns; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static int quota_getxquota(struct super_block *sb, int type, qid_t id, |
| 703 | void __user *addr) |
| 704 | { |
| 705 | struct fs_disk_quota fdq; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 706 | struct qc_dqblk qdq; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 707 | struct kqid qid; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 708 | int ret; |
| 709 | |
Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 710 | if (!sb->s_qcop->get_dqblk) |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 711 | return -ENOSYS; |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 712 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 713 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric W. Biederman | 74a8a10 | 2012-09-16 02:07:49 -0700 | [diff] [blame] | 714 | return -EINVAL; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 715 | ret = sb->s_qcop->get_dqblk(sb, qid, &qdq); |
| 716 | if (ret) |
| 717 | return ret; |
| 718 | copy_to_xfs_dqblk(&fdq, &qdq, type, id); |
| 719 | if (copy_to_user(addr, &fdq, sizeof(fdq))) |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 720 | return -EFAULT; |
| 721 | return ret; |
| 722 | } |
| 723 | |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 724 | /* |
| 725 | * Return quota for next active quota >= this id, if any exists, |
Eric Sandeen | ba58148 | 2016-01-22 12:25:32 -0600 | [diff] [blame] | 726 | * otherwise return -ENOENT via ->get_nextdqblk. |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 727 | */ |
| 728 | static int quota_getnextxquota(struct super_block *sb, int type, qid_t id, |
| 729 | void __user *addr) |
| 730 | { |
| 731 | struct fs_disk_quota fdq; |
| 732 | struct qc_dqblk qdq; |
| 733 | struct kqid qid; |
| 734 | qid_t id_out; |
| 735 | int ret; |
| 736 | |
| 737 | if (!sb->s_qcop->get_nextdqblk) |
| 738 | return -ENOSYS; |
| 739 | qid = make_kqid(current_user_ns(), type, id); |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 740 | if (!qid_has_mapping(sb->s_user_ns, qid)) |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 741 | return -EINVAL; |
| 742 | ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq); |
| 743 | if (ret) |
| 744 | return ret; |
| 745 | id_out = from_kqid(current_user_ns(), qid); |
| 746 | copy_to_xfs_dqblk(&fdq, &qdq, type, id_out); |
| 747 | if (copy_to_user(addr, &fdq, sizeof(fdq))) |
| 748 | return -EFAULT; |
| 749 | return ret; |
| 750 | } |
| 751 | |
Eric Sandeen | 9da93f9 | 2014-05-05 17:25:50 +1000 | [diff] [blame] | 752 | static int quota_rmxquota(struct super_block *sb, void __user *addr) |
| 753 | { |
| 754 | __u32 flags; |
| 755 | |
| 756 | if (copy_from_user(&flags, addr, sizeof(flags))) |
| 757 | return -EFAULT; |
| 758 | if (!sb->s_qcop->rm_xquota) |
| 759 | return -ENOSYS; |
| 760 | return sb->s_qcop->rm_xquota(sb, flags); |
| 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* Copy parameters and call proper function */ |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 764 | static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, |
Al Viro | 8c54ca9 | 2016-11-20 19:49:34 -0500 | [diff] [blame] | 765 | void __user *addr, const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | { |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 767 | int ret; |
| 768 | |
Jeremy Cline | 7b6924d | 2018-07-31 01:37:31 +0000 | [diff] [blame] | 769 | type = array_index_nospec(type, MAXQUOTAS); |
Jan Kara | 2c5f648 | 2014-09-30 10:43:09 +0200 | [diff] [blame] | 770 | /* |
| 771 | * Quota not supported on this fs? Check this before s_quota_types |
| 772 | * since they needn't be set if quota is not supported at all. |
| 773 | */ |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 774 | if (!sb->s_qcop) |
| 775 | return -ENOSYS; |
Jan Kara | 2c5f648 | 2014-09-30 10:43:09 +0200 | [diff] [blame] | 776 | if (!(sb->s_quota_types & (1 << type))) |
| 777 | return -EINVAL; |
Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 778 | |
| 779 | ret = check_quotactl_permission(sb, type, cmd, id); |
| 780 | if (ret < 0) |
| 781 | return ret; |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | switch (cmd) { |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 784 | case Q_QUOTAON: |
Eric Sandeen | 3218a3e | 2016-02-08 11:21:24 +1100 | [diff] [blame] | 785 | return quota_quotaon(sb, type, id, path); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 786 | case Q_QUOTAOFF: |
Jan Kara | d3b8632 | 2014-10-08 16:07:12 +0200 | [diff] [blame] | 787 | return quota_quotaoff(sb, type); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 788 | case Q_GETFMT: |
| 789 | return quota_getfmt(sb, type, addr); |
| 790 | case Q_GETINFO: |
| 791 | return quota_getinfo(sb, type, addr); |
| 792 | case Q_SETINFO: |
| 793 | return quota_setinfo(sb, type, addr); |
| 794 | case Q_GETQUOTA: |
| 795 | return quota_getquota(sb, type, id, addr); |
Eric Sandeen | 926132c | 2016-02-08 11:22:21 +1100 | [diff] [blame] | 796 | case Q_GETNEXTQUOTA: |
| 797 | return quota_getnextquota(sb, type, id, addr); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 798 | case Q_SETQUOTA: |
| 799 | return quota_setquota(sb, type, id, addr); |
| 800 | case Q_SYNC: |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 801 | if (!sb->s_qcop->quota_sync) |
| 802 | return -ENOSYS; |
Jan Kara | ceed172 | 2012-07-03 16:45:28 +0200 | [diff] [blame] | 803 | return sb->s_qcop->quota_sync(sb, type); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 804 | case Q_XQUOTAON: |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 805 | return quota_enable(sb, addr); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 806 | case Q_XQUOTAOFF: |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 807 | return quota_disable(sb, addr); |
Eric Sandeen | 9da93f9 | 2014-05-05 17:25:50 +1000 | [diff] [blame] | 808 | case Q_XQUOTARM: |
| 809 | return quota_rmxquota(sb, addr); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 810 | case Q_XGETQSTAT: |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 811 | return quota_getxstate(sb, type, addr); |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 812 | case Q_XGETQSTATV: |
Eric Sandeen | 555b2c3 | 2019-06-21 18:27:13 -0500 | [diff] [blame] | 813 | return quota_getxstatev(sb, type, addr); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 814 | case Q_XSETQLIM: |
| 815 | return quota_setxquota(sb, type, id, addr); |
| 816 | case Q_XGETQUOTA: |
| 817 | return quota_getxquota(sb, type, id, addr); |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 818 | case Q_XGETNEXTQUOTA: |
| 819 | return quota_getnextxquota(sb, type, id, addr); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 820 | case Q_XQUOTASYNC: |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 821 | if (sb_rdonly(sb)) |
Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 822 | return -EROFS; |
Christoph Hellwig | 4b217ed | 2012-02-20 02:28:18 +0000 | [diff] [blame] | 823 | /* XFS quotas are fully coherent now, making this call a noop */ |
Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 824 | return 0; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 825 | default: |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 826 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Lee Jones | 56df127 | 2012-11-03 23:02:28 +0100 | [diff] [blame] | 830 | #ifdef CONFIG_BLOCK |
| 831 | |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 832 | /* Return 1 if 'cmd' will block on frozen filesystem */ |
| 833 | static int quotactl_cmd_write(int cmd) |
| 834 | { |
Jan Kara | ccf370e | 2016-02-18 14:03:03 +0100 | [diff] [blame] | 835 | /* |
| 836 | * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access |
| 837 | * as dquot_acquire() may allocate space for new structure and OCFS2 |
| 838 | * needs to increment on-disk use count. |
| 839 | */ |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 840 | switch (cmd) { |
| 841 | case Q_GETFMT: |
| 842 | case Q_GETINFO: |
| 843 | case Q_SYNC: |
| 844 | case Q_XGETQSTAT: |
Chandra Seetharaman | af30cb4 | 2013-08-06 17:27:07 -0500 | [diff] [blame] | 845 | case Q_XGETQSTATV: |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 846 | case Q_XGETQUOTA: |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 847 | case Q_XGETNEXTQUOTA: |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 848 | case Q_XQUOTASYNC: |
| 849 | return 0; |
| 850 | } |
| 851 | return 1; |
| 852 | } |
Lee Jones | 56df127 | 2012-11-03 23:02:28 +0100 | [diff] [blame] | 853 | #endif /* CONFIG_BLOCK */ |
| 854 | |
Jan Kara | 7d6cd73 | 2016-11-23 13:16:10 +0100 | [diff] [blame] | 855 | /* Return true if quotactl command is manipulating quota on/off state */ |
| 856 | static bool quotactl_cmd_onoff(int cmd) |
| 857 | { |
Javier Barrio | 41c4f85 | 2018-12-13 01:06:29 +0100 | [diff] [blame] | 858 | return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) || |
| 859 | (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF); |
Jan Kara | 7d6cd73 | 2016-11-23 13:16:10 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 863 | * look up a superblock on which quota ops will be performed |
| 864 | * - use the name of a block device to find the superblock thereon |
| 865 | */ |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 866 | static struct super_block *quotactl_block(const char __user *special, int cmd) |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 867 | { |
| 868 | #ifdef CONFIG_BLOCK |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 869 | struct super_block *sb; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 870 | struct filename *tmp = getname(special); |
Christoph Hellwig | 60b4988 | 2020-11-16 15:21:18 +0100 | [diff] [blame] | 871 | bool excl = false, thawed = false; |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame^] | 872 | int error; |
| 873 | dev_t dev; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 874 | |
| 875 | if (IS_ERR(tmp)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 876 | return ERR_CAST(tmp); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame^] | 877 | error = lookup_bdev(tmp->name, &dev); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 878 | putname(tmp); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame^] | 879 | if (error) |
| 880 | return ERR_PTR(error); |
Christoph Hellwig | 60b4988 | 2020-11-16 15:21:18 +0100 | [diff] [blame] | 881 | |
| 882 | if (quotactl_cmd_onoff(cmd)) { |
| 883 | excl = true; |
| 884 | thawed = true; |
| 885 | } else if (quotactl_cmd_write(cmd)) { |
| 886 | thawed = true; |
| 887 | } |
| 888 | |
| 889 | retry: |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame^] | 890 | sb = user_get_super(dev, excl); |
| 891 | if (!sb) |
| 892 | return ERR_PTR(-ENODEV); |
| 893 | if (thawed && sb->s_writers.frozen != SB_UNFROZEN) { |
Christoph Hellwig | 60b4988 | 2020-11-16 15:21:18 +0100 | [diff] [blame] | 894 | if (excl) |
| 895 | up_write(&sb->s_umount); |
| 896 | else |
| 897 | up_read(&sb->s_umount); |
| 898 | wait_event(sb->s_writers.wait_unfrozen, |
| 899 | sb->s_writers.frozen == SB_UNFROZEN); |
| 900 | put_super(sb); |
| 901 | goto retry; |
| 902 | } |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 903 | return sb; |
Christoph Hellwig | 60b4988 | 2020-11-16 15:21:18 +0100 | [diff] [blame] | 904 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 905 | #else |
| 906 | return ERR_PTR(-ENODEV); |
| 907 | #endif |
| 908 | } |
| 909 | |
| 910 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | * This is the system call interface. This communicates with |
| 912 | * the user-level programs. Currently this only supports diskquota |
| 913 | * calls. Maybe we need to add the process quotas etc. in the future, |
| 914 | * but we probably should use rlimits for that. |
| 915 | */ |
Christoph Hellwig | 80bdad3 | 2020-09-17 09:41:59 +0200 | [diff] [blame] | 916 | SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, |
| 917 | qid_t, id, void __user *, addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
| 919 | uint cmds, type; |
| 920 | struct super_block *sb = NULL; |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 921 | struct path path, *pathp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | int ret; |
| 923 | |
| 924 | cmds = cmd >> SUBCMDSHIFT; |
| 925 | type = cmd & SUBCMDMASK; |
| 926 | |
Chengguang Xu | a681031 | 2019-10-08 22:50:59 +0800 | [diff] [blame] | 927 | if (type >= MAXQUOTAS) |
| 928 | return -EINVAL; |
| 929 | |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 930 | /* |
| 931 | * As a special case Q_SYNC can be called without a specific device. |
| 932 | * It will iterate all superblocks that have quota enabled and call |
| 933 | * the sync action on each of them. |
| 934 | */ |
| 935 | if (!special) { |
| 936 | if (cmds == Q_SYNC) |
| 937 | return quota_sync_all(type); |
| 938 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 941 | /* |
| 942 | * Path for quotaon has to be resolved before grabbing superblock |
| 943 | * because that gets s_umount sem which is also possibly needed by path |
| 944 | * resolution (think about autofs) and thus deadlocks could arise. |
| 945 | */ |
| 946 | if (cmds == Q_QUOTAON) { |
Trond Myklebust | 815d405 | 2011-09-26 20:36:09 -0400 | [diff] [blame] | 947 | ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 948 | if (ret) |
| 949 | pathp = ERR_PTR(ret); |
| 950 | else |
| 951 | pathp = &path; |
| 952 | } |
| 953 | |
Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 954 | sb = quotactl_block(special, cmds); |
Jan Kara | 0aaa618 | 2011-10-10 18:32:06 +0200 | [diff] [blame] | 955 | if (IS_ERR(sb)) { |
| 956 | ret = PTR_ERR(sb); |
| 957 | goto out; |
| 958 | } |
Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 959 | |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 960 | ret = do_quotactl(sb, type, cmds, id, addr, pathp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Jan Kara | 7d6cd73 | 2016-11-23 13:16:10 +0100 | [diff] [blame] | 962 | if (!quotactl_cmd_onoff(cmds)) |
| 963 | drop_super(sb); |
| 964 | else |
| 965 | drop_super_exclusive(sb); |
Jan Kara | 0aaa618 | 2011-10-10 18:32:06 +0200 | [diff] [blame] | 966 | out: |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 967 | if (pathp && !IS_ERR(pathp)) |
| 968 | path_put(pathp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return ret; |
| 970 | } |