blob: 81880c6d6b5e68dc1609933a2e3f56aa24034a4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/fs/jbd/journal.c
Linus Torvalds1da177e2005-04-16 15:20:36 -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>
28#include <linux/jbd.h>
29#include <linux/errno.h>
30#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/pagemap.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080035#include <linux/kthread.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070036#include <linux/poison.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080037#include <linux/proc_fs.h>
Jose R. Santosc2a91592007-10-18 23:39:22 -070038#include <linux/debugfs.h>
Namhyung Kimf81e3d42010-10-04 19:12:13 +090039#include <linux/ratelimit.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080040
Lukas Czerner99cb1a32011-05-23 18:33:02 +020041#define CREATE_TRACE_POINTS
42#include <trace/events/jbd.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47EXPORT_SYMBOL(journal_start);
48EXPORT_SYMBOL(journal_restart);
49EXPORT_SYMBOL(journal_extend);
50EXPORT_SYMBOL(journal_stop);
51EXPORT_SYMBOL(journal_lock_updates);
52EXPORT_SYMBOL(journal_unlock_updates);
53EXPORT_SYMBOL(journal_get_write_access);
54EXPORT_SYMBOL(journal_get_create_access);
55EXPORT_SYMBOL(journal_get_undo_access);
56EXPORT_SYMBOL(journal_dirty_data);
57EXPORT_SYMBOL(journal_dirty_metadata);
58EXPORT_SYMBOL(journal_release_buffer);
59EXPORT_SYMBOL(journal_forget);
60#if 0
61EXPORT_SYMBOL(journal_sync_buffer);
62#endif
63EXPORT_SYMBOL(journal_flush);
64EXPORT_SYMBOL(journal_revoke);
65
66EXPORT_SYMBOL(journal_init_dev);
67EXPORT_SYMBOL(journal_init_inode);
68EXPORT_SYMBOL(journal_update_format);
69EXPORT_SYMBOL(journal_check_used_features);
70EXPORT_SYMBOL(journal_check_available_features);
71EXPORT_SYMBOL(journal_set_features);
72EXPORT_SYMBOL(journal_create);
73EXPORT_SYMBOL(journal_load);
74EXPORT_SYMBOL(journal_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL(journal_abort);
76EXPORT_SYMBOL(journal_errno);
77EXPORT_SYMBOL(journal_ack_err);
78EXPORT_SYMBOL(journal_clear_err);
79EXPORT_SYMBOL(log_wait_commit);
Stefan Schmidtff5e4b52009-11-12 09:53:50 +010080EXPORT_SYMBOL(log_start_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081EXPORT_SYMBOL(journal_start_commit);
82EXPORT_SYMBOL(journal_force_commit_nested);
83EXPORT_SYMBOL(journal_wipe);
84EXPORT_SYMBOL(journal_blocks_per_page);
85EXPORT_SYMBOL(journal_invalidatepage);
86EXPORT_SYMBOL(journal_try_to_free_buffers);
87EXPORT_SYMBOL(journal_force_commit);
88
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
Adrian Bunk022a4a72005-09-06 15:16:41 -070090static void __journal_abort_soft (journal_t *journal, int errno);
Darrick J. Wongdff68252010-10-04 12:35:05 -070091static const char *journal_dev_name(journal_t *journal, char *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * Helper function used to manage commit timeouts
95 */
96
97static void commit_timeout(unsigned long __data)
98{
99 struct task_struct * p = (struct task_struct *) __data;
100
101 wake_up_process(p);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * kjournald: The main thread function used to manage a logging device
106 * journal.
107 *
108 * This kernel thread is responsible for two things:
109 *
110 * 1) COMMIT: Every so often we need to commit the current state of the
111 * filesystem to disk. The journal thread is responsible for writing
112 * all of the metadata buffers to disk.
113 *
114 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115 * of the data in that part of the log has been rewritten elsewhere on
116 * the disk. Flushing these old buffers to reclaim space in the log is
117 * known as checkpointing, and this thread is responsible for that job.
118 */
119
Adrian Bunk022a4a72005-09-06 15:16:41 -0700120static int kjournald(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Andrew Mortone3df1892006-03-25 03:06:53 -0800122 journal_t *journal = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 transaction_t *transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Andrew Mortone3df1892006-03-25 03:06:53 -0800125 /*
126 * Set up an interval timer which can be used to trigger a commit wakeup
127 * after the commit interval expires
128 */
129 setup_timer(&journal->j_commit_timer, commit_timeout,
130 (unsigned long)current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Nigel Cunningham35c80422012-02-03 19:59:41 +1100132 set_freezable();
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Record that the journal thread is running */
135 journal->j_task = current;
136 wake_up(&journal->j_wait_done_commit);
137
138 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
139 journal->j_commit_interval / HZ);
140
141 /*
142 * And now, wait forever for commit wakeup events.
143 */
144 spin_lock(&journal->j_state_lock);
145
146loop:
147 if (journal->j_flags & JFS_UNMOUNT)
148 goto end_loop;
149
150 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
151 journal->j_commit_sequence, journal->j_commit_request);
152
153 if (journal->j_commit_sequence != journal->j_commit_request) {
154 jbd_debug(1, "OK, requests differ\n");
155 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800156 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 journal_commit_transaction(journal);
158 spin_lock(&journal->j_state_lock);
159 goto loop;
160 }
161
162 wake_up(&journal->j_wait_done_commit);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700163 if (freezing(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * The simpler the better. Flushing journal isn't a
166 * good idea, because that depends on threads that may
167 * be already stopped.
168 */
169 jbd_debug(1, "Now suspending kjournald\n");
170 spin_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800171 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_lock(&journal->j_state_lock);
173 } else {
174 /*
175 * We assume on resume that commits are already there,
176 * so we don't sleep
177 */
178 DEFINE_WAIT(wait);
179 int should_sleep = 1;
180
181 prepare_to_wait(&journal->j_wait_commit, &wait,
182 TASK_INTERRUPTIBLE);
183 if (journal->j_commit_sequence != journal->j_commit_request)
184 should_sleep = 0;
185 transaction = journal->j_running_transaction;
186 if (transaction && time_after_eq(jiffies,
187 transaction->t_expires))
188 should_sleep = 0;
Mark Fashehcbf0d272005-09-06 15:19:08 -0700189 if (journal->j_flags & JFS_UNMOUNT)
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700190 should_sleep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (should_sleep) {
192 spin_unlock(&journal->j_state_lock);
193 schedule();
194 spin_lock(&journal->j_state_lock);
195 }
196 finish_wait(&journal->j_wait_commit, &wait);
197 }
198
199 jbd_debug(1, "kjournald wakes\n");
200
201 /*
202 * Were we woken up by a commit wakeup event?
203 */
204 transaction = journal->j_running_transaction;
205 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
206 journal->j_commit_request = transaction->t_tid;
207 jbd_debug(1, "woke because of timeout\n");
208 }
209 goto loop;
210
211end_loop:
212 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800213 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 journal->j_task = NULL;
215 wake_up(&journal->j_wait_done_commit);
216 jbd_debug(1, "Journal thread exiting.\n");
217 return 0;
218}
219
Pavel Emelianov97f06782007-05-08 00:30:42 -0700220static int journal_start_thread(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700222 struct task_struct *t;
223
224 t = kthread_run(kjournald, journal, "kjournald");
225 if (IS_ERR(t))
226 return PTR_ERR(t);
227
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700228 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static void journal_kill_thread(journal_t *journal)
233{
234 spin_lock(&journal->j_state_lock);
235 journal->j_flags |= JFS_UNMOUNT;
236
237 while (journal->j_task) {
238 wake_up(&journal->j_wait_commit);
239 spin_unlock(&journal->j_state_lock);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700240 wait_event(journal->j_wait_done_commit,
241 journal->j_task == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spin_lock(&journal->j_state_lock);
243 }
244 spin_unlock(&journal->j_state_lock);
245}
246
247/*
248 * journal_write_metadata_buffer: write a metadata buffer to the journal.
249 *
250 * Writes a metadata buffer to a given disk block. The actual IO is not
251 * performed but a new buffer_head is constructed which labels the data
252 * to be written with the correct destination disk block.
253 *
254 * Any magic-number escaping which needs to be done will cause a
255 * copy-out here. If the buffer happens to start with the
256 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
257 * magic number is only written to the log for descripter blocks. In
258 * this case, we copy the data and replace the first word with 0, and we
259 * return a result code which indicates that this buffer needs to be
260 * marked as an escaped buffer in the corresponding log descriptor
261 * block. The missing word can then be restored when the block is read
262 * during recovery.
263 *
264 * If the source buffer has already been modified by a new transaction
265 * since we took the last commit snapshot, we use the frozen copy of
266 * that data for IO. If we end up using the existing buffer_head's data
267 * for the write, then we *have* to lock the buffer to prevent anyone
268 * else from using and possibly modifying it while the IO is in
269 * progress.
270 *
271 * The function returns a pointer to the buffer_heads to be used for IO.
272 *
273 * We assume that the journal has already been locked in this function.
274 *
275 * Return value:
276 * <0: Error
277 * >=0: Finished OK
278 *
279 * On success:
280 * Bit 0 set == escape performed on the data
281 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282 */
283
284int journal_write_metadata_buffer(transaction_t *transaction,
285 struct journal_head *jh_in,
286 struct journal_head **jh_out,
Jan Kara9c28cbc2009-08-03 19:21:00 +0200287 unsigned int blocknr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int need_copy_out = 0;
290 int done_copy_out = 0;
291 int do_escape = 0;
292 char *mapped_data;
293 struct buffer_head *new_bh;
294 struct journal_head *new_jh;
295 struct page *new_page;
296 unsigned int new_offset;
297 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghuaf1015c42009-07-15 21:42:05 +0200298 journal_t *journal = transaction->t_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /*
301 * The buffer really shouldn't be locked: only the current committing
302 * transaction is allowed to write it, so nobody else is allowed
303 * to do any IO.
304 *
305 * akpm: except if we're journalling data, and write() output is
306 * also part of a shared mapping, and another thread has
307 * decided to launch a writepage() against this buffer.
308 */
309 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
310
311 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghuaf1015c42009-07-15 21:42:05 +0200312 /* keep subsequent assertions sane */
313 new_bh->b_state = 0;
314 init_buffer(new_bh, NULL, NULL);
315 atomic_set(&new_bh->b_count, 1);
316 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * If a new transaction has already done a buffer copy-out, then
320 * we use that version of the data for the commit.
321 */
322 jbd_lock_bh_state(bh_in);
323repeat:
324 if (jh_in->b_frozen_data) {
325 done_copy_out = 1;
326 new_page = virt_to_page(jh_in->b_frozen_data);
327 new_offset = offset_in_page(jh_in->b_frozen_data);
328 } else {
329 new_page = jh2bh(jh_in)->b_page;
330 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
331 }
332
Cong Wang8fb53c42011-11-25 23:14:31 +0800333 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * Check for escaping
336 */
337 if (*((__be32 *)(mapped_data + new_offset)) ==
338 cpu_to_be32(JFS_MAGIC_NUMBER)) {
339 need_copy_out = 1;
340 do_escape = 1;
341 }
Cong Wang8fb53c42011-11-25 23:14:31 +0800342 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * Do we need to do a data copy?
346 */
347 if (need_copy_out && !done_copy_out) {
348 char *tmp;
349
350 jbd_unlock_bh_state(bh_in);
Mingming Caoc089d492007-10-16 18:38:25 -0400351 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 jbd_lock_bh_state(bh_in);
353 if (jh_in->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400354 jbd_free(tmp, bh_in->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto repeat;
356 }
357
358 jh_in->b_frozen_data = tmp;
Cong Wang8fb53c42011-11-25 23:14:31 +0800359 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang8fb53c42011-11-25 23:14:31 +0800361 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 new_page = virt_to_page(tmp);
364 new_offset = offset_in_page(tmp);
365 done_copy_out = 1;
366 }
367
368 /*
369 * Did we need to do an escaping? Now we've done all the
370 * copying, we can finally do so.
371 */
372 if (do_escape) {
Cong Wang8fb53c42011-11-25 23:14:31 +0800373 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang8fb53c42011-11-25 23:14:31 +0800375 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 set_bh_page(new_bh, new_page, new_offset);
379 new_jh->b_transaction = NULL;
380 new_bh->b_size = jh2bh(jh_in)->b_size;
381 new_bh->b_bdev = transaction->t_journal->j_dev;
382 new_bh->b_blocknr = blocknr;
383 set_buffer_mapped(new_bh);
384 set_buffer_dirty(new_bh);
385
386 *jh_out = new_jh;
387
388 /*
389 * The to-be-written buffer needs to get moved to the io queue,
390 * and the original buffer whose contents we are shadowing or
391 * copying is moved to the transaction's shadow queue.
392 */
393 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghuaf1015c42009-07-15 21:42:05 +0200394 spin_lock(&journal->j_list_lock);
395 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
396 spin_unlock(&journal->j_list_lock);
397 jbd_unlock_bh_state(bh_in);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 JBUFFER_TRACE(new_jh, "file as BJ_IO");
400 journal_file_buffer(new_jh, transaction, BJ_IO);
401
402 return do_escape | (done_copy_out << 1);
403}
404
405/*
406 * Allocation code for the journal file. Manage the space left in the
407 * journal, so that we can begin checkpointing when appropriate.
408 */
409
410/*
411 * __log_space_left: Return the number of free blocks left in the journal.
412 *
413 * Called with the journal already locked.
414 *
415 * Called under j_state_lock
416 */
417
418int __log_space_left(journal_t *journal)
419{
420 int left = journal->j_free;
421
422 assert_spin_locked(&journal->j_state_lock);
423
424 /*
425 * Be pessimistic here about the number of those free blocks which
426 * might be required for log descriptor control blocks.
427 */
428
429#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
430
431 left -= MIN_LOG_RESERVED_BLOCKS;
432
433 if (left <= 0)
434 return 0;
435 left -= (left >> 3);
436 return left;
437}
438
439/*
Jan Kara8fe4cd02009-02-11 13:04:25 -0800440 * Called under j_state_lock. Returns true if a transaction commit was started.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
442int __log_start_commit(journal_t *journal, tid_t target)
443{
444 /*
Ted Ts'od9b01932011-04-30 13:17:11 -0400445 * The only transaction we can possibly wait upon is the
446 * currently running transaction (if it exists). Otherwise,
447 * the target tid must be an old one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Eric Sandeen7e2fb2d2012-12-18 11:03:57 -0600449 if (journal->j_commit_request != target &&
450 journal->j_running_transaction &&
Ted Ts'od9b01932011-04-30 13:17:11 -0400451 journal->j_running_transaction->t_tid == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200453 * We want a new commit: OK, mark the request and wakeup the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * commit thread. We do _not_ do the commit ourselves.
455 */
456
457 journal->j_commit_request = target;
458 jbd_debug(1, "JBD: requesting commit %d/%d\n",
459 journal->j_commit_request,
460 journal->j_commit_sequence);
461 wake_up(&journal->j_wait_commit);
462 return 1;
Ted Ts'od9b01932011-04-30 13:17:11 -0400463 } else if (!tid_geq(journal->j_commit_request, target))
464 /* This should never happen, but if it does, preserve
465 the evidence before kjournald goes into a loop and
466 increments j_commit_sequence beyond all recognition. */
467 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
468 journal->j_commit_request, journal->j_commit_sequence,
469 target, journal->j_running_transaction ?
470 journal->j_running_transaction->t_tid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472}
473
474int log_start_commit(journal_t *journal, tid_t tid)
475{
476 int ret;
477
478 spin_lock(&journal->j_state_lock);
479 ret = __log_start_commit(journal, tid);
480 spin_unlock(&journal->j_state_lock);
481 return ret;
482}
483
484/*
485 * Force and wait upon a commit if the calling process is not within
486 * transaction. This is used for forcing out undo-protected data which contains
487 * bitmaps, when the fs is running out of space.
488 *
489 * We can only force the running transaction if we don't have an active handle;
490 * otherwise, we will deadlock.
491 *
492 * Returns true if a transaction was started.
493 */
494int journal_force_commit_nested(journal_t *journal)
495{
496 transaction_t *transaction = NULL;
497 tid_t tid;
498
499 spin_lock(&journal->j_state_lock);
500 if (journal->j_running_transaction && !current->journal_info) {
501 transaction = journal->j_running_transaction;
502 __log_start_commit(journal, transaction->t_tid);
503 } else if (journal->j_committing_transaction)
504 transaction = journal->j_committing_transaction;
505
506 if (!transaction) {
507 spin_unlock(&journal->j_state_lock);
508 return 0; /* Nothing to retry */
509 }
510
511 tid = transaction->t_tid;
512 spin_unlock(&journal->j_state_lock);
513 log_wait_commit(journal, tid);
514 return 1;
515}
516
517/*
518 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800519 * if a transaction is going to be committed (or is currently already
520 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
522int journal_start_commit(journal_t *journal, tid_t *ptid)
523{
524 int ret = 0;
525
526 spin_lock(&journal->j_state_lock);
527 if (journal->j_running_transaction) {
528 tid_t tid = journal->j_running_transaction->t_tid;
529
Jan Kara8fe4cd02009-02-11 13:04:25 -0800530 __log_start_commit(journal, tid);
531 /* There's a running transaction and we've just made sure
532 * it's commit has been scheduled. */
533 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800535 ret = 1;
536 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300538 * If commit has been started, then we have to wait for
539 * completion of that transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800541 if (ptid)
542 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ret = 1;
544 }
545 spin_unlock(&journal->j_state_lock);
546 return ret;
547}
548
549/*
550 * Wait for a specified commit to complete.
551 * The caller may not hold the journal lock.
552 */
553int log_wait_commit(journal_t *journal, tid_t tid)
554{
555 int err = 0;
556
557#ifdef CONFIG_JBD_DEBUG
558 spin_lock(&journal->j_state_lock);
559 if (!tid_geq(journal->j_commit_request, tid)) {
560 printk(KERN_EMERG
561 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700562 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 spin_unlock(&journal->j_state_lock);
565#endif
566 spin_lock(&journal->j_state_lock);
Jan Karae678a4f2013-03-27 17:30:59 +0100567 /*
568 * Not running or committing trans? Must be already committed. This
569 * saves us from waiting for a *long* time when tid overflows.
570 */
571 if (!((journal->j_running_transaction &&
572 journal->j_running_transaction->t_tid == tid) ||
573 (journal->j_committing_transaction &&
574 journal->j_committing_transaction->t_tid == tid)))
575 goto out_unlock;
576
Jan Kara2db938b2011-02-21 17:25:37 +0100577 if (!tid_geq(journal->j_commit_waited, tid))
578 journal->j_commit_waited = tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 while (tid_gt(tid, journal->j_commit_sequence)) {
580 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
581 tid, journal->j_commit_sequence);
582 wake_up(&journal->j_wait_commit);
583 spin_unlock(&journal->j_state_lock);
584 wait_event(journal->j_wait_done_commit,
585 !tid_gt(tid, journal->j_commit_sequence));
586 spin_lock(&journal->j_state_lock);
587 }
Jan Karae678a4f2013-03-27 17:30:59 +0100588out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_unlock(&journal->j_state_lock);
590
591 if (unlikely(is_journal_aborted(journal))) {
592 printk(KERN_EMERG "journal commit I/O error\n");
593 err = -EIO;
594 }
595 return err;
596}
597
598/*
Jan Kara03f4d802010-04-15 22:16:24 +0200599 * Return 1 if a given transaction has not yet sent barrier request
600 * connected with a transaction commit. If 0 is returned, transaction
601 * may or may not have sent the barrier. Used to avoid sending barrier
602 * twice in common cases.
603 */
604int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
605{
606 int ret = 0;
607 transaction_t *commit_trans;
608
609 if (!(journal->j_flags & JFS_BARRIER))
610 return 0;
611 spin_lock(&journal->j_state_lock);
612 /* Transaction already committed? */
613 if (tid_geq(journal->j_commit_sequence, tid))
614 goto out;
615 /*
616 * Transaction is being committed and we already proceeded to
617 * writing commit record?
618 */
619 commit_trans = journal->j_committing_transaction;
620 if (commit_trans && commit_trans->t_tid == tid &&
621 commit_trans->t_state >= T_COMMIT_RECORD)
622 goto out;
623 ret = 1;
624out:
625 spin_unlock(&journal->j_state_lock);
626 return ret;
627}
Jan Kara52779702010-04-15 22:24:26 +0200628EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200629
630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Log buffer allocation routines:
632 */
633
Jan Kara9c28cbc2009-08-03 19:21:00 +0200634int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200636 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 spin_lock(&journal->j_state_lock);
639 J_ASSERT(journal->j_free > 1);
640
641 blocknr = journal->j_head;
642 journal->j_head++;
643 journal->j_free--;
644 if (journal->j_head == journal->j_last)
645 journal->j_head = journal->j_first;
646 spin_unlock(&journal->j_state_lock);
647 return journal_bmap(journal, blocknr, retp);
648}
649
650/*
651 * Conversion of logical to physical block numbers for the journal
652 *
653 * On external journals the journal blocks are identity-mapped, so
654 * this is a no-op. If needed, we can use j_blk_offset - everything is
655 * ready.
656 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200657int journal_bmap(journal_t *journal, unsigned int blocknr,
658 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200661 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (journal->j_inode) {
664 ret = bmap(journal->j_inode, blocknr);
665 if (ret)
666 *retp = ret;
667 else {
668 char b[BDEVNAME_SIZE];
669
670 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200671 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700672 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 blocknr,
674 bdevname(journal->j_dev, b));
675 err = -EIO;
676 __journal_abort_soft(journal, err);
677 }
678 } else {
679 *retp = blocknr; /* +journal->j_blk_offset */
680 }
681 return err;
682}
683
684/*
685 * We play buffer_head aliasing tricks to write data/metadata blocks to
686 * the journal without copying their contents, but for journal
687 * descriptor blocks we do need to generate bona fide buffers.
688 *
689 * After the caller of journal_get_descriptor_buffer() has finished modifying
690 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
691 * But we don't bother doing that, so there will be coherency problems with
692 * mmaps of blockdevs which hold live JBD-controlled filesystems.
693 */
694struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
695{
696 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200697 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int err;
699
700 err = journal_next_log_block(journal, &blocknr);
701
702 if (err)
703 return NULL;
704
705 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700706 if (!bh)
707 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 lock_buffer(bh);
709 memset(bh->b_data, 0, journal->j_blocksize);
710 set_buffer_uptodate(bh);
711 unlock_buffer(bh);
712 BUFFER_TRACE(bh, "return this buffer");
713 return journal_add_journal_head(bh);
714}
715
716/*
717 * Management for journal control blocks: functions to create and
718 * destroy journal_t structures, and to initialise and read existing
719 * journal blocks from disk. */
720
721/* First: create and setup a journal_t object in memory. We initialise
722 * very few fields yet: that has to wait until we have created the
723 * journal structures from from scratch, or loaded them from disk. */
724
725static journal_t * journal_init_common (void)
726{
727 journal_t *journal;
728 int err;
729
Mingming Cao8c3478a2007-10-18 23:39:20 -0700730 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!journal)
732 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 init_waitqueue_head(&journal->j_wait_transaction_locked);
735 init_waitqueue_head(&journal->j_wait_logspace);
736 init_waitqueue_head(&journal->j_wait_done_commit);
737 init_waitqueue_head(&journal->j_wait_checkpoint);
738 init_waitqueue_head(&journal->j_wait_commit);
739 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800740 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_init(&journal->j_revoke_lock);
742 spin_lock_init(&journal->j_list_lock);
743 spin_lock_init(&journal->j_state_lock);
744
745 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
746
747 /* The journal is marked for error until we succeed with recovery! */
748 journal->j_flags = JFS_ABORT;
749
750 /* Set up a default-sized revoke table for the new mount. */
751 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
752 if (err) {
753 kfree(journal);
754 goto fail;
755 }
756 return journal;
757fail:
758 return NULL;
759}
760
761/* journal_init_dev and journal_init_inode:
762 *
763 * Create a journal structure assigned some fixed set of disk blocks to
764 * the journal. We don't actually touch those disk blocks yet, but we
765 * need to set up all of the mapping information to tell the journaling
766 * system where the journal blocks are.
767 *
768 */
769
770/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700771 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * @bdev: Block device on which to create the journal
773 * @fs_dev: Device which hold journalled filesystem for this journal.
774 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700775 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700777 *
778 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700779 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * journal_init_dev creates a journal which maps a fixed contiguous
781 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
784journal_t * journal_init_dev(struct block_device *bdev,
785 struct block_device *fs_dev,
786 int start, int len, int blocksize)
787{
788 journal_t *journal = journal_init_common();
789 struct buffer_head *bh;
790 int n;
791
792 if (!journal)
793 return NULL;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700796 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 n = journal->j_blocksize / sizeof(journal_block_tag_t);
798 journal->j_wbufsize = n;
799 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
800 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700802 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700803 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700805 journal->j_dev = bdev;
806 journal->j_fs_dev = fs_dev;
807 journal->j_blk_offset = start;
808 journal->j_maxlen = len;
809
810 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700811 if (!bh) {
812 printk(KERN_ERR
813 "%s: Cannot get buffer for journal superblock\n",
814 __func__);
815 goto out_err;
816 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700817 journal->j_sb_buffer = bh;
818 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700821out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800822 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700823 kfree(journal);
824 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700826
827/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
829 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700830 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * journal_init_inode creates a journal which maps an on-disk inode as
832 * the journal. The inode must exist already, must support bmap() and
833 * must have all data blocks preallocated.
834 */
835journal_t * journal_init_inode (struct inode *inode)
836{
837 struct buffer_head *bh;
838 journal_t *journal = journal_init_common();
839 int err;
840 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200841 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (!journal)
844 return NULL;
845
846 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
847 journal->j_inode = inode;
848 jbd_debug(1,
849 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700850 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 (long long) inode->i_size,
852 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
853
854 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
855 journal->j_blocksize = inode->i_sb->s_blocksize;
856
857 /* journal descriptor can store up to n blocks -bzzz */
858 n = journal->j_blocksize / sizeof(journal_block_tag_t);
859 journal->j_wbufsize = n;
860 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
861 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300862 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700863 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700864 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 err = journal_bmap(journal, 0, &blocknr);
868 /* If that failed, give up */
869 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -0800870 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700871 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700872 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700876 if (!bh) {
877 printk(KERN_ERR
878 "%s: Cannot get buffer for journal superblock\n",
879 __func__);
880 goto out_err;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 journal->j_sb_buffer = bh;
883 journal->j_superblock = (journal_superblock_t *)bh->b_data;
884
885 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700886out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800887 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700888 kfree(journal);
889 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * If the journal init or create aborts, we need to mark the journal
894 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700895 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897static void journal_fail_superblock (journal_t *journal)
898{
899 struct buffer_head *bh = journal->j_sb_buffer;
900 brelse(bh);
901 journal->j_sb_buffer = NULL;
902}
903
904/*
905 * Given a journal_t structure, initialise the various fields for
906 * startup of a new journaling session. We use this both when creating
907 * a journal, and after recovering an old journal to reset it for
908 * subsequent use.
909 */
910
911static int journal_reset(journal_t *journal)
912{
913 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200914 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 first = be32_to_cpu(sb->s_first);
917 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200918 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200919 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200920 first, last);
921 journal_fail_superblock(journal);
922 return -EINVAL;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 journal->j_first = first;
926 journal->j_last = last;
927
928 journal->j_head = first;
929 journal->j_tail = first;
930 journal->j_free = last - first;
931
932 journal->j_tail_sequence = journal->j_transaction_sequence;
933 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
934 journal->j_commit_request = journal->j_commit_sequence;
935
936 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
937
Jan Kara9754e392012-04-07 12:33:03 +0200938 /*
939 * As a special case, if the on-disk copy is already marked as needing
940 * no recovery (s_start == 0), then we can safely defer the superblock
941 * update until the next commit by setting JFS_FLUSHED. This avoids
942 * attempting a write to a potential-readonly device.
943 */
944 if (sb->s_start == 0) {
945 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
946 "(start %u, seq %d, errno %d)\n",
947 journal->j_tail, journal->j_tail_sequence,
948 journal->j_errno);
949 journal->j_flags |= JFS_FLUSHED;
950 } else {
Jan Kara1ce84862012-04-07 12:50:13 +0200951 /* Lock here to make assertions happy... */
952 mutex_lock(&journal->j_checkpoint_mutex);
Jan Karafd2cbd42012-04-07 11:05:19 +0200953 /*
954 * Update log tail information. We use WRITE_FUA since new
955 * transaction will start reusing journal space and so we
956 * must make sure information about current log tail is on
957 * disk before that.
958 */
959 journal_update_sb_log_tail(journal,
960 journal->j_tail_sequence,
961 journal->j_tail,
962 WRITE_FUA);
Jan Kara1ce84862012-04-07 12:50:13 +0200963 mutex_unlock(&journal->j_checkpoint_mutex);
Jan Kara9754e392012-04-07 12:33:03 +0200964 }
Pavel Emelianov97f06782007-05-08 00:30:42 -0700965 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700968/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * int journal_create() - Initialise the new journal file
970 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700971 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * Given a journal_t structure which tells us which disk blocks we can
973 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700974 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 **/
976int journal_create(journal_t *journal)
977{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200978 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct buffer_head *bh;
980 journal_superblock_t *sb;
981 int i, err;
982
983 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
984 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
985 journal->j_maxlen);
986 journal_fail_superblock(journal);
987 return -EINVAL;
988 }
989
990 if (journal->j_inode == NULL) {
991 /*
992 * We don't know what block to start at!
993 */
994 printk(KERN_EMERG
995 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700996 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 BUG();
998 }
999
1000 /* Zero out the entire journal on disk. We cannot afford to
1001 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
1002 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1003 for (i = 0; i < journal->j_maxlen; i++) {
1004 err = journal_bmap(journal, i, &blocknr);
1005 if (err)
1006 return err;
1007 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Namhyung Kim2a0e3382010-10-13 17:17:18 +09001008 if (unlikely(!bh))
1009 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 lock_buffer(bh);
1011 memset (bh->b_data, 0, journal->j_blocksize);
1012 BUFFER_TRACE(bh, "marking dirty");
1013 mark_buffer_dirty(bh);
1014 BUFFER_TRACE(bh, "marking uptodate");
1015 set_buffer_uptodate(bh);
1016 unlock_buffer(bh);
1017 __brelse(bh);
1018 }
1019
1020 sync_blockdev(journal->j_dev);
1021 jbd_debug(1, "JBD: journal cleared.\n");
1022
1023 /* OK, fill in the initial static fields in the new superblock */
1024 sb = journal->j_superblock;
1025
1026 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1027 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1028
1029 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1030 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1031 sb->s_first = cpu_to_be32(1);
1032
1033 journal->j_transaction_sequence = 1;
1034
1035 journal->j_flags &= ~JFS_ABORT;
1036 journal->j_format_version = 2;
1037
1038 return journal_reset(journal);
1039}
1040
Jan Karafd2cbd42012-04-07 11:05:19 +02001041static void journal_write_superblock(journal_t *journal, int write_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct buffer_head *bh = journal->j_sb_buffer;
Jan Karafd2cbd42012-04-07 11:05:19 +02001044 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Jan Karafd2cbd42012-04-07 11:05:19 +02001046 trace_journal_write_superblock(journal, write_op);
1047 if (!(journal->j_flags & JFS_BARRIER))
1048 write_op &= ~(REQ_FUA | REQ_FLUSH);
1049 lock_buffer(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001050 if (buffer_write_io_error(bh)) {
1051 char b[BDEVNAME_SIZE];
1052 /*
1053 * Oh, dear. A previous attempt to write the journal
1054 * superblock failed. This could happen because the
1055 * USB device was yanked out. Or it could happen to
1056 * be a transient write error and maybe the block will
1057 * be remapped. Nothing we can do but to retry the
1058 * write and hope for the best.
1059 */
1060 printk(KERN_ERR "JBD: previous I/O error detected "
1061 "for journal superblock update for %s.\n",
1062 journal_dev_name(journal, b));
1063 clear_buffer_write_io_error(bh);
1064 set_buffer_uptodate(bh);
1065 }
1066
Jan Karafd2cbd42012-04-07 11:05:19 +02001067 get_bh(bh);
1068 bh->b_end_io = end_buffer_write_sync;
1069 ret = submit_bh(write_op, bh);
1070 wait_on_buffer(bh);
Jan Kara9754e392012-04-07 12:33:03 +02001071 if (buffer_write_io_error(bh)) {
Jan Kara9754e392012-04-07 12:33:03 +02001072 clear_buffer_write_io_error(bh);
1073 set_buffer_uptodate(bh);
Jan Karafd2cbd42012-04-07 11:05:19 +02001074 ret = -EIO;
1075 }
1076 if (ret) {
1077 char b[BDEVNAME_SIZE];
1078 printk(KERN_ERR "JBD: Error %d detected "
1079 "when updating journal superblock for %s.\n",
1080 ret, journal_dev_name(journal, b));
Jan Kara9754e392012-04-07 12:33:03 +02001081 }
1082}
1083
1084/**
1085 * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1086 * @journal: The journal to update.
Jan Karafd2cbd42012-04-07 11:05:19 +02001087 * @tail_tid: TID of the new transaction at the tail of the log
1088 * @tail_block: The first block of the transaction at the tail of the log
1089 * @write_op: With which operation should we write the journal sb
Jan Kara9754e392012-04-07 12:33:03 +02001090 *
1091 * Update a journal's superblock information about log tail and write it to
1092 * disk, waiting for the IO to complete.
1093 */
Jan Karafd2cbd42012-04-07 11:05:19 +02001094void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1095 unsigned int tail_block, int write_op)
Jan Kara9754e392012-04-07 12:33:03 +02001096{
1097 journal_superblock_t *sb = journal->j_superblock;
1098
Jan Kara1ce84862012-04-07 12:50:13 +02001099 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Karafd2cbd42012-04-07 11:05:19 +02001100 jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
1101 tail_block, tail_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Jan Karafd2cbd42012-04-07 11:05:19 +02001103 sb->s_sequence = cpu_to_be32(tail_tid);
1104 sb->s_start = cpu_to_be32(tail_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Jan Karafd2cbd42012-04-07 11:05:19 +02001106 journal_write_superblock(journal, write_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jan Kara9754e392012-04-07 12:33:03 +02001108 /* Log is no longer empty */
1109 spin_lock(&journal->j_state_lock);
1110 WARN_ON(!sb->s_sequence);
1111 journal->j_flags &= ~JFS_FLUSHED;
1112 spin_unlock(&journal->j_state_lock);
1113}
1114
1115/**
1116 * mark_journal_empty() - Mark on disk journal as empty.
1117 * @journal: The journal to update.
1118 *
1119 * Update a journal's dynamic superblock fields to show that journal is empty.
1120 * Write updated superblock to disk waiting for IO to complete.
1121 */
1122static void mark_journal_empty(journal_t *journal)
1123{
1124 journal_superblock_t *sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Jan Kara1ce84862012-04-07 12:50:13 +02001126 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 spin_lock(&journal->j_state_lock);
Jan Kara2e84f262012-08-15 13:50:27 +02001128 /* Is it already empty? */
1129 if (sb->s_start == 0) {
1130 spin_unlock(&journal->j_state_lock);
1131 return;
1132 }
Jan Kara9754e392012-04-07 12:33:03 +02001133 jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1134 journal->j_tail_sequence);
1135
1136 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1137 sb->s_start = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 spin_unlock(&journal->j_state_lock);
Jan Kara9754e392012-04-07 12:33:03 +02001139
Jan Karafd2cbd42012-04-07 11:05:19 +02001140 journal_write_superblock(journal, WRITE_FUA);
Jan Kara9754e392012-04-07 12:33:03 +02001141
1142 spin_lock(&journal->j_state_lock);
1143 /* Log is empty */
1144 journal->j_flags |= JFS_FLUSHED;
1145 spin_unlock(&journal->j_state_lock);
1146}
1147
1148/**
1149 * journal_update_sb_errno() - Update error in the journal.
1150 * @journal: The journal to update.
1151 *
1152 * Update a journal's errno. Write updated superblock to disk waiting for IO
1153 * to complete.
1154 */
1155static void journal_update_sb_errno(journal_t *journal)
1156{
1157 journal_superblock_t *sb = journal->j_superblock;
1158
1159 spin_lock(&journal->j_state_lock);
1160 jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1161 journal->j_errno);
1162 sb->s_errno = cpu_to_be32(journal->j_errno);
1163 spin_unlock(&journal->j_state_lock);
1164
Jan Karafd2cbd42012-04-07 11:05:19 +02001165 journal_write_superblock(journal, WRITE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168/*
1169 * Read the superblock for a given journal, performing initial
1170 * validation of the format.
1171 */
1172
1173static int journal_get_superblock(journal_t *journal)
1174{
1175 struct buffer_head *bh;
1176 journal_superblock_t *sb;
1177 int err = -EIO;
1178
1179 bh = journal->j_sb_buffer;
1180
1181 J_ASSERT(bh != NULL);
1182 if (!buffer_uptodate(bh)) {
1183 ll_rw_block(READ, 1, &bh);
1184 wait_on_buffer(bh);
1185 if (!buffer_uptodate(bh)) {
1186 printk (KERN_ERR
1187 "JBD: IO error reading journal superblock\n");
1188 goto out;
1189 }
1190 }
1191
1192 sb = journal->j_superblock;
1193
1194 err = -EINVAL;
1195
1196 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1197 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1198 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1199 goto out;
1200 }
1201
1202 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1203 case JFS_SUPERBLOCK_V1:
1204 journal->j_format_version = 1;
1205 break;
1206 case JFS_SUPERBLOCK_V2:
1207 journal->j_format_version = 2;
1208 break;
1209 default:
1210 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1211 goto out;
1212 }
1213
1214 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1215 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1216 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1217 printk (KERN_WARNING "JBD: journal file too short\n");
1218 goto out;
1219 }
1220
Eryu Guan87622022011-11-01 19:04:59 -04001221 if (be32_to_cpu(sb->s_first) == 0 ||
1222 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1223 printk(KERN_WARNING
1224 "JBD: Invalid start block of journal: %u\n",
1225 be32_to_cpu(sb->s_first));
1226 goto out;
1227 }
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return 0;
1230
1231out:
1232 journal_fail_superblock(journal);
1233 return err;
1234}
1235
1236/*
1237 * Load the on-disk journal superblock and read the key fields into the
1238 * journal_t.
1239 */
1240
1241static int load_superblock(journal_t *journal)
1242{
1243 int err;
1244 journal_superblock_t *sb;
1245
1246 err = journal_get_superblock(journal);
1247 if (err)
1248 return err;
1249
1250 sb = journal->j_superblock;
1251
1252 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1253 journal->j_tail = be32_to_cpu(sb->s_start);
1254 journal->j_first = be32_to_cpu(sb->s_first);
1255 journal->j_last = be32_to_cpu(sb->s_maxlen);
1256 journal->j_errno = be32_to_cpu(sb->s_errno);
1257
1258 return 0;
1259}
1260
1261
1262/**
1263 * int journal_load() - Read journal from disk.
1264 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001265 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 * Given a journal_t structure which tells us which disk blocks contain
1267 * a journal, read the journal from disk to initialise the in-memory
1268 * structures.
1269 */
1270int journal_load(journal_t *journal)
1271{
1272 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001273 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 err = load_superblock(journal);
1276 if (err)
1277 return err;
1278
Badari Pulavartyea817392006-08-27 01:23:52 -07001279 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* If this is a V2 superblock, then we have to check the
1281 * features flags on it. */
1282
1283 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if ((sb->s_feature_ro_compat &
1285 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1286 (sb->s_feature_incompat &
1287 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1288 printk (KERN_WARNING
1289 "JBD: Unrecognised features on journal\n");
1290 return -EINVAL;
1291 }
1292 }
1293
1294 /* Let the recovery code check whether it needs to recover any
1295 * data from the journal. */
1296 if (journal_recover(journal))
1297 goto recovery_error;
1298
1299 /* OK, we've finished with the dynamic journal bits:
1300 * reinitialise the dynamic contents of the superblock in memory
1301 * and reset them on disk. */
1302 if (journal_reset(journal))
1303 goto recovery_error;
1304
1305 journal->j_flags &= ~JFS_ABORT;
1306 journal->j_flags |= JFS_LOADED;
1307 return 0;
1308
1309recovery_error:
1310 printk (KERN_WARNING "JBD: recovery failed\n");
1311 return -EIO;
1312}
1313
1314/**
1315 * void journal_destroy() - Release a journal_t structure.
1316 * @journal: Journal to act on.
1317 *
1318 * Release a journal_t structure once it is no longer in use by the
1319 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001320 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001322int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001324 int err = 0;
1325
Jan Kara03f4d802010-04-15 22:16:24 +02001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* Wait for the commit thread to wake up and die. */
1328 journal_kill_thread(journal);
1329
1330 /* Force a final log commit */
1331 if (journal->j_running_transaction)
1332 journal_commit_transaction(journal);
1333
1334 /* Force any old transactions to disk */
1335
Jan Kara1ce84862012-04-07 12:50:13 +02001336 /* We cannot race with anybody but must keep assertions happy */
1337 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Totally anal locking here... */
1339 spin_lock(&journal->j_list_lock);
1340 while (journal->j_checkpoint_transactions != NULL) {
1341 spin_unlock(&journal->j_list_lock);
1342 log_do_checkpoint(journal);
1343 spin_lock(&journal->j_list_lock);
1344 }
1345
1346 J_ASSERT(journal->j_running_transaction == NULL);
1347 J_ASSERT(journal->j_committing_transaction == NULL);
1348 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1349 spin_unlock(&journal->j_list_lock);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001352 if (!is_journal_aborted(journal)) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001353 journal->j_tail_sequence =
1354 ++journal->j_transaction_sequence;
Jan Kara9754e392012-04-07 12:33:03 +02001355 mark_journal_empty(journal);
1356 } else
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001357 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 brelse(journal->j_sb_buffer);
1359 }
Jan Kara1ce84862012-04-07 12:50:13 +02001360 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 if (journal->j_inode)
1363 iput(journal->j_inode);
1364 if (journal->j_revoke)
1365 journal_destroy_revoke(journal);
1366 kfree(journal->j_wbuf);
1367 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001368
1369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372
1373/**
1374 *int journal_check_used_features () - Check if features specified are used.
1375 * @journal: Journal to check.
1376 * @compat: bitmask of compatible features
1377 * @ro: bitmask of features that force read-only mount
1378 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001379 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001381 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 **/
1383
1384int journal_check_used_features (journal_t *journal, unsigned long compat,
1385 unsigned long ro, unsigned long incompat)
1386{
1387 journal_superblock_t *sb;
1388
1389 if (!compat && !ro && !incompat)
1390 return 1;
1391 if (journal->j_format_version == 1)
1392 return 0;
1393
1394 sb = journal->j_superblock;
1395
1396 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1397 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1398 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1399 return 1;
1400
1401 return 0;
1402}
1403
1404/**
1405 * int journal_check_available_features() - Check feature set in journalling layer
1406 * @journal: Journal to check.
1407 * @compat: bitmask of compatible features
1408 * @ro: bitmask of features that force read-only mount
1409 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001410 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * Check whether the journaling code supports the use of
1412 * all of a given set of features on this journal. Return true
1413 * (non-zero) if it can. */
1414
1415int journal_check_available_features (journal_t *journal, unsigned long compat,
1416 unsigned long ro, unsigned long incompat)
1417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (!compat && !ro && !incompat)
1419 return 1;
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /* We can support any known requested features iff the
1422 * superblock is in version 2. Otherwise we fail to support any
1423 * extended sb features. */
1424
1425 if (journal->j_format_version != 2)
1426 return 0;
1427
1428 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1429 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1430 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1431 return 1;
1432
1433 return 0;
1434}
1435
1436/**
1437 * int journal_set_features () - Mark a given journal feature in the superblock
1438 * @journal: Journal to act on.
1439 * @compat: bitmask of compatible features
1440 * @ro: bitmask of features that force read-only mount
1441 * @incompat: bitmask of incompatible features
1442 *
1443 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001444 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 *
1446 */
1447
1448int journal_set_features (journal_t *journal, unsigned long compat,
1449 unsigned long ro, unsigned long incompat)
1450{
1451 journal_superblock_t *sb;
1452
1453 if (journal_check_used_features(journal, compat, ro, incompat))
1454 return 1;
1455
1456 if (!journal_check_available_features(journal, compat, ro, incompat))
1457 return 0;
1458
1459 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1460 compat, ro, incompat);
1461
1462 sb = journal->j_superblock;
1463
1464 sb->s_feature_compat |= cpu_to_be32(compat);
1465 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1466 sb->s_feature_incompat |= cpu_to_be32(incompat);
1467
1468 return 1;
1469}
1470
1471
1472/**
1473 * int journal_update_format () - Update on-disk journal structure.
1474 * @journal: Journal to act on.
1475 *
1476 * Given an initialised but unloaded journal struct, poke about in the
1477 * on-disk structure to update it to the most recent supported version.
1478 */
1479int journal_update_format (journal_t *journal)
1480{
1481 journal_superblock_t *sb;
1482 int err;
1483
1484 err = journal_get_superblock(journal);
1485 if (err)
1486 return err;
1487
1488 sb = journal->j_superblock;
1489
1490 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1491 case JFS_SUPERBLOCK_V2:
1492 return 0;
1493 case JFS_SUPERBLOCK_V1:
1494 return journal_convert_superblock_v1(journal, sb);
1495 default:
1496 break;
1497 }
1498 return -EINVAL;
1499}
1500
1501static int journal_convert_superblock_v1(journal_t *journal,
1502 journal_superblock_t *sb)
1503{
1504 int offset, blocksize;
1505 struct buffer_head *bh;
1506
1507 printk(KERN_WARNING
1508 "JBD: Converting superblock from version 1 to 2.\n");
1509
1510 /* Pre-initialise new fields to zero */
1511 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1512 blocksize = be32_to_cpu(sb->s_blocksize);
1513 memset(&sb->s_feature_compat, 0, blocksize-offset);
1514
1515 sb->s_nr_users = cpu_to_be32(1);
1516 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1517 journal->j_format_version = 2;
1518
1519 bh = journal->j_sb_buffer;
1520 BUFFER_TRACE(bh, "marking dirty");
1521 mark_buffer_dirty(bh);
1522 sync_dirty_buffer(bh);
1523 return 0;
1524}
1525
1526
1527/**
1528 * int journal_flush () - Flush journal
1529 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001530 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 * Flush all data for a given journal to disk and empty the journal.
1532 * Filesystems can use this when remounting readonly to ensure that
1533 * recovery does not need to happen on remount.
1534 */
1535
1536int journal_flush(journal_t *journal)
1537{
1538 int err = 0;
1539 transaction_t *transaction = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 spin_lock(&journal->j_state_lock);
1542
1543 /* Force everything buffered to the log... */
1544 if (journal->j_running_transaction) {
1545 transaction = journal->j_running_transaction;
1546 __log_start_commit(journal, transaction->t_tid);
1547 } else if (journal->j_committing_transaction)
1548 transaction = journal->j_committing_transaction;
1549
1550 /* Wait for the log commit to complete... */
1551 if (transaction) {
1552 tid_t tid = transaction->t_tid;
1553
1554 spin_unlock(&journal->j_state_lock);
1555 log_wait_commit(journal, tid);
1556 } else {
1557 spin_unlock(&journal->j_state_lock);
1558 }
1559
1560 /* ...and flush everything in the log out to disk. */
1561 spin_lock(&journal->j_list_lock);
1562 while (!err && journal->j_checkpoint_transactions != NULL) {
1563 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001564 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001566 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 spin_lock(&journal->j_list_lock);
1568 }
1569 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001570
1571 if (is_journal_aborted(journal))
1572 return -EIO;
1573
Jan Kara1ce84862012-04-07 12:50:13 +02001574 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 cleanup_journal_tail(journal);
1576
1577 /* Finally, mark the journal as really needing no recovery.
1578 * This sets s_start==0 in the underlying superblock, which is
1579 * the magic code for a fully-recovered superblock. Any future
1580 * commits of data to the journal will restore the current
1581 * s_start value. */
Jan Kara9754e392012-04-07 12:33:03 +02001582 mark_journal_empty(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001583 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 spin_lock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 J_ASSERT(!journal->j_running_transaction);
1586 J_ASSERT(!journal->j_committing_transaction);
1587 J_ASSERT(!journal->j_checkpoint_transactions);
1588 J_ASSERT(journal->j_head == journal->j_tail);
1589 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1590 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/**
1595 * int journal_wipe() - Wipe journal contents
1596 * @journal: Journal to act on.
1597 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001598 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * Wipe out all of the contents of a journal, safely. This will produce
1600 * a warning if the journal contains any valid recovery information.
1601 * Must be called between journal_init_*() and journal_load().
1602 *
1603 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1604 * we merely suppress recovery.
1605 */
1606
1607int journal_wipe(journal_t *journal, int write)
1608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int err = 0;
1610
1611 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1612
1613 err = load_superblock(journal);
1614 if (err)
1615 return err;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (!journal->j_tail)
1618 goto no_recovery;
1619
1620 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1621 write ? "Clearing" : "Ignoring");
1622
1623 err = journal_skip_recovery(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001624 if (write) {
1625 /* Lock to make assertions happy... */
1626 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara9754e392012-04-07 12:33:03 +02001627 mark_journal_empty(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001628 mutex_unlock(&journal->j_checkpoint_mutex);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 no_recovery:
1632 return err;
1633}
1634
1635/*
1636 * journal_dev_name: format a character string to describe on what
1637 * device this journal is present.
1638 */
1639
Adrian Bunk022a4a72005-09-06 15:16:41 -07001640static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct block_device *bdev;
1643
1644 if (journal->j_inode)
1645 bdev = journal->j_inode->i_sb->s_bdev;
1646 else
1647 bdev = journal->j_dev;
1648
1649 return bdevname(bdev, buffer);
1650}
1651
1652/*
1653 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001654 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 *
1656 * Two internal function, which provide abort to te jbd layer
1657 * itself are here.
1658 */
1659
1660/*
1661 * Quick version for internal journal use (doesn't lock the journal).
1662 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1663 * and don't attempt to make any other journal updates.
1664 */
Adrian Bunk53308382008-02-06 01:40:12 -08001665static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 transaction_t *transaction;
1668 char b[BDEVNAME_SIZE];
1669
1670 if (journal->j_flags & JFS_ABORT)
1671 return;
1672
1673 printk(KERN_ERR "Aborting journal on device %s.\n",
1674 journal_dev_name(journal, b));
1675
1676 spin_lock(&journal->j_state_lock);
1677 journal->j_flags |= JFS_ABORT;
1678 transaction = journal->j_running_transaction;
1679 if (transaction)
1680 __log_start_commit(journal, transaction->t_tid);
1681 spin_unlock(&journal->j_state_lock);
1682}
1683
1684/* Soft abort: record the abort error status in the journal superblock,
1685 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001686static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 if (journal->j_flags & JFS_ABORT)
1689 return;
1690
1691 if (!journal->j_errno)
1692 journal->j_errno = errno;
1693
1694 __journal_abort_hard(journal);
1695
1696 if (errno)
Jan Kara9754e392012-04-07 12:33:03 +02001697 journal_update_sb_errno(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700/**
1701 * void journal_abort () - Shutdown the journal immediately.
1702 * @journal: the journal to shutdown.
1703 * @errno: an error number to record in the journal indicating
1704 * the reason for the shutdown.
1705 *
1706 * Perform a complete, immediate shutdown of the ENTIRE
1707 * journal (not of a single transaction). This operation cannot be
1708 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001709 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * The journal_abort function is intended to support higher level error
1711 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1712 * mode.
1713 *
1714 * Journal abort has very specific semantics. Any existing dirty,
1715 * unjournaled buffers in the main filesystem will still be written to
1716 * disk by bdflush, but the journaling mechanism will be suspended
1717 * immediately and no further transaction commits will be honoured.
1718 *
1719 * Any dirty, journaled buffers will be written back to disk without
1720 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1721 * filesystem, but we _do_ attempt to leave as much data as possible
1722 * behind for fsck to use for cleanup.
1723 *
1724 * Any attempt to get a new transaction handle on a journal which is in
1725 * ABORT state will just result in an -EROFS error return. A
1726 * journal_stop on an existing handle will return -EIO if we have
1727 * entered abort state during the update.
1728 *
1729 * Recursive transactions are not disturbed by journal abort until the
1730 * final journal_stop, which will receive the -EIO error.
1731 *
1732 * Finally, the journal_abort call allows the caller to supply an errno
1733 * which will be recorded (if possible) in the journal superblock. This
1734 * allows a client to record failure conditions in the middle of a
1735 * transaction without having to complete the transaction to record the
1736 * failure to disk. ext3_error, for example, now uses this
1737 * functionality.
1738 *
1739 * Errors which originate from within the journaling layer will NOT
1740 * supply an errno; a null errno implies that absolutely no further
1741 * writes are done to the journal (unless there are any already in
1742 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001743 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
1745
1746void journal_abort(journal_t *journal, int errno)
1747{
1748 __journal_abort_soft(journal, errno);
1749}
1750
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001751/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * int journal_errno () - returns the journal's error state.
1753 * @journal: journal to examine.
1754 *
1755 * This is the errno numbet set with journal_abort(), the last
1756 * time the journal was mounted - if the journal was stopped
1757 * without calling abort this will be 0.
1758 *
1759 * If the journal has been aborted on this mount time -EROFS will
1760 * be returned.
1761 */
1762int journal_errno(journal_t *journal)
1763{
1764 int err;
1765
1766 spin_lock(&journal->j_state_lock);
1767 if (journal->j_flags & JFS_ABORT)
1768 err = -EROFS;
1769 else
1770 err = journal->j_errno;
1771 spin_unlock(&journal->j_state_lock);
1772 return err;
1773}
1774
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001775/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * int journal_clear_err () - clears the journal's error state
1777 * @journal: journal to act on.
1778 *
1779 * An error must be cleared or Acked to take a FS out of readonly
1780 * mode.
1781 */
1782int journal_clear_err(journal_t *journal)
1783{
1784 int err = 0;
1785
1786 spin_lock(&journal->j_state_lock);
1787 if (journal->j_flags & JFS_ABORT)
1788 err = -EROFS;
1789 else
1790 journal->j_errno = 0;
1791 spin_unlock(&journal->j_state_lock);
1792 return err;
1793}
1794
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001795/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * void journal_ack_err() - Ack journal err.
1797 * @journal: journal to act on.
1798 *
1799 * An error must be cleared or Acked to take a FS out of readonly
1800 * mode.
1801 */
1802void journal_ack_err(journal_t *journal)
1803{
1804 spin_lock(&journal->j_state_lock);
1805 if (journal->j_errno)
1806 journal->j_flags |= JFS_ACK_ERR;
1807 spin_unlock(&journal->j_state_lock);
1808}
1809
1810int journal_blocks_per_page(struct inode *inode)
1811{
1812 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1813}
1814
1815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * Journal_head storage management
1817 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001818static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819#ifdef CONFIG_JBD_DEBUG
1820static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1821#endif
1822
1823static int journal_init_journal_head_cache(void)
1824{
1825 int retval;
1826
Al Viro1076d172008-03-29 03:07:18 +00001827 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 journal_head_cache = kmem_cache_create("journal_head",
1829 sizeof(struct journal_head),
1830 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001831 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001832 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001834 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 retval = -ENOMEM;
1836 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1837 }
1838 return retval;
1839}
1840
1841static void journal_destroy_journal_head_cache(void)
1842{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001843 if (journal_head_cache) {
1844 kmem_cache_destroy(journal_head_cache);
1845 journal_head_cache = NULL;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/*
1850 * journal_head splicing and dicing
1851 */
1852static struct journal_head *journal_alloc_journal_head(void)
1853{
1854 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856#ifdef CONFIG_JBD_DEBUG
1857 atomic_inc(&nr_journal_heads);
1858#endif
1859 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001860 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001862 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1863 __func__);
1864
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001865 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 yield();
1867 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1868 }
1869 }
1870 return ret;
1871}
1872
1873static void journal_free_journal_head(struct journal_head *jh)
1874{
1875#ifdef CONFIG_JBD_DEBUG
1876 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001877 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878#endif
1879 kmem_cache_free(journal_head_cache, jh);
1880}
1881
1882/*
1883 * A journal_head is attached to a buffer_head whenever JBD has an
1884 * interest in the buffer.
1885 *
1886 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1887 * is set. This bit is tested in core kernel code where we need to take
1888 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1889 * there.
1890 *
1891 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1892 *
1893 * When a buffer has its BH_JBD bit set it is immune from being released by
1894 * core kernel code, mainly via ->b_count.
1895 *
Jan Karabb189242011-06-24 23:11:59 +02001896 * A journal_head is detached from its buffer_head when the journal_head's
1897 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1898 * transaction (b_cp_transaction) hold their references to b_jcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *
1900 * Various places in the kernel want to attach a journal_head to a buffer_head
1901 * _before_ attaching the journal_head to a transaction. To protect the
1902 * journal_head in this situation, journal_add_journal_head elevates the
1903 * journal_head's b_jcount refcount by one. The caller must call
1904 * journal_put_journal_head() to undo this.
1905 *
1906 * So the typical usage would be:
1907 *
1908 * (Attach a journal_head if needed. Increments b_jcount)
1909 * struct journal_head *jh = journal_add_journal_head(bh);
1910 * ...
Jan Karabb189242011-06-24 23:11:59 +02001911 * (Get another reference for transaction)
1912 * journal_grab_journal_head(bh);
1913 * jh->b_transaction = xxx;
1914 * (Put original reference)
1915 * journal_put_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
1917
1918/*
1919 * Give a buffer_head a journal_head.
1920 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 * May sleep.
1922 */
1923struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1924{
1925 struct journal_head *jh;
1926 struct journal_head *new_jh = NULL;
1927
1928repeat:
1929 if (!buffer_jbd(bh)) {
1930 new_jh = journal_alloc_journal_head();
1931 memset(new_jh, 0, sizeof(*new_jh));
1932 }
1933
1934 jbd_lock_bh_journal_head(bh);
1935 if (buffer_jbd(bh)) {
1936 jh = bh2jh(bh);
1937 } else {
1938 J_ASSERT_BH(bh,
1939 (atomic_read(&bh->b_count) > 0) ||
1940 (bh->b_page && bh->b_page->mapping));
1941
1942 if (!new_jh) {
1943 jbd_unlock_bh_journal_head(bh);
1944 goto repeat;
1945 }
1946
1947 jh = new_jh;
1948 new_jh = NULL; /* We consumed it */
1949 set_buffer_jbd(bh);
1950 bh->b_private = jh;
1951 jh->b_bh = bh;
1952 get_bh(bh);
1953 BUFFER_TRACE(bh, "added journal_head");
1954 }
1955 jh->b_jcount++;
1956 jbd_unlock_bh_journal_head(bh);
1957 if (new_jh)
1958 journal_free_journal_head(new_jh);
1959 return bh->b_private;
1960}
1961
1962/*
1963 * Grab a ref against this buffer_head's journal_head. If it ended up not
1964 * having a journal_head, return NULL
1965 */
1966struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1967{
1968 struct journal_head *jh = NULL;
1969
1970 jbd_lock_bh_journal_head(bh);
1971 if (buffer_jbd(bh)) {
1972 jh = bh2jh(bh);
1973 jh->b_jcount++;
1974 }
1975 jbd_unlock_bh_journal_head(bh);
1976 return jh;
1977}
1978
1979static void __journal_remove_journal_head(struct buffer_head *bh)
1980{
1981 struct journal_head *jh = bh2jh(bh);
1982
1983 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karabb189242011-06-24 23:11:59 +02001984 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1985 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1986 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1987 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1988 J_ASSERT_BH(bh, buffer_jbd(bh));
1989 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1990 BUFFER_TRACE(bh, "remove journal_head");
1991 if (jh->b_frozen_data) {
1992 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1993 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Jan Karabb189242011-06-24 23:11:59 +02001995 if (jh->b_committed_data) {
1996 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1997 jbd_free(jh->b_committed_data, bh->b_size);
1998 }
1999 bh->b_private = NULL;
2000 jh->b_bh = NULL; /* debug, really */
2001 clear_buffer_jbd(bh);
2002 journal_free_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/*
Jan Karabb189242011-06-24 23:11:59 +02002006 * Drop a reference on the passed journal_head. If it fell to zero then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * release the journal_head from the buffer_head.
2008 */
2009void journal_put_journal_head(struct journal_head *jh)
2010{
2011 struct buffer_head *bh = jh2bh(jh);
2012
2013 jbd_lock_bh_journal_head(bh);
2014 J_ASSERT_JH(jh, jh->b_jcount > 0);
2015 --jh->b_jcount;
Jan Karabb189242011-06-24 23:11:59 +02002016 if (!jh->b_jcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 __journal_remove_journal_head(bh);
Jan Karabb189242011-06-24 23:11:59 +02002018 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 __brelse(bh);
Jan Karabb189242011-06-24 23:11:59 +02002020 } else
2021 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
2024/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07002025 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07002027#ifdef CONFIG_JBD_DEBUG
2028
2029u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Jose R. Santosc2a91592007-10-18 23:39:22 -07002032static struct dentry *jbd_debugfs_dir;
2033static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Jose R. Santosc2a91592007-10-18 23:39:22 -07002035static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Jose R. Santosc2a91592007-10-18 23:39:22 -07002037 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2038 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002039 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07002040 jbd_debugfs_dir,
2041 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Jose R. Santosc2a91592007-10-18 23:39:22 -07002044static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Jose R. Santosc2a91592007-10-18 23:39:22 -07002046 debugfs_remove(jbd_debug);
2047 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050#else
2051
Jose R. Santosc2a91592007-10-18 23:39:22 -07002052static inline void jbd_create_debugfs_entry(void)
2053{
2054}
2055
2056static inline void jbd_remove_debugfs_entry(void)
2057{
2058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060#endif
2061
Christoph Lametere18b8902006-12-06 20:33:20 -08002062struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064static int __init journal_init_handle_cache(void)
2065{
2066 jbd_handle_cache = kmem_cache_create("journal_handle",
2067 sizeof(handle_t),
2068 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07002069 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002070 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (jbd_handle_cache == NULL) {
2072 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2073 return -ENOMEM;
2074 }
2075 return 0;
2076}
2077
2078static void journal_destroy_handle_cache(void)
2079{
2080 if (jbd_handle_cache)
2081 kmem_cache_destroy(jbd_handle_cache);
2082}
2083
2084/*
2085 * Module startup and shutdown
2086 */
2087
2088static int __init journal_init_caches(void)
2089{
2090 int ret;
2091
2092 ret = journal_init_revoke_caches();
2093 if (ret == 0)
2094 ret = journal_init_journal_head_cache();
2095 if (ret == 0)
2096 ret = journal_init_handle_cache();
2097 return ret;
2098}
2099
2100static void journal_destroy_caches(void)
2101{
2102 journal_destroy_revoke_caches();
2103 journal_destroy_journal_head_cache();
2104 journal_destroy_handle_cache();
2105}
2106
2107static int __init journal_init(void)
2108{
2109 int ret;
2110
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002111 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ret = journal_init_caches();
2114 if (ret != 0)
2115 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002116 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return ret;
2118}
2119
2120static void __exit journal_exit(void)
2121{
2122#ifdef CONFIG_JBD_DEBUG
2123 int n = atomic_read(&nr_journal_heads);
2124 if (n)
2125 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2126#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002127 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 journal_destroy_caches();
2129}
2130
2131MODULE_LICENSE("GPL");
2132module_init(journal_init);
2133module_exit(journal_exit);
2134