Ext2FileSystem.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. #include "Ext2FileSystem.h"
  2. #include "ext2_fs.h"
  3. #include "UnixTypes.h"
  4. #include <AK/Bitmap.h>
  5. #include <AK/StdLib.h>
  6. #include <AK/kmalloc.h>
  7. #include <AK/ktime.h>
  8. #include <AK/kstdio.h>
  9. #include <AK/BufferStream.h>
  10. #include <LibC/errno_numbers.h>
  11. //#define EXT2_DEBUG
  12. class Ext2FileSystem::CachedExt2InodeImpl : public Retainable<CachedExt2InodeImpl> {
  13. public:
  14. CachedExt2InodeImpl(OwnPtr<ext2_inode>&& e2i) : e2inode(move(e2i)) { }
  15. ~CachedExt2InodeImpl() { }
  16. OwnPtr<ext2_inode> e2inode;
  17. };
  18. class Ext2FileSystem::CachedExt2Inode {
  19. public:
  20. const ext2_inode* operator->() const { return ptr->e2inode.ptr(); }
  21. const ext2_inode& operator*() const { return *ptr->e2inode; }
  22. ext2_inode* operator->() { return ptr->e2inode.ptr(); }
  23. ext2_inode& operator*() { return *ptr->e2inode; }
  24. bool operator!() const { return !ptr; }
  25. operator bool() const { return !!ptr; }
  26. CachedExt2Inode() { }
  27. explicit CachedExt2Inode(OwnPtr<ext2_inode>&& e2inode)
  28. : ptr(adopt(*new CachedExt2InodeImpl(move(e2inode))))
  29. { }
  30. explicit CachedExt2Inode(RetainPtr<CachedExt2InodeImpl> p)
  31. : ptr(p)
  32. { }
  33. RetainPtr<CachedExt2InodeImpl> ptr;
  34. };
  35. RetainPtr<Ext2FileSystem> Ext2FileSystem::create(RetainPtr<DiskDevice>&& device)
  36. {
  37. return adopt(*new Ext2FileSystem(move(device)));
  38. }
  39. Ext2FileSystem::Ext2FileSystem(RetainPtr<DiskDevice>&& device)
  40. : DiskBackedFileSystem(move(device))
  41. {
  42. }
  43. Ext2FileSystem::~Ext2FileSystem()
  44. {
  45. }
  46. ByteBuffer Ext2FileSystem::readSuperBlock() const
  47. {
  48. auto buffer = ByteBuffer::createUninitialized(1024);
  49. device().readBlock(2, buffer.pointer());
  50. device().readBlock(3, buffer.offsetPointer(512));
  51. return buffer;
  52. }
  53. bool Ext2FileSystem::writeSuperBlock(const ext2_super_block& sb)
  54. {
  55. const byte* raw = (const byte*)&sb;
  56. bool success;
  57. success = device().writeBlock(2, raw);
  58. ASSERT(success);
  59. success = device().writeBlock(3, raw + 512);
  60. ASSERT(success);
  61. // FIXME: This is an ugly way to refresh the superblock cache. :-|
  62. superBlock();
  63. return true;
  64. }
  65. unsigned Ext2FileSystem::firstBlockOfGroup(unsigned groupIndex) const
  66. {
  67. return superBlock().s_first_data_block + (groupIndex * superBlock().s_blocks_per_group);
  68. }
  69. const ext2_super_block& Ext2FileSystem::superBlock() const
  70. {
  71. if (!m_cachedSuperBlock)
  72. m_cachedSuperBlock = readSuperBlock();
  73. return *reinterpret_cast<ext2_super_block*>(m_cachedSuperBlock.pointer());
  74. }
  75. const ext2_group_desc& Ext2FileSystem::blockGroupDescriptor(unsigned groupIndex) const
  76. {
  77. // FIXME: Should this fail gracefully somehow?
  78. ASSERT(groupIndex <= m_blockGroupCount);
  79. if (!m_cachedBlockGroupDescriptorTable) {
  80. unsigned blocksToRead = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  81. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  82. #ifdef EXT2_DEBUG
  83. kprintf("ext2fs: block group count: %u, blocks-to-read: %u\n", m_blockGroupCount, blocksToRead);
  84. kprintf("ext2fs: first block of BGDT: %u\n", firstBlockOfBGDT);
  85. #endif
  86. m_cachedBlockGroupDescriptorTable = readBlocks(firstBlockOfBGDT, blocksToRead);
  87. }
  88. return reinterpret_cast<ext2_group_desc*>(m_cachedBlockGroupDescriptorTable.pointer())[groupIndex - 1];
  89. }
  90. bool Ext2FileSystem::initialize()
  91. {
  92. auto& superBlock = this->superBlock();
  93. #ifdef EXT2_DEBUG
  94. kprintf("ext2fs: super block magic: %x (super block size: %u)\n", superBlock.s_magic, sizeof(ext2_super_block));
  95. #endif
  96. if (superBlock.s_magic != EXT2_SUPER_MAGIC)
  97. return false;
  98. #ifdef EXT2_DEBUG
  99. kprintf("ext2fs: %u inodes, %u blocks\n", superBlock.s_inodes_count, superBlock.s_blocks_count);
  100. kprintf("ext2fs: block size = %u\n", EXT2_BLOCK_SIZE(&superBlock));
  101. kprintf("ext2fs: first data block = %u\n", superBlock.s_first_data_block);
  102. kprintf("ext2fs: inodes per block = %u\n", inodesPerBlock());
  103. kprintf("ext2fs: inodes per group = %u\n", inodesPerGroup());
  104. kprintf("ext2fs: free inodes = %u\n", superBlock.s_free_inodes_count);
  105. kprintf("ext2fs: desc per block = %u\n", EXT2_DESC_PER_BLOCK(&superBlock));
  106. kprintf("ext2fs: desc size = %u\n", EXT2_DESC_SIZE(&superBlock));
  107. #endif
  108. setBlockSize(EXT2_BLOCK_SIZE(&superBlock));
  109. m_blockGroupCount = ceilDiv(superBlock.s_blocks_count, superBlock.s_blocks_per_group);
  110. if (m_blockGroupCount == 0) {
  111. kprintf("ext2fs: no block groups :(\n");
  112. return false;
  113. }
  114. // Preheat the BGD cache.
  115. blockGroupDescriptor(0);
  116. #ifdef EXT2_DEBUG
  117. for (unsigned i = 1; i <= m_blockGroupCount; ++i) {
  118. auto& group = blockGroupDescriptor(i);
  119. kprintf("ext2fs: group[%u] { block_bitmap: %u, inode_bitmap: %u, inode_table: %u }\n",
  120. i,
  121. group.bg_block_bitmap,
  122. group.bg_inode_bitmap,
  123. group.bg_inode_table);
  124. }
  125. #endif
  126. return true;
  127. }
  128. const char* Ext2FileSystem::className() const
  129. {
  130. return "ext2fs";
  131. }
  132. InodeIdentifier Ext2FileSystem::rootInode() const
  133. {
  134. return { id(), EXT2_ROOT_INO };
  135. }
  136. #ifdef EXT2_DEBUG
  137. static void dumpExt2Inode(const ext2_inode& inode)
  138. {
  139. kprintf("Dump of ext2_inode:\n");
  140. kprintf(" i_size: %u\n", inode.i_size);
  141. kprintf(" i_mode: %u\n", inode.i_mode);
  142. kprintf(" i_blocks: %u\n", inode.i_blocks);
  143. kprintf(" i_uid: %u\n", inode.i_uid);
  144. kprintf(" i_gid: %u\n", inode.i_gid);
  145. }
  146. #endif
  147. ByteBuffer Ext2FileSystem::readBlockContainingInode(unsigned inode, unsigned& blockIndex, unsigned& offset) const
  148. {
  149. auto& superBlock = this->superBlock();
  150. if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&superBlock))
  151. return { };
  152. if (inode > superBlock.s_inodes_count)
  153. return { };
  154. auto& bgd = blockGroupDescriptor(groupIndexFromInode(inode));
  155. offset = ((inode - 1) % inodesPerGroup()) * inodeSize();
  156. blockIndex = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&superBlock));
  157. offset &= blockSize() - 1;
  158. return readBlock(blockIndex);
  159. }
  160. auto Ext2FileSystem::lookupExt2Inode(unsigned inode) const -> CachedExt2Inode
  161. {
  162. {
  163. LOCKER(m_inodeCacheLock);
  164. auto it = m_inodeCache.find(inode);
  165. if (it != m_inodeCache.end()) {
  166. return CachedExt2Inode{ (*it).value };
  167. }
  168. }
  169. unsigned blockIndex;
  170. unsigned offset;
  171. auto block = readBlockContainingInode(inode, blockIndex, offset);
  172. if (!block)
  173. return { };
  174. auto* e2inode = reinterpret_cast<ext2_inode*>(kmalloc(inodeSize()));
  175. memcpy(e2inode, reinterpret_cast<ext2_inode*>(block.offsetPointer(offset)), inodeSize());
  176. #ifdef EXT2_DEBUG
  177. dumpExt2Inode(*e2inode);
  178. #endif
  179. LOCKER(m_inodeCacheLock);
  180. if (m_inodeCache.size() >= 128)
  181. m_inodeCache.removeOneRandomly();
  182. auto cachedInode = adopt(*new CachedExt2InodeImpl(OwnPtr<ext2_inode>(e2inode)));
  183. m_inodeCache.set(inode, cachedInode.copyRef());
  184. return CachedExt2Inode{ cachedInode };
  185. }
  186. InodeMetadata Ext2FileSystem::inodeMetadata(InodeIdentifier inode) const
  187. {
  188. ASSERT(inode.fileSystemID() == id());
  189. auto e2inode = lookupExt2Inode(inode.index());
  190. if (!e2inode)
  191. return InodeMetadata();
  192. InodeMetadata metadata;
  193. metadata.inode = inode;
  194. metadata.size = e2inode->i_size;
  195. metadata.mode = e2inode->i_mode;
  196. metadata.uid = e2inode->i_uid;
  197. metadata.gid = e2inode->i_gid;
  198. metadata.linkCount = e2inode->i_links_count;
  199. metadata.atime = e2inode->i_atime;
  200. metadata.ctime = e2inode->i_ctime;
  201. metadata.mtime = e2inode->i_mtime;
  202. metadata.dtime = e2inode->i_dtime;
  203. metadata.blockSize = blockSize();
  204. metadata.blockCount = e2inode->i_blocks;
  205. if (isBlockDevice(e2inode->i_mode) || isCharacterDevice(e2inode->i_mode)) {
  206. unsigned dev = e2inode->i_block[0];
  207. metadata.majorDevice = (dev & 0xfff00) >> 8;
  208. metadata.minorDevice= (dev & 0xff) | ((dev >> 12) & 0xfff00);
  209. }
  210. return metadata;
  211. }
  212. Vector<unsigned> Ext2FileSystem::blockListForInode(const ext2_inode& e2inode) const
  213. {
  214. unsigned entriesPerBlock = EXT2_ADDR_PER_BLOCK(&superBlock());
  215. // NOTE: i_blocks is number of 512-byte blocks, not number of fs-blocks.
  216. unsigned blockCount = e2inode.i_blocks / (blockSize() / 512);
  217. unsigned blocksRemaining = blockCount;
  218. Vector<unsigned> list;
  219. list.ensureCapacity(blocksRemaining);
  220. unsigned directCount = min(blockCount, (unsigned)EXT2_NDIR_BLOCKS);
  221. for (unsigned i = 0; i < directCount; ++i) {
  222. list.unchecked_append(e2inode.i_block[i]);
  223. --blocksRemaining;
  224. }
  225. if (!blocksRemaining)
  226. return list;
  227. auto processBlockArray = [&] (unsigned arrayBlockIndex, auto&& callback) {
  228. auto arrayBlock = readBlock(arrayBlockIndex);
  229. ASSERT(arrayBlock);
  230. auto* array = reinterpret_cast<const __u32*>(arrayBlock.pointer());
  231. unsigned count = min(blocksRemaining, entriesPerBlock);
  232. for (unsigned i = 0; i < count; ++i) {
  233. if (!array[i]) {
  234. blocksRemaining = 0;
  235. return;
  236. }
  237. callback(array[i]);
  238. --blocksRemaining;
  239. }
  240. };
  241. processBlockArray(e2inode.i_block[EXT2_IND_BLOCK], [&] (unsigned entry) {
  242. list.unchecked_append(entry);
  243. });
  244. if (!blocksRemaining)
  245. return list;
  246. processBlockArray(e2inode.i_block[EXT2_DIND_BLOCK], [&] (unsigned entry) {
  247. processBlockArray(entry, [&] (unsigned entry) {
  248. list.unchecked_append(entry);
  249. });
  250. });
  251. if (!blocksRemaining)
  252. return list;
  253. processBlockArray(e2inode.i_block[EXT2_TIND_BLOCK], [&] (unsigned entry) {
  254. processBlockArray(entry, [&] (unsigned entry) {
  255. processBlockArray(entry, [&] (unsigned entry) {
  256. list.unchecked_append(entry);
  257. });
  258. });
  259. });
  260. return list;
  261. }
  262. Ext2Inode::Ext2Inode(Ext2FileSystem& fs, unsigned index, const ext2_inode& raw_inode)
  263. : CoreInode(fs, index)
  264. , m_raw_inode(raw_inode)
  265. {
  266. }
  267. Ext2Inode::~Ext2Inode()
  268. {
  269. }
  270. RetainPtr<CoreInode> Ext2FileSystem::get_inode(InodeIdentifier inode)
  271. {
  272. ASSERT(inode.fileSystemID() == id());
  273. {
  274. LOCKER(m_inode_cache_lock);
  275. auto it = m_inode_cache.find(inode.index());
  276. if (it != m_inode_cache.end())
  277. return (*it).value;
  278. }
  279. auto raw_inode = lookupExt2Inode(inode.index());
  280. if (!raw_inode)
  281. return nullptr;
  282. LOCKER(m_inode_cache_lock);
  283. auto it = m_inode_cache.find(inode.index());
  284. if (it != m_inode_cache.end())
  285. return (*it).value;
  286. auto new_inode = adopt(*new Ext2Inode(*this, inode.index(), *raw_inode));
  287. m_inode_cache.set(inode.index(), new_inode.copyRef());
  288. return new_inode;
  289. }
  290. Unix::ssize_t Ext2Inode::read_bytes(Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*)
  291. {
  292. ASSERT(offset >= 0);
  293. if (m_raw_inode.i_size == 0)
  294. return 0;
  295. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  296. // This avoids wasting an entire block on short links. (Most links are short.)
  297. static const unsigned max_inline_symlink_length = 60;
  298. if (is_symlink() && size() < max_inline_symlink_length) {
  299. Unix::ssize_t nread = min((Unix::off_t)size() - offset, static_cast<Unix::off_t>(count));
  300. memcpy(buffer, m_raw_inode.i_block + offset, nread);
  301. return nread;
  302. }
  303. if (m_block_list.isEmpty()) {
  304. auto block_list = fs().blockListForInode(m_raw_inode);
  305. LOCKER(m_lock);
  306. if (m_block_list.size() != block_list.size())
  307. m_block_list = move(block_list);
  308. }
  309. if (m_block_list.isEmpty()) {
  310. kprintf("ext2fs: read_bytes: empty block list for inode %u\n", index());
  311. return -EIO;
  312. }
  313. const size_t block_size = fs().blockSize();
  314. dword first_block_logical_index = offset / block_size;
  315. dword last_block_logical_index = (offset + count) / block_size;
  316. if (last_block_logical_index >= m_block_list.size())
  317. last_block_logical_index = m_block_list.size() - 1;
  318. dword offset_into_first_block = offset % block_size;
  319. Unix::ssize_t nread = 0;
  320. Unix::size_t remaining_count = min((Unix::off_t)count, (Unix::off_t)size() - offset);
  321. byte* out = buffer;
  322. #ifdef EXT2_DEBUG
  323. kprintf("ok let's do it, read(%llu, %u) -> blocks %u thru %u, oifb: %u\n", offset, count, firstBlockLogicalIndex, lastBlockLogicalIndex, offsetIntoFirstBlock);
  324. #endif
  325. for (dword bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  326. auto block = fs().readBlock(m_block_list[bi]);
  327. if (!block) {
  328. kprintf("ext2fs: read_bytes: readBlock(%u) failed (lbi: %u)\n", m_block_list[bi], bi);
  329. return -EIO;
  330. }
  331. dword offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  332. dword num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  333. memcpy(out, block.pointer() + offset_into_block, num_bytes_to_copy);
  334. remaining_count -= num_bytes_to_copy;
  335. nread += num_bytes_to_copy;
  336. out += num_bytes_to_copy;
  337. }
  338. return nread;
  339. }
  340. Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const
  341. {
  342. ASSERT(offset >= 0);
  343. ASSERT(inode.fileSystemID() == id());
  344. auto e2inode = lookupExt2Inode(inode.index());
  345. if (!e2inode) {
  346. kprintf("ext2fs: readInodeBytes: metadata lookup for inode %u failed\n", inode.index());
  347. return -EIO;
  348. }
  349. #if 0
  350. // FIXME: We can't fail here while the directory traversal depends on this function. :]
  351. if (isDirectory(e2inode->i_mode))
  352. return -EISDIR;
  353. #endif
  354. if (e2inode->i_size == 0)
  355. return 0;
  356. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  357. // This avoids wasting an entire block on short links. (Most links are short.)
  358. static const unsigned maxInlineSymlinkLength = 60;
  359. if (isSymbolicLink(e2inode->i_mode) && e2inode->i_size < maxInlineSymlinkLength) {
  360. Unix::ssize_t nread = min((Unix::off_t)e2inode->i_size - offset, static_cast<Unix::off_t>(count));
  361. memcpy(buffer, e2inode->i_block + offset, nread);
  362. return nread;
  363. }
  364. // FIXME: It's grossly inefficient to fetch the blocklist on every call to readInodeBytes().
  365. // It needs to be cached!
  366. auto list = blockListForInode(*e2inode);
  367. if (list.isEmpty()) {
  368. kprintf("ext2fs: readInodeBytes: empty block list for inode %u\n", inode.index());
  369. return -EIO;
  370. }
  371. dword firstBlockLogicalIndex = offset / blockSize();
  372. dword lastBlockLogicalIndex = (offset + count) / blockSize();
  373. if (lastBlockLogicalIndex >= list.size())
  374. lastBlockLogicalIndex = list.size() - 1;
  375. dword offsetIntoFirstBlock = offset % blockSize();
  376. Unix::ssize_t nread = 0;
  377. Unix::size_t remainingCount = min((Unix::off_t)count, (Unix::off_t)e2inode->i_size - offset);
  378. byte* out = buffer;
  379. #ifdef EXT2_DEBUG
  380. kprintf("ok let's do it, read(%llu, %u) -> blocks %u thru %u, oifb: %u\n", offset, count, firstBlockLogicalIndex, lastBlockLogicalIndex, offsetIntoFirstBlock);
  381. #endif
  382. for (dword bi = firstBlockLogicalIndex; bi <= lastBlockLogicalIndex; ++bi) {
  383. auto block = readBlock(list[bi]);
  384. if (!block) {
  385. kprintf("ext2fs: readInodeBytes: readBlock(%u) failed (lbi: %u)\n", list[bi], bi);
  386. return -EIO;
  387. }
  388. dword offsetIntoBlock;
  389. if (bi == firstBlockLogicalIndex)
  390. offsetIntoBlock = offsetIntoFirstBlock;
  391. else
  392. offsetIntoBlock = 0;
  393. dword numBytesToCopy = min(blockSize() - offsetIntoBlock, remainingCount);
  394. memcpy(out, block.pointer() + offsetIntoBlock, numBytesToCopy);
  395. remainingCount -= numBytesToCopy;
  396. nread += numBytesToCopy;
  397. out += numBytesToCopy;
  398. }
  399. return nread;
  400. }
  401. bool Ext2FileSystem::writeInode(InodeIdentifier inode, const ByteBuffer& data)
  402. {
  403. ASSERT(inode.fileSystemID() == id());
  404. auto e2inode = lookupExt2Inode(inode.index());
  405. if (!e2inode) {
  406. kprintf("ext2fs: writeInode: metadata lookup for inode %u failed\n", inode.index());
  407. return false;
  408. }
  409. // FIXME: Support writing to symlink inodes.
  410. ASSERT(!isSymbolicLink(e2inode->i_mode));
  411. unsigned blocksNeededBefore = ceilDiv(e2inode->i_size, blockSize());
  412. unsigned blocksNeededAfter = ceilDiv((unsigned)data.size(), blockSize());
  413. // FIXME: Support growing or shrinking the block list.
  414. ASSERT(blocksNeededBefore == blocksNeededAfter);
  415. auto list = blockListForInode(*e2inode);
  416. if (list.isEmpty()) {
  417. kprintf("ext2fs: writeInode: empty block list for inode %u\n", inode.index());
  418. return false;
  419. }
  420. for (unsigned i = 0; i < list.size(); ++i) {
  421. auto section = data.slice(i * blockSize(), blockSize());
  422. kprintf("section = %p (%u)\n", section.pointer(), section.size());
  423. bool success = writeBlock(list[i], section);
  424. ASSERT(success);
  425. }
  426. return true;
  427. }
  428. bool Ext2FileSystem::enumerateDirectoryInode(InodeIdentifier inode, Function<bool(const DirectoryEntry&)> callback) const
  429. {
  430. ASSERT(inode.fileSystemID() == id());
  431. ASSERT(isDirectoryInode(inode.index()));
  432. #ifdef EXT2_DEBUG
  433. kprintf("ext2fs: Enumerating directory contents of inode %u:\n", inode.index());
  434. #endif
  435. auto buffer = readEntireInode(inode);
  436. ASSERT(buffer);
  437. auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.pointer());
  438. while (entry < buffer.endPointer()) {
  439. if (entry->inode != 0) {
  440. #ifdef EXT2_DEBUG
  441. kprintf("inode: %u, name_len: %u, rec_len: %u, file_type: %u, name: %s\n", entry->inode, entry->name_len, entry->rec_len, entry->file_type, namebuf);
  442. #endif
  443. if (!callback({ entry->name, entry->name_len, { id(), entry->inode }, entry->file_type }))
  444. break;
  445. }
  446. entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
  447. }
  448. return true;
  449. }
  450. bool Ext2FileSystem::addInodeToDirectory(unsigned directoryInode, unsigned inode, const String& name, byte fileType)
  451. {
  452. auto e2inodeForDirectory = lookupExt2Inode(directoryInode);
  453. ASSERT(e2inodeForDirectory);
  454. ASSERT(isDirectory(e2inodeForDirectory->i_mode));
  455. //#ifdef EXT2_DEBUG
  456. kprintf("ext2fs: Adding inode %u with name '%s' to directory %u\n", inode, name.characters(), directoryInode);
  457. //#endif
  458. Vector<DirectoryEntry> entries;
  459. bool nameAlreadyExists = false;
  460. enumerateDirectoryInode({ id(), directoryInode }, [&] (const DirectoryEntry& entry) {
  461. if (!strcmp(entry.name, name.characters())) {
  462. nameAlreadyExists = true;
  463. return false;
  464. }
  465. entries.append(entry);
  466. return true;
  467. });
  468. if (nameAlreadyExists) {
  469. kprintf("ext2fs: Name '%s' already exists in directory inode %u\n", name.characters(), directoryInode);
  470. return false;
  471. }
  472. entries.append({ name.characters(), name.length(), { id(), inode }, fileType });
  473. return writeDirectoryInode(directoryInode, move(entries));
  474. }
  475. bool Ext2FileSystem::writeDirectoryInode(unsigned directoryInode, Vector<DirectoryEntry>&& entries)
  476. {
  477. kprintf("ext2fs: New directory inode %u contents to write:\n", directoryInode);
  478. unsigned directorySize = 0;
  479. for (auto& entry : entries) {
  480. kprintf(" - %08u %s\n", entry.inode.index(), entry.name);
  481. directorySize += EXT2_DIR_REC_LEN(entry.name_length);
  482. }
  483. unsigned blocksNeeded = ceilDiv(directorySize, blockSize());
  484. unsigned occupiedSize = blocksNeeded * blockSize();
  485. kprintf("ext2fs: directory size: %u (occupied: %u)\n", directorySize, occupiedSize);
  486. auto directoryData = ByteBuffer::createUninitialized(occupiedSize);
  487. BufferStream stream(directoryData);
  488. for (unsigned i = 0; i < entries.size(); ++i) {
  489. auto& entry = entries[i];
  490. unsigned recordLength = EXT2_DIR_REC_LEN(entry.name_length);
  491. if (i == entries.size() - 1)
  492. recordLength += occupiedSize - directorySize;
  493. kprintf("* inode: %u", entry.inode.index());
  494. kprintf(", name_len: %u", word(entry.name_length));
  495. kprintf(", rec_len: %u", word(recordLength));
  496. kprintf(", file_type: %u", byte(entry.fileType));
  497. kprintf(", name: %s\n", entry.name);
  498. stream << dword(entry.inode.index());
  499. stream << word(recordLength);
  500. stream << byte(entry.name_length);
  501. stream << byte(entry.fileType);
  502. stream << entry.name;
  503. unsigned padding = recordLength - entry.name_length - 8;
  504. kprintf(" *** pad %u bytes\n", padding);
  505. for (unsigned j = 0; j < padding; ++j) {
  506. stream << byte(0);
  507. }
  508. }
  509. stream.fillToEnd(0);
  510. #if 0
  511. kprintf("data to write (%u):\n", directoryData.size());
  512. for (unsigned i = 0; i < directoryData.size(); ++i) {
  513. kprintf("%02x ", directoryData[i]);
  514. if ((i + 1) % 8 == 0)
  515. kprintf(" ");
  516. if ((i + 1) % 16 == 0)
  517. kprintf("\n");
  518. }
  519. kprintf("\n");
  520. #endif
  521. writeInode({ id(), directoryInode }, directoryData);
  522. return true;
  523. }
  524. unsigned Ext2FileSystem::inodesPerBlock() const
  525. {
  526. return EXT2_INODES_PER_BLOCK(&superBlock());
  527. }
  528. unsigned Ext2FileSystem::inodesPerGroup() const
  529. {
  530. return EXT2_INODES_PER_GROUP(&superBlock());
  531. }
  532. unsigned Ext2FileSystem::inodeSize() const
  533. {
  534. return EXT2_INODE_SIZE(&superBlock());
  535. }
  536. unsigned Ext2FileSystem::blocksPerGroup() const
  537. {
  538. return EXT2_BLOCKS_PER_GROUP(&superBlock());
  539. }
  540. void Ext2FileSystem::dumpBlockBitmap(unsigned groupIndex) const
  541. {
  542. ASSERT(groupIndex <= m_blockGroupCount);
  543. auto& bgd = blockGroupDescriptor(groupIndex);
  544. unsigned blocksInGroup = min(blocksPerGroup(), superBlock().s_blocks_count);
  545. unsigned blockCount = ceilDiv(blocksInGroup, 8u);
  546. auto bitmapBlocks = readBlocks(bgd.bg_block_bitmap, blockCount);
  547. ASSERT(bitmapBlocks);
  548. kprintf("ext2fs: group[%u] block bitmap (bitmap occupies %u blocks):\n", groupIndex, blockCount);
  549. auto bitmap = Bitmap::wrap(bitmapBlocks.pointer(), blocksInGroup);
  550. for (unsigned i = 0; i < blocksInGroup; ++i) {
  551. kprintf("%c", bitmap.get(i) ? '1' : '0');
  552. }
  553. kprintf("\n");
  554. }
  555. void Ext2FileSystem::dumpInodeBitmap(unsigned groupIndex) const
  556. {
  557. traverseInodeBitmap(groupIndex, [] (unsigned, const Bitmap& bitmap) {
  558. for (unsigned i = 0; i < bitmap.size(); ++i)
  559. kprintf("%c", bitmap.get(i) ? '1' : '0');
  560. return true;
  561. });
  562. }
  563. template<typename F>
  564. void Ext2FileSystem::traverseInodeBitmap(unsigned groupIndex, F callback) const
  565. {
  566. ASSERT(groupIndex <= m_blockGroupCount);
  567. auto& bgd = blockGroupDescriptor(groupIndex);
  568. unsigned inodesInGroup = min(inodesPerGroup(), superBlock().s_inodes_count);
  569. unsigned blockCount = ceilDiv(inodesInGroup, 8u);
  570. for (unsigned i = 0; i < blockCount; ++i) {
  571. auto block = readBlock(bgd.bg_inode_bitmap + i);
  572. ASSERT(block);
  573. bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), inodesInGroup));
  574. if (!shouldContinue)
  575. break;
  576. }
  577. }
  578. template<typename F>
  579. void Ext2FileSystem::traverseBlockBitmap(unsigned groupIndex, F callback) const
  580. {
  581. ASSERT(groupIndex <= m_blockGroupCount);
  582. auto& bgd = blockGroupDescriptor(groupIndex);
  583. unsigned blocksInGroup = min(blocksPerGroup(), superBlock().s_blocks_count);
  584. unsigned blockCount = ceilDiv(blocksInGroup, 8u);
  585. for (unsigned i = 0; i < blockCount; ++i) {
  586. auto block = readBlock(bgd.bg_block_bitmap + i);
  587. ASSERT(block);
  588. bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), blocksInGroup));
  589. if (!shouldContinue)
  590. break;
  591. }
  592. }
  593. bool Ext2FileSystem::modifyLinkCount(InodeIndex inode, int delta)
  594. {
  595. ASSERT(inode);
  596. auto e2inode = lookupExt2Inode(inode);
  597. if (!e2inode)
  598. return false;
  599. auto newLinkCount = e2inode->i_links_count + delta;
  600. kprintf("changing inode %u link count from %u to %u\n", inode, e2inode->i_links_count, newLinkCount);
  601. e2inode->i_links_count = newLinkCount;
  602. return writeExt2Inode(inode, *e2inode);
  603. }
  604. bool Ext2FileSystem::setModificationTime(InodeIdentifier inode, dword timestamp)
  605. {
  606. ASSERT(inode.fileSystemID() == id());
  607. auto e2inode = lookupExt2Inode(inode.index());
  608. if (!e2inode)
  609. return false;
  610. kprintf("changing inode %u mtime from %u to %u\n", inode.index(), e2inode->i_mtime, timestamp);
  611. e2inode->i_mtime = timestamp;
  612. return writeExt2Inode(inode.index(), *e2inode);
  613. }
  614. bool Ext2FileSystem::writeExt2Inode(unsigned inode, const ext2_inode& e2inode)
  615. {
  616. unsigned blockIndex;
  617. unsigned offset;
  618. auto block = readBlockContainingInode(inode, blockIndex, offset);
  619. if (!block)
  620. return false;
  621. memcpy(reinterpret_cast<ext2_inode*>(block.offsetPointer(offset)), &e2inode, inodeSize());
  622. writeBlock(blockIndex, block);
  623. return true;
  624. }
  625. bool Ext2FileSystem::isDirectoryInode(unsigned inode) const
  626. {
  627. if (auto e2inode = lookupExt2Inode(inode))
  628. return isDirectory(e2inode->i_mode);
  629. return false;
  630. }
  631. Vector<Ext2FileSystem::BlockIndex> Ext2FileSystem::allocateBlocks(unsigned group, unsigned count)
  632. {
  633. kprintf("ext2fs: allocateBlocks(group: %u, count: %u)\n", group, count);
  634. auto& bgd = blockGroupDescriptor(group);
  635. if (bgd.bg_free_blocks_count < count) {
  636. kprintf("ext2fs: allocateBlocks can't allocate out of group %u, wanted %u but only %u available\n", group, count, bgd.bg_free_blocks_count);
  637. return { };
  638. }
  639. // FIXME: Implement a scan that finds consecutive blocks if possible.
  640. Vector<BlockIndex> blocks;
  641. traverseBlockBitmap(group, [&blocks, count] (unsigned firstBlockInBitmap, const Bitmap& bitmap) {
  642. for (unsigned i = 0; i < bitmap.size(); ++i) {
  643. if (!bitmap.get(i)) {
  644. blocks.append(firstBlockInBitmap + i);
  645. if (blocks.size() == count)
  646. return false;
  647. }
  648. }
  649. return true;
  650. });
  651. kprintf("ext2fs: allocateBlock found these blocks:\n");
  652. for (auto& bi : blocks) {
  653. kprintf(" > %u\n", bi);
  654. }
  655. return blocks;
  656. }
  657. unsigned Ext2FileSystem::allocateInode(unsigned preferredGroup, unsigned expectedSize)
  658. {
  659. kprintf("ext2fs: allocateInode(preferredGroup: %u, expectedSize: %u)\n", preferredGroup, expectedSize);
  660. unsigned neededBlocks = ceilDiv(expectedSize, blockSize());
  661. kprintf("ext2fs: minimum needed blocks: %u\n", neededBlocks);
  662. unsigned groupIndex = 0;
  663. auto isSuitableGroup = [this, neededBlocks] (unsigned groupIndex) {
  664. auto& bgd = blockGroupDescriptor(groupIndex);
  665. return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= neededBlocks;
  666. };
  667. if (preferredGroup && isSuitableGroup(preferredGroup)) {
  668. groupIndex = preferredGroup;
  669. } else {
  670. for (unsigned i = 1; i <= m_blockGroupCount; ++i) {
  671. if (isSuitableGroup(i))
  672. groupIndex = i;
  673. }
  674. }
  675. if (!groupIndex) {
  676. kprintf("ext2fs: allocateInode: no suitable group found for new inode with %u blocks needed :(\n", neededBlocks);
  677. return 0;
  678. }
  679. kprintf("ext2fs: allocateInode: found suitable group [%u] for new inode with %u blocks needed :^)\n", groupIndex, neededBlocks);
  680. unsigned firstFreeInodeInGroup = 0;
  681. traverseInodeBitmap(groupIndex, [&firstFreeInodeInGroup] (unsigned firstInodeInBitmap, const Bitmap& bitmap) {
  682. for (unsigned i = 0; i < bitmap.size(); ++i) {
  683. if (!bitmap.get(i)) {
  684. firstFreeInodeInGroup = firstInodeInBitmap + i;
  685. return false;
  686. }
  687. }
  688. return true;
  689. });
  690. if (!firstFreeInodeInGroup) {
  691. kprintf("ext2fs: firstFreeInodeInGroup returned no inode, despite bgd claiming there are inodes :(\n");
  692. return 0;
  693. }
  694. unsigned inode = firstFreeInodeInGroup;
  695. kprintf("ext2fs: found suitable inode %u\n", inode);
  696. // FIXME: allocate blocks if needed!
  697. return inode;
  698. }
  699. unsigned Ext2FileSystem::groupIndexFromInode(unsigned inode) const
  700. {
  701. if (!inode)
  702. return 0;
  703. return (inode - 1) / inodesPerGroup() + 1;
  704. }
  705. bool Ext2FileSystem::setInodeAllocationState(unsigned inode, bool newState)
  706. {
  707. auto& bgd = blockGroupDescriptor(groupIndexFromInode(inode));
  708. // Update inode bitmap
  709. unsigned inodesPerBitmapBlock = blockSize() * 8;
  710. unsigned bitmapBlockIndex = (inode - 1) / inodesPerBitmapBlock;
  711. unsigned bitIndex = (inode - 1) % inodesPerBitmapBlock;
  712. auto block = readBlock(bgd.bg_inode_bitmap + bitmapBlockIndex);
  713. ASSERT(block);
  714. auto bitmap = Bitmap::wrap(block.pointer(), block.size());
  715. bool currentState = bitmap.get(bitIndex);
  716. kprintf("ext2fs: setInodeAllocationState(%u) %u -> %u\n", inode, currentState, newState);
  717. if (currentState == newState)
  718. return true;
  719. bitmap.set(bitIndex, newState);
  720. writeBlock(bgd.bg_inode_bitmap + bitmapBlockIndex, block);
  721. // Update superblock
  722. auto& sb = *reinterpret_cast<ext2_super_block*>(m_cachedSuperBlock.pointer());
  723. kprintf("ext2fs: superblock free inode count %u -> %u\n", sb.s_free_inodes_count, sb.s_free_inodes_count - 1);
  724. if (newState)
  725. --sb.s_free_inodes_count;
  726. else
  727. ++sb.s_free_inodes_count;
  728. writeSuperBlock(sb);
  729. // Update BGD
  730. auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
  731. if (newState)
  732. --mutableBGD.bg_free_inodes_count;
  733. else
  734. ++mutableBGD.bg_free_inodes_count;
  735. kprintf("ext2fs: group free inode count %u -> %u\n", bgd.bg_free_inodes_count, bgd.bg_free_inodes_count - 1);
  736. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  737. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  738. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cachedBlockGroupDescriptorTable);
  739. return true;
  740. }
  741. bool Ext2FileSystem::setBlockAllocationState(GroupIndex group, BlockIndex bi, bool newState)
  742. {
  743. auto& bgd = blockGroupDescriptor(group);
  744. // Update block bitmap
  745. unsigned blocksPerBitmapBlock = blockSize() * 8;
  746. unsigned bitmapBlockIndex = (bi - 1) / blocksPerBitmapBlock;
  747. unsigned bitIndex = (bi - 1) % blocksPerBitmapBlock;
  748. auto block = readBlock(bgd.bg_block_bitmap + bitmapBlockIndex);
  749. ASSERT(block);
  750. auto bitmap = Bitmap::wrap(block.pointer(), block.size());
  751. bool currentState = bitmap.get(bitIndex);
  752. kprintf("ext2fs: setBlockAllocationState(%u) %u -> %u\n", bi, currentState, newState);
  753. if (currentState == newState)
  754. return true;
  755. bitmap.set(bitIndex, newState);
  756. writeBlock(bgd.bg_block_bitmap + bitmapBlockIndex, block);
  757. // Update superblock
  758. auto& sb = *reinterpret_cast<ext2_super_block*>(m_cachedSuperBlock.pointer());
  759. kprintf("ext2fs: superblock free block count %u -> %u\n", sb.s_free_blocks_count, sb.s_free_blocks_count - 1);
  760. if (newState)
  761. --sb.s_free_blocks_count;
  762. else
  763. ++sb.s_free_blocks_count;
  764. writeSuperBlock(sb);
  765. // Update BGD
  766. auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
  767. if (newState)
  768. --mutableBGD.bg_free_blocks_count;
  769. else
  770. ++mutableBGD.bg_free_blocks_count;
  771. kprintf("ext2fs: group free block count %u -> %u\n", bgd.bg_free_blocks_count, bgd.bg_free_blocks_count - 1);
  772. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  773. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  774. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cachedBlockGroupDescriptorTable);
  775. return true;
  776. }
  777. InodeIdentifier Ext2FileSystem::makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t mode)
  778. {
  779. ASSERT(parentInode.fileSystemID() == id());
  780. ASSERT(isDirectoryInode(parentInode.index()));
  781. // Fix up the mode to definitely be a directory.
  782. // FIXME: This is a bit on the hackish side.
  783. mode &= ~0170000;
  784. mode |= 0040000;
  785. // NOTE: When creating a new directory, make the size 1 block.
  786. // There's probably a better strategy here, but this works for now.
  787. auto inode = createInode(parentInode, name, mode, blockSize());
  788. if (!inode.isValid())
  789. return { };
  790. kprintf("ext2fs: makeDirectory: created new directory named '%s' with inode %u\n", name.characters(), inode.index());
  791. Vector<DirectoryEntry> entries;
  792. entries.append({ ".", inode, EXT2_FT_DIR });
  793. entries.append({ "..", parentInode, EXT2_FT_DIR });
  794. bool success = writeDirectoryInode(inode.index(), move(entries));
  795. ASSERT(success);
  796. success = modifyLinkCount(parentInode.index(), 1);
  797. ASSERT(success);
  798. auto& bgd = const_cast<ext2_group_desc&>(blockGroupDescriptor(groupIndexFromInode(inode.index())));
  799. ++bgd.bg_used_dirs_count;
  800. kprintf("ext2fs: incremented bg_used_dirs_count %u -> %u\n", bgd.bg_used_dirs_count - 1, bgd.bg_used_dirs_count);
  801. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  802. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  803. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cachedBlockGroupDescriptorTable);
  804. return inode;
  805. }
  806. InodeIdentifier Ext2FileSystem::createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, unsigned size)
  807. {
  808. ASSERT(parentInode.fileSystemID() == id());
  809. ASSERT(isDirectoryInode(parentInode.index()));
  810. //#ifdef EXT2_DEBUG
  811. kprintf("ext2fs: Adding inode '%s' (mode %o) to parent directory %u:\n", name.characters(), mode, parentInode.index());
  812. //#endif
  813. // NOTE: This doesn't commit the inode allocation just yet!
  814. auto inode = allocateInode(0, 0);
  815. if (!inode) {
  816. kprintf("ext2fs: createInode: allocateInode failed\n");
  817. return { };
  818. }
  819. auto blocks = allocateBlocks(groupIndexFromInode(inode), ceilDiv(size, blockSize()));
  820. if (blocks.isEmpty()) {
  821. kprintf("ext2fs: createInode: allocateBlocks failed\n");
  822. return { };
  823. }
  824. byte fileType = 0;
  825. if (isRegularFile(mode))
  826. fileType = EXT2_FT_REG_FILE;
  827. else if (isDirectory(mode))
  828. fileType = EXT2_FT_DIR;
  829. else if (isCharacterDevice(mode))
  830. fileType = EXT2_FT_CHRDEV;
  831. else if (isBlockDevice(mode))
  832. fileType = EXT2_FT_BLKDEV;
  833. else if (isFIFO(mode))
  834. fileType = EXT2_FT_FIFO;
  835. else if (isSocket(mode))
  836. fileType = EXT2_FT_SOCK;
  837. else if (isSymbolicLink(mode))
  838. fileType = EXT2_FT_SYMLINK;
  839. // Try adding it to the directory first, in case the name is already in use.
  840. bool success = addInodeToDirectory(parentInode.index(), inode, name, fileType);
  841. if (!success) {
  842. kprintf("ext2fs: failed to add inode to directory :(\n");
  843. return { };
  844. }
  845. // Looks like we're good, time to update the inode bitmap and group+global inode counters.
  846. success = setInodeAllocationState(inode, true);
  847. ASSERT(success);
  848. for (auto bi : blocks) {
  849. success = setBlockAllocationState(groupIndexFromInode(inode), bi, true);
  850. ASSERT(success);
  851. }
  852. unsigned initialLinksCount;
  853. if (isDirectory(mode))
  854. initialLinksCount = 2; // (parent directory + "." entry in self)
  855. else
  856. initialLinksCount = 1;
  857. auto timestamp = ktime(nullptr);
  858. auto e2inode = make<ext2_inode>();
  859. memset(e2inode.ptr(), 0, sizeof(ext2_inode));
  860. e2inode->i_mode = mode;
  861. e2inode->i_uid = 0;
  862. e2inode->i_size = size;
  863. e2inode->i_atime = timestamp;
  864. e2inode->i_ctime = timestamp;
  865. e2inode->i_mtime = timestamp;
  866. e2inode->i_dtime = 0;
  867. e2inode->i_gid = 0;
  868. e2inode->i_links_count = initialLinksCount;
  869. e2inode->i_blocks = blocks.size() * (blockSize() / 512);
  870. // FIXME: Implement writing out indirect blocks!
  871. ASSERT(blocks.size() < EXT2_NDIR_BLOCKS);
  872. kprintf("[XXX] writing %zu blocks to i_block array\n", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()));
  873. for (unsigned i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i) {
  874. e2inode->i_block[i] = blocks[i];
  875. }
  876. e2inode->i_flags = 0;
  877. success = writeExt2Inode(inode, *e2inode);
  878. ASSERT(success);
  879. return { id(), inode };
  880. }
  881. InodeIdentifier Ext2FileSystem::findParentOfInode(InodeIdentifier inode) const
  882. {
  883. ASSERT(inode.fileSystemID() == id());
  884. unsigned groupIndex = groupIndexFromInode(inode.index());
  885. unsigned firstInodeInGroup = inodesPerGroup() * (groupIndex - 1);
  886. Vector<InodeIdentifier> directoriesInGroup;
  887. for (unsigned i = 0; i < inodesPerGroup(); ++i) {
  888. auto e2inode = lookupExt2Inode(firstInodeInGroup + i);
  889. if (!e2inode)
  890. continue;
  891. if (isDirectory(e2inode->i_mode)) {
  892. directoriesInGroup.append({ id(), firstInodeInGroup + i });
  893. }
  894. }
  895. InodeIdentifier foundParent;
  896. for (auto& directory : directoriesInGroup) {
  897. enumerateDirectoryInode(directory, [inode, directory, &foundParent] (auto& entry) {
  898. if (entry.inode == inode) {
  899. foundParent = directory;
  900. return false;
  901. }
  902. return true;
  903. });
  904. if (foundParent.isValid())
  905. break;
  906. }
  907. return foundParent;
  908. }