2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
40 using namespace testing;
42 class Link: public FuseTest {
44 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
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)));
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;
65 void expect_lookup(const char *relpath, uint64_t ino)
67 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
71 class Link_7_8: public FuseTest {
73 virtual void SetUp() {
74 m_kernel_minor_version = 8;
78 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
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)));
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;
99 void expect_lookup(const char *relpath, uint64_t ino)
101 FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, 0, 1);
106 * A successful link should clear the parent directory's attribute cache,
107 * because the fuse daemon should update its mtime and ctime
109 TEST_F(Link, clear_attr_cache)
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;
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);
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;
135 EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
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;
145 expect_link(ino, RELPATH, mode, 2);
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);
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;
160 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
161 .WillOnce(Invoke(ReturnErrno(ENOENT)));
162 expect_lookup(RELDST, dst_ino);
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)));
173 ).WillOnce(Invoke(ReturnErrno(EMLINK)));
175 EXPECT_EQ(-1, link(FULLDST, FULLPATH));
176 EXPECT_EQ(EMLINK, errno);
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.
183 TEST_F(Link, bad_inode)
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;
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;
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)));
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;
222 ASSERT_EQ(-1, link(FULLDST, FULLPATH));
223 ASSERT_EQ(EIO, errno);
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;
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;
247 expect_link(ino, RELPATH, mode, 2);
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);
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;
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;
276 expect_link(ino, RELPATH, mode, 2);
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);