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