xref: /freebsd/tools/test/stress2/misc/nlink.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
125a1b1f3SPeter Holm#!/bin/sh
225a1b1f3SPeter Holm#
3*4d846d26SWarner 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# Regression test for:
3025a1b1f3SPeter Holm# D35514 UFS: make mkdir() reliable when using SU and reaching nlink limit
3125a1b1f3SPeter Holm# Bug 165392 - [ufs] [patch] Multiple mkdir/rmdir fails with errno 31
3225a1b1f3SPeter Holm
3325a1b1f3SPeter Holm. ../default.cfg
3425a1b1f3SPeter Holm
3525a1b1f3SPeter Holmcat > /tmp/nlink.c <<EOF
3625a1b1f3SPeter Holm#include <sys/stat.h>
3725a1b1f3SPeter Holm#include <ufs/ufs/dinode.h>
3825a1b1f3SPeter Holm#include <err.h>
3925a1b1f3SPeter Holm#include <errno.h>
4025a1b1f3SPeter Holm#include <stdio.h>
4125a1b1f3SPeter Holm#include <unistd.h>
4225a1b1f3SPeter Holm
4325a1b1f3SPeter Holmint
4425a1b1f3SPeter Holmmain (void) {
4525a1b1f3SPeter Holm	int i, mx;
4625a1b1f3SPeter Holm	char dir[100];
4725a1b1f3SPeter Holm
4825a1b1f3SPeter Holm	mx = UFS_LINK_MAX - 2;
4925a1b1f3SPeter Holm	for (i = 0; i < mx; i++) {
5025a1b1f3SPeter Holm		snprintf(dir, sizeof(dir), "%d", i);
5125a1b1f3SPeter Holm		if (mkdir(dir, 0700) == -1)
5225a1b1f3SPeter Holm			err(1, "mkdir(%s)", dir);
5325a1b1f3SPeter Holm	}
5425a1b1f3SPeter Holm
5525a1b1f3SPeter Holm	/* The following mkdir(2) must fail */
5625a1b1f3SPeter Holm	i = mx;
5725a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%d", i);
5825a1b1f3SPeter Holm	if (mkdir(dir, 0700) != -1)	/* this must fail */
5925a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
6025a1b1f3SPeter Holm	if (errno != EMLINK)
6125a1b1f3SPeter Holm		err(1, "Must fail: mkdir(%s)", dir);
6225a1b1f3SPeter Holm
6325a1b1f3SPeter Holm	/* Must succeed */
6425a1b1f3SPeter Holm	i = 0;
6525a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%d", i);
6625a1b1f3SPeter Holm	if (rmdir(dir) == -1)
6725a1b1f3SPeter Holm		err(1, "rmdir(%s)", dir);
6825a1b1f3SPeter Holm	snprintf(dir, sizeof(dir), "%s", "a");
6925a1b1f3SPeter Holm	if (mkdir(dir, 0700) == -1)
7025a1b1f3SPeter Holm		err(1, "mkdir(%s)", dir);
7125a1b1f3SPeter Holm
7225a1b1f3SPeter Holm	return (0);
7325a1b1f3SPeter Holm}
7425a1b1f3SPeter HolmEOF
7525a1b1f3SPeter Holmmycc -o /tmp/nlink -Wall -Wextra -O2 /tmp/nlink.c || exit 1
7625a1b1f3SPeter Holmrm /tmp/nlink.c
7725a1b1f3SPeter Holm
7825a1b1f3SPeter Holmset -e
7925a1b1f3SPeter Holmhere=`pwd`
8025a1b1f3SPeter Holmmount | grep -q "on $mntpoint " && umount -f $mntpoint
8125a1b1f3SPeter Holmmdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
8225a1b1f3SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart
8325a1b1f3SPeter Holmnewfs -Un /dev/md$mdstart > /dev/null
8425a1b1f3SPeter Holmmount /dev/md$mdstart $mntpoint
8525a1b1f3SPeter Holmset +e
8625a1b1f3SPeter Holm
8725a1b1f3SPeter Holmcd $mntpoint
8825a1b1f3SPeter Holm/tmp/nlink; s=$?
8925a1b1f3SPeter Holmn=`ls -a | wc -l`
9025a1b1f3SPeter Holm[ $s -ne 0 ] && echo "$n dirs"
9125a1b1f3SPeter Holmcd $here
9225a1b1f3SPeter Holm
9325a1b1f3SPeter Holmumount $mntpoint
9425a1b1f3SPeter Holmmdconfig -d -u $mdstart
9525a1b1f3SPeter Holmrm /tmp/nlink
9625a1b1f3SPeter Holmexit $s
97