The Pedigree Project 0.1
MountView-internal.h
1/* Copyright (c) 2026, Pedigree Developers. */
2#ifndef PEDIGREE_VFS_MOUNTVIEW_INTERNAL_H
3#define PEDIGREE_VFS_MOUNTVIEW_INTERNAL_H
4#include "pedigree/kernel/Atomic.h"
5#include "pedigree/kernel/LockGuard.h"
6#include "pedigree/kernel/process/TerminationDeferral.h"
7#include "pedigree/kernel/utilities/Pointers.h"
8
9#include "MountView.h"
10
11// Unlike ChildLease/RetainedFile, this owner may outlive the acquiring thread.
13 public:
14 VfsNodeReference() = default;
16 VfsNodeReference(VfsNodeReference&& other) noexcept;
17 VfsNodeReference& operator=(VfsNodeReference&& other) noexcept;
19 bool retain(File* node, Filesystem* backing);
20 bool retainAnonymous(File* node);
21 void reset();
22 File* get() const {
23 return m_Node;
24 }
25
26 private:
27 VfsNodeReference(const VfsNodeReference&) = delete;
28 VfsNodeReference& operator=(const VfsNodeReference&) = delete;
29 enum class Ownership { Borrowed, Tracked, Virtual };
30 File* m_Node = nullptr;
31 Ownership m_Ownership = Ownership::Borrowed;
32};
33
35 public:
36 explicit VfsBacking(VFS::FilesystemPin&& retained) : pin(pedigree_std::move(retained)) {}
38 VFS* owningRegistry = nullptr;
40};
42
44 public:
45 VfsAttachment(const VfsBackingRef& retained, uint64_t identity, VfsNodeReference&& selected,
46 uint64_t options)
47 : backing(retained),
48 root(selected.get()),
49 id(identity),
50 flags(options),
51 rootReference(pedigree_std::move(selected)) {}
52 VfsBackingRef backing;
53 File* const root;
54 const uint64_t id;
55 Atomic<uint64_t> flags;
56 uint64_t lockedFlags = 0;
57 bool lockedMount = false;
58 Atomic<size_t> writers{0};
59 VfsNodeReference rootReference;
61};
63
64class VfsPath final : public FilesystemPath {
65 public:
66 enum class Kind { Mounted, Anonymous };
67 VfsPath(VfsMountView& owner, const VfsAttachmentRef& attachment, VfsNodeReference&& node);
68 VfsPath(VfsMountView& owner, VfsNodeReference&& anonymousNode);
69 const Kind kind;
70 ~VfsPath() override;
71 File* node() const override {
72 return file.get();
73 }
74 const void* provider() const override {
75 return &view;
76 }
77 VfsMountView& view;
78 VfsAttachmentRef attachment;
80 // A regular inode can have several names with different directory ancestry.
81 FilesystemPathRef lookupParent;
82 String lookupName;
83};
84
86 VfsAttachmentRef attachment;
87 VfsAttachmentRef parent;
89 VfsAttachmentRow* next = nullptr;
90};
93 VfsContextRow* next = nullptr;
94};
95
97 public:
98 explicit VfsFilesystemContext(VfsMountView& owner) : view(owner) {
99 view.retain();
100 }
101 ~VfsFilesystemContext() override;
102 bool snapshot(FilesystemContextSnapshot& result) const override;
103 bool forkForProcess(FilesystemContextOwner& result) const override;
104 void retireProcessOwner() override;
105 const void* provider() const override {
106 return &view;
107 }
108
109 VfsMountView& view;
112 uint64_t generation = 1;
113 VfsContextRow* registration = nullptr;
114};
115
117 explicit State(VfsMountView& owner) : view(owner) {}
118 ~State();
119 VfsMountView& view;
120 Mutex graph;
121 Atomic<size_t> anonymousPaths{0};
122 VfsAttachmentRow* attachments = nullptr;
123 VfsContextRow* contexts = nullptr;
124 size_t contextCount = 0;
125 uint64_t topology = 1;
126 uint64_t rootId = 0;
127 uint64_t nextId = 1;
128
129 VfsAttachmentRow* find(uint64_t id) const;
130 VfsAttachmentRow* at(const VfsPath& path) const;
131 bool snapshotRows(Vector<VfsAttachmentRow>& result);
132 bool contains(const FilesystemPathRef& path) const;
133 VfsPath* nodePath(const FilesystemPathRef& reference) const;
134 VfsPath* path(const FilesystemPathRef& reference) const;
135 bool makePath(const VfsAttachmentRef& attachment, File* node, FilesystemPathRef& result);
136 bool makePath(const VfsAttachmentRef& attachment, VfsNodeReference&& node,
137 FilesystemPathRef& result);
138 bool makeAttachment(const VfsBackingRef& backing, File* root, uint64_t flags,
139 VfsAttachmentRef& result);
140 bool ancestors(const FilesystemPathRef& path, Vector<FilesystemPathRef>& result);
141 bool context(const FilesystemContextRef& reference, VfsFilesystemContext*& result) const;
142 bool createContext(const VfsFilesystemContext* parent, FilesystemContextOwner& result);
143 bool cross(const FilesystemPathRef& path, FilesystemPathRef& result);
144 bool parent(const FilesystemPathRef& path, FilesystemPathRef& result,
145 const FilesystemPathRef& boundary = FilesystemPathRef());
146 bool beneath(const FilesystemPathRef& descendant, const FilesystemPathRef& ancestor);
147 bool format(const FilesystemContextSnapshot& context, const FilesystemPathRef& path,
148 String& result, const VFS::NamespaceMutation& writer);
149 bool resolve(const FilesystemContextSnapshot& context, const FilesystemPathRef& start,
150 const String& pathname, const ResolveOptions& options, FilesystemPathRef& result,
151 const VFS::NamespaceMutation* writer = nullptr);
152 bool follow(const FilesystemContextSnapshot& context, const FilesystemPathRef& selected,
153 const ResolveOptions& options, FilesystemPathRef& result, size_t& links);
154 bool walk(const FilesystemContextSnapshot& context, const FilesystemPathRef& start,
155 const String& pathname, const ResolveOptions& options, FilesystemPathRef& result,
156 size_t& links);
157 bool attach(const FilesystemPathRef& covered, VFS::FilesystemPin&& pin,
158 const VFS::NamespaceMutation& writer, BackingOwnership ownership, uint64_t flags);
159 void reapDetached();
160};
161#endif
Definition File.h:75
Definition Mutex.h:56
Definition VFS.h:58
A vector / dynamic array.
Definition Vector.h:33