Ext2FileSystem.cpp 33 KB

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