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