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