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 * $FreeBSD$
31 */
32
33 extern "C" {
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/uio.h>
40
41 #include <aio.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <unistd.h>
45 }
46
47 #include "mockfs.hh"
48 #include "utils.hh"
49
50 using namespace testing;
51
52 class Write: public FuseTest {
53
54 public:
55 static sig_atomic_t s_sigxfsz;
56
SetUp()57 void SetUp() {
58 s_sigxfsz = 0;
59 FuseTest::SetUp();
60 }
61
TearDown()62 void TearDown() {
63 struct sigaction sa;
64
65 bzero(&sa, sizeof(sa));
66 sa.sa_handler = SIG_DFL;
67 sigaction(SIGXFSZ, &sa, NULL);
68
69 FuseTest::TearDown();
70 }
71
expect_lookup(const char * relpath,uint64_t ino,uint64_t size)72 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
73 {
74 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
75 }
76
expect_release(uint64_t ino,ProcessMockerT r)77 void expect_release(uint64_t ino, ProcessMockerT r)
78 {
79 EXPECT_CALL(*m_mock, process(
80 ResultOf([=](auto in) {
81 return (in.header.opcode == FUSE_RELEASE &&
82 in.header.nodeid == ino);
83 }, Eq(true)),
84 _)
85 ).WillRepeatedly(Invoke(r));
86 }
87
expect_write(uint64_t ino,uint64_t offset,uint64_t isize,uint64_t osize,const void * contents)88 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
89 uint64_t osize, const void *contents)
90 {
91 FuseTest::expect_write(ino, offset, isize, osize, 0, 0, contents);
92 }
93
94 /* Expect a write that may or may not come, depending on the cache mode */
maybe_expect_write(uint64_t ino,uint64_t offset,uint64_t size,const void * contents)95 void maybe_expect_write(uint64_t ino, uint64_t offset, uint64_t size,
96 const void *contents)
97 {
98 EXPECT_CALL(*m_mock, process(
99 ResultOf([=](auto in) {
100 const char *buf = (const char*)in.body.bytes +
101 sizeof(struct fuse_write_in);
102
103 return (in.header.opcode == FUSE_WRITE &&
104 in.header.nodeid == ino &&
105 in.body.write.offset == offset &&
106 in.body.write.size == size &&
107 0 == bcmp(buf, contents, size));
108 }, Eq(true)),
109 _)
110 ).Times(AtMost(1))
111 .WillRepeatedly(Invoke(
112 ReturnImmediate([=](auto in __unused, auto& out) {
113 SET_OUT_HEADER_LEN(out, write);
114 out.body.write.size = size;
115 })
116 ));
117 }
118
119 };
120
121 sig_atomic_t Write::s_sigxfsz = 0;
122
123 class Write_7_8: public FuseTest {
124
125 public:
SetUp()126 virtual void SetUp() {
127 m_kernel_minor_version = 8;
128 FuseTest::SetUp();
129 }
130
expect_lookup(const char * relpath,uint64_t ino,uint64_t size)131 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
132 {
133 FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
134 }
135
136 };
137
138 class AioWrite: public Write {
SetUp()139 virtual void SetUp() {
140 if (!is_unsafe_aio_enabled())
141 GTEST_SKIP() <<
142 "vfs.aio.enable_unsafe must be set for this test";
143 FuseTest::SetUp();
144 }
145 };
146
147 /* Tests for the writeback cache mode */
148 class WriteBack: public Write {
149 public:
SetUp()150 virtual void SetUp() {
151 m_init_flags |= FUSE_WRITEBACK_CACHE;
152 FuseTest::SetUp();
153 if (IsSkipped())
154 return;
155 }
156
expect_write(uint64_t ino,uint64_t offset,uint64_t isize,uint64_t osize,const void * contents)157 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
158 uint64_t osize, const void *contents)
159 {
160 FuseTest::expect_write(ino, offset, isize, osize, FUSE_WRITE_CACHE, 0,
161 contents);
162 }
163 };
164
165 class WriteBackAsync: public WriteBack {
166 public:
SetUp()167 virtual void SetUp() {
168 m_async = true;
169 WriteBack::SetUp();
170 }
171 };
172
173 class TimeGran: public WriteBackAsync, public WithParamInterface<unsigned> {
174 public:
SetUp()175 virtual void SetUp() {
176 m_time_gran = 1 << GetParam();
177 WriteBackAsync::SetUp();
178 }
179 };
180
181 /* Tests for clustered writes with WriteBack cacheing */
182 class WriteCluster: public WriteBack {
183 public:
SetUp()184 virtual void SetUp() {
185 m_async = true;
186 m_maxwrite = 1 << 25; // Anything larger than MAXPHYS will suffice
187 WriteBack::SetUp();
188 if (m_maxphys < 2 * DFLTPHYS)
189 GTEST_SKIP() << "MAXPHYS must be at least twice DFLTPHYS"
190 << " for this test";
191 if (m_maxphys < 2 * m_maxbcachebuf)
192 GTEST_SKIP() << "MAXPHYS must be at least twice maxbcachebuf"
193 << " for this test";
194 }
195 };
196
sigxfsz_handler(int __unused sig)197 void sigxfsz_handler(int __unused sig) {
198 Write::s_sigxfsz = 1;
199 }
200
201 /* AIO writes need to set the header's pid field correctly */
202 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
TEST_F(AioWrite,DISABLED_aio_write)203 TEST_F(AioWrite, DISABLED_aio_write)
204 {
205 const char FULLPATH[] = "mountpoint/some_file.txt";
206 const char RELPATH[] = "some_file.txt";
207 const char *CONTENTS = "abcdefgh";
208 uint64_t ino = 42;
209 uint64_t offset = 4096;
210 int fd;
211 ssize_t bufsize = strlen(CONTENTS);
212 struct aiocb iocb, *piocb;
213
214 expect_lookup(RELPATH, ino, 0);
215 expect_open(ino, 0, 1);
216 expect_write(ino, offset, bufsize, bufsize, CONTENTS);
217
218 fd = open(FULLPATH, O_WRONLY);
219 EXPECT_LE(0, fd) << strerror(errno);
220
221 iocb.aio_nbytes = bufsize;
222 iocb.aio_fildes = fd;
223 iocb.aio_buf = __DECONST(void *, CONTENTS);
224 iocb.aio_offset = offset;
225 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
226 ASSERT_EQ(0, aio_write(&iocb)) << strerror(errno);
227 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
228 leak(fd);
229 }
230
231 /*
232 * When a file is opened with O_APPEND, we should forward that flag to
233 * FUSE_OPEN (tested by Open.o_append) but still attempt to calculate the
234 * offset internally. That way we'll work both with filesystems that
235 * understand O_APPEND (and ignore the offset) and filesystems that don't (and
236 * simply use the offset).
237 *
238 * Note that verifying the O_APPEND flag in FUSE_OPEN is done in the
239 * Open.o_append test.
240 */
TEST_F(Write,append)241 TEST_F(Write, append)
242 {
243 const ssize_t BUFSIZE = 9;
244 const char FULLPATH[] = "mountpoint/some_file.txt";
245 const char RELPATH[] = "some_file.txt";
246 const char CONTENTS[BUFSIZE] = "abcdefgh";
247 uint64_t ino = 42;
248 /*
249 * Set offset to a maxbcachebuf boundary so we don't need to RMW when
250 * using writeback caching
251 */
252 uint64_t initial_offset = m_maxbcachebuf;
253 int fd;
254
255 expect_lookup(RELPATH, ino, initial_offset);
256 expect_open(ino, 0, 1);
257 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
258
259 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
260 fd = open(FULLPATH, O_RDWR | O_APPEND);
261 EXPECT_LE(0, fd) << strerror(errno);
262
263 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
264 leak(fd);
265 }
266
267 /* If a file is cached, then appending to the end should not cause a read */
TEST_F(Write,append_to_cached)268 TEST_F(Write, append_to_cached)
269 {
270 const ssize_t BUFSIZE = 9;
271 const char FULLPATH[] = "mountpoint/some_file.txt";
272 const char RELPATH[] = "some_file.txt";
273 char *oldcontents, *oldbuf;
274 const char CONTENTS[BUFSIZE] = "abcdefgh";
275 uint64_t ino = 42;
276 /*
277 * Set offset in between maxbcachebuf boundary to test buffer handling
278 */
279 uint64_t oldsize = m_maxbcachebuf / 2;
280 int fd;
281
282 oldcontents = (char*)calloc(1, oldsize);
283 ASSERT_NE(nullptr, oldcontents) << strerror(errno);
284 oldbuf = (char*)malloc(oldsize);
285 ASSERT_NE(nullptr, oldbuf) << strerror(errno);
286
287 expect_lookup(RELPATH, ino, oldsize);
288 expect_open(ino, 0, 1);
289 expect_read(ino, 0, oldsize, oldsize, oldcontents);
290 maybe_expect_write(ino, oldsize, BUFSIZE, CONTENTS);
291
292 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
293 fd = open(FULLPATH, O_RDWR | O_APPEND);
294 EXPECT_LE(0, fd) << strerror(errno);
295
296 /* Read the old data into the cache */
297 ASSERT_EQ((ssize_t)oldsize, read(fd, oldbuf, oldsize))
298 << strerror(errno);
299
300 /* Write the new data. There should be no more read operations */
301 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
302 leak(fd);
303 }
304
TEST_F(Write,append_direct_io)305 TEST_F(Write, append_direct_io)
306 {
307 const ssize_t BUFSIZE = 9;
308 const char FULLPATH[] = "mountpoint/some_file.txt";
309 const char RELPATH[] = "some_file.txt";
310 const char CONTENTS[BUFSIZE] = "abcdefgh";
311 uint64_t ino = 42;
312 uint64_t initial_offset = 4096;
313 int fd;
314
315 expect_lookup(RELPATH, ino, initial_offset);
316 expect_open(ino, FOPEN_DIRECT_IO, 1);
317 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
318
319 fd = open(FULLPATH, O_WRONLY | O_APPEND);
320 EXPECT_LE(0, fd) << strerror(errno);
321
322 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
323 leak(fd);
324 }
325
326 /* A direct write should evict any overlapping cached data */
TEST_F(Write,direct_io_evicts_cache)327 TEST_F(Write, direct_io_evicts_cache)
328 {
329 const char FULLPATH[] = "mountpoint/some_file.txt";
330 const char RELPATH[] = "some_file.txt";
331 const char CONTENTS0[] = "abcdefgh";
332 const char CONTENTS1[] = "ijklmnop";
333 uint64_t ino = 42;
334 int fd;
335 ssize_t bufsize = strlen(CONTENTS0) + 1;
336 char readbuf[bufsize];
337
338 expect_lookup(RELPATH, ino, bufsize);
339 expect_open(ino, 0, 1);
340 expect_read(ino, 0, bufsize, bufsize, CONTENTS0);
341 expect_write(ino, 0, bufsize, bufsize, CONTENTS1);
342
343 fd = open(FULLPATH, O_RDWR);
344 EXPECT_LE(0, fd) << strerror(errno);
345
346 // Prime cache
347 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
348
349 // Write directly, evicting cache
350 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
351 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
352 ASSERT_EQ(bufsize, write(fd, CONTENTS1, bufsize)) << strerror(errno);
353
354 // Read again. Cache should be bypassed
355 expect_read(ino, 0, bufsize, bufsize, CONTENTS1);
356 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
357 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
358 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
359 ASSERT_STREQ(readbuf, CONTENTS1);
360
361 leak(fd);
362 }
363
364 /*
365 * If the server doesn't return FOPEN_DIRECT_IO during FUSE_OPEN, then it's not
366 * allowed to return a short write for that file handle. However, if it does
367 * then we should still do our darndest to handle it by resending the unwritten
368 * portion.
369 */
TEST_F(Write,indirect_io_short_write)370 TEST_F(Write, indirect_io_short_write)
371 {
372 const char FULLPATH[] = "mountpoint/some_file.txt";
373 const char RELPATH[] = "some_file.txt";
374 const char *CONTENTS = "abcdefghijklmnop";
375 uint64_t ino = 42;
376 int fd;
377 ssize_t bufsize = strlen(CONTENTS);
378 ssize_t bufsize0 = 11;
379 ssize_t bufsize1 = strlen(CONTENTS) - bufsize0;
380 const char *contents1 = CONTENTS + bufsize0;
381
382 expect_lookup(RELPATH, ino, 0);
383 expect_open(ino, 0, 1);
384 expect_write(ino, 0, bufsize, bufsize0, CONTENTS);
385 expect_write(ino, bufsize0, bufsize1, bufsize1, contents1);
386
387 fd = open(FULLPATH, O_WRONLY);
388 EXPECT_LE(0, fd) << strerror(errno);
389
390 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
391 leak(fd);
392 }
393
394 /*
395 * When the direct_io option is used, filesystems are allowed to write less
396 * data than requested. We should return the short write to userland.
397 */
TEST_F(Write,direct_io_short_write)398 TEST_F(Write, direct_io_short_write)
399 {
400 const char FULLPATH[] = "mountpoint/some_file.txt";
401 const char RELPATH[] = "some_file.txt";
402 const char *CONTENTS = "abcdefghijklmnop";
403 uint64_t ino = 42;
404 int fd;
405 ssize_t bufsize = strlen(CONTENTS);
406 ssize_t halfbufsize = bufsize / 2;
407
408 expect_lookup(RELPATH, ino, 0);
409 expect_open(ino, FOPEN_DIRECT_IO, 1);
410 expect_write(ino, 0, bufsize, halfbufsize, CONTENTS);
411
412 fd = open(FULLPATH, O_WRONLY);
413 EXPECT_LE(0, fd) << strerror(errno);
414
415 ASSERT_EQ(halfbufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
416 leak(fd);
417 }
418
419 /*
420 * An insidious edge case: the filesystem returns a short write, and the
421 * difference between what we requested and what it actually wrote crosses an
422 * iov element boundary
423 */
TEST_F(Write,direct_io_short_write_iov)424 TEST_F(Write, direct_io_short_write_iov)
425 {
426 const char FULLPATH[] = "mountpoint/some_file.txt";
427 const char RELPATH[] = "some_file.txt";
428 const char *CONTENTS0 = "abcdefgh";
429 const char *CONTENTS1 = "ijklmnop";
430 const char *EXPECTED0 = "abcdefghijklmnop";
431 uint64_t ino = 42;
432 int fd;
433 ssize_t size0 = strlen(CONTENTS0) - 1;
434 ssize_t size1 = strlen(CONTENTS1) + 1;
435 ssize_t totalsize = size0 + size1;
436 struct iovec iov[2];
437
438 expect_lookup(RELPATH, ino, 0);
439 expect_open(ino, FOPEN_DIRECT_IO, 1);
440 expect_write(ino, 0, totalsize, size0, EXPECTED0);
441
442 fd = open(FULLPATH, O_WRONLY);
443 EXPECT_LE(0, fd) << strerror(errno);
444
445 iov[0].iov_base = __DECONST(void*, CONTENTS0);
446 iov[0].iov_len = strlen(CONTENTS0);
447 iov[1].iov_base = __DECONST(void*, CONTENTS1);
448 iov[1].iov_len = strlen(CONTENTS1);
449 ASSERT_EQ(size0, writev(fd, iov, 2)) << strerror(errno);
450 leak(fd);
451 }
452
453 /* fusefs should respect RLIMIT_FSIZE */
TEST_F(Write,rlimit_fsize)454 TEST_F(Write, rlimit_fsize)
455 {
456 const char FULLPATH[] = "mountpoint/some_file.txt";
457 const char RELPATH[] = "some_file.txt";
458 const char *CONTENTS = "abcdefgh";
459 struct rlimit rl;
460 ssize_t bufsize = strlen(CONTENTS);
461 off_t offset = 1'000'000'000;
462 uint64_t ino = 42;
463 int fd;
464
465 expect_lookup(RELPATH, ino, 0);
466 expect_open(ino, 0, 1);
467
468 rl.rlim_cur = offset;
469 rl.rlim_max = 10 * offset;
470 ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rl)) << strerror(errno);
471 ASSERT_NE(SIG_ERR, signal(SIGXFSZ, sigxfsz_handler)) << strerror(errno);
472
473 fd = open(FULLPATH, O_WRONLY);
474
475 EXPECT_LE(0, fd) << strerror(errno);
476
477 ASSERT_EQ(-1, pwrite(fd, CONTENTS, bufsize, offset));
478 EXPECT_EQ(EFBIG, errno);
479 EXPECT_EQ(1, s_sigxfsz);
480 leak(fd);
481 }
482
483 /*
484 * A short read indicates EOF. Test that nothing bad happens if we get EOF
485 * during the R of a RMW operation.
486 */
TEST_F(Write,eof_during_rmw)487 TEST_F(Write, eof_during_rmw)
488 {
489 const char FULLPATH[] = "mountpoint/some_file.txt";
490 const char RELPATH[] = "some_file.txt";
491 const char *CONTENTS = "abcdefgh";
492 const char *INITIAL = "XXXXXXXXXX";
493 uint64_t ino = 42;
494 uint64_t offset = 1;
495 ssize_t bufsize = strlen(CONTENTS);
496 off_t orig_fsize = 10;
497 off_t truncated_fsize = 5;
498 off_t final_fsize = bufsize;
499 int fd;
500
501 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, orig_fsize, 1);
502 expect_open(ino, 0, 1);
503 expect_read(ino, 0, orig_fsize, truncated_fsize, INITIAL, O_RDWR);
504 expect_getattr(ino, truncated_fsize);
505 expect_read(ino, 0, final_fsize, final_fsize, INITIAL, O_RDWR);
506 maybe_expect_write(ino, offset, bufsize, CONTENTS);
507
508 fd = open(FULLPATH, O_RDWR);
509 EXPECT_LE(0, fd) << strerror(errno);
510
511 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
512 << strerror(errno);
513 leak(fd);
514 }
515
516 /*
517 * If the kernel cannot be sure which uid, gid, or pid was responsible for a
518 * write, then it must set the FUSE_WRITE_CACHE bit
519 */
520 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236378 */
TEST_F(Write,mmap)521 TEST_F(Write, mmap)
522 {
523 const char FULLPATH[] = "mountpoint/some_file.txt";
524 const char RELPATH[] = "some_file.txt";
525 const char *CONTENTS = "abcdefgh";
526 uint64_t ino = 42;
527 int fd;
528 ssize_t bufsize = strlen(CONTENTS);
529 void *p;
530 uint64_t offset = 10;
531 size_t len;
532 void *zeros, *expected;
533
534 len = getpagesize();
535
536 zeros = calloc(1, len);
537 ASSERT_NE(nullptr, zeros);
538 expected = calloc(1, len);
539 ASSERT_NE(nullptr, expected);
540 memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
541
542 expect_lookup(RELPATH, ino, len);
543 expect_open(ino, 0, 1);
544 expect_read(ino, 0, len, len, zeros);
545 /*
546 * Writes from the pager may or may not be associated with the correct
547 * pid, so they must set FUSE_WRITE_CACHE.
548 */
549 FuseTest::expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, 0, expected);
550 expect_flush(ino, 1, ReturnErrno(0));
551 expect_release(ino, ReturnErrno(0));
552
553 fd = open(FULLPATH, O_RDWR);
554 EXPECT_LE(0, fd) << strerror(errno);
555
556 p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
557 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
558
559 memmove((uint8_t*)p + offset, CONTENTS, bufsize);
560
561 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
562 close(fd); // Write mmap'd data on close
563
564 free(expected);
565 free(zeros);
566
567 leak(fd);
568 }
569
TEST_F(Write,pwrite)570 TEST_F(Write, pwrite)
571 {
572 const char FULLPATH[] = "mountpoint/some_file.txt";
573 const char RELPATH[] = "some_file.txt";
574 const char *CONTENTS = "abcdefgh";
575 uint64_t ino = 42;
576 uint64_t offset = m_maxbcachebuf;
577 int fd;
578 ssize_t bufsize = strlen(CONTENTS);
579
580 expect_lookup(RELPATH, ino, 0);
581 expect_open(ino, 0, 1);
582 expect_write(ino, offset, bufsize, bufsize, CONTENTS);
583
584 fd = open(FULLPATH, O_WRONLY);
585 EXPECT_LE(0, fd) << strerror(errno);
586
587 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
588 << strerror(errno);
589 leak(fd);
590 }
591
592 /* Writing a file should update its cached mtime and ctime */
TEST_F(Write,timestamps)593 TEST_F(Write, timestamps)
594 {
595 const char FULLPATH[] = "mountpoint/some_file.txt";
596 const char RELPATH[] = "some_file.txt";
597 const char *CONTENTS = "abcdefgh";
598 ssize_t bufsize = strlen(CONTENTS);
599 uint64_t ino = 42;
600 struct stat sb0, sb1;
601 int fd;
602
603 expect_lookup(RELPATH, ino, 0);
604 expect_open(ino, 0, 1);
605 maybe_expect_write(ino, 0, bufsize, CONTENTS);
606
607 fd = open(FULLPATH, O_RDWR);
608 EXPECT_LE(0, fd) << strerror(errno);
609 ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno);
610 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
611
612 nap();
613
614 ASSERT_EQ(0, fstat(fd, &sb1)) << strerror(errno);
615
616 EXPECT_EQ(sb0.st_atime, sb1.st_atime);
617 EXPECT_NE(sb0.st_mtime, sb1.st_mtime);
618 EXPECT_NE(sb0.st_ctime, sb1.st_ctime);
619
620 leak(fd);
621 }
622
TEST_F(Write,write)623 TEST_F(Write, write)
624 {
625 const char FULLPATH[] = "mountpoint/some_file.txt";
626 const char RELPATH[] = "some_file.txt";
627 const char *CONTENTS = "abcdefgh";
628 uint64_t ino = 42;
629 int fd;
630 ssize_t bufsize = strlen(CONTENTS);
631
632 expect_lookup(RELPATH, ino, 0);
633 expect_open(ino, 0, 1);
634 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
635
636 fd = open(FULLPATH, O_WRONLY);
637 EXPECT_LE(0, fd) << strerror(errno);
638
639 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
640 leak(fd);
641 }
642
643 /* fuse(4) should not issue writes of greater size than the daemon requests */
TEST_F(Write,write_large)644 TEST_F(Write, write_large)
645 {
646 const char FULLPATH[] = "mountpoint/some_file.txt";
647 const char RELPATH[] = "some_file.txt";
648 int *contents;
649 uint64_t ino = 42;
650 int fd;
651 ssize_t halfbufsize, bufsize;
652
653 halfbufsize = m_mock->m_maxwrite;
654 bufsize = halfbufsize * 2;
655 contents = (int*)malloc(bufsize);
656 ASSERT_NE(nullptr, contents);
657 for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
658 contents[i] = i;
659 }
660
661 expect_lookup(RELPATH, ino, 0);
662 expect_open(ino, 0, 1);
663 maybe_expect_write(ino, 0, halfbufsize, contents);
664 maybe_expect_write(ino, halfbufsize, halfbufsize,
665 &contents[halfbufsize / sizeof(int)]);
666
667 fd = open(FULLPATH, O_WRONLY);
668 EXPECT_LE(0, fd) << strerror(errno);
669
670 ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
671 leak(fd);
672
673 free(contents);
674 }
675
TEST_F(Write,write_nothing)676 TEST_F(Write, write_nothing)
677 {
678 const char FULLPATH[] = "mountpoint/some_file.txt";
679 const char RELPATH[] = "some_file.txt";
680 const char *CONTENTS = "";
681 uint64_t ino = 42;
682 int fd;
683 ssize_t bufsize = 0;
684
685 expect_lookup(RELPATH, ino, 0);
686 expect_open(ino, 0, 1);
687
688 fd = open(FULLPATH, O_WRONLY);
689 EXPECT_LE(0, fd) << strerror(errno);
690
691 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
692 leak(fd);
693 }
694
TEST_F(Write_7_8,write)695 TEST_F(Write_7_8, write)
696 {
697 const char FULLPATH[] = "mountpoint/some_file.txt";
698 const char RELPATH[] = "some_file.txt";
699 const char *CONTENTS = "abcdefgh";
700 uint64_t ino = 42;
701 int fd;
702 ssize_t bufsize = strlen(CONTENTS);
703
704 expect_lookup(RELPATH, ino, 0);
705 expect_open(ino, 0, 1);
706 expect_write_7_8(ino, 0, bufsize, bufsize, CONTENTS);
707
708 fd = open(FULLPATH, O_WRONLY);
709 EXPECT_LE(0, fd) << strerror(errno);
710
711 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
712 leak(fd);
713 }
714
715 /* In writeback mode, dirty data should be written on close */
TEST_F(WriteBackAsync,close)716 TEST_F(WriteBackAsync, close)
717 {
718 const char FULLPATH[] = "mountpoint/some_file.txt";
719 const char RELPATH[] = "some_file.txt";
720 const char *CONTENTS = "abcdefgh";
721 uint64_t ino = 42;
722 int fd;
723 ssize_t bufsize = strlen(CONTENTS);
724
725 expect_lookup(RELPATH, ino, 0);
726 expect_open(ino, 0, 1);
727 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
728 EXPECT_CALL(*m_mock, process(
729 ResultOf([=](auto in) {
730 return (in.header.opcode == FUSE_SETATTR);
731 }, Eq(true)),
732 _)
733 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
734 SET_OUT_HEADER_LEN(out, attr);
735 out.body.attr.attr.ino = ino; // Must match nodeid
736 })));
737 expect_flush(ino, 1, ReturnErrno(0));
738 expect_release(ino, ReturnErrno(0));
739
740 fd = open(FULLPATH, O_RDWR);
741 ASSERT_LE(0, fd) << strerror(errno);
742
743 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
744 close(fd);
745 }
746
747 /* In writeback mode, adjacent writes will be clustered together */
TEST_F(WriteCluster,clustering)748 TEST_F(WriteCluster, clustering)
749 {
750 const char FULLPATH[] = "mountpoint/some_file.txt";
751 const char RELPATH[] = "some_file.txt";
752 uint64_t ino = 42;
753 int i, fd;
754 void *wbuf, *wbuf2x;
755 ssize_t bufsize = m_maxbcachebuf;
756 off_t filesize = 5 * bufsize;
757
758 wbuf = malloc(bufsize);
759 ASSERT_NE(nullptr, wbuf) << strerror(errno);
760 memset(wbuf, 'X', bufsize);
761 wbuf2x = malloc(2 * bufsize);
762 ASSERT_NE(nullptr, wbuf2x) << strerror(errno);
763 memset(wbuf2x, 'X', 2 * bufsize);
764
765 expect_lookup(RELPATH, ino, filesize);
766 expect_open(ino, 0, 1);
767 /*
768 * Writes of bufsize-bytes each should be clustered into greater sizes.
769 * The amount of clustering is adaptive, so the first write actually
770 * issued will be 2x bufsize and subsequent writes may be larger
771 */
772 expect_write(ino, 0, 2 * bufsize, 2 * bufsize, wbuf2x);
773 expect_write(ino, 2 * bufsize, 2 * bufsize, 2 * bufsize, wbuf2x);
774 expect_flush(ino, 1, ReturnErrno(0));
775 expect_release(ino, ReturnErrno(0));
776
777 fd = open(FULLPATH, O_RDWR);
778 ASSERT_LE(0, fd) << strerror(errno);
779
780 for (i = 0; i < 4; i++) {
781 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
782 << strerror(errno);
783 }
784 close(fd);
785 }
786
787 /*
788 * When clustering writes, an I/O error to any of the cluster's children should
789 * not panic the system on unmount
790 */
791 /*
792 * Disabled because it panics.
793 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=238565
794 */
TEST_F(WriteCluster,DISABLED_cluster_write_err)795 TEST_F(WriteCluster, DISABLED_cluster_write_err)
796 {
797 const char FULLPATH[] = "mountpoint/some_file.txt";
798 const char RELPATH[] = "some_file.txt";
799 uint64_t ino = 42;
800 int i, fd;
801 void *wbuf;
802 ssize_t bufsize = m_maxbcachebuf;
803 off_t filesize = 4 * bufsize;
804
805 wbuf = malloc(bufsize);
806 ASSERT_NE(nullptr, wbuf) << strerror(errno);
807 memset(wbuf, 'X', bufsize);
808
809 expect_lookup(RELPATH, ino, filesize);
810 expect_open(ino, 0, 1);
811 EXPECT_CALL(*m_mock, process(
812 ResultOf([=](auto in) {
813 return (in.header.opcode == FUSE_WRITE);
814 }, Eq(true)),
815 _)
816 ).WillRepeatedly(Invoke(ReturnErrno(EIO)));
817 expect_flush(ino, 1, ReturnErrno(0));
818 expect_release(ino, ReturnErrno(0));
819
820 fd = open(FULLPATH, O_RDWR);
821 ASSERT_LE(0, fd) << strerror(errno);
822
823 for (i = 0; i < 3; i++) {
824 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
825 << strerror(errno);
826 }
827 close(fd);
828 }
829
830 /*
831 * In writeback mode, writes to an O_WRONLY file could trigger reads from the
832 * server. The FUSE protocol explicitly allows that.
833 */
TEST_F(WriteBack,rmw)834 TEST_F(WriteBack, rmw)
835 {
836 const char FULLPATH[] = "mountpoint/some_file.txt";
837 const char RELPATH[] = "some_file.txt";
838 const char *CONTENTS = "abcdefgh";
839 const char *INITIAL = "XXXXXXXXXX";
840 uint64_t ino = 42;
841 uint64_t offset = 1;
842 off_t fsize = 10;
843 int fd;
844 ssize_t bufsize = strlen(CONTENTS);
845
846 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
847 expect_open(ino, 0, 1);
848 expect_read(ino, 0, fsize, fsize, INITIAL, O_WRONLY);
849 maybe_expect_write(ino, offset, bufsize, CONTENTS);
850
851 fd = open(FULLPATH, O_WRONLY);
852 EXPECT_LE(0, fd) << strerror(errno);
853
854 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
855 << strerror(errno);
856 leak(fd);
857 }
858
859 /*
860 * Without direct_io, writes should be committed to cache
861 */
TEST_F(WriteBack,cache)862 TEST_F(WriteBack, cache)
863 {
864 const char FULLPATH[] = "mountpoint/some_file.txt";
865 const char RELPATH[] = "some_file.txt";
866 const char *CONTENTS = "abcdefgh";
867 uint64_t ino = 42;
868 int fd;
869 ssize_t bufsize = strlen(CONTENTS);
870 uint8_t readbuf[bufsize];
871
872 expect_lookup(RELPATH, ino, 0);
873 expect_open(ino, 0, 1);
874 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
875
876 fd = open(FULLPATH, O_RDWR);
877 EXPECT_LE(0, fd) << strerror(errno);
878
879 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
880 /*
881 * A subsequent read should be serviced by cache, without querying the
882 * filesystem daemon
883 */
884 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
885 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
886 leak(fd);
887 }
888
889 /*
890 * With O_DIRECT, writes should be not committed to cache. Admittedly this is
891 * an odd test, because it would be unusual to use O_DIRECT for writes but not
892 * reads.
893 */
TEST_F(WriteBack,o_direct)894 TEST_F(WriteBack, o_direct)
895 {
896 const char FULLPATH[] = "mountpoint/some_file.txt";
897 const char RELPATH[] = "some_file.txt";
898 const char *CONTENTS = "abcdefgh";
899 uint64_t ino = 42;
900 int fd;
901 ssize_t bufsize = strlen(CONTENTS);
902 uint8_t readbuf[bufsize];
903
904 expect_lookup(RELPATH, ino, 0);
905 expect_open(ino, 0, 1);
906 FuseTest::expect_write(ino, 0, bufsize, bufsize, 0, FUSE_WRITE_CACHE,
907 CONTENTS);
908 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
909
910 fd = open(FULLPATH, O_RDWR | O_DIRECT);
911 EXPECT_LE(0, fd) << strerror(errno);
912
913 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
914 /* A subsequent read must query the daemon because cache is empty */
915 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
916 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
917 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
918 leak(fd);
919 }
920
921 /*
922 * When mounted with -o async, the writeback cache mode should delay writes
923 */
TEST_F(WriteBackAsync,delay)924 TEST_F(WriteBackAsync, delay)
925 {
926 const char FULLPATH[] = "mountpoint/some_file.txt";
927 const char RELPATH[] = "some_file.txt";
928 const char *CONTENTS = "abcdefgh";
929 uint64_t ino = 42;
930 int fd;
931 ssize_t bufsize = strlen(CONTENTS);
932
933 expect_lookup(RELPATH, ino, 0);
934 expect_open(ino, 0, 1);
935 /* Write should be cached, but FUSE_WRITE shouldn't be sent */
936 EXPECT_CALL(*m_mock, process(
937 ResultOf([=](auto in) {
938 return (in.header.opcode == FUSE_WRITE);
939 }, Eq(true)),
940 _)
941 ).Times(0);
942
943 fd = open(FULLPATH, O_RDWR);
944 EXPECT_LE(0, fd) << strerror(errno);
945
946 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
947
948 /* Don't close the file because that would flush the cache */
949 leak(fd);
950 }
951
952 /*
953 * A direct write should not evict dirty cached data from outside of its own
954 * byte range.
955 */
TEST_F(WriteBackAsync,direct_io_ignores_unrelated_cached)956 TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
957 {
958 const char FULLPATH[] = "mountpoint/some_file.txt";
959 const char RELPATH[] = "some_file.txt";
960 const char CONTENTS0[] = "abcdefgh";
961 const char CONTENTS1[] = "ijklmnop";
962 uint64_t ino = 42;
963 int fd;
964 ssize_t bufsize = strlen(CONTENTS0) + 1;
965 ssize_t fsize = 2 * m_maxbcachebuf;
966 char readbuf[bufsize];
967 void *zeros;
968
969 zeros = calloc(1, m_maxbcachebuf);
970 ASSERT_NE(nullptr, zeros);
971
972 expect_lookup(RELPATH, ino, fsize);
973 expect_open(ino, 0, 1);
974 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf, zeros);
975 FuseTest::expect_write(ino, m_maxbcachebuf, bufsize, bufsize, 0, 0,
976 CONTENTS1);
977
978 fd = open(FULLPATH, O_RDWR);
979 EXPECT_LE(0, fd) << strerror(errno);
980
981 // Cache first block with dirty data. This will entail first reading
982 // the existing data.
983 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS0, bufsize, 0))
984 << strerror(errno);
985
986 // Write directly to second block
987 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
988 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS1, bufsize, m_maxbcachebuf))
989 << strerror(errno);
990
991 // Read from the first block again. Should be serviced by cache.
992 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
993 ASSERT_EQ(bufsize, pread(fd, readbuf, bufsize, 0)) << strerror(errno);
994 ASSERT_STREQ(readbuf, CONTENTS0);
995
996 leak(fd);
997 free(zeros);
998 }
999
1000 /*
1001 * If a direct io write partially overlaps one or two blocks of dirty cached
1002 * data, No dirty data should be lost. Admittedly this is a weird test,
1003 * because it would be unusual to use O_DIRECT and the writeback cache.
1004 */
TEST_F(WriteBackAsync,direct_io_partially_overlaps_cached_block)1005 TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
1006 {
1007 const char FULLPATH[] = "mountpoint/some_file.txt";
1008 const char RELPATH[] = "some_file.txt";
1009 uint64_t ino = 42;
1010 int fd;
1011 off_t bs = m_maxbcachebuf;
1012 ssize_t fsize = 3 * bs;
1013 void *readbuf, *zeros, *ones, *zeroones, *onezeros;
1014
1015 readbuf = malloc(bs);
1016 ASSERT_NE(nullptr, readbuf) << strerror(errno);
1017 zeros = calloc(1, 3 * bs);
1018 ASSERT_NE(nullptr, zeros);
1019 ones = calloc(1, 2 * bs);
1020 ASSERT_NE(nullptr, ones);
1021 memset(ones, 1, 2 * bs);
1022 zeroones = calloc(1, bs);
1023 ASSERT_NE(nullptr, zeroones);
1024 memset((uint8_t*)zeroones + bs / 2, 1, bs / 2);
1025 onezeros = calloc(1, bs);
1026 ASSERT_NE(nullptr, onezeros);
1027 memset(onezeros, 1, bs / 2);
1028
1029 expect_lookup(RELPATH, ino, fsize);
1030 expect_open(ino, 0, 1);
1031
1032 fd = open(FULLPATH, O_RDWR);
1033 EXPECT_LE(0, fd) << strerror(errno);
1034
1035 /* Cache first and third blocks with dirty data. */
1036 ASSERT_EQ(3 * bs, pwrite(fd, zeros, 3 * bs, 0)) << strerror(errno);
1037
1038 /*
1039 * Write directly to all three blocks. The partially written blocks
1040 * will be flushed because they're dirty.
1041 */
1042 FuseTest::expect_write(ino, 0, bs, bs, 0, 0, zeros);
1043 FuseTest::expect_write(ino, 2 * bs, bs, bs, 0, 0, zeros);
1044 /* The direct write is split in two because of the m_maxwrite value */
1045 FuseTest::expect_write(ino, bs / 2, bs, bs, 0, 0, ones);
1046 FuseTest::expect_write(ino, 3 * bs / 2, bs, bs, 0, 0, ones);
1047 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
1048 ASSERT_EQ(2 * bs, pwrite(fd, ones, 2 * bs, bs / 2)) << strerror(errno);
1049
1050 /*
1051 * Read from both the valid and invalid portions of the first and third
1052 * blocks again. This will entail FUSE_READ operations because these
1053 * blocks were invalidated by the direct write.
1054 */
1055 expect_read(ino, 0, bs, bs, zeroones);
1056 expect_read(ino, 2 * bs, bs, bs, onezeros);
1057 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1058 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 0)) << strerror(errno);
1059 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1060 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 5 * bs / 2))
1061 << strerror(errno);
1062 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1063 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, bs / 2))
1064 << strerror(errno);
1065 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1066 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 2 * bs))
1067 << strerror(errno);
1068 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1069
1070 leak(fd);
1071 free(zeroones);
1072 free(onezeros);
1073 free(ones);
1074 free(zeros);
1075 free(readbuf);
1076 }
1077
1078 /*
1079 * In WriteBack mode, writes may be cached beyond what the server thinks is the
1080 * EOF. In this case, a short read at EOF should _not_ cause fusefs to update
1081 * the file's size.
1082 */
TEST_F(WriteBackAsync,eof)1083 TEST_F(WriteBackAsync, eof)
1084 {
1085 const char FULLPATH[] = "mountpoint/some_file.txt";
1086 const char RELPATH[] = "some_file.txt";
1087 const char *CONTENTS0 = "abcdefgh";
1088 const char *CONTENTS1 = "ijklmnop";
1089 uint64_t ino = 42;
1090 int fd;
1091 off_t offset = m_maxbcachebuf;
1092 ssize_t wbufsize = strlen(CONTENTS1);
1093 off_t old_filesize = (off_t)strlen(CONTENTS0);
1094 ssize_t rbufsize = 2 * old_filesize;
1095 char readbuf[rbufsize];
1096 size_t holesize = rbufsize - old_filesize;
1097 char hole[holesize];
1098 struct stat sb;
1099 ssize_t r;
1100
1101 expect_lookup(RELPATH, ino, 0);
1102 expect_open(ino, 0, 1);
1103 expect_read(ino, 0, m_maxbcachebuf, old_filesize, CONTENTS0);
1104
1105 fd = open(FULLPATH, O_RDWR);
1106 EXPECT_LE(0, fd) << strerror(errno);
1107
1108 /* Write and cache data beyond EOF */
1109 ASSERT_EQ(wbufsize, pwrite(fd, CONTENTS1, wbufsize, offset))
1110 << strerror(errno);
1111
1112 /* Read from the old EOF */
1113 r = pread(fd, readbuf, rbufsize, 0);
1114 ASSERT_LE(0, r) << strerror(errno);
1115 EXPECT_EQ(rbufsize, r) << "read should've synthesized a hole";
1116 EXPECT_EQ(0, memcmp(CONTENTS0, readbuf, old_filesize));
1117 bzero(hole, holesize);
1118 EXPECT_EQ(0, memcmp(hole, readbuf + old_filesize, holesize));
1119
1120 /* The file's size should still be what was established by pwrite */
1121 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1122 EXPECT_EQ(offset + wbufsize, sb.st_size);
1123 leak(fd);
1124 }
1125
1126 /*
1127 * When a file has dirty writes that haven't been flushed, the server's notion
1128 * of its mtime and ctime will be wrong. The kernel should ignore those if it
1129 * gets them from a FUSE_GETATTR before flushing.
1130 */
TEST_F(WriteBackAsync,timestamps)1131 TEST_F(WriteBackAsync, timestamps)
1132 {
1133 const char FULLPATH[] = "mountpoint/some_file.txt";
1134 const char RELPATH[] = "some_file.txt";
1135 const char *CONTENTS = "abcdefgh";
1136 ssize_t bufsize = strlen(CONTENTS);
1137 uint64_t ino = 42;
1138 uint64_t attr_valid = 0;
1139 uint64_t attr_valid_nsec = 0;
1140 uint64_t server_time = 12345;
1141 mode_t mode = S_IFREG | 0644;
1142 int fd;
1143
1144 struct stat sb;
1145
1146 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
1147 .WillRepeatedly(Invoke(
1148 ReturnImmediate([=](auto in __unused, auto& out) {
1149 SET_OUT_HEADER_LEN(out, entry);
1150 out.body.entry.attr.mode = mode;
1151 out.body.entry.nodeid = ino;
1152 out.body.entry.attr.nlink = 1;
1153 out.body.entry.attr_valid = attr_valid;
1154 out.body.entry.attr_valid_nsec = attr_valid_nsec;
1155 })));
1156 expect_open(ino, 0, 1);
1157 EXPECT_CALL(*m_mock, process(
1158 ResultOf([=](auto in) {
1159 return (in.header.opcode == FUSE_GETATTR &&
1160 in.header.nodeid == ino);
1161 }, Eq(true)),
1162 _)
1163 ).WillRepeatedly(Invoke(
1164 ReturnImmediate([=](auto i __unused, auto& out) {
1165 SET_OUT_HEADER_LEN(out, attr);
1166 out.body.attr.attr.ino = ino;
1167 out.body.attr.attr.mode = mode;
1168 out.body.attr.attr_valid = attr_valid;
1169 out.body.attr.attr_valid_nsec = attr_valid_nsec;
1170 out.body.attr.attr.atime = server_time;
1171 out.body.attr.attr.mtime = server_time;
1172 out.body.attr.attr.ctime = server_time;
1173 })));
1174
1175 fd = open(FULLPATH, O_RDWR);
1176 EXPECT_LE(0, fd) << strerror(errno);
1177 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1178
1179 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1180 EXPECT_EQ((time_t)server_time, sb.st_atime);
1181 EXPECT_NE((time_t)server_time, sb.st_mtime);
1182 EXPECT_NE((time_t)server_time, sb.st_ctime);
1183
1184 leak(fd);
1185 }
1186
1187 /* Any dirty timestamp fields should be flushed during a SETATTR */
TEST_F(WriteBackAsync,timestamps_during_setattr)1188 TEST_F(WriteBackAsync, timestamps_during_setattr)
1189 {
1190 const char FULLPATH[] = "mountpoint/some_file.txt";
1191 const char RELPATH[] = "some_file.txt";
1192 const char *CONTENTS = "abcdefgh";
1193 ssize_t bufsize = strlen(CONTENTS);
1194 uint64_t ino = 42;
1195 const mode_t newmode = 0755;
1196 int fd;
1197
1198 expect_lookup(RELPATH, ino, 0);
1199 expect_open(ino, 0, 1);
1200 EXPECT_CALL(*m_mock, process(
1201 ResultOf([=](auto in) {
1202 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1203 return (in.header.opcode == FUSE_SETATTR &&
1204 in.header.nodeid == ino &&
1205 in.body.setattr.valid == valid);
1206 }, Eq(true)),
1207 _)
1208 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1209 SET_OUT_HEADER_LEN(out, attr);
1210 out.body.attr.attr.ino = ino;
1211 out.body.attr.attr.mode = S_IFREG | newmode;
1212 })));
1213
1214 fd = open(FULLPATH, O_RDWR);
1215 EXPECT_LE(0, fd) << strerror(errno);
1216 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1217 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1218
1219 leak(fd);
1220 }
1221
1222 /* fuse_init_out.time_gran controls the granularity of timestamps */
TEST_P(TimeGran,timestamps_during_setattr)1223 TEST_P(TimeGran, timestamps_during_setattr)
1224 {
1225 const char FULLPATH[] = "mountpoint/some_file.txt";
1226 const char RELPATH[] = "some_file.txt";
1227 const char *CONTENTS = "abcdefgh";
1228 ssize_t bufsize = strlen(CONTENTS);
1229 uint64_t ino = 42;
1230 const mode_t newmode = 0755;
1231 int fd;
1232
1233 expect_lookup(RELPATH, ino, 0);
1234 expect_open(ino, 0, 1);
1235 EXPECT_CALL(*m_mock, process(
1236 ResultOf([=](auto in) {
1237 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1238 return (in.header.opcode == FUSE_SETATTR &&
1239 in.header.nodeid == ino &&
1240 in.body.setattr.valid == valid &&
1241 in.body.setattr.mtimensec % m_time_gran == 0 &&
1242 in.body.setattr.ctimensec % m_time_gran == 0);
1243 }, Eq(true)),
1244 _)
1245 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1246 SET_OUT_HEADER_LEN(out, attr);
1247 out.body.attr.attr.ino = ino;
1248 out.body.attr.attr.mode = S_IFREG | newmode;
1249 })));
1250
1251 fd = open(FULLPATH, O_RDWR);
1252 EXPECT_LE(0, fd) << strerror(errno);
1253 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1254 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1255
1256 leak(fd);
1257 }
1258
1259 INSTANTIATE_TEST_CASE_P(RA, TimeGran, Range(0u, 10u));
1260
1261 /*
1262 * Without direct_io, writes should be committed to cache
1263 */
TEST_F(Write,writethrough)1264 TEST_F(Write, writethrough)
1265 {
1266 const char FULLPATH[] = "mountpoint/some_file.txt";
1267 const char RELPATH[] = "some_file.txt";
1268 const char *CONTENTS = "abcdefgh";
1269 uint64_t ino = 42;
1270 int fd;
1271 ssize_t bufsize = strlen(CONTENTS);
1272 uint8_t readbuf[bufsize];
1273
1274 expect_lookup(RELPATH, ino, 0);
1275 expect_open(ino, 0, 1);
1276 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1277
1278 fd = open(FULLPATH, O_RDWR);
1279 EXPECT_LE(0, fd) << strerror(errno);
1280
1281 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1282 /*
1283 * A subsequent read should be serviced by cache, without querying the
1284 * filesystem daemon
1285 */
1286 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1287 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1288 leak(fd);
1289 }
1290
1291 /* Writes that extend a file should update the cached file size */
TEST_F(Write,update_file_size)1292 TEST_F(Write, update_file_size)
1293 {
1294 const char FULLPATH[] = "mountpoint/some_file.txt";
1295 const char RELPATH[] = "some_file.txt";
1296 const char *CONTENTS = "abcdefgh";
1297 struct stat sb;
1298 uint64_t ino = 42;
1299 int fd;
1300 ssize_t bufsize = strlen(CONTENTS);
1301
1302 expect_lookup(RELPATH, ino, 0);
1303 expect_open(ino, 0, 1);
1304 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1305
1306 fd = open(FULLPATH, O_RDWR);
1307 EXPECT_LE(0, fd) << strerror(errno);
1308
1309 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1310 /* Get cached attributes */
1311 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1312 ASSERT_EQ(bufsize, sb.st_size);
1313 leak(fd);
1314 }
1315