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);
46 printk(KERN_WARNING "VFS: Not enough memory for quota buffers.\n");
50 static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
52 struct super_block *sb = info->dqi_sb;
54 memset(buf, 0, info->dqi_usable_bs);
55 return sb->s_op->quota_read(sb, info->dqi_type, buf,
56 info->dqi_usable_bs, blk << info->dqi_blocksize_bits);
59 static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
61 struct super_block *sb = info->dqi_sb;
63 return sb->s_op->quota_write(sb, info->dqi_type, buf,
64 info->dqi_usable_bs, blk << info->dqi_blocksize_bits);
67 /* Remove empty block from list and return it */
68 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
70 char *buf = getdqbuf(info->dqi_usable_bs);
71 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
76 if (info->dqi_free_blk) {
77 blk = info->dqi_free_blk;
78 ret = read_blk(info, blk, buf);
81 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
84 memset(buf, 0, info->dqi_usable_bs);
85 /* Assure block allocation... */
86 ret = write_blk(info, info->dqi_blocks, buf);
89 blk = info->dqi_blocks++;
91 mark_info_dirty(info->dqi_sb, info->dqi_type);
98 /* Insert empty block to the list */
99 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
101 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
104 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
105 dh->dqdh_prev_free = cpu_to_le32(0);
106 dh->dqdh_entries = cpu_to_le16(0);
107 err = write_blk(info, blk, buf);
110 info->dqi_free_blk = blk;
111 mark_info_dirty(info->dqi_sb, info->dqi_type);
115 /* Remove given block from the list of blocks with free entries */
116 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf, uint blk)
118 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
119 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
120 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
121 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
127 err = read_blk(info, nextblk, tmpbuf);
130 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
132 err = write_blk(info, nextblk, tmpbuf);
137 err = read_blk(info, prevblk, tmpbuf);
140 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
142 err = write_blk(info, prevblk, tmpbuf);
146 info->dqi_free_entry = nextblk;
147 mark_info_dirty(info->dqi_sb, info->dqi_type);
150 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
151 /* No matter whether write succeeds block is out of list */
152 if (write_blk(info, blk, buf) < 0)
153 printk(KERN_ERR "VFS: Can't write block (%u) with free entries.\n", blk);
160 /* Insert given block to the beginning of list with free entries */
161 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf, uint blk)
163 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
164 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
169 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
170 dh->dqdh_prev_free = cpu_to_le32(0);
171 err = write_blk(info, blk, buf);
174 if (info->dqi_free_entry) {
175 err = read_blk(info, info->dqi_free_entry, tmpbuf);
178 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
180 err = write_blk(info, info->dqi_free_entry, tmpbuf);
185 info->dqi_free_entry = blk;
186 mark_info_dirty(info->dqi_sb, info->dqi_type);
193 /* Is the entry in the block free? */
194 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
198 for (i = 0; i < info->dqi_entry_size; i++)
203 EXPORT_SYMBOL(qtree_entry_unused);
205 /* Find space for dquot */
206 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
207 struct dquot *dquot, int *err)
210 struct qt_disk_dqdbheader *dh;
211 char *buf = getdqbuf(info->dqi_usable_bs);
219 dh = (struct qt_disk_dqdbheader *)buf;
220 if (info->dqi_free_entry) {
221 blk = info->dqi_free_entry;
222 *err = read_blk(info, blk, buf);
226 blk = get_free_dqblk(info);
232 memset(buf, 0, info->dqi_usable_bs);
233 /* This is enough as block is already zeroed and entry list is empty... */
234 info->dqi_free_entry = blk;
235 mark_info_dirty(dquot->dq_sb, dquot->dq_type);
237 /* Block will be full? */
238 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
239 *err = remove_free_dqentry(info, buf, blk);
241 printk(KERN_ERR "VFS: find_free_dqentry(): Can't "
242 "remove block (%u) from entry free list.\n",
247 le16_add_cpu(&dh->dqdh_entries, 1);
248 /* Find free structure in block */
249 for (i = 0, ddquot = buf + sizeof(struct qt_disk_dqdbheader);
250 i < qtree_dqstr_in_blk(info) && !qtree_entry_unused(info, ddquot);
251 i++, ddquot += info->dqi_entry_size)
253 #ifdef __QUOTA_QT_PARANOIA
254 if (i == qtree_dqstr_in_blk(info)) {
255 printk(KERN_ERR "VFS: find_free_dqentry(): Data block full "
256 "but it shouldn't.\n");
261 *err = write_blk(info, blk, buf);
263 printk(KERN_ERR "VFS: find_free_dqentry(): Can't write quota "
264 "data block %u.\n", blk);
267 dquot->dq_off = (blk << info->dqi_blocksize_bits) +
268 sizeof(struct qt_disk_dqdbheader) +
269 i * info->dqi_entry_size;
277 /* Insert reference to structure into the trie */
278 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
279 uint *treeblk, int depth)
281 char *buf = getdqbuf(info->dqi_usable_bs);
282 int ret = 0, newson = 0, newact = 0;
289 ret = get_free_dqblk(info);
293 memset(buf, 0, info->dqi_usable_bs);
296 ret = read_blk(info, *treeblk, buf);
298 printk(KERN_ERR "VFS: Can't read tree quota block "
304 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
307 if (depth == info->dqi_qtree_depth - 1) {
308 #ifdef __QUOTA_QT_PARANOIA
310 printk(KERN_ERR "VFS: Inserting already present quota "
311 "entry (block %u).\n",
312 le32_to_cpu(ref[get_index(info,
313 dquot->dq_id, depth)]));
318 newblk = find_free_dqentry(info, dquot, &ret);
320 ret = do_insert_tree(info, dquot, &newblk, depth+1);
322 if (newson && ret >= 0) {
323 ref[get_index(info, dquot->dq_id, depth)] =
325 ret = write_blk(info, *treeblk, buf);
326 } else if (newact && ret < 0) {
327 put_free_dqblk(info, buf, *treeblk);
334 /* Wrapper for inserting quota structure into tree */
335 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
338 int tmp = QT_TREEOFF;
339 return do_insert_tree(info, dquot, &tmp, 0);
343 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
345 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
347 int type = dquot->dq_type;
348 struct super_block *sb = dquot->dq_sb;
350 char *ddquot = getdqbuf(info->dqi_entry_size);
355 /* dq_off is guarded by dqio_mutex */
356 if (!dquot->dq_off) {
357 ret = dq_insert_tree(info, dquot);
359 printk(KERN_ERR "VFS: Error %zd occurred while "
360 "creating quota.\n", ret);
365 spin_lock(&dq_data_lock);
366 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
367 spin_unlock(&dq_data_lock);
368 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
370 if (ret != info->dqi_entry_size) {
371 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
383 EXPORT_SYMBOL(qtree_write_dquot);
385 /* Free dquot entry in data block */
386 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
389 struct qt_disk_dqdbheader *dh;
390 char *buf = getdqbuf(info->dqi_usable_bs);
395 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
396 printk(KERN_ERR "VFS: Quota structure has offset to other "
397 "block (%u) than it should (%u).\n", blk,
398 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
401 ret = read_blk(info, blk, buf);
403 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
406 dh = (struct qt_disk_dqdbheader *)buf;
407 le16_add_cpu(&dh->dqdh_entries, -1);
408 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
409 ret = remove_free_dqentry(info, buf, blk);
411 ret = put_free_dqblk(info, buf, blk);
413 printk(KERN_ERR "VFS: Can't move quota data block (%u) "
414 "to free list.\n", blk);
419 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
420 0, info->dqi_entry_size);
421 if (le16_to_cpu(dh->dqdh_entries) ==
422 qtree_dqstr_in_blk(info) - 1) {
423 /* Insert will write block itself */
424 ret = insert_free_dqentry(info, buf, blk);
426 printk(KERN_ERR "VFS: Can't insert quota data "
427 "block (%u) to free entry list.\n", blk);
431 ret = write_blk(info, blk, buf);
433 printk(KERN_ERR "VFS: Can't write quota data "
439 dquot->dq_off = 0; /* Quota is now unattached */
445 /* Remove reference to dquot from tree */
446 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
447 uint *blk, int depth)
449 char *buf = getdqbuf(info->dqi_usable_bs);
452 __le32 *ref = (__le32 *)buf;
456 ret = read_blk(info, *blk, buf);
458 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
461 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
462 if (depth == info->dqi_qtree_depth - 1) {
463 ret = free_dqentry(info, dquot, newblk);
466 ret = remove_tree(info, dquot, &newblk, depth+1);
468 if (ret >= 0 && !newblk) {
470 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
471 /* Block got empty? */
472 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
474 /* Don't put the root block into the free block list */
475 if (i == (info->dqi_usable_bs >> 2)
476 && *blk != QT_TREEOFF) {
477 put_free_dqblk(info, buf, *blk);
480 ret = write_blk(info, *blk, buf);
482 printk(KERN_ERR "VFS: Can't write quota tree "
483 "block %u.\n", *blk);
491 /* Delete dquot from tree */
492 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
494 uint tmp = QT_TREEOFF;
496 if (!dquot->dq_off) /* Even not allocated? */
498 return remove_tree(info, dquot, &tmp, 0);
500 EXPORT_SYMBOL(qtree_delete_dquot);
502 /* Find entry in block */
503 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
504 struct dquot *dquot, uint blk)
506 char *buf = getdqbuf(info->dqi_usable_bs);
513 ret = read_blk(info, blk, buf);
515 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
518 for (i = 0, ddquot = buf + sizeof(struct qt_disk_dqdbheader);
519 i < qtree_dqstr_in_blk(info) && !info->dqi_ops->is_id(ddquot, dquot);
520 i++, ddquot += info->dqi_entry_size)
522 if (i == qtree_dqstr_in_blk(info)) {
523 printk(KERN_ERR "VFS: Quota for id %u referenced "
524 "but not present.\n", dquot->dq_id);
528 ret = (blk << info->dqi_blocksize_bits) + sizeof(struct
529 qt_disk_dqdbheader) + i * info->dqi_entry_size;
536 /* Find entry for given id in the tree */
537 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
538 struct dquot *dquot, uint blk, int depth)
540 char *buf = getdqbuf(info->dqi_usable_bs);
542 __le32 *ref = (__le32 *)buf;
546 ret = read_blk(info, blk, buf);
548 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
552 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
553 if (!blk) /* No reference? */
555 if (depth < info->dqi_qtree_depth - 1)
556 ret = find_tree_dqentry(info, dquot, blk, depth+1);
558 ret = find_block_dqentry(info, dquot, blk);
564 /* Find entry for given id in the tree - wrapper function */
565 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
568 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
571 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
573 int type = dquot->dq_type;
574 struct super_block *sb = dquot->dq_sb;
579 #ifdef __QUOTA_QT_PARANOIA
580 /* Invalidated quota? */
581 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
582 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
586 /* Do we know offset of the dquot entry in the quota file? */
587 if (!dquot->dq_off) {
588 offset = find_dqentry(info, dquot);
589 if (offset <= 0) { /* Entry not present? */
591 printk(KERN_ERR "VFS: Can't read quota "
592 "structure for id %u.\n", dquot->dq_id);
594 set_bit(DQ_FAKE_B, &dquot->dq_flags);
595 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
599 dquot->dq_off = offset;
601 ddquot = getdqbuf(info->dqi_entry_size);
604 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
606 if (ret != info->dqi_entry_size) {
609 printk(KERN_ERR "VFS: Error while reading quota "
610 "structure for id %u.\n", dquot->dq_id);
611 set_bit(DQ_FAKE_B, &dquot->dq_flags);
612 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
616 spin_lock(&dq_data_lock);
617 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
618 if (!dquot->dq_dqb.dqb_bhardlimit &&
619 !dquot->dq_dqb.dqb_bsoftlimit &&
620 !dquot->dq_dqb.dqb_ihardlimit &&
621 !dquot->dq_dqb.dqb_isoftlimit)
622 set_bit(DQ_FAKE_B, &dquot->dq_flags);
623 spin_unlock(&dq_data_lock);
629 EXPORT_SYMBOL(qtree_read_dquot);
631 /* Check whether dquot should not be deleted. We know we are
632 * the only one operating on dquot (thanks to dq_lock) */
633 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
635 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
636 return qtree_delete_dquot(info, dquot);
639 EXPORT_SYMBOL(qtree_release_dquot);