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, struct kqid qid, int depth)
27 unsigned int epb = info->dqi_usable_bs >> 2;
28 qid_t id = from_kqid(&init_user_ns, qid);
30 depth = info->dqi_qtree_depth - depth - 1;
36 /* Number of entries in one blocks */
37 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
39 return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
40 / info->dqi_entry_size;
43 static char *getdqbuf(size_t size)
45 char *buf = kmalloc(size, GFP_NOFS);
48 "VFS: Not enough memory for quota buffers.\n");
52 static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
54 struct super_block *sb = info->dqi_sb;
56 memset(buf, 0, info->dqi_usable_bs);
57 return sb->s_op->quota_read(sb, info->dqi_type, buf,
58 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
61 static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
63 struct super_block *sb = info->dqi_sb;
66 ret = sb->s_op->quota_write(sb, info->dqi_type, buf,
67 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
68 if (ret != info->dqi_usable_bs) {
69 quota_error(sb, "dquota write failed");
76 /* Remove empty block from list and return it */
77 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
79 char *buf = getdqbuf(info->dqi_usable_bs);
80 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
85 if (info->dqi_free_blk) {
86 blk = info->dqi_free_blk;
87 ret = read_blk(info, blk, buf);
90 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
93 memset(buf, 0, info->dqi_usable_bs);
94 /* Assure block allocation... */
95 ret = write_blk(info, info->dqi_blocks, buf);
98 blk = info->dqi_blocks++;
100 mark_info_dirty(info->dqi_sb, info->dqi_type);
107 /* Insert empty block to the list */
108 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
110 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
113 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
114 dh->dqdh_prev_free = cpu_to_le32(0);
115 dh->dqdh_entries = cpu_to_le16(0);
116 err = write_blk(info, blk, buf);
119 info->dqi_free_blk = blk;
120 mark_info_dirty(info->dqi_sb, info->dqi_type);
124 /* Remove given block from the list of blocks with free entries */
125 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
128 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
129 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
130 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
131 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
137 err = read_blk(info, nextblk, tmpbuf);
140 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
142 err = write_blk(info, nextblk, tmpbuf);
147 err = read_blk(info, prevblk, tmpbuf);
150 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
152 err = write_blk(info, prevblk, tmpbuf);
156 info->dqi_free_entry = nextblk;
157 mark_info_dirty(info->dqi_sb, info->dqi_type);
160 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
161 /* No matter whether write succeeds block is out of list */
162 if (write_blk(info, blk, buf) < 0)
163 quota_error(info->dqi_sb, "Can't write block (%u) "
164 "with free entries", blk);
171 /* Insert given block to the beginning of list with free entries */
172 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
175 char *tmpbuf = getdqbuf(info->dqi_usable_bs);
176 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
181 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
182 dh->dqdh_prev_free = cpu_to_le32(0);
183 err = write_blk(info, blk, buf);
186 if (info->dqi_free_entry) {
187 err = read_blk(info, info->dqi_free_entry, tmpbuf);
190 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
192 err = write_blk(info, info->dqi_free_entry, tmpbuf);
197 info->dqi_free_entry = blk;
198 mark_info_dirty(info->dqi_sb, info->dqi_type);
205 /* Is the entry in the block free? */
206 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
210 for (i = 0; i < info->dqi_entry_size; i++)
215 EXPORT_SYMBOL(qtree_entry_unused);
217 /* Find space for dquot */
218 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
219 struct dquot *dquot, int *err)
222 struct qt_disk_dqdbheader *dh;
223 char *buf = getdqbuf(info->dqi_usable_bs);
231 dh = (struct qt_disk_dqdbheader *)buf;
232 if (info->dqi_free_entry) {
233 blk = info->dqi_free_entry;
234 *err = read_blk(info, blk, buf);
238 blk = get_free_dqblk(info);
244 memset(buf, 0, info->dqi_usable_bs);
245 /* This is enough as the block is already zeroed and the entry
246 * list is empty... */
247 info->dqi_free_entry = blk;
248 mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
250 /* Block will be full? */
251 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
252 *err = remove_free_dqentry(info, buf, blk);
254 quota_error(dquot->dq_sb, "Can't remove block (%u) "
255 "from entry free list", blk);
259 le16_add_cpu(&dh->dqdh_entries, 1);
260 /* Find free structure in block */
261 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
262 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
263 if (qtree_entry_unused(info, ddquot))
265 ddquot += info->dqi_entry_size;
267 #ifdef __QUOTA_QT_PARANOIA
268 if (i == qtree_dqstr_in_blk(info)) {
269 quota_error(dquot->dq_sb, "Data block full but it shouldn't");
274 *err = write_blk(info, blk, buf);
276 quota_error(dquot->dq_sb, "Can't write quota data block %u",
280 dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
281 sizeof(struct qt_disk_dqdbheader) +
282 i * info->dqi_entry_size;
290 /* Insert reference to structure into the trie */
291 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
292 uint *treeblk, int depth)
294 char *buf = getdqbuf(info->dqi_usable_bs);
295 int ret = 0, newson = 0, newact = 0;
302 ret = get_free_dqblk(info);
306 memset(buf, 0, info->dqi_usable_bs);
309 ret = read_blk(info, *treeblk, buf);
311 quota_error(dquot->dq_sb, "Can't read tree quota "
312 "block %u", *treeblk);
317 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
320 if (depth == info->dqi_qtree_depth - 1) {
321 #ifdef __QUOTA_QT_PARANOIA
323 quota_error(dquot->dq_sb, "Inserting already present "
324 "quota entry (block %u)",
325 le32_to_cpu(ref[get_index(info,
326 dquot->dq_id, depth)]));
331 newblk = find_free_dqentry(info, dquot, &ret);
333 ret = do_insert_tree(info, dquot, &newblk, depth+1);
335 if (newson && ret >= 0) {
336 ref[get_index(info, dquot->dq_id, depth)] =
338 ret = write_blk(info, *treeblk, buf);
339 } else if (newact && ret < 0) {
340 put_free_dqblk(info, buf, *treeblk);
347 /* Wrapper for inserting quota structure into tree */
348 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
351 int tmp = QT_TREEOFF;
353 #ifdef __QUOTA_QT_PARANOIA
354 if (info->dqi_blocks <= QT_TREEOFF) {
355 quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
359 return do_insert_tree(info, dquot, &tmp, 0);
363 * We don't have to be afraid of deadlocks as we never have quotas on quota
366 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
368 int type = dquot->dq_id.type;
369 struct super_block *sb = dquot->dq_sb;
371 char *ddquot = getdqbuf(info->dqi_entry_size);
376 /* dq_off is guarded by dqio_mutex */
377 if (!dquot->dq_off) {
378 ret = dq_insert_tree(info, dquot);
380 quota_error(sb, "Error %zd occurred while creating "
386 spin_lock(&dq_data_lock);
387 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
388 spin_unlock(&dq_data_lock);
389 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
391 if (ret != info->dqi_entry_size) {
392 quota_error(sb, "dquota write failed");
398 dqstats_inc(DQST_WRITES);
403 EXPORT_SYMBOL(qtree_write_dquot);
405 /* Free dquot entry in data block */
406 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
409 struct qt_disk_dqdbheader *dh;
410 char *buf = getdqbuf(info->dqi_usable_bs);
415 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
416 quota_error(dquot->dq_sb, "Quota structure has offset to "
417 "other block (%u) than it should (%u)", blk,
418 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
422 ret = read_blk(info, blk, buf);
424 quota_error(dquot->dq_sb, "Can't read quota data block %u",
428 dh = (struct qt_disk_dqdbheader *)buf;
429 le16_add_cpu(&dh->dqdh_entries, -1);
430 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
431 ret = remove_free_dqentry(info, buf, blk);
433 ret = put_free_dqblk(info, buf, blk);
435 quota_error(dquot->dq_sb, "Can't move quota data block "
436 "(%u) to free list", blk);
441 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
442 0, info->dqi_entry_size);
443 if (le16_to_cpu(dh->dqdh_entries) ==
444 qtree_dqstr_in_blk(info) - 1) {
445 /* Insert will write block itself */
446 ret = insert_free_dqentry(info, buf, blk);
448 quota_error(dquot->dq_sb, "Can't insert quota "
449 "data block (%u) to free entry list", blk);
453 ret = write_blk(info, blk, buf);
455 quota_error(dquot->dq_sb, "Can't write quota "
456 "data block %u", blk);
461 dquot->dq_off = 0; /* Quota is now unattached */
467 /* Remove reference to dquot from tree */
468 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
469 uint *blk, int depth)
471 char *buf = getdqbuf(info->dqi_usable_bs);
474 __le32 *ref = (__le32 *)buf;
478 ret = read_blk(info, *blk, buf);
480 quota_error(dquot->dq_sb, "Can't read quota data block %u",
484 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
485 if (newblk < QT_TREEOFF || newblk >= info->dqi_blocks) {
486 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
487 newblk, info->dqi_blocks);
492 if (depth == info->dqi_qtree_depth - 1) {
493 ret = free_dqentry(info, dquot, newblk);
496 ret = remove_tree(info, dquot, &newblk, depth+1);
498 if (ret >= 0 && !newblk) {
500 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
501 /* Block got empty? */
502 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
504 /* Don't put the root block into the free block list */
505 if (i == (info->dqi_usable_bs >> 2)
506 && *blk != QT_TREEOFF) {
507 put_free_dqblk(info, buf, *blk);
510 ret = write_blk(info, *blk, buf);
512 quota_error(dquot->dq_sb,
513 "Can't write quota tree block %u",
522 /* Delete dquot from tree */
523 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
525 uint tmp = QT_TREEOFF;
527 if (!dquot->dq_off) /* Even not allocated? */
529 return remove_tree(info, dquot, &tmp, 0);
531 EXPORT_SYMBOL(qtree_delete_dquot);
533 /* Find entry in block */
534 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
535 struct dquot *dquot, uint blk)
537 char *buf = getdqbuf(info->dqi_usable_bs);
544 ret = read_blk(info, blk, buf);
546 quota_error(dquot->dq_sb, "Can't read quota tree "
550 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
551 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
552 if (info->dqi_ops->is_id(ddquot, dquot))
554 ddquot += info->dqi_entry_size;
556 if (i == qtree_dqstr_in_blk(info)) {
557 quota_error(dquot->dq_sb,
558 "Quota for id %u referenced but not present",
559 from_kqid(&init_user_ns, dquot->dq_id));
563 ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
564 qt_disk_dqdbheader) + i * info->dqi_entry_size;
571 /* Find entry for given id in the tree */
572 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
573 struct dquot *dquot, uint blk, int depth)
575 char *buf = getdqbuf(info->dqi_usable_bs);
577 __le32 *ref = (__le32 *)buf;
581 ret = read_blk(info, blk, buf);
583 quota_error(dquot->dq_sb, "Can't read quota tree block %u",
588 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
589 if (!blk) /* No reference? */
591 if (blk < QT_TREEOFF || blk >= info->dqi_blocks) {
592 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
593 blk, info->dqi_blocks);
598 if (depth < info->dqi_qtree_depth - 1)
599 ret = find_tree_dqentry(info, dquot, blk, depth+1);
601 ret = find_block_dqentry(info, dquot, blk);
607 /* Find entry for given id in the tree - wrapper function */
608 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
611 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
614 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
616 int type = dquot->dq_id.type;
617 struct super_block *sb = dquot->dq_sb;
622 #ifdef __QUOTA_QT_PARANOIA
623 /* Invalidated quota? */
624 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
625 quota_error(sb, "Quota invalidated while reading!");
629 /* Do we know offset of the dquot entry in the quota file? */
630 if (!dquot->dq_off) {
631 offset = find_dqentry(info, dquot);
632 if (offset <= 0) { /* Entry not present? */
634 quota_error(sb,"Can't read quota structure "
636 from_kqid(&init_user_ns,
639 set_bit(DQ_FAKE_B, &dquot->dq_flags);
640 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
644 dquot->dq_off = offset;
646 ddquot = getdqbuf(info->dqi_entry_size);
649 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
651 if (ret != info->dqi_entry_size) {
654 quota_error(sb, "Error while reading quota structure for id %u",
655 from_kqid(&init_user_ns, dquot->dq_id));
656 set_bit(DQ_FAKE_B, &dquot->dq_flags);
657 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
661 spin_lock(&dq_data_lock);
662 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
663 if (!dquot->dq_dqb.dqb_bhardlimit &&
664 !dquot->dq_dqb.dqb_bsoftlimit &&
665 !dquot->dq_dqb.dqb_ihardlimit &&
666 !dquot->dq_dqb.dqb_isoftlimit)
667 set_bit(DQ_FAKE_B, &dquot->dq_flags);
668 spin_unlock(&dq_data_lock);
671 dqstats_inc(DQST_READS);
674 EXPORT_SYMBOL(qtree_read_dquot);
676 /* Check whether dquot should not be deleted. We know we are
677 * the only one operating on dquot (thanks to dq_lock) */
678 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
680 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
681 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
682 return qtree_delete_dquot(info, dquot);
685 EXPORT_SYMBOL(qtree_release_dquot);