TmpFS.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. NonnullRefPtr<TmpFS> TmpFS::create()
  30. {
  31. return adopt(*new TmpFS);
  32. }
  33. TmpFS::TmpFS()
  34. {
  35. }
  36. TmpFS::~TmpFS()
  37. {
  38. }
  39. bool TmpFS::initialize()
  40. {
  41. m_root_inode = TmpFSInode::create_root(*this);
  42. return true;
  43. }
  44. InodeIdentifier TmpFS::root_inode() const
  45. {
  46. ASSERT(!m_root_inode.is_null());
  47. return m_root_inode->identifier();
  48. }
  49. void TmpFS::register_inode(TmpFSInode& inode)
  50. {
  51. LOCKER(m_lock);
  52. ASSERT(inode.identifier().fsid() == fsid());
  53. unsigned index = inode.identifier().index();
  54. m_inodes.set(index, inode);
  55. }
  56. void TmpFS::unregister_inode(InodeIdentifier identifier)
  57. {
  58. LOCKER(m_lock);
  59. ASSERT(identifier.fsid() == fsid());
  60. m_inodes.remove(identifier.index());
  61. }
  62. unsigned TmpFS::next_inode_index()
  63. {
  64. LOCKER(m_lock);
  65. return m_next_inode_index++;
  66. }
  67. RefPtr<Inode> TmpFS::get_inode(InodeIdentifier identifier) const
  68. {
  69. LOCKER(m_lock);
  70. ASSERT(identifier.fsid() == fsid());
  71. auto it = m_inodes.find(identifier.index());
  72. if (it == m_inodes.end())
  73. return nullptr;
  74. return it->value;
  75. }
  76. KResultOr<NonnullRefPtr<Inode>> TmpFS::create_inode(InodeIdentifier parent_id, const String& name, mode_t mode, off_t size, dev_t dev, uid_t uid, gid_t gid)
  77. {
  78. LOCKER(m_lock);
  79. ASSERT(parent_id.fsid() == fsid());
  80. ASSERT(size == 0);
  81. ASSERT(dev == 0);
  82. struct timeval now;
  83. kgettimeofday(now);
  84. InodeMetadata metadata;
  85. metadata.mode = mode;
  86. metadata.uid = uid;
  87. metadata.gid = gid;
  88. metadata.atime = now.tv_sec;
  89. metadata.ctime = now.tv_sec;
  90. metadata.mtime = now.tv_sec;
  91. auto inode = TmpFSInode::create(*this, metadata, parent_id);
  92. auto it = m_inodes.find(parent_id.index());
  93. ASSERT(it != m_inodes.end());
  94. auto parent_inode = it->value;
  95. auto result = parent_inode->add_child(inode->identifier(), name, mode);
  96. if (result.is_error())
  97. return result;
  98. return inode;
  99. }
  100. KResult TmpFS::create_directory(InodeIdentifier parent_id, const String& name, mode_t mode, uid_t uid, gid_t gid)
  101. {
  102. // Ensure it's a directory.
  103. mode &= ~0170000;
  104. mode |= 0040000;
  105. auto result = create_inode(parent_id, name, mode, 0, 0, uid, gid);
  106. if (result.is_error())
  107. return result.error();
  108. return KSuccess;
  109. }
  110. TmpFSInode::TmpFSInode(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent)
  111. : Inode(fs, fs.next_inode_index())
  112. , m_metadata(metadata)
  113. , m_parent(parent)
  114. {
  115. m_metadata.inode = identifier();
  116. }
  117. TmpFSInode::~TmpFSInode()
  118. {
  119. }
  120. NonnullRefPtr<TmpFSInode> TmpFSInode::create(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent)
  121. {
  122. auto inode = adopt(*new TmpFSInode(fs, metadata, parent));
  123. fs.register_inode(inode);
  124. return inode;
  125. }
  126. NonnullRefPtr<TmpFSInode> TmpFSInode::create_root(TmpFS& fs)
  127. {
  128. InodeMetadata metadata;
  129. metadata.mode = 0041777;
  130. return create(fs, metadata, { fs.fsid(), 1 });
  131. }
  132. InodeMetadata TmpFSInode::metadata() const
  133. {
  134. LOCKER(m_lock);
  135. return m_metadata;
  136. }
  137. bool TmpFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  138. {
  139. LOCKER(m_lock);
  140. if (!is_directory())
  141. return false;
  142. callback({ ".", identifier(), 0 });
  143. callback({ "..", m_parent, 0 });
  144. for (auto& it : m_children)
  145. callback(it.value.entry);
  146. return true;
  147. }
  148. ssize_t TmpFSInode::read_bytes(off_t offset, ssize_t size, u8* buffer, FileDescription*) const
  149. {
  150. LOCKER(m_lock);
  151. ASSERT(!is_directory());
  152. ASSERT(size >= 0);
  153. ASSERT(offset >= 0);
  154. if (!m_content.has_value())
  155. return 0;
  156. if (offset >= m_metadata.size)
  157. return 0;
  158. if (static_cast<off_t>(size) > m_metadata.size - offset)
  159. size = m_metadata.size - offset;
  160. memcpy(buffer, m_content.value().data() + offset, size);
  161. return size;
  162. }
  163. ssize_t TmpFSInode::write_bytes(off_t offset, ssize_t size, const u8* buffer, FileDescription*)
  164. {
  165. LOCKER(m_lock);
  166. ASSERT(!is_directory());
  167. ASSERT(offset >= 0);
  168. off_t old_size = m_metadata.size;
  169. off_t new_size = m_metadata.size;
  170. if ((offset + size) > new_size)
  171. new_size = offset + size;
  172. if (new_size > old_size) {
  173. if (m_content.has_value() && m_content.value().capacity() >= (size_t)new_size) {
  174. m_content.value().set_size(new_size);
  175. } else {
  176. // Grow the content buffer 2x the new sizeto accomodate repeating write() calls.
  177. // Note that we're not actually committing physical memory to the buffer
  178. // until it's needed. We only grow VM here.
  179. // FIXME: Fix this so that no memcpy() is necessary, and we can just grow the
  180. // KBuffer and it will add physical pages as needed while keeping the
  181. // existing ones.
  182. auto tmp = KBuffer::create_with_size(new_size * 2);
  183. tmp.set_size(new_size);
  184. if (m_content.has_value())
  185. memcpy(tmp.data(), m_content.value().data(), old_size);
  186. m_content = move(tmp);
  187. }
  188. m_metadata.size = new_size;
  189. set_metadata_dirty(true);
  190. set_metadata_dirty(false);
  191. inode_size_changed(old_size, new_size);
  192. }
  193. memcpy(m_content.value().data() + offset, buffer, size);
  194. inode_contents_changed(offset, size, buffer);
  195. return size;
  196. }
  197. RefPtr<Inode> TmpFSInode::lookup(StringView name)
  198. {
  199. LOCKER(m_lock);
  200. ASSERT(is_directory());
  201. if (name == ".")
  202. return fs().get_inode(identifier());
  203. if (name == "..")
  204. return fs().get_inode(m_parent);
  205. auto it = m_children.find(name);
  206. if (it == m_children.end())
  207. return {};
  208. return fs().get_inode(it->value.entry.inode);
  209. }
  210. size_t TmpFSInode::directory_entry_count() const
  211. {
  212. LOCKER(m_lock);
  213. ASSERT(is_directory());
  214. return 2 + m_children.size();
  215. }
  216. void TmpFSInode::flush_metadata()
  217. {
  218. // We don't really have any metadata that could become dirty.
  219. // The only reason we even call set_metadata_dirty() is
  220. // to let the watchers know we have updates. Once that is
  221. // switched to a different mechanism, we can stop ever marking
  222. // our metadata as dirty at all.
  223. set_metadata_dirty(false);
  224. }
  225. KResult TmpFSInode::chmod(mode_t mode)
  226. {
  227. LOCKER(m_lock);
  228. m_metadata.mode = mode;
  229. set_metadata_dirty(true);
  230. set_metadata_dirty(false);
  231. return KSuccess;
  232. }
  233. KResult TmpFSInode::chown(uid_t uid, gid_t gid)
  234. {
  235. LOCKER(m_lock);
  236. m_metadata.uid = uid;
  237. m_metadata.gid = gid;
  238. set_metadata_dirty(true);
  239. set_metadata_dirty(false);
  240. return KSuccess;
  241. }
  242. KResult TmpFSInode::add_child(InodeIdentifier child_id, const StringView& name, mode_t)
  243. {
  244. LOCKER(m_lock);
  245. ASSERT(is_directory());
  246. ASSERT(child_id.fsid() == fsid());
  247. String owned_name = name;
  248. FS::DirectoryEntry entry = { owned_name.characters(), owned_name.length(), child_id, 0 };
  249. RefPtr<Inode> child_tmp = fs().get_inode(child_id);
  250. NonnullRefPtr<TmpFSInode> child = static_cast<NonnullRefPtr<TmpFSInode>>(child_tmp.release_nonnull());
  251. m_children.set(owned_name, { entry, move(child) });
  252. set_metadata_dirty(true);
  253. set_metadata_dirty(false);
  254. return KSuccess;
  255. }
  256. KResult TmpFSInode::remove_child(const StringView& name)
  257. {
  258. LOCKER(m_lock);
  259. ASSERT(is_directory());
  260. if (name == "." || name == "..")
  261. return KSuccess;
  262. auto it = m_children.find(name);
  263. if (it == m_children.end())
  264. return KResult(-ENOENT);
  265. m_children.remove(it);
  266. set_metadata_dirty(true);
  267. set_metadata_dirty(false);
  268. return KSuccess;
  269. }
  270. KResult TmpFSInode::truncate(u64 size)
  271. {
  272. LOCKER(m_lock);
  273. ASSERT(!is_directory());
  274. if (size == 0)
  275. m_content.clear();
  276. else if (!m_content.has_value()) {
  277. m_content = KBuffer::create_with_size(size);
  278. } else if (static_cast<size_t>(size) < m_content.value().capacity()) {
  279. size_t prev_size = m_metadata.size;
  280. m_content.value().set_size(size);
  281. if (prev_size < static_cast<size_t>(size))
  282. memset(m_content.value().data() + prev_size, 0, size - prev_size);
  283. } else {
  284. size_t prev_size = m_metadata.size;
  285. KBuffer tmp = KBuffer::create_with_size(size);
  286. memcpy(tmp.data(), m_content.value().data(), prev_size);
  287. m_content = move(tmp);
  288. }
  289. size_t old_size = m_metadata.size;
  290. m_metadata.size = size;
  291. set_metadata_dirty(true);
  292. set_metadata_dirty(false);
  293. if (old_size != (size_t)size) {
  294. inode_size_changed(old_size, size);
  295. if (m_content.has_value())
  296. inode_contents_changed(0, size, m_content.value().data());
  297. }
  298. return KSuccess;
  299. }
  300. int TmpFSInode::set_atime(time_t time)
  301. {
  302. LOCKER(m_lock);
  303. m_metadata.atime = time;
  304. set_metadata_dirty(true);
  305. set_metadata_dirty(false);
  306. return KSuccess;
  307. }
  308. int TmpFSInode::set_ctime(time_t time)
  309. {
  310. LOCKER(m_lock);
  311. m_metadata.ctime = time;
  312. set_metadata_dirty(true);
  313. set_metadata_dirty(false);
  314. return KSuccess;
  315. }
  316. int TmpFSInode::set_mtime(time_t time)
  317. {
  318. LOCKER(m_lock);
  319. m_metadata.mtime = time;
  320. set_metadata_dirty(true);
  321. set_metadata_dirty(false);
  322. return KSuccess;
  323. }
  324. void TmpFSInode::one_ref_left()
  325. {
  326. // Destroy ourselves.
  327. fs().unregister_inode(identifier());
  328. }