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