blob: 484b8d1c6cb6cd645a009f2cdb1c50884f3a9563 [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>
Jose R. Santos0f49d5d2007-07-18 08:50:18 -040038#include <linux/debugfs.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050039#include <linux/seq_file.h>
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -050040#include <linux/math64.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040041#include <linux/hash.h>
Theodore Ts'od2eecb02009-12-07 10:36:20 -050042#include <linux/log2.h>
43#include <linux/vmalloc.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040044#include <linux/backing-dev.h>
Brian King39e3ac22010-10-27 21:25:12 -040045#include <linux/bitops.h>
Theodore Ts'o670be5a2010-12-17 10:44:16 -050046#include <linux/ratelimit.h>
Theodore Ts'o879c5e62009-06-17 11:47:48 -040047
48#define CREATE_TRACE_POINTS
49#include <trace/events/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070050
51#include <asm/uaccess.h>
52#include <asm/page.h>
53
Mingming Caof7f4bcc2006-10-11 01:20:59 -070054EXPORT_SYMBOL(jbd2_journal_extend);
55EXPORT_SYMBOL(jbd2_journal_stop);
56EXPORT_SYMBOL(jbd2_journal_lock_updates);
57EXPORT_SYMBOL(jbd2_journal_unlock_updates);
58EXPORT_SYMBOL(jbd2_journal_get_write_access);
59EXPORT_SYMBOL(jbd2_journal_get_create_access);
60EXPORT_SYMBOL(jbd2_journal_get_undo_access);
Joel Beckere06c8222008-09-11 15:35:47 -070061EXPORT_SYMBOL(jbd2_journal_set_triggers);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070062EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
63EXPORT_SYMBOL(jbd2_journal_release_buffer);
64EXPORT_SYMBOL(jbd2_journal_forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -070065#if 0
66EXPORT_SYMBOL(journal_sync_buffer);
67#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068EXPORT_SYMBOL(jbd2_journal_flush);
69EXPORT_SYMBOL(jbd2_journal_revoke);
Dave Kleikamp470decc2006-10-11 01:20:57 -070070
Mingming Caof7f4bcc2006-10-11 01:20:59 -070071EXPORT_SYMBOL(jbd2_journal_init_dev);
72EXPORT_SYMBOL(jbd2_journal_init_inode);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070073EXPORT_SYMBOL(jbd2_journal_check_used_features);
74EXPORT_SYMBOL(jbd2_journal_check_available_features);
75EXPORT_SYMBOL(jbd2_journal_set_features);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070076EXPORT_SYMBOL(jbd2_journal_load);
77EXPORT_SYMBOL(jbd2_journal_destroy);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070078EXPORT_SYMBOL(jbd2_journal_abort);
79EXPORT_SYMBOL(jbd2_journal_errno);
80EXPORT_SYMBOL(jbd2_journal_ack_err);
81EXPORT_SYMBOL(jbd2_journal_clear_err);
82EXPORT_SYMBOL(jbd2_log_wait_commit);
Theodore Ts'o3b799d12009-12-09 20:42:53 -050083EXPORT_SYMBOL(jbd2_log_start_commit);
Mingming Caof7f4bcc2006-10-11 01:20:59 -070084EXPORT_SYMBOL(jbd2_journal_start_commit);
85EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
86EXPORT_SYMBOL(jbd2_journal_wipe);
87EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
88EXPORT_SYMBOL(jbd2_journal_invalidatepage);
89EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
90EXPORT_SYMBOL(jbd2_journal_force_commit);
Jan Karac851ed52008-07-11 19:27:31 -040091EXPORT_SYMBOL(jbd2_journal_file_inode);
92EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
93EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
94EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050095EXPORT_SYMBOL(jbd2_inode_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -070096
Dave Kleikamp470decc2006-10-11 01:20:57 -070097static void __journal_abort_soft (journal_t *journal, int errno);
Theodore Ts'od2eecb02009-12-07 10:36:20 -050098static int jbd2_journal_create_slab(size_t slab_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -070099
Darrick J. Wong25ed6e82012-05-27 07:48:56 -0400100/* Checksumming functions */
101int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
102{
103 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
104 return 1;
105
106 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
107}
108
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -0400109static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
110{
111 __u32 csum, old_csum;
112
113 old_csum = sb->s_checksum;
114 sb->s_checksum = 0;
115 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
116 sb->s_checksum = old_csum;
117
118 return cpu_to_be32(csum);
119}
120
121int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
122{
123 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
124 return 1;
125
126 return sb->s_checksum == jbd2_superblock_csum(j, sb);
127}
128
129void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
130{
131 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
132 return;
133
134 sb->s_checksum = jbd2_superblock_csum(j, sb);
135}
136
Dave Kleikamp470decc2006-10-11 01:20:57 -0700137/*
138 * Helper function used to manage commit timeouts
139 */
140
141static void commit_timeout(unsigned long __data)
142{
143 struct task_struct * p = (struct task_struct *) __data;
144
145 wake_up_process(p);
146}
147
148/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700149 * kjournald2: The main thread function used to manage a logging device
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 * journal.
151 *
152 * This kernel thread is responsible for two things:
153 *
154 * 1) COMMIT: Every so often we need to commit the current state of the
155 * filesystem to disk. The journal thread is responsible for writing
156 * all of the metadata buffers to disk.
157 *
158 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
159 * of the data in that part of the log has been rewritten elsewhere on
160 * the disk. Flushing these old buffers to reclaim space in the log is
161 * known as checkpointing, and this thread is responsible for that job.
162 */
163
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700164static int kjournald2(void *arg)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700165{
166 journal_t *journal = arg;
167 transaction_t *transaction;
168
169 /*
170 * Set up an interval timer which can be used to trigger a commit wakeup
171 * after the commit interval expires
172 */
173 setup_timer(&journal->j_commit_timer, commit_timeout,
174 (unsigned long)current);
175
Nigel Cunningham35c80422012-02-03 19:59:41 +1100176 set_freezable();
177
Dave Kleikamp470decc2006-10-11 01:20:57 -0700178 /* Record that the journal thread is running */
179 journal->j_task = current;
180 wake_up(&journal->j_wait_done_commit);
181
Dave Kleikamp470decc2006-10-11 01:20:57 -0700182 /*
183 * And now, wait forever for commit wakeup events.
184 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400185 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186
187loop:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700188 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700189 goto end_loop;
190
191 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
192 journal->j_commit_sequence, journal->j_commit_request);
193
194 if (journal->j_commit_sequence != journal->j_commit_request) {
195 jbd_debug(1, "OK, requests differ\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400196 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197 del_timer_sync(&journal->j_commit_timer);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700198 jbd2_journal_commit_transaction(journal);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400199 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 goto loop;
201 }
202
203 wake_up(&journal->j_wait_done_commit);
204 if (freezing(current)) {
205 /*
206 * The simpler the better. Flushing journal isn't a
207 * good idea, because that depends on threads that may
208 * be already stopped.
209 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700210 jbd_debug(1, "Now suspending kjournald2\n");
Theodore Ts'oa931da62010-08-03 21:35:12 -0400211 write_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800212 try_to_freeze();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400213 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700214 } else {
215 /*
216 * We assume on resume that commits are already there,
217 * so we don't sleep
218 */
219 DEFINE_WAIT(wait);
220 int should_sleep = 1;
221
222 prepare_to_wait(&journal->j_wait_commit, &wait,
223 TASK_INTERRUPTIBLE);
224 if (journal->j_commit_sequence != journal->j_commit_request)
225 should_sleep = 0;
226 transaction = journal->j_running_transaction;
227 if (transaction && time_after_eq(jiffies,
228 transaction->t_expires))
229 should_sleep = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700230 if (journal->j_flags & JBD2_UNMOUNT)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700231 should_sleep = 0;
232 if (should_sleep) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400233 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234 schedule();
Theodore Ts'oa931da62010-08-03 21:35:12 -0400235 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700236 }
237 finish_wait(&journal->j_wait_commit, &wait);
238 }
239
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700240 jbd_debug(1, "kjournald2 wakes\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -0700241
242 /*
243 * Were we woken up by a commit wakeup event?
244 */
245 transaction = journal->j_running_transaction;
246 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
247 journal->j_commit_request = transaction->t_tid;
248 jbd_debug(1, "woke because of timeout\n");
249 }
250 goto loop;
251
252end_loop:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400253 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700254 del_timer_sync(&journal->j_commit_timer);
255 journal->j_task = NULL;
256 wake_up(&journal->j_wait_done_commit);
257 jbd_debug(1, "Journal thread exiting.\n");
258 return 0;
259}
260
Pavel Emelianov97f06782007-05-08 00:30:42 -0700261static int jbd2_journal_start_thread(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700263 struct task_struct *t;
264
Theodore Ts'o90576c02009-09-29 15:51:30 -0400265 t = kthread_run(kjournald2, journal, "jbd2/%s",
266 journal->j_devname);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700267 if (IS_ERR(t))
268 return PTR_ERR(t);
269
Al Viro1076d172008-03-29 03:07:18 +0000270 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700271 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700272}
273
274static void journal_kill_thread(journal_t *journal)
275{
Theodore Ts'oa931da62010-08-03 21:35:12 -0400276 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700277 journal->j_flags |= JBD2_UNMOUNT;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700278
279 while (journal->j_task) {
280 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400281 write_unlock(&journal->j_state_lock);
Al Viro1076d172008-03-29 03:07:18 +0000282 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400283 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400285 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286}
287
288/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700289 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700290 *
291 * Writes a metadata buffer to a given disk block. The actual IO is not
292 * performed but a new buffer_head is constructed which labels the data
293 * to be written with the correct destination disk block.
294 *
295 * Any magic-number escaping which needs to be done will cause a
296 * copy-out here. If the buffer happens to start with the
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700297 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700298 * magic number is only written to the log for descripter blocks. In
299 * this case, we copy the data and replace the first word with 0, and we
300 * return a result code which indicates that this buffer needs to be
301 * marked as an escaped buffer in the corresponding log descriptor
302 * block. The missing word can then be restored when the block is read
303 * during recovery.
304 *
305 * If the source buffer has already been modified by a new transaction
306 * since we took the last commit snapshot, we use the frozen copy of
307 * that data for IO. If we end up using the existing buffer_head's data
308 * for the write, then we *have* to lock the buffer to prevent anyone
309 * else from using and possibly modifying it while the IO is in
310 * progress.
311 *
312 * The function returns a pointer to the buffer_heads to be used for IO.
313 *
314 * We assume that the journal has already been locked in this function.
315 *
316 * Return value:
317 * <0: Error
318 * >=0: Finished OK
319 *
320 * On success:
321 * Bit 0 set == escape performed on the data
322 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
323 */
324
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700325int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700326 struct journal_head *jh_in,
327 struct journal_head **jh_out,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700328 unsigned long long blocknr)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329{
330 int need_copy_out = 0;
331 int done_copy_out = 0;
332 int do_escape = 0;
333 char *mapped_data;
334 struct buffer_head *new_bh;
335 struct journal_head *new_jh;
336 struct page *new_page;
337 unsigned int new_offset;
338 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghua96577c42009-07-13 17:55:35 -0400339 journal_t *journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700340
341 /*
342 * The buffer really shouldn't be locked: only the current committing
343 * transaction is allowed to write it, so nobody else is allowed
344 * to do any IO.
345 *
346 * akpm: except if we're journalling data, and write() output is
347 * also part of a shared mapping, and another thread has
348 * decided to launch a writepage() against this buffer.
349 */
350 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
351
Theodore Ts'o47def822010-07-27 11:56:05 -0400352retry_alloc:
353 new_bh = alloc_buffer_head(GFP_NOFS);
354 if (!new_bh) {
355 /*
356 * Failure is not an option, but __GFP_NOFAIL is going
357 * away; so we retry ourselves here.
358 */
359 congestion_wait(BLK_RW_ASYNC, HZ/50);
360 goto retry_alloc;
361 }
362
dingdinghua96577c42009-07-13 17:55:35 -0400363 /* keep subsequent assertions sane */
364 new_bh->b_state = 0;
365 init_buffer(new_bh, NULL, NULL);
366 atomic_set(&new_bh->b_count, 1);
367 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700368
369 /*
370 * If a new transaction has already done a buffer copy-out, then
371 * we use that version of the data for the commit.
372 */
373 jbd_lock_bh_state(bh_in);
374repeat:
375 if (jh_in->b_frozen_data) {
376 done_copy_out = 1;
377 new_page = virt_to_page(jh_in->b_frozen_data);
378 new_offset = offset_in_page(jh_in->b_frozen_data);
379 } else {
380 new_page = jh2bh(jh_in)->b_page;
381 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
382 }
383
Cong Wang303a8f22011-11-25 23:14:31 +0800384 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700385 /*
Jan Kara13ceef02010-07-14 07:56:33 +0200386 * Fire data frozen trigger if data already wasn't frozen. Do this
387 * before checking for escaping, as the trigger may modify the magic
388 * offset. If a copy-out happens afterwards, it will have the correct
389 * data in the buffer.
Joel Beckere06c8222008-09-11 15:35:47 -0700390 */
Jan Kara13ceef02010-07-14 07:56:33 +0200391 if (!done_copy_out)
392 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
393 jh_in->b_triggers);
Joel Beckere06c8222008-09-11 15:35:47 -0700394
395 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396 * Check for escaping
397 */
398 if (*((__be32 *)(mapped_data + new_offset)) ==
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700399 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700400 need_copy_out = 1;
401 do_escape = 1;
402 }
Cong Wang303a8f22011-11-25 23:14:31 +0800403 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404
405 /*
406 * Do we need to do a data copy?
407 */
408 if (need_copy_out && !done_copy_out) {
409 char *tmp;
410
411 jbd_unlock_bh_state(bh_in);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400412 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
Theodore Ts'oe6ec1162009-12-01 09:04:42 -0500413 if (!tmp) {
414 jbd2_journal_put_journal_head(new_jh);
415 return -ENOMEM;
416 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417 jbd_lock_bh_state(bh_in);
418 if (jh_in->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400419 jbd2_free(tmp, bh_in->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 goto repeat;
421 }
422
423 jh_in->b_frozen_data = tmp;
Cong Wang303a8f22011-11-25 23:14:31 +0800424 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700425 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800426 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700427
428 new_page = virt_to_page(tmp);
429 new_offset = offset_in_page(tmp);
430 done_copy_out = 1;
Joel Beckere06c8222008-09-11 15:35:47 -0700431
432 /*
433 * This isn't strictly necessary, as we're using frozen
434 * data for the escaping, but it keeps consistency with
435 * b_frozen_data usage.
436 */
437 jh_in->b_frozen_triggers = jh_in->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438 }
439
440 /*
441 * Did we need to do an escaping? Now we've done all the
442 * copying, we can finally do so.
443 */
444 if (do_escape) {
Cong Wang303a8f22011-11-25 23:14:31 +0800445 mapped_data = kmap_atomic(new_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang303a8f22011-11-25 23:14:31 +0800447 kunmap_atomic(mapped_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700448 }
449
Dave Kleikamp470decc2006-10-11 01:20:57 -0700450 set_bh_page(new_bh, new_page, new_offset);
451 new_jh->b_transaction = NULL;
452 new_bh->b_size = jh2bh(jh_in)->b_size;
453 new_bh->b_bdev = transaction->t_journal->j_dev;
454 new_bh->b_blocknr = blocknr;
455 set_buffer_mapped(new_bh);
456 set_buffer_dirty(new_bh);
457
458 *jh_out = new_jh;
459
460 /*
461 * The to-be-written buffer needs to get moved to the io queue,
462 * and the original buffer whose contents we are shadowing or
463 * copying is moved to the transaction's shadow queue.
464 */
465 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghua96577c42009-07-13 17:55:35 -0400466 spin_lock(&journal->j_list_lock);
467 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
468 spin_unlock(&journal->j_list_lock);
469 jbd_unlock_bh_state(bh_in);
470
Dave Kleikamp470decc2006-10-11 01:20:57 -0700471 JBUFFER_TRACE(new_jh, "file as BJ_IO");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700472 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700473
474 return do_escape | (done_copy_out << 1);
475}
476
477/*
478 * Allocation code for the journal file. Manage the space left in the
479 * journal, so that we can begin checkpointing when appropriate.
480 */
481
482/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700483 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700484 *
485 * Called with the journal already locked.
486 *
487 * Called under j_state_lock
488 */
489
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700490int __jbd2_log_space_left(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700491{
492 int left = journal->j_free;
493
Theodore Ts'oa931da62010-08-03 21:35:12 -0400494 /* assert_spin_locked(&journal->j_state_lock); */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700495
496 /*
497 * Be pessimistic here about the number of those free blocks which
498 * might be required for log descriptor control blocks.
499 */
500
501#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
502
503 left -= MIN_LOG_RESERVED_BLOCKS;
504
505 if (left <= 0)
506 return 0;
507 left -= (left >> 3);
508 return left;
509}
510
511/*
Theodore Ts'oe4471832011-02-12 08:18:24 -0500512 * Called with j_state_lock locked for writing.
513 * Returns true if a transaction commit was started.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700515int __jbd2_log_start_commit(journal_t *journal, tid_t target)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516{
517 /*
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400518 * The only transaction we can possibly wait upon is the
519 * currently running transaction (if it exists). Otherwise,
520 * the target tid must be an old one.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700521 */
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400522 if (journal->j_running_transaction &&
523 journal->j_running_transaction->t_tid == target) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200525 * We want a new commit: OK, mark the request and wakeup the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 * commit thread. We do _not_ do the commit ourselves.
527 */
528
529 journal->j_commit_request = target;
Eryu Guanf2a44522011-11-01 19:09:18 -0400530 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531 journal->j_commit_request,
532 journal->j_commit_sequence);
533 wake_up(&journal->j_wait_commit);
534 return 1;
Theodore Ts'odeeeaf12011-05-01 18:16:26 -0400535 } else if (!tid_geq(journal->j_commit_request, target))
536 /* This should never happen, but if it does, preserve
537 the evidence before kjournald goes into a loop and
538 increments j_commit_sequence beyond all recognition. */
Eryu Guanf2a44522011-11-01 19:09:18 -0400539 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
Theodore Ts'o1be2add2011-05-08 19:37:54 -0400540 journal->j_commit_request,
541 journal->j_commit_sequence,
542 target, journal->j_running_transaction ?
543 journal->j_running_transaction->t_tid : 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 return 0;
545}
546
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700547int jbd2_log_start_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548{
549 int ret;
550
Theodore Ts'oa931da62010-08-03 21:35:12 -0400551 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700552 ret = __jbd2_log_start_commit(journal, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400553 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700554 return ret;
555}
556
557/*
558 * Force and wait upon a commit if the calling process is not within
559 * transaction. This is used for forcing out undo-protected data which contains
560 * bitmaps, when the fs is running out of space.
561 *
562 * We can only force the running transaction if we don't have an active handle;
563 * otherwise, we will deadlock.
564 *
565 * Returns true if a transaction was started.
566 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700567int jbd2_journal_force_commit_nested(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568{
569 transaction_t *transaction = NULL;
570 tid_t tid;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500571 int need_to_start = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572
Theodore Ts'oa931da62010-08-03 21:35:12 -0400573 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700574 if (journal->j_running_transaction && !current->journal_info) {
575 transaction = journal->j_running_transaction;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500576 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
577 need_to_start = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578 } else if (journal->j_committing_transaction)
579 transaction = journal->j_committing_transaction;
580
581 if (!transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400582 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583 return 0; /* Nothing to retry */
584 }
585
586 tid = transaction->t_tid;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400587 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500588 if (need_to_start)
589 jbd2_log_start_commit(journal, tid);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700590 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700591 return 1;
592}
593
594/*
595 * Start a commit of the current running transaction (if any). Returns true
Jan Karac88ccea2009-02-10 11:27:46 -0500596 * if a transaction is going to be committed (or is currently already
597 * committing), and fills its tid in at *ptid
Dave Kleikamp470decc2006-10-11 01:20:57 -0700598 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700599int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700600{
601 int ret = 0;
602
Theodore Ts'oa931da62010-08-03 21:35:12 -0400603 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604 if (journal->j_running_transaction) {
605 tid_t tid = journal->j_running_transaction->t_tid;
606
Jan Karac88ccea2009-02-10 11:27:46 -0500607 __jbd2_log_start_commit(journal, tid);
608 /* There's a running transaction and we've just made sure
609 * it's commit has been scheduled. */
610 if (ptid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700611 *ptid = tid;
Jan Karac88ccea2009-02-10 11:27:46 -0500612 ret = 1;
613 } else if (journal->j_committing_transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300615 * If commit has been started, then we have to wait for
616 * completion of that transaction.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700617 */
Jan Karac88ccea2009-02-10 11:27:46 -0500618 if (ptid)
619 *ptid = journal->j_committing_transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700620 ret = 1;
621 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400622 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700623 return ret;
624}
625
626/*
Jan Karabbd2be32011-05-24 11:59:18 -0400627 * Return 1 if a given transaction has not yet sent barrier request
628 * connected with a transaction commit. If 0 is returned, transaction
629 * may or may not have sent the barrier. Used to avoid sending barrier
630 * twice in common cases.
631 */
632int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
633{
634 int ret = 0;
635 transaction_t *commit_trans;
636
637 if (!(journal->j_flags & JBD2_BARRIER))
638 return 0;
639 read_lock(&journal->j_state_lock);
640 /* Transaction already committed? */
641 if (tid_geq(journal->j_commit_sequence, tid))
642 goto out;
643 commit_trans = journal->j_committing_transaction;
644 if (!commit_trans || commit_trans->t_tid != tid) {
645 ret = 1;
646 goto out;
647 }
648 /*
649 * Transaction is being committed and we already proceeded to
650 * submitting a flush to fs partition?
651 */
652 if (journal->j_fs_dev != journal->j_dev) {
653 if (!commit_trans->t_need_data_flush ||
654 commit_trans->t_state >= T_COMMIT_DFLUSH)
655 goto out;
656 } else {
657 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
658 goto out;
659 }
660 ret = 1;
661out:
662 read_unlock(&journal->j_state_lock);
663 return ret;
664}
665EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
666
667/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700668 * Wait for a specified commit to complete.
669 * The caller may not hold the journal lock.
670 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700671int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672{
673 int err = 0;
674
Theodore Ts'oa931da62010-08-03 21:35:12 -0400675 read_lock(&journal->j_state_lock);
Jose R. Santose23291b2007-07-18 08:57:06 -0400676#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -0700677 if (!tid_geq(journal->j_commit_request, tid)) {
678 printk(KERN_EMERG
679 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400680 __func__, journal->j_commit_request, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700681 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700682#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700683 while (tid_gt(tid, journal->j_commit_sequence)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400684 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700685 tid, journal->j_commit_sequence);
686 wake_up(&journal->j_wait_commit);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400687 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700688 wait_event(journal->j_wait_done_commit,
689 !tid_gt(tid, journal->j_commit_sequence));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400690 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700691 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400692 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700693
694 if (unlikely(is_journal_aborted(journal))) {
695 printk(KERN_EMERG "journal commit I/O error\n");
696 err = -EIO;
697 }
698 return err;
699}
700
701/*
702 * Log buffer allocation routines:
703 */
704
Mingming Cao18eba7a2006-10-11 01:21:13 -0700705int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700706{
707 unsigned long blocknr;
708
Theodore Ts'oa931da62010-08-03 21:35:12 -0400709 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700710 J_ASSERT(journal->j_free > 1);
711
712 blocknr = journal->j_head;
713 journal->j_head++;
714 journal->j_free--;
715 if (journal->j_head == journal->j_last)
716 journal->j_head = journal->j_first;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400717 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700718 return jbd2_journal_bmap(journal, blocknr, retp);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700719}
720
721/*
722 * Conversion of logical to physical block numbers for the journal
723 *
724 * On external journals the journal blocks are identity-mapped, so
725 * this is a no-op. If needed, we can use j_blk_offset - everything is
726 * ready.
727 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700728int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700729 unsigned long long *retp)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700730{
731 int err = 0;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700732 unsigned long long ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700733
734 if (journal->j_inode) {
735 ret = bmap(journal->j_inode, blocknr);
736 if (ret)
737 *retp = ret;
738 else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700739 printk(KERN_ALERT "%s: journal block not found "
740 "at offset %lu on %s\n",
Theodore Ts'o05496762008-09-16 14:36:17 -0400741 __func__, blocknr, journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700742 err = -EIO;
743 __journal_abort_soft(journal, err);
744 }
745 } else {
746 *retp = blocknr; /* +journal->j_blk_offset */
747 }
748 return err;
749}
750
751/*
752 * We play buffer_head aliasing tricks to write data/metadata blocks to
753 * the journal without copying their contents, but for journal
754 * descriptor blocks we do need to generate bona fide buffers.
755 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700756 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
Dave Kleikamp470decc2006-10-11 01:20:57 -0700757 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
758 * But we don't bother doing that, so there will be coherency problems with
759 * mmaps of blockdevs which hold live JBD-controlled filesystems.
760 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700761struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700762{
763 struct buffer_head *bh;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700764 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700765 int err;
766
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700767 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700768
769 if (err)
770 return NULL;
771
772 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -0500773 if (!bh)
774 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700775 lock_buffer(bh);
776 memset(bh->b_data, 0, journal->j_blocksize);
777 set_buffer_uptodate(bh);
778 unlock_buffer(bh);
779 BUFFER_TRACE(bh, "return this buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700780 return jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700781}
782
Jan Kara79feb522012-03-13 22:22:54 -0400783/*
784 * Return tid of the oldest transaction in the journal and block in the journal
785 * where the transaction starts.
786 *
787 * If the journal is now empty, return which will be the next transaction ID
788 * we will write and where will that transaction start.
789 *
790 * The return value is 0 if journal tail cannot be pushed any further, 1 if
791 * it can.
792 */
793int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
794 unsigned long *block)
795{
796 transaction_t *transaction;
797 int ret;
798
799 read_lock(&journal->j_state_lock);
800 spin_lock(&journal->j_list_lock);
801 transaction = journal->j_checkpoint_transactions;
802 if (transaction) {
803 *tid = transaction->t_tid;
804 *block = transaction->t_log_start;
805 } else if ((transaction = journal->j_committing_transaction) != NULL) {
806 *tid = transaction->t_tid;
807 *block = transaction->t_log_start;
808 } else if ((transaction = journal->j_running_transaction) != NULL) {
809 *tid = transaction->t_tid;
810 *block = journal->j_head;
811 } else {
812 *tid = journal->j_transaction_sequence;
813 *block = journal->j_head;
814 }
815 ret = tid_gt(*tid, journal->j_tail_sequence);
816 spin_unlock(&journal->j_list_lock);
817 read_unlock(&journal->j_state_lock);
818
819 return ret;
820}
821
822/*
823 * Update information in journal structure and in on disk journal superblock
824 * about log tail. This function does not check whether information passed in
825 * really pushes log tail further. It's responsibility of the caller to make
826 * sure provided log tail information is valid (e.g. by holding
827 * j_checkpoint_mutex all the time between computing log tail and calling this
828 * function as is the case with jbd2_cleanup_journal_tail()).
829 *
830 * Requires j_checkpoint_mutex
831 */
832void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
833{
834 unsigned long freed;
835
836 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
837
838 /*
839 * We cannot afford for write to remain in drive's caches since as
840 * soon as we update j_tail, next transaction can start reusing journal
841 * space and if we lose sb update during power failure we'd replay
842 * old transaction with possibly newly overwritten data.
843 */
844 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
845 write_lock(&journal->j_state_lock);
846 freed = block - journal->j_tail;
847 if (block < journal->j_tail)
848 freed += journal->j_last - journal->j_first;
849
850 trace_jbd2_update_log_tail(journal, tid, block, freed);
851 jbd_debug(1,
852 "Cleaning journal tail from %d to %d (offset %lu), "
853 "freeing %lu\n",
854 journal->j_tail_sequence, tid, block, freed);
855
856 journal->j_free += freed;
857 journal->j_tail_sequence = tid;
858 journal->j_tail = block;
859 write_unlock(&journal->j_state_lock);
860}
861
Jan Kara33395782012-03-13 22:45:38 -0400862/*
863 * This is a variaon of __jbd2_update_log_tail which checks for validity of
864 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
865 * with other threads updating log tail.
866 */
867void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
868{
869 mutex_lock(&journal->j_checkpoint_mutex);
870 if (tid_gt(tid, journal->j_tail_sequence))
871 __jbd2_update_log_tail(journal, tid, block);
872 mutex_unlock(&journal->j_checkpoint_mutex);
873}
874
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500875struct jbd2_stats_proc_session {
876 journal_t *journal;
877 struct transaction_stats_s *stats;
878 int start;
879 int max;
880};
881
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500882static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
883{
884 return *pos ? NULL : SEQ_START_TOKEN;
885}
886
887static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
888{
889 return NULL;
890}
891
892static int jbd2_seq_info_show(struct seq_file *seq, void *v)
893{
894 struct jbd2_stats_proc_session *s = seq->private;
895
896 if (v != SEQ_START_TOKEN)
897 return 0;
Theodore Ts'obf699322009-09-30 00:32:06 -0400898 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500899 s->stats->ts_tid,
900 s->journal->j_max_transaction_buffers);
901 if (s->stats->ts_tid == 0)
902 return 0;
903 seq_printf(seq, "average: \n %ums waiting for transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400904 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500905 seq_printf(seq, " %ums running transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400906 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500907 seq_printf(seq, " %ums transaction was being locked\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400908 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500909 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400910 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500911 seq_printf(seq, " %ums logging transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400912 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
Simon Holm Thøgersenc225aa52009-01-11 22:34:01 -0500913 seq_printf(seq, " %lluus average transaction commit time\n",
914 div_u64(s->journal->j_average_commit_time, 1000));
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500915 seq_printf(seq, " %lu handles per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400916 s->stats->run.rs_handle_count / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500917 seq_printf(seq, " %lu blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400918 s->stats->run.rs_blocks / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500919 seq_printf(seq, " %lu logged blocks per transaction\n",
Theodore Ts'obf699322009-09-30 00:32:06 -0400920 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500921 return 0;
922}
923
924static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
925{
926}
927
James Morris88e9d342009-09-22 16:43:43 -0700928static const struct seq_operations jbd2_seq_info_ops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500929 .start = jbd2_seq_info_start,
930 .next = jbd2_seq_info_next,
931 .stop = jbd2_seq_info_stop,
932 .show = jbd2_seq_info_show,
933};
934
935static int jbd2_seq_info_open(struct inode *inode, struct file *file)
936{
937 journal_t *journal = PDE(inode)->data;
938 struct jbd2_stats_proc_session *s;
939 int rc, size;
940
941 s = kmalloc(sizeof(*s), GFP_KERNEL);
942 if (s == NULL)
943 return -ENOMEM;
944 size = sizeof(struct transaction_stats_s);
945 s->stats = kmalloc(size, GFP_KERNEL);
946 if (s->stats == NULL) {
947 kfree(s);
948 return -ENOMEM;
949 }
950 spin_lock(&journal->j_history_lock);
951 memcpy(s->stats, &journal->j_stats, size);
952 s->journal = journal;
953 spin_unlock(&journal->j_history_lock);
954
955 rc = seq_open(file, &jbd2_seq_info_ops);
956 if (rc == 0) {
957 struct seq_file *m = file->private_data;
958 m->private = s;
959 } else {
960 kfree(s->stats);
961 kfree(s);
962 }
963 return rc;
964
965}
966
967static int jbd2_seq_info_release(struct inode *inode, struct file *file)
968{
969 struct seq_file *seq = file->private_data;
970 struct jbd2_stats_proc_session *s = seq->private;
971 kfree(s->stats);
972 kfree(s);
973 return seq_release(inode, file);
974}
975
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700976static const struct file_operations jbd2_seq_info_fops = {
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500977 .owner = THIS_MODULE,
978 .open = jbd2_seq_info_open,
979 .read = seq_read,
980 .llseek = seq_lseek,
981 .release = jbd2_seq_info_release,
982};
983
984static struct proc_dir_entry *proc_jbd2_stats;
985
986static void jbd2_stats_proc_init(journal_t *journal)
987{
Theodore Ts'o05496762008-09-16 14:36:17 -0400988 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500989 if (journal->j_proc_entry) {
Denis V. Lunev79da3662008-04-29 01:02:11 -0700990 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
991 &jbd2_seq_info_fops, journal);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500992 }
993}
994
995static void jbd2_stats_proc_exit(journal_t *journal)
996{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500997 remove_proc_entry("info", journal->j_proc_entry);
Theodore Ts'o05496762008-09-16 14:36:17 -0400998 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500999}
1000
Dave Kleikamp470decc2006-10-11 01:20:57 -07001001/*
1002 * Management for journal control blocks: functions to create and
1003 * destroy journal_t structures, and to initialise and read existing
1004 * journal blocks from disk. */
1005
1006/* First: create and setup a journal_t object in memory. We initialise
1007 * very few fields yet: that has to wait until we have created the
1008 * journal structures from from scratch, or loaded them from disk. */
1009
1010static journal_t * journal_init_common (void)
1011{
1012 journal_t *journal;
1013 int err;
1014
Andrew Morton3ebfdf82009-12-23 08:05:15 -05001015 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001016 if (!journal)
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001017 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001018
1019 init_waitqueue_head(&journal->j_wait_transaction_locked);
1020 init_waitqueue_head(&journal->j_wait_logspace);
1021 init_waitqueue_head(&journal->j_wait_done_commit);
1022 init_waitqueue_head(&journal->j_wait_checkpoint);
1023 init_waitqueue_head(&journal->j_wait_commit);
1024 init_waitqueue_head(&journal->j_wait_updates);
1025 mutex_init(&journal->j_barrier);
1026 mutex_init(&journal->j_checkpoint_mutex);
1027 spin_lock_init(&journal->j_revoke_lock);
1028 spin_lock_init(&journal->j_list_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001029 rwlock_init(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001030
Mingming Caocd02ff02007-10-16 18:38:25 -04001031 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
Theodore Ts'o30773842009-01-03 20:27:38 -05001032 journal->j_min_batch_time = 0;
1033 journal->j_max_batch_time = 15000; /* 15ms */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034
1035 /* The journal is marked for error until we succeed with recovery! */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001036 journal->j_flags = JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037
1038 /* Set up a default-sized revoke table for the new mount. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001039 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040 if (err) {
1041 kfree(journal);
Theodore Ts'ob7271b02010-12-18 13:39:38 -05001042 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001043 }
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001044
Theodore Ts'obf699322009-09-30 00:32:06 -04001045 spin_lock_init(&journal->j_history_lock);
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001046
Dave Kleikamp470decc2006-10-11 01:20:57 -07001047 return journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001048}
1049
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001050/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051 *
1052 * Create a journal structure assigned some fixed set of disk blocks to
1053 * the journal. We don't actually touch those disk blocks yet, but we
1054 * need to set up all of the mapping information to tell the journaling
1055 * system where the journal blocks are.
1056 *
1057 */
1058
1059/**
Randy Dunlap5648ba52008-04-17 10:38:59 -04001060 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
Dave Kleikamp470decc2006-10-11 01:20:57 -07001061 * @bdev: Block device on which to create the journal
1062 * @fs_dev: Device which hold journalled filesystem for this journal.
1063 * @start: Block nr Start of journal.
1064 * @len: Length of the journal in blocks.
1065 * @blocksize: blocksize of journalling device
Randy Dunlap5648ba52008-04-17 10:38:59 -04001066 *
1067 * Returns: a newly created journal_t *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001068 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001069 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
Dave Kleikamp470decc2006-10-11 01:20:57 -07001070 * range of blocks on an arbitrary block device.
1071 *
1072 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001073journal_t * jbd2_journal_init_dev(struct block_device *bdev,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001074 struct block_device *fs_dev,
Mingming Cao18eba7a2006-10-11 01:21:13 -07001075 unsigned long long start, int len, int blocksize)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001076{
1077 journal_t *journal = journal_init_common();
1078 struct buffer_head *bh;
Theodore Ts'o05496762008-09-16 14:36:17 -04001079 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001080 int n;
1081
1082 if (!journal)
1083 return NULL;
1084
1085 /* journal descriptor can store up to n blocks -bzzz */
1086 journal->j_blocksize = blocksize;
yangsheng0587aa32010-11-17 21:46:26 -05001087 journal->j_dev = bdev;
1088 journal->j_fs_dev = fs_dev;
1089 journal->j_blk_offset = start;
1090 journal->j_maxlen = len;
1091 bdevname(journal->j_dev, journal->j_devname);
1092 p = journal->j_devname;
1093 while ((p = strchr(p, '/')))
1094 *p = '!';
Jan Kara4b905672009-01-06 14:53:35 -05001095 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001096 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1097 journal->j_wbufsize = n;
1098 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1099 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001100 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001101 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001102 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001103 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104
1105 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001106 if (!bh) {
1107 printk(KERN_ERR
1108 "%s: Cannot get buffer for journal superblock\n",
1109 __func__);
1110 goto out_err;
1111 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001112 journal->j_sb_buffer = bh;
1113 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Kara4b905672009-01-06 14:53:35 -05001114
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001116out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001117 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001118 jbd2_stats_proc_exit(journal);
1119 kfree(journal);
1120 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121}
1122
1123/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001124 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001125 * @inode: An inode to create the journal in
1126 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001127 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128 * the journal. The inode must exist already, must support bmap() and
1129 * must have all data blocks preallocated.
1130 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001131journal_t * jbd2_journal_init_inode (struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001132{
1133 struct buffer_head *bh;
1134 journal_t *journal = journal_init_common();
Theodore Ts'o05496762008-09-16 14:36:17 -04001135 char *p;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136 int err;
1137 int n;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001138 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139
1140 if (!journal)
1141 return NULL;
1142
1143 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1144 journal->j_inode = inode;
Theodore Ts'o05496762008-09-16 14:36:17 -04001145 bdevname(journal->j_dev, journal->j_devname);
1146 p = journal->j_devname;
1147 while ((p = strchr(p, '/')))
1148 *p = '!';
1149 p = journal->j_devname + strlen(journal->j_devname);
Theodore Ts'o90576c02009-09-29 15:51:30 -04001150 sprintf(p, "-%lu", journal->j_inode->i_ino);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001151 jbd_debug(1,
1152 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1153 journal, inode->i_sb->s_id, inode->i_ino,
1154 (long long) inode->i_size,
1155 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1156
1157 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1158 journal->j_blocksize = inode->i_sb->s_blocksize;
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001159 jbd2_stats_proc_init(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160
1161 /* journal descriptor can store up to n blocks -bzzz */
1162 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1163 journal->j_wbufsize = n;
1164 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1165 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001166 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001167 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001168 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169 }
1170
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001171 err = jbd2_journal_bmap(journal, 0, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001172 /* If that failed, give up */
1173 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -08001174 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001175 __func__);
Jan Kara4b905672009-01-06 14:53:35 -05001176 goto out_err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001177 }
1178
1179 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Kara4b905672009-01-06 14:53:35 -05001180 if (!bh) {
1181 printk(KERN_ERR
1182 "%s: Cannot get buffer for journal superblock\n",
1183 __func__);
1184 goto out_err;
1185 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001186 journal->j_sb_buffer = bh;
1187 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1188
1189 return journal;
Jan Kara4b905672009-01-06 14:53:35 -05001190out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +08001191 kfree(journal->j_wbuf);
Jan Kara4b905672009-01-06 14:53:35 -05001192 jbd2_stats_proc_exit(journal);
1193 kfree(journal);
1194 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001195}
1196
1197/*
1198 * If the journal init or create aborts, we need to mark the journal
1199 * superblock as being NULL to prevent the journal destroy from writing
1200 * back a bogus superblock.
1201 */
1202static void journal_fail_superblock (journal_t *journal)
1203{
1204 struct buffer_head *bh = journal->j_sb_buffer;
1205 brelse(bh);
1206 journal->j_sb_buffer = NULL;
1207}
1208
1209/*
1210 * Given a journal_t structure, initialise the various fields for
1211 * startup of a new journaling session. We use this both when creating
1212 * a journal, and after recovering an old journal to reset it for
1213 * subsequent use.
1214 */
1215
1216static int journal_reset(journal_t *journal)
1217{
1218 journal_superblock_t *sb = journal->j_superblock;
Mingming Cao18eba7a2006-10-11 01:21:13 -07001219 unsigned long long first, last;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001220
1221 first = be32_to_cpu(sb->s_first);
1222 last = be32_to_cpu(sb->s_maxlen);
Jan Karaf6f50e22009-07-17 10:40:01 -04001223 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001224 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
Jan Karaf6f50e22009-07-17 10:40:01 -04001225 first, last);
1226 journal_fail_superblock(journal);
1227 return -EINVAL;
1228 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001229
1230 journal->j_first = first;
1231 journal->j_last = last;
1232
1233 journal->j_head = first;
1234 journal->j_tail = first;
1235 journal->j_free = last - first;
1236
1237 journal->j_tail_sequence = journal->j_transaction_sequence;
1238 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1239 journal->j_commit_request = journal->j_commit_sequence;
1240
1241 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1242
Dave Kleikamp470decc2006-10-11 01:20:57 -07001243 /*
1244 * As a special case, if the on-disk copy is already marked as needing
Jan Kara24bcc892012-03-13 15:41:04 -04001245 * no recovery (s_start == 0), then we can safely defer the superblock
1246 * update until the next commit by setting JBD2_FLUSHED. This avoids
Dave Kleikamp470decc2006-10-11 01:20:57 -07001247 * attempting a write to a potential-readonly device.
1248 */
Jan Kara24bcc892012-03-13 15:41:04 -04001249 if (sb->s_start == 0) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001250 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
Dave Kleikamp470decc2006-10-11 01:20:57 -07001251 "(start %ld, seq %d, errno %d)\n",
1252 journal->j_tail, journal->j_tail_sequence,
1253 journal->j_errno);
Jan Kara24bcc892012-03-13 15:41:04 -04001254 journal->j_flags |= JBD2_FLUSHED;
1255 } else {
Jan Karaa78bb112012-03-13 15:43:04 -04001256 /* Lock here to make assertions happy... */
1257 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara79feb522012-03-13 22:22:54 -04001258 /*
1259 * Update log tail information. We use WRITE_FUA since new
1260 * transaction will start reusing journal space and so we
1261 * must make sure information about current log tail is on
1262 * disk before that.
1263 */
1264 jbd2_journal_update_sb_log_tail(journal,
1265 journal->j_tail_sequence,
1266 journal->j_tail,
1267 WRITE_FUA);
Jan Karaa78bb112012-03-13 15:43:04 -04001268 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001269 }
Jan Kara24bcc892012-03-13 15:41:04 -04001270 return jbd2_journal_start_thread(journal);
1271}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001272
Jan Kara79feb522012-03-13 22:22:54 -04001273static void jbd2_write_superblock(journal_t *journal, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001274{
1275 struct buffer_head *bh = journal->j_sb_buffer;
Jan Kara79feb522012-03-13 22:22:54 -04001276 int ret;
Jan Kara24bcc892012-03-13 15:41:04 -04001277
Jan Kara79feb522012-03-13 22:22:54 -04001278 trace_jbd2_write_superblock(journal, write_op);
1279 if (!(journal->j_flags & JBD2_BARRIER))
1280 write_op &= ~(REQ_FUA | REQ_FLUSH);
1281 lock_buffer(bh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04001282 if (buffer_write_io_error(bh)) {
1283 /*
1284 * Oh, dear. A previous attempt to write the journal
1285 * superblock failed. This could happen because the
1286 * USB device was yanked out. Or it could happen to
1287 * be a transient write error and maybe the block will
1288 * be remapped. Nothing we can do but to retry the
1289 * write and hope for the best.
1290 */
1291 printk(KERN_ERR "JBD2: previous I/O error detected "
1292 "for journal superblock update for %s.\n",
1293 journal->j_devname);
1294 clear_buffer_write_io_error(bh);
1295 set_buffer_uptodate(bh);
1296 }
Jan Kara79feb522012-03-13 22:22:54 -04001297 get_bh(bh);
1298 bh->b_end_io = end_buffer_write_sync;
1299 ret = submit_bh(write_op, bh);
1300 wait_on_buffer(bh);
Jan Kara24bcc892012-03-13 15:41:04 -04001301 if (buffer_write_io_error(bh)) {
Jan Kara24bcc892012-03-13 15:41:04 -04001302 clear_buffer_write_io_error(bh);
1303 set_buffer_uptodate(bh);
Jan Kara79feb522012-03-13 22:22:54 -04001304 ret = -EIO;
1305 }
1306 if (ret) {
1307 printk(KERN_ERR "JBD2: Error %d detected when updating "
1308 "journal superblock for %s.\n", ret,
1309 journal->j_devname);
Jan Kara24bcc892012-03-13 15:41:04 -04001310 }
1311}
Theodore Ts'o914258b2008-10-06 21:35:40 -04001312
Jan Kara24bcc892012-03-13 15:41:04 -04001313/**
1314 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1315 * @journal: The journal to update.
Jan Kara79feb522012-03-13 22:22:54 -04001316 * @tail_tid: TID of the new transaction at the tail of the log
1317 * @tail_block: The first block of the transaction at the tail of the log
1318 * @write_op: With which operation should we write the journal sb
Jan Kara24bcc892012-03-13 15:41:04 -04001319 *
1320 * Update a journal's superblock information about log tail and write it to
1321 * disk, waiting for the IO to complete.
1322 */
Jan Kara79feb522012-03-13 22:22:54 -04001323void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1324 unsigned long tail_block, int write_op)
Jan Kara24bcc892012-03-13 15:41:04 -04001325{
1326 journal_superblock_t *sb = journal->j_superblock;
1327
Jan Karaa78bb112012-03-13 15:43:04 -04001328 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Kara79feb522012-03-13 22:22:54 -04001329 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1330 tail_block, tail_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001331
Jan Kara79feb522012-03-13 22:22:54 -04001332 sb->s_sequence = cpu_to_be32(tail_tid);
1333 sb->s_start = cpu_to_be32(tail_block);
Jan Kara24bcc892012-03-13 15:41:04 -04001334
Jan Kara79feb522012-03-13 22:22:54 -04001335 jbd2_write_superblock(journal, write_op);
Jan Kara24bcc892012-03-13 15:41:04 -04001336
1337 /* Log is no longer empty */
1338 write_lock(&journal->j_state_lock);
1339 WARN_ON(!sb->s_sequence);
1340 journal->j_flags &= ~JBD2_FLUSHED;
1341 write_unlock(&journal->j_state_lock);
1342}
1343
1344/**
1345 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1346 * @journal: The journal to update.
1347 *
1348 * Update a journal's dynamic superblock fields to show that journal is empty.
1349 * Write updated superblock to disk waiting for IO to complete.
1350 */
1351static void jbd2_mark_journal_empty(journal_t *journal)
1352{
1353 journal_superblock_t *sb = journal->j_superblock;
1354
Jan Karaa78bb112012-03-13 15:43:04 -04001355 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356 read_lock(&journal->j_state_lock);
Eric Sandeeneeecef02012-08-18 22:29:40 -04001357 /* Is it already empty? */
1358 if (sb->s_start == 0) {
1359 read_unlock(&journal->j_state_lock);
1360 return;
1361 }
Jan Kara24bcc892012-03-13 15:41:04 -04001362 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1363 journal->j_tail_sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001364
1365 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
Jan Kara24bcc892012-03-13 15:41:04 -04001366 sb->s_start = cpu_to_be32(0);
1367 read_unlock(&journal->j_state_lock);
1368
Jan Kara79feb522012-03-13 22:22:54 -04001369 jbd2_write_superblock(journal, WRITE_FUA);
Jan Kara24bcc892012-03-13 15:41:04 -04001370
1371 /* Log is no longer empty */
1372 write_lock(&journal->j_state_lock);
1373 journal->j_flags |= JBD2_FLUSHED;
1374 write_unlock(&journal->j_state_lock);
1375}
1376
1377
1378/**
1379 * jbd2_journal_update_sb_errno() - Update error in the journal.
1380 * @journal: The journal to update.
1381 *
1382 * Update a journal's errno. Write updated superblock to disk waiting for IO
1383 * to complete.
1384 */
Theodore Ts'od796c522012-08-05 19:04:57 -04001385void jbd2_journal_update_sb_errno(journal_t *journal)
Jan Kara24bcc892012-03-13 15:41:04 -04001386{
1387 journal_superblock_t *sb = journal->j_superblock;
1388
1389 read_lock(&journal->j_state_lock);
1390 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1391 journal->j_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001392 sb->s_errno = cpu_to_be32(journal->j_errno);
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001393 jbd2_superblock_csum_set(journal, sb);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001394 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395
Jan Kara79feb522012-03-13 22:22:54 -04001396 jbd2_write_superblock(journal, WRITE_SYNC);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397}
Theodore Ts'od796c522012-08-05 19:04:57 -04001398EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001399
1400/*
1401 * Read the superblock for a given journal, performing initial
1402 * validation of the format.
1403 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001404static int journal_get_superblock(journal_t *journal)
1405{
1406 struct buffer_head *bh;
1407 journal_superblock_t *sb;
1408 int err = -EIO;
1409
1410 bh = journal->j_sb_buffer;
1411
1412 J_ASSERT(bh != NULL);
1413 if (!buffer_uptodate(bh)) {
1414 ll_rw_block(READ, 1, &bh);
1415 wait_on_buffer(bh);
1416 if (!buffer_uptodate(bh)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001417 printk(KERN_ERR
1418 "JBD2: IO error reading journal superblock\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001419 goto out;
1420 }
1421 }
1422
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001423 if (buffer_verified(bh))
1424 return 0;
1425
Dave Kleikamp470decc2006-10-11 01:20:57 -07001426 sb = journal->j_superblock;
1427
1428 err = -EINVAL;
1429
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001430 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001431 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001432 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433 goto out;
1434 }
1435
1436 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001437 case JBD2_SUPERBLOCK_V1:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 journal->j_format_version = 1;
1439 break;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 case JBD2_SUPERBLOCK_V2:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 journal->j_format_version = 2;
1442 break;
1443 default:
Eryu Guanf2a44522011-11-01 19:09:18 -04001444 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001445 goto out;
1446 }
1447
1448 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1449 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1450 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001451 printk(KERN_WARNING "JBD2: journal file too short\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001452 goto out;
1453 }
1454
Eryu Guan87622022011-11-01 19:04:59 -04001455 if (be32_to_cpu(sb->s_first) == 0 ||
1456 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1457 printk(KERN_WARNING
1458 "JBD2: Invalid start block of journal: %u\n",
1459 be32_to_cpu(sb->s_first));
1460 goto out;
1461 }
1462
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001463 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1464 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1465 /* Can't have checksum v1 and v2 on at the same time! */
1466 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1467 "at the same time!\n");
1468 goto out;
1469 }
1470
1471 if (!jbd2_verify_csum_type(journal, sb)) {
1472 printk(KERN_ERR "JBD: Unknown checksum type\n");
1473 goto out;
1474 }
1475
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001476 /* Load the checksum driver */
1477 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1478 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1479 if (IS_ERR(journal->j_chksum_driver)) {
1480 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1481 err = PTR_ERR(journal->j_chksum_driver);
1482 journal->j_chksum_driver = NULL;
1483 goto out;
1484 }
1485 }
1486
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001487 /* Check superblock checksum */
1488 if (!jbd2_superblock_csum_verify(journal, sb)) {
1489 printk(KERN_ERR "JBD: journal checksum error\n");
1490 goto out;
1491 }
1492
1493 /* Precompute checksum seed for all metadata */
1494 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1495 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1496 sizeof(sb->s_uuid));
1497
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001498 set_buffer_verified(bh);
1499
Dave Kleikamp470decc2006-10-11 01:20:57 -07001500 return 0;
1501
1502out:
1503 journal_fail_superblock(journal);
1504 return err;
1505}
1506
1507/*
1508 * Load the on-disk journal superblock and read the key fields into the
1509 * journal_t.
1510 */
1511
1512static int load_superblock(journal_t *journal)
1513{
1514 int err;
1515 journal_superblock_t *sb;
1516
1517 err = journal_get_superblock(journal);
1518 if (err)
1519 return err;
1520
1521 sb = journal->j_superblock;
1522
1523 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1524 journal->j_tail = be32_to_cpu(sb->s_start);
1525 journal->j_first = be32_to_cpu(sb->s_first);
1526 journal->j_last = be32_to_cpu(sb->s_maxlen);
1527 journal->j_errno = be32_to_cpu(sb->s_errno);
1528
1529 return 0;
1530}
1531
1532
1533/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001534 * int jbd2_journal_load() - Read journal from disk.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001535 * @journal: Journal to act on.
1536 *
1537 * Given a journal_t structure which tells us which disk blocks contain
1538 * a journal, read the journal from disk to initialise the in-memory
1539 * structures.
1540 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001541int jbd2_journal_load(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001542{
1543 int err;
1544 journal_superblock_t *sb;
1545
1546 err = load_superblock(journal);
1547 if (err)
1548 return err;
1549
1550 sb = journal->j_superblock;
1551 /* If this is a V2 superblock, then we have to check the
1552 * features flags on it. */
1553
1554 if (journal->j_format_version >= 2) {
1555 if ((sb->s_feature_ro_compat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001556 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
Dave Kleikamp470decc2006-10-11 01:20:57 -07001557 (sb->s_feature_incompat &
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001558 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
Eryu Guanf2a44522011-11-01 19:09:18 -04001559 printk(KERN_WARNING
1560 "JBD2: Unrecognised features on journal\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001561 return -EINVAL;
1562 }
1563 }
1564
Theodore Ts'od2eecb02009-12-07 10:36:20 -05001565 /*
1566 * Create a slab for this blocksize
1567 */
1568 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1569 if (err)
1570 return err;
1571
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 /* Let the recovery code check whether it needs to recover any
1573 * data from the journal. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001574 if (jbd2_journal_recover(journal))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001575 goto recovery_error;
1576
Theodore Ts'oe6a47422009-11-15 15:31:37 -05001577 if (journal->j_failed_commit) {
1578 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1579 "is corrupt.\n", journal->j_failed_commit,
1580 journal->j_devname);
1581 return -EIO;
1582 }
1583
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 /* OK, we've finished with the dynamic journal bits:
1585 * reinitialise the dynamic contents of the superblock in memory
1586 * and reset them on disk. */
1587 if (journal_reset(journal))
1588 goto recovery_error;
1589
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001590 journal->j_flags &= ~JBD2_ABORT;
1591 journal->j_flags |= JBD2_LOADED;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001592 return 0;
1593
1594recovery_error:
Eryu Guanf2a44522011-11-01 19:09:18 -04001595 printk(KERN_WARNING "JBD2: recovery failed\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596 return -EIO;
1597}
1598
1599/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 * void jbd2_journal_destroy() - Release a journal_t structure.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 * @journal: Journal to act on.
1602 *
1603 * Release a journal_t structure once it is no longer in use by the
1604 * journaled object.
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001605 * Return <0 if we couldn't clean up the journal.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001606 */
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001607int jbd2_journal_destroy(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001608{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001609 int err = 0;
1610
Dave Kleikamp470decc2006-10-11 01:20:57 -07001611 /* Wait for the commit thread to wake up and die. */
1612 journal_kill_thread(journal);
1613
1614 /* Force a final log commit */
1615 if (journal->j_running_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001616 jbd2_journal_commit_transaction(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617
1618 /* Force any old transactions to disk */
1619
1620 /* Totally anal locking here... */
1621 spin_lock(&journal->j_list_lock);
1622 while (journal->j_checkpoint_transactions != NULL) {
1623 spin_unlock(&journal->j_list_lock);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001624 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001625 jbd2_log_do_checkpoint(journal);
Theodore Ts'o1a0d3782008-11-05 00:09:22 -05001626 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 spin_lock(&journal->j_list_lock);
1628 }
1629
1630 J_ASSERT(journal->j_running_transaction == NULL);
1631 J_ASSERT(journal->j_committing_transaction == NULL);
1632 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1633 spin_unlock(&journal->j_list_lock);
1634
Dave Kleikamp470decc2006-10-11 01:20:57 -07001635 if (journal->j_sb_buffer) {
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001636 if (!is_journal_aborted(journal)) {
Jan Karaa78bb112012-03-13 15:43:04 -04001637 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001638 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001639 mutex_unlock(&journal->j_checkpoint_mutex);
1640 } else
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001641 err = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 brelse(journal->j_sb_buffer);
1643 }
1644
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001645 if (journal->j_proc_entry)
1646 jbd2_stats_proc_exit(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647 if (journal->j_inode)
1648 iput(journal->j_inode);
1649 if (journal->j_revoke)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001650 jbd2_journal_destroy_revoke(journal);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001651 if (journal->j_chksum_driver)
1652 crypto_free_shash(journal->j_chksum_driver);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001653 kfree(journal->j_wbuf);
1654 kfree(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001655
1656 return err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001657}
1658
1659
1660/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001661 *int jbd2_journal_check_used_features () - Check if features specified are used.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001662 * @journal: Journal to check.
1663 * @compat: bitmask of compatible features
1664 * @ro: bitmask of features that force read-only mount
1665 * @incompat: bitmask of incompatible features
1666 *
1667 * Check whether the journal uses all of a given set of
1668 * features. Return true (non-zero) if it does.
1669 **/
1670
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001671int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001672 unsigned long ro, unsigned long incompat)
1673{
1674 journal_superblock_t *sb;
1675
1676 if (!compat && !ro && !incompat)
1677 return 1;
Patrick J. LoPresti1113e1b2010-07-22 15:04:16 -07001678 /* Load journal superblock if it is not loaded yet. */
1679 if (journal->j_format_version == 0 &&
1680 journal_get_superblock(journal) != 0)
1681 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001682 if (journal->j_format_version == 1)
1683 return 0;
1684
1685 sb = journal->j_superblock;
1686
1687 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1688 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1689 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1690 return 1;
1691
1692 return 0;
1693}
1694
1695/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001696 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001697 * @journal: Journal to check.
1698 * @compat: bitmask of compatible features
1699 * @ro: bitmask of features that force read-only mount
1700 * @incompat: bitmask of incompatible features
1701 *
1702 * Check whether the journaling code supports the use of
1703 * all of a given set of features on this journal. Return true
1704 * (non-zero) if it can. */
1705
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001706int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001707 unsigned long ro, unsigned long incompat)
1708{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 if (!compat && !ro && !incompat)
1710 return 1;
1711
Dave Kleikamp470decc2006-10-11 01:20:57 -07001712 /* We can support any known requested features iff the
1713 * superblock is in version 2. Otherwise we fail to support any
1714 * extended sb features. */
1715
1716 if (journal->j_format_version != 2)
1717 return 0;
1718
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001719 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1720 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1721 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 return 1;
1723
1724 return 0;
1725}
1726
1727/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729 * @journal: Journal to act on.
1730 * @compat: bitmask of compatible features
1731 * @ro: bitmask of features that force read-only mount
1732 * @incompat: bitmask of incompatible features
1733 *
1734 * Mark a given journal feature as present on the
1735 * superblock. Returns true if the requested features could be set.
1736 *
1737 */
1738
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001739int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001740 unsigned long ro, unsigned long incompat)
1741{
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001742#define INCOMPAT_FEATURE_ON(f) \
1743 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1744#define COMPAT_FEATURE_ON(f) \
1745 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001746 journal_superblock_t *sb;
1747
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001748 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749 return 1;
1750
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001751 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
Dave Kleikamp470decc2006-10-11 01:20:57 -07001752 return 0;
1753
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001754 /* Asking for checksumming v2 and v1? Only give them v2. */
1755 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1756 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1757 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1758
Dave Kleikamp470decc2006-10-11 01:20:57 -07001759 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1760 compat, ro, incompat);
1761
1762 sb = journal->j_superblock;
1763
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001764 /* If enabling v2 checksums, update superblock */
1765 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1766 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1767 sb->s_feature_compat &=
1768 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
Darrick J. Wong01b5adc2012-05-27 07:50:56 -04001769
1770 /* Load the checksum driver */
1771 if (journal->j_chksum_driver == NULL) {
1772 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1773 0, 0);
1774 if (IS_ERR(journal->j_chksum_driver)) {
1775 printk(KERN_ERR "JBD: Cannot load crc32c "
1776 "driver.\n");
1777 journal->j_chksum_driver = NULL;
1778 return 0;
1779 }
1780 }
Darrick J. Wong4fd5ea42012-05-27 08:08:22 -04001781
1782 /* Precompute checksum seed for all metadata */
1783 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1784 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1785 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1786 sb->s_uuid,
1787 sizeof(sb->s_uuid));
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001788 }
1789
1790 /* If enabling v1 checksums, downgrade superblock */
1791 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1792 sb->s_feature_incompat &=
1793 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1794
Dave Kleikamp470decc2006-10-11 01:20:57 -07001795 sb->s_feature_compat |= cpu_to_be32(compat);
1796 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1797 sb->s_feature_incompat |= cpu_to_be32(incompat);
1798
1799 return 1;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04001800#undef COMPAT_FEATURE_ON
1801#undef INCOMPAT_FEATURE_ON
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802}
1803
Girish Shilamkar818d2762008-01-28 23:58:27 -05001804/*
1805 * jbd2_journal_clear_features () - Clear a given journal feature in the
1806 * superblock
1807 * @journal: Journal to act on.
1808 * @compat: bitmask of compatible features
1809 * @ro: bitmask of features that force read-only mount
1810 * @incompat: bitmask of incompatible features
1811 *
1812 * Clear a given journal feature as present on the
1813 * superblock.
1814 */
1815void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1816 unsigned long ro, unsigned long incompat)
1817{
1818 journal_superblock_t *sb;
1819
1820 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1821 compat, ro, incompat);
1822
1823 sb = journal->j_superblock;
1824
1825 sb->s_feature_compat &= ~cpu_to_be32(compat);
1826 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1827 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1828}
1829EXPORT_SYMBOL(jbd2_journal_clear_features);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001830
1831/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001832 * int jbd2_journal_flush () - Flush journal
Dave Kleikamp470decc2006-10-11 01:20:57 -07001833 * @journal: Journal to act on.
1834 *
1835 * Flush all data for a given journal to disk and empty the journal.
1836 * Filesystems can use this when remounting readonly to ensure that
1837 * recovery does not need to happen on remount.
1838 */
1839
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001840int jbd2_journal_flush(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841{
1842 int err = 0;
1843 transaction_t *transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001844
Theodore Ts'oa931da62010-08-03 21:35:12 -04001845 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001846
1847 /* Force everything buffered to the log... */
1848 if (journal->j_running_transaction) {
1849 transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001850 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001851 } else if (journal->j_committing_transaction)
1852 transaction = journal->j_committing_transaction;
1853
1854 /* Wait for the log commit to complete... */
1855 if (transaction) {
1856 tid_t tid = transaction->t_tid;
1857
Theodore Ts'oa931da62010-08-03 21:35:12 -04001858 write_unlock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001859 jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001860 } else {
Theodore Ts'oa931da62010-08-03 21:35:12 -04001861 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001862 }
1863
1864 /* ...and flush everything in the log out to disk. */
1865 spin_lock(&journal->j_list_lock);
1866 while (!err && journal->j_checkpoint_transactions != NULL) {
1867 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001868 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001869 err = jbd2_log_do_checkpoint(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001870 mutex_unlock(&journal->j_checkpoint_mutex);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871 spin_lock(&journal->j_list_lock);
1872 }
1873 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001874
1875 if (is_journal_aborted(journal))
1876 return -EIO;
1877
Jan Karaa78bb112012-03-13 15:43:04 -04001878 mutex_lock(&journal->j_checkpoint_mutex);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001879 jbd2_cleanup_journal_tail(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880
1881 /* Finally, mark the journal as really needing no recovery.
1882 * This sets s_start==0 in the underlying superblock, which is
1883 * the magic code for a fully-recovered superblock. Any future
1884 * commits of data to the journal will restore the current
1885 * s_start value. */
Jan Kara24bcc892012-03-13 15:41:04 -04001886 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001887 mutex_unlock(&journal->j_checkpoint_mutex);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001888 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889 J_ASSERT(!journal->j_running_transaction);
1890 J_ASSERT(!journal->j_committing_transaction);
1891 J_ASSERT(!journal->j_checkpoint_transactions);
1892 J_ASSERT(journal->j_head == journal->j_tail);
1893 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001894 write_unlock(&journal->j_state_lock);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -04001895 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001896}
1897
1898/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001899 * int jbd2_journal_wipe() - Wipe journal contents
Dave Kleikamp470decc2006-10-11 01:20:57 -07001900 * @journal: Journal to act on.
1901 * @write: flag (see below)
1902 *
1903 * Wipe out all of the contents of a journal, safely. This will produce
1904 * a warning if the journal contains any valid recovery information.
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001905 * Must be called between journal_init_*() and jbd2_journal_load().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001906 *
1907 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1908 * we merely suppress recovery.
1909 */
1910
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001911int jbd2_journal_wipe(journal_t *journal, int write)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001912{
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 int err = 0;
1914
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001915 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001916
1917 err = load_superblock(journal);
1918 if (err)
1919 return err;
1920
Dave Kleikamp470decc2006-10-11 01:20:57 -07001921 if (!journal->j_tail)
1922 goto no_recovery;
1923
Eryu Guanf2a44522011-11-01 19:09:18 -04001924 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925 write ? "Clearing" : "Ignoring");
1926
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001927 err = jbd2_journal_skip_recovery(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001928 if (write) {
1929 /* Lock to make assertions happy... */
1930 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara24bcc892012-03-13 15:41:04 -04001931 jbd2_mark_journal_empty(journal);
Jan Karaa78bb112012-03-13 15:43:04 -04001932 mutex_unlock(&journal->j_checkpoint_mutex);
1933 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001934
1935 no_recovery:
1936 return err;
1937}
1938
1939/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07001940 * Journal abort has very specific semantics, which we describe
1941 * for journal abort.
1942 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04001943 * Two internal functions, which provide abort to the jbd layer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001944 * itself are here.
1945 */
1946
1947/*
1948 * Quick version for internal journal use (doesn't lock the journal).
1949 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1950 * and don't attempt to make any other journal updates.
1951 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001952void __jbd2_journal_abort_hard(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001953{
1954 transaction_t *transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001955
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001956 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 return;
1958
1959 printk(KERN_ERR "Aborting journal on device %s.\n",
Theodore Ts'o05496762008-09-16 14:36:17 -04001960 journal->j_devname);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961
Theodore Ts'oa931da62010-08-03 21:35:12 -04001962 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001963 journal->j_flags |= JBD2_ABORT;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 transaction = journal->j_running_transaction;
1965 if (transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001967 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968}
1969
1970/* Soft abort: record the abort error status in the journal superblock,
1971 * but don't do any other IO. */
1972static void __journal_abort_soft (journal_t *journal, int errno)
1973{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001974 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975 return;
1976
1977 if (!journal->j_errno)
1978 journal->j_errno = errno;
1979
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001980 __jbd2_journal_abort_hard(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001981
1982 if (errno)
Jan Kara24bcc892012-03-13 15:41:04 -04001983 jbd2_journal_update_sb_errno(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984}
1985
1986/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001987 * void jbd2_journal_abort () - Shutdown the journal immediately.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988 * @journal: the journal to shutdown.
1989 * @errno: an error number to record in the journal indicating
1990 * the reason for the shutdown.
1991 *
1992 * Perform a complete, immediate shutdown of the ENTIRE
1993 * journal (not of a single transaction). This operation cannot be
1994 * undone without closing and reopening the journal.
1995 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001996 * The jbd2_journal_abort function is intended to support higher level error
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1998 * mode.
1999 *
2000 * Journal abort has very specific semantics. Any existing dirty,
2001 * unjournaled buffers in the main filesystem will still be written to
2002 * disk by bdflush, but the journaling mechanism will be suspended
2003 * immediately and no further transaction commits will be honoured.
2004 *
2005 * Any dirty, journaled buffers will be written back to disk without
2006 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2007 * filesystem, but we _do_ attempt to leave as much data as possible
2008 * behind for fsck to use for cleanup.
2009 *
2010 * Any attempt to get a new transaction handle on a journal which is in
2011 * ABORT state will just result in an -EROFS error return. A
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002012 * jbd2_journal_stop on an existing handle will return -EIO if we have
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 * entered abort state during the update.
2014 *
2015 * Recursive transactions are not disturbed by journal abort until the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002016 * final jbd2_journal_stop, which will receive the -EIO error.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002018 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
Dave Kleikamp470decc2006-10-11 01:20:57 -07002019 * which will be recorded (if possible) in the journal superblock. This
2020 * allows a client to record failure conditions in the middle of a
2021 * transaction without having to complete the transaction to record the
2022 * failure to disk. ext3_error, for example, now uses this
2023 * functionality.
2024 *
2025 * Errors which originate from within the journaling layer will NOT
2026 * supply an errno; a null errno implies that absolutely no further
2027 * writes are done to the journal (unless there are any already in
2028 * progress).
2029 *
2030 */
2031
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032void jbd2_journal_abort(journal_t *journal, int errno)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033{
2034 __journal_abort_soft(journal, errno);
2035}
2036
2037/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002038 * int jbd2_journal_errno () - returns the journal's error state.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002039 * @journal: journal to examine.
2040 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002041 * This is the errno number set with jbd2_journal_abort(), the last
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 * time the journal was mounted - if the journal was stopped
2043 * without calling abort this will be 0.
2044 *
2045 * If the journal has been aborted on this mount time -EROFS will
2046 * be returned.
2047 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002048int jbd2_journal_errno(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049{
2050 int err;
2051
Theodore Ts'oa931da62010-08-03 21:35:12 -04002052 read_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 err = -EROFS;
2055 else
2056 err = journal->j_errno;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002057 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 return err;
2059}
2060
2061/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002062 * int jbd2_journal_clear_err () - clears the journal's error state
Dave Kleikamp470decc2006-10-11 01:20:57 -07002063 * @journal: journal to act on.
2064 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002065 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002066 * mode.
2067 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002068int jbd2_journal_clear_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002069{
2070 int err = 0;
2071
Theodore Ts'oa931da62010-08-03 21:35:12 -04002072 write_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002073 if (journal->j_flags & JBD2_ABORT)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002074 err = -EROFS;
2075 else
2076 journal->j_errno = 0;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002077 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002078 return err;
2079}
2080
2081/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002082 * void jbd2_journal_ack_err() - Ack journal err.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002083 * @journal: journal to act on.
2084 *
Alberto Bertoglibfcd3552009-06-09 00:06:20 -04002085 * An error must be cleared or acked to take a FS out of readonly
Dave Kleikamp470decc2006-10-11 01:20:57 -07002086 * mode.
2087 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002088void jbd2_journal_ack_err(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002089{
Theodore Ts'oa931da62010-08-03 21:35:12 -04002090 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002091 if (journal->j_errno)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002092 journal->j_flags |= JBD2_ACK_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002093 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094}
2095
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002096int jbd2_journal_blocks_per_page(struct inode *inode)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097{
2098 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2099}
2100
2101/*
Zach Brownb517bea2006-10-11 01:21:08 -07002102 * helper functions to deal with 32 or 64bit block numbers.
2103 */
2104size_t journal_tag_bytes(journal_t *journal)
2105{
Darrick J. Wongc3900872012-05-27 08:12:12 -04002106 journal_block_tag_t tag;
2107 size_t x = 0;
2108
2109 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2110 x += sizeof(tag.t_checksum);
2111
Zach Brownb517bea2006-10-11 01:21:08 -07002112 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
Darrick J. Wongc3900872012-05-27 08:12:12 -04002113 return x + JBD2_TAG_SIZE64;
Zach Brownb517bea2006-10-11 01:21:08 -07002114 else
Darrick J. Wongc3900872012-05-27 08:12:12 -04002115 return x + JBD2_TAG_SIZE32;
Zach Brownb517bea2006-10-11 01:21:08 -07002116}
2117
2118/*
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002119 * JBD memory management
2120 *
2121 * These functions are used to allocate block-sized chunks of memory
2122 * used for making copies of buffer_head data. Very often it will be
2123 * page-sized chunks of data, but sometimes it will be in
2124 * sub-page-size chunks. (For example, 16k pages on Power systems
2125 * with a 4k block file system.) For blocks smaller than a page, we
2126 * use a SLAB allocator. There are slab caches for each block size,
2127 * which are allocated at mount time, if necessary, and we only free
2128 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2129 * this reason we don't need to a mutex to protect access to
2130 * jbd2_slab[] allocating or releasing memory; only in
2131 * jbd2_journal_create_slab().
2132 */
2133#define JBD2_MAX_SLABS 8
2134static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002135
2136static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2137 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2138 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2139};
2140
2141
2142static void jbd2_journal_destroy_slabs(void)
2143{
2144 int i;
2145
2146 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2147 if (jbd2_slab[i])
2148 kmem_cache_destroy(jbd2_slab[i]);
2149 jbd2_slab[i] = NULL;
2150 }
2151}
2152
2153static int jbd2_journal_create_slab(size_t size)
2154{
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002155 static DEFINE_MUTEX(jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002156 int i = order_base_2(size) - 10;
2157 size_t slab_size;
2158
2159 if (size == PAGE_SIZE)
2160 return 0;
2161
2162 if (i >= JBD2_MAX_SLABS)
2163 return -EINVAL;
2164
2165 if (unlikely(i < 0))
2166 i = 0;
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002167 mutex_lock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002168 if (jbd2_slab[i]) {
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002169 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002170 return 0; /* Already created */
2171 }
2172
2173 slab_size = 1 << (i+10);
2174 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2175 slab_size, 0, NULL);
Thomas Gleixner51dfacde2010-10-16 22:34:39 +02002176 mutex_unlock(&jbd2_slab_create_mutex);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002177 if (!jbd2_slab[i]) {
2178 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2179 return -ENOMEM;
2180 }
2181 return 0;
2182}
2183
2184static struct kmem_cache *get_slab(size_t size)
2185{
2186 int i = order_base_2(size) - 10;
2187
2188 BUG_ON(i >= JBD2_MAX_SLABS);
2189 if (unlikely(i < 0))
2190 i = 0;
Bill Pemberton8ac97b72010-04-30 09:34:31 -04002191 BUG_ON(jbd2_slab[i] == NULL);
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002192 return jbd2_slab[i];
2193}
2194
2195void *jbd2_alloc(size_t size, gfp_t flags)
2196{
2197 void *ptr;
2198
2199 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2200
2201 flags |= __GFP_REPEAT;
2202 if (size == PAGE_SIZE)
2203 ptr = (void *)__get_free_pages(flags, 0);
2204 else if (size > PAGE_SIZE) {
2205 int order = get_order(size);
2206
2207 if (order < 3)
2208 ptr = (void *)__get_free_pages(flags, order);
2209 else
2210 ptr = vmalloc(size);
2211 } else
2212 ptr = kmem_cache_alloc(get_slab(size), flags);
2213
2214 /* Check alignment; SLUB has gotten this wrong in the past,
2215 * and this can lead to user data corruption! */
2216 BUG_ON(((unsigned long) ptr) & (size-1));
2217
2218 return ptr;
2219}
2220
2221void jbd2_free(void *ptr, size_t size)
2222{
2223 if (size == PAGE_SIZE) {
2224 free_pages((unsigned long)ptr, 0);
2225 return;
2226 }
2227 if (size > PAGE_SIZE) {
2228 int order = get_order(size);
2229
2230 if (order < 3)
2231 free_pages((unsigned long)ptr, order);
2232 else
2233 vfree(ptr);
2234 return;
2235 }
2236 kmem_cache_free(get_slab(size), ptr);
2237};
2238
2239/*
Dave Kleikamp470decc2006-10-11 01:20:57 -07002240 * Journal_head storage management
2241 */
Christoph Lametere18b8902006-12-06 20:33:20 -08002242static struct kmem_cache *jbd2_journal_head_cache;
Jose R. Santose23291b2007-07-18 08:57:06 -04002243#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002244static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2245#endif
2246
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002247static int jbd2_journal_init_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002248{
2249 int retval;
2250
Al Viro1076d172008-03-29 03:07:18 +00002251 J_ASSERT(jbd2_journal_head_cache == NULL);
Johann Lombardia920e942006-10-11 01:21:00 -07002252 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
Dave Kleikamp470decc2006-10-11 01:20:57 -07002253 sizeof(struct journal_head),
2254 0, /* offset */
Mingming Cao77160952008-01-28 23:58:27 -05002255 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002256 NULL); /* ctor */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00002258 if (!jbd2_journal_head_cache) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002259 retval = -ENOMEM;
Eryu Guanf2a44522011-11-01 19:09:18 -04002260 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002261 }
2262 return retval;
2263}
2264
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002265static void jbd2_journal_destroy_journal_head_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002266{
Duane Griffin8a9362e2008-04-17 10:38:59 -04002267 if (jbd2_journal_head_cache) {
2268 kmem_cache_destroy(jbd2_journal_head_cache);
2269 jbd2_journal_head_cache = NULL;
2270 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002271}
2272
2273/*
2274 * journal_head splicing and dicing
2275 */
2276static struct journal_head *journal_alloc_journal_head(void)
2277{
2278 struct journal_head *ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002279
Jose R. Santose23291b2007-07-18 08:57:06 -04002280#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002281 atomic_inc(&nr_journal_heads);
2282#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002283 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Al Viro1076d172008-03-29 03:07:18 +00002284 if (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002285 jbd_debug(1, "out of memory for journal_head\n");
Theodore Ts'o670be5a2010-12-17 10:44:16 -05002286 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
Al Viro1076d172008-03-29 03:07:18 +00002287 while (!ret) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288 yield();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002289 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002290 }
2291 }
2292 return ret;
2293}
2294
2295static void journal_free_journal_head(struct journal_head *jh)
2296{
Jose R. Santose23291b2007-07-18 08:57:06 -04002297#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002298 atomic_dec(&nr_journal_heads);
Mingming Caocd02ff02007-10-16 18:38:25 -04002299 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -07002300#endif
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002301 kmem_cache_free(jbd2_journal_head_cache, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002302}
2303
2304/*
2305 * A journal_head is attached to a buffer_head whenever JBD has an
2306 * interest in the buffer.
2307 *
2308 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2309 * is set. This bit is tested in core kernel code where we need to take
2310 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2311 * there.
2312 *
2313 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2314 *
2315 * When a buffer has its BH_JBD bit set it is immune from being released by
2316 * core kernel code, mainly via ->b_count.
2317 *
Jan Karade1b7942011-06-13 15:38:22 -04002318 * A journal_head is detached from its buffer_head when the journal_head's
2319 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2320 * transaction (b_cp_transaction) hold their references to b_jcount.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002321 *
2322 * Various places in the kernel want to attach a journal_head to a buffer_head
2323 * _before_ attaching the journal_head to a transaction. To protect the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002324 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
Dave Kleikamp470decc2006-10-11 01:20:57 -07002325 * journal_head's b_jcount refcount by one. The caller must call
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002326 * jbd2_journal_put_journal_head() to undo this.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002327 *
2328 * So the typical usage would be:
2329 *
2330 * (Attach a journal_head if needed. Increments b_jcount)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002331 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002332 * ...
Jan Karade1b7942011-06-13 15:38:22 -04002333 * (Get another reference for transaction)
2334 * jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002335 * jh->b_transaction = xxx;
Jan Karade1b7942011-06-13 15:38:22 -04002336 * (Put original reference)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002337 * jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002338 */
2339
2340/*
2341 * Give a buffer_head a journal_head.
2342 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07002343 * May sleep.
2344 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002345struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002346{
2347 struct journal_head *jh;
2348 struct journal_head *new_jh = NULL;
2349
2350repeat:
2351 if (!buffer_jbd(bh)) {
2352 new_jh = journal_alloc_journal_head();
2353 memset(new_jh, 0, sizeof(*new_jh));
2354 }
2355
2356 jbd_lock_bh_journal_head(bh);
2357 if (buffer_jbd(bh)) {
2358 jh = bh2jh(bh);
2359 } else {
2360 J_ASSERT_BH(bh,
2361 (atomic_read(&bh->b_count) > 0) ||
2362 (bh->b_page && bh->b_page->mapping));
2363
2364 if (!new_jh) {
2365 jbd_unlock_bh_journal_head(bh);
2366 goto repeat;
2367 }
2368
2369 jh = new_jh;
2370 new_jh = NULL; /* We consumed it */
2371 set_buffer_jbd(bh);
2372 bh->b_private = jh;
2373 jh->b_bh = bh;
2374 get_bh(bh);
2375 BUFFER_TRACE(bh, "added journal_head");
2376 }
2377 jh->b_jcount++;
2378 jbd_unlock_bh_journal_head(bh);
2379 if (new_jh)
2380 journal_free_journal_head(new_jh);
2381 return bh->b_private;
2382}
2383
2384/*
2385 * Grab a ref against this buffer_head's journal_head. If it ended up not
2386 * having a journal_head, return NULL
2387 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002388struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002389{
2390 struct journal_head *jh = NULL;
2391
2392 jbd_lock_bh_journal_head(bh);
2393 if (buffer_jbd(bh)) {
2394 jh = bh2jh(bh);
2395 jh->b_jcount++;
2396 }
2397 jbd_unlock_bh_journal_head(bh);
2398 return jh;
2399}
2400
2401static void __journal_remove_journal_head(struct buffer_head *bh)
2402{
2403 struct journal_head *jh = bh2jh(bh);
2404
2405 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karade1b7942011-06-13 15:38:22 -04002406 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2407 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2408 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2409 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2410 J_ASSERT_BH(bh, buffer_jbd(bh));
2411 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2412 BUFFER_TRACE(bh, "remove journal_head");
2413 if (jh->b_frozen_data) {
2414 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2415 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002416 }
Jan Karade1b7942011-06-13 15:38:22 -04002417 if (jh->b_committed_data) {
2418 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2419 jbd2_free(jh->b_committed_data, bh->b_size);
2420 }
2421 bh->b_private = NULL;
2422 jh->b_bh = NULL; /* debug, really */
2423 clear_buffer_jbd(bh);
2424 journal_free_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002425}
2426
2427/*
Jan Karade1b7942011-06-13 15:38:22 -04002428 * Drop a reference on the passed journal_head. If it fell to zero then
Dave Kleikamp470decc2006-10-11 01:20:57 -07002429 * release the journal_head from the buffer_head.
2430 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002431void jbd2_journal_put_journal_head(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002432{
2433 struct buffer_head *bh = jh2bh(jh);
2434
2435 jbd_lock_bh_journal_head(bh);
2436 J_ASSERT_JH(jh, jh->b_jcount > 0);
2437 --jh->b_jcount;
Jan Karade1b7942011-06-13 15:38:22 -04002438 if (!jh->b_jcount) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002439 __journal_remove_journal_head(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002440 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002441 __brelse(bh);
Jan Karade1b7942011-06-13 15:38:22 -04002442 } else
2443 jbd_unlock_bh_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002444}
2445
2446/*
Jan Karac851ed52008-07-11 19:27:31 -04002447 * Initialize jbd inode head
2448 */
2449void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2450{
2451 jinode->i_transaction = NULL;
2452 jinode->i_next_transaction = NULL;
2453 jinode->i_vfs_inode = inode;
2454 jinode->i_flags = 0;
2455 INIT_LIST_HEAD(&jinode->i_list);
2456}
2457
2458/*
2459 * Function to be called before we start removing inode from memory (i.e.,
2460 * clear_inode() is a fine place to be called from). It removes inode from
2461 * transaction's lists.
2462 */
2463void jbd2_journal_release_jbd_inode(journal_t *journal,
2464 struct jbd2_inode *jinode)
2465{
Jan Karac851ed52008-07-11 19:27:31 -04002466 if (!journal)
2467 return;
2468restart:
2469 spin_lock(&journal->j_list_lock);
2470 /* Is commit writing out inode - we have to wait */
Brian King39e3ac22010-10-27 21:25:12 -04002471 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
Jan Karac851ed52008-07-11 19:27:31 -04002472 wait_queue_head_t *wq;
2473 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2474 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2475 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2476 spin_unlock(&journal->j_list_lock);
2477 schedule();
2478 finish_wait(wq, &wait.wait);
2479 goto restart;
2480 }
2481
Jan Karac851ed52008-07-11 19:27:31 -04002482 if (jinode->i_transaction) {
2483 list_del(&jinode->i_list);
2484 jinode->i_transaction = NULL;
2485 }
2486 spin_unlock(&journal->j_list_lock);
2487}
2488
2489/*
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002490 * debugfs tunables
Dave Kleikamp470decc2006-10-11 01:20:57 -07002491 */
Jose R. Santos6f38c742007-10-16 18:38:25 -04002492#ifdef CONFIG_JBD2_DEBUG
2493u8 jbd2_journal_enable_debug __read_mostly;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002494EXPORT_SYMBOL(jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002495
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002496#define JBD2_DEBUG_NAME "jbd2-debug"
Dave Kleikamp470decc2006-10-11 01:20:57 -07002497
Jose R. Santos6f38c742007-10-16 18:38:25 -04002498static struct dentry *jbd2_debugfs_dir;
2499static struct dentry *jbd2_debug;
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002500
2501static void __init jbd2_create_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002502{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002503 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2504 if (jbd2_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002505 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2506 S_IRUGO | S_IWUSR,
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002507 jbd2_debugfs_dir,
2508 &jbd2_journal_enable_debug);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002509}
2510
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002511static void __exit jbd2_remove_debugfs_entry(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002512{
Jose R. Santos6f38c742007-10-16 18:38:25 -04002513 debugfs_remove(jbd2_debug);
2514 debugfs_remove(jbd2_debugfs_dir);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002515}
2516
2517#else
2518
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002519static void __init jbd2_create_debugfs_entry(void)
2520{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002521}
2522
2523static void __exit jbd2_remove_debugfs_entry(void)
2524{
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002525}
Dave Kleikamp470decc2006-10-11 01:20:57 -07002526
2527#endif
2528
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002529#ifdef CONFIG_PROC_FS
2530
2531#define JBD2_STATS_PROC_NAME "fs/jbd2"
2532
2533static void __init jbd2_create_jbd_stats_proc_entry(void)
2534{
2535 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2536}
2537
2538static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2539{
2540 if (proc_jbd2_stats)
2541 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2542}
2543
2544#else
2545
2546#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2547#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2548
2549#endif
2550
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002551struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002552
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002553static int __init jbd2_journal_init_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002554{
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002555 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002556 if (jbd2_handle_cache == NULL) {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002557 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2558 return -ENOMEM;
2559 }
2560 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2561 if (jbd2_inode_cache == NULL) {
2562 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2563 kmem_cache_destroy(jbd2_handle_cache);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002564 return -ENOMEM;
2565 }
2566 return 0;
2567}
2568
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002569static void jbd2_journal_destroy_handle_cache(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002570{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002571 if (jbd2_handle_cache)
2572 kmem_cache_destroy(jbd2_handle_cache);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05002573 if (jbd2_inode_cache)
2574 kmem_cache_destroy(jbd2_inode_cache);
2575
Dave Kleikamp470decc2006-10-11 01:20:57 -07002576}
2577
2578/*
2579 * Module startup and shutdown
2580 */
2581
2582static int __init journal_init_caches(void)
2583{
2584 int ret;
2585
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002586 ret = jbd2_journal_init_revoke_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002587 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002588 ret = jbd2_journal_init_journal_head_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002589 if (ret == 0)
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002590 ret = jbd2_journal_init_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002591 if (ret == 0)
2592 ret = jbd2_journal_init_transaction_cache();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002593 return ret;
2594}
2595
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002596static void jbd2_journal_destroy_caches(void)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002597{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002598 jbd2_journal_destroy_revoke_caches();
Yongqiang Yang4185a2a2012-02-20 17:53:03 -05002599 jbd2_journal_destroy_journal_head_cache();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002600 jbd2_journal_destroy_handle_cache();
Yongqiang Yang0c2022e2012-02-20 17:53:02 -05002601 jbd2_journal_destroy_transaction_cache();
Theodore Ts'od2eecb02009-12-07 10:36:20 -05002602 jbd2_journal_destroy_slabs();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002603}
2604
2605static int __init journal_init(void)
2606{
2607 int ret;
2608
2609 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2610
2611 ret = journal_init_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002612 if (ret == 0) {
2613 jbd2_create_debugfs_entry();
2614 jbd2_create_jbd_stats_proc_entry();
2615 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002616 jbd2_journal_destroy_caches();
Duane Griffin620de4e2008-04-29 22:02:47 -04002617 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07002618 return ret;
2619}
2620
2621static void __exit journal_exit(void)
2622{
Jose R. Santose23291b2007-07-18 08:57:06 -04002623#ifdef CONFIG_JBD2_DEBUG
Dave Kleikamp470decc2006-10-11 01:20:57 -07002624 int n = atomic_read(&nr_journal_heads);
2625 if (n)
Eryu Guanf2a44522011-11-01 19:09:18 -04002626 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002627#endif
Jose R. Santos0f49d5d2007-07-18 08:50:18 -04002628 jbd2_remove_debugfs_entry();
Johann Lombardi8e85fb32008-01-28 23:58:27 -05002629 jbd2_remove_jbd_stats_proc_entry();
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002630 jbd2_journal_destroy_caches();
Dave Kleikamp470decc2006-10-11 01:20:57 -07002631}
2632
Dave Kleikamp470decc2006-10-11 01:20:57 -07002633MODULE_LICENSE("GPL");
2634module_init(journal_init);
2635module_exit(journal_exit);
2636