ext4_journal.c 17 KB

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