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