regmap: fix small typo in regmap_bulk_write comment
[deliverable/linux.git] / fs / jbd / journal.c
CommitLineData
1da177e4 1/*
f30c2269 2 * linux/fs/jbd/journal.c
1da177e4
LT
3 *
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>
1da177e4
LT
31#include <linux/init.h>
32#include <linux/mm.h>
7dfb7103 33#include <linux/freezer.h>
1da177e4 34#include <linux/pagemap.h>
8d8c8511 35#include <linux/kthread.h>
c9cf5528 36#include <linux/poison.h>
8d8c8511 37#include <linux/proc_fs.h>
c2a9159c 38#include <linux/debugfs.h>
f81e3d45 39#include <linux/ratelimit.h>
8d8c8511 40
99cb1a31
LC
41#define CREATE_TRACE_POINTS
42#include <trace/events/jbd.h>
43
1da177e4
LT
44#include <asm/uaccess.h>
45#include <asm/page.h>
1da177e4
LT
46
47EXPORT_SYMBOL(journal_start);
48EXPORT_SYMBOL(journal_restart);
49EXPORT_SYMBOL(journal_extend);
50EXPORT_SYMBOL(journal_stop);
51EXPORT_SYMBOL(journal_lock_updates);
52EXPORT_SYMBOL(journal_unlock_updates);
53EXPORT_SYMBOL(journal_get_write_access);
54EXPORT_SYMBOL(journal_get_create_access);
55EXPORT_SYMBOL(journal_get_undo_access);
56EXPORT_SYMBOL(journal_dirty_data);
57EXPORT_SYMBOL(journal_dirty_metadata);
58EXPORT_SYMBOL(journal_release_buffer);
59EXPORT_SYMBOL(journal_forget);
60#if 0
61EXPORT_SYMBOL(journal_sync_buffer);
62#endif
63EXPORT_SYMBOL(journal_flush);
64EXPORT_SYMBOL(journal_revoke);
65
66EXPORT_SYMBOL(journal_init_dev);
67EXPORT_SYMBOL(journal_init_inode);
68EXPORT_SYMBOL(journal_update_format);
69EXPORT_SYMBOL(journal_check_used_features);
70EXPORT_SYMBOL(journal_check_available_features);
71EXPORT_SYMBOL(journal_set_features);
72EXPORT_SYMBOL(journal_create);
73EXPORT_SYMBOL(journal_load);
74EXPORT_SYMBOL(journal_destroy);
1da177e4
LT
75EXPORT_SYMBOL(journal_abort);
76EXPORT_SYMBOL(journal_errno);
77EXPORT_SYMBOL(journal_ack_err);
78EXPORT_SYMBOL(journal_clear_err);
79EXPORT_SYMBOL(log_wait_commit);
ff5e4b51 80EXPORT_SYMBOL(log_start_commit);
1da177e4
LT
81EXPORT_SYMBOL(journal_start_commit);
82EXPORT_SYMBOL(journal_force_commit_nested);
83EXPORT_SYMBOL(journal_wipe);
84EXPORT_SYMBOL(journal_blocks_per_page);
85EXPORT_SYMBOL(journal_invalidatepage);
86EXPORT_SYMBOL(journal_try_to_free_buffers);
87EXPORT_SYMBOL(journal_force_commit);
88
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
022a4a7b 90static void __journal_abort_soft (journal_t *journal, int errno);
dff6825e 91static const char *journal_dev_name(journal_t *journal, char *buffer);
1da177e4
LT
92
93/*
94 * Helper function used to manage commit timeouts
95 */
96
97static void commit_timeout(unsigned long __data)
98{
99 struct task_struct * p = (struct task_struct *) __data;
100
101 wake_up_process(p);
102}
103
1da177e4
LT
104/*
105 * kjournald: The main thread function used to manage a logging device
106 * journal.
107 *
108 * This kernel thread is responsible for two things:
109 *
110 * 1) COMMIT: Every so often we need to commit the current state of the
111 * filesystem to disk. The journal thread is responsible for writing
112 * all of the metadata buffers to disk.
113 *
114 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115 * of the data in that part of the log has been rewritten elsewhere on
116 * the disk. Flushing these old buffers to reclaim space in the log is
117 * known as checkpointing, and this thread is responsible for that job.
118 */
119
022a4a7b 120static int kjournald(void *arg)
1da177e4 121{
e3df1898 122 journal_t *journal = arg;
1da177e4 123 transaction_t *transaction;
1da177e4 124
e3df1898
AM
125 /*
126 * Set up an interval timer which can be used to trigger a commit wakeup
127 * after the commit interval expires
128 */
129 setup_timer(&journal->j_commit_timer, commit_timeout,
130 (unsigned long)current);
1da177e4 131
35c80422
NC
132 set_freezable();
133
1da177e4
LT
134 /* Record that the journal thread is running */
135 journal->j_task = current;
136 wake_up(&journal->j_wait_done_commit);
137
138 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
139 journal->j_commit_interval / HZ);
140
141 /*
142 * And now, wait forever for commit wakeup events.
143 */
144 spin_lock(&journal->j_state_lock);
145
146loop:
147 if (journal->j_flags & JFS_UNMOUNT)
148 goto end_loop;
149
150 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
151 journal->j_commit_sequence, journal->j_commit_request);
152
153 if (journal->j_commit_sequence != journal->j_commit_request) {
154 jbd_debug(1, "OK, requests differ\n");
155 spin_unlock(&journal->j_state_lock);
e3df1898 156 del_timer_sync(&journal->j_commit_timer);
1da177e4
LT
157 journal_commit_transaction(journal);
158 spin_lock(&journal->j_state_lock);
159 goto loop;
160 }
161
162 wake_up(&journal->j_wait_done_commit);
3e1d1d28 163 if (freezing(current)) {
1da177e4
LT
164 /*
165 * The simpler the better. Flushing journal isn't a
166 * good idea, because that depends on threads that may
167 * be already stopped.
168 */
169 jbd_debug(1, "Now suspending kjournald\n");
170 spin_unlock(&journal->j_state_lock);
a0acae0e 171 try_to_freeze();
1da177e4
LT
172 spin_lock(&journal->j_state_lock);
173 } else {
174 /*
175 * We assume on resume that commits are already there,
176 * so we don't sleep
177 */
178 DEFINE_WAIT(wait);
179 int should_sleep = 1;
180
181 prepare_to_wait(&journal->j_wait_commit, &wait,
182 TASK_INTERRUPTIBLE);
183 if (journal->j_commit_sequence != journal->j_commit_request)
184 should_sleep = 0;
185 transaction = journal->j_running_transaction;
186 if (transaction && time_after_eq(jiffies,
187 transaction->t_expires))
188 should_sleep = 0;
cbf0d27a 189 if (journal->j_flags & JFS_UNMOUNT)
e9ad5620 190 should_sleep = 0;
1da177e4
LT
191 if (should_sleep) {
192 spin_unlock(&journal->j_state_lock);
193 schedule();
194 spin_lock(&journal->j_state_lock);
195 }
196 finish_wait(&journal->j_wait_commit, &wait);
197 }
198
199 jbd_debug(1, "kjournald wakes\n");
200
201 /*
202 * Were we woken up by a commit wakeup event?
203 */
204 transaction = journal->j_running_transaction;
205 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
206 journal->j_commit_request = transaction->t_tid;
207 jbd_debug(1, "woke because of timeout\n");
208 }
209 goto loop;
210
211end_loop:
212 spin_unlock(&journal->j_state_lock);
e3df1898 213 del_timer_sync(&journal->j_commit_timer);
1da177e4
LT
214 journal->j_task = NULL;
215 wake_up(&journal->j_wait_done_commit);
216 jbd_debug(1, "Journal thread exiting.\n");
217 return 0;
218}
219
97f06784 220static int journal_start_thread(journal_t *journal)
1da177e4 221{
97f06784
PE
222 struct task_struct *t;
223
224 t = kthread_run(kjournald, journal, "kjournald");
225 if (IS_ERR(t))
226 return PTR_ERR(t);
227
c80544dc 228 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
97f06784 229 return 0;
1da177e4
LT
230}
231
232static void journal_kill_thread(journal_t *journal)
233{
234 spin_lock(&journal->j_state_lock);
235 journal->j_flags |= JFS_UNMOUNT;
236
237 while (journal->j_task) {
238 wake_up(&journal->j_wait_commit);
239 spin_unlock(&journal->j_state_lock);
c80544dc
SH
240 wait_event(journal->j_wait_done_commit,
241 journal->j_task == NULL);
1da177e4
LT
242 spin_lock(&journal->j_state_lock);
243 }
244 spin_unlock(&journal->j_state_lock);
245}
246
247/*
248 * journal_write_metadata_buffer: write a metadata buffer to the journal.
249 *
250 * Writes a metadata buffer to a given disk block. The actual IO is not
251 * performed but a new buffer_head is constructed which labels the data
252 * to be written with the correct destination disk block.
253 *
254 * Any magic-number escaping which needs to be done will cause a
255 * copy-out here. If the buffer happens to start with the
256 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
257 * magic number is only written to the log for descripter blocks. In
258 * this case, we copy the data and replace the first word with 0, and we
259 * return a result code which indicates that this buffer needs to be
260 * marked as an escaped buffer in the corresponding log descriptor
261 * block. The missing word can then be restored when the block is read
262 * during recovery.
263 *
264 * If the source buffer has already been modified by a new transaction
265 * since we took the last commit snapshot, we use the frozen copy of
266 * that data for IO. If we end up using the existing buffer_head's data
267 * for the write, then we *have* to lock the buffer to prevent anyone
268 * else from using and possibly modifying it while the IO is in
269 * progress.
270 *
271 * The function returns a pointer to the buffer_heads to be used for IO.
272 *
273 * We assume that the journal has already been locked in this function.
274 *
275 * Return value:
276 * <0: Error
277 * >=0: Finished OK
278 *
279 * On success:
280 * Bit 0 set == escape performed on the data
281 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282 */
283
284int journal_write_metadata_buffer(transaction_t *transaction,
285 struct journal_head *jh_in,
286 struct journal_head **jh_out,
9c28cbcc 287 unsigned int blocknr)
1da177e4
LT
288{
289 int need_copy_out = 0;
290 int done_copy_out = 0;
291 int do_escape = 0;
292 char *mapped_data;
293 struct buffer_head *new_bh;
294 struct journal_head *new_jh;
295 struct page *new_page;
296 unsigned int new_offset;
297 struct buffer_head *bh_in = jh2bh(jh_in);
f1015c44 298 journal_t *journal = transaction->t_journal;
1da177e4
LT
299
300 /*
301 * The buffer really shouldn't be locked: only the current committing
302 * transaction is allowed to write it, so nobody else is allowed
303 * to do any IO.
304 *
305 * akpm: except if we're journalling data, and write() output is
306 * also part of a shared mapping, and another thread has
307 * decided to launch a writepage() against this buffer.
308 */
309 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
310
311 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
f1015c44 312 /* keep subsequent assertions sane */
313 new_bh->b_state = 0;
314 init_buffer(new_bh, NULL, NULL);
315 atomic_set(&new_bh->b_count, 1);
316 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
1da177e4
LT
317
318 /*
319 * If a new transaction has already done a buffer copy-out, then
320 * we use that version of the data for the commit.
321 */
322 jbd_lock_bh_state(bh_in);
323repeat:
324 if (jh_in->b_frozen_data) {
325 done_copy_out = 1;
326 new_page = virt_to_page(jh_in->b_frozen_data);
327 new_offset = offset_in_page(jh_in->b_frozen_data);
328 } else {
329 new_page = jh2bh(jh_in)->b_page;
330 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
331 }
332
8fb53c46 333 mapped_data = kmap_atomic(new_page);
1da177e4
LT
334 /*
335 * Check for escaping
336 */
337 if (*((__be32 *)(mapped_data + new_offset)) ==
338 cpu_to_be32(JFS_MAGIC_NUMBER)) {
339 need_copy_out = 1;
340 do_escape = 1;
341 }
8fb53c46 342 kunmap_atomic(mapped_data);
1da177e4
LT
343
344 /*
345 * Do we need to do a data copy?
346 */
347 if (need_copy_out && !done_copy_out) {
348 char *tmp;
349
350 jbd_unlock_bh_state(bh_in);
c089d490 351 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
1da177e4
LT
352 jbd_lock_bh_state(bh_in);
353 if (jh_in->b_frozen_data) {
c089d490 354 jbd_free(tmp, bh_in->b_size);
1da177e4
LT
355 goto repeat;
356 }
357
358 jh_in->b_frozen_data = tmp;
8fb53c46 359 mapped_data = kmap_atomic(new_page);
1da177e4 360 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
8fb53c46 361 kunmap_atomic(mapped_data);
1da177e4
LT
362
363 new_page = virt_to_page(tmp);
364 new_offset = offset_in_page(tmp);
365 done_copy_out = 1;
366 }
367
368 /*
369 * Did we need to do an escaping? Now we've done all the
370 * copying, we can finally do so.
371 */
372 if (do_escape) {
8fb53c46 373 mapped_data = kmap_atomic(new_page);
1da177e4 374 *((unsigned int *)(mapped_data + new_offset)) = 0;
8fb53c46 375 kunmap_atomic(mapped_data);
1da177e4
LT
376 }
377
1da177e4
LT
378 set_bh_page(new_bh, new_page, new_offset);
379 new_jh->b_transaction = NULL;
380 new_bh->b_size = jh2bh(jh_in)->b_size;
381 new_bh->b_bdev = transaction->t_journal->j_dev;
382 new_bh->b_blocknr = blocknr;
383 set_buffer_mapped(new_bh);
384 set_buffer_dirty(new_bh);
385
386 *jh_out = new_jh;
387
388 /*
389 * The to-be-written buffer needs to get moved to the io queue,
390 * and the original buffer whose contents we are shadowing or
391 * copying is moved to the transaction's shadow queue.
392 */
393 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
f1015c44 394 spin_lock(&journal->j_list_lock);
395 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
396 spin_unlock(&journal->j_list_lock);
397 jbd_unlock_bh_state(bh_in);
398
1da177e4
LT
399 JBUFFER_TRACE(new_jh, "file as BJ_IO");
400 journal_file_buffer(new_jh, transaction, BJ_IO);
401
402 return do_escape | (done_copy_out << 1);
403}
404
405/*
406 * Allocation code for the journal file. Manage the space left in the
407 * journal, so that we can begin checkpointing when appropriate.
408 */
409
410/*
411 * __log_space_left: Return the number of free blocks left in the journal.
412 *
413 * Called with the journal already locked.
414 *
415 * Called under j_state_lock
416 */
417
418int __log_space_left(journal_t *journal)
419{
420 int left = journal->j_free;
421
422 assert_spin_locked(&journal->j_state_lock);
423
424 /*
425 * Be pessimistic here about the number of those free blocks which
426 * might be required for log descriptor control blocks.
427 */
428
429#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
430
431 left -= MIN_LOG_RESERVED_BLOCKS;
432
433 if (left <= 0)
434 return 0;
435 left -= (left >> 3);
436 return left;
437}
438
439/*
8fe4cd0d 440 * Called under j_state_lock. Returns true if a transaction commit was started.
1da177e4
LT
441 */
442int __log_start_commit(journal_t *journal, tid_t target)
443{
444 /*
d9b01934
TT
445 * The only transaction we can possibly wait upon is the
446 * currently running transaction (if it exists). Otherwise,
447 * the target tid must be an old one.
1da177e4 448 */
7e2fb2d7
ES
449 if (journal->j_commit_request != target &&
450 journal->j_running_transaction &&
d9b01934 451 journal->j_running_transaction->t_tid == target) {
1da177e4 452 /*
bcf3d0bc 453 * We want a new commit: OK, mark the request and wakeup the
1da177e4
LT
454 * commit thread. We do _not_ do the commit ourselves.
455 */
456
457 journal->j_commit_request = target;
458 jbd_debug(1, "JBD: requesting commit %d/%d\n",
459 journal->j_commit_request,
460 journal->j_commit_sequence);
461 wake_up(&journal->j_wait_commit);
462 return 1;
d9b01934
TT
463 } else if (!tid_geq(journal->j_commit_request, target))
464 /* This should never happen, but if it does, preserve
465 the evidence before kjournald goes into a loop and
466 increments j_commit_sequence beyond all recognition. */
467 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
468 journal->j_commit_request, journal->j_commit_sequence,
469 target, journal->j_running_transaction ?
470 journal->j_running_transaction->t_tid : 0);
1da177e4
LT
471 return 0;
472}
473
474int log_start_commit(journal_t *journal, tid_t tid)
475{
476 int ret;
477
478 spin_lock(&journal->j_state_lock);
479 ret = __log_start_commit(journal, tid);
480 spin_unlock(&journal->j_state_lock);
481 return ret;
482}
483
484/*
485 * Force and wait upon a commit if the calling process is not within
486 * transaction. This is used for forcing out undo-protected data which contains
487 * bitmaps, when the fs is running out of space.
488 *
489 * We can only force the running transaction if we don't have an active handle;
490 * otherwise, we will deadlock.
491 *
492 * Returns true if a transaction was started.
493 */
494int journal_force_commit_nested(journal_t *journal)
495{
496 transaction_t *transaction = NULL;
497 tid_t tid;
498
499 spin_lock(&journal->j_state_lock);
500 if (journal->j_running_transaction && !current->journal_info) {
501 transaction = journal->j_running_transaction;
502 __log_start_commit(journal, transaction->t_tid);
503 } else if (journal->j_committing_transaction)
504 transaction = journal->j_committing_transaction;
505
506 if (!transaction) {
507 spin_unlock(&journal->j_state_lock);
508 return 0; /* Nothing to retry */
509 }
510
511 tid = transaction->t_tid;
512 spin_unlock(&journal->j_state_lock);
513 log_wait_commit(journal, tid);
514 return 1;
515}
516
517/*
518 * Start a commit of the current running transaction (if any). Returns true
8fe4cd0d
JK
519 * if a transaction is going to be committed (or is currently already
520 * committing), and fills its tid in at *ptid
1da177e4
LT
521 */
522int journal_start_commit(journal_t *journal, tid_t *ptid)
523{
524 int ret = 0;
525
526 spin_lock(&journal->j_state_lock);
527 if (journal->j_running_transaction) {
528 tid_t tid = journal->j_running_transaction->t_tid;
529
8fe4cd0d
JK
530 __log_start_commit(journal, tid);
531 /* There's a running transaction and we've just made sure
532 * it's commit has been scheduled. */
533 if (ptid)
1da177e4 534 *ptid = tid;
8fe4cd0d
JK
535 ret = 1;
536 } else if (journal->j_committing_transaction) {
1da177e4 537 /*
12810ad7
AB
538 * If commit has been started, then we have to wait for
539 * completion of that transaction.
1da177e4 540 */
8fe4cd0d
JK
541 if (ptid)
542 *ptid = journal->j_committing_transaction->t_tid;
1da177e4
LT
543 ret = 1;
544 }
545 spin_unlock(&journal->j_state_lock);
546 return ret;
547}
548
549/*
550 * Wait for a specified commit to complete.
551 * The caller may not hold the journal lock.
552 */
553int log_wait_commit(journal_t *journal, tid_t tid)
554{
555 int err = 0;
556
557#ifdef CONFIG_JBD_DEBUG
558 spin_lock(&journal->j_state_lock);
559 if (!tid_geq(journal->j_commit_request, tid)) {
560 printk(KERN_EMERG
561 "%s: error: j_commit_request=%d, tid=%d\n",
08fc99bf 562 __func__, journal->j_commit_request, tid);
1da177e4
LT
563 }
564 spin_unlock(&journal->j_state_lock);
565#endif
566 spin_lock(&journal->j_state_lock);
2db938be
JK
567 if (!tid_geq(journal->j_commit_waited, tid))
568 journal->j_commit_waited = tid;
1da177e4
LT
569 while (tid_gt(tid, journal->j_commit_sequence)) {
570 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
571 tid, journal->j_commit_sequence);
572 wake_up(&journal->j_wait_commit);
573 spin_unlock(&journal->j_state_lock);
574 wait_event(journal->j_wait_done_commit,
575 !tid_gt(tid, journal->j_commit_sequence));
576 spin_lock(&journal->j_state_lock);
577 }
578 spin_unlock(&journal->j_state_lock);
579
580 if (unlikely(is_journal_aborted(journal))) {
581 printk(KERN_EMERG "journal commit I/O error\n");
582 err = -EIO;
583 }
584 return err;
585}
586
03f4d804
JK
587/*
588 * Return 1 if a given transaction has not yet sent barrier request
589 * connected with a transaction commit. If 0 is returned, transaction
590 * may or may not have sent the barrier. Used to avoid sending barrier
591 * twice in common cases.
592 */
593int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
594{
595 int ret = 0;
596 transaction_t *commit_trans;
597
598 if (!(journal->j_flags & JFS_BARRIER))
599 return 0;
600 spin_lock(&journal->j_state_lock);
601 /* Transaction already committed? */
602 if (tid_geq(journal->j_commit_sequence, tid))
603 goto out;
604 /*
605 * Transaction is being committed and we already proceeded to
606 * writing commit record?
607 */
608 commit_trans = journal->j_committing_transaction;
609 if (commit_trans && commit_trans->t_tid == tid &&
610 commit_trans->t_state >= T_COMMIT_RECORD)
611 goto out;
612 ret = 1;
613out:
614 spin_unlock(&journal->j_state_lock);
615 return ret;
616}
52779708 617EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
03f4d804 618
1da177e4
LT
619/*
620 * Log buffer allocation routines:
621 */
622
9c28cbcc 623int journal_next_log_block(journal_t *journal, unsigned int *retp)
1da177e4 624{
9c28cbcc 625 unsigned int blocknr;
1da177e4
LT
626
627 spin_lock(&journal->j_state_lock);
628 J_ASSERT(journal->j_free > 1);
629
630 blocknr = journal->j_head;
631 journal->j_head++;
632 journal->j_free--;
633 if (journal->j_head == journal->j_last)
634 journal->j_head = journal->j_first;
635 spin_unlock(&journal->j_state_lock);
636 return journal_bmap(journal, blocknr, retp);
637}
638
639/*
640 * Conversion of logical to physical block numbers for the journal
641 *
642 * On external journals the journal blocks are identity-mapped, so
643 * this is a no-op. If needed, we can use j_blk_offset - everything is
644 * ready.
645 */
9c28cbcc
JK
646int journal_bmap(journal_t *journal, unsigned int blocknr,
647 unsigned int *retp)
1da177e4
LT
648{
649 int err = 0;
9c28cbcc 650 unsigned int ret;
1da177e4
LT
651
652 if (journal->j_inode) {
653 ret = bmap(journal->j_inode, blocknr);
654 if (ret)
655 *retp = ret;
656 else {
657 char b[BDEVNAME_SIZE];
658
659 printk(KERN_ALERT "%s: journal block not found "
9c28cbcc 660 "at offset %u on %s\n",
08fc99bf 661 __func__,
1da177e4
LT
662 blocknr,
663 bdevname(journal->j_dev, b));
664 err = -EIO;
665 __journal_abort_soft(journal, err);
666 }
667 } else {
668 *retp = blocknr; /* +journal->j_blk_offset */
669 }
670 return err;
671}
672
673/*
674 * We play buffer_head aliasing tricks to write data/metadata blocks to
675 * the journal without copying their contents, but for journal
676 * descriptor blocks we do need to generate bona fide buffers.
677 *
678 * After the caller of journal_get_descriptor_buffer() has finished modifying
679 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
680 * But we don't bother doing that, so there will be coherency problems with
681 * mmaps of blockdevs which hold live JBD-controlled filesystems.
682 */
683struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
684{
685 struct buffer_head *bh;
9c28cbcc 686 unsigned int blocknr;
1da177e4
LT
687 int err;
688
689 err = journal_next_log_block(journal, &blocknr);
690
691 if (err)
692 return NULL;
693
694 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
ecca9af0
JK
695 if (!bh)
696 return NULL;
1da177e4
LT
697 lock_buffer(bh);
698 memset(bh->b_data, 0, journal->j_blocksize);
699 set_buffer_uptodate(bh);
700 unlock_buffer(bh);
701 BUFFER_TRACE(bh, "return this buffer");
702 return journal_add_journal_head(bh);
703}
704
705/*
706 * Management for journal control blocks: functions to create and
707 * destroy journal_t structures, and to initialise and read existing
708 * journal blocks from disk. */
709
710/* First: create and setup a journal_t object in memory. We initialise
711 * very few fields yet: that has to wait until we have created the
712 * journal structures from from scratch, or loaded them from disk. */
713
714static journal_t * journal_init_common (void)
715{
716 journal_t *journal;
717 int err;
718
8c3478a5 719 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1da177e4
LT
720 if (!journal)
721 goto fail;
1da177e4
LT
722
723 init_waitqueue_head(&journal->j_wait_transaction_locked);
724 init_waitqueue_head(&journal->j_wait_logspace);
725 init_waitqueue_head(&journal->j_wait_done_commit);
726 init_waitqueue_head(&journal->j_wait_checkpoint);
727 init_waitqueue_head(&journal->j_wait_commit);
728 init_waitqueue_head(&journal->j_wait_updates);
2c68ee75 729 mutex_init(&journal->j_checkpoint_mutex);
1da177e4
LT
730 spin_lock_init(&journal->j_revoke_lock);
731 spin_lock_init(&journal->j_list_lock);
732 spin_lock_init(&journal->j_state_lock);
733
734 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
735
736 /* The journal is marked for error until we succeed with recovery! */
737 journal->j_flags = JFS_ABORT;
738
739 /* Set up a default-sized revoke table for the new mount. */
740 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
741 if (err) {
742 kfree(journal);
743 goto fail;
744 }
745 return journal;
746fail:
747 return NULL;
748}
749
750/* journal_init_dev and journal_init_inode:
751 *
752 * Create a journal structure assigned some fixed set of disk blocks to
753 * the journal. We don't actually touch those disk blocks yet, but we
754 * need to set up all of the mapping information to tell the journaling
755 * system where the journal blocks are.
756 *
757 */
758
759/**
0cf01f66 760 * journal_t * journal_init_dev() - creates and initialises a journal structure
1da177e4
LT
761 * @bdev: Block device on which to create the journal
762 * @fs_dev: Device which hold journalled filesystem for this journal.
763 * @start: Block nr Start of journal.
37ed3222 764 * @len: Length of the journal in blocks.
1da177e4 765 * @blocksize: blocksize of journalling device
0cf01f66
RD
766 *
767 * Returns: a newly created journal_t *
ae6ddcc5 768 *
1da177e4
LT
769 * journal_init_dev creates a journal which maps a fixed contiguous
770 * range of blocks on an arbitrary block device.
ae6ddcc5 771 *
1da177e4
LT
772 */
773journal_t * journal_init_dev(struct block_device *bdev,
774 struct block_device *fs_dev,
775 int start, int len, int blocksize)
776{
777 journal_t *journal = journal_init_common();
778 struct buffer_head *bh;
779 int n;
780
781 if (!journal)
782 return NULL;
783
1da177e4 784 /* journal descriptor can store up to n blocks -bzzz */
d1807793 785 journal->j_blocksize = blocksize;
1da177e4
LT
786 n = journal->j_blocksize / sizeof(journal_block_tag_t);
787 journal->j_wbufsize = n;
788 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
789 if (!journal->j_wbuf) {
25985edc 790 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
08fc99bf 791 __func__);
ecca9af0 792 goto out_err;
1da177e4 793 }
d1807793
ZM
794 journal->j_dev = bdev;
795 journal->j_fs_dev = fs_dev;
796 journal->j_blk_offset = start;
797 journal->j_maxlen = len;
798
799 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
ecca9af0
JK
800 if (!bh) {
801 printk(KERN_ERR
802 "%s: Cannot get buffer for journal superblock\n",
803 __func__);
804 goto out_err;
805 }
d1807793
ZM
806 journal->j_sb_buffer = bh;
807 journal->j_superblock = (journal_superblock_t *)bh->b_data;
ecca9af0 808
1da177e4 809 return journal;
ecca9af0 810out_err:
7b02bec0 811 kfree(journal->j_wbuf);
ecca9af0
JK
812 kfree(journal);
813 return NULL;
1da177e4 814}
ae6ddcc5
MC
815
816/**
1da177e4
LT
817 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
818 * @inode: An inode to create the journal in
ae6ddcc5 819 *
1da177e4
LT
820 * journal_init_inode creates a journal which maps an on-disk inode as
821 * the journal. The inode must exist already, must support bmap() and
822 * must have all data blocks preallocated.
823 */
824journal_t * journal_init_inode (struct inode *inode)
825{
826 struct buffer_head *bh;
827 journal_t *journal = journal_init_common();
828 int err;
829 int n;
9c28cbcc 830 unsigned int blocknr;
1da177e4
LT
831
832 if (!journal)
833 return NULL;
834
835 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
836 journal->j_inode = inode;
837 jbd_debug(1,
838 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
ae6ddcc5 839 journal, inode->i_sb->s_id, inode->i_ino,
1da177e4
LT
840 (long long) inode->i_size,
841 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
842
843 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
844 journal->j_blocksize = inode->i_sb->s_blocksize;
845
846 /* journal descriptor can store up to n blocks -bzzz */
847 n = journal->j_blocksize / sizeof(journal_block_tag_t);
848 journal->j_wbufsize = n;
849 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
850 if (!journal->j_wbuf) {
25985edc 851 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
08fc99bf 852 __func__);
ecca9af0 853 goto out_err;
1da177e4
LT
854 }
855
856 err = journal_bmap(journal, 0, &blocknr);
857 /* If that failed, give up */
858 if (err) {
3c26bdb4 859 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
08fc99bf 860 __func__);
ecca9af0 861 goto out_err;
1da177e4
LT
862 }
863
864 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
ecca9af0
JK
865 if (!bh) {
866 printk(KERN_ERR
867 "%s: Cannot get buffer for journal superblock\n",
868 __func__);
869 goto out_err;
870 }
1da177e4
LT
871 journal->j_sb_buffer = bh;
872 journal->j_superblock = (journal_superblock_t *)bh->b_data;
873
874 return journal;
ecca9af0 875out_err:
7b02bec0 876 kfree(journal->j_wbuf);
ecca9af0
JK
877 kfree(journal);
878 return NULL;
1da177e4
LT
879}
880
ae6ddcc5 881/*
1da177e4
LT
882 * If the journal init or create aborts, we need to mark the journal
883 * superblock as being NULL to prevent the journal destroy from writing
ae6ddcc5 884 * back a bogus superblock.
1da177e4
LT
885 */
886static void journal_fail_superblock (journal_t *journal)
887{
888 struct buffer_head *bh = journal->j_sb_buffer;
889 brelse(bh);
890 journal->j_sb_buffer = NULL;
891}
892
893/*
894 * Given a journal_t structure, initialise the various fields for
895 * startup of a new journaling session. We use this both when creating
896 * a journal, and after recovering an old journal to reset it for
897 * subsequent use.
898 */
899
900static int journal_reset(journal_t *journal)
901{
902 journal_superblock_t *sb = journal->j_superblock;
9c28cbcc 903 unsigned int first, last;
1da177e4
LT
904
905 first = be32_to_cpu(sb->s_first);
906 last = be32_to_cpu(sb->s_maxlen);
7447a668 907 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
9c28cbcc 908 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
7447a668
JK
909 first, last);
910 journal_fail_superblock(journal);
911 return -EINVAL;
912 }
1da177e4
LT
913
914 journal->j_first = first;
915 journal->j_last = last;
916
917 journal->j_head = first;
918 journal->j_tail = first;
919 journal->j_free = last - first;
920
921 journal->j_tail_sequence = journal->j_transaction_sequence;
922 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
923 journal->j_commit_request = journal->j_commit_sequence;
924
925 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
926
9754e39c
JK
927 /*
928 * As a special case, if the on-disk copy is already marked as needing
929 * no recovery (s_start == 0), then we can safely defer the superblock
930 * update until the next commit by setting JFS_FLUSHED. This avoids
931 * attempting a write to a potential-readonly device.
932 */
933 if (sb->s_start == 0) {
934 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
935 "(start %u, seq %d, errno %d)\n",
936 journal->j_tail, journal->j_tail_sequence,
937 journal->j_errno);
938 journal->j_flags |= JFS_FLUSHED;
939 } else {
1ce8486d
JK
940 /* Lock here to make assertions happy... */
941 mutex_lock(&journal->j_checkpoint_mutex);
fd2cbd4d
JK
942 /*
943 * Update log tail information. We use WRITE_FUA since new
944 * transaction will start reusing journal space and so we
945 * must make sure information about current log tail is on
946 * disk before that.
947 */
948 journal_update_sb_log_tail(journal,
949 journal->j_tail_sequence,
950 journal->j_tail,
951 WRITE_FUA);
1ce8486d 952 mutex_unlock(&journal->j_checkpoint_mutex);
9754e39c 953 }
97f06784 954 return journal_start_thread(journal);
1da177e4
LT
955}
956
ae6ddcc5 957/**
1da177e4
LT
958 * int journal_create() - Initialise the new journal file
959 * @journal: Journal to create. This structure must have been initialised
ae6ddcc5 960 *
1da177e4
LT
961 * Given a journal_t structure which tells us which disk blocks we can
962 * use, create a new journal superblock and initialise all of the
ae6ddcc5 963 * journal fields from scratch.
1da177e4
LT
964 **/
965int journal_create(journal_t *journal)
966{
9c28cbcc 967 unsigned int blocknr;
1da177e4
LT
968 struct buffer_head *bh;
969 journal_superblock_t *sb;
970 int i, err;
971
972 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
973 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
974 journal->j_maxlen);
975 journal_fail_superblock(journal);
976 return -EINVAL;
977 }
978
979 if (journal->j_inode == NULL) {
980 /*
981 * We don't know what block to start at!
982 */
983 printk(KERN_EMERG
984 "%s: creation of journal on external device!\n",
08fc99bf 985 __func__);
1da177e4
LT
986 BUG();
987 }
988
989 /* Zero out the entire journal on disk. We cannot afford to
990 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
991 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
992 for (i = 0; i < journal->j_maxlen; i++) {
993 err = journal_bmap(journal, i, &blocknr);
994 if (err)
995 return err;
996 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
2a0e3388
NK
997 if (unlikely(!bh))
998 return -ENOMEM;
1da177e4
LT
999 lock_buffer(bh);
1000 memset (bh->b_data, 0, journal->j_blocksize);
1001 BUFFER_TRACE(bh, "marking dirty");
1002 mark_buffer_dirty(bh);
1003 BUFFER_TRACE(bh, "marking uptodate");
1004 set_buffer_uptodate(bh);
1005 unlock_buffer(bh);
1006 __brelse(bh);
1007 }
1008
1009 sync_blockdev(journal->j_dev);
1010 jbd_debug(1, "JBD: journal cleared.\n");
1011
1012 /* OK, fill in the initial static fields in the new superblock */
1013 sb = journal->j_superblock;
1014
1015 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1016 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1017
1018 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1019 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1020 sb->s_first = cpu_to_be32(1);
1021
1022 journal->j_transaction_sequence = 1;
1023
1024 journal->j_flags &= ~JFS_ABORT;
1025 journal->j_format_version = 2;
1026
1027 return journal_reset(journal);
1028}
1029
fd2cbd4d 1030static void journal_write_superblock(journal_t *journal, int write_op)
1da177e4 1031{
1da177e4 1032 struct buffer_head *bh = journal->j_sb_buffer;
fd2cbd4d 1033 int ret;
1da177e4 1034
fd2cbd4d
JK
1035 trace_journal_write_superblock(journal, write_op);
1036 if (!(journal->j_flags & JFS_BARRIER))
1037 write_op &= ~(REQ_FUA | REQ_FLUSH);
1038 lock_buffer(bh);
dff6825e
DW
1039 if (buffer_write_io_error(bh)) {
1040 char b[BDEVNAME_SIZE];
1041 /*
1042 * Oh, dear. A previous attempt to write the journal
1043 * superblock failed. This could happen because the
1044 * USB device was yanked out. Or it could happen to
1045 * be a transient write error and maybe the block will
1046 * be remapped. Nothing we can do but to retry the
1047 * write and hope for the best.
1048 */
1049 printk(KERN_ERR "JBD: previous I/O error detected "
1050 "for journal superblock update for %s.\n",
1051 journal_dev_name(journal, b));
1052 clear_buffer_write_io_error(bh);
1053 set_buffer_uptodate(bh);
1054 }
1055
fd2cbd4d
JK
1056 get_bh(bh);
1057 bh->b_end_io = end_buffer_write_sync;
1058 ret = submit_bh(write_op, bh);
1059 wait_on_buffer(bh);
9754e39c 1060 if (buffer_write_io_error(bh)) {
9754e39c
JK
1061 clear_buffer_write_io_error(bh);
1062 set_buffer_uptodate(bh);
fd2cbd4d
JK
1063 ret = -EIO;
1064 }
1065 if (ret) {
1066 char b[BDEVNAME_SIZE];
1067 printk(KERN_ERR "JBD: Error %d detected "
1068 "when updating journal superblock for %s.\n",
1069 ret, journal_dev_name(journal, b));
9754e39c
JK
1070 }
1071}
1072
1073/**
1074 * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1075 * @journal: The journal to update.
fd2cbd4d
JK
1076 * @tail_tid: TID of the new transaction at the tail of the log
1077 * @tail_block: The first block of the transaction at the tail of the log
1078 * @write_op: With which operation should we write the journal sb
9754e39c
JK
1079 *
1080 * Update a journal's superblock information about log tail and write it to
1081 * disk, waiting for the IO to complete.
1082 */
fd2cbd4d
JK
1083void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1084 unsigned int tail_block, int write_op)
9754e39c
JK
1085{
1086 journal_superblock_t *sb = journal->j_superblock;
1087
1ce8486d 1088 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
fd2cbd4d
JK
1089 jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
1090 tail_block, tail_tid);
1da177e4 1091
fd2cbd4d
JK
1092 sb->s_sequence = cpu_to_be32(tail_tid);
1093 sb->s_start = cpu_to_be32(tail_block);
1da177e4 1094
fd2cbd4d 1095 journal_write_superblock(journal, write_op);
1da177e4 1096
9754e39c
JK
1097 /* Log is no longer empty */
1098 spin_lock(&journal->j_state_lock);
1099 WARN_ON(!sb->s_sequence);
1100 journal->j_flags &= ~JFS_FLUSHED;
1101 spin_unlock(&journal->j_state_lock);
1102}
1103
1104/**
1105 * mark_journal_empty() - Mark on disk journal as empty.
1106 * @journal: The journal to update.
1107 *
1108 * Update a journal's dynamic superblock fields to show that journal is empty.
1109 * Write updated superblock to disk waiting for IO to complete.
1110 */
1111static void mark_journal_empty(journal_t *journal)
1112{
1113 journal_superblock_t *sb = journal->j_superblock;
1da177e4 1114
1ce8486d 1115 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1da177e4 1116 spin_lock(&journal->j_state_lock);
2e84f264
JK
1117 /* Is it already empty? */
1118 if (sb->s_start == 0) {
1119 spin_unlock(&journal->j_state_lock);
1120 return;
1121 }
9754e39c
JK
1122 jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1123 journal->j_tail_sequence);
1124
1125 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1126 sb->s_start = cpu_to_be32(0);
1127 spin_unlock(&journal->j_state_lock);
1128
fd2cbd4d 1129 journal_write_superblock(journal, WRITE_FUA);
9754e39c
JK
1130
1131 spin_lock(&journal->j_state_lock);
1132 /* Log is empty */
1133 journal->j_flags |= JFS_FLUSHED;
1da177e4
LT
1134 spin_unlock(&journal->j_state_lock);
1135}
1136
9754e39c
JK
1137/**
1138 * journal_update_sb_errno() - Update error in the journal.
1139 * @journal: The journal to update.
1140 *
1141 * Update a journal's errno. Write updated superblock to disk waiting for IO
1142 * to complete.
1143 */
1144static void journal_update_sb_errno(journal_t *journal)
1145{
1146 journal_superblock_t *sb = journal->j_superblock;
1147
1148 spin_lock(&journal->j_state_lock);
1149 jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1150 journal->j_errno);
1151 sb->s_errno = cpu_to_be32(journal->j_errno);
1152 spin_unlock(&journal->j_state_lock);
1153
fd2cbd4d 1154 journal_write_superblock(journal, WRITE_SYNC);
9754e39c
JK
1155}
1156
1da177e4
LT
1157/*
1158 * Read the superblock for a given journal, performing initial
1159 * validation of the format.
1160 */
1161
1162static int journal_get_superblock(journal_t *journal)
1163{
1164 struct buffer_head *bh;
1165 journal_superblock_t *sb;
1166 int err = -EIO;
1167
1168 bh = journal->j_sb_buffer;
1169
1170 J_ASSERT(bh != NULL);
1171 if (!buffer_uptodate(bh)) {
1172 ll_rw_block(READ, 1, &bh);
1173 wait_on_buffer(bh);
1174 if (!buffer_uptodate(bh)) {
1175 printk (KERN_ERR
1176 "JBD: IO error reading journal superblock\n");
1177 goto out;
1178 }
1179 }
1180
1181 sb = journal->j_superblock;
1182
1183 err = -EINVAL;
1184
1185 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1186 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1187 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1188 goto out;
1189 }
1190
1191 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1192 case JFS_SUPERBLOCK_V1:
1193 journal->j_format_version = 1;
1194 break;
1195 case JFS_SUPERBLOCK_V2:
1196 journal->j_format_version = 2;
1197 break;
1198 default:
1199 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1200 goto out;
1201 }
1202
1203 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1204 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1205 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1206 printk (KERN_WARNING "JBD: journal file too short\n");
1207 goto out;
1208 }
1209
8762202d
EG
1210 if (be32_to_cpu(sb->s_first) == 0 ||
1211 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1212 printk(KERN_WARNING
1213 "JBD: Invalid start block of journal: %u\n",
1214 be32_to_cpu(sb->s_first));
1215 goto out;
1216 }
1217
1da177e4
LT
1218 return 0;
1219
1220out:
1221 journal_fail_superblock(journal);
1222 return err;
1223}
1224
1225/*
1226 * Load the on-disk journal superblock and read the key fields into the
1227 * journal_t.
1228 */
1229
1230static int load_superblock(journal_t *journal)
1231{
1232 int err;
1233 journal_superblock_t *sb;
1234
1235 err = journal_get_superblock(journal);
1236 if (err)
1237 return err;
1238
1239 sb = journal->j_superblock;
1240
1241 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1242 journal->j_tail = be32_to_cpu(sb->s_start);
1243 journal->j_first = be32_to_cpu(sb->s_first);
1244 journal->j_last = be32_to_cpu(sb->s_maxlen);
1245 journal->j_errno = be32_to_cpu(sb->s_errno);
1246
1247 return 0;
1248}
1249
1250
1251/**
1252 * int journal_load() - Read journal from disk.
1253 * @journal: Journal to act on.
ae6ddcc5 1254 *
1da177e4
LT
1255 * Given a journal_t structure which tells us which disk blocks contain
1256 * a journal, read the journal from disk to initialise the in-memory
1257 * structures.
1258 */
1259int journal_load(journal_t *journal)
1260{
1261 int err;
ea817398 1262 journal_superblock_t *sb;
1da177e4
LT
1263
1264 err = load_superblock(journal);
1265 if (err)
1266 return err;
1267
ea817398 1268 sb = journal->j_superblock;
1da177e4
LT
1269 /* If this is a V2 superblock, then we have to check the
1270 * features flags on it. */
1271
1272 if (journal->j_format_version >= 2) {
1da177e4
LT
1273 if ((sb->s_feature_ro_compat &
1274 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1275 (sb->s_feature_incompat &
1276 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1277 printk (KERN_WARNING
1278 "JBD: Unrecognised features on journal\n");
1279 return -EINVAL;
1280 }
1281 }
1282
1283 /* Let the recovery code check whether it needs to recover any
1284 * data from the journal. */
1285 if (journal_recover(journal))
1286 goto recovery_error;
1287
1288 /* OK, we've finished with the dynamic journal bits:
1289 * reinitialise the dynamic contents of the superblock in memory
1290 * and reset them on disk. */
1291 if (journal_reset(journal))
1292 goto recovery_error;
1293
1294 journal->j_flags &= ~JFS_ABORT;
1295 journal->j_flags |= JFS_LOADED;
1296 return 0;
1297
1298recovery_error:
1299 printk (KERN_WARNING "JBD: recovery failed\n");
1300 return -EIO;
1301}
1302
1303/**
1304 * void journal_destroy() - Release a journal_t structure.
1305 * @journal: Journal to act on.
1306 *
1307 * Release a journal_t structure once it is no longer in use by the
1308 * journaled object.
4afe9785 1309 * Return <0 if we couldn't clean up the journal.
1da177e4 1310 */
4afe9785 1311int journal_destroy(journal_t *journal)
1da177e4 1312{
4afe9785
HK
1313 int err = 0;
1314
03f4d804 1315
1da177e4
LT
1316 /* Wait for the commit thread to wake up and die. */
1317 journal_kill_thread(journal);
1318
1319 /* Force a final log commit */
1320 if (journal->j_running_transaction)
1321 journal_commit_transaction(journal);
1322
1323 /* Force any old transactions to disk */
1324
1ce8486d
JK
1325 /* We cannot race with anybody but must keep assertions happy */
1326 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4
LT
1327 /* Totally anal locking here... */
1328 spin_lock(&journal->j_list_lock);
1329 while (journal->j_checkpoint_transactions != NULL) {
1330 spin_unlock(&journal->j_list_lock);
1331 log_do_checkpoint(journal);
1332 spin_lock(&journal->j_list_lock);
1333 }
1334
1335 J_ASSERT(journal->j_running_transaction == NULL);
1336 J_ASSERT(journal->j_committing_transaction == NULL);
1337 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1338 spin_unlock(&journal->j_list_lock);
1339
1da177e4 1340 if (journal->j_sb_buffer) {
4afe9785 1341 if (!is_journal_aborted(journal)) {
4afe9785
HK
1342 journal->j_tail_sequence =
1343 ++journal->j_transaction_sequence;
9754e39c
JK
1344 mark_journal_empty(journal);
1345 } else
4afe9785 1346 err = -EIO;
1da177e4
LT
1347 brelse(journal->j_sb_buffer);
1348 }
1ce8486d 1349 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4
LT
1350
1351 if (journal->j_inode)
1352 iput(journal->j_inode);
1353 if (journal->j_revoke)
1354 journal_destroy_revoke(journal);
1355 kfree(journal->j_wbuf);
1356 kfree(journal);
4afe9785
HK
1357
1358 return err;
1da177e4
LT
1359}
1360
1361
1362/**
1363 *int journal_check_used_features () - Check if features specified are used.
1364 * @journal: Journal to check.
1365 * @compat: bitmask of compatible features
1366 * @ro: bitmask of features that force read-only mount
1367 * @incompat: bitmask of incompatible features
ae6ddcc5 1368 *
1da177e4 1369 * Check whether the journal uses all of a given set of
ae6ddcc5 1370 * features. Return true (non-zero) if it does.
1da177e4
LT
1371 **/
1372
1373int journal_check_used_features (journal_t *journal, unsigned long compat,
1374 unsigned long ro, unsigned long incompat)
1375{
1376 journal_superblock_t *sb;
1377
1378 if (!compat && !ro && !incompat)
1379 return 1;
1380 if (journal->j_format_version == 1)
1381 return 0;
1382
1383 sb = journal->j_superblock;
1384
1385 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1386 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1387 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1388 return 1;
1389
1390 return 0;
1391}
1392
1393/**
1394 * int journal_check_available_features() - Check feature set in journalling layer
1395 * @journal: Journal to check.
1396 * @compat: bitmask of compatible features
1397 * @ro: bitmask of features that force read-only mount
1398 * @incompat: bitmask of incompatible features
ae6ddcc5 1399 *
1da177e4
LT
1400 * Check whether the journaling code supports the use of
1401 * all of a given set of features on this journal. Return true
1402 * (non-zero) if it can. */
1403
1404int journal_check_available_features (journal_t *journal, unsigned long compat,
1405 unsigned long ro, unsigned long incompat)
1406{
1da177e4
LT
1407 if (!compat && !ro && !incompat)
1408 return 1;
1409
1da177e4
LT
1410 /* We can support any known requested features iff the
1411 * superblock is in version 2. Otherwise we fail to support any
1412 * extended sb features. */
1413
1414 if (journal->j_format_version != 2)
1415 return 0;
1416
1417 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1418 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1419 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1420 return 1;
1421
1422 return 0;
1423}
1424
1425/**
1426 * int journal_set_features () - Mark a given journal feature in the superblock
1427 * @journal: Journal to act on.
1428 * @compat: bitmask of compatible features
1429 * @ro: bitmask of features that force read-only mount
1430 * @incompat: bitmask of incompatible features
1431 *
1432 * Mark a given journal feature as present on the
ae6ddcc5 1433 * superblock. Returns true if the requested features could be set.
1da177e4
LT
1434 *
1435 */
1436
1437int journal_set_features (journal_t *journal, unsigned long compat,
1438 unsigned long ro, unsigned long incompat)
1439{
1440 journal_superblock_t *sb;
1441
1442 if (journal_check_used_features(journal, compat, ro, incompat))
1443 return 1;
1444
1445 if (!journal_check_available_features(journal, compat, ro, incompat))
1446 return 0;
1447
1448 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1449 compat, ro, incompat);
1450
1451 sb = journal->j_superblock;
1452
1453 sb->s_feature_compat |= cpu_to_be32(compat);
1454 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1455 sb->s_feature_incompat |= cpu_to_be32(incompat);
1456
1457 return 1;
1458}
1459
1460
1461/**
1462 * int journal_update_format () - Update on-disk journal structure.
1463 * @journal: Journal to act on.
1464 *
1465 * Given an initialised but unloaded journal struct, poke about in the
1466 * on-disk structure to update it to the most recent supported version.
1467 */
1468int journal_update_format (journal_t *journal)
1469{
1470 journal_superblock_t *sb;
1471 int err;
1472
1473 err = journal_get_superblock(journal);
1474 if (err)
1475 return err;
1476
1477 sb = journal->j_superblock;
1478
1479 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1480 case JFS_SUPERBLOCK_V2:
1481 return 0;
1482 case JFS_SUPERBLOCK_V1:
1483 return journal_convert_superblock_v1(journal, sb);
1484 default:
1485 break;
1486 }
1487 return -EINVAL;
1488}
1489
1490static int journal_convert_superblock_v1(journal_t *journal,
1491 journal_superblock_t *sb)
1492{
1493 int offset, blocksize;
1494 struct buffer_head *bh;
1495
1496 printk(KERN_WARNING
1497 "JBD: Converting superblock from version 1 to 2.\n");
1498
1499 /* Pre-initialise new fields to zero */
1500 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1501 blocksize = be32_to_cpu(sb->s_blocksize);
1502 memset(&sb->s_feature_compat, 0, blocksize-offset);
1503
1504 sb->s_nr_users = cpu_to_be32(1);
1505 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1506 journal->j_format_version = 2;
1507
1508 bh = journal->j_sb_buffer;
1509 BUFFER_TRACE(bh, "marking dirty");
1510 mark_buffer_dirty(bh);
1511 sync_dirty_buffer(bh);
1512 return 0;
1513}
1514
1515
1516/**
1517 * int journal_flush () - Flush journal
1518 * @journal: Journal to act on.
ae6ddcc5 1519 *
1da177e4
LT
1520 * Flush all data for a given journal to disk and empty the journal.
1521 * Filesystems can use this when remounting readonly to ensure that
1522 * recovery does not need to happen on remount.
1523 */
1524
1525int journal_flush(journal_t *journal)
1526{
1527 int err = 0;
1528 transaction_t *transaction = NULL;
1da177e4
LT
1529
1530 spin_lock(&journal->j_state_lock);
1531
1532 /* Force everything buffered to the log... */
1533 if (journal->j_running_transaction) {
1534 transaction = journal->j_running_transaction;
1535 __log_start_commit(journal, transaction->t_tid);
1536 } else if (journal->j_committing_transaction)
1537 transaction = journal->j_committing_transaction;
1538
1539 /* Wait for the log commit to complete... */
1540 if (transaction) {
1541 tid_t tid = transaction->t_tid;
1542
1543 spin_unlock(&journal->j_state_lock);
1544 log_wait_commit(journal, tid);
1545 } else {
1546 spin_unlock(&journal->j_state_lock);
1547 }
1548
1549 /* ...and flush everything in the log out to disk. */
1550 spin_lock(&journal->j_list_lock);
1551 while (!err && journal->j_checkpoint_transactions != NULL) {
1552 spin_unlock(&journal->j_list_lock);
4afe9785 1553 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4 1554 err = log_do_checkpoint(journal);
4afe9785 1555 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4
LT
1556 spin_lock(&journal->j_list_lock);
1557 }
1558 spin_unlock(&journal->j_list_lock);
4afe9785
HK
1559
1560 if (is_journal_aborted(journal))
1561 return -EIO;
1562
1ce8486d 1563 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4
LT
1564 cleanup_journal_tail(journal);
1565
1566 /* Finally, mark the journal as really needing no recovery.
1567 * This sets s_start==0 in the underlying superblock, which is
1568 * the magic code for a fully-recovered superblock. Any future
1569 * commits of data to the journal will restore the current
1570 * s_start value. */
9754e39c 1571 mark_journal_empty(journal);
1ce8486d 1572 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4 1573 spin_lock(&journal->j_state_lock);
1da177e4
LT
1574 J_ASSERT(!journal->j_running_transaction);
1575 J_ASSERT(!journal->j_committing_transaction);
1576 J_ASSERT(!journal->j_checkpoint_transactions);
1577 J_ASSERT(journal->j_head == journal->j_tail);
1578 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1579 spin_unlock(&journal->j_state_lock);
4afe9785 1580 return 0;
1da177e4
LT
1581}
1582
1583/**
1584 * int journal_wipe() - Wipe journal contents
1585 * @journal: Journal to act on.
1586 * @write: flag (see below)
ae6ddcc5 1587 *
1da177e4
LT
1588 * Wipe out all of the contents of a journal, safely. This will produce
1589 * a warning if the journal contains any valid recovery information.
1590 * Must be called between journal_init_*() and journal_load().
1591 *
1592 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1593 * we merely suppress recovery.
1594 */
1595
1596int journal_wipe(journal_t *journal, int write)
1597{
1da177e4
LT
1598 int err = 0;
1599
1600 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1601
1602 err = load_superblock(journal);
1603 if (err)
1604 return err;
1605
1da177e4
LT
1606 if (!journal->j_tail)
1607 goto no_recovery;
1608
1609 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1610 write ? "Clearing" : "Ignoring");
1611
1612 err = journal_skip_recovery(journal);
1ce8486d
JK
1613 if (write) {
1614 /* Lock to make assertions happy... */
1615 mutex_lock(&journal->j_checkpoint_mutex);
9754e39c 1616 mark_journal_empty(journal);
1ce8486d
JK
1617 mutex_unlock(&journal->j_checkpoint_mutex);
1618 }
1da177e4
LT
1619
1620 no_recovery:
1621 return err;
1622}
1623
1624/*
1625 * journal_dev_name: format a character string to describe on what
1626 * device this journal is present.
1627 */
1628
022a4a7b 1629static const char *journal_dev_name(journal_t *journal, char *buffer)
1da177e4
LT
1630{
1631 struct block_device *bdev;
1632
1633 if (journal->j_inode)
1634 bdev = journal->j_inode->i_sb->s_bdev;
1635 else
1636 bdev = journal->j_dev;
1637
1638 return bdevname(bdev, buffer);
1639}
1640
1641/*
1642 * Journal abort has very specific semantics, which we describe
ae6ddcc5 1643 * for journal abort.
1da177e4
LT
1644 *
1645 * Two internal function, which provide abort to te jbd layer
1646 * itself are here.
1647 */
1648
1649/*
1650 * Quick version for internal journal use (doesn't lock the journal).
1651 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1652 * and don't attempt to make any other journal updates.
1653 */
53308383 1654static void __journal_abort_hard(journal_t *journal)
1da177e4
LT
1655{
1656 transaction_t *transaction;
1657 char b[BDEVNAME_SIZE];
1658
1659 if (journal->j_flags & JFS_ABORT)
1660 return;
1661
1662 printk(KERN_ERR "Aborting journal on device %s.\n",
1663 journal_dev_name(journal, b));
1664
1665 spin_lock(&journal->j_state_lock);
1666 journal->j_flags |= JFS_ABORT;
1667 transaction = journal->j_running_transaction;
1668 if (transaction)
1669 __log_start_commit(journal, transaction->t_tid);
1670 spin_unlock(&journal->j_state_lock);
1671}
1672
1673/* Soft abort: record the abort error status in the journal superblock,
1674 * but don't do any other IO. */
022a4a7b 1675static void __journal_abort_soft (journal_t *journal, int errno)
1da177e4
LT
1676{
1677 if (journal->j_flags & JFS_ABORT)
1678 return;
1679
1680 if (!journal->j_errno)
1681 journal->j_errno = errno;
1682
1683 __journal_abort_hard(journal);
1684
1685 if (errno)
9754e39c 1686 journal_update_sb_errno(journal);
1da177e4
LT
1687}
1688
1689/**
1690 * void journal_abort () - Shutdown the journal immediately.
1691 * @journal: the journal to shutdown.
1692 * @errno: an error number to record in the journal indicating
1693 * the reason for the shutdown.
1694 *
1695 * Perform a complete, immediate shutdown of the ENTIRE
1696 * journal (not of a single transaction). This operation cannot be
1697 * undone without closing and reopening the journal.
ae6ddcc5 1698 *
1da177e4
LT
1699 * The journal_abort function is intended to support higher level error
1700 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1701 * mode.
1702 *
1703 * Journal abort has very specific semantics. Any existing dirty,
1704 * unjournaled buffers in the main filesystem will still be written to
1705 * disk by bdflush, but the journaling mechanism will be suspended
1706 * immediately and no further transaction commits will be honoured.
1707 *
1708 * Any dirty, journaled buffers will be written back to disk without
1709 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1710 * filesystem, but we _do_ attempt to leave as much data as possible
1711 * behind for fsck to use for cleanup.
1712 *
1713 * Any attempt to get a new transaction handle on a journal which is in
1714 * ABORT state will just result in an -EROFS error return. A
1715 * journal_stop on an existing handle will return -EIO if we have
1716 * entered abort state during the update.
1717 *
1718 * Recursive transactions are not disturbed by journal abort until the
1719 * final journal_stop, which will receive the -EIO error.
1720 *
1721 * Finally, the journal_abort call allows the caller to supply an errno
1722 * which will be recorded (if possible) in the journal superblock. This
1723 * allows a client to record failure conditions in the middle of a
1724 * transaction without having to complete the transaction to record the
1725 * failure to disk. ext3_error, for example, now uses this
1726 * functionality.
1727 *
1728 * Errors which originate from within the journaling layer will NOT
1729 * supply an errno; a null errno implies that absolutely no further
1730 * writes are done to the journal (unless there are any already in
1731 * progress).
ae6ddcc5 1732 *
1da177e4
LT
1733 */
1734
1735void journal_abort(journal_t *journal, int errno)
1736{
1737 __journal_abort_soft(journal, errno);
1738}
1739
ae6ddcc5 1740/**
1da177e4
LT
1741 * int journal_errno () - returns the journal's error state.
1742 * @journal: journal to examine.
1743 *
1744 * This is the errno numbet set with journal_abort(), the last
1745 * time the journal was mounted - if the journal was stopped
1746 * without calling abort this will be 0.
1747 *
1748 * If the journal has been aborted on this mount time -EROFS will
1749 * be returned.
1750 */
1751int journal_errno(journal_t *journal)
1752{
1753 int err;
1754
1755 spin_lock(&journal->j_state_lock);
1756 if (journal->j_flags & JFS_ABORT)
1757 err = -EROFS;
1758 else
1759 err = journal->j_errno;
1760 spin_unlock(&journal->j_state_lock);
1761 return err;
1762}
1763
ae6ddcc5 1764/**
1da177e4
LT
1765 * int journal_clear_err () - clears the journal's error state
1766 * @journal: journal to act on.
1767 *
1768 * An error must be cleared or Acked to take a FS out of readonly
1769 * mode.
1770 */
1771int journal_clear_err(journal_t *journal)
1772{
1773 int err = 0;
1774
1775 spin_lock(&journal->j_state_lock);
1776 if (journal->j_flags & JFS_ABORT)
1777 err = -EROFS;
1778 else
1779 journal->j_errno = 0;
1780 spin_unlock(&journal->j_state_lock);
1781 return err;
1782}
1783
ae6ddcc5 1784/**
1da177e4
LT
1785 * void journal_ack_err() - Ack journal err.
1786 * @journal: journal to act on.
1787 *
1788 * An error must be cleared or Acked to take a FS out of readonly
1789 * mode.
1790 */
1791void journal_ack_err(journal_t *journal)
1792{
1793 spin_lock(&journal->j_state_lock);
1794 if (journal->j_errno)
1795 journal->j_flags |= JFS_ACK_ERR;
1796 spin_unlock(&journal->j_state_lock);
1797}
1798
1799int journal_blocks_per_page(struct inode *inode)
1800{
1801 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1802}
1803
1da177e4
LT
1804/*
1805 * Journal_head storage management
1806 */
e18b890b 1807static struct kmem_cache *journal_head_cache;
1da177e4
LT
1808#ifdef CONFIG_JBD_DEBUG
1809static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1810#endif
1811
1812static int journal_init_journal_head_cache(void)
1813{
1814 int retval;
1815
1076d17a 1816 J_ASSERT(journal_head_cache == NULL);
1da177e4
LT
1817 journal_head_cache = kmem_cache_create("journal_head",
1818 sizeof(struct journal_head),
1819 0, /* offset */
e12ba74d 1820 SLAB_TEMPORARY, /* flags */
20c2df83 1821 NULL); /* ctor */
1da177e4 1822 retval = 0;
1076d17a 1823 if (!journal_head_cache) {
1da177e4
LT
1824 retval = -ENOMEM;
1825 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1826 }
1827 return retval;
1828}
1829
1830static void journal_destroy_journal_head_cache(void)
1831{
3850f7a5
DG
1832 if (journal_head_cache) {
1833 kmem_cache_destroy(journal_head_cache);
1834 journal_head_cache = NULL;
1835 }
1da177e4
LT
1836}
1837
1838/*
1839 * journal_head splicing and dicing
1840 */
1841static struct journal_head *journal_alloc_journal_head(void)
1842{
1843 struct journal_head *ret;
1da177e4
LT
1844
1845#ifdef CONFIG_JBD_DEBUG
1846 atomic_inc(&nr_journal_heads);
1847#endif
1848 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
c80544dc 1849 if (ret == NULL) {
1da177e4 1850 jbd_debug(1, "out of memory for journal_head\n");
f81e3d45
NK
1851 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1852 __func__);
1853
c80544dc 1854 while (ret == NULL) {
1da177e4
LT
1855 yield();
1856 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1857 }
1858 }
1859 return ret;
1860}
1861
1862static void journal_free_journal_head(struct journal_head *jh)
1863{
1864#ifdef CONFIG_JBD_DEBUG
1865 atomic_dec(&nr_journal_heads);
c9cf5528 1866 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1da177e4
LT
1867#endif
1868 kmem_cache_free(journal_head_cache, jh);
1869}
1870
1871/*
1872 * A journal_head is attached to a buffer_head whenever JBD has an
1873 * interest in the buffer.
1874 *
1875 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1876 * is set. This bit is tested in core kernel code where we need to take
1877 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1878 * there.
1879 *
1880 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1881 *
1882 * When a buffer has its BH_JBD bit set it is immune from being released by
1883 * core kernel code, mainly via ->b_count.
1884 *
bb189247
JK
1885 * A journal_head is detached from its buffer_head when the journal_head's
1886 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1887 * transaction (b_cp_transaction) hold their references to b_jcount.
1da177e4
LT
1888 *
1889 * Various places in the kernel want to attach a journal_head to a buffer_head
1890 * _before_ attaching the journal_head to a transaction. To protect the
1891 * journal_head in this situation, journal_add_journal_head elevates the
1892 * journal_head's b_jcount refcount by one. The caller must call
1893 * journal_put_journal_head() to undo this.
1894 *
1895 * So the typical usage would be:
1896 *
1897 * (Attach a journal_head if needed. Increments b_jcount)
1898 * struct journal_head *jh = journal_add_journal_head(bh);
1899 * ...
bb189247
JK
1900 * (Get another reference for transaction)
1901 * journal_grab_journal_head(bh);
1902 * jh->b_transaction = xxx;
1903 * (Put original reference)
1904 * journal_put_journal_head(jh);
1da177e4
LT
1905 */
1906
1907/*
1908 * Give a buffer_head a journal_head.
1909 *
1da177e4
LT
1910 * May sleep.
1911 */
1912struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1913{
1914 struct journal_head *jh;
1915 struct journal_head *new_jh = NULL;
1916
1917repeat:
1918 if (!buffer_jbd(bh)) {
1919 new_jh = journal_alloc_journal_head();
1920 memset(new_jh, 0, sizeof(*new_jh));
1921 }
1922
1923 jbd_lock_bh_journal_head(bh);
1924 if (buffer_jbd(bh)) {
1925 jh = bh2jh(bh);
1926 } else {
1927 J_ASSERT_BH(bh,
1928 (atomic_read(&bh->b_count) > 0) ||
1929 (bh->b_page && bh->b_page->mapping));
1930
1931 if (!new_jh) {
1932 jbd_unlock_bh_journal_head(bh);
1933 goto repeat;
1934 }
1935
1936 jh = new_jh;
1937 new_jh = NULL; /* We consumed it */
1938 set_buffer_jbd(bh);
1939 bh->b_private = jh;
1940 jh->b_bh = bh;
1941 get_bh(bh);
1942 BUFFER_TRACE(bh, "added journal_head");
1943 }
1944 jh->b_jcount++;
1945 jbd_unlock_bh_journal_head(bh);
1946 if (new_jh)
1947 journal_free_journal_head(new_jh);
1948 return bh->b_private;
1949}
1950
1951/*
1952 * Grab a ref against this buffer_head's journal_head. If it ended up not
1953 * having a journal_head, return NULL
1954 */
1955struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1956{
1957 struct journal_head *jh = NULL;
1958
1959 jbd_lock_bh_journal_head(bh);
1960 if (buffer_jbd(bh)) {
1961 jh = bh2jh(bh);
1962 jh->b_jcount++;
1963 }
1964 jbd_unlock_bh_journal_head(bh);
1965 return jh;
1966}
1967
1968static void __journal_remove_journal_head(struct buffer_head *bh)
1969{
1970 struct journal_head *jh = bh2jh(bh);
1971
1972 J_ASSERT_JH(jh, jh->b_jcount >= 0);
bb189247
JK
1973 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1974 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1975 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1976 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1977 J_ASSERT_BH(bh, buffer_jbd(bh));
1978 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1979 BUFFER_TRACE(bh, "remove journal_head");
1980 if (jh->b_frozen_data) {
1981 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1982 jbd_free(jh->b_frozen_data, bh->b_size);
1da177e4 1983 }
bb189247
JK
1984 if (jh->b_committed_data) {
1985 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1986 jbd_free(jh->b_committed_data, bh->b_size);
1987 }
1988 bh->b_private = NULL;
1989 jh->b_bh = NULL; /* debug, really */
1990 clear_buffer_jbd(bh);
1991 journal_free_journal_head(jh);
1da177e4
LT
1992}
1993
1994/*
bb189247 1995 * Drop a reference on the passed journal_head. If it fell to zero then
1da177e4
LT
1996 * release the journal_head from the buffer_head.
1997 */
1998void journal_put_journal_head(struct journal_head *jh)
1999{
2000 struct buffer_head *bh = jh2bh(jh);
2001
2002 jbd_lock_bh_journal_head(bh);
2003 J_ASSERT_JH(jh, jh->b_jcount > 0);
2004 --jh->b_jcount;
bb189247 2005 if (!jh->b_jcount) {
1da177e4 2006 __journal_remove_journal_head(bh);
bb189247 2007 jbd_unlock_bh_journal_head(bh);
1da177e4 2008 __brelse(bh);
bb189247
JK
2009 } else
2010 jbd_unlock_bh_journal_head(bh);
1da177e4
LT
2011}
2012
2013/*
c2a9159c 2014 * debugfs tunables
1da177e4 2015 */
c2a9159c 2016#ifdef CONFIG_JBD_DEBUG
1da177e4 2017
c2a9159c
JS
2018u8 journal_enable_debug __read_mostly;
2019EXPORT_SYMBOL(journal_enable_debug);
1da177e4 2020
c2a9159c
JS
2021static struct dentry *jbd_debugfs_dir;
2022static struct dentry *jbd_debug;
1da177e4 2023
c2a9159c 2024static void __init jbd_create_debugfs_entry(void)
1da177e4 2025{
c2a9159c
JS
2026 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2027 if (jbd_debugfs_dir)
765f8361 2028 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
c2a9159c
JS
2029 jbd_debugfs_dir,
2030 &journal_enable_debug);
1da177e4
LT
2031}
2032
c2a9159c 2033static void __exit jbd_remove_debugfs_entry(void)
1da177e4 2034{
c2a9159c
JS
2035 debugfs_remove(jbd_debug);
2036 debugfs_remove(jbd_debugfs_dir);
1da177e4
LT
2037}
2038
c2a9159c 2039#else
1da177e4 2040
c2a9159c 2041static inline void jbd_create_debugfs_entry(void)
1da177e4 2042{
1da177e4
LT
2043}
2044
c2a9159c 2045static inline void jbd_remove_debugfs_entry(void)
1da177e4 2046{
1da177e4
LT
2047}
2048
1da177e4
LT
2049#endif
2050
e18b890b 2051struct kmem_cache *jbd_handle_cache;
1da177e4
LT
2052
2053static int __init journal_init_handle_cache(void)
2054{
2055 jbd_handle_cache = kmem_cache_create("journal_handle",
2056 sizeof(handle_t),
2057 0, /* offset */
e12ba74d 2058 SLAB_TEMPORARY, /* flags */
20c2df83 2059 NULL); /* ctor */
1da177e4
LT
2060 if (jbd_handle_cache == NULL) {
2061 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2062 return -ENOMEM;
2063 }
2064 return 0;
2065}
2066
2067static void journal_destroy_handle_cache(void)
2068{
2069 if (jbd_handle_cache)
2070 kmem_cache_destroy(jbd_handle_cache);
2071}
2072
2073/*
2074 * Module startup and shutdown
2075 */
2076
2077static int __init journal_init_caches(void)
2078{
2079 int ret;
2080
2081 ret = journal_init_revoke_caches();
2082 if (ret == 0)
2083 ret = journal_init_journal_head_cache();
2084 if (ret == 0)
2085 ret = journal_init_handle_cache();
2086 return ret;
2087}
2088
2089static void journal_destroy_caches(void)
2090{
2091 journal_destroy_revoke_caches();
2092 journal_destroy_journal_head_cache();
2093 journal_destroy_handle_cache();
2094}
2095
2096static int __init journal_init(void)
2097{
2098 int ret;
2099
2aed3484 2100 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
022a4a7b 2101
1da177e4
LT
2102 ret = journal_init_caches();
2103 if (ret != 0)
2104 journal_destroy_caches();
c2a9159c 2105 jbd_create_debugfs_entry();
1da177e4
LT
2106 return ret;
2107}
2108
2109static void __exit journal_exit(void)
2110{
2111#ifdef CONFIG_JBD_DEBUG
2112 int n = atomic_read(&nr_journal_heads);
2113 if (n)
2114 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2115#endif
c2a9159c 2116 jbd_remove_debugfs_entry();
1da177e4
LT
2117 journal_destroy_caches();
2118}
2119
2120MODULE_LICENSE("GPL");
2121module_init(journal_init);
2122module_exit(journal_exit);
2123
This page took 1.103999 seconds and 5 git commands to generate.