xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/userquota/userspace_003_pos.ksh (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1#!/bin/ksh -p
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 https://opensource.org/licenses/CDDL-1.0.
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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Jinshan Xiong. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/include/math.shlib
34. $STF_SUITE/tests/functional/userquota/userquota_common.kshlib
35
36#
37# DESCRIPTION:
38#       Check the user used object accounting in zfs userspace
39#
40#
41# STRATEGY:
42#       1. create a bunch of files by specific users
43#	2. use zfs userspace to check the used objects
44#	3. change the owner of test files and verify object count
45#	4. delete files and verify object count
46#
47
48function cleanup
49{
50	datasetexists $snapfs && destroy_dataset $snapfs
51
52	log_must rm -f ${QFILE}_*
53	log_must cleanup_quota
54}
55
56function user_object_count
57{
58	typeset fs=$1
59	typeset user=$2
60	typeset -i userspacecnt=$(zfs userspace -oname,objused $fs |
61	    awk /$user/'{print $2}')
62	typeset -i zfsgetcnt=$(zfs get -H -ovalue userobjused@$user $fs)
63
64	# 'zfs userspace' and 'zfs get userobjused@' should be equal
65	verify_eq "$userspacecnt" "$zfsgetcnt" "userobjused@$user"
66
67	echo $userspacecnt
68}
69
70log_onexit cleanup
71
72log_assert "Check the zfs userspace object used"
73
74mkmount_writable $QFS
75log_must zfs set xattr=sa $QFS
76
77((user1_cnt = RANDOM % 100 + 1))
78((user2_cnt = RANDOM % 100 + 1))
79
80log_must user_run $QUSER1 mkfiles ${QFILE}_1 $user1_cnt
81log_must user_run $QUSER2 mkfiles ${QFILE}_2 $user2_cnt
82sync_all_pools
83
84typeset snapfs=$QFS@snap
85
86log_must zfs snapshot $snapfs
87
88log_must eval "zfs userspace $QFS >/dev/null 2>&1"
89log_must eval "zfs userspace $snapfs >/dev/null 2>&1"
90
91for fs in "$QFS" "$snapfs"; do
92	log_note "check the user used objects in zfs userspace $fs"
93	[[ $(user_object_count $fs $QUSER1) -eq $user1_cnt ]] ||
94		log_fail "expected $user1_cnt"
95	[[ $(user_object_count $fs $QUSER2) -eq $user2_cnt ]] ||
96		log_fail "expected $user2_cnt"
97done
98
99log_note "change the owner of files"
100log_must chown $QUSER2 ${QFILE}_1*
101sync_pool
102
103[[ $(user_object_count $QFS $QUSER1) -eq 0 ]] ||
104	log_fail "expected 0 files for $QUSER1"
105
106[[ $(user_object_count $snapfs $QUSER1) -eq $user1_cnt ]] ||
107	log_fail "expected $user_cnt files for $QUSER1 in snapfs"
108
109[[ $(user_object_count $QFS $QUSER2) -eq $((user1_cnt+user2_cnt)) ]] ||
110	log_fail "expected $((user1_cnt+user2_cnt)) files for $QUSER2"
111
112log_note "file removal"
113log_must rm ${QFILE}_*
114sync_pool
115
116[[ $(user_object_count $QFS $QUSER2) -eq 0 ]] ||
117        log_fail "expected 0 files for $QUSER2"
118
119cleanup
120log_pass "Check the zfs userspace object used"
121