ext4_journal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT4 filesystem implementation in Uboot by
  4. * Uma Shankar <uma.shankar@samsung.com>
  5. * Manjunatha C Achar <a.manjunatha@samsung.com>
  6. *
  7. * Journal data structures and headers for Journaling feature of ext4
  8. * have been referred from JBD2 (Journaling Block device 2)
  9. * implementation in Linux Kernel.
  10. * Written by Stephen C. Tweedie <sct@redhat.com>
  11. *
  12. * Copyright 1998-2000 Red Hat, Inc --- All Rights Reserved
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <ext4fs.h>
  17. #include <malloc.h>
  18. #include <ext_common.h>
  19. #include "ext4_common.h"
  20. static struct revoke_blk_list *revk_blk_list;
  21. static struct revoke_blk_list *prev_node;
  22. static int first_node = true;
  23. int gindex;
  24. int gd_index;
  25. int jrnl_blk_idx;
  26. struct journal_log *journal_ptr[MAX_JOURNAL_ENTRIES];
  27. struct dirty_blocks *dirty_block_ptr[MAX_JOURNAL_ENTRIES];
  28. int ext4fs_init_journal(void)
  29. {
  30. int i;
  31. char *temp = NULL;
  32. struct ext_filesystem *fs = get_fs();
  33. /* init globals */
  34. revk_blk_list = NULL;
  35. prev_node = NULL;
  36. gindex = 0;
  37. gd_index = 0;
  38. jrnl_blk_idx = 1;
  39. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  40. journal_ptr[i] = zalloc(sizeof(struct journal_log));
  41. if (!journal_ptr[i])
  42. goto fail;
  43. dirty_block_ptr[i] = zalloc(sizeof(struct dirty_blocks));
  44. if (!dirty_block_ptr[i])
  45. goto fail;
  46. journal_ptr[i]->buf = NULL;
  47. journal_ptr[i]->blknr = -1;
  48. dirty_block_ptr[i]->buf = NULL;
  49. dirty_block_ptr[i]->blknr = -1;
  50. }
  51. if (fs->blksz == 4096) {
  52. temp = zalloc(fs->blksz);
  53. if (!temp)
  54. goto fail;
  55. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  56. if (!journal_ptr[gindex]->buf)
  57. goto fail;
  58. ext4fs_devread(0, 0, fs->blksz, temp);
  59. memcpy(temp + SUPERBLOCK_SIZE, fs->sb, SUPERBLOCK_SIZE);
  60. memcpy(journal_ptr[gindex]->buf, temp, fs->blksz);
  61. journal_ptr[gindex++]->blknr = 0;
  62. free(temp);
  63. } else {
  64. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  65. if (!journal_ptr[gindex]->buf)
  66. goto fail;
  67. memcpy(journal_ptr[gindex]->buf, fs->sb, SUPERBLOCK_SIZE);
  68. journal_ptr[gindex++]->blknr = 1;
  69. }
  70. /* Check the file system state using journal super block */
  71. if (ext4fs_check_journal_state(SCAN))
  72. goto fail;
  73. /* Check the file system state using journal super block */
  74. if (ext4fs_check_journal_state(RECOVER))
  75. goto fail;
  76. return 0;
  77. fail:
  78. return -1;
  79. }
  80. void ext4fs_dump_metadata(void)
  81. {
  82. struct ext_filesystem *fs = get_fs();
  83. int i;
  84. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  85. if (dirty_block_ptr[i]->blknr == -1)
  86. break;
  87. put_ext4((uint64_t) ((uint64_t)dirty_block_ptr[i]->blknr *
  88. (uint64_t)fs->blksz), dirty_block_ptr[i]->buf,
  89. fs->blksz);
  90. }
  91. }
  92. void ext4fs_free_journal(void)
  93. {
  94. int i;
  95. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  96. if (dirty_block_ptr[i]->blknr == -1)
  97. break;
  98. if (dirty_block_ptr[i]->buf)
  99. free(dirty_block_ptr[i]->buf);
  100. }
  101. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  102. if (journal_ptr[i]->blknr == -1)
  103. break;
  104. if (journal_ptr[i]->buf)
  105. free(journal_ptr[i]->buf);
  106. }
  107. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  108. if (journal_ptr[i])
  109. free(journal_ptr[i]);
  110. if (dirty_block_ptr[i])
  111. free(dirty_block_ptr[i]);
  112. }
  113. gindex = 0;
  114. gd_index = 0;
  115. jrnl_blk_idx = 1;
  116. }
  117. int ext4fs_log_gdt(char *gd_table)
  118. {
  119. struct ext_filesystem *fs = get_fs();
  120. short i;
  121. long int var = fs->gdtable_blkno;
  122. for (i = 0; i < fs->no_blk_pergdt; i++) {
  123. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  124. if (!journal_ptr[gindex]->buf)
  125. return -ENOMEM;
  126. memcpy(journal_ptr[gindex]->buf, gd_table, fs->blksz);
  127. gd_table += fs->blksz;
  128. journal_ptr[gindex++]->blknr = var++;
  129. }
  130. return 0;
  131. }
  132. /*
  133. * This function stores the backup copy of meta data in RAM
  134. * journal_buffer -- Buffer containing meta data
  135. * blknr -- Block number on disk of the meta data buffer
  136. */
  137. int ext4fs_log_journal(char *journal_buffer, long int blknr)
  138. {
  139. struct ext_filesystem *fs = get_fs();
  140. short i;
  141. if (!journal_buffer) {
  142. printf("Invalid input arguments %s\n", __func__);
  143. return -EINVAL;
  144. }
  145. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  146. if (journal_ptr[i]->blknr == -1)
  147. break;
  148. if (journal_ptr[i]->blknr == blknr)
  149. return 0;
  150. }
  151. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  152. if (!journal_ptr[gindex]->buf)
  153. return -ENOMEM;
  154. memcpy(journal_ptr[gindex]->buf, journal_buffer, fs->blksz);
  155. journal_ptr[gindex++]->blknr = blknr;
  156. return 0;
  157. }
  158. /*
  159. * This function stores the modified meta data in RAM
  160. * metadata_buffer -- Buffer containing meta data
  161. * blknr -- Block number on disk of the meta data buffer
  162. */
  163. int ext4fs_put_metadata(char *metadata_buffer, long int blknr)
  164. {
  165. struct ext_filesystem *fs = get_fs();
  166. if (!metadata_buffer) {
  167. printf("Invalid input arguments %s\n", __func__);
  168. return -EINVAL;
  169. }
  170. dirty_block_ptr[gd_index]->buf = zalloc(fs->blksz);
  171. if (!dirty_block_ptr[gd_index]->buf)
  172. return -ENOMEM;
  173. memcpy(dirty_block_ptr[gd_index]->buf, metadata_buffer, fs->blksz);
  174. dirty_block_ptr[gd_index++]->blknr = blknr;
  175. return 0;
  176. }
  177. void print_revoke_blks(char *revk_blk)
  178. {
  179. int offset;
  180. int max;
  181. long int blocknr;
  182. struct journal_revoke_header_t *header;
  183. if (revk_blk == NULL)
  184. return;
  185. header = (struct journal_revoke_header_t *) revk_blk;
  186. offset = sizeof(struct journal_revoke_header_t);
  187. max = be32_to_cpu(header->r_count);
  188. printf("total bytes %d\n", max);
  189. while (offset < max) {
  190. blocknr = be32_to_cpu(*((long int *)(revk_blk + offset)));
  191. printf("revoke blknr is %ld\n", blocknr);
  192. offset += 4;
  193. }
  194. }
  195. static struct revoke_blk_list *_get_node(void)
  196. {
  197. struct revoke_blk_list *tmp_node;
  198. tmp_node = zalloc(sizeof(struct revoke_blk_list));
  199. if (tmp_node == NULL)
  200. return NULL;
  201. tmp_node->content = NULL;
  202. tmp_node->next = NULL;
  203. return tmp_node;
  204. }
  205. void ext4fs_push_revoke_blk(char *buffer)
  206. {
  207. struct revoke_blk_list *node = NULL;
  208. struct ext_filesystem *fs = get_fs();
  209. if (buffer == NULL) {
  210. printf("buffer ptr is NULL\n");
  211. return;
  212. }
  213. node = _get_node();
  214. if (!node) {
  215. printf("_get_node: malloc failed\n");
  216. return;
  217. }
  218. node->content = zalloc(fs->blksz);
  219. if (node->content == NULL)
  220. return;
  221. memcpy(node->content, buffer, fs->blksz);
  222. if (first_node == true) {
  223. revk_blk_list = node;
  224. prev_node = node;
  225. first_node = false;
  226. } else {
  227. prev_node->next = node;
  228. prev_node = node;
  229. }
  230. }
  231. void ext4fs_free_revoke_blks(void)
  232. {
  233. struct revoke_blk_list *tmp_node = revk_blk_list;
  234. struct revoke_blk_list *next_node = NULL;
  235. while (tmp_node != NULL) {
  236. if (tmp_node->content)
  237. free(tmp_node->content);
  238. tmp_node = tmp_node->next;
  239. }
  240. tmp_node = revk_blk_list;
  241. while (tmp_node != NULL) {
  242. next_node = tmp_node->next;
  243. free(tmp_node);
  244. tmp_node = next_node;
  245. }
  246. revk_blk_list = NULL;
  247. prev_node = NULL;
  248. first_node = true;
  249. }
  250. int check_blknr_for_revoke(long int blknr, int sequence_no)
  251. {
  252. struct journal_revoke_header_t *header;
  253. int offset;
  254. int max;
  255. long int blocknr;
  256. char *revk_blk;
  257. struct revoke_blk_list *tmp_revk_node = revk_blk_list;
  258. while (tmp_revk_node != NULL) {
  259. revk_blk = tmp_revk_node->content;
  260. header = (struct journal_revoke_header_t *) revk_blk;
  261. if (sequence_no < be32_to_cpu(header->r_header.h_sequence)) {
  262. offset = sizeof(struct journal_revoke_header_t);
  263. max = be32_to_cpu(header->r_count);
  264. while (offset < max) {
  265. blocknr = be32_to_cpu(*((long int *)
  266. (revk_blk + offset)));
  267. if (blocknr == blknr)
  268. goto found;
  269. offset += 4;
  270. }
  271. }
  272. tmp_revk_node = tmp_revk_node->next;
  273. }
  274. return -1;
  275. found:
  276. return 0;
  277. }
  278. /*
  279. * This function parses the journal blocks and replays the
  280. * suceessful transactions. A transaction is successfull
  281. * if commit block is found for a descriptor block
  282. * The tags in descriptor block contain the disk block
  283. * numbers of the metadata to be replayed
  284. */
  285. void recover_transaction(int prev_desc_logical_no)
  286. {
  287. struct ext2_inode inode_journal;
  288. struct ext_filesystem *fs = get_fs();
  289. struct journal_header_t *jdb;
  290. long int blknr;
  291. char *p_jdb;
  292. int ofs, flags;
  293. int i;
  294. struct ext3_journal_block_tag *tag;
  295. char *temp_buff = zalloc(fs->blksz);
  296. char *metadata_buff = zalloc(fs->blksz);
  297. if (!temp_buff || !metadata_buff)
  298. goto fail;
  299. i = prev_desc_logical_no;
  300. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  301. (struct ext2_inode *)&inode_journal);
  302. blknr = read_allocated_block((struct ext2_inode *)
  303. &inode_journal, i);
  304. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  305. temp_buff);
  306. p_jdb = (char *)temp_buff;
  307. jdb = (struct journal_header_t *) temp_buff;
  308. ofs = sizeof(struct journal_header_t);
  309. do {
  310. tag = (struct ext3_journal_block_tag *)&p_jdb[ofs];
  311. ofs += sizeof(struct ext3_journal_block_tag);
  312. if (ofs > fs->blksz)
  313. break;
  314. flags = be32_to_cpu(tag->flags);
  315. if (!(flags & EXT3_JOURNAL_FLAG_SAME_UUID))
  316. ofs += 16;
  317. i++;
  318. debug("\t\ttag %u\n", be32_to_cpu(tag->block));
  319. if (revk_blk_list != NULL) {
  320. if (check_blknr_for_revoke(be32_to_cpu(tag->block),
  321. be32_to_cpu(jdb->h_sequence)) == 0)
  322. continue;
  323. }
  324. blknr = read_allocated_block(&inode_journal, i);
  325. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  326. fs->blksz, metadata_buff);
  327. put_ext4((uint64_t)((uint64_t)be32_to_cpu(tag->block) * (uint64_t)fs->blksz),
  328. metadata_buff, (uint32_t) fs->blksz);
  329. } while (!(flags & EXT3_JOURNAL_FLAG_LAST_TAG));
  330. fail:
  331. free(temp_buff);
  332. free(metadata_buff);
  333. }
  334. void print_jrnl_status(int recovery_flag)
  335. {
  336. if (recovery_flag == RECOVER)
  337. printf("Journal Recovery Completed\n");
  338. else
  339. printf("Journal Scan Completed\n");
  340. }
  341. int ext4fs_check_journal_state(int recovery_flag)
  342. {
  343. int i;
  344. int DB_FOUND = NO;
  345. long int blknr;
  346. int transaction_state = TRANSACTION_COMPLETE;
  347. int prev_desc_logical_no = 0;
  348. int curr_desc_logical_no = 0;
  349. int ofs, flags;
  350. struct ext2_inode inode_journal;
  351. struct journal_superblock_t *jsb = NULL;
  352. struct journal_header_t *jdb = NULL;
  353. char *p_jdb = NULL;
  354. struct ext3_journal_block_tag *tag = NULL;
  355. char *temp_buff = NULL;
  356. char *temp_buff1 = NULL;
  357. struct ext_filesystem *fs = get_fs();
  358. temp_buff = zalloc(fs->blksz);
  359. if (!temp_buff)
  360. return -ENOMEM;
  361. temp_buff1 = zalloc(fs->blksz);
  362. if (!temp_buff1) {
  363. free(temp_buff);
  364. return -ENOMEM;
  365. }
  366. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  367. blknr = read_allocated_block(&inode_journal, EXT2_JOURNAL_SUPERBLOCK);
  368. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  369. temp_buff);
  370. jsb = (struct journal_superblock_t *) temp_buff;
  371. if (fs->sb->feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
  372. if (recovery_flag == RECOVER)
  373. printf("Recovery required\n");
  374. } else {
  375. if (recovery_flag == RECOVER)
  376. printf("File System is consistent\n");
  377. goto end;
  378. }
  379. if (be32_to_cpu(jsb->s_start) == 0)
  380. goto end;
  381. if (!(jsb->s_feature_compat &
  382. cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM)))
  383. jsb->s_feature_compat |=
  384. cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
  385. i = be32_to_cpu(jsb->s_first);
  386. while (1) {
  387. blknr = read_allocated_block(&inode_journal, i);
  388. memset(temp_buff1, '\0', fs->blksz);
  389. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
  390. 0, fs->blksz, temp_buff1);
  391. jdb = (struct journal_header_t *) temp_buff1;
  392. if (be32_to_cpu(jdb->h_blocktype) ==
  393. EXT3_JOURNAL_DESCRIPTOR_BLOCK) {
  394. if (be32_to_cpu(jdb->h_sequence) !=
  395. be32_to_cpu(jsb->s_sequence)) {
  396. print_jrnl_status(recovery_flag);
  397. break;
  398. }
  399. curr_desc_logical_no = i;
  400. if (transaction_state == TRANSACTION_COMPLETE)
  401. transaction_state = TRANSACTION_RUNNING;
  402. else
  403. return -1;
  404. p_jdb = (char *)temp_buff1;
  405. ofs = sizeof(struct journal_header_t);
  406. do {
  407. tag = (struct ext3_journal_block_tag *)
  408. &p_jdb[ofs];
  409. ofs += sizeof(struct ext3_journal_block_tag);
  410. if (ofs > fs->blksz)
  411. break;
  412. flags = be32_to_cpu(tag->flags);
  413. if (!(flags & EXT3_JOURNAL_FLAG_SAME_UUID))
  414. ofs += 16;
  415. i++;
  416. debug("\t\ttag %u\n", be32_to_cpu(tag->block));
  417. } while (!(flags & EXT3_JOURNAL_FLAG_LAST_TAG));
  418. i++;
  419. DB_FOUND = YES;
  420. } else if (be32_to_cpu(jdb->h_blocktype) ==
  421. EXT3_JOURNAL_COMMIT_BLOCK) {
  422. if (be32_to_cpu(jdb->h_sequence) !=
  423. be32_to_cpu(jsb->s_sequence)) {
  424. print_jrnl_status(recovery_flag);
  425. break;
  426. }
  427. if (transaction_state == TRANSACTION_RUNNING ||
  428. (DB_FOUND == NO)) {
  429. transaction_state = TRANSACTION_COMPLETE;
  430. i++;
  431. jsb->s_sequence =
  432. cpu_to_be32(be32_to_cpu(
  433. jsb->s_sequence) + 1);
  434. }
  435. prev_desc_logical_no = curr_desc_logical_no;
  436. if ((recovery_flag == RECOVER) && (DB_FOUND == YES))
  437. recover_transaction(prev_desc_logical_no);
  438. DB_FOUND = NO;
  439. } else if (be32_to_cpu(jdb->h_blocktype) ==
  440. EXT3_JOURNAL_REVOKE_BLOCK) {
  441. if (be32_to_cpu(jdb->h_sequence) !=
  442. be32_to_cpu(jsb->s_sequence)) {
  443. print_jrnl_status(recovery_flag);
  444. break;
  445. }
  446. if (recovery_flag == SCAN)
  447. ext4fs_push_revoke_blk((char *)jdb);
  448. i++;
  449. } else {
  450. debug("Else Case\n");
  451. if (be32_to_cpu(jdb->h_sequence) !=
  452. be32_to_cpu(jsb->s_sequence)) {
  453. print_jrnl_status(recovery_flag);
  454. break;
  455. }
  456. }
  457. }
  458. end:
  459. if (recovery_flag == RECOVER) {
  460. jsb->s_start = cpu_to_be32(1);
  461. jsb->s_sequence = cpu_to_be32(be32_to_cpu(jsb->s_sequence) + 1);
  462. /* get the superblock */
  463. ext4_read_superblock((char *)fs->sb);
  464. fs->sb->feature_incompat |= EXT3_FEATURE_INCOMPAT_RECOVER;
  465. /* Update the super block */
  466. put_ext4((uint64_t) (SUPERBLOCK_SIZE),
  467. (struct ext2_sblock *)fs->sb,
  468. (uint32_t) SUPERBLOCK_SIZE);
  469. ext4_read_superblock((char *)fs->sb);
  470. blknr = read_allocated_block(&inode_journal,
  471. EXT2_JOURNAL_SUPERBLOCK);
  472. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  473. (struct journal_superblock_t *)temp_buff,
  474. (uint32_t) fs->blksz);
  475. ext4fs_free_revoke_blks();
  476. }
  477. free(temp_buff);
  478. free(temp_buff1);
  479. return 0;
  480. }
  481. static void update_descriptor_block(long int blknr)
  482. {
  483. int i;
  484. long int jsb_blknr;
  485. struct journal_header_t jdb;
  486. struct ext3_journal_block_tag tag;
  487. struct ext2_inode inode_journal;
  488. struct journal_superblock_t *jsb = NULL;
  489. char *buf = NULL;
  490. char *temp = NULL;
  491. struct ext_filesystem *fs = get_fs();
  492. char *temp_buff = zalloc(fs->blksz);
  493. if (!temp_buff)
  494. return;
  495. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  496. jsb_blknr = read_allocated_block(&inode_journal,
  497. EXT2_JOURNAL_SUPERBLOCK);
  498. ext4fs_devread((lbaint_t)jsb_blknr * fs->sect_perblk, 0, fs->blksz,
  499. temp_buff);
  500. jsb = (struct journal_superblock_t *) temp_buff;
  501. jdb.h_blocktype = cpu_to_be32(EXT3_JOURNAL_DESCRIPTOR_BLOCK);
  502. jdb.h_magic = cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER);
  503. jdb.h_sequence = jsb->s_sequence;
  504. buf = zalloc(fs->blksz);
  505. if (!buf) {
  506. free(temp_buff);
  507. return;
  508. }
  509. temp = buf;
  510. memcpy(buf, &jdb, sizeof(struct journal_header_t));
  511. temp += sizeof(struct journal_header_t);
  512. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  513. if (journal_ptr[i]->blknr == -1)
  514. break;
  515. tag.block = cpu_to_be32(journal_ptr[i]->blknr);
  516. tag.flags = cpu_to_be32(EXT3_JOURNAL_FLAG_SAME_UUID);
  517. memcpy(temp, &tag, sizeof(struct ext3_journal_block_tag));
  518. temp = temp + sizeof(struct ext3_journal_block_tag);
  519. }
  520. tag.block = cpu_to_be32(journal_ptr[--i]->blknr);
  521. tag.flags = cpu_to_be32(EXT3_JOURNAL_FLAG_LAST_TAG);
  522. memcpy(temp - sizeof(struct ext3_journal_block_tag), &tag,
  523. sizeof(struct ext3_journal_block_tag));
  524. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), buf, (uint32_t) fs->blksz);
  525. free(temp_buff);
  526. free(buf);
  527. }
  528. static void update_commit_block(long int blknr)
  529. {
  530. struct journal_header_t jdb;
  531. struct ext_filesystem *fs = get_fs();
  532. char *buf = NULL;
  533. struct ext2_inode inode_journal;
  534. struct journal_superblock_t *jsb;
  535. long int jsb_blknr;
  536. char *temp_buff = zalloc(fs->blksz);
  537. if (!temp_buff)
  538. return;
  539. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  540. &inode_journal);
  541. jsb_blknr = read_allocated_block(&inode_journal,
  542. EXT2_JOURNAL_SUPERBLOCK);
  543. ext4fs_devread((lbaint_t)jsb_blknr * fs->sect_perblk, 0, fs->blksz,
  544. temp_buff);
  545. jsb = (struct journal_superblock_t *) temp_buff;
  546. jdb.h_blocktype = cpu_to_be32(EXT3_JOURNAL_COMMIT_BLOCK);
  547. jdb.h_magic = cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER);
  548. jdb.h_sequence = jsb->s_sequence;
  549. buf = zalloc(fs->blksz);
  550. if (!buf) {
  551. free(temp_buff);
  552. return;
  553. }
  554. memcpy(buf, &jdb, sizeof(struct journal_header_t));
  555. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), buf, (uint32_t) fs->blksz);
  556. free(temp_buff);
  557. free(buf);
  558. }
  559. void ext4fs_update_journal(void)
  560. {
  561. struct ext2_inode inode_journal;
  562. struct ext_filesystem *fs = get_fs();
  563. long int blknr;
  564. int i;
  565. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  566. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++);
  567. update_descriptor_block(blknr);
  568. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  569. if (journal_ptr[i]->blknr == -1)
  570. break;
  571. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++);
  572. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  573. journal_ptr[i]->buf, fs->blksz);
  574. }
  575. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++);
  576. update_commit_block(blknr);
  577. printf("update journal finished\n");
  578. }