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