Index: stable/12/tests/sys/fs/fusefs/io.cc =================================================================== --- stable/12/tests/sys/fs/fusefs/io.cc (revision 359211) +++ stable/12/tests/sys/fs/fusefs/io.cc (revision 359212) @@ -1,545 +1,542 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2019 The FreeBSD Foundation * * This software was developed by BFF Storage Systems, LLC under sponsorship * from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ extern "C" { #include #include #include #include #include #include } #include "mockfs.hh" #include "utils.hh" /* * For testing I/O like fsx does, but deterministically and without a real * underlying file system - * - * TODO: after fusefs gains the options to select cache mode for each mount - * point, run each of these tests for all cache modes. */ using namespace testing; enum cache_mode { Uncached, Writethrough, Writeback, WritebackAsync }; const char *cache_mode_to_s(enum cache_mode cm) { switch (cm) { case Uncached: return "Uncached"; case Writethrough: return "Writethrough"; case Writeback: return "Writeback"; case WritebackAsync: return "WritebackAsync"; default: return "Unknown"; } } const char FULLPATH[] = "mountpoint/some_file.txt"; const char RELPATH[] = "some_file.txt"; const uint64_t ino = 42; static void compare(const void *tbuf, const void *controlbuf, off_t baseofs, ssize_t size) { int i; for (i = 0; i < size; i++) { if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) { off_t ofs = baseofs + i; FAIL() << "miscompare at offset " << std::hex << std::showbase << ofs << ". expected = " << std::setw(2) << (unsigned)((const uint8_t*)controlbuf)[i] << " got = " << (unsigned)((const uint8_t*)tbuf)[i]; } } } typedef tuple IoParam; class Io: public FuseTest, public WithParamInterface { public: int m_backing_fd, m_control_fd, m_test_fd; off_t m_filesize; bool m_direct_io; Io(): m_backing_fd(-1), m_control_fd(-1), m_test_fd(-1), m_filesize(0), m_direct_io(false) {}; void SetUp() { m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644); if (m_backing_fd < 0) FAIL() << strerror(errno); m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644); if (m_control_fd < 0) FAIL() << strerror(errno); srandom(22'9'1982); // Seed with my birthday if (get<0>(GetParam())) m_init_flags |= FUSE_ASYNC_READ; m_maxwrite = get<1>(GetParam()); switch (get<2>(GetParam())) { case Uncached: m_direct_io = true; break; case WritebackAsync: m_async = true; /* FALLTHROUGH */ case Writeback: m_init_flags |= FUSE_WRITEBACK_CACHE; /* FALLTHROUGH */ case Writethrough: break; default: FAIL() << "Unknown cache mode"; } FuseTest::SetUp(); if (IsSkipped()) return; if (verbosity > 0) { printf("Test Parameters: init_flags=%#x maxwrite=%#x " "%sasync cache=%s\n", m_init_flags, m_maxwrite, m_async? "" : "no", cache_mode_to_s(get<2>(GetParam()))); } expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO : 0, 1); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_WRITE && in.header.nodeid == ino); }, Eq(true)), _) ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { const char *buf = (const char*)in.body.bytes + sizeof(struct fuse_write_in); ssize_t isize = in.body.write.size; off_t iofs = in.body.write.offset; ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs)) << strerror(errno); SET_OUT_HEADER_LEN(out, write); out.body.write.size = isize; }))); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_READ && in.header.nodeid == ino); }, Eq(true)), _) ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { ssize_t isize = in.body.write.size; off_t iofs = in.body.write.offset; void *buf = out.body.bytes; ssize_t osize; osize = pread(m_backing_fd, buf, isize, iofs); ASSERT_LE(0, osize) << strerror(errno); out.header.len = sizeof(struct fuse_out_header) + osize; }))); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_SETATTR && in.header.nodeid == ino && (in.body.setattr.valid & FATTR_SIZE)); }, Eq(true)), _) ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size)) << strerror(errno); SET_OUT_HEADER_LEN(out, attr); out.body.attr.attr.ino = ino; out.body.attr.attr.mode = S_IFREG | 0755; out.body.attr.attr.size = in.body.setattr.size; out.body.attr.attr_valid = UINT64_MAX; }))); /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */ EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_FLUSH && in.header.nodeid == ino); }, Eq(true)), _) ).WillRepeatedly(Invoke(ReturnErrno(0))); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_RELEASE && in.header.nodeid == ino); }, Eq(true)), _) ).WillRepeatedly(Invoke(ReturnErrno(0))); m_test_fd = open(FULLPATH, O_RDWR ); EXPECT_LE(0, m_test_fd) << strerror(errno); } void TearDown() { if (m_test_fd >= 0) close(m_test_fd); if (m_backing_fd >= 0) close(m_backing_fd); if (m_control_fd >= 0) close(m_control_fd); FuseTest::TearDown(); leak(m_test_fd); } void do_closeopen() { ASSERT_EQ(0, close(m_test_fd)) << strerror(errno); m_test_fd = open("backing_file", O_RDWR); ASSERT_LE(0, m_test_fd) << strerror(errno); ASSERT_EQ(0, close(m_control_fd)) << strerror(errno); m_control_fd = open("control", O_RDWR); ASSERT_LE(0, m_control_fd) << strerror(errno); } void do_ftruncate(off_t offs) { ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno); ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno); m_filesize = offs; } void do_mapread(ssize_t size, off_t offs) { void *control_buf, *p; off_t pg_offset, page_mask; size_t map_size; page_mask = getpagesize() - 1; pg_offset = offs & page_mask; map_size = pg_offset + size; p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset); ASSERT_NE(p, MAP_FAILED) << strerror(errno); control_buf = malloc(size); ASSERT_NE(nullptr, control_buf) << strerror(errno); ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs)) << strerror(errno); compare((void*)((char*)p + pg_offset), control_buf, offs, size); ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno); free(control_buf); } void do_read(ssize_t size, off_t offs) { void *test_buf, *control_buf; ssize_t r; test_buf = malloc(size); ASSERT_NE(nullptr, test_buf) << strerror(errno); control_buf = malloc(size); ASSERT_NE(nullptr, control_buf) << strerror(errno); errno = 0; r = pread(m_test_fd, test_buf, size, offs); ASSERT_NE(-1, r) << strerror(errno); ASSERT_EQ(size, r) << "unexpected short read"; r = pread(m_control_fd, control_buf, size, offs); ASSERT_NE(-1, r) << strerror(errno); ASSERT_EQ(size, r) << "unexpected short read"; compare(test_buf, control_buf, offs, size); free(control_buf); free(test_buf); } void do_mapwrite(ssize_t size, off_t offs) { char *buf; void *p; off_t pg_offset, page_mask; size_t map_size; long i; page_mask = getpagesize() - 1; pg_offset = offs & page_mask; map_size = pg_offset + size; buf = (char*)malloc(size); ASSERT_NE(nullptr, buf) << strerror(errno); for (i=0; i < size; i++) buf[i] = random(); if (offs + size > m_filesize) { /* * Must manually extend. vm_mmap_vnode will not implicitly * extend a vnode */ do_ftruncate(offs + size); } p = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset); ASSERT_NE(p, MAP_FAILED) << strerror(errno); bcopy(buf, (char*)p + pg_offset, size); ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs)) << strerror(errno); free(buf); ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno); } void do_write(ssize_t size, off_t offs) { char *buf; long i; buf = (char*)malloc(size); ASSERT_NE(nullptr, buf) << strerror(errno); for (i=0; i < size; i++) buf[i] = random(); ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs )) << strerror(errno); ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs)) << strerror(errno); m_filesize = std::max(m_filesize, offs + size); free(buf); } }; class IoCacheable: public Io { public: virtual void SetUp() { Io::SetUp(); } }; /* * Extend a file with dirty data in the last page of the last block. * * fsx -WR -P /tmp -S8 -N3 fsx.bin */ TEST_P(Io, extend_from_dirty_page) { off_t wofs = 0x21a0; ssize_t wsize = 0xf0a8; off_t rofs = 0xb284; ssize_t rsize = 0x9b22; off_t truncsize = 0x28702; do_write(wsize, wofs); do_ftruncate(truncsize); do_read(rsize, rofs); } /* * mapwrite into a newly extended part of a file. * * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin */ TEST_P(IoCacheable, extend_by_mapwrite) { do_mapwrite(0x849e, 0x29a3a); /* [0x29a3a, 0x31ed7] */ do_mapwrite(0x3994, 0x3c7d8); /* [0x3c7d8, 0x4016b] */ do_read(0xf556, 0x30c16); /* [0x30c16, 0x4016b] */ } /* * When writing the last page of a file, it must be written synchronously. * Otherwise the cached page can become invalid by a subsequent extend * operation. * * fsx -WR -P /tmp -S642 -N3 fsx.bin */ TEST_P(Io, last_page) { do_write(0xcc77, 0x1134f); /* [0x1134f, 0x1dfc5] */ do_write(0xdfa7, 0x2096a); /* [0x2096a, 0x2e910] */ do_read(0xb5b7, 0x1a3aa); /* [0x1a3aa, 0x25960] */ } /* * Read a hole using mmap * * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp -S14 fsx.bin */ TEST_P(IoCacheable, mapread_hole) { do_write(0x123b7, 0xf205); /* [0xf205, 0x215bb] */ do_mapread(0xeeea, 0x2f4c); /* [0x2f4c, 0x11e35] */ } /* * Read a hole from a block that contains some cached data. * * fsx -WR -P /tmp -S55 fsx.bin */ TEST_P(Io, read_hole_from_cached_block) { off_t wofs = 0x160c5; ssize_t wsize = 0xa996; off_t rofs = 0x472e; ssize_t rsize = 0xd8d5; do_write(wsize, wofs); do_read(rsize, rofs); } /* * Truncating a file into a dirty buffer should not causing anything untoward * to happen when that buffer is eventually flushed. * * fsx -WR -P /tmp -S839 -d -N6 fsx.bin */ TEST_P(Io, truncate_into_dirty_buffer) { off_t wofs0 = 0x3bad7; ssize_t wsize0 = 0x4529; off_t wofs1 = 0xc30d; ssize_t wsize1 = 0x5f77; off_t truncsize0 = 0x10916; off_t rofs = 0xdf17; ssize_t rsize = 0x29ff; off_t truncsize1 = 0x152b4; do_write(wsize0, wofs0); do_write(wsize1, wofs1); do_ftruncate(truncsize0); do_read(rsize, rofs); do_ftruncate(truncsize1); close(m_test_fd); } /* * Truncating a file into a dirty buffer should not causing anything untoward * to happen when that buffer is eventually flushed, even when the buffer's * dirty_off is > 0. * * Based on this command with a few steps removed: * fsx -WR -P /tmp -S677 -d -N8 fsx.bin */ TEST_P(Io, truncate_into_dirty_buffer2) { off_t truncsize0 = 0x344f3; off_t wofs = 0x2790c; ssize_t wsize = 0xd86a; off_t truncsize1 = 0x2de38; off_t rofs2 = 0x1fd7a; ssize_t rsize2 = 0xc594; off_t truncsize2 = 0x31e71; /* Sets the file size to something larger than the next write */ do_ftruncate(truncsize0); /* * Creates a dirty buffer. The part in lbn 2 doesn't flush * synchronously. */ do_write(wsize, wofs); /* Truncates part of the dirty buffer created in step 2 */ do_ftruncate(truncsize1); /* XXX ?I don't know why this is necessary? */ do_read(rsize2, rofs2); /* Truncates the dirty buffer */ do_ftruncate(truncsize2); close(m_test_fd); } /* * Regression test for a bug introduced in r348931 * * Sequence of operations: * 1) The first write reads lbn so it can modify it * 2) The first write flushes lbn 3 immediately because it's the end of file * 3) The first write then flushes lbn 4 because it's the end of the file * 4) The second write modifies the cached versions of lbn 3 and 4 * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's * extending the buffer. Then it flushes lbn 4 because B_DELWRI was set but * B_CACHE was clear. * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the * buffer into a weird write-only state. All read operations would return * 0. Writes were apparently still processed, because the buffer's contents * were correct when examined in a core dump. * 7) The third write reads lbn 4 because cache is clear * 9) uiomove dutifully copies new data into the buffer * 10) The buffer's dirty is flushed to lbn 4 * 11) The read returns all zeros because of step 6. * * Based on: * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q fsx.bin */ TEST_P(Io, resize_a_valid_buffer_while_extending) { do_write(0x14530, 0x36ee6); /* [0x36ee6, 0x4b415] */ do_write(0x1507c, 0x33256); /* [0x33256, 0x482d1] */ do_write(0x175c, 0x4c03d); /* [0x4c03d, 0x4d798] */ do_read(0xe277, 0x3599c); /* [0x3599c, 0x43c12] */ close(m_test_fd); } INSTANTIATE_TEST_CASE_P(Io, Io, Combine(Bool(), /* async read */ Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */ Values(Uncached, Writethrough, Writeback, WritebackAsync) ) ); INSTANTIATE_TEST_CASE_P(Io, IoCacheable, Combine(Bool(), /* async read */ Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */ Values(Writethrough, Writeback, WritebackAsync) ) ); Index: stable/12/tests/sys/fs/fusefs/notify.cc =================================================================== --- stable/12/tests/sys/fs/fusefs/notify.cc (revision 359211) +++ stable/12/tests/sys/fs/fusefs/notify.cc (revision 359212) @@ -1,549 +1,549 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2019 The FreeBSD Foundation * * This software was developed by BFF Storage Systems, LLC under sponsorship * from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ extern "C" { #include #include #include #include } #include "mockfs.hh" #include "utils.hh" using namespace testing; /* * FUSE asynchonous notification * * FUSE servers can send unprompted notification messages for things like cache * invalidation. This file tests our client's handling of those messages. */ class Notify: public FuseTest { public: /* Ignore an optional FUSE_FSYNC */ void maybe_expect_fsync(uint64_t ino) { EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_FSYNC && in.header.nodeid == ino); }, Eq(true)), _) ).WillOnce(Invoke(ReturnErrno(0))); } void expect_lookup(uint64_t parent, const char *relpath, uint64_t ino, off_t size, Sequence &seq) { EXPECT_LOOKUP(parent, relpath) .InSequence(seq) .WillOnce(Invoke( ReturnImmediate([=](auto in __unused, auto& out) { SET_OUT_HEADER_LEN(out, entry); out.body.entry.attr.mode = S_IFREG | 0644; out.body.entry.nodeid = ino; out.body.entry.attr.ino = ino; out.body.entry.attr.nlink = 1; out.body.entry.attr.size = size; out.body.entry.attr_valid = UINT64_MAX; out.body.entry.entry_valid = UINT64_MAX; }))); } }; class NotifyWriteback: public Notify { public: virtual void SetUp() { m_init_flags |= FUSE_WRITEBACK_CACHE; m_async = true; Notify::SetUp(); if (IsSkipped()) return; } void expect_write(uint64_t ino, uint64_t offset, uint64_t size, const void *contents) { FuseTest::expect_write(ino, offset, size, size, 0, 0, contents); } }; struct inval_entry_args { MockFS *mock; ino_t parent; const char *name; size_t namelen; }; static void* inval_entry(void* arg) { const struct inval_entry_args *iea = (struct inval_entry_args*)arg; ssize_t r; r = iea->mock->notify_inval_entry(iea->parent, iea->name, iea->namelen); if (r >= 0) return 0; else return (void*)(intptr_t)errno; } struct inval_inode_args { MockFS *mock; ino_t ino; off_t off; ssize_t len; }; struct store_args { MockFS *mock; ino_t nodeid; off_t offset; ssize_t size; const void* data; }; static void* inval_inode(void* arg) { const struct inval_inode_args *iia = (struct inval_inode_args*)arg; ssize_t r; r = iia->mock->notify_inval_inode(iia->ino, iia->off, iia->len); if (r >= 0) return 0; else return (void*)(intptr_t)errno; } static void* store(void* arg) { const struct store_args *sa = (struct store_args*)arg; ssize_t r; r = sa->mock->notify_store(sa->nodeid, sa->offset, sa->data, sa->size); if (r >= 0) return 0; else return (void*)(intptr_t)errno; } /* Invalidate a nonexistent entry */ TEST_F(Notify, inval_entry_nonexistent) { const static char *name = "foo"; struct inval_entry_args iea; void *thr0_value; pthread_t th0; iea.mock = m_mock; iea.parent = FUSE_ROOT_ID; iea.name = name; iea.namelen = strlen(name); ASSERT_EQ(0, pthread_create(&th0, NULL, inval_entry, &iea)) << strerror(errno); pthread_join(th0, &thr0_value); /* It's not an error for an entry to not be cached */ EXPECT_EQ(0, (intptr_t)thr0_value); } /* Invalidate a cached entry */ TEST_F(Notify, inval_entry) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; struct inval_entry_args iea; struct stat sb; void *thr0_value; uint64_t ino0 = 42; uint64_t ino1 = 43; Sequence seq; pthread_t th0; expect_lookup(FUSE_ROOT_ID, RELPATH, ino0, 0, seq); expect_lookup(FUSE_ROOT_ID, RELPATH, ino1, 0, seq); /* Fill the entry cache */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(ino0, sb.st_ino); /* Now invalidate the entry */ iea.mock = m_mock; iea.parent = FUSE_ROOT_ID; iea.name = RELPATH; iea.namelen = strlen(RELPATH); ASSERT_EQ(0, pthread_create(&th0, NULL, inval_entry, &iea)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); /* The second lookup should return the alternate ino */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(ino1, sb.st_ino); } /* * Invalidate a cached entry beneath the root, which uses a slightly different * code path. */ TEST_F(Notify, inval_entry_below_root) { const static char FULLPATH[] = "mountpoint/some_dir/foo"; const static char DNAME[] = "some_dir"; const static char FNAME[] = "foo"; struct inval_entry_args iea; struct stat sb; void *thr0_value; uint64_t dir_ino = 41; uint64_t ino0 = 42; uint64_t ino1 = 43; Sequence seq; pthread_t th0; EXPECT_LOOKUP(FUSE_ROOT_ID, DNAME) .WillOnce(Invoke( ReturnImmediate([=](auto in __unused, auto& out) { SET_OUT_HEADER_LEN(out, entry); out.body.entry.attr.mode = S_IFDIR | 0755; out.body.entry.nodeid = dir_ino; out.body.entry.attr.nlink = 2; out.body.entry.attr_valid = UINT64_MAX; out.body.entry.entry_valid = UINT64_MAX; }))); expect_lookup(dir_ino, FNAME, ino0, 0, seq); expect_lookup(dir_ino, FNAME, ino1, 0, seq); /* Fill the entry cache */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(ino0, sb.st_ino); /* Now invalidate the entry */ iea.mock = m_mock; iea.parent = dir_ino; iea.name = FNAME; iea.namelen = strlen(FNAME); ASSERT_EQ(0, pthread_create(&th0, NULL, inval_entry, &iea)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); /* The second lookup should return the alternate ino */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(ino1, sb.st_ino); } /* Invalidating an entry invalidates the parent directory's attributes */ TEST_F(Notify, inval_entry_invalidates_parent_attrs) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; struct inval_entry_args iea; struct stat sb; void *thr0_value; uint64_t ino = 42; Sequence seq; pthread_t th0; expect_lookup(FUSE_ROOT_ID, RELPATH, ino, 0, seq); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_GETATTR && in.header.nodeid == FUSE_ROOT_ID); }, Eq(true)), _) ).Times(2) .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { SET_OUT_HEADER_LEN(out, attr); out.body.attr.attr.mode = S_IFDIR | 0755; out.body.attr.attr_valid = UINT64_MAX; }))); /* Fill the attr and entry cache */ ASSERT_EQ(0, stat("mountpoint", &sb)) << strerror(errno); ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); /* Now invalidate the entry */ iea.mock = m_mock; iea.parent = FUSE_ROOT_ID; iea.name = RELPATH; iea.namelen = strlen(RELPATH); ASSERT_EQ(0, pthread_create(&th0, NULL, inval_entry, &iea)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); /* /'s attribute cache should be cleared */ ASSERT_EQ(0, stat("mountpoint", &sb)) << strerror(errno); } TEST_F(Notify, inval_inode_nonexistent) { struct inval_inode_args iia; ino_t ino = 42; void *thr0_value; pthread_t th0; iia.mock = m_mock; iia.ino = ino; iia.off = 0; iia.len = 0; ASSERT_EQ(0, pthread_create(&th0, NULL, inval_inode, &iia)) << strerror(errno); pthread_join(th0, &thr0_value); /* It's not an error for an inode to not be cached */ EXPECT_EQ(0, (intptr_t)thr0_value); } TEST_F(Notify, inval_inode_with_clean_cache) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; const char CONTENTS0[] = "abcdefgh"; const char CONTENTS1[] = "ijklmnopqrstuvwxyz"; struct inval_inode_args iia; struct stat sb; ino_t ino = 42; void *thr0_value; Sequence seq; uid_t uid = 12345; pthread_t th0; ssize_t size0 = sizeof(CONTENTS0); ssize_t size1 = sizeof(CONTENTS1); char buf[80]; int fd; expect_lookup(FUSE_ROOT_ID, RELPATH, ino, size0, seq); expect_open(ino, 0, 1); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_GETATTR && in.header.nodeid == ino); }, Eq(true)), _) ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { SET_OUT_HEADER_LEN(out, attr); out.body.attr.attr.mode = S_IFREG | 0644; out.body.attr.attr_valid = UINT64_MAX; out.body.attr.attr.size = size1; out.body.attr.attr.uid = uid; }))); expect_read(ino, 0, size0, size0, CONTENTS0); expect_read(ino, 0, size1, size1, CONTENTS1); /* Fill the data cache */ fd = open(FULLPATH, O_RDWR); ASSERT_LE(0, fd) << strerror(errno); ASSERT_EQ(size0, read(fd, buf, size0)) << strerror(errno); EXPECT_EQ(0, memcmp(buf, CONTENTS0, size0)); /* Evict the data cache */ iia.mock = m_mock; iia.ino = ino; iia.off = 0; iia.len = 0; ASSERT_EQ(0, pthread_create(&th0, NULL, inval_inode, &iia)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); - /* cache attributes were been purged; this will trigger a new GETATTR */ + /* cache attributes were purged; this will trigger a new GETATTR */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(uid, sb.st_uid); EXPECT_EQ(size1, sb.st_size); /* This read should not be serviced by cache */ ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno); ASSERT_EQ(size1, read(fd, buf, size1)) << strerror(errno); EXPECT_EQ(0, memcmp(buf, CONTENTS1, size1)); leak(fd); } /* FUSE_NOTIFY_STORE with a file that's not in the entry cache */ /* disabled because FUSE_NOTIFY_STORE is not yet implemented */ TEST_F(Notify, DISABLED_store_nonexistent) { struct store_args sa; ino_t ino = 42; void *thr0_value; pthread_t th0; sa.mock = m_mock; sa.nodeid = ino; sa.offset = 0; sa.size = 0; ASSERT_EQ(0, pthread_create(&th0, NULL, store, &sa)) << strerror(errno); pthread_join(th0, &thr0_value); /* It's not an error for a file to be unknown to the kernel */ EXPECT_EQ(0, (intptr_t)thr0_value); } /* Store data into for a file that does not yet have anything cached */ /* disabled because FUSE_NOTIFY_STORE is not yet implemented */ TEST_F(Notify, DISABLED_store_with_blank_cache) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; const char CONTENTS1[] = "ijklmnopqrstuvwxyz"; struct store_args sa; ino_t ino = 42; void *thr0_value; Sequence seq; pthread_t th0; ssize_t size1 = sizeof(CONTENTS1); char buf[80]; int fd; expect_lookup(FUSE_ROOT_ID, RELPATH, ino, size1, seq); expect_open(ino, 0, 1); /* Fill the data cache */ fd = open(FULLPATH, O_RDWR); ASSERT_LE(0, fd) << strerror(errno); /* Evict the data cache */ sa.mock = m_mock; sa.nodeid = ino; sa.offset = 0; sa.size = size1; sa.data = (const void*)CONTENTS1; ASSERT_EQ(0, pthread_create(&th0, NULL, store, &sa)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); /* This read should be serviced by cache */ ASSERT_EQ(size1, read(fd, buf, size1)) << strerror(errno); EXPECT_EQ(0, memcmp(buf, CONTENTS1, size1)); leak(fd); } TEST_F(NotifyWriteback, inval_inode_with_dirty_cache) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; const char CONTENTS[] = "abcdefgh"; struct inval_inode_args iia; ino_t ino = 42; void *thr0_value; Sequence seq; pthread_t th0; ssize_t bufsize = sizeof(CONTENTS); int fd; expect_lookup(FUSE_ROOT_ID, RELPATH, ino, 0, seq); expect_open(ino, 0, 1); /* Fill the data cache */ fd = open(FULLPATH, O_RDWR); ASSERT_LE(0, fd); ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno); expect_write(ino, 0, bufsize, CONTENTS); /* * The FUSE protocol does not require an fsync here, but FreeBSD's * bufobj_invalbuf sends it anyway */ maybe_expect_fsync(ino); /* Evict the data cache */ iia.mock = m_mock; iia.ino = ino; iia.off = 0; iia.len = 0; ASSERT_EQ(0, pthread_create(&th0, NULL, inval_inode, &iia)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); leak(fd); } TEST_F(NotifyWriteback, inval_inode_attrs_only) { const static char FULLPATH[] = "mountpoint/foo"; const static char RELPATH[] = "foo"; const char CONTENTS[] = "abcdefgh"; struct inval_inode_args iia; struct stat sb; uid_t uid = 12345; ino_t ino = 42; void *thr0_value; Sequence seq; pthread_t th0; ssize_t bufsize = sizeof(CONTENTS); int fd; expect_lookup(FUSE_ROOT_ID, RELPATH, ino, 0, seq); expect_open(ino, 0, 1); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_WRITE); }, Eq(true)), _) ).Times(0); EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_GETATTR && in.header.nodeid == ino); }, Eq(true)), _) ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { SET_OUT_HEADER_LEN(out, attr); out.body.attr.attr.mode = S_IFREG | 0644; out.body.attr.attr_valid = UINT64_MAX; out.body.attr.attr.size = bufsize; out.body.attr.attr.uid = uid; }))); /* Fill the data cache */ fd = open(FULLPATH, O_RDWR); ASSERT_LE(0, fd) << strerror(errno); ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno); /* Evict the attributes, but not data cache */ iia.mock = m_mock; iia.ino = ino; iia.off = -1; iia.len = 0; ASSERT_EQ(0, pthread_create(&th0, NULL, inval_inode, &iia)) << strerror(errno); pthread_join(th0, &thr0_value); EXPECT_EQ(0, (intptr_t)thr0_value); /* cache attributes were been purged; this will trigger a new GETATTR */ ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno); EXPECT_EQ(uid, sb.st_uid); EXPECT_EQ(bufsize, sb.st_size); leak(fd); } Index: stable/12 =================================================================== --- stable/12 (revision 359211) +++ stable/12 (revision 359212) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r358089,358797