blob: 1b45f79094c87ed08fa569213026c4928ce33d0c [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"
27
28#define MAX_SANE_HARD_LINKS 65535
29
30int tst_fs_fill_hardlinks(void (*cleanup) (void), const char *dir)
31{
32 unsigned int i, j;
33 char base_filename[PATH_MAX], link_filename[PATH_MAX];
34 struct stat s;
35
36 if (stat(dir, &s) == -1 && errno == ENOENT)
37 SAFE_MKDIR(cleanup, dir, 0744);
38
39 SAFE_STAT(cleanup, dir, &s);
40 if (!S_ISDIR(s.st_mode))
41 tst_brkm(TBROK, cleanup, "%s is not directory", dir);
42
43 sprintf(base_filename, "%s/testfile0", dir);
44 SAFE_TOUCH(cleanup, base_filename, 0644, NULL);
45
46 for (i = 1; i < MAX_SANE_HARD_LINKS; i++) {
47 sprintf(link_filename, "%s/testfile%d", dir, i);
48
49 if (link(base_filename, link_filename) == 0)
50 continue;
51
52 switch (errno) {
53 case EMLINK:
54 SAFE_STAT(cleanup, base_filename, &s);
55 if (s.st_nlink != i) {
56 tst_brkm(TBROK, cleanup, "wrong number of "
57 "hard links for %s have %i, should be"
58 " %d", base_filename,
59 (int)s.st_nlink, i);
60 } else {
61 tst_resm(TINFO, "the maximum number of hard "
62 "links to %s is hit: %d",
63 base_filename, (int)s.st_nlink);
64 return s.st_nlink;
65 }
66 case ENOSPC:
67 case EDQUOT:
68 tst_resm(TINFO | TERRNO, "link(%s, %s) failed",
69 base_filename, link_filename);
70 goto max_hardlinks_cleanup;
71 default:
72 tst_brkm(TBROK, cleanup, "link(%s, %s) failed "
73 "unexpectedly: %s", base_filename,
74 link_filename, strerror(errno));
75 }
76 }
77
78 tst_resm(TINFO, "Failed reach the hardlinks limit");
79
80max_hardlinks_cleanup:
81 for (j = 0; j < i; j++) {
82 sprintf(link_filename, "%s/testfile%d", dir, j);
83 SAFE_UNLINK(cleanup, link_filename);
84 }
85
86 return 0;
87}
88
89int tst_fs_fill_subdirs(void (*cleanup) (void), const char *dir)
90{
91 unsigned int i, j;
92 char dirname[PATH_MAX];
93 struct stat s;
94
95 if (stat(dir, &s) == -1 && errno == ENOENT)
96 SAFE_MKDIR(cleanup, dir, 0744);
97
98 SAFE_STAT(cleanup, dir, &s);
99 if (!S_ISDIR(s.st_mode))
100 tst_brkm(TBROK, cleanup, "%s is not directory", dir);
101
102 for (i = 0; i < MAX_SANE_HARD_LINKS; i++) {
103 sprintf(dirname, "%s/testdir%d", dir, i);
104
105 if (mkdir(dirname, 0755) == 0)
106 continue;
107
108 switch (errno) {
109 case EMLINK:
110 SAFE_STAT(cleanup, dir, &s);
111 /*
112 * i+2 because there are two links to each newly
113 * created directory (the '.' and link from parent dir)
114 */
115 if (s.st_nlink != (i + 2)) {
116 tst_brkm(TBROK, cleanup, "%s link counts have"
117 "%d, should be %d", dir,
118 (int)s.st_nlink, i + 2);
119 } else {
120 tst_resm(TINFO, "the maximum subdirectories in "
121 "%s is hit: %d", dir, (int)s.st_nlink);
122 return s.st_nlink;
123 }
124 case ENOSPC:
125 case EDQUOT:
126 tst_resm(TINFO | TERRNO, "mkdir(%s, 0755) failed",
127 dirname);
128 goto max_subdirs_cleanup;
129 default:
130 tst_brkm(TBROK, cleanup, "mkdir(%s, 0755) failed "
131 "unexpectedly: %s", dirname,
132 strerror(errno));
133 }
134
135 }
136
137 tst_resm(TINFO, "Failed reach the subdirs limit");
138
139max_subdirs_cleanup:
140 for (j = 0; j < i; j++) {
141 sprintf(dirname, "%s/testdir%d", dir, j);
142 SAFE_RMDIR(cleanup, dirname);
143 }
144
145 return 0;
146}