ext4_journal.c 17 KB

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