1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/TmpFS.h>
#include <Kernel/Process.h>
#include <LibC/limits.h>
namespace Kernel {
ErrorOr<NonnullRefPtr<FileSystem>> TmpFS::try_create()
{
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TmpFS));
}
TmpFS::TmpFS() = default;
TmpFS::~TmpFS() = default;
ErrorOr<void> TmpFS::initialize()
{
m_root_inode = TRY(TmpFSInode::try_create_root(*this));
return {};
}
Inode& TmpFS::root_inode()
{
VERIFY(!m_root_inode.is_null());
return *m_root_inode;
}
unsigned TmpFS::next_inode_index()
{
MutexLocker locker(m_lock);
return m_next_inode_index++;
}
TmpFSInode::TmpFSInode(TmpFS& fs, InodeMetadata const& metadata, WeakPtr<TmpFSInode> parent)
: Inode(fs, fs.next_inode_index())
, m_metadata(metadata)
, m_parent(move(parent))
{
m_metadata.inode = identifier();
}
TmpFSInode::~TmpFSInode() = default;
ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, WeakPtr<TmpFSInode> parent)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) TmpFSInode(fs, metadata, move(parent)));
}
ErrorOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create_root(TmpFS& fs)
{
InodeMetadata metadata;
auto now = kgettimeofday().to_truncated_seconds();
metadata.atime = now;
metadata.ctime = now;
metadata.mtime = now;
metadata.mode = S_IFDIR | S_ISVTX | 0777;
return try_create(fs, metadata, {});
}
InodeMetadata TmpFSInode::metadata() const
{
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
return m_metadata;
}
ErrorOr<void> TmpFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
if (!is_directory())
return ENOTDIR;
TRY(callback({ "."sv, identifier(), 0 }));
if (auto parent = m_parent.strong_ref())
TRY(callback({ ".."sv, parent->identifier(), 0 }));
for (auto& child : m_children) {
TRY(callback({ child.name->view(), child.inode->identifier(), 0 }));
}
return {};
}
ErrorOr<size_t> TmpFSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
VERIFY(!is_directory());
VERIFY(offset >= 0);
if (!m_content)
return 0;
if (offset >= m_metadata.size)
return 0;
if (static_cast<off_t>(size) > m_metadata.size - offset)
size = m_metadata.size - offset;
TRY(buffer.write(m_content->data() + offset, size));
return size;
}
ErrorOr<size_t> TmpFSInode::write_bytes(off_t offset, size_t size, UserOrKernelBuffer const& buffer, OpenFileDescription*)
{
MutexLocker locker(m_inode_lock);
VERIFY(!is_directory());
VERIFY(offset >= 0);
TRY(prepare_to_write_data());
off_t old_size = m_metadata.size;
off_t new_size = m_metadata.size;
if (static_cast<off_t>(offset + size) > new_size)
new_size = offset + size;
if (static_cast<u64>(new_size) > (NumericLimits<size_t>::max() / 2)) // on 32-bit, size_t might be 32 bits while off_t is 64 bits
return ENOMEM; // we won't be able to resize to this capacity
if (new_size > old_size) {
if (m_content && static_cast<off_t>(m_content->capacity()) >= new_size) {
m_content->set_size(new_size);
} else {
// Grow the content buffer 2x the new size to accommodate repeating write() calls.
// Note that we're not actually committing physical memory to the buffer
// until it's needed. We only grow VM here.
// FIXME: Fix this so that no memcpy() is necessary, and we can just grow the
// KBuffer and it will add physical pages as needed while keeping the
// existing ones.
auto tmp = TRY(KBuffer::try_create_with_size("TmpFSInode: Content"sv, new_size * 2));
tmp->set_size(new_size);
if (m_content)
memcpy(tmp->data(), m_content->data(), old_size);
m_content = move(tmp);
}
m_metadata.size = new_size;
set_metadata_dirty(true);
}
TRY(buffer.read(m_content->data() + offset, size)); // TODO: partial reads?
did_modify_contents();
return size;
}
ErrorOr<NonnullRefPtr<Inode>> TmpFSInode::lookup(StringView name)
{
MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
VERIFY(is_directory());
if (name == ".")
return *this;
if (name == "..") {
if (auto parent = m_parent.strong_ref())
return parent.release_nonnull();
return ENOENT;
}
auto* child = find_child_by_name(name);
if (!child)
return ENOENT;
return child->inode;
}
TmpFSInode::Child* TmpFSInode::find_child_by_name(StringView name)
{
for (auto& child : m_children) {
if (child.name->view() == name)
return &child;
}
return nullptr;
}
ErrorOr<void> TmpFSInode::flush_metadata()
{
// We don't really have any metadata that could become dirty.
// The only reason we even call set_metadata_dirty() is
// to let the watchers know we have updates. Once that is
// switched to a different mechanism, we can stop ever marking
// our metadata as dirty at all.
set_metadata_dirty(false);
return {};
}
ErrorOr<void> TmpFSInode::chmod(mode_t mode)
{
MutexLocker locker(m_inode_lock);
m_metadata.mode = mode;
set_metadata_dirty(true);
return {};
}
ErrorOr<void> TmpFSInode::chown(UserID uid, GroupID gid)
{
MutexLocker locker(m_inode_lock);
m_metadata.uid = uid;
m_metadata.gid = gid;
set_metadata_dirty(true);
return {};
}
ErrorOr<NonnullRefPtr<Inode>> TmpFSInode::create_child(StringView name, mode_t mode, dev_t dev, UserID uid, GroupID gid)
{
MutexLocker locker(m_inode_lock);
// TODO: Support creating devices on TmpFS.
if (dev != 0)
return ENOTSUP;
time_t now = kgettimeofday().to_truncated_seconds();
InodeMetadata metadata;
metadata.mode = mode;
metadata.uid = uid;
metadata.gid = gid;
metadata.atime = now;
metadata.ctime = now;
metadata.mtime = now;
auto child = TRY(TmpFSInode::try_create(fs(), metadata, *this));
TRY(add_child(*child, name, mode));
return child;
}
ErrorOr<void> TmpFSInode::add_child(Inode& child, StringView name, mode_t)
{
VERIFY(is_directory());
VERIFY(child.fsid() == fsid());
if (name.length() > NAME_MAX)
return ENAMETOOLONG;
MutexLocker locker(m_inode_lock);
for (auto const& existing_child : m_children) {
if (existing_child.name->view() == name)
return EEXIST;
}
auto name_kstring = TRY(KString::try_create(name));
// Balanced by `delete` in remove_child()
auto* child_entry = new (nothrow) Child { move(name_kstring), static_cast<TmpFSInode&>(child) };
if (!child_entry)
return ENOMEM;
m_children.append(*child_entry);
did_add_child(child.identifier(), name);
return {};
}
ErrorOr<void> TmpFSInode::remove_child(StringView name)
{
MutexLocker locker(m_inode_lock);
VERIFY(is_directory());
if (name == "." || name == "..")
return {};
auto* child = find_child_by_name(name);
if (!child)
return ENOENT;
auto child_id = child->inode->identifier();
child->inode->did_delete_self();
m_children.remove(*child);
did_remove_child(child_id, name);
// Balanced by `new` in add_child()
delete child;
return {};
}
ErrorOr<void> TmpFSInode::truncate(u64 size)
{
MutexLocker locker(m_inode_lock);
VERIFY(!is_directory());
if (size == 0)
m_content.clear();
else if (!m_content) {
m_content = TRY(KBuffer::try_create_with_size("TmpFSInode: Content"sv, size));
} else if (static_cast<size_t>(size) < m_content->capacity()) {
size_t prev_size = m_metadata.size;
m_content->set_size(size);
if (prev_size < static_cast<size_t>(size))
memset(m_content->data() + prev_size, 0, size - prev_size);
} else {
size_t prev_size = m_metadata.size;
auto tmp = TRY(KBuffer::try_create_with_size("TmpFSInode: Content"sv, size));
memcpy(tmp->data(), m_content->data(), prev_size);
m_content = move(tmp);
}
m_metadata.size = size;
set_metadata_dirty(true);
return {};
}
ErrorOr<void> TmpFSInode::set_atime(time_t time)
{
MutexLocker locker(m_inode_lock);
m_metadata.atime = time;
set_metadata_dirty(true);
return {};
}
ErrorOr<void> TmpFSInode::set_ctime(time_t time)
{
MutexLocker locker(m_inode_lock);
m_metadata.ctime = time;
set_metadata_dirty(true);
return {};
}
ErrorOr<void> TmpFSInode::set_mtime(time_t t)
{
MutexLocker locker(m_inode_lock);
m_metadata.mtime = t;
set_metadata_dirty(true);
return {};
}
}
|