Ext2FileSystem.cpp 57 KB

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