Ext2FileSystem.cpp 57 KB

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