/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2019 The FreeBSD Foundation4*5* This software was developed by BFF Storage Systems, LLC under sponsorship6* from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930extern "C" {31#include <aio.h>32#include <fcntl.h>33#include <unistd.h>34}3536#include "mockfs.hh"37#include "utils.hh"3839using namespace testing;4041/*42* TODO: remove FUSE_FSYNC_FDATASYNC definition when upgrading to protocol 7.28.43* This bit was actually part of kernel protocol version 5.2, but never44* documented until after 7.2845*/46#ifndef FUSE_FSYNC_FDATASYNC47#define FUSE_FSYNC_FDATASYNC 148#endif4950class FsyncDir: public FuseTest {51public:52void expect_fsyncdir(uint64_t ino, uint32_t flags, int error)53{54EXPECT_CALL(*m_mock, process(55ResultOf([=](auto in) {56return (in.header.opcode == FUSE_FSYNCDIR &&57in.header.nodeid == ino &&58/*59* TODO: reenable pid check after fixing60* bug 23637961*/62//(pid_t)in.header.pid == getpid() &&63in.body.fsyncdir.fh == FH &&64in.body.fsyncdir.fsync_flags == flags);65}, Eq(true)),66_)67).WillOnce(Invoke(ReturnErrno(error)));68}6970void expect_lookup(const char *relpath, uint64_t ino)71{72FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);73}7475};7677class AioFsyncDir: public FsyncDir {78virtual void SetUp() {79if (!is_unsafe_aio_enabled())80GTEST_SKIP() <<81"vfs.aio.enable_unsafe must be set for this test";82FuseTest::SetUp();83}84};8586/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */87TEST_F(AioFsyncDir, aio_fsync)88{89const char FULLPATH[] = "mountpoint/some_file.txt";90const char RELPATH[] = "some_file.txt";91uint64_t ino = 42;92struct aiocb iocb, *piocb;93int fd;9495expect_lookup(RELPATH, ino);96expect_opendir(ino);97expect_fsyncdir(ino, 0, 0);9899fd = open(FULLPATH, O_DIRECTORY);100ASSERT_LE(0, fd) << strerror(errno);101102bzero(&iocb, sizeof(iocb));103iocb.aio_fildes = fd;104105ASSERT_EQ(0, aio_fsync(O_SYNC, &iocb)) << strerror(errno);106ASSERT_EQ(0, aio_waitcomplete(&piocb, NULL)) << strerror(errno);107108leak(fd);109}110111TEST_F(FsyncDir, eio)112{113const char FULLPATH[] = "mountpoint/some_dir";114const char RELPATH[] = "some_dir";115uint64_t ino = 42;116int fd;117118expect_lookup(RELPATH, ino);119expect_opendir(ino);120expect_fsyncdir(ino, 0, EIO);121122fd = open(FULLPATH, O_DIRECTORY);123ASSERT_LE(0, fd) << strerror(errno);124ASSERT_NE(0, fsync(fd));125ASSERT_EQ(EIO, errno);126127leak(fd);128}129130/*131* If the filesystem returns ENOSYS, it will be treated as success and132* subsequent calls to VOP_FSYNC will succeed automatically without being sent133* to the filesystem daemon134*/135TEST_F(FsyncDir, enosys)136{137const char FULLPATH[] = "mountpoint/some_dir";138const char RELPATH[] = "some_dir";139uint64_t ino = 42;140int fd;141142expect_lookup(RELPATH, ino);143expect_opendir(ino);144expect_fsyncdir(ino, 0, ENOSYS);145146fd = open(FULLPATH, O_DIRECTORY);147ASSERT_LE(0, fd) << strerror(errno);148EXPECT_EQ(0, fsync(fd)) << strerror(errno);149150/* Subsequent calls shouldn't query the daemon*/151EXPECT_EQ(0, fsync(fd)) << strerror(errno);152153leak(fd);154}155156TEST_F(FsyncDir, fsyncdata)157{158const char FULLPATH[] = "mountpoint/some_dir";159const char RELPATH[] = "some_dir";160uint64_t ino = 42;161int fd;162163expect_lookup(RELPATH, ino);164expect_opendir(ino);165expect_fsyncdir(ino, FUSE_FSYNC_FDATASYNC, 0);166167fd = open(FULLPATH, O_DIRECTORY);168ASSERT_LE(0, fd) << strerror(errno);169ASSERT_EQ(0, fdatasync(fd)) << strerror(errno);170171leak(fd);172}173174/*175* Unlike regular files, the kernel doesn't know whether a directory is or176* isn't dirty, so fuse(4) should always send FUSE_FSYNCDIR on fsync(2)177*/178TEST_F(FsyncDir, fsync)179{180const char FULLPATH[] = "mountpoint/some_dir";181const char RELPATH[] = "some_dir";182uint64_t ino = 42;183int fd;184185expect_lookup(RELPATH, ino);186expect_opendir(ino);187expect_fsyncdir(ino, 0, 0);188189fd = open(FULLPATH, O_DIRECTORY);190ASSERT_LE(0, fd) << strerror(errno);191ASSERT_EQ(0, fsync(fd)) << strerror(errno);192193leak(fd);194}195196197