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