1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Alan Somers 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 extern "C" { 29 #include <sys/param.h> 30 #include <sys/mount.h> 31 #include <sys/resource.h> 32 #include <sys/time.h> 33 34 #include <fcntl.h> 35 #include <signal.h> 36 #include <unistd.h> 37 38 #include "mntopts.h" // for build_iovec 39 } 40 41 #include "mockfs.hh" 42 #include "utils.hh" 43 44 using namespace testing; 45 46 /* Is buf all zero? */ 47 static bool 48 is_zero(const char *buf, uint64_t size) 49 { 50 return buf[0] == 0 && !memcmp(buf, buf + 1, size - 1); 51 } 52 53 class Fallocate: public FuseTest { 54 public: 55 /* 56 * expect VOP_DEALLOCATE to be implemented by vop_stddeallocate. 57 */ 58 void expect_vop_stddeallocate(uint64_t ino, uint64_t off, uint64_t length) 59 { 60 /* XXX read offset and size may depend on cache mode */ 61 EXPECT_CALL(*m_mock, process( 62 ResultOf([=](auto in) { 63 return (in.header.opcode == FUSE_READ && 64 in.header.nodeid == ino && 65 in.body.read.offset <= off && 66 in.body.read.offset + in.body.read.size >= 67 off + length); 68 }, Eq(true)), 69 _) 70 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto& out) { 71 assert(in.body.read.size <= sizeof(out.body.bytes)); 72 out.header.len = sizeof(struct fuse_out_header) + 73 in.body.read.size; 74 memset(out.body.bytes, 'X', in.body.read.size); 75 }))).RetiresOnSaturation(); 76 EXPECT_CALL(*m_mock, process( 77 ResultOf([=](auto in) { 78 const char *buf = (const char*)in.body.bytes + 79 sizeof(struct fuse_write_in); 80 81 assert(length <= sizeof(in.body.bytes) - 82 sizeof(struct fuse_write_in)); 83 return (in.header.opcode == FUSE_WRITE && 84 in.header.nodeid == ino && 85 in.body.write.offset == off && 86 in.body.write.size == length && 87 is_zero(buf, length)); 88 }, Eq(true)), 89 _) 90 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 91 SET_OUT_HEADER_LEN(out, write); 92 out.body.write.size = length; 93 }))); 94 } 95 }; 96 97 class Fspacectl: public Fallocate {}; 98 99 class Fspacectl_7_18: public Fspacectl { 100 public: 101 virtual void SetUp() { 102 m_kernel_minor_version = 18; 103 Fspacectl::SetUp(); 104 } 105 }; 106 107 class FspacectlCache: public Fspacectl, public WithParamInterface<cache_mode> { 108 public: 109 bool m_direct_io; 110 111 FspacectlCache(): m_direct_io(false) {}; 112 113 virtual void SetUp() { 114 int cache_mode = GetParam(); 115 switch (cache_mode) { 116 case Uncached: 117 m_direct_io = true; 118 break; 119 case WritebackAsync: 120 m_async = true; 121 /* FALLTHROUGH */ 122 case Writeback: 123 m_init_flags |= FUSE_WRITEBACK_CACHE; 124 /* FALLTHROUGH */ 125 case Writethrough: 126 break; 127 default: 128 FAIL() << "Unknown cache mode"; 129 } 130 131 FuseTest::SetUp(); 132 if (IsSkipped()) 133 return; 134 } 135 }; 136 137 class PosixFallocate: public Fallocate { 138 public: 139 static sig_atomic_t s_sigxfsz; 140 141 void SetUp() { 142 s_sigxfsz = 0; 143 FuseTest::SetUp(); 144 } 145 146 void TearDown() { 147 struct sigaction sa; 148 149 bzero(&sa, sizeof(sa)); 150 sa.sa_handler = SIG_DFL; 151 sigaction(SIGXFSZ, &sa, NULL); 152 153 Fallocate::TearDown(); 154 } 155 156 }; 157 158 sig_atomic_t PosixFallocate::s_sigxfsz = 0; 159 160 void sigxfsz_handler(int __unused sig) { 161 PosixFallocate::s_sigxfsz = 1; 162 } 163 164 class PosixFallocate_7_18: public PosixFallocate { 165 public: 166 virtual void SetUp() { 167 m_kernel_minor_version = 18; 168 PosixFallocate::SetUp(); 169 } 170 }; 171 172 173 /* 174 * If the server returns ENOSYS, it indicates that the server does not support 175 * FUSE_FALLOCATE. This and future calls should fall back to vop_stddeallocate. 176 */ 177 TEST_F(Fspacectl, enosys) 178 { 179 const char FULLPATH[] = "mountpoint/some_file.txt"; 180 const char RELPATH[] = "some_file.txt"; 181 off_t fsize = 1 << 20; 182 off_t off0 = 100; 183 off_t len0 = 500; 184 struct spacectl_range rqsr = { .r_offset = off0, .r_len = len0 }; 185 uint64_t ino = 42; 186 uint64_t off1 = fsize; 187 uint64_t len1 = 1000; 188 off_t off2 = fsize / 2; 189 off_t len2 = 500; 190 int fd; 191 192 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 193 expect_open(ino, 0, 1); 194 expect_fallocate(ino, off0, len0, 195 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, ENOSYS); 196 expect_vop_stddeallocate(ino, off0, len0); 197 expect_vop_stddeallocate(ino, off2, len2); 198 199 fd = open(FULLPATH, O_RDWR); 200 ASSERT_LE(0, fd) << strerror(errno); 201 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 202 203 /* Subsequent calls shouldn't query the daemon either */ 204 rqsr.r_offset = off2; 205 rqsr.r_len = len2; 206 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 207 208 /* Neither should posix_fallocate query the daemon */ 209 EXPECT_EQ(EINVAL, posix_fallocate(fd, off1, len1)); 210 211 leak(fd); 212 } 213 214 /* 215 * EOPNOTSUPP means "the file system does not support fallocate with the 216 * supplied mode on this particular file". So we should fallback, but not 217 * assume anything about whether the operation will fail on a different file or 218 * with a different mode. 219 */ 220 TEST_F(Fspacectl, eopnotsupp) 221 { 222 const char FULLPATH[] = "mountpoint/some_file.txt"; 223 const char RELPATH[] = "some_file.txt"; 224 struct spacectl_range rqsr; 225 uint64_t ino = 42; 226 uint64_t fsize = 1 << 20; 227 uint64_t off0 = 500; 228 uint64_t len = 1000; 229 uint64_t off1 = fsize / 2; 230 int fd; 231 232 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 233 expect_open(ino, 0, 1); 234 expect_fallocate(ino, off0, len, 235 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 236 EOPNOTSUPP); 237 expect_vop_stddeallocate(ino, off0, len); 238 expect_fallocate(ino, off1, len, 239 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 240 EOPNOTSUPP); 241 expect_vop_stddeallocate(ino, off1, len); 242 expect_fallocate(ino, fsize, len, 0, 0); 243 244 fd = open(FULLPATH, O_RDWR); 245 ASSERT_LE(0, fd) << strerror(errno); 246 247 /* 248 * Though the FUSE daemon will reject the call, the kernel should fall 249 * back to a read-modify-write approach. 250 */ 251 rqsr.r_offset = off0; 252 rqsr.r_len = len; 253 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 254 255 /* Subsequent calls should still query the daemon */ 256 rqsr.r_offset = off1; 257 rqsr.r_len = len; 258 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 259 260 /* But subsequent posix_fallocate calls _should_ query the daemon */ 261 EXPECT_EQ(0, posix_fallocate(fd, fsize, len)); 262 263 leak(fd); 264 } 265 266 TEST_F(Fspacectl, erofs) 267 { 268 const char FULLPATH[] = "mountpoint/some_file.txt"; 269 const char RELPATH[] = "some_file.txt"; 270 struct statfs statbuf; 271 uint64_t fsize = 2000; 272 struct spacectl_range rqsr = { .r_offset = 0, .r_len = 1 }; 273 struct iovec *iov = NULL; 274 int iovlen = 0; 275 uint64_t ino = 42; 276 int fd; 277 int newflags; 278 279 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 280 expect_open(ino, 0, 1); 281 EXPECT_CALL(*m_mock, process( 282 ResultOf([](auto in) { 283 return (in.header.opcode == FUSE_STATFS); 284 }, Eq(true)), 285 _) 286 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) 287 { 288 /* 289 * All of the fields except f_flags are don't care, and f_flags 290 * is set by the VFS 291 */ 292 SET_OUT_HEADER_LEN(out, statfs); 293 }))); 294 295 fd = open(FULLPATH, O_RDWR); 296 ASSERT_LE(0, fd) << strerror(errno); 297 298 /* Remount read-only */ 299 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno); 300 newflags = statbuf.f_flags | MNT_UPDATE | MNT_RDONLY; 301 build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1); 302 build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1); 303 build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1); 304 ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno); 305 306 EXPECT_EQ(-1, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 307 EXPECT_EQ(EROFS, errno); 308 309 leak(fd); 310 } 311 312 TEST_F(Fspacectl, ok) 313 { 314 const char FULLPATH[] = "mountpoint/some_file.txt"; 315 const char RELPATH[] = "some_file.txt"; 316 struct spacectl_range rqsr, rmsr; 317 struct stat sb0, sb1; 318 uint64_t ino = 42; 319 uint64_t fsize = 2000; 320 uint64_t offset = 500; 321 uint64_t length = 1000; 322 int fd; 323 324 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 325 expect_open(ino, 0, 1); 326 expect_fallocate(ino, offset, length, 327 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 0); 328 329 fd = open(FULLPATH, O_RDWR); 330 ASSERT_LE(0, fd) << strerror(errno); 331 ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno); 332 rqsr.r_offset = offset; 333 rqsr.r_len = length; 334 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, &rmsr)); 335 EXPECT_EQ(0, rmsr.r_len); 336 EXPECT_EQ((off_t)(offset + length), rmsr.r_offset); 337 338 /* 339 * The file's attributes should not have been invalidated, so this fstat 340 * will not requery the daemon. 341 */ 342 EXPECT_EQ(0, fstat(fd, &sb1)); 343 EXPECT_EQ(fsize, (uint64_t)sb1.st_size); 344 345 /* mtime and ctime should be updated */ 346 EXPECT_EQ(sb0.st_atime, sb1.st_atime); 347 EXPECT_NE(sb0.st_mtime, sb1.st_mtime); 348 EXPECT_NE(sb0.st_ctime, sb1.st_ctime); 349 350 leak(fd); 351 } 352 353 /* The returned rqsr.r_off should be clipped at EoF */ 354 TEST_F(Fspacectl, past_eof) 355 { 356 const char FULLPATH[] = "mountpoint/some_file.txt"; 357 const char RELPATH[] = "some_file.txt"; 358 struct spacectl_range rqsr, rmsr; 359 uint64_t ino = 42; 360 uint64_t fsize = 1000; 361 uint64_t offset = 1500; 362 uint64_t length = 1000; 363 int fd; 364 365 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 366 expect_open(ino, 0, 1); 367 expect_fallocate(ino, offset, length, 368 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 0); 369 370 fd = open(FULLPATH, O_RDWR); 371 ASSERT_LE(0, fd) << strerror(errno); 372 rqsr.r_offset = offset; 373 rqsr.r_len = length; 374 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, &rmsr)); 375 EXPECT_EQ(0, rmsr.r_len); 376 EXPECT_EQ((off_t)fsize, rmsr.r_offset); 377 378 leak(fd); 379 } 380 381 /* The returned rqsr.r_off should be clipped at EoF */ 382 TEST_F(Fspacectl, spans_eof) 383 { 384 const char FULLPATH[] = "mountpoint/some_file.txt"; 385 const char RELPATH[] = "some_file.txt"; 386 struct spacectl_range rqsr, rmsr; 387 uint64_t ino = 42; 388 uint64_t fsize = 1000; 389 uint64_t offset = 500; 390 uint64_t length = 1000; 391 int fd; 392 393 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 394 expect_open(ino, 0, 1); 395 expect_fallocate(ino, offset, length, 396 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 0); 397 398 fd = open(FULLPATH, O_RDWR); 399 ASSERT_LE(0, fd) << strerror(errno); 400 rqsr.r_offset = offset; 401 rqsr.r_len = length; 402 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, &rmsr)); 403 EXPECT_EQ(0, rmsr.r_len); 404 EXPECT_EQ((off_t)fsize, rmsr.r_offset); 405 406 leak(fd); 407 } 408 409 /* 410 * With older servers, no FUSE_FALLOCATE should be attempted. The kernel 411 * should fall back to vop_stddeallocate. 412 */ 413 TEST_F(Fspacectl_7_18, ok) 414 { 415 const char FULLPATH[] = "mountpoint/some_file.txt"; 416 const char RELPATH[] = "some_file.txt"; 417 struct spacectl_range rqsr, rmsr; 418 void *buf; 419 uint64_t ino = 42; 420 uint64_t fsize = 2000; 421 uint64_t offset = 500; 422 uint64_t length = 1000; 423 int fd; 424 425 buf = malloc(length); 426 427 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 428 expect_open(ino, 0, 1); 429 expect_vop_stddeallocate(ino, offset, length); 430 431 fd = open(FULLPATH, O_RDWR); 432 ASSERT_LE(0, fd) << strerror(errno); 433 rqsr.r_offset = offset; 434 rqsr.r_len = length; 435 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, &rmsr)); 436 EXPECT_EQ(0, rmsr.r_len); 437 EXPECT_EQ((off_t)(offset + length), rmsr.r_offset); 438 439 leak(fd); 440 free(buf); 441 } 442 443 /* 444 * A successful fspacectl should clear the zeroed data from the kernel cache. 445 */ 446 TEST_P(FspacectlCache, clears_cache) 447 { 448 const char FULLPATH[] = "mountpoint/some_file.txt"; 449 const char RELPATH[] = "some_file.txt"; 450 const char *CONTENTS = "abcdefghijklmnopqrstuvwxyz"; 451 struct spacectl_range rqsr, rmsr; 452 uint64_t ino = 42; 453 ssize_t bufsize = strlen(CONTENTS); 454 uint64_t fsize = bufsize; 455 uint8_t buf[bufsize]; 456 char zbuf[bufsize]; 457 uint64_t offset = 0; 458 uint64_t length = bufsize; 459 int fd; 460 461 bzero(zbuf, bufsize); 462 463 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 464 expect_open(ino, 0, 1); 465 /* NB: expectations are applied in LIFO order */ 466 expect_read(ino, 0, fsize, fsize, zbuf); 467 expect_read(ino, 0, fsize, fsize, CONTENTS); 468 expect_fallocate(ino, offset, length, 469 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 0); 470 471 fd = open(FULLPATH, O_RDWR); 472 ASSERT_LE(0, fd) << strerror(errno); 473 474 /* Populate the cache */ 475 ASSERT_EQ(fsize, (uint64_t)pread(fd, buf, bufsize, 0)) 476 << strerror(errno); 477 ASSERT_EQ(0, memcmp(buf, CONTENTS, fsize)); 478 479 /* Zero the file */ 480 rqsr.r_offset = offset; 481 rqsr.r_len = length; 482 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, &rmsr)); 483 EXPECT_EQ(0, rmsr.r_len); 484 EXPECT_EQ((off_t)(offset + length), rmsr.r_offset); 485 486 /* Read again. This should query the daemon */ 487 ASSERT_EQ(fsize, (uint64_t)pread(fd, buf, bufsize, 0)) 488 << strerror(errno); 489 ASSERT_EQ(0, memcmp(buf, zbuf, fsize)); 490 491 leak(fd); 492 } 493 494 INSTANTIATE_TEST_SUITE_P(FspacectlCache, FspacectlCache, 495 Values(Uncached, Writethrough, Writeback) 496 ); 497 498 /* 499 * If the server returns ENOSYS, it indicates that the server does not support 500 * FUSE_FALLOCATE. This and future calls should return EINVAL. 501 */ 502 TEST_F(PosixFallocate, enosys) 503 { 504 const char FULLPATH[] = "mountpoint/some_file.txt"; 505 const char RELPATH[] = "some_file.txt"; 506 uint64_t ino = 42; 507 uint64_t off0 = 0; 508 uint64_t len0 = 1000; 509 off_t off1 = 100; 510 off_t len1 = 200; 511 uint64_t fsize = 500; 512 struct spacectl_range rqsr = { .r_offset = off1, .r_len = len1 }; 513 int fd; 514 515 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 516 expect_open(ino, 0, 1); 517 expect_fallocate(ino, off0, len0, 0, ENOSYS); 518 expect_vop_stddeallocate(ino, off1, len1); 519 520 fd = open(FULLPATH, O_RDWR); 521 ASSERT_LE(0, fd) << strerror(errno); 522 EXPECT_EQ(EINVAL, posix_fallocate(fd, off0, len0)); 523 524 /* Subsequent calls shouldn't query the daemon*/ 525 EXPECT_EQ(EINVAL, posix_fallocate(fd, off0, len0)); 526 527 /* Neither should VOP_DEALLOCATE query the daemon */ 528 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 529 530 leak(fd); 531 } 532 533 /* 534 * EOPNOTSUPP means "the file system does not support fallocate with the 535 * supplied mode on this particular file". So we should fallback, but not 536 * assume anything about whether the operation will fail on a different file or 537 * with a different mode. 538 */ 539 TEST_F(PosixFallocate, eopnotsupp) 540 { 541 const char FULLPATH[] = "mountpoint/some_file.txt"; 542 const char RELPATH[] = "some_file.txt"; 543 struct spacectl_range rqsr; 544 uint64_t ino = 42; 545 uint64_t fsize = 2000; 546 uint64_t offset = 0; 547 uint64_t length = 1000; 548 int fd; 549 550 expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1); 551 expect_open(ino, 0, 1); 552 expect_fallocate(ino, fsize, length, 0, EOPNOTSUPP); 553 expect_fallocate(ino, offset, length, 0, EOPNOTSUPP); 554 expect_fallocate(ino, offset, length, 555 FUSE_FALLOC_FL_KEEP_SIZE | FUSE_FALLOC_FL_PUNCH_HOLE, 0); 556 557 fd = open(FULLPATH, O_RDWR); 558 ASSERT_LE(0, fd) << strerror(errno); 559 EXPECT_EQ(EINVAL, posix_fallocate(fd, fsize, length)); 560 561 /* Subsequent calls should still query the daemon*/ 562 EXPECT_EQ(EINVAL, posix_fallocate(fd, offset, length)); 563 564 /* And subsequent VOP_DEALLOCATE calls should also query the daemon */ 565 rqsr.r_len = length; 566 rqsr.r_offset = offset; 567 EXPECT_EQ(0, fspacectl(fd, SPACECTL_DEALLOC, &rqsr, 0, NULL)); 568 569 leak(fd); 570 } 571 572 /* EIO is not a permanent error, and may be retried */ 573 TEST_F(PosixFallocate, eio) 574 { 575 const char FULLPATH[] = "mountpoint/some_file.txt"; 576 const char RELPATH[] = "some_file.txt"; 577 uint64_t ino = 42; 578 uint64_t offset = 0; 579 uint64_t length = 1000; 580 int fd; 581 582 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); 583 expect_open(ino, 0, 1); 584 expect_fallocate(ino, offset, length, 0, EIO); 585 586 fd = open(FULLPATH, O_RDWR); 587 ASSERT_LE(0, fd) << strerror(errno); 588 EXPECT_EQ(EIO, posix_fallocate(fd, offset, length)); 589 590 expect_fallocate(ino, offset, length, 0, 0); 591 592 EXPECT_EQ(0, posix_fallocate(fd, offset, length)); 593 594 leak(fd); 595 } 596 597 TEST_F(PosixFallocate, erofs) 598 { 599 const char FULLPATH[] = "mountpoint/some_file.txt"; 600 const char RELPATH[] = "some_file.txt"; 601 struct statfs statbuf; 602 struct iovec *iov = NULL; 603 int iovlen = 0; 604 uint64_t ino = 42; 605 uint64_t offset = 0; 606 uint64_t length = 1000; 607 int fd; 608 int newflags; 609 610 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); 611 expect_open(ino, 0, 1); 612 EXPECT_CALL(*m_mock, process( 613 ResultOf([](auto in) { 614 return (in.header.opcode == FUSE_STATFS); 615 }, Eq(true)), 616 _) 617 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) 618 { 619 /* 620 * All of the fields except f_flags are don't care, and f_flags 621 * is set by the VFS 622 */ 623 SET_OUT_HEADER_LEN(out, statfs); 624 }))); 625 626 fd = open(FULLPATH, O_RDWR); 627 ASSERT_LE(0, fd) << strerror(errno); 628 629 /* Remount read-only */ 630 ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno); 631 newflags = statbuf.f_flags | MNT_UPDATE | MNT_RDONLY; 632 build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1); 633 build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1); 634 build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1); 635 ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno); 636 637 EXPECT_EQ(EROFS, posix_fallocate(fd, offset, length)); 638 639 leak(fd); 640 } 641 642 TEST_F(PosixFallocate, ok) 643 { 644 const char FULLPATH[] = "mountpoint/some_file.txt"; 645 const char RELPATH[] = "some_file.txt"; 646 struct stat sb0, sb1; 647 uint64_t ino = 42; 648 uint64_t offset = 0; 649 uint64_t length = 1000; 650 int fd; 651 652 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 653 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 654 SET_OUT_HEADER_LEN(out, entry); 655 out.body.entry.attr.mode = S_IFREG | 0644; 656 out.body.entry.nodeid = ino; 657 out.body.entry.entry_valid = UINT64_MAX; 658 out.body.entry.attr_valid = UINT64_MAX; 659 }))); 660 expect_open(ino, 0, 1); 661 expect_fallocate(ino, offset, length, 0, 0); 662 663 fd = open(FULLPATH, O_RDWR); 664 ASSERT_LE(0, fd) << strerror(errno); 665 ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno); 666 EXPECT_EQ(0, posix_fallocate(fd, offset, length)); 667 /* 668 * Despite the originally cached file size of zero, stat should now 669 * return either the new size or requery the daemon. 670 */ 671 EXPECT_EQ(0, stat(FULLPATH, &sb1)); 672 EXPECT_EQ(length, (uint64_t)sb1.st_size); 673 674 /* mtime and ctime should be updated */ 675 EXPECT_EQ(sb0.st_atime, sb1.st_atime); 676 EXPECT_NE(sb0.st_mtime, sb1.st_mtime); 677 EXPECT_NE(sb0.st_ctime, sb1.st_ctime); 678 679 leak(fd); 680 } 681 682 /* fusefs should respect RLIMIT_FSIZE */ 683 TEST_F(PosixFallocate, rlimit_fsize) 684 { 685 const char FULLPATH[] = "mountpoint/some_file.txt"; 686 const char RELPATH[] = "some_file.txt"; 687 struct rlimit rl; 688 uint64_t ino = 42; 689 uint64_t offset = 0; 690 uint64_t length = 1'000'000; 691 int fd; 692 693 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); 694 expect_open(ino, 0, 1); 695 696 rl.rlim_cur = length / 2; 697 rl.rlim_max = 10 * length; 698 ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rl)) << strerror(errno); 699 ASSERT_NE(SIG_ERR, signal(SIGXFSZ, sigxfsz_handler)) << strerror(errno); 700 701 fd = open(FULLPATH, O_RDWR); 702 ASSERT_LE(0, fd) << strerror(errno); 703 EXPECT_EQ(EFBIG, posix_fallocate(fd, offset, length)); 704 EXPECT_EQ(1, s_sigxfsz); 705 706 leak(fd); 707 } 708 709 /* With older servers, no FUSE_FALLOCATE should be attempted */ 710 TEST_F(PosixFallocate_7_18, einval) 711 { 712 const char FULLPATH[] = "mountpoint/some_file.txt"; 713 const char RELPATH[] = "some_file.txt"; 714 uint64_t ino = 42; 715 uint64_t offset = 0; 716 uint64_t length = 1000; 717 int fd; 718 719 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); 720 expect_open(ino, 0, 1); 721 722 fd = open(FULLPATH, O_RDWR); 723 ASSERT_LE(0, fd) << strerror(errno); 724 EXPECT_EQ(EINVAL, posix_fallocate(fd, offset, length)); 725 726 leak(fd); 727 } 728