PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ext4/ext4_journal.c

https://bitbucket.org/hldspb/uboot-sbc8600
C | 660 lines | 548 code | 76 blank | 36 comment | 121 complexity | 1310c91319eb033f1113ef7f2a7e60b4 MD5 | raw file
  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, uint32_t 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, uint32_t 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. if (dirty_block_ptr[gd_index]->buf)
  171. assert(dirty_block_ptr[gd_index]->blknr == blknr);
  172. else
  173. dirty_block_ptr[gd_index]->buf = zalloc(fs->blksz);
  174. if (!dirty_block_ptr[gd_index]->buf)
  175. return -ENOMEM;
  176. memcpy(dirty_block_ptr[gd_index]->buf, metadata_buffer, fs->blksz);
  177. dirty_block_ptr[gd_index++]->blknr = blknr;
  178. return 0;
  179. }
  180. void print_revoke_blks(char *revk_blk)
  181. {
  182. int offset;
  183. int max;
  184. long int blocknr;
  185. struct journal_revoke_header_t *header;
  186. if (revk_blk == NULL)
  187. return;
  188. header = (struct journal_revoke_header_t *) revk_blk;
  189. offset = sizeof(struct journal_revoke_header_t);
  190. max = be32_to_cpu(header->r_count);
  191. printf("total bytes %d\n", max);
  192. while (offset < max) {
  193. blocknr = be32_to_cpu(*((__be32 *)(revk_blk + offset)));
  194. printf("revoke blknr is %ld\n", blocknr);
  195. offset += 4;
  196. }
  197. }
  198. static struct revoke_blk_list *_get_node(void)
  199. {
  200. struct revoke_blk_list *tmp_node;
  201. tmp_node = zalloc(sizeof(struct revoke_blk_list));
  202. if (tmp_node == NULL)
  203. return NULL;
  204. tmp_node->content = NULL;
  205. tmp_node->next = NULL;
  206. return tmp_node;
  207. }
  208. void ext4fs_push_revoke_blk(char *buffer)
  209. {
  210. struct revoke_blk_list *node = NULL;
  211. struct ext_filesystem *fs = get_fs();
  212. if (buffer == NULL) {
  213. printf("buffer ptr is NULL\n");
  214. return;
  215. }
  216. node = _get_node();
  217. if (!node) {
  218. printf("_get_node: malloc failed\n");
  219. return;
  220. }
  221. node->content = zalloc(fs->blksz);
  222. if (node->content == NULL)
  223. return;
  224. memcpy(node->content, buffer, fs->blksz);
  225. if (first_node == true) {
  226. revk_blk_list = node;
  227. prev_node = node;
  228. first_node = false;
  229. } else {
  230. prev_node->next = node;
  231. prev_node = node;
  232. }
  233. }
  234. void ext4fs_free_revoke_blks(void)
  235. {
  236. struct revoke_blk_list *tmp_node = revk_blk_list;
  237. struct revoke_blk_list *next_node = NULL;
  238. while (tmp_node != NULL) {
  239. if (tmp_node->content)
  240. free(tmp_node->content);
  241. tmp_node = tmp_node->next;
  242. }
  243. tmp_node = revk_blk_list;
  244. while (tmp_node != NULL) {
  245. next_node = tmp_node->next;
  246. free(tmp_node);
  247. tmp_node = next_node;
  248. }
  249. revk_blk_list = NULL;
  250. prev_node = NULL;
  251. first_node = true;
  252. }
  253. int check_blknr_for_revoke(long int blknr, int sequence_no)
  254. {
  255. struct journal_revoke_header_t *header;
  256. int offset;
  257. int max;
  258. long int blocknr;
  259. char *revk_blk;
  260. struct revoke_blk_list *tmp_revk_node = revk_blk_list;
  261. while (tmp_revk_node != NULL) {
  262. revk_blk = tmp_revk_node->content;
  263. header = (struct journal_revoke_header_t *) revk_blk;
  264. if (sequence_no < be32_to_cpu(header->r_header.h_sequence)) {
  265. offset = sizeof(struct journal_revoke_header_t);
  266. max = be32_to_cpu(header->r_count);
  267. while (offset < max) {
  268. blocknr = be32_to_cpu(*((__be32 *)
  269. (revk_blk + offset)));
  270. if (blocknr == blknr)
  271. goto found;
  272. offset += 4;
  273. }
  274. }
  275. tmp_revk_node = tmp_revk_node->next;
  276. }
  277. return -1;
  278. found:
  279. return 0;
  280. }
  281. /*
  282. * This function parses the journal blocks and replays the
  283. * suceessful transactions. A transaction is successfull
  284. * if commit block is found for a descriptor block
  285. * The tags in descriptor block contain the disk block
  286. * numbers of the metadata to be replayed
  287. */
  288. void recover_transaction(int prev_desc_logical_no)
  289. {
  290. struct ext2_inode inode_journal;
  291. struct ext_filesystem *fs = get_fs();
  292. struct journal_header_t *jdb;
  293. long int blknr;
  294. char *p_jdb;
  295. int ofs, flags;
  296. int i;
  297. struct ext3_journal_block_tag *tag;
  298. char *temp_buff = zalloc(fs->blksz);
  299. char *metadata_buff = zalloc(fs->blksz);
  300. if (!temp_buff || !metadata_buff)
  301. goto fail;
  302. i = prev_desc_logical_no;
  303. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  304. (struct ext2_inode *)&inode_journal);
  305. blknr = read_allocated_block((struct ext2_inode *)
  306. &inode_journal, i);
  307. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  308. temp_buff);
  309. p_jdb = (char *)temp_buff;
  310. jdb = (struct journal_header_t *) temp_buff;
  311. ofs = sizeof(struct journal_header_t);
  312. do {
  313. tag = (struct ext3_journal_block_tag *)(p_jdb + ofs);
  314. ofs += sizeof(struct ext3_journal_block_tag);
  315. if (ofs > fs->blksz)
  316. break;
  317. flags = be32_to_cpu(tag->flags);
  318. if (!(flags & EXT3_JOURNAL_FLAG_SAME_UUID))
  319. ofs += 16;
  320. i++;
  321. debug("\t\ttag %u\n", be32_to_cpu(tag->block));
  322. if (revk_blk_list != NULL) {
  323. if (check_blknr_for_revoke(be32_to_cpu(tag->block),
  324. be32_to_cpu(jdb->h_sequence)) == 0)
  325. continue;
  326. }
  327. blknr = read_allocated_block(&inode_journal, i);
  328. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  329. fs->blksz, metadata_buff);
  330. put_ext4((uint64_t)((uint64_t)be32_to_cpu(tag->block) * (uint64_t)fs->blksz),
  331. metadata_buff, (uint32_t) fs->blksz);
  332. } while (!(flags & EXT3_JOURNAL_FLAG_LAST_TAG));
  333. fail:
  334. free(temp_buff);
  335. free(metadata_buff);
  336. }
  337. void print_jrnl_status(int recovery_flag)
  338. {
  339. if (recovery_flag == RECOVER)
  340. printf("Journal Recovery Completed\n");
  341. else
  342. printf("Journal Scan Completed\n");
  343. }
  344. int ext4fs_check_journal_state(int recovery_flag)
  345. {
  346. int i;
  347. int DB_FOUND = NO;
  348. long int blknr;
  349. int transaction_state = TRANSACTION_COMPLETE;
  350. int prev_desc_logical_no = 0;
  351. int curr_desc_logical_no = 0;
  352. int ofs, flags;
  353. struct ext2_inode inode_journal;
  354. struct journal_superblock_t *jsb = NULL;
  355. struct journal_header_t *jdb = NULL;
  356. char *p_jdb = NULL;
  357. struct ext3_journal_block_tag *tag = NULL;
  358. char *temp_buff = NULL;
  359. char *temp_buff1 = NULL;
  360. struct ext_filesystem *fs = get_fs();
  361. temp_buff = zalloc(fs->blksz);
  362. if (!temp_buff)
  363. return -ENOMEM;
  364. temp_buff1 = zalloc(fs->blksz);
  365. if (!temp_buff1) {
  366. free(temp_buff);
  367. return -ENOMEM;
  368. }
  369. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  370. blknr = read_allocated_block(&inode_journal, EXT2_JOURNAL_SUPERBLOCK);
  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);
  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);
  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);
  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);
  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. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, &inode_journal);
  572. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++);
  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. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  579. journal_ptr[i]->buf, fs->blksz);
  580. }
  581. blknr = read_allocated_block(&inode_journal, jrnl_blk_idx++);
  582. update_commit_block(blknr);
  583. printf("update journal finished\n");
  584. }