xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/fallocate/fallocate_extend_timestamps.ksh (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# https://opensource.org/license/CDDL-1.0.
12#
13
14#
15# Copyright (c) 2026 by iXsystems, Inc.
16#
17
18. $STF_SUITE/include/libtest.shlib
19. $STF_SUITE/include/math.shlib
20
21#
22# DESCRIPTION:
23# A fallocate(2) that extends a file past EOF must update mtime/ctime; a
24# FALLOC_FL_KEEP_SIZE preallocation must not change the size or timestamps.
25#
26# STRATEGY:
27# 1. Create a small file and record its mtime/ctime
28# 2. fallocate -l to a larger size (extend) and verify the size grew and both
29#    mtime and ctime advanced
30# 3. fallocate --keep-size to a larger length and verify the size and both
31#    timestamps are unchanged
32#
33
34verify_runnable "global"
35
36FILE=$TESTDIR/$TESTFILE0
37STARTSIZE=$((128 * 1024))
38
39function cleanup
40{
41	[[ -f $FILE ]] && rm -f $FILE
42}
43
44# extend past EOF: size must grow and mtime/ctime must advance
45function verify_extend # <newsize>
46{
47	typeset -i size="$1"
48
49	log_must mkfile $STARTSIZE $FILE
50	typeset -i tm="$(stat -c %Y $FILE)"
51	typeset -i tc="$(stat -c %Z $FILE)"
52	log_must sleep 1
53	log_must fallocate -l $size $FILE
54	verify_eq $size "$(stat_size $FILE)" "size"
55	verify_ne $tm "$(stat -c %Y $FILE)" "mtime"
56	verify_ne $tc "$(stat -c %Z $FILE)" "ctime"
57	log_must rm -f $FILE
58}
59
60log_assert "Ensure fallocate extend updates mtime/ctime and --keep-size does not"
61log_onexit cleanup
62
63verify_extend $((256 * 1024))
64verify_extend $((1024 * 1024))
65
66# --keep-size preallocation must not change size or timestamps
67log_must mkfile $STARTSIZE $FILE
68typeset -i tm="$(stat -c %Y $FILE)"
69typeset -i tc="$(stat -c %Z $FILE)"
70log_must sleep 1
71log_must fallocate --keep-size -l $((1024 * 1024)) $FILE
72verify_eq $STARTSIZE "$(stat_size $FILE)" "size"
73verify_eq $tm "$(stat -c %Y $FILE)" "mtime"
74verify_eq $tc "$(stat -c %Z $FILE)" "ctime"
75log_must rm -f $FILE
76
77log_pass "fallocate extend correctly updates timestamps"
78