xref: /freebsd/tools/test/stress2/misc/nlink5.sh (revision 4b0615e585d83d2a67b4501b5bf460582556925b)
125a1b1f3SPeter Holm#!/bin/sh
225a1b1f3SPeter Holm#
34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
425a1b1f3SPeter Holm#
525a1b1f3SPeter Holm# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
625a1b1f3SPeter Holm#
725a1b1f3SPeter Holm# Redistribution and use in source and binary forms, with or without
825a1b1f3SPeter Holm# modification, are permitted provided that the following conditions
925a1b1f3SPeter Holm# are met:
1025a1b1f3SPeter Holm# 1. Redistributions of source code must retain the above copyright
1125a1b1f3SPeter Holm#    notice, this list of conditions and the following disclaimer.
1225a1b1f3SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
1325a1b1f3SPeter Holm#    notice, this list of conditions and the following disclaimer in the
1425a1b1f3SPeter Holm#    documentation and/or other materials provided with the distribution.
1525a1b1f3SPeter Holm#
1625a1b1f3SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1725a1b1f3SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1825a1b1f3SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1925a1b1f3SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2025a1b1f3SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2125a1b1f3SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2225a1b1f3SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2325a1b1f3SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2425a1b1f3SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2525a1b1f3SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2625a1b1f3SPeter Holm# SUCH DAMAGE.
2725a1b1f3SPeter Holm#
2825a1b1f3SPeter Holm
2925a1b1f3SPeter Holm# https://reviews.freebsd.org/D35514
3025a1b1f3SPeter Holm# Rename dir a/b to c/d
3125a1b1f3SPeter Holm
3225a1b1f3SPeter Holm# Test scenario suggestion by kib
3325a1b1f3SPeter Holm
3425a1b1f3SPeter Holm. ../default.cfg
3525a1b1f3SPeter Holm
3649a83b94SPeter HolmUFS_LINK_MAX=`grep UFS_LINK_MAX /usr/include/ufs/ufs/dinode.h 2>/dev/null`
3749a83b94SPeter Holm[ -z "$UFS_LINK_MAX" ] && exit 0
38*4b0615e5SPeter HolmUFS_LINK_MAX=`echo $UFS_LINK_MAX | awk '{print $3}'`
3925a1b1f3SPeter Holmcat > /tmp/nlink5.c <<EOF
4025a1b1f3SPeter Holm#include <sys/stat.h>
4125a1b1f3SPeter Holm#include <ufs/ufs/dinode.h>
4225a1b1f3SPeter Holm#include <err.h>
4325a1b1f3SPeter Holm#include <errno.h>
4425a1b1f3SPeter Holm#include <stdio.h>
4525a1b1f3SPeter Holm#include <unistd.h>
4625a1b1f3SPeter Holm
4725a1b1f3SPeter Holmint
4825a1b1f3SPeter Holmmain (void) {
4925a1b1f3SPeter Holm	int i, mx;
5025a1b1f3SPeter Holm	char dir[100];
5125a1b1f3SPeter Holm
5249a83b94SPeter Holm	mx = UFS_LINK_MAX - 2;
5325a1b1f3SPeter Holm	for (i = 0; i < mx; i++) {
5425a1b1f3SPeter Holm		snprintf(dir, sizeof(dir), "%d", i);
5525a1b1f3SPeter Holm		if (mkdir(dir, 0700) == -1)
5625a1b1f3SPeter Holm			err(1, "mkdir(%s)", dir);
5725a1b1f3SPeter Holm	}
5825a1b1f3SPeter Holm
5925a1b1f3SPeter Holm	/* The following mkdir(2) must fail */
6025a1b1f3SPeter Holm	i = mx;
6125a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%d", i);
6225a1b1f3SPeter Holm	if (mkdir(dir, 0700) != -1)	/* this must fail */
6325a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
6425a1b1f3SPeter Holm	if (errno != EMLINK)
6525a1b1f3SPeter Holm		err(1, "Must fail: mkdir(%s)", dir);
6625a1b1f3SPeter Holm
6725a1b1f3SPeter Holm	/* Must succeed */
6825a1b1f3SPeter Holm	i = 0;
6925a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%d", i);
7025a1b1f3SPeter Holm	if (rmdir(dir) == -1)
7125a1b1f3SPeter Holm		err(1, "rmdir(%s)", dir);
7225a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%s", "x");
7325a1b1f3SPeter Holm	if (mkdir(dir, 0700) == -1)
7425a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
7525a1b1f3SPeter Holm
7625a1b1f3SPeter Holm	/* Make room for two top level directories */
7725a1b1f3SPeter Holm	if (rmdir("1") == -1)
7825a1b1f3SPeter Holm		err(1, "rmdir(1)");
7925a1b1f3SPeter Holm	if (rmdir("2") == -1)
8025a1b1f3SPeter Holm		err(1, "rmdir(2)");
8125a1b1f3SPeter Holm
8225a1b1f3SPeter Holm	/* mkdir a c */
8325a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%s", "a");
8425a1b1f3SPeter Holm	if (mkdir(dir, 0700) == -1)
8525a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
8625a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%s", "c");
8725a1b1f3SPeter Holm	if (mkdir(dir, 0700) == -1)
8825a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
8925a1b1f3SPeter Holm
9025a1b1f3SPeter Holm	/* mkdir a/b */
9125a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%s/%s", "a", "b");
9225a1b1f3SPeter Holm	if (mkdir(dir, 0700) == -1)
9325a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
9425a1b1f3SPeter Holm
9525a1b1f3SPeter Holm	/* mv a/b c/d */
9625a1b1f3SPeter Holm	if (rename("a/b", "c/d") == -1)
9725a1b1f3SPeter Holm		err(1, "rename(a/b, c/d)");
9825a1b1f3SPeter Holm
9925a1b1f3SPeter Holm	return (0);
10025a1b1f3SPeter Holm}
10125a1b1f3SPeter HolmEOF
10225a1b1f3SPeter Holmmycc -o /tmp/nlink5 -Wall -Wextra -O2 /tmp/nlink5.c || exit 1
10325a1b1f3SPeter Holmrm /tmp/nlink5.c
10425a1b1f3SPeter Holm
10525a1b1f3SPeter Holmset -e
10625a1b1f3SPeter Holmhere=`pwd`
10725a1b1f3SPeter Holmmount | grep -q "on $mntpoint " && umount -f $mntpoint
10825a1b1f3SPeter Holmmdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
10925a1b1f3SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart
11025a1b1f3SPeter Holmnewfs -Un /dev/md$mdstart > /dev/null
11125a1b1f3SPeter Holmmount /dev/md$mdstart $mntpoint
11225a1b1f3SPeter Holmset +e
11325a1b1f3SPeter Holm
11425a1b1f3SPeter Holmcd $mntpoint
11525a1b1f3SPeter Holm/tmp/nlink5; s=$?
11625a1b1f3SPeter Holmn=`find . -type d -maxdepth 1 | wc -l`
11749a83b94SPeter Holm[ $n -ne $((UFS_LINK_MAX - 1)) ] && s=2
11825a1b1f3SPeter Holmcd $here
11925a1b1f3SPeter Holm
12025a1b1f3SPeter Holmumount $mntpoint
12125a1b1f3SPeter Holmmdconfig -d -u $mdstart
12225a1b1f3SPeter Holmrm /tmp/nlink5
12325a1b1f3SPeter Holmexit $s
124