blob: 44c104abfb362842c71b75717f16fd5e5b9011ff [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 */
Ted Ts'od9b01932011-04-30 13:17:11 -0400449 if (journal->j_running_transaction &&
450 journal->j_running_transaction->t_tid == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200452 * We want a new commit: OK, mark the request and wakeup the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * commit thread. We do _not_ do the commit ourselves.
454 */
455
456 journal->j_commit_request = target;
457 jbd_debug(1, "JBD: requesting commit %d/%d\n",
458 journal->j_commit_request,
459 journal->j_commit_sequence);
460 wake_up(&journal->j_wait_commit);
461 return 1;
Ted Ts'od9b01932011-04-30 13:17:11 -0400462 } else if (!tid_geq(journal->j_commit_request, target))
463 /* This should never happen, but if it does, preserve
464 the evidence before kjournald goes into a loop and
465 increments j_commit_sequence beyond all recognition. */
466 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
467 journal->j_commit_request, journal->j_commit_sequence,
468 target, journal->j_running_transaction ?
469 journal->j_running_transaction->t_tid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471}
472
473int log_start_commit(journal_t *journal, tid_t tid)
474{
475 int ret;
476
477 spin_lock(&journal->j_state_lock);
478 ret = __log_start_commit(journal, tid);
479 spin_unlock(&journal->j_state_lock);
480 return ret;
481}
482
483/*
484 * Force and wait upon a commit if the calling process is not within
485 * transaction. This is used for forcing out undo-protected data which contains
486 * bitmaps, when the fs is running out of space.
487 *
488 * We can only force the running transaction if we don't have an active handle;
489 * otherwise, we will deadlock.
490 *
491 * Returns true if a transaction was started.
492 */
493int journal_force_commit_nested(journal_t *journal)
494{
495 transaction_t *transaction = NULL;
496 tid_t tid;
497
498 spin_lock(&journal->j_state_lock);
499 if (journal->j_running_transaction && !current->journal_info) {
500 transaction = journal->j_running_transaction;
501 __log_start_commit(journal, transaction->t_tid);
502 } else if (journal->j_committing_transaction)
503 transaction = journal->j_committing_transaction;
504
505 if (!transaction) {
506 spin_unlock(&journal->j_state_lock);
507 return 0; /* Nothing to retry */
508 }
509
510 tid = transaction->t_tid;
511 spin_unlock(&journal->j_state_lock);
512 log_wait_commit(journal, tid);
513 return 1;
514}
515
516/*
517 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800518 * if a transaction is going to be committed (or is currently already
519 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521int journal_start_commit(journal_t *journal, tid_t *ptid)
522{
523 int ret = 0;
524
525 spin_lock(&journal->j_state_lock);
526 if (journal->j_running_transaction) {
527 tid_t tid = journal->j_running_transaction->t_tid;
528
Jan Kara8fe4cd02009-02-11 13:04:25 -0800529 __log_start_commit(journal, tid);
530 /* There's a running transaction and we've just made sure
531 * it's commit has been scheduled. */
532 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800534 ret = 1;
535 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * If ext3_write_super() recently started a commit, then we
538 * have to wait for completion of that transaction
539 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800540 if (ptid)
541 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ret = 1;
543 }
544 spin_unlock(&journal->j_state_lock);
545 return ret;
546}
547
548/*
549 * Wait for a specified commit to complete.
550 * The caller may not hold the journal lock.
551 */
552int log_wait_commit(journal_t *journal, tid_t tid)
553{
554 int err = 0;
555
556#ifdef CONFIG_JBD_DEBUG
557 spin_lock(&journal->j_state_lock);
558 if (!tid_geq(journal->j_commit_request, tid)) {
559 printk(KERN_EMERG
560 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700561 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 spin_unlock(&journal->j_state_lock);
564#endif
565 spin_lock(&journal->j_state_lock);
Jan Kara2db938b2011-02-21 17:25:37 +0100566 if (!tid_geq(journal->j_commit_waited, tid))
567 journal->j_commit_waited = tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 while (tid_gt(tid, journal->j_commit_sequence)) {
569 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
570 tid, journal->j_commit_sequence);
571 wake_up(&journal->j_wait_commit);
572 spin_unlock(&journal->j_state_lock);
573 wait_event(journal->j_wait_done_commit,
574 !tid_gt(tid, journal->j_commit_sequence));
575 spin_lock(&journal->j_state_lock);
576 }
577 spin_unlock(&journal->j_state_lock);
578
579 if (unlikely(is_journal_aborted(journal))) {
580 printk(KERN_EMERG "journal commit I/O error\n");
581 err = -EIO;
582 }
583 return err;
584}
585
586/*
Jan Kara03f4d802010-04-15 22:16:24 +0200587 * Return 1 if a given transaction has not yet sent barrier request
588 * connected with a transaction commit. If 0 is returned, transaction
589 * may or may not have sent the barrier. Used to avoid sending barrier
590 * twice in common cases.
591 */
592int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
593{
594 int ret = 0;
595 transaction_t *commit_trans;
596
597 if (!(journal->j_flags & JFS_BARRIER))
598 return 0;
599 spin_lock(&journal->j_state_lock);
600 /* Transaction already committed? */
601 if (tid_geq(journal->j_commit_sequence, tid))
602 goto out;
603 /*
604 * Transaction is being committed and we already proceeded to
605 * writing commit record?
606 */
607 commit_trans = journal->j_committing_transaction;
608 if (commit_trans && commit_trans->t_tid == tid &&
609 commit_trans->t_state >= T_COMMIT_RECORD)
610 goto out;
611 ret = 1;
612out:
613 spin_unlock(&journal->j_state_lock);
614 return ret;
615}
Jan Kara52779702010-04-15 22:24:26 +0200616EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200617
618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Log buffer allocation routines:
620 */
621
Jan Kara9c28cbc2009-08-03 19:21:00 +0200622int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200624 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 spin_lock(&journal->j_state_lock);
627 J_ASSERT(journal->j_free > 1);
628
629 blocknr = journal->j_head;
630 journal->j_head++;
631 journal->j_free--;
632 if (journal->j_head == journal->j_last)
633 journal->j_head = journal->j_first;
634 spin_unlock(&journal->j_state_lock);
635 return journal_bmap(journal, blocknr, retp);
636}
637
638/*
639 * Conversion of logical to physical block numbers for the journal
640 *
641 * On external journals the journal blocks are identity-mapped, so
642 * this is a no-op. If needed, we can use j_blk_offset - everything is
643 * ready.
644 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200645int journal_bmap(journal_t *journal, unsigned int blocknr,
646 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200649 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (journal->j_inode) {
652 ret = bmap(journal->j_inode, blocknr);
653 if (ret)
654 *retp = ret;
655 else {
656 char b[BDEVNAME_SIZE];
657
658 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200659 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700660 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 blocknr,
662 bdevname(journal->j_dev, b));
663 err = -EIO;
664 __journal_abort_soft(journal, err);
665 }
666 } else {
667 *retp = blocknr; /* +journal->j_blk_offset */
668 }
669 return err;
670}
671
672/*
673 * We play buffer_head aliasing tricks to write data/metadata blocks to
674 * the journal without copying their contents, but for journal
675 * descriptor blocks we do need to generate bona fide buffers.
676 *
677 * After the caller of journal_get_descriptor_buffer() has finished modifying
678 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
679 * But we don't bother doing that, so there will be coherency problems with
680 * mmaps of blockdevs which hold live JBD-controlled filesystems.
681 */
682struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
683{
684 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200685 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int err;
687
688 err = journal_next_log_block(journal, &blocknr);
689
690 if (err)
691 return NULL;
692
693 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700694 if (!bh)
695 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 lock_buffer(bh);
697 memset(bh->b_data, 0, journal->j_blocksize);
698 set_buffer_uptodate(bh);
699 unlock_buffer(bh);
700 BUFFER_TRACE(bh, "return this buffer");
701 return journal_add_journal_head(bh);
702}
703
704/*
705 * Management for journal control blocks: functions to create and
706 * destroy journal_t structures, and to initialise and read existing
707 * journal blocks from disk. */
708
709/* First: create and setup a journal_t object in memory. We initialise
710 * very few fields yet: that has to wait until we have created the
711 * journal structures from from scratch, or loaded them from disk. */
712
713static journal_t * journal_init_common (void)
714{
715 journal_t *journal;
716 int err;
717
Mingming Cao8c3478a2007-10-18 23:39:20 -0700718 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!journal)
720 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 init_waitqueue_head(&journal->j_wait_transaction_locked);
723 init_waitqueue_head(&journal->j_wait_logspace);
724 init_waitqueue_head(&journal->j_wait_done_commit);
725 init_waitqueue_head(&journal->j_wait_checkpoint);
726 init_waitqueue_head(&journal->j_wait_commit);
727 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800728 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_lock_init(&journal->j_revoke_lock);
730 spin_lock_init(&journal->j_list_lock);
731 spin_lock_init(&journal->j_state_lock);
732
733 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
734
735 /* The journal is marked for error until we succeed with recovery! */
736 journal->j_flags = JFS_ABORT;
737
738 /* Set up a default-sized revoke table for the new mount. */
739 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
740 if (err) {
741 kfree(journal);
742 goto fail;
743 }
744 return journal;
745fail:
746 return NULL;
747}
748
749/* journal_init_dev and journal_init_inode:
750 *
751 * Create a journal structure assigned some fixed set of disk blocks to
752 * the journal. We don't actually touch those disk blocks yet, but we
753 * need to set up all of the mapping information to tell the journaling
754 * system where the journal blocks are.
755 *
756 */
757
758/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700759 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * @bdev: Block device on which to create the journal
761 * @fs_dev: Device which hold journalled filesystem for this journal.
762 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700763 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700765 *
766 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * journal_init_dev creates a journal which maps a fixed contiguous
769 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700770 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
772journal_t * journal_init_dev(struct block_device *bdev,
773 struct block_device *fs_dev,
774 int start, int len, int blocksize)
775{
776 journal_t *journal = journal_init_common();
777 struct buffer_head *bh;
778 int n;
779
780 if (!journal)
781 return NULL;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700784 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 n = journal->j_blocksize / sizeof(journal_block_tag_t);
786 journal->j_wbufsize = n;
787 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
788 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300789 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700790 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700791 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700793 journal->j_dev = bdev;
794 journal->j_fs_dev = fs_dev;
795 journal->j_blk_offset = start;
796 journal->j_maxlen = len;
797
798 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700799 if (!bh) {
800 printk(KERN_ERR
801 "%s: Cannot get buffer for journal superblock\n",
802 __func__);
803 goto out_err;
804 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700805 journal->j_sb_buffer = bh;
806 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700809out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800810 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700811 kfree(journal);
812 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700814
815/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
817 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700818 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * journal_init_inode creates a journal which maps an on-disk inode as
820 * the journal. The inode must exist already, must support bmap() and
821 * must have all data blocks preallocated.
822 */
823journal_t * journal_init_inode (struct inode *inode)
824{
825 struct buffer_head *bh;
826 journal_t *journal = journal_init_common();
827 int err;
828 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200829 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (!journal)
832 return NULL;
833
834 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
835 journal->j_inode = inode;
836 jbd_debug(1,
837 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700838 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 (long long) inode->i_size,
840 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
841
842 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
843 journal->j_blocksize = inode->i_sb->s_blocksize;
844
845 /* journal descriptor can store up to n blocks -bzzz */
846 n = journal->j_blocksize / sizeof(journal_block_tag_t);
847 journal->j_wbufsize = n;
848 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
849 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300850 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700851 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700852 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 err = journal_bmap(journal, 0, &blocknr);
856 /* If that failed, give up */
857 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -0800858 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700859 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700860 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700864 if (!bh) {
865 printk(KERN_ERR
866 "%s: Cannot get buffer for journal superblock\n",
867 __func__);
868 goto out_err;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 journal->j_sb_buffer = bh;
871 journal->j_superblock = (journal_superblock_t *)bh->b_data;
872
873 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700874out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800875 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700876 kfree(journal);
877 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * If the journal init or create aborts, we need to mark the journal
882 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700883 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
885static void journal_fail_superblock (journal_t *journal)
886{
887 struct buffer_head *bh = journal->j_sb_buffer;
888 brelse(bh);
889 journal->j_sb_buffer = NULL;
890}
891
892/*
893 * Given a journal_t structure, initialise the various fields for
894 * startup of a new journaling session. We use this both when creating
895 * a journal, and after recovering an old journal to reset it for
896 * subsequent use.
897 */
898
899static int journal_reset(journal_t *journal)
900{
901 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200902 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 first = be32_to_cpu(sb->s_first);
905 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200906 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200907 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200908 first, last);
909 journal_fail_superblock(journal);
910 return -EINVAL;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 journal->j_first = first;
914 journal->j_last = last;
915
916 journal->j_head = first;
917 journal->j_tail = first;
918 journal->j_free = last - first;
919
920 journal->j_tail_sequence = journal->j_transaction_sequence;
921 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
922 journal->j_commit_request = journal->j_commit_sequence;
923
924 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
925
Jan Kara9754e392012-04-07 12:33:03 +0200926 /*
927 * As a special case, if the on-disk copy is already marked as needing
928 * no recovery (s_start == 0), then we can safely defer the superblock
929 * update until the next commit by setting JFS_FLUSHED. This avoids
930 * attempting a write to a potential-readonly device.
931 */
932 if (sb->s_start == 0) {
933 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
934 "(start %u, seq %d, errno %d)\n",
935 journal->j_tail, journal->j_tail_sequence,
936 journal->j_errno);
937 journal->j_flags |= JFS_FLUSHED;
938 } else {
939 /* Add the dynamic fields and write it to disk. */
940 journal_update_sb_log_tail(journal);
941 }
Pavel Emelianov97f06782007-05-08 00:30:42 -0700942 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700945/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * int journal_create() - Initialise the new journal file
947 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700948 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Given a journal_t structure which tells us which disk blocks we can
950 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700951 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 **/
953int journal_create(journal_t *journal)
954{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200955 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct buffer_head *bh;
957 journal_superblock_t *sb;
958 int i, err;
959
960 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
961 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
962 journal->j_maxlen);
963 journal_fail_superblock(journal);
964 return -EINVAL;
965 }
966
967 if (journal->j_inode == NULL) {
968 /*
969 * We don't know what block to start at!
970 */
971 printk(KERN_EMERG
972 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700973 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 BUG();
975 }
976
977 /* Zero out the entire journal on disk. We cannot afford to
978 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
979 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
980 for (i = 0; i < journal->j_maxlen; i++) {
981 err = journal_bmap(journal, i, &blocknr);
982 if (err)
983 return err;
984 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Namhyung Kim2a0e3382010-10-13 17:17:18 +0900985 if (unlikely(!bh))
986 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 lock_buffer(bh);
988 memset (bh->b_data, 0, journal->j_blocksize);
989 BUFFER_TRACE(bh, "marking dirty");
990 mark_buffer_dirty(bh);
991 BUFFER_TRACE(bh, "marking uptodate");
992 set_buffer_uptodate(bh);
993 unlock_buffer(bh);
994 __brelse(bh);
995 }
996
997 sync_blockdev(journal->j_dev);
998 jbd_debug(1, "JBD: journal cleared.\n");
999
1000 /* OK, fill in the initial static fields in the new superblock */
1001 sb = journal->j_superblock;
1002
1003 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1004 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1005
1006 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1007 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1008 sb->s_first = cpu_to_be32(1);
1009
1010 journal->j_transaction_sequence = 1;
1011
1012 journal->j_flags &= ~JFS_ABORT;
1013 journal->j_format_version = 2;
1014
1015 return journal_reset(journal);
1016}
1017
Jan Kara9754e392012-04-07 12:33:03 +02001018static void journal_write_superblock(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct buffer_head *bh = journal->j_sb_buffer;
1021
Jan Kara9754e392012-04-07 12:33:03 +02001022 trace_journal_write_superblock(journal);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001023 if (buffer_write_io_error(bh)) {
1024 char b[BDEVNAME_SIZE];
1025 /*
1026 * Oh, dear. A previous attempt to write the journal
1027 * superblock failed. This could happen because the
1028 * USB device was yanked out. Or it could happen to
1029 * be a transient write error and maybe the block will
1030 * be remapped. Nothing we can do but to retry the
1031 * write and hope for the best.
1032 */
1033 printk(KERN_ERR "JBD: previous I/O error detected "
1034 "for journal superblock update for %s.\n",
1035 journal_dev_name(journal, b));
1036 clear_buffer_write_io_error(bh);
1037 set_buffer_uptodate(bh);
1038 }
1039
Jan Kara9754e392012-04-07 12:33:03 +02001040 BUFFER_TRACE(bh, "marking dirty");
1041 mark_buffer_dirty(bh);
1042 sync_dirty_buffer(bh);
1043 if (buffer_write_io_error(bh)) {
1044 char b[BDEVNAME_SIZE];
1045 printk(KERN_ERR "JBD: I/O error detected "
1046 "when updating journal superblock for %s.\n",
1047 journal_dev_name(journal, b));
1048 clear_buffer_write_io_error(bh);
1049 set_buffer_uptodate(bh);
1050 }
1051}
1052
1053/**
1054 * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1055 * @journal: The journal to update.
1056 *
1057 * Update a journal's superblock information about log tail and write it to
1058 * disk, waiting for the IO to complete.
1059 */
1060void journal_update_sb_log_tail(journal_t *journal)
1061{
1062 journal_superblock_t *sb = journal->j_superblock;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_lock(&journal->j_state_lock);
Jan Kara9c28cbc2009-08-03 19:21:00 +02001065 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1067
1068 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1069 sb->s_start = cpu_to_be32(journal->j_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_unlock(&journal->j_state_lock);
1071
Jan Kara9754e392012-04-07 12:33:03 +02001072 journal_write_superblock(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jan Kara9754e392012-04-07 12:33:03 +02001074 /* Log is no longer empty */
1075 spin_lock(&journal->j_state_lock);
1076 WARN_ON(!sb->s_sequence);
1077 journal->j_flags &= ~JFS_FLUSHED;
1078 spin_unlock(&journal->j_state_lock);
1079}
1080
1081/**
1082 * mark_journal_empty() - Mark on disk journal as empty.
1083 * @journal: The journal to update.
1084 *
1085 * Update a journal's dynamic superblock fields to show that journal is empty.
1086 * Write updated superblock to disk waiting for IO to complete.
1087 */
1088static void mark_journal_empty(journal_t *journal)
1089{
1090 journal_superblock_t *sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 spin_lock(&journal->j_state_lock);
Jan Kara9754e392012-04-07 12:33:03 +02001093 jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1094 journal->j_tail_sequence);
1095
1096 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1097 sb->s_start = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 spin_unlock(&journal->j_state_lock);
Jan Kara9754e392012-04-07 12:33:03 +02001099
1100 journal_write_superblock(journal);
1101
1102 spin_lock(&journal->j_state_lock);
1103 /* Log is empty */
1104 journal->j_flags |= JFS_FLUSHED;
1105 spin_unlock(&journal->j_state_lock);
1106}
1107
1108/**
1109 * journal_update_sb_errno() - Update error in the journal.
1110 * @journal: The journal to update.
1111 *
1112 * Update a journal's errno. Write updated superblock to disk waiting for IO
1113 * to complete.
1114 */
1115static void journal_update_sb_errno(journal_t *journal)
1116{
1117 journal_superblock_t *sb = journal->j_superblock;
1118
1119 spin_lock(&journal->j_state_lock);
1120 jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1121 journal->j_errno);
1122 sb->s_errno = cpu_to_be32(journal->j_errno);
1123 spin_unlock(&journal->j_state_lock);
1124
1125 journal_write_superblock(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/*
1129 * Read the superblock for a given journal, performing initial
1130 * validation of the format.
1131 */
1132
1133static int journal_get_superblock(journal_t *journal)
1134{
1135 struct buffer_head *bh;
1136 journal_superblock_t *sb;
1137 int err = -EIO;
1138
1139 bh = journal->j_sb_buffer;
1140
1141 J_ASSERT(bh != NULL);
1142 if (!buffer_uptodate(bh)) {
1143 ll_rw_block(READ, 1, &bh);
1144 wait_on_buffer(bh);
1145 if (!buffer_uptodate(bh)) {
1146 printk (KERN_ERR
1147 "JBD: IO error reading journal superblock\n");
1148 goto out;
1149 }
1150 }
1151
1152 sb = journal->j_superblock;
1153
1154 err = -EINVAL;
1155
1156 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1157 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1158 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1159 goto out;
1160 }
1161
1162 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1163 case JFS_SUPERBLOCK_V1:
1164 journal->j_format_version = 1;
1165 break;
1166 case JFS_SUPERBLOCK_V2:
1167 journal->j_format_version = 2;
1168 break;
1169 default:
1170 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1171 goto out;
1172 }
1173
1174 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1175 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1176 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1177 printk (KERN_WARNING "JBD: journal file too short\n");
1178 goto out;
1179 }
1180
Eryu Guan87622022011-11-01 19:04:59 -04001181 if (be32_to_cpu(sb->s_first) == 0 ||
1182 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1183 printk(KERN_WARNING
1184 "JBD: Invalid start block of journal: %u\n",
1185 be32_to_cpu(sb->s_first));
1186 goto out;
1187 }
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190
1191out:
1192 journal_fail_superblock(journal);
1193 return err;
1194}
1195
1196/*
1197 * Load the on-disk journal superblock and read the key fields into the
1198 * journal_t.
1199 */
1200
1201static int load_superblock(journal_t *journal)
1202{
1203 int err;
1204 journal_superblock_t *sb;
1205
1206 err = journal_get_superblock(journal);
1207 if (err)
1208 return err;
1209
1210 sb = journal->j_superblock;
1211
1212 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1213 journal->j_tail = be32_to_cpu(sb->s_start);
1214 journal->j_first = be32_to_cpu(sb->s_first);
1215 journal->j_last = be32_to_cpu(sb->s_maxlen);
1216 journal->j_errno = be32_to_cpu(sb->s_errno);
1217
1218 return 0;
1219}
1220
1221
1222/**
1223 * int journal_load() - Read journal from disk.
1224 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001225 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * Given a journal_t structure which tells us which disk blocks contain
1227 * a journal, read the journal from disk to initialise the in-memory
1228 * structures.
1229 */
1230int journal_load(journal_t *journal)
1231{
1232 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001233 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 err = load_superblock(journal);
1236 if (err)
1237 return err;
1238
Badari Pulavartyea817392006-08-27 01:23:52 -07001239 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* If this is a V2 superblock, then we have to check the
1241 * features flags on it. */
1242
1243 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if ((sb->s_feature_ro_compat &
1245 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1246 (sb->s_feature_incompat &
1247 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1248 printk (KERN_WARNING
1249 "JBD: Unrecognised features on journal\n");
1250 return -EINVAL;
1251 }
1252 }
1253
1254 /* Let the recovery code check whether it needs to recover any
1255 * data from the journal. */
1256 if (journal_recover(journal))
1257 goto recovery_error;
1258
1259 /* OK, we've finished with the dynamic journal bits:
1260 * reinitialise the dynamic contents of the superblock in memory
1261 * and reset them on disk. */
1262 if (journal_reset(journal))
1263 goto recovery_error;
1264
1265 journal->j_flags &= ~JFS_ABORT;
1266 journal->j_flags |= JFS_LOADED;
1267 return 0;
1268
1269recovery_error:
1270 printk (KERN_WARNING "JBD: recovery failed\n");
1271 return -EIO;
1272}
1273
1274/**
1275 * void journal_destroy() - Release a journal_t structure.
1276 * @journal: Journal to act on.
1277 *
1278 * Release a journal_t structure once it is no longer in use by the
1279 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001280 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001282int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001284 int err = 0;
1285
Jan Kara03f4d802010-04-15 22:16:24 +02001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* Wait for the commit thread to wake up and die. */
1288 journal_kill_thread(journal);
1289
1290 /* Force a final log commit */
1291 if (journal->j_running_transaction)
1292 journal_commit_transaction(journal);
1293
1294 /* Force any old transactions to disk */
1295
1296 /* Totally anal locking here... */
1297 spin_lock(&journal->j_list_lock);
1298 while (journal->j_checkpoint_transactions != NULL) {
1299 spin_unlock(&journal->j_list_lock);
1300 log_do_checkpoint(journal);
1301 spin_lock(&journal->j_list_lock);
1302 }
1303
1304 J_ASSERT(journal->j_running_transaction == NULL);
1305 J_ASSERT(journal->j_committing_transaction == NULL);
1306 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1307 spin_unlock(&journal->j_list_lock);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001310 if (!is_journal_aborted(journal)) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001311 journal->j_tail_sequence =
1312 ++journal->j_transaction_sequence;
Jan Kara9754e392012-04-07 12:33:03 +02001313 mark_journal_empty(journal);
1314 } else
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001315 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 brelse(journal->j_sb_buffer);
1317 }
1318
1319 if (journal->j_inode)
1320 iput(journal->j_inode);
1321 if (journal->j_revoke)
1322 journal_destroy_revoke(journal);
1323 kfree(journal->j_wbuf);
1324 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001325
1326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329
1330/**
1331 *int journal_check_used_features () - Check if features specified are used.
1332 * @journal: Journal to check.
1333 * @compat: bitmask of compatible features
1334 * @ro: bitmask of features that force read-only mount
1335 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001338 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 **/
1340
1341int journal_check_used_features (journal_t *journal, unsigned long compat,
1342 unsigned long ro, unsigned long incompat)
1343{
1344 journal_superblock_t *sb;
1345
1346 if (!compat && !ro && !incompat)
1347 return 1;
1348 if (journal->j_format_version == 1)
1349 return 0;
1350
1351 sb = journal->j_superblock;
1352
1353 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1354 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1355 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1356 return 1;
1357
1358 return 0;
1359}
1360
1361/**
1362 * int journal_check_available_features() - Check feature set in journalling layer
1363 * @journal: Journal to check.
1364 * @compat: bitmask of compatible features
1365 * @ro: bitmask of features that force read-only mount
1366 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001367 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * Check whether the journaling code supports the use of
1369 * all of a given set of features on this journal. Return true
1370 * (non-zero) if it can. */
1371
1372int journal_check_available_features (journal_t *journal, unsigned long compat,
1373 unsigned long ro, unsigned long incompat)
1374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!compat && !ro && !incompat)
1376 return 1;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* We can support any known requested features iff the
1379 * superblock is in version 2. Otherwise we fail to support any
1380 * extended sb features. */
1381
1382 if (journal->j_format_version != 2)
1383 return 0;
1384
1385 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1386 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1387 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1388 return 1;
1389
1390 return 0;
1391}
1392
1393/**
1394 * int journal_set_features () - Mark a given journal feature in the superblock
1395 * @journal: Journal to act on.
1396 * @compat: bitmask of compatible features
1397 * @ro: bitmask of features that force read-only mount
1398 * @incompat: bitmask of incompatible features
1399 *
1400 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001401 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
1403 */
1404
1405int journal_set_features (journal_t *journal, unsigned long compat,
1406 unsigned long ro, unsigned long incompat)
1407{
1408 journal_superblock_t *sb;
1409
1410 if (journal_check_used_features(journal, compat, ro, incompat))
1411 return 1;
1412
1413 if (!journal_check_available_features(journal, compat, ro, incompat))
1414 return 0;
1415
1416 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1417 compat, ro, incompat);
1418
1419 sb = journal->j_superblock;
1420
1421 sb->s_feature_compat |= cpu_to_be32(compat);
1422 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1423 sb->s_feature_incompat |= cpu_to_be32(incompat);
1424
1425 return 1;
1426}
1427
1428
1429/**
1430 * int journal_update_format () - Update on-disk journal structure.
1431 * @journal: Journal to act on.
1432 *
1433 * Given an initialised but unloaded journal struct, poke about in the
1434 * on-disk structure to update it to the most recent supported version.
1435 */
1436int journal_update_format (journal_t *journal)
1437{
1438 journal_superblock_t *sb;
1439 int err;
1440
1441 err = journal_get_superblock(journal);
1442 if (err)
1443 return err;
1444
1445 sb = journal->j_superblock;
1446
1447 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1448 case JFS_SUPERBLOCK_V2:
1449 return 0;
1450 case JFS_SUPERBLOCK_V1:
1451 return journal_convert_superblock_v1(journal, sb);
1452 default:
1453 break;
1454 }
1455 return -EINVAL;
1456}
1457
1458static int journal_convert_superblock_v1(journal_t *journal,
1459 journal_superblock_t *sb)
1460{
1461 int offset, blocksize;
1462 struct buffer_head *bh;
1463
1464 printk(KERN_WARNING
1465 "JBD: Converting superblock from version 1 to 2.\n");
1466
1467 /* Pre-initialise new fields to zero */
1468 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1469 blocksize = be32_to_cpu(sb->s_blocksize);
1470 memset(&sb->s_feature_compat, 0, blocksize-offset);
1471
1472 sb->s_nr_users = cpu_to_be32(1);
1473 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1474 journal->j_format_version = 2;
1475
1476 bh = journal->j_sb_buffer;
1477 BUFFER_TRACE(bh, "marking dirty");
1478 mark_buffer_dirty(bh);
1479 sync_dirty_buffer(bh);
1480 return 0;
1481}
1482
1483
1484/**
1485 * int journal_flush () - Flush journal
1486 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * Flush all data for a given journal to disk and empty the journal.
1489 * Filesystems can use this when remounting readonly to ensure that
1490 * recovery does not need to happen on remount.
1491 */
1492
1493int journal_flush(journal_t *journal)
1494{
1495 int err = 0;
1496 transaction_t *transaction = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 spin_lock(&journal->j_state_lock);
1499
1500 /* Force everything buffered to the log... */
1501 if (journal->j_running_transaction) {
1502 transaction = journal->j_running_transaction;
1503 __log_start_commit(journal, transaction->t_tid);
1504 } else if (journal->j_committing_transaction)
1505 transaction = journal->j_committing_transaction;
1506
1507 /* Wait for the log commit to complete... */
1508 if (transaction) {
1509 tid_t tid = transaction->t_tid;
1510
1511 spin_unlock(&journal->j_state_lock);
1512 log_wait_commit(journal, tid);
1513 } else {
1514 spin_unlock(&journal->j_state_lock);
1515 }
1516
1517 /* ...and flush everything in the log out to disk. */
1518 spin_lock(&journal->j_list_lock);
1519 while (!err && journal->j_checkpoint_transactions != NULL) {
1520 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001521 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001523 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 spin_lock(&journal->j_list_lock);
1525 }
1526 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001527
1528 if (is_journal_aborted(journal))
1529 return -EIO;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 cleanup_journal_tail(journal);
1532
1533 /* Finally, mark the journal as really needing no recovery.
1534 * This sets s_start==0 in the underlying superblock, which is
1535 * the magic code for a fully-recovered superblock. Any future
1536 * commits of data to the journal will restore the current
1537 * s_start value. */
Jan Kara9754e392012-04-07 12:33:03 +02001538 mark_journal_empty(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_lock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 J_ASSERT(!journal->j_running_transaction);
1541 J_ASSERT(!journal->j_committing_transaction);
1542 J_ASSERT(!journal->j_checkpoint_transactions);
1543 J_ASSERT(journal->j_head == journal->j_tail);
1544 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1545 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549/**
1550 * int journal_wipe() - Wipe journal contents
1551 * @journal: Journal to act on.
1552 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * Wipe out all of the contents of a journal, safely. This will produce
1555 * a warning if the journal contains any valid recovery information.
1556 * Must be called between journal_init_*() and journal_load().
1557 *
1558 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1559 * we merely suppress recovery.
1560 */
1561
1562int journal_wipe(journal_t *journal, int write)
1563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int err = 0;
1565
1566 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1567
1568 err = load_superblock(journal);
1569 if (err)
1570 return err;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (!journal->j_tail)
1573 goto no_recovery;
1574
1575 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1576 write ? "Clearing" : "Ignoring");
1577
1578 err = journal_skip_recovery(journal);
1579 if (write)
Jan Kara9754e392012-04-07 12:33:03 +02001580 mark_journal_empty(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 no_recovery:
1583 return err;
1584}
1585
1586/*
1587 * journal_dev_name: format a character string to describe on what
1588 * device this journal is present.
1589 */
1590
Adrian Bunk022a4a72005-09-06 15:16:41 -07001591static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 struct block_device *bdev;
1594
1595 if (journal->j_inode)
1596 bdev = journal->j_inode->i_sb->s_bdev;
1597 else
1598 bdev = journal->j_dev;
1599
1600 return bdevname(bdev, buffer);
1601}
1602
1603/*
1604 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001605 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *
1607 * Two internal function, which provide abort to te jbd layer
1608 * itself are here.
1609 */
1610
1611/*
1612 * Quick version for internal journal use (doesn't lock the journal).
1613 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1614 * and don't attempt to make any other journal updates.
1615 */
Adrian Bunk53308382008-02-06 01:40:12 -08001616static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 transaction_t *transaction;
1619 char b[BDEVNAME_SIZE];
1620
1621 if (journal->j_flags & JFS_ABORT)
1622 return;
1623
1624 printk(KERN_ERR "Aborting journal on device %s.\n",
1625 journal_dev_name(journal, b));
1626
1627 spin_lock(&journal->j_state_lock);
1628 journal->j_flags |= JFS_ABORT;
1629 transaction = journal->j_running_transaction;
1630 if (transaction)
1631 __log_start_commit(journal, transaction->t_tid);
1632 spin_unlock(&journal->j_state_lock);
1633}
1634
1635/* Soft abort: record the abort error status in the journal superblock,
1636 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001637static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 if (journal->j_flags & JFS_ABORT)
1640 return;
1641
1642 if (!journal->j_errno)
1643 journal->j_errno = errno;
1644
1645 __journal_abort_hard(journal);
1646
1647 if (errno)
Jan Kara9754e392012-04-07 12:33:03 +02001648 journal_update_sb_errno(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/**
1652 * void journal_abort () - Shutdown the journal immediately.
1653 * @journal: the journal to shutdown.
1654 * @errno: an error number to record in the journal indicating
1655 * the reason for the shutdown.
1656 *
1657 * Perform a complete, immediate shutdown of the ENTIRE
1658 * journal (not of a single transaction). This operation cannot be
1659 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * The journal_abort function is intended to support higher level error
1662 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1663 * mode.
1664 *
1665 * Journal abort has very specific semantics. Any existing dirty,
1666 * unjournaled buffers in the main filesystem will still be written to
1667 * disk by bdflush, but the journaling mechanism will be suspended
1668 * immediately and no further transaction commits will be honoured.
1669 *
1670 * Any dirty, journaled buffers will be written back to disk without
1671 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1672 * filesystem, but we _do_ attempt to leave as much data as possible
1673 * behind for fsck to use for cleanup.
1674 *
1675 * Any attempt to get a new transaction handle on a journal which is in
1676 * ABORT state will just result in an -EROFS error return. A
1677 * journal_stop on an existing handle will return -EIO if we have
1678 * entered abort state during the update.
1679 *
1680 * Recursive transactions are not disturbed by journal abort until the
1681 * final journal_stop, which will receive the -EIO error.
1682 *
1683 * Finally, the journal_abort call allows the caller to supply an errno
1684 * which will be recorded (if possible) in the journal superblock. This
1685 * allows a client to record failure conditions in the middle of a
1686 * transaction without having to complete the transaction to record the
1687 * failure to disk. ext3_error, for example, now uses this
1688 * functionality.
1689 *
1690 * Errors which originate from within the journaling layer will NOT
1691 * supply an errno; a null errno implies that absolutely no further
1692 * writes are done to the journal (unless there are any already in
1693 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
1696
1697void journal_abort(journal_t *journal, int errno)
1698{
1699 __journal_abort_soft(journal, errno);
1700}
1701
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001702/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * int journal_errno () - returns the journal's error state.
1704 * @journal: journal to examine.
1705 *
1706 * This is the errno numbet set with journal_abort(), the last
1707 * time the journal was mounted - if the journal was stopped
1708 * without calling abort this will be 0.
1709 *
1710 * If the journal has been aborted on this mount time -EROFS will
1711 * be returned.
1712 */
1713int journal_errno(journal_t *journal)
1714{
1715 int err;
1716
1717 spin_lock(&journal->j_state_lock);
1718 if (journal->j_flags & JFS_ABORT)
1719 err = -EROFS;
1720 else
1721 err = journal->j_errno;
1722 spin_unlock(&journal->j_state_lock);
1723 return err;
1724}
1725
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * int journal_clear_err () - clears the journal's error state
1728 * @journal: journal to act on.
1729 *
1730 * An error must be cleared or Acked to take a FS out of readonly
1731 * mode.
1732 */
1733int journal_clear_err(journal_t *journal)
1734{
1735 int err = 0;
1736
1737 spin_lock(&journal->j_state_lock);
1738 if (journal->j_flags & JFS_ABORT)
1739 err = -EROFS;
1740 else
1741 journal->j_errno = 0;
1742 spin_unlock(&journal->j_state_lock);
1743 return err;
1744}
1745
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001746/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * void journal_ack_err() - Ack journal err.
1748 * @journal: journal to act on.
1749 *
1750 * An error must be cleared or Acked to take a FS out of readonly
1751 * mode.
1752 */
1753void journal_ack_err(journal_t *journal)
1754{
1755 spin_lock(&journal->j_state_lock);
1756 if (journal->j_errno)
1757 journal->j_flags |= JFS_ACK_ERR;
1758 spin_unlock(&journal->j_state_lock);
1759}
1760
1761int journal_blocks_per_page(struct inode *inode)
1762{
1763 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1764}
1765
1766/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * Journal_head storage management
1768 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001769static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770#ifdef CONFIG_JBD_DEBUG
1771static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1772#endif
1773
1774static int journal_init_journal_head_cache(void)
1775{
1776 int retval;
1777
Al Viro1076d172008-03-29 03:07:18 +00001778 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 journal_head_cache = kmem_cache_create("journal_head",
1780 sizeof(struct journal_head),
1781 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001782 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001783 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001785 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 retval = -ENOMEM;
1787 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1788 }
1789 return retval;
1790}
1791
1792static void journal_destroy_journal_head_cache(void)
1793{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001794 if (journal_head_cache) {
1795 kmem_cache_destroy(journal_head_cache);
1796 journal_head_cache = NULL;
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800/*
1801 * journal_head splicing and dicing
1802 */
1803static struct journal_head *journal_alloc_journal_head(void)
1804{
1805 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807#ifdef CONFIG_JBD_DEBUG
1808 atomic_inc(&nr_journal_heads);
1809#endif
1810 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001811 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001813 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1814 __func__);
1815
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001816 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 yield();
1818 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1819 }
1820 }
1821 return ret;
1822}
1823
1824static void journal_free_journal_head(struct journal_head *jh)
1825{
1826#ifdef CONFIG_JBD_DEBUG
1827 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001828 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829#endif
1830 kmem_cache_free(journal_head_cache, jh);
1831}
1832
1833/*
1834 * A journal_head is attached to a buffer_head whenever JBD has an
1835 * interest in the buffer.
1836 *
1837 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1838 * is set. This bit is tested in core kernel code where we need to take
1839 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1840 * there.
1841 *
1842 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1843 *
1844 * When a buffer has its BH_JBD bit set it is immune from being released by
1845 * core kernel code, mainly via ->b_count.
1846 *
Jan Karabb189242011-06-24 23:11:59 +02001847 * A journal_head is detached from its buffer_head when the journal_head's
1848 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1849 * transaction (b_cp_transaction) hold their references to b_jcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 *
1851 * Various places in the kernel want to attach a journal_head to a buffer_head
1852 * _before_ attaching the journal_head to a transaction. To protect the
1853 * journal_head in this situation, journal_add_journal_head elevates the
1854 * journal_head's b_jcount refcount by one. The caller must call
1855 * journal_put_journal_head() to undo this.
1856 *
1857 * So the typical usage would be:
1858 *
1859 * (Attach a journal_head if needed. Increments b_jcount)
1860 * struct journal_head *jh = journal_add_journal_head(bh);
1861 * ...
Jan Karabb189242011-06-24 23:11:59 +02001862 * (Get another reference for transaction)
1863 * journal_grab_journal_head(bh);
1864 * jh->b_transaction = xxx;
1865 * (Put original reference)
1866 * journal_put_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868
1869/*
1870 * Give a buffer_head a journal_head.
1871 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * May sleep.
1873 */
1874struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1875{
1876 struct journal_head *jh;
1877 struct journal_head *new_jh = NULL;
1878
1879repeat:
1880 if (!buffer_jbd(bh)) {
1881 new_jh = journal_alloc_journal_head();
1882 memset(new_jh, 0, sizeof(*new_jh));
1883 }
1884
1885 jbd_lock_bh_journal_head(bh);
1886 if (buffer_jbd(bh)) {
1887 jh = bh2jh(bh);
1888 } else {
1889 J_ASSERT_BH(bh,
1890 (atomic_read(&bh->b_count) > 0) ||
1891 (bh->b_page && bh->b_page->mapping));
1892
1893 if (!new_jh) {
1894 jbd_unlock_bh_journal_head(bh);
1895 goto repeat;
1896 }
1897
1898 jh = new_jh;
1899 new_jh = NULL; /* We consumed it */
1900 set_buffer_jbd(bh);
1901 bh->b_private = jh;
1902 jh->b_bh = bh;
1903 get_bh(bh);
1904 BUFFER_TRACE(bh, "added journal_head");
1905 }
1906 jh->b_jcount++;
1907 jbd_unlock_bh_journal_head(bh);
1908 if (new_jh)
1909 journal_free_journal_head(new_jh);
1910 return bh->b_private;
1911}
1912
1913/*
1914 * Grab a ref against this buffer_head's journal_head. If it ended up not
1915 * having a journal_head, return NULL
1916 */
1917struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1918{
1919 struct journal_head *jh = NULL;
1920
1921 jbd_lock_bh_journal_head(bh);
1922 if (buffer_jbd(bh)) {
1923 jh = bh2jh(bh);
1924 jh->b_jcount++;
1925 }
1926 jbd_unlock_bh_journal_head(bh);
1927 return jh;
1928}
1929
1930static void __journal_remove_journal_head(struct buffer_head *bh)
1931{
1932 struct journal_head *jh = bh2jh(bh);
1933
1934 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karabb189242011-06-24 23:11:59 +02001935 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1936 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1937 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1938 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1939 J_ASSERT_BH(bh, buffer_jbd(bh));
1940 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1941 BUFFER_TRACE(bh, "remove journal_head");
1942 if (jh->b_frozen_data) {
1943 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1944 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Jan Karabb189242011-06-24 23:11:59 +02001946 if (jh->b_committed_data) {
1947 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1948 jbd_free(jh->b_committed_data, bh->b_size);
1949 }
1950 bh->b_private = NULL;
1951 jh->b_bh = NULL; /* debug, really */
1952 clear_buffer_jbd(bh);
1953 journal_free_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956/*
Jan Karabb189242011-06-24 23:11:59 +02001957 * Drop a reference on the passed journal_head. If it fell to zero then
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * release the journal_head from the buffer_head.
1959 */
1960void journal_put_journal_head(struct journal_head *jh)
1961{
1962 struct buffer_head *bh = jh2bh(jh);
1963
1964 jbd_lock_bh_journal_head(bh);
1965 J_ASSERT_JH(jh, jh->b_jcount > 0);
1966 --jh->b_jcount;
Jan Karabb189242011-06-24 23:11:59 +02001967 if (!jh->b_jcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 __journal_remove_journal_head(bh);
Jan Karabb189242011-06-24 23:11:59 +02001969 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 __brelse(bh);
Jan Karabb189242011-06-24 23:11:59 +02001971 } else
1972 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07001976 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07001978#ifdef CONFIG_JBD_DEBUG
1979
1980u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Jose R. Santosc2a91592007-10-18 23:39:22 -07001983static struct dentry *jbd_debugfs_dir;
1984static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jose R. Santosc2a91592007-10-18 23:39:22 -07001986static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001988 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1989 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08001990 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07001991 jbd_debugfs_dir,
1992 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Jose R. Santosc2a91592007-10-18 23:39:22 -07001995static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001997 debugfs_remove(jbd_debug);
1998 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001#else
2002
Jose R. Santosc2a91592007-10-18 23:39:22 -07002003static inline void jbd_create_debugfs_entry(void)
2004{
2005}
2006
2007static inline void jbd_remove_debugfs_entry(void)
2008{
2009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011#endif
2012
Christoph Lametere18b8902006-12-06 20:33:20 -08002013struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015static int __init journal_init_handle_cache(void)
2016{
2017 jbd_handle_cache = kmem_cache_create("journal_handle",
2018 sizeof(handle_t),
2019 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07002020 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002021 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (jbd_handle_cache == NULL) {
2023 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2024 return -ENOMEM;
2025 }
2026 return 0;
2027}
2028
2029static void journal_destroy_handle_cache(void)
2030{
2031 if (jbd_handle_cache)
2032 kmem_cache_destroy(jbd_handle_cache);
2033}
2034
2035/*
2036 * Module startup and shutdown
2037 */
2038
2039static int __init journal_init_caches(void)
2040{
2041 int ret;
2042
2043 ret = journal_init_revoke_caches();
2044 if (ret == 0)
2045 ret = journal_init_journal_head_cache();
2046 if (ret == 0)
2047 ret = journal_init_handle_cache();
2048 return ret;
2049}
2050
2051static void journal_destroy_caches(void)
2052{
2053 journal_destroy_revoke_caches();
2054 journal_destroy_journal_head_cache();
2055 journal_destroy_handle_cache();
2056}
2057
2058static int __init journal_init(void)
2059{
2060 int ret;
2061
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002062 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 ret = journal_init_caches();
2065 if (ret != 0)
2066 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002067 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return ret;
2069}
2070
2071static void __exit journal_exit(void)
2072{
2073#ifdef CONFIG_JBD_DEBUG
2074 int n = atomic_read(&nr_journal_heads);
2075 if (n)
2076 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2077#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002078 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 journal_destroy_caches();
2080}
2081
2082MODULE_LICENSE("GPL");
2083module_init(journal_init);
2084module_exit(journal_exit);
2085