xref: /freebsd/tests/sys/fs/fusefs/bmap.cc (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 	/*
87 	 * Pick fsize and lbn large enough that max length runs won't reach
88 	 * either beginning or end of file
89 	 */
90 	const off_t filesize = 1 << 30;
91 	int64_t lbn = 100;
92 	int64_t pbn = 12345;
93 	const ino_t ino = 42;
94 	int fd;
95 
96 	expect_lookup(RELPATH, 42, filesize);
97 	expect_open(ino, 0, 1);
98 	expect_bmap(ino, lbn, m_maxbcachebuf, pbn);
99 
100 	fd = open(FULLPATH, O_RDWR);
101 	ASSERT_LE(0, fd) << strerror(errno);
102 
103 	arg.bn = lbn;
104 	arg.runp = -1;
105 	arg.runb = -1;
106 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
107 	EXPECT_EQ(arg.bn, pbn);
108 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
109 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
110 }
111 
112 /*
113  * If the daemon does not implement VOP_BMAP, fusefs should return sensible
114  * defaults.
115  */
116 TEST_F(Bmap, default_)
117 {
118 	struct fiobmap2_arg arg;
119 	const off_t filesize = 1 << 30;
120 	const ino_t ino = 42;
121 	int64_t lbn;
122 	int fd;
123 
124 	expect_lookup(RELPATH, 42, filesize);
125 	expect_open(ino, 0, 1);
126 	EXPECT_CALL(*m_mock, process(
127 		ResultOf([=](auto in) {
128 			return (in.header.opcode == FUSE_BMAP);
129 		}, Eq(true)),
130 		_)
131 	).WillOnce(Invoke(ReturnErrno(ENOSYS)));
132 
133 	fd = open(FULLPATH, O_RDWR);
134 	ASSERT_LE(0, fd) << strerror(errno);
135 
136 	/* First block */
137 	lbn = 0;
138 	arg.bn = lbn;
139 	arg.runp = -1;
140 	arg.runb = -1;
141 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
142 	EXPECT_EQ(arg.bn, 0);
143 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
144 	EXPECT_EQ(arg.runb, 0);
145 
146 	/* In the middle */
147 	lbn = filesize / m_maxbcachebuf / 2;
148 	arg.bn = lbn;
149 	arg.runp = -1;
150 	arg.runb = -1;
151 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
152 	EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
153 	EXPECT_EQ(arg.runp, m_maxphys / m_maxbcachebuf - 1);
154 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
155 
156 	/* Last block */
157 	lbn = filesize / m_maxbcachebuf - 1;
158 	arg.bn = lbn;
159 	arg.runp = -1;
160 	arg.runb = -1;
161 	ASSERT_EQ(0, ioctl(fd, FIOBMAP2, &arg)) << strerror(errno);
162 	EXPECT_EQ(arg.bn, lbn * m_maxbcachebuf / DEV_BSIZE);
163 	EXPECT_EQ(arg.runp, 0);
164 	EXPECT_EQ(arg.runb, m_maxphys / m_maxbcachebuf - 1);
165 
166 	leak(fd);
167 }
168