Ext2FileSystem.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@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 <AK/Bitmap.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/MemoryStream.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/StringView.h>
  31. #include <Kernel/Debug.h>
  32. #include <Kernel/Devices/BlockDevice.h>
  33. #include <Kernel/FileSystem/Ext2FileSystem.h>
  34. #include <Kernel/FileSystem/FileDescription.h>
  35. #include <Kernel/FileSystem/ext2_fs.h>
  36. #include <Kernel/Process.h>
  37. #include <Kernel/UnixTypes.h>
  38. #include <LibC/errno_numbers.h>
  39. namespace Kernel {
  40. static const size_t max_link_count = 65535;
  41. static const size_t max_block_size = 4096;
  42. static const ssize_t max_inline_symlink_length = 60;
  43. struct Ext2FSDirectoryEntry {
  44. String name;
  45. InodeIndex inode_index { 0 };
  46. u8 file_type { 0 };
  47. };
  48. static u8 to_ext2_file_type(mode_t mode)
  49. {
  50. if (is_regular_file(mode))
  51. return EXT2_FT_REG_FILE;
  52. if (is_directory(mode))
  53. return EXT2_FT_DIR;
  54. if (is_character_device(mode))
  55. return EXT2_FT_CHRDEV;
  56. if (is_block_device(mode))
  57. return EXT2_FT_BLKDEV;
  58. if (is_fifo(mode))
  59. return EXT2_FT_FIFO;
  60. if (is_socket(mode))
  61. return EXT2_FT_SOCK;
  62. if (is_symlink(mode))
  63. return EXT2_FT_SYMLINK;
  64. return EXT2_FT_UNKNOWN;
  65. }
  66. static unsigned divide_rounded_up(unsigned a, unsigned b)
  67. {
  68. return (a / b) + (a % b != 0);
  69. }
  70. NonnullRefPtr<Ext2FS> Ext2FS::create(FileDescription& file_description)
  71. {
  72. return adopt(*new Ext2FS(file_description));
  73. }
  74. Ext2FS::Ext2FS(FileDescription& file_description)
  75. : BlockBasedFS(file_description)
  76. {
  77. }
  78. Ext2FS::~Ext2FS()
  79. {
  80. }
  81. bool Ext2FS::flush_super_block()
  82. {
  83. LOCKER(m_lock);
  84. ASSERT((sizeof(ext2_super_block) % logical_block_size()) == 0);
  85. auto super_block_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&m_super_block);
  86. bool success = raw_write_blocks(2, (sizeof(ext2_super_block) / logical_block_size()), super_block_buffer);
  87. ASSERT(success);
  88. return true;
  89. }
  90. const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
  91. {
  92. // FIXME: Should this fail gracefully somehow?
  93. ASSERT(group_index <= m_block_group_count);
  94. ASSERT(group_index > 0);
  95. return block_group_descriptors()[group_index.value() - 1];
  96. }
  97. bool Ext2FS::initialize()
  98. {
  99. LOCKER(m_lock);
  100. ASSERT((sizeof(ext2_super_block) % logical_block_size()) == 0);
  101. auto super_block_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&m_super_block);
  102. bool success = raw_read_blocks(2, (sizeof(ext2_super_block) / logical_block_size()), super_block_buffer);
  103. ASSERT(success);
  104. auto& super_block = this->super_block();
  105. if constexpr (EXT2_DEBUG) {
  106. klog() << "ext2fs: super block magic: " << String::format("%x", super_block.s_magic) << " (super block size: " << sizeof(ext2_super_block) << ")";
  107. }
  108. if (super_block.s_magic != EXT2_SUPER_MAGIC)
  109. return false;
  110. if constexpr (EXT2_DEBUG) {
  111. klog() << "ext2fs: " << super_block.s_inodes_count << " inodes, " << super_block.s_blocks_count << " blocks";
  112. klog() << "ext2fs: block size = " << EXT2_BLOCK_SIZE(&super_block);
  113. klog() << "ext2fs: first data block = " << super_block.s_first_data_block;
  114. klog() << "ext2fs: inodes per block = " << inodes_per_block();
  115. klog() << "ext2fs: inodes per group = " << inodes_per_group();
  116. klog() << "ext2fs: free inodes = " << super_block.s_free_inodes_count;
  117. klog() << "ext2fs: desc per block = " << EXT2_DESC_PER_BLOCK(&super_block);
  118. klog() << "ext2fs: desc size = " << EXT2_DESC_SIZE(&super_block);
  119. }
  120. set_block_size(EXT2_BLOCK_SIZE(&super_block));
  121. ASSERT(block_size() <= (int)max_block_size);
  122. m_block_group_count = ceil_div(super_block.s_blocks_count, super_block.s_blocks_per_group);
  123. if (m_block_group_count == 0) {
  124. klog() << "ext2fs: no block groups :(";
  125. return false;
  126. }
  127. unsigned blocks_to_read = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
  128. BlockIndex first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
  129. m_cached_group_descriptor_table = KBuffer::try_create_with_size(block_size() * blocks_to_read, Region::Access::Read | Region::Access::Write, "Ext2FS: Block group descriptors");
  130. if (!m_cached_group_descriptor_table) {
  131. dbgln("Ext2FS: Failed to allocate memory for group descriptor table");
  132. return false;
  133. }
  134. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table->data());
  135. auto result = read_blocks(first_block_of_bgdt, blocks_to_read, buffer);
  136. if (result.is_error()) {
  137. // FIXME: Propagate the error
  138. dbgln("Ext2FS: initialize had error: {}", result.error());
  139. return false;
  140. }
  141. if constexpr (EXT2_DEBUG) {
  142. for (unsigned i = 1; i <= m_block_group_count; ++i) {
  143. auto& group = group_descriptor(i);
  144. klog() << "ext2fs: group[" << i << "] { block_bitmap: " << group.bg_block_bitmap << ", inode_bitmap: " << group.bg_inode_bitmap << ", inode_table: " << group.bg_inode_table << " }";
  145. }
  146. }
  147. return true;
  148. }
  149. const char* Ext2FS::class_name() const
  150. {
  151. return "Ext2FS";
  152. }
  153. NonnullRefPtr<Inode> Ext2FS::root_inode() const
  154. {
  155. return *get_inode({ fsid(), EXT2_ROOT_INO });
  156. }
  157. bool Ext2FS::find_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const
  158. {
  159. LOCKER(m_lock);
  160. auto& super_block = this->super_block();
  161. if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&super_block))
  162. return false;
  163. if (inode > super_block.s_inodes_count)
  164. return false;
  165. auto& bgd = group_descriptor(group_index_from_inode(inode));
  166. offset = ((inode.value() - 1) % inodes_per_group()) * inode_size();
  167. block_index = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&super_block));
  168. offset &= block_size() - 1;
  169. return true;
  170. }
  171. Ext2FS::BlockListShape Ext2FS::compute_block_list_shape(unsigned blocks) const
  172. {
  173. BlockListShape shape;
  174. const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  175. unsigned blocks_remaining = blocks;
  176. shape.direct_blocks = min((unsigned)EXT2_NDIR_BLOCKS, blocks_remaining);
  177. blocks_remaining -= shape.direct_blocks;
  178. if (!blocks_remaining)
  179. return shape;
  180. shape.indirect_blocks = min(blocks_remaining, entries_per_block);
  181. shape.meta_blocks += 1;
  182. blocks_remaining -= shape.indirect_blocks;
  183. if (!blocks_remaining)
  184. return shape;
  185. shape.doubly_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block);
  186. shape.meta_blocks += 1;
  187. shape.meta_blocks += divide_rounded_up(shape.doubly_indirect_blocks, entries_per_block);
  188. blocks_remaining -= shape.doubly_indirect_blocks;
  189. if (!blocks_remaining)
  190. return shape;
  191. shape.triply_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block * entries_per_block);
  192. shape.meta_blocks += 1;
  193. shape.meta_blocks += divide_rounded_up(shape.triply_indirect_blocks, entries_per_block * entries_per_block);
  194. shape.meta_blocks += divide_rounded_up(shape.triply_indirect_blocks, entries_per_block);
  195. blocks_remaining -= shape.triply_indirect_blocks;
  196. ASSERT(blocks_remaining == 0);
  197. return shape;
  198. }
  199. KResult Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2inode, const Vector<BlockIndex>& blocks)
  200. {
  201. LOCKER(m_lock);
  202. if (blocks.is_empty()) {
  203. e2inode.i_blocks = 0;
  204. memset(e2inode.i_block, 0, sizeof(e2inode.i_block));
  205. write_ext2_inode(inode_index, e2inode);
  206. return KSuccess;
  207. }
  208. // NOTE: There is a mismatch between i_blocks and blocks.size() since i_blocks includes meta blocks and blocks.size() does not.
  209. auto old_block_count = ceil_div(static_cast<size_t>(e2inode.i_size), block_size());
  210. auto old_shape = compute_block_list_shape(old_block_count);
  211. auto new_shape = compute_block_list_shape(blocks.size());
  212. Vector<BlockIndex> new_meta_blocks;
  213. if (new_shape.meta_blocks > old_shape.meta_blocks) {
  214. new_meta_blocks = allocate_blocks(group_index_from_inode(inode_index), new_shape.meta_blocks - old_shape.meta_blocks);
  215. }
  216. e2inode.i_blocks = (blocks.size() + new_shape.meta_blocks) * (block_size() / 512);
  217. bool inode_dirty = false;
  218. unsigned output_block_index = 0;
  219. unsigned remaining_blocks = blocks.size();
  220. for (unsigned i = 0; i < new_shape.direct_blocks; ++i) {
  221. if (e2inode.i_block[i] != blocks[output_block_index])
  222. inode_dirty = true;
  223. e2inode.i_block[i] = blocks[output_block_index].value();
  224. ++output_block_index;
  225. --remaining_blocks;
  226. }
  227. if (inode_dirty) {
  228. if constexpr (EXT2_DEBUG) {
  229. dbgln("Ext2FS: Writing {} direct block(s) to i_block array of inode {}", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()), inode_index);
  230. for (size_t i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i)
  231. dbgln(" + {}", blocks[i]);
  232. }
  233. write_ext2_inode(inode_index, e2inode);
  234. inode_dirty = false;
  235. }
  236. if (!remaining_blocks)
  237. return KSuccess;
  238. const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  239. bool ind_block_new = !e2inode.i_block[EXT2_IND_BLOCK];
  240. if (ind_block_new) {
  241. BlockIndex new_indirect_block = new_meta_blocks.take_last();
  242. if (e2inode.i_block[EXT2_IND_BLOCK] != new_indirect_block)
  243. inode_dirty = true;
  244. e2inode.i_block[EXT2_IND_BLOCK] = new_indirect_block.value();
  245. if (inode_dirty) {
  246. dbgln_if(EXT2_DEBUG, "Ext2FS: Adding the indirect block to i_block array of inode {}", inode_index);
  247. write_ext2_inode(inode_index, e2inode);
  248. inode_dirty = false;
  249. }
  250. }
  251. if (old_shape.indirect_blocks == new_shape.indirect_blocks) {
  252. // No need to update the singly indirect block array.
  253. remaining_blocks -= new_shape.indirect_blocks;
  254. output_block_index += new_shape.indirect_blocks;
  255. } else {
  256. auto block_contents = ByteBuffer::create_uninitialized(block_size());
  257. OutputMemoryStream stream { block_contents };
  258. ASSERT(new_shape.indirect_blocks <= entries_per_block);
  259. for (unsigned i = 0; i < new_shape.indirect_blocks; ++i) {
  260. stream << blocks[output_block_index++].value();
  261. --remaining_blocks;
  262. }
  263. stream.fill_to_end(0);
  264. auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
  265. auto result = write_block(e2inode.i_block[EXT2_IND_BLOCK], buffer, stream.size());
  266. if (result.is_error())
  267. return result;
  268. }
  269. if (!remaining_blocks)
  270. return KSuccess;
  271. bool dind_block_dirty = false;
  272. bool dind_block_new = !e2inode.i_block[EXT2_DIND_BLOCK];
  273. if (dind_block_new) {
  274. BlockIndex new_dindirect_block = new_meta_blocks.take_last();
  275. if (e2inode.i_block[EXT2_DIND_BLOCK] != new_dindirect_block)
  276. inode_dirty = true;
  277. e2inode.i_block[EXT2_DIND_BLOCK] = new_dindirect_block.value();
  278. if (inode_dirty) {
  279. dbgln_if(EXT2_DEBUG, "Ext2FS: Adding the doubly-indirect block to i_block array of inode {}", inode_index);
  280. write_ext2_inode(inode_index, e2inode);
  281. inode_dirty = false;
  282. }
  283. }
  284. if (old_shape.doubly_indirect_blocks == new_shape.doubly_indirect_blocks) {
  285. // No need to update the doubly indirect block data.
  286. remaining_blocks -= new_shape.doubly_indirect_blocks;
  287. output_block_index += new_shape.doubly_indirect_blocks;
  288. } else {
  289. unsigned indirect_block_count = divide_rounded_up(new_shape.doubly_indirect_blocks, entries_per_block);
  290. auto dind_block_contents = ByteBuffer::create_uninitialized(block_size());
  291. if (dind_block_new) {
  292. dind_block_contents.zero_fill();
  293. dind_block_dirty = true;
  294. } else {
  295. auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
  296. auto result = read_block(e2inode.i_block[EXT2_DIND_BLOCK], &buffer, block_size());
  297. if (result.is_error()) {
  298. dbgln("Ext2FS: write_block_list_for_inode had error: {}", result.error());
  299. return result;
  300. }
  301. }
  302. auto* dind_block_as_pointers = (unsigned*)dind_block_contents.data();
  303. ASSERT(indirect_block_count <= entries_per_block);
  304. for (unsigned i = 0; i < indirect_block_count; ++i) {
  305. bool ind_block_dirty = false;
  306. BlockIndex indirect_block_index = dind_block_as_pointers[i];
  307. bool ind_block_new = !indirect_block_index;
  308. if (ind_block_new) {
  309. indirect_block_index = new_meta_blocks.take_last();
  310. dind_block_as_pointers[i] = indirect_block_index.value();
  311. dind_block_dirty = true;
  312. }
  313. auto ind_block_contents = ByteBuffer::create_uninitialized(block_size());
  314. if (ind_block_new) {
  315. ind_block_contents.zero_fill();
  316. ind_block_dirty = true;
  317. } else {
  318. auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
  319. auto result = read_block(indirect_block_index, &buffer, block_size());
  320. if (result.is_error()) {
  321. dbgln("Ext2FS: write_block_list_for_inode had error: {}", result.error());
  322. return result;
  323. }
  324. }
  325. auto* ind_block_as_pointers = (unsigned*)ind_block_contents.data();
  326. unsigned entries_to_write = new_shape.doubly_indirect_blocks - (i * entries_per_block);
  327. if (entries_to_write > entries_per_block)
  328. entries_to_write = entries_per_block;
  329. ASSERT(entries_to_write <= entries_per_block);
  330. for (unsigned j = 0; j < entries_to_write; ++j) {
  331. BlockIndex output_block = blocks[output_block_index++];
  332. if (ind_block_as_pointers[j] != output_block) {
  333. ind_block_as_pointers[j] = output_block.value();
  334. ind_block_dirty = true;
  335. }
  336. --remaining_blocks;
  337. }
  338. for (unsigned j = entries_to_write; j < entries_per_block; ++j) {
  339. if (ind_block_as_pointers[j] != 0) {
  340. ind_block_as_pointers[j] = 0;
  341. ind_block_dirty = true;
  342. }
  343. }
  344. if (ind_block_dirty) {
  345. auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
  346. int err = write_block(indirect_block_index, buffer, block_size());
  347. ASSERT(err >= 0);
  348. }
  349. }
  350. for (unsigned i = indirect_block_count; i < entries_per_block; ++i) {
  351. if (dind_block_as_pointers[i] != 0) {
  352. dind_block_as_pointers[i] = 0;
  353. dind_block_dirty = true;
  354. }
  355. }
  356. if (dind_block_dirty) {
  357. auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
  358. int err = write_block(e2inode.i_block[EXT2_DIND_BLOCK], buffer, block_size());
  359. ASSERT(err >= 0);
  360. }
  361. }
  362. if (!remaining_blocks)
  363. return KSuccess;
  364. // FIXME: Implement!
  365. dbgln("we don't know how to write tind ext2fs blocks yet!");
  366. ASSERT_NOT_REACHED();
  367. }
  368. Vector<Ext2FS::BlockIndex> Ext2FS::block_list_for_inode(const ext2_inode& e2inode, bool include_block_list_blocks) const
  369. {
  370. auto block_list = block_list_for_inode_impl(e2inode, include_block_list_blocks);
  371. while (!block_list.is_empty() && block_list.last() == 0)
  372. block_list.take_last();
  373. return block_list;
  374. }
  375. Vector<Ext2FS::BlockIndex> Ext2FS::block_list_for_inode_impl(const ext2_inode& e2inode, bool include_block_list_blocks) const
  376. {
  377. LOCKER(m_lock);
  378. unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  379. unsigned block_count = ceil_div(static_cast<size_t>(e2inode.i_size), block_size());
  380. // If we are handling a symbolic link, the path is stored in the 60 bytes in
  381. // the inode that are used for the 12 direct and 3 indirect block pointers,
  382. // If the path is longer than 60 characters, a block is allocated, and the
  383. // block contains the destination path. The file size corresponds to the
  384. // path length of the destination.
  385. if (is_symlink(e2inode.i_mode) && e2inode.i_blocks == 0)
  386. block_count = 0;
  387. dbgln_if(EXT2_DEBUG, "Ext2FS::block_list_for_inode(): i_size={}, i_blocks={}, block_count={}", e2inode.i_size, e2inode.i_blocks, block_count);
  388. unsigned blocks_remaining = block_count;
  389. if (include_block_list_blocks) {
  390. auto shape = compute_block_list_shape(block_count);
  391. blocks_remaining += shape.meta_blocks;
  392. }
  393. Vector<BlockIndex> list;
  394. auto add_block = [&](BlockIndex bi) {
  395. if (blocks_remaining) {
  396. list.append(bi);
  397. --blocks_remaining;
  398. }
  399. };
  400. if (include_block_list_blocks) {
  401. // This seems like an excessive over-estimate but w/e.
  402. list.ensure_capacity(blocks_remaining * 2);
  403. } else {
  404. list.ensure_capacity(blocks_remaining);
  405. }
  406. unsigned direct_count = min(block_count, (unsigned)EXT2_NDIR_BLOCKS);
  407. for (unsigned i = 0; i < direct_count; ++i) {
  408. auto block_index = e2inode.i_block[i];
  409. add_block(block_index);
  410. }
  411. if (!blocks_remaining)
  412. return list;
  413. // Don't need to make copy of add_block, since this capture will only
  414. // be called before block_list_for_inode_impl finishes.
  415. auto process_block_array = [&](BlockIndex array_block_index, auto&& callback) {
  416. if (include_block_list_blocks)
  417. add_block(array_block_index);
  418. auto count = min(blocks_remaining, entries_per_block);
  419. if (!count)
  420. return;
  421. u32 array[count];
  422. auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)array);
  423. auto result = read_block(array_block_index, &buffer, sizeof(array), 0);
  424. if (result.is_error()) {
  425. // FIXME: Stop here and propagate this error.
  426. dbgln("Ext2FS: block_list_for_inode_impl had error: {}", result.error());
  427. }
  428. for (unsigned i = 0; i < count; ++i)
  429. callback(BlockIndex(array[i]));
  430. };
  431. process_block_array(e2inode.i_block[EXT2_IND_BLOCK], [&](BlockIndex block_index) {
  432. add_block(block_index);
  433. });
  434. if (!blocks_remaining)
  435. return list;
  436. process_block_array(e2inode.i_block[EXT2_DIND_BLOCK], [&](BlockIndex block_index) {
  437. process_block_array(block_index, [&](BlockIndex block_index2) {
  438. add_block(block_index2);
  439. });
  440. });
  441. if (!blocks_remaining)
  442. return list;
  443. process_block_array(e2inode.i_block[EXT2_TIND_BLOCK], [&](BlockIndex block_index) {
  444. process_block_array(block_index, [&](BlockIndex block_index2) {
  445. process_block_array(block_index2, [&](BlockIndex block_index3) {
  446. add_block(block_index3);
  447. });
  448. });
  449. });
  450. return list;
  451. }
  452. void Ext2FS::free_inode(Ext2FSInode& inode)
  453. {
  454. LOCKER(m_lock);
  455. ASSERT(inode.m_raw_inode.i_links_count == 0);
  456. dbgln_if(EXT2_DEBUG, "Ext2FS: Inode {} has no more links, time to delete!", inode.index());
  457. // Mark all blocks used by this inode as free.
  458. auto block_list = block_list_for_inode(inode.m_raw_inode, true);
  459. for (auto block_index : block_list) {
  460. ASSERT(block_index <= super_block().s_blocks_count);
  461. if (block_index.value())
  462. set_block_allocation_state(block_index, false);
  463. }
  464. // If the inode being freed is a directory, update block group directory counter.
  465. if (inode.is_directory()) {
  466. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode.index())));
  467. --bgd.bg_used_dirs_count;
  468. dbgln("Ext2FS: Decremented bg_used_dirs_count to {}", bgd.bg_used_dirs_count);
  469. m_block_group_descriptors_dirty = true;
  470. }
  471. // NOTE: After this point, the inode metadata is wiped.
  472. memset(&inode.m_raw_inode, 0, sizeof(ext2_inode));
  473. inode.m_raw_inode.i_dtime = kgettimeofday().tv_sec;
  474. write_ext2_inode(inode.index(), inode.m_raw_inode);
  475. // Mark the inode as free.
  476. set_inode_allocation_state(inode.index(), false);
  477. }
  478. void Ext2FS::flush_block_group_descriptor_table()
  479. {
  480. LOCKER(m_lock);
  481. unsigned blocks_to_write = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
  482. unsigned first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
  483. auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)block_group_descriptors());
  484. auto result = write_blocks(first_block_of_bgdt, blocks_to_write, buffer);
  485. if (result.is_error())
  486. dbgln("Ext2FS: flush_block_group_descriptor_table had error: {}", result.error());
  487. }
  488. void Ext2FS::flush_writes()
  489. {
  490. LOCKER(m_lock);
  491. if (m_super_block_dirty) {
  492. flush_super_block();
  493. m_super_block_dirty = false;
  494. }
  495. if (m_block_group_descriptors_dirty) {
  496. flush_block_group_descriptor_table();
  497. m_block_group_descriptors_dirty = false;
  498. }
  499. for (auto& cached_bitmap : m_cached_bitmaps) {
  500. if (cached_bitmap->dirty) {
  501. auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer.data());
  502. auto result = write_block(cached_bitmap->bitmap_block_index, buffer, block_size());
  503. if (result.is_error()) {
  504. dbgln("Ext2FS: flush_writes() had error {}", result.error());
  505. }
  506. cached_bitmap->dirty = false;
  507. dbgln_if(EXT2_DEBUG, "Flushed bitmap block {}", cached_bitmap->bitmap_block_index);
  508. }
  509. }
  510. BlockBasedFS::flush_writes();
  511. // Uncache Inodes that are only kept alive by the index-to-inode lookup cache.
  512. // We don't uncache Inodes that are being watched by at least one InodeWatcher.
  513. // FIXME: It would be better to keep a capped number of Inodes around.
  514. // The problem is that they are quite heavy objects, and use a lot of heap memory
  515. // for their (child name lookup) and (block list) caches.
  516. Vector<InodeIndex> unused_inodes;
  517. for (auto& it : m_inode_cache) {
  518. if (it.value->ref_count() != 1)
  519. continue;
  520. if (it.value->has_watchers())
  521. continue;
  522. unused_inodes.append(it.key);
  523. }
  524. for (auto index : unused_inodes)
  525. uncache_inode(index);
  526. }
  527. Ext2FSInode::Ext2FSInode(Ext2FS& fs, InodeIndex index)
  528. : Inode(fs, index)
  529. {
  530. }
  531. Ext2FSInode::~Ext2FSInode()
  532. {
  533. if (m_raw_inode.i_links_count == 0)
  534. fs().free_inode(*this);
  535. }
  536. InodeMetadata Ext2FSInode::metadata() const
  537. {
  538. LOCKER(m_lock);
  539. InodeMetadata metadata;
  540. metadata.inode = identifier();
  541. metadata.size = m_raw_inode.i_size;
  542. metadata.mode = m_raw_inode.i_mode;
  543. metadata.uid = m_raw_inode.i_uid;
  544. metadata.gid = m_raw_inode.i_gid;
  545. metadata.link_count = m_raw_inode.i_links_count;
  546. metadata.atime = m_raw_inode.i_atime;
  547. metadata.ctime = m_raw_inode.i_ctime;
  548. metadata.mtime = m_raw_inode.i_mtime;
  549. metadata.dtime = m_raw_inode.i_dtime;
  550. metadata.block_size = fs().block_size();
  551. metadata.block_count = m_raw_inode.i_blocks;
  552. if (Kernel::is_character_device(m_raw_inode.i_mode) || Kernel::is_block_device(m_raw_inode.i_mode)) {
  553. unsigned dev = m_raw_inode.i_block[0];
  554. if (!dev)
  555. dev = m_raw_inode.i_block[1];
  556. metadata.major_device = (dev & 0xfff00) >> 8;
  557. metadata.minor_device = (dev & 0xff) | ((dev >> 12) & 0xfff00);
  558. }
  559. return metadata;
  560. }
  561. void Ext2FSInode::flush_metadata()
  562. {
  563. LOCKER(m_lock);
  564. dbgln_if(EXT2_DEBUG, "Ext2FS: flush_metadata for inode {}", index());
  565. fs().write_ext2_inode(index(), m_raw_inode);
  566. if (is_directory()) {
  567. // Unless we're about to go away permanently, invalidate the lookup cache.
  568. if (m_raw_inode.i_links_count != 0) {
  569. // FIXME: This invalidation is way too hardcore. It's sad to throw away the whole cache.
  570. m_lookup_cache.clear();
  571. }
  572. }
  573. set_metadata_dirty(false);
  574. }
  575. RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
  576. {
  577. LOCKER(m_lock);
  578. ASSERT(inode.fsid() == fsid());
  579. {
  580. auto it = m_inode_cache.find(inode.index());
  581. if (it != m_inode_cache.end())
  582. return (*it).value;
  583. }
  584. if (!get_inode_allocation_state(inode.index())) {
  585. m_inode_cache.set(inode.index(), nullptr);
  586. return nullptr;
  587. }
  588. BlockIndex block_index;
  589. unsigned offset;
  590. if (!find_block_containing_inode(inode.index(), block_index, offset))
  591. return {};
  592. auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
  593. auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode));
  594. auto result = read_block(block_index, &buffer, sizeof(ext2_inode), offset);
  595. if (result.is_error()) {
  596. // FIXME: Propagate the actual error.
  597. return nullptr;
  598. }
  599. m_inode_cache.set(inode.index(), new_inode);
  600. return new_inode;
  601. }
  602. ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  603. {
  604. Locker inode_locker(m_lock);
  605. ASSERT(offset >= 0);
  606. if (m_raw_inode.i_size == 0)
  607. return 0;
  608. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  609. // This avoids wasting an entire block on short links. (Most links are short.)
  610. if (is_symlink() && size() < max_inline_symlink_length) {
  611. ASSERT(offset == 0);
  612. ssize_t nread = min((off_t)size() - offset, static_cast<off_t>(count));
  613. if (!buffer.write(((const u8*)m_raw_inode.i_block) + offset, (size_t)nread))
  614. return -EFAULT;
  615. return nread;
  616. }
  617. Locker fs_locker(fs().m_lock);
  618. if (m_block_list.is_empty())
  619. m_block_list = fs().block_list_for_inode(m_raw_inode);
  620. if (m_block_list.is_empty()) {
  621. dmesgln("Ext2FS: read_bytes: empty block list for inode {}", index());
  622. return -EIO;
  623. }
  624. bool allow_cache = !description || !description->is_direct();
  625. const int block_size = fs().block_size();
  626. size_t first_block_logical_index = offset / block_size;
  627. size_t last_block_logical_index = (offset + count) / block_size;
  628. if (last_block_logical_index >= m_block_list.size())
  629. last_block_logical_index = m_block_list.size() - 1;
  630. int offset_into_first_block = offset % block_size;
  631. ssize_t nread = 0;
  632. size_t remaining_count = min((off_t)count, (off_t)size() - offset);
  633. dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Reading up to {} bytes, {} bytes into inode {} to {}", count, offset, index(), buffer.user_or_kernel_ptr());
  634. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  635. auto block_index = m_block_list[bi];
  636. ASSERT(block_index.value());
  637. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  638. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  639. auto buffer_offset = buffer.offset(nread);
  640. int err = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
  641. if (err < 0) {
  642. dmesgln("Ext2FS: read_bytes: read_block({}) failed (bi: {})", block_index.value(), bi);
  643. return err;
  644. }
  645. remaining_count -= num_bytes_to_copy;
  646. nread += num_bytes_to_copy;
  647. }
  648. return nread;
  649. }
  650. KResult Ext2FSInode::resize(u64 new_size)
  651. {
  652. u64 old_size = size();
  653. if (old_size == new_size)
  654. return KSuccess;
  655. u64 block_size = fs().block_size();
  656. size_t blocks_needed_before = ceil_div(old_size, block_size);
  657. size_t blocks_needed_after = ceil_div(new_size, block_size);
  658. if constexpr (EXT2_DEBUG) {
  659. dbgln("Ext2FSInode::resize(): blocks needed before (size was {}): {}", old_size, blocks_needed_before);
  660. dbgln("Ext2FSInode::resize(): blocks needed after (size is {}): {}", new_size, blocks_needed_after);
  661. }
  662. if (blocks_needed_after > blocks_needed_before) {
  663. u32 additional_blocks_needed = blocks_needed_after - blocks_needed_before;
  664. if (additional_blocks_needed > fs().super_block().s_free_blocks_count)
  665. return ENOSPC;
  666. }
  667. Vector<Ext2FS::BlockIndex> block_list;
  668. if (!m_block_list.is_empty())
  669. block_list = m_block_list;
  670. else
  671. block_list = fs().block_list_for_inode(m_raw_inode);
  672. if (blocks_needed_after > blocks_needed_before) {
  673. auto new_blocks = fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before);
  674. block_list.append(move(new_blocks));
  675. } else if (blocks_needed_after < blocks_needed_before) {
  676. if constexpr (EXT2_DEBUG) {
  677. dbgln("Ext2FS: Shrinking inode {}. Old block list is {} entries:", index(), block_list.size());
  678. for (auto block_index : block_list) {
  679. dbgln(" # {}", block_index);
  680. }
  681. }
  682. while (block_list.size() != blocks_needed_after) {
  683. auto block_index = block_list.take_last();
  684. if (block_index.value())
  685. fs().set_block_allocation_state(block_index, false);
  686. }
  687. }
  688. auto result = fs().write_block_list_for_inode(index(), m_raw_inode, block_list);
  689. if (result.is_error())
  690. return result;
  691. m_raw_inode.i_size = new_size;
  692. set_metadata_dirty(true);
  693. m_block_list = move(block_list);
  694. if (new_size > old_size) {
  695. // If we're growing the inode, make sure we zero out all the new space.
  696. // FIXME: There are definitely more efficient ways to achieve this.
  697. size_t bytes_to_clear = new_size - old_size;
  698. size_t clear_from = old_size;
  699. u8 zero_buffer[PAGE_SIZE];
  700. memset(zero_buffer, 0, sizeof(zero_buffer));
  701. while (bytes_to_clear) {
  702. auto nwritten = write_bytes(clear_from, min(sizeof(zero_buffer), bytes_to_clear), UserOrKernelBuffer::for_kernel_buffer(zero_buffer), nullptr);
  703. if (nwritten < 0)
  704. return KResult((ErrnoCode)-nwritten);
  705. ASSERT(nwritten != 0);
  706. bytes_to_clear -= nwritten;
  707. clear_from += nwritten;
  708. }
  709. }
  710. return KSuccess;
  711. }
  712. ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& data, FileDescription* description)
  713. {
  714. ASSERT(offset >= 0);
  715. ASSERT(count >= 0);
  716. Locker inode_locker(m_lock);
  717. Locker fs_locker(fs().m_lock);
  718. auto result = prepare_to_write_data();
  719. if (result.is_error())
  720. return result;
  721. if (is_symlink()) {
  722. ASSERT(offset == 0);
  723. if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) {
  724. dbgln_if(EXT2_DEBUG, "Ext2FS: write_bytes poking into i_block array for inline symlink '{}' ({} bytes)", data.copy_into_string(count), count);
  725. if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count))
  726. return -EFAULT;
  727. if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size)
  728. m_raw_inode.i_size = offset + count;
  729. set_metadata_dirty(true);
  730. return count;
  731. }
  732. }
  733. bool allow_cache = !description || !description->is_direct();
  734. const size_t block_size = fs().block_size();
  735. u64 old_size = size();
  736. u64 new_size = max(static_cast<u64>(offset) + count, (u64)size());
  737. auto resize_result = resize(new_size);
  738. if (resize_result.is_error())
  739. return resize_result;
  740. if (m_block_list.is_empty())
  741. m_block_list = fs().block_list_for_inode(m_raw_inode);
  742. if (m_block_list.is_empty()) {
  743. dbgln("Ext2FSInode::write_bytes(): empty block list for inode {}", index());
  744. return -EIO;
  745. }
  746. size_t first_block_logical_index = offset / block_size;
  747. size_t last_block_logical_index = (offset + count) / block_size;
  748. if (last_block_logical_index >= m_block_list.size())
  749. last_block_logical_index = m_block_list.size() - 1;
  750. size_t offset_into_first_block = offset % block_size;
  751. ssize_t nwritten = 0;
  752. size_t remaining_count = min((off_t)count, (off_t)new_size - offset);
  753. dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Writing {} bytes, {} bytes into inode {} from {}", count, offset, index(), data.user_or_kernel_ptr());
  754. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  755. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  756. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  757. dbgln_if(EXT2_DEBUG, "Ext2FS: Writing block {} (offset_into_block: {})", m_block_list[bi], offset_into_block);
  758. result = fs().write_block(m_block_list[bi], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache);
  759. if (result.is_error()) {
  760. dbgln("Ext2FS: write_block({}) failed (bi: {})", m_block_list[bi], bi);
  761. return result;
  762. }
  763. remaining_count -= num_bytes_to_copy;
  764. nwritten += num_bytes_to_copy;
  765. }
  766. dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: After write, i_size={}, i_blocks={} ({} blocks in list)", m_raw_inode.i_size, m_raw_inode.i_blocks, m_block_list.size());
  767. if (old_size != new_size)
  768. inode_size_changed(old_size, new_size);
  769. inode_contents_changed(offset, count, data);
  770. return nwritten;
  771. }
  772. u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
  773. {
  774. switch (entry.file_type) {
  775. case EXT2_FT_REG_FILE:
  776. return DT_REG;
  777. case EXT2_FT_DIR:
  778. return DT_DIR;
  779. case EXT2_FT_CHRDEV:
  780. return DT_CHR;
  781. case EXT2_FT_BLKDEV:
  782. return DT_BLK;
  783. case EXT2_FT_FIFO:
  784. return DT_FIFO;
  785. case EXT2_FT_SOCK:
  786. return DT_SOCK;
  787. case EXT2_FT_SYMLINK:
  788. return DT_LNK;
  789. default:
  790. return DT_UNKNOWN;
  791. }
  792. }
  793. KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  794. {
  795. LOCKER(m_lock);
  796. ASSERT(is_directory());
  797. dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Traversing as directory: {}", index());
  798. auto buffer_or = read_entire();
  799. if (buffer_or.is_error())
  800. return buffer_or.error();
  801. auto& buffer = *buffer_or.value();
  802. auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.data());
  803. while (entry < buffer.end_pointer()) {
  804. if (entry->inode != 0) {
  805. dbgln_if(EXT2_DEBUG, "Ext2Inode::traverse_as_directory: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry->inode, entry->name_len, entry->rec_len, entry->file_type, StringView(entry->name, entry->name_len));
  806. if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, entry->file_type }))
  807. break;
  808. }
  809. entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
  810. }
  811. return KSuccess;
  812. }
  813. KResult Ext2FSInode::write_directory(const Vector<Ext2FSDirectoryEntry>& entries)
  814. {
  815. LOCKER(m_lock);
  816. int directory_size = 0;
  817. for (auto& entry : entries)
  818. directory_size += EXT2_DIR_REC_LEN(entry.name.length());
  819. auto block_size = fs().block_size();
  820. int blocks_needed = ceil_div(static_cast<size_t>(directory_size), block_size);
  821. int occupied_size = blocks_needed * block_size;
  822. dbgln_if(EXT2_DEBUG, "Ext2FS: New directory inode {} contents to write (size {}, occupied {}):", index(), directory_size, occupied_size);
  823. auto directory_data = ByteBuffer::create_uninitialized(occupied_size);
  824. OutputMemoryStream stream { directory_data };
  825. for (size_t i = 0; i < entries.size(); ++i) {
  826. auto& entry = entries[i];
  827. int record_length = EXT2_DIR_REC_LEN(entry.name.length());
  828. if (i == entries.size() - 1)
  829. record_length += occupied_size - directory_size;
  830. dbgln_if(EXT2_DEBUG, "* Inode: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry.inode_index, u16(entry.name.length()), u16(record_length), u8(entry.file_type), entry.name);
  831. stream << u32(entry.inode_index.value());
  832. stream << u16(record_length);
  833. stream << u8(entry.name.length());
  834. stream << u8(entry.file_type);
  835. stream << entry.name.bytes();
  836. int padding = record_length - entry.name.length() - 8;
  837. for (int j = 0; j < padding; ++j)
  838. stream << u8(0);
  839. }
  840. stream.fill_to_end(0);
  841. auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
  842. ssize_t nwritten = write_bytes(0, stream.size(), buffer, nullptr);
  843. if (nwritten < 0)
  844. return KResult((ErrnoCode)-nwritten);
  845. set_metadata_dirty(true);
  846. if (static_cast<size_t>(nwritten) != directory_data.size())
  847. return EIO;
  848. return KSuccess;
  849. }
  850. KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  851. {
  852. if (::is_directory(mode))
  853. return fs().create_directory(*this, name, mode, uid, gid);
  854. return fs().create_inode(*this, name, mode, dev, uid, gid);
  855. }
  856. KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode)
  857. {
  858. LOCKER(m_lock);
  859. ASSERT(is_directory());
  860. if (name.length() > EXT2_NAME_LEN)
  861. return ENAMETOOLONG;
  862. dbgln_if(EXT2_DEBUG, "Ext2FSInode::add_child: Adding inode {} with name '{}' and mode {:o} to directory {}", child.index(), name, mode, index());
  863. Vector<Ext2FSDirectoryEntry> entries;
  864. bool name_already_exists = false;
  865. KResult result = traverse_as_directory([&](auto& entry) {
  866. if (name == entry.name) {
  867. name_already_exists = true;
  868. return false;
  869. }
  870. entries.append({ entry.name, entry.inode.index(), entry.file_type });
  871. return true;
  872. });
  873. if (result.is_error())
  874. return result;
  875. if (name_already_exists) {
  876. dbgln("Ext2FSInode::add_child: Name '{}' already exists in inode {}", name, index());
  877. return EEXIST;
  878. }
  879. result = child.increment_link_count();
  880. if (result.is_error())
  881. return result;
  882. entries.empend(name, child.index(), to_ext2_file_type(mode));
  883. result = write_directory(entries);
  884. if (result.is_error())
  885. return result;
  886. m_lookup_cache.set(name, child.index());
  887. did_add_child(child.identifier());
  888. return KSuccess;
  889. }
  890. KResult Ext2FSInode::remove_child(const StringView& name)
  891. {
  892. LOCKER(m_lock);
  893. dbgln_if(EXT2_DEBUG, "Ext2FSInode::remove_child('{}') in inode {}", name, index());
  894. ASSERT(is_directory());
  895. auto it = m_lookup_cache.find(name);
  896. if (it == m_lookup_cache.end())
  897. return ENOENT;
  898. auto child_inode_index = (*it).value;
  899. InodeIdentifier child_id { fsid(), child_inode_index };
  900. dbgln_if(EXT2_DEBUG, "Ext2FSInode::remove_child(): Removing '{}' in directory {}", name, index());
  901. Vector<Ext2FSDirectoryEntry> entries;
  902. KResult result = traverse_as_directory([&](auto& entry) {
  903. if (name != entry.name)
  904. entries.append({ entry.name, entry.inode.index(), entry.file_type });
  905. return true;
  906. });
  907. if (result.is_error())
  908. return result;
  909. result = write_directory(entries);
  910. if (result.is_error())
  911. return result;
  912. m_lookup_cache.remove(name);
  913. auto child_inode = fs().get_inode(child_id);
  914. result = child_inode->decrement_link_count();
  915. if (result.is_error())
  916. return result;
  917. did_remove_child(child_id);
  918. return KSuccess;
  919. }
  920. unsigned Ext2FS::inodes_per_block() const
  921. {
  922. return EXT2_INODES_PER_BLOCK(&super_block());
  923. }
  924. unsigned Ext2FS::inodes_per_group() const
  925. {
  926. return EXT2_INODES_PER_GROUP(&super_block());
  927. }
  928. unsigned Ext2FS::inode_size() const
  929. {
  930. return EXT2_INODE_SIZE(&super_block());
  931. }
  932. unsigned Ext2FS::blocks_per_group() const
  933. {
  934. return EXT2_BLOCKS_PER_GROUP(&super_block());
  935. }
  936. bool Ext2FS::write_ext2_inode(InodeIndex inode, const ext2_inode& e2inode)
  937. {
  938. LOCKER(m_lock);
  939. BlockIndex block_index;
  940. unsigned offset;
  941. if (!find_block_containing_inode(inode, block_index, offset))
  942. return false;
  943. auto buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)&e2inode));
  944. return write_block(block_index, buffer, inode_size(), offset) >= 0;
  945. }
  946. auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> Vector<BlockIndex>
  947. {
  948. LOCKER(m_lock);
  949. dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_blocks(preferred group: {}, count {})", preferred_group_index, count);
  950. if (count == 0)
  951. return {};
  952. Vector<BlockIndex> blocks;
  953. dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_blocks:");
  954. blocks.ensure_capacity(count);
  955. auto group_index = preferred_group_index;
  956. if (!group_descriptor(preferred_group_index).bg_free_blocks_count) {
  957. group_index = 1;
  958. }
  959. while (blocks.size() < count) {
  960. bool found_a_group = false;
  961. if (group_descriptor(group_index).bg_free_blocks_count) {
  962. found_a_group = true;
  963. } else {
  964. if (group_index == preferred_group_index)
  965. group_index = 1;
  966. for (; group_index <= m_block_group_count; group_index = GroupIndex { group_index.value() + 1 }) {
  967. if (group_descriptor(group_index).bg_free_blocks_count) {
  968. found_a_group = true;
  969. break;
  970. }
  971. }
  972. }
  973. ASSERT(found_a_group);
  974. auto& bgd = group_descriptor(group_index);
  975. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  976. int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
  977. auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
  978. BlockIndex first_block_in_group = (group_index.value() - 1) * blocks_per_group() + first_block_index().value();
  979. size_t free_region_size = 0;
  980. auto first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
  981. ASSERT(first_unset_bit_index.has_value());
  982. dbgln_if(EXT2_DEBUG, "Ext2FS: allocating free region of size: {} [{}]", free_region_size, group_index);
  983. for (size_t i = 0; i < free_region_size; ++i) {
  984. BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group.value();
  985. set_block_allocation_state(block_index, true);
  986. blocks.unchecked_append(block_index);
  987. dbgln_if(EXT2_DEBUG, " allocated > {}", block_index);
  988. }
  989. }
  990. ASSERT(blocks.size() == count);
  991. return blocks;
  992. }
  993. InodeIndex Ext2FS::find_a_free_inode(GroupIndex preferred_group)
  994. {
  995. LOCKER(m_lock);
  996. dbgln_if(EXT2_DEBUG, "Ext2FS: find_a_free_inode(preferred_group: {})", preferred_group);
  997. GroupIndex group_index;
  998. // FIXME: We shouldn't refuse to allocate an inode if there is no group that can house the whole thing.
  999. // In those cases we should just spread it across multiple groups.
  1000. auto is_suitable_group = [this](auto group_index) {
  1001. auto& bgd = group_descriptor(group_index);
  1002. return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= 1;
  1003. };
  1004. if (preferred_group.value() && is_suitable_group(preferred_group)) {
  1005. group_index = preferred_group;
  1006. } else {
  1007. for (unsigned i = 1; i <= m_block_group_count; ++i) {
  1008. if (is_suitable_group(i))
  1009. group_index = i;
  1010. }
  1011. }
  1012. if (!group_index) {
  1013. dmesgln("Ext2FS: find_a_free_inode: no suitable group found for new inode");
  1014. return 0;
  1015. }
  1016. dbgln_if(EXT2_DEBUG, "Ext2FS: find_a_free_inode: found suitable group [{}] for new inode :^)", group_index);
  1017. auto& bgd = group_descriptor(group_index);
  1018. unsigned inodes_in_group = min(inodes_per_group(), super_block().s_inodes_count);
  1019. InodeIndex first_free_inode_in_group = 0;
  1020. InodeIndex first_inode_in_group = (group_index.value() - 1) * inodes_per_group() + 1;
  1021. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1022. auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
  1023. for (size_t i = 0; i < inode_bitmap.size(); ++i) {
  1024. if (inode_bitmap.get(i))
  1025. continue;
  1026. first_free_inode_in_group = InodeIndex(first_inode_in_group.value() + i);
  1027. break;
  1028. }
  1029. if (!first_free_inode_in_group) {
  1030. klog() << "Ext2FS: first_free_inode_in_group returned no inode, despite bgd claiming there are inodes :(";
  1031. return 0;
  1032. }
  1033. InodeIndex inode = first_free_inode_in_group;
  1034. dbgln_if(EXT2_DEBUG, "Ext2FS: found suitable inode {}", inode);
  1035. ASSERT(get_inode_allocation_state(inode) == false);
  1036. return inode;
  1037. }
  1038. Ext2FS::GroupIndex Ext2FS::group_index_from_block_index(BlockIndex block_index) const
  1039. {
  1040. if (!block_index)
  1041. return 0;
  1042. return (block_index.value() - 1) / blocks_per_group() + 1;
  1043. }
  1044. auto Ext2FS::group_index_from_inode(InodeIndex inode) const -> GroupIndex
  1045. {
  1046. if (!inode)
  1047. return 0;
  1048. return (inode.value() - 1) / inodes_per_group() + 1;
  1049. }
  1050. bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
  1051. {
  1052. LOCKER(m_lock);
  1053. if (index == 0)
  1054. return true;
  1055. auto group_index = group_index_from_inode(index);
  1056. auto& bgd = group_descriptor(group_index);
  1057. unsigned index_in_group = index.value() - ((group_index.value() - 1) * inodes_per_group());
  1058. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1059. auto& cached_bitmap = const_cast<Ext2FS&>(*this).get_bitmap_block(bgd.bg_inode_bitmap);
  1060. return cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1061. }
  1062. bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
  1063. {
  1064. LOCKER(m_lock);
  1065. auto group_index = group_index_from_inode(inode_index);
  1066. auto& bgd = group_descriptor(group_index);
  1067. unsigned index_in_group = inode_index.value() - ((group_index.value() - 1) * inodes_per_group());
  1068. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1069. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1070. bool current_state = cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1071. dbgln_if(EXT2_DEBUG, "Ext2FS: set_inode_allocation_state({}) {} -> {}", inode_index, current_state, new_state);
  1072. if (current_state == new_state) {
  1073. ASSERT_NOT_REACHED();
  1074. return true;
  1075. }
  1076. cached_bitmap.bitmap(inodes_per_group()).set(bit_index, new_state);
  1077. cached_bitmap.dirty = true;
  1078. // Update superblock
  1079. if (new_state)
  1080. --m_super_block.s_free_inodes_count;
  1081. else
  1082. ++m_super_block.s_free_inodes_count;
  1083. m_super_block_dirty = true;
  1084. // Update BGD
  1085. auto& mutable_bgd = const_cast<ext2_group_desc&>(bgd);
  1086. if (new_state)
  1087. --mutable_bgd.bg_free_inodes_count;
  1088. else
  1089. ++mutable_bgd.bg_free_inodes_count;
  1090. m_block_group_descriptors_dirty = true;
  1091. return true;
  1092. }
  1093. Ext2FS::BlockIndex Ext2FS::first_block_index() const
  1094. {
  1095. return block_size() == 1024 ? 1 : 0;
  1096. }
  1097. Ext2FS::CachedBitmap& Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index)
  1098. {
  1099. for (auto& cached_bitmap : m_cached_bitmaps) {
  1100. if (cached_bitmap->bitmap_block_index == bitmap_block_index)
  1101. return *cached_bitmap;
  1102. }
  1103. auto block = KBuffer::create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block");
  1104. auto buffer = UserOrKernelBuffer::for_kernel_buffer(block.data());
  1105. int err = read_block(bitmap_block_index, &buffer, block_size());
  1106. ASSERT(err >= 0);
  1107. m_cached_bitmaps.append(make<CachedBitmap>(bitmap_block_index, move(block)));
  1108. return *m_cached_bitmaps.last();
  1109. }
  1110. bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
  1111. {
  1112. ASSERT(block_index != 0);
  1113. LOCKER(m_lock);
  1114. auto group_index = group_index_from_block_index(block_index);
  1115. auto& bgd = group_descriptor(group_index);
  1116. unsigned index_in_group = (block_index.value() - first_block_index().value()) - ((group_index.value() - 1) * blocks_per_group());
  1117. unsigned bit_index = index_in_group % blocks_per_group();
  1118. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  1119. bool current_state = cached_bitmap.bitmap(blocks_per_group()).get(bit_index);
  1120. dbgln_if(EXT2_DEBUG, "Ext2FS: block {} state: {} -> {} (in bitmap block {})", block_index, current_state, new_state, bgd.bg_block_bitmap);
  1121. if (current_state == new_state) {
  1122. ASSERT_NOT_REACHED();
  1123. return true;
  1124. }
  1125. cached_bitmap.bitmap(blocks_per_group()).set(bit_index, new_state);
  1126. cached_bitmap.dirty = true;
  1127. // Update superblock
  1128. if (new_state)
  1129. --m_super_block.s_free_blocks_count;
  1130. else
  1131. ++m_super_block.s_free_blocks_count;
  1132. m_super_block_dirty = true;
  1133. // Update BGD
  1134. auto& mutable_bgd = const_cast<ext2_group_desc&>(bgd);
  1135. if (new_state)
  1136. --mutable_bgd.bg_free_blocks_count;
  1137. else
  1138. ++mutable_bgd.bg_free_blocks_count;
  1139. m_block_group_descriptors_dirty = true;
  1140. return true;
  1141. }
  1142. KResult Ext2FS::create_directory(Ext2FSInode& parent_inode, const String& name, mode_t mode, uid_t uid, gid_t gid)
  1143. {
  1144. LOCKER(m_lock);
  1145. ASSERT(is_directory(mode));
  1146. auto inode_or_error = create_inode(parent_inode, name, mode, 0, uid, gid);
  1147. if (inode_or_error.is_error())
  1148. return inode_or_error.error();
  1149. auto& inode = inode_or_error.value();
  1150. dbgln_if(EXT2_DEBUG, "Ext2FS: create_directory: created new directory named '{} with inode {}", name, inode->index());
  1151. Vector<Ext2FSDirectoryEntry> entries;
  1152. entries.empend(".", inode->index(), static_cast<u8>(EXT2_FT_DIR));
  1153. entries.empend("..", parent_inode.index(), static_cast<u8>(EXT2_FT_DIR));
  1154. auto result = static_cast<Ext2FSInode&>(*inode).write_directory(entries);
  1155. if (result.is_error())
  1156. return result;
  1157. result = parent_inode.increment_link_count();
  1158. if (result.is_error())
  1159. return result;
  1160. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
  1161. ++bgd.bg_used_dirs_count;
  1162. m_block_group_descriptors_dirty = true;
  1163. return KSuccess;
  1164. }
  1165. KResultOr<NonnullRefPtr<Inode>> Ext2FS::create_inode(Ext2FSInode& parent_inode, const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  1166. {
  1167. LOCKER(m_lock);
  1168. if (parent_inode.m_raw_inode.i_links_count == 0)
  1169. return ENOENT;
  1170. if (name.length() > EXT2_NAME_LEN)
  1171. return ENAMETOOLONG;
  1172. dbgln_if(EXT2_DEBUG, "Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode.index());
  1173. // NOTE: This doesn't commit the inode allocation just yet!
  1174. auto inode_id = find_a_free_inode();
  1175. if (!inode_id) {
  1176. klog() << "Ext2FS: create_inode: allocate_inode failed";
  1177. return ENOSPC;
  1178. }
  1179. // Looks like we're good, time to update the inode bitmap and group+global inode counters.
  1180. bool success = set_inode_allocation_state(inode_id, true);
  1181. ASSERT(success);
  1182. struct timeval now;
  1183. kgettimeofday(now);
  1184. ext2_inode e2inode;
  1185. memset(&e2inode, 0, sizeof(ext2_inode));
  1186. e2inode.i_mode = mode;
  1187. e2inode.i_uid = uid;
  1188. e2inode.i_gid = gid;
  1189. e2inode.i_size = 0;
  1190. e2inode.i_atime = now.tv_sec;
  1191. e2inode.i_ctime = now.tv_sec;
  1192. e2inode.i_mtime = now.tv_sec;
  1193. e2inode.i_dtime = 0;
  1194. // For directories, add +1 link count for the "." entry in self.
  1195. e2inode.i_links_count = is_directory(mode);
  1196. if (is_character_device(mode))
  1197. e2inode.i_block[0] = dev;
  1198. else if (is_block_device(mode))
  1199. e2inode.i_block[1] = dev;
  1200. dbgln_if(EXT2_DEBUG, "Ext2FS: writing initial metadata for inode {}", inode_id);
  1201. e2inode.i_flags = 0;
  1202. success = write_ext2_inode(inode_id, e2inode);
  1203. ASSERT(success);
  1204. // We might have cached the fact that this inode didn't exist. Wipe the slate.
  1205. m_inode_cache.remove(inode_id);
  1206. auto inode = get_inode({ fsid(), inode_id });
  1207. auto result = parent_inode.add_child(*inode, name, mode);
  1208. if (result.is_error())
  1209. return result;
  1210. return inode.release_nonnull();
  1211. }
  1212. bool Ext2FSInode::populate_lookup_cache() const
  1213. {
  1214. LOCKER(m_lock);
  1215. if (!m_lookup_cache.is_empty())
  1216. return true;
  1217. HashMap<String, InodeIndex> children;
  1218. KResult result = traverse_as_directory([&children](auto& entry) {
  1219. children.set(entry.name, entry.inode.index());
  1220. return true;
  1221. });
  1222. if (!result.is_success())
  1223. return false;
  1224. if (!m_lookup_cache.is_empty())
  1225. return false;
  1226. m_lookup_cache = move(children);
  1227. return true;
  1228. }
  1229. RefPtr<Inode> Ext2FSInode::lookup(StringView name)
  1230. {
  1231. ASSERT(is_directory());
  1232. if (!populate_lookup_cache())
  1233. return {};
  1234. LOCKER(m_lock);
  1235. auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
  1236. if (it != m_lookup_cache.end())
  1237. return fs().get_inode({ fsid(), (*it).value });
  1238. return {};
  1239. }
  1240. void Ext2FSInode::one_ref_left()
  1241. {
  1242. // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
  1243. }
  1244. int Ext2FSInode::set_atime(time_t t)
  1245. {
  1246. LOCKER(m_lock);
  1247. if (fs().is_readonly())
  1248. return -EROFS;
  1249. m_raw_inode.i_atime = t;
  1250. set_metadata_dirty(true);
  1251. return 0;
  1252. }
  1253. int Ext2FSInode::set_ctime(time_t t)
  1254. {
  1255. LOCKER(m_lock);
  1256. if (fs().is_readonly())
  1257. return -EROFS;
  1258. m_raw_inode.i_ctime = t;
  1259. set_metadata_dirty(true);
  1260. return 0;
  1261. }
  1262. int Ext2FSInode::set_mtime(time_t t)
  1263. {
  1264. LOCKER(m_lock);
  1265. if (fs().is_readonly())
  1266. return -EROFS;
  1267. m_raw_inode.i_mtime = t;
  1268. set_metadata_dirty(true);
  1269. return 0;
  1270. }
  1271. KResult Ext2FSInode::increment_link_count()
  1272. {
  1273. LOCKER(m_lock);
  1274. if (fs().is_readonly())
  1275. return EROFS;
  1276. if (m_raw_inode.i_links_count == max_link_count)
  1277. return EMLINK;
  1278. ++m_raw_inode.i_links_count;
  1279. set_metadata_dirty(true);
  1280. return KSuccess;
  1281. }
  1282. KResult Ext2FSInode::decrement_link_count()
  1283. {
  1284. LOCKER(m_lock);
  1285. if (fs().is_readonly())
  1286. return EROFS;
  1287. ASSERT(m_raw_inode.i_links_count);
  1288. --m_raw_inode.i_links_count;
  1289. if (ref_count() == 1 && m_raw_inode.i_links_count == 0)
  1290. fs().uncache_inode(index());
  1291. set_metadata_dirty(true);
  1292. return KSuccess;
  1293. }
  1294. void Ext2FS::uncache_inode(InodeIndex index)
  1295. {
  1296. LOCKER(m_lock);
  1297. m_inode_cache.remove(index);
  1298. }
  1299. KResultOr<size_t> Ext2FSInode::directory_entry_count() const
  1300. {
  1301. ASSERT(is_directory());
  1302. LOCKER(m_lock);
  1303. populate_lookup_cache();
  1304. return m_lookup_cache.size();
  1305. }
  1306. KResult Ext2FSInode::chmod(mode_t mode)
  1307. {
  1308. LOCKER(m_lock);
  1309. if (m_raw_inode.i_mode == mode)
  1310. return KSuccess;
  1311. m_raw_inode.i_mode = mode;
  1312. set_metadata_dirty(true);
  1313. return KSuccess;
  1314. }
  1315. KResult Ext2FSInode::chown(uid_t uid, gid_t gid)
  1316. {
  1317. LOCKER(m_lock);
  1318. if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid)
  1319. return KSuccess;
  1320. m_raw_inode.i_uid = uid;
  1321. m_raw_inode.i_gid = gid;
  1322. set_metadata_dirty(true);
  1323. return KSuccess;
  1324. }
  1325. KResult Ext2FSInode::truncate(u64 size)
  1326. {
  1327. LOCKER(m_lock);
  1328. if (static_cast<u64>(m_raw_inode.i_size) == size)
  1329. return KSuccess;
  1330. auto result = resize(size);
  1331. if (result.is_error())
  1332. return result;
  1333. set_metadata_dirty(true);
  1334. return KSuccess;
  1335. }
  1336. KResultOr<int> Ext2FSInode::get_block_address(int index)
  1337. {
  1338. LOCKER(m_lock);
  1339. if (m_block_list.is_empty())
  1340. m_block_list = fs().block_list_for_inode(m_raw_inode);
  1341. if (index < 0 || (size_t)index >= m_block_list.size())
  1342. return 0;
  1343. return m_block_list[index].value();
  1344. }
  1345. unsigned Ext2FS::total_block_count() const
  1346. {
  1347. LOCKER(m_lock);
  1348. return super_block().s_blocks_count;
  1349. }
  1350. unsigned Ext2FS::free_block_count() const
  1351. {
  1352. LOCKER(m_lock);
  1353. return super_block().s_free_blocks_count;
  1354. }
  1355. unsigned Ext2FS::total_inode_count() const
  1356. {
  1357. LOCKER(m_lock);
  1358. return super_block().s_inodes_count;
  1359. }
  1360. unsigned Ext2FS::free_inode_count() const
  1361. {
  1362. LOCKER(m_lock);
  1363. return super_block().s_free_inodes_count;
  1364. }
  1365. KResult Ext2FS::prepare_to_unmount() const
  1366. {
  1367. LOCKER(m_lock);
  1368. for (auto& it : m_inode_cache) {
  1369. if (it.value->ref_count() > 1)
  1370. return EBUSY;
  1371. }
  1372. m_inode_cache.clear();
  1373. return KSuccess;
  1374. }
  1375. }