ext4_journal.c 17 KB

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