blob: 886ec2faa9b419a28f8f657dec3a56ccc26a07f8 [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
313 * that data for IO. If we end up using the existing buffer_head's data
314 * for the write, then we *have* to lock the buffer to prevent anyone
315 * else from using and possibly modifying it while the IO is in
316 * progress.
317 *
318 * The function returns a pointer to the buffer_heads to be used for IO.
319 *
320 * We assume that the journal has already been locked in this function.
321 *
322 * Return value:
323 * <0: Error
324 * >=0: Finished OK
325 *
326 * On success:
327 * Bit 0 set == escape performed on the data
328 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
329 */
330
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700331int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700332 struct journal_head *jh_in,
333 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700334 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700335{
336 int need_copy_out = 0;
337 int done_copy_out = 0;
338 int do_escape = 0;
339 char *mapped_data;
340 struct buffer_head *new_bh;
341 struct journal_head *new_jh;
342 struct page *new_page;
343 unsigned int new_offset;
344 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400345 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700346
347 /*
348 * The buffer really shouldn't be locked: only the current committing
349 * transaction is allowed to write it, so nobody else is allowed
350 * to do any IO.
351 *
352 * akpm: except if we're journalling data, and write() output is
353 * also part of a shared mapping, and another thread has
354 * decided to launch a writepage() against this buffer.
355 */
356 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
357
Theodore Ts'o47def822010-07-27 11:56:05 -0400358retry_alloc:
359 new_bh = alloc_buffer_head(GFP_NOFS);
360 if (!new_bh) {
361 /*
362 * Failure is not an option, but __GFP_NOFAIL is going
363 * away; so we retry ourselves here.
364 */
365 congestion_wait(BLK_RW_ASYNC, HZ/50);
366 goto retry_alloc;
367 }
368
dingdinghua96577c42009-07-13 17:55:35 -0400369 /* keep subsequent assertions sane */
370 new_bh->b_state = 0;
371 init_buffer(new_bh, NULL, NULL);
372 atomic_set(&new_bh->b_count, 1);
373 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700374
375 /*
376 * If a new transaction has already done a buffer copy-out, then
377 * we use that version of the data for the commit.
378 */
379 jbd_lock_bh_state(bh_in);
380repeat:
381 if (jh_in->b_frozen_data) {
382 done_copy_out = 1;
383 new_page = virt_to_page(jh_in->b_frozen_data);
384 new_offset = offset_in_page(jh_in->b_frozen_data);
385 } else {
386 new_page = jh2bh(jh_in)->b_page;
387 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
388 }
389
Cong Wang303a8f22011-11-25 23:14:31 +0800390 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700391 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200392 * Fire data frozen trigger if data already wasn't frozen. Do this
393 * before checking for escaping, as the trigger may modify the magic
394 * offset. If a copy-out happens afterwards, it will have the correct
395 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700396 */
Jan Kara13ceef02010-07-14 07:56:33 +0200397 if (!done_copy_out)
398 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
399 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700400
401 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700402 * Check for escaping
403 */
404 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700405 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700406 need_copy_out = 1;
407 do_escape = 1;
408 }
Cong Wang303a8f22011-11-25 23:14:31 +0800409 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700410
411 /*
412 * Do we need to do a data copy?
413 */
414 if (need_copy_out && !done_copy_out) {
415 char *tmp;
416
417 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400418 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500419 if (!tmp) {
420 jbd2_journal_put_journal_head(new_jh);
421 return -ENOMEM;
422 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700423 jbd_lock_bh_state(bh_in);
424 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400425 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700426 goto repeat;
427 }
428
429 jh_in->b_frozen_data = tmp;
Cong Wang303a8f22011-11-25 23:14:31 +0800430 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700431 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800432 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433
434 new_page = virt_to_page(tmp);
435 new_offset = offset_in_page(tmp);
436 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700437
438 /*
439 * This isn't strictly necessary, as we're using frozen
440 * data for the escaping, but it keeps consistency with
441 * b_frozen_data usage.
442 */
443 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700444 }
445
446 /*
447 * Did we need to do an escaping? Now we've done all the
448 * copying, we can finally do so.
449 */
450 if (do_escape) {
Cong Wang303a8f22011-11-25 23:14:31 +0800451 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700452 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang303a8f22011-11-25 23:14:31 +0800453 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700454 }
455
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456 set_bh_page(new_bh, new_page, new_offset);
457 new_jh->b_transaction = NULL;
458 new_bh->b_size = jh2bh(jh_in)->b_size;
459 new_bh->b_bdev = transaction->t_journal->j_dev;
460 new_bh->b_blocknr = blocknr;
461 set_buffer_mapped(new_bh);
462 set_buffer_dirty(new_bh);
463
464 *jh_out = new_jh;
465
466 /*
467 * The to-be-written buffer needs to get moved to the io queue,
468 * and the original buffer whose contents we are shadowing or
469 * copying is moved to the transaction's shadow queue.
470 */
471 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400472 spin_lock(&journal->j_list_lock);
473 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
474 spin_unlock(&journal->j_list_lock);
475 jbd_unlock_bh_state(bh_in);
476
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700478 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700479
480 return do_escape | (done_copy_out << 1);
481}
482
483/*
484 * Allocation code for the journal file. Manage the space left in the
485 * journal, so that we can begin checkpointing when appropriate.
486 */
487
488/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700489 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700490 *
491 * Called with the journal already locked.
492 *
493 * Called under j_state_lock
494 */
495
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700496int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700497{
498 int left = journal->j_free;
499
Theodore Ts'oa931da62010-08-03 21:35:12 -0400500 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700501
502 /*
503 * Be pessimistic here about the number of those free blocks which
504 * might be required for log descriptor control blocks.
505 */
506
507#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
508
509 left -= MIN_LOG_RESERVED_BLOCKS;
510
511 if (left <= 0)
512 return 0;
513 left -= (left >> 3);
514 return left;
515}
516
517/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500518 * Called with j_state_lock locked for writing.
519 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700521int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700522{
Eric Sandeene7b04ac2013-01-30 00:39:28 -0500523 /* Return if the txn has already requested to be committed */
524 if (journal->j_commit_request == target)
525 return 0;
526
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400528 * The only transaction we can possibly wait upon is the
529 * currently running transaction (if it exists). Otherwise,
530 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400532 if (journal->j_running_transaction &&
533 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700534 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200535 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 * commit thread. We do _not_ do the commit ourselves.
537 */
538
539 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400540 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700541 journal->j_commit_request,
542 journal->j_commit_sequence);
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500543 journal->j_running_transaction->t_requested = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 wake_up(&journal->j_wait_commit);
545 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400546 } else if (!tid_geq(journal->j_commit_request, target))
547 /* This should never happen, but if it does, preserve
548 the evidence before kjournald goes into a loop and
549 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400550 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400551 journal->j_commit_request,
552 journal->j_commit_sequence,
553 target, journal->j_running_transaction ?
554 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 return 0;
556}
557
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700558int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559{
560 int ret;
561
Theodore Ts'oa931da62010-08-03 21:35:12 -0400562 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700563 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400564 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700565 return ret;
566}
567
568/*
569 * Force and wait upon a commit if the calling process is not within
570 * transaction. This is used for forcing out undo-protected data which contains
571 * bitmaps, when the fs is running out of space.
572 *
573 * We can only force the running transaction if we don't have an active handle;
574 * otherwise, we will deadlock.
575 *
576 * Returns true if a transaction was started.
577 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700578int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700579{
580 transaction_t *transaction = NULL;
581 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500582 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583
Theodore Ts'oa931da62010-08-03 21:35:12 -0400584 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700585 if (journal->j_running_transaction && !current->journal_info) {
586 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500587 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
588 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589 } else if (journal->j_committing_transaction)
590 transaction = journal->j_committing_transaction;
591
592 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400593 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 return 0; /* Nothing to retry */
595 }
596
597 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400598 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500599 if (need_to_start)
600 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700601 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700602 return 1;
603}
604
605/*
606 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500607 * if a transaction is going to be committed (or is currently already
608 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700609 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700610int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700611{
612 int ret = 0;
613
Theodore Ts'oa931da62010-08-03 21:35:12 -0400614 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700615 if (journal->j_running_transaction) {
616 tid_t tid = journal->j_running_transaction->t_tid;
617
Jan Karac88ccea2009-02-10 11:27:46 -0500618 __jbd2_log_start_commit(journal, tid);
619 /* There's a running transaction and we've just made sure
620 * it's commit has been scheduled. */
621 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700622 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500623 ret = 1;
624 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300626 * If commit has been started, then we have to wait for
627 * completion of that transaction.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 */
Jan Karac88ccea2009-02-10 11:27:46 -0500629 if (ptid)
630 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700631 ret = 1;
632 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400633 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634 return ret;
635}
636
637/*
Jan Karabbd2be32011-05-24 11:59:18 -0400638 * Return 1 if a given transaction has not yet sent barrier request
639 * connected with a transaction commit. If 0 is returned, transaction
640 * may or may not have sent the barrier. Used to avoid sending barrier
641 * twice in common cases.
642 */
643int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
644{
645 int ret = 0;
646 transaction_t *commit_trans;
647
648 if (!(journal->j_flags & JBD2_BARRIER))
649 return 0;
650 read_lock(&journal->j_state_lock);
651 /* Transaction already committed? */
652 if (tid_geq(journal->j_commit_sequence, tid))
653 goto out;
654 commit_trans = journal->j_committing_transaction;
655 if (!commit_trans || commit_trans->t_tid != tid) {
656 ret = 1;
657 goto out;
658 }
659 /*
660 * Transaction is being committed and we already proceeded to
661 * submitting a flush to fs partition?
662 */
663 if (journal->j_fs_dev != journal->j_dev) {
664 if (!commit_trans->t_need_data_flush ||
665 commit_trans->t_state >= T_COMMIT_DFLUSH)
666 goto out;
667 } else {
668 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
669 goto out;
670 }
671 ret = 1;
672out:
673 read_unlock(&journal->j_state_lock);
674 return ret;
675}
676EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
677
678/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700679 * Wait for a specified commit to complete.
680 * The caller may not hold the journal lock.
681 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700682int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700683{
684 int err = 0;
685
Theodore Ts'oa931da62010-08-03 21:35:12 -0400686 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400687#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700688 if (!tid_geq(journal->j_commit_request, tid)) {
689 printk(KERN_EMERG
690 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400691 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700692 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700693#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700694 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400695 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700696 tid, journal->j_commit_sequence);
697 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400698 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700699 wait_event(journal->j_wait_done_commit,
700 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400701 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700702 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400703 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700704
705 if (unlikely(is_journal_aborted(journal))) {
706 printk(KERN_EMERG "journal commit I/O error\n");
707 err = -EIO;
708 }
709 return err;
710}
711
712/*
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400713 * When this function returns the transaction corresponding to tid
714 * will be completed. If the transaction has currently running, start
715 * committing that transaction before waiting for it to complete. If
716 * the transaction id is stale, it is by definition already completed,
717 * so just return SUCCESS.
718 */
719int jbd2_complete_transaction(journal_t *journal, tid_t tid)
720{
721 int need_to_wait = 1;
722
723 read_lock(&journal->j_state_lock);
724 if (journal->j_running_transaction &&
725 journal->j_running_transaction->t_tid == tid) {
726 if (journal->j_commit_request != tid) {
727 /* transaction not yet started, so request it */
728 read_unlock(&journal->j_state_lock);
729 jbd2_log_start_commit(journal, tid);
730 goto wait_commit;
731 }
732 } else if (!(journal->j_committing_transaction &&
733 journal->j_committing_transaction->t_tid == tid))
734 need_to_wait = 0;
735 read_unlock(&journal->j_state_lock);
736 if (!need_to_wait)
737 return 0;
738wait_commit:
739 return jbd2_log_wait_commit(journal, tid);
740}
741EXPORT_SYMBOL(jbd2_complete_transaction);
742
743/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700744 * Log buffer allocation routines:
745 */
746
Mingming Cao18eba7a2006-10-11 01:21:13 -0700747int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700748{
749 unsigned long blocknr;
750
Theodore Ts'oa931da62010-08-03 21:35:12 -0400751 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700752 J_ASSERT(journal->j_free > 1);
753
754 blocknr = journal->j_head;
755 journal->j_head++;
756 journal->j_free--;
757 if (journal->j_head == journal->j_last)
758 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400759 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700760 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700761}
762
763/*
764 * Conversion of logical to physical block numbers for the journal
765 *
766 * On external journals the journal blocks are identity-mapped, so
767 * this is a no-op. If needed, we can use j_blk_offset - everything is
768 * ready.
769 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700770int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700771 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700772{
773 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700774 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700775
776 if (journal->j_inode) {
777 ret = bmap(journal->j_inode, blocknr);
778 if (ret)
779 *retp = ret;
780 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700781 printk(KERN_ALERT "%s: journal block not found "
782 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400783 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700784 err = -EIO;
785 __journal_abort_soft(journal, err);
786 }
787 } else {
788 *retp = blocknr; /* +journal->j_blk_offset */
789 }
790 return err;
791}
792
793/*
794 * We play buffer_head aliasing tricks to write data/metadata blocks to
795 * the journal without copying their contents, but for journal
796 * descriptor blocks we do need to generate bona fide buffers.
797 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700798 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700799 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
800 * But we don't bother doing that, so there will be coherency problems with
801 * mmaps of blockdevs which hold live JBD-controlled filesystems.
802 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700803struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700804{
805 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700806 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700807 int err;
808
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700809 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700810
811 if (err)
812 return NULL;
813
814 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500815 if (!bh)
816 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700817 lock_buffer(bh);
818 memset(bh->b_data, 0, journal->j_blocksize);
819 set_buffer_uptodate(bh);
820 unlock_buffer(bh);
821 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700822 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700823}
824
Jan Kara79feb522012-03-13 22:22:54 -0400825/*
826 * Return tid of the oldest transaction in the journal and block in the journal
827 * where the transaction starts.
828 *
829 * If the journal is now empty, return which will be the next transaction ID
830 * we will write and where will that transaction start.
831 *
832 * The return value is 0 if journal tail cannot be pushed any further, 1 if
833 * it can.
834 */
835int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
836 unsigned long *block)
837{
838 transaction_t *transaction;
839 int ret;
840
841 read_lock(&journal->j_state_lock);
842 spin_lock(&journal->j_list_lock);
843 transaction = journal->j_checkpoint_transactions;
844 if (transaction) {
845 *tid = transaction->t_tid;
846 *block = transaction->t_log_start;
847 } else if ((transaction = journal->j_committing_transaction) != NULL) {
848 *tid = transaction->t_tid;
849 *block = transaction->t_log_start;
850 } else if ((transaction = journal->j_running_transaction) != NULL) {
851 *tid = transaction->t_tid;
852 *block = journal->j_head;
853 } else {
854 *tid = journal->j_transaction_sequence;
855 *block = journal->j_head;
856 }
857 ret = tid_gt(*tid, journal->j_tail_sequence);
858 spin_unlock(&journal->j_list_lock);
859 read_unlock(&journal->j_state_lock);
860
861 return ret;
862}
863
864/*
865 * Update information in journal structure and in on disk journal superblock
866 * about log tail. This function does not check whether information passed in
867 * really pushes log tail further. It's responsibility of the caller to make
868 * sure provided log tail information is valid (e.g. by holding
869 * j_checkpoint_mutex all the time between computing log tail and calling this
870 * function as is the case with jbd2_cleanup_journal_tail()).
871 *
872 * Requires j_checkpoint_mutex
873 */
874void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
875{
876 unsigned long freed;
877
878 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
879
880 /*
881 * We cannot afford for write to remain in drive's caches since as
882 * soon as we update j_tail, next transaction can start reusing journal
883 * space and if we lose sb update during power failure we'd replay
884 * old transaction with possibly newly overwritten data.
885 */
886 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
887 write_lock(&journal->j_state_lock);
888 freed = block - journal->j_tail;
889 if (block < journal->j_tail)
890 freed += journal->j_last - journal->j_first;
891
892 trace_jbd2_update_log_tail(journal, tid, block, freed);
893 jbd_debug(1,
894 "Cleaning journal tail from %d to %d (offset %lu), "
895 "freeing %lu\n",
896 journal->j_tail_sequence, tid, block, freed);
897
898 journal->j_free += freed;
899 journal->j_tail_sequence = tid;
900 journal->j_tail = block;
901 write_unlock(&journal->j_state_lock);
902}
903
Jan Kara33395782012-03-13 22:45:38 -0400904/*
905 * This is a variaon of __jbd2_update_log_tail which checks for validity of
906 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
907 * with other threads updating log tail.
908 */
909void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
910{
911 mutex_lock(&journal->j_checkpoint_mutex);
912 if (tid_gt(tid, journal->j_tail_sequence))
913 __jbd2_update_log_tail(journal, tid, block);
914 mutex_unlock(&journal->j_checkpoint_mutex);
915}
916
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500917struct jbd2_stats_proc_session {
918 journal_t *journal;
919 struct transaction_stats_s *stats;
920 int start;
921 int max;
922};
923
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500924static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
925{
926 return *pos ? NULL : SEQ_START_TOKEN;
927}
928
929static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
930{
931 return NULL;
932}
933
934static int jbd2_seq_info_show(struct seq_file *seq, void *v)
935{
936 struct jbd2_stats_proc_session *s = seq->private;
937
938 if (v != SEQ_START_TOKEN)
939 return 0;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500940 seq_printf(seq, "%lu transactions (%lu requested), "
941 "each up to %u blocks\n",
942 s->stats->ts_tid, s->stats->ts_requested,
943 s->journal->j_max_transaction_buffers);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500944 if (s->stats->ts_tid == 0)
945 return 0;
946 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400947 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500948 seq_printf(seq, " %ums request delay\n",
949 (s->stats->ts_requested == 0) ? 0 :
950 jiffies_to_msecs(s->stats->run.rs_request_delay /
951 s->stats->ts_requested));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500952 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400953 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500954 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400955 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500956 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400957 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500958 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400959 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500960 seq_printf(seq, " %lluus average transaction commit time\n",
961 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500962 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400963 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500964 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400965 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500966 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400967 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500968 return 0;
969}
970
971static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
972{
973}
974
James Morris88e9d342009-09-22 16:43:43 -0700975static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500976 .start = jbd2_seq_info_start,
977 .next = jbd2_seq_info_next,
978 .stop = jbd2_seq_info_stop,
979 .show = jbd2_seq_info_show,
980};
981
982static int jbd2_seq_info_open(struct inode *inode, struct file *file)
983{
984 journal_t *journal = PDE(inode)->data;
985 struct jbd2_stats_proc_session *s;
986 int rc, size;
987
988 s = kmalloc(sizeof(*s), GFP_KERNEL);
989 if (s == NULL)
990 return -ENOMEM;
991 size = sizeof(struct transaction_stats_s);
992 s->stats = kmalloc(size, GFP_KERNEL);
993 if (s->stats == NULL) {
994 kfree(s);
995 return -ENOMEM;
996 }
997 spin_lock(&journal->j_history_lock);
998 memcpy(s->stats, &journal->j_stats, size);
999 s->journal = journal;
1000 spin_unlock(&journal->j_history_lock);
1001
1002 rc = seq_open(file, &jbd2_seq_info_ops);
1003 if (rc == 0) {
1004 struct seq_file *m = file->private_data;
1005 m->private = s;
1006 } else {
1007 kfree(s->stats);
1008 kfree(s);
1009 }
1010 return rc;
1011
1012}
1013
1014static int jbd2_seq_info_release(struct inode *inode, struct file *file)
1015{
1016 struct seq_file *seq = file->private_data;
1017 struct jbd2_stats_proc_session *s = seq->private;
1018 kfree(s->stats);
1019 kfree(s);
1020 return seq_release(inode, file);
1021}
1022
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001023static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001024 .owner = THIS_MODULE,
1025 .open = jbd2_seq_info_open,
1026 .read = seq_read,
1027 .llseek = seq_lseek,
1028 .release = jbd2_seq_info_release,
1029};
1030
1031static struct proc_dir_entry *proc_jbd2_stats;
1032
1033static void jbd2_stats_proc_init(journal_t *journal)
1034{
Theodore Ts'o05496762008-09-16 14:36:17 -04001035 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001036 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -07001037 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1038 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001039 }
1040}
1041
1042static void jbd2_stats_proc_exit(journal_t *journal)
1043{
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001044 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -04001045 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001046}
1047
Dave Kleikamp470decc2006-10-11 01:20:57 -07001048/*
1049 * Management for journal control blocks: functions to create and
1050 * destroy journal_t structures, and to initialise and read existing
1051 * journal blocks from disk. */
1052
1053/* First: create and setup a journal_t object in memory. We initialise
1054 * very few fields yet: that has to wait until we have created the
1055 * journal structures from from scratch, or loaded them from disk. */
1056
1057static journal_t * journal_init_common (void)
1058{
1059 journal_t *journal;
1060 int err;
1061
Andrew Morton3ebfdf82009-12-23 08:05:15 -05001062 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001064 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065
1066 init_waitqueue_head(&journal->j_wait_transaction_locked);
1067 init_waitqueue_head(&journal->j_wait_logspace);
1068 init_waitqueue_head(&journal->j_wait_done_commit);
1069 init_waitqueue_head(&journal->j_wait_checkpoint);
1070 init_waitqueue_head(&journal->j_wait_commit);
1071 init_waitqueue_head(&journal->j_wait_updates);
1072 mutex_init(&journal->j_barrier);
1073 mutex_init(&journal->j_checkpoint_mutex);
1074 spin_lock_init(&journal->j_revoke_lock);
1075 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001076 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001077
Mingming Caocd02ff02007-10-16 18:38:25 -04001078 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001079 journal->j_min_batch_time = 0;
1080 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001081
1082 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001083 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084
1085 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001086 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001087 if (err) {
1088 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001089 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001091
Theodore Ts'obf699322009-09-30 00:32:06 -04001092 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001093
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001095}
1096
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001097/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001098 *
1099 * Create a journal structure assigned some fixed set of disk blocks to
1100 * the journal. We don't actually touch those disk blocks yet, but we
1101 * need to set up all of the mapping information to tell the journaling
1102 * system where the journal blocks are.
1103 *
1104 */
1105
1106/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001107 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001108 * @bdev: Block device on which to create the journal
1109 * @fs_dev: Device which hold journalled filesystem for this journal.
1110 * @start: Block nr Start of journal.
1111 * @len: Length of the journal in blocks.
1112 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001113 *
1114 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001116 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117 * range of blocks on an arbitrary block device.
1118 *
1119 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001120journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001122 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001123{
1124 journal_t *journal = journal_init_common();
1125 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001126 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001127 int n;
1128
1129 if (!journal)
1130 return NULL;
1131
1132 /* journal descriptor can store up to n blocks -bzzz */
1133 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001134 journal->j_dev = bdev;
1135 journal->j_fs_dev = fs_dev;
1136 journal->j_blk_offset = start;
1137 journal->j_maxlen = len;
1138 bdevname(journal->j_dev, journal->j_devname);
1139 p = journal->j_devname;
1140 while ((p = strchr(p, '/')))
1141 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001142 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1144 journal->j_wbufsize = n;
1145 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1146 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001147 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001148 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001149 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001150 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151
1152 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001153 if (!bh) {
1154 printk(KERN_ERR
1155 "%s: Cannot get buffer for journal superblock\n",
1156 __func__);
1157 goto out_err;
1158 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001159 journal->j_sb_buffer = bh;
1160 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001161
Dave Kleikamp470decc2006-10-11 01:20:57 -07001162 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001163out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001164 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001165 jbd2_stats_proc_exit(journal);
1166 kfree(journal);
1167 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001168}
1169
1170/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001171 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001172 * @inode: An inode to create the journal in
1173 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001174 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001175 * the journal. The inode must exist already, must support bmap() and
1176 * must have all data blocks preallocated.
1177 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001178journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001179{
1180 struct buffer_head *bh;
1181 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001182 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001183 int err;
1184 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001185 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001186
1187 if (!journal)
1188 return NULL;
1189
1190 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1191 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001192 bdevname(journal->j_dev, journal->j_devname);
1193 p = journal->j_devname;
1194 while ((p = strchr(p, '/')))
1195 *p = '!';
1196 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001197 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001198 jbd_debug(1,
1199 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1200 journal, inode->i_sb->s_id, inode->i_ino,
1201 (long long) inode->i_size,
1202 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1203
1204 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1205 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001206 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001207
1208 /* journal descriptor can store up to n blocks -bzzz */
1209 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1210 journal->j_wbufsize = n;
1211 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1212 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001213 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\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
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001218 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001219 /* If that failed, give up */
1220 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001221 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001222 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001223 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001224 }
1225
1226 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001227 if (!bh) {
1228 printk(KERN_ERR
1229 "%s: Cannot get buffer for journal superblock\n",
1230 __func__);
1231 goto out_err;
1232 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001233 journal->j_sb_buffer = bh;
1234 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1235
1236 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001237out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001238 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001239 jbd2_stats_proc_exit(journal);
1240 kfree(journal);
1241 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001242}
1243
1244/*
1245 * If the journal init or create aborts, we need to mark the journal
1246 * superblock as being NULL to prevent the journal destroy from writing
1247 * back a bogus superblock.
1248 */
1249static void journal_fail_superblock (journal_t *journal)
1250{
1251 struct buffer_head *bh = journal->j_sb_buffer;
1252 brelse(bh);
1253 journal->j_sb_buffer = NULL;
1254}
1255
1256/*
1257 * Given a journal_t structure, initialise the various fields for
1258 * startup of a new journaling session. We use this both when creating
1259 * a journal, and after recovering an old journal to reset it for
1260 * subsequent use.
1261 */
1262
1263static int journal_reset(journal_t *journal)
1264{
1265 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001266 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001267
1268 first = be32_to_cpu(sb->s_first);
1269 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001270 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001271 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001272 first, last);
1273 journal_fail_superblock(journal);
1274 return -EINVAL;
1275 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001276
1277 journal->j_first = first;
1278 journal->j_last = last;
1279
1280 journal->j_head = first;
1281 journal->j_tail = first;
1282 journal->j_free = last - first;
1283
1284 journal->j_tail_sequence = journal->j_transaction_sequence;
1285 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1286 journal->j_commit_request = journal->j_commit_sequence;
1287
1288 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1289
Dave Kleikamp470decc2006-10-11 01:20:57 -07001290 /*
1291 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001292 * no recovery (s_start == 0), then we can safely defer the superblock
1293 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 * attempting a write to a potential-readonly device.
1295 */
Jan Kara24bcc892012-03-13 15:41:04 -04001296 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001297 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001298 "(start %ld, seq %d, errno %d)\n",
1299 journal->j_tail, journal->j_tail_sequence,
1300 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001301 journal->j_flags |= JBD2_FLUSHED;
1302 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001303 /* Lock here to make assertions happy... */
1304 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001305 /*
1306 * Update log tail information. We use WRITE_FUA since new
1307 * transaction will start reusing journal space and so we
1308 * must make sure information about current log tail is on
1309 * disk before that.
1310 */
1311 jbd2_journal_update_sb_log_tail(journal,
1312 journal->j_tail_sequence,
1313 journal->j_tail,
1314 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001315 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001316 }
Jan Kara24bcc892012-03-13 15:41:04 -04001317 return jbd2_journal_start_thread(journal);
1318}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001319
Jan Kara79feb522012-03-13 22:22:54 -04001320static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001321{
1322 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001323 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001324
Jan Kara79feb522012-03-13 22:22:54 -04001325 trace_jbd2_write_superblock(journal, write_op);
1326 if (!(journal->j_flags & JBD2_BARRIER))
1327 write_op &= ~(REQ_FUA | REQ_FLUSH);
1328 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001329 if (buffer_write_io_error(bh)) {
1330 /*
1331 * Oh, dear. A previous attempt to write the journal
1332 * superblock failed. This could happen because the
1333 * USB device was yanked out. Or it could happen to
1334 * be a transient write error and maybe the block will
1335 * be remapped. Nothing we can do but to retry the
1336 * write and hope for the best.
1337 */
1338 printk(KERN_ERR "JBD2: previous I/O error detected "
1339 "for journal superblock update for %s.\n",
1340 journal->j_devname);
1341 clear_buffer_write_io_error(bh);
1342 set_buffer_uptodate(bh);
1343 }
Jan Kara79feb522012-03-13 22:22:54 -04001344 get_bh(bh);
1345 bh->b_end_io = end_buffer_write_sync;
1346 ret = submit_bh(write_op, bh);
1347 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001348 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001349 clear_buffer_write_io_error(bh);
1350 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001351 ret = -EIO;
1352 }
1353 if (ret) {
1354 printk(KERN_ERR "JBD2: Error %d detected when updating "
1355 "journal superblock for %s.\n", ret,
1356 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001357 }
1358}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001359
Jan Kara24bcc892012-03-13 15:41:04 -04001360/**
1361 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1362 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001363 * @tail_tid: TID of the new transaction at the tail of the log
1364 * @tail_block: The first block of the transaction at the tail of the log
1365 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001366 *
1367 * Update a journal's superblock information about log tail and write it to
1368 * disk, waiting for the IO to complete.
1369 */
Jan Kara79feb522012-03-13 22:22:54 -04001370void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1371 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001372{
1373 journal_superblock_t *sb = journal->j_superblock;
1374
Jan Karaa78bb112012-03-13 15:43:04 -04001375 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001376 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1377 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001378
Jan Kara79feb522012-03-13 22:22:54 -04001379 sb->s_sequence = cpu_to_be32(tail_tid);
1380 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001381
Jan Kara79feb522012-03-13 22:22:54 -04001382 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001383
1384 /* Log is no longer empty */
1385 write_lock(&journal->j_state_lock);
1386 WARN_ON(!sb->s_sequence);
1387 journal->j_flags &= ~JBD2_FLUSHED;
1388 write_unlock(&journal->j_state_lock);
1389}
1390
1391/**
1392 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1393 * @journal: The journal to update.
1394 *
1395 * Update a journal's dynamic superblock fields to show that journal is empty.
1396 * Write updated superblock to disk waiting for IO to complete.
1397 */
1398static void jbd2_mark_journal_empty(journal_t *journal)
1399{
1400 journal_superblock_t *sb = journal->j_superblock;
1401
Jan Karaa78bb112012-03-13 15:43:04 -04001402 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 read_lock(&journal->j_state_lock);
Eric Sandeeneeecef02012-08-18 22:29:40 -04001404 /* Is it already empty? */
1405 if (sb->s_start == 0) {
1406 read_unlock(&journal->j_state_lock);
1407 return;
1408 }
Jan Kara24bcc892012-03-13 15:41:04 -04001409 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1410 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411
1412 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001413 sb->s_start = cpu_to_be32(0);
1414 read_unlock(&journal->j_state_lock);
1415
Jan Kara79feb522012-03-13 22:22:54 -04001416 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001417
1418 /* Log is no longer empty */
1419 write_lock(&journal->j_state_lock);
1420 journal->j_flags |= JBD2_FLUSHED;
1421 write_unlock(&journal->j_state_lock);
1422}
1423
1424
1425/**
1426 * jbd2_journal_update_sb_errno() - Update error in the journal.
1427 * @journal: The journal to update.
1428 *
1429 * Update a journal's errno. Write updated superblock to disk waiting for IO
1430 * to complete.
1431 */
Theodore Ts'od796c522012-08-05 19:04:57 -04001432void jbd2_journal_update_sb_errno(journal_t *journal)
Jan Kara24bcc892012-03-13 15:41:04 -04001433{
1434 journal_superblock_t *sb = journal->j_superblock;
1435
1436 read_lock(&journal->j_state_lock);
1437 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1438 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001439 sb->s_errno = cpu_to_be32(journal->j_errno);
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001440 jbd2_superblock_csum_set(journal, sb);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001441 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001442
Jan Kara79feb522012-03-13 22:22:54 -04001443 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001444}
Theodore Ts'od796c522012-08-05 19:04:57 -04001445EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001446
1447/*
1448 * Read the superblock for a given journal, performing initial
1449 * validation of the format.
1450 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001451static int journal_get_superblock(journal_t *journal)
1452{
1453 struct buffer_head *bh;
1454 journal_superblock_t *sb;
1455 int err = -EIO;
1456
1457 bh = journal->j_sb_buffer;
1458
1459 J_ASSERT(bh != NULL);
1460 if (!buffer_uptodate(bh)) {
1461 ll_rw_block(READ, 1, &bh);
1462 wait_on_buffer(bh);
1463 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001464 printk(KERN_ERR
1465 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001466 goto out;
1467 }
1468 }
1469
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001470 if (buffer_verified(bh))
1471 return 0;
1472
Dave Kleikamp470decc2006-10-11 01:20:57 -07001473 sb = journal->j_superblock;
1474
1475 err = -EINVAL;
1476
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001477 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001478 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001479 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001480 goto out;
1481 }
1482
1483 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001484 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485 journal->j_format_version = 1;
1486 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001487 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001488 journal->j_format_version = 2;
1489 break;
1490 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001491 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001492 goto out;
1493 }
1494
1495 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1496 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1497 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001498 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001499 goto out;
1500 }
1501
Eryu Guan87622022011-11-01 19:04:59 -04001502 if (be32_to_cpu(sb->s_first) == 0 ||
1503 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1504 printk(KERN_WARNING
1505 "JBD2: Invalid start block of journal: %u\n",
1506 be32_to_cpu(sb->s_first));
1507 goto out;
1508 }
1509
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001510 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1511 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1512 /* Can't have checksum v1 and v2 on at the same time! */
1513 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1514 "at the same time!\n");
1515 goto out;
1516 }
1517
1518 if (!jbd2_verify_csum_type(journal, sb)) {
1519 printk(KERN_ERR "JBD: Unknown checksum type\n");
1520 goto out;
1521 }
1522
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001523 /* Load the checksum driver */
1524 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1525 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1526 if (IS_ERR(journal->j_chksum_driver)) {
1527 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1528 err = PTR_ERR(journal->j_chksum_driver);
1529 journal->j_chksum_driver = NULL;
1530 goto out;
1531 }
1532 }
1533
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001534 /* Check superblock checksum */
1535 if (!jbd2_superblock_csum_verify(journal, sb)) {
1536 printk(KERN_ERR "JBD: journal checksum error\n");
1537 goto out;
1538 }
1539
1540 /* Precompute checksum seed for all metadata */
1541 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1542 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1543 sizeof(sb->s_uuid));
1544
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001545 set_buffer_verified(bh);
1546
Dave Kleikamp470decc2006-10-11 01:20:57 -07001547 return 0;
1548
1549out:
1550 journal_fail_superblock(journal);
1551 return err;
1552}
1553
1554/*
1555 * Load the on-disk journal superblock and read the key fields into the
1556 * journal_t.
1557 */
1558
1559static int load_superblock(journal_t *journal)
1560{
1561 int err;
1562 journal_superblock_t *sb;
1563
1564 err = journal_get_superblock(journal);
1565 if (err)
1566 return err;
1567
1568 sb = journal->j_superblock;
1569
1570 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1571 journal->j_tail = be32_to_cpu(sb->s_start);
1572 journal->j_first = be32_to_cpu(sb->s_first);
1573 journal->j_last = be32_to_cpu(sb->s_maxlen);
1574 journal->j_errno = be32_to_cpu(sb->s_errno);
1575
1576 return 0;
1577}
1578
1579
1580/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001581 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001582 * @journal: Journal to act on.
1583 *
1584 * Given a journal_t structure which tells us which disk blocks contain
1585 * a journal, read the journal from disk to initialise the in-memory
1586 * structures.
1587 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001588int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001589{
1590 int err;
1591 journal_superblock_t *sb;
1592
1593 err = load_superblock(journal);
1594 if (err)
1595 return err;
1596
1597 sb = journal->j_superblock;
1598 /* If this is a V2 superblock, then we have to check the
1599 * features flags on it. */
1600
1601 if (journal->j_format_version >= 2) {
1602 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001603 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001604 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001605 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001606 printk(KERN_WARNING
1607 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001608 return -EINVAL;
1609 }
1610 }
1611
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001612 /*
1613 * Create a slab for this blocksize
1614 */
1615 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1616 if (err)
1617 return err;
1618
Dave Kleikamp470decc2006-10-11 01:20:57 -07001619 /* Let the recovery code check whether it needs to recover any
1620 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001621 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001622 goto recovery_error;
1623
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001624 if (journal->j_failed_commit) {
1625 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1626 "is corrupt.\n", journal->j_failed_commit,
1627 journal->j_devname);
1628 return -EIO;
1629 }
1630
Dave Kleikamp470decc2006-10-11 01:20:57 -07001631 /* OK, we've finished with the dynamic journal bits:
1632 * reinitialise the dynamic contents of the superblock in memory
1633 * and reset them on disk. */
1634 if (journal_reset(journal))
1635 goto recovery_error;
1636
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001637 journal->j_flags &= ~JBD2_ABORT;
1638 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 return 0;
1640
1641recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001642 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001643 return -EIO;
1644}
1645
1646/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001647 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648 * @journal: Journal to act on.
1649 *
1650 * Release a journal_t structure once it is no longer in use by the
1651 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001652 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001654int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001655{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001656 int err = 0;
1657
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 /* Wait for the commit thread to wake up and die. */
1659 journal_kill_thread(journal);
1660
1661 /* Force a final log commit */
1662 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664
1665 /* Force any old transactions to disk */
1666
1667 /* Totally anal locking here... */
1668 spin_lock(&journal->j_list_lock);
1669 while (journal->j_checkpoint_transactions != NULL) {
1670 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001671 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001672 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001673 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 spin_lock(&journal->j_list_lock);
1675 }
1676
1677 J_ASSERT(journal->j_running_transaction == NULL);
1678 J_ASSERT(journal->j_committing_transaction == NULL);
1679 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1680 spin_unlock(&journal->j_list_lock);
1681
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001683 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001684 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001685 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001686 mutex_unlock(&journal->j_checkpoint_mutex);
1687 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001688 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001689 brelse(journal->j_sb_buffer);
1690 }
1691
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001692 if (journal->j_proc_entry)
1693 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694 if (journal->j_inode)
1695 iput(journal->j_inode);
1696 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001697 jbd2_journal_destroy_revoke(journal);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001698 if (journal->j_chksum_driver)
1699 crypto_free_shash(journal->j_chksum_driver);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001700 kfree(journal->j_wbuf);
1701 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001702
1703 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704}
1705
1706
1707/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001708 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 * @journal: Journal to check.
1710 * @compat: bitmask of compatible features
1711 * @ro: bitmask of features that force read-only mount
1712 * @incompat: bitmask of incompatible features
1713 *
1714 * Check whether the journal uses all of a given set of
1715 * features. Return true (non-zero) if it does.
1716 **/
1717
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001718int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001719 unsigned long ro, unsigned long incompat)
1720{
1721 journal_superblock_t *sb;
1722
1723 if (!compat && !ro && !incompat)
1724 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001725 /* Load journal superblock if it is not loaded yet. */
1726 if (journal->j_format_version == 0 &&
1727 journal_get_superblock(journal) != 0)
1728 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729 if (journal->j_format_version == 1)
1730 return 0;
1731
1732 sb = journal->j_superblock;
1733
1734 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1735 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1736 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1737 return 1;
1738
1739 return 0;
1740}
1741
1742/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001743 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001744 * @journal: Journal to check.
1745 * @compat: bitmask of compatible features
1746 * @ro: bitmask of features that force read-only mount
1747 * @incompat: bitmask of incompatible features
1748 *
1749 * Check whether the journaling code supports the use of
1750 * all of a given set of features on this journal. Return true
1751 * (non-zero) if it can. */
1752
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001753int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001754 unsigned long ro, unsigned long incompat)
1755{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001756 if (!compat && !ro && !incompat)
1757 return 1;
1758
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759 /* We can support any known requested features iff the
1760 * superblock is in version 2. Otherwise we fail to support any
1761 * extended sb features. */
1762
1763 if (journal->j_format_version != 2)
1764 return 0;
1765
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001766 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1767 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1768 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001769 return 1;
1770
1771 return 0;
1772}
1773
1774/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001775 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001776 * @journal: Journal to act on.
1777 * @compat: bitmask of compatible features
1778 * @ro: bitmask of features that force read-only mount
1779 * @incompat: bitmask of incompatible features
1780 *
1781 * Mark a given journal feature as present on the
1782 * superblock. Returns true if the requested features could be set.
1783 *
1784 */
1785
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001786int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787 unsigned long ro, unsigned long incompat)
1788{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001789#define INCOMPAT_FEATURE_ON(f) \
1790 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1791#define COMPAT_FEATURE_ON(f) \
1792 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 journal_superblock_t *sb;
1794
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001795 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001796 return 1;
1797
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001798 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001799 return 0;
1800
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001801 /* Asking for checksumming v2 and v1? Only give them v2. */
1802 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1803 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1804 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1805
Dave Kleikamp470decc2006-10-11 01:20:57 -07001806 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1807 compat, ro, incompat);
1808
1809 sb = journal->j_superblock;
1810
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001811 /* If enabling v2 checksums, update superblock */
1812 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1813 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1814 sb->s_feature_compat &=
1815 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001816
1817 /* Load the checksum driver */
1818 if (journal->j_chksum_driver == NULL) {
1819 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1820 0, 0);
1821 if (IS_ERR(journal->j_chksum_driver)) {
1822 printk(KERN_ERR "JBD: Cannot load crc32c "
1823 "driver.\n");
1824 journal->j_chksum_driver = NULL;
1825 return 0;
1826 }
1827 }
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001828
1829 /* Precompute checksum seed for all metadata */
1830 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1831 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1832 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1833 sb->s_uuid,
1834 sizeof(sb->s_uuid));
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001835 }
1836
1837 /* If enabling v1 checksums, downgrade superblock */
1838 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1839 sb->s_feature_incompat &=
1840 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1841
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 sb->s_feature_compat |= cpu_to_be32(compat);
1843 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1844 sb->s_feature_incompat |= cpu_to_be32(incompat);
1845
1846 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001847#undef COMPAT_FEATURE_ON
1848#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849}
1850
Girish Shilamkar818d2762008-01-28 23:58:27 -05001851/*
1852 * jbd2_journal_clear_features () - Clear a given journal feature in the
1853 * superblock
1854 * @journal: Journal to act on.
1855 * @compat: bitmask of compatible features
1856 * @ro: bitmask of features that force read-only mount
1857 * @incompat: bitmask of incompatible features
1858 *
1859 * Clear a given journal feature as present on the
1860 * superblock.
1861 */
1862void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1863 unsigned long ro, unsigned long incompat)
1864{
1865 journal_superblock_t *sb;
1866
1867 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1868 compat, ro, incompat);
1869
1870 sb = journal->j_superblock;
1871
1872 sb->s_feature_compat &= ~cpu_to_be32(compat);
1873 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1874 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1875}
1876EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001877
1878/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 * @journal: Journal to act on.
1881 *
1882 * Flush all data for a given journal to disk and empty the journal.
1883 * Filesystems can use this when remounting readonly to ensure that
1884 * recovery does not need to happen on remount.
1885 */
1886
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001887int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001888{
1889 int err = 0;
1890 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001891
Theodore Ts'oa931da62010-08-03 21:35:12 -04001892 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893
1894 /* Force everything buffered to the log... */
1895 if (journal->j_running_transaction) {
1896 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001897 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001898 } else if (journal->j_committing_transaction)
1899 transaction = journal->j_committing_transaction;
1900
1901 /* Wait for the log commit to complete... */
1902 if (transaction) {
1903 tid_t tid = transaction->t_tid;
1904
Theodore Ts'oa931da62010-08-03 21:35:12 -04001905 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001906 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001908 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909 }
1910
1911 /* ...and flush everything in the log out to disk. */
1912 spin_lock(&journal->j_list_lock);
1913 while (!err && journal->j_checkpoint_transactions != NULL) {
1914 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001915 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001916 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001917 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001918 spin_lock(&journal->j_list_lock);
1919 }
1920 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001921
1922 if (is_journal_aborted(journal))
1923 return -EIO;
1924
Jan Karaa78bb112012-03-13 15:43:04 -04001925 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001926 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001927
1928 /* Finally, mark the journal as really needing no recovery.
1929 * This sets s_start==0 in the underlying superblock, which is
1930 * the magic code for a fully-recovered superblock. Any future
1931 * commits of data to the journal will restore the current
1932 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001933 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001934 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001935 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936 J_ASSERT(!journal->j_running_transaction);
1937 J_ASSERT(!journal->j_committing_transaction);
1938 J_ASSERT(!journal->j_checkpoint_transactions);
1939 J_ASSERT(journal->j_head == journal->j_tail);
1940 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001941 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001942 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001943}
1944
1945/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001946 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947 * @journal: Journal to act on.
1948 * @write: flag (see below)
1949 *
1950 * Wipe out all of the contents of a journal, safely. This will produce
1951 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001952 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001953 *
1954 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1955 * we merely suppress recovery.
1956 */
1957
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001958int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001959{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960 int err = 0;
1961
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001962 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001963
1964 err = load_superblock(journal);
1965 if (err)
1966 return err;
1967
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968 if (!journal->j_tail)
1969 goto no_recovery;
1970
Eryu Guanf2a44522011-11-01 19:09:18 -04001971 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001972 write ? "Clearing" : "Ignoring");
1973
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001974 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001975 if (write) {
1976 /* Lock to make assertions happy... */
1977 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001978 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001979 mutex_unlock(&journal->j_checkpoint_mutex);
1980 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001981
1982 no_recovery:
1983 return err;
1984}
1985
1986/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987 * Journal abort has very specific semantics, which we describe
1988 * for journal abort.
1989 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001990 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991 * itself are here.
1992 */
1993
1994/*
1995 * Quick version for internal journal use (doesn't lock the journal).
1996 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1997 * and don't attempt to make any other journal updates.
1998 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001999void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000{
2001 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002002
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002003 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 return;
2005
2006 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04002007 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002008
Theodore Ts'oa931da62010-08-03 21:35:12 -04002009 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002010 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011 transaction = journal->j_running_transaction;
2012 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002013 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04002014 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002015}
2016
2017/* Soft abort: record the abort error status in the journal superblock,
2018 * but don't do any other IO. */
2019static void __journal_abort_soft (journal_t *journal, int errno)
2020{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002021 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 return;
2023
2024 if (!journal->j_errno)
2025 journal->j_errno = errno;
2026
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028
2029 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04002030 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002031}
2032
2033/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002034 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002035 * @journal: the journal to shutdown.
2036 * @errno: an error number to record in the journal indicating
2037 * the reason for the shutdown.
2038 *
2039 * Perform a complete, immediate shutdown of the ENTIRE
2040 * journal (not of a single transaction). This operation cannot be
2041 * undone without closing and reopening the journal.
2042 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002043 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2045 * mode.
2046 *
2047 * Journal abort has very specific semantics. Any existing dirty,
2048 * unjournaled buffers in the main filesystem will still be written to
2049 * disk by bdflush, but the journaling mechanism will be suspended
2050 * immediately and no further transaction commits will be honoured.
2051 *
2052 * Any dirty, journaled buffers will be written back to disk without
2053 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2054 * filesystem, but we _do_ attempt to leave as much data as possible
2055 * behind for fsck to use for cleanup.
2056 *
2057 * Any attempt to get a new transaction handle on a journal which is in
2058 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002059 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07002060 * entered abort state during the update.
2061 *
2062 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002065 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07002066 * which will be recorded (if possible) in the journal superblock. This
2067 * allows a client to record failure conditions in the middle of a
2068 * transaction without having to complete the transaction to record the
2069 * failure to disk. ext3_error, for example, now uses this
2070 * functionality.
2071 *
2072 * Errors which originate from within the journaling layer will NOT
2073 * supply an errno; a null errno implies that absolutely no further
2074 * writes are done to the journal (unless there are any already in
2075 * progress).
2076 *
2077 */
2078
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002079void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002080{
2081 __journal_abort_soft(journal, errno);
2082}
2083
2084/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002085 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002086 * @journal: journal to examine.
2087 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002088 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07002089 * time the journal was mounted - if the journal was stopped
2090 * without calling abort this will be 0.
2091 *
2092 * If the journal has been aborted on this mount time -EROFS will
2093 * be returned.
2094 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002095int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002096{
2097 int err;
2098
Theodore Ts'oa931da62010-08-03 21:35:12 -04002099 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002100 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002101 err = -EROFS;
2102 else
2103 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002104 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002105 return err;
2106}
2107
2108/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002109 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07002110 * @journal: journal to act on.
2111 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002112 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002113 * mode.
2114 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002115int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002116{
2117 int err = 0;
2118
Theodore Ts'oa931da62010-08-03 21:35:12 -04002119 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002120 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002121 err = -EROFS;
2122 else
2123 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002124 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002125 return err;
2126}
2127
2128/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002129 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002130 * @journal: journal to act on.
2131 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002132 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002133 * mode.
2134 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002135void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002136{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002137 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002138 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002139 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002140 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002141}
2142
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002143int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002144{
2145 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2146}
2147
2148/*
Zach Brownb517bea2006-10-11 01:21:08 -07002149 * helper functions to deal with 32 or 64bit block numbers.
2150 */
2151size_t journal_tag_bytes(journal_t *journal)
2152{
Darrick J. Wongc3900872012-05-27 08:12:12 -04002153 journal_block_tag_t tag;
2154 size_t x = 0;
2155
2156 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2157 x += sizeof(tag.t_checksum);
2158
Zach Brownb517bea2006-10-11 01:21:08 -07002159 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Darrick J. Wongc3900872012-05-27 08:12:12 -04002160 return x + JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002161 else
Darrick J. Wongc3900872012-05-27 08:12:12 -04002162 return x + JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002163}
2164
2165/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002166 * JBD memory management
2167 *
2168 * These functions are used to allocate block-sized chunks of memory
2169 * used for making copies of buffer_head data. Very often it will be
2170 * page-sized chunks of data, but sometimes it will be in
2171 * sub-page-size chunks. (For example, 16k pages on Power systems
2172 * with a 4k block file system.) For blocks smaller than a page, we
2173 * use a SLAB allocator. There are slab caches for each block size,
2174 * which are allocated at mount time, if necessary, and we only free
2175 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2176 * this reason we don't need to a mutex to protect access to
2177 * jbd2_slab[] allocating or releasing memory; only in
2178 * jbd2_journal_create_slab().
2179 */
2180#define JBD2_MAX_SLABS 8
2181static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002182
2183static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2184 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2185 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2186};
2187
2188
2189static void jbd2_journal_destroy_slabs(void)
2190{
2191 int i;
2192
2193 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2194 if (jbd2_slab[i])
2195 kmem_cache_destroy(jbd2_slab[i]);
2196 jbd2_slab[i] = NULL;
2197 }
2198}
2199
2200static int jbd2_journal_create_slab(size_t size)
2201{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002202 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002203 int i = order_base_2(size) - 10;
2204 size_t slab_size;
2205
2206 if (size == PAGE_SIZE)
2207 return 0;
2208
2209 if (i >= JBD2_MAX_SLABS)
2210 return -EINVAL;
2211
2212 if (unlikely(i < 0))
2213 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002214 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002215 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002216 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002217 return 0; /* Already created */
2218 }
2219
2220 slab_size = 1 << (i+10);
2221 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2222 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002223 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002224 if (!jbd2_slab[i]) {
2225 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2226 return -ENOMEM;
2227 }
2228 return 0;
2229}
2230
2231static struct kmem_cache *get_slab(size_t size)
2232{
2233 int i = order_base_2(size) - 10;
2234
2235 BUG_ON(i >= JBD2_MAX_SLABS);
2236 if (unlikely(i < 0))
2237 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002238 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002239 return jbd2_slab[i];
2240}
2241
2242void *jbd2_alloc(size_t size, gfp_t flags)
2243{
2244 void *ptr;
2245
2246 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2247
2248 flags |= __GFP_REPEAT;
2249 if (size == PAGE_SIZE)
2250 ptr = (void *)__get_free_pages(flags, 0);
2251 else if (size > PAGE_SIZE) {
2252 int order = get_order(size);
2253
2254 if (order < 3)
2255 ptr = (void *)__get_free_pages(flags, order);
2256 else
2257 ptr = vmalloc(size);
2258 } else
2259 ptr = kmem_cache_alloc(get_slab(size), flags);
2260
2261 /* Check alignment; SLUB has gotten this wrong in the past,
2262 * and this can lead to user data corruption! */
2263 BUG_ON(((unsigned long) ptr) & (size-1));
2264
2265 return ptr;
2266}
2267
2268void jbd2_free(void *ptr, size_t size)
2269{
2270 if (size == PAGE_SIZE) {
2271 free_pages((unsigned long)ptr, 0);
2272 return;
2273 }
2274 if (size > PAGE_SIZE) {
2275 int order = get_order(size);
2276
2277 if (order < 3)
2278 free_pages((unsigned long)ptr, order);
2279 else
2280 vfree(ptr);
2281 return;
2282 }
2283 kmem_cache_free(get_slab(size), ptr);
2284};
2285
2286/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002287 * Journal_head storage management
2288 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002289static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002290#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002291static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2292#endif
2293
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002294static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002295{
2296 int retval;
2297
Al Viro1076d172008-03-29 03:07:18 +00002298 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002299 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002300 sizeof(struct journal_head),
2301 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002302 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002303 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002304 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002305 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002306 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002307 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002308 }
2309 return retval;
2310}
2311
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002312static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002313{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002314 if (jbd2_journal_head_cache) {
2315 kmem_cache_destroy(jbd2_journal_head_cache);
2316 jbd2_journal_head_cache = NULL;
2317 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002318}
2319
2320/*
2321 * journal_head splicing and dicing
2322 */
2323static struct journal_head *journal_alloc_journal_head(void)
2324{
2325 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002326
Jose R. Santose23291b2007-07-18 08:57:06 -04002327#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002328 atomic_inc(&nr_journal_heads);
2329#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002330 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002331 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002332 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002333 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002334 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002335 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002336 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002337 }
2338 }
2339 return ret;
2340}
2341
2342static void journal_free_journal_head(struct journal_head *jh)
2343{
Jose R. Santose23291b2007-07-18 08:57:06 -04002344#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002345 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002346 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002347#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002348 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002349}
2350
2351/*
2352 * A journal_head is attached to a buffer_head whenever JBD has an
2353 * interest in the buffer.
2354 *
2355 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2356 * is set. This bit is tested in core kernel code where we need to take
2357 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2358 * there.
2359 *
2360 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2361 *
2362 * When a buffer has its BH_JBD bit set it is immune from being released by
2363 * core kernel code, mainly via ->b_count.
2364 *
Jan Karade1b7942011-06-13 15:38:22 -04002365 * A journal_head is detached from its buffer_head when the journal_head's
2366 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2367 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002368 *
2369 * Various places in the kernel want to attach a journal_head to a buffer_head
2370 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002371 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002372 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002373 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002374 *
2375 * So the typical usage would be:
2376 *
2377 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002378 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002379 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002380 * (Get another reference for transaction)
2381 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002382 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002383 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002384 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002385 */
2386
2387/*
2388 * Give a buffer_head a journal_head.
2389 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002390 * May sleep.
2391 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002392struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002393{
2394 struct journal_head *jh;
2395 struct journal_head *new_jh = NULL;
2396
2397repeat:
2398 if (!buffer_jbd(bh)) {
2399 new_jh = journal_alloc_journal_head();
2400 memset(new_jh, 0, sizeof(*new_jh));
2401 }
2402
2403 jbd_lock_bh_journal_head(bh);
2404 if (buffer_jbd(bh)) {
2405 jh = bh2jh(bh);
2406 } else {
2407 J_ASSERT_BH(bh,
2408 (atomic_read(&bh->b_count) > 0) ||
2409 (bh->b_page && bh->b_page->mapping));
2410
2411 if (!new_jh) {
2412 jbd_unlock_bh_journal_head(bh);
2413 goto repeat;
2414 }
2415
2416 jh = new_jh;
2417 new_jh = NULL; /* We consumed it */
2418 set_buffer_jbd(bh);
2419 bh->b_private = jh;
2420 jh->b_bh = bh;
2421 get_bh(bh);
2422 BUFFER_TRACE(bh, "added journal_head");
2423 }
2424 jh->b_jcount++;
2425 jbd_unlock_bh_journal_head(bh);
2426 if (new_jh)
2427 journal_free_journal_head(new_jh);
2428 return bh->b_private;
2429}
2430
2431/*
2432 * Grab a ref against this buffer_head's journal_head. If it ended up not
2433 * having a journal_head, return NULL
2434 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002435struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002436{
2437 struct journal_head *jh = NULL;
2438
2439 jbd_lock_bh_journal_head(bh);
2440 if (buffer_jbd(bh)) {
2441 jh = bh2jh(bh);
2442 jh->b_jcount++;
2443 }
2444 jbd_unlock_bh_journal_head(bh);
2445 return jh;
2446}
2447
2448static void __journal_remove_journal_head(struct buffer_head *bh)
2449{
2450 struct journal_head *jh = bh2jh(bh);
2451
2452 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002453 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2454 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2455 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2456 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2457 J_ASSERT_BH(bh, buffer_jbd(bh));
2458 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2459 BUFFER_TRACE(bh, "remove journal_head");
2460 if (jh->b_frozen_data) {
2461 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2462 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002463 }
Jan Karade1b7942011-06-13 15:38:22 -04002464 if (jh->b_committed_data) {
2465 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2466 jbd2_free(jh->b_committed_data, bh->b_size);
2467 }
2468 bh->b_private = NULL;
2469 jh->b_bh = NULL; /* debug, really */
2470 clear_buffer_jbd(bh);
2471 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002472}
2473
2474/*
Jan Karade1b7942011-06-13 15:38:22 -04002475 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002476 * release the journal_head from the buffer_head.
2477 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002478void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002479{
2480 struct buffer_head *bh = jh2bh(jh);
2481
2482 jbd_lock_bh_journal_head(bh);
2483 J_ASSERT_JH(jh, jh->b_jcount > 0);
2484 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002485 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002486 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002487 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002488 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002489 } else
2490 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002491}
2492
2493/*
Jan Karac851ed52008-07-11 19:27:31 -04002494 * Initialize jbd inode head
2495 */
2496void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2497{
2498 jinode->i_transaction = NULL;
2499 jinode->i_next_transaction = NULL;
2500 jinode->i_vfs_inode = inode;
2501 jinode->i_flags = 0;
2502 INIT_LIST_HEAD(&jinode->i_list);
2503}
2504
2505/*
2506 * Function to be called before we start removing inode from memory (i.e.,
2507 * clear_inode() is a fine place to be called from). It removes inode from
2508 * transaction's lists.
2509 */
2510void jbd2_journal_release_jbd_inode(journal_t *journal,
2511 struct jbd2_inode *jinode)
2512{
Jan Karac851ed52008-07-11 19:27:31 -04002513 if (!journal)
2514 return;
2515restart:
2516 spin_lock(&journal->j_list_lock);
2517 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002518 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002519 wait_queue_head_t *wq;
2520 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2521 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2522 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2523 spin_unlock(&journal->j_list_lock);
2524 schedule();
2525 finish_wait(wq, &wait.wait);
2526 goto restart;
2527 }
2528
Jan Karac851ed52008-07-11 19:27:31 -04002529 if (jinode->i_transaction) {
2530 list_del(&jinode->i_list);
2531 jinode->i_transaction = NULL;
2532 }
2533 spin_unlock(&journal->j_list_lock);
2534}
2535
Dave Kleikamp470decc2006-10-11 01:20:57 -07002536
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002537#ifdef CONFIG_PROC_FS
2538
2539#define JBD2_STATS_PROC_NAME "fs/jbd2"
2540
2541static void __init jbd2_create_jbd_stats_proc_entry(void)
2542{
2543 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2544}
2545
2546static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2547{
2548 if (proc_jbd2_stats)
2549 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2550}
2551
2552#else
2553
2554#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2555#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2556
2557#endif
2558
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002559struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002560
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002561static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002562{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002563 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002564 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002565 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2566 return -ENOMEM;
2567 }
2568 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2569 if (jbd2_inode_cache == NULL) {
2570 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2571 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002572 return -ENOMEM;
2573 }
2574 return 0;
2575}
2576
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002577static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002578{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002579 if (jbd2_handle_cache)
2580 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002581 if (jbd2_inode_cache)
2582 kmem_cache_destroy(jbd2_inode_cache);
2583
Dave Kleikamp470decc2006-10-11 01:20:57 -07002584}
2585
2586/*
2587 * Module startup and shutdown
2588 */
2589
2590static int __init journal_init_caches(void)
2591{
2592 int ret;
2593
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002594 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002595 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002596 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002597 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002598 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002599 if (ret == 0)
2600 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002601 return ret;
2602}
2603
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002604static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002605{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002606 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002607 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002608 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002609 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002610 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002611}
2612
2613static int __init journal_init(void)
2614{
2615 int ret;
2616
2617 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2618
2619 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002620 if (ret == 0) {
Duane Griffin620de4e2008-04-29 22:02:47 -04002621 jbd2_create_jbd_stats_proc_entry();
2622 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002623 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002624 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002625 return ret;
2626}
2627
2628static void __exit journal_exit(void)
2629{
Jose R. Santose23291b2007-07-18 08:57:06 -04002630#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002631 int n = atomic_read(&nr_journal_heads);
2632 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002633 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002634#endif
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002635 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002636 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002637}
2638
Dave Kleikamp470decc2006-10-11 01:20:57 -07002639MODULE_LICENSE("GPL");
2640module_init(journal_init);
2641module_exit(journal_exit);
2642