]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/jbd2/journal.c
ext4: Change unsigned long to unsigned int
[mirror_ubuntu-hirsute-kernel.git] / fs / jbd2 / journal.c
CommitLineData
470decc6 1/*
f7f4bccb 2 * linux/fs/jbd2/journal.c
470decc6
DK
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>
f7f4bccb 28#include <linux/jbd2.h>
470decc6
DK
29#include <linux/errno.h>
30#include <linux/slab.h>
470decc6
DK
31#include <linux/init.h>
32#include <linux/mm.h>
7dfb7103 33#include <linux/freezer.h>
470decc6
DK
34#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
0f49d5d0 38#include <linux/debugfs.h>
8e85fb3f 39#include <linux/seq_file.h>
470decc6
DK
40
41#include <asm/uaccess.h>
42#include <asm/page.h>
d7cfa468 43#include <asm/div64.h>
470decc6 44
f7f4bccb
MC
45EXPORT_SYMBOL(jbd2_journal_start);
46EXPORT_SYMBOL(jbd2_journal_restart);
47EXPORT_SYMBOL(jbd2_journal_extend);
48EXPORT_SYMBOL(jbd2_journal_stop);
49EXPORT_SYMBOL(jbd2_journal_lock_updates);
50EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51EXPORT_SYMBOL(jbd2_journal_get_write_access);
52EXPORT_SYMBOL(jbd2_journal_get_create_access);
53EXPORT_SYMBOL(jbd2_journal_get_undo_access);
f7f4bccb
MC
54EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55EXPORT_SYMBOL(jbd2_journal_release_buffer);
56EXPORT_SYMBOL(jbd2_journal_forget);
470decc6
DK
57#if 0
58EXPORT_SYMBOL(journal_sync_buffer);
59#endif
f7f4bccb
MC
60EXPORT_SYMBOL(jbd2_journal_flush);
61EXPORT_SYMBOL(jbd2_journal_revoke);
62
63EXPORT_SYMBOL(jbd2_journal_init_dev);
64EXPORT_SYMBOL(jbd2_journal_init_inode);
65EXPORT_SYMBOL(jbd2_journal_update_format);
66EXPORT_SYMBOL(jbd2_journal_check_used_features);
67EXPORT_SYMBOL(jbd2_journal_check_available_features);
68EXPORT_SYMBOL(jbd2_journal_set_features);
69EXPORT_SYMBOL(jbd2_journal_create);
70EXPORT_SYMBOL(jbd2_journal_load);
71EXPORT_SYMBOL(jbd2_journal_destroy);
f7f4bccb
MC
72EXPORT_SYMBOL(jbd2_journal_abort);
73EXPORT_SYMBOL(jbd2_journal_errno);
74EXPORT_SYMBOL(jbd2_journal_ack_err);
75EXPORT_SYMBOL(jbd2_journal_clear_err);
76EXPORT_SYMBOL(jbd2_log_wait_commit);
77EXPORT_SYMBOL(jbd2_journal_start_commit);
78EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79EXPORT_SYMBOL(jbd2_journal_wipe);
80EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83EXPORT_SYMBOL(jbd2_journal_force_commit);
c851ed54
JK
84EXPORT_SYMBOL(jbd2_journal_file_inode);
85EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
86EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
87EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
470decc6
DK
88
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90static void __journal_abort_soft (journal_t *journal, int errno);
470decc6
DK
91
92/*
93 * Helper function used to manage commit timeouts
94 */
95
96static void commit_timeout(unsigned long __data)
97{
98 struct task_struct * p = (struct task_struct *) __data;
99
100 wake_up_process(p);
101}
102
103/*
f7f4bccb 104 * kjournald2: The main thread function used to manage a logging device
470decc6
DK
105 * journal.
106 *
107 * This kernel thread is responsible for two things:
108 *
109 * 1) COMMIT: Every so often we need to commit the current state of the
110 * filesystem to disk. The journal thread is responsible for writing
111 * all of the metadata buffers to disk.
112 *
113 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
114 * of the data in that part of the log has been rewritten elsewhere on
115 * the disk. Flushing these old buffers to reclaim space in the log is
116 * known as checkpointing, and this thread is responsible for that job.
117 */
118
f7f4bccb 119static int kjournald2(void *arg)
470decc6
DK
120{
121 journal_t *journal = arg;
122 transaction_t *transaction;
123
124 /*
125 * Set up an interval timer which can be used to trigger a commit wakeup
126 * after the commit interval expires
127 */
128 setup_timer(&journal->j_commit_timer, commit_timeout,
129 (unsigned long)current);
130
131 /* Record that the journal thread is running */
132 journal->j_task = current;
133 wake_up(&journal->j_wait_done_commit);
134
f7f4bccb 135 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
470decc6
DK
136 journal->j_commit_interval / HZ);
137
138 /*
139 * And now, wait forever for commit wakeup events.
140 */
141 spin_lock(&journal->j_state_lock);
142
143loop:
f7f4bccb 144 if (journal->j_flags & JBD2_UNMOUNT)
470decc6
DK
145 goto end_loop;
146
147 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
148 journal->j_commit_sequence, journal->j_commit_request);
149
150 if (journal->j_commit_sequence != journal->j_commit_request) {
151 jbd_debug(1, "OK, requests differ\n");
152 spin_unlock(&journal->j_state_lock);
153 del_timer_sync(&journal->j_commit_timer);
f7f4bccb 154 jbd2_journal_commit_transaction(journal);
470decc6
DK
155 spin_lock(&journal->j_state_lock);
156 goto loop;
157 }
158
159 wake_up(&journal->j_wait_done_commit);
160 if (freezing(current)) {
161 /*
162 * The simpler the better. Flushing journal isn't a
163 * good idea, because that depends on threads that may
164 * be already stopped.
165 */
f7f4bccb 166 jbd_debug(1, "Now suspending kjournald2\n");
470decc6
DK
167 spin_unlock(&journal->j_state_lock);
168 refrigerator();
169 spin_lock(&journal->j_state_lock);
170 } else {
171 /*
172 * We assume on resume that commits are already there,
173 * so we don't sleep
174 */
175 DEFINE_WAIT(wait);
176 int should_sleep = 1;
177
178 prepare_to_wait(&journal->j_wait_commit, &wait,
179 TASK_INTERRUPTIBLE);
180 if (journal->j_commit_sequence != journal->j_commit_request)
181 should_sleep = 0;
182 transaction = journal->j_running_transaction;
183 if (transaction && time_after_eq(jiffies,
184 transaction->t_expires))
185 should_sleep = 0;
f7f4bccb 186 if (journal->j_flags & JBD2_UNMOUNT)
470decc6
DK
187 should_sleep = 0;
188 if (should_sleep) {
189 spin_unlock(&journal->j_state_lock);
190 schedule();
191 spin_lock(&journal->j_state_lock);
192 }
193 finish_wait(&journal->j_wait_commit, &wait);
194 }
195
f7f4bccb 196 jbd_debug(1, "kjournald2 wakes\n");
470decc6
DK
197
198 /*
199 * Were we woken up by a commit wakeup event?
200 */
201 transaction = journal->j_running_transaction;
202 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
203 journal->j_commit_request = transaction->t_tid;
204 jbd_debug(1, "woke because of timeout\n");
205 }
206 goto loop;
207
208end_loop:
209 spin_unlock(&journal->j_state_lock);
210 del_timer_sync(&journal->j_commit_timer);
211 journal->j_task = NULL;
212 wake_up(&journal->j_wait_done_commit);
213 jbd_debug(1, "Journal thread exiting.\n");
214 return 0;
215}
216
97f06784 217static int jbd2_journal_start_thread(journal_t *journal)
470decc6 218{
97f06784
PE
219 struct task_struct *t;
220
221 t = kthread_run(kjournald2, journal, "kjournald2");
222 if (IS_ERR(t))
223 return PTR_ERR(t);
224
1076d17a 225 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
97f06784 226 return 0;
470decc6
DK
227}
228
229static void journal_kill_thread(journal_t *journal)
230{
231 spin_lock(&journal->j_state_lock);
f7f4bccb 232 journal->j_flags |= JBD2_UNMOUNT;
470decc6
DK
233
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
1076d17a 237 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
470decc6
DK
238 spin_lock(&journal->j_state_lock);
239 }
240 spin_unlock(&journal->j_state_lock);
241}
242
243/*
f7f4bccb 244 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
470decc6
DK
245 *
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
249 *
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
f7f4bccb 252 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
470decc6
DK
253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
258 * during recovery.
259 *
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
265 * progress.
266 *
267 * The function returns a pointer to the buffer_heads to be used for IO.
268 *
269 * We assume that the journal has already been locked in this function.
270 *
271 * Return value:
272 * <0: Error
273 * >=0: Finished OK
274 *
275 * On success:
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
278 */
279
f7f4bccb 280int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
470decc6
DK
281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
18eba7aa 283 unsigned long long blocknr)
470decc6
DK
284{
285 int need_copy_out = 0;
286 int done_copy_out = 0;
287 int do_escape = 0;
288 char *mapped_data;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
294
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);
307
308 /*
309 * If a new transaction has already done a buffer copy-out, then
310 * we use that version of the data for the commit.
311 */
312 jbd_lock_bh_state(bh_in);
313repeat:
314 if (jh_in->b_frozen_data) {
315 done_copy_out = 1;
316 new_page = virt_to_page(jh_in->b_frozen_data);
317 new_offset = offset_in_page(jh_in->b_frozen_data);
318 } else {
319 new_page = jh2bh(jh_in)->b_page;
320 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
321 }
322
323 mapped_data = kmap_atomic(new_page, KM_USER0);
324 /*
325 * Check for escaping
326 */
327 if (*((__be32 *)(mapped_data + new_offset)) ==
f7f4bccb 328 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
470decc6
DK
329 need_copy_out = 1;
330 do_escape = 1;
331 }
332 kunmap_atomic(mapped_data, KM_USER0);
333
334 /*
335 * Do we need to do a data copy?
336 */
337 if (need_copy_out && !done_copy_out) {
338 char *tmp;
339
340 jbd_unlock_bh_state(bh_in);
af1e76d6 341 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
470decc6
DK
342 jbd_lock_bh_state(bh_in);
343 if (jh_in->b_frozen_data) {
af1e76d6 344 jbd2_free(tmp, bh_in->b_size);
470decc6
DK
345 goto repeat;
346 }
347
348 jh_in->b_frozen_data = tmp;
349 mapped_data = kmap_atomic(new_page, KM_USER0);
350 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
351 kunmap_atomic(mapped_data, KM_USER0);
352
353 new_page = virt_to_page(tmp);
354 new_offset = offset_in_page(tmp);
355 done_copy_out = 1;
356 }
357
358 /*
359 * Did we need to do an escaping? Now we've done all the
360 * copying, we can finally do so.
361 */
362 if (do_escape) {
363 mapped_data = kmap_atomic(new_page, KM_USER0);
364 *((unsigned int *)(mapped_data + new_offset)) = 0;
365 kunmap_atomic(mapped_data, KM_USER0);
366 }
367
368 /* keep subsequent assertions sane */
369 new_bh->b_state = 0;
370 init_buffer(new_bh, NULL, NULL);
371 atomic_set(&new_bh->b_count, 1);
372 jbd_unlock_bh_state(bh_in);
373
f7f4bccb 374 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
470decc6
DK
375
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
383
384 *jh_out = new_jh;
385
386 /*
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
390 */
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
f7f4bccb 392 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
470decc6 393 JBUFFER_TRACE(new_jh, "file as BJ_IO");
f7f4bccb 394 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
470decc6
DK
395
396 return do_escape | (done_copy_out << 1);
397}
398
399/*
400 * Allocation code for the journal file. Manage the space left in the
401 * journal, so that we can begin checkpointing when appropriate.
402 */
403
404/*
f7f4bccb 405 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
470decc6
DK
406 *
407 * Called with the journal already locked.
408 *
409 * Called under j_state_lock
410 */
411
f7f4bccb 412int __jbd2_log_space_left(journal_t *journal)
470decc6
DK
413{
414 int left = journal->j_free;
415
416 assert_spin_locked(&journal->j_state_lock);
417
418 /*
419 * Be pessimistic here about the number of those free blocks which
420 * might be required for log descriptor control blocks.
421 */
422
423#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424
425 left -= MIN_LOG_RESERVED_BLOCKS;
426
427 if (left <= 0)
428 return 0;
429 left -= (left >> 3);
430 return left;
431}
432
433/*
434 * Called under j_state_lock. Returns true if a transaction was started.
435 */
f7f4bccb 436int __jbd2_log_start_commit(journal_t *journal, tid_t target)
470decc6
DK
437{
438 /*
439 * Are we already doing a recent enough commit?
440 */
441 if (!tid_geq(journal->j_commit_request, target)) {
442 /*
443 * We want a new commit: OK, mark the request and wakup the
444 * commit thread. We do _not_ do the commit ourselves.
445 */
446
447 journal->j_commit_request = target;
448 jbd_debug(1, "JBD: requesting commit %d/%d\n",
449 journal->j_commit_request,
450 journal->j_commit_sequence);
451 wake_up(&journal->j_wait_commit);
452 return 1;
453 }
454 return 0;
455}
456
f7f4bccb 457int jbd2_log_start_commit(journal_t *journal, tid_t tid)
470decc6
DK
458{
459 int ret;
460
461 spin_lock(&journal->j_state_lock);
f7f4bccb 462 ret = __jbd2_log_start_commit(journal, tid);
470decc6
DK
463 spin_unlock(&journal->j_state_lock);
464 return ret;
465}
466
467/*
468 * Force and wait upon a commit if the calling process is not within
469 * transaction. This is used for forcing out undo-protected data which contains
470 * bitmaps, when the fs is running out of space.
471 *
472 * We can only force the running transaction if we don't have an active handle;
473 * otherwise, we will deadlock.
474 *
475 * Returns true if a transaction was started.
476 */
f7f4bccb 477int jbd2_journal_force_commit_nested(journal_t *journal)
470decc6
DK
478{
479 transaction_t *transaction = NULL;
480 tid_t tid;
481
482 spin_lock(&journal->j_state_lock);
483 if (journal->j_running_transaction && !current->journal_info) {
484 transaction = journal->j_running_transaction;
f7f4bccb 485 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
486 } else if (journal->j_committing_transaction)
487 transaction = journal->j_committing_transaction;
488
489 if (!transaction) {
490 spin_unlock(&journal->j_state_lock);
491 return 0; /* Nothing to retry */
492 }
493
494 tid = transaction->t_tid;
495 spin_unlock(&journal->j_state_lock);
f7f4bccb 496 jbd2_log_wait_commit(journal, tid);
470decc6
DK
497 return 1;
498}
499
500/*
501 * Start a commit of the current running transaction (if any). Returns true
502 * if a transaction was started, and fills its tid in at *ptid
503 */
f7f4bccb 504int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
470decc6
DK
505{
506 int ret = 0;
507
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
511
f7f4bccb 512 ret = __jbd2_log_start_commit(journal, tid);
470decc6
DK
513 if (ret && ptid)
514 *ptid = tid;
515 } else if (journal->j_committing_transaction && ptid) {
516 /*
517 * If ext3_write_super() recently started a commit, then we
518 * have to wait for completion of that transaction
519 */
520 *ptid = journal->j_committing_transaction->t_tid;
521 ret = 1;
522 }
523 spin_unlock(&journal->j_state_lock);
524 return ret;
525}
526
527/*
528 * Wait for a specified commit to complete.
529 * The caller may not hold the journal lock.
530 */
f7f4bccb 531int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
470decc6
DK
532{
533 int err = 0;
534
e23291b9 535#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
536 spin_lock(&journal->j_state_lock);
537 if (!tid_geq(journal->j_commit_request, tid)) {
538 printk(KERN_EMERG
539 "%s: error: j_commit_request=%d, tid=%d\n",
329d291f 540 __func__, journal->j_commit_request, tid);
470decc6
DK
541 }
542 spin_unlock(&journal->j_state_lock);
543#endif
544 spin_lock(&journal->j_state_lock);
545 while (tid_gt(tid, journal->j_commit_sequence)) {
546 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
547 tid, journal->j_commit_sequence);
548 wake_up(&journal->j_wait_commit);
549 spin_unlock(&journal->j_state_lock);
550 wait_event(journal->j_wait_done_commit,
551 !tid_gt(tid, journal->j_commit_sequence));
552 spin_lock(&journal->j_state_lock);
553 }
554 spin_unlock(&journal->j_state_lock);
555
556 if (unlikely(is_journal_aborted(journal))) {
557 printk(KERN_EMERG "journal commit I/O error\n");
558 err = -EIO;
559 }
560 return err;
561}
562
563/*
564 * Log buffer allocation routines:
565 */
566
18eba7aa 567int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
470decc6
DK
568{
569 unsigned long blocknr;
570
571 spin_lock(&journal->j_state_lock);
572 J_ASSERT(journal->j_free > 1);
573
574 blocknr = journal->j_head;
575 journal->j_head++;
576 journal->j_free--;
577 if (journal->j_head == journal->j_last)
578 journal->j_head = journal->j_first;
579 spin_unlock(&journal->j_state_lock);
f7f4bccb 580 return jbd2_journal_bmap(journal, blocknr, retp);
470decc6
DK
581}
582
583/*
584 * Conversion of logical to physical block numbers for the journal
585 *
586 * On external journals the journal blocks are identity-mapped, so
587 * this is a no-op. If needed, we can use j_blk_offset - everything is
588 * ready.
589 */
f7f4bccb 590int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
18eba7aa 591 unsigned long long *retp)
470decc6
DK
592{
593 int err = 0;
18eba7aa 594 unsigned long long ret;
470decc6
DK
595
596 if (journal->j_inode) {
597 ret = bmap(journal->j_inode, blocknr);
598 if (ret)
599 *retp = ret;
600 else {
470decc6
DK
601 printk(KERN_ALERT "%s: journal block not found "
602 "at offset %lu on %s\n",
05496769 603 __func__, blocknr, journal->j_devname);
470decc6
DK
604 err = -EIO;
605 __journal_abort_soft(journal, err);
606 }
607 } else {
608 *retp = blocknr; /* +journal->j_blk_offset */
609 }
610 return err;
611}
612
613/*
614 * We play buffer_head aliasing tricks to write data/metadata blocks to
615 * the journal without copying their contents, but for journal
616 * descriptor blocks we do need to generate bona fide buffers.
617 *
f7f4bccb 618 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
470decc6
DK
619 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
620 * But we don't bother doing that, so there will be coherency problems with
621 * mmaps of blockdevs which hold live JBD-controlled filesystems.
622 */
f7f4bccb 623struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
470decc6
DK
624{
625 struct buffer_head *bh;
18eba7aa 626 unsigned long long blocknr;
470decc6
DK
627 int err;
628
f7f4bccb 629 err = jbd2_journal_next_log_block(journal, &blocknr);
470decc6
DK
630
631 if (err)
632 return NULL;
633
634 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
635 lock_buffer(bh);
636 memset(bh->b_data, 0, journal->j_blocksize);
637 set_buffer_uptodate(bh);
638 unlock_buffer(bh);
639 BUFFER_TRACE(bh, "return this buffer");
f7f4bccb 640 return jbd2_journal_add_journal_head(bh);
470decc6
DK
641}
642
8e85fb3f
JL
643struct jbd2_stats_proc_session {
644 journal_t *journal;
645 struct transaction_stats_s *stats;
646 int start;
647 int max;
648};
649
650static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
651 struct transaction_stats_s *ts,
652 int first)
653{
654 if (ts == s->stats + s->max)
655 ts = s->stats;
656 if (!first && ts == s->stats + s->start)
657 return NULL;
658 while (ts->ts_type == 0) {
659 ts++;
660 if (ts == s->stats + s->max)
661 ts = s->stats;
662 if (ts == s->stats + s->start)
663 return NULL;
664 }
665 return ts;
666
667}
668
669static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
670{
671 struct jbd2_stats_proc_session *s = seq->private;
672 struct transaction_stats_s *ts;
673 int l = *pos;
674
675 if (l == 0)
676 return SEQ_START_TOKEN;
677 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
678 if (!ts)
679 return NULL;
680 l--;
681 while (l) {
682 ts = jbd2_history_skip_empty(s, ++ts, 0);
683 if (!ts)
684 break;
685 l--;
686 }
687 return ts;
688}
689
690static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
691{
692 struct jbd2_stats_proc_session *s = seq->private;
693 struct transaction_stats_s *ts = v;
694
695 ++*pos;
696 if (v == SEQ_START_TOKEN)
697 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
698 else
699 return jbd2_history_skip_empty(s, ++ts, 0);
700}
701
702static int jbd2_seq_history_show(struct seq_file *seq, void *v)
703{
704 struct transaction_stats_s *ts = v;
705 if (v == SEQ_START_TOKEN) {
706 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
707 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
708 "wait", "run", "lock", "flush", "log", "hndls",
709 "block", "inlog", "ctime", "write", "drop",
710 "close");
711 return 0;
712 }
713 if (ts->ts_type == JBD2_STATS_RUN)
714 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
715 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
716 jiffies_to_msecs(ts->u.run.rs_wait),
717 jiffies_to_msecs(ts->u.run.rs_running),
718 jiffies_to_msecs(ts->u.run.rs_locked),
719 jiffies_to_msecs(ts->u.run.rs_flushing),
720 jiffies_to_msecs(ts->u.run.rs_logging),
721 ts->u.run.rs_handle_count,
722 ts->u.run.rs_blocks,
723 ts->u.run.rs_blocks_logged);
724 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
725 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
726 "C", ts->ts_tid, " ",
727 jiffies_to_msecs(ts->u.chp.cs_chp_time),
728 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
729 ts->u.chp.cs_forced_to_close);
730 else
731 J_ASSERT(0);
732 return 0;
733}
734
735static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
736{
737}
738
739static struct seq_operations jbd2_seq_history_ops = {
740 .start = jbd2_seq_history_start,
741 .next = jbd2_seq_history_next,
742 .stop = jbd2_seq_history_stop,
743 .show = jbd2_seq_history_show,
744};
745
746static int jbd2_seq_history_open(struct inode *inode, struct file *file)
747{
748 journal_t *journal = PDE(inode)->data;
749 struct jbd2_stats_proc_session *s;
750 int rc, size;
751
752 s = kmalloc(sizeof(*s), GFP_KERNEL);
753 if (s == NULL)
754 return -ENOMEM;
755 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
756 s->stats = kmalloc(size, GFP_KERNEL);
757 if (s->stats == NULL) {
758 kfree(s);
759 return -ENOMEM;
760 }
761 spin_lock(&journal->j_history_lock);
762 memcpy(s->stats, journal->j_history, size);
763 s->max = journal->j_history_max;
764 s->start = journal->j_history_cur % s->max;
765 spin_unlock(&journal->j_history_lock);
766
767 rc = seq_open(file, &jbd2_seq_history_ops);
768 if (rc == 0) {
769 struct seq_file *m = file->private_data;
770 m->private = s;
771 } else {
772 kfree(s->stats);
773 kfree(s);
774 }
775 return rc;
776
777}
778
779static int jbd2_seq_history_release(struct inode *inode, struct file *file)
780{
781 struct seq_file *seq = file->private_data;
782 struct jbd2_stats_proc_session *s = seq->private;
783
784 kfree(s->stats);
785 kfree(s);
786 return seq_release(inode, file);
787}
788
789static struct file_operations jbd2_seq_history_fops = {
790 .owner = THIS_MODULE,
791 .open = jbd2_seq_history_open,
792 .read = seq_read,
793 .llseek = seq_lseek,
794 .release = jbd2_seq_history_release,
795};
796
797static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
798{
799 return *pos ? NULL : SEQ_START_TOKEN;
800}
801
802static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
803{
804 return NULL;
805}
806
807static int jbd2_seq_info_show(struct seq_file *seq, void *v)
808{
809 struct jbd2_stats_proc_session *s = seq->private;
810
811 if (v != SEQ_START_TOKEN)
812 return 0;
813 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
814 s->stats->ts_tid,
815 s->journal->j_max_transaction_buffers);
816 if (s->stats->ts_tid == 0)
817 return 0;
818 seq_printf(seq, "average: \n %ums waiting for transaction\n",
819 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
820 seq_printf(seq, " %ums running transaction\n",
821 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
822 seq_printf(seq, " %ums transaction was being locked\n",
823 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
824 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
825 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
826 seq_printf(seq, " %ums logging transaction\n",
827 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
d7cfa468
TT
828 seq_printf(seq, " %luus average transaction commit time\n",
829 do_div(s->journal->j_average_commit_time, 1000));
8e85fb3f
JL
830 seq_printf(seq, " %lu handles per transaction\n",
831 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
832 seq_printf(seq, " %lu blocks per transaction\n",
833 s->stats->u.run.rs_blocks / s->stats->ts_tid);
834 seq_printf(seq, " %lu logged blocks per transaction\n",
835 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
836 return 0;
837}
838
839static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
840{
841}
842
843static struct seq_operations jbd2_seq_info_ops = {
844 .start = jbd2_seq_info_start,
845 .next = jbd2_seq_info_next,
846 .stop = jbd2_seq_info_stop,
847 .show = jbd2_seq_info_show,
848};
849
850static int jbd2_seq_info_open(struct inode *inode, struct file *file)
851{
852 journal_t *journal = PDE(inode)->data;
853 struct jbd2_stats_proc_session *s;
854 int rc, size;
855
856 s = kmalloc(sizeof(*s), GFP_KERNEL);
857 if (s == NULL)
858 return -ENOMEM;
859 size = sizeof(struct transaction_stats_s);
860 s->stats = kmalloc(size, GFP_KERNEL);
861 if (s->stats == NULL) {
862 kfree(s);
863 return -ENOMEM;
864 }
865 spin_lock(&journal->j_history_lock);
866 memcpy(s->stats, &journal->j_stats, size);
867 s->journal = journal;
868 spin_unlock(&journal->j_history_lock);
869
870 rc = seq_open(file, &jbd2_seq_info_ops);
871 if (rc == 0) {
872 struct seq_file *m = file->private_data;
873 m->private = s;
874 } else {
875 kfree(s->stats);
876 kfree(s);
877 }
878 return rc;
879
880}
881
882static int jbd2_seq_info_release(struct inode *inode, struct file *file)
883{
884 struct seq_file *seq = file->private_data;
885 struct jbd2_stats_proc_session *s = seq->private;
886 kfree(s->stats);
887 kfree(s);
888 return seq_release(inode, file);
889}
890
891static struct file_operations jbd2_seq_info_fops = {
892 .owner = THIS_MODULE,
893 .open = jbd2_seq_info_open,
894 .read = seq_read,
895 .llseek = seq_lseek,
896 .release = jbd2_seq_info_release,
897};
898
899static struct proc_dir_entry *proc_jbd2_stats;
900
901static void jbd2_stats_proc_init(journal_t *journal)
902{
05496769 903 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
8e85fb3f 904 if (journal->j_proc_entry) {
79da3664
DL
905 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
906 &jbd2_seq_history_fops, journal);
907 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
908 &jbd2_seq_info_fops, journal);
8e85fb3f
JL
909 }
910}
911
912static void jbd2_stats_proc_exit(journal_t *journal)
913{
8e85fb3f
JL
914 remove_proc_entry("info", journal->j_proc_entry);
915 remove_proc_entry("history", journal->j_proc_entry);
05496769 916 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
8e85fb3f
JL
917}
918
919static void journal_init_stats(journal_t *journal)
920{
921 int size;
922
923 if (!proc_jbd2_stats)
924 return;
925
926 journal->j_history_max = 100;
927 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
928 journal->j_history = kzalloc(size, GFP_KERNEL);
929 if (!journal->j_history) {
930 journal->j_history_max = 0;
931 return;
932 }
933 spin_lock_init(&journal->j_history_lock);
934}
935
470decc6
DK
936/*
937 * Management for journal control blocks: functions to create and
938 * destroy journal_t structures, and to initialise and read existing
939 * journal blocks from disk. */
940
941/* First: create and setup a journal_t object in memory. We initialise
942 * very few fields yet: that has to wait until we have created the
943 * journal structures from from scratch, or loaded them from disk. */
944
945static journal_t * journal_init_common (void)
946{
947 journal_t *journal;
948 int err;
949
d802ffa8 950 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
470decc6
DK
951 if (!journal)
952 goto fail;
470decc6
DK
953
954 init_waitqueue_head(&journal->j_wait_transaction_locked);
955 init_waitqueue_head(&journal->j_wait_logspace);
956 init_waitqueue_head(&journal->j_wait_done_commit);
957 init_waitqueue_head(&journal->j_wait_checkpoint);
958 init_waitqueue_head(&journal->j_wait_commit);
959 init_waitqueue_head(&journal->j_wait_updates);
960 mutex_init(&journal->j_barrier);
961 mutex_init(&journal->j_checkpoint_mutex);
962 spin_lock_init(&journal->j_revoke_lock);
963 spin_lock_init(&journal->j_list_lock);
964 spin_lock_init(&journal->j_state_lock);
965
cd02ff0b 966 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
30773840
TT
967 journal->j_min_batch_time = 0;
968 journal->j_max_batch_time = 15000; /* 15ms */
470decc6
DK
969
970 /* The journal is marked for error until we succeed with recovery! */
f7f4bccb 971 journal->j_flags = JBD2_ABORT;
470decc6
DK
972
973 /* Set up a default-sized revoke table for the new mount. */
f7f4bccb 974 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
470decc6
DK
975 if (err) {
976 kfree(journal);
977 goto fail;
978 }
8e85fb3f
JL
979
980 journal_init_stats(journal);
981
470decc6
DK
982 return journal;
983fail:
984 return NULL;
985}
986
f7f4bccb 987/* jbd2_journal_init_dev and jbd2_journal_init_inode:
470decc6
DK
988 *
989 * Create a journal structure assigned some fixed set of disk blocks to
990 * the journal. We don't actually touch those disk blocks yet, but we
991 * need to set up all of the mapping information to tell the journaling
992 * system where the journal blocks are.
993 *
994 */
995
996/**
5648ba5b 997 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
470decc6
DK
998 * @bdev: Block device on which to create the journal
999 * @fs_dev: Device which hold journalled filesystem for this journal.
1000 * @start: Block nr Start of journal.
1001 * @len: Length of the journal in blocks.
1002 * @blocksize: blocksize of journalling device
5648ba5b
RD
1003 *
1004 * Returns: a newly created journal_t *
470decc6 1005 *
f7f4bccb 1006 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
470decc6
DK
1007 * range of blocks on an arbitrary block device.
1008 *
1009 */
f7f4bccb 1010journal_t * jbd2_journal_init_dev(struct block_device *bdev,
470decc6 1011 struct block_device *fs_dev,
18eba7aa 1012 unsigned long long start, int len, int blocksize)
470decc6
DK
1013{
1014 journal_t *journal = journal_init_common();
1015 struct buffer_head *bh;
05496769 1016 char *p;
470decc6
DK
1017 int n;
1018
1019 if (!journal)
1020 return NULL;
1021
1022 /* journal descriptor can store up to n blocks -bzzz */
1023 journal->j_blocksize = blocksize;
1024 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1025 journal->j_wbufsize = n;
1026 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1027 if (!journal->j_wbuf) {
1028 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
329d291f 1029 __func__);
470decc6
DK
1030 kfree(journal);
1031 journal = NULL;
5eb30790 1032 goto out;
470decc6
DK
1033 }
1034 journal->j_dev = bdev;
1035 journal->j_fs_dev = fs_dev;
1036 journal->j_blk_offset = start;
1037 journal->j_maxlen = len;
05496769
TT
1038 bdevname(journal->j_dev, journal->j_devname);
1039 p = journal->j_devname;
1040 while ((p = strchr(p, '/')))
1041 *p = '!';
8e85fb3f 1042 jbd2_stats_proc_init(journal);
470decc6
DK
1043
1044 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1045 J_ASSERT(bh != NULL);
1046 journal->j_sb_buffer = bh;
1047 journal->j_superblock = (journal_superblock_t *)bh->b_data;
5eb30790 1048out:
470decc6
DK
1049 return journal;
1050}
1051
1052/**
f7f4bccb 1053 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
470decc6
DK
1054 * @inode: An inode to create the journal in
1055 *
f7f4bccb 1056 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
470decc6
DK
1057 * the journal. The inode must exist already, must support bmap() and
1058 * must have all data blocks preallocated.
1059 */
f7f4bccb 1060journal_t * jbd2_journal_init_inode (struct inode *inode)
470decc6
DK
1061{
1062 struct buffer_head *bh;
1063 journal_t *journal = journal_init_common();
05496769 1064 char *p;
470decc6
DK
1065 int err;
1066 int n;
18eba7aa 1067 unsigned long long blocknr;
470decc6
DK
1068
1069 if (!journal)
1070 return NULL;
1071
1072 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1073 journal->j_inode = inode;
05496769
TT
1074 bdevname(journal->j_dev, journal->j_devname);
1075 p = journal->j_devname;
1076 while ((p = strchr(p, '/')))
1077 *p = '!';
1078 p = journal->j_devname + strlen(journal->j_devname);
1079 sprintf(p, ":%lu", journal->j_inode->i_ino);
470decc6
DK
1080 jbd_debug(1,
1081 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1082 journal, inode->i_sb->s_id, inode->i_ino,
1083 (long long) inode->i_size,
1084 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1085
1086 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1087 journal->j_blocksize = inode->i_sb->s_blocksize;
8e85fb3f 1088 jbd2_stats_proc_init(journal);
470decc6
DK
1089
1090 /* journal descriptor can store up to n blocks -bzzz */
1091 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1092 journal->j_wbufsize = n;
1093 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1094 if (!journal->j_wbuf) {
1095 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
329d291f 1096 __func__);
2423840d 1097 jbd2_stats_proc_exit(journal);
470decc6
DK
1098 kfree(journal);
1099 return NULL;
1100 }
1101
f7f4bccb 1102 err = jbd2_journal_bmap(journal, 0, &blocknr);
470decc6
DK
1103 /* If that failed, give up */
1104 if (err) {
1105 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
329d291f 1106 __func__);
2423840d 1107 jbd2_stats_proc_exit(journal);
470decc6
DK
1108 kfree(journal);
1109 return NULL;
1110 }
1111
1112 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1113 J_ASSERT(bh != NULL);
1114 journal->j_sb_buffer = bh;
1115 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1116
1117 return journal;
1118}
1119
1120/*
1121 * If the journal init or create aborts, we need to mark the journal
1122 * superblock as being NULL to prevent the journal destroy from writing
1123 * back a bogus superblock.
1124 */
1125static void journal_fail_superblock (journal_t *journal)
1126{
1127 struct buffer_head *bh = journal->j_sb_buffer;
1128 brelse(bh);
1129 journal->j_sb_buffer = NULL;
1130}
1131
1132/*
1133 * Given a journal_t structure, initialise the various fields for
1134 * startup of a new journaling session. We use this both when creating
1135 * a journal, and after recovering an old journal to reset it for
1136 * subsequent use.
1137 */
1138
1139static int journal_reset(journal_t *journal)
1140{
1141 journal_superblock_t *sb = journal->j_superblock;
18eba7aa 1142 unsigned long long first, last;
470decc6
DK
1143
1144 first = be32_to_cpu(sb->s_first);
1145 last = be32_to_cpu(sb->s_maxlen);
1146
1147 journal->j_first = first;
1148 journal->j_last = last;
1149
1150 journal->j_head = first;
1151 journal->j_tail = first;
1152 journal->j_free = last - first;
1153
1154 journal->j_tail_sequence = journal->j_transaction_sequence;
1155 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1156 journal->j_commit_request = journal->j_commit_sequence;
1157
1158 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1159
1160 /* Add the dynamic fields and write it to disk. */
f7f4bccb 1161 jbd2_journal_update_superblock(journal, 1);
97f06784 1162 return jbd2_journal_start_thread(journal);
470decc6
DK
1163}
1164
1165/**
f7f4bccb 1166 * int jbd2_journal_create() - Initialise the new journal file
470decc6
DK
1167 * @journal: Journal to create. This structure must have been initialised
1168 *
1169 * Given a journal_t structure which tells us which disk blocks we can
1170 * use, create a new journal superblock and initialise all of the
1171 * journal fields from scratch.
1172 **/
f7f4bccb 1173int jbd2_journal_create(journal_t *journal)
470decc6 1174{
18eba7aa 1175 unsigned long long blocknr;
470decc6
DK
1176 struct buffer_head *bh;
1177 journal_superblock_t *sb;
1178 int i, err;
1179
f7f4bccb 1180 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
470decc6
DK
1181 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1182 journal->j_maxlen);
1183 journal_fail_superblock(journal);
1184 return -EINVAL;
1185 }
1186
1187 if (journal->j_inode == NULL) {
1188 /*
1189 * We don't know what block to start at!
1190 */
1191 printk(KERN_EMERG
1192 "%s: creation of journal on external device!\n",
329d291f 1193 __func__);
470decc6
DK
1194 BUG();
1195 }
1196
1197 /* Zero out the entire journal on disk. We cannot afford to
f7f4bccb 1198 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
470decc6
DK
1199 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1200 for (i = 0; i < journal->j_maxlen; i++) {
f7f4bccb 1201 err = jbd2_journal_bmap(journal, i, &blocknr);
470decc6
DK
1202 if (err)
1203 return err;
1204 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1205 lock_buffer(bh);
1206 memset (bh->b_data, 0, journal->j_blocksize);
1207 BUFFER_TRACE(bh, "marking dirty");
1208 mark_buffer_dirty(bh);
1209 BUFFER_TRACE(bh, "marking uptodate");
1210 set_buffer_uptodate(bh);
1211 unlock_buffer(bh);
1212 __brelse(bh);
1213 }
1214
1215 sync_blockdev(journal->j_dev);
1216 jbd_debug(1, "JBD: journal cleared.\n");
1217
1218 /* OK, fill in the initial static fields in the new superblock */
1219 sb = journal->j_superblock;
1220
f7f4bccb
MC
1221 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1222 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
470decc6
DK
1223
1224 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1225 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1226 sb->s_first = cpu_to_be32(1);
1227
1228 journal->j_transaction_sequence = 1;
1229
f7f4bccb 1230 journal->j_flags &= ~JBD2_ABORT;
470decc6
DK
1231 journal->j_format_version = 2;
1232
1233 return journal_reset(journal);
1234}
1235
1236/**
f7f4bccb 1237 * void jbd2_journal_update_superblock() - Update journal sb on disk.
470decc6
DK
1238 * @journal: The journal to update.
1239 * @wait: Set to '0' if you don't want to wait for IO completion.
1240 *
1241 * Update a journal's dynamic superblock fields and write it to disk,
1242 * optionally waiting for the IO to complete.
1243 */
f7f4bccb 1244void jbd2_journal_update_superblock(journal_t *journal, int wait)
470decc6
DK
1245{
1246 journal_superblock_t *sb = journal->j_superblock;
1247 struct buffer_head *bh = journal->j_sb_buffer;
1248
1249 /*
1250 * As a special case, if the on-disk copy is already marked as needing
1251 * no recovery (s_start == 0) and there are no outstanding transactions
1252 * in the filesystem, then we can safely defer the superblock update
f7f4bccb 1253 * until the next commit by setting JBD2_FLUSHED. This avoids
470decc6
DK
1254 * attempting a write to a potential-readonly device.
1255 */
1256 if (sb->s_start == 0 && journal->j_tail_sequence ==
1257 journal->j_transaction_sequence) {
1258 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1259 "(start %ld, seq %d, errno %d)\n",
1260 journal->j_tail, journal->j_tail_sequence,
1261 journal->j_errno);
1262 goto out;
1263 }
1264
914258bf
TT
1265 if (buffer_write_io_error(bh)) {
1266 /*
1267 * Oh, dear. A previous attempt to write the journal
1268 * superblock failed. This could happen because the
1269 * USB device was yanked out. Or it could happen to
1270 * be a transient write error and maybe the block will
1271 * be remapped. Nothing we can do but to retry the
1272 * write and hope for the best.
1273 */
1274 printk(KERN_ERR "JBD2: previous I/O error detected "
1275 "for journal superblock update for %s.\n",
1276 journal->j_devname);
1277 clear_buffer_write_io_error(bh);
1278 set_buffer_uptodate(bh);
1279 }
1280
470decc6
DK
1281 spin_lock(&journal->j_state_lock);
1282 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1283 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1284
1285 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1286 sb->s_start = cpu_to_be32(journal->j_tail);
1287 sb->s_errno = cpu_to_be32(journal->j_errno);
1288 spin_unlock(&journal->j_state_lock);
1289
1290 BUFFER_TRACE(bh, "marking dirty");
1291 mark_buffer_dirty(bh);
914258bf 1292 if (wait) {
470decc6 1293 sync_dirty_buffer(bh);
914258bf
TT
1294 if (buffer_write_io_error(bh)) {
1295 printk(KERN_ERR "JBD2: I/O error detected "
1296 "when updating journal superblock for %s.\n",
1297 journal->j_devname);
1298 clear_buffer_write_io_error(bh);
1299 set_buffer_uptodate(bh);
1300 }
1301 } else
470decc6
DK
1302 ll_rw_block(SWRITE, 1, &bh);
1303
1304out:
1305 /* If we have just flushed the log (by marking s_start==0), then
1306 * any future commit will have to be careful to update the
1307 * superblock again to re-record the true start of the log. */
1308
1309 spin_lock(&journal->j_state_lock);
1310 if (sb->s_start)
f7f4bccb 1311 journal->j_flags &= ~JBD2_FLUSHED;
470decc6 1312 else
f7f4bccb 1313 journal->j_flags |= JBD2_FLUSHED;
470decc6
DK
1314 spin_unlock(&journal->j_state_lock);
1315}
1316
1317/*
1318 * Read the superblock for a given journal, performing initial
1319 * validation of the format.
1320 */
1321
1322static int journal_get_superblock(journal_t *journal)
1323{
1324 struct buffer_head *bh;
1325 journal_superblock_t *sb;
1326 int err = -EIO;
1327
1328 bh = journal->j_sb_buffer;
1329
1330 J_ASSERT(bh != NULL);
1331 if (!buffer_uptodate(bh)) {
1332 ll_rw_block(READ, 1, &bh);
1333 wait_on_buffer(bh);
1334 if (!buffer_uptodate(bh)) {
1335 printk (KERN_ERR
1336 "JBD: IO error reading journal superblock\n");
1337 goto out;
1338 }
1339 }
1340
1341 sb = journal->j_superblock;
1342
1343 err = -EINVAL;
1344
f7f4bccb 1345 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
470decc6
DK
1346 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1347 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1348 goto out;
1349 }
1350
1351 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
f7f4bccb 1352 case JBD2_SUPERBLOCK_V1:
470decc6
DK
1353 journal->j_format_version = 1;
1354 break;
f7f4bccb 1355 case JBD2_SUPERBLOCK_V2:
470decc6
DK
1356 journal->j_format_version = 2;
1357 break;
1358 default:
1359 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1360 goto out;
1361 }
1362
1363 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1364 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1365 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1366 printk (KERN_WARNING "JBD: journal file too short\n");
1367 goto out;
1368 }
1369
1370 return 0;
1371
1372out:
1373 journal_fail_superblock(journal);
1374 return err;
1375}
1376
1377/*
1378 * Load the on-disk journal superblock and read the key fields into the
1379 * journal_t.
1380 */
1381
1382static int load_superblock(journal_t *journal)
1383{
1384 int err;
1385 journal_superblock_t *sb;
1386
1387 err = journal_get_superblock(journal);
1388 if (err)
1389 return err;
1390
1391 sb = journal->j_superblock;
1392
1393 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1394 journal->j_tail = be32_to_cpu(sb->s_start);
1395 journal->j_first = be32_to_cpu(sb->s_first);
1396 journal->j_last = be32_to_cpu(sb->s_maxlen);
1397 journal->j_errno = be32_to_cpu(sb->s_errno);
1398
1399 return 0;
1400}
1401
1402
1403/**
f7f4bccb 1404 * int jbd2_journal_load() - Read journal from disk.
470decc6
DK
1405 * @journal: Journal to act on.
1406 *
1407 * Given a journal_t structure which tells us which disk blocks contain
1408 * a journal, read the journal from disk to initialise the in-memory
1409 * structures.
1410 */
f7f4bccb 1411int jbd2_journal_load(journal_t *journal)
470decc6
DK
1412{
1413 int err;
1414 journal_superblock_t *sb;
1415
1416 err = load_superblock(journal);
1417 if (err)
1418 return err;
1419
1420 sb = journal->j_superblock;
1421 /* If this is a V2 superblock, then we have to check the
1422 * features flags on it. */
1423
1424 if (journal->j_format_version >= 2) {
1425 if ((sb->s_feature_ro_compat &
f7f4bccb 1426 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
470decc6 1427 (sb->s_feature_incompat &
f7f4bccb 1428 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
470decc6
DK
1429 printk (KERN_WARNING
1430 "JBD: Unrecognised features on journal\n");
1431 return -EINVAL;
1432 }
1433 }
1434
470decc6
DK
1435 /* Let the recovery code check whether it needs to recover any
1436 * data from the journal. */
f7f4bccb 1437 if (jbd2_journal_recover(journal))
470decc6
DK
1438 goto recovery_error;
1439
1440 /* OK, we've finished with the dynamic journal bits:
1441 * reinitialise the dynamic contents of the superblock in memory
1442 * and reset them on disk. */
1443 if (journal_reset(journal))
1444 goto recovery_error;
1445
f7f4bccb
MC
1446 journal->j_flags &= ~JBD2_ABORT;
1447 journal->j_flags |= JBD2_LOADED;
470decc6
DK
1448 return 0;
1449
1450recovery_error:
1451 printk (KERN_WARNING "JBD: recovery failed\n");
1452 return -EIO;
1453}
1454
1455/**
f7f4bccb 1456 * void jbd2_journal_destroy() - Release a journal_t structure.
470decc6
DK
1457 * @journal: Journal to act on.
1458 *
1459 * Release a journal_t structure once it is no longer in use by the
1460 * journaled object.
44519faf 1461 * Return <0 if we couldn't clean up the journal.
470decc6 1462 */
44519faf 1463int jbd2_journal_destroy(journal_t *journal)
470decc6 1464{
44519faf
HK
1465 int err = 0;
1466
470decc6
DK
1467 /* Wait for the commit thread to wake up and die. */
1468 journal_kill_thread(journal);
1469
1470 /* Force a final log commit */
1471 if (journal->j_running_transaction)
f7f4bccb 1472 jbd2_journal_commit_transaction(journal);
470decc6
DK
1473
1474 /* Force any old transactions to disk */
1475
1476 /* Totally anal locking here... */
1477 spin_lock(&journal->j_list_lock);
1478 while (journal->j_checkpoint_transactions != NULL) {
1479 spin_unlock(&journal->j_list_lock);
f7f4bccb 1480 jbd2_log_do_checkpoint(journal);
470decc6
DK
1481 spin_lock(&journal->j_list_lock);
1482 }
1483
1484 J_ASSERT(journal->j_running_transaction == NULL);
1485 J_ASSERT(journal->j_committing_transaction == NULL);
1486 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1487 spin_unlock(&journal->j_list_lock);
1488
470decc6 1489 if (journal->j_sb_buffer) {
44519faf
HK
1490 if (!is_journal_aborted(journal)) {
1491 /* We can now mark the journal as empty. */
1492 journal->j_tail = 0;
1493 journal->j_tail_sequence =
1494 ++journal->j_transaction_sequence;
1495 jbd2_journal_update_superblock(journal, 1);
1496 } else {
1497 err = -EIO;
1498 }
470decc6
DK
1499 brelse(journal->j_sb_buffer);
1500 }
1501
8e85fb3f
JL
1502 if (journal->j_proc_entry)
1503 jbd2_stats_proc_exit(journal);
470decc6
DK
1504 if (journal->j_inode)
1505 iput(journal->j_inode);
1506 if (journal->j_revoke)
f7f4bccb 1507 jbd2_journal_destroy_revoke(journal);
470decc6
DK
1508 kfree(journal->j_wbuf);
1509 kfree(journal);
44519faf
HK
1510
1511 return err;
470decc6
DK
1512}
1513
1514
1515/**
f7f4bccb 1516 *int jbd2_journal_check_used_features () - Check if features specified are used.
470decc6
DK
1517 * @journal: Journal to check.
1518 * @compat: bitmask of compatible features
1519 * @ro: bitmask of features that force read-only mount
1520 * @incompat: bitmask of incompatible features
1521 *
1522 * Check whether the journal uses all of a given set of
1523 * features. Return true (non-zero) if it does.
1524 **/
1525
f7f4bccb 1526int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
470decc6
DK
1527 unsigned long ro, unsigned long incompat)
1528{
1529 journal_superblock_t *sb;
1530
1531 if (!compat && !ro && !incompat)
1532 return 1;
1533 if (journal->j_format_version == 1)
1534 return 0;
1535
1536 sb = journal->j_superblock;
1537
1538 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1539 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1540 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1541 return 1;
1542
1543 return 0;
1544}
1545
1546/**
f7f4bccb 1547 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
470decc6
DK
1548 * @journal: Journal to check.
1549 * @compat: bitmask of compatible features
1550 * @ro: bitmask of features that force read-only mount
1551 * @incompat: bitmask of incompatible features
1552 *
1553 * Check whether the journaling code supports the use of
1554 * all of a given set of features on this journal. Return true
1555 * (non-zero) if it can. */
1556
f7f4bccb 1557int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
470decc6
DK
1558 unsigned long ro, unsigned long incompat)
1559{
1560 journal_superblock_t *sb;
1561
1562 if (!compat && !ro && !incompat)
1563 return 1;
1564
1565 sb = journal->j_superblock;
1566
1567 /* We can support any known requested features iff the
1568 * superblock is in version 2. Otherwise we fail to support any
1569 * extended sb features. */
1570
1571 if (journal->j_format_version != 2)
1572 return 0;
1573
f7f4bccb
MC
1574 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1575 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1576 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
470decc6
DK
1577 return 1;
1578
1579 return 0;
1580}
1581
1582/**
f7f4bccb 1583 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
470decc6
DK
1584 * @journal: Journal to act on.
1585 * @compat: bitmask of compatible features
1586 * @ro: bitmask of features that force read-only mount
1587 * @incompat: bitmask of incompatible features
1588 *
1589 * Mark a given journal feature as present on the
1590 * superblock. Returns true if the requested features could be set.
1591 *
1592 */
1593
f7f4bccb 1594int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
470decc6
DK
1595 unsigned long ro, unsigned long incompat)
1596{
1597 journal_superblock_t *sb;
1598
f7f4bccb 1599 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
470decc6
DK
1600 return 1;
1601
f7f4bccb 1602 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
470decc6
DK
1603 return 0;
1604
1605 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1606 compat, ro, incompat);
1607
1608 sb = journal->j_superblock;
1609
1610 sb->s_feature_compat |= cpu_to_be32(compat);
1611 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1612 sb->s_feature_incompat |= cpu_to_be32(incompat);
1613
1614 return 1;
1615}
1616
818d276c
GS
1617/*
1618 * jbd2_journal_clear_features () - Clear a given journal feature in the
1619 * superblock
1620 * @journal: Journal to act on.
1621 * @compat: bitmask of compatible features
1622 * @ro: bitmask of features that force read-only mount
1623 * @incompat: bitmask of incompatible features
1624 *
1625 * Clear a given journal feature as present on the
1626 * superblock.
1627 */
1628void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1629 unsigned long ro, unsigned long incompat)
1630{
1631 journal_superblock_t *sb;
1632
1633 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1634 compat, ro, incompat);
1635
1636 sb = journal->j_superblock;
1637
1638 sb->s_feature_compat &= ~cpu_to_be32(compat);
1639 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1640 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1641}
1642EXPORT_SYMBOL(jbd2_journal_clear_features);
470decc6
DK
1643
1644/**
f7f4bccb 1645 * int jbd2_journal_update_format () - Update on-disk journal structure.
470decc6
DK
1646 * @journal: Journal to act on.
1647 *
1648 * Given an initialised but unloaded journal struct, poke about in the
1649 * on-disk structure to update it to the most recent supported version.
1650 */
f7f4bccb 1651int jbd2_journal_update_format (journal_t *journal)
470decc6
DK
1652{
1653 journal_superblock_t *sb;
1654 int err;
1655
1656 err = journal_get_superblock(journal);
1657 if (err)
1658 return err;
1659
1660 sb = journal->j_superblock;
1661
1662 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
f7f4bccb 1663 case JBD2_SUPERBLOCK_V2:
470decc6 1664 return 0;
f7f4bccb 1665 case JBD2_SUPERBLOCK_V1:
470decc6
DK
1666 return journal_convert_superblock_v1(journal, sb);
1667 default:
1668 break;
1669 }
1670 return -EINVAL;
1671}
1672
1673static int journal_convert_superblock_v1(journal_t *journal,
1674 journal_superblock_t *sb)
1675{
1676 int offset, blocksize;
1677 struct buffer_head *bh;
1678
1679 printk(KERN_WARNING
1680 "JBD: Converting superblock from version 1 to 2.\n");
1681
1682 /* Pre-initialise new fields to zero */
1683 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1684 blocksize = be32_to_cpu(sb->s_blocksize);
1685 memset(&sb->s_feature_compat, 0, blocksize-offset);
1686
1687 sb->s_nr_users = cpu_to_be32(1);
f7f4bccb 1688 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
470decc6
DK
1689 journal->j_format_version = 2;
1690
1691 bh = journal->j_sb_buffer;
1692 BUFFER_TRACE(bh, "marking dirty");
1693 mark_buffer_dirty(bh);
1694 sync_dirty_buffer(bh);
1695 return 0;
1696}
1697
1698
1699/**
f7f4bccb 1700 * int jbd2_journal_flush () - Flush journal
470decc6
DK
1701 * @journal: Journal to act on.
1702 *
1703 * Flush all data for a given journal to disk and empty the journal.
1704 * Filesystems can use this when remounting readonly to ensure that
1705 * recovery does not need to happen on remount.
1706 */
1707
f7f4bccb 1708int jbd2_journal_flush(journal_t *journal)
470decc6
DK
1709{
1710 int err = 0;
1711 transaction_t *transaction = NULL;
1712 unsigned long old_tail;
1713
1714 spin_lock(&journal->j_state_lock);
1715
1716 /* Force everything buffered to the log... */
1717 if (journal->j_running_transaction) {
1718 transaction = journal->j_running_transaction;
f7f4bccb 1719 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
1720 } else if (journal->j_committing_transaction)
1721 transaction = journal->j_committing_transaction;
1722
1723 /* Wait for the log commit to complete... */
1724 if (transaction) {
1725 tid_t tid = transaction->t_tid;
1726
1727 spin_unlock(&journal->j_state_lock);
f7f4bccb 1728 jbd2_log_wait_commit(journal, tid);
470decc6
DK
1729 } else {
1730 spin_unlock(&journal->j_state_lock);
1731 }
1732
1733 /* ...and flush everything in the log out to disk. */
1734 spin_lock(&journal->j_list_lock);
1735 while (!err && journal->j_checkpoint_transactions != NULL) {
1736 spin_unlock(&journal->j_list_lock);
44519faf 1737 mutex_lock(&journal->j_checkpoint_mutex);
f7f4bccb 1738 err = jbd2_log_do_checkpoint(journal);
44519faf 1739 mutex_unlock(&journal->j_checkpoint_mutex);
470decc6
DK
1740 spin_lock(&journal->j_list_lock);
1741 }
1742 spin_unlock(&journal->j_list_lock);
44519faf
HK
1743
1744 if (is_journal_aborted(journal))
1745 return -EIO;
1746
f7f4bccb 1747 jbd2_cleanup_journal_tail(journal);
470decc6
DK
1748
1749 /* Finally, mark the journal as really needing no recovery.
1750 * This sets s_start==0 in the underlying superblock, which is
1751 * the magic code for a fully-recovered superblock. Any future
1752 * commits of data to the journal will restore the current
1753 * s_start value. */
1754 spin_lock(&journal->j_state_lock);
1755 old_tail = journal->j_tail;
1756 journal->j_tail = 0;
1757 spin_unlock(&journal->j_state_lock);
f7f4bccb 1758 jbd2_journal_update_superblock(journal, 1);
470decc6
DK
1759 spin_lock(&journal->j_state_lock);
1760 journal->j_tail = old_tail;
1761
1762 J_ASSERT(!journal->j_running_transaction);
1763 J_ASSERT(!journal->j_committing_transaction);
1764 J_ASSERT(!journal->j_checkpoint_transactions);
1765 J_ASSERT(journal->j_head == journal->j_tail);
1766 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1767 spin_unlock(&journal->j_state_lock);
44519faf 1768 return 0;
470decc6
DK
1769}
1770
1771/**
f7f4bccb 1772 * int jbd2_journal_wipe() - Wipe journal contents
470decc6
DK
1773 * @journal: Journal to act on.
1774 * @write: flag (see below)
1775 *
1776 * Wipe out all of the contents of a journal, safely. This will produce
1777 * a warning if the journal contains any valid recovery information.
f7f4bccb 1778 * Must be called between journal_init_*() and jbd2_journal_load().
470decc6
DK
1779 *
1780 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1781 * we merely suppress recovery.
1782 */
1783
f7f4bccb 1784int jbd2_journal_wipe(journal_t *journal, int write)
470decc6
DK
1785{
1786 journal_superblock_t *sb;
1787 int err = 0;
1788
f7f4bccb 1789 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
470decc6
DK
1790
1791 err = load_superblock(journal);
1792 if (err)
1793 return err;
1794
1795 sb = journal->j_superblock;
1796
1797 if (!journal->j_tail)
1798 goto no_recovery;
1799
1800 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1801 write ? "Clearing" : "Ignoring");
1802
f7f4bccb 1803 err = jbd2_journal_skip_recovery(journal);
470decc6 1804 if (write)
f7f4bccb 1805 jbd2_journal_update_superblock(journal, 1);
470decc6
DK
1806
1807 no_recovery:
1808 return err;
1809}
1810
470decc6
DK
1811/*
1812 * Journal abort has very specific semantics, which we describe
1813 * for journal abort.
1814 *
1815 * Two internal function, which provide abort to te jbd layer
1816 * itself are here.
1817 */
1818
1819/*
1820 * Quick version for internal journal use (doesn't lock the journal).
1821 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1822 * and don't attempt to make any other journal updates.
1823 */
f7f4bccb 1824void __jbd2_journal_abort_hard(journal_t *journal)
470decc6
DK
1825{
1826 transaction_t *transaction;
470decc6 1827
f7f4bccb 1828 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1829 return;
1830
1831 printk(KERN_ERR "Aborting journal on device %s.\n",
05496769 1832 journal->j_devname);
470decc6
DK
1833
1834 spin_lock(&journal->j_state_lock);
f7f4bccb 1835 journal->j_flags |= JBD2_ABORT;
470decc6
DK
1836 transaction = journal->j_running_transaction;
1837 if (transaction)
f7f4bccb 1838 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
1839 spin_unlock(&journal->j_state_lock);
1840}
1841
1842/* Soft abort: record the abort error status in the journal superblock,
1843 * but don't do any other IO. */
1844static void __journal_abort_soft (journal_t *journal, int errno)
1845{
f7f4bccb 1846 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1847 return;
1848
1849 if (!journal->j_errno)
1850 journal->j_errno = errno;
1851
f7f4bccb 1852 __jbd2_journal_abort_hard(journal);
470decc6
DK
1853
1854 if (errno)
f7f4bccb 1855 jbd2_journal_update_superblock(journal, 1);
470decc6
DK
1856}
1857
1858/**
f7f4bccb 1859 * void jbd2_journal_abort () - Shutdown the journal immediately.
470decc6
DK
1860 * @journal: the journal to shutdown.
1861 * @errno: an error number to record in the journal indicating
1862 * the reason for the shutdown.
1863 *
1864 * Perform a complete, immediate shutdown of the ENTIRE
1865 * journal (not of a single transaction). This operation cannot be
1866 * undone without closing and reopening the journal.
1867 *
f7f4bccb 1868 * The jbd2_journal_abort function is intended to support higher level error
470decc6
DK
1869 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1870 * mode.
1871 *
1872 * Journal abort has very specific semantics. Any existing dirty,
1873 * unjournaled buffers in the main filesystem will still be written to
1874 * disk by bdflush, but the journaling mechanism will be suspended
1875 * immediately and no further transaction commits will be honoured.
1876 *
1877 * Any dirty, journaled buffers will be written back to disk without
1878 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1879 * filesystem, but we _do_ attempt to leave as much data as possible
1880 * behind for fsck to use for cleanup.
1881 *
1882 * Any attempt to get a new transaction handle on a journal which is in
1883 * ABORT state will just result in an -EROFS error return. A
f7f4bccb 1884 * jbd2_journal_stop on an existing handle will return -EIO if we have
470decc6
DK
1885 * entered abort state during the update.
1886 *
1887 * Recursive transactions are not disturbed by journal abort until the
f7f4bccb 1888 * final jbd2_journal_stop, which will receive the -EIO error.
470decc6 1889 *
f7f4bccb 1890 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
470decc6
DK
1891 * which will be recorded (if possible) in the journal superblock. This
1892 * allows a client to record failure conditions in the middle of a
1893 * transaction without having to complete the transaction to record the
1894 * failure to disk. ext3_error, for example, now uses this
1895 * functionality.
1896 *
1897 * Errors which originate from within the journaling layer will NOT
1898 * supply an errno; a null errno implies that absolutely no further
1899 * writes are done to the journal (unless there are any already in
1900 * progress).
1901 *
1902 */
1903
f7f4bccb 1904void jbd2_journal_abort(journal_t *journal, int errno)
470decc6
DK
1905{
1906 __journal_abort_soft(journal, errno);
1907}
1908
1909/**
f7f4bccb 1910 * int jbd2_journal_errno () - returns the journal's error state.
470decc6
DK
1911 * @journal: journal to examine.
1912 *
f7f4bccb 1913 * This is the errno numbet set with jbd2_journal_abort(), the last
470decc6
DK
1914 * time the journal was mounted - if the journal was stopped
1915 * without calling abort this will be 0.
1916 *
1917 * If the journal has been aborted on this mount time -EROFS will
1918 * be returned.
1919 */
f7f4bccb 1920int jbd2_journal_errno(journal_t *journal)
470decc6
DK
1921{
1922 int err;
1923
1924 spin_lock(&journal->j_state_lock);
f7f4bccb 1925 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1926 err = -EROFS;
1927 else
1928 err = journal->j_errno;
1929 spin_unlock(&journal->j_state_lock);
1930 return err;
1931}
1932
1933/**
f7f4bccb 1934 * int jbd2_journal_clear_err () - clears the journal's error state
470decc6
DK
1935 * @journal: journal to act on.
1936 *
1937 * An error must be cleared or Acked to take a FS out of readonly
1938 * mode.
1939 */
f7f4bccb 1940int jbd2_journal_clear_err(journal_t *journal)
470decc6
DK
1941{
1942 int err = 0;
1943
1944 spin_lock(&journal->j_state_lock);
f7f4bccb 1945 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1946 err = -EROFS;
1947 else
1948 journal->j_errno = 0;
1949 spin_unlock(&journal->j_state_lock);
1950 return err;
1951}
1952
1953/**
f7f4bccb 1954 * void jbd2_journal_ack_err() - Ack journal err.
470decc6
DK
1955 * @journal: journal to act on.
1956 *
1957 * An error must be cleared or Acked to take a FS out of readonly
1958 * mode.
1959 */
f7f4bccb 1960void jbd2_journal_ack_err(journal_t *journal)
470decc6
DK
1961{
1962 spin_lock(&journal->j_state_lock);
1963 if (journal->j_errno)
f7f4bccb 1964 journal->j_flags |= JBD2_ACK_ERR;
470decc6
DK
1965 spin_unlock(&journal->j_state_lock);
1966}
1967
f7f4bccb 1968int jbd2_journal_blocks_per_page(struct inode *inode)
470decc6
DK
1969{
1970 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1971}
1972
b517bea1
ZB
1973/*
1974 * helper functions to deal with 32 or 64bit block numbers.
1975 */
1976size_t journal_tag_bytes(journal_t *journal)
1977{
1978 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
cd02ff0b 1979 return JBD2_TAG_SIZE64;
b517bea1 1980 else
cd02ff0b 1981 return JBD2_TAG_SIZE32;
b517bea1
ZB
1982}
1983
470decc6
DK
1984/*
1985 * Journal_head storage management
1986 */
e18b890b 1987static struct kmem_cache *jbd2_journal_head_cache;
e23291b9 1988#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
1989static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1990#endif
1991
f7f4bccb 1992static int journal_init_jbd2_journal_head_cache(void)
470decc6
DK
1993{
1994 int retval;
1995
1076d17a 1996 J_ASSERT(jbd2_journal_head_cache == NULL);
a920e941 1997 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
470decc6
DK
1998 sizeof(struct journal_head),
1999 0, /* offset */
77160957 2000 SLAB_TEMPORARY, /* flags */
20c2df83 2001 NULL); /* ctor */
470decc6 2002 retval = 0;
1076d17a 2003 if (!jbd2_journal_head_cache) {
470decc6
DK
2004 retval = -ENOMEM;
2005 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2006 }
2007 return retval;
2008}
2009
f7f4bccb 2010static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
470decc6 2011{
8a9362eb
DG
2012 if (jbd2_journal_head_cache) {
2013 kmem_cache_destroy(jbd2_journal_head_cache);
2014 jbd2_journal_head_cache = NULL;
2015 }
470decc6
DK
2016}
2017
2018/*
2019 * journal_head splicing and dicing
2020 */
2021static struct journal_head *journal_alloc_journal_head(void)
2022{
2023 struct journal_head *ret;
2024 static unsigned long last_warning;
2025
e23291b9 2026#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
2027 atomic_inc(&nr_journal_heads);
2028#endif
f7f4bccb 2029 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1076d17a 2030 if (!ret) {
470decc6
DK
2031 jbd_debug(1, "out of memory for journal_head\n");
2032 if (time_after(jiffies, last_warning + 5*HZ)) {
2033 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
329d291f 2034 __func__);
470decc6
DK
2035 last_warning = jiffies;
2036 }
1076d17a 2037 while (!ret) {
470decc6 2038 yield();
f7f4bccb 2039 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
470decc6
DK
2040 }
2041 }
2042 return ret;
2043}
2044
2045static void journal_free_journal_head(struct journal_head *jh)
2046{
e23291b9 2047#ifdef CONFIG_JBD2_DEBUG
470decc6 2048 atomic_dec(&nr_journal_heads);
cd02ff0b 2049 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
470decc6 2050#endif
f7f4bccb 2051 kmem_cache_free(jbd2_journal_head_cache, jh);
470decc6
DK
2052}
2053
2054/*
2055 * A journal_head is attached to a buffer_head whenever JBD has an
2056 * interest in the buffer.
2057 *
2058 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2059 * is set. This bit is tested in core kernel code where we need to take
2060 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2061 * there.
2062 *
2063 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2064 *
2065 * When a buffer has its BH_JBD bit set it is immune from being released by
2066 * core kernel code, mainly via ->b_count.
2067 *
2068 * A journal_head may be detached from its buffer_head when the journal_head's
2069 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
f7f4bccb 2070 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
470decc6
DK
2071 * journal_head can be dropped if needed.
2072 *
2073 * Various places in the kernel want to attach a journal_head to a buffer_head
2074 * _before_ attaching the journal_head to a transaction. To protect the
f7f4bccb 2075 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
470decc6 2076 * journal_head's b_jcount refcount by one. The caller must call
f7f4bccb 2077 * jbd2_journal_put_journal_head() to undo this.
470decc6
DK
2078 *
2079 * So the typical usage would be:
2080 *
2081 * (Attach a journal_head if needed. Increments b_jcount)
f7f4bccb 2082 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc6
DK
2083 * ...
2084 * jh->b_transaction = xxx;
f7f4bccb 2085 * jbd2_journal_put_journal_head(jh);
470decc6
DK
2086 *
2087 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2088 * because it has a non-zero b_transaction.
2089 */
2090
2091/*
2092 * Give a buffer_head a journal_head.
2093 *
2094 * Doesn't need the journal lock.
2095 * May sleep.
2096 */
f7f4bccb 2097struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
470decc6
DK
2098{
2099 struct journal_head *jh;
2100 struct journal_head *new_jh = NULL;
2101
2102repeat:
2103 if (!buffer_jbd(bh)) {
2104 new_jh = journal_alloc_journal_head();
2105 memset(new_jh, 0, sizeof(*new_jh));
2106 }
2107
2108 jbd_lock_bh_journal_head(bh);
2109 if (buffer_jbd(bh)) {
2110 jh = bh2jh(bh);
2111 } else {
2112 J_ASSERT_BH(bh,
2113 (atomic_read(&bh->b_count) > 0) ||
2114 (bh->b_page && bh->b_page->mapping));
2115
2116 if (!new_jh) {
2117 jbd_unlock_bh_journal_head(bh);
2118 goto repeat;
2119 }
2120
2121 jh = new_jh;
2122 new_jh = NULL; /* We consumed it */
2123 set_buffer_jbd(bh);
2124 bh->b_private = jh;
2125 jh->b_bh = bh;
2126 get_bh(bh);
2127 BUFFER_TRACE(bh, "added journal_head");
2128 }
2129 jh->b_jcount++;
2130 jbd_unlock_bh_journal_head(bh);
2131 if (new_jh)
2132 journal_free_journal_head(new_jh);
2133 return bh->b_private;
2134}
2135
2136/*
2137 * Grab a ref against this buffer_head's journal_head. If it ended up not
2138 * having a journal_head, return NULL
2139 */
f7f4bccb 2140struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
470decc6
DK
2141{
2142 struct journal_head *jh = NULL;
2143
2144 jbd_lock_bh_journal_head(bh);
2145 if (buffer_jbd(bh)) {
2146 jh = bh2jh(bh);
2147 jh->b_jcount++;
2148 }
2149 jbd_unlock_bh_journal_head(bh);
2150 return jh;
2151}
2152
2153static void __journal_remove_journal_head(struct buffer_head *bh)
2154{
2155 struct journal_head *jh = bh2jh(bh);
2156
2157 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2158
2159 get_bh(bh);
2160 if (jh->b_jcount == 0) {
2161 if (jh->b_transaction == NULL &&
2162 jh->b_next_transaction == NULL &&
2163 jh->b_cp_transaction == NULL) {
2164 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2165 J_ASSERT_BH(bh, buffer_jbd(bh));
2166 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2167 BUFFER_TRACE(bh, "remove journal_head");
2168 if (jh->b_frozen_data) {
2169 printk(KERN_WARNING "%s: freeing "
2170 "b_frozen_data\n",
329d291f 2171 __func__);
af1e76d6 2172 jbd2_free(jh->b_frozen_data, bh->b_size);
470decc6
DK
2173 }
2174 if (jh->b_committed_data) {
2175 printk(KERN_WARNING "%s: freeing "
2176 "b_committed_data\n",
329d291f 2177 __func__);
af1e76d6 2178 jbd2_free(jh->b_committed_data, bh->b_size);
470decc6
DK
2179 }
2180 bh->b_private = NULL;
2181 jh->b_bh = NULL; /* debug, really */
2182 clear_buffer_jbd(bh);
2183 __brelse(bh);
2184 journal_free_journal_head(jh);
2185 } else {
2186 BUFFER_TRACE(bh, "journal_head was locked");
2187 }
2188 }
2189}
2190
2191/*
f7f4bccb 2192 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
470decc6
DK
2193 * and has a zero b_jcount then remove and release its journal_head. If we did
2194 * see that the buffer is not used by any transaction we also "logically"
2195 * decrement ->b_count.
2196 *
2197 * We in fact take an additional increment on ->b_count as a convenience,
2198 * because the caller usually wants to do additional things with the bh
2199 * after calling here.
f7f4bccb 2200 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
470decc6
DK
2201 * time. Once the caller has run __brelse(), the buffer is eligible for
2202 * reaping by try_to_free_buffers().
2203 */
f7f4bccb 2204void jbd2_journal_remove_journal_head(struct buffer_head *bh)
470decc6
DK
2205{
2206 jbd_lock_bh_journal_head(bh);
2207 __journal_remove_journal_head(bh);
2208 jbd_unlock_bh_journal_head(bh);
2209}
2210
2211/*
2212 * Drop a reference on the passed journal_head. If it fell to zero then try to
2213 * release the journal_head from the buffer_head.
2214 */
f7f4bccb 2215void jbd2_journal_put_journal_head(struct journal_head *jh)
470decc6
DK
2216{
2217 struct buffer_head *bh = jh2bh(jh);
2218
2219 jbd_lock_bh_journal_head(bh);
2220 J_ASSERT_JH(jh, jh->b_jcount > 0);
2221 --jh->b_jcount;
2222 if (!jh->b_jcount && !jh->b_transaction) {
2223 __journal_remove_journal_head(bh);
2224 __brelse(bh);
2225 }
2226 jbd_unlock_bh_journal_head(bh);
2227}
2228
c851ed54
JK
2229/*
2230 * Initialize jbd inode head
2231 */
2232void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2233{
2234 jinode->i_transaction = NULL;
2235 jinode->i_next_transaction = NULL;
2236 jinode->i_vfs_inode = inode;
2237 jinode->i_flags = 0;
2238 INIT_LIST_HEAD(&jinode->i_list);
2239}
2240
2241/*
2242 * Function to be called before we start removing inode from memory (i.e.,
2243 * clear_inode() is a fine place to be called from). It removes inode from
2244 * transaction's lists.
2245 */
2246void jbd2_journal_release_jbd_inode(journal_t *journal,
2247 struct jbd2_inode *jinode)
2248{
2249 int writeout = 0;
2250
2251 if (!journal)
2252 return;
2253restart:
2254 spin_lock(&journal->j_list_lock);
2255 /* Is commit writing out inode - we have to wait */
2256 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2257 wait_queue_head_t *wq;
2258 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2259 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2260 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2261 spin_unlock(&journal->j_list_lock);
2262 schedule();
2263 finish_wait(wq, &wait.wait);
2264 goto restart;
2265 }
2266
2267 /* Do we need to wait for data writeback? */
2268 if (journal->j_committing_transaction == jinode->i_transaction)
2269 writeout = 1;
2270 if (jinode->i_transaction) {
2271 list_del(&jinode->i_list);
2272 jinode->i_transaction = NULL;
2273 }
2274 spin_unlock(&journal->j_list_lock);
2275}
2276
470decc6 2277/*
0f49d5d0 2278 * debugfs tunables
470decc6 2279 */
6f38c74f
JS
2280#ifdef CONFIG_JBD2_DEBUG
2281u8 jbd2_journal_enable_debug __read_mostly;
f7f4bccb 2282EXPORT_SYMBOL(jbd2_journal_enable_debug);
470decc6 2283
0f49d5d0 2284#define JBD2_DEBUG_NAME "jbd2-debug"
470decc6 2285
6f38c74f
JS
2286static struct dentry *jbd2_debugfs_dir;
2287static struct dentry *jbd2_debug;
470decc6 2288
0f49d5d0
JS
2289static void __init jbd2_create_debugfs_entry(void)
2290{
2291 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2292 if (jbd2_debugfs_dir)
2293 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2294 jbd2_debugfs_dir,
2295 &jbd2_journal_enable_debug);
470decc6
DK
2296}
2297
0f49d5d0 2298static void __exit jbd2_remove_debugfs_entry(void)
470decc6 2299{
6f38c74f
JS
2300 debugfs_remove(jbd2_debug);
2301 debugfs_remove(jbd2_debugfs_dir);
470decc6
DK
2302}
2303
0f49d5d0 2304#else
470decc6 2305
0f49d5d0 2306static void __init jbd2_create_debugfs_entry(void)
470decc6 2307{
470decc6
DK
2308}
2309
0f49d5d0 2310static void __exit jbd2_remove_debugfs_entry(void)
470decc6 2311{
470decc6
DK
2312}
2313
470decc6
DK
2314#endif
2315
8e85fb3f
JL
2316#ifdef CONFIG_PROC_FS
2317
2318#define JBD2_STATS_PROC_NAME "fs/jbd2"
2319
2320static void __init jbd2_create_jbd_stats_proc_entry(void)
2321{
2322 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2323}
2324
2325static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2326{
2327 if (proc_jbd2_stats)
2328 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2329}
2330
2331#else
2332
2333#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2334#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2335
2336#endif
2337
e18b890b 2338struct kmem_cache *jbd2_handle_cache;
470decc6
DK
2339
2340static int __init journal_init_handle_cache(void)
2341{
a920e941 2342 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
470decc6
DK
2343 sizeof(handle_t),
2344 0, /* offset */
77160957 2345 SLAB_TEMPORARY, /* flags */
20c2df83 2346 NULL); /* ctor */
f7f4bccb 2347 if (jbd2_handle_cache == NULL) {
470decc6
DK
2348 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2349 return -ENOMEM;
2350 }
2351 return 0;
2352}
2353
f7f4bccb 2354static void jbd2_journal_destroy_handle_cache(void)
470decc6 2355{
f7f4bccb
MC
2356 if (jbd2_handle_cache)
2357 kmem_cache_destroy(jbd2_handle_cache);
470decc6
DK
2358}
2359
2360/*
2361 * Module startup and shutdown
2362 */
2363
2364static int __init journal_init_caches(void)
2365{
2366 int ret;
2367
f7f4bccb 2368 ret = jbd2_journal_init_revoke_caches();
470decc6 2369 if (ret == 0)
f7f4bccb 2370 ret = journal_init_jbd2_journal_head_cache();
470decc6
DK
2371 if (ret == 0)
2372 ret = journal_init_handle_cache();
2373 return ret;
2374}
2375
f7f4bccb 2376static void jbd2_journal_destroy_caches(void)
470decc6 2377{
f7f4bccb
MC
2378 jbd2_journal_destroy_revoke_caches();
2379 jbd2_journal_destroy_jbd2_journal_head_cache();
2380 jbd2_journal_destroy_handle_cache();
470decc6
DK
2381}
2382
2383static int __init journal_init(void)
2384{
2385 int ret;
2386
2387 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2388
2389 ret = journal_init_caches();
620de4e1
DG
2390 if (ret == 0) {
2391 jbd2_create_debugfs_entry();
2392 jbd2_create_jbd_stats_proc_entry();
2393 } else {
f7f4bccb 2394 jbd2_journal_destroy_caches();
620de4e1 2395 }
470decc6
DK
2396 return ret;
2397}
2398
2399static void __exit journal_exit(void)
2400{
e23291b9 2401#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
2402 int n = atomic_read(&nr_journal_heads);
2403 if (n)
2404 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2405#endif
0f49d5d0 2406 jbd2_remove_debugfs_entry();
8e85fb3f 2407 jbd2_remove_jbd_stats_proc_entry();
f7f4bccb 2408 jbd2_journal_destroy_caches();
470decc6
DK
2409}
2410
2411MODULE_LICENSE("GPL");
2412module_init(journal_init);
2413module_exit(journal_exit);
2414