BUG_ON(existing->is_data != update->is_data);
 
        spin_lock(&existing->lock);
+
+       /*
+        * When freeing an extent, we may not know the owning root when we
+        * first create the head_ref. However, some deref before the last deref
+        * will know it, so we just need to update the head_ref accordingly.
+        */
+       if (!existing->owning_root)
+               existing->owning_root = update->owning_root;
+
        if (update->must_insert_reserved) {
                /* if the extent was freed and then
                 * reallocated before the delayed ref
                 * Set it again here
                 */
                existing->must_insert_reserved = update->must_insert_reserved;
+               existing->owning_root = update->owning_root;
 
                /*
                 * update the num_bytes so we make sure the accounting
                                  struct btrfs_qgroup_extent_record *qrecord,
                                  u64 bytenr, u64 num_bytes, u64 ref_root,
                                  u64 reserved, int action, bool is_data,
-                                 bool is_system)
+                                 bool is_system, u64 owning_root)
 {
        int count_mod = 1;
        bool must_insert_reserved = false;
        head_ref->num_bytes = num_bytes;
        head_ref->ref_mod = count_mod;
        head_ref->must_insert_reserved = must_insert_reserved;
+       head_ref->owning_root = owning_root;
        head_ref->is_data = is_data;
        head_ref->is_system = is_system;
        head_ref->ref_tree = RB_ROOT_CACHED;
 
        init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
                              generic_ref->tree_ref.ref_root, 0, action,
-                             false, is_system);
+                             false, is_system, generic_ref->owning_root);
        head_ref->extent_op = extent_op;
 
        delayed_refs = &trans->transaction->delayed_refs;
        }
 
        init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
-                             reserved, action, true, false);
+                             reserved, action, true, false, generic_ref->owning_root);
        head_ref->extent_op = NULL;
 
        delayed_refs = &trans->transaction->delayed_refs;
                return -ENOMEM;
 
        init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
-                             BTRFS_UPDATE_DELAYED_HEAD, false, false);
+                             BTRFS_UPDATE_DELAYED_HEAD, false, false, 0);
        head_ref->extent_op = extent_op;
 
        delayed_refs = &trans->transaction->delayed_refs;