2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 17:26:07 +02:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2021-12-11 16:43:03 +01:00
|
|
|
#include <AK/StringView.h>
|
2019-04-28 14:53:50 +02:00
|
|
|
#include <AK/Types.h>
|
2020-02-16 01:50:16 +01:00
|
|
|
#include <Kernel/Forward.h>
|
2022-08-19 20:53:40 +02:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
|
|
|
#include <Kernel/Library/NonnullLockRefPtr.h>
|
2019-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2020-09-11 21:11:07 -06:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/VirtualAddress.h>
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
class File;
|
|
|
|
|
2021-08-22 15:59:47 +02:00
|
|
|
class FileBlockerSet final : public Thread::BlockerSet {
|
2020-11-29 16:05:27 -07:00
|
|
|
public:
|
2021-08-22 15:59:47 +02:00
|
|
|
FileBlockerSet() { }
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
virtual bool should_add_blocker(Thread::Blocker& b, void* data) override
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 16:05:27 -07:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
2021-09-05 00:12:49 +02:00
|
|
|
return !blocker.unblock_if_conditions_are_met(true, data);
|
2020-11-29 16:05:27 -07:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:38:16 +02:00
|
|
|
void unblock_all_blockers_whose_conditions_are_met()
|
2020-11-29 16:05:27 -07:00
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-08-22 17:38:16 +02:00
|
|
|
BlockerSet::unblock_all_blockers_whose_conditions_are_met_locked([&](auto& b, void* data, bool&) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 16:05:27 -07:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
2021-09-05 00:12:49 +02:00
|
|
|
return blocker.unblock_if_conditions_are_met(false, data);
|
2020-11-29 16:05:27 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
// File is the base class for anything that can be referenced by a OpenFileDescription.
|
2019-06-02 09:23:37 +02:00
|
|
|
//
|
|
|
|
// The most important functions in File are:
|
|
|
|
//
|
|
|
|
// read() and write()
|
|
|
|
// - Implement reading and writing.
|
|
|
|
// - Return the number of bytes read/written, OR a negative error code.
|
|
|
|
//
|
|
|
|
// can_read() and can_write()
|
|
|
|
//
|
|
|
|
// - Used to implement blocking I/O, and the select() and poll() syscalls.
|
|
|
|
// - Return true if read() or write() would succeed, respectively.
|
|
|
|
// - Note that can_read() should return true in EOF conditions,
|
|
|
|
// and a subsequent call to read() should return 0.
|
|
|
|
//
|
|
|
|
// ioctl()
|
|
|
|
//
|
|
|
|
// - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
|
|
|
|
// - Can be overridden in subclasses to implement arbitrary functionality.
|
|
|
|
// - Subclasses should take care to validate incoming addresses before dereferencing.
|
|
|
|
//
|
2022-08-23 18:51:18 +02:00
|
|
|
// vmobject_for_mmap()
|
2019-06-02 09:23:37 +02:00
|
|
|
//
|
|
|
|
// - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
|
|
|
|
// - Called by mmap() when userspace wants to memory-map this File somewhere.
|
2022-08-23 18:51:18 +02:00
|
|
|
// - Should return a VMObject suitable for mapping into the calling process.
|
2019-06-02 09:23:37 +02:00
|
|
|
|
2020-09-06 18:46:46 +02:00
|
|
|
class File
|
2022-08-19 17:26:07 +02:00
|
|
|
: public AtomicRefCounted<File>
|
2022-08-19 20:53:40 +02:00
|
|
|
, public LockWeakable<File> {
|
2019-04-28 14:53:50 +02:00
|
|
|
public:
|
2022-08-19 17:26:07 +02:00
|
|
|
virtual bool unref() const { return AtomicRefCounted<File>::unref(); }
|
2021-12-29 01:00:29 +02:00
|
|
|
virtual void will_be_destroyed() { }
|
2019-04-28 14:53:50 +02:00
|
|
|
virtual ~File();
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
virtual ErrorOr<NonnullLockRefPtr<OpenFileDescription>> open(int options);
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> close();
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const = 0;
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> attach(OpenFileDescription&);
|
2021-09-07 13:39:11 +02:00
|
|
|
virtual void detach(OpenFileDescription&);
|
|
|
|
virtual void did_seek(OpenFileDescription&, off_t) { }
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) = 0;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg);
|
2022-08-23 18:51:18 +02:00
|
|
|
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
|
2021-12-17 11:22:27 +01:00
|
|
|
virtual ErrorOr<struct stat> stat() const { return EBADF; }
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2021-10-30 00:45:23 +02:00
|
|
|
// Although this might be better described "name" or "description", these terms already have other meanings.
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> truncate(u64) { return EINVAL; }
|
|
|
|
virtual ErrorOr<void> sync() { return EINVAL; }
|
2022-08-21 16:15:29 +02:00
|
|
|
virtual ErrorOr<void> chown(Credentials const&, OpenFileDescription&, UserID, GroupID) { return EBADF; }
|
|
|
|
virtual ErrorOr<void> chmod(Credentials const&, OpenFileDescription&, mode_t) { return EBADF; }
|
2019-05-30 13:39:17 +02:00
|
|
|
|
2021-07-11 01:46:09 +02:00
|
|
|
virtual StringView class_name() const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
|
|
|
virtual bool is_seekable() const { return false; }
|
|
|
|
|
2019-05-30 13:39:17 +02:00
|
|
|
virtual bool is_inode() const { return false; }
|
2019-04-29 04:55:54 +02:00
|
|
|
virtual bool is_fifo() const { return false; }
|
2019-04-28 14:53:50 +02:00
|
|
|
virtual bool is_device() const { return false; }
|
|
|
|
virtual bool is_tty() const { return false; }
|
|
|
|
virtual bool is_master_pty() const { return false; }
|
|
|
|
virtual bool is_block_device() const { return false; }
|
|
|
|
virtual bool is_character_device() const { return false; }
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual bool is_socket() const { return false; }
|
2021-05-12 19:17:51 +00:00
|
|
|
virtual bool is_inode_watcher() const { return false; }
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2022-11-27 23:50:28 +01:00
|
|
|
virtual bool is_regular_file() const { return false; }
|
|
|
|
|
2021-08-22 15:59:47 +02:00
|
|
|
virtual FileBlockerSet& blocker_set() { return m_blocker_set; }
|
2020-11-29 16:05:27 -07:00
|
|
|
|
2021-04-30 10:33:33 +02:00
|
|
|
size_t attach_count() const { return m_attach_count; }
|
|
|
|
|
2019-04-28 14:53:50 +02:00
|
|
|
protected:
|
|
|
|
File();
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
void evaluate_block_conditions()
|
|
|
|
{
|
2021-11-06 15:06:08 -06:00
|
|
|
if (Processor::current_in_irq() != 0) {
|
2020-11-29 16:05:27 -07:00
|
|
|
// If called from an IRQ handler we need to delay evaluation
|
2020-12-07 21:29:41 -07:00
|
|
|
// and unblocking of waiting threads. Note that this File
|
|
|
|
// instance may be deleted until the deferred call is executed!
|
2022-02-13 21:21:14 +02:00
|
|
|
Processor::deferred_call_queue([self = try_make_weak_ptr().release_value_but_fixme_should_propagate_errors()]() {
|
2020-12-07 21:29:41 -07:00
|
|
|
if (auto file = self.strong_ref())
|
|
|
|
file->do_evaluate_block_conditions();
|
2020-11-29 16:05:27 -07:00
|
|
|
});
|
|
|
|
} else {
|
2020-12-07 21:29:41 -07:00
|
|
|
do_evaluate_block_conditions();
|
2020-11-29 16:05:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-12-07 21:29:41 -07:00
|
|
|
ALWAYS_INLINE void do_evaluate_block_conditions()
|
|
|
|
{
|
2021-08-22 12:21:31 +02:00
|
|
|
VERIFY(!Processor::current_in_irq());
|
2021-08-22 17:38:16 +02:00
|
|
|
blocker_set().unblock_all_blockers_whose_conditions_are_met();
|
2020-12-07 21:29:41 -07:00
|
|
|
}
|
|
|
|
|
2021-08-22 15:59:47 +02:00
|
|
|
FileBlockerSet m_blocker_set;
|
2021-04-30 10:33:33 +02:00
|
|
|
size_t m_attach_count { 0 };
|
2019-04-28 14:53:50 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|