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
6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci * (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Description:
22f08c3bdfSopenharmony_ci *  Verify that,
23f08c3bdfSopenharmony_ci *   1. rename() fails with -1 return value and sets errno to ELOOP, if too
24f08c3bdfSopenharmony_ci *      many symbolic links were encountered in resolving oldpath or newpath.
25f08c3bdfSopenharmony_ci *   2. rename() fails with -1 return value and sets errno to EROFS,
26f08c3bdfSopenharmony_ci *      if the file is on a read-only file system.
27f08c3bdfSopenharmony_ci *   3. rename() fails with -1 return value and sets errno to EMLINK,
28f08c3bdfSopenharmony_ci *	if the file named by old is a directory and the link count of
29f08c3bdfSopenharmony_ci *	the parent directory of new would exceed {LINK_MAX}.
30f08c3bdfSopenharmony_ci */
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#include <stdio.h>
33f08c3bdfSopenharmony_ci#include <errno.h>
34f08c3bdfSopenharmony_ci#include <sys/types.h>
35f08c3bdfSopenharmony_ci#include <sys/stat.h>
36f08c3bdfSopenharmony_ci#include <fcntl.h>
37f08c3bdfSopenharmony_ci#include <sys/mount.h>
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci#include "test.h"
40f08c3bdfSopenharmony_ci#include "safe_macros.h"
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cichar *TCID = "rename11";
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
45f08c3bdfSopenharmony_ci#define TEST_EROFS	"mntpoint/test_erofs"
46f08c3bdfSopenharmony_ci#define TEST_NEW_EROFS	"mntpoint/new_test_erofs"
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#define TEST_EMLINK	"test_emlink"
49f08c3bdfSopenharmony_ci#define TEST_NEW_EMLINK	"emlink_dir/testdir"
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci#define TEST_NEW_ELOOP	"new_test_eloop"
52f08c3bdfSopenharmony_ci#define ELOPFILE	"/test_eloop"
53f08c3bdfSopenharmony_cistatic char elooppathname[sizeof(ELOPFILE) * 43] = ".";
54f08c3bdfSopenharmony_cistatic int max_subdirs;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic const char *device;
57f08c3bdfSopenharmony_cistatic const char *fs_type;
58f08c3bdfSopenharmony_cistatic int mount_flag;
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void cleanup(void);
61f08c3bdfSopenharmony_cistatic void setup(void);
62f08c3bdfSopenharmony_cistatic void test_eloop(void);
63f08c3bdfSopenharmony_cistatic void test_erofs(void);
64f08c3bdfSopenharmony_cistatic void test_emlink(void);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void (*testfunc[])(void) = { test_eloop, test_erofs, test_emlink };
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(testfunc);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ciint main(int ac, char **av)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	int lc, i;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	setup();
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
79f08c3bdfSopenharmony_ci		tst_count = 0;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
82f08c3bdfSopenharmony_ci			(*testfunc[i])();
83f08c3bdfSopenharmony_ci	}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	cleanup();
86f08c3bdfSopenharmony_ci	tst_exit();
87f08c3bdfSopenharmony_ci}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_cistatic void setup(void)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	int i;
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	tst_require_root();
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	tst_tmpdir();
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	TEST_PAUSE;
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	fs_type = tst_dev_fs_type();
102f08c3bdfSopenharmony_ci	device = tst_acquire_device(cleanup);
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	if (!device)
105f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, MNTPOINT, 0755);
110f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
111f08c3bdfSopenharmony_ci	mount_flag = 1;
112f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_EROFS, 0644, NULL);
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_EMLINK, 0755);
115f08c3bdfSopenharmony_ci	max_subdirs = tst_fs_fill_subdirs(cleanup, "emlink_dir");
116f08c3bdfSopenharmony_ci	/*
117f08c3bdfSopenharmony_ci	 * NOTE: the ELOOP test is written based on that the consecutive
118f08c3bdfSopenharmony_ci	 * symlinks limits in kernel is hardwired to 40.
119f08c3bdfSopenharmony_ci	 */
120f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, "test_eloop", 0644);
121f08c3bdfSopenharmony_ci	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
122f08c3bdfSopenharmony_ci	for (i = 0; i < 43; i++)
123f08c3bdfSopenharmony_ci		strcat(elooppathname, ELOPFILE);
124f08c3bdfSopenharmony_ci}
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_cistatic void check_and_print(int expected_errno)
127f08c3bdfSopenharmony_ci{
128f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1) {
129f08c3bdfSopenharmony_ci		if (TEST_ERRNO == expected_errno) {
130f08c3bdfSopenharmony_ci			tst_resm(TPASS | TTERRNO, "failed as expected");
131f08c3bdfSopenharmony_ci		} else {
132f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
133f08c3bdfSopenharmony_ci				 "failed unexpectedly; expected - %d : %s",
134f08c3bdfSopenharmony_ci				 expected_errno, strerror(expected_errno));
135f08c3bdfSopenharmony_ci		}
136f08c3bdfSopenharmony_ci	} else {
137f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "rename succeeded unexpectedly");
138f08c3bdfSopenharmony_ci	}
139f08c3bdfSopenharmony_ci}
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_cistatic void test_eloop(void)
142f08c3bdfSopenharmony_ci{
143f08c3bdfSopenharmony_ci	TEST(rename(elooppathname, TEST_NEW_ELOOP));
144f08c3bdfSopenharmony_ci	check_and_print(ELOOP);
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0)
147f08c3bdfSopenharmony_ci		SAFE_UNLINK(cleanup, TEST_NEW_ELOOP);
148f08c3bdfSopenharmony_ci}
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_cistatic void test_erofs(void)
151f08c3bdfSopenharmony_ci{
152f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
153f08c3bdfSopenharmony_ci		   NULL);
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	TEST(rename(TEST_EROFS, TEST_NEW_EROFS));
156f08c3bdfSopenharmony_ci	check_and_print(EROFS);
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0)
159f08c3bdfSopenharmony_ci		SAFE_UNLINK(cleanup, TEST_NEW_EROFS);
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT, NULL);
162f08c3bdfSopenharmony_ci}
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_cistatic void test_emlink(void)
165f08c3bdfSopenharmony_ci{
166f08c3bdfSopenharmony_ci	if (max_subdirs == 0) {
167f08c3bdfSopenharmony_ci		tst_resm(TCONF, "EMLINK test is not appropriate");
168f08c3bdfSopenharmony_ci		return;
169f08c3bdfSopenharmony_ci	}
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	TEST(rename(TEST_EMLINK, TEST_NEW_EMLINK));
172f08c3bdfSopenharmony_ci	check_and_print(EMLINK);
173f08c3bdfSopenharmony_ci
174f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0)
175f08c3bdfSopenharmony_ci		SAFE_RMDIR(cleanup, TEST_NEW_EMLINK);
176f08c3bdfSopenharmony_ci}
177f08c3bdfSopenharmony_ci
178f08c3bdfSopenharmony_cistatic void cleanup(void)
179f08c3bdfSopenharmony_ci{
180f08c3bdfSopenharmony_ci	if (mount_flag && tst_umount(MNTPOINT) < 0)
181f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
182f08c3bdfSopenharmony_ci
183f08c3bdfSopenharmony_ci	if (device)
184f08c3bdfSopenharmony_ci		tst_release_device(device);
185f08c3bdfSopenharmony_ci
186f08c3bdfSopenharmony_ci	tst_rmdir();
187f08c3bdfSopenharmony_ci}
188