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
, qid_t id
, int depth
)
27 unsigned int epb
= info
->dqi_usable_bs
>> 2;
29 depth
= info
->dqi_qtree_depth
- depth
- 1;
35 static int get_index(struct qtree_mem_dqinfo
*info
, struct kqid qid
, int depth
)
37 qid_t id
= from_kqid(&init_user_ns
, qid
);
39 return __get_index(info
, id
, depth
);
42 /* Number of entries in one blocks */
43 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo
*info
)
45 return (info
->dqi_usable_bs
- sizeof(struct qt_disk_dqdbheader
))
46 / info
->dqi_entry_size
;
49 static char *getdqbuf(size_t size
)
51 char *buf
= kmalloc(size
, GFP_NOFS
);
54 "VFS: Not enough memory for quota buffers.\n");
58 static ssize_t
read_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
60 struct super_block
*sb
= info
->dqi_sb
;
62 memset(buf
, 0, info
->dqi_usable_bs
);
63 return sb
->s_op
->quota_read(sb
, info
->dqi_type
, buf
,
64 info
->dqi_usable_bs
, blk
<< info
->dqi_blocksize_bits
);
67 static ssize_t
write_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
69 struct super_block
*sb
= info
->dqi_sb
;
72 ret
= sb
->s_op
->quota_write(sb
, info
->dqi_type
, buf
,
73 info
->dqi_usable_bs
, blk
<< info
->dqi_blocksize_bits
);
74 if (ret
!= info
->dqi_usable_bs
) {
75 quota_error(sb
, "dquota write failed");
82 /* Remove empty block from list and return it */
83 static int get_free_dqblk(struct qtree_mem_dqinfo
*info
)
85 char *buf
= getdqbuf(info
->dqi_usable_bs
);
86 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
91 if (info
->dqi_free_blk
) {
92 blk
= info
->dqi_free_blk
;
93 ret
= read_blk(info
, blk
, buf
);
96 info
->dqi_free_blk
= le32_to_cpu(dh
->dqdh_next_free
);
99 memset(buf
, 0, info
->dqi_usable_bs
);
100 /* Assure block allocation... */
101 ret
= write_blk(info
, info
->dqi_blocks
, buf
);
104 blk
= info
->dqi_blocks
++;
106 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
113 /* Insert empty block to the list */
114 static int put_free_dqblk(struct qtree_mem_dqinfo
*info
, char *buf
, uint blk
)
116 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
119 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_blk
);
120 dh
->dqdh_prev_free
= cpu_to_le32(0);
121 dh
->dqdh_entries
= cpu_to_le16(0);
122 err
= write_blk(info
, blk
, buf
);
125 info
->dqi_free_blk
= blk
;
126 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
130 /* Remove given block from the list of blocks with free entries */
131 static int remove_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
134 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
135 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
136 uint nextblk
= le32_to_cpu(dh
->dqdh_next_free
);
137 uint prevblk
= le32_to_cpu(dh
->dqdh_prev_free
);
143 err
= read_blk(info
, nextblk
, tmpbuf
);
146 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
148 err
= write_blk(info
, nextblk
, tmpbuf
);
153 err
= read_blk(info
, prevblk
, tmpbuf
);
156 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_next_free
=
158 err
= write_blk(info
, prevblk
, tmpbuf
);
162 info
->dqi_free_entry
= nextblk
;
163 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
166 dh
->dqdh_next_free
= dh
->dqdh_prev_free
= cpu_to_le32(0);
167 /* No matter whether write succeeds block is out of list */
168 if (write_blk(info
, blk
, buf
) < 0)
169 quota_error(info
->dqi_sb
, "Can't write block (%u) "
170 "with free entries", blk
);
177 /* Insert given block to the beginning of list with free entries */
178 static int insert_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
181 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
182 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
187 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_entry
);
188 dh
->dqdh_prev_free
= cpu_to_le32(0);
189 err
= write_blk(info
, blk
, buf
);
192 if (info
->dqi_free_entry
) {
193 err
= read_blk(info
, info
->dqi_free_entry
, tmpbuf
);
196 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
198 err
= write_blk(info
, info
->dqi_free_entry
, tmpbuf
);
203 info
->dqi_free_entry
= blk
;
204 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
211 /* Is the entry in the block free? */
212 int qtree_entry_unused(struct qtree_mem_dqinfo
*info
, char *disk
)
216 for (i
= 0; i
< info
->dqi_entry_size
; i
++)
221 EXPORT_SYMBOL(qtree_entry_unused
);
223 /* Find space for dquot */
224 static uint
find_free_dqentry(struct qtree_mem_dqinfo
*info
,
225 struct dquot
*dquot
, int *err
)
228 struct qt_disk_dqdbheader
*dh
;
229 char *buf
= getdqbuf(info
->dqi_usable_bs
);
237 dh
= (struct qt_disk_dqdbheader
*)buf
;
238 if (info
->dqi_free_entry
) {
239 blk
= info
->dqi_free_entry
;
240 *err
= read_blk(info
, blk
, buf
);
244 blk
= get_free_dqblk(info
);
250 memset(buf
, 0, info
->dqi_usable_bs
);
251 /* This is enough as the block is already zeroed and the entry
252 * list is empty... */
253 info
->dqi_free_entry
= blk
;
254 mark_info_dirty(dquot
->dq_sb
, dquot
->dq_id
.type
);
256 /* Block will be full? */
257 if (le16_to_cpu(dh
->dqdh_entries
) + 1 >= qtree_dqstr_in_blk(info
)) {
258 *err
= remove_free_dqentry(info
, buf
, blk
);
260 quota_error(dquot
->dq_sb
, "Can't remove block (%u) "
261 "from entry free list", blk
);
265 le16_add_cpu(&dh
->dqdh_entries
, 1);
266 /* Find free structure in block */
267 ddquot
= buf
+ sizeof(struct qt_disk_dqdbheader
);
268 for (i
= 0; i
< qtree_dqstr_in_blk(info
); i
++) {
269 if (qtree_entry_unused(info
, ddquot
))
271 ddquot
+= info
->dqi_entry_size
;
273 #ifdef __QUOTA_QT_PARANOIA
274 if (i
== qtree_dqstr_in_blk(info
)) {
275 quota_error(dquot
->dq_sb
, "Data block full but it shouldn't");
280 *err
= write_blk(info
, blk
, buf
);
282 quota_error(dquot
->dq_sb
, "Can't write quota data block %u",
286 dquot
->dq_off
= (blk
<< info
->dqi_blocksize_bits
) +
287 sizeof(struct qt_disk_dqdbheader
) +
288 i
* info
->dqi_entry_size
;
296 /* Insert reference to structure into the trie */
297 static int do_insert_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
298 uint
*treeblk
, int depth
)
300 char *buf
= getdqbuf(info
->dqi_usable_bs
);
301 int ret
= 0, newson
= 0, newact
= 0;
308 ret
= get_free_dqblk(info
);
312 memset(buf
, 0, info
->dqi_usable_bs
);
315 ret
= read_blk(info
, *treeblk
, buf
);
317 quota_error(dquot
->dq_sb
, "Can't read tree quota "
318 "block %u", *treeblk
);
323 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
326 if (depth
== info
->dqi_qtree_depth
- 1) {
327 #ifdef __QUOTA_QT_PARANOIA
329 quota_error(dquot
->dq_sb
, "Inserting already present "
330 "quota entry (block %u)",
331 le32_to_cpu(ref
[get_index(info
,
332 dquot
->dq_id
, depth
)]));
337 newblk
= find_free_dqentry(info
, dquot
, &ret
);
339 ret
= do_insert_tree(info
, dquot
, &newblk
, depth
+1);
341 if (newson
&& ret
>= 0) {
342 ref
[get_index(info
, dquot
->dq_id
, depth
)] =
344 ret
= write_blk(info
, *treeblk
, buf
);
345 } else if (newact
&& ret
< 0) {
346 put_free_dqblk(info
, buf
, *treeblk
);
353 /* Wrapper for inserting quota structure into tree */
354 static inline int dq_insert_tree(struct qtree_mem_dqinfo
*info
,
357 int tmp
= QT_TREEOFF
;
359 #ifdef __QUOTA_QT_PARANOIA
360 if (info
->dqi_blocks
<= QT_TREEOFF
) {
361 quota_error(dquot
->dq_sb
, "Quota tree root isn't allocated!");
365 return do_insert_tree(info
, dquot
, &tmp
, 0);
369 * We don't have to be afraid of deadlocks as we never have quotas on quota
372 int qtree_write_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
374 int type
= dquot
->dq_id
.type
;
375 struct super_block
*sb
= dquot
->dq_sb
;
377 char *ddquot
= getdqbuf(info
->dqi_entry_size
);
382 /* dq_off is guarded by dqio_sem */
383 if (!dquot
->dq_off
) {
384 ret
= dq_insert_tree(info
, dquot
);
386 quota_error(sb
, "Error %zd occurred while creating "
392 spin_lock(&dquot
->dq_dqb_lock
);
393 info
->dqi_ops
->mem2disk_dqblk(ddquot
, dquot
);
394 spin_unlock(&dquot
->dq_dqb_lock
);
395 ret
= sb
->s_op
->quota_write(sb
, type
, ddquot
, info
->dqi_entry_size
,
397 if (ret
!= info
->dqi_entry_size
) {
398 quota_error(sb
, "dquota write failed");
404 dqstats_inc(DQST_WRITES
);
409 EXPORT_SYMBOL(qtree_write_dquot
);
411 /* Free dquot entry in data block */
412 static int free_dqentry(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
415 struct qt_disk_dqdbheader
*dh
;
416 char *buf
= getdqbuf(info
->dqi_usable_bs
);
421 if (dquot
->dq_off
>> info
->dqi_blocksize_bits
!= blk
) {
422 quota_error(dquot
->dq_sb
, "Quota structure has offset to "
423 "other block (%u) than it should (%u)", blk
,
424 (uint
)(dquot
->dq_off
>> info
->dqi_blocksize_bits
));
427 ret
= read_blk(info
, blk
, buf
);
429 quota_error(dquot
->dq_sb
, "Can't read quota data block %u",
433 dh
= (struct qt_disk_dqdbheader
*)buf
;
434 le16_add_cpu(&dh
->dqdh_entries
, -1);
435 if (!le16_to_cpu(dh
->dqdh_entries
)) { /* Block got free? */
436 ret
= remove_free_dqentry(info
, buf
, blk
);
438 ret
= put_free_dqblk(info
, buf
, blk
);
440 quota_error(dquot
->dq_sb
, "Can't move quota data block "
441 "(%u) to free list", blk
);
446 (dquot
->dq_off
& ((1 << info
->dqi_blocksize_bits
) - 1)),
447 0, info
->dqi_entry_size
);
448 if (le16_to_cpu(dh
->dqdh_entries
) ==
449 qtree_dqstr_in_blk(info
) - 1) {
450 /* Insert will write block itself */
451 ret
= insert_free_dqentry(info
, buf
, blk
);
453 quota_error(dquot
->dq_sb
, "Can't insert quota "
454 "data block (%u) to free entry list", blk
);
458 ret
= write_blk(info
, blk
, buf
);
460 quota_error(dquot
->dq_sb
, "Can't write quota "
461 "data block %u", blk
);
466 dquot
->dq_off
= 0; /* Quota is now unattached */
472 /* Remove reference to dquot from tree */
473 static int remove_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
474 uint
*blk
, int depth
)
476 char *buf
= getdqbuf(info
->dqi_usable_bs
);
479 __le32
*ref
= (__le32
*)buf
;
483 ret
= read_blk(info
, *blk
, buf
);
485 quota_error(dquot
->dq_sb
, "Can't read quota data block %u",
489 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
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
= getdqbuf(info
->dqi_usable_bs
);
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
= (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
= getdqbuf(info
->dqi_usable_bs
);
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 (depth
< info
->dqi_qtree_depth
- 1)
590 ret
= find_tree_dqentry(info
, dquot
, blk
, depth
+1);
592 ret
= find_block_dqentry(info
, dquot
, blk
);
598 /* Find entry for given id in the tree - wrapper function */
599 static inline loff_t
find_dqentry(struct qtree_mem_dqinfo
*info
,
602 return find_tree_dqentry(info
, dquot
, QT_TREEOFF
, 0);
605 int qtree_read_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
607 int type
= dquot
->dq_id
.type
;
608 struct super_block
*sb
= dquot
->dq_sb
;
613 #ifdef __QUOTA_QT_PARANOIA
614 /* Invalidated quota? */
615 if (!sb_dqopt(dquot
->dq_sb
)->files
[type
]) {
616 quota_error(sb
, "Quota invalidated while reading!");
620 /* Do we know offset of the dquot entry in the quota file? */
621 if (!dquot
->dq_off
) {
622 offset
= find_dqentry(info
, dquot
);
623 if (offset
<= 0) { /* Entry not present? */
625 quota_error(sb
,"Can't read quota structure "
627 from_kqid(&init_user_ns
,
630 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
631 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
635 dquot
->dq_off
= offset
;
637 ddquot
= getdqbuf(info
->dqi_entry_size
);
640 ret
= sb
->s_op
->quota_read(sb
, type
, ddquot
, info
->dqi_entry_size
,
642 if (ret
!= info
->dqi_entry_size
) {
645 quota_error(sb
, "Error while reading quota structure for id %u",
646 from_kqid(&init_user_ns
, dquot
->dq_id
));
647 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
648 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
652 spin_lock(&dquot
->dq_dqb_lock
);
653 info
->dqi_ops
->disk2mem_dqblk(dquot
, ddquot
);
654 if (!dquot
->dq_dqb
.dqb_bhardlimit
&&
655 !dquot
->dq_dqb
.dqb_bsoftlimit
&&
656 !dquot
->dq_dqb
.dqb_ihardlimit
&&
657 !dquot
->dq_dqb
.dqb_isoftlimit
)
658 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
659 spin_unlock(&dquot
->dq_dqb_lock
);
662 dqstats_inc(DQST_READS
);
665 EXPORT_SYMBOL(qtree_read_dquot
);
667 /* Check whether dquot should not be deleted. We know we are
668 * the only one operating on dquot (thanks to dq_lock) */
669 int qtree_release_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
671 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) &&
672 !(dquot
->dq_dqb
.dqb_curinodes
| dquot
->dq_dqb
.dqb_curspace
))
673 return qtree_delete_dquot(info
, dquot
);
676 EXPORT_SYMBOL(qtree_release_dquot
);
678 static int find_next_id(struct qtree_mem_dqinfo
*info
, qid_t
*id
,
679 unsigned int blk
, int depth
)
681 char *buf
= getdqbuf(info
->dqi_usable_bs
);
682 __le32
*ref
= (__le32
*)buf
;
684 unsigned int epb
= info
->dqi_usable_bs
>> 2;
685 unsigned int level_inc
= 1;
691 for (i
= depth
; i
< info
->dqi_qtree_depth
- 1; i
++)
694 ret
= read_blk(info
, blk
, buf
);
696 quota_error(info
->dqi_sb
,
697 "Can't read quota tree block %u", blk
);
700 for (i
= __get_index(info
, *id
, depth
); i
< epb
; i
++) {
701 if (ref
[i
] == cpu_to_le32(0)) {
705 if (depth
== info
->dqi_qtree_depth
- 1) {
709 ret
= find_next_id(info
, id
, le32_to_cpu(ref
[i
]), depth
+ 1);
722 int qtree_get_next_id(struct qtree_mem_dqinfo
*info
, struct kqid
*qid
)
724 qid_t id
= from_kqid(&init_user_ns
, *qid
);
727 ret
= find_next_id(info
, &id
, QT_TREEOFF
, 0);
730 *qid
= make_kqid(&init_user_ns
, qid
->type
, id
);
733 EXPORT_SYMBOL(qtree_get_next_id
);