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
32 #include <sys/param.h>
38 using namespace testing;
40 class Getattr : public FuseTest {
42 void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
43 uint64_t size, int times, uint64_t attr_valid, uint32_t attr_valid_nsec)
45 EXPECT_LOOKUP(1, relpath)
47 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
48 SET_OUT_HEADER_LEN(out, entry);
49 out->body.entry.attr.mode = mode;
50 out->body.entry.nodeid = ino;
51 out->body.entry.attr.nlink = 1;
52 out->body.entry.attr_valid = attr_valid;
53 out->body.entry.attr_valid_nsec = attr_valid_nsec;
54 out->body.entry.attr.size = size;
55 out->body.entry.entry_valid = UINT64_MAX;
60 class Getattr_7_8: public FuseTest {
62 virtual void SetUp() {
63 m_kernel_minor_version = 8;
69 * If getattr returns a non-zero cache timeout, then subsequent VOP_GETATTRs
70 * should use the cached attributes, rather than query the daemon
72 TEST_F(Getattr, attr_cache)
74 const char FULLPATH[] = "mountpoint/some_file.txt";
75 const char RELPATH[] = "some_file.txt";
76 const uint64_t ino = 42;
79 EXPECT_LOOKUP(1, RELPATH)
80 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
81 SET_OUT_HEADER_LEN(out, entry);
82 out->body.entry.attr.mode = S_IFREG | 0644;
83 out->body.entry.nodeid = ino;
84 out->body.entry.entry_valid = UINT64_MAX;
86 EXPECT_CALL(*m_mock, process(
87 ResultOf([](auto in) {
88 return (in->header.opcode == FUSE_GETATTR &&
89 in->header.nodeid == ino);
92 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
93 SET_OUT_HEADER_LEN(out, attr);
94 out->body.attr.attr_valid = UINT64_MAX;
95 out->body.attr.attr.ino = ino; // Must match nodeid
96 out->body.attr.attr.mode = S_IFREG | 0644;
98 EXPECT_EQ(0, stat(FULLPATH, &sb));
99 /* The second stat(2) should use cached attributes */
100 EXPECT_EQ(0, stat(FULLPATH, &sb));
104 * If getattr returns a finite but non-zero cache timeout, then we should
105 * discard the cached attributes and requery the daemon after the timeout
108 TEST_F(Getattr, attr_cache_timeout)
110 const char FULLPATH[] = "mountpoint/some_file.txt";
111 const char RELPATH[] = "some_file.txt";
112 const uint64_t ino = 42;
115 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1, 0, 0);
116 EXPECT_CALL(*m_mock, process(
117 ResultOf([](auto in) {
118 return (in->header.opcode == FUSE_GETATTR &&
119 in->header.nodeid == ino);
123 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
124 SET_OUT_HEADER_LEN(out, attr);
125 out->body.attr.attr_valid_nsec = NAP_NS / 2;
126 out->body.attr.attr_valid = 0;
127 out->body.attr.attr.ino = ino; // Must match nodeid
128 out->body.attr.attr.mode = S_IFREG | 0644;
131 EXPECT_EQ(0, stat(FULLPATH, &sb));
133 /* Timeout has expired. stat(2) should requery the daemon */
134 EXPECT_EQ(0, stat(FULLPATH, &sb));
138 * If attr.blksize is zero, then the kernel should use a default value for
141 TEST_F(Getattr, blksize_zero)
143 const char FULLPATH[] = "mountpoint/some_file.txt";
144 const char RELPATH[] = "some_file.txt";
145 const uint64_t ino = 42;
148 expect_lookup(RELPATH, ino, S_IFREG | 0644, 1, 1, 0, 0);
149 EXPECT_CALL(*m_mock, process(
150 ResultOf([](auto in) {
151 return (in->header.opcode == FUSE_GETATTR &&
152 in->header.nodeid == ino);
155 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
156 SET_OUT_HEADER_LEN(out, attr);
157 out->body.attr.attr.mode = S_IFREG | 0644;
158 out->body.attr.attr.ino = ino; // Must match nodeid
159 out->body.attr.attr.blksize = 0;
162 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
163 EXPECT_EQ((blksize_t)PAGE_SIZE, sb.st_blksize);
166 TEST_F(Getattr, enoent)
168 const char FULLPATH[] = "mountpoint/some_file.txt";
169 const char RELPATH[] = "some_file.txt";
171 const uint64_t ino = 42;
173 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1, 0, 0);
174 EXPECT_CALL(*m_mock, process(
175 ResultOf([](auto in) {
176 return (in->header.opcode == FUSE_GETATTR &&
177 in->header.nodeid == ino);
180 ).WillOnce(Invoke(ReturnErrno(ENOENT)));
181 EXPECT_NE(0, stat(FULLPATH, &sb));
182 EXPECT_EQ(ENOENT, errno);
187 const char FULLPATH[] = "mountpoint/some_file.txt";
188 const char RELPATH[] = "some_file.txt";
189 const uint64_t ino = 42;
192 expect_lookup(RELPATH, ino, S_IFREG | 0644, 1, 1, 0, 0);
193 EXPECT_CALL(*m_mock, process(
194 ResultOf([](auto in) {
195 return (in->header.opcode == FUSE_GETATTR &&
196 in->header.nodeid == ino);
199 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
200 SET_OUT_HEADER_LEN(out, attr);
201 out->body.attr.attr.ino = ino; // Must match nodeid
202 out->body.attr.attr.mode = S_IFREG | 0644;
203 out->body.attr.attr.size = 1;
204 out->body.attr.attr.blocks = 2;
205 out->body.attr.attr.atime = 3;
206 out->body.attr.attr.mtime = 4;
207 out->body.attr.attr.ctime = 5;
208 out->body.attr.attr.atimensec = 6;
209 out->body.attr.attr.mtimensec = 7;
210 out->body.attr.attr.ctimensec = 8;
211 out->body.attr.attr.nlink = 9;
212 out->body.attr.attr.uid = 10;
213 out->body.attr.attr.gid = 11;
214 out->body.attr.attr.rdev = 12;
215 out->body.attr.attr.blksize = 12345;
218 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
219 EXPECT_EQ(1, sb.st_size);
220 EXPECT_EQ(2, sb.st_blocks);
221 EXPECT_EQ(3, sb.st_atim.tv_sec);
222 EXPECT_EQ(6, sb.st_atim.tv_nsec);
223 EXPECT_EQ(4, sb.st_mtim.tv_sec);
224 EXPECT_EQ(7, sb.st_mtim.tv_nsec);
225 EXPECT_EQ(5, sb.st_ctim.tv_sec);
226 EXPECT_EQ(8, sb.st_ctim.tv_nsec);
227 EXPECT_EQ(9ull, sb.st_nlink);
228 EXPECT_EQ(10ul, sb.st_uid);
229 EXPECT_EQ(11ul, sb.st_gid);
230 EXPECT_EQ(12ul, sb.st_rdev);
231 EXPECT_EQ((blksize_t)12345, sb.st_blksize);
232 EXPECT_EQ(ino, sb.st_ino);
233 EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
235 //st_birthtim and st_flags are not supported by protocol 7.8. They're
236 //only supported as OS-specific extensions to OSX.
237 //EXPECT_EQ(, sb.st_birthtim);
238 //EXPECT_EQ(, sb.st_flags);
240 //FUSE can't set st_blksize until protocol 7.9
243 TEST_F(Getattr_7_8, ok)
245 const char FULLPATH[] = "mountpoint/some_file.txt";
246 const char RELPATH[] = "some_file.txt";
247 const uint64_t ino = 42;
250 EXPECT_LOOKUP(1, RELPATH)
251 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
252 SET_OUT_HEADER_LEN(out, entry_7_8);
253 out->body.entry.attr.mode = S_IFREG | 0644;
254 out->body.entry.nodeid = ino;
255 out->body.entry.attr.nlink = 1;
256 out->body.entry.attr.size = 1;
258 EXPECT_CALL(*m_mock, process(
259 ResultOf([](auto in) {
260 return (in->header.opcode == FUSE_GETATTR &&
261 in->header.nodeid == ino);
264 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
265 SET_OUT_HEADER_LEN(out, attr_7_8);
266 out->body.attr.attr.ino = ino; // Must match nodeid
267 out->body.attr.attr.mode = S_IFREG | 0644;
268 out->body.attr.attr.size = 1;
269 out->body.attr.attr.blocks = 2;
270 out->body.attr.attr.atime = 3;
271 out->body.attr.attr.mtime = 4;
272 out->body.attr.attr.ctime = 5;
273 out->body.attr.attr.atimensec = 6;
274 out->body.attr.attr.mtimensec = 7;
275 out->body.attr.attr.ctimensec = 8;
276 out->body.attr.attr.nlink = 9;
277 out->body.attr.attr.uid = 10;
278 out->body.attr.attr.gid = 11;
279 out->body.attr.attr.rdev = 12;
282 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
283 EXPECT_EQ(1, sb.st_size);
284 EXPECT_EQ(2, sb.st_blocks);
285 EXPECT_EQ(3, sb.st_atim.tv_sec);
286 EXPECT_EQ(6, sb.st_atim.tv_nsec);
287 EXPECT_EQ(4, sb.st_mtim.tv_sec);
288 EXPECT_EQ(7, sb.st_mtim.tv_nsec);
289 EXPECT_EQ(5, sb.st_ctim.tv_sec);
290 EXPECT_EQ(8, sb.st_ctim.tv_nsec);
291 EXPECT_EQ(9ull, sb.st_nlink);
292 EXPECT_EQ(10ul, sb.st_uid);
293 EXPECT_EQ(11ul, sb.st_gid);
294 EXPECT_EQ(12ul, sb.st_rdev);
295 EXPECT_EQ(ino, sb.st_ino);
296 EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
298 //st_birthtim and st_flags are not supported by protocol 7.8. They're
299 //only supported as OS-specific extensions to OSX.