1# $NetBSD: t_check_quotas.sh,v 1.3 2023/09/26 12:15:44 kre Exp $ 
2#
3#  Copyright (c) 2011 Manuel Bouyer
4#  All rights reserved.
5# 
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions
8#  are met:
9#  1. Redistributions of source code must retain the above copyright
10#     notice, this list of conditions and the following disclaimer.
11#  2. Redistributions in binary form must reproduce the above copyright
12#     notice, this list of conditions and the following disclaimer in the
13#     documentation and/or other materials provided with the distribution.
14# 
15#  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18#  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19#  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25#  POSSIBILITY OF SUCH DAMAGE.
26#
27
28for e in le be; do
29  for v in 1 2; do
30    test_case "corrupt_list_${e}_${v}" corrupt_list \
31	"recovery of corrupted free list in" "${e}" "${v}"
32    test_case "expand1_list_${e}_${v}" expand_list \
33	"allocation of direct block in" 40 "${e}" "${v}"
34    test_case "expand2_list_${e}_${v}" expand_list \
35	"allocation of indirect block in" 1000 "${e}" "${v}"
36  done
37done
38
39corrupt_list()
40{
41	create_with_quotas "$@"
42	local blkno=$(printf "inode 3\nblks\n" |
43		/sbin/fsdb -nF -f "${IMG}" |
44		awk '$1 == "0:" {print $2}')
45	blkno=$(($blkno * 512 + 104))
46	#clear the free list
47	atf_check -o ignore -e ignore dd if=/dev/zero "of=${IMG}" bs=1 \
48		count=8 "seek=${blkno}" conv=notrunc
49	atf_check -s exit:0 \
50		-o "match:QUOTA ENTRY NOT IN LIST \(FIXED\)" \
51		fsck_ffs -fp -F "${IMG}"
52	atf_check -s exit:0 -o "match:3 files" fsck_ffs -nf -F "${IMG}"
53}
54
55expand_list()
56{
57	local nuid=$1; shift
58	local expected_files=$((nuid + 2))
59	echo "/set uid=0 gid=0" > spec
60	echo ".		type=dir  mode=0755" >> spec
61	mkdir ${DIR}
62	for i in $(seq "${nuid}"); do
63		touch "${DIR}/f${i}"
64		echo "./f$i	type=file mode=0600 uid=$i gid=$i" >> spec
65	done
66
67	atf_check -o ignore -e ignore makefs -B "$1" -o "version=$2" \
68		-F spec -s 4000b "${IMG}" "${DIR}"
69	atf_check -o ignore -e ignore tunefs -q user -F "${IMG}"
70	atf_check -s exit:0 -o 'match:NO USER QUOTA INODE \(CREATED\)' \
71		-o 'match:USER QUOTA MISMATCH FOR ID 10: 0/0 SHOULD BE 0/1' \
72		fsck_ffs -p -F "${IMG}"
73	atf_check -s exit:0 -o "match:${expected_files} files" \
74		fsck_ffs -nf -F "${IMG}"
75}
76