2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
40 #include <semaphore.h>
47 using namespace testing;
49 class Read: public FuseTest {
52 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
54 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
58 class Read_7_8: public FuseTest {
60 virtual void SetUp() {
61 m_kernel_minor_version = 8;
65 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
67 FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
71 class AioRead: public Read {
73 virtual void SetUp() {
74 const char *node = "vfs.aio.enable_unsafe";
76 size_t size = sizeof(val);
80 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
84 "vfs.aio.enable_unsafe must be set for this test";
88 class AsyncRead: public AioRead {
89 virtual void SetUp() {
90 m_init_flags = FUSE_ASYNC_READ;
95 class ReadCacheable: public Read {
97 virtual void SetUp() {
98 const char *node = "vfs.fusefs.data_cache_mode";
100 size_t size = sizeof(val);
104 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
108 "fusefs data caching must be enabled for this test";
112 class ReadAhead: public ReadCacheable,
113 public WithParamInterface<tuple<bool, uint32_t>>
115 virtual void SetUp() {
116 m_maxreadahead = get<1>(GetParam());
117 m_noclusterr = get<0>(GetParam());
118 ReadCacheable::SetUp();
122 /* AIO reads need to set the header's pid field correctly */
123 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
124 TEST_F(AioRead, aio_read)
126 const char FULLPATH[] = "mountpoint/some_file.txt";
127 const char RELPATH[] = "some_file.txt";
128 const char *CONTENTS = "abcdefgh";
131 ssize_t bufsize = strlen(CONTENTS);
133 struct aiocb iocb, *piocb;
135 expect_lookup(RELPATH, ino, bufsize);
136 expect_open(ino, 0, 1);
137 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
139 fd = open(FULLPATH, O_RDONLY);
140 ASSERT_LE(0, fd) << strerror(errno);
142 iocb.aio_nbytes = bufsize;
143 iocb.aio_fildes = fd;
146 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
147 ASSERT_EQ(0, aio_read(&iocb)) << strerror(errno);
148 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
149 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
150 /* Deliberately leak fd. close(2) will be tested in release.cc */
154 * Without the FUSE_ASYNC_READ mount option, fuse(4) should ensure that there
155 * is at most one outstanding read operation per file handle
157 TEST_F(AioRead, async_read_disabled)
159 const char FULLPATH[] = "mountpoint/some_file.txt";
160 const char RELPATH[] = "some_file.txt";
163 ssize_t bufsize = 50;
164 char buf0[bufsize], buf1[bufsize];
167 struct aiocb iocb0, iocb1;
168 volatile sig_atomic_t read_count = 0;
170 expect_lookup(RELPATH, ino, 131072);
171 expect_open(ino, 0, 1);
172 EXPECT_CALL(*m_mock, process(
173 ResultOf([=](auto in) {
174 return (in.header.opcode == FUSE_READ &&
175 in.header.nodeid == ino &&
176 in.body.read.fh == FH &&
177 in.body.read.offset == (uint64_t)off0);
180 ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
182 /* Filesystem is slow to respond */
184 EXPECT_CALL(*m_mock, process(
185 ResultOf([=](auto in) {
186 return (in.header.opcode == FUSE_READ &&
187 in.header.nodeid == ino &&
188 in.body.read.fh == FH &&
189 in.body.read.offset == (uint64_t)off1);
192 ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
194 /* Filesystem is slow to respond */
197 fd = open(FULLPATH, O_RDONLY);
198 ASSERT_LE(0, fd) << strerror(errno);
201 * Submit two AIO read requests, and respond to neither. If the
202 * filesystem ever gets the second read request, then we failed to
203 * limit outstanding reads.
205 iocb0.aio_nbytes = bufsize;
206 iocb0.aio_fildes = fd;
207 iocb0.aio_buf = buf0;
208 iocb0.aio_offset = off0;
209 iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
210 ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
212 iocb1.aio_nbytes = bufsize;
213 iocb1.aio_fildes = fd;
214 iocb1.aio_buf = buf1;
215 iocb1.aio_offset = off1;
216 iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
217 ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
220 * Sleep for awhile to make sure the kernel has had a chance to issue
221 * the second read, even though the first has not yet returned
224 EXPECT_EQ(read_count, 1);
226 m_mock->kill_daemon();
227 /* Wait for AIO activity to complete, but ignore errors */
228 (void)aio_waitcomplete(NULL, NULL);
230 /* Deliberately leak fd. close(2) will be tested in release.cc */
234 * With the FUSE_ASYNC_READ mount option, fuse(4) may issue multiple
235 * simultaneous read requests on the same file handle.
237 TEST_F(AsyncRead, async_read)
239 const char FULLPATH[] = "mountpoint/some_file.txt";
240 const char RELPATH[] = "some_file.txt";
243 ssize_t bufsize = 50;
244 char buf0[bufsize], buf1[bufsize];
247 struct aiocb iocb0, iocb1;
250 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
252 expect_lookup(RELPATH, ino, 131072);
253 expect_open(ino, 0, 1);
254 EXPECT_CALL(*m_mock, process(
255 ResultOf([=](auto in) {
256 return (in.header.opcode == FUSE_READ &&
257 in.header.nodeid == ino &&
258 in.body.read.fh == FH &&
259 in.body.read.offset == (uint64_t)off0);
262 ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
264 /* Filesystem is slow to respond */
266 EXPECT_CALL(*m_mock, process(
267 ResultOf([=](auto in) {
268 return (in.header.opcode == FUSE_READ &&
269 in.header.nodeid == ino &&
270 in.body.read.fh == FH &&
271 in.body.read.offset == (uint64_t)off1);
274 ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
276 /* Filesystem is slow to respond */
279 fd = open(FULLPATH, O_RDONLY);
280 ASSERT_LE(0, fd) << strerror(errno);
283 * Submit two AIO read requests, but respond to neither. Ensure that
286 iocb0.aio_nbytes = bufsize;
287 iocb0.aio_fildes = fd;
288 iocb0.aio_buf = buf0;
289 iocb0.aio_offset = off0;
290 iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
291 ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
293 iocb1.aio_nbytes = bufsize;
294 iocb1.aio_fildes = fd;
295 iocb1.aio_buf = buf1;
296 iocb1.aio_offset = off1;
297 iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
298 ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
300 /* Wait until both reads have reached the daemon */
301 ASSERT_EQ(0, sem_wait(&sem)) << strerror(errno);
302 ASSERT_EQ(0, sem_wait(&sem)) << strerror(errno);
304 m_mock->kill_daemon();
305 /* Wait for AIO activity to complete, but ignore errors */
306 (void)aio_waitcomplete(NULL, NULL);
308 /* Deliberately leak fd. close(2) will be tested in release.cc */
311 /* 0-length reads shouldn't cause any confusion */
312 TEST_F(Read, direct_io_read_nothing)
314 const char FULLPATH[] = "mountpoint/some_file.txt";
315 const char RELPATH[] = "some_file.txt";
318 uint64_t offset = 100;
321 expect_lookup(RELPATH, ino, offset + 1000);
322 expect_open(ino, FOPEN_DIRECT_IO, 1);
324 fd = open(FULLPATH, O_RDONLY);
325 ASSERT_LE(0, fd) << strerror(errno);
327 ASSERT_EQ(0, pread(fd, buf, 0, offset)) << strerror(errno);
328 /* Deliberately leak fd. close(2) will be tested in release.cc */
332 * With direct_io, reads should not fill the cache. They should go straight to
335 TEST_F(Read, direct_io_pread)
337 const char FULLPATH[] = "mountpoint/some_file.txt";
338 const char RELPATH[] = "some_file.txt";
339 const char *CONTENTS = "abcdefgh";
342 uint64_t offset = 100;
343 ssize_t bufsize = strlen(CONTENTS);
346 expect_lookup(RELPATH, ino, offset + bufsize);
347 expect_open(ino, FOPEN_DIRECT_IO, 1);
348 expect_read(ino, offset, bufsize, bufsize, CONTENTS);
350 fd = open(FULLPATH, O_RDONLY);
351 ASSERT_LE(0, fd) << strerror(errno);
353 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
354 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
355 /* Deliberately leak fd. close(2) will be tested in release.cc */
359 * With direct_io, filesystems are allowed to return less data than is
360 * requested. fuse(4) should return a short read to userland.
362 TEST_F(Read, direct_io_short_read)
364 const char FULLPATH[] = "mountpoint/some_file.txt";
365 const char RELPATH[] = "some_file.txt";
366 const char *CONTENTS = "abcdefghijklmnop";
369 uint64_t offset = 100;
370 ssize_t bufsize = strlen(CONTENTS);
371 ssize_t halfbufsize = bufsize / 2;
374 expect_lookup(RELPATH, ino, offset + bufsize);
375 expect_open(ino, FOPEN_DIRECT_IO, 1);
376 expect_read(ino, offset, bufsize, halfbufsize, CONTENTS);
378 fd = open(FULLPATH, O_RDONLY);
379 ASSERT_LE(0, fd) << strerror(errno);
381 ASSERT_EQ(halfbufsize, pread(fd, buf, bufsize, offset))
383 ASSERT_EQ(0, memcmp(buf, CONTENTS, halfbufsize));
384 /* Deliberately leak fd. close(2) will be tested in release.cc */
389 const char FULLPATH[] = "mountpoint/some_file.txt";
390 const char RELPATH[] = "some_file.txt";
391 const char *CONTENTS = "abcdefgh";
394 ssize_t bufsize = strlen(CONTENTS);
397 expect_lookup(RELPATH, ino, bufsize);
398 expect_open(ino, 0, 1);
399 EXPECT_CALL(*m_mock, process(
400 ResultOf([=](auto in) {
401 return (in.header.opcode == FUSE_READ);
404 ).WillOnce(Invoke(ReturnErrno(EIO)));
406 fd = open(FULLPATH, O_RDONLY);
407 ASSERT_LE(0, fd) << strerror(errno);
409 ASSERT_EQ(-1, read(fd, buf, bufsize)) << strerror(errno);
410 ASSERT_EQ(EIO, errno);
411 /* Deliberately leak fd. close(2) will be tested in release.cc */
415 * With the keep_cache option, the kernel may keep its read cache across
418 TEST_F(ReadCacheable, keep_cache)
420 const char FULLPATH[] = "mountpoint/some_file.txt";
421 const char RELPATH[] = "some_file.txt";
422 const char *CONTENTS = "abcdefgh";
425 ssize_t bufsize = strlen(CONTENTS);
428 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
429 expect_open(ino, FOPEN_KEEP_CACHE, 2);
430 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
432 fd0 = open(FULLPATH, O_RDONLY);
433 ASSERT_LE(0, fd0) << strerror(errno);
434 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
436 fd1 = open(FULLPATH, O_RDWR);
437 ASSERT_LE(0, fd1) << strerror(errno);
440 * This read should be serviced by cache, even though it's on the other
443 ASSERT_EQ(bufsize, read(fd1, buf, bufsize)) << strerror(errno);
445 /* Deliberately leak fd0 and fd1. */
449 * Without the keep_cache option, the kernel should drop its read caches on
452 TEST_F(Read, keep_cache_disabled)
454 const char FULLPATH[] = "mountpoint/some_file.txt";
455 const char RELPATH[] = "some_file.txt";
456 const char *CONTENTS = "abcdefgh";
459 ssize_t bufsize = strlen(CONTENTS);
462 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
463 expect_open(ino, 0, 2);
464 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
466 fd0 = open(FULLPATH, O_RDONLY);
467 ASSERT_LE(0, fd0) << strerror(errno);
468 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
470 fd1 = open(FULLPATH, O_RDWR);
471 ASSERT_LE(0, fd1) << strerror(errno);
474 * This read should not be serviced by cache, even though it's on the
475 * original file descriptor
477 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
478 ASSERT_EQ(0, lseek(fd0, 0, SEEK_SET)) << strerror(errno);
479 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
481 /* Deliberately leak fd0 and fd1. */
484 TEST_F(ReadCacheable, mmap)
486 const char FULLPATH[] = "mountpoint/some_file.txt";
487 const char RELPATH[] = "some_file.txt";
488 const char *CONTENTS = "abcdefgh";
492 size_t bufsize = strlen(CONTENTS);
497 expect_lookup(RELPATH, ino, bufsize);
498 expect_open(ino, 0, 1);
499 /* mmap may legitimately try to read more data than is available */
500 EXPECT_CALL(*m_mock, process(
501 ResultOf([=](auto in) {
502 return (in.header.opcode == FUSE_READ &&
503 in.header.nodeid == ino &&
504 in.body.read.fh == Read::FH &&
505 in.body.read.offset == 0 &&
506 in.body.read.size >= bufsize);
509 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
510 out.header.len = sizeof(struct fuse_out_header) + bufsize;
511 memmove(out.body.bytes, CONTENTS, bufsize);
514 fd = open(FULLPATH, O_RDONLY);
515 ASSERT_LE(0, fd) << strerror(errno);
517 p = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
518 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
520 ASSERT_EQ(0, memcmp(p, CONTENTS, bufsize));
522 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
523 /* Deliberately leak fd. close(2) will be tested in release.cc */
527 * Just as when FOPEN_DIRECT_IO is used, reads with O_DIRECT should bypass
528 * cache and to straight to the daemon
530 TEST_F(Read, o_direct)
532 const char FULLPATH[] = "mountpoint/some_file.txt";
533 const char RELPATH[] = "some_file.txt";
534 const char *CONTENTS = "abcdefgh";
537 ssize_t bufsize = strlen(CONTENTS);
540 expect_lookup(RELPATH, ino, bufsize);
541 expect_open(ino, 0, 1);
542 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
544 fd = open(FULLPATH, O_RDONLY);
545 ASSERT_LE(0, fd) << strerror(errno);
548 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
549 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
551 // Reads with o_direct should bypass the cache
552 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
553 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
554 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
555 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
556 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
558 /* Deliberately leak fd. close(2) will be tested in release.cc */
563 const char FULLPATH[] = "mountpoint/some_file.txt";
564 const char RELPATH[] = "some_file.txt";
565 const char *CONTENTS = "abcdefgh";
569 * Set offset to a maxbcachebuf boundary so we'll be sure what offset
570 * to read from. Without this, the read might start at a lower offset.
572 uint64_t offset = m_maxbcachebuf;
573 ssize_t bufsize = strlen(CONTENTS);
576 expect_lookup(RELPATH, ino, offset + bufsize);
577 expect_open(ino, 0, 1);
578 expect_read(ino, offset, bufsize, bufsize, CONTENTS);
580 fd = open(FULLPATH, O_RDONLY);
581 ASSERT_LE(0, fd) << strerror(errno);
583 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
584 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
585 /* Deliberately leak fd. close(2) will be tested in release.cc */
590 const char FULLPATH[] = "mountpoint/some_file.txt";
591 const char RELPATH[] = "some_file.txt";
592 const char *CONTENTS = "abcdefgh";
595 ssize_t bufsize = strlen(CONTENTS);
598 expect_lookup(RELPATH, ino, bufsize);
599 expect_open(ino, 0, 1);
600 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
602 fd = open(FULLPATH, O_RDONLY);
603 ASSERT_LE(0, fd) << strerror(errno);
605 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
606 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
608 /* Deliberately leak fd. close(2) will be tested in release.cc */
611 TEST_F(Read_7_8, read)
613 const char FULLPATH[] = "mountpoint/some_file.txt";
614 const char RELPATH[] = "some_file.txt";
615 const char *CONTENTS = "abcdefgh";
618 ssize_t bufsize = strlen(CONTENTS);
621 expect_lookup(RELPATH, ino, bufsize);
622 expect_open(ino, 0, 1);
623 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
625 fd = open(FULLPATH, O_RDONLY);
626 ASSERT_LE(0, fd) << strerror(errno);
628 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
629 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
631 /* Deliberately leak fd. close(2) will be tested in release.cc */
635 * If cacheing is enabled, the kernel should try to read an entire cache block
638 TEST_F(ReadCacheable, cache_block)
640 const char FULLPATH[] = "mountpoint/some_file.txt";
641 const char RELPATH[] = "some_file.txt";
642 const char *CONTENTS0 = "abcdefghijklmnop";
646 ssize_t filesize = m_maxbcachebuf * 2;
649 const char *contents1 = CONTENTS0 + bufsize;
651 contents = (char*)calloc(1, filesize);
652 ASSERT_NE(NULL, contents);
653 memmove(contents, CONTENTS0, strlen(CONTENTS0));
655 expect_lookup(RELPATH, ino, filesize);
656 expect_open(ino, 0, 1);
657 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf,
660 fd = open(FULLPATH, O_RDONLY);
661 ASSERT_LE(0, fd) << strerror(errno);
663 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
664 ASSERT_EQ(0, memcmp(buf, CONTENTS0, bufsize));
666 /* A subsequent read should be serviced by cache */
667 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
668 ASSERT_EQ(0, memcmp(buf, contents1, bufsize));
669 /* Deliberately leak fd. close(2) will be tested in release.cc */
672 /* Reading with sendfile should work (though it obviously won't be 0-copy) */
673 TEST_F(ReadCacheable, sendfile)
675 const char FULLPATH[] = "mountpoint/some_file.txt";
676 const char RELPATH[] = "some_file.txt";
677 const char *CONTENTS = "abcdefgh";
680 size_t bufsize = strlen(CONTENTS);
685 expect_lookup(RELPATH, ino, bufsize);
686 expect_open(ino, 0, 1);
687 /* Like mmap, sendfile may request more data than is available */
688 EXPECT_CALL(*m_mock, process(
689 ResultOf([=](auto in) {
690 return (in.header.opcode == FUSE_READ &&
691 in.header.nodeid == ino &&
692 in.body.read.fh == Read::FH &&
693 in.body.read.offset == 0 &&
694 in.body.read.size >= bufsize);
697 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
698 out.header.len = sizeof(struct fuse_out_header) + bufsize;
699 memmove(out.body.bytes, CONTENTS, bufsize);
702 ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
704 fd = open(FULLPATH, O_RDONLY);
705 ASSERT_LE(0, fd) << strerror(errno);
707 ASSERT_EQ(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0))
709 ASSERT_EQ(static_cast<ssize_t>(bufsize), read(sp[0], buf, bufsize))
711 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
715 /* Deliberately leak fd. close(2) will be tested in release.cc */
718 /* sendfile should fail gracefully if fuse declines the read */
719 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236466 */
720 TEST_F(ReadCacheable, DISABLED_sendfile_eio)
722 const char FULLPATH[] = "mountpoint/some_file.txt";
723 const char RELPATH[] = "some_file.txt";
724 const char *CONTENTS = "abcdefgh";
727 ssize_t bufsize = strlen(CONTENTS);
731 expect_lookup(RELPATH, ino, bufsize);
732 expect_open(ino, 0, 1);
733 EXPECT_CALL(*m_mock, process(
734 ResultOf([=](auto in) {
735 return (in.header.opcode == FUSE_READ);
738 ).WillOnce(Invoke(ReturnErrno(EIO)));
740 ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
742 fd = open(FULLPATH, O_RDONLY);
743 ASSERT_LE(0, fd) << strerror(errno);
745 ASSERT_NE(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0));
749 /* Deliberately leak fd. close(2) will be tested in release.cc */
752 /* Large reads should be clustered, even across cache block boundaries */
754 * Disabled because clustered reads requires VOP_BMAP, which fusefs does not
757 TEST_P(ReadAhead, DISABLED_cluster) {
758 const char FULLPATH[] = "mountpoint/some_file.txt";
759 const char RELPATH[] = "some_file.txt";
762 ssize_t bufsize = 4 * m_maxbcachebuf;
763 ssize_t filesize = bufsize;
765 char *rbuf, *contents;
768 contents = (char*)malloc(filesize);
769 ASSERT_NE(NULL, contents);
770 memset(contents, 'X', filesize);
771 rbuf = (char*)calloc(1, bufsize);
773 expect_lookup(RELPATH, ino, filesize);
774 expect_open(ino, 0, 1);
775 maxcontig = m_noclusterr ? m_maxbcachebuf :
776 m_maxbcachebuf + (int)get<1>(GetParam());
777 for (offs = 0; offs < bufsize; offs += maxcontig) {
778 len = std::min((size_t)maxcontig, (size_t)(filesize - offs));
779 expect_read(ino, offs, len, len, contents + offs);
782 fd = open(FULLPATH, O_RDONLY);
783 ASSERT_LE(0, fd) << strerror(errno);
785 /* Set the internal readahead counter to a "large" value */
786 ASSERT_EQ(0, fcntl(fd, F_READAHEAD, 1'000'000'000)) << strerror(errno);
788 ASSERT_EQ(bufsize, read(fd, rbuf, bufsize)) << strerror(errno);
789 ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
791 /* Deliberately leak fd. close(2) will be tested in release.cc */
794 /* fuse(4) should honor the filesystem's requested m_readahead parameter */
795 TEST_P(ReadAhead, readahead) {
796 const char FULLPATH[] = "mountpoint/some_file.txt";
797 const char RELPATH[] = "some_file.txt";
800 ssize_t bufsize = m_maxbcachebuf;
801 ssize_t filesize = m_maxbcachebuf * 6;
802 char *rbuf, *contents;
804 contents = (char*)malloc(filesize);
805 ASSERT_NE(NULL, contents);
806 memset(contents, 'X', filesize);
807 rbuf = (char*)calloc(1, bufsize);
809 expect_lookup(RELPATH, ino, filesize);
810 expect_open(ino, 0, 1);
811 /* fuse(4) should only read ahead the allowed amount */
812 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf, contents);
813 for (i = 0; i < (int)get<1>(GetParam()) / m_maxbcachebuf; i++) {
814 off_t offs = (i + 1) * m_maxbcachebuf;
815 expect_read(ino, offs, m_maxbcachebuf, m_maxbcachebuf,
819 fd = open(FULLPATH, O_RDONLY);
820 ASSERT_LE(0, fd) << strerror(errno);
822 /* Set the internal readahead counter to a "large" value */
823 ASSERT_EQ(0, fcntl(fd, F_READAHEAD, 1'000'000'000)) << strerror(errno);
825 ASSERT_EQ(bufsize, read(fd, rbuf, bufsize)) << strerror(errno);
826 ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
828 /* Deliberately leak fd. close(2) will be tested in release.cc */
831 INSTANTIATE_TEST_CASE_P(RA, ReadAhead,
832 Values(tuple<bool, int>(false, 0u),
833 tuple<bool, int>(false, 0x10000),
834 tuple<bool, int>(false, 0x20000),
835 tuple<bool, int>(false, 0x30000),
836 tuple<bool, int>(true, 0u),
837 tuple<bool, int>(true, 0x10000)));