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