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");
24 #define __QUOTA_QT_PARANOIA
26 static int __get_index(struct qtree_mem_dqinfo *info, qid_t id, int depth)
28 unsigned int epb = info->dqi_usable_bs >> 2;
30 depth = info->dqi_qtree_depth - depth - 1;
36 static int get_index(struct qtree_mem_dqinfo *info, struct kqid qid, int depth)
38 qid_t id = from_kqid(&init_user_ns, qid);
40 return __get_index(info, id, depth);
43 /* Number of entries in one blocks */
44 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
46 return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
47 / info->dqi_entry_size;
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, (loff_t)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;
64 ret = sb->s_op->quota_write(sb, info->dqi_type, buf,
65 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
66 if (ret != info->dqi_usable_bs) {
67 quota_error(sb, "dquota write failed");
74 static inline int do_check_range(struct super_block *sb, const char *val_name,
75 uint val, uint min_val, uint max_val)
77 if (val < min_val || val > max_val) {
78 quota_error(sb, "Getting %s %u out of range %u-%u",
79 val_name, val, min_val, max_val);
86 static int check_dquot_block_header(struct qtree_mem_dqinfo *info,
87 struct qt_disk_dqdbheader *dh)
91 err = do_check_range(info->dqi_sb, "dqdh_next_free",
92 le32_to_cpu(dh->dqdh_next_free), 0,
93 info->dqi_blocks - 1);
96 err = do_check_range(info->dqi_sb, "dqdh_prev_free",
97 le32_to_cpu(dh->dqdh_prev_free), 0,
98 info->dqi_blocks - 1);
101 err = do_check_range(info->dqi_sb, "dqdh_entries",
102 le16_to_cpu(dh->dqdh_entries), 0,
103 qtree_dqstr_in_blk(info));
108 /* Remove empty block from list and return it */
109 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
111 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
112 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
117 if (info->dqi_free_blk) {
118 blk = info->dqi_free_blk;
119 ret = read_blk(info, blk, buf);
122 ret = check_dquot_block_header(info, dh);
125 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
128 memset(buf, 0, info->dqi_usable_bs);
129 /* Assure block allocation... */
130 ret = write_blk(info, info->dqi_blocks, buf);
133 blk = info->dqi_blocks++;
135 mark_info_dirty(info->dqi_sb, info->dqi_type);
142 /* Insert empty block to the list */
143 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
145 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
148 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
149 dh->dqdh_prev_free = cpu_to_le32(0);
150 dh->dqdh_entries = cpu_to_le16(0);
151 err = write_blk(info, blk, buf);
154 info->dqi_free_blk = blk;
155 mark_info_dirty(info->dqi_sb, info->dqi_type);
159 /* Remove given block from the list of blocks with free entries */
160 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
163 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
164 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
165 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
166 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
172 err = read_blk(info, nextblk, tmpbuf);
175 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
177 err = write_blk(info, nextblk, tmpbuf);
182 err = read_blk(info, prevblk, tmpbuf);
185 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
187 err = write_blk(info, prevblk, tmpbuf);
191 info->dqi_free_entry = nextblk;
192 mark_info_dirty(info->dqi_sb, info->dqi_type);
195 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
196 /* No matter whether write succeeds block is out of list */
197 if (write_blk(info, blk, buf) < 0)
198 quota_error(info->dqi_sb, "Can't write block (%u) "
199 "with free entries", blk);
206 /* Insert given block to the beginning of list with free entries */
207 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
210 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
211 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
216 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
217 dh->dqdh_prev_free = cpu_to_le32(0);
218 err = write_blk(info, blk, buf);
221 if (info->dqi_free_entry) {
222 err = read_blk(info, info->dqi_free_entry, tmpbuf);
225 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
227 err = write_blk(info, info->dqi_free_entry, tmpbuf);
232 info->dqi_free_entry = blk;
233 mark_info_dirty(info->dqi_sb, info->dqi_type);
240 /* Is the entry in the block free? */
241 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
245 for (i = 0; i < info->dqi_entry_size; i++)
250 EXPORT_SYMBOL(qtree_entry_unused);
252 /* Find space for dquot */
253 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
254 struct dquot *dquot, int *err)
257 struct qt_disk_dqdbheader *dh;
258 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
266 dh = (struct qt_disk_dqdbheader *)buf;
267 if (info->dqi_free_entry) {
268 blk = info->dqi_free_entry;
269 *err = read_blk(info, blk, buf);
272 *err = check_dquot_block_header(info, dh);
276 blk = get_free_dqblk(info);
282 memset(buf, 0, info->dqi_usable_bs);
283 /* This is enough as the block is already zeroed and the entry
284 * list is empty... */
285 info->dqi_free_entry = blk;
286 mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
288 /* Block will be full? */
289 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
290 *err = remove_free_dqentry(info, buf, blk);
292 quota_error(dquot->dq_sb, "Can't remove block (%u) "
293 "from entry free list", blk);
297 le16_add_cpu(&dh->dqdh_entries, 1);
298 /* Find free structure in block */
299 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
300 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
301 if (qtree_entry_unused(info, ddquot))
303 ddquot += info->dqi_entry_size;
305 #ifdef __QUOTA_QT_PARANOIA
306 if (i == qtree_dqstr_in_blk(info)) {
307 quota_error(dquot->dq_sb, "Data block full but it shouldn't");
312 *err = write_blk(info, blk, buf);
314 quota_error(dquot->dq_sb, "Can't write quota data block %u",
318 dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
319 sizeof(struct qt_disk_dqdbheader) +
320 i * info->dqi_entry_size;
328 /* Insert reference to structure into the trie */
329 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
330 uint *treeblk, int depth)
332 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
333 int ret = 0, newson = 0, newact = 0;
340 ret = get_free_dqblk(info);
344 memset(buf, 0, info->dqi_usable_bs);
347 ret = read_blk(info, *treeblk, buf);
349 quota_error(dquot->dq_sb, "Can't read tree quota "
350 "block %u", *treeblk);
355 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
356 ret = do_check_range(dquot->dq_sb, "block", newblk, 0,
357 info->dqi_blocks - 1);
362 if (depth == info->dqi_qtree_depth - 1) {
363 #ifdef __QUOTA_QT_PARANOIA
365 quota_error(dquot->dq_sb, "Inserting already present "
366 "quota entry (block %u)",
367 le32_to_cpu(ref[get_index(info,
368 dquot->dq_id, depth)]));
373 newblk = find_free_dqentry(info, dquot, &ret);
375 ret = do_insert_tree(info, dquot, &newblk, depth+1);
377 if (newson && ret >= 0) {
378 ref[get_index(info, dquot->dq_id, depth)] =
380 ret = write_blk(info, *treeblk, buf);
381 } else if (newact && ret < 0) {
382 put_free_dqblk(info, buf, *treeblk);
389 /* Wrapper for inserting quota structure into tree */
390 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
393 int tmp = QT_TREEOFF;
395 #ifdef __QUOTA_QT_PARANOIA
396 if (info->dqi_blocks <= QT_TREEOFF) {
397 quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
401 return do_insert_tree(info, dquot, &tmp, 0);
405 * We don't have to be afraid of deadlocks as we never have quotas on quota
408 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
410 int type = dquot->dq_id.type;
411 struct super_block *sb = dquot->dq_sb;
413 char *ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
418 /* dq_off is guarded by dqio_sem */
419 if (!dquot->dq_off) {
420 ret = dq_insert_tree(info, dquot);
422 quota_error(sb, "Error %zd occurred while creating "
428 spin_lock(&dquot->dq_dqb_lock);
429 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
430 spin_unlock(&dquot->dq_dqb_lock);
431 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
433 if (ret != info->dqi_entry_size) {
434 quota_error(sb, "dquota write failed");
440 dqstats_inc(DQST_WRITES);
445 EXPORT_SYMBOL(qtree_write_dquot);
447 /* Free dquot entry in data block */
448 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
451 struct qt_disk_dqdbheader *dh;
452 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
457 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
458 quota_error(dquot->dq_sb, "Quota structure has offset to "
459 "other block (%u) than it should (%u)", blk,
460 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
464 ret = read_blk(info, blk, buf);
466 quota_error(dquot->dq_sb, "Can't read quota data block %u",
470 dh = (struct qt_disk_dqdbheader *)buf;
471 ret = check_dquot_block_header(info, dh);
474 le16_add_cpu(&dh->dqdh_entries, -1);
475 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
476 ret = remove_free_dqentry(info, buf, blk);
478 ret = put_free_dqblk(info, buf, blk);
480 quota_error(dquot->dq_sb, "Can't move quota data block "
481 "(%u) to free list", blk);
486 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
487 0, info->dqi_entry_size);
488 if (le16_to_cpu(dh->dqdh_entries) ==
489 qtree_dqstr_in_blk(info) - 1) {
490 /* Insert will write block itself */
491 ret = insert_free_dqentry(info, buf, blk);
493 quota_error(dquot->dq_sb, "Can't insert quota "
494 "data block (%u) to free entry list", blk);
498 ret = write_blk(info, blk, buf);
500 quota_error(dquot->dq_sb, "Can't write quota "
501 "data block %u", blk);
506 dquot->dq_off = 0; /* Quota is now unattached */
512 /* Remove reference to dquot from tree */
513 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
514 uint *blk, int depth)
516 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
519 __le32 *ref = (__le32 *)buf;
523 ret = read_blk(info, *blk, buf);
525 quota_error(dquot->dq_sb, "Can't read quota data block %u",
529 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
530 ret = do_check_range(dquot->dq_sb, "block", newblk, QT_TREEOFF,
531 info->dqi_blocks - 1);
535 if (depth == info->dqi_qtree_depth - 1) {
536 ret = free_dqentry(info, dquot, newblk);
539 ret = remove_tree(info, dquot, &newblk, depth+1);
541 if (ret >= 0 && !newblk) {
543 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
544 /* Block got empty? */
545 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
547 /* Don't put the root block into the free block list */
548 if (i == (info->dqi_usable_bs >> 2)
549 && *blk != QT_TREEOFF) {
550 put_free_dqblk(info, buf, *blk);
553 ret = write_blk(info, *blk, buf);
555 quota_error(dquot->dq_sb,
556 "Can't write quota tree block %u",
565 /* Delete dquot from tree */
566 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
568 uint tmp = QT_TREEOFF;
570 if (!dquot->dq_off) /* Even not allocated? */
572 return remove_tree(info, dquot, &tmp, 0);
574 EXPORT_SYMBOL(qtree_delete_dquot);
576 /* Find entry in block */
577 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
578 struct dquot *dquot, uint blk)
580 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
587 ret = read_blk(info, blk, buf);
589 quota_error(dquot->dq_sb, "Can't read quota tree "
593 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
594 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
595 if (info->dqi_ops->is_id(ddquot, dquot))
597 ddquot += info->dqi_entry_size;
599 if (i == qtree_dqstr_in_blk(info)) {
600 quota_error(dquot->dq_sb,
601 "Quota for id %u referenced but not present",
602 from_kqid(&init_user_ns, dquot->dq_id));
606 ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
607 qt_disk_dqdbheader) + i * info->dqi_entry_size;
614 /* Find entry for given id in the tree */
615 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
616 struct dquot *dquot, uint blk, int depth)
618 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
620 __le32 *ref = (__le32 *)buf;
624 ret = read_blk(info, blk, buf);
626 quota_error(dquot->dq_sb, "Can't read quota tree block %u",
631 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
632 if (!blk) /* No reference? */
634 ret = do_check_range(dquot->dq_sb, "block", blk, QT_TREEOFF,
635 info->dqi_blocks - 1);
639 if (depth < info->dqi_qtree_depth - 1)
640 ret = find_tree_dqentry(info, dquot, blk, depth+1);
642 ret = find_block_dqentry(info, dquot, blk);
648 /* Find entry for given id in the tree - wrapper function */
649 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
652 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
655 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
657 int type = dquot->dq_id.type;
658 struct super_block *sb = dquot->dq_sb;
663 #ifdef __QUOTA_QT_PARANOIA
664 /* Invalidated quota? */
665 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
666 quota_error(sb, "Quota invalidated while reading!");
670 /* Do we know offset of the dquot entry in the quota file? */
671 if (!dquot->dq_off) {
672 offset = find_dqentry(info, dquot);
673 if (offset <= 0) { /* Entry not present? */
675 quota_error(sb,"Can't read quota structure "
677 from_kqid(&init_user_ns,
680 set_bit(DQ_FAKE_B, &dquot->dq_flags);
681 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
685 dquot->dq_off = offset;
687 ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
690 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
692 if (ret != info->dqi_entry_size) {
695 quota_error(sb, "Error while reading quota structure for id %u",
696 from_kqid(&init_user_ns, dquot->dq_id));
697 set_bit(DQ_FAKE_B, &dquot->dq_flags);
698 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
702 spin_lock(&dquot->dq_dqb_lock);
703 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
704 if (!dquot->dq_dqb.dqb_bhardlimit &&
705 !dquot->dq_dqb.dqb_bsoftlimit &&
706 !dquot->dq_dqb.dqb_ihardlimit &&
707 !dquot->dq_dqb.dqb_isoftlimit)
708 set_bit(DQ_FAKE_B, &dquot->dq_flags);
709 spin_unlock(&dquot->dq_dqb_lock);
712 dqstats_inc(DQST_READS);
715 EXPORT_SYMBOL(qtree_read_dquot);
717 /* Check whether dquot should not be deleted. We know we are
718 * the only one operating on dquot (thanks to dq_lock) */
719 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
721 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
722 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
723 return qtree_delete_dquot(info, dquot);
726 EXPORT_SYMBOL(qtree_release_dquot);
728 static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
729 unsigned int blk, int depth)
731 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
732 __le32 *ref = (__le32 *)buf;
734 unsigned int epb = info->dqi_usable_bs >> 2;
735 unsigned int level_inc = 1;
741 for (i = depth; i < info->dqi_qtree_depth - 1; i++)
744 ret = read_blk(info, blk, buf);
746 quota_error(info->dqi_sb,
747 "Can't read quota tree block %u", blk);
750 for (i = __get_index(info, *id, depth); i < epb; i++) {
751 uint blk_no = le32_to_cpu(ref[i]);
757 ret = do_check_range(info->dqi_sb, "block", blk_no, 0,
758 info->dqi_blocks - 1);
761 if (depth == info->dqi_qtree_depth - 1) {
765 ret = find_next_id(info, id, blk_no, depth + 1);
778 int qtree_get_next_id(struct qtree_mem_dqinfo *info, struct kqid *qid)
780 qid_t id = from_kqid(&init_user_ns, *qid);
783 ret = find_next_id(info, &id, QT_TREEOFF, 0);
786 *qid = make_kqid(&init_user_ns, qid->type, id);
789 EXPORT_SYMBOL(qtree_get_next_id);