]> git.hungrycats.org Git - linux/commitdiff
btrfs: honor FMODE_NOCMTIME in fallocate
authorJeff Layton <jlayton@kernel.org>
Thu, 5 Nov 2026 20:07:44 +0000 (15:07 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 9 Nov 2026 00:18:30 +0000 (19:18 -0500)
btrfs_fallocate() calls file_modified(), which skips the c/mtime update
for FMODE_NOCMTIME files. But the helpers that btrfs_fallocate() calls
update the times anyway:

- btrfs_prealloc_file_range() sets ctime. Its
  insert_prealloc_file_extent() tells btrfs_replace_file_extents() to
  update c/mtime.
- btrfs_fallocate_update_isize() sets ctime.

btrfs_zero_range() also calls these helpers. nfsd sets FMODE_NOCMTIME
while a client holds delegated timestamps. Thus an ALLOCATE from the
holder advances the server's ctime, and the times that the holder
returns cannot move it back.

Pass an update_times flag from btrfs_fallocate() to these helpers. Set
the flag from btrfs_should_update_cmtime(). Relocation still updates the
times.

btrfs_fallocate_update_isize() never incremented i_version. When a size
extension stayed within existing preallocation, only the ctime update
changed the NFS change attribute. Increment i_version unconditionally
there, as btrfs_prealloc_file_range() does.

[zygo: 6.18 backport: 6.18 still has the __btrfs_prealloc_file_range()
wrapper and btrfs_prealloc_file_range_trans() for the v1 space cache.
update_times goes through insert_prealloc_file_extent() and
__btrfs_prealloc_file_range(); btrfs_prealloc_file_range_trans() passes
true, so the v1 cache inode keeps updating its times as before.
btrfs_fallocate() context lacks the btrfs_is_shutdown() check]

Fixes: e5e9b24ab8fa ("nfsd: freeze c/mtime updates with outstanding WRITE_ATTRS delegation")
Signed-off-by: Jeff Layton <jlayton@kernel.org>
Link: https://lore.kernel.org/r/20261105-delegts-btrfs-v1-3-a42a2b93cdc2@kernel.org
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/btrfs_inode.h
fs/btrfs/file.c
fs/btrfs/inode.c
fs/btrfs/relocation.c

index b6b46f951f11a2cf373a932c1c4f9d1cf2bce4d8..481b2e7329b3906cc9e921d96a27079cdd815809 100644 (file)
@@ -631,7 +631,8 @@ void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
 int btrfs_prealloc_file_range(struct inode *inode, int mode,
                              u64 start, u64 num_bytes, u64 min_size,
-                             loff_t actual_len, u64 *alloc_hint);
+                             loff_t actual_len, u64 *alloc_hint,
+                             bool update_times);
 int btrfs_prealloc_file_range_trans(struct inode *inode,
                                    struct btrfs_trans_handle *trans, int mode,
                                    u64 start, u64 num_bytes, u64 min_size,
index 7508f0246ff1161d9c7e84ed4eafa94cdde52162..e0dd846fe4e0045e531c3b68290a349b515764a6 100644 (file)
@@ -2825,7 +2825,8 @@ static int add_falloc_range(struct list_head *head, u64 start, u64 len)
 
 static int btrfs_fallocate_update_isize(struct inode *inode,
                                        const u64 end,
-                                       const int mode)
+                                       const int mode,
+                                       bool update_times)
 {
        struct btrfs_trans_handle *trans;
        struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -2849,7 +2850,9 @@ static int btrfs_fallocate_update_isize(struct inode *inode,
        if (IS_ERR(trans))
                return PTR_ERR(trans);
 
-       inode_set_ctime_current(inode);
+       inode_inc_iversion(inode);
+       if (update_times)
+               inode_set_ctime_current(inode);
        i_size_write(inode, end);
        btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
        ret = btrfs_update_inode(trans, BTRFS_I(inode));
@@ -2890,7 +2893,8 @@ static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
 static int btrfs_zero_range(struct inode *inode,
                            loff_t offset,
                            loff_t len,
-                           const int mode)
+                           const int mode,
+                           bool update_times)
 {
        struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
        struct extent_map *em;
@@ -2931,7 +2935,7 @@ static int btrfs_zero_range(struct inode *inode,
                         */
                        btrfs_free_extent_map(em);
                        ret = btrfs_fallocate_update_isize(inode, offset + len,
-                                                          mode);
+                                                          mode, update_times);
                        goto out;
                }
                /*
@@ -2957,7 +2961,7 @@ static int btrfs_zero_range(struct inode *inode,
                if (em->flags & EXTENT_FLAG_PREALLOC) {
                        btrfs_free_extent_map(em);
                        ret = btrfs_fallocate_update_isize(inode, offset + len,
-                                                          mode);
+                                                          mode, update_times);
                        goto out;
                }
                if (len < sectorsize && em->disk_bytenr != EXTENT_MAP_HOLE) {
@@ -2967,7 +2971,8 @@ static int btrfs_zero_range(struct inode *inode,
                        if (!ret)
                                ret = btrfs_fallocate_update_isize(inode,
                                                                   offset + len,
-                                                                  mode);
+                                                                  mode,
+                                                                  update_times);
                        return ret;
                }
                btrfs_free_extent_map(em);
@@ -3045,7 +3050,8 @@ reserve_space:
                ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
                                                alloc_end - alloc_start,
                                                fs_info->sectorsize,
-                                               offset + len, &alloc_hint);
+                                               offset + len, &alloc_hint,
+                                               update_times);
                btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
                                    &cached_state);
                /* btrfs_prealloc_file_range releases reserved space on error */
@@ -3054,7 +3060,8 @@ reserve_space:
                        goto out;
                }
        }
-       ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
+       ret = btrfs_fallocate_update_isize(inode, offset + len, mode,
+                                          update_times);
  out:
        if (ret && space_reserved)
                btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
@@ -3085,6 +3092,7 @@ static long btrfs_fallocate(struct file *file, int mode,
        u64 qgroup_reserved = 0;
        struct extent_map *em;
        int blocksize = BTRFS_I(inode)->root->fs_info->sectorsize;
+       const bool update_times = btrfs_should_update_cmtime(file);
        int ret;
 
        /* Do not allow fallocate in ZONED mode */
@@ -3153,7 +3161,7 @@ static long btrfs_fallocate(struct file *file, int mode,
                goto out;
 
        if (mode & FALLOC_FL_ZERO_RANGE) {
-               ret = btrfs_zero_range(inode, offset, len, mode);
+               ret = btrfs_zero_range(inode, offset, len, mode, update_times);
                btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
                return ret;
        }
@@ -3218,7 +3226,8 @@ static long btrfs_fallocate(struct file *file, int mode,
                        ret = btrfs_prealloc_file_range(inode, mode,
                                        range->start,
                                        range->len, blocksize,
-                                       offset + len, &alloc_hint);
+                                       offset + len, &alloc_hint,
+                                       update_times);
                        /*
                         * btrfs_prealloc_file_range() releases space even
                         * if it returns an error.
@@ -3246,7 +3255,8 @@ static long btrfs_fallocate(struct file *file, int mode,
         * We didn't need to allocate any more space, but we still extended the
         * size of the file so we need to update i_size and the inode item.
         */
-       ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
+       ret = btrfs_fallocate_update_isize(inode, actual_end, mode,
+                                          update_times);
 out_unlock:
        btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
                            &cached_state);
index 9490d7c6cec4754da51e0ea5418fa22c0188ca31..7e6cffa05b556e00c6bb31e8f6ea74c521e11651 100644 (file)
@@ -9060,7 +9060,7 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
                                       struct btrfs_trans_handle *trans_in,
                                       struct btrfs_inode *inode,
                                       struct btrfs_key *ins,
-                                      u64 file_offset)
+                                      u64 file_offset, bool update_times)
 {
        struct btrfs_file_extent_item stack_fi;
        struct btrfs_replace_extent_info extent_info;
@@ -9112,7 +9112,7 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
 
        ret = btrfs_replace_file_extents(inode, path, file_offset,
                                     file_offset + len - 1, &extent_info,
-                                    true, &trans);
+                                    update_times, &trans);
        btrfs_free_path(path);
        if (ret)
                goto free_qgroup;
@@ -9135,7 +9135,8 @@ free_qgroup:
 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
                                       u64 start, u64 num_bytes, u64 min_size,
                                       loff_t actual_len, u64 *alloc_hint,
-                                      struct btrfs_trans_handle *trans)
+                                      struct btrfs_trans_handle *trans,
+                                      bool update_times)
 {
        struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
        struct extent_map *em;
@@ -9178,7 +9179,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
 
                last_alloc = ins.offset;
                trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
-                                                   &ins, cur_offset);
+                                                   &ins, cur_offset,
+                                                   update_times);
                /*
                 * Now that we inserted the prealloc extent we can finally
                 * decrement the number of reservations in the block group.
@@ -9218,7 +9220,8 @@ next:
                *alloc_hint = ins.objectid + ins.offset;
 
                inode_inc_iversion(inode);
-               inode_set_ctime_current(inode);
+               if (update_times)
+                       inode_set_ctime_current(inode);
                BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
                if (!(mode & FALLOC_FL_KEEP_SIZE) &&
                    (actual_len > inode->i_size) &&
@@ -9253,11 +9256,12 @@ next:
 
 int btrfs_prealloc_file_range(struct inode *inode, int mode,
                              u64 start, u64 num_bytes, u64 min_size,
-                             loff_t actual_len, u64 *alloc_hint)
+                             loff_t actual_len, u64 *alloc_hint,
+                             bool update_times)
 {
        return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
                                           min_size, actual_len, alloc_hint,
-                                          NULL);
+                                          NULL, update_times);
 }
 
 int btrfs_prealloc_file_range_trans(struct inode *inode,
@@ -9266,7 +9270,8 @@ int btrfs_prealloc_file_range_trans(struct inode *inode,
                                    loff_t actual_len, u64 *alloc_hint)
 {
        return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
-                                          min_size, actual_len, alloc_hint, trans);
+                                          min_size, actual_len, alloc_hint, trans,
+                                          true);
 }
 
 static int btrfs_permission(struct mnt_idmap *idmap,
index fcb0ab937de5c568f0cdd6089874c26cbbe32e7a..6a27f3b6ebe181f6e6624361e5f86b6e648e03bc 100644 (file)
@@ -2735,7 +2735,7 @@ static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control
                num_bytes = end + 1 - start;
                ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
                                                num_bytes, num_bytes,
-                                               end + 1, &alloc_hint);
+                                               end + 1, &alloc_hint, true);
                cur_offset = end + 1;
                btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state);
                if (ret)