blob: 6510d6355729c021b79afe88adbff86c96a2112f [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 */
dingdinghuaf1015c42009-07-15 21:42:05 +0200313 atomic_set(&new_bh->b_count, 1);
314 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * If a new transaction has already done a buffer copy-out, then
318 * we use that version of the data for the commit.
319 */
320 jbd_lock_bh_state(bh_in);
321repeat:
322 if (jh_in->b_frozen_data) {
323 done_copy_out = 1;
324 new_page = virt_to_page(jh_in->b_frozen_data);
325 new_offset = offset_in_page(jh_in->b_frozen_data);
326 } else {
327 new_page = jh2bh(jh_in)->b_page;
328 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
329 }
330
Cong Wang8fb53c42011-11-25 23:14:31 +0800331 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * Check for escaping
334 */
335 if (*((__be32 *)(mapped_data + new_offset)) ==
336 cpu_to_be32(JFS_MAGIC_NUMBER)) {
337 need_copy_out = 1;
338 do_escape = 1;
339 }
Cong Wang8fb53c42011-11-25 23:14:31 +0800340 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /*
343 * Do we need to do a data copy?
344 */
345 if (need_copy_out && !done_copy_out) {
346 char *tmp;
347
348 jbd_unlock_bh_state(bh_in);
Mingming Caoc089d492007-10-16 18:38:25 -0400349 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 jbd_lock_bh_state(bh_in);
351 if (jh_in->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400352 jbd_free(tmp, bh_in->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto repeat;
354 }
355
356 jh_in->b_frozen_data = tmp;
Cong Wang8fb53c42011-11-25 23:14:31 +0800357 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
Cong Wang8fb53c42011-11-25 23:14:31 +0800359 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 new_page = virt_to_page(tmp);
362 new_offset = offset_in_page(tmp);
363 done_copy_out = 1;
364 }
365
366 /*
367 * Did we need to do an escaping? Now we've done all the
368 * copying, we can finally do so.
369 */
370 if (do_escape) {
Cong Wang8fb53c42011-11-25 23:14:31 +0800371 mapped_data = kmap_atomic(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *((unsigned int *)(mapped_data + new_offset)) = 0;
Cong Wang8fb53c42011-11-25 23:14:31 +0800373 kunmap_atomic(mapped_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
383
384 *jh_out = new_jh;
385
386 /*
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
390 */
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghuaf1015c42009-07-15 21:42:05 +0200392 spin_lock(&journal->j_list_lock);
393 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
394 spin_unlock(&journal->j_list_lock);
395 jbd_unlock_bh_state(bh_in);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 JBUFFER_TRACE(new_jh, "file as BJ_IO");
398 journal_file_buffer(new_jh, transaction, BJ_IO);
399
400 return do_escape | (done_copy_out << 1);
401}
402
403/*
404 * Allocation code for the journal file. Manage the space left in the
405 * journal, so that we can begin checkpointing when appropriate.
406 */
407
408/*
409 * __log_space_left: Return the number of free blocks left in the journal.
410 *
411 * Called with the journal already locked.
412 *
413 * Called under j_state_lock
414 */
415
416int __log_space_left(journal_t *journal)
417{
418 int left = journal->j_free;
419
420 assert_spin_locked(&journal->j_state_lock);
421
422 /*
423 * Be pessimistic here about the number of those free blocks which
424 * might be required for log descriptor control blocks.
425 */
426
427#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
428
429 left -= MIN_LOG_RESERVED_BLOCKS;
430
431 if (left <= 0)
432 return 0;
433 left -= (left >> 3);
434 return left;
435}
436
437/*
Jan Kara8fe4cd02009-02-11 13:04:25 -0800438 * Called under j_state_lock. Returns true if a transaction commit was started.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440int __log_start_commit(journal_t *journal, tid_t target)
441{
442 /*
Ted Ts'od9b01932011-04-30 13:17:11 -0400443 * The only transaction we can possibly wait upon is the
444 * currently running transaction (if it exists). Otherwise,
445 * the target tid must be an old one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Eric Sandeen7e2fb2d2012-12-18 11:03:57 -0600447 if (journal->j_commit_request != target &&
448 journal->j_running_transaction &&
Ted Ts'od9b01932011-04-30 13:17:11 -0400449 journal->j_running_transaction->t_tid == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200451 * We want a new commit: OK, mark the request and wakeup the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * commit thread. We do _not_ do the commit ourselves.
453 */
454
455 journal->j_commit_request = target;
456 jbd_debug(1, "JBD: requesting commit %d/%d\n",
457 journal->j_commit_request,
458 journal->j_commit_sequence);
459 wake_up(&journal->j_wait_commit);
460 return 1;
Ted Ts'od9b01932011-04-30 13:17:11 -0400461 } else if (!tid_geq(journal->j_commit_request, target))
462 /* This should never happen, but if it does, preserve
463 the evidence before kjournald goes into a loop and
464 increments j_commit_sequence beyond all recognition. */
465 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
466 journal->j_commit_request, journal->j_commit_sequence,
467 target, journal->j_running_transaction ?
468 journal->j_running_transaction->t_tid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470}
471
472int log_start_commit(journal_t *journal, tid_t tid)
473{
474 int ret;
475
476 spin_lock(&journal->j_state_lock);
477 ret = __log_start_commit(journal, tid);
478 spin_unlock(&journal->j_state_lock);
479 return ret;
480}
481
482/*
483 * Force and wait upon a commit if the calling process is not within
484 * transaction. This is used for forcing out undo-protected data which contains
485 * bitmaps, when the fs is running out of space.
486 *
487 * We can only force the running transaction if we don't have an active handle;
488 * otherwise, we will deadlock.
489 *
490 * Returns true if a transaction was started.
491 */
492int journal_force_commit_nested(journal_t *journal)
493{
494 transaction_t *transaction = NULL;
495 tid_t tid;
496
497 spin_lock(&journal->j_state_lock);
498 if (journal->j_running_transaction && !current->journal_info) {
499 transaction = journal->j_running_transaction;
500 __log_start_commit(journal, transaction->t_tid);
501 } else if (journal->j_committing_transaction)
502 transaction = journal->j_committing_transaction;
503
504 if (!transaction) {
505 spin_unlock(&journal->j_state_lock);
506 return 0; /* Nothing to retry */
507 }
508
509 tid = transaction->t_tid;
510 spin_unlock(&journal->j_state_lock);
511 log_wait_commit(journal, tid);
512 return 1;
513}
514
515/*
516 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800517 * if a transaction is going to be committed (or is currently already
518 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
520int journal_start_commit(journal_t *journal, tid_t *ptid)
521{
522 int ret = 0;
523
524 spin_lock(&journal->j_state_lock);
525 if (journal->j_running_transaction) {
526 tid_t tid = journal->j_running_transaction->t_tid;
527
Jan Kara8fe4cd02009-02-11 13:04:25 -0800528 __log_start_commit(journal, tid);
529 /* There's a running transaction and we've just made sure
530 * it's commit has been scheduled. */
531 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800533 ret = 1;
534 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
Artem Bityutskiy12810ad2012-07-25 18:12:07 +0300536 * If commit has been started, then we have to wait for
537 * completion of that transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800539 if (ptid)
540 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ret = 1;
542 }
543 spin_unlock(&journal->j_state_lock);
544 return ret;
545}
546
547/*
548 * Wait for a specified commit to complete.
549 * The caller may not hold the journal lock.
550 */
551int log_wait_commit(journal_t *journal, tid_t tid)
552{
553 int err = 0;
554
555#ifdef CONFIG_JBD_DEBUG
556 spin_lock(&journal->j_state_lock);
557 if (!tid_geq(journal->j_commit_request, tid)) {
558 printk(KERN_EMERG
559 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700560 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 spin_unlock(&journal->j_state_lock);
563#endif
564 spin_lock(&journal->j_state_lock);
Jan Karae678a4f2013-03-27 17:30:59 +0100565 /*
566 * Not running or committing trans? Must be already committed. This
567 * saves us from waiting for a *long* time when tid overflows.
568 */
569 if (!((journal->j_running_transaction &&
570 journal->j_running_transaction->t_tid == tid) ||
571 (journal->j_committing_transaction &&
572 journal->j_committing_transaction->t_tid == tid)))
573 goto out_unlock;
574
Jan Kara2db938b2011-02-21 17:25:37 +0100575 if (!tid_geq(journal->j_commit_waited, tid))
576 journal->j_commit_waited = tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 while (tid_gt(tid, journal->j_commit_sequence)) {
578 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
579 tid, journal->j_commit_sequence);
580 wake_up(&journal->j_wait_commit);
581 spin_unlock(&journal->j_state_lock);
582 wait_event(journal->j_wait_done_commit,
583 !tid_gt(tid, journal->j_commit_sequence));
584 spin_lock(&journal->j_state_lock);
585 }
Jan Karae678a4f2013-03-27 17:30:59 +0100586out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_unlock(&journal->j_state_lock);
588
589 if (unlikely(is_journal_aborted(journal))) {
590 printk(KERN_EMERG "journal commit I/O error\n");
591 err = -EIO;
592 }
593 return err;
594}
595
596/*
Jan Kara03f4d802010-04-15 22:16:24 +0200597 * Return 1 if a given transaction has not yet sent barrier request
598 * connected with a transaction commit. If 0 is returned, transaction
599 * may or may not have sent the barrier. Used to avoid sending barrier
600 * twice in common cases.
601 */
602int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
603{
604 int ret = 0;
605 transaction_t *commit_trans;
606
607 if (!(journal->j_flags & JFS_BARRIER))
608 return 0;
609 spin_lock(&journal->j_state_lock);
610 /* Transaction already committed? */
611 if (tid_geq(journal->j_commit_sequence, tid))
612 goto out;
613 /*
614 * Transaction is being committed and we already proceeded to
615 * writing commit record?
616 */
617 commit_trans = journal->j_committing_transaction;
618 if (commit_trans && commit_trans->t_tid == tid &&
619 commit_trans->t_state >= T_COMMIT_RECORD)
620 goto out;
621 ret = 1;
622out:
623 spin_unlock(&journal->j_state_lock);
624 return ret;
625}
Jan Kara52779702010-04-15 22:24:26 +0200626EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200627
628/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * Log buffer allocation routines:
630 */
631
Jan Kara9c28cbc2009-08-03 19:21:00 +0200632int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200634 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 spin_lock(&journal->j_state_lock);
637 J_ASSERT(journal->j_free > 1);
638
639 blocknr = journal->j_head;
640 journal->j_head++;
641 journal->j_free--;
642 if (journal->j_head == journal->j_last)
643 journal->j_head = journal->j_first;
644 spin_unlock(&journal->j_state_lock);
645 return journal_bmap(journal, blocknr, retp);
646}
647
648/*
649 * Conversion of logical to physical block numbers for the journal
650 *
651 * On external journals the journal blocks are identity-mapped, so
652 * this is a no-op. If needed, we can use j_blk_offset - everything is
653 * ready.
654 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200655int journal_bmap(journal_t *journal, unsigned int blocknr,
656 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200659 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (journal->j_inode) {
662 ret = bmap(journal->j_inode, blocknr);
663 if (ret)
664 *retp = ret;
665 else {
666 char b[BDEVNAME_SIZE];
667
668 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200669 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700670 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 blocknr,
672 bdevname(journal->j_dev, b));
673 err = -EIO;
674 __journal_abort_soft(journal, err);
675 }
676 } else {
677 *retp = blocknr; /* +journal->j_blk_offset */
678 }
679 return err;
680}
681
682/*
683 * We play buffer_head aliasing tricks to write data/metadata blocks to
684 * the journal without copying their contents, but for journal
685 * descriptor blocks we do need to generate bona fide buffers.
686 *
687 * After the caller of journal_get_descriptor_buffer() has finished modifying
688 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
689 * But we don't bother doing that, so there will be coherency problems with
690 * mmaps of blockdevs which hold live JBD-controlled filesystems.
691 */
692struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
693{
694 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200695 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int err;
697
698 err = journal_next_log_block(journal, &blocknr);
699
700 if (err)
701 return NULL;
702
703 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700704 if (!bh)
705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 lock_buffer(bh);
707 memset(bh->b_data, 0, journal->j_blocksize);
708 set_buffer_uptodate(bh);
709 unlock_buffer(bh);
710 BUFFER_TRACE(bh, "return this buffer");
711 return journal_add_journal_head(bh);
712}
713
714/*
715 * Management for journal control blocks: functions to create and
716 * destroy journal_t structures, and to initialise and read existing
717 * journal blocks from disk. */
718
719/* First: create and setup a journal_t object in memory. We initialise
720 * very few fields yet: that has to wait until we have created the
721 * journal structures from from scratch, or loaded them from disk. */
722
723static journal_t * journal_init_common (void)
724{
725 journal_t *journal;
726 int err;
727
Mingming Cao8c3478a2007-10-18 23:39:20 -0700728 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!journal)
730 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 init_waitqueue_head(&journal->j_wait_transaction_locked);
733 init_waitqueue_head(&journal->j_wait_logspace);
734 init_waitqueue_head(&journal->j_wait_done_commit);
735 init_waitqueue_head(&journal->j_wait_checkpoint);
736 init_waitqueue_head(&journal->j_wait_commit);
737 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800738 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_lock_init(&journal->j_revoke_lock);
740 spin_lock_init(&journal->j_list_lock);
741 spin_lock_init(&journal->j_state_lock);
742
743 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
744
745 /* The journal is marked for error until we succeed with recovery! */
746 journal->j_flags = JFS_ABORT;
747
748 /* Set up a default-sized revoke table for the new mount. */
749 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
750 if (err) {
751 kfree(journal);
752 goto fail;
753 }
754 return journal;
755fail:
756 return NULL;
757}
758
759/* journal_init_dev and journal_init_inode:
760 *
761 * Create a journal structure assigned some fixed set of disk blocks to
762 * the journal. We don't actually touch those disk blocks yet, but we
763 * need to set up all of the mapping information to tell the journaling
764 * system where the journal blocks are.
765 *
766 */
767
768/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700769 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * @bdev: Block device on which to create the journal
771 * @fs_dev: Device which hold journalled filesystem for this journal.
772 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700773 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700775 *
776 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * journal_init_dev creates a journal which maps a fixed contiguous
779 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700780 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
782journal_t * journal_init_dev(struct block_device *bdev,
783 struct block_device *fs_dev,
784 int start, int len, int blocksize)
785{
786 journal_t *journal = journal_init_common();
787 struct buffer_head *bh;
788 int n;
789
790 if (!journal)
791 return NULL;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700794 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 n = journal->j_blocksize / sizeof(journal_block_tag_t);
796 journal->j_wbufsize = n;
797 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
798 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300799 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700800 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700801 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700803 journal->j_dev = bdev;
804 journal->j_fs_dev = fs_dev;
805 journal->j_blk_offset = start;
806 journal->j_maxlen = len;
807
808 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700809 if (!bh) {
810 printk(KERN_ERR
811 "%s: Cannot get buffer for journal superblock\n",
812 __func__);
813 goto out_err;
814 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700815 journal->j_sb_buffer = bh;
816 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700819out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800820 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700821 kfree(journal);
822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700824
825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
827 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * journal_init_inode creates a journal which maps an on-disk inode as
830 * the journal. The inode must exist already, must support bmap() and
831 * must have all data blocks preallocated.
832 */
833journal_t * journal_init_inode (struct inode *inode)
834{
835 struct buffer_head *bh;
836 journal_t *journal = journal_init_common();
837 int err;
838 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200839 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (!journal)
842 return NULL;
843
844 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
845 journal->j_inode = inode;
846 jbd_debug(1,
847 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700848 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 (long long) inode->i_size,
850 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
851
852 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
853 journal->j_blocksize = inode->i_sb->s_blocksize;
854
855 /* journal descriptor can store up to n blocks -bzzz */
856 n = journal->j_blocksize / sizeof(journal_block_tag_t);
857 journal->j_wbufsize = n;
858 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
859 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300860 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700861 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700862 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
865 err = journal_bmap(journal, 0, &blocknr);
866 /* If that failed, give up */
867 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -0800868 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700869 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700870 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700874 if (!bh) {
875 printk(KERN_ERR
876 "%s: Cannot get buffer for journal superblock\n",
877 __func__);
878 goto out_err;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 journal->j_sb_buffer = bh;
881 journal->j_superblock = (journal_superblock_t *)bh->b_data;
882
883 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700884out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800885 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700886 kfree(journal);
887 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700890/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * If the journal init or create aborts, we need to mark the journal
892 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700893 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895static void journal_fail_superblock (journal_t *journal)
896{
897 struct buffer_head *bh = journal->j_sb_buffer;
898 brelse(bh);
899 journal->j_sb_buffer = NULL;
900}
901
902/*
903 * Given a journal_t structure, initialise the various fields for
904 * startup of a new journaling session. We use this both when creating
905 * a journal, and after recovering an old journal to reset it for
906 * subsequent use.
907 */
908
909static int journal_reset(journal_t *journal)
910{
911 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200912 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 first = be32_to_cpu(sb->s_first);
915 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200916 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200917 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200918 first, last);
919 journal_fail_superblock(journal);
920 return -EINVAL;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 journal->j_first = first;
924 journal->j_last = last;
925
926 journal->j_head = first;
927 journal->j_tail = first;
928 journal->j_free = last - first;
929
930 journal->j_tail_sequence = journal->j_transaction_sequence;
931 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
932 journal->j_commit_request = journal->j_commit_sequence;
933
934 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
935
Jan Kara9754e392012-04-07 12:33:03 +0200936 /*
937 * As a special case, if the on-disk copy is already marked as needing
938 * no recovery (s_start == 0), then we can safely defer the superblock
939 * update until the next commit by setting JFS_FLUSHED. This avoids
940 * attempting a write to a potential-readonly device.
941 */
942 if (sb->s_start == 0) {
943 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
944 "(start %u, seq %d, errno %d)\n",
945 journal->j_tail, journal->j_tail_sequence,
946 journal->j_errno);
947 journal->j_flags |= JFS_FLUSHED;
948 } else {
Jan Kara1ce84862012-04-07 12:50:13 +0200949 /* Lock here to make assertions happy... */
950 mutex_lock(&journal->j_checkpoint_mutex);
Jan Karafd2cbd42012-04-07 11:05:19 +0200951 /*
952 * Update log tail information. We use WRITE_FUA since new
953 * transaction will start reusing journal space and so we
954 * must make sure information about current log tail is on
955 * disk before that.
956 */
957 journal_update_sb_log_tail(journal,
958 journal->j_tail_sequence,
959 journal->j_tail,
960 WRITE_FUA);
Jan Kara1ce84862012-04-07 12:50:13 +0200961 mutex_unlock(&journal->j_checkpoint_mutex);
Jan Kara9754e392012-04-07 12:33:03 +0200962 }
Pavel Emelianov97f06782007-05-08 00:30:42 -0700963 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700966/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * int journal_create() - Initialise the new journal file
968 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700969 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * Given a journal_t structure which tells us which disk blocks we can
971 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700972 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 **/
974int journal_create(journal_t *journal)
975{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200976 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct buffer_head *bh;
978 journal_superblock_t *sb;
979 int i, err;
980
981 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
982 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
983 journal->j_maxlen);
984 journal_fail_superblock(journal);
985 return -EINVAL;
986 }
987
988 if (journal->j_inode == NULL) {
989 /*
990 * We don't know what block to start at!
991 */
992 printk(KERN_EMERG
993 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700994 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 BUG();
996 }
997
998 /* Zero out the entire journal on disk. We cannot afford to
999 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
1000 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1001 for (i = 0; i < journal->j_maxlen; i++) {
1002 err = journal_bmap(journal, i, &blocknr);
1003 if (err)
1004 return err;
1005 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Namhyung Kim2a0e3382010-10-13 17:17:18 +09001006 if (unlikely(!bh))
1007 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 lock_buffer(bh);
1009 memset (bh->b_data, 0, journal->j_blocksize);
1010 BUFFER_TRACE(bh, "marking dirty");
1011 mark_buffer_dirty(bh);
1012 BUFFER_TRACE(bh, "marking uptodate");
1013 set_buffer_uptodate(bh);
1014 unlock_buffer(bh);
1015 __brelse(bh);
1016 }
1017
1018 sync_blockdev(journal->j_dev);
1019 jbd_debug(1, "JBD: journal cleared.\n");
1020
1021 /* OK, fill in the initial static fields in the new superblock */
1022 sb = journal->j_superblock;
1023
1024 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1025 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1026
1027 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1028 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1029 sb->s_first = cpu_to_be32(1);
1030
1031 journal->j_transaction_sequence = 1;
1032
1033 journal->j_flags &= ~JFS_ABORT;
1034 journal->j_format_version = 2;
1035
1036 return journal_reset(journal);
1037}
1038
Jan Karafd2cbd42012-04-07 11:05:19 +02001039static void journal_write_superblock(journal_t *journal, int write_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct buffer_head *bh = journal->j_sb_buffer;
Jan Karafd2cbd42012-04-07 11:05:19 +02001042 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Jan Karafd2cbd42012-04-07 11:05:19 +02001044 trace_journal_write_superblock(journal, write_op);
1045 if (!(journal->j_flags & JFS_BARRIER))
1046 write_op &= ~(REQ_FUA | REQ_FLUSH);
1047 lock_buffer(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001048 if (buffer_write_io_error(bh)) {
1049 char b[BDEVNAME_SIZE];
1050 /*
1051 * Oh, dear. A previous attempt to write the journal
1052 * superblock failed. This could happen because the
1053 * USB device was yanked out. Or it could happen to
1054 * be a transient write error and maybe the block will
1055 * be remapped. Nothing we can do but to retry the
1056 * write and hope for the best.
1057 */
1058 printk(KERN_ERR "JBD: previous I/O error detected "
1059 "for journal superblock update for %s.\n",
1060 journal_dev_name(journal, b));
1061 clear_buffer_write_io_error(bh);
1062 set_buffer_uptodate(bh);
1063 }
1064
Jan Karafd2cbd42012-04-07 11:05:19 +02001065 get_bh(bh);
1066 bh->b_end_io = end_buffer_write_sync;
1067 ret = submit_bh(write_op, bh);
1068 wait_on_buffer(bh);
Jan Kara9754e392012-04-07 12:33:03 +02001069 if (buffer_write_io_error(bh)) {
Jan Kara9754e392012-04-07 12:33:03 +02001070 clear_buffer_write_io_error(bh);
1071 set_buffer_uptodate(bh);
Jan Karafd2cbd42012-04-07 11:05:19 +02001072 ret = -EIO;
1073 }
1074 if (ret) {
1075 char b[BDEVNAME_SIZE];
1076 printk(KERN_ERR "JBD: Error %d detected "
1077 "when updating journal superblock for %s.\n",
1078 ret, journal_dev_name(journal, b));
Jan Kara9754e392012-04-07 12:33:03 +02001079 }
1080}
1081
1082/**
1083 * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1084 * @journal: The journal to update.
Jan Karafd2cbd42012-04-07 11:05:19 +02001085 * @tail_tid: TID of the new transaction at the tail of the log
1086 * @tail_block: The first block of the transaction at the tail of the log
1087 * @write_op: With which operation should we write the journal sb
Jan Kara9754e392012-04-07 12:33:03 +02001088 *
1089 * Update a journal's superblock information about log tail and write it to
1090 * disk, waiting for the IO to complete.
1091 */
Jan Karafd2cbd42012-04-07 11:05:19 +02001092void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1093 unsigned int tail_block, int write_op)
Jan Kara9754e392012-04-07 12:33:03 +02001094{
1095 journal_superblock_t *sb = journal->j_superblock;
1096
Jan Kara1ce84862012-04-07 12:50:13 +02001097 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Jan Karafd2cbd42012-04-07 11:05:19 +02001098 jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
1099 tail_block, tail_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jan Karafd2cbd42012-04-07 11:05:19 +02001101 sb->s_sequence = cpu_to_be32(tail_tid);
1102 sb->s_start = cpu_to_be32(tail_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Jan Karafd2cbd42012-04-07 11:05:19 +02001104 journal_write_superblock(journal, write_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Jan Kara9754e392012-04-07 12:33:03 +02001106 /* Log is no longer empty */
1107 spin_lock(&journal->j_state_lock);
1108 WARN_ON(!sb->s_sequence);
1109 journal->j_flags &= ~JFS_FLUSHED;
1110 spin_unlock(&journal->j_state_lock);
1111}
1112
1113/**
1114 * mark_journal_empty() - Mark on disk journal as empty.
1115 * @journal: The journal to update.
1116 *
1117 * Update a journal's dynamic superblock fields to show that journal is empty.
1118 * Write updated superblock to disk waiting for IO to complete.
1119 */
1120static void mark_journal_empty(journal_t *journal)
1121{
1122 journal_superblock_t *sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jan Kara1ce84862012-04-07 12:50:13 +02001124 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 spin_lock(&journal->j_state_lock);
Jan Kara2e84f262012-08-15 13:50:27 +02001126 /* Is it already empty? */
1127 if (sb->s_start == 0) {
1128 spin_unlock(&journal->j_state_lock);
1129 return;
1130 }
Jan Kara9754e392012-04-07 12:33:03 +02001131 jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1132 journal->j_tail_sequence);
1133
1134 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1135 sb->s_start = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 spin_unlock(&journal->j_state_lock);
Jan Kara9754e392012-04-07 12:33:03 +02001137
Jan Karafd2cbd42012-04-07 11:05:19 +02001138 journal_write_superblock(journal, WRITE_FUA);
Jan Kara9754e392012-04-07 12:33:03 +02001139
1140 spin_lock(&journal->j_state_lock);
1141 /* Log is empty */
1142 journal->j_flags |= JFS_FLUSHED;
1143 spin_unlock(&journal->j_state_lock);
1144}
1145
1146/**
1147 * journal_update_sb_errno() - Update error in the journal.
1148 * @journal: The journal to update.
1149 *
1150 * Update a journal's errno. Write updated superblock to disk waiting for IO
1151 * to complete.
1152 */
1153static void journal_update_sb_errno(journal_t *journal)
1154{
1155 journal_superblock_t *sb = journal->j_superblock;
1156
1157 spin_lock(&journal->j_state_lock);
1158 jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1159 journal->j_errno);
1160 sb->s_errno = cpu_to_be32(journal->j_errno);
1161 spin_unlock(&journal->j_state_lock);
1162
Jan Karafd2cbd42012-04-07 11:05:19 +02001163 journal_write_superblock(journal, WRITE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Read the superblock for a given journal, performing initial
1168 * validation of the format.
1169 */
1170
1171static int journal_get_superblock(journal_t *journal)
1172{
1173 struct buffer_head *bh;
1174 journal_superblock_t *sb;
1175 int err = -EIO;
1176
1177 bh = journal->j_sb_buffer;
1178
1179 J_ASSERT(bh != NULL);
1180 if (!buffer_uptodate(bh)) {
1181 ll_rw_block(READ, 1, &bh);
1182 wait_on_buffer(bh);
1183 if (!buffer_uptodate(bh)) {
1184 printk (KERN_ERR
1185 "JBD: IO error reading journal superblock\n");
1186 goto out;
1187 }
1188 }
1189
1190 sb = journal->j_superblock;
1191
1192 err = -EINVAL;
1193
1194 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1195 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1196 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1197 goto out;
1198 }
1199
1200 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1201 case JFS_SUPERBLOCK_V1:
1202 journal->j_format_version = 1;
1203 break;
1204 case JFS_SUPERBLOCK_V2:
1205 journal->j_format_version = 2;
1206 break;
1207 default:
1208 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1209 goto out;
1210 }
1211
1212 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1213 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1214 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1215 printk (KERN_WARNING "JBD: journal file too short\n");
1216 goto out;
1217 }
1218
Eryu Guan87622022011-11-01 19:04:59 -04001219 if (be32_to_cpu(sb->s_first) == 0 ||
1220 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1221 printk(KERN_WARNING
1222 "JBD: Invalid start block of journal: %u\n",
1223 be32_to_cpu(sb->s_first));
1224 goto out;
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
1228
1229out:
1230 journal_fail_superblock(journal);
1231 return err;
1232}
1233
1234/*
1235 * Load the on-disk journal superblock and read the key fields into the
1236 * journal_t.
1237 */
1238
1239static int load_superblock(journal_t *journal)
1240{
1241 int err;
1242 journal_superblock_t *sb;
1243
1244 err = journal_get_superblock(journal);
1245 if (err)
1246 return err;
1247
1248 sb = journal->j_superblock;
1249
1250 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1251 journal->j_tail = be32_to_cpu(sb->s_start);
1252 journal->j_first = be32_to_cpu(sb->s_first);
1253 journal->j_last = be32_to_cpu(sb->s_maxlen);
1254 journal->j_errno = be32_to_cpu(sb->s_errno);
1255
1256 return 0;
1257}
1258
1259
1260/**
1261 * int journal_load() - Read journal from disk.
1262 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * Given a journal_t structure which tells us which disk blocks contain
1265 * a journal, read the journal from disk to initialise the in-memory
1266 * structures.
1267 */
1268int journal_load(journal_t *journal)
1269{
1270 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001271 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 err = load_superblock(journal);
1274 if (err)
1275 return err;
1276
Badari Pulavartyea817392006-08-27 01:23:52 -07001277 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* If this is a V2 superblock, then we have to check the
1279 * features flags on it. */
1280
1281 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if ((sb->s_feature_ro_compat &
1283 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1284 (sb->s_feature_incompat &
1285 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1286 printk (KERN_WARNING
1287 "JBD: Unrecognised features on journal\n");
1288 return -EINVAL;
1289 }
1290 }
1291
1292 /* Let the recovery code check whether it needs to recover any
1293 * data from the journal. */
1294 if (journal_recover(journal))
1295 goto recovery_error;
1296
1297 /* OK, we've finished with the dynamic journal bits:
1298 * reinitialise the dynamic contents of the superblock in memory
1299 * and reset them on disk. */
1300 if (journal_reset(journal))
1301 goto recovery_error;
1302
1303 journal->j_flags &= ~JFS_ABORT;
1304 journal->j_flags |= JFS_LOADED;
1305 return 0;
1306
1307recovery_error:
1308 printk (KERN_WARNING "JBD: recovery failed\n");
1309 return -EIO;
1310}
1311
1312/**
1313 * void journal_destroy() - Release a journal_t structure.
1314 * @journal: Journal to act on.
1315 *
1316 * Release a journal_t structure once it is no longer in use by the
1317 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001318 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001320int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001322 int err = 0;
1323
Jan Kara03f4d802010-04-15 22:16:24 +02001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* Wait for the commit thread to wake up and die. */
1326 journal_kill_thread(journal);
1327
1328 /* Force a final log commit */
1329 if (journal->j_running_transaction)
1330 journal_commit_transaction(journal);
1331
1332 /* Force any old transactions to disk */
1333
Jan Kara1ce84862012-04-07 12:50:13 +02001334 /* We cannot race with anybody but must keep assertions happy */
1335 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* Totally anal locking here... */
1337 spin_lock(&journal->j_list_lock);
1338 while (journal->j_checkpoint_transactions != NULL) {
1339 spin_unlock(&journal->j_list_lock);
1340 log_do_checkpoint(journal);
1341 spin_lock(&journal->j_list_lock);
1342 }
1343
1344 J_ASSERT(journal->j_running_transaction == NULL);
1345 J_ASSERT(journal->j_committing_transaction == NULL);
1346 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1347 spin_unlock(&journal->j_list_lock);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001350 if (!is_journal_aborted(journal)) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001351 journal->j_tail_sequence =
1352 ++journal->j_transaction_sequence;
Jan Kara9754e392012-04-07 12:33:03 +02001353 mark_journal_empty(journal);
1354 } else
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001355 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 brelse(journal->j_sb_buffer);
1357 }
Jan Kara1ce84862012-04-07 12:50:13 +02001358 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (journal->j_inode)
1361 iput(journal->j_inode);
1362 if (journal->j_revoke)
1363 journal_destroy_revoke(journal);
1364 kfree(journal->j_wbuf);
1365 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001366
1367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370
1371/**
1372 *int journal_check_used_features () - Check if features specified are used.
1373 * @journal: Journal to check.
1374 * @compat: bitmask of compatible features
1375 * @ro: bitmask of features that force read-only mount
1376 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001379 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 **/
1381
1382int journal_check_used_features (journal_t *journal, unsigned long compat,
1383 unsigned long ro, unsigned long incompat)
1384{
1385 journal_superblock_t *sb;
1386
1387 if (!compat && !ro && !incompat)
1388 return 1;
1389 if (journal->j_format_version == 1)
1390 return 0;
1391
1392 sb = journal->j_superblock;
1393
1394 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1395 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1396 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1397 return 1;
1398
1399 return 0;
1400}
1401
1402/**
1403 * int journal_check_available_features() - Check feature set in journalling layer
1404 * @journal: Journal to check.
1405 * @compat: bitmask of compatible features
1406 * @ro: bitmask of features that force read-only mount
1407 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001408 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * Check whether the journaling code supports the use of
1410 * all of a given set of features on this journal. Return true
1411 * (non-zero) if it can. */
1412
1413int journal_check_available_features (journal_t *journal, unsigned long compat,
1414 unsigned long ro, unsigned long incompat)
1415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!compat && !ro && !incompat)
1417 return 1;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* We can support any known requested features iff the
1420 * superblock is in version 2. Otherwise we fail to support any
1421 * extended sb features. */
1422
1423 if (journal->j_format_version != 2)
1424 return 0;
1425
1426 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1427 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1428 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1429 return 1;
1430
1431 return 0;
1432}
1433
1434/**
1435 * int journal_set_features () - Mark a given journal feature in the superblock
1436 * @journal: Journal to act on.
1437 * @compat: bitmask of compatible features
1438 * @ro: bitmask of features that force read-only mount
1439 * @incompat: bitmask of incompatible features
1440 *
1441 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001442 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *
1444 */
1445
1446int journal_set_features (journal_t *journal, unsigned long compat,
1447 unsigned long ro, unsigned long incompat)
1448{
1449 journal_superblock_t *sb;
1450
1451 if (journal_check_used_features(journal, compat, ro, incompat))
1452 return 1;
1453
1454 if (!journal_check_available_features(journal, compat, ro, incompat))
1455 return 0;
1456
1457 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1458 compat, ro, incompat);
1459
1460 sb = journal->j_superblock;
1461
1462 sb->s_feature_compat |= cpu_to_be32(compat);
1463 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1464 sb->s_feature_incompat |= cpu_to_be32(incompat);
1465
1466 return 1;
1467}
1468
1469
1470/**
1471 * int journal_update_format () - Update on-disk journal structure.
1472 * @journal: Journal to act on.
1473 *
1474 * Given an initialised but unloaded journal struct, poke about in the
1475 * on-disk structure to update it to the most recent supported version.
1476 */
1477int journal_update_format (journal_t *journal)
1478{
1479 journal_superblock_t *sb;
1480 int err;
1481
1482 err = journal_get_superblock(journal);
1483 if (err)
1484 return err;
1485
1486 sb = journal->j_superblock;
1487
1488 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1489 case JFS_SUPERBLOCK_V2:
1490 return 0;
1491 case JFS_SUPERBLOCK_V1:
1492 return journal_convert_superblock_v1(journal, sb);
1493 default:
1494 break;
1495 }
1496 return -EINVAL;
1497}
1498
1499static int journal_convert_superblock_v1(journal_t *journal,
1500 journal_superblock_t *sb)
1501{
1502 int offset, blocksize;
1503 struct buffer_head *bh;
1504
1505 printk(KERN_WARNING
1506 "JBD: Converting superblock from version 1 to 2.\n");
1507
1508 /* Pre-initialise new fields to zero */
1509 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1510 blocksize = be32_to_cpu(sb->s_blocksize);
1511 memset(&sb->s_feature_compat, 0, blocksize-offset);
1512
1513 sb->s_nr_users = cpu_to_be32(1);
1514 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1515 journal->j_format_version = 2;
1516
1517 bh = journal->j_sb_buffer;
1518 BUFFER_TRACE(bh, "marking dirty");
1519 mark_buffer_dirty(bh);
1520 sync_dirty_buffer(bh);
1521 return 0;
1522}
1523
1524
1525/**
1526 * int journal_flush () - Flush journal
1527 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001528 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * Flush all data for a given journal to disk and empty the journal.
1530 * Filesystems can use this when remounting readonly to ensure that
1531 * recovery does not need to happen on remount.
1532 */
1533
1534int journal_flush(journal_t *journal)
1535{
1536 int err = 0;
1537 transaction_t *transaction = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 spin_lock(&journal->j_state_lock);
1540
1541 /* Force everything buffered to the log... */
1542 if (journal->j_running_transaction) {
1543 transaction = journal->j_running_transaction;
1544 __log_start_commit(journal, transaction->t_tid);
1545 } else if (journal->j_committing_transaction)
1546 transaction = journal->j_committing_transaction;
1547
1548 /* Wait for the log commit to complete... */
1549 if (transaction) {
1550 tid_t tid = transaction->t_tid;
1551
1552 spin_unlock(&journal->j_state_lock);
1553 log_wait_commit(journal, tid);
1554 } else {
1555 spin_unlock(&journal->j_state_lock);
1556 }
1557
1558 /* ...and flush everything in the log out to disk. */
1559 spin_lock(&journal->j_list_lock);
1560 while (!err && journal->j_checkpoint_transactions != NULL) {
1561 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001562 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001564 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 spin_lock(&journal->j_list_lock);
1566 }
1567 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001568
1569 if (is_journal_aborted(journal))
1570 return -EIO;
1571
Jan Kara1ce84862012-04-07 12:50:13 +02001572 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 cleanup_journal_tail(journal);
1574
1575 /* Finally, mark the journal as really needing no recovery.
1576 * This sets s_start==0 in the underlying superblock, which is
1577 * the magic code for a fully-recovered superblock. Any future
1578 * commits of data to the journal will restore the current
1579 * s_start value. */
Jan Kara9754e392012-04-07 12:33:03 +02001580 mark_journal_empty(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001581 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 spin_lock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 J_ASSERT(!journal->j_running_transaction);
1584 J_ASSERT(!journal->j_committing_transaction);
1585 J_ASSERT(!journal->j_checkpoint_transactions);
1586 J_ASSERT(journal->j_head == journal->j_tail);
1587 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1588 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/**
1593 * int journal_wipe() - Wipe journal contents
1594 * @journal: Journal to act on.
1595 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001596 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 * Wipe out all of the contents of a journal, safely. This will produce
1598 * a warning if the journal contains any valid recovery information.
1599 * Must be called between journal_init_*() and journal_load().
1600 *
1601 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1602 * we merely suppress recovery.
1603 */
1604
1605int journal_wipe(journal_t *journal, int write)
1606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int err = 0;
1608
1609 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1610
1611 err = load_superblock(journal);
1612 if (err)
1613 return err;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!journal->j_tail)
1616 goto no_recovery;
1617
1618 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1619 write ? "Clearing" : "Ignoring");
1620
1621 err = journal_skip_recovery(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001622 if (write) {
1623 /* Lock to make assertions happy... */
1624 mutex_lock(&journal->j_checkpoint_mutex);
Jan Kara9754e392012-04-07 12:33:03 +02001625 mark_journal_empty(journal);
Jan Kara1ce84862012-04-07 12:50:13 +02001626 mutex_unlock(&journal->j_checkpoint_mutex);
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 no_recovery:
1630 return err;
1631}
1632
1633/*
1634 * journal_dev_name: format a character string to describe on what
1635 * device this journal is present.
1636 */
1637
Adrian Bunk022a4a72005-09-06 15:16:41 -07001638static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct block_device *bdev;
1641
1642 if (journal->j_inode)
1643 bdev = journal->j_inode->i_sb->s_bdev;
1644 else
1645 bdev = journal->j_dev;
1646
1647 return bdevname(bdev, buffer);
1648}
1649
1650/*
1651 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001652 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 *
1654 * Two internal function, which provide abort to te jbd layer
1655 * itself are here.
1656 */
1657
1658/*
1659 * Quick version for internal journal use (doesn't lock the journal).
1660 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1661 * and don't attempt to make any other journal updates.
1662 */
Adrian Bunk53308382008-02-06 01:40:12 -08001663static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 transaction_t *transaction;
1666 char b[BDEVNAME_SIZE];
1667
1668 if (journal->j_flags & JFS_ABORT)
1669 return;
1670
1671 printk(KERN_ERR "Aborting journal on device %s.\n",
1672 journal_dev_name(journal, b));
1673
1674 spin_lock(&journal->j_state_lock);
1675 journal->j_flags |= JFS_ABORT;
1676 transaction = journal->j_running_transaction;
1677 if (transaction)
1678 __log_start_commit(journal, transaction->t_tid);
1679 spin_unlock(&journal->j_state_lock);
1680}
1681
1682/* Soft abort: record the abort error status in the journal superblock,
1683 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001684static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 if (journal->j_flags & JFS_ABORT)
1687 return;
1688
1689 if (!journal->j_errno)
1690 journal->j_errno = errno;
1691
1692 __journal_abort_hard(journal);
1693
1694 if (errno)
Jan Kara9754e392012-04-07 12:33:03 +02001695 journal_update_sb_errno(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
1698/**
1699 * void journal_abort () - Shutdown the journal immediately.
1700 * @journal: the journal to shutdown.
1701 * @errno: an error number to record in the journal indicating
1702 * the reason for the shutdown.
1703 *
1704 * Perform a complete, immediate shutdown of the ENTIRE
1705 * journal (not of a single transaction). This operation cannot be
1706 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001707 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * The journal_abort function is intended to support higher level error
1709 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1710 * mode.
1711 *
1712 * Journal abort has very specific semantics. Any existing dirty,
1713 * unjournaled buffers in the main filesystem will still be written to
1714 * disk by bdflush, but the journaling mechanism will be suspended
1715 * immediately and no further transaction commits will be honoured.
1716 *
1717 * Any dirty, journaled buffers will be written back to disk without
1718 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1719 * filesystem, but we _do_ attempt to leave as much data as possible
1720 * behind for fsck to use for cleanup.
1721 *
1722 * Any attempt to get a new transaction handle on a journal which is in
1723 * ABORT state will just result in an -EROFS error return. A
1724 * journal_stop on an existing handle will return -EIO if we have
1725 * entered abort state during the update.
1726 *
1727 * Recursive transactions are not disturbed by journal abort until the
1728 * final journal_stop, which will receive the -EIO error.
1729 *
1730 * Finally, the journal_abort call allows the caller to supply an errno
1731 * which will be recorded (if possible) in the journal superblock. This
1732 * allows a client to record failure conditions in the middle of a
1733 * transaction without having to complete the transaction to record the
1734 * failure to disk. ext3_error, for example, now uses this
1735 * functionality.
1736 *
1737 * Errors which originate from within the journaling layer will NOT
1738 * supply an errno; a null errno implies that absolutely no further
1739 * writes are done to the journal (unless there are any already in
1740 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001741 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743
1744void journal_abort(journal_t *journal, int errno)
1745{
1746 __journal_abort_soft(journal, errno);
1747}
1748
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * int journal_errno () - returns the journal's error state.
1751 * @journal: journal to examine.
1752 *
1753 * This is the errno numbet set with journal_abort(), the last
1754 * time the journal was mounted - if the journal was stopped
1755 * without calling abort this will be 0.
1756 *
1757 * If the journal has been aborted on this mount time -EROFS will
1758 * be returned.
1759 */
1760int journal_errno(journal_t *journal)
1761{
1762 int err;
1763
1764 spin_lock(&journal->j_state_lock);
1765 if (journal->j_flags & JFS_ABORT)
1766 err = -EROFS;
1767 else
1768 err = journal->j_errno;
1769 spin_unlock(&journal->j_state_lock);
1770 return err;
1771}
1772
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001773/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * int journal_clear_err () - clears the journal's error state
1775 * @journal: journal to act on.
1776 *
1777 * An error must be cleared or Acked to take a FS out of readonly
1778 * mode.
1779 */
1780int journal_clear_err(journal_t *journal)
1781{
1782 int err = 0;
1783
1784 spin_lock(&journal->j_state_lock);
1785 if (journal->j_flags & JFS_ABORT)
1786 err = -EROFS;
1787 else
1788 journal->j_errno = 0;
1789 spin_unlock(&journal->j_state_lock);
1790 return err;
1791}
1792
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001793/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 * void journal_ack_err() - Ack journal err.
1795 * @journal: journal to act on.
1796 *
1797 * An error must be cleared or Acked to take a FS out of readonly
1798 * mode.
1799 */
1800void journal_ack_err(journal_t *journal)
1801{
1802 spin_lock(&journal->j_state_lock);
1803 if (journal->j_errno)
1804 journal->j_flags |= JFS_ACK_ERR;
1805 spin_unlock(&journal->j_state_lock);
1806}
1807
1808int journal_blocks_per_page(struct inode *inode)
1809{
1810 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1811}
1812
1813/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 * Journal_head storage management
1815 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001816static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817#ifdef CONFIG_JBD_DEBUG
1818static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1819#endif
1820
1821static int journal_init_journal_head_cache(void)
1822{
1823 int retval;
1824
Al Viro1076d172008-03-29 03:07:18 +00001825 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 journal_head_cache = kmem_cache_create("journal_head",
1827 sizeof(struct journal_head),
1828 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001829 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001830 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001832 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 retval = -ENOMEM;
1834 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1835 }
1836 return retval;
1837}
1838
1839static void journal_destroy_journal_head_cache(void)
1840{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001841 if (journal_head_cache) {
1842 kmem_cache_destroy(journal_head_cache);
1843 journal_head_cache = NULL;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847/*
1848 * journal_head splicing and dicing
1849 */
1850static struct journal_head *journal_alloc_journal_head(void)
1851{
1852 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854#ifdef CONFIG_JBD_DEBUG
1855 atomic_inc(&nr_journal_heads);
1856#endif
Zheng Liu8bb9da92013-04-29 17:08:51 +08001857 ret = kmem_cache_zalloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001858 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001860 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1861 __func__);
1862
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001863 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 yield();
Zheng Liu8bb9da92013-04-29 17:08:51 +08001865 ret = kmem_cache_zalloc(journal_head_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867 }
1868 return ret;
1869}
1870
1871static void journal_free_journal_head(struct journal_head *jh)
1872{
1873#ifdef CONFIG_JBD_DEBUG
1874 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001875 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876#endif
1877 kmem_cache_free(journal_head_cache, jh);
1878}
1879
1880/*
1881 * A journal_head is attached to a buffer_head whenever JBD has an
1882 * interest in the buffer.
1883 *
1884 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1885 * is set. This bit is tested in core kernel code where we need to take
1886 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1887 * there.
1888 *
1889 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1890 *
1891 * When a buffer has its BH_JBD bit set it is immune from being released by
1892 * core kernel code, mainly via ->b_count.
1893 *
Jan Karabb189242011-06-24 23:11:59 +02001894 * A journal_head is detached from its buffer_head when the journal_head's
1895 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1896 * transaction (b_cp_transaction) hold their references to b_jcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 * Various places in the kernel want to attach a journal_head to a buffer_head
1899 * _before_ attaching the journal_head to a transaction. To protect the
1900 * journal_head in this situation, journal_add_journal_head elevates the
1901 * journal_head's b_jcount refcount by one. The caller must call
1902 * journal_put_journal_head() to undo this.
1903 *
1904 * So the typical usage would be:
1905 *
1906 * (Attach a journal_head if needed. Increments b_jcount)
1907 * struct journal_head *jh = journal_add_journal_head(bh);
1908 * ...
Jan Karabb189242011-06-24 23:11:59 +02001909 * (Get another reference for transaction)
1910 * journal_grab_journal_head(bh);
1911 * jh->b_transaction = xxx;
1912 * (Put original reference)
1913 * journal_put_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 */
1915
1916/*
1917 * Give a buffer_head a journal_head.
1918 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * May sleep.
1920 */
1921struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1922{
1923 struct journal_head *jh;
1924 struct journal_head *new_jh = NULL;
1925
1926repeat:
Zheng Liu8bb9da92013-04-29 17:08:51 +08001927 if (!buffer_jbd(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 new_jh = journal_alloc_journal_head();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 jbd_lock_bh_journal_head(bh);
1931 if (buffer_jbd(bh)) {
1932 jh = bh2jh(bh);
1933 } else {
1934 J_ASSERT_BH(bh,
1935 (atomic_read(&bh->b_count) > 0) ||
1936 (bh->b_page && bh->b_page->mapping));
1937
1938 if (!new_jh) {
1939 jbd_unlock_bh_journal_head(bh);
1940 goto repeat;
1941 }
1942
1943 jh = new_jh;
1944 new_jh = NULL; /* We consumed it */
1945 set_buffer_jbd(bh);
1946 bh->b_private = jh;
1947 jh->b_bh = bh;
1948 get_bh(bh);
1949 BUFFER_TRACE(bh, "added journal_head");
1950 }
1951 jh->b_jcount++;
1952 jbd_unlock_bh_journal_head(bh);
1953 if (new_jh)
1954 journal_free_journal_head(new_jh);
1955 return bh->b_private;
1956}
1957
1958/*
1959 * Grab a ref against this buffer_head's journal_head. If it ended up not
1960 * having a journal_head, return NULL
1961 */
1962struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1963{
1964 struct journal_head *jh = NULL;
1965
1966 jbd_lock_bh_journal_head(bh);
1967 if (buffer_jbd(bh)) {
1968 jh = bh2jh(bh);
1969 jh->b_jcount++;
1970 }
1971 jbd_unlock_bh_journal_head(bh);
1972 return jh;
1973}
1974
1975static void __journal_remove_journal_head(struct buffer_head *bh)
1976{
1977 struct journal_head *jh = bh2jh(bh);
1978
1979 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karabb189242011-06-24 23:11:59 +02001980 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1981 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1982 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1983 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1984 J_ASSERT_BH(bh, buffer_jbd(bh));
1985 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1986 BUFFER_TRACE(bh, "remove journal_head");
1987 if (jh->b_frozen_data) {
1988 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1989 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Jan Karabb189242011-06-24 23:11:59 +02001991 if (jh->b_committed_data) {
1992 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1993 jbd_free(jh->b_committed_data, bh->b_size);
1994 }
1995 bh->b_private = NULL;
1996 jh->b_bh = NULL; /* debug, really */
1997 clear_buffer_jbd(bh);
1998 journal_free_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001/*
Jan Karabb189242011-06-24 23:11:59 +02002002 * Drop a reference on the passed journal_head. If it fell to zero then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 * release the journal_head from the buffer_head.
2004 */
2005void journal_put_journal_head(struct journal_head *jh)
2006{
2007 struct buffer_head *bh = jh2bh(jh);
2008
2009 jbd_lock_bh_journal_head(bh);
2010 J_ASSERT_JH(jh, jh->b_jcount > 0);
2011 --jh->b_jcount;
Jan Karabb189242011-06-24 23:11:59 +02002012 if (!jh->b_jcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 __journal_remove_journal_head(bh);
Jan Karabb189242011-06-24 23:11:59 +02002014 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 __brelse(bh);
Jan Karabb189242011-06-24 23:11:59 +02002016 } else
2017 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
2020/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07002021 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07002023#ifdef CONFIG_JBD_DEBUG
2024
2025u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Jose R. Santosc2a91592007-10-18 23:39:22 -07002028static struct dentry *jbd_debugfs_dir;
2029static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Jose R. Santosc2a91592007-10-18 23:39:22 -07002031static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Jose R. Santosc2a91592007-10-18 23:39:22 -07002033 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2034 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08002035 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07002036 jbd_debugfs_dir,
2037 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Jose R. Santosc2a91592007-10-18 23:39:22 -07002040static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Jose R. Santosc2a91592007-10-18 23:39:22 -07002042 debugfs_remove(jbd_debug);
2043 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046#else
2047
Jose R. Santosc2a91592007-10-18 23:39:22 -07002048static inline void jbd_create_debugfs_entry(void)
2049{
2050}
2051
2052static inline void jbd_remove_debugfs_entry(void)
2053{
2054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056#endif
2057
Christoph Lametere18b8902006-12-06 20:33:20 -08002058struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060static int __init journal_init_handle_cache(void)
2061{
2062 jbd_handle_cache = kmem_cache_create("journal_handle",
2063 sizeof(handle_t),
2064 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07002065 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002066 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (jbd_handle_cache == NULL) {
2068 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2069 return -ENOMEM;
2070 }
2071 return 0;
2072}
2073
2074static void journal_destroy_handle_cache(void)
2075{
2076 if (jbd_handle_cache)
2077 kmem_cache_destroy(jbd_handle_cache);
2078}
2079
2080/*
2081 * Module startup and shutdown
2082 */
2083
2084static int __init journal_init_caches(void)
2085{
2086 int ret;
2087
2088 ret = journal_init_revoke_caches();
2089 if (ret == 0)
2090 ret = journal_init_journal_head_cache();
2091 if (ret == 0)
2092 ret = journal_init_handle_cache();
2093 return ret;
2094}
2095
2096static void journal_destroy_caches(void)
2097{
2098 journal_destroy_revoke_caches();
2099 journal_destroy_journal_head_cache();
2100 journal_destroy_handle_cache();
2101}
2102
2103static int __init journal_init(void)
2104{
2105 int ret;
2106
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002107 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 ret = journal_init_caches();
2110 if (ret != 0)
2111 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002112 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return ret;
2114}
2115
2116static void __exit journal_exit(void)
2117{
2118#ifdef CONFIG_JBD_DEBUG
2119 int n = atomic_read(&nr_journal_heads);
2120 if (n)
2121 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2122#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002123 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 journal_destroy_caches();
2125}
2126
2127MODULE_LICENSE("GPL");
2128module_init(journal_init);
2129module_exit(journal_exit);
2130