blob: 5ef0712e2f7a698c9874a1ba86a8a28c1b0409f9 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/journal.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070028#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029#include <linux/errno.h>
30#include <linux/slab.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070034#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050038#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050039#include <linux/math64.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040040#include <linux/hash.h>
Theodore Ts'od2eecb02009-12-07 10:36:20 -050041#include <linux/log2.h>
42#include <linux/vmalloc.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040043#include <linux/backing-dev.h>
Brian King39e3ac22010-10-27 21:25:12 -040044#include <linux/bitops.h>
Theodore Ts'o670be5a2010-12-17 10:44:16 -050045#include <linux/ratelimit.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040046
47#define CREATE_TRACE_POINTS
48#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070049
50#include <asm/uaccess.h>
51#include <asm/page.h>
52
Theodore Ts'ob6e96d02013-02-09 16:29:20 -050053#ifdef CONFIG_JBD2_DEBUG
54ushort jbd2_journal_enable_debug __read_mostly;
55EXPORT_SYMBOL(jbd2_journal_enable_debug);
56
57module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
58MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
59#endif
60
Mingming Caof7f4bcc2006-10-11 01:20:59 -070061EXPORT_SYMBOL(jbd2_journal_extend);
62EXPORT_SYMBOL(jbd2_journal_stop);
63EXPORT_SYMBOL(jbd2_journal_lock_updates);
64EXPORT_SYMBOL(jbd2_journal_unlock_updates);
65EXPORT_SYMBOL(jbd2_journal_get_write_access);
66EXPORT_SYMBOL(jbd2_journal_get_create_access);
67EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070068EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070069EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070070EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070071#if 0
72EXPORT_SYMBOL(journal_sync_buffer);
73#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070074EXPORT_SYMBOL(jbd2_journal_flush);
75EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070076
Mingming Caof7f4bcc2006-10-11 01:20:59 -070077EXPORT_SYMBOL(jbd2_journal_init_dev);
78EXPORT_SYMBOL(jbd2_journal_init_inode);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070079EXPORT_SYMBOL(jbd2_journal_check_used_features);
80EXPORT_SYMBOL(jbd2_journal_check_available_features);
81EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070082EXPORT_SYMBOL(jbd2_journal_load);
83EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070084EXPORT_SYMBOL(jbd2_journal_abort);
85EXPORT_SYMBOL(jbd2_journal_errno);
86EXPORT_SYMBOL(jbd2_journal_ack_err);
87EXPORT_SYMBOL(jbd2_journal_clear_err);
88EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050089EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070090EXPORT_SYMBOL(jbd2_journal_start_commit);
91EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
92EXPORT_SYMBOL(jbd2_journal_wipe);
93EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
94EXPORT_SYMBOL(jbd2_journal_invalidatepage);
95EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
96EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040097EXPORT_SYMBOL(jbd2_journal_file_inode);
98EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500101EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700102
Dave Kleikamp470decc2006-10-11 01:20:57 -0700103static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -0500104static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700105
Darrick J. Wong25ed6e82012-05-27 07:48:56 -0400106/* Checksumming functions */
107int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
108{
109 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
110 return 1;
111
112 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
113}
114
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -0400115static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
116{
117 __u32 csum, old_csum;
118
119 old_csum = sb->s_checksum;
120 sb->s_checksum = 0;
121 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
122 sb->s_checksum = old_csum;
123
124 return cpu_to_be32(csum);
125}
126
127int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
128{
129 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
130 return 1;
131
132 return sb->s_checksum == jbd2_superblock_csum(j, sb);
133}
134
135void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
136{
137 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
138 return;
139
140 sb->s_checksum = jbd2_superblock_csum(j, sb);
141}
142
Dave Kleikamp470decc2006-10-11 01:20:57 -0700143/*
144 * Helper function used to manage commit timeouts
145 */
146
147static void commit_timeout(unsigned long __data)
148{
149 struct task_struct * p = (struct task_struct *) __data;
150
151 wake_up_process(p);
152}
153
154/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700155 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156 * journal.
157 *
158 * This kernel thread is responsible for two things:
159 *
160 * 1) COMMIT: Every so often we need to commit the current state of the
161 * filesystem to disk. The journal thread is responsible for writing
162 * all of the metadata buffers to disk.
163 *
164 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
165 * of the data in that part of the log has been rewritten elsewhere on
166 * the disk. Flushing these old buffers to reclaim space in the log is
167 * known as checkpointing, and this thread is responsible for that job.
168 */
169
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700170static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700171{
172 journal_t *journal = arg;
173 transaction_t *transaction;
174
175 /*
176 * Set up an interval timer which can be used to trigger a commit wakeup
177 * after the commit interval expires
178 */
179 setup_timer(&journal->j_commit_timer, commit_timeout,
180 (unsigned long)current);
181
Nigel Cunningham35c80422012-02-03 19:59:41 +1100182 set_freezable();
183
Dave Kleikamp470decc2006-10-11 01:20:57 -0700184 /* Record that the journal thread is running */
185 journal->j_task = current;
186 wake_up(&journal->j_wait_done_commit);
187
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 /*
189 * And now, wait forever for commit wakeup events.
190 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400191 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700192
193loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700194 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700195 goto end_loop;
196
197 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
198 journal->j_commit_sequence, journal->j_commit_request);
199
200 if (journal->j_commit_sequence != journal->j_commit_request) {
201 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400202 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700203 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700204 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400205 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700206 goto loop;
207 }
208
209 wake_up(&journal->j_wait_done_commit);
210 if (freezing(current)) {
211 /*
212 * The simpler the better. Flushing journal isn't a
213 * good idea, because that depends on threads that may
214 * be already stopped.
215 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700216 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400217 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800218 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400219 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700220 } else {
221 /*
222 * We assume on resume that commits are already there,
223 * so we don't sleep
224 */
225 DEFINE_WAIT(wait);
226 int should_sleep = 1;
227
228 prepare_to_wait(&journal->j_wait_commit, &wait,
229 TASK_INTERRUPTIBLE);
230 if (journal->j_commit_sequence != journal->j_commit_request)
231 should_sleep = 0;
232 transaction = journal->j_running_transaction;
233 if (transaction && time_after_eq(jiffies,
234 transaction->t_expires))
235 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700236 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700237 should_sleep = 0;
238 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400239 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700240 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400241 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700242 }
243 finish_wait(&journal->j_wait_commit, &wait);
244 }
245
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700246 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700247
248 /*
249 * Were we woken up by a commit wakeup event?
250 */
251 transaction = journal->j_running_transaction;
252 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
253 journal->j_commit_request = transaction->t_tid;
254 jbd_debug(1, "woke because of timeout\n");
255 }
256 goto loop;
257
258end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400259 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700260 del_timer_sync(&journal->j_commit_timer);
261 journal->j_task = NULL;
262 wake_up(&journal->j_wait_done_commit);
263 jbd_debug(1, "Journal thread exiting.\n");
264 return 0;
265}
266
Pavel Emelianov97f06782007-05-08 00:30:42 -0700267static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700268{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700269 struct task_struct *t;
270
Theodore Ts'o90576c02009-09-29 15:51:30 -0400271 t = kthread_run(kjournald2, journal, "jbd2/%s",
272 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700273 if (IS_ERR(t))
274 return PTR_ERR(t);
275
Al Viro1076d172008-03-29 03:07:18 +0000276 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700277 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700278}
279
280static void journal_kill_thread(journal_t *journal)
281{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400282 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700283 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284
285 while (journal->j_task) {
286 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400287 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000288 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400289 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700290 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400291 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292}
293
294/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700295 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700296 *
297 * Writes a metadata buffer to a given disk block. The actual IO is not
298 * performed but a new buffer_head is constructed which labels the data
299 * to be written with the correct destination disk block.
300 *
301 * Any magic-number escaping which needs to be done will cause a
302 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700303 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700304 * magic number is only written to the log for descripter blocks. In
305 * this case, we copy the data and replace the first word with 0, and we
306 * return a result code which indicates that this buffer needs to be
307 * marked as an escaped buffer in the corresponding log descriptor
308 * block. The missing word can then be restored when the block is read
309 * during recovery.
310 *
311 * If the source buffer has already been modified by a new transaction
312 * since we took the last commit snapshot, we use the frozen copy of
Jan Karaf5113ef2013-06-04 12:01:45 -0400313 * that data for IO. If we end up using the existing buffer_head's data
314 * for the write, then we have to make sure nobody modifies it while the
315 * IO is in progress. do_get_write_access() handles this.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700316 *
Jan Karaf5113ef2013-06-04 12:01:45 -0400317 * The function returns a pointer to the buffer_head to be used for IO.
318 *
Dave Kleikamp470decc2006-10-11 01:20:57 -0700319 *
320 * Return value:
321 * <0: Error
322 * >=0: Finished OK
323 *
324 * On success:
325 * Bit 0 set == escape performed on the data
326 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
327 */
328
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700329int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700330 struct journal_head *jh_in,
Jan Karaf5113ef2013-06-04 12:01:45 -0400331 struct buffer_head **bh_out,
332 sector_t blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700333{
334 int need_copy_out = 0;
335 int done_copy_out = 0;
336 int do_escape = 0;
337 char *mapped_data;
338 struct buffer_head *new_bh;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339 struct page *new_page;
340 unsigned int new_offset;
341 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400342 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343
344 /*
345 * The buffer really shouldn't be locked: only the current committing
346 * transaction is allowed to write it, so nobody else is allowed
347 * to do any IO.
348 *
349 * akpm: except if we're journalling data, and write() output is
350 * also part of a shared mapping, and another thread has
351 * decided to launch a writepage() against this buffer.
352 */
353 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
354
Theodore Ts'o47def822010-07-27 11:56:05 -0400355retry_alloc:
356 new_bh = alloc_buffer_head(GFP_NOFS);
357 if (!new_bh) {
358 /*
359 * Failure is not an option, but __GFP_NOFAIL is going
360 * away; so we retry ourselves here.
361 */
362 congestion_wait(BLK_RW_ASYNC, HZ/50);
363 goto retry_alloc;
364 }
365
dingdinghua96577c42009-07-13 17:55:35 -0400366 /* keep subsequent assertions sane */
dingdinghua96577c42009-07-13 17:55:35 -0400367 atomic_set(&new_bh->b_count, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700368
Jan Karaf5113ef2013-06-04 12:01:45 -0400369 jbd_lock_bh_state(bh_in);
370repeat:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700371 /*
372 * If a new transaction has already done a buffer copy-out, then
373 * we use that version of the data for the commit.
374 */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375 if (jh_in->b_frozen_data) {
376 done_copy_out = 1;
377 new_page = virt_to_page(jh_in->b_frozen_data);
378 new_offset = offset_in_page(jh_in->b_frozen_data);
379 } else {
380 new_page = jh2bh(jh_in)->b_page;
381 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
382 }
383
Cong Wang303a8f22011-11-25 23:14:31 +0800384 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700385 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200386 * Fire data frozen trigger if data already wasn't frozen. Do this
387 * before checking for escaping, as the trigger may modify the magic
388 * offset. If a copy-out happens afterwards, it will have the correct
389 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700390 */
Jan Kara13ceef02010-07-14 07:56:33 +0200391 if (!done_copy_out)
392 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
393 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700394
395 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396 * Check for escaping
397 */
398 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700399 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700400 need_copy_out = 1;
401 do_escape = 1;
402 }
Cong Wang303a8f22011-11-25 23:14:31 +0800403 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404
405 /*
406 * Do we need to do a data copy?
407 */
408 if (need_copy_out && !done_copy_out) {
409 char *tmp;
410
411 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400412 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500413 if (!tmp) {
Jan Karaf5113ef2013-06-04 12:01:45 -0400414 brelse(new_bh);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500415 return -ENOMEM;
416 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417 jbd_lock_bh_state(bh_in);
418 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400419 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 goto repeat;
421 }
422
423 jh_in->b_frozen_data = tmp;
Cong Wang303a8f22011-11-25 23:14:31 +0800424 mapped_data = kmap_atomic(new_page);
Jan Karaf5113ef2013-06-04 12:01:45 -0400425 memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800426 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700427
428 new_page = virt_to_page(tmp);
429 new_offset = offset_in_page(tmp);
430 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700431
432 /*
433 * This isn't strictly necessary, as we're using frozen
434 * data for the escaping, but it keeps consistency with
435 * b_frozen_data usage.
436 */
437 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438 }
439
440 /*
441 * Did we need to do an escaping? Now we've done all the
442 * copying, we can finally do so.
443 */
444 if (do_escape) {
Cong Wang303a8f22011-11-25 23:14:31 +0800445 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang303a8f22011-11-25 23:14:31 +0800447 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700448 }
449
Dave Kleikamp470decc2006-10-11 01:20:57 -0700450 set_bh_page(new_bh, new_page, new_offset);
Jan Karaf5113ef2013-06-04 12:01:45 -0400451 new_bh->b_size = bh_in->b_size;
452 new_bh->b_bdev = journal->j_dev;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700453 new_bh->b_blocknr = blocknr;
Jan Karab34090e2013-06-04 12:08:56 -0400454 new_bh->b_private = bh_in;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455 set_buffer_mapped(new_bh);
456 set_buffer_dirty(new_bh);
457
Jan Karaf5113ef2013-06-04 12:01:45 -0400458 *bh_out = new_bh;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459
460 /*
461 * The to-be-written buffer needs to get moved to the io queue,
462 * and the original buffer whose contents we are shadowing or
463 * copying is moved to the transaction's shadow queue.
464 */
465 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400466 spin_lock(&journal->j_list_lock);
467 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
468 spin_unlock(&journal->j_list_lock);
Jan Karab34090e2013-06-04 12:08:56 -0400469 set_buffer_shadow(bh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400470 jbd_unlock_bh_state(bh_in);
471
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472 return do_escape | (done_copy_out << 1);
473}
474
475/*
476 * Allocation code for the journal file. Manage the space left in the
477 * journal, so that we can begin checkpointing when appropriate.
478 */
479
480/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700481 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700482 *
483 * Called with the journal already locked.
484 *
485 * Called under j_state_lock
486 */
487
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700488int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700489{
490 int left = journal->j_free;
491
Theodore Ts'oa931da62010-08-03 21:35:12 -0400492 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700493
494 /*
495 * Be pessimistic here about the number of those free blocks which
496 * might be required for log descriptor control blocks.
497 */
498
499#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
500
501 left -= MIN_LOG_RESERVED_BLOCKS;
502
503 if (left <= 0)
504 return 0;
505 left -= (left >> 3);
506 return left;
507}
508
509/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500510 * Called with j_state_lock locked for writing.
511 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700513int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514{
Eric Sandeene7b04ac2013-01-30 00:39:28 -0500515 /* Return if the txn has already requested to be committed */
516 if (journal->j_commit_request == target)
517 return 0;
518
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400520 * The only transaction we can possibly wait upon is the
521 * currently running transaction (if it exists). Otherwise,
522 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400524 if (journal->j_running_transaction &&
525 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200527 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700528 * commit thread. We do _not_ do the commit ourselves.
529 */
530
531 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400532 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700533 journal->j_commit_request,
534 journal->j_commit_sequence);
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500535 journal->j_running_transaction->t_requested = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 wake_up(&journal->j_wait_commit);
537 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400538 } else if (!tid_geq(journal->j_commit_request, target))
539 /* This should never happen, but if it does, preserve
540 the evidence before kjournald goes into a loop and
541 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400542 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400543 journal->j_commit_request,
544 journal->j_commit_sequence,
545 target, journal->j_running_transaction ?
546 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547 return 0;
548}
549
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700550int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551{
552 int ret;
553
Theodore Ts'oa931da62010-08-03 21:35:12 -0400554 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700555 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400556 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 return ret;
558}
559
560/*
561 * Force and wait upon a commit if the calling process is not within
562 * transaction. This is used for forcing out undo-protected data which contains
563 * bitmaps, when the fs is running out of space.
564 *
565 * We can only force the running transaction if we don't have an active handle;
566 * otherwise, we will deadlock.
567 *
568 * Returns true if a transaction was started.
569 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700570int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700571{
572 transaction_t *transaction = NULL;
573 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500574 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700575
Theodore Ts'oa931da62010-08-03 21:35:12 -0400576 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700577 if (journal->j_running_transaction && !current->journal_info) {
578 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500579 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
580 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581 } else if (journal->j_committing_transaction)
582 transaction = journal->j_committing_transaction;
583
584 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400585 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700586 return 0; /* Nothing to retry */
587 }
588
589 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400590 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500591 if (need_to_start)
592 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700593 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 return 1;
595}
596
597/*
598 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500599 * if a transaction is going to be committed (or is currently already
600 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700601 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700602int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603{
604 int ret = 0;
605
Theodore Ts'oa931da62010-08-03 21:35:12 -0400606 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700607 if (journal->j_running_transaction) {
608 tid_t tid = journal->j_running_transaction->t_tid;
609
Jan Karac88ccea2009-02-10 11:27:46 -0500610 __jbd2_log_start_commit(journal, tid);
611 /* There's a running transaction and we've just made sure
612 * it's commit has been scheduled. */
613 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500615 ret = 1;
616 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700617 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300618 * If commit has been started, then we have to wait for
619 * completion of that transaction.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620 */
Jan Karac88ccea2009-02-10 11:27:46 -0500621 if (ptid)
622 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623 ret = 1;
624 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400625 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700626 return ret;
627}
628
629/*
Jan Karabbd2be32011-05-24 11:59:18 -0400630 * Return 1 if a given transaction has not yet sent barrier request
631 * connected with a transaction commit. If 0 is returned, transaction
632 * may or may not have sent the barrier. Used to avoid sending barrier
633 * twice in common cases.
634 */
635int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
636{
637 int ret = 0;
638 transaction_t *commit_trans;
639
640 if (!(journal->j_flags & JBD2_BARRIER))
641 return 0;
642 read_lock(&journal->j_state_lock);
643 /* Transaction already committed? */
644 if (tid_geq(journal->j_commit_sequence, tid))
645 goto out;
646 commit_trans = journal->j_committing_transaction;
647 if (!commit_trans || commit_trans->t_tid != tid) {
648 ret = 1;
649 goto out;
650 }
651 /*
652 * Transaction is being committed and we already proceeded to
653 * submitting a flush to fs partition?
654 */
655 if (journal->j_fs_dev != journal->j_dev) {
656 if (!commit_trans->t_need_data_flush ||
657 commit_trans->t_state >= T_COMMIT_DFLUSH)
658 goto out;
659 } else {
660 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
661 goto out;
662 }
663 ret = 1;
664out:
665 read_unlock(&journal->j_state_lock);
666 return ret;
667}
668EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
669
670/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 * Wait for a specified commit to complete.
672 * The caller may not hold the journal lock.
673 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700674int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700675{
676 int err = 0;
677
Theodore Ts'oa931da62010-08-03 21:35:12 -0400678 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400679#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700680 if (!tid_geq(journal->j_commit_request, tid)) {
681 printk(KERN_EMERG
682 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400683 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700684 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400687 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700688 tid, journal->j_commit_sequence);
689 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400690 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700691 wait_event(journal->j_wait_done_commit,
692 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400693 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700694 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400695 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700696
697 if (unlikely(is_journal_aborted(journal))) {
698 printk(KERN_EMERG "journal commit I/O error\n");
699 err = -EIO;
700 }
701 return err;
702}
703
704/*
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400705 * When this function returns the transaction corresponding to tid
706 * will be completed. If the transaction has currently running, start
707 * committing that transaction before waiting for it to complete. If
708 * the transaction id is stale, it is by definition already completed,
709 * so just return SUCCESS.
710 */
711int jbd2_complete_transaction(journal_t *journal, tid_t tid)
712{
713 int need_to_wait = 1;
714
715 read_lock(&journal->j_state_lock);
716 if (journal->j_running_transaction &&
717 journal->j_running_transaction->t_tid == tid) {
718 if (journal->j_commit_request != tid) {
719 /* transaction not yet started, so request it */
720 read_unlock(&journal->j_state_lock);
721 jbd2_log_start_commit(journal, tid);
722 goto wait_commit;
723 }
724 } else if (!(journal->j_committing_transaction &&
725 journal->j_committing_transaction->t_tid == tid))
726 need_to_wait = 0;
727 read_unlock(&journal->j_state_lock);
728 if (!need_to_wait)
729 return 0;
730wait_commit:
731 return jbd2_log_wait_commit(journal, tid);
732}
733EXPORT_SYMBOL(jbd2_complete_transaction);
734
735/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700736 * Log buffer allocation routines:
737 */
738
Mingming Cao18eba7a2006-10-11 01:21:13 -0700739int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700740{
741 unsigned long blocknr;
742
Theodore Ts'oa931da62010-08-03 21:35:12 -0400743 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700744 J_ASSERT(journal->j_free > 1);
745
746 blocknr = journal->j_head;
747 journal->j_head++;
748 journal->j_free--;
749 if (journal->j_head == journal->j_last)
750 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400751 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700752 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700753}
754
755/*
756 * Conversion of logical to physical block numbers for the journal
757 *
758 * On external journals the journal blocks are identity-mapped, so
759 * this is a no-op. If needed, we can use j_blk_offset - everything is
760 * ready.
761 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700762int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700763 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700764{
765 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700766 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700767
768 if (journal->j_inode) {
769 ret = bmap(journal->j_inode, blocknr);
770 if (ret)
771 *retp = ret;
772 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700773 printk(KERN_ALERT "%s: journal block not found "
774 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400775 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700776 err = -EIO;
777 __journal_abort_soft(journal, err);
778 }
779 } else {
780 *retp = blocknr; /* +journal->j_blk_offset */
781 }
782 return err;
783}
784
785/*
786 * We play buffer_head aliasing tricks to write data/metadata blocks to
787 * the journal without copying their contents, but for journal
788 * descriptor blocks we do need to generate bona fide buffers.
789 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700790 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700791 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
792 * But we don't bother doing that, so there will be coherency problems with
793 * mmaps of blockdevs which hold live JBD-controlled filesystems.
794 */
Jan Karae5a120a2013-06-04 12:06:01 -0400795struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700796{
797 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700798 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700799 int err;
800
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700801 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700802
803 if (err)
804 return NULL;
805
806 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500807 if (!bh)
808 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700809 lock_buffer(bh);
810 memset(bh->b_data, 0, journal->j_blocksize);
811 set_buffer_uptodate(bh);
812 unlock_buffer(bh);
813 BUFFER_TRACE(bh, "return this buffer");
Jan Karae5a120a2013-06-04 12:06:01 -0400814 return bh;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815}
816
Jan Kara79feb522012-03-13 22:22:54 -0400817/*
818 * Return tid of the oldest transaction in the journal and block in the journal
819 * where the transaction starts.
820 *
821 * If the journal is now empty, return which will be the next transaction ID
822 * we will write and where will that transaction start.
823 *
824 * The return value is 0 if journal tail cannot be pushed any further, 1 if
825 * it can.
826 */
827int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
828 unsigned long *block)
829{
830 transaction_t *transaction;
831 int ret;
832
833 read_lock(&journal->j_state_lock);
834 spin_lock(&journal->j_list_lock);
835 transaction = journal->j_checkpoint_transactions;
836 if (transaction) {
837 *tid = transaction->t_tid;
838 *block = transaction->t_log_start;
839 } else if ((transaction = journal->j_committing_transaction) != NULL) {
840 *tid = transaction->t_tid;
841 *block = transaction->t_log_start;
842 } else if ((transaction = journal->j_running_transaction) != NULL) {
843 *tid = transaction->t_tid;
844 *block = journal->j_head;
845 } else {
846 *tid = journal->j_transaction_sequence;
847 *block = journal->j_head;
848 }
849 ret = tid_gt(*tid, journal->j_tail_sequence);
850 spin_unlock(&journal->j_list_lock);
851 read_unlock(&journal->j_state_lock);
852
853 return ret;
854}
855
856/*
857 * Update information in journal structure and in on disk journal superblock
858 * about log tail. This function does not check whether information passed in
859 * really pushes log tail further. It's responsibility of the caller to make
860 * sure provided log tail information is valid (e.g. by holding
861 * j_checkpoint_mutex all the time between computing log tail and calling this
862 * function as is the case with jbd2_cleanup_journal_tail()).
863 *
864 * Requires j_checkpoint_mutex
865 */
866void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
867{
868 unsigned long freed;
869
870 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
871
872 /*
873 * We cannot afford for write to remain in drive's caches since as
874 * soon as we update j_tail, next transaction can start reusing journal
875 * space and if we lose sb update during power failure we'd replay
876 * old transaction with possibly newly overwritten data.
877 */
878 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
879 write_lock(&journal->j_state_lock);
880 freed = block - journal->j_tail;
881 if (block < journal->j_tail)
882 freed += journal->j_last - journal->j_first;
883
884 trace_jbd2_update_log_tail(journal, tid, block, freed);
885 jbd_debug(1,
886 "Cleaning journal tail from %d to %d (offset %lu), "
887 "freeing %lu\n",
888 journal->j_tail_sequence, tid, block, freed);
889
890 journal->j_free += freed;
891 journal->j_tail_sequence = tid;
892 journal->j_tail = block;
893 write_unlock(&journal->j_state_lock);
894}
895
Jan Kara33395782012-03-13 22:45:38 -0400896/*
897 * This is a variaon of __jbd2_update_log_tail which checks for validity of
898 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
899 * with other threads updating log tail.
900 */
901void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
902{
903 mutex_lock(&journal->j_checkpoint_mutex);
904 if (tid_gt(tid, journal->j_tail_sequence))
905 __jbd2_update_log_tail(journal, tid, block);
906 mutex_unlock(&journal->j_checkpoint_mutex);
907}
908
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500909struct jbd2_stats_proc_session {
910 journal_t *journal;
911 struct transaction_stats_s *stats;
912 int start;
913 int max;
914};
915
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500916static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
917{
918 return *pos ? NULL : SEQ_START_TOKEN;
919}
920
921static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
922{
923 return NULL;
924}
925
926static int jbd2_seq_info_show(struct seq_file *seq, void *v)
927{
928 struct jbd2_stats_proc_session *s = seq->private;
929
930 if (v != SEQ_START_TOKEN)
931 return 0;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500932 seq_printf(seq, "%lu transactions (%lu requested), "
933 "each up to %u blocks\n",
934 s->stats->ts_tid, s->stats->ts_requested,
935 s->journal->j_max_transaction_buffers);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500936 if (s->stats->ts_tid == 0)
937 return 0;
938 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400939 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500940 seq_printf(seq, " %ums request delay\n",
941 (s->stats->ts_requested == 0) ? 0 :
942 jiffies_to_msecs(s->stats->run.rs_request_delay /
943 s->stats->ts_requested));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500944 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400945 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500946 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400947 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500948 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400949 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500950 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400951 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500952 seq_printf(seq, " %lluus average transaction commit time\n",
953 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500954 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400955 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500956 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400957 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500958 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400959 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500960 return 0;
961}
962
963static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
964{
965}
966
James Morris88e9d342009-09-22 16:43:43 -0700967static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500968 .start = jbd2_seq_info_start,
969 .next = jbd2_seq_info_next,
970 .stop = jbd2_seq_info_stop,
971 .show = jbd2_seq_info_show,
972};
973
974static int jbd2_seq_info_open(struct inode *inode, struct file *file)
975{
Al Virod9dda782013-03-31 18:16:14 -0400976 journal_t *journal = PDE_DATA(inode);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500977 struct jbd2_stats_proc_session *s;
978 int rc, size;
979
980 s = kmalloc(sizeof(*s), GFP_KERNEL);
981 if (s == NULL)
982 return -ENOMEM;
983 size = sizeof(struct transaction_stats_s);
984 s->stats = kmalloc(size, GFP_KERNEL);
985 if (s->stats == NULL) {
986 kfree(s);
987 return -ENOMEM;
988 }
989 spin_lock(&journal->j_history_lock);
990 memcpy(s->stats, &journal->j_stats, size);
991 s->journal = journal;
992 spin_unlock(&journal->j_history_lock);
993
994 rc = seq_open(file, &jbd2_seq_info_ops);
995 if (rc == 0) {
996 struct seq_file *m = file->private_data;
997 m->private = s;
998 } else {
999 kfree(s->stats);
1000 kfree(s);
1001 }
1002 return rc;
1003
1004}
1005
1006static int jbd2_seq_info_release(struct inode *inode, struct file *file)
1007{
1008 struct seq_file *seq = file->private_data;
1009 struct jbd2_stats_proc_session *s = seq->private;
1010 kfree(s->stats);
1011 kfree(s);
1012 return seq_release(inode, file);
1013}
1014
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001015static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001016 .owner = THIS_MODULE,
1017 .open = jbd2_seq_info_open,
1018 .read = seq_read,
1019 .llseek = seq_lseek,
1020 .release = jbd2_seq_info_release,
1021};
1022
1023static struct proc_dir_entry *proc_jbd2_stats;
1024
1025static void jbd2_stats_proc_init(journal_t *journal)
1026{
Theodore Ts'o05496762008-09-16 14:36:17 -04001027 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001028 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -07001029 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1030 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001031 }
1032}
1033
1034static void jbd2_stats_proc_exit(journal_t *journal)
1035{
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001036 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -04001037 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001038}
1039
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040/*
1041 * Management for journal control blocks: functions to create and
1042 * destroy journal_t structures, and to initialise and read existing
1043 * journal blocks from disk. */
1044
1045/* First: create and setup a journal_t object in memory. We initialise
1046 * very few fields yet: that has to wait until we have created the
1047 * journal structures from from scratch, or loaded them from disk. */
1048
1049static journal_t * journal_init_common (void)
1050{
1051 journal_t *journal;
1052 int err;
1053
Andrew Morton3ebfdf82009-12-23 08:05:15 -05001054 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001055 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001056 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001057
1058 init_waitqueue_head(&journal->j_wait_transaction_locked);
1059 init_waitqueue_head(&journal->j_wait_logspace);
1060 init_waitqueue_head(&journal->j_wait_done_commit);
1061 init_waitqueue_head(&journal->j_wait_checkpoint);
1062 init_waitqueue_head(&journal->j_wait_commit);
1063 init_waitqueue_head(&journal->j_wait_updates);
1064 mutex_init(&journal->j_barrier);
1065 mutex_init(&journal->j_checkpoint_mutex);
1066 spin_lock_init(&journal->j_revoke_lock);
1067 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001068 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001069
Mingming Caocd02ff02007-10-16 18:38:25 -04001070 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001071 journal->j_min_batch_time = 0;
1072 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001073
1074 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001075 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001076
1077 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001078 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 if (err) {
1080 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001081 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001082 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001083
Theodore Ts'obf699322009-09-30 00:32:06 -04001084 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001085
Dave Kleikamp470decc2006-10-11 01:20:57 -07001086 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087}
1088
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001089/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 *
1091 * Create a journal structure assigned some fixed set of disk blocks to
1092 * the journal. We don't actually touch those disk blocks yet, but we
1093 * need to set up all of the mapping information to tell the journaling
1094 * system where the journal blocks are.
1095 *
1096 */
1097
1098/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001099 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001100 * @bdev: Block device on which to create the journal
1101 * @fs_dev: Device which hold journalled filesystem for this journal.
1102 * @start: Block nr Start of journal.
1103 * @len: Length of the journal in blocks.
1104 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001105 *
1106 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001108 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001109 * range of blocks on an arbitrary block device.
1110 *
1111 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001112journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001114 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115{
1116 journal_t *journal = journal_init_common();
1117 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001118 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001119 int n;
1120
1121 if (!journal)
1122 return NULL;
1123
1124 /* journal descriptor can store up to n blocks -bzzz */
1125 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001126 journal->j_dev = bdev;
1127 journal->j_fs_dev = fs_dev;
1128 journal->j_blk_offset = start;
1129 journal->j_maxlen = len;
1130 bdevname(journal->j_dev, journal->j_devname);
1131 p = journal->j_devname;
1132 while ((p = strchr(p, '/')))
1133 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001134 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1136 journal->j_wbufsize = n;
1137 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1138 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001139 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001140 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001141 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143
1144 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001145 if (!bh) {
1146 printk(KERN_ERR
1147 "%s: Cannot get buffer for journal superblock\n",
1148 __func__);
1149 goto out_err;
1150 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151 journal->j_sb_buffer = bh;
1152 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001153
Dave Kleikamp470decc2006-10-11 01:20:57 -07001154 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001155out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001156 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001157 jbd2_stats_proc_exit(journal);
1158 kfree(journal);
1159 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160}
1161
1162/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001163 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001164 * @inode: An inode to create the journal in
1165 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001166 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167 * the journal. The inode must exist already, must support bmap() and
1168 * must have all data blocks preallocated.
1169 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001170journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001171{
1172 struct buffer_head *bh;
1173 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001174 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001175 int err;
1176 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001177 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001178
1179 if (!journal)
1180 return NULL;
1181
1182 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1183 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001184 bdevname(journal->j_dev, journal->j_devname);
1185 p = journal->j_devname;
1186 while ((p = strchr(p, '/')))
1187 *p = '!';
1188 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001189 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001190 jbd_debug(1,
1191 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1192 journal, inode->i_sb->s_id, inode->i_ino,
1193 (long long) inode->i_size,
1194 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1195
1196 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1197 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001198 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001199
1200 /* journal descriptor can store up to n blocks -bzzz */
1201 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1202 journal->j_wbufsize = n;
1203 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1204 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001205 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001206 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001207 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001208 }
1209
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001210 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001211 /* If that failed, give up */
1212 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001213 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001214 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001215 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001216 }
1217
1218 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001219 if (!bh) {
1220 printk(KERN_ERR
1221 "%s: Cannot get buffer for journal superblock\n",
1222 __func__);
1223 goto out_err;
1224 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001225 journal->j_sb_buffer = bh;
1226 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1227
1228 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001229out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001230 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001231 jbd2_stats_proc_exit(journal);
1232 kfree(journal);
1233 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001234}
1235
1236/*
1237 * If the journal init or create aborts, we need to mark the journal
1238 * superblock as being NULL to prevent the journal destroy from writing
1239 * back a bogus superblock.
1240 */
1241static void journal_fail_superblock (journal_t *journal)
1242{
1243 struct buffer_head *bh = journal->j_sb_buffer;
1244 brelse(bh);
1245 journal->j_sb_buffer = NULL;
1246}
1247
1248/*
1249 * Given a journal_t structure, initialise the various fields for
1250 * startup of a new journaling session. We use this both when creating
1251 * a journal, and after recovering an old journal to reset it for
1252 * subsequent use.
1253 */
1254
1255static int journal_reset(journal_t *journal)
1256{
1257 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001258 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001259
1260 first = be32_to_cpu(sb->s_first);
1261 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001262 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001263 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001264 first, last);
1265 journal_fail_superblock(journal);
1266 return -EINVAL;
1267 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001268
1269 journal->j_first = first;
1270 journal->j_last = last;
1271
1272 journal->j_head = first;
1273 journal->j_tail = first;
1274 journal->j_free = last - first;
1275
1276 journal->j_tail_sequence = journal->j_transaction_sequence;
1277 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1278 journal->j_commit_request = journal->j_commit_sequence;
1279
1280 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1281
Dave Kleikamp470decc2006-10-11 01:20:57 -07001282 /*
1283 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001284 * no recovery (s_start == 0), then we can safely defer the superblock
1285 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286 * attempting a write to a potential-readonly device.
1287 */
Jan Kara24bcc892012-03-13 15:41:04 -04001288 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001289 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001290 "(start %ld, seq %d, errno %d)\n",
1291 journal->j_tail, journal->j_tail_sequence,
1292 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001293 journal->j_flags |= JBD2_FLUSHED;
1294 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001295 /* Lock here to make assertions happy... */
1296 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001297 /*
1298 * Update log tail information. We use WRITE_FUA since new
1299 * transaction will start reusing journal space and so we
1300 * must make sure information about current log tail is on
1301 * disk before that.
1302 */
1303 jbd2_journal_update_sb_log_tail(journal,
1304 journal->j_tail_sequence,
1305 journal->j_tail,
1306 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001307 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001308 }
Jan Kara24bcc892012-03-13 15:41:04 -04001309 return jbd2_journal_start_thread(journal);
1310}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001311
Jan Kara79feb522012-03-13 22:22:54 -04001312static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001313{
1314 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001315 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001316
Jan Kara79feb522012-03-13 22:22:54 -04001317 trace_jbd2_write_superblock(journal, write_op);
1318 if (!(journal->j_flags & JBD2_BARRIER))
1319 write_op &= ~(REQ_FUA | REQ_FLUSH);
1320 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001321 if (buffer_write_io_error(bh)) {
1322 /*
1323 * Oh, dear. A previous attempt to write the journal
1324 * superblock failed. This could happen because the
1325 * USB device was yanked out. Or it could happen to
1326 * be a transient write error and maybe the block will
1327 * be remapped. Nothing we can do but to retry the
1328 * write and hope for the best.
1329 */
1330 printk(KERN_ERR "JBD2: previous I/O error detected "
1331 "for journal superblock update for %s.\n",
1332 journal->j_devname);
1333 clear_buffer_write_io_error(bh);
1334 set_buffer_uptodate(bh);
1335 }
Jan Kara79feb522012-03-13 22:22:54 -04001336 get_bh(bh);
1337 bh->b_end_io = end_buffer_write_sync;
1338 ret = submit_bh(write_op, bh);
1339 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001340 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001341 clear_buffer_write_io_error(bh);
1342 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001343 ret = -EIO;
1344 }
1345 if (ret) {
1346 printk(KERN_ERR "JBD2: Error %d detected when updating "
1347 "journal superblock for %s.\n", ret,
1348 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001349 }
1350}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001351
Jan Kara24bcc892012-03-13 15:41:04 -04001352/**
1353 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1354 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001355 * @tail_tid: TID of the new transaction at the tail of the log
1356 * @tail_block: The first block of the transaction at the tail of the log
1357 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001358 *
1359 * Update a journal's superblock information about log tail and write it to
1360 * disk, waiting for the IO to complete.
1361 */
Jan Kara79feb522012-03-13 22:22:54 -04001362void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1363 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001364{
1365 journal_superblock_t *sb = journal->j_superblock;
1366
Jan Karaa78bb112012-03-13 15:43:04 -04001367 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001368 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1369 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370
Jan Kara79feb522012-03-13 22:22:54 -04001371 sb->s_sequence = cpu_to_be32(tail_tid);
1372 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001373
Jan Kara79feb522012-03-13 22:22:54 -04001374 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001375
1376 /* Log is no longer empty */
1377 write_lock(&journal->j_state_lock);
1378 WARN_ON(!sb->s_sequence);
1379 journal->j_flags &= ~JBD2_FLUSHED;
1380 write_unlock(&journal->j_state_lock);
1381}
1382
1383/**
1384 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1385 * @journal: The journal to update.
1386 *
1387 * Update a journal's dynamic superblock fields to show that journal is empty.
1388 * Write updated superblock to disk waiting for IO to complete.
1389 */
1390static void jbd2_mark_journal_empty(journal_t *journal)
1391{
1392 journal_superblock_t *sb = journal->j_superblock;
1393
Jan Karaa78bb112012-03-13 15:43:04 -04001394 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 read_lock(&journal->j_state_lock);
Eric Sandeeneeecef02012-08-18 22:29:40 -04001396 /* Is it already empty? */
1397 if (sb->s_start == 0) {
1398 read_unlock(&journal->j_state_lock);
1399 return;
1400 }
Jan Kara24bcc892012-03-13 15:41:04 -04001401 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1402 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403
1404 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001405 sb->s_start = cpu_to_be32(0);
1406 read_unlock(&journal->j_state_lock);
1407
Jan Kara79feb522012-03-13 22:22:54 -04001408 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001409
1410 /* Log is no longer empty */
1411 write_lock(&journal->j_state_lock);
1412 journal->j_flags |= JBD2_FLUSHED;
1413 write_unlock(&journal->j_state_lock);
1414}
1415
1416
1417/**
1418 * jbd2_journal_update_sb_errno() - Update error in the journal.
1419 * @journal: The journal to update.
1420 *
1421 * Update a journal's errno. Write updated superblock to disk waiting for IO
1422 * to complete.
1423 */
Theodore Ts'od796c522012-08-05 19:04:57 -04001424void jbd2_journal_update_sb_errno(journal_t *journal)
Jan Kara24bcc892012-03-13 15:41:04 -04001425{
1426 journal_superblock_t *sb = journal->j_superblock;
1427
1428 read_lock(&journal->j_state_lock);
1429 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1430 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001431 sb->s_errno = cpu_to_be32(journal->j_errno);
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001432 jbd2_superblock_csum_set(journal, sb);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001433 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001434
Jan Kara79feb522012-03-13 22:22:54 -04001435 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436}
Theodore Ts'od796c522012-08-05 19:04:57 -04001437EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438
1439/*
1440 * Read the superblock for a given journal, performing initial
1441 * validation of the format.
1442 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443static int journal_get_superblock(journal_t *journal)
1444{
1445 struct buffer_head *bh;
1446 journal_superblock_t *sb;
1447 int err = -EIO;
1448
1449 bh = journal->j_sb_buffer;
1450
1451 J_ASSERT(bh != NULL);
1452 if (!buffer_uptodate(bh)) {
1453 ll_rw_block(READ, 1, &bh);
1454 wait_on_buffer(bh);
1455 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001456 printk(KERN_ERR
1457 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001458 goto out;
1459 }
1460 }
1461
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001462 if (buffer_verified(bh))
1463 return 0;
1464
Dave Kleikamp470decc2006-10-11 01:20:57 -07001465 sb = journal->j_superblock;
1466
1467 err = -EINVAL;
1468
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001469 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001471 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001472 goto out;
1473 }
1474
1475 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001476 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001477 journal->j_format_version = 1;
1478 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001479 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001480 journal->j_format_version = 2;
1481 break;
1482 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001483 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001484 goto out;
1485 }
1486
1487 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1488 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1489 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001490 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491 goto out;
1492 }
1493
Eryu Guan87622022011-11-01 19:04:59 -04001494 if (be32_to_cpu(sb->s_first) == 0 ||
1495 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1496 printk(KERN_WARNING
1497 "JBD2: Invalid start block of journal: %u\n",
1498 be32_to_cpu(sb->s_first));
1499 goto out;
1500 }
1501
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001502 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1503 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1504 /* Can't have checksum v1 and v2 on at the same time! */
1505 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1506 "at the same time!\n");
1507 goto out;
1508 }
1509
1510 if (!jbd2_verify_csum_type(journal, sb)) {
1511 printk(KERN_ERR "JBD: Unknown checksum type\n");
1512 goto out;
1513 }
1514
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001515 /* Load the checksum driver */
1516 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1517 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1518 if (IS_ERR(journal->j_chksum_driver)) {
1519 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1520 err = PTR_ERR(journal->j_chksum_driver);
1521 journal->j_chksum_driver = NULL;
1522 goto out;
1523 }
1524 }
1525
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001526 /* Check superblock checksum */
1527 if (!jbd2_superblock_csum_verify(journal, sb)) {
1528 printk(KERN_ERR "JBD: journal checksum error\n");
1529 goto out;
1530 }
1531
1532 /* Precompute checksum seed for all metadata */
1533 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1534 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1535 sizeof(sb->s_uuid));
1536
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001537 set_buffer_verified(bh);
1538
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 return 0;
1540
1541out:
1542 journal_fail_superblock(journal);
1543 return err;
1544}
1545
1546/*
1547 * Load the on-disk journal superblock and read the key fields into the
1548 * journal_t.
1549 */
1550
1551static int load_superblock(journal_t *journal)
1552{
1553 int err;
1554 journal_superblock_t *sb;
1555
1556 err = journal_get_superblock(journal);
1557 if (err)
1558 return err;
1559
1560 sb = journal->j_superblock;
1561
1562 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1563 journal->j_tail = be32_to_cpu(sb->s_start);
1564 journal->j_first = be32_to_cpu(sb->s_first);
1565 journal->j_last = be32_to_cpu(sb->s_maxlen);
1566 journal->j_errno = be32_to_cpu(sb->s_errno);
1567
1568 return 0;
1569}
1570
1571
1572/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001573 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001574 * @journal: Journal to act on.
1575 *
1576 * Given a journal_t structure which tells us which disk blocks contain
1577 * a journal, read the journal from disk to initialise the in-memory
1578 * structures.
1579 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001580int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001581{
1582 int err;
1583 journal_superblock_t *sb;
1584
1585 err = load_superblock(journal);
1586 if (err)
1587 return err;
1588
1589 sb = journal->j_superblock;
1590 /* If this is a V2 superblock, then we have to check the
1591 * features flags on it. */
1592
1593 if (journal->j_format_version >= 2) {
1594 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001595 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001597 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001598 printk(KERN_WARNING
1599 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001600 return -EINVAL;
1601 }
1602 }
1603
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001604 /*
1605 * Create a slab for this blocksize
1606 */
1607 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1608 if (err)
1609 return err;
1610
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611 /* Let the recovery code check whether it needs to recover any
1612 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001613 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 goto recovery_error;
1615
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001616 if (journal->j_failed_commit) {
1617 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1618 "is corrupt.\n", journal->j_failed_commit,
1619 journal->j_devname);
1620 return -EIO;
1621 }
1622
Dave Kleikamp470decc2006-10-11 01:20:57 -07001623 /* OK, we've finished with the dynamic journal bits:
1624 * reinitialise the dynamic contents of the superblock in memory
1625 * and reset them on disk. */
1626 if (journal_reset(journal))
1627 goto recovery_error;
1628
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001629 journal->j_flags &= ~JBD2_ABORT;
1630 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001631 return 0;
1632
1633recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001634 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001635 return -EIO;
1636}
1637
1638/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001639 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001640 * @journal: Journal to act on.
1641 *
1642 * Release a journal_t structure once it is no longer in use by the
1643 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001644 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001646int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001648 int err = 0;
1649
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 /* Wait for the commit thread to wake up and die. */
1651 journal_kill_thread(journal);
1652
1653 /* Force a final log commit */
1654 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001655 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656
1657 /* Force any old transactions to disk */
1658
1659 /* Totally anal locking here... */
1660 spin_lock(&journal->j_list_lock);
1661 while (journal->j_checkpoint_transactions != NULL) {
1662 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001663 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001665 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666 spin_lock(&journal->j_list_lock);
1667 }
1668
1669 J_ASSERT(journal->j_running_transaction == NULL);
1670 J_ASSERT(journal->j_committing_transaction == NULL);
1671 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1672 spin_unlock(&journal->j_list_lock);
1673
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001675 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001676 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001677 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001678 mutex_unlock(&journal->j_checkpoint_mutex);
1679 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001680 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681 brelse(journal->j_sb_buffer);
1682 }
1683
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001684 if (journal->j_proc_entry)
1685 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001686 if (journal->j_inode)
1687 iput(journal->j_inode);
1688 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001689 jbd2_journal_destroy_revoke(journal);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001690 if (journal->j_chksum_driver)
1691 crypto_free_shash(journal->j_chksum_driver);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001692 kfree(journal->j_wbuf);
1693 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001694
1695 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001696}
1697
1698
1699/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701 * @journal: Journal to check.
1702 * @compat: bitmask of compatible features
1703 * @ro: bitmask of features that force read-only mount
1704 * @incompat: bitmask of incompatible features
1705 *
1706 * Check whether the journal uses all of a given set of
1707 * features. Return true (non-zero) if it does.
1708 **/
1709
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001710int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001711 unsigned long ro, unsigned long incompat)
1712{
1713 journal_superblock_t *sb;
1714
1715 if (!compat && !ro && !incompat)
1716 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001717 /* Load journal superblock if it is not loaded yet. */
1718 if (journal->j_format_version == 0 &&
1719 journal_get_superblock(journal) != 0)
1720 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721 if (journal->j_format_version == 1)
1722 return 0;
1723
1724 sb = journal->j_superblock;
1725
1726 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1727 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1728 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1729 return 1;
1730
1731 return 0;
1732}
1733
1734/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001735 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001736 * @journal: Journal to check.
1737 * @compat: bitmask of compatible features
1738 * @ro: bitmask of features that force read-only mount
1739 * @incompat: bitmask of incompatible features
1740 *
1741 * Check whether the journaling code supports the use of
1742 * all of a given set of features on this journal. Return true
1743 * (non-zero) if it can. */
1744
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001745int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746 unsigned long ro, unsigned long incompat)
1747{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001748 if (!compat && !ro && !incompat)
1749 return 1;
1750
Dave Kleikamp470decc2006-10-11 01:20:57 -07001751 /* We can support any known requested features iff the
1752 * superblock is in version 2. Otherwise we fail to support any
1753 * extended sb features. */
1754
1755 if (journal->j_format_version != 2)
1756 return 0;
1757
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001758 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1759 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1760 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001761 return 1;
1762
1763 return 0;
1764}
1765
1766/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001767 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001768 * @journal: Journal to act on.
1769 * @compat: bitmask of compatible features
1770 * @ro: bitmask of features that force read-only mount
1771 * @incompat: bitmask of incompatible features
1772 *
1773 * Mark a given journal feature as present on the
1774 * superblock. Returns true if the requested features could be set.
1775 *
1776 */
1777
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001778int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779 unsigned long ro, unsigned long incompat)
1780{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001781#define INCOMPAT_FEATURE_ON(f) \
1782 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1783#define COMPAT_FEATURE_ON(f) \
1784 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785 journal_superblock_t *sb;
1786
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001787 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001788 return 1;
1789
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 return 0;
1792
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001793 /* Asking for checksumming v2 and v1? Only give them v2. */
1794 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1795 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1796 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1797
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1799 compat, ro, incompat);
1800
1801 sb = journal->j_superblock;
1802
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001803 /* If enabling v2 checksums, update superblock */
1804 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1805 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1806 sb->s_feature_compat &=
1807 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001808
1809 /* Load the checksum driver */
1810 if (journal->j_chksum_driver == NULL) {
1811 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1812 0, 0);
1813 if (IS_ERR(journal->j_chksum_driver)) {
1814 printk(KERN_ERR "JBD: Cannot load crc32c "
1815 "driver.\n");
1816 journal->j_chksum_driver = NULL;
1817 return 0;
1818 }
1819 }
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001820
1821 /* Precompute checksum seed for all metadata */
1822 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1823 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1824 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1825 sb->s_uuid,
1826 sizeof(sb->s_uuid));
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001827 }
1828
1829 /* If enabling v1 checksums, downgrade superblock */
1830 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1831 sb->s_feature_incompat &=
1832 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1833
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834 sb->s_feature_compat |= cpu_to_be32(compat);
1835 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1836 sb->s_feature_incompat |= cpu_to_be32(incompat);
1837
1838 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001839#undef COMPAT_FEATURE_ON
1840#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841}
1842
Girish Shilamkar818d2762008-01-28 23:58:27 -05001843/*
1844 * jbd2_journal_clear_features () - Clear a given journal feature in the
1845 * superblock
1846 * @journal: Journal to act on.
1847 * @compat: bitmask of compatible features
1848 * @ro: bitmask of features that force read-only mount
1849 * @incompat: bitmask of incompatible features
1850 *
1851 * Clear a given journal feature as present on the
1852 * superblock.
1853 */
1854void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1855 unsigned long ro, unsigned long incompat)
1856{
1857 journal_superblock_t *sb;
1858
1859 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1860 compat, ro, incompat);
1861
1862 sb = journal->j_superblock;
1863
1864 sb->s_feature_compat &= ~cpu_to_be32(compat);
1865 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1866 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1867}
1868EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869
1870/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001871 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001872 * @journal: Journal to act on.
1873 *
1874 * Flush all data for a given journal to disk and empty the journal.
1875 * Filesystems can use this when remounting readonly to ensure that
1876 * recovery does not need to happen on remount.
1877 */
1878
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880{
1881 int err = 0;
1882 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001883
Theodore Ts'oa931da62010-08-03 21:35:12 -04001884 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001885
1886 /* Force everything buffered to the log... */
1887 if (journal->j_running_transaction) {
1888 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001889 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890 } else if (journal->j_committing_transaction)
1891 transaction = journal->j_committing_transaction;
1892
1893 /* Wait for the log commit to complete... */
1894 if (transaction) {
1895 tid_t tid = transaction->t_tid;
1896
Theodore Ts'oa931da62010-08-03 21:35:12 -04001897 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001898 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001900 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001901 }
1902
1903 /* ...and flush everything in the log out to disk. */
1904 spin_lock(&journal->j_list_lock);
1905 while (!err && journal->j_checkpoint_transactions != NULL) {
1906 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001907 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001909 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001910 spin_lock(&journal->j_list_lock);
1911 }
1912 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001913
1914 if (is_journal_aborted(journal))
1915 return -EIO;
1916
Jan Karaa78bb112012-03-13 15:43:04 -04001917 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919
1920 /* Finally, mark the journal as really needing no recovery.
1921 * This sets s_start==0 in the underlying superblock, which is
1922 * the magic code for a fully-recovered superblock. Any future
1923 * commits of data to the journal will restore the current
1924 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001925 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001926 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001927 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001928 J_ASSERT(!journal->j_running_transaction);
1929 J_ASSERT(!journal->j_committing_transaction);
1930 J_ASSERT(!journal->j_checkpoint_transactions);
1931 J_ASSERT(journal->j_head == journal->j_tail);
1932 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001933 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001934 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001935}
1936
1937/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001938 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001939 * @journal: Journal to act on.
1940 * @write: flag (see below)
1941 *
1942 * Wipe out all of the contents of a journal, safely. This will produce
1943 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001944 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001945 *
1946 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1947 * we merely suppress recovery.
1948 */
1949
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001950int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001951{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001952 int err = 0;
1953
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001954 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955
1956 err = load_superblock(journal);
1957 if (err)
1958 return err;
1959
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 if (!journal->j_tail)
1961 goto no_recovery;
1962
Eryu Guanf2a44522011-11-01 19:09:18 -04001963 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 write ? "Clearing" : "Ignoring");
1965
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001967 if (write) {
1968 /* Lock to make assertions happy... */
1969 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001970 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001971 mutex_unlock(&journal->j_checkpoint_mutex);
1972 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973
1974 no_recovery:
1975 return err;
1976}
1977
1978/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001979 * Journal abort has very specific semantics, which we describe
1980 * for journal abort.
1981 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001982 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001983 * itself are here.
1984 */
1985
1986/*
1987 * Quick version for internal journal use (doesn't lock the journal).
1988 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1989 * and don't attempt to make any other journal updates.
1990 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001991void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992{
1993 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001995 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001996 return;
1997
1998 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001999 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000
Theodore Ts'oa931da62010-08-03 21:35:12 -04002001 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002002 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003 transaction = journal->j_running_transaction;
2004 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002005 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04002006 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007}
2008
2009/* Soft abort: record the abort error status in the journal superblock,
2010 * but don't do any other IO. */
2011static void __journal_abort_soft (journal_t *journal, int errno)
2012{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002013 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002014 return;
2015
2016 if (!journal->j_errno)
2017 journal->j_errno = errno;
2018
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002019 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020
2021 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04002022 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023}
2024
2025/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002026 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027 * @journal: the journal to shutdown.
2028 * @errno: an error number to record in the journal indicating
2029 * the reason for the shutdown.
2030 *
2031 * Perform a complete, immediate shutdown of the ENTIRE
2032 * journal (not of a single transaction). This operation cannot be
2033 * undone without closing and reopening the journal.
2034 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002035 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07002036 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2037 * mode.
2038 *
2039 * Journal abort has very specific semantics. Any existing dirty,
2040 * unjournaled buffers in the main filesystem will still be written to
2041 * disk by bdflush, but the journaling mechanism will be suspended
2042 * immediately and no further transaction commits will be honoured.
2043 *
2044 * Any dirty, journaled buffers will be written back to disk without
2045 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2046 * filesystem, but we _do_ attempt to leave as much data as possible
2047 * behind for fsck to use for cleanup.
2048 *
2049 * Any attempt to get a new transaction handle on a journal which is in
2050 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 * entered abort state during the update.
2053 *
2054 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002055 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002056 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002057 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 * which will be recorded (if possible) in the journal superblock. This
2059 * allows a client to record failure conditions in the middle of a
2060 * transaction without having to complete the transaction to record the
2061 * failure to disk. ext3_error, for example, now uses this
2062 * functionality.
2063 *
2064 * Errors which originate from within the journaling layer will NOT
2065 * supply an errno; a null errno implies that absolutely no further
2066 * writes are done to the journal (unless there are any already in
2067 * progress).
2068 *
2069 */
2070
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002071void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002072{
2073 __journal_abort_soft(journal, errno);
2074}
2075
2076/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002077 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002078 * @journal: journal to examine.
2079 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002080 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07002081 * time the journal was mounted - if the journal was stopped
2082 * without calling abort this will be 0.
2083 *
2084 * If the journal has been aborted on this mount time -EROFS will
2085 * be returned.
2086 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002087int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002088{
2089 int err;
2090
Theodore Ts'oa931da62010-08-03 21:35:12 -04002091 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002092 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002093 err = -EROFS;
2094 else
2095 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002096 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097 return err;
2098}
2099
2100/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002101 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07002102 * @journal: journal to act on.
2103 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002104 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002105 * mode.
2106 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002107int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002108{
2109 int err = 0;
2110
Theodore Ts'oa931da62010-08-03 21:35:12 -04002111 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002112 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002113 err = -EROFS;
2114 else
2115 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002116 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002117 return err;
2118}
2119
2120/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002121 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002122 * @journal: journal to act on.
2123 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002124 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002125 * mode.
2126 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002127void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002128{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002129 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002130 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002131 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002132 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002133}
2134
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002135int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002136{
2137 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2138}
2139
2140/*
Zach Brownb517bea2006-10-11 01:21:08 -07002141 * helper functions to deal with 32 or 64bit block numbers.
2142 */
2143size_t journal_tag_bytes(journal_t *journal)
2144{
Darrick J. Wongc3900872012-05-27 08:12:12 -04002145 journal_block_tag_t tag;
2146 size_t x = 0;
2147
2148 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2149 x += sizeof(tag.t_checksum);
2150
Zach Brownb517bea2006-10-11 01:21:08 -07002151 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Darrick J. Wongc3900872012-05-27 08:12:12 -04002152 return x + JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002153 else
Darrick J. Wongc3900872012-05-27 08:12:12 -04002154 return x + JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002155}
2156
2157/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002158 * JBD memory management
2159 *
2160 * These functions are used to allocate block-sized chunks of memory
2161 * used for making copies of buffer_head data. Very often it will be
2162 * page-sized chunks of data, but sometimes it will be in
2163 * sub-page-size chunks. (For example, 16k pages on Power systems
2164 * with a 4k block file system.) For blocks smaller than a page, we
2165 * use a SLAB allocator. There are slab caches for each block size,
2166 * which are allocated at mount time, if necessary, and we only free
2167 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2168 * this reason we don't need to a mutex to protect access to
2169 * jbd2_slab[] allocating or releasing memory; only in
2170 * jbd2_journal_create_slab().
2171 */
2172#define JBD2_MAX_SLABS 8
2173static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002174
2175static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2176 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2177 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2178};
2179
2180
2181static void jbd2_journal_destroy_slabs(void)
2182{
2183 int i;
2184
2185 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2186 if (jbd2_slab[i])
2187 kmem_cache_destroy(jbd2_slab[i]);
2188 jbd2_slab[i] = NULL;
2189 }
2190}
2191
2192static int jbd2_journal_create_slab(size_t size)
2193{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002194 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002195 int i = order_base_2(size) - 10;
2196 size_t slab_size;
2197
2198 if (size == PAGE_SIZE)
2199 return 0;
2200
2201 if (i >= JBD2_MAX_SLABS)
2202 return -EINVAL;
2203
2204 if (unlikely(i < 0))
2205 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002206 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002207 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002208 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002209 return 0; /* Already created */
2210 }
2211
2212 slab_size = 1 << (i+10);
2213 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2214 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002215 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002216 if (!jbd2_slab[i]) {
2217 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2218 return -ENOMEM;
2219 }
2220 return 0;
2221}
2222
2223static struct kmem_cache *get_slab(size_t size)
2224{
2225 int i = order_base_2(size) - 10;
2226
2227 BUG_ON(i >= JBD2_MAX_SLABS);
2228 if (unlikely(i < 0))
2229 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002230 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002231 return jbd2_slab[i];
2232}
2233
2234void *jbd2_alloc(size_t size, gfp_t flags)
2235{
2236 void *ptr;
2237
2238 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2239
2240 flags |= __GFP_REPEAT;
2241 if (size == PAGE_SIZE)
2242 ptr = (void *)__get_free_pages(flags, 0);
2243 else if (size > PAGE_SIZE) {
2244 int order = get_order(size);
2245
2246 if (order < 3)
2247 ptr = (void *)__get_free_pages(flags, order);
2248 else
2249 ptr = vmalloc(size);
2250 } else
2251 ptr = kmem_cache_alloc(get_slab(size), flags);
2252
2253 /* Check alignment; SLUB has gotten this wrong in the past,
2254 * and this can lead to user data corruption! */
2255 BUG_ON(((unsigned long) ptr) & (size-1));
2256
2257 return ptr;
2258}
2259
2260void jbd2_free(void *ptr, size_t size)
2261{
2262 if (size == PAGE_SIZE) {
2263 free_pages((unsigned long)ptr, 0);
2264 return;
2265 }
2266 if (size > PAGE_SIZE) {
2267 int order = get_order(size);
2268
2269 if (order < 3)
2270 free_pages((unsigned long)ptr, order);
2271 else
2272 vfree(ptr);
2273 return;
2274 }
2275 kmem_cache_free(get_slab(size), ptr);
2276};
2277
2278/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002279 * Journal_head storage management
2280 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002281static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002282#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002283static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2284#endif
2285
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002286static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002287{
2288 int retval;
2289
Al Viro1076d172008-03-29 03:07:18 +00002290 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002291 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002292 sizeof(struct journal_head),
2293 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002294 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002295 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002296 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002297 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002298 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002299 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002300 }
2301 return retval;
2302}
2303
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002304static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002305{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002306 if (jbd2_journal_head_cache) {
2307 kmem_cache_destroy(jbd2_journal_head_cache);
2308 jbd2_journal_head_cache = NULL;
2309 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002310}
2311
2312/*
2313 * journal_head splicing and dicing
2314 */
2315static struct journal_head *journal_alloc_journal_head(void)
2316{
2317 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002318
Jose R. Santose23291b2007-07-18 08:57:06 -04002319#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002320 atomic_inc(&nr_journal_heads);
2321#endif
Zheng Liu5d9cf9c2013-05-28 07:27:11 -04002322 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002323 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002324 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002325 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002326 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002327 yield();
Zheng Liu5d9cf9c2013-05-28 07:27:11 -04002328 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002329 }
2330 }
2331 return ret;
2332}
2333
2334static void journal_free_journal_head(struct journal_head *jh)
2335{
Jose R. Santose23291b2007-07-18 08:57:06 -04002336#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002337 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002338 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002339#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002340 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002341}
2342
2343/*
2344 * A journal_head is attached to a buffer_head whenever JBD has an
2345 * interest in the buffer.
2346 *
2347 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2348 * is set. This bit is tested in core kernel code where we need to take
2349 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2350 * there.
2351 *
2352 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2353 *
2354 * When a buffer has its BH_JBD bit set it is immune from being released by
2355 * core kernel code, mainly via ->b_count.
2356 *
Jan Karade1b7942011-06-13 15:38:22 -04002357 * A journal_head is detached from its buffer_head when the journal_head's
2358 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2359 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002360 *
2361 * Various places in the kernel want to attach a journal_head to a buffer_head
2362 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002363 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002364 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002365 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002366 *
2367 * So the typical usage would be:
2368 *
2369 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002370 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002371 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002372 * (Get another reference for transaction)
2373 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002375 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002376 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002377 */
2378
2379/*
2380 * Give a buffer_head a journal_head.
2381 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002382 * May sleep.
2383 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002384struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002385{
2386 struct journal_head *jh;
2387 struct journal_head *new_jh = NULL;
2388
2389repeat:
Zheng Liu5d9cf9c2013-05-28 07:27:11 -04002390 if (!buffer_jbd(bh))
Dave Kleikamp470decc2006-10-11 01:20:57 -07002391 new_jh = journal_alloc_journal_head();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002392
2393 jbd_lock_bh_journal_head(bh);
2394 if (buffer_jbd(bh)) {
2395 jh = bh2jh(bh);
2396 } else {
2397 J_ASSERT_BH(bh,
2398 (atomic_read(&bh->b_count) > 0) ||
2399 (bh->b_page && bh->b_page->mapping));
2400
2401 if (!new_jh) {
2402 jbd_unlock_bh_journal_head(bh);
2403 goto repeat;
2404 }
2405
2406 jh = new_jh;
2407 new_jh = NULL; /* We consumed it */
2408 set_buffer_jbd(bh);
2409 bh->b_private = jh;
2410 jh->b_bh = bh;
2411 get_bh(bh);
2412 BUFFER_TRACE(bh, "added journal_head");
2413 }
2414 jh->b_jcount++;
2415 jbd_unlock_bh_journal_head(bh);
2416 if (new_jh)
2417 journal_free_journal_head(new_jh);
2418 return bh->b_private;
2419}
2420
2421/*
2422 * Grab a ref against this buffer_head's journal_head. If it ended up not
2423 * having a journal_head, return NULL
2424 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002425struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002426{
2427 struct journal_head *jh = NULL;
2428
2429 jbd_lock_bh_journal_head(bh);
2430 if (buffer_jbd(bh)) {
2431 jh = bh2jh(bh);
2432 jh->b_jcount++;
2433 }
2434 jbd_unlock_bh_journal_head(bh);
2435 return jh;
2436}
2437
2438static void __journal_remove_journal_head(struct buffer_head *bh)
2439{
2440 struct journal_head *jh = bh2jh(bh);
2441
2442 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002443 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2444 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2445 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2446 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2447 J_ASSERT_BH(bh, buffer_jbd(bh));
2448 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2449 BUFFER_TRACE(bh, "remove journal_head");
2450 if (jh->b_frozen_data) {
2451 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2452 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002453 }
Jan Karade1b7942011-06-13 15:38:22 -04002454 if (jh->b_committed_data) {
2455 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2456 jbd2_free(jh->b_committed_data, bh->b_size);
2457 }
2458 bh->b_private = NULL;
2459 jh->b_bh = NULL; /* debug, really */
2460 clear_buffer_jbd(bh);
2461 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002462}
2463
2464/*
Jan Karade1b7942011-06-13 15:38:22 -04002465 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002466 * release the journal_head from the buffer_head.
2467 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002468void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002469{
2470 struct buffer_head *bh = jh2bh(jh);
2471
2472 jbd_lock_bh_journal_head(bh);
2473 J_ASSERT_JH(jh, jh->b_jcount > 0);
2474 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002475 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002476 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002477 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002478 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002479 } else
2480 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002481}
2482
2483/*
Jan Karac851ed52008-07-11 19:27:31 -04002484 * Initialize jbd inode head
2485 */
2486void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2487{
2488 jinode->i_transaction = NULL;
2489 jinode->i_next_transaction = NULL;
2490 jinode->i_vfs_inode = inode;
2491 jinode->i_flags = 0;
2492 INIT_LIST_HEAD(&jinode->i_list);
2493}
2494
2495/*
2496 * Function to be called before we start removing inode from memory (i.e.,
2497 * clear_inode() is a fine place to be called from). It removes inode from
2498 * transaction's lists.
2499 */
2500void jbd2_journal_release_jbd_inode(journal_t *journal,
2501 struct jbd2_inode *jinode)
2502{
Jan Karac851ed52008-07-11 19:27:31 -04002503 if (!journal)
2504 return;
2505restart:
2506 spin_lock(&journal->j_list_lock);
2507 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002508 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002509 wait_queue_head_t *wq;
2510 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2511 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2512 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2513 spin_unlock(&journal->j_list_lock);
2514 schedule();
2515 finish_wait(wq, &wait.wait);
2516 goto restart;
2517 }
2518
Jan Karac851ed52008-07-11 19:27:31 -04002519 if (jinode->i_transaction) {
2520 list_del(&jinode->i_list);
2521 jinode->i_transaction = NULL;
2522 }
2523 spin_unlock(&journal->j_list_lock);
2524}
2525
Dave Kleikamp470decc2006-10-11 01:20:57 -07002526
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002527#ifdef CONFIG_PROC_FS
2528
2529#define JBD2_STATS_PROC_NAME "fs/jbd2"
2530
2531static void __init jbd2_create_jbd_stats_proc_entry(void)
2532{
2533 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2534}
2535
2536static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2537{
2538 if (proc_jbd2_stats)
2539 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2540}
2541
2542#else
2543
2544#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2545#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2546
2547#endif
2548
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002549struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002550
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002551static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002552{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002553 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002554 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002555 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2556 return -ENOMEM;
2557 }
2558 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2559 if (jbd2_inode_cache == NULL) {
2560 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2561 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002562 return -ENOMEM;
2563 }
2564 return 0;
2565}
2566
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002567static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002568{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002569 if (jbd2_handle_cache)
2570 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002571 if (jbd2_inode_cache)
2572 kmem_cache_destroy(jbd2_inode_cache);
2573
Dave Kleikamp470decc2006-10-11 01:20:57 -07002574}
2575
2576/*
2577 * Module startup and shutdown
2578 */
2579
2580static int __init journal_init_caches(void)
2581{
2582 int ret;
2583
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002584 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002585 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002586 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002587 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002588 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002589 if (ret == 0)
2590 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002591 return ret;
2592}
2593
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002594static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002595{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002596 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002597 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002598 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002599 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002600 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002601}
2602
2603static int __init journal_init(void)
2604{
2605 int ret;
2606
2607 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2608
2609 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002610 if (ret == 0) {
Duane Griffin620de4e2008-04-29 22:02:47 -04002611 jbd2_create_jbd_stats_proc_entry();
2612 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002613 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002614 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002615 return ret;
2616}
2617
2618static void __exit journal_exit(void)
2619{
Jose R. Santose23291b2007-07-18 08:57:06 -04002620#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002621 int n = atomic_read(&nr_journal_heads);
2622 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002623 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002624#endif
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002625 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002626 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002627}
2628
Dave Kleikamp470decc2006-10-11 01:20:57 -07002629MODULE_LICENSE("GPL");
2630module_init(journal_init);
2631module_exit(journal_exit);
2632