1#!/bin/ksh
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2019 by Tomohiro Kusumi. All rights reserved.
16#
17
18. $STF_SUITE/include/libtest.shlib
19
20#
21# DESCRIPTION:
22# Verify root inode (directory) has correct link count.
23#
24# STRATEGY:
25# 1. Create pool and fs.
26# 2. Test link count of root inode.
27# 3. Create directories and test link count of root inode.
28# 4. Delete directories and test link count of root inode.
29# 5. Create regular file and test link count of root inode.
30# 6. Delete regular file and test link count of root inode.
31#
32
33function assert_link_count
34{
35	typeset dirpath="$1"
36	typeset value="$2"
37
38	log_must test "$(ls -ld $dirpath | awk '{ print $2 }')" == "$value"
39}
40
41verify_runnable "both"
42
43log_note "Verify root inode (directory) has correct link count."
44
45# Delete a directory from link_count_001.ksh.
46if [ -d "${TESTDIR}" -a -d "${TESTDIR}/tmp" ]; then
47	log_must rm -rf ${TESTDIR}/tmp
48fi
49
50#
51# Test with hidden '.zfs' directory.
52# This also tests general directories.
53#
54log_note "Testing with snapdir set to hidden (default)"
55
56for dst in $TESTPOOL $TESTPOOL/$TESTFS
57do
58	typeset mtpt=$(get_prop mountpoint $dst)
59	log_must zfs set snapdir=hidden $dst
60	log_must test -d "$mtpt/.zfs"
61	if test -n "$(ls $mtpt)"; then
62		ls $mtpt
63		log_note "$mtpt not empty, skipping"
64		continue
65	fi
66	assert_link_count $mtpt 2
67
68	log_must mkdir $mtpt/a
69	assert_link_count $mtpt 3
70	log_must rmdir $mtpt/a
71	assert_link_count $mtpt 2
72
73	log_must mkdir -p $mtpt/a/b
74	assert_link_count $mtpt 3
75	log_must rmdir $mtpt/a/b
76	log_must rmdir $mtpt/a
77	assert_link_count $mtpt 2
78
79	log_must touch $mtpt/a
80	assert_link_count $mtpt 2
81	log_must rm $mtpt/a
82	assert_link_count $mtpt 2
83done
84
85#
86# Test with visible '.zfs' directory.
87#
88log_note "Testing with snapdir set to visible"
89
90for dst in $TESTPOOL $TESTPOOL/$TESTFS
91do
92	typeset mtpt=$(get_prop mountpoint $dst)
93	log_must zfs set snapdir=visible $dst
94	log_must test -d "$mtpt/.zfs"
95	if test -n "$(ls $mtpt)"; then
96		ls $mtpt
97		log_note "$mtpt not empty, skipping"
98		continue
99	fi
100	assert_link_count $mtpt 3
101
102	log_must mkdir $mtpt/a
103	assert_link_count $mtpt 4
104	log_must rmdir $mtpt/a
105	assert_link_count $mtpt 3
106
107	log_must mkdir -p $mtpt/a/b
108	assert_link_count $mtpt 4
109	log_must rmdir $mtpt/a/b
110	log_must rmdir $mtpt/a
111	assert_link_count $mtpt 3
112
113	log_must touch $mtpt/a
114	assert_link_count $mtpt 3
115	log_must rm $mtpt/a
116	assert_link_count $mtpt 3
117done
118
119log_pass "Verify root inode (directory) has correct link count passed"
120