Ext2FileSystem.cpp 57 KB

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