xref: /openbmc/linux/fs/jbd2/journal.c (revision 6f6a6fda294506dfe0e3e0a253bb2d2923f28f0a)
1470decc6SDave Kleikamp /*
2f7f4bccbSMingming Cao  * linux/fs/jbd2/journal.c
3470decc6SDave Kleikamp  *
4470decc6SDave Kleikamp  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5470decc6SDave Kleikamp  *
6470decc6SDave Kleikamp  * Copyright 1998 Red Hat corp --- All Rights Reserved
7470decc6SDave Kleikamp  *
8470decc6SDave Kleikamp  * This file is part of the Linux kernel and is made available under
9470decc6SDave Kleikamp  * the terms of the GNU General Public License, version 2, or at your
10470decc6SDave Kleikamp  * option, any later version, incorporated herein by reference.
11470decc6SDave Kleikamp  *
12470decc6SDave Kleikamp  * Generic filesystem journal-writing code; part of the ext2fs
13470decc6SDave Kleikamp  * journaling system.
14470decc6SDave Kleikamp  *
15470decc6SDave Kleikamp  * This file manages journals: areas of disk reserved for logging
16470decc6SDave Kleikamp  * transactional updates.  This includes the kernel journaling thread
17470decc6SDave Kleikamp  * which is responsible for scheduling updates to the log.
18470decc6SDave Kleikamp  *
19470decc6SDave Kleikamp  * We do not actually manage the physical storage of the journal in this
20470decc6SDave Kleikamp  * file: that is left to a per-journal policy function, which allows us
21470decc6SDave Kleikamp  * to store the journal within a filesystem-specified area for ext2
22470decc6SDave Kleikamp  * journaling (ext2 can use a reserved inode for storing the log).
23470decc6SDave Kleikamp  */
24470decc6SDave Kleikamp 
25470decc6SDave Kleikamp #include <linux/module.h>
26470decc6SDave Kleikamp #include <linux/time.h>
27470decc6SDave Kleikamp #include <linux/fs.h>
28f7f4bccbSMingming Cao #include <linux/jbd2.h>
29470decc6SDave Kleikamp #include <linux/errno.h>
30470decc6SDave Kleikamp #include <linux/slab.h>
31470decc6SDave Kleikamp #include <linux/init.h>
32470decc6SDave Kleikamp #include <linux/mm.h>
337dfb7103SNigel Cunningham #include <linux/freezer.h>
34470decc6SDave Kleikamp #include <linux/pagemap.h>
35470decc6SDave Kleikamp #include <linux/kthread.h>
36470decc6SDave Kleikamp #include <linux/poison.h>
37470decc6SDave Kleikamp #include <linux/proc_fs.h>
388e85fb3fSJohann Lombardi #include <linux/seq_file.h>
39c225aa57SSimon Holm Thøgersen #include <linux/math64.h>
40879c5e6bSTheodore Ts'o #include <linux/hash.h>
41d2eecb03STheodore Ts'o #include <linux/log2.h>
42d2eecb03STheodore Ts'o #include <linux/vmalloc.h>
4347def826STheodore Ts'o #include <linux/backing-dev.h>
4439e3ac25SBrian King #include <linux/bitops.h>
45670be5a7STheodore Ts'o #include <linux/ratelimit.h>
46879c5e6bSTheodore Ts'o 
47879c5e6bSTheodore Ts'o #define CREATE_TRACE_POINTS
48879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h>
49470decc6SDave Kleikamp 
50470decc6SDave Kleikamp #include <asm/uaccess.h>
51470decc6SDave Kleikamp #include <asm/page.h>
52470decc6SDave Kleikamp 
53b6e96d00STheodore Ts'o #ifdef CONFIG_JBD2_DEBUG
54b6e96d00STheodore Ts'o ushort jbd2_journal_enable_debug __read_mostly;
55b6e96d00STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_enable_debug);
56b6e96d00STheodore Ts'o 
57b6e96d00STheodore Ts'o module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
58b6e96d00STheodore Ts'o MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
59b6e96d00STheodore Ts'o #endif
60b6e96d00STheodore Ts'o 
61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend);
62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop);
63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates);
64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates);
65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access);
66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access);
67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access);
68e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers);
69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget);
71470decc6SDave Kleikamp #if 0
72470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer);
73470decc6SDave Kleikamp #endif
74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush);
75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke);
76470decc6SDave Kleikamp 
77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev);
78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode);
79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features);
80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features);
81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features);
82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load);
83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy);
84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort);
85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno);
86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err);
87f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err);
88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit);
893b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit);
90f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit);
91f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
92f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe);
93f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
94f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage);
95f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
96f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit);
97c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_file_inode);
98c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
1018aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache);
102470decc6SDave Kleikamp 
103470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno);
104d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size);
105470decc6SDave Kleikamp 
106169f1a2aSPaul Gortmaker #ifdef CONFIG_JBD2_DEBUG
107169f1a2aSPaul Gortmaker void __jbd2_debug(int level, const char *file, const char *func,
108169f1a2aSPaul Gortmaker 		  unsigned int line, const char *fmt, ...)
109169f1a2aSPaul Gortmaker {
110169f1a2aSPaul Gortmaker 	struct va_format vaf;
111169f1a2aSPaul Gortmaker 	va_list args;
112169f1a2aSPaul Gortmaker 
113169f1a2aSPaul Gortmaker 	if (level > jbd2_journal_enable_debug)
114169f1a2aSPaul Gortmaker 		return;
115169f1a2aSPaul Gortmaker 	va_start(args, fmt);
116169f1a2aSPaul Gortmaker 	vaf.fmt = fmt;
117169f1a2aSPaul Gortmaker 	vaf.va = &args;
118169f1a2aSPaul Gortmaker 	printk(KERN_DEBUG "%s: (%s, %u): %pV\n", file, func, line, &vaf);
119169f1a2aSPaul Gortmaker 	va_end(args);
120169f1a2aSPaul Gortmaker }
121169f1a2aSPaul Gortmaker EXPORT_SYMBOL(__jbd2_debug);
122169f1a2aSPaul Gortmaker #endif
123169f1a2aSPaul Gortmaker 
12425ed6e8aSDarrick J. Wong /* Checksumming functions */
1257747e6d0SRashika Kheria static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
12625ed6e8aSDarrick J. Wong {
127db9ee220SDarrick J. Wong 	if (!jbd2_journal_has_csum_v2or3(j))
12825ed6e8aSDarrick J. Wong 		return 1;
12925ed6e8aSDarrick J. Wong 
13025ed6e8aSDarrick J. Wong 	return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
13125ed6e8aSDarrick J. Wong }
13225ed6e8aSDarrick J. Wong 
13318a6ea1eSDarrick J. Wong static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
1344fd5ea43SDarrick J. Wong {
13518a6ea1eSDarrick J. Wong 	__u32 csum;
13618a6ea1eSDarrick J. Wong 	__be32 old_csum;
1374fd5ea43SDarrick J. Wong 
1384fd5ea43SDarrick J. Wong 	old_csum = sb->s_checksum;
1394fd5ea43SDarrick J. Wong 	sb->s_checksum = 0;
1404fd5ea43SDarrick J. Wong 	csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
1414fd5ea43SDarrick J. Wong 	sb->s_checksum = old_csum;
1424fd5ea43SDarrick J. Wong 
1434fd5ea43SDarrick J. Wong 	return cpu_to_be32(csum);
1444fd5ea43SDarrick J. Wong }
1454fd5ea43SDarrick J. Wong 
1467747e6d0SRashika Kheria static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
1474fd5ea43SDarrick J. Wong {
148db9ee220SDarrick J. Wong 	if (!jbd2_journal_has_csum_v2or3(j))
1494fd5ea43SDarrick J. Wong 		return 1;
1504fd5ea43SDarrick J. Wong 
1514fd5ea43SDarrick J. Wong 	return sb->s_checksum == jbd2_superblock_csum(j, sb);
1524fd5ea43SDarrick J. Wong }
1534fd5ea43SDarrick J. Wong 
1547747e6d0SRashika Kheria static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
1554fd5ea43SDarrick J. Wong {
156db9ee220SDarrick J. Wong 	if (!jbd2_journal_has_csum_v2or3(j))
1574fd5ea43SDarrick J. Wong 		return;
1584fd5ea43SDarrick J. Wong 
1594fd5ea43SDarrick J. Wong 	sb->s_checksum = jbd2_superblock_csum(j, sb);
1604fd5ea43SDarrick J. Wong }
1614fd5ea43SDarrick J. Wong 
162470decc6SDave Kleikamp /*
163470decc6SDave Kleikamp  * Helper function used to manage commit timeouts
164470decc6SDave Kleikamp  */
165470decc6SDave Kleikamp 
166470decc6SDave Kleikamp static void commit_timeout(unsigned long __data)
167470decc6SDave Kleikamp {
168470decc6SDave Kleikamp 	struct task_struct * p = (struct task_struct *) __data;
169470decc6SDave Kleikamp 
170470decc6SDave Kleikamp 	wake_up_process(p);
171470decc6SDave Kleikamp }
172470decc6SDave Kleikamp 
173470decc6SDave Kleikamp /*
174f7f4bccbSMingming Cao  * kjournald2: The main thread function used to manage a logging device
175470decc6SDave Kleikamp  * journal.
176470decc6SDave Kleikamp  *
177470decc6SDave Kleikamp  * This kernel thread is responsible for two things:
178470decc6SDave Kleikamp  *
179470decc6SDave Kleikamp  * 1) COMMIT:  Every so often we need to commit the current state of the
180470decc6SDave Kleikamp  *    filesystem to disk.  The journal thread is responsible for writing
181470decc6SDave Kleikamp  *    all of the metadata buffers to disk.
182470decc6SDave Kleikamp  *
183470decc6SDave Kleikamp  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
184470decc6SDave Kleikamp  *    of the data in that part of the log has been rewritten elsewhere on
185470decc6SDave Kleikamp  *    the disk.  Flushing these old buffers to reclaim space in the log is
186470decc6SDave Kleikamp  *    known as checkpointing, and this thread is responsible for that job.
187470decc6SDave Kleikamp  */
188470decc6SDave Kleikamp 
189f7f4bccbSMingming Cao static int kjournald2(void *arg)
190470decc6SDave Kleikamp {
191470decc6SDave Kleikamp 	journal_t *journal = arg;
192470decc6SDave Kleikamp 	transaction_t *transaction;
193470decc6SDave Kleikamp 
194470decc6SDave Kleikamp 	/*
195470decc6SDave Kleikamp 	 * Set up an interval timer which can be used to trigger a commit wakeup
196470decc6SDave Kleikamp 	 * after the commit interval expires
197470decc6SDave Kleikamp 	 */
198470decc6SDave Kleikamp 	setup_timer(&journal->j_commit_timer, commit_timeout,
199470decc6SDave Kleikamp 			(unsigned long)current);
200470decc6SDave Kleikamp 
20135c80422SNigel Cunningham 	set_freezable();
20235c80422SNigel Cunningham 
203470decc6SDave Kleikamp 	/* Record that the journal thread is running */
204470decc6SDave Kleikamp 	journal->j_task = current;
205470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
206470decc6SDave Kleikamp 
207470decc6SDave Kleikamp 	/*
208470decc6SDave Kleikamp 	 * And now, wait forever for commit wakeup events.
209470decc6SDave Kleikamp 	 */
210a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
211470decc6SDave Kleikamp 
212470decc6SDave Kleikamp loop:
213f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_UNMOUNT)
214470decc6SDave Kleikamp 		goto end_loop;
215470decc6SDave Kleikamp 
216470decc6SDave Kleikamp 	jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
217470decc6SDave Kleikamp 		journal->j_commit_sequence, journal->j_commit_request);
218470decc6SDave Kleikamp 
219470decc6SDave Kleikamp 	if (journal->j_commit_sequence != journal->j_commit_request) {
220470decc6SDave Kleikamp 		jbd_debug(1, "OK, requests differ\n");
221a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
222470decc6SDave Kleikamp 		del_timer_sync(&journal->j_commit_timer);
223f7f4bccbSMingming Cao 		jbd2_journal_commit_transaction(journal);
224a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
225470decc6SDave Kleikamp 		goto loop;
226470decc6SDave Kleikamp 	}
227470decc6SDave Kleikamp 
228470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
229470decc6SDave Kleikamp 	if (freezing(current)) {
230470decc6SDave Kleikamp 		/*
231470decc6SDave Kleikamp 		 * The simpler the better. Flushing journal isn't a
232470decc6SDave Kleikamp 		 * good idea, because that depends on threads that may
233470decc6SDave Kleikamp 		 * be already stopped.
234470decc6SDave Kleikamp 		 */
235f7f4bccbSMingming Cao 		jbd_debug(1, "Now suspending kjournald2\n");
236a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
237a0acae0eSTejun Heo 		try_to_freeze();
238a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
239470decc6SDave Kleikamp 	} else {
240470decc6SDave Kleikamp 		/*
241470decc6SDave Kleikamp 		 * We assume on resume that commits are already there,
242470decc6SDave Kleikamp 		 * so we don't sleep
243470decc6SDave Kleikamp 		 */
244470decc6SDave Kleikamp 		DEFINE_WAIT(wait);
245470decc6SDave Kleikamp 		int should_sleep = 1;
246470decc6SDave Kleikamp 
247470decc6SDave Kleikamp 		prepare_to_wait(&journal->j_wait_commit, &wait,
248470decc6SDave Kleikamp 				TASK_INTERRUPTIBLE);
249470decc6SDave Kleikamp 		if (journal->j_commit_sequence != journal->j_commit_request)
250470decc6SDave Kleikamp 			should_sleep = 0;
251470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
252470decc6SDave Kleikamp 		if (transaction && time_after_eq(jiffies,
253470decc6SDave Kleikamp 						transaction->t_expires))
254470decc6SDave Kleikamp 			should_sleep = 0;
255f7f4bccbSMingming Cao 		if (journal->j_flags & JBD2_UNMOUNT)
256470decc6SDave Kleikamp 			should_sleep = 0;
257470decc6SDave Kleikamp 		if (should_sleep) {
258a931da6aSTheodore Ts'o 			write_unlock(&journal->j_state_lock);
259470decc6SDave Kleikamp 			schedule();
260a931da6aSTheodore Ts'o 			write_lock(&journal->j_state_lock);
261470decc6SDave Kleikamp 		}
262470decc6SDave Kleikamp 		finish_wait(&journal->j_wait_commit, &wait);
263470decc6SDave Kleikamp 	}
264470decc6SDave Kleikamp 
265f7f4bccbSMingming Cao 	jbd_debug(1, "kjournald2 wakes\n");
266470decc6SDave Kleikamp 
267470decc6SDave Kleikamp 	/*
268470decc6SDave Kleikamp 	 * Were we woken up by a commit wakeup event?
269470decc6SDave Kleikamp 	 */
270470decc6SDave Kleikamp 	transaction = journal->j_running_transaction;
271470decc6SDave Kleikamp 	if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
272470decc6SDave Kleikamp 		journal->j_commit_request = transaction->t_tid;
273470decc6SDave Kleikamp 		jbd_debug(1, "woke because of timeout\n");
274470decc6SDave Kleikamp 	}
275470decc6SDave Kleikamp 	goto loop;
276470decc6SDave Kleikamp 
277470decc6SDave Kleikamp end_loop:
278a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
279470decc6SDave Kleikamp 	del_timer_sync(&journal->j_commit_timer);
280470decc6SDave Kleikamp 	journal->j_task = NULL;
281470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
282470decc6SDave Kleikamp 	jbd_debug(1, "Journal thread exiting.\n");
283470decc6SDave Kleikamp 	return 0;
284470decc6SDave Kleikamp }
285470decc6SDave Kleikamp 
28697f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal)
287470decc6SDave Kleikamp {
28897f06784SPavel Emelianov 	struct task_struct *t;
28997f06784SPavel Emelianov 
29090576c0bSTheodore Ts'o 	t = kthread_run(kjournald2, journal, "jbd2/%s",
29190576c0bSTheodore Ts'o 			journal->j_devname);
29297f06784SPavel Emelianov 	if (IS_ERR(t))
29397f06784SPavel Emelianov 		return PTR_ERR(t);
29497f06784SPavel Emelianov 
2951076d17aSAl Viro 	wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
29697f06784SPavel Emelianov 	return 0;
297470decc6SDave Kleikamp }
298470decc6SDave Kleikamp 
299470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal)
300470decc6SDave Kleikamp {
301a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
302f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_UNMOUNT;
303470decc6SDave Kleikamp 
304470decc6SDave Kleikamp 	while (journal->j_task) {
305a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
3063469a32aSTheodore Ts'o 		wake_up(&journal->j_wait_commit);
3071076d17aSAl Viro 		wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
308a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
309470decc6SDave Kleikamp 	}
310a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
311470decc6SDave Kleikamp }
312470decc6SDave Kleikamp 
313470decc6SDave Kleikamp /*
314f7f4bccbSMingming Cao  * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
315470decc6SDave Kleikamp  *
316470decc6SDave Kleikamp  * Writes a metadata buffer to a given disk block.  The actual IO is not
317470decc6SDave Kleikamp  * performed but a new buffer_head is constructed which labels the data
318470decc6SDave Kleikamp  * to be written with the correct destination disk block.
319470decc6SDave Kleikamp  *
320470decc6SDave Kleikamp  * Any magic-number escaping which needs to be done will cause a
321470decc6SDave Kleikamp  * copy-out here.  If the buffer happens to start with the
322f7f4bccbSMingming Cao  * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
323470decc6SDave Kleikamp  * magic number is only written to the log for descripter blocks.  In
324470decc6SDave Kleikamp  * this case, we copy the data and replace the first word with 0, and we
325470decc6SDave Kleikamp  * return a result code which indicates that this buffer needs to be
326470decc6SDave Kleikamp  * marked as an escaped buffer in the corresponding log descriptor
327470decc6SDave Kleikamp  * block.  The missing word can then be restored when the block is read
328470decc6SDave Kleikamp  * during recovery.
329470decc6SDave Kleikamp  *
330470decc6SDave Kleikamp  * If the source buffer has already been modified by a new transaction
331470decc6SDave Kleikamp  * since we took the last commit snapshot, we use the frozen copy of
332470decc6SDave Kleikamp  * that data for IO. If we end up using the existing buffer_head's data
333f5113effSJan Kara  * for the write, then we have to make sure nobody modifies it while the
334f5113effSJan Kara  * IO is in progress. do_get_write_access() handles this.
335470decc6SDave Kleikamp  *
336f5113effSJan Kara  * The function returns a pointer to the buffer_head to be used for IO.
337470decc6SDave Kleikamp  *
338470decc6SDave Kleikamp  *
339470decc6SDave Kleikamp  * Return value:
340470decc6SDave Kleikamp  *  <0: Error
341470decc6SDave Kleikamp  * >=0: Finished OK
342470decc6SDave Kleikamp  *
343470decc6SDave Kleikamp  * On success:
344470decc6SDave Kleikamp  * Bit 0 set == escape performed on the data
345470decc6SDave Kleikamp  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
346470decc6SDave Kleikamp  */
347470decc6SDave Kleikamp 
348f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
349470decc6SDave Kleikamp 				  struct journal_head  *jh_in,
350f5113effSJan Kara 				  struct buffer_head **bh_out,
351f5113effSJan Kara 				  sector_t blocknr)
352470decc6SDave Kleikamp {
353470decc6SDave Kleikamp 	int need_copy_out = 0;
354470decc6SDave Kleikamp 	int done_copy_out = 0;
355470decc6SDave Kleikamp 	int do_escape = 0;
356470decc6SDave Kleikamp 	char *mapped_data;
357470decc6SDave Kleikamp 	struct buffer_head *new_bh;
358470decc6SDave Kleikamp 	struct page *new_page;
359470decc6SDave Kleikamp 	unsigned int new_offset;
360470decc6SDave Kleikamp 	struct buffer_head *bh_in = jh2bh(jh_in);
36196577c43Sdingdinghua 	journal_t *journal = transaction->t_journal;
362470decc6SDave Kleikamp 
363470decc6SDave Kleikamp 	/*
364470decc6SDave Kleikamp 	 * The buffer really shouldn't be locked: only the current committing
365470decc6SDave Kleikamp 	 * transaction is allowed to write it, so nobody else is allowed
366470decc6SDave Kleikamp 	 * to do any IO.
367470decc6SDave Kleikamp 	 *
368470decc6SDave Kleikamp 	 * akpm: except if we're journalling data, and write() output is
369470decc6SDave Kleikamp 	 * also part of a shared mapping, and another thread has
370470decc6SDave Kleikamp 	 * decided to launch a writepage() against this buffer.
371470decc6SDave Kleikamp 	 */
372470decc6SDave Kleikamp 	J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
373470decc6SDave Kleikamp 
3746ccaf3e2SMichal Hocko 	new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
37547def826STheodore Ts'o 
37696577c43Sdingdinghua 	/* keep subsequent assertions sane */
37796577c43Sdingdinghua 	atomic_set(&new_bh->b_count, 1);
378470decc6SDave Kleikamp 
379f5113effSJan Kara 	jbd_lock_bh_state(bh_in);
380f5113effSJan Kara repeat:
381470decc6SDave Kleikamp 	/*
382470decc6SDave Kleikamp 	 * If a new transaction has already done a buffer copy-out, then
383470decc6SDave Kleikamp 	 * we use that version of the data for the commit.
384470decc6SDave Kleikamp 	 */
385470decc6SDave Kleikamp 	if (jh_in->b_frozen_data) {
386470decc6SDave Kleikamp 		done_copy_out = 1;
387470decc6SDave Kleikamp 		new_page = virt_to_page(jh_in->b_frozen_data);
388470decc6SDave Kleikamp 		new_offset = offset_in_page(jh_in->b_frozen_data);
389470decc6SDave Kleikamp 	} else {
390470decc6SDave Kleikamp 		new_page = jh2bh(jh_in)->b_page;
391470decc6SDave Kleikamp 		new_offset = offset_in_page(jh2bh(jh_in)->b_data);
392470decc6SDave Kleikamp 	}
393470decc6SDave Kleikamp 
394303a8f2aSCong Wang 	mapped_data = kmap_atomic(new_page);
395470decc6SDave Kleikamp 	/*
39613ceef09SJan Kara 	 * Fire data frozen trigger if data already wasn't frozen.  Do this
39713ceef09SJan Kara 	 * before checking for escaping, as the trigger may modify the magic
39813ceef09SJan Kara 	 * offset.  If a copy-out happens afterwards, it will have the correct
39913ceef09SJan Kara 	 * data in the buffer.
400e06c8227SJoel Becker 	 */
40113ceef09SJan Kara 	if (!done_copy_out)
40213ceef09SJan Kara 		jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
40313ceef09SJan Kara 					   jh_in->b_triggers);
404e06c8227SJoel Becker 
405e06c8227SJoel Becker 	/*
406470decc6SDave Kleikamp 	 * Check for escaping
407470decc6SDave Kleikamp 	 */
408470decc6SDave Kleikamp 	if (*((__be32 *)(mapped_data + new_offset)) ==
409f7f4bccbSMingming Cao 				cpu_to_be32(JBD2_MAGIC_NUMBER)) {
410470decc6SDave Kleikamp 		need_copy_out = 1;
411470decc6SDave Kleikamp 		do_escape = 1;
412470decc6SDave Kleikamp 	}
413303a8f2aSCong Wang 	kunmap_atomic(mapped_data);
414470decc6SDave Kleikamp 
415470decc6SDave Kleikamp 	/*
416470decc6SDave Kleikamp 	 * Do we need to do a data copy?
417470decc6SDave Kleikamp 	 */
418470decc6SDave Kleikamp 	if (need_copy_out && !done_copy_out) {
419470decc6SDave Kleikamp 		char *tmp;
420470decc6SDave Kleikamp 
421470decc6SDave Kleikamp 		jbd_unlock_bh_state(bh_in);
422af1e76d6SMingming Cao 		tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
423e6ec116bSTheodore Ts'o 		if (!tmp) {
424f5113effSJan Kara 			brelse(new_bh);
425e6ec116bSTheodore Ts'o 			return -ENOMEM;
426e6ec116bSTheodore Ts'o 		}
427470decc6SDave Kleikamp 		jbd_lock_bh_state(bh_in);
428470decc6SDave Kleikamp 		if (jh_in->b_frozen_data) {
429af1e76d6SMingming Cao 			jbd2_free(tmp, bh_in->b_size);
430470decc6SDave Kleikamp 			goto repeat;
431470decc6SDave Kleikamp 		}
432470decc6SDave Kleikamp 
433470decc6SDave Kleikamp 		jh_in->b_frozen_data = tmp;
434303a8f2aSCong Wang 		mapped_data = kmap_atomic(new_page);
435f5113effSJan Kara 		memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
436303a8f2aSCong Wang 		kunmap_atomic(mapped_data);
437470decc6SDave Kleikamp 
438470decc6SDave Kleikamp 		new_page = virt_to_page(tmp);
439470decc6SDave Kleikamp 		new_offset = offset_in_page(tmp);
440470decc6SDave Kleikamp 		done_copy_out = 1;
441e06c8227SJoel Becker 
442e06c8227SJoel Becker 		/*
443e06c8227SJoel Becker 		 * This isn't strictly necessary, as we're using frozen
444e06c8227SJoel Becker 		 * data for the escaping, but it keeps consistency with
445e06c8227SJoel Becker 		 * b_frozen_data usage.
446e06c8227SJoel Becker 		 */
447e06c8227SJoel Becker 		jh_in->b_frozen_triggers = jh_in->b_triggers;
448470decc6SDave Kleikamp 	}
449470decc6SDave Kleikamp 
450470decc6SDave Kleikamp 	/*
451470decc6SDave Kleikamp 	 * Did we need to do an escaping?  Now we've done all the
452470decc6SDave Kleikamp 	 * copying, we can finally do so.
453470decc6SDave Kleikamp 	 */
454470decc6SDave Kleikamp 	if (do_escape) {
455303a8f2aSCong Wang 		mapped_data = kmap_atomic(new_page);
456470decc6SDave Kleikamp 		*((unsigned int *)(mapped_data + new_offset)) = 0;
457303a8f2aSCong Wang 		kunmap_atomic(mapped_data);
458470decc6SDave Kleikamp 	}
459470decc6SDave Kleikamp 
460470decc6SDave Kleikamp 	set_bh_page(new_bh, new_page, new_offset);
461f5113effSJan Kara 	new_bh->b_size = bh_in->b_size;
462f5113effSJan Kara 	new_bh->b_bdev = journal->j_dev;
463470decc6SDave Kleikamp 	new_bh->b_blocknr = blocknr;
464b34090e5SJan Kara 	new_bh->b_private = bh_in;
465470decc6SDave Kleikamp 	set_buffer_mapped(new_bh);
466470decc6SDave Kleikamp 	set_buffer_dirty(new_bh);
467470decc6SDave Kleikamp 
468f5113effSJan Kara 	*bh_out = new_bh;
469470decc6SDave Kleikamp 
470470decc6SDave Kleikamp 	/*
471470decc6SDave Kleikamp 	 * The to-be-written buffer needs to get moved to the io queue,
472470decc6SDave Kleikamp 	 * and the original buffer whose contents we are shadowing or
473470decc6SDave Kleikamp 	 * copying is moved to the transaction's shadow queue.
474470decc6SDave Kleikamp 	 */
475470decc6SDave Kleikamp 	JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
47696577c43Sdingdinghua 	spin_lock(&journal->j_list_lock);
47796577c43Sdingdinghua 	__jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
47896577c43Sdingdinghua 	spin_unlock(&journal->j_list_lock);
479b34090e5SJan Kara 	set_buffer_shadow(bh_in);
48096577c43Sdingdinghua 	jbd_unlock_bh_state(bh_in);
48196577c43Sdingdinghua 
482470decc6SDave Kleikamp 	return do_escape | (done_copy_out << 1);
483470decc6SDave Kleikamp }
484470decc6SDave Kleikamp 
485470decc6SDave Kleikamp /*
486470decc6SDave Kleikamp  * Allocation code for the journal file.  Manage the space left in the
487470decc6SDave Kleikamp  * journal, so that we can begin checkpointing when appropriate.
488470decc6SDave Kleikamp  */
489470decc6SDave Kleikamp 
490470decc6SDave Kleikamp /*
491e4471831STheodore Ts'o  * Called with j_state_lock locked for writing.
492e4471831STheodore Ts'o  * Returns true if a transaction commit was started.
493470decc6SDave Kleikamp  */
494f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target)
495470decc6SDave Kleikamp {
496e7b04ac0SEric Sandeen 	/* Return if the txn has already requested to be committed */
497e7b04ac0SEric Sandeen 	if (journal->j_commit_request == target)
498e7b04ac0SEric Sandeen 		return 0;
499e7b04ac0SEric Sandeen 
500470decc6SDave Kleikamp 	/*
501deeeaf13STheodore Ts'o 	 * The only transaction we can possibly wait upon is the
502deeeaf13STheodore Ts'o 	 * currently running transaction (if it exists).  Otherwise,
503deeeaf13STheodore Ts'o 	 * the target tid must be an old one.
504470decc6SDave Kleikamp 	 */
505deeeaf13STheodore Ts'o 	if (journal->j_running_transaction &&
506deeeaf13STheodore Ts'o 	    journal->j_running_transaction->t_tid == target) {
507470decc6SDave Kleikamp 		/*
508bcf3d0bcSAndrea Gelmini 		 * We want a new commit: OK, mark the request and wakeup the
509470decc6SDave Kleikamp 		 * commit thread.  We do _not_ do the commit ourselves.
510470decc6SDave Kleikamp 		 */
511470decc6SDave Kleikamp 
512470decc6SDave Kleikamp 		journal->j_commit_request = target;
513f2a44523SEryu Guan 		jbd_debug(1, "JBD2: requesting commit %d/%d\n",
514470decc6SDave Kleikamp 			  journal->j_commit_request,
515470decc6SDave Kleikamp 			  journal->j_commit_sequence);
5169fff24aaSTheodore Ts'o 		journal->j_running_transaction->t_requested = jiffies;
517470decc6SDave Kleikamp 		wake_up(&journal->j_wait_commit);
518470decc6SDave Kleikamp 		return 1;
519deeeaf13STheodore Ts'o 	} else if (!tid_geq(journal->j_commit_request, target))
520deeeaf13STheodore Ts'o 		/* This should never happen, but if it does, preserve
521deeeaf13STheodore Ts'o 		   the evidence before kjournald goes into a loop and
522deeeaf13STheodore Ts'o 		   increments j_commit_sequence beyond all recognition. */
523f2a44523SEryu Guan 		WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
5241be2add6STheodore Ts'o 			  journal->j_commit_request,
5251be2add6STheodore Ts'o 			  journal->j_commit_sequence,
526deeeaf13STheodore Ts'o 			  target, journal->j_running_transaction ?
527deeeaf13STheodore Ts'o 			  journal->j_running_transaction->t_tid : 0);
528470decc6SDave Kleikamp 	return 0;
529470decc6SDave Kleikamp }
530470decc6SDave Kleikamp 
531f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid)
532470decc6SDave Kleikamp {
533470decc6SDave Kleikamp 	int ret;
534470decc6SDave Kleikamp 
535a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
536f7f4bccbSMingming Cao 	ret = __jbd2_log_start_commit(journal, tid);
537a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
538470decc6SDave Kleikamp 	return ret;
539470decc6SDave Kleikamp }
540470decc6SDave Kleikamp 
541470decc6SDave Kleikamp /*
5429ff86446SDmitry Monakhov  * Force and wait any uncommitted transactions.  We can only force the running
5439ff86446SDmitry Monakhov  * transaction if we don't have an active handle, otherwise, we will deadlock.
5449ff86446SDmitry Monakhov  * Returns: <0 in case of error,
5459ff86446SDmitry Monakhov  *           0 if nothing to commit,
5469ff86446SDmitry Monakhov  *           1 if transaction was successfully committed.
547470decc6SDave Kleikamp  */
5489ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal)
549470decc6SDave Kleikamp {
550470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
551470decc6SDave Kleikamp 	tid_t tid;
5529ff86446SDmitry Monakhov 	int need_to_start = 0, ret = 0;
553470decc6SDave Kleikamp 
554a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
555470decc6SDave Kleikamp 	if (journal->j_running_transaction && !current->journal_info) {
556470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
557e4471831STheodore Ts'o 		if (!tid_geq(journal->j_commit_request, transaction->t_tid))
558e4471831STheodore Ts'o 			need_to_start = 1;
559470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
560470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
561470decc6SDave Kleikamp 
562470decc6SDave Kleikamp 	if (!transaction) {
5639ff86446SDmitry Monakhov 		/* Nothing to commit */
564a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
5659ff86446SDmitry Monakhov 		return 0;
566470decc6SDave Kleikamp 	}
567470decc6SDave Kleikamp 	tid = transaction->t_tid;
568a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
569e4471831STheodore Ts'o 	if (need_to_start)
570e4471831STheodore Ts'o 		jbd2_log_start_commit(journal, tid);
5719ff86446SDmitry Monakhov 	ret = jbd2_log_wait_commit(journal, tid);
5729ff86446SDmitry Monakhov 	if (!ret)
5739ff86446SDmitry Monakhov 		ret = 1;
5749ff86446SDmitry Monakhov 
5759ff86446SDmitry Monakhov 	return ret;
5769ff86446SDmitry Monakhov }
5779ff86446SDmitry Monakhov 
5789ff86446SDmitry Monakhov /**
5799ff86446SDmitry Monakhov  * Force and wait upon a commit if the calling process is not within
5809ff86446SDmitry Monakhov  * transaction.  This is used for forcing out undo-protected data which contains
5819ff86446SDmitry Monakhov  * bitmaps, when the fs is running out of space.
5829ff86446SDmitry Monakhov  *
5839ff86446SDmitry Monakhov  * @journal: journal to force
5849ff86446SDmitry Monakhov  * Returns true if progress was made.
5859ff86446SDmitry Monakhov  */
5869ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal)
5879ff86446SDmitry Monakhov {
5889ff86446SDmitry Monakhov 	int ret;
5899ff86446SDmitry Monakhov 
5909ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
5919ff86446SDmitry Monakhov 	return ret > 0;
5929ff86446SDmitry Monakhov }
5939ff86446SDmitry Monakhov 
5949ff86446SDmitry Monakhov /**
5959ff86446SDmitry Monakhov  * int journal_force_commit() - force any uncommitted transactions
5969ff86446SDmitry Monakhov  * @journal: journal to force
5979ff86446SDmitry Monakhov  *
5989ff86446SDmitry Monakhov  * Caller want unconditional commit. We can only force the running transaction
5999ff86446SDmitry Monakhov  * if we don't have an active handle, otherwise, we will deadlock.
6009ff86446SDmitry Monakhov  */
6019ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal)
6029ff86446SDmitry Monakhov {
6039ff86446SDmitry Monakhov 	int ret;
6049ff86446SDmitry Monakhov 
6059ff86446SDmitry Monakhov 	J_ASSERT(!current->journal_info);
6069ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
6079ff86446SDmitry Monakhov 	if (ret > 0)
6089ff86446SDmitry Monakhov 		ret = 0;
6099ff86446SDmitry Monakhov 	return ret;
610470decc6SDave Kleikamp }
611470decc6SDave Kleikamp 
612470decc6SDave Kleikamp /*
613470decc6SDave Kleikamp  * Start a commit of the current running transaction (if any).  Returns true
614c88ccea3SJan Kara  * if a transaction is going to be committed (or is currently already
615c88ccea3SJan Kara  * committing), and fills its tid in at *ptid
616470decc6SDave Kleikamp  */
617f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
618470decc6SDave Kleikamp {
619470decc6SDave Kleikamp 	int ret = 0;
620470decc6SDave Kleikamp 
621a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
622470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
623470decc6SDave Kleikamp 		tid_t tid = journal->j_running_transaction->t_tid;
624470decc6SDave Kleikamp 
625c88ccea3SJan Kara 		__jbd2_log_start_commit(journal, tid);
626c88ccea3SJan Kara 		/* There's a running transaction and we've just made sure
627c88ccea3SJan Kara 		 * it's commit has been scheduled. */
628c88ccea3SJan Kara 		if (ptid)
629470decc6SDave Kleikamp 			*ptid = tid;
630c88ccea3SJan Kara 		ret = 1;
631c88ccea3SJan Kara 	} else if (journal->j_committing_transaction) {
632470decc6SDave Kleikamp 		/*
63312810ad7SArtem Bityutskiy 		 * If commit has been started, then we have to wait for
63412810ad7SArtem Bityutskiy 		 * completion of that transaction.
635470decc6SDave Kleikamp 		 */
636c88ccea3SJan Kara 		if (ptid)
637470decc6SDave Kleikamp 			*ptid = journal->j_committing_transaction->t_tid;
638470decc6SDave Kleikamp 		ret = 1;
639470decc6SDave Kleikamp 	}
640a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
641470decc6SDave Kleikamp 	return ret;
642470decc6SDave Kleikamp }
643470decc6SDave Kleikamp 
644470decc6SDave Kleikamp /*
645bbd2be36SJan Kara  * Return 1 if a given transaction has not yet sent barrier request
646bbd2be36SJan Kara  * connected with a transaction commit. If 0 is returned, transaction
647bbd2be36SJan Kara  * may or may not have sent the barrier. Used to avoid sending barrier
648bbd2be36SJan Kara  * twice in common cases.
649bbd2be36SJan Kara  */
650bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
651bbd2be36SJan Kara {
652bbd2be36SJan Kara 	int ret = 0;
653bbd2be36SJan Kara 	transaction_t *commit_trans;
654bbd2be36SJan Kara 
655bbd2be36SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
656bbd2be36SJan Kara 		return 0;
657bbd2be36SJan Kara 	read_lock(&journal->j_state_lock);
658bbd2be36SJan Kara 	/* Transaction already committed? */
659bbd2be36SJan Kara 	if (tid_geq(journal->j_commit_sequence, tid))
660bbd2be36SJan Kara 		goto out;
661bbd2be36SJan Kara 	commit_trans = journal->j_committing_transaction;
662bbd2be36SJan Kara 	if (!commit_trans || commit_trans->t_tid != tid) {
663bbd2be36SJan Kara 		ret = 1;
664bbd2be36SJan Kara 		goto out;
665bbd2be36SJan Kara 	}
666bbd2be36SJan Kara 	/*
667bbd2be36SJan Kara 	 * Transaction is being committed and we already proceeded to
668bbd2be36SJan Kara 	 * submitting a flush to fs partition?
669bbd2be36SJan Kara 	 */
670bbd2be36SJan Kara 	if (journal->j_fs_dev != journal->j_dev) {
671bbd2be36SJan Kara 		if (!commit_trans->t_need_data_flush ||
672bbd2be36SJan Kara 		    commit_trans->t_state >= T_COMMIT_DFLUSH)
673bbd2be36SJan Kara 			goto out;
674bbd2be36SJan Kara 	} else {
675bbd2be36SJan Kara 		if (commit_trans->t_state >= T_COMMIT_JFLUSH)
676bbd2be36SJan Kara 			goto out;
677bbd2be36SJan Kara 	}
678bbd2be36SJan Kara 	ret = 1;
679bbd2be36SJan Kara out:
680bbd2be36SJan Kara 	read_unlock(&journal->j_state_lock);
681bbd2be36SJan Kara 	return ret;
682bbd2be36SJan Kara }
683bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
684bbd2be36SJan Kara 
685bbd2be36SJan Kara /*
686470decc6SDave Kleikamp  * Wait for a specified commit to complete.
687470decc6SDave Kleikamp  * The caller may not hold the journal lock.
688470decc6SDave Kleikamp  */
689f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
690470decc6SDave Kleikamp {
691470decc6SDave Kleikamp 	int err = 0;
692470decc6SDave Kleikamp 
693a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
694e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
695470decc6SDave Kleikamp 	if (!tid_geq(journal->j_commit_request, tid)) {
69675685071SJan Kara 		printk(KERN_ERR
697470decc6SDave Kleikamp 		       "%s: error: j_commit_request=%d, tid=%d\n",
698329d291fSHarvey Harrison 		       __func__, journal->j_commit_request, tid);
699470decc6SDave Kleikamp 	}
700470decc6SDave Kleikamp #endif
701470decc6SDave Kleikamp 	while (tid_gt(tid, journal->j_commit_sequence)) {
702f2a44523SEryu Guan 		jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
703470decc6SDave Kleikamp 				  tid, journal->j_commit_sequence);
704a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
7053469a32aSTheodore Ts'o 		wake_up(&journal->j_wait_commit);
706470decc6SDave Kleikamp 		wait_event(journal->j_wait_done_commit,
707470decc6SDave Kleikamp 				!tid_gt(tid, journal->j_commit_sequence));
708a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
709470decc6SDave Kleikamp 	}
710a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
711470decc6SDave Kleikamp 
71275685071SJan Kara 	if (unlikely(is_journal_aborted(journal)))
713470decc6SDave Kleikamp 		err = -EIO;
714470decc6SDave Kleikamp 	return err;
715470decc6SDave Kleikamp }
716470decc6SDave Kleikamp 
717470decc6SDave Kleikamp /*
718d76a3a77STheodore Ts'o  * When this function returns the transaction corresponding to tid
719d76a3a77STheodore Ts'o  * will be completed.  If the transaction has currently running, start
720d76a3a77STheodore Ts'o  * committing that transaction before waiting for it to complete.  If
721d76a3a77STheodore Ts'o  * the transaction id is stale, it is by definition already completed,
722d76a3a77STheodore Ts'o  * so just return SUCCESS.
723d76a3a77STheodore Ts'o  */
724d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid)
725d76a3a77STheodore Ts'o {
726d76a3a77STheodore Ts'o 	int	need_to_wait = 1;
727d76a3a77STheodore Ts'o 
728d76a3a77STheodore Ts'o 	read_lock(&journal->j_state_lock);
729d76a3a77STheodore Ts'o 	if (journal->j_running_transaction &&
730d76a3a77STheodore Ts'o 	    journal->j_running_transaction->t_tid == tid) {
731d76a3a77STheodore Ts'o 		if (journal->j_commit_request != tid) {
732d76a3a77STheodore Ts'o 			/* transaction not yet started, so request it */
733d76a3a77STheodore Ts'o 			read_unlock(&journal->j_state_lock);
734d76a3a77STheodore Ts'o 			jbd2_log_start_commit(journal, tid);
735d76a3a77STheodore Ts'o 			goto wait_commit;
736d76a3a77STheodore Ts'o 		}
737d76a3a77STheodore Ts'o 	} else if (!(journal->j_committing_transaction &&
738d76a3a77STheodore Ts'o 		     journal->j_committing_transaction->t_tid == tid))
739d76a3a77STheodore Ts'o 		need_to_wait = 0;
740d76a3a77STheodore Ts'o 	read_unlock(&journal->j_state_lock);
741d76a3a77STheodore Ts'o 	if (!need_to_wait)
742d76a3a77STheodore Ts'o 		return 0;
743d76a3a77STheodore Ts'o wait_commit:
744d76a3a77STheodore Ts'o 	return jbd2_log_wait_commit(journal, tid);
745d76a3a77STheodore Ts'o }
746d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction);
747d76a3a77STheodore Ts'o 
748d76a3a77STheodore Ts'o /*
749470decc6SDave Kleikamp  * Log buffer allocation routines:
750470decc6SDave Kleikamp  */
751470decc6SDave Kleikamp 
75218eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
753470decc6SDave Kleikamp {
754470decc6SDave Kleikamp 	unsigned long blocknr;
755470decc6SDave Kleikamp 
756a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
757470decc6SDave Kleikamp 	J_ASSERT(journal->j_free > 1);
758470decc6SDave Kleikamp 
759470decc6SDave Kleikamp 	blocknr = journal->j_head;
760470decc6SDave Kleikamp 	journal->j_head++;
761470decc6SDave Kleikamp 	journal->j_free--;
762470decc6SDave Kleikamp 	if (journal->j_head == journal->j_last)
763470decc6SDave Kleikamp 		journal->j_head = journal->j_first;
764a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
765f7f4bccbSMingming Cao 	return jbd2_journal_bmap(journal, blocknr, retp);
766470decc6SDave Kleikamp }
767470decc6SDave Kleikamp 
768470decc6SDave Kleikamp /*
769470decc6SDave Kleikamp  * Conversion of logical to physical block numbers for the journal
770470decc6SDave Kleikamp  *
771470decc6SDave Kleikamp  * On external journals the journal blocks are identity-mapped, so
772470decc6SDave Kleikamp  * this is a no-op.  If needed, we can use j_blk_offset - everything is
773470decc6SDave Kleikamp  * ready.
774470decc6SDave Kleikamp  */
775f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
77618eba7aaSMingming Cao 		 unsigned long long *retp)
777470decc6SDave Kleikamp {
778470decc6SDave Kleikamp 	int err = 0;
77918eba7aaSMingming Cao 	unsigned long long ret;
780470decc6SDave Kleikamp 
781470decc6SDave Kleikamp 	if (journal->j_inode) {
782470decc6SDave Kleikamp 		ret = bmap(journal->j_inode, blocknr);
783470decc6SDave Kleikamp 		if (ret)
784470decc6SDave Kleikamp 			*retp = ret;
785470decc6SDave Kleikamp 		else {
786470decc6SDave Kleikamp 			printk(KERN_ALERT "%s: journal block not found "
787470decc6SDave Kleikamp 					"at offset %lu on %s\n",
78805496769STheodore Ts'o 			       __func__, blocknr, journal->j_devname);
789470decc6SDave Kleikamp 			err = -EIO;
790470decc6SDave Kleikamp 			__journal_abort_soft(journal, err);
791470decc6SDave Kleikamp 		}
792470decc6SDave Kleikamp 	} else {
793470decc6SDave Kleikamp 		*retp = blocknr; /* +journal->j_blk_offset */
794470decc6SDave Kleikamp 	}
795470decc6SDave Kleikamp 	return err;
796470decc6SDave Kleikamp }
797470decc6SDave Kleikamp 
798470decc6SDave Kleikamp /*
799470decc6SDave Kleikamp  * We play buffer_head aliasing tricks to write data/metadata blocks to
800470decc6SDave Kleikamp  * the journal without copying their contents, but for journal
801470decc6SDave Kleikamp  * descriptor blocks we do need to generate bona fide buffers.
802470decc6SDave Kleikamp  *
803f7f4bccbSMingming Cao  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
804470decc6SDave Kleikamp  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
805470decc6SDave Kleikamp  * But we don't bother doing that, so there will be coherency problems with
806470decc6SDave Kleikamp  * mmaps of blockdevs which hold live JBD-controlled filesystems.
807470decc6SDave Kleikamp  */
808e5a120aeSJan Kara struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
809470decc6SDave Kleikamp {
810470decc6SDave Kleikamp 	struct buffer_head *bh;
81118eba7aaSMingming Cao 	unsigned long long blocknr;
812470decc6SDave Kleikamp 	int err;
813470decc6SDave Kleikamp 
814f7f4bccbSMingming Cao 	err = jbd2_journal_next_log_block(journal, &blocknr);
815470decc6SDave Kleikamp 
816470decc6SDave Kleikamp 	if (err)
817470decc6SDave Kleikamp 		return NULL;
818470decc6SDave Kleikamp 
819470decc6SDave Kleikamp 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
8204b905671SJan Kara 	if (!bh)
8214b905671SJan Kara 		return NULL;
822470decc6SDave Kleikamp 	lock_buffer(bh);
823470decc6SDave Kleikamp 	memset(bh->b_data, 0, journal->j_blocksize);
824470decc6SDave Kleikamp 	set_buffer_uptodate(bh);
825470decc6SDave Kleikamp 	unlock_buffer(bh);
826470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "return this buffer");
827e5a120aeSJan Kara 	return bh;
828470decc6SDave Kleikamp }
829470decc6SDave Kleikamp 
83079feb521SJan Kara /*
83179feb521SJan Kara  * Return tid of the oldest transaction in the journal and block in the journal
83279feb521SJan Kara  * where the transaction starts.
83379feb521SJan Kara  *
83479feb521SJan Kara  * If the journal is now empty, return which will be the next transaction ID
83579feb521SJan Kara  * we will write and where will that transaction start.
83679feb521SJan Kara  *
83779feb521SJan Kara  * The return value is 0 if journal tail cannot be pushed any further, 1 if
83879feb521SJan Kara  * it can.
83979feb521SJan Kara  */
84079feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
84179feb521SJan Kara 			      unsigned long *block)
84279feb521SJan Kara {
84379feb521SJan Kara 	transaction_t *transaction;
84479feb521SJan Kara 	int ret;
84579feb521SJan Kara 
84679feb521SJan Kara 	read_lock(&journal->j_state_lock);
84779feb521SJan Kara 	spin_lock(&journal->j_list_lock);
84879feb521SJan Kara 	transaction = journal->j_checkpoint_transactions;
84979feb521SJan Kara 	if (transaction) {
85079feb521SJan Kara 		*tid = transaction->t_tid;
85179feb521SJan Kara 		*block = transaction->t_log_start;
85279feb521SJan Kara 	} else if ((transaction = journal->j_committing_transaction) != NULL) {
85379feb521SJan Kara 		*tid = transaction->t_tid;
85479feb521SJan Kara 		*block = transaction->t_log_start;
85579feb521SJan Kara 	} else if ((transaction = journal->j_running_transaction) != NULL) {
85679feb521SJan Kara 		*tid = transaction->t_tid;
85779feb521SJan Kara 		*block = journal->j_head;
85879feb521SJan Kara 	} else {
85979feb521SJan Kara 		*tid = journal->j_transaction_sequence;
86079feb521SJan Kara 		*block = journal->j_head;
86179feb521SJan Kara 	}
86279feb521SJan Kara 	ret = tid_gt(*tid, journal->j_tail_sequence);
86379feb521SJan Kara 	spin_unlock(&journal->j_list_lock);
86479feb521SJan Kara 	read_unlock(&journal->j_state_lock);
86579feb521SJan Kara 
86679feb521SJan Kara 	return ret;
86779feb521SJan Kara }
86879feb521SJan Kara 
86979feb521SJan Kara /*
87079feb521SJan Kara  * Update information in journal structure and in on disk journal superblock
87179feb521SJan Kara  * about log tail. This function does not check whether information passed in
87279feb521SJan Kara  * really pushes log tail further. It's responsibility of the caller to make
87379feb521SJan Kara  * sure provided log tail information is valid (e.g. by holding
87479feb521SJan Kara  * j_checkpoint_mutex all the time between computing log tail and calling this
87579feb521SJan Kara  * function as is the case with jbd2_cleanup_journal_tail()).
87679feb521SJan Kara  *
87779feb521SJan Kara  * Requires j_checkpoint_mutex
87879feb521SJan Kara  */
879*6f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
88079feb521SJan Kara {
88179feb521SJan Kara 	unsigned long freed;
882*6f6a6fdaSJoseph Qi 	int ret;
88379feb521SJan Kara 
88479feb521SJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
88579feb521SJan Kara 
88679feb521SJan Kara 	/*
88779feb521SJan Kara 	 * We cannot afford for write to remain in drive's caches since as
88879feb521SJan Kara 	 * soon as we update j_tail, next transaction can start reusing journal
88979feb521SJan Kara 	 * space and if we lose sb update during power failure we'd replay
89079feb521SJan Kara 	 * old transaction with possibly newly overwritten data.
89179feb521SJan Kara 	 */
892*6f6a6fdaSJoseph Qi 	ret = jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
893*6f6a6fdaSJoseph Qi 	if (ret)
894*6f6a6fdaSJoseph Qi 		goto out;
895*6f6a6fdaSJoseph Qi 
89679feb521SJan Kara 	write_lock(&journal->j_state_lock);
89779feb521SJan Kara 	freed = block - journal->j_tail;
89879feb521SJan Kara 	if (block < journal->j_tail)
89979feb521SJan Kara 		freed += journal->j_last - journal->j_first;
90079feb521SJan Kara 
90179feb521SJan Kara 	trace_jbd2_update_log_tail(journal, tid, block, freed);
90279feb521SJan Kara 	jbd_debug(1,
90379feb521SJan Kara 		  "Cleaning journal tail from %d to %d (offset %lu), "
90479feb521SJan Kara 		  "freeing %lu\n",
90579feb521SJan Kara 		  journal->j_tail_sequence, tid, block, freed);
90679feb521SJan Kara 
90779feb521SJan Kara 	journal->j_free += freed;
90879feb521SJan Kara 	journal->j_tail_sequence = tid;
90979feb521SJan Kara 	journal->j_tail = block;
91079feb521SJan Kara 	write_unlock(&journal->j_state_lock);
911*6f6a6fdaSJoseph Qi 
912*6f6a6fdaSJoseph Qi out:
913*6f6a6fdaSJoseph Qi 	return ret;
91479feb521SJan Kara }
91579feb521SJan Kara 
9163339578fSJan Kara /*
9173339578fSJan Kara  * This is a variaon of __jbd2_update_log_tail which checks for validity of
9183339578fSJan Kara  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
9193339578fSJan Kara  * with other threads updating log tail.
9203339578fSJan Kara  */
9213339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
9223339578fSJan Kara {
9233339578fSJan Kara 	mutex_lock(&journal->j_checkpoint_mutex);
9243339578fSJan Kara 	if (tid_gt(tid, journal->j_tail_sequence))
9253339578fSJan Kara 		__jbd2_update_log_tail(journal, tid, block);
9263339578fSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
9273339578fSJan Kara }
9283339578fSJan Kara 
9298e85fb3fSJohann Lombardi struct jbd2_stats_proc_session {
9308e85fb3fSJohann Lombardi 	journal_t *journal;
9318e85fb3fSJohann Lombardi 	struct transaction_stats_s *stats;
9328e85fb3fSJohann Lombardi 	int start;
9338e85fb3fSJohann Lombardi 	int max;
9348e85fb3fSJohann Lombardi };
9358e85fb3fSJohann Lombardi 
9368e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
9378e85fb3fSJohann Lombardi {
9388e85fb3fSJohann Lombardi 	return *pos ? NULL : SEQ_START_TOKEN;
9398e85fb3fSJohann Lombardi }
9408e85fb3fSJohann Lombardi 
9418e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
9428e85fb3fSJohann Lombardi {
9438e85fb3fSJohann Lombardi 	return NULL;
9448e85fb3fSJohann Lombardi }
9458e85fb3fSJohann Lombardi 
9468e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v)
9478e85fb3fSJohann Lombardi {
9488e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
9498e85fb3fSJohann Lombardi 
9508e85fb3fSJohann Lombardi 	if (v != SEQ_START_TOKEN)
9518e85fb3fSJohann Lombardi 		return 0;
9529fff24aaSTheodore Ts'o 	seq_printf(seq, "%lu transactions (%lu requested), "
9539fff24aaSTheodore Ts'o 		   "each up to %u blocks\n",
9549fff24aaSTheodore Ts'o 		   s->stats->ts_tid, s->stats->ts_requested,
9558e85fb3fSJohann Lombardi 		   s->journal->j_max_transaction_buffers);
9568e85fb3fSJohann Lombardi 	if (s->stats->ts_tid == 0)
9578e85fb3fSJohann Lombardi 		return 0;
9588e85fb3fSJohann Lombardi 	seq_printf(seq, "average: \n  %ums waiting for transaction\n",
959bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
9609fff24aaSTheodore Ts'o 	seq_printf(seq, "  %ums request delay\n",
9619fff24aaSTheodore Ts'o 	    (s->stats->ts_requested == 0) ? 0 :
9629fff24aaSTheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_request_delay /
9639fff24aaSTheodore Ts'o 			     s->stats->ts_requested));
9648e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums running transaction\n",
965bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
9668e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums transaction was being locked\n",
967bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
9688e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
969bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
9708e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums logging transaction\n",
971bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
972c225aa57SSimon Holm Thøgersen 	seq_printf(seq, "  %lluus average transaction commit time\n",
973c225aa57SSimon Holm Thøgersen 		   div_u64(s->journal->j_average_commit_time, 1000));
9748e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu handles per transaction\n",
975bf699327STheodore Ts'o 	    s->stats->run.rs_handle_count / s->stats->ts_tid);
9768e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu blocks per transaction\n",
977bf699327STheodore Ts'o 	    s->stats->run.rs_blocks / s->stats->ts_tid);
9788e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu logged blocks per transaction\n",
979bf699327STheodore Ts'o 	    s->stats->run.rs_blocks_logged / s->stats->ts_tid);
9808e85fb3fSJohann Lombardi 	return 0;
9818e85fb3fSJohann Lombardi }
9828e85fb3fSJohann Lombardi 
9838e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
9848e85fb3fSJohann Lombardi {
9858e85fb3fSJohann Lombardi }
9868e85fb3fSJohann Lombardi 
98788e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = {
9888e85fb3fSJohann Lombardi 	.start  = jbd2_seq_info_start,
9898e85fb3fSJohann Lombardi 	.next   = jbd2_seq_info_next,
9908e85fb3fSJohann Lombardi 	.stop   = jbd2_seq_info_stop,
9918e85fb3fSJohann Lombardi 	.show   = jbd2_seq_info_show,
9928e85fb3fSJohann Lombardi };
9938e85fb3fSJohann Lombardi 
9948e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file)
9958e85fb3fSJohann Lombardi {
996d9dda78bSAl Viro 	journal_t *journal = PDE_DATA(inode);
9978e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s;
9988e85fb3fSJohann Lombardi 	int rc, size;
9998e85fb3fSJohann Lombardi 
10008e85fb3fSJohann Lombardi 	s = kmalloc(sizeof(*s), GFP_KERNEL);
10018e85fb3fSJohann Lombardi 	if (s == NULL)
10028e85fb3fSJohann Lombardi 		return -ENOMEM;
10038e85fb3fSJohann Lombardi 	size = sizeof(struct transaction_stats_s);
10048e85fb3fSJohann Lombardi 	s->stats = kmalloc(size, GFP_KERNEL);
10058e85fb3fSJohann Lombardi 	if (s->stats == NULL) {
10068e85fb3fSJohann Lombardi 		kfree(s);
10078e85fb3fSJohann Lombardi 		return -ENOMEM;
10088e85fb3fSJohann Lombardi 	}
10098e85fb3fSJohann Lombardi 	spin_lock(&journal->j_history_lock);
10108e85fb3fSJohann Lombardi 	memcpy(s->stats, &journal->j_stats, size);
10118e85fb3fSJohann Lombardi 	s->journal = journal;
10128e85fb3fSJohann Lombardi 	spin_unlock(&journal->j_history_lock);
10138e85fb3fSJohann Lombardi 
10148e85fb3fSJohann Lombardi 	rc = seq_open(file, &jbd2_seq_info_ops);
10158e85fb3fSJohann Lombardi 	if (rc == 0) {
10168e85fb3fSJohann Lombardi 		struct seq_file *m = file->private_data;
10178e85fb3fSJohann Lombardi 		m->private = s;
10188e85fb3fSJohann Lombardi 	} else {
10198e85fb3fSJohann Lombardi 		kfree(s->stats);
10208e85fb3fSJohann Lombardi 		kfree(s);
10218e85fb3fSJohann Lombardi 	}
10228e85fb3fSJohann Lombardi 	return rc;
10238e85fb3fSJohann Lombardi 
10248e85fb3fSJohann Lombardi }
10258e85fb3fSJohann Lombardi 
10268e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file)
10278e85fb3fSJohann Lombardi {
10288e85fb3fSJohann Lombardi 	struct seq_file *seq = file->private_data;
10298e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
10308e85fb3fSJohann Lombardi 	kfree(s->stats);
10318e85fb3fSJohann Lombardi 	kfree(s);
10328e85fb3fSJohann Lombardi 	return seq_release(inode, file);
10338e85fb3fSJohann Lombardi }
10348e85fb3fSJohann Lombardi 
1035828c0950SAlexey Dobriyan static const struct file_operations jbd2_seq_info_fops = {
10368e85fb3fSJohann Lombardi 	.owner		= THIS_MODULE,
10378e85fb3fSJohann Lombardi 	.open           = jbd2_seq_info_open,
10388e85fb3fSJohann Lombardi 	.read           = seq_read,
10398e85fb3fSJohann Lombardi 	.llseek         = seq_lseek,
10408e85fb3fSJohann Lombardi 	.release        = jbd2_seq_info_release,
10418e85fb3fSJohann Lombardi };
10428e85fb3fSJohann Lombardi 
10438e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats;
10448e85fb3fSJohann Lombardi 
10458e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal)
10468e85fb3fSJohann Lombardi {
104705496769STheodore Ts'o 	journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
10488e85fb3fSJohann Lombardi 	if (journal->j_proc_entry) {
104979da3664SDenis V. Lunev 		proc_create_data("info", S_IRUGO, journal->j_proc_entry,
105079da3664SDenis V. Lunev 				 &jbd2_seq_info_fops, journal);
10518e85fb3fSJohann Lombardi 	}
10528e85fb3fSJohann Lombardi }
10538e85fb3fSJohann Lombardi 
10548e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal)
10558e85fb3fSJohann Lombardi {
10568e85fb3fSJohann Lombardi 	remove_proc_entry("info", journal->j_proc_entry);
105705496769STheodore Ts'o 	remove_proc_entry(journal->j_devname, proc_jbd2_stats);
10588e85fb3fSJohann Lombardi }
10598e85fb3fSJohann Lombardi 
1060470decc6SDave Kleikamp /*
1061470decc6SDave Kleikamp  * Management for journal control blocks: functions to create and
1062470decc6SDave Kleikamp  * destroy journal_t structures, and to initialise and read existing
1063470decc6SDave Kleikamp  * journal blocks from disk.  */
1064470decc6SDave Kleikamp 
1065470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory.  We initialise
1066470decc6SDave Kleikamp  * very few fields yet: that has to wait until we have created the
1067470decc6SDave Kleikamp  * journal structures from from scratch, or loaded them from disk. */
1068470decc6SDave Kleikamp 
1069470decc6SDave Kleikamp static journal_t * journal_init_common (void)
1070470decc6SDave Kleikamp {
1071470decc6SDave Kleikamp 	journal_t *journal;
1072470decc6SDave Kleikamp 	int err;
1073470decc6SDave Kleikamp 
10743ebfdf88SAndrew Morton 	journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1075470decc6SDave Kleikamp 	if (!journal)
1076b7271b0aSTheodore Ts'o 		return NULL;
1077470decc6SDave Kleikamp 
1078470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_transaction_locked);
1079470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_done_commit);
1080470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_commit);
1081470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_updates);
10828f7d89f3SJan Kara 	init_waitqueue_head(&journal->j_wait_reserved);
1083470decc6SDave Kleikamp 	mutex_init(&journal->j_barrier);
1084470decc6SDave Kleikamp 	mutex_init(&journal->j_checkpoint_mutex);
1085470decc6SDave Kleikamp 	spin_lock_init(&journal->j_revoke_lock);
1086470decc6SDave Kleikamp 	spin_lock_init(&journal->j_list_lock);
1087a931da6aSTheodore Ts'o 	rwlock_init(&journal->j_state_lock);
1088470decc6SDave Kleikamp 
1089cd02ff0bSMingming Cao 	journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
109030773840STheodore Ts'o 	journal->j_min_batch_time = 0;
109130773840STheodore Ts'o 	journal->j_max_batch_time = 15000; /* 15ms */
10928f7d89f3SJan Kara 	atomic_set(&journal->j_reserved_credits, 0);
1093470decc6SDave Kleikamp 
1094470decc6SDave Kleikamp 	/* The journal is marked for error until we succeed with recovery! */
1095f7f4bccbSMingming Cao 	journal->j_flags = JBD2_ABORT;
1096470decc6SDave Kleikamp 
1097470decc6SDave Kleikamp 	/* Set up a default-sized revoke table for the new mount. */
1098f7f4bccbSMingming Cao 	err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1099470decc6SDave Kleikamp 	if (err) {
1100470decc6SDave Kleikamp 		kfree(journal);
1101b7271b0aSTheodore Ts'o 		return NULL;
1102470decc6SDave Kleikamp 	}
11038e85fb3fSJohann Lombardi 
1104bf699327STheodore Ts'o 	spin_lock_init(&journal->j_history_lock);
11058e85fb3fSJohann Lombardi 
1106470decc6SDave Kleikamp 	return journal;
1107470decc6SDave Kleikamp }
1108470decc6SDave Kleikamp 
1109f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1110470decc6SDave Kleikamp  *
1111470decc6SDave Kleikamp  * Create a journal structure assigned some fixed set of disk blocks to
1112470decc6SDave Kleikamp  * the journal.  We don't actually touch those disk blocks yet, but we
1113470decc6SDave Kleikamp  * need to set up all of the mapping information to tell the journaling
1114470decc6SDave Kleikamp  * system where the journal blocks are.
1115470decc6SDave Kleikamp  *
1116470decc6SDave Kleikamp  */
1117470decc6SDave Kleikamp 
1118470decc6SDave Kleikamp /**
11195648ba5bSRandy Dunlap  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1120470decc6SDave Kleikamp  *  @bdev: Block device on which to create the journal
1121470decc6SDave Kleikamp  *  @fs_dev: Device which hold journalled filesystem for this journal.
1122470decc6SDave Kleikamp  *  @start: Block nr Start of journal.
1123470decc6SDave Kleikamp  *  @len:  Length of the journal in blocks.
1124470decc6SDave Kleikamp  *  @blocksize: blocksize of journalling device
11255648ba5bSRandy Dunlap  *
11265648ba5bSRandy Dunlap  *  Returns: a newly created journal_t *
1127470decc6SDave Kleikamp  *
1128f7f4bccbSMingming Cao  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1129470decc6SDave Kleikamp  *  range of blocks on an arbitrary block device.
1130470decc6SDave Kleikamp  *
1131470decc6SDave Kleikamp  */
1132f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1133470decc6SDave Kleikamp 			struct block_device *fs_dev,
113418eba7aaSMingming Cao 			unsigned long long start, int len, int blocksize)
1135470decc6SDave Kleikamp {
1136470decc6SDave Kleikamp 	journal_t *journal = journal_init_common();
1137470decc6SDave Kleikamp 	struct buffer_head *bh;
113805496769STheodore Ts'o 	char *p;
1139470decc6SDave Kleikamp 	int n;
1140470decc6SDave Kleikamp 
1141470decc6SDave Kleikamp 	if (!journal)
1142470decc6SDave Kleikamp 		return NULL;
1143470decc6SDave Kleikamp 
1144470decc6SDave Kleikamp 	/* journal descriptor can store up to n blocks -bzzz */
1145470decc6SDave Kleikamp 	journal->j_blocksize = blocksize;
11460587aa3dSyangsheng 	journal->j_dev = bdev;
11470587aa3dSyangsheng 	journal->j_fs_dev = fs_dev;
11480587aa3dSyangsheng 	journal->j_blk_offset = start;
11490587aa3dSyangsheng 	journal->j_maxlen = len;
11500587aa3dSyangsheng 	bdevname(journal->j_dev, journal->j_devname);
11510587aa3dSyangsheng 	p = journal->j_devname;
11520587aa3dSyangsheng 	while ((p = strchr(p, '/')))
11530587aa3dSyangsheng 		*p = '!';
11544b905671SJan Kara 	jbd2_stats_proc_init(journal);
1155470decc6SDave Kleikamp 	n = journal->j_blocksize / sizeof(journal_block_tag_t);
1156470decc6SDave Kleikamp 	journal->j_wbufsize = n;
1157470decc6SDave Kleikamp 	journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1158470decc6SDave Kleikamp 	if (!journal->j_wbuf) {
115925985edcSLucas De Marchi 		printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1160329d291fSHarvey Harrison 			__func__);
11614b905671SJan Kara 		goto out_err;
1162470decc6SDave Kleikamp 	}
1163470decc6SDave Kleikamp 
1164470decc6SDave Kleikamp 	bh = __getblk(journal->j_dev, start, journal->j_blocksize);
11654b905671SJan Kara 	if (!bh) {
11664b905671SJan Kara 		printk(KERN_ERR
11674b905671SJan Kara 		       "%s: Cannot get buffer for journal superblock\n",
11684b905671SJan Kara 		       __func__);
11694b905671SJan Kara 		goto out_err;
11704b905671SJan Kara 	}
1171470decc6SDave Kleikamp 	journal->j_sb_buffer = bh;
1172470decc6SDave Kleikamp 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
11734b905671SJan Kara 
1174470decc6SDave Kleikamp 	return journal;
11754b905671SJan Kara out_err:
11767b02bec0STao Ma 	kfree(journal->j_wbuf);
11774b905671SJan Kara 	jbd2_stats_proc_exit(journal);
11784b905671SJan Kara 	kfree(journal);
11794b905671SJan Kara 	return NULL;
1180470decc6SDave Kleikamp }
1181470decc6SDave Kleikamp 
1182470decc6SDave Kleikamp /**
1183f7f4bccbSMingming Cao  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1184470decc6SDave Kleikamp  *  @inode: An inode to create the journal in
1185470decc6SDave Kleikamp  *
1186f7f4bccbSMingming Cao  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1187470decc6SDave Kleikamp  * the journal.  The inode must exist already, must support bmap() and
1188470decc6SDave Kleikamp  * must have all data blocks preallocated.
1189470decc6SDave Kleikamp  */
1190f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode)
1191470decc6SDave Kleikamp {
1192470decc6SDave Kleikamp 	struct buffer_head *bh;
1193470decc6SDave Kleikamp 	journal_t *journal = journal_init_common();
119405496769STheodore Ts'o 	char *p;
1195470decc6SDave Kleikamp 	int err;
1196470decc6SDave Kleikamp 	int n;
119718eba7aaSMingming Cao 	unsigned long long blocknr;
1198470decc6SDave Kleikamp 
1199470decc6SDave Kleikamp 	if (!journal)
1200470decc6SDave Kleikamp 		return NULL;
1201470decc6SDave Kleikamp 
1202470decc6SDave Kleikamp 	journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1203470decc6SDave Kleikamp 	journal->j_inode = inode;
120405496769STheodore Ts'o 	bdevname(journal->j_dev, journal->j_devname);
120505496769STheodore Ts'o 	p = journal->j_devname;
120605496769STheodore Ts'o 	while ((p = strchr(p, '/')))
120705496769STheodore Ts'o 		*p = '!';
120805496769STheodore Ts'o 	p = journal->j_devname + strlen(journal->j_devname);
120990576c0bSTheodore Ts'o 	sprintf(p, "-%lu", journal->j_inode->i_ino);
1210470decc6SDave Kleikamp 	jbd_debug(1,
1211470decc6SDave Kleikamp 		  "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1212470decc6SDave Kleikamp 		  journal, inode->i_sb->s_id, inode->i_ino,
1213470decc6SDave Kleikamp 		  (long long) inode->i_size,
1214470decc6SDave Kleikamp 		  inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1215470decc6SDave Kleikamp 
1216470decc6SDave Kleikamp 	journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1217470decc6SDave Kleikamp 	journal->j_blocksize = inode->i_sb->s_blocksize;
12188e85fb3fSJohann Lombardi 	jbd2_stats_proc_init(journal);
1219470decc6SDave Kleikamp 
1220470decc6SDave Kleikamp 	/* journal descriptor can store up to n blocks -bzzz */
1221470decc6SDave Kleikamp 	n = journal->j_blocksize / sizeof(journal_block_tag_t);
1222470decc6SDave Kleikamp 	journal->j_wbufsize = n;
1223470decc6SDave Kleikamp 	journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1224470decc6SDave Kleikamp 	if (!journal->j_wbuf) {
122525985edcSLucas De Marchi 		printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1226329d291fSHarvey Harrison 			__func__);
12274b905671SJan Kara 		goto out_err;
1228470decc6SDave Kleikamp 	}
1229470decc6SDave Kleikamp 
1230f7f4bccbSMingming Cao 	err = jbd2_journal_bmap(journal, 0, &blocknr);
1231470decc6SDave Kleikamp 	/* If that failed, give up */
1232470decc6SDave Kleikamp 	if (err) {
12333c26bdb4SJustin P. Mattock 		printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1234329d291fSHarvey Harrison 		       __func__);
12354b905671SJan Kara 		goto out_err;
1236470decc6SDave Kleikamp 	}
1237470decc6SDave Kleikamp 
1238a49058faSGioh Kim 	bh = getblk_unmovable(journal->j_dev, blocknr, journal->j_blocksize);
12394b905671SJan Kara 	if (!bh) {
12404b905671SJan Kara 		printk(KERN_ERR
12414b905671SJan Kara 		       "%s: Cannot get buffer for journal superblock\n",
12424b905671SJan Kara 		       __func__);
12434b905671SJan Kara 		goto out_err;
12444b905671SJan Kara 	}
1245470decc6SDave Kleikamp 	journal->j_sb_buffer = bh;
1246470decc6SDave Kleikamp 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
1247470decc6SDave Kleikamp 
1248470decc6SDave Kleikamp 	return journal;
12494b905671SJan Kara out_err:
12507b02bec0STao Ma 	kfree(journal->j_wbuf);
12514b905671SJan Kara 	jbd2_stats_proc_exit(journal);
12524b905671SJan Kara 	kfree(journal);
12534b905671SJan Kara 	return NULL;
1254470decc6SDave Kleikamp }
1255470decc6SDave Kleikamp 
1256470decc6SDave Kleikamp /*
1257470decc6SDave Kleikamp  * If the journal init or create aborts, we need to mark the journal
1258470decc6SDave Kleikamp  * superblock as being NULL to prevent the journal destroy from writing
1259470decc6SDave Kleikamp  * back a bogus superblock.
1260470decc6SDave Kleikamp  */
1261470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal)
1262470decc6SDave Kleikamp {
1263470decc6SDave Kleikamp 	struct buffer_head *bh = journal->j_sb_buffer;
1264470decc6SDave Kleikamp 	brelse(bh);
1265470decc6SDave Kleikamp 	journal->j_sb_buffer = NULL;
1266470decc6SDave Kleikamp }
1267470decc6SDave Kleikamp 
1268470decc6SDave Kleikamp /*
1269470decc6SDave Kleikamp  * Given a journal_t structure, initialise the various fields for
1270470decc6SDave Kleikamp  * startup of a new journaling session.  We use this both when creating
1271470decc6SDave Kleikamp  * a journal, and after recovering an old journal to reset it for
1272470decc6SDave Kleikamp  * subsequent use.
1273470decc6SDave Kleikamp  */
1274470decc6SDave Kleikamp 
1275470decc6SDave Kleikamp static int journal_reset(journal_t *journal)
1276470decc6SDave Kleikamp {
1277470decc6SDave Kleikamp 	journal_superblock_t *sb = journal->j_superblock;
127818eba7aaSMingming Cao 	unsigned long long first, last;
1279470decc6SDave Kleikamp 
1280470decc6SDave Kleikamp 	first = be32_to_cpu(sb->s_first);
1281470decc6SDave Kleikamp 	last = be32_to_cpu(sb->s_maxlen);
1282f6f50e28SJan Kara 	if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1283f2a44523SEryu Guan 		printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1284f6f50e28SJan Kara 		       first, last);
1285f6f50e28SJan Kara 		journal_fail_superblock(journal);
1286f6f50e28SJan Kara 		return -EINVAL;
1287f6f50e28SJan Kara 	}
1288470decc6SDave Kleikamp 
1289470decc6SDave Kleikamp 	journal->j_first = first;
1290470decc6SDave Kleikamp 	journal->j_last = last;
1291470decc6SDave Kleikamp 
1292470decc6SDave Kleikamp 	journal->j_head = first;
1293470decc6SDave Kleikamp 	journal->j_tail = first;
1294470decc6SDave Kleikamp 	journal->j_free = last - first;
1295470decc6SDave Kleikamp 
1296470decc6SDave Kleikamp 	journal->j_tail_sequence = journal->j_transaction_sequence;
1297470decc6SDave Kleikamp 	journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1298470decc6SDave Kleikamp 	journal->j_commit_request = journal->j_commit_sequence;
1299470decc6SDave Kleikamp 
1300470decc6SDave Kleikamp 	journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1301470decc6SDave Kleikamp 
1302470decc6SDave Kleikamp 	/*
1303470decc6SDave Kleikamp 	 * As a special case, if the on-disk copy is already marked as needing
130424bcc89cSJan Kara 	 * no recovery (s_start == 0), then we can safely defer the superblock
130524bcc89cSJan Kara 	 * update until the next commit by setting JBD2_FLUSHED.  This avoids
1306470decc6SDave Kleikamp 	 * attempting a write to a potential-readonly device.
1307470decc6SDave Kleikamp 	 */
130824bcc89cSJan Kara 	if (sb->s_start == 0) {
1309f2a44523SEryu Guan 		jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1310470decc6SDave Kleikamp 			"(start %ld, seq %d, errno %d)\n",
1311470decc6SDave Kleikamp 			journal->j_tail, journal->j_tail_sequence,
1312470decc6SDave Kleikamp 			journal->j_errno);
131324bcc89cSJan Kara 		journal->j_flags |= JBD2_FLUSHED;
131424bcc89cSJan Kara 	} else {
1315a78bb11dSJan Kara 		/* Lock here to make assertions happy... */
1316a78bb11dSJan Kara 		mutex_lock(&journal->j_checkpoint_mutex);
131779feb521SJan Kara 		/*
131879feb521SJan Kara 		 * Update log tail information. We use WRITE_FUA since new
131979feb521SJan Kara 		 * transaction will start reusing journal space and so we
132079feb521SJan Kara 		 * must make sure information about current log tail is on
132179feb521SJan Kara 		 * disk before that.
132279feb521SJan Kara 		 */
132379feb521SJan Kara 		jbd2_journal_update_sb_log_tail(journal,
132479feb521SJan Kara 						journal->j_tail_sequence,
132579feb521SJan Kara 						journal->j_tail,
132679feb521SJan Kara 						WRITE_FUA);
1327a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
132824bcc89cSJan Kara 	}
132924bcc89cSJan Kara 	return jbd2_journal_start_thread(journal);
1330470decc6SDave Kleikamp }
1331470decc6SDave Kleikamp 
1332*6f6a6fdaSJoseph Qi static int jbd2_write_superblock(journal_t *journal, int write_op)
133324bcc89cSJan Kara {
133424bcc89cSJan Kara 	struct buffer_head *bh = journal->j_sb_buffer;
1335fe52d17cSTheodore Ts'o 	journal_superblock_t *sb = journal->j_superblock;
133679feb521SJan Kara 	int ret;
133724bcc89cSJan Kara 
133879feb521SJan Kara 	trace_jbd2_write_superblock(journal, write_op);
133979feb521SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
134079feb521SJan Kara 		write_op &= ~(REQ_FUA | REQ_FLUSH);
134179feb521SJan Kara 	lock_buffer(bh);
1342914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1343914258bfSTheodore Ts'o 		/*
1344914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the journal
1345914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
1346914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
1347914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
1348914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
1349914258bfSTheodore Ts'o 		 * write and hope for the best.
1350914258bfSTheodore Ts'o 		 */
1351914258bfSTheodore Ts'o 		printk(KERN_ERR "JBD2: previous I/O error detected "
1352914258bfSTheodore Ts'o 		       "for journal superblock update for %s.\n",
1353914258bfSTheodore Ts'o 		       journal->j_devname);
1354914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1355914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
1356914258bfSTheodore Ts'o 	}
1357fe52d17cSTheodore Ts'o 	jbd2_superblock_csum_set(journal, sb);
135879feb521SJan Kara 	get_bh(bh);
135979feb521SJan Kara 	bh->b_end_io = end_buffer_write_sync;
136079feb521SJan Kara 	ret = submit_bh(write_op, bh);
136179feb521SJan Kara 	wait_on_buffer(bh);
1362914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1363914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1364914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
136579feb521SJan Kara 		ret = -EIO;
136679feb521SJan Kara 	}
136779feb521SJan Kara 	if (ret) {
136879feb521SJan Kara 		printk(KERN_ERR "JBD2: Error %d detected when updating "
136979feb521SJan Kara 		       "journal superblock for %s.\n", ret,
137079feb521SJan Kara 		       journal->j_devname);
1371*6f6a6fdaSJoseph Qi 		jbd2_journal_abort(journal, ret);
1372914258bfSTheodore Ts'o 	}
1373*6f6a6fdaSJoseph Qi 
1374*6f6a6fdaSJoseph Qi 	return ret;
137524bcc89cSJan Kara }
1376470decc6SDave Kleikamp 
137724bcc89cSJan Kara /**
137824bcc89cSJan Kara  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
137924bcc89cSJan Kara  * @journal: The journal to update.
138079feb521SJan Kara  * @tail_tid: TID of the new transaction at the tail of the log
138179feb521SJan Kara  * @tail_block: The first block of the transaction at the tail of the log
138279feb521SJan Kara  * @write_op: With which operation should we write the journal sb
138324bcc89cSJan Kara  *
138424bcc89cSJan Kara  * Update a journal's superblock information about log tail and write it to
138524bcc89cSJan Kara  * disk, waiting for the IO to complete.
138624bcc89cSJan Kara  */
1387*6f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
138879feb521SJan Kara 				     unsigned long tail_block, int write_op)
138924bcc89cSJan Kara {
139024bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
1391*6f6a6fdaSJoseph Qi 	int ret;
13922201c590SSeiji Aguchi 
1393a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
139479feb521SJan Kara 	jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
139579feb521SJan Kara 		  tail_block, tail_tid);
1396470decc6SDave Kleikamp 
139779feb521SJan Kara 	sb->s_sequence = cpu_to_be32(tail_tid);
139879feb521SJan Kara 	sb->s_start    = cpu_to_be32(tail_block);
139924bcc89cSJan Kara 
1400*6f6a6fdaSJoseph Qi 	ret = jbd2_write_superblock(journal, write_op);
1401*6f6a6fdaSJoseph Qi 	if (ret)
1402*6f6a6fdaSJoseph Qi 		goto out;
140324bcc89cSJan Kara 
140424bcc89cSJan Kara 	/* Log is no longer empty */
1405a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
140624bcc89cSJan Kara 	WARN_ON(!sb->s_sequence);
1407f7f4bccbSMingming Cao 	journal->j_flags &= ~JBD2_FLUSHED;
140824bcc89cSJan Kara 	write_unlock(&journal->j_state_lock);
1409*6f6a6fdaSJoseph Qi 
1410*6f6a6fdaSJoseph Qi out:
1411*6f6a6fdaSJoseph Qi 	return ret;
141224bcc89cSJan Kara }
141324bcc89cSJan Kara 
141424bcc89cSJan Kara /**
141524bcc89cSJan Kara  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
141624bcc89cSJan Kara  * @journal: The journal to update.
141724bcc89cSJan Kara  *
141824bcc89cSJan Kara  * Update a journal's dynamic superblock fields to show that journal is empty.
141924bcc89cSJan Kara  * Write updated superblock to disk waiting for IO to complete.
142024bcc89cSJan Kara  */
142124bcc89cSJan Kara static void jbd2_mark_journal_empty(journal_t *journal)
142224bcc89cSJan Kara {
142324bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
142424bcc89cSJan Kara 
1425a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1426470decc6SDave Kleikamp 	read_lock(&journal->j_state_lock);
1427eeecef0aSEric Sandeen 	/* Is it already empty? */
1428eeecef0aSEric Sandeen 	if (sb->s_start == 0) {
1429eeecef0aSEric Sandeen 		read_unlock(&journal->j_state_lock);
1430eeecef0aSEric Sandeen 		return;
1431eeecef0aSEric Sandeen 	}
143224bcc89cSJan Kara 	jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
143324bcc89cSJan Kara 		  journal->j_tail_sequence);
1434470decc6SDave Kleikamp 
1435470decc6SDave Kleikamp 	sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
143624bcc89cSJan Kara 	sb->s_start    = cpu_to_be32(0);
143724bcc89cSJan Kara 	read_unlock(&journal->j_state_lock);
143824bcc89cSJan Kara 
143979feb521SJan Kara 	jbd2_write_superblock(journal, WRITE_FUA);
144024bcc89cSJan Kara 
144124bcc89cSJan Kara 	/* Log is no longer empty */
144224bcc89cSJan Kara 	write_lock(&journal->j_state_lock);
1443f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_FLUSHED;
1444a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
1445470decc6SDave Kleikamp }
1446470decc6SDave Kleikamp 
144724bcc89cSJan Kara 
144824bcc89cSJan Kara /**
144924bcc89cSJan Kara  * jbd2_journal_update_sb_errno() - Update error in the journal.
145024bcc89cSJan Kara  * @journal: The journal to update.
145124bcc89cSJan Kara  *
145224bcc89cSJan Kara  * Update a journal's errno.  Write updated superblock to disk waiting for IO
145324bcc89cSJan Kara  * to complete.
145424bcc89cSJan Kara  */
1455d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal)
145624bcc89cSJan Kara {
145724bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
145824bcc89cSJan Kara 
145924bcc89cSJan Kara 	read_lock(&journal->j_state_lock);
146024bcc89cSJan Kara 	jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
146124bcc89cSJan Kara 		  journal->j_errno);
1462470decc6SDave Kleikamp 	sb->s_errno    = cpu_to_be32(journal->j_errno);
1463470decc6SDave Kleikamp 	read_unlock(&journal->j_state_lock);
1464470decc6SDave Kleikamp 
146579feb521SJan Kara 	jbd2_write_superblock(journal, WRITE_SYNC);
1466470decc6SDave Kleikamp }
1467d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1468470decc6SDave Kleikamp 
1469470decc6SDave Kleikamp /*
1470470decc6SDave Kleikamp  * Read the superblock for a given journal, performing initial
1471470decc6SDave Kleikamp  * validation of the format.
1472470decc6SDave Kleikamp  */
1473470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal)
1474470decc6SDave Kleikamp {
1475470decc6SDave Kleikamp 	struct buffer_head *bh;
1476470decc6SDave Kleikamp 	journal_superblock_t *sb;
1477470decc6SDave Kleikamp 	int err = -EIO;
1478470decc6SDave Kleikamp 
1479470decc6SDave Kleikamp 	bh = journal->j_sb_buffer;
1480470decc6SDave Kleikamp 
1481470decc6SDave Kleikamp 	J_ASSERT(bh != NULL);
1482470decc6SDave Kleikamp 	if (!buffer_uptodate(bh)) {
1483470decc6SDave Kleikamp 		ll_rw_block(READ, 1, &bh);
1484470decc6SDave Kleikamp 		wait_on_buffer(bh);
1485470decc6SDave Kleikamp 		if (!buffer_uptodate(bh)) {
1486470decc6SDave Kleikamp 			printk(KERN_ERR
1487f2a44523SEryu Guan 				"JBD2: IO error reading journal superblock\n");
1488470decc6SDave Kleikamp 			goto out;
1489470decc6SDave Kleikamp 		}
1490470decc6SDave Kleikamp 	}
1491470decc6SDave Kleikamp 
149225ed6e8aSDarrick J. Wong 	if (buffer_verified(bh))
149325ed6e8aSDarrick J. Wong 		return 0;
149425ed6e8aSDarrick J. Wong 
1495470decc6SDave Kleikamp 	sb = journal->j_superblock;
1496470decc6SDave Kleikamp 
1497470decc6SDave Kleikamp 	err = -EINVAL;
1498470decc6SDave Kleikamp 
1499f7f4bccbSMingming Cao 	if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1500470decc6SDave Kleikamp 	    sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1501f2a44523SEryu Guan 		printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1502470decc6SDave Kleikamp 		goto out;
1503470decc6SDave Kleikamp 	}
1504470decc6SDave Kleikamp 
1505470decc6SDave Kleikamp 	switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1506f7f4bccbSMingming Cao 	case JBD2_SUPERBLOCK_V1:
1507470decc6SDave Kleikamp 		journal->j_format_version = 1;
1508470decc6SDave Kleikamp 		break;
1509f7f4bccbSMingming Cao 	case JBD2_SUPERBLOCK_V2:
1510470decc6SDave Kleikamp 		journal->j_format_version = 2;
1511470decc6SDave Kleikamp 		break;
1512470decc6SDave Kleikamp 	default:
1513f2a44523SEryu Guan 		printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1514470decc6SDave Kleikamp 		goto out;
1515470decc6SDave Kleikamp 	}
1516470decc6SDave Kleikamp 
1517470decc6SDave Kleikamp 	if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1518470decc6SDave Kleikamp 		journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1519470decc6SDave Kleikamp 	else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1520f2a44523SEryu Guan 		printk(KERN_WARNING "JBD2: journal file too short\n");
1521470decc6SDave Kleikamp 		goto out;
1522470decc6SDave Kleikamp 	}
1523470decc6SDave Kleikamp 
15248762202dSEryu Guan 	if (be32_to_cpu(sb->s_first) == 0 ||
15258762202dSEryu Guan 	    be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
15268762202dSEryu Guan 		printk(KERN_WARNING
15278762202dSEryu Guan 			"JBD2: Invalid start block of journal: %u\n",
15288762202dSEryu Guan 			be32_to_cpu(sb->s_first));
15298762202dSEryu Guan 		goto out;
15308762202dSEryu Guan 	}
15318762202dSEryu Guan 
1532db9ee220SDarrick J. Wong 	if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2) &&
1533db9ee220SDarrick J. Wong 	    JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1534db9ee220SDarrick J. Wong 		/* Can't have checksum v2 and v3 at the same time! */
1535db9ee220SDarrick J. Wong 		printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1536db9ee220SDarrick J. Wong 		       "at the same time!\n");
1537db9ee220SDarrick J. Wong 		goto out;
1538db9ee220SDarrick J. Wong 	}
1539db9ee220SDarrick J. Wong 
1540feb8c6d3SDarrick J. Wong 	if (jbd2_journal_has_csum_v2or3(journal) &&
1541feb8c6d3SDarrick J. Wong 	    JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM)) {
1542feb8c6d3SDarrick J. Wong 		/* Can't have checksum v1 and v2 on at the same time! */
1543feb8c6d3SDarrick J. Wong 		printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
1544feb8c6d3SDarrick J. Wong 		       "at the same time!\n");
1545feb8c6d3SDarrick J. Wong 		goto out;
1546feb8c6d3SDarrick J. Wong 	}
1547feb8c6d3SDarrick J. Wong 
154825ed6e8aSDarrick J. Wong 	if (!jbd2_verify_csum_type(journal, sb)) {
1549a67c848aSDmitry Monakhov 		printk(KERN_ERR "JBD2: Unknown checksum type\n");
155025ed6e8aSDarrick J. Wong 		goto out;
155125ed6e8aSDarrick J. Wong 	}
155225ed6e8aSDarrick J. Wong 
155301b5adceSDarrick J. Wong 	/* Load the checksum driver */
1554db9ee220SDarrick J. Wong 	if (jbd2_journal_has_csum_v2or3(journal)) {
155501b5adceSDarrick J. Wong 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
155601b5adceSDarrick J. Wong 		if (IS_ERR(journal->j_chksum_driver)) {
1557a67c848aSDmitry Monakhov 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
155801b5adceSDarrick J. Wong 			err = PTR_ERR(journal->j_chksum_driver);
155901b5adceSDarrick J. Wong 			journal->j_chksum_driver = NULL;
156001b5adceSDarrick J. Wong 			goto out;
156101b5adceSDarrick J. Wong 		}
156201b5adceSDarrick J. Wong 	}
156301b5adceSDarrick J. Wong 
15644fd5ea43SDarrick J. Wong 	/* Check superblock checksum */
15654fd5ea43SDarrick J. Wong 	if (!jbd2_superblock_csum_verify(journal, sb)) {
1566a67c848aSDmitry Monakhov 		printk(KERN_ERR "JBD2: journal checksum error\n");
15674fd5ea43SDarrick J. Wong 		goto out;
15684fd5ea43SDarrick J. Wong 	}
15694fd5ea43SDarrick J. Wong 
15704fd5ea43SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
1571db9ee220SDarrick J. Wong 	if (jbd2_journal_has_csum_v2or3(journal))
15724fd5ea43SDarrick J. Wong 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
15734fd5ea43SDarrick J. Wong 						   sizeof(sb->s_uuid));
15744fd5ea43SDarrick J. Wong 
157525ed6e8aSDarrick J. Wong 	set_buffer_verified(bh);
157625ed6e8aSDarrick J. Wong 
1577470decc6SDave Kleikamp 	return 0;
1578470decc6SDave Kleikamp 
1579470decc6SDave Kleikamp out:
1580470decc6SDave Kleikamp 	journal_fail_superblock(journal);
1581470decc6SDave Kleikamp 	return err;
1582470decc6SDave Kleikamp }
1583470decc6SDave Kleikamp 
1584470decc6SDave Kleikamp /*
1585470decc6SDave Kleikamp  * Load the on-disk journal superblock and read the key fields into the
1586470decc6SDave Kleikamp  * journal_t.
1587470decc6SDave Kleikamp  */
1588470decc6SDave Kleikamp 
1589470decc6SDave Kleikamp static int load_superblock(journal_t *journal)
1590470decc6SDave Kleikamp {
1591470decc6SDave Kleikamp 	int err;
1592470decc6SDave Kleikamp 	journal_superblock_t *sb;
1593470decc6SDave Kleikamp 
1594470decc6SDave Kleikamp 	err = journal_get_superblock(journal);
1595470decc6SDave Kleikamp 	if (err)
1596470decc6SDave Kleikamp 		return err;
1597470decc6SDave Kleikamp 
1598470decc6SDave Kleikamp 	sb = journal->j_superblock;
1599470decc6SDave Kleikamp 
1600470decc6SDave Kleikamp 	journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1601470decc6SDave Kleikamp 	journal->j_tail = be32_to_cpu(sb->s_start);
1602470decc6SDave Kleikamp 	journal->j_first = be32_to_cpu(sb->s_first);
1603470decc6SDave Kleikamp 	journal->j_last = be32_to_cpu(sb->s_maxlen);
1604470decc6SDave Kleikamp 	journal->j_errno = be32_to_cpu(sb->s_errno);
1605470decc6SDave Kleikamp 
1606470decc6SDave Kleikamp 	return 0;
1607470decc6SDave Kleikamp }
1608470decc6SDave Kleikamp 
1609470decc6SDave Kleikamp 
1610470decc6SDave Kleikamp /**
1611f7f4bccbSMingming Cao  * int jbd2_journal_load() - Read journal from disk.
1612470decc6SDave Kleikamp  * @journal: Journal to act on.
1613470decc6SDave Kleikamp  *
1614470decc6SDave Kleikamp  * Given a journal_t structure which tells us which disk blocks contain
1615470decc6SDave Kleikamp  * a journal, read the journal from disk to initialise the in-memory
1616470decc6SDave Kleikamp  * structures.
1617470decc6SDave Kleikamp  */
1618f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal)
1619470decc6SDave Kleikamp {
1620470decc6SDave Kleikamp 	int err;
1621470decc6SDave Kleikamp 	journal_superblock_t *sb;
1622470decc6SDave Kleikamp 
1623470decc6SDave Kleikamp 	err = load_superblock(journal);
1624470decc6SDave Kleikamp 	if (err)
1625470decc6SDave Kleikamp 		return err;
1626470decc6SDave Kleikamp 
1627470decc6SDave Kleikamp 	sb = journal->j_superblock;
1628470decc6SDave Kleikamp 	/* If this is a V2 superblock, then we have to check the
1629470decc6SDave Kleikamp 	 * features flags on it. */
1630470decc6SDave Kleikamp 
1631470decc6SDave Kleikamp 	if (journal->j_format_version >= 2) {
1632470decc6SDave Kleikamp 		if ((sb->s_feature_ro_compat &
1633f7f4bccbSMingming Cao 		     ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1634470decc6SDave Kleikamp 		    (sb->s_feature_incompat &
1635f7f4bccbSMingming Cao 		     ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1636470decc6SDave Kleikamp 			printk(KERN_WARNING
1637f2a44523SEryu Guan 				"JBD2: Unrecognised features on journal\n");
1638470decc6SDave Kleikamp 			return -EINVAL;
1639470decc6SDave Kleikamp 		}
1640470decc6SDave Kleikamp 	}
1641470decc6SDave Kleikamp 
1642d2eecb03STheodore Ts'o 	/*
1643d2eecb03STheodore Ts'o 	 * Create a slab for this blocksize
1644d2eecb03STheodore Ts'o 	 */
1645d2eecb03STheodore Ts'o 	err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1646d2eecb03STheodore Ts'o 	if (err)
1647d2eecb03STheodore Ts'o 		return err;
1648d2eecb03STheodore Ts'o 
1649470decc6SDave Kleikamp 	/* Let the recovery code check whether it needs to recover any
1650470decc6SDave Kleikamp 	 * data from the journal. */
1651f7f4bccbSMingming Cao 	if (jbd2_journal_recover(journal))
1652470decc6SDave Kleikamp 		goto recovery_error;
1653470decc6SDave Kleikamp 
1654e6a47428STheodore Ts'o 	if (journal->j_failed_commit) {
1655e6a47428STheodore Ts'o 		printk(KERN_ERR "JBD2: journal transaction %u on %s "
1656e6a47428STheodore Ts'o 		       "is corrupt.\n", journal->j_failed_commit,
1657e6a47428STheodore Ts'o 		       journal->j_devname);
1658e6a47428STheodore Ts'o 		return -EIO;
1659e6a47428STheodore Ts'o 	}
1660e6a47428STheodore Ts'o 
1661470decc6SDave Kleikamp 	/* OK, we've finished with the dynamic journal bits:
1662470decc6SDave Kleikamp 	 * reinitialise the dynamic contents of the superblock in memory
1663470decc6SDave Kleikamp 	 * and reset them on disk. */
1664470decc6SDave Kleikamp 	if (journal_reset(journal))
1665470decc6SDave Kleikamp 		goto recovery_error;
1666470decc6SDave Kleikamp 
1667f7f4bccbSMingming Cao 	journal->j_flags &= ~JBD2_ABORT;
1668f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_LOADED;
1669470decc6SDave Kleikamp 	return 0;
1670470decc6SDave Kleikamp 
1671470decc6SDave Kleikamp recovery_error:
1672f2a44523SEryu Guan 	printk(KERN_WARNING "JBD2: recovery failed\n");
1673470decc6SDave Kleikamp 	return -EIO;
1674470decc6SDave Kleikamp }
1675470decc6SDave Kleikamp 
1676470decc6SDave Kleikamp /**
1677f7f4bccbSMingming Cao  * void jbd2_journal_destroy() - Release a journal_t structure.
1678470decc6SDave Kleikamp  * @journal: Journal to act on.
1679470decc6SDave Kleikamp  *
1680470decc6SDave Kleikamp  * Release a journal_t structure once it is no longer in use by the
1681470decc6SDave Kleikamp  * journaled object.
168244519fafSHidehiro Kawai  * Return <0 if we couldn't clean up the journal.
1683470decc6SDave Kleikamp  */
168444519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal)
1685470decc6SDave Kleikamp {
168644519fafSHidehiro Kawai 	int err = 0;
168744519fafSHidehiro Kawai 
1688470decc6SDave Kleikamp 	/* Wait for the commit thread to wake up and die. */
1689470decc6SDave Kleikamp 	journal_kill_thread(journal);
1690470decc6SDave Kleikamp 
1691470decc6SDave Kleikamp 	/* Force a final log commit */
1692470decc6SDave Kleikamp 	if (journal->j_running_transaction)
1693f7f4bccbSMingming Cao 		jbd2_journal_commit_transaction(journal);
1694470decc6SDave Kleikamp 
1695470decc6SDave Kleikamp 	/* Force any old transactions to disk */
1696470decc6SDave Kleikamp 
1697470decc6SDave Kleikamp 	/* Totally anal locking here... */
1698470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
1699470decc6SDave Kleikamp 	while (journal->j_checkpoint_transactions != NULL) {
1700470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
17011a0d3786STheodore Ts'o 		mutex_lock(&journal->j_checkpoint_mutex);
1702f7f4bccbSMingming Cao 		jbd2_log_do_checkpoint(journal);
17031a0d3786STheodore Ts'o 		mutex_unlock(&journal->j_checkpoint_mutex);
1704470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
1705470decc6SDave Kleikamp 	}
1706470decc6SDave Kleikamp 
1707470decc6SDave Kleikamp 	J_ASSERT(journal->j_running_transaction == NULL);
1708470decc6SDave Kleikamp 	J_ASSERT(journal->j_committing_transaction == NULL);
1709470decc6SDave Kleikamp 	J_ASSERT(journal->j_checkpoint_transactions == NULL);
1710470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
1711470decc6SDave Kleikamp 
171244519fafSHidehiro Kawai 	if (journal->j_sb_buffer) {
171344519fafSHidehiro Kawai 		if (!is_journal_aborted(journal)) {
1714a78bb11dSJan Kara 			mutex_lock(&journal->j_checkpoint_mutex);
171524bcc89cSJan Kara 			jbd2_mark_journal_empty(journal);
1716a78bb11dSJan Kara 			mutex_unlock(&journal->j_checkpoint_mutex);
1717a78bb11dSJan Kara 		} else
171844519fafSHidehiro Kawai 			err = -EIO;
1719470decc6SDave Kleikamp 		brelse(journal->j_sb_buffer);
1720470decc6SDave Kleikamp 	}
1721470decc6SDave Kleikamp 
17228e85fb3fSJohann Lombardi 	if (journal->j_proc_entry)
17238e85fb3fSJohann Lombardi 		jbd2_stats_proc_exit(journal);
1724470decc6SDave Kleikamp 	iput(journal->j_inode);
1725470decc6SDave Kleikamp 	if (journal->j_revoke)
1726f7f4bccbSMingming Cao 		jbd2_journal_destroy_revoke(journal);
172701b5adceSDarrick J. Wong 	if (journal->j_chksum_driver)
172801b5adceSDarrick J. Wong 		crypto_free_shash(journal->j_chksum_driver);
1729470decc6SDave Kleikamp 	kfree(journal->j_wbuf);
1730470decc6SDave Kleikamp 	kfree(journal);
173144519fafSHidehiro Kawai 
173244519fafSHidehiro Kawai 	return err;
1733470decc6SDave Kleikamp }
1734470decc6SDave Kleikamp 
1735470decc6SDave Kleikamp 
1736470decc6SDave Kleikamp /**
1737f7f4bccbSMingming Cao  *int jbd2_journal_check_used_features () - Check if features specified are used.
1738470decc6SDave Kleikamp  * @journal: Journal to check.
1739470decc6SDave Kleikamp  * @compat: bitmask of compatible features
1740470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
1741470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
1742470decc6SDave Kleikamp  *
1743470decc6SDave Kleikamp  * Check whether the journal uses all of a given set of
1744470decc6SDave Kleikamp  * features.  Return true (non-zero) if it does.
1745470decc6SDave Kleikamp  **/
1746470decc6SDave Kleikamp 
1747f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1748470decc6SDave Kleikamp 				 unsigned long ro, unsigned long incompat)
1749470decc6SDave Kleikamp {
1750470decc6SDave Kleikamp 	journal_superblock_t *sb;
1751470decc6SDave Kleikamp 
1752470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
1753470decc6SDave Kleikamp 		return 1;
17541113e1b5SPatrick J. LoPresti 	/* Load journal superblock if it is not loaded yet. */
17551113e1b5SPatrick J. LoPresti 	if (journal->j_format_version == 0 &&
17561113e1b5SPatrick J. LoPresti 	    journal_get_superblock(journal) != 0)
17571113e1b5SPatrick J. LoPresti 		return 0;
1758470decc6SDave Kleikamp 	if (journal->j_format_version == 1)
1759470decc6SDave Kleikamp 		return 0;
1760470decc6SDave Kleikamp 
1761470decc6SDave Kleikamp 	sb = journal->j_superblock;
1762470decc6SDave Kleikamp 
1763470decc6SDave Kleikamp 	if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1764470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1765470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1766470decc6SDave Kleikamp 		return 1;
1767470decc6SDave Kleikamp 
1768470decc6SDave Kleikamp 	return 0;
1769470decc6SDave Kleikamp }
1770470decc6SDave Kleikamp 
1771470decc6SDave Kleikamp /**
1772f7f4bccbSMingming Cao  * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1773470decc6SDave Kleikamp  * @journal: Journal to check.
1774470decc6SDave Kleikamp  * @compat: bitmask of compatible features
1775470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
1776470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
1777470decc6SDave Kleikamp  *
1778470decc6SDave Kleikamp  * Check whether the journaling code supports the use of
1779470decc6SDave Kleikamp  * all of a given set of features on this journal.  Return true
1780470decc6SDave Kleikamp  * (non-zero) if it can. */
1781470decc6SDave Kleikamp 
1782f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1783470decc6SDave Kleikamp 				      unsigned long ro, unsigned long incompat)
1784470decc6SDave Kleikamp {
1785470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
1786470decc6SDave Kleikamp 		return 1;
1787470decc6SDave Kleikamp 
1788470decc6SDave Kleikamp 	/* We can support any known requested features iff the
1789470decc6SDave Kleikamp 	 * superblock is in version 2.  Otherwise we fail to support any
1790470decc6SDave Kleikamp 	 * extended sb features. */
1791470decc6SDave Kleikamp 
1792470decc6SDave Kleikamp 	if (journal->j_format_version != 2)
1793470decc6SDave Kleikamp 		return 0;
1794470decc6SDave Kleikamp 
1795f7f4bccbSMingming Cao 	if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1796f7f4bccbSMingming Cao 	    (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1797f7f4bccbSMingming Cao 	    (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1798470decc6SDave Kleikamp 		return 1;
1799470decc6SDave Kleikamp 
1800470decc6SDave Kleikamp 	return 0;
1801470decc6SDave Kleikamp }
1802470decc6SDave Kleikamp 
1803470decc6SDave Kleikamp /**
1804f7f4bccbSMingming Cao  * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1805470decc6SDave Kleikamp  * @journal: Journal to act on.
1806470decc6SDave Kleikamp  * @compat: bitmask of compatible features
1807470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
1808470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
1809470decc6SDave Kleikamp  *
1810470decc6SDave Kleikamp  * Mark a given journal feature as present on the
1811470decc6SDave Kleikamp  * superblock.  Returns true if the requested features could be set.
1812470decc6SDave Kleikamp  *
1813470decc6SDave Kleikamp  */
1814470decc6SDave Kleikamp 
1815f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1816470decc6SDave Kleikamp 			  unsigned long ro, unsigned long incompat)
1817470decc6SDave Kleikamp {
181825ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \
181925ed6e8aSDarrick J. Wong 		((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
182025ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \
182125ed6e8aSDarrick J. Wong 		((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1822470decc6SDave Kleikamp 	journal_superblock_t *sb;
1823470decc6SDave Kleikamp 
1824f7f4bccbSMingming Cao 	if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1825470decc6SDave Kleikamp 		return 1;
1826470decc6SDave Kleikamp 
1827f7f4bccbSMingming Cao 	if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1828470decc6SDave Kleikamp 		return 0;
1829470decc6SDave Kleikamp 
1830db9ee220SDarrick J. Wong 	/* If enabling v2 checksums, turn on v3 instead */
1831db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
1832db9ee220SDarrick J. Wong 		incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
1833db9ee220SDarrick J. Wong 		incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
1834db9ee220SDarrick J. Wong 	}
1835db9ee220SDarrick J. Wong 
1836db9ee220SDarrick J. Wong 	/* Asking for checksumming v3 and v1?  Only give them v3. */
1837db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
183825ed6e8aSDarrick J. Wong 	    compat & JBD2_FEATURE_COMPAT_CHECKSUM)
183925ed6e8aSDarrick J. Wong 		compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
184025ed6e8aSDarrick J. Wong 
1841470decc6SDave Kleikamp 	jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1842470decc6SDave Kleikamp 		  compat, ro, incompat);
1843470decc6SDave Kleikamp 
1844470decc6SDave Kleikamp 	sb = journal->j_superblock;
1845470decc6SDave Kleikamp 
1846db9ee220SDarrick J. Wong 	/* If enabling v3 checksums, update superblock */
1847db9ee220SDarrick J. Wong 	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
184825ed6e8aSDarrick J. Wong 		sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
184925ed6e8aSDarrick J. Wong 		sb->s_feature_compat &=
185025ed6e8aSDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
185101b5adceSDarrick J. Wong 
185201b5adceSDarrick J. Wong 		/* Load the checksum driver */
185301b5adceSDarrick J. Wong 		if (journal->j_chksum_driver == NULL) {
185401b5adceSDarrick J. Wong 			journal->j_chksum_driver = crypto_alloc_shash("crc32c",
185501b5adceSDarrick J. Wong 								      0, 0);
185601b5adceSDarrick J. Wong 			if (IS_ERR(journal->j_chksum_driver)) {
1857a67c848aSDmitry Monakhov 				printk(KERN_ERR "JBD2: Cannot load crc32c "
185801b5adceSDarrick J. Wong 				       "driver.\n");
185901b5adceSDarrick J. Wong 				journal->j_chksum_driver = NULL;
186001b5adceSDarrick J. Wong 				return 0;
186101b5adceSDarrick J. Wong 			}
18624fd5ea43SDarrick J. Wong 
18634fd5ea43SDarrick J. Wong 			/* Precompute checksum seed for all metadata */
18644fd5ea43SDarrick J. Wong 			journal->j_csum_seed = jbd2_chksum(journal, ~0,
18654fd5ea43SDarrick J. Wong 							   sb->s_uuid,
18664fd5ea43SDarrick J. Wong 							   sizeof(sb->s_uuid));
186725ed6e8aSDarrick J. Wong 		}
186832f38691SDarrick J. Wong 	}
186925ed6e8aSDarrick J. Wong 
187025ed6e8aSDarrick J. Wong 	/* If enabling v1 checksums, downgrade superblock */
187125ed6e8aSDarrick J. Wong 	if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
187225ed6e8aSDarrick J. Wong 		sb->s_feature_incompat &=
1873db9ee220SDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
1874db9ee220SDarrick J. Wong 				     JBD2_FEATURE_INCOMPAT_CSUM_V3);
187525ed6e8aSDarrick J. Wong 
1876470decc6SDave Kleikamp 	sb->s_feature_compat    |= cpu_to_be32(compat);
1877470decc6SDave Kleikamp 	sb->s_feature_ro_compat |= cpu_to_be32(ro);
1878470decc6SDave Kleikamp 	sb->s_feature_incompat  |= cpu_to_be32(incompat);
1879470decc6SDave Kleikamp 
1880470decc6SDave Kleikamp 	return 1;
188125ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON
188225ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON
1883470decc6SDave Kleikamp }
1884470decc6SDave Kleikamp 
1885818d276cSGirish Shilamkar /*
1886818d276cSGirish Shilamkar  * jbd2_journal_clear_features () - Clear a given journal feature in the
1887818d276cSGirish Shilamkar  * 				    superblock
1888818d276cSGirish Shilamkar  * @journal: Journal to act on.
1889818d276cSGirish Shilamkar  * @compat: bitmask of compatible features
1890818d276cSGirish Shilamkar  * @ro: bitmask of features that force read-only mount
1891818d276cSGirish Shilamkar  * @incompat: bitmask of incompatible features
1892818d276cSGirish Shilamkar  *
1893818d276cSGirish Shilamkar  * Clear a given journal feature as present on the
1894818d276cSGirish Shilamkar  * superblock.
1895818d276cSGirish Shilamkar  */
1896818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1897818d276cSGirish Shilamkar 				unsigned long ro, unsigned long incompat)
1898818d276cSGirish Shilamkar {
1899818d276cSGirish Shilamkar 	journal_superblock_t *sb;
1900818d276cSGirish Shilamkar 
1901818d276cSGirish Shilamkar 	jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1902818d276cSGirish Shilamkar 		  compat, ro, incompat);
1903818d276cSGirish Shilamkar 
1904818d276cSGirish Shilamkar 	sb = journal->j_superblock;
1905818d276cSGirish Shilamkar 
1906818d276cSGirish Shilamkar 	sb->s_feature_compat    &= ~cpu_to_be32(compat);
1907818d276cSGirish Shilamkar 	sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1908818d276cSGirish Shilamkar 	sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
1909818d276cSGirish Shilamkar }
1910818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features);
1911470decc6SDave Kleikamp 
1912470decc6SDave Kleikamp /**
1913f7f4bccbSMingming Cao  * int jbd2_journal_flush () - Flush journal
1914470decc6SDave Kleikamp  * @journal: Journal to act on.
1915470decc6SDave Kleikamp  *
1916470decc6SDave Kleikamp  * Flush all data for a given journal to disk and empty the journal.
1917470decc6SDave Kleikamp  * Filesystems can use this when remounting readonly to ensure that
1918470decc6SDave Kleikamp  * recovery does not need to happen on remount.
1919470decc6SDave Kleikamp  */
1920470decc6SDave Kleikamp 
1921f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal)
1922470decc6SDave Kleikamp {
1923470decc6SDave Kleikamp 	int err = 0;
1924470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
1925470decc6SDave Kleikamp 
1926a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
1927470decc6SDave Kleikamp 
1928470decc6SDave Kleikamp 	/* Force everything buffered to the log... */
1929470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
1930470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
1931f7f4bccbSMingming Cao 		__jbd2_log_start_commit(journal, transaction->t_tid);
1932470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
1933470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
1934470decc6SDave Kleikamp 
1935470decc6SDave Kleikamp 	/* Wait for the log commit to complete... */
1936470decc6SDave Kleikamp 	if (transaction) {
1937470decc6SDave Kleikamp 		tid_t tid = transaction->t_tid;
1938470decc6SDave Kleikamp 
1939a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
1940f7f4bccbSMingming Cao 		jbd2_log_wait_commit(journal, tid);
1941470decc6SDave Kleikamp 	} else {
1942a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
1943470decc6SDave Kleikamp 	}
1944470decc6SDave Kleikamp 
1945470decc6SDave Kleikamp 	/* ...and flush everything in the log out to disk. */
1946470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
1947470decc6SDave Kleikamp 	while (!err && journal->j_checkpoint_transactions != NULL) {
1948470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
194944519fafSHidehiro Kawai 		mutex_lock(&journal->j_checkpoint_mutex);
1950f7f4bccbSMingming Cao 		err = jbd2_log_do_checkpoint(journal);
195144519fafSHidehiro Kawai 		mutex_unlock(&journal->j_checkpoint_mutex);
1952470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
1953470decc6SDave Kleikamp 	}
1954470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
195544519fafSHidehiro Kawai 
195644519fafSHidehiro Kawai 	if (is_journal_aborted(journal))
195744519fafSHidehiro Kawai 		return -EIO;
195844519fafSHidehiro Kawai 
1959a78bb11dSJan Kara 	mutex_lock(&journal->j_checkpoint_mutex);
1960*6f6a6fdaSJoseph Qi 	if (!err) {
1961*6f6a6fdaSJoseph Qi 		err = jbd2_cleanup_journal_tail(journal);
1962*6f6a6fdaSJoseph Qi 		if (err < 0) {
1963*6f6a6fdaSJoseph Qi 			mutex_unlock(&journal->j_checkpoint_mutex);
1964*6f6a6fdaSJoseph Qi 			goto out;
1965*6f6a6fdaSJoseph Qi 		}
1966*6f6a6fdaSJoseph Qi 		err = 0;
1967*6f6a6fdaSJoseph Qi 	}
1968470decc6SDave Kleikamp 
1969470decc6SDave Kleikamp 	/* Finally, mark the journal as really needing no recovery.
1970470decc6SDave Kleikamp 	 * This sets s_start==0 in the underlying superblock, which is
1971470decc6SDave Kleikamp 	 * the magic code for a fully-recovered superblock.  Any future
1972470decc6SDave Kleikamp 	 * commits of data to the journal will restore the current
1973470decc6SDave Kleikamp 	 * s_start value. */
197424bcc89cSJan Kara 	jbd2_mark_journal_empty(journal);
1975a78bb11dSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
1976a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
1977470decc6SDave Kleikamp 	J_ASSERT(!journal->j_running_transaction);
1978470decc6SDave Kleikamp 	J_ASSERT(!journal->j_committing_transaction);
1979470decc6SDave Kleikamp 	J_ASSERT(!journal->j_checkpoint_transactions);
1980470decc6SDave Kleikamp 	J_ASSERT(journal->j_head == journal->j_tail);
1981470decc6SDave Kleikamp 	J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1982a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
1983*6f6a6fdaSJoseph Qi out:
1984*6f6a6fdaSJoseph Qi 	return err;
1985470decc6SDave Kleikamp }
1986470decc6SDave Kleikamp 
1987470decc6SDave Kleikamp /**
1988f7f4bccbSMingming Cao  * int jbd2_journal_wipe() - Wipe journal contents
1989470decc6SDave Kleikamp  * @journal: Journal to act on.
1990470decc6SDave Kleikamp  * @write: flag (see below)
1991470decc6SDave Kleikamp  *
1992470decc6SDave Kleikamp  * Wipe out all of the contents of a journal, safely.  This will produce
1993470decc6SDave Kleikamp  * a warning if the journal contains any valid recovery information.
1994f7f4bccbSMingming Cao  * Must be called between journal_init_*() and jbd2_journal_load().
1995470decc6SDave Kleikamp  *
1996470decc6SDave Kleikamp  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1997470decc6SDave Kleikamp  * we merely suppress recovery.
1998470decc6SDave Kleikamp  */
1999470decc6SDave Kleikamp 
2000f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write)
2001470decc6SDave Kleikamp {
2002470decc6SDave Kleikamp 	int err = 0;
2003470decc6SDave Kleikamp 
2004f7f4bccbSMingming Cao 	J_ASSERT (!(journal->j_flags & JBD2_LOADED));
2005470decc6SDave Kleikamp 
2006470decc6SDave Kleikamp 	err = load_superblock(journal);
2007470decc6SDave Kleikamp 	if (err)
2008470decc6SDave Kleikamp 		return err;
2009470decc6SDave Kleikamp 
2010470decc6SDave Kleikamp 	if (!journal->j_tail)
2011470decc6SDave Kleikamp 		goto no_recovery;
2012470decc6SDave Kleikamp 
2013f2a44523SEryu Guan 	printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
2014470decc6SDave Kleikamp 		write ? "Clearing" : "Ignoring");
2015470decc6SDave Kleikamp 
2016f7f4bccbSMingming Cao 	err = jbd2_journal_skip_recovery(journal);
2017a78bb11dSJan Kara 	if (write) {
2018a78bb11dSJan Kara 		/* Lock to make assertions happy... */
2019a78bb11dSJan Kara 		mutex_lock(&journal->j_checkpoint_mutex);
202024bcc89cSJan Kara 		jbd2_mark_journal_empty(journal);
2021a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
2022a78bb11dSJan Kara 	}
2023470decc6SDave Kleikamp 
2024470decc6SDave Kleikamp  no_recovery:
2025470decc6SDave Kleikamp 	return err;
2026470decc6SDave Kleikamp }
2027470decc6SDave Kleikamp 
2028470decc6SDave Kleikamp /*
2029470decc6SDave Kleikamp  * Journal abort has very specific semantics, which we describe
2030470decc6SDave Kleikamp  * for journal abort.
2031470decc6SDave Kleikamp  *
2032bfcd3555SAlberto Bertogli  * Two internal functions, which provide abort to the jbd layer
2033470decc6SDave Kleikamp  * itself are here.
2034470decc6SDave Kleikamp  */
2035470decc6SDave Kleikamp 
2036470decc6SDave Kleikamp /*
2037470decc6SDave Kleikamp  * Quick version for internal journal use (doesn't lock the journal).
2038470decc6SDave Kleikamp  * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
2039470decc6SDave Kleikamp  * and don't attempt to make any other journal updates.
2040470decc6SDave Kleikamp  */
2041f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal)
2042470decc6SDave Kleikamp {
2043470decc6SDave Kleikamp 	transaction_t *transaction;
2044470decc6SDave Kleikamp 
2045f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2046470decc6SDave Kleikamp 		return;
2047470decc6SDave Kleikamp 
2048470decc6SDave Kleikamp 	printk(KERN_ERR "Aborting journal on device %s.\n",
204905496769STheodore Ts'o 	       journal->j_devname);
2050470decc6SDave Kleikamp 
2051a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2052f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_ABORT;
2053470decc6SDave Kleikamp 	transaction = journal->j_running_transaction;
2054470decc6SDave Kleikamp 	if (transaction)
2055f7f4bccbSMingming Cao 		__jbd2_log_start_commit(journal, transaction->t_tid);
2056a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2057470decc6SDave Kleikamp }
2058470decc6SDave Kleikamp 
2059470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock,
2060470decc6SDave Kleikamp  * but don't do any other IO. */
2061470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno)
2062470decc6SDave Kleikamp {
2063f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2064470decc6SDave Kleikamp 		return;
2065470decc6SDave Kleikamp 
2066470decc6SDave Kleikamp 	if (!journal->j_errno)
2067470decc6SDave Kleikamp 		journal->j_errno = errno;
2068470decc6SDave Kleikamp 
2069f7f4bccbSMingming Cao 	__jbd2_journal_abort_hard(journal);
2070470decc6SDave Kleikamp 
2071470decc6SDave Kleikamp 	if (errno)
207224bcc89cSJan Kara 		jbd2_journal_update_sb_errno(journal);
2073470decc6SDave Kleikamp }
2074470decc6SDave Kleikamp 
2075470decc6SDave Kleikamp /**
2076f7f4bccbSMingming Cao  * void jbd2_journal_abort () - Shutdown the journal immediately.
2077470decc6SDave Kleikamp  * @journal: the journal to shutdown.
2078470decc6SDave Kleikamp  * @errno:   an error number to record in the journal indicating
2079470decc6SDave Kleikamp  *           the reason for the shutdown.
2080470decc6SDave Kleikamp  *
2081470decc6SDave Kleikamp  * Perform a complete, immediate shutdown of the ENTIRE
2082470decc6SDave Kleikamp  * journal (not of a single transaction).  This operation cannot be
2083470decc6SDave Kleikamp  * undone without closing and reopening the journal.
2084470decc6SDave Kleikamp  *
2085f7f4bccbSMingming Cao  * The jbd2_journal_abort function is intended to support higher level error
2086470decc6SDave Kleikamp  * recovery mechanisms such as the ext2/ext3 remount-readonly error
2087470decc6SDave Kleikamp  * mode.
2088470decc6SDave Kleikamp  *
2089470decc6SDave Kleikamp  * Journal abort has very specific semantics.  Any existing dirty,
2090470decc6SDave Kleikamp  * unjournaled buffers in the main filesystem will still be written to
2091470decc6SDave Kleikamp  * disk by bdflush, but the journaling mechanism will be suspended
2092470decc6SDave Kleikamp  * immediately and no further transaction commits will be honoured.
2093470decc6SDave Kleikamp  *
2094470decc6SDave Kleikamp  * Any dirty, journaled buffers will be written back to disk without
2095470decc6SDave Kleikamp  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
2096470decc6SDave Kleikamp  * filesystem, but we _do_ attempt to leave as much data as possible
2097470decc6SDave Kleikamp  * behind for fsck to use for cleanup.
2098470decc6SDave Kleikamp  *
2099470decc6SDave Kleikamp  * Any attempt to get a new transaction handle on a journal which is in
2100470decc6SDave Kleikamp  * ABORT state will just result in an -EROFS error return.  A
2101f7f4bccbSMingming Cao  * jbd2_journal_stop on an existing handle will return -EIO if we have
2102470decc6SDave Kleikamp  * entered abort state during the update.
2103470decc6SDave Kleikamp  *
2104470decc6SDave Kleikamp  * Recursive transactions are not disturbed by journal abort until the
2105f7f4bccbSMingming Cao  * final jbd2_journal_stop, which will receive the -EIO error.
2106470decc6SDave Kleikamp  *
2107f7f4bccbSMingming Cao  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2108470decc6SDave Kleikamp  * which will be recorded (if possible) in the journal superblock.  This
2109470decc6SDave Kleikamp  * allows a client to record failure conditions in the middle of a
2110470decc6SDave Kleikamp  * transaction without having to complete the transaction to record the
2111470decc6SDave Kleikamp  * failure to disk.  ext3_error, for example, now uses this
2112470decc6SDave Kleikamp  * functionality.
2113470decc6SDave Kleikamp  *
2114470decc6SDave Kleikamp  * Errors which originate from within the journaling layer will NOT
2115470decc6SDave Kleikamp  * supply an errno; a null errno implies that absolutely no further
2116470decc6SDave Kleikamp  * writes are done to the journal (unless there are any already in
2117470decc6SDave Kleikamp  * progress).
2118470decc6SDave Kleikamp  *
2119470decc6SDave Kleikamp  */
2120470decc6SDave Kleikamp 
2121f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno)
2122470decc6SDave Kleikamp {
2123470decc6SDave Kleikamp 	__journal_abort_soft(journal, errno);
2124470decc6SDave Kleikamp }
2125470decc6SDave Kleikamp 
2126470decc6SDave Kleikamp /**
2127f7f4bccbSMingming Cao  * int jbd2_journal_errno () - returns the journal's error state.
2128470decc6SDave Kleikamp  * @journal: journal to examine.
2129470decc6SDave Kleikamp  *
2130bfcd3555SAlberto Bertogli  * This is the errno number set with jbd2_journal_abort(), the last
2131470decc6SDave Kleikamp  * time the journal was mounted - if the journal was stopped
2132470decc6SDave Kleikamp  * without calling abort this will be 0.
2133470decc6SDave Kleikamp  *
2134470decc6SDave Kleikamp  * If the journal has been aborted on this mount time -EROFS will
2135470decc6SDave Kleikamp  * be returned.
2136470decc6SDave Kleikamp  */
2137f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal)
2138470decc6SDave Kleikamp {
2139470decc6SDave Kleikamp 	int err;
2140470decc6SDave Kleikamp 
2141a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
2142f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2143470decc6SDave Kleikamp 		err = -EROFS;
2144470decc6SDave Kleikamp 	else
2145470decc6SDave Kleikamp 		err = journal->j_errno;
2146a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
2147470decc6SDave Kleikamp 	return err;
2148470decc6SDave Kleikamp }
2149470decc6SDave Kleikamp 
2150470decc6SDave Kleikamp /**
2151f7f4bccbSMingming Cao  * int jbd2_journal_clear_err () - clears the journal's error state
2152470decc6SDave Kleikamp  * @journal: journal to act on.
2153470decc6SDave Kleikamp  *
2154bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2155470decc6SDave Kleikamp  * mode.
2156470decc6SDave Kleikamp  */
2157f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal)
2158470decc6SDave Kleikamp {
2159470decc6SDave Kleikamp 	int err = 0;
2160470decc6SDave Kleikamp 
2161a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2162f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2163470decc6SDave Kleikamp 		err = -EROFS;
2164470decc6SDave Kleikamp 	else
2165470decc6SDave Kleikamp 		journal->j_errno = 0;
2166a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2167470decc6SDave Kleikamp 	return err;
2168470decc6SDave Kleikamp }
2169470decc6SDave Kleikamp 
2170470decc6SDave Kleikamp /**
2171f7f4bccbSMingming Cao  * void jbd2_journal_ack_err() - Ack journal err.
2172470decc6SDave Kleikamp  * @journal: journal to act on.
2173470decc6SDave Kleikamp  *
2174bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2175470decc6SDave Kleikamp  * mode.
2176470decc6SDave Kleikamp  */
2177f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal)
2178470decc6SDave Kleikamp {
2179a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2180470decc6SDave Kleikamp 	if (journal->j_errno)
2181f7f4bccbSMingming Cao 		journal->j_flags |= JBD2_ACK_ERR;
2182a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2183470decc6SDave Kleikamp }
2184470decc6SDave Kleikamp 
2185f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode)
2186470decc6SDave Kleikamp {
2187470decc6SDave Kleikamp 	return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2188470decc6SDave Kleikamp }
2189470decc6SDave Kleikamp 
2190470decc6SDave Kleikamp /*
2191b517bea1SZach Brown  * helper functions to deal with 32 or 64bit block numbers.
2192b517bea1SZach Brown  */
2193b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal)
2194b517bea1SZach Brown {
2195db9ee220SDarrick J. Wong 	size_t sz;
2196db9ee220SDarrick J. Wong 
2197db9ee220SDarrick J. Wong 	if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3))
2198db9ee220SDarrick J. Wong 		return sizeof(journal_block_tag3_t);
2199db9ee220SDarrick J. Wong 
2200db9ee220SDarrick J. Wong 	sz = sizeof(journal_block_tag_t);
2201c3900875SDarrick J. Wong 
2202c3900875SDarrick J. Wong 	if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2203db9ee220SDarrick J. Wong 		sz += sizeof(__u16);
2204c3900875SDarrick J. Wong 
2205b517bea1SZach Brown 	if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2206db9ee220SDarrick J. Wong 		return sz;
2207b517bea1SZach Brown 	else
2208db9ee220SDarrick J. Wong 		return sz - sizeof(__u32);
2209b517bea1SZach Brown }
2210b517bea1SZach Brown 
2211b517bea1SZach Brown /*
2212d2eecb03STheodore Ts'o  * JBD memory management
2213d2eecb03STheodore Ts'o  *
2214d2eecb03STheodore Ts'o  * These functions are used to allocate block-sized chunks of memory
2215d2eecb03STheodore Ts'o  * used for making copies of buffer_head data.  Very often it will be
2216d2eecb03STheodore Ts'o  * page-sized chunks of data, but sometimes it will be in
2217d2eecb03STheodore Ts'o  * sub-page-size chunks.  (For example, 16k pages on Power systems
2218d2eecb03STheodore Ts'o  * with a 4k block file system.)  For blocks smaller than a page, we
2219d2eecb03STheodore Ts'o  * use a SLAB allocator.  There are slab caches for each block size,
2220d2eecb03STheodore Ts'o  * which are allocated at mount time, if necessary, and we only free
2221d2eecb03STheodore Ts'o  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
2222d2eecb03STheodore Ts'o  * this reason we don't need to a mutex to protect access to
2223d2eecb03STheodore Ts'o  * jbd2_slab[] allocating or releasing memory; only in
2224d2eecb03STheodore Ts'o  * jbd2_journal_create_slab().
2225d2eecb03STheodore Ts'o  */
2226d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8
2227d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2228d2eecb03STheodore Ts'o 
2229d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2230d2eecb03STheodore Ts'o 	"jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2231d2eecb03STheodore Ts'o 	"jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2232d2eecb03STheodore Ts'o };
2233d2eecb03STheodore Ts'o 
2234d2eecb03STheodore Ts'o 
2235d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void)
2236d2eecb03STheodore Ts'o {
2237d2eecb03STheodore Ts'o 	int i;
2238d2eecb03STheodore Ts'o 
2239d2eecb03STheodore Ts'o 	for (i = 0; i < JBD2_MAX_SLABS; i++) {
2240d2eecb03STheodore Ts'o 		if (jbd2_slab[i])
2241d2eecb03STheodore Ts'o 			kmem_cache_destroy(jbd2_slab[i]);
2242d2eecb03STheodore Ts'o 		jbd2_slab[i] = NULL;
2243d2eecb03STheodore Ts'o 	}
2244d2eecb03STheodore Ts'o }
2245d2eecb03STheodore Ts'o 
2246d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size)
2247d2eecb03STheodore Ts'o {
224851dfacdeSThomas Gleixner 	static DEFINE_MUTEX(jbd2_slab_create_mutex);
2249d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2250d2eecb03STheodore Ts'o 	size_t slab_size;
2251d2eecb03STheodore Ts'o 
2252d2eecb03STheodore Ts'o 	if (size == PAGE_SIZE)
2253d2eecb03STheodore Ts'o 		return 0;
2254d2eecb03STheodore Ts'o 
2255d2eecb03STheodore Ts'o 	if (i >= JBD2_MAX_SLABS)
2256d2eecb03STheodore Ts'o 		return -EINVAL;
2257d2eecb03STheodore Ts'o 
2258d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2259d2eecb03STheodore Ts'o 		i = 0;
226051dfacdeSThomas Gleixner 	mutex_lock(&jbd2_slab_create_mutex);
2261d2eecb03STheodore Ts'o 	if (jbd2_slab[i]) {
226251dfacdeSThomas Gleixner 		mutex_unlock(&jbd2_slab_create_mutex);
2263d2eecb03STheodore Ts'o 		return 0;	/* Already created */
2264d2eecb03STheodore Ts'o 	}
2265d2eecb03STheodore Ts'o 
2266d2eecb03STheodore Ts'o 	slab_size = 1 << (i+10);
2267d2eecb03STheodore Ts'o 	jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2268d2eecb03STheodore Ts'o 					 slab_size, 0, NULL);
226951dfacdeSThomas Gleixner 	mutex_unlock(&jbd2_slab_create_mutex);
2270d2eecb03STheodore Ts'o 	if (!jbd2_slab[i]) {
2271d2eecb03STheodore Ts'o 		printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2272d2eecb03STheodore Ts'o 		return -ENOMEM;
2273d2eecb03STheodore Ts'o 	}
2274d2eecb03STheodore Ts'o 	return 0;
2275d2eecb03STheodore Ts'o }
2276d2eecb03STheodore Ts'o 
2277d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size)
2278d2eecb03STheodore Ts'o {
2279d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2280d2eecb03STheodore Ts'o 
2281d2eecb03STheodore Ts'o 	BUG_ON(i >= JBD2_MAX_SLABS);
2282d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2283d2eecb03STheodore Ts'o 		i = 0;
22848ac97b74SBill Pemberton 	BUG_ON(jbd2_slab[i] == NULL);
2285d2eecb03STheodore Ts'o 	return jbd2_slab[i];
2286d2eecb03STheodore Ts'o }
2287d2eecb03STheodore Ts'o 
2288d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags)
2289d2eecb03STheodore Ts'o {
2290d2eecb03STheodore Ts'o 	void *ptr;
2291d2eecb03STheodore Ts'o 
2292d2eecb03STheodore Ts'o 	BUG_ON(size & (size-1)); /* Must be a power of 2 */
2293d2eecb03STheodore Ts'o 
2294d2eecb03STheodore Ts'o 	flags |= __GFP_REPEAT;
2295d2eecb03STheodore Ts'o 	if (size == PAGE_SIZE)
2296d2eecb03STheodore Ts'o 		ptr = (void *)__get_free_pages(flags, 0);
2297d2eecb03STheodore Ts'o 	else if (size > PAGE_SIZE) {
2298d2eecb03STheodore Ts'o 		int order = get_order(size);
2299d2eecb03STheodore Ts'o 
2300d2eecb03STheodore Ts'o 		if (order < 3)
2301d2eecb03STheodore Ts'o 			ptr = (void *)__get_free_pages(flags, order);
2302d2eecb03STheodore Ts'o 		else
2303d2eecb03STheodore Ts'o 			ptr = vmalloc(size);
2304d2eecb03STheodore Ts'o 	} else
2305d2eecb03STheodore Ts'o 		ptr = kmem_cache_alloc(get_slab(size), flags);
2306d2eecb03STheodore Ts'o 
2307d2eecb03STheodore Ts'o 	/* Check alignment; SLUB has gotten this wrong in the past,
2308d2eecb03STheodore Ts'o 	 * and this can lead to user data corruption! */
2309d2eecb03STheodore Ts'o 	BUG_ON(((unsigned long) ptr) & (size-1));
2310d2eecb03STheodore Ts'o 
2311d2eecb03STheodore Ts'o 	return ptr;
2312d2eecb03STheodore Ts'o }
2313d2eecb03STheodore Ts'o 
2314d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size)
2315d2eecb03STheodore Ts'o {
2316d2eecb03STheodore Ts'o 	if (size == PAGE_SIZE) {
2317d2eecb03STheodore Ts'o 		free_pages((unsigned long)ptr, 0);
2318d2eecb03STheodore Ts'o 		return;
2319d2eecb03STheodore Ts'o 	}
2320d2eecb03STheodore Ts'o 	if (size > PAGE_SIZE) {
2321d2eecb03STheodore Ts'o 		int order = get_order(size);
2322d2eecb03STheodore Ts'o 
2323d2eecb03STheodore Ts'o 		if (order < 3)
2324d2eecb03STheodore Ts'o 			free_pages((unsigned long)ptr, order);
2325d2eecb03STheodore Ts'o 		else
2326d2eecb03STheodore Ts'o 			vfree(ptr);
2327d2eecb03STheodore Ts'o 		return;
2328d2eecb03STheodore Ts'o 	}
2329d2eecb03STheodore Ts'o 	kmem_cache_free(get_slab(size), ptr);
2330d2eecb03STheodore Ts'o };
2331d2eecb03STheodore Ts'o 
2332d2eecb03STheodore Ts'o /*
2333470decc6SDave Kleikamp  * Journal_head storage management
2334470decc6SDave Kleikamp  */
2335e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache;
2336e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2337470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2338470decc6SDave Kleikamp #endif
2339470decc6SDave Kleikamp 
23404185a2acSYongqiang Yang static int jbd2_journal_init_journal_head_cache(void)
2341470decc6SDave Kleikamp {
2342470decc6SDave Kleikamp 	int retval;
2343470decc6SDave Kleikamp 
23441076d17aSAl Viro 	J_ASSERT(jbd2_journal_head_cache == NULL);
2345a920e941SJohann Lombardi 	jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2346470decc6SDave Kleikamp 				sizeof(struct journal_head),
2347470decc6SDave Kleikamp 				0,		/* offset */
2348de92c8caSJan Kara 				SLAB_TEMPORARY | SLAB_DESTROY_BY_RCU,
234920c2df83SPaul Mundt 				NULL);		/* ctor */
2350470decc6SDave Kleikamp 	retval = 0;
23511076d17aSAl Viro 	if (!jbd2_journal_head_cache) {
2352470decc6SDave Kleikamp 		retval = -ENOMEM;
2353f2a44523SEryu Guan 		printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2354470decc6SDave Kleikamp 	}
2355470decc6SDave Kleikamp 	return retval;
2356470decc6SDave Kleikamp }
2357470decc6SDave Kleikamp 
23584185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void)
2359470decc6SDave Kleikamp {
23608a9362ebSDuane Griffin 	if (jbd2_journal_head_cache) {
2361f7f4bccbSMingming Cao 		kmem_cache_destroy(jbd2_journal_head_cache);
2362f7f4bccbSMingming Cao 		jbd2_journal_head_cache = NULL;
2363470decc6SDave Kleikamp 	}
23648a9362ebSDuane Griffin }
2365470decc6SDave Kleikamp 
2366470decc6SDave Kleikamp /*
2367470decc6SDave Kleikamp  * journal_head splicing and dicing
2368470decc6SDave Kleikamp  */
2369470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void)
2370470decc6SDave Kleikamp {
2371470decc6SDave Kleikamp 	struct journal_head *ret;
2372470decc6SDave Kleikamp 
2373e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2374470decc6SDave Kleikamp 	atomic_inc(&nr_journal_heads);
2375470decc6SDave Kleikamp #endif
23765d9cf9c6SZheng Liu 	ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
23771076d17aSAl Viro 	if (!ret) {
2378470decc6SDave Kleikamp 		jbd_debug(1, "out of memory for journal_head\n");
2379670be5a7STheodore Ts'o 		pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
23801076d17aSAl Viro 		while (!ret) {
2381470decc6SDave Kleikamp 			yield();
23825d9cf9c6SZheng Liu 			ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2383470decc6SDave Kleikamp 		}
2384470decc6SDave Kleikamp 	}
2385470decc6SDave Kleikamp 	return ret;
2386470decc6SDave Kleikamp }
2387470decc6SDave Kleikamp 
2388470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh)
2389470decc6SDave Kleikamp {
2390e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2391470decc6SDave Kleikamp 	atomic_dec(&nr_journal_heads);
2392cd02ff0bSMingming Cao 	memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2393470decc6SDave Kleikamp #endif
2394f7f4bccbSMingming Cao 	kmem_cache_free(jbd2_journal_head_cache, jh);
2395470decc6SDave Kleikamp }
2396470decc6SDave Kleikamp 
2397470decc6SDave Kleikamp /*
2398470decc6SDave Kleikamp  * A journal_head is attached to a buffer_head whenever JBD has an
2399470decc6SDave Kleikamp  * interest in the buffer.
2400470decc6SDave Kleikamp  *
2401470decc6SDave Kleikamp  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2402470decc6SDave Kleikamp  * is set.  This bit is tested in core kernel code where we need to take
2403470decc6SDave Kleikamp  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
2404470decc6SDave Kleikamp  * there.
2405470decc6SDave Kleikamp  *
2406470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2407470decc6SDave Kleikamp  *
2408470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set it is immune from being released by
2409470decc6SDave Kleikamp  * core kernel code, mainly via ->b_count.
2410470decc6SDave Kleikamp  *
2411de1b7941SJan Kara  * A journal_head is detached from its buffer_head when the journal_head's
2412de1b7941SJan Kara  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2413de1b7941SJan Kara  * transaction (b_cp_transaction) hold their references to b_jcount.
2414470decc6SDave Kleikamp  *
2415470decc6SDave Kleikamp  * Various places in the kernel want to attach a journal_head to a buffer_head
2416470decc6SDave Kleikamp  * _before_ attaching the journal_head to a transaction.  To protect the
2417f7f4bccbSMingming Cao  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2418470decc6SDave Kleikamp  * journal_head's b_jcount refcount by one.  The caller must call
2419f7f4bccbSMingming Cao  * jbd2_journal_put_journal_head() to undo this.
2420470decc6SDave Kleikamp  *
2421470decc6SDave Kleikamp  * So the typical usage would be:
2422470decc6SDave Kleikamp  *
2423470decc6SDave Kleikamp  *	(Attach a journal_head if needed.  Increments b_jcount)
2424f7f4bccbSMingming Cao  *	struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2425470decc6SDave Kleikamp  *	...
2426de1b7941SJan Kara  *      (Get another reference for transaction)
2427de1b7941SJan Kara  *	jbd2_journal_grab_journal_head(bh);
2428470decc6SDave Kleikamp  *	jh->b_transaction = xxx;
2429de1b7941SJan Kara  *	(Put original reference)
2430f7f4bccbSMingming Cao  *	jbd2_journal_put_journal_head(jh);
2431470decc6SDave Kleikamp  */
2432470decc6SDave Kleikamp 
2433470decc6SDave Kleikamp /*
2434470decc6SDave Kleikamp  * Give a buffer_head a journal_head.
2435470decc6SDave Kleikamp  *
2436470decc6SDave Kleikamp  * May sleep.
2437470decc6SDave Kleikamp  */
2438f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2439470decc6SDave Kleikamp {
2440470decc6SDave Kleikamp 	struct journal_head *jh;
2441470decc6SDave Kleikamp 	struct journal_head *new_jh = NULL;
2442470decc6SDave Kleikamp 
2443470decc6SDave Kleikamp repeat:
24445d9cf9c6SZheng Liu 	if (!buffer_jbd(bh))
2445470decc6SDave Kleikamp 		new_jh = journal_alloc_journal_head();
2446470decc6SDave Kleikamp 
2447470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2448470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2449470decc6SDave Kleikamp 		jh = bh2jh(bh);
2450470decc6SDave Kleikamp 	} else {
2451470decc6SDave Kleikamp 		J_ASSERT_BH(bh,
2452470decc6SDave Kleikamp 			(atomic_read(&bh->b_count) > 0) ||
2453470decc6SDave Kleikamp 			(bh->b_page && bh->b_page->mapping));
2454470decc6SDave Kleikamp 
2455470decc6SDave Kleikamp 		if (!new_jh) {
2456470decc6SDave Kleikamp 			jbd_unlock_bh_journal_head(bh);
2457470decc6SDave Kleikamp 			goto repeat;
2458470decc6SDave Kleikamp 		}
2459470decc6SDave Kleikamp 
2460470decc6SDave Kleikamp 		jh = new_jh;
2461470decc6SDave Kleikamp 		new_jh = NULL;		/* We consumed it */
2462470decc6SDave Kleikamp 		set_buffer_jbd(bh);
2463470decc6SDave Kleikamp 		bh->b_private = jh;
2464470decc6SDave Kleikamp 		jh->b_bh = bh;
2465470decc6SDave Kleikamp 		get_bh(bh);
2466470decc6SDave Kleikamp 		BUFFER_TRACE(bh, "added journal_head");
2467470decc6SDave Kleikamp 	}
2468470decc6SDave Kleikamp 	jh->b_jcount++;
2469470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2470470decc6SDave Kleikamp 	if (new_jh)
2471470decc6SDave Kleikamp 		journal_free_journal_head(new_jh);
2472470decc6SDave Kleikamp 	return bh->b_private;
2473470decc6SDave Kleikamp }
2474470decc6SDave Kleikamp 
2475470decc6SDave Kleikamp /*
2476470decc6SDave Kleikamp  * Grab a ref against this buffer_head's journal_head.  If it ended up not
2477470decc6SDave Kleikamp  * having a journal_head, return NULL
2478470decc6SDave Kleikamp  */
2479f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2480470decc6SDave Kleikamp {
2481470decc6SDave Kleikamp 	struct journal_head *jh = NULL;
2482470decc6SDave Kleikamp 
2483470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2484470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2485470decc6SDave Kleikamp 		jh = bh2jh(bh);
2486470decc6SDave Kleikamp 		jh->b_jcount++;
2487470decc6SDave Kleikamp 	}
2488470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2489470decc6SDave Kleikamp 	return jh;
2490470decc6SDave Kleikamp }
2491470decc6SDave Kleikamp 
2492470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh)
2493470decc6SDave Kleikamp {
2494470decc6SDave Kleikamp 	struct journal_head *jh = bh2jh(bh);
2495470decc6SDave Kleikamp 
2496470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jcount >= 0);
2497de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_transaction == NULL);
2498de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2499de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2500470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2501470decc6SDave Kleikamp 	J_ASSERT_BH(bh, buffer_jbd(bh));
2502470decc6SDave Kleikamp 	J_ASSERT_BH(bh, jh2bh(jh) == bh);
2503470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "remove journal_head");
2504470decc6SDave Kleikamp 	if (jh->b_frozen_data) {
2505de1b7941SJan Kara 		printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2506af1e76d6SMingming Cao 		jbd2_free(jh->b_frozen_data, bh->b_size);
2507470decc6SDave Kleikamp 	}
2508470decc6SDave Kleikamp 	if (jh->b_committed_data) {
2509de1b7941SJan Kara 		printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2510af1e76d6SMingming Cao 		jbd2_free(jh->b_committed_data, bh->b_size);
2511470decc6SDave Kleikamp 	}
2512470decc6SDave Kleikamp 	bh->b_private = NULL;
2513470decc6SDave Kleikamp 	jh->b_bh = NULL;	/* debug, really */
2514470decc6SDave Kleikamp 	clear_buffer_jbd(bh);
2515470decc6SDave Kleikamp 	journal_free_journal_head(jh);
2516470decc6SDave Kleikamp }
2517470decc6SDave Kleikamp 
2518470decc6SDave Kleikamp /*
2519de1b7941SJan Kara  * Drop a reference on the passed journal_head.  If it fell to zero then
2520470decc6SDave Kleikamp  * release the journal_head from the buffer_head.
2521470decc6SDave Kleikamp  */
2522f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh)
2523470decc6SDave Kleikamp {
2524470decc6SDave Kleikamp 	struct buffer_head *bh = jh2bh(jh);
2525470decc6SDave Kleikamp 
2526470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2527470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jcount > 0);
2528470decc6SDave Kleikamp 	--jh->b_jcount;
2529de1b7941SJan Kara 	if (!jh->b_jcount) {
2530470decc6SDave Kleikamp 		__journal_remove_journal_head(bh);
2531de1b7941SJan Kara 		jbd_unlock_bh_journal_head(bh);
2532470decc6SDave Kleikamp 		__brelse(bh);
2533de1b7941SJan Kara 	} else
2534470decc6SDave Kleikamp 		jbd_unlock_bh_journal_head(bh);
2535470decc6SDave Kleikamp }
2536470decc6SDave Kleikamp 
2537470decc6SDave Kleikamp /*
2538c851ed54SJan Kara  * Initialize jbd inode head
2539c851ed54SJan Kara  */
2540c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2541c851ed54SJan Kara {
2542c851ed54SJan Kara 	jinode->i_transaction = NULL;
2543c851ed54SJan Kara 	jinode->i_next_transaction = NULL;
2544c851ed54SJan Kara 	jinode->i_vfs_inode = inode;
2545c851ed54SJan Kara 	jinode->i_flags = 0;
2546c851ed54SJan Kara 	INIT_LIST_HEAD(&jinode->i_list);
2547c851ed54SJan Kara }
2548c851ed54SJan Kara 
2549c851ed54SJan Kara /*
2550c851ed54SJan Kara  * Function to be called before we start removing inode from memory (i.e.,
2551c851ed54SJan Kara  * clear_inode() is a fine place to be called from). It removes inode from
2552c851ed54SJan Kara  * transaction's lists.
2553c851ed54SJan Kara  */
2554c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal,
2555c851ed54SJan Kara 				    struct jbd2_inode *jinode)
2556c851ed54SJan Kara {
2557c851ed54SJan Kara 	if (!journal)
2558c851ed54SJan Kara 		return;
2559c851ed54SJan Kara restart:
2560c851ed54SJan Kara 	spin_lock(&journal->j_list_lock);
2561c851ed54SJan Kara 	/* Is commit writing out inode - we have to wait */
256239e3ac25SBrian King 	if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2563c851ed54SJan Kara 		wait_queue_head_t *wq;
2564c851ed54SJan Kara 		DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2565c851ed54SJan Kara 		wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2566c851ed54SJan Kara 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2567c851ed54SJan Kara 		spin_unlock(&journal->j_list_lock);
2568c851ed54SJan Kara 		schedule();
2569c851ed54SJan Kara 		finish_wait(wq, &wait.wait);
2570c851ed54SJan Kara 		goto restart;
2571c851ed54SJan Kara 	}
2572c851ed54SJan Kara 
2573c851ed54SJan Kara 	if (jinode->i_transaction) {
2574c851ed54SJan Kara 		list_del(&jinode->i_list);
2575c851ed54SJan Kara 		jinode->i_transaction = NULL;
2576c851ed54SJan Kara 	}
2577c851ed54SJan Kara 	spin_unlock(&journal->j_list_lock);
2578c851ed54SJan Kara }
2579c851ed54SJan Kara 
2580470decc6SDave Kleikamp 
25818e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS
25828e85fb3fSJohann Lombardi 
25838e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2"
25848e85fb3fSJohann Lombardi 
25858e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void)
25868e85fb3fSJohann Lombardi {
25878e85fb3fSJohann Lombardi 	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
25888e85fb3fSJohann Lombardi }
25898e85fb3fSJohann Lombardi 
25908e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void)
25918e85fb3fSJohann Lombardi {
25928e85fb3fSJohann Lombardi 	if (proc_jbd2_stats)
25938e85fb3fSJohann Lombardi 		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
25948e85fb3fSJohann Lombardi }
25958e85fb3fSJohann Lombardi 
25968e85fb3fSJohann Lombardi #else
25978e85fb3fSJohann Lombardi 
25988e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
25998e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
26008e85fb3fSJohann Lombardi 
26018e85fb3fSJohann Lombardi #endif
26028e85fb3fSJohann Lombardi 
26038aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2604470decc6SDave Kleikamp 
26054185a2acSYongqiang Yang static int __init jbd2_journal_init_handle_cache(void)
2606470decc6SDave Kleikamp {
26078aefcd55STheodore Ts'o 	jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2608f7f4bccbSMingming Cao 	if (jbd2_handle_cache == NULL) {
26098aefcd55STheodore Ts'o 		printk(KERN_EMERG "JBD2: failed to create handle cache\n");
26108aefcd55STheodore Ts'o 		return -ENOMEM;
26118aefcd55STheodore Ts'o 	}
26128aefcd55STheodore Ts'o 	jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
26138aefcd55STheodore Ts'o 	if (jbd2_inode_cache == NULL) {
26148aefcd55STheodore Ts'o 		printk(KERN_EMERG "JBD2: failed to create inode cache\n");
26158aefcd55STheodore Ts'o 		kmem_cache_destroy(jbd2_handle_cache);
2616470decc6SDave Kleikamp 		return -ENOMEM;
2617470decc6SDave Kleikamp 	}
2618470decc6SDave Kleikamp 	return 0;
2619470decc6SDave Kleikamp }
2620470decc6SDave Kleikamp 
2621f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void)
2622470decc6SDave Kleikamp {
2623f7f4bccbSMingming Cao 	if (jbd2_handle_cache)
2624f7f4bccbSMingming Cao 		kmem_cache_destroy(jbd2_handle_cache);
26258aefcd55STheodore Ts'o 	if (jbd2_inode_cache)
26268aefcd55STheodore Ts'o 		kmem_cache_destroy(jbd2_inode_cache);
26278aefcd55STheodore Ts'o 
2628470decc6SDave Kleikamp }
2629470decc6SDave Kleikamp 
2630470decc6SDave Kleikamp /*
2631470decc6SDave Kleikamp  * Module startup and shutdown
2632470decc6SDave Kleikamp  */
2633470decc6SDave Kleikamp 
2634470decc6SDave Kleikamp static int __init journal_init_caches(void)
2635470decc6SDave Kleikamp {
2636470decc6SDave Kleikamp 	int ret;
2637470decc6SDave Kleikamp 
2638f7f4bccbSMingming Cao 	ret = jbd2_journal_init_revoke_caches();
2639470decc6SDave Kleikamp 	if (ret == 0)
26404185a2acSYongqiang Yang 		ret = jbd2_journal_init_journal_head_cache();
2641470decc6SDave Kleikamp 	if (ret == 0)
26424185a2acSYongqiang Yang 		ret = jbd2_journal_init_handle_cache();
26430c2022ecSYongqiang Yang 	if (ret == 0)
26440c2022ecSYongqiang Yang 		ret = jbd2_journal_init_transaction_cache();
2645470decc6SDave Kleikamp 	return ret;
2646470decc6SDave Kleikamp }
2647470decc6SDave Kleikamp 
2648f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void)
2649470decc6SDave Kleikamp {
2650f7f4bccbSMingming Cao 	jbd2_journal_destroy_revoke_caches();
26514185a2acSYongqiang Yang 	jbd2_journal_destroy_journal_head_cache();
2652f7f4bccbSMingming Cao 	jbd2_journal_destroy_handle_cache();
26530c2022ecSYongqiang Yang 	jbd2_journal_destroy_transaction_cache();
2654d2eecb03STheodore Ts'o 	jbd2_journal_destroy_slabs();
2655470decc6SDave Kleikamp }
2656470decc6SDave Kleikamp 
2657470decc6SDave Kleikamp static int __init journal_init(void)
2658470decc6SDave Kleikamp {
2659470decc6SDave Kleikamp 	int ret;
2660470decc6SDave Kleikamp 
2661470decc6SDave Kleikamp 	BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2662470decc6SDave Kleikamp 
2663470decc6SDave Kleikamp 	ret = journal_init_caches();
2664620de4e1SDuane Griffin 	if (ret == 0) {
26658e85fb3fSJohann Lombardi 		jbd2_create_jbd_stats_proc_entry();
2666620de4e1SDuane Griffin 	} else {
2667620de4e1SDuane Griffin 		jbd2_journal_destroy_caches();
2668620de4e1SDuane Griffin 	}
2669470decc6SDave Kleikamp 	return ret;
2670470decc6SDave Kleikamp }
2671470decc6SDave Kleikamp 
2672470decc6SDave Kleikamp static void __exit journal_exit(void)
2673470decc6SDave Kleikamp {
2674e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2675470decc6SDave Kleikamp 	int n = atomic_read(&nr_journal_heads);
2676470decc6SDave Kleikamp 	if (n)
267775685071SJan Kara 		printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
2678470decc6SDave Kleikamp #endif
26798e85fb3fSJohann Lombardi 	jbd2_remove_jbd_stats_proc_entry();
2680f7f4bccbSMingming Cao 	jbd2_journal_destroy_caches();
2681470decc6SDave Kleikamp }
2682470decc6SDave Kleikamp 
2683470decc6SDave Kleikamp MODULE_LICENSE("GPL");
2684470decc6SDave Kleikamp module_init(journal_init);
2685470decc6SDave Kleikamp module_exit(journal_exit);
2686470decc6SDave Kleikamp 
2687