1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd.
3f08c3bdfSopenharmony_ci * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
6f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
7f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
10f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
14f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
15f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <string.h>
19f08c3bdfSopenharmony_ci#include <errno.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include <sys/stat.h>
22f08c3bdfSopenharmony_ci#include <unistd.h>
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "test.h"
25f08c3bdfSopenharmony_ci#include "usctest.h"
26f08c3bdfSopenharmony_ci#include "safe_macros.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#define MAX_SANE_HARD_LINKS	65535
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci/*
31f08c3bdfSopenharmony_ci * filesystems whose subdir limit is less than MAX_SANE_HARD_LINKS
32f08c3bdfSopenharmony_ci * XXX: we cannot filter ext4 out, because ext2/ext3/ext4 have the
33f08c3bdfSopenharmony_ci * same magic number
34f08c3bdfSopenharmony_ci */
35f08c3bdfSopenharmony_ciconst long subdir_limit_whitelist[] = {
36f08c3bdfSopenharmony_ci	TST_EXT2_OLD_MAGIC, TST_EXT234_MAGIC, TST_MINIX_MAGIC,
37f08c3bdfSopenharmony_ci	TST_MINIX_MAGIC2,   TST_MINIX2_MAGIC, TST_MINIX2_MAGIC2,
38f08c3bdfSopenharmony_ci	TST_MINIX3_MAGIC,   TST_UDF_MAGIC,    TST_SYSV2_MAGIC,
39f08c3bdfSopenharmony_ci	TST_SYSV4_MAGIC,    TST_UFS_MAGIC,    TST_UFS2_MAGIC,
40f08c3bdfSopenharmony_ci	TST_F2FS_MAGIC,     TST_NILFS_MAGIC,  TST_EXOFS_MAGIC
41f08c3bdfSopenharmony_ci};
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ciint tst_fs_fill_hardlinks_(void (*cleanup) (void), const char *dir)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	unsigned int i, j;
46f08c3bdfSopenharmony_ci	char base_filename[PATH_MAX], link_filename[PATH_MAX];
47f08c3bdfSopenharmony_ci	struct stat s;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if (stat(dir, &s) == -1 && errno == ENOENT)
50f08c3bdfSopenharmony_ci		SAFE_MKDIR(cleanup, dir, 0744);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_STAT(cleanup, dir, &s);
53f08c3bdfSopenharmony_ci	if (!S_ISDIR(s.st_mode)) {
54f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "%s is not directory", dir);
55f08c3bdfSopenharmony_ci		return 0;
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	sprintf(base_filename, "%s/testfile0", dir);
59f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, base_filename, 0644, NULL);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	for (i = 1; i < MAX_SANE_HARD_LINKS; i++) {
62f08c3bdfSopenharmony_ci		sprintf(link_filename, "%s/testfile%d", dir, i);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci		if (link(base_filename, link_filename) == 0)
65f08c3bdfSopenharmony_ci			continue;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci		switch (errno) {
68f08c3bdfSopenharmony_ci		case EMLINK:
69f08c3bdfSopenharmony_ci			SAFE_STAT(cleanup, base_filename, &s);
70f08c3bdfSopenharmony_ci			if (s.st_nlink != i) {
71f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "wrong number of "
72f08c3bdfSopenharmony_ci					 "hard links for %s have %i, should be"
73f08c3bdfSopenharmony_ci					 " %d", base_filename,
74f08c3bdfSopenharmony_ci					 (int)s.st_nlink, i);
75f08c3bdfSopenharmony_ci				return 0;
76f08c3bdfSopenharmony_ci			} else {
77f08c3bdfSopenharmony_ci				tst_resm(TINFO, "the maximum number of hard "
78f08c3bdfSopenharmony_ci					 "links to %s is hit: %d",
79f08c3bdfSopenharmony_ci					 base_filename, (int)s.st_nlink);
80f08c3bdfSopenharmony_ci				return s.st_nlink;
81f08c3bdfSopenharmony_ci			}
82f08c3bdfSopenharmony_ci		case ENOSPC:
83f08c3bdfSopenharmony_ci		case EDQUOT:
84f08c3bdfSopenharmony_ci			tst_resm(TINFO | TERRNO, "link(%s, %s) failed",
85f08c3bdfSopenharmony_ci				 base_filename, link_filename);
86f08c3bdfSopenharmony_ci			goto max_hardlinks_cleanup;
87f08c3bdfSopenharmony_ci		default:
88f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "link(%s, %s) failed "
89f08c3bdfSopenharmony_ci				 "unexpectedly: %s", base_filename,
90f08c3bdfSopenharmony_ci				 link_filename, strerror(errno));
91f08c3bdfSopenharmony_ci			return 0;
92f08c3bdfSopenharmony_ci		}
93f08c3bdfSopenharmony_ci	}
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	tst_resm(TINFO, "Failed reach the hardlinks limit");
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_cimax_hardlinks_cleanup:
98f08c3bdfSopenharmony_ci	for (j = 0; j < i; j++) {
99f08c3bdfSopenharmony_ci		sprintf(link_filename, "%s/testfile%d", dir, j);
100f08c3bdfSopenharmony_ci		SAFE_UNLINK(cleanup, link_filename);
101f08c3bdfSopenharmony_ci	}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	return 0;
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ciint tst_fs_fill_subdirs_(void (*cleanup) (void), const char *dir)
107f08c3bdfSopenharmony_ci{
108f08c3bdfSopenharmony_ci	unsigned int i, j, whitelist_size;
109f08c3bdfSopenharmony_ci	char dirname[PATH_MAX];
110f08c3bdfSopenharmony_ci	struct stat s;
111f08c3bdfSopenharmony_ci	long fs_type;
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	if (stat(dir, &s) == -1 && errno == ENOENT)
114f08c3bdfSopenharmony_ci		SAFE_MKDIR(cleanup, dir, 0744);
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	SAFE_STAT(cleanup, dir, &s);
117f08c3bdfSopenharmony_ci	if (!S_ISDIR(s.st_mode)) {
118f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "%s is not directory", dir);
119f08c3bdfSopenharmony_ci		return 0;
120f08c3bdfSopenharmony_ci	}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	/* for current kernel, subdir limit is not available for all fs */
123f08c3bdfSopenharmony_ci	fs_type = tst_fs_type(cleanup, dir);
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	whitelist_size = ARRAY_SIZE(subdir_limit_whitelist);
126f08c3bdfSopenharmony_ci	for (i = 0; i < whitelist_size; i++) {
127f08c3bdfSopenharmony_ci		if (fs_type == subdir_limit_whitelist[i])
128f08c3bdfSopenharmony_ci			break;
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci	if (i == whitelist_size) {
131f08c3bdfSopenharmony_ci		tst_resm(TINFO, "subdir limit is not availiable for "
132f08c3bdfSopenharmony_ci			 "%s filesystem", tst_fs_type_name(fs_type));
133f08c3bdfSopenharmony_ci		return 0;
134f08c3bdfSopenharmony_ci	}
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci	for (i = 0; i < MAX_SANE_HARD_LINKS; i++) {
137f08c3bdfSopenharmony_ci		sprintf(dirname, "%s/testdir%d", dir, i);
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci		if (mkdir(dirname, 0755) == 0)
140f08c3bdfSopenharmony_ci			continue;
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ci		switch (errno) {
143f08c3bdfSopenharmony_ci		case EMLINK:
144f08c3bdfSopenharmony_ci			SAFE_STAT(cleanup, dir, &s);
145f08c3bdfSopenharmony_ci			/*
146f08c3bdfSopenharmony_ci			 * i+2 because there are two links to each newly
147f08c3bdfSopenharmony_ci			 * created directory (the '.' and link from parent dir)
148f08c3bdfSopenharmony_ci			 */
149f08c3bdfSopenharmony_ci			if (s.st_nlink != (i + 2)) {
150f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup, "%s link counts have %d, should be %d",
151f08c3bdfSopenharmony_ci					 dir, (int)s.st_nlink, i + 2);
152f08c3bdfSopenharmony_ci				return 0;
153f08c3bdfSopenharmony_ci			} else {
154f08c3bdfSopenharmony_ci				tst_resm(TINFO, "the maximum subdirectories in "
155f08c3bdfSopenharmony_ci				 "%s is hit: %d", dir, (int)s.st_nlink);
156f08c3bdfSopenharmony_ci				return s.st_nlink;
157f08c3bdfSopenharmony_ci			}
158f08c3bdfSopenharmony_ci		case ENOSPC:
159f08c3bdfSopenharmony_ci		case EDQUOT:
160f08c3bdfSopenharmony_ci			tst_resm(TINFO | TERRNO, "mkdir(%s, 0755) failed",
161f08c3bdfSopenharmony_ci			         dirname);
162f08c3bdfSopenharmony_ci			goto max_subdirs_cleanup;
163f08c3bdfSopenharmony_ci		default:
164f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "mkdir(%s, 0755) failed "
165f08c3bdfSopenharmony_ci				 "unexpectedly: %s", dirname,
166f08c3bdfSopenharmony_ci				 strerror(errno));
167f08c3bdfSopenharmony_ci			return 0;
168f08c3bdfSopenharmony_ci		}
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci	}
171f08c3bdfSopenharmony_ci
172f08c3bdfSopenharmony_ci	tst_resm(TINFO, "Failed reach the subdirs limit on %s filesystem",
173f08c3bdfSopenharmony_ci		 tst_fs_type_name(fs_type));
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_cimax_subdirs_cleanup:
176f08c3bdfSopenharmony_ci	for (j = 0; j < i; j++) {
177f08c3bdfSopenharmony_ci		sprintf(dirname, "%s/testdir%d", dir, j);
178f08c3bdfSopenharmony_ci		SAFE_RMDIR(cleanup, dirname);
179f08c3bdfSopenharmony_ci	}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci	return 0;
182f08c3bdfSopenharmony_ci}
183