]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/flush.cc
fuse(4): Add some tests for FUSE_FLUSH
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / flush.cc
1 /*-
2  * Copyright (c) 2019 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by BFF Storage Systems, LLC under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 extern "C" {
31 #include <fcntl.h>
32 #include <unistd.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Flush: public FuseTest {
41
42 const static uint64_t FH = 0xdeadbeef1a7ebabe;
43
44 public:
45 void expect_flush(uint64_t ino, int times, ProcessMockerT r)
46 {
47         EXPECT_CALL(*m_mock, process(
48                 ResultOf([=](auto in) {
49                         return (in->header.opcode == FUSE_FLUSH &&
50                                 in->header.nodeid == ino &&
51                                 in->body.flush.fh == Flush::FH);
52                 }, Eq(true)),
53                 _)
54         ).Times(times)
55         .WillRepeatedly(Invoke(r));
56 }
57
58 void expect_getattr(uint64_t ino)
59 {
60         /* Until the attr cache is working, we may send an additional GETATTR */
61         EXPECT_CALL(*m_mock, process(
62                 ResultOf([=](auto in) {
63                         return (in->header.opcode == FUSE_GETATTR &&
64                                 in->header.nodeid == ino);
65                 }, Eq(true)),
66                 _)
67         ).WillRepeatedly(Invoke([=](auto in, auto out) {
68                 out->header.unique = in->header.unique;
69                 SET_OUT_HEADER_LEN(out, attr);
70                 out->body.attr.attr.ino = ino;  // Must match nodeid
71                 out->body.attr.attr.mode = S_IFREG | 0644;
72         }));
73
74 }
75
76 void expect_lookup(const char *relpath, uint64_t ino)
77 {
78         EXPECT_LOOKUP(1, relpath).WillRepeatedly(Invoke([=](auto in, auto out) {
79                 out->header.unique = in->header.unique;
80                 SET_OUT_HEADER_LEN(out, entry);
81                 out->body.entry.attr.mode = S_IFREG | 0644;
82                 out->body.entry.nodeid = ino;
83                 out->body.entry.attr_valid = UINT64_MAX;
84         }));
85 }
86
87 void expect_open(uint64_t ino, int times)
88 {
89         EXPECT_CALL(*m_mock, process(
90                 ResultOf([=](auto in) {
91                         return (in->header.opcode == FUSE_OPEN &&
92                                 in->header.nodeid == ino);
93                 }, Eq(true)),
94                 _)
95         ).Times(times)
96         .WillRepeatedly(Invoke([](auto in, auto out) {
97                 out->header.unique = in->header.unique;
98                 out->header.len = sizeof(out->header);
99                 SET_OUT_HEADER_LEN(out, open);
100                 out->body.open.fh = Flush::FH;
101         }));
102
103 }
104
105 /*
106  * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting.  This
107  * expectation will silence googlemock warnings
108  */
109 void expect_release()
110 {
111         EXPECT_CALL(*m_mock, process(
112                 ResultOf([=](auto in) {
113                         return (in->header.opcode == FUSE_RELEASE);
114                 }, Eq(true)),
115                 _)
116         ).WillRepeatedly(Invoke(ReturnErrno(0)));
117 }
118 };
119
120 // TODO: lock_owner stuff
121
122 /* If a file descriptor is duplicated, every close causes FLUSH */
123 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
124 TEST_F(Flush, DISABLED_dup)
125 {
126         const char FULLPATH[] = "mountpoint/some_file.txt";
127         const char RELPATH[] = "some_file.txt";
128         uint64_t ino = 42;
129         int fd, fd2;
130
131         expect_lookup(RELPATH, ino);
132         expect_open(ino, 1);
133         expect_getattr(ino);
134         expect_flush(ino, 2, ReturnErrno(0));
135         expect_release();
136
137         fd = open(FULLPATH, O_WRONLY);
138         EXPECT_LE(0, fd) << strerror(errno);
139
140         fd2 = dup(fd);
141
142         ASSERT_EQ(0, close(fd2)) << strerror(errno);
143         ASSERT_EQ(0, close(fd)) << strerror(errno);
144 }
145
146 /*
147  * Some FUSE filesystem cache data internally and flush it on release.  Such
148  * filesystems may generate errors during release.  On Linux, these get
149  * returned by close(2).  However, POSIX does not require close(2) to return
150  * this error.  FreeBSD's fuse(4) should return EIO if it returns an error at
151  * all.
152  */
153 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
154 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
155 TEST_F(Flush, DISABLED_eio)
156 {
157         const char FULLPATH[] = "mountpoint/some_file.txt";
158         const char RELPATH[] = "some_file.txt";
159         uint64_t ino = 42;
160         int fd;
161
162         expect_lookup(RELPATH, ino);
163         expect_open(ino, 1);
164         expect_getattr(ino);
165         expect_flush(ino, 1, ReturnErrno(EIO));
166         expect_release();
167
168         fd = open(FULLPATH, O_WRONLY);
169         EXPECT_LE(0, fd) << strerror(errno);
170
171         ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
172 }
173
174 /* A FUSE_FLUSH should be sent on close(2) */
175 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
176 TEST_F(Flush, DISABLED_flush)
177 {
178         const char FULLPATH[] = "mountpoint/some_file.txt";
179         const char RELPATH[] = "some_file.txt";
180         uint64_t ino = 42;
181         int fd;
182
183         expect_lookup(RELPATH, ino);
184         expect_open(ino, 1);
185         expect_getattr(ino);
186         expect_flush(ino, 1, ReturnErrno(0));
187         expect_release();
188
189         fd = open(FULLPATH, O_WRONLY);
190         EXPECT_LE(0, fd) << strerror(errno);
191
192         ASSERT_TRUE(0 == close(fd)) << strerror(errno);
193 }