xref: /freebsd/tests/sys/cddl/zfs/tests/quota/quota.kshlib (revision 4b9d6057)
1# vim: filetype=sh
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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27# BLOCK_SIZE, QUOTA_VALUE and TOLERANCE set in quota.cfg
28readonly EDQUOT=69
29
30#
31# Function to fill the quota of a zfs filesystem
32#
33# $1 - The File system or container to fill.
34# $2 - The mountpoint to use.
35#
36function fill_quota
37{
38	typeset FILESYSTEM="$1"
39	typeset MNTPT="$2"
40
41	log_must $ZFS set quota=$QUOTA_VALUE $FILESYSTEM
42
43	typeset -i write_size=0
44	(( write_size = 2 * QUOTA_VALUE ))
45
46	typeset -i zret=0
47	$FILE_WRITE -o create -f $MNTPT/$TESTFILE1 -b $BLOCK_SIZE \
48		 -c $write_size -d 0
49	zret=$?
50        [[ $zret -ne EDQUOT ]] && log_fail "Got error $zret; expected $EDQUOT"
51
52	typeset -i file_size=`$LS -ls $MNTPT/$TESTFILE1 | $AWK '{ print $1 }'`
53	typeset -i limit=0
54	(( file_size = file_size * 512 ))
55	(( limit = QUOTA_VALUE + TOLERANCE ))
56	(( file_size > limit )) && \
57	    log_fail "File was created larger than the quota value, aborting!!!"
58	return 0
59}
60
61#
62# Function attempts to write another file in a ZFS filesystem
63# that has already filled its quota
64#
65function exceed_quota
66{
67	typeset FILESYSTEM="$1"
68	typeset MNTPT="$2"
69
70	log_must fill_quota $FILESYSTEM $MNTPT
71	typeset -i write_size=0
72        (( write_size = 2 * QUOTA_VALUE ))
73	typeset -i zret=0
74	#
75	# Writing a file without API to access return code
76	#
77	log_note "Creating a file in a FS that has already exceeded its quota"
78	$FILE_WRITE -o create -f $MNTPT/$TESTFILE2 \
79		-b $BLOCK_SIZE -c $write_size -d 0
80	zret=$?
81        [[ $zret -ne EDQUOT ]] && log_fail "Got error $zret; expected $EDQUOT"
82	return 0
83}
84