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