]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/quota/quota_v2.c
treewide: Add SPDX license identifier for more missed files
[thirdparty/linux.git] / fs / quota / quota_v2.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * vfsv0 quota IO operations on file
4 */
5
6#include <linux/errno.h>
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/dqblk_v2.h>
1da177e4
LT
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/slab.h>
74abb989 14#include <linux/quotaops.h>
1da177e4
LT
15
16#include <asm/byteorder.h>
17
1ccd14b9 18#include "quota_tree.h"
cf770c13
JK
19#include "quotaio_v2.h"
20
1da177e4
LT
21MODULE_AUTHOR("Jan Kara");
22MODULE_DESCRIPTION("Quota format v2 support");
23MODULE_LICENSE("GPL");
24
25#define __QUOTA_V2_PARANOIA
26
498c6015
JK
27static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
28static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
29static int v2r0_is_id(void *dp, struct dquot *dquot);
30static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
31static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
32static int v2r1_is_id(void *dp, struct dquot *dquot);
33
d1b98c23 34static const struct qtree_fmt_operations v2r0_qtree_ops = {
498c6015
JK
35 .mem2disk_dqblk = v2r0_mem2diskdqb,
36 .disk2mem_dqblk = v2r0_disk2memdqb,
37 .is_id = v2r0_is_id,
38};
39
d1b98c23 40static const struct qtree_fmt_operations v2r1_qtree_ops = {
498c6015
JK
41 .mem2disk_dqblk = v2r1_mem2diskdqb,
42 .disk2mem_dqblk = v2r1_disk2memdqb,
43 .is_id = v2r1_is_id,
1ccd14b9 44};
1da177e4 45
12095460
JK
46#define QUOTABLOCK_BITS 10
47#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
48
49static inline qsize_t v2_stoqb(qsize_t space)
50{
51 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
52}
53
54static inline qsize_t v2_qbtos(qsize_t blocks)
55{
56 return blocks << QUOTABLOCK_BITS;
57}
58
498c6015
JK
59static int v2_read_header(struct super_block *sb, int type,
60 struct v2_disk_dqheader *dqhead)
61{
62 ssize_t size;
63
64 size = sb->s_op->quota_read(sb, type, (char *)dqhead,
65 sizeof(struct v2_disk_dqheader), 0);
66 if (size != sizeof(struct v2_disk_dqheader)) {
fb5ffb0e
JZ
67 quota_error(sb, "Failed header read: expected=%zd got=%zd",
68 sizeof(struct v2_disk_dqheader), size);
f98bbe37
JK
69 if (size < 0)
70 return size;
71 return -EIO;
498c6015 72 }
f98bbe37 73 return 0;
498c6015
JK
74}
75
1da177e4
LT
76/* Check whether given file is really vfsv0 quotafile */
77static int v2_check_quota_file(struct super_block *sb, int type)
78{
79 struct v2_disk_dqheader dqhead;
1da177e4
LT
80 static const uint quota_magics[] = V2_INITQMAGICS;
81 static const uint quota_versions[] = V2_INITQVERSIONS;
27942ef5 82
f98bbe37 83 if (v2_read_header(sb, type, &dqhead))
1da177e4 84 return 0;
1da177e4 85 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
498c6015 86 le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
1da177e4
LT
87 return 0;
88 return 1;
89}
90
91/* Read information header from quota file */
92static int v2_read_file_info(struct super_block *sb, int type)
93{
94 struct v2_disk_dqinfo dinfo;
498c6015 95 struct v2_disk_dqheader dqhead;
42fdb858
JK
96 struct quota_info *dqopt = sb_dqopt(sb);
97 struct mem_dqinfo *info = &dqopt->info[type];
e3d4d56b 98 struct qtree_mem_dqinfo *qinfo;
1da177e4 99 ssize_t size;
498c6015 100 unsigned int version;
42fdb858 101 int ret;
498c6015 102
42fdb858 103 down_read(&dqopt->dqio_sem);
f98bbe37
JK
104 ret = v2_read_header(sb, type, &dqhead);
105 if (ret < 0)
42fdb858 106 goto out;
498c6015 107 version = le32_to_cpu(dqhead.dqh_version);
869835df 108 if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
42fdb858 109 (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
cb8d01b4 110 ret = -EINVAL;
42fdb858
JK
111 goto out;
112 }
1da177e4
LT
113
114 size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
115 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
116 if (size != sizeof(struct v2_disk_dqinfo)) {
fb5ffb0e 117 quota_error(sb, "Can't read info structure");
f98bbe37
JK
118 if (size < 0)
119 ret = size;
120 else
121 ret = -EIO;
42fdb858 122 goto out;
1da177e4 123 }
e3d4d56b
JK
124 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
125 if (!info->dqi_priv) {
42fdb858
JK
126 ret = -ENOMEM;
127 goto out;
e3d4d56b
JK
128 }
129 qinfo = info->dqi_priv;
498c6015
JK
130 if (version == 0) {
131 /* limits are stored as unsigned 32-bit data */
7e08da50 132 info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
b10a0819 133 info->dqi_max_ino_limit = 0xffffffff;
498c6015 134 } else {
7e08da50
JK
135 /*
136 * Used space is stored as unsigned 64-bit value in bytes but
137 * quota core supports only signed 64-bit values so use that
138 * as a limit
139 */
140 info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
141 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
498c6015 142 }
1da177e4
LT
143 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
144 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
c119c5b9
JK
145 /* No flags currently supported */
146 info->dqi_flags = 0;
e3d4d56b
JK
147 qinfo->dqi_sb = sb;
148 qinfo->dqi_type = type;
149 qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
150 qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
151 qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
152 qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
153 qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
154 qinfo->dqi_qtree_depth = qtree_depth(qinfo);
498c6015
JK
155 if (version == 0) {
156 qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
157 qinfo->dqi_ops = &v2r0_qtree_ops;
158 } else {
159 qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
160 qinfo->dqi_ops = &v2r1_qtree_ops;
161 }
42fdb858
JK
162 ret = 0;
163out:
164 up_read(&dqopt->dqio_sem);
165 return ret;
1da177e4
LT
166}
167
168/* Write information header to quota file */
169static int v2_write_file_info(struct super_block *sb, int type)
170{
171 struct v2_disk_dqinfo dinfo;
9a8ae30e
JK
172 struct quota_info *dqopt = sb_dqopt(sb);
173 struct mem_dqinfo *info = &dqopt->info[type];
e3d4d56b 174 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
1da177e4
LT
175 ssize_t size;
176
9a8ae30e 177 down_write(&dqopt->dqio_sem);
1da177e4
LT
178 spin_lock(&dq_data_lock);
179 info->dqi_flags &= ~DQF_INFO_DIRTY;
180 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
181 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
c119c5b9
JK
182 /* No flags currently supported */
183 dinfo.dqi_flags = cpu_to_le32(0);
1da177e4 184 spin_unlock(&dq_data_lock);
e3d4d56b
JK
185 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
186 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
187 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
1da177e4
LT
188 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
189 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
9a8ae30e 190 up_write(&dqopt->dqio_sem);
1da177e4 191 if (size != sizeof(struct v2_disk_dqinfo)) {
fb5ffb0e 192 quota_error(sb, "Can't write info structure");
1da177e4
LT
193 return -1;
194 }
195 return 0;
196}
197
498c6015 198static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
1da177e4 199{
498c6015 200 struct v2r0_disk_dqblk *d = dp, empty;
1ccd14b9
JK
201 struct mem_dqblk *m = &dquot->dq_dqb;
202
1da177e4
LT
203 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
204 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
205 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
206 m->dqb_itime = le64_to_cpu(d->dqb_itime);
12095460
JK
207 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
208 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
1da177e4
LT
209 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
210 m->dqb_btime = le64_to_cpu(d->dqb_btime);
1ccd14b9 211 /* We need to escape back all-zero structure */
498c6015 212 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
1ccd14b9 213 empty.dqb_itime = cpu_to_le64(1);
498c6015 214 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
1ccd14b9 215 m->dqb_itime = 0;
1da177e4
LT
216}
217
498c6015 218static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
1da177e4 219{
498c6015 220 struct v2r0_disk_dqblk *d = dp;
1ccd14b9
JK
221 struct mem_dqblk *m = &dquot->dq_dqb;
222 struct qtree_mem_dqinfo *info =
4c376dca 223 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
1ccd14b9 224
1da177e4
LT
225 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
226 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
227 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
228 d->dqb_itime = cpu_to_le64(m->dqb_itime);
cf770c13
JK
229 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
230 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
1da177e4
LT
231 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
232 d->dqb_btime = cpu_to_le64(m->dqb_btime);
4c376dca 233 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
1ccd14b9
JK
234 if (qtree_entry_unused(info, dp))
235 d->dqb_itime = cpu_to_le64(1);
1da177e4
LT
236}
237
498c6015
JK
238static int v2r0_is_id(void *dp, struct dquot *dquot)
239{
240 struct v2r0_disk_dqblk *d = dp;
241 struct qtree_mem_dqinfo *info =
4c376dca 242 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
498c6015
JK
243
244 if (qtree_entry_unused(info, dp))
245 return 0;
4c376dca
EB
246 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
247 le32_to_cpu(d->dqb_id)),
248 dquot->dq_id);
498c6015
JK
249}
250
251static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
252{
253 struct v2r1_disk_dqblk *d = dp, empty;
254 struct mem_dqblk *m = &dquot->dq_dqb;
255
256 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
257 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
258 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
259 m->dqb_itime = le64_to_cpu(d->dqb_itime);
260 m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
261 m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
262 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
263 m->dqb_btime = le64_to_cpu(d->dqb_btime);
264 /* We need to escape back all-zero structure */
265 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
266 empty.dqb_itime = cpu_to_le64(1);
267 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
268 m->dqb_itime = 0;
269}
270
271static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
272{
273 struct v2r1_disk_dqblk *d = dp;
274 struct mem_dqblk *m = &dquot->dq_dqb;
275 struct qtree_mem_dqinfo *info =
4c376dca 276 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
498c6015
JK
277
278 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
279 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
280 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
281 d->dqb_itime = cpu_to_le64(m->dqb_itime);
282 d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
283 d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
284 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
285 d->dqb_btime = cpu_to_le64(m->dqb_btime);
4c376dca 286 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
498c6015
JK
287 if (qtree_entry_unused(info, dp))
288 d->dqb_itime = cpu_to_le64(1);
289}
290
291static int v2r1_is_id(void *dp, struct dquot *dquot)
1da177e4 292{
498c6015 293 struct v2r1_disk_dqblk *d = dp;
1ccd14b9 294 struct qtree_mem_dqinfo *info =
4c376dca 295 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
1da177e4 296
1ccd14b9 297 if (qtree_entry_unused(info, dp))
1da177e4 298 return 0;
4c376dca
EB
299 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
300 le32_to_cpu(d->dqb_id)),
301 dquot->dq_id);
1da177e4
LT
302}
303
1ccd14b9 304static int v2_read_dquot(struct dquot *dquot)
1da177e4 305{
e342e38d
JK
306 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
307 int ret;
308
309 down_read(&dqopt->dqio_sem);
310 ret = qtree_read_dquot(
311 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
312 dquot);
313 up_read(&dqopt->dqio_sem);
314 return ret;
1da177e4
LT
315}
316
1da177e4
LT
317static int v2_write_dquot(struct dquot *dquot)
318{
8fc32c2b
JK
319 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
320 int ret;
d2faa415
JK
321 bool alloc = false;
322
323 /*
324 * If space for dquot is already allocated, we don't need any
325 * protection as we'll only overwrite the place of dquot. We are
326 * still protected by concurrent writes of the same dquot by
327 * dquot->dq_lock.
328 */
329 if (!dquot->dq_off) {
330 alloc = true;
331 down_write(&dqopt->dqio_sem);
4c6bb696
JK
332 } else {
333 down_read(&dqopt->dqio_sem);
d2faa415 334 }
8fc32c2b
JK
335 ret = qtree_write_dquot(
336 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
337 dquot);
d2faa415
JK
338 if (alloc)
339 up_write(&dqopt->dqio_sem);
4c6bb696
JK
340 else
341 up_read(&dqopt->dqio_sem);
8fc32c2b 342 return ret;
1da177e4
LT
343}
344
1da177e4
LT
345static int v2_release_dquot(struct dquot *dquot)
346{
b9a1a7f4
JK
347 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
348 int ret;
349
350 down_write(&dqopt->dqio_sem);
351 ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
352 up_write(&dqopt->dqio_sem);
353
354 return ret;
e3d4d56b
JK
355}
356
357static int v2_free_file_info(struct super_block *sb, int type)
358{
359 kfree(sb_dqinfo(sb, type)->dqi_priv);
360 return 0;
1da177e4
LT
361}
362
0066373d
JK
363static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
364{
f14618c6
JK
365 struct quota_info *dqopt = sb_dqopt(sb);
366 int ret;
367
368 down_read(&dqopt->dqio_sem);
369 ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
370 up_read(&dqopt->dqio_sem);
371 return ret;
0066373d
JK
372}
373
1472da5f 374static const struct quota_format_ops v2_format_ops = {
1da177e4
LT
375 .check_quota_file = v2_check_quota_file,
376 .read_file_info = v2_read_file_info,
377 .write_file_info = v2_write_file_info,
e3d4d56b 378 .free_file_info = v2_free_file_info,
1da177e4
LT
379 .read_dqblk = v2_read_dquot,
380 .commit_dqblk = v2_write_dquot,
381 .release_dqblk = v2_release_dquot,
0066373d 382 .get_next_id = v2_get_next_id,
1da177e4
LT
383};
384
498c6015 385static struct quota_format_type v2r0_quota_format = {
1da177e4
LT
386 .qf_fmt_id = QFMT_VFS_V0,
387 .qf_ops = &v2_format_ops,
388 .qf_owner = THIS_MODULE
389};
390
498c6015
JK
391static struct quota_format_type v2r1_quota_format = {
392 .qf_fmt_id = QFMT_VFS_V1,
393 .qf_ops = &v2_format_ops,
394 .qf_owner = THIS_MODULE
395};
396
1da177e4
LT
397static int __init init_v2_quota_format(void)
398{
498c6015
JK
399 int ret;
400
401 ret = register_quota_format(&v2r0_quota_format);
402 if (ret)
403 return ret;
404 return register_quota_format(&v2r1_quota_format);
1da177e4
LT
405}
406
407static void __exit exit_v2_quota_format(void)
408{
498c6015
JK
409 unregister_quota_format(&v2r0_quota_format);
410 unregister_quota_format(&v2r1_quota_format);
1da177e4
LT
411}
412
413module_init(init_v2_quota_format);
414module_exit(exit_v2_quota_format);