blob: 49d5cd6053c868f7011d7659dccedefbda90a3b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/fs/jbd/journal.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
28#include <linux/jbd.h>
29#include <linux/errno.h>
30#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/mm.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/pagemap.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080035#include <linux/kthread.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070036#include <linux/poison.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080037#include <linux/proc_fs.h>
Jose R. Santosc2a91592007-10-18 23:39:22 -070038#include <linux/debugfs.h>
Andrew Morton8d8c8512006-03-25 03:06:53 -080039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43EXPORT_SYMBOL(journal_start);
44EXPORT_SYMBOL(journal_restart);
45EXPORT_SYMBOL(journal_extend);
46EXPORT_SYMBOL(journal_stop);
47EXPORT_SYMBOL(journal_lock_updates);
48EXPORT_SYMBOL(journal_unlock_updates);
49EXPORT_SYMBOL(journal_get_write_access);
50EXPORT_SYMBOL(journal_get_create_access);
51EXPORT_SYMBOL(journal_get_undo_access);
52EXPORT_SYMBOL(journal_dirty_data);
53EXPORT_SYMBOL(journal_dirty_metadata);
54EXPORT_SYMBOL(journal_release_buffer);
55EXPORT_SYMBOL(journal_forget);
56#if 0
57EXPORT_SYMBOL(journal_sync_buffer);
58#endif
59EXPORT_SYMBOL(journal_flush);
60EXPORT_SYMBOL(journal_revoke);
61
62EXPORT_SYMBOL(journal_init_dev);
63EXPORT_SYMBOL(journal_init_inode);
64EXPORT_SYMBOL(journal_update_format);
65EXPORT_SYMBOL(journal_check_used_features);
66EXPORT_SYMBOL(journal_check_available_features);
67EXPORT_SYMBOL(journal_set_features);
68EXPORT_SYMBOL(journal_create);
69EXPORT_SYMBOL(journal_load);
70EXPORT_SYMBOL(journal_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071EXPORT_SYMBOL(journal_abort);
72EXPORT_SYMBOL(journal_errno);
73EXPORT_SYMBOL(journal_ack_err);
74EXPORT_SYMBOL(journal_clear_err);
75EXPORT_SYMBOL(log_wait_commit);
76EXPORT_SYMBOL(journal_start_commit);
77EXPORT_SYMBOL(journal_force_commit_nested);
78EXPORT_SYMBOL(journal_wipe);
79EXPORT_SYMBOL(journal_blocks_per_page);
80EXPORT_SYMBOL(journal_invalidatepage);
81EXPORT_SYMBOL(journal_try_to_free_buffers);
82EXPORT_SYMBOL(journal_force_commit);
83
84static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
Adrian Bunk022a4a72005-09-06 15:16:41 -070085static void __journal_abort_soft (journal_t *journal, int errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Helper function used to manage commit timeouts
89 */
90
91static void commit_timeout(unsigned long __data)
92{
93 struct task_struct * p = (struct task_struct *) __data;
94
95 wake_up_process(p);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * kjournald: The main thread function used to manage a logging device
100 * journal.
101 *
102 * This kernel thread is responsible for two things:
103 *
104 * 1) COMMIT: Every so often we need to commit the current state of the
105 * filesystem to disk. The journal thread is responsible for writing
106 * all of the metadata buffers to disk.
107 *
108 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
109 * of the data in that part of the log has been rewritten elsewhere on
110 * the disk. Flushing these old buffers to reclaim space in the log is
111 * known as checkpointing, and this thread is responsible for that job.
112 */
113
Adrian Bunk022a4a72005-09-06 15:16:41 -0700114static int kjournald(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Andrew Mortone3df1892006-03-25 03:06:53 -0800116 journal_t *journal = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 transaction_t *transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Andrew Mortone3df1892006-03-25 03:06:53 -0800119 /*
120 * Set up an interval timer which can be used to trigger a commit wakeup
121 * after the commit interval expires
122 */
123 setup_timer(&journal->j_commit_timer, commit_timeout,
124 (unsigned long)current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* Record that the journal thread is running */
127 journal->j_task = current;
128 wake_up(&journal->j_wait_done_commit);
129
130 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
131 journal->j_commit_interval / HZ);
132
133 /*
134 * And now, wait forever for commit wakeup events.
135 */
136 spin_lock(&journal->j_state_lock);
137
138loop:
139 if (journal->j_flags & JFS_UNMOUNT)
140 goto end_loop;
141
142 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
143 journal->j_commit_sequence, journal->j_commit_request);
144
145 if (journal->j_commit_sequence != journal->j_commit_request) {
146 jbd_debug(1, "OK, requests differ\n");
147 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800148 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 journal_commit_transaction(journal);
150 spin_lock(&journal->j_state_lock);
151 goto loop;
152 }
153
154 wake_up(&journal->j_wait_done_commit);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700155 if (freezing(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * The simpler the better. Flushing journal isn't a
158 * good idea, because that depends on threads that may
159 * be already stopped.
160 */
161 jbd_debug(1, "Now suspending kjournald\n");
162 spin_unlock(&journal->j_state_lock);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700163 refrigerator();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 spin_lock(&journal->j_state_lock);
165 } else {
166 /*
167 * We assume on resume that commits are already there,
168 * so we don't sleep
169 */
170 DEFINE_WAIT(wait);
171 int should_sleep = 1;
172
173 prepare_to_wait(&journal->j_wait_commit, &wait,
174 TASK_INTERRUPTIBLE);
175 if (journal->j_commit_sequence != journal->j_commit_request)
176 should_sleep = 0;
177 transaction = journal->j_running_transaction;
178 if (transaction && time_after_eq(jiffies,
179 transaction->t_expires))
180 should_sleep = 0;
Mark Fashehcbf0d272005-09-06 15:19:08 -0700181 if (journal->j_flags & JFS_UNMOUNT)
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700182 should_sleep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (should_sleep) {
184 spin_unlock(&journal->j_state_lock);
185 schedule();
186 spin_lock(&journal->j_state_lock);
187 }
188 finish_wait(&journal->j_wait_commit, &wait);
189 }
190
191 jbd_debug(1, "kjournald wakes\n");
192
193 /*
194 * Were we woken up by a commit wakeup event?
195 */
196 transaction = journal->j_running_transaction;
197 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
198 journal->j_commit_request = transaction->t_tid;
199 jbd_debug(1, "woke because of timeout\n");
200 }
201 goto loop;
202
203end_loop:
204 spin_unlock(&journal->j_state_lock);
Andrew Mortone3df1892006-03-25 03:06:53 -0800205 del_timer_sync(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 journal->j_task = NULL;
207 wake_up(&journal->j_wait_done_commit);
208 jbd_debug(1, "Journal thread exiting.\n");
209 return 0;
210}
211
Pavel Emelianov97f06782007-05-08 00:30:42 -0700212static int journal_start_thread(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Pavel Emelianov97f06782007-05-08 00:30:42 -0700214 struct task_struct *t;
215
216 t = kthread_run(kjournald, journal, "kjournald");
217 if (IS_ERR(t))
218 return PTR_ERR(t);
219
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700220 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static void journal_kill_thread(journal_t *journal)
225{
226 spin_lock(&journal->j_state_lock);
227 journal->j_flags |= JFS_UNMOUNT;
228
229 while (journal->j_task) {
230 wake_up(&journal->j_wait_commit);
231 spin_unlock(&journal->j_state_lock);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700232 wait_event(journal->j_wait_done_commit,
233 journal->j_task == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock(&journal->j_state_lock);
235 }
236 spin_unlock(&journal->j_state_lock);
237}
238
239/*
240 * journal_write_metadata_buffer: write a metadata buffer to the journal.
241 *
242 * Writes a metadata buffer to a given disk block. The actual IO is not
243 * performed but a new buffer_head is constructed which labels the data
244 * to be written with the correct destination disk block.
245 *
246 * Any magic-number escaping which needs to be done will cause a
247 * copy-out here. If the buffer happens to start with the
248 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
249 * magic number is only written to the log for descripter blocks. In
250 * this case, we copy the data and replace the first word with 0, and we
251 * return a result code which indicates that this buffer needs to be
252 * marked as an escaped buffer in the corresponding log descriptor
253 * block. The missing word can then be restored when the block is read
254 * during recovery.
255 *
256 * If the source buffer has already been modified by a new transaction
257 * since we took the last commit snapshot, we use the frozen copy of
258 * that data for IO. If we end up using the existing buffer_head's data
259 * for the write, then we *have* to lock the buffer to prevent anyone
260 * else from using and possibly modifying it while the IO is in
261 * progress.
262 *
263 * The function returns a pointer to the buffer_heads to be used for IO.
264 *
265 * We assume that the journal has already been locked in this function.
266 *
267 * Return value:
268 * <0: Error
269 * >=0: Finished OK
270 *
271 * On success:
272 * Bit 0 set == escape performed on the data
273 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
274 */
275
276int journal_write_metadata_buffer(transaction_t *transaction,
277 struct journal_head *jh_in,
278 struct journal_head **jh_out,
Jan Kara9c28cbc2009-08-03 19:21:00 +0200279 unsigned int blocknr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int need_copy_out = 0;
282 int done_copy_out = 0;
283 int do_escape = 0;
284 char *mapped_data;
285 struct buffer_head *new_bh;
286 struct journal_head *new_jh;
287 struct page *new_page;
288 unsigned int new_offset;
289 struct buffer_head *bh_in = jh2bh(jh_in);
dingdinghuaf1015c42009-07-15 21:42:05 +0200290 journal_t *journal = transaction->t_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * The buffer really shouldn't be locked: only the current committing
294 * transaction is allowed to write it, so nobody else is allowed
295 * to do any IO.
296 *
297 * akpm: except if we're journalling data, and write() output is
298 * also part of a shared mapping, and another thread has
299 * decided to launch a writepage() against this buffer.
300 */
301 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
302
303 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
dingdinghuaf1015c42009-07-15 21:42:05 +0200304 /* keep subsequent assertions sane */
305 new_bh->b_state = 0;
306 init_buffer(new_bh, NULL, NULL);
307 atomic_set(&new_bh->b_count, 1);
308 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /*
311 * If a new transaction has already done a buffer copy-out, then
312 * we use that version of the data for the commit.
313 */
314 jbd_lock_bh_state(bh_in);
315repeat:
316 if (jh_in->b_frozen_data) {
317 done_copy_out = 1;
318 new_page = virt_to_page(jh_in->b_frozen_data);
319 new_offset = offset_in_page(jh_in->b_frozen_data);
320 } else {
321 new_page = jh2bh(jh_in)->b_page;
322 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
323 }
324
325 mapped_data = kmap_atomic(new_page, KM_USER0);
326 /*
327 * Check for escaping
328 */
329 if (*((__be32 *)(mapped_data + new_offset)) ==
330 cpu_to_be32(JFS_MAGIC_NUMBER)) {
331 need_copy_out = 1;
332 do_escape = 1;
333 }
334 kunmap_atomic(mapped_data, KM_USER0);
335
336 /*
337 * Do we need to do a data copy?
338 */
339 if (need_copy_out && !done_copy_out) {
340 char *tmp;
341
342 jbd_unlock_bh_state(bh_in);
Mingming Caoc089d492007-10-16 18:38:25 -0400343 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 jbd_lock_bh_state(bh_in);
345 if (jh_in->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400346 jbd_free(tmp, bh_in->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto repeat;
348 }
349
350 jh_in->b_frozen_data = tmp;
351 mapped_data = kmap_atomic(new_page, KM_USER0);
352 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
353 kunmap_atomic(mapped_data, KM_USER0);
354
355 new_page = virt_to_page(tmp);
356 new_offset = offset_in_page(tmp);
357 done_copy_out = 1;
358 }
359
360 /*
361 * Did we need to do an escaping? Now we've done all the
362 * copying, we can finally do so.
363 */
364 if (do_escape) {
365 mapped_data = kmap_atomic(new_page, KM_USER0);
366 *((unsigned int *)(mapped_data + new_offset)) = 0;
367 kunmap_atomic(mapped_data, KM_USER0);
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 set_bh_page(new_bh, new_page, new_offset);
371 new_jh->b_transaction = NULL;
372 new_bh->b_size = jh2bh(jh_in)->b_size;
373 new_bh->b_bdev = transaction->t_journal->j_dev;
374 new_bh->b_blocknr = blocknr;
375 set_buffer_mapped(new_bh);
376 set_buffer_dirty(new_bh);
377
378 *jh_out = new_jh;
379
380 /*
381 * The to-be-written buffer needs to get moved to the io queue,
382 * and the original buffer whose contents we are shadowing or
383 * copying is moved to the transaction's shadow queue.
384 */
385 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
dingdinghuaf1015c42009-07-15 21:42:05 +0200386 spin_lock(&journal->j_list_lock);
387 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
388 spin_unlock(&journal->j_list_lock);
389 jbd_unlock_bh_state(bh_in);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 JBUFFER_TRACE(new_jh, "file as BJ_IO");
392 journal_file_buffer(new_jh, transaction, BJ_IO);
393
394 return do_escape | (done_copy_out << 1);
395}
396
397/*
398 * Allocation code for the journal file. Manage the space left in the
399 * journal, so that we can begin checkpointing when appropriate.
400 */
401
402/*
403 * __log_space_left: Return the number of free blocks left in the journal.
404 *
405 * Called with the journal already locked.
406 *
407 * Called under j_state_lock
408 */
409
410int __log_space_left(journal_t *journal)
411{
412 int left = journal->j_free;
413
414 assert_spin_locked(&journal->j_state_lock);
415
416 /*
417 * Be pessimistic here about the number of those free blocks which
418 * might be required for log descriptor control blocks.
419 */
420
421#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
422
423 left -= MIN_LOG_RESERVED_BLOCKS;
424
425 if (left <= 0)
426 return 0;
427 left -= (left >> 3);
428 return left;
429}
430
431/*
Jan Kara8fe4cd02009-02-11 13:04:25 -0800432 * Called under j_state_lock. Returns true if a transaction commit was started.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434int __log_start_commit(journal_t *journal, tid_t target)
435{
436 /*
437 * Are we already doing a recent enough commit?
438 */
439 if (!tid_geq(journal->j_commit_request, target)) {
440 /*
441 * We want a new commit: OK, mark the request and wakup the
442 * commit thread. We do _not_ do the commit ourselves.
443 */
444
445 journal->j_commit_request = target;
446 jbd_debug(1, "JBD: requesting commit %d/%d\n",
447 journal->j_commit_request,
448 journal->j_commit_sequence);
449 wake_up(&journal->j_wait_commit);
450 return 1;
451 }
452 return 0;
453}
454
455int log_start_commit(journal_t *journal, tid_t tid)
456{
457 int ret;
458
459 spin_lock(&journal->j_state_lock);
460 ret = __log_start_commit(journal, tid);
461 spin_unlock(&journal->j_state_lock);
462 return ret;
463}
464
465/*
466 * Force and wait upon a commit if the calling process is not within
467 * transaction. This is used for forcing out undo-protected data which contains
468 * bitmaps, when the fs is running out of space.
469 *
470 * We can only force the running transaction if we don't have an active handle;
471 * otherwise, we will deadlock.
472 *
473 * Returns true if a transaction was started.
474 */
475int journal_force_commit_nested(journal_t *journal)
476{
477 transaction_t *transaction = NULL;
478 tid_t tid;
479
480 spin_lock(&journal->j_state_lock);
481 if (journal->j_running_transaction && !current->journal_info) {
482 transaction = journal->j_running_transaction;
483 __log_start_commit(journal, transaction->t_tid);
484 } else if (journal->j_committing_transaction)
485 transaction = journal->j_committing_transaction;
486
487 if (!transaction) {
488 spin_unlock(&journal->j_state_lock);
489 return 0; /* Nothing to retry */
490 }
491
492 tid = transaction->t_tid;
493 spin_unlock(&journal->j_state_lock);
494 log_wait_commit(journal, tid);
495 return 1;
496}
497
498/*
499 * Start a commit of the current running transaction (if any). Returns true
Jan Kara8fe4cd02009-02-11 13:04:25 -0800500 * if a transaction is going to be committed (or is currently already
501 * committing), and fills its tid in at *ptid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503int journal_start_commit(journal_t *journal, tid_t *ptid)
504{
505 int ret = 0;
506
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction) {
509 tid_t tid = journal->j_running_transaction->t_tid;
510
Jan Kara8fe4cd02009-02-11 13:04:25 -0800511 __log_start_commit(journal, tid);
512 /* There's a running transaction and we've just made sure
513 * it's commit has been scheduled. */
514 if (ptid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *ptid = tid;
Jan Kara8fe4cd02009-02-11 13:04:25 -0800516 ret = 1;
517 } else if (journal->j_committing_transaction) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * If ext3_write_super() recently started a commit, then we
520 * have to wait for completion of that transaction
521 */
Jan Kara8fe4cd02009-02-11 13:04:25 -0800522 if (ptid)
523 *ptid = journal->j_committing_transaction->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 ret = 1;
525 }
526 spin_unlock(&journal->j_state_lock);
527 return ret;
528}
529
530/*
531 * Wait for a specified commit to complete.
532 * The caller may not hold the journal lock.
533 */
534int log_wait_commit(journal_t *journal, tid_t tid)
535{
536 int err = 0;
537
538#ifdef CONFIG_JBD_DEBUG
539 spin_lock(&journal->j_state_lock);
540 if (!tid_geq(journal->j_commit_request, tid)) {
541 printk(KERN_EMERG
542 "%s: error: j_commit_request=%d, tid=%d\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700543 __func__, journal->j_commit_request, tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 spin_unlock(&journal->j_state_lock);
546#endif
547 spin_lock(&journal->j_state_lock);
548 while (tid_gt(tid, journal->j_commit_sequence)) {
549 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
550 tid, journal->j_commit_sequence);
551 wake_up(&journal->j_wait_commit);
552 spin_unlock(&journal->j_state_lock);
553 wait_event(journal->j_wait_done_commit,
554 !tid_gt(tid, journal->j_commit_sequence));
555 spin_lock(&journal->j_state_lock);
556 }
557 spin_unlock(&journal->j_state_lock);
558
559 if (unlikely(is_journal_aborted(journal))) {
560 printk(KERN_EMERG "journal commit I/O error\n");
561 err = -EIO;
562 }
563 return err;
564}
565
566/*
567 * Log buffer allocation routines:
568 */
569
Jan Kara9c28cbc2009-08-03 19:21:00 +0200570int journal_next_log_block(journal_t *journal, unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200572 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 spin_lock(&journal->j_state_lock);
575 J_ASSERT(journal->j_free > 1);
576
577 blocknr = journal->j_head;
578 journal->j_head++;
579 journal->j_free--;
580 if (journal->j_head == journal->j_last)
581 journal->j_head = journal->j_first;
582 spin_unlock(&journal->j_state_lock);
583 return journal_bmap(journal, blocknr, retp);
584}
585
586/*
587 * Conversion of logical to physical block numbers for the journal
588 *
589 * On external journals the journal blocks are identity-mapped, so
590 * this is a no-op. If needed, we can use j_blk_offset - everything is
591 * ready.
592 */
Jan Kara9c28cbc2009-08-03 19:21:00 +0200593int journal_bmap(journal_t *journal, unsigned int blocknr,
594 unsigned int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int err = 0;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200597 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (journal->j_inode) {
600 ret = bmap(journal->j_inode, blocknr);
601 if (ret)
602 *retp = ret;
603 else {
604 char b[BDEVNAME_SIZE];
605
606 printk(KERN_ALERT "%s: journal block not found "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200607 "at offset %u on %s\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700608 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 blocknr,
610 bdevname(journal->j_dev, b));
611 err = -EIO;
612 __journal_abort_soft(journal, err);
613 }
614 } else {
615 *retp = blocknr; /* +journal->j_blk_offset */
616 }
617 return err;
618}
619
620/*
621 * We play buffer_head aliasing tricks to write data/metadata blocks to
622 * the journal without copying their contents, but for journal
623 * descriptor blocks we do need to generate bona fide buffers.
624 *
625 * After the caller of journal_get_descriptor_buffer() has finished modifying
626 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
627 * But we don't bother doing that, so there will be coherency problems with
628 * mmaps of blockdevs which hold live JBD-controlled filesystems.
629 */
630struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
631{
632 struct buffer_head *bh;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200633 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int err;
635
636 err = journal_next_log_block(journal, &blocknr);
637
638 if (err)
639 return NULL;
640
641 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700642 if (!bh)
643 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 lock_buffer(bh);
645 memset(bh->b_data, 0, journal->j_blocksize);
646 set_buffer_uptodate(bh);
647 unlock_buffer(bh);
648 BUFFER_TRACE(bh, "return this buffer");
649 return journal_add_journal_head(bh);
650}
651
652/*
653 * Management for journal control blocks: functions to create and
654 * destroy journal_t structures, and to initialise and read existing
655 * journal blocks from disk. */
656
657/* First: create and setup a journal_t object in memory. We initialise
658 * very few fields yet: that has to wait until we have created the
659 * journal structures from from scratch, or loaded them from disk. */
660
661static journal_t * journal_init_common (void)
662{
663 journal_t *journal;
664 int err;
665
Mingming Cao8c3478a2007-10-18 23:39:20 -0700666 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!journal)
668 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 init_waitqueue_head(&journal->j_wait_transaction_locked);
671 init_waitqueue_head(&journal->j_wait_logspace);
672 init_waitqueue_head(&journal->j_wait_done_commit);
673 init_waitqueue_head(&journal->j_wait_checkpoint);
674 init_waitqueue_head(&journal->j_wait_commit);
675 init_waitqueue_head(&journal->j_wait_updates);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800676 mutex_init(&journal->j_barrier);
677 mutex_init(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 spin_lock_init(&journal->j_revoke_lock);
679 spin_lock_init(&journal->j_list_lock);
680 spin_lock_init(&journal->j_state_lock);
681
682 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
683
684 /* The journal is marked for error until we succeed with recovery! */
685 journal->j_flags = JFS_ABORT;
686
687 /* Set up a default-sized revoke table for the new mount. */
688 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
689 if (err) {
690 kfree(journal);
691 goto fail;
692 }
693 return journal;
694fail:
695 return NULL;
696}
697
698/* journal_init_dev and journal_init_inode:
699 *
700 * Create a journal structure assigned some fixed set of disk blocks to
701 * the journal. We don't actually touch those disk blocks yet, but we
702 * need to set up all of the mapping information to tell the journaling
703 * system where the journal blocks are.
704 *
705 */
706
707/**
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700708 * journal_t * journal_init_dev() - creates and initialises a journal structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * @bdev: Block device on which to create the journal
710 * @fs_dev: Device which hold journalled filesystem for this journal.
711 * @start: Block nr Start of journal.
Eric Sandeen37ed3222006-09-27 01:49:31 -0700712 * @len: Length of the journal in blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * @blocksize: blocksize of journalling device
Randy Dunlap0cf01f62008-03-19 17:00:44 -0700714 *
715 * Returns: a newly created journal_t *
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * journal_init_dev creates a journal which maps a fixed contiguous
718 * range of blocks on an arbitrary block device.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721journal_t * journal_init_dev(struct block_device *bdev,
722 struct block_device *fs_dev,
723 int start, int len, int blocksize)
724{
725 journal_t *journal = journal_init_common();
726 struct buffer_head *bh;
727 int n;
728
729 if (!journal)
730 return NULL;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* journal descriptor can store up to n blocks -bzzz */
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700733 journal->j_blocksize = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 n = journal->j_blocksize / sizeof(journal_block_tag_t);
735 journal->j_wbufsize = n;
736 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
737 if (!journal->j_wbuf) {
738 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700739 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700740 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700742 journal->j_dev = bdev;
743 journal->j_fs_dev = fs_dev;
744 journal->j_blk_offset = start;
745 journal->j_maxlen = len;
746
747 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700748 if (!bh) {
749 printk(KERN_ERR
750 "%s: Cannot get buffer for journal superblock\n",
751 __func__);
752 goto out_err;
753 }
Zoltan Menyhartd1807792006-09-29 01:58:40 -0700754 journal->j_sb_buffer = bh;
755 journal->j_superblock = (journal_superblock_t *)bh->b_data;
Jan Karaecca9af2009-04-02 16:57:13 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700758out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800759 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700760 kfree(journal);
761 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700763
764/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
766 * @inode: An inode to create the journal in
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * journal_init_inode creates a journal which maps an on-disk inode as
769 * the journal. The inode must exist already, must support bmap() and
770 * must have all data blocks preallocated.
771 */
772journal_t * journal_init_inode (struct inode *inode)
773{
774 struct buffer_head *bh;
775 journal_t *journal = journal_init_common();
776 int err;
777 int n;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200778 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (!journal)
781 return NULL;
782
783 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
784 journal->j_inode = inode;
785 jbd_debug(1,
786 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700787 journal, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 (long long) inode->i_size,
789 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
790
791 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
792 journal->j_blocksize = inode->i_sb->s_blocksize;
793
794 /* journal descriptor can store up to n blocks -bzzz */
795 n = journal->j_blocksize / sizeof(journal_block_tag_t);
796 journal->j_wbufsize = n;
797 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
798 if (!journal->j_wbuf) {
799 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700800 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700801 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 err = journal_bmap(journal, 0, &blocknr);
805 /* If that failed, give up */
806 if (err) {
807 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700808 __func__);
Jan Karaecca9af2009-04-02 16:57:13 -0700809 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
Jan Karaecca9af2009-04-02 16:57:13 -0700813 if (!bh) {
814 printk(KERN_ERR
815 "%s: Cannot get buffer for journal superblock\n",
816 __func__);
817 goto out_err;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 journal->j_sb_buffer = bh;
820 journal->j_superblock = (journal_superblock_t *)bh->b_data;
821
822 return journal;
Jan Karaecca9af2009-04-02 16:57:13 -0700823out_err:
Tao Ma7b02bec2009-11-10 17:13:22 +0800824 kfree(journal->j_wbuf);
Jan Karaecca9af2009-04-02 16:57:13 -0700825 kfree(journal);
826 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * If the journal init or create aborts, we need to mark the journal
831 * superblock as being NULL to prevent the journal destroy from writing
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700832 * back a bogus superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
834static void journal_fail_superblock (journal_t *journal)
835{
836 struct buffer_head *bh = journal->j_sb_buffer;
837 brelse(bh);
838 journal->j_sb_buffer = NULL;
839}
840
841/*
842 * Given a journal_t structure, initialise the various fields for
843 * startup of a new journaling session. We use this both when creating
844 * a journal, and after recovering an old journal to reset it for
845 * subsequent use.
846 */
847
848static int journal_reset(journal_t *journal)
849{
850 journal_superblock_t *sb = journal->j_superblock;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200851 unsigned int first, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 first = be32_to_cpu(sb->s_first);
854 last = be32_to_cpu(sb->s_maxlen);
Jan Kara7447a662009-07-15 20:36:08 +0200855 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
Jan Kara9c28cbc2009-08-03 19:21:00 +0200856 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
Jan Kara7447a662009-07-15 20:36:08 +0200857 first, last);
858 journal_fail_superblock(journal);
859 return -EINVAL;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 journal->j_first = first;
863 journal->j_last = last;
864
865 journal->j_head = first;
866 journal->j_tail = first;
867 journal->j_free = last - first;
868
869 journal->j_tail_sequence = journal->j_transaction_sequence;
870 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
871 journal->j_commit_request = journal->j_commit_sequence;
872
873 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
874
875 /* Add the dynamic fields and write it to disk. */
876 journal_update_superblock(journal, 1);
Pavel Emelianov97f06782007-05-08 00:30:42 -0700877 return journal_start_thread(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700880/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * int journal_create() - Initialise the new journal file
882 * @journal: Journal to create. This structure must have been initialised
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * Given a journal_t structure which tells us which disk blocks we can
885 * use, create a new journal superblock and initialise all of the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700886 * journal fields from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 **/
888int journal_create(journal_t *journal)
889{
Jan Kara9c28cbc2009-08-03 19:21:00 +0200890 unsigned int blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct buffer_head *bh;
892 journal_superblock_t *sb;
893 int i, err;
894
895 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
896 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
897 journal->j_maxlen);
898 journal_fail_superblock(journal);
899 return -EINVAL;
900 }
901
902 if (journal->j_inode == NULL) {
903 /*
904 * We don't know what block to start at!
905 */
906 printk(KERN_EMERG
907 "%s: creation of journal on external device!\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700908 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 BUG();
910 }
911
912 /* Zero out the entire journal on disk. We cannot afford to
913 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
914 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
915 for (i = 0; i < journal->j_maxlen; i++) {
916 err = journal_bmap(journal, i, &blocknr);
917 if (err)
918 return err;
919 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
920 lock_buffer(bh);
921 memset (bh->b_data, 0, journal->j_blocksize);
922 BUFFER_TRACE(bh, "marking dirty");
923 mark_buffer_dirty(bh);
924 BUFFER_TRACE(bh, "marking uptodate");
925 set_buffer_uptodate(bh);
926 unlock_buffer(bh);
927 __brelse(bh);
928 }
929
930 sync_blockdev(journal->j_dev);
931 jbd_debug(1, "JBD: journal cleared.\n");
932
933 /* OK, fill in the initial static fields in the new superblock */
934 sb = journal->j_superblock;
935
936 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
937 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
938
939 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
940 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
941 sb->s_first = cpu_to_be32(1);
942
943 journal->j_transaction_sequence = 1;
944
945 journal->j_flags &= ~JFS_ABORT;
946 journal->j_format_version = 2;
947
948 return journal_reset(journal);
949}
950
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700951/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * void journal_update_superblock() - Update journal sb on disk.
953 * @journal: The journal to update.
954 * @wait: Set to '0' if you don't want to wait for IO completion.
955 *
956 * Update a journal's dynamic superblock fields and write it to disk,
957 * optionally waiting for the IO to complete.
958 */
959void journal_update_superblock(journal_t *journal, int wait)
960{
961 journal_superblock_t *sb = journal->j_superblock;
962 struct buffer_head *bh = journal->j_sb_buffer;
963
964 /*
965 * As a special case, if the on-disk copy is already marked as needing
966 * no recovery (s_start == 0) and there are no outstanding transactions
967 * in the filesystem, then we can safely defer the superblock update
968 * until the next commit by setting JFS_FLUSHED. This avoids
969 * attempting a write to a potential-readonly device.
970 */
971 if (sb->s_start == 0 && journal->j_tail_sequence ==
972 journal->j_transaction_sequence) {
973 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
Jan Kara9c28cbc2009-08-03 19:21:00 +0200974 "(start %u, seq %d, errno %d)\n",
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700975 journal->j_tail, journal->j_tail_sequence,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 journal->j_errno);
977 goto out;
978 }
979
980 spin_lock(&journal->j_state_lock);
Jan Kara9c28cbc2009-08-03 19:21:00 +0200981 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
983
984 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
985 sb->s_start = cpu_to_be32(journal->j_tail);
986 sb->s_errno = cpu_to_be32(journal->j_errno);
987 spin_unlock(&journal->j_state_lock);
988
989 BUFFER_TRACE(bh, "marking dirty");
990 mark_buffer_dirty(bh);
991 if (wait)
992 sync_dirty_buffer(bh);
993 else
Jan Kara26707692005-09-06 15:19:12 -0700994 ll_rw_block(SWRITE, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996out:
997 /* If we have just flushed the log (by marking s_start==0), then
998 * any future commit will have to be careful to update the
999 * superblock again to re-record the true start of the log. */
1000
1001 spin_lock(&journal->j_state_lock);
1002 if (sb->s_start)
1003 journal->j_flags &= ~JFS_FLUSHED;
1004 else
1005 journal->j_flags |= JFS_FLUSHED;
1006 spin_unlock(&journal->j_state_lock);
1007}
1008
1009/*
1010 * Read the superblock for a given journal, performing initial
1011 * validation of the format.
1012 */
1013
1014static int journal_get_superblock(journal_t *journal)
1015{
1016 struct buffer_head *bh;
1017 journal_superblock_t *sb;
1018 int err = -EIO;
1019
1020 bh = journal->j_sb_buffer;
1021
1022 J_ASSERT(bh != NULL);
1023 if (!buffer_uptodate(bh)) {
1024 ll_rw_block(READ, 1, &bh);
1025 wait_on_buffer(bh);
1026 if (!buffer_uptodate(bh)) {
1027 printk (KERN_ERR
1028 "JBD: IO error reading journal superblock\n");
1029 goto out;
1030 }
1031 }
1032
1033 sb = journal->j_superblock;
1034
1035 err = -EINVAL;
1036
1037 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1038 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1039 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1040 goto out;
1041 }
1042
1043 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1044 case JFS_SUPERBLOCK_V1:
1045 journal->j_format_version = 1;
1046 break;
1047 case JFS_SUPERBLOCK_V2:
1048 journal->j_format_version = 2;
1049 break;
1050 default:
1051 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1052 goto out;
1053 }
1054
1055 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1056 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1057 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1058 printk (KERN_WARNING "JBD: journal file too short\n");
1059 goto out;
1060 }
1061
1062 return 0;
1063
1064out:
1065 journal_fail_superblock(journal);
1066 return err;
1067}
1068
1069/*
1070 * Load the on-disk journal superblock and read the key fields into the
1071 * journal_t.
1072 */
1073
1074static int load_superblock(journal_t *journal)
1075{
1076 int err;
1077 journal_superblock_t *sb;
1078
1079 err = journal_get_superblock(journal);
1080 if (err)
1081 return err;
1082
1083 sb = journal->j_superblock;
1084
1085 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1086 journal->j_tail = be32_to_cpu(sb->s_start);
1087 journal->j_first = be32_to_cpu(sb->s_first);
1088 journal->j_last = be32_to_cpu(sb->s_maxlen);
1089 journal->j_errno = be32_to_cpu(sb->s_errno);
1090
1091 return 0;
1092}
1093
1094
1095/**
1096 * int journal_load() - Read journal from disk.
1097 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * Given a journal_t structure which tells us which disk blocks contain
1100 * a journal, read the journal from disk to initialise the in-memory
1101 * structures.
1102 */
1103int journal_load(journal_t *journal)
1104{
1105 int err;
Badari Pulavartyea817392006-08-27 01:23:52 -07001106 journal_superblock_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 err = load_superblock(journal);
1109 if (err)
1110 return err;
1111
Badari Pulavartyea817392006-08-27 01:23:52 -07001112 sb = journal->j_superblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* If this is a V2 superblock, then we have to check the
1114 * features flags on it. */
1115
1116 if (journal->j_format_version >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if ((sb->s_feature_ro_compat &
1118 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1119 (sb->s_feature_incompat &
1120 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1121 printk (KERN_WARNING
1122 "JBD: Unrecognised features on journal\n");
1123 return -EINVAL;
1124 }
1125 }
1126
1127 /* Let the recovery code check whether it needs to recover any
1128 * data from the journal. */
1129 if (journal_recover(journal))
1130 goto recovery_error;
1131
1132 /* OK, we've finished with the dynamic journal bits:
1133 * reinitialise the dynamic contents of the superblock in memory
1134 * and reset them on disk. */
1135 if (journal_reset(journal))
1136 goto recovery_error;
1137
1138 journal->j_flags &= ~JFS_ABORT;
1139 journal->j_flags |= JFS_LOADED;
1140 return 0;
1141
1142recovery_error:
1143 printk (KERN_WARNING "JBD: recovery failed\n");
1144 return -EIO;
1145}
1146
1147/**
1148 * void journal_destroy() - Release a journal_t structure.
1149 * @journal: Journal to act on.
1150 *
1151 * Release a journal_t structure once it is no longer in use by the
1152 * journaled object.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001153 * Return <0 if we couldn't clean up the journal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001155int journal_destroy(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001157 int err = 0;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* Wait for the commit thread to wake up and die. */
1160 journal_kill_thread(journal);
1161
1162 /* Force a final log commit */
1163 if (journal->j_running_transaction)
1164 journal_commit_transaction(journal);
1165
1166 /* Force any old transactions to disk */
1167
1168 /* Totally anal locking here... */
1169 spin_lock(&journal->j_list_lock);
1170 while (journal->j_checkpoint_transactions != NULL) {
1171 spin_unlock(&journal->j_list_lock);
1172 log_do_checkpoint(journal);
1173 spin_lock(&journal->j_list_lock);
1174 }
1175
1176 J_ASSERT(journal->j_running_transaction == NULL);
1177 J_ASSERT(journal->j_committing_transaction == NULL);
1178 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1179 spin_unlock(&journal->j_list_lock);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (journal->j_sb_buffer) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001182 if (!is_journal_aborted(journal)) {
1183 /* We can now mark the journal as empty. */
1184 journal->j_tail = 0;
1185 journal->j_tail_sequence =
1186 ++journal->j_transaction_sequence;
1187 journal_update_superblock(journal, 1);
1188 } else {
1189 err = -EIO;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 brelse(journal->j_sb_buffer);
1192 }
1193
1194 if (journal->j_inode)
1195 iput(journal->j_inode);
1196 if (journal->j_revoke)
1197 journal_destroy_revoke(journal);
1198 kfree(journal->j_wbuf);
1199 kfree(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001200
1201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204
1205/**
1206 *int journal_check_used_features () - Check if features specified are used.
1207 * @journal: Journal to check.
1208 * @compat: bitmask of compatible features
1209 * @ro: bitmask of features that force read-only mount
1210 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001211 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * Check whether the journal uses all of a given set of
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001213 * features. Return true (non-zero) if it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 **/
1215
1216int journal_check_used_features (journal_t *journal, unsigned long compat,
1217 unsigned long ro, unsigned long incompat)
1218{
1219 journal_superblock_t *sb;
1220
1221 if (!compat && !ro && !incompat)
1222 return 1;
1223 if (journal->j_format_version == 1)
1224 return 0;
1225
1226 sb = journal->j_superblock;
1227
1228 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1229 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1230 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1231 return 1;
1232
1233 return 0;
1234}
1235
1236/**
1237 * int journal_check_available_features() - Check feature set in journalling layer
1238 * @journal: Journal to check.
1239 * @compat: bitmask of compatible features
1240 * @ro: bitmask of features that force read-only mount
1241 * @incompat: bitmask of incompatible features
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001242 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * Check whether the journaling code supports the use of
1244 * all of a given set of features on this journal. Return true
1245 * (non-zero) if it can. */
1246
1247int journal_check_available_features (journal_t *journal, unsigned long compat,
1248 unsigned long ro, unsigned long incompat)
1249{
1250 journal_superblock_t *sb;
1251
1252 if (!compat && !ro && !incompat)
1253 return 1;
1254
1255 sb = journal->j_superblock;
1256
1257 /* We can support any known requested features iff the
1258 * superblock is in version 2. Otherwise we fail to support any
1259 * extended sb features. */
1260
1261 if (journal->j_format_version != 2)
1262 return 0;
1263
1264 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1265 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1266 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1267 return 1;
1268
1269 return 0;
1270}
1271
1272/**
1273 * int journal_set_features () - Mark a given journal feature in the superblock
1274 * @journal: Journal to act on.
1275 * @compat: bitmask of compatible features
1276 * @ro: bitmask of features that force read-only mount
1277 * @incompat: bitmask of incompatible features
1278 *
1279 * Mark a given journal feature as present on the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001280 * superblock. Returns true if the requested features could be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 *
1282 */
1283
1284int journal_set_features (journal_t *journal, unsigned long compat,
1285 unsigned long ro, unsigned long incompat)
1286{
1287 journal_superblock_t *sb;
1288
1289 if (journal_check_used_features(journal, compat, ro, incompat))
1290 return 1;
1291
1292 if (!journal_check_available_features(journal, compat, ro, incompat))
1293 return 0;
1294
1295 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1296 compat, ro, incompat);
1297
1298 sb = journal->j_superblock;
1299
1300 sb->s_feature_compat |= cpu_to_be32(compat);
1301 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1302 sb->s_feature_incompat |= cpu_to_be32(incompat);
1303
1304 return 1;
1305}
1306
1307
1308/**
1309 * int journal_update_format () - Update on-disk journal structure.
1310 * @journal: Journal to act on.
1311 *
1312 * Given an initialised but unloaded journal struct, poke about in the
1313 * on-disk structure to update it to the most recent supported version.
1314 */
1315int journal_update_format (journal_t *journal)
1316{
1317 journal_superblock_t *sb;
1318 int err;
1319
1320 err = journal_get_superblock(journal);
1321 if (err)
1322 return err;
1323
1324 sb = journal->j_superblock;
1325
1326 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1327 case JFS_SUPERBLOCK_V2:
1328 return 0;
1329 case JFS_SUPERBLOCK_V1:
1330 return journal_convert_superblock_v1(journal, sb);
1331 default:
1332 break;
1333 }
1334 return -EINVAL;
1335}
1336
1337static int journal_convert_superblock_v1(journal_t *journal,
1338 journal_superblock_t *sb)
1339{
1340 int offset, blocksize;
1341 struct buffer_head *bh;
1342
1343 printk(KERN_WARNING
1344 "JBD: Converting superblock from version 1 to 2.\n");
1345
1346 /* Pre-initialise new fields to zero */
1347 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1348 blocksize = be32_to_cpu(sb->s_blocksize);
1349 memset(&sb->s_feature_compat, 0, blocksize-offset);
1350
1351 sb->s_nr_users = cpu_to_be32(1);
1352 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1353 journal->j_format_version = 2;
1354
1355 bh = journal->j_sb_buffer;
1356 BUFFER_TRACE(bh, "marking dirty");
1357 mark_buffer_dirty(bh);
1358 sync_dirty_buffer(bh);
1359 return 0;
1360}
1361
1362
1363/**
1364 * int journal_flush () - Flush journal
1365 * @journal: Journal to act on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001366 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * Flush all data for a given journal to disk and empty the journal.
1368 * Filesystems can use this when remounting readonly to ensure that
1369 * recovery does not need to happen on remount.
1370 */
1371
1372int journal_flush(journal_t *journal)
1373{
1374 int err = 0;
1375 transaction_t *transaction = NULL;
Jan Kara9c28cbc2009-08-03 19:21:00 +02001376 unsigned int old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 spin_lock(&journal->j_state_lock);
1379
1380 /* Force everything buffered to the log... */
1381 if (journal->j_running_transaction) {
1382 transaction = journal->j_running_transaction;
1383 __log_start_commit(journal, transaction->t_tid);
1384 } else if (journal->j_committing_transaction)
1385 transaction = journal->j_committing_transaction;
1386
1387 /* Wait for the log commit to complete... */
1388 if (transaction) {
1389 tid_t tid = transaction->t_tid;
1390
1391 spin_unlock(&journal->j_state_lock);
1392 log_wait_commit(journal, tid);
1393 } else {
1394 spin_unlock(&journal->j_state_lock);
1395 }
1396
1397 /* ...and flush everything in the log out to disk. */
1398 spin_lock(&journal->j_list_lock);
1399 while (!err && journal->j_checkpoint_transactions != NULL) {
1400 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001401 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 err = log_do_checkpoint(journal);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001403 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 spin_lock(&journal->j_list_lock);
1405 }
1406 spin_unlock(&journal->j_list_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001407
1408 if (is_journal_aborted(journal))
1409 return -EIO;
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 cleanup_journal_tail(journal);
1412
1413 /* Finally, mark the journal as really needing no recovery.
1414 * This sets s_start==0 in the underlying superblock, which is
1415 * the magic code for a fully-recovered superblock. Any future
1416 * commits of data to the journal will restore the current
1417 * s_start value. */
1418 spin_lock(&journal->j_state_lock);
1419 old_tail = journal->j_tail;
1420 journal->j_tail = 0;
1421 spin_unlock(&journal->j_state_lock);
1422 journal_update_superblock(journal, 1);
1423 spin_lock(&journal->j_state_lock);
1424 journal->j_tail = old_tail;
1425
1426 J_ASSERT(!journal->j_running_transaction);
1427 J_ASSERT(!journal->j_committing_transaction);
1428 J_ASSERT(!journal->j_checkpoint_transactions);
1429 J_ASSERT(journal->j_head == journal->j_tail);
1430 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1431 spin_unlock(&journal->j_state_lock);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -07001432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435/**
1436 * int journal_wipe() - Wipe journal contents
1437 * @journal: Journal to act on.
1438 * @write: flag (see below)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * Wipe out all of the contents of a journal, safely. This will produce
1441 * a warning if the journal contains any valid recovery information.
1442 * Must be called between journal_init_*() and journal_load().
1443 *
1444 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1445 * we merely suppress recovery.
1446 */
1447
1448int journal_wipe(journal_t *journal, int write)
1449{
1450 journal_superblock_t *sb;
1451 int err = 0;
1452
1453 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1454
1455 err = load_superblock(journal);
1456 if (err)
1457 return err;
1458
1459 sb = journal->j_superblock;
1460
1461 if (!journal->j_tail)
1462 goto no_recovery;
1463
1464 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1465 write ? "Clearing" : "Ignoring");
1466
1467 err = journal_skip_recovery(journal);
1468 if (write)
1469 journal_update_superblock(journal, 1);
1470
1471 no_recovery:
1472 return err;
1473}
1474
1475/*
1476 * journal_dev_name: format a character string to describe on what
1477 * device this journal is present.
1478 */
1479
Adrian Bunk022a4a72005-09-06 15:16:41 -07001480static const char *journal_dev_name(journal_t *journal, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct block_device *bdev;
1483
1484 if (journal->j_inode)
1485 bdev = journal->j_inode->i_sb->s_bdev;
1486 else
1487 bdev = journal->j_dev;
1488
1489 return bdevname(bdev, buffer);
1490}
1491
1492/*
1493 * Journal abort has very specific semantics, which we describe
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001494 * for journal abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
1496 * Two internal function, which provide abort to te jbd layer
1497 * itself are here.
1498 */
1499
1500/*
1501 * Quick version for internal journal use (doesn't lock the journal).
1502 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1503 * and don't attempt to make any other journal updates.
1504 */
Adrian Bunk53308382008-02-06 01:40:12 -08001505static void __journal_abort_hard(journal_t *journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 transaction_t *transaction;
1508 char b[BDEVNAME_SIZE];
1509
1510 if (journal->j_flags & JFS_ABORT)
1511 return;
1512
1513 printk(KERN_ERR "Aborting journal on device %s.\n",
1514 journal_dev_name(journal, b));
1515
1516 spin_lock(&journal->j_state_lock);
1517 journal->j_flags |= JFS_ABORT;
1518 transaction = journal->j_running_transaction;
1519 if (transaction)
1520 __log_start_commit(journal, transaction->t_tid);
1521 spin_unlock(&journal->j_state_lock);
1522}
1523
1524/* Soft abort: record the abort error status in the journal superblock,
1525 * but don't do any other IO. */
Adrian Bunk022a4a72005-09-06 15:16:41 -07001526static void __journal_abort_soft (journal_t *journal, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 if (journal->j_flags & JFS_ABORT)
1529 return;
1530
1531 if (!journal->j_errno)
1532 journal->j_errno = errno;
1533
1534 __journal_abort_hard(journal);
1535
1536 if (errno)
1537 journal_update_superblock(journal, 1);
1538}
1539
1540/**
1541 * void journal_abort () - Shutdown the journal immediately.
1542 * @journal: the journal to shutdown.
1543 * @errno: an error number to record in the journal indicating
1544 * the reason for the shutdown.
1545 *
1546 * Perform a complete, immediate shutdown of the ENTIRE
1547 * journal (not of a single transaction). This operation cannot be
1548 * undone without closing and reopening the journal.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * The journal_abort function is intended to support higher level error
1551 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1552 * mode.
1553 *
1554 * Journal abort has very specific semantics. Any existing dirty,
1555 * unjournaled buffers in the main filesystem will still be written to
1556 * disk by bdflush, but the journaling mechanism will be suspended
1557 * immediately and no further transaction commits will be honoured.
1558 *
1559 * Any dirty, journaled buffers will be written back to disk without
1560 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1561 * filesystem, but we _do_ attempt to leave as much data as possible
1562 * behind for fsck to use for cleanup.
1563 *
1564 * Any attempt to get a new transaction handle on a journal which is in
1565 * ABORT state will just result in an -EROFS error return. A
1566 * journal_stop on an existing handle will return -EIO if we have
1567 * entered abort state during the update.
1568 *
1569 * Recursive transactions are not disturbed by journal abort until the
1570 * final journal_stop, which will receive the -EIO error.
1571 *
1572 * Finally, the journal_abort call allows the caller to supply an errno
1573 * which will be recorded (if possible) in the journal superblock. This
1574 * allows a client to record failure conditions in the middle of a
1575 * transaction without having to complete the transaction to record the
1576 * failure to disk. ext3_error, for example, now uses this
1577 * functionality.
1578 *
1579 * Errors which originate from within the journaling layer will NOT
1580 * supply an errno; a null errno implies that absolutely no further
1581 * writes are done to the journal (unless there are any already in
1582 * progress).
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585
1586void journal_abort(journal_t *journal, int errno)
1587{
1588 __journal_abort_soft(journal, errno);
1589}
1590
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001591/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * int journal_errno () - returns the journal's error state.
1593 * @journal: journal to examine.
1594 *
1595 * This is the errno numbet set with journal_abort(), the last
1596 * time the journal was mounted - if the journal was stopped
1597 * without calling abort this will be 0.
1598 *
1599 * If the journal has been aborted on this mount time -EROFS will
1600 * be returned.
1601 */
1602int journal_errno(journal_t *journal)
1603{
1604 int err;
1605
1606 spin_lock(&journal->j_state_lock);
1607 if (journal->j_flags & JFS_ABORT)
1608 err = -EROFS;
1609 else
1610 err = journal->j_errno;
1611 spin_unlock(&journal->j_state_lock);
1612 return err;
1613}
1614
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001615/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * int journal_clear_err () - clears the journal's error state
1617 * @journal: journal to act on.
1618 *
1619 * An error must be cleared or Acked to take a FS out of readonly
1620 * mode.
1621 */
1622int journal_clear_err(journal_t *journal)
1623{
1624 int err = 0;
1625
1626 spin_lock(&journal->j_state_lock);
1627 if (journal->j_flags & JFS_ABORT)
1628 err = -EROFS;
1629 else
1630 journal->j_errno = 0;
1631 spin_unlock(&journal->j_state_lock);
1632 return err;
1633}
1634
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001635/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * void journal_ack_err() - Ack journal err.
1637 * @journal: journal to act on.
1638 *
1639 * An error must be cleared or Acked to take a FS out of readonly
1640 * mode.
1641 */
1642void journal_ack_err(journal_t *journal)
1643{
1644 spin_lock(&journal->j_state_lock);
1645 if (journal->j_errno)
1646 journal->j_flags |= JFS_ACK_ERR;
1647 spin_unlock(&journal->j_state_lock);
1648}
1649
1650int journal_blocks_per_page(struct inode *inode)
1651{
1652 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1653}
1654
1655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * Journal_head storage management
1657 */
Christoph Lametere18b8902006-12-06 20:33:20 -08001658static struct kmem_cache *journal_head_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659#ifdef CONFIG_JBD_DEBUG
1660static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1661#endif
1662
1663static int journal_init_journal_head_cache(void)
1664{
1665 int retval;
1666
Al Viro1076d172008-03-29 03:07:18 +00001667 J_ASSERT(journal_head_cache == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 journal_head_cache = kmem_cache_create("journal_head",
1669 sizeof(struct journal_head),
1670 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001671 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001672 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 retval = 0;
Al Viro1076d172008-03-29 03:07:18 +00001674 if (!journal_head_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 retval = -ENOMEM;
1676 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1677 }
1678 return retval;
1679}
1680
1681static void journal_destroy_journal_head_cache(void)
1682{
Duane Griffin3850f7a2008-07-25 01:46:19 -07001683 if (journal_head_cache) {
1684 kmem_cache_destroy(journal_head_cache);
1685 journal_head_cache = NULL;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/*
1690 * journal_head splicing and dicing
1691 */
1692static struct journal_head *journal_alloc_journal_head(void)
1693{
1694 struct journal_head *ret;
1695 static unsigned long last_warning;
1696
1697#ifdef CONFIG_JBD_DEBUG
1698 atomic_inc(&nr_journal_heads);
1699#endif
1700 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001701 if (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 jbd_debug(1, "out of memory for journal_head\n");
1703 if (time_after(jiffies, last_warning + 5*HZ)) {
1704 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001705 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 last_warning = jiffies;
1707 }
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001708 while (ret == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 yield();
1710 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1711 }
1712 }
1713 return ret;
1714}
1715
1716static void journal_free_journal_head(struct journal_head *jh)
1717{
1718#ifdef CONFIG_JBD_DEBUG
1719 atomic_dec(&nr_journal_heads);
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001720 memset(jh, JBD_POISON_FREE, sizeof(*jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721#endif
1722 kmem_cache_free(journal_head_cache, jh);
1723}
1724
1725/*
1726 * A journal_head is attached to a buffer_head whenever JBD has an
1727 * interest in the buffer.
1728 *
1729 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1730 * is set. This bit is tested in core kernel code where we need to take
1731 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1732 * there.
1733 *
1734 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1735 *
1736 * When a buffer has its BH_JBD bit set it is immune from being released by
1737 * core kernel code, mainly via ->b_count.
1738 *
1739 * A journal_head may be detached from its buffer_head when the journal_head's
1740 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1741 * Various places in JBD call journal_remove_journal_head() to indicate that the
1742 * journal_head can be dropped if needed.
1743 *
1744 * Various places in the kernel want to attach a journal_head to a buffer_head
1745 * _before_ attaching the journal_head to a transaction. To protect the
1746 * journal_head in this situation, journal_add_journal_head elevates the
1747 * journal_head's b_jcount refcount by one. The caller must call
1748 * journal_put_journal_head() to undo this.
1749 *
1750 * So the typical usage would be:
1751 *
1752 * (Attach a journal_head if needed. Increments b_jcount)
1753 * struct journal_head *jh = journal_add_journal_head(bh);
1754 * ...
1755 * jh->b_transaction = xxx;
1756 * journal_put_journal_head(jh);
1757 *
1758 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1759 * because it has a non-zero b_transaction.
1760 */
1761
1762/*
1763 * Give a buffer_head a journal_head.
1764 *
1765 * Doesn't need the journal lock.
1766 * May sleep.
1767 */
1768struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1769{
1770 struct journal_head *jh;
1771 struct journal_head *new_jh = NULL;
1772
1773repeat:
1774 if (!buffer_jbd(bh)) {
1775 new_jh = journal_alloc_journal_head();
1776 memset(new_jh, 0, sizeof(*new_jh));
1777 }
1778
1779 jbd_lock_bh_journal_head(bh);
1780 if (buffer_jbd(bh)) {
1781 jh = bh2jh(bh);
1782 } else {
1783 J_ASSERT_BH(bh,
1784 (atomic_read(&bh->b_count) > 0) ||
1785 (bh->b_page && bh->b_page->mapping));
1786
1787 if (!new_jh) {
1788 jbd_unlock_bh_journal_head(bh);
1789 goto repeat;
1790 }
1791
1792 jh = new_jh;
1793 new_jh = NULL; /* We consumed it */
1794 set_buffer_jbd(bh);
1795 bh->b_private = jh;
1796 jh->b_bh = bh;
1797 get_bh(bh);
1798 BUFFER_TRACE(bh, "added journal_head");
1799 }
1800 jh->b_jcount++;
1801 jbd_unlock_bh_journal_head(bh);
1802 if (new_jh)
1803 journal_free_journal_head(new_jh);
1804 return bh->b_private;
1805}
1806
1807/*
1808 * Grab a ref against this buffer_head's journal_head. If it ended up not
1809 * having a journal_head, return NULL
1810 */
1811struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1812{
1813 struct journal_head *jh = NULL;
1814
1815 jbd_lock_bh_journal_head(bh);
1816 if (buffer_jbd(bh)) {
1817 jh = bh2jh(bh);
1818 jh->b_jcount++;
1819 }
1820 jbd_unlock_bh_journal_head(bh);
1821 return jh;
1822}
1823
1824static void __journal_remove_journal_head(struct buffer_head *bh)
1825{
1826 struct journal_head *jh = bh2jh(bh);
1827
1828 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1829
1830 get_bh(bh);
1831 if (jh->b_jcount == 0) {
1832 if (jh->b_transaction == NULL &&
1833 jh->b_next_transaction == NULL &&
1834 jh->b_cp_transaction == NULL) {
1835 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1836 J_ASSERT_BH(bh, buffer_jbd(bh));
1837 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1838 BUFFER_TRACE(bh, "remove journal_head");
1839 if (jh->b_frozen_data) {
1840 printk(KERN_WARNING "%s: freeing "
1841 "b_frozen_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001842 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001843 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845 if (jh->b_committed_data) {
1846 printk(KERN_WARNING "%s: freeing "
1847 "b_committed_data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -07001848 __func__);
Mingming Caoc089d492007-10-16 18:38:25 -04001849 jbd_free(jh->b_committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851 bh->b_private = NULL;
1852 jh->b_bh = NULL; /* debug, really */
1853 clear_buffer_jbd(bh);
1854 __brelse(bh);
1855 journal_free_journal_head(jh);
1856 } else {
1857 BUFFER_TRACE(bh, "journal_head was locked");
1858 }
1859 }
1860}
1861
1862/*
1863 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1864 * and has a zero b_jcount then remove and release its journal_head. If we did
1865 * see that the buffer is not used by any transaction we also "logically"
1866 * decrement ->b_count.
1867 *
1868 * We in fact take an additional increment on ->b_count as a convenience,
1869 * because the caller usually wants to do additional things with the bh
1870 * after calling here.
1871 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1872 * time. Once the caller has run __brelse(), the buffer is eligible for
1873 * reaping by try_to_free_buffers().
1874 */
1875void journal_remove_journal_head(struct buffer_head *bh)
1876{
1877 jbd_lock_bh_journal_head(bh);
1878 __journal_remove_journal_head(bh);
1879 jbd_unlock_bh_journal_head(bh);
1880}
1881
1882/*
1883 * Drop a reference on the passed journal_head. If it fell to zero then try to
1884 * release the journal_head from the buffer_head.
1885 */
1886void journal_put_journal_head(struct journal_head *jh)
1887{
1888 struct buffer_head *bh = jh2bh(jh);
1889
1890 jbd_lock_bh_journal_head(bh);
1891 J_ASSERT_JH(jh, jh->b_jcount > 0);
1892 --jh->b_jcount;
1893 if (!jh->b_jcount && !jh->b_transaction) {
1894 __journal_remove_journal_head(bh);
1895 __brelse(bh);
1896 }
1897 jbd_unlock_bh_journal_head(bh);
1898}
1899
1900/*
Jose R. Santosc2a91592007-10-18 23:39:22 -07001901 * debugfs tunables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Jose R. Santosc2a91592007-10-18 23:39:22 -07001903#ifdef CONFIG_JBD_DEBUG
1904
1905u8 journal_enable_debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906EXPORT_SYMBOL(journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Jose R. Santosc2a91592007-10-18 23:39:22 -07001908static struct dentry *jbd_debugfs_dir;
1909static struct dentry *jbd_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Jose R. Santosc2a91592007-10-18 23:39:22 -07001911static void __init jbd_create_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001913 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1914 if (jbd_debugfs_dir)
1915 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO,
1916 jbd_debugfs_dir,
1917 &journal_enable_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Jose R. Santosc2a91592007-10-18 23:39:22 -07001920static void __exit jbd_remove_debugfs_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Jose R. Santosc2a91592007-10-18 23:39:22 -07001922 debugfs_remove(jbd_debug);
1923 debugfs_remove(jbd_debugfs_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926#else
1927
Jose R. Santosc2a91592007-10-18 23:39:22 -07001928static inline void jbd_create_debugfs_entry(void)
1929{
1930}
1931
1932static inline void jbd_remove_debugfs_entry(void)
1933{
1934}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936#endif
1937
Christoph Lametere18b8902006-12-06 20:33:20 -08001938struct kmem_cache *jbd_handle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940static int __init journal_init_handle_cache(void)
1941{
1942 jbd_handle_cache = kmem_cache_create("journal_handle",
1943 sizeof(handle_t),
1944 0, /* offset */
Mel Gormane12ba742007-10-16 01:25:52 -07001945 SLAB_TEMPORARY, /* flags */
Paul Mundt20c2df82007-07-20 10:11:58 +09001946 NULL); /* ctor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (jbd_handle_cache == NULL) {
1948 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1949 return -ENOMEM;
1950 }
1951 return 0;
1952}
1953
1954static void journal_destroy_handle_cache(void)
1955{
1956 if (jbd_handle_cache)
1957 kmem_cache_destroy(jbd_handle_cache);
1958}
1959
1960/*
1961 * Module startup and shutdown
1962 */
1963
1964static int __init journal_init_caches(void)
1965{
1966 int ret;
1967
1968 ret = journal_init_revoke_caches();
1969 if (ret == 0)
1970 ret = journal_init_journal_head_cache();
1971 if (ret == 0)
1972 ret = journal_init_handle_cache();
1973 return ret;
1974}
1975
1976static void journal_destroy_caches(void)
1977{
1978 journal_destroy_revoke_caches();
1979 journal_destroy_journal_head_cache();
1980 journal_destroy_handle_cache();
1981}
1982
1983static int __init journal_init(void)
1984{
1985 int ret;
1986
Alexey Dobriyan2aed3482006-09-27 01:49:28 -07001987 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
Adrian Bunk022a4a72005-09-06 15:16:41 -07001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 ret = journal_init_caches();
1990 if (ret != 0)
1991 journal_destroy_caches();
Jose R. Santosc2a91592007-10-18 23:39:22 -07001992 jbd_create_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return ret;
1994}
1995
1996static void __exit journal_exit(void)
1997{
1998#ifdef CONFIG_JBD_DEBUG
1999 int n = atomic_read(&nr_journal_heads);
2000 if (n)
2001 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2002#endif
Jose R. Santosc2a91592007-10-18 23:39:22 -07002003 jbd_remove_debugfs_entry();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 journal_destroy_caches();
2005}
2006
2007MODULE_LICENSE("GPL");
2008module_init(journal_init);
2009module_exit(journal_exit);
2010