Ext2FileSystem.cpp 56 KB

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