1 // SPDX-License-Identifier: GPL-2.0-only
3 * vfsv0 quota IO operations on file
6 #include <linux/errno.h>
8 #include <linux/mount.h>
9 #include <linux/dqblk_v2.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/quotaops.h>
16 #include <asm/byteorder.h>
18 #include "quota_tree.h"
20 MODULE_AUTHOR("Jan Kara");
21 MODULE_DESCRIPTION("Quota trie support");
22 MODULE_LICENSE("GPL");
25 * Maximum quota tree depth we support. Only to limit recursion when working
28 #define MAX_QTREE_DEPTH 6
30 #define __QUOTA_QT_PARANOIA
32 static int __get_index(struct qtree_mem_dqinfo *info, qid_t id, int depth)
34 unsigned int epb = info->dqi_usable_bs >> 2;
36 depth = info->dqi_qtree_depth - depth - 1;
42 static int get_index(struct qtree_mem_dqinfo *info, struct kqid qid, int depth)
44 qid_t id = from_kqid(&init_user_ns, qid);
46 return __get_index(info, id, depth);
49 /* Number of entries in one blocks */
50 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
52 return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
53 / info->dqi_entry_size;
56 static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
58 struct super_block *sb = info->dqi_sb;
60 memset(buf, 0, info->dqi_usable_bs);
61 return sb->s_op->quota_read(sb, info->dqi_type, buf,
62 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
65 static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
67 struct super_block *sb = info->dqi_sb;
70 ret = sb->s_op->quota_write(sb, info->dqi_type, buf,
71 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
72 if (ret != info->dqi_usable_bs) {
73 quota_error(sb, "dquota write failed");
80 static inline int do_check_range(struct super_block *sb, const char *val_name,
81 uint val, uint min_val, uint max_val)
83 if (val < min_val || val > max_val) {
84 quota_error(sb, "Getting %s %u out of range %u-%u",
85 val_name, val, min_val, max_val);
92 static int check_dquot_block_header(struct qtree_mem_dqinfo *info,
93 struct qt_disk_dqdbheader *dh)
97 err = do_check_range(info->dqi_sb, "dqdh_next_free",
98 le32_to_cpu(dh->dqdh_next_free), 0,
99 info->dqi_blocks - 1);
102 err = do_check_range(info->dqi_sb, "dqdh_prev_free",
103 le32_to_cpu(dh->dqdh_prev_free), 0,
104 info->dqi_blocks - 1);
107 err = do_check_range(info->dqi_sb, "dqdh_entries",
108 le16_to_cpu(dh->dqdh_entries), 0,
109 qtree_dqstr_in_blk(info));
114 /* Remove empty block from list and return it */
115 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
117 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
118 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
123 if (info->dqi_free_blk) {
124 blk = info->dqi_free_blk;
125 ret = read_blk(info, blk, buf);
128 ret = check_dquot_block_header(info, dh);
131 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
134 memset(buf, 0, info->dqi_usable_bs);
135 /* Assure block allocation... */
136 ret = write_blk(info, info->dqi_blocks, buf);
139 blk = info->dqi_blocks++;
141 mark_info_dirty(info->dqi_sb, info->dqi_type);
148 /* Insert empty block to the list */
149 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
151 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
154 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
155 dh->dqdh_prev_free = cpu_to_le32(0);
156 dh->dqdh_entries = cpu_to_le16(0);
157 err = write_blk(info, blk, buf);
160 info->dqi_free_blk = blk;
161 mark_info_dirty(info->dqi_sb, info->dqi_type);
165 /* Remove given block from the list of blocks with free entries */
166 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
169 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
170 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
171 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
172 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
178 err = read_blk(info, nextblk, tmpbuf);
181 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
183 err = write_blk(info, nextblk, tmpbuf);
188 err = read_blk(info, prevblk, tmpbuf);
191 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
193 err = write_blk(info, prevblk, tmpbuf);
197 info->dqi_free_entry = nextblk;
198 mark_info_dirty(info->dqi_sb, info->dqi_type);
201 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
202 /* No matter whether write succeeds block is out of list */
203 if (write_blk(info, blk, buf) < 0)
204 quota_error(info->dqi_sb, "Can't write block (%u) "
205 "with free entries", blk);
212 /* Insert given block to the beginning of list with free entries */
213 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
216 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
217 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
222 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
223 dh->dqdh_prev_free = cpu_to_le32(0);
224 err = write_blk(info, blk, buf);
227 if (info->dqi_free_entry) {
228 err = read_blk(info, info->dqi_free_entry, tmpbuf);
231 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
233 err = write_blk(info, info->dqi_free_entry, tmpbuf);
238 info->dqi_free_entry = blk;
239 mark_info_dirty(info->dqi_sb, info->dqi_type);
246 /* Is the entry in the block free? */
247 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
251 for (i = 0; i < info->dqi_entry_size; i++)
256 EXPORT_SYMBOL(qtree_entry_unused);
258 /* Find space for dquot */
259 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
260 struct dquot *dquot, int *err)
263 struct qt_disk_dqdbheader *dh;
264 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
272 dh = (struct qt_disk_dqdbheader *)buf;
273 if (info->dqi_free_entry) {
274 blk = info->dqi_free_entry;
275 *err = read_blk(info, blk, buf);
278 *err = check_dquot_block_header(info, dh);
282 blk = get_free_dqblk(info);
288 memset(buf, 0, info->dqi_usable_bs);
289 /* This is enough as the block is already zeroed and the entry
290 * list is empty... */
291 info->dqi_free_entry = blk;
292 mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
294 /* Block will be full? */
295 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
296 *err = remove_free_dqentry(info, buf, blk);
298 quota_error(dquot->dq_sb, "Can't remove block (%u) "
299 "from entry free list", blk);
303 le16_add_cpu(&dh->dqdh_entries, 1);
304 /* Find free structure in block */
305 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
306 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
307 if (qtree_entry_unused(info, ddquot))
309 ddquot += info->dqi_entry_size;
311 #ifdef __QUOTA_QT_PARANOIA
312 if (i == qtree_dqstr_in_blk(info)) {
313 quota_error(dquot->dq_sb, "Data block full but it shouldn't");
318 *err = write_blk(info, blk, buf);
320 quota_error(dquot->dq_sb, "Can't write quota data block %u",
324 dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
325 sizeof(struct qt_disk_dqdbheader) +
326 i * info->dqi_entry_size;
334 /* Insert reference to structure into the trie */
335 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
336 uint *blks, int depth)
338 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
339 int ret = 0, newson = 0, newact = 0;
347 ret = get_free_dqblk(info);
350 for (i = 0; i < depth; i++)
351 if (ret == blks[i]) {
352 quota_error(dquot->dq_sb,
353 "Free block already used in tree: block %u",
359 memset(buf, 0, info->dqi_usable_bs);
362 ret = read_blk(info, blks[depth], buf);
364 quota_error(dquot->dq_sb, "Can't read tree quota "
365 "block %u", blks[depth]);
370 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
371 ret = do_check_range(dquot->dq_sb, "block", newblk, 0,
372 info->dqi_blocks - 1);
378 for (i = 0; i <= depth; i++)
379 if (newblk == blks[i]) {
380 quota_error(dquot->dq_sb,
381 "Cycle in quota tree detected: block %u index %u",
383 get_index(info, dquot->dq_id, depth));
388 blks[depth + 1] = newblk;
389 if (depth == info->dqi_qtree_depth - 1) {
390 #ifdef __QUOTA_QT_PARANOIA
392 quota_error(dquot->dq_sb, "Inserting already present "
393 "quota entry (block %u)",
394 le32_to_cpu(ref[get_index(info,
395 dquot->dq_id, depth)]));
400 blks[depth + 1] = find_free_dqentry(info, dquot, &ret);
402 ret = do_insert_tree(info, dquot, blks, depth + 1);
404 if (newson && ret >= 0) {
405 ref[get_index(info, dquot->dq_id, depth)] =
406 cpu_to_le32(blks[depth + 1]);
407 ret = write_blk(info, blks[depth], buf);
408 } else if (newact && ret < 0) {
409 put_free_dqblk(info, buf, blks[depth]);
416 /* Wrapper for inserting quota structure into tree */
417 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
420 uint blks[MAX_QTREE_DEPTH] = { QT_TREEOFF };
422 #ifdef __QUOTA_QT_PARANOIA
423 if (info->dqi_blocks <= QT_TREEOFF) {
424 quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
428 if (info->dqi_qtree_depth >= MAX_QTREE_DEPTH) {
429 quota_error(dquot->dq_sb, "Quota tree depth too big!");
432 return do_insert_tree(info, dquot, blks, 0);
436 * We don't have to be afraid of deadlocks as we never have quotas on quota
439 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
441 int type = dquot->dq_id.type;
442 struct super_block *sb = dquot->dq_sb;
444 char *ddquot = kmalloc(info->dqi_entry_size, GFP_KERNEL);
449 /* dq_off is guarded by dqio_sem */
450 if (!dquot->dq_off) {
451 ret = dq_insert_tree(info, dquot);
453 quota_error(sb, "Error %zd occurred while creating "
459 spin_lock(&dquot->dq_dqb_lock);
460 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
461 spin_unlock(&dquot->dq_dqb_lock);
462 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
464 if (ret != info->dqi_entry_size) {
465 quota_error(sb, "dquota write failed");
471 dqstats_inc(DQST_WRITES);
476 EXPORT_SYMBOL(qtree_write_dquot);
478 /* Free dquot entry in data block */
479 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
482 struct qt_disk_dqdbheader *dh;
483 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
488 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
489 quota_error(dquot->dq_sb, "Quota structure has offset to "
490 "other block (%u) than it should (%u)", blk,
491 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
495 ret = read_blk(info, blk, buf);
497 quota_error(dquot->dq_sb, "Can't read quota data block %u",
501 dh = (struct qt_disk_dqdbheader *)buf;
502 ret = check_dquot_block_header(info, dh);
505 le16_add_cpu(&dh->dqdh_entries, -1);
506 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
507 ret = remove_free_dqentry(info, buf, blk);
509 ret = put_free_dqblk(info, buf, blk);
511 quota_error(dquot->dq_sb, "Can't move quota data block "
512 "(%u) to free list", blk);
517 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
518 0, info->dqi_entry_size);
519 if (le16_to_cpu(dh->dqdh_entries) ==
520 qtree_dqstr_in_blk(info) - 1) {
521 /* Insert will write block itself */
522 ret = insert_free_dqentry(info, buf, blk);
524 quota_error(dquot->dq_sb, "Can't insert quota "
525 "data block (%u) to free entry list", blk);
529 ret = write_blk(info, blk, buf);
531 quota_error(dquot->dq_sb, "Can't write quota "
532 "data block %u", blk);
537 dquot->dq_off = 0; /* Quota is now unattached */
543 /* Remove reference to dquot from tree */
544 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
545 uint *blks, int depth)
547 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
550 __le32 *ref = (__le32 *)buf;
555 ret = read_blk(info, blks[depth], buf);
557 quota_error(dquot->dq_sb, "Can't read quota data block %u",
561 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
562 ret = do_check_range(dquot->dq_sb, "block", newblk, QT_TREEOFF,
563 info->dqi_blocks - 1);
567 for (i = 0; i <= depth; i++)
568 if (newblk == blks[i]) {
569 quota_error(dquot->dq_sb,
570 "Cycle in quota tree detected: block %u index %u",
572 get_index(info, dquot->dq_id, depth));
576 if (depth == info->dqi_qtree_depth - 1) {
577 ret = free_dqentry(info, dquot, newblk);
580 blks[depth + 1] = newblk;
581 ret = remove_tree(info, dquot, blks, depth + 1);
583 if (ret >= 0 && !blks[depth + 1]) {
584 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
585 /* Block got empty? */
586 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
588 /* Don't put the root block into the free block list */
589 if (i == (info->dqi_usable_bs >> 2)
590 && blks[depth] != QT_TREEOFF) {
591 put_free_dqblk(info, buf, blks[depth]);
594 ret = write_blk(info, blks[depth], buf);
596 quota_error(dquot->dq_sb,
597 "Can't write quota tree block %u",
606 /* Delete dquot from tree */
607 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
609 uint blks[MAX_QTREE_DEPTH] = { QT_TREEOFF };
611 if (!dquot->dq_off) /* Even not allocated? */
613 if (info->dqi_qtree_depth >= MAX_QTREE_DEPTH) {
614 quota_error(dquot->dq_sb, "Quota tree depth too big!");
617 return remove_tree(info, dquot, blks, 0);
619 EXPORT_SYMBOL(qtree_delete_dquot);
621 /* Find entry in block */
622 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
623 struct dquot *dquot, uint blk)
625 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
632 ret = read_blk(info, blk, buf);
634 quota_error(dquot->dq_sb, "Can't read quota tree "
638 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
639 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
640 if (info->dqi_ops->is_id(ddquot, dquot))
642 ddquot += info->dqi_entry_size;
644 if (i == qtree_dqstr_in_blk(info)) {
645 quota_error(dquot->dq_sb,
646 "Quota for id %u referenced but not present",
647 from_kqid(&init_user_ns, dquot->dq_id));
651 ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
652 qt_disk_dqdbheader) + i * info->dqi_entry_size;
659 /* Find entry for given id in the tree */
660 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
661 struct dquot *dquot, uint *blks, int depth)
663 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
665 __le32 *ref = (__le32 *)buf;
671 ret = read_blk(info, blks[depth], buf);
673 quota_error(dquot->dq_sb, "Can't read quota tree block %u",
678 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
679 if (!blk) /* No reference? */
681 ret = do_check_range(dquot->dq_sb, "block", blk, QT_TREEOFF,
682 info->dqi_blocks - 1);
686 /* Check for cycles in the tree */
687 for (i = 0; i <= depth; i++)
688 if (blk == blks[i]) {
689 quota_error(dquot->dq_sb,
690 "Cycle in quota tree detected: block %u index %u",
692 get_index(info, dquot->dq_id, depth));
696 blks[depth + 1] = blk;
697 if (depth < info->dqi_qtree_depth - 1)
698 ret = find_tree_dqentry(info, dquot, blks, depth + 1);
700 ret = find_block_dqentry(info, dquot, blk);
706 /* Find entry for given id in the tree - wrapper function */
707 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
710 uint blks[MAX_QTREE_DEPTH] = { QT_TREEOFF };
712 if (info->dqi_qtree_depth >= MAX_QTREE_DEPTH) {
713 quota_error(dquot->dq_sb, "Quota tree depth too big!");
716 return find_tree_dqentry(info, dquot, blks, 0);
719 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
721 int type = dquot->dq_id.type;
722 struct super_block *sb = dquot->dq_sb;
727 #ifdef __QUOTA_QT_PARANOIA
728 /* Invalidated quota? */
729 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
730 quota_error(sb, "Quota invalidated while reading!");
734 /* Do we know offset of the dquot entry in the quota file? */
735 if (!dquot->dq_off) {
736 offset = find_dqentry(info, dquot);
737 if (offset <= 0) { /* Entry not present? */
739 quota_error(sb,"Can't read quota structure "
741 from_kqid(&init_user_ns,
744 set_bit(DQ_FAKE_B, &dquot->dq_flags);
745 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
749 dquot->dq_off = offset;
751 ddquot = kmalloc(info->dqi_entry_size, GFP_KERNEL);
754 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
756 if (ret != info->dqi_entry_size) {
759 quota_error(sb, "Error while reading quota structure for id %u",
760 from_kqid(&init_user_ns, dquot->dq_id));
761 set_bit(DQ_FAKE_B, &dquot->dq_flags);
762 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
766 spin_lock(&dquot->dq_dqb_lock);
767 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
768 if (!dquot->dq_dqb.dqb_bhardlimit &&
769 !dquot->dq_dqb.dqb_bsoftlimit &&
770 !dquot->dq_dqb.dqb_ihardlimit &&
771 !dquot->dq_dqb.dqb_isoftlimit)
772 set_bit(DQ_FAKE_B, &dquot->dq_flags);
773 spin_unlock(&dquot->dq_dqb_lock);
776 dqstats_inc(DQST_READS);
779 EXPORT_SYMBOL(qtree_read_dquot);
781 /* Check whether dquot should not be deleted. We know we are
782 * the only one operating on dquot (thanks to dq_lock) */
783 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
785 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
786 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
787 return qtree_delete_dquot(info, dquot);
790 EXPORT_SYMBOL(qtree_release_dquot);
792 static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
793 unsigned int blk, int depth)
795 char *buf = kmalloc(info->dqi_usable_bs, GFP_KERNEL);
796 __le32 *ref = (__le32 *)buf;
798 unsigned int epb = info->dqi_usable_bs >> 2;
799 unsigned int level_inc = 1;
805 for (i = depth; i < info->dqi_qtree_depth - 1; i++)
808 ret = read_blk(info, blk, buf);
810 quota_error(info->dqi_sb,
811 "Can't read quota tree block %u", blk);
814 for (i = __get_index(info, *id, depth); i < epb; i++) {
815 uint blk_no = le32_to_cpu(ref[i]);
821 ret = do_check_range(info->dqi_sb, "block", blk_no, 0,
822 info->dqi_blocks - 1);
825 if (depth == info->dqi_qtree_depth - 1) {
829 ret = find_next_id(info, id, blk_no, depth + 1);
842 int qtree_get_next_id(struct qtree_mem_dqinfo *info, struct kqid *qid)
844 qid_t id = from_kqid(&init_user_ns, *qid);
847 ret = find_next_id(info, &id, QT_TREEOFF, 0);
850 *qid = make_kqid(&init_user_ns, qid->type, id);
853 EXPORT_SYMBOL(qtree_get_next_id);