Ext2FileSystem.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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. dbgln("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. dbgln("Ext2FS: Writing {} direct block(s) to i_block array of inode {}", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()), inode_index);
  224. for (size_t i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i)
  225. dbgln(" + {}", 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. dbgln("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. dbgln("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. dbgln("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. dbgln("Ext2FS::block_list_for_inode(): i_size={}, i_blocks={}, block_count={}", e2inode.i_size, e2inode.i_blocks, 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. dbgln("Ext2FS: Inode {} has no more links, time to delete!", inode.index());
  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. dbgln("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. dbgln("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. dbgln("Ext2FS: flush_metadata for inode {}", index());
  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. dbgln("Ext2FS: Reading up to {} bytes, {} bytes into inode {} to {}", count, offset, index(), 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. dbgln("Ext2FSInode::resize(): blocks needed before (size was {}): {}", old_size, blocks_needed_before);
  644. dbgln("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. dbgln("Ext2FS: Shrinking inode {}. Old block list is {} entries:", index(), block_list.size());
  662. for (auto block_index : block_list) {
  663. dbgln(" # {}", 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. if (new_size > old_size) {
  679. // If we're growing the inode, make sure we zero out all the new space.
  680. // FIXME: There are definitely more efficient ways to achieve this.
  681. size_t bytes_to_clear = new_size - old_size;
  682. size_t clear_from = old_size;
  683. u8 zero_buffer[PAGE_SIZE];
  684. memset(zero_buffer, 0, sizeof(zero_buffer));
  685. while (bytes_to_clear) {
  686. auto nwritten = write_bytes(clear_from, min(sizeof(zero_buffer), bytes_to_clear), UserOrKernelBuffer::for_kernel_buffer(zero_buffer), nullptr);
  687. if (nwritten < 0)
  688. return KResult(-nwritten);
  689. ASSERT(nwritten != 0);
  690. bytes_to_clear -= nwritten;
  691. clear_from += nwritten;
  692. }
  693. }
  694. return KSuccess;
  695. }
  696. ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& data, FileDescription* description)
  697. {
  698. ASSERT(offset >= 0);
  699. ASSERT(count >= 0);
  700. Locker inode_locker(m_lock);
  701. Locker fs_locker(fs().m_lock);
  702. auto result = prepare_to_write_data();
  703. if (result.is_error())
  704. return result;
  705. if (is_symlink()) {
  706. ASSERT(offset == 0);
  707. if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) {
  708. #ifdef EXT2_DEBUG
  709. dbgln("Ext2FS: write_bytes poking into i_block array for inline symlink '{}' ({} bytes)", data.copy_into_string(count), count);
  710. #endif
  711. if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count))
  712. return -EFAULT;
  713. if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size)
  714. m_raw_inode.i_size = offset + count;
  715. set_metadata_dirty(true);
  716. return count;
  717. }
  718. }
  719. bool allow_cache = !description || !description->is_direct();
  720. const size_t block_size = fs().block_size();
  721. u64 old_size = size();
  722. u64 new_size = max(static_cast<u64>(offset) + count, (u64)size());
  723. auto resize_result = resize(new_size);
  724. if (resize_result.is_error())
  725. return resize_result;
  726. if (m_block_list.is_empty())
  727. m_block_list = fs().block_list_for_inode(m_raw_inode);
  728. if (m_block_list.is_empty()) {
  729. dbgln("Ext2FSInode::write_bytes(): empty block list for inode {}", index());
  730. return -EIO;
  731. }
  732. size_t first_block_logical_index = offset / block_size;
  733. size_t last_block_logical_index = (offset + count) / block_size;
  734. if (last_block_logical_index >= m_block_list.size())
  735. last_block_logical_index = m_block_list.size() - 1;
  736. size_t offset_into_first_block = offset % block_size;
  737. ssize_t nwritten = 0;
  738. size_t remaining_count = min((off_t)count, (off_t)new_size - offset);
  739. #ifdef EXT2_VERY_DEBUG
  740. dbgln("Ext2FS: Writing {} bytes, {} bytes into inode {} from {}", count, offset, index(), data.user_or_kernel_ptr());
  741. #endif
  742. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  743. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  744. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  745. #ifdef EXT2_VERY_DEBUG
  746. dbgln("Ext2FS: Writing block {} (offset_into_block: {})", m_block_list[bi], offset_into_block);
  747. #endif
  748. int err = fs().write_block(m_block_list[bi], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache);
  749. if (err < 0) {
  750. dbgln("Ext2FS: write_block({}) failed (bi: {})", m_block_list[bi], bi);
  751. ASSERT_NOT_REACHED();
  752. return err;
  753. }
  754. remaining_count -= num_bytes_to_copy;
  755. nwritten += num_bytes_to_copy;
  756. }
  757. #ifdef EXT2_VERY_DEBUG
  758. dbgln("Ext2FS: After write, i_size={}, i_blocks={} ({} blocks in list)", m_raw_inode.i_size, m_raw_inode.i_blocks, m_block_list.size());
  759. #endif
  760. if (old_size != new_size)
  761. inode_size_changed(old_size, new_size);
  762. inode_contents_changed(offset, count, data);
  763. return nwritten;
  764. }
  765. u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
  766. {
  767. switch (entry.file_type) {
  768. case EXT2_FT_REG_FILE:
  769. return DT_REG;
  770. case EXT2_FT_DIR:
  771. return DT_DIR;
  772. case EXT2_FT_CHRDEV:
  773. return DT_CHR;
  774. case EXT2_FT_BLKDEV:
  775. return DT_BLK;
  776. case EXT2_FT_FIFO:
  777. return DT_FIFO;
  778. case EXT2_FT_SOCK:
  779. return DT_SOCK;
  780. case EXT2_FT_SYMLINK:
  781. return DT_LNK;
  782. default:
  783. return DT_UNKNOWN;
  784. }
  785. }
  786. KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  787. {
  788. LOCKER(m_lock);
  789. ASSERT(is_directory());
  790. #ifdef EXT2_VERY_DEBUG
  791. dbgln("Ext2FS: Traversing as directory: {}", index());
  792. #endif
  793. auto buffer_or = read_entire();
  794. if (buffer_or.is_error())
  795. return buffer_or.error();
  796. auto& buffer = *buffer_or.value();
  797. auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.data());
  798. while (entry < buffer.end_pointer()) {
  799. if (entry->inode != 0) {
  800. #ifdef EXT2_VERY_DEBUG
  801. dbgln("Ext2Inode::traverse_as_directory: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry->inode, entry->name_len, entry->rec_len, entry->file_type, StringView(entry->name, entry->name_len));
  802. #endif
  803. if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, entry->file_type }))
  804. break;
  805. }
  806. entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
  807. }
  808. return KSuccess;
  809. }
  810. bool Ext2FSInode::write_directory(const Vector<Ext2FSDirectoryEntry>& entries)
  811. {
  812. LOCKER(m_lock);
  813. int directory_size = 0;
  814. for (auto& entry : entries)
  815. directory_size += EXT2_DIR_REC_LEN(entry.name.length());
  816. auto block_size = fs().block_size();
  817. int blocks_needed = ceil_div(static_cast<size_t>(directory_size), block_size);
  818. int occupied_size = blocks_needed * block_size;
  819. #ifdef EXT2_DEBUG
  820. dbgln("Ext2FS: New directory inode {} contents to write (size {}, occupied {}):", index(), directory_size, occupied_size);
  821. #endif
  822. auto directory_data = ByteBuffer::create_uninitialized(occupied_size);
  823. OutputMemoryStream stream { directory_data };
  824. for (size_t i = 0; i < entries.size(); ++i) {
  825. auto& entry = entries[i];
  826. int record_length = EXT2_DIR_REC_LEN(entry.name.length());
  827. if (i == entries.size() - 1)
  828. record_length += occupied_size - directory_size;
  829. #ifdef EXT2_DEBUG
  830. dbgln("* Inode: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry.inode.index(), u16(entry.name.length()), u16(record_length), u8(entry.file_type), entry.name);
  831. #endif
  832. stream << u32(entry.inode.index());
  833. stream << u16(record_length);
  834. stream << u8(entry.name.length());
  835. stream << u8(entry.file_type);
  836. stream << entry.name.bytes();
  837. int padding = record_length - entry.name.length() - 8;
  838. for (int j = 0; j < padding; ++j)
  839. stream << u8(0);
  840. }
  841. stream.fill_to_end(0);
  842. auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
  843. ssize_t nwritten = write_bytes(0, stream.size(), buffer, nullptr);
  844. if (nwritten < 0)
  845. return false;
  846. set_metadata_dirty(true);
  847. return static_cast<size_t>(nwritten) == directory_data.size();
  848. }
  849. KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  850. {
  851. if (mode & S_IFDIR)
  852. return fs().create_directory(identifier(), name, mode, uid, gid);
  853. return fs().create_inode(identifier(), name, mode, 0, dev, uid, gid);
  854. }
  855. KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode)
  856. {
  857. LOCKER(m_lock);
  858. ASSERT(is_directory());
  859. if (name.length() > EXT2_NAME_LEN)
  860. return KResult(-ENAMETOOLONG);
  861. #ifdef EXT2_DEBUG
  862. dbgln("Ext2FSInode::add_child: Adding inode {} with name '{}' and mode {:o} to directory {}", child.index(), name, mode, index());
  863. #endif
  864. Vector<Ext2FSDirectoryEntry> entries;
  865. bool name_already_exists = false;
  866. KResult result = traverse_as_directory([&](auto& entry) {
  867. if (name == entry.name) {
  868. name_already_exists = true;
  869. return false;
  870. }
  871. entries.append({ entry.name, entry.inode, entry.file_type });
  872. return true;
  873. });
  874. if (result.is_error())
  875. return result;
  876. if (name_already_exists) {
  877. dbgln("Ext2FSInode::add_child: Name '{}' already exists in inode {}", name, index());
  878. return KResult(-EEXIST);
  879. }
  880. result = child.increment_link_count();
  881. if (result.is_error())
  882. return result;
  883. entries.empend(name, child.identifier(), to_ext2_file_type(mode));
  884. bool success = write_directory(entries);
  885. if (success)
  886. m_lookup_cache.set(name, child.index());
  887. did_add_child(child.identifier());
  888. return KSuccess;
  889. }
  890. KResult Ext2FSInode::remove_child(const StringView& name)
  891. {
  892. LOCKER(m_lock);
  893. #ifdef EXT2_DEBUG
  894. dbgln("Ext2FSInode::remove_child('{}') in inode {}", name, index());
  895. #endif
  896. ASSERT(is_directory());
  897. auto it = m_lookup_cache.find(name);
  898. if (it == m_lookup_cache.end())
  899. return KResult(-ENOENT);
  900. auto child_inode_index = (*it).value;
  901. InodeIdentifier child_id { fsid(), child_inode_index };
  902. #ifdef EXT2_DEBUG
  903. dbgln("Ext2FSInode::remove_child(): Removing '{}' in directory {}", name, index());
  904. #endif
  905. Vector<Ext2FSDirectoryEntry> entries;
  906. KResult result = traverse_as_directory([&](auto& entry) {
  907. if (name != entry.name)
  908. entries.append({ entry.name, entry.inode, entry.file_type });
  909. return true;
  910. });
  911. if (result.is_error())
  912. return result;
  913. bool success = write_directory(entries);
  914. if (!success) {
  915. // FIXME: Plumb error from write_directory().
  916. return KResult(-EIO);
  917. }
  918. m_lookup_cache.remove(name);
  919. auto child_inode = fs().get_inode(child_id);
  920. result = child_inode->decrement_link_count();
  921. if (result.is_error())
  922. return result;
  923. did_remove_child(child_id);
  924. return KSuccess;
  925. }
  926. unsigned Ext2FS::inodes_per_block() const
  927. {
  928. return EXT2_INODES_PER_BLOCK(&super_block());
  929. }
  930. unsigned Ext2FS::inodes_per_group() const
  931. {
  932. return EXT2_INODES_PER_GROUP(&super_block());
  933. }
  934. unsigned Ext2FS::inode_size() const
  935. {
  936. return EXT2_INODE_SIZE(&super_block());
  937. }
  938. unsigned Ext2FS::blocks_per_group() const
  939. {
  940. return EXT2_BLOCKS_PER_GROUP(&super_block());
  941. }
  942. bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
  943. {
  944. LOCKER(m_lock);
  945. unsigned block_index;
  946. unsigned offset;
  947. if (!find_block_containing_inode(inode, block_index, offset))
  948. return false;
  949. auto buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)&e2inode));
  950. return write_block(block_index, buffer, inode_size(), offset) >= 0;
  951. }
  952. Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count)
  953. {
  954. LOCKER(m_lock);
  955. #ifdef EXT2_DEBUG
  956. dbgln("Ext2FS: allocate_blocks(preferred group: {}, count {})", preferred_group_index, count);
  957. #endif
  958. if (count == 0)
  959. return {};
  960. Vector<BlockIndex> blocks;
  961. #ifdef EXT2_DEBUG
  962. dbgln("Ext2FS: allocate_blocks:");
  963. #endif
  964. blocks.ensure_capacity(count);
  965. GroupIndex group_index = preferred_group_index;
  966. if (!group_descriptor(preferred_group_index).bg_free_blocks_count) {
  967. group_index = 1;
  968. }
  969. while (blocks.size() < count) {
  970. bool found_a_group = false;
  971. if (group_descriptor(group_index).bg_free_blocks_count) {
  972. found_a_group = true;
  973. } else {
  974. if (group_index == preferred_group_index)
  975. group_index = 1;
  976. for (; group_index <= m_block_group_count; ++group_index) {
  977. if (group_descriptor(group_index).bg_free_blocks_count) {
  978. found_a_group = true;
  979. break;
  980. }
  981. }
  982. }
  983. ASSERT(found_a_group);
  984. auto& bgd = group_descriptor(group_index);
  985. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  986. int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
  987. auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
  988. BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
  989. size_t free_region_size = 0;
  990. auto first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
  991. ASSERT(first_unset_bit_index.has_value());
  992. #ifdef EXT2_DEBUG
  993. dbgln("Ext2FS: allocating free region of size: {} [{}]", free_region_size, group_index);
  994. #endif
  995. for (size_t i = 0; i < free_region_size; ++i) {
  996. BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group;
  997. set_block_allocation_state(block_index, true);
  998. blocks.unchecked_append(block_index);
  999. #ifdef EXT2_DEBUG
  1000. dbgln(" allocated > {}", block_index);
  1001. #endif
  1002. }
  1003. }
  1004. ASSERT(blocks.size() == count);
  1005. return blocks;
  1006. }
  1007. unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group, off_t expected_size)
  1008. {
  1009. ASSERT(expected_size >= 0);
  1010. LOCKER(m_lock);
  1011. #ifdef EXT2_DEBUG
  1012. dbgln("Ext2FS: find_a_free_inode(preferred_group: {}, expected_size: {})", preferred_group, expected_size);
  1013. #endif
  1014. unsigned needed_blocks = ceil_div(static_cast<size_t>(expected_size), block_size());
  1015. #ifdef EXT2_DEBUG
  1016. dbgln("Ext2FS: minimum needed blocks: {}", needed_blocks);
  1017. #endif
  1018. unsigned group_index = 0;
  1019. // FIXME: We shouldn't refuse to allocate an inode if there is no group that can house the whole thing.
  1020. // In those cases we should just spread it across multiple groups.
  1021. auto is_suitable_group = [this, needed_blocks](GroupIndex group_index) {
  1022. auto& bgd = group_descriptor(group_index);
  1023. return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= needed_blocks;
  1024. };
  1025. if (preferred_group && is_suitable_group(preferred_group)) {
  1026. group_index = preferred_group;
  1027. } else {
  1028. for (unsigned i = 1; i <= m_block_group_count; ++i) {
  1029. if (is_suitable_group(i))
  1030. group_index = i;
  1031. }
  1032. }
  1033. if (!group_index) {
  1034. klog() << "Ext2FS: find_a_free_inode: no suitable group found for new inode with " << needed_blocks << " blocks needed :(";
  1035. return 0;
  1036. }
  1037. #ifdef EXT2_DEBUG
  1038. dbgln("Ext2FS: find_a_free_inode: found suitable group [{}] for new inode with {} blocks needed :^)", group_index, needed_blocks);
  1039. #endif
  1040. auto& bgd = group_descriptor(group_index);
  1041. unsigned inodes_in_group = min(inodes_per_group(), super_block().s_inodes_count);
  1042. unsigned first_free_inode_in_group = 0;
  1043. unsigned first_inode_in_group = (group_index - 1) * inodes_per_group() + 1;
  1044. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1045. auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
  1046. for (size_t i = 0; i < inode_bitmap.size(); ++i) {
  1047. if (inode_bitmap.get(i))
  1048. continue;
  1049. first_free_inode_in_group = first_inode_in_group + i;
  1050. break;
  1051. }
  1052. if (!first_free_inode_in_group) {
  1053. klog() << "Ext2FS: first_free_inode_in_group returned no inode, despite bgd claiming there are inodes :(";
  1054. return 0;
  1055. }
  1056. unsigned inode = first_free_inode_in_group;
  1057. #ifdef EXT2_DEBUG
  1058. dbgln("Ext2FS: found suitable inode {}", inode);
  1059. #endif
  1060. ASSERT(get_inode_allocation_state(inode) == false);
  1061. return inode;
  1062. }
  1063. Ext2FS::GroupIndex Ext2FS::group_index_from_block_index(BlockIndex block_index) const
  1064. {
  1065. if (!block_index)
  1066. return 0;
  1067. return (block_index - 1) / blocks_per_group() + 1;
  1068. }
  1069. unsigned Ext2FS::group_index_from_inode(unsigned inode) const
  1070. {
  1071. if (!inode)
  1072. return 0;
  1073. return (inode - 1) / inodes_per_group() + 1;
  1074. }
  1075. bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
  1076. {
  1077. LOCKER(m_lock);
  1078. if (index == 0)
  1079. return true;
  1080. unsigned group_index = group_index_from_inode(index);
  1081. auto& bgd = group_descriptor(group_index);
  1082. unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
  1083. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1084. auto& cached_bitmap = const_cast<Ext2FS&>(*this).get_bitmap_block(bgd.bg_inode_bitmap);
  1085. return cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1086. }
  1087. bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
  1088. {
  1089. LOCKER(m_lock);
  1090. unsigned group_index = group_index_from_inode(inode_index);
  1091. auto& bgd = group_descriptor(group_index);
  1092. unsigned index_in_group = inode_index - ((group_index - 1) * inodes_per_group());
  1093. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1094. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1095. bool current_state = cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1096. #ifdef EXT2_DEBUG
  1097. dbgln("Ext2FS: set_inode_allocation_state({}) {} -> {}", inode_index, current_state, new_state);
  1098. #endif
  1099. if (current_state == new_state) {
  1100. ASSERT_NOT_REACHED();
  1101. return true;
  1102. }
  1103. cached_bitmap.bitmap(inodes_per_group()).set(bit_index, new_state);
  1104. cached_bitmap.dirty = true;
  1105. // Update superblock
  1106. if (new_state)
  1107. --m_super_block.s_free_inodes_count;
  1108. else
  1109. ++m_super_block.s_free_inodes_count;
  1110. m_super_block_dirty = true;
  1111. // Update BGD
  1112. auto& mutable_bgd = const_cast<ext2_group_desc&>(bgd);
  1113. if (new_state)
  1114. --mutable_bgd.bg_free_inodes_count;
  1115. else
  1116. ++mutable_bgd.bg_free_inodes_count;
  1117. m_block_group_descriptors_dirty = true;
  1118. return true;
  1119. }
  1120. Ext2FS::BlockIndex Ext2FS::first_block_index() const
  1121. {
  1122. return block_size() == 1024 ? 1 : 0;
  1123. }
  1124. Ext2FS::CachedBitmap& Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index)
  1125. {
  1126. for (auto& cached_bitmap : m_cached_bitmaps) {
  1127. if (cached_bitmap->bitmap_block_index == bitmap_block_index)
  1128. return *cached_bitmap;
  1129. }
  1130. auto block = KBuffer::create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block");
  1131. auto buffer = UserOrKernelBuffer::for_kernel_buffer(block.data());
  1132. int err = read_block(bitmap_block_index, &buffer, block_size());
  1133. ASSERT(err >= 0);
  1134. m_cached_bitmaps.append(make<CachedBitmap>(bitmap_block_index, move(block)));
  1135. return *m_cached_bitmaps.last();
  1136. }
  1137. bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
  1138. {
  1139. ASSERT(block_index != 0);
  1140. LOCKER(m_lock);
  1141. GroupIndex group_index = group_index_from_block_index(block_index);
  1142. auto& bgd = group_descriptor(group_index);
  1143. BlockIndex index_in_group = (block_index - first_block_index()) - ((group_index - 1) * blocks_per_group());
  1144. unsigned bit_index = index_in_group % blocks_per_group();
  1145. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  1146. bool current_state = cached_bitmap.bitmap(blocks_per_group()).get(bit_index);
  1147. #ifdef EXT2_DEBUG
  1148. dbgln("Ext2FS: block {} state: {} -> {} (in bitmap block {})", block_index, current_state, new_state, bgd.bg_block_bitmap);
  1149. #endif
  1150. if (current_state == new_state) {
  1151. ASSERT_NOT_REACHED();
  1152. return true;
  1153. }
  1154. cached_bitmap.bitmap(blocks_per_group()).set(bit_index, new_state);
  1155. cached_bitmap.dirty = true;
  1156. // Update superblock
  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. m_block_group_descriptors_dirty = true;
  1169. return true;
  1170. }
  1171. KResult Ext2FS::create_directory(InodeIdentifier parent_id, const String& name, mode_t mode, uid_t uid, gid_t gid)
  1172. {
  1173. LOCKER(m_lock);
  1174. ASSERT(parent_id.fsid() == fsid());
  1175. // Fix up the mode to definitely be a directory.
  1176. // FIXME: This is a bit on the hackish side.
  1177. mode &= ~0170000;
  1178. mode |= 0040000;
  1179. // NOTE: When creating a new directory, make the size 1 block.
  1180. // There's probably a better strategy here, but this works for now.
  1181. auto inode_or_error = create_inode(parent_id, name, mode, block_size(), 0, uid, gid);
  1182. if (inode_or_error.is_error())
  1183. return inode_or_error.error();
  1184. auto& inode = inode_or_error.value();
  1185. #ifdef EXT2_DEBUG
  1186. dbgln("Ext2FS: create_directory: created new directory named '{} with inode {}", name, inode->index());
  1187. #endif
  1188. Vector<Ext2FSDirectoryEntry> entries;
  1189. entries.empend(".", inode->identifier(), static_cast<u8>(EXT2_FT_DIR));
  1190. entries.empend("..", parent_id, static_cast<u8>(EXT2_FT_DIR));
  1191. bool success = static_cast<Ext2FSInode&>(*inode).write_directory(entries);
  1192. ASSERT(success);
  1193. auto parent_inode = get_inode(parent_id);
  1194. auto result = parent_inode->increment_link_count();
  1195. if (result.is_error())
  1196. return result;
  1197. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
  1198. ++bgd.bg_used_dirs_count;
  1199. m_block_group_descriptors_dirty = true;
  1200. return KSuccess;
  1201. }
  1202. 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)
  1203. {
  1204. LOCKER(m_lock);
  1205. ASSERT(size >= 0);
  1206. ASSERT(parent_id.fsid() == fsid());
  1207. auto parent_inode = get_inode(parent_id);
  1208. ASSERT(parent_inode);
  1209. if (static_cast<const Ext2FSInode&>(*parent_inode).m_raw_inode.i_links_count == 0)
  1210. return KResult(-ENOENT);
  1211. if (name.length() > EXT2_NAME_LEN)
  1212. return KResult(-ENAMETOOLONG);
  1213. #ifdef EXT2_DEBUG
  1214. dbgln("Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode->index());
  1215. #endif
  1216. size_t needed_blocks = ceil_div(static_cast<size_t>(size), block_size());
  1217. if ((size_t)needed_blocks > super_block().s_free_blocks_count) {
  1218. dbgln("Ext2FS: create_inode: not enough free blocks");
  1219. return KResult(-ENOSPC);
  1220. }
  1221. // NOTE: This doesn't commit the inode allocation just yet!
  1222. auto inode_id = find_a_free_inode(0, size);
  1223. if (!inode_id) {
  1224. klog() << "Ext2FS: create_inode: allocate_inode failed";
  1225. return KResult(-ENOSPC);
  1226. }
  1227. auto blocks = allocate_blocks(group_index_from_inode(inode_id), needed_blocks);
  1228. ASSERT(blocks.size() == needed_blocks);
  1229. // Looks like we're good, time to update the inode bitmap and group+global inode counters.
  1230. bool success = set_inode_allocation_state(inode_id, true);
  1231. ASSERT(success);
  1232. struct timeval now;
  1233. kgettimeofday(now);
  1234. ext2_inode e2inode;
  1235. memset(&e2inode, 0, sizeof(ext2_inode));
  1236. e2inode.i_mode = mode;
  1237. e2inode.i_uid = uid;
  1238. e2inode.i_gid = gid;
  1239. e2inode.i_size = size;
  1240. e2inode.i_atime = now.tv_sec;
  1241. e2inode.i_ctime = now.tv_sec;
  1242. e2inode.i_mtime = now.tv_sec;
  1243. e2inode.i_dtime = 0;
  1244. // For directories, add +1 link count for the "." entry in self.
  1245. e2inode.i_links_count = is_directory(mode);
  1246. if (is_character_device(mode))
  1247. e2inode.i_block[0] = dev;
  1248. else if (is_block_device(mode))
  1249. e2inode.i_block[1] = dev;
  1250. success = write_block_list_for_inode(inode_id, e2inode, blocks);
  1251. ASSERT(success);
  1252. #ifdef EXT2_DEBUG
  1253. dbgln("Ext2FS: writing initial metadata for inode {}", inode_id);
  1254. #endif
  1255. e2inode.i_flags = 0;
  1256. success = write_ext2_inode(inode_id, e2inode);
  1257. ASSERT(success);
  1258. // We might have cached the fact that this inode didn't exist. Wipe the slate.
  1259. m_inode_cache.remove(inode_id);
  1260. auto inode = get_inode({ fsid(), inode_id });
  1261. // If we've already computed a block list, no sense in throwing it away.
  1262. static_cast<Ext2FSInode&>(*inode).m_block_list = move(blocks);
  1263. auto result = parent_inode->add_child(*inode, name, mode);
  1264. ASSERT(result.is_success());
  1265. return inode.release_nonnull();
  1266. }
  1267. bool Ext2FSInode::populate_lookup_cache() const
  1268. {
  1269. LOCKER(m_lock);
  1270. if (!m_lookup_cache.is_empty())
  1271. return true;
  1272. HashMap<String, unsigned> children;
  1273. KResult result = traverse_as_directory([&children](auto& entry) {
  1274. children.set(entry.name, entry.inode.index());
  1275. return true;
  1276. });
  1277. if (!result.is_success())
  1278. return false;
  1279. if (!m_lookup_cache.is_empty())
  1280. return false;
  1281. m_lookup_cache = move(children);
  1282. return true;
  1283. }
  1284. RefPtr<Inode> Ext2FSInode::lookup(StringView name)
  1285. {
  1286. ASSERT(is_directory());
  1287. if (!populate_lookup_cache())
  1288. return {};
  1289. LOCKER(m_lock);
  1290. auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
  1291. if (it != m_lookup_cache.end())
  1292. return fs().get_inode({ fsid(), (*it).value });
  1293. return {};
  1294. }
  1295. void Ext2FSInode::one_ref_left()
  1296. {
  1297. // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
  1298. }
  1299. int Ext2FSInode::set_atime(time_t t)
  1300. {
  1301. LOCKER(m_lock);
  1302. if (fs().is_readonly())
  1303. return -EROFS;
  1304. m_raw_inode.i_atime = t;
  1305. set_metadata_dirty(true);
  1306. return 0;
  1307. }
  1308. int Ext2FSInode::set_ctime(time_t t)
  1309. {
  1310. LOCKER(m_lock);
  1311. if (fs().is_readonly())
  1312. return -EROFS;
  1313. m_raw_inode.i_ctime = t;
  1314. set_metadata_dirty(true);
  1315. return 0;
  1316. }
  1317. int Ext2FSInode::set_mtime(time_t t)
  1318. {
  1319. LOCKER(m_lock);
  1320. if (fs().is_readonly())
  1321. return -EROFS;
  1322. m_raw_inode.i_mtime = t;
  1323. set_metadata_dirty(true);
  1324. return 0;
  1325. }
  1326. KResult Ext2FSInode::increment_link_count()
  1327. {
  1328. LOCKER(m_lock);
  1329. if (fs().is_readonly())
  1330. return KResult(-EROFS);
  1331. if (m_raw_inode.i_links_count == max_link_count)
  1332. return KResult(-EMLINK);
  1333. ++m_raw_inode.i_links_count;
  1334. set_metadata_dirty(true);
  1335. return KSuccess;
  1336. }
  1337. KResult Ext2FSInode::decrement_link_count()
  1338. {
  1339. LOCKER(m_lock);
  1340. if (fs().is_readonly())
  1341. return KResult(-EROFS);
  1342. ASSERT(m_raw_inode.i_links_count);
  1343. --m_raw_inode.i_links_count;
  1344. if (ref_count() == 1 && m_raw_inode.i_links_count == 0)
  1345. fs().uncache_inode(index());
  1346. set_metadata_dirty(true);
  1347. return KSuccess;
  1348. }
  1349. void Ext2FS::uncache_inode(InodeIndex index)
  1350. {
  1351. LOCKER(m_lock);
  1352. m_inode_cache.remove(index);
  1353. }
  1354. KResultOr<size_t> Ext2FSInode::directory_entry_count() const
  1355. {
  1356. ASSERT(is_directory());
  1357. LOCKER(m_lock);
  1358. populate_lookup_cache();
  1359. return m_lookup_cache.size();
  1360. }
  1361. KResult Ext2FSInode::chmod(mode_t mode)
  1362. {
  1363. LOCKER(m_lock);
  1364. if (m_raw_inode.i_mode == mode)
  1365. return KSuccess;
  1366. m_raw_inode.i_mode = mode;
  1367. set_metadata_dirty(true);
  1368. return KSuccess;
  1369. }
  1370. KResult Ext2FSInode::chown(uid_t uid, gid_t gid)
  1371. {
  1372. LOCKER(m_lock);
  1373. if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid)
  1374. return KSuccess;
  1375. m_raw_inode.i_uid = uid;
  1376. m_raw_inode.i_gid = gid;
  1377. set_metadata_dirty(true);
  1378. return KSuccess;
  1379. }
  1380. KResult Ext2FSInode::truncate(u64 size)
  1381. {
  1382. LOCKER(m_lock);
  1383. if (static_cast<u64>(m_raw_inode.i_size) == size)
  1384. return KSuccess;
  1385. auto result = resize(size);
  1386. if (result.is_error())
  1387. return result;
  1388. set_metadata_dirty(true);
  1389. return KSuccess;
  1390. }
  1391. unsigned Ext2FS::total_block_count() const
  1392. {
  1393. LOCKER(m_lock);
  1394. return super_block().s_blocks_count;
  1395. }
  1396. unsigned Ext2FS::free_block_count() const
  1397. {
  1398. LOCKER(m_lock);
  1399. return super_block().s_free_blocks_count;
  1400. }
  1401. unsigned Ext2FS::total_inode_count() const
  1402. {
  1403. LOCKER(m_lock);
  1404. return super_block().s_inodes_count;
  1405. }
  1406. unsigned Ext2FS::free_inode_count() const
  1407. {
  1408. LOCKER(m_lock);
  1409. return super_block().s_free_inodes_count;
  1410. }
  1411. KResult Ext2FS::prepare_to_unmount() const
  1412. {
  1413. LOCKER(m_lock);
  1414. for (auto& it : m_inode_cache) {
  1415. if (it.value->ref_count() > 1)
  1416. return KResult(-EBUSY);
  1417. }
  1418. m_inode_cache.clear();
  1419. return KSuccess;
  1420. }
  1421. }