@@ -1722,138 +1722,118 @@ size_t sanitize_iovecs(std::vector<iovec>& iov, size_t disk_alignment) noexcept
17221722future<file>
17231723reactor::open_file_dma (std::string_view nameref, open_flags flags, file_open_options options) noexcept  {
17241724    auto  open_flags = static_cast <int >(flags);
1725-     {
1726-         sstring name (nameref);
1727-         syscall_result_extra<struct  stat > sr = co_await  _thread_pool->submit <syscall_result_extra<struct  stat >>(
1728-                 internal::thread_pool_submit_reason::file_operation, [this , name, &open_flags, &options, strict_o_direct = _cfg.strict_o_direct , bypass_fsync = _cfg.bypass_fsync ] () mutable  {
1729-             //  We want O_DIRECT, except in three cases:
1730-             //    - tmpfs (which doesn't support it, but works fine anyway)
1731-             //    - strict_o_direct == false (where we forgive it being not supported)
1732-             //    - kernel_page_cache == true (where we disable it for short-lived test processes)
1733-             //  Because open() with O_DIRECT will fail, we open it without O_DIRECT, try
1734-             //  to update it to O_DIRECT with fcntl(), and if that fails, see if we
1735-             //  can forgive it.
1736-             auto  is_tmpfs = [] (int  fd) {
1737-                 struct  ::statfs buf;
1738-                 auto  r = ::fstatfs (fd, &buf);
1739-                 if  (r == -1 ) {
1740-                     return  false ;
1741-                 }
1742-                 return  buf.f_type  == internal::fs_magic::tmpfs;
1743-             };
1744-             open_flags |= O_CLOEXEC;
1745-             if  (bypass_fsync) {
1746-                 open_flags &= ~O_DSYNC;
1747-             }
1748-             struct  stat  st;
1749-             auto  mode = static_cast <mode_t >(options.create_permissions );
1750-             int  fd = ::open (name.c_str (), open_flags, mode);
1751-             if  (fd == -1 ) {
1752-                 return  wrap_syscall (fd, st);
1725+     sstring name (nameref);
1726+     syscall_result_extra<struct  stat > sr = co_await  _thread_pool->submit <syscall_result_extra<struct  stat >>(
1727+             internal::thread_pool_submit_reason::file_operation, [this , name, &open_flags, &options, strict_o_direct = _cfg.strict_o_direct , bypass_fsync = _cfg.bypass_fsync ] () mutable  {
1728+         //  We want O_DIRECT, except in three cases:
1729+         //    - tmpfs (which doesn't support it, but works fine anyway)
1730+         //    - strict_o_direct == false (where we forgive it being not supported)
1731+         //    - kernel_page_cache == true (where we disable it for short-lived test processes)
1732+         //  Because open() with O_DIRECT will fail, we open it without O_DIRECT, try
1733+         //  to update it to O_DIRECT with fcntl(), and if that fails, see if we
1734+         //  can forgive it.
1735+         auto  is_tmpfs = [] (int  fd) {
1736+             struct  ::statfs buf;
1737+             auto  r = ::fstatfs (fd, &buf);
1738+             if  (r == -1 ) {
1739+                 return  false ;
17531740            }
1754-             auto  close_fd = defer ([fd] () noexcept  { ::close (fd); });
1755-             int  o_direct_flag = _cfg.kernel_page_cache  ? 0  : O_DIRECT;
1756-             int  r = ::fcntl (fd, F_SETFL, open_flags | o_direct_flag);
1757-             if  (r == -1   && strict_o_direct) {
1758-                 auto  maybe_ret = wrap_syscall (r, st);  //  capture errno (should be EINVAL)
1759-                 if  (!is_tmpfs (fd)) {
1760-                     return  maybe_ret;
1761-                 }
1741+             return  buf.f_type  == internal::fs_magic::tmpfs;
1742+         };
1743+         open_flags |= O_CLOEXEC;
1744+         if  (bypass_fsync) {
1745+             open_flags &= ~O_DSYNC;
1746+         }
1747+         struct  stat  st;
1748+         auto  mode = static_cast <mode_t >(options.create_permissions );
1749+         int  fd = ::open (name.c_str (), open_flags, mode);
1750+         if  (fd == -1 ) {
1751+             return  wrap_syscall (fd, st);
1752+         }
1753+         auto  close_fd = defer ([fd] () noexcept  { ::close (fd); });
1754+         int  o_direct_flag = _cfg.kernel_page_cache  ? 0  : O_DIRECT;
1755+         int  r = ::fcntl (fd, F_SETFL, open_flags | o_direct_flag);
1756+         if  (r == -1   && strict_o_direct) {
1757+             auto  maybe_ret = wrap_syscall (r, st);  //  capture errno (should be EINVAL)
1758+             if  (!is_tmpfs (fd)) {
1759+                 return  maybe_ret;
17621760            }
1763-             if  (fd != -1  && options.extent_allocation_size_hint  && !_cfg.kernel_page_cache ) {
1764-                 fsxattr attr = {};
1765-                 int  r = ::ioctl (fd, XFS_IOC_FSGETXATTR, &attr);
1766-                 //  xfs delayed allocation is disabled when extent size hints are present.
1767-                 //  This causes tons of xfs log fsyncs. Given that extent size hints are
1768-                 //  unneeded when delayed allocation is available (which is the case
1769-                 //  when not using O_DIRECT), disable them.
1770-                 // 
1771-                 //  Ignore error; may be !xfs, and just a hint anyway
1772-                 if  (r != -1 ) {
1773-                     attr.fsx_xflags  |= XFS_XFLAG_EXTSIZE;
1774-                     attr.fsx_extsize  = std::min (options.extent_allocation_size_hint ,
1775-                                         file_open_options::max_extent_allocation_size_hint);
1761+         }
1762+         if  (fd != -1  && options.extent_allocation_size_hint  && !_cfg.kernel_page_cache ) {
1763+             fsxattr attr = {};
1764+             int  r = ::ioctl (fd, XFS_IOC_FSGETXATTR, &attr);
1765+             //  xfs delayed allocation is disabled when extent size hints are present.
1766+             //  This causes tons of xfs log fsyncs. Given that extent size hints are
1767+             //  unneeded when delayed allocation is available (which is the case
1768+             //  when not using O_DIRECT), disable them.
1769+             // 
1770+             //  Ignore error; may be !xfs, and just a hint anyway
1771+             if  (r != -1 ) {
1772+                 attr.fsx_xflags  |= XFS_XFLAG_EXTSIZE;
1773+                 attr.fsx_extsize  = std::min (options.extent_allocation_size_hint ,
1774+                                     file_open_options::max_extent_allocation_size_hint);
17761775
1777-                      attr.fsx_extsize  = align_up<uint32_t >(attr.fsx_extsize , file_open_options::min_extent_size_hint_alignment);
1776+                 attr.fsx_extsize  = align_up<uint32_t >(attr.fsx_extsize , file_open_options::min_extent_size_hint_alignment);
17781777
1779-                     //  Ignore error; may be !xfs, and just a hint anyway
1780-                     ::ioctl (fd, XFS_IOC_FSSETXATTR, &attr);
1781-                 }
1782-             }
1783-             r = ::fstat (fd, &st);
1784-             if  (r == -1 ) {
1785-                 return  wrap_syscall (r, st);
1778+                 //  Ignore error; may be !xfs, and just a hint anyway
1779+                 ::ioctl (fd, XFS_IOC_FSSETXATTR, &attr);
17861780            }
1787-             close_fd.cancel ();
1788-             return  wrap_syscall (fd, st);
1789-         });
1790-         {
1791-             sr.throw_fs_exception_if_error (" open failed" 
1792-             shared_ptr<file_impl> impl = co_await  make_file_impl (sr.result , options, open_flags, sr.extra );
1793-             co_return  file (std::move (impl));
17941781        }
1795-     }
1782+         r = ::fstat (fd, &st);
1783+         if  (r == -1 ) {
1784+             return  wrap_syscall (r, st);
1785+         }
1786+         close_fd.cancel ();
1787+         return  wrap_syscall (fd, st);
1788+     });
1789+     sr.throw_fs_exception_if_error (" open failed" 
1790+     shared_ptr<file_impl> impl = co_await  make_file_impl (sr.result , options, open_flags, sr.extra );
1791+     co_return  file (std::move (impl));
17961792}
17971793
17981794future<>
17991795reactor::remove_file (std::string_view pathname_view) noexcept  {
18001796    auto  pathname = sstring (pathname_view);
1801-     {
1802-         syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1803-                 internal::thread_pool_submit_reason::file_operation, [pathname] {
1804-             return  wrap_syscall<int >(::remove (pathname.c_str ()));
1805-         });
1806-         {
1807-             sr.throw_fs_exception_if_error (" remove failed" 
1808-         }
1809-     }
1797+     syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1798+             internal::thread_pool_submit_reason::file_operation, [pathname] {
1799+         return  wrap_syscall<int >(::remove (pathname.c_str ()));
1800+     });
1801+     sr.throw_fs_exception_if_error (" remove failed" 
18101802}
18111803
18121804future<>
18131805reactor::rename_file (std::string_view old_pathname_view, std::string_view new_pathname_view) noexcept  {
18141806    auto  old_pathname = sstring (old_pathname_view);
18151807    auto  new_pathname = sstring (new_pathname_view);
1816-     {
1817-         syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1818-                 internal::thread_pool_submit_reason::file_operation, [old_pathname, new_pathname] {
1819-             return  wrap_syscall<int >(::rename (old_pathname.c_str (), new_pathname.c_str ()));
1820-         });
1821-         {
1822-             sr.throw_fs_exception_if_error (" rename failed" 
1823-         }
1824-     }
1808+     syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1809+             internal::thread_pool_submit_reason::file_operation, [old_pathname, new_pathname] {
1810+         return  wrap_syscall<int >(::rename (old_pathname.c_str (), new_pathname.c_str ()));
1811+     });
1812+     sr.throw_fs_exception_if_error (" rename failed" 
18251813}
18261814
18271815future<>
18281816reactor::link_file (std::string_view oldpath_view, std::string_view newpath_view) noexcept  {
18291817    auto  oldpath = sstring (oldpath_view);
18301818    auto  newpath = sstring (newpath_view);
1831-     {
1832-         syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1833-                 internal::thread_pool_submit_reason::file_operation, [oldpath, newpath] {
1834-             return  wrap_syscall<int >(::link (oldpath.c_str (), newpath.c_str ()));
1835-         });
1836-         {
1837-             sr.throw_fs_exception_if_error (" link failed" 
1838-         }
1839-     }
1819+     syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1820+             internal::thread_pool_submit_reason::file_operation, [oldpath, newpath] {
1821+         return  wrap_syscall<int >(::link (oldpath.c_str (), newpath.c_str ()));
1822+     });
1823+     sr.throw_fs_exception_if_error (" link failed" 
18401824}
18411825
18421826future<>
18431827reactor::chmod (std::string_view name_view, file_permissions permissions) noexcept  {
18441828    auto  mode = static_cast <mode_t >(permissions);
18451829    auto  name = sstring (name_view);
1846-     {
1847-         syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1848-                 internal::thread_pool_submit_reason::file_operation, [name = sstring (name), mode] {
1849-             return  wrap_syscall<int >(::chmod (name.c_str (), mode));
1850-         });
1851-         {
1852-             if  (sr.result  == -1 ) {
1853-                 auto  reason = format (" chmod(0{:o}) failed" 
1854-                 sr.throw_fs_exception (reason, fs::path (name));
1855-             }
1856-         }
1830+     syscall_result<int > sr = co_await  _thread_pool->submit <syscall_result<int >>(
1831+             internal::thread_pool_submit_reason::file_operation, [name = sstring (name), mode] {
1832+         return  wrap_syscall<int >(::chmod (name.c_str (), mode));
1833+     });
1834+     if  (sr.result  == -1 ) {
1835+         auto  reason = format (" chmod(0{:o}) failed" 
1836+         sr.throw_fs_exception (reason, fs::path (name));
18571837    }
18581838}
18591839
@@ -1885,24 +1865,20 @@ directory_entry_type stat_to_entry_type(mode_t type) {
18851865future<std::optional<directory_entry_type>>
18861866reactor::file_type (std::string_view name_view, follow_symlink follow) noexcept  {
18871867    auto  name = sstring (name_view);
1888-     {
1889-         syscall_result_extra<struct  stat > sr = co_await  _thread_pool->submit <syscall_result_extra<struct  stat >>(
1890-                 internal::thread_pool_submit_reason::file_operation, [name, follow] {
1891-             struct  stat  st;
1892-             auto  stat_syscall = follow ? stat : lstat;
1893-             auto  ret = stat_syscall (name.c_str (), &st);
1894-             return  wrap_syscall (ret, st);
1895-         });
1896-         {
1897-             if  (long (sr.result ) == -1 ) {
1898-                 if  (sr.error  != ENOENT && sr.error  != ENOTDIR) {
1899-                     sr.throw_fs_exception_if_error (" stat failed" 
1900-                 }
1901-                 co_return  std::optional<directory_entry_type>();
1902-             }
1903-             co_return  std::optional<directory_entry_type>(stat_to_entry_type (sr.extra .st_mode ));
1868+     syscall_result_extra<struct  stat > sr = co_await  _thread_pool->submit <syscall_result_extra<struct  stat >>(
1869+             internal::thread_pool_submit_reason::file_operation, [name, follow] {
1870+         struct  stat  st;
1871+         auto  stat_syscall = follow ? stat : lstat;
1872+         auto  ret = stat_syscall (name.c_str (), &st);
1873+         return  wrap_syscall (ret, st);
1874+     });
1875+     if  (long (sr.result ) == -1 ) {
1876+         if  (sr.error  != ENOENT && sr.error  != ENOTDIR) {
1877+             sr.throw_fs_exception_if_error (" stat failed" 
19041878        }
1879+         co_return  std::optional<directory_entry_type>();
19051880    }
1881+     co_return  std::optional<directory_entry_type>(stat_to_entry_type (sr.extra .st_mode ));
19061882}
19071883
19081884future<std::optional<directory_entry_type>>
@@ -1923,42 +1899,32 @@ future<size_t> reactor::read_directory(int fd, char* buffer, size_t buffer_size)
19231899        auto  ret = ::syscall (__NR_getdents64, fd, reinterpret_cast <linux_dirent64*>(buffer), buffer_size);
19241900        return  wrap_syscall (ret);
19251901    });
1926-     {
1927-         ret.throw_if_error ();
1928-         co_return  ret.result ;
1929-     }
1902+     ret.throw_if_error ();
1903+     co_return  ret.result ;
19301904}
19311905
19321906future<int >
19331907reactor::inotify_add_watch (int  fd, std::string_view path_view, uint32_t  flags) {
19341908    auto  path = sstring (path_view);
1935-     {
1936-         syscall_result<int > ret = co_await  _thread_pool->submit <syscall_result<int >>(
1937-                 internal::thread_pool_submit_reason::file_operation, [fd, path, flags] {
1938-             auto  ret = ::inotify_add_watch (fd, path.c_str (), flags);
1939-             return  wrap_syscall (ret);
1940-         });
1941-         {
1942-             ret.throw_if_error ();
1943-             co_return  ret.result ;
1944-         }
1945-     }
1909+     syscall_result<int > ret = co_await  _thread_pool->submit <syscall_result<int >>(
1910+             internal::thread_pool_submit_reason::file_operation, [fd, path, flags] {
1911+         auto  ret = ::inotify_add_watch (fd, path.c_str (), flags);
1912+         return  wrap_syscall (ret);
1913+     });
1914+     ret.throw_if_error ();
1915+     co_return  ret.result ;
19461916}
19471917
19481918future<std::tuple<file_desc, file_desc>>
19491919reactor::make_pipe () {
19501920    auto  pipe = std::array<int , 2 >{};
1951-     {
1952-         syscall_result<int > ret = co_await  _thread_pool->submit <syscall_result<int >>(
1953-                 internal::thread_pool_submit_reason::file_operation, [&pipe] {
1954-             return  wrap_syscall<int >(::pipe2 (pipe.data (), O_NONBLOCK));
1955-         });
1956-         {
1957-             ret.throw_if_error ();
1958-             co_return  std::tuple<file_desc, file_desc>(file_desc::from_fd (pipe[0 ]),
1959-                                                                        file_desc::from_fd (pipe[1 ]));
1960-         }
1961-     }
1921+     syscall_result<int > ret = co_await  _thread_pool->submit <syscall_result<int >>(
1922+             internal::thread_pool_submit_reason::file_operation, [&pipe] {
1923+         return  wrap_syscall<int >(::pipe2 (pipe.data (), O_NONBLOCK));
1924+     });
1925+     ret.throw_if_error ();
1926+     co_return  std::tuple<file_desc, file_desc>(file_desc::from_fd (pipe[0 ]),
1927+                                                                 file_desc::from_fd (pipe[1 ]));
19621928}
19631929
19641930future<std::tuple<pid_t , file_desc, file_desc, file_desc>>
0 commit comments