]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/read.cc
Fix the branch build
[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 <unistd.h>
41 }
42
43 #include "mockfs.hh"
44 #include "utils.hh"
45
46 using namespace testing;
47
48 class Read: public FuseTest {
49
50 public:
51 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
52 {
53         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
54 }
55 };
56
57 class AioRead: public Read {
58 public:
59 virtual void SetUp() {
60         const char *node = "vfs.aio.enable_unsafe";
61         int val = 0;
62         size_t size = sizeof(val);
63
64         FuseTest::SetUp();
65
66         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
67                 << strerror(errno);
68         if (!val)
69                 GTEST_SKIP() <<
70                         "vfs.aio.enable_unsafe must be set for this test";
71 }
72 };
73
74 class AsyncRead: public AioRead {
75         virtual void SetUp() {
76                 m_init_flags = FUSE_ASYNC_READ;
77                 AioRead::SetUp();
78         }
79 };
80
81 class ReadCacheable: public Read {
82 public:
83 virtual void SetUp() {
84         const char *node = "vfs.fusefs.data_cache_mode";
85         int val = 0;
86         size_t size = sizeof(val);
87
88         FuseTest::SetUp();
89
90         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
91                 << strerror(errno);
92         if (val == 0)
93                 GTEST_SKIP() <<
94                         "fusefs data caching must be enabled for this test";
95 }
96 };
97
98 class ReadAhead: public ReadCacheable, public WithParamInterface<uint32_t> {
99         virtual void SetUp() {
100                 m_maxreadahead = GetParam();
101                 Read::SetUp();
102         }
103 };
104
105 /* AIO reads need to set the header's pid field correctly */
106 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
107 TEST_F(AioRead, aio_read)
108 {
109         const char FULLPATH[] = "mountpoint/some_file.txt";
110         const char RELPATH[] = "some_file.txt";
111         const char *CONTENTS = "abcdefgh";
112         uint64_t ino = 42;
113         int fd;
114         ssize_t bufsize = strlen(CONTENTS);
115         char buf[bufsize];
116         struct aiocb iocb, *piocb;
117
118         expect_lookup(RELPATH, ino, bufsize);
119         expect_open(ino, 0, 1);
120         expect_getattr(ino, bufsize);
121         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
122
123         fd = open(FULLPATH, O_RDONLY);
124         ASSERT_LE(0, fd) << strerror(errno);
125
126         iocb.aio_nbytes = bufsize;
127         iocb.aio_fildes = fd;
128         iocb.aio_buf = buf;
129         iocb.aio_offset = 0;
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));
134         /* Deliberately leak fd.  close(2) will be tested in release.cc */
135 }
136
137 /* 
138  * Without the FUSE_ASYNC_READ mount option, fuse(4) should ensure that there
139  * is at most one outstanding read operation per file handle
140  */
141 TEST_F(AioRead, async_read_disabled)
142 {
143         const char FULLPATH[] = "mountpoint/some_file.txt";
144         const char RELPATH[] = "some_file.txt";
145         uint64_t ino = 42;
146         int fd;
147         ssize_t bufsize = 50;
148         char buf0[bufsize], buf1[bufsize];
149         off_t off0 = 0;
150         off_t off1 = 4096;
151         struct aiocb iocb0, iocb1;
152
153         expect_lookup(RELPATH, ino, bufsize);
154         expect_open(ino, 0, 1);
155         expect_getattr(ino, bufsize);
156         EXPECT_CALL(*m_mock, process(
157                 ResultOf([=](auto in) {
158                         return (in->header.opcode == FUSE_READ &&
159                                 in->header.nodeid == ino &&
160                                 in->body.read.fh == FH &&
161                                 in->body.read.offset == (uint64_t)off0 &&
162                                 in->body.read.size == bufsize);
163                 }, Eq(true)),
164                 _)
165         ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
166                 /* Filesystem is slow to respond */
167         }));
168         EXPECT_CALL(*m_mock, process(
169                 ResultOf([=](auto in) {
170                         return (in->header.opcode == FUSE_READ &&
171                                 in->header.nodeid == ino &&
172                                 in->body.read.fh == FH &&
173                                 in->body.read.offset == (uint64_t)off1 &&
174                                 in->body.read.size == bufsize);
175                 }, Eq(true)),
176                 _)
177         ).Times(0);
178
179         fd = open(FULLPATH, O_RDONLY);
180         ASSERT_LE(0, fd) << strerror(errno);
181
182         /* 
183          * Submit two AIO read requests, and respond to neither.  If the
184          * filesystem ever gets the second read request, then we failed to
185          * limit outstanding reads.
186          */
187         iocb0.aio_nbytes = bufsize;
188         iocb0.aio_fildes = fd;
189         iocb0.aio_buf = buf0;
190         iocb0.aio_offset = off0;
191         iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
192         ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
193
194         iocb1.aio_nbytes = bufsize;
195         iocb1.aio_fildes = fd;
196         iocb1.aio_buf = buf1;
197         iocb1.aio_offset = off1;
198         iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
199         ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
200
201         /* 
202          * Sleep for awhile to make sure the kernel has had a chance to issue
203          * the second read, even though the first has not yet returned
204          */
205         usleep(250'000);
206         
207         /* Deliberately leak iocbs */
208         /* Deliberately leak fd.  close(2) will be tested in release.cc */
209 }
210
211 /* 
212  * With the FUSE_ASYNC_READ mount option, fuse(4) may issue multiple
213  * simultaneous read requests on the same file handle.
214  */
215 /* 
216  * Disabled because we don't yet implement FUSE_ASYNC_READ.  No bugzilla
217  * entry, because that's a feature request, not a bug.
218  */
219 TEST_F(AsyncRead, DISABLED_async_read)
220 {
221         const char FULLPATH[] = "mountpoint/some_file.txt";
222         const char RELPATH[] = "some_file.txt";
223         uint64_t ino = 42;
224         int fd;
225         ssize_t bufsize = 50;
226         char buf0[bufsize], buf1[bufsize];
227         off_t off0 = 0;
228         off_t off1 = 4096;
229         struct aiocb iocb0, iocb1;
230
231         expect_lookup(RELPATH, ino, bufsize);
232         expect_open(ino, 0, 1);
233         expect_getattr(ino, bufsize);
234         EXPECT_CALL(*m_mock, process(
235                 ResultOf([=](auto in) {
236                         return (in->header.opcode == FUSE_READ &&
237                                 in->header.nodeid == ino &&
238                                 in->body.read.fh == FH &&
239                                 in->body.read.offset == (uint64_t)off0 &&
240                                 in->body.read.size == bufsize);
241                 }, Eq(true)),
242                 _)
243         ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
244                 /* Filesystem is slow to respond */
245         }));
246         EXPECT_CALL(*m_mock, process(
247                 ResultOf([=](auto in) {
248                         return (in->header.opcode == FUSE_READ &&
249                                 in->header.nodeid == ino &&
250                                 in->body.read.fh == FH &&
251                                 in->body.read.offset == (uint64_t)off1 &&
252                                 in->body.read.size == bufsize);
253                 }, Eq(true)),
254                 _)
255         ).WillOnce(Invoke([](auto in __unused, auto &out __unused) {
256                 /* Filesystem is slow to respond */
257         }));
258
259         fd = open(FULLPATH, O_RDONLY);
260         ASSERT_LE(0, fd) << strerror(errno);
261
262         /* 
263          * Submit two AIO read requests, but respond to neither.  Ensure that
264          * we received both.
265          */
266         iocb0.aio_nbytes = bufsize;
267         iocb0.aio_fildes = fd;
268         iocb0.aio_buf = buf0;
269         iocb0.aio_offset = off0;
270         iocb0.aio_sigevent.sigev_notify = SIGEV_NONE;
271         ASSERT_EQ(0, aio_read(&iocb0)) << strerror(errno);
272
273         iocb1.aio_nbytes = bufsize;
274         iocb1.aio_fildes = fd;
275         iocb1.aio_buf = buf1;
276         iocb1.aio_offset = off1;
277         iocb1.aio_sigevent.sigev_notify = SIGEV_NONE;
278         ASSERT_EQ(0, aio_read(&iocb1)) << strerror(errno);
279
280         /* 
281          * Sleep for awhile to make sure the kernel has had a chance to issue
282          * both reads.
283          */
284         usleep(250'000);
285         
286         /* Deliberately leak iocbs */
287         /* Deliberately leak fd.  close(2) will be tested in release.cc */
288 }
289
290 /* 0-length reads shouldn't cause any confusion */
291 TEST_F(Read, direct_io_read_nothing)
292 {
293         const char FULLPATH[] = "mountpoint/some_file.txt";
294         const char RELPATH[] = "some_file.txt";
295         uint64_t ino = 42;
296         int fd;
297         uint64_t offset = 100;
298         char buf[80];
299
300         expect_lookup(RELPATH, ino, offset + 1000);
301         expect_open(ino, FOPEN_DIRECT_IO, 1);
302         expect_getattr(ino, offset + 1000);
303
304         fd = open(FULLPATH, O_RDONLY);
305         ASSERT_LE(0, fd) << strerror(errno);
306
307         ASSERT_EQ(0, pread(fd, buf, 0, offset)) << strerror(errno);
308         /* Deliberately leak fd.  close(2) will be tested in release.cc */
309 }
310
311 /* 
312  * With direct_io, reads should not fill the cache.  They should go straight to
313  * the daemon
314  */
315 TEST_F(Read, direct_io_pread)
316 {
317         const char FULLPATH[] = "mountpoint/some_file.txt";
318         const char RELPATH[] = "some_file.txt";
319         const char *CONTENTS = "abcdefgh";
320         uint64_t ino = 42;
321         int fd;
322         uint64_t offset = 100;
323         ssize_t bufsize = strlen(CONTENTS);
324         char buf[bufsize];
325
326         expect_lookup(RELPATH, ino, offset + bufsize);
327         expect_open(ino, FOPEN_DIRECT_IO, 1);
328         expect_getattr(ino, offset + bufsize);
329         expect_read(ino, offset, bufsize, bufsize, CONTENTS);
330
331         fd = open(FULLPATH, O_RDONLY);
332         ASSERT_LE(0, fd) << strerror(errno);
333
334         ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
335         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
336         /* Deliberately leak fd.  close(2) will be tested in release.cc */
337 }
338
339 /* 
340  * With direct_io, filesystems are allowed to return less data than is
341  * requested.  fuse(4) should return a short read to userland.
342  */
343 TEST_F(Read, direct_io_short_read)
344 {
345         const char FULLPATH[] = "mountpoint/some_file.txt";
346         const char RELPATH[] = "some_file.txt";
347         const char *CONTENTS = "abcdefghijklmnop";
348         uint64_t ino = 42;
349         int fd;
350         uint64_t offset = 100;
351         ssize_t bufsize = strlen(CONTENTS);
352         ssize_t halfbufsize = bufsize / 2;
353         char buf[bufsize];
354
355         expect_lookup(RELPATH, ino, offset + bufsize);
356         expect_open(ino, FOPEN_DIRECT_IO, 1);
357         expect_getattr(ino, offset + bufsize);
358         expect_read(ino, offset, bufsize, halfbufsize, CONTENTS);
359
360         fd = open(FULLPATH, O_RDONLY);
361         ASSERT_LE(0, fd) << strerror(errno);
362
363         ASSERT_EQ(halfbufsize, pread(fd, buf, bufsize, offset))
364                 << strerror(errno);
365         ASSERT_EQ(0, memcmp(buf, CONTENTS, halfbufsize));
366         /* Deliberately leak fd.  close(2) will be tested in release.cc */
367 }
368
369 TEST_F(Read, eio)
370 {
371         const char FULLPATH[] = "mountpoint/some_file.txt";
372         const char RELPATH[] = "some_file.txt";
373         const char *CONTENTS = "abcdefgh";
374         uint64_t ino = 42;
375         int fd;
376         ssize_t bufsize = strlen(CONTENTS);
377         char buf[bufsize];
378
379         expect_lookup(RELPATH, ino, bufsize);
380         expect_open(ino, 0, 1);
381         expect_getattr(ino, bufsize);
382         EXPECT_CALL(*m_mock, process(
383                 ResultOf([=](auto in) {
384                         return (in->header.opcode == FUSE_READ);
385                 }, Eq(true)),
386                 _)
387         ).WillOnce(Invoke(ReturnErrno(EIO)));
388
389         fd = open(FULLPATH, O_RDONLY);
390         ASSERT_LE(0, fd) << strerror(errno);
391
392         ASSERT_EQ(-1, read(fd, buf, bufsize)) << strerror(errno);
393         ASSERT_EQ(EIO, errno);
394         /* Deliberately leak fd.  close(2) will be tested in release.cc */
395 }
396
397 /* 
398  * With the keep_cache option, the kernel may keep its read cache across
399  * multiple open(2)s.
400  */
401 TEST_F(Read, keep_cache)
402 {
403         const char FULLPATH[] = "mountpoint/some_file.txt";
404         const char RELPATH[] = "some_file.txt";
405         const char *CONTENTS = "abcdefgh";
406         uint64_t ino = 42;
407         int fd0, fd1;
408         ssize_t bufsize = strlen(CONTENTS);
409         char buf[bufsize];
410
411         FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
412         expect_open(ino, FOPEN_KEEP_CACHE, 2);
413         expect_getattr(ino, bufsize);
414         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
415
416         fd0 = open(FULLPATH, O_RDONLY);
417         ASSERT_LE(0, fd0) << strerror(errno);
418         ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
419
420         fd1 = open(FULLPATH, O_RDWR);
421         ASSERT_LE(0, fd1) << strerror(errno);
422
423         /*
424          * This read should be serviced by cache, even though it's on the other
425          * file descriptor
426          */
427         ASSERT_EQ(bufsize, read(fd1, buf, bufsize)) << strerror(errno);
428
429         /* Deliberately leak fd0 and fd1. */
430 }
431
432 /* 
433  * Without the keep_cache option, the kernel should drop its read caches on
434  * every open
435  */
436 TEST_F(Read, keep_cache_disabled)
437 {
438         const char FULLPATH[] = "mountpoint/some_file.txt";
439         const char RELPATH[] = "some_file.txt";
440         const char *CONTENTS = "abcdefgh";
441         uint64_t ino = 42;
442         int fd0, fd1;
443         ssize_t bufsize = strlen(CONTENTS);
444         char buf[bufsize];
445
446         FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, bufsize, 2);
447         expect_open(ino, 0, 2);
448         expect_getattr(ino, bufsize);
449         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
450
451         fd0 = open(FULLPATH, O_RDONLY);
452         ASSERT_LE(0, fd0) << strerror(errno);
453         ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
454
455         fd1 = open(FULLPATH, O_RDWR);
456         ASSERT_LE(0, fd1) << strerror(errno);
457
458         /*
459          * This read should not be serviced by cache, even though it's on the
460          * original file descriptor
461          */
462         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
463         ASSERT_EQ(0, lseek(fd0, 0, SEEK_SET)) << strerror(errno);
464         ASSERT_EQ(bufsize, read(fd0, buf, bufsize)) << strerror(errno);
465
466         /* Deliberately leak fd0 and fd1. */
467 }
468
469 TEST_F(ReadCacheable, mmap)
470 {
471         const char FULLPATH[] = "mountpoint/some_file.txt";
472         const char RELPATH[] = "some_file.txt";
473         const char *CONTENTS = "abcdefgh";
474         uint64_t ino = 42;
475         int fd;
476         ssize_t len;
477         ssize_t bufsize = strlen(CONTENTS);
478         void *p;
479         //char buf[bufsize];
480
481         len = getpagesize();
482
483         expect_lookup(RELPATH, ino, bufsize);
484         expect_open(ino, 0, 1);
485         expect_getattr(ino, bufsize);
486         /* mmap may legitimately try to read more data than is available */
487         EXPECT_CALL(*m_mock, process(
488                 ResultOf([=](auto in) {
489                         return (in->header.opcode == FUSE_READ &&
490                                 in->header.nodeid == ino &&
491                                 in->body.read.fh == Read::FH &&
492                                 in->body.read.offset == 0 &&
493                                 in->body.read.size >= bufsize);
494                 }, Eq(true)),
495                 _)
496         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
497                 out->header.len = sizeof(struct fuse_out_header) + bufsize;
498                 memmove(out->body.bytes, CONTENTS, bufsize);
499         })));
500
501         fd = open(FULLPATH, O_RDONLY);
502         ASSERT_LE(0, fd) << strerror(errno);
503
504         p = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
505         ASSERT_NE(MAP_FAILED, p) << strerror(errno);
506
507         ASSERT_EQ(0, memcmp(p, CONTENTS, bufsize));
508
509         ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
510         /* Deliberately leak fd.  close(2) will be tested in release.cc */
511 }
512
513 /*
514  * Just as when FOPEN_DIRECT_IO is used, reads with O_DIRECT should bypass
515  * cache and to straight to the daemon
516  */
517 TEST_F(Read, o_direct)
518 {
519         const char FULLPATH[] = "mountpoint/some_file.txt";
520         const char RELPATH[] = "some_file.txt";
521         const char *CONTENTS = "abcdefgh";
522         uint64_t ino = 42;
523         int fd;
524         ssize_t bufsize = strlen(CONTENTS);
525         char buf[bufsize];
526
527         expect_lookup(RELPATH, ino, bufsize);
528         expect_open(ino, 0, 1);
529         expect_getattr(ino, bufsize);
530         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
531
532         fd = open(FULLPATH, O_RDONLY);
533         ASSERT_LE(0, fd) << strerror(errno);
534
535         // Fill the cache
536         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
537         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
538
539         // Reads with o_direct should bypass the cache
540         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
541         ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
542         ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
543         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
544         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
545         
546         /* Deliberately leak fd.  close(2) will be tested in release.cc */
547 }
548
549 TEST_F(Read, pread)
550 {
551         const char FULLPATH[] = "mountpoint/some_file.txt";
552         const char RELPATH[] = "some_file.txt";
553         const char *CONTENTS = "abcdefgh";
554         uint64_t ino = 42;
555         int fd;
556         /* 
557          * Set offset to a maxbcachebuf boundary so we'll be sure what offset
558          * to read from.  Without this, the read might start at a lower offset.
559          */
560         uint64_t offset = m_maxbcachebuf;
561         ssize_t bufsize = strlen(CONTENTS);
562         char buf[bufsize];
563
564         expect_lookup(RELPATH, ino, offset + bufsize);
565         expect_open(ino, 0, 1);
566         expect_getattr(ino, offset + bufsize);
567         expect_read(ino, offset, bufsize, bufsize, CONTENTS);
568
569         fd = open(FULLPATH, O_RDONLY);
570         ASSERT_LE(0, fd) << strerror(errno);
571
572         ASSERT_EQ(bufsize, pread(fd, buf, bufsize, offset)) << strerror(errno);
573         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
574         /* Deliberately leak fd.  close(2) will be tested in release.cc */
575 }
576
577 TEST_F(Read, read)
578 {
579         const char FULLPATH[] = "mountpoint/some_file.txt";
580         const char RELPATH[] = "some_file.txt";
581         const char *CONTENTS = "abcdefgh";
582         uint64_t ino = 42;
583         int fd;
584         ssize_t bufsize = strlen(CONTENTS);
585         char buf[bufsize];
586
587         expect_lookup(RELPATH, ino, bufsize);
588         expect_open(ino, 0, 1);
589         expect_getattr(ino, bufsize);
590         expect_read(ino, 0, bufsize, bufsize, CONTENTS);
591
592         fd = open(FULLPATH, O_RDONLY);
593         ASSERT_LE(0, fd) << strerror(errno);
594
595         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
596         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
597
598         /* Deliberately leak fd.  close(2) will be tested in release.cc */
599 }
600
601 /* If the filesystem allows it, the kernel should try to readahead */
602 TEST_F(ReadCacheable, default_readahead)
603 {
604         const char FULLPATH[] = "mountpoint/some_file.txt";
605         const char RELPATH[] = "some_file.txt";
606         const char *CONTENTS0 = "abcdefghijklmnop";
607         uint64_t ino = 42;
608         int fd;
609         ssize_t bufsize = 8;
610         /* hard-coded in fuse_internal.c */
611         size_t default_maxreadahead = 65536;
612         ssize_t filesize = default_maxreadahead * 2;
613         char *contents;
614         char buf[bufsize];
615         const char *contents1 = CONTENTS0 + bufsize;
616
617         contents = (char*)calloc(1, filesize);
618         ASSERT_NE(NULL, contents);
619         memmove(contents, CONTENTS0, strlen(CONTENTS0));
620
621         expect_lookup(RELPATH, ino, filesize);
622         expect_open(ino, 0, 1);
623         expect_getattr(ino, filesize);
624         expect_read(ino, 0, default_maxreadahead, default_maxreadahead,
625                 contents);
626
627         fd = open(FULLPATH, O_RDONLY);
628         ASSERT_LE(0, fd) << strerror(errno);
629
630         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
631         ASSERT_EQ(0, memcmp(buf, CONTENTS0, bufsize));
632
633         /* A subsequent read should be serviced by cache */
634         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
635         ASSERT_EQ(0, memcmp(buf, contents1, bufsize));
636         /* Deliberately leak fd.  close(2) will be tested in release.cc */
637 }
638
639 /* Reading with sendfile should work (though it obviously won't be 0-copy) */
640 TEST_F(ReadCacheable, sendfile)
641 {
642         const char FULLPATH[] = "mountpoint/some_file.txt";
643         const char RELPATH[] = "some_file.txt";
644         const char *CONTENTS = "abcdefgh";
645         uint64_t ino = 42;
646         int fd;
647         ssize_t bufsize = strlen(CONTENTS);
648         char buf[bufsize];
649         int sp[2];
650         off_t sbytes;
651
652         expect_lookup(RELPATH, ino, bufsize);
653         expect_open(ino, 0, 1);
654         expect_getattr(ino, bufsize);
655         /* Like mmap, sendfile may request more data than is available */
656         EXPECT_CALL(*m_mock, process(
657                 ResultOf([=](auto in) {
658                         return (in->header.opcode == FUSE_READ &&
659                                 in->header.nodeid == ino &&
660                                 in->body.read.fh == Read::FH &&
661                                 in->body.read.offset == 0 &&
662                                 in->body.read.size >= bufsize);
663                 }, Eq(true)),
664                 _)
665         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
666                 out->header.len = sizeof(struct fuse_out_header) + bufsize;
667                 memmove(out->body.bytes, CONTENTS, bufsize);
668         })));
669
670         ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
671                 << strerror(errno);
672         fd = open(FULLPATH, O_RDONLY);
673         ASSERT_LE(0, fd) << strerror(errno);
674
675         ASSERT_EQ(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0))
676                 << strerror(errno);
677         ASSERT_EQ(bufsize, read(sp[0], buf, bufsize)) << strerror(errno);
678         ASSERT_EQ(0, memcmp(buf, CONTENTS, bufsize));
679
680         close(sp[1]);
681         close(sp[0]);
682         /* Deliberately leak fd.  close(2) will be tested in release.cc */
683 }
684
685 /* sendfile should fail gracefully if fuse declines the read */
686 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236466 */
687 TEST_F(ReadCacheable, DISABLED_sendfile_eio)
688 {
689         const char FULLPATH[] = "mountpoint/some_file.txt";
690         const char RELPATH[] = "some_file.txt";
691         const char *CONTENTS = "abcdefgh";
692         uint64_t ino = 42;
693         int fd;
694         ssize_t bufsize = strlen(CONTENTS);
695         int sp[2];
696         off_t sbytes;
697
698         expect_lookup(RELPATH, ino, bufsize);
699         expect_open(ino, 0, 1);
700         expect_getattr(ino, bufsize);
701         EXPECT_CALL(*m_mock, process(
702                 ResultOf([=](auto in) {
703                         return (in->header.opcode == FUSE_READ);
704                 }, Eq(true)),
705                 _)
706         ).WillOnce(Invoke(ReturnErrno(EIO)));
707
708         ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_STREAM, 0, sp))
709                 << strerror(errno);
710         fd = open(FULLPATH, O_RDONLY);
711         ASSERT_LE(0, fd) << strerror(errno);
712
713         ASSERT_NE(0, sendfile(fd, sp[1], 0, bufsize, NULL, &sbytes, 0));
714
715         close(sp[1]);
716         close(sp[0]);
717         /* Deliberately leak fd.  close(2) will be tested in release.cc */
718 }
719
720 /* fuse(4) should honor the filesystem's requested m_readahead parameter */
721 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236472 */
722 TEST_P(ReadAhead, DISABLED_readahead) {
723         const char FULLPATH[] = "mountpoint/some_file.txt";
724         const char RELPATH[] = "some_file.txt";
725         const char *CONTENTS0 = "abcdefghijklmnop";
726         uint64_t ino = 42;
727         int fd;
728         ssize_t bufsize = 8;
729         ssize_t filesize = m_maxbcachebuf * 2;
730         char *contents;
731         char buf[bufsize];
732
733         ASSERT_TRUE(GetParam() < (uint32_t)m_maxbcachebuf)
734                 << "Test assumes that max_readahead < maxbcachebuf";
735
736         contents = (char*)calloc(1, filesize);
737         ASSERT_NE(NULL, contents);
738         memmove(contents, CONTENTS0, strlen(CONTENTS0));
739
740         expect_lookup(RELPATH, ino, filesize);
741         expect_open(ino, 0, 1);
742         expect_getattr(ino, filesize);
743         /* fuse(4) should only read ahead the allowed amount */
744         expect_read(ino, 0, GetParam(), GetParam(), contents);
745
746         fd = open(FULLPATH, O_RDONLY);
747         ASSERT_LE(0, fd) << strerror(errno);
748
749         ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
750         ASSERT_EQ(0, memcmp(buf, CONTENTS0, bufsize));
751
752         /* Deliberately leak fd.  close(2) will be tested in release.cc */
753 }
754
755 INSTANTIATE_TEST_CASE_P(RA, ReadAhead, ::testing::Values(0u, 2048u));