blob: e49e81bb80ef76feafe8054e04b717f547699505 [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
132 /* Record that the journal thread is running */
133 journal->j_task = current;
134 wake_up(&journal->j_wait_done_commit);
135
136 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
137 journal->j_commit_interval / HZ);
138
139 /*
140 * And now, wait forever for commit wakeup events.
141 */
142 spin_lock(&journal->j_state_lock);
143
144loop:
145 if (journal->j_flags & JFS_UNMOUNT)
146 goto end_loop;
147
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
150
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800154 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 journal_commit_transaction(journal);
156 spin_lock(&journal->j_state_lock);
157 goto loop;
158 }
159
160 wake_up(&journal->j_wait_done_commit);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700161 if (freezing(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
166 */
167 jbd_debug(1, "Now suspending kjournald\n");
168 spin_unlock(&journal->j_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -0800169 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_lock(&journal->j_state_lock);
171 } else {
172 /*
173 * We assume on resume that commits are already there,
174 * so we don't sleep
175 */
176 DEFINE_WAIT(wait);
177 int should_sleep = 1;
178
179 prepare_to_wait(&journal->j_wait_commit, &wait,
180 TASK_INTERRUPTIBLE);
181 if (journal->j_commit_sequence != journal->j_commit_request)
182 should_sleep = 0;
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
186 should_sleep = 0;
Mark Fashehcbf0d272005-09-06 15:19:08 -0700187 if (journal->j_flags & JFS_UNMOUNT)
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700188 should_sleep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (should_sleep) {
190 spin_unlock(&journal->j_state_lock);
191 schedule();
192 spin_lock(&journal->j_state_lock);
193 }
194 finish_wait(&journal->j_wait_commit, &wait);
195 }
196
197 jbd_debug(1, "kjournald wakes\n");
198
199 /*
200 * Were we woken up by a commit wakeup event?
201 */
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
206 }
207 goto loop;
208
209end_loop:
210 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800211 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
215 return 0;
216}
217
Pavel Emelianov97f06782007-05-08 00:30:42 -0700218static int journal_start_thread(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700220 struct task_struct *t;
221
222 t = kthread_run(kjournald, journal, "kjournald");
223 if (IS_ERR(t))
224 return PTR_ERR(t);
225
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700226 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static void journal_kill_thread(journal_t *journal)
231{
232 spin_lock(&journal->j_state_lock);
233 journal->j_flags |= JFS_UNMOUNT;
234
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700238 wait_event(journal->j_wait_done_commit,
239 journal->j_task == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 spin_lock(&journal->j_state_lock);
241 }
242 spin_unlock(&journal->j_state_lock);
243}
244
245/*
246 * journal_write_metadata_buffer: write a metadata buffer to the journal.
247 *
248 * Writes a metadata buffer to a given disk block. The actual IO is not
249 * performed but a new buffer_head is constructed which labels the data
250 * to be written with the correct destination disk block.
251 *
252 * Any magic-number escaping which needs to be done will cause a
253 * copy-out here. If the buffer happens to start with the
254 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
255 * magic number is only written to the log for descripter blocks. In
256 * this case, we copy the data and replace the first word with 0, and we
257 * return a result code which indicates that this buffer needs to be
258 * marked as an escaped buffer in the corresponding log descriptor
259 * block. The missing word can then be restored when the block is read
260 * during recovery.
261 *
262 * If the source buffer has already been modified by a new transaction
263 * since we took the last commit snapshot, we use the frozen copy of
264 * that data for IO. If we end up using the existing buffer_head's data
265 * for the write, then we *have* to lock the buffer to prevent anyone
266 * else from using and possibly modifying it while the IO is in
267 * progress.
268 *
269 * The function returns a pointer to the buffer_heads to be used for IO.
270 *
271 * We assume that the journal has already been locked in this function.
272 *
273 * Return value:
274 * <0: Error
275 * >=0: Finished OK
276 *
277 * On success:
278 * Bit 0 set == escape performed on the data
279 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
280 */
281
282int journal_write_metadata_buffer(transaction_t *transaction,
283 struct journal_head *jh_in,
284 struct journal_head **jh_out,
Jan Kara9c28cbc2009-08-03 19:21:00 +0200285 unsigned int blocknr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 int need_copy_out = 0;
288 int done_copy_out = 0;
289 int do_escape = 0;
290 char *mapped_data;
291 struct buffer_head *new_bh;
292 struct journal_head *new_jh;
293 struct page *new_page;
294 unsigned int new_offset;
295 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghuaf1015c42009-07-15 21:42:05 +0200296 journal_t *journal = transaction->t_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * The buffer really shouldn't be locked: only the current committing
300 * transaction is allowed to write it, so nobody else is allowed
301 * to do any IO.
302 *
303 * akpm: except if we're journalling data, and write() output is
304 * also part of a shared mapping, and another thread has
305 * decided to launch a writepage() against this buffer.
306 */
307 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
308
309 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghuaf1015c42009-07-15 21:42:05 +0200310 /* keep subsequent assertions sane */
311 new_bh->b_state = 0;
312 init_buffer(new_bh, NULL, NULL);
313 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 */
Ted Ts'od9b01932011-04-30 13:17:11 -0400447 if (journal->j_running_transaction &&
448 journal->j_running_transaction->t_tid == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200450 * We want a new commit: OK, mark the request and wakeup the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * commit thread. We do _not_ do the commit ourselves.
452 */
453
454 journal->j_commit_request = target;
455 jbd_debug(1, "JBD: requesting commit %d/%d\n",
456 journal->j_commit_request,
457 journal->j_commit_sequence);
458 wake_up(&journal->j_wait_commit);
459 return 1;
Ted Ts'od9b01932011-04-30 13:17:11 -0400460 } else if (!tid_geq(journal->j_commit_request, target))
461 /* This should never happen, but if it does, preserve
462 the evidence before kjournald goes into a loop and
463 increments j_commit_sequence beyond all recognition. */
464 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
465 journal->j_commit_request, journal->j_commit_sequence,
466 target, journal->j_running_transaction ?
467 journal->j_running_transaction->t_tid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
471int log_start_commit(journal_t *journal, tid_t tid)
472{
473 int ret;
474
475 spin_lock(&journal->j_state_lock);
476 ret = __log_start_commit(journal, tid);
477 spin_unlock(&journal->j_state_lock);
478 return ret;
479}
480
481/*
482 * Force and wait upon a commit if the calling process is not within
483 * transaction. This is used for forcing out undo-protected data which contains
484 * bitmaps, when the fs is running out of space.
485 *
486 * We can only force the running transaction if we don't have an active handle;
487 * otherwise, we will deadlock.
488 *
489 * Returns true if a transaction was started.
490 */
491int journal_force_commit_nested(journal_t *journal)
492{
493 transaction_t *transaction = NULL;
494 tid_t tid;
495
496 spin_lock(&journal->j_state_lock);
497 if (journal->j_running_transaction && !current->journal_info) {
498 transaction = journal->j_running_transaction;
499 __log_start_commit(journal, transaction->t_tid);
500 } else if (journal->j_committing_transaction)
501 transaction = journal->j_committing_transaction;
502
503 if (!transaction) {
504 spin_unlock(&journal->j_state_lock);
505 return 0; /* Nothing to retry */
506 }
507
508 tid = transaction->t_tid;
509 spin_unlock(&journal->j_state_lock);
510 log_wait_commit(journal, tid);
511 return 1;
512}
513
514/*
515 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800516 * if a transaction is going to be committed (or is currently already
517 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519int journal_start_commit(journal_t *journal, tid_t *ptid)
520{
521 int ret = 0;
522
523 spin_lock(&journal->j_state_lock);
524 if (journal->j_running_transaction) {
525 tid_t tid = journal->j_running_transaction->t_tid;
526
Jan Kara8fe4cd02009-02-11 13:04:25 -0800527 __log_start_commit(journal, tid);
528 /* There's a running transaction and we've just made sure
529 * it's commit has been scheduled. */
530 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800532 ret = 1;
533 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * If ext3_write_super() recently started a commit, then we
536 * have to wait for completion of that transaction
537 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800538 if (ptid)
539 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ret = 1;
541 }
542 spin_unlock(&journal->j_state_lock);
543 return ret;
544}
545
546/*
547 * Wait for a specified commit to complete.
548 * The caller may not hold the journal lock.
549 */
550int log_wait_commit(journal_t *journal, tid_t tid)
551{
552 int err = 0;
553
554#ifdef CONFIG_JBD_DEBUG
555 spin_lock(&journal->j_state_lock);
556 if (!tid_geq(journal->j_commit_request, tid)) {
557 printk(KERN_EMERG
558 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700559 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 spin_unlock(&journal->j_state_lock);
562#endif
563 spin_lock(&journal->j_state_lock);
564 while (tid_gt(tid, journal->j_commit_sequence)) {
565 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
566 tid, journal->j_commit_sequence);
567 wake_up(&journal->j_wait_commit);
568 spin_unlock(&journal->j_state_lock);
569 wait_event(journal->j_wait_done_commit,
570 !tid_gt(tid, journal->j_commit_sequence));
571 spin_lock(&journal->j_state_lock);
572 }
573 spin_unlock(&journal->j_state_lock);
574
575 if (unlikely(is_journal_aborted(journal))) {
576 printk(KERN_EMERG "journal commit I/O error\n");
577 err = -EIO;
578 }
579 return err;
580}
581
582/*
Jan Kara03f4d802010-04-15 22:16:24 +0200583 * Return 1 if a given transaction has not yet sent barrier request
584 * connected with a transaction commit. If 0 is returned, transaction
585 * may or may not have sent the barrier. Used to avoid sending barrier
586 * twice in common cases.
587 */
588int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
589{
590 int ret = 0;
591 transaction_t *commit_trans;
592
593 if (!(journal->j_flags & JFS_BARRIER))
594 return 0;
595 spin_lock(&journal->j_state_lock);
596 /* Transaction already committed? */
597 if (tid_geq(journal->j_commit_sequence, tid))
598 goto out;
599 /*
600 * Transaction is being committed and we already proceeded to
601 * writing commit record?
602 */
603 commit_trans = journal->j_committing_transaction;
604 if (commit_trans && commit_trans->t_tid == tid &&
605 commit_trans->t_state >= T_COMMIT_RECORD)
606 goto out;
607 ret = 1;
608out:
609 spin_unlock(&journal->j_state_lock);
610 return ret;
611}
Jan Kara52779702010-04-15 22:24:26 +0200612EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200613
614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * Log buffer allocation routines:
616 */
617
Jan Kara9c28cbc2009-08-03 19:21:00 +0200618int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200620 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 spin_lock(&journal->j_state_lock);
623 J_ASSERT(journal->j_free > 1);
624
625 blocknr = journal->j_head;
626 journal->j_head++;
627 journal->j_free--;
628 if (journal->j_head == journal->j_last)
629 journal->j_head = journal->j_first;
630 spin_unlock(&journal->j_state_lock);
631 return journal_bmap(journal, blocknr, retp);
632}
633
634/*
635 * Conversion of logical to physical block numbers for the journal
636 *
637 * On external journals the journal blocks are identity-mapped, so
638 * this is a no-op. If needed, we can use j_blk_offset - everything is
639 * ready.
640 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200641int journal_bmap(journal_t *journal, unsigned int blocknr,
642 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200645 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (journal->j_inode) {
648 ret = bmap(journal->j_inode, blocknr);
649 if (ret)
650 *retp = ret;
651 else {
652 char b[BDEVNAME_SIZE];
653
654 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200655 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700656 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 blocknr,
658 bdevname(journal->j_dev, b));
659 err = -EIO;
660 __journal_abort_soft(journal, err);
661 }
662 } else {
663 *retp = blocknr; /* +journal->j_blk_offset */
664 }
665 return err;
666}
667
668/*
669 * We play buffer_head aliasing tricks to write data/metadata blocks to
670 * the journal without copying their contents, but for journal
671 * descriptor blocks we do need to generate bona fide buffers.
672 *
673 * After the caller of journal_get_descriptor_buffer() has finished modifying
674 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
675 * But we don't bother doing that, so there will be coherency problems with
676 * mmaps of blockdevs which hold live JBD-controlled filesystems.
677 */
678struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
679{
680 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200681 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int err;
683
684 err = journal_next_log_block(journal, &blocknr);
685
686 if (err)
687 return NULL;
688
689 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700690 if (!bh)
691 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 lock_buffer(bh);
693 memset(bh->b_data, 0, journal->j_blocksize);
694 set_buffer_uptodate(bh);
695 unlock_buffer(bh);
696 BUFFER_TRACE(bh, "return this buffer");
697 return journal_add_journal_head(bh);
698}
699
700/*
701 * Management for journal control blocks: functions to create and
702 * destroy journal_t structures, and to initialise and read existing
703 * journal blocks from disk. */
704
705/* First: create and setup a journal_t object in memory. We initialise
706 * very few fields yet: that has to wait until we have created the
707 * journal structures from from scratch, or loaded them from disk. */
708
709static journal_t * journal_init_common (void)
710{
711 journal_t *journal;
712 int err;
713
Mingming Cao8c3478a2007-10-18 23:39:20 -0700714 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!journal)
716 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 init_waitqueue_head(&journal->j_wait_transaction_locked);
719 init_waitqueue_head(&journal->j_wait_logspace);
720 init_waitqueue_head(&journal->j_wait_done_commit);
721 init_waitqueue_head(&journal->j_wait_checkpoint);
722 init_waitqueue_head(&journal->j_wait_commit);
723 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800724 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_lock_init(&journal->j_revoke_lock);
726 spin_lock_init(&journal->j_list_lock);
727 spin_lock_init(&journal->j_state_lock);
728
729 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
730
731 /* The journal is marked for error until we succeed with recovery! */
732 journal->j_flags = JFS_ABORT;
733
734 /* Set up a default-sized revoke table for the new mount. */
735 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
736 if (err) {
737 kfree(journal);
738 goto fail;
739 }
740 return journal;
741fail:
742 return NULL;
743}
744
745/* journal_init_dev and journal_init_inode:
746 *
747 * Create a journal structure assigned some fixed set of disk blocks to
748 * the journal. We don't actually touch those disk blocks yet, but we
749 * need to set up all of the mapping information to tell the journaling
750 * system where the journal blocks are.
751 *
752 */
753
754/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700755 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * @bdev: Block device on which to create the journal
757 * @fs_dev: Device which hold journalled filesystem for this journal.
758 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700759 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700761 *
762 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700763 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * journal_init_dev creates a journal which maps a fixed contiguous
765 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700766 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
768journal_t * journal_init_dev(struct block_device *bdev,
769 struct block_device *fs_dev,
770 int start, int len, int blocksize)
771{
772 journal_t *journal = journal_init_common();
773 struct buffer_head *bh;
774 int n;
775
776 if (!journal)
777 return NULL;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700780 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 n = journal->j_blocksize / sizeof(journal_block_tag_t);
782 journal->j_wbufsize = n;
783 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
784 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300785 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700786 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700787 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700789 journal->j_dev = bdev;
790 journal->j_fs_dev = fs_dev;
791 journal->j_blk_offset = start;
792 journal->j_maxlen = len;
793
794 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700795 if (!bh) {
796 printk(KERN_ERR
797 "%s: Cannot get buffer for journal superblock\n",
798 __func__);
799 goto out_err;
800 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700801 journal->j_sb_buffer = bh;
802 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700805out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800806 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700807 kfree(journal);
808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700810
811/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
813 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700814 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * journal_init_inode creates a journal which maps an on-disk inode as
816 * the journal. The inode must exist already, must support bmap() and
817 * must have all data blocks preallocated.
818 */
819journal_t * journal_init_inode (struct inode *inode)
820{
821 struct buffer_head *bh;
822 journal_t *journal = journal_init_common();
823 int err;
824 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200825 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (!journal)
828 return NULL;
829
830 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
831 journal->j_inode = inode;
832 jbd_debug(1,
833 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700834 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 (long long) inode->i_size,
836 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
837
838 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
839 journal->j_blocksize = inode->i_sb->s_blocksize;
840
841 /* journal descriptor can store up to n blocks -bzzz */
842 n = journal->j_blocksize / sizeof(journal_block_tag_t);
843 journal->j_wbufsize = n;
844 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
845 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300846 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700847 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700848 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 err = journal_bmap(journal, 0, &blocknr);
852 /* If that failed, give up */
853 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -0800854 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700855 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700856 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700860 if (!bh) {
861 printk(KERN_ERR
862 "%s: Cannot get buffer for journal superblock\n",
863 __func__);
864 goto out_err;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 journal->j_sb_buffer = bh;
867 journal->j_superblock = (journal_superblock_t *)bh->b_data;
868
869 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700870out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800871 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700872 kfree(journal);
873 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * If the journal init or create aborts, we need to mark the journal
878 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700879 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
881static void journal_fail_superblock (journal_t *journal)
882{
883 struct buffer_head *bh = journal->j_sb_buffer;
884 brelse(bh);
885 journal->j_sb_buffer = NULL;
886}
887
888/*
889 * Given a journal_t structure, initialise the various fields for
890 * startup of a new journaling session. We use this both when creating
891 * a journal, and after recovering an old journal to reset it for
892 * subsequent use.
893 */
894
895static int journal_reset(journal_t *journal)
896{
897 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200898 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 first = be32_to_cpu(sb->s_first);
901 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200902 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200903 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200904 first, last);
905 journal_fail_superblock(journal);
906 return -EINVAL;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 journal->j_first = first;
910 journal->j_last = last;
911
912 journal->j_head = first;
913 journal->j_tail = first;
914 journal->j_free = last - first;
915
916 journal->j_tail_sequence = journal->j_transaction_sequence;
917 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
918 journal->j_commit_request = journal->j_commit_sequence;
919
920 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
921
922 /* Add the dynamic fields and write it to disk. */
923 journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700924 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700927/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * int journal_create() - Initialise the new journal file
929 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700930 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * Given a journal_t structure which tells us which disk blocks we can
932 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700933 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 **/
935int journal_create(journal_t *journal)
936{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200937 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct buffer_head *bh;
939 journal_superblock_t *sb;
940 int i, err;
941
942 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
943 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
944 journal->j_maxlen);
945 journal_fail_superblock(journal);
946 return -EINVAL;
947 }
948
949 if (journal->j_inode == NULL) {
950 /*
951 * We don't know what block to start at!
952 */
953 printk(KERN_EMERG
954 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700955 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 BUG();
957 }
958
959 /* Zero out the entire journal on disk. We cannot afford to
960 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
961 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
962 for (i = 0; i < journal->j_maxlen; i++) {
963 err = journal_bmap(journal, i, &blocknr);
964 if (err)
965 return err;
966 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Namhyung Kim2a0e3382010-10-13 17:17:18 +0900967 if (unlikely(!bh))
968 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 lock_buffer(bh);
970 memset (bh->b_data, 0, journal->j_blocksize);
971 BUFFER_TRACE(bh, "marking dirty");
972 mark_buffer_dirty(bh);
973 BUFFER_TRACE(bh, "marking uptodate");
974 set_buffer_uptodate(bh);
975 unlock_buffer(bh);
976 __brelse(bh);
977 }
978
979 sync_blockdev(journal->j_dev);
980 jbd_debug(1, "JBD: journal cleared.\n");
981
982 /* OK, fill in the initial static fields in the new superblock */
983 sb = journal->j_superblock;
984
985 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
986 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
987
988 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
989 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
990 sb->s_first = cpu_to_be32(1);
991
992 journal->j_transaction_sequence = 1;
993
994 journal->j_flags &= ~JFS_ABORT;
995 journal->j_format_version = 2;
996
997 return journal_reset(journal);
998}
999
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001000/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * void journal_update_superblock() - Update journal sb on disk.
1002 * @journal: The journal to update.
1003 * @wait: Set to '0' if you don't want to wait for IO completion.
1004 *
1005 * Update a journal's dynamic superblock fields and write it to disk,
1006 * optionally waiting for the IO to complete.
1007 */
1008void journal_update_superblock(journal_t *journal, int wait)
1009{
1010 journal_superblock_t *sb = journal->j_superblock;
1011 struct buffer_head *bh = journal->j_sb_buffer;
1012
1013 /*
1014 * As a special case, if the on-disk copy is already marked as needing
1015 * no recovery (s_start == 0) and there are no outstanding transactions
1016 * in the filesystem, then we can safely defer the superblock update
1017 * until the next commit by setting JFS_FLUSHED. This avoids
1018 * attempting a write to a potential-readonly device.
1019 */
1020 if (sb->s_start == 0 && journal->j_tail_sequence ==
1021 journal->j_transaction_sequence) {
1022 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
Jan Kara9c28cbc2009-08-03 19:21:00 +02001023 "(start %u, seq %d, errno %d)\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001024 journal->j_tail, journal->j_tail_sequence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 journal->j_errno);
1026 goto out;
1027 }
1028
Darrick J. Wongdff68252010-10-04 12:35:05 -07001029 if (buffer_write_io_error(bh)) {
1030 char b[BDEVNAME_SIZE];
1031 /*
1032 * Oh, dear. A previous attempt to write the journal
1033 * superblock failed. This could happen because the
1034 * USB device was yanked out. Or it could happen to
1035 * be a transient write error and maybe the block will
1036 * be remapped. Nothing we can do but to retry the
1037 * write and hope for the best.
1038 */
1039 printk(KERN_ERR "JBD: previous I/O error detected "
1040 "for journal superblock update for %s.\n",
1041 journal_dev_name(journal, b));
1042 clear_buffer_write_io_error(bh);
1043 set_buffer_uptodate(bh);
1044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 spin_lock(&journal->j_state_lock);
Jan Kara9c28cbc2009-08-03 19:21:00 +02001047 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1049
1050 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1051 sb->s_start = cpu_to_be32(journal->j_tail);
1052 sb->s_errno = cpu_to_be32(journal->j_errno);
1053 spin_unlock(&journal->j_state_lock);
1054
1055 BUFFER_TRACE(bh, "marking dirty");
1056 mark_buffer_dirty(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001057 if (wait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 sync_dirty_buffer(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001059 if (buffer_write_io_error(bh)) {
1060 char b[BDEVNAME_SIZE];
1061 printk(KERN_ERR "JBD: I/O error detected "
1062 "when updating journal superblock for %s.\n",
1063 journal_dev_name(journal, b));
1064 clear_buffer_write_io_error(bh);
1065 set_buffer_uptodate(bh);
1066 }
1067 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001068 write_dirty_buffer(bh, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Lukas Czerner99cb1a32011-05-23 18:33:02 +02001070 trace_jbd_update_superblock_end(journal, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071out:
1072 /* If we have just flushed the log (by marking s_start==0), then
1073 * any future commit will have to be careful to update the
1074 * superblock again to re-record the true start of the log. */
1075
1076 spin_lock(&journal->j_state_lock);
1077 if (sb->s_start)
1078 journal->j_flags &= ~JFS_FLUSHED;
1079 else
1080 journal->j_flags |= JFS_FLUSHED;
1081 spin_unlock(&journal->j_state_lock);
1082}
1083
1084/*
1085 * Read the superblock for a given journal, performing initial
1086 * validation of the format.
1087 */
1088
1089static int journal_get_superblock(journal_t *journal)
1090{
1091 struct buffer_head *bh;
1092 journal_superblock_t *sb;
1093 int err = -EIO;
1094
1095 bh = journal->j_sb_buffer;
1096
1097 J_ASSERT(bh != NULL);
1098 if (!buffer_uptodate(bh)) {
1099 ll_rw_block(READ, 1, &bh);
1100 wait_on_buffer(bh);
1101 if (!buffer_uptodate(bh)) {
1102 printk (KERN_ERR
1103 "JBD: IO error reading journal superblock\n");
1104 goto out;
1105 }
1106 }
1107
1108 sb = journal->j_superblock;
1109
1110 err = -EINVAL;
1111
1112 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1113 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1114 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1115 goto out;
1116 }
1117
1118 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1119 case JFS_SUPERBLOCK_V1:
1120 journal->j_format_version = 1;
1121 break;
1122 case JFS_SUPERBLOCK_V2:
1123 journal->j_format_version = 2;
1124 break;
1125 default:
1126 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1127 goto out;
1128 }
1129
1130 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1131 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1132 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1133 printk (KERN_WARNING "JBD: journal file too short\n");
1134 goto out;
1135 }
1136
Eryu Guan87622022011-11-01 19:04:59 -04001137 if (be32_to_cpu(sb->s_first) == 0 ||
1138 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1139 printk(KERN_WARNING
1140 "JBD: Invalid start block of journal: %u\n",
1141 be32_to_cpu(sb->s_first));
1142 goto out;
1143 }
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146
1147out:
1148 journal_fail_superblock(journal);
1149 return err;
1150}
1151
1152/*
1153 * Load the on-disk journal superblock and read the key fields into the
1154 * journal_t.
1155 */
1156
1157static int load_superblock(journal_t *journal)
1158{
1159 int err;
1160 journal_superblock_t *sb;
1161
1162 err = journal_get_superblock(journal);
1163 if (err)
1164 return err;
1165
1166 sb = journal->j_superblock;
1167
1168 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1169 journal->j_tail = be32_to_cpu(sb->s_start);
1170 journal->j_first = be32_to_cpu(sb->s_first);
1171 journal->j_last = be32_to_cpu(sb->s_maxlen);
1172 journal->j_errno = be32_to_cpu(sb->s_errno);
1173
1174 return 0;
1175}
1176
1177
1178/**
1179 * int journal_load() - Read journal from disk.
1180 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * Given a journal_t structure which tells us which disk blocks contain
1183 * a journal, read the journal from disk to initialise the in-memory
1184 * structures.
1185 */
1186int journal_load(journal_t *journal)
1187{
1188 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001189 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 err = load_superblock(journal);
1192 if (err)
1193 return err;
1194
Badari Pulavartyea817392006-08-27 01:23:52 -07001195 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* If this is a V2 superblock, then we have to check the
1197 * features flags on it. */
1198
1199 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if ((sb->s_feature_ro_compat &
1201 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1202 (sb->s_feature_incompat &
1203 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1204 printk (KERN_WARNING
1205 "JBD: Unrecognised features on journal\n");
1206 return -EINVAL;
1207 }
1208 }
1209
1210 /* Let the recovery code check whether it needs to recover any
1211 * data from the journal. */
1212 if (journal_recover(journal))
1213 goto recovery_error;
1214
1215 /* OK, we've finished with the dynamic journal bits:
1216 * reinitialise the dynamic contents of the superblock in memory
1217 * and reset them on disk. */
1218 if (journal_reset(journal))
1219 goto recovery_error;
1220
1221 journal->j_flags &= ~JFS_ABORT;
1222 journal->j_flags |= JFS_LOADED;
1223 return 0;
1224
1225recovery_error:
1226 printk (KERN_WARNING "JBD: recovery failed\n");
1227 return -EIO;
1228}
1229
1230/**
1231 * void journal_destroy() - Release a journal_t structure.
1232 * @journal: Journal to act on.
1233 *
1234 * Release a journal_t structure once it is no longer in use by the
1235 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001236 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001238int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001240 int err = 0;
1241
Jan Kara03f4d802010-04-15 22:16:24 +02001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Wait for the commit thread to wake up and die. */
1244 journal_kill_thread(journal);
1245
1246 /* Force a final log commit */
1247 if (journal->j_running_transaction)
1248 journal_commit_transaction(journal);
1249
1250 /* Force any old transactions to disk */
1251
1252 /* Totally anal locking here... */
1253 spin_lock(&journal->j_list_lock);
1254 while (journal->j_checkpoint_transactions != NULL) {
1255 spin_unlock(&journal->j_list_lock);
1256 log_do_checkpoint(journal);
1257 spin_lock(&journal->j_list_lock);
1258 }
1259
1260 J_ASSERT(journal->j_running_transaction == NULL);
1261 J_ASSERT(journal->j_committing_transaction == NULL);
1262 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1263 spin_unlock(&journal->j_list_lock);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001266 if (!is_journal_aborted(journal)) {
1267 /* We can now mark the journal as empty. */
1268 journal->j_tail = 0;
1269 journal->j_tail_sequence =
1270 ++journal->j_transaction_sequence;
1271 journal_update_superblock(journal, 1);
1272 } else {
1273 err = -EIO;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 brelse(journal->j_sb_buffer);
1276 }
1277
1278 if (journal->j_inode)
1279 iput(journal->j_inode);
1280 if (journal->j_revoke)
1281 journal_destroy_revoke(journal);
1282 kfree(journal->j_wbuf);
1283 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001284
1285 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288
1289/**
1290 *int journal_check_used_features () - Check if features specified are used.
1291 * @journal: Journal to check.
1292 * @compat: bitmask of compatible features
1293 * @ro: bitmask of features that force read-only mount
1294 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001295 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001297 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 **/
1299
1300int journal_check_used_features (journal_t *journal, unsigned long compat,
1301 unsigned long ro, unsigned long incompat)
1302{
1303 journal_superblock_t *sb;
1304
1305 if (!compat && !ro && !incompat)
1306 return 1;
1307 if (journal->j_format_version == 1)
1308 return 0;
1309
1310 sb = journal->j_superblock;
1311
1312 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1313 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1314 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1315 return 1;
1316
1317 return 0;
1318}
1319
1320/**
1321 * int journal_check_available_features() - Check feature set in journalling layer
1322 * @journal: Journal to check.
1323 * @compat: bitmask of compatible features
1324 * @ro: bitmask of features that force read-only mount
1325 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * Check whether the journaling code supports the use of
1328 * all of a given set of features on this journal. Return true
1329 * (non-zero) if it can. */
1330
1331int journal_check_available_features (journal_t *journal, unsigned long compat,
1332 unsigned long ro, unsigned long incompat)
1333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!compat && !ro && !incompat)
1335 return 1;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* We can support any known requested features iff the
1338 * superblock is in version 2. Otherwise we fail to support any
1339 * extended sb features. */
1340
1341 if (journal->j_format_version != 2)
1342 return 0;
1343
1344 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1345 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1346 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1347 return 1;
1348
1349 return 0;
1350}
1351
1352/**
1353 * int journal_set_features () - Mark a given journal feature in the superblock
1354 * @journal: Journal to act on.
1355 * @compat: bitmask of compatible features
1356 * @ro: bitmask of features that force read-only mount
1357 * @incompat: bitmask of incompatible features
1358 *
1359 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001360 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 *
1362 */
1363
1364int journal_set_features (journal_t *journal, unsigned long compat,
1365 unsigned long ro, unsigned long incompat)
1366{
1367 journal_superblock_t *sb;
1368
1369 if (journal_check_used_features(journal, compat, ro, incompat))
1370 return 1;
1371
1372 if (!journal_check_available_features(journal, compat, ro, incompat))
1373 return 0;
1374
1375 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1376 compat, ro, incompat);
1377
1378 sb = journal->j_superblock;
1379
1380 sb->s_feature_compat |= cpu_to_be32(compat);
1381 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1382 sb->s_feature_incompat |= cpu_to_be32(incompat);
1383
1384 return 1;
1385}
1386
1387
1388/**
1389 * int journal_update_format () - Update on-disk journal structure.
1390 * @journal: Journal to act on.
1391 *
1392 * Given an initialised but unloaded journal struct, poke about in the
1393 * on-disk structure to update it to the most recent supported version.
1394 */
1395int journal_update_format (journal_t *journal)
1396{
1397 journal_superblock_t *sb;
1398 int err;
1399
1400 err = journal_get_superblock(journal);
1401 if (err)
1402 return err;
1403
1404 sb = journal->j_superblock;
1405
1406 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1407 case JFS_SUPERBLOCK_V2:
1408 return 0;
1409 case JFS_SUPERBLOCK_V1:
1410 return journal_convert_superblock_v1(journal, sb);
1411 default:
1412 break;
1413 }
1414 return -EINVAL;
1415}
1416
1417static int journal_convert_superblock_v1(journal_t *journal,
1418 journal_superblock_t *sb)
1419{
1420 int offset, blocksize;
1421 struct buffer_head *bh;
1422
1423 printk(KERN_WARNING
1424 "JBD: Converting superblock from version 1 to 2.\n");
1425
1426 /* Pre-initialise new fields to zero */
1427 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1428 blocksize = be32_to_cpu(sb->s_blocksize);
1429 memset(&sb->s_feature_compat, 0, blocksize-offset);
1430
1431 sb->s_nr_users = cpu_to_be32(1);
1432 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1433 journal->j_format_version = 2;
1434
1435 bh = journal->j_sb_buffer;
1436 BUFFER_TRACE(bh, "marking dirty");
1437 mark_buffer_dirty(bh);
1438 sync_dirty_buffer(bh);
1439 return 0;
1440}
1441
1442
1443/**
1444 * int journal_flush () - Flush journal
1445 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001446 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 * Flush all data for a given journal to disk and empty the journal.
1448 * Filesystems can use this when remounting readonly to ensure that
1449 * recovery does not need to happen on remount.
1450 */
1451
1452int journal_flush(journal_t *journal)
1453{
1454 int err = 0;
1455 transaction_t *transaction = NULL;
Jan Kara9c28cbc2009-08-03 19:21:00 +02001456 unsigned int old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 spin_lock(&journal->j_state_lock);
1459
1460 /* Force everything buffered to the log... */
1461 if (journal->j_running_transaction) {
1462 transaction = journal->j_running_transaction;
1463 __log_start_commit(journal, transaction->t_tid);
1464 } else if (journal->j_committing_transaction)
1465 transaction = journal->j_committing_transaction;
1466
1467 /* Wait for the log commit to complete... */
1468 if (transaction) {
1469 tid_t tid = transaction->t_tid;
1470
1471 spin_unlock(&journal->j_state_lock);
1472 log_wait_commit(journal, tid);
1473 } else {
1474 spin_unlock(&journal->j_state_lock);
1475 }
1476
1477 /* ...and flush everything in the log out to disk. */
1478 spin_lock(&journal->j_list_lock);
1479 while (!err && journal->j_checkpoint_transactions != NULL) {
1480 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001481 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001483 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_lock(&journal->j_list_lock);
1485 }
1486 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001487
1488 if (is_journal_aborted(journal))
1489 return -EIO;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 cleanup_journal_tail(journal);
1492
1493 /* Finally, mark the journal as really needing no recovery.
1494 * This sets s_start==0 in the underlying superblock, which is
1495 * the magic code for a fully-recovered superblock. Any future
1496 * commits of data to the journal will restore the current
1497 * s_start value. */
1498 spin_lock(&journal->j_state_lock);
1499 old_tail = journal->j_tail;
1500 journal->j_tail = 0;
1501 spin_unlock(&journal->j_state_lock);
1502 journal_update_superblock(journal, 1);
1503 spin_lock(&journal->j_state_lock);
1504 journal->j_tail = old_tail;
1505
1506 J_ASSERT(!journal->j_running_transaction);
1507 J_ASSERT(!journal->j_committing_transaction);
1508 J_ASSERT(!journal->j_checkpoint_transactions);
1509 J_ASSERT(journal->j_head == journal->j_tail);
1510 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1511 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/**
1516 * int journal_wipe() - Wipe journal contents
1517 * @journal: Journal to act on.
1518 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001519 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 * Wipe out all of the contents of a journal, safely. This will produce
1521 * a warning if the journal contains any valid recovery information.
1522 * Must be called between journal_init_*() and journal_load().
1523 *
1524 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1525 * we merely suppress recovery.
1526 */
1527
1528int journal_wipe(journal_t *journal, int write)
1529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 int err = 0;
1531
1532 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1533
1534 err = load_superblock(journal);
1535 if (err)
1536 return err;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!journal->j_tail)
1539 goto no_recovery;
1540
1541 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1542 write ? "Clearing" : "Ignoring");
1543
1544 err = journal_skip_recovery(journal);
1545 if (write)
1546 journal_update_superblock(journal, 1);
1547
1548 no_recovery:
1549 return err;
1550}
1551
1552/*
1553 * journal_dev_name: format a character string to describe on what
1554 * device this journal is present.
1555 */
1556
Adrian Bunk022a4a72005-09-06 15:16:41 -07001557static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct block_device *bdev;
1560
1561 if (journal->j_inode)
1562 bdev = journal->j_inode->i_sb->s_bdev;
1563 else
1564 bdev = journal->j_dev;
1565
1566 return bdevname(bdev, buffer);
1567}
1568
1569/*
1570 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001571 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 *
1573 * Two internal function, which provide abort to te jbd layer
1574 * itself are here.
1575 */
1576
1577/*
1578 * Quick version for internal journal use (doesn't lock the journal).
1579 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1580 * and don't attempt to make any other journal updates.
1581 */
Adrian Bunk53308382008-02-06 01:40:12 -08001582static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 transaction_t *transaction;
1585 char b[BDEVNAME_SIZE];
1586
1587 if (journal->j_flags & JFS_ABORT)
1588 return;
1589
1590 printk(KERN_ERR "Aborting journal on device %s.\n",
1591 journal_dev_name(journal, b));
1592
1593 spin_lock(&journal->j_state_lock);
1594 journal->j_flags |= JFS_ABORT;
1595 transaction = journal->j_running_transaction;
1596 if (transaction)
1597 __log_start_commit(journal, transaction->t_tid);
1598 spin_unlock(&journal->j_state_lock);
1599}
1600
1601/* Soft abort: record the abort error status in the journal superblock,
1602 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001603static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 if (journal->j_flags & JFS_ABORT)
1606 return;
1607
1608 if (!journal->j_errno)
1609 journal->j_errno = errno;
1610
1611 __journal_abort_hard(journal);
1612
1613 if (errno)
1614 journal_update_superblock(journal, 1);
1615}
1616
1617/**
1618 * void journal_abort () - Shutdown the journal immediately.
1619 * @journal: the journal to shutdown.
1620 * @errno: an error number to record in the journal indicating
1621 * the reason for the shutdown.
1622 *
1623 * Perform a complete, immediate shutdown of the ENTIRE
1624 * journal (not of a single transaction). This operation cannot be
1625 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * The journal_abort function is intended to support higher level error
1628 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1629 * mode.
1630 *
1631 * Journal abort has very specific semantics. Any existing dirty,
1632 * unjournaled buffers in the main filesystem will still be written to
1633 * disk by bdflush, but the journaling mechanism will be suspended
1634 * immediately and no further transaction commits will be honoured.
1635 *
1636 * Any dirty, journaled buffers will be written back to disk without
1637 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1638 * filesystem, but we _do_ attempt to leave as much data as possible
1639 * behind for fsck to use for cleanup.
1640 *
1641 * Any attempt to get a new transaction handle on a journal which is in
1642 * ABORT state will just result in an -EROFS error return. A
1643 * journal_stop on an existing handle will return -EIO if we have
1644 * entered abort state during the update.
1645 *
1646 * Recursive transactions are not disturbed by journal abort until the
1647 * final journal_stop, which will receive the -EIO error.
1648 *
1649 * Finally, the journal_abort call allows the caller to supply an errno
1650 * which will be recorded (if possible) in the journal superblock. This
1651 * allows a client to record failure conditions in the middle of a
1652 * transaction without having to complete the transaction to record the
1653 * failure to disk. ext3_error, for example, now uses this
1654 * functionality.
1655 *
1656 * Errors which originate from within the journaling layer will NOT
1657 * supply an errno; a null errno implies that absolutely no further
1658 * writes are done to the journal (unless there are any already in
1659 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662
1663void journal_abort(journal_t *journal, int errno)
1664{
1665 __journal_abort_soft(journal, errno);
1666}
1667
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001668/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * int journal_errno () - returns the journal's error state.
1670 * @journal: journal to examine.
1671 *
1672 * This is the errno numbet set with journal_abort(), the last
1673 * time the journal was mounted - if the journal was stopped
1674 * without calling abort this will be 0.
1675 *
1676 * If the journal has been aborted on this mount time -EROFS will
1677 * be returned.
1678 */
1679int journal_errno(journal_t *journal)
1680{
1681 int err;
1682
1683 spin_lock(&journal->j_state_lock);
1684 if (journal->j_flags & JFS_ABORT)
1685 err = -EROFS;
1686 else
1687 err = journal->j_errno;
1688 spin_unlock(&journal->j_state_lock);
1689 return err;
1690}
1691
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001692/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 * int journal_clear_err () - clears the journal's error state
1694 * @journal: journal to act on.
1695 *
1696 * An error must be cleared or Acked to take a FS out of readonly
1697 * mode.
1698 */
1699int journal_clear_err(journal_t *journal)
1700{
1701 int err = 0;
1702
1703 spin_lock(&journal->j_state_lock);
1704 if (journal->j_flags & JFS_ABORT)
1705 err = -EROFS;
1706 else
1707 journal->j_errno = 0;
1708 spin_unlock(&journal->j_state_lock);
1709 return err;
1710}
1711
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001712/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * void journal_ack_err() - Ack journal err.
1714 * @journal: journal to act on.
1715 *
1716 * An error must be cleared or Acked to take a FS out of readonly
1717 * mode.
1718 */
1719void journal_ack_err(journal_t *journal)
1720{
1721 spin_lock(&journal->j_state_lock);
1722 if (journal->j_errno)
1723 journal->j_flags |= JFS_ACK_ERR;
1724 spin_unlock(&journal->j_state_lock);
1725}
1726
1727int journal_blocks_per_page(struct inode *inode)
1728{
1729 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1730}
1731
1732/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * Journal_head storage management
1734 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001735static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736#ifdef CONFIG_JBD_DEBUG
1737static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1738#endif
1739
1740static int journal_init_journal_head_cache(void)
1741{
1742 int retval;
1743
Al Viro1076d172008-03-29 03:07:18 +00001744 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 journal_head_cache = kmem_cache_create("journal_head",
1746 sizeof(struct journal_head),
1747 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001748 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001749 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001751 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 retval = -ENOMEM;
1753 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1754 }
1755 return retval;
1756}
1757
1758static void journal_destroy_journal_head_cache(void)
1759{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001760 if (journal_head_cache) {
1761 kmem_cache_destroy(journal_head_cache);
1762 journal_head_cache = NULL;
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * journal_head splicing and dicing
1768 */
1769static struct journal_head *journal_alloc_journal_head(void)
1770{
1771 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773#ifdef CONFIG_JBD_DEBUG
1774 atomic_inc(&nr_journal_heads);
1775#endif
1776 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001777 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001779 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1780 __func__);
1781
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001782 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 yield();
1784 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1785 }
1786 }
1787 return ret;
1788}
1789
1790static void journal_free_journal_head(struct journal_head *jh)
1791{
1792#ifdef CONFIG_JBD_DEBUG
1793 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001794 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#endif
1796 kmem_cache_free(journal_head_cache, jh);
1797}
1798
1799/*
1800 * A journal_head is attached to a buffer_head whenever JBD has an
1801 * interest in the buffer.
1802 *
1803 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1804 * is set. This bit is tested in core kernel code where we need to take
1805 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1806 * there.
1807 *
1808 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1809 *
1810 * When a buffer has its BH_JBD bit set it is immune from being released by
1811 * core kernel code, mainly via ->b_count.
1812 *
Jan Karabb189242011-06-24 23:11:59 +02001813 * A journal_head is detached from its buffer_head when the journal_head's
1814 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1815 * transaction (b_cp_transaction) hold their references to b_jcount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
1817 * Various places in the kernel want to attach a journal_head to a buffer_head
1818 * _before_ attaching the journal_head to a transaction. To protect the
1819 * journal_head in this situation, journal_add_journal_head elevates the
1820 * journal_head's b_jcount refcount by one. The caller must call
1821 * journal_put_journal_head() to undo this.
1822 *
1823 * So the typical usage would be:
1824 *
1825 * (Attach a journal_head if needed. Increments b_jcount)
1826 * struct journal_head *jh = journal_add_journal_head(bh);
1827 * ...
Jan Karabb189242011-06-24 23:11:59 +02001828 * (Get another reference for transaction)
1829 * journal_grab_journal_head(bh);
1830 * jh->b_transaction = xxx;
1831 * (Put original reference)
1832 * journal_put_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
1834
1835/*
1836 * Give a buffer_head a journal_head.
1837 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * May sleep.
1839 */
1840struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1841{
1842 struct journal_head *jh;
1843 struct journal_head *new_jh = NULL;
1844
1845repeat:
1846 if (!buffer_jbd(bh)) {
1847 new_jh = journal_alloc_journal_head();
1848 memset(new_jh, 0, sizeof(*new_jh));
1849 }
1850
1851 jbd_lock_bh_journal_head(bh);
1852 if (buffer_jbd(bh)) {
1853 jh = bh2jh(bh);
1854 } else {
1855 J_ASSERT_BH(bh,
1856 (atomic_read(&bh->b_count) > 0) ||
1857 (bh->b_page && bh->b_page->mapping));
1858
1859 if (!new_jh) {
1860 jbd_unlock_bh_journal_head(bh);
1861 goto repeat;
1862 }
1863
1864 jh = new_jh;
1865 new_jh = NULL; /* We consumed it */
1866 set_buffer_jbd(bh);
1867 bh->b_private = jh;
1868 jh->b_bh = bh;
1869 get_bh(bh);
1870 BUFFER_TRACE(bh, "added journal_head");
1871 }
1872 jh->b_jcount++;
1873 jbd_unlock_bh_journal_head(bh);
1874 if (new_jh)
1875 journal_free_journal_head(new_jh);
1876 return bh->b_private;
1877}
1878
1879/*
1880 * Grab a ref against this buffer_head's journal_head. If it ended up not
1881 * having a journal_head, return NULL
1882 */
1883struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1884{
1885 struct journal_head *jh = NULL;
1886
1887 jbd_lock_bh_journal_head(bh);
1888 if (buffer_jbd(bh)) {
1889 jh = bh2jh(bh);
1890 jh->b_jcount++;
1891 }
1892 jbd_unlock_bh_journal_head(bh);
1893 return jh;
1894}
1895
1896static void __journal_remove_journal_head(struct buffer_head *bh)
1897{
1898 struct journal_head *jh = bh2jh(bh);
1899
1900 J_ASSERT_JH(jh, jh->b_jcount >= 0);
Jan Karabb189242011-06-24 23:11:59 +02001901 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1902 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1903 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1904 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1905 J_ASSERT_BH(bh, buffer_jbd(bh));
1906 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1907 BUFFER_TRACE(bh, "remove journal_head");
1908 if (jh->b_frozen_data) {
1909 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1910 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Jan Karabb189242011-06-24 23:11:59 +02001912 if (jh->b_committed_data) {
1913 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1914 jbd_free(jh->b_committed_data, bh->b_size);
1915 }
1916 bh->b_private = NULL;
1917 jh->b_bh = NULL; /* debug, really */
1918 clear_buffer_jbd(bh);
1919 journal_free_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922/*
Jan Karabb189242011-06-24 23:11:59 +02001923 * Drop a reference on the passed journal_head. If it fell to zero then
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * release the journal_head from the buffer_head.
1925 */
1926void journal_put_journal_head(struct journal_head *jh)
1927{
1928 struct buffer_head *bh = jh2bh(jh);
1929
1930 jbd_lock_bh_journal_head(bh);
1931 J_ASSERT_JH(jh, jh->b_jcount > 0);
1932 --jh->b_jcount;
Jan Karabb189242011-06-24 23:11:59 +02001933 if (!jh->b_jcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 __journal_remove_journal_head(bh);
Jan Karabb189242011-06-24 23:11:59 +02001935 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 __brelse(bh);
Jan Karabb189242011-06-24 23:11:59 +02001937 } else
1938 jbd_unlock_bh_journal_head(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07001942 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07001944#ifdef CONFIG_JBD_DEBUG
1945
1946u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jose R. Santosc2a91592007-10-18 23:39:22 -07001949static struct dentry *jbd_debugfs_dir;
1950static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jose R. Santosc2a91592007-10-18 23:39:22 -07001952static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001954 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1955 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08001956 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07001957 jbd_debugfs_dir,
1958 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Jose R. Santosc2a91592007-10-18 23:39:22 -07001961static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001963 debugfs_remove(jbd_debug);
1964 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967#else
1968
Jose R. Santosc2a91592007-10-18 23:39:22 -07001969static inline void jbd_create_debugfs_entry(void)
1970{
1971}
1972
1973static inline void jbd_remove_debugfs_entry(void)
1974{
1975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977#endif
1978
Christoph Lametere18b8902006-12-06 20:33:20 -08001979struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981static int __init journal_init_handle_cache(void)
1982{
1983 jbd_handle_cache = kmem_cache_create("journal_handle",
1984 sizeof(handle_t),
1985 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001986 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001987 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (jbd_handle_cache == NULL) {
1989 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1990 return -ENOMEM;
1991 }
1992 return 0;
1993}
1994
1995static void journal_destroy_handle_cache(void)
1996{
1997 if (jbd_handle_cache)
1998 kmem_cache_destroy(jbd_handle_cache);
1999}
2000
2001/*
2002 * Module startup and shutdown
2003 */
2004
2005static int __init journal_init_caches(void)
2006{
2007 int ret;
2008
2009 ret = journal_init_revoke_caches();
2010 if (ret == 0)
2011 ret = journal_init_journal_head_cache();
2012 if (ret == 0)
2013 ret = journal_init_handle_cache();
2014 return ret;
2015}
2016
2017static void journal_destroy_caches(void)
2018{
2019 journal_destroy_revoke_caches();
2020 journal_destroy_journal_head_cache();
2021 journal_destroy_handle_cache();
2022}
2023
2024static int __init journal_init(void)
2025{
2026 int ret;
2027
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002028 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 ret = journal_init_caches();
2031 if (ret != 0)
2032 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002033 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return ret;
2035}
2036
2037static void __exit journal_exit(void)
2038{
2039#ifdef CONFIG_JBD_DEBUG
2040 int n = atomic_read(&nr_journal_heads);
2041 if (n)
2042 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2043#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002044 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 journal_destroy_caches();
2046}
2047
2048MODULE_LICENSE("GPL");
2049module_init(journal_init);
2050module_exit(journal_exit);
2051