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 (c) 2017 by Fan Yong. All rights reserved.
25#
26
27. $STF_SUITE/tests/functional/upgrade/upgrade_common.kshlib
28
29#
30# DESCRIPTION:
31#
32# Check whether zfs upgrade for project quota works or not.
33# The project quota is per dataset based feature, this test
34# will create multiple datasets and try different upgrade methods.
35#
36# STRATEGY:
37# 1. Create a pool with all features disabled
38# 2. Create a few dataset for testing
39# 3. Make sure automatic upgrade work
40# 4. Make sure manual upgrade work
41#
42
43verify_runnable "global"
44
45if ! lsattr -pd > /dev/null 2>&1; then
46	log_unsupported "Current lsattr does not support set/show project ID"
47fi
48
49log_assert "pool upgrade for projectquota should work"
50log_onexit cleanup_upgrade
51
52log_must zpool create -d -m $TESTDIR $TESTPOOL $TMPDEV
53
54log_must mkfiles $TESTDIR/tf $((RANDOM % 100 + 1))
55log_must zfs create $TESTPOOL/fs1
56log_must mkfiles $TESTDIR/fs1/tf $((RANDOM % 100 + 1))
57log_must zfs umount $TESTPOOL/fs1
58
59log_must zfs create $TESTPOOL/fs2
60log_must mkdir $TESTDIR/fs2/dir
61log_must mkfiles $TESTDIR/fs2/tf $((RANDOM % 100 + 1))
62
63log_must zfs create $TESTPOOL/fs3
64log_must mkdir $TESTDIR/fs3/dir
65log_must mkfiles $TESTDIR/fs3/tf $((RANDOM % 100 + 1))
66
67# Make sure project quota is disabled
68zfs projectspace -o used $TESTPOOL | grep -q "USED" &&
69	log_fail "project quota should be disabled initially"
70
71# set projectquota before upgrade will fail
72log_mustnot zfs set projectquota@100=100m $TESTDIR/fs3
73
74# set projectobjquota before upgrade will fail
75log_mustnot zfs set projectobjquota@100=1000 $TESTDIR/fs3
76
77# 'chattr -p' should fail before upgrade
78log_mustnot chattr -p 100 $TESTDIR/fs3/dir
79
80# 'chattr +P' should fail before upgrade
81log_mustnot chattr +P $TESTDIR/fs3/dir
82
83# Upgrade zpool to support all features
84log_must zpool upgrade $TESTPOOL
85
86# Double check project quota is disabled
87zfs projectspace -o used $TESTPOOL | grep -q "USED" &&
88	log_fail "project quota should be disabled after pool upgrade"
89
90# Mount dataset should trigger upgrade
91log_must zfs mount $TESTPOOL/fs1
92log_must sleep 3 # upgrade done in the background so let's wait for a while
93zfs projectspace -o used $TESTPOOL/fs1 | grep -q "USED" ||
94	log_fail "project quota should be enabled for $TESTPOOL/fs1"
95
96# Create file should trigger dataset upgrade
97log_must mkfile 1m $TESTDIR/fs2/dir/tf
98log_must sleep 3 # upgrade done in the background so let's wait for a while
99zfs projectspace -o used $TESTPOOL/fs2 | grep -q "USED" ||
100	log_fail "project quota should be enabled for $TESTPOOL/fs2"
101
102# "lsattr -p" should NOT trigger upgrade
103log_must lsattr -p -d $TESTDIR/fs3/dir
104zfs projectspace -o used $TESTPOOL/fs3 | grep -q "USED" &&
105	log_fail "project quota should not active for $TESTPOOL/fs3"
106
107# 'chattr -p' should trigger dataset upgrade
108log_must chattr -p 100 $TESTDIR/fs3/dir
109log_must sleep 5 # upgrade done in the background so let's wait for a while
110zfs projectspace -o used $TESTPOOL/fs3 | grep -q "USED" ||
111	log_fail "project quota should be enabled for $TESTPOOL/fs3"
112cnt=$(zfs get -H projectobjused@100 $TESTPOOL/fs3 | awk '{print $3}')
113# if 'xattr=on', then 'cnt = 2'
114[[ $cnt -ne 1 ]] && [[ $cnt -ne 2 ]] &&
115	log_fail "projectquota accounting failed $cnt"
116
117# All in all, after having been through this, the dataset for testpool
118# still shouldn't be upgraded
119zfs projectspace -o used $TESTPOOL | grep -q "USED" &&
120	log_fail "project quota should be disabled for $TESTPOOL"
121
122# Manual upgrade root dataset
123# uses an ioctl which will wait for the upgrade to be done before returning
124log_must zfs set version=current $TESTPOOL
125zfs projectspace -o used $TESTPOOL | grep -q "USED" ||
126	log_fail "project quota should be enabled for $TESTPOOL"
127
128log_pass "Project Quota upgrade done"
129