xref: /freebsd/tests/sys/fs/fusefs/dev_fuse_poll.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
13429092cSAlan Somers /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33429092cSAlan Somers  *
43429092cSAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
53429092cSAlan Somers  *
63429092cSAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
73429092cSAlan Somers  * from the FreeBSD Foundation.
83429092cSAlan Somers  *
93429092cSAlan Somers  * Redistribution and use in source and binary forms, with or without
103429092cSAlan Somers  * modification, are permitted provided that the following conditions
113429092cSAlan Somers  * are met:
123429092cSAlan Somers  * 1. Redistributions of source code must retain the above copyright
133429092cSAlan Somers  *    notice, this list of conditions and the following disclaimer.
143429092cSAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
153429092cSAlan Somers  *    notice, this list of conditions and the following disclaimer in the
163429092cSAlan Somers  *    documentation and/or other materials provided with the distribution.
173429092cSAlan Somers  *
183429092cSAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
193429092cSAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203429092cSAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
213429092cSAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
223429092cSAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
233429092cSAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
243429092cSAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
253429092cSAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263429092cSAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
273429092cSAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
283429092cSAlan Somers  * SUCH DAMAGE.
293429092cSAlan Somers  */
303429092cSAlan Somers 
313429092cSAlan Somers /*
323429092cSAlan Somers  * This file tests different polling methods for the /dev/fuse device
333429092cSAlan Somers  */
343429092cSAlan Somers 
353429092cSAlan Somers extern "C" {
363429092cSAlan Somers #include <fcntl.h>
370a7c63e0SAlan Somers #include <semaphore.h>
383429092cSAlan Somers #include <unistd.h>
393429092cSAlan Somers }
403429092cSAlan Somers 
413429092cSAlan Somers #include "mockfs.hh"
423429092cSAlan Somers #include "utils.hh"
433429092cSAlan Somers 
443429092cSAlan Somers using namespace testing;
453429092cSAlan Somers 
463429092cSAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
473429092cSAlan Somers const char RELPATH[] = "some_file.txt";
483429092cSAlan Somers const uint64_t ino = 42;
493429092cSAlan Somers const mode_t access_mode = R_OK;
503429092cSAlan Somers 
513429092cSAlan Somers /*
523429092cSAlan Somers  * Translate a poll method's string representation to the enum value.
533429092cSAlan Somers  * Using strings with ::testing::Values gives better output with
543429092cSAlan Somers  * --gtest_list_tests
553429092cSAlan Somers  */
poll_method_from_string(const char * s)563429092cSAlan Somers enum poll_method poll_method_from_string(const char *s)
573429092cSAlan Somers {
583429092cSAlan Somers 	if (0 == strcmp("BLOCKING", s))
593429092cSAlan Somers 		return BLOCKING;
603429092cSAlan Somers 	else if (0 == strcmp("KQ", s))
613429092cSAlan Somers 		return KQ;
623429092cSAlan Somers 	else if (0 == strcmp("POLL", s))
633429092cSAlan Somers 		return POLL;
643429092cSAlan Somers 	else
653429092cSAlan Somers 		return SELECT;
663429092cSAlan Somers }
673429092cSAlan Somers 
683429092cSAlan Somers class DevFusePoll: public FuseTest, public WithParamInterface<const char *> {
SetUp()693429092cSAlan Somers 	virtual void SetUp() {
703429092cSAlan Somers 		m_pm = poll_method_from_string(GetParam());
713429092cSAlan Somers 		FuseTest::SetUp();
723429092cSAlan Somers 	}
733429092cSAlan Somers };
743429092cSAlan Somers 
750a7c63e0SAlan Somers class Kqueue: public FuseTest {
SetUp()760a7c63e0SAlan Somers 	virtual void SetUp() {
770a7c63e0SAlan Somers 		m_pm = KQ;
780a7c63e0SAlan Somers 		FuseTest::SetUp();
790a7c63e0SAlan Somers 	}
800a7c63e0SAlan Somers };
810a7c63e0SAlan Somers 
TEST_P(DevFusePoll,access)823429092cSAlan Somers TEST_P(DevFusePoll, access)
833429092cSAlan Somers {
84a34cdd26SAlan Somers 	expect_access(FUSE_ROOT_ID, X_OK, 0);
853429092cSAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
863429092cSAlan Somers 	expect_access(ino, access_mode, 0);
873429092cSAlan Somers 
883429092cSAlan Somers 	ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
893429092cSAlan Somers }
903429092cSAlan Somers 
913429092cSAlan Somers /* Ensure that we wake up pollers during unmount */
TEST_P(DevFusePoll,destroy)923429092cSAlan Somers TEST_P(DevFusePoll, destroy)
933429092cSAlan Somers {
943429092cSAlan Somers 	expect_destroy(0);
953429092cSAlan Somers 
963429092cSAlan Somers 	m_mock->unmount();
973429092cSAlan Somers }
983429092cSAlan Somers 
99*811e0a31SEnji Cooper INSTANTIATE_TEST_SUITE_P(PM, DevFusePoll,
1003429092cSAlan Somers 		::testing::Values("BLOCKING", "KQ", "POLL", "SELECT"));
1010a7c63e0SAlan Somers 
statter(void * arg)1020a7c63e0SAlan Somers static void* statter(void* arg) {
1030a7c63e0SAlan Somers 	const char *name;
1040a7c63e0SAlan Somers 	struct stat sb;
1050a7c63e0SAlan Somers 
1060a7c63e0SAlan Somers 	name = (const char*)arg;
1078e765737SAlan Somers 	return ((void*)(intptr_t)stat(name, &sb));
1080a7c63e0SAlan Somers }
1090a7c63e0SAlan Somers 
1100a7c63e0SAlan Somers /*
1110a7c63e0SAlan Somers  * A kevent's data field should contain the number of operations available to
1128e765737SAlan Somers  * be immediately read.
1130a7c63e0SAlan Somers  */
TEST_F(Kqueue,data)1140a7c63e0SAlan Somers TEST_F(Kqueue, data)
1150a7c63e0SAlan Somers {
1160a7c63e0SAlan Somers 	pthread_t th0, th1, th2;
1170a7c63e0SAlan Somers 	sem_t sem0, sem1;
1180a7c63e0SAlan Somers 	int nready0, nready1, nready2;
1190a7c63e0SAlan Somers 	uint64_t foo_ino = 42;
1200a7c63e0SAlan Somers 	uint64_t bar_ino = 43;
1210a7c63e0SAlan Somers 	uint64_t baz_ino = 44;
1226fd2d8e1SAlan Somers 	Sequence seq;
1238e765737SAlan Somers 	void *th_ret;
1240a7c63e0SAlan Somers 
1250a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
1260a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);
1270a7c63e0SAlan Somers 
128a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
12929edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1300a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
13129edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
13229edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
13329edc611SAlan Somers 		out.body.entry.nodeid = foo_ino;
1340a7c63e0SAlan Somers 	})));
135a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "bar")
13629edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1370a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
13829edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
13929edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
14029edc611SAlan Somers 		out.body.entry.nodeid = bar_ino;
1410a7c63e0SAlan Somers 	})));
142a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "baz")
14329edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1440a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
14529edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
14629edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
14729edc611SAlan Somers 		out.body.entry.nodeid = baz_ino;
1480a7c63e0SAlan Somers 	})));
1490a7c63e0SAlan Somers 
1500a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1510a7c63e0SAlan Somers 		ResultOf([=](auto in) {
15229edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
15329edc611SAlan Somers 				in.header.nodeid == foo_ino);
1540a7c63e0SAlan Somers 		}, Eq(true)),
1550a7c63e0SAlan Somers 		_)
1560a7c63e0SAlan Somers 	)
15729edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1580a7c63e0SAlan Somers 		nready0 = m_mock->m_nready;
1590a7c63e0SAlan Somers 
1600a7c63e0SAlan Somers 		sem_post(&sem0);
1610a7c63e0SAlan Somers 		// Block the daemon so we can accumulate a few more ops
1620a7c63e0SAlan Somers 		sem_wait(&sem1);
1630a7c63e0SAlan Somers 
16429edc611SAlan Somers 		out.header.unique = in.header.unique;
16529edc611SAlan Somers 		out.header.error = -EIO;
16629edc611SAlan Somers 		out.header.len = sizeof(out.header);
1670a7c63e0SAlan Somers 	})));
1680a7c63e0SAlan Somers 
1690a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1700a7c63e0SAlan Somers 		ResultOf([=](auto in) {
17129edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
17229edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
17329edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1740a7c63e0SAlan Somers 		}, Eq(true)),
1750a7c63e0SAlan Somers 		_)
1766fd2d8e1SAlan Somers 	).InSequence(seq)
17729edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1780a7c63e0SAlan Somers 		nready1 = m_mock->m_nready;
17929edc611SAlan Somers 		out.header.unique = in.header.unique;
18029edc611SAlan Somers 		out.header.error = -EIO;
18129edc611SAlan Somers 		out.header.len = sizeof(out.header);
1820a7c63e0SAlan Somers 	})));
1830a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1840a7c63e0SAlan Somers 		ResultOf([=](auto in) {
18529edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
18629edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
18729edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1880a7c63e0SAlan Somers 		}, Eq(true)),
1890a7c63e0SAlan Somers 		_)
1906fd2d8e1SAlan Somers 	).InSequence(seq)
19129edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1920a7c63e0SAlan Somers 		nready2 = m_mock->m_nready;
19329edc611SAlan Somers 		out.header.unique = in.header.unique;
19429edc611SAlan Somers 		out.header.error = -EIO;
19529edc611SAlan Somers 		out.header.len = sizeof(out.header);
1960a7c63e0SAlan Somers 	})));
1970a7c63e0SAlan Somers 
1980a7c63e0SAlan Somers 	/*
1990a7c63e0SAlan Somers 	 * Create cached lookup entries for these files.  It seems that only
2000a7c63e0SAlan Somers 	 * one thread at a time can be in VOP_LOOKUP for a given directory
2010a7c63e0SAlan Somers 	 */
2020a7c63e0SAlan Somers 	access("mountpoint/foo", F_OK);
2030a7c63e0SAlan Somers 	access("mountpoint/bar", F_OK);
2040a7c63e0SAlan Somers 	access("mountpoint/baz", F_OK);
2050a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th0, NULL, statter,
2065a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/foo"))) << strerror(errno);
2070a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_wait(&sem0)) << strerror(errno);
2080a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th1, NULL, statter,
2095a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/bar"))) << strerror(errno);
2100a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th2, NULL, statter,
2115a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/baz"))) << strerror(errno);
2120a7c63e0SAlan Somers 
2130a7c63e0SAlan Somers 	nap();		// Allow th1 and th2 to send their ops to the daemon
2140a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_post(&sem1)) << strerror(errno);
2150a7c63e0SAlan Somers 
2168e765737SAlan Somers 	pthread_join(th0, &th_ret);
2178e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2188e765737SAlan Somers 	pthread_join(th1, &th_ret);
2198e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2208e765737SAlan Somers 	pthread_join(th2, &th_ret);
2218e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2220a7c63e0SAlan Somers 
2230a7c63e0SAlan Somers 	EXPECT_EQ(1, nready0);
2240a7c63e0SAlan Somers 	EXPECT_EQ(2, nready1);
2250a7c63e0SAlan Somers 	EXPECT_EQ(1, nready2);
2260a7c63e0SAlan Somers 
2270a7c63e0SAlan Somers 	sem_destroy(&sem0);
2280a7c63e0SAlan Somers 	sem_destroy(&sem1);
2290a7c63e0SAlan Somers }
230