xref: /freebsd/tools/test/stress2/misc/nlink4.sh (revision 49a83b94395a6eaae4642aa72c9f6a40143f0f45)
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(2) version
3125a1b1f3SPeter Holm
3225a1b1f3SPeter Holm. ../default.cfg
3325a1b1f3SPeter Holm
3425a1b1f3SPeter Holmcat > /tmp/nlink4.c <<EOF
3525a1b1f3SPeter Holm#include <sys/stat.h>
3625a1b1f3SPeter Holm#include <ufs/ufs/dinode.h>
3725a1b1f3SPeter Holm#include <err.h>
3825a1b1f3SPeter Holm#include <errno.h>
3925a1b1f3SPeter Holm#include <fcntl.h>
4025a1b1f3SPeter Holm#include <stdio.h>
4125a1b1f3SPeter Holm#include <unistd.h>
4225a1b1f3SPeter Holm
4325a1b1f3SPeter Holmint
4425a1b1f3SPeter Holmmain (void) {
4525a1b1f3SPeter Holm	int fd, i, mx;
4625a1b1f3SPeter Holm	char file[100];
4725a1b1f3SPeter Holm
4825a1b1f3SPeter Holm	snprintf(file, sizeof(file), "f");
4925a1b1f3SPeter Holm	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
5025a1b1f3SPeter Holm	    DEFFILEMODE)) == -1)
5125a1b1f3SPeter Holm		err(1, "creat(%s)", file);
5225a1b1f3SPeter Holm	close(fd);
5325a1b1f3SPeter Holm
54*49a83b94SPeter Holm	mx = UFS_LINK_MAX - 1;
5525a1b1f3SPeter Holm	for (i = 0; i < mx; i++) {
5625a1b1f3SPeter Holm		snprintf(file, sizeof(file), "%d", i);
5725a1b1f3SPeter Holm		if (link("f", file) == -1)
5825a1b1f3SPeter Holm			err(1, "link(%s, %s)", "f", file);
5925a1b1f3SPeter Holm
6025a1b1f3SPeter Holm	}
6125a1b1f3SPeter Holm
6225a1b1f3SPeter Holm	/* The following link(2) must fail */
6325a1b1f3SPeter Holm	i = mx;
6425a1b1f3SPeter Holm	snprintf(file, sizeof(file), "%d", i);
6525a1b1f3SPeter Holm	if (link("f", file) != -1)
6625a1b1f3SPeter Holm		err(1, "link(%s, %s)", "f", file);
6725a1b1f3SPeter Holm	if (errno != EMLINK)
6825a1b1f3SPeter Holm		err(1, "Must fail: link(%s, %s)", "f", file);
6925a1b1f3SPeter Holm
7025a1b1f3SPeter Holm	/* Must succeed */
7125a1b1f3SPeter Holm	i = 0;
7225a1b1f3SPeter Holm	snprintf(file, sizeof(file), "%d", i);
7325a1b1f3SPeter Holm	if (unlink(file) == -1)
7425a1b1f3SPeter Holm		err(1, "unlink(%s)", file);
7525a1b1f3SPeter Holm	if (rename("1", "a") == -1) {
7625a1b1f3SPeter Holm		if (errno == EMLINK)
7725a1b1f3SPeter Holm			fprintf(stderr, "Unexpected EMLINK\n");
7825a1b1f3SPeter Holm		err(1, "rename(%s, %s)", "1", "a");
7925a1b1f3SPeter Holm	}
8025a1b1f3SPeter Holm
8125a1b1f3SPeter Holm	return (0);
8225a1b1f3SPeter Holm}
8325a1b1f3SPeter HolmEOF
8425a1b1f3SPeter Holmmycc -o /tmp/nlink4 -Wall -Wextra -O2 /tmp/nlink4.c || exit 1
8525a1b1f3SPeter Holmrm /tmp/nlink4.c
8625a1b1f3SPeter Holm
8725a1b1f3SPeter Holmset -e
8825a1b1f3SPeter Holmhere=`pwd`
8925a1b1f3SPeter Holmmount | grep -q "on $mntpoint " && umount -f $mntpoint
9025a1b1f3SPeter Holmmdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
9125a1b1f3SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart
9225a1b1f3SPeter Holmnewfs -Un /dev/md$mdstart > /dev/null
9325a1b1f3SPeter Holmmount /dev/md$mdstart $mntpoint
9425a1b1f3SPeter Holmset +e
9525a1b1f3SPeter Holm
9625a1b1f3SPeter Holmcd $mntpoint
9725a1b1f3SPeter Holm/tmp/nlink4; s=$?
9825a1b1f3SPeter Holmn=`ls -a | wc -l`
9925a1b1f3SPeter Holm[ $s -ne 0 ] && echo "$n files"
10025a1b1f3SPeter Holmcd $here
10125a1b1f3SPeter Holm
10225a1b1f3SPeter Holmumount $mntpoint
10325a1b1f3SPeter Holmmdconfig -d -u $mdstart
10425a1b1f3SPeter Holmrm /tmp/nlink4
10525a1b1f3SPeter Holmexit $s
106