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"
19 MODULE_AUTHOR("Jan Kara");
20 MODULE_DESCRIPTION("Quota trie support");
21 MODULE_LICENSE("GPL");
23 #define __QUOTA_QT_PARANOIA
25 static int get_index(struct qtree_mem_dqinfo *info, qid_t id, int depth)
27 unsigned int epb = info->dqi_usable_bs >> 2;
29 depth = info->dqi_qtree_depth - depth - 1;
35 /* Number of entries in one blocks */
36 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
38 return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
39 / info->dqi_entry_size;
42 static char *getdqbuf(size_t size)
44 char *buf = kmalloc(size, GFP_NOFS);
47 "VFS: Not enough memory for quota buffers.\n");
51 static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
53 struct super_block *sb = info->dqi_sb;
55 memset(buf, 0, info->dqi_usable_bs);
56 return sb->s_op->quota_read(sb, info->dqi_type, buf,
57 info->dqi_usable_bs, blk << info->dqi_blocksize_bits);
60 static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
62 struct super_block *sb = info->dqi_sb;
64 return sb->s_op->quota_write(sb, info->dqi_type, buf,
65 info->dqi_usable_bs, blk << info->dqi_blocksize_bits);
68 /* Remove empty block from list and return it */
69 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
71 char *buf = getdqbuf(info->dqi_usable_bs);
72 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
77 if (info->dqi_free_blk) {
78 blk = info->dqi_free_blk;
79 ret = read_blk(info, blk, buf);
82 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
85 memset(buf, 0, info->dqi_usable_bs);
86 /* Assure block allocation... */
87 ret = write_blk(info, info->dqi_blocks, buf);
90 blk = info->dqi_blocks++;
92 mark_info_dirty(info->dqi_sb, info->dqi_type);
99 /* Insert empty block to the list */
100 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
102 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
105 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
106 dh->dqdh_prev_free = cpu_to_le32(0);
107 dh->dqdh_entries = cpu_to_le16(0);
108 err = write_blk(info, blk, buf);
111 info->dqi_free_blk = blk;
112 mark_info_dirty(info->dqi_sb, info->dqi_type);
116 /* Remove given block from the list of blocks with free entries */
117 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
120 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
121 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
122 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
123 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
129 err = read_blk(info, nextblk, tmpbuf);
132 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
134 err = write_blk(info, nextblk, tmpbuf);
139 err = read_blk(info, prevblk, tmpbuf);
142 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
144 err = write_blk(info, prevblk, tmpbuf);
148 info->dqi_free_entry = nextblk;
149 mark_info_dirty(info->dqi_sb, info->dqi_type);
152 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
153 /* No matter whether write succeeds block is out of list */
154 if (write_blk(info, blk, buf) < 0)
156 "VFS: Can't write block (%u) with free entries.\n",
164 /* Insert given block to the beginning of list with free entries */
165 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
168 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
169 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
174 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
175 dh->dqdh_prev_free = cpu_to_le32(0);
176 err = write_blk(info, blk, buf);
179 if (info->dqi_free_entry) {
180 err = read_blk(info, info->dqi_free_entry, tmpbuf);
183 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
185 err = write_blk(info, info->dqi_free_entry, tmpbuf);
190 info->dqi_free_entry = blk;
191 mark_info_dirty(info->dqi_sb, info->dqi_type);
198 /* Is the entry in the block free? */
199 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
203 for (i = 0; i < info->dqi_entry_size; i++)
208 EXPORT_SYMBOL(qtree_entry_unused);
210 /* Find space for dquot */
211 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
212 struct dquot *dquot, int *err)
215 struct qt_disk_dqdbheader *dh;
216 char *buf = getdqbuf(info->dqi_usable_bs);
224 dh = (struct qt_disk_dqdbheader *)buf;
225 if (info->dqi_free_entry) {
226 blk = info->dqi_free_entry;
227 *err = read_blk(info, blk, buf);
231 blk = get_free_dqblk(info);
237 memset(buf, 0, info->dqi_usable_bs);
238 /* This is enough as the block is already zeroed and the entry
239 * list is empty... */
240 info->dqi_free_entry = blk;
241 mark_info_dirty(dquot->dq_sb, dquot->dq_type);
243 /* Block will be full? */
244 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
245 *err = remove_free_dqentry(info, buf, blk);
247 printk(KERN_ERR "VFS: find_free_dqentry(): Can't "
248 "remove block (%u) from entry free list.\n",
253 le16_add_cpu(&dh->dqdh_entries, 1);
254 /* Find free structure in block */
255 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
256 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
257 if (qtree_entry_unused(info, ddquot))
259 ddquot += info->dqi_entry_size;
261 #ifdef __QUOTA_QT_PARANOIA
262 if (i == qtree_dqstr_in_blk(info)) {
263 printk(KERN_ERR "VFS: find_free_dqentry(): Data block full "
264 "but it shouldn't.\n");
269 *err = write_blk(info, blk, buf);
271 printk(KERN_ERR "VFS: find_free_dqentry(): Can't write quota "
272 "data block %u.\n", blk);
275 dquot->dq_off = (blk << info->dqi_blocksize_bits) +
276 sizeof(struct qt_disk_dqdbheader) +
277 i * info->dqi_entry_size;
285 /* Insert reference to structure into the trie */
286 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
287 uint *treeblk, int depth)
289 char *buf = getdqbuf(info->dqi_usable_bs);
290 int ret = 0, newson = 0, newact = 0;
297 ret = get_free_dqblk(info);
301 memset(buf, 0, info->dqi_usable_bs);
304 ret = read_blk(info, *treeblk, buf);
306 printk(KERN_ERR "VFS: Can't read tree quota block "
312 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
315 if (depth == info->dqi_qtree_depth - 1) {
316 #ifdef __QUOTA_QT_PARANOIA
318 printk(KERN_ERR "VFS: Inserting already present quota "
319 "entry (block %u).\n",
320 le32_to_cpu(ref[get_index(info,
321 dquot->dq_id, depth)]));
326 newblk = find_free_dqentry(info, dquot, &ret);
328 ret = do_insert_tree(info, dquot, &newblk, depth+1);
330 if (newson && ret >= 0) {
331 ref[get_index(info, dquot->dq_id, depth)] =
333 ret = write_blk(info, *treeblk, buf);
334 } else if (newact && ret < 0) {
335 put_free_dqblk(info, buf, *treeblk);
342 /* Wrapper for inserting quota structure into tree */
343 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
346 int tmp = QT_TREEOFF;
347 return do_insert_tree(info, dquot, &tmp, 0);
351 * We don't have to be afraid of deadlocks as we never have quotas on quota
354 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
356 int type = dquot->dq_type;
357 struct super_block *sb = dquot->dq_sb;
359 char *ddquot = getdqbuf(info->dqi_entry_size);
364 /* dq_off is guarded by dqio_mutex */
365 if (!dquot->dq_off) {
366 ret = dq_insert_tree(info, dquot);
368 printk(KERN_ERR "VFS: Error %zd occurred while "
369 "creating quota.\n", ret);
374 spin_lock(&dq_data_lock);
375 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
376 spin_unlock(&dq_data_lock);
377 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
379 if (ret != info->dqi_entry_size) {
380 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
392 EXPORT_SYMBOL(qtree_write_dquot);
394 /* Free dquot entry in data block */
395 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
398 struct qt_disk_dqdbheader *dh;
399 char *buf = getdqbuf(info->dqi_usable_bs);
404 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
405 printk(KERN_ERR "VFS: Quota structure has offset to other "
406 "block (%u) than it should (%u).\n", blk,
407 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
410 ret = read_blk(info, blk, buf);
412 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
415 dh = (struct qt_disk_dqdbheader *)buf;
416 le16_add_cpu(&dh->dqdh_entries, -1);
417 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
418 ret = remove_free_dqentry(info, buf, blk);
420 ret = put_free_dqblk(info, buf, blk);
422 printk(KERN_ERR "VFS: Can't move quota data block (%u) "
423 "to free list.\n", blk);
428 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
429 0, info->dqi_entry_size);
430 if (le16_to_cpu(dh->dqdh_entries) ==
431 qtree_dqstr_in_blk(info) - 1) {
432 /* Insert will write block itself */
433 ret = insert_free_dqentry(info, buf, blk);
435 printk(KERN_ERR "VFS: Can't insert quota data "
436 "block (%u) to free entry list.\n", blk);
440 ret = write_blk(info, blk, buf);
442 printk(KERN_ERR "VFS: Can't write quota data "
448 dquot->dq_off = 0; /* Quota is now unattached */
454 /* Remove reference to dquot from tree */
455 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
456 uint *blk, int depth)
458 char *buf = getdqbuf(info->dqi_usable_bs);
461 __le32 *ref = (__le32 *)buf;
465 ret = read_blk(info, *blk, buf);
467 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
470 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
471 if (depth == info->dqi_qtree_depth - 1) {
472 ret = free_dqentry(info, dquot, newblk);
475 ret = remove_tree(info, dquot, &newblk, depth+1);
477 if (ret >= 0 && !newblk) {
479 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
480 /* Block got empty? */
481 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
483 /* Don't put the root block into the free block list */
484 if (i == (info->dqi_usable_bs >> 2)
485 && *blk != QT_TREEOFF) {
486 put_free_dqblk(info, buf, *blk);
489 ret = write_blk(info, *blk, buf);
491 printk(KERN_ERR "VFS: Can't write quota tree "
492 "block %u.\n", *blk);
500 /* Delete dquot from tree */
501 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
503 uint tmp = QT_TREEOFF;
505 if (!dquot->dq_off) /* Even not allocated? */
507 return remove_tree(info, dquot, &tmp, 0);
509 EXPORT_SYMBOL(qtree_delete_dquot);
511 /* Find entry in block */
512 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
513 struct dquot *dquot, uint blk)
515 char *buf = getdqbuf(info->dqi_usable_bs);
522 ret = read_blk(info, blk, buf);
524 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
527 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
528 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
529 if (info->dqi_ops->is_id(ddquot, dquot))
531 ddquot += info->dqi_entry_size;
533 if (i == qtree_dqstr_in_blk(info)) {
534 printk(KERN_ERR "VFS: Quota for id %u referenced "
535 "but not present.\n", dquot->dq_id);
539 ret = (blk << info->dqi_blocksize_bits) + sizeof(struct
540 qt_disk_dqdbheader) + i * info->dqi_entry_size;
547 /* Find entry for given id in the tree */
548 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
549 struct dquot *dquot, uint blk, int depth)
551 char *buf = getdqbuf(info->dqi_usable_bs);
553 __le32 *ref = (__le32 *)buf;
557 ret = read_blk(info, blk, buf);
559 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
563 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
564 if (!blk) /* No reference? */
566 if (depth < info->dqi_qtree_depth - 1)
567 ret = find_tree_dqentry(info, dquot, blk, depth+1);
569 ret = find_block_dqentry(info, dquot, blk);
575 /* Find entry for given id in the tree - wrapper function */
576 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
579 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
582 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
584 int type = dquot->dq_type;
585 struct super_block *sb = dquot->dq_sb;
590 #ifdef __QUOTA_QT_PARANOIA
591 /* Invalidated quota? */
592 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
593 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
597 /* Do we know offset of the dquot entry in the quota file? */
598 if (!dquot->dq_off) {
599 offset = find_dqentry(info, dquot);
600 if (offset <= 0) { /* Entry not present? */
602 printk(KERN_ERR "VFS: Can't read quota "
603 "structure for id %u.\n", dquot->dq_id);
605 set_bit(DQ_FAKE_B, &dquot->dq_flags);
606 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
610 dquot->dq_off = offset;
612 ddquot = getdqbuf(info->dqi_entry_size);
615 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
617 if (ret != info->dqi_entry_size) {
620 printk(KERN_ERR "VFS: Error while reading quota "
621 "structure for id %u.\n", dquot->dq_id);
622 set_bit(DQ_FAKE_B, &dquot->dq_flags);
623 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
627 spin_lock(&dq_data_lock);
628 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
629 if (!dquot->dq_dqb.dqb_bhardlimit &&
630 !dquot->dq_dqb.dqb_bsoftlimit &&
631 !dquot->dq_dqb.dqb_ihardlimit &&
632 !dquot->dq_dqb.dqb_isoftlimit)
633 set_bit(DQ_FAKE_B, &dquot->dq_flags);
634 spin_unlock(&dq_data_lock);
640 EXPORT_SYMBOL(qtree_read_dquot);
642 /* Check whether dquot should not be deleted. We know we are
643 * the only one operating on dquot (thanks to dq_lock) */
644 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
646 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
647 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
648 return qtree_delete_dquot(info, dquot);
651 EXPORT_SYMBOL(qtree_release_dquot);