File.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/StringView.h>
  11. #include <AK/Types.h>
  12. #include <AK/Weakable.h>
  13. #include <Kernel/Forward.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/UserOrKernelBuffer.h>
  16. #include <Kernel/VirtualAddress.h>
  17. namespace Kernel {
  18. class File;
  19. class FileBlockerSet final : public Thread::BlockerSet {
  20. public:
  21. FileBlockerSet() { }
  22. virtual bool should_add_blocker(Thread::Blocker& b, void* data) override
  23. {
  24. VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
  25. auto& blocker = static_cast<Thread::FileBlocker&>(b);
  26. return !blocker.unblock_if_conditions_are_met(true, data);
  27. }
  28. void unblock_all_blockers_whose_conditions_are_met()
  29. {
  30. SpinlockLocker lock(m_lock);
  31. BlockerSet::unblock_all_blockers_whose_conditions_are_met_locked([&](auto& b, void* data, bool&) {
  32. VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
  33. auto& blocker = static_cast<Thread::FileBlocker&>(b);
  34. return blocker.unblock_if_conditions_are_met(false, data);
  35. });
  36. }
  37. };
  38. // File is the base class for anything that can be referenced by a OpenFileDescription.
  39. //
  40. // The most important functions in File are:
  41. //
  42. // read() and write()
  43. // - Implement reading and writing.
  44. // - Return the number of bytes read/written, OR a negative error code.
  45. //
  46. // can_read() and can_write()
  47. //
  48. // - Used to implement blocking I/O, and the select() and poll() syscalls.
  49. // - Return true if read() or write() would succeed, respectively.
  50. // - Note that can_read() should return true in EOF conditions,
  51. // and a subsequent call to read() should return 0.
  52. //
  53. // ioctl()
  54. //
  55. // - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
  56. // - Can be overridden in subclasses to implement arbitrary functionality.
  57. // - Subclasses should take care to validate incoming addresses before dereferencing.
  58. //
  59. // mmap()
  60. //
  61. // - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
  62. // - Called by mmap() when userspace wants to memory-map this File somewhere.
  63. // - Should create a Region in the Process and return it if successful.
  64. class File
  65. : public RefCountedBase
  66. , public Weakable<File> {
  67. public:
  68. virtual bool unref() const;
  69. virtual void before_removing() { }
  70. virtual ~File();
  71. virtual ErrorOr<NonnullRefPtr<OpenFileDescription>> open(int options);
  72. virtual ErrorOr<void> close();
  73. virtual bool can_read(const OpenFileDescription&, size_t) const = 0;
  74. virtual bool can_write(const OpenFileDescription&, size_t) const = 0;
  75. virtual ErrorOr<void> attach(OpenFileDescription&);
  76. virtual void detach(OpenFileDescription&);
  77. virtual void did_seek(OpenFileDescription&, off_t) { }
  78. virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
  79. virtual ErrorOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0;
  80. virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg);
  81. virtual ErrorOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared);
  82. virtual ErrorOr<void> stat(::stat&) const { return EBADF; }
  83. // Although this might be better described "name" or "description", these terms already have other meanings.
  84. virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const = 0;
  85. virtual ErrorOr<void> truncate(u64) { return EINVAL; }
  86. virtual ErrorOr<void> sync() { return EINVAL; }
  87. virtual ErrorOr<void> chown(OpenFileDescription&, UserID, GroupID) { return EBADF; }
  88. virtual ErrorOr<void> chmod(OpenFileDescription&, mode_t) { return EBADF; }
  89. virtual StringView class_name() const = 0;
  90. virtual bool is_seekable() const { return false; }
  91. virtual bool is_inode() const { return false; }
  92. virtual bool is_fifo() const { return false; }
  93. virtual bool is_device() const { return false; }
  94. virtual bool is_tty() const { return false; }
  95. virtual bool is_master_pty() const { return false; }
  96. virtual bool is_block_device() const { return false; }
  97. virtual bool is_character_device() const { return false; }
  98. virtual bool is_socket() const { return false; }
  99. virtual bool is_inode_watcher() const { return false; }
  100. virtual FileBlockerSet& blocker_set() { return m_blocker_set; }
  101. size_t attach_count() const { return m_attach_count; }
  102. protected:
  103. File();
  104. void evaluate_block_conditions()
  105. {
  106. if (Processor::current_in_irq() != 0) {
  107. // If called from an IRQ handler we need to delay evaluation
  108. // and unblocking of waiting threads. Note that this File
  109. // instance may be deleted until the deferred call is executed!
  110. Processor::deferred_call_queue([self = make_weak_ptr()]() {
  111. if (auto file = self.strong_ref())
  112. file->do_evaluate_block_conditions();
  113. });
  114. } else {
  115. do_evaluate_block_conditions();
  116. }
  117. }
  118. private:
  119. ALWAYS_INLINE void do_evaluate_block_conditions()
  120. {
  121. VERIFY(!Processor::current_in_irq());
  122. blocker_set().unblock_all_blockers_whose_conditions_are_met();
  123. }
  124. FileBlockerSet m_blocker_set;
  125. size_t m_attach_count { 0 };
  126. };
  127. }