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 /* Number of entries in one blocks */
36 static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo
*info
)
38 return (info
->dqi_usable_bs
- sizeof(struct qt_disk_dqdbheader
))
39 / info
->dqi_entry_size
;
42 static char *getdqbuf(size_t size
)
44 char *buf
= kmalloc(size
, GFP_NOFS
);
47 "VFS: Not enough memory for quota buffers.\n");
51 static ssize_t
read_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
53 struct super_block
*sb
= info
->dqi_sb
;
55 memset(buf
, 0, info
->dqi_usable_bs
);
56 return sb
->s_op
->quota_read(sb
, info
->dqi_type
, buf
,
57 info
->dqi_usable_bs
, blk
<< info
->dqi_blocksize_bits
);
60 static ssize_t
write_blk(struct qtree_mem_dqinfo
*info
, uint blk
, char *buf
)
62 struct super_block
*sb
= info
->dqi_sb
;
65 ret
= sb
->s_op
->quota_write(sb
, info
->dqi_type
, buf
,
66 info
->dqi_usable_bs
, blk
<< info
->dqi_blocksize_bits
);
67 if (ret
!= info
->dqi_usable_bs
) {
68 q_warn(KERN_WARNING
"VFS: dquota write failed on "
69 "dev %s\n", sb
->s_id
);
76 /* Remove empty block from list and return it */
77 static int get_free_dqblk(struct qtree_mem_dqinfo
*info
)
79 char *buf
= getdqbuf(info
->dqi_usable_bs
);
80 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
85 if (info
->dqi_free_blk
) {
86 blk
= info
->dqi_free_blk
;
87 ret
= read_blk(info
, blk
, buf
);
90 info
->dqi_free_blk
= le32_to_cpu(dh
->dqdh_next_free
);
93 memset(buf
, 0, info
->dqi_usable_bs
);
94 /* Assure block allocation... */
95 ret
= write_blk(info
, info
->dqi_blocks
, buf
);
98 blk
= info
->dqi_blocks
++;
100 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
107 /* Insert empty block to the list */
108 static int put_free_dqblk(struct qtree_mem_dqinfo
*info
, char *buf
, uint blk
)
110 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
113 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_blk
);
114 dh
->dqdh_prev_free
= cpu_to_le32(0);
115 dh
->dqdh_entries
= cpu_to_le16(0);
116 err
= write_blk(info
, blk
, buf
);
119 info
->dqi_free_blk
= blk
;
120 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
124 /* Remove given block from the list of blocks with free entries */
125 static int remove_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
128 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
129 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
130 uint nextblk
= le32_to_cpu(dh
->dqdh_next_free
);
131 uint prevblk
= le32_to_cpu(dh
->dqdh_prev_free
);
137 err
= read_blk(info
, nextblk
, tmpbuf
);
140 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
142 err
= write_blk(info
, nextblk
, tmpbuf
);
147 err
= read_blk(info
, prevblk
, tmpbuf
);
150 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_next_free
=
152 err
= write_blk(info
, prevblk
, tmpbuf
);
156 info
->dqi_free_entry
= nextblk
;
157 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
160 dh
->dqdh_next_free
= dh
->dqdh_prev_free
= cpu_to_le32(0);
161 /* No matter whether write succeeds block is out of list */
162 if (write_blk(info
, blk
, buf
) < 0)
164 "VFS: Can't write block (%u) with free entries.\n",
172 /* Insert given block to the beginning of list with free entries */
173 static int insert_free_dqentry(struct qtree_mem_dqinfo
*info
, char *buf
,
176 char *tmpbuf
= getdqbuf(info
->dqi_usable_bs
);
177 struct qt_disk_dqdbheader
*dh
= (struct qt_disk_dqdbheader
*)buf
;
182 dh
->dqdh_next_free
= cpu_to_le32(info
->dqi_free_entry
);
183 dh
->dqdh_prev_free
= cpu_to_le32(0);
184 err
= write_blk(info
, blk
, buf
);
187 if (info
->dqi_free_entry
) {
188 err
= read_blk(info
, info
->dqi_free_entry
, tmpbuf
);
191 ((struct qt_disk_dqdbheader
*)tmpbuf
)->dqdh_prev_free
=
193 err
= write_blk(info
, info
->dqi_free_entry
, tmpbuf
);
198 info
->dqi_free_entry
= blk
;
199 mark_info_dirty(info
->dqi_sb
, info
->dqi_type
);
206 /* Is the entry in the block free? */
207 int qtree_entry_unused(struct qtree_mem_dqinfo
*info
, char *disk
)
211 for (i
= 0; i
< info
->dqi_entry_size
; i
++)
216 EXPORT_SYMBOL(qtree_entry_unused
);
218 /* Find space for dquot */
219 static uint
find_free_dqentry(struct qtree_mem_dqinfo
*info
,
220 struct dquot
*dquot
, int *err
)
223 struct qt_disk_dqdbheader
*dh
;
224 char *buf
= getdqbuf(info
->dqi_usable_bs
);
232 dh
= (struct qt_disk_dqdbheader
*)buf
;
233 if (info
->dqi_free_entry
) {
234 blk
= info
->dqi_free_entry
;
235 *err
= read_blk(info
, blk
, buf
);
239 blk
= get_free_dqblk(info
);
245 memset(buf
, 0, info
->dqi_usable_bs
);
246 /* This is enough as the block is already zeroed and the entry
247 * list is empty... */
248 info
->dqi_free_entry
= blk
;
249 mark_info_dirty(dquot
->dq_sb
, dquot
->dq_type
);
251 /* Block will be full? */
252 if (le16_to_cpu(dh
->dqdh_entries
) + 1 >= qtree_dqstr_in_blk(info
)) {
253 *err
= remove_free_dqentry(info
, buf
, blk
);
255 q_warn(KERN_ERR
"VFS: find_free_dqentry(): Can't "
256 "remove block (%u) from entry free list.\n",
261 le16_add_cpu(&dh
->dqdh_entries
, 1);
262 /* Find free structure in block */
263 ddquot
= buf
+ sizeof(struct qt_disk_dqdbheader
);
264 for (i
= 0; i
< qtree_dqstr_in_blk(info
); i
++) {
265 if (qtree_entry_unused(info
, ddquot
))
267 ddquot
+= info
->dqi_entry_size
;
269 #ifdef __QUOTA_QT_PARANOIA
270 if (i
== qtree_dqstr_in_blk(info
)) {
271 printk(KERN_ERR
"VFS: find_free_dqentry(): Data block full "
272 "but it shouldn't.\n");
277 *err
= write_blk(info
, blk
, buf
);
279 q_warn(KERN_ERR
"VFS: find_free_dqentry(): Can't write quota "
280 "data block %u.\n", blk
);
283 dquot
->dq_off
= (blk
<< info
->dqi_blocksize_bits
) +
284 sizeof(struct qt_disk_dqdbheader
) +
285 i
* info
->dqi_entry_size
;
293 /* Insert reference to structure into the trie */
294 static int do_insert_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
295 uint
*treeblk
, int depth
)
297 char *buf
= getdqbuf(info
->dqi_usable_bs
);
298 int ret
= 0, newson
= 0, newact
= 0;
305 ret
= get_free_dqblk(info
);
309 memset(buf
, 0, info
->dqi_usable_bs
);
312 ret
= read_blk(info
, *treeblk
, buf
);
314 q_warn(KERN_ERR
"VFS: Can't read tree quota block "
320 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
323 if (depth
== info
->dqi_qtree_depth
- 1) {
324 #ifdef __QUOTA_QT_PARANOIA
326 printk(KERN_ERR
"VFS: Inserting already present quota "
327 "entry (block %u).\n",
328 le32_to_cpu(ref
[get_index(info
,
329 dquot
->dq_id
, depth
)]));
334 newblk
= find_free_dqentry(info
, dquot
, &ret
);
336 ret
= do_insert_tree(info
, dquot
, &newblk
, depth
+1);
338 if (newson
&& ret
>= 0) {
339 ref
[get_index(info
, dquot
->dq_id
, depth
)] =
341 ret
= write_blk(info
, *treeblk
, buf
);
342 } else if (newact
&& ret
< 0) {
343 put_free_dqblk(info
, buf
, *treeblk
);
350 /* Wrapper for inserting quota structure into tree */
351 static inline int dq_insert_tree(struct qtree_mem_dqinfo
*info
,
354 int tmp
= QT_TREEOFF
;
355 return do_insert_tree(info
, dquot
, &tmp
, 0);
359 * We don't have to be afraid of deadlocks as we never have quotas on quota
362 int qtree_write_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
364 int type
= dquot
->dq_type
;
365 struct super_block
*sb
= dquot
->dq_sb
;
367 char *ddquot
= getdqbuf(info
->dqi_entry_size
);
372 /* dq_off is guarded by dqio_mutex */
373 if (!dquot
->dq_off
) {
374 ret
= dq_insert_tree(info
, dquot
);
376 q_warn(KERN_ERR
"VFS: Error %zd occurred while "
377 "creating quota.\n", ret
);
382 spin_lock(&dq_data_lock
);
383 info
->dqi_ops
->mem2disk_dqblk(ddquot
, dquot
);
384 spin_unlock(&dq_data_lock
);
385 ret
= sb
->s_op
->quota_write(sb
, type
, ddquot
, info
->dqi_entry_size
,
387 if (ret
!= info
->dqi_entry_size
) {
388 q_warn(KERN_WARNING
"VFS: dquota write failed on dev %s\n",
395 dqstats_inc(DQST_WRITES
);
400 EXPORT_SYMBOL(qtree_write_dquot
);
402 /* Free dquot entry in data block */
403 static int free_dqentry(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
406 struct qt_disk_dqdbheader
*dh
;
407 char *buf
= getdqbuf(info
->dqi_usable_bs
);
412 if (dquot
->dq_off
>> info
->dqi_blocksize_bits
!= blk
) {
413 q_warn(KERN_ERR
"VFS: Quota structure has offset to other "
414 "block (%u) than it should (%u).\n", blk
,
415 (uint
)(dquot
->dq_off
>> info
->dqi_blocksize_bits
));
418 ret
= read_blk(info
, blk
, buf
);
420 q_warn(KERN_ERR
"VFS: Can't read quota data block %u\n", blk
);
423 dh
= (struct qt_disk_dqdbheader
*)buf
;
424 le16_add_cpu(&dh
->dqdh_entries
, -1);
425 if (!le16_to_cpu(dh
->dqdh_entries
)) { /* Block got free? */
426 ret
= remove_free_dqentry(info
, buf
, blk
);
428 ret
= put_free_dqblk(info
, buf
, blk
);
430 q_warn(KERN_ERR
"VFS: Can't move quota data block (%u) "
431 "to free list.\n", blk
);
436 (dquot
->dq_off
& ((1 << info
->dqi_blocksize_bits
) - 1)),
437 0, info
->dqi_entry_size
);
438 if (le16_to_cpu(dh
->dqdh_entries
) ==
439 qtree_dqstr_in_blk(info
) - 1) {
440 /* Insert will write block itself */
441 ret
= insert_free_dqentry(info
, buf
, blk
);
443 q_warn(KERN_ERR
"VFS: Can't insert quota data "
444 "block (%u) to free entry list.\n", blk
);
448 ret
= write_blk(info
, blk
, buf
);
450 q_warn(KERN_ERR
"VFS: Can't write quota data "
456 dquot
->dq_off
= 0; /* Quota is now unattached */
462 /* Remove reference to dquot from tree */
463 static int remove_tree(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
,
464 uint
*blk
, int depth
)
466 char *buf
= getdqbuf(info
->dqi_usable_bs
);
469 __le32
*ref
= (__le32
*)buf
;
473 ret
= read_blk(info
, *blk
, buf
);
475 q_warn(KERN_ERR
"VFS: Can't read quota data block %u\n", *blk
);
478 newblk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
479 if (depth
== info
->dqi_qtree_depth
- 1) {
480 ret
= free_dqentry(info
, dquot
, newblk
);
483 ret
= remove_tree(info
, dquot
, &newblk
, depth
+1);
485 if (ret
>= 0 && !newblk
) {
487 ref
[get_index(info
, dquot
->dq_id
, depth
)] = cpu_to_le32(0);
488 /* Block got empty? */
489 for (i
= 0; i
< (info
->dqi_usable_bs
>> 2) && !ref
[i
]; i
++)
491 /* Don't put the root block into the free block list */
492 if (i
== (info
->dqi_usable_bs
>> 2)
493 && *blk
!= QT_TREEOFF
) {
494 put_free_dqblk(info
, buf
, *blk
);
497 ret
= write_blk(info
, *blk
, buf
);
499 q_warn(KERN_ERR
"VFS: Can't write quota tree "
500 "block %u.\n", *blk
);
508 /* Delete dquot from tree */
509 int qtree_delete_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
511 uint tmp
= QT_TREEOFF
;
513 if (!dquot
->dq_off
) /* Even not allocated? */
515 return remove_tree(info
, dquot
, &tmp
, 0);
517 EXPORT_SYMBOL(qtree_delete_dquot
);
519 /* Find entry in block */
520 static loff_t
find_block_dqentry(struct qtree_mem_dqinfo
*info
,
521 struct dquot
*dquot
, uint blk
)
523 char *buf
= getdqbuf(info
->dqi_usable_bs
);
530 ret
= read_blk(info
, blk
, buf
);
532 q_warn(KERN_ERR
"VFS: Can't read quota tree block %u.\n", blk
);
535 ddquot
= buf
+ sizeof(struct qt_disk_dqdbheader
);
536 for (i
= 0; i
< qtree_dqstr_in_blk(info
); i
++) {
537 if (info
->dqi_ops
->is_id(ddquot
, dquot
))
539 ddquot
+= info
->dqi_entry_size
;
541 if (i
== qtree_dqstr_in_blk(info
)) {
542 q_warn(KERN_ERR
"VFS: Quota for id %u referenced "
543 "but not present.\n", dquot
->dq_id
);
547 ret
= (blk
<< info
->dqi_blocksize_bits
) + sizeof(struct
548 qt_disk_dqdbheader
) + i
* info
->dqi_entry_size
;
555 /* Find entry for given id in the tree */
556 static loff_t
find_tree_dqentry(struct qtree_mem_dqinfo
*info
,
557 struct dquot
*dquot
, uint blk
, int depth
)
559 char *buf
= getdqbuf(info
->dqi_usable_bs
);
561 __le32
*ref
= (__le32
*)buf
;
565 ret
= read_blk(info
, blk
, buf
);
567 q_warn(KERN_ERR
"VFS: Can't read quota tree block %u.\n", blk
);
571 blk
= le32_to_cpu(ref
[get_index(info
, dquot
->dq_id
, depth
)]);
572 if (!blk
) /* No reference? */
574 if (depth
< info
->dqi_qtree_depth
- 1)
575 ret
= find_tree_dqentry(info
, dquot
, blk
, depth
+1);
577 ret
= find_block_dqentry(info
, dquot
, blk
);
583 /* Find entry for given id in the tree - wrapper function */
584 static inline loff_t
find_dqentry(struct qtree_mem_dqinfo
*info
,
587 return find_tree_dqentry(info
, dquot
, QT_TREEOFF
, 0);
590 int qtree_read_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
592 int type
= dquot
->dq_type
;
593 struct super_block
*sb
= dquot
->dq_sb
;
598 #ifdef __QUOTA_QT_PARANOIA
599 /* Invalidated quota? */
600 if (!sb_dqopt(dquot
->dq_sb
)->files
[type
]) {
601 printk(KERN_ERR
"VFS: Quota invalidated while reading!\n");
605 /* Do we know offset of the dquot entry in the quota file? */
606 if (!dquot
->dq_off
) {
607 offset
= find_dqentry(info
, dquot
);
608 if (offset
<= 0) { /* Entry not present? */
610 q_warn(KERN_ERR
"VFS: Can't read quota "
611 "structure for id %u.\n", dquot
->dq_id
);
613 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
614 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
618 dquot
->dq_off
= offset
;
620 ddquot
= getdqbuf(info
->dqi_entry_size
);
623 ret
= sb
->s_op
->quota_read(sb
, type
, ddquot
, info
->dqi_entry_size
,
625 if (ret
!= info
->dqi_entry_size
) {
628 q_warn(KERN_ERR
"VFS: Error while reading quota "
629 "structure for id %u.\n", dquot
->dq_id
);
630 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
631 memset(&dquot
->dq_dqb
, 0, sizeof(struct mem_dqblk
));
635 spin_lock(&dq_data_lock
);
636 info
->dqi_ops
->disk2mem_dqblk(dquot
, ddquot
);
637 if (!dquot
->dq_dqb
.dqb_bhardlimit
&&
638 !dquot
->dq_dqb
.dqb_bsoftlimit
&&
639 !dquot
->dq_dqb
.dqb_ihardlimit
&&
640 !dquot
->dq_dqb
.dqb_isoftlimit
)
641 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
642 spin_unlock(&dq_data_lock
);
645 dqstats_inc(DQST_READS
);
648 EXPORT_SYMBOL(qtree_read_dquot
);
650 /* Check whether dquot should not be deleted. We know we are
651 * the only one operating on dquot (thanks to dq_lock) */
652 int qtree_release_dquot(struct qtree_mem_dqinfo
*info
, struct dquot
*dquot
)
654 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) &&
655 !(dquot
->dq_dqb
.dqb_curinodes
| dquot
->dq_dqb
.dqb_curspace
))
656 return qtree_delete_dquot(info
, dquot
);
659 EXPORT_SYMBOL(qtree_release_dquot
);