Ext2FileSystem.cpp 36 KB

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