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
34 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
42 #include <semaphore.h>
49 using namespace testing;
51 class Read: public FuseTest {
54 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
56 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
60 class Read_7_8: public FuseTest {
62 virtual void SetUp() {
63 m_kernel_minor_version = 8;
67 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
69 FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
73 class AioRead: public Read {
75 virtual void SetUp() {
76 if (!is_unsafe_aio_enabled())
78 "vfs.aio.enable_unsafe must be set for this test";
83 class AsyncRead: public AioRead {
84 virtual void SetUp() {
85 m_init_flags = FUSE_ASYNC_READ;
90 class ReadAhead: public Read,
91 public WithParamInterface<tuple<bool, int>>
93 virtual void SetUp() {
95 const char *node = "vfs.maxbcachebuf";
96 size_t size = sizeof(val);
97 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
100 m_maxreadahead = val * get<1>(GetParam());
101 m_noclusterr = get<0>(GetParam());
106 /* AIO reads need to set the header's pid field correctly */
107 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
108 TEST_F(AioRead, aio_read)
110 const char FULLPATH[] = "mountpoint/some_file.txt";
111 const char RELPATH[] = "some_file.txt";
112 const char *CONTENTS = "abcdefgh";
115 ssize_t bufsize = strlen(CONTENTS);
117 struct aiocb iocb, *piocb;
119 expect_lookup(RELPATH, ino, bufsize);
120 expect_open(ino, 0, 1);
121 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
123 fd = open(FULLPATH, O_RDONLY);
124 ASSERT_LE(0, fd) << strerror(errno);
126 iocb.aio_nbytes = bufsize;
127 iocb.aio_fildes = fd;
130 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
131 ASSERT_EQ(0, aio_read(&iocb)) << strerror(errno);
132 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
133 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
139 * Without the FUSE_ASYNC_READ mount option, fuse(4) should ensure that there
140 * is at most one outstanding read operation per file handle
142 TEST_F(AioRead, async_read_disabled)
144 const char FULLPATH[] = "mountpoint/some_file.txt";
145 const char RELPATH[] = "some_file.txt";
148 ssize_t bufsize = 50;
149 char buf0[bufsize], buf1[bufsize];
151 off_t off1 = m_maxbcachebuf;
152 struct aiocb iocb0, iocb1;
153 volatile sig_atomic_t read_count = 0;
155 expect_lookup(RELPATH, ino, 131072);
156 expect_open(ino, 0, 1);
157 EXPECT_CALL(*m_mock, process(
158 ResultOf([=](auto in) {
159 return (in.header.opcode == FUSE_READ &&
160 in.header.nodeid == ino &&
161 in.body.read.fh == FH &&
162 in.body.read.offset == (uint64_t)off0);
165 ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
167 /* Filesystem is slow to respond */
169 EXPECT_CALL(*m_mock, process(
170 ResultOf([=](auto in) {
171 return (in.header.opcode == FUSE_READ &&
172 in.header.nodeid == ino &&
173 in.body.read.fh == FH &&
174 in.body.read.offset == (uint64_t)off1);
177 ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
179 /* Filesystem is slow to respond */
182 fd = open(FULLPATH, O_RDONLY);
183 ASSERT_LE(0, fd) << strerror(errno);
186 * Submit two AIO read requests, and respond to neither. If the
187 * filesystem ever gets the second read request, then we failed to
188 * limit outstanding reads.
190 iocb0.aio_nbytes = bufsize;
191 iocb0.aio_fildes = fd;
192 iocb0.aio_buf = buf0;
193 iocb0.aio_offset = off0;
194 iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
195 ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
197 iocb1.aio_nbytes = bufsize;
198 iocb1.aio_fildes = fd;
199 iocb1.aio_buf = buf1;
200 iocb1.aio_offset = off1;
201 iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
202 ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
205 * Sleep for awhile to make sure the kernel has had a chance to issue
206 * the second read, even though the first has not yet returned
209 EXPECT_EQ(read_count, 1);
211 m_mock->kill_daemon();
212 /* Wait for AIO activity to complete, but ignore errors */
213 (void)aio_waitcomplete(NULL, NULL);
219 * With the FUSE_ASYNC_READ mount option, fuse(4) may issue multiple
220 * simultaneous read requests on the same file handle.
222 TEST_F(AsyncRead, async_read)
224 const char FULLPATH[] = "mountpoint/some_file.txt";
225 const char RELPATH[] = "some_file.txt";
228 ssize_t bufsize = 50;
229 char buf0[bufsize], buf1[bufsize];
231 off_t off1 = m_maxbcachebuf;
232 off_t fsize = 2 * m_maxbcachebuf;
233 struct aiocb iocb0, iocb1;
236 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
238 expect_lookup(RELPATH, ino, fsize);
239 expect_open(ino, 0, 1);
240 EXPECT_CALL(*m_mock, process(
241 ResultOf([=](auto in) {
242 return (in.header.opcode == FUSE_READ &&
243 in.header.nodeid == ino &&
244 in.body.read.fh == FH &&
245 in.body.read.offset == (uint64_t)off0);
248 ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
250 /* Filesystem is slow to respond */
252 EXPECT_CALL(*m_mock, process(
253 ResultOf([=](auto in) {
254 return (in.header.opcode == FUSE_READ &&
255 in.header.nodeid == ino &&
256 in.body.read.fh == FH &&
257 in.body.read.offset == (uint64_t)off1);
260 ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
262 /* Filesystem is slow to respond */
265 fd = open(FULLPATH, O_RDONLY);
266 ASSERT_LE(0, fd) << strerror(errno);
269 * Submit two AIO read requests, but respond to neither. Ensure that
272 iocb0.aio_nbytes = bufsize;
273 iocb0.aio_fildes = fd;
274 iocb0.aio_buf = buf0;
275 iocb0.aio_offset = off0;
276 iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
277 ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
279 iocb1.aio_nbytes = bufsize;
280 iocb1.aio_fildes = fd;
281 iocb1.aio_buf = buf1;
282 iocb1.aio_offset = off1;
283 iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
284 ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
286 /* Wait until both reads have reached the daemon */
287 ASSERT_EQ(0, sem_wait(&sem)) << strerror(errno);
288 ASSERT_EQ(0, sem_wait(&sem)) << strerror(errno);
290 m_mock->kill_daemon();
291 /* Wait for AIO activity to complete, but ignore errors */
292 (void)aio_waitcomplete(NULL, NULL);
297 /* 0-length reads shouldn't cause any confusion */
298 TEST_F(Read, direct_io_read_nothing)
300 const char FULLPATH[] = "mountpoint/some_file.txt";
301 const char RELPATH[] = "some_file.txt";
304 uint64_t offset = 100;
307 expect_lookup(RELPATH, ino, offset + 1000);
308 expect_open(ino, FOPEN_DIRECT_IO, 1);
310 fd = open(FULLPATH, O_RDONLY);
311 ASSERT_LE(0, fd) << strerror(errno);
313 ASSERT_EQ(0, pread(fd, buf, 0, offset)) << strerror(errno);
318 * With direct_io, reads should not fill the cache. They should go straight to
321 TEST_F(Read, direct_io_pread)
323 const char FULLPATH[] = "mountpoint/some_file.txt";
324 const char RELPATH[] = "some_file.txt";
325 const char *CONTENTS = "abcdefgh";
328 uint64_t offset = 100;
329 ssize_t bufsize = strlen(CONTENTS);
332 expect_lookup(RELPATH, ino, offset + bufsize);
333 expect_open(ino, FOPEN_DIRECT_IO, 1);
334 expect_read(ino, offset, bufsize, bufsize, CONTENTS);
336 fd = open(FULLPATH, O_RDONLY);
337 ASSERT_LE(0, fd) << strerror(errno);
339 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
340 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
342 // With FOPEN_DIRECT_IO, the cache should be bypassed. The server will
343 // get a 2nd read request.
344 expect_read(ino, offset, bufsize, bufsize, CONTENTS);
345 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
346 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
351 * With direct_io, filesystems are allowed to return less data than is
352 * requested. fuse(4) should return a short read to userland.
354 TEST_F(Read, direct_io_short_read)
356 const char FULLPATH[] = "mountpoint/some_file.txt";
357 const char RELPATH[] = "some_file.txt";
358 const char *CONTENTS = "abcdefghijklmnop";
361 uint64_t offset = 100;
362 ssize_t bufsize = strlen(CONTENTS);
363 ssize_t halfbufsize = bufsize / 2;
366 expect_lookup(RELPATH, ino, offset + bufsize);
367 expect_open(ino, FOPEN_DIRECT_IO, 1);
368 expect_read(ino, offset, bufsize, halfbufsize, CONTENTS);
370 fd = open(FULLPATH, O_RDONLY);
371 ASSERT_LE(0, fd) << strerror(errno);
373 ASSERT_EQ(halfbufsize, pread(fd, buf, bufsize, offset))
375 ASSERT_EQ(0, memcmp(buf, CONTENTS, halfbufsize));
381 const char FULLPATH[] = "mountpoint/some_file.txt";
382 const char RELPATH[] = "some_file.txt";
383 const char *CONTENTS = "abcdefgh";
386 ssize_t bufsize = strlen(CONTENTS);
389 expect_lookup(RELPATH, ino, bufsize);
390 expect_open(ino, 0, 1);
391 EXPECT_CALL(*m_mock, process(
392 ResultOf([=](auto in) {
393 return (in.header.opcode == FUSE_READ);
396 ).WillOnce(Invoke(ReturnErrno(EIO)));
398 fd = open(FULLPATH, O_RDONLY);
399 ASSERT_LE(0, fd) << strerror(errno);
401 ASSERT_EQ(-1, read(fd, buf, bufsize)) << strerror(errno);
402 ASSERT_EQ(EIO, errno);
407 * If the server returns a short read when direct io is not in use, that
408 * indicates EOF, because of a server-side truncation. We should invalidate
409 * all cached attributes. We may update the file size,
413 const char FULLPATH[] = "mountpoint/some_file.txt";
414 const char RELPATH[] = "some_file.txt";
415 const char *CONTENTS = "abcdefghijklmnop";
418 uint64_t offset = 100;
419 ssize_t bufsize = strlen(CONTENTS);
420 ssize_t partbufsize = 3 * bufsize / 4;
425 expect_lookup(RELPATH, ino, offset + bufsize);
426 expect_open(ino, 0, 1);
427 expect_read(ino, 0, offset + bufsize, offset + partbufsize, CONTENTS);
428 expect_getattr(ino, offset + partbufsize);
430 fd = open(FULLPATH, O_RDONLY);
431 ASSERT_LE(0, fd) << strerror(errno);
433 r = pread(fd, buf, bufsize, offset);
434 ASSERT_LE(0, r) << strerror(errno);
435 EXPECT_EQ(partbufsize, r) << strerror(errno);
436 ASSERT_EQ(0, fstat(fd, &sb));
437 EXPECT_EQ((off_t)(offset + partbufsize), sb.st_size);
441 /* Like Read.eof, but causes an entire buffer to be invalidated */
442 TEST_F(Read, eof_of_whole_buffer)
444 const char FULLPATH[] = "mountpoint/some_file.txt";
445 const char RELPATH[] = "some_file.txt";
446 const char *CONTENTS = "abcdefghijklmnop";
449 ssize_t bufsize = strlen(CONTENTS);
450 off_t old_filesize = m_maxbcachebuf * 2 + bufsize;
454 expect_lookup(RELPATH, ino, old_filesize);
455 expect_open(ino, 0, 1);
456 expect_read(ino, 2 * m_maxbcachebuf, bufsize, bufsize, CONTENTS);
457 expect_read(ino, m_maxbcachebuf, m_maxbcachebuf, 0, CONTENTS);
458 expect_getattr(ino, m_maxbcachebuf);
460 fd = open(FULLPATH, O_RDONLY);
461 ASSERT_LE(0, fd) << strerror(errno);
463 /* Cache the third block */
464 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, m_maxbcachebuf * 2))
466 /* Try to read the 2nd block, but it's past EOF */
467 ASSERT_EQ(0, pread(fd, buf, bufsize, m_maxbcachebuf))
469 ASSERT_EQ(0, fstat(fd, &sb));
470 EXPECT_EQ((off_t)(m_maxbcachebuf), sb.st_size);
475 * With the keep_cache option, the kernel may keep its read cache across
478 TEST_F(Read, keep_cache)
480 const char FULLPATH[] = "mountpoint/some_file.txt";
481 const char RELPATH[] = "some_file.txt";
482 const char *CONTENTS = "abcdefgh";
485 ssize_t bufsize = strlen(CONTENTS);
488 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
489 expect_open(ino, FOPEN_KEEP_CACHE, 2);
490 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
492 fd0 = open(FULLPATH, O_RDONLY);
493 ASSERT_LE(0, fd0) << strerror(errno);
494 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
496 fd1 = open(FULLPATH, O_RDWR);
497 ASSERT_LE(0, fd1) << strerror(errno);
500 * This read should be serviced by cache, even though it's on the other
503 ASSERT_EQ(bufsize, read(fd1, buf, bufsize)) << strerror(errno);
510 * Without the keep_cache option, the kernel should drop its read caches on
513 TEST_F(Read, keep_cache_disabled)
515 const char FULLPATH[] = "mountpoint/some_file.txt";
516 const char RELPATH[] = "some_file.txt";
517 const char *CONTENTS = "abcdefgh";
520 ssize_t bufsize = strlen(CONTENTS);
523 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
524 expect_open(ino, 0, 2);
525 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
527 fd0 = open(FULLPATH, O_RDONLY);
528 ASSERT_LE(0, fd0) << strerror(errno);
529 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
531 fd1 = open(FULLPATH, O_RDWR);
532 ASSERT_LE(0, fd1) << strerror(errno);
535 * This read should not be serviced by cache, even though it's on the
536 * original file descriptor
538 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
539 ASSERT_EQ(0, lseek(fd0, 0, SEEK_SET)) << strerror(errno);
540 ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
548 const char FULLPATH[] = "mountpoint/some_file.txt";
549 const char RELPATH[] = "some_file.txt";
550 const char *CONTENTS = "abcdefgh";
554 size_t bufsize = strlen(CONTENTS);
559 expect_lookup(RELPATH, ino, bufsize);
560 expect_open(ino, 0, 1);
561 EXPECT_CALL(*m_mock, process(
562 ResultOf([=](auto in) {
563 return (in.header.opcode == FUSE_READ &&
564 in.header.nodeid == ino &&
565 in.body.read.fh == Read::FH &&
566 in.body.read.offset == 0 &&
567 in.body.read.size == bufsize);
570 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
571 out.header.len = sizeof(struct fuse_out_header) + bufsize;
572 memmove(out.body.bytes, CONTENTS, bufsize);
575 fd = open(FULLPATH, O_RDONLY);
576 ASSERT_LE(0, fd) << strerror(errno);
578 p = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
579 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
581 ASSERT_EQ(0, memcmp(p, CONTENTS, bufsize));
583 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
588 * A read via mmap comes up short, indicating that the file was truncated
591 TEST_F(Read, mmap_eof)
593 const char FULLPATH[] = "mountpoint/some_file.txt";
594 const char RELPATH[] = "some_file.txt";
595 const char *CONTENTS = "abcdefgh";
599 size_t bufsize = strlen(CONTENTS);
605 expect_lookup(RELPATH, ino, m_maxbcachebuf);
606 expect_open(ino, 0, 1);
607 EXPECT_CALL(*m_mock, process(
608 ResultOf([=](auto in) {
609 return (in.header.opcode == FUSE_READ &&
610 in.header.nodeid == ino &&
611 in.body.read.fh == Read::FH &&
612 in.body.read.offset == 0 &&
613 in.body.read.size == (uint32_t)m_maxbcachebuf);
616 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
617 out.header.len = sizeof(struct fuse_out_header) + bufsize;
618 memmove(out.body.bytes, CONTENTS, bufsize);
620 expect_getattr(ino, bufsize);
622 fd = open(FULLPATH, O_RDONLY);
623 ASSERT_LE(0, fd) << strerror(errno);
625 p = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
626 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
628 /* The file size should be automatically truncated */
629 ASSERT_EQ(0, memcmp(p, CONTENTS, bufsize));
630 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
631 EXPECT_EQ((off_t)bufsize, sb.st_size);
633 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
638 * Just as when FOPEN_DIRECT_IO is used, reads with O_DIRECT should bypass
639 * cache and to straight to the daemon
641 TEST_F(Read, o_direct)
643 const char FULLPATH[] = "mountpoint/some_file.txt";
644 const char RELPATH[] = "some_file.txt";
645 const char *CONTENTS = "abcdefgh";
648 ssize_t bufsize = strlen(CONTENTS);
651 expect_lookup(RELPATH, ino, bufsize);
652 expect_open(ino, 0, 1);
653 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
655 fd = open(FULLPATH, O_RDONLY);
656 ASSERT_LE(0, fd) << strerror(errno);
659 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
660 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
662 // Reads with o_direct should bypass the cache
663 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
664 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
665 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
666 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
667 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
674 const char FULLPATH[] = "mountpoint/some_file.txt";
675 const char RELPATH[] = "some_file.txt";
676 const char *CONTENTS = "abcdefgh";
680 * Set offset to a maxbcachebuf boundary so we'll be sure what offset
681 * to read from. Without this, the read might start at a lower offset.
683 uint64_t offset = m_maxbcachebuf;
684 ssize_t bufsize = strlen(CONTENTS);
687 expect_lookup(RELPATH, ino, offset + bufsize);
688 expect_open(ino, 0, 1);
689 expect_read(ino, offset, bufsize, bufsize, CONTENTS);
691 fd = open(FULLPATH, O_RDONLY);
692 ASSERT_LE(0, fd) << strerror(errno);
694 ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
695 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
701 const char FULLPATH[] = "mountpoint/some_file.txt";
702 const char RELPATH[] = "some_file.txt";
703 const char *CONTENTS = "abcdefgh";
706 ssize_t bufsize = strlen(CONTENTS);
709 expect_lookup(RELPATH, ino, bufsize);
710 expect_open(ino, 0, 1);
711 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
713 fd = open(FULLPATH, O_RDONLY);
714 ASSERT_LE(0, fd) << strerror(errno);
716 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
717 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
722 TEST_F(Read_7_8, read)
724 const char FULLPATH[] = "mountpoint/some_file.txt";
725 const char RELPATH[] = "some_file.txt";
726 const char *CONTENTS = "abcdefgh";
729 ssize_t bufsize = strlen(CONTENTS);
732 expect_lookup(RELPATH, ino, bufsize);
733 expect_open(ino, 0, 1);
734 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
736 fd = open(FULLPATH, O_RDONLY);
737 ASSERT_LE(0, fd) << strerror(errno);
739 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
740 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
746 * If cacheing is enabled, the kernel should try to read an entire cache block
749 TEST_F(Read, cache_block)
751 const char FULLPATH[] = "mountpoint/some_file.txt";
752 const char RELPATH[] = "some_file.txt";
753 const char *CONTENTS0 = "abcdefghijklmnop";
757 ssize_t filesize = m_maxbcachebuf * 2;
760 const char *contents1 = CONTENTS0 + bufsize;
762 contents = (char*)calloc(1, filesize);
763 ASSERT_NE(nullptr, contents);
764 memmove(contents, CONTENTS0, strlen(CONTENTS0));
766 expect_lookup(RELPATH, ino, filesize);
767 expect_open(ino, 0, 1);
768 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf,
771 fd = open(FULLPATH, O_RDONLY);
772 ASSERT_LE(0, fd) << strerror(errno);
774 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
775 ASSERT_EQ(0, memcmp(buf, CONTENTS0, bufsize));
777 /* A subsequent read should be serviced by cache */
778 ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
779 ASSERT_EQ(0, memcmp(buf, contents1, bufsize));
783 /* Reading with sendfile should work (though it obviously won't be 0-copy) */
784 TEST_F(Read, sendfile)
786 const char FULLPATH[] = "mountpoint/some_file.txt";
787 const char RELPATH[] = "some_file.txt";
788 const char *CONTENTS = "abcdefgh";
791 size_t bufsize = strlen(CONTENTS);
796 expect_lookup(RELPATH, ino, bufsize);
797 expect_open(ino, 0, 1);
798 EXPECT_CALL(*m_mock, process(
799 ResultOf([=](auto in) {
800 return (in.header.opcode == FUSE_READ &&
801 in.header.nodeid == ino &&
802 in.body.read.fh == Read::FH &&
803 in.body.read.offset == 0 &&
804 in.body.read.size == bufsize);
807 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
808 out.header.len = sizeof(struct fuse_out_header) + bufsize;
809 memmove(out.body.bytes, CONTENTS, bufsize);
812 ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
814 fd = open(FULLPATH, O_RDONLY);
815 ASSERT_LE(0, fd) << strerror(errno);
817 ASSERT_EQ(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0))
819 ASSERT_EQ(static_cast<ssize_t>(bufsize), read(sp[0], buf, bufsize))
821 ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
828 /* sendfile should fail gracefully if fuse declines the read */
829 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236466 */
830 TEST_F(Read, sendfile_eio)
832 const char FULLPATH[] = "mountpoint/some_file.txt";
833 const char RELPATH[] = "some_file.txt";
834 const char *CONTENTS = "abcdefgh";
837 ssize_t bufsize = strlen(CONTENTS);
841 expect_lookup(RELPATH, ino, bufsize);
842 expect_open(ino, 0, 1);
843 EXPECT_CALL(*m_mock, process(
844 ResultOf([=](auto in) {
845 return (in.header.opcode == FUSE_READ);
848 ).WillOnce(Invoke(ReturnErrno(EIO)));
850 ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
852 fd = open(FULLPATH, O_RDONLY);
853 ASSERT_LE(0, fd) << strerror(errno);
855 ASSERT_NE(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0));
863 * Sequential reads should use readahead. And if allowed, large reads should
866 TEST_P(ReadAhead, readahead) {
867 const char FULLPATH[] = "mountpoint/some_file.txt";
868 const char RELPATH[] = "some_file.txt";
870 int fd, maxcontig, clustersize;
871 ssize_t bufsize = 4 * m_maxbcachebuf;
872 ssize_t filesize = bufsize;
874 char *rbuf, *contents;
877 contents = (char*)malloc(filesize);
878 ASSERT_NE(nullptr, contents);
879 memset(contents, 'X', filesize);
880 rbuf = (char*)calloc(1, bufsize);
882 expect_lookup(RELPATH, ino, filesize);
883 expect_open(ino, 0, 1);
884 maxcontig = m_noclusterr ? m_maxbcachebuf :
885 m_maxbcachebuf + m_maxreadahead;
886 clustersize = MIN(maxcontig, m_maxphys);
887 for (offs = 0; offs < bufsize; offs += clustersize) {
888 len = std::min((size_t)clustersize, (size_t)(filesize - offs));
889 expect_read(ino, offs, len, len, contents + offs);
892 fd = open(FULLPATH, O_RDONLY);
893 ASSERT_LE(0, fd) << strerror(errno);
895 /* Set the internal readahead counter to a "large" value */
896 ASSERT_EQ(0, fcntl(fd, F_READAHEAD, 1'000'000'000)) << strerror(errno);
898 ASSERT_EQ(bufsize, read(fd, rbuf, bufsize)) << strerror(errno);
899 ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
904 INSTANTIATE_TEST_CASE_P(RA, ReadAhead,
905 Values(tuple<bool, int>(false, 0),
906 tuple<bool, int>(false, 1),
907 tuple<bool, int>(false, 2),
908 tuple<bool, int>(false, 3),
909 tuple<bool, int>(true, 0),
910 tuple<bool, int>(true, 1),
911 tuple<bool, int>(true, 2)));