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 /* Remove empty block from list and return it */
75 static int get_free_dqblk(struct qtree_mem_dqinfo *info)
77 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
78 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
83 if (info->dqi_free_blk) {
84 blk = info->dqi_free_blk;
85 ret = read_blk(info, blk, buf);
88 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
91 memset(buf, 0, info->dqi_usable_bs);
92 /* Assure block allocation... */
93 ret = write_blk(info, info->dqi_blocks, buf);
96 blk = info->dqi_blocks++;
98 mark_info_dirty(info->dqi_sb, info->dqi_type);
105 /* Insert empty block to the list */
106 static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
108 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
111 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
112 dh->dqdh_prev_free = cpu_to_le32(0);
113 dh->dqdh_entries = cpu_to_le16(0);
114 err = write_blk(info, blk, buf);
117 info->dqi_free_blk = blk;
118 mark_info_dirty(info->dqi_sb, info->dqi_type);
122 /* Remove given block from the list of blocks with free entries */
123 static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
126 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
127 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
128 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
129 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
135 err = read_blk(info, nextblk, tmpbuf);
138 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
140 err = write_blk(info, nextblk, tmpbuf);
145 err = read_blk(info, prevblk, tmpbuf);
148 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
150 err = write_blk(info, prevblk, tmpbuf);
154 info->dqi_free_entry = nextblk;
155 mark_info_dirty(info->dqi_sb, info->dqi_type);
158 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
159 /* No matter whether write succeeds block is out of list */
160 if (write_blk(info, blk, buf) < 0)
161 quota_error(info->dqi_sb, "Can't write block (%u) "
162 "with free entries", blk);
169 /* Insert given block to the beginning of list with free entries */
170 static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
173 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
174 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
179 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
180 dh->dqdh_prev_free = cpu_to_le32(0);
181 err = write_blk(info, blk, buf);
184 if (info->dqi_free_entry) {
185 err = read_blk(info, info->dqi_free_entry, tmpbuf);
188 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
190 err = write_blk(info, info->dqi_free_entry, tmpbuf);
195 info->dqi_free_entry = blk;
196 mark_info_dirty(info->dqi_sb, info->dqi_type);
203 /* Is the entry in the block free? */
204 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
208 for (i = 0; i < info->dqi_entry_size; i++)
213 EXPORT_SYMBOL(qtree_entry_unused);
215 /* Find space for dquot */
216 static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
217 struct dquot *dquot, int *err)
220 struct qt_disk_dqdbheader *dh;
221 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
229 dh = (struct qt_disk_dqdbheader *)buf;
230 if (info->dqi_free_entry) {
231 blk = info->dqi_free_entry;
232 *err = read_blk(info, blk, buf);
236 blk = get_free_dqblk(info);
242 memset(buf, 0, info->dqi_usable_bs);
243 /* This is enough as the block is already zeroed and the entry
244 * list is empty... */
245 info->dqi_free_entry = blk;
246 mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
248 /* Block will be full? */
249 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
250 *err = remove_free_dqentry(info, buf, blk);
252 quota_error(dquot->dq_sb, "Can't remove block (%u) "
253 "from entry free list", blk);
257 le16_add_cpu(&dh->dqdh_entries, 1);
258 /* Find free structure in block */
259 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
260 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
261 if (qtree_entry_unused(info, ddquot))
263 ddquot += info->dqi_entry_size;
265 #ifdef __QUOTA_QT_PARANOIA
266 if (i == qtree_dqstr_in_blk(info)) {
267 quota_error(dquot->dq_sb, "Data block full but it shouldn't");
272 *err = write_blk(info, blk, buf);
274 quota_error(dquot->dq_sb, "Can't write quota data block %u",
278 dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
279 sizeof(struct qt_disk_dqdbheader) +
280 i * info->dqi_entry_size;
288 /* Insert reference to structure into the trie */
289 static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
290 uint *treeblk, int depth)
292 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
293 int ret = 0, newson = 0, newact = 0;
300 ret = get_free_dqblk(info);
304 memset(buf, 0, info->dqi_usable_bs);
307 ret = read_blk(info, *treeblk, buf);
309 quota_error(dquot->dq_sb, "Can't read tree quota "
310 "block %u", *treeblk);
315 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
318 if (depth == info->dqi_qtree_depth - 1) {
319 #ifdef __QUOTA_QT_PARANOIA
321 quota_error(dquot->dq_sb, "Inserting already present "
322 "quota entry (block %u)",
323 le32_to_cpu(ref[get_index(info,
324 dquot->dq_id, depth)]));
329 newblk = find_free_dqentry(info, dquot, &ret);
331 ret = do_insert_tree(info, dquot, &newblk, depth+1);
333 if (newson && ret >= 0) {
334 ref[get_index(info, dquot->dq_id, depth)] =
336 ret = write_blk(info, *treeblk, buf);
337 } else if (newact && ret < 0) {
338 put_free_dqblk(info, buf, *treeblk);
345 /* Wrapper for inserting quota structure into tree */
346 static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
349 int tmp = QT_TREEOFF;
351 #ifdef __QUOTA_QT_PARANOIA
352 if (info->dqi_blocks <= QT_TREEOFF) {
353 quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
357 return do_insert_tree(info, dquot, &tmp, 0);
361 * We don't have to be afraid of deadlocks as we never have quotas on quota
364 int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
366 int type = dquot->dq_id.type;
367 struct super_block *sb = dquot->dq_sb;
369 char *ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
374 /* dq_off is guarded by dqio_sem */
375 if (!dquot->dq_off) {
376 ret = dq_insert_tree(info, dquot);
378 quota_error(sb, "Error %zd occurred while creating "
384 spin_lock(&dquot->dq_dqb_lock);
385 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
386 spin_unlock(&dquot->dq_dqb_lock);
387 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
389 if (ret != info->dqi_entry_size) {
390 quota_error(sb, "dquota write failed");
396 dqstats_inc(DQST_WRITES);
401 EXPORT_SYMBOL(qtree_write_dquot);
403 /* Free dquot entry in data block */
404 static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
407 struct qt_disk_dqdbheader *dh;
408 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
413 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
414 quota_error(dquot->dq_sb, "Quota structure has offset to "
415 "other block (%u) than it should (%u)", blk,
416 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
420 ret = read_blk(info, blk, buf);
422 quota_error(dquot->dq_sb, "Can't read quota data block %u",
426 dh = (struct qt_disk_dqdbheader *)buf;
427 le16_add_cpu(&dh->dqdh_entries, -1);
428 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
429 ret = remove_free_dqentry(info, buf, blk);
431 ret = put_free_dqblk(info, buf, blk);
433 quota_error(dquot->dq_sb, "Can't move quota data block "
434 "(%u) to free list", blk);
439 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
440 0, info->dqi_entry_size);
441 if (le16_to_cpu(dh->dqdh_entries) ==
442 qtree_dqstr_in_blk(info) - 1) {
443 /* Insert will write block itself */
444 ret = insert_free_dqentry(info, buf, blk);
446 quota_error(dquot->dq_sb, "Can't insert quota "
447 "data block (%u) to free entry list", blk);
451 ret = write_blk(info, blk, buf);
453 quota_error(dquot->dq_sb, "Can't write quota "
454 "data block %u", blk);
459 dquot->dq_off = 0; /* Quota is now unattached */
465 /* Remove reference to dquot from tree */
466 static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
467 uint *blk, int depth)
469 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
472 __le32 *ref = (__le32 *)buf;
476 ret = read_blk(info, *blk, buf);
478 quota_error(dquot->dq_sb, "Can't read quota data block %u",
482 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
483 if (newblk < QT_TREEOFF || newblk >= info->dqi_blocks) {
484 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
485 newblk, info->dqi_blocks);
490 if (depth == info->dqi_qtree_depth - 1) {
491 ret = free_dqentry(info, dquot, newblk);
494 ret = remove_tree(info, dquot, &newblk, depth+1);
496 if (ret >= 0 && !newblk) {
498 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
499 /* Block got empty? */
500 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
502 /* Don't put the root block into the free block list */
503 if (i == (info->dqi_usable_bs >> 2)
504 && *blk != QT_TREEOFF) {
505 put_free_dqblk(info, buf, *blk);
508 ret = write_blk(info, *blk, buf);
510 quota_error(dquot->dq_sb,
511 "Can't write quota tree block %u",
520 /* Delete dquot from tree */
521 int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
523 uint tmp = QT_TREEOFF;
525 if (!dquot->dq_off) /* Even not allocated? */
527 return remove_tree(info, dquot, &tmp, 0);
529 EXPORT_SYMBOL(qtree_delete_dquot);
531 /* Find entry in block */
532 static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
533 struct dquot *dquot, uint blk)
535 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
542 ret = read_blk(info, blk, buf);
544 quota_error(dquot->dq_sb, "Can't read quota tree "
548 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
549 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
550 if (info->dqi_ops->is_id(ddquot, dquot))
552 ddquot += info->dqi_entry_size;
554 if (i == qtree_dqstr_in_blk(info)) {
555 quota_error(dquot->dq_sb,
556 "Quota for id %u referenced but not present",
557 from_kqid(&init_user_ns, dquot->dq_id));
561 ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
562 qt_disk_dqdbheader) + i * info->dqi_entry_size;
569 /* Find entry for given id in the tree */
570 static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
571 struct dquot *dquot, uint blk, int depth)
573 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
575 __le32 *ref = (__le32 *)buf;
579 ret = read_blk(info, blk, buf);
581 quota_error(dquot->dq_sb, "Can't read quota tree block %u",
586 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
587 if (!blk) /* No reference? */
589 if (blk < QT_TREEOFF || blk >= info->dqi_blocks) {
590 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
591 blk, info->dqi_blocks);
596 if (depth < info->dqi_qtree_depth - 1)
597 ret = find_tree_dqentry(info, dquot, blk, depth+1);
599 ret = find_block_dqentry(info, dquot, blk);
605 /* Find entry for given id in the tree - wrapper function */
606 static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
609 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
612 int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
614 int type = dquot->dq_id.type;
615 struct super_block *sb = dquot->dq_sb;
620 #ifdef __QUOTA_QT_PARANOIA
621 /* Invalidated quota? */
622 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
623 quota_error(sb, "Quota invalidated while reading!");
627 /* Do we know offset of the dquot entry in the quota file? */
628 if (!dquot->dq_off) {
629 offset = find_dqentry(info, dquot);
630 if (offset <= 0) { /* Entry not present? */
632 quota_error(sb,"Can't read quota structure "
634 from_kqid(&init_user_ns,
637 set_bit(DQ_FAKE_B, &dquot->dq_flags);
638 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
642 dquot->dq_off = offset;
644 ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
647 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
649 if (ret != info->dqi_entry_size) {
652 quota_error(sb, "Error while reading quota structure for id %u",
653 from_kqid(&init_user_ns, dquot->dq_id));
654 set_bit(DQ_FAKE_B, &dquot->dq_flags);
655 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
659 spin_lock(&dquot->dq_dqb_lock);
660 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
661 if (!dquot->dq_dqb.dqb_bhardlimit &&
662 !dquot->dq_dqb.dqb_bsoftlimit &&
663 !dquot->dq_dqb.dqb_ihardlimit &&
664 !dquot->dq_dqb.dqb_isoftlimit)
665 set_bit(DQ_FAKE_B, &dquot->dq_flags);
666 spin_unlock(&dquot->dq_dqb_lock);
669 dqstats_inc(DQST_READS);
672 EXPORT_SYMBOL(qtree_read_dquot);
674 /* Check whether dquot should not be deleted. We know we are
675 * the only one operating on dquot (thanks to dq_lock) */
676 int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
678 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
679 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
680 return qtree_delete_dquot(info, dquot);
683 EXPORT_SYMBOL(qtree_release_dquot);
685 static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
686 unsigned int blk, int depth)
688 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
689 __le32 *ref = (__le32 *)buf;
691 unsigned int epb = info->dqi_usable_bs >> 2;
692 unsigned int level_inc = 1;
698 for (i = depth; i < info->dqi_qtree_depth - 1; i++)
701 ret = read_blk(info, blk, buf);
703 quota_error(info->dqi_sb,
704 "Can't read quota tree block %u", blk);
707 for (i = __get_index(info, *id, depth); i < epb; i++) {
708 if (ref[i] == cpu_to_le32(0)) {
712 if (depth == info->dqi_qtree_depth - 1) {
716 ret = find_next_id(info, id, le32_to_cpu(ref[i]), depth + 1);
729 int qtree_get_next_id(struct qtree_mem_dqinfo *info, struct kqid *qid)
731 qid_t id = from_kqid(&init_user_ns, *qid);
734 ret = find_next_id(info, &id, QT_TREEOFF, 0);
737 *qid = make_kqid(&init_user_ns, qid->type, id);
740 EXPORT_SYMBOL(qtree_get_next_id);