2222#include "ctree.h"
2323#include "delayed-ref.h"
2424#include "transaction.h"
25+ #include "qgroup.h"
2526
2627struct kmem_cache * btrfs_delayed_ref_head_cachep ;
2728struct kmem_cache * btrfs_delayed_tree_ref_cachep ;
@@ -420,12 +421,14 @@ update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
420421static noinline struct btrfs_delayed_ref_head *
421422add_delayed_ref_head (struct btrfs_fs_info * fs_info ,
422423 struct btrfs_trans_handle * trans ,
423- struct btrfs_delayed_ref_node * ref , u64 bytenr ,
424- u64 num_bytes , int action , int is_data )
424+ struct btrfs_delayed_ref_node * ref ,
425+ struct btrfs_qgroup_extent_record * qrecord ,
426+ u64 bytenr , u64 num_bytes , int action , int is_data )
425427{
426428 struct btrfs_delayed_ref_head * existing ;
427429 struct btrfs_delayed_ref_head * head_ref = NULL ;
428430 struct btrfs_delayed_ref_root * delayed_refs ;
431+ struct btrfs_qgroup_extent_record * qexisting ;
429432 int count_mod = 1 ;
430433 int must_insert_reserved = 0 ;
431434
@@ -474,6 +477,18 @@ add_delayed_ref_head(struct btrfs_fs_info *fs_info,
474477 head_ref -> processing = 0 ;
475478 head_ref -> total_ref_mod = count_mod ;
476479
480+ /* Record qgroup extent info if provided */
481+ if (qrecord ) {
482+ qrecord -> bytenr = bytenr ;
483+ qrecord -> num_bytes = num_bytes ;
484+ qrecord -> old_roots = NULL ;
485+
486+ qexisting = btrfs_qgroup_insert_dirty_extent (delayed_refs ,
487+ qrecord );
488+ if (qexisting )
489+ kfree (qrecord );
490+ }
491+
477492 spin_lock_init (& head_ref -> lock );
478493 mutex_init (& head_ref -> mutex );
479494
@@ -624,6 +639,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
624639 struct btrfs_delayed_tree_ref * ref ;
625640 struct btrfs_delayed_ref_head * head_ref ;
626641 struct btrfs_delayed_ref_root * delayed_refs ;
642+ struct btrfs_qgroup_extent_record * record = NULL ;
627643
628644 if (!is_fstree (ref_root ) || !fs_info -> quota_enabled )
629645 no_quota = 0 ;
@@ -639,6 +655,15 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
639655 return - ENOMEM ;
640656 }
641657
658+ if (fs_info -> quota_enabled && is_fstree (ref_root )) {
659+ record = kmalloc (sizeof (* record ), GFP_NOFS );
660+ if (!record ) {
661+ kmem_cache_free (btrfs_delayed_tree_ref_cachep , ref );
662+ kmem_cache_free (btrfs_delayed_ref_head_cachep , ref );
663+ return - ENOMEM ;
664+ }
665+ }
666+
642667 head_ref -> extent_op = extent_op ;
643668
644669 delayed_refs = & trans -> transaction -> delayed_refs ;
@@ -648,7 +673,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
648673 * insert both the head node and the new ref without dropping
649674 * the spin lock
650675 */
651- head_ref = add_delayed_ref_head (fs_info , trans , & head_ref -> node ,
676+ head_ref = add_delayed_ref_head (fs_info , trans , & head_ref -> node , record ,
652677 bytenr , num_bytes , action , 0 );
653678
654679 add_delayed_tree_ref (fs_info , trans , head_ref , & ref -> node , bytenr ,
@@ -673,6 +698,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
673698 struct btrfs_delayed_data_ref * ref ;
674699 struct btrfs_delayed_ref_head * head_ref ;
675700 struct btrfs_delayed_ref_root * delayed_refs ;
701+ struct btrfs_qgroup_extent_record * record = NULL ;
676702
677703 if (!is_fstree (ref_root ) || !fs_info -> quota_enabled )
678704 no_quota = 0 ;
@@ -688,6 +714,16 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
688714 return - ENOMEM ;
689715 }
690716
717+ if (fs_info -> quota_enabled && is_fstree (ref_root )) {
718+ record = kmalloc (sizeof (* record ), GFP_NOFS );
719+ if (!record ) {
720+ kmem_cache_free (btrfs_delayed_data_ref_cachep , ref );
721+ kmem_cache_free (btrfs_delayed_ref_head_cachep ,
722+ head_ref );
723+ return - ENOMEM ;
724+ }
725+ }
726+
691727 head_ref -> extent_op = extent_op ;
692728
693729 delayed_refs = & trans -> transaction -> delayed_refs ;
@@ -697,7 +733,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
697733 * insert both the head node and the new ref without dropping
698734 * the spin lock
699735 */
700- head_ref = add_delayed_ref_head (fs_info , trans , & head_ref -> node ,
736+ head_ref = add_delayed_ref_head (fs_info , trans , & head_ref -> node , record ,
701737 bytenr , num_bytes , action , 1 );
702738
703739 add_delayed_data_ref (fs_info , trans , head_ref , & ref -> node , bytenr ,
@@ -725,9 +761,9 @@ int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
725761 delayed_refs = & trans -> transaction -> delayed_refs ;
726762 spin_lock (& delayed_refs -> lock );
727763
728- add_delayed_ref_head (fs_info , trans , & head_ref -> node , bytenr ,
729- num_bytes , BTRFS_UPDATE_DELAYED_HEAD ,
730- extent_op -> is_data );
764+ add_delayed_ref_head (fs_info , trans , & head_ref -> node , NULL , bytenr ,
765+ num_bytes , BTRFS_UPDATE_DELAYED_HEAD ,
766+ extent_op -> is_data );
731767
732768 spin_unlock (& delayed_refs -> lock );
733769 return 0 ;
0 commit comments