1f38cb554SJohn Wren Kennedy#!/usr/bin/bash -p
2f38cb554SJohn Wren Kennedy#
3f38cb554SJohn Wren Kennedy# CDDL HEADER START
4f38cb554SJohn Wren Kennedy#
5f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
6f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
7f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
8f38cb554SJohn Wren Kennedy#
9f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
11f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
12f38cb554SJohn Wren Kennedy# and limitations under the License.
13f38cb554SJohn Wren Kennedy#
14f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
15f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
17f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
18f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
19f38cb554SJohn Wren Kennedy#
20f38cb554SJohn Wren Kennedy# CDDL HEADER END
21f38cb554SJohn Wren Kennedy#
22f38cb554SJohn Wren Kennedy
23f38cb554SJohn Wren Kennedy#
24f38cb554SJohn Wren Kennedy# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25f38cb554SJohn Wren Kennedy# Use is subject to license terms.
26f38cb554SJohn Wren Kennedy#
27f38cb554SJohn Wren Kennedy
28f38cb554SJohn Wren Kennedy#
29*1d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30f38cb554SJohn Wren Kennedy#
31f38cb554SJohn Wren Kennedy
32f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
33f38cb554SJohn Wren Kennedy. $STF_SUITE/tests/functional/reservation/reservation.shlib
34f38cb554SJohn Wren Kennedy
35f38cb554SJohn Wren Kennedy#
36f38cb554SJohn Wren Kennedy# DESCRIPTION:
37f38cb554SJohn Wren Kennedy#
38f38cb554SJohn Wren Kennedy# Setting a reservation reserves a defined minimum amount of space for
39f38cb554SJohn Wren Kennedy# a dataset, and prevents other datasets using that space. Verify that
40f38cb554SJohn Wren Kennedy# reducing the reservation on a filesystem allows other datasets in
41f38cb554SJohn Wren Kennedy# the pool to use that space.
42f38cb554SJohn Wren Kennedy#
43f38cb554SJohn Wren Kennedy# STRATEGY:
44f38cb554SJohn Wren Kennedy# 1) Create multiple filesystems
45f38cb554SJohn Wren Kennedy# 2) Set reservations on all bar one of the filesystems
46f38cb554SJohn Wren Kennedy# 3) Fill up the one non-reserved filesystem
47f38cb554SJohn Wren Kennedy# 4) Reduce one of the reservations and verify can write more
48f38cb554SJohn Wren Kennedy# data into the non-reserved filesystem
49f38cb554SJohn Wren Kennedy#
50f38cb554SJohn Wren Kennedy
51f38cb554SJohn Wren Kennedyverify_runnable "both"
52f38cb554SJohn Wren Kennedy
53f38cb554SJohn Wren Kennedylog_assert "Verify reducing reservation allows other datasets to use space"
54f38cb554SJohn Wren Kennedy
55f38cb554SJohn Wren Kennedyfunction cleanup
56f38cb554SJohn Wren Kennedy{
57f38cb554SJohn Wren Kennedy	typeset -i loop=0
58f38cb554SJohn Wren Kennedy	while (($loop < $RESV_NUM_FS)); do
59f38cb554SJohn Wren Kennedy		datasetexists $TESTPOOL/${TESTFS}$loop && \
60*1d32ba66SJohn Wren Kennedy		    log_must zfs destroy -f $TESTPOOL/${TESTFS}$loop
61f38cb554SJohn Wren Kennedy
62*1d32ba66SJohn Wren Kennedy		[[ -d ${TESTDIR}$loop ]] && log_must rm -r ${TESTDIR}$loop
63f38cb554SJohn Wren Kennedy
64f38cb554SJohn Wren Kennedy		((loop = loop + 1))
65f38cb554SJohn Wren Kennedy	done
66f38cb554SJohn Wren Kennedy}
67f38cb554SJohn Wren Kennedy
68f38cb554SJohn Wren Kennedylog_onexit cleanup
69f38cb554SJohn Wren Kennedy
70f38cb554SJohn Wren Kennedylog_must create_multiple_fs $RESV_NUM_FS $TESTPOOL/$TESTFS $TESTDIR
71f38cb554SJohn Wren Kennedy
72f38cb554SJohn Wren Kennedyspace_avail=`get_prop available $TESTPOOL`
73f38cb554SJohn Wren Kennedyspace_used=`get_prop used $TESTPOOL`
74f38cb554SJohn Wren Kennedy
75f38cb554SJohn Wren Kennedy#
76f38cb554SJohn Wren Kennedy# To make sure this test doesn't take too long to execute on
77f38cb554SJohn Wren Kennedy# large pools, we calculate a reservation setting which when
78f38cb554SJohn Wren Kennedy# applied to all bar one of the filesystems (RESV_NUM_FS-1) will
79f38cb554SJohn Wren Kennedy# ensure we have RESV_FREE_SPACE left free in the pool, which we will
80f38cb554SJohn Wren Kennedy# be able to quickly fill.
81f38cb554SJohn Wren Kennedy#
82f38cb554SJohn Wren Kennedyresv_space_avail=`expr $space_avail - $RESV_FREE_SPACE`
83f38cb554SJohn Wren Kennedynum_resv_fs=`expr $RESV_NUM_FS - 1` # Number of FS to which resv will be applied
84f38cb554SJohn Wren Kennedyresv_size_set=`expr $resv_space_avail / $num_resv_fs`
85f38cb554SJohn Wren Kennedy
86f38cb554SJohn Wren Kennedy#
87f38cb554SJohn Wren Kennedy# We set the reservations now, rather than when we created the filesystems
88f38cb554SJohn Wren Kennedy# to allow us to take into account space used by the filsystem metadata
89f38cb554SJohn Wren Kennedy#
90f38cb554SJohn Wren Kennedy# Note we don't set a reservation on the first filesystem we created,
91f38cb554SJohn Wren Kennedy# hence num=1 rather than zero below.
92f38cb554SJohn Wren Kennedy#
93f38cb554SJohn Wren Kennedytypeset -i num=1
94f38cb554SJohn Wren Kennedywhile (($num < $RESV_NUM_FS)); do
95*1d32ba66SJohn Wren Kennedy	log_must zfs set reservation=$resv_size_set $TESTPOOL/$TESTFS$num
96f38cb554SJohn Wren Kennedy	((num = num + 1))
97f38cb554SJohn Wren Kennedydone
98f38cb554SJohn Wren Kennedy
99f38cb554SJohn Wren Kennedyspace_avail_still=`get_prop available $TESTPOOL`
100f38cb554SJohn Wren Kennedy
101f38cb554SJohn Wren Kennedyfill_size=`expr $space_avail_still + $RESV_TOLERANCE`
102f38cb554SJohn Wren Kennedywrite_count=`expr $fill_size / $BLOCK_SIZE`
103f38cb554SJohn Wren Kennedy
104f38cb554SJohn Wren Kennedy# Now fill up the first filesystem (which doesn't have a reservation set
105f38cb554SJohn Wren Kennedy# and thus will use up whatever free space is left in the pool).
106f38cb554SJohn Wren Kennedynum=0
107f38cb554SJohn Wren Kennedylog_note "Writing to $TESTDIR$num/$TESTFILE1"
108f38cb554SJohn Wren Kennedy
109*1d32ba66SJohn Wren Kennedyfile_write -o create -f $TESTDIR$num/$TESTFILE1 -b $BLOCK_SIZE \
110f38cb554SJohn Wren Kennedy    -c $write_count -d 0
111f38cb554SJohn Wren Kennedyret=$?
112f38cb554SJohn Wren Kennedyif (($ret != $ENOSPC)); then
113f38cb554SJohn Wren Kennedy	log_fail "Did not get ENOSPC as expected (got $ret)."
114f38cb554SJohn Wren Kennedyfi
115f38cb554SJohn Wren Kennedy
116f38cb554SJohn Wren Kennedy# Remove the reservation on one of the other filesystems and verify
117f38cb554SJohn Wren Kennedy# can write more data to the original non-reservation filesystem.
118f38cb554SJohn Wren Kennedynum=1
119*1d32ba66SJohn Wren Kennedylog_must zfs set reservation=none $TESTPOOL/${TESTFS}$num
120f38cb554SJohn Wren Kennedynum=0
121*1d32ba66SJohn Wren Kennedylog_must file_write -o create -f ${TESTDIR}$num/$TESTFILE2 -b pagesize \
122f38cb554SJohn Wren Kennedy    -c 1000 -d 0
123f38cb554SJohn Wren Kennedy
124f38cb554SJohn Wren Kennedylog_pass "reducing reservation allows other datasets to use space"
125