Ext2FileSystem.cpp 58 KB

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