File.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/NonnullRefPtr.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/String.h>
  10. #include <AK/Types.h>
  11. #include <AK/Weakable.h>
  12. #include <Kernel/Forward.h>
  13. #include <Kernel/KResult.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/UserOrKernelBuffer.h>
  16. #include <Kernel/VirtualAddress.h>
  17. namespace Kernel {
  18. class File;
  19. class FileBlockCondition : public Thread::BlockCondition {
  20. public:
  21. FileBlockCondition() { }
  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(true, data);
  27. }
  28. void unblock()
  29. {
  30. ScopedSpinLock lock(m_lock);
  31. do_unblock([&](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(false, data);
  35. });
  36. }
  37. };
  38. // File is the base class for anything that can be referenced by a FileDescription.
  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 RefCounted<File>
  66. , public Weakable<File> {
  67. public:
  68. virtual ~File();
  69. virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
  70. virtual KResult close();
  71. virtual bool can_read(const FileDescription&, size_t) const = 0;
  72. virtual bool can_write(const FileDescription&, size_t) const = 0;
  73. virtual KResult attach(FileDescription&);
  74. virtual void detach(FileDescription&);
  75. virtual void did_seek(FileDescription&, off_t) { }
  76. virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
  77. virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0;
  78. virtual int ioctl(FileDescription&, unsigned request, Userspace<void*> arg);
  79. virtual KResultOr<Region*> mmap(Process&, FileDescription&, const Range&, u64 offset, int prot, bool shared);
  80. virtual KResult stat(::stat&) const { return EBADF; }
  81. virtual String absolute_path(const FileDescription&) const = 0;
  82. virtual KResult truncate(u64) { return EINVAL; }
  83. virtual KResult chown(FileDescription&, uid_t, gid_t) { return EBADF; }
  84. virtual KResult chmod(FileDescription&, mode_t) { return EBADF; }
  85. virtual StringView class_name() const = 0;
  86. virtual bool is_seekable() const { return false; }
  87. virtual bool is_inode() const { return false; }
  88. virtual bool is_fifo() const { return false; }
  89. virtual bool is_device() const { return false; }
  90. virtual bool is_tty() const { return false; }
  91. virtual bool is_master_pty() const { return false; }
  92. virtual bool is_block_device() const { return false; }
  93. virtual bool is_character_device() const { return false; }
  94. virtual bool is_socket() const { return false; }
  95. virtual bool is_inode_watcher() const { return false; }
  96. virtual FileBlockCondition& block_condition() { return m_block_condition; }
  97. size_t attach_count() const { return m_attach_count; }
  98. protected:
  99. File();
  100. void evaluate_block_conditions()
  101. {
  102. if (Processor::current().in_irq()) {
  103. // If called from an IRQ handler we need to delay evaluation
  104. // and unblocking of waiting threads. Note that this File
  105. // instance may be deleted until the deferred call is executed!
  106. Processor::deferred_call_queue([self = make_weak_ptr()]() {
  107. if (auto file = self.strong_ref())
  108. file->do_evaluate_block_conditions();
  109. });
  110. } else {
  111. do_evaluate_block_conditions();
  112. }
  113. }
  114. private:
  115. ALWAYS_INLINE void do_evaluate_block_conditions()
  116. {
  117. VERIFY(!Processor::current().in_irq());
  118. block_condition().unblock();
  119. }
  120. FileBlockCondition m_block_condition;
  121. size_t m_attach_count { 0 };
  122. };
  123. }