TmpFS.cpp 9.9 KB

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