Ext2FileSystem.cpp 57 KB

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