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>
33 #include <sys/ioctl.h>
34 #include <sys/filio.h>
42 using namespace testing;
44 const static char FULLPATH[] = "mountpoint/foo";
45 const static char RELPATH[] = "foo";
47 class Bmap: public FuseTest {
49 virtual void SetUp() {
50 m_maxreadahead = UINT32_MAX;
53 void expect_bmap(uint64_t ino, uint64_t lbn, uint32_t blocksize, uint64_t pbn)
55 EXPECT_CALL(*m_mock, process(
56 ResultOf([=](auto in) {
57 return (in.header.opcode == FUSE_BMAP &&
58 in.header.nodeid == ino &&
59 in.body.bmap.block == lbn &&
60 in.body.bmap.blocksize == blocksize);
63 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
64 SET_OUT_HEADER_LEN(out, bmap);
65 out.body.bmap.block = pbn;
69 void expect_lookup(const char *relpath, uint64_t ino, off_t size)
71 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1,
78 * XXX The FUSE protocol does not include the runp and runb variables, so those
79 * must be guessed in-kernel.
83 struct fiobmap2_arg arg;
84 const off_t filesize = 1 << 20;
90 expect_lookup(RELPATH, 42, filesize);
91 expect_open(ino, 0, 1);
92 expect_bmap(ino, lbn, m_maxbcachebuf, pbn);
94 fd = open(FULLPATH, O_RDWR);
95 ASSERT_LE(0, fd) << strerror(errno);
100 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
101 EXPECT_EQ(arg.bn, pbn);
102 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
103 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
107 * If the daemon does not implement VOP_BMAP, fusefs should return sensible
110 TEST_F(Bmap, default_)
112 struct fiobmap2_arg arg;
113 const off_t filesize = 1 << 20;
114 const ino_t ino = 42;
118 expect_lookup(RELPATH, 42, filesize);
119 expect_open(ino, 0, 1);
120 EXPECT_CALL(*m_mock, process(
121 ResultOf([=](auto in) {
122 return (in.header.opcode == FUSE_BMAP);
125 ).WillOnce(Invoke(ReturnErrno(ENOSYS)));
127 fd = open(FULLPATH, O_RDWR);
128 ASSERT_LE(0, fd) << strerror(errno);
135 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
136 EXPECT_EQ(arg.bn, 0);
137 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
138 EXPECT_EQ(arg.runb, 0);
141 lbn = filesize / m_maxbcachebuf / 2;
145 ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
146 EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
147 EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
148 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
151 lbn = filesize / m_maxbcachebuf - 1;
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, 0);
158 EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);