blob: e561176518260c23a595e7b6439d2036ebbf9148 [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 /*
440 * Are we already doing a recent enough commit?
441 */
442 if (!tid_geq(journal->j_commit_request, target)) {
443 /*
444 * We want a new commit: OK, mark the request and wakup the
445 * commit thread. We do _not_ do the commit ourselves.
446 */
447
448 journal->j_commit_request = target;
449 jbd_debug(1, "JBD: requesting commit %d/%d\n",
450 journal->j_commit_request,
451 journal->j_commit_sequence);
452 wake_up(&journal->j_wait_commit);
453 return 1;
454 }
455 return 0;
456}
457
458int log_start_commit(journal_t *journal, tid_t tid)
459{
460 int ret;
461
462 spin_lock(&journal->j_state_lock);
463 ret = __log_start_commit(journal, tid);
464 spin_unlock(&journal->j_state_lock);
465 return ret;
466}
467
468/*
469 * Force and wait upon a commit if the calling process is not within
470 * transaction. This is used for forcing out undo-protected data which contains
471 * bitmaps, when the fs is running out of space.
472 *
473 * We can only force the running transaction if we don't have an active handle;
474 * otherwise, we will deadlock.
475 *
476 * Returns true if a transaction was started.
477 */
478int journal_force_commit_nested(journal_t *journal)
479{
480 transaction_t *transaction = NULL;
481 tid_t tid;
482
483 spin_lock(&journal->j_state_lock);
484 if (journal->j_running_transaction && !current->journal_info) {
485 transaction = journal->j_running_transaction;
486 __log_start_commit(journal, transaction->t_tid);
487 } else if (journal->j_committing_transaction)
488 transaction = journal->j_committing_transaction;
489
490 if (!transaction) {
491 spin_unlock(&journal->j_state_lock);
492 return 0; /* Nothing to retry */
493 }
494
495 tid = transaction->t_tid;
496 spin_unlock(&journal->j_state_lock);
497 log_wait_commit(journal, tid);
498 return 1;
499}
500
501/*
502 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800503 * if a transaction is going to be committed (or is currently already
504 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
506int journal_start_commit(journal_t *journal, tid_t *ptid)
507{
508 int ret = 0;
509
510 spin_lock(&journal->j_state_lock);
511 if (journal->j_running_transaction) {
512 tid_t tid = journal->j_running_transaction->t_tid;
513
Jan Kara8fe4cd02009-02-11 13:04:25 -0800514 __log_start_commit(journal, tid);
515 /* There's a running transaction and we've just made sure
516 * it's commit has been scheduled. */
517 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800519 ret = 1;
520 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * If ext3_write_super() recently started a commit, then we
523 * have to wait for completion of that transaction
524 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800525 if (ptid)
526 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ret = 1;
528 }
529 spin_unlock(&journal->j_state_lock);
530 return ret;
531}
532
533/*
534 * Wait for a specified commit to complete.
535 * The caller may not hold the journal lock.
536 */
537int log_wait_commit(journal_t *journal, tid_t tid)
538{
539 int err = 0;
540
541#ifdef CONFIG_JBD_DEBUG
542 spin_lock(&journal->j_state_lock);
543 if (!tid_geq(journal->j_commit_request, tid)) {
544 printk(KERN_EMERG
545 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700546 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 spin_unlock(&journal->j_state_lock);
549#endif
550 spin_lock(&journal->j_state_lock);
551 while (tid_gt(tid, journal->j_commit_sequence)) {
552 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
553 tid, journal->j_commit_sequence);
554 wake_up(&journal->j_wait_commit);
555 spin_unlock(&journal->j_state_lock);
556 wait_event(journal->j_wait_done_commit,
557 !tid_gt(tid, journal->j_commit_sequence));
558 spin_lock(&journal->j_state_lock);
559 }
560 spin_unlock(&journal->j_state_lock);
561
562 if (unlikely(is_journal_aborted(journal))) {
563 printk(KERN_EMERG "journal commit I/O error\n");
564 err = -EIO;
565 }
566 return err;
567}
568
569/*
Jan Kara03f4d802010-04-15 22:16:24 +0200570 * Return 1 if a given transaction has not yet sent barrier request
571 * connected with a transaction commit. If 0 is returned, transaction
572 * may or may not have sent the barrier. Used to avoid sending barrier
573 * twice in common cases.
574 */
575int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
576{
577 int ret = 0;
578 transaction_t *commit_trans;
579
580 if (!(journal->j_flags & JFS_BARRIER))
581 return 0;
582 spin_lock(&journal->j_state_lock);
583 /* Transaction already committed? */
584 if (tid_geq(journal->j_commit_sequence, tid))
585 goto out;
586 /*
587 * Transaction is being committed and we already proceeded to
588 * writing commit record?
589 */
590 commit_trans = journal->j_committing_transaction;
591 if (commit_trans && commit_trans->t_tid == tid &&
592 commit_trans->t_state >= T_COMMIT_RECORD)
593 goto out;
594 ret = 1;
595out:
596 spin_unlock(&journal->j_state_lock);
597 return ret;
598}
Jan Kara52779702010-04-15 22:24:26 +0200599EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
Jan Kara03f4d802010-04-15 22:16:24 +0200600
601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * Log buffer allocation routines:
603 */
604
Jan Kara9c28cbc2009-08-03 19:21:00 +0200605int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200607 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 spin_lock(&journal->j_state_lock);
610 J_ASSERT(journal->j_free > 1);
611
612 blocknr = journal->j_head;
613 journal->j_head++;
614 journal->j_free--;
615 if (journal->j_head == journal->j_last)
616 journal->j_head = journal->j_first;
617 spin_unlock(&journal->j_state_lock);
618 return journal_bmap(journal, blocknr, retp);
619}
620
621/*
622 * Conversion of logical to physical block numbers for the journal
623 *
624 * On external journals the journal blocks are identity-mapped, so
625 * this is a no-op. If needed, we can use j_blk_offset - everything is
626 * ready.
627 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200628int journal_bmap(journal_t *journal, unsigned int blocknr,
629 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200632 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (journal->j_inode) {
635 ret = bmap(journal->j_inode, blocknr);
636 if (ret)
637 *retp = ret;
638 else {
639 char b[BDEVNAME_SIZE];
640
641 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200642 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700643 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 blocknr,
645 bdevname(journal->j_dev, b));
646 err = -EIO;
647 __journal_abort_soft(journal, err);
648 }
649 } else {
650 *retp = blocknr; /* +journal->j_blk_offset */
651 }
652 return err;
653}
654
655/*
656 * We play buffer_head aliasing tricks to write data/metadata blocks to
657 * the journal without copying their contents, but for journal
658 * descriptor blocks we do need to generate bona fide buffers.
659 *
660 * After the caller of journal_get_descriptor_buffer() has finished modifying
661 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
662 * But we don't bother doing that, so there will be coherency problems with
663 * mmaps of blockdevs which hold live JBD-controlled filesystems.
664 */
665struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
666{
667 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200668 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int err;
670
671 err = journal_next_log_block(journal, &blocknr);
672
673 if (err)
674 return NULL;
675
676 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700677 if (!bh)
678 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 lock_buffer(bh);
680 memset(bh->b_data, 0, journal->j_blocksize);
681 set_buffer_uptodate(bh);
682 unlock_buffer(bh);
683 BUFFER_TRACE(bh, "return this buffer");
684 return journal_add_journal_head(bh);
685}
686
687/*
688 * Management for journal control blocks: functions to create and
689 * destroy journal_t structures, and to initialise and read existing
690 * journal blocks from disk. */
691
692/* First: create and setup a journal_t object in memory. We initialise
693 * very few fields yet: that has to wait until we have created the
694 * journal structures from from scratch, or loaded them from disk. */
695
696static journal_t * journal_init_common (void)
697{
698 journal_t *journal;
699 int err;
700
Mingming Cao8c3478a2007-10-18 23:39:20 -0700701 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!journal)
703 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 init_waitqueue_head(&journal->j_wait_transaction_locked);
706 init_waitqueue_head(&journal->j_wait_logspace);
707 init_waitqueue_head(&journal->j_wait_done_commit);
708 init_waitqueue_head(&journal->j_wait_checkpoint);
709 init_waitqueue_head(&journal->j_wait_commit);
710 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800711 mutex_init(&journal->j_barrier);
712 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_lock_init(&journal->j_revoke_lock);
714 spin_lock_init(&journal->j_list_lock);
715 spin_lock_init(&journal->j_state_lock);
716
717 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
718
719 /* The journal is marked for error until we succeed with recovery! */
720 journal->j_flags = JFS_ABORT;
721
722 /* Set up a default-sized revoke table for the new mount. */
723 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
724 if (err) {
725 kfree(journal);
726 goto fail;
727 }
728 return journal;
729fail:
730 return NULL;
731}
732
733/* journal_init_dev and journal_init_inode:
734 *
735 * Create a journal structure assigned some fixed set of disk blocks to
736 * the journal. We don't actually touch those disk blocks yet, but we
737 * need to set up all of the mapping information to tell the journaling
738 * system where the journal blocks are.
739 *
740 */
741
742/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700743 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * @bdev: Block device on which to create the journal
745 * @fs_dev: Device which hold journalled filesystem for this journal.
746 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700747 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700749 *
750 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700751 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * journal_init_dev creates a journal which maps a fixed contiguous
753 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700754 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
756journal_t * journal_init_dev(struct block_device *bdev,
757 struct block_device *fs_dev,
758 int start, int len, int blocksize)
759{
760 journal_t *journal = journal_init_common();
761 struct buffer_head *bh;
762 int n;
763
764 if (!journal)
765 return NULL;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700768 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 n = journal->j_blocksize / sizeof(journal_block_tag_t);
770 journal->j_wbufsize = n;
771 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
772 if (!journal->j_wbuf) {
773 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700774 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700775 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700777 journal->j_dev = bdev;
778 journal->j_fs_dev = fs_dev;
779 journal->j_blk_offset = start;
780 journal->j_maxlen = len;
781
782 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700783 if (!bh) {
784 printk(KERN_ERR
785 "%s: Cannot get buffer for journal superblock\n",
786 __func__);
787 goto out_err;
788 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700789 journal->j_sb_buffer = bh;
790 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700793out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800794 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700795 kfree(journal);
796 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700798
799/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
801 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700802 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * journal_init_inode creates a journal which maps an on-disk inode as
804 * the journal. The inode must exist already, must support bmap() and
805 * must have all data blocks preallocated.
806 */
807journal_t * journal_init_inode (struct inode *inode)
808{
809 struct buffer_head *bh;
810 journal_t *journal = journal_init_common();
811 int err;
812 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200813 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!journal)
816 return NULL;
817
818 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
819 journal->j_inode = inode;
820 jbd_debug(1,
821 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700822 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 (long long) inode->i_size,
824 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
825
826 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
827 journal->j_blocksize = inode->i_sb->s_blocksize;
828
829 /* journal descriptor can store up to n blocks -bzzz */
830 n = journal->j_blocksize / sizeof(journal_block_tag_t);
831 journal->j_wbufsize = n;
832 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
833 if (!journal->j_wbuf) {
834 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700835 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700836 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 err = journal_bmap(journal, 0, &blocknr);
840 /* If that failed, give up */
841 if (err) {
842 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700843 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700844 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
847 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700848 if (!bh) {
849 printk(KERN_ERR
850 "%s: Cannot get buffer for journal superblock\n",
851 __func__);
852 goto out_err;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 journal->j_sb_buffer = bh;
855 journal->j_superblock = (journal_superblock_t *)bh->b_data;
856
857 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700858out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800859 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700860 kfree(journal);
861 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * If the journal init or create aborts, we need to mark the journal
866 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700867 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869static void journal_fail_superblock (journal_t *journal)
870{
871 struct buffer_head *bh = journal->j_sb_buffer;
872 brelse(bh);
873 journal->j_sb_buffer = NULL;
874}
875
876/*
877 * Given a journal_t structure, initialise the various fields for
878 * startup of a new journaling session. We use this both when creating
879 * a journal, and after recovering an old journal to reset it for
880 * subsequent use.
881 */
882
883static int journal_reset(journal_t *journal)
884{
885 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200886 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 first = be32_to_cpu(sb->s_first);
889 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200890 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200891 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200892 first, last);
893 journal_fail_superblock(journal);
894 return -EINVAL;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 journal->j_first = first;
898 journal->j_last = last;
899
900 journal->j_head = first;
901 journal->j_tail = first;
902 journal->j_free = last - first;
903
904 journal->j_tail_sequence = journal->j_transaction_sequence;
905 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
906 journal->j_commit_request = journal->j_commit_sequence;
907
908 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
909
910 /* Add the dynamic fields and write it to disk. */
911 journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700912 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * int journal_create() - Initialise the new journal file
917 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700918 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * Given a journal_t structure which tells us which disk blocks we can
920 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700921 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 **/
923int journal_create(journal_t *journal)
924{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200925 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct buffer_head *bh;
927 journal_superblock_t *sb;
928 int i, err;
929
930 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
931 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
932 journal->j_maxlen);
933 journal_fail_superblock(journal);
934 return -EINVAL;
935 }
936
937 if (journal->j_inode == NULL) {
938 /*
939 * We don't know what block to start at!
940 */
941 printk(KERN_EMERG
942 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700943 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 BUG();
945 }
946
947 /* Zero out the entire journal on disk. We cannot afford to
948 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
949 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
950 for (i = 0; i < journal->j_maxlen; i++) {
951 err = journal_bmap(journal, i, &blocknr);
952 if (err)
953 return err;
954 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
955 lock_buffer(bh);
956 memset (bh->b_data, 0, journal->j_blocksize);
957 BUFFER_TRACE(bh, "marking dirty");
958 mark_buffer_dirty(bh);
959 BUFFER_TRACE(bh, "marking uptodate");
960 set_buffer_uptodate(bh);
961 unlock_buffer(bh);
962 __brelse(bh);
963 }
964
965 sync_blockdev(journal->j_dev);
966 jbd_debug(1, "JBD: journal cleared.\n");
967
968 /* OK, fill in the initial static fields in the new superblock */
969 sb = journal->j_superblock;
970
971 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
972 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
973
974 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
975 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
976 sb->s_first = cpu_to_be32(1);
977
978 journal->j_transaction_sequence = 1;
979
980 journal->j_flags &= ~JFS_ABORT;
981 journal->j_format_version = 2;
982
983 return journal_reset(journal);
984}
985
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700986/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * void journal_update_superblock() - Update journal sb on disk.
988 * @journal: The journal to update.
989 * @wait: Set to '0' if you don't want to wait for IO completion.
990 *
991 * Update a journal's dynamic superblock fields and write it to disk,
992 * optionally waiting for the IO to complete.
993 */
994void journal_update_superblock(journal_t *journal, int wait)
995{
996 journal_superblock_t *sb = journal->j_superblock;
997 struct buffer_head *bh = journal->j_sb_buffer;
998
999 /*
1000 * As a special case, if the on-disk copy is already marked as needing
1001 * no recovery (s_start == 0) and there are no outstanding transactions
1002 * in the filesystem, then we can safely defer the superblock update
1003 * until the next commit by setting JFS_FLUSHED. This avoids
1004 * attempting a write to a potential-readonly device.
1005 */
1006 if (sb->s_start == 0 && journal->j_tail_sequence ==
1007 journal->j_transaction_sequence) {
1008 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
Jan Kara9c28cbc2009-08-03 19:21:00 +02001009 "(start %u, seq %d, errno %d)\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001010 journal->j_tail, journal->j_tail_sequence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 journal->j_errno);
1012 goto out;
1013 }
1014
Darrick J. Wongdff68252010-10-04 12:35:05 -07001015 if (buffer_write_io_error(bh)) {
1016 char b[BDEVNAME_SIZE];
1017 /*
1018 * Oh, dear. A previous attempt to write the journal
1019 * superblock failed. This could happen because the
1020 * USB device was yanked out. Or it could happen to
1021 * be a transient write error and maybe the block will
1022 * be remapped. Nothing we can do but to retry the
1023 * write and hope for the best.
1024 */
1025 printk(KERN_ERR "JBD: previous I/O error detected "
1026 "for journal superblock update for %s.\n",
1027 journal_dev_name(journal, b));
1028 clear_buffer_write_io_error(bh);
1029 set_buffer_uptodate(bh);
1030 }
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_lock(&journal->j_state_lock);
Jan Kara9c28cbc2009-08-03 19:21:00 +02001033 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1035
1036 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1037 sb->s_start = cpu_to_be32(journal->j_tail);
1038 sb->s_errno = cpu_to_be32(journal->j_errno);
1039 spin_unlock(&journal->j_state_lock);
1040
1041 BUFFER_TRACE(bh, "marking dirty");
1042 mark_buffer_dirty(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001043 if (wait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 sync_dirty_buffer(bh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07001045 if (buffer_write_io_error(bh)) {
1046 char b[BDEVNAME_SIZE];
1047 printk(KERN_ERR "JBD: I/O error detected "
1048 "when updating journal superblock for %s.\n",
1049 journal_dev_name(journal, b));
1050 clear_buffer_write_io_error(bh);
1051 set_buffer_uptodate(bh);
1052 }
1053 } else
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02001054 write_dirty_buffer(bh, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056out:
1057 /* If we have just flushed the log (by marking s_start==0), then
1058 * any future commit will have to be careful to update the
1059 * superblock again to re-record the true start of the log. */
1060
1061 spin_lock(&journal->j_state_lock);
1062 if (sb->s_start)
1063 journal->j_flags &= ~JFS_FLUSHED;
1064 else
1065 journal->j_flags |= JFS_FLUSHED;
1066 spin_unlock(&journal->j_state_lock);
1067}
1068
1069/*
1070 * Read the superblock for a given journal, performing initial
1071 * validation of the format.
1072 */
1073
1074static int journal_get_superblock(journal_t *journal)
1075{
1076 struct buffer_head *bh;
1077 journal_superblock_t *sb;
1078 int err = -EIO;
1079
1080 bh = journal->j_sb_buffer;
1081
1082 J_ASSERT(bh != NULL);
1083 if (!buffer_uptodate(bh)) {
1084 ll_rw_block(READ, 1, &bh);
1085 wait_on_buffer(bh);
1086 if (!buffer_uptodate(bh)) {
1087 printk (KERN_ERR
1088 "JBD: IO error reading journal superblock\n");
1089 goto out;
1090 }
1091 }
1092
1093 sb = journal->j_superblock;
1094
1095 err = -EINVAL;
1096
1097 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1098 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1099 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1100 goto out;
1101 }
1102
1103 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1104 case JFS_SUPERBLOCK_V1:
1105 journal->j_format_version = 1;
1106 break;
1107 case JFS_SUPERBLOCK_V2:
1108 journal->j_format_version = 2;
1109 break;
1110 default:
1111 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1112 goto out;
1113 }
1114
1115 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1116 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1117 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1118 printk (KERN_WARNING "JBD: journal file too short\n");
1119 goto out;
1120 }
1121
1122 return 0;
1123
1124out:
1125 journal_fail_superblock(journal);
1126 return err;
1127}
1128
1129/*
1130 * Load the on-disk journal superblock and read the key fields into the
1131 * journal_t.
1132 */
1133
1134static int load_superblock(journal_t *journal)
1135{
1136 int err;
1137 journal_superblock_t *sb;
1138
1139 err = journal_get_superblock(journal);
1140 if (err)
1141 return err;
1142
1143 sb = journal->j_superblock;
1144
1145 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1146 journal->j_tail = be32_to_cpu(sb->s_start);
1147 journal->j_first = be32_to_cpu(sb->s_first);
1148 journal->j_last = be32_to_cpu(sb->s_maxlen);
1149 journal->j_errno = be32_to_cpu(sb->s_errno);
1150
1151 return 0;
1152}
1153
1154
1155/**
1156 * int journal_load() - Read journal from disk.
1157 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * Given a journal_t structure which tells us which disk blocks contain
1160 * a journal, read the journal from disk to initialise the in-memory
1161 * structures.
1162 */
1163int journal_load(journal_t *journal)
1164{
1165 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001166 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 err = load_superblock(journal);
1169 if (err)
1170 return err;
1171
Badari Pulavartyea817392006-08-27 01:23:52 -07001172 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* If this is a V2 superblock, then we have to check the
1174 * features flags on it. */
1175
1176 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if ((sb->s_feature_ro_compat &
1178 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1179 (sb->s_feature_incompat &
1180 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1181 printk (KERN_WARNING
1182 "JBD: Unrecognised features on journal\n");
1183 return -EINVAL;
1184 }
1185 }
1186
1187 /* Let the recovery code check whether it needs to recover any
1188 * data from the journal. */
1189 if (journal_recover(journal))
1190 goto recovery_error;
1191
1192 /* OK, we've finished with the dynamic journal bits:
1193 * reinitialise the dynamic contents of the superblock in memory
1194 * and reset them on disk. */
1195 if (journal_reset(journal))
1196 goto recovery_error;
1197
1198 journal->j_flags &= ~JFS_ABORT;
1199 journal->j_flags |= JFS_LOADED;
1200 return 0;
1201
1202recovery_error:
1203 printk (KERN_WARNING "JBD: recovery failed\n");
1204 return -EIO;
1205}
1206
1207/**
1208 * void journal_destroy() - Release a journal_t structure.
1209 * @journal: Journal to act on.
1210 *
1211 * Release a journal_t structure once it is no longer in use by the
1212 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001213 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001215int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001217 int err = 0;
1218
Jan Kara03f4d802010-04-15 22:16:24 +02001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* Wait for the commit thread to wake up and die. */
1221 journal_kill_thread(journal);
1222
1223 /* Force a final log commit */
1224 if (journal->j_running_transaction)
1225 journal_commit_transaction(journal);
1226
1227 /* Force any old transactions to disk */
1228
1229 /* Totally anal locking here... */
1230 spin_lock(&journal->j_list_lock);
1231 while (journal->j_checkpoint_transactions != NULL) {
1232 spin_unlock(&journal->j_list_lock);
1233 log_do_checkpoint(journal);
1234 spin_lock(&journal->j_list_lock);
1235 }
1236
1237 J_ASSERT(journal->j_running_transaction == NULL);
1238 J_ASSERT(journal->j_committing_transaction == NULL);
1239 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1240 spin_unlock(&journal->j_list_lock);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001243 if (!is_journal_aborted(journal)) {
1244 /* We can now mark the journal as empty. */
1245 journal->j_tail = 0;
1246 journal->j_tail_sequence =
1247 ++journal->j_transaction_sequence;
1248 journal_update_superblock(journal, 1);
1249 } else {
1250 err = -EIO;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 brelse(journal->j_sb_buffer);
1253 }
1254
1255 if (journal->j_inode)
1256 iput(journal->j_inode);
1257 if (journal->j_revoke)
1258 journal_destroy_revoke(journal);
1259 kfree(journal->j_wbuf);
1260 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001261
1262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265
1266/**
1267 *int journal_check_used_features () - Check if features specified are used.
1268 * @journal: Journal to check.
1269 * @compat: bitmask of compatible features
1270 * @ro: bitmask of features that force read-only mount
1271 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001274 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 **/
1276
1277int journal_check_used_features (journal_t *journal, unsigned long compat,
1278 unsigned long ro, unsigned long incompat)
1279{
1280 journal_superblock_t *sb;
1281
1282 if (!compat && !ro && !incompat)
1283 return 1;
1284 if (journal->j_format_version == 1)
1285 return 0;
1286
1287 sb = journal->j_superblock;
1288
1289 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1290 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1291 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1292 return 1;
1293
1294 return 0;
1295}
1296
1297/**
1298 * int journal_check_available_features() - Check feature set in journalling layer
1299 * @journal: Journal to check.
1300 * @compat: bitmask of compatible features
1301 * @ro: bitmask of features that force read-only mount
1302 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001303 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * Check whether the journaling code supports the use of
1305 * all of a given set of features on this journal. Return true
1306 * (non-zero) if it can. */
1307
1308int journal_check_available_features (journal_t *journal, unsigned long compat,
1309 unsigned long ro, unsigned long incompat)
1310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!compat && !ro && !incompat)
1312 return 1;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* We can support any known requested features iff the
1315 * superblock is in version 2. Otherwise we fail to support any
1316 * extended sb features. */
1317
1318 if (journal->j_format_version != 2)
1319 return 0;
1320
1321 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1322 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1323 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1324 return 1;
1325
1326 return 0;
1327}
1328
1329/**
1330 * int journal_set_features () - Mark a given journal feature in the superblock
1331 * @journal: Journal to act on.
1332 * @compat: bitmask of compatible features
1333 * @ro: bitmask of features that force read-only mount
1334 * @incompat: bitmask of incompatible features
1335 *
1336 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001337 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 *
1339 */
1340
1341int journal_set_features (journal_t *journal, unsigned long compat,
1342 unsigned long ro, unsigned long incompat)
1343{
1344 journal_superblock_t *sb;
1345
1346 if (journal_check_used_features(journal, compat, ro, incompat))
1347 return 1;
1348
1349 if (!journal_check_available_features(journal, compat, ro, incompat))
1350 return 0;
1351
1352 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1353 compat, ro, incompat);
1354
1355 sb = journal->j_superblock;
1356
1357 sb->s_feature_compat |= cpu_to_be32(compat);
1358 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1359 sb->s_feature_incompat |= cpu_to_be32(incompat);
1360
1361 return 1;
1362}
1363
1364
1365/**
1366 * int journal_update_format () - Update on-disk journal structure.
1367 * @journal: Journal to act on.
1368 *
1369 * Given an initialised but unloaded journal struct, poke about in the
1370 * on-disk structure to update it to the most recent supported version.
1371 */
1372int journal_update_format (journal_t *journal)
1373{
1374 journal_superblock_t *sb;
1375 int err;
1376
1377 err = journal_get_superblock(journal);
1378 if (err)
1379 return err;
1380
1381 sb = journal->j_superblock;
1382
1383 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1384 case JFS_SUPERBLOCK_V2:
1385 return 0;
1386 case JFS_SUPERBLOCK_V1:
1387 return journal_convert_superblock_v1(journal, sb);
1388 default:
1389 break;
1390 }
1391 return -EINVAL;
1392}
1393
1394static int journal_convert_superblock_v1(journal_t *journal,
1395 journal_superblock_t *sb)
1396{
1397 int offset, blocksize;
1398 struct buffer_head *bh;
1399
1400 printk(KERN_WARNING
1401 "JBD: Converting superblock from version 1 to 2.\n");
1402
1403 /* Pre-initialise new fields to zero */
1404 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1405 blocksize = be32_to_cpu(sb->s_blocksize);
1406 memset(&sb->s_feature_compat, 0, blocksize-offset);
1407
1408 sb->s_nr_users = cpu_to_be32(1);
1409 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1410 journal->j_format_version = 2;
1411
1412 bh = journal->j_sb_buffer;
1413 BUFFER_TRACE(bh, "marking dirty");
1414 mark_buffer_dirty(bh);
1415 sync_dirty_buffer(bh);
1416 return 0;
1417}
1418
1419
1420/**
1421 * int journal_flush () - Flush journal
1422 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * Flush all data for a given journal to disk and empty the journal.
1425 * Filesystems can use this when remounting readonly to ensure that
1426 * recovery does not need to happen on remount.
1427 */
1428
1429int journal_flush(journal_t *journal)
1430{
1431 int err = 0;
1432 transaction_t *transaction = NULL;
Jan Kara9c28cbc2009-08-03 19:21:00 +02001433 unsigned int old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 spin_lock(&journal->j_state_lock);
1436
1437 /* Force everything buffered to the log... */
1438 if (journal->j_running_transaction) {
1439 transaction = journal->j_running_transaction;
1440 __log_start_commit(journal, transaction->t_tid);
1441 } else if (journal->j_committing_transaction)
1442 transaction = journal->j_committing_transaction;
1443
1444 /* Wait for the log commit to complete... */
1445 if (transaction) {
1446 tid_t tid = transaction->t_tid;
1447
1448 spin_unlock(&journal->j_state_lock);
1449 log_wait_commit(journal, tid);
1450 } else {
1451 spin_unlock(&journal->j_state_lock);
1452 }
1453
1454 /* ...and flush everything in the log out to disk. */
1455 spin_lock(&journal->j_list_lock);
1456 while (!err && journal->j_checkpoint_transactions != NULL) {
1457 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001458 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001460 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 spin_lock(&journal->j_list_lock);
1462 }
1463 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001464
1465 if (is_journal_aborted(journal))
1466 return -EIO;
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 cleanup_journal_tail(journal);
1469
1470 /* Finally, mark the journal as really needing no recovery.
1471 * This sets s_start==0 in the underlying superblock, which is
1472 * the magic code for a fully-recovered superblock. Any future
1473 * commits of data to the journal will restore the current
1474 * s_start value. */
1475 spin_lock(&journal->j_state_lock);
1476 old_tail = journal->j_tail;
1477 journal->j_tail = 0;
1478 spin_unlock(&journal->j_state_lock);
1479 journal_update_superblock(journal, 1);
1480 spin_lock(&journal->j_state_lock);
1481 journal->j_tail = old_tail;
1482
1483 J_ASSERT(!journal->j_running_transaction);
1484 J_ASSERT(!journal->j_committing_transaction);
1485 J_ASSERT(!journal->j_checkpoint_transactions);
1486 J_ASSERT(journal->j_head == journal->j_tail);
1487 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1488 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/**
1493 * int journal_wipe() - Wipe journal contents
1494 * @journal: Journal to act on.
1495 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * Wipe out all of the contents of a journal, safely. This will produce
1498 * a warning if the journal contains any valid recovery information.
1499 * Must be called between journal_init_*() and journal_load().
1500 *
1501 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1502 * we merely suppress recovery.
1503 */
1504
1505int journal_wipe(journal_t *journal, int write)
1506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int err = 0;
1508
1509 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1510
1511 err = load_superblock(journal);
1512 if (err)
1513 return err;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (!journal->j_tail)
1516 goto no_recovery;
1517
1518 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1519 write ? "Clearing" : "Ignoring");
1520
1521 err = journal_skip_recovery(journal);
1522 if (write)
1523 journal_update_superblock(journal, 1);
1524
1525 no_recovery:
1526 return err;
1527}
1528
1529/*
1530 * journal_dev_name: format a character string to describe on what
1531 * device this journal is present.
1532 */
1533
Adrian Bunk022a4a72005-09-06 15:16:41 -07001534static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct block_device *bdev;
1537
1538 if (journal->j_inode)
1539 bdev = journal->j_inode->i_sb->s_bdev;
1540 else
1541 bdev = journal->j_dev;
1542
1543 return bdevname(bdev, buffer);
1544}
1545
1546/*
1547 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001548 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 *
1550 * Two internal function, which provide abort to te jbd layer
1551 * itself are here.
1552 */
1553
1554/*
1555 * Quick version for internal journal use (doesn't lock the journal).
1556 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1557 * and don't attempt to make any other journal updates.
1558 */
Adrian Bunk53308382008-02-06 01:40:12 -08001559static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 transaction_t *transaction;
1562 char b[BDEVNAME_SIZE];
1563
1564 if (journal->j_flags & JFS_ABORT)
1565 return;
1566
1567 printk(KERN_ERR "Aborting journal on device %s.\n",
1568 journal_dev_name(journal, b));
1569
1570 spin_lock(&journal->j_state_lock);
1571 journal->j_flags |= JFS_ABORT;
1572 transaction = journal->j_running_transaction;
1573 if (transaction)
1574 __log_start_commit(journal, transaction->t_tid);
1575 spin_unlock(&journal->j_state_lock);
1576}
1577
1578/* Soft abort: record the abort error status in the journal superblock,
1579 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001580static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 if (journal->j_flags & JFS_ABORT)
1583 return;
1584
1585 if (!journal->j_errno)
1586 journal->j_errno = errno;
1587
1588 __journal_abort_hard(journal);
1589
1590 if (errno)
1591 journal_update_superblock(journal, 1);
1592}
1593
1594/**
1595 * void journal_abort () - Shutdown the journal immediately.
1596 * @journal: the journal to shutdown.
1597 * @errno: an error number to record in the journal indicating
1598 * the reason for the shutdown.
1599 *
1600 * Perform a complete, immediate shutdown of the ENTIRE
1601 * journal (not of a single transaction). This operation cannot be
1602 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001603 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * The journal_abort function is intended to support higher level error
1605 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1606 * mode.
1607 *
1608 * Journal abort has very specific semantics. Any existing dirty,
1609 * unjournaled buffers in the main filesystem will still be written to
1610 * disk by bdflush, but the journaling mechanism will be suspended
1611 * immediately and no further transaction commits will be honoured.
1612 *
1613 * Any dirty, journaled buffers will be written back to disk without
1614 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1615 * filesystem, but we _do_ attempt to leave as much data as possible
1616 * behind for fsck to use for cleanup.
1617 *
1618 * Any attempt to get a new transaction handle on a journal which is in
1619 * ABORT state will just result in an -EROFS error return. A
1620 * journal_stop on an existing handle will return -EIO if we have
1621 * entered abort state during the update.
1622 *
1623 * Recursive transactions are not disturbed by journal abort until the
1624 * final journal_stop, which will receive the -EIO error.
1625 *
1626 * Finally, the journal_abort call allows the caller to supply an errno
1627 * which will be recorded (if possible) in the journal superblock. This
1628 * allows a client to record failure conditions in the middle of a
1629 * transaction without having to complete the transaction to record the
1630 * failure to disk. ext3_error, for example, now uses this
1631 * functionality.
1632 *
1633 * Errors which originate from within the journaling layer will NOT
1634 * supply an errno; a null errno implies that absolutely no further
1635 * writes are done to the journal (unless there are any already in
1636 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
1639
1640void journal_abort(journal_t *journal, int errno)
1641{
1642 __journal_abort_soft(journal, errno);
1643}
1644
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001645/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * int journal_errno () - returns the journal's error state.
1647 * @journal: journal to examine.
1648 *
1649 * This is the errno numbet set with journal_abort(), the last
1650 * time the journal was mounted - if the journal was stopped
1651 * without calling abort this will be 0.
1652 *
1653 * If the journal has been aborted on this mount time -EROFS will
1654 * be returned.
1655 */
1656int journal_errno(journal_t *journal)
1657{
1658 int err;
1659
1660 spin_lock(&journal->j_state_lock);
1661 if (journal->j_flags & JFS_ABORT)
1662 err = -EROFS;
1663 else
1664 err = journal->j_errno;
1665 spin_unlock(&journal->j_state_lock);
1666 return err;
1667}
1668
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001669/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * int journal_clear_err () - clears the journal's error state
1671 * @journal: journal to act on.
1672 *
1673 * An error must be cleared or Acked to take a FS out of readonly
1674 * mode.
1675 */
1676int journal_clear_err(journal_t *journal)
1677{
1678 int err = 0;
1679
1680 spin_lock(&journal->j_state_lock);
1681 if (journal->j_flags & JFS_ABORT)
1682 err = -EROFS;
1683 else
1684 journal->j_errno = 0;
1685 spin_unlock(&journal->j_state_lock);
1686 return err;
1687}
1688
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001689/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * void journal_ack_err() - Ack journal err.
1691 * @journal: journal to act on.
1692 *
1693 * An error must be cleared or Acked to take a FS out of readonly
1694 * mode.
1695 */
1696void journal_ack_err(journal_t *journal)
1697{
1698 spin_lock(&journal->j_state_lock);
1699 if (journal->j_errno)
1700 journal->j_flags |= JFS_ACK_ERR;
1701 spin_unlock(&journal->j_state_lock);
1702}
1703
1704int journal_blocks_per_page(struct inode *inode)
1705{
1706 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1707}
1708
1709/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * Journal_head storage management
1711 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001712static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713#ifdef CONFIG_JBD_DEBUG
1714static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1715#endif
1716
1717static int journal_init_journal_head_cache(void)
1718{
1719 int retval;
1720
Al Viro1076d172008-03-29 03:07:18 +00001721 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 journal_head_cache = kmem_cache_create("journal_head",
1723 sizeof(struct journal_head),
1724 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001725 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001726 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001728 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 retval = -ENOMEM;
1730 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1731 }
1732 return retval;
1733}
1734
1735static void journal_destroy_journal_head_cache(void)
1736{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001737 if (journal_head_cache) {
1738 kmem_cache_destroy(journal_head_cache);
1739 journal_head_cache = NULL;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743/*
1744 * journal_head splicing and dicing
1745 */
1746static struct journal_head *journal_alloc_journal_head(void)
1747{
1748 struct journal_head *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750#ifdef CONFIG_JBD_DEBUG
1751 atomic_inc(&nr_journal_heads);
1752#endif
1753 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001754 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 jbd_debug(1, "out of memory for journal_head\n");
Namhyung Kimf81e3d42010-10-04 19:12:13 +09001756 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1757 __func__);
1758
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001759 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 yield();
1761 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1762 }
1763 }
1764 return ret;
1765}
1766
1767static void journal_free_journal_head(struct journal_head *jh)
1768{
1769#ifdef CONFIG_JBD_DEBUG
1770 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001771 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772#endif
1773 kmem_cache_free(journal_head_cache, jh);
1774}
1775
1776/*
1777 * A journal_head is attached to a buffer_head whenever JBD has an
1778 * interest in the buffer.
1779 *
1780 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1781 * is set. This bit is tested in core kernel code where we need to take
1782 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1783 * there.
1784 *
1785 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1786 *
1787 * When a buffer has its BH_JBD bit set it is immune from being released by
1788 * core kernel code, mainly via ->b_count.
1789 *
1790 * A journal_head may be detached from its buffer_head when the journal_head's
1791 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1792 * Various places in JBD call journal_remove_journal_head() to indicate that the
1793 * journal_head can be dropped if needed.
1794 *
1795 * Various places in the kernel want to attach a journal_head to a buffer_head
1796 * _before_ attaching the journal_head to a transaction. To protect the
1797 * journal_head in this situation, journal_add_journal_head elevates the
1798 * journal_head's b_jcount refcount by one. The caller must call
1799 * journal_put_journal_head() to undo this.
1800 *
1801 * So the typical usage would be:
1802 *
1803 * (Attach a journal_head if needed. Increments b_jcount)
1804 * struct journal_head *jh = journal_add_journal_head(bh);
1805 * ...
1806 * jh->b_transaction = xxx;
1807 * journal_put_journal_head(jh);
1808 *
1809 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1810 * because it has a non-zero b_transaction.
1811 */
1812
1813/*
1814 * Give a buffer_head a journal_head.
1815 *
1816 * Doesn't need the journal lock.
1817 * May sleep.
1818 */
1819struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1820{
1821 struct journal_head *jh;
1822 struct journal_head *new_jh = NULL;
1823
1824repeat:
1825 if (!buffer_jbd(bh)) {
1826 new_jh = journal_alloc_journal_head();
1827 memset(new_jh, 0, sizeof(*new_jh));
1828 }
1829
1830 jbd_lock_bh_journal_head(bh);
1831 if (buffer_jbd(bh)) {
1832 jh = bh2jh(bh);
1833 } else {
1834 J_ASSERT_BH(bh,
1835 (atomic_read(&bh->b_count) > 0) ||
1836 (bh->b_page && bh->b_page->mapping));
1837
1838 if (!new_jh) {
1839 jbd_unlock_bh_journal_head(bh);
1840 goto repeat;
1841 }
1842
1843 jh = new_jh;
1844 new_jh = NULL; /* We consumed it */
1845 set_buffer_jbd(bh);
1846 bh->b_private = jh;
1847 jh->b_bh = bh;
1848 get_bh(bh);
1849 BUFFER_TRACE(bh, "added journal_head");
1850 }
1851 jh->b_jcount++;
1852 jbd_unlock_bh_journal_head(bh);
1853 if (new_jh)
1854 journal_free_journal_head(new_jh);
1855 return bh->b_private;
1856}
1857
1858/*
1859 * Grab a ref against this buffer_head's journal_head. If it ended up not
1860 * having a journal_head, return NULL
1861 */
1862struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1863{
1864 struct journal_head *jh = NULL;
1865
1866 jbd_lock_bh_journal_head(bh);
1867 if (buffer_jbd(bh)) {
1868 jh = bh2jh(bh);
1869 jh->b_jcount++;
1870 }
1871 jbd_unlock_bh_journal_head(bh);
1872 return jh;
1873}
1874
1875static void __journal_remove_journal_head(struct buffer_head *bh)
1876{
1877 struct journal_head *jh = bh2jh(bh);
1878
1879 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1880
1881 get_bh(bh);
1882 if (jh->b_jcount == 0) {
1883 if (jh->b_transaction == NULL &&
1884 jh->b_next_transaction == NULL &&
1885 jh->b_cp_transaction == NULL) {
1886 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1887 J_ASSERT_BH(bh, buffer_jbd(bh));
1888 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1889 BUFFER_TRACE(bh, "remove journal_head");
1890 if (jh->b_frozen_data) {
1891 printk(KERN_WARNING "%s: freeing "
1892 "b_frozen_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001893 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001894 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 if (jh->b_committed_data) {
1897 printk(KERN_WARNING "%s: freeing "
1898 "b_committed_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001899 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001900 jbd_free(jh->b_committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902 bh->b_private = NULL;
1903 jh->b_bh = NULL; /* debug, really */
1904 clear_buffer_jbd(bh);
1905 __brelse(bh);
1906 journal_free_journal_head(jh);
1907 } else {
1908 BUFFER_TRACE(bh, "journal_head was locked");
1909 }
1910 }
1911}
1912
1913/*
1914 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1915 * and has a zero b_jcount then remove and release its journal_head. If we did
1916 * see that the buffer is not used by any transaction we also "logically"
1917 * decrement ->b_count.
1918 *
1919 * We in fact take an additional increment on ->b_count as a convenience,
1920 * because the caller usually wants to do additional things with the bh
1921 * after calling here.
1922 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1923 * time. Once the caller has run __brelse(), the buffer is eligible for
1924 * reaping by try_to_free_buffers().
1925 */
1926void journal_remove_journal_head(struct buffer_head *bh)
1927{
1928 jbd_lock_bh_journal_head(bh);
1929 __journal_remove_journal_head(bh);
1930 jbd_unlock_bh_journal_head(bh);
1931}
1932
1933/*
1934 * Drop a reference on the passed journal_head. If it fell to zero then try to
1935 * release the journal_head from the buffer_head.
1936 */
1937void journal_put_journal_head(struct journal_head *jh)
1938{
1939 struct buffer_head *bh = jh2bh(jh);
1940
1941 jbd_lock_bh_journal_head(bh);
1942 J_ASSERT_JH(jh, jh->b_jcount > 0);
1943 --jh->b_jcount;
1944 if (!jh->b_jcount && !jh->b_transaction) {
1945 __journal_remove_journal_head(bh);
1946 __brelse(bh);
1947 }
1948 jbd_unlock_bh_journal_head(bh);
1949}
1950
1951/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07001952 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07001954#ifdef CONFIG_JBD_DEBUG
1955
1956u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Jose R. Santosc2a91592007-10-18 23:39:22 -07001959static struct dentry *jbd_debugfs_dir;
1960static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Jose R. Santosc2a91592007-10-18 23:39:22 -07001962static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001964 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1965 if (jbd_debugfs_dir)
Yin Kangkai765f8362009-12-15 14:48:25 -08001966 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
Jose R. Santosc2a91592007-10-18 23:39:22 -07001967 jbd_debugfs_dir,
1968 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Jose R. Santosc2a91592007-10-18 23:39:22 -07001971static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001973 debugfs_remove(jbd_debug);
1974 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977#else
1978
Jose R. Santosc2a91592007-10-18 23:39:22 -07001979static inline void jbd_create_debugfs_entry(void)
1980{
1981}
1982
1983static inline void jbd_remove_debugfs_entry(void)
1984{
1985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987#endif
1988
Christoph Lametere18b8902006-12-06 20:33:20 -08001989struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991static int __init journal_init_handle_cache(void)
1992{
1993 jbd_handle_cache = kmem_cache_create("journal_handle",
1994 sizeof(handle_t),
1995 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001996 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001997 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (jbd_handle_cache == NULL) {
1999 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2000 return -ENOMEM;
2001 }
2002 return 0;
2003}
2004
2005static void journal_destroy_handle_cache(void)
2006{
2007 if (jbd_handle_cache)
2008 kmem_cache_destroy(jbd_handle_cache);
2009}
2010
2011/*
2012 * Module startup and shutdown
2013 */
2014
2015static int __init journal_init_caches(void)
2016{
2017 int ret;
2018
2019 ret = journal_init_revoke_caches();
2020 if (ret == 0)
2021 ret = journal_init_journal_head_cache();
2022 if (ret == 0)
2023 ret = journal_init_handle_cache();
2024 return ret;
2025}
2026
2027static void journal_destroy_caches(void)
2028{
2029 journal_destroy_revoke_caches();
2030 journal_destroy_journal_head_cache();
2031 journal_destroy_handle_cache();
2032}
2033
2034static int __init journal_init(void)
2035{
2036 int ret;
2037
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07002038 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 ret = journal_init_caches();
2041 if (ret != 0)
2042 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07002043 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return ret;
2045}
2046
2047static void __exit journal_exit(void)
2048{
2049#ifdef CONFIG_JBD_DEBUG
2050 int n = atomic_read(&nr_journal_heads);
2051 if (n)
2052 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2053#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002054 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 journal_destroy_caches();
2056}
2057
2058MODULE_LICENSE("GPL");
2059module_init(journal_init);
2060module_exit(journal_exit);
2061