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