TmpFS.cpp 10 KB

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