InodeWatcher.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/Checked.h>
  10. #include <AK/CircularQueue.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <Kernel/API/InodeWatcherEvent.h>
  14. #include <Kernel/FileSystem/File.h>
  15. #include <Kernel/Forward.h>
  16. #include <Kernel/Locking/MutexProtected.h>
  17. #include <Kernel/Locking/SpinlockProtected.h>
  18. namespace Kernel {
  19. // A specific description of a watch.
  20. struct WatchDescription {
  21. int wd;
  22. Inode& inode;
  23. unsigned event_mask;
  24. static ErrorOr<NonnullOwnPtr<WatchDescription>> create(int wd, Inode& inode, unsigned event_mask)
  25. {
  26. return adopt_nonnull_own_or_enomem(new (nothrow) WatchDescription(wd, inode, event_mask));
  27. }
  28. private:
  29. WatchDescription(int wd, Inode& inode, unsigned event_mask)
  30. : wd(wd)
  31. , inode(inode)
  32. , event_mask(event_mask)
  33. {
  34. }
  35. };
  36. class InodeWatcher final : public File {
  37. public:
  38. static ErrorOr<NonnullRefPtr<InodeWatcher>> try_create();
  39. virtual ~InodeWatcher() override;
  40. virtual bool can_read(OpenFileDescription const&, u64) const override;
  41. virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
  42. // Can't write to an inode watcher.
  43. virtual bool can_write(OpenFileDescription const&, u64) const override { return true; }
  44. virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override { return EIO; }
  45. virtual ErrorOr<void> close() override;
  46. virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const override;
  47. virtual StringView class_name() const override { return "InodeWatcher"sv; };
  48. virtual bool is_inode_watcher() const override { return true; }
  49. void notify_inode_event(Badge<Inode>, InodeIdentifier, InodeWatcherEvent::Type, StringView name = {});
  50. ErrorOr<int> register_inode(Inode&, unsigned event_mask);
  51. ErrorOr<void> unregister_by_wd(int);
  52. void unregister_by_inode(Badge<Inode>, InodeIdentifier);
  53. private:
  54. explicit InodeWatcher() { }
  55. struct Event {
  56. int wd { 0 };
  57. InodeWatcherEvent::Type type { InodeWatcherEvent::Type::Invalid };
  58. OwnPtr<KString> path;
  59. };
  60. SpinlockProtected<CircularQueue<Event, 32>, LockRank::None> m_queue;
  61. Checked<int> m_wd_counter { 1 };
  62. // NOTE: These two hashmaps provide two different ways of reaching the same
  63. // watch description, so they will overlap.
  64. struct WatchMaps {
  65. HashMap<int, NonnullOwnPtr<WatchDescription>> wd_to_watches;
  66. HashMap<InodeIdentifier, WatchDescription*> inode_to_watches;
  67. };
  68. mutable SpinlockProtected<WatchMaps, LockRank::None> m_watch_maps;
  69. };
  70. }