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