TmpFS.cpp 10 KB

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