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