xref: /openbmc/linux/fs/jbd2/journal.c (revision 5c480a69)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0+
2470decc6SDave Kleikamp /*
3f7f4bccbSMingming Cao  * linux/fs/jbd2/journal.c
4470decc6SDave Kleikamp  *
5470decc6SDave Kleikamp  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6470decc6SDave Kleikamp  *
7470decc6SDave Kleikamp  * Copyright 1998 Red Hat corp --- All Rights Reserved
8470decc6SDave Kleikamp  *
9470decc6SDave Kleikamp  * Generic filesystem journal-writing code; part of the ext2fs
10470decc6SDave Kleikamp  * journaling system.
11470decc6SDave Kleikamp  *
12470decc6SDave Kleikamp  * This file manages journals: areas of disk reserved for logging
13470decc6SDave Kleikamp  * transactional updates.  This includes the kernel journaling thread
14470decc6SDave Kleikamp  * which is responsible for scheduling updates to the log.
15470decc6SDave Kleikamp  *
16470decc6SDave Kleikamp  * We do not actually manage the physical storage of the journal in this
17470decc6SDave Kleikamp  * file: that is left to a per-journal policy function, which allows us
18470decc6SDave Kleikamp  * to store the journal within a filesystem-specified area for ext2
19470decc6SDave Kleikamp  * journaling (ext2 can use a reserved inode for storing the log).
20470decc6SDave Kleikamp  */
21470decc6SDave Kleikamp 
22470decc6SDave Kleikamp #include <linux/module.h>
23470decc6SDave Kleikamp #include <linux/time.h>
24470decc6SDave Kleikamp #include <linux/fs.h>
25f7f4bccbSMingming Cao #include <linux/jbd2.h>
26470decc6SDave Kleikamp #include <linux/errno.h>
27470decc6SDave Kleikamp #include <linux/slab.h>
28470decc6SDave Kleikamp #include <linux/init.h>
29470decc6SDave Kleikamp #include <linux/mm.h>
307dfb7103SNigel Cunningham #include <linux/freezer.h>
31470decc6SDave Kleikamp #include <linux/pagemap.h>
32470decc6SDave Kleikamp #include <linux/kthread.h>
33470decc6SDave Kleikamp #include <linux/poison.h>
34470decc6SDave Kleikamp #include <linux/proc_fs.h>
358e85fb3fSJohann Lombardi #include <linux/seq_file.h>
36c225aa57SSimon Holm Thøgersen #include <linux/math64.h>
37879c5e6bSTheodore Ts'o #include <linux/hash.h>
38d2eecb03STheodore Ts'o #include <linux/log2.h>
39d2eecb03STheodore Ts'o #include <linux/vmalloc.h>
4047def826STheodore Ts'o #include <linux/backing-dev.h>
4139e3ac25SBrian King #include <linux/bitops.h>
42670be5a7STheodore Ts'o #include <linux/ratelimit.h>
43eb52da3fSMichal Hocko #include <linux/sched/mm.h>
44879c5e6bSTheodore Ts'o 
45879c5e6bSTheodore Ts'o #define CREATE_TRACE_POINTS
46879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h>
47470decc6SDave Kleikamp 
487c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
49470decc6SDave Kleikamp #include <asm/page.h>
50470decc6SDave Kleikamp 
51b6e96d00STheodore Ts'o #ifdef CONFIG_JBD2_DEBUG
5268af74e9SJan Kara static ushort jbd2_journal_enable_debug __read_mostly;
53b6e96d00STheodore Ts'o 
54b6e96d00STheodore Ts'o module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
55b6e96d00STheodore Ts'o MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
56b6e96d00STheodore Ts'o #endif
57b6e96d00STheodore Ts'o 
58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend);
59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop);
60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates);
61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates);
62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access);
63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access);
64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access);
65e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers);
66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget);
68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush);
69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke);
70470decc6SDave Kleikamp 
71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev);
72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode);
73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features);
74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features);
75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features);
76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load);
77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy);
78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort);
79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno);
80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err);
81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err);
82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit);
83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit);
84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe);
86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
87ccd16945SMatthew Wilcox (Oracle) EXPORT_SYMBOL(jbd2_journal_invalidate_folio);
88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit);
906ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_write);
916ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait);
92aa3c0c61SMauricio Faria de Oliveira EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers);
93c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
94c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
95c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
968aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache);
97470decc6SDave Kleikamp 
98d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size);
99470decc6SDave Kleikamp 
100169f1a2aSPaul Gortmaker #ifdef CONFIG_JBD2_DEBUG
__jbd2_debug(int level,const char * file,const char * func,unsigned int line,const char * fmt,...)101169f1a2aSPaul Gortmaker void __jbd2_debug(int level, const char *file, const char *func,
102169f1a2aSPaul Gortmaker 		  unsigned int line, const char *fmt, ...)
103169f1a2aSPaul Gortmaker {
104169f1a2aSPaul Gortmaker 	struct va_format vaf;
105169f1a2aSPaul Gortmaker 	va_list args;
106169f1a2aSPaul Gortmaker 
107169f1a2aSPaul Gortmaker 	if (level > jbd2_journal_enable_debug)
108169f1a2aSPaul Gortmaker 		return;
109169f1a2aSPaul Gortmaker 	va_start(args, fmt);
110169f1a2aSPaul Gortmaker 	vaf.fmt = fmt;
111169f1a2aSPaul Gortmaker 	vaf.va = &args;
1129196f571SWang Shilong 	printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf);
113169f1a2aSPaul Gortmaker 	va_end(args);
114169f1a2aSPaul Gortmaker }
115169f1a2aSPaul Gortmaker #endif
116169f1a2aSPaul Gortmaker 
11725ed6e8aSDarrick J. Wong /* Checksumming functions */
jbd2_superblock_csum(journal_t * j,journal_superblock_t * sb)11818a6ea1eSDarrick J. Wong static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
1194fd5ea43SDarrick J. Wong {
12018a6ea1eSDarrick J. Wong 	__u32 csum;
12118a6ea1eSDarrick J. Wong 	__be32 old_csum;
1224fd5ea43SDarrick J. Wong 
1234fd5ea43SDarrick J. Wong 	old_csum = sb->s_checksum;
1244fd5ea43SDarrick J. Wong 	sb->s_checksum = 0;
1254fd5ea43SDarrick J. Wong 	csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
1264fd5ea43SDarrick J. Wong 	sb->s_checksum = old_csum;
1274fd5ea43SDarrick J. Wong 
1284fd5ea43SDarrick J. Wong 	return cpu_to_be32(csum);
1294fd5ea43SDarrick J. Wong }
1304fd5ea43SDarrick J. Wong 
131470decc6SDave Kleikamp /*
132470decc6SDave Kleikamp  * Helper function used to manage commit timeouts
133470decc6SDave Kleikamp  */
134470decc6SDave Kleikamp 
commit_timeout(struct timer_list * t)135e3c95788SKees Cook static void commit_timeout(struct timer_list *t)
136470decc6SDave Kleikamp {
137e3c95788SKees Cook 	journal_t *journal = from_timer(journal, t, j_commit_timer);
138470decc6SDave Kleikamp 
139e3c95788SKees Cook 	wake_up_process(journal->j_task);
140470decc6SDave Kleikamp }
141470decc6SDave Kleikamp 
142470decc6SDave Kleikamp /*
143f7f4bccbSMingming Cao  * kjournald2: The main thread function used to manage a logging device
144470decc6SDave Kleikamp  * journal.
145470decc6SDave Kleikamp  *
146470decc6SDave Kleikamp  * This kernel thread is responsible for two things:
147470decc6SDave Kleikamp  *
148470decc6SDave Kleikamp  * 1) COMMIT:  Every so often we need to commit the current state of the
149470decc6SDave Kleikamp  *    filesystem to disk.  The journal thread is responsible for writing
150ff780b91SHarshad Shirwadkar  *    all of the metadata buffers to disk. If a fast commit is ongoing
151ff780b91SHarshad Shirwadkar  *    journal thread waits until it's done and then continues from
152ff780b91SHarshad Shirwadkar  *    there on.
153470decc6SDave Kleikamp  *
154470decc6SDave Kleikamp  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
155470decc6SDave Kleikamp  *    of the data in that part of the log has been rewritten elsewhere on
156470decc6SDave Kleikamp  *    the disk.  Flushing these old buffers to reclaim space in the log is
157470decc6SDave Kleikamp  *    known as checkpointing, and this thread is responsible for that job.
158470decc6SDave Kleikamp  */
159470decc6SDave Kleikamp 
kjournald2(void * arg)160f7f4bccbSMingming Cao static int kjournald2(void *arg)
161470decc6SDave Kleikamp {
162470decc6SDave Kleikamp 	journal_t *journal = arg;
163470decc6SDave Kleikamp 	transaction_t *transaction;
164470decc6SDave Kleikamp 
165470decc6SDave Kleikamp 	/*
166470decc6SDave Kleikamp 	 * Set up an interval timer which can be used to trigger a commit wakeup
167470decc6SDave Kleikamp 	 * after the commit interval expires
168470decc6SDave Kleikamp 	 */
169e3c95788SKees Cook 	timer_setup(&journal->j_commit_timer, commit_timeout, 0);
170470decc6SDave Kleikamp 
17135c80422SNigel Cunningham 	set_freezable();
17235c80422SNigel Cunningham 
173470decc6SDave Kleikamp 	/* Record that the journal thread is running */
174470decc6SDave Kleikamp 	journal->j_task = current;
175470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
176470decc6SDave Kleikamp 
177470decc6SDave Kleikamp 	/*
178eb52da3fSMichal Hocko 	 * Make sure that no allocations from this kernel thread will ever
179eb52da3fSMichal Hocko 	 * recurse to the fs layer because we are responsible for the
180eb52da3fSMichal Hocko 	 * transaction commit and any fs involvement might get stuck waiting for
181eb52da3fSMichal Hocko 	 * the trasn. commit.
182eb52da3fSMichal Hocko 	 */
183eb52da3fSMichal Hocko 	memalloc_nofs_save();
184eb52da3fSMichal Hocko 
185eb52da3fSMichal Hocko 	/*
186470decc6SDave Kleikamp 	 * And now, wait forever for commit wakeup events.
187470decc6SDave Kleikamp 	 */
188a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
189470decc6SDave Kleikamp 
190470decc6SDave Kleikamp loop:
191f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_UNMOUNT)
192470decc6SDave Kleikamp 		goto end_loop;
193470decc6SDave Kleikamp 
194cb3b3bf2SJan Kara 	jbd2_debug(1, "commit_sequence=%u, commit_request=%u\n",
195470decc6SDave Kleikamp 		journal->j_commit_sequence, journal->j_commit_request);
196470decc6SDave Kleikamp 
197470decc6SDave Kleikamp 	if (journal->j_commit_sequence != journal->j_commit_request) {
198cb3b3bf2SJan Kara 		jbd2_debug(1, "OK, requests differ\n");
199a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
200470decc6SDave Kleikamp 		del_timer_sync(&journal->j_commit_timer);
201f7f4bccbSMingming Cao 		jbd2_journal_commit_transaction(journal);
202a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
203470decc6SDave Kleikamp 		goto loop;
204470decc6SDave Kleikamp 	}
205470decc6SDave Kleikamp 
206470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
207470decc6SDave Kleikamp 	if (freezing(current)) {
208470decc6SDave Kleikamp 		/*
209470decc6SDave Kleikamp 		 * The simpler the better. Flushing journal isn't a
210470decc6SDave Kleikamp 		 * good idea, because that depends on threads that may
211470decc6SDave Kleikamp 		 * be already stopped.
212470decc6SDave Kleikamp 		 */
213cb3b3bf2SJan Kara 		jbd2_debug(1, "Now suspending kjournald2\n");
214a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
215a0acae0eSTejun Heo 		try_to_freeze();
216a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
217470decc6SDave Kleikamp 	} else {
218470decc6SDave Kleikamp 		/*
219470decc6SDave Kleikamp 		 * We assume on resume that commits are already there,
220470decc6SDave Kleikamp 		 * so we don't sleep
221470decc6SDave Kleikamp 		 */
222470decc6SDave Kleikamp 		DEFINE_WAIT(wait);
223470decc6SDave Kleikamp 		int should_sleep = 1;
224470decc6SDave Kleikamp 
225470decc6SDave Kleikamp 		prepare_to_wait(&journal->j_wait_commit, &wait,
226470decc6SDave Kleikamp 				TASK_INTERRUPTIBLE);
227470decc6SDave Kleikamp 		if (journal->j_commit_sequence != journal->j_commit_request)
228470decc6SDave Kleikamp 			should_sleep = 0;
229470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
230470decc6SDave Kleikamp 		if (transaction && time_after_eq(jiffies,
231470decc6SDave Kleikamp 						transaction->t_expires))
232470decc6SDave Kleikamp 			should_sleep = 0;
233f7f4bccbSMingming Cao 		if (journal->j_flags & JBD2_UNMOUNT)
234470decc6SDave Kleikamp 			should_sleep = 0;
235470decc6SDave Kleikamp 		if (should_sleep) {
236a931da6aSTheodore Ts'o 			write_unlock(&journal->j_state_lock);
237470decc6SDave Kleikamp 			schedule();
238a931da6aSTheodore Ts'o 			write_lock(&journal->j_state_lock);
239470decc6SDave Kleikamp 		}
240470decc6SDave Kleikamp 		finish_wait(&journal->j_wait_commit, &wait);
241470decc6SDave Kleikamp 	}
242470decc6SDave Kleikamp 
243cb3b3bf2SJan Kara 	jbd2_debug(1, "kjournald2 wakes\n");
244470decc6SDave Kleikamp 
245470decc6SDave Kleikamp 	/*
246470decc6SDave Kleikamp 	 * Were we woken up by a commit wakeup event?
247470decc6SDave Kleikamp 	 */
248470decc6SDave Kleikamp 	transaction = journal->j_running_transaction;
249470decc6SDave Kleikamp 	if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
250470decc6SDave Kleikamp 		journal->j_commit_request = transaction->t_tid;
251cb3b3bf2SJan Kara 		jbd2_debug(1, "woke because of timeout\n");
252470decc6SDave Kleikamp 	}
253470decc6SDave Kleikamp 	goto loop;
254470decc6SDave Kleikamp 
255470decc6SDave Kleikamp end_loop:
256470decc6SDave Kleikamp 	del_timer_sync(&journal->j_commit_timer);
257470decc6SDave Kleikamp 	journal->j_task = NULL;
258470decc6SDave Kleikamp 	wake_up(&journal->j_wait_done_commit);
259cb3b3bf2SJan Kara 	jbd2_debug(1, "Journal thread exiting.\n");
260dbfcef6bSSahitya Tummala 	write_unlock(&journal->j_state_lock);
261470decc6SDave Kleikamp 	return 0;
262470decc6SDave Kleikamp }
263470decc6SDave Kleikamp 
jbd2_journal_start_thread(journal_t * journal)26497f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal)
265470decc6SDave Kleikamp {
26697f06784SPavel Emelianov 	struct task_struct *t;
26797f06784SPavel Emelianov 
26890576c0bSTheodore Ts'o 	t = kthread_run(kjournald2, journal, "jbd2/%s",
26990576c0bSTheodore Ts'o 			journal->j_devname);
27097f06784SPavel Emelianov 	if (IS_ERR(t))
27197f06784SPavel Emelianov 		return PTR_ERR(t);
27297f06784SPavel Emelianov 
2731076d17aSAl Viro 	wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
27497f06784SPavel Emelianov 	return 0;
275470decc6SDave Kleikamp }
276470decc6SDave Kleikamp 
journal_kill_thread(journal_t * journal)277470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal)
278470decc6SDave Kleikamp {
279a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
280f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_UNMOUNT;
281470decc6SDave Kleikamp 
282470decc6SDave Kleikamp 	while (journal->j_task) {
283a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
2843469a32aSTheodore Ts'o 		wake_up(&journal->j_wait_commit);
2851076d17aSAl Viro 		wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
286a931da6aSTheodore Ts'o 		write_lock(&journal->j_state_lock);
287470decc6SDave Kleikamp 	}
288a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
289470decc6SDave Kleikamp }
290470decc6SDave Kleikamp 
291470decc6SDave Kleikamp /*
292f7f4bccbSMingming Cao  * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
293470decc6SDave Kleikamp  *
294470decc6SDave Kleikamp  * Writes a metadata buffer to a given disk block.  The actual IO is not
295470decc6SDave Kleikamp  * performed but a new buffer_head is constructed which labels the data
296470decc6SDave Kleikamp  * to be written with the correct destination disk block.
297470decc6SDave Kleikamp  *
298470decc6SDave Kleikamp  * Any magic-number escaping which needs to be done will cause a
299470decc6SDave Kleikamp  * copy-out here.  If the buffer happens to start with the
300f7f4bccbSMingming Cao  * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
301470decc6SDave Kleikamp  * magic number is only written to the log for descripter blocks.  In
302470decc6SDave Kleikamp  * this case, we copy the data and replace the first word with 0, and we
303470decc6SDave Kleikamp  * return a result code which indicates that this buffer needs to be
304470decc6SDave Kleikamp  * marked as an escaped buffer in the corresponding log descriptor
305470decc6SDave Kleikamp  * block.  The missing word can then be restored when the block is read
306470decc6SDave Kleikamp  * during recovery.
307470decc6SDave Kleikamp  *
308470decc6SDave Kleikamp  * If the source buffer has already been modified by a new transaction
309470decc6SDave Kleikamp  * since we took the last commit snapshot, we use the frozen copy of
310470decc6SDave Kleikamp  * that data for IO. If we end up using the existing buffer_head's data
311f5113effSJan Kara  * for the write, then we have to make sure nobody modifies it while the
312f5113effSJan Kara  * IO is in progress. do_get_write_access() handles this.
313470decc6SDave Kleikamp  *
314f5113effSJan Kara  * The function returns a pointer to the buffer_head to be used for IO.
315470decc6SDave Kleikamp  *
316470decc6SDave Kleikamp  *
317470decc6SDave Kleikamp  * Return value:
318470decc6SDave Kleikamp  *  <0: Error
319470decc6SDave Kleikamp  * >=0: Finished OK
320470decc6SDave Kleikamp  *
321470decc6SDave Kleikamp  * On success:
322470decc6SDave Kleikamp  * Bit 0 set == escape performed on the data
323470decc6SDave Kleikamp  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
324470decc6SDave Kleikamp  */
325470decc6SDave Kleikamp 
jbd2_journal_write_metadata_buffer(transaction_t * transaction,struct journal_head * jh_in,struct buffer_head ** bh_out,sector_t blocknr)326f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
327470decc6SDave Kleikamp 				  struct journal_head  *jh_in,
328f5113effSJan Kara 				  struct buffer_head **bh_out,
329f5113effSJan Kara 				  sector_t blocknr)
330470decc6SDave Kleikamp {
331470decc6SDave Kleikamp 	int need_copy_out = 0;
332470decc6SDave Kleikamp 	int done_copy_out = 0;
333470decc6SDave Kleikamp 	int do_escape = 0;
334470decc6SDave Kleikamp 	char *mapped_data;
335470decc6SDave Kleikamp 	struct buffer_head *new_bh;
3368147c4c4SMatthew Wilcox (Oracle) 	struct folio *new_folio;
337470decc6SDave Kleikamp 	unsigned int new_offset;
338470decc6SDave Kleikamp 	struct buffer_head *bh_in = jh2bh(jh_in);
33996577c43Sdingdinghua 	journal_t *journal = transaction->t_journal;
340470decc6SDave Kleikamp 
341470decc6SDave Kleikamp 	/*
342470decc6SDave Kleikamp 	 * The buffer really shouldn't be locked: only the current committing
343470decc6SDave Kleikamp 	 * transaction is allowed to write it, so nobody else is allowed
344470decc6SDave Kleikamp 	 * to do any IO.
345470decc6SDave Kleikamp 	 *
346470decc6SDave Kleikamp 	 * akpm: except if we're journalling data, and write() output is
347470decc6SDave Kleikamp 	 * also part of a shared mapping, and another thread has
348470decc6SDave Kleikamp 	 * decided to launch a writepage() against this buffer.
349470decc6SDave Kleikamp 	 */
350470decc6SDave Kleikamp 	J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
351470decc6SDave Kleikamp 
3526ccaf3e2SMichal Hocko 	new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
35347def826STheodore Ts'o 
35496577c43Sdingdinghua 	/* keep subsequent assertions sane */
35596577c43Sdingdinghua 	atomic_set(&new_bh->b_count, 1);
356470decc6SDave Kleikamp 
35746417064SThomas Gleixner 	spin_lock(&jh_in->b_state_lock);
358f5113effSJan Kara repeat:
359470decc6SDave Kleikamp 	/*
360470decc6SDave Kleikamp 	 * If a new transaction has already done a buffer copy-out, then
361470decc6SDave Kleikamp 	 * we use that version of the data for the commit.
362470decc6SDave Kleikamp 	 */
363470decc6SDave Kleikamp 	if (jh_in->b_frozen_data) {
364470decc6SDave Kleikamp 		done_copy_out = 1;
3658147c4c4SMatthew Wilcox (Oracle) 		new_folio = virt_to_folio(jh_in->b_frozen_data);
3668147c4c4SMatthew Wilcox (Oracle) 		new_offset = offset_in_folio(new_folio, jh_in->b_frozen_data);
367470decc6SDave Kleikamp 	} else {
3688147c4c4SMatthew Wilcox (Oracle) 		new_folio = jh2bh(jh_in)->b_folio;
3698147c4c4SMatthew Wilcox (Oracle) 		new_offset = offset_in_folio(new_folio, jh2bh(jh_in)->b_data);
370470decc6SDave Kleikamp 	}
371470decc6SDave Kleikamp 
3728147c4c4SMatthew Wilcox (Oracle) 	mapped_data = kmap_local_folio(new_folio, new_offset);
373470decc6SDave Kleikamp 	/*
37413ceef09SJan Kara 	 * Fire data frozen trigger if data already wasn't frozen.  Do this
37513ceef09SJan Kara 	 * before checking for escaping, as the trigger may modify the magic
37613ceef09SJan Kara 	 * offset.  If a copy-out happens afterwards, it will have the correct
37713ceef09SJan Kara 	 * data in the buffer.
378e06c8227SJoel Becker 	 */
37913ceef09SJan Kara 	if (!done_copy_out)
3808147c4c4SMatthew Wilcox (Oracle) 		jbd2_buffer_frozen_trigger(jh_in, mapped_data,
38113ceef09SJan Kara 					   jh_in->b_triggers);
382e06c8227SJoel Becker 
383e06c8227SJoel Becker 	/*
384470decc6SDave Kleikamp 	 * Check for escaping
385470decc6SDave Kleikamp 	 */
3868147c4c4SMatthew Wilcox (Oracle) 	if (*((__be32 *)mapped_data) == cpu_to_be32(JBD2_MAGIC_NUMBER)) {
387470decc6SDave Kleikamp 		need_copy_out = 1;
388470decc6SDave Kleikamp 		do_escape = 1;
389470decc6SDave Kleikamp 	}
3908147c4c4SMatthew Wilcox (Oracle) 	kunmap_local(mapped_data);
391470decc6SDave Kleikamp 
392470decc6SDave Kleikamp 	/*
393470decc6SDave Kleikamp 	 * Do we need to do a data copy?
394470decc6SDave Kleikamp 	 */
395470decc6SDave Kleikamp 	if (need_copy_out && !done_copy_out) {
396470decc6SDave Kleikamp 		char *tmp;
397470decc6SDave Kleikamp 
39846417064SThomas Gleixner 		spin_unlock(&jh_in->b_state_lock);
399af1e76d6SMingming Cao 		tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
400e6ec116bSTheodore Ts'o 		if (!tmp) {
401f5113effSJan Kara 			brelse(new_bh);
402e6ec116bSTheodore Ts'o 			return -ENOMEM;
403e6ec116bSTheodore Ts'o 		}
40446417064SThomas Gleixner 		spin_lock(&jh_in->b_state_lock);
405470decc6SDave Kleikamp 		if (jh_in->b_frozen_data) {
406af1e76d6SMingming Cao 			jbd2_free(tmp, bh_in->b_size);
407470decc6SDave Kleikamp 			goto repeat;
408470decc6SDave Kleikamp 		}
409470decc6SDave Kleikamp 
410470decc6SDave Kleikamp 		jh_in->b_frozen_data = tmp;
4118147c4c4SMatthew Wilcox (Oracle) 		memcpy_from_folio(tmp, new_folio, new_offset, bh_in->b_size);
412470decc6SDave Kleikamp 
4138147c4c4SMatthew Wilcox (Oracle) 		new_folio = virt_to_folio(tmp);
4148147c4c4SMatthew Wilcox (Oracle) 		new_offset = offset_in_folio(new_folio, tmp);
415470decc6SDave Kleikamp 		done_copy_out = 1;
416e06c8227SJoel Becker 
417e06c8227SJoel Becker 		/*
418e06c8227SJoel Becker 		 * This isn't strictly necessary, as we're using frozen
419e06c8227SJoel Becker 		 * data for the escaping, but it keeps consistency with
420e06c8227SJoel Becker 		 * b_frozen_data usage.
421e06c8227SJoel Becker 		 */
422e06c8227SJoel Becker 		jh_in->b_frozen_triggers = jh_in->b_triggers;
423470decc6SDave Kleikamp 	}
424470decc6SDave Kleikamp 
425470decc6SDave Kleikamp 	/*
426470decc6SDave Kleikamp 	 * Did we need to do an escaping?  Now we've done all the
427470decc6SDave Kleikamp 	 * copying, we can finally do so.
428470decc6SDave Kleikamp 	 */
429470decc6SDave Kleikamp 	if (do_escape) {
4308147c4c4SMatthew Wilcox (Oracle) 		mapped_data = kmap_local_folio(new_folio, new_offset);
4318147c4c4SMatthew Wilcox (Oracle) 		*((unsigned int *)mapped_data) = 0;
4328147c4c4SMatthew Wilcox (Oracle) 		kunmap_local(mapped_data);
433470decc6SDave Kleikamp 	}
434470decc6SDave Kleikamp 
4358147c4c4SMatthew Wilcox (Oracle) 	folio_set_bh(new_bh, new_folio, new_offset);
436f5113effSJan Kara 	new_bh->b_size = bh_in->b_size;
437f5113effSJan Kara 	new_bh->b_bdev = journal->j_dev;
438470decc6SDave Kleikamp 	new_bh->b_blocknr = blocknr;
439b34090e5SJan Kara 	new_bh->b_private = bh_in;
440470decc6SDave Kleikamp 	set_buffer_mapped(new_bh);
441470decc6SDave Kleikamp 	set_buffer_dirty(new_bh);
442470decc6SDave Kleikamp 
443f5113effSJan Kara 	*bh_out = new_bh;
444470decc6SDave Kleikamp 
445470decc6SDave Kleikamp 	/*
446470decc6SDave Kleikamp 	 * The to-be-written buffer needs to get moved to the io queue,
447470decc6SDave Kleikamp 	 * and the original buffer whose contents we are shadowing or
448470decc6SDave Kleikamp 	 * copying is moved to the transaction's shadow queue.
449470decc6SDave Kleikamp 	 */
450470decc6SDave Kleikamp 	JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
45196577c43Sdingdinghua 	spin_lock(&journal->j_list_lock);
45296577c43Sdingdinghua 	__jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
45396577c43Sdingdinghua 	spin_unlock(&journal->j_list_lock);
454b34090e5SJan Kara 	set_buffer_shadow(bh_in);
45546417064SThomas Gleixner 	spin_unlock(&jh_in->b_state_lock);
45696577c43Sdingdinghua 
457470decc6SDave Kleikamp 	return do_escape | (done_copy_out << 1);
458470decc6SDave Kleikamp }
459470decc6SDave Kleikamp 
460470decc6SDave Kleikamp /*
461470decc6SDave Kleikamp  * Allocation code for the journal file.  Manage the space left in the
462470decc6SDave Kleikamp  * journal, so that we can begin checkpointing when appropriate.
463470decc6SDave Kleikamp  */
464470decc6SDave Kleikamp 
465470decc6SDave Kleikamp /*
466e4471831STheodore Ts'o  * Called with j_state_lock locked for writing.
467e4471831STheodore Ts'o  * Returns true if a transaction commit was started.
468470decc6SDave Kleikamp  */
__jbd2_log_start_commit(journal_t * journal,tid_t target)469d1324958SJan Kara static int __jbd2_log_start_commit(journal_t *journal, tid_t target)
470470decc6SDave Kleikamp {
471e7b04ac0SEric Sandeen 	/* Return if the txn has already requested to be committed */
472e7b04ac0SEric Sandeen 	if (journal->j_commit_request == target)
473e7b04ac0SEric Sandeen 		return 0;
474e7b04ac0SEric Sandeen 
475470decc6SDave Kleikamp 	/*
476deeeaf13STheodore Ts'o 	 * The only transaction we can possibly wait upon is the
477deeeaf13STheodore Ts'o 	 * currently running transaction (if it exists).  Otherwise,
478deeeaf13STheodore Ts'o 	 * the target tid must be an old one.
479470decc6SDave Kleikamp 	 */
480deeeaf13STheodore Ts'o 	if (journal->j_running_transaction &&
481deeeaf13STheodore Ts'o 	    journal->j_running_transaction->t_tid == target) {
482470decc6SDave Kleikamp 		/*
483bcf3d0bcSAndrea Gelmini 		 * We want a new commit: OK, mark the request and wakeup the
484470decc6SDave Kleikamp 		 * commit thread.  We do _not_ do the commit ourselves.
485470decc6SDave Kleikamp 		 */
486470decc6SDave Kleikamp 
487470decc6SDave Kleikamp 		journal->j_commit_request = target;
488cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: requesting commit %u/%u\n",
489470decc6SDave Kleikamp 			  journal->j_commit_request,
490470decc6SDave Kleikamp 			  journal->j_commit_sequence);
4919fff24aaSTheodore Ts'o 		journal->j_running_transaction->t_requested = jiffies;
492470decc6SDave Kleikamp 		wake_up(&journal->j_wait_commit);
493470decc6SDave Kleikamp 		return 1;
494deeeaf13STheodore Ts'o 	} else if (!tid_geq(journal->j_commit_request, target))
495deeeaf13STheodore Ts'o 		/* This should never happen, but if it does, preserve
496deeeaf13STheodore Ts'o 		   the evidence before kjournald goes into a loop and
497deeeaf13STheodore Ts'o 		   increments j_commit_sequence beyond all recognition. */
498f2a44523SEryu Guan 		WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
4991be2add6STheodore Ts'o 			  journal->j_commit_request,
5001be2add6STheodore Ts'o 			  journal->j_commit_sequence,
501deeeaf13STheodore Ts'o 			  target, journal->j_running_transaction ?
502deeeaf13STheodore Ts'o 			  journal->j_running_transaction->t_tid : 0);
503470decc6SDave Kleikamp 	return 0;
504470decc6SDave Kleikamp }
505470decc6SDave Kleikamp 
jbd2_log_start_commit(journal_t * journal,tid_t tid)506f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid)
507470decc6SDave Kleikamp {
508470decc6SDave Kleikamp 	int ret;
509470decc6SDave Kleikamp 
510a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
511f7f4bccbSMingming Cao 	ret = __jbd2_log_start_commit(journal, tid);
512a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
513470decc6SDave Kleikamp 	return ret;
514470decc6SDave Kleikamp }
515470decc6SDave Kleikamp 
516470decc6SDave Kleikamp /*
5179ff86446SDmitry Monakhov  * Force and wait any uncommitted transactions.  We can only force the running
5189ff86446SDmitry Monakhov  * transaction if we don't have an active handle, otherwise, we will deadlock.
5199ff86446SDmitry Monakhov  * Returns: <0 in case of error,
5209ff86446SDmitry Monakhov  *           0 if nothing to commit,
5219ff86446SDmitry Monakhov  *           1 if transaction was successfully committed.
522470decc6SDave Kleikamp  */
__jbd2_journal_force_commit(journal_t * journal)5239ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal)
524470decc6SDave Kleikamp {
525470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
526470decc6SDave Kleikamp 	tid_t tid;
5279ff86446SDmitry Monakhov 	int need_to_start = 0, ret = 0;
528470decc6SDave Kleikamp 
529a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
530470decc6SDave Kleikamp 	if (journal->j_running_transaction && !current->journal_info) {
531470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
532e4471831STheodore Ts'o 		if (!tid_geq(journal->j_commit_request, transaction->t_tid))
533e4471831STheodore Ts'o 			need_to_start = 1;
534470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
535470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
536470decc6SDave Kleikamp 
537470decc6SDave Kleikamp 	if (!transaction) {
5389ff86446SDmitry Monakhov 		/* Nothing to commit */
539a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
5409ff86446SDmitry Monakhov 		return 0;
541470decc6SDave Kleikamp 	}
542470decc6SDave Kleikamp 	tid = transaction->t_tid;
543a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
544e4471831STheodore Ts'o 	if (need_to_start)
545e4471831STheodore Ts'o 		jbd2_log_start_commit(journal, tid);
5469ff86446SDmitry Monakhov 	ret = jbd2_log_wait_commit(journal, tid);
5479ff86446SDmitry Monakhov 	if (!ret)
5489ff86446SDmitry Monakhov 		ret = 1;
5499ff86446SDmitry Monakhov 
5509ff86446SDmitry Monakhov 	return ret;
5519ff86446SDmitry Monakhov }
5529ff86446SDmitry Monakhov 
5539ff86446SDmitry Monakhov /**
5542bf31d94SMauro Carvalho Chehab  * jbd2_journal_force_commit_nested - Force and wait upon a commit if the
5552bf31d94SMauro Carvalho Chehab  * calling process is not within transaction.
5569ff86446SDmitry Monakhov  *
5579ff86446SDmitry Monakhov  * @journal: journal to force
5589ff86446SDmitry Monakhov  * Returns true if progress was made.
5592bf31d94SMauro Carvalho Chehab  *
5602bf31d94SMauro Carvalho Chehab  * This is used for forcing out undo-protected data which contains
5612bf31d94SMauro Carvalho Chehab  * bitmaps, when the fs is running out of space.
5629ff86446SDmitry Monakhov  */
jbd2_journal_force_commit_nested(journal_t * journal)5639ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal)
5649ff86446SDmitry Monakhov {
5659ff86446SDmitry Monakhov 	int ret;
5669ff86446SDmitry Monakhov 
5679ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
5689ff86446SDmitry Monakhov 	return ret > 0;
5699ff86446SDmitry Monakhov }
5709ff86446SDmitry Monakhov 
5719ff86446SDmitry Monakhov /**
5722bf31d94SMauro Carvalho Chehab  * jbd2_journal_force_commit() - force any uncommitted transactions
5739ff86446SDmitry Monakhov  * @journal: journal to force
5749ff86446SDmitry Monakhov  *
5759ff86446SDmitry Monakhov  * Caller want unconditional commit. We can only force the running transaction
5769ff86446SDmitry Monakhov  * if we don't have an active handle, otherwise, we will deadlock.
5779ff86446SDmitry Monakhov  */
jbd2_journal_force_commit(journal_t * journal)5789ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal)
5799ff86446SDmitry Monakhov {
5809ff86446SDmitry Monakhov 	int ret;
5819ff86446SDmitry Monakhov 
5829ff86446SDmitry Monakhov 	J_ASSERT(!current->journal_info);
5839ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
5849ff86446SDmitry Monakhov 	if (ret > 0)
5859ff86446SDmitry Monakhov 		ret = 0;
5869ff86446SDmitry Monakhov 	return ret;
587470decc6SDave Kleikamp }
588470decc6SDave Kleikamp 
589470decc6SDave Kleikamp /*
590470decc6SDave Kleikamp  * Start a commit of the current running transaction (if any).  Returns true
591c88ccea3SJan Kara  * if a transaction is going to be committed (or is currently already
592c88ccea3SJan Kara  * committing), and fills its tid in at *ptid
593470decc6SDave Kleikamp  */
jbd2_journal_start_commit(journal_t * journal,tid_t * ptid)594f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
595470decc6SDave Kleikamp {
596470decc6SDave Kleikamp 	int ret = 0;
597470decc6SDave Kleikamp 
598a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
599470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
600470decc6SDave Kleikamp 		tid_t tid = journal->j_running_transaction->t_tid;
601470decc6SDave Kleikamp 
602c88ccea3SJan Kara 		__jbd2_log_start_commit(journal, tid);
603c88ccea3SJan Kara 		/* There's a running transaction and we've just made sure
604c88ccea3SJan Kara 		 * it's commit has been scheduled. */
605c88ccea3SJan Kara 		if (ptid)
606470decc6SDave Kleikamp 			*ptid = tid;
607c88ccea3SJan Kara 		ret = 1;
608c88ccea3SJan Kara 	} else if (journal->j_committing_transaction) {
609470decc6SDave Kleikamp 		/*
61012810ad7SArtem Bityutskiy 		 * If commit has been started, then we have to wait for
61112810ad7SArtem Bityutskiy 		 * completion of that transaction.
612470decc6SDave Kleikamp 		 */
613c88ccea3SJan Kara 		if (ptid)
614470decc6SDave Kleikamp 			*ptid = journal->j_committing_transaction->t_tid;
615470decc6SDave Kleikamp 		ret = 1;
616470decc6SDave Kleikamp 	}
617a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
618470decc6SDave Kleikamp 	return ret;
619470decc6SDave Kleikamp }
620470decc6SDave Kleikamp 
621470decc6SDave Kleikamp /*
622bbd2be36SJan Kara  * Return 1 if a given transaction has not yet sent barrier request
623bbd2be36SJan Kara  * connected with a transaction commit. If 0 is returned, transaction
624bbd2be36SJan Kara  * may or may not have sent the barrier. Used to avoid sending barrier
625bbd2be36SJan Kara  * twice in common cases.
626bbd2be36SJan Kara  */
jbd2_trans_will_send_data_barrier(journal_t * journal,tid_t tid)627bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
628bbd2be36SJan Kara {
629bbd2be36SJan Kara 	int ret = 0;
630bbd2be36SJan Kara 	transaction_t *commit_trans;
631bbd2be36SJan Kara 
632bbd2be36SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
633bbd2be36SJan Kara 		return 0;
634bbd2be36SJan Kara 	read_lock(&journal->j_state_lock);
635bbd2be36SJan Kara 	/* Transaction already committed? */
636bbd2be36SJan Kara 	if (tid_geq(journal->j_commit_sequence, tid))
637bbd2be36SJan Kara 		goto out;
638bbd2be36SJan Kara 	commit_trans = journal->j_committing_transaction;
639bbd2be36SJan Kara 	if (!commit_trans || commit_trans->t_tid != tid) {
640bbd2be36SJan Kara 		ret = 1;
641bbd2be36SJan Kara 		goto out;
642bbd2be36SJan Kara 	}
643bbd2be36SJan Kara 	/*
644bbd2be36SJan Kara 	 * Transaction is being committed and we already proceeded to
645bbd2be36SJan Kara 	 * submitting a flush to fs partition?
646bbd2be36SJan Kara 	 */
647bbd2be36SJan Kara 	if (journal->j_fs_dev != journal->j_dev) {
648bbd2be36SJan Kara 		if (!commit_trans->t_need_data_flush ||
649bbd2be36SJan Kara 		    commit_trans->t_state >= T_COMMIT_DFLUSH)
650bbd2be36SJan Kara 			goto out;
651bbd2be36SJan Kara 	} else {
652bbd2be36SJan Kara 		if (commit_trans->t_state >= T_COMMIT_JFLUSH)
653bbd2be36SJan Kara 			goto out;
654bbd2be36SJan Kara 	}
655bbd2be36SJan Kara 	ret = 1;
656bbd2be36SJan Kara out:
657bbd2be36SJan Kara 	read_unlock(&journal->j_state_lock);
658bbd2be36SJan Kara 	return ret;
659bbd2be36SJan Kara }
660bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
661bbd2be36SJan Kara 
662bbd2be36SJan Kara /*
663470decc6SDave Kleikamp  * Wait for a specified commit to complete.
664470decc6SDave Kleikamp  * The caller may not hold the journal lock.
665470decc6SDave Kleikamp  */
jbd2_log_wait_commit(journal_t * journal,tid_t tid)666f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
667470decc6SDave Kleikamp {
668470decc6SDave Kleikamp 	int err = 0;
669470decc6SDave Kleikamp 
670c52c47e4SJan Kara 	read_lock(&journal->j_state_lock);
671c52c47e4SJan Kara #ifdef CONFIG_PROVE_LOCKING
672c52c47e4SJan Kara 	/*
673c52c47e4SJan Kara 	 * Some callers make sure transaction is already committing and in that
674c52c47e4SJan Kara 	 * case we cannot block on open handles anymore. So don't warn in that
675c52c47e4SJan Kara 	 * case.
676c52c47e4SJan Kara 	 */
677c52c47e4SJan Kara 	if (tid_gt(tid, journal->j_commit_sequence) &&
678c52c47e4SJan Kara 	    (!journal->j_committing_transaction ||
679c52c47e4SJan Kara 	     journal->j_committing_transaction->t_tid != tid)) {
680c52c47e4SJan Kara 		read_unlock(&journal->j_state_lock);
6811eaa566dSJan Kara 		jbd2_might_wait_for_commit(journal);
682a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
683c52c47e4SJan Kara 	}
684c52c47e4SJan Kara #endif
685e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
686470decc6SDave Kleikamp 	if (!tid_geq(journal->j_commit_request, tid)) {
68775685071SJan Kara 		printk(KERN_ERR
6887821ce41SGaowei Pu 		       "%s: error: j_commit_request=%u, tid=%u\n",
689329d291fSHarvey Harrison 		       __func__, journal->j_commit_request, tid);
690470decc6SDave Kleikamp 	}
691470decc6SDave Kleikamp #endif
692470decc6SDave Kleikamp 	while (tid_gt(tid, journal->j_commit_sequence)) {
693cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: want %u, j_commit_sequence=%u\n",
694470decc6SDave Kleikamp 				  tid, journal->j_commit_sequence);
695a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
6963469a32aSTheodore Ts'o 		wake_up(&journal->j_wait_commit);
697470decc6SDave Kleikamp 		wait_event(journal->j_wait_done_commit,
698470decc6SDave Kleikamp 				!tid_gt(tid, journal->j_commit_sequence));
699a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
700470decc6SDave Kleikamp 	}
701a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
702470decc6SDave Kleikamp 
70375685071SJan Kara 	if (unlikely(is_journal_aborted(journal)))
704470decc6SDave Kleikamp 		err = -EIO;
705470decc6SDave Kleikamp 	return err;
706470decc6SDave Kleikamp }
707470decc6SDave Kleikamp 
708ff780b91SHarshad Shirwadkar /*
709ff780b91SHarshad Shirwadkar  * Start a fast commit. If there's an ongoing fast or full commit wait for
710ff780b91SHarshad Shirwadkar  * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY
711ff780b91SHarshad Shirwadkar  * if a fast commit is not needed, either because there's an already a commit
712ff780b91SHarshad Shirwadkar  * going on or this tid has already been committed. Returns -EINVAL if no jbd2
713ff780b91SHarshad Shirwadkar  * commit has yet been performed.
714ff780b91SHarshad Shirwadkar  */
jbd2_fc_begin_commit(journal_t * journal,tid_t tid)715ff780b91SHarshad Shirwadkar int jbd2_fc_begin_commit(journal_t *journal, tid_t tid)
716ff780b91SHarshad Shirwadkar {
71787a144f0SHarshad Shirwadkar 	if (unlikely(is_journal_aborted(journal)))
71887a144f0SHarshad Shirwadkar 		return -EIO;
719ff780b91SHarshad Shirwadkar 	/*
720ff780b91SHarshad Shirwadkar 	 * Fast commits only allowed if at least one full commit has
721ff780b91SHarshad Shirwadkar 	 * been processed.
722ff780b91SHarshad Shirwadkar 	 */
723ff780b91SHarshad Shirwadkar 	if (!journal->j_stats.ts_tid)
724ff780b91SHarshad Shirwadkar 		return -EINVAL;
725ff780b91SHarshad Shirwadkar 
726ff780b91SHarshad Shirwadkar 	write_lock(&journal->j_state_lock);
727480f89d5SHarshad Shirwadkar 	if (tid <= journal->j_commit_sequence) {
728480f89d5SHarshad Shirwadkar 		write_unlock(&journal->j_state_lock);
729480f89d5SHarshad Shirwadkar 		return -EALREADY;
730480f89d5SHarshad Shirwadkar 	}
731480f89d5SHarshad Shirwadkar 
732ff780b91SHarshad Shirwadkar 	if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
733ff780b91SHarshad Shirwadkar 	    (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) {
734ff780b91SHarshad Shirwadkar 		DEFINE_WAIT(wait);
735ff780b91SHarshad Shirwadkar 
736ff780b91SHarshad Shirwadkar 		prepare_to_wait(&journal->j_fc_wait, &wait,
737ff780b91SHarshad Shirwadkar 				TASK_UNINTERRUPTIBLE);
738ff780b91SHarshad Shirwadkar 		write_unlock(&journal->j_state_lock);
739ff780b91SHarshad Shirwadkar 		schedule();
740ff780b91SHarshad Shirwadkar 		finish_wait(&journal->j_fc_wait, &wait);
741ff780b91SHarshad Shirwadkar 		return -EALREADY;
742ff780b91SHarshad Shirwadkar 	}
743ff780b91SHarshad Shirwadkar 	journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
744ff780b91SHarshad Shirwadkar 	write_unlock(&journal->j_state_lock);
7452729cfdcSHarshad Shirwadkar 	jbd2_journal_lock_updates(journal);
746ff780b91SHarshad Shirwadkar 
747ff780b91SHarshad Shirwadkar 	return 0;
748ff780b91SHarshad Shirwadkar }
749ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_begin_commit);
750ff780b91SHarshad Shirwadkar 
751ff780b91SHarshad Shirwadkar /*
752ff780b91SHarshad Shirwadkar  * Stop a fast commit. If fallback is set, this function starts commit of
753ff780b91SHarshad Shirwadkar  * TID tid before any other fast commit can start.
754ff780b91SHarshad Shirwadkar  */
__jbd2_fc_end_commit(journal_t * journal,tid_t tid,bool fallback)755ff780b91SHarshad Shirwadkar static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback)
756ff780b91SHarshad Shirwadkar {
7572729cfdcSHarshad Shirwadkar 	jbd2_journal_unlock_updates(journal);
758ff780b91SHarshad Shirwadkar 	if (journal->j_fc_cleanup_callback)
759e85c81baSXin Yin 		journal->j_fc_cleanup_callback(journal, 0, tid);
760ff780b91SHarshad Shirwadkar 	write_lock(&journal->j_state_lock);
761ff780b91SHarshad Shirwadkar 	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
762ff780b91SHarshad Shirwadkar 	if (fallback)
763ff780b91SHarshad Shirwadkar 		journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
764ff780b91SHarshad Shirwadkar 	write_unlock(&journal->j_state_lock);
765ff780b91SHarshad Shirwadkar 	wake_up(&journal->j_fc_wait);
766ff780b91SHarshad Shirwadkar 	if (fallback)
767ff780b91SHarshad Shirwadkar 		return jbd2_complete_transaction(journal, tid);
768ff780b91SHarshad Shirwadkar 	return 0;
769ff780b91SHarshad Shirwadkar }
770ff780b91SHarshad Shirwadkar 
jbd2_fc_end_commit(journal_t * journal)771ff780b91SHarshad Shirwadkar int jbd2_fc_end_commit(journal_t *journal)
772ff780b91SHarshad Shirwadkar {
7730bce577bSHarshad Shirwadkar 	return __jbd2_fc_end_commit(journal, 0, false);
774ff780b91SHarshad Shirwadkar }
775ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit);
776ff780b91SHarshad Shirwadkar 
jbd2_fc_end_commit_fallback(journal_t * journal)7770bce577bSHarshad Shirwadkar int jbd2_fc_end_commit_fallback(journal_t *journal)
778ff780b91SHarshad Shirwadkar {
7790bce577bSHarshad Shirwadkar 	tid_t tid;
7800bce577bSHarshad Shirwadkar 
7810bce577bSHarshad Shirwadkar 	read_lock(&journal->j_state_lock);
7820bce577bSHarshad Shirwadkar 	tid = journal->j_running_transaction ?
7830bce577bSHarshad Shirwadkar 		journal->j_running_transaction->t_tid : 0;
7840bce577bSHarshad Shirwadkar 	read_unlock(&journal->j_state_lock);
7850bce577bSHarshad Shirwadkar 	return __jbd2_fc_end_commit(journal, tid, true);
786ff780b91SHarshad Shirwadkar }
787ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit_fallback);
788ff780b91SHarshad Shirwadkar 
789b8a6176cSJan Kara /* Return 1 when transaction with given tid has already committed. */
jbd2_transaction_committed(journal_t * journal,tid_t tid)790b8a6176cSJan Kara int jbd2_transaction_committed(journal_t *journal, tid_t tid)
791b8a6176cSJan Kara {
792b8a6176cSJan Kara 	int ret = 1;
793b8a6176cSJan Kara 
794b8a6176cSJan Kara 	read_lock(&journal->j_state_lock);
795b8a6176cSJan Kara 	if (journal->j_running_transaction &&
796b8a6176cSJan Kara 	    journal->j_running_transaction->t_tid == tid)
797b8a6176cSJan Kara 		ret = 0;
798b8a6176cSJan Kara 	if (journal->j_committing_transaction &&
799b8a6176cSJan Kara 	    journal->j_committing_transaction->t_tid == tid)
800b8a6176cSJan Kara 		ret = 0;
801b8a6176cSJan Kara 	read_unlock(&journal->j_state_lock);
802b8a6176cSJan Kara 	return ret;
803b8a6176cSJan Kara }
804b8a6176cSJan Kara EXPORT_SYMBOL(jbd2_transaction_committed);
805b8a6176cSJan Kara 
806470decc6SDave Kleikamp /*
807d76a3a77STheodore Ts'o  * When this function returns the transaction corresponding to tid
808d76a3a77STheodore Ts'o  * will be completed.  If the transaction has currently running, start
809d76a3a77STheodore Ts'o  * committing that transaction before waiting for it to complete.  If
810d76a3a77STheodore Ts'o  * the transaction id is stale, it is by definition already completed,
811d76a3a77STheodore Ts'o  * so just return SUCCESS.
812d76a3a77STheodore Ts'o  */
jbd2_complete_transaction(journal_t * journal,tid_t tid)813d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid)
814d76a3a77STheodore Ts'o {
815d76a3a77STheodore Ts'o 	int	need_to_wait = 1;
816d76a3a77STheodore Ts'o 
817d76a3a77STheodore Ts'o 	read_lock(&journal->j_state_lock);
818d76a3a77STheodore Ts'o 	if (journal->j_running_transaction &&
819d76a3a77STheodore Ts'o 	    journal->j_running_transaction->t_tid == tid) {
820d76a3a77STheodore Ts'o 		if (journal->j_commit_request != tid) {
821d76a3a77STheodore Ts'o 			/* transaction not yet started, so request it */
822d76a3a77STheodore Ts'o 			read_unlock(&journal->j_state_lock);
823d76a3a77STheodore Ts'o 			jbd2_log_start_commit(journal, tid);
824d76a3a77STheodore Ts'o 			goto wait_commit;
825d76a3a77STheodore Ts'o 		}
826d76a3a77STheodore Ts'o 	} else if (!(journal->j_committing_transaction &&
827d76a3a77STheodore Ts'o 		     journal->j_committing_transaction->t_tid == tid))
828d76a3a77STheodore Ts'o 		need_to_wait = 0;
829d76a3a77STheodore Ts'o 	read_unlock(&journal->j_state_lock);
830d76a3a77STheodore Ts'o 	if (!need_to_wait)
831d76a3a77STheodore Ts'o 		return 0;
832d76a3a77STheodore Ts'o wait_commit:
833d76a3a77STheodore Ts'o 	return jbd2_log_wait_commit(journal, tid);
834d76a3a77STheodore Ts'o }
835d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction);
836d76a3a77STheodore Ts'o 
837d76a3a77STheodore Ts'o /*
838470decc6SDave Kleikamp  * Log buffer allocation routines:
839470decc6SDave Kleikamp  */
840470decc6SDave Kleikamp 
jbd2_journal_next_log_block(journal_t * journal,unsigned long long * retp)84118eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
842470decc6SDave Kleikamp {
843470decc6SDave Kleikamp 	unsigned long blocknr;
844470decc6SDave Kleikamp 
845a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
846470decc6SDave Kleikamp 	J_ASSERT(journal->j_free > 1);
847470decc6SDave Kleikamp 
848470decc6SDave Kleikamp 	blocknr = journal->j_head;
849470decc6SDave Kleikamp 	journal->j_head++;
850470decc6SDave Kleikamp 	journal->j_free--;
851470decc6SDave Kleikamp 	if (journal->j_head == journal->j_last)
852470decc6SDave Kleikamp 		journal->j_head = journal->j_first;
853a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
854f7f4bccbSMingming Cao 	return jbd2_journal_bmap(journal, blocknr, retp);
855470decc6SDave Kleikamp }
856470decc6SDave Kleikamp 
857ff780b91SHarshad Shirwadkar /* Map one fast commit buffer for use by the file system */
jbd2_fc_get_buf(journal_t * journal,struct buffer_head ** bh_out)858ff780b91SHarshad Shirwadkar int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out)
859ff780b91SHarshad Shirwadkar {
860ff780b91SHarshad Shirwadkar 	unsigned long long pblock;
861ff780b91SHarshad Shirwadkar 	unsigned long blocknr;
862ff780b91SHarshad Shirwadkar 	int ret = 0;
863ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
864ff780b91SHarshad Shirwadkar 	int fc_off;
865ff780b91SHarshad Shirwadkar 
866ff780b91SHarshad Shirwadkar 	*bh_out = NULL;
867ff780b91SHarshad Shirwadkar 
868ff780b91SHarshad Shirwadkar 	if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) {
869ff780b91SHarshad Shirwadkar 		fc_off = journal->j_fc_off;
870ff780b91SHarshad Shirwadkar 		blocknr = journal->j_fc_first + fc_off;
871ff780b91SHarshad Shirwadkar 		journal->j_fc_off++;
872ff780b91SHarshad Shirwadkar 	} else {
873ff780b91SHarshad Shirwadkar 		ret = -EINVAL;
874ff780b91SHarshad Shirwadkar 	}
875ff780b91SHarshad Shirwadkar 
876ff780b91SHarshad Shirwadkar 	if (ret)
877ff780b91SHarshad Shirwadkar 		return ret;
878ff780b91SHarshad Shirwadkar 
879ff780b91SHarshad Shirwadkar 	ret = jbd2_journal_bmap(journal, blocknr, &pblock);
880ff780b91SHarshad Shirwadkar 	if (ret)
881ff780b91SHarshad Shirwadkar 		return ret;
882ff780b91SHarshad Shirwadkar 
883ff780b91SHarshad Shirwadkar 	bh = __getblk(journal->j_dev, pblock, journal->j_blocksize);
884ff780b91SHarshad Shirwadkar 	if (!bh)
885ff780b91SHarshad Shirwadkar 		return -ENOMEM;
886ff780b91SHarshad Shirwadkar 
887ff780b91SHarshad Shirwadkar 
888ff780b91SHarshad Shirwadkar 	journal->j_fc_wbuf[fc_off] = bh;
889ff780b91SHarshad Shirwadkar 
890ff780b91SHarshad Shirwadkar 	*bh_out = bh;
891ff780b91SHarshad Shirwadkar 
892ff780b91SHarshad Shirwadkar 	return 0;
893ff780b91SHarshad Shirwadkar }
894ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_get_buf);
895ff780b91SHarshad Shirwadkar 
896ff780b91SHarshad Shirwadkar /*
897ff780b91SHarshad Shirwadkar  * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
898ff780b91SHarshad Shirwadkar  * for completion.
899ff780b91SHarshad Shirwadkar  */
jbd2_fc_wait_bufs(journal_t * journal,int num_blks)900ff780b91SHarshad Shirwadkar int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
901ff780b91SHarshad Shirwadkar {
902ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
903ff780b91SHarshad Shirwadkar 	int i, j_fc_off;
904ff780b91SHarshad Shirwadkar 
905ff780b91SHarshad Shirwadkar 	j_fc_off = journal->j_fc_off;
906ff780b91SHarshad Shirwadkar 
907ff780b91SHarshad Shirwadkar 	/*
908ff780b91SHarshad Shirwadkar 	 * Wait in reverse order to minimize chances of us being woken up before
909ff780b91SHarshad Shirwadkar 	 * all IOs have completed
910ff780b91SHarshad Shirwadkar 	 */
911ff780b91SHarshad Shirwadkar 	for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
912ff780b91SHarshad Shirwadkar 		bh = journal->j_fc_wbuf[i];
913ff780b91SHarshad Shirwadkar 		wait_on_buffer(bh);
914e0d5fc7aSYe Bin 		/*
915e0d5fc7aSYe Bin 		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
916e0d5fc7aSYe Bin 		 * buffer head.
917e0d5fc7aSYe Bin 		 */
918e0d5fc7aSYe Bin 		if (unlikely(!buffer_uptodate(bh))) {
919243d1a5dSYe Bin 			journal->j_fc_off = i + 1;
920ff780b91SHarshad Shirwadkar 			return -EIO;
921ff780b91SHarshad Shirwadkar 		}
922243d1a5dSYe Bin 		put_bh(bh);
923243d1a5dSYe Bin 		journal->j_fc_wbuf[i] = NULL;
924e0d5fc7aSYe Bin 	}
925ff780b91SHarshad Shirwadkar 
926ff780b91SHarshad Shirwadkar 	return 0;
927ff780b91SHarshad Shirwadkar }
928ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_wait_bufs);
929ff780b91SHarshad Shirwadkar 
jbd2_fc_release_bufs(journal_t * journal)930ff780b91SHarshad Shirwadkar int jbd2_fc_release_bufs(journal_t *journal)
931ff780b91SHarshad Shirwadkar {
932ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
933ff780b91SHarshad Shirwadkar 	int i, j_fc_off;
934ff780b91SHarshad Shirwadkar 
935ff780b91SHarshad Shirwadkar 	j_fc_off = journal->j_fc_off;
936ff780b91SHarshad Shirwadkar 
937ff780b91SHarshad Shirwadkar 	for (i = j_fc_off - 1; i >= 0; i--) {
938ff780b91SHarshad Shirwadkar 		bh = journal->j_fc_wbuf[i];
939ff780b91SHarshad Shirwadkar 		if (!bh)
940ff780b91SHarshad Shirwadkar 			break;
941ff780b91SHarshad Shirwadkar 		put_bh(bh);
942ff780b91SHarshad Shirwadkar 		journal->j_fc_wbuf[i] = NULL;
943ff780b91SHarshad Shirwadkar 	}
944ff780b91SHarshad Shirwadkar 
945ff780b91SHarshad Shirwadkar 	return 0;
946ff780b91SHarshad Shirwadkar }
947ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_release_bufs);
948ff780b91SHarshad Shirwadkar 
949470decc6SDave Kleikamp /*
950470decc6SDave Kleikamp  * Conversion of logical to physical block numbers for the journal
951470decc6SDave Kleikamp  *
952470decc6SDave Kleikamp  * On external journals the journal blocks are identity-mapped, so
953470decc6SDave Kleikamp  * this is a no-op.  If needed, we can use j_blk_offset - everything is
954470decc6SDave Kleikamp  * ready.
955470decc6SDave Kleikamp  */
jbd2_journal_bmap(journal_t * journal,unsigned long blocknr,unsigned long long * retp)956f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
95718eba7aaSMingming Cao 		 unsigned long long *retp)
958470decc6SDave Kleikamp {
959470decc6SDave Kleikamp 	int err = 0;
96018eba7aaSMingming Cao 	unsigned long long ret;
96162913ae9STheodore Ts'o 	sector_t block = blocknr;
962470decc6SDave Kleikamp 
96362913ae9STheodore Ts'o 	if (journal->j_bmap) {
96462913ae9STheodore Ts'o 		err = journal->j_bmap(journal, &block);
96562913ae9STheodore Ts'o 		if (err == 0)
96662913ae9STheodore Ts'o 			*retp = block;
96762913ae9STheodore Ts'o 	} else if (journal->j_inode) {
96830460e1eSCarlos Maiolino 		ret = bmap(journal->j_inode, &block);
96930460e1eSCarlos Maiolino 
97030460e1eSCarlos Maiolino 		if (ret || !block) {
971470decc6SDave Kleikamp 			printk(KERN_ALERT "%s: journal block not found "
972470decc6SDave Kleikamp 					"at offset %lu on %s\n",
97305496769STheodore Ts'o 			       __func__, blocknr, journal->j_devname);
974470decc6SDave Kleikamp 			err = -EIO;
9757f6225e4Szhangyi (F) 			jbd2_journal_abort(journal, err);
97630460e1eSCarlos Maiolino 		} else {
97730460e1eSCarlos Maiolino 			*retp = block;
978470decc6SDave Kleikamp 		}
97930460e1eSCarlos Maiolino 
980470decc6SDave Kleikamp 	} else {
981470decc6SDave Kleikamp 		*retp = blocknr; /* +journal->j_blk_offset */
982470decc6SDave Kleikamp 	}
983470decc6SDave Kleikamp 	return err;
984470decc6SDave Kleikamp }
985470decc6SDave Kleikamp 
986470decc6SDave Kleikamp /*
987470decc6SDave Kleikamp  * We play buffer_head aliasing tricks to write data/metadata blocks to
988470decc6SDave Kleikamp  * the journal without copying their contents, but for journal
989470decc6SDave Kleikamp  * descriptor blocks we do need to generate bona fide buffers.
990470decc6SDave Kleikamp  *
991f7f4bccbSMingming Cao  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
992470decc6SDave Kleikamp  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
993470decc6SDave Kleikamp  * But we don't bother doing that, so there will be coherency problems with
994470decc6SDave Kleikamp  * mmaps of blockdevs which hold live JBD-controlled filesystems.
995470decc6SDave Kleikamp  */
99632ab6715SJan Kara struct buffer_head *
jbd2_journal_get_descriptor_buffer(transaction_t * transaction,int type)99732ab6715SJan Kara jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type)
998470decc6SDave Kleikamp {
99932ab6715SJan Kara 	journal_t *journal = transaction->t_journal;
1000470decc6SDave Kleikamp 	struct buffer_head *bh;
100118eba7aaSMingming Cao 	unsigned long long blocknr;
100232ab6715SJan Kara 	journal_header_t *header;
1003470decc6SDave Kleikamp 	int err;
1004470decc6SDave Kleikamp 
1005f7f4bccbSMingming Cao 	err = jbd2_journal_next_log_block(journal, &blocknr);
1006470decc6SDave Kleikamp 
1007470decc6SDave Kleikamp 	if (err)
1008470decc6SDave Kleikamp 		return NULL;
1009470decc6SDave Kleikamp 
1010470decc6SDave Kleikamp 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
10114b905671SJan Kara 	if (!bh)
10124b905671SJan Kara 		return NULL;
10139f356e5aSJan Kara 	atomic_dec(&transaction->t_outstanding_credits);
1014470decc6SDave Kleikamp 	lock_buffer(bh);
1015470decc6SDave Kleikamp 	memset(bh->b_data, 0, journal->j_blocksize);
101632ab6715SJan Kara 	header = (journal_header_t *)bh->b_data;
101732ab6715SJan Kara 	header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
101832ab6715SJan Kara 	header->h_blocktype = cpu_to_be32(type);
101932ab6715SJan Kara 	header->h_sequence = cpu_to_be32(transaction->t_tid);
1020470decc6SDave Kleikamp 	set_buffer_uptodate(bh);
1021470decc6SDave Kleikamp 	unlock_buffer(bh);
1022470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "return this buffer");
1023e5a120aeSJan Kara 	return bh;
1024470decc6SDave Kleikamp }
1025470decc6SDave Kleikamp 
jbd2_descriptor_block_csum_set(journal_t * j,struct buffer_head * bh)10261101cd4dSJan Kara void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh)
10271101cd4dSJan Kara {
10281101cd4dSJan Kara 	struct jbd2_journal_block_tail *tail;
10291101cd4dSJan Kara 	__u32 csum;
10301101cd4dSJan Kara 
10311101cd4dSJan Kara 	if (!jbd2_journal_has_csum_v2or3(j))
10321101cd4dSJan Kara 		return;
10331101cd4dSJan Kara 
10341101cd4dSJan Kara 	tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
10351101cd4dSJan Kara 			sizeof(struct jbd2_journal_block_tail));
10361101cd4dSJan Kara 	tail->t_checksum = 0;
10371101cd4dSJan Kara 	csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
10381101cd4dSJan Kara 	tail->t_checksum = cpu_to_be32(csum);
10391101cd4dSJan Kara }
10401101cd4dSJan Kara 
104179feb521SJan Kara /*
104279feb521SJan Kara  * Return tid of the oldest transaction in the journal and block in the journal
104379feb521SJan Kara  * where the transaction starts.
104479feb521SJan Kara  *
104579feb521SJan Kara  * If the journal is now empty, return which will be the next transaction ID
104679feb521SJan Kara  * we will write and where will that transaction start.
104779feb521SJan Kara  *
104879feb521SJan Kara  * The return value is 0 if journal tail cannot be pushed any further, 1 if
104979feb521SJan Kara  * it can.
105079feb521SJan Kara  */
jbd2_journal_get_log_tail(journal_t * journal,tid_t * tid,unsigned long * block)105179feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
105279feb521SJan Kara 			      unsigned long *block)
105379feb521SJan Kara {
105479feb521SJan Kara 	transaction_t *transaction;
105579feb521SJan Kara 	int ret;
105679feb521SJan Kara 
105779feb521SJan Kara 	read_lock(&journal->j_state_lock);
105879feb521SJan Kara 	spin_lock(&journal->j_list_lock);
105979feb521SJan Kara 	transaction = journal->j_checkpoint_transactions;
106079feb521SJan Kara 	if (transaction) {
106179feb521SJan Kara 		*tid = transaction->t_tid;
106279feb521SJan Kara 		*block = transaction->t_log_start;
106379feb521SJan Kara 	} else if ((transaction = journal->j_committing_transaction) != NULL) {
106479feb521SJan Kara 		*tid = transaction->t_tid;
106579feb521SJan Kara 		*block = transaction->t_log_start;
106679feb521SJan Kara 	} else if ((transaction = journal->j_running_transaction) != NULL) {
106779feb521SJan Kara 		*tid = transaction->t_tid;
106879feb521SJan Kara 		*block = journal->j_head;
106979feb521SJan Kara 	} else {
107079feb521SJan Kara 		*tid = journal->j_transaction_sequence;
107179feb521SJan Kara 		*block = journal->j_head;
107279feb521SJan Kara 	}
107379feb521SJan Kara 	ret = tid_gt(*tid, journal->j_tail_sequence);
107479feb521SJan Kara 	spin_unlock(&journal->j_list_lock);
107579feb521SJan Kara 	read_unlock(&journal->j_state_lock);
107679feb521SJan Kara 
107779feb521SJan Kara 	return ret;
107879feb521SJan Kara }
107979feb521SJan Kara 
108079feb521SJan Kara /*
108179feb521SJan Kara  * Update information in journal structure and in on disk journal superblock
108279feb521SJan Kara  * about log tail. This function does not check whether information passed in
108379feb521SJan Kara  * really pushes log tail further. It's responsibility of the caller to make
108479feb521SJan Kara  * sure provided log tail information is valid (e.g. by holding
108579feb521SJan Kara  * j_checkpoint_mutex all the time between computing log tail and calling this
108679feb521SJan Kara  * function as is the case with jbd2_cleanup_journal_tail()).
108779feb521SJan Kara  *
108879feb521SJan Kara  * Requires j_checkpoint_mutex
108979feb521SJan Kara  */
__jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)10906f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
109179feb521SJan Kara {
109279feb521SJan Kara 	unsigned long freed;
10936f6a6fdaSJoseph Qi 	int ret;
109479feb521SJan Kara 
109579feb521SJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
109679feb521SJan Kara 
109779feb521SJan Kara 	/*
109879feb521SJan Kara 	 * We cannot afford for write to remain in drive's caches since as
109979feb521SJan Kara 	 * soon as we update j_tail, next transaction can start reusing journal
110079feb521SJan Kara 	 * space and if we lose sb update during power failure we'd replay
110179feb521SJan Kara 	 * old transaction with possibly newly overwritten data.
110279feb521SJan Kara 	 */
1103*5c480a69SZhang Yi 	ret = jbd2_journal_update_sb_log_tail(journal, tid, block, REQ_FUA);
11046f6a6fdaSJoseph Qi 	if (ret)
11056f6a6fdaSJoseph Qi 		goto out;
11066f6a6fdaSJoseph Qi 
110779feb521SJan Kara 	write_lock(&journal->j_state_lock);
110879feb521SJan Kara 	freed = block - journal->j_tail;
110979feb521SJan Kara 	if (block < journal->j_tail)
111079feb521SJan Kara 		freed += journal->j_last - journal->j_first;
111179feb521SJan Kara 
111279feb521SJan Kara 	trace_jbd2_update_log_tail(journal, tid, block, freed);
1113cb3b3bf2SJan Kara 	jbd2_debug(1,
11147821ce41SGaowei Pu 		  "Cleaning journal tail from %u to %u (offset %lu), "
111579feb521SJan Kara 		  "freeing %lu\n",
111679feb521SJan Kara 		  journal->j_tail_sequence, tid, block, freed);
111779feb521SJan Kara 
111879feb521SJan Kara 	journal->j_free += freed;
111979feb521SJan Kara 	journal->j_tail_sequence = tid;
112079feb521SJan Kara 	journal->j_tail = block;
112179feb521SJan Kara 	write_unlock(&journal->j_state_lock);
11226f6a6fdaSJoseph Qi 
11236f6a6fdaSJoseph Qi out:
11246f6a6fdaSJoseph Qi 	return ret;
112579feb521SJan Kara }
112679feb521SJan Kara 
11273339578fSJan Kara /*
112885e0c4e8STheodore Ts'o  * This is a variation of __jbd2_update_log_tail which checks for validity of
11293339578fSJan Kara  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
11303339578fSJan Kara  * with other threads updating log tail.
11313339578fSJan Kara  */
jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)11323339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
11333339578fSJan Kara {
11346fa7aa50STejun Heo 	mutex_lock_io(&journal->j_checkpoint_mutex);
11353339578fSJan Kara 	if (tid_gt(tid, journal->j_tail_sequence))
11363339578fSJan Kara 		__jbd2_update_log_tail(journal, tid, block);
11373339578fSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
11383339578fSJan Kara }
11393339578fSJan Kara 
11408e85fb3fSJohann Lombardi struct jbd2_stats_proc_session {
11418e85fb3fSJohann Lombardi 	journal_t *journal;
11428e85fb3fSJohann Lombardi 	struct transaction_stats_s *stats;
11438e85fb3fSJohann Lombardi 	int start;
11448e85fb3fSJohann Lombardi 	int max;
11458e85fb3fSJohann Lombardi };
11468e85fb3fSJohann Lombardi 
jbd2_seq_info_start(struct seq_file * seq,loff_t * pos)11478e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
11488e85fb3fSJohann Lombardi {
11498e85fb3fSJohann Lombardi 	return *pos ? NULL : SEQ_START_TOKEN;
11508e85fb3fSJohann Lombardi }
11518e85fb3fSJohann Lombardi 
jbd2_seq_info_next(struct seq_file * seq,void * v,loff_t * pos)11528e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
11538e85fb3fSJohann Lombardi {
11541a8e9cf4SVasily Averin 	(*pos)++;
11558e85fb3fSJohann Lombardi 	return NULL;
11568e85fb3fSJohann Lombardi }
11578e85fb3fSJohann Lombardi 
jbd2_seq_info_show(struct seq_file * seq,void * v)11588e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v)
11598e85fb3fSJohann Lombardi {
11608e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
11618e85fb3fSJohann Lombardi 
11628e85fb3fSJohann Lombardi 	if (v != SEQ_START_TOKEN)
11638e85fb3fSJohann Lombardi 		return 0;
11649fff24aaSTheodore Ts'o 	seq_printf(seq, "%lu transactions (%lu requested), "
11659fff24aaSTheodore Ts'o 		   "each up to %u blocks\n",
11669fff24aaSTheodore Ts'o 		   s->stats->ts_tid, s->stats->ts_requested,
11678e85fb3fSJohann Lombardi 		   s->journal->j_max_transaction_buffers);
11688e85fb3fSJohann Lombardi 	if (s->stats->ts_tid == 0)
11698e85fb3fSJohann Lombardi 		return 0;
11708e85fb3fSJohann Lombardi 	seq_printf(seq, "average: \n  %ums waiting for transaction\n",
1171bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
11729fff24aaSTheodore Ts'o 	seq_printf(seq, "  %ums request delay\n",
11739fff24aaSTheodore Ts'o 	    (s->stats->ts_requested == 0) ? 0 :
11749fff24aaSTheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_request_delay /
11759fff24aaSTheodore Ts'o 			     s->stats->ts_requested));
11768e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums running transaction\n",
1177bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
11788e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums transaction was being locked\n",
1179bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
11808e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
1181bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
11828e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums logging transaction\n",
1183bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
1184c225aa57SSimon Holm Thøgersen 	seq_printf(seq, "  %lluus average transaction commit time\n",
1185c225aa57SSimon Holm Thøgersen 		   div_u64(s->journal->j_average_commit_time, 1000));
11868e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu handles per transaction\n",
1187bf699327STheodore Ts'o 	    s->stats->run.rs_handle_count / s->stats->ts_tid);
11888e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu blocks per transaction\n",
1189bf699327STheodore Ts'o 	    s->stats->run.rs_blocks / s->stats->ts_tid);
11908e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu logged blocks per transaction\n",
1191bf699327STheodore Ts'o 	    s->stats->run.rs_blocks_logged / s->stats->ts_tid);
11928e85fb3fSJohann Lombardi 	return 0;
11938e85fb3fSJohann Lombardi }
11948e85fb3fSJohann Lombardi 
jbd2_seq_info_stop(struct seq_file * seq,void * v)11958e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
11968e85fb3fSJohann Lombardi {
11978e85fb3fSJohann Lombardi }
11988e85fb3fSJohann Lombardi 
119988e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = {
12008e85fb3fSJohann Lombardi 	.start  = jbd2_seq_info_start,
12018e85fb3fSJohann Lombardi 	.next   = jbd2_seq_info_next,
12028e85fb3fSJohann Lombardi 	.stop   = jbd2_seq_info_stop,
12038e85fb3fSJohann Lombardi 	.show   = jbd2_seq_info_show,
12048e85fb3fSJohann Lombardi };
12058e85fb3fSJohann Lombardi 
jbd2_seq_info_open(struct inode * inode,struct file * file)12068e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file)
12078e85fb3fSJohann Lombardi {
1208359745d7SMuchun Song 	journal_t *journal = pde_data(inode);
12098e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s;
12108e85fb3fSJohann Lombardi 	int rc, size;
12118e85fb3fSJohann Lombardi 
12128e85fb3fSJohann Lombardi 	s = kmalloc(sizeof(*s), GFP_KERNEL);
12138e85fb3fSJohann Lombardi 	if (s == NULL)
12148e85fb3fSJohann Lombardi 		return -ENOMEM;
12158e85fb3fSJohann Lombardi 	size = sizeof(struct transaction_stats_s);
12168e85fb3fSJohann Lombardi 	s->stats = kmalloc(size, GFP_KERNEL);
12178e85fb3fSJohann Lombardi 	if (s->stats == NULL) {
12188e85fb3fSJohann Lombardi 		kfree(s);
12198e85fb3fSJohann Lombardi 		return -ENOMEM;
12208e85fb3fSJohann Lombardi 	}
12218e85fb3fSJohann Lombardi 	spin_lock(&journal->j_history_lock);
12228e85fb3fSJohann Lombardi 	memcpy(s->stats, &journal->j_stats, size);
12238e85fb3fSJohann Lombardi 	s->journal = journal;
12248e85fb3fSJohann Lombardi 	spin_unlock(&journal->j_history_lock);
12258e85fb3fSJohann Lombardi 
12268e85fb3fSJohann Lombardi 	rc = seq_open(file, &jbd2_seq_info_ops);
12278e85fb3fSJohann Lombardi 	if (rc == 0) {
12288e85fb3fSJohann Lombardi 		struct seq_file *m = file->private_data;
12298e85fb3fSJohann Lombardi 		m->private = s;
12308e85fb3fSJohann Lombardi 	} else {
12318e85fb3fSJohann Lombardi 		kfree(s->stats);
12328e85fb3fSJohann Lombardi 		kfree(s);
12338e85fb3fSJohann Lombardi 	}
12348e85fb3fSJohann Lombardi 	return rc;
12358e85fb3fSJohann Lombardi 
12368e85fb3fSJohann Lombardi }
12378e85fb3fSJohann Lombardi 
jbd2_seq_info_release(struct inode * inode,struct file * file)12388e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file)
12398e85fb3fSJohann Lombardi {
12408e85fb3fSJohann Lombardi 	struct seq_file *seq = file->private_data;
12418e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
12428e85fb3fSJohann Lombardi 	kfree(s->stats);
12438e85fb3fSJohann Lombardi 	kfree(s);
12448e85fb3fSJohann Lombardi 	return seq_release(inode, file);
12458e85fb3fSJohann Lombardi }
12468e85fb3fSJohann Lombardi 
124797a32539SAlexey Dobriyan static const struct proc_ops jbd2_info_proc_ops = {
124897a32539SAlexey Dobriyan 	.proc_open	= jbd2_seq_info_open,
124997a32539SAlexey Dobriyan 	.proc_read	= seq_read,
125097a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
125197a32539SAlexey Dobriyan 	.proc_release	= jbd2_seq_info_release,
12528e85fb3fSJohann Lombardi };
12538e85fb3fSJohann Lombardi 
12548e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats;
12558e85fb3fSJohann Lombardi 
jbd2_stats_proc_init(journal_t * journal)12568e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal)
12578e85fb3fSJohann Lombardi {
125805496769STheodore Ts'o 	journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
12598e85fb3fSJohann Lombardi 	if (journal->j_proc_entry) {
126079da3664SDenis V. Lunev 		proc_create_data("info", S_IRUGO, journal->j_proc_entry,
126197a32539SAlexey Dobriyan 				 &jbd2_info_proc_ops, journal);
12628e85fb3fSJohann Lombardi 	}
12638e85fb3fSJohann Lombardi }
12648e85fb3fSJohann Lombardi 
jbd2_stats_proc_exit(journal_t * journal)12658e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal)
12668e85fb3fSJohann Lombardi {
12678e85fb3fSJohann Lombardi 	remove_proc_entry("info", journal->j_proc_entry);
126805496769STheodore Ts'o 	remove_proc_entry(journal->j_devname, proc_jbd2_stats);
12698e85fb3fSJohann Lombardi }
12708e85fb3fSJohann Lombardi 
1271b90bfdf5SJan Kara /* Minimum size of descriptor tag */
jbd2_min_tag_size(void)1272b90bfdf5SJan Kara static int jbd2_min_tag_size(void)
1273b90bfdf5SJan Kara {
1274b90bfdf5SJan Kara 	/*
1275b90bfdf5SJan Kara 	 * Tag with 32-bit block numbers does not use last four bytes of the
1276b90bfdf5SJan Kara 	 * structure
1277b90bfdf5SJan Kara 	 */
1278b90bfdf5SJan Kara 	return sizeof(journal_block_tag_t) - 4;
1279b90bfdf5SJan Kara }
1280b90bfdf5SJan Kara 
12810705e8d1STheodore Ts'o /**
12820705e8d1STheodore Ts'o  * jbd2_journal_shrink_scan()
1283715a67f1SYang Li  * @shrink: shrinker to work on
1284715a67f1SYang Li  * @sc: reclaim request to process
12850705e8d1STheodore Ts'o  *
12860705e8d1STheodore Ts'o  * Scan the checkpointed buffer on the checkpoint list and release the
12870705e8d1STheodore Ts'o  * journal_head.
12880705e8d1STheodore Ts'o  */
jbd2_journal_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)12890705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink,
12900705e8d1STheodore Ts'o 					      struct shrink_control *sc)
12910705e8d1STheodore Ts'o {
12920705e8d1STheodore Ts'o 	journal_t *journal = container_of(shrink, journal_t, j_shrinker);
12930705e8d1STheodore Ts'o 	unsigned long nr_to_scan = sc->nr_to_scan;
12940705e8d1STheodore Ts'o 	unsigned long nr_shrunk;
12950705e8d1STheodore Ts'o 	unsigned long count;
12960705e8d1STheodore Ts'o 
12970705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
12980705e8d1STheodore Ts'o 	trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count);
12990705e8d1STheodore Ts'o 
13000705e8d1STheodore Ts'o 	nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan);
13010705e8d1STheodore Ts'o 
13020705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
13030705e8d1STheodore Ts'o 	trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count);
13040705e8d1STheodore Ts'o 
13050705e8d1STheodore Ts'o 	return nr_shrunk;
13060705e8d1STheodore Ts'o }
13070705e8d1STheodore Ts'o 
13080705e8d1STheodore Ts'o /**
13090705e8d1STheodore Ts'o  * jbd2_journal_shrink_count()
1310715a67f1SYang Li  * @shrink: shrinker to work on
1311715a67f1SYang Li  * @sc: reclaim request to process
13120705e8d1STheodore Ts'o  *
13130705e8d1STheodore Ts'o  * Count the number of checkpoint buffers on the checkpoint list.
13140705e8d1STheodore Ts'o  */
jbd2_journal_shrink_count(struct shrinker * shrink,struct shrink_control * sc)13150705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_count(struct shrinker *shrink,
13160705e8d1STheodore Ts'o 					       struct shrink_control *sc)
13170705e8d1STheodore Ts'o {
13180705e8d1STheodore Ts'o 	journal_t *journal = container_of(shrink, journal_t, j_shrinker);
13190705e8d1STheodore Ts'o 	unsigned long count;
13200705e8d1STheodore Ts'o 
13210705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
13220705e8d1STheodore Ts'o 	trace_jbd2_shrink_count(journal, sc->nr_to_scan, count);
13230705e8d1STheodore Ts'o 
13240705e8d1STheodore Ts'o 	return count;
13250705e8d1STheodore Ts'o }
13260705e8d1STheodore Ts'o 
1327470decc6SDave Kleikamp /*
132829a511e4SZhang Yi  * If the journal init or create aborts, we need to mark the journal
132929a511e4SZhang Yi  * superblock as being NULL to prevent the journal destroy from writing
133029a511e4SZhang Yi  * back a bogus superblock.
133129a511e4SZhang Yi  */
journal_fail_superblock(journal_t * journal)133229a511e4SZhang Yi static void journal_fail_superblock(journal_t *journal)
133329a511e4SZhang Yi {
133429a511e4SZhang Yi 	struct buffer_head *bh = journal->j_sb_buffer;
133529a511e4SZhang Yi 	brelse(bh);
133629a511e4SZhang Yi 	journal->j_sb_buffer = NULL;
133729a511e4SZhang Yi }
133829a511e4SZhang Yi 
133929a511e4SZhang Yi /*
1340054d9c8fSZhang Yi  * Check the superblock for a given journal, performing initial
134129a511e4SZhang Yi  * validation of the format.
134229a511e4SZhang Yi  */
journal_check_superblock(journal_t * journal)1343054d9c8fSZhang Yi static int journal_check_superblock(journal_t *journal)
134429a511e4SZhang Yi {
1345054d9c8fSZhang Yi 	journal_superblock_t *sb = journal->j_superblock;
13460dbc759aSZhang Yi 	int num_fc_blks;
1347054d9c8fSZhang Yi 	int err = -EINVAL;
134829a511e4SZhang Yi 
134929a511e4SZhang Yi 	if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
135029a511e4SZhang Yi 	    sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
135129a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1352054d9c8fSZhang Yi 		return err;
135329a511e4SZhang Yi 	}
135429a511e4SZhang Yi 
135529a511e4SZhang Yi 	if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
135629a511e4SZhang Yi 	    be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
135729a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1358054d9c8fSZhang Yi 		return err;
135929a511e4SZhang Yi 	}
136029a511e4SZhang Yi 
136129a511e4SZhang Yi 	if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
136229a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: journal file too short\n");
1363054d9c8fSZhang Yi 		return err;
136429a511e4SZhang Yi 	}
136529a511e4SZhang Yi 
136629a511e4SZhang Yi 	if (be32_to_cpu(sb->s_first) == 0 ||
136729a511e4SZhang Yi 	    be32_to_cpu(sb->s_first) >= journal->j_total_len) {
136829a511e4SZhang Yi 		printk(KERN_WARNING
136929a511e4SZhang Yi 			"JBD2: Invalid start block of journal: %u\n",
137029a511e4SZhang Yi 			be32_to_cpu(sb->s_first));
1371054d9c8fSZhang Yi 		return err;
137229a511e4SZhang Yi 	}
137329a511e4SZhang Yi 
1374e4adf8b8SZhang Yi 	/*
1375e4adf8b8SZhang Yi 	 * If this is a V2 superblock, then we have to check the
1376e4adf8b8SZhang Yi 	 * features flags on it.
1377e4adf8b8SZhang Yi 	 */
1378e4adf8b8SZhang Yi 	if (!jbd2_format_support_feature(journal))
1379e4adf8b8SZhang Yi 		return 0;
1380e4adf8b8SZhang Yi 
1381e4adf8b8SZhang Yi 	if ((sb->s_feature_ro_compat &
1382e4adf8b8SZhang Yi 			~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1383e4adf8b8SZhang Yi 	    (sb->s_feature_incompat &
1384e4adf8b8SZhang Yi 			~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1385e4adf8b8SZhang Yi 		printk(KERN_WARNING "JBD2: Unrecognised features on journal\n");
1386054d9c8fSZhang Yi 		return err;
1387e4adf8b8SZhang Yi 	}
1388e4adf8b8SZhang Yi 
13890dbc759aSZhang Yi 	num_fc_blks = jbd2_has_feature_fast_commit(journal) ?
13900dbc759aSZhang Yi 				jbd2_journal_get_num_fc_blks(sb) : 0;
13910dbc759aSZhang Yi 	if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS ||
13920dbc759aSZhang Yi 	    be32_to_cpu(sb->s_maxlen) - JBD2_MIN_JOURNAL_BLOCKS < num_fc_blks) {
13930dbc759aSZhang Yi 		printk(KERN_ERR "JBD2: journal file too short %u,%d\n",
13940dbc759aSZhang Yi 		       be32_to_cpu(sb->s_maxlen), num_fc_blks);
13950dbc759aSZhang Yi 		return err;
13960dbc759aSZhang Yi 	}
13970dbc759aSZhang Yi 
139829a511e4SZhang Yi 	if (jbd2_has_feature_csum2(journal) &&
139929a511e4SZhang Yi 	    jbd2_has_feature_csum3(journal)) {
140029a511e4SZhang Yi 		/* Can't have checksum v2 and v3 at the same time! */
140129a511e4SZhang Yi 		printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
140229a511e4SZhang Yi 		       "at the same time!\n");
1403054d9c8fSZhang Yi 		return err;
140429a511e4SZhang Yi 	}
140529a511e4SZhang Yi 
140629a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3_feature(journal) &&
140729a511e4SZhang Yi 	    jbd2_has_feature_checksum(journal)) {
140829a511e4SZhang Yi 		/* Can't have checksum v1 and v2 on at the same time! */
140929a511e4SZhang Yi 		printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
141029a511e4SZhang Yi 		       "at the same time!\n");
1411054d9c8fSZhang Yi 		return err;
141229a511e4SZhang Yi 	}
141329a511e4SZhang Yi 
141418dad509SZhang Yi 	/* Load the checksum driver */
141518dad509SZhang Yi 	if (jbd2_journal_has_csum_v2or3_feature(journal)) {
141618dad509SZhang Yi 		if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
141729a511e4SZhang Yi 			printk(KERN_ERR "JBD2: Unknown checksum type\n");
1418054d9c8fSZhang Yi 			return err;
141929a511e4SZhang Yi 		}
142029a511e4SZhang Yi 
142129a511e4SZhang Yi 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
142229a511e4SZhang Yi 		if (IS_ERR(journal->j_chksum_driver)) {
142329a511e4SZhang Yi 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
142429a511e4SZhang Yi 			err = PTR_ERR(journal->j_chksum_driver);
142529a511e4SZhang Yi 			journal->j_chksum_driver = NULL;
1426054d9c8fSZhang Yi 			return err;
142729a511e4SZhang Yi 		}
142829a511e4SZhang Yi 		/* Check superblock checksum */
142929a511e4SZhang Yi 		if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
143029a511e4SZhang Yi 			printk(KERN_ERR "JBD2: journal checksum error\n");
143129a511e4SZhang Yi 			err = -EFSBADCRC;
143229a511e4SZhang Yi 			return err;
143329a511e4SZhang Yi 		}
1434054d9c8fSZhang Yi 	}
1435054d9c8fSZhang Yi 
1436054d9c8fSZhang Yi 	return 0;
1437054d9c8fSZhang Yi }
143829a511e4SZhang Yi 
journal_revoke_records_per_block(journal_t * journal)143929a511e4SZhang Yi static int journal_revoke_records_per_block(journal_t *journal)
144029a511e4SZhang Yi {
144129a511e4SZhang Yi 	int record_size;
144229a511e4SZhang Yi 	int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
144329a511e4SZhang Yi 
144429a511e4SZhang Yi 	if (jbd2_has_feature_64bit(journal))
144529a511e4SZhang Yi 		record_size = 8;
144629a511e4SZhang Yi 	else
144729a511e4SZhang Yi 		record_size = 4;
144829a511e4SZhang Yi 
144929a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3(journal))
145029a511e4SZhang Yi 		space -= sizeof(struct jbd2_journal_block_tail);
145129a511e4SZhang Yi 	return space / record_size;
145229a511e4SZhang Yi }
145329a511e4SZhang Yi 
145429a511e4SZhang Yi /*
145529a511e4SZhang Yi  * Load the on-disk journal superblock and read the key fields into the
145629a511e4SZhang Yi  * journal_t.
145729a511e4SZhang Yi  */
journal_load_superblock(journal_t * journal)1458054d9c8fSZhang Yi static int journal_load_superblock(journal_t *journal)
145929a511e4SZhang Yi {
146029a511e4SZhang Yi 	int err;
1461054d9c8fSZhang Yi 	struct buffer_head *bh;
146229a511e4SZhang Yi 	journal_superblock_t *sb;
146329a511e4SZhang Yi 
1464054d9c8fSZhang Yi 	bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
1465054d9c8fSZhang Yi 			      journal->j_blocksize);
1466054d9c8fSZhang Yi 	if (bh)
1467054d9c8fSZhang Yi 		err = bh_read(bh, 0);
1468054d9c8fSZhang Yi 	if (!bh || err < 0) {
1469054d9c8fSZhang Yi 		pr_err("%s: Cannot read journal superblock\n", __func__);
1470054d9c8fSZhang Yi 		brelse(bh);
1471054d9c8fSZhang Yi 		return -EIO;
1472054d9c8fSZhang Yi 	}
147329a511e4SZhang Yi 
1474054d9c8fSZhang Yi 	journal->j_sb_buffer = bh;
1475054d9c8fSZhang Yi 	sb = (journal_superblock_t *)bh->b_data;
1476054d9c8fSZhang Yi 	journal->j_superblock = sb;
1477054d9c8fSZhang Yi 	err = journal_check_superblock(journal);
1478054d9c8fSZhang Yi 	if (err) {
1479054d9c8fSZhang Yi 		journal_fail_superblock(journal);
1480054d9c8fSZhang Yi 		return err;
1481054d9c8fSZhang Yi 	}
148229a511e4SZhang Yi 
148329a511e4SZhang Yi 	journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
148429a511e4SZhang Yi 	journal->j_tail = be32_to_cpu(sb->s_start);
148529a511e4SZhang Yi 	journal->j_first = be32_to_cpu(sb->s_first);
148629a511e4SZhang Yi 	journal->j_errno = be32_to_cpu(sb->s_errno);
148729a511e4SZhang Yi 	journal->j_last = be32_to_cpu(sb->s_maxlen);
148829a511e4SZhang Yi 
148929a511e4SZhang Yi 	if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
149029a511e4SZhang Yi 		journal->j_total_len = be32_to_cpu(sb->s_maxlen);
149129a511e4SZhang Yi 	/* Precompute checksum seed for all metadata */
149229a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3(journal))
149329a511e4SZhang Yi 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
149429a511e4SZhang Yi 						   sizeof(sb->s_uuid));
149529a511e4SZhang Yi 	journal->j_revoke_records_per_block =
149629a511e4SZhang Yi 				journal_revoke_records_per_block(journal);
149729a511e4SZhang Yi 
149829a511e4SZhang Yi 	if (jbd2_has_feature_fast_commit(journal)) {
149929a511e4SZhang Yi 		journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
15000dbc759aSZhang Yi 		journal->j_last = journal->j_fc_last -
15010dbc759aSZhang Yi 				  jbd2_journal_get_num_fc_blks(sb);
150229a511e4SZhang Yi 		journal->j_fc_first = journal->j_last + 1;
150329a511e4SZhang Yi 		journal->j_fc_off = 0;
150429a511e4SZhang Yi 	}
150529a511e4SZhang Yi 
150629a511e4SZhang Yi 	return 0;
150729a511e4SZhang Yi }
150829a511e4SZhang Yi 
150929a511e4SZhang Yi 
151029a511e4SZhang Yi /*
1511470decc6SDave Kleikamp  * Management for journal control blocks: functions to create and
1512470decc6SDave Kleikamp  * destroy journal_t structures, and to initialise and read existing
1513470decc6SDave Kleikamp  * journal blocks from disk.  */
1514470decc6SDave Kleikamp 
1515470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory.  We initialise
1516470decc6SDave Kleikamp  * very few fields yet: that has to wait until we have created the
1517470decc6SDave Kleikamp  * journal structures from from scratch, or loaded them from disk. */
1518470decc6SDave Kleikamp 
journal_init_common(struct block_device * bdev,struct block_device * fs_dev,unsigned long long start,int len,int blocksize)1519f0c9fd54SGeliang Tang static journal_t *journal_init_common(struct block_device *bdev,
1520f0c9fd54SGeliang Tang 			struct block_device *fs_dev,
1521f0c9fd54SGeliang Tang 			unsigned long long start, int len, int blocksize)
1522470decc6SDave Kleikamp {
1523ab714affSJan Kara 	static struct lock_class_key jbd2_trans_commit_key;
1524470decc6SDave Kleikamp 	journal_t *journal;
1525470decc6SDave Kleikamp 	int err;
1526f0c9fd54SGeliang Tang 	int n;
1527470decc6SDave Kleikamp 
15283ebfdf88SAndrew Morton 	journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1529470decc6SDave Kleikamp 	if (!journal)
15308e6cf5fbSZhang Yi 		return ERR_PTR(-ENOMEM);
1531470decc6SDave Kleikamp 
153249887e47SZhang Yi 	journal->j_blocksize = blocksize;
153349887e47SZhang Yi 	journal->j_dev = bdev;
153449887e47SZhang Yi 	journal->j_fs_dev = fs_dev;
153549887e47SZhang Yi 	journal->j_blk_offset = start;
153649887e47SZhang Yi 	journal->j_total_len = len;
153749887e47SZhang Yi 
153849887e47SZhang Yi 	err = journal_load_superblock(journal);
153949887e47SZhang Yi 	if (err)
154049887e47SZhang Yi 		goto err_cleanup;
1541470decc6SDave Kleikamp 
1542470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_transaction_locked);
1543470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_done_commit);
1544470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_commit);
1545470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_updates);
15468f7d89f3SJan Kara 	init_waitqueue_head(&journal->j_wait_reserved);
1547ff780b91SHarshad Shirwadkar 	init_waitqueue_head(&journal->j_fc_wait);
15487b97d868Szhangyi (F) 	mutex_init(&journal->j_abort_mutex);
1549470decc6SDave Kleikamp 	mutex_init(&journal->j_barrier);
1550470decc6SDave Kleikamp 	mutex_init(&journal->j_checkpoint_mutex);
1551470decc6SDave Kleikamp 	spin_lock_init(&journal->j_revoke_lock);
1552470decc6SDave Kleikamp 	spin_lock_init(&journal->j_list_lock);
155349887e47SZhang Yi 	spin_lock_init(&journal->j_history_lock);
1554a931da6aSTheodore Ts'o 	rwlock_init(&journal->j_state_lock);
1555470decc6SDave Kleikamp 
1556cd02ff0bSMingming Cao 	journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
155730773840STheodore Ts'o 	journal->j_min_batch_time = 0;
155830773840STheodore Ts'o 	journal->j_max_batch_time = 15000; /* 15ms */
15598f7d89f3SJan Kara 	atomic_set(&journal->j_reserved_credits, 0);
156049887e47SZhang Yi 	lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle",
156149887e47SZhang Yi 			 &jbd2_trans_commit_key, 0);
1562470decc6SDave Kleikamp 
1563470decc6SDave Kleikamp 	/* The journal is marked for error until we succeed with recovery! */
1564f7f4bccbSMingming Cao 	journal->j_flags = JBD2_ABORT;
1565470decc6SDave Kleikamp 
1566470decc6SDave Kleikamp 	/* Set up a default-sized revoke table for the new mount. */
1567f7f4bccbSMingming Cao 	err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1568cd9cb405SEric Biggers 	if (err)
1569cd9cb405SEric Biggers 		goto err_cleanup;
15708e85fb3fSJohann Lombardi 
157149887e47SZhang Yi 	/*
157249887e47SZhang Yi 	 * journal descriptor can store up to n blocks, we need enough
157349887e47SZhang Yi 	 * buffers to write out full descriptor block.
157449887e47SZhang Yi 	 */
15758e6cf5fbSZhang Yi 	err = -ENOMEM;
1576b90bfdf5SJan Kara 	n = journal->j_blocksize / jbd2_min_tag_size();
1577f0c9fd54SGeliang Tang 	journal->j_wbufsize = n;
1578a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbuf = NULL;
1579f0c9fd54SGeliang Tang 	journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
1580f0c9fd54SGeliang Tang 					GFP_KERNEL);
1581cd9cb405SEric Biggers 	if (!journal->j_wbuf)
1582cd9cb405SEric Biggers 		goto err_cleanup;
1583f0c9fd54SGeliang Tang 
158449887e47SZhang Yi 	err = percpu_counter_init(&journal->j_checkpoint_jh_count, 0,
158549887e47SZhang Yi 				  GFP_KERNEL);
1586c3071308SZhang Yi 	if (err)
1587cd9cb405SEric Biggers 		goto err_cleanup;
1588f0c9fd54SGeliang Tang 
15890705e8d1STheodore Ts'o 	journal->j_shrink_transaction = NULL;
15900705e8d1STheodore Ts'o 	journal->j_shrinker.scan_objects = jbd2_journal_shrink_scan;
15910705e8d1STheodore Ts'o 	journal->j_shrinker.count_objects = jbd2_journal_shrink_count;
15920705e8d1STheodore Ts'o 	journal->j_shrinker.seeks = DEFAULT_SEEKS;
15930705e8d1STheodore Ts'o 	journal->j_shrinker.batch = journal->j_max_transaction_buffers;
159449887e47SZhang Yi 	err = register_shrinker(&journal->j_shrinker, "jbd2-journal:(%u:%u)",
159549887e47SZhang Yi 				MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
159649887e47SZhang Yi 	if (err)
15970705e8d1STheodore Ts'o 		goto err_cleanup;
15980705e8d1STheodore Ts'o 
1599470decc6SDave Kleikamp 	return journal;
1600cd9cb405SEric Biggers 
1601cd9cb405SEric Biggers err_cleanup:
160249887e47SZhang Yi 	percpu_counter_destroy(&journal->j_checkpoint_jh_count);
16031bb0763fSLi Zetao 	if (journal->j_chksum_driver)
16041bb0763fSLi Zetao 		crypto_free_shash(journal->j_chksum_driver);
1605cd9cb405SEric Biggers 	kfree(journal->j_wbuf);
1606cd9cb405SEric Biggers 	jbd2_journal_destroy_revoke(journal);
160749887e47SZhang Yi 	journal_fail_superblock(journal);
1608cd9cb405SEric Biggers 	kfree(journal);
16098e6cf5fbSZhang Yi 	return ERR_PTR(err);
1610470decc6SDave Kleikamp }
1611470decc6SDave Kleikamp 
1612f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1613470decc6SDave Kleikamp  *
1614470decc6SDave Kleikamp  * Create a journal structure assigned some fixed set of disk blocks to
1615470decc6SDave Kleikamp  * the journal.  We don't actually touch those disk blocks yet, but we
1616470decc6SDave Kleikamp  * need to set up all of the mapping information to tell the journaling
1617470decc6SDave Kleikamp  * system where the journal blocks are.
1618470decc6SDave Kleikamp  *
1619470decc6SDave Kleikamp  */
1620470decc6SDave Kleikamp 
1621470decc6SDave Kleikamp /**
16225648ba5bSRandy Dunlap  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1623470decc6SDave Kleikamp  *  @bdev: Block device on which to create the journal
1624470decc6SDave Kleikamp  *  @fs_dev: Device which hold journalled filesystem for this journal.
1625470decc6SDave Kleikamp  *  @start: Block nr Start of journal.
1626470decc6SDave Kleikamp  *  @len:  Length of the journal in blocks.
1627470decc6SDave Kleikamp  *  @blocksize: blocksize of journalling device
16285648ba5bSRandy Dunlap  *
16295648ba5bSRandy Dunlap  *  Returns: a newly created journal_t *
1630470decc6SDave Kleikamp  *
1631f7f4bccbSMingming Cao  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1632470decc6SDave Kleikamp  *  range of blocks on an arbitrary block device.
1633470decc6SDave Kleikamp  *
1634470decc6SDave Kleikamp  */
jbd2_journal_init_dev(struct block_device * bdev,struct block_device * fs_dev,unsigned long long start,int len,int blocksize)1635f7f4bccbSMingming Cao journal_t *jbd2_journal_init_dev(struct block_device *bdev,
1636470decc6SDave Kleikamp 			struct block_device *fs_dev,
163718eba7aaSMingming Cao 			unsigned long long start, int len, int blocksize)
1638470decc6SDave Kleikamp {
1639f0c9fd54SGeliang Tang 	journal_t *journal;
1640470decc6SDave Kleikamp 
1641f0c9fd54SGeliang Tang 	journal = journal_init_common(bdev, fs_dev, start, len, blocksize);
16428e6cf5fbSZhang Yi 	if (IS_ERR(journal))
16438e6cf5fbSZhang Yi 		return ERR_CAST(journal);
1644470decc6SDave Kleikamp 
1645900d156bSChristoph Hellwig 	snprintf(journal->j_devname, sizeof(journal->j_devname),
1646900d156bSChristoph Hellwig 		 "%pg", journal->j_dev);
164781ae394bSRasmus Villemoes 	strreplace(journal->j_devname, '/', '!');
16484b905671SJan Kara 	jbd2_stats_proc_init(journal);
16494b905671SJan Kara 
1650470decc6SDave Kleikamp 	return journal;
1651470decc6SDave Kleikamp }
1652470decc6SDave Kleikamp 
1653470decc6SDave Kleikamp /**
1654f7f4bccbSMingming Cao  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1655470decc6SDave Kleikamp  *  @inode: An inode to create the journal in
1656470decc6SDave Kleikamp  *
1657f7f4bccbSMingming Cao  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1658470decc6SDave Kleikamp  * the journal.  The inode must exist already, must support bmap() and
1659470decc6SDave Kleikamp  * must have all data blocks preallocated.
1660470decc6SDave Kleikamp  */
jbd2_journal_init_inode(struct inode * inode)1661f7f4bccbSMingming Cao journal_t *jbd2_journal_init_inode(struct inode *inode)
1662470decc6SDave Kleikamp {
1663f0c9fd54SGeliang Tang 	journal_t *journal;
166430460e1eSCarlos Maiolino 	sector_t blocknr;
166530460e1eSCarlos Maiolino 	int err = 0;
1666470decc6SDave Kleikamp 
166730460e1eSCarlos Maiolino 	blocknr = 0;
166830460e1eSCarlos Maiolino 	err = bmap(inode, &blocknr);
166930460e1eSCarlos Maiolino 	if (err || !blocknr) {
16708e6cf5fbSZhang Yi 		pr_err("%s: Cannot locate journal superblock\n", __func__);
16718e6cf5fbSZhang Yi 		return err ? ERR_PTR(err) : ERR_PTR(-EINVAL);
1672f0c9fd54SGeliang Tang 	}
1673f0c9fd54SGeliang Tang 
1674cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n",
1675f0c9fd54SGeliang Tang 		  inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size,
1676f0c9fd54SGeliang Tang 		  inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1677f0c9fd54SGeliang Tang 
1678f0c9fd54SGeliang Tang 	journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev,
1679f0c9fd54SGeliang Tang 			blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits,
1680f0c9fd54SGeliang Tang 			inode->i_sb->s_blocksize);
16818e6cf5fbSZhang Yi 	if (IS_ERR(journal))
16828e6cf5fbSZhang Yi 		return ERR_CAST(journal);
1683470decc6SDave Kleikamp 
1684470decc6SDave Kleikamp 	journal->j_inode = inode;
1685900d156bSChristoph Hellwig 	snprintf(journal->j_devname, sizeof(journal->j_devname),
16867afb6d8fSAndy Shevchenko 		 "%pg-%lu", journal->j_dev, journal->j_inode->i_ino);
16877afb6d8fSAndy Shevchenko 	strreplace(journal->j_devname, '/', '!');
16888e85fb3fSJohann Lombardi 	jbd2_stats_proc_init(journal);
1689470decc6SDave Kleikamp 
1690470decc6SDave Kleikamp 	return journal;
1691470decc6SDave Kleikamp }
1692470decc6SDave Kleikamp 
1693470decc6SDave Kleikamp /*
1694470decc6SDave Kleikamp  * Given a journal_t structure, initialise the various fields for
1695470decc6SDave Kleikamp  * startup of a new journaling session.  We use this both when creating
1696470decc6SDave Kleikamp  * a journal, and after recovering an old journal to reset it for
1697470decc6SDave Kleikamp  * subsequent use.
1698470decc6SDave Kleikamp  */
1699470decc6SDave Kleikamp 
journal_reset(journal_t * journal)1700470decc6SDave Kleikamp static int journal_reset(journal_t *journal)
1701470decc6SDave Kleikamp {
1702470decc6SDave Kleikamp 	journal_superblock_t *sb = journal->j_superblock;
170318eba7aaSMingming Cao 	unsigned long long first, last;
1704470decc6SDave Kleikamp 
1705470decc6SDave Kleikamp 	first = be32_to_cpu(sb->s_first);
1706470decc6SDave Kleikamp 	last = be32_to_cpu(sb->s_maxlen);
1707f6f50e28SJan Kara 	if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1708f2a44523SEryu Guan 		printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1709f6f50e28SJan Kara 		       first, last);
1710f6f50e28SJan Kara 		journal_fail_superblock(journal);
1711f6f50e28SJan Kara 		return -EINVAL;
1712f6f50e28SJan Kara 	}
1713470decc6SDave Kleikamp 
1714470decc6SDave Kleikamp 	journal->j_first = first;
17156866d7b3SHarshad Shirwadkar 	journal->j_last = last;
17166866d7b3SHarshad Shirwadkar 
1717c7fc6055SZhang Yi 	if (journal->j_head != 0 && journal->j_flags & JBD2_CYCLE_RECORD) {
1718c7fc6055SZhang Yi 		/*
1719c7fc6055SZhang Yi 		 * Disable the cycled recording mode if the journal head block
1720c7fc6055SZhang Yi 		 * number is not correct.
1721c7fc6055SZhang Yi 		 */
1722c7fc6055SZhang Yi 		if (journal->j_head < first || journal->j_head >= last) {
1723c7fc6055SZhang Yi 			printk(KERN_WARNING "JBD2: Incorrect Journal head block %lu, "
1724c7fc6055SZhang Yi 			       "disable journal_cycle_record\n",
1725c7fc6055SZhang Yi 			       journal->j_head);
17266866d7b3SHarshad Shirwadkar 			journal->j_head = journal->j_first;
1727c7fc6055SZhang Yi 		}
1728c7fc6055SZhang Yi 	} else {
1729c7fc6055SZhang Yi 		journal->j_head = journal->j_first;
1730c7fc6055SZhang Yi 	}
1731c7fc6055SZhang Yi 	journal->j_tail = journal->j_head;
17326866d7b3SHarshad Shirwadkar 	journal->j_free = journal->j_last - journal->j_first;
1733470decc6SDave Kleikamp 
1734470decc6SDave Kleikamp 	journal->j_tail_sequence = journal->j_transaction_sequence;
1735470decc6SDave Kleikamp 	journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1736470decc6SDave Kleikamp 	journal->j_commit_request = journal->j_commit_sequence;
1737470decc6SDave Kleikamp 
1738ede7dc7fSHarshad Shirwadkar 	journal->j_max_transaction_buffers = jbd2_journal_get_max_txn_bufs(journal);
1739470decc6SDave Kleikamp 
1740470decc6SDave Kleikamp 	/*
1741a1e5e465SHarshad Shirwadkar 	 * Now that journal recovery is done, turn fast commits off here. This
1742a1e5e465SHarshad Shirwadkar 	 * way, if fast commit was enabled before the crash but if now FS has
1743a1e5e465SHarshad Shirwadkar 	 * disabled it, we don't enable fast commits.
1744a1e5e465SHarshad Shirwadkar 	 */
1745a1e5e465SHarshad Shirwadkar 	jbd2_clear_feature_fast_commit(journal);
1746a1e5e465SHarshad Shirwadkar 
1747a1e5e465SHarshad Shirwadkar 	/*
1748470decc6SDave Kleikamp 	 * As a special case, if the on-disk copy is already marked as needing
174924bcc89cSJan Kara 	 * no recovery (s_start == 0), then we can safely defer the superblock
175024bcc89cSJan Kara 	 * update until the next commit by setting JBD2_FLUSHED.  This avoids
1751470decc6SDave Kleikamp 	 * attempting a write to a potential-readonly device.
1752470decc6SDave Kleikamp 	 */
175324bcc89cSJan Kara 	if (sb->s_start == 0) {
1754cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: Skipping superblock update on recovered sb "
17557821ce41SGaowei Pu 			"(start %ld, seq %u, errno %d)\n",
1756470decc6SDave Kleikamp 			journal->j_tail, journal->j_tail_sequence,
1757470decc6SDave Kleikamp 			journal->j_errno);
175824bcc89cSJan Kara 		journal->j_flags |= JBD2_FLUSHED;
175924bcc89cSJan Kara 	} else {
1760a78bb11dSJan Kara 		/* Lock here to make assertions happy... */
17616fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
176279feb521SJan Kara 		/*
176370fd7614SChristoph Hellwig 		 * Update log tail information. We use REQ_FUA since new
176479feb521SJan Kara 		 * transaction will start reusing journal space and so we
176579feb521SJan Kara 		 * must make sure information about current log tail is on
176679feb521SJan Kara 		 * disk before that.
176779feb521SJan Kara 		 */
176879feb521SJan Kara 		jbd2_journal_update_sb_log_tail(journal,
176979feb521SJan Kara 						journal->j_tail_sequence,
1770*5c480a69SZhang Yi 						journal->j_tail, REQ_FUA);
1771a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
177224bcc89cSJan Kara 	}
177324bcc89cSJan Kara 	return jbd2_journal_start_thread(journal);
1774470decc6SDave Kleikamp }
1775470decc6SDave Kleikamp 
1776538bcaa6STheodore Ts'o /*
1777538bcaa6STheodore Ts'o  * This function expects that the caller will have locked the journal
1778538bcaa6STheodore Ts'o  * buffer head, and will return with it unlocked
1779538bcaa6STheodore Ts'o  */
jbd2_write_superblock(journal_t * journal,blk_opf_t write_flags)17806669797bSBart Van Assche static int jbd2_write_superblock(journal_t *journal, blk_opf_t write_flags)
178124bcc89cSJan Kara {
178224bcc89cSJan Kara 	struct buffer_head *bh = journal->j_sb_buffer;
1783fe52d17cSTheodore Ts'o 	journal_superblock_t *sb = journal->j_superblock;
1784f3ed5df3SRitesh Harjani (IBM) 	int ret = 0;
178524bcc89cSJan Kara 
1786742b06b5SJiufei Xue 	/* Buffer got discarded which means block device got invalidated */
1787ef3f5830Szhangyi (F) 	if (!buffer_mapped(bh)) {
1788ef3f5830Szhangyi (F) 		unlock_buffer(bh);
1789742b06b5SJiufei Xue 		return -EIO;
1790ef3f5830Szhangyi (F) 	}
1791742b06b5SJiufei Xue 
1792*5c480a69SZhang Yi 	/*
1793*5c480a69SZhang Yi 	 * Always set high priority flags to exempt from block layer's
1794*5c480a69SZhang Yi 	 * QOS policies, e.g. writeback throttle.
1795*5c480a69SZhang Yi 	 */
1796*5c480a69SZhang Yi 	write_flags |= JBD2_JOURNAL_REQ_FLAGS;
179779feb521SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
179828a8f0d3SMike Christie 		write_flags &= ~(REQ_FUA | REQ_PREFLUSH);
179964596560SZhang Yi 
180064596560SZhang Yi 	trace_jbd2_write_superblock(journal, write_flags);
180164596560SZhang Yi 
1802914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1803914258bfSTheodore Ts'o 		/*
1804914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the journal
1805914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
1806914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
1807914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
1808914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
1809914258bfSTheodore Ts'o 		 * write and hope for the best.
1810914258bfSTheodore Ts'o 		 */
1811914258bfSTheodore Ts'o 		printk(KERN_ERR "JBD2: previous I/O error detected "
1812914258bfSTheodore Ts'o 		       "for journal superblock update for %s.\n",
1813914258bfSTheodore Ts'o 		       journal->j_devname);
1814914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1815914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
1816914258bfSTheodore Ts'o 	}
1817a58ca992STheodore Ts'o 	if (jbd2_journal_has_csum_v2or3(journal))
1818a58ca992STheodore Ts'o 		sb->s_checksum = jbd2_superblock_csum(journal, sb);
181979feb521SJan Kara 	get_bh(bh);
182079feb521SJan Kara 	bh->b_end_io = end_buffer_write_sync;
1821f3ed5df3SRitesh Harjani (IBM) 	submit_bh(REQ_OP_WRITE | write_flags, bh);
182279feb521SJan Kara 	wait_on_buffer(bh);
1823914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1824914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1825914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
182679feb521SJan Kara 		ret = -EIO;
182779feb521SJan Kara 	}
182879feb521SJan Kara 	if (ret) {
1829f3ed5df3SRitesh Harjani (IBM) 		printk(KERN_ERR "JBD2: I/O error when updating journal superblock for %s.\n",
183079feb521SJan Kara 				journal->j_devname);
18317b97d868Szhangyi (F) 		if (!is_journal_aborted(journal))
18326f6a6fdaSJoseph Qi 			jbd2_journal_abort(journal, ret);
1833914258bfSTheodore Ts'o 	}
18346f6a6fdaSJoseph Qi 
18356f6a6fdaSJoseph Qi 	return ret;
183624bcc89cSJan Kara }
1837470decc6SDave Kleikamp 
183824bcc89cSJan Kara /**
183924bcc89cSJan Kara  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
184024bcc89cSJan Kara  * @journal: The journal to update.
184179feb521SJan Kara  * @tail_tid: TID of the new transaction at the tail of the log
184279feb521SJan Kara  * @tail_block: The first block of the transaction at the tail of the log
18436669797bSBart Van Assche  * @write_flags: Flags for the journal sb write operation
184424bcc89cSJan Kara  *
184524bcc89cSJan Kara  * Update a journal's superblock information about log tail and write it to
184624bcc89cSJan Kara  * disk, waiting for the IO to complete.
184724bcc89cSJan Kara  */
jbd2_journal_update_sb_log_tail(journal_t * journal,tid_t tail_tid,unsigned long tail_block,blk_opf_t write_flags)18486f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
18496669797bSBart Van Assche 				    unsigned long tail_block,
18506669797bSBart Van Assche 				    blk_opf_t write_flags)
185124bcc89cSJan Kara {
185224bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
18536f6a6fdaSJoseph Qi 	int ret;
18542201c590SSeiji Aguchi 
185585e0c4e8STheodore Ts'o 	if (is_journal_aborted(journal))
185685e0c4e8STheodore Ts'o 		return -EIO;
1857fcf37549SZhang Yi 	if (test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) {
1858fcf37549SZhang Yi 		jbd2_journal_abort(journal, -EIO);
1859fcf37549SZhang Yi 		return -EIO;
1860fcf37549SZhang Yi 	}
186185e0c4e8STheodore Ts'o 
1862a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1863cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
186479feb521SJan Kara 		  tail_block, tail_tid);
1865470decc6SDave Kleikamp 
1866538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
186779feb521SJan Kara 	sb->s_sequence = cpu_to_be32(tail_tid);
186879feb521SJan Kara 	sb->s_start    = cpu_to_be32(tail_block);
186924bcc89cSJan Kara 
18706669797bSBart Van Assche 	ret = jbd2_write_superblock(journal, write_flags);
18716f6a6fdaSJoseph Qi 	if (ret)
18726f6a6fdaSJoseph Qi 		goto out;
187324bcc89cSJan Kara 
187424bcc89cSJan Kara 	/* Log is no longer empty */
1875a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
187624bcc89cSJan Kara 	WARN_ON(!sb->s_sequence);
1877f7f4bccbSMingming Cao 	journal->j_flags &= ~JBD2_FLUSHED;
187824bcc89cSJan Kara 	write_unlock(&journal->j_state_lock);
18796f6a6fdaSJoseph Qi 
18806f6a6fdaSJoseph Qi out:
18816f6a6fdaSJoseph Qi 	return ret;
188224bcc89cSJan Kara }
188324bcc89cSJan Kara 
188424bcc89cSJan Kara /**
188524bcc89cSJan Kara  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
188624bcc89cSJan Kara  * @journal: The journal to update.
18876669797bSBart Van Assche  * @write_flags: Flags for the journal sb write operation
188824bcc89cSJan Kara  *
188924bcc89cSJan Kara  * Update a journal's dynamic superblock fields to show that journal is empty.
189024bcc89cSJan Kara  * Write updated superblock to disk waiting for IO to complete.
189124bcc89cSJan Kara  */
jbd2_mark_journal_empty(journal_t * journal,blk_opf_t write_flags)18926669797bSBart Van Assche static void jbd2_mark_journal_empty(journal_t *journal, blk_opf_t write_flags)
189324bcc89cSJan Kara {
189424bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
1895ff780b91SHarshad Shirwadkar 	bool had_fast_commit = false;
189624bcc89cSJan Kara 
1897a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1898538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
1899538bcaa6STheodore Ts'o 	if (sb->s_start == 0) {		/* Is it already empty? */
1900538bcaa6STheodore Ts'o 		unlock_buffer(journal->j_sb_buffer);
1901eeecef0aSEric Sandeen 		return;
1902eeecef0aSEric Sandeen 	}
1903538bcaa6STheodore Ts'o 
1904cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: Marking journal as empty (seq %u)\n",
190524bcc89cSJan Kara 		  journal->j_tail_sequence);
1906470decc6SDave Kleikamp 
1907470decc6SDave Kleikamp 	sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
190824bcc89cSJan Kara 	sb->s_start    = cpu_to_be32(0);
1909c7fc6055SZhang Yi 	sb->s_head     = cpu_to_be32(journal->j_head);
1910ff780b91SHarshad Shirwadkar 	if (jbd2_has_feature_fast_commit(journal)) {
1911ff780b91SHarshad Shirwadkar 		/*
1912ff780b91SHarshad Shirwadkar 		 * When journal is clean, no need to commit fast commit flag and
1913ff780b91SHarshad Shirwadkar 		 * make file system incompatible with older kernels.
1914ff780b91SHarshad Shirwadkar 		 */
1915ff780b91SHarshad Shirwadkar 		jbd2_clear_feature_fast_commit(journal);
1916ff780b91SHarshad Shirwadkar 		had_fast_commit = true;
1917ff780b91SHarshad Shirwadkar 	}
191824bcc89cSJan Kara 
19196669797bSBart Van Assche 	jbd2_write_superblock(journal, write_flags);
192024bcc89cSJan Kara 
1921ff780b91SHarshad Shirwadkar 	if (had_fast_commit)
1922ff780b91SHarshad Shirwadkar 		jbd2_set_feature_fast_commit(journal);
1923ff780b91SHarshad Shirwadkar 
192424bcc89cSJan Kara 	/* Log is no longer empty */
192524bcc89cSJan Kara 	write_lock(&journal->j_state_lock);
1926f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_FLUSHED;
1927a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
1928470decc6SDave Kleikamp }
1929470decc6SDave Kleikamp 
193001d5d965SLeah Rumancik /**
193101d5d965SLeah Rumancik  * __jbd2_journal_erase() - Discard or zeroout journal blocks (excluding superblock)
193201d5d965SLeah Rumancik  * @journal: The journal to erase.
193301d5d965SLeah Rumancik  * @flags: A discard/zeroout request is sent for each physically contigous
193401d5d965SLeah Rumancik  *	region of the journal. Either JBD2_JOURNAL_FLUSH_DISCARD or
193501d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_ZEROOUT must be set to determine which operation
193601d5d965SLeah Rumancik  *	to perform.
193701d5d965SLeah Rumancik  *
193801d5d965SLeah Rumancik  * Note: JBD2_JOURNAL_FLUSH_ZEROOUT attempts to use hardware offload. Zeroes
193901d5d965SLeah Rumancik  * will be explicitly written if no hardware offload is available, see
194001d5d965SLeah Rumancik  * blkdev_issue_zeroout for more details.
194101d5d965SLeah Rumancik  */
__jbd2_journal_erase(journal_t * journal,unsigned int flags)194201d5d965SLeah Rumancik static int __jbd2_journal_erase(journal_t *journal, unsigned int flags)
194301d5d965SLeah Rumancik {
194401d5d965SLeah Rumancik 	int err = 0;
194501d5d965SLeah Rumancik 	unsigned long block, log_offset; /* logical */
194601d5d965SLeah Rumancik 	unsigned long long phys_block, block_start, block_stop; /* physical */
194701d5d965SLeah Rumancik 	loff_t byte_start, byte_stop, byte_count;
194801d5d965SLeah Rumancik 
194901d5d965SLeah Rumancik 	/* flags must be set to either discard or zeroout */
195001d5d965SLeah Rumancik 	if ((flags & ~JBD2_JOURNAL_FLUSH_VALID) || !flags ||
195101d5d965SLeah Rumancik 			((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
195201d5d965SLeah Rumancik 			(flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
195301d5d965SLeah Rumancik 		return -EINVAL;
195401d5d965SLeah Rumancik 
195570200574SChristoph Hellwig 	if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
195670200574SChristoph Hellwig 	    !bdev_max_discard_sectors(journal->j_dev))
195701d5d965SLeah Rumancik 		return -EOPNOTSUPP;
195801d5d965SLeah Rumancik 
195901d5d965SLeah Rumancik 	/*
196001d5d965SLeah Rumancik 	 * lookup block mapping and issue discard/zeroout for each
196101d5d965SLeah Rumancik 	 * contiguous region
196201d5d965SLeah Rumancik 	 */
196301d5d965SLeah Rumancik 	log_offset = be32_to_cpu(journal->j_superblock->s_first);
196401d5d965SLeah Rumancik 	block_start =  ~0ULL;
196501d5d965SLeah Rumancik 	for (block = log_offset; block < journal->j_total_len; block++) {
196601d5d965SLeah Rumancik 		err = jbd2_journal_bmap(journal, block, &phys_block);
196701d5d965SLeah Rumancik 		if (err) {
196801d5d965SLeah Rumancik 			pr_err("JBD2: bad block at offset %lu", block);
196901d5d965SLeah Rumancik 			return err;
197001d5d965SLeah Rumancik 		}
197101d5d965SLeah Rumancik 
197201d5d965SLeah Rumancik 		if (block_start == ~0ULL) {
197301d5d965SLeah Rumancik 			block_start = phys_block;
197401d5d965SLeah Rumancik 			block_stop = block_start - 1;
197501d5d965SLeah Rumancik 		}
197601d5d965SLeah Rumancik 
197701d5d965SLeah Rumancik 		/*
197801d5d965SLeah Rumancik 		 * last block not contiguous with current block,
197901d5d965SLeah Rumancik 		 * process last contiguous region and return to this block on
198001d5d965SLeah Rumancik 		 * next loop
198101d5d965SLeah Rumancik 		 */
198201d5d965SLeah Rumancik 		if (phys_block != block_stop + 1) {
198301d5d965SLeah Rumancik 			block--;
198401d5d965SLeah Rumancik 		} else {
198501d5d965SLeah Rumancik 			block_stop++;
198601d5d965SLeah Rumancik 			/*
198701d5d965SLeah Rumancik 			 * if this isn't the last block of journal,
198801d5d965SLeah Rumancik 			 * no need to process now because next block may also
198901d5d965SLeah Rumancik 			 * be part of this contiguous region
199001d5d965SLeah Rumancik 			 */
199101d5d965SLeah Rumancik 			if (block != journal->j_total_len - 1)
199201d5d965SLeah Rumancik 				continue;
199301d5d965SLeah Rumancik 		}
199401d5d965SLeah Rumancik 
199501d5d965SLeah Rumancik 		/*
199601d5d965SLeah Rumancik 		 * end of contiguous region or this is last block of journal,
199701d5d965SLeah Rumancik 		 * take care of the region
199801d5d965SLeah Rumancik 		 */
199901d5d965SLeah Rumancik 		byte_start = block_start * journal->j_blocksize;
200001d5d965SLeah Rumancik 		byte_stop = block_stop * journal->j_blocksize;
200101d5d965SLeah Rumancik 		byte_count = (block_stop - block_start + 1) *
200201d5d965SLeah Rumancik 				journal->j_blocksize;
200301d5d965SLeah Rumancik 
200401d5d965SLeah Rumancik 		truncate_inode_pages_range(journal->j_dev->bd_inode->i_mapping,
200501d5d965SLeah Rumancik 				byte_start, byte_stop);
200601d5d965SLeah Rumancik 
200701d5d965SLeah Rumancik 		if (flags & JBD2_JOURNAL_FLUSH_DISCARD) {
200801d5d965SLeah Rumancik 			err = blkdev_issue_discard(journal->j_dev,
200901d5d965SLeah Rumancik 					byte_start >> SECTOR_SHIFT,
201001d5d965SLeah Rumancik 					byte_count >> SECTOR_SHIFT,
201144abff2cSChristoph Hellwig 					GFP_NOFS);
201201d5d965SLeah Rumancik 		} else if (flags & JBD2_JOURNAL_FLUSH_ZEROOUT) {
201301d5d965SLeah Rumancik 			err = blkdev_issue_zeroout(journal->j_dev,
201401d5d965SLeah Rumancik 					byte_start >> SECTOR_SHIFT,
201501d5d965SLeah Rumancik 					byte_count >> SECTOR_SHIFT,
201601d5d965SLeah Rumancik 					GFP_NOFS, 0);
201701d5d965SLeah Rumancik 		}
201801d5d965SLeah Rumancik 
201901d5d965SLeah Rumancik 		if (unlikely(err != 0)) {
202001d5d965SLeah Rumancik 			pr_err("JBD2: (error %d) unable to wipe journal at physical blocks %llu - %llu",
202101d5d965SLeah Rumancik 					err, block_start, block_stop);
202201d5d965SLeah Rumancik 			return err;
202301d5d965SLeah Rumancik 		}
202401d5d965SLeah Rumancik 
202501d5d965SLeah Rumancik 		/* reset start and stop after processing a region */
202601d5d965SLeah Rumancik 		block_start = ~0ULL;
202701d5d965SLeah Rumancik 	}
202801d5d965SLeah Rumancik 
202901d5d965SLeah Rumancik 	return blkdev_issue_flush(journal->j_dev);
203001d5d965SLeah Rumancik }
203124bcc89cSJan Kara 
203224bcc89cSJan Kara /**
203324bcc89cSJan Kara  * jbd2_journal_update_sb_errno() - Update error in the journal.
203424bcc89cSJan Kara  * @journal: The journal to update.
203524bcc89cSJan Kara  *
203624bcc89cSJan Kara  * Update a journal's errno.  Write updated superblock to disk waiting for IO
203724bcc89cSJan Kara  * to complete.
203824bcc89cSJan Kara  */
jbd2_journal_update_sb_errno(journal_t * journal)2039d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal)
204024bcc89cSJan Kara {
204124bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
2042fb7c0244STheodore Ts'o 	int errcode;
204324bcc89cSJan Kara 
2044538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
2045fb7c0244STheodore Ts'o 	errcode = journal->j_errno;
2046fb7c0244STheodore Ts'o 	if (errcode == -ESHUTDOWN)
2047fb7c0244STheodore Ts'o 		errcode = 0;
2048cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
2049fb7c0244STheodore Ts'o 	sb->s_errno    = cpu_to_be32(errcode);
2050470decc6SDave Kleikamp 
2051*5c480a69SZhang Yi 	jbd2_write_superblock(journal, REQ_FUA);
2052470decc6SDave Kleikamp }
2053d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
2054470decc6SDave Kleikamp 
2055470decc6SDave Kleikamp /**
20562bf31d94SMauro Carvalho Chehab  * jbd2_journal_load() - Read journal from disk.
2057470decc6SDave Kleikamp  * @journal: Journal to act on.
2058470decc6SDave Kleikamp  *
2059470decc6SDave Kleikamp  * Given a journal_t structure which tells us which disk blocks contain
2060470decc6SDave Kleikamp  * a journal, read the journal from disk to initialise the in-memory
2061470decc6SDave Kleikamp  * structures.
2062470decc6SDave Kleikamp  */
jbd2_journal_load(journal_t * journal)2063f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal)
2064470decc6SDave Kleikamp {
2065470decc6SDave Kleikamp 	int err;
2066c3071308SZhang Yi 	journal_superblock_t *sb = journal->j_superblock;
2067470decc6SDave Kleikamp 
2068d2eecb03STheodore Ts'o 	/*
2069d2eecb03STheodore Ts'o 	 * Create a slab for this blocksize
2070d2eecb03STheodore Ts'o 	 */
2071d2eecb03STheodore Ts'o 	err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
2072d2eecb03STheodore Ts'o 	if (err)
2073d2eecb03STheodore Ts'o 		return err;
2074d2eecb03STheodore Ts'o 
2075470decc6SDave Kleikamp 	/* Let the recovery code check whether it needs to recover any
2076470decc6SDave Kleikamp 	 * data from the journal. */
20771d401650SGuoqing Cai 	err = jbd2_journal_recover(journal);
20781d401650SGuoqing Cai 	if (err) {
20791d401650SGuoqing Cai 		pr_warn("JBD2: journal recovery failed\n");
20801d401650SGuoqing Cai 		return err;
20811d401650SGuoqing Cai 	}
2082470decc6SDave Kleikamp 
2083e6a47428STheodore Ts'o 	if (journal->j_failed_commit) {
2084e6a47428STheodore Ts'o 		printk(KERN_ERR "JBD2: journal transaction %u on %s "
2085e6a47428STheodore Ts'o 		       "is corrupt.\n", journal->j_failed_commit,
2086e6a47428STheodore Ts'o 		       journal->j_devname);
20876a797d27SDarrick J. Wong 		return -EFSCORRUPTED;
2088e6a47428STheodore Ts'o 	}
2089a09decffSKai Li 	/*
2090a09decffSKai Li 	 * clear JBD2_ABORT flag initialized in journal_init_common
2091a09decffSKai Li 	 * here to update log tail information with the newest seq.
2092a09decffSKai Li 	 */
2093a09decffSKai Li 	journal->j_flags &= ~JBD2_ABORT;
2094e6a47428STheodore Ts'o 
2095470decc6SDave Kleikamp 	/* OK, we've finished with the dynamic journal bits:
2096470decc6SDave Kleikamp 	 * reinitialise the dynamic contents of the superblock in memory
2097470decc6SDave Kleikamp 	 * and reset them on disk. */
20981d401650SGuoqing Cai 	err = journal_reset(journal);
20991d401650SGuoqing Cai 	if (err) {
21001d401650SGuoqing Cai 		pr_warn("JBD2: journal reset failed\n");
21011d401650SGuoqing Cai 		return err;
21021d401650SGuoqing Cai 	}
2103470decc6SDave Kleikamp 
2104f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_LOADED;
2105470decc6SDave Kleikamp 	return 0;
2106470decc6SDave Kleikamp }
2107470decc6SDave Kleikamp 
2108470decc6SDave Kleikamp /**
21092bf31d94SMauro Carvalho Chehab  * jbd2_journal_destroy() - Release a journal_t structure.
2110470decc6SDave Kleikamp  * @journal: Journal to act on.
2111470decc6SDave Kleikamp  *
2112470decc6SDave Kleikamp  * Release a journal_t structure once it is no longer in use by the
2113470decc6SDave Kleikamp  * journaled object.
211444519fafSHidehiro Kawai  * Return <0 if we couldn't clean up the journal.
2115470decc6SDave Kleikamp  */
jbd2_journal_destroy(journal_t * journal)211644519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal)
2117470decc6SDave Kleikamp {
211844519fafSHidehiro Kawai 	int err = 0;
211944519fafSHidehiro Kawai 
2120470decc6SDave Kleikamp 	/* Wait for the commit thread to wake up and die. */
2121470decc6SDave Kleikamp 	journal_kill_thread(journal);
2122470decc6SDave Kleikamp 
2123470decc6SDave Kleikamp 	/* Force a final log commit */
2124470decc6SDave Kleikamp 	if (journal->j_running_transaction)
2125f7f4bccbSMingming Cao 		jbd2_journal_commit_transaction(journal);
2126470decc6SDave Kleikamp 
2127470decc6SDave Kleikamp 	/* Force any old transactions to disk */
2128470decc6SDave Kleikamp 
2129470decc6SDave Kleikamp 	/* Totally anal locking here... */
2130470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
2131470decc6SDave Kleikamp 	while (journal->j_checkpoint_transactions != NULL) {
2132470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
21336fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
2134841df7dfSJan Kara 		err = jbd2_log_do_checkpoint(journal);
21351a0d3786STheodore Ts'o 		mutex_unlock(&journal->j_checkpoint_mutex);
2136841df7dfSJan Kara 		/*
2137841df7dfSJan Kara 		 * If checkpointing failed, just free the buffers to avoid
2138841df7dfSJan Kara 		 * looping forever
2139841df7dfSJan Kara 		 */
2140841df7dfSJan Kara 		if (err) {
2141841df7dfSJan Kara 			jbd2_journal_destroy_checkpoint(journal);
2142841df7dfSJan Kara 			spin_lock(&journal->j_list_lock);
2143841df7dfSJan Kara 			break;
2144841df7dfSJan Kara 		}
2145470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
2146470decc6SDave Kleikamp 	}
2147470decc6SDave Kleikamp 
2148470decc6SDave Kleikamp 	J_ASSERT(journal->j_running_transaction == NULL);
2149470decc6SDave Kleikamp 	J_ASSERT(journal->j_committing_transaction == NULL);
2150470decc6SDave Kleikamp 	J_ASSERT(journal->j_checkpoint_transactions == NULL);
2151470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
2152470decc6SDave Kleikamp 
2153fcf37549SZhang Yi 	/*
2154fcf37549SZhang Yi 	 * OK, all checkpoint transactions have been checked, now check the
2155fcf37549SZhang Yi 	 * write out io error flag and abort the journal if some buffer failed
2156fcf37549SZhang Yi 	 * to write back to the original location, otherwise the filesystem
2157fcf37549SZhang Yi 	 * may become inconsistent.
2158fcf37549SZhang Yi 	 */
2159fcf37549SZhang Yi 	if (!is_journal_aborted(journal) &&
2160fcf37549SZhang Yi 	    test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags))
2161fcf37549SZhang Yi 		jbd2_journal_abort(journal, -EIO);
2162fcf37549SZhang Yi 
216344519fafSHidehiro Kawai 	if (journal->j_sb_buffer) {
216444519fafSHidehiro Kawai 		if (!is_journal_aborted(journal)) {
21656fa7aa50STejun Heo 			mutex_lock_io(&journal->j_checkpoint_mutex);
2166c0a2ad9bSOGAWA Hirofumi 
2167c0a2ad9bSOGAWA Hirofumi 			write_lock(&journal->j_state_lock);
2168c0a2ad9bSOGAWA Hirofumi 			journal->j_tail_sequence =
2169c0a2ad9bSOGAWA Hirofumi 				++journal->j_transaction_sequence;
2170c0a2ad9bSOGAWA Hirofumi 			write_unlock(&journal->j_state_lock);
2171c0a2ad9bSOGAWA Hirofumi 
2172*5c480a69SZhang Yi 			jbd2_mark_journal_empty(journal, REQ_PREFLUSH | REQ_FUA);
2173a78bb11dSJan Kara 			mutex_unlock(&journal->j_checkpoint_mutex);
2174a78bb11dSJan Kara 		} else
217544519fafSHidehiro Kawai 			err = -EIO;
2176470decc6SDave Kleikamp 		brelse(journal->j_sb_buffer);
2177470decc6SDave Kleikamp 	}
2178470decc6SDave Kleikamp 
21790705e8d1STheodore Ts'o 	if (journal->j_shrinker.flags & SHRINKER_REGISTERED) {
21800705e8d1STheodore Ts'o 		percpu_counter_destroy(&journal->j_checkpoint_jh_count);
21810705e8d1STheodore Ts'o 		unregister_shrinker(&journal->j_shrinker);
21820705e8d1STheodore Ts'o 	}
21838e85fb3fSJohann Lombardi 	if (journal->j_proc_entry)
21848e85fb3fSJohann Lombardi 		jbd2_stats_proc_exit(journal);
2185470decc6SDave Kleikamp 	iput(journal->j_inode);
2186470decc6SDave Kleikamp 	if (journal->j_revoke)
2187f7f4bccbSMingming Cao 		jbd2_journal_destroy_revoke(journal);
218801b5adceSDarrick J. Wong 	if (journal->j_chksum_driver)
218901b5adceSDarrick J. Wong 		crypto_free_shash(journal->j_chksum_driver);
21906866d7b3SHarshad Shirwadkar 	kfree(journal->j_fc_wbuf);
2191470decc6SDave Kleikamp 	kfree(journal->j_wbuf);
2192470decc6SDave Kleikamp 	kfree(journal);
219344519fafSHidehiro Kawai 
219444519fafSHidehiro Kawai 	return err;
2195470decc6SDave Kleikamp }
2196470decc6SDave Kleikamp 
2197470decc6SDave Kleikamp 
2198470decc6SDave Kleikamp /**
21992bf31d94SMauro Carvalho Chehab  * jbd2_journal_check_used_features() - Check if features specified are used.
2200470decc6SDave Kleikamp  * @journal: Journal to check.
2201470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2202470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2203470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2204470decc6SDave Kleikamp  *
2205470decc6SDave Kleikamp  * Check whether the journal uses all of a given set of
2206470decc6SDave Kleikamp  * features.  Return true (non-zero) if it does.
2207470decc6SDave Kleikamp  **/
2208470decc6SDave Kleikamp 
jbd2_journal_check_used_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2209f7f4bccbSMingming Cao int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat,
2210470decc6SDave Kleikamp 				 unsigned long ro, unsigned long incompat)
2211470decc6SDave Kleikamp {
2212470decc6SDave Kleikamp 	journal_superblock_t *sb;
2213470decc6SDave Kleikamp 
2214470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
2215470decc6SDave Kleikamp 		return 1;
22165cf036d4SZhang Yi 	if (!jbd2_format_support_feature(journal))
2217470decc6SDave Kleikamp 		return 0;
2218470decc6SDave Kleikamp 
2219470decc6SDave Kleikamp 	sb = journal->j_superblock;
2220470decc6SDave Kleikamp 
2221470decc6SDave Kleikamp 	if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
2222470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
2223470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
2224470decc6SDave Kleikamp 		return 1;
2225470decc6SDave Kleikamp 
2226470decc6SDave Kleikamp 	return 0;
2227470decc6SDave Kleikamp }
2228470decc6SDave Kleikamp 
2229470decc6SDave Kleikamp /**
22302bf31d94SMauro Carvalho Chehab  * jbd2_journal_check_available_features() - Check feature set in journalling layer
2231470decc6SDave Kleikamp  * @journal: Journal to check.
2232470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2233470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2234470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2235470decc6SDave Kleikamp  *
2236470decc6SDave Kleikamp  * Check whether the journaling code supports the use of
2237470decc6SDave Kleikamp  * all of a given set of features on this journal.  Return true
2238470decc6SDave Kleikamp  * (non-zero) if it can. */
2239470decc6SDave Kleikamp 
jbd2_journal_check_available_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2240f7f4bccbSMingming Cao int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat,
2241470decc6SDave Kleikamp 				      unsigned long ro, unsigned long incompat)
2242470decc6SDave Kleikamp {
2243470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
2244470decc6SDave Kleikamp 		return 1;
2245470decc6SDave Kleikamp 
22465cf036d4SZhang Yi 	if (!jbd2_format_support_feature(journal))
2247470decc6SDave Kleikamp 		return 0;
2248470decc6SDave Kleikamp 
2249f7f4bccbSMingming Cao 	if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
2250f7f4bccbSMingming Cao 	    (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
2251f7f4bccbSMingming Cao 	    (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
2252470decc6SDave Kleikamp 		return 1;
2253470decc6SDave Kleikamp 
2254470decc6SDave Kleikamp 	return 0;
2255470decc6SDave Kleikamp }
2256470decc6SDave Kleikamp 
2257a1e5e465SHarshad Shirwadkar static int
jbd2_journal_initialize_fast_commit(journal_t * journal)2258a1e5e465SHarshad Shirwadkar jbd2_journal_initialize_fast_commit(journal_t *journal)
2259a1e5e465SHarshad Shirwadkar {
2260a1e5e465SHarshad Shirwadkar 	journal_superblock_t *sb = journal->j_superblock;
2261a1e5e465SHarshad Shirwadkar 	unsigned long long num_fc_blks;
2262a1e5e465SHarshad Shirwadkar 
22639bd23c31SHarshad Shirwadkar 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
2264a1e5e465SHarshad Shirwadkar 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
2265a1e5e465SHarshad Shirwadkar 		return -ENOSPC;
2266a1e5e465SHarshad Shirwadkar 
2267a1e5e465SHarshad Shirwadkar 	/* Are we called twice? */
2268a1e5e465SHarshad Shirwadkar 	WARN_ON(journal->j_fc_wbuf != NULL);
2269a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbuf = kmalloc_array(num_fc_blks,
2270a1e5e465SHarshad Shirwadkar 				sizeof(struct buffer_head *), GFP_KERNEL);
2271a1e5e465SHarshad Shirwadkar 	if (!journal->j_fc_wbuf)
2272a1e5e465SHarshad Shirwadkar 		return -ENOMEM;
2273a1e5e465SHarshad Shirwadkar 
2274a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbufsize = num_fc_blks;
2275a1e5e465SHarshad Shirwadkar 	journal->j_fc_last = journal->j_last;
2276a1e5e465SHarshad Shirwadkar 	journal->j_last = journal->j_fc_last - num_fc_blks;
2277a1e5e465SHarshad Shirwadkar 	journal->j_fc_first = journal->j_last + 1;
2278a1e5e465SHarshad Shirwadkar 	journal->j_fc_off = 0;
2279a1e5e465SHarshad Shirwadkar 	journal->j_free = journal->j_last - journal->j_first;
2280a1e5e465SHarshad Shirwadkar 	journal->j_max_transaction_buffers =
2281a1e5e465SHarshad Shirwadkar 		jbd2_journal_get_max_txn_bufs(journal);
2282a1e5e465SHarshad Shirwadkar 
2283a1e5e465SHarshad Shirwadkar 	return 0;
2284a1e5e465SHarshad Shirwadkar }
2285a1e5e465SHarshad Shirwadkar 
2286470decc6SDave Kleikamp /**
22872bf31d94SMauro Carvalho Chehab  * jbd2_journal_set_features() - Mark a given journal feature in the superblock
2288470decc6SDave Kleikamp  * @journal: Journal to act on.
2289470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2290470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2291470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2292470decc6SDave Kleikamp  *
2293470decc6SDave Kleikamp  * Mark a given journal feature as present on the
2294470decc6SDave Kleikamp  * superblock.  Returns true if the requested features could be set.
2295470decc6SDave Kleikamp  *
2296470decc6SDave Kleikamp  */
2297470decc6SDave Kleikamp 
jbd2_journal_set_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2298f7f4bccbSMingming Cao int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
2299470decc6SDave Kleikamp 			  unsigned long ro, unsigned long incompat)
2300470decc6SDave Kleikamp {
230125ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \
230225ed6e8aSDarrick J. Wong 		((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
230325ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \
230425ed6e8aSDarrick J. Wong 		((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
2305470decc6SDave Kleikamp 	journal_superblock_t *sb;
2306470decc6SDave Kleikamp 
2307f7f4bccbSMingming Cao 	if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
2308470decc6SDave Kleikamp 		return 1;
2309470decc6SDave Kleikamp 
2310f7f4bccbSMingming Cao 	if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
2311470decc6SDave Kleikamp 		return 0;
2312470decc6SDave Kleikamp 
2313db9ee220SDarrick J. Wong 	/* If enabling v2 checksums, turn on v3 instead */
2314db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
2315db9ee220SDarrick J. Wong 		incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
2316db9ee220SDarrick J. Wong 		incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
2317db9ee220SDarrick J. Wong 	}
2318db9ee220SDarrick J. Wong 
2319db9ee220SDarrick J. Wong 	/* Asking for checksumming v3 and v1?  Only give them v3. */
2320db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
232125ed6e8aSDarrick J. Wong 	    compat & JBD2_FEATURE_COMPAT_CHECKSUM)
232225ed6e8aSDarrick J. Wong 		compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
232325ed6e8aSDarrick J. Wong 
2324cb3b3bf2SJan Kara 	jbd2_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
2325470decc6SDave Kleikamp 		  compat, ro, incompat);
2326470decc6SDave Kleikamp 
2327470decc6SDave Kleikamp 	sb = journal->j_superblock;
2328470decc6SDave Kleikamp 
2329a1e5e465SHarshad Shirwadkar 	if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) {
2330a1e5e465SHarshad Shirwadkar 		if (jbd2_journal_initialize_fast_commit(journal)) {
2331a1e5e465SHarshad Shirwadkar 			pr_err("JBD2: Cannot enable fast commits.\n");
2332a1e5e465SHarshad Shirwadkar 			return 0;
2333a1e5e465SHarshad Shirwadkar 		}
2334a1e5e465SHarshad Shirwadkar 	}
2335a1e5e465SHarshad Shirwadkar 
2336538bcaa6STheodore Ts'o 	/* Load the checksum driver if necessary */
2337538bcaa6STheodore Ts'o 	if ((journal->j_chksum_driver == NULL) &&
2338538bcaa6STheodore Ts'o 	    INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
2339538bcaa6STheodore Ts'o 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
2340538bcaa6STheodore Ts'o 		if (IS_ERR(journal->j_chksum_driver)) {
2341538bcaa6STheodore Ts'o 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
2342538bcaa6STheodore Ts'o 			journal->j_chksum_driver = NULL;
2343538bcaa6STheodore Ts'o 			return 0;
2344538bcaa6STheodore Ts'o 		}
2345538bcaa6STheodore Ts'o 		/* Precompute checksum seed for all metadata */
2346538bcaa6STheodore Ts'o 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
2347538bcaa6STheodore Ts'o 						   sizeof(sb->s_uuid));
2348538bcaa6STheodore Ts'o 	}
2349538bcaa6STheodore Ts'o 
2350538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
2351538bcaa6STheodore Ts'o 
2352db9ee220SDarrick J. Wong 	/* If enabling v3 checksums, update superblock */
2353db9ee220SDarrick J. Wong 	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
235425ed6e8aSDarrick J. Wong 		sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
235525ed6e8aSDarrick J. Wong 		sb->s_feature_compat &=
235625ed6e8aSDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
235732f38691SDarrick J. Wong 	}
235825ed6e8aSDarrick J. Wong 
235925ed6e8aSDarrick J. Wong 	/* If enabling v1 checksums, downgrade superblock */
236025ed6e8aSDarrick J. Wong 	if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
236125ed6e8aSDarrick J. Wong 		sb->s_feature_incompat &=
2362db9ee220SDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
2363db9ee220SDarrick J. Wong 				     JBD2_FEATURE_INCOMPAT_CSUM_V3);
236425ed6e8aSDarrick J. Wong 
2365470decc6SDave Kleikamp 	sb->s_feature_compat    |= cpu_to_be32(compat);
2366470decc6SDave Kleikamp 	sb->s_feature_ro_compat |= cpu_to_be32(ro);
2367470decc6SDave Kleikamp 	sb->s_feature_incompat  |= cpu_to_be32(incompat);
2368538bcaa6STheodore Ts'o 	unlock_buffer(journal->j_sb_buffer);
2369fdc3ef88SJan Kara 	journal->j_revoke_records_per_block =
2370fdc3ef88SJan Kara 				journal_revoke_records_per_block(journal);
2371470decc6SDave Kleikamp 
2372470decc6SDave Kleikamp 	return 1;
237325ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON
237425ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON
2375470decc6SDave Kleikamp }
2376470decc6SDave Kleikamp 
2377818d276cSGirish Shilamkar /*
2378818d276cSGirish Shilamkar  * jbd2_journal_clear_features() - Clear a given journal feature in the
2379818d276cSGirish Shilamkar  * 				    superblock
2380818d276cSGirish Shilamkar  * @journal: Journal to act on.
2381818d276cSGirish Shilamkar  * @compat: bitmask of compatible features
2382818d276cSGirish Shilamkar  * @ro: bitmask of features that force read-only mount
2383818d276cSGirish Shilamkar  * @incompat: bitmask of incompatible features
2384818d276cSGirish Shilamkar  *
2385818d276cSGirish Shilamkar  * Clear a given journal feature as present on the
2386818d276cSGirish Shilamkar  * superblock.
2387818d276cSGirish Shilamkar  */
jbd2_journal_clear_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2388818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
2389818d276cSGirish Shilamkar 				unsigned long ro, unsigned long incompat)
2390818d276cSGirish Shilamkar {
2391818d276cSGirish Shilamkar 	journal_superblock_t *sb;
2392818d276cSGirish Shilamkar 
2393cb3b3bf2SJan Kara 	jbd2_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
2394818d276cSGirish Shilamkar 		  compat, ro, incompat);
2395818d276cSGirish Shilamkar 
2396818d276cSGirish Shilamkar 	sb = journal->j_superblock;
2397818d276cSGirish Shilamkar 
2398818d276cSGirish Shilamkar 	sb->s_feature_compat    &= ~cpu_to_be32(compat);
2399818d276cSGirish Shilamkar 	sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
2400818d276cSGirish Shilamkar 	sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
2401fdc3ef88SJan Kara 	journal->j_revoke_records_per_block =
2402fdc3ef88SJan Kara 				journal_revoke_records_per_block(journal);
2403818d276cSGirish Shilamkar }
2404818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features);
2405470decc6SDave Kleikamp 
2406470decc6SDave Kleikamp /**
24072bf31d94SMauro Carvalho Chehab  * jbd2_journal_flush() - Flush journal
2408470decc6SDave Kleikamp  * @journal: Journal to act on.
240901d5d965SLeah Rumancik  * @flags: optional operation on the journal blocks after the flush (see below)
2410470decc6SDave Kleikamp  *
2411470decc6SDave Kleikamp  * Flush all data for a given journal to disk and empty the journal.
2412470decc6SDave Kleikamp  * Filesystems can use this when remounting readonly to ensure that
241301d5d965SLeah Rumancik  * recovery does not need to happen on remount. Optionally, a discard or zeroout
241401d5d965SLeah Rumancik  * can be issued on the journal blocks after flushing.
241501d5d965SLeah Rumancik  *
241601d5d965SLeah Rumancik  * flags:
241701d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_DISCARD: issues discards for the journal blocks
241801d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_ZEROOUT: issues zeroouts for the journal blocks
2419470decc6SDave Kleikamp  */
jbd2_journal_flush(journal_t * journal,unsigned int flags)242001d5d965SLeah Rumancik int jbd2_journal_flush(journal_t *journal, unsigned int flags)
2421470decc6SDave Kleikamp {
2422470decc6SDave Kleikamp 	int err = 0;
2423470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
2424470decc6SDave Kleikamp 
2425a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2426470decc6SDave Kleikamp 
2427470decc6SDave Kleikamp 	/* Force everything buffered to the log... */
2428470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
2429470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
2430f7f4bccbSMingming Cao 		__jbd2_log_start_commit(journal, transaction->t_tid);
2431470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
2432470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
2433470decc6SDave Kleikamp 
2434470decc6SDave Kleikamp 	/* Wait for the log commit to complete... */
2435470decc6SDave Kleikamp 	if (transaction) {
2436470decc6SDave Kleikamp 		tid_t tid = transaction->t_tid;
2437470decc6SDave Kleikamp 
2438a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
2439f7f4bccbSMingming Cao 		jbd2_log_wait_commit(journal, tid);
2440470decc6SDave Kleikamp 	} else {
2441a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
2442470decc6SDave Kleikamp 	}
2443470decc6SDave Kleikamp 
2444470decc6SDave Kleikamp 	/* ...and flush everything in the log out to disk. */
2445470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
2446470decc6SDave Kleikamp 	while (!err && journal->j_checkpoint_transactions != NULL) {
2447470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
24486fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
2449f7f4bccbSMingming Cao 		err = jbd2_log_do_checkpoint(journal);
245044519fafSHidehiro Kawai 		mutex_unlock(&journal->j_checkpoint_mutex);
2451470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
2452470decc6SDave Kleikamp 	}
2453470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
245444519fafSHidehiro Kawai 
245544519fafSHidehiro Kawai 	if (is_journal_aborted(journal))
245644519fafSHidehiro Kawai 		return -EIO;
245744519fafSHidehiro Kawai 
24586fa7aa50STejun Heo 	mutex_lock_io(&journal->j_checkpoint_mutex);
24596f6a6fdaSJoseph Qi 	if (!err) {
24606f6a6fdaSJoseph Qi 		err = jbd2_cleanup_journal_tail(journal);
24616f6a6fdaSJoseph Qi 		if (err < 0) {
24626f6a6fdaSJoseph Qi 			mutex_unlock(&journal->j_checkpoint_mutex);
24636f6a6fdaSJoseph Qi 			goto out;
24646f6a6fdaSJoseph Qi 		}
24656f6a6fdaSJoseph Qi 		err = 0;
24666f6a6fdaSJoseph Qi 	}
2467470decc6SDave Kleikamp 
2468470decc6SDave Kleikamp 	/* Finally, mark the journal as really needing no recovery.
2469470decc6SDave Kleikamp 	 * This sets s_start==0 in the underlying superblock, which is
2470470decc6SDave Kleikamp 	 * the magic code for a fully-recovered superblock.  Any future
2471470decc6SDave Kleikamp 	 * commits of data to the journal will restore the current
2472470decc6SDave Kleikamp 	 * s_start value. */
2473*5c480a69SZhang Yi 	jbd2_mark_journal_empty(journal, REQ_FUA);
247401d5d965SLeah Rumancik 
247501d5d965SLeah Rumancik 	if (flags)
247601d5d965SLeah Rumancik 		err = __jbd2_journal_erase(journal, flags);
247701d5d965SLeah Rumancik 
2478a78bb11dSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
2479a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2480470decc6SDave Kleikamp 	J_ASSERT(!journal->j_running_transaction);
2481470decc6SDave Kleikamp 	J_ASSERT(!journal->j_committing_transaction);
2482470decc6SDave Kleikamp 	J_ASSERT(!journal->j_checkpoint_transactions);
2483470decc6SDave Kleikamp 	J_ASSERT(journal->j_head == journal->j_tail);
2484470decc6SDave Kleikamp 	J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
2485a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
24866f6a6fdaSJoseph Qi out:
24876f6a6fdaSJoseph Qi 	return err;
2488470decc6SDave Kleikamp }
2489470decc6SDave Kleikamp 
2490470decc6SDave Kleikamp /**
24912bf31d94SMauro Carvalho Chehab  * jbd2_journal_wipe() - Wipe journal contents
2492470decc6SDave Kleikamp  * @journal: Journal to act on.
2493470decc6SDave Kleikamp  * @write: flag (see below)
2494470decc6SDave Kleikamp  *
2495470decc6SDave Kleikamp  * Wipe out all of the contents of a journal, safely.  This will produce
2496470decc6SDave Kleikamp  * a warning if the journal contains any valid recovery information.
2497f7f4bccbSMingming Cao  * Must be called between journal_init_*() and jbd2_journal_load().
2498470decc6SDave Kleikamp  *
2499470decc6SDave Kleikamp  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
2500470decc6SDave Kleikamp  * we merely suppress recovery.
2501470decc6SDave Kleikamp  */
2502470decc6SDave Kleikamp 
jbd2_journal_wipe(journal_t * journal,int write)2503f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write)
2504470decc6SDave Kleikamp {
2505d9a45496SZhang Yi 	int err;
2506470decc6SDave Kleikamp 
2507f7f4bccbSMingming Cao 	J_ASSERT (!(journal->j_flags & JBD2_LOADED));
2508470decc6SDave Kleikamp 
2509470decc6SDave Kleikamp 	if (!journal->j_tail)
2510d9a45496SZhang Yi 		return 0;
2511470decc6SDave Kleikamp 
2512f2a44523SEryu Guan 	printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
2513470decc6SDave Kleikamp 		write ? "Clearing" : "Ignoring");
2514470decc6SDave Kleikamp 
2515f7f4bccbSMingming Cao 	err = jbd2_journal_skip_recovery(journal);
2516a78bb11dSJan Kara 	if (write) {
2517a78bb11dSJan Kara 		/* Lock to make assertions happy... */
251853cf9784SXiaoguang Wang 		mutex_lock_io(&journal->j_checkpoint_mutex);
2519*5c480a69SZhang Yi 		jbd2_mark_journal_empty(journal, REQ_FUA);
2520a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
2521a78bb11dSJan Kara 	}
2522470decc6SDave Kleikamp 
2523470decc6SDave Kleikamp 	return err;
2524470decc6SDave Kleikamp }
2525470decc6SDave Kleikamp 
2526470decc6SDave Kleikamp /**
25272bf31d94SMauro Carvalho Chehab  * jbd2_journal_abort () - Shutdown the journal immediately.
2528470decc6SDave Kleikamp  * @journal: the journal to shutdown.
2529470decc6SDave Kleikamp  * @errno:   an error number to record in the journal indicating
2530470decc6SDave Kleikamp  *           the reason for the shutdown.
2531470decc6SDave Kleikamp  *
2532470decc6SDave Kleikamp  * Perform a complete, immediate shutdown of the ENTIRE
2533470decc6SDave Kleikamp  * journal (not of a single transaction).  This operation cannot be
2534470decc6SDave Kleikamp  * undone without closing and reopening the journal.
2535470decc6SDave Kleikamp  *
2536f7f4bccbSMingming Cao  * The jbd2_journal_abort function is intended to support higher level error
2537470decc6SDave Kleikamp  * recovery mechanisms such as the ext2/ext3 remount-readonly error
2538470decc6SDave Kleikamp  * mode.
2539470decc6SDave Kleikamp  *
2540470decc6SDave Kleikamp  * Journal abort has very specific semantics.  Any existing dirty,
2541470decc6SDave Kleikamp  * unjournaled buffers in the main filesystem will still be written to
2542470decc6SDave Kleikamp  * disk by bdflush, but the journaling mechanism will be suspended
2543470decc6SDave Kleikamp  * immediately and no further transaction commits will be honoured.
2544470decc6SDave Kleikamp  *
2545470decc6SDave Kleikamp  * Any dirty, journaled buffers will be written back to disk without
2546470decc6SDave Kleikamp  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
2547470decc6SDave Kleikamp  * filesystem, but we _do_ attempt to leave as much data as possible
2548470decc6SDave Kleikamp  * behind for fsck to use for cleanup.
2549470decc6SDave Kleikamp  *
2550470decc6SDave Kleikamp  * Any attempt to get a new transaction handle on a journal which is in
2551470decc6SDave Kleikamp  * ABORT state will just result in an -EROFS error return.  A
2552f7f4bccbSMingming Cao  * jbd2_journal_stop on an existing handle will return -EIO if we have
2553470decc6SDave Kleikamp  * entered abort state during the update.
2554470decc6SDave Kleikamp  *
2555470decc6SDave Kleikamp  * Recursive transactions are not disturbed by journal abort until the
2556f7f4bccbSMingming Cao  * final jbd2_journal_stop, which will receive the -EIO error.
2557470decc6SDave Kleikamp  *
2558f7f4bccbSMingming Cao  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2559470decc6SDave Kleikamp  * which will be recorded (if possible) in the journal superblock.  This
2560470decc6SDave Kleikamp  * allows a client to record failure conditions in the middle of a
2561470decc6SDave Kleikamp  * transaction without having to complete the transaction to record the
2562470decc6SDave Kleikamp  * failure to disk.  ext3_error, for example, now uses this
2563470decc6SDave Kleikamp  * functionality.
2564470decc6SDave Kleikamp  *
2565470decc6SDave Kleikamp  */
2566470decc6SDave Kleikamp 
jbd2_journal_abort(journal_t * journal,int errno)2567f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno)
2568470decc6SDave Kleikamp {
25697f6225e4Szhangyi (F) 	transaction_t *transaction;
25707f6225e4Szhangyi (F) 
25717f6225e4Szhangyi (F) 	/*
25727b97d868Szhangyi (F) 	 * Lock the aborting procedure until everything is done, this avoid
25737b97d868Szhangyi (F) 	 * races between filesystem's error handling flow (e.g. ext4_abort()),
25747b97d868Szhangyi (F) 	 * ensure panic after the error info is written into journal's
25757b97d868Szhangyi (F) 	 * superblock.
25767b97d868Szhangyi (F) 	 */
25777b97d868Szhangyi (F) 	mutex_lock(&journal->j_abort_mutex);
25787b97d868Szhangyi (F) 	/*
25797f6225e4Szhangyi (F) 	 * ESHUTDOWN always takes precedence because a file system check
25807f6225e4Szhangyi (F) 	 * caused by any other journal abort error is not required after
25817f6225e4Szhangyi (F) 	 * a shutdown triggered.
25827f6225e4Szhangyi (F) 	 */
25837f6225e4Szhangyi (F) 	write_lock(&journal->j_state_lock);
25847f6225e4Szhangyi (F) 	if (journal->j_flags & JBD2_ABORT) {
25857f6225e4Szhangyi (F) 		int old_errno = journal->j_errno;
25867f6225e4Szhangyi (F) 
25877f6225e4Szhangyi (F) 		write_unlock(&journal->j_state_lock);
25887f6225e4Szhangyi (F) 		if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) {
25897f6225e4Szhangyi (F) 			journal->j_errno = errno;
25907f6225e4Szhangyi (F) 			jbd2_journal_update_sb_errno(journal);
25917f6225e4Szhangyi (F) 		}
25927b97d868Szhangyi (F) 		mutex_unlock(&journal->j_abort_mutex);
25937f6225e4Szhangyi (F) 		return;
25947f6225e4Szhangyi (F) 	}
25957f6225e4Szhangyi (F) 
25967f6225e4Szhangyi (F) 	/*
25977f6225e4Szhangyi (F) 	 * Mark the abort as occurred and start current running transaction
25987f6225e4Szhangyi (F) 	 * to release all journaled buffer.
25997f6225e4Szhangyi (F) 	 */
26007f6225e4Szhangyi (F) 	pr_err("Aborting journal on device %s.\n", journal->j_devname);
26017f6225e4Szhangyi (F) 
26027f6225e4Szhangyi (F) 	journal->j_flags |= JBD2_ABORT;
26037f6225e4Szhangyi (F) 	journal->j_errno = errno;
26047f6225e4Szhangyi (F) 	transaction = journal->j_running_transaction;
26057f6225e4Szhangyi (F) 	if (transaction)
26067f6225e4Szhangyi (F) 		__jbd2_log_start_commit(journal, transaction->t_tid);
26077f6225e4Szhangyi (F) 	write_unlock(&journal->j_state_lock);
26087f6225e4Szhangyi (F) 
26097f6225e4Szhangyi (F) 	/*
26107f6225e4Szhangyi (F) 	 * Record errno to the journal super block, so that fsck and jbd2
26117f6225e4Szhangyi (F) 	 * layer could realise that a filesystem check is needed.
26127f6225e4Szhangyi (F) 	 */
26137f6225e4Szhangyi (F) 	jbd2_journal_update_sb_errno(journal);
26147b97d868Szhangyi (F) 	mutex_unlock(&journal->j_abort_mutex);
2615470decc6SDave Kleikamp }
2616470decc6SDave Kleikamp 
2617470decc6SDave Kleikamp /**
26182bf31d94SMauro Carvalho Chehab  * jbd2_journal_errno() - returns the journal's error state.
2619470decc6SDave Kleikamp  * @journal: journal to examine.
2620470decc6SDave Kleikamp  *
2621bfcd3555SAlberto Bertogli  * This is the errno number set with jbd2_journal_abort(), the last
2622470decc6SDave Kleikamp  * time the journal was mounted - if the journal was stopped
2623470decc6SDave Kleikamp  * without calling abort this will be 0.
2624470decc6SDave Kleikamp  *
2625470decc6SDave Kleikamp  * If the journal has been aborted on this mount time -EROFS will
2626470decc6SDave Kleikamp  * be returned.
2627470decc6SDave Kleikamp  */
jbd2_journal_errno(journal_t * journal)2628f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal)
2629470decc6SDave Kleikamp {
2630470decc6SDave Kleikamp 	int err;
2631470decc6SDave Kleikamp 
2632a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
2633f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2634470decc6SDave Kleikamp 		err = -EROFS;
2635470decc6SDave Kleikamp 	else
2636470decc6SDave Kleikamp 		err = journal->j_errno;
2637a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
2638470decc6SDave Kleikamp 	return err;
2639470decc6SDave Kleikamp }
2640470decc6SDave Kleikamp 
2641470decc6SDave Kleikamp /**
26422bf31d94SMauro Carvalho Chehab  * jbd2_journal_clear_err() - clears the journal's error state
2643470decc6SDave Kleikamp  * @journal: journal to act on.
2644470decc6SDave Kleikamp  *
2645bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2646470decc6SDave Kleikamp  * mode.
2647470decc6SDave Kleikamp  */
jbd2_journal_clear_err(journal_t * journal)2648f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal)
2649470decc6SDave Kleikamp {
2650470decc6SDave Kleikamp 	int err = 0;
2651470decc6SDave Kleikamp 
2652a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2653f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2654470decc6SDave Kleikamp 		err = -EROFS;
2655470decc6SDave Kleikamp 	else
2656470decc6SDave Kleikamp 		journal->j_errno = 0;
2657a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2658470decc6SDave Kleikamp 	return err;
2659470decc6SDave Kleikamp }
2660470decc6SDave Kleikamp 
2661470decc6SDave Kleikamp /**
26622bf31d94SMauro Carvalho Chehab  * jbd2_journal_ack_err() - Ack journal err.
2663470decc6SDave Kleikamp  * @journal: journal to act on.
2664470decc6SDave Kleikamp  *
2665bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2666470decc6SDave Kleikamp  * mode.
2667470decc6SDave Kleikamp  */
jbd2_journal_ack_err(journal_t * journal)2668f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal)
2669470decc6SDave Kleikamp {
2670a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2671470decc6SDave Kleikamp 	if (journal->j_errno)
2672f7f4bccbSMingming Cao 		journal->j_flags |= JBD2_ACK_ERR;
2673a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2674470decc6SDave Kleikamp }
2675470decc6SDave Kleikamp 
jbd2_journal_blocks_per_page(struct inode * inode)2676f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode)
2677470decc6SDave Kleikamp {
267809cbfeafSKirill A. Shutemov 	return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
2679470decc6SDave Kleikamp }
2680470decc6SDave Kleikamp 
2681470decc6SDave Kleikamp /*
2682b517bea1SZach Brown  * helper functions to deal with 32 or 64bit block numbers.
2683b517bea1SZach Brown  */
journal_tag_bytes(journal_t * journal)2684b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal)
2685b517bea1SZach Brown {
2686db9ee220SDarrick J. Wong 	size_t sz;
2687db9ee220SDarrick J. Wong 
268856316a0dSDarrick J. Wong 	if (jbd2_has_feature_csum3(journal))
2689db9ee220SDarrick J. Wong 		return sizeof(journal_block_tag3_t);
2690db9ee220SDarrick J. Wong 
2691db9ee220SDarrick J. Wong 	sz = sizeof(journal_block_tag_t);
2692c3900875SDarrick J. Wong 
269356316a0dSDarrick J. Wong 	if (jbd2_has_feature_csum2(journal))
2694db9ee220SDarrick J. Wong 		sz += sizeof(__u16);
2695c3900875SDarrick J. Wong 
269656316a0dSDarrick J. Wong 	if (jbd2_has_feature_64bit(journal))
2697db9ee220SDarrick J. Wong 		return sz;
2698b517bea1SZach Brown 	else
2699db9ee220SDarrick J. Wong 		return sz - sizeof(__u32);
2700b517bea1SZach Brown }
2701b517bea1SZach Brown 
2702b517bea1SZach Brown /*
2703d2eecb03STheodore Ts'o  * JBD memory management
2704d2eecb03STheodore Ts'o  *
2705d2eecb03STheodore Ts'o  * These functions are used to allocate block-sized chunks of memory
2706d2eecb03STheodore Ts'o  * used for making copies of buffer_head data.  Very often it will be
2707d2eecb03STheodore Ts'o  * page-sized chunks of data, but sometimes it will be in
2708d2eecb03STheodore Ts'o  * sub-page-size chunks.  (For example, 16k pages on Power systems
2709d2eecb03STheodore Ts'o  * with a 4k block file system.)  For blocks smaller than a page, we
2710d2eecb03STheodore Ts'o  * use a SLAB allocator.  There are slab caches for each block size,
2711d2eecb03STheodore Ts'o  * which are allocated at mount time, if necessary, and we only free
2712d2eecb03STheodore Ts'o  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
2713d2eecb03STheodore Ts'o  * this reason we don't need to a mutex to protect access to
2714d2eecb03STheodore Ts'o  * jbd2_slab[] allocating or releasing memory; only in
2715d2eecb03STheodore Ts'o  * jbd2_journal_create_slab().
2716d2eecb03STheodore Ts'o  */
2717d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8
2718d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2719d2eecb03STheodore Ts'o 
2720d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2721d2eecb03STheodore Ts'o 	"jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2722d2eecb03STheodore Ts'o 	"jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2723d2eecb03STheodore Ts'o };
2724d2eecb03STheodore Ts'o 
2725d2eecb03STheodore Ts'o 
jbd2_journal_destroy_slabs(void)2726d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void)
2727d2eecb03STheodore Ts'o {
2728d2eecb03STheodore Ts'o 	int i;
2729d2eecb03STheodore Ts'o 
2730d2eecb03STheodore Ts'o 	for (i = 0; i < JBD2_MAX_SLABS; i++) {
2731d2eecb03STheodore Ts'o 		kmem_cache_destroy(jbd2_slab[i]);
2732d2eecb03STheodore Ts'o 		jbd2_slab[i] = NULL;
2733d2eecb03STheodore Ts'o 	}
2734d2eecb03STheodore Ts'o }
2735d2eecb03STheodore Ts'o 
jbd2_journal_create_slab(size_t size)2736d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size)
2737d2eecb03STheodore Ts'o {
273851dfacdeSThomas Gleixner 	static DEFINE_MUTEX(jbd2_slab_create_mutex);
2739d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2740d2eecb03STheodore Ts'o 	size_t slab_size;
2741d2eecb03STheodore Ts'o 
2742d2eecb03STheodore Ts'o 	if (size == PAGE_SIZE)
2743d2eecb03STheodore Ts'o 		return 0;
2744d2eecb03STheodore Ts'o 
2745d2eecb03STheodore Ts'o 	if (i >= JBD2_MAX_SLABS)
2746d2eecb03STheodore Ts'o 		return -EINVAL;
2747d2eecb03STheodore Ts'o 
2748d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2749d2eecb03STheodore Ts'o 		i = 0;
275051dfacdeSThomas Gleixner 	mutex_lock(&jbd2_slab_create_mutex);
2751d2eecb03STheodore Ts'o 	if (jbd2_slab[i]) {
275251dfacdeSThomas Gleixner 		mutex_unlock(&jbd2_slab_create_mutex);
2753d2eecb03STheodore Ts'o 		return 0;	/* Already created */
2754d2eecb03STheodore Ts'o 	}
2755d2eecb03STheodore Ts'o 
2756d2eecb03STheodore Ts'o 	slab_size = 1 << (i+10);
2757d2eecb03STheodore Ts'o 	jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2758d2eecb03STheodore Ts'o 					 slab_size, 0, NULL);
275951dfacdeSThomas Gleixner 	mutex_unlock(&jbd2_slab_create_mutex);
2760d2eecb03STheodore Ts'o 	if (!jbd2_slab[i]) {
2761d2eecb03STheodore Ts'o 		printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2762d2eecb03STheodore Ts'o 		return -ENOMEM;
2763d2eecb03STheodore Ts'o 	}
2764d2eecb03STheodore Ts'o 	return 0;
2765d2eecb03STheodore Ts'o }
2766d2eecb03STheodore Ts'o 
get_slab(size_t size)2767d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size)
2768d2eecb03STheodore Ts'o {
2769d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2770d2eecb03STheodore Ts'o 
2771d2eecb03STheodore Ts'o 	BUG_ON(i >= JBD2_MAX_SLABS);
2772d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2773d2eecb03STheodore Ts'o 		i = 0;
27748ac97b74SBill Pemberton 	BUG_ON(jbd2_slab[i] == NULL);
2775d2eecb03STheodore Ts'o 	return jbd2_slab[i];
2776d2eecb03STheodore Ts'o }
2777d2eecb03STheodore Ts'o 
jbd2_alloc(size_t size,gfp_t flags)2778d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags)
2779d2eecb03STheodore Ts'o {
2780d2eecb03STheodore Ts'o 	void *ptr;
2781d2eecb03STheodore Ts'o 
2782d2eecb03STheodore Ts'o 	BUG_ON(size & (size-1)); /* Must be a power of 2 */
2783d2eecb03STheodore Ts'o 
2784f2db1971SMichal Hocko 	if (size < PAGE_SIZE)
2785d2eecb03STheodore Ts'o 		ptr = kmem_cache_alloc(get_slab(size), flags);
2786f2db1971SMichal Hocko 	else
2787f2db1971SMichal Hocko 		ptr = (void *)__get_free_pages(flags, get_order(size));
2788d2eecb03STheodore Ts'o 
2789d2eecb03STheodore Ts'o 	/* Check alignment; SLUB has gotten this wrong in the past,
2790d2eecb03STheodore Ts'o 	 * and this can lead to user data corruption! */
2791d2eecb03STheodore Ts'o 	BUG_ON(((unsigned long) ptr) & (size-1));
2792d2eecb03STheodore Ts'o 
2793d2eecb03STheodore Ts'o 	return ptr;
2794d2eecb03STheodore Ts'o }
2795d2eecb03STheodore Ts'o 
jbd2_free(void * ptr,size_t size)2796d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size)
2797d2eecb03STheodore Ts'o {
2798f2db1971SMichal Hocko 	if (size < PAGE_SIZE)
2799d2eecb03STheodore Ts'o 		kmem_cache_free(get_slab(size), ptr);
2800f2db1971SMichal Hocko 	else
2801f2db1971SMichal Hocko 		free_pages((unsigned long)ptr, get_order(size));
2802d2eecb03STheodore Ts'o };
2803d2eecb03STheodore Ts'o 
2804d2eecb03STheodore Ts'o /*
2805470decc6SDave Kleikamp  * Journal_head storage management
2806470decc6SDave Kleikamp  */
2807e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache;
2808e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2809470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2810470decc6SDave Kleikamp #endif
2811470decc6SDave Kleikamp 
jbd2_journal_init_journal_head_cache(void)28120d52154bSChengguang Xu static int __init jbd2_journal_init_journal_head_cache(void)
2813470decc6SDave Kleikamp {
28140d52154bSChengguang Xu 	J_ASSERT(!jbd2_journal_head_cache);
2815a920e941SJohann Lombardi 	jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2816470decc6SDave Kleikamp 				sizeof(struct journal_head),
2817470decc6SDave Kleikamp 				0,		/* offset */
28185f0d5a3aSPaul E. McKenney 				SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU,
281920c2df83SPaul Mundt 				NULL);		/* ctor */
28201076d17aSAl Viro 	if (!jbd2_journal_head_cache) {
2821f2a44523SEryu Guan 		printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
28220d52154bSChengguang Xu 		return -ENOMEM;
2823470decc6SDave Kleikamp 	}
28240d52154bSChengguang Xu 	return 0;
2825470decc6SDave Kleikamp }
2826470decc6SDave Kleikamp 
jbd2_journal_destroy_journal_head_cache(void)28274185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void)
2828470decc6SDave Kleikamp {
2829f7f4bccbSMingming Cao 	kmem_cache_destroy(jbd2_journal_head_cache);
2830f7f4bccbSMingming Cao 	jbd2_journal_head_cache = NULL;
2831470decc6SDave Kleikamp }
2832470decc6SDave Kleikamp 
2833470decc6SDave Kleikamp /*
2834470decc6SDave Kleikamp  * journal_head splicing and dicing
2835470decc6SDave Kleikamp  */
journal_alloc_journal_head(void)2836470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void)
2837470decc6SDave Kleikamp {
2838470decc6SDave Kleikamp 	struct journal_head *ret;
2839470decc6SDave Kleikamp 
2840e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2841470decc6SDave Kleikamp 	atomic_inc(&nr_journal_heads);
2842470decc6SDave Kleikamp #endif
28435d9cf9c6SZheng Liu 	ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
28441076d17aSAl Viro 	if (!ret) {
2845cb3b3bf2SJan Kara 		jbd2_debug(1, "out of memory for journal_head\n");
2846670be5a7STheodore Ts'o 		pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
28477b506b10SMichal Hocko 		ret = kmem_cache_zalloc(jbd2_journal_head_cache,
28487b506b10SMichal Hocko 				GFP_NOFS | __GFP_NOFAIL);
2849470decc6SDave Kleikamp 	}
285046417064SThomas Gleixner 	if (ret)
285146417064SThomas Gleixner 		spin_lock_init(&ret->b_state_lock);
2852470decc6SDave Kleikamp 	return ret;
2853470decc6SDave Kleikamp }
2854470decc6SDave Kleikamp 
journal_free_journal_head(struct journal_head * jh)2855470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh)
2856470decc6SDave Kleikamp {
2857e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2858470decc6SDave Kleikamp 	atomic_dec(&nr_journal_heads);
2859cd02ff0bSMingming Cao 	memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2860470decc6SDave Kleikamp #endif
2861f7f4bccbSMingming Cao 	kmem_cache_free(jbd2_journal_head_cache, jh);
2862470decc6SDave Kleikamp }
2863470decc6SDave Kleikamp 
2864470decc6SDave Kleikamp /*
2865470decc6SDave Kleikamp  * A journal_head is attached to a buffer_head whenever JBD has an
2866470decc6SDave Kleikamp  * interest in the buffer.
2867470decc6SDave Kleikamp  *
2868470decc6SDave Kleikamp  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2869470decc6SDave Kleikamp  * is set.  This bit is tested in core kernel code where we need to take
2870470decc6SDave Kleikamp  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
2871470decc6SDave Kleikamp  * there.
2872470decc6SDave Kleikamp  *
2873470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2874470decc6SDave Kleikamp  *
2875470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set it is immune from being released by
2876470decc6SDave Kleikamp  * core kernel code, mainly via ->b_count.
2877470decc6SDave Kleikamp  *
2878de1b7941SJan Kara  * A journal_head is detached from its buffer_head when the journal_head's
2879de1b7941SJan Kara  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2880de1b7941SJan Kara  * transaction (b_cp_transaction) hold their references to b_jcount.
2881470decc6SDave Kleikamp  *
2882470decc6SDave Kleikamp  * Various places in the kernel want to attach a journal_head to a buffer_head
2883470decc6SDave Kleikamp  * _before_ attaching the journal_head to a transaction.  To protect the
2884f7f4bccbSMingming Cao  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2885470decc6SDave Kleikamp  * journal_head's b_jcount refcount by one.  The caller must call
2886f7f4bccbSMingming Cao  * jbd2_journal_put_journal_head() to undo this.
2887470decc6SDave Kleikamp  *
2888470decc6SDave Kleikamp  * So the typical usage would be:
2889470decc6SDave Kleikamp  *
2890470decc6SDave Kleikamp  *	(Attach a journal_head if needed.  Increments b_jcount)
2891f7f4bccbSMingming Cao  *	struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2892470decc6SDave Kleikamp  *	...
2893de1b7941SJan Kara  *      (Get another reference for transaction)
2894de1b7941SJan Kara  *	jbd2_journal_grab_journal_head(bh);
2895470decc6SDave Kleikamp  *	jh->b_transaction = xxx;
2896de1b7941SJan Kara  *	(Put original reference)
2897f7f4bccbSMingming Cao  *	jbd2_journal_put_journal_head(jh);
2898470decc6SDave Kleikamp  */
2899470decc6SDave Kleikamp 
2900470decc6SDave Kleikamp /*
2901470decc6SDave Kleikamp  * Give a buffer_head a journal_head.
2902470decc6SDave Kleikamp  *
2903470decc6SDave Kleikamp  * May sleep.
2904470decc6SDave Kleikamp  */
jbd2_journal_add_journal_head(struct buffer_head * bh)2905f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2906470decc6SDave Kleikamp {
2907470decc6SDave Kleikamp 	struct journal_head *jh;
2908470decc6SDave Kleikamp 	struct journal_head *new_jh = NULL;
2909470decc6SDave Kleikamp 
2910470decc6SDave Kleikamp repeat:
29115d9cf9c6SZheng Liu 	if (!buffer_jbd(bh))
2912470decc6SDave Kleikamp 		new_jh = journal_alloc_journal_head();
2913470decc6SDave Kleikamp 
2914470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2915470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2916470decc6SDave Kleikamp 		jh = bh2jh(bh);
2917470decc6SDave Kleikamp 	} else {
2918470decc6SDave Kleikamp 		J_ASSERT_BH(bh,
2919470decc6SDave Kleikamp 			(atomic_read(&bh->b_count) > 0) ||
29200d22fe2fSMatthew Wilcox (Oracle) 			(bh->b_folio && bh->b_folio->mapping));
2921470decc6SDave Kleikamp 
2922470decc6SDave Kleikamp 		if (!new_jh) {
2923470decc6SDave Kleikamp 			jbd_unlock_bh_journal_head(bh);
2924470decc6SDave Kleikamp 			goto repeat;
2925470decc6SDave Kleikamp 		}
2926470decc6SDave Kleikamp 
2927470decc6SDave Kleikamp 		jh = new_jh;
2928470decc6SDave Kleikamp 		new_jh = NULL;		/* We consumed it */
2929470decc6SDave Kleikamp 		set_buffer_jbd(bh);
2930470decc6SDave Kleikamp 		bh->b_private = jh;
2931470decc6SDave Kleikamp 		jh->b_bh = bh;
2932470decc6SDave Kleikamp 		get_bh(bh);
2933470decc6SDave Kleikamp 		BUFFER_TRACE(bh, "added journal_head");
2934470decc6SDave Kleikamp 	}
2935470decc6SDave Kleikamp 	jh->b_jcount++;
2936470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2937470decc6SDave Kleikamp 	if (new_jh)
2938470decc6SDave Kleikamp 		journal_free_journal_head(new_jh);
2939470decc6SDave Kleikamp 	return bh->b_private;
2940470decc6SDave Kleikamp }
2941470decc6SDave Kleikamp 
2942470decc6SDave Kleikamp /*
2943470decc6SDave Kleikamp  * Grab a ref against this buffer_head's journal_head.  If it ended up not
2944470decc6SDave Kleikamp  * having a journal_head, return NULL
2945470decc6SDave Kleikamp  */
jbd2_journal_grab_journal_head(struct buffer_head * bh)2946f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2947470decc6SDave Kleikamp {
2948470decc6SDave Kleikamp 	struct journal_head *jh = NULL;
2949470decc6SDave Kleikamp 
2950470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2951470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2952470decc6SDave Kleikamp 		jh = bh2jh(bh);
2953470decc6SDave Kleikamp 		jh->b_jcount++;
2954470decc6SDave Kleikamp 	}
2955470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2956470decc6SDave Kleikamp 	return jh;
2957470decc6SDave Kleikamp }
29584cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_grab_journal_head);
2959470decc6SDave Kleikamp 
__journal_remove_journal_head(struct buffer_head * bh)2960470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh)
2961470decc6SDave Kleikamp {
2962470decc6SDave Kleikamp 	struct journal_head *jh = bh2jh(bh);
2963470decc6SDave Kleikamp 
2964de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_transaction == NULL);
2965de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2966de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2967470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2968470decc6SDave Kleikamp 	J_ASSERT_BH(bh, buffer_jbd(bh));
2969470decc6SDave Kleikamp 	J_ASSERT_BH(bh, jh2bh(jh) == bh);
2970470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "remove journal_head");
29717855a57dSThomas Gleixner 
29727855a57dSThomas Gleixner 	/* Unlink before dropping the lock */
2973470decc6SDave Kleikamp 	bh->b_private = NULL;
2974470decc6SDave Kleikamp 	jh->b_bh = NULL;	/* debug, really */
2975470decc6SDave Kleikamp 	clear_buffer_jbd(bh);
29767855a57dSThomas Gleixner }
29777855a57dSThomas Gleixner 
journal_release_journal_head(struct journal_head * jh,size_t b_size)29787855a57dSThomas Gleixner static void journal_release_journal_head(struct journal_head *jh, size_t b_size)
29797855a57dSThomas Gleixner {
29807855a57dSThomas Gleixner 	if (jh->b_frozen_data) {
29817855a57dSThomas Gleixner 		printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
29827855a57dSThomas Gleixner 		jbd2_free(jh->b_frozen_data, b_size);
29837855a57dSThomas Gleixner 	}
29847855a57dSThomas Gleixner 	if (jh->b_committed_data) {
29857855a57dSThomas Gleixner 		printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
29867855a57dSThomas Gleixner 		jbd2_free(jh->b_committed_data, b_size);
29877855a57dSThomas Gleixner 	}
2988470decc6SDave Kleikamp 	journal_free_journal_head(jh);
2989470decc6SDave Kleikamp }
2990470decc6SDave Kleikamp 
2991470decc6SDave Kleikamp /*
2992de1b7941SJan Kara  * Drop a reference on the passed journal_head.  If it fell to zero then
2993470decc6SDave Kleikamp  * release the journal_head from the buffer_head.
2994470decc6SDave Kleikamp  */
jbd2_journal_put_journal_head(struct journal_head * jh)2995f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh)
2996470decc6SDave Kleikamp {
2997470decc6SDave Kleikamp 	struct buffer_head *bh = jh2bh(jh);
2998470decc6SDave Kleikamp 
2999470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
3000470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jcount > 0);
3001470decc6SDave Kleikamp 	--jh->b_jcount;
3002de1b7941SJan Kara 	if (!jh->b_jcount) {
3003470decc6SDave Kleikamp 		__journal_remove_journal_head(bh);
3004de1b7941SJan Kara 		jbd_unlock_bh_journal_head(bh);
30057855a57dSThomas Gleixner 		journal_release_journal_head(jh, bh->b_size);
3006470decc6SDave Kleikamp 		__brelse(bh);
30077855a57dSThomas Gleixner 	} else {
3008470decc6SDave Kleikamp 		jbd_unlock_bh_journal_head(bh);
3009470decc6SDave Kleikamp 	}
30107855a57dSThomas Gleixner }
30114cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_put_journal_head);
3012470decc6SDave Kleikamp 
3013470decc6SDave Kleikamp /*
3014c851ed54SJan Kara  * Initialize jbd inode head
3015c851ed54SJan Kara  */
jbd2_journal_init_jbd_inode(struct jbd2_inode * jinode,struct inode * inode)3016c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
3017c851ed54SJan Kara {
3018c851ed54SJan Kara 	jinode->i_transaction = NULL;
3019c851ed54SJan Kara 	jinode->i_next_transaction = NULL;
3020c851ed54SJan Kara 	jinode->i_vfs_inode = inode;
3021c851ed54SJan Kara 	jinode->i_flags = 0;
30226ba0e7dcSRoss Zwisler 	jinode->i_dirty_start = 0;
30236ba0e7dcSRoss Zwisler 	jinode->i_dirty_end = 0;
3024c851ed54SJan Kara 	INIT_LIST_HEAD(&jinode->i_list);
3025c851ed54SJan Kara }
3026c851ed54SJan Kara 
3027c851ed54SJan Kara /*
3028c851ed54SJan Kara  * Function to be called before we start removing inode from memory (i.e.,
3029c851ed54SJan Kara  * clear_inode() is a fine place to be called from). It removes inode from
3030c851ed54SJan Kara  * transaction's lists.
3031c851ed54SJan Kara  */
jbd2_journal_release_jbd_inode(journal_t * journal,struct jbd2_inode * jinode)3032c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal,
3033c851ed54SJan Kara 				    struct jbd2_inode *jinode)
3034c851ed54SJan Kara {
3035c851ed54SJan Kara 	if (!journal)
3036c851ed54SJan Kara 		return;
3037c851ed54SJan Kara restart:
3038c851ed54SJan Kara 	spin_lock(&journal->j_list_lock);
3039c851ed54SJan Kara 	/* Is commit writing out inode - we have to wait */
3040cb0d9d47SJan Kara 	if (jinode->i_flags & JI_COMMIT_RUNNING) {
3041c851ed54SJan Kara 		wait_queue_head_t *wq;
3042c851ed54SJan Kara 		DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
3043c851ed54SJan Kara 		wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
304421417136SIngo Molnar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
3045c851ed54SJan Kara 		spin_unlock(&journal->j_list_lock);
3046c851ed54SJan Kara 		schedule();
304721417136SIngo Molnar 		finish_wait(wq, &wait.wq_entry);
3048c851ed54SJan Kara 		goto restart;
3049c851ed54SJan Kara 	}
3050c851ed54SJan Kara 
3051c851ed54SJan Kara 	if (jinode->i_transaction) {
3052c851ed54SJan Kara 		list_del(&jinode->i_list);
3053c851ed54SJan Kara 		jinode->i_transaction = NULL;
3054c851ed54SJan Kara 	}
3055c851ed54SJan Kara 	spin_unlock(&journal->j_list_lock);
3056c851ed54SJan Kara }
3057c851ed54SJan Kara 
3058470decc6SDave Kleikamp 
30598e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS
30608e85fb3fSJohann Lombardi 
30618e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2"
30628e85fb3fSJohann Lombardi 
jbd2_create_jbd_stats_proc_entry(void)30638e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void)
30648e85fb3fSJohann Lombardi {
30658e85fb3fSJohann Lombardi 	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
30668e85fb3fSJohann Lombardi }
30678e85fb3fSJohann Lombardi 
jbd2_remove_jbd_stats_proc_entry(void)30688e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void)
30698e85fb3fSJohann Lombardi {
30708e85fb3fSJohann Lombardi 	if (proc_jbd2_stats)
30718e85fb3fSJohann Lombardi 		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
30728e85fb3fSJohann Lombardi }
30738e85fb3fSJohann Lombardi 
30748e85fb3fSJohann Lombardi #else
30758e85fb3fSJohann Lombardi 
30768e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
30778e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
30788e85fb3fSJohann Lombardi 
30798e85fb3fSJohann Lombardi #endif
30808e85fb3fSJohann Lombardi 
30818aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
3082470decc6SDave Kleikamp 
jbd2_journal_init_inode_cache(void)30830d52154bSChengguang Xu static int __init jbd2_journal_init_inode_cache(void)
3084470decc6SDave Kleikamp {
30850d52154bSChengguang Xu 	J_ASSERT(!jbd2_inode_cache);
30868aefcd55STheodore Ts'o 	jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
30870d52154bSChengguang Xu 	if (!jbd2_inode_cache) {
30880d52154bSChengguang Xu 		pr_emerg("JBD2: failed to create inode cache\n");
3089470decc6SDave Kleikamp 		return -ENOMEM;
3090470decc6SDave Kleikamp 	}
3091470decc6SDave Kleikamp 	return 0;
3092470decc6SDave Kleikamp }
3093470decc6SDave Kleikamp 
jbd2_journal_init_handle_cache(void)30940d52154bSChengguang Xu static int __init jbd2_journal_init_handle_cache(void)
30950d52154bSChengguang Xu {
30960d52154bSChengguang Xu 	J_ASSERT(!jbd2_handle_cache);
30970d52154bSChengguang Xu 	jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
30980d52154bSChengguang Xu 	if (!jbd2_handle_cache) {
30990d52154bSChengguang Xu 		printk(KERN_EMERG "JBD2: failed to create handle cache\n");
31000d52154bSChengguang Xu 		return -ENOMEM;
31010d52154bSChengguang Xu 	}
31020d52154bSChengguang Xu 	return 0;
31030d52154bSChengguang Xu }
31040d52154bSChengguang Xu 
jbd2_journal_destroy_inode_cache(void)31050d52154bSChengguang Xu static void jbd2_journal_destroy_inode_cache(void)
31060d52154bSChengguang Xu {
31070d52154bSChengguang Xu 	kmem_cache_destroy(jbd2_inode_cache);
31080d52154bSChengguang Xu 	jbd2_inode_cache = NULL;
31090d52154bSChengguang Xu }
31100d52154bSChengguang Xu 
jbd2_journal_destroy_handle_cache(void)3111f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void)
3112470decc6SDave Kleikamp {
3113f7f4bccbSMingming Cao 	kmem_cache_destroy(jbd2_handle_cache);
31148bdd5b60SWang Long 	jbd2_handle_cache = NULL;
3115470decc6SDave Kleikamp }
3116470decc6SDave Kleikamp 
3117470decc6SDave Kleikamp /*
3118470decc6SDave Kleikamp  * Module startup and shutdown
3119470decc6SDave Kleikamp  */
3120470decc6SDave Kleikamp 
journal_init_caches(void)3121470decc6SDave Kleikamp static int __init journal_init_caches(void)
3122470decc6SDave Kleikamp {
3123470decc6SDave Kleikamp 	int ret;
3124470decc6SDave Kleikamp 
31250d52154bSChengguang Xu 	ret = jbd2_journal_init_revoke_record_cache();
31260d52154bSChengguang Xu 	if (ret == 0)
31270d52154bSChengguang Xu 		ret = jbd2_journal_init_revoke_table_cache();
3128470decc6SDave Kleikamp 	if (ret == 0)
31294185a2acSYongqiang Yang 		ret = jbd2_journal_init_journal_head_cache();
3130470decc6SDave Kleikamp 	if (ret == 0)
31314185a2acSYongqiang Yang 		ret = jbd2_journal_init_handle_cache();
31320c2022ecSYongqiang Yang 	if (ret == 0)
31330d52154bSChengguang Xu 		ret = jbd2_journal_init_inode_cache();
31340d52154bSChengguang Xu 	if (ret == 0)
31350c2022ecSYongqiang Yang 		ret = jbd2_journal_init_transaction_cache();
3136470decc6SDave Kleikamp 	return ret;
3137470decc6SDave Kleikamp }
3138470decc6SDave Kleikamp 
jbd2_journal_destroy_caches(void)3139f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void)
3140470decc6SDave Kleikamp {
31410d52154bSChengguang Xu 	jbd2_journal_destroy_revoke_record_cache();
31420d52154bSChengguang Xu 	jbd2_journal_destroy_revoke_table_cache();
31434185a2acSYongqiang Yang 	jbd2_journal_destroy_journal_head_cache();
3144f7f4bccbSMingming Cao 	jbd2_journal_destroy_handle_cache();
31450d52154bSChengguang Xu 	jbd2_journal_destroy_inode_cache();
31460c2022ecSYongqiang Yang 	jbd2_journal_destroy_transaction_cache();
3147d2eecb03STheodore Ts'o 	jbd2_journal_destroy_slabs();
3148470decc6SDave Kleikamp }
3149470decc6SDave Kleikamp 
journal_init(void)3150470decc6SDave Kleikamp static int __init journal_init(void)
3151470decc6SDave Kleikamp {
3152470decc6SDave Kleikamp 	int ret;
3153470decc6SDave Kleikamp 
3154470decc6SDave Kleikamp 	BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
3155470decc6SDave Kleikamp 
3156470decc6SDave Kleikamp 	ret = journal_init_caches();
3157620de4e1SDuane Griffin 	if (ret == 0) {
31588e85fb3fSJohann Lombardi 		jbd2_create_jbd_stats_proc_entry();
3159620de4e1SDuane Griffin 	} else {
3160620de4e1SDuane Griffin 		jbd2_journal_destroy_caches();
3161620de4e1SDuane Griffin 	}
3162470decc6SDave Kleikamp 	return ret;
3163470decc6SDave Kleikamp }
3164470decc6SDave Kleikamp 
journal_exit(void)3165470decc6SDave Kleikamp static void __exit journal_exit(void)
3166470decc6SDave Kleikamp {
3167e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
3168470decc6SDave Kleikamp 	int n = atomic_read(&nr_journal_heads);
3169470decc6SDave Kleikamp 	if (n)
317075685071SJan Kara 		printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
3171470decc6SDave Kleikamp #endif
31728e85fb3fSJohann Lombardi 	jbd2_remove_jbd_stats_proc_entry();
3173f7f4bccbSMingming Cao 	jbd2_journal_destroy_caches();
3174470decc6SDave Kleikamp }
3175470decc6SDave Kleikamp 
3176470decc6SDave Kleikamp MODULE_LICENSE("GPL");
3177470decc6SDave Kleikamp module_init(journal_init);
3178470decc6SDave Kleikamp module_exit(journal_exit);
3179470decc6SDave Kleikamp 
3180