Ext2FileSystem.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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.ensure_capacity(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. if (is_directory()) {
  211. // FIXME: This invalidation is way too hardcore.
  212. LOCKER(m_lock);
  213. m_lookup_cache.clear();
  214. }
  215. set_metadata_dirty(false);
  216. }
  217. RetainPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
  218. {
  219. ASSERT(inode.fsid() == id());
  220. {
  221. LOCKER(m_inode_cache_lock);
  222. auto it = m_inode_cache.find(inode.index());
  223. if (it != m_inode_cache.end())
  224. return (*it).value;
  225. }
  226. if (!get_inode_allocation_state(inode.index())) {
  227. LOCKER(m_inode_cache_lock);
  228. m_inode_cache.set(inode.index(), nullptr);
  229. return nullptr;
  230. }
  231. unsigned block_index;
  232. unsigned offset;
  233. auto block = read_block_containing_inode(inode.index(), block_index, offset);
  234. if (!block)
  235. return { };
  236. // FIXME: Avoid this extra allocation, copy the raw inode directly into the Ext2FSInode metadata somehow.
  237. auto* e2inode = reinterpret_cast<ext2_inode*>(kmalloc(inode_size()));
  238. memcpy(e2inode, reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), inode_size());
  239. auto raw_inode = OwnPtr<ext2_inode>(e2inode);
  240. if (!raw_inode)
  241. return nullptr;
  242. LOCKER(m_inode_cache_lock);
  243. auto it = m_inode_cache.find(inode.index());
  244. if (it != m_inode_cache.end())
  245. return (*it).value;
  246. auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index(), *raw_inode));
  247. m_inode_cache.set(inode.index(), new_inode.copyRef());
  248. return new_inode;
  249. }
  250. ssize_t Ext2FSInode::read_bytes(Unix::off_t offset, size_t count, byte* buffer, FileDescriptor*)
  251. {
  252. ASSERT(offset >= 0);
  253. if (m_raw_inode.i_size == 0)
  254. return 0;
  255. // Symbolic links shorter than 60 characters are store inline inside the i_block array.
  256. // This avoids wasting an entire block on short links. (Most links are short.)
  257. static const unsigned max_inline_symlink_length = 60;
  258. if (is_symlink() && size() < max_inline_symlink_length) {
  259. ssize_t nread = min((Unix::off_t)size() - offset, static_cast<Unix::off_t>(count));
  260. memcpy(buffer, m_raw_inode.i_block + offset, nread);
  261. return nread;
  262. }
  263. if (m_block_list.is_empty()) {
  264. auto block_list = fs().block_list_for_inode(m_raw_inode);
  265. LOCKER(m_lock);
  266. if (m_block_list.size() != block_list.size())
  267. m_block_list = move(block_list);
  268. }
  269. if (m_block_list.is_empty()) {
  270. kprintf("ext2fs: read_bytes: empty block list for inode %u\n", index());
  271. return -EIO;
  272. }
  273. const size_t block_size = fs().blockSize();
  274. dword first_block_logical_index = offset / block_size;
  275. dword last_block_logical_index = (offset + count) / block_size;
  276. if (last_block_logical_index >= m_block_list.size())
  277. last_block_logical_index = m_block_list.size() - 1;
  278. dword offset_into_first_block = offset % block_size;
  279. ssize_t nread = 0;
  280. size_t remaining_count = min((Unix::off_t)count, (Unix::off_t)size() - offset);
  281. byte* out = buffer;
  282. #ifdef EXT2_DEBUG
  283. kprintf("Ext2FS: Reading %u bytes %d bytes into inode %u:%u to %p\n", count, offset, identifier().fsid(), identifier().index(), buffer);
  284. //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);
  285. #endif
  286. for (dword bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
  287. auto block = fs().readBlock(m_block_list[bi]);
  288. if (!block) {
  289. kprintf("ext2fs: read_bytes: readBlock(%u) failed (lbi: %u)\n", m_block_list[bi], bi);
  290. return -EIO;
  291. }
  292. dword offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
  293. dword num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
  294. memcpy(out, block.pointer() + offset_into_block, num_bytes_to_copy);
  295. remaining_count -= num_bytes_to_copy;
  296. nread += num_bytes_to_copy;
  297. out += num_bytes_to_copy;
  298. }
  299. return nread;
  300. }
  301. bool Ext2FSInode::write(const ByteBuffer& data)
  302. {
  303. // FIXME: Support writing to symlink inodes.
  304. ASSERT(!is_symlink());
  305. unsigned blocksNeededBefore = ceilDiv(size(), fs().blockSize());
  306. unsigned blocksNeededAfter = ceilDiv((unsigned)data.size(), fs().blockSize());
  307. // FIXME: Support growing or shrinking the block list.
  308. ASSERT(blocksNeededBefore == blocksNeededAfter);
  309. auto list = fs().block_list_for_inode(m_raw_inode);
  310. if (list.is_empty()) {
  311. kprintf("ext2fs: writeInode: empty block list for inode %u\n", index());
  312. return false;
  313. }
  314. for (unsigned i = 0; i < list.size(); ++i) {
  315. auto section = data.slice(i * fs().blockSize(), fs().blockSize());
  316. //kprintf("section = %p (%u)\n", section.pointer(), section.size());
  317. bool success = fs().writeBlock(list[i], section);
  318. ASSERT(success);
  319. }
  320. return true;
  321. }
  322. bool Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback)
  323. {
  324. ASSERT(metadata().isDirectory());
  325. #ifdef EXT2_DEBUG
  326. kprintf("Ext2Inode::traverse_as_directory: inode=%u:\n", index());
  327. #endif
  328. auto buffer = read_entire();
  329. ASSERT(buffer);
  330. auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.pointer());
  331. while (entry < buffer.end_pointer()) {
  332. if (entry->inode != 0) {
  333. #ifdef EXT2_DEBUG
  334. 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);
  335. #endif
  336. if (!callback({ entry->name, entry->name_len, { fsid(), entry->inode }, entry->file_type }))
  337. break;
  338. }
  339. entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
  340. }
  341. return true;
  342. }
  343. bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
  344. {
  345. ASSERT(is_directory());
  346. //#ifdef EXT2_DEBUG
  347. dbgprintf("Ext2FS: Adding inode %u with name '%s' to directory %u\n", child_id.index(), name.characters(), index());
  348. //#endif
  349. Vector<FS::DirectoryEntry> entries;
  350. bool name_already_exists = false;
  351. traverse_as_directory([&] (auto& entry) {
  352. if (!strcmp(entry.name, name.characters())) {
  353. name_already_exists = true;
  354. return false;
  355. }
  356. entries.append(entry);
  357. return true;
  358. });
  359. if (name_already_exists) {
  360. kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
  361. error = -EEXIST;
  362. return false;
  363. }
  364. entries.append({ name.characters(), name.length(), child_id, file_type });
  365. return fs().write_directory_inode(index(), move(entries));
  366. }
  367. bool Ext2FS::write_directory_inode(unsigned directoryInode, Vector<DirectoryEntry>&& entries)
  368. {
  369. dbgprintf("Ext2FS: New directory inode %u contents to write:\n", directoryInode);
  370. unsigned directorySize = 0;
  371. for (auto& entry : entries) {
  372. //kprintf(" - %08u %s\n", entry.inode.index(), entry.name);
  373. directorySize += EXT2_DIR_REC_LEN(entry.name_length);
  374. }
  375. unsigned blocksNeeded = ceilDiv(directorySize, blockSize());
  376. unsigned occupiedSize = blocksNeeded * blockSize();
  377. dbgprintf("Ext2FS: directory size: %u (occupied: %u)\n", directorySize, occupiedSize);
  378. auto directoryData = ByteBuffer::create_uninitialized(occupiedSize);
  379. BufferStream stream(directoryData);
  380. for (unsigned i = 0; i < entries.size(); ++i) {
  381. auto& entry = entries[i];
  382. unsigned recordLength = EXT2_DIR_REC_LEN(entry.name_length);
  383. if (i == entries.size() - 1)
  384. recordLength += occupiedSize - directorySize;
  385. dbgprintf("* inode: %u", entry.inode.index());
  386. dbgprintf(", name_len: %u", word(entry.name_length));
  387. dbgprintf(", rec_len: %u", word(recordLength));
  388. dbgprintf(", file_type: %u", byte(entry.fileType));
  389. dbgprintf(", name: %s\n", entry.name);
  390. stream << dword(entry.inode.index());
  391. stream << word(recordLength);
  392. stream << byte(entry.name_length);
  393. stream << byte(entry.fileType);
  394. stream << entry.name;
  395. unsigned padding = recordLength - entry.name_length - 8;
  396. //dbgprintf(" *** pad %u bytes\n", padding);
  397. for (unsigned j = 0; j < padding; ++j) {
  398. stream << byte(0);
  399. }
  400. }
  401. stream.fillToEnd(0);
  402. #if 0
  403. kprintf("data to write (%u):\n", directoryData.size());
  404. for (unsigned i = 0; i < directoryData.size(); ++i) {
  405. kprintf("%02x ", directoryData[i]);
  406. if ((i + 1) % 8 == 0)
  407. kprintf(" ");
  408. if ((i + 1) % 16 == 0)
  409. kprintf("\n");
  410. }
  411. kprintf("\n");
  412. #endif
  413. return get_inode({ id(), directoryInode })->write(directoryData);
  414. }
  415. unsigned Ext2FS::inodes_per_block() const
  416. {
  417. return EXT2_INODES_PER_BLOCK(&super_block());
  418. }
  419. unsigned Ext2FS::inodes_per_group() const
  420. {
  421. return EXT2_INODES_PER_GROUP(&super_block());
  422. }
  423. unsigned Ext2FS::inode_size() const
  424. {
  425. return EXT2_INODE_SIZE(&super_block());
  426. }
  427. unsigned Ext2FS::blocks_per_group() const
  428. {
  429. return EXT2_BLOCKS_PER_GROUP(&super_block());
  430. }
  431. void Ext2FS::dump_block_bitmap(unsigned groupIndex) const
  432. {
  433. ASSERT(groupIndex <= m_blockGroupCount);
  434. auto& bgd = group_descriptor(groupIndex);
  435. unsigned blocksInGroup = min(blocks_per_group(), super_block().s_blocks_count);
  436. unsigned blockCount = ceilDiv(blocksInGroup, 8u);
  437. auto bitmapBlocks = readBlocks(bgd.bg_block_bitmap, blockCount);
  438. ASSERT(bitmapBlocks);
  439. kprintf("ext2fs: group[%u] block bitmap (bitmap occupies %u blocks):\n", groupIndex, blockCount);
  440. auto bitmap = Bitmap::wrap(bitmapBlocks.pointer(), blocksInGroup);
  441. for (unsigned i = 0; i < blocksInGroup; ++i) {
  442. kprintf("%c", bitmap.get(i) ? '1' : '0');
  443. }
  444. kprintf("\n");
  445. }
  446. void Ext2FS::dump_inode_bitmap(unsigned groupIndex) const
  447. {
  448. traverse_inode_bitmap(groupIndex, [] (unsigned, const Bitmap& bitmap) {
  449. for (unsigned i = 0; i < bitmap.size(); ++i)
  450. kprintf("%c", bitmap.get(i) ? '1' : '0');
  451. return true;
  452. });
  453. }
  454. template<typename F>
  455. void Ext2FS::traverse_inode_bitmap(unsigned groupIndex, F callback) const
  456. {
  457. ASSERT(groupIndex <= m_blockGroupCount);
  458. auto& bgd = group_descriptor(groupIndex);
  459. unsigned inodesInGroup = min(inodes_per_group(), super_block().s_inodes_count);
  460. unsigned blockCount = ceilDiv(inodesInGroup, 8u);
  461. for (unsigned i = 0; i < blockCount; ++i) {
  462. auto block = readBlock(bgd.bg_inode_bitmap + i);
  463. ASSERT(block);
  464. bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), inodesInGroup));
  465. if (!shouldContinue)
  466. break;
  467. }
  468. }
  469. template<typename F>
  470. void Ext2FS::traverse_block_bitmap(unsigned groupIndex, F callback) const
  471. {
  472. ASSERT(groupIndex <= m_blockGroupCount);
  473. auto& bgd = group_descriptor(groupIndex);
  474. unsigned blocksInGroup = min(blocks_per_group(), super_block().s_blocks_count);
  475. unsigned blockCount = ceilDiv(blocksInGroup, 8u);
  476. for (unsigned i = 0; i < blockCount; ++i) {
  477. auto block = readBlock(bgd.bg_block_bitmap + i);
  478. ASSERT(block);
  479. bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), blocksInGroup));
  480. if (!shouldContinue)
  481. break;
  482. }
  483. }
  484. bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
  485. {
  486. unsigned blockIndex;
  487. unsigned offset;
  488. auto block = read_block_containing_inode(inode, blockIndex, offset);
  489. if (!block)
  490. return false;
  491. memcpy(reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), &e2inode, inode_size());
  492. writeBlock(blockIndex, block);
  493. return true;
  494. }
  495. Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(unsigned group, unsigned count)
  496. {
  497. dbgprintf("Ext2FS: allocateBlocks(group: %u, count: %u)\n", group, count);
  498. auto& bgd = group_descriptor(group);
  499. if (bgd.bg_free_blocks_count < count) {
  500. kprintf("ExtFS: allocateBlocks can't allocate out of group %u, wanted %u but only %u available\n", group, count, bgd.bg_free_blocks_count);
  501. return { };
  502. }
  503. // FIXME: Implement a scan that finds consecutive blocks if possible.
  504. Vector<BlockIndex> blocks;
  505. traverse_block_bitmap(group, [&blocks, count] (unsigned firstBlockInBitmap, const Bitmap& bitmap) {
  506. for (unsigned i = 0; i < bitmap.size(); ++i) {
  507. if (!bitmap.get(i)) {
  508. blocks.append(firstBlockInBitmap + i);
  509. if (blocks.size() == count)
  510. return false;
  511. }
  512. }
  513. return true;
  514. });
  515. dbgprintf("Ext2FS: allocateBlock found these blocks:\n");
  516. for (auto& bi : blocks) {
  517. dbgprintf(" > %u\n", bi);
  518. }
  519. return blocks;
  520. }
  521. unsigned Ext2FS::allocate_inode(unsigned preferredGroup, unsigned expectedSize)
  522. {
  523. dbgprintf("Ext2FS: allocateInode(preferredGroup: %u, expectedSize: %u)\n", preferredGroup, expectedSize);
  524. unsigned neededBlocks = ceilDiv(expectedSize, blockSize());
  525. dbgprintf("Ext2FS: minimum needed blocks: %u\n", neededBlocks);
  526. unsigned groupIndex = 0;
  527. auto isSuitableGroup = [this, neededBlocks] (unsigned groupIndex) {
  528. auto& bgd = group_descriptor(groupIndex);
  529. return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= neededBlocks;
  530. };
  531. if (preferredGroup && isSuitableGroup(preferredGroup)) {
  532. groupIndex = preferredGroup;
  533. } else {
  534. for (unsigned i = 1; i <= m_blockGroupCount; ++i) {
  535. if (isSuitableGroup(i))
  536. groupIndex = i;
  537. }
  538. }
  539. if (!groupIndex) {
  540. kprintf("Ext2FS: allocateInode: no suitable group found for new inode with %u blocks needed :(\n", neededBlocks);
  541. return 0;
  542. }
  543. dbgprintf("Ext2FS: allocateInode: found suitable group [%u] for new inode with %u blocks needed :^)\n", groupIndex, neededBlocks);
  544. unsigned firstFreeInodeInGroup = 0;
  545. traverse_inode_bitmap(groupIndex, [&firstFreeInodeInGroup] (unsigned firstInodeInBitmap, const Bitmap& bitmap) {
  546. for (unsigned i = 0; i < bitmap.size(); ++i) {
  547. if (!bitmap.get(i)) {
  548. firstFreeInodeInGroup = firstInodeInBitmap + i;
  549. return false;
  550. }
  551. }
  552. return true;
  553. });
  554. if (!firstFreeInodeInGroup) {
  555. kprintf("Ext2FS: firstFreeInodeInGroup returned no inode, despite bgd claiming there are inodes :(\n");
  556. return 0;
  557. }
  558. unsigned inode = firstFreeInodeInGroup;
  559. dbgprintf("Ext2FS: found suitable inode %u\n", inode);
  560. // FIXME: allocate blocks if needed!
  561. return inode;
  562. }
  563. unsigned Ext2FS::group_index_from_inode(unsigned inode) const
  564. {
  565. if (!inode)
  566. return 0;
  567. return (inode - 1) / inodes_per_group() + 1;
  568. }
  569. bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
  570. {
  571. if (index == 0)
  572. return true;
  573. auto& bgd = group_descriptor(group_index_from_inode(index));
  574. unsigned inodes_per_bitmap_block = blockSize() * 8;
  575. unsigned bitmap_block_index = (index - 1) / inodes_per_bitmap_block;
  576. unsigned bit_index = (index - 1) % inodes_per_bitmap_block;
  577. auto block = readBlock(bgd.bg_inode_bitmap + bitmap_block_index);
  578. ASSERT(block);
  579. auto bitmap = Bitmap::wrap(block.pointer(), block.size());
  580. return bitmap.get(bit_index);
  581. }
  582. bool Ext2FS::set_inode_allocation_state(unsigned index, bool newState)
  583. {
  584. auto& bgd = group_descriptor(group_index_from_inode(index));
  585. // Update inode bitmap
  586. unsigned inodes_per_bitmap_block = blockSize() * 8;
  587. unsigned bitmap_block_index = (index - 1) / inodes_per_bitmap_block;
  588. unsigned bit_index = (index - 1) % inodes_per_bitmap_block;
  589. auto block = readBlock(bgd.bg_inode_bitmap + bitmap_block_index);
  590. ASSERT(block);
  591. auto bitmap = Bitmap::wrap(block.pointer(), block.size());
  592. bool currentState = bitmap.get(bit_index);
  593. dbgprintf("Ext2FS: set_inode_allocation_state(%u) %u -> %u\n", index, currentState, newState);
  594. if (currentState == newState)
  595. return true;
  596. bitmap.set(bit_index, newState);
  597. writeBlock(bgd.bg_inode_bitmap + bitmap_block_index, block);
  598. // Update superblock
  599. auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
  600. dbgprintf("Ext2FS: superblock free inode count %u -> %u\n", sb.s_free_inodes_count, sb.s_free_inodes_count - 1);
  601. if (newState)
  602. --sb.s_free_inodes_count;
  603. else
  604. ++sb.s_free_inodes_count;
  605. write_super_block(sb);
  606. // Update BGD
  607. auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
  608. if (newState)
  609. --mutableBGD.bg_free_inodes_count;
  610. else
  611. ++mutableBGD.bg_free_inodes_count;
  612. dbgprintf("Ext2FS: group free inode count %u -> %u\n", bgd.bg_free_inodes_count, bgd.bg_free_inodes_count - 1);
  613. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  614. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  615. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
  616. return true;
  617. }
  618. bool Ext2FS::set_block_allocation_state(GroupIndex group, BlockIndex bi, bool newState)
  619. {
  620. auto& bgd = group_descriptor(group);
  621. // Update block bitmap
  622. unsigned blocksPerBitmapBlock = blockSize() * 8;
  623. unsigned bitmapBlockIndex = (bi - 1) / blocksPerBitmapBlock;
  624. unsigned bitIndex = (bi - 1) % blocksPerBitmapBlock;
  625. auto block = readBlock(bgd.bg_block_bitmap + bitmapBlockIndex);
  626. ASSERT(block);
  627. auto bitmap = Bitmap::wrap(block.pointer(), block.size());
  628. bool currentState = bitmap.get(bitIndex);
  629. dbgprintf("Ext2FS: setBlockAllocationState(%u) %u -> %u\n", bi, currentState, newState);
  630. if (currentState == newState)
  631. return true;
  632. bitmap.set(bitIndex, newState);
  633. writeBlock(bgd.bg_block_bitmap + bitmapBlockIndex, block);
  634. // Update superblock
  635. auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
  636. dbgprintf("Ext2FS: superblock free block count %u -> %u\n", sb.s_free_blocks_count, sb.s_free_blocks_count - 1);
  637. if (newState)
  638. --sb.s_free_blocks_count;
  639. else
  640. ++sb.s_free_blocks_count;
  641. write_super_block(sb);
  642. // Update BGD
  643. auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
  644. if (newState)
  645. --mutableBGD.bg_free_blocks_count;
  646. else
  647. ++mutableBGD.bg_free_blocks_count;
  648. dbgprintf("Ext2FS: group free block count %u -> %u\n", bgd.bg_free_blocks_count, bgd.bg_free_blocks_count - 1);
  649. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  650. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  651. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
  652. return true;
  653. }
  654. RetainPtr<Inode> Ext2FS::create_directory(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, int& error)
  655. {
  656. ASSERT(parent_id.fsid() == id());
  657. // Fix up the mode to definitely be a directory.
  658. // FIXME: This is a bit on the hackish side.
  659. mode &= ~0170000;
  660. mode |= 0040000;
  661. // NOTE: When creating a new directory, make the size 1 block.
  662. // There's probably a better strategy here, but this works for now.
  663. auto inode = create_inode(parent_id, name, mode, blockSize(), error);
  664. if (!inode)
  665. return nullptr;
  666. dbgprintf("Ext2FS: create_directory: created new directory named '%s' with inode %u\n", name.characters(), inode->identifier().index());
  667. Vector<DirectoryEntry> entries;
  668. entries.append({ ".", inode->identifier(), EXT2_FT_DIR });
  669. entries.append({ "..", parent_id, EXT2_FT_DIR });
  670. bool success = write_directory_inode(inode->identifier().index(), move(entries));
  671. ASSERT(success);
  672. auto parent_inode = get_inode(parent_id);
  673. error = parent_inode->increment_link_count();
  674. if (error < 0)
  675. return nullptr;
  676. auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
  677. ++bgd.bg_used_dirs_count;
  678. dbgprintf("Ext2FS: incremented bg_used_dirs_count %u -> %u\n", bgd.bg_used_dirs_count - 1, bgd.bg_used_dirs_count);
  679. unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
  680. unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
  681. writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
  682. error = 0;
  683. return inode;
  684. }
  685. RetainPtr<Inode> Ext2FS::create_inode(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, unsigned size, int& error)
  686. {
  687. ASSERT(parent_id.fsid() == id());
  688. auto parent_inode = get_inode(parent_id);
  689. dbgprintf("Ext2FS: Adding inode '%s' (mode %u) to parent directory %u:\n", name.characters(), mode, parent_inode->identifier().index());
  690. // NOTE: This doesn't commit the inode allocation just yet!
  691. auto inode_id = allocate_inode(0, 0);
  692. if (!inode_id) {
  693. kprintf("Ext2FS: createInode: allocate_inode failed\n");
  694. error = -ENOSPC;
  695. return { };
  696. }
  697. auto blocks = allocate_blocks(group_index_from_inode(inode_id), ceilDiv(size, blockSize()));
  698. if (blocks.is_empty()) {
  699. kprintf("Ext2FS: createInode: allocate_blocks failed\n");
  700. error = -ENOSPC;
  701. return { };
  702. }
  703. byte fileType = 0;
  704. if (isRegularFile(mode))
  705. fileType = EXT2_FT_REG_FILE;
  706. else if (isDirectory(mode))
  707. fileType = EXT2_FT_DIR;
  708. else if (isCharacterDevice(mode))
  709. fileType = EXT2_FT_CHRDEV;
  710. else if (isBlockDevice(mode))
  711. fileType = EXT2_FT_BLKDEV;
  712. else if (isFIFO(mode))
  713. fileType = EXT2_FT_FIFO;
  714. else if (isSocket(mode))
  715. fileType = EXT2_FT_SOCK;
  716. else if (isSymbolicLink(mode))
  717. fileType = EXT2_FT_SYMLINK;
  718. // Try adding it to the directory first, in case the name is already in use.
  719. bool success = parent_inode->add_child({ id(), inode_id }, name, fileType, error);
  720. if (!success)
  721. return { };
  722. // Looks like we're good, time to update the inode bitmap and group+global inode counters.
  723. success = set_inode_allocation_state(inode_id, true);
  724. ASSERT(success);
  725. for (auto bi : blocks) {
  726. success = set_block_allocation_state(group_index_from_inode(inode_id), bi, true);
  727. ASSERT(success);
  728. }
  729. unsigned initialLinksCount;
  730. if (isDirectory(mode))
  731. initialLinksCount = 2; // (parent directory + "." entry in self)
  732. else
  733. initialLinksCount = 1;
  734. auto timestamp = ktime(nullptr);
  735. auto e2inode = make<ext2_inode>();
  736. memset(e2inode.ptr(), 0, sizeof(ext2_inode));
  737. e2inode->i_mode = mode;
  738. e2inode->i_uid = 0;
  739. e2inode->i_size = size;
  740. e2inode->i_atime = timestamp;
  741. e2inode->i_ctime = timestamp;
  742. e2inode->i_mtime = timestamp;
  743. e2inode->i_dtime = 0;
  744. e2inode->i_gid = 0;
  745. e2inode->i_links_count = initialLinksCount;
  746. e2inode->i_blocks = blocks.size() * (blockSize() / 512);
  747. // FIXME: Implement writing out indirect blocks!
  748. ASSERT(blocks.size() < EXT2_NDIR_BLOCKS);
  749. dbgprintf("Ext2FS: writing %u blocks to i_block array\n", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()));
  750. for (unsigned i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i) {
  751. e2inode->i_block[i] = blocks[i];
  752. }
  753. dbgprintf("Ext2FS: writing initial metadata for inode %u\n", inode_id);
  754. e2inode->i_flags = 0;
  755. success = write_ext2_inode(inode_id, *e2inode);
  756. ASSERT(success);
  757. {
  758. // We might have cached the fact that this inode didn't exist. Wipe the slate.
  759. LOCKER(m_inode_cache_lock);
  760. m_inode_cache.remove(inode_id);
  761. }
  762. return get_inode({ id(), inode_id });
  763. }
  764. RetainPtr<Inode> Ext2FSInode::parent() const
  765. {
  766. if (m_parent_id.is_valid())
  767. return fs().get_inode(m_parent_id);
  768. unsigned group_index = fs().group_index_from_inode(index());
  769. unsigned first_inode_in_group = fs().inodes_per_group() * (group_index - 1);
  770. Vector<RetainPtr<Ext2FSInode>> directories_in_group;
  771. for (unsigned i = 0; i < fs().inodes_per_group(); ++i) {
  772. auto group_member = fs().get_inode({ fsid(), first_inode_in_group + i });
  773. if (!group_member)
  774. continue;
  775. if (group_member->is_directory())
  776. directories_in_group.append(move(group_member));
  777. }
  778. for (auto& directory : directories_in_group) {
  779. if (!directory->reverse_lookup(identifier()).is_null()) {
  780. m_parent_id = directory->identifier();
  781. break;
  782. }
  783. }
  784. ASSERT(m_parent_id.is_valid());
  785. return fs().get_inode(m_parent_id);
  786. }
  787. void Ext2FSInode::populate_lookup_cache()
  788. {
  789. {
  790. LOCKER(m_lock);
  791. if (!m_lookup_cache.is_empty())
  792. return;
  793. }
  794. HashMap<String, unsigned> children;
  795. traverse_as_directory([&children] (auto& entry) {
  796. children.set(String(entry.name, entry.name_length), entry.inode.index());
  797. return true;
  798. });
  799. LOCKER(m_lock);
  800. if (!m_lookup_cache.is_empty())
  801. return;
  802. m_lookup_cache = move(children);
  803. }
  804. InodeIdentifier Ext2FSInode::lookup(const String& name)
  805. {
  806. ASSERT(is_directory());
  807. populate_lookup_cache();
  808. LOCKER(m_lock);
  809. auto it = m_lookup_cache.find(name);
  810. if (it != m_lookup_cache.end())
  811. return { fsid(), (*it).value };
  812. return { };
  813. }
  814. String Ext2FSInode::reverse_lookup(InodeIdentifier child_id)
  815. {
  816. ASSERT(is_directory());
  817. ASSERT(child_id.fsid() == fsid());
  818. populate_lookup_cache();
  819. LOCKER(m_lock);
  820. for (auto it : m_lookup_cache) {
  821. if (it.value == child_id.index())
  822. return it.key;
  823. }
  824. return { };
  825. }
  826. void Ext2FSInode::one_retain_left()
  827. {
  828. // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
  829. }
  830. int Ext2FSInode::set_atime(Unix::time_t t)
  831. {
  832. if (fs().is_readonly())
  833. return -EROFS;
  834. m_raw_inode.i_atime = t;
  835. set_metadata_dirty(true);
  836. return 0;
  837. }
  838. int Ext2FSInode::set_ctime(Unix::time_t t)
  839. {
  840. if (fs().is_readonly())
  841. return -EROFS;
  842. m_raw_inode.i_ctime = t;
  843. set_metadata_dirty(true);
  844. return 0;
  845. }
  846. int Ext2FSInode::set_mtime(Unix::time_t t)
  847. {
  848. if (fs().is_readonly())
  849. return -EROFS;
  850. m_raw_inode.i_mtime = t;
  851. set_metadata_dirty(true);
  852. return 0;
  853. }
  854. int Ext2FSInode::increment_link_count()
  855. {
  856. if (fs().is_readonly())
  857. return -EROFS;
  858. ++m_raw_inode.i_links_count;
  859. set_metadata_dirty(true);
  860. return 0;
  861. }
  862. int Ext2FSInode::decrement_link_count()
  863. {
  864. if (fs().is_readonly())
  865. return -EROFS;
  866. --m_raw_inode.i_links_count;
  867. set_metadata_dirty(true);
  868. return 0;
  869. }