Commit Graph

1039 Commits

Author SHA1 Message Date
Brian Gianforcaro
fa517b213a Kernel: Handle string format errors in FileSystem APIs :^) 2021-11-30 11:16:35 +01:00
Andreas Kling
e08d213830 Kernel: Use DistinctNumeric for filesystem ID's
This patch adds the FileSystemID type, which is a distinct u32.
This prevents accidental conversion from arbitrary integers.
2021-11-18 21:11:30 +01:00
Andreas Kling
b285323d91 Kernel: Propagate Vector append failures from Inode::apply_flock() 2021-11-18 21:11:30 +01:00
Andreas Kling
af6358e1e1 Kernel: Allow mmap() with PROT_WRITE+MAP_SHARED
Now that we have a way to flush changes back to disk, let's allow this
type of mapping.
2021-11-17 19:35:13 +01:00
Andreas Kling
216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Daniel Bertalan
304c03f457 Kernel: Reject writable shared file mappings
We have no way of writing changes to memory-mapped files back to disk,
and software relying on this functionality for output would fail
miserably. Let's just return ENOTSUP instead to allow callers to fall
back to standard file IO instead of silently discarding writes.

This makes the LLD port work, which uses memory-mapped files to write
its output by default.
2021-11-17 00:15:30 +01:00
Andrew Kaster
7243bcb7da Kernel: Use static_ptr_cast to convert between Userspace<T*> types
Some calls of copy_to_user were converting Userspace<T*> to
Userspace<U*> via the implicit conversion to FlatPtr. Change them to use
the static_ptr_cast overload that is designed to express this conversion
2021-11-16 00:13:22 +01:00
Andrew Kaster
65edc62c02 Kernel: Resolve clang-tidy readability-make-member-function-const
... In files included from Kernel/Thread.cpp or Kernel/Process.cpp

Some places the warning is suppressed, because we do not want a const
object do have non-const access to the returned sub-object.
2021-11-14 22:52:35 +01:00
Andrew Kaster
a92132e44a Kernel: Resolve clang-tidy readability-implicit-bool-conversion warnings
... In files included from Kernel/Process.cpp and Kernel/Thread.cpp
2021-11-14 22:52:35 +01:00
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Andreas Kling
ad5d217e76 Kernel/Ext2FS: Propagate HashMap errors instead of panicking 2021-11-11 01:27:46 +01:00
Andreas Kling
f86ee71f65 Kernel/Ext2FS: Propagate errors from block list computation functions 2021-11-10 21:58:58 +01:00
Andreas Kling
4661ca5f15 Kernel: Propagate Vector append errors in two places in Ext2FS
There are a bunch more of these, just taking care of some simple ones.
2021-11-10 21:58:58 +01:00
Andreas Kling
5ce753b74d Kernel: Make Inode::traverse_as_directory() callback return ErrorOr
This allows us to propagate errors from inside the callback with TRY().
2021-11-10 21:58:58 +01:00
Andreas Kling
88b6428c25 AK: Make Vector::try_* functions return ErrorOr<void>
Instead of signalling allocation failure with a bool return value
(false), we now use ErrorOr<void> and return ENOMEM as appropriate.
This allows us to use TRY() and MUST() with Vector. :^)
2021-11-10 21:58:58 +01:00
Ben Wiederhake
bf7a2ff941 AK+Kernel: Make BitmapView read-only 2021-11-10 14:39:42 +01:00
Andreas Kling
79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00
Liav A
066b1183cd Kernel: Return ENOTIMPL when trying to read from SysFS inodes
Instead of asserting, just return reasonable error back to userland.
2021-11-05 16:26:45 +02:00
Ben Wiederhake
e3b7c305bc Kernel: Don't crash on writes to ProcFS 2021-10-31 18:44:12 +01:00
Ben Wiederhake
9a4e125468 Kernel: Remove misleading FIXME in DevTmpFS
This FIXME does not seem to apply anymore. Yes, symbolic links in all
filesystems appear to be slightly broken, but that has nothing to do
with File::absolute_path. Let's remove the wrong FIXME instead of adding
to the confusion.
2021-10-31 12:06:28 +01:00
Ben Wiederhake
c05c5a7ff4 Kernel: Clarify ambiguous {File,Description}::absolute_path
Found due to smelly code in InodeFile::absolute_path.

In particular, this replaces the following misleading methods:

File::absolute_path
This method *never* returns an actual path, and if called on an
InodeFile (which is impossible), it would VERIFY_NOT_REACHED().

OpenFileDescription::try_serialize_absolute_path
OpenFileDescription::absolute_path
These methods do not guarantee to return an actual path (just like the
other method), and just like Custody::absolute_path they do not
guarantee accuracy. In particular, just renaming the method made a
TOCTOU bug obvious.

The new method signatures use KResultOr, just like
try_serialize_absolute_path() already did.
2021-10-31 12:06:28 +01:00
Ben Wiederhake
88ca12f037 Kernel: Enable early-returns from VFS::for_each_mount 2021-10-31 12:06:28 +01:00
Andreas Kling
21bfa02dd2 Kernel: Remove unused InodeIdentifier::to_string() 2021-10-22 01:19:04 +02:00
Andreas Kling
fe98cb2c4b Kernel: Make Ext2FS::free_inode() return KResult, and use TRY() more
While there's no clear propagation path for errors that happen in an
inode destructor, using TRY() still makes the code a lot nicer.
2021-10-21 23:23:23 +02:00
Andreas Kling
98b865fe10 Kernel: Make Ext2FS::write_ext2_inode() return KResult
This allows us to use TRY() in more places.
2021-10-21 23:23:23 +02:00
Andreas Kling
6f69d5204f Kernel: Make Inode::flush_metadata() return a KResult
Even if this goes nowhere yet, we have to start building an error
propagation path somewhere.
2021-10-21 23:23:23 +02:00
Daniel Bertalan
018c4e0e7e AK+Kernel: Format DistinctNumeric using the underlying type's formatter
Forcing the formatting to go through `Formatter<FormatString>` is
completely unnecessary, increases code size, performs a String
allocation and prevents us from using the formatting options available
on that type.

This commit also removes explicit formatters from
`BlockBasedFileSystem::BlockIndex` and `Kernel::InodeIndex`, as those
are already covered by the blanket implementation for all
`DistinctNumeric` types.
2021-10-21 22:19:50 +02:00
SeekingBlues
3d174e3ad2 Kernel/ProcFS: Provide a way to write to ProcFS inodes
ProcFSGlobalInode now calls `write_bytes()`, `truncate()` and
`set_mtime()` on its associated component. This allows us to write 0 or
1 to a ProcFSSystemBoolean component to toggle a boolean value.
2021-10-17 14:46:59 +02:00
James Mintram
545ce5b595 Kernel: Add per platform Processor.h headers
The platform independent Processor.h file includes the shared processor
code and includes the specific platform header file.

All references to the Arch/x86/Processor.h file have been replaced with
a reference to Arch/Processor.h.
2021-10-14 01:23:08 +01:00
James Mintram
26d7c25076 Kernel: Add header includes closer to their use 2021-10-14 01:23:08 +01:00
Liav A
f8489da8ee Kernel/SysFS: Provide a way to "truncate" and "set" mtime on inodes
Normally, trying to truncate a SysFSInode should result in EPERM error.
However, as suggested by Ali (@alimpfard), we can allow the PowerState
node to be "truncated" so one can open that file with O_TRUNC option.
Likewise, we also need to provide a way to set modified time on SysFS
inodes. For most inodes, we should return ENOTIMPL error, but for the
power state switch, we ignore the modified time setting and just return
KSuccess.

These fixes allow to do "echo -n 1 > /sys/firmware/power_state" in Shell
after gaining root permissions, to switch the power state.
2021-10-09 12:07:56 +02:00
Brian Gianforcaro
5f1c98e576 Kernel: Use operator ""sv in all class_name() implementations
Previously there was a mix of returning plain strings and returning
explicit string views using `operator ""sv`. This change switches them
all to standardized on `operator ""sv` as it avoids a call to strlen.
2021-10-03 13:36:10 +02:00
Liav A
aee4786d8e Kernel: Introduce the DeviceManagement singleton
This singleton simplifies many aspects that we struggled with before:
1. There's no need to make derived classes of Device expose the
constructor as public anymore. The singleton is a friend of them, so he
can call the constructor. This solves the issue with try_create_device
helper neatly, hopefully for good.
2. Getting a reference of the NullDevice is now being done from this
singleton, which means that NullDevice no longer needs to use its own
singleton, and we can apply the try_create_device helper on it too :)
3. We can now defer registration completely after the Device constructor
which means the Device constructor is merely assigning the major and
minor numbers of the Device, and the try_create_device helper ensures it
calls the after_inserting method immediately after construction. This
creates a great opportunity to make registration more OOM-safe.
2021-09-17 01:02:48 +03:00
Brian Gianforcaro
85d36e56d2 Kernel: Pack Flock struct tighter
Flagged by pvs-studio, ordering the members from largest to smallest
allows us to save a few bytes in the size of the struct.
2021-09-16 17:17:13 +02:00
sin-ack
220b7dd779 Kernel: Weakly hold on to the file in LocalSocket
Because we were holding a strong ref to the OpenFileDescription in
LocalSocket and a strong ref to the LocalSocket in Inode, we were
creating a reference cycle in the event of the socket being cleaned up
after the file description did (i.e. unlinking the file before closing
the socket), because the file description never got destructed.
2021-09-16 16:50:36 +02:00
Liav A
06e95d0fd7 Kernel/SysFS: Make it possible to have custom permissions for nodes 2021-09-12 11:52:16 +02:00
TheFightingCatfish
a81b21c1a7 Kernel+LibC: Implement fsync 2021-09-12 11:24:02 +02:00
Liav A
f5de4f24b2 Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the
constructor, so we can safely pass a reference of a Device, so the
SysFSDeviceComponent constructor can use that object to identify whether
it's a block device or a character device.
This allows to us to not hold a device in SysFSDeviceComponent with a
RefPtr.
Also, we also call the before_removing method in both SlavePTY::unref
and File::unref, so because Device has that method being overrided, it
can ensure the device is removed always cleanly.
2021-09-11 11:41:14 +02:00
Ali Mohammad Pur
5a0cdb15b0 AK+Everywhere: Reduce the number of template parameters of IntrusiveList
This makes the user-facing type only take the node member pointer, and
lets the compiler figure out the other needed types from that.
2021-09-10 18:05:46 +03:00
Ralf Donau
ec03f29fd1 Kernel/VFS: Restrict special unveil rule for Loader.so 2021-09-09 11:32:16 +02:00
Andreas Kling
524ef5e475 Kernel: Add KBuffer::bytes() and use it
(Instead of hand-wrapping { data(), size() } in a bunch of places.)
2021-09-08 20:16:00 +02:00
Liav A
74c4c864bd Kernel+SystemServer: Simplify the DevTmpFS design
We are no longer have a separate Inode object class for the pts
directory. With a small exception to this, all chmod and chown code
is now at one place.
It's now possible to create any name of a sub-directory in the
filesystem.
2021-09-08 00:42:20 +02:00
Liav A
3d5ddbab74 Kernel: Rename DevFS => DevTmpFS
The current implementation of DevFS resembles the linux devtmpfs, and
not the traditional DevFS, so let's rename it to better represent the
direction of the development in regard to this filesystem.

The abbreviation for DevTmpFS is still "dev", because it doesn't add
value as a commandline option to make it longer.

In quick summary - DevFS in unix OSes is simply a static filesystem, so
device nodes are generated and removed by the kernel code. DevTmpFS
is a "modern reinvention" of the DevFS, so it is much more like a TmpFS
in the sense that not only it's stored entirely in RAM, but the userland
is responsible to add and remove devices nodes as it sees fit, and no
kernel code is directly being involved to keep the filesystem in sync.
2021-09-08 00:42:20 +02:00
Liav A
750ca2190b Kernel/DevFS: Add the ability to remove device nodes
In order to make this kind of operation simpler, we no longer use a
Vector to store pointers to DevFSDeviceInode, but an IntrusiveList is
used instead. Also, we only allow to remove device nodes for now, but
in theory we can allow to remove all kinds of files from the DevFS.
2021-09-08 00:42:20 +02:00
Liav A
6a9c717a30 Kernel: Expose device presence in /sys/dev/char and /sys/dev/block
These files are not marked as block devices or character devices so they
are not meant to be used as device nodes. The filenames are formatted to
the pattern "major:minor", but a Userland program need to call the parse
these format and inspect the the major and minor numbers and create the
real device nodes in /dev.

Later on, it might be a good idea to ensure we don't create new
SysFSComponents on the heap for each Device, but rather generate
them only when required (and preferably to not create a SysFSComponent
at all if possible).
2021-09-08 00:42:20 +02:00
Liav A
4f04cb98c1 Kernel/VFS: Silence mknod debug spam
Since we populate the DevFS now in userspace, this creates a bunch of
unnecessary noise in the kernel log.
2021-09-08 00:42:20 +02:00
Liav A
e0d712c6f7 Kernel+SystemServer: Defer creation of device nodes to userspace
Don't create these device nodes in the Kernel, so we essentially enforce
userspace (SystemServer) to take control of this operation and to decide
how to create these device nodes.

This makes the DevFS to resemble linux devtmpfs, and allows us to remove
a bunch of unneeded overriding implementations of device name creation
in the Kernel.
2021-09-08 00:42:20 +02:00
Liav A
fcc046047f Kernel/DevFS: Simplify nodes insertion and lookup
Use IntrusiveList instead of a Vector to add inodes to a directory.
2021-09-08 00:42:20 +02:00
Andreas Kling
049d846eb9 Kernel/TmpFS: Stop leaking directory entry metadata
When creating and removing a child to a TmpFS directory, we were
forgetting to delete the TmpFSInode::Child struct.
2021-09-07 22:16:25 +02:00
Andreas Kling
ec4b814c9a Kernel: Use OOM-safe absolute path serialization in InodeFile::mmap()
Switch from OpenFileDescription::absolute_path() to the OOM-safe
try_serialize_absolute_path() (and propagate any errors to the caller.)
2021-09-07 22:16:25 +02:00