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