xref: /freebsd/tests/sys/fs/fusefs/dev_fuse_poll.cc (revision 811e0a31acafd6ab21f89bec8ba0fb8c09f258d2)
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.
291fa8ebfbSAlan Somers  *
301fa8ebfbSAlan Somers  * $FreeBSD$
313429092cSAlan Somers  */
323429092cSAlan Somers 
333429092cSAlan Somers /*
343429092cSAlan Somers  * This file tests different polling methods for the /dev/fuse device
353429092cSAlan Somers  */
363429092cSAlan Somers 
373429092cSAlan Somers extern "C" {
383429092cSAlan Somers #include <fcntl.h>
390a7c63e0SAlan Somers #include <semaphore.h>
403429092cSAlan Somers #include <unistd.h>
413429092cSAlan Somers }
423429092cSAlan Somers 
433429092cSAlan Somers #include "mockfs.hh"
443429092cSAlan Somers #include "utils.hh"
453429092cSAlan Somers 
463429092cSAlan Somers using namespace testing;
473429092cSAlan Somers 
483429092cSAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
493429092cSAlan Somers const char RELPATH[] = "some_file.txt";
503429092cSAlan Somers const uint64_t ino = 42;
513429092cSAlan Somers const mode_t access_mode = R_OK;
523429092cSAlan Somers 
533429092cSAlan Somers /*
543429092cSAlan Somers  * Translate a poll method's string representation to the enum value.
553429092cSAlan Somers  * Using strings with ::testing::Values gives better output with
563429092cSAlan Somers  * --gtest_list_tests
573429092cSAlan Somers  */
583429092cSAlan Somers enum poll_method poll_method_from_string(const char *s)
593429092cSAlan Somers {
603429092cSAlan Somers 	if (0 == strcmp("BLOCKING", s))
613429092cSAlan Somers 		return BLOCKING;
623429092cSAlan Somers 	else if (0 == strcmp("KQ", s))
633429092cSAlan Somers 		return KQ;
643429092cSAlan Somers 	else if (0 == strcmp("POLL", s))
653429092cSAlan Somers 		return POLL;
663429092cSAlan Somers 	else
673429092cSAlan Somers 		return SELECT;
683429092cSAlan Somers }
693429092cSAlan Somers 
703429092cSAlan Somers class DevFusePoll: public FuseTest, public WithParamInterface<const char *> {
713429092cSAlan Somers 	virtual void SetUp() {
723429092cSAlan Somers 		m_pm = poll_method_from_string(GetParam());
733429092cSAlan Somers 		FuseTest::SetUp();
743429092cSAlan Somers 	}
753429092cSAlan Somers };
763429092cSAlan Somers 
770a7c63e0SAlan Somers class Kqueue: public FuseTest {
780a7c63e0SAlan Somers 	virtual void SetUp() {
790a7c63e0SAlan Somers 		m_pm = KQ;
800a7c63e0SAlan Somers 		FuseTest::SetUp();
810a7c63e0SAlan Somers 	}
820a7c63e0SAlan Somers };
830a7c63e0SAlan Somers 
843429092cSAlan Somers TEST_P(DevFusePoll, access)
853429092cSAlan Somers {
86a34cdd26SAlan Somers 	expect_access(FUSE_ROOT_ID, X_OK, 0);
873429092cSAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
883429092cSAlan Somers 	expect_access(ino, access_mode, 0);
893429092cSAlan Somers 
903429092cSAlan Somers 	ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
913429092cSAlan Somers }
923429092cSAlan Somers 
933429092cSAlan Somers /* Ensure that we wake up pollers during unmount */
943429092cSAlan Somers TEST_P(DevFusePoll, destroy)
953429092cSAlan Somers {
963429092cSAlan Somers 	expect_destroy(0);
973429092cSAlan Somers 
983429092cSAlan Somers 	m_mock->unmount();
993429092cSAlan Somers }
1003429092cSAlan Somers 
101*811e0a31SEnji Cooper INSTANTIATE_TEST_SUITE_P(PM, DevFusePoll,
1023429092cSAlan Somers 		::testing::Values("BLOCKING", "KQ", "POLL", "SELECT"));
1030a7c63e0SAlan Somers 
1040a7c63e0SAlan Somers static void* statter(void* arg) {
1050a7c63e0SAlan Somers 	const char *name;
1060a7c63e0SAlan Somers 	struct stat sb;
1070a7c63e0SAlan Somers 
1080a7c63e0SAlan Somers 	name = (const char*)arg;
1098e765737SAlan Somers 	return ((void*)(intptr_t)stat(name, &sb));
1100a7c63e0SAlan Somers }
1110a7c63e0SAlan Somers 
1120a7c63e0SAlan Somers /*
1130a7c63e0SAlan Somers  * A kevent's data field should contain the number of operations available to
1148e765737SAlan Somers  * be immediately read.
1150a7c63e0SAlan Somers  */
1160a7c63e0SAlan Somers TEST_F(Kqueue, data)
1170a7c63e0SAlan Somers {
1180a7c63e0SAlan Somers 	pthread_t th0, th1, th2;
1190a7c63e0SAlan Somers 	sem_t sem0, sem1;
1200a7c63e0SAlan Somers 	int nready0, nready1, nready2;
1210a7c63e0SAlan Somers 	uint64_t foo_ino = 42;
1220a7c63e0SAlan Somers 	uint64_t bar_ino = 43;
1230a7c63e0SAlan Somers 	uint64_t baz_ino = 44;
1246fd2d8e1SAlan Somers 	Sequence seq;
1258e765737SAlan Somers 	void *th_ret;
1260a7c63e0SAlan Somers 
1270a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
1280a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);
1290a7c63e0SAlan Somers 
130a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
13129edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1320a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
13329edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
13429edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
13529edc611SAlan Somers 		out.body.entry.nodeid = foo_ino;
1360a7c63e0SAlan Somers 	})));
137a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "bar")
13829edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1390a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
14029edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
14129edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
14229edc611SAlan Somers 		out.body.entry.nodeid = bar_ino;
1430a7c63e0SAlan Somers 	})));
144a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, "baz")
14529edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1460a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
14729edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
14829edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
14929edc611SAlan Somers 		out.body.entry.nodeid = baz_ino;
1500a7c63e0SAlan Somers 	})));
1510a7c63e0SAlan Somers 
1520a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1530a7c63e0SAlan Somers 		ResultOf([=](auto in) {
15429edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
15529edc611SAlan Somers 				in.header.nodeid == foo_ino);
1560a7c63e0SAlan Somers 		}, Eq(true)),
1570a7c63e0SAlan Somers 		_)
1580a7c63e0SAlan Somers 	)
15929edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1600a7c63e0SAlan Somers 		nready0 = m_mock->m_nready;
1610a7c63e0SAlan Somers 
1620a7c63e0SAlan Somers 		sem_post(&sem0);
1630a7c63e0SAlan Somers 		// Block the daemon so we can accumulate a few more ops
1640a7c63e0SAlan Somers 		sem_wait(&sem1);
1650a7c63e0SAlan Somers 
16629edc611SAlan Somers 		out.header.unique = in.header.unique;
16729edc611SAlan Somers 		out.header.error = -EIO;
16829edc611SAlan Somers 		out.header.len = sizeof(out.header);
1690a7c63e0SAlan Somers 	})));
1700a7c63e0SAlan Somers 
1710a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1720a7c63e0SAlan Somers 		ResultOf([=](auto in) {
17329edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
17429edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
17529edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1760a7c63e0SAlan Somers 		}, Eq(true)),
1770a7c63e0SAlan Somers 		_)
1786fd2d8e1SAlan Somers 	).InSequence(seq)
17929edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1800a7c63e0SAlan Somers 		nready1 = m_mock->m_nready;
18129edc611SAlan Somers 		out.header.unique = in.header.unique;
18229edc611SAlan Somers 		out.header.error = -EIO;
18329edc611SAlan Somers 		out.header.len = sizeof(out.header);
1840a7c63e0SAlan Somers 	})));
1850a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1860a7c63e0SAlan Somers 		ResultOf([=](auto in) {
18729edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
18829edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
18929edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1900a7c63e0SAlan Somers 		}, Eq(true)),
1910a7c63e0SAlan Somers 		_)
1926fd2d8e1SAlan Somers 	).InSequence(seq)
19329edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1940a7c63e0SAlan Somers 		nready2 = m_mock->m_nready;
19529edc611SAlan Somers 		out.header.unique = in.header.unique;
19629edc611SAlan Somers 		out.header.error = -EIO;
19729edc611SAlan Somers 		out.header.len = sizeof(out.header);
1980a7c63e0SAlan Somers 	})));
1990a7c63e0SAlan Somers 
2000a7c63e0SAlan Somers 	/*
2010a7c63e0SAlan Somers 	 * Create cached lookup entries for these files.  It seems that only
2020a7c63e0SAlan Somers 	 * one thread at a time can be in VOP_LOOKUP for a given directory
2030a7c63e0SAlan Somers 	 */
2040a7c63e0SAlan Somers 	access("mountpoint/foo", F_OK);
2050a7c63e0SAlan Somers 	access("mountpoint/bar", F_OK);
2060a7c63e0SAlan Somers 	access("mountpoint/baz", F_OK);
2070a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th0, NULL, statter,
2085a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/foo"))) << strerror(errno);
2090a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_wait(&sem0)) << strerror(errno);
2100a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th1, NULL, statter,
2115a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/bar"))) << strerror(errno);
2120a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th2, NULL, statter,
2135a0b9a27SAlan Somers 		__DECONST(void*, "mountpoint/baz"))) << strerror(errno);
2140a7c63e0SAlan Somers 
2150a7c63e0SAlan Somers 	nap();		// Allow th1 and th2 to send their ops to the daemon
2160a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_post(&sem1)) << strerror(errno);
2170a7c63e0SAlan Somers 
2188e765737SAlan Somers 	pthread_join(th0, &th_ret);
2198e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2208e765737SAlan Somers 	pthread_join(th1, &th_ret);
2218e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2228e765737SAlan Somers 	pthread_join(th2, &th_ret);
2238e765737SAlan Somers 	ASSERT_EQ(-1, (intptr_t)th_ret);
2240a7c63e0SAlan Somers 
2250a7c63e0SAlan Somers 	EXPECT_EQ(1, nready0);
2260a7c63e0SAlan Somers 	EXPECT_EQ(2, nready1);
2270a7c63e0SAlan Somers 	EXPECT_EQ(1, nready2);
2280a7c63e0SAlan Somers 
2290a7c63e0SAlan Somers 	sem_destroy(&sem0);
2300a7c63e0SAlan Somers 	sem_destroy(&sem1);
2310a7c63e0SAlan Somers }
232