Ext2FileSystem.cpp 57 KB

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