xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/quota/quota.kshlib (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/tests/functional/quota/quota.cfg
32
33# BLOCK_SIZE, QUOTA_VALUE and TOLERANCE set in quota.cfg
34readonly EDQUOT=49
35
36#
37# Function to fill the quota of a zfs filesystem
38#
39# $1 - The File system or container to fill.
40# $2 - The mountpoint to use.
41#
42function fill_quota
43{
44	typeset FILESYSTEM="$1"
45	typeset MNTPT="$2"
46
47	log_must zfs set quota=$QUOTA_VALUE $FILESYSTEM
48
49	typeset -i write_size=0
50	(( write_size = 2 * QUOTA_VALUE ))
51
52	typeset -i zret=0
53	file_write -o create -f $MNTPT/$TESTFILE1 -b $BLOCK_SIZE \
54		 -c $write_size -d 0
55	zret=$?
56        [[ $zret -ne EDQUOT ]] && \
57            log_fail "Returned error code: $zret. Expected: $EDQUOT."
58
59	typeset -i file_size=`ls -ls $MNTPT/$TESTFILE1 | awk '{ print $1 }'`
60	typeset -i limit=0
61	(( file_size = file_size * 512 ))
62	(( limit = QUOTA_VALUE + TOLERANCE ))
63	(( file_size > limit )) && \
64	    log_fail "File was created larger than the quota value, aborting!!!"
65	return 0
66}
67
68#
69# Function attempts to write another file in a ZFS filesystem
70# that has already filled its quota
71#
72function exceed_quota
73{
74	typeset FILESYSTEM="$1"
75	typeset MNTPT="$2"
76
77	log_must fill_quota $FILESYSTEM $MNTPT
78	typeset -i write_size=0
79        (( write_size = 2 * QUOTA_VALUE ))
80	typeset -i zret=0
81	#
82	# Writing a file without API to access return code
83	#
84	log_note "Creating a file in a FS that has already exceeded its quota"
85	file_write -o create -f $MNTPT/$TESTFILE2 \
86		-b $BLOCK_SIZE -c $write_size -d 0
87	zret=$?
88	[[ $zret -ne EDQUOT ]] && \
89	    log_fail "Returned error code: $zret. Expected: EDQUOT."
90	return 0
91}
92