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 char *getdqbuf(size_t size
)
52 char *buf
= kmalloc(size
, GFP_NOFS
);
55 "VFS: Not enough memory for quota buffers.\n");
59 static ssize_t
read_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
61 struct super_block
*sb
= info
->dqi_sb
;
63 memset(buf
, 0, info
->dqi_usable_bs
);
64 return sb
->s_op
->quota_read(sb
, info
->dqi_type
, buf
,
65 info
->dqi_usable_bs
, (loff_t
)blk
<< info
->dqi_blocksize_bits
);
68 static ssize_t
write_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
70 struct super_block
*sb
= info
->dqi_sb
;
73 ret
= sb
->s_op
->quota_write(sb
, info
->dqi_type
, buf
,
74 info
->dqi_usable_bs
, (loff_t
)blk
<< info
->dqi_blocksize_bits
);
75 if (ret
!= info
->dqi_usable_bs
) {
76 quota_error(sb
, "dquota write failed");
83 /* Remove empty block from list and return it */
84 static int get_free_dqblk(struct qtree_mem_dqinfo
*info
)
86 char *buf
= getdqbuf(info
->dqi_usable_bs
);
87 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
92 if (info
->dqi_free_blk
) {
93 blk
= info
->dqi_free_blk
;
94 ret
= read_blk(info
, blk
, buf
);
97 info
->dqi_free_blk
= le32_to_cpu(dh
->dqdh_next_free
);
100 memset(buf
, 0, info
->dqi_usable_bs
);
101 /* Assure block allocation... */
102 ret
= write_blk(info
, info
->dqi_blocks
, buf
);
105 blk
= info
->dqi_blocks
++;
107 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
114 /* Insert empty block to the list */
115 static int put_free_dqblk(struct qtree_mem_dqinfo
*info
, char *buf
, uint blk
)
117 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
120 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_blk
);
121 dh
->dqdh_prev_free
= cpu_to_le32(0);
122 dh
->dqdh_entries
= cpu_to_le16(0);
123 err
= write_blk(info
, blk
, buf
);
126 info
->dqi_free_blk
= blk
;
127 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
131 /* Remove given block from the list of blocks with free entries */
132 static int remove_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
135 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
136 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
137 uint nextblk
= le32_to_cpu(dh
->dqdh_next_free
);
138 uint prevblk
= le32_to_cpu(dh
->dqdh_prev_free
);
144 err
= read_blk(info
, nextblk
, tmpbuf
);
147 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
149 err
= write_blk(info
, nextblk
, tmpbuf
);
154 err
= read_blk(info
, prevblk
, tmpbuf
);
157 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_next_free
=
159 err
= write_blk(info
, prevblk
, tmpbuf
);
163 info
->dqi_free_entry
= nextblk
;
164 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
167 dh
->dqdh_next_free
= dh
->dqdh_prev_free
= cpu_to_le32(0);
168 /* No matter whether write succeeds block is out of list */
169 if (write_blk(info
, blk
, buf
) < 0)
170 quota_error(info
->dqi_sb
, "Can't write block (%u) "
171 "with free entries", blk
);
178 /* Insert given block to the beginning of list with free entries */
179 static int insert_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
182 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
183 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
188 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_entry
);
189 dh
->dqdh_prev_free
= cpu_to_le32(0);
190 err
= write_blk(info
, blk
, buf
);
193 if (info
->dqi_free_entry
) {
194 err
= read_blk(info
, info
->dqi_free_entry
, tmpbuf
);
197 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
199 err
= write_blk(info
, info
->dqi_free_entry
, tmpbuf
);
204 info
->dqi_free_entry
= blk
;
205 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
212 /* Is the entry in the block free? */
213 int qtree_entry_unused(struct qtree_mem_dqinfo
*info
, char *disk
)
217 for (i
= 0; i
< info
->dqi_entry_size
; i
++)
222 EXPORT_SYMBOL(qtree_entry_unused
);
224 /* Find space for dquot */
225 static uint
find_free_dqentry(struct qtree_mem_dqinfo
*info
,
226 struct dquot
*dquot
, int *err
)
229 struct qt_disk_dqdbheader
*dh
;
230 char *buf
= getdqbuf(info
->dqi_usable_bs
);
238 dh
= (struct qt_disk_dqdbheader
*)buf
;
239 if (info
->dqi_free_entry
) {
240 blk
= info
->dqi_free_entry
;
241 *err
= read_blk(info
, blk
, buf
);
245 blk
= get_free_dqblk(info
);
251 memset(buf
, 0, info
->dqi_usable_bs
);
252 /* This is enough as the block is already zeroed and the entry
253 * list is empty... */
254 info
->dqi_free_entry
= blk
;
255 mark_info_dirty(dquot
->dq_sb
, dquot
->dq_id
.type
);
257 /* Block will be full? */
258 if (le16_to_cpu(dh
->dqdh_entries
) + 1 >= qtree_dqstr_in_blk(info
)) {
259 *err
= remove_free_dqentry(info
, buf
, blk
);
261 quota_error(dquot
->dq_sb
, "Can't remove block (%u) "
262 "from entry free list", blk
);
266 le16_add_cpu(&dh
->dqdh_entries
, 1);
267 /* Find free structure in block */
268 ddquot
= buf
+ sizeof(struct qt_disk_dqdbheader
);
269 for (i
= 0; i
< qtree_dqstr_in_blk(info
); i
++) {
270 if (qtree_entry_unused(info
, ddquot
))
272 ddquot
+= info
->dqi_entry_size
;
274 #ifdef __QUOTA_QT_PARANOIA
275 if (i
== qtree_dqstr_in_blk(info
)) {
276 quota_error(dquot
->dq_sb
, "Data block full but it shouldn't");
281 *err
= write_blk(info
, blk
, buf
);
283 quota_error(dquot
->dq_sb
, "Can't write quota data block %u",
287 dquot
->dq_off
= ((loff_t
)blk
<< info
->dqi_blocksize_bits
) +
288 sizeof(struct qt_disk_dqdbheader
) +
289 i
* info
->dqi_entry_size
;
297 /* Insert reference to structure into the trie */
298 static int do_insert_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
299 uint
*treeblk
, int depth
)
301 char *buf
= getdqbuf(info
->dqi_usable_bs
);
302 int ret
= 0, newson
= 0, newact
= 0;
309 ret
= get_free_dqblk(info
);
313 memset(buf
, 0, info
->dqi_usable_bs
);
316 ret
= read_blk(info
, *treeblk
, buf
);
318 quota_error(dquot
->dq_sb
, "Can't read tree quota "
319 "block %u", *treeblk
);
324 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
327 if (depth
== info
->dqi_qtree_depth
- 1) {
328 #ifdef __QUOTA_QT_PARANOIA
330 quota_error(dquot
->dq_sb
, "Inserting already present "
331 "quota entry (block %u)",
332 le32_to_cpu(ref
[get_index(info
,
333 dquot
->dq_id
, depth
)]));
338 newblk
= find_free_dqentry(info
, dquot
, &ret
);
340 ret
= do_insert_tree(info
, dquot
, &newblk
, depth
+1);
342 if (newson
&& ret
>= 0) {
343 ref
[get_index(info
, dquot
->dq_id
, depth
)] =
345 ret
= write_blk(info
, *treeblk
, buf
);
346 } else if (newact
&& ret
< 0) {
347 put_free_dqblk(info
, buf
, *treeblk
);
354 /* Wrapper for inserting quota structure into tree */
355 static inline int dq_insert_tree(struct qtree_mem_dqinfo
*info
,
358 int tmp
= QT_TREEOFF
;
360 #ifdef __QUOTA_QT_PARANOIA
361 if (info
->dqi_blocks
<= QT_TREEOFF
) {
362 quota_error(dquot
->dq_sb
, "Quota tree root isn't allocated!");
366 return do_insert_tree(info
, dquot
, &tmp
, 0);
370 * We don't have to be afraid of deadlocks as we never have quotas on quota
373 int qtree_write_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
375 int type
= dquot
->dq_id
.type
;
376 struct super_block
*sb
= dquot
->dq_sb
;
378 char *ddquot
= getdqbuf(info
->dqi_entry_size
);
383 /* dq_off is guarded by dqio_sem */
384 if (!dquot
->dq_off
) {
385 ret
= dq_insert_tree(info
, dquot
);
387 quota_error(sb
, "Error %zd occurred while creating "
393 spin_lock(&dquot
->dq_dqb_lock
);
394 info
->dqi_ops
->mem2disk_dqblk(ddquot
, dquot
);
395 spin_unlock(&dquot
->dq_dqb_lock
);
396 ret
= sb
->s_op
->quota_write(sb
, type
, ddquot
, info
->dqi_entry_size
,
398 if (ret
!= info
->dqi_entry_size
) {
399 quota_error(sb
, "dquota write failed");
405 dqstats_inc(DQST_WRITES
);
410 EXPORT_SYMBOL(qtree_write_dquot
);
412 /* Free dquot entry in data block */
413 static int free_dqentry(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
416 struct qt_disk_dqdbheader
*dh
;
417 char *buf
= getdqbuf(info
->dqi_usable_bs
);
422 if (dquot
->dq_off
>> info
->dqi_blocksize_bits
!= blk
) {
423 quota_error(dquot
->dq_sb
, "Quota structure has offset to "
424 "other block (%u) than it should (%u)", blk
,
425 (uint
)(dquot
->dq_off
>> info
->dqi_blocksize_bits
));
428 ret
= read_blk(info
, blk
, buf
);
430 quota_error(dquot
->dq_sb
, "Can't read quota data block %u",
434 dh
= (struct qt_disk_dqdbheader
*)buf
;
435 le16_add_cpu(&dh
->dqdh_entries
, -1);
436 if (!le16_to_cpu(dh
->dqdh_entries
)) { /* Block got free? */
437 ret
= remove_free_dqentry(info
, buf
, blk
);
439 ret
= put_free_dqblk(info
, buf
, blk
);
441 quota_error(dquot
->dq_sb
, "Can't move quota data block "
442 "(%u) to free list", blk
);
447 (dquot
->dq_off
& ((1 << info
->dqi_blocksize_bits
) - 1)),
448 0, info
->dqi_entry_size
);
449 if (le16_to_cpu(dh
->dqdh_entries
) ==
450 qtree_dqstr_in_blk(info
) - 1) {
451 /* Insert will write block itself */
452 ret
= insert_free_dqentry(info
, buf
, blk
);
454 quota_error(dquot
->dq_sb
, "Can't insert quota "
455 "data block (%u) to free entry list", blk
);
459 ret
= write_blk(info
, blk
, buf
);
461 quota_error(dquot
->dq_sb
, "Can't write quota "
462 "data block %u", blk
);
467 dquot
->dq_off
= 0; /* Quota is now unattached */
473 /* Remove reference to dquot from tree */
474 static int remove_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
475 uint
*blk
, int depth
)
477 char *buf
= getdqbuf(info
->dqi_usable_bs
);
480 __le32
*ref
= (__le32
*)buf
;
484 ret
= read_blk(info
, *blk
, buf
);
486 quota_error(dquot
->dq_sb
, "Can't read quota data block %u",
490 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
491 if (depth
== info
->dqi_qtree_depth
- 1) {
492 ret
= free_dqentry(info
, dquot
, newblk
);
495 ret
= remove_tree(info
, dquot
, &newblk
, depth
+1);
497 if (ret
>= 0 && !newblk
) {
499 ref
[get_index(info
, dquot
->dq_id
, depth
)] = cpu_to_le32(0);
500 /* Block got empty? */
501 for (i
= 0; i
< (info
->dqi_usable_bs
>> 2) && !ref
[i
]; i
++)
503 /* Don't put the root block into the free block list */
504 if (i
== (info
->dqi_usable_bs
>> 2)
505 && *blk
!= QT_TREEOFF
) {
506 put_free_dqblk(info
, buf
, *blk
);
509 ret
= write_blk(info
, *blk
, buf
);
511 quota_error(dquot
->dq_sb
,
512 "Can't write quota tree block %u",
521 /* Delete dquot from tree */
522 int qtree_delete_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
524 uint tmp
= QT_TREEOFF
;
526 if (!dquot
->dq_off
) /* Even not allocated? */
528 return remove_tree(info
, dquot
, &tmp
, 0);
530 EXPORT_SYMBOL(qtree_delete_dquot
);
532 /* Find entry in block */
533 static loff_t
find_block_dqentry(struct qtree_mem_dqinfo
*info
,
534 struct dquot
*dquot
, uint blk
)
536 char *buf
= getdqbuf(info
->dqi_usable_bs
);
543 ret
= read_blk(info
, blk
, buf
);
545 quota_error(dquot
->dq_sb
, "Can't read quota tree "
549 ddquot
= buf
+ sizeof(struct qt_disk_dqdbheader
);
550 for (i
= 0; i
< qtree_dqstr_in_blk(info
); i
++) {
551 if (info
->dqi_ops
->is_id(ddquot
, dquot
))
553 ddquot
+= info
->dqi_entry_size
;
555 if (i
== qtree_dqstr_in_blk(info
)) {
556 quota_error(dquot
->dq_sb
,
557 "Quota for id %u referenced but not present",
558 from_kqid(&init_user_ns
, dquot
->dq_id
));
562 ret
= ((loff_t
)blk
<< info
->dqi_blocksize_bits
) + sizeof(struct
563 qt_disk_dqdbheader
) + i
* info
->dqi_entry_size
;
570 /* Find entry for given id in the tree */
571 static loff_t
find_tree_dqentry(struct qtree_mem_dqinfo
*info
,
572 struct dquot
*dquot
, uint blk
, int depth
)
574 char *buf
= getdqbuf(info
->dqi_usable_bs
);
576 __le32
*ref
= (__le32
*)buf
;
580 ret
= read_blk(info
, blk
, buf
);
582 quota_error(dquot
->dq_sb
, "Can't read quota tree block %u",
587 blk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
588 if (!blk
) /* No reference? */
590 if (depth
< info
->dqi_qtree_depth
- 1)
591 ret
= find_tree_dqentry(info
, dquot
, blk
, depth
+1);
593 ret
= find_block_dqentry(info
, dquot
, blk
);
599 /* Find entry for given id in the tree - wrapper function */
600 static inline loff_t
find_dqentry(struct qtree_mem_dqinfo
*info
,
603 return find_tree_dqentry(info
, dquot
, QT_TREEOFF
, 0);
606 int qtree_read_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
608 int type
= dquot
->dq_id
.type
;
609 struct super_block
*sb
= dquot
->dq_sb
;
614 #ifdef __QUOTA_QT_PARANOIA
615 /* Invalidated quota? */
616 if (!sb_dqopt(dquot
->dq_sb
)->files
[type
]) {
617 quota_error(sb
, "Quota invalidated while reading!");
621 /* Do we know offset of the dquot entry in the quota file? */
622 if (!dquot
->dq_off
) {
623 offset
= find_dqentry(info
, dquot
);
624 if (offset
<= 0) { /* Entry not present? */
626 quota_error(sb
,"Can't read quota structure "
628 from_kqid(&init_user_ns
,
631 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
632 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
636 dquot
->dq_off
= offset
;
638 ddquot
= getdqbuf(info
->dqi_entry_size
);
641 ret
= sb
->s_op
->quota_read(sb
, type
, ddquot
, info
->dqi_entry_size
,
643 if (ret
!= info
->dqi_entry_size
) {
646 quota_error(sb
, "Error while reading quota structure for id %u",
647 from_kqid(&init_user_ns
, dquot
->dq_id
));
648 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
649 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
653 spin_lock(&dquot
->dq_dqb_lock
);
654 info
->dqi_ops
->disk2mem_dqblk(dquot
, ddquot
);
655 if (!dquot
->dq_dqb
.dqb_bhardlimit
&&
656 !dquot
->dq_dqb
.dqb_bsoftlimit
&&
657 !dquot
->dq_dqb
.dqb_ihardlimit
&&
658 !dquot
->dq_dqb
.dqb_isoftlimit
)
659 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
660 spin_unlock(&dquot
->dq_dqb_lock
);
663 dqstats_inc(DQST_READS
);
666 EXPORT_SYMBOL(qtree_read_dquot
);
668 /* Check whether dquot should not be deleted. We know we are
669 * the only one operating on dquot (thanks to dq_lock) */
670 int qtree_release_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
672 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) &&
673 !(dquot
->dq_dqb
.dqb_curinodes
| dquot
->dq_dqb
.dqb_curspace
))
674 return qtree_delete_dquot(info
, dquot
);
677 EXPORT_SYMBOL(qtree_release_dquot
);
679 static int find_next_id(struct qtree_mem_dqinfo
*info
, qid_t
*id
,
680 unsigned int blk
, int depth
)
682 char *buf
= getdqbuf(info
->dqi_usable_bs
);
683 __le32
*ref
= (__le32
*)buf
;
685 unsigned int epb
= info
->dqi_usable_bs
>> 2;
686 unsigned int level_inc
= 1;
692 for (i
= depth
; i
< info
->dqi_qtree_depth
- 1; i
++)
695 ret
= read_blk(info
, blk
, buf
);
697 quota_error(info
->dqi_sb
,
698 "Can't read quota tree block %u", blk
);
701 for (i
= __get_index(info
, *id
, depth
); i
< epb
; i
++) {
702 if (ref
[i
] == cpu_to_le32(0)) {
706 if (depth
== info
->dqi_qtree_depth
- 1) {
710 ret
= find_next_id(info
, id
, le32_to_cpu(ref
[i
]), depth
+ 1);
723 int qtree_get_next_id(struct qtree_mem_dqinfo
*info
, struct kqid
*qid
)
725 qid_t id
= from_kqid(&init_user_ns
, *qid
);
728 ret
= find_next_id(info
, &id
, QT_TREEOFF
, 0);
731 *qid
= make_kqid(&init_user_ns
, qid
->type
, id
);
734 EXPORT_SYMBOL(qtree_get_next_id
);