Ext2FileSystem.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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::create_with_size(block_size() * blocks_to_read, Region::Access::Read | Region::Access::Write, "Ext2FS: Block group descriptors");
  126. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table.value().data());
  127. read_blocks(first_block_of_bgdt, blocks_to_read, buffer);
  128. #ifdef EXT2_DEBUG
  129. for (unsigned i = 1; i <= m_block_group_count; ++i) {
  130. auto& group = group_descriptor(i);
  131. klog() << "ext2fs: group[" << i << "] { block_bitmap: " << group.bg_block_bitmap << ", inode_bitmap: " << group.bg_inode_bitmap << ", inode_table: " << group.bg_inode_table << " }";
  132. }
  133. #endif
  134. return true;
  135. }
  136. const char* Ext2FS::class_name() const
  137. {
  138. return "Ext2FS";
  139. }
  140. NonnullRefPtr<Inode> Ext2FS::root_inode() const
  141. {
  142. return *get_inode({ fsid(), EXT2_ROOT_INO });
  143. }
  144. bool Ext2FS::find_block_containing_inode(unsigned inode, unsigned& block_index, unsigned& offset) const
  145. {
  146. LOCKER(m_lock);
  147. auto& super_block = this->super_block();
  148. if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&super_block))
  149. return false;
  150. if (inode > super_block.s_inodes_count)
  151. return false;
  152. auto& bgd = group_descriptor(group_index_from_inode(inode));
  153. offset = ((inode - 1) % inodes_per_group()) * inode_size();
  154. block_index = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&super_block));
  155. offset &= block_size() - 1;
  156. return true;
  157. }
  158. Ext2FS::BlockListShape Ext2FS::compute_block_list_shape(unsigned blocks) const
  159. {
  160. BlockListShape shape;
  161. const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  162. unsigned blocks_remaining = blocks;
  163. shape.direct_blocks = min((unsigned)EXT2_NDIR_BLOCKS, blocks_remaining);
  164. blocks_remaining -= shape.direct_blocks;
  165. if (!blocks_remaining)
  166. return shape;
  167. shape.indirect_blocks = min(blocks_remaining, entries_per_block);
  168. blocks_remaining -= shape.indirect_blocks;
  169. shape.meta_blocks += 1;
  170. if (!blocks_remaining)
  171. return shape;
  172. shape.doubly_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block);
  173. blocks_remaining -= shape.doubly_indirect_blocks;
  174. shape.meta_blocks += 1;
  175. shape.meta_blocks += shape.doubly_indirect_blocks / entries_per_block;
  176. if ((shape.doubly_indirect_blocks % entries_per_block) != 0)
  177. shape.meta_blocks += 1;
  178. if (!blocks_remaining)
  179. return shape;
  180. dbg() << "we don't know how to compute tind ext2fs blocks yet!";
  181. ASSERT_NOT_REACHED();
  182. shape.triply_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block * entries_per_block);
  183. blocks_remaining -= shape.triply_indirect_blocks;
  184. if (!blocks_remaining)
  185. return shape;
  186. ASSERT_NOT_REACHED();
  187. return {};
  188. }
  189. bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2inode, const Vector<BlockIndex>& blocks)
  190. {
  191. LOCKER(m_lock);
  192. if (blocks.is_empty()) {
  193. e2inode.i_blocks = 0;
  194. memset(e2inode.i_block, 0, sizeof(e2inode.i_block));
  195. write_ext2_inode(inode_index, e2inode);
  196. return true;
  197. }
  198. // NOTE: There is a mismatch between i_blocks and blocks.size() since i_blocks includes meta blocks and blocks.size() does not.
  199. auto old_block_count = ceil_div(static_cast<size_t>(e2inode.i_size), block_size());
  200. auto old_shape = compute_block_list_shape(old_block_count);
  201. auto new_shape = compute_block_list_shape(blocks.size());
  202. Vector<BlockIndex> new_meta_blocks;
  203. if (new_shape.meta_blocks > old_shape.meta_blocks) {
  204. new_meta_blocks = allocate_blocks(group_index_from_inode(inode_index), new_shape.meta_blocks - old_shape.meta_blocks);
  205. }
  206. e2inode.i_blocks = (blocks.size() + new_shape.meta_blocks) * (block_size() / 512);
  207. bool inode_dirty = false;
  208. unsigned output_block_index = 0;
  209. unsigned remaining_blocks = blocks.size();
  210. for (unsigned i = 0; i < new_shape.direct_blocks; ++i) {
  211. if (e2inode.i_block[i] != blocks[output_block_index])
  212. inode_dirty = true;
  213. e2inode.i_block[i] = blocks[output_block_index];
  214. ++output_block_index;
  215. --remaining_blocks;
  216. }
  217. if (inode_dirty) {
  218. #ifdef EXT2_DEBUG
  219. dbg() << "Ext2FS: Writing " << min((size_t)EXT2_NDIR_BLOCKS, blocks.size()) << " direct block(s) to i_block array of inode " << inode_index;
  220. for (size_t i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i)
  221. dbg() << " + " << blocks[i];
  222. #endif
  223. write_ext2_inode(inode_index, e2inode);
  224. inode_dirty = false;
  225. }
  226. if (!remaining_blocks)
  227. return true;
  228. const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  229. bool ind_block_new = !e2inode.i_block[EXT2_IND_BLOCK];
  230. if (ind_block_new) {
  231. BlockIndex new_indirect_block = new_meta_blocks.take_last();
  232. if (e2inode.i_block[EXT2_IND_BLOCK] != new_indirect_block)
  233. inode_dirty = true;
  234. e2inode.i_block[EXT2_IND_BLOCK] = new_indirect_block;
  235. if (inode_dirty) {
  236. #ifdef EXT2_DEBUG
  237. dbg() << "Ext2FS: Adding the indirect block to i_block array of inode " << inode_index;
  238. #endif
  239. write_ext2_inode(inode_index, e2inode);
  240. inode_dirty = false;
  241. }
  242. }
  243. if (old_shape.indirect_blocks == new_shape.indirect_blocks) {
  244. // No need to update the singly indirect block array.
  245. remaining_blocks -= new_shape.indirect_blocks;
  246. output_block_index += new_shape.indirect_blocks;
  247. } else {
  248. auto block_contents = ByteBuffer::create_uninitialized(block_size());
  249. OutputMemoryStream stream { block_contents };
  250. ASSERT(new_shape.indirect_blocks <= entries_per_block);
  251. for (unsigned i = 0; i < new_shape.indirect_blocks; ++i) {
  252. stream << blocks[output_block_index++];
  253. --remaining_blocks;
  254. }
  255. stream.fill_to_end(0);
  256. auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
  257. int err = write_block(e2inode.i_block[EXT2_IND_BLOCK], buffer, stream.size());
  258. ASSERT(err >= 0);
  259. }
  260. if (!remaining_blocks)
  261. return true;
  262. bool dind_block_dirty = false;
  263. bool dind_block_new = !e2inode.i_block[EXT2_DIND_BLOCK];
  264. if (dind_block_new) {
  265. BlockIndex new_dindirect_block = new_meta_blocks.take_last();
  266. if (e2inode.i_block[EXT2_DIND_BLOCK] != new_dindirect_block)
  267. inode_dirty = true;
  268. e2inode.i_block[EXT2_DIND_BLOCK] = new_dindirect_block;
  269. if (inode_dirty) {
  270. #ifdef EXT2_DEBUG
  271. dbg() << "Ext2FS: Adding the doubly-indirect block to i_block array of inode " << inode_index;
  272. #endif
  273. write_ext2_inode(inode_index, e2inode);
  274. inode_dirty = false;
  275. }
  276. }
  277. if (old_shape.doubly_indirect_blocks == new_shape.doubly_indirect_blocks) {
  278. // No need to update the doubly indirect block data.
  279. remaining_blocks -= new_shape.doubly_indirect_blocks;
  280. output_block_index += new_shape.doubly_indirect_blocks;
  281. } else {
  282. unsigned indirect_block_count = new_shape.doubly_indirect_blocks / entries_per_block;
  283. if ((new_shape.doubly_indirect_blocks % entries_per_block) != 0)
  284. indirect_block_count++;
  285. auto dind_block_contents = ByteBuffer::create_uninitialized(block_size());
  286. if (dind_block_new) {
  287. memset(dind_block_contents.data(), 0, dind_block_contents.size());
  288. dind_block_dirty = true;
  289. } else {
  290. auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
  291. read_block(e2inode.i_block[EXT2_DIND_BLOCK], &buffer, block_size());
  292. }
  293. auto* dind_block_as_pointers = (unsigned*)dind_block_contents.data();
  294. ASSERT(indirect_block_count <= entries_per_block);
  295. for (unsigned i = 0; i < indirect_block_count; ++i) {
  296. bool ind_block_dirty = false;
  297. BlockIndex indirect_block_index = dind_block_as_pointers[i];
  298. bool ind_block_new = !indirect_block_index;
  299. if (ind_block_new) {
  300. indirect_block_index = new_meta_blocks.take_last();
  301. dind_block_as_pointers[i] = indirect_block_index;
  302. dind_block_dirty = true;
  303. }
  304. auto ind_block_contents = ByteBuffer::create_uninitialized(block_size());
  305. if (ind_block_new) {
  306. memset(ind_block_contents.data(), 0, dind_block_contents.size());
  307. ind_block_dirty = true;
  308. } else {
  309. auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
  310. read_block(indirect_block_index, &buffer, block_size());
  311. }
  312. auto* ind_block_as_pointers = (unsigned*)ind_block_contents.data();
  313. unsigned entries_to_write = new_shape.doubly_indirect_blocks - (i * entries_per_block);
  314. if (entries_to_write > entries_per_block)
  315. entries_to_write = entries_per_block;
  316. ASSERT(entries_to_write <= entries_per_block);
  317. for (unsigned j = 0; j < entries_to_write; ++j) {
  318. BlockIndex output_block = blocks[output_block_index++];
  319. if (ind_block_as_pointers[j] != output_block) {
  320. ind_block_as_pointers[j] = output_block;
  321. ind_block_dirty = true;
  322. }
  323. --remaining_blocks;
  324. }
  325. for (unsigned j = entries_to_write; j < entries_per_block; ++j) {
  326. if (ind_block_as_pointers[j] != 0) {
  327. ind_block_as_pointers[j] = 0;
  328. ind_block_dirty = true;
  329. }
  330. }
  331. if (ind_block_dirty) {
  332. auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
  333. int err = write_block(indirect_block_index, buffer, block_size());
  334. ASSERT(err >= 0);
  335. }
  336. }
  337. for (unsigned i = indirect_block_count; i < entries_per_block; ++i) {
  338. if (dind_block_as_pointers[i] != 0) {
  339. dind_block_as_pointers[i] = 0;
  340. dind_block_dirty = true;
  341. }
  342. }
  343. if (dind_block_dirty) {
  344. auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
  345. int err = write_block(e2inode.i_block[EXT2_DIND_BLOCK], buffer, block_size());
  346. ASSERT(err >= 0);
  347. }
  348. }
  349. if (!remaining_blocks)
  350. return true;
  351. // FIXME: Implement!
  352. dbg() << "we don't know how to write tind ext2fs blocks yet!";
  353. ASSERT_NOT_REACHED();
  354. }
  355. Vector<Ext2FS::BlockIndex> Ext2FS::block_list_for_inode(const ext2_inode& e2inode, bool include_block_list_blocks) const
  356. {
  357. auto block_list = block_list_for_inode_impl(e2inode, include_block_list_blocks);
  358. while (!block_list.is_empty() && block_list.last() == 0)
  359. block_list.take_last();
  360. return block_list;
  361. }
  362. Vector<Ext2FS::BlockIndex> Ext2FS::block_list_for_inode_impl(const ext2_inode& e2inode, bool include_block_list_blocks) const
  363. {
  364. LOCKER(m_lock);
  365. unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
  366. unsigned block_count = ceil_div(static_cast<size_t>(e2inode.i_size), block_size());
  367. // If we are handling a symbolic link, the path is stored in the 60 bytes in
  368. // the inode that are used for the 12 direct and 3 indirect block pointers,
  369. // If the path is longer than 60 characters, a block is allocated, and the
  370. // block contains the destination path. The file size corresponds to the
  371. // path length of the destination.
  372. if (is_symlink(e2inode.i_mode) && e2inode.i_blocks == 0)
  373. block_count = 0;
  374. #ifdef EXT2_DEBUG
  375. dbg() << "Ext2FS::block_list_for_inode(): i_size=" << e2inode.i_size << ", i_blocks=" << e2inode.i_blocks << ", block_count=" << block_count;
  376. #endif
  377. unsigned blocks_remaining = block_count;
  378. if (include_block_list_blocks) {
  379. auto shape = compute_block_list_shape(block_count);
  380. blocks_remaining += shape.meta_blocks;
  381. }
  382. Vector<BlockIndex> list;
  383. auto add_block = [&](BlockIndex bi) {
  384. if (blocks_remaining) {
  385. list.append(bi);
  386. --blocks_remaining;
  387. }
  388. };
  389. if (include_block_list_blocks) {
  390. // This seems like an excessive over-estimate but w/e.
  391. list.ensure_capacity(blocks_remaining * 2);
  392. } else {
  393. list.ensure_capacity(blocks_remaining);
  394. }
  395. unsigned direct_count = min(block_count, (unsigned)EXT2_NDIR_BLOCKS);
  396. for (unsigned i = 0; i < direct_count; ++i) {
  397. auto block_index = e2inode.i_block[i];
  398. add_block(block_index);
  399. }
  400. if (!blocks_remaining)
  401. return list;
  402. // Don't need to make copy of add_block, since this capture will only
  403. // be called before block_list_for_inode_impl finishes.
  404. auto process_block_array = [&](unsigned array_block_index, auto&& callback) {
  405. if (include_block_list_blocks)
  406. add_block(array_block_index);
  407. unsigned count = min(blocks_remaining, entries_per_block);
  408. size_t read_size = count * sizeof(__u32);
  409. auto array_block = ByteBuffer::create_uninitialized(read_size);
  410. auto buffer = UserOrKernelBuffer::for_kernel_buffer(array_block.data());
  411. read_block(array_block_index, &buffer, read_size, 0);
  412. ASSERT(array_block);
  413. auto* array = reinterpret_cast<const __u32*>(array_block.data());
  414. for (BlockIndex i = 0; i < count; ++i)
  415. callback(array[i]);
  416. };
  417. process_block_array(e2inode.i_block[EXT2_IND_BLOCK], [&](unsigned block_index) {
  418. add_block(block_index);
  419. });
  420. if (!blocks_remaining)
  421. return list;
  422. process_block_array(e2inode.i_block[EXT2_DIND_BLOCK], [&](unsigned block_index) {
  423. process_block_array(block_index, [&](unsigned block_index2) {
  424. add_block(block_index2);
  425. });
  426. });
  427. if (!blocks_remaining)
  428. return list;
  429. process_block_array(e2inode.i_block[EXT2_TIND_BLOCK], [&](unsigned block_index) {
  430. process_block_array(block_index, [&](unsigned block_index2) {
  431. process_block_array(block_index2, [&](unsigned block_index3) {
  432. add_block(block_index3);
  433. });
  434. });
  435. });
  436. return list;
  437. }
  438. void Ext2FS::free_inode(Ext2FSInode& inode)
  439. {
  440. LOCKER(m_lock);
  441. ASSERT(inode.m_raw_inode.i_links_count == 0);
  442. #ifdef EXT2_DEBUG
  443. dbg() << "Ext2FS: Inode " << inode.identifier() << " has no more links, time to delete!";
  444. #endif
  445. auto block_list = block_list_for_inode(inode.m_raw_inode, true);
  446. for (auto block_index : block_list) {
  447. ASSERT(block_index <= super_block().s_blocks_count);
  448. if (block_index)
  449. set_block_allocation_state(block_index, false);
  450. }
  451. struct timeval now;
  452. kgettimeofday(now);
  453. memset(&inode.m_raw_inode, 0, sizeof(ext2_inode));
  454. inode.m_raw_inode.i_dtime = now.tv_sec;
  455. write_ext2_inode(inode.index(), inode.m_raw_inode);
  456. set_inode_allocation_state(inode.index(), false);
  457. if (inode.is_directory()) {
  458. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode.index())));
  459. --bgd.bg_used_dirs_count;
  460. dbg() << "Ext2FS: Decremented bg_used_dirs_count to " << bgd.bg_used_dirs_count;
  461. m_block_group_descriptors_dirty = true;
  462. }
  463. }
  464. void Ext2FS::flush_block_group_descriptor_table()
  465. {
  466. LOCKER(m_lock);
  467. unsigned blocks_to_write = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
  468. unsigned first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
  469. auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)block_group_descriptors());
  470. write_blocks(first_block_of_bgdt, blocks_to_write, buffer);
  471. }
  472. void Ext2FS::flush_writes()
  473. {
  474. LOCKER(m_lock);
  475. if (m_super_block_dirty) {
  476. flush_super_block();
  477. m_super_block_dirty = false;
  478. }
  479. if (m_block_group_descriptors_dirty) {
  480. flush_block_group_descriptor_table();
  481. m_block_group_descriptors_dirty = false;
  482. }
  483. for (auto& cached_bitmap : m_cached_bitmaps) {
  484. if (cached_bitmap->dirty) {
  485. auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer.data());
  486. write_block(cached_bitmap->bitmap_block_index, buffer, block_size());
  487. cached_bitmap->dirty = false;
  488. #ifdef EXT2_DEBUG
  489. dbg() << "Flushed bitmap block " << cached_bitmap->bitmap_block_index;
  490. #endif
  491. }
  492. }
  493. BlockBasedFS::flush_writes();
  494. // Uncache Inodes that are only kept alive by the index-to-inode lookup cache.
  495. // We don't uncache Inodes that are being watched by at least one InodeWatcher.
  496. // FIXME: It would be better to keep a capped number of Inodes around.
  497. // The problem is that they are quite heavy objects, and use a lot of heap memory
  498. // for their (child name lookup) and (block list) caches.
  499. Vector<InodeIndex> unused_inodes;
  500. for (auto& it : m_inode_cache) {
  501. if (it.value->ref_count() != 1)
  502. continue;
  503. if (it.value->has_watchers())
  504. continue;
  505. unused_inodes.append(it.key);
  506. }
  507. for (auto index : unused_inodes)
  508. uncache_inode(index);
  509. }
  510. Ext2FSInode::Ext2FSInode(Ext2FS& fs, unsigned index)
  511. : Inode(fs, index)
  512. {
  513. }
  514. Ext2FSInode::~Ext2FSInode()
  515. {
  516. if (m_raw_inode.i_links_count == 0)
  517. fs().free_inode(*this);
  518. }
  519. InodeMetadata Ext2FSInode::metadata() const
  520. {
  521. LOCKER(m_lock);
  522. InodeMetadata metadata;
  523. metadata.inode = identifier();
  524. metadata.size = m_raw_inode.i_size;
  525. metadata.mode = m_raw_inode.i_mode;
  526. metadata.uid = m_raw_inode.i_uid;
  527. metadata.gid = m_raw_inode.i_gid;
  528. metadata.link_count = m_raw_inode.i_links_count;
  529. metadata.atime = m_raw_inode.i_atime;
  530. metadata.ctime = m_raw_inode.i_ctime;
  531. metadata.mtime = m_raw_inode.i_mtime;
  532. metadata.dtime = m_raw_inode.i_dtime;
  533. metadata.block_size = fs().block_size();
  534. metadata.block_count = m_raw_inode.i_blocks;
  535. if (Kernel::is_character_device(m_raw_inode.i_mode) || Kernel::is_block_device(m_raw_inode.i_mode)) {
  536. unsigned dev = m_raw_inode.i_block[0];
  537. if (!dev)
  538. dev = m_raw_inode.i_block[1];
  539. metadata.major_device = (dev & 0xfff00) >> 8;
  540. metadata.minor_device = (dev & 0xff) | ((dev >> 12) & 0xfff00);
  541. }
  542. return metadata;
  543. }
  544. void Ext2FSInode::flush_metadata()
  545. {
  546. LOCKER(m_lock);
  547. #ifdef EXT2_DEBUG
  548. dbg() << "Ext2FS: flush_metadata for inode " << identifier();
  549. #endif
  550. fs().write_ext2_inode(index(), m_raw_inode);
  551. if (is_directory()) {
  552. // Unless we're about to go away permanently, invalidate the lookup cache.
  553. if (m_raw_inode.i_links_count != 0) {
  554. // FIXME: This invalidation is way too hardcore. It's sad to throw away the whole cache.
  555. m_lookup_cache.clear();
  556. }
  557. }
  558. set_metadata_dirty(false);
  559. }
  560. RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
  561. {
  562. LOCKER(m_lock);
  563. ASSERT(inode.fsid() == fsid());
  564. {
  565. auto it = m_inode_cache.find(inode.index());
  566. if (it != m_inode_cache.end())
  567. return (*it).value;
  568. }
  569. if (!get_inode_allocation_state(inode.index())) {
  570. m_inode_cache.set(inode.index(), nullptr);
  571. return nullptr;
  572. }
  573. unsigned block_index;
  574. unsigned offset;
  575. if (!find_block_containing_inode(inode.index(), block_index, offset))
  576. return {};
  577. auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
  578. auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode));
  579. read_block(block_index, &buffer, sizeof(ext2_inode), offset);
  580. m_inode_cache.set(inode.index(), new_inode);
  581. return new_inode;
  582. }
  583. ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  584. {
  585. Locker inode_locker(m_lock);
  586. ASSERT(offset >= 0);
  587. if (m_raw_inode.i_size == 0)
  588. return 0;
  589. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  590. // This avoids wasting an entire block on short links. (Most links are short.)
  591. if (is_symlink() && size() < max_inline_symlink_length) {
  592. ASSERT(offset == 0);
  593. ssize_t nread = min((off_t)size() - offset, static_cast<off_t>(count));
  594. if (!buffer.write(((const u8*)m_raw_inode.i_block) + offset, (size_t)nread))
  595. return -EFAULT;
  596. return nread;
  597. }
  598. Locker fs_locker(fs().m_lock);
  599. if (m_block_list.is_empty())
  600. m_block_list = fs().block_list_for_inode(m_raw_inode);
  601. if (m_block_list.is_empty()) {
  602. klog() << "ext2fs: read_bytes: empty block list for inode " << index();
  603. return -EIO;
  604. }
  605. bool allow_cache = !description || !description->is_direct();
  606. const int block_size = fs().block_size();
  607. size_t first_block_logical_index = offset / block_size;
  608. size_t last_block_logical_index = (offset + count) / block_size;
  609. if (last_block_logical_index >= m_block_list.size())
  610. last_block_logical_index = m_block_list.size() - 1;
  611. int offset_into_first_block = offset % block_size;
  612. ssize_t nread = 0;
  613. size_t remaining_count = min((off_t)count, (off_t)size() - offset);
  614. #ifdef EXT2_VERY_DEBUG
  615. dbg() << "Ext2FS: Reading up to " << count << " bytes " << offset << " bytes into inode " << identifier() << " to " << buffer.user_or_kernel_ptr();
  616. #endif
  617. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  618. auto block_index = m_block_list[bi];
  619. ASSERT(block_index);
  620. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  621. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  622. auto buffer_offset = buffer.offset(nread);
  623. int err = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
  624. if (err < 0) {
  625. klog() << "ext2fs: read_bytes: read_block(" << block_index << ") failed (lbi: " << bi << ")";
  626. return err;
  627. }
  628. remaining_count -= num_bytes_to_copy;
  629. nread += num_bytes_to_copy;
  630. }
  631. return nread;
  632. }
  633. KResult Ext2FSInode::resize(u64 new_size)
  634. {
  635. u64 old_size = size();
  636. if (old_size == new_size)
  637. return KSuccess;
  638. u64 block_size = fs().block_size();
  639. size_t blocks_needed_before = ceil_div(old_size, block_size);
  640. size_t blocks_needed_after = ceil_div(new_size, block_size);
  641. #ifdef EXT2_DEBUG
  642. dbg() << "Ext2FSInode::resize(): blocks needed before (size was " << old_size << "): " << blocks_needed_before;
  643. dbg() << "Ext2FSInode::resize(): blocks needed after (size is " << new_size << "): " << blocks_needed_after;
  644. #endif
  645. if (blocks_needed_after > blocks_needed_before) {
  646. u32 additional_blocks_needed = blocks_needed_after - blocks_needed_before;
  647. if (additional_blocks_needed > fs().super_block().s_free_blocks_count)
  648. return KResult(-ENOSPC);
  649. }
  650. Vector<Ext2FS::BlockIndex> block_list;
  651. if (!m_block_list.is_empty())
  652. block_list = m_block_list;
  653. else
  654. block_list = fs().block_list_for_inode(m_raw_inode);
  655. if (blocks_needed_after > blocks_needed_before) {
  656. auto new_blocks = fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before);
  657. block_list.append(move(new_blocks));
  658. } else if (blocks_needed_after < blocks_needed_before) {
  659. #ifdef EXT2_DEBUG
  660. dbg() << "Ext2FS: Shrinking inode " << identifier() << ". Old block list is " << block_list.size() << " entries:";
  661. for (auto block_index : block_list) {
  662. dbg() << " # " << block_index;
  663. }
  664. #endif
  665. while (block_list.size() != blocks_needed_after) {
  666. auto block_index = block_list.take_last();
  667. if (block_index)
  668. fs().set_block_allocation_state(block_index, false);
  669. }
  670. }
  671. int err = fs().write_block_list_for_inode(index(), m_raw_inode, block_list);
  672. if (err < 0)
  673. return KResult(err);
  674. m_raw_inode.i_size = new_size;
  675. set_metadata_dirty(true);
  676. m_block_list = move(block_list);
  677. return KSuccess;
  678. }
  679. ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& data, FileDescription* description)
  680. {
  681. ASSERT(offset >= 0);
  682. ASSERT(count >= 0);
  683. Locker inode_locker(m_lock);
  684. Locker fs_locker(fs().m_lock);
  685. auto result = prepare_to_write_data();
  686. if (result.is_error())
  687. return result;
  688. if (is_symlink()) {
  689. ASSERT(offset == 0);
  690. if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) {
  691. #ifdef EXT2_DEBUG
  692. dbg() << "Ext2FS: write_bytes poking into i_block array for inline symlink '" << data.copy_into_string(count) << " ' (" << count << " bytes)";
  693. #endif
  694. if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count))
  695. return -EFAULT;
  696. if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size)
  697. m_raw_inode.i_size = offset + count;
  698. set_metadata_dirty(true);
  699. return count;
  700. }
  701. }
  702. bool allow_cache = !description || !description->is_direct();
  703. const size_t block_size = fs().block_size();
  704. u64 old_size = size();
  705. u64 new_size = max(static_cast<u64>(offset) + count, (u64)size());
  706. auto resize_result = resize(new_size);
  707. if (resize_result.is_error())
  708. return resize_result;
  709. if (m_block_list.is_empty())
  710. m_block_list = fs().block_list_for_inode(m_raw_inode);
  711. if (m_block_list.is_empty()) {
  712. dbg() << "Ext2FSInode::write_bytes(): empty block list for inode " << index();
  713. return -EIO;
  714. }
  715. size_t first_block_logical_index = offset / block_size;
  716. size_t last_block_logical_index = (offset + count) / block_size;
  717. if (last_block_logical_index >= m_block_list.size())
  718. last_block_logical_index = m_block_list.size() - 1;
  719. size_t offset_into_first_block = offset % block_size;
  720. ssize_t nwritten = 0;
  721. size_t remaining_count = min((off_t)count, (off_t)new_size - offset);
  722. #ifdef EXT2_VERY_DEBUG
  723. dbg() << "Ext2FS: Writing " << count << " bytes " << offset << " bytes into inode " << identifier() << " from " << data.user_or_kernel_ptr();
  724. #endif
  725. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  726. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  727. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  728. #ifdef EXT2_VERY_DEBUG
  729. dbg() << "Ext2FS: Writing block " << m_block_list[bi] << " (offset_into_block: " << offset_into_block << ")";
  730. #endif
  731. int err = fs().write_block(m_block_list[bi], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache);
  732. if (err < 0) {
  733. dbg() << "Ext2FS: write_block(" << m_block_list[bi] << ") failed (bi: " << bi << ")";
  734. ASSERT_NOT_REACHED();
  735. return err;
  736. }
  737. remaining_count -= num_bytes_to_copy;
  738. nwritten += num_bytes_to_copy;
  739. }
  740. #ifdef EXT2_VERY_DEBUG
  741. 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)";
  742. #endif
  743. if (old_size != new_size)
  744. inode_size_changed(old_size, new_size);
  745. inode_contents_changed(offset, count, data);
  746. return nwritten;
  747. }
  748. u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
  749. {
  750. switch (entry.file_type) {
  751. case EXT2_FT_REG_FILE:
  752. return DT_REG;
  753. case EXT2_FT_DIR:
  754. return DT_DIR;
  755. case EXT2_FT_CHRDEV:
  756. return DT_CHR;
  757. case EXT2_FT_BLKDEV:
  758. return DT_BLK;
  759. case EXT2_FT_FIFO:
  760. return DT_FIFO;
  761. case EXT2_FT_SOCK:
  762. return DT_SOCK;
  763. case EXT2_FT_SYMLINK:
  764. return DT_LNK;
  765. default:
  766. return DT_UNKNOWN;
  767. }
  768. }
  769. KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  770. {
  771. LOCKER(m_lock);
  772. ASSERT(is_directory());
  773. #ifdef EXT2_VERY_DEBUG
  774. dbg() << "Ext2FS: Traversing as directory: " << identifier();
  775. #endif
  776. auto buffer_or = read_entire();
  777. ASSERT(!buffer_or.is_error());
  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(), EXT2_FT_DIR);
  1193. entries.empend("..", parent_id, 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. void Ext2FSInode::populate_lookup_cache() const
  1274. {
  1275. LOCKER(m_lock);
  1276. if (!m_lookup_cache.is_empty())
  1277. return;
  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. ASSERT(result.is_success());
  1284. if (!m_lookup_cache.is_empty())
  1285. return;
  1286. m_lookup_cache = move(children);
  1287. }
  1288. RefPtr<Inode> Ext2FSInode::lookup(StringView name)
  1289. {
  1290. ASSERT(is_directory());
  1291. populate_lookup_cache();
  1292. LOCKER(m_lock);
  1293. auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
  1294. if (it != m_lookup_cache.end())
  1295. return fs().get_inode({ fsid(), (*it).value });
  1296. return {};
  1297. }
  1298. void Ext2FSInode::one_ref_left()
  1299. {
  1300. // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
  1301. }
  1302. int Ext2FSInode::set_atime(time_t t)
  1303. {
  1304. LOCKER(m_lock);
  1305. if (fs().is_readonly())
  1306. return -EROFS;
  1307. m_raw_inode.i_atime = t;
  1308. set_metadata_dirty(true);
  1309. return 0;
  1310. }
  1311. int Ext2FSInode::set_ctime(time_t t)
  1312. {
  1313. LOCKER(m_lock);
  1314. if (fs().is_readonly())
  1315. return -EROFS;
  1316. m_raw_inode.i_ctime = t;
  1317. set_metadata_dirty(true);
  1318. return 0;
  1319. }
  1320. int Ext2FSInode::set_mtime(time_t t)
  1321. {
  1322. LOCKER(m_lock);
  1323. if (fs().is_readonly())
  1324. return -EROFS;
  1325. m_raw_inode.i_mtime = t;
  1326. set_metadata_dirty(true);
  1327. return 0;
  1328. }
  1329. KResult Ext2FSInode::increment_link_count()
  1330. {
  1331. LOCKER(m_lock);
  1332. if (fs().is_readonly())
  1333. return KResult(-EROFS);
  1334. if (m_raw_inode.i_links_count == max_link_count)
  1335. return KResult(-EMLINK);
  1336. ++m_raw_inode.i_links_count;
  1337. set_metadata_dirty(true);
  1338. return KSuccess;
  1339. }
  1340. KResult Ext2FSInode::decrement_link_count()
  1341. {
  1342. LOCKER(m_lock);
  1343. if (fs().is_readonly())
  1344. return KResult(-EROFS);
  1345. ASSERT(m_raw_inode.i_links_count);
  1346. --m_raw_inode.i_links_count;
  1347. if (ref_count() == 1 && m_raw_inode.i_links_count == 0)
  1348. fs().uncache_inode(index());
  1349. set_metadata_dirty(true);
  1350. return KSuccess;
  1351. }
  1352. void Ext2FS::uncache_inode(InodeIndex index)
  1353. {
  1354. LOCKER(m_lock);
  1355. m_inode_cache.remove(index);
  1356. }
  1357. KResultOr<size_t> Ext2FSInode::directory_entry_count() const
  1358. {
  1359. ASSERT(is_directory());
  1360. LOCKER(m_lock);
  1361. populate_lookup_cache();
  1362. return m_lookup_cache.size();
  1363. }
  1364. KResult Ext2FSInode::chmod(mode_t mode)
  1365. {
  1366. LOCKER(m_lock);
  1367. if (m_raw_inode.i_mode == mode)
  1368. return KSuccess;
  1369. m_raw_inode.i_mode = mode;
  1370. set_metadata_dirty(true);
  1371. return KSuccess;
  1372. }
  1373. KResult Ext2FSInode::chown(uid_t uid, gid_t gid)
  1374. {
  1375. LOCKER(m_lock);
  1376. if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid)
  1377. return KSuccess;
  1378. m_raw_inode.i_uid = uid;
  1379. m_raw_inode.i_gid = gid;
  1380. set_metadata_dirty(true);
  1381. return KSuccess;
  1382. }
  1383. KResult Ext2FSInode::truncate(u64 size)
  1384. {
  1385. LOCKER(m_lock);
  1386. if (static_cast<u64>(m_raw_inode.i_size) == size)
  1387. return KSuccess;
  1388. auto result = resize(size);
  1389. if (result.is_error())
  1390. return result;
  1391. set_metadata_dirty(true);
  1392. return KSuccess;
  1393. }
  1394. unsigned Ext2FS::total_block_count() const
  1395. {
  1396. LOCKER(m_lock);
  1397. return super_block().s_blocks_count;
  1398. }
  1399. unsigned Ext2FS::free_block_count() const
  1400. {
  1401. LOCKER(m_lock);
  1402. return super_block().s_free_blocks_count;
  1403. }
  1404. unsigned Ext2FS::total_inode_count() const
  1405. {
  1406. LOCKER(m_lock);
  1407. return super_block().s_inodes_count;
  1408. }
  1409. unsigned Ext2FS::free_inode_count() const
  1410. {
  1411. LOCKER(m_lock);
  1412. return super_block().s_free_inodes_count;
  1413. }
  1414. KResult Ext2FS::prepare_to_unmount() const
  1415. {
  1416. LOCKER(m_lock);
  1417. for (auto& it : m_inode_cache) {
  1418. if (it.value->ref_count() > 1)
  1419. return KResult(-EBUSY);
  1420. }
  1421. m_inode_cache.clear();
  1422. return KSuccess;
  1423. }
  1424. }