1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/filio.h>
37
38 #include <fcntl.h>
39 }
40
41 #include "mockfs.hh"
42 #include "utils.hh"
43
44 using namespace testing;
45
46 const static char FULLPATH[] = "mountpoint/foo";
47 const static char RELPATH[] = "foo";
48
49 class Bmap: public FuseTest {
50 public:
SetUp()51 virtual void SetUp() {
52 m_maxreadahead = UINT32_MAX;
53 FuseTest::SetUp();
54 }
expect_bmap(uint64_t ino,uint64_t lbn,uint32_t blocksize,uint64_t pbn)55 void expect_bmap(uint64_t ino, uint64_t lbn, uint32_t blocksize, uint64_t pbn)
56 {
57 EXPECT_CALL(*m_mock, process(
58 ResultOf([=](auto in) {
59 return (in.header.opcode == FUSE_BMAP &&
60 in.header.nodeid == ino &&
61 in.body.bmap.block == lbn &&
62 in.body.bmap.blocksize == blocksize);
63 }, Eq(true)),
64 _)
65 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
66 SET_OUT_HEADER_LEN(out, bmap);
67 out.body.bmap.block = pbn;
68 })));
69 }
70
expect_lookup(const char * relpath,uint64_t ino,off_t size)71 void expect_lookup(const char *relpath, uint64_t ino, off_t size)
72 {
73 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1,
74 UINT64_MAX);
75 }
76 };
77
78 class BmapEof: public Bmap, public WithParamInterface<int> {};
79
80 /*
81 * Test FUSE_BMAP
82 * XXX The FUSE protocol does not include the runp and runb variables, so those
83 * must be guessed in-kernel.
84 */
TEST_F(Bmap,bmap)85 TEST_F(Bmap, bmap)
86 {
87 struct fiobmap2_arg arg;
88 /*
89 * Pick fsize and lbn large enough that max length runs won't reach
90 * either beginning or end of file
91 */
92 const off_t filesize = 1 << 30;
93 int64_t lbn = 100;
94 int64_t pbn = 12345;
95 const ino_t ino = 42;
96 int fd;
97
98 expect_lookup(RELPATH, 42, filesize);
99 expect_open(ino, 0, 1);
100 expect_bmap(ino, lbn, m_maxbcachebuf, pbn);
101
102 fd = open(FULLPATH, O_RDWR);
103 ASSERT_LE(0, fd) << strerror(errno);
104
105 arg.bn = lbn;
106 arg.runp = -1;
107 arg.runb = -1;
108 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
109 EXPECT_EQ(arg.bn, pbn);
110 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
111 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
112 }
113
114 /*
115 * If the daemon does not implement VOP_BMAP, fusefs should return sensible
116 * defaults.
117 */
TEST_F(Bmap,default_)118 TEST_F(Bmap, default_)
119 {
120 struct fiobmap2_arg arg;
121 const off_t filesize = 1 << 30;
122 const ino_t ino = 42;
123 int64_t lbn;
124 int fd;
125
126 expect_lookup(RELPATH, 42, filesize);
127 expect_open(ino, 0, 1);
128 EXPECT_CALL(*m_mock, process(
129 ResultOf([=](auto in) {
130 return (in.header.opcode == FUSE_BMAP);
131 }, Eq(true)),
132 _)
133 ).WillOnce(Invoke(ReturnErrno(ENOSYS)));
134
135 fd = open(FULLPATH, O_RDWR);
136 ASSERT_LE(0, fd) << strerror(errno);
137
138 /* First block */
139 lbn = 0;
140 arg.bn = lbn;
141 arg.runp = -1;
142 arg.runb = -1;
143 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
144 EXPECT_EQ(arg.bn, 0);
145 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
146 EXPECT_EQ(arg.runb, 0);
147
148 /* In the middle */
149 lbn = filesize / m_maxbcachebuf / 2;
150 arg.bn = lbn;
151 arg.runp = -1;
152 arg.runb = -1;
153 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
154 EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
155 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
156 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
157
158 /* Last block */
159 lbn = filesize / m_maxbcachebuf - 1;
160 arg.bn = lbn;
161 arg.runp = -1;
162 arg.runb = -1;
163 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
164 EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
165 EXPECT_EQ(arg.runp, 0);
166 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
167
168 leak(fd);
169 }
170
171 /*
172 * VOP_BMAP should not query the server for the file's size, even if its cached
173 * attributes have expired.
174 * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=256937
175 */
TEST_P(BmapEof,eof)176 TEST_P(BmapEof, eof)
177 {
178 /*
179 * Outline:
180 * 1) lookup the file, setting attr_valid=0
181 * 2) Read more than one block, causing the kernel to issue VOP_BMAP to
182 * plan readahead.
183 * 3) Nothing should panic
184 * 4) Repeat the tests, truncating the file after different numbers of
185 * GETATTR operations.
186 */
187 Sequence seq;
188 const off_t filesize = 2 * m_maxbcachebuf;
189 const ino_t ino = 42;
190 mode_t mode = S_IFREG | 0644;
191 void *contents, *buf;
192 int fd;
193 int ngetattrs;
194
195 ngetattrs = GetParam();
196 contents = calloc(1, filesize);
197 FuseTest::expect_lookup(RELPATH, ino, mode, filesize, 1, 0);
198 expect_open(ino, 0, 1);
199 // Depending on ngetattrs, FUSE_READ could be called with either
200 // filesize or filesize / 2 .
201 EXPECT_CALL(*m_mock, process(
202 ResultOf([=](auto in) {
203 return (in.header.opcode == FUSE_READ &&
204 in.header.nodeid == ino &&
205 in.body.read.offset == 0 &&
206 ( in.body.read.size == filesize ||
207 in.body.read.size == filesize / 2));
208 }, Eq(true)),
209 _)
210 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto& out) {
211 size_t osize = in.body.read.size;
212 out.header.len = sizeof(struct fuse_out_header) + osize;
213 bzero(out.body.bytes, osize);
214 })));
215 EXPECT_CALL(*m_mock, process(
216 ResultOf([](auto in) {
217 return (in.header.opcode == FUSE_GETATTR &&
218 in.header.nodeid == ino);
219 }, Eq(true)),
220 _)
221 ).Times(Between(ngetattrs - 1, ngetattrs))
222 .InSequence(seq)
223 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
224 SET_OUT_HEADER_LEN(out, attr);
225 out.body.attr.attr_valid = 0;
226 out.body.attr.attr.ino = ino;
227 out.body.attr.attr.mode = S_IFREG | 0644;
228 out.body.attr.attr.size = filesize;
229 })));
230 EXPECT_CALL(*m_mock, process(
231 ResultOf([](auto in) {
232 return (in.header.opcode == FUSE_GETATTR &&
233 in.header.nodeid == ino);
234 }, Eq(true)),
235 _)
236 ).InSequence(seq)
237 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
238 SET_OUT_HEADER_LEN(out, attr);
239 out.body.attr.attr_valid = 0;
240 out.body.attr.attr.ino = ino;
241 out.body.attr.attr.mode = S_IFREG | 0644;
242 out.body.attr.attr.size = filesize / 2;
243 })));
244
245 buf = calloc(1, filesize);
246 fd = open(FULLPATH, O_RDWR);
247 ASSERT_LE(0, fd) << strerror(errno);
248 read(fd, buf, filesize);
249 }
250
251 INSTANTIATE_TEST_CASE_P(BE, BmapEof,
252 Values(1, 2, 3)
253 );
254