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