1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
25# Copyright (c) 2021 by The FreeBSD Foundation.
26# Copyright (c) 2022 by Delphix. All rights reserved.
27#
28
29. $STF_SUITE/include/libtest.shlib
30
31#
32# DESCRIPTION:
33# Test FALLOC_FL_ZERO_RANGE functionality
34#
35# STRATEGY:
36# 1. Create a dense file
37# 2. Zero various ranges in the file and verify the result.
38#
39# Note: We can't compare exact block numbers as reported by du, because
40# different backing stores may allocate different numbers of blocks for
41# the same amount of data.
42#
43
44verify_runnable "global"
45
46if is_freebsd; then
47	log_unsupported "FreeBSD does not implement an analogue to ZERO_RANGE."
48fi
49
50FILE=$TESTDIR/$TESTFILE0
51BLKSZ=$(get_prop recordsize $TESTPOOL)
52
53function cleanup
54{
55	[[ -e $TESTDIR ]] && log_must rm -f $FILE
56}
57
58function get_reported_size
59{
60	if ! [ -e "$FILE" ]; then
61		log_fail "$FILE does not exist"
62	fi
63
64	sync_pool $TESTPOOL >/dev/null 2>&1
65	du "$FILE" | awk '{print $1}'
66}
67
68function check_apparent_size
69{
70	typeset expected_size=$1
71
72	apparent_size=$(stat_size "$FILE")
73	if [ "$apparent_size" != "$expected_size" ]; then
74		log_fail \
75		    "Incorrect apparent size: $apparent_size != $expected_size"
76	fi
77}
78
79log_assert "Ensure ranges can be zeroed in files"
80
81log_onexit cleanup
82
83# Create a dense file and check it is the correct size.
84log_must file_write -o create -f $FILE -b $BLKSZ -c 8
85sync_pool $TESTPOOL
86full_size=$(get_reported_size)
87
88# Zero a range covering the first full block. The reported size should decrease.
89log_must zero_range 0 $BLKSZ $FILE
90one_range=$(get_reported_size)
91[[ $full_size -gt $one_range ]] || log_fail \
92    "One range failure: $full_size -> $one_range"
93
94# Partially zero a range in the second block. The reported size should
95# remain constant.
96log_must zero_range $BLKSZ $((BLKSZ / 2)) $FILE
97partial_range=$(get_reported_size)
98[[ $one_range -eq $partial_range ]] || log_fail \
99    "Partial range failure: $one_range -> $partial_range"
100
101# Zero range which overlaps the third and fourth block. The reported size
102# should remain constant.
103log_must zero_range $(((BLKSZ * 2) + (BLKSZ / 2))) $((BLKSZ)) $FILE
104overlap_range=$(get_reported_size)
105[[ $one_range -eq $overlap_range ]] || log_fail \
106    "Overlap range failure: $one_range -> $overlap_range"
107
108# Zero range from the fifth block past the end of file, with --keep-size.
109# The reported size should decrease, and the apparent file size must not
110# change, since we did specify --keep-size.
111apparent_size=$(stat_size $FILE)
112log_must fallocate --keep-size --zero-range --offset $((BLKSZ * 4)) --length $((BLKSZ * 10)) "$FILE"
113eof_range=$(get_reported_size)
114[[ $overlap_range -gt $eof_range ]] || log_fail \
115    "EOF range failure: $overlap_range -> $eof_range"
116log_must check_apparent_size $apparent_size
117
118# Zero range from the fifth block past the end of file.  The apparent
119# file size should change since --keep-size is not implied, unlike
120# with PUNCH_HOLE. The reported size should remain constant.
121apparent_size=$(stat_size $FILE)
122log_must zero_range $((BLKSZ * 4)) $((BLKSZ * 10)) $FILE
123eof_range2=$(get_reported_size)
124[[ $eof_range -eq $eof_range2 ]] || log_fail \
125    "Second EOF range failure: $eof_range -> $eof_range2"
126log_must check_apparent_size $((BLKSZ * 14))
127
128log_pass "Ensure ranges can be zeroed in files"
129