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. 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 { 96a34cdd26SAlan Somers expect_forget(FUSE_ROOT_ID, 1); 973429092cSAlan Somers expect_destroy(0); 983429092cSAlan Somers 993429092cSAlan Somers m_mock->unmount(); 1003429092cSAlan Somers } 1013429092cSAlan Somers 1023429092cSAlan Somers INSTANTIATE_TEST_CASE_P(PM, DevFusePoll, 1033429092cSAlan Somers ::testing::Values("BLOCKING", "KQ", "POLL", "SELECT")); 1040a7c63e0SAlan Somers 1050a7c63e0SAlan Somers static void* statter(void* arg) { 1060a7c63e0SAlan Somers const char *name; 1070a7c63e0SAlan Somers struct stat sb; 1080a7c63e0SAlan Somers 1090a7c63e0SAlan Somers name = (const char*)arg; 110*8e765737SAlan Somers return ((void*)(intptr_t)stat(name, &sb)); 1110a7c63e0SAlan Somers } 1120a7c63e0SAlan Somers 1130a7c63e0SAlan Somers /* 1140a7c63e0SAlan Somers * A kevent's data field should contain the number of operations available to 115*8e765737SAlan Somers * be immediately read. 1160a7c63e0SAlan Somers */ 1170a7c63e0SAlan Somers TEST_F(Kqueue, data) 1180a7c63e0SAlan Somers { 1190a7c63e0SAlan Somers pthread_t th0, th1, th2; 1200a7c63e0SAlan Somers sem_t sem0, sem1; 1210a7c63e0SAlan Somers int nready0, nready1, nready2; 1220a7c63e0SAlan Somers uint64_t foo_ino = 42; 1230a7c63e0SAlan Somers uint64_t bar_ino = 43; 1240a7c63e0SAlan Somers uint64_t baz_ino = 44; 1256fd2d8e1SAlan Somers Sequence seq; 126*8e765737SAlan Somers void *th_ret; 1270a7c63e0SAlan Somers 1280a7c63e0SAlan Somers ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno); 1290a7c63e0SAlan Somers ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno); 1300a7c63e0SAlan Somers 131a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, "foo") 13229edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 1330a7c63e0SAlan Somers SET_OUT_HEADER_LEN(out, entry); 13429edc611SAlan Somers out.body.entry.entry_valid = UINT64_MAX; 13529edc611SAlan Somers out.body.entry.attr.mode = S_IFREG | 0644; 13629edc611SAlan Somers out.body.entry.nodeid = foo_ino; 1370a7c63e0SAlan Somers }))); 138a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, "bar") 13929edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 1400a7c63e0SAlan Somers SET_OUT_HEADER_LEN(out, entry); 14129edc611SAlan Somers out.body.entry.entry_valid = UINT64_MAX; 14229edc611SAlan Somers out.body.entry.attr.mode = S_IFREG | 0644; 14329edc611SAlan Somers out.body.entry.nodeid = bar_ino; 1440a7c63e0SAlan Somers }))); 145a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, "baz") 14629edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 1470a7c63e0SAlan Somers SET_OUT_HEADER_LEN(out, entry); 14829edc611SAlan Somers out.body.entry.entry_valid = UINT64_MAX; 14929edc611SAlan Somers out.body.entry.attr.mode = S_IFREG | 0644; 15029edc611SAlan Somers out.body.entry.nodeid = baz_ino; 1510a7c63e0SAlan Somers }))); 1520a7c63e0SAlan Somers 1530a7c63e0SAlan Somers EXPECT_CALL(*m_mock, process( 1540a7c63e0SAlan Somers ResultOf([=](auto in) { 15529edc611SAlan Somers return (in.header.opcode == FUSE_GETATTR && 15629edc611SAlan Somers in.header.nodeid == foo_ino); 1570a7c63e0SAlan Somers }, Eq(true)), 1580a7c63e0SAlan Somers _) 1590a7c63e0SAlan Somers ) 16029edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) { 1610a7c63e0SAlan Somers nready0 = m_mock->m_nready; 1620a7c63e0SAlan Somers 1630a7c63e0SAlan Somers sem_post(&sem0); 1640a7c63e0SAlan Somers // Block the daemon so we can accumulate a few more ops 1650a7c63e0SAlan Somers sem_wait(&sem1); 1660a7c63e0SAlan Somers 16729edc611SAlan Somers out.header.unique = in.header.unique; 16829edc611SAlan Somers out.header.error = -EIO; 16929edc611SAlan Somers out.header.len = sizeof(out.header); 1700a7c63e0SAlan Somers }))); 1710a7c63e0SAlan Somers 1720a7c63e0SAlan Somers EXPECT_CALL(*m_mock, process( 1730a7c63e0SAlan Somers ResultOf([=](auto in) { 17429edc611SAlan Somers return (in.header.opcode == FUSE_GETATTR && 17529edc611SAlan Somers (in.header.nodeid == bar_ino || 17629edc611SAlan Somers in.header.nodeid == baz_ino)); 1770a7c63e0SAlan Somers }, Eq(true)), 1780a7c63e0SAlan Somers _) 1796fd2d8e1SAlan Somers ).InSequence(seq) 18029edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) { 1810a7c63e0SAlan Somers nready1 = m_mock->m_nready; 18229edc611SAlan Somers out.header.unique = in.header.unique; 18329edc611SAlan Somers out.header.error = -EIO; 18429edc611SAlan Somers out.header.len = sizeof(out.header); 1850a7c63e0SAlan Somers }))); 1860a7c63e0SAlan Somers EXPECT_CALL(*m_mock, process( 1870a7c63e0SAlan Somers ResultOf([=](auto in) { 18829edc611SAlan Somers return (in.header.opcode == FUSE_GETATTR && 18929edc611SAlan Somers (in.header.nodeid == bar_ino || 19029edc611SAlan Somers in.header.nodeid == baz_ino)); 1910a7c63e0SAlan Somers }, Eq(true)), 1920a7c63e0SAlan Somers _) 1936fd2d8e1SAlan Somers ).InSequence(seq) 19429edc611SAlan Somers .WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) { 1950a7c63e0SAlan Somers nready2 = m_mock->m_nready; 19629edc611SAlan Somers out.header.unique = in.header.unique; 19729edc611SAlan Somers out.header.error = -EIO; 19829edc611SAlan Somers out.header.len = sizeof(out.header); 1990a7c63e0SAlan Somers }))); 2000a7c63e0SAlan Somers 2010a7c63e0SAlan Somers /* 2020a7c63e0SAlan Somers * Create cached lookup entries for these files. It seems that only 2030a7c63e0SAlan Somers * one thread at a time can be in VOP_LOOKUP for a given directory 2040a7c63e0SAlan Somers */ 2050a7c63e0SAlan Somers access("mountpoint/foo", F_OK); 2060a7c63e0SAlan Somers access("mountpoint/bar", F_OK); 2070a7c63e0SAlan Somers access("mountpoint/baz", F_OK); 2080a7c63e0SAlan Somers ASSERT_EQ(0, pthread_create(&th0, NULL, statter, 2095a0b9a27SAlan Somers __DECONST(void*, "mountpoint/foo"))) << strerror(errno); 2100a7c63e0SAlan Somers EXPECT_EQ(0, sem_wait(&sem0)) << strerror(errno); 2110a7c63e0SAlan Somers ASSERT_EQ(0, pthread_create(&th1, NULL, statter, 2125a0b9a27SAlan Somers __DECONST(void*, "mountpoint/bar"))) << strerror(errno); 2130a7c63e0SAlan Somers ASSERT_EQ(0, pthread_create(&th2, NULL, statter, 2145a0b9a27SAlan Somers __DECONST(void*, "mountpoint/baz"))) << strerror(errno); 2150a7c63e0SAlan Somers 2160a7c63e0SAlan Somers nap(); // Allow th1 and th2 to send their ops to the daemon 2170a7c63e0SAlan Somers EXPECT_EQ(0, sem_post(&sem1)) << strerror(errno); 2180a7c63e0SAlan Somers 219*8e765737SAlan Somers pthread_join(th0, &th_ret); 220*8e765737SAlan Somers ASSERT_EQ(-1, (intptr_t)th_ret); 221*8e765737SAlan Somers pthread_join(th1, &th_ret); 222*8e765737SAlan Somers ASSERT_EQ(-1, (intptr_t)th_ret); 223*8e765737SAlan Somers pthread_join(th2, &th_ret); 224*8e765737SAlan Somers ASSERT_EQ(-1, (intptr_t)th_ret); 2250a7c63e0SAlan Somers 2260a7c63e0SAlan Somers EXPECT_EQ(1, nready0); 2270a7c63e0SAlan Somers EXPECT_EQ(2, nready1); 2280a7c63e0SAlan Somers EXPECT_EQ(1, nready2); 2290a7c63e0SAlan Somers 2300a7c63e0SAlan Somers sem_destroy(&sem0); 2310a7c63e0SAlan Somers sem_destroy(&sem1); 2320a7c63e0SAlan Somers } 233