Kernel: Start using StringView in the VFS class.

The less kmalloc() we can do, the better. Calling kmalloc() disables all
interrupts while it runs, so it's directly affecting responsiveness.
This commit is contained in:
Andreas Kling 2019-04-15 14:57:27 +02:00
parent 461aa550eb
commit 13041f894f
Notes: sideshowbarker 2024-07-19 14:42:04 +09:00
3 changed files with 53 additions and 52 deletions

View File

@ -35,7 +35,7 @@ InodeIdentifier VFS::root_inode_id() const
return m_root_inode->identifier();
}
bool VFS::mount(RetainPtr<FS>&& file_system, const String& path)
bool VFS::mount(RetainPtr<FS>&& file_system, StringView path)
{
ASSERT(file_system);
int error;
@ -127,7 +127,7 @@ KResultOr<Retained<FileDescriptor>> VFS::open(RetainPtr<Device>&& device, int op
return FileDescriptor::create(move(device));
}
KResult VFS::utime(const String& path, Inode& base, time_t atime, time_t mtime)
KResult VFS::utime(StringView path, Inode& base, time_t atime, time_t mtime)
{
auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
if (descriptor_or_error.is_error())
@ -147,7 +147,7 @@ KResult VFS::utime(const String& path, Inode& base, time_t atime, time_t mtime)
return KSuccess;
}
KResult VFS::stat(const String& path, int options, Inode& base, struct stat& statbuf)
KResult VFS::stat(StringView path, int options, Inode& base, struct stat& statbuf)
{
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
if (inode_or_error.is_error())
@ -155,7 +155,7 @@ KResult VFS::stat(const String& path, int options, Inode& base, struct stat& sta
return FileDescriptor::create(inode_or_error.value().ptr())->fstat(statbuf);
}
KResultOr<Retained<FileDescriptor>> VFS::open(const String& path, int options, mode_t mode, Inode& base)
KResultOr<Retained<FileDescriptor>> VFS::open(StringView path, int options, mode_t mode, Inode& base)
{
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
if (options & O_CREAT) {
@ -202,7 +202,7 @@ KResultOr<Retained<FileDescriptor>> VFS::open(const String& path, int options, m
return FileDescriptor::create(*inode);
}
KResultOr<Retained<FileDescriptor>> VFS::create(const String& path, int options, mode_t mode, Inode& base)
KResultOr<Retained<FileDescriptor>> VFS::create(StringView path, int options, mode_t mode, Inode& base)
{
(void)options;
@ -232,7 +232,7 @@ KResultOr<Retained<FileDescriptor>> VFS::create(const String& path, int options,
return FileDescriptor::create(move(new_file));
}
KResult VFS::mkdir(const String& path, mode_t mode, Inode& base)
KResult VFS::mkdir(StringView path, mode_t mode, Inode& base)
{
RetainPtr<Inode> parent_inode;
auto result = resolve_path_to_inode(path, base, &parent_inode);
@ -255,7 +255,7 @@ KResult VFS::mkdir(const String& path, mode_t mode, Inode& base)
return KResult(error);
}
KResult VFS::access(const String& path, int mode, Inode& base)
KResult VFS::access(StringView path, int mode, Inode& base)
{
auto inode_or_error = resolve_path_to_inode(path, base);
if (inode_or_error.is_error())
@ -277,7 +277,7 @@ KResult VFS::access(const String& path, int mode, Inode& base)
return KSuccess;
}
KResultOr<Retained<Inode>> VFS::open_directory(const String& path, Inode& base)
KResultOr<Retained<Inode>> VFS::open_directory(StringView path, Inode& base)
{
auto inode_or_error = resolve_path_to_inode(path, base);
if (inode_or_error.is_error())
@ -303,7 +303,7 @@ KResult VFS::chmod(Inode& inode, mode_t mode)
return inode.chmod(mode);
}
KResult VFS::chmod(const String& path, mode_t mode, Inode& base)
KResult VFS::chmod(StringView path, mode_t mode, Inode& base)
{
auto inode_or_error = resolve_path_to_inode(path, base);
if (inode_or_error.is_error())
@ -312,7 +312,7 @@ KResult VFS::chmod(const String& path, mode_t mode, Inode& base)
return chmod(*inode, mode);
}
KResult VFS::rename(const String& old_path, const String& new_path, Inode& base)
KResult VFS::rename(StringView old_path, StringView new_path, Inode& base)
{
RetainPtr<Inode> old_parent_inode;
auto old_inode_or_error = resolve_path_to_inode(old_path, base, &old_parent_inode);
@ -356,7 +356,7 @@ KResult VFS::rename(const String& old_path, const String& new_path, Inode& base)
return KSuccess;
}
KResult VFS::chown(const String& path, uid_t a_uid, gid_t a_gid, Inode& base)
KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Inode& base)
{
auto inode_or_error = resolve_path_to_inode(path, base);
if (inode_or_error.is_error())
@ -387,7 +387,7 @@ KResult VFS::chown(const String& path, uid_t a_uid, gid_t a_gid, Inode& base)
return inode->chown(new_uid, new_gid);
}
KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
{
// FIXME: This won't work nicely across mount boundaries.
FileSystemPath p(path);
@ -402,7 +402,7 @@ KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode&
return Retained<Inode>(*get_inode(result.value()));
}
KResult VFS::link(const String& old_path, const String& new_path, Inode& base)
KResult VFS::link(StringView old_path, StringView new_path, Inode& base)
{
auto old_inode_or_error = resolve_path_to_inode(old_path, base);
if (old_inode_or_error.is_error())
@ -429,7 +429,7 @@ KResult VFS::link(const String& old_path, const String& new_path, Inode& base)
return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0);
}
KResult VFS::unlink(const String& path, Inode& base)
KResult VFS::unlink(StringView path, Inode& base)
{
RetainPtr<Inode> parent_inode;
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
@ -446,7 +446,7 @@ KResult VFS::unlink(const String& path, Inode& base)
return parent_inode->remove_child(FileSystemPath(path).basename());
}
KResult VFS::symlink(const String& target, const String& linkpath, Inode& base)
KResult VFS::symlink(StringView target, StringView linkpath, Inode& base)
{
RetainPtr<Inode> parent_inode;
auto existing_file_or_error = resolve_path_to_inode(linkpath, base, &parent_inode);
@ -471,7 +471,7 @@ KResult VFS::symlink(const String& target, const String& linkpath, Inode& base)
return KSuccess;
}
KResult VFS::rmdir(const String& path, Inode& base)
KResult VFS::rmdir(StringView path, Inode& base)
{
RetainPtr<Inode> parent_inode;
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
@ -511,7 +511,7 @@ KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inod
auto symlink_contents = symlink_inode.read_entire();
if (!symlink_contents)
return KResult(-ENOENT);
auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
auto linkee = StringView(symlink_contents.pointer(), symlink_contents.size());
#ifdef VFS_DEBUG
kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
#endif
@ -572,12 +572,13 @@ KResultOr<String> VFS::absolute_path(Inode& core_inode)
return builder.to_string();
}
KResultOr<InodeIdentifier> VFS::resolve_path(const String& path, InodeIdentifier base, int options, InodeIdentifier* parent_id)
KResultOr<InodeIdentifier> VFS::resolve_path(StringView path, InodeIdentifier base, int options, InodeIdentifier* parent_id)
{
if (path.is_empty())
return KResult(-EINVAL);
auto parts = path.split('/');
// FIXME: Use StringView::split() once it exists.
auto parts = String(path).split('/');
InodeIdentifier crumb_id;
if (path[0] == '/')
@ -661,7 +662,7 @@ KResultOr<InodeIdentifier> VFS::resolve_path(const String& path, InodeIdentifier
return crumb_id;
}
InodeIdentifier VFS::old_resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
InodeIdentifier VFS::old_resolve_path(StringView path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
{
auto result = resolve_path(path, base, options, parent_id);
if (result.is_error()) {

View File

@ -59,24 +59,24 @@ public:
~VFS();
bool mount_root(RetainPtr<FS>&&);
bool mount(RetainPtr<FS>&&, const String& path);
bool mount(RetainPtr<FS>&&, StringView path);
KResultOr<Retained<FileDescriptor>> open(RetainPtr<Device>&&, int options);
KResultOr<Retained<FileDescriptor>> open(const String& path, int options, mode_t mode, Inode& base);
KResultOr<Retained<FileDescriptor>> create(const String& path, int options, mode_t mode, Inode& base);
KResult mkdir(const String& path, mode_t mode, Inode& base);
KResult link(const String& old_path, const String& new_path, Inode& base);
KResult unlink(const String& path, Inode& base);
KResult symlink(const String& target, const String& linkpath, Inode& base);
KResult rmdir(const String& path, Inode& base);
KResult chmod(const String& path, mode_t, Inode& base);
KResultOr<Retained<FileDescriptor>> open(StringView path, int options, mode_t mode, Inode& base);
KResultOr<Retained<FileDescriptor>> create(StringView path, int options, mode_t mode, Inode& base);
KResult mkdir(StringView path, mode_t mode, Inode& base);
KResult link(StringView old_path, StringView new_path, Inode& base);
KResult unlink(StringView path, Inode& base);
KResult symlink(StringView target, StringView linkpath, Inode& base);
KResult rmdir(StringView path, Inode& base);
KResult chmod(StringView path, mode_t, Inode& base);
KResult chmod(Inode&, mode_t);
KResult chown(const String& path, uid_t, gid_t, Inode& base);
KResult access(const String& path, int mode, Inode& base);
KResult stat(const String& path, int options, Inode& base, struct stat&);
KResult utime(const String& path, Inode& base, time_t atime, time_t mtime);
KResult rename(const String& oldpath, const String& newpath, Inode& base);
KResultOr<Retained<Inode>> open_directory(const String& path, Inode& base);
KResult chown(StringView path, uid_t, gid_t, Inode& base);
KResult access(StringView path, int mode, Inode& base);
KResult stat(StringView path, int options, Inode& base, struct stat&);
KResult utime(StringView path, Inode& base, time_t atime, time_t mtime);
KResult rename(StringView oldpath, StringView newpath, Inode& base);
KResultOr<Retained<Inode>> open_directory(StringView path, Inode& base);
void register_device(Device&);
void unregister_device(Device&);
@ -103,9 +103,9 @@ private:
bool is_vfs_root(InodeIdentifier) const;
void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
InodeIdentifier old_resolve_path(const String& path, InodeIdentifier base, int& error, int options = 0, InodeIdentifier* parent_id = nullptr);
KResultOr<InodeIdentifier> resolve_path(const String& path, InodeIdentifier base, int options = 0, InodeIdentifier* parent_id = nullptr);
KResultOr<Retained<Inode>> resolve_path_to_inode(const String& path, Inode& base, RetainPtr<Inode>* parent_id = nullptr, int options = 0);
InodeIdentifier old_resolve_path(StringView path, InodeIdentifier base, int& error, int options = 0, InodeIdentifier* parent_id = nullptr);
KResultOr<InodeIdentifier> resolve_path(StringView path, InodeIdentifier base, int options = 0, InodeIdentifier* parent_id = nullptr);
KResultOr<Retained<Inode>> resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_id = nullptr, int options = 0);
KResultOr<InodeIdentifier> resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode);
Mount* find_mount_for_host(InodeIdentifier);

View File

@ -287,7 +287,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
if (parts.is_empty())
return -ENOENT;
auto result = VFS::the().open(path, 0, 0, cwd_inode());
auto result = VFS::the().open(path.view(), 0, 0, cwd_inode());
if (result.is_error())
return result.error();
auto descriptor = result.value();
@ -905,14 +905,14 @@ int Process::sys$utime(const char* pathname, const utimbuf* buf)
mtime = now.tv_sec;
atime = now.tv_sec;
}
return VFS::the().utime(String(pathname), cwd_inode(), atime, mtime);
return VFS::the().utime(StringView(pathname), cwd_inode(), atime, mtime);
}
int Process::sys$access(const char* pathname, int mode)
{
if (!validate_read_str(pathname))
return -EFAULT;
return VFS::the().access(String(pathname), mode, cwd_inode());
return VFS::the().access(StringView(pathname), mode, cwd_inode());
}
int Process::sys$fcntl(int fd, int cmd, dword arg)
@ -967,14 +967,14 @@ int Process::sys$lstat(const char* path, stat* statbuf)
{
if (!validate_write_typed(statbuf))
return -EFAULT;
return VFS::the().stat(String(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf);
return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf);
}
int Process::sys$stat(const char* path, stat* statbuf)
{
if (!validate_write_typed(statbuf))
return -EFAULT;
return VFS::the().stat(String(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf);
return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf);
}
int Process::sys$readlink(const char* path, char* buffer, ssize_t size)
@ -1008,7 +1008,7 @@ int Process::sys$chdir(const char* path)
{
if (!validate_read_str(path))
return -EFAULT;
auto directory_or_error = VFS::the().open_directory(String(path), cwd_inode());
auto directory_or_error = VFS::the().open_directory(StringView(path), cwd_inode());
if (directory_or_error.is_error())
return directory_or_error.error();
m_cwd = *directory_or_error.value();
@ -1648,7 +1648,7 @@ int Process::sys$mkdir(const char* pathname, mode_t mode)
return -EINVAL;
if (pathname_length >= 255)
return -ENAMETOOLONG;
return VFS::the().mkdir(String(pathname, pathname_length), mode & ~umask(), cwd_inode());
return VFS::the().mkdir(StringView(pathname, pathname_length), mode & ~umask(), cwd_inode());
}
clock_t Process::sys$times(tms* times)
@ -1816,14 +1816,14 @@ int Process::sys$link(const char* old_path, const char* new_path)
return -EFAULT;
if (!validate_read_str(new_path))
return -EFAULT;
return VFS::the().link(String(old_path), String(new_path), cwd_inode());
return VFS::the().link(StringView(old_path), StringView(new_path), cwd_inode());
}
int Process::sys$unlink(const char* pathname)
{
if (!validate_read_str(pathname))
return -EFAULT;
return VFS::the().unlink(String(pathname), cwd_inode());
return VFS::the().unlink(StringView(pathname), cwd_inode());
}
int Process::sys$symlink(const char* target, const char* linkpath)
@ -1832,14 +1832,14 @@ int Process::sys$symlink(const char* target, const char* linkpath)
return -EFAULT;
if (!validate_read_str(linkpath))
return -EFAULT;
return VFS::the().symlink(String(target), String(linkpath), cwd_inode());
return VFS::the().symlink(StringView(target), StringView(linkpath), cwd_inode());
}
int Process::sys$rmdir(const char* pathname)
{
if (!validate_read_str(pathname))
return -EFAULT;
return VFS::the().rmdir(String(pathname), cwd_inode());
return VFS::the().rmdir(StringView(pathname), cwd_inode());
}
int Process::sys$read_tsc(dword* lsw, dword* msw)
@ -1856,7 +1856,7 @@ int Process::sys$chmod(const char* pathname, mode_t mode)
{
if (!validate_read_str(pathname))
return -EFAULT;
return VFS::the().chmod(String(pathname), mode, cwd_inode());
return VFS::the().chmod(StringView(pathname), mode, cwd_inode());
}
int Process::sys$fchmod(int fd, mode_t mode)
@ -1871,7 +1871,7 @@ int Process::sys$chown(const char* pathname, uid_t uid, gid_t gid)
{
if (!validate_read_str(pathname))
return -EFAULT;
return VFS::the().chown(String(pathname), uid, gid, cwd_inode());
return VFS::the().chown(StringView(pathname), uid, gid, cwd_inode());
}
void Process::finalize()
@ -2452,7 +2452,7 @@ int Process::sys$rename(const char* oldpath, const char* newpath)
return -EFAULT;
if (!validate_read_str(newpath))
return -EFAULT;
return VFS::the().rename(String(oldpath), String(newpath), cwd_inode());
return VFS::the().rename(StringView(oldpath), StringView(newpath), cwd_inode());
}
int Process::sys$shm_open(const char* name, int flags, mode_t mode)