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
34 using namespace testing;
36 class Getattr : public FuseTest {
38 void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
39 uint64_t size, int times, uint64_t attr_valid, uint32_t attr_valid_nsec)
41 EXPECT_LOOKUP(1, relpath)
43 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
44 SET_OUT_HEADER_LEN(out, entry);
45 out->body.entry.attr.mode = mode;
46 out->body.entry.nodeid = ino;
47 out->body.entry.attr.nlink = 1;
48 out->body.entry.attr_valid = attr_valid;
49 out->body.entry.attr_valid_nsec = attr_valid_nsec;
50 out->body.entry.attr.size = size;
51 out->body.entry.entry_valid = UINT64_MAX;
58 * If getattr returns a non-zero cache timeout, then subsequent VOP_GETATTRs
59 * should use the cached attributes, rather than query the daemon
61 TEST_F(Getattr, attr_cache)
63 const char FULLPATH[] = "mountpoint/some_file.txt";
64 const char RELPATH[] = "some_file.txt";
65 const uint64_t ino = 42;
68 EXPECT_LOOKUP(1, RELPATH)
69 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
70 SET_OUT_HEADER_LEN(out, entry);
71 out->body.entry.attr.mode = S_IFREG | 0644;
72 out->body.entry.nodeid = ino;
73 out->body.entry.entry_valid = UINT64_MAX;
75 EXPECT_CALL(*m_mock, process(
76 ResultOf([](auto in) {
77 return (in->header.opcode == FUSE_GETATTR &&
78 in->header.nodeid == ino);
81 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
82 SET_OUT_HEADER_LEN(out, attr);
83 out->body.attr.attr_valid = UINT64_MAX;
84 out->body.attr.attr.ino = ino; // Must match nodeid
85 out->body.attr.attr.mode = S_IFREG | 0644;
87 EXPECT_EQ(0, stat(FULLPATH, &sb));
88 /* The second stat(2) should use cached attributes */
89 EXPECT_EQ(0, stat(FULLPATH, &sb));
93 * If getattr returns a finite but non-zero cache timeout, then we should
94 * discard the cached attributes and requery the daemon after the timeout
97 TEST_F(Getattr, attr_cache_timeout)
99 const char FULLPATH[] = "mountpoint/some_file.txt";
100 const char RELPATH[] = "some_file.txt";
101 const uint64_t ino = 42;
104 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1, 0, 0);
105 EXPECT_CALL(*m_mock, process(
106 ResultOf([](auto in) {
107 return (in->header.opcode == FUSE_GETATTR &&
108 in->header.nodeid == ino);
112 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
113 SET_OUT_HEADER_LEN(out, attr);
114 out->body.attr.attr_valid_nsec = NAP_NS / 2;
115 out->body.attr.attr_valid = 0;
116 out->body.attr.attr.ino = ino; // Must match nodeid
117 out->body.attr.attr.mode = S_IFREG | 0644;
120 EXPECT_EQ(0, stat(FULLPATH, &sb));
122 /* Timeout has expired. stat(2) should requery the daemon */
123 EXPECT_EQ(0, stat(FULLPATH, &sb));
126 TEST_F(Getattr, enoent)
128 const char FULLPATH[] = "mountpoint/some_file.txt";
129 const char RELPATH[] = "some_file.txt";
131 const uint64_t ino = 42;
133 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1, 0, 0);
134 EXPECT_CALL(*m_mock, process(
135 ResultOf([](auto in) {
136 return (in->header.opcode == FUSE_GETATTR &&
137 in->header.nodeid == ino);
140 ).WillOnce(Invoke(ReturnErrno(ENOENT)));
141 EXPECT_NE(0, stat(FULLPATH, &sb));
142 EXPECT_EQ(ENOENT, errno);
147 const char FULLPATH[] = "mountpoint/some_file.txt";
148 const char RELPATH[] = "some_file.txt";
149 const uint64_t ino = 42;
152 expect_lookup(RELPATH, ino, S_IFREG | 0644, 1, 1, 0, 0);
153 EXPECT_CALL(*m_mock, process(
154 ResultOf([](auto in) {
155 return (in->header.opcode == FUSE_GETATTR &&
156 in->header.nodeid == ino);
159 ).WillOnce(Invoke(ReturnImmediate([](auto i __unused, auto out) {
160 SET_OUT_HEADER_LEN(out, attr);
161 out->body.attr.attr.ino = ino; // Must match nodeid
162 out->body.attr.attr.mode = S_IFREG | 0644;
163 out->body.attr.attr.size = 1;
164 out->body.attr.attr.blocks = 2;
165 out->body.attr.attr.atime = 3;
166 out->body.attr.attr.mtime = 4;
167 out->body.attr.attr.ctime = 5;
168 out->body.attr.attr.atimensec = 6;
169 out->body.attr.attr.mtimensec = 7;
170 out->body.attr.attr.ctimensec = 8;
171 out->body.attr.attr.nlink = 9;
172 out->body.attr.attr.uid = 10;
173 out->body.attr.attr.gid = 11;
174 out->body.attr.attr.rdev = 12;
177 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
178 EXPECT_EQ(1, sb.st_size);
179 EXPECT_EQ(2, sb.st_blocks);
180 EXPECT_EQ(3, sb.st_atim.tv_sec);
181 EXPECT_EQ(6, sb.st_atim.tv_nsec);
182 EXPECT_EQ(4, sb.st_mtim.tv_sec);
183 EXPECT_EQ(7, sb.st_mtim.tv_nsec);
184 EXPECT_EQ(5, sb.st_ctim.tv_sec);
185 EXPECT_EQ(8, sb.st_ctim.tv_nsec);
186 EXPECT_EQ(9ull, sb.st_nlink);
187 EXPECT_EQ(10ul, sb.st_uid);
188 EXPECT_EQ(11ul, sb.st_gid);
189 EXPECT_EQ(12ul, sb.st_rdev);
190 EXPECT_EQ(ino, sb.st_ino);
191 EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
193 //st_birthtim and st_flags are not supported by protocol 7.8. They're
194 //only supported as OS-specific extensions to OSX.
195 //EXPECT_EQ(, sb.st_birthtim);
196 //EXPECT_EQ(, sb.st_flags);
198 //FUSE can't set st_blksize until protocol 7.9