ext4_journal: fix compilation warnings (may be used uninitialized)
[lwext4.git] / src / ext4_journal.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  * - The name of the author may not be used to endorse or promote products
16  *   derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /** @addtogroup lwext4
31  * @{
32  */
33 /**
34  * @file  ext4_journal.c
35  * @brief Journal handle functions
36  */
37
38 #include "ext4_config.h"
39 #include "ext4_types.h"
40 #include "ext4_misc.h"
41 #include "ext4_errno.h"
42 #include "ext4_debug.h"
43
44 #include "ext4_fs.h"
45 #include "ext4_super.h"
46 #include "ext4_journal.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_crc32.h"
49 #include "ext4_journal.h"
50
51 #include <string.h>
52 #include <stdlib.h>
53
54 /**@brief  Revoke entry during journal replay.*/
55 struct revoke_entry {
56         /**@brief  Block number not to be replayed.*/
57         ext4_fsblk_t block;
58
59         /**@brief  For any transaction id smaller
60          *         than trans_id, records of @block
61          *         in those transactions should not
62          *         be replayed.*/
63         uint32_t trans_id;
64
65         /**@brief  Revoke tree node.*/
66         RB_ENTRY(revoke_entry) revoke_node;
67 };
68
69 /**@brief  Valid journal replay information.*/
70 struct recover_info {
71         /**@brief  Starting transaction id.*/
72         uint32_t start_trans_id;
73
74         /**@brief  Ending transaction id.*/
75         uint32_t last_trans_id;
76
77         /**@brief  Used as internal argument.*/
78         uint32_t this_trans_id;
79
80         /**@brief  No of transactions went through.*/
81         uint32_t trans_cnt;
82
83         /**@brief  RB-Tree storing revoke entries.*/
84         RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
85 };
86
87 /**@brief  Journal replay internal arguments.*/
88 struct replay_arg {
89         /**@brief  Journal replay information.*/
90         struct recover_info *info;
91
92         /**@brief  Current block we are on.*/
93         uint32_t *this_block;
94
95         /**@brief  Current trans_id we are on.*/
96         uint32_t this_trans_id;
97 };
98
99 static int
100 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
101 {
102         if (a->block > b->block)
103                 return 1;
104         else if (a->block < b->block)
105                 return -1;
106         return 0;
107 }
108
109 static int
110 jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b)
111 {
112         if (a->lba > b->lba)
113                 return 1;
114         else if (a->lba < b->lba)
115                 return -1;
116         return 0;
117 }
118
119 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
120                      jbd_revoke_entry_cmp, static inline)
121 RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
122                      jbd_block_rec_cmp, static inline)
123
124 #define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
125 #define jbd_free_revoke_entry(addr) free(addr)
126
127 static int jbd_has_csum(struct jbd_sb *jbd_sb)
128 {
129         if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V2))
130                 return 2;
131
132         if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V3))
133                 return 3;
134
135         return 0;
136 }
137
138 #if CONFIG_META_CSUM_ENABLE
139 static uint32_t jbd_sb_csum(struct jbd_sb *jbd_sb)
140 {
141         uint32_t checksum = 0;
142
143         if (jbd_has_csum(jbd_sb)) {
144                 uint32_t orig_checksum = jbd_sb->checksum;
145                 jbd_set32(jbd_sb, checksum, 0);
146                 /* Calculate crc32c checksum against tho whole superblock */
147                 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_sb,
148                                 JBD_SUPERBLOCK_SIZE);
149                 jbd_sb->checksum = orig_checksum;
150         }
151         return checksum;
152 }
153 #else
154 #define jbd_sb_csum(...) 0
155 #endif
156
157 static void jbd_sb_csum_set(struct jbd_sb *jbd_sb)
158 {
159         if (!jbd_has_csum(jbd_sb))
160                 return;
161
162         jbd_set32(jbd_sb, checksum, jbd_sb_csum(jbd_sb));
163 }
164
165 #if CONFIG_META_CSUM_ENABLE
166 static bool
167 jbd_verify_sb_csum(struct jbd_sb *jbd_sb)
168 {
169         if (!jbd_has_csum(jbd_sb))
170                 return true;
171
172         return jbd_sb_csum(jbd_sb) == jbd_get32(jbd_sb, checksum);
173 }
174 #else
175 #define jbd_verify_sb_csum(...) true
176 #endif
177
178 #if CONFIG_META_CSUM_ENABLE
179 static uint32_t jbd_meta_csum(struct jbd_fs *jbd_fs,
180                               struct jbd_bhdr *bhdr)
181 {
182         uint32_t checksum = 0;
183
184         if (jbd_has_csum(&jbd_fs->sb)) {
185                 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
186                 struct jbd_block_tail *tail =
187                         (struct jbd_block_tail *)((char *)bhdr + block_size -
188                                 sizeof(struct jbd_block_tail));
189                 uint32_t orig_checksum = tail->checksum;
190                 tail->checksum = 0;
191
192                 /* First calculate crc32c checksum against fs uuid */
193                 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
194                                        sizeof(jbd_fs->sb.uuid));
195                 /* Calculate crc32c checksum against tho whole block */
196                 checksum = ext4_crc32c(checksum, bhdr,
197                                 block_size);
198                 tail->checksum = orig_checksum;
199         }
200         return checksum;
201 }
202 #else
203 #define jbd_meta_csum(...) 0
204 #endif
205
206 static void jbd_meta_csum_set(struct jbd_fs *jbd_fs,
207                               struct jbd_bhdr *bhdr)
208 {
209         uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
210         struct jbd_block_tail *tail = (struct jbd_block_tail *)
211                                 ((char *)bhdr + block_size -
212                                 sizeof(struct jbd_block_tail));
213         if (!jbd_has_csum(&jbd_fs->sb))
214                 return;
215
216         tail->checksum = to_be32(jbd_meta_csum(jbd_fs, bhdr));
217 }
218
219 #if CONFIG_META_CSUM_ENABLE
220 static bool
221 jbd_verify_meta_csum(struct jbd_fs *jbd_fs,
222                      struct jbd_bhdr *bhdr)
223 {
224         uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
225         struct jbd_block_tail *tail = (struct jbd_block_tail *)
226                                 ((char *)bhdr + block_size -
227                                 sizeof(struct jbd_block_tail));
228         if (!jbd_has_csum(&jbd_fs->sb))
229                 return true;
230
231         return jbd_meta_csum(jbd_fs, bhdr) == to_be32(tail->checksum);
232 }
233 #else
234 #define jbd_verify_meta_csum(...) true
235 #endif
236
237 #if CONFIG_META_CSUM_ENABLE
238 static uint32_t jbd_commit_csum(struct jbd_fs *jbd_fs,
239                               struct jbd_commit_header *header)
240 {
241         uint32_t checksum = 0;
242
243         if (jbd_has_csum(&jbd_fs->sb)) {
244                 uint32_t orig_checksum_type = header->chksum_type,
245                          orig_checksum_size = header->chksum_size,
246                          orig_checksum = header->chksum[0];
247                 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
248                 header->chksum_type = 0;
249                 header->chksum_size = 0;
250                 header->chksum[0] = 0;
251
252                 /* First calculate crc32c checksum against fs uuid */
253                 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
254                                        sizeof(jbd_fs->sb.uuid));
255                 /* Calculate crc32c checksum against tho whole block */
256                 checksum = ext4_crc32c(checksum, header,
257                                 block_size);
258
259                 header->chksum_type = orig_checksum_type;
260                 header->chksum_size = orig_checksum_size;
261                 header->chksum[0] = orig_checksum;
262         }
263         return checksum;
264 }
265 #else
266 #define jbd_commit_csum(...) 0
267 #endif
268
269 static void jbd_commit_csum_set(struct jbd_fs *jbd_fs,
270                               struct jbd_commit_header *header)
271 {
272         if (!jbd_has_csum(&jbd_fs->sb))
273                 return;
274
275         header->chksum_type = 0;
276         header->chksum_size = 0;
277         header->chksum[0] = jbd_commit_csum(jbd_fs, header);
278 }
279
280 #if CONFIG_META_CSUM_ENABLE
281 static bool jbd_verify_commit_csum(struct jbd_fs *jbd_fs,
282                                    struct jbd_commit_header *header)
283 {
284         if (!jbd_has_csum(&jbd_fs->sb))
285                 return true;
286
287         return header->chksum[0] == to_be32(jbd_commit_csum(jbd_fs,
288                                             header));
289 }
290 #else
291 #define jbd_verify_commit_csum(...) true
292 #endif
293
294 #if CONFIG_META_CSUM_ENABLE
295 /*
296  * NOTE: We only make use of @csum parameter when
297  *       JBD_FEATURE_COMPAT_CHECKSUM is enabled.
298  */
299 static uint32_t jbd_block_csum(struct jbd_fs *jbd_fs, const void *buf,
300                                uint32_t csum,
301                                uint32_t sequence)
302 {
303         uint32_t checksum = 0;
304
305         if (jbd_has_csum(&jbd_fs->sb)) {
306                 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
307                 /* First calculate crc32c checksum against fs uuid */
308                 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
309                                        sizeof(jbd_fs->sb.uuid));
310                 /* Then calculate crc32c checksum against sequence no. */
311                 checksum = ext4_crc32c(checksum, &sequence,
312                                 sizeof(uint32_t));
313                 /* Calculate crc32c checksum against tho whole block */
314                 checksum = ext4_crc32c(checksum, buf,
315                                 block_size);
316         } else if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
317                                      JBD_FEATURE_COMPAT_CHECKSUM)) {
318                 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
319                 /* Calculate crc32c checksum against tho whole block */
320                 checksum = ext4_crc32(csum, buf,
321                                 block_size);
322         }
323         return checksum;
324 }
325 #else
326 #define jbd_block_csum(...) 0
327 #endif
328
329 static void jbd_block_tag_csum_set(struct jbd_fs *jbd_fs, void *__tag,
330                                    uint32_t checksum)
331 {
332         int ver = jbd_has_csum(&jbd_fs->sb);
333         if (!ver)
334                 return;
335
336         if (ver == 2) {
337                 struct jbd_block_tag *tag = __tag;
338                 tag->checksum = (uint16_t)to_be32(checksum);
339         } else {
340                 struct jbd_block_tag3 *tag = __tag;
341                 tag->checksum = to_be32(checksum);
342         }
343 }
344
345 /**@brief  Write jbd superblock to disk.
346  * @param  jbd_fs jbd filesystem
347  * @param  s jbd superblock
348  * @return standard error code*/
349 static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
350 {
351         int rc;
352         struct ext4_fs *fs = jbd_fs->inode_ref.fs;
353         uint64_t offset;
354         ext4_fsblk_t fblock;
355         rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
356         if (rc != EOK)
357                 return rc;
358
359         jbd_sb_csum_set(s);
360         offset = fblock * ext4_sb_get_block_size(&fs->sb);
361         return ext4_block_writebytes(fs->bdev, offset, s,
362                                      EXT4_SUPERBLOCK_SIZE);
363 }
364
365 /**@brief  Read jbd superblock from disk.
366  * @param  jbd_fs jbd filesystem
367  * @param  s jbd superblock
368  * @return standard error code*/
369 static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
370 {
371         int rc;
372         struct ext4_fs *fs = jbd_fs->inode_ref.fs;
373         uint64_t offset;
374         ext4_fsblk_t fblock;
375         rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
376         if (rc != EOK)
377                 return rc;
378
379         offset = fblock * ext4_sb_get_block_size(&fs->sb);
380         return ext4_block_readbytes(fs->bdev, offset, s,
381                                     EXT4_SUPERBLOCK_SIZE);
382 }
383
384 /**@brief  Verify jbd superblock.
385  * @param  sb jbd superblock
386  * @return true if jbd superblock is valid */
387 static bool jbd_verify_sb(struct jbd_sb *sb)
388 {
389         struct jbd_bhdr *header = &sb->header;
390         if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
391                 return false;
392
393         if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
394             jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
395                 return false;
396
397         return jbd_verify_sb_csum(sb);
398 }
399
400 /**@brief  Write back dirty jbd superblock to disk.
401  * @param  jbd_fs jbd filesystem
402  * @return standard error code*/
403 static int jbd_write_sb(struct jbd_fs *jbd_fs)
404 {
405         int rc = EOK;
406         if (jbd_fs->dirty) {
407                 rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
408                 if (rc != EOK)
409                         return rc;
410
411                 jbd_fs->dirty = false;
412         }
413         return rc;
414 }
415
416 /**@brief  Get reference to jbd filesystem.
417  * @param  fs Filesystem to load journal of
418  * @param  jbd_fs jbd filesystem
419  * @return standard error code*/
420 int jbd_get_fs(struct ext4_fs *fs,
421                struct jbd_fs *jbd_fs)
422 {
423         int rc;
424         uint32_t journal_ino;
425
426         memset(jbd_fs, 0, sizeof(struct jbd_fs));
427         /* See if there is journal inode on this filesystem.*/
428         /* FIXME: detection on existance ofbkejournal bdev is
429          *        missing.*/
430         journal_ino = ext4_get32(&fs->sb, journal_inode_number);
431
432         rc = ext4_fs_get_inode_ref(fs,
433                                    journal_ino,
434                                    &jbd_fs->inode_ref);
435         if (rc != EOK) {
436                 memset(jbd_fs, 0, sizeof(struct jbd_fs));
437                 return rc;
438         }
439         rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
440         if (rc != EOK) {
441                 memset(jbd_fs, 0, sizeof(struct jbd_fs));
442                 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
443                 return rc;
444         }
445         if (!jbd_verify_sb(&jbd_fs->sb)) {
446                 memset(jbd_fs, 0, sizeof(struct jbd_fs));
447                 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
448                 rc = EIO;
449         }
450
451         return rc;
452 }
453
454 /**@brief  Put reference of jbd filesystem.
455  * @param  jbd_fs jbd filesystem
456  * @return standard error code*/
457 int jbd_put_fs(struct jbd_fs *jbd_fs)
458 {
459         int rc = EOK;
460         rc = jbd_write_sb(jbd_fs);
461
462         ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
463         return rc;
464 }
465
466 /**@brief  Data block lookup helper.
467  * @param  jbd_fs jbd filesystem
468  * @param  iblock block index
469  * @param  fblock logical block address
470  * @return standard error code*/
471 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
472                    ext4_lblk_t iblock,
473                    ext4_fsblk_t *fblock)
474 {
475         int rc = ext4_fs_get_inode_dblk_idx(
476                         &jbd_fs->inode_ref,
477                         iblock,
478                         fblock,
479                         false);
480         return rc;
481 }
482
483 /**@brief   jbd block get function (through cache).
484  * @param   jbd_fs jbd filesystem
485  * @param   block block descriptor
486  * @param   fblock jbd logical block address
487  * @return  standard error code*/
488 static int jbd_block_get(struct jbd_fs *jbd_fs,
489                   struct ext4_block *block,
490                   ext4_fsblk_t fblock)
491 {
492         /* TODO: journal device. */
493         int rc;
494         ext4_lblk_t iblock = (ext4_lblk_t)fblock;
495
496         /* Lookup the logical block address of
497          * fblock.*/
498         rc = jbd_inode_bmap(jbd_fs, iblock,
499                             &fblock);
500         if (rc != EOK)
501                 return rc;
502
503         struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
504         rc = ext4_block_get(bdev, block, fblock);
505
506         /* If succeeded, mark buffer as BC_FLUSH to indicate
507          * that data should be written to disk immediately.*/
508         if (rc == EOK) {
509                 ext4_bcache_set_flag(block->buf, BC_FLUSH);
510                 /* As we don't want to occupy too much space
511                  * in block cache, we set this buffer BC_TMP.*/
512                 ext4_bcache_set_flag(block->buf, BC_TMP);
513         }
514
515         return rc;
516 }
517
518 /**@brief   jbd block get function (through cache, don't read).
519  * @param   jbd_fs jbd filesystem
520  * @param   block block descriptor
521  * @param   fblock jbd logical block address
522  * @return  standard error code*/
523 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
524                          struct ext4_block *block,
525                          ext4_fsblk_t fblock)
526 {
527         /* TODO: journal device. */
528         int rc;
529         ext4_lblk_t iblock = (ext4_lblk_t)fblock;
530         rc = jbd_inode_bmap(jbd_fs, iblock,
531                             &fblock);
532         if (rc != EOK)
533                 return rc;
534
535         struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
536         rc = ext4_block_get_noread(bdev, block, fblock);
537         if (rc == EOK)
538                 ext4_bcache_set_flag(block->buf, BC_FLUSH);
539
540         return rc;
541 }
542
543 /**@brief   jbd block set procedure (through cache).
544  * @param   jbd_fs jbd filesystem
545  * @param   block block descriptor
546  * @return  standard error code*/
547 static int jbd_block_set(struct jbd_fs *jbd_fs,
548                   struct ext4_block *block)
549 {
550         return ext4_block_set(jbd_fs->inode_ref.fs->bdev,
551                               block);
552 }
553
554 /**@brief  helper functions to calculate
555  *         block tag size, not including UUID part.
556  * @param  jbd_fs jbd filesystem
557  * @return tag size in bytes*/
558 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
559 {
560         int size;
561
562         /* It is very easy to deal with the case which
563          * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
564         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
565                                      JBD_FEATURE_INCOMPAT_CSUM_V3))
566                 return sizeof(struct jbd_block_tag3);
567
568         size = sizeof(struct jbd_block_tag);
569
570         /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
571          * add 2 bytes to size.*/
572         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
573                                      JBD_FEATURE_INCOMPAT_CSUM_V2))
574                 size += sizeof(uint16_t);
575
576         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
577                                      JBD_FEATURE_INCOMPAT_64BIT))
578                 return size;
579
580         /* If block number is 4 bytes in size,
581          * minus 4 bytes from size */
582         return size - sizeof(uint32_t);
583 }
584
585 /**@brief  Tag information. */
586 struct tag_info {
587         /**@brief  Tag size in bytes, including UUID part.*/
588         int tag_bytes;
589
590         /**@brief  block number stored in this tag.*/
591         ext4_fsblk_t block;
592
593         /**@brief  whether UUID part exists or not.*/
594         bool uuid_exist;
595
596         /**@brief  UUID content if UUID part exists.*/
597         uint8_t uuid[UUID_SIZE];
598
599         /**@brief  Is this the last tag? */
600         bool last_tag;
601
602         /**@brief  crc32c checksum. */
603         uint32_t checksum;
604 };
605
606 /**@brief  Extract information from a block tag.
607  * @param  __tag pointer to the block tag
608  * @param  tag_bytes block tag size of this jbd filesystem
609  * @param  remaining size in buffer containing the block tag
610  * @param  tag_info information of this tag.
611  * @return  EOK when succeed, otherwise return EINVAL.*/
612 static int
613 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
614                       void *__tag,
615                       int tag_bytes,
616                       int32_t remain_buf_size,
617                       struct tag_info *tag_info)
618 {
619         char *uuid_start;
620         tag_info->tag_bytes = tag_bytes;
621         tag_info->uuid_exist = false;
622         tag_info->last_tag = false;
623
624         /* See whether it is possible to hold a valid block tag.*/
625         if (remain_buf_size - tag_bytes < 0)
626                 return EINVAL;
627
628         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
629                                      JBD_FEATURE_INCOMPAT_CSUM_V3)) {
630                 struct jbd_block_tag3 *tag = __tag;
631                 tag_info->block = jbd_get32(tag, blocknr);
632                 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
633                                              JBD_FEATURE_INCOMPAT_64BIT))
634                          tag_info->block |=
635                                  (uint64_t)jbd_get32(tag, blocknr_high) << 32;
636
637                 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
638                         tag_info->block = 0;
639
640                 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
641                         /* See whether it is possible to hold UUID part.*/
642                         if (remain_buf_size - tag_bytes < UUID_SIZE)
643                                 return EINVAL;
644
645                         uuid_start = (char *)tag + tag_bytes;
646                         tag_info->uuid_exist = true;
647                         tag_info->tag_bytes += UUID_SIZE;
648                         memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
649                 }
650
651                 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
652                         tag_info->last_tag = true;
653
654         } else {
655                 struct jbd_block_tag *tag = __tag;
656                 tag_info->block = jbd_get32(tag, blocknr);
657                 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
658                                              JBD_FEATURE_INCOMPAT_64BIT))
659                          tag_info->block |=
660                                  (uint64_t)jbd_get32(tag, blocknr_high) << 32;
661
662                 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
663                         tag_info->block = 0;
664
665                 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
666                         /* See whether it is possible to hold UUID part.*/
667                         if (remain_buf_size - tag_bytes < UUID_SIZE)
668                                 return EINVAL;
669
670                         uuid_start = (char *)tag + tag_bytes;
671                         tag_info->uuid_exist = true;
672                         tag_info->tag_bytes += UUID_SIZE;
673                         memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
674                 }
675
676                 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
677                         tag_info->last_tag = true;
678
679         }
680         return EOK;
681 }
682
683 /**@brief  Write information to a block tag.
684  * @param  __tag pointer to the block tag
685  * @param  remaining size in buffer containing the block tag
686  * @param  tag_info information of this tag.
687  * @return  EOK when succeed, otherwise return EINVAL.*/
688 static int
689 jbd_write_block_tag(struct jbd_fs *jbd_fs,
690                     void *__tag,
691                     int32_t remain_buf_size,
692                     struct tag_info *tag_info)
693 {
694         char *uuid_start;
695         int tag_bytes = jbd_tag_bytes(jbd_fs);
696
697         tag_info->tag_bytes = tag_bytes;
698
699         /* See whether it is possible to hold a valid block tag.*/
700         if (remain_buf_size - tag_bytes < 0)
701                 return EINVAL;
702
703         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
704                                      JBD_FEATURE_INCOMPAT_CSUM_V3)) {
705                 struct jbd_block_tag3 *tag = __tag;
706                 memset(tag, 0, sizeof(struct jbd_block_tag3));
707                 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
708                 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
709                                              JBD_FEATURE_INCOMPAT_64BIT))
710                         jbd_set32(tag, blocknr_high, tag_info->block >> 32);
711
712                 if (tag_info->uuid_exist) {
713                         /* See whether it is possible to hold UUID part.*/
714                         if (remain_buf_size - tag_bytes < UUID_SIZE)
715                                 return EINVAL;
716
717                         uuid_start = (char *)tag + tag_bytes;
718                         tag_info->tag_bytes += UUID_SIZE;
719                         memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
720                 } else
721                         jbd_set32(tag, flags,
722                                   jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
723
724                 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
725
726                 if (tag_info->last_tag)
727                         jbd_set32(tag, flags,
728                                   jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
729
730         } else {
731                 struct jbd_block_tag *tag = __tag;
732                 memset(tag, 0, sizeof(struct jbd_block_tag));
733                 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
734                 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
735                                              JBD_FEATURE_INCOMPAT_64BIT))
736                         jbd_set32(tag, blocknr_high, tag_info->block >> 32);
737
738                 if (tag_info->uuid_exist) {
739                         /* See whether it is possible to hold UUID part.*/
740                         if (remain_buf_size - tag_bytes < UUID_SIZE)
741                                 return EINVAL;
742
743                         uuid_start = (char *)tag + tag_bytes;
744                         tag_info->tag_bytes += UUID_SIZE;
745                         memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
746                 } else
747                         jbd_set16(tag, flags,
748                                   jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
749
750                 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
751
752                 if (tag_info->last_tag)
753                         jbd_set16(tag, flags,
754                                   jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
755
756         }
757         return EOK;
758 }
759
760 /**@brief  Iterate all block tags in a block.
761  * @param  jbd_fs jbd filesystem
762  * @param  __tag_start pointer to the block
763  * @param  tag_tbl_size size of the block
764  * @param  func callback routine to indicate that
765  *         a block tag is found
766  * @param  arg additional argument to be passed to func */
767 static void
768 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
769                         void *__tag_start,
770                         int32_t tag_tbl_size,
771                         void (*func)(struct jbd_fs * jbd_fs,
772                                         ext4_fsblk_t block,
773                                         uint8_t *uuid,
774                                         void *arg),
775                         void *arg)
776 {
777         char *tag_start, *tag_ptr;
778         int tag_bytes = jbd_tag_bytes(jbd_fs);
779         tag_start = __tag_start;
780         tag_ptr = tag_start;
781
782         /* Cut off the size of block tail storing checksum. */
783         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
784                                      JBD_FEATURE_INCOMPAT_CSUM_V2) ||
785             JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
786                                      JBD_FEATURE_INCOMPAT_CSUM_V3))
787                 tag_tbl_size -= sizeof(struct jbd_block_tail);
788
789         while (tag_tbl_size) {
790                 struct tag_info tag_info;
791                 int rc = jbd_extract_block_tag(jbd_fs,
792                                       tag_ptr,
793                                       tag_bytes,
794                                       tag_tbl_size,
795                                       &tag_info);
796                 if (rc != EOK)
797                         break;
798
799                 if (func)
800                         func(jbd_fs, tag_info.block, tag_info.uuid, arg);
801
802                 /* Stop the iteration when we reach the last tag. */
803                 if (tag_info.last_tag)
804                         break;
805
806                 tag_ptr += tag_info.tag_bytes;
807                 tag_tbl_size -= tag_info.tag_bytes;
808         }
809 }
810
811 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
812                                    ext4_fsblk_t block,
813                                    uint8_t *uuid,
814                                    void *arg)
815 {
816         uint32_t *iblock = arg;
817         ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", block);
818         (*iblock)++;
819         (void)jbd_fs;
820         (void)uuid;
821         return;
822 }
823
824 static struct revoke_entry *
825 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
826 {
827         struct revoke_entry tmp = {
828                 .block = block
829         };
830
831         return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
832 }
833
834 /**@brief  Replay a block in a transaction.
835  * @param  jbd_fs jbd filesystem
836  * @param  block  block address to be replayed.*/
837 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
838                                   ext4_fsblk_t block,
839                                   uint8_t *uuid __unused,
840                                   void *__arg)
841 {
842         int r;
843         struct replay_arg *arg = __arg;
844         struct recover_info *info = arg->info;
845         uint32_t *this_block = arg->this_block;
846         struct revoke_entry *revoke_entry;
847         struct ext4_block journal_block, ext4_block;
848         struct ext4_fs *fs = jbd_fs->inode_ref.fs;
849
850         (*this_block)++;
851
852         /* We replay this block only if the current transaction id
853          * is equal or greater than that in revoke entry.*/
854         revoke_entry = jbd_revoke_entry_lookup(info, block);
855         if (revoke_entry &&
856             arg->this_trans_id < revoke_entry->trans_id)
857                 return;
858
859         ext4_dbg(DEBUG_JBD,
860                  "Replaying block in block_tag: %" PRIu64 "\n",
861                  block);
862
863         r = jbd_block_get(jbd_fs, &journal_block, *this_block);
864         if (r != EOK)
865                 return;
866
867         /* We need special treatment for ext4 superblock. */
868         if (block) {
869                 r = ext4_block_get_noread(fs->bdev, &ext4_block, block);
870                 if (r != EOK) {
871                         jbd_block_set(jbd_fs, &journal_block);
872                         return;
873                 }
874
875                 memcpy(ext4_block.data,
876                         journal_block.data,
877                         jbd_get32(&jbd_fs->sb, blocksize));
878
879                 ext4_bcache_set_dirty(ext4_block.buf);
880                 ext4_block_set(fs->bdev, &ext4_block);
881         } else {
882                 uint16_t mount_count, state;
883                 mount_count = ext4_get16(&fs->sb, mount_count);
884                 state = ext4_get16(&fs->sb, state);
885
886                 memcpy(&fs->sb,
887                         journal_block.data + EXT4_SUPERBLOCK_OFFSET,
888                         EXT4_SUPERBLOCK_SIZE);
889
890                 /* Mark system as mounted */
891                 ext4_set16(&fs->sb, state, state);
892                 r = ext4_sb_write(fs->bdev, &fs->sb);
893                 if (r != EOK)
894                         return;
895
896                 /*Update mount count*/
897                 ext4_set16(&fs->sb, mount_count, mount_count);
898         }
899
900         jbd_block_set(jbd_fs, &journal_block);
901         
902         return;
903 }
904
905 /**@brief  Add block address to revoke tree, along with
906  *         its transaction id.
907  * @param  info  journal replay info
908  * @param  block  block address to be replayed.*/
909 static void jbd_add_revoke_block_tags(struct recover_info *info,
910                                       ext4_fsblk_t block)
911 {
912         struct revoke_entry *revoke_entry;
913
914         ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
915         /* If the revoke entry with respect to the block address
916          * exists already, update its transaction id.*/
917         revoke_entry = jbd_revoke_entry_lookup(info, block);
918         if (revoke_entry) {
919                 revoke_entry->trans_id = info->this_trans_id;
920                 return;
921         }
922
923         revoke_entry = jbd_alloc_revoke_entry();
924         ext4_assert(revoke_entry);
925         revoke_entry->block = block;
926         revoke_entry->trans_id = info->this_trans_id;
927         RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
928
929         return;
930 }
931
932 static void jbd_destroy_revoke_tree(struct recover_info *info)
933 {
934         while (!RB_EMPTY(&info->revoke_root)) {
935                 struct revoke_entry *revoke_entry =
936                         RB_MIN(jbd_revoke, &info->revoke_root);
937                 ext4_assert(revoke_entry);
938                 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
939                 jbd_free_revoke_entry(revoke_entry);
940         }
941 }
942
943 /* Make sure we wrap around the log correctly! */
944 #define wrap(sb, var)                                           \
945 do {                                                                    \
946         if (var >= jbd_get32((sb), maxlen))                                     \
947                 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first));      \
948 } while (0)
949
950 #define ACTION_SCAN 0
951 #define ACTION_REVOKE 1
952 #define ACTION_RECOVER 2
953
954 /**@brief  Add entries in a revoke block to revoke tree.
955  * @param  jbd_fs jbd filesystem
956  * @param  header revoke block header
957  * @param  recover_info  journal replay info*/
958 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
959                                   struct jbd_bhdr *header,
960                                   struct recover_info *info)
961 {
962         char *blocks_entry;
963         struct jbd_revoke_header *revoke_hdr =
964                 (struct jbd_revoke_header *)header;
965         uint32_t i, nr_entries, record_len = 4;
966
967         /* If we are working on a 64bit jbd filesystem, */
968         if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
969                                      JBD_FEATURE_INCOMPAT_64BIT))
970                 record_len = 8;
971
972         nr_entries = (jbd_get32(revoke_hdr, count) -
973                         sizeof(struct jbd_revoke_header)) /
974                         record_len;
975
976         blocks_entry = (char *)(revoke_hdr + 1);
977
978         for (i = 0;i < nr_entries;i++) {
979                 if (record_len == 8) {
980                         uint64_t *blocks =
981                                 (uint64_t *)blocks_entry;
982                         jbd_add_revoke_block_tags(info, to_be64(*blocks));
983                 } else {
984                         uint32_t *blocks =
985                                 (uint32_t *)blocks_entry;
986                         jbd_add_revoke_block_tags(info, to_be32(*blocks));
987                 }
988                 blocks_entry += record_len;
989         }
990 }
991
992 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
993                                        struct jbd_bhdr *header,
994                                        uint32_t *iblock)
995 {
996         jbd_iterate_block_table(jbd_fs,
997                                 header + 1,
998                                 jbd_get32(&jbd_fs->sb, blocksize) -
999                                         sizeof(struct jbd_bhdr),
1000                                 jbd_display_block_tags,
1001                                 iblock);
1002 }
1003
1004 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
1005                                         struct jbd_bhdr *header,
1006                                         struct replay_arg *arg)
1007 {
1008         jbd_iterate_block_table(jbd_fs,
1009                                 header + 1,
1010                                 jbd_get32(&jbd_fs->sb, blocksize) -
1011                                         sizeof(struct jbd_bhdr),
1012                                 jbd_replay_block_tags,
1013                                 arg);
1014 }
1015
1016 /**@brief  The core routine of journal replay.
1017  * @param  jbd_fs jbd filesystem
1018  * @param  recover_info  journal replay info
1019  * @param  action action needed to be taken
1020  * @return standard error code*/
1021 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
1022                            struct recover_info *info,
1023                            int action)
1024 {
1025         int r = EOK;
1026         bool log_end = false;
1027         struct jbd_sb *sb = &jbd_fs->sb;
1028         uint32_t start_trans_id, this_trans_id;
1029         uint32_t start_block, this_block;
1030
1031         /* We start iterating valid blocks in the whole journal.*/
1032         start_trans_id = this_trans_id = jbd_get32(sb, sequence);
1033         start_block = this_block = jbd_get32(sb, start);
1034         if (action == ACTION_SCAN)
1035                 info->trans_cnt = 0;
1036         else if (!info->trans_cnt)
1037                 log_end = true;
1038
1039         ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
1040                             start_trans_id);
1041
1042         while (!log_end) {
1043                 struct ext4_block block;
1044                 struct jbd_bhdr *header;
1045                 /* If we are not scanning for the last
1046                  * valid transaction in the journal,
1047                  * we will stop when we reach the end of
1048                  * the journal.*/
1049                 if (action != ACTION_SCAN)
1050                         if (this_trans_id > info->last_trans_id) {
1051                                 log_end = true;
1052                                 continue;
1053                         }
1054
1055                 r = jbd_block_get(jbd_fs, &block, this_block);
1056                 if (r != EOK)
1057                         break;
1058
1059                 header = (struct jbd_bhdr *)block.data;
1060                 /* This block does not have a valid magic number,
1061                  * so we have reached the end of the journal.*/
1062                 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
1063                         jbd_block_set(jbd_fs, &block);
1064                         log_end = true;
1065                         continue;
1066                 }
1067
1068                 /* If the transaction id we found is not expected,
1069                  * we may have reached the end of the journal.
1070                  *
1071                  * If we are not scanning the journal, something
1072                  * bad might have taken place. :-( */
1073                 if (jbd_get32(header, sequence) != this_trans_id) {
1074                         if (action != ACTION_SCAN)
1075                                 r = EIO;
1076
1077                         jbd_block_set(jbd_fs, &block);
1078                         log_end = true;
1079                         continue;
1080                 }
1081
1082                 switch (jbd_get32(header, blocktype)) {
1083                 case JBD_DESCRIPTOR_BLOCK:
1084                         if (!jbd_verify_meta_csum(jbd_fs, header)) {
1085                                 ext4_dbg(DEBUG_JBD,
1086                                         DBG_WARN "Descriptor block checksum failed."
1087                                                 "Journal block: %" PRIu32"\n",
1088                                                 this_block);
1089                                 log_end = true;
1090                                 break;
1091                         }
1092                         ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
1093                                             "trans_id: %" PRIu32"\n",
1094                                             this_block, this_trans_id);
1095                         if (action == ACTION_RECOVER) {
1096                                 struct replay_arg replay_arg;
1097                                 replay_arg.info = info;
1098                                 replay_arg.this_block = &this_block;
1099                                 replay_arg.this_trans_id = this_trans_id;
1100
1101                                 jbd_replay_descriptor_block(jbd_fs,
1102                                                 header, &replay_arg);
1103                         } else
1104                                 jbd_debug_descriptor_block(jbd_fs,
1105                                                 header, &this_block);
1106
1107                         break;
1108                 case JBD_COMMIT_BLOCK:
1109                         if (!jbd_verify_commit_csum(jbd_fs,
1110                                         (struct jbd_commit_header *)header)) {
1111                                 ext4_dbg(DEBUG_JBD,
1112                                         DBG_WARN "Commit block checksum failed."
1113                                                 "Journal block: %" PRIu32"\n",
1114                                                 this_block);
1115                                 log_end = true;
1116                                 break;
1117                         }
1118                         ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
1119                                             "trans_id: %" PRIu32"\n",
1120                                             this_block, this_trans_id);
1121                         /* This is the end of a transaction,
1122                          * we may now proceed to the next transaction.
1123                          */
1124                         this_trans_id++;
1125                         info->trans_cnt++;
1126                         break;
1127                 case JBD_REVOKE_BLOCK:
1128                         if (!jbd_verify_meta_csum(jbd_fs, header)) {
1129                                 ext4_dbg(DEBUG_JBD,
1130                                         DBG_WARN "Revoke block checksum failed."
1131                                                 "Journal block: %" PRIu32"\n",
1132                                                 this_block);
1133                                 log_end = true;
1134                                 break;
1135                         }
1136                         ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
1137                                             "trans_id: %" PRIu32"\n",
1138                                             this_block, this_trans_id);
1139                         if (action == ACTION_REVOKE) {
1140                                 info->this_trans_id = this_trans_id;
1141                                 jbd_build_revoke_tree(jbd_fs,
1142                                                 header, info);
1143                         }
1144                         break;
1145                 default:
1146                         log_end = true;
1147                         break;
1148                 }
1149                 jbd_block_set(jbd_fs, &block);
1150                 this_block++;
1151                 wrap(sb, this_block);
1152                 if (this_block == start_block)
1153                         log_end = true;
1154
1155         }
1156         ext4_dbg(DEBUG_JBD, "End of journal.\n");
1157         if (r == EOK && action == ACTION_SCAN) {
1158                 /* We have finished scanning the journal. */
1159                 info->start_trans_id = start_trans_id;
1160                 if (this_trans_id > start_trans_id)
1161                         info->last_trans_id = this_trans_id - 1;
1162                 else
1163                         info->last_trans_id = this_trans_id;
1164         }
1165
1166         return r;
1167 }
1168
1169 /**@brief  Replay journal.
1170  * @param  jbd_fs jbd filesystem
1171  * @return standard error code*/
1172 int jbd_recover(struct jbd_fs *jbd_fs)
1173 {
1174         int r;
1175         struct recover_info info;
1176         struct jbd_sb *sb = &jbd_fs->sb;
1177         if (!sb->start)
1178                 return EOK;
1179
1180         RB_INIT(&info.revoke_root);
1181
1182         r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
1183         if (r != EOK)
1184                 return r;
1185
1186         r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
1187         if (r != EOK)
1188                 return r;
1189
1190         r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
1191         if (r == EOK) {
1192                 /* If we successfully replay the journal,
1193                  * clear EXT4_FINCOM_RECOVER flag on the
1194                  * ext4 superblock, and set the start of
1195                  * journal to 0.*/
1196                 uint32_t features_incompatible =
1197                         ext4_get32(&jbd_fs->inode_ref.fs->sb,
1198                                    features_incompatible);
1199                 jbd_set32(&jbd_fs->sb, start, 0);
1200                 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1201                 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1202                            features_incompatible,
1203                            features_incompatible);
1204                 jbd_fs->dirty = true;
1205                 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1206                                   &jbd_fs->inode_ref.fs->sb);
1207         }
1208         jbd_destroy_revoke_tree(&info);
1209         return r;
1210 }
1211
1212 static void jbd_journal_write_sb(struct jbd_journal *journal)
1213 {
1214         struct jbd_fs *jbd_fs = journal->jbd_fs;
1215         jbd_set32(&jbd_fs->sb, start, journal->start);
1216         jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
1217         jbd_fs->dirty = true;
1218 }
1219
1220 /**@brief  Start accessing the journal.
1221  * @param  jbd_fs jbd filesystem
1222  * @param  journal current journal session
1223  * @return standard error code*/
1224 int jbd_journal_start(struct jbd_fs *jbd_fs,
1225                       struct jbd_journal *journal)
1226 {
1227         int r;
1228         uint32_t features_incompatible =
1229                         ext4_get32(&jbd_fs->inode_ref.fs->sb,
1230                                    features_incompatible);
1231         struct ext4_block block = EXT4_BLOCK_ZERO();
1232         features_incompatible |= EXT4_FINCOM_RECOVER;
1233         ext4_set32(&jbd_fs->inode_ref.fs->sb,
1234                         features_incompatible,
1235                         features_incompatible);
1236         r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1237                         &jbd_fs->inode_ref.fs->sb);
1238         if (r != EOK)
1239                 return r;
1240
1241         journal->first = jbd_get32(&jbd_fs->sb, first);
1242         journal->start = journal->first;
1243         journal->last = journal->first;
1244         journal->trans_id = 1;
1245         journal->alloc_trans_id = 1;
1246
1247         journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
1248
1249         r = jbd_block_get_noread(jbd_fs,
1250                          &block,
1251                          journal->start);
1252         if (r != EOK) {
1253                 memset(journal, 0, sizeof(struct jbd_journal));
1254                 return r;
1255         }
1256         memset(block.data, 0, journal->block_size);
1257         ext4_bcache_set_dirty(block.buf);
1258         r = jbd_block_set(jbd_fs, &block);
1259         if (r != EOK) {
1260                 memset(journal, 0, sizeof(struct jbd_journal));
1261                 return r;
1262         }
1263
1264         TAILQ_INIT(&journal->trans_queue);
1265         TAILQ_INIT(&journal->cp_queue);
1266         RB_INIT(&journal->block_rec_root);
1267         journal->jbd_fs = jbd_fs;
1268         jbd_journal_write_sb(journal);
1269         return jbd_write_sb(jbd_fs);
1270 }
1271
1272 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1273                           struct ext4_buf *buf __unused,
1274                           int res,
1275                           void *arg);
1276
1277 static void jbd_journal_flush_trans(struct jbd_trans *trans)
1278 {
1279         struct jbd_buf *jbd_buf, *tmp;
1280         struct jbd_journal *journal = trans->journal;
1281         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1282         void *tmp_data = malloc(journal->block_size);
1283         ext4_assert(tmp_data);
1284
1285         TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1286                         tmp) {
1287                 struct ext4_buf *buf = jbd_buf->block_rec->buf;
1288                 /* The buffer in memory is still dirty. */
1289                 if (buf) {
1290                         if (jbd_buf->block_rec->trans != trans) {
1291                                 int r;
1292                                 struct ext4_block jbd_block = EXT4_BLOCK_ZERO();
1293                                 ext4_assert(ext4_block_get(fs->bdev,
1294                                                         &jbd_block,
1295                                                         jbd_buf->jbd_lba) == EOK);
1296                                 memcpy(tmp_data, jbd_block.data,
1297                                                 journal->block_size);
1298                                 ext4_block_set(fs->bdev, &jbd_block);
1299                                 r = ext4_blocks_set_direct(fs->bdev, tmp_data,
1300                                                 buf->lba, 1);
1301                                 jbd_trans_end_write(fs->bdev->bc, buf, r, jbd_buf);
1302                         } else
1303                                 ext4_block_flush_buf(fs->bdev, buf);
1304
1305                 }
1306         }
1307
1308         free(tmp_data);
1309 }
1310
1311 static void
1312 jbd_journal_skip_pure_revoke(struct jbd_journal *journal,
1313                              struct jbd_trans *trans)
1314 {
1315         journal->start = trans->start_iblock +
1316                 trans->alloc_blocks;
1317         wrap(&journal->jbd_fs->sb, journal->start);
1318         journal->trans_id = trans->trans_id + 1;
1319         jbd_journal_free_trans(journal,
1320                         trans, false);
1321         jbd_journal_write_sb(journal);
1322 }
1323
1324 static void
1325 jbd_journal_purge_cp_trans(struct jbd_journal *journal,
1326                            bool flush)
1327 {
1328         struct jbd_trans *trans;
1329         while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1330                 if (!trans->data_cnt) {
1331                         TAILQ_REMOVE(&journal->cp_queue,
1332                                         trans,
1333                                         trans_node);
1334                         jbd_journal_skip_pure_revoke(journal, trans);
1335                 } else {
1336                         if (trans->data_cnt ==
1337                                         trans->written_cnt) {
1338                                 journal->start =
1339                                         trans->start_iblock +
1340                                         trans->alloc_blocks;
1341                                 wrap(&journal->jbd_fs->sb,
1342                                                 journal->start);
1343                                 journal->trans_id =
1344                                         trans->trans_id + 1;
1345                                 TAILQ_REMOVE(&journal->cp_queue,
1346                                                 trans,
1347                                                 trans_node);
1348                                 jbd_journal_free_trans(journal,
1349                                                 trans,
1350                                                 false);
1351                                 jbd_journal_write_sb(journal);
1352                         } else if (!flush) {
1353                                 journal->start =
1354                                         trans->start_iblock;
1355                                 wrap(&journal->jbd_fs->sb,
1356                                                 journal->start);
1357                                 journal->trans_id =
1358                                         trans->trans_id;
1359                                 jbd_journal_write_sb(journal);
1360                                 break;
1361                         } else
1362                                 jbd_journal_flush_trans(trans);
1363                 }
1364         }
1365 }
1366
1367 /**@brief  Stop accessing the journal.
1368  * @param  journal current journal session
1369  * @return standard error code*/
1370 int jbd_journal_stop(struct jbd_journal *journal)
1371 {
1372         int r;
1373         struct jbd_fs *jbd_fs = journal->jbd_fs;
1374         uint32_t features_incompatible;
1375
1376         /* Make sure that journalled content have reached
1377          * the disk.*/
1378         jbd_journal_purge_cp_trans(journal, true);
1379
1380         /* There should be no block record in this journal
1381          * session. */
1382         if (!RB_EMPTY(&journal->block_rec_root))
1383                 ext4_dbg(DEBUG_JBD,
1384                          DBG_WARN "There are still block records "
1385                                   "in this journal session!\n");
1386
1387         features_incompatible =
1388                 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1389                            features_incompatible);
1390         features_incompatible &= ~EXT4_FINCOM_RECOVER;
1391         ext4_set32(&jbd_fs->inode_ref.fs->sb,
1392                         features_incompatible,
1393                         features_incompatible);
1394         r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1395                         &jbd_fs->inode_ref.fs->sb);
1396         if (r != EOK)
1397                 return r;
1398
1399         journal->start = 0;
1400         journal->trans_id = 0;
1401         jbd_journal_write_sb(journal);
1402         return jbd_write_sb(journal->jbd_fs);
1403 }
1404
1405 /**@brief  Allocate a block in the journal.
1406  * @param  journal current journal session
1407  * @param  trans transaction
1408  * @return allocated block address*/
1409 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1410                                         struct jbd_trans *trans)
1411 {
1412         uint32_t start_block;
1413
1414         start_block = journal->last++;
1415         trans->alloc_blocks++;
1416         wrap(&journal->jbd_fs->sb, journal->last);
1417         
1418         /* If there is no space left, flush all journalled
1419          * blocks to disk first.*/
1420         if (journal->last == journal->start)
1421                 jbd_journal_purge_cp_trans(journal, true);
1422
1423         return start_block;
1424 }
1425
1426 /**@brief  Allocate a new transaction
1427  * @param  journal current journal session
1428  * @return transaction allocated*/
1429 struct jbd_trans *
1430 jbd_journal_new_trans(struct jbd_journal *journal)
1431 {
1432         struct jbd_trans *trans = calloc(1, sizeof(struct jbd_trans));
1433         if (!trans)
1434                 return NULL;
1435
1436         /* We will assign a trans_id to this transaction,
1437          * once it has been committed.*/
1438         trans->journal = journal;
1439         trans->data_csum = EXT4_CRC32_INIT;
1440         trans->error = EOK;
1441         TAILQ_INIT(&trans->buf_queue);
1442         return trans;
1443 }
1444
1445 /**@brief  gain access to it before making any modications.
1446  * @param  journal current journal session
1447  * @param  trans transaction
1448  * @param  block descriptor
1449  * @return standard error code.*/
1450 int jbd_trans_get_access(struct jbd_journal *journal,
1451                          struct jbd_trans *trans,
1452                          struct ext4_block *block)
1453 {
1454         int r = EOK;
1455         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1456         struct jbd_buf *jbd_buf = block->buf->end_write_arg;
1457
1458         /* If the buffer has already been modified, we should
1459          * flush dirty data in this buffer to disk.*/
1460         if (ext4_bcache_test_flag(block->buf, BC_DIRTY) &&
1461             block->buf->end_write == jbd_trans_end_write) {
1462                 ext4_assert(jbd_buf);
1463                 if (jbd_buf->trans != trans)
1464                         r = ext4_block_flush_buf(fs->bdev, block->buf);
1465
1466         }
1467         return r;
1468 }
1469
1470 static struct jbd_block_rec *
1471 jbd_trans_block_rec_lookup(struct jbd_journal *journal,
1472                            ext4_fsblk_t lba)
1473 {
1474         struct jbd_block_rec tmp = {
1475                 .lba = lba
1476         };
1477
1478         return RB_FIND(jbd_block,
1479                        &journal->block_rec_root,
1480                        &tmp);
1481 }
1482
1483 static void
1484 jbd_trans_change_ownership(struct jbd_block_rec *block_rec,
1485                            struct jbd_trans *new_trans,
1486                            struct ext4_buf *new_buf)
1487 {
1488         LIST_REMOVE(block_rec, tbrec_node);
1489         /* Now this block record belongs to this transaction. */
1490         LIST_INSERT_HEAD(&new_trans->tbrec_list, block_rec, tbrec_node);
1491         block_rec->trans = new_trans;
1492         block_rec->buf = new_buf;
1493 }
1494
1495 static inline struct jbd_block_rec *
1496 jbd_trans_insert_block_rec(struct jbd_trans *trans,
1497                            ext4_fsblk_t lba,
1498                            struct ext4_buf *buf)
1499 {
1500         struct jbd_block_rec *block_rec;
1501         block_rec = jbd_trans_block_rec_lookup(trans->journal, lba);
1502         if (block_rec) {
1503                 jbd_trans_change_ownership(block_rec, trans, buf);
1504                 return block_rec;
1505         }
1506         block_rec = calloc(1, sizeof(struct jbd_block_rec));
1507         if (!block_rec)
1508                 return NULL;
1509
1510         block_rec->lba = lba;
1511         block_rec->buf = buf;
1512         block_rec->trans = trans;
1513         TAILQ_INIT(&block_rec->dirty_buf_queue);
1514         LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node);
1515         RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec);
1516         return block_rec;
1517 }
1518
1519 static void
1520 jbd_trans_finish_callback(struct jbd_journal *journal,
1521                           const struct jbd_trans *trans,
1522                           struct jbd_block_rec *block_rec,
1523                           bool abort)
1524 {
1525         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1526         if (block_rec->trans != trans)
1527                 return;
1528
1529         if (!abort) {
1530                 struct jbd_buf *jbd_buf, *tmp;
1531                 TAILQ_FOREACH_SAFE(jbd_buf,
1532                                 &block_rec->dirty_buf_queue,
1533                                 dirty_buf_node,
1534                                 tmp) {
1535                         /* All we need is a fake ext4_buf. */
1536                         struct ext4_buf buf;
1537
1538                         jbd_trans_end_write(fs->bdev->bc,
1539                                         &buf,
1540                                         EOK,
1541                                         jbd_buf);
1542                 }
1543         } else {
1544                 struct jbd_buf *jbd_buf;
1545                 struct ext4_block jbd_block = EXT4_BLOCK_ZERO(),
1546                                   block = EXT4_BLOCK_ZERO();
1547                 jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue,
1548                                 jbd_buf_dirty);
1549                 if (jbd_buf) {
1550                         ext4_assert(ext4_block_get(fs->bdev,
1551                                                 &jbd_block,
1552                                                 jbd_buf->jbd_lba) == EOK);
1553                         ext4_assert(ext4_block_get_noread(fs->bdev,
1554                                                 &block,
1555                                                 block_rec->lba) == EOK);
1556                         memcpy(block.data, jbd_block.data,
1557                                         journal->block_size);
1558
1559                         jbd_trans_change_ownership(block_rec,
1560                                         jbd_buf->trans, block.buf);
1561
1562                         block.buf->end_write = jbd_trans_end_write;
1563                         block.buf->end_write_arg = jbd_buf;
1564
1565                         ext4_bcache_set_flag(jbd_block.buf, BC_TMP);
1566                         ext4_bcache_set_dirty(block.buf);
1567
1568                         ext4_block_set(fs->bdev, &jbd_block);
1569                         ext4_block_set(fs->bdev, &block);
1570                         return;
1571                 }
1572         }
1573 }
1574
1575 static inline void
1576 jbd_trans_remove_block_rec(struct jbd_journal *journal,
1577                            struct jbd_block_rec *block_rec,
1578                            struct jbd_trans *trans)
1579 {
1580         /* If this block record doesn't belong to this transaction,
1581          * give up.*/
1582         if (block_rec->trans == trans) {
1583                 LIST_REMOVE(block_rec, tbrec_node);
1584                 RB_REMOVE(jbd_block,
1585                                 &journal->block_rec_root,
1586                                 block_rec);
1587                 free(block_rec);
1588         }
1589 }
1590
1591 /**@brief  Add block to a transaction and mark it dirty.
1592  * @param  trans transaction
1593  * @param  block block descriptor
1594  * @return standard error code*/
1595 int jbd_trans_set_block_dirty(struct jbd_trans *trans,
1596                               struct ext4_block *block)
1597 {
1598         struct jbd_buf *buf;
1599
1600         struct jbd_block_rec *block_rec;
1601         if (block->buf->end_write == jbd_trans_end_write) {
1602                 buf = block->buf->end_write_arg;
1603                 if (buf && buf->trans == trans)
1604                         return EOK;
1605         }
1606         buf = calloc(1, sizeof(struct jbd_buf));
1607         if (!buf)
1608                 return ENOMEM;
1609
1610         if ((block_rec = jbd_trans_insert_block_rec(trans,
1611                                         block->lb_id,
1612                                         block->buf)) == NULL) {
1613                 free(buf);
1614                 return ENOMEM;
1615         }
1616
1617         TAILQ_INSERT_TAIL(&block_rec->dirty_buf_queue,
1618                         buf,
1619                         dirty_buf_node);
1620
1621         buf->block_rec = block_rec;
1622         buf->trans = trans;
1623         buf->block = *block;
1624         ext4_bcache_inc_ref(block->buf);
1625
1626         /* If the content reach the disk, notify us
1627          * so that we may do a checkpoint. */
1628         block->buf->end_write = jbd_trans_end_write;
1629         block->buf->end_write_arg = buf;
1630
1631         trans->data_cnt++;
1632         TAILQ_INSERT_HEAD(&trans->buf_queue, buf, buf_node);
1633
1634         ext4_bcache_set_dirty(block->buf);
1635         return EOK;
1636 }
1637
1638 /**@brief  Add block to be revoked to a transaction
1639  * @param  trans transaction
1640  * @param  lba logical block address
1641  * @return standard error code*/
1642 int jbd_trans_revoke_block(struct jbd_trans *trans,
1643                            ext4_fsblk_t lba)
1644 {
1645         struct jbd_revoke_rec *rec =
1646                 calloc(1, sizeof(struct jbd_revoke_rec));
1647         if (!rec)
1648                 return ENOMEM;
1649
1650         rec->lba = lba;
1651         LIST_INSERT_HEAD(&trans->revoke_list, rec, revoke_node);
1652         return EOK;
1653 }
1654
1655 /**@brief  Try to add block to be revoked to a transaction.
1656  *         If @lba still remains in an transaction on checkpoint
1657  *         queue, add @lba as a revoked block to the transaction.
1658  * @param  trans transaction
1659  * @param  lba logical block address
1660  * @return standard error code*/
1661 int jbd_trans_try_revoke_block(struct jbd_trans *trans,
1662                                ext4_fsblk_t lba)
1663 {
1664         int r = EOK;
1665         struct jbd_journal *journal = trans->journal;
1666         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1667         struct jbd_block_rec *block_rec =
1668                 jbd_trans_block_rec_lookup(journal, lba);
1669
1670         /* Make sure we don't flush any buffers belong to this transaction. */
1671         if (block_rec && block_rec->trans != trans) {
1672                 /* If the buffer has not been flushed yet, flush it now. */
1673                 if (block_rec->buf) {
1674                         r = ext4_block_flush_buf(fs->bdev, block_rec->buf);
1675                         if (r != EOK)
1676                                 return r;
1677
1678                 }
1679
1680                 jbd_trans_revoke_block(trans, lba);
1681         }
1682
1683         return EOK;
1684 }
1685
1686 /**@brief  Free a transaction
1687  * @param  journal current journal session
1688  * @param  trans transaction
1689  * @param  abort discard all the modifications on the block?
1690  * @return standard error code*/
1691 void jbd_journal_free_trans(struct jbd_journal *journal,
1692                             struct jbd_trans *trans,
1693                             bool abort)
1694 {
1695         struct jbd_buf *jbd_buf, *tmp;
1696         struct jbd_revoke_rec *rec, *tmp2;
1697         struct jbd_block_rec *block_rec, *tmp3;
1698         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1699         TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1700                           tmp) {
1701                 block_rec = jbd_buf->block_rec;
1702                 if (abort) {
1703                         jbd_buf->block.buf->end_write = NULL;
1704                         jbd_buf->block.buf->end_write_arg = NULL;
1705                         ext4_bcache_clear_dirty(jbd_buf->block.buf);
1706                         ext4_block_set(fs->bdev, &jbd_buf->block);
1707                 }
1708
1709                 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1710                         jbd_buf,
1711                         dirty_buf_node);
1712                 jbd_trans_finish_callback(journal,
1713                                 trans,
1714                                 block_rec,
1715                                 abort);
1716                 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1717                 free(jbd_buf);
1718         }
1719         LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1720                           tmp2) {
1721                 LIST_REMOVE(rec, revoke_node);
1722                 free(rec);
1723         }
1724         LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node,
1725                           tmp3) {
1726                 jbd_trans_remove_block_rec(journal, block_rec, trans);
1727         }
1728
1729         free(trans);
1730 }
1731
1732 /**@brief  Write commit block for a transaction
1733  * @param  trans transaction
1734  * @return standard error code*/
1735 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1736 {
1737         int rc;
1738         struct jbd_commit_header *header;
1739         uint32_t commit_iblock = 0;
1740         struct ext4_block commit_block;
1741         struct jbd_journal *journal = trans->journal;
1742
1743         commit_iblock = jbd_journal_alloc_block(journal, trans);
1744         rc = jbd_block_get_noread(journal->jbd_fs,
1745                         &commit_block, commit_iblock);
1746         if (rc != EOK)
1747                 return rc;
1748
1749         header = (struct jbd_commit_header *)commit_block.data;
1750         jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1751         jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1752         jbd_set32(&header->header, sequence, trans->trans_id);
1753
1754         if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1755                                 JBD_FEATURE_COMPAT_CHECKSUM)) {
1756                 jbd_set32(header, chksum_type, JBD_CRC32_CHKSUM);
1757                 jbd_set32(header, chksum_size, JBD_CRC32_CHKSUM_SIZE);
1758                 jbd_set32(header, chksum[0], trans->data_csum);
1759         }
1760         jbd_commit_csum_set(journal->jbd_fs, header);
1761         ext4_bcache_set_dirty(commit_block.buf);
1762         rc = jbd_block_set(journal->jbd_fs, &commit_block);
1763         if (rc != EOK)
1764                 return rc;
1765
1766         return EOK;
1767 }
1768
1769 /**@brief  Write descriptor block for a transaction
1770  * @param  journal current journal session
1771  * @param  trans transaction
1772  * @return standard error code*/
1773 static int jbd_journal_prepare(struct jbd_journal *journal,
1774                                struct jbd_trans *trans)
1775 {
1776         int rc = EOK, i = 0;
1777         int32_t tag_tbl_size = 0;
1778         uint32_t desc_iblock = 0;
1779         uint32_t data_iblock = 0;
1780         char *tag_start = NULL, *tag_ptr = NULL;
1781         struct jbd_buf *jbd_buf, *tmp;
1782         struct ext4_block desc_block, data_block;
1783         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1784         uint32_t checksum = EXT4_CRC32_INIT;
1785
1786         /* Try to remove any non-dirty buffers from the tail of
1787          * buf_queue. */
1788         TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue,
1789                         jbd_trans_buf, buf_node, tmp) {
1790                 /* We stop the iteration when we find a dirty buffer. */
1791                 if (ext4_bcache_test_flag(jbd_buf->block.buf,
1792                                         BC_DIRTY))
1793                         break;
1794         
1795                 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1796                         jbd_buf,
1797                         dirty_buf_node);
1798
1799                 jbd_buf->block.buf->end_write = NULL;
1800                 jbd_buf->block.buf->end_write_arg = NULL;
1801                 jbd_trans_finish_callback(journal,
1802                                 trans,
1803                                 jbd_buf->block_rec,
1804                                 true);
1805
1806                 /* The buffer has not been modified, just release
1807                  * that jbd_buf. */
1808                 jbd_trans_remove_block_rec(journal,
1809                                 jbd_buf->block_rec, trans);
1810                 trans->data_cnt--;
1811
1812                 ext4_block_set(fs->bdev, &jbd_buf->block);
1813                 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1814                 free(jbd_buf);
1815         }
1816
1817         TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
1818                 struct tag_info tag_info;
1819                 bool uuid_exist = false;
1820                 if (!ext4_bcache_test_flag(jbd_buf->block.buf,
1821                                            BC_DIRTY)) {
1822                         TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1823                                         jbd_buf,
1824                                         dirty_buf_node);
1825
1826                         jbd_buf->block.buf->end_write = NULL;
1827                         jbd_buf->block.buf->end_write_arg = NULL;
1828                         jbd_trans_finish_callback(journal,
1829                                         trans,
1830                                         jbd_buf->block_rec,
1831                                         true);
1832
1833                         /* The buffer has not been modified, just release
1834                          * that jbd_buf. */
1835                         jbd_trans_remove_block_rec(journal,
1836                                         jbd_buf->block_rec, trans);
1837                         trans->data_cnt--;
1838
1839                         ext4_block_set(fs->bdev, &jbd_buf->block);
1840                         TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1841                         free(jbd_buf);
1842                         continue;
1843                 }
1844                 checksum = jbd_block_csum(journal->jbd_fs,
1845                                           jbd_buf->block.data,
1846                                           checksum,
1847                                           trans->trans_id);
1848 again:
1849                 if (!desc_iblock) {
1850                         struct jbd_bhdr *bhdr;
1851                         desc_iblock = jbd_journal_alloc_block(journal, trans);
1852                         rc = jbd_block_get_noread(journal->jbd_fs,
1853                                            &desc_block, desc_iblock);
1854                         if (rc != EOK)
1855                                 break;
1856
1857                         ext4_bcache_set_dirty(desc_block.buf);
1858
1859                         bhdr = (struct jbd_bhdr *)desc_block.data;
1860                         jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1861                         jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1862                         jbd_set32(bhdr, sequence, trans->trans_id);
1863
1864                         tag_start = (char *)(bhdr + 1);
1865                         tag_ptr = tag_start;
1866                         uuid_exist = true;
1867                         tag_tbl_size = journal->block_size -
1868                                 sizeof(struct jbd_bhdr);
1869
1870                         if (jbd_has_csum(&journal->jbd_fs->sb))
1871                                 tag_tbl_size -= sizeof(struct jbd_block_tail);
1872
1873                         if (!trans->start_iblock)
1874                                 trans->start_iblock = desc_iblock;
1875
1876                 }
1877                 tag_info.block = jbd_buf->block.lb_id;
1878                 tag_info.uuid_exist = uuid_exist;
1879                 if (i == trans->data_cnt - 1)
1880                         tag_info.last_tag = true;
1881                 else
1882                         tag_info.last_tag = false;
1883
1884                 tag_info.checksum = checksum;
1885
1886                 if (uuid_exist)
1887                         memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1888                                         UUID_SIZE);
1889
1890                 rc = jbd_write_block_tag(journal->jbd_fs,
1891                                 tag_ptr,
1892                                 tag_tbl_size,
1893                                 &tag_info);
1894                 if (rc != EOK) {
1895                         jbd_meta_csum_set(journal->jbd_fs,
1896                                         (struct jbd_bhdr *)desc_block.data);
1897                         jbd_block_set(journal->jbd_fs, &desc_block);
1898                         desc_iblock = 0;
1899                         goto again;
1900                 }
1901
1902                 data_iblock = jbd_journal_alloc_block(journal, trans);
1903                 rc = jbd_block_get_noread(journal->jbd_fs,
1904                                 &data_block, data_iblock);
1905                 if (rc != EOK)
1906                         break;
1907
1908                 ext4_bcache_set_dirty(data_block.buf);
1909
1910                 memcpy(data_block.data, jbd_buf->block.data,
1911                         journal->block_size);
1912                 jbd_buf->jbd_lba = data_block.lb_id;
1913
1914                 rc = jbd_block_set(journal->jbd_fs, &data_block);
1915                 if (rc != EOK)
1916                         break;
1917
1918                 tag_ptr += tag_info.tag_bytes;
1919                 tag_tbl_size -= tag_info.tag_bytes;
1920
1921                 i++;
1922         }
1923         if (rc == EOK && desc_iblock) {
1924                 jbd_meta_csum_set(journal->jbd_fs,
1925                                 (struct jbd_bhdr *)desc_block.data);
1926                 trans->data_csum = checksum;
1927                 jbd_block_set(journal->jbd_fs, &desc_block);
1928         }
1929
1930         return rc;
1931 }
1932
1933 /**@brief  Write revoke block for a transaction
1934  * @param  journal current journal session
1935  * @param  trans transaction
1936  * @return standard error code*/
1937 static int
1938 jbd_journal_prepare_revoke(struct jbd_journal *journal,
1939                            struct jbd_trans *trans)
1940 {
1941         int rc = EOK, i = 0;
1942         int32_t tag_tbl_size = 0;
1943         uint32_t desc_iblock = 0;
1944         char *blocks_entry = NULL;
1945         struct jbd_revoke_rec *rec, *tmp;
1946         struct ext4_block desc_block;
1947         struct jbd_revoke_header *header = NULL;
1948         int32_t record_len = 4;
1949
1950         if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1951                                      JBD_FEATURE_INCOMPAT_64BIT))
1952                 record_len = 8;
1953
1954         LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1955                           tmp) {
1956 again:
1957                 if (!desc_iblock) {
1958                         struct jbd_bhdr *bhdr;
1959                         desc_iblock = jbd_journal_alloc_block(journal, trans);
1960                         rc = jbd_block_get_noread(journal->jbd_fs,
1961                                            &desc_block, desc_iblock);
1962                         if (rc != EOK) {
1963                                 break;
1964                         }
1965
1966                         ext4_bcache_set_dirty(desc_block.buf);
1967
1968                         bhdr = (struct jbd_bhdr *)desc_block.data;
1969                         jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1970                         jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
1971                         jbd_set32(bhdr, sequence, trans->trans_id);
1972                         
1973                         header = (struct jbd_revoke_header *)bhdr;
1974                         blocks_entry = (char *)(header + 1);
1975                         tag_tbl_size = journal->block_size -
1976                                 sizeof(struct jbd_revoke_header);
1977
1978                         if (jbd_has_csum(&journal->jbd_fs->sb))
1979                                 tag_tbl_size -= sizeof(struct jbd_block_tail);
1980
1981                         if (!trans->start_iblock)
1982                                 trans->start_iblock = desc_iblock;
1983
1984                 }
1985
1986                 if (tag_tbl_size < record_len) {
1987                         jbd_set32(header, count,
1988                                   journal->block_size - tag_tbl_size);
1989                         jbd_meta_csum_set(journal->jbd_fs,
1990                                         (struct jbd_bhdr *)desc_block.data);
1991                         jbd_block_set(journal->jbd_fs, &desc_block);
1992                         desc_iblock = 0;
1993                         header = NULL;
1994                         goto again;
1995                 }
1996                 if (record_len == 8) {
1997                         uint64_t *blocks =
1998                                 (uint64_t *)blocks_entry;
1999                         *blocks = to_be64(rec->lba);
2000                 } else {
2001                         uint32_t *blocks =
2002                                 (uint32_t *)blocks_entry;
2003                         *blocks = to_be32((uint32_t)rec->lba);
2004                 }
2005                 blocks_entry += record_len;
2006                 tag_tbl_size -= record_len;
2007
2008                 i++;
2009         }
2010         if (rc == EOK && desc_iblock) {
2011                 if (header != NULL)
2012                         jbd_set32(header, count,
2013                                   journal->block_size - tag_tbl_size);
2014
2015                 jbd_meta_csum_set(journal->jbd_fs,
2016                                 (struct jbd_bhdr *)desc_block.data);
2017                 jbd_block_set(journal->jbd_fs, &desc_block);
2018         }
2019
2020         return rc;
2021 }
2022
2023 /**@brief  Put references of block descriptors in a transaction.
2024  * @param  journal current journal session
2025  * @param  trans transaction*/
2026 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
2027 {
2028         struct jbd_buf *jbd_buf, *tmp;
2029         struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
2030         TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
2031                         tmp) {
2032                 struct ext4_block block = jbd_buf->block;
2033                 ext4_block_set(fs->bdev, &block);
2034         }
2035 }
2036
2037 /**@brief  Update the start block of the journal when
2038  *         all the contents in a transaction reach the disk.*/
2039 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
2040                           struct ext4_buf *buf,
2041                           int res,
2042                           void *arg)
2043 {
2044         struct jbd_buf *jbd_buf = arg;
2045         struct jbd_trans *trans = jbd_buf->trans;
2046         struct jbd_block_rec *block_rec = jbd_buf->block_rec;
2047         struct jbd_journal *journal = trans->journal;
2048         bool first_in_queue =
2049                 trans == TAILQ_FIRST(&journal->cp_queue);
2050         if (res != EOK)
2051                 trans->error = res;
2052
2053         TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
2054         TAILQ_REMOVE(&block_rec->dirty_buf_queue,
2055                         jbd_buf,
2056                         dirty_buf_node);
2057
2058         jbd_trans_finish_callback(journal,
2059                         trans,
2060                         jbd_buf->block_rec,
2061                         false);
2062         if (block_rec->trans == trans) {
2063                 block_rec->buf = NULL;
2064                 /* Clear the end_write and end_write_arg fields. */
2065                 buf->end_write = NULL;
2066                 buf->end_write_arg = NULL;
2067         }
2068
2069         free(jbd_buf);
2070
2071         trans->written_cnt++;
2072         if (trans->written_cnt == trans->data_cnt) {
2073                 /* If it is the first transaction on checkpoint queue,
2074                  * we will shift the start of the journal to the next
2075                  * transaction, and remove subsequent written
2076                  * transactions from checkpoint queue until we find
2077                  * an unwritten one. */
2078                 if (first_in_queue) {
2079                         journal->start = trans->start_iblock +
2080                                 trans->alloc_blocks;
2081                         wrap(&journal->jbd_fs->sb, journal->start);
2082                         journal->trans_id = trans->trans_id + 1;
2083                         TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
2084                         jbd_journal_free_trans(journal, trans, false);
2085
2086                         jbd_journal_purge_cp_trans(journal, false);
2087                         jbd_journal_write_sb(journal);
2088                         jbd_write_sb(journal->jbd_fs);
2089                 }
2090         }
2091 }
2092
2093 /**@brief  Commit a transaction to the journal immediately.
2094  * @param  journal current journal session
2095  * @param  trans transaction
2096  * @return standard error code*/
2097 int jbd_journal_commit_trans(struct jbd_journal *journal,
2098                              struct jbd_trans *trans)
2099 {
2100         int rc = EOK;
2101         uint32_t last = journal->last;
2102
2103         trans->trans_id = journal->alloc_trans_id;
2104         rc = jbd_journal_prepare(journal, trans);
2105         if (rc != EOK)
2106                 goto Finish;
2107
2108         rc = jbd_journal_prepare_revoke(journal, trans);
2109         if (rc != EOK)
2110                 goto Finish;
2111
2112         if (TAILQ_EMPTY(&trans->buf_queue) &&
2113             LIST_EMPTY(&trans->revoke_list)) {
2114                 /* Since there are no entries in both buffer list
2115                  * and revoke entry list, we do not consider trans as
2116                  * complete transaction and just return EOK.*/
2117                 jbd_journal_free_trans(journal, trans, false);
2118                 goto Finish;
2119         }
2120
2121         rc = jbd_trans_write_commit_block(trans);
2122         if (rc != EOK)
2123                 goto Finish;
2124
2125         journal->alloc_trans_id++;
2126         if (TAILQ_EMPTY(&journal->cp_queue)) {
2127                 if (trans->data_cnt) {
2128                         journal->start = trans->start_iblock;
2129                         wrap(&journal->jbd_fs->sb, journal->start);
2130                         journal->trans_id = trans->trans_id;
2131                         jbd_journal_write_sb(journal);
2132                         jbd_write_sb(journal->jbd_fs);
2133                         TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2134                                         trans_node);
2135                         jbd_journal_cp_trans(journal, trans);
2136                 } else {
2137                         journal->start = trans->start_iblock +
2138                                 trans->alloc_blocks;
2139                         wrap(&journal->jbd_fs->sb, journal->start);
2140                         journal->trans_id = trans->trans_id + 1;
2141                         jbd_journal_write_sb(journal);
2142                         jbd_journal_free_trans(journal, trans, false);
2143                 }
2144         } else {
2145                 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2146                                 trans_node);
2147                 if (trans->data_cnt)
2148                         jbd_journal_cp_trans(journal, trans);
2149
2150         }
2151 Finish:
2152         if (rc != EOK) {
2153                 journal->last = last;
2154                 jbd_journal_free_trans(journal, trans, true);
2155         }
2156         return rc;
2157 }
2158
2159 /**
2160  * @}
2161  */