xref: /freebsd/tests/sys/fs/fusefs/link.cc (revision b17b639832e707aab0e9514cf94727498e2d67bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <unistd.h>
35 }
36 
37 #include "mockfs.hh"
38 #include "utils.hh"
39 
40 using namespace testing;
41 
42 class Link: public FuseTest {
43 public:
44 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
45 {
46 	EXPECT_CALL(*m_mock, process(
47 		ResultOf([=](auto in) {
48 			const char *name = (const char*)in.body.bytes
49 				+ sizeof(struct fuse_link_in);
50 			return (in.header.opcode == FUSE_LINK &&
51 				in.body.link.oldnodeid == ino &&
52 				(0 == strcmp(name, relpath)));
53 		}, Eq(true)),
54 		_)
55 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
56 		SET_OUT_HEADER_LEN(out, entry);
57 		out.body.entry.nodeid = ino;
58 		out.body.entry.attr.mode = mode;
59 		out.body.entry.attr.nlink = nlink;
60 		out.body.entry.attr_valid = UINT64_MAX;
61 		out.body.entry.entry_valid = UINT64_MAX;
62 	})));
63 }
64 
65 void expect_lookup(const char *relpath, uint64_t ino)
66 {
67 	FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
68 }
69 };
70 
71 class Link_7_8: public FuseTest {
72 public:
73 virtual void SetUp() {
74 	m_kernel_minor_version = 8;
75 	FuseTest::SetUp();
76 }
77 
78 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
79 {
80 	EXPECT_CALL(*m_mock, process(
81 		ResultOf([=](auto in) {
82 			const char *name = (const char*)in.body.bytes
83 				+ sizeof(struct fuse_link_in);
84 			return (in.header.opcode == FUSE_LINK &&
85 				in.body.link.oldnodeid == ino &&
86 				(0 == strcmp(name, relpath)));
87 		}, Eq(true)),
88 		_)
89 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
90 		SET_OUT_HEADER_LEN(out, entry_7_8);
91 		out.body.entry.nodeid = ino;
92 		out.body.entry.attr.mode = mode;
93 		out.body.entry.attr.nlink = nlink;
94 		out.body.entry.attr_valid = UINT64_MAX;
95 		out.body.entry.entry_valid = UINT64_MAX;
96 	})));
97 }
98 
99 void expect_lookup(const char *relpath, uint64_t ino)
100 {
101 	FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, 0, 1);
102 }
103 };
104 
105 /*
106  * A successful link should clear the parent directory's attribute cache,
107  * because the fuse daemon should update its mtime and ctime
108  */
109 TEST_F(Link, clear_attr_cache)
110 {
111 	const char FULLPATH[] = "mountpoint/src";
112 	const char RELPATH[] = "src";
113 	const char FULLDST[] = "mountpoint/dst";
114 	const char RELDST[] = "dst";
115 	const uint64_t ino = 42;
116 	mode_t mode = S_IFREG | 0644;
117 	struct stat sb;
118 
119 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
120 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
121 	EXPECT_CALL(*m_mock, process(
122 		ResultOf([=](auto in) {
123 			return (in.header.opcode == FUSE_GETATTR &&
124 				in.header.nodeid == FUSE_ROOT_ID);
125 		}, Eq(true)),
126 		_)
127 	).Times(2)
128 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
129 		SET_OUT_HEADER_LEN(out, attr);
130 		out.body.attr.attr.ino = FUSE_ROOT_ID;
131 		out.body.attr.attr.mode = S_IFDIR | 0755;
132 		out.body.attr.attr_valid = UINT64_MAX;
133 	})));
134 
135 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
136 
137 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
138 		SET_OUT_HEADER_LEN(out, entry);
139 		out.body.entry.attr.mode = mode;
140 		out.body.entry.nodeid = ino;
141 		out.body.entry.attr.nlink = 1;
142 		out.body.entry.attr_valid = UINT64_MAX;
143 		out.body.entry.entry_valid = UINT64_MAX;
144 	})));
145 	expect_link(ino, RELPATH, mode, 2);
146 
147 	EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
148 	EXPECT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
149 	EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
150 }
151 
152 TEST_F(Link, emlink)
153 {
154 	const char FULLPATH[] = "mountpoint/lnk";
155 	const char RELPATH[] = "lnk";
156 	const char FULLDST[] = "mountpoint/dst";
157 	const char RELDST[] = "dst";
158 	uint64_t dst_ino = 42;
159 
160 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
161 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
162 	expect_lookup(RELDST, dst_ino);
163 
164 	EXPECT_CALL(*m_mock, process(
165 		ResultOf([=](auto in) {
166 			const char *name = (const char*)in.body.bytes
167 				+ sizeof(struct fuse_link_in);
168 			return (in.header.opcode == FUSE_LINK &&
169 				in.body.link.oldnodeid == dst_ino &&
170 				(0 == strcmp(name, RELPATH)));
171 		}, Eq(true)),
172 		_)
173 	).WillOnce(Invoke(ReturnErrno(EMLINK)));
174 
175 	EXPECT_EQ(-1, link(FULLDST, FULLPATH));
176 	EXPECT_EQ(EMLINK, errno);
177 }
178 
179 /*
180  * A hard link should always have the same inode as its source.  If it doesn't,
181  * then it's not a hard link.
182  */
183 TEST_F(Link, bad_inode)
184 {
185 	const char FULLPATH[] = "mountpoint/src";
186 	const char RELPATH[] = "src";
187 	const char FULLDST[] = "mountpoint/dst";
188 	const char RELDST[] = "dst";
189 	const uint64_t src_ino = 42;
190 	const uint64_t dst_ino = 43;
191 	mode_t mode = S_IFREG | 0644;
192 
193 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
194 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
195 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
196 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
197 		SET_OUT_HEADER_LEN(out, entry);
198 		out.body.entry.attr.mode = mode;
199 		out.body.entry.nodeid = dst_ino;
200 		out.body.entry.attr.nlink = 1;
201 		out.body.entry.attr_valid = UINT64_MAX;
202 		out.body.entry.entry_valid = UINT64_MAX;
203 	})));
204 	EXPECT_CALL(*m_mock, process(
205 		ResultOf([=](auto in) {
206 			const char *name = (const char*)in.body.bytes
207 				+ sizeof(struct fuse_link_in);
208 			return (in.header.opcode == FUSE_LINK &&
209 				in.body.link.oldnodeid == dst_ino &&
210 				(0 == strcmp(name, RELPATH)));
211 		}, Eq(true)),
212 		_)
213 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
214 		SET_OUT_HEADER_LEN(out, entry);
215 		out.body.entry.nodeid = src_ino;
216 		out.body.entry.attr.mode = mode;
217 		out.body.entry.attr.nlink = 2;
218 		out.body.entry.attr_valid = UINT64_MAX;
219 		out.body.entry.entry_valid = UINT64_MAX;
220 	})));
221 
222 	ASSERT_EQ(-1, link(FULLDST, FULLPATH));
223 	ASSERT_EQ(EIO, errno);
224 }
225 
226 TEST_F(Link, ok)
227 {
228 	const char FULLPATH[] = "mountpoint/src";
229 	const char RELPATH[] = "src";
230 	const char FULLDST[] = "mountpoint/dst";
231 	const char RELDST[] = "dst";
232 	const uint64_t ino = 42;
233 	mode_t mode = S_IFREG | 0644;
234 	struct stat sb;
235 
236 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
237 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
238 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
239 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
240 		SET_OUT_HEADER_LEN(out, entry);
241 		out.body.entry.attr.mode = mode;
242 		out.body.entry.nodeid = ino;
243 		out.body.entry.attr.nlink = 1;
244 		out.body.entry.attr_valid = UINT64_MAX;
245 		out.body.entry.entry_valid = UINT64_MAX;
246 	})));
247 	expect_link(ino, RELPATH, mode, 2);
248 
249 	ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
250 	// Check that the original file's nlink count has increased.
251 	ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
252 	EXPECT_EQ(2ul, sb.st_nlink);
253 }
254 
255 TEST_F(Link_7_8, ok)
256 {
257 	const char FULLPATH[] = "mountpoint/src";
258 	const char RELPATH[] = "src";
259 	const char FULLDST[] = "mountpoint/dst";
260 	const char RELDST[] = "dst";
261 	const uint64_t ino = 42;
262 	mode_t mode = S_IFREG | 0644;
263 	struct stat sb;
264 
265 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
266 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
267 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
268 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
269 		SET_OUT_HEADER_LEN(out, entry_7_8);
270 		out.body.entry.attr.mode = mode;
271 		out.body.entry.nodeid = ino;
272 		out.body.entry.attr.nlink = 1;
273 		out.body.entry.attr_valid = UINT64_MAX;
274 		out.body.entry.entry_valid = UINT64_MAX;
275 	})));
276 	expect_link(ino, RELPATH, mode, 2);
277 
278 	ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
279 	// Check that the original file's nlink count has increased.
280 	ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
281 	EXPECT_EQ(2ul, sb.st_nlink);
282 }
283