Ext2FileSystem.cpp 56 KB

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