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 http://www.opensolaris.org/os/licensing.
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#
26
27. $STF_SUITE/include/libtest.shlib
28
29#
30# DESCRIPTION:
31# Test `fallocate --punch-hole`
32#
33# STRATEGY:
34# 1. Create a dense file
35# 2. Punch an assortment of holes in the file and verify the result.
36#
37
38verify_runnable "global"
39
40FILE=$TESTDIR/$TESTFILE0
41BLKSZ=$(get_prop recordsize $TESTPOOL)
42
43function cleanup
44{
45	[[ -e $TESTDIR ]] && log_must rm -f $FILE
46}
47
48function check_disk_size
49{
50	typeset expected_size=$1
51
52	disk_size=$(du $TESTDIR/file | awk '{print $1}')
53	if [ $disk_size -ne $expected_size ]; then
54		log_fail "Incorrect size: $disk_size != $expected_size"
55	fi
56}
57
58function check_apparent_size
59{
60	typeset expected_size=$1
61
62	apparent_size=$(stat_size)
63	if [ $apparent_size -ne $expected_size ]; then
64		log_fail "Incorrect size: $apparent_size != $expected_size"
65	fi
66}
67
68log_assert "Ensure holes can be punched in files making them sparse"
69
70log_onexit cleanup
71
72# Create a dense file and check it is the correct size.
73log_must file_write -o create -f $FILE -b $BLKSZ -c 8
74log_must check_disk_size  $((131072 * 8))
75
76# Punch a hole for the first full block.
77log_must fallocate --punch-hole --offset 0 --length $BLKSZ $FILE
78log_must check_disk_size  $((131072 * 7))
79
80# Partially punch a hole in the second block.
81log_must fallocate --punch-hole --offset $BLKSZ --length $((BLKSZ / 2)) $FILE
82log_must check_disk_size  $((131072 * 7))
83
84# Punch a hole which overlaps the third and forth block.
85log_must fallocate --punch-hole --offset $(((BLKSZ * 2) + (BLKSZ / 2))) \
86    --length $((BLKSZ)) $FILE
87log_must check_disk_size  $((131072 * 7))
88
89# Punch a hole from the fifth block past the end of file.  The apparent
90# file size should not change since --keep-size is implied.
91apparent_size=$(stat_size $FILE)
92log_must fallocate --punch-hole --offset $((BLKSZ * 4)) \
93    --length $((BLKSZ * 10)) $FILE
94log_must check_disk_size  $((131072 * 4))
95log_must check_apparent_size $apparent_size
96
97log_pass "Ensure holes can be punched in files making them sparse"
98