add patch create-function-to-read-journal-inode
[ext4-patch-queue.git] / move-more-common-code-into-journal_init_common
blob85a0c53e951c3386c034189d03e9ee1461c1d1d1
1 jbd2: move more common code into journal_init_common()
3 From: Geliang Tang <geliangtang@gmail.com>
5 There are some repetitive code in jbd2_journal_init_dev() and
6 jbd2_journal_init_inode(). So this patch moves the common code into
7 journal_init_common() helper to simplify the code. And fix the coding
8 style warnings reported by checkpatch.pl by the way.
10 Signed-off-by: Geliang Tang <geliangtang@gmail.com>
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
12 Reviewed-by: Jan Kara <jack@suse.cz>
13 ---
14  fs/jbd2/journal.c | 131 +++++++++++++++++++++++++++++++++-----------------------------------------------------
15  1 file changed, 50 insertions(+), 81 deletions(-)
17 diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
18 index 46261a6..927da49 100644
19 --- a/fs/jbd2/journal.c
20 +++ b/fs/jbd2/journal.c
21 @@ -1090,11 +1090,15 @@ static void jbd2_stats_proc_exit(journal_t *journal)
22   * very few fields yet: that has to wait until we have created the
23   * journal structures from from scratch, or loaded them from disk. */
25 -static journal_t * journal_init_common (void)
26 +static journal_t *journal_init_common(struct block_device *bdev,
27 +                       struct block_device *fs_dev,
28 +                       unsigned long long start, int len, int blocksize)
29  {
30         static struct lock_class_key jbd2_trans_commit_key;
31         journal_t *journal;
32         int err;
33 +       struct buffer_head *bh;
34 +       int n;
36         journal = kzalloc(sizeof(*journal), GFP_KERNEL);
37         if (!journal)
38 @@ -1131,6 +1135,32 @@ static journal_t * journal_init_common (void)
39         lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle",
40                          &jbd2_trans_commit_key, 0);
42 +       /* journal descriptor can store up to n blocks -bzzz */
43 +       journal->j_blocksize = blocksize;
44 +       journal->j_dev = bdev;
45 +       journal->j_fs_dev = fs_dev;
46 +       journal->j_blk_offset = start;
47 +       journal->j_maxlen = len;
48 +       n = journal->j_blocksize / sizeof(journal_block_tag_t);
49 +       journal->j_wbufsize = n;
50 +       journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
51 +                                       GFP_KERNEL);
52 +       if (!journal->j_wbuf) {
53 +               kfree(journal);
54 +               return NULL;
55 +       }
57 +       bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
58 +       if (!bh) {
59 +               pr_err("%s: Cannot get buffer for journal superblock\n",
60 +                       __func__);
61 +               kfree(journal->j_wbuf);
62 +               kfree(journal);
63 +               return NULL;
64 +       }
65 +       journal->j_sb_buffer = bh;
66 +       journal->j_superblock = (journal_superblock_t *)bh->b_data;
68         return journal;
69  }
71 @@ -1157,51 +1187,21 @@ static journal_t * journal_init_common (void)
72   *  range of blocks on an arbitrary block device.
73   *
74   */
75 -journal_t * jbd2_journal_init_dev(struct block_device *bdev,
76 +journal_t *jbd2_journal_init_dev(struct block_device *bdev,
77                         struct block_device *fs_dev,
78                         unsigned long long start, int len, int blocksize)
79  {
80 -       journal_t *journal = journal_init_common();
81 -       struct buffer_head *bh;
82 -       int n;
83 +       journal_t *journal;
85 +       journal = journal_init_common(bdev, fs_dev, start, len, blocksize);
86         if (!journal)
87                 return NULL;
89 -       /* journal descriptor can store up to n blocks -bzzz */
90 -       journal->j_blocksize = blocksize;
91 -       journal->j_dev = bdev;
92 -       journal->j_fs_dev = fs_dev;
93 -       journal->j_blk_offset = start;
94 -       journal->j_maxlen = len;
95         bdevname(journal->j_dev, journal->j_devname);
96         strreplace(journal->j_devname, '/', '!');
97         jbd2_stats_proc_init(journal);
98 -       n = journal->j_blocksize / sizeof(journal_block_tag_t);
99 -       journal->j_wbufsize = n;
100 -       journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
101 -       if (!journal->j_wbuf) {
102 -               printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
103 -                       __func__);
104 -               goto out_err;
105 -       }
107 -       bh = __getblk(journal->j_dev, start, journal->j_blocksize);
108 -       if (!bh) {
109 -               printk(KERN_ERR
110 -                      "%s: Cannot get buffer for journal superblock\n",
111 -                      __func__);
112 -               goto out_err;
113 -       }
114 -       journal->j_sb_buffer = bh;
115 -       journal->j_superblock = (journal_superblock_t *)bh->b_data;
117         return journal;
118 -out_err:
119 -       kfree(journal->j_wbuf);
120 -       jbd2_stats_proc_exit(journal);
121 -       kfree(journal);
122 -       return NULL;
125  /**
126 @@ -1212,67 +1212,36 @@ out_err:
127   * the journal.  The inode must exist already, must support bmap() and
128   * must have all data blocks preallocated.
129   */
130 -journal_t * jbd2_journal_init_inode (struct inode *inode)
131 +journal_t *jbd2_journal_init_inode(struct inode *inode)
133 -       struct buffer_head *bh;
134 -       journal_t *journal = journal_init_common();
135 +       journal_t *journal;
136         char *p;
137 -       int err;
138 -       int n;
139         unsigned long long blocknr;
141 +       blocknr = bmap(inode, 0);
142 +       if (!blocknr) {
143 +               pr_err("%s: Cannot locate journal superblock\n",
144 +                       __func__);
145 +               return NULL;
146 +       }
148 +       jbd_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n",
149 +                 inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size,
150 +                 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
152 +       journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev,
153 +                       blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits,
154 +                       inode->i_sb->s_blocksize);
155         if (!journal)
156                 return NULL;
158 -       journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
159         journal->j_inode = inode;
160         bdevname(journal->j_dev, journal->j_devname);
161         p = strreplace(journal->j_devname, '/', '!');
162         sprintf(p, "-%lu", journal->j_inode->i_ino);
163 -       jbd_debug(1,
164 -                 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
165 -                 journal, inode->i_sb->s_id, inode->i_ino,
166 -                 (long long) inode->i_size,
167 -                 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
169 -       journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
170 -       journal->j_blocksize = inode->i_sb->s_blocksize;
171         jbd2_stats_proc_init(journal);
173 -       /* journal descriptor can store up to n blocks -bzzz */
174 -       n = journal->j_blocksize / sizeof(journal_block_tag_t);
175 -       journal->j_wbufsize = n;
176 -       journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
177 -       if (!journal->j_wbuf) {
178 -               printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
179 -                       __func__);
180 -               goto out_err;
181 -       }
183 -       err = jbd2_journal_bmap(journal, 0, &blocknr);
184 -       /* If that failed, give up */
185 -       if (err) {
186 -               printk(KERN_ERR "%s: Cannot locate journal superblock\n",
187 -                      __func__);
188 -               goto out_err;
189 -       }
191 -       bh = getblk_unmovable(journal->j_dev, blocknr, journal->j_blocksize);
192 -       if (!bh) {
193 -               printk(KERN_ERR
194 -                      "%s: Cannot get buffer for journal superblock\n",
195 -                      __func__);
196 -               goto out_err;
197 -       }
198 -       journal->j_sb_buffer = bh;
199 -       journal->j_superblock = (journal_superblock_t *)bh->b_data;
201         return journal;
202 -out_err:
203 -       kfree(journal->j_wbuf);
204 -       jbd2_stats_proc_exit(journal);
205 -       kfree(journal);
206 -       return NULL;
209  /*