Ext2FileSystem.cpp 56 KB

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