TmpFS.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/FileSystem/TmpFS.h>
  27. #include <Kernel/Process.h>
  28. #include <Kernel/Thread.h>
  29. namespace Kernel {
  30. NonnullRefPtr<TmpFS> TmpFS::create()
  31. {
  32. return adopt(*new TmpFS);
  33. }
  34. TmpFS::TmpFS()
  35. {
  36. }
  37. TmpFS::~TmpFS()
  38. {
  39. }
  40. bool TmpFS::initialize()
  41. {
  42. m_root_inode = TmpFSInode::create_root(*this);
  43. return true;
  44. }
  45. NonnullRefPtr<Inode> TmpFS::root_inode() const
  46. {
  47. ASSERT(!m_root_inode.is_null());
  48. return *m_root_inode;
  49. }
  50. void TmpFS::register_inode(TmpFSInode& inode)
  51. {
  52. LOCKER(m_lock);
  53. ASSERT(inode.identifier().fsid() == fsid());
  54. unsigned index = inode.identifier().index();
  55. m_inodes.set(index, inode);
  56. }
  57. void TmpFS::unregister_inode(InodeIdentifier identifier)
  58. {
  59. LOCKER(m_lock);
  60. ASSERT(identifier.fsid() == fsid());
  61. m_inodes.remove(identifier.index());
  62. }
  63. unsigned TmpFS::next_inode_index()
  64. {
  65. LOCKER(m_lock);
  66. return m_next_inode_index++;
  67. }
  68. RefPtr<Inode> TmpFS::get_inode(InodeIdentifier identifier) const
  69. {
  70. LOCKER(m_lock, Lock::Mode::Shared);
  71. ASSERT(identifier.fsid() == fsid());
  72. auto it = m_inodes.find(identifier.index());
  73. if (it == m_inodes.end())
  74. return nullptr;
  75. return it->value;
  76. }
  77. TmpFSInode::TmpFSInode(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent)
  78. : Inode(fs, fs.next_inode_index())
  79. , m_metadata(metadata)
  80. , m_parent(parent)
  81. {
  82. m_metadata.inode = identifier();
  83. }
  84. TmpFSInode::~TmpFSInode()
  85. {
  86. }
  87. NonnullRefPtr<TmpFSInode> TmpFSInode::create(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent)
  88. {
  89. auto inode = adopt(*new TmpFSInode(fs, metadata, parent));
  90. fs.register_inode(inode);
  91. return inode;
  92. }
  93. NonnullRefPtr<TmpFSInode> TmpFSInode::create_root(TmpFS& fs)
  94. {
  95. InodeMetadata metadata;
  96. metadata.mode = S_IFDIR | S_ISVTX | 0777;
  97. return create(fs, metadata, { fs.fsid(), 1 });
  98. }
  99. InodeMetadata TmpFSInode::metadata() const
  100. {
  101. LOCKER(m_lock, Lock::Mode::Shared);
  102. return m_metadata;
  103. }
  104. KResult TmpFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  105. {
  106. LOCKER(m_lock, Lock::Mode::Shared);
  107. if (!is_directory())
  108. return KResult(-ENOTDIR);
  109. callback({ ".", identifier(), 0 });
  110. callback({ "..", m_parent, 0 });
  111. for (auto& it : m_children) {
  112. auto& entry = it.value;
  113. callback({ entry.name, entry.inode->identifier(), 0 });
  114. }
  115. return KSuccess;
  116. }
  117. ssize_t TmpFSInode::read_bytes(off_t offset, ssize_t size, UserOrKernelBuffer& buffer, FileDescription*) const
  118. {
  119. LOCKER(m_lock, Lock::Mode::Shared);
  120. ASSERT(!is_directory());
  121. ASSERT(size >= 0);
  122. ASSERT(offset >= 0);
  123. if (!m_content.has_value())
  124. return 0;
  125. if (offset >= m_metadata.size)
  126. return 0;
  127. if (static_cast<off_t>(size) > m_metadata.size - offset)
  128. size = m_metadata.size - offset;
  129. if (!buffer.write(m_content.value().data() + offset, size))
  130. return -EFAULT;
  131. return size;
  132. }
  133. ssize_t TmpFSInode::write_bytes(off_t offset, ssize_t size, const UserOrKernelBuffer& buffer, FileDescription*)
  134. {
  135. LOCKER(m_lock);
  136. ASSERT(!is_directory());
  137. ASSERT(offset >= 0);
  138. auto result = prepare_to_write_data();
  139. if (result.is_error())
  140. return result;
  141. off_t old_size = m_metadata.size;
  142. off_t new_size = m_metadata.size;
  143. if ((offset + size) > new_size)
  144. new_size = offset + size;
  145. if (new_size > old_size) {
  146. if (m_content.has_value() && m_content.value().capacity() >= (size_t)new_size) {
  147. m_content.value().set_size(new_size);
  148. } else {
  149. // Grow the content buffer 2x the new sizeto accomodate repeating write() calls.
  150. // Note that we're not actually committing physical memory to the buffer
  151. // until it's needed. We only grow VM here.
  152. // FIXME: Fix this so that no memcpy() is necessary, and we can just grow the
  153. // KBuffer and it will add physical pages as needed while keeping the
  154. // existing ones.
  155. auto tmp = KBuffer::create_with_size(new_size * 2);
  156. tmp.set_size(new_size);
  157. if (m_content.has_value())
  158. memcpy(tmp.data(), m_content.value().data(), old_size);
  159. m_content = move(tmp);
  160. }
  161. m_metadata.size = new_size;
  162. set_metadata_dirty(true);
  163. set_metadata_dirty(false);
  164. inode_size_changed(old_size, new_size);
  165. }
  166. if (!buffer.read(m_content.value().data() + offset, size)) // TODO: partial reads?
  167. return -EFAULT;
  168. inode_contents_changed(offset, size, buffer);
  169. return size;
  170. }
  171. RefPtr<Inode> TmpFSInode::lookup(StringView name)
  172. {
  173. LOCKER(m_lock, Lock::Mode::Shared);
  174. ASSERT(is_directory());
  175. if (name == ".")
  176. return this;
  177. if (name == "..")
  178. return fs().get_inode(m_parent);
  179. auto it = m_children.find(name);
  180. if (it == m_children.end())
  181. return {};
  182. return fs().get_inode(it->value.inode->identifier());
  183. }
  184. KResultOr<size_t> TmpFSInode::directory_entry_count() const
  185. {
  186. LOCKER(m_lock, Lock::Mode::Shared);
  187. ASSERT(is_directory());
  188. return 2 + m_children.size();
  189. }
  190. void TmpFSInode::notify_watchers()
  191. {
  192. set_metadata_dirty(true);
  193. set_metadata_dirty(false);
  194. }
  195. void TmpFSInode::flush_metadata()
  196. {
  197. // We don't really have any metadata that could become dirty.
  198. // The only reason we even call set_metadata_dirty() is
  199. // to let the watchers know we have updates. Once that is
  200. // switched to a different mechanism, we can stop ever marking
  201. // our metadata as dirty at all.
  202. set_metadata_dirty(false);
  203. }
  204. KResult TmpFSInode::chmod(mode_t mode)
  205. {
  206. LOCKER(m_lock);
  207. m_metadata.mode = mode;
  208. notify_watchers();
  209. return KSuccess;
  210. }
  211. KResult TmpFSInode::chown(uid_t uid, gid_t gid)
  212. {
  213. LOCKER(m_lock);
  214. m_metadata.uid = uid;
  215. m_metadata.gid = gid;
  216. notify_watchers();
  217. return KSuccess;
  218. }
  219. KResultOr<NonnullRefPtr<Inode>> TmpFSInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  220. {
  221. LOCKER(m_lock);
  222. // TODO: Support creating devices on TmpFS.
  223. if (dev != 0)
  224. return KResult(-ENOTSUP);
  225. struct timeval now;
  226. kgettimeofday(now);
  227. InodeMetadata metadata;
  228. metadata.mode = mode;
  229. metadata.uid = uid;
  230. metadata.gid = gid;
  231. metadata.atime = now.tv_sec;
  232. metadata.ctime = now.tv_sec;
  233. metadata.mtime = now.tv_sec;
  234. auto child = TmpFSInode::create(fs(), metadata, identifier());
  235. auto result = add_child(child, name, mode);
  236. if (result.is_error())
  237. return result;
  238. return child;
  239. }
  240. KResult TmpFSInode::add_child(Inode& child, const StringView& name, mode_t)
  241. {
  242. LOCKER(m_lock);
  243. ASSERT(is_directory());
  244. ASSERT(child.fsid() == fsid());
  245. m_children.set(name, { name, static_cast<TmpFSInode&>(child) });
  246. did_add_child(name);
  247. return KSuccess;
  248. }
  249. KResult TmpFSInode::remove_child(const StringView& name)
  250. {
  251. LOCKER(m_lock);
  252. ASSERT(is_directory());
  253. if (name == "." || name == "..")
  254. return KSuccess;
  255. auto it = m_children.find(name);
  256. if (it == m_children.end())
  257. return KResult(-ENOENT);
  258. m_children.remove(it);
  259. did_remove_child(name);
  260. return KSuccess;
  261. }
  262. KResult TmpFSInode::truncate(u64 size)
  263. {
  264. LOCKER(m_lock);
  265. ASSERT(!is_directory());
  266. if (size == 0)
  267. m_content.clear();
  268. else if (!m_content.has_value()) {
  269. m_content = KBuffer::create_with_size(size);
  270. } else if (static_cast<size_t>(size) < m_content.value().capacity()) {
  271. size_t prev_size = m_metadata.size;
  272. m_content.value().set_size(size);
  273. if (prev_size < static_cast<size_t>(size))
  274. memset(m_content.value().data() + prev_size, 0, size - prev_size);
  275. } else {
  276. size_t prev_size = m_metadata.size;
  277. KBuffer tmp = KBuffer::create_with_size(size);
  278. memcpy(tmp.data(), m_content.value().data(), prev_size);
  279. m_content = move(tmp);
  280. }
  281. size_t old_size = m_metadata.size;
  282. m_metadata.size = size;
  283. notify_watchers();
  284. if (old_size != (size_t)size) {
  285. inode_size_changed(old_size, size);
  286. if (m_content.has_value()) {
  287. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_content.value().data());
  288. inode_contents_changed(0, size, buffer);
  289. }
  290. }
  291. return KSuccess;
  292. }
  293. int TmpFSInode::set_atime(time_t time)
  294. {
  295. LOCKER(m_lock);
  296. m_metadata.atime = time;
  297. set_metadata_dirty(true);
  298. set_metadata_dirty(false);
  299. return KSuccess;
  300. }
  301. int TmpFSInode::set_ctime(time_t time)
  302. {
  303. LOCKER(m_lock);
  304. m_metadata.ctime = time;
  305. notify_watchers();
  306. return KSuccess;
  307. }
  308. int TmpFSInode::set_mtime(time_t time)
  309. {
  310. LOCKER(m_lock);
  311. m_metadata.mtime = time;
  312. notify_watchers();
  313. return KSuccess;
  314. }
  315. void TmpFSInode::one_ref_left()
  316. {
  317. // Destroy ourselves.
  318. fs().unregister_inode(identifier());
  319. }
  320. }