2 * vfsv0 quota IO operations on file
5 #include <linux/errno.h>
7 #include <linux/dqblk_v2.h>
8 #include <linux/quotaio_v2.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
14 #include <asm/byteorder.h>
15 #include <asm/uaccess.h>
17 MODULE_AUTHOR("Jan Kara");
18 MODULE_DESCRIPTION("Quota format v2 support");
19 MODULE_LICENSE("GPL");
21 #define __QUOTA_V2_PARANOIA
23 typedef char *dqbuf_t;
25 #define GETIDINDEX(id, depth) (((id) >> ((V2_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
26 #define GETENTRIES(buf) ((struct v2_disk_dqblk *)(((char *)buf)+sizeof(struct v2_disk_dqdbheader)))
28 /* Check whether given file is really vfsv0 quotafile */
29 static int v2_check_quota_file(struct super_block *sb, int type)
31 struct v2_disk_dqheader dqhead;
32 struct file *f = sb_dqopt(sb)->files[type];
36 static const uint quota_magics[] = V2_INITQMAGICS;
37 static const uint quota_versions[] = V2_INITQVERSIONS;
41 size = f->f_op->read(f, (char *)&dqhead, sizeof(struct v2_disk_dqheader), &offset);
43 if (size != sizeof(struct v2_disk_dqheader))
45 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
46 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
51 /* Read information header from quota file */
52 static int v2_read_file_info(struct super_block *sb, int type)
55 struct v2_disk_dqinfo dinfo;
56 struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
57 struct file *f = sb_dqopt(sb)->files[type];
59 loff_t offset = V2_DQINFOOFF;
63 size = f->f_op->read(f, (char *)&dinfo, sizeof(struct v2_disk_dqinfo), &offset);
65 if (size != sizeof(struct v2_disk_dqinfo)) {
66 printk(KERN_WARNING "Can't read info structure on device %s.\n",
67 kdevname(f->f_dentry->d_sb->s_dev));
70 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
71 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
72 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
73 info->u.v2_i.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
74 info->u.v2_i.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
75 info->u.v2_i.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
79 /* Write information header to quota file */
80 static int v2_write_file_info(struct super_block *sb, int type)
83 struct v2_disk_dqinfo dinfo;
84 struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
85 struct file *f = sb_dqopt(sb)->files[type];
87 loff_t offset = V2_DQINFOOFF;
89 info->dqi_flags &= ~DQF_INFO_DIRTY;
90 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
91 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
92 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
93 dinfo.dqi_blocks = cpu_to_le32(info->u.v2_i.dqi_blocks);
94 dinfo.dqi_free_blk = cpu_to_le32(info->u.v2_i.dqi_free_blk);
95 dinfo.dqi_free_entry = cpu_to_le32(info->u.v2_i.dqi_free_entry);
98 size = f->f_op->write(f, (char *)&dinfo, sizeof(struct v2_disk_dqinfo), &offset);
100 if (size != sizeof(struct v2_disk_dqinfo)) {
101 printk(KERN_WARNING "Can't write info structure on device %s.\n",
102 kdevname(f->f_dentry->d_sb->s_dev));
108 static void disk2memdqb(struct mem_dqblk *m, struct v2_disk_dqblk *d)
110 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
111 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
112 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
113 m->dqb_itime = le64_to_cpu(d->dqb_itime);
114 m->dqb_bhardlimit = le32_to_cpu(d->dqb_bhardlimit);
115 m->dqb_bsoftlimit = le32_to_cpu(d->dqb_bsoftlimit);
116 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
117 m->dqb_btime = le64_to_cpu(d->dqb_btime);
120 static void mem2diskdqb(struct v2_disk_dqblk *d, struct mem_dqblk *m, qid_t id)
122 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
123 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
124 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
125 d->dqb_itime = cpu_to_le64(m->dqb_itime);
126 d->dqb_bhardlimit = cpu_to_le32(m->dqb_bhardlimit);
127 d->dqb_bsoftlimit = cpu_to_le32(m->dqb_bsoftlimit);
128 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
129 d->dqb_btime = cpu_to_le64(m->dqb_btime);
130 d->dqb_id = cpu_to_le32(id);
133 static dqbuf_t getdqbuf(void)
135 dqbuf_t buf = kmalloc(V2_DQBLKSIZE, GFP_KERNEL);
137 printk(KERN_WARNING "VFS: Not enough memory for quota buffers.\n");
141 static inline void freedqbuf(dqbuf_t buf)
146 static ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
150 loff_t offset = blk<<V2_DQBLKSIZE_BITS;
152 memset(buf, 0, V2_DQBLKSIZE);
155 ret = filp->f_op->read(filp, (char *)buf, V2_DQBLKSIZE, &offset);
160 static ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
164 loff_t offset = blk<<V2_DQBLKSIZE_BITS;
168 ret = filp->f_op->write(filp, (char *)buf, V2_DQBLKSIZE, &offset);
174 /* Remove empty block from list and return it */
175 static int get_free_dqblk(struct file *filp, struct mem_dqinfo *info)
177 dqbuf_t buf = getdqbuf();
178 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
183 if (info->u.v2_i.dqi_free_blk) {
184 blk = info->u.v2_i.dqi_free_blk;
185 if ((ret = read_blk(filp, blk, buf)) < 0)
187 info->u.v2_i.dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
190 memset(buf, 0, V2_DQBLKSIZE);
191 if ((ret = write_blk(filp, info->u.v2_i.dqi_blocks, buf)) < 0) /* Assure block allocation... */
193 blk = info->u.v2_i.dqi_blocks++;
195 mark_info_dirty(info);
202 /* Insert empty block to the list */
203 static int put_free_dqblk(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
205 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
208 dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_blk);
209 dh->dqdh_prev_free = cpu_to_le32(0);
210 dh->dqdh_entries = cpu_to_le16(0);
211 info->u.v2_i.dqi_free_blk = blk;
212 mark_info_dirty(info);
213 if ((err = write_blk(filp, blk, buf)) < 0) /* Some strange block. We had better leave it... */
218 /* Remove given block from the list of blocks with free entries */
219 static int remove_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
221 dqbuf_t tmpbuf = getdqbuf();
222 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
223 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk = le32_to_cpu(dh->dqdh_prev_free);
229 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
231 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = dh->dqdh_prev_free;
232 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
236 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
238 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_next_free = dh->dqdh_next_free;
239 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
243 info->u.v2_i.dqi_free_entry = nextblk;
244 mark_info_dirty(info);
247 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
248 if (write_blk(filp, blk, buf) < 0) /* No matter whether write succeeds block is out of list */
249 printk(KERN_ERR "VFS: Can't write block (%u) with free entries.\n", blk);
256 /* Insert given block to the beginning of list with free entries */
257 static int insert_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
259 dqbuf_t tmpbuf = getdqbuf();
260 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
265 dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_entry);
266 dh->dqdh_prev_free = cpu_to_le32(0);
267 if ((err = write_blk(filp, blk, buf)) < 0)
269 if (info->u.v2_i.dqi_free_entry) {
270 if ((err = read_blk(filp, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
272 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = cpu_to_le32(blk);
273 if ((err = write_blk(filp, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
277 info->u.v2_i.dqi_free_entry = blk;
278 mark_info_dirty(info);
285 /* Find space for dquot */
286 static uint find_free_dqentry(struct dquot *dquot, int *err)
288 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
289 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info+dquot->dq_type;
291 struct v2_disk_dqdbheader *dh;
292 struct v2_disk_dqblk *ddquot;
293 struct v2_disk_dqblk fakedquot;
297 if (!(buf = getdqbuf())) {
301 dh = (struct v2_disk_dqdbheader *)buf;
302 ddquot = GETENTRIES(buf);
303 if (info->u.v2_i.dqi_free_entry) {
304 blk = info->u.v2_i.dqi_free_entry;
305 if ((*err = read_blk(filp, blk, buf)) < 0)
309 blk = get_free_dqblk(filp, info);
314 memset(buf, 0, V2_DQBLKSIZE);
315 info->u.v2_i.dqi_free_entry = blk; /* This is enough as block is already zeroed and entry list is empty... */
316 mark_info_dirty(info);
318 if (le16_to_cpu(dh->dqdh_entries)+1 >= V2_DQSTRINBLK) /* Block will be full? */
319 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
320 printk(KERN_ERR "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n", blk);
323 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)+1);
324 memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
325 /* Find free structure in block */
326 for (i = 0; i < V2_DQSTRINBLK && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)); i++);
327 #ifdef __QUOTA_V2_PARANOIA
328 if (i == V2_DQSTRINBLK) {
329 printk(KERN_ERR "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
334 if ((*err = write_blk(filp, blk, buf)) < 0) {
335 printk(KERN_ERR "VFS: find_free_dqentry(): Can't write quota data block %u.\n", blk);
338 dquot->dq_off = (blk<<V2_DQBLKSIZE_BITS)+sizeof(struct v2_disk_dqdbheader)+i*sizeof(struct v2_disk_dqblk);
346 /* Insert reference to structure into the trie */
347 static int do_insert_tree(struct dquot *dquot, uint *treeblk, int depth)
349 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
350 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
352 int ret = 0, newson = 0, newact = 0;
356 if (!(buf = getdqbuf()))
359 ret = get_free_dqblk(filp, info);
363 memset(buf, 0, V2_DQBLKSIZE);
367 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
368 printk(KERN_ERR "VFS: Can't read tree quota block %u.\n", *treeblk);
373 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
376 if (depth == V2_DQTREEDEPTH-1) {
377 #ifdef __QUOTA_V2_PARANOIA
379 printk(KERN_ERR "VFS: Inserting already present quota entry (block %u).\n", ref[GETIDINDEX(dquot->dq_id, depth)]);
384 newblk = find_free_dqentry(dquot, &ret);
387 ret = do_insert_tree(dquot, &newblk, depth+1);
388 if (newson && ret >= 0) {
389 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
390 ret = write_blk(filp, *treeblk, buf);
392 else if (newact && ret < 0)
393 put_free_dqblk(filp, info, buf, *treeblk);
399 /* Wrapper for inserting quota structure into tree */
400 static inline int dq_insert_tree(struct dquot *dquot)
402 int tmp = V2_DQTREEOFF;
403 return do_insert_tree(dquot, &tmp, 0);
407 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
409 static int v2_write_dquot(struct dquot *dquot)
411 int type = dquot->dq_type;
416 struct v2_disk_dqblk ddquot;
419 if ((ret = dq_insert_tree(dquot)) < 0) {
420 printk(KERN_ERR "VFS: Error %d occured while creating quota.\n", ret);
423 filp = sb_dqopt(dquot->dq_sb)->files[type];
424 offset = dquot->dq_off;
425 mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id);
428 ret = filp->f_op->write(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset);
430 if (ret != sizeof(struct v2_disk_dqblk)) {
431 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n", kdevname(dquot->dq_dev));
441 /* Free dquot entry in data block */
442 static int free_dqentry(struct dquot *dquot, uint blk)
444 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
445 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
446 struct v2_disk_dqdbheader *dh;
447 dqbuf_t buf = getdqbuf();
452 if (dquot->dq_off >> V2_DQBLKSIZE_BITS != blk) {
453 printk(KERN_ERR "VFS: Quota structure has offset to other block (%u) than it should (%u).\n", blk, (uint)(dquot->dq_off >> V2_DQBLKSIZE_BITS));
456 if ((ret = read_blk(filp, blk, buf)) < 0) {
457 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
460 dh = (struct v2_disk_dqdbheader *)buf;
461 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)-1);
462 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
463 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
464 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
465 printk(KERN_ERR "VFS: Can't move quota data block (%u) to free list.\n", blk);
470 memset(buf+(dquot->dq_off & ((1 << V2_DQBLKSIZE_BITS)-1)), 0, sizeof(struct v2_disk_dqblk));
471 if (le16_to_cpu(dh->dqdh_entries) == V2_DQSTRINBLK-1) {
472 /* Insert will write block itself */
473 if ((ret = insert_free_dqentry(filp, info, buf, blk)) < 0) {
474 printk(KERN_ERR "VFS: Can't insert quota data block (%u) to free entry list.\n", blk);
479 if ((ret = write_blk(filp, blk, buf)) < 0) {
480 printk(KERN_ERR "VFS: Can't write quota data block %u\n", blk);
484 dquot->dq_off = 0; /* Quota is now unattached */
490 /* Remove reference to dquot from tree */
491 static int remove_tree(struct dquot *dquot, uint *blk, int depth)
493 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
494 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
495 dqbuf_t buf = getdqbuf();
498 u32 *ref = (u32 *)buf;
502 if ((ret = read_blk(filp, *blk, buf)) < 0) {
503 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
506 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
507 if (depth == V2_DQTREEDEPTH-1) {
508 ret = free_dqentry(dquot, newblk);
512 ret = remove_tree(dquot, &newblk, depth+1);
513 if (ret >= 0 && !newblk) {
515 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
516 for (i = 0; i < V2_DQBLKSIZE && !buf[i]; i++); /* Block got empty? */
517 if (i == V2_DQBLKSIZE) {
518 put_free_dqblk(filp, info, buf, *blk);
522 if ((ret = write_blk(filp, *blk, buf)) < 0)
523 printk(KERN_ERR "VFS: Can't write quota tree block %u.\n", *blk);
530 /* Delete dquot from tree */
531 static int v2_delete_dquot(struct dquot *dquot)
533 uint tmp = V2_DQTREEOFF;
535 if (!dquot->dq_off) /* Even not allocated? */
537 return remove_tree(dquot, &tmp, 0);
540 /* Find entry in block */
541 static loff_t find_block_dqentry(struct dquot *dquot, uint blk)
543 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
544 dqbuf_t buf = getdqbuf();
547 struct v2_disk_dqblk *ddquot = GETENTRIES(buf);
551 if ((ret = read_blk(filp, blk, buf)) < 0) {
552 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
556 for (i = 0; i < V2_DQSTRINBLK && le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++);
557 else { /* ID 0 as a bit more complicated searching... */
558 struct v2_disk_dqblk fakedquot;
560 memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
561 for (i = 0; i < V2_DQSTRINBLK; i++)
562 if (!le32_to_cpu(ddquot[i].dqb_id) && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)))
565 if (i == V2_DQSTRINBLK) {
566 printk(KERN_ERR "VFS: Quota for id %u referenced but not present.\n", dquot->dq_id);
571 ret = (blk << V2_DQBLKSIZE_BITS) + sizeof(struct v2_disk_dqdbheader) + i * sizeof(struct v2_disk_dqblk);
577 /* Find entry for given id in the tree */
578 static loff_t find_tree_dqentry(struct dquot *dquot, uint blk, int depth)
580 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
581 dqbuf_t buf = getdqbuf();
583 u32 *ref = (u32 *)buf;
587 if ((ret = read_blk(filp, blk, buf)) < 0) {
588 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
592 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
593 if (!blk) /* No reference? */
595 if (depth < V2_DQTREEDEPTH-1)
596 ret = find_tree_dqentry(dquot, blk, depth+1);
598 ret = find_block_dqentry(dquot, blk);
604 /* Find entry for given id in the tree - wrapper function */
605 static inline loff_t find_dqentry(struct dquot *dquot)
607 return find_tree_dqentry(dquot, V2_DQTREEOFF, 0);
610 static int v2_read_dquot(struct dquot *dquot)
612 int type = dquot->dq_type;
616 struct v2_disk_dqblk ddquot;
619 filp = sb_dqopt(dquot->dq_sb)->files[type];
621 #ifdef __QUOTA_V2_PARANOIA
622 if (!filp || !dquot->dq_sb) { /* Invalidated quota? */
623 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
627 offset = find_dqentry(dquot);
628 if (offset <= 0) { /* Entry not present? */
630 printk(KERN_ERR "VFS: Can't read quota structure for id %u.\n", dquot->dq_id);
632 dquot->dq_flags |= DQ_FAKE;
633 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
637 dquot->dq_off = offset;
640 if ((ret = filp->f_op->read(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset)) != sizeof(struct v2_disk_dqblk)) {
643 printk(KERN_ERR "VFS: Error while reading quota structure for id %u.\n", dquot->dq_id);
644 memset(&ddquot, 0, sizeof(struct v2_disk_dqblk));
649 disk2memdqb(&dquot->dq_dqb, &ddquot);
655 /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake one and user has no blocks... */
656 static int v2_commit_dquot(struct dquot *dquot)
658 /* We clear the flag everytime so we don't loop when there was an IO error... */
659 dquot->dq_flags &= ~DQ_MOD;
660 if (dquot->dq_flags & DQ_FAKE && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
661 return v2_delete_dquot(dquot);
663 return v2_write_dquot(dquot);
666 static struct quota_format_ops v2_format_ops = {
667 check_quota_file: v2_check_quota_file,
668 read_file_info: v2_read_file_info,
669 write_file_info: v2_write_file_info,
670 free_file_info: NULL,
671 read_dqblk: v2_read_dquot,
672 commit_dqblk: v2_commit_dquot,
675 static struct quota_format_type v2_quota_format = {
676 qf_fmt_id: QFMT_VFS_V0,
677 qf_ops: &v2_format_ops,
678 qf_owner: THIS_MODULE
681 static int __init init_v2_quota_format(void)
683 return register_quota_format(&v2_quota_format);
686 static void __exit exit_v2_quota_format(void)
688 unregister_quota_format(&v2_quota_format);
693 module_init(init_v2_quota_format);
694 module_exit(exit_v2_quota_format);