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#
27
28. $STF_SUITE/include/libtest.shlib
29
30#
31# DESCRIPTION:
32# Test FALLOC_FL_ZERO_RANGE functionality
33#
34# STRATEGY:
35# 1. Create a dense file
36# 2. Zero various ranges in the file and verify the result.
37#
38
39verify_runnable "global"
40
41if is_freebsd; then
42	log_unsupported "FreeBSD does not implement an analogue to ZERO_RANGE."
43fi
44
45FILE=$TESTDIR/$TESTFILE0
46BLKSZ=$(get_prop recordsize $TESTPOOL)
47
48function cleanup
49{
50	[[ -e $TESTDIR ]] && log_must rm -f $FILE
51}
52
53# Helpfully, this function expects kilobytes, and check_apparent_size expects bytes.
54function check_reported_size
55{
56	typeset expected_size=$1
57
58	if ! [ -e "${FILE}" ]; then
59		log_fail "$FILE does not exist"
60	fi
61
62	reported_size=$(du "${FILE}" | awk '{print $1}')
63	if [ "$reported_size" != "$expected_size" ]; then
64		log_fail "Incorrect reported size: $reported_size != $expected_size"
65	fi
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 "Incorrect apparent size: $apparent_size != $expected_size"
75	fi
76}
77
78log_assert "Ensure ranges can be zeroed in files"
79
80log_onexit cleanup
81
82# Create a dense file and check it is the correct size.
83log_must file_write -o create -f $FILE -b $BLKSZ -c 8
84sync_pool $TESTPOOL
85log_must check_reported_size 1027
86
87# Zero a range covering the first full block.
88log_must zero_range 0 $BLKSZ $FILE
89sync_pool $TESTPOOL
90log_must check_reported_size 899
91
92# Partially zero a range in the second block.
93log_must zero_range $BLKSZ $((BLKSZ / 2)) $FILE
94sync_pool $TESTPOOL
95log_must check_reported_size 899
96
97# Zero range which overlaps the third and fourth block.
98log_must zero_range $(((BLKSZ * 2) + (BLKSZ / 2))) $((BLKSZ)) $FILE
99sync_pool $TESTPOOL
100log_must check_reported_size 899
101
102# Zero range from the fifth block past the end of file, with --keep-size.
103# The apparent file size must not change, since we did specify --keep-size.
104apparent_size=$(stat_size $FILE)
105log_must fallocate --keep-size --zero-range --offset $((BLKSZ * 4)) --length $((BLKSZ * 10)) "$FILE"
106sync_pool $TESTPOOL
107log_must check_reported_size 387
108log_must check_apparent_size $apparent_size
109
110# Zero range from the fifth block past the end of file.  The apparent
111# file size should change since --keep-size is not implied, unlike
112# with PUNCH_HOLE.
113apparent_size=$(stat_size $FILE)
114log_must zero_range $((BLKSZ * 4)) $((BLKSZ * 10)) $FILE
115sync_pool $TESTPOOL
116log_must check_reported_size 387
117log_must check_apparent_size $((BLKSZ * 14))
118
119log_pass "Ensure ranges can be zeroed in files"
120