Ext2FileSystem.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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. // Mark all blocks used by this inode as free.
  447. auto block_list = block_list_for_inode(inode.m_raw_inode, true);
  448. for (auto block_index : block_list) {
  449. ASSERT(block_index <= super_block().s_blocks_count);
  450. if (block_index)
  451. set_block_allocation_state(block_index, false);
  452. }
  453. // If the inode being freed is a directory, update block group directory counter.
  454. if (inode.is_directory()) {
  455. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode.index())));
  456. --bgd.bg_used_dirs_count;
  457. dbgln("Ext2FS: Decremented bg_used_dirs_count to {}", bgd.bg_used_dirs_count);
  458. m_block_group_descriptors_dirty = true;
  459. }
  460. // NOTE: After this point, the inode metadata is wiped.
  461. memset(&inode.m_raw_inode, 0, sizeof(ext2_inode));
  462. inode.m_raw_inode.i_dtime = kgettimeofday().tv_sec;
  463. write_ext2_inode(inode.index(), inode.m_raw_inode);
  464. // Mark the inode as free.
  465. set_inode_allocation_state(inode.index(), false);
  466. }
  467. void Ext2FS::flush_block_group_descriptor_table()
  468. {
  469. LOCKER(m_lock);
  470. unsigned blocks_to_write = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
  471. unsigned first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
  472. auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)block_group_descriptors());
  473. write_blocks(first_block_of_bgdt, blocks_to_write, buffer);
  474. }
  475. void Ext2FS::flush_writes()
  476. {
  477. LOCKER(m_lock);
  478. if (m_super_block_dirty) {
  479. flush_super_block();
  480. m_super_block_dirty = false;
  481. }
  482. if (m_block_group_descriptors_dirty) {
  483. flush_block_group_descriptor_table();
  484. m_block_group_descriptors_dirty = false;
  485. }
  486. for (auto& cached_bitmap : m_cached_bitmaps) {
  487. if (cached_bitmap->dirty) {
  488. auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer.data());
  489. write_block(cached_bitmap->bitmap_block_index, buffer, block_size());
  490. cached_bitmap->dirty = false;
  491. #ifdef EXT2_DEBUG
  492. dbgln("Flushed bitmap block {}", cached_bitmap->bitmap_block_index);
  493. #endif
  494. }
  495. }
  496. BlockBasedFS::flush_writes();
  497. // Uncache Inodes that are only kept alive by the index-to-inode lookup cache.
  498. // We don't uncache Inodes that are being watched by at least one InodeWatcher.
  499. // FIXME: It would be better to keep a capped number of Inodes around.
  500. // The problem is that they are quite heavy objects, and use a lot of heap memory
  501. // for their (child name lookup) and (block list) caches.
  502. Vector<InodeIndex> unused_inodes;
  503. for (auto& it : m_inode_cache) {
  504. if (it.value->ref_count() != 1)
  505. continue;
  506. if (it.value->has_watchers())
  507. continue;
  508. unused_inodes.append(it.key);
  509. }
  510. for (auto index : unused_inodes)
  511. uncache_inode(index);
  512. }
  513. Ext2FSInode::Ext2FSInode(Ext2FS& fs, unsigned index)
  514. : Inode(fs, index)
  515. {
  516. }
  517. Ext2FSInode::~Ext2FSInode()
  518. {
  519. if (m_raw_inode.i_links_count == 0)
  520. fs().free_inode(*this);
  521. }
  522. InodeMetadata Ext2FSInode::metadata() const
  523. {
  524. LOCKER(m_lock);
  525. InodeMetadata metadata;
  526. metadata.inode = identifier();
  527. metadata.size = m_raw_inode.i_size;
  528. metadata.mode = m_raw_inode.i_mode;
  529. metadata.uid = m_raw_inode.i_uid;
  530. metadata.gid = m_raw_inode.i_gid;
  531. metadata.link_count = m_raw_inode.i_links_count;
  532. metadata.atime = m_raw_inode.i_atime;
  533. metadata.ctime = m_raw_inode.i_ctime;
  534. metadata.mtime = m_raw_inode.i_mtime;
  535. metadata.dtime = m_raw_inode.i_dtime;
  536. metadata.block_size = fs().block_size();
  537. metadata.block_count = m_raw_inode.i_blocks;
  538. if (Kernel::is_character_device(m_raw_inode.i_mode) || Kernel::is_block_device(m_raw_inode.i_mode)) {
  539. unsigned dev = m_raw_inode.i_block[0];
  540. if (!dev)
  541. dev = m_raw_inode.i_block[1];
  542. metadata.major_device = (dev & 0xfff00) >> 8;
  543. metadata.minor_device = (dev & 0xff) | ((dev >> 12) & 0xfff00);
  544. }
  545. return metadata;
  546. }
  547. void Ext2FSInode::flush_metadata()
  548. {
  549. LOCKER(m_lock);
  550. #ifdef EXT2_DEBUG
  551. dbgln("Ext2FS: flush_metadata for inode {}", index());
  552. #endif
  553. fs().write_ext2_inode(index(), m_raw_inode);
  554. if (is_directory()) {
  555. // Unless we're about to go away permanently, invalidate the lookup cache.
  556. if (m_raw_inode.i_links_count != 0) {
  557. // FIXME: This invalidation is way too hardcore. It's sad to throw away the whole cache.
  558. m_lookup_cache.clear();
  559. }
  560. }
  561. set_metadata_dirty(false);
  562. }
  563. RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
  564. {
  565. LOCKER(m_lock);
  566. ASSERT(inode.fsid() == fsid());
  567. {
  568. auto it = m_inode_cache.find(inode.index());
  569. if (it != m_inode_cache.end())
  570. return (*it).value;
  571. }
  572. if (!get_inode_allocation_state(inode.index())) {
  573. m_inode_cache.set(inode.index(), nullptr);
  574. return nullptr;
  575. }
  576. unsigned block_index;
  577. unsigned offset;
  578. if (!find_block_containing_inode(inode.index(), block_index, offset))
  579. return {};
  580. auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
  581. auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode));
  582. read_block(block_index, &buffer, sizeof(ext2_inode), offset);
  583. m_inode_cache.set(inode.index(), new_inode);
  584. return new_inode;
  585. }
  586. ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  587. {
  588. Locker inode_locker(m_lock);
  589. ASSERT(offset >= 0);
  590. if (m_raw_inode.i_size == 0)
  591. return 0;
  592. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  593. // This avoids wasting an entire block on short links. (Most links are short.)
  594. if (is_symlink() && size() < max_inline_symlink_length) {
  595. ASSERT(offset == 0);
  596. ssize_t nread = min((off_t)size() - offset, static_cast<off_t>(count));
  597. if (!buffer.write(((const u8*)m_raw_inode.i_block) + offset, (size_t)nread))
  598. return -EFAULT;
  599. return nread;
  600. }
  601. Locker fs_locker(fs().m_lock);
  602. if (m_block_list.is_empty())
  603. m_block_list = fs().block_list_for_inode(m_raw_inode);
  604. if (m_block_list.is_empty()) {
  605. klog() << "ext2fs: read_bytes: empty block list for inode " << index();
  606. return -EIO;
  607. }
  608. bool allow_cache = !description || !description->is_direct();
  609. const int block_size = fs().block_size();
  610. size_t first_block_logical_index = offset / block_size;
  611. size_t last_block_logical_index = (offset + count) / block_size;
  612. if (last_block_logical_index >= m_block_list.size())
  613. last_block_logical_index = m_block_list.size() - 1;
  614. int offset_into_first_block = offset % block_size;
  615. ssize_t nread = 0;
  616. size_t remaining_count = min((off_t)count, (off_t)size() - offset);
  617. #ifdef EXT2_VERY_DEBUG
  618. dbgln("Ext2FS: Reading up to {} bytes, {} bytes into inode {} to {}", count, offset, index(), buffer.user_or_kernel_ptr());
  619. #endif
  620. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  621. auto block_index = m_block_list[bi];
  622. ASSERT(block_index);
  623. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  624. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  625. auto buffer_offset = buffer.offset(nread);
  626. int err = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
  627. if (err < 0) {
  628. klog() << "ext2fs: read_bytes: read_block(" << block_index << ") failed (lbi: " << bi << ")";
  629. return err;
  630. }
  631. remaining_count -= num_bytes_to_copy;
  632. nread += num_bytes_to_copy;
  633. }
  634. return nread;
  635. }
  636. KResult Ext2FSInode::resize(u64 new_size)
  637. {
  638. u64 old_size = size();
  639. if (old_size == new_size)
  640. return KSuccess;
  641. u64 block_size = fs().block_size();
  642. size_t blocks_needed_before = ceil_div(old_size, block_size);
  643. size_t blocks_needed_after = ceil_div(new_size, block_size);
  644. #ifdef EXT2_DEBUG
  645. dbgln("Ext2FSInode::resize(): blocks needed before (size was {}): {}", old_size, blocks_needed_before);
  646. dbgln("Ext2FSInode::resize(): blocks needed after (size is {}): {}", new_size, blocks_needed_after);
  647. #endif
  648. if (blocks_needed_after > blocks_needed_before) {
  649. u32 additional_blocks_needed = blocks_needed_after - blocks_needed_before;
  650. if (additional_blocks_needed > fs().super_block().s_free_blocks_count)
  651. return KResult(-ENOSPC);
  652. }
  653. Vector<Ext2FS::BlockIndex> block_list;
  654. if (!m_block_list.is_empty())
  655. block_list = m_block_list;
  656. else
  657. block_list = fs().block_list_for_inode(m_raw_inode);
  658. if (blocks_needed_after > blocks_needed_before) {
  659. auto new_blocks = fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before);
  660. block_list.append(move(new_blocks));
  661. } else if (blocks_needed_after < blocks_needed_before) {
  662. #ifdef EXT2_DEBUG
  663. dbgln("Ext2FS: Shrinking inode {}. Old block list is {} entries:", index(), block_list.size());
  664. for (auto block_index : block_list) {
  665. dbgln(" # {}", block_index);
  666. }
  667. #endif
  668. while (block_list.size() != blocks_needed_after) {
  669. auto block_index = block_list.take_last();
  670. if (block_index)
  671. fs().set_block_allocation_state(block_index, false);
  672. }
  673. }
  674. int err = fs().write_block_list_for_inode(index(), m_raw_inode, block_list);
  675. if (err < 0)
  676. return KResult(err);
  677. m_raw_inode.i_size = new_size;
  678. set_metadata_dirty(true);
  679. m_block_list = move(block_list);
  680. if (new_size > old_size) {
  681. // If we're growing the inode, make sure we zero out all the new space.
  682. // FIXME: There are definitely more efficient ways to achieve this.
  683. size_t bytes_to_clear = new_size - old_size;
  684. size_t clear_from = old_size;
  685. u8 zero_buffer[PAGE_SIZE];
  686. memset(zero_buffer, 0, sizeof(zero_buffer));
  687. while (bytes_to_clear) {
  688. auto nwritten = write_bytes(clear_from, min(sizeof(zero_buffer), bytes_to_clear), UserOrKernelBuffer::for_kernel_buffer(zero_buffer), nullptr);
  689. if (nwritten < 0)
  690. return KResult(-nwritten);
  691. ASSERT(nwritten != 0);
  692. bytes_to_clear -= nwritten;
  693. clear_from += nwritten;
  694. }
  695. }
  696. return KSuccess;
  697. }
  698. ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& data, FileDescription* description)
  699. {
  700. ASSERT(offset >= 0);
  701. ASSERT(count >= 0);
  702. Locker inode_locker(m_lock);
  703. Locker fs_locker(fs().m_lock);
  704. auto result = prepare_to_write_data();
  705. if (result.is_error())
  706. return result;
  707. if (is_symlink()) {
  708. ASSERT(offset == 0);
  709. if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) {
  710. #ifdef EXT2_DEBUG
  711. dbgln("Ext2FS: write_bytes poking into i_block array for inline symlink '{}' ({} bytes)", data.copy_into_string(count), count);
  712. #endif
  713. if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count))
  714. return -EFAULT;
  715. if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size)
  716. m_raw_inode.i_size = offset + count;
  717. set_metadata_dirty(true);
  718. return count;
  719. }
  720. }
  721. bool allow_cache = !description || !description->is_direct();
  722. const size_t block_size = fs().block_size();
  723. u64 old_size = size();
  724. u64 new_size = max(static_cast<u64>(offset) + count, (u64)size());
  725. auto resize_result = resize(new_size);
  726. if (resize_result.is_error())
  727. return resize_result;
  728. if (m_block_list.is_empty())
  729. m_block_list = fs().block_list_for_inode(m_raw_inode);
  730. if (m_block_list.is_empty()) {
  731. dbgln("Ext2FSInode::write_bytes(): empty block list for inode {}", index());
  732. return -EIO;
  733. }
  734. size_t first_block_logical_index = offset / block_size;
  735. size_t last_block_logical_index = (offset + count) / block_size;
  736. if (last_block_logical_index >= m_block_list.size())
  737. last_block_logical_index = m_block_list.size() - 1;
  738. size_t offset_into_first_block = offset % block_size;
  739. ssize_t nwritten = 0;
  740. size_t remaining_count = min((off_t)count, (off_t)new_size - offset);
  741. #ifdef EXT2_VERY_DEBUG
  742. dbgln("Ext2FS: Writing {} bytes, {} bytes into inode {} from {}", count, offset, index(), data.user_or_kernel_ptr());
  743. #endif
  744. for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  745. size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  746. size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  747. #ifdef EXT2_VERY_DEBUG
  748. dbgln("Ext2FS: Writing block {} (offset_into_block: {})", m_block_list[bi], offset_into_block);
  749. #endif
  750. int err = fs().write_block(m_block_list[bi], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache);
  751. if (err < 0) {
  752. dbgln("Ext2FS: write_block({}) failed (bi: {})", m_block_list[bi], bi);
  753. ASSERT_NOT_REACHED();
  754. return err;
  755. }
  756. remaining_count -= num_bytes_to_copy;
  757. nwritten += num_bytes_to_copy;
  758. }
  759. #ifdef EXT2_VERY_DEBUG
  760. 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());
  761. #endif
  762. if (old_size != new_size)
  763. inode_size_changed(old_size, new_size);
  764. inode_contents_changed(offset, count, data);
  765. return nwritten;
  766. }
  767. u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
  768. {
  769. switch (entry.file_type) {
  770. case EXT2_FT_REG_FILE:
  771. return DT_REG;
  772. case EXT2_FT_DIR:
  773. return DT_DIR;
  774. case EXT2_FT_CHRDEV:
  775. return DT_CHR;
  776. case EXT2_FT_BLKDEV:
  777. return DT_BLK;
  778. case EXT2_FT_FIFO:
  779. return DT_FIFO;
  780. case EXT2_FT_SOCK:
  781. return DT_SOCK;
  782. case EXT2_FT_SYMLINK:
  783. return DT_LNK;
  784. default:
  785. return DT_UNKNOWN;
  786. }
  787. }
  788. KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  789. {
  790. LOCKER(m_lock);
  791. ASSERT(is_directory());
  792. #ifdef EXT2_VERY_DEBUG
  793. dbgln("Ext2FS: Traversing as directory: {}", index());
  794. #endif
  795. auto buffer_or = read_entire();
  796. if (buffer_or.is_error())
  797. return buffer_or.error();
  798. auto& buffer = *buffer_or.value();
  799. auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.data());
  800. while (entry < buffer.end_pointer()) {
  801. if (entry->inode != 0) {
  802. #ifdef EXT2_VERY_DEBUG
  803. 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));
  804. #endif
  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. bool 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. #ifdef EXT2_DEBUG
  822. dbgln("Ext2FS: New directory inode {} contents to write (size {}, occupied {}):", index(), directory_size, occupied_size);
  823. #endif
  824. auto directory_data = ByteBuffer::create_uninitialized(occupied_size);
  825. OutputMemoryStream stream { directory_data };
  826. for (size_t i = 0; i < entries.size(); ++i) {
  827. auto& entry = entries[i];
  828. int record_length = EXT2_DIR_REC_LEN(entry.name.length());
  829. if (i == entries.size() - 1)
  830. record_length += occupied_size - directory_size;
  831. #ifdef EXT2_DEBUG
  832. 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);
  833. #endif
  834. stream << u32(entry.inode.index());
  835. stream << u16(record_length);
  836. stream << u8(entry.name.length());
  837. stream << u8(entry.file_type);
  838. stream << entry.name.bytes();
  839. int padding = record_length - entry.name.length() - 8;
  840. for (int j = 0; j < padding; ++j)
  841. stream << u8(0);
  842. }
  843. stream.fill_to_end(0);
  844. auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
  845. ssize_t nwritten = write_bytes(0, stream.size(), buffer, nullptr);
  846. if (nwritten < 0)
  847. return false;
  848. set_metadata_dirty(true);
  849. return static_cast<size_t>(nwritten) == directory_data.size();
  850. }
  851. KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  852. {
  853. if (mode & S_IFDIR)
  854. return fs().create_directory(identifier(), name, mode, uid, gid);
  855. return fs().create_inode(identifier(), name, mode, 0, dev, uid, gid);
  856. }
  857. KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode)
  858. {
  859. LOCKER(m_lock);
  860. ASSERT(is_directory());
  861. if (name.length() > EXT2_NAME_LEN)
  862. return KResult(-ENAMETOOLONG);
  863. #ifdef EXT2_DEBUG
  864. dbgln("Ext2FSInode::add_child: Adding inode {} with name '{}' and mode {:o} to directory {}", child.index(), name, mode, index());
  865. #endif
  866. Vector<Ext2FSDirectoryEntry> entries;
  867. bool name_already_exists = false;
  868. KResult result = traverse_as_directory([&](auto& entry) {
  869. if (name == entry.name) {
  870. name_already_exists = true;
  871. return false;
  872. }
  873. entries.append({ entry.name, entry.inode, entry.file_type });
  874. return true;
  875. });
  876. if (result.is_error())
  877. return result;
  878. if (name_already_exists) {
  879. dbgln("Ext2FSInode::add_child: Name '{}' already exists in inode {}", name, index());
  880. return KResult(-EEXIST);
  881. }
  882. result = child.increment_link_count();
  883. if (result.is_error())
  884. return result;
  885. entries.empend(name, child.identifier(), to_ext2_file_type(mode));
  886. bool success = write_directory(entries);
  887. if (success)
  888. m_lookup_cache.set(name, child.index());
  889. did_add_child(child.identifier());
  890. return KSuccess;
  891. }
  892. KResult Ext2FSInode::remove_child(const StringView& name)
  893. {
  894. LOCKER(m_lock);
  895. #ifdef EXT2_DEBUG
  896. dbgln("Ext2FSInode::remove_child('{}') in inode {}", name, index());
  897. #endif
  898. ASSERT(is_directory());
  899. auto it = m_lookup_cache.find(name);
  900. if (it == m_lookup_cache.end())
  901. return KResult(-ENOENT);
  902. auto child_inode_index = (*it).value;
  903. InodeIdentifier child_id { fsid(), child_inode_index };
  904. #ifdef EXT2_DEBUG
  905. dbgln("Ext2FSInode::remove_child(): Removing '{}' in directory {}", name, index());
  906. #endif
  907. Vector<Ext2FSDirectoryEntry> entries;
  908. KResult result = traverse_as_directory([&](auto& entry) {
  909. if (name != entry.name)
  910. entries.append({ entry.name, entry.inode, entry.file_type });
  911. return true;
  912. });
  913. if (result.is_error())
  914. return result;
  915. bool success = write_directory(entries);
  916. if (!success) {
  917. // FIXME: Plumb error from write_directory().
  918. return KResult(-EIO);
  919. }
  920. m_lookup_cache.remove(name);
  921. auto child_inode = fs().get_inode(child_id);
  922. result = child_inode->decrement_link_count();
  923. if (result.is_error())
  924. return result;
  925. did_remove_child(child_id);
  926. return KSuccess;
  927. }
  928. unsigned Ext2FS::inodes_per_block() const
  929. {
  930. return EXT2_INODES_PER_BLOCK(&super_block());
  931. }
  932. unsigned Ext2FS::inodes_per_group() const
  933. {
  934. return EXT2_INODES_PER_GROUP(&super_block());
  935. }
  936. unsigned Ext2FS::inode_size() const
  937. {
  938. return EXT2_INODE_SIZE(&super_block());
  939. }
  940. unsigned Ext2FS::blocks_per_group() const
  941. {
  942. return EXT2_BLOCKS_PER_GROUP(&super_block());
  943. }
  944. bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
  945. {
  946. LOCKER(m_lock);
  947. unsigned block_index;
  948. unsigned offset;
  949. if (!find_block_containing_inode(inode, block_index, offset))
  950. return false;
  951. auto buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)&e2inode));
  952. return write_block(block_index, buffer, inode_size(), offset) >= 0;
  953. }
  954. Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count)
  955. {
  956. LOCKER(m_lock);
  957. #ifdef EXT2_DEBUG
  958. dbgln("Ext2FS: allocate_blocks(preferred group: {}, count {})", preferred_group_index, count);
  959. #endif
  960. if (count == 0)
  961. return {};
  962. Vector<BlockIndex> blocks;
  963. #ifdef EXT2_DEBUG
  964. dbgln("Ext2FS: allocate_blocks:");
  965. #endif
  966. blocks.ensure_capacity(count);
  967. GroupIndex group_index = preferred_group_index;
  968. if (!group_descriptor(preferred_group_index).bg_free_blocks_count) {
  969. group_index = 1;
  970. }
  971. while (blocks.size() < count) {
  972. bool found_a_group = false;
  973. if (group_descriptor(group_index).bg_free_blocks_count) {
  974. found_a_group = true;
  975. } else {
  976. if (group_index == preferred_group_index)
  977. group_index = 1;
  978. for (; group_index <= m_block_group_count; ++group_index) {
  979. if (group_descriptor(group_index).bg_free_blocks_count) {
  980. found_a_group = true;
  981. break;
  982. }
  983. }
  984. }
  985. ASSERT(found_a_group);
  986. auto& bgd = group_descriptor(group_index);
  987. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  988. int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
  989. auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
  990. BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
  991. size_t free_region_size = 0;
  992. auto first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
  993. ASSERT(first_unset_bit_index.has_value());
  994. #ifdef EXT2_DEBUG
  995. dbgln("Ext2FS: allocating free region of size: {} [{}]", free_region_size, group_index);
  996. #endif
  997. for (size_t i = 0; i < free_region_size; ++i) {
  998. BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group;
  999. set_block_allocation_state(block_index, true);
  1000. blocks.unchecked_append(block_index);
  1001. #ifdef EXT2_DEBUG
  1002. dbgln(" allocated > {}", block_index);
  1003. #endif
  1004. }
  1005. }
  1006. ASSERT(blocks.size() == count);
  1007. return blocks;
  1008. }
  1009. unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group, off_t expected_size)
  1010. {
  1011. ASSERT(expected_size >= 0);
  1012. LOCKER(m_lock);
  1013. #ifdef EXT2_DEBUG
  1014. dbgln("Ext2FS: find_a_free_inode(preferred_group: {}, expected_size: {})", preferred_group, expected_size);
  1015. #endif
  1016. unsigned needed_blocks = ceil_div(static_cast<size_t>(expected_size), block_size());
  1017. #ifdef EXT2_DEBUG
  1018. dbgln("Ext2FS: minimum needed blocks: {}", needed_blocks);
  1019. #endif
  1020. unsigned group_index = 0;
  1021. // FIXME: We shouldn't refuse to allocate an inode if there is no group that can house the whole thing.
  1022. // In those cases we should just spread it across multiple groups.
  1023. auto is_suitable_group = [this, needed_blocks](GroupIndex group_index) {
  1024. auto& bgd = group_descriptor(group_index);
  1025. return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= needed_blocks;
  1026. };
  1027. if (preferred_group && is_suitable_group(preferred_group)) {
  1028. group_index = preferred_group;
  1029. } else {
  1030. for (unsigned i = 1; i <= m_block_group_count; ++i) {
  1031. if (is_suitable_group(i))
  1032. group_index = i;
  1033. }
  1034. }
  1035. if (!group_index) {
  1036. klog() << "Ext2FS: find_a_free_inode: no suitable group found for new inode with " << needed_blocks << " blocks needed :(";
  1037. return 0;
  1038. }
  1039. #ifdef EXT2_DEBUG
  1040. dbgln("Ext2FS: find_a_free_inode: found suitable group [{}] for new inode with {} blocks needed :^)", group_index, needed_blocks);
  1041. #endif
  1042. auto& bgd = group_descriptor(group_index);
  1043. unsigned inodes_in_group = min(inodes_per_group(), super_block().s_inodes_count);
  1044. unsigned first_free_inode_in_group = 0;
  1045. unsigned first_inode_in_group = (group_index - 1) * inodes_per_group() + 1;
  1046. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1047. auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
  1048. for (size_t i = 0; i < inode_bitmap.size(); ++i) {
  1049. if (inode_bitmap.get(i))
  1050. continue;
  1051. first_free_inode_in_group = first_inode_in_group + i;
  1052. break;
  1053. }
  1054. if (!first_free_inode_in_group) {
  1055. klog() << "Ext2FS: first_free_inode_in_group returned no inode, despite bgd claiming there are inodes :(";
  1056. return 0;
  1057. }
  1058. unsigned inode = first_free_inode_in_group;
  1059. #ifdef EXT2_DEBUG
  1060. dbgln("Ext2FS: found suitable inode {}", inode);
  1061. #endif
  1062. ASSERT(get_inode_allocation_state(inode) == false);
  1063. return inode;
  1064. }
  1065. Ext2FS::GroupIndex Ext2FS::group_index_from_block_index(BlockIndex block_index) const
  1066. {
  1067. if (!block_index)
  1068. return 0;
  1069. return (block_index - 1) / blocks_per_group() + 1;
  1070. }
  1071. unsigned Ext2FS::group_index_from_inode(unsigned inode) const
  1072. {
  1073. if (!inode)
  1074. return 0;
  1075. return (inode - 1) / inodes_per_group() + 1;
  1076. }
  1077. bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
  1078. {
  1079. LOCKER(m_lock);
  1080. if (index == 0)
  1081. return true;
  1082. unsigned group_index = group_index_from_inode(index);
  1083. auto& bgd = group_descriptor(group_index);
  1084. unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
  1085. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1086. auto& cached_bitmap = const_cast<Ext2FS&>(*this).get_bitmap_block(bgd.bg_inode_bitmap);
  1087. return cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1088. }
  1089. bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
  1090. {
  1091. LOCKER(m_lock);
  1092. unsigned group_index = group_index_from_inode(inode_index);
  1093. auto& bgd = group_descriptor(group_index);
  1094. unsigned index_in_group = inode_index - ((group_index - 1) * inodes_per_group());
  1095. unsigned bit_index = (index_in_group - 1) % inodes_per_group();
  1096. auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
  1097. bool current_state = cached_bitmap.bitmap(inodes_per_group()).get(bit_index);
  1098. #ifdef EXT2_DEBUG
  1099. dbgln("Ext2FS: set_inode_allocation_state({}) {} -> {}", inode_index, current_state, new_state);
  1100. #endif
  1101. if (current_state == new_state) {
  1102. ASSERT_NOT_REACHED();
  1103. return true;
  1104. }
  1105. cached_bitmap.bitmap(inodes_per_group()).set(bit_index, new_state);
  1106. cached_bitmap.dirty = true;
  1107. // Update superblock
  1108. if (new_state)
  1109. --m_super_block.s_free_inodes_count;
  1110. else
  1111. ++m_super_block.s_free_inodes_count;
  1112. m_super_block_dirty = true;
  1113. // Update BGD
  1114. auto& mutable_bgd = const_cast<ext2_group_desc&>(bgd);
  1115. if (new_state)
  1116. --mutable_bgd.bg_free_inodes_count;
  1117. else
  1118. ++mutable_bgd.bg_free_inodes_count;
  1119. m_block_group_descriptors_dirty = true;
  1120. return true;
  1121. }
  1122. Ext2FS::BlockIndex Ext2FS::first_block_index() const
  1123. {
  1124. return block_size() == 1024 ? 1 : 0;
  1125. }
  1126. Ext2FS::CachedBitmap& Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index)
  1127. {
  1128. for (auto& cached_bitmap : m_cached_bitmaps) {
  1129. if (cached_bitmap->bitmap_block_index == bitmap_block_index)
  1130. return *cached_bitmap;
  1131. }
  1132. auto block = KBuffer::create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block");
  1133. auto buffer = UserOrKernelBuffer::for_kernel_buffer(block.data());
  1134. int err = read_block(bitmap_block_index, &buffer, block_size());
  1135. ASSERT(err >= 0);
  1136. m_cached_bitmaps.append(make<CachedBitmap>(bitmap_block_index, move(block)));
  1137. return *m_cached_bitmaps.last();
  1138. }
  1139. bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
  1140. {
  1141. ASSERT(block_index != 0);
  1142. LOCKER(m_lock);
  1143. GroupIndex group_index = group_index_from_block_index(block_index);
  1144. auto& bgd = group_descriptor(group_index);
  1145. BlockIndex index_in_group = (block_index - first_block_index()) - ((group_index - 1) * blocks_per_group());
  1146. unsigned bit_index = index_in_group % blocks_per_group();
  1147. auto& cached_bitmap = get_bitmap_block(bgd.bg_block_bitmap);
  1148. bool current_state = cached_bitmap.bitmap(blocks_per_group()).get(bit_index);
  1149. #ifdef EXT2_DEBUG
  1150. dbgln("Ext2FS: block {} state: {} -> {} (in bitmap block {})", block_index, current_state, new_state, bgd.bg_block_bitmap);
  1151. #endif
  1152. if (current_state == new_state) {
  1153. ASSERT_NOT_REACHED();
  1154. return true;
  1155. }
  1156. cached_bitmap.bitmap(blocks_per_group()).set(bit_index, new_state);
  1157. cached_bitmap.dirty = true;
  1158. // Update superblock
  1159. if (new_state)
  1160. --m_super_block.s_free_blocks_count;
  1161. else
  1162. ++m_super_block.s_free_blocks_count;
  1163. m_super_block_dirty = true;
  1164. // Update BGD
  1165. auto& mutable_bgd = const_cast<ext2_group_desc&>(bgd);
  1166. if (new_state)
  1167. --mutable_bgd.bg_free_blocks_count;
  1168. else
  1169. ++mutable_bgd.bg_free_blocks_count;
  1170. m_block_group_descriptors_dirty = true;
  1171. return true;
  1172. }
  1173. KResult Ext2FS::create_directory(InodeIdentifier parent_id, const String& name, mode_t mode, uid_t uid, gid_t gid)
  1174. {
  1175. LOCKER(m_lock);
  1176. ASSERT(parent_id.fsid() == fsid());
  1177. // Fix up the mode to definitely be a directory.
  1178. // FIXME: This is a bit on the hackish side.
  1179. mode &= ~0170000;
  1180. mode |= 0040000;
  1181. // NOTE: When creating a new directory, make the size 1 block.
  1182. // There's probably a better strategy here, but this works for now.
  1183. auto inode_or_error = create_inode(parent_id, name, mode, block_size(), 0, uid, gid);
  1184. if (inode_or_error.is_error())
  1185. return inode_or_error.error();
  1186. auto& inode = inode_or_error.value();
  1187. #ifdef EXT2_DEBUG
  1188. dbgln("Ext2FS: create_directory: created new directory named '{} with inode {}", name, inode->index());
  1189. #endif
  1190. Vector<Ext2FSDirectoryEntry> entries;
  1191. entries.empend(".", inode->identifier(), static_cast<u8>(EXT2_FT_DIR));
  1192. entries.empend("..", parent_id, static_cast<u8>(EXT2_FT_DIR));
  1193. bool success = static_cast<Ext2FSInode&>(*inode).write_directory(entries);
  1194. ASSERT(success);
  1195. auto parent_inode = get_inode(parent_id);
  1196. auto result = parent_inode->increment_link_count();
  1197. if (result.is_error())
  1198. return result;
  1199. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
  1200. ++bgd.bg_used_dirs_count;
  1201. m_block_group_descriptors_dirty = true;
  1202. return KSuccess;
  1203. }
  1204. KResultOr<NonnullRefPtr<Inode>> Ext2FS::create_inode(InodeIdentifier parent_id, const String& name, mode_t mode, off_t size, dev_t dev, uid_t uid, gid_t gid)
  1205. {
  1206. LOCKER(m_lock);
  1207. ASSERT(size >= 0);
  1208. ASSERT(parent_id.fsid() == fsid());
  1209. auto parent_inode = get_inode(parent_id);
  1210. ASSERT(parent_inode);
  1211. if (static_cast<const Ext2FSInode&>(*parent_inode).m_raw_inode.i_links_count == 0)
  1212. return KResult(-ENOENT);
  1213. if (name.length() > EXT2_NAME_LEN)
  1214. return KResult(-ENAMETOOLONG);
  1215. #ifdef EXT2_DEBUG
  1216. dbgln("Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode->index());
  1217. #endif
  1218. size_t needed_blocks = ceil_div(static_cast<size_t>(size), block_size());
  1219. if ((size_t)needed_blocks > super_block().s_free_blocks_count) {
  1220. dbgln("Ext2FS: create_inode: not enough free blocks");
  1221. return KResult(-ENOSPC);
  1222. }
  1223. // NOTE: This doesn't commit the inode allocation just yet!
  1224. auto inode_id = find_a_free_inode(0, size);
  1225. if (!inode_id) {
  1226. klog() << "Ext2FS: create_inode: allocate_inode failed";
  1227. return KResult(-ENOSPC);
  1228. }
  1229. auto blocks = allocate_blocks(group_index_from_inode(inode_id), needed_blocks);
  1230. ASSERT(blocks.size() == needed_blocks);
  1231. // Looks like we're good, time to update the inode bitmap and group+global inode counters.
  1232. bool success = set_inode_allocation_state(inode_id, true);
  1233. ASSERT(success);
  1234. struct timeval now;
  1235. kgettimeofday(now);
  1236. ext2_inode e2inode;
  1237. memset(&e2inode, 0, sizeof(ext2_inode));
  1238. e2inode.i_mode = mode;
  1239. e2inode.i_uid = uid;
  1240. e2inode.i_gid = gid;
  1241. e2inode.i_size = size;
  1242. e2inode.i_atime = now.tv_sec;
  1243. e2inode.i_ctime = now.tv_sec;
  1244. e2inode.i_mtime = now.tv_sec;
  1245. e2inode.i_dtime = 0;
  1246. // For directories, add +1 link count for the "." entry in self.
  1247. e2inode.i_links_count = is_directory(mode);
  1248. if (is_character_device(mode))
  1249. e2inode.i_block[0] = dev;
  1250. else if (is_block_device(mode))
  1251. e2inode.i_block[1] = dev;
  1252. success = write_block_list_for_inode(inode_id, e2inode, blocks);
  1253. ASSERT(success);
  1254. #ifdef EXT2_DEBUG
  1255. dbgln("Ext2FS: writing initial metadata for inode {}", inode_id);
  1256. #endif
  1257. e2inode.i_flags = 0;
  1258. success = write_ext2_inode(inode_id, e2inode);
  1259. ASSERT(success);
  1260. // We might have cached the fact that this inode didn't exist. Wipe the slate.
  1261. m_inode_cache.remove(inode_id);
  1262. auto inode = get_inode({ fsid(), inode_id });
  1263. // If we've already computed a block list, no sense in throwing it away.
  1264. static_cast<Ext2FSInode&>(*inode).m_block_list = move(blocks);
  1265. auto result = parent_inode->add_child(*inode, name, mode);
  1266. ASSERT(result.is_success());
  1267. return inode.release_nonnull();
  1268. }
  1269. bool Ext2FSInode::populate_lookup_cache() const
  1270. {
  1271. LOCKER(m_lock);
  1272. if (!m_lookup_cache.is_empty())
  1273. return true;
  1274. HashMap<String, unsigned> children;
  1275. KResult result = traverse_as_directory([&children](auto& entry) {
  1276. children.set(entry.name, entry.inode.index());
  1277. return true;
  1278. });
  1279. if (!result.is_success())
  1280. return false;
  1281. if (!m_lookup_cache.is_empty())
  1282. return false;
  1283. m_lookup_cache = move(children);
  1284. return true;
  1285. }
  1286. RefPtr<Inode> Ext2FSInode::lookup(StringView name)
  1287. {
  1288. ASSERT(is_directory());
  1289. if (!populate_lookup_cache())
  1290. return {};
  1291. LOCKER(m_lock);
  1292. auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
  1293. if (it != m_lookup_cache.end())
  1294. return fs().get_inode({ fsid(), (*it).value });
  1295. return {};
  1296. }
  1297. void Ext2FSInode::one_ref_left()
  1298. {
  1299. // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
  1300. }
  1301. int Ext2FSInode::set_atime(time_t t)
  1302. {
  1303. LOCKER(m_lock);
  1304. if (fs().is_readonly())
  1305. return -EROFS;
  1306. m_raw_inode.i_atime = t;
  1307. set_metadata_dirty(true);
  1308. return 0;
  1309. }
  1310. int Ext2FSInode::set_ctime(time_t t)
  1311. {
  1312. LOCKER(m_lock);
  1313. if (fs().is_readonly())
  1314. return -EROFS;
  1315. m_raw_inode.i_ctime = t;
  1316. set_metadata_dirty(true);
  1317. return 0;
  1318. }
  1319. int Ext2FSInode::set_mtime(time_t t)
  1320. {
  1321. LOCKER(m_lock);
  1322. if (fs().is_readonly())
  1323. return -EROFS;
  1324. m_raw_inode.i_mtime = t;
  1325. set_metadata_dirty(true);
  1326. return 0;
  1327. }
  1328. KResult Ext2FSInode::increment_link_count()
  1329. {
  1330. LOCKER(m_lock);
  1331. if (fs().is_readonly())
  1332. return KResult(-EROFS);
  1333. if (m_raw_inode.i_links_count == max_link_count)
  1334. return KResult(-EMLINK);
  1335. ++m_raw_inode.i_links_count;
  1336. set_metadata_dirty(true);
  1337. return KSuccess;
  1338. }
  1339. KResult Ext2FSInode::decrement_link_count()
  1340. {
  1341. LOCKER(m_lock);
  1342. if (fs().is_readonly())
  1343. return KResult(-EROFS);
  1344. ASSERT(m_raw_inode.i_links_count);
  1345. --m_raw_inode.i_links_count;
  1346. if (ref_count() == 1 && m_raw_inode.i_links_count == 0)
  1347. fs().uncache_inode(index());
  1348. set_metadata_dirty(true);
  1349. return KSuccess;
  1350. }
  1351. void Ext2FS::uncache_inode(InodeIndex index)
  1352. {
  1353. LOCKER(m_lock);
  1354. m_inode_cache.remove(index);
  1355. }
  1356. KResultOr<size_t> Ext2FSInode::directory_entry_count() const
  1357. {
  1358. ASSERT(is_directory());
  1359. LOCKER(m_lock);
  1360. populate_lookup_cache();
  1361. return m_lookup_cache.size();
  1362. }
  1363. KResult Ext2FSInode::chmod(mode_t mode)
  1364. {
  1365. LOCKER(m_lock);
  1366. if (m_raw_inode.i_mode == mode)
  1367. return KSuccess;
  1368. m_raw_inode.i_mode = mode;
  1369. set_metadata_dirty(true);
  1370. return KSuccess;
  1371. }
  1372. KResult Ext2FSInode::chown(uid_t uid, gid_t gid)
  1373. {
  1374. LOCKER(m_lock);
  1375. if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid)
  1376. return KSuccess;
  1377. m_raw_inode.i_uid = uid;
  1378. m_raw_inode.i_gid = gid;
  1379. set_metadata_dirty(true);
  1380. return KSuccess;
  1381. }
  1382. KResult Ext2FSInode::truncate(u64 size)
  1383. {
  1384. LOCKER(m_lock);
  1385. if (static_cast<u64>(m_raw_inode.i_size) == size)
  1386. return KSuccess;
  1387. auto result = resize(size);
  1388. if (result.is_error())
  1389. return result;
  1390. set_metadata_dirty(true);
  1391. return KSuccess;
  1392. }
  1393. unsigned Ext2FS::total_block_count() const
  1394. {
  1395. LOCKER(m_lock);
  1396. return super_block().s_blocks_count;
  1397. }
  1398. unsigned Ext2FS::free_block_count() const
  1399. {
  1400. LOCKER(m_lock);
  1401. return super_block().s_free_blocks_count;
  1402. }
  1403. unsigned Ext2FS::total_inode_count() const
  1404. {
  1405. LOCKER(m_lock);
  1406. return super_block().s_inodes_count;
  1407. }
  1408. unsigned Ext2FS::free_inode_count() const
  1409. {
  1410. LOCKER(m_lock);
  1411. return super_block().s_free_inodes_count;
  1412. }
  1413. KResult Ext2FS::prepare_to_unmount() const
  1414. {
  1415. LOCKER(m_lock);
  1416. for (auto& it : m_inode_cache) {
  1417. if (it.value->ref_count() > 1)
  1418. return KResult(-EBUSY);
  1419. }
  1420. m_inode_cache.clear();
  1421. return KSuccess;
  1422. }
  1423. }