]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/io.cc
fusefs: improve the short read fix from r349279
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / io.cc
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
31 extern "C" {
32 #include <sys/mman.h>
33
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 }
38
39 #include "mockfs.hh"
40 #include "utils.hh"
41
42 /* 
43  * For testing I/O like fsx does, but deterministically and without a real
44  * underlying file system
45  *
46  * TODO: after fusefs gains the options to select cache mode for each mount
47  * point, run each of these tests for all cache modes.
48  */
49
50 using namespace testing;
51
52 const char FULLPATH[] = "mountpoint/some_file.txt";
53 const char RELPATH[] = "some_file.txt";
54 const uint64_t ino = 42;
55
56 static void compare(const void *tbuf, const void *controlbuf, off_t baseofs,
57         ssize_t size)
58 {
59         int i;
60
61         for (i = 0; i < size; i++) {
62                 if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) {
63                         off_t ofs = baseofs + i;
64                         FAIL() << "miscompare at offset "
65                                << std::hex
66                                << std::showbase
67                                << ofs
68                                << ".  expected = "
69                                << std::setw(2)
70                                << (unsigned)((const uint8_t*)controlbuf)[i]
71                                << " got = "
72                                << (unsigned)((const uint8_t*)tbuf)[i];
73                 }
74         }
75 }
76
77 class Io: public FuseTest,
78           public WithParamInterface<tuple<uint32_t, uint32_t, bool>> {
79 public:
80 int m_backing_fd, m_control_fd, m_test_fd;
81 off_t m_filesize;
82
83 Io(): m_backing_fd(-1), m_control_fd(-1) {};
84
85 void SetUp()
86 {
87         m_filesize = 0;
88         m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
89         if (m_backing_fd < 0)
90                 FAIL() << strerror(errno);
91         m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
92         if (m_control_fd < 0)
93                 FAIL() << strerror(errno);
94         srandom(22'9'1982);     // Seed with my birthday
95
96         m_init_flags = get<0>(GetParam());
97         m_maxwrite = get<1>(GetParam());
98         m_async = get<2>(GetParam());
99
100         FuseTest::SetUp();
101         if (IsSkipped())
102                 return;
103
104         expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
105         expect_open(ino, 0, 1);
106         EXPECT_CALL(*m_mock, process(
107                 ResultOf([=](auto in) {
108                         return (in.header.opcode == FUSE_WRITE &&
109                                 in.header.nodeid == ino);
110                 }, Eq(true)),
111                 _)
112         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
113                 const char *buf = (const char*)in.body.bytes +
114                         sizeof(struct fuse_write_in);
115                 ssize_t isize = in.body.write.size;
116                 off_t iofs = in.body.write.offset;
117
118                 ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
119                         << strerror(errno);
120                 SET_OUT_HEADER_LEN(out, write);
121                 out.body.write.size = isize;
122         })));
123         EXPECT_CALL(*m_mock, process(
124                 ResultOf([=](auto in) {
125                         return (in.header.opcode == FUSE_READ &&
126                                 in.header.nodeid == ino);
127                 }, Eq(true)),
128                 _)
129         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
130                 ssize_t isize = in.body.write.size;
131                 off_t iofs = in.body.write.offset;
132                 void *buf = out.body.bytes;
133                 ssize_t osize;
134
135                 osize = pread(m_backing_fd, buf, isize, iofs);
136                 ASSERT_LE(0, osize) << strerror(errno);
137                 out.header.len = sizeof(struct fuse_out_header) + osize;
138         })));
139         EXPECT_CALL(*m_mock, process(
140                 ResultOf([=](auto in) {
141                         uint32_t valid = FATTR_SIZE | FATTR_FH;
142                         return (in.header.opcode == FUSE_SETATTR &&
143                                 in.header.nodeid == ino &&
144                                 in.body.setattr.valid == valid);
145                 }, Eq(true)),
146                 _)
147         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
148                 ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
149                         << strerror(errno);
150                 SET_OUT_HEADER_LEN(out, attr);
151                 out.body.attr.attr.ino = ino;
152                 out.body.attr.attr.mode = S_IFREG | 0755;
153                 out.body.attr.attr.size = in.body.setattr.size;
154                 out.body.attr.attr_valid = UINT64_MAX;
155         })));
156         /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
157         EXPECT_CALL(*m_mock, process(
158                 ResultOf([=](auto in) {
159                         return (in.header.opcode == FUSE_FLUSH &&
160                                 in.header.nodeid == ino);
161                 }, Eq(true)),
162                 _)
163         ).WillRepeatedly(Invoke(ReturnErrno(0)));
164         EXPECT_CALL(*m_mock, process(
165                 ResultOf([=](auto in) {
166                         return (in.header.opcode == FUSE_RELEASE &&
167                                 in.header.nodeid == ino);
168                 }, Eq(true)),
169                 _)
170         ).WillRepeatedly(Invoke(ReturnErrno(0)));
171
172         m_test_fd = open(FULLPATH, O_RDWR );
173         EXPECT_LE(0, m_test_fd) << strerror(errno);
174 }
175
176 void TearDown()
177 {
178         if (m_backing_fd >= 0)
179                 close(m_backing_fd);
180         if (m_control_fd >= 0)
181                 close(m_control_fd);
182         FuseTest::TearDown();
183         /* Deliberately leak test_fd */
184 }
185
186 void do_ftruncate(off_t offs)
187 {
188         ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
189         ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
190         m_filesize = offs;
191 }
192
193 void do_mapread(ssize_t size, off_t offs)
194 {
195         void *control_buf, *p;
196         off_t pg_offset, page_mask;
197         size_t map_size;
198
199         page_mask = getpagesize() - 1;
200         pg_offset = offs & page_mask;
201         map_size = pg_offset + size;
202
203         p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd,
204             offs - pg_offset);
205         ASSERT_NE(p, MAP_FAILED) << strerror(errno);
206
207         control_buf = malloc(size);
208         ASSERT_NE(NULL, control_buf) << strerror(errno);
209
210         ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
211                 << strerror(errno);
212
213         compare((void*)((char*)p + pg_offset), control_buf, offs, size);
214
215         ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
216         free(control_buf);
217 }
218
219 void do_read(ssize_t size, off_t offs)
220 {
221         void *test_buf, *control_buf;
222         ssize_t r;
223
224         test_buf = malloc(size);
225         ASSERT_NE(NULL, test_buf) << strerror(errno);
226         control_buf = malloc(size);
227         ASSERT_NE(NULL, control_buf) << strerror(errno);
228
229         errno = 0;
230         r = pread(m_test_fd, test_buf, size, offs);
231         ASSERT_NE(-1, r) << strerror(errno);
232         ASSERT_EQ(size, r) << "unexpected short read";
233         r = pread(m_control_fd, control_buf, size, offs);
234         ASSERT_NE(-1, r) << strerror(errno);
235         ASSERT_EQ(size, r) << "unexpected short read";
236
237         compare(test_buf, control_buf, offs, size);
238
239         free(control_buf);
240         free(test_buf);
241 }
242
243 void do_mapwrite(ssize_t size, off_t offs)
244 {
245         char *buf;
246         void *p;
247         off_t pg_offset, page_mask;
248         size_t map_size;
249         long i;
250
251         page_mask = getpagesize() - 1;
252         pg_offset = offs & page_mask;
253         map_size = pg_offset + size;
254
255         buf = (char*)malloc(size);
256         ASSERT_NE(NULL, buf) << strerror(errno);
257         for (i=0; i < size; i++)
258                 buf[i] = random();
259
260         if (offs + size > m_filesize) {
261                 /* 
262                  * Must manually extend.  vm_mmap_vnode will not implicitly
263                  * extend a vnode
264                  */
265                 do_ftruncate(offs + size);
266         }
267
268         p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
269             MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset);
270         ASSERT_NE(p, MAP_FAILED) << strerror(errno);
271
272         bcopy(buf, (char*)p + pg_offset, size);
273         ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
274                 << strerror(errno);
275
276         free(buf);
277         ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
278 }
279
280 void do_write(ssize_t size, off_t offs)
281 {
282         char *buf;
283         long i;
284
285         buf = (char*)malloc(size);
286         ASSERT_NE(NULL, buf) << strerror(errno);
287         for (i=0; i < size; i++)
288                 buf[i] = random();
289
290         ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
291                 << strerror(errno);
292         ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
293                 << strerror(errno);
294         m_filesize = std::max(m_filesize, offs + size);
295
296         free(buf);
297 }
298
299 };
300
301 /*
302  * Extend a file with dirty data in the last page of the last block.
303  *
304  * fsx -WR -P /tmp -S8 -N3 fsx.bin
305  */
306 TEST_P(Io, extend_from_dirty_page)
307 {
308         off_t wofs = 0x21a0;
309         ssize_t wsize = 0xf0a8;
310         off_t rofs = 0xb284;
311         ssize_t rsize = 0x9b22;
312         off_t truncsize = 0x28702;
313
314         do_write(wsize, wofs);
315         do_ftruncate(truncsize);
316         do_read(rsize, rofs);
317 }
318
319 /*
320  * mapwrite into a newly extended part of a file.
321  *
322  * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin
323  */
324 TEST_P(Io, extend_by_mapwrite)
325 {
326         do_mapwrite(0x849e, 0x29a3a);   /* [0x29a3a, 0x31ed7] */
327         do_mapwrite(0x3994, 0x3c7d8);   /* [0x3c7d8, 0x4016b] */
328         do_read(0xf556, 0x30c16);       /* [0x30c16, 0x4016b] */
329 }
330
331 /*
332  * When writing the last page of a file, it must be written synchronously.
333  * Otherwise the cached page can become invalid by a subsequent extend
334  * operation.
335  *
336  * fsx -WR -P /tmp -S642 -N3 fsx.bin
337  */
338 TEST_P(Io, last_page)
339 {
340         do_write(0xcc77, 0x1134f);      /* [0x1134f, 0x1dfc5] */
341         do_write(0xdfa7, 0x2096a);      /* [0x2096a, 0x2e910] */
342         do_read(0xb5b7, 0x1a3aa);       /* [0x1a3aa, 0x25960] */
343 }
344
345 /*
346  * Read a hole using mmap
347  *
348  * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp  -S14 fsx.bin
349  */
350 TEST_P(Io, mapread_hole)
351 {
352         do_write(0x123b7, 0xf205);      /* [0xf205, 0x215bb] */
353         do_mapread(0xeeea, 0x2f4c);     /* [0x2f4c, 0x11e35] */
354 }
355
356 /* 
357  * Read a hole from a block that contains some cached data.
358  *
359  * fsx -WR -P /tmp -S55  fsx.bin
360  */
361 TEST_P(Io, read_hole_from_cached_block)
362 {
363         off_t wofs = 0x160c5;
364         ssize_t wsize = 0xa996;
365         off_t rofs = 0x472e;
366         ssize_t rsize = 0xd8d5;
367
368         do_write(wsize, wofs);
369         do_read(rsize, rofs);
370 }
371
372 /*
373  * Truncating a file into a dirty buffer should not causing anything untoward
374  * to happen when that buffer is eventually flushed.
375  *
376  * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
377  */
378 TEST_P(Io, truncate_into_dirty_buffer)
379 {
380         off_t wofs0 = 0x3bad7;
381         ssize_t wsize0 = 0x4529;
382         off_t wofs1 = 0xc30d;
383         ssize_t wsize1 = 0x5f77;
384         off_t truncsize0 = 0x10916;
385         off_t rofs = 0xdf17;
386         ssize_t rsize = 0x29ff;
387         off_t truncsize1 = 0x152b4;
388
389         do_write(wsize0, wofs0);
390         do_write(wsize1, wofs1);
391         do_ftruncate(truncsize0);
392         do_read(rsize, rofs);
393         do_ftruncate(truncsize1);
394         close(m_test_fd);
395 }
396
397 /*
398  * Truncating a file into a dirty buffer should not causing anything untoward
399  * to happen when that buffer is eventually flushed, even when the buffer's
400  * dirty_off is > 0.
401  *
402  * Based on this command with a few steps removed:
403  * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
404  */
405 TEST_P(Io, truncate_into_dirty_buffer2)
406 {
407         off_t truncsize0 = 0x344f3;
408         off_t wofs = 0x2790c;
409         ssize_t wsize = 0xd86a;
410         off_t truncsize1 = 0x2de38;
411         off_t rofs2 = 0x1fd7a;
412         ssize_t rsize2 = 0xc594;
413         off_t truncsize2 = 0x31e71;
414
415         /* Sets the file size to something larger than the next write */
416         do_ftruncate(truncsize0);
417         /* 
418          * Creates a dirty buffer.  The part in lbn 2 doesn't flush
419          * synchronously.
420          */
421         do_write(wsize, wofs);
422         /* Truncates part of the dirty buffer created in step 2 */
423         do_ftruncate(truncsize1);
424         /* XXX ?I don't know why this is necessary? */
425         do_read(rsize2, rofs2);
426         /* Truncates the dirty buffer */
427         do_ftruncate(truncsize2);
428         close(m_test_fd);
429 }
430
431 /*
432  * Regression test for a bug introduced in r348931
433  *
434  * Sequence of operations:
435  * 1) The first write reads lbn so it can modify it
436  * 2) The first write flushes lbn 3 immediately because it's the end of file
437  * 3) The first write then flushes lbn 4 because it's the end of the file
438  * 4) The second write modifies the cached versions of lbn 3 and 4
439  * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's
440  *    extending the buffer.  Then it flushes lbn 4 because B_DELWRI was set but
441  *    B_CACHE was clear.
442  * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the
443  *    buffer into a weird write-only state.  All read operations would return
444  *    0.  Writes were apparently still processed, because the buffer's contents
445  *    were correct when examined in a core dump.
446  * 7) The third write reads lbn 4 because cache is clear
447  * 9) uiomove dutifully copies new data into the buffer
448  * 10) The buffer's dirty is flushed to lbn 4
449  * 11) The read returns all zeros because of step 6.
450  *
451  * Based on:
452  * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q  fsx.bin
453  */
454 TEST_P(Io, resize_a_valid_buffer_while_extending)
455 {
456         do_write(0x14530, 0x36ee6);     /* [0x36ee6, 0x4b415] */
457         do_write(0x1507c, 0x33256);     /* [0x33256, 0x482d1] */
458         do_write(0x175c, 0x4c03d);      /* [0x4c03d, 0x4d798] */
459         do_read(0xe277, 0x3599c);       /* [0x3599c, 0x43c12] */
460         close(m_test_fd);
461 }
462
463 INSTANTIATE_TEST_CASE_P(Io, Io,
464         Combine(Values(0, FUSE_ASYNC_READ),             /* m_init_flags */
465                 Values(0x1000, 0x10000, 0x20000),       /* m_maxwrite */
466                 Bool()));                               /* m_async */