ext4_journal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 <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. free(dirty_block_ptr[i]->buf);
  99. }
  100. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  101. if (journal_ptr[i]->blknr == -1)
  102. break;
  103. free(journal_ptr[i]->buf);
  104. }
  105. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  106. free(journal_ptr[i]);
  107. free(dirty_block_ptr[i]);
  108. }
  109. gindex = 0;
  110. gd_index = 0;
  111. jrnl_blk_idx = 1;
  112. }
  113. int ext4fs_log_gdt(char *gd_table)
  114. {
  115. struct ext_filesystem *fs = get_fs();
  116. short i;
  117. long int var = fs->gdtable_blkno;
  118. for (i = 0; i < fs->no_blk_pergdt; i++) {
  119. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  120. if (!journal_ptr[gindex]->buf)
  121. return -ENOMEM;
  122. memcpy(journal_ptr[gindex]->buf, gd_table, fs->blksz);
  123. gd_table += fs->blksz;
  124. journal_ptr[gindex++]->blknr = var++;
  125. }
  126. return 0;
  127. }
  128. /*
  129. * This function stores the backup copy of meta data in RAM
  130. * journal_buffer -- Buffer containing meta data
  131. * blknr -- Block number on disk of the meta data buffer
  132. */
  133. int ext4fs_log_journal(char *journal_buffer, uint32_t blknr)
  134. {
  135. struct ext_filesystem *fs = get_fs();
  136. short i;
  137. if (!journal_buffer) {
  138. printf("Invalid input arguments %s\n", __func__);
  139. return -EINVAL;
  140. }
  141. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  142. if (journal_ptr[i]->blknr == -1)
  143. break;
  144. if (journal_ptr[i]->blknr == blknr)
  145. return 0;
  146. }
  147. journal_ptr[gindex]->buf = zalloc(fs->blksz);
  148. if (!journal_ptr[gindex]->buf)
  149. return -ENOMEM;
  150. memcpy(journal_ptr[gindex]->buf, journal_buffer, fs->blksz);
  151. journal_ptr[gindex++]->blknr = blknr;
  152. return 0;
  153. }
  154. /*
  155. * This function stores the modified meta data in RAM
  156. * metadata_buffer -- Buffer containing meta data
  157. * blknr -- Block number on disk of the meta data buffer
  158. */
  159. int ext4fs_put_metadata(char *metadata_buffer, uint32_t blknr)
  160. {
  161. struct ext_filesystem *fs = get_fs();
  162. if (!metadata_buffer) {
  163. printf("Invalid input arguments %s\n", __func__);
  164. return -EINVAL;
  165. }
  166. if (dirty_block_ptr[gd_index]->buf)
  167. assert(dirty_block_ptr[gd_index]->blknr == blknr);
  168. else
  169. dirty_block_ptr[gd_index]->buf = zalloc(fs->blksz);
  170. if (!dirty_block_ptr[gd_index]->buf)
  171. return -ENOMEM;
  172. memcpy(dirty_block_ptr[gd_index]->buf, metadata_buffer, fs->blksz);
  173. dirty_block_ptr[gd_index++]->blknr = blknr;
  174. return 0;
  175. }
  176. void print_revoke_blks(char *revk_blk)
  177. {
  178. int offset;
  179. int max;
  180. long int blocknr;
  181. struct journal_revoke_header_t *header;
  182. if (revk_blk == NULL)
  183. return;
  184. header = (struct journal_revoke_header_t *) revk_blk;
  185. offset = sizeof(struct journal_revoke_header_t);
  186. max = be32_to_cpu(header->r_count);
  187. printf("total bytes %d\n", max);
  188. while (offset < max) {
  189. blocknr = be32_to_cpu(*((__be32 *)(revk_blk + offset)));
  190. printf("revoke blknr is %ld\n", blocknr);
  191. offset += 4;
  192. }
  193. }
  194. static struct revoke_blk_list *_get_node(void)
  195. {
  196. struct revoke_blk_list *tmp_node;
  197. tmp_node = zalloc(sizeof(struct revoke_blk_list));
  198. if (tmp_node == NULL)
  199. return NULL;
  200. tmp_node->content = NULL;
  201. tmp_node->next = NULL;
  202. return tmp_node;
  203. }
  204. void ext4fs_push_revoke_blk(char *buffer)
  205. {
  206. struct revoke_blk_list *node = NULL;
  207. struct ext_filesystem *fs = get_fs();
  208. if (buffer == NULL) {
  209. printf("buffer ptr is NULL\n");
  210. return;
  211. }
  212. node = _get_node();
  213. if (!node) {
  214. printf("_get_node: malloc failed\n");
  215. return;
  216. }
  217. node->content = zalloc(fs->blksz);
  218. if (node->content == NULL)
  219. return;
  220. memcpy(node->content, buffer, fs->blksz);
  221. if (first_node == true) {
  222. revk_blk_list = node;
  223. prev_node = node;
  224. first_node = false;
  225. } else {
  226. prev_node->next = node;
  227. prev_node = node;
  228. }
  229. }
  230. void ext4fs_free_revoke_blks(void)
  231. {
  232. struct revoke_blk_list *tmp_node = revk_blk_list;
  233. struct revoke_blk_list *next_node = NULL;
  234. while (tmp_node != NULL) {
  235. free(tmp_node->content);
  236. tmp_node = tmp_node->next;
  237. }
  238. tmp_node = revk_blk_list;
  239. while (tmp_node != NULL) {
  240. next_node = tmp_node->next;
  241. free(tmp_node);
  242. tmp_node = next_node;
  243. }
  244. revk_blk_list = NULL;
  245. prev_node = NULL;
  246. first_node = true;
  247. }
  248. int check_blknr_for_revoke(long int blknr, int sequence_no)
  249. {
  250. struct journal_revoke_header_t *header;
  251. int offset;
  252. int max;
  253. long int blocknr;
  254. char *revk_blk;
  255. struct revoke_blk_list *tmp_revk_node = revk_blk_list;
  256. while (tmp_revk_node != NULL) {
  257. revk_blk = tmp_revk_node->content;
  258. header = (struct journal_revoke_header_t *) revk_blk;
  259. if (sequence_no < be32_to_cpu(header->r_header.h_sequence)) {
  260. offset = sizeof(struct journal_revoke_header_t);
  261. max = be32_to_cpu(header->r_count);
  262. while (offset < max) {
  263. blocknr = be32_to_cpu(*((__be32 *)
  264. (revk_blk + offset)));
  265. if (blocknr == blknr)
  266. goto found;
  267. offset += 4;
  268. }
  269. }
  270. tmp_revk_node = tmp_revk_node->next;
  271. }
  272. return -1;
  273. found:
  274. return 0;
  275. }
  276. /*
  277. * This function parses the journal blocks and replays the
  278. * suceessful transactions. A transaction is successfull
  279. * if commit block is found for a descriptor block
  280. * The tags in descriptor block contain the disk block
  281. * numbers of the metadata to be replayed
  282. */
  283. void recover_transaction(int prev_desc_logical_no)
  284. {
  285. struct ext2_inode inode_journal;
  286. struct ext_filesystem *fs = get_fs();
  287. struct journal_header_t *jdb;
  288. long int blknr;
  289. char *p_jdb;
  290. int ofs, flags;
  291. int i;
  292. struct ext3_journal_block_tag *tag;
  293. char *temp_buff = zalloc(fs->blksz);
  294. char *metadata_buff = zalloc(fs->blksz);
  295. if (!temp_buff || !metadata_buff)
  296. goto fail;
  297. i = prev_desc_logical_no;
  298. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  299. (struct ext2_inode *)&inode_journal);
  300. blknr = read_allocated_block((struct ext2_inode *)
  301. &inode_journal, i, NULL);
  302. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  303. temp_buff);
  304. p_jdb = (char *)temp_buff;
  305. jdb = (struct journal_header_t *) temp_buff;
  306. ofs = sizeof(struct journal_header_t);
  307. do {
  308. tag = (struct ext3_journal_block_tag *)(p_jdb + ofs);
  309. ofs += sizeof(struct ext3_journal_block_tag);
  310. if (ofs > fs->blksz)
  311. break;
  312. flags = be32_to_cpu(tag->flags);
  313. if (!(flags & EXT3_JOURNAL_FLAG_SAME_UUID))
  314. ofs += 16;
  315. i++;
  316. debug("\t\ttag %u\n", be32_to_cpu(tag->block));
  317. if (revk_blk_list != NULL) {
  318. if (check_blknr_for_revoke(be32_to_cpu(tag->block),
  319. be32_to_cpu(jdb->h_sequence)) == 0)
  320. continue;
  321. }
  322. blknr = read_allocated_block(&inode_journal, i, NULL);
  323. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  324. fs->blksz, metadata_buff);
  325. put_ext4((uint64_t)((uint64_t)be32_to_cpu(tag->block) * (uint64_t)fs->blksz),
  326. metadata_buff, (uint32_t) fs->blksz);
  327. } while (!(flags & EXT3_JOURNAL_FLAG_LAST_TAG));
  328. fail:
  329. free(temp_buff);
  330. free(metadata_buff);
  331. }
  332. void print_jrnl_status(int recovery_flag)
  333. {
  334. if (recovery_flag == RECOVER)
  335. printf("Journal Recovery Completed\n");
  336. else
  337. printf("Journal Scan Completed\n");
  338. }
  339. int ext4fs_check_journal_state(int recovery_flag)
  340. {
  341. int i;
  342. int DB_FOUND = NO;
  343. long int blknr;
  344. int transaction_state = TRANSACTION_COMPLETE;
  345. int prev_desc_logical_no = 0;
  346. int curr_desc_logical_no = 0;
  347. int ofs, flags;
  348. struct ext2_inode inode_journal;
  349. struct journal_superblock_t *jsb = NULL;
  350. struct journal_header_t *jdb = NULL;
  351. char *p_jdb = NULL;
  352. struct ext3_journal_block_tag *tag = NULL;
  353. char *temp_buff = NULL;
  354. char *temp_buff1 = NULL;
  355. struct ext_filesystem *fs = get_fs();
  356. if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
  357. return 0;
  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. NULL);
  369. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  370. temp_buff);
  371. jsb = (struct journal_superblock_t *) temp_buff;
  372. if (le32_to_cpu(fs->sb->feature_incompat) & EXT3_FEATURE_INCOMPAT_RECOVER) {
  373. if (recovery_flag == RECOVER)
  374. printf("Recovery required\n");
  375. } else {
  376. if (recovery_flag == RECOVER)
  377. printf("File System is consistent\n");
  378. goto end;
  379. }
  380. if (be32_to_cpu(jsb->s_start) == 0)
  381. goto end;
  382. if (!(jsb->s_feature_compat &
  383. cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM)))
  384. jsb->s_feature_compat |=
  385. cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
  386. i = be32_to_cpu(jsb->s_first);
  387. while (1) {
  388. blknr = read_allocated_block(&inode_journal, i, NULL);
  389. memset(temp_buff1, '\0', fs->blksz);
  390. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
  391. 0, fs->blksz, temp_buff1);
  392. jdb = (struct journal_header_t *) temp_buff1;
  393. if (be32_to_cpu(jdb->h_blocktype) ==
  394. EXT3_JOURNAL_DESCRIPTOR_BLOCK) {
  395. if (be32_to_cpu(jdb->h_sequence) !=
  396. be32_to_cpu(jsb->s_sequence)) {
  397. print_jrnl_status(recovery_flag);
  398. break;
  399. }
  400. curr_desc_logical_no = i;
  401. if (transaction_state == TRANSACTION_COMPLETE)
  402. transaction_state = TRANSACTION_RUNNING;
  403. else
  404. return -1;
  405. p_jdb = (char *)temp_buff1;
  406. ofs = sizeof(struct journal_header_t);
  407. do {
  408. tag = (struct ext3_journal_block_tag *)
  409. (p_jdb + ofs);
  410. ofs += sizeof(struct ext3_journal_block_tag);
  411. if (ofs > fs->blksz)
  412. break;
  413. flags = be32_to_cpu(tag->flags);
  414. if (!(flags & EXT3_JOURNAL_FLAG_SAME_UUID))
  415. ofs += 16;
  416. i++;
  417. debug("\t\ttag %u\n", be32_to_cpu(tag->block));
  418. } while (!(flags & EXT3_JOURNAL_FLAG_LAST_TAG));
  419. i++;
  420. DB_FOUND = YES;
  421. } else if (be32_to_cpu(jdb->h_blocktype) ==
  422. EXT3_JOURNAL_COMMIT_BLOCK) {
  423. if (be32_to_cpu(jdb->h_sequence) !=
  424. be32_to_cpu(jsb->s_sequence)) {
  425. print_jrnl_status(recovery_flag);
  426. break;
  427. }
  428. if (transaction_state == TRANSACTION_RUNNING ||
  429. (DB_FOUND == NO)) {
  430. transaction_state = TRANSACTION_COMPLETE;
  431. i++;
  432. jsb->s_sequence =
  433. cpu_to_be32(be32_to_cpu(
  434. jsb->s_sequence) + 1);
  435. }
  436. prev_desc_logical_no = curr_desc_logical_no;
  437. if ((recovery_flag == RECOVER) && (DB_FOUND == YES))
  438. recover_transaction(prev_desc_logical_no);
  439. DB_FOUND = NO;
  440. } else if (be32_to_cpu(jdb->h_blocktype) ==
  441. EXT3_JOURNAL_REVOKE_BLOCK) {
  442. if (be32_to_cpu(jdb->h_sequence) !=
  443. be32_to_cpu(jsb->s_sequence)) {
  444. print_jrnl_status(recovery_flag);
  445. break;
  446. }
  447. if (recovery_flag == SCAN)
  448. ext4fs_push_revoke_blk((char *)jdb);
  449. i++;
  450. } else {
  451. debug("Else Case\n");
  452. if (be32_to_cpu(jdb->h_sequence) !=
  453. be32_to_cpu(jsb->s_sequence)) {
  454. print_jrnl_status(recovery_flag);
  455. break;
  456. }
  457. }
  458. }
  459. end:
  460. if (recovery_flag == RECOVER) {
  461. uint32_t new_feature_incompat;
  462. jsb->s_start = cpu_to_be32(1);
  463. jsb->s_sequence = cpu_to_be32(be32_to_cpu(jsb->s_sequence) + 1);
  464. /* get the superblock */
  465. ext4_read_superblock((char *)fs->sb);
  466. new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
  467. new_feature_incompat |= EXT3_FEATURE_INCOMPAT_RECOVER;
  468. fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
  469. /* Update the super block */
  470. put_ext4((uint64_t) (SUPERBLOCK_SIZE),
  471. (struct ext2_sblock *)fs->sb,
  472. (uint32_t) SUPERBLOCK_SIZE);
  473. ext4_read_superblock((char *)fs->sb);
  474. blknr = read_allocated_block(&inode_journal,
  475. EXT2_JOURNAL_SUPERBLOCK, NULL);
  476. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  477. (struct journal_superblock_t *)temp_buff,
  478. (uint32_t) fs->blksz);
  479. ext4fs_free_revoke_blks();
  480. }
  481. free(temp_buff);
  482. free(temp_buff1);
  483. return 0;
  484. }
  485. static void update_descriptor_block(long int blknr)
  486. {
  487. int i;
  488. long int jsb_blknr;
  489. struct journal_header_t jdb;
  490. struct ext3_journal_block_tag tag;
  491. struct ext2_inode inode_journal;
  492. struct journal_superblock_t *jsb = NULL;
  493. char *buf = NULL;
  494. char *temp = NULL;
  495. struct ext_filesystem *fs = get_fs();
  496. char *temp_buff = zalloc(fs->blksz);
  497. if (!temp_buff)
  498. return;
  499. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  500. jsb_blknr = read_allocated_block(&inode_journal,
  501. EXT2_JOURNAL_SUPERBLOCK, NULL);
  502. ext4fs_devread((lbaint_t)jsb_blknr * fs->sect_perblk, 0, fs->blksz,
  503. temp_buff);
  504. jsb = (struct journal_superblock_t *) temp_buff;
  505. jdb.h_blocktype = cpu_to_be32(EXT3_JOURNAL_DESCRIPTOR_BLOCK);
  506. jdb.h_magic = cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER);
  507. jdb.h_sequence = jsb->s_sequence;
  508. buf = zalloc(fs->blksz);
  509. if (!buf) {
  510. free(temp_buff);
  511. return;
  512. }
  513. temp = buf;
  514. memcpy(buf, &jdb, sizeof(struct journal_header_t));
  515. temp += sizeof(struct journal_header_t);
  516. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  517. if (journal_ptr[i]->blknr == -1)
  518. break;
  519. tag.block = cpu_to_be32(journal_ptr[i]->blknr);
  520. tag.flags = cpu_to_be32(EXT3_JOURNAL_FLAG_SAME_UUID);
  521. memcpy(temp, &tag, sizeof(struct ext3_journal_block_tag));
  522. temp = temp + sizeof(struct ext3_journal_block_tag);
  523. }
  524. tag.block = cpu_to_be32(journal_ptr[--i]->blknr);
  525. tag.flags = cpu_to_be32(EXT3_JOURNAL_FLAG_LAST_TAG);
  526. memcpy(temp - sizeof(struct ext3_journal_block_tag), &tag,
  527. sizeof(struct ext3_journal_block_tag));
  528. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), buf, (uint32_t) fs->blksz);
  529. free(temp_buff);
  530. free(buf);
  531. }
  532. static void update_commit_block(long int blknr)
  533. {
  534. struct journal_header_t jdb;
  535. struct ext_filesystem *fs = get_fs();
  536. char *buf = NULL;
  537. struct ext2_inode inode_journal;
  538. struct journal_superblock_t *jsb;
  539. long int jsb_blknr;
  540. char *temp_buff = zalloc(fs->blksz);
  541. if (!temp_buff)
  542. return;
  543. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  544. &inode_journal);
  545. jsb_blknr = read_allocated_block(&inode_journal,
  546. EXT2_JOURNAL_SUPERBLOCK, NULL);
  547. ext4fs_devread((lbaint_t)jsb_blknr * fs->sect_perblk, 0, fs->blksz,
  548. temp_buff);
  549. jsb = (struct journal_superblock_t *) temp_buff;
  550. jdb.h_blocktype = cpu_to_be32(EXT3_JOURNAL_COMMIT_BLOCK);
  551. jdb.h_magic = cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER);
  552. jdb.h_sequence = jsb->s_sequence;
  553. buf = zalloc(fs->blksz);
  554. if (!buf) {
  555. free(temp_buff);
  556. return;
  557. }
  558. memcpy(buf, &jdb, sizeof(struct journal_header_t));
  559. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), buf, (uint32_t) fs->blksz);
  560. free(temp_buff);
  561. free(buf);
  562. }
  563. void ext4fs_update_journal(void)
  564. {
  565. struct ext2_inode inode_journal;
  566. struct ext_filesystem *fs = get_fs();
  567. long int blknr;
  568. int i;
  569. if (!(fs->sb->feature_compatibility & EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  570. return;
  571. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  572. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++, NULL);
  573. update_descriptor_block(blknr);
  574. for (i = 0; i < MAX_JOURNAL_ENTRIES; i++) {
  575. if (journal_ptr[i]->blknr == -1)
  576. break;
  577. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++,
  578. NULL);
  579. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  580. journal_ptr[i]->buf, fs->blksz);
  581. }
  582. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++, NULL);
  583. update_commit_block(blknr);
  584. printf("update journal finished\n");
  585. }