xref: /freebsd/tools/test/stress2/misc/nlink3.sh (revision 25a1b1f30c85670b6d970df426b97c07d4e0e61d)
1*25a1b1f3SPeter Holm#!/bin/sh
2*25a1b1f3SPeter Holm
3*25a1b1f3SPeter Holm#
4*25a1b1f3SPeter Holm# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5*25a1b1f3SPeter Holm#
6*25a1b1f3SPeter Holm# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
7*25a1b1f3SPeter Holm#
8*25a1b1f3SPeter Holm# Redistribution and use in source and binary forms, with or without
9*25a1b1f3SPeter Holm# modification, are permitted provided that the following conditions
10*25a1b1f3SPeter Holm# are met:
11*25a1b1f3SPeter Holm# 1. Redistributions of source code must retain the above copyright
12*25a1b1f3SPeter Holm#    notice, this list of conditions and the following disclaimer.
13*25a1b1f3SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
14*25a1b1f3SPeter Holm#    notice, this list of conditions and the following disclaimer in the
15*25a1b1f3SPeter Holm#    documentation and/or other materials provided with the distribution.
16*25a1b1f3SPeter Holm#
17*25a1b1f3SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*25a1b1f3SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*25a1b1f3SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*25a1b1f3SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*25a1b1f3SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*25a1b1f3SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*25a1b1f3SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*25a1b1f3SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*25a1b1f3SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*25a1b1f3SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*25a1b1f3SPeter Holm# SUCH DAMAGE.
28*25a1b1f3SPeter Holm#
29*25a1b1f3SPeter Holm
30*25a1b1f3SPeter Holm# https://reviews.freebsd.org/D35514
31*25a1b1f3SPeter Holm# link() version
32*25a1b1f3SPeter Holm
33*25a1b1f3SPeter Holm. ../default.cfg
34*25a1b1f3SPeter Holm
35*25a1b1f3SPeter Holmcat > /tmp/nlink3.c <<EOF
36*25a1b1f3SPeter Holm#include <sys/stat.h>
37*25a1b1f3SPeter Holm#include <ufs/ufs/dinode.h>
38*25a1b1f3SPeter Holm#include <err.h>
39*25a1b1f3SPeter Holm#include <errno.h>
40*25a1b1f3SPeter Holm#include <fcntl.h>
41*25a1b1f3SPeter Holm#include <stdio.h>
42*25a1b1f3SPeter Holm#include <unistd.h>
43*25a1b1f3SPeter Holm
44*25a1b1f3SPeter Holmint
45*25a1b1f3SPeter Holmmain (void) {
46*25a1b1f3SPeter Holm	int fd, i, mx;
47*25a1b1f3SPeter Holm	char file[100];
48*25a1b1f3SPeter Holm
49*25a1b1f3SPeter Holm	snprintf(file, sizeof(file), "f");
50*25a1b1f3SPeter Holm	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
51*25a1b1f3SPeter Holm	    DEFFILEMODE)) == -1)
52*25a1b1f3SPeter Holm		err(1, "creat(%s)", file);
53*25a1b1f3SPeter Holm	close(fd);
54*25a1b1f3SPeter Holm
55*25a1b1f3SPeter Holm	mx = UFS_LINK_MAX - 1; /* UFS_LINK_MAX = 32767 */
56*25a1b1f3SPeter Holm	for (i = 0; i < mx; i++) {
57*25a1b1f3SPeter Holm		snprintf(file, sizeof(file), "%d", i);
58*25a1b1f3SPeter Holm		if (link("f", file) == -1)
59*25a1b1f3SPeter Holm			err(1, "link(%s, %s)", "f", file);
60*25a1b1f3SPeter Holm
61*25a1b1f3SPeter Holm	}
62*25a1b1f3SPeter Holm
63*25a1b1f3SPeter Holm	/* The following link(2) must fail */
64*25a1b1f3SPeter Holm	i = mx;
65*25a1b1f3SPeter Holm	snprintf(file, sizeof(file), "%d", i);
66*25a1b1f3SPeter Holm	if (link("f", file) != -1)
67*25a1b1f3SPeter Holm		err(1, "link(%s, %s)", "f", file);
68*25a1b1f3SPeter Holm	if (errno != EMLINK)
69*25a1b1f3SPeter Holm		err(1, "Must fail: link(%s, %s)", "f", file);
70*25a1b1f3SPeter Holm
71*25a1b1f3SPeter Holm	i = 0;
72*25a1b1f3SPeter Holm	snprintf(file, sizeof(file), "%d", i);
73*25a1b1f3SPeter Holm	if (unlink(file) == -1)
74*25a1b1f3SPeter Holm		err(1, "unlink(%s)", file);
75*25a1b1f3SPeter Holm
76*25a1b1f3SPeter Holm	/* Must succeed */
77*25a1b1f3SPeter Holm	i = mx;
78*25a1b1f3SPeter Holm	snprintf(file, sizeof(file), "%d", i);
79*25a1b1f3SPeter Holm	if (link("f", file) == -1)
80*25a1b1f3SPeter Holm		err(1, "link(%s, %s)", "f", file);
81*25a1b1f3SPeter Holm
82*25a1b1f3SPeter Holm	return (0);
83*25a1b1f3SPeter Holm}
84*25a1b1f3SPeter HolmEOF
85*25a1b1f3SPeter Holmmycc -o /tmp/nlink3 -Wall -Wextra -O2 /tmp/nlink3.c || exit 1
86*25a1b1f3SPeter Holmrm /tmp/nlink3.c
87*25a1b1f3SPeter Holm
88*25a1b1f3SPeter Holmset -e
89*25a1b1f3SPeter Holmhere=`pwd`
90*25a1b1f3SPeter Holmmount | grep -q "on $mntpoint " && umount -f $mntpoint
91*25a1b1f3SPeter Holmmdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
92*25a1b1f3SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart
93*25a1b1f3SPeter Holmnewfs -Un /dev/md$mdstart > /dev/null
94*25a1b1f3SPeter Holmmount /dev/md$mdstart $mntpoint
95*25a1b1f3SPeter Holmset +e
96*25a1b1f3SPeter Holm
97*25a1b1f3SPeter Holmcd $mntpoint
98*25a1b1f3SPeter Holm/tmp/nlink3; s=$?
99*25a1b1f3SPeter Holmn=`ls -a | wc -l`
100*25a1b1f3SPeter Holm[ $s -ne 0 ] && echo "$n files"
101*25a1b1f3SPeter Holmcd $here
102*25a1b1f3SPeter Holm
103*25a1b1f3SPeter Holmumount $mntpoint
104*25a1b1f3SPeter Holmmdconfig -d -u $mdstart
105*25a1b1f3SPeter Holmrm /tmp/nlink3
106*25a1b1f3SPeter Holmexit $s
107