Ext2FileSystem.cpp 57 KB

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