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);
181 const char FULLPATH[] = "mountpoint/src";
182 const char RELPATH[] = "src";
183 const char FULLDST[] = "mountpoint/dst";
184 const char RELDST[] = "dst";
185 const uint64_t ino = 42;
186 mode_t mode = S_IFREG | 0644;
189 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
190 .WillOnce(Invoke(ReturnErrno(ENOENT)));
191 EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
192 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
193 SET_OUT_HEADER_LEN(out, entry);
194 out.body.entry.attr.mode = mode;
195 out.body.entry.nodeid = ino;
196 out.body.entry.attr.nlink = 1;
197 out.body.entry.attr_valid = UINT64_MAX;
198 out.body.entry.entry_valid = UINT64_MAX;
200 expect_link(ino, RELPATH, mode, 2);
202 ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
203 // Check that the original file's nlink count has increased.
204 ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
205 EXPECT_EQ(2ul, sb.st_nlink);
210 const char FULLPATH[] = "mountpoint/src";
211 const char RELPATH[] = "src";
212 const char FULLDST[] = "mountpoint/dst";
213 const char RELDST[] = "dst";
214 const uint64_t ino = 42;
215 mode_t mode = S_IFREG | 0644;
218 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
219 .WillOnce(Invoke(ReturnErrno(ENOENT)));
220 EXPECT_LOOKUP(FUSE_ROOT_ID, RELDST)
221 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
222 SET_OUT_HEADER_LEN(out, entry_7_8);
223 out.body.entry.attr.mode = mode;
224 out.body.entry.nodeid = ino;
225 out.body.entry.attr.nlink = 1;
226 out.body.entry.attr_valid = UINT64_MAX;
227 out.body.entry.entry_valid = UINT64_MAX;
229 expect_link(ino, RELPATH, mode, 2);
231 ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
232 // Check that the original file's nlink count has increased.
233 ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
234 EXPECT_EQ(2ul, sb.st_nlink);