xref: /freebsd/tests/sys/fs/fusefs/dev_fuse_poll.cc (revision 29edc611c1de5413dc0a28056e26f9ec648dc413)
13429092cSAlan Somers /*-
23429092cSAlan Somers  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  */
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 *> {
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 {
760a7c63e0SAlan Somers 	virtual void SetUp() {
770a7c63e0SAlan Somers 		m_pm = KQ;
780a7c63e0SAlan Somers 		FuseTest::SetUp();
790a7c63e0SAlan Somers 	}
800a7c63e0SAlan Somers };
810a7c63e0SAlan Somers 
823429092cSAlan Somers TEST_P(DevFusePoll, access)
833429092cSAlan Somers {
843429092cSAlan Somers 	expect_access(1, 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 */
923429092cSAlan Somers TEST_P(DevFusePoll, destroy)
933429092cSAlan Somers {
943429092cSAlan Somers 	expect_forget(1, 1);
953429092cSAlan Somers 	expect_destroy(0);
963429092cSAlan Somers 
973429092cSAlan Somers 	m_mock->unmount();
983429092cSAlan Somers }
993429092cSAlan Somers 
1003429092cSAlan Somers INSTANTIATE_TEST_CASE_P(PM, DevFusePoll,
1013429092cSAlan Somers 		::testing::Values("BLOCKING", "KQ", "POLL", "SELECT"));
1020a7c63e0SAlan Somers 
1030a7c63e0SAlan Somers static void* statter(void* arg) {
1040a7c63e0SAlan Somers 	const char *name;
1050a7c63e0SAlan Somers 	struct stat sb;
1060a7c63e0SAlan Somers 
1070a7c63e0SAlan Somers 	name = (const char*)arg;
1080a7c63e0SAlan Somers 	stat(name, &sb);
1090a7c63e0SAlan Somers 	return 0;
1100a7c63e0SAlan Somers }
1110a7c63e0SAlan Somers 
1120a7c63e0SAlan Somers /*
1130a7c63e0SAlan Somers  * A kevent's data field should contain the number of operations available to
1140a7c63e0SAlan Somers  * be immediately rea.
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;
1250a7c63e0SAlan Somers 
1260a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
1270a7c63e0SAlan Somers 	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);
1280a7c63e0SAlan Somers 
1290a7c63e0SAlan Somers 	EXPECT_LOOKUP(1, "foo")
130*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1310a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
132*29edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
133*29edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
134*29edc611SAlan Somers 		out.body.entry.nodeid = foo_ino;
1350a7c63e0SAlan Somers 	})));
1360a7c63e0SAlan Somers 	EXPECT_LOOKUP(1, "bar")
137*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1380a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
139*29edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
140*29edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
141*29edc611SAlan Somers 		out.body.entry.nodeid = bar_ino;
1420a7c63e0SAlan Somers 	})));
1430a7c63e0SAlan Somers 	EXPECT_LOOKUP(1, "baz")
144*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1450a7c63e0SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
146*29edc611SAlan Somers 		out.body.entry.entry_valid = UINT64_MAX;
147*29edc611SAlan Somers 		out.body.entry.attr.mode = S_IFREG | 0644;
148*29edc611SAlan Somers 		out.body.entry.nodeid = baz_ino;
1490a7c63e0SAlan Somers 	})));
1500a7c63e0SAlan Somers 
1510a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1520a7c63e0SAlan Somers 		ResultOf([=](auto in) {
153*29edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
154*29edc611SAlan Somers 				in.header.nodeid == foo_ino);
1550a7c63e0SAlan Somers 		}, Eq(true)),
1560a7c63e0SAlan Somers 		_)
1570a7c63e0SAlan Somers 	)
158*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1590a7c63e0SAlan Somers 		nready0 = m_mock->m_nready;
1600a7c63e0SAlan Somers 
1610a7c63e0SAlan Somers 		sem_post(&sem0);
1620a7c63e0SAlan Somers 		// Block the daemon so we can accumulate a few more ops
1630a7c63e0SAlan Somers 		sem_wait(&sem1);
1640a7c63e0SAlan Somers 
165*29edc611SAlan Somers 		out.header.unique = in.header.unique;
166*29edc611SAlan Somers 		out.header.error = -EIO;
167*29edc611SAlan Somers 		out.header.len = sizeof(out.header);
1680a7c63e0SAlan Somers 	})));
1690a7c63e0SAlan Somers 
1700a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1710a7c63e0SAlan Somers 		ResultOf([=](auto in) {
172*29edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
173*29edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
174*29edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1750a7c63e0SAlan Somers 		}, Eq(true)),
1760a7c63e0SAlan Somers 		_)
1776fd2d8e1SAlan Somers 	).InSequence(seq)
178*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1790a7c63e0SAlan Somers 		nready1 = m_mock->m_nready;
180*29edc611SAlan Somers 		out.header.unique = in.header.unique;
181*29edc611SAlan Somers 		out.header.error = -EIO;
182*29edc611SAlan Somers 		out.header.len = sizeof(out.header);
1830a7c63e0SAlan Somers 	})));
1840a7c63e0SAlan Somers 	EXPECT_CALL(*m_mock, process(
1850a7c63e0SAlan Somers 		ResultOf([=](auto in) {
186*29edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
187*29edc611SAlan Somers 				(in.header.nodeid == bar_ino ||
188*29edc611SAlan Somers 				 in.header.nodeid == baz_ino));
1890a7c63e0SAlan Somers 		}, Eq(true)),
1900a7c63e0SAlan Somers 		_)
1916fd2d8e1SAlan Somers 	).InSequence(seq)
192*29edc611SAlan Somers 	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
1930a7c63e0SAlan Somers 		nready2 = m_mock->m_nready;
194*29edc611SAlan Somers 		out.header.unique = in.header.unique;
195*29edc611SAlan Somers 		out.header.error = -EIO;
196*29edc611SAlan Somers 		out.header.len = sizeof(out.header);
1970a7c63e0SAlan Somers 	})));
1980a7c63e0SAlan Somers 
1990a7c63e0SAlan Somers 	/*
2000a7c63e0SAlan Somers 	 * Create cached lookup entries for these files.  It seems that only
2010a7c63e0SAlan Somers 	 * one thread at a time can be in VOP_LOOKUP for a given directory
2020a7c63e0SAlan Somers 	 */
2030a7c63e0SAlan Somers 	access("mountpoint/foo", F_OK);
2040a7c63e0SAlan Somers 	access("mountpoint/bar", F_OK);
2050a7c63e0SAlan Somers 	access("mountpoint/baz", F_OK);
2060a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th0, NULL, statter,
2070a7c63e0SAlan Somers 		(void*)"mountpoint/foo")) << strerror(errno);
2080a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_wait(&sem0)) << strerror(errno);
2090a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th1, NULL, statter,
2100a7c63e0SAlan Somers 		(void*)"mountpoint/bar")) << strerror(errno);
2110a7c63e0SAlan Somers 	ASSERT_EQ(0, pthread_create(&th2, NULL, statter,
2120a7c63e0SAlan Somers 		(void*)"mountpoint/baz")) << strerror(errno);
2130a7c63e0SAlan Somers 
2140a7c63e0SAlan Somers 	nap();		// Allow th1 and th2 to send their ops to the daemon
2150a7c63e0SAlan Somers 	EXPECT_EQ(0, sem_post(&sem1)) << strerror(errno);
2160a7c63e0SAlan Somers 
2170a7c63e0SAlan Somers 	pthread_join(th0, NULL);
2180a7c63e0SAlan Somers 	pthread_join(th1, NULL);
2190a7c63e0SAlan Somers 	pthread_join(th2, NULL);
2200a7c63e0SAlan Somers 
2210a7c63e0SAlan Somers 	EXPECT_EQ(1, nready0);
2220a7c63e0SAlan Somers 	EXPECT_EQ(2, nready1);
2230a7c63e0SAlan Somers 	EXPECT_EQ(1, nready2);
2240a7c63e0SAlan Somers 
2250a7c63e0SAlan Somers 	sem_destroy(&sem0);
2260a7c63e0SAlan Somers 	sem_destroy(&sem1);
2270a7c63e0SAlan Somers }
228