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