94a64a199a63a5b6ab389450d701a43e325582bf
[deliverable/linux.git] / fs / jbd / journal.c
1 /*
2 * linux/fs/jbd/journal.c
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>
28 #include <linux/jbd.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/page.h>
42
43 EXPORT_SYMBOL(journal_start);
44 EXPORT_SYMBOL(journal_restart);
45 EXPORT_SYMBOL(journal_extend);
46 EXPORT_SYMBOL(journal_stop);
47 EXPORT_SYMBOL(journal_lock_updates);
48 EXPORT_SYMBOL(journal_unlock_updates);
49 EXPORT_SYMBOL(journal_get_write_access);
50 EXPORT_SYMBOL(journal_get_create_access);
51 EXPORT_SYMBOL(journal_get_undo_access);
52 EXPORT_SYMBOL(journal_dirty_data);
53 EXPORT_SYMBOL(journal_dirty_metadata);
54 EXPORT_SYMBOL(journal_release_buffer);
55 EXPORT_SYMBOL(journal_forget);
56 #if 0
57 EXPORT_SYMBOL(journal_sync_buffer);
58 #endif
59 EXPORT_SYMBOL(journal_flush);
60 EXPORT_SYMBOL(journal_revoke);
61
62 EXPORT_SYMBOL(journal_init_dev);
63 EXPORT_SYMBOL(journal_init_inode);
64 EXPORT_SYMBOL(journal_update_format);
65 EXPORT_SYMBOL(journal_check_used_features);
66 EXPORT_SYMBOL(journal_check_available_features);
67 EXPORT_SYMBOL(journal_set_features);
68 EXPORT_SYMBOL(journal_create);
69 EXPORT_SYMBOL(journal_load);
70 EXPORT_SYMBOL(journal_destroy);
71 EXPORT_SYMBOL(journal_abort);
72 EXPORT_SYMBOL(journal_errno);
73 EXPORT_SYMBOL(journal_ack_err);
74 EXPORT_SYMBOL(journal_clear_err);
75 EXPORT_SYMBOL(log_wait_commit);
76 EXPORT_SYMBOL(journal_start_commit);
77 EXPORT_SYMBOL(journal_force_commit_nested);
78 EXPORT_SYMBOL(journal_wipe);
79 EXPORT_SYMBOL(journal_blocks_per_page);
80 EXPORT_SYMBOL(journal_invalidatepage);
81 EXPORT_SYMBOL(journal_try_to_free_buffers);
82 EXPORT_SYMBOL(journal_force_commit);
83
84 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
85 static void __journal_abort_soft (journal_t *journal, int errno);
86
87 /*
88 * Helper function used to manage commit timeouts
89 */
90
91 static void commit_timeout(unsigned long __data)
92 {
93 struct task_struct * p = (struct task_struct *) __data;
94
95 wake_up_process(p);
96 }
97
98 /*
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
114 static int kjournald(void *arg)
115 {
116 journal_t *journal = arg;
117 transaction_t *transaction;
118
119 /*
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);
125
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
138 loop:
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);
148 del_timer_sync(&journal->j_commit_timer);
149 journal_commit_transaction(journal);
150 spin_lock(&journal->j_state_lock);
151 goto loop;
152 }
153
154 wake_up(&journal->j_wait_done_commit);
155 if (freezing(current)) {
156 /*
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);
163 refrigerator();
164 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;
181 if (journal->j_flags & JFS_UNMOUNT)
182 should_sleep = 0;
183 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
203 end_loop:
204 spin_unlock(&journal->j_state_lock);
205 del_timer_sync(&journal->j_commit_timer);
206 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
212 static int journal_start_thread(journal_t *journal)
213 {
214 struct task_struct *t;
215
216 t = kthread_run(kjournald, journal, "kjournald");
217 if (IS_ERR(t))
218 return PTR_ERR(t);
219
220 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
221 return 0;
222 }
223
224 static 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);
232 wait_event(journal->j_wait_done_commit,
233 journal->j_task == NULL);
234 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
276 int journal_write_metadata_buffer(transaction_t *transaction,
277 struct journal_head *jh_in,
278 struct journal_head **jh_out,
279 unsigned long blocknr)
280 {
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);
290
291 /*
292 * The buffer really shouldn't be locked: only the current committing
293 * transaction is allowed to write it, so nobody else is allowed
294 * to do any IO.
295 *
296 * akpm: except if we're journalling data, and write() output is
297 * also part of a shared mapping, and another thread has
298 * decided to launch a writepage() against this buffer.
299 */
300 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
301
302 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
303
304 /*
305 * If a new transaction has already done a buffer copy-out, then
306 * we use that version of the data for the commit.
307 */
308 jbd_lock_bh_state(bh_in);
309 repeat:
310 if (jh_in->b_frozen_data) {
311 done_copy_out = 1;
312 new_page = virt_to_page(jh_in->b_frozen_data);
313 new_offset = offset_in_page(jh_in->b_frozen_data);
314 } else {
315 new_page = jh2bh(jh_in)->b_page;
316 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
317 }
318
319 mapped_data = kmap_atomic(new_page, KM_USER0);
320 /*
321 * Check for escaping
322 */
323 if (*((__be32 *)(mapped_data + new_offset)) ==
324 cpu_to_be32(JFS_MAGIC_NUMBER)) {
325 need_copy_out = 1;
326 do_escape = 1;
327 }
328 kunmap_atomic(mapped_data, KM_USER0);
329
330 /*
331 * Do we need to do a data copy?
332 */
333 if (need_copy_out && !done_copy_out) {
334 char *tmp;
335
336 jbd_unlock_bh_state(bh_in);
337 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
338 jbd_lock_bh_state(bh_in);
339 if (jh_in->b_frozen_data) {
340 jbd_free(tmp, bh_in->b_size);
341 goto repeat;
342 }
343
344 jh_in->b_frozen_data = tmp;
345 mapped_data = kmap_atomic(new_page, KM_USER0);
346 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
347 kunmap_atomic(mapped_data, KM_USER0);
348
349 new_page = virt_to_page(tmp);
350 new_offset = offset_in_page(tmp);
351 done_copy_out = 1;
352 }
353
354 /*
355 * Did we need to do an escaping? Now we've done all the
356 * copying, we can finally do so.
357 */
358 if (do_escape) {
359 mapped_data = kmap_atomic(new_page, KM_USER0);
360 *((unsigned int *)(mapped_data + new_offset)) = 0;
361 kunmap_atomic(mapped_data, KM_USER0);
362 }
363
364 /* keep subsequent assertions sane */
365 new_bh->b_state = 0;
366 init_buffer(new_bh, NULL, NULL);
367 atomic_set(&new_bh->b_count, 1);
368 jbd_unlock_bh_state(bh_in);
369
370 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
371
372 set_bh_page(new_bh, new_page, new_offset);
373 new_jh->b_transaction = NULL;
374 new_bh->b_size = jh2bh(jh_in)->b_size;
375 new_bh->b_bdev = transaction->t_journal->j_dev;
376 new_bh->b_blocknr = blocknr;
377 set_buffer_mapped(new_bh);
378 set_buffer_dirty(new_bh);
379
380 *jh_out = new_jh;
381
382 /*
383 * The to-be-written buffer needs to get moved to the io queue,
384 * and the original buffer whose contents we are shadowing or
385 * copying is moved to the transaction's shadow queue.
386 */
387 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
388 journal_file_buffer(jh_in, transaction, BJ_Shadow);
389 JBUFFER_TRACE(new_jh, "file as BJ_IO");
390 journal_file_buffer(new_jh, transaction, BJ_IO);
391
392 return do_escape | (done_copy_out << 1);
393 }
394
395 /*
396 * Allocation code for the journal file. Manage the space left in the
397 * journal, so that we can begin checkpointing when appropriate.
398 */
399
400 /*
401 * __log_space_left: Return the number of free blocks left in the journal.
402 *
403 * Called with the journal already locked.
404 *
405 * Called under j_state_lock
406 */
407
408 int __log_space_left(journal_t *journal)
409 {
410 int left = journal->j_free;
411
412 assert_spin_locked(&journal->j_state_lock);
413
414 /*
415 * Be pessimistic here about the number of those free blocks which
416 * might be required for log descriptor control blocks.
417 */
418
419 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
420
421 left -= MIN_LOG_RESERVED_BLOCKS;
422
423 if (left <= 0)
424 return 0;
425 left -= (left >> 3);
426 return left;
427 }
428
429 /*
430 * Called under j_state_lock. Returns true if a transaction commit was started.
431 */
432 int __log_start_commit(journal_t *journal, tid_t target)
433 {
434 /*
435 * Are we already doing a recent enough commit?
436 */
437 if (!tid_geq(journal->j_commit_request, target)) {
438 /*
439 * We want a new commit: OK, mark the request and wakup the
440 * commit thread. We do _not_ do the commit ourselves.
441 */
442
443 journal->j_commit_request = target;
444 jbd_debug(1, "JBD: requesting commit %d/%d\n",
445 journal->j_commit_request,
446 journal->j_commit_sequence);
447 wake_up(&journal->j_wait_commit);
448 return 1;
449 }
450 return 0;
451 }
452
453 int log_start_commit(journal_t *journal, tid_t tid)
454 {
455 int ret;
456
457 spin_lock(&journal->j_state_lock);
458 ret = __log_start_commit(journal, tid);
459 spin_unlock(&journal->j_state_lock);
460 return ret;
461 }
462
463 /*
464 * Force and wait upon a commit if the calling process is not within
465 * transaction. This is used for forcing out undo-protected data which contains
466 * bitmaps, when the fs is running out of space.
467 *
468 * We can only force the running transaction if we don't have an active handle;
469 * otherwise, we will deadlock.
470 *
471 * Returns true if a transaction was started.
472 */
473 int journal_force_commit_nested(journal_t *journal)
474 {
475 transaction_t *transaction = NULL;
476 tid_t tid;
477
478 spin_lock(&journal->j_state_lock);
479 if (journal->j_running_transaction && !current->journal_info) {
480 transaction = journal->j_running_transaction;
481 __log_start_commit(journal, transaction->t_tid);
482 } else if (journal->j_committing_transaction)
483 transaction = journal->j_committing_transaction;
484
485 if (!transaction) {
486 spin_unlock(&journal->j_state_lock);
487 return 0; /* Nothing to retry */
488 }
489
490 tid = transaction->t_tid;
491 spin_unlock(&journal->j_state_lock);
492 log_wait_commit(journal, tid);
493 return 1;
494 }
495
496 /*
497 * Start a commit of the current running transaction (if any). Returns true
498 * if a transaction is going to be committed (or is currently already
499 * committing), and fills its tid in at *ptid
500 */
501 int journal_start_commit(journal_t *journal, tid_t *ptid)
502 {
503 int ret = 0;
504
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction) {
507 tid_t tid = journal->j_running_transaction->t_tid;
508
509 __log_start_commit(journal, tid);
510 /* There's a running transaction and we've just made sure
511 * it's commit has been scheduled. */
512 if (ptid)
513 *ptid = tid;
514 ret = 1;
515 } else if (journal->j_committing_transaction) {
516 /*
517 * If ext3_write_super() recently started a commit, then we
518 * have to wait for completion of that transaction
519 */
520 if (ptid)
521 *ptid = journal->j_committing_transaction->t_tid;
522 ret = 1;
523 }
524 spin_unlock(&journal->j_state_lock);
525 return ret;
526 }
527
528 /*
529 * Wait for a specified commit to complete.
530 * The caller may not hold the journal lock.
531 */
532 int log_wait_commit(journal_t *journal, tid_t tid)
533 {
534 int err = 0;
535
536 #ifdef CONFIG_JBD_DEBUG
537 spin_lock(&journal->j_state_lock);
538 if (!tid_geq(journal->j_commit_request, tid)) {
539 printk(KERN_EMERG
540 "%s: error: j_commit_request=%d, tid=%d\n",
541 __func__, journal->j_commit_request, tid);
542 }
543 spin_unlock(&journal->j_state_lock);
544 #endif
545 spin_lock(&journal->j_state_lock);
546 while (tid_gt(tid, journal->j_commit_sequence)) {
547 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
548 tid, journal->j_commit_sequence);
549 wake_up(&journal->j_wait_commit);
550 spin_unlock(&journal->j_state_lock);
551 wait_event(journal->j_wait_done_commit,
552 !tid_gt(tid, journal->j_commit_sequence));
553 spin_lock(&journal->j_state_lock);
554 }
555 spin_unlock(&journal->j_state_lock);
556
557 if (unlikely(is_journal_aborted(journal))) {
558 printk(KERN_EMERG "journal commit I/O error\n");
559 err = -EIO;
560 }
561 return err;
562 }
563
564 /*
565 * Log buffer allocation routines:
566 */
567
568 int journal_next_log_block(journal_t *journal, unsigned long *retp)
569 {
570 unsigned long blocknr;
571
572 spin_lock(&journal->j_state_lock);
573 J_ASSERT(journal->j_free > 1);
574
575 blocknr = journal->j_head;
576 journal->j_head++;
577 journal->j_free--;
578 if (journal->j_head == journal->j_last)
579 journal->j_head = journal->j_first;
580 spin_unlock(&journal->j_state_lock);
581 return journal_bmap(journal, blocknr, retp);
582 }
583
584 /*
585 * Conversion of logical to physical block numbers for the journal
586 *
587 * On external journals the journal blocks are identity-mapped, so
588 * this is a no-op. If needed, we can use j_blk_offset - everything is
589 * ready.
590 */
591 int journal_bmap(journal_t *journal, unsigned long blocknr,
592 unsigned long *retp)
593 {
594 int err = 0;
595 unsigned long ret;
596
597 if (journal->j_inode) {
598 ret = bmap(journal->j_inode, blocknr);
599 if (ret)
600 *retp = ret;
601 else {
602 char b[BDEVNAME_SIZE];
603
604 printk(KERN_ALERT "%s: journal block not found "
605 "at offset %lu on %s\n",
606 __func__,
607 blocknr,
608 bdevname(journal->j_dev, b));
609 err = -EIO;
610 __journal_abort_soft(journal, err);
611 }
612 } else {
613 *retp = blocknr; /* +journal->j_blk_offset */
614 }
615 return err;
616 }
617
618 /*
619 * We play buffer_head aliasing tricks to write data/metadata blocks to
620 * the journal without copying their contents, but for journal
621 * descriptor blocks we do need to generate bona fide buffers.
622 *
623 * After the caller of journal_get_descriptor_buffer() has finished modifying
624 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
625 * But we don't bother doing that, so there will be coherency problems with
626 * mmaps of blockdevs which hold live JBD-controlled filesystems.
627 */
628 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
629 {
630 struct buffer_head *bh;
631 unsigned long blocknr;
632 int err;
633
634 err = journal_next_log_block(journal, &blocknr);
635
636 if (err)
637 return NULL;
638
639 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
640 if (!bh)
641 return NULL;
642 lock_buffer(bh);
643 memset(bh->b_data, 0, journal->j_blocksize);
644 set_buffer_uptodate(bh);
645 unlock_buffer(bh);
646 BUFFER_TRACE(bh, "return this buffer");
647 return journal_add_journal_head(bh);
648 }
649
650 /*
651 * Management for journal control blocks: functions to create and
652 * destroy journal_t structures, and to initialise and read existing
653 * journal blocks from disk. */
654
655 /* First: create and setup a journal_t object in memory. We initialise
656 * very few fields yet: that has to wait until we have created the
657 * journal structures from from scratch, or loaded them from disk. */
658
659 static journal_t * journal_init_common (void)
660 {
661 journal_t *journal;
662 int err;
663
664 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
665 if (!journal)
666 goto fail;
667
668 init_waitqueue_head(&journal->j_wait_transaction_locked);
669 init_waitqueue_head(&journal->j_wait_logspace);
670 init_waitqueue_head(&journal->j_wait_done_commit);
671 init_waitqueue_head(&journal->j_wait_checkpoint);
672 init_waitqueue_head(&journal->j_wait_commit);
673 init_waitqueue_head(&journal->j_wait_updates);
674 mutex_init(&journal->j_barrier);
675 mutex_init(&journal->j_checkpoint_mutex);
676 spin_lock_init(&journal->j_revoke_lock);
677 spin_lock_init(&journal->j_list_lock);
678 spin_lock_init(&journal->j_state_lock);
679
680 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
681
682 /* The journal is marked for error until we succeed with recovery! */
683 journal->j_flags = JFS_ABORT;
684
685 /* Set up a default-sized revoke table for the new mount. */
686 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
687 if (err) {
688 kfree(journal);
689 goto fail;
690 }
691 return journal;
692 fail:
693 return NULL;
694 }
695
696 /* journal_init_dev and journal_init_inode:
697 *
698 * Create a journal structure assigned some fixed set of disk blocks to
699 * the journal. We don't actually touch those disk blocks yet, but we
700 * need to set up all of the mapping information to tell the journaling
701 * system where the journal blocks are.
702 *
703 */
704
705 /**
706 * journal_t * journal_init_dev() - creates and initialises a journal structure
707 * @bdev: Block device on which to create the journal
708 * @fs_dev: Device which hold journalled filesystem for this journal.
709 * @start: Block nr Start of journal.
710 * @len: Length of the journal in blocks.
711 * @blocksize: blocksize of journalling device
712 *
713 * Returns: a newly created journal_t *
714 *
715 * journal_init_dev creates a journal which maps a fixed contiguous
716 * range of blocks on an arbitrary block device.
717 *
718 */
719 journal_t * journal_init_dev(struct block_device *bdev,
720 struct block_device *fs_dev,
721 int start, int len, int blocksize)
722 {
723 journal_t *journal = journal_init_common();
724 struct buffer_head *bh;
725 int n;
726
727 if (!journal)
728 return NULL;
729
730 /* journal descriptor can store up to n blocks -bzzz */
731 journal->j_blocksize = blocksize;
732 n = journal->j_blocksize / sizeof(journal_block_tag_t);
733 journal->j_wbufsize = n;
734 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
735 if (!journal->j_wbuf) {
736 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
737 __func__);
738 goto out_err;
739 }
740 journal->j_dev = bdev;
741 journal->j_fs_dev = fs_dev;
742 journal->j_blk_offset = start;
743 journal->j_maxlen = len;
744
745 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
746 if (!bh) {
747 printk(KERN_ERR
748 "%s: Cannot get buffer for journal superblock\n",
749 __func__);
750 goto out_err;
751 }
752 journal->j_sb_buffer = bh;
753 journal->j_superblock = (journal_superblock_t *)bh->b_data;
754
755 return journal;
756 out_err:
757 kfree(journal);
758 return NULL;
759 }
760
761 /**
762 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
763 * @inode: An inode to create the journal in
764 *
765 * journal_init_inode creates a journal which maps an on-disk inode as
766 * the journal. The inode must exist already, must support bmap() and
767 * must have all data blocks preallocated.
768 */
769 journal_t * journal_init_inode (struct inode *inode)
770 {
771 struct buffer_head *bh;
772 journal_t *journal = journal_init_common();
773 int err;
774 int n;
775 unsigned long blocknr;
776
777 if (!journal)
778 return NULL;
779
780 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
781 journal->j_inode = inode;
782 jbd_debug(1,
783 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
784 journal, inode->i_sb->s_id, inode->i_ino,
785 (long long) inode->i_size,
786 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
787
788 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
789 journal->j_blocksize = inode->i_sb->s_blocksize;
790
791 /* journal descriptor can store up to n blocks -bzzz */
792 n = journal->j_blocksize / sizeof(journal_block_tag_t);
793 journal->j_wbufsize = n;
794 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
795 if (!journal->j_wbuf) {
796 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
797 __func__);
798 goto out_err;
799 }
800
801 err = journal_bmap(journal, 0, &blocknr);
802 /* If that failed, give up */
803 if (err) {
804 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
805 __func__);
806 goto out_err;
807 }
808
809 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
810 if (!bh) {
811 printk(KERN_ERR
812 "%s: Cannot get buffer for journal superblock\n",
813 __func__);
814 goto out_err;
815 }
816 journal->j_sb_buffer = bh;
817 journal->j_superblock = (journal_superblock_t *)bh->b_data;
818
819 return journal;
820 out_err:
821 kfree(journal);
822 return NULL;
823 }
824
825 /*
826 * If the journal init or create aborts, we need to mark the journal
827 * superblock as being NULL to prevent the journal destroy from writing
828 * back a bogus superblock.
829 */
830 static void journal_fail_superblock (journal_t *journal)
831 {
832 struct buffer_head *bh = journal->j_sb_buffer;
833 brelse(bh);
834 journal->j_sb_buffer = NULL;
835 }
836
837 /*
838 * Given a journal_t structure, initialise the various fields for
839 * startup of a new journaling session. We use this both when creating
840 * a journal, and after recovering an old journal to reset it for
841 * subsequent use.
842 */
843
844 static int journal_reset(journal_t *journal)
845 {
846 journal_superblock_t *sb = journal->j_superblock;
847 unsigned long first, last;
848
849 first = be32_to_cpu(sb->s_first);
850 last = be32_to_cpu(sb->s_maxlen);
851 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
852 printk(KERN_ERR "JBD: Journal too short (blocks %lu-%lu).\n",
853 first, last);
854 journal_fail_superblock(journal);
855 return -EINVAL;
856 }
857
858 journal->j_first = first;
859 journal->j_last = last;
860
861 journal->j_head = first;
862 journal->j_tail = first;
863 journal->j_free = last - first;
864
865 journal->j_tail_sequence = journal->j_transaction_sequence;
866 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
867 journal->j_commit_request = journal->j_commit_sequence;
868
869 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
870
871 /* Add the dynamic fields and write it to disk. */
872 journal_update_superblock(journal, 1);
873 return journal_start_thread(journal);
874 }
875
876 /**
877 * int journal_create() - Initialise the new journal file
878 * @journal: Journal to create. This structure must have been initialised
879 *
880 * Given a journal_t structure which tells us which disk blocks we can
881 * use, create a new journal superblock and initialise all of the
882 * journal fields from scratch.
883 **/
884 int journal_create(journal_t *journal)
885 {
886 unsigned long blocknr;
887 struct buffer_head *bh;
888 journal_superblock_t *sb;
889 int i, err;
890
891 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
892 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
893 journal->j_maxlen);
894 journal_fail_superblock(journal);
895 return -EINVAL;
896 }
897
898 if (journal->j_inode == NULL) {
899 /*
900 * We don't know what block to start at!
901 */
902 printk(KERN_EMERG
903 "%s: creation of journal on external device!\n",
904 __func__);
905 BUG();
906 }
907
908 /* Zero out the entire journal on disk. We cannot afford to
909 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
910 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
911 for (i = 0; i < journal->j_maxlen; i++) {
912 err = journal_bmap(journal, i, &blocknr);
913 if (err)
914 return err;
915 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
916 lock_buffer(bh);
917 memset (bh->b_data, 0, journal->j_blocksize);
918 BUFFER_TRACE(bh, "marking dirty");
919 mark_buffer_dirty(bh);
920 BUFFER_TRACE(bh, "marking uptodate");
921 set_buffer_uptodate(bh);
922 unlock_buffer(bh);
923 __brelse(bh);
924 }
925
926 sync_blockdev(journal->j_dev);
927 jbd_debug(1, "JBD: journal cleared.\n");
928
929 /* OK, fill in the initial static fields in the new superblock */
930 sb = journal->j_superblock;
931
932 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
933 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
934
935 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
936 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
937 sb->s_first = cpu_to_be32(1);
938
939 journal->j_transaction_sequence = 1;
940
941 journal->j_flags &= ~JFS_ABORT;
942 journal->j_format_version = 2;
943
944 return journal_reset(journal);
945 }
946
947 /**
948 * void journal_update_superblock() - Update journal sb on disk.
949 * @journal: The journal to update.
950 * @wait: Set to '0' if you don't want to wait for IO completion.
951 *
952 * Update a journal's dynamic superblock fields and write it to disk,
953 * optionally waiting for the IO to complete.
954 */
955 void journal_update_superblock(journal_t *journal, int wait)
956 {
957 journal_superblock_t *sb = journal->j_superblock;
958 struct buffer_head *bh = journal->j_sb_buffer;
959
960 /*
961 * As a special case, if the on-disk copy is already marked as needing
962 * no recovery (s_start == 0) and there are no outstanding transactions
963 * in the filesystem, then we can safely defer the superblock update
964 * until the next commit by setting JFS_FLUSHED. This avoids
965 * attempting a write to a potential-readonly device.
966 */
967 if (sb->s_start == 0 && journal->j_tail_sequence ==
968 journal->j_transaction_sequence) {
969 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
970 "(start %ld, seq %d, errno %d)\n",
971 journal->j_tail, journal->j_tail_sequence,
972 journal->j_errno);
973 goto out;
974 }
975
976 spin_lock(&journal->j_state_lock);
977 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
978 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
979
980 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
981 sb->s_start = cpu_to_be32(journal->j_tail);
982 sb->s_errno = cpu_to_be32(journal->j_errno);
983 spin_unlock(&journal->j_state_lock);
984
985 BUFFER_TRACE(bh, "marking dirty");
986 mark_buffer_dirty(bh);
987 if (wait)
988 sync_dirty_buffer(bh);
989 else
990 ll_rw_block(SWRITE, 1, &bh);
991
992 out:
993 /* If we have just flushed the log (by marking s_start==0), then
994 * any future commit will have to be careful to update the
995 * superblock again to re-record the true start of the log. */
996
997 spin_lock(&journal->j_state_lock);
998 if (sb->s_start)
999 journal->j_flags &= ~JFS_FLUSHED;
1000 else
1001 journal->j_flags |= JFS_FLUSHED;
1002 spin_unlock(&journal->j_state_lock);
1003 }
1004
1005 /*
1006 * Read the superblock for a given journal, performing initial
1007 * validation of the format.
1008 */
1009
1010 static int journal_get_superblock(journal_t *journal)
1011 {
1012 struct buffer_head *bh;
1013 journal_superblock_t *sb;
1014 int err = -EIO;
1015
1016 bh = journal->j_sb_buffer;
1017
1018 J_ASSERT(bh != NULL);
1019 if (!buffer_uptodate(bh)) {
1020 ll_rw_block(READ, 1, &bh);
1021 wait_on_buffer(bh);
1022 if (!buffer_uptodate(bh)) {
1023 printk (KERN_ERR
1024 "JBD: IO error reading journal superblock\n");
1025 goto out;
1026 }
1027 }
1028
1029 sb = journal->j_superblock;
1030
1031 err = -EINVAL;
1032
1033 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1034 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1035 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1036 goto out;
1037 }
1038
1039 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1040 case JFS_SUPERBLOCK_V1:
1041 journal->j_format_version = 1;
1042 break;
1043 case JFS_SUPERBLOCK_V2:
1044 journal->j_format_version = 2;
1045 break;
1046 default:
1047 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1048 goto out;
1049 }
1050
1051 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1052 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1053 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1054 printk (KERN_WARNING "JBD: journal file too short\n");
1055 goto out;
1056 }
1057
1058 return 0;
1059
1060 out:
1061 journal_fail_superblock(journal);
1062 return err;
1063 }
1064
1065 /*
1066 * Load the on-disk journal superblock and read the key fields into the
1067 * journal_t.
1068 */
1069
1070 static int load_superblock(journal_t *journal)
1071 {
1072 int err;
1073 journal_superblock_t *sb;
1074
1075 err = journal_get_superblock(journal);
1076 if (err)
1077 return err;
1078
1079 sb = journal->j_superblock;
1080
1081 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1082 journal->j_tail = be32_to_cpu(sb->s_start);
1083 journal->j_first = be32_to_cpu(sb->s_first);
1084 journal->j_last = be32_to_cpu(sb->s_maxlen);
1085 journal->j_errno = be32_to_cpu(sb->s_errno);
1086
1087 return 0;
1088 }
1089
1090
1091 /**
1092 * int journal_load() - Read journal from disk.
1093 * @journal: Journal to act on.
1094 *
1095 * Given a journal_t structure which tells us which disk blocks contain
1096 * a journal, read the journal from disk to initialise the in-memory
1097 * structures.
1098 */
1099 int journal_load(journal_t *journal)
1100 {
1101 int err;
1102 journal_superblock_t *sb;
1103
1104 err = load_superblock(journal);
1105 if (err)
1106 return err;
1107
1108 sb = journal->j_superblock;
1109 /* If this is a V2 superblock, then we have to check the
1110 * features flags on it. */
1111
1112 if (journal->j_format_version >= 2) {
1113 if ((sb->s_feature_ro_compat &
1114 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1115 (sb->s_feature_incompat &
1116 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1117 printk (KERN_WARNING
1118 "JBD: Unrecognised features on journal\n");
1119 return -EINVAL;
1120 }
1121 }
1122
1123 /* Let the recovery code check whether it needs to recover any
1124 * data from the journal. */
1125 if (journal_recover(journal))
1126 goto recovery_error;
1127
1128 /* OK, we've finished with the dynamic journal bits:
1129 * reinitialise the dynamic contents of the superblock in memory
1130 * and reset them on disk. */
1131 if (journal_reset(journal))
1132 goto recovery_error;
1133
1134 journal->j_flags &= ~JFS_ABORT;
1135 journal->j_flags |= JFS_LOADED;
1136 return 0;
1137
1138 recovery_error:
1139 printk (KERN_WARNING "JBD: recovery failed\n");
1140 return -EIO;
1141 }
1142
1143 /**
1144 * void journal_destroy() - Release a journal_t structure.
1145 * @journal: Journal to act on.
1146 *
1147 * Release a journal_t structure once it is no longer in use by the
1148 * journaled object.
1149 * Return <0 if we couldn't clean up the journal.
1150 */
1151 int journal_destroy(journal_t *journal)
1152 {
1153 int err = 0;
1154
1155 /* Wait for the commit thread to wake up and die. */
1156 journal_kill_thread(journal);
1157
1158 /* Force a final log commit */
1159 if (journal->j_running_transaction)
1160 journal_commit_transaction(journal);
1161
1162 /* Force any old transactions to disk */
1163
1164 /* Totally anal locking here... */
1165 spin_lock(&journal->j_list_lock);
1166 while (journal->j_checkpoint_transactions != NULL) {
1167 spin_unlock(&journal->j_list_lock);
1168 log_do_checkpoint(journal);
1169 spin_lock(&journal->j_list_lock);
1170 }
1171
1172 J_ASSERT(journal->j_running_transaction == NULL);
1173 J_ASSERT(journal->j_committing_transaction == NULL);
1174 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1175 spin_unlock(&journal->j_list_lock);
1176
1177 if (journal->j_sb_buffer) {
1178 if (!is_journal_aborted(journal)) {
1179 /* We can now mark the journal as empty. */
1180 journal->j_tail = 0;
1181 journal->j_tail_sequence =
1182 ++journal->j_transaction_sequence;
1183 journal_update_superblock(journal, 1);
1184 } else {
1185 err = -EIO;
1186 }
1187 brelse(journal->j_sb_buffer);
1188 }
1189
1190 if (journal->j_inode)
1191 iput(journal->j_inode);
1192 if (journal->j_revoke)
1193 journal_destroy_revoke(journal);
1194 kfree(journal->j_wbuf);
1195 kfree(journal);
1196
1197 return err;
1198 }
1199
1200
1201 /**
1202 *int journal_check_used_features () - Check if features specified are used.
1203 * @journal: Journal to check.
1204 * @compat: bitmask of compatible features
1205 * @ro: bitmask of features that force read-only mount
1206 * @incompat: bitmask of incompatible features
1207 *
1208 * Check whether the journal uses all of a given set of
1209 * features. Return true (non-zero) if it does.
1210 **/
1211
1212 int journal_check_used_features (journal_t *journal, unsigned long compat,
1213 unsigned long ro, unsigned long incompat)
1214 {
1215 journal_superblock_t *sb;
1216
1217 if (!compat && !ro && !incompat)
1218 return 1;
1219 if (journal->j_format_version == 1)
1220 return 0;
1221
1222 sb = journal->j_superblock;
1223
1224 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1225 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1226 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1227 return 1;
1228
1229 return 0;
1230 }
1231
1232 /**
1233 * int journal_check_available_features() - Check feature set in journalling layer
1234 * @journal: Journal to check.
1235 * @compat: bitmask of compatible features
1236 * @ro: bitmask of features that force read-only mount
1237 * @incompat: bitmask of incompatible features
1238 *
1239 * Check whether the journaling code supports the use of
1240 * all of a given set of features on this journal. Return true
1241 * (non-zero) if it can. */
1242
1243 int journal_check_available_features (journal_t *journal, unsigned long compat,
1244 unsigned long ro, unsigned long incompat)
1245 {
1246 journal_superblock_t *sb;
1247
1248 if (!compat && !ro && !incompat)
1249 return 1;
1250
1251 sb = journal->j_superblock;
1252
1253 /* We can support any known requested features iff the
1254 * superblock is in version 2. Otherwise we fail to support any
1255 * extended sb features. */
1256
1257 if (journal->j_format_version != 2)
1258 return 0;
1259
1260 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1261 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1262 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1263 return 1;
1264
1265 return 0;
1266 }
1267
1268 /**
1269 * int journal_set_features () - Mark a given journal feature in the superblock
1270 * @journal: Journal to act on.
1271 * @compat: bitmask of compatible features
1272 * @ro: bitmask of features that force read-only mount
1273 * @incompat: bitmask of incompatible features
1274 *
1275 * Mark a given journal feature as present on the
1276 * superblock. Returns true if the requested features could be set.
1277 *
1278 */
1279
1280 int journal_set_features (journal_t *journal, unsigned long compat,
1281 unsigned long ro, unsigned long incompat)
1282 {
1283 journal_superblock_t *sb;
1284
1285 if (journal_check_used_features(journal, compat, ro, incompat))
1286 return 1;
1287
1288 if (!journal_check_available_features(journal, compat, ro, incompat))
1289 return 0;
1290
1291 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1292 compat, ro, incompat);
1293
1294 sb = journal->j_superblock;
1295
1296 sb->s_feature_compat |= cpu_to_be32(compat);
1297 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1298 sb->s_feature_incompat |= cpu_to_be32(incompat);
1299
1300 return 1;
1301 }
1302
1303
1304 /**
1305 * int journal_update_format () - Update on-disk journal structure.
1306 * @journal: Journal to act on.
1307 *
1308 * Given an initialised but unloaded journal struct, poke about in the
1309 * on-disk structure to update it to the most recent supported version.
1310 */
1311 int journal_update_format (journal_t *journal)
1312 {
1313 journal_superblock_t *sb;
1314 int err;
1315
1316 err = journal_get_superblock(journal);
1317 if (err)
1318 return err;
1319
1320 sb = journal->j_superblock;
1321
1322 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1323 case JFS_SUPERBLOCK_V2:
1324 return 0;
1325 case JFS_SUPERBLOCK_V1:
1326 return journal_convert_superblock_v1(journal, sb);
1327 default:
1328 break;
1329 }
1330 return -EINVAL;
1331 }
1332
1333 static int journal_convert_superblock_v1(journal_t *journal,
1334 journal_superblock_t *sb)
1335 {
1336 int offset, blocksize;
1337 struct buffer_head *bh;
1338
1339 printk(KERN_WARNING
1340 "JBD: Converting superblock from version 1 to 2.\n");
1341
1342 /* Pre-initialise new fields to zero */
1343 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1344 blocksize = be32_to_cpu(sb->s_blocksize);
1345 memset(&sb->s_feature_compat, 0, blocksize-offset);
1346
1347 sb->s_nr_users = cpu_to_be32(1);
1348 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1349 journal->j_format_version = 2;
1350
1351 bh = journal->j_sb_buffer;
1352 BUFFER_TRACE(bh, "marking dirty");
1353 mark_buffer_dirty(bh);
1354 sync_dirty_buffer(bh);
1355 return 0;
1356 }
1357
1358
1359 /**
1360 * int journal_flush () - Flush journal
1361 * @journal: Journal to act on.
1362 *
1363 * Flush all data for a given journal to disk and empty the journal.
1364 * Filesystems can use this when remounting readonly to ensure that
1365 * recovery does not need to happen on remount.
1366 */
1367
1368 int journal_flush(journal_t *journal)
1369 {
1370 int err = 0;
1371 transaction_t *transaction = NULL;
1372 unsigned long old_tail;
1373
1374 spin_lock(&journal->j_state_lock);
1375
1376 /* Force everything buffered to the log... */
1377 if (journal->j_running_transaction) {
1378 transaction = journal->j_running_transaction;
1379 __log_start_commit(journal, transaction->t_tid);
1380 } else if (journal->j_committing_transaction)
1381 transaction = journal->j_committing_transaction;
1382
1383 /* Wait for the log commit to complete... */
1384 if (transaction) {
1385 tid_t tid = transaction->t_tid;
1386
1387 spin_unlock(&journal->j_state_lock);
1388 log_wait_commit(journal, tid);
1389 } else {
1390 spin_unlock(&journal->j_state_lock);
1391 }
1392
1393 /* ...and flush everything in the log out to disk. */
1394 spin_lock(&journal->j_list_lock);
1395 while (!err && journal->j_checkpoint_transactions != NULL) {
1396 spin_unlock(&journal->j_list_lock);
1397 mutex_lock(&journal->j_checkpoint_mutex);
1398 err = log_do_checkpoint(journal);
1399 mutex_unlock(&journal->j_checkpoint_mutex);
1400 spin_lock(&journal->j_list_lock);
1401 }
1402 spin_unlock(&journal->j_list_lock);
1403
1404 if (is_journal_aborted(journal))
1405 return -EIO;
1406
1407 cleanup_journal_tail(journal);
1408
1409 /* Finally, mark the journal as really needing no recovery.
1410 * This sets s_start==0 in the underlying superblock, which is
1411 * the magic code for a fully-recovered superblock. Any future
1412 * commits of data to the journal will restore the current
1413 * s_start value. */
1414 spin_lock(&journal->j_state_lock);
1415 old_tail = journal->j_tail;
1416 journal->j_tail = 0;
1417 spin_unlock(&journal->j_state_lock);
1418 journal_update_superblock(journal, 1);
1419 spin_lock(&journal->j_state_lock);
1420 journal->j_tail = old_tail;
1421
1422 J_ASSERT(!journal->j_running_transaction);
1423 J_ASSERT(!journal->j_committing_transaction);
1424 J_ASSERT(!journal->j_checkpoint_transactions);
1425 J_ASSERT(journal->j_head == journal->j_tail);
1426 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1427 spin_unlock(&journal->j_state_lock);
1428 return 0;
1429 }
1430
1431 /**
1432 * int journal_wipe() - Wipe journal contents
1433 * @journal: Journal to act on.
1434 * @write: flag (see below)
1435 *
1436 * Wipe out all of the contents of a journal, safely. This will produce
1437 * a warning if the journal contains any valid recovery information.
1438 * Must be called between journal_init_*() and journal_load().
1439 *
1440 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1441 * we merely suppress recovery.
1442 */
1443
1444 int journal_wipe(journal_t *journal, int write)
1445 {
1446 journal_superblock_t *sb;
1447 int err = 0;
1448
1449 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1450
1451 err = load_superblock(journal);
1452 if (err)
1453 return err;
1454
1455 sb = journal->j_superblock;
1456
1457 if (!journal->j_tail)
1458 goto no_recovery;
1459
1460 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1461 write ? "Clearing" : "Ignoring");
1462
1463 err = journal_skip_recovery(journal);
1464 if (write)
1465 journal_update_superblock(journal, 1);
1466
1467 no_recovery:
1468 return err;
1469 }
1470
1471 /*
1472 * journal_dev_name: format a character string to describe on what
1473 * device this journal is present.
1474 */
1475
1476 static const char *journal_dev_name(journal_t *journal, char *buffer)
1477 {
1478 struct block_device *bdev;
1479
1480 if (journal->j_inode)
1481 bdev = journal->j_inode->i_sb->s_bdev;
1482 else
1483 bdev = journal->j_dev;
1484
1485 return bdevname(bdev, buffer);
1486 }
1487
1488 /*
1489 * Journal abort has very specific semantics, which we describe
1490 * for journal abort.
1491 *
1492 * Two internal function, which provide abort to te jbd layer
1493 * itself are here.
1494 */
1495
1496 /*
1497 * Quick version for internal journal use (doesn't lock the journal).
1498 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1499 * and don't attempt to make any other journal updates.
1500 */
1501 static void __journal_abort_hard(journal_t *journal)
1502 {
1503 transaction_t *transaction;
1504 char b[BDEVNAME_SIZE];
1505
1506 if (journal->j_flags & JFS_ABORT)
1507 return;
1508
1509 printk(KERN_ERR "Aborting journal on device %s.\n",
1510 journal_dev_name(journal, b));
1511
1512 spin_lock(&journal->j_state_lock);
1513 journal->j_flags |= JFS_ABORT;
1514 transaction = journal->j_running_transaction;
1515 if (transaction)
1516 __log_start_commit(journal, transaction->t_tid);
1517 spin_unlock(&journal->j_state_lock);
1518 }
1519
1520 /* Soft abort: record the abort error status in the journal superblock,
1521 * but don't do any other IO. */
1522 static void __journal_abort_soft (journal_t *journal, int errno)
1523 {
1524 if (journal->j_flags & JFS_ABORT)
1525 return;
1526
1527 if (!journal->j_errno)
1528 journal->j_errno = errno;
1529
1530 __journal_abort_hard(journal);
1531
1532 if (errno)
1533 journal_update_superblock(journal, 1);
1534 }
1535
1536 /**
1537 * void journal_abort () - Shutdown the journal immediately.
1538 * @journal: the journal to shutdown.
1539 * @errno: an error number to record in the journal indicating
1540 * the reason for the shutdown.
1541 *
1542 * Perform a complete, immediate shutdown of the ENTIRE
1543 * journal (not of a single transaction). This operation cannot be
1544 * undone without closing and reopening the journal.
1545 *
1546 * The journal_abort function is intended to support higher level error
1547 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1548 * mode.
1549 *
1550 * Journal abort has very specific semantics. Any existing dirty,
1551 * unjournaled buffers in the main filesystem will still be written to
1552 * disk by bdflush, but the journaling mechanism will be suspended
1553 * immediately and no further transaction commits will be honoured.
1554 *
1555 * Any dirty, journaled buffers will be written back to disk without
1556 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1557 * filesystem, but we _do_ attempt to leave as much data as possible
1558 * behind for fsck to use for cleanup.
1559 *
1560 * Any attempt to get a new transaction handle on a journal which is in
1561 * ABORT state will just result in an -EROFS error return. A
1562 * journal_stop on an existing handle will return -EIO if we have
1563 * entered abort state during the update.
1564 *
1565 * Recursive transactions are not disturbed by journal abort until the
1566 * final journal_stop, which will receive the -EIO error.
1567 *
1568 * Finally, the journal_abort call allows the caller to supply an errno
1569 * which will be recorded (if possible) in the journal superblock. This
1570 * allows a client to record failure conditions in the middle of a
1571 * transaction without having to complete the transaction to record the
1572 * failure to disk. ext3_error, for example, now uses this
1573 * functionality.
1574 *
1575 * Errors which originate from within the journaling layer will NOT
1576 * supply an errno; a null errno implies that absolutely no further
1577 * writes are done to the journal (unless there are any already in
1578 * progress).
1579 *
1580 */
1581
1582 void journal_abort(journal_t *journal, int errno)
1583 {
1584 __journal_abort_soft(journal, errno);
1585 }
1586
1587 /**
1588 * int journal_errno () - returns the journal's error state.
1589 * @journal: journal to examine.
1590 *
1591 * This is the errno numbet set with journal_abort(), the last
1592 * time the journal was mounted - if the journal was stopped
1593 * without calling abort this will be 0.
1594 *
1595 * If the journal has been aborted on this mount time -EROFS will
1596 * be returned.
1597 */
1598 int journal_errno(journal_t *journal)
1599 {
1600 int err;
1601
1602 spin_lock(&journal->j_state_lock);
1603 if (journal->j_flags & JFS_ABORT)
1604 err = -EROFS;
1605 else
1606 err = journal->j_errno;
1607 spin_unlock(&journal->j_state_lock);
1608 return err;
1609 }
1610
1611 /**
1612 * int journal_clear_err () - clears the journal's error state
1613 * @journal: journal to act on.
1614 *
1615 * An error must be cleared or Acked to take a FS out of readonly
1616 * mode.
1617 */
1618 int journal_clear_err(journal_t *journal)
1619 {
1620 int err = 0;
1621
1622 spin_lock(&journal->j_state_lock);
1623 if (journal->j_flags & JFS_ABORT)
1624 err = -EROFS;
1625 else
1626 journal->j_errno = 0;
1627 spin_unlock(&journal->j_state_lock);
1628 return err;
1629 }
1630
1631 /**
1632 * void journal_ack_err() - Ack journal err.
1633 * @journal: journal to act on.
1634 *
1635 * An error must be cleared or Acked to take a FS out of readonly
1636 * mode.
1637 */
1638 void journal_ack_err(journal_t *journal)
1639 {
1640 spin_lock(&journal->j_state_lock);
1641 if (journal->j_errno)
1642 journal->j_flags |= JFS_ACK_ERR;
1643 spin_unlock(&journal->j_state_lock);
1644 }
1645
1646 int journal_blocks_per_page(struct inode *inode)
1647 {
1648 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1649 }
1650
1651 /*
1652 * Journal_head storage management
1653 */
1654 static struct kmem_cache *journal_head_cache;
1655 #ifdef CONFIG_JBD_DEBUG
1656 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1657 #endif
1658
1659 static int journal_init_journal_head_cache(void)
1660 {
1661 int retval;
1662
1663 J_ASSERT(journal_head_cache == NULL);
1664 journal_head_cache = kmem_cache_create("journal_head",
1665 sizeof(struct journal_head),
1666 0, /* offset */
1667 SLAB_TEMPORARY, /* flags */
1668 NULL); /* ctor */
1669 retval = 0;
1670 if (!journal_head_cache) {
1671 retval = -ENOMEM;
1672 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1673 }
1674 return retval;
1675 }
1676
1677 static void journal_destroy_journal_head_cache(void)
1678 {
1679 if (journal_head_cache) {
1680 kmem_cache_destroy(journal_head_cache);
1681 journal_head_cache = NULL;
1682 }
1683 }
1684
1685 /*
1686 * journal_head splicing and dicing
1687 */
1688 static struct journal_head *journal_alloc_journal_head(void)
1689 {
1690 struct journal_head *ret;
1691 static unsigned long last_warning;
1692
1693 #ifdef CONFIG_JBD_DEBUG
1694 atomic_inc(&nr_journal_heads);
1695 #endif
1696 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1697 if (ret == NULL) {
1698 jbd_debug(1, "out of memory for journal_head\n");
1699 if (time_after(jiffies, last_warning + 5*HZ)) {
1700 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1701 __func__);
1702 last_warning = jiffies;
1703 }
1704 while (ret == NULL) {
1705 yield();
1706 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1707 }
1708 }
1709 return ret;
1710 }
1711
1712 static void journal_free_journal_head(struct journal_head *jh)
1713 {
1714 #ifdef CONFIG_JBD_DEBUG
1715 atomic_dec(&nr_journal_heads);
1716 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1717 #endif
1718 kmem_cache_free(journal_head_cache, jh);
1719 }
1720
1721 /*
1722 * A journal_head is attached to a buffer_head whenever JBD has an
1723 * interest in the buffer.
1724 *
1725 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1726 * is set. This bit is tested in core kernel code where we need to take
1727 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1728 * there.
1729 *
1730 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1731 *
1732 * When a buffer has its BH_JBD bit set it is immune from being released by
1733 * core kernel code, mainly via ->b_count.
1734 *
1735 * A journal_head may be detached from its buffer_head when the journal_head's
1736 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1737 * Various places in JBD call journal_remove_journal_head() to indicate that the
1738 * journal_head can be dropped if needed.
1739 *
1740 * Various places in the kernel want to attach a journal_head to a buffer_head
1741 * _before_ attaching the journal_head to a transaction. To protect the
1742 * journal_head in this situation, journal_add_journal_head elevates the
1743 * journal_head's b_jcount refcount by one. The caller must call
1744 * journal_put_journal_head() to undo this.
1745 *
1746 * So the typical usage would be:
1747 *
1748 * (Attach a journal_head if needed. Increments b_jcount)
1749 * struct journal_head *jh = journal_add_journal_head(bh);
1750 * ...
1751 * jh->b_transaction = xxx;
1752 * journal_put_journal_head(jh);
1753 *
1754 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1755 * because it has a non-zero b_transaction.
1756 */
1757
1758 /*
1759 * Give a buffer_head a journal_head.
1760 *
1761 * Doesn't need the journal lock.
1762 * May sleep.
1763 */
1764 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1765 {
1766 struct journal_head *jh;
1767 struct journal_head *new_jh = NULL;
1768
1769 repeat:
1770 if (!buffer_jbd(bh)) {
1771 new_jh = journal_alloc_journal_head();
1772 memset(new_jh, 0, sizeof(*new_jh));
1773 }
1774
1775 jbd_lock_bh_journal_head(bh);
1776 if (buffer_jbd(bh)) {
1777 jh = bh2jh(bh);
1778 } else {
1779 J_ASSERT_BH(bh,
1780 (atomic_read(&bh->b_count) > 0) ||
1781 (bh->b_page && bh->b_page->mapping));
1782
1783 if (!new_jh) {
1784 jbd_unlock_bh_journal_head(bh);
1785 goto repeat;
1786 }
1787
1788 jh = new_jh;
1789 new_jh = NULL; /* We consumed it */
1790 set_buffer_jbd(bh);
1791 bh->b_private = jh;
1792 jh->b_bh = bh;
1793 get_bh(bh);
1794 BUFFER_TRACE(bh, "added journal_head");
1795 }
1796 jh->b_jcount++;
1797 jbd_unlock_bh_journal_head(bh);
1798 if (new_jh)
1799 journal_free_journal_head(new_jh);
1800 return bh->b_private;
1801 }
1802
1803 /*
1804 * Grab a ref against this buffer_head's journal_head. If it ended up not
1805 * having a journal_head, return NULL
1806 */
1807 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1808 {
1809 struct journal_head *jh = NULL;
1810
1811 jbd_lock_bh_journal_head(bh);
1812 if (buffer_jbd(bh)) {
1813 jh = bh2jh(bh);
1814 jh->b_jcount++;
1815 }
1816 jbd_unlock_bh_journal_head(bh);
1817 return jh;
1818 }
1819
1820 static void __journal_remove_journal_head(struct buffer_head *bh)
1821 {
1822 struct journal_head *jh = bh2jh(bh);
1823
1824 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1825
1826 get_bh(bh);
1827 if (jh->b_jcount == 0) {
1828 if (jh->b_transaction == NULL &&
1829 jh->b_next_transaction == NULL &&
1830 jh->b_cp_transaction == NULL) {
1831 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1832 J_ASSERT_BH(bh, buffer_jbd(bh));
1833 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1834 BUFFER_TRACE(bh, "remove journal_head");
1835 if (jh->b_frozen_data) {
1836 printk(KERN_WARNING "%s: freeing "
1837 "b_frozen_data\n",
1838 __func__);
1839 jbd_free(jh->b_frozen_data, bh->b_size);
1840 }
1841 if (jh->b_committed_data) {
1842 printk(KERN_WARNING "%s: freeing "
1843 "b_committed_data\n",
1844 __func__);
1845 jbd_free(jh->b_committed_data, bh->b_size);
1846 }
1847 bh->b_private = NULL;
1848 jh->b_bh = NULL; /* debug, really */
1849 clear_buffer_jbd(bh);
1850 __brelse(bh);
1851 journal_free_journal_head(jh);
1852 } else {
1853 BUFFER_TRACE(bh, "journal_head was locked");
1854 }
1855 }
1856 }
1857
1858 /*
1859 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1860 * and has a zero b_jcount then remove and release its journal_head. If we did
1861 * see that the buffer is not used by any transaction we also "logically"
1862 * decrement ->b_count.
1863 *
1864 * We in fact take an additional increment on ->b_count as a convenience,
1865 * because the caller usually wants to do additional things with the bh
1866 * after calling here.
1867 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1868 * time. Once the caller has run __brelse(), the buffer is eligible for
1869 * reaping by try_to_free_buffers().
1870 */
1871 void journal_remove_journal_head(struct buffer_head *bh)
1872 {
1873 jbd_lock_bh_journal_head(bh);
1874 __journal_remove_journal_head(bh);
1875 jbd_unlock_bh_journal_head(bh);
1876 }
1877
1878 /*
1879 * Drop a reference on the passed journal_head. If it fell to zero then try to
1880 * release the journal_head from the buffer_head.
1881 */
1882 void journal_put_journal_head(struct journal_head *jh)
1883 {
1884 struct buffer_head *bh = jh2bh(jh);
1885
1886 jbd_lock_bh_journal_head(bh);
1887 J_ASSERT_JH(jh, jh->b_jcount > 0);
1888 --jh->b_jcount;
1889 if (!jh->b_jcount && !jh->b_transaction) {
1890 __journal_remove_journal_head(bh);
1891 __brelse(bh);
1892 }
1893 jbd_unlock_bh_journal_head(bh);
1894 }
1895
1896 /*
1897 * debugfs tunables
1898 */
1899 #ifdef CONFIG_JBD_DEBUG
1900
1901 u8 journal_enable_debug __read_mostly;
1902 EXPORT_SYMBOL(journal_enable_debug);
1903
1904 static struct dentry *jbd_debugfs_dir;
1905 static struct dentry *jbd_debug;
1906
1907 static void __init jbd_create_debugfs_entry(void)
1908 {
1909 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1910 if (jbd_debugfs_dir)
1911 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO,
1912 jbd_debugfs_dir,
1913 &journal_enable_debug);
1914 }
1915
1916 static void __exit jbd_remove_debugfs_entry(void)
1917 {
1918 debugfs_remove(jbd_debug);
1919 debugfs_remove(jbd_debugfs_dir);
1920 }
1921
1922 #else
1923
1924 static inline void jbd_create_debugfs_entry(void)
1925 {
1926 }
1927
1928 static inline void jbd_remove_debugfs_entry(void)
1929 {
1930 }
1931
1932 #endif
1933
1934 struct kmem_cache *jbd_handle_cache;
1935
1936 static int __init journal_init_handle_cache(void)
1937 {
1938 jbd_handle_cache = kmem_cache_create("journal_handle",
1939 sizeof(handle_t),
1940 0, /* offset */
1941 SLAB_TEMPORARY, /* flags */
1942 NULL); /* ctor */
1943 if (jbd_handle_cache == NULL) {
1944 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1945 return -ENOMEM;
1946 }
1947 return 0;
1948 }
1949
1950 static void journal_destroy_handle_cache(void)
1951 {
1952 if (jbd_handle_cache)
1953 kmem_cache_destroy(jbd_handle_cache);
1954 }
1955
1956 /*
1957 * Module startup and shutdown
1958 */
1959
1960 static int __init journal_init_caches(void)
1961 {
1962 int ret;
1963
1964 ret = journal_init_revoke_caches();
1965 if (ret == 0)
1966 ret = journal_init_journal_head_cache();
1967 if (ret == 0)
1968 ret = journal_init_handle_cache();
1969 return ret;
1970 }
1971
1972 static void journal_destroy_caches(void)
1973 {
1974 journal_destroy_revoke_caches();
1975 journal_destroy_journal_head_cache();
1976 journal_destroy_handle_cache();
1977 }
1978
1979 static int __init journal_init(void)
1980 {
1981 int ret;
1982
1983 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
1984
1985 ret = journal_init_caches();
1986 if (ret != 0)
1987 journal_destroy_caches();
1988 jbd_create_debugfs_entry();
1989 return ret;
1990 }
1991
1992 static void __exit journal_exit(void)
1993 {
1994 #ifdef CONFIG_JBD_DEBUG
1995 int n = atomic_read(&nr_journal_heads);
1996 if (n)
1997 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1998 #endif
1999 jbd_remove_debugfs_entry();
2000 journal_destroy_caches();
2001 }
2002
2003 MODULE_LICENSE("GPL");
2004 module_init(journal_init);
2005 module_exit(journal_exit);
2006
This page took 0.070251 seconds and 4 git commands to generate.