2 * vfsv0 quota IO operations on file
5 #include <linux/errno.h>
7 #include <linux/mount.h>
8 #include <linux/dqblk_v2.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/quotaops.h>
15 #include <asm/byteorder.h>
17 #include "quota_tree.h"
18 #include "quotaio_v2.h"
20 MODULE_AUTHOR("Jan Kara");
21 MODULE_DESCRIPTION("Quota format v2 support");
22 MODULE_LICENSE("GPL");
24 #define __QUOTA_V2_PARANOIA
26 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
27 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
28 static int v2r0_is_id(void *dp, struct dquot *dquot);
29 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
30 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
31 static int v2r1_is_id(void *dp, struct dquot *dquot);
33 static const struct qtree_fmt_operations v2r0_qtree_ops = {
34 .mem2disk_dqblk = v2r0_mem2diskdqb,
35 .disk2mem_dqblk = v2r0_disk2memdqb,
39 static const struct qtree_fmt_operations v2r1_qtree_ops = {
40 .mem2disk_dqblk = v2r1_mem2diskdqb,
41 .disk2mem_dqblk = v2r1_disk2memdqb,
45 #define QUOTABLOCK_BITS 10
46 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
48 static inline qsize_t v2_stoqb(qsize_t space)
50 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
53 static inline qsize_t v2_qbtos(qsize_t blocks)
55 return blocks << QUOTABLOCK_BITS;
58 static int v2_read_header(struct super_block *sb, int type,
59 struct v2_disk_dqheader *dqhead)
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)) {
66 quota_error(sb, "Failed header read: expected=%zd got=%zd",
67 sizeof(struct v2_disk_dqheader), size);
75 /* Check whether given file is really vfsv0 quotafile */
76 static int v2_check_quota_file(struct super_block *sb, int type)
78 struct v2_disk_dqheader dqhead;
79 static const uint quota_magics[] = V2_INITQMAGICS;
80 static const uint quota_versions[] = V2_INITQVERSIONS;
82 if (v2_read_header(sb, type, &dqhead))
84 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
85 le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
90 /* Read information header from quota file */
91 static int v2_read_file_info(struct super_block *sb, int type)
93 struct v2_disk_dqinfo dinfo;
94 struct v2_disk_dqheader dqhead;
95 struct quota_info *dqopt = sb_dqopt(sb);
96 struct mem_dqinfo *info = &dqopt->info[type];
97 struct qtree_mem_dqinfo *qinfo;
102 down_read(&dqopt->dqio_sem);
103 ret = v2_read_header(sb, type, &dqhead);
106 version = le32_to_cpu(dqhead.dqh_version);
107 if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
108 (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
113 size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
114 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
115 if (size != sizeof(struct v2_disk_dqinfo)) {
116 quota_error(sb, "Can't read info structure");
123 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
124 if (!info->dqi_priv) {
128 qinfo = info->dqi_priv;
130 /* limits are stored as unsigned 32-bit data */
131 info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
132 info->dqi_max_ino_limit = 0xffffffff;
135 * Used space is stored as unsigned 64-bit value in bytes but
136 * quota core supports only signed 64-bit values so use that
139 info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
140 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
142 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
143 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
144 /* No flags currently supported */
147 qinfo->dqi_type = type;
148 qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
149 qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
150 qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
151 qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
152 qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
153 qinfo->dqi_qtree_depth = qtree_depth(qinfo);
155 qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
156 qinfo->dqi_ops = &v2r0_qtree_ops;
158 qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
159 qinfo->dqi_ops = &v2r1_qtree_ops;
162 /* Some sanity checks of the read headers... */
163 if ((loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits >
164 i_size_read(sb_dqopt(sb)->files[type])) {
165 quota_error(sb, "Number of blocks too big for quota file size (%llu > %llu).",
166 (loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits,
167 i_size_read(sb_dqopt(sb)->files[type]));
170 if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
171 quota_error(sb, "Free block number too big (%u >= %u).",
172 qinfo->dqi_free_blk, qinfo->dqi_blocks);
175 if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
176 quota_error(sb, "Block with free entry too big (%u >= %u).",
177 qinfo->dqi_free_entry, qinfo->dqi_blocks);
183 kfree(info->dqi_priv);
184 info->dqi_priv = NULL;
187 up_read(&dqopt->dqio_sem);
191 /* Write information header to quota file */
192 static int v2_write_file_info(struct super_block *sb, int type)
194 struct v2_disk_dqinfo dinfo;
195 struct quota_info *dqopt = sb_dqopt(sb);
196 struct mem_dqinfo *info = &dqopt->info[type];
197 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
200 down_write(&dqopt->dqio_sem);
201 spin_lock(&dq_data_lock);
202 info->dqi_flags &= ~DQF_INFO_DIRTY;
203 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
204 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
205 /* No flags currently supported */
206 dinfo.dqi_flags = cpu_to_le32(0);
207 spin_unlock(&dq_data_lock);
208 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
209 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
210 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
211 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
212 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
213 up_write(&dqopt->dqio_sem);
214 if (size != sizeof(struct v2_disk_dqinfo)) {
215 quota_error(sb, "Can't write info structure");
221 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
223 struct v2r0_disk_dqblk *d = dp, empty;
224 struct mem_dqblk *m = &dquot->dq_dqb;
226 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
227 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
228 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
229 m->dqb_itime = le64_to_cpu(d->dqb_itime);
230 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
231 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
232 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
233 m->dqb_btime = le64_to_cpu(d->dqb_btime);
234 /* We need to escape back all-zero structure */
235 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
236 empty.dqb_itime = cpu_to_le64(1);
237 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
241 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
243 struct v2r0_disk_dqblk *d = dp;
244 struct mem_dqblk *m = &dquot->dq_dqb;
245 struct qtree_mem_dqinfo *info =
246 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
248 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
249 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
250 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
251 d->dqb_itime = cpu_to_le64(m->dqb_itime);
252 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
253 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
254 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
255 d->dqb_btime = cpu_to_le64(m->dqb_btime);
256 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
257 if (qtree_entry_unused(info, dp))
258 d->dqb_itime = cpu_to_le64(1);
261 static int v2r0_is_id(void *dp, struct dquot *dquot)
263 struct v2r0_disk_dqblk *d = dp;
264 struct qtree_mem_dqinfo *info =
265 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
267 if (qtree_entry_unused(info, dp))
269 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
270 le32_to_cpu(d->dqb_id)),
274 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
276 struct v2r1_disk_dqblk *d = dp, empty;
277 struct mem_dqblk *m = &dquot->dq_dqb;
279 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
280 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
281 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
282 m->dqb_itime = le64_to_cpu(d->dqb_itime);
283 m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
284 m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
285 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
286 m->dqb_btime = le64_to_cpu(d->dqb_btime);
287 /* We need to escape back all-zero structure */
288 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
289 empty.dqb_itime = cpu_to_le64(1);
290 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
294 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
296 struct v2r1_disk_dqblk *d = dp;
297 struct mem_dqblk *m = &dquot->dq_dqb;
298 struct qtree_mem_dqinfo *info =
299 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
301 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
302 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
303 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
304 d->dqb_itime = cpu_to_le64(m->dqb_itime);
305 d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
306 d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
307 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
308 d->dqb_btime = cpu_to_le64(m->dqb_btime);
309 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
311 if (qtree_entry_unused(info, dp))
312 d->dqb_itime = cpu_to_le64(1);
315 static int v2r1_is_id(void *dp, struct dquot *dquot)
317 struct v2r1_disk_dqblk *d = dp;
318 struct qtree_mem_dqinfo *info =
319 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
321 if (qtree_entry_unused(info, dp))
323 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
324 le32_to_cpu(d->dqb_id)),
328 static int v2_read_dquot(struct dquot *dquot)
330 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
333 down_read(&dqopt->dqio_sem);
334 ret = qtree_read_dquot(
335 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
337 up_read(&dqopt->dqio_sem);
341 static int v2_write_dquot(struct dquot *dquot)
343 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
348 * If space for dquot is already allocated, we don't need any
349 * protection as we'll only overwrite the place of dquot. We are
350 * still protected by concurrent writes of the same dquot by
353 if (!dquot->dq_off) {
355 down_write(&dqopt->dqio_sem);
357 down_read(&dqopt->dqio_sem);
359 ret = qtree_write_dquot(
360 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
363 up_write(&dqopt->dqio_sem);
365 up_read(&dqopt->dqio_sem);
369 static int v2_release_dquot(struct dquot *dquot)
371 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
374 down_write(&dqopt->dqio_sem);
375 ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
376 up_write(&dqopt->dqio_sem);
381 static int v2_free_file_info(struct super_block *sb, int type)
383 kfree(sb_dqinfo(sb, type)->dqi_priv);
387 static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
389 struct quota_info *dqopt = sb_dqopt(sb);
392 down_read(&dqopt->dqio_sem);
393 ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
394 up_read(&dqopt->dqio_sem);
398 static const struct quota_format_ops v2_format_ops = {
399 .check_quota_file = v2_check_quota_file,
400 .read_file_info = v2_read_file_info,
401 .write_file_info = v2_write_file_info,
402 .free_file_info = v2_free_file_info,
403 .read_dqblk = v2_read_dquot,
404 .commit_dqblk = v2_write_dquot,
405 .release_dqblk = v2_release_dquot,
406 .get_next_id = v2_get_next_id,
409 static struct quota_format_type v2r0_quota_format = {
410 .qf_fmt_id = QFMT_VFS_V0,
411 .qf_ops = &v2_format_ops,
412 .qf_owner = THIS_MODULE
415 static struct quota_format_type v2r1_quota_format = {
416 .qf_fmt_id = QFMT_VFS_V1,
417 .qf_ops = &v2_format_ops,
418 .qf_owner = THIS_MODULE
421 static int __init init_v2_quota_format(void)
425 ret = register_quota_format(&v2r0_quota_format);
428 return register_quota_format(&v2r1_quota_format);
431 static void __exit exit_v2_quota_format(void)
433 unregister_quota_format(&v2r0_quota_format);
434 unregister_quota_format(&v2r1_quota_format);
437 module_init(init_v2_quota_format);
438 module_exit(exit_v2_quota_format);