]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/read.cc
fusefs: use cluster_read for more readahead
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / read.cc
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
31 extern "C" {
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 #include <sys/uio.h>
37
38 #include <aio.h>
39 #include <fcntl.h>
40 #include <semaphore.h>
41 #include <unistd.h>
42 }
43
44 #include "mockfs.hh"
45 #include "utils.hh"
46
47 using namespace testing;
48
49 class Read: public FuseTest {
50
51 public:
52 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
53 {
54         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
55 }
56 };
57
58 class Read_7_8: public FuseTest {
59 public:
60 virtual void SetUp() {
61         m_kernel_minor_version = 8;
62         FuseTest::SetUp();
63 }
64
65 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
66 {
67         FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
68 }
69 };
70
71 class AioRead: public Read {
72 public:
73 virtual void SetUp() {
74         const char *node = "vfs.aio.enable_unsafe";
75         int val = 0;
76         size_t size = sizeof(val);
77
78         FuseTest::SetUp();
79
80         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
81                 << strerror(errno);
82         if (!val)
83                 GTEST_SKIP() <<
84                         "vfs.aio.enable_unsafe must be set for this test";
85 }
86 };
87
88 class AsyncRead: public AioRead {
89         virtual void SetUp() {
90                 m_init_flags = FUSE_ASYNC_READ;
91                 AioRead::SetUp();
92         }
93 };
94
95 class ReadCacheable: public Read {
96 public:
97 virtual void SetUp() {
98         const char *node = "vfs.fusefs.data_cache_mode";
99         int val = 0;
100         size_t size = sizeof(val);
101
102         FuseTest::SetUp();
103
104         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
105                 << strerror(errno);
106         if (val == 0)
107                 GTEST_SKIP() <<
108                         "fusefs data caching must be enabled for this test";
109 }
110 };
111
112 class ReadAhead: public ReadCacheable,
113                  public WithParamInterface<tuple<bool, uint32_t>>
114 {
115         virtual void SetUp() {
116                 m_maxreadahead = get<1>(GetParam());
117                 m_noclusterr = get<0>(GetParam());
118                 ReadCacheable::SetUp();
119         }
120 };
121
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)
125 {
126         const char FULLPATH[] = "mountpoint/some_file.txt";
127         const char RELPATH[] = "some_file.txt";
128         const char *CONTENTS = "abcdefgh";
129         uint64_t ino = 42;
130         int fd;
131         ssize_t bufsize = strlen(CONTENTS);
132         char buf[bufsize];
133         struct aiocb iocb, *piocb;
134
135         expect_lookup(RELPATH, ino, bufsize);
136         expect_open(ino, 0, 1);
137         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
138
139         fd = open(FULLPATH, O_RDONLY);
140         ASSERT_LE(0, fd) << strerror(errno);
141
142         iocb.aio_nbytes = bufsize;
143         iocb.aio_fildes = fd;
144         iocb.aio_buf = buf;
145         iocb.aio_offset = 0;
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 */
151 }
152
153 /* 
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
156  */
157 TEST_F(AioRead, async_read_disabled)
158 {
159         const char FULLPATH[] = "mountpoint/some_file.txt";
160         const char RELPATH[] = "some_file.txt";
161         uint64_t ino = 42;
162         int fd;
163         ssize_t bufsize = 50;
164         char buf0[bufsize], buf1[bufsize];
165         off_t off0 = 0;
166         off_t off1 = 65536;
167         struct aiocb iocb0, iocb1;
168         volatile sig_atomic_t read_count = 0;
169
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);
178                 }, Eq(true)),
179                 _)
180         ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
181                 read_count++;
182                 /* Filesystem is slow to respond */
183         }));
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);
190                 }, Eq(true)),
191                 _)
192         ).WillRepeatedly(Invoke([&](auto in __unused, auto &out __unused) {
193                 read_count++;
194                 /* Filesystem is slow to respond */
195         }));
196
197         fd = open(FULLPATH, O_RDONLY);
198         ASSERT_LE(0, fd) << strerror(errno);
199
200         /* 
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.
204          */
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);
211
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);
218
219         /* 
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
222          */
223         nap();
224         EXPECT_EQ(read_count, 1);
225         
226         m_mock->kill_daemon();
227         /* Wait for AIO activity to complete, but ignore errors */
228         (void)aio_waitcomplete(NULL, NULL);
229
230         /* Deliberately leak fd.  close(2) will be tested in release.cc */
231 }
232
233 /* 
234  * With the FUSE_ASYNC_READ mount option, fuse(4) may issue multiple
235  * simultaneous read requests on the same file handle.
236  */
237 TEST_F(AsyncRead, async_read)
238 {
239         const char FULLPATH[] = "mountpoint/some_file.txt";
240         const char RELPATH[] = "some_file.txt";
241         uint64_t ino = 42;
242         int fd;
243         ssize_t bufsize = 50;
244         char buf0[bufsize], buf1[bufsize];
245         off_t off0 = 0;
246         off_t off1 = 65536;
247         struct aiocb iocb0, iocb1;
248         sem_t sem;
249
250         ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
251
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);
260                 }, Eq(true)),
261                 _)
262         ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
263                 sem_post(&sem);
264                 /* Filesystem is slow to respond */
265         }));
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);
272                 }, Eq(true)),
273                 _)
274         ).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
275                 sem_post(&sem);
276                 /* Filesystem is slow to respond */
277         }));
278
279         fd = open(FULLPATH, O_RDONLY);
280         ASSERT_LE(0, fd) << strerror(errno);
281
282         /* 
283          * Submit two AIO read requests, but respond to neither.  Ensure that
284          * we received both.
285          */
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);
292
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);
299
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);
303
304         m_mock->kill_daemon();
305         /* Wait for AIO activity to complete, but ignore errors */
306         (void)aio_waitcomplete(NULL, NULL);
307         
308         /* Deliberately leak fd.  close(2) will be tested in release.cc */
309 }
310
311 /* 0-length reads shouldn't cause any confusion */
312 TEST_F(Read, direct_io_read_nothing)
313 {
314         const char FULLPATH[] = "mountpoint/some_file.txt";
315         const char RELPATH[] = "some_file.txt";
316         uint64_t ino = 42;
317         int fd;
318         uint64_t offset = 100;
319         char buf[80];
320
321         expect_lookup(RELPATH, ino, offset + 1000);
322         expect_open(ino, FOPEN_DIRECT_IO, 1);
323
324         fd = open(FULLPATH, O_RDONLY);
325         ASSERT_LE(0, fd) << strerror(errno);
326
327         ASSERT_EQ(0, pread(fd, buf, 0, offset)) << strerror(errno);
328         /* Deliberately leak fd.  close(2) will be tested in release.cc */
329 }
330
331 /* 
332  * With direct_io, reads should not fill the cache.  They should go straight to
333  * the daemon
334  */
335 TEST_F(Read, direct_io_pread)
336 {
337         const char FULLPATH[] = "mountpoint/some_file.txt";
338         const char RELPATH[] = "some_file.txt";
339         const char *CONTENTS = "abcdefgh";
340         uint64_t ino = 42;
341         int fd;
342         uint64_t offset = 100;
343         ssize_t bufsize = strlen(CONTENTS);
344         char buf[bufsize];
345
346         expect_lookup(RELPATH, ino, offset + bufsize);
347         expect_open(ino, FOPEN_DIRECT_IO, 1);
348         expect_read(ino, offset, bufsize, bufsize, CONTENTS);
349
350         fd = open(FULLPATH, O_RDONLY);
351         ASSERT_LE(0, fd) << strerror(errno);
352
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 */
356 }
357
358 /* 
359  * With direct_io, filesystems are allowed to return less data than is
360  * requested.  fuse(4) should return a short read to userland.
361  */
362 TEST_F(Read, direct_io_short_read)
363 {
364         const char FULLPATH[] = "mountpoint/some_file.txt";
365         const char RELPATH[] = "some_file.txt";
366         const char *CONTENTS = "abcdefghijklmnop";
367         uint64_t ino = 42;
368         int fd;
369         uint64_t offset = 100;
370         ssize_t bufsize = strlen(CONTENTS);
371         ssize_t halfbufsize = bufsize / 2;
372         char buf[bufsize];
373
374         expect_lookup(RELPATH, ino, offset + bufsize);
375         expect_open(ino, FOPEN_DIRECT_IO, 1);
376         expect_read(ino, offset, bufsize, halfbufsize, CONTENTS);
377
378         fd = open(FULLPATH, O_RDONLY);
379         ASSERT_LE(0, fd) << strerror(errno);
380
381         ASSERT_EQ(halfbufsize, pread(fd, buf, bufsize, offset))
382                 << strerror(errno);
383         ASSERT_EQ(0, memcmp(buf, CONTENTS, halfbufsize));
384         /* Deliberately leak fd.  close(2) will be tested in release.cc */
385 }
386
387 TEST_F(Read, eio)
388 {
389         const char FULLPATH[] = "mountpoint/some_file.txt";
390         const char RELPATH[] = "some_file.txt";
391         const char *CONTENTS = "abcdefgh";
392         uint64_t ino = 42;
393         int fd;
394         ssize_t bufsize = strlen(CONTENTS);
395         char buf[bufsize];
396
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);
402                 }, Eq(true)),
403                 _)
404         ).WillOnce(Invoke(ReturnErrno(EIO)));
405
406         fd = open(FULLPATH, O_RDONLY);
407         ASSERT_LE(0, fd) << strerror(errno);
408
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 */
412 }
413
414 /* 
415  * With the keep_cache option, the kernel may keep its read cache across
416  * multiple open(2)s.
417  */
418 TEST_F(ReadCacheable, keep_cache)
419 {
420         const char FULLPATH[] = "mountpoint/some_file.txt";
421         const char RELPATH[] = "some_file.txt";
422         const char *CONTENTS = "abcdefgh";
423         uint64_t ino = 42;
424         int fd0, fd1;
425         ssize_t bufsize = strlen(CONTENTS);
426         char buf[bufsize];
427
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);
431
432         fd0 = open(FULLPATH, O_RDONLY);
433         ASSERT_LE(0, fd0) << strerror(errno);
434         ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
435
436         fd1 = open(FULLPATH, O_RDWR);
437         ASSERT_LE(0, fd1) << strerror(errno);
438
439         /*
440          * This read should be serviced by cache, even though it's on the other
441          * file descriptor
442          */
443         ASSERT_EQ(bufsize, read(fd1, buf, bufsize)) << strerror(errno);
444
445         /* Deliberately leak fd0 and fd1. */
446 }
447
448 /* 
449  * Without the keep_cache option, the kernel should drop its read caches on
450  * every open
451  */
452 TEST_F(Read, keep_cache_disabled)
453 {
454         const char FULLPATH[] = "mountpoint/some_file.txt";
455         const char RELPATH[] = "some_file.txt";
456         const char *CONTENTS = "abcdefgh";
457         uint64_t ino = 42;
458         int fd0, fd1;
459         ssize_t bufsize = strlen(CONTENTS);
460         char buf[bufsize];
461
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);
465
466         fd0 = open(FULLPATH, O_RDONLY);
467         ASSERT_LE(0, fd0) << strerror(errno);
468         ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
469
470         fd1 = open(FULLPATH, O_RDWR);
471         ASSERT_LE(0, fd1) << strerror(errno);
472
473         /*
474          * This read should not be serviced by cache, even though it's on the
475          * original file descriptor
476          */
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);
480
481         /* Deliberately leak fd0 and fd1. */
482 }
483
484 TEST_F(ReadCacheable, mmap)
485 {
486         const char FULLPATH[] = "mountpoint/some_file.txt";
487         const char RELPATH[] = "some_file.txt";
488         const char *CONTENTS = "abcdefgh";
489         uint64_t ino = 42;
490         int fd;
491         ssize_t len;
492         size_t bufsize = strlen(CONTENTS);
493         void *p;
494
495         len = getpagesize();
496
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);
507                 }, Eq(true)),
508                 _)
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);
512         })));
513
514         fd = open(FULLPATH, O_RDONLY);
515         ASSERT_LE(0, fd) << strerror(errno);
516
517         p = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
518         ASSERT_NE(MAP_FAILED, p) << strerror(errno);
519
520         ASSERT_EQ(0, memcmp(p, CONTENTS, bufsize));
521
522         ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
523         /* Deliberately leak fd.  close(2) will be tested in release.cc */
524 }
525
526 /*
527  * Just as when FOPEN_DIRECT_IO is used, reads with O_DIRECT should bypass
528  * cache and to straight to the daemon
529  */
530 TEST_F(Read, o_direct)
531 {
532         const char FULLPATH[] = "mountpoint/some_file.txt";
533         const char RELPATH[] = "some_file.txt";
534         const char *CONTENTS = "abcdefgh";
535         uint64_t ino = 42;
536         int fd;
537         ssize_t bufsize = strlen(CONTENTS);
538         char buf[bufsize];
539
540         expect_lookup(RELPATH, ino, bufsize);
541         expect_open(ino, 0, 1);
542         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
543
544         fd = open(FULLPATH, O_RDONLY);
545         ASSERT_LE(0, fd) << strerror(errno);
546
547         // Fill the cache
548         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
549         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
550
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));
557         
558         /* Deliberately leak fd.  close(2) will be tested in release.cc */
559 }
560
561 TEST_F(Read, pread)
562 {
563         const char FULLPATH[] = "mountpoint/some_file.txt";
564         const char RELPATH[] = "some_file.txt";
565         const char *CONTENTS = "abcdefgh";
566         uint64_t ino = 42;
567         int fd;
568         /* 
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.
571          */
572         uint64_t offset = m_maxbcachebuf;
573         ssize_t bufsize = strlen(CONTENTS);
574         char buf[bufsize];
575
576         expect_lookup(RELPATH, ino, offset + bufsize);
577         expect_open(ino, 0, 1);
578         expect_read(ino, offset, bufsize, bufsize, CONTENTS);
579
580         fd = open(FULLPATH, O_RDONLY);
581         ASSERT_LE(0, fd) << strerror(errno);
582
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 */
586 }
587
588 TEST_F(Read, read)
589 {
590         const char FULLPATH[] = "mountpoint/some_file.txt";
591         const char RELPATH[] = "some_file.txt";
592         const char *CONTENTS = "abcdefgh";
593         uint64_t ino = 42;
594         int fd;
595         ssize_t bufsize = strlen(CONTENTS);
596         char buf[bufsize];
597
598         expect_lookup(RELPATH, ino, bufsize);
599         expect_open(ino, 0, 1);
600         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
601
602         fd = open(FULLPATH, O_RDONLY);
603         ASSERT_LE(0, fd) << strerror(errno);
604
605         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
606         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
607
608         /* Deliberately leak fd.  close(2) will be tested in release.cc */
609 }
610
611 TEST_F(Read_7_8, read)
612 {
613         const char FULLPATH[] = "mountpoint/some_file.txt";
614         const char RELPATH[] = "some_file.txt";
615         const char *CONTENTS = "abcdefgh";
616         uint64_t ino = 42;
617         int fd;
618         ssize_t bufsize = strlen(CONTENTS);
619         char buf[bufsize];
620
621         expect_lookup(RELPATH, ino, bufsize);
622         expect_open(ino, 0, 1);
623         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
624
625         fd = open(FULLPATH, O_RDONLY);
626         ASSERT_LE(0, fd) << strerror(errno);
627
628         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
629         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
630
631         /* Deliberately leak fd.  close(2) will be tested in release.cc */
632 }
633
634 /* 
635  * If cacheing is enabled, the kernel should try to read an entire cache block
636  * at a time.
637  */
638 TEST_F(ReadCacheable, cache_block)
639 {
640         const char FULLPATH[] = "mountpoint/some_file.txt";
641         const char RELPATH[] = "some_file.txt";
642         const char *CONTENTS0 = "abcdefghijklmnop";
643         uint64_t ino = 42;
644         int fd;
645         ssize_t bufsize = 8;
646         ssize_t filesize = m_maxbcachebuf * 2;
647         char *contents;
648         char buf[bufsize];
649         const char *contents1 = CONTENTS0 + bufsize;
650
651         contents = (char*)calloc(1, filesize);
652         ASSERT_NE(NULL, contents);
653         memmove(contents, CONTENTS0, strlen(CONTENTS0));
654
655         expect_lookup(RELPATH, ino, filesize);
656         expect_open(ino, 0, 1);
657         expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf,
658                 contents);
659
660         fd = open(FULLPATH, O_RDONLY);
661         ASSERT_LE(0, fd) << strerror(errno);
662
663         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
664         ASSERT_EQ(0, memcmp(buf, CONTENTS0, bufsize));
665
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 */
670 }
671
672 /* Reading with sendfile should work (though it obviously won't be 0-copy) */
673 TEST_F(ReadCacheable, sendfile)
674 {
675         const char FULLPATH[] = "mountpoint/some_file.txt";
676         const char RELPATH[] = "some_file.txt";
677         const char *CONTENTS = "abcdefgh";
678         uint64_t ino = 42;
679         int fd;
680         size_t bufsize = strlen(CONTENTS);
681         char buf[bufsize];
682         int sp[2];
683         off_t sbytes;
684
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);
695                 }, Eq(true)),
696                 _)
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);
700         })));
701
702         ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
703                 << strerror(errno);
704         fd = open(FULLPATH, O_RDONLY);
705         ASSERT_LE(0, fd) << strerror(errno);
706
707         ASSERT_EQ(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0))
708                 << strerror(errno);
709         ASSERT_EQ(static_cast<ssize_t>(bufsize), read(sp[0], buf, bufsize))
710                 << strerror(errno);
711         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
712
713         close(sp[1]);
714         close(sp[0]);
715         /* Deliberately leak fd.  close(2) will be tested in release.cc */
716 }
717
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)
721 {
722         const char FULLPATH[] = "mountpoint/some_file.txt";
723         const char RELPATH[] = "some_file.txt";
724         const char *CONTENTS = "abcdefgh";
725         uint64_t ino = 42;
726         int fd;
727         ssize_t bufsize = strlen(CONTENTS);
728         int sp[2];
729         off_t sbytes;
730
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);
736                 }, Eq(true)),
737                 _)
738         ).WillOnce(Invoke(ReturnErrno(EIO)));
739
740         ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
741                 << strerror(errno);
742         fd = open(FULLPATH, O_RDONLY);
743         ASSERT_LE(0, fd) << strerror(errno);
744
745         ASSERT_NE(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0));
746
747         close(sp[1]);
748         close(sp[0]);
749         /* Deliberately leak fd.  close(2) will be tested in release.cc */
750 }
751
752 /* Large reads should be clustered, even across cache block boundaries */
753 /* 
754  * Disabled because clustered reads requires VOP_BMAP, which fusefs does not
755  * yet support
756  */
757 TEST_P(ReadAhead, DISABLED_cluster) {
758         const char FULLPATH[] = "mountpoint/some_file.txt";
759         const char RELPATH[] = "some_file.txt";
760         uint64_t ino = 42;
761         int fd, maxcontig;
762         ssize_t bufsize = 4 * m_maxbcachebuf;
763         ssize_t filesize = bufsize;
764         uint64_t len;
765         char *rbuf, *contents;
766         off_t offs;
767
768         contents = (char*)malloc(filesize);
769         ASSERT_NE(NULL, contents);
770         memset(contents, 'X', filesize);
771         rbuf = (char*)calloc(1, bufsize);
772
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);
780         }
781
782         fd = open(FULLPATH, O_RDONLY);
783         ASSERT_LE(0, fd) << strerror(errno);
784
785         /* Set the internal readahead counter to a "large" value */
786         ASSERT_EQ(0, fcntl(fd, F_READAHEAD, 1'000'000'000)) << strerror(errno);
787
788         ASSERT_EQ(bufsize, read(fd, rbuf, bufsize)) << strerror(errno);
789         ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
790
791         /* Deliberately leak fd.  close(2) will be tested in release.cc */
792 }
793
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";
798         uint64_t ino = 42;
799         int fd, i;
800         ssize_t bufsize = m_maxbcachebuf;
801         ssize_t filesize = m_maxbcachebuf * 6;
802         char *rbuf, *contents;
803
804         contents = (char*)malloc(filesize);
805         ASSERT_NE(NULL, contents);
806         memset(contents, 'X', filesize);
807         rbuf = (char*)calloc(1, bufsize);
808
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,
816                         contents + offs);
817         }
818
819         fd = open(FULLPATH, O_RDONLY);
820         ASSERT_LE(0, fd) << strerror(errno);
821
822         /* Set the internal readahead counter to a "large" value */
823         ASSERT_EQ(0, fcntl(fd, F_READAHEAD, 1'000'000'000)) << strerror(errno);
824
825         ASSERT_EQ(bufsize, read(fd, rbuf, bufsize)) << strerror(errno);
826         ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
827
828         /* Deliberately leak fd.  close(2) will be tested in release.cc */
829 }
830
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)));