xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/userquota/userspace_003_pos.ksh (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# CDDL HEADER START
5#
6# The contents of this file are subject to the terms of the
7# Common Development and Distribution License (the "License").
8# You may not use this file except in compliance with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or https://opensource.org/licenses/CDDL-1.0.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29#
30# Copyright (c) 2016 by Jinshan Xiong. All rights reserved.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/include/math.shlib
35. $STF_SUITE/tests/functional/userquota/userquota_common.kshlib
36
37#
38# DESCRIPTION:
39#       Check the user used object accounting in zfs userspace
40#
41#
42# STRATEGY:
43#       1. create a bunch of files by specific users
44#	2. use zfs userspace to check the used objects
45#	3. change the owner of test files and verify object count
46#	4. delete files and verify object count
47#
48
49function cleanup
50{
51	datasetexists $snapfs && destroy_dataset $snapfs
52
53	log_must rm -f ${QFILE}_*
54	log_must cleanup_quota
55}
56
57function user_object_count
58{
59	typeset fs=$1
60	typeset user=$2
61	typeset -i userspacecnt=$(zfs userspace -oname,objused $fs |
62	    awk /$user/'{print $2}')
63	typeset -i zfsgetcnt=$(zfs get -H -ovalue userobjused@$user $fs)
64
65	# 'zfs userspace' and 'zfs get userobjused@' should be equal
66	verify_eq "$userspacecnt" "$zfsgetcnt" "userobjused@$user"
67
68	echo $userspacecnt
69}
70
71log_onexit cleanup
72
73log_assert "Check the zfs userspace object used"
74
75mkmount_writable $QFS
76log_must zfs set xattr=sa $QFS
77
78((user1_cnt = RANDOM % 100 + 1))
79((user2_cnt = RANDOM % 100 + 1))
80
81log_must user_run $QUSER1 mkfiles ${QFILE}_1 $user1_cnt
82log_must user_run $QUSER2 mkfiles ${QFILE}_2 $user2_cnt
83sync_all_pools
84
85typeset snapfs=$QFS@snap
86
87log_must zfs snapshot $snapfs
88
89log_must eval "zfs userspace $QFS >/dev/null 2>&1"
90log_must eval "zfs userspace $snapfs >/dev/null 2>&1"
91
92for fs in "$QFS" "$snapfs"; do
93	log_note "check the user used objects in zfs userspace $fs"
94	[[ $(user_object_count $fs $QUSER1) -eq $user1_cnt ]] ||
95		log_fail "expected $user1_cnt"
96	[[ $(user_object_count $fs $QUSER2) -eq $user2_cnt ]] ||
97		log_fail "expected $user2_cnt"
98done
99
100log_note "change the owner of files"
101log_must chown $QUSER2 ${QFILE}_1*
102sync_pool
103
104[[ $(user_object_count $QFS $QUSER1) -eq 0 ]] ||
105	log_fail "expected 0 files for $QUSER1"
106
107[[ $(user_object_count $snapfs $QUSER1) -eq $user1_cnt ]] ||
108	log_fail "expected $user_cnt files for $QUSER1 in snapfs"
109
110[[ $(user_object_count $QFS $QUSER2) -eq $((user1_cnt+user2_cnt)) ]] ||
111	log_fail "expected $((user1_cnt+user2_cnt)) files for $QUSER2"
112
113log_note "file removal"
114log_must rm ${QFILE}_*
115sync_pool
116
117[[ $(user_object_count $QFS $QUSER2) -eq 0 ]] ||
118        log_fail "expected 0 files for $QUSER2"
119
120cleanup
121log_pass "Check the zfs userspace object used"
122