xref: /freebsd/tests/sys/fs/fusefs/bmap.cc (revision e17f5b1d)
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:
51 virtual void SetUp() {
52 	m_maxreadahead = UINT32_MAX;
53 	FuseTest::SetUp();
54 }
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 
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 /*
79  * Test FUSE_BMAP
80  * XXX The FUSE protocol does not include the runp and runb variables, so those
81  * must be guessed in-kernel.
82  */
83 TEST_F(Bmap, bmap)
84 {
85 	struct fiobmap2_arg arg;
86 	const off_t filesize = 1 << 20;
87 	const ino_t ino = 42;
88 	int64_t lbn = 10;
89 	int64_t pbn = 12345;
90 	int fd;
91 
92 	expect_lookup(RELPATH, 42, filesize);
93 	expect_open(ino, 0, 1);
94 	expect_bmap(ino, lbn, m_maxbcachebuf, pbn);
95 
96 	fd = open(FULLPATH, O_RDWR);
97 	ASSERT_LE(0, fd) << strerror(errno);
98 
99 	arg.bn = lbn;
100 	arg.runp = -1;
101 	arg.runb = -1;
102 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
103 	EXPECT_EQ(arg.bn, pbn);
104 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
105 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
106 }
107 
108 /*
109  * If the daemon does not implement VOP_BMAP, fusefs should return sensible
110  * defaults.
111  */
112 TEST_F(Bmap, default_)
113 {
114 	struct fiobmap2_arg arg;
115 	const off_t filesize = 1 << 20;
116 	const ino_t ino = 42;
117 	int64_t lbn;
118 	int fd;
119 
120 	expect_lookup(RELPATH, 42, filesize);
121 	expect_open(ino, 0, 1);
122 	EXPECT_CALL(*m_mock, process(
123 		ResultOf([=](auto in) {
124 			return (in.header.opcode == FUSE_BMAP);
125 		}, Eq(true)),
126 		_)
127 	).WillOnce(Invoke(ReturnErrno(ENOSYS)));
128 
129 	fd = open(FULLPATH, O_RDWR);
130 	ASSERT_LE(0, fd) << strerror(errno);
131 
132 	/* First block */
133 	lbn = 0;
134 	arg.bn = lbn;
135 	arg.runp = -1;
136 	arg.runb = -1;
137 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
138 	EXPECT_EQ(arg.bn, 0);
139 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
140 	EXPECT_EQ(arg.runb, 0);
141 
142 	/* In the middle */
143 	lbn = filesize / m_maxbcachebuf / 2;
144 	arg.bn = lbn;
145 	arg.runp = -1;
146 	arg.runb = -1;
147 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
148 	EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
149 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
150 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
151 
152 	/* Last block */
153 	lbn = filesize / m_maxbcachebuf - 1;
154 	arg.bn = lbn;
155 	arg.runp = -1;
156 	arg.runb = -1;
157 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
158 	EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
159 	EXPECT_EQ(arg.runp, 0);
160 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
161 
162 	leak(fd);
163 }
164