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