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