]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/link.cc
fusefs: clear a dir's attr cache when its contents change
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / link.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 <unistd.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Link: public FuseTest {
41 public:
42 void expect_link(uint64_t ino, const char *relpath, mode_t mode, uint32_t nlink)
43 {
44         EXPECT_CALL(*m_mock, process(
45                 ResultOf([=](auto in) {
46                         const char *name = (const char*)in->body.bytes
47                                 + sizeof(struct fuse_link_in);
48                         return (in->header.opcode == FUSE_LINK &&
49                                 in->body.link.oldnodeid == ino &&
50                                 (0 == strcmp(name, relpath)));
51                 }, Eq(true)),
52                 _)
53         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
54                 SET_OUT_HEADER_LEN(out, entry);
55                 out->body.entry.nodeid = ino;
56                 out->body.entry.attr.mode = mode;
57                 out->body.entry.attr.nlink = nlink;
58                 out->body.entry.attr_valid = UINT64_MAX;
59                 out->body.entry.entry_valid = UINT64_MAX;
60         })));
61 }
62
63 void expect_lookup(const char *relpath, uint64_t ino)
64 {
65         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
66 }
67 };
68
69 /*
70  * A successful link should clear the parent directory's attribute cache,
71  * because the fuse daemon should update its mtime and ctime
72  */
73 TEST_F(Link, clear_attr_cache)
74 {
75         const char FULLPATH[] = "mountpoint/src";
76         const char RELPATH[] = "src";
77         const char FULLDST[] = "mountpoint/dst";
78         const char RELDST[] = "dst";
79         const uint64_t ino = 42;
80         mode_t mode = S_IFREG | 0644;
81         struct stat sb;
82
83         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
84         EXPECT_CALL(*m_mock, process(
85                 ResultOf([=](auto in) {
86                         return (in->header.opcode == FUSE_GETATTR &&
87                                 in->header.nodeid == 1);
88                 }, Eq(true)),
89                 _)
90         ).Times(2)
91         .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
92                 SET_OUT_HEADER_LEN(out, attr);
93                 out->body.attr.attr.ino = 1;
94                 out->body.attr.attr.mode = S_IFDIR | 0755;
95                 out->body.attr.attr_valid = UINT64_MAX;
96         })));
97
98         EXPECT_LOOKUP(1, RELDST)
99         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
100                 SET_OUT_HEADER_LEN(out, entry);
101                 out->body.entry.attr.mode = mode;
102                 out->body.entry.nodeid = ino;
103                 out->body.entry.attr.nlink = 1;
104                 out->body.entry.attr_valid = UINT64_MAX;
105                 out->body.entry.entry_valid = UINT64_MAX;
106         })));
107         expect_link(ino, RELPATH, mode, 2);
108
109         EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
110         EXPECT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
111         EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
112 }
113
114 TEST_F(Link, emlink)
115 {
116         const char FULLPATH[] = "mountpoint/lnk";
117         const char RELPATH[] = "lnk";
118         const char FULLDST[] = "mountpoint/dst";
119         const char RELDST[] = "dst";
120         uint64_t dst_ino = 42;
121
122         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
123         expect_lookup(RELDST, dst_ino);
124
125         EXPECT_CALL(*m_mock, process(
126                 ResultOf([=](auto in) {
127                         const char *name = (const char*)in->body.bytes
128                                 + sizeof(struct fuse_link_in);
129                         return (in->header.opcode == FUSE_LINK &&
130                                 in->body.link.oldnodeid == dst_ino &&
131                                 (0 == strcmp(name, RELPATH)));
132                 }, Eq(true)),
133                 _)
134         ).WillOnce(Invoke(ReturnErrno(EMLINK)));
135
136         EXPECT_EQ(-1, link(FULLDST, FULLPATH));
137         EXPECT_EQ(EMLINK, errno);
138 }
139
140 TEST_F(Link, ok)
141 {
142         const char FULLPATH[] = "mountpoint/src";
143         const char RELPATH[] = "src";
144         const char FULLDST[] = "mountpoint/dst";
145         const char RELDST[] = "dst";
146         const uint64_t ino = 42;
147         mode_t mode = S_IFREG | 0644;
148         struct stat sb;
149
150         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
151         EXPECT_LOOKUP(1, RELDST)
152         .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
153                 SET_OUT_HEADER_LEN(out, entry);
154                 out->body.entry.attr.mode = mode;
155                 out->body.entry.nodeid = ino;
156                 out->body.entry.attr.nlink = 1;
157                 out->body.entry.attr_valid = UINT64_MAX;
158                 out->body.entry.entry_valid = UINT64_MAX;
159         })));
160         expect_link(ino, RELPATH, mode, 2);
161
162         ASSERT_EQ(0, link(FULLDST, FULLPATH)) << strerror(errno);
163         // Check that the original file's nlink count has increased.
164         ASSERT_EQ(0, stat(FULLDST, &sb)) << strerror(errno);
165         EXPECT_EQ(2ul, sb.st_nlink);
166 }