1 // SPDX-License-Identifier: GPL-2.0-only
3 * vfsv0 quota IO operations on file
6 #include <linux/errno.h>
8 #include <linux/mount.h>
9 #include <linux/dqblk_v2.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/quotaops.h>
16 #include <asm/byteorder.h>
18 #include "quota_tree.h"
19 #include "quotaio_v2.h"
21 MODULE_AUTHOR("Jan Kara");
22 MODULE_DESCRIPTION("Quota format v2 support");
23 MODULE_LICENSE("GPL");
25 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
26 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
27 static int v2r0_is_id(void *dp, struct dquot *dquot);
28 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
29 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
30 static int v2r1_is_id(void *dp, struct dquot *dquot);
32 static const struct qtree_fmt_operations v2r0_qtree_ops = {
33 .mem2disk_dqblk = v2r0_mem2diskdqb,
34 .disk2mem_dqblk = v2r0_disk2memdqb,
38 static const struct qtree_fmt_operations v2r1_qtree_ops = {
39 .mem2disk_dqblk = v2r1_mem2diskdqb,
40 .disk2mem_dqblk = v2r1_disk2memdqb,
44 #define QUOTABLOCK_BITS 10
45 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
47 static inline qsize_t v2_stoqb(qsize_t space)
49 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
52 static inline qsize_t v2_qbtos(qsize_t blocks)
54 return blocks << QUOTABLOCK_BITS;
57 static int v2_read_header(struct super_block *sb, int type,
58 struct v2_disk_dqheader *dqhead)
62 size = sb->s_op->quota_read(sb, type, (char *)dqhead,
63 sizeof(struct v2_disk_dqheader), 0);
64 if (size != sizeof(struct v2_disk_dqheader)) {
65 quota_error(sb, "Failed header read: expected=%zd got=%zd",
66 sizeof(struct v2_disk_dqheader), size);
74 /* Check whether given file is really vfsv0 quotafile */
75 static int v2_check_quota_file(struct super_block *sb, int type)
77 struct v2_disk_dqheader dqhead;
78 static const uint quota_magics[] = V2_INITQMAGICS;
79 static const uint quota_versions[] = V2_INITQVERSIONS;
81 if (v2_read_header(sb, type, &dqhead))
83 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
84 le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
89 /* Read information header from quota file */
90 static int v2_read_file_info(struct super_block *sb, int type)
92 struct v2_disk_dqinfo dinfo;
93 struct v2_disk_dqheader dqhead;
94 struct quota_info *dqopt = sb_dqopt(sb);
95 struct mem_dqinfo *info = &dqopt->info[type];
96 struct qtree_mem_dqinfo *qinfo;
99 unsigned int memalloc;
102 down_read(&dqopt->dqio_sem);
103 memalloc = memalloc_nofs_save();
104 ret = v2_read_header(sb, type, &dqhead);
107 version = le32_to_cpu(dqhead.dqh_version);
108 if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
109 (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
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)) {
117 quota_error(sb, "Can't read info structure");
124 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_KERNEL);
125 if (!info->dqi_priv) {
129 qinfo = info->dqi_priv;
131 /* limits are stored as unsigned 32-bit data */
132 info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
133 info->dqi_max_ino_limit = 0xffffffff;
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
140 info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
141 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
143 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
144 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
145 /* No flags currently supported */
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);
156 qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
157 qinfo->dqi_ops = &v2r0_qtree_ops;
159 qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
160 qinfo->dqi_ops = &v2r1_qtree_ops;
163 /* Some sanity checks of the read headers... */
164 if ((loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits >
165 i_size_read(sb_dqopt(sb)->files[type])) {
166 quota_error(sb, "Number of blocks too big for quota file size (%llu > %llu).",
167 (loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits,
168 i_size_read(sb_dqopt(sb)->files[type]));
171 if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
172 quota_error(sb, "Free block number too big (%u >= %u).",
173 qinfo->dqi_free_blk, qinfo->dqi_blocks);
176 if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
177 quota_error(sb, "Block with free entry too big (%u >= %u).",
178 qinfo->dqi_free_entry, qinfo->dqi_blocks);
184 kfree(info->dqi_priv);
185 info->dqi_priv = NULL;
188 memalloc_nofs_restore(memalloc);
189 up_read(&dqopt->dqio_sem);
193 /* Write information header to quota file */
194 static int v2_write_file_info(struct super_block *sb, int type)
196 struct v2_disk_dqinfo dinfo;
197 struct quota_info *dqopt = sb_dqopt(sb);
198 struct mem_dqinfo *info = &dqopt->info[type];
199 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
201 unsigned int memalloc;
203 down_write(&dqopt->dqio_sem);
204 memalloc = memalloc_nofs_save();
205 spin_lock(&dq_data_lock);
206 info->dqi_flags &= ~DQF_INFO_DIRTY;
207 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
208 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
209 /* No flags currently supported */
210 dinfo.dqi_flags = cpu_to_le32(0);
211 spin_unlock(&dq_data_lock);
212 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
213 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
214 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
215 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
216 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
217 memalloc_nofs_restore(memalloc);
218 up_write(&dqopt->dqio_sem);
219 if (size != sizeof(struct v2_disk_dqinfo)) {
220 quota_error(sb, "Can't write info structure");
221 return size < 0 ? size : -EIO;
226 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
228 struct v2r0_disk_dqblk *d = dp, empty;
229 struct mem_dqblk *m = &dquot->dq_dqb;
231 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
232 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
233 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
234 m->dqb_itime = le64_to_cpu(d->dqb_itime);
235 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
236 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
237 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
238 m->dqb_btime = le64_to_cpu(d->dqb_btime);
239 /* We need to escape back all-zero structure */
240 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
241 empty.dqb_itime = cpu_to_le64(1);
242 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
246 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
248 struct v2r0_disk_dqblk *d = dp;
249 struct mem_dqblk *m = &dquot->dq_dqb;
250 struct qtree_mem_dqinfo *info =
251 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
253 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
254 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
255 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
256 d->dqb_itime = cpu_to_le64(m->dqb_itime);
257 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
258 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
259 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
260 d->dqb_btime = cpu_to_le64(m->dqb_btime);
261 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
262 if (qtree_entry_unused(info, dp))
263 d->dqb_itime = cpu_to_le64(1);
266 static int v2r0_is_id(void *dp, struct dquot *dquot)
268 struct v2r0_disk_dqblk *d = dp;
269 struct qtree_mem_dqinfo *info =
270 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
272 if (qtree_entry_unused(info, dp))
274 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
275 le32_to_cpu(d->dqb_id)),
279 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
281 struct v2r1_disk_dqblk *d = dp, empty;
282 struct mem_dqblk *m = &dquot->dq_dqb;
284 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
285 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
286 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
287 m->dqb_itime = le64_to_cpu(d->dqb_itime);
288 m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
289 m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
290 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
291 m->dqb_btime = le64_to_cpu(d->dqb_btime);
292 /* We need to escape back all-zero structure */
293 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
294 empty.dqb_itime = cpu_to_le64(1);
295 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
299 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
301 struct v2r1_disk_dqblk *d = dp;
302 struct mem_dqblk *m = &dquot->dq_dqb;
303 struct qtree_mem_dqinfo *info =
304 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
306 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
307 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
308 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
309 d->dqb_itime = cpu_to_le64(m->dqb_itime);
310 d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
311 d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
312 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
313 d->dqb_btime = cpu_to_le64(m->dqb_btime);
314 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
316 if (qtree_entry_unused(info, dp))
317 d->dqb_itime = cpu_to_le64(1);
320 static int v2r1_is_id(void *dp, struct dquot *dquot)
322 struct v2r1_disk_dqblk *d = dp;
323 struct qtree_mem_dqinfo *info =
324 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
326 if (qtree_entry_unused(info, dp))
328 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
329 le32_to_cpu(d->dqb_id)),
333 static int v2_read_dquot(struct dquot *dquot)
335 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
337 unsigned int memalloc;
339 down_read(&dqopt->dqio_sem);
340 memalloc = memalloc_nofs_save();
341 ret = qtree_read_dquot(
342 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
344 memalloc_nofs_restore(memalloc);
345 up_read(&dqopt->dqio_sem);
349 static int v2_write_dquot(struct dquot *dquot)
351 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
354 unsigned int memalloc;
357 * If space for dquot is already allocated, we don't need any
358 * protection as we'll only overwrite the place of dquot. We are
359 * still protected by concurrent writes of the same dquot by
362 if (!dquot->dq_off) {
364 down_write(&dqopt->dqio_sem);
366 down_read(&dqopt->dqio_sem);
368 memalloc = memalloc_nofs_save();
369 ret = qtree_write_dquot(
370 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
372 memalloc_nofs_restore(memalloc);
374 up_write(&dqopt->dqio_sem);
376 up_read(&dqopt->dqio_sem);
380 static int v2_release_dquot(struct dquot *dquot)
382 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
383 unsigned int memalloc;
386 down_write(&dqopt->dqio_sem);
387 memalloc = memalloc_nofs_save();
388 ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
389 memalloc_nofs_restore(memalloc);
390 up_write(&dqopt->dqio_sem);
395 static int v2_free_file_info(struct super_block *sb, int type)
397 kfree(sb_dqinfo(sb, type)->dqi_priv);
401 static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
403 struct quota_info *dqopt = sb_dqopt(sb);
404 unsigned int memalloc;
407 down_read(&dqopt->dqio_sem);
408 memalloc = memalloc_nofs_save();
409 ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
410 memalloc_nofs_restore(memalloc);
411 up_read(&dqopt->dqio_sem);
415 static const struct quota_format_ops v2_format_ops = {
416 .check_quota_file = v2_check_quota_file,
417 .read_file_info = v2_read_file_info,
418 .write_file_info = v2_write_file_info,
419 .free_file_info = v2_free_file_info,
420 .read_dqblk = v2_read_dquot,
421 .commit_dqblk = v2_write_dquot,
422 .release_dqblk = v2_release_dquot,
423 .get_next_id = v2_get_next_id,
426 static struct quota_format_type v2r0_quota_format = {
427 .qf_fmt_id = QFMT_VFS_V0,
428 .qf_ops = &v2_format_ops,
429 .qf_owner = THIS_MODULE
432 static struct quota_format_type v2r1_quota_format = {
433 .qf_fmt_id = QFMT_VFS_V1,
434 .qf_ops = &v2_format_ops,
435 .qf_owner = THIS_MODULE
438 static int __init init_v2_quota_format(void)
442 ret = register_quota_format(&v2r0_quota_format);
445 return register_quota_format(&v2r1_quota_format);
448 static void __exit exit_v2_quota_format(void)
450 unregister_quota_format(&v2r0_quota_format);
451 unregister_quota_format(&v2r1_quota_format);
454 module_init(init_v2_quota_format);
455 module_exit(exit_v2_quota_format);