blob: e2d4285fbe90ebcc511a96574283418bc933634d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(journal_start);
45EXPORT_SYMBOL(journal_restart);
46EXPORT_SYMBOL(journal_extend);
47EXPORT_SYMBOL(journal_stop);
48EXPORT_SYMBOL(journal_lock_updates);
49EXPORT_SYMBOL(journal_unlock_updates);
50EXPORT_SYMBOL(journal_get_write_access);
51EXPORT_SYMBOL(journal_get_create_access);
52EXPORT_SYMBOL(journal_get_undo_access);
53EXPORT_SYMBOL(journal_dirty_data);
54EXPORT_SYMBOL(journal_dirty_metadata);
55EXPORT_SYMBOL(journal_release_buffer);
56EXPORT_SYMBOL(journal_forget);
57#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
60EXPORT_SYMBOL(journal_flush);
61EXPORT_SYMBOL(journal_revoke);
62
63EXPORT_SYMBOL(journal_init_dev);
64EXPORT_SYMBOL(journal_init_inode);
65EXPORT_SYMBOL(journal_update_format);
66EXPORT_SYMBOL(journal_check_used_features);
67EXPORT_SYMBOL(journal_check_available_features);
68EXPORT_SYMBOL(journal_set_features);
69EXPORT_SYMBOL(journal_create);
70EXPORT_SYMBOL(journal_load);
71EXPORT_SYMBOL(journal_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072EXPORT_SYMBOL(journal_abort);
73EXPORT_SYMBOL(journal_errno);
74EXPORT_SYMBOL(journal_ack_err);
75EXPORT_SYMBOL(journal_clear_err);
76EXPORT_SYMBOL(log_wait_commit);
Stefan Schmidtff5e4b52009-11-12 09:53:50 +010077EXPORT_SYMBOL(log_start_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078EXPORT_SYMBOL(journal_start_commit);
79EXPORT_SYMBOL(journal_force_commit_nested);
80EXPORT_SYMBOL(journal_wipe);
81EXPORT_SYMBOL(journal_blocks_per_page);
82EXPORT_SYMBOL(journal_invalidatepage);
83EXPORT_SYMBOL(journal_try_to_free_buffers);
84EXPORT_SYMBOL(journal_force_commit);
85
86static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
Adrian Bunk022a4a72005-09-06 15:16:41 -070087static void __journal_abort_soft (journal_t *journal, int errno);
Darrick J. Wongdff68252010-10-04 12:35:05 -070088static const char *journal_dev_name(journal_t *journal, char *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Helper function used to manage commit timeouts
92 */
93
94static void commit_timeout(unsigned long __data)
95{
96 struct task_struct * p = (struct task_struct *) __data;
97
98 wake_up_process(p);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * kjournald: The main thread function used to manage a logging device
103 * journal.
104 *
105 * This kernel thread is responsible for two things:
106 *
107 * 1) COMMIT: Every so often we need to commit the current state of the
108 * filesystem to disk. The journal thread is responsible for writing
109 * all of the metadata buffers to disk.
110 *
111 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
112 * of the data in that part of the log has been rewritten elsewhere on
113 * the disk. Flushing these old buffers to reclaim space in the log is
114 * known as checkpointing, and this thread is responsible for that job.
115 */
116
Adrian Bunk022a4a72005-09-06 15:16:41 -0700117static int kjournald(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Andrew Mortone3df1892006-03-25 03:06:53 -0800119 journal_t *journal = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 transaction_t *transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Andrew Mortone3df1892006-03-25 03:06:53 -0800122 /*
123 * Set up an interval timer which can be used to trigger a commit wakeup
124 * after the commit interval expires
125 */
126 setup_timer(&journal->j_commit_timer, commit_timeout,
127 (unsigned long)current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Record that the journal thread is running */
130 journal->j_task = current;
131 wake_up(&journal->j_wait_done_commit);
132
133 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
134 journal->j_commit_interval / HZ);
135
136 /*
137 * And now, wait forever for commit wakeup events.
138 */
139 spin_lock(&journal->j_state_lock);
140
141loop:
142 if (journal->j_flags & JFS_UNMOUNT)
143 goto end_loop;
144
145 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
146 journal->j_commit_sequence, journal->j_commit_request);
147
148 if (journal->j_commit_sequence != journal->j_commit_request) {
149 jbd_debug(1, "OK, requests differ\n");
150 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800151 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 journal_commit_transaction(journal);
153 spin_lock(&journal->j_state_lock);
154 goto loop;
155 }
156
157 wake_up(&journal->j_wait_done_commit);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700158 if (freezing(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * The simpler the better. Flushing journal isn't a
161 * good idea, because that depends on threads that may
162 * be already stopped.
163 */
164 jbd_debug(1, "Now suspending kjournald\n");
165 spin_unlock(&journal->j_state_lock);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700166 refrigerator();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 spin_lock(&journal->j_state_lock);
168 } else {
169 /*
170 * We assume on resume that commits are already there,
171 * so we don't sleep
172 */
173 DEFINE_WAIT(wait);
174 int should_sleep = 1;
175
176 prepare_to_wait(&journal->j_wait_commit, &wait,
177 TASK_INTERRUPTIBLE);
178 if (journal->j_commit_sequence != journal->j_commit_request)
179 should_sleep = 0;
180 transaction = journal->j_running_transaction;
181 if (transaction && time_after_eq(jiffies,
182 transaction->t_expires))
183 should_sleep = 0;
Mark Fashehcbf0d272005-09-06 15:19:08 -0700184 if (journal->j_flags & JFS_UNMOUNT)
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700185 should_sleep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (should_sleep) {
187 spin_unlock(&journal->j_state_lock);
188 schedule();
189 spin_lock(&journal->j_state_lock);
190 }
191 finish_wait(&journal->j_wait_commit, &wait);
192 }
193
194 jbd_debug(1, "kjournald wakes\n");
195
196 /*
197 * Were we woken up by a commit wakeup event?
198 */
199 transaction = journal->j_running_transaction;
200 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
201 journal->j_commit_request = transaction->t_tid;
202 jbd_debug(1, "woke because of timeout\n");
203 }
204 goto loop;
205
206end_loop:
207 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800208 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 journal->j_task = NULL;
210 wake_up(&journal->j_wait_done_commit);
211 jbd_debug(1, "Journal thread exiting.\n");
212 return 0;
213}
214
Pavel Emelianov97f06782007-05-08 00:30:42 -0700215static int journal_start_thread(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700217 struct task_struct *t;
218
219 t = kthread_run(kjournald, journal, "kjournald");
220 if (IS_ERR(t))
221 return PTR_ERR(t);
222
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700223 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static void journal_kill_thread(journal_t *journal)
228{
229 spin_lock(&journal->j_state_lock);
230 journal->j_flags |= JFS_UNMOUNT;
231
232 while (journal->j_task) {
233 wake_up(&journal->j_wait_commit);
234 spin_unlock(&journal->j_state_lock);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700235 wait_event(journal->j_wait_done_commit,
236 journal->j_task == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_lock(&journal->j_state_lock);
238 }
239 spin_unlock(&journal->j_state_lock);
240}
241
242/*
243 * journal_write_metadata_buffer: write a metadata buffer to the journal.
244 *
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
248 *
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
257 * during recovery.
258 *
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
264 * progress.
265 *
266 * The function returns a pointer to the buffer_heads to be used for IO.
267 *
268 * We assume that the journal has already been locked in this function.
269 *
270 * Return value:
271 * <0: Error
272 * >=0: Finished OK
273 *
274 * On success:
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
277 */
278
279int journal_write_metadata_buffer(transaction_t *transaction,
280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
Jan Kara9c28cbc2009-08-03 19:21:00 +0200282 unsigned int blocknr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 int need_copy_out = 0;
285 int done_copy_out = 0;
286 int do_escape = 0;
287 char *mapped_data;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghuaf1015c42009-07-15 21:42:05 +0200293 journal_t *journal = transaction->t_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /*
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
298 * to do any IO.
299 *
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
303 */
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghuaf1015c42009-07-15 21:42:05 +0200307 /* keep subsequent assertions sane */
308 new_bh->b_state = 0;
309 init_buffer(new_bh, NULL, NULL);
310 atomic_set(&new_bh->b_count, 1);
311 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /*
314 * If a new transaction has already done a buffer copy-out, then
315 * we use that version of the data for the commit.
316 */
317 jbd_lock_bh_state(bh_in);
318repeat:
319 if (jh_in->b_frozen_data) {
320 done_copy_out = 1;
321 new_page = virt_to_page(jh_in->b_frozen_data);
322 new_offset = offset_in_page(jh_in->b_frozen_data);
323 } else {
324 new_page = jh2bh(jh_in)->b_page;
325 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
326 }
327
328 mapped_data = kmap_atomic(new_page, KM_USER0);
329 /*
330 * Check for escaping
331 */
332 if (*((__be32 *)(mapped_data + new_offset)) ==
333 cpu_to_be32(JFS_MAGIC_NUMBER)) {
334 need_copy_out = 1;
335 do_escape = 1;
336 }
337 kunmap_atomic(mapped_data, KM_USER0);
338
339 /*
340 * Do we need to do a data copy?
341 */
342 if (need_copy_out && !done_copy_out) {
343 char *tmp;
344
345 jbd_unlock_bh_state(bh_in);
Mingming Caoc089d492007-10-16 18:38:25 -0400346 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 jbd_lock_bh_state(bh_in);
348 if (jh_in->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400349 jbd_free(tmp, bh_in->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto repeat;
351 }
352
353 jh_in->b_frozen_data = tmp;
354 mapped_data = kmap_atomic(new_page, KM_USER0);
355 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
356 kunmap_atomic(mapped_data, KM_USER0);
357
358 new_page = virt_to_page(tmp);
359 new_offset = offset_in_page(tmp);
360 done_copy_out = 1;
361 }
362
363 /*
364 * Did we need to do an escaping? Now we've done all the
365 * copying, we can finally do so.
366 */
367 if (do_escape) {
368 mapped_data = kmap_atomic(new_page, KM_USER0);
369 *((unsigned int *)(mapped_data + new_offset)) = 0;
370 kunmap_atomic(mapped_data, KM_USER0);
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 set_bh_page(new_bh, new_page, new_offset);
374 new_jh->b_transaction = NULL;
375 new_bh->b_size = jh2bh(jh_in)->b_size;
376 new_bh->b_bdev = transaction->t_journal->j_dev;
377 new_bh->b_blocknr = blocknr;
378 set_buffer_mapped(new_bh);
379 set_buffer_dirty(new_bh);
380
381 *jh_out = new_jh;
382
383 /*
384 * The to-be-written buffer needs to get moved to the io queue,
385 * and the original buffer whose contents we are shadowing or
386 * copying is moved to the transaction's shadow queue.
387 */
388 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghuaf1015c42009-07-15 21:42:05 +0200389 spin_lock(&journal->j_list_lock);
390 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
391 spin_unlock(&journal->j_list_lock);
392 jbd_unlock_bh_state(bh_in);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 JBUFFER_TRACE(new_jh, "file as BJ_IO");
395 journal_file_buffer(new_jh, transaction, BJ_IO);
396
397 return do_escape | (done_copy_out << 1);
398}
399
400/*
401 * Allocation code for the journal file. Manage the space left in the
402 * journal, so that we can begin checkpointing when appropriate.
403 */
404
405/*
406 * __log_space_left: Return the number of free blocks left in the journal.
407 *
408 * Called with the journal already locked.
409 *
410 * Called under j_state_lock
411 */
412
413int __log_space_left(journal_t *journal)
414{
415 int left = journal->j_free;
416
417 assert_spin_locked(&journal->j_state_lock);
418
419 /*
420 * Be pessimistic here about the number of those free blocks which
421 * might be required for log descriptor control blocks.
422 */
423
424#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
425
426 left -= MIN_LOG_RESERVED_BLOCKS;
427
428 if (left <= 0)
429 return 0;
430 left -= (left >> 3);
431 return left;
432}
433
434/*
Jan Kara8fe4cd02009-02-11 13:04:25 -0800435 * Called under j_state_lock. Returns true if a transaction commit was started.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437int __log_start_commit(journal_t *journal, tid_t target)
438{
439 /*
Ted Ts'od9b01932011-04-30 13:17:11 -0400440 * The only transaction we can possibly wait upon is the
441 * currently running transaction (if it exists). Otherwise,
442 * the target tid must be an old one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
Ted Ts'od9b01932011-04-30 13:17:11 -0400444 if (journal->j_running_transaction &&
445 journal->j_running_transaction->t_tid == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
Andrea Gelminibcf3d0b2010-10-16 15:19:14 +0200447 * We want a new commit: OK, mark the request and wakeup the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * commit thread. We do _not_ do the commit ourselves.
449 */
450
451 journal->j_commit_request = target;
452 jbd_debug(1, "JBD: requesting commit %d/%d\n",
453 journal->j_commit_request,
454 journal->j_commit_sequence);
455 wake_up(&journal->j_wait_commit);
456 return 1;
Ted Ts'od9b01932011-04-30 13:17:11 -0400457 } else if (!tid_geq(journal->j_commit_request, target))
458 /* This should never happen, but if it does, preserve
459 the evidence before kjournald goes into a loop and
460 increments j_commit_sequence beyond all recognition. */
461 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
462 journal->j_commit_request, journal->j_commit_sequence,
463 target, journal->j_running_transaction ?
464 journal->j_running_transaction->t_tid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
468int log_start_commit(journal_t *journal, tid_t tid)
469{
470 int ret;
471
472 spin_lock(&journal->j_state_lock);
473 ret = __log_start_commit(journal, tid);
474 spin_unlock(&journal->j_state_lock);
475 return ret;
476}
477
478/*
479 * Force and wait upon a commit if the calling process is not within
480 * transaction. This is used for forcing out undo-protected data which contains
481 * bitmaps, when the fs is running out of space.
482 *
483 * We can only force the running transaction if we don't have an active handle;
484 * otherwise, we will deadlock.
485 *
486 * Returns true if a transaction was started.
487 */
488int journal_force_commit_nested(journal_t *journal)
489{
490 transaction_t *transaction = NULL;
491 tid_t tid;
492
493 spin_lock(&journal->j_state_lock);
494 if (journal->j_running_transaction && !current->journal_info) {
495 transaction = journal->j_running_transaction;
496 __log_start_commit(journal, transaction->t_tid);
497 } else if (journal->j_committing_transaction)
498 transaction = journal->j_committing_transaction;
499
500 if (!transaction) {
501 spin_unlock(&journal->j_state_lock);
502 return 0; /* Nothing to retry */
503 }
504
505 tid = transaction->t_tid;
506 spin_unlock(&journal->j_state_lock);
507 log_wait_commit(journal, tid);
508 return 1;
509}
510
511/*
512 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800513 * if a transaction is going to be committed (or is currently already
514 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
516int journal_start_commit(journal_t *journal, tid_t *ptid)
517{
518 int ret = 0;
519
520 spin_lock(&journal->j_state_lock);
521 if (journal->j_running_transaction) {
522 tid_t tid = journal->j_running_transaction->t_tid;
523
Jan Kara8fe4cd02009-02-11 13:04:25 -0800524 __log_start_commit(journal, tid);
525 /* There's a running transaction and we've just made sure
526 * it's commit has been scheduled. */
527 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800529 ret = 1;
530 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * If ext3_write_super() recently started a commit, then we
533 * have to wait for completion of that transaction
534 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800535 if (ptid)
536 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ret = 1;
538 }
539 spin_unlock(&journal->j_state_lock);
540 return ret;
541}
542
543/*
544 * Wait for a specified commit to complete.
545 * The caller may not hold the journal lock.
546 */
547int log_wait_commit(journal_t *journal, tid_t tid)
548{
549 int err = 0;
550
551#ifdef CONFIG_JBD_DEBUG
552 spin_lock(&journal->j_state_lock);
553 if (!tid_geq(journal->j_commit_request, tid)) {
554 printk(KERN_EMERG
555 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700556 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 spin_unlock(&journal->j_state_lock);
559#endif
560 spin_lock(&journal->j_state_lock);
561 while (tid_gt(tid, journal->j_commit_sequence)) {
562 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
563 tid, journal->j_commit_sequence);
564 wake_up(&journal->j_wait_commit);
565 spin_unlock(&journal->j_state_lock);
566 wait_event(journal->j_wait_done_commit,
567 !tid_gt(tid, journal->j_commit_sequence));
568 spin_lock(&journal->j_state_lock);
569 }
570 spin_unlock(&journal->j_state_lock);
571
572 if (unlikely(is_journal_aborted(journal))) {
573 printk(KERN_EMERG "journal commit I/O error\n");
574 err = -EIO;
575 }
576 return err;
577}
578
579/*
Jan Kara03f4d802010-04-15 22:16:24 +0200580 * Return 1 if a given transaction has not yet sent barrier request
581 * connected with a transaction commit. If 0 is returned, transaction
582 * may or may not have sent the barrier. Used to avoid sending barrier
583 * twice in common cases.
584 */
585int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
586{
587 int ret = 0;
588 transaction_t *commit_trans;
589
590 if (!(journal->j_flags & JFS_BARRIER))
591 return 0;
592 spin_lock(&journal->j_state_lock);
593 /* Transaction already committed? */
594 if (tid_geq(journal->j_commit_sequence, tid))
595 goto out;
596 /*
597 * Transaction is being committed and we already proceeded to
598 * writing commit record?
599 */
600 commit_trans = journal->j_committing_transaction;
601 if (commit_trans && commit_trans->t_tid == tid &&
602 commit_trans->t_state >= T_COMMIT_RECORD)
603 goto out;
604 ret = 1;
605out:
606 spin_unlock(&journal->j_state_lock);
607 return ret;
608}
Jan Kara52779702010-04-15 22:24:26 +0200609EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200610
611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * Log buffer allocation routines:
613 */
614
Jan Kara9c28cbc2009-08-03 19:21:00 +0200615int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200617 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 spin_lock(&journal->j_state_lock);
620 J_ASSERT(journal->j_free > 1);
621
622 blocknr = journal->j_head;
623 journal->j_head++;
624 journal->j_free--;
625 if (journal->j_head == journal->j_last)
626 journal->j_head = journal->j_first;
627 spin_unlock(&journal->j_state_lock);
628 return journal_bmap(journal, blocknr, retp);
629}
630
631/*
632 * Conversion of logical to physical block numbers for the journal
633 *
634 * On external journals the journal blocks are identity-mapped, so
635 * this is a no-op. If needed, we can use j_blk_offset - everything is
636 * ready.
637 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200638int journal_bmap(journal_t *journal, unsigned int blocknr,
639 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200642 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (journal->j_inode) {
645 ret = bmap(journal->j_inode, blocknr);
646 if (ret)
647 *retp = ret;
648 else {
649 char b[BDEVNAME_SIZE];
650
651 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200652 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700653 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 blocknr,
655 bdevname(journal->j_dev, b));
656 err = -EIO;
657 __journal_abort_soft(journal, err);
658 }
659 } else {
660 *retp = blocknr; /* +journal->j_blk_offset */
661 }
662 return err;
663}
664
665/*
666 * We play buffer_head aliasing tricks to write data/metadata blocks to
667 * the journal without copying their contents, but for journal
668 * descriptor blocks we do need to generate bona fide buffers.
669 *
670 * After the caller of journal_get_descriptor_buffer() has finished modifying
671 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
672 * But we don't bother doing that, so there will be coherency problems with
673 * mmaps of blockdevs which hold live JBD-controlled filesystems.
674 */
675struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
676{
677 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200678 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int err;
680
681 err = journal_next_log_block(journal, &blocknr);
682
683 if (err)
684 return NULL;
685
686 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700687 if (!bh)
688 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 lock_buffer(bh);
690 memset(bh->b_data, 0, journal->j_blocksize);
691 set_buffer_uptodate(bh);
692 unlock_buffer(bh);
693 BUFFER_TRACE(bh, "return this buffer");
694 return journal_add_journal_head(bh);
695}
696
697/*
698 * Management for journal control blocks: functions to create and
699 * destroy journal_t structures, and to initialise and read existing
700 * journal blocks from disk. */
701
702/* First: create and setup a journal_t object in memory. We initialise
703 * very few fields yet: that has to wait until we have created the
704 * journal structures from from scratch, or loaded them from disk. */
705
706static journal_t * journal_init_common (void)
707{
708 journal_t *journal;
709 int err;
710
Mingming Cao8c3478a2007-10-18 23:39:20 -0700711 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!journal)
713 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 init_waitqueue_head(&journal->j_wait_transaction_locked);
716 init_waitqueue_head(&journal->j_wait_logspace);
717 init_waitqueue_head(&journal->j_wait_done_commit);
718 init_waitqueue_head(&journal->j_wait_checkpoint);
719 init_waitqueue_head(&journal->j_wait_commit);
720 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800721 mutex_init(&journal->j_barrier);
722 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 spin_lock_init(&journal->j_revoke_lock);
724 spin_lock_init(&journal->j_list_lock);
725 spin_lock_init(&journal->j_state_lock);
726
727 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
728
729 /* The journal is marked for error until we succeed with recovery! */
730 journal->j_flags = JFS_ABORT;
731
732 /* Set up a default-sized revoke table for the new mount. */
733 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
734 if (err) {
735 kfree(journal);
736 goto fail;
737 }
738 return journal;
739fail:
740 return NULL;
741}
742
743/* journal_init_dev and journal_init_inode:
744 *
745 * Create a journal structure assigned some fixed set of disk blocks to
746 * the journal. We don't actually touch those disk blocks yet, but we
747 * need to set up all of the mapping information to tell the journaling
748 * system where the journal blocks are.
749 *
750 */
751
752/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700753 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * @bdev: Block device on which to create the journal
755 * @fs_dev: Device which hold journalled filesystem for this journal.
756 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700757 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700759 *
760 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * journal_init_dev creates a journal which maps a fixed contiguous
763 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
766journal_t * journal_init_dev(struct block_device *bdev,
767 struct block_device *fs_dev,
768 int start, int len, int blocksize)
769{
770 journal_t *journal = journal_init_common();
771 struct buffer_head *bh;
772 int n;
773
774 if (!journal)
775 return NULL;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700778 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 n = journal->j_blocksize / sizeof(journal_block_tag_t);
780 journal->j_wbufsize = n;
781 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
782 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300783 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700784 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700785 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700787 journal->j_dev = bdev;
788 journal->j_fs_dev = fs_dev;
789 journal->j_blk_offset = start;
790 journal->j_maxlen = len;
791
792 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700793 if (!bh) {
794 printk(KERN_ERR
795 "%s: Cannot get buffer for journal superblock\n",
796 __func__);
797 goto out_err;
798 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700799 journal->j_sb_buffer = bh;
800 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700803out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800804 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700805 kfree(journal);
806 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700808
809/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
811 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700812 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * journal_init_inode creates a journal which maps an on-disk inode as
814 * the journal. The inode must exist already, must support bmap() and
815 * must have all data blocks preallocated.
816 */
817journal_t * journal_init_inode (struct inode *inode)
818{
819 struct buffer_head *bh;
820 journal_t *journal = journal_init_common();
821 int err;
822 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200823 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (!journal)
826 return NULL;
827
828 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
829 journal->j_inode = inode;
830 jbd_debug(1,
831 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700832 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 (long long) inode->i_size,
834 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
835
836 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
837 journal->j_blocksize = inode->i_sb->s_blocksize;
838
839 /* journal descriptor can store up to n blocks -bzzz */
840 n = journal->j_blocksize / sizeof(journal_block_tag_t);
841 journal->j_wbufsize = n;
842 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
843 if (!journal->j_wbuf) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300844 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700845 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700846 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 err = journal_bmap(journal, 0, &blocknr);
850 /* If that failed, give up */
851 if (err) {
Justin P. Mattock3c26bdb2011-02-26 20:34:05 -0800852 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700853 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700854 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700858 if (!bh) {
859 printk(KERN_ERR
860 "%s: Cannot get buffer for journal superblock\n",
861 __func__);
862 goto out_err;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 journal->j_sb_buffer = bh;
865 journal->j_superblock = (journal_superblock_t *)bh->b_data;
866
867 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700868out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800869 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700870 kfree(journal);
871 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700874/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * If the journal init or create aborts, we need to mark the journal
876 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700877 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
879static void journal_fail_superblock (journal_t *journal)
880{
881 struct buffer_head *bh = journal->j_sb_buffer;
882 brelse(bh);
883 journal->j_sb_buffer = NULL;
884}
885
886/*
887 * Given a journal_t structure, initialise the various fields for
888 * startup of a new journaling session. We use this both when creating
889 * a journal, and after recovering an old journal to reset it for
890 * subsequent use.
891 */
892
893static int journal_reset(journal_t *journal)
894{
895 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200896 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 first = be32_to_cpu(sb->s_first);
899 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200900 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200901 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200902 first, last);
903 journal_fail_superblock(journal);
904 return -EINVAL;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 journal->j_first = first;
908 journal->j_last = last;
909
910 journal->j_head = first;
911 journal->j_tail = first;
912 journal->j_free = last - first;
913
914 journal->j_tail_sequence = journal->j_transaction_sequence;
915 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
916 journal->j_commit_request = journal->j_commit_sequence;
917
918 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
919
920 /* Add the dynamic fields and write it to disk. */
921 journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700922 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700925/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * int journal_create() - Initialise the new journal file
927 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700928 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * Given a journal_t structure which tells us which disk blocks we can
930 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700931 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 **/
933int journal_create(journal_t *journal)
934{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200935 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct buffer_head *bh;
937 journal_superblock_t *sb;
938 int i, err;
939
940 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
941 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
942 journal->j_maxlen);
943 journal_fail_superblock(journal);
944 return -EINVAL;
945 }
946
947 if (journal->j_inode == NULL) {
948 /*
949 * We don't know what block to start at!
950 */
951 printk(KERN_EMERG
952 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700953 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 BUG();
955 }
956
957 /* Zero out the entire journal on disk. We cannot afford to
958 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
959 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
960 for (i = 0; i < journal->j_maxlen; i++) {
961 err = journal_bmap(journal, i, &blocknr);
962 if (err)
963 return err;
964 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Namhyung Kim2a0e3382010-10-13 17:17:18 +0900965 if (unlikely(!bh))
966 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 lock_buffer(bh);
968 memset (bh->b_data, 0, journal->j_blocksize);
969 BUFFER_TRACE(bh, "marking dirty");
970 mark_buffer_dirty(bh);
971 BUFFER_TRACE(bh, "marking uptodate");
972 set_buffer_uptodate(bh);
973 unlock_buffer(bh);
974 __brelse(bh);
975 }
976
977 sync_blockdev(journal->j_dev);
978 jbd_debug(1, "JBD: journal cleared.\n");
979
980 /* OK, fill in the initial static fields in the new superblock */
981 sb = journal->j_superblock;
982
983 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
984 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
985
986 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
987 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
988 sb->s_first = cpu_to_be32(1);
989
990 journal->j_transaction_sequence = 1;
991
992 journal->j_flags &= ~JFS_ABORT;
993 journal->j_format_version = 2;
994
995 return journal_reset(journal);
996}
997
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700998/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * void journal_update_superblock() - Update journal sb on disk.
1000 * @journal: The journal to update.
1001 * @wait: Set to '0' if you don't want to wait for IO completion.
1002 *
1003 * Update a journal's dynamic superblock fields and write it to disk,
1004 * optionally waiting for the IO to complete.
1005 */
1006void journal_update_superblock(journal_t *journal, int wait)
1007{
1008 journal_superblock_t *sb = journal->j_superblock;
1009 struct buffer_head *bh = journal->j_sb_buffer;
1010
1011 /*
1012 * As a special case, if the on-disk copy is already marked as needing
1013 * no recovery (s_start == 0) and there are no outstanding transactions
1014 * in the filesystem, then we can safely defer the superblock update
1015 * until the next commit by setting JFS_FLUSHED. This avoids
1016 * attempting a write to a potential-readonly device.
1017 */
1018 if (sb->s_start == 0 && journal->j_tail_sequence ==
1019 journal->j_transaction_sequence) {
1020 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
Jan Kara9c28cbc2009-08-03 19:21:00 +02001021 "(start %u, seq %d, errno %d)\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001022 journal->j_tail, journal->j_tail_sequence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 journal->j_errno);
1024 goto out;
1025 }
1026
Darrick J. Wongdff68252010-10-04 12:35:05 -07001027 if (buffer_write_io_error(bh)) {
1028 char b[BDEVNAME_SIZE];
1029 /*
1030 * Oh, dear. A previous attempt to write the journal
1031 * superblock failed. This could happen because the
1032 * USB device was yanked out. Or it could happen to
1033 * be a transient write error and maybe the block will
1034 * be remapped. Nothing we can do but to retry the
1035 * write and hope for the best.
1036 */
1037 printk(KERN_ERR "JBD: previous I/O error detected "
1038 "for journal superblock update for %s.\n",
1039 journal_dev_name(journal, b));
1040 clear_buffer_write_io_error(bh);
1041 set_buffer_uptodate(bh);
1042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 spin_lock(&journal->j_state_lock);
Jan Kara9c28cbc2009-08-03 19:21:00 +02001045 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1047
1048 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1049 sb->s_start = cpu_to_be32(journal->j_tail);
1050 sb->s_errno = cpu_to_be32(journal->j_errno);
1051 spin_unlock(&journal->j_state_lock);
1052
1053 BUFFER_TRACE(bh, "marking dirty");
1054 mark_buffer_dirty(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001055 if (wait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 sync_dirty_buffer(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001057 if (buffer_write_io_error(bh)) {
1058 char b[BDEVNAME_SIZE];
1059 printk(KERN_ERR "JBD: I/O error detected "
1060 "when updating journal superblock for %s.\n",
1061 journal_dev_name(journal, b));
1062 clear_buffer_write_io_error(bh);
1063 set_buffer_uptodate(bh);
1064 }
1065 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001066 write_dirty_buffer(bh, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068out:
1069 /* If we have just flushed the log (by marking s_start==0), then
1070 * any future commit will have to be careful to update the
1071 * superblock again to re-record the true start of the log. */
1072
1073 spin_lock(&journal->j_state_lock);
1074 if (sb->s_start)
1075 journal->j_flags &= ~JFS_FLUSHED;
1076 else
1077 journal->j_flags |= JFS_FLUSHED;
1078 spin_unlock(&journal->j_state_lock);
1079}
1080
1081/*
1082 * Read the superblock for a given journal, performing initial
1083 * validation of the format.
1084 */
1085
1086static int journal_get_superblock(journal_t *journal)
1087{
1088 struct buffer_head *bh;
1089 journal_superblock_t *sb;
1090 int err = -EIO;
1091
1092 bh = journal->j_sb_buffer;
1093
1094 J_ASSERT(bh != NULL);
1095 if (!buffer_uptodate(bh)) {
1096 ll_rw_block(READ, 1, &bh);
1097 wait_on_buffer(bh);
1098 if (!buffer_uptodate(bh)) {
1099 printk (KERN_ERR
1100 "JBD: IO error reading journal superblock\n");
1101 goto out;
1102 }
1103 }
1104
1105 sb = journal->j_superblock;
1106
1107 err = -EINVAL;
1108
1109 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1110 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1111 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1112 goto out;
1113 }
1114
1115 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1116 case JFS_SUPERBLOCK_V1:
1117 journal->j_format_version = 1;
1118 break;
1119 case JFS_SUPERBLOCK_V2:
1120 journal->j_format_version = 2;
1121 break;
1122 default:
1123 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1124 goto out;
1125 }
1126
1127 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1128 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1129 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1130 printk (KERN_WARNING "JBD: journal file too short\n");
1131 goto out;
1132 }
1133
1134 return 0;
1135
1136out:
1137 journal_fail_superblock(journal);
1138 return err;
1139}
1140
1141/*
1142 * Load the on-disk journal superblock and read the key fields into the
1143 * journal_t.
1144 */
1145
1146static int load_superblock(journal_t *journal)
1147{
1148 int err;
1149 journal_superblock_t *sb;
1150
1151 err = journal_get_superblock(journal);
1152 if (err)
1153 return err;
1154
1155 sb = journal->j_superblock;
1156
1157 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1158 journal->j_tail = be32_to_cpu(sb->s_start);
1159 journal->j_first = be32_to_cpu(sb->s_first);
1160 journal->j_last = be32_to_cpu(sb->s_maxlen);
1161 journal->j_errno = be32_to_cpu(sb->s_errno);
1162
1163 return 0;
1164}
1165
1166
1167/**
1168 * int journal_load() - Read journal from disk.
1169 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * Given a journal_t structure which tells us which disk blocks contain
1172 * a journal, read the journal from disk to initialise the in-memory
1173 * structures.
1174 */
1175int journal_load(journal_t *journal)
1176{
1177 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001178 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 err = load_superblock(journal);
1181 if (err)
1182 return err;
1183
Badari Pulavartyea817392006-08-27 01:23:52 -07001184 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* If this is a V2 superblock, then we have to check the
1186 * features flags on it. */
1187
1188 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if ((sb->s_feature_ro_compat &
1190 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1191 (sb->s_feature_incompat &
1192 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1193 printk (KERN_WARNING
1194 "JBD: Unrecognised features on journal\n");
1195 return -EINVAL;
1196 }
1197 }
1198
1199 /* Let the recovery code check whether it needs to recover any
1200 * data from the journal. */
1201 if (journal_recover(journal))
1202 goto recovery_error;
1203
1204 /* OK, we've finished with the dynamic journal bits:
1205 * reinitialise the dynamic contents of the superblock in memory
1206 * and reset them on disk. */
1207 if (journal_reset(journal))
1208 goto recovery_error;
1209
1210 journal->j_flags &= ~JFS_ABORT;
1211 journal->j_flags |= JFS_LOADED;
1212 return 0;
1213
1214recovery_error:
1215 printk (KERN_WARNING "JBD: recovery failed\n");
1216 return -EIO;
1217}
1218
1219/**
1220 * void journal_destroy() - Release a journal_t structure.
1221 * @journal: Journal to act on.
1222 *
1223 * Release a journal_t structure once it is no longer in use by the
1224 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001225 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001227int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001229 int err = 0;
1230
Jan Kara03f4d802010-04-15 22:16:24 +02001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* Wait for the commit thread to wake up and die. */
1233 journal_kill_thread(journal);
1234
1235 /* Force a final log commit */
1236 if (journal->j_running_transaction)
1237 journal_commit_transaction(journal);
1238
1239 /* Force any old transactions to disk */
1240
1241 /* Totally anal locking here... */
1242 spin_lock(&journal->j_list_lock);
1243 while (journal->j_checkpoint_transactions != NULL) {
1244 spin_unlock(&journal->j_list_lock);
1245 log_do_checkpoint(journal);
1246 spin_lock(&journal->j_list_lock);
1247 }
1248
1249 J_ASSERT(journal->j_running_transaction == NULL);
1250 J_ASSERT(journal->j_committing_transaction == NULL);
1251 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1252 spin_unlock(&journal->j_list_lock);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001255 if (!is_journal_aborted(journal)) {
1256 /* We can now mark the journal as empty. */
1257 journal->j_tail = 0;
1258 journal->j_tail_sequence =
1259 ++journal->j_transaction_sequence;
1260 journal_update_superblock(journal, 1);
1261 } else {
1262 err = -EIO;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 brelse(journal->j_sb_buffer);
1265 }
1266
1267 if (journal->j_inode)
1268 iput(journal->j_inode);
1269 if (journal->j_revoke)
1270 journal_destroy_revoke(journal);
1271 kfree(journal->j_wbuf);
1272 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001273
1274 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277
1278/**
1279 *int journal_check_used_features () - Check if features specified are used.
1280 * @journal: Journal to check.
1281 * @compat: bitmask of compatible features
1282 * @ro: bitmask of features that force read-only mount
1283 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001286 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 **/
1288
1289int journal_check_used_features (journal_t *journal, unsigned long compat,
1290 unsigned long ro, unsigned long incompat)
1291{
1292 journal_superblock_t *sb;
1293
1294 if (!compat && !ro && !incompat)
1295 return 1;
1296 if (journal->j_format_version == 1)
1297 return 0;
1298
1299 sb = journal->j_superblock;
1300
1301 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1302 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1303 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1304 return 1;
1305
1306 return 0;
1307}
1308
1309/**
1310 * int journal_check_available_features() - Check feature set in journalling layer
1311 * @journal: Journal to check.
1312 * @compat: bitmask of compatible features
1313 * @ro: bitmask of features that force read-only mount
1314 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * Check whether the journaling code supports the use of
1317 * all of a given set of features on this journal. Return true
1318 * (non-zero) if it can. */
1319
1320int journal_check_available_features (journal_t *journal, unsigned long compat,
1321 unsigned long ro, unsigned long incompat)
1322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!compat && !ro && !incompat)
1324 return 1;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* We can support any known requested features iff the
1327 * superblock is in version 2. Otherwise we fail to support any
1328 * extended sb features. */
1329
1330 if (journal->j_format_version != 2)
1331 return 0;
1332
1333 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1334 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1335 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1336 return 1;
1337
1338 return 0;
1339}
1340
1341/**
1342 * int journal_set_features () - Mark a given journal feature in the superblock
1343 * @journal: Journal to act on.
1344 * @compat: bitmask of compatible features
1345 * @ro: bitmask of features that force read-only mount
1346 * @incompat: bitmask of incompatible features
1347 *
1348 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001349 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
1351 */
1352
1353int journal_set_features (journal_t *journal, unsigned long compat,
1354 unsigned long ro, unsigned long incompat)
1355{
1356 journal_superblock_t *sb;
1357
1358 if (journal_check_used_features(journal, compat, ro, incompat))
1359 return 1;
1360
1361 if (!journal_check_available_features(journal, compat, ro, incompat))
1362 return 0;
1363
1364 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1365 compat, ro, incompat);
1366
1367 sb = journal->j_superblock;
1368
1369 sb->s_feature_compat |= cpu_to_be32(compat);
1370 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1371 sb->s_feature_incompat |= cpu_to_be32(incompat);
1372
1373 return 1;
1374}
1375
1376
1377/**
1378 * int journal_update_format () - Update on-disk journal structure.
1379 * @journal: Journal to act on.
1380 *
1381 * Given an initialised but unloaded journal struct, poke about in the
1382 * on-disk structure to update it to the most recent supported version.
1383 */
1384int journal_update_format (journal_t *journal)
1385{
1386 journal_superblock_t *sb;
1387 int err;
1388
1389 err = journal_get_superblock(journal);
1390 if (err)
1391 return err;
1392
1393 sb = journal->j_superblock;
1394
1395 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1396 case JFS_SUPERBLOCK_V2:
1397 return 0;
1398 case JFS_SUPERBLOCK_V1:
1399 return journal_convert_superblock_v1(journal, sb);
1400 default:
1401 break;
1402 }
1403 return -EINVAL;
1404}
1405
1406static int journal_convert_superblock_v1(journal_t *journal,
1407 journal_superblock_t *sb)
1408{
1409 int offset, blocksize;
1410 struct buffer_head *bh;
1411
1412 printk(KERN_WARNING
1413 "JBD: Converting superblock from version 1 to 2.\n");
1414
1415 /* Pre-initialise new fields to zero */
1416 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1417 blocksize = be32_to_cpu(sb->s_blocksize);
1418 memset(&sb->s_feature_compat, 0, blocksize-offset);
1419
1420 sb->s_nr_users = cpu_to_be32(1);
1421 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1422 journal->j_format_version = 2;
1423
1424 bh = journal->j_sb_buffer;
1425 BUFFER_TRACE(bh, "marking dirty");
1426 mark_buffer_dirty(bh);
1427 sync_dirty_buffer(bh);
1428 return 0;
1429}
1430
1431
1432/**
1433 * int journal_flush () - Flush journal
1434 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001435 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * Flush all data for a given journal to disk and empty the journal.
1437 * Filesystems can use this when remounting readonly to ensure that
1438 * recovery does not need to happen on remount.
1439 */
1440
1441int journal_flush(journal_t *journal)
1442{
1443 int err = 0;
1444 transaction_t *transaction = NULL;
Jan Kara9c28cbc2009-08-03 19:21:00 +02001445 unsigned int old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 spin_lock(&journal->j_state_lock);
1448
1449 /* Force everything buffered to the log... */
1450 if (journal->j_running_transaction) {
1451 transaction = journal->j_running_transaction;
1452 __log_start_commit(journal, transaction->t_tid);
1453 } else if (journal->j_committing_transaction)
1454 transaction = journal->j_committing_transaction;
1455
1456 /* Wait for the log commit to complete... */
1457 if (transaction) {
1458 tid_t tid = transaction->t_tid;
1459
1460 spin_unlock(&journal->j_state_lock);
1461 log_wait_commit(journal, tid);
1462 } else {
1463 spin_unlock(&journal->j_state_lock);
1464 }
1465
1466 /* ...and flush everything in the log out to disk. */
1467 spin_lock(&journal->j_list_lock);
1468 while (!err && journal->j_checkpoint_transactions != NULL) {
1469 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001470 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001472 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 spin_lock(&journal->j_list_lock);
1474 }
1475 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001476
1477 if (is_journal_aborted(journal))
1478 return -EIO;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 cleanup_journal_tail(journal);
1481
1482 /* Finally, mark the journal as really needing no recovery.
1483 * This sets s_start==0 in the underlying superblock, which is
1484 * the magic code for a fully-recovered superblock. Any future
1485 * commits of data to the journal will restore the current
1486 * s_start value. */
1487 spin_lock(&journal->j_state_lock);
1488 old_tail = journal->j_tail;
1489 journal->j_tail = 0;
1490 spin_unlock(&journal->j_state_lock);
1491 journal_update_superblock(journal, 1);
1492 spin_lock(&journal->j_state_lock);
1493 journal->j_tail = old_tail;
1494
1495 J_ASSERT(!journal->j_running_transaction);
1496 J_ASSERT(!journal->j_committing_transaction);
1497 J_ASSERT(!journal->j_checkpoint_transactions);
1498 J_ASSERT(journal->j_head == journal->j_tail);
1499 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1500 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504/**
1505 * int journal_wipe() - Wipe journal contents
1506 * @journal: Journal to act on.
1507 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * Wipe out all of the contents of a journal, safely. This will produce
1510 * a warning if the journal contains any valid recovery information.
1511 * Must be called between journal_init_*() and journal_load().
1512 *
1513 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1514 * we merely suppress recovery.
1515 */
1516
1517int journal_wipe(journal_t *journal, int write)
1518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 int err = 0;
1520
1521 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1522
1523 err = load_superblock(journal);
1524 if (err)
1525 return err;
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!journal->j_tail)
1528 goto no_recovery;
1529
1530 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1531 write ? "Clearing" : "Ignoring");
1532
1533 err = journal_skip_recovery(journal);
1534 if (write)
1535 journal_update_superblock(journal, 1);
1536
1537 no_recovery:
1538 return err;
1539}
1540
1541/*
1542 * journal_dev_name: format a character string to describe on what
1543 * device this journal is present.
1544 */
1545
Adrian Bunk022a4a72005-09-06 15:16:41 -07001546static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct block_device *bdev;
1549
1550 if (journal->j_inode)
1551 bdev = journal->j_inode->i_sb->s_bdev;
1552 else
1553 bdev = journal->j_dev;
1554
1555 return bdevname(bdev, buffer);
1556}
1557
1558/*
1559 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001560 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 *
1562 * Two internal function, which provide abort to te jbd layer
1563 * itself are here.
1564 */
1565
1566/*
1567 * Quick version for internal journal use (doesn't lock the journal).
1568 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1569 * and don't attempt to make any other journal updates.
1570 */
Adrian Bunk53308382008-02-06 01:40:12 -08001571static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 transaction_t *transaction;
1574 char b[BDEVNAME_SIZE];
1575
1576 if (journal->j_flags & JFS_ABORT)
1577 return;
1578
1579 printk(KERN_ERR "Aborting journal on device %s.\n",
1580 journal_dev_name(journal, b));
1581
1582 spin_lock(&journal->j_state_lock);
1583 journal->j_flags |= JFS_ABORT;
1584 transaction = journal->j_running_transaction;
1585 if (transaction)
1586 __log_start_commit(journal, transaction->t_tid);
1587 spin_unlock(&journal->j_state_lock);
1588}
1589
1590/* Soft abort: record the abort error status in the journal superblock,
1591 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001592static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 if (journal->j_flags & JFS_ABORT)
1595 return;
1596
1597 if (!journal->j_errno)
1598 journal->j_errno = errno;
1599
1600 __journal_abort_hard(journal);
1601
1602 if (errno)
1603 journal_update_superblock(journal, 1);
1604}
1605
1606/**
1607 * void journal_abort () - Shutdown the journal immediately.
1608 * @journal: the journal to shutdown.
1609 * @errno: an error number to record in the journal indicating
1610 * the reason for the shutdown.
1611 *
1612 * Perform a complete, immediate shutdown of the ENTIRE
1613 * journal (not of a single transaction). This operation cannot be
1614 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001615 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * The journal_abort function is intended to support higher level error
1617 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1618 * mode.
1619 *
1620 * Journal abort has very specific semantics. Any existing dirty,
1621 * unjournaled buffers in the main filesystem will still be written to
1622 * disk by bdflush, but the journaling mechanism will be suspended
1623 * immediately and no further transaction commits will be honoured.
1624 *
1625 * Any dirty, journaled buffers will be written back to disk without
1626 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1627 * filesystem, but we _do_ attempt to leave as much data as possible
1628 * behind for fsck to use for cleanup.
1629 *
1630 * Any attempt to get a new transaction handle on a journal which is in
1631 * ABORT state will just result in an -EROFS error return. A
1632 * journal_stop on an existing handle will return -EIO if we have
1633 * entered abort state during the update.
1634 *
1635 * Recursive transactions are not disturbed by journal abort until the
1636 * final journal_stop, which will receive the -EIO error.
1637 *
1638 * Finally, the journal_abort call allows the caller to supply an errno
1639 * which will be recorded (if possible) in the journal superblock. This
1640 * allows a client to record failure conditions in the middle of a
1641 * transaction without having to complete the transaction to record the
1642 * failure to disk. ext3_error, for example, now uses this
1643 * functionality.
1644 *
1645 * Errors which originate from within the journaling layer will NOT
1646 * supply an errno; a null errno implies that absolutely no further
1647 * writes are done to the journal (unless there are any already in
1648 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001649 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651
1652void journal_abort(journal_t *journal, int errno)
1653{
1654 __journal_abort_soft(journal, errno);
1655}
1656
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001657/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * int journal_errno () - returns the journal's error state.
1659 * @journal: journal to examine.
1660 *
1661 * This is the errno numbet set with journal_abort(), the last
1662 * time the journal was mounted - if the journal was stopped
1663 * without calling abort this will be 0.
1664 *
1665 * If the journal has been aborted on this mount time -EROFS will
1666 * be returned.
1667 */
1668int journal_errno(journal_t *journal)
1669{
1670 int err;
1671
1672 spin_lock(&journal->j_state_lock);
1673 if (journal->j_flags & JFS_ABORT)
1674 err = -EROFS;
1675 else
1676 err = journal->j_errno;
1677 spin_unlock(&journal->j_state_lock);
1678 return err;
1679}
1680
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001681/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * int journal_clear_err () - clears the journal's error state
1683 * @journal: journal to act on.
1684 *
1685 * An error must be cleared or Acked to take a FS out of readonly
1686 * mode.
1687 */
1688int journal_clear_err(journal_t *journal)
1689{
1690 int err = 0;
1691
1692 spin_lock(&journal->j_state_lock);
1693 if (journal->j_flags & JFS_ABORT)
1694 err = -EROFS;
1695 else
1696 journal->j_errno = 0;
1697 spin_unlock(&journal->j_state_lock);
1698 return err;
1699}
1700
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001701/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * void journal_ack_err() - Ack journal err.
1703 * @journal: journal to act on.
1704 *
1705 * An error must be cleared or Acked to take a FS out of readonly
1706 * mode.
1707 */
1708void journal_ack_err(journal_t *journal)
1709{
1710 spin_lock(&journal->j_state_lock);
1711 if (journal->j_errno)
1712 journal->j_flags |= JFS_ACK_ERR;
1713 spin_unlock(&journal->j_state_lock);
1714}
1715
1716int journal_blocks_per_page(struct inode *inode)
1717{
1718 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1719}
1720
1721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * Journal_head storage management
1723 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001724static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725#ifdef CONFIG_JBD_DEBUG
1726static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1727#endif
1728
1729static int journal_init_journal_head_cache(void)
1730{
1731 int retval;
1732
Al Viro1076d172008-03-29 03:07:18 +00001733 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 journal_head_cache = kmem_cache_create("journal_head",
1735 sizeof(struct journal_head),
1736 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001737 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001738 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001740 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 retval = -ENOMEM;
1742 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1743 }
1744 return retval;
1745}
1746
1747static void journal_destroy_journal_head_cache(void)
1748{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001749 if (journal_head_cache) {
1750 kmem_cache_destroy(journal_head_cache);
1751 journal_head_cache = NULL;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/*
1756 * journal_head splicing and dicing
1757 */
1758static struct journal_head *journal_alloc_journal_head(void)
1759{
1760 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762#ifdef CONFIG_JBD_DEBUG
1763 atomic_inc(&nr_journal_heads);
1764#endif
1765 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001766 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001768 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1769 __func__);
1770
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001771 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 yield();
1773 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1774 }
1775 }
1776 return ret;
1777}
1778
1779static void journal_free_journal_head(struct journal_head *jh)
1780{
1781#ifdef CONFIG_JBD_DEBUG
1782 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001783 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#endif
1785 kmem_cache_free(journal_head_cache, jh);
1786}
1787
1788/*
1789 * A journal_head is attached to a buffer_head whenever JBD has an
1790 * interest in the buffer.
1791 *
1792 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1793 * is set. This bit is tested in core kernel code where we need to take
1794 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1795 * there.
1796 *
1797 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1798 *
1799 * When a buffer has its BH_JBD bit set it is immune from being released by
1800 * core kernel code, mainly via ->b_count.
1801 *
1802 * A journal_head may be detached from its buffer_head when the journal_head's
1803 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1804 * Various places in JBD call journal_remove_journal_head() to indicate that the
1805 * journal_head can be dropped if needed.
1806 *
1807 * Various places in the kernel want to attach a journal_head to a buffer_head
1808 * _before_ attaching the journal_head to a transaction. To protect the
1809 * journal_head in this situation, journal_add_journal_head elevates the
1810 * journal_head's b_jcount refcount by one. The caller must call
1811 * journal_put_journal_head() to undo this.
1812 *
1813 * So the typical usage would be:
1814 *
1815 * (Attach a journal_head if needed. Increments b_jcount)
1816 * struct journal_head *jh = journal_add_journal_head(bh);
1817 * ...
1818 * jh->b_transaction = xxx;
1819 * journal_put_journal_head(jh);
1820 *
1821 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1822 * because it has a non-zero b_transaction.
1823 */
1824
1825/*
1826 * Give a buffer_head a journal_head.
1827 *
1828 * Doesn't need the journal lock.
1829 * May sleep.
1830 */
1831struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1832{
1833 struct journal_head *jh;
1834 struct journal_head *new_jh = NULL;
1835
1836repeat:
1837 if (!buffer_jbd(bh)) {
1838 new_jh = journal_alloc_journal_head();
1839 memset(new_jh, 0, sizeof(*new_jh));
1840 }
1841
1842 jbd_lock_bh_journal_head(bh);
1843 if (buffer_jbd(bh)) {
1844 jh = bh2jh(bh);
1845 } else {
1846 J_ASSERT_BH(bh,
1847 (atomic_read(&bh->b_count) > 0) ||
1848 (bh->b_page && bh->b_page->mapping));
1849
1850 if (!new_jh) {
1851 jbd_unlock_bh_journal_head(bh);
1852 goto repeat;
1853 }
1854
1855 jh = new_jh;
1856 new_jh = NULL; /* We consumed it */
1857 set_buffer_jbd(bh);
1858 bh->b_private = jh;
1859 jh->b_bh = bh;
1860 get_bh(bh);
1861 BUFFER_TRACE(bh, "added journal_head");
1862 }
1863 jh->b_jcount++;
1864 jbd_unlock_bh_journal_head(bh);
1865 if (new_jh)
1866 journal_free_journal_head(new_jh);
1867 return bh->b_private;
1868}
1869
1870/*
1871 * Grab a ref against this buffer_head's journal_head. If it ended up not
1872 * having a journal_head, return NULL
1873 */
1874struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1875{
1876 struct journal_head *jh = NULL;
1877
1878 jbd_lock_bh_journal_head(bh);
1879 if (buffer_jbd(bh)) {
1880 jh = bh2jh(bh);
1881 jh->b_jcount++;
1882 }
1883 jbd_unlock_bh_journal_head(bh);
1884 return jh;
1885}
1886
1887static void __journal_remove_journal_head(struct buffer_head *bh)
1888{
1889 struct journal_head *jh = bh2jh(bh);
1890
1891 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1892
1893 get_bh(bh);
1894 if (jh->b_jcount == 0) {
1895 if (jh->b_transaction == NULL &&
1896 jh->b_next_transaction == NULL &&
1897 jh->b_cp_transaction == NULL) {
1898 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1899 J_ASSERT_BH(bh, buffer_jbd(bh));
1900 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1901 BUFFER_TRACE(bh, "remove journal_head");
1902 if (jh->b_frozen_data) {
1903 printk(KERN_WARNING "%s: freeing "
1904 "b_frozen_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001905 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001906 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908 if (jh->b_committed_data) {
1909 printk(KERN_WARNING "%s: freeing "
1910 "b_committed_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001911 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001912 jbd_free(jh->b_committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 bh->b_private = NULL;
1915 jh->b_bh = NULL; /* debug, really */
1916 clear_buffer_jbd(bh);
1917 __brelse(bh);
1918 journal_free_journal_head(jh);
1919 } else {
1920 BUFFER_TRACE(bh, "journal_head was locked");
1921 }
1922 }
1923}
1924
1925/*
1926 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1927 * and has a zero b_jcount then remove and release its journal_head. If we did
1928 * see that the buffer is not used by any transaction we also "logically"
1929 * decrement ->b_count.
1930 *
1931 * We in fact take an additional increment on ->b_count as a convenience,
1932 * because the caller usually wants to do additional things with the bh
1933 * after calling here.
1934 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1935 * time. Once the caller has run __brelse(), the buffer is eligible for
1936 * reaping by try_to_free_buffers().
1937 */
1938void journal_remove_journal_head(struct buffer_head *bh)
1939{
1940 jbd_lock_bh_journal_head(bh);
1941 __journal_remove_journal_head(bh);
1942 jbd_unlock_bh_journal_head(bh);
1943}
1944
1945/*
1946 * Drop a reference on the passed journal_head. If it fell to zero then try to
1947 * release the journal_head from the buffer_head.
1948 */
1949void journal_put_journal_head(struct journal_head *jh)
1950{
1951 struct buffer_head *bh = jh2bh(jh);
1952
1953 jbd_lock_bh_journal_head(bh);
1954 J_ASSERT_JH(jh, jh->b_jcount > 0);
1955 --jh->b_jcount;
1956 if (!jh->b_jcount && !jh->b_transaction) {
1957 __journal_remove_journal_head(bh);
1958 __brelse(bh);
1959 }
1960 jbd_unlock_bh_journal_head(bh);
1961}
1962
1963/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07001964 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07001966#ifdef CONFIG_JBD_DEBUG
1967
1968u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jose R. Santosc2a91592007-10-18 23:39:22 -07001971static struct dentry *jbd_debugfs_dir;
1972static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Jose R. Santosc2a91592007-10-18 23:39:22 -07001974static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001976 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1977 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08001978 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07001979 jbd_debugfs_dir,
1980 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Jose R. Santosc2a91592007-10-18 23:39:22 -07001983static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001985 debugfs_remove(jbd_debug);
1986 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
1989#else
1990
Jose R. Santosc2a91592007-10-18 23:39:22 -07001991static inline void jbd_create_debugfs_entry(void)
1992{
1993}
1994
1995static inline void jbd_remove_debugfs_entry(void)
1996{
1997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999#endif
2000
Christoph Lametere18b8902006-12-06 20:33:20 -08002001struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003static int __init journal_init_handle_cache(void)
2004{
2005 jbd_handle_cache = kmem_cache_create("journal_handle",
2006 sizeof(handle_t),
2007 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07002008 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09002009 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (jbd_handle_cache == NULL) {
2011 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2012 return -ENOMEM;
2013 }
2014 return 0;
2015}
2016
2017static void journal_destroy_handle_cache(void)
2018{
2019 if (jbd_handle_cache)
2020 kmem_cache_destroy(jbd_handle_cache);
2021}
2022
2023/*
2024 * Module startup and shutdown
2025 */
2026
2027static int __init journal_init_caches(void)
2028{
2029 int ret;
2030
2031 ret = journal_init_revoke_caches();
2032 if (ret == 0)
2033 ret = journal_init_journal_head_cache();
2034 if (ret == 0)
2035 ret = journal_init_handle_cache();
2036 return ret;
2037}
2038
2039static void journal_destroy_caches(void)
2040{
2041 journal_destroy_revoke_caches();
2042 journal_destroy_journal_head_cache();
2043 journal_destroy_handle_cache();
2044}
2045
2046static int __init journal_init(void)
2047{
2048 int ret;
2049
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002050 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 ret = journal_init_caches();
2053 if (ret != 0)
2054 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002055 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return ret;
2057}
2058
2059static void __exit journal_exit(void)
2060{
2061#ifdef CONFIG_JBD_DEBUG
2062 int n = atomic_read(&nr_journal_heads);
2063 if (n)
2064 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2065#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002066 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 journal_destroy_caches();
2068}
2069
2070MODULE_LICENSE("GPL");
2071module_init(journal_init);
2072module_exit(journal_exit);
2073