xref: /openbmc/linux/fs/jbd2/journal.c (revision fac59652993f075d57860769c99045b3ca18780d)
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);
4026dacca9fSKemeng Shi 			free_buffer_head(new_bh);
403e6ec116bSTheodore Ts'o 			return -ENOMEM;
404e6ec116bSTheodore Ts'o 		}
40546417064SThomas Gleixner 		spin_lock(&jh_in->b_state_lock);
406470decc6SDave Kleikamp 		if (jh_in->b_frozen_data) {
407af1e76d6SMingming Cao 			jbd2_free(tmp, bh_in->b_size);
408470decc6SDave Kleikamp 			goto repeat;
409470decc6SDave Kleikamp 		}
410470decc6SDave Kleikamp 
411470decc6SDave Kleikamp 		jh_in->b_frozen_data = tmp;
4128147c4c4SMatthew Wilcox (Oracle) 		memcpy_from_folio(tmp, new_folio, new_offset, bh_in->b_size);
413470decc6SDave Kleikamp 
4148147c4c4SMatthew Wilcox (Oracle) 		new_folio = virt_to_folio(tmp);
4158147c4c4SMatthew Wilcox (Oracle) 		new_offset = offset_in_folio(new_folio, tmp);
416470decc6SDave Kleikamp 		done_copy_out = 1;
417e06c8227SJoel Becker 
418e06c8227SJoel Becker 		/*
419e06c8227SJoel Becker 		 * This isn't strictly necessary, as we're using frozen
420e06c8227SJoel Becker 		 * data for the escaping, but it keeps consistency with
421e06c8227SJoel Becker 		 * b_frozen_data usage.
422e06c8227SJoel Becker 		 */
423e06c8227SJoel Becker 		jh_in->b_frozen_triggers = jh_in->b_triggers;
424470decc6SDave Kleikamp 	}
425470decc6SDave Kleikamp 
426470decc6SDave Kleikamp 	/*
427470decc6SDave Kleikamp 	 * Did we need to do an escaping?  Now we've done all the
428470decc6SDave Kleikamp 	 * copying, we can finally do so.
429470decc6SDave Kleikamp 	 */
430470decc6SDave Kleikamp 	if (do_escape) {
4318147c4c4SMatthew Wilcox (Oracle) 		mapped_data = kmap_local_folio(new_folio, new_offset);
4328147c4c4SMatthew Wilcox (Oracle) 		*((unsigned int *)mapped_data) = 0;
4338147c4c4SMatthew Wilcox (Oracle) 		kunmap_local(mapped_data);
434470decc6SDave Kleikamp 	}
435470decc6SDave Kleikamp 
4368147c4c4SMatthew Wilcox (Oracle) 	folio_set_bh(new_bh, new_folio, new_offset);
437f5113effSJan Kara 	new_bh->b_size = bh_in->b_size;
438f5113effSJan Kara 	new_bh->b_bdev = journal->j_dev;
439470decc6SDave Kleikamp 	new_bh->b_blocknr = blocknr;
440b34090e5SJan Kara 	new_bh->b_private = bh_in;
441470decc6SDave Kleikamp 	set_buffer_mapped(new_bh);
442470decc6SDave Kleikamp 	set_buffer_dirty(new_bh);
443470decc6SDave Kleikamp 
444f5113effSJan Kara 	*bh_out = new_bh;
445470decc6SDave Kleikamp 
446470decc6SDave Kleikamp 	/*
447470decc6SDave Kleikamp 	 * The to-be-written buffer needs to get moved to the io queue,
448470decc6SDave Kleikamp 	 * and the original buffer whose contents we are shadowing or
449470decc6SDave Kleikamp 	 * copying is moved to the transaction's shadow queue.
450470decc6SDave Kleikamp 	 */
451470decc6SDave Kleikamp 	JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
45296577c43Sdingdinghua 	spin_lock(&journal->j_list_lock);
45396577c43Sdingdinghua 	__jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
45496577c43Sdingdinghua 	spin_unlock(&journal->j_list_lock);
455b34090e5SJan Kara 	set_buffer_shadow(bh_in);
45646417064SThomas Gleixner 	spin_unlock(&jh_in->b_state_lock);
45796577c43Sdingdinghua 
458470decc6SDave Kleikamp 	return do_escape | (done_copy_out << 1);
459470decc6SDave Kleikamp }
460470decc6SDave Kleikamp 
461470decc6SDave Kleikamp /*
462470decc6SDave Kleikamp  * Allocation code for the journal file.  Manage the space left in the
463470decc6SDave Kleikamp  * journal, so that we can begin checkpointing when appropriate.
464470decc6SDave Kleikamp  */
465470decc6SDave Kleikamp 
466470decc6SDave Kleikamp /*
467e4471831STheodore Ts'o  * Called with j_state_lock locked for writing.
468e4471831STheodore Ts'o  * Returns true if a transaction commit was started.
469470decc6SDave Kleikamp  */
__jbd2_log_start_commit(journal_t * journal,tid_t target)470d1324958SJan Kara static int __jbd2_log_start_commit(journal_t *journal, tid_t target)
471470decc6SDave Kleikamp {
472e7b04ac0SEric Sandeen 	/* Return if the txn has already requested to be committed */
473e7b04ac0SEric Sandeen 	if (journal->j_commit_request == target)
474e7b04ac0SEric Sandeen 		return 0;
475e7b04ac0SEric Sandeen 
476470decc6SDave Kleikamp 	/*
477deeeaf13STheodore Ts'o 	 * The only transaction we can possibly wait upon is the
478deeeaf13STheodore Ts'o 	 * currently running transaction (if it exists).  Otherwise,
479deeeaf13STheodore Ts'o 	 * the target tid must be an old one.
480470decc6SDave Kleikamp 	 */
481deeeaf13STheodore Ts'o 	if (journal->j_running_transaction &&
482deeeaf13STheodore Ts'o 	    journal->j_running_transaction->t_tid == target) {
483470decc6SDave Kleikamp 		/*
484bcf3d0bcSAndrea Gelmini 		 * We want a new commit: OK, mark the request and wakeup the
485470decc6SDave Kleikamp 		 * commit thread.  We do _not_ do the commit ourselves.
486470decc6SDave Kleikamp 		 */
487470decc6SDave Kleikamp 
488470decc6SDave Kleikamp 		journal->j_commit_request = target;
489cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: requesting commit %u/%u\n",
490470decc6SDave Kleikamp 			  journal->j_commit_request,
491470decc6SDave Kleikamp 			  journal->j_commit_sequence);
4929fff24aaSTheodore Ts'o 		journal->j_running_transaction->t_requested = jiffies;
493470decc6SDave Kleikamp 		wake_up(&journal->j_wait_commit);
494470decc6SDave Kleikamp 		return 1;
495deeeaf13STheodore Ts'o 	} else if (!tid_geq(journal->j_commit_request, target))
496deeeaf13STheodore Ts'o 		/* This should never happen, but if it does, preserve
497deeeaf13STheodore Ts'o 		   the evidence before kjournald goes into a loop and
498deeeaf13STheodore Ts'o 		   increments j_commit_sequence beyond all recognition. */
499f2a44523SEryu Guan 		WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
5001be2add6STheodore Ts'o 			  journal->j_commit_request,
5011be2add6STheodore Ts'o 			  journal->j_commit_sequence,
502deeeaf13STheodore Ts'o 			  target, journal->j_running_transaction ?
503deeeaf13STheodore Ts'o 			  journal->j_running_transaction->t_tid : 0);
504470decc6SDave Kleikamp 	return 0;
505470decc6SDave Kleikamp }
506470decc6SDave Kleikamp 
jbd2_log_start_commit(journal_t * journal,tid_t tid)507f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid)
508470decc6SDave Kleikamp {
509470decc6SDave Kleikamp 	int ret;
510470decc6SDave Kleikamp 
511a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
512f7f4bccbSMingming Cao 	ret = __jbd2_log_start_commit(journal, tid);
513a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
514470decc6SDave Kleikamp 	return ret;
515470decc6SDave Kleikamp }
516470decc6SDave Kleikamp 
517470decc6SDave Kleikamp /*
5189ff86446SDmitry Monakhov  * Force and wait any uncommitted transactions.  We can only force the running
5199ff86446SDmitry Monakhov  * transaction if we don't have an active handle, otherwise, we will deadlock.
5209ff86446SDmitry Monakhov  * Returns: <0 in case of error,
5219ff86446SDmitry Monakhov  *           0 if nothing to commit,
5229ff86446SDmitry Monakhov  *           1 if transaction was successfully committed.
523470decc6SDave Kleikamp  */
__jbd2_journal_force_commit(journal_t * journal)5249ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal)
525470decc6SDave Kleikamp {
526470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
527470decc6SDave Kleikamp 	tid_t tid;
5289ff86446SDmitry Monakhov 	int need_to_start = 0, ret = 0;
529470decc6SDave Kleikamp 
530a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
531470decc6SDave Kleikamp 	if (journal->j_running_transaction && !current->journal_info) {
532470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
533e4471831STheodore Ts'o 		if (!tid_geq(journal->j_commit_request, transaction->t_tid))
534e4471831STheodore Ts'o 			need_to_start = 1;
535470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
536470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
537470decc6SDave Kleikamp 
538470decc6SDave Kleikamp 	if (!transaction) {
5399ff86446SDmitry Monakhov 		/* Nothing to commit */
540a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
5419ff86446SDmitry Monakhov 		return 0;
542470decc6SDave Kleikamp 	}
543470decc6SDave Kleikamp 	tid = transaction->t_tid;
544a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
545e4471831STheodore Ts'o 	if (need_to_start)
546e4471831STheodore Ts'o 		jbd2_log_start_commit(journal, tid);
5479ff86446SDmitry Monakhov 	ret = jbd2_log_wait_commit(journal, tid);
5489ff86446SDmitry Monakhov 	if (!ret)
5499ff86446SDmitry Monakhov 		ret = 1;
5509ff86446SDmitry Monakhov 
5519ff86446SDmitry Monakhov 	return ret;
5529ff86446SDmitry Monakhov }
5539ff86446SDmitry Monakhov 
5549ff86446SDmitry Monakhov /**
5552bf31d94SMauro Carvalho Chehab  * jbd2_journal_force_commit_nested - Force and wait upon a commit if the
5562bf31d94SMauro Carvalho Chehab  * calling process is not within transaction.
5579ff86446SDmitry Monakhov  *
5589ff86446SDmitry Monakhov  * @journal: journal to force
5599ff86446SDmitry Monakhov  * Returns true if progress was made.
5602bf31d94SMauro Carvalho Chehab  *
5612bf31d94SMauro Carvalho Chehab  * This is used for forcing out undo-protected data which contains
5622bf31d94SMauro Carvalho Chehab  * bitmaps, when the fs is running out of space.
5639ff86446SDmitry Monakhov  */
jbd2_journal_force_commit_nested(journal_t * journal)5649ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal)
5659ff86446SDmitry Monakhov {
5669ff86446SDmitry Monakhov 	int ret;
5679ff86446SDmitry Monakhov 
5689ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
5699ff86446SDmitry Monakhov 	return ret > 0;
5709ff86446SDmitry Monakhov }
5719ff86446SDmitry Monakhov 
5729ff86446SDmitry Monakhov /**
5732bf31d94SMauro Carvalho Chehab  * jbd2_journal_force_commit() - force any uncommitted transactions
5749ff86446SDmitry Monakhov  * @journal: journal to force
5759ff86446SDmitry Monakhov  *
5769ff86446SDmitry Monakhov  * Caller want unconditional commit. We can only force the running transaction
5779ff86446SDmitry Monakhov  * if we don't have an active handle, otherwise, we will deadlock.
5789ff86446SDmitry Monakhov  */
jbd2_journal_force_commit(journal_t * journal)5799ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal)
5809ff86446SDmitry Monakhov {
5819ff86446SDmitry Monakhov 	int ret;
5829ff86446SDmitry Monakhov 
5839ff86446SDmitry Monakhov 	J_ASSERT(!current->journal_info);
5849ff86446SDmitry Monakhov 	ret = __jbd2_journal_force_commit(journal);
5859ff86446SDmitry Monakhov 	if (ret > 0)
5869ff86446SDmitry Monakhov 		ret = 0;
5879ff86446SDmitry Monakhov 	return ret;
588470decc6SDave Kleikamp }
589470decc6SDave Kleikamp 
590470decc6SDave Kleikamp /*
591470decc6SDave Kleikamp  * Start a commit of the current running transaction (if any).  Returns true
592c88ccea3SJan Kara  * if a transaction is going to be committed (or is currently already
593c88ccea3SJan Kara  * committing), and fills its tid in at *ptid
594470decc6SDave Kleikamp  */
jbd2_journal_start_commit(journal_t * journal,tid_t * ptid)595f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
596470decc6SDave Kleikamp {
597470decc6SDave Kleikamp 	int ret = 0;
598470decc6SDave Kleikamp 
599a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
600470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
601470decc6SDave Kleikamp 		tid_t tid = journal->j_running_transaction->t_tid;
602470decc6SDave Kleikamp 
603c88ccea3SJan Kara 		__jbd2_log_start_commit(journal, tid);
604c88ccea3SJan Kara 		/* There's a running transaction and we've just made sure
605c88ccea3SJan Kara 		 * it's commit has been scheduled. */
606c88ccea3SJan Kara 		if (ptid)
607470decc6SDave Kleikamp 			*ptid = tid;
608c88ccea3SJan Kara 		ret = 1;
609c88ccea3SJan Kara 	} else if (journal->j_committing_transaction) {
610470decc6SDave Kleikamp 		/*
61112810ad7SArtem Bityutskiy 		 * If commit has been started, then we have to wait for
61212810ad7SArtem Bityutskiy 		 * completion of that transaction.
613470decc6SDave Kleikamp 		 */
614c88ccea3SJan Kara 		if (ptid)
615470decc6SDave Kleikamp 			*ptid = journal->j_committing_transaction->t_tid;
616470decc6SDave Kleikamp 		ret = 1;
617470decc6SDave Kleikamp 	}
618a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
619470decc6SDave Kleikamp 	return ret;
620470decc6SDave Kleikamp }
621470decc6SDave Kleikamp 
622470decc6SDave Kleikamp /*
623bbd2be36SJan Kara  * Return 1 if a given transaction has not yet sent barrier request
624bbd2be36SJan Kara  * connected with a transaction commit. If 0 is returned, transaction
625bbd2be36SJan Kara  * may or may not have sent the barrier. Used to avoid sending barrier
626bbd2be36SJan Kara  * twice in common cases.
627bbd2be36SJan Kara  */
jbd2_trans_will_send_data_barrier(journal_t * journal,tid_t tid)628bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
629bbd2be36SJan Kara {
630bbd2be36SJan Kara 	int ret = 0;
631bbd2be36SJan Kara 	transaction_t *commit_trans;
632bbd2be36SJan Kara 
633bbd2be36SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
634bbd2be36SJan Kara 		return 0;
635bbd2be36SJan Kara 	read_lock(&journal->j_state_lock);
636bbd2be36SJan Kara 	/* Transaction already committed? */
637bbd2be36SJan Kara 	if (tid_geq(journal->j_commit_sequence, tid))
638bbd2be36SJan Kara 		goto out;
639bbd2be36SJan Kara 	commit_trans = journal->j_committing_transaction;
640bbd2be36SJan Kara 	if (!commit_trans || commit_trans->t_tid != tid) {
641bbd2be36SJan Kara 		ret = 1;
642bbd2be36SJan Kara 		goto out;
643bbd2be36SJan Kara 	}
644bbd2be36SJan Kara 	/*
645bbd2be36SJan Kara 	 * Transaction is being committed and we already proceeded to
646bbd2be36SJan Kara 	 * submitting a flush to fs partition?
647bbd2be36SJan Kara 	 */
648bbd2be36SJan Kara 	if (journal->j_fs_dev != journal->j_dev) {
649bbd2be36SJan Kara 		if (!commit_trans->t_need_data_flush ||
650bbd2be36SJan Kara 		    commit_trans->t_state >= T_COMMIT_DFLUSH)
651bbd2be36SJan Kara 			goto out;
652bbd2be36SJan Kara 	} else {
653bbd2be36SJan Kara 		if (commit_trans->t_state >= T_COMMIT_JFLUSH)
654bbd2be36SJan Kara 			goto out;
655bbd2be36SJan Kara 	}
656bbd2be36SJan Kara 	ret = 1;
657bbd2be36SJan Kara out:
658bbd2be36SJan Kara 	read_unlock(&journal->j_state_lock);
659bbd2be36SJan Kara 	return ret;
660bbd2be36SJan Kara }
661bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
662bbd2be36SJan Kara 
663bbd2be36SJan Kara /*
664470decc6SDave Kleikamp  * Wait for a specified commit to complete.
665470decc6SDave Kleikamp  * The caller may not hold the journal lock.
666470decc6SDave Kleikamp  */
jbd2_log_wait_commit(journal_t * journal,tid_t tid)667f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
668470decc6SDave Kleikamp {
669470decc6SDave Kleikamp 	int err = 0;
670470decc6SDave Kleikamp 
671c52c47e4SJan Kara 	read_lock(&journal->j_state_lock);
672c52c47e4SJan Kara #ifdef CONFIG_PROVE_LOCKING
673c52c47e4SJan Kara 	/*
674c52c47e4SJan Kara 	 * Some callers make sure transaction is already committing and in that
675c52c47e4SJan Kara 	 * case we cannot block on open handles anymore. So don't warn in that
676c52c47e4SJan Kara 	 * case.
677c52c47e4SJan Kara 	 */
678c52c47e4SJan Kara 	if (tid_gt(tid, journal->j_commit_sequence) &&
679c52c47e4SJan Kara 	    (!journal->j_committing_transaction ||
680c52c47e4SJan Kara 	     journal->j_committing_transaction->t_tid != tid)) {
681c52c47e4SJan Kara 		read_unlock(&journal->j_state_lock);
6821eaa566dSJan Kara 		jbd2_might_wait_for_commit(journal);
683a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
684c52c47e4SJan Kara 	}
685c52c47e4SJan Kara #endif
686e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
687470decc6SDave Kleikamp 	if (!tid_geq(journal->j_commit_request, tid)) {
68875685071SJan Kara 		printk(KERN_ERR
6897821ce41SGaowei Pu 		       "%s: error: j_commit_request=%u, tid=%u\n",
690329d291fSHarvey Harrison 		       __func__, journal->j_commit_request, tid);
691470decc6SDave Kleikamp 	}
692470decc6SDave Kleikamp #endif
693470decc6SDave Kleikamp 	while (tid_gt(tid, journal->j_commit_sequence)) {
694cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: want %u, j_commit_sequence=%u\n",
695470decc6SDave Kleikamp 				  tid, journal->j_commit_sequence);
696a931da6aSTheodore Ts'o 		read_unlock(&journal->j_state_lock);
6973469a32aSTheodore Ts'o 		wake_up(&journal->j_wait_commit);
698470decc6SDave Kleikamp 		wait_event(journal->j_wait_done_commit,
699470decc6SDave Kleikamp 				!tid_gt(tid, journal->j_commit_sequence));
700a931da6aSTheodore Ts'o 		read_lock(&journal->j_state_lock);
701470decc6SDave Kleikamp 	}
702a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
703470decc6SDave Kleikamp 
70475685071SJan Kara 	if (unlikely(is_journal_aborted(journal)))
705470decc6SDave Kleikamp 		err = -EIO;
706470decc6SDave Kleikamp 	return err;
707470decc6SDave Kleikamp }
708470decc6SDave Kleikamp 
709ff780b91SHarshad Shirwadkar /*
710ff780b91SHarshad Shirwadkar  * Start a fast commit. If there's an ongoing fast or full commit wait for
711ff780b91SHarshad Shirwadkar  * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY
712ff780b91SHarshad Shirwadkar  * if a fast commit is not needed, either because there's an already a commit
713ff780b91SHarshad Shirwadkar  * going on or this tid has already been committed. Returns -EINVAL if no jbd2
714ff780b91SHarshad Shirwadkar  * commit has yet been performed.
715ff780b91SHarshad Shirwadkar  */
jbd2_fc_begin_commit(journal_t * journal,tid_t tid)716ff780b91SHarshad Shirwadkar int jbd2_fc_begin_commit(journal_t *journal, tid_t tid)
717ff780b91SHarshad Shirwadkar {
71887a144f0SHarshad Shirwadkar 	if (unlikely(is_journal_aborted(journal)))
71987a144f0SHarshad Shirwadkar 		return -EIO;
720ff780b91SHarshad Shirwadkar 	/*
721ff780b91SHarshad Shirwadkar 	 * Fast commits only allowed if at least one full commit has
722ff780b91SHarshad Shirwadkar 	 * been processed.
723ff780b91SHarshad Shirwadkar 	 */
724ff780b91SHarshad Shirwadkar 	if (!journal->j_stats.ts_tid)
725ff780b91SHarshad Shirwadkar 		return -EINVAL;
726ff780b91SHarshad Shirwadkar 
727ff780b91SHarshad Shirwadkar 	write_lock(&journal->j_state_lock);
728*fd349624SKemeng Shi 	if (tid_geq(journal->j_commit_sequence, tid)) {
729480f89d5SHarshad Shirwadkar 		write_unlock(&journal->j_state_lock);
730480f89d5SHarshad Shirwadkar 		return -EALREADY;
731480f89d5SHarshad Shirwadkar 	}
732480f89d5SHarshad Shirwadkar 
733ff780b91SHarshad Shirwadkar 	if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
734ff780b91SHarshad Shirwadkar 	    (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) {
735ff780b91SHarshad Shirwadkar 		DEFINE_WAIT(wait);
736ff780b91SHarshad Shirwadkar 
737ff780b91SHarshad Shirwadkar 		prepare_to_wait(&journal->j_fc_wait, &wait,
738ff780b91SHarshad Shirwadkar 				TASK_UNINTERRUPTIBLE);
739ff780b91SHarshad Shirwadkar 		write_unlock(&journal->j_state_lock);
740ff780b91SHarshad Shirwadkar 		schedule();
741ff780b91SHarshad Shirwadkar 		finish_wait(&journal->j_fc_wait, &wait);
742ff780b91SHarshad Shirwadkar 		return -EALREADY;
743ff780b91SHarshad Shirwadkar 	}
744ff780b91SHarshad Shirwadkar 	journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
745ff780b91SHarshad Shirwadkar 	write_unlock(&journal->j_state_lock);
7462729cfdcSHarshad Shirwadkar 	jbd2_journal_lock_updates(journal);
747ff780b91SHarshad Shirwadkar 
748ff780b91SHarshad Shirwadkar 	return 0;
749ff780b91SHarshad Shirwadkar }
750ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_begin_commit);
751ff780b91SHarshad Shirwadkar 
752ff780b91SHarshad Shirwadkar /*
753ff780b91SHarshad Shirwadkar  * Stop a fast commit. If fallback is set, this function starts commit of
754ff780b91SHarshad Shirwadkar  * TID tid before any other fast commit can start.
755ff780b91SHarshad Shirwadkar  */
__jbd2_fc_end_commit(journal_t * journal,tid_t tid,bool fallback)756ff780b91SHarshad Shirwadkar static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback)
757ff780b91SHarshad Shirwadkar {
758ff780b91SHarshad Shirwadkar 	if (journal->j_fc_cleanup_callback)
759e85c81baSXin Yin 		journal->j_fc_cleanup_callback(journal, 0, tid);
760d13a3558SLuis Henriques (SUSE) 	jbd2_journal_unlock_updates(journal);
761ff780b91SHarshad Shirwadkar 	write_lock(&journal->j_state_lock);
762ff780b91SHarshad Shirwadkar 	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
763ff780b91SHarshad Shirwadkar 	if (fallback)
764ff780b91SHarshad Shirwadkar 		journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
765ff780b91SHarshad Shirwadkar 	write_unlock(&journal->j_state_lock);
766ff780b91SHarshad Shirwadkar 	wake_up(&journal->j_fc_wait);
767ff780b91SHarshad Shirwadkar 	if (fallback)
768ff780b91SHarshad Shirwadkar 		return jbd2_complete_transaction(journal, tid);
769ff780b91SHarshad Shirwadkar 	return 0;
770ff780b91SHarshad Shirwadkar }
771ff780b91SHarshad Shirwadkar 
jbd2_fc_end_commit(journal_t * journal)772ff780b91SHarshad Shirwadkar int jbd2_fc_end_commit(journal_t *journal)
773ff780b91SHarshad Shirwadkar {
7740bce577bSHarshad Shirwadkar 	return __jbd2_fc_end_commit(journal, 0, false);
775ff780b91SHarshad Shirwadkar }
776ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit);
777ff780b91SHarshad Shirwadkar 
jbd2_fc_end_commit_fallback(journal_t * journal)7780bce577bSHarshad Shirwadkar int jbd2_fc_end_commit_fallback(journal_t *journal)
779ff780b91SHarshad Shirwadkar {
7800bce577bSHarshad Shirwadkar 	tid_t tid;
7810bce577bSHarshad Shirwadkar 
7820bce577bSHarshad Shirwadkar 	read_lock(&journal->j_state_lock);
7830bce577bSHarshad Shirwadkar 	tid = journal->j_running_transaction ?
7840bce577bSHarshad Shirwadkar 		journal->j_running_transaction->t_tid : 0;
7850bce577bSHarshad Shirwadkar 	read_unlock(&journal->j_state_lock);
7860bce577bSHarshad Shirwadkar 	return __jbd2_fc_end_commit(journal, tid, true);
787ff780b91SHarshad Shirwadkar }
788ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit_fallback);
789ff780b91SHarshad Shirwadkar 
790b8a6176cSJan Kara /* Return 1 when transaction with given tid has already committed. */
jbd2_transaction_committed(journal_t * journal,tid_t tid)791b8a6176cSJan Kara int jbd2_transaction_committed(journal_t *journal, tid_t tid)
792b8a6176cSJan Kara {
793b8a6176cSJan Kara 	int ret = 1;
794b8a6176cSJan Kara 
795b8a6176cSJan Kara 	read_lock(&journal->j_state_lock);
796b8a6176cSJan Kara 	if (journal->j_running_transaction &&
797b8a6176cSJan Kara 	    journal->j_running_transaction->t_tid == tid)
798b8a6176cSJan Kara 		ret = 0;
799b8a6176cSJan Kara 	if (journal->j_committing_transaction &&
800b8a6176cSJan Kara 	    journal->j_committing_transaction->t_tid == tid)
801b8a6176cSJan Kara 		ret = 0;
802b8a6176cSJan Kara 	read_unlock(&journal->j_state_lock);
803b8a6176cSJan Kara 	return ret;
804b8a6176cSJan Kara }
805b8a6176cSJan Kara EXPORT_SYMBOL(jbd2_transaction_committed);
806b8a6176cSJan Kara 
807470decc6SDave Kleikamp /*
808d76a3a77STheodore Ts'o  * When this function returns the transaction corresponding to tid
809d76a3a77STheodore Ts'o  * will be completed.  If the transaction has currently running, start
810d76a3a77STheodore Ts'o  * committing that transaction before waiting for it to complete.  If
811d76a3a77STheodore Ts'o  * the transaction id is stale, it is by definition already completed,
812d76a3a77STheodore Ts'o  * so just return SUCCESS.
813d76a3a77STheodore Ts'o  */
jbd2_complete_transaction(journal_t * journal,tid_t tid)814d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid)
815d76a3a77STheodore Ts'o {
816d76a3a77STheodore Ts'o 	int	need_to_wait = 1;
817d76a3a77STheodore Ts'o 
818d76a3a77STheodore Ts'o 	read_lock(&journal->j_state_lock);
819d76a3a77STheodore Ts'o 	if (journal->j_running_transaction &&
820d76a3a77STheodore Ts'o 	    journal->j_running_transaction->t_tid == tid) {
821d76a3a77STheodore Ts'o 		if (journal->j_commit_request != tid) {
822d76a3a77STheodore Ts'o 			/* transaction not yet started, so request it */
823d76a3a77STheodore Ts'o 			read_unlock(&journal->j_state_lock);
824d76a3a77STheodore Ts'o 			jbd2_log_start_commit(journal, tid);
825d76a3a77STheodore Ts'o 			goto wait_commit;
826d76a3a77STheodore Ts'o 		}
827d76a3a77STheodore Ts'o 	} else if (!(journal->j_committing_transaction &&
828d76a3a77STheodore Ts'o 		     journal->j_committing_transaction->t_tid == tid))
829d76a3a77STheodore Ts'o 		need_to_wait = 0;
830d76a3a77STheodore Ts'o 	read_unlock(&journal->j_state_lock);
831d76a3a77STheodore Ts'o 	if (!need_to_wait)
832d76a3a77STheodore Ts'o 		return 0;
833d76a3a77STheodore Ts'o wait_commit:
834d76a3a77STheodore Ts'o 	return jbd2_log_wait_commit(journal, tid);
835d76a3a77STheodore Ts'o }
836d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction);
837d76a3a77STheodore Ts'o 
838d76a3a77STheodore Ts'o /*
839470decc6SDave Kleikamp  * Log buffer allocation routines:
840470decc6SDave Kleikamp  */
841470decc6SDave Kleikamp 
jbd2_journal_next_log_block(journal_t * journal,unsigned long long * retp)84218eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
843470decc6SDave Kleikamp {
844470decc6SDave Kleikamp 	unsigned long blocknr;
845470decc6SDave Kleikamp 
846a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
847470decc6SDave Kleikamp 	J_ASSERT(journal->j_free > 1);
848470decc6SDave Kleikamp 
849470decc6SDave Kleikamp 	blocknr = journal->j_head;
850470decc6SDave Kleikamp 	journal->j_head++;
851470decc6SDave Kleikamp 	journal->j_free--;
852470decc6SDave Kleikamp 	if (journal->j_head == journal->j_last)
853470decc6SDave Kleikamp 		journal->j_head = journal->j_first;
854a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
855f7f4bccbSMingming Cao 	return jbd2_journal_bmap(journal, blocknr, retp);
856470decc6SDave Kleikamp }
857470decc6SDave Kleikamp 
858ff780b91SHarshad Shirwadkar /* Map one fast commit buffer for use by the file system */
jbd2_fc_get_buf(journal_t * journal,struct buffer_head ** bh_out)859ff780b91SHarshad Shirwadkar int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out)
860ff780b91SHarshad Shirwadkar {
861ff780b91SHarshad Shirwadkar 	unsigned long long pblock;
862ff780b91SHarshad Shirwadkar 	unsigned long blocknr;
863ff780b91SHarshad Shirwadkar 	int ret = 0;
864ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
865ff780b91SHarshad Shirwadkar 	int fc_off;
866ff780b91SHarshad Shirwadkar 
867ff780b91SHarshad Shirwadkar 	*bh_out = NULL;
868ff780b91SHarshad Shirwadkar 
869ff780b91SHarshad Shirwadkar 	if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) {
870ff780b91SHarshad Shirwadkar 		fc_off = journal->j_fc_off;
871ff780b91SHarshad Shirwadkar 		blocknr = journal->j_fc_first + fc_off;
872ff780b91SHarshad Shirwadkar 		journal->j_fc_off++;
873ff780b91SHarshad Shirwadkar 	} else {
874ff780b91SHarshad Shirwadkar 		ret = -EINVAL;
875ff780b91SHarshad Shirwadkar 	}
876ff780b91SHarshad Shirwadkar 
877ff780b91SHarshad Shirwadkar 	if (ret)
878ff780b91SHarshad Shirwadkar 		return ret;
879ff780b91SHarshad Shirwadkar 
880ff780b91SHarshad Shirwadkar 	ret = jbd2_journal_bmap(journal, blocknr, &pblock);
881ff780b91SHarshad Shirwadkar 	if (ret)
882ff780b91SHarshad Shirwadkar 		return ret;
883ff780b91SHarshad Shirwadkar 
884ff780b91SHarshad Shirwadkar 	bh = __getblk(journal->j_dev, pblock, journal->j_blocksize);
885ff780b91SHarshad Shirwadkar 	if (!bh)
886ff780b91SHarshad Shirwadkar 		return -ENOMEM;
887ff780b91SHarshad Shirwadkar 
888ff780b91SHarshad Shirwadkar 
889ff780b91SHarshad Shirwadkar 	journal->j_fc_wbuf[fc_off] = bh;
890ff780b91SHarshad Shirwadkar 
891ff780b91SHarshad Shirwadkar 	*bh_out = bh;
892ff780b91SHarshad Shirwadkar 
893ff780b91SHarshad Shirwadkar 	return 0;
894ff780b91SHarshad Shirwadkar }
895ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_get_buf);
896ff780b91SHarshad Shirwadkar 
897ff780b91SHarshad Shirwadkar /*
898ff780b91SHarshad Shirwadkar  * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
899ff780b91SHarshad Shirwadkar  * for completion.
900ff780b91SHarshad Shirwadkar  */
jbd2_fc_wait_bufs(journal_t * journal,int num_blks)901ff780b91SHarshad Shirwadkar int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
902ff780b91SHarshad Shirwadkar {
903ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
904ff780b91SHarshad Shirwadkar 	int i, j_fc_off;
905ff780b91SHarshad Shirwadkar 
906ff780b91SHarshad Shirwadkar 	j_fc_off = journal->j_fc_off;
907ff780b91SHarshad Shirwadkar 
908ff780b91SHarshad Shirwadkar 	/*
909ff780b91SHarshad Shirwadkar 	 * Wait in reverse order to minimize chances of us being woken up before
910ff780b91SHarshad Shirwadkar 	 * all IOs have completed
911ff780b91SHarshad Shirwadkar 	 */
912ff780b91SHarshad Shirwadkar 	for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
913ff780b91SHarshad Shirwadkar 		bh = journal->j_fc_wbuf[i];
914ff780b91SHarshad Shirwadkar 		wait_on_buffer(bh);
915e0d5fc7aSYe Bin 		/*
916e0d5fc7aSYe Bin 		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
917e0d5fc7aSYe Bin 		 * buffer head.
918e0d5fc7aSYe Bin 		 */
919e0d5fc7aSYe Bin 		if (unlikely(!buffer_uptodate(bh))) {
920243d1a5dSYe Bin 			journal->j_fc_off = i + 1;
921ff780b91SHarshad Shirwadkar 			return -EIO;
922ff780b91SHarshad Shirwadkar 		}
923243d1a5dSYe Bin 		put_bh(bh);
924243d1a5dSYe Bin 		journal->j_fc_wbuf[i] = NULL;
925e0d5fc7aSYe Bin 	}
926ff780b91SHarshad Shirwadkar 
927ff780b91SHarshad Shirwadkar 	return 0;
928ff780b91SHarshad Shirwadkar }
929ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_wait_bufs);
930ff780b91SHarshad Shirwadkar 
jbd2_fc_release_bufs(journal_t * journal)931ff780b91SHarshad Shirwadkar int jbd2_fc_release_bufs(journal_t *journal)
932ff780b91SHarshad Shirwadkar {
933ff780b91SHarshad Shirwadkar 	struct buffer_head *bh;
934ff780b91SHarshad Shirwadkar 	int i, j_fc_off;
935ff780b91SHarshad Shirwadkar 
936ff780b91SHarshad Shirwadkar 	j_fc_off = journal->j_fc_off;
937ff780b91SHarshad Shirwadkar 
938ff780b91SHarshad Shirwadkar 	for (i = j_fc_off - 1; i >= 0; i--) {
939ff780b91SHarshad Shirwadkar 		bh = journal->j_fc_wbuf[i];
940ff780b91SHarshad Shirwadkar 		if (!bh)
941ff780b91SHarshad Shirwadkar 			break;
942ff780b91SHarshad Shirwadkar 		put_bh(bh);
943ff780b91SHarshad Shirwadkar 		journal->j_fc_wbuf[i] = NULL;
944ff780b91SHarshad Shirwadkar 	}
945ff780b91SHarshad Shirwadkar 
946ff780b91SHarshad Shirwadkar 	return 0;
947ff780b91SHarshad Shirwadkar }
948ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_release_bufs);
949ff780b91SHarshad Shirwadkar 
950470decc6SDave Kleikamp /*
951470decc6SDave Kleikamp  * Conversion of logical to physical block numbers for the journal
952470decc6SDave Kleikamp  *
953470decc6SDave Kleikamp  * On external journals the journal blocks are identity-mapped, so
954470decc6SDave Kleikamp  * this is a no-op.  If needed, we can use j_blk_offset - everything is
955470decc6SDave Kleikamp  * ready.
956470decc6SDave Kleikamp  */
jbd2_journal_bmap(journal_t * journal,unsigned long blocknr,unsigned long long * retp)957f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
95818eba7aaSMingming Cao 		 unsigned long long *retp)
959470decc6SDave Kleikamp {
960470decc6SDave Kleikamp 	int err = 0;
96118eba7aaSMingming Cao 	unsigned long long ret;
96262913ae9STheodore Ts'o 	sector_t block = blocknr;
963470decc6SDave Kleikamp 
96462913ae9STheodore Ts'o 	if (journal->j_bmap) {
96562913ae9STheodore Ts'o 		err = journal->j_bmap(journal, &block);
96662913ae9STheodore Ts'o 		if (err == 0)
96762913ae9STheodore Ts'o 			*retp = block;
96862913ae9STheodore Ts'o 	} else if (journal->j_inode) {
96930460e1eSCarlos Maiolino 		ret = bmap(journal->j_inode, &block);
97030460e1eSCarlos Maiolino 
97130460e1eSCarlos Maiolino 		if (ret || !block) {
972470decc6SDave Kleikamp 			printk(KERN_ALERT "%s: journal block not found "
973470decc6SDave Kleikamp 					"at offset %lu on %s\n",
97405496769STheodore Ts'o 			       __func__, blocknr, journal->j_devname);
975470decc6SDave Kleikamp 			err = -EIO;
9767f6225e4Szhangyi (F) 			jbd2_journal_abort(journal, err);
97730460e1eSCarlos Maiolino 		} else {
97830460e1eSCarlos Maiolino 			*retp = block;
979470decc6SDave Kleikamp 		}
98030460e1eSCarlos Maiolino 
981470decc6SDave Kleikamp 	} else {
982470decc6SDave Kleikamp 		*retp = blocknr; /* +journal->j_blk_offset */
983470decc6SDave Kleikamp 	}
984470decc6SDave Kleikamp 	return err;
985470decc6SDave Kleikamp }
986470decc6SDave Kleikamp 
987470decc6SDave Kleikamp /*
988470decc6SDave Kleikamp  * We play buffer_head aliasing tricks to write data/metadata blocks to
989470decc6SDave Kleikamp  * the journal without copying their contents, but for journal
990470decc6SDave Kleikamp  * descriptor blocks we do need to generate bona fide buffers.
991470decc6SDave Kleikamp  *
992f7f4bccbSMingming Cao  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
993470decc6SDave Kleikamp  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
994470decc6SDave Kleikamp  * But we don't bother doing that, so there will be coherency problems with
995470decc6SDave Kleikamp  * mmaps of blockdevs which hold live JBD-controlled filesystems.
996470decc6SDave Kleikamp  */
99732ab6715SJan Kara struct buffer_head *
jbd2_journal_get_descriptor_buffer(transaction_t * transaction,int type)99832ab6715SJan Kara jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type)
999470decc6SDave Kleikamp {
100032ab6715SJan Kara 	journal_t *journal = transaction->t_journal;
1001470decc6SDave Kleikamp 	struct buffer_head *bh;
100218eba7aaSMingming Cao 	unsigned long long blocknr;
100332ab6715SJan Kara 	journal_header_t *header;
1004470decc6SDave Kleikamp 	int err;
1005470decc6SDave Kleikamp 
1006f7f4bccbSMingming Cao 	err = jbd2_journal_next_log_block(journal, &blocknr);
1007470decc6SDave Kleikamp 
1008470decc6SDave Kleikamp 	if (err)
1009470decc6SDave Kleikamp 		return NULL;
1010470decc6SDave Kleikamp 
1011470decc6SDave Kleikamp 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
10124b905671SJan Kara 	if (!bh)
10134b905671SJan Kara 		return NULL;
10149f356e5aSJan Kara 	atomic_dec(&transaction->t_outstanding_credits);
1015470decc6SDave Kleikamp 	lock_buffer(bh);
1016470decc6SDave Kleikamp 	memset(bh->b_data, 0, journal->j_blocksize);
101732ab6715SJan Kara 	header = (journal_header_t *)bh->b_data;
101832ab6715SJan Kara 	header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
101932ab6715SJan Kara 	header->h_blocktype = cpu_to_be32(type);
102032ab6715SJan Kara 	header->h_sequence = cpu_to_be32(transaction->t_tid);
1021470decc6SDave Kleikamp 	set_buffer_uptodate(bh);
1022470decc6SDave Kleikamp 	unlock_buffer(bh);
1023470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "return this buffer");
1024e5a120aeSJan Kara 	return bh;
1025470decc6SDave Kleikamp }
1026470decc6SDave Kleikamp 
jbd2_descriptor_block_csum_set(journal_t * j,struct buffer_head * bh)10271101cd4dSJan Kara void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh)
10281101cd4dSJan Kara {
10291101cd4dSJan Kara 	struct jbd2_journal_block_tail *tail;
10301101cd4dSJan Kara 	__u32 csum;
10311101cd4dSJan Kara 
10321101cd4dSJan Kara 	if (!jbd2_journal_has_csum_v2or3(j))
10331101cd4dSJan Kara 		return;
10341101cd4dSJan Kara 
10351101cd4dSJan Kara 	tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
10361101cd4dSJan Kara 			sizeof(struct jbd2_journal_block_tail));
10371101cd4dSJan Kara 	tail->t_checksum = 0;
10381101cd4dSJan Kara 	csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
10391101cd4dSJan Kara 	tail->t_checksum = cpu_to_be32(csum);
10401101cd4dSJan Kara }
10411101cd4dSJan Kara 
104279feb521SJan Kara /*
104379feb521SJan Kara  * Return tid of the oldest transaction in the journal and block in the journal
104479feb521SJan Kara  * where the transaction starts.
104579feb521SJan Kara  *
104679feb521SJan Kara  * If the journal is now empty, return which will be the next transaction ID
104779feb521SJan Kara  * we will write and where will that transaction start.
104879feb521SJan Kara  *
104979feb521SJan Kara  * The return value is 0 if journal tail cannot be pushed any further, 1 if
105079feb521SJan Kara  * it can.
105179feb521SJan Kara  */
jbd2_journal_get_log_tail(journal_t * journal,tid_t * tid,unsigned long * block)105279feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
105379feb521SJan Kara 			      unsigned long *block)
105479feb521SJan Kara {
105579feb521SJan Kara 	transaction_t *transaction;
105679feb521SJan Kara 	int ret;
105779feb521SJan Kara 
105879feb521SJan Kara 	read_lock(&journal->j_state_lock);
105979feb521SJan Kara 	spin_lock(&journal->j_list_lock);
106079feb521SJan Kara 	transaction = journal->j_checkpoint_transactions;
106179feb521SJan Kara 	if (transaction) {
106279feb521SJan Kara 		*tid = transaction->t_tid;
106379feb521SJan Kara 		*block = transaction->t_log_start;
106479feb521SJan Kara 	} else if ((transaction = journal->j_committing_transaction) != NULL) {
106579feb521SJan Kara 		*tid = transaction->t_tid;
106679feb521SJan Kara 		*block = transaction->t_log_start;
106779feb521SJan Kara 	} else if ((transaction = journal->j_running_transaction) != NULL) {
106879feb521SJan Kara 		*tid = transaction->t_tid;
106979feb521SJan Kara 		*block = journal->j_head;
107079feb521SJan Kara 	} else {
107179feb521SJan Kara 		*tid = journal->j_transaction_sequence;
107279feb521SJan Kara 		*block = journal->j_head;
107379feb521SJan Kara 	}
107479feb521SJan Kara 	ret = tid_gt(*tid, journal->j_tail_sequence);
107579feb521SJan Kara 	spin_unlock(&journal->j_list_lock);
107679feb521SJan Kara 	read_unlock(&journal->j_state_lock);
107779feb521SJan Kara 
107879feb521SJan Kara 	return ret;
107979feb521SJan Kara }
108079feb521SJan Kara 
108179feb521SJan Kara /*
108279feb521SJan Kara  * Update information in journal structure and in on disk journal superblock
108379feb521SJan Kara  * about log tail. This function does not check whether information passed in
108479feb521SJan Kara  * really pushes log tail further. It's responsibility of the caller to make
108579feb521SJan Kara  * sure provided log tail information is valid (e.g. by holding
108679feb521SJan Kara  * j_checkpoint_mutex all the time between computing log tail and calling this
108779feb521SJan Kara  * function as is the case with jbd2_cleanup_journal_tail()).
108879feb521SJan Kara  *
108979feb521SJan Kara  * Requires j_checkpoint_mutex
109079feb521SJan Kara  */
__jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)10916f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
109279feb521SJan Kara {
109379feb521SJan Kara 	unsigned long freed;
10946f6a6fdaSJoseph Qi 	int ret;
109579feb521SJan Kara 
109679feb521SJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
109779feb521SJan Kara 
109879feb521SJan Kara 	/*
109979feb521SJan Kara 	 * We cannot afford for write to remain in drive's caches since as
110079feb521SJan Kara 	 * soon as we update j_tail, next transaction can start reusing journal
110179feb521SJan Kara 	 * space and if we lose sb update during power failure we'd replay
110279feb521SJan Kara 	 * old transaction with possibly newly overwritten data.
110379feb521SJan Kara 	 */
11045c480a69SZhang Yi 	ret = jbd2_journal_update_sb_log_tail(journal, tid, block, REQ_FUA);
11056f6a6fdaSJoseph Qi 	if (ret)
11066f6a6fdaSJoseph Qi 		goto out;
11076f6a6fdaSJoseph Qi 
110879feb521SJan Kara 	write_lock(&journal->j_state_lock);
110979feb521SJan Kara 	freed = block - journal->j_tail;
111079feb521SJan Kara 	if (block < journal->j_tail)
111179feb521SJan Kara 		freed += journal->j_last - journal->j_first;
111279feb521SJan Kara 
111379feb521SJan Kara 	trace_jbd2_update_log_tail(journal, tid, block, freed);
1114cb3b3bf2SJan Kara 	jbd2_debug(1,
11157821ce41SGaowei Pu 		  "Cleaning journal tail from %u to %u (offset %lu), "
111679feb521SJan Kara 		  "freeing %lu\n",
111779feb521SJan Kara 		  journal->j_tail_sequence, tid, block, freed);
111879feb521SJan Kara 
111979feb521SJan Kara 	journal->j_free += freed;
112079feb521SJan Kara 	journal->j_tail_sequence = tid;
112179feb521SJan Kara 	journal->j_tail = block;
112279feb521SJan Kara 	write_unlock(&journal->j_state_lock);
11236f6a6fdaSJoseph Qi 
11246f6a6fdaSJoseph Qi out:
11256f6a6fdaSJoseph Qi 	return ret;
112679feb521SJan Kara }
112779feb521SJan Kara 
11283339578fSJan Kara /*
112985e0c4e8STheodore Ts'o  * This is a variation of __jbd2_update_log_tail which checks for validity of
11303339578fSJan Kara  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
11313339578fSJan Kara  * with other threads updating log tail.
11323339578fSJan Kara  */
jbd2_update_log_tail(journal_t * journal,tid_t tid,unsigned long block)11333339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
11343339578fSJan Kara {
11356fa7aa50STejun Heo 	mutex_lock_io(&journal->j_checkpoint_mutex);
11363339578fSJan Kara 	if (tid_gt(tid, journal->j_tail_sequence))
11373339578fSJan Kara 		__jbd2_update_log_tail(journal, tid, block);
11383339578fSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
11393339578fSJan Kara }
11403339578fSJan Kara 
11418e85fb3fSJohann Lombardi struct jbd2_stats_proc_session {
11428e85fb3fSJohann Lombardi 	journal_t *journal;
11438e85fb3fSJohann Lombardi 	struct transaction_stats_s *stats;
11448e85fb3fSJohann Lombardi 	int start;
11458e85fb3fSJohann Lombardi 	int max;
11468e85fb3fSJohann Lombardi };
11478e85fb3fSJohann Lombardi 
jbd2_seq_info_start(struct seq_file * seq,loff_t * pos)11488e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
11498e85fb3fSJohann Lombardi {
11508e85fb3fSJohann Lombardi 	return *pos ? NULL : SEQ_START_TOKEN;
11518e85fb3fSJohann Lombardi }
11528e85fb3fSJohann Lombardi 
jbd2_seq_info_next(struct seq_file * seq,void * v,loff_t * pos)11538e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
11548e85fb3fSJohann Lombardi {
11551a8e9cf4SVasily Averin 	(*pos)++;
11568e85fb3fSJohann Lombardi 	return NULL;
11578e85fb3fSJohann Lombardi }
11588e85fb3fSJohann Lombardi 
jbd2_seq_info_show(struct seq_file * seq,void * v)11598e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v)
11608e85fb3fSJohann Lombardi {
11618e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
11628e85fb3fSJohann Lombardi 
11638e85fb3fSJohann Lombardi 	if (v != SEQ_START_TOKEN)
11648e85fb3fSJohann Lombardi 		return 0;
11659fff24aaSTheodore Ts'o 	seq_printf(seq, "%lu transactions (%lu requested), "
11669fff24aaSTheodore Ts'o 		   "each up to %u blocks\n",
11679fff24aaSTheodore Ts'o 		   s->stats->ts_tid, s->stats->ts_requested,
11688e85fb3fSJohann Lombardi 		   s->journal->j_max_transaction_buffers);
11698e85fb3fSJohann Lombardi 	if (s->stats->ts_tid == 0)
11708e85fb3fSJohann Lombardi 		return 0;
11718e85fb3fSJohann Lombardi 	seq_printf(seq, "average: \n  %ums waiting for transaction\n",
1172bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
11739fff24aaSTheodore Ts'o 	seq_printf(seq, "  %ums request delay\n",
11749fff24aaSTheodore Ts'o 	    (s->stats->ts_requested == 0) ? 0 :
11759fff24aaSTheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_request_delay /
11769fff24aaSTheodore Ts'o 			     s->stats->ts_requested));
11778e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums running transaction\n",
1178bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
11798e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums transaction was being locked\n",
1180bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
11818e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
1182bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
11838e85fb3fSJohann Lombardi 	seq_printf(seq, "  %ums logging transaction\n",
1184bf699327STheodore Ts'o 	    jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
1185c225aa57SSimon Holm Thøgersen 	seq_printf(seq, "  %lluus average transaction commit time\n",
1186c225aa57SSimon Holm Thøgersen 		   div_u64(s->journal->j_average_commit_time, 1000));
11878e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu handles per transaction\n",
1188bf699327STheodore Ts'o 	    s->stats->run.rs_handle_count / s->stats->ts_tid);
11898e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu blocks per transaction\n",
1190bf699327STheodore Ts'o 	    s->stats->run.rs_blocks / s->stats->ts_tid);
11918e85fb3fSJohann Lombardi 	seq_printf(seq, "  %lu logged blocks per transaction\n",
1192bf699327STheodore Ts'o 	    s->stats->run.rs_blocks_logged / s->stats->ts_tid);
11938e85fb3fSJohann Lombardi 	return 0;
11948e85fb3fSJohann Lombardi }
11958e85fb3fSJohann Lombardi 
jbd2_seq_info_stop(struct seq_file * seq,void * v)11968e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
11978e85fb3fSJohann Lombardi {
11988e85fb3fSJohann Lombardi }
11998e85fb3fSJohann Lombardi 
120088e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = {
12018e85fb3fSJohann Lombardi 	.start  = jbd2_seq_info_start,
12028e85fb3fSJohann Lombardi 	.next   = jbd2_seq_info_next,
12038e85fb3fSJohann Lombardi 	.stop   = jbd2_seq_info_stop,
12048e85fb3fSJohann Lombardi 	.show   = jbd2_seq_info_show,
12058e85fb3fSJohann Lombardi };
12068e85fb3fSJohann Lombardi 
jbd2_seq_info_open(struct inode * inode,struct file * file)12078e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file)
12088e85fb3fSJohann Lombardi {
1209359745d7SMuchun Song 	journal_t *journal = pde_data(inode);
12108e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s;
12118e85fb3fSJohann Lombardi 	int rc, size;
12128e85fb3fSJohann Lombardi 
12138e85fb3fSJohann Lombardi 	s = kmalloc(sizeof(*s), GFP_KERNEL);
12148e85fb3fSJohann Lombardi 	if (s == NULL)
12158e85fb3fSJohann Lombardi 		return -ENOMEM;
12168e85fb3fSJohann Lombardi 	size = sizeof(struct transaction_stats_s);
12178e85fb3fSJohann Lombardi 	s->stats = kmalloc(size, GFP_KERNEL);
12188e85fb3fSJohann Lombardi 	if (s->stats == NULL) {
12198e85fb3fSJohann Lombardi 		kfree(s);
12208e85fb3fSJohann Lombardi 		return -ENOMEM;
12218e85fb3fSJohann Lombardi 	}
12228e85fb3fSJohann Lombardi 	spin_lock(&journal->j_history_lock);
12238e85fb3fSJohann Lombardi 	memcpy(s->stats, &journal->j_stats, size);
12248e85fb3fSJohann Lombardi 	s->journal = journal;
12258e85fb3fSJohann Lombardi 	spin_unlock(&journal->j_history_lock);
12268e85fb3fSJohann Lombardi 
12278e85fb3fSJohann Lombardi 	rc = seq_open(file, &jbd2_seq_info_ops);
12288e85fb3fSJohann Lombardi 	if (rc == 0) {
12298e85fb3fSJohann Lombardi 		struct seq_file *m = file->private_data;
12308e85fb3fSJohann Lombardi 		m->private = s;
12318e85fb3fSJohann Lombardi 	} else {
12328e85fb3fSJohann Lombardi 		kfree(s->stats);
12338e85fb3fSJohann Lombardi 		kfree(s);
12348e85fb3fSJohann Lombardi 	}
12358e85fb3fSJohann Lombardi 	return rc;
12368e85fb3fSJohann Lombardi 
12378e85fb3fSJohann Lombardi }
12388e85fb3fSJohann Lombardi 
jbd2_seq_info_release(struct inode * inode,struct file * file)12398e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file)
12408e85fb3fSJohann Lombardi {
12418e85fb3fSJohann Lombardi 	struct seq_file *seq = file->private_data;
12428e85fb3fSJohann Lombardi 	struct jbd2_stats_proc_session *s = seq->private;
12438e85fb3fSJohann Lombardi 	kfree(s->stats);
12448e85fb3fSJohann Lombardi 	kfree(s);
12458e85fb3fSJohann Lombardi 	return seq_release(inode, file);
12468e85fb3fSJohann Lombardi }
12478e85fb3fSJohann Lombardi 
124897a32539SAlexey Dobriyan static const struct proc_ops jbd2_info_proc_ops = {
124997a32539SAlexey Dobriyan 	.proc_open	= jbd2_seq_info_open,
125097a32539SAlexey Dobriyan 	.proc_read	= seq_read,
125197a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
125297a32539SAlexey Dobriyan 	.proc_release	= jbd2_seq_info_release,
12538e85fb3fSJohann Lombardi };
12548e85fb3fSJohann Lombardi 
12558e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats;
12568e85fb3fSJohann Lombardi 
jbd2_stats_proc_init(journal_t * journal)12578e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal)
12588e85fb3fSJohann Lombardi {
125905496769STheodore Ts'o 	journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
12608e85fb3fSJohann Lombardi 	if (journal->j_proc_entry) {
126179da3664SDenis V. Lunev 		proc_create_data("info", S_IRUGO, journal->j_proc_entry,
126297a32539SAlexey Dobriyan 				 &jbd2_info_proc_ops, journal);
12638e85fb3fSJohann Lombardi 	}
12648e85fb3fSJohann Lombardi }
12658e85fb3fSJohann Lombardi 
jbd2_stats_proc_exit(journal_t * journal)12668e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal)
12678e85fb3fSJohann Lombardi {
12688e85fb3fSJohann Lombardi 	remove_proc_entry("info", journal->j_proc_entry);
126905496769STheodore Ts'o 	remove_proc_entry(journal->j_devname, proc_jbd2_stats);
12708e85fb3fSJohann Lombardi }
12718e85fb3fSJohann Lombardi 
1272b90bfdf5SJan Kara /* Minimum size of descriptor tag */
jbd2_min_tag_size(void)1273b90bfdf5SJan Kara static int jbd2_min_tag_size(void)
1274b90bfdf5SJan Kara {
1275b90bfdf5SJan Kara 	/*
1276b90bfdf5SJan Kara 	 * Tag with 32-bit block numbers does not use last four bytes of the
1277b90bfdf5SJan Kara 	 * structure
1278b90bfdf5SJan Kara 	 */
1279b90bfdf5SJan Kara 	return sizeof(journal_block_tag_t) - 4;
1280b90bfdf5SJan Kara }
1281b90bfdf5SJan Kara 
12820705e8d1STheodore Ts'o /**
12830705e8d1STheodore Ts'o  * jbd2_journal_shrink_scan()
1284715a67f1SYang Li  * @shrink: shrinker to work on
1285715a67f1SYang Li  * @sc: reclaim request to process
12860705e8d1STheodore Ts'o  *
12870705e8d1STheodore Ts'o  * Scan the checkpointed buffer on the checkpoint list and release the
12880705e8d1STheodore Ts'o  * journal_head.
12890705e8d1STheodore Ts'o  */
jbd2_journal_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)12900705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink,
12910705e8d1STheodore Ts'o 					      struct shrink_control *sc)
12920705e8d1STheodore Ts'o {
12930705e8d1STheodore Ts'o 	journal_t *journal = container_of(shrink, journal_t, j_shrinker);
12940705e8d1STheodore Ts'o 	unsigned long nr_to_scan = sc->nr_to_scan;
12950705e8d1STheodore Ts'o 	unsigned long nr_shrunk;
12960705e8d1STheodore Ts'o 	unsigned long count;
12970705e8d1STheodore Ts'o 
12980705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
12990705e8d1STheodore Ts'o 	trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count);
13000705e8d1STheodore Ts'o 
13010705e8d1STheodore Ts'o 	nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan);
13020705e8d1STheodore Ts'o 
13030705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
13040705e8d1STheodore Ts'o 	trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count);
13050705e8d1STheodore Ts'o 
13060705e8d1STheodore Ts'o 	return nr_shrunk;
13070705e8d1STheodore Ts'o }
13080705e8d1STheodore Ts'o 
13090705e8d1STheodore Ts'o /**
13100705e8d1STheodore Ts'o  * jbd2_journal_shrink_count()
1311715a67f1SYang Li  * @shrink: shrinker to work on
1312715a67f1SYang Li  * @sc: reclaim request to process
13130705e8d1STheodore Ts'o  *
13140705e8d1STheodore Ts'o  * Count the number of checkpoint buffers on the checkpoint list.
13150705e8d1STheodore Ts'o  */
jbd2_journal_shrink_count(struct shrinker * shrink,struct shrink_control * sc)13160705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_count(struct shrinker *shrink,
13170705e8d1STheodore Ts'o 					       struct shrink_control *sc)
13180705e8d1STheodore Ts'o {
13190705e8d1STheodore Ts'o 	journal_t *journal = container_of(shrink, journal_t, j_shrinker);
13200705e8d1STheodore Ts'o 	unsigned long count;
13210705e8d1STheodore Ts'o 
13220705e8d1STheodore Ts'o 	count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
13230705e8d1STheodore Ts'o 	trace_jbd2_shrink_count(journal, sc->nr_to_scan, count);
13240705e8d1STheodore Ts'o 
13250705e8d1STheodore Ts'o 	return count;
13260705e8d1STheodore Ts'o }
13270705e8d1STheodore Ts'o 
1328470decc6SDave Kleikamp /*
132929a511e4SZhang Yi  * If the journal init or create aborts, we need to mark the journal
133029a511e4SZhang Yi  * superblock as being NULL to prevent the journal destroy from writing
133129a511e4SZhang Yi  * back a bogus superblock.
133229a511e4SZhang Yi  */
journal_fail_superblock(journal_t * journal)133329a511e4SZhang Yi static void journal_fail_superblock(journal_t *journal)
133429a511e4SZhang Yi {
133529a511e4SZhang Yi 	struct buffer_head *bh = journal->j_sb_buffer;
133629a511e4SZhang Yi 	brelse(bh);
133729a511e4SZhang Yi 	journal->j_sb_buffer = NULL;
133829a511e4SZhang Yi }
133929a511e4SZhang Yi 
134029a511e4SZhang Yi /*
1341054d9c8fSZhang Yi  * Check the superblock for a given journal, performing initial
134229a511e4SZhang Yi  * validation of the format.
134329a511e4SZhang Yi  */
journal_check_superblock(journal_t * journal)1344054d9c8fSZhang Yi static int journal_check_superblock(journal_t *journal)
134529a511e4SZhang Yi {
1346054d9c8fSZhang Yi 	journal_superblock_t *sb = journal->j_superblock;
13470dbc759aSZhang Yi 	int num_fc_blks;
1348054d9c8fSZhang Yi 	int err = -EINVAL;
134929a511e4SZhang Yi 
135029a511e4SZhang Yi 	if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
135129a511e4SZhang Yi 	    sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
135229a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1353054d9c8fSZhang Yi 		return err;
135429a511e4SZhang Yi 	}
135529a511e4SZhang Yi 
135629a511e4SZhang Yi 	if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 &&
135729a511e4SZhang Yi 	    be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) {
135829a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1359054d9c8fSZhang Yi 		return err;
136029a511e4SZhang Yi 	}
136129a511e4SZhang Yi 
136229a511e4SZhang Yi 	if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
136329a511e4SZhang Yi 		printk(KERN_WARNING "JBD2: journal file too short\n");
1364054d9c8fSZhang Yi 		return err;
136529a511e4SZhang Yi 	}
136629a511e4SZhang Yi 
136729a511e4SZhang Yi 	if (be32_to_cpu(sb->s_first) == 0 ||
136829a511e4SZhang Yi 	    be32_to_cpu(sb->s_first) >= journal->j_total_len) {
136929a511e4SZhang Yi 		printk(KERN_WARNING
137029a511e4SZhang Yi 			"JBD2: Invalid start block of journal: %u\n",
137129a511e4SZhang Yi 			be32_to_cpu(sb->s_first));
1372054d9c8fSZhang Yi 		return err;
137329a511e4SZhang Yi 	}
137429a511e4SZhang Yi 
1375e4adf8b8SZhang Yi 	/*
1376e4adf8b8SZhang Yi 	 * If this is a V2 superblock, then we have to check the
1377e4adf8b8SZhang Yi 	 * features flags on it.
1378e4adf8b8SZhang Yi 	 */
1379e4adf8b8SZhang Yi 	if (!jbd2_format_support_feature(journal))
1380e4adf8b8SZhang Yi 		return 0;
1381e4adf8b8SZhang Yi 
1382e4adf8b8SZhang Yi 	if ((sb->s_feature_ro_compat &
1383e4adf8b8SZhang Yi 			~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1384e4adf8b8SZhang Yi 	    (sb->s_feature_incompat &
1385e4adf8b8SZhang Yi 			~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1386e4adf8b8SZhang Yi 		printk(KERN_WARNING "JBD2: Unrecognised features on journal\n");
1387054d9c8fSZhang Yi 		return err;
1388e4adf8b8SZhang Yi 	}
1389e4adf8b8SZhang Yi 
13900dbc759aSZhang Yi 	num_fc_blks = jbd2_has_feature_fast_commit(journal) ?
13910dbc759aSZhang Yi 				jbd2_journal_get_num_fc_blks(sb) : 0;
13920dbc759aSZhang Yi 	if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS ||
13930dbc759aSZhang Yi 	    be32_to_cpu(sb->s_maxlen) - JBD2_MIN_JOURNAL_BLOCKS < num_fc_blks) {
13940dbc759aSZhang Yi 		printk(KERN_ERR "JBD2: journal file too short %u,%d\n",
13950dbc759aSZhang Yi 		       be32_to_cpu(sb->s_maxlen), num_fc_blks);
13960dbc759aSZhang Yi 		return err;
13970dbc759aSZhang Yi 	}
13980dbc759aSZhang Yi 
139929a511e4SZhang Yi 	if (jbd2_has_feature_csum2(journal) &&
140029a511e4SZhang Yi 	    jbd2_has_feature_csum3(journal)) {
140129a511e4SZhang Yi 		/* Can't have checksum v2 and v3 at the same time! */
140229a511e4SZhang Yi 		printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
140329a511e4SZhang Yi 		       "at the same time!\n");
1404054d9c8fSZhang Yi 		return err;
140529a511e4SZhang Yi 	}
140629a511e4SZhang Yi 
140729a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3_feature(journal) &&
140829a511e4SZhang Yi 	    jbd2_has_feature_checksum(journal)) {
140929a511e4SZhang Yi 		/* Can't have checksum v1 and v2 on at the same time! */
141029a511e4SZhang Yi 		printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
141129a511e4SZhang Yi 		       "at the same time!\n");
1412054d9c8fSZhang Yi 		return err;
141329a511e4SZhang Yi 	}
141429a511e4SZhang Yi 
141518dad509SZhang Yi 	/* Load the checksum driver */
141618dad509SZhang Yi 	if (jbd2_journal_has_csum_v2or3_feature(journal)) {
141718dad509SZhang Yi 		if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
141829a511e4SZhang Yi 			printk(KERN_ERR "JBD2: Unknown checksum type\n");
1419054d9c8fSZhang Yi 			return err;
142029a511e4SZhang Yi 		}
142129a511e4SZhang Yi 
142229a511e4SZhang Yi 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
142329a511e4SZhang Yi 		if (IS_ERR(journal->j_chksum_driver)) {
142429a511e4SZhang Yi 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
142529a511e4SZhang Yi 			err = PTR_ERR(journal->j_chksum_driver);
142629a511e4SZhang Yi 			journal->j_chksum_driver = NULL;
1427054d9c8fSZhang Yi 			return err;
142829a511e4SZhang Yi 		}
142929a511e4SZhang Yi 		/* Check superblock checksum */
143029a511e4SZhang Yi 		if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
143129a511e4SZhang Yi 			printk(KERN_ERR "JBD2: journal checksum error\n");
143229a511e4SZhang Yi 			err = -EFSBADCRC;
143329a511e4SZhang Yi 			return err;
143429a511e4SZhang Yi 		}
1435054d9c8fSZhang Yi 	}
1436054d9c8fSZhang Yi 
1437054d9c8fSZhang Yi 	return 0;
1438054d9c8fSZhang Yi }
143929a511e4SZhang Yi 
journal_revoke_records_per_block(journal_t * journal)144029a511e4SZhang Yi static int journal_revoke_records_per_block(journal_t *journal)
144129a511e4SZhang Yi {
144229a511e4SZhang Yi 	int record_size;
144329a511e4SZhang Yi 	int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
144429a511e4SZhang Yi 
144529a511e4SZhang Yi 	if (jbd2_has_feature_64bit(journal))
144629a511e4SZhang Yi 		record_size = 8;
144729a511e4SZhang Yi 	else
144829a511e4SZhang Yi 		record_size = 4;
144929a511e4SZhang Yi 
145029a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3(journal))
145129a511e4SZhang Yi 		space -= sizeof(struct jbd2_journal_block_tail);
145229a511e4SZhang Yi 	return space / record_size;
145329a511e4SZhang Yi }
145429a511e4SZhang Yi 
jbd2_journal_get_max_txn_bufs(journal_t * journal)1455b81c3758SJan Kara static int jbd2_journal_get_max_txn_bufs(journal_t *journal)
1456b81c3758SJan Kara {
1457b81c3758SJan Kara 	return (journal->j_total_len - journal->j_fc_wbufsize) / 4;
1458b81c3758SJan Kara }
1459b81c3758SJan Kara 
1460b81c3758SJan Kara /*
1461b81c3758SJan Kara  * Base amount of descriptor blocks we reserve for each transaction.
1462b81c3758SJan Kara  */
jbd2_descriptor_blocks_per_trans(journal_t * journal)1463b81c3758SJan Kara static int jbd2_descriptor_blocks_per_trans(journal_t *journal)
1464b81c3758SJan Kara {
1465b81c3758SJan Kara 	int tag_space = journal->j_blocksize - sizeof(journal_header_t);
1466b81c3758SJan Kara 	int tags_per_block;
1467b81c3758SJan Kara 
1468b81c3758SJan Kara 	/* Subtract UUID */
1469b81c3758SJan Kara 	tag_space -= 16;
1470b81c3758SJan Kara 	if (jbd2_journal_has_csum_v2or3(journal))
1471b81c3758SJan Kara 		tag_space -= sizeof(struct jbd2_journal_block_tail);
1472b81c3758SJan Kara 	/* Commit code leaves a slack space of 16 bytes at the end of block */
1473b81c3758SJan Kara 	tags_per_block = (tag_space - 16) / journal_tag_bytes(journal);
1474b81c3758SJan Kara 	/*
1475b81c3758SJan Kara 	 * Revoke descriptors are accounted separately so we need to reserve
1476b81c3758SJan Kara 	 * space for commit block and normal transaction descriptor blocks.
1477b81c3758SJan Kara 	 */
1478b81c3758SJan Kara 	return 1 + DIV_ROUND_UP(jbd2_journal_get_max_txn_bufs(journal),
1479b81c3758SJan Kara 				tags_per_block);
1480b81c3758SJan Kara }
1481b81c3758SJan Kara 
1482b81c3758SJan Kara /*
1483b81c3758SJan Kara  * Initialize number of blocks each transaction reserves for its bookkeeping
1484b81c3758SJan Kara  * and maximum number of blocks a transaction can use. This needs to be called
1485b81c3758SJan Kara  * after the journal size and the fastcommit area size are initialized.
1486b81c3758SJan Kara  */
jbd2_journal_init_transaction_limits(journal_t * journal)1487b81c3758SJan Kara static void jbd2_journal_init_transaction_limits(journal_t *journal)
1488b81c3758SJan Kara {
1489b81c3758SJan Kara 	journal->j_revoke_records_per_block =
1490b81c3758SJan Kara 				journal_revoke_records_per_block(journal);
1491b81c3758SJan Kara 	journal->j_transaction_overhead_buffers =
1492b81c3758SJan Kara 				jbd2_descriptor_blocks_per_trans(journal);
1493b81c3758SJan Kara 	journal->j_max_transaction_buffers =
1494b81c3758SJan Kara 				jbd2_journal_get_max_txn_bufs(journal);
1495b81c3758SJan Kara }
1496b81c3758SJan Kara 
149729a511e4SZhang Yi /*
149829a511e4SZhang Yi  * Load the on-disk journal superblock and read the key fields into the
149929a511e4SZhang Yi  * journal_t.
150029a511e4SZhang Yi  */
journal_load_superblock(journal_t * journal)1501054d9c8fSZhang Yi static int journal_load_superblock(journal_t *journal)
150229a511e4SZhang Yi {
150329a511e4SZhang Yi 	int err;
1504054d9c8fSZhang Yi 	struct buffer_head *bh;
150529a511e4SZhang Yi 	journal_superblock_t *sb;
150629a511e4SZhang Yi 
1507054d9c8fSZhang Yi 	bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset,
1508054d9c8fSZhang Yi 			      journal->j_blocksize);
1509054d9c8fSZhang Yi 	if (bh)
1510054d9c8fSZhang Yi 		err = bh_read(bh, 0);
1511054d9c8fSZhang Yi 	if (!bh || err < 0) {
1512054d9c8fSZhang Yi 		pr_err("%s: Cannot read journal superblock\n", __func__);
1513054d9c8fSZhang Yi 		brelse(bh);
1514054d9c8fSZhang Yi 		return -EIO;
1515054d9c8fSZhang Yi 	}
151629a511e4SZhang Yi 
1517054d9c8fSZhang Yi 	journal->j_sb_buffer = bh;
1518054d9c8fSZhang Yi 	sb = (journal_superblock_t *)bh->b_data;
1519054d9c8fSZhang Yi 	journal->j_superblock = sb;
1520054d9c8fSZhang Yi 	err = journal_check_superblock(journal);
1521054d9c8fSZhang Yi 	if (err) {
1522054d9c8fSZhang Yi 		journal_fail_superblock(journal);
1523054d9c8fSZhang Yi 		return err;
1524054d9c8fSZhang Yi 	}
152529a511e4SZhang Yi 
152629a511e4SZhang Yi 	journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
152729a511e4SZhang Yi 	journal->j_tail = be32_to_cpu(sb->s_start);
152829a511e4SZhang Yi 	journal->j_first = be32_to_cpu(sb->s_first);
152929a511e4SZhang Yi 	journal->j_errno = be32_to_cpu(sb->s_errno);
153029a511e4SZhang Yi 	journal->j_last = be32_to_cpu(sb->s_maxlen);
153129a511e4SZhang Yi 
153229a511e4SZhang Yi 	if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
153329a511e4SZhang Yi 		journal->j_total_len = be32_to_cpu(sb->s_maxlen);
153429a511e4SZhang Yi 	/* Precompute checksum seed for all metadata */
153529a511e4SZhang Yi 	if (jbd2_journal_has_csum_v2or3(journal))
153629a511e4SZhang Yi 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
153729a511e4SZhang Yi 						   sizeof(sb->s_uuid));
1538b81c3758SJan Kara 	/* After journal features are set, we can compute transaction limits */
1539b81c3758SJan Kara 	jbd2_journal_init_transaction_limits(journal);
154029a511e4SZhang Yi 
154129a511e4SZhang Yi 	if (jbd2_has_feature_fast_commit(journal)) {
154229a511e4SZhang Yi 		journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
15430dbc759aSZhang Yi 		journal->j_last = journal->j_fc_last -
15440dbc759aSZhang Yi 				  jbd2_journal_get_num_fc_blks(sb);
154529a511e4SZhang Yi 		journal->j_fc_first = journal->j_last + 1;
154629a511e4SZhang Yi 		journal->j_fc_off = 0;
154729a511e4SZhang Yi 	}
154829a511e4SZhang Yi 
154929a511e4SZhang Yi 	return 0;
155029a511e4SZhang Yi }
155129a511e4SZhang Yi 
155229a511e4SZhang Yi 
155329a511e4SZhang Yi /*
1554470decc6SDave Kleikamp  * Management for journal control blocks: functions to create and
1555470decc6SDave Kleikamp  * destroy journal_t structures, and to initialise and read existing
1556470decc6SDave Kleikamp  * journal blocks from disk.  */
1557470decc6SDave Kleikamp 
1558470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory.  We initialise
1559470decc6SDave Kleikamp  * very few fields yet: that has to wait until we have created the
1560470decc6SDave Kleikamp  * journal structures from from scratch, or loaded them from disk. */
1561470decc6SDave Kleikamp 
journal_init_common(struct block_device * bdev,struct block_device * fs_dev,unsigned long long start,int len,int blocksize)1562f0c9fd54SGeliang Tang static journal_t *journal_init_common(struct block_device *bdev,
1563f0c9fd54SGeliang Tang 			struct block_device *fs_dev,
1564f0c9fd54SGeliang Tang 			unsigned long long start, int len, int blocksize)
1565470decc6SDave Kleikamp {
1566ab714affSJan Kara 	static struct lock_class_key jbd2_trans_commit_key;
1567470decc6SDave Kleikamp 	journal_t *journal;
1568470decc6SDave Kleikamp 	int err;
1569f0c9fd54SGeliang Tang 	int n;
1570470decc6SDave Kleikamp 
15713ebfdf88SAndrew Morton 	journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1572470decc6SDave Kleikamp 	if (!journal)
15738e6cf5fbSZhang Yi 		return ERR_PTR(-ENOMEM);
1574470decc6SDave Kleikamp 
157549887e47SZhang Yi 	journal->j_blocksize = blocksize;
157649887e47SZhang Yi 	journal->j_dev = bdev;
157749887e47SZhang Yi 	journal->j_fs_dev = fs_dev;
157849887e47SZhang Yi 	journal->j_blk_offset = start;
157949887e47SZhang Yi 	journal->j_total_len = len;
158049887e47SZhang Yi 
158149887e47SZhang Yi 	err = journal_load_superblock(journal);
158249887e47SZhang Yi 	if (err)
158349887e47SZhang Yi 		goto err_cleanup;
1584470decc6SDave Kleikamp 
1585470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_transaction_locked);
1586470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_done_commit);
1587470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_commit);
1588470decc6SDave Kleikamp 	init_waitqueue_head(&journal->j_wait_updates);
15898f7d89f3SJan Kara 	init_waitqueue_head(&journal->j_wait_reserved);
1590ff780b91SHarshad Shirwadkar 	init_waitqueue_head(&journal->j_fc_wait);
15917b97d868Szhangyi (F) 	mutex_init(&journal->j_abort_mutex);
1592470decc6SDave Kleikamp 	mutex_init(&journal->j_barrier);
1593470decc6SDave Kleikamp 	mutex_init(&journal->j_checkpoint_mutex);
1594470decc6SDave Kleikamp 	spin_lock_init(&journal->j_revoke_lock);
1595470decc6SDave Kleikamp 	spin_lock_init(&journal->j_list_lock);
159649887e47SZhang Yi 	spin_lock_init(&journal->j_history_lock);
1597a931da6aSTheodore Ts'o 	rwlock_init(&journal->j_state_lock);
1598470decc6SDave Kleikamp 
1599cd02ff0bSMingming Cao 	journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
160030773840STheodore Ts'o 	journal->j_min_batch_time = 0;
160130773840STheodore Ts'o 	journal->j_max_batch_time = 15000; /* 15ms */
16028f7d89f3SJan Kara 	atomic_set(&journal->j_reserved_credits, 0);
160349887e47SZhang Yi 	lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle",
160449887e47SZhang Yi 			 &jbd2_trans_commit_key, 0);
1605470decc6SDave Kleikamp 
1606470decc6SDave Kleikamp 	/* The journal is marked for error until we succeed with recovery! */
1607f7f4bccbSMingming Cao 	journal->j_flags = JBD2_ABORT;
1608470decc6SDave Kleikamp 
1609470decc6SDave Kleikamp 	/* Set up a default-sized revoke table for the new mount. */
1610f7f4bccbSMingming Cao 	err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1611cd9cb405SEric Biggers 	if (err)
1612cd9cb405SEric Biggers 		goto err_cleanup;
16138e85fb3fSJohann Lombardi 
161449887e47SZhang Yi 	/*
161549887e47SZhang Yi 	 * journal descriptor can store up to n blocks, we need enough
161649887e47SZhang Yi 	 * buffers to write out full descriptor block.
161749887e47SZhang Yi 	 */
16188e6cf5fbSZhang Yi 	err = -ENOMEM;
1619b90bfdf5SJan Kara 	n = journal->j_blocksize / jbd2_min_tag_size();
1620f0c9fd54SGeliang Tang 	journal->j_wbufsize = n;
1621a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbuf = NULL;
1622f0c9fd54SGeliang Tang 	journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
1623f0c9fd54SGeliang Tang 					GFP_KERNEL);
1624cd9cb405SEric Biggers 	if (!journal->j_wbuf)
1625cd9cb405SEric Biggers 		goto err_cleanup;
1626f0c9fd54SGeliang Tang 
162749887e47SZhang Yi 	err = percpu_counter_init(&journal->j_checkpoint_jh_count, 0,
162849887e47SZhang Yi 				  GFP_KERNEL);
1629c3071308SZhang Yi 	if (err)
1630cd9cb405SEric Biggers 		goto err_cleanup;
1631f0c9fd54SGeliang Tang 
16320705e8d1STheodore Ts'o 	journal->j_shrink_transaction = NULL;
16330705e8d1STheodore Ts'o 	journal->j_shrinker.scan_objects = jbd2_journal_shrink_scan;
16340705e8d1STheodore Ts'o 	journal->j_shrinker.count_objects = jbd2_journal_shrink_count;
16350705e8d1STheodore Ts'o 	journal->j_shrinker.seeks = DEFAULT_SEEKS;
16360705e8d1STheodore Ts'o 	journal->j_shrinker.batch = journal->j_max_transaction_buffers;
163749887e47SZhang Yi 	err = register_shrinker(&journal->j_shrinker, "jbd2-journal:(%u:%u)",
163849887e47SZhang Yi 				MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
163949887e47SZhang Yi 	if (err)
16400705e8d1STheodore Ts'o 		goto err_cleanup;
16410705e8d1STheodore Ts'o 
1642470decc6SDave Kleikamp 	return journal;
1643cd9cb405SEric Biggers 
1644cd9cb405SEric Biggers err_cleanup:
164549887e47SZhang Yi 	percpu_counter_destroy(&journal->j_checkpoint_jh_count);
16461bb0763fSLi Zetao 	if (journal->j_chksum_driver)
16471bb0763fSLi Zetao 		crypto_free_shash(journal->j_chksum_driver);
1648cd9cb405SEric Biggers 	kfree(journal->j_wbuf);
1649cd9cb405SEric Biggers 	jbd2_journal_destroy_revoke(journal);
165049887e47SZhang Yi 	journal_fail_superblock(journal);
1651cd9cb405SEric Biggers 	kfree(journal);
16528e6cf5fbSZhang Yi 	return ERR_PTR(err);
1653470decc6SDave Kleikamp }
1654470decc6SDave Kleikamp 
1655f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1656470decc6SDave Kleikamp  *
1657470decc6SDave Kleikamp  * Create a journal structure assigned some fixed set of disk blocks to
1658470decc6SDave Kleikamp  * the journal.  We don't actually touch those disk blocks yet, but we
1659470decc6SDave Kleikamp  * need to set up all of the mapping information to tell the journaling
1660470decc6SDave Kleikamp  * system where the journal blocks are.
1661470decc6SDave Kleikamp  *
1662470decc6SDave Kleikamp  */
1663470decc6SDave Kleikamp 
1664470decc6SDave Kleikamp /**
16655648ba5bSRandy Dunlap  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1666470decc6SDave Kleikamp  *  @bdev: Block device on which to create the journal
1667470decc6SDave Kleikamp  *  @fs_dev: Device which hold journalled filesystem for this journal.
1668470decc6SDave Kleikamp  *  @start: Block nr Start of journal.
1669470decc6SDave Kleikamp  *  @len:  Length of the journal in blocks.
1670470decc6SDave Kleikamp  *  @blocksize: blocksize of journalling device
16715648ba5bSRandy Dunlap  *
16725648ba5bSRandy Dunlap  *  Returns: a newly created journal_t *
1673470decc6SDave Kleikamp  *
1674f7f4bccbSMingming Cao  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1675470decc6SDave Kleikamp  *  range of blocks on an arbitrary block device.
1676470decc6SDave Kleikamp  *
1677470decc6SDave Kleikamp  */
jbd2_journal_init_dev(struct block_device * bdev,struct block_device * fs_dev,unsigned long long start,int len,int blocksize)1678f7f4bccbSMingming Cao journal_t *jbd2_journal_init_dev(struct block_device *bdev,
1679470decc6SDave Kleikamp 			struct block_device *fs_dev,
168018eba7aaSMingming Cao 			unsigned long long start, int len, int blocksize)
1681470decc6SDave Kleikamp {
1682f0c9fd54SGeliang Tang 	journal_t *journal;
1683470decc6SDave Kleikamp 
1684f0c9fd54SGeliang Tang 	journal = journal_init_common(bdev, fs_dev, start, len, blocksize);
16858e6cf5fbSZhang Yi 	if (IS_ERR(journal))
16868e6cf5fbSZhang Yi 		return ERR_CAST(journal);
1687470decc6SDave Kleikamp 
1688900d156bSChristoph Hellwig 	snprintf(journal->j_devname, sizeof(journal->j_devname),
1689900d156bSChristoph Hellwig 		 "%pg", journal->j_dev);
169081ae394bSRasmus Villemoes 	strreplace(journal->j_devname, '/', '!');
16914b905671SJan Kara 	jbd2_stats_proc_init(journal);
16924b905671SJan Kara 
1693470decc6SDave Kleikamp 	return journal;
1694470decc6SDave Kleikamp }
1695470decc6SDave Kleikamp 
1696470decc6SDave Kleikamp /**
1697f7f4bccbSMingming Cao  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1698470decc6SDave Kleikamp  *  @inode: An inode to create the journal in
1699470decc6SDave Kleikamp  *
1700f7f4bccbSMingming Cao  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1701470decc6SDave Kleikamp  * the journal.  The inode must exist already, must support bmap() and
1702470decc6SDave Kleikamp  * must have all data blocks preallocated.
1703470decc6SDave Kleikamp  */
jbd2_journal_init_inode(struct inode * inode)1704f7f4bccbSMingming Cao journal_t *jbd2_journal_init_inode(struct inode *inode)
1705470decc6SDave Kleikamp {
1706f0c9fd54SGeliang Tang 	journal_t *journal;
170730460e1eSCarlos Maiolino 	sector_t blocknr;
170830460e1eSCarlos Maiolino 	int err = 0;
1709470decc6SDave Kleikamp 
171030460e1eSCarlos Maiolino 	blocknr = 0;
171130460e1eSCarlos Maiolino 	err = bmap(inode, &blocknr);
171230460e1eSCarlos Maiolino 	if (err || !blocknr) {
17138e6cf5fbSZhang Yi 		pr_err("%s: Cannot locate journal superblock\n", __func__);
17148e6cf5fbSZhang Yi 		return err ? ERR_PTR(err) : ERR_PTR(-EINVAL);
1715f0c9fd54SGeliang Tang 	}
1716f0c9fd54SGeliang Tang 
1717cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n",
1718f0c9fd54SGeliang Tang 		  inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size,
1719f0c9fd54SGeliang Tang 		  inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1720f0c9fd54SGeliang Tang 
1721f0c9fd54SGeliang Tang 	journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev,
1722f0c9fd54SGeliang Tang 			blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits,
1723f0c9fd54SGeliang Tang 			inode->i_sb->s_blocksize);
17248e6cf5fbSZhang Yi 	if (IS_ERR(journal))
17258e6cf5fbSZhang Yi 		return ERR_CAST(journal);
1726470decc6SDave Kleikamp 
1727470decc6SDave Kleikamp 	journal->j_inode = inode;
1728900d156bSChristoph Hellwig 	snprintf(journal->j_devname, sizeof(journal->j_devname),
17297afb6d8fSAndy Shevchenko 		 "%pg-%lu", journal->j_dev, journal->j_inode->i_ino);
17307afb6d8fSAndy Shevchenko 	strreplace(journal->j_devname, '/', '!');
17318e85fb3fSJohann Lombardi 	jbd2_stats_proc_init(journal);
1732470decc6SDave Kleikamp 
1733470decc6SDave Kleikamp 	return journal;
1734470decc6SDave Kleikamp }
1735470decc6SDave Kleikamp 
1736470decc6SDave Kleikamp /*
1737470decc6SDave Kleikamp  * Given a journal_t structure, initialise the various fields for
1738470decc6SDave Kleikamp  * startup of a new journaling session.  We use this both when creating
1739470decc6SDave Kleikamp  * a journal, and after recovering an old journal to reset it for
1740470decc6SDave Kleikamp  * subsequent use.
1741470decc6SDave Kleikamp  */
1742470decc6SDave Kleikamp 
journal_reset(journal_t * journal)1743470decc6SDave Kleikamp static int journal_reset(journal_t *journal)
1744470decc6SDave Kleikamp {
1745470decc6SDave Kleikamp 	journal_superblock_t *sb = journal->j_superblock;
174618eba7aaSMingming Cao 	unsigned long long first, last;
1747470decc6SDave Kleikamp 
1748470decc6SDave Kleikamp 	first = be32_to_cpu(sb->s_first);
1749470decc6SDave Kleikamp 	last = be32_to_cpu(sb->s_maxlen);
1750f6f50e28SJan Kara 	if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1751f2a44523SEryu Guan 		printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1752f6f50e28SJan Kara 		       first, last);
1753f6f50e28SJan Kara 		journal_fail_superblock(journal);
1754f6f50e28SJan Kara 		return -EINVAL;
1755f6f50e28SJan Kara 	}
1756470decc6SDave Kleikamp 
1757470decc6SDave Kleikamp 	journal->j_first = first;
17586866d7b3SHarshad Shirwadkar 	journal->j_last = last;
17596866d7b3SHarshad Shirwadkar 
1760c7fc6055SZhang Yi 	if (journal->j_head != 0 && journal->j_flags & JBD2_CYCLE_RECORD) {
1761c7fc6055SZhang Yi 		/*
1762c7fc6055SZhang Yi 		 * Disable the cycled recording mode if the journal head block
1763c7fc6055SZhang Yi 		 * number is not correct.
1764c7fc6055SZhang Yi 		 */
1765c7fc6055SZhang Yi 		if (journal->j_head < first || journal->j_head >= last) {
1766c7fc6055SZhang Yi 			printk(KERN_WARNING "JBD2: Incorrect Journal head block %lu, "
1767c7fc6055SZhang Yi 			       "disable journal_cycle_record\n",
1768c7fc6055SZhang Yi 			       journal->j_head);
17696866d7b3SHarshad Shirwadkar 			journal->j_head = journal->j_first;
1770c7fc6055SZhang Yi 		}
1771c7fc6055SZhang Yi 	} else {
1772c7fc6055SZhang Yi 		journal->j_head = journal->j_first;
1773c7fc6055SZhang Yi 	}
1774c7fc6055SZhang Yi 	journal->j_tail = journal->j_head;
17756866d7b3SHarshad Shirwadkar 	journal->j_free = journal->j_last - journal->j_first;
1776470decc6SDave Kleikamp 
1777470decc6SDave Kleikamp 	journal->j_tail_sequence = journal->j_transaction_sequence;
1778470decc6SDave Kleikamp 	journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1779470decc6SDave Kleikamp 	journal->j_commit_request = journal->j_commit_sequence;
1780470decc6SDave Kleikamp 
1781470decc6SDave Kleikamp 	/*
1782a1e5e465SHarshad Shirwadkar 	 * Now that journal recovery is done, turn fast commits off here. This
1783a1e5e465SHarshad Shirwadkar 	 * way, if fast commit was enabled before the crash but if now FS has
1784a1e5e465SHarshad Shirwadkar 	 * disabled it, we don't enable fast commits.
1785a1e5e465SHarshad Shirwadkar 	 */
1786a1e5e465SHarshad Shirwadkar 	jbd2_clear_feature_fast_commit(journal);
1787a1e5e465SHarshad Shirwadkar 
1788a1e5e465SHarshad Shirwadkar 	/*
1789470decc6SDave Kleikamp 	 * As a special case, if the on-disk copy is already marked as needing
179024bcc89cSJan Kara 	 * no recovery (s_start == 0), then we can safely defer the superblock
179124bcc89cSJan Kara 	 * update until the next commit by setting JBD2_FLUSHED.  This avoids
1792470decc6SDave Kleikamp 	 * attempting a write to a potential-readonly device.
1793470decc6SDave Kleikamp 	 */
179424bcc89cSJan Kara 	if (sb->s_start == 0) {
1795cb3b3bf2SJan Kara 		jbd2_debug(1, "JBD2: Skipping superblock update on recovered sb "
17967821ce41SGaowei Pu 			"(start %ld, seq %u, errno %d)\n",
1797470decc6SDave Kleikamp 			journal->j_tail, journal->j_tail_sequence,
1798470decc6SDave Kleikamp 			journal->j_errno);
179924bcc89cSJan Kara 		journal->j_flags |= JBD2_FLUSHED;
180024bcc89cSJan Kara 	} else {
1801a78bb11dSJan Kara 		/* Lock here to make assertions happy... */
18026fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
180379feb521SJan Kara 		/*
180470fd7614SChristoph Hellwig 		 * Update log tail information. We use REQ_FUA since new
180579feb521SJan Kara 		 * transaction will start reusing journal space and so we
180679feb521SJan Kara 		 * must make sure information about current log tail is on
180779feb521SJan Kara 		 * disk before that.
180879feb521SJan Kara 		 */
180979feb521SJan Kara 		jbd2_journal_update_sb_log_tail(journal,
181079feb521SJan Kara 						journal->j_tail_sequence,
18115c480a69SZhang Yi 						journal->j_tail, REQ_FUA);
1812a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
181324bcc89cSJan Kara 	}
181424bcc89cSJan Kara 	return jbd2_journal_start_thread(journal);
1815470decc6SDave Kleikamp }
1816470decc6SDave Kleikamp 
1817538bcaa6STheodore Ts'o /*
1818538bcaa6STheodore Ts'o  * This function expects that the caller will have locked the journal
1819538bcaa6STheodore Ts'o  * buffer head, and will return with it unlocked
1820538bcaa6STheodore Ts'o  */
jbd2_write_superblock(journal_t * journal,blk_opf_t write_flags)18216669797bSBart Van Assche static int jbd2_write_superblock(journal_t *journal, blk_opf_t write_flags)
182224bcc89cSJan Kara {
182324bcc89cSJan Kara 	struct buffer_head *bh = journal->j_sb_buffer;
1824fe52d17cSTheodore Ts'o 	journal_superblock_t *sb = journal->j_superblock;
1825f3ed5df3SRitesh Harjani (IBM) 	int ret = 0;
182624bcc89cSJan Kara 
1827742b06b5SJiufei Xue 	/* Buffer got discarded which means block device got invalidated */
1828ef3f5830Szhangyi (F) 	if (!buffer_mapped(bh)) {
1829ef3f5830Szhangyi (F) 		unlock_buffer(bh);
1830742b06b5SJiufei Xue 		return -EIO;
1831ef3f5830Szhangyi (F) 	}
1832742b06b5SJiufei Xue 
18335c480a69SZhang Yi 	/*
18345c480a69SZhang Yi 	 * Always set high priority flags to exempt from block layer's
18355c480a69SZhang Yi 	 * QOS policies, e.g. writeback throttle.
18365c480a69SZhang Yi 	 */
18375c480a69SZhang Yi 	write_flags |= JBD2_JOURNAL_REQ_FLAGS;
183879feb521SJan Kara 	if (!(journal->j_flags & JBD2_BARRIER))
183928a8f0d3SMike Christie 		write_flags &= ~(REQ_FUA | REQ_PREFLUSH);
184064596560SZhang Yi 
184164596560SZhang Yi 	trace_jbd2_write_superblock(journal, write_flags);
184264596560SZhang Yi 
1843914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1844914258bfSTheodore Ts'o 		/*
1845914258bfSTheodore Ts'o 		 * Oh, dear.  A previous attempt to write the journal
1846914258bfSTheodore Ts'o 		 * superblock failed.  This could happen because the
1847914258bfSTheodore Ts'o 		 * USB device was yanked out.  Or it could happen to
1848914258bfSTheodore Ts'o 		 * be a transient write error and maybe the block will
1849914258bfSTheodore Ts'o 		 * be remapped.  Nothing we can do but to retry the
1850914258bfSTheodore Ts'o 		 * write and hope for the best.
1851914258bfSTheodore Ts'o 		 */
1852914258bfSTheodore Ts'o 		printk(KERN_ERR "JBD2: previous I/O error detected "
1853914258bfSTheodore Ts'o 		       "for journal superblock update for %s.\n",
1854914258bfSTheodore Ts'o 		       journal->j_devname);
1855914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1856914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
1857914258bfSTheodore Ts'o 	}
1858a58ca992STheodore Ts'o 	if (jbd2_journal_has_csum_v2or3(journal))
1859a58ca992STheodore Ts'o 		sb->s_checksum = jbd2_superblock_csum(journal, sb);
186079feb521SJan Kara 	get_bh(bh);
186179feb521SJan Kara 	bh->b_end_io = end_buffer_write_sync;
1862f3ed5df3SRitesh Harjani (IBM) 	submit_bh(REQ_OP_WRITE | write_flags, bh);
186379feb521SJan Kara 	wait_on_buffer(bh);
1864914258bfSTheodore Ts'o 	if (buffer_write_io_error(bh)) {
1865914258bfSTheodore Ts'o 		clear_buffer_write_io_error(bh);
1866914258bfSTheodore Ts'o 		set_buffer_uptodate(bh);
186779feb521SJan Kara 		ret = -EIO;
186879feb521SJan Kara 	}
186979feb521SJan Kara 	if (ret) {
1870f3ed5df3SRitesh Harjani (IBM) 		printk(KERN_ERR "JBD2: I/O error when updating journal superblock for %s.\n",
187179feb521SJan Kara 				journal->j_devname);
18727b97d868Szhangyi (F) 		if (!is_journal_aborted(journal))
18736f6a6fdaSJoseph Qi 			jbd2_journal_abort(journal, ret);
1874914258bfSTheodore Ts'o 	}
18756f6a6fdaSJoseph Qi 
18766f6a6fdaSJoseph Qi 	return ret;
187724bcc89cSJan Kara }
1878470decc6SDave Kleikamp 
187924bcc89cSJan Kara /**
188024bcc89cSJan Kara  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
188124bcc89cSJan Kara  * @journal: The journal to update.
188279feb521SJan Kara  * @tail_tid: TID of the new transaction at the tail of the log
188379feb521SJan Kara  * @tail_block: The first block of the transaction at the tail of the log
18846669797bSBart Van Assche  * @write_flags: Flags for the journal sb write operation
188524bcc89cSJan Kara  *
188624bcc89cSJan Kara  * Update a journal's superblock information about log tail and write it to
188724bcc89cSJan Kara  * disk, waiting for the IO to complete.
188824bcc89cSJan Kara  */
jbd2_journal_update_sb_log_tail(journal_t * journal,tid_t tail_tid,unsigned long tail_block,blk_opf_t write_flags)18896f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
18906669797bSBart Van Assche 				    unsigned long tail_block,
18916669797bSBart Van Assche 				    blk_opf_t write_flags)
189224bcc89cSJan Kara {
189324bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
18946f6a6fdaSJoseph Qi 	int ret;
18952201c590SSeiji Aguchi 
189685e0c4e8STheodore Ts'o 	if (is_journal_aborted(journal))
189785e0c4e8STheodore Ts'o 		return -EIO;
1898fcf37549SZhang Yi 	if (test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) {
1899fcf37549SZhang Yi 		jbd2_journal_abort(journal, -EIO);
1900fcf37549SZhang Yi 		return -EIO;
1901fcf37549SZhang Yi 	}
190285e0c4e8STheodore Ts'o 
1903a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1904cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
190579feb521SJan Kara 		  tail_block, tail_tid);
1906470decc6SDave Kleikamp 
1907538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
190879feb521SJan Kara 	sb->s_sequence = cpu_to_be32(tail_tid);
190979feb521SJan Kara 	sb->s_start    = cpu_to_be32(tail_block);
191024bcc89cSJan Kara 
19116669797bSBart Van Assche 	ret = jbd2_write_superblock(journal, write_flags);
19126f6a6fdaSJoseph Qi 	if (ret)
19136f6a6fdaSJoseph Qi 		goto out;
191424bcc89cSJan Kara 
191524bcc89cSJan Kara 	/* Log is no longer empty */
1916a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
191724bcc89cSJan Kara 	WARN_ON(!sb->s_sequence);
1918f7f4bccbSMingming Cao 	journal->j_flags &= ~JBD2_FLUSHED;
191924bcc89cSJan Kara 	write_unlock(&journal->j_state_lock);
19206f6a6fdaSJoseph Qi 
19216f6a6fdaSJoseph Qi out:
19226f6a6fdaSJoseph Qi 	return ret;
192324bcc89cSJan Kara }
192424bcc89cSJan Kara 
192524bcc89cSJan Kara /**
192624bcc89cSJan Kara  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
192724bcc89cSJan Kara  * @journal: The journal to update.
19286669797bSBart Van Assche  * @write_flags: Flags for the journal sb write operation
192924bcc89cSJan Kara  *
193024bcc89cSJan Kara  * Update a journal's dynamic superblock fields to show that journal is empty.
193124bcc89cSJan Kara  * Write updated superblock to disk waiting for IO to complete.
193224bcc89cSJan Kara  */
jbd2_mark_journal_empty(journal_t * journal,blk_opf_t write_flags)19336669797bSBart Van Assche static void jbd2_mark_journal_empty(journal_t *journal, blk_opf_t write_flags)
193424bcc89cSJan Kara {
193524bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
1936ff780b91SHarshad Shirwadkar 	bool had_fast_commit = false;
193724bcc89cSJan Kara 
1938a78bb11dSJan Kara 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1939538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
1940538bcaa6STheodore Ts'o 	if (sb->s_start == 0) {		/* Is it already empty? */
1941538bcaa6STheodore Ts'o 		unlock_buffer(journal->j_sb_buffer);
1942eeecef0aSEric Sandeen 		return;
1943eeecef0aSEric Sandeen 	}
1944538bcaa6STheodore Ts'o 
1945cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: Marking journal as empty (seq %u)\n",
194624bcc89cSJan Kara 		  journal->j_tail_sequence);
1947470decc6SDave Kleikamp 
1948470decc6SDave Kleikamp 	sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
194924bcc89cSJan Kara 	sb->s_start    = cpu_to_be32(0);
1950c7fc6055SZhang Yi 	sb->s_head     = cpu_to_be32(journal->j_head);
1951ff780b91SHarshad Shirwadkar 	if (jbd2_has_feature_fast_commit(journal)) {
1952ff780b91SHarshad Shirwadkar 		/*
1953ff780b91SHarshad Shirwadkar 		 * When journal is clean, no need to commit fast commit flag and
1954ff780b91SHarshad Shirwadkar 		 * make file system incompatible with older kernels.
1955ff780b91SHarshad Shirwadkar 		 */
1956ff780b91SHarshad Shirwadkar 		jbd2_clear_feature_fast_commit(journal);
1957ff780b91SHarshad Shirwadkar 		had_fast_commit = true;
1958ff780b91SHarshad Shirwadkar 	}
195924bcc89cSJan Kara 
19606669797bSBart Van Assche 	jbd2_write_superblock(journal, write_flags);
196124bcc89cSJan Kara 
1962ff780b91SHarshad Shirwadkar 	if (had_fast_commit)
1963ff780b91SHarshad Shirwadkar 		jbd2_set_feature_fast_commit(journal);
1964ff780b91SHarshad Shirwadkar 
196524bcc89cSJan Kara 	/* Log is no longer empty */
196624bcc89cSJan Kara 	write_lock(&journal->j_state_lock);
1967f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_FLUSHED;
1968a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
1969470decc6SDave Kleikamp }
1970470decc6SDave Kleikamp 
197101d5d965SLeah Rumancik /**
197201d5d965SLeah Rumancik  * __jbd2_journal_erase() - Discard or zeroout journal blocks (excluding superblock)
197301d5d965SLeah Rumancik  * @journal: The journal to erase.
197401d5d965SLeah Rumancik  * @flags: A discard/zeroout request is sent for each physically contigous
197501d5d965SLeah Rumancik  *	region of the journal. Either JBD2_JOURNAL_FLUSH_DISCARD or
197601d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_ZEROOUT must be set to determine which operation
197701d5d965SLeah Rumancik  *	to perform.
197801d5d965SLeah Rumancik  *
197901d5d965SLeah Rumancik  * Note: JBD2_JOURNAL_FLUSH_ZEROOUT attempts to use hardware offload. Zeroes
198001d5d965SLeah Rumancik  * will be explicitly written if no hardware offload is available, see
198101d5d965SLeah Rumancik  * blkdev_issue_zeroout for more details.
198201d5d965SLeah Rumancik  */
__jbd2_journal_erase(journal_t * journal,unsigned int flags)198301d5d965SLeah Rumancik static int __jbd2_journal_erase(journal_t *journal, unsigned int flags)
198401d5d965SLeah Rumancik {
198501d5d965SLeah Rumancik 	int err = 0;
198601d5d965SLeah Rumancik 	unsigned long block, log_offset; /* logical */
198701d5d965SLeah Rumancik 	unsigned long long phys_block, block_start, block_stop; /* physical */
198801d5d965SLeah Rumancik 	loff_t byte_start, byte_stop, byte_count;
198901d5d965SLeah Rumancik 
199001d5d965SLeah Rumancik 	/* flags must be set to either discard or zeroout */
199101d5d965SLeah Rumancik 	if ((flags & ~JBD2_JOURNAL_FLUSH_VALID) || !flags ||
199201d5d965SLeah Rumancik 			((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
199301d5d965SLeah Rumancik 			(flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
199401d5d965SLeah Rumancik 		return -EINVAL;
199501d5d965SLeah Rumancik 
199670200574SChristoph Hellwig 	if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
199770200574SChristoph Hellwig 	    !bdev_max_discard_sectors(journal->j_dev))
199801d5d965SLeah Rumancik 		return -EOPNOTSUPP;
199901d5d965SLeah Rumancik 
200001d5d965SLeah Rumancik 	/*
200101d5d965SLeah Rumancik 	 * lookup block mapping and issue discard/zeroout for each
200201d5d965SLeah Rumancik 	 * contiguous region
200301d5d965SLeah Rumancik 	 */
200401d5d965SLeah Rumancik 	log_offset = be32_to_cpu(journal->j_superblock->s_first);
200501d5d965SLeah Rumancik 	block_start =  ~0ULL;
200601d5d965SLeah Rumancik 	for (block = log_offset; block < journal->j_total_len; block++) {
200701d5d965SLeah Rumancik 		err = jbd2_journal_bmap(journal, block, &phys_block);
200801d5d965SLeah Rumancik 		if (err) {
200901d5d965SLeah Rumancik 			pr_err("JBD2: bad block at offset %lu", block);
201001d5d965SLeah Rumancik 			return err;
201101d5d965SLeah Rumancik 		}
201201d5d965SLeah Rumancik 
201301d5d965SLeah Rumancik 		if (block_start == ~0ULL) {
201401d5d965SLeah Rumancik 			block_start = phys_block;
201501d5d965SLeah Rumancik 			block_stop = block_start - 1;
201601d5d965SLeah Rumancik 		}
201701d5d965SLeah Rumancik 
201801d5d965SLeah Rumancik 		/*
201901d5d965SLeah Rumancik 		 * last block not contiguous with current block,
202001d5d965SLeah Rumancik 		 * process last contiguous region and return to this block on
202101d5d965SLeah Rumancik 		 * next loop
202201d5d965SLeah Rumancik 		 */
202301d5d965SLeah Rumancik 		if (phys_block != block_stop + 1) {
202401d5d965SLeah Rumancik 			block--;
202501d5d965SLeah Rumancik 		} else {
202601d5d965SLeah Rumancik 			block_stop++;
202701d5d965SLeah Rumancik 			/*
202801d5d965SLeah Rumancik 			 * if this isn't the last block of journal,
202901d5d965SLeah Rumancik 			 * no need to process now because next block may also
203001d5d965SLeah Rumancik 			 * be part of this contiguous region
203101d5d965SLeah Rumancik 			 */
203201d5d965SLeah Rumancik 			if (block != journal->j_total_len - 1)
203301d5d965SLeah Rumancik 				continue;
203401d5d965SLeah Rumancik 		}
203501d5d965SLeah Rumancik 
203601d5d965SLeah Rumancik 		/*
203701d5d965SLeah Rumancik 		 * end of contiguous region or this is last block of journal,
203801d5d965SLeah Rumancik 		 * take care of the region
203901d5d965SLeah Rumancik 		 */
204001d5d965SLeah Rumancik 		byte_start = block_start * journal->j_blocksize;
204101d5d965SLeah Rumancik 		byte_stop = block_stop * journal->j_blocksize;
204201d5d965SLeah Rumancik 		byte_count = (block_stop - block_start + 1) *
204301d5d965SLeah Rumancik 				journal->j_blocksize;
204401d5d965SLeah Rumancik 
204501d5d965SLeah Rumancik 		truncate_inode_pages_range(journal->j_dev->bd_inode->i_mapping,
204601d5d965SLeah Rumancik 				byte_start, byte_stop);
204701d5d965SLeah Rumancik 
204801d5d965SLeah Rumancik 		if (flags & JBD2_JOURNAL_FLUSH_DISCARD) {
204901d5d965SLeah Rumancik 			err = blkdev_issue_discard(journal->j_dev,
205001d5d965SLeah Rumancik 					byte_start >> SECTOR_SHIFT,
205101d5d965SLeah Rumancik 					byte_count >> SECTOR_SHIFT,
205244abff2cSChristoph Hellwig 					GFP_NOFS);
205301d5d965SLeah Rumancik 		} else if (flags & JBD2_JOURNAL_FLUSH_ZEROOUT) {
205401d5d965SLeah Rumancik 			err = blkdev_issue_zeroout(journal->j_dev,
205501d5d965SLeah Rumancik 					byte_start >> SECTOR_SHIFT,
205601d5d965SLeah Rumancik 					byte_count >> SECTOR_SHIFT,
205701d5d965SLeah Rumancik 					GFP_NOFS, 0);
205801d5d965SLeah Rumancik 		}
205901d5d965SLeah Rumancik 
206001d5d965SLeah Rumancik 		if (unlikely(err != 0)) {
206101d5d965SLeah Rumancik 			pr_err("JBD2: (error %d) unable to wipe journal at physical blocks %llu - %llu",
206201d5d965SLeah Rumancik 					err, block_start, block_stop);
206301d5d965SLeah Rumancik 			return err;
206401d5d965SLeah Rumancik 		}
206501d5d965SLeah Rumancik 
206601d5d965SLeah Rumancik 		/* reset start and stop after processing a region */
206701d5d965SLeah Rumancik 		block_start = ~0ULL;
206801d5d965SLeah Rumancik 	}
206901d5d965SLeah Rumancik 
207001d5d965SLeah Rumancik 	return blkdev_issue_flush(journal->j_dev);
207101d5d965SLeah Rumancik }
207224bcc89cSJan Kara 
207324bcc89cSJan Kara /**
207424bcc89cSJan Kara  * jbd2_journal_update_sb_errno() - Update error in the journal.
207524bcc89cSJan Kara  * @journal: The journal to update.
207624bcc89cSJan Kara  *
207724bcc89cSJan Kara  * Update a journal's errno.  Write updated superblock to disk waiting for IO
207824bcc89cSJan Kara  * to complete.
207924bcc89cSJan Kara  */
jbd2_journal_update_sb_errno(journal_t * journal)2080d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal)
208124bcc89cSJan Kara {
208224bcc89cSJan Kara 	journal_superblock_t *sb = journal->j_superblock;
2083fb7c0244STheodore Ts'o 	int errcode;
208424bcc89cSJan Kara 
2085538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
2086fb7c0244STheodore Ts'o 	errcode = journal->j_errno;
2087fb7c0244STheodore Ts'o 	if (errcode == -ESHUTDOWN)
2088fb7c0244STheodore Ts'o 		errcode = 0;
2089cb3b3bf2SJan Kara 	jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
2090fb7c0244STheodore Ts'o 	sb->s_errno    = cpu_to_be32(errcode);
2091470decc6SDave Kleikamp 
20925c480a69SZhang Yi 	jbd2_write_superblock(journal, REQ_FUA);
2093470decc6SDave Kleikamp }
2094d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
2095470decc6SDave Kleikamp 
2096470decc6SDave Kleikamp /**
20972bf31d94SMauro Carvalho Chehab  * jbd2_journal_load() - Read journal from disk.
2098470decc6SDave Kleikamp  * @journal: Journal to act on.
2099470decc6SDave Kleikamp  *
2100470decc6SDave Kleikamp  * Given a journal_t structure which tells us which disk blocks contain
2101470decc6SDave Kleikamp  * a journal, read the journal from disk to initialise the in-memory
2102470decc6SDave Kleikamp  * structures.
2103470decc6SDave Kleikamp  */
jbd2_journal_load(journal_t * journal)2104f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal)
2105470decc6SDave Kleikamp {
2106470decc6SDave Kleikamp 	int err;
2107c3071308SZhang Yi 	journal_superblock_t *sb = journal->j_superblock;
2108470decc6SDave Kleikamp 
2109d2eecb03STheodore Ts'o 	/*
2110d2eecb03STheodore Ts'o 	 * Create a slab for this blocksize
2111d2eecb03STheodore Ts'o 	 */
2112d2eecb03STheodore Ts'o 	err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
2113d2eecb03STheodore Ts'o 	if (err)
2114d2eecb03STheodore Ts'o 		return err;
2115d2eecb03STheodore Ts'o 
2116470decc6SDave Kleikamp 	/* Let the recovery code check whether it needs to recover any
2117470decc6SDave Kleikamp 	 * data from the journal. */
21181d401650SGuoqing Cai 	err = jbd2_journal_recover(journal);
21191d401650SGuoqing Cai 	if (err) {
21201d401650SGuoqing Cai 		pr_warn("JBD2: journal recovery failed\n");
21211d401650SGuoqing Cai 		return err;
21221d401650SGuoqing Cai 	}
2123470decc6SDave Kleikamp 
2124e6a47428STheodore Ts'o 	if (journal->j_failed_commit) {
2125e6a47428STheodore Ts'o 		printk(KERN_ERR "JBD2: journal transaction %u on %s "
2126e6a47428STheodore Ts'o 		       "is corrupt.\n", journal->j_failed_commit,
2127e6a47428STheodore Ts'o 		       journal->j_devname);
21286a797d27SDarrick J. Wong 		return -EFSCORRUPTED;
2129e6a47428STheodore Ts'o 	}
2130a09decffSKai Li 	/*
2131a09decffSKai Li 	 * clear JBD2_ABORT flag initialized in journal_init_common
2132a09decffSKai Li 	 * here to update log tail information with the newest seq.
2133a09decffSKai Li 	 */
2134a09decffSKai Li 	journal->j_flags &= ~JBD2_ABORT;
2135e6a47428STheodore Ts'o 
2136470decc6SDave Kleikamp 	/* OK, we've finished with the dynamic journal bits:
2137470decc6SDave Kleikamp 	 * reinitialise the dynamic contents of the superblock in memory
2138470decc6SDave Kleikamp 	 * and reset them on disk. */
21391d401650SGuoqing Cai 	err = journal_reset(journal);
21401d401650SGuoqing Cai 	if (err) {
21411d401650SGuoqing Cai 		pr_warn("JBD2: journal reset failed\n");
21421d401650SGuoqing Cai 		return err;
21431d401650SGuoqing Cai 	}
2144470decc6SDave Kleikamp 
2145f7f4bccbSMingming Cao 	journal->j_flags |= JBD2_LOADED;
2146470decc6SDave Kleikamp 	return 0;
2147470decc6SDave Kleikamp }
2148470decc6SDave Kleikamp 
2149470decc6SDave Kleikamp /**
21502bf31d94SMauro Carvalho Chehab  * jbd2_journal_destroy() - Release a journal_t structure.
2151470decc6SDave Kleikamp  * @journal: Journal to act on.
2152470decc6SDave Kleikamp  *
2153470decc6SDave Kleikamp  * Release a journal_t structure once it is no longer in use by the
2154470decc6SDave Kleikamp  * journaled object.
215544519fafSHidehiro Kawai  * Return <0 if we couldn't clean up the journal.
2156470decc6SDave Kleikamp  */
jbd2_journal_destroy(journal_t * journal)215744519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal)
2158470decc6SDave Kleikamp {
215944519fafSHidehiro Kawai 	int err = 0;
216044519fafSHidehiro Kawai 
2161470decc6SDave Kleikamp 	/* Wait for the commit thread to wake up and die. */
2162470decc6SDave Kleikamp 	journal_kill_thread(journal);
2163470decc6SDave Kleikamp 
2164470decc6SDave Kleikamp 	/* Force a final log commit */
2165470decc6SDave Kleikamp 	if (journal->j_running_transaction)
2166f7f4bccbSMingming Cao 		jbd2_journal_commit_transaction(journal);
2167470decc6SDave Kleikamp 
2168470decc6SDave Kleikamp 	/* Force any old transactions to disk */
2169470decc6SDave Kleikamp 
2170470decc6SDave Kleikamp 	/* Totally anal locking here... */
2171470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
2172470decc6SDave Kleikamp 	while (journal->j_checkpoint_transactions != NULL) {
2173470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
21746fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
2175841df7dfSJan Kara 		err = jbd2_log_do_checkpoint(journal);
21761a0d3786STheodore Ts'o 		mutex_unlock(&journal->j_checkpoint_mutex);
2177841df7dfSJan Kara 		/*
2178841df7dfSJan Kara 		 * If checkpointing failed, just free the buffers to avoid
2179841df7dfSJan Kara 		 * looping forever
2180841df7dfSJan Kara 		 */
2181841df7dfSJan Kara 		if (err) {
2182841df7dfSJan Kara 			jbd2_journal_destroy_checkpoint(journal);
2183841df7dfSJan Kara 			spin_lock(&journal->j_list_lock);
2184841df7dfSJan Kara 			break;
2185841df7dfSJan Kara 		}
2186470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
2187470decc6SDave Kleikamp 	}
2188470decc6SDave Kleikamp 
2189470decc6SDave Kleikamp 	J_ASSERT(journal->j_running_transaction == NULL);
2190470decc6SDave Kleikamp 	J_ASSERT(journal->j_committing_transaction == NULL);
2191470decc6SDave Kleikamp 	J_ASSERT(journal->j_checkpoint_transactions == NULL);
2192470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
2193470decc6SDave Kleikamp 
2194fcf37549SZhang Yi 	/*
2195fcf37549SZhang Yi 	 * OK, all checkpoint transactions have been checked, now check the
2196fcf37549SZhang Yi 	 * write out io error flag and abort the journal if some buffer failed
2197fcf37549SZhang Yi 	 * to write back to the original location, otherwise the filesystem
2198fcf37549SZhang Yi 	 * may become inconsistent.
2199fcf37549SZhang Yi 	 */
2200fcf37549SZhang Yi 	if (!is_journal_aborted(journal) &&
2201fcf37549SZhang Yi 	    test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags))
2202fcf37549SZhang Yi 		jbd2_journal_abort(journal, -EIO);
2203fcf37549SZhang Yi 
220444519fafSHidehiro Kawai 	if (journal->j_sb_buffer) {
220544519fafSHidehiro Kawai 		if (!is_journal_aborted(journal)) {
22066fa7aa50STejun Heo 			mutex_lock_io(&journal->j_checkpoint_mutex);
2207c0a2ad9bSOGAWA Hirofumi 
2208c0a2ad9bSOGAWA Hirofumi 			write_lock(&journal->j_state_lock);
2209c0a2ad9bSOGAWA Hirofumi 			journal->j_tail_sequence =
2210c0a2ad9bSOGAWA Hirofumi 				++journal->j_transaction_sequence;
2211c0a2ad9bSOGAWA Hirofumi 			write_unlock(&journal->j_state_lock);
2212c0a2ad9bSOGAWA Hirofumi 
22135c480a69SZhang Yi 			jbd2_mark_journal_empty(journal, REQ_PREFLUSH | REQ_FUA);
2214a78bb11dSJan Kara 			mutex_unlock(&journal->j_checkpoint_mutex);
2215a78bb11dSJan Kara 		} else
221644519fafSHidehiro Kawai 			err = -EIO;
2217470decc6SDave Kleikamp 		brelse(journal->j_sb_buffer);
2218470decc6SDave Kleikamp 	}
2219470decc6SDave Kleikamp 
22200705e8d1STheodore Ts'o 	if (journal->j_shrinker.flags & SHRINKER_REGISTERED) {
22210705e8d1STheodore Ts'o 		percpu_counter_destroy(&journal->j_checkpoint_jh_count);
22220705e8d1STheodore Ts'o 		unregister_shrinker(&journal->j_shrinker);
22230705e8d1STheodore Ts'o 	}
22248e85fb3fSJohann Lombardi 	if (journal->j_proc_entry)
22258e85fb3fSJohann Lombardi 		jbd2_stats_proc_exit(journal);
2226470decc6SDave Kleikamp 	iput(journal->j_inode);
2227470decc6SDave Kleikamp 	if (journal->j_revoke)
2228f7f4bccbSMingming Cao 		jbd2_journal_destroy_revoke(journal);
222901b5adceSDarrick J. Wong 	if (journal->j_chksum_driver)
223001b5adceSDarrick J. Wong 		crypto_free_shash(journal->j_chksum_driver);
22316866d7b3SHarshad Shirwadkar 	kfree(journal->j_fc_wbuf);
2232470decc6SDave Kleikamp 	kfree(journal->j_wbuf);
2233470decc6SDave Kleikamp 	kfree(journal);
223444519fafSHidehiro Kawai 
223544519fafSHidehiro Kawai 	return err;
2236470decc6SDave Kleikamp }
2237470decc6SDave Kleikamp 
2238470decc6SDave Kleikamp 
2239470decc6SDave Kleikamp /**
22402bf31d94SMauro Carvalho Chehab  * jbd2_journal_check_used_features() - Check if features specified are used.
2241470decc6SDave Kleikamp  * @journal: Journal to check.
2242470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2243470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2244470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2245470decc6SDave Kleikamp  *
2246470decc6SDave Kleikamp  * Check whether the journal uses all of a given set of
2247470decc6SDave Kleikamp  * features.  Return true (non-zero) if it does.
2248470decc6SDave Kleikamp  **/
2249470decc6SDave Kleikamp 
jbd2_journal_check_used_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2250f7f4bccbSMingming Cao int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat,
2251470decc6SDave Kleikamp 				 unsigned long ro, unsigned long incompat)
2252470decc6SDave Kleikamp {
2253470decc6SDave Kleikamp 	journal_superblock_t *sb;
2254470decc6SDave Kleikamp 
2255470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
2256470decc6SDave Kleikamp 		return 1;
22575cf036d4SZhang Yi 	if (!jbd2_format_support_feature(journal))
2258470decc6SDave Kleikamp 		return 0;
2259470decc6SDave Kleikamp 
2260470decc6SDave Kleikamp 	sb = journal->j_superblock;
2261470decc6SDave Kleikamp 
2262470decc6SDave Kleikamp 	if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
2263470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
2264470decc6SDave Kleikamp 	    ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
2265470decc6SDave Kleikamp 		return 1;
2266470decc6SDave Kleikamp 
2267470decc6SDave Kleikamp 	return 0;
2268470decc6SDave Kleikamp }
2269470decc6SDave Kleikamp 
2270470decc6SDave Kleikamp /**
22712bf31d94SMauro Carvalho Chehab  * jbd2_journal_check_available_features() - Check feature set in journalling layer
2272470decc6SDave Kleikamp  * @journal: Journal to check.
2273470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2274470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2275470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2276470decc6SDave Kleikamp  *
2277470decc6SDave Kleikamp  * Check whether the journaling code supports the use of
2278470decc6SDave Kleikamp  * all of a given set of features on this journal.  Return true
2279470decc6SDave Kleikamp  * (non-zero) if it can. */
2280470decc6SDave Kleikamp 
jbd2_journal_check_available_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2281f7f4bccbSMingming Cao int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat,
2282470decc6SDave Kleikamp 				      unsigned long ro, unsigned long incompat)
2283470decc6SDave Kleikamp {
2284470decc6SDave Kleikamp 	if (!compat && !ro && !incompat)
2285470decc6SDave Kleikamp 		return 1;
2286470decc6SDave Kleikamp 
22875cf036d4SZhang Yi 	if (!jbd2_format_support_feature(journal))
2288470decc6SDave Kleikamp 		return 0;
2289470decc6SDave Kleikamp 
2290f7f4bccbSMingming Cao 	if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
2291f7f4bccbSMingming Cao 	    (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
2292f7f4bccbSMingming Cao 	    (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
2293470decc6SDave Kleikamp 		return 1;
2294470decc6SDave Kleikamp 
2295470decc6SDave Kleikamp 	return 0;
2296470decc6SDave Kleikamp }
2297470decc6SDave Kleikamp 
2298a1e5e465SHarshad Shirwadkar static int
jbd2_journal_initialize_fast_commit(journal_t * journal)2299a1e5e465SHarshad Shirwadkar jbd2_journal_initialize_fast_commit(journal_t *journal)
2300a1e5e465SHarshad Shirwadkar {
2301a1e5e465SHarshad Shirwadkar 	journal_superblock_t *sb = journal->j_superblock;
2302a1e5e465SHarshad Shirwadkar 	unsigned long long num_fc_blks;
2303a1e5e465SHarshad Shirwadkar 
23049bd23c31SHarshad Shirwadkar 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
2305a1e5e465SHarshad Shirwadkar 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
2306a1e5e465SHarshad Shirwadkar 		return -ENOSPC;
2307a1e5e465SHarshad Shirwadkar 
2308a1e5e465SHarshad Shirwadkar 	/* Are we called twice? */
2309a1e5e465SHarshad Shirwadkar 	WARN_ON(journal->j_fc_wbuf != NULL);
2310a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbuf = kmalloc_array(num_fc_blks,
2311a1e5e465SHarshad Shirwadkar 				sizeof(struct buffer_head *), GFP_KERNEL);
2312a1e5e465SHarshad Shirwadkar 	if (!journal->j_fc_wbuf)
2313a1e5e465SHarshad Shirwadkar 		return -ENOMEM;
2314a1e5e465SHarshad Shirwadkar 
2315a1e5e465SHarshad Shirwadkar 	journal->j_fc_wbufsize = num_fc_blks;
2316a1e5e465SHarshad Shirwadkar 	journal->j_fc_last = journal->j_last;
2317a1e5e465SHarshad Shirwadkar 	journal->j_last = journal->j_fc_last - num_fc_blks;
2318a1e5e465SHarshad Shirwadkar 	journal->j_fc_first = journal->j_last + 1;
2319a1e5e465SHarshad Shirwadkar 	journal->j_fc_off = 0;
2320a1e5e465SHarshad Shirwadkar 	journal->j_free = journal->j_last - journal->j_first;
2321a1e5e465SHarshad Shirwadkar 
2322a1e5e465SHarshad Shirwadkar 	return 0;
2323a1e5e465SHarshad Shirwadkar }
2324a1e5e465SHarshad Shirwadkar 
2325470decc6SDave Kleikamp /**
23262bf31d94SMauro Carvalho Chehab  * jbd2_journal_set_features() - Mark a given journal feature in the superblock
2327470decc6SDave Kleikamp  * @journal: Journal to act on.
2328470decc6SDave Kleikamp  * @compat: bitmask of compatible features
2329470decc6SDave Kleikamp  * @ro: bitmask of features that force read-only mount
2330470decc6SDave Kleikamp  * @incompat: bitmask of incompatible features
2331470decc6SDave Kleikamp  *
2332470decc6SDave Kleikamp  * Mark a given journal feature as present on the
2333470decc6SDave Kleikamp  * superblock.  Returns true if the requested features could be set.
2334470decc6SDave Kleikamp  *
2335470decc6SDave Kleikamp  */
2336470decc6SDave Kleikamp 
jbd2_journal_set_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2337f7f4bccbSMingming Cao int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
2338470decc6SDave Kleikamp 			  unsigned long ro, unsigned long incompat)
2339470decc6SDave Kleikamp {
234025ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \
234125ed6e8aSDarrick J. Wong 		((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
234225ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \
234325ed6e8aSDarrick J. Wong 		((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
2344470decc6SDave Kleikamp 	journal_superblock_t *sb;
2345470decc6SDave Kleikamp 
2346f7f4bccbSMingming Cao 	if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
2347470decc6SDave Kleikamp 		return 1;
2348470decc6SDave Kleikamp 
2349f7f4bccbSMingming Cao 	if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
2350470decc6SDave Kleikamp 		return 0;
2351470decc6SDave Kleikamp 
2352db9ee220SDarrick J. Wong 	/* If enabling v2 checksums, turn on v3 instead */
2353db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
2354db9ee220SDarrick J. Wong 		incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
2355db9ee220SDarrick J. Wong 		incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
2356db9ee220SDarrick J. Wong 	}
2357db9ee220SDarrick J. Wong 
2358db9ee220SDarrick J. Wong 	/* Asking for checksumming v3 and v1?  Only give them v3. */
2359db9ee220SDarrick J. Wong 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
236025ed6e8aSDarrick J. Wong 	    compat & JBD2_FEATURE_COMPAT_CHECKSUM)
236125ed6e8aSDarrick J. Wong 		compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
236225ed6e8aSDarrick J. Wong 
2363cb3b3bf2SJan Kara 	jbd2_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
2364470decc6SDave Kleikamp 		  compat, ro, incompat);
2365470decc6SDave Kleikamp 
2366470decc6SDave Kleikamp 	sb = journal->j_superblock;
2367470decc6SDave Kleikamp 
2368a1e5e465SHarshad Shirwadkar 	if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) {
2369a1e5e465SHarshad Shirwadkar 		if (jbd2_journal_initialize_fast_commit(journal)) {
2370a1e5e465SHarshad Shirwadkar 			pr_err("JBD2: Cannot enable fast commits.\n");
2371a1e5e465SHarshad Shirwadkar 			return 0;
2372a1e5e465SHarshad Shirwadkar 		}
2373a1e5e465SHarshad Shirwadkar 	}
2374a1e5e465SHarshad Shirwadkar 
2375538bcaa6STheodore Ts'o 	/* Load the checksum driver if necessary */
2376538bcaa6STheodore Ts'o 	if ((journal->j_chksum_driver == NULL) &&
2377538bcaa6STheodore Ts'o 	    INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
2378538bcaa6STheodore Ts'o 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
2379538bcaa6STheodore Ts'o 		if (IS_ERR(journal->j_chksum_driver)) {
2380538bcaa6STheodore Ts'o 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
2381538bcaa6STheodore Ts'o 			journal->j_chksum_driver = NULL;
2382538bcaa6STheodore Ts'o 			return 0;
2383538bcaa6STheodore Ts'o 		}
2384538bcaa6STheodore Ts'o 		/* Precompute checksum seed for all metadata */
2385538bcaa6STheodore Ts'o 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
2386538bcaa6STheodore Ts'o 						   sizeof(sb->s_uuid));
2387538bcaa6STheodore Ts'o 	}
2388538bcaa6STheodore Ts'o 
2389538bcaa6STheodore Ts'o 	lock_buffer(journal->j_sb_buffer);
2390538bcaa6STheodore Ts'o 
2391db9ee220SDarrick J. Wong 	/* If enabling v3 checksums, update superblock */
2392db9ee220SDarrick J. Wong 	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
239325ed6e8aSDarrick J. Wong 		sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
239425ed6e8aSDarrick J. Wong 		sb->s_feature_compat &=
239525ed6e8aSDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
239632f38691SDarrick J. Wong 	}
239725ed6e8aSDarrick J. Wong 
239825ed6e8aSDarrick J. Wong 	/* If enabling v1 checksums, downgrade superblock */
239925ed6e8aSDarrick J. Wong 	if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
240025ed6e8aSDarrick J. Wong 		sb->s_feature_incompat &=
2401db9ee220SDarrick J. Wong 			~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
2402db9ee220SDarrick J. Wong 				     JBD2_FEATURE_INCOMPAT_CSUM_V3);
240325ed6e8aSDarrick J. Wong 
2404470decc6SDave Kleikamp 	sb->s_feature_compat    |= cpu_to_be32(compat);
2405470decc6SDave Kleikamp 	sb->s_feature_ro_compat |= cpu_to_be32(ro);
2406470decc6SDave Kleikamp 	sb->s_feature_incompat  |= cpu_to_be32(incompat);
2407538bcaa6STheodore Ts'o 	unlock_buffer(journal->j_sb_buffer);
2408b81c3758SJan Kara 	jbd2_journal_init_transaction_limits(journal);
2409470decc6SDave Kleikamp 
2410470decc6SDave Kleikamp 	return 1;
241125ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON
241225ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON
2413470decc6SDave Kleikamp }
2414470decc6SDave Kleikamp 
2415818d276cSGirish Shilamkar /*
2416818d276cSGirish Shilamkar  * jbd2_journal_clear_features() - Clear a given journal feature in the
2417818d276cSGirish Shilamkar  * 				    superblock
2418818d276cSGirish Shilamkar  * @journal: Journal to act on.
2419818d276cSGirish Shilamkar  * @compat: bitmask of compatible features
2420818d276cSGirish Shilamkar  * @ro: bitmask of features that force read-only mount
2421818d276cSGirish Shilamkar  * @incompat: bitmask of incompatible features
2422818d276cSGirish Shilamkar  *
2423818d276cSGirish Shilamkar  * Clear a given journal feature as present on the
2424818d276cSGirish Shilamkar  * superblock.
2425818d276cSGirish Shilamkar  */
jbd2_journal_clear_features(journal_t * journal,unsigned long compat,unsigned long ro,unsigned long incompat)2426818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
2427818d276cSGirish Shilamkar 				unsigned long ro, unsigned long incompat)
2428818d276cSGirish Shilamkar {
2429818d276cSGirish Shilamkar 	journal_superblock_t *sb;
2430818d276cSGirish Shilamkar 
2431cb3b3bf2SJan Kara 	jbd2_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
2432818d276cSGirish Shilamkar 		  compat, ro, incompat);
2433818d276cSGirish Shilamkar 
2434818d276cSGirish Shilamkar 	sb = journal->j_superblock;
2435818d276cSGirish Shilamkar 
2436818d276cSGirish Shilamkar 	sb->s_feature_compat    &= ~cpu_to_be32(compat);
2437818d276cSGirish Shilamkar 	sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
2438818d276cSGirish Shilamkar 	sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
2439b81c3758SJan Kara 	jbd2_journal_init_transaction_limits(journal);
2440818d276cSGirish Shilamkar }
2441818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features);
2442470decc6SDave Kleikamp 
2443470decc6SDave Kleikamp /**
24442bf31d94SMauro Carvalho Chehab  * jbd2_journal_flush() - Flush journal
2445470decc6SDave Kleikamp  * @journal: Journal to act on.
244601d5d965SLeah Rumancik  * @flags: optional operation on the journal blocks after the flush (see below)
2447470decc6SDave Kleikamp  *
2448470decc6SDave Kleikamp  * Flush all data for a given journal to disk and empty the journal.
2449470decc6SDave Kleikamp  * Filesystems can use this when remounting readonly to ensure that
245001d5d965SLeah Rumancik  * recovery does not need to happen on remount. Optionally, a discard or zeroout
245101d5d965SLeah Rumancik  * can be issued on the journal blocks after flushing.
245201d5d965SLeah Rumancik  *
245301d5d965SLeah Rumancik  * flags:
245401d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_DISCARD: issues discards for the journal blocks
245501d5d965SLeah Rumancik  *	JBD2_JOURNAL_FLUSH_ZEROOUT: issues zeroouts for the journal blocks
2456470decc6SDave Kleikamp  */
jbd2_journal_flush(journal_t * journal,unsigned int flags)245701d5d965SLeah Rumancik int jbd2_journal_flush(journal_t *journal, unsigned int flags)
2458470decc6SDave Kleikamp {
2459470decc6SDave Kleikamp 	int err = 0;
2460470decc6SDave Kleikamp 	transaction_t *transaction = NULL;
2461470decc6SDave Kleikamp 
2462a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2463470decc6SDave Kleikamp 
2464470decc6SDave Kleikamp 	/* Force everything buffered to the log... */
2465470decc6SDave Kleikamp 	if (journal->j_running_transaction) {
2466470decc6SDave Kleikamp 		transaction = journal->j_running_transaction;
2467f7f4bccbSMingming Cao 		__jbd2_log_start_commit(journal, transaction->t_tid);
2468470decc6SDave Kleikamp 	} else if (journal->j_committing_transaction)
2469470decc6SDave Kleikamp 		transaction = journal->j_committing_transaction;
2470470decc6SDave Kleikamp 
2471470decc6SDave Kleikamp 	/* Wait for the log commit to complete... */
2472470decc6SDave Kleikamp 	if (transaction) {
2473470decc6SDave Kleikamp 		tid_t tid = transaction->t_tid;
2474470decc6SDave Kleikamp 
2475a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
2476f7f4bccbSMingming Cao 		jbd2_log_wait_commit(journal, tid);
2477470decc6SDave Kleikamp 	} else {
2478a931da6aSTheodore Ts'o 		write_unlock(&journal->j_state_lock);
2479470decc6SDave Kleikamp 	}
2480470decc6SDave Kleikamp 
2481470decc6SDave Kleikamp 	/* ...and flush everything in the log out to disk. */
2482470decc6SDave Kleikamp 	spin_lock(&journal->j_list_lock);
2483470decc6SDave Kleikamp 	while (!err && journal->j_checkpoint_transactions != NULL) {
2484470decc6SDave Kleikamp 		spin_unlock(&journal->j_list_lock);
24856fa7aa50STejun Heo 		mutex_lock_io(&journal->j_checkpoint_mutex);
2486f7f4bccbSMingming Cao 		err = jbd2_log_do_checkpoint(journal);
248744519fafSHidehiro Kawai 		mutex_unlock(&journal->j_checkpoint_mutex);
2488470decc6SDave Kleikamp 		spin_lock(&journal->j_list_lock);
2489470decc6SDave Kleikamp 	}
2490470decc6SDave Kleikamp 	spin_unlock(&journal->j_list_lock);
249144519fafSHidehiro Kawai 
249244519fafSHidehiro Kawai 	if (is_journal_aborted(journal))
249344519fafSHidehiro Kawai 		return -EIO;
249444519fafSHidehiro Kawai 
24956fa7aa50STejun Heo 	mutex_lock_io(&journal->j_checkpoint_mutex);
24966f6a6fdaSJoseph Qi 	if (!err) {
24976f6a6fdaSJoseph Qi 		err = jbd2_cleanup_journal_tail(journal);
24986f6a6fdaSJoseph Qi 		if (err < 0) {
24996f6a6fdaSJoseph Qi 			mutex_unlock(&journal->j_checkpoint_mutex);
25006f6a6fdaSJoseph Qi 			goto out;
25016f6a6fdaSJoseph Qi 		}
25026f6a6fdaSJoseph Qi 		err = 0;
25036f6a6fdaSJoseph Qi 	}
2504470decc6SDave Kleikamp 
2505470decc6SDave Kleikamp 	/* Finally, mark the journal as really needing no recovery.
2506470decc6SDave Kleikamp 	 * This sets s_start==0 in the underlying superblock, which is
2507470decc6SDave Kleikamp 	 * the magic code for a fully-recovered superblock.  Any future
2508470decc6SDave Kleikamp 	 * commits of data to the journal will restore the current
2509470decc6SDave Kleikamp 	 * s_start value. */
25105c480a69SZhang Yi 	jbd2_mark_journal_empty(journal, REQ_FUA);
251101d5d965SLeah Rumancik 
251201d5d965SLeah Rumancik 	if (flags)
251301d5d965SLeah Rumancik 		err = __jbd2_journal_erase(journal, flags);
251401d5d965SLeah Rumancik 
2515a78bb11dSJan Kara 	mutex_unlock(&journal->j_checkpoint_mutex);
2516a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2517470decc6SDave Kleikamp 	J_ASSERT(!journal->j_running_transaction);
2518470decc6SDave Kleikamp 	J_ASSERT(!journal->j_committing_transaction);
2519470decc6SDave Kleikamp 	J_ASSERT(!journal->j_checkpoint_transactions);
2520470decc6SDave Kleikamp 	J_ASSERT(journal->j_head == journal->j_tail);
2521470decc6SDave Kleikamp 	J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
2522a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
25236f6a6fdaSJoseph Qi out:
25246f6a6fdaSJoseph Qi 	return err;
2525470decc6SDave Kleikamp }
2526470decc6SDave Kleikamp 
2527470decc6SDave Kleikamp /**
25282bf31d94SMauro Carvalho Chehab  * jbd2_journal_wipe() - Wipe journal contents
2529470decc6SDave Kleikamp  * @journal: Journal to act on.
2530470decc6SDave Kleikamp  * @write: flag (see below)
2531470decc6SDave Kleikamp  *
2532470decc6SDave Kleikamp  * Wipe out all of the contents of a journal, safely.  This will produce
2533470decc6SDave Kleikamp  * a warning if the journal contains any valid recovery information.
2534f7f4bccbSMingming Cao  * Must be called between journal_init_*() and jbd2_journal_load().
2535470decc6SDave Kleikamp  *
2536470decc6SDave Kleikamp  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
2537470decc6SDave Kleikamp  * we merely suppress recovery.
2538470decc6SDave Kleikamp  */
2539470decc6SDave Kleikamp 
jbd2_journal_wipe(journal_t * journal,int write)2540f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write)
2541470decc6SDave Kleikamp {
2542d9a45496SZhang Yi 	int err;
2543470decc6SDave Kleikamp 
2544f7f4bccbSMingming Cao 	J_ASSERT (!(journal->j_flags & JBD2_LOADED));
2545470decc6SDave Kleikamp 
2546470decc6SDave Kleikamp 	if (!journal->j_tail)
2547d9a45496SZhang Yi 		return 0;
2548470decc6SDave Kleikamp 
2549f2a44523SEryu Guan 	printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
2550470decc6SDave Kleikamp 		write ? "Clearing" : "Ignoring");
2551470decc6SDave Kleikamp 
2552f7f4bccbSMingming Cao 	err = jbd2_journal_skip_recovery(journal);
2553a78bb11dSJan Kara 	if (write) {
2554a78bb11dSJan Kara 		/* Lock to make assertions happy... */
255553cf9784SXiaoguang Wang 		mutex_lock_io(&journal->j_checkpoint_mutex);
25565c480a69SZhang Yi 		jbd2_mark_journal_empty(journal, REQ_FUA);
2557a78bb11dSJan Kara 		mutex_unlock(&journal->j_checkpoint_mutex);
2558a78bb11dSJan Kara 	}
2559470decc6SDave Kleikamp 
2560470decc6SDave Kleikamp 	return err;
2561470decc6SDave Kleikamp }
2562470decc6SDave Kleikamp 
2563470decc6SDave Kleikamp /**
25642bf31d94SMauro Carvalho Chehab  * jbd2_journal_abort () - Shutdown the journal immediately.
2565470decc6SDave Kleikamp  * @journal: the journal to shutdown.
2566470decc6SDave Kleikamp  * @errno:   an error number to record in the journal indicating
2567470decc6SDave Kleikamp  *           the reason for the shutdown.
2568470decc6SDave Kleikamp  *
2569470decc6SDave Kleikamp  * Perform a complete, immediate shutdown of the ENTIRE
2570470decc6SDave Kleikamp  * journal (not of a single transaction).  This operation cannot be
2571470decc6SDave Kleikamp  * undone without closing and reopening the journal.
2572470decc6SDave Kleikamp  *
2573f7f4bccbSMingming Cao  * The jbd2_journal_abort function is intended to support higher level error
2574470decc6SDave Kleikamp  * recovery mechanisms such as the ext2/ext3 remount-readonly error
2575470decc6SDave Kleikamp  * mode.
2576470decc6SDave Kleikamp  *
2577470decc6SDave Kleikamp  * Journal abort has very specific semantics.  Any existing dirty,
2578470decc6SDave Kleikamp  * unjournaled buffers in the main filesystem will still be written to
2579470decc6SDave Kleikamp  * disk by bdflush, but the journaling mechanism will be suspended
2580470decc6SDave Kleikamp  * immediately and no further transaction commits will be honoured.
2581470decc6SDave Kleikamp  *
2582470decc6SDave Kleikamp  * Any dirty, journaled buffers will be written back to disk without
2583470decc6SDave Kleikamp  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
2584470decc6SDave Kleikamp  * filesystem, but we _do_ attempt to leave as much data as possible
2585470decc6SDave Kleikamp  * behind for fsck to use for cleanup.
2586470decc6SDave Kleikamp  *
2587470decc6SDave Kleikamp  * Any attempt to get a new transaction handle on a journal which is in
2588470decc6SDave Kleikamp  * ABORT state will just result in an -EROFS error return.  A
2589f7f4bccbSMingming Cao  * jbd2_journal_stop on an existing handle will return -EIO if we have
2590470decc6SDave Kleikamp  * entered abort state during the update.
2591470decc6SDave Kleikamp  *
2592470decc6SDave Kleikamp  * Recursive transactions are not disturbed by journal abort until the
2593f7f4bccbSMingming Cao  * final jbd2_journal_stop, which will receive the -EIO error.
2594470decc6SDave Kleikamp  *
2595f7f4bccbSMingming Cao  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2596470decc6SDave Kleikamp  * which will be recorded (if possible) in the journal superblock.  This
2597470decc6SDave Kleikamp  * allows a client to record failure conditions in the middle of a
2598470decc6SDave Kleikamp  * transaction without having to complete the transaction to record the
2599470decc6SDave Kleikamp  * failure to disk.  ext3_error, for example, now uses this
2600470decc6SDave Kleikamp  * functionality.
2601470decc6SDave Kleikamp  *
2602470decc6SDave Kleikamp  */
2603470decc6SDave Kleikamp 
jbd2_journal_abort(journal_t * journal,int errno)2604f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno)
2605470decc6SDave Kleikamp {
26067f6225e4Szhangyi (F) 	transaction_t *transaction;
26077f6225e4Szhangyi (F) 
26087f6225e4Szhangyi (F) 	/*
26097b97d868Szhangyi (F) 	 * Lock the aborting procedure until everything is done, this avoid
26107b97d868Szhangyi (F) 	 * races between filesystem's error handling flow (e.g. ext4_abort()),
26117b97d868Szhangyi (F) 	 * ensure panic after the error info is written into journal's
26127b97d868Szhangyi (F) 	 * superblock.
26137b97d868Szhangyi (F) 	 */
26147b97d868Szhangyi (F) 	mutex_lock(&journal->j_abort_mutex);
26157b97d868Szhangyi (F) 	/*
26167f6225e4Szhangyi (F) 	 * ESHUTDOWN always takes precedence because a file system check
26177f6225e4Szhangyi (F) 	 * caused by any other journal abort error is not required after
26187f6225e4Szhangyi (F) 	 * a shutdown triggered.
26197f6225e4Szhangyi (F) 	 */
26207f6225e4Szhangyi (F) 	write_lock(&journal->j_state_lock);
26217f6225e4Szhangyi (F) 	if (journal->j_flags & JBD2_ABORT) {
26227f6225e4Szhangyi (F) 		int old_errno = journal->j_errno;
26237f6225e4Szhangyi (F) 
26247f6225e4Szhangyi (F) 		write_unlock(&journal->j_state_lock);
26257f6225e4Szhangyi (F) 		if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) {
26267f6225e4Szhangyi (F) 			journal->j_errno = errno;
26277f6225e4Szhangyi (F) 			jbd2_journal_update_sb_errno(journal);
26287f6225e4Szhangyi (F) 		}
26297b97d868Szhangyi (F) 		mutex_unlock(&journal->j_abort_mutex);
26307f6225e4Szhangyi (F) 		return;
26317f6225e4Szhangyi (F) 	}
26327f6225e4Szhangyi (F) 
26337f6225e4Szhangyi (F) 	/*
26347f6225e4Szhangyi (F) 	 * Mark the abort as occurred and start current running transaction
26357f6225e4Szhangyi (F) 	 * to release all journaled buffer.
26367f6225e4Szhangyi (F) 	 */
26377f6225e4Szhangyi (F) 	pr_err("Aborting journal on device %s.\n", journal->j_devname);
26387f6225e4Szhangyi (F) 
26397f6225e4Szhangyi (F) 	journal->j_flags |= JBD2_ABORT;
26407f6225e4Szhangyi (F) 	journal->j_errno = errno;
26417f6225e4Szhangyi (F) 	transaction = journal->j_running_transaction;
26427f6225e4Szhangyi (F) 	if (transaction)
26437f6225e4Szhangyi (F) 		__jbd2_log_start_commit(journal, transaction->t_tid);
26447f6225e4Szhangyi (F) 	write_unlock(&journal->j_state_lock);
26457f6225e4Szhangyi (F) 
26467f6225e4Szhangyi (F) 	/*
26477f6225e4Szhangyi (F) 	 * Record errno to the journal super block, so that fsck and jbd2
26487f6225e4Szhangyi (F) 	 * layer could realise that a filesystem check is needed.
26497f6225e4Szhangyi (F) 	 */
26507f6225e4Szhangyi (F) 	jbd2_journal_update_sb_errno(journal);
26517b97d868Szhangyi (F) 	mutex_unlock(&journal->j_abort_mutex);
2652470decc6SDave Kleikamp }
2653470decc6SDave Kleikamp 
2654470decc6SDave Kleikamp /**
26552bf31d94SMauro Carvalho Chehab  * jbd2_journal_errno() - returns the journal's error state.
2656470decc6SDave Kleikamp  * @journal: journal to examine.
2657470decc6SDave Kleikamp  *
2658bfcd3555SAlberto Bertogli  * This is the errno number set with jbd2_journal_abort(), the last
2659470decc6SDave Kleikamp  * time the journal was mounted - if the journal was stopped
2660470decc6SDave Kleikamp  * without calling abort this will be 0.
2661470decc6SDave Kleikamp  *
2662470decc6SDave Kleikamp  * If the journal has been aborted on this mount time -EROFS will
2663470decc6SDave Kleikamp  * be returned.
2664470decc6SDave Kleikamp  */
jbd2_journal_errno(journal_t * journal)2665f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal)
2666470decc6SDave Kleikamp {
2667470decc6SDave Kleikamp 	int err;
2668470decc6SDave Kleikamp 
2669a931da6aSTheodore Ts'o 	read_lock(&journal->j_state_lock);
2670f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2671470decc6SDave Kleikamp 		err = -EROFS;
2672470decc6SDave Kleikamp 	else
2673470decc6SDave Kleikamp 		err = journal->j_errno;
2674a931da6aSTheodore Ts'o 	read_unlock(&journal->j_state_lock);
2675470decc6SDave Kleikamp 	return err;
2676470decc6SDave Kleikamp }
2677470decc6SDave Kleikamp 
2678470decc6SDave Kleikamp /**
26792bf31d94SMauro Carvalho Chehab  * jbd2_journal_clear_err() - clears the journal's error state
2680470decc6SDave Kleikamp  * @journal: journal to act on.
2681470decc6SDave Kleikamp  *
2682bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2683470decc6SDave Kleikamp  * mode.
2684470decc6SDave Kleikamp  */
jbd2_journal_clear_err(journal_t * journal)2685f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal)
2686470decc6SDave Kleikamp {
2687470decc6SDave Kleikamp 	int err = 0;
2688470decc6SDave Kleikamp 
2689a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2690f7f4bccbSMingming Cao 	if (journal->j_flags & JBD2_ABORT)
2691470decc6SDave Kleikamp 		err = -EROFS;
2692470decc6SDave Kleikamp 	else
2693470decc6SDave Kleikamp 		journal->j_errno = 0;
2694a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2695470decc6SDave Kleikamp 	return err;
2696470decc6SDave Kleikamp }
2697470decc6SDave Kleikamp 
2698470decc6SDave Kleikamp /**
26992bf31d94SMauro Carvalho Chehab  * jbd2_journal_ack_err() - Ack journal err.
2700470decc6SDave Kleikamp  * @journal: journal to act on.
2701470decc6SDave Kleikamp  *
2702bfcd3555SAlberto Bertogli  * An error must be cleared or acked to take a FS out of readonly
2703470decc6SDave Kleikamp  * mode.
2704470decc6SDave Kleikamp  */
jbd2_journal_ack_err(journal_t * journal)2705f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal)
2706470decc6SDave Kleikamp {
2707a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
2708470decc6SDave Kleikamp 	if (journal->j_errno)
2709f7f4bccbSMingming Cao 		journal->j_flags |= JBD2_ACK_ERR;
2710a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
2711470decc6SDave Kleikamp }
2712470decc6SDave Kleikamp 
jbd2_journal_blocks_per_page(struct inode * inode)2713f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode)
2714470decc6SDave Kleikamp {
271509cbfeafSKirill A. Shutemov 	return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
2716470decc6SDave Kleikamp }
2717470decc6SDave Kleikamp 
2718470decc6SDave Kleikamp /*
2719b517bea1SZach Brown  * helper functions to deal with 32 or 64bit block numbers.
2720b517bea1SZach Brown  */
journal_tag_bytes(journal_t * journal)2721b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal)
2722b517bea1SZach Brown {
2723db9ee220SDarrick J. Wong 	size_t sz;
2724db9ee220SDarrick J. Wong 
272556316a0dSDarrick J. Wong 	if (jbd2_has_feature_csum3(journal))
2726db9ee220SDarrick J. Wong 		return sizeof(journal_block_tag3_t);
2727db9ee220SDarrick J. Wong 
2728db9ee220SDarrick J. Wong 	sz = sizeof(journal_block_tag_t);
2729c3900875SDarrick J. Wong 
273056316a0dSDarrick J. Wong 	if (jbd2_has_feature_csum2(journal))
2731db9ee220SDarrick J. Wong 		sz += sizeof(__u16);
2732c3900875SDarrick J. Wong 
273356316a0dSDarrick J. Wong 	if (jbd2_has_feature_64bit(journal))
2734db9ee220SDarrick J. Wong 		return sz;
2735b517bea1SZach Brown 	else
2736db9ee220SDarrick J. Wong 		return sz - sizeof(__u32);
2737b517bea1SZach Brown }
2738b517bea1SZach Brown 
2739b517bea1SZach Brown /*
2740d2eecb03STheodore Ts'o  * JBD memory management
2741d2eecb03STheodore Ts'o  *
2742d2eecb03STheodore Ts'o  * These functions are used to allocate block-sized chunks of memory
2743d2eecb03STheodore Ts'o  * used for making copies of buffer_head data.  Very often it will be
2744d2eecb03STheodore Ts'o  * page-sized chunks of data, but sometimes it will be in
2745d2eecb03STheodore Ts'o  * sub-page-size chunks.  (For example, 16k pages on Power systems
2746d2eecb03STheodore Ts'o  * with a 4k block file system.)  For blocks smaller than a page, we
2747d2eecb03STheodore Ts'o  * use a SLAB allocator.  There are slab caches for each block size,
2748d2eecb03STheodore Ts'o  * which are allocated at mount time, if necessary, and we only free
2749d2eecb03STheodore Ts'o  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
2750d2eecb03STheodore Ts'o  * this reason we don't need to a mutex to protect access to
2751d2eecb03STheodore Ts'o  * jbd2_slab[] allocating or releasing memory; only in
2752d2eecb03STheodore Ts'o  * jbd2_journal_create_slab().
2753d2eecb03STheodore Ts'o  */
2754d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8
2755d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2756d2eecb03STheodore Ts'o 
2757d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2758d2eecb03STheodore Ts'o 	"jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2759d2eecb03STheodore Ts'o 	"jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2760d2eecb03STheodore Ts'o };
2761d2eecb03STheodore Ts'o 
2762d2eecb03STheodore Ts'o 
jbd2_journal_destroy_slabs(void)2763d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void)
2764d2eecb03STheodore Ts'o {
2765d2eecb03STheodore Ts'o 	int i;
2766d2eecb03STheodore Ts'o 
2767d2eecb03STheodore Ts'o 	for (i = 0; i < JBD2_MAX_SLABS; i++) {
2768d2eecb03STheodore Ts'o 		kmem_cache_destroy(jbd2_slab[i]);
2769d2eecb03STheodore Ts'o 		jbd2_slab[i] = NULL;
2770d2eecb03STheodore Ts'o 	}
2771d2eecb03STheodore Ts'o }
2772d2eecb03STheodore Ts'o 
jbd2_journal_create_slab(size_t size)2773d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size)
2774d2eecb03STheodore Ts'o {
277551dfacdeSThomas Gleixner 	static DEFINE_MUTEX(jbd2_slab_create_mutex);
2776d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2777d2eecb03STheodore Ts'o 	size_t slab_size;
2778d2eecb03STheodore Ts'o 
2779d2eecb03STheodore Ts'o 	if (size == PAGE_SIZE)
2780d2eecb03STheodore Ts'o 		return 0;
2781d2eecb03STheodore Ts'o 
2782d2eecb03STheodore Ts'o 	if (i >= JBD2_MAX_SLABS)
2783d2eecb03STheodore Ts'o 		return -EINVAL;
2784d2eecb03STheodore Ts'o 
2785d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2786d2eecb03STheodore Ts'o 		i = 0;
278751dfacdeSThomas Gleixner 	mutex_lock(&jbd2_slab_create_mutex);
2788d2eecb03STheodore Ts'o 	if (jbd2_slab[i]) {
278951dfacdeSThomas Gleixner 		mutex_unlock(&jbd2_slab_create_mutex);
2790d2eecb03STheodore Ts'o 		return 0;	/* Already created */
2791d2eecb03STheodore Ts'o 	}
2792d2eecb03STheodore Ts'o 
2793d2eecb03STheodore Ts'o 	slab_size = 1 << (i+10);
2794d2eecb03STheodore Ts'o 	jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2795d2eecb03STheodore Ts'o 					 slab_size, 0, NULL);
279651dfacdeSThomas Gleixner 	mutex_unlock(&jbd2_slab_create_mutex);
2797d2eecb03STheodore Ts'o 	if (!jbd2_slab[i]) {
2798d2eecb03STheodore Ts'o 		printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2799d2eecb03STheodore Ts'o 		return -ENOMEM;
2800d2eecb03STheodore Ts'o 	}
2801d2eecb03STheodore Ts'o 	return 0;
2802d2eecb03STheodore Ts'o }
2803d2eecb03STheodore Ts'o 
get_slab(size_t size)2804d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size)
2805d2eecb03STheodore Ts'o {
2806d2eecb03STheodore Ts'o 	int i = order_base_2(size) - 10;
2807d2eecb03STheodore Ts'o 
2808d2eecb03STheodore Ts'o 	BUG_ON(i >= JBD2_MAX_SLABS);
2809d2eecb03STheodore Ts'o 	if (unlikely(i < 0))
2810d2eecb03STheodore Ts'o 		i = 0;
28118ac97b74SBill Pemberton 	BUG_ON(jbd2_slab[i] == NULL);
2812d2eecb03STheodore Ts'o 	return jbd2_slab[i];
2813d2eecb03STheodore Ts'o }
2814d2eecb03STheodore Ts'o 
jbd2_alloc(size_t size,gfp_t flags)2815d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags)
2816d2eecb03STheodore Ts'o {
2817d2eecb03STheodore Ts'o 	void *ptr;
2818d2eecb03STheodore Ts'o 
2819d2eecb03STheodore Ts'o 	BUG_ON(size & (size-1)); /* Must be a power of 2 */
2820d2eecb03STheodore Ts'o 
2821f2db1971SMichal Hocko 	if (size < PAGE_SIZE)
2822d2eecb03STheodore Ts'o 		ptr = kmem_cache_alloc(get_slab(size), flags);
2823f2db1971SMichal Hocko 	else
2824f2db1971SMichal Hocko 		ptr = (void *)__get_free_pages(flags, get_order(size));
2825d2eecb03STheodore Ts'o 
2826d2eecb03STheodore Ts'o 	/* Check alignment; SLUB has gotten this wrong in the past,
2827d2eecb03STheodore Ts'o 	 * and this can lead to user data corruption! */
2828d2eecb03STheodore Ts'o 	BUG_ON(((unsigned long) ptr) & (size-1));
2829d2eecb03STheodore Ts'o 
2830d2eecb03STheodore Ts'o 	return ptr;
2831d2eecb03STheodore Ts'o }
2832d2eecb03STheodore Ts'o 
jbd2_free(void * ptr,size_t size)2833d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size)
2834d2eecb03STheodore Ts'o {
2835f2db1971SMichal Hocko 	if (size < PAGE_SIZE)
2836d2eecb03STheodore Ts'o 		kmem_cache_free(get_slab(size), ptr);
2837f2db1971SMichal Hocko 	else
2838f2db1971SMichal Hocko 		free_pages((unsigned long)ptr, get_order(size));
2839d2eecb03STheodore Ts'o };
2840d2eecb03STheodore Ts'o 
2841d2eecb03STheodore Ts'o /*
2842470decc6SDave Kleikamp  * Journal_head storage management
2843470decc6SDave Kleikamp  */
2844e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache;
2845e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2846470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2847470decc6SDave Kleikamp #endif
2848470decc6SDave Kleikamp 
jbd2_journal_init_journal_head_cache(void)28490d52154bSChengguang Xu static int __init jbd2_journal_init_journal_head_cache(void)
2850470decc6SDave Kleikamp {
28510d52154bSChengguang Xu 	J_ASSERT(!jbd2_journal_head_cache);
2852a920e941SJohann Lombardi 	jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2853470decc6SDave Kleikamp 				sizeof(struct journal_head),
2854470decc6SDave Kleikamp 				0,		/* offset */
28555f0d5a3aSPaul E. McKenney 				SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU,
285620c2df83SPaul Mundt 				NULL);		/* ctor */
28571076d17aSAl Viro 	if (!jbd2_journal_head_cache) {
2858f2a44523SEryu Guan 		printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
28590d52154bSChengguang Xu 		return -ENOMEM;
2860470decc6SDave Kleikamp 	}
28610d52154bSChengguang Xu 	return 0;
2862470decc6SDave Kleikamp }
2863470decc6SDave Kleikamp 
jbd2_journal_destroy_journal_head_cache(void)28644185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void)
2865470decc6SDave Kleikamp {
2866f7f4bccbSMingming Cao 	kmem_cache_destroy(jbd2_journal_head_cache);
2867f7f4bccbSMingming Cao 	jbd2_journal_head_cache = NULL;
2868470decc6SDave Kleikamp }
2869470decc6SDave Kleikamp 
2870470decc6SDave Kleikamp /*
2871470decc6SDave Kleikamp  * journal_head splicing and dicing
2872470decc6SDave Kleikamp  */
journal_alloc_journal_head(void)2873470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void)
2874470decc6SDave Kleikamp {
2875470decc6SDave Kleikamp 	struct journal_head *ret;
2876470decc6SDave Kleikamp 
2877e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2878470decc6SDave Kleikamp 	atomic_inc(&nr_journal_heads);
2879470decc6SDave Kleikamp #endif
28805d9cf9c6SZheng Liu 	ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
28811076d17aSAl Viro 	if (!ret) {
2882cb3b3bf2SJan Kara 		jbd2_debug(1, "out of memory for journal_head\n");
2883670be5a7STheodore Ts'o 		pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
28847b506b10SMichal Hocko 		ret = kmem_cache_zalloc(jbd2_journal_head_cache,
28857b506b10SMichal Hocko 				GFP_NOFS | __GFP_NOFAIL);
2886470decc6SDave Kleikamp 	}
288746417064SThomas Gleixner 	if (ret)
288846417064SThomas Gleixner 		spin_lock_init(&ret->b_state_lock);
2889470decc6SDave Kleikamp 	return ret;
2890470decc6SDave Kleikamp }
2891470decc6SDave Kleikamp 
journal_free_journal_head(struct journal_head * jh)2892470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh)
2893470decc6SDave Kleikamp {
2894e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
2895470decc6SDave Kleikamp 	atomic_dec(&nr_journal_heads);
2896cd02ff0bSMingming Cao 	memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2897470decc6SDave Kleikamp #endif
2898f7f4bccbSMingming Cao 	kmem_cache_free(jbd2_journal_head_cache, jh);
2899470decc6SDave Kleikamp }
2900470decc6SDave Kleikamp 
2901470decc6SDave Kleikamp /*
2902470decc6SDave Kleikamp  * A journal_head is attached to a buffer_head whenever JBD has an
2903470decc6SDave Kleikamp  * interest in the buffer.
2904470decc6SDave Kleikamp  *
2905470decc6SDave Kleikamp  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2906470decc6SDave Kleikamp  * is set.  This bit is tested in core kernel code where we need to take
2907470decc6SDave Kleikamp  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
2908470decc6SDave Kleikamp  * there.
2909470decc6SDave Kleikamp  *
2910470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2911470decc6SDave Kleikamp  *
2912470decc6SDave Kleikamp  * When a buffer has its BH_JBD bit set it is immune from being released by
2913470decc6SDave Kleikamp  * core kernel code, mainly via ->b_count.
2914470decc6SDave Kleikamp  *
2915de1b7941SJan Kara  * A journal_head is detached from its buffer_head when the journal_head's
2916de1b7941SJan Kara  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2917de1b7941SJan Kara  * transaction (b_cp_transaction) hold their references to b_jcount.
2918470decc6SDave Kleikamp  *
2919470decc6SDave Kleikamp  * Various places in the kernel want to attach a journal_head to a buffer_head
2920470decc6SDave Kleikamp  * _before_ attaching the journal_head to a transaction.  To protect the
2921f7f4bccbSMingming Cao  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2922470decc6SDave Kleikamp  * journal_head's b_jcount refcount by one.  The caller must call
2923f7f4bccbSMingming Cao  * jbd2_journal_put_journal_head() to undo this.
2924470decc6SDave Kleikamp  *
2925470decc6SDave Kleikamp  * So the typical usage would be:
2926470decc6SDave Kleikamp  *
2927470decc6SDave Kleikamp  *	(Attach a journal_head if needed.  Increments b_jcount)
2928f7f4bccbSMingming Cao  *	struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2929470decc6SDave Kleikamp  *	...
2930de1b7941SJan Kara  *      (Get another reference for transaction)
2931de1b7941SJan Kara  *	jbd2_journal_grab_journal_head(bh);
2932470decc6SDave Kleikamp  *	jh->b_transaction = xxx;
2933de1b7941SJan Kara  *	(Put original reference)
2934f7f4bccbSMingming Cao  *	jbd2_journal_put_journal_head(jh);
2935470decc6SDave Kleikamp  */
2936470decc6SDave Kleikamp 
2937470decc6SDave Kleikamp /*
2938470decc6SDave Kleikamp  * Give a buffer_head a journal_head.
2939470decc6SDave Kleikamp  *
2940470decc6SDave Kleikamp  * May sleep.
2941470decc6SDave Kleikamp  */
jbd2_journal_add_journal_head(struct buffer_head * bh)2942f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2943470decc6SDave Kleikamp {
2944470decc6SDave Kleikamp 	struct journal_head *jh;
2945470decc6SDave Kleikamp 	struct journal_head *new_jh = NULL;
2946470decc6SDave Kleikamp 
2947470decc6SDave Kleikamp repeat:
29485d9cf9c6SZheng Liu 	if (!buffer_jbd(bh))
2949470decc6SDave Kleikamp 		new_jh = journal_alloc_journal_head();
2950470decc6SDave Kleikamp 
2951470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2952470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2953470decc6SDave Kleikamp 		jh = bh2jh(bh);
2954470decc6SDave Kleikamp 	} else {
2955470decc6SDave Kleikamp 		J_ASSERT_BH(bh,
2956470decc6SDave Kleikamp 			(atomic_read(&bh->b_count) > 0) ||
29570d22fe2fSMatthew Wilcox (Oracle) 			(bh->b_folio && bh->b_folio->mapping));
2958470decc6SDave Kleikamp 
2959470decc6SDave Kleikamp 		if (!new_jh) {
2960470decc6SDave Kleikamp 			jbd_unlock_bh_journal_head(bh);
2961470decc6SDave Kleikamp 			goto repeat;
2962470decc6SDave Kleikamp 		}
2963470decc6SDave Kleikamp 
2964470decc6SDave Kleikamp 		jh = new_jh;
2965470decc6SDave Kleikamp 		new_jh = NULL;		/* We consumed it */
2966470decc6SDave Kleikamp 		set_buffer_jbd(bh);
2967470decc6SDave Kleikamp 		bh->b_private = jh;
2968470decc6SDave Kleikamp 		jh->b_bh = bh;
2969470decc6SDave Kleikamp 		get_bh(bh);
2970470decc6SDave Kleikamp 		BUFFER_TRACE(bh, "added journal_head");
2971470decc6SDave Kleikamp 	}
2972470decc6SDave Kleikamp 	jh->b_jcount++;
2973470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2974470decc6SDave Kleikamp 	if (new_jh)
2975470decc6SDave Kleikamp 		journal_free_journal_head(new_jh);
2976470decc6SDave Kleikamp 	return bh->b_private;
2977470decc6SDave Kleikamp }
2978470decc6SDave Kleikamp 
2979470decc6SDave Kleikamp /*
2980470decc6SDave Kleikamp  * Grab a ref against this buffer_head's journal_head.  If it ended up not
2981470decc6SDave Kleikamp  * having a journal_head, return NULL
2982470decc6SDave Kleikamp  */
jbd2_journal_grab_journal_head(struct buffer_head * bh)2983f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2984470decc6SDave Kleikamp {
2985470decc6SDave Kleikamp 	struct journal_head *jh = NULL;
2986470decc6SDave Kleikamp 
2987470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
2988470decc6SDave Kleikamp 	if (buffer_jbd(bh)) {
2989470decc6SDave Kleikamp 		jh = bh2jh(bh);
2990470decc6SDave Kleikamp 		jh->b_jcount++;
2991470decc6SDave Kleikamp 	}
2992470decc6SDave Kleikamp 	jbd_unlock_bh_journal_head(bh);
2993470decc6SDave Kleikamp 	return jh;
2994470decc6SDave Kleikamp }
29954cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_grab_journal_head);
2996470decc6SDave Kleikamp 
__journal_remove_journal_head(struct buffer_head * bh)2997470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh)
2998470decc6SDave Kleikamp {
2999470decc6SDave Kleikamp 	struct journal_head *jh = bh2jh(bh);
3000470decc6SDave Kleikamp 
3001de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_transaction == NULL);
3002de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
3003de1b7941SJan Kara 	J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
3004470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
3005470decc6SDave Kleikamp 	J_ASSERT_BH(bh, buffer_jbd(bh));
3006470decc6SDave Kleikamp 	J_ASSERT_BH(bh, jh2bh(jh) == bh);
3007470decc6SDave Kleikamp 	BUFFER_TRACE(bh, "remove journal_head");
30087855a57dSThomas Gleixner 
30097855a57dSThomas Gleixner 	/* Unlink before dropping the lock */
3010470decc6SDave Kleikamp 	bh->b_private = NULL;
3011470decc6SDave Kleikamp 	jh->b_bh = NULL;	/* debug, really */
3012470decc6SDave Kleikamp 	clear_buffer_jbd(bh);
30137855a57dSThomas Gleixner }
30147855a57dSThomas Gleixner 
journal_release_journal_head(struct journal_head * jh,size_t b_size)30157855a57dSThomas Gleixner static void journal_release_journal_head(struct journal_head *jh, size_t b_size)
30167855a57dSThomas Gleixner {
30177855a57dSThomas Gleixner 	if (jh->b_frozen_data) {
30187855a57dSThomas Gleixner 		printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
30197855a57dSThomas Gleixner 		jbd2_free(jh->b_frozen_data, b_size);
30207855a57dSThomas Gleixner 	}
30217855a57dSThomas Gleixner 	if (jh->b_committed_data) {
30227855a57dSThomas Gleixner 		printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
30237855a57dSThomas Gleixner 		jbd2_free(jh->b_committed_data, b_size);
30247855a57dSThomas Gleixner 	}
3025470decc6SDave Kleikamp 	journal_free_journal_head(jh);
3026470decc6SDave Kleikamp }
3027470decc6SDave Kleikamp 
3028470decc6SDave Kleikamp /*
3029de1b7941SJan Kara  * Drop a reference on the passed journal_head.  If it fell to zero then
3030470decc6SDave Kleikamp  * release the journal_head from the buffer_head.
3031470decc6SDave Kleikamp  */
jbd2_journal_put_journal_head(struct journal_head * jh)3032f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh)
3033470decc6SDave Kleikamp {
3034470decc6SDave Kleikamp 	struct buffer_head *bh = jh2bh(jh);
3035470decc6SDave Kleikamp 
3036470decc6SDave Kleikamp 	jbd_lock_bh_journal_head(bh);
3037470decc6SDave Kleikamp 	J_ASSERT_JH(jh, jh->b_jcount > 0);
3038470decc6SDave Kleikamp 	--jh->b_jcount;
3039de1b7941SJan Kara 	if (!jh->b_jcount) {
3040470decc6SDave Kleikamp 		__journal_remove_journal_head(bh);
3041de1b7941SJan Kara 		jbd_unlock_bh_journal_head(bh);
30427855a57dSThomas Gleixner 		journal_release_journal_head(jh, bh->b_size);
3043470decc6SDave Kleikamp 		__brelse(bh);
30447855a57dSThomas Gleixner 	} else {
3045470decc6SDave Kleikamp 		jbd_unlock_bh_journal_head(bh);
3046470decc6SDave Kleikamp 	}
30477855a57dSThomas Gleixner }
30484cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_put_journal_head);
3049470decc6SDave Kleikamp 
3050470decc6SDave Kleikamp /*
3051c851ed54SJan Kara  * Initialize jbd inode head
3052c851ed54SJan Kara  */
jbd2_journal_init_jbd_inode(struct jbd2_inode * jinode,struct inode * inode)3053c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
3054c851ed54SJan Kara {
3055c851ed54SJan Kara 	jinode->i_transaction = NULL;
3056c851ed54SJan Kara 	jinode->i_next_transaction = NULL;
3057c851ed54SJan Kara 	jinode->i_vfs_inode = inode;
3058c851ed54SJan Kara 	jinode->i_flags = 0;
30596ba0e7dcSRoss Zwisler 	jinode->i_dirty_start = 0;
30606ba0e7dcSRoss Zwisler 	jinode->i_dirty_end = 0;
3061c851ed54SJan Kara 	INIT_LIST_HEAD(&jinode->i_list);
3062c851ed54SJan Kara }
3063c851ed54SJan Kara 
3064c851ed54SJan Kara /*
3065c851ed54SJan Kara  * Function to be called before we start removing inode from memory (i.e.,
3066c851ed54SJan Kara  * clear_inode() is a fine place to be called from). It removes inode from
3067c851ed54SJan Kara  * transaction's lists.
3068c851ed54SJan Kara  */
jbd2_journal_release_jbd_inode(journal_t * journal,struct jbd2_inode * jinode)3069c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal,
3070c851ed54SJan Kara 				    struct jbd2_inode *jinode)
3071c851ed54SJan Kara {
3072c851ed54SJan Kara 	if (!journal)
3073c851ed54SJan Kara 		return;
3074c851ed54SJan Kara restart:
3075c851ed54SJan Kara 	spin_lock(&journal->j_list_lock);
3076c851ed54SJan Kara 	/* Is commit writing out inode - we have to wait */
3077cb0d9d47SJan Kara 	if (jinode->i_flags & JI_COMMIT_RUNNING) {
3078c851ed54SJan Kara 		wait_queue_head_t *wq;
3079c851ed54SJan Kara 		DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
3080c851ed54SJan Kara 		wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
308121417136SIngo Molnar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
3082c851ed54SJan Kara 		spin_unlock(&journal->j_list_lock);
3083c851ed54SJan Kara 		schedule();
308421417136SIngo Molnar 		finish_wait(wq, &wait.wq_entry);
3085c851ed54SJan Kara 		goto restart;
3086c851ed54SJan Kara 	}
3087c851ed54SJan Kara 
3088c851ed54SJan Kara 	if (jinode->i_transaction) {
3089c851ed54SJan Kara 		list_del(&jinode->i_list);
3090c851ed54SJan Kara 		jinode->i_transaction = NULL;
3091c851ed54SJan Kara 	}
3092c851ed54SJan Kara 	spin_unlock(&journal->j_list_lock);
3093c851ed54SJan Kara }
3094c851ed54SJan Kara 
3095470decc6SDave Kleikamp 
30968e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS
30978e85fb3fSJohann Lombardi 
30988e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2"
30998e85fb3fSJohann Lombardi 
jbd2_create_jbd_stats_proc_entry(void)31008e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void)
31018e85fb3fSJohann Lombardi {
31028e85fb3fSJohann Lombardi 	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
31038e85fb3fSJohann Lombardi }
31048e85fb3fSJohann Lombardi 
jbd2_remove_jbd_stats_proc_entry(void)31058e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void)
31068e85fb3fSJohann Lombardi {
31078e85fb3fSJohann Lombardi 	if (proc_jbd2_stats)
31088e85fb3fSJohann Lombardi 		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
31098e85fb3fSJohann Lombardi }
31108e85fb3fSJohann Lombardi 
31118e85fb3fSJohann Lombardi #else
31128e85fb3fSJohann Lombardi 
31138e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
31148e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
31158e85fb3fSJohann Lombardi 
31168e85fb3fSJohann Lombardi #endif
31178e85fb3fSJohann Lombardi 
31188aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
3119470decc6SDave Kleikamp 
jbd2_journal_init_inode_cache(void)31200d52154bSChengguang Xu static int __init jbd2_journal_init_inode_cache(void)
3121470decc6SDave Kleikamp {
31220d52154bSChengguang Xu 	J_ASSERT(!jbd2_inode_cache);
31238aefcd55STheodore Ts'o 	jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
31240d52154bSChengguang Xu 	if (!jbd2_inode_cache) {
31250d52154bSChengguang Xu 		pr_emerg("JBD2: failed to create inode cache\n");
3126470decc6SDave Kleikamp 		return -ENOMEM;
3127470decc6SDave Kleikamp 	}
3128470decc6SDave Kleikamp 	return 0;
3129470decc6SDave Kleikamp }
3130470decc6SDave Kleikamp 
jbd2_journal_init_handle_cache(void)31310d52154bSChengguang Xu static int __init jbd2_journal_init_handle_cache(void)
31320d52154bSChengguang Xu {
31330d52154bSChengguang Xu 	J_ASSERT(!jbd2_handle_cache);
31340d52154bSChengguang Xu 	jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
31350d52154bSChengguang Xu 	if (!jbd2_handle_cache) {
31360d52154bSChengguang Xu 		printk(KERN_EMERG "JBD2: failed to create handle cache\n");
31370d52154bSChengguang Xu 		return -ENOMEM;
31380d52154bSChengguang Xu 	}
31390d52154bSChengguang Xu 	return 0;
31400d52154bSChengguang Xu }
31410d52154bSChengguang Xu 
jbd2_journal_destroy_inode_cache(void)31420d52154bSChengguang Xu static void jbd2_journal_destroy_inode_cache(void)
31430d52154bSChengguang Xu {
31440d52154bSChengguang Xu 	kmem_cache_destroy(jbd2_inode_cache);
31450d52154bSChengguang Xu 	jbd2_inode_cache = NULL;
31460d52154bSChengguang Xu }
31470d52154bSChengguang Xu 
jbd2_journal_destroy_handle_cache(void)3148f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void)
3149470decc6SDave Kleikamp {
3150f7f4bccbSMingming Cao 	kmem_cache_destroy(jbd2_handle_cache);
31518bdd5b60SWang Long 	jbd2_handle_cache = NULL;
3152470decc6SDave Kleikamp }
3153470decc6SDave Kleikamp 
3154470decc6SDave Kleikamp /*
3155470decc6SDave Kleikamp  * Module startup and shutdown
3156470decc6SDave Kleikamp  */
3157470decc6SDave Kleikamp 
journal_init_caches(void)3158470decc6SDave Kleikamp static int __init journal_init_caches(void)
3159470decc6SDave Kleikamp {
3160470decc6SDave Kleikamp 	int ret;
3161470decc6SDave Kleikamp 
31620d52154bSChengguang Xu 	ret = jbd2_journal_init_revoke_record_cache();
31630d52154bSChengguang Xu 	if (ret == 0)
31640d52154bSChengguang Xu 		ret = jbd2_journal_init_revoke_table_cache();
3165470decc6SDave Kleikamp 	if (ret == 0)
31664185a2acSYongqiang Yang 		ret = jbd2_journal_init_journal_head_cache();
3167470decc6SDave Kleikamp 	if (ret == 0)
31684185a2acSYongqiang Yang 		ret = jbd2_journal_init_handle_cache();
31690c2022ecSYongqiang Yang 	if (ret == 0)
31700d52154bSChengguang Xu 		ret = jbd2_journal_init_inode_cache();
31710d52154bSChengguang Xu 	if (ret == 0)
31720c2022ecSYongqiang Yang 		ret = jbd2_journal_init_transaction_cache();
3173470decc6SDave Kleikamp 	return ret;
3174470decc6SDave Kleikamp }
3175470decc6SDave Kleikamp 
jbd2_journal_destroy_caches(void)3176f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void)
3177470decc6SDave Kleikamp {
31780d52154bSChengguang Xu 	jbd2_journal_destroy_revoke_record_cache();
31790d52154bSChengguang Xu 	jbd2_journal_destroy_revoke_table_cache();
31804185a2acSYongqiang Yang 	jbd2_journal_destroy_journal_head_cache();
3181f7f4bccbSMingming Cao 	jbd2_journal_destroy_handle_cache();
31820d52154bSChengguang Xu 	jbd2_journal_destroy_inode_cache();
31830c2022ecSYongqiang Yang 	jbd2_journal_destroy_transaction_cache();
3184d2eecb03STheodore Ts'o 	jbd2_journal_destroy_slabs();
3185470decc6SDave Kleikamp }
3186470decc6SDave Kleikamp 
journal_init(void)3187470decc6SDave Kleikamp static int __init journal_init(void)
3188470decc6SDave Kleikamp {
3189470decc6SDave Kleikamp 	int ret;
3190470decc6SDave Kleikamp 
3191470decc6SDave Kleikamp 	BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
3192470decc6SDave Kleikamp 
3193470decc6SDave Kleikamp 	ret = journal_init_caches();
3194620de4e1SDuane Griffin 	if (ret == 0) {
31958e85fb3fSJohann Lombardi 		jbd2_create_jbd_stats_proc_entry();
3196620de4e1SDuane Griffin 	} else {
3197620de4e1SDuane Griffin 		jbd2_journal_destroy_caches();
3198620de4e1SDuane Griffin 	}
3199470decc6SDave Kleikamp 	return ret;
3200470decc6SDave Kleikamp }
3201470decc6SDave Kleikamp 
journal_exit(void)3202470decc6SDave Kleikamp static void __exit journal_exit(void)
3203470decc6SDave Kleikamp {
3204e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG
3205470decc6SDave Kleikamp 	int n = atomic_read(&nr_journal_heads);
3206470decc6SDave Kleikamp 	if (n)
320775685071SJan Kara 		printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
3208470decc6SDave Kleikamp #endif
32098e85fb3fSJohann Lombardi 	jbd2_remove_jbd_stats_proc_entry();
3210f7f4bccbSMingming Cao 	jbd2_journal_destroy_caches();
3211470decc6SDave Kleikamp }
3212470decc6SDave Kleikamp 
3213470decc6SDave Kleikamp MODULE_LICENSE("GPL");
3214470decc6SDave Kleikamp module_init(journal_init);
3215470decc6SDave Kleikamp module_exit(journal_exit);
3216470decc6SDave Kleikamp 
3217