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 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/filio.h>
44 using namespace testing;
46 const static char FULLPATH[] = "mountpoint/foo";
47 const static char RELPATH[] = "foo";
49 class Bmap: public FuseTest {
51 virtual void SetUp() {
52 m_maxreadahead = UINT32_MAX;
55 void expect_bmap(uint64_t ino, uint64_t lbn, uint32_t blocksize, uint64_t pbn)
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);
65 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
66 SET_OUT_HEADER_LEN(out, bmap);
67 out.body.bmap.block = pbn;
71 void expect_lookup(const char *relpath, uint64_t ino, off_t size)
73 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1,
78 class BmapEof: public Bmap, public WithParamInterface<int> {};
82 * XXX The FUSE protocol does not include the runp and runb variables, so those
83 * must be guessed in-kernel.
87 struct fiobmap2_arg arg;
89 * Pick fsize and lbn large enough that max length runs won't reach
90 * either beginning or end of file
92 const off_t filesize = 1 << 30;
98 expect_lookup(RELPATH, 42, filesize);
99 expect_open(ino, 0, 1);
100 expect_bmap(ino, lbn, m_maxbcachebuf, pbn);
102 fd = open(FULLPATH, O_RDWR);
103 ASSERT_LE(0, fd) << strerror(errno);
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);
117 * If the daemon does not implement VOP_BMAP, fusefs should return sensible
120 TEST_F(Bmap, default_)
122 struct fiobmap2_arg arg;
123 const off_t filesize = 1 << 30;
124 const ino_t ino = 42;
128 expect_lookup(RELPATH, 42, filesize);
129 expect_open(ino, 0, 1);
130 EXPECT_CALL(*m_mock, process(
131 ResultOf([=](auto in) {
132 return (in.header.opcode == FUSE_BMAP);
135 ).WillOnce(Invoke(ReturnErrno(ENOSYS)));
137 fd = open(FULLPATH, O_RDWR);
138 ASSERT_LE(0, fd) << strerror(errno);
145 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
146 EXPECT_EQ(arg.bn, 0);
147 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
148 EXPECT_EQ(arg.runb, 0);
151 lbn = filesize / m_maxbcachebuf / 2;
155 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
156 EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
157 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
158 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
161 lbn = filesize / m_maxbcachebuf - 1;
165 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
166 EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
167 EXPECT_EQ(arg.runp, 0);
168 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
174 * VOP_BMAP should not query the server for the file's size, even if its cached
175 * attributes have expired.
176 * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=256937
182 * 1) lookup the file, setting attr_valid=0
183 * 2) Read more than one block, causing the kernel to issue VOP_BMAP to
185 * 3) Nothing should panic
186 * 4) Repeat the tests, truncating the file after different numbers of
187 * GETATTR operations.
190 const off_t filesize = 2 * m_maxbcachebuf;
191 const ino_t ino = 42;
192 mode_t mode = S_IFREG | 0644;
197 ngetattrs = GetParam();
198 FuseTest::expect_lookup(RELPATH, ino, mode, filesize, 1, 0);
199 expect_open(ino, 0, 1);
200 // Depending on ngetattrs, FUSE_READ could be called with either
201 // filesize or filesize / 2 .
202 EXPECT_CALL(*m_mock, process(
203 ResultOf([=](auto in) {
204 return (in.header.opcode == FUSE_READ &&
205 in.header.nodeid == ino &&
206 in.body.read.offset == 0 &&
207 ( in.body.read.size == filesize ||
208 in.body.read.size == filesize / 2));
211 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto& out) {
212 size_t osize = in.body.read.size;
213 out.header.len = sizeof(struct fuse_out_header) + osize;
214 bzero(out.body.bytes, osize);
216 EXPECT_CALL(*m_mock, process(
217 ResultOf([](auto in) {
218 return (in.header.opcode == FUSE_GETATTR &&
219 in.header.nodeid == ino);
222 ).Times(Between(ngetattrs - 1, ngetattrs))
224 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
225 SET_OUT_HEADER_LEN(out, attr);
226 out.body.attr.attr_valid = 0;
227 out.body.attr.attr.ino = ino;
228 out.body.attr.attr.mode = S_IFREG | 0644;
229 out.body.attr.attr.size = filesize;
231 EXPECT_CALL(*m_mock, process(
232 ResultOf([](auto in) {
233 return (in.header.opcode == FUSE_GETATTR &&
234 in.header.nodeid == ino);
238 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
239 SET_OUT_HEADER_LEN(out, attr);
240 out.body.attr.attr_valid = 0;
241 out.body.attr.attr.ino = ino;
242 out.body.attr.attr.mode = S_IFREG | 0644;
243 out.body.attr.attr.size = filesize / 2;
246 buf = calloc(1, filesize);
247 fd = open(FULLPATH, O_RDWR);
248 ASSERT_LE(0, fd) << strerror(errno);
249 read(fd, buf, filesize);
254 INSTANTIATE_TEST_CASE_P(BE, BmapEof,