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