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 hole-punching functionality
34#
35# STRATEGY:
36# 1. Create a dense file
37# 2. Punch an assortment of holes 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
46#
47# Prior to __FreeBSD_version 1400032 there are no mechanism to punch hole in a
48# file on FreeBSD.  truncate -d support is required to call fspacectl(2) on
49# behalf of the script.
50#
51if is_freebsd; then
52	if [[ $(uname -K) -lt 1400032 ]]; then
53		log_unsupported "Requires fspacectl(2) support on FreeBSD"
54	fi
55	if truncate -d 2>&1 | grep "illegal option" > /dev/null; then
56		log_unsupported "Requires truncate(1) -d support on FreeBSD"
57	fi
58fi
59
60FILE=$TESTDIR/$TESTFILE0
61BLKSZ=$(get_prop recordsize $TESTPOOL)
62
63function cleanup
64{
65	[[ -e $TESTDIR ]] && log_must rm -f $FILE
66}
67
68function get_reported_size
69{
70	if ! [ -e "$FILE" ]; then
71		log_fail "$FILE does not exist"
72	fi
73
74	sync_pool $TESTPOOL >/dev/null 2>&1
75	du "$FILE" | awk '{print $1}'
76}
77
78function check_apparent_size
79{
80	typeset expected_size=$1
81
82	apparent_size=$(stat_size "$FILE")
83	if [ "$apparent_size" != "$expected_size" ]; then
84		log_fail \
85		    "Incorrect apparent size: $apparent_size != $expected_size"
86	fi
87}
88
89log_assert "Ensure holes can be punched in files making them sparse"
90
91log_onexit cleanup
92
93# Create a dense file and check it is the correct size.
94log_must file_write -o create -f $FILE -b $BLKSZ -c 8
95full_size=$(get_reported_size)
96
97# Punch a hole for the first full block. The reported size should decrease.
98log_must punch_hole 0 $BLKSZ $FILE
99one_hole=$(get_reported_size)
100[[ $full_size -gt $one_hole ]] || log_fail \
101    "One hole failure: $full_size -> $one_hole"
102
103# Partially punch a hole in the second block. The reported size should
104# remain constant.
105log_must punch_hole $BLKSZ $((BLKSZ / 2)) $FILE
106partial_hole=$(get_reported_size)
107[[ $one_hole -eq $partial_hole ]] || log_fail \
108    "Partial hole failure: $one_hole -> $partial_hole"
109
110# Punch a hole which overlaps the third and fourth block. The reported size
111# should remain constant.
112log_must punch_hole $(((BLKSZ * 2) + (BLKSZ / 2))) $((BLKSZ)) $FILE
113overlap_hole=$(get_reported_size)
114[[ $one_hole -eq $overlap_hole ]] || log_fail \
115    "Overlap hole failure: $one_hole -> $overlap_hole"
116
117# Punch a hole from the fifth block past the end of file.  The reported size
118# should decrease, and the apparent file size should not change since
119# --keep-size is implied.
120apparent_size=$(stat_size $FILE)
121log_must punch_hole $((BLKSZ * 4)) $((BLKSZ * 10)) $FILE
122eof_hole=$(get_reported_size)
123[[ $overlap_hole -gt $eof_hole ]] || log_fail \
124    "EOF hole failure: $overlap_hole -> $eof_hole"
125log_must check_apparent_size $apparent_size
126
127log_pass "Ensure holes can be punched in files making them sparse"
128