ext4_mkfs: fix mkfs function when journaling is enabled
[lwext4.git] / lwext4 / ext4_trans.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.h
35  * @brief Ext4 transaction buffer operations.
36  */
37
38 #include "ext4_config.h"
39 #include "ext4_types.h"
40 #include "ext4_journal.h"
41
42 static int ext4_trans_get_write_access(struct ext4_fs *fs,
43                                 struct ext4_block *block)
44 {
45         int r = EOK;
46         if (fs->jbd_journal && fs->curr_trans) {
47                 struct jbd_journal *journal = fs->jbd_journal;
48                 struct jbd_trans *trans = fs->curr_trans;
49                 r = jbd_trans_get_access(journal, trans, block);
50         }
51         return r;
52 }
53
54 int ext4_trans_set_block_dirty(struct ext4_buf *buf)
55 {
56         int r = EOK;
57         struct ext4_fs *fs = buf->bc->bdev->fs;
58         struct ext4_block block = {
59                 .lb_id = buf->lba,
60                 .data = buf->data,
61                 .buf = buf
62         };
63
64         if (fs->jbd_journal && fs->curr_trans) {
65                 struct jbd_trans *trans = fs->curr_trans;
66                 r = jbd_trans_set_block_dirty(trans, &block);
67         } else
68                 ext4_bcache_set_dirty(buf);
69
70         return r;
71 }
72
73 int ext4_trans_block_get_noread(struct ext4_blockdev *bdev,
74                           struct ext4_block *b,
75                           uint64_t lba)
76 {
77         int r = ext4_block_get_noread(bdev, b, lba);
78         if (r != EOK)
79                 return r;
80
81         r = ext4_trans_get_write_access(bdev->fs, b);
82         if (r != EOK)
83                 ext4_block_set(bdev, b);
84
85         return r;
86 }
87
88 int ext4_trans_block_get(struct ext4_blockdev *bdev,
89                    struct ext4_block *b,
90                    uint64_t lba)
91 {
92         int r = ext4_block_get(bdev, b, lba);
93         if (r != EOK)
94                 return r;
95
96         r = ext4_trans_get_write_access(bdev->fs, b);
97         if (r != EOK)
98                 ext4_block_set(bdev, b);
99
100         return r;
101 }
102
103 int ext4_trans_try_revoke_block(struct ext4_blockdev *bdev,
104                                uint64_t lba)
105 {
106         int r = EOK;
107         struct ext4_fs *fs = bdev->fs;
108         if (fs->jbd_journal && fs->curr_trans) {
109                 struct jbd_trans *trans = fs->curr_trans;
110                 r = jbd_trans_try_revoke_block(trans, lba);
111         }
112         return r;
113 }
114
115 /**
116  * @}
117  */