btrfs: fold error checks when allocating ordered extent and update comments
authorFilipe Manana <fdmanana@suse.com>
Wed, 7 May 2025 15:05:06 +0000 (16:05 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 15 May 2025 12:30:56 +0000 (14:30 +0200)
Instead of having an error check and return on each branch of the if
statement, move the error check to happen after that if branch, reducing
source code and object code sizes.

Before this change:

   $ size fs/btrfs/btrfs.ko
      text    data     bss     dec     hex filename
   1840174  163742   16136 2020052  1ed2d4 fs/btrfs/btrfs.ko

After this change:

   $ size fs/btrfs/btrfs.ko
      text    data     bss     dec     hex filename
   1840138  163742   16136 2020016  1ed2b0 fs/btrfs/btrfs.ko

While at it and moving the comments, update the comments to be more clear
about how qgroup reserved space is released and the intricacies of how
it's managed for COW writes.

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ordered-data.c

index a6c5fc78bc4f190cfe2be997ffcc6390c73493b3..9212ce110cdeef2b2135939a5eaae2ceb55c3587 100644 (file)
@@ -156,20 +156,22 @@ static struct btrfs_ordered_extent *alloc_ordered_extent(
        const bool is_nocow = (flags &
               ((1U << BTRFS_ORDERED_NOCOW) | (1U << BTRFS_ORDERED_PREALLOC)));
 
-       if (is_nocow) {
-               /* For nocow write, we can release the qgroup rsv right now */
+       /*
+        * For a NOCOW write we can free the qgroup reserve right now. For a COW
+        * one we transfer the reserved space from the inode's iotree into the
+        * ordered extent by calling btrfs_qgroup_release_data() and tracking
+        * the qgroup reserved amount in the ordered extent, so that later after
+        * completing the ordered extent, when running the data delayed ref it
+        * creates, we free the reserved data with btrfs_qgroup_free_refroot().
+        */
+       if (is_nocow)
                ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes, &qgroup_rsv);
-               if (ret < 0)
-                       return ERR_PTR(ret);
-       } else {
-               /*
-                * The ordered extent has reserved qgroup space, release now
-                * and pass the reserved number for qgroup_record to free.
-                */
+       else
                ret = btrfs_qgroup_release_data(inode, file_offset, num_bytes, &qgroup_rsv);
-               if (ret < 0)
-                       return ERR_PTR(ret);
-       }
+
+       if (ret < 0)
+               return ERR_PTR(ret);
+
        entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
        if (!entry) {
                entry = ERR_PTR(-ENOMEM);