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 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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2017 by Fan Yong. All rights reserved.
30#
31
32. $STF_SUITE/tests/functional/projectquota/projectquota_common.kshlib
33
34#
35# DESCRIPTION:
36#	Check the project used object accounting in zfs projectspace
37#
38#
39# STRATEGY:
40#	1. create a bunch of files by specific project
41#	2. use zfs projectspace to check the used objects
42#	3. change the project ID of test files and verify object count
43#	4. delete files and verify object count
44#
45
46function cleanup
47{
48	datasetexists $snapfs && destroy_dataset $snapfs
49
50	log_must cleanup_projectquota
51}
52
53if ! lsattr -pd > /dev/null 2>&1; then
54	log_unsupported "Current e2fsprogs does not support set/show project ID"
55fi
56
57log_onexit cleanup
58
59log_assert "Check the zfs projectspace object used"
60
61mkmount_writable $QFS
62log_must zfs set xattr=sa $QFS
63log_must user_run $PUSER mkdir $PRJDIR1
64log_must user_run $PUSER mkdir $PRJDIR2
65log_must chattr +P -p $PRJID1 $PRJDIR1
66log_must chattr +P -p $PRJID2 $PRJDIR2
67
68((prj_cnt1 = RANDOM % 100 + 2))
69((prj_cnt2 = RANDOM % 100 + 2))
70
71log_must user_run $PUSER mkfiles $PRJDIR1/qf $((prj_cnt1 - 1))
72log_must user_run $PUSER mkfiles $PRJDIR2/qf $((prj_cnt2 - 1))
73sync_pool
74
75typeset snapfs=$QFS@snap
76
77log_must zfs snapshot $snapfs
78
79log_must eval "zfs projectspace $QFS >/dev/null 2>&1"
80log_must eval "zfs projectspace $snapfs >/dev/null 2>&1"
81
82for fs in "$QFS" "$snapfs"; do
83	log_note "check the project used objects in zfs projectspace $fs"
84	prjused=$(project_obj_count $fs $PRJID1)
85	[[ $prjused -eq $prj_cnt1 ]] ||
86		log_fail "($PRJID1) expected $prj_cnt1, got $prjused"
87	prjused=$(project_obj_count $fs $PRJID2)
88	[[ $prjused -eq $prj_cnt2 ]] ||
89		log_fail "($PRJID2) expected $prj_cnt2, got $prjused"
90done
91
92log_note "change the project of files"
93log_must chattr -p $PRJID2 $PRJDIR1/qf*
94sync_pool
95
96prjused=$(project_obj_count $QFS $PRJID1)
97[[ $prjused -eq 1 ]] ||
98	log_fail "expected 1 for project $PRJID1, got $prjused"
99
100prjused=$(project_obj_count $snapfs $PRJID1)
101[[ $prjused -eq $prj_cnt1 ]] ||
102	log_fail "expected $prj_cnt1 for $PRJID1 in snapfs, got $prjused"
103
104prjused=$(project_obj_count $QFS $PRJID2)
105[[ $prjused -eq $((prj_cnt1 + prj_cnt2 - 1)) ]] ||
106	log_fail "($PRJID2) expected $((prj_cnt1 + prj_cnt2 - 1)), got $prjused"
107
108log_note "file removal"
109log_must rm -rf $PRJDIR1
110sync_pool
111
112prjused=$(project_obj_count $QFS $PRJID1)
113[[ $prjused -eq 0 ]] || log_fail "expected 0 for $PRJID1, got $prjused"
114
115cleanup
116log_pass "Check the zfs projectspace object used"
117