VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   57 Tags
f51667685749e (Theodore Ts'o              2017-12-17 22:00:59 -0500    1) // SPDX-License-Identifier: GPL-2.0+
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    2) /*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700    3)  * linux/fs/jbd2/journal.c
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    4)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    5)  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    6)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    7)  * Copyright 1998 Red Hat corp --- All Rights Reserved
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    8)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    9)  * Generic filesystem journal-writing code; part of the ext2fs
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   10)  * journaling system.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   11)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   12)  * This file manages journals: areas of disk reserved for logging
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   13)  * transactional updates.  This includes the kernel journaling thread
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   14)  * which is responsible for scheduling updates to the log.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   15)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   16)  * We do not actually manage the physical storage of the journal in this
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   17)  * file: that is left to a per-journal policy function, which allows us
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   18)  * to store the journal within a filesystem-specified area for ext2
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   19)  * journaling (ext2 can use a reserved inode for storing the log).
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   20)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   21) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   22) #include <linux/module.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   23) #include <linux/time.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   24) #include <linux/fs.h>
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   25) #include <linux/jbd2.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   26) #include <linux/errno.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   27) #include <linux/slab.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   28) #include <linux/init.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   29) #include <linux/mm.h>
7dfb71030f763 (Nigel Cunningham           2006-12-06 20:34:23 -0800   30) #include <linux/freezer.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   31) #include <linux/pagemap.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   32) #include <linux/kthread.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   33) #include <linux/poison.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   34) #include <linux/proc_fs.h>
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500   35) #include <linux/seq_file.h>
c225aa57ff4ff (Simon Holm Thøgersen       2009-01-11 22:34:01 -0500   36) #include <linux/math64.h>
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400   37) #include <linux/hash.h>
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500   38) #include <linux/log2.h>
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500   39) #include <linux/vmalloc.h>
47def82672b3b (Theodore Ts'o              2010-07-27 11:56:05 -0400   40) #include <linux/backing-dev.h>
39e3ac2599a5f (Brian King                 2010-10-27 21:25:12 -0400   41) #include <linux/bitops.h>
670be5a78ac7c (Theodore Ts'o              2010-12-17 10:44:16 -0500   42) #include <linux/ratelimit.h>
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700   43) #include <linux/sched/mm.h>
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400   44) 
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400   45) #define CREATE_TRACE_POINTS
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400   46) #include <trace/events/jbd2.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   47) 
7c0f6ba682b9c (Linus Torvalds             2016-12-24 11:46:01 -0800   48) #include <linux/uaccess.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   49) #include <asm/page.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   50) 
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   51) #ifdef CONFIG_JBD2_DEBUG
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   52) ushort jbd2_journal_enable_debug __read_mostly;
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   53) EXPORT_SYMBOL(jbd2_journal_enable_debug);
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   54) 
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   55) module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   56) MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   57) #endif
b6e96d0067d81 (Theodore Ts'o              2013-02-09 16:29:20 -0500   58) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   59) EXPORT_SYMBOL(jbd2_journal_extend);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   60) EXPORT_SYMBOL(jbd2_journal_stop);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   61) EXPORT_SYMBOL(jbd2_journal_lock_updates);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   62) EXPORT_SYMBOL(jbd2_journal_unlock_updates);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   63) EXPORT_SYMBOL(jbd2_journal_get_write_access);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   64) EXPORT_SYMBOL(jbd2_journal_get_create_access);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   65) EXPORT_SYMBOL(jbd2_journal_get_undo_access);
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700   66) EXPORT_SYMBOL(jbd2_journal_set_triggers);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   67) EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   68) EXPORT_SYMBOL(jbd2_journal_forget);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   69) EXPORT_SYMBOL(jbd2_journal_flush);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   70) EXPORT_SYMBOL(jbd2_journal_revoke);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   71) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   72) EXPORT_SYMBOL(jbd2_journal_init_dev);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   73) EXPORT_SYMBOL(jbd2_journal_init_inode);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   74) EXPORT_SYMBOL(jbd2_journal_check_used_features);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   75) EXPORT_SYMBOL(jbd2_journal_check_available_features);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   76) EXPORT_SYMBOL(jbd2_journal_set_features);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   77) EXPORT_SYMBOL(jbd2_journal_load);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   78) EXPORT_SYMBOL(jbd2_journal_destroy);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   79) EXPORT_SYMBOL(jbd2_journal_abort);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   80) EXPORT_SYMBOL(jbd2_journal_errno);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   81) EXPORT_SYMBOL(jbd2_journal_ack_err);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   82) EXPORT_SYMBOL(jbd2_journal_clear_err);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   83) EXPORT_SYMBOL(jbd2_log_wait_commit);
3b799d15f2622 (Theodore Ts'o              2009-12-09 20:42:53 -0500   84) EXPORT_SYMBOL(jbd2_log_start_commit);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   85) EXPORT_SYMBOL(jbd2_journal_start_commit);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   86) EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   87) EXPORT_SYMBOL(jbd2_journal_wipe);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   88) EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   89) EXPORT_SYMBOL(jbd2_journal_invalidatepage);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   90) EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   91) EXPORT_SYMBOL(jbd2_journal_force_commit);
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400   92) EXPORT_SYMBOL(jbd2_journal_inode_ranged_write);
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400   93) EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait);
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300   94) EXPORT_SYMBOL(jbd2_journal_submit_inode_data_buffers);
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300   95) EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400   96) EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400   97) EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400   98) EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500   99) EXPORT_SYMBOL(jbd2_inode_cache);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  100) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500  101) static int jbd2_journal_create_slab(size_t slab_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  102) 
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  103) #ifdef CONFIG_JBD2_DEBUG
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  104) void __jbd2_debug(int level, const char *file, const char *func,
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  105) 		  unsigned int line, const char *fmt, ...)
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  106) {
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  107) 	struct va_format vaf;
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  108) 	va_list args;
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  109) 
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  110) 	if (level > jbd2_journal_enable_debug)
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  111) 		return;
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  112) 	va_start(args, fmt);
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  113) 	vaf.fmt = fmt;
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  114) 	vaf.va = &args;
9196f57151ffe (Wang Shilong               2018-05-20 22:14:29 -0400  115) 	printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf);
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  116) 	va_end(args);
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  117) }
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  118) EXPORT_SYMBOL(__jbd2_debug);
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  119) #endif
169f1a2a87aae (Paul Gortmaker             2013-06-12 23:04:04 -0400  120) 
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  121) /* Checksumming functions */
7747e6d028b89 (Rashika Kheria             2014-02-17 20:49:04 -0500  122) static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  123) {
8595798ca34d1 (Darrick J. Wong            2015-10-15 10:30:36 -0400  124) 	if (!jbd2_journal_has_csum_v2or3_feature(j))
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  125) 		return 1;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  126) 
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  127) 	return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  128) }
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400  129) 
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  130) static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  131) {
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  132) 	__u32 csum;
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  133) 	__be32 old_csum;
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  134) 
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  135) 	old_csum = sb->s_checksum;
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  136) 	sb->s_checksum = 0;
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  137) 	csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  138) 	sb->s_checksum = old_csum;
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  139) 
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  140) 	return cpu_to_be32(csum);
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  141) }
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400  142) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  143) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  144)  * Helper function used to manage commit timeouts
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  145)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  146) 
e3c957885e428 (Kees Cook                  2017-10-18 12:40:28 -0400  147) static void commit_timeout(struct timer_list *t)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  148) {
e3c957885e428 (Kees Cook                  2017-10-18 12:40:28 -0400  149) 	journal_t *journal = from_timer(journal, t, j_commit_timer);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  150) 
e3c957885e428 (Kees Cook                  2017-10-18 12:40:28 -0400  151) 	wake_up_process(journal->j_task);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  152) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  153) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  154) /*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  155)  * kjournald2: The main thread function used to manage a logging device
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  156)  * journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  157)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  158)  * This kernel thread is responsible for two things:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  159)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  160)  * 1) COMMIT:  Every so often we need to commit the current state of the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  161)  *    filesystem to disk.  The journal thread is responsible for writing
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  162)  *    all of the metadata buffers to disk. If a fast commit is ongoing
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  163)  *    journal thread waits until it's done and then continues from
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  164)  *    there on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  165)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  166)  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  167)  *    of the data in that part of the log has been rewritten elsewhere on
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  168)  *    the disk.  Flushing these old buffers to reclaim space in the log is
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  169)  *    known as checkpointing, and this thread is responsible for that job.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  170)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  171) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  172) static int kjournald2(void *arg)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  173) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  174) 	journal_t *journal = arg;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  175) 	transaction_t *transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  176) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  177) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  178) 	 * Set up an interval timer which can be used to trigger a commit wakeup
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  179) 	 * after the commit interval expires
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  180) 	 */
e3c957885e428 (Kees Cook                  2017-10-18 12:40:28 -0400  181) 	timer_setup(&journal->j_commit_timer, commit_timeout, 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  182) 
35c80422afc83 (Nigel Cunningham           2012-02-03 19:59:41 +1100  183) 	set_freezable();
35c80422afc83 (Nigel Cunningham           2012-02-03 19:59:41 +1100  184) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  185) 	/* Record that the journal thread is running */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  186) 	journal->j_task = current;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  187) 	wake_up(&journal->j_wait_done_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  188) 
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  189) 	/*
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  190) 	 * Make sure that no allocations from this kernel thread will ever
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  191) 	 * recurse to the fs layer because we are responsible for the
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  192) 	 * transaction commit and any fs involvement might get stuck waiting for
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  193) 	 * the trasn. commit.
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  194) 	 */
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  195) 	memalloc_nofs_save();
eb52da3f48f5b (Michal Hocko               2017-05-03 14:53:26 -0700  196) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  197) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  198) 	 * And now, wait forever for commit wakeup events.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  199) 	 */
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  200) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  201) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  202) loop:
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  203) 	if (journal->j_flags & JBD2_UNMOUNT)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  204) 		goto end_loop;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  205) 
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  206) 	jbd_debug(1, "commit_sequence=%u, commit_request=%u\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  207) 		journal->j_commit_sequence, journal->j_commit_request);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  208) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  209) 	if (journal->j_commit_sequence != journal->j_commit_request) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  210) 		jbd_debug(1, "OK, requests differ\n");
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  211) 		write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  212) 		del_timer_sync(&journal->j_commit_timer);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  213) 		jbd2_journal_commit_transaction(journal);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  214) 		write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  215) 		goto loop;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  216) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  217) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  218) 	wake_up(&journal->j_wait_done_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  219) 	if (freezing(current)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  220) 		/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  221) 		 * The simpler the better. Flushing journal isn't a
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  222) 		 * good idea, because that depends on threads that may
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  223) 		 * be already stopped.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  224) 		 */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  225) 		jbd_debug(1, "Now suspending kjournald2\n");
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  226) 		write_unlock(&journal->j_state_lock);
a0acae0e886d4 (Tejun Heo                  2011-11-21 12:32:22 -0800  227) 		try_to_freeze();
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  228) 		write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  229) 	} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  230) 		/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  231) 		 * We assume on resume that commits are already there,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  232) 		 * so we don't sleep
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  233) 		 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  234) 		DEFINE_WAIT(wait);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  235) 		int should_sleep = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  236) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  237) 		prepare_to_wait(&journal->j_wait_commit, &wait,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  238) 				TASK_INTERRUPTIBLE);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  239) 		if (journal->j_commit_sequence != journal->j_commit_request)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  240) 			should_sleep = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  241) 		transaction = journal->j_running_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  242) 		if (transaction && time_after_eq(jiffies,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  243) 						transaction->t_expires))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  244) 			should_sleep = 0;
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  245) 		if (journal->j_flags & JBD2_UNMOUNT)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  246) 			should_sleep = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  247) 		if (should_sleep) {
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  248) 			write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  249) 			schedule();
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  250) 			write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  251) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  252) 		finish_wait(&journal->j_wait_commit, &wait);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  253) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  254) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  255) 	jbd_debug(1, "kjournald2 wakes\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  256) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  257) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  258) 	 * Were we woken up by a commit wakeup event?
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  259) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  260) 	transaction = journal->j_running_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  261) 	if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  262) 		journal->j_commit_request = transaction->t_tid;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  263) 		jbd_debug(1, "woke because of timeout\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  264) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  265) 	goto loop;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  266) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  267) end_loop:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  268) 	del_timer_sync(&journal->j_commit_timer);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  269) 	journal->j_task = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  270) 	wake_up(&journal->j_wait_done_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  271) 	jbd_debug(1, "Journal thread exiting.\n");
dbfcef6b0f401 (Sahitya Tummala            2017-02-01 20:49:35 -0500  272) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  273) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  274) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  275) 
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  276) static int jbd2_journal_start_thread(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  277) {
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  278) 	struct task_struct *t;
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  279) 
90576c0b9a0b5 (Theodore Ts'o              2009-09-29 15:51:30 -0400  280) 	t = kthread_run(kjournald2, journal, "jbd2/%s",
90576c0b9a0b5 (Theodore Ts'o              2009-09-29 15:51:30 -0400  281) 			journal->j_devname);
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  282) 	if (IS_ERR(t))
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  283) 		return PTR_ERR(t);
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  284) 
1076d17ac70d1 (Al Viro                    2008-03-29 03:07:18 +0000  285) 	wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
97f067846786d (Pavel Emelianov            2007-05-08 00:30:42 -0700  286) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  287) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  288) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  289) static void journal_kill_thread(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  290) {
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  291) 	write_lock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  292) 	journal->j_flags |= JBD2_UNMOUNT;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  293) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  294) 	while (journal->j_task) {
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  295) 		write_unlock(&journal->j_state_lock);
3469a32a1e948 (Theodore Ts'o              2014-03-08 19:11:36 -0500  296) 		wake_up(&journal->j_wait_commit);
1076d17ac70d1 (Al Viro                    2008-03-29 03:07:18 +0000  297) 		wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  298) 		write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  299) 	}
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  300) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  301) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  302) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  303) /*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  304)  * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  305)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  306)  * Writes a metadata buffer to a given disk block.  The actual IO is not
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  307)  * performed but a new buffer_head is constructed which labels the data
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  308)  * to be written with the correct destination disk block.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  309)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  310)  * Any magic-number escaping which needs to be done will cause a
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  311)  * copy-out here.  If the buffer happens to start with the
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  312)  * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  313)  * magic number is only written to the log for descripter blocks.  In
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  314)  * this case, we copy the data and replace the first word with 0, and we
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  315)  * return a result code which indicates that this buffer needs to be
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  316)  * marked as an escaped buffer in the corresponding log descriptor
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  317)  * block.  The missing word can then be restored when the block is read
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  318)  * during recovery.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  319)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  320)  * If the source buffer has already been modified by a new transaction
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  321)  * since we took the last commit snapshot, we use the frozen copy of
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  322)  * that data for IO. If we end up using the existing buffer_head's data
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  323)  * for the write, then we have to make sure nobody modifies it while the
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  324)  * IO is in progress. do_get_write_access() handles this.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  325)  *
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  326)  * The function returns a pointer to the buffer_head to be used for IO.
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  327)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  328)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  329)  * Return value:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  330)  *  <0: Error
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  331)  * >=0: Finished OK
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  332)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  333)  * On success:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  334)  * Bit 0 set == escape performed on the data
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  335)  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  336)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  337) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  338) int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  339) 				  struct journal_head  *jh_in,
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  340) 				  struct buffer_head **bh_out,
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  341) 				  sector_t blocknr)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  342) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  343) 	int need_copy_out = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  344) 	int done_copy_out = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  345) 	int do_escape = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  346) 	char *mapped_data;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  347) 	struct buffer_head *new_bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  348) 	struct page *new_page;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  349) 	unsigned int new_offset;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  350) 	struct buffer_head *bh_in = jh2bh(jh_in);
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  351) 	journal_t *journal = transaction->t_journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  352) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  353) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  354) 	 * The buffer really shouldn't be locked: only the current committing
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  355) 	 * transaction is allowed to write it, so nobody else is allowed
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  356) 	 * to do any IO.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  357) 	 *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  358) 	 * akpm: except if we're journalling data, and write() output is
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  359) 	 * also part of a shared mapping, and another thread has
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  360) 	 * decided to launch a writepage() against this buffer.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  361) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  362) 	J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  363) 
6ccaf3e2f302b (Michal Hocko               2015-06-08 10:53:10 -0400  364) 	new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
47def82672b3b (Theodore Ts'o              2010-07-27 11:56:05 -0400  365) 
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  366) 	/* keep subsequent assertions sane */
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  367) 	atomic_set(&new_bh->b_count, 1);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  368) 
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  369) 	spin_lock(&jh_in->b_state_lock);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  370) repeat:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  371) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  372) 	 * If a new transaction has already done a buffer copy-out, then
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  373) 	 * we use that version of the data for the commit.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  374) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  375) 	if (jh_in->b_frozen_data) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  376) 		done_copy_out = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  377) 		new_page = virt_to_page(jh_in->b_frozen_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  378) 		new_offset = offset_in_page(jh_in->b_frozen_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  379) 	} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  380) 		new_page = jh2bh(jh_in)->b_page;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  381) 		new_offset = offset_in_page(jh2bh(jh_in)->b_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  382) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  383) 
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  384) 	mapped_data = kmap_atomic(new_page);
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  385) 	/*
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  386) 	 * Fire data frozen trigger if data already wasn't frozen.  Do this
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  387) 	 * before checking for escaping, as the trigger may modify the magic
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  388) 	 * offset.  If a copy-out happens afterwards, it will have the correct
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  389) 	 * data in the buffer.
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  390) 	 */
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  391) 	if (!done_copy_out)
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  392) 		jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
13ceef099edd2 (Jan Kara                   2010-07-14 07:56:33 +0200  393) 					   jh_in->b_triggers);
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  394) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  395) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  396) 	 * Check for escaping
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  397) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  398) 	if (*((__be32 *)(mapped_data + new_offset)) ==
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  399) 				cpu_to_be32(JBD2_MAGIC_NUMBER)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  400) 		need_copy_out = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  401) 		do_escape = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  402) 	}
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  403) 	kunmap_atomic(mapped_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  404) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  405) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  406) 	 * Do we need to do a data copy?
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  407) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  408) 	if (need_copy_out && !done_copy_out) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  409) 		char *tmp;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  410) 
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  411) 		spin_unlock(&jh_in->b_state_lock);
af1e76d6b3f37 (Mingming Cao               2007-10-16 18:38:25 -0400  412) 		tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  413) 		if (!tmp) {
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  414) 			brelse(new_bh);
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  415) 			return -ENOMEM;
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  416) 		}
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  417) 		spin_lock(&jh_in->b_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  418) 		if (jh_in->b_frozen_data) {
af1e76d6b3f37 (Mingming Cao               2007-10-16 18:38:25 -0400  419) 			jbd2_free(tmp, bh_in->b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  420) 			goto repeat;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  421) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  422) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  423) 		jh_in->b_frozen_data = tmp;
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  424) 		mapped_data = kmap_atomic(new_page);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  425) 		memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  426) 		kunmap_atomic(mapped_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  427) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  428) 		new_page = virt_to_page(tmp);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  429) 		new_offset = offset_in_page(tmp);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  430) 		done_copy_out = 1;
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  431) 
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  432) 		/*
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  433) 		 * This isn't strictly necessary, as we're using frozen
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  434) 		 * data for the escaping, but it keeps consistency with
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  435) 		 * b_frozen_data usage.
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  436) 		 */
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  437) 		jh_in->b_frozen_triggers = jh_in->b_triggers;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  438) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  439) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  440) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  441) 	 * Did we need to do an escaping?  Now we've done all the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  442) 	 * copying, we can finally do so.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  443) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  444) 	if (do_escape) {
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  445) 		mapped_data = kmap_atomic(new_page);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  446) 		*((unsigned int *)(mapped_data + new_offset)) = 0;
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  447) 		kunmap_atomic(mapped_data);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  448) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  449) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  450) 	set_bh_page(new_bh, new_page, new_offset);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  451) 	new_bh->b_size = bh_in->b_size;
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  452) 	new_bh->b_bdev = journal->j_dev;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  453) 	new_bh->b_blocknr = blocknr;
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400  454) 	new_bh->b_private = bh_in;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  455) 	set_buffer_mapped(new_bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  456) 	set_buffer_dirty(new_bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  457) 
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  458) 	*bh_out = new_bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  459) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  460) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  461) 	 * The to-be-written buffer needs to get moved to the io queue,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  462) 	 * and the original buffer whose contents we are shadowing or
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  463) 	 * copying is moved to the transaction's shadow queue.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  464) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  465) 	JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  466) 	spin_lock(&journal->j_list_lock);
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  467) 	__jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  468) 	spin_unlock(&journal->j_list_lock);
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400  469) 	set_buffer_shadow(bh_in);
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  470) 	spin_unlock(&jh_in->b_state_lock);
96577c4382769 (dingdinghua                2009-07-13 17:55:35 -0400  471) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  472) 	return do_escape | (done_copy_out << 1);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  473) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  474) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  475) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  476)  * Allocation code for the journal file.  Manage the space left in the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  477)  * journal, so that we can begin checkpointing when appropriate.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  478)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  479) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  480) /*
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  481)  * Called with j_state_lock locked for writing.
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  482)  * Returns true if a transaction commit was started.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  483)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  484) int __jbd2_log_start_commit(journal_t *journal, tid_t target)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  485) {
e7b04ac00ee27 (Eric Sandeen               2013-01-30 00:39:28 -0500  486) 	/* Return if the txn has already requested to be committed */
e7b04ac00ee27 (Eric Sandeen               2013-01-30 00:39:28 -0500  487) 	if (journal->j_commit_request == target)
e7b04ac00ee27 (Eric Sandeen               2013-01-30 00:39:28 -0500  488) 		return 0;
e7b04ac00ee27 (Eric Sandeen               2013-01-30 00:39:28 -0500  489) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  490) 	/*
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  491) 	 * The only transaction we can possibly wait upon is the
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  492) 	 * currently running transaction (if it exists).  Otherwise,
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  493) 	 * the target tid must be an old one.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  494) 	 */
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  495) 	if (journal->j_running_transaction &&
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  496) 	    journal->j_running_transaction->t_tid == target) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  497) 		/*
bcf3d0bcff9c2 (Andrea Gelmini             2010-10-16 15:19:14 +0200  498) 		 * We want a new commit: OK, mark the request and wakeup the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  499) 		 * commit thread.  We do _not_ do the commit ourselves.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  500) 		 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  501) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  502) 		journal->j_commit_request = target;
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  503) 		jbd_debug(1, "JBD2: requesting commit %u/%u\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  504) 			  journal->j_commit_request,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  505) 			  journal->j_commit_sequence);
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  506) 		journal->j_running_transaction->t_requested = jiffies;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  507) 		wake_up(&journal->j_wait_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  508) 		return 1;
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  509) 	} else if (!tid_geq(journal->j_commit_request, target))
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  510) 		/* This should never happen, but if it does, preserve
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  511) 		   the evidence before kjournald goes into a loop and
deeeaf13b2914 (Theodore Ts'o              2011-05-01 18:16:26 -0400  512) 		   increments j_commit_sequence beyond all recognition. */
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  513) 		WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
1be2add685181 (Theodore Ts'o              2011-05-08 19:37:54 -0400  514) 			  journal->j_commit_request,
1be2add685181 (Theodore Ts'o              2011-05-08 19:37:54 -0400  515) 			  journal->j_commit_sequence,
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  516) 			  target, journal->j_running_transaction ?
1be2add685181 (Theodore Ts'o              2011-05-08 19:37:54 -0400  517) 			  journal->j_running_transaction->t_tid : 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  518) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  519) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  520) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  521) int jbd2_log_start_commit(journal_t *journal, tid_t tid)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  522) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  523) 	int ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  524) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  525) 	write_lock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  526) 	ret = __jbd2_log_start_commit(journal, tid);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  527) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  528) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  529) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  530) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  531) /*
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  532)  * Force and wait any uncommitted transactions.  We can only force the running
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  533)  * transaction if we don't have an active handle, otherwise, we will deadlock.
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  534)  * Returns: <0 in case of error,
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  535)  *           0 if nothing to commit,
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  536)  *           1 if transaction was successfully committed.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  537)  */
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  538) static int __jbd2_journal_force_commit(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  539) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  540) 	transaction_t *transaction = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  541) 	tid_t tid;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  542) 	int need_to_start = 0, ret = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  543) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  544) 	read_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  545) 	if (journal->j_running_transaction && !current->journal_info) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  546) 		transaction = journal->j_running_transaction;
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  547) 		if (!tid_geq(journal->j_commit_request, transaction->t_tid))
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  548) 			need_to_start = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  549) 	} else if (journal->j_committing_transaction)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  550) 		transaction = journal->j_committing_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  551) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  552) 	if (!transaction) {
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  553) 		/* Nothing to commit */
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  554) 		read_unlock(&journal->j_state_lock);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  555) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  556) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  557) 	tid = transaction->t_tid;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  558) 	read_unlock(&journal->j_state_lock);
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  559) 	if (need_to_start)
e44718318004a (Theodore Ts'o              2011-02-12 08:18:24 -0500  560) 		jbd2_log_start_commit(journal, tid);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  561) 	ret = jbd2_log_wait_commit(journal, tid);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  562) 	if (!ret)
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  563) 		ret = 1;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  564) 
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  565) 	return ret;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  566) }
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  567) 
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  568) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  569)  * jbd2_journal_force_commit_nested - Force and wait upon a commit if the
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  570)  * calling process is not within transaction.
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  571)  *
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  572)  * @journal: journal to force
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  573)  * Returns true if progress was made.
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  574)  *
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  575)  * This is used for forcing out undo-protected data which contains
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  576)  * bitmaps, when the fs is running out of space.
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  577)  */
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  578) int jbd2_journal_force_commit_nested(journal_t *journal)
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  579) {
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  580) 	int ret;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  581) 
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  582) 	ret = __jbd2_journal_force_commit(journal);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  583) 	return ret > 0;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  584) }
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  585) 
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  586) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100  587)  * jbd2_journal_force_commit() - force any uncommitted transactions
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  588)  * @journal: journal to force
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  589)  *
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  590)  * Caller want unconditional commit. We can only force the running transaction
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  591)  * if we don't have an active handle, otherwise, we will deadlock.
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  592)  */
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  593) int jbd2_journal_force_commit(journal_t *journal)
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  594) {
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  595) 	int ret;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  596) 
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  597) 	J_ASSERT(!current->journal_info);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  598) 	ret = __jbd2_journal_force_commit(journal);
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  599) 	if (ret > 0)
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  600) 		ret = 0;
9ff8644624772 (Dmitry Monakhov            2013-06-12 22:24:07 -0400  601) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  602) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  603) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  604) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  605)  * Start a commit of the current running transaction (if any).  Returns true
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  606)  * if a transaction is going to be committed (or is currently already
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  607)  * committing), and fills its tid in at *ptid
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  608)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  609) int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  610) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  611) 	int ret = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  612) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  613) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  614) 	if (journal->j_running_transaction) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  615) 		tid_t tid = journal->j_running_transaction->t_tid;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  616) 
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  617) 		__jbd2_log_start_commit(journal, tid);
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  618) 		/* There's a running transaction and we've just made sure
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  619) 		 * it's commit has been scheduled. */
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  620) 		if (ptid)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  621) 			*ptid = tid;
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  622) 		ret = 1;
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  623) 	} else if (journal->j_committing_transaction) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  624) 		/*
12810ad70858a (Artem Bityutskiy           2012-07-25 18:12:07 +0300  625) 		 * If commit has been started, then we have to wait for
12810ad70858a (Artem Bityutskiy           2012-07-25 18:12:07 +0300  626) 		 * completion of that transaction.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  627) 		 */
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  628) 		if (ptid)
c88ccea314397 (Jan Kara                   2009-02-10 11:27:46 -0500  629) 			*ptid = journal->j_committing_transaction->t_tid;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  630) 		ret = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  631) 	}
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  632) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  633) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  634) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  635) 
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  636) /*
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  637)  * Return 1 if a given transaction has not yet sent barrier request
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  638)  * connected with a transaction commit. If 0 is returned, transaction
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  639)  * may or may not have sent the barrier. Used to avoid sending barrier
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  640)  * twice in common cases.
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  641)  */
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  642) int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  643) {
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  644) 	int ret = 0;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  645) 	transaction_t *commit_trans;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  646) 
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  647) 	if (!(journal->j_flags & JBD2_BARRIER))
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  648) 		return 0;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  649) 	read_lock(&journal->j_state_lock);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  650) 	/* Transaction already committed? */
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  651) 	if (tid_geq(journal->j_commit_sequence, tid))
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  652) 		goto out;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  653) 	commit_trans = journal->j_committing_transaction;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  654) 	if (!commit_trans || commit_trans->t_tid != tid) {
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  655) 		ret = 1;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  656) 		goto out;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  657) 	}
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  658) 	/*
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  659) 	 * Transaction is being committed and we already proceeded to
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  660) 	 * submitting a flush to fs partition?
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  661) 	 */
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  662) 	if (journal->j_fs_dev != journal->j_dev) {
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  663) 		if (!commit_trans->t_need_data_flush ||
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  664) 		    commit_trans->t_state >= T_COMMIT_DFLUSH)
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  665) 			goto out;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  666) 	} else {
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  667) 		if (commit_trans->t_state >= T_COMMIT_JFLUSH)
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  668) 			goto out;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  669) 	}
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  670) 	ret = 1;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  671) out:
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  672) 	read_unlock(&journal->j_state_lock);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  673) 	return ret;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  674) }
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  675) EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  676) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  677) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  678)  * Wait for a specified commit to complete.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  679)  * The caller may not hold the journal lock.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  680)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  681) int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  682) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  683) 	int err = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  684) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  685) 	read_lock(&journal->j_state_lock);
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  686) #ifdef CONFIG_PROVE_LOCKING
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  687) 	/*
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  688) 	 * Some callers make sure transaction is already committing and in that
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  689) 	 * case we cannot block on open handles anymore. So don't warn in that
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  690) 	 * case.
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  691) 	 */
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  692) 	if (tid_gt(tid, journal->j_commit_sequence) &&
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  693) 	    (!journal->j_committing_transaction ||
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  694) 	     journal->j_committing_transaction->t_tid != tid)) {
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  695) 		read_unlock(&journal->j_state_lock);
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  696) 		jbd2_might_wait_for_commit(journal);
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  697) 		read_lock(&journal->j_state_lock);
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  698) 	}
c52c47e4b4fbe (Jan Kara                   2017-04-29 20:12:16 -0400  699) #endif
e23291b9120c1 (Jose R. Santos             2007-07-18 08:57:06 -0400  700) #ifdef CONFIG_JBD2_DEBUG
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  701) 	if (!tid_geq(journal->j_commit_request, tid)) {
75685071cd5b2 (Jan Kara                   2013-12-08 21:13:59 -0500  702) 		printk(KERN_ERR
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  703) 		       "%s: error: j_commit_request=%u, tid=%u\n",
329d291f50d53 (Harvey Harrison            2008-04-17 10:38:59 -0400  704) 		       __func__, journal->j_commit_request, tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  705) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  706) #endif
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  707) 	while (tid_gt(tid, journal->j_commit_sequence)) {
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400  708) 		jbd_debug(1, "JBD2: want %u, j_commit_sequence=%u\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  709) 				  tid, journal->j_commit_sequence);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  710) 		read_unlock(&journal->j_state_lock);
3469a32a1e948 (Theodore Ts'o              2014-03-08 19:11:36 -0500  711) 		wake_up(&journal->j_wait_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  712) 		wait_event(journal->j_wait_done_commit,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  713) 				!tid_gt(tid, journal->j_commit_sequence));
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  714) 		read_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  715) 	}
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  716) 	read_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  717) 
75685071cd5b2 (Jan Kara                   2013-12-08 21:13:59 -0500  718) 	if (unlikely(is_journal_aborted(journal)))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  719) 		err = -EIO;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  720) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  721) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  722) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  723) /*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  724)  * Start a fast commit. If there's an ongoing fast or full commit wait for
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  725)  * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  726)  * if a fast commit is not needed, either because there's an already a commit
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  727)  * going on or this tid has already been committed. Returns -EINVAL if no jbd2
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  728)  * commit has yet been performed.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  729)  */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  730) int jbd2_fc_begin_commit(journal_t *journal, tid_t tid)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  731) {
87a144f093801 (Harshad Shirwadkar         2020-11-05 19:59:10 -0800  732) 	if (unlikely(is_journal_aborted(journal)))
87a144f093801 (Harshad Shirwadkar         2020-11-05 19:59:10 -0800  733) 		return -EIO;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  734) 	/*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  735) 	 * Fast commits only allowed if at least one full commit has
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  736) 	 * been processed.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  737) 	 */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  738) 	if (!journal->j_stats.ts_tid)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  739) 		return -EINVAL;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  740) 
480f89d553260 (Harshad Shirwadkar         2020-11-05 19:59:01 -0800  741) 	write_lock(&journal->j_state_lock);
480f89d553260 (Harshad Shirwadkar         2020-11-05 19:59:01 -0800  742) 	if (tid <= journal->j_commit_sequence) {
480f89d553260 (Harshad Shirwadkar         2020-11-05 19:59:01 -0800  743) 		write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  744) 		return -EALREADY;
480f89d553260 (Harshad Shirwadkar         2020-11-05 19:59:01 -0800  745) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  746) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  747) 	if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  748) 	    (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  749) 		DEFINE_WAIT(wait);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  750) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  751) 		prepare_to_wait(&journal->j_fc_wait, &wait,
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  752) 				TASK_UNINTERRUPTIBLE);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  753) 		write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  754) 		schedule();
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  755) 		finish_wait(&journal->j_fc_wait, &wait);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  756) 		return -EALREADY;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  757) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  758) 	journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  759) 	write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  760) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  761) 	return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  762) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  763) EXPORT_SYMBOL(jbd2_fc_begin_commit);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  764) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  765) /*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  766)  * Stop a fast commit. If fallback is set, this function starts commit of
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  767)  * TID tid before any other fast commit can start.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  768)  */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  769) static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  770) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  771) 	if (journal->j_fc_cleanup_callback)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  772) 		journal->j_fc_cleanup_callback(journal, 0);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  773) 	write_lock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  774) 	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  775) 	if (fallback)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  776) 		journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  777) 	write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  778) 	wake_up(&journal->j_fc_wait);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  779) 	if (fallback)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  780) 		return jbd2_complete_transaction(journal, tid);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  781) 	return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  782) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  783) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  784) int jbd2_fc_end_commit(journal_t *journal)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  785) {
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  786) 	return __jbd2_fc_end_commit(journal, 0, false);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  787) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  788) EXPORT_SYMBOL(jbd2_fc_end_commit);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  789) 
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  790) int jbd2_fc_end_commit_fallback(journal_t *journal)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  791) {
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  792) 	tid_t tid;
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  793) 
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  794) 	read_lock(&journal->j_state_lock);
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  795) 	tid = journal->j_running_transaction ?
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  796) 		journal->j_running_transaction->t_tid : 0;
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  797) 	read_unlock(&journal->j_state_lock);
0bce577bf9cae (Harshad Shirwadkar         2020-11-05 19:58:58 -0800  798) 	return __jbd2_fc_end_commit(journal, tid, true);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  799) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  800) EXPORT_SYMBOL(jbd2_fc_end_commit_fallback);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  801) 
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  802) /* Return 1 when transaction with given tid has already committed. */
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  803) int jbd2_transaction_committed(journal_t *journal, tid_t tid)
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  804) {
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  805) 	int ret = 1;
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  806) 
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  807) 	read_lock(&journal->j_state_lock);
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  808) 	if (journal->j_running_transaction &&
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  809) 	    journal->j_running_transaction->t_tid == tid)
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  810) 		ret = 0;
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  811) 	if (journal->j_committing_transaction &&
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  812) 	    journal->j_committing_transaction->t_tid == tid)
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  813) 		ret = 0;
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  814) 	read_unlock(&journal->j_state_lock);
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  815) 	return ret;
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  816) }
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  817) EXPORT_SYMBOL(jbd2_transaction_committed);
b8a6176c214cf (Jan Kara                   2017-11-01 16:36:45 +0100  818) 
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  819) /*
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  820)  * When this function returns the transaction corresponding to tid
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  821)  * will be completed.  If the transaction has currently running, start
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  822)  * committing that transaction before waiting for it to complete.  If
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  823)  * the transaction id is stale, it is by definition already completed,
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  824)  * so just return SUCCESS.
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  825)  */
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  826) int jbd2_complete_transaction(journal_t *journal, tid_t tid)
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  827) {
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  828) 	int	need_to_wait = 1;
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  829) 
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  830) 	read_lock(&journal->j_state_lock);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  831) 	if (journal->j_running_transaction &&
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  832) 	    journal->j_running_transaction->t_tid == tid) {
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  833) 		if (journal->j_commit_request != tid) {
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  834) 			/* transaction not yet started, so request it */
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  835) 			read_unlock(&journal->j_state_lock);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  836) 			jbd2_log_start_commit(journal, tid);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  837) 			goto wait_commit;
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  838) 		}
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  839) 	} else if (!(journal->j_committing_transaction &&
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  840) 		     journal->j_committing_transaction->t_tid == tid))
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  841) 		need_to_wait = 0;
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  842) 	read_unlock(&journal->j_state_lock);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  843) 	if (!need_to_wait)
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  844) 		return 0;
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  845) wait_commit:
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  846) 	return jbd2_log_wait_commit(journal, tid);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  847) }
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  848) EXPORT_SYMBOL(jbd2_complete_transaction);
d76a3a77113db (Theodore Ts'o              2013-04-03 22:02:52 -0400  849) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  850) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  851)  * Log buffer allocation routines:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  852)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  853) 
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700  854) int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  855) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  856) 	unsigned long blocknr;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  857) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  858) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  859) 	J_ASSERT(journal->j_free > 1);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  860) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  861) 	blocknr = journal->j_head;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  862) 	journal->j_head++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  863) 	journal->j_free--;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  864) 	if (journal->j_head == journal->j_last)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  865) 		journal->j_head = journal->j_first;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  866) 	write_unlock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  867) 	return jbd2_journal_bmap(journal, blocknr, retp);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  868) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  869) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  870) /* Map one fast commit buffer for use by the file system */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  871) int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  872) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  873) 	unsigned long long pblock;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  874) 	unsigned long blocknr;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  875) 	int ret = 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  876) 	struct buffer_head *bh;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  877) 	int fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  878) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  879) 	*bh_out = NULL;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  880) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  881) 	if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  882) 		fc_off = journal->j_fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  883) 		blocknr = journal->j_fc_first + fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  884) 		journal->j_fc_off++;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  885) 	} else {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  886) 		ret = -EINVAL;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  887) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  888) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  889) 	if (ret)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  890) 		return ret;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  891) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  892) 	ret = jbd2_journal_bmap(journal, blocknr, &pblock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  893) 	if (ret)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  894) 		return ret;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  895) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  896) 	bh = __getblk(journal->j_dev, pblock, journal->j_blocksize);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  897) 	if (!bh)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  898) 		return -ENOMEM;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  899) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  900) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  901) 	journal->j_fc_wbuf[fc_off] = bh;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  902) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  903) 	*bh_out = bh;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  904) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  905) 	return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  906) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  907) EXPORT_SYMBOL(jbd2_fc_get_buf);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  908) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  909) /*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  910)  * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  911)  * for completion.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  912)  */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  913) int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  914) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  915) 	struct buffer_head *bh;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  916) 	int i, j_fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  917) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  918) 	j_fc_off = journal->j_fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  919) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  920) 	/*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  921) 	 * Wait in reverse order to minimize chances of us being woken up before
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  922) 	 * all IOs have completed
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  923) 	 */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  924) 	for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  925) 		bh = journal->j_fc_wbuf[i];
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  926) 		wait_on_buffer(bh);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  927) 		put_bh(bh);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  928) 		journal->j_fc_wbuf[i] = NULL;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  929) 		if (unlikely(!buffer_uptodate(bh)))
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  930) 			return -EIO;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  931) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  932) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  933) 	return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  934) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  935) EXPORT_SYMBOL(jbd2_fc_wait_bufs);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  936) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  937) /*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  938)  * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  939)  * for completion.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  940)  */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  941) int jbd2_fc_release_bufs(journal_t *journal)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  942) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  943) 	struct buffer_head *bh;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  944) 	int i, j_fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  945) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  946) 	j_fc_off = journal->j_fc_off;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  947) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  948) 	/*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  949) 	 * Wait in reverse order to minimize chances of us being woken up before
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  950) 	 * all IOs have completed
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  951) 	 */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  952) 	for (i = j_fc_off - 1; i >= 0; i--) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  953) 		bh = journal->j_fc_wbuf[i];
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  954) 		if (!bh)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  955) 			break;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  956) 		put_bh(bh);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  957) 		journal->j_fc_wbuf[i] = NULL;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  958) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  959) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  960) 	return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  961) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  962) EXPORT_SYMBOL(jbd2_fc_release_bufs);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  963) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  964) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  965)  * Conversion of logical to physical block numbers for the journal
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  966)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  967)  * On external journals the journal blocks are identity-mapped, so
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  968)  * this is a no-op.  If needed, we can use j_blk_offset - everything is
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  969)  * ready.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  970)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  971) int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700  972) 		 unsigned long long *retp)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  973) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  974) 	int err = 0;
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700  975) 	unsigned long long ret;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  976) 	sector_t block = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  977) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  978) 	if (journal->j_inode) {
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  979) 		block = blocknr;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  980) 		ret = bmap(journal->j_inode, &block);
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  981) 
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  982) 		if (ret || !block) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  983) 			printk(KERN_ALERT "%s: journal block not found "
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  984) 					"at offset %lu on %s\n",
05496769e5da8 (Theodore Ts'o              2008-09-16 14:36:17 -0400  985) 			       __func__, blocknr, journal->j_devname);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  986) 			err = -EIO;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800  987) 			jbd2_journal_abort(journal, err);
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  988) 		} else {
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  989) 			*retp = block;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  990) 		}
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100  991) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  992) 	} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  993) 		*retp = blocknr; /* +journal->j_blk_offset */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  994) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  995) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  996) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  997) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  998) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  999)  * We play buffer_head aliasing tricks to write data/metadata blocks to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1000)  * the journal without copying their contents, but for journal
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1001)  * descriptor blocks we do need to generate bona fide buffers.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1002)  *
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1003)  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1004)  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1005)  * But we don't bother doing that, so there will be coherency problems with
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1006)  * mmaps of blockdevs which hold live JBD-controlled filesystems.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1007)  */
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1008) struct buffer_head *
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1009) jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1010) {
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1011) 	journal_t *journal = transaction->t_journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1012) 	struct buffer_head *bh;
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700 1013) 	unsigned long long blocknr;
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1014) 	journal_header_t *header;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1015) 	int err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1016) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1017) 	err = jbd2_journal_next_log_block(journal, &blocknr);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1018) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1019) 	if (err)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1020) 		return NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1021) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1022) 	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
4b905671d2ea0 (Jan Kara                   2009-01-06 14:53:35 -0500 1023) 	if (!bh)
4b905671d2ea0 (Jan Kara                   2009-01-06 14:53:35 -0500 1024) 		return NULL;
9f356e5a4f120 (Jan Kara                   2019-11-05 17:44:24 +0100 1025) 	atomic_dec(&transaction->t_outstanding_credits);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1026) 	lock_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1027) 	memset(bh->b_data, 0, journal->j_blocksize);
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1028) 	header = (journal_header_t *)bh->b_data;
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1029) 	header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1030) 	header->h_blocktype = cpu_to_be32(type);
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500 1031) 	header->h_sequence = cpu_to_be32(transaction->t_tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1032) 	set_buffer_uptodate(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1033) 	unlock_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1034) 	BUFFER_TRACE(bh, "return this buffer");
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400 1035) 	return bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1036) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1037) 
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1038) void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh)
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1039) {
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1040) 	struct jbd2_journal_block_tail *tail;
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1041) 	__u32 csum;
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1042) 
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1043) 	if (!jbd2_journal_has_csum_v2or3(j))
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1044) 		return;
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1045) 
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1046) 	tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1047) 			sizeof(struct jbd2_journal_block_tail));
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1048) 	tail->t_checksum = 0;
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1049) 	csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1050) 	tail->t_checksum = cpu_to_be32(csum);
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1051) }
1101cd4d13ba2 (Jan Kara                   2016-02-22 23:19:09 -0500 1052) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1053) /*
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1054)  * Return tid of the oldest transaction in the journal and block in the journal
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1055)  * where the transaction starts.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1056)  *
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1057)  * If the journal is now empty, return which will be the next transaction ID
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1058)  * we will write and where will that transaction start.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1059)  *
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1060)  * The return value is 0 if journal tail cannot be pushed any further, 1 if
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1061)  * it can.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1062)  */
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1063) int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1064) 			      unsigned long *block)
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1065) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1066) 	transaction_t *transaction;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1067) 	int ret;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1068) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1069) 	read_lock(&journal->j_state_lock);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1070) 	spin_lock(&journal->j_list_lock);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1071) 	transaction = journal->j_checkpoint_transactions;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1072) 	if (transaction) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1073) 		*tid = transaction->t_tid;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1074) 		*block = transaction->t_log_start;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1075) 	} else if ((transaction = journal->j_committing_transaction) != NULL) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1076) 		*tid = transaction->t_tid;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1077) 		*block = transaction->t_log_start;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1078) 	} else if ((transaction = journal->j_running_transaction) != NULL) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1079) 		*tid = transaction->t_tid;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1080) 		*block = journal->j_head;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1081) 	} else {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1082) 		*tid = journal->j_transaction_sequence;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1083) 		*block = journal->j_head;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1084) 	}
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1085) 	ret = tid_gt(*tid, journal->j_tail_sequence);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1086) 	spin_unlock(&journal->j_list_lock);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1087) 	read_unlock(&journal->j_state_lock);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1088) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1089) 	return ret;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1090) }
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1091) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1092) /*
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1093)  * Update information in journal structure and in on disk journal superblock
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1094)  * about log tail. This function does not check whether information passed in
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1095)  * really pushes log tail further. It's responsibility of the caller to make
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1096)  * sure provided log tail information is valid (e.g. by holding
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1097)  * j_checkpoint_mutex all the time between computing log tail and calling this
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1098)  * function as is the case with jbd2_cleanup_journal_tail()).
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1099)  *
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1100)  * Requires j_checkpoint_mutex
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1101)  */
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1102) int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1103) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1104) 	unsigned long freed;
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1105) 	int ret;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1106) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1107) 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1108) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1109) 	/*
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1110) 	 * We cannot afford for write to remain in drive's caches since as
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1111) 	 * soon as we update j_tail, next transaction can start reusing journal
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1112) 	 * space and if we lose sb update during power failure we'd replay
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1113) 	 * old transaction with possibly newly overwritten data.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1114) 	 */
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 1115) 	ret = jbd2_journal_update_sb_log_tail(journal, tid, block,
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 1116) 					      REQ_SYNC | REQ_FUA);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1117) 	if (ret)
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1118) 		goto out;
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1119) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1120) 	write_lock(&journal->j_state_lock);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1121) 	freed = block - journal->j_tail;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1122) 	if (block < journal->j_tail)
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1123) 		freed += journal->j_last - journal->j_first;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1124) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1125) 	trace_jbd2_update_log_tail(journal, tid, block, freed);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1126) 	jbd_debug(1,
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400 1127) 		  "Cleaning journal tail from %u to %u (offset %lu), "
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1128) 		  "freeing %lu\n",
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1129) 		  journal->j_tail_sequence, tid, block, freed);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1130) 
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1131) 	journal->j_free += freed;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1132) 	journal->j_tail_sequence = tid;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1133) 	journal->j_tail = block;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1134) 	write_unlock(&journal->j_state_lock);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1135) 
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1136) out:
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1137) 	return ret;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1138) }
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1139) 
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1140) /*
85e0c4e89c1b8 (Theodore Ts'o              2018-02-19 12:22:53 -0500 1141)  * This is a variation of __jbd2_update_log_tail which checks for validity of
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1142)  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1143)  * with other threads updating log tail.
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1144)  */
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1145) void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1146) {
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 1147) 	mutex_lock_io(&journal->j_checkpoint_mutex);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1148) 	if (tid_gt(tid, journal->j_tail_sequence))
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1149) 		__jbd2_update_log_tail(journal, tid, block);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1150) 	mutex_unlock(&journal->j_checkpoint_mutex);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1151) }
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400 1152) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1153) struct jbd2_stats_proc_session {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1154) 	journal_t *journal;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1155) 	struct transaction_stats_s *stats;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1156) 	int start;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1157) 	int max;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1158) };
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1159) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1160) static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1161) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1162) 	return *pos ? NULL : SEQ_START_TOKEN;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1163) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1164) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1165) static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1166) {
1a8e9cf40c9a6 (Vasily Averin              2020-01-23 12:05:10 +0300 1167) 	(*pos)++;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1168) 	return NULL;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1169) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1170) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1171) static int jbd2_seq_info_show(struct seq_file *seq, void *v)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1172) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1173) 	struct jbd2_stats_proc_session *s = seq->private;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1174) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1175) 	if (v != SEQ_START_TOKEN)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1176) 		return 0;
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1177) 	seq_printf(seq, "%lu transactions (%lu requested), "
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1178) 		   "each up to %u blocks\n",
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1179) 		   s->stats->ts_tid, s->stats->ts_requested,
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1180) 		   s->journal->j_max_transaction_buffers);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1181) 	if (s->stats->ts_tid == 0)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1182) 		return 0;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1183) 	seq_printf(seq, "average: \n  %ums waiting for transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1184) 	    jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1185) 	seq_printf(seq, "  %ums request delay\n",
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1186) 	    (s->stats->ts_requested == 0) ? 0 :
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1187) 	    jiffies_to_msecs(s->stats->run.rs_request_delay /
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500 1188) 			     s->stats->ts_requested));
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1189) 	seq_printf(seq, "  %ums running transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1190) 	    jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1191) 	seq_printf(seq, "  %ums transaction was being locked\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1192) 	    jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1193) 	seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1194) 	    jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1195) 	seq_printf(seq, "  %ums logging transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1196) 	    jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
c225aa57ff4ff (Simon Holm Thøgersen       2009-01-11 22:34:01 -0500 1197) 	seq_printf(seq, "  %lluus average transaction commit time\n",
c225aa57ff4ff (Simon Holm Thøgersen       2009-01-11 22:34:01 -0500 1198) 		   div_u64(s->journal->j_average_commit_time, 1000));
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1199) 	seq_printf(seq, "  %lu handles per transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1200) 	    s->stats->run.rs_handle_count / s->stats->ts_tid);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1201) 	seq_printf(seq, "  %lu blocks per transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1202) 	    s->stats->run.rs_blocks / s->stats->ts_tid);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1203) 	seq_printf(seq, "  %lu logged blocks per transaction\n",
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1204) 	    s->stats->run.rs_blocks_logged / s->stats->ts_tid);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1205) 	return 0;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1206) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1207) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1208) static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1209) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1210) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1211) 
88e9d34c72788 (James Morris               2009-09-22 16:43:43 -0700 1212) static const struct seq_operations jbd2_seq_info_ops = {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1213) 	.start  = jbd2_seq_info_start,
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1214) 	.next   = jbd2_seq_info_next,
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1215) 	.stop   = jbd2_seq_info_stop,
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1216) 	.show   = jbd2_seq_info_show,
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1217) };
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1218) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1219) static int jbd2_seq_info_open(struct inode *inode, struct file *file)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1220) {
d9dda78bad879 (Al Viro                    2013-03-31 18:16:14 -0400 1221) 	journal_t *journal = PDE_DATA(inode);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1222) 	struct jbd2_stats_proc_session *s;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1223) 	int rc, size;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1224) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1225) 	s = kmalloc(sizeof(*s), GFP_KERNEL);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1226) 	if (s == NULL)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1227) 		return -ENOMEM;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1228) 	size = sizeof(struct transaction_stats_s);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1229) 	s->stats = kmalloc(size, GFP_KERNEL);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1230) 	if (s->stats == NULL) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1231) 		kfree(s);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1232) 		return -ENOMEM;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1233) 	}
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1234) 	spin_lock(&journal->j_history_lock);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1235) 	memcpy(s->stats, &journal->j_stats, size);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1236) 	s->journal = journal;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1237) 	spin_unlock(&journal->j_history_lock);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1238) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1239) 	rc = seq_open(file, &jbd2_seq_info_ops);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1240) 	if (rc == 0) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1241) 		struct seq_file *m = file->private_data;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1242) 		m->private = s;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1243) 	} else {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1244) 		kfree(s->stats);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1245) 		kfree(s);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1246) 	}
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1247) 	return rc;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1248) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1249) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1250) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1251) static int jbd2_seq_info_release(struct inode *inode, struct file *file)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1252) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1253) 	struct seq_file *seq = file->private_data;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1254) 	struct jbd2_stats_proc_session *s = seq->private;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1255) 	kfree(s->stats);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1256) 	kfree(s);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1257) 	return seq_release(inode, file);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1258) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1259) 
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1260) static const struct proc_ops jbd2_info_proc_ops = {
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1261) 	.proc_open	= jbd2_seq_info_open,
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1262) 	.proc_read	= seq_read,
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1263) 	.proc_lseek	= seq_lseek,
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1264) 	.proc_release	= jbd2_seq_info_release,
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1265) };
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1266) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1267) static struct proc_dir_entry *proc_jbd2_stats;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1268) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1269) static void jbd2_stats_proc_init(journal_t *journal)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1270) {
05496769e5da8 (Theodore Ts'o              2008-09-16 14:36:17 -0400 1271) 	journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1272) 	if (journal->j_proc_entry) {
79da3664f6164 (Denis V. Lunev             2008-04-29 01:02:11 -0700 1273) 		proc_create_data("info", S_IRUGO, journal->j_proc_entry,
97a32539b9568 (Alexey Dobriyan            2020-02-03 17:37:17 -0800 1274) 				 &jbd2_info_proc_ops, journal);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1275) 	}
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1276) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1277) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1278) static void jbd2_stats_proc_exit(journal_t *journal)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1279) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1280) 	remove_proc_entry("info", journal->j_proc_entry);
05496769e5da8 (Theodore Ts'o              2008-09-16 14:36:17 -0400 1281) 	remove_proc_entry(journal->j_devname, proc_jbd2_stats);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1282) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1283) 
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1284) /* Minimum size of descriptor tag */
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1285) static int jbd2_min_tag_size(void)
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1286) {
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1287) 	/*
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1288) 	 * Tag with 32-bit block numbers does not use last four bytes of the
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1289) 	 * structure
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1290) 	 */
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1291) 	return sizeof(journal_block_tag_t) - 4;
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1292) }
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1293) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1294) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1295)  * Management for journal control blocks: functions to create and
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1296)  * destroy journal_t structures, and to initialise and read existing
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1297)  * journal blocks from disk.  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1298) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1299) /* First: create and setup a journal_t object in memory.  We initialise
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1300)  * very few fields yet: that has to wait until we have created the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1301)  * journal structures from from scratch, or loaded them from disk. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1302) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1303) static journal_t *journal_init_common(struct block_device *bdev,
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1304) 			struct block_device *fs_dev,
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1305) 			unsigned long long start, int len, int blocksize)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1306) {
ab714aff4f744 (Jan Kara                   2016-06-30 11:39:38 -0400 1307) 	static struct lock_class_key jbd2_trans_commit_key;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1308) 	journal_t *journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1309) 	int err;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1310) 	struct buffer_head *bh;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1311) 	int n;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1312) 
3ebfdf885aced (Andrew Morton              2009-12-23 08:05:15 -0500 1313) 	journal = kzalloc(sizeof(*journal), GFP_KERNEL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1314) 	if (!journal)
b7271b0a39947 (Theodore Ts'o              2010-12-18 13:39:38 -0500 1315) 		return NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1316) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1317) 	init_waitqueue_head(&journal->j_wait_transaction_locked);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1318) 	init_waitqueue_head(&journal->j_wait_done_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1319) 	init_waitqueue_head(&journal->j_wait_commit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1320) 	init_waitqueue_head(&journal->j_wait_updates);
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400 1321) 	init_waitqueue_head(&journal->j_wait_reserved);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1322) 	init_waitqueue_head(&journal->j_fc_wait);
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 1323) 	mutex_init(&journal->j_abort_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1324) 	mutex_init(&journal->j_barrier);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1325) 	mutex_init(&journal->j_checkpoint_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1326) 	spin_lock_init(&journal->j_revoke_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1327) 	spin_lock_init(&journal->j_list_lock);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1328) 	rwlock_init(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1329) 
cd02ff0b14b7e (Mingming Cao               2007-10-16 18:38:25 -0400 1330) 	journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
30773840c19ce (Theodore Ts'o              2009-01-03 20:27:38 -0500 1331) 	journal->j_min_batch_time = 0;
30773840c19ce (Theodore Ts'o              2009-01-03 20:27:38 -0500 1332) 	journal->j_max_batch_time = 15000; /* 15ms */
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400 1333) 	atomic_set(&journal->j_reserved_credits, 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1334) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1335) 	/* The journal is marked for error until we succeed with recovery! */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1336) 	journal->j_flags = JBD2_ABORT;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1337) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1338) 	/* Set up a default-sized revoke table for the new mount. */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1339) 	err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1340) 	if (err)
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1341) 		goto err_cleanup;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1342) 
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1343) 	spin_lock_init(&journal->j_history_lock);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1344) 
ab714aff4f744 (Jan Kara                   2016-06-30 11:39:38 -0400 1345) 	lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle",
ab714aff4f744 (Jan Kara                   2016-06-30 11:39:38 -0400 1346) 			 &jbd2_trans_commit_key, 0);
ab714aff4f744 (Jan Kara                   2016-06-30 11:39:38 -0400 1347) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1348) 	/* journal descriptor can store up to n blocks -bzzz */
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1349) 	journal->j_blocksize = blocksize;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1350) 	journal->j_dev = bdev;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1351) 	journal->j_fs_dev = fs_dev;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1352) 	journal->j_blk_offset = start;
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1353) 	journal->j_total_len = len;
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1354) 	/* We need enough buffers to write out full descriptor block. */
b90bfdf581194 (Jan Kara                   2019-11-05 17:44:09 +0100 1355) 	n = journal->j_blocksize / jbd2_min_tag_size();
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1356) 	journal->j_wbufsize = n;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1357) 	journal->j_fc_wbuf = NULL;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1358) 	journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1359) 					GFP_KERNEL);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1360) 	if (!journal->j_wbuf)
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1361) 		goto err_cleanup;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1362) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1363) 	bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1364) 	if (!bh) {
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1365) 		pr_err("%s: Cannot get buffer for journal superblock\n",
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1366) 			__func__);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1367) 		goto err_cleanup;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1368) 	}
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1369) 	journal->j_sb_buffer = bh;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1370) 	journal->j_superblock = (journal_superblock_t *)bh->b_data;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1371) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1372) 	return journal;
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1373) 
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1374) err_cleanup:
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1375) 	kfree(journal->j_wbuf);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1376) 	jbd2_journal_destroy_revoke(journal);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1377) 	kfree(journal);
cd9cb405e0b94 (Eric Biggers               2017-03-15 15:08:48 -0400 1378) 	return NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1379) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1380) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1381) /* jbd2_journal_init_dev and jbd2_journal_init_inode:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1382)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1383)  * Create a journal structure assigned some fixed set of disk blocks to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1384)  * the journal.  We don't actually touch those disk blocks yet, but we
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1385)  * need to set up all of the mapping information to tell the journaling
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1386)  * system where the journal blocks are.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1387)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1388)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1389) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1390) /**
5648ba5b2dc0d (Randy Dunlap               2008-04-17 10:38:59 -0400 1391)  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1392)  *  @bdev: Block device on which to create the journal
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1393)  *  @fs_dev: Device which hold journalled filesystem for this journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1394)  *  @start: Block nr Start of journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1395)  *  @len:  Length of the journal in blocks.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1396)  *  @blocksize: blocksize of journalling device
5648ba5b2dc0d (Randy Dunlap               2008-04-17 10:38:59 -0400 1397)  *
5648ba5b2dc0d (Randy Dunlap               2008-04-17 10:38:59 -0400 1398)  *  Returns: a newly created journal_t *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1399)  *
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1400)  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1401)  *  range of blocks on an arbitrary block device.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1402)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1403)  */
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1404) journal_t *jbd2_journal_init_dev(struct block_device *bdev,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1405) 			struct block_device *fs_dev,
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700 1406) 			unsigned long long start, int len, int blocksize)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1407) {
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1408) 	journal_t *journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1409) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1410) 	journal = journal_init_common(bdev, fs_dev, start, len, blocksize);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1411) 	if (!journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1412) 		return NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1413) 
0587aa3d11f97 (yangsheng                  2010-11-17 21:46:26 -0500 1414) 	bdevname(journal->j_dev, journal->j_devname);
81ae394bdc473 (Rasmus Villemoes           2015-06-25 15:02:38 -0700 1415) 	strreplace(journal->j_devname, '/', '!');
4b905671d2ea0 (Jan Kara                   2009-01-06 14:53:35 -0500 1416) 	jbd2_stats_proc_init(journal);
4b905671d2ea0 (Jan Kara                   2009-01-06 14:53:35 -0500 1417) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1418) 	return journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1419) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1420) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1421) /**
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1422)  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1423)  *  @inode: An inode to create the journal in
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1424)  *
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1425)  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1426)  * the journal.  The inode must exist already, must support bmap() and
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1427)  * must have all data blocks preallocated.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1428)  */
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1429) journal_t *jbd2_journal_init_inode(struct inode *inode)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1430) {
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1431) 	journal_t *journal;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1432) 	sector_t blocknr;
05496769e5da8 (Theodore Ts'o              2008-09-16 14:36:17 -0400 1433) 	char *p;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1434) 	int err = 0;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1435) 
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1436) 	blocknr = 0;
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1437) 	err = bmap(inode, &blocknr);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1438) 
30460e1ea3e62 (Carlos Maiolino            2020-01-09 14:30:41 +0100 1439) 	if (err || !blocknr) {
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1440) 		pr_err("%s: Cannot locate journal superblock\n",
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1441) 			__func__);
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1442) 		return NULL;
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1443) 	}
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1444) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1445) 	jbd_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n",
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1446) 		  inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size,
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1447) 		  inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1448) 
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1449) 	journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev,
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1450) 			blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits,
f0c9fd5458bac (Geliang Tang               2016-09-15 12:02:32 -0400 1451) 			inode->i_sb->s_blocksize);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1452) 	if (!journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1453) 		return NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1454) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1455) 	journal->j_inode = inode;
05496769e5da8 (Theodore Ts'o              2008-09-16 14:36:17 -0400 1456) 	bdevname(journal->j_dev, journal->j_devname);
81ae394bdc473 (Rasmus Villemoes           2015-06-25 15:02:38 -0700 1457) 	p = strreplace(journal->j_devname, '/', '!');
90576c0b9a0b5 (Theodore Ts'o              2009-09-29 15:51:30 -0400 1458) 	sprintf(p, "-%lu", journal->j_inode->i_ino);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1459) 	jbd2_stats_proc_init(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1460) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1461) 	return journal;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1462) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1463) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1464) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1465)  * If the journal init or create aborts, we need to mark the journal
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1466)  * superblock as being NULL to prevent the journal destroy from writing
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1467)  * back a bogus superblock.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1468)  */
60ed633f51d0c (Xianting Tian              2020-07-18 08:57:37 -0400 1469) static void journal_fail_superblock(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1470) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1471) 	struct buffer_head *bh = journal->j_sb_buffer;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1472) 	brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1473) 	journal->j_sb_buffer = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1474) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1475) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1476) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1477)  * Given a journal_t structure, initialise the various fields for
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1478)  * startup of a new journaling session.  We use this both when creating
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1479)  * a journal, and after recovering an old journal to reset it for
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1480)  * subsequent use.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1481)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1482) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1483) static int journal_reset(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1484) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1485) 	journal_superblock_t *sb = journal->j_superblock;
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700 1486) 	unsigned long long first, last;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1487) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1488) 	first = be32_to_cpu(sb->s_first);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1489) 	last = be32_to_cpu(sb->s_maxlen);
f6f50e28f0cb8 (Jan Kara                   2009-07-17 10:40:01 -0400 1490) 	if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1491) 		printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
f6f50e28f0cb8 (Jan Kara                   2009-07-17 10:40:01 -0400 1492) 		       first, last);
f6f50e28f0cb8 (Jan Kara                   2009-07-17 10:40:01 -0400 1493) 		journal_fail_superblock(journal);
f6f50e28f0cb8 (Jan Kara                   2009-07-17 10:40:01 -0400 1494) 		return -EINVAL;
f6f50e28f0cb8 (Jan Kara                   2009-07-17 10:40:01 -0400 1495) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1496) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1497) 	journal->j_first = first;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1498) 	journal->j_last = last;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1499) 
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1500) 	journal->j_head = journal->j_first;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1501) 	journal->j_tail = journal->j_first;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1502) 	journal->j_free = journal->j_last - journal->j_first;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1503) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1504) 	journal->j_tail_sequence = journal->j_transaction_sequence;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1505) 	journal->j_commit_sequence = journal->j_transaction_sequence - 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1506) 	journal->j_commit_request = journal->j_commit_sequence;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1507) 
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1508) 	journal->j_max_transaction_buffers = jbd2_journal_get_max_txn_bufs(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1509) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1510) 	/*
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1511) 	 * Now that journal recovery is done, turn fast commits off here. This
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1512) 	 * way, if fast commit was enabled before the crash but if now FS has
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1513) 	 * disabled it, we don't enable fast commits.
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1514) 	 */
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1515) 	jbd2_clear_feature_fast_commit(journal);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1516) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1517) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1518) 	 * As a special case, if the on-disk copy is already marked as needing
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1519) 	 * no recovery (s_start == 0), then we can safely defer the superblock
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1520) 	 * update until the next commit by setting JBD2_FLUSHED.  This avoids
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1521) 	 * attempting a write to a potential-readonly device.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1522) 	 */
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1523) 	if (sb->s_start == 0) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1524) 		jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400 1525) 			"(start %ld, seq %u, errno %d)\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1526) 			journal->j_tail, journal->j_tail_sequence,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1527) 			journal->j_errno);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1528) 		journal->j_flags |= JBD2_FLUSHED;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1529) 	} else {
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 1530) 		/* Lock here to make assertions happy... */
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 1531) 		mutex_lock_io(&journal->j_checkpoint_mutex);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1532) 		/*
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600 1533) 		 * Update log tail information. We use REQ_FUA since new
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1534) 		 * transaction will start reusing journal space and so we
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1535) 		 * must make sure information about current log tail is on
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1536) 		 * disk before that.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1537) 		 */
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1538) 		jbd2_journal_update_sb_log_tail(journal,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1539) 						journal->j_tail_sequence,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1540) 						journal->j_tail,
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 1541) 						REQ_SYNC | REQ_FUA);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 1542) 		mutex_unlock(&journal->j_checkpoint_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1543) 	}
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1544) 	return jbd2_journal_start_thread(journal);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1545) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1546) 
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1547) /*
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1548)  * This function expects that the caller will have locked the journal
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1549)  * buffer head, and will return with it unlocked
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1550)  */
2a222ca992c35 (Mike Christie              2016-06-05 14:31:43 -0500 1551) static int jbd2_write_superblock(journal_t *journal, int write_flags)
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1552) {
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1553) 	struct buffer_head *bh = journal->j_sb_buffer;
fe52d17cdd343 (Theodore Ts'o              2013-07-01 08:12:38 -0400 1554) 	journal_superblock_t *sb = journal->j_superblock;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1555) 	int ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1556) 
742b06b5628f2 (Jiufei Xue                 2019-04-06 18:57:40 -0400 1557) 	/* Buffer got discarded which means block device got invalidated */
ef3f5830b8596 (zhangyi (F)                2020-06-20 14:19:48 +0800 1558) 	if (!buffer_mapped(bh)) {
ef3f5830b8596 (zhangyi (F)                2020-06-20 14:19:48 +0800 1559) 		unlock_buffer(bh);
742b06b5628f2 (Jiufei Xue                 2019-04-06 18:57:40 -0400 1560) 		return -EIO;
ef3f5830b8596 (zhangyi (F)                2020-06-20 14:19:48 +0800 1561) 	}
742b06b5628f2 (Jiufei Xue                 2019-04-06 18:57:40 -0400 1562) 
2a222ca992c35 (Mike Christie              2016-06-05 14:31:43 -0500 1563) 	trace_jbd2_write_superblock(journal, write_flags);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1564) 	if (!(journal->j_flags & JBD2_BARRIER))
28a8f0d317bf2 (Mike Christie              2016-06-05 14:32:25 -0500 1565) 		write_flags &= ~(REQ_FUA | REQ_PREFLUSH);
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1566) 	if (buffer_write_io_error(bh)) {
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1567) 		/*
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1568) 		 * Oh, dear.  A previous attempt to write the journal
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1569) 		 * superblock failed.  This could happen because the
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1570) 		 * USB device was yanked out.  Or it could happen to
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1571) 		 * be a transient write error and maybe the block will
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1572) 		 * be remapped.  Nothing we can do but to retry the
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1573) 		 * write and hope for the best.
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1574) 		 */
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1575) 		printk(KERN_ERR "JBD2: previous I/O error detected "
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1576) 		       "for journal superblock update for %s.\n",
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1577) 		       journal->j_devname);
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1578) 		clear_buffer_write_io_error(bh);
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1579) 		set_buffer_uptodate(bh);
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1580) 	}
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1581) 	if (jbd2_journal_has_csum_v2or3(journal))
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1582) 		sb->s_checksum = jbd2_superblock_csum(journal, sb);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1583) 	get_bh(bh);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1584) 	bh->b_end_io = end_buffer_write_sync;
2a222ca992c35 (Mike Christie              2016-06-05 14:31:43 -0500 1585) 	ret = submit_bh(REQ_OP_WRITE, write_flags, bh);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1586) 	wait_on_buffer(bh);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1587) 	if (buffer_write_io_error(bh)) {
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1588) 		clear_buffer_write_io_error(bh);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1589) 		set_buffer_uptodate(bh);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1590) 		ret = -EIO;
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1591) 	}
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1592) 	if (ret) {
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1593) 		printk(KERN_ERR "JBD2: Error %d detected when updating "
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1594) 		       "journal superblock for %s.\n", ret,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1595) 		       journal->j_devname);
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 1596) 		if (!is_journal_aborted(journal))
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 1597) 			jbd2_journal_abort(journal, ret);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1598) 	}
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1599) 
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1600) 	return ret;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1601) }
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1602) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1603) /**
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1604)  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1605)  * @journal: The journal to update.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1606)  * @tail_tid: TID of the new transaction at the tail of the log
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1607)  * @tail_block: The first block of the transaction at the tail of the log
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1608)  * @write_op: With which operation should we write the journal sb
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1609)  *
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1610)  * Update a journal's superblock information about log tail and write it to
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1611)  * disk, waiting for the IO to complete.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1612)  */
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1613) int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1614) 				     unsigned long tail_block, int write_op)
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1615) {
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1616) 	journal_superblock_t *sb = journal->j_superblock;
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1617) 	int ret;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1618) 
85e0c4e89c1b8 (Theodore Ts'o              2018-02-19 12:22:53 -0500 1619) 	if (is_journal_aborted(journal))
85e0c4e89c1b8 (Theodore Ts'o              2018-02-19 12:22:53 -0500 1620) 		return -EIO;
85e0c4e89c1b8 (Theodore Ts'o              2018-02-19 12:22:53 -0500 1621) 
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 1622) 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1623) 	jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1624) 		  tail_block, tail_tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1625) 
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1626) 	lock_buffer(journal->j_sb_buffer);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1627) 	sb->s_sequence = cpu_to_be32(tail_tid);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400 1628) 	sb->s_start    = cpu_to_be32(tail_block);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1629) 
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1630) 	ret = jbd2_write_superblock(journal, write_op);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1631) 	if (ret)
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1632) 		goto out;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1633) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1634) 	/* Log is no longer empty */
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1635) 	write_lock(&journal->j_state_lock);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1636) 	WARN_ON(!sb->s_sequence);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1637) 	journal->j_flags &= ~JBD2_FLUSHED;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1638) 	write_unlock(&journal->j_state_lock);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1639) 
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1640) out:
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 1641) 	return ret;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1642) }
2201c590dd6e8 (Seiji Aguchi               2012-02-20 17:53:01 -0500 1643) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1644) /**
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1645)  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1646)  * @journal: The journal to update.
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 1647)  * @write_op: With which operation should we write the journal sb
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1648)  *
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1649)  * Update a journal's dynamic superblock fields to show that journal is empty.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1650)  * Write updated superblock to disk waiting for IO to complete.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1651)  */
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 1652) static void jbd2_mark_journal_empty(journal_t *journal, int write_op)
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1653) {
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1654) 	journal_superblock_t *sb = journal->j_superblock;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1655) 	bool had_fast_commit = false;
914258bf2cb22 (Theodore Ts'o              2008-10-06 21:35:40 -0400 1656) 
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 1657) 	BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1658) 	lock_buffer(journal->j_sb_buffer);
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1659) 	if (sb->s_start == 0) {		/* Is it already empty? */
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1660) 		unlock_buffer(journal->j_sb_buffer);
eeecef0af5ea4 (Eric Sandeen               2012-08-18 22:29:40 -0400 1661) 		return;
eeecef0af5ea4 (Eric Sandeen               2012-08-18 22:29:40 -0400 1662) 	}
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1663) 
7821ce417ec73 (Gaowei Pu                  2019-05-30 15:08:34 -0400 1664) 	jbd_debug(1, "JBD2: Marking journal as empty (seq %u)\n",
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1665) 		  journal->j_tail_sequence);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1666) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1667) 	sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1668) 	sb->s_start    = cpu_to_be32(0);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1669) 	if (jbd2_has_feature_fast_commit(journal)) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1670) 		/*
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1671) 		 * When journal is clean, no need to commit fast commit flag and
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1672) 		 * make file system incompatible with older kernels.
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1673) 		 */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1674) 		jbd2_clear_feature_fast_commit(journal);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1675) 		had_fast_commit = true;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1676) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1677) 
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 1678) 	jbd2_write_superblock(journal, write_op);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1679) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1680) 	if (had_fast_commit)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1681) 		jbd2_set_feature_fast_commit(journal);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1682) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1683) 	/* Log is no longer empty */
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1684) 	write_lock(&journal->j_state_lock);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1685) 	journal->j_flags |= JBD2_FLUSHED;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1686) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1687) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1688) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1689) 
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1690) /**
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1691)  * jbd2_journal_update_sb_errno() - Update error in the journal.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1692)  * @journal: The journal to update.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1693)  *
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1694)  * Update a journal's errno.  Write updated superblock to disk waiting for IO
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1695)  * to complete.
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1696)  */
d796c52ef0b71 (Theodore Ts'o              2012-08-05 19:04:57 -0400 1697) void jbd2_journal_update_sb_errno(journal_t *journal)
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1698) {
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1699) 	journal_superblock_t *sb = journal->j_superblock;
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1700) 	int errcode;
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1701) 
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 1702) 	lock_buffer(journal->j_sb_buffer);
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1703) 	errcode = journal->j_errno;
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1704) 	if (errcode == -ESHUTDOWN)
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1705) 		errcode = 0;
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1706) 	jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
fb7c02445c497 (Theodore Ts'o              2018-02-18 23:45:18 -0500 1707) 	sb->s_errno    = cpu_to_be32(errcode);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1708) 
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 1709) 	jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1710) }
d796c52ef0b71 (Theodore Ts'o              2012-08-05 19:04:57 -0400 1711) EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
24bcc89c7e7c6 (Jan Kara                   2012-03-13 15:41:04 -0400 1712) 
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1713) static int journal_revoke_records_per_block(journal_t *journal)
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1714) {
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1715) 	int record_size;
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1716) 	int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1717) 
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1718) 	if (jbd2_has_feature_64bit(journal))
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1719) 		record_size = 8;
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1720) 	else
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1721) 		record_size = 4;
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1722) 
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1723) 	if (jbd2_journal_has_csum_v2or3(journal))
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1724) 		space -= sizeof(struct jbd2_journal_block_tail);
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1725) 	return space / record_size;
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1726) }
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1727) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1728) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1729)  * Read the superblock for a given journal, performing initial
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1730)  * validation of the format.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1731)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1732) static int journal_get_superblock(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1733) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1734) 	struct buffer_head *bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1735) 	journal_superblock_t *sb;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1736) 	int err = -EIO;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1737) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1738) 	bh = journal->j_sb_buffer;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1739) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1740) 	J_ASSERT(bh != NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1741) 	if (!buffer_uptodate(bh)) {
dfec8a14fc904 (Mike Christie              2016-06-05 14:31:44 -0500 1742) 		ll_rw_block(REQ_OP_READ, 0, 1, &bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1743) 		wait_on_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1744) 		if (!buffer_uptodate(bh)) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1745) 			printk(KERN_ERR
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1746) 				"JBD2: IO error reading journal superblock\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1747) 			goto out;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1748) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1749) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1750) 
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1751) 	if (buffer_verified(bh))
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1752) 		return 0;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1753) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1754) 	sb = journal->j_superblock;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1755) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1756) 	err = -EINVAL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1757) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1758) 	if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1759) 	    sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1760) 		printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1761) 		goto out;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1762) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1763) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1764) 	switch(be32_to_cpu(sb->s_header.h_blocktype)) {
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1765) 	case JBD2_SUPERBLOCK_V1:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1766) 		journal->j_format_version = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1767) 		break;
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1768) 	case JBD2_SUPERBLOCK_V2:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1769) 		journal->j_format_version = 2;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1770) 		break;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1771) 	default:
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1772) 		printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1773) 		goto out;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1774) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1775) 
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1776) 	if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1777) 		journal->j_total_len = be32_to_cpu(sb->s_maxlen);
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1778) 	else if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1779) 		printk(KERN_WARNING "JBD2: journal file too short\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1780) 		goto out;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1781) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1782) 
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1783) 	if (be32_to_cpu(sb->s_first) == 0 ||
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800 1784) 	    be32_to_cpu(sb->s_first) >= journal->j_total_len) {
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1785) 		printk(KERN_WARNING
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1786) 			"JBD2: Invalid start block of journal: %u\n",
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1787) 			be32_to_cpu(sb->s_first));
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1788) 		goto out;
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1789) 	}
8762202dd0d6e (Eryu Guan                  2011-11-01 19:04:59 -0400 1790) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 1791) 	if (jbd2_has_feature_csum2(journal) &&
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 1792) 	    jbd2_has_feature_csum3(journal)) {
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1793) 		/* Can't have checksum v2 and v3 at the same time! */
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1794) 		printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1795) 		       "at the same time!\n");
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1796) 		goto out;
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1797) 	}
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 1798) 
8595798ca34d1 (Darrick J. Wong            2015-10-15 10:30:36 -0400 1799) 	if (jbd2_journal_has_csum_v2or3_feature(journal) &&
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 1800) 	    jbd2_has_feature_checksum(journal)) {
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1801) 		/* Can't have checksum v1 and v2 on at the same time! */
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1802) 		printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1803) 		       "at the same time!\n");
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1804) 		goto out;
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1805) 	}
feb8c6d3dd0f2 (Darrick J. Wong            2014-09-11 11:38:21 -0400 1806) 
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1807) 	if (!jbd2_verify_csum_type(journal, sb)) {
a67c848a8b9aa (Dmitry Monakhov            2013-12-08 21:14:59 -0500 1808) 		printk(KERN_ERR "JBD2: Unknown checksum type\n");
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1809) 		goto out;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1810) 	}
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1811) 
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1812) 	/* Load the checksum driver */
8595798ca34d1 (Darrick J. Wong            2015-10-15 10:30:36 -0400 1813) 	if (jbd2_journal_has_csum_v2or3_feature(journal)) {
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1814) 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1815) 		if (IS_ERR(journal->j_chksum_driver)) {
a67c848a8b9aa (Dmitry Monakhov            2013-12-08 21:14:59 -0500 1816) 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1817) 			err = PTR_ERR(journal->j_chksum_driver);
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1818) 			journal->j_chksum_driver = NULL;
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1819) 			goto out;
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1820) 		}
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1821) 	}
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 1822) 
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1823) 	if (jbd2_journal_has_csum_v2or3(journal)) {
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1824) 		/* Check superblock checksum */
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1825) 		if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1826) 			printk(KERN_ERR "JBD2: journal checksum error\n");
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1827) 			err = -EFSBADCRC;
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1828) 			goto out;
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1829) 		}
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400 1830) 
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1831) 		/* Precompute checksum seed for all metadata */
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400 1832) 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400 1833) 						   sizeof(sb->s_uuid));
a58ca992661a4 (Theodore Ts'o              2019-02-14 16:28:14 -0500 1834) 	}
4fd5ea43bc116 (Darrick J. Wong            2012-05-27 08:08:22 -0400 1835) 
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1836) 	journal->j_revoke_records_per_block =
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 1837) 				journal_revoke_records_per_block(journal);
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1838) 	set_buffer_verified(bh);
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 1839) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1840) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1841) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1842) out:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1843) 	journal_fail_superblock(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1844) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1845) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1846) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1847) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1848)  * Load the on-disk journal superblock and read the key fields into the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1849)  * journal_t.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1850)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1851) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1852) static int load_superblock(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1853) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1854) 	int err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1855) 	journal_superblock_t *sb;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1856) 	int num_fc_blocks;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1857) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1858) 	err = journal_get_superblock(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1859) 	if (err)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1860) 		return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1861) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1862) 	sb = journal->j_superblock;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1863) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1864) 	journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1865) 	journal->j_tail = be32_to_cpu(sb->s_start);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1866) 	journal->j_first = be32_to_cpu(sb->s_first);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1867) 	journal->j_errno = be32_to_cpu(sb->s_errno);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1868) 	journal->j_last = be32_to_cpu(sb->s_maxlen);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1869) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1870) 	if (jbd2_has_feature_fast_commit(journal)) {
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1871) 		journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
9bd23c31f392b (Harshad Shirwadkar         2020-11-20 12:22:32 -0800 1872) 		num_fc_blocks = jbd2_journal_get_num_fc_blks(sb);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1873) 		if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS)
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 1874) 			journal->j_last = journal->j_fc_last - num_fc_blocks;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1875) 		journal->j_fc_first = journal->j_last + 1;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1876) 		journal->j_fc_off = 0;
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1877) 	}
6866d7b3f2bb4 (Harshad Shirwadkar         2020-10-15 13:37:55 -0700 1878) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1879) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1880) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1881) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1882) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1883) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 1884)  * jbd2_journal_load() - Read journal from disk.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1885)  * @journal: Journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1886)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1887)  * Given a journal_t structure which tells us which disk blocks contain
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1888)  * a journal, read the journal from disk to initialise the in-memory
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1889)  * structures.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1890)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1891) int jbd2_journal_load(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1892) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1893) 	int err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1894) 	journal_superblock_t *sb;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1895) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1896) 	err = load_superblock(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1897) 	if (err)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1898) 		return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1899) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1900) 	sb = journal->j_superblock;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1901) 	/* If this is a V2 superblock, then we have to check the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1902) 	 * features flags on it. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1903) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1904) 	if (journal->j_format_version >= 2) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1905) 		if ((sb->s_feature_ro_compat &
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1906) 		     ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1907) 		    (sb->s_feature_incompat &
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1908) 		     ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1909) 			printk(KERN_WARNING
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1910) 				"JBD2: Unrecognised features on journal\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1911) 			return -EINVAL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1912) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1913) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1914) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1915) 	/*
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1916) 	 * Create a slab for this blocksize
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1917) 	 */
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1918) 	err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1919) 	if (err)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1920) 		return err;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 1921) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1922) 	/* Let the recovery code check whether it needs to recover any
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1923) 	 * data from the journal. */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1924) 	if (jbd2_journal_recover(journal))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1925) 		goto recovery_error;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1926) 
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1927) 	if (journal->j_failed_commit) {
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1928) 		printk(KERN_ERR "JBD2: journal transaction %u on %s "
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1929) 		       "is corrupt.\n", journal->j_failed_commit,
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1930) 		       journal->j_devname);
6a797d2737838 (Darrick J. Wong            2015-10-17 16:16:04 -0400 1931) 		return -EFSCORRUPTED;
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1932) 	}
a09decff5c320 (Kai Li                     2020-01-11 10:25:42 +0800 1933) 	/*
a09decff5c320 (Kai Li                     2020-01-11 10:25:42 +0800 1934) 	 * clear JBD2_ABORT flag initialized in journal_init_common
a09decff5c320 (Kai Li                     2020-01-11 10:25:42 +0800 1935) 	 * here to update log tail information with the newest seq.
a09decff5c320 (Kai Li                     2020-01-11 10:25:42 +0800 1936) 	 */
a09decff5c320 (Kai Li                     2020-01-11 10:25:42 +0800 1937) 	journal->j_flags &= ~JBD2_ABORT;
e6a47428de84e (Theodore Ts'o              2009-11-15 15:31:37 -0500 1938) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1939) 	/* OK, we've finished with the dynamic journal bits:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1940) 	 * reinitialise the dynamic contents of the superblock in memory
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1941) 	 * and reset them on disk. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1942) 	if (journal_reset(journal))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1943) 		goto recovery_error;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1944) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1945) 	journal->j_flags |= JBD2_LOADED;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1946) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1947) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1948) recovery_error:
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1949) 	printk(KERN_WARNING "JBD2: recovery failed\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1950) 	return -EIO;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1951) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1952) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1953) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 1954)  * jbd2_journal_destroy() - Release a journal_t structure.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1955)  * @journal: Journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1956)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1957)  * Release a journal_t structure once it is no longer in use by the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1958)  * journaled object.
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 1959)  * Return <0 if we couldn't clean up the journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1960)  */
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 1961) int jbd2_journal_destroy(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1962) {
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 1963) 	int err = 0;
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 1964) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1965) 	/* Wait for the commit thread to wake up and die. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1966) 	journal_kill_thread(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1967) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1968) 	/* Force a final log commit */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1969) 	if (journal->j_running_transaction)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1970) 		jbd2_journal_commit_transaction(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1971) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1972) 	/* Force any old transactions to disk */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1973) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1974) 	/* Totally anal locking here... */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1975) 	spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1976) 	while (journal->j_checkpoint_transactions != NULL) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1977) 		spin_unlock(&journal->j_list_lock);
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 1978) 		mutex_lock_io(&journal->j_checkpoint_mutex);
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1979) 		err = jbd2_log_do_checkpoint(journal);
1a0d3786dd57d (Theodore Ts'o              2008-11-05 00:09:22 -0500 1980) 		mutex_unlock(&journal->j_checkpoint_mutex);
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1981) 		/*
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1982) 		 * If checkpointing failed, just free the buffers to avoid
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1983) 		 * looping forever
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1984) 		 */
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1985) 		if (err) {
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1986) 			jbd2_journal_destroy_checkpoint(journal);
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1987) 			spin_lock(&journal->j_list_lock);
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1988) 			break;
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400 1989) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1990) 		spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1991) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1992) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1993) 	J_ASSERT(journal->j_running_transaction == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1994) 	J_ASSERT(journal->j_committing_transaction == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1995) 	J_ASSERT(journal->j_checkpoint_transactions == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1996) 	spin_unlock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1997) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1998) 	if (journal->j_sb_buffer) {
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 1999) 		if (!is_journal_aborted(journal)) {
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 2000) 			mutex_lock_io(&journal->j_checkpoint_mutex);
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2001) 
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2002) 			write_lock(&journal->j_state_lock);
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2003) 			journal->j_tail_sequence =
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2004) 				++journal->j_transaction_sequence;
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2005) 			write_unlock(&journal->j_state_lock);
c0a2ad9b50dd8 (OGAWA Hirofumi             2016-03-09 23:47:25 -0500 2006) 
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600 2007) 			jbd2_mark_journal_empty(journal,
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 2008) 					REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2009) 			mutex_unlock(&journal->j_checkpoint_mutex);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2010) 		} else
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2011) 			err = -EIO;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2012) 		brelse(journal->j_sb_buffer);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2013) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2014) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2015) 	if (journal->j_proc_entry)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2016) 		jbd2_stats_proc_exit(journal);
d9f39d1e44c43 (Theodore Ts'o              2014-11-25 20:02:37 -0500 2017) 	iput(journal->j_inode);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2018) 	if (journal->j_revoke)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2019) 		jbd2_journal_destroy_revoke(journal);
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 2020) 	if (journal->j_chksum_driver)
01b5adcebb977 (Darrick J. Wong            2012-05-27 07:50:56 -0400 2021) 		crypto_free_shash(journal->j_chksum_driver);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2022) 	kfree(journal->j_fc_wbuf);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2023) 	kfree(journal->j_wbuf);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2024) 	kfree(journal);
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2025) 
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2026) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2027) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2028) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2029) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2030) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2031)  * jbd2_journal_check_used_features() - Check if features specified are used.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2032)  * @journal: Journal to check.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2033)  * @compat: bitmask of compatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2034)  * @ro: bitmask of features that force read-only mount
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2035)  * @incompat: bitmask of incompatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2036)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2037)  * Check whether the journal uses all of a given set of
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2038)  * features.  Return true (non-zero) if it does.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2039)  **/
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2040) 
60ed633f51d0c (Xianting Tian              2020-07-18 08:57:37 -0400 2041) int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2042) 				 unsigned long ro, unsigned long incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2043) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2044) 	journal_superblock_t *sb;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2045) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2046) 	if (!compat && !ro && !incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2047) 		return 1;
1113e1b504f6e (Patrick J. LoPresti        2010-07-22 15:04:16 -0700 2048) 	/* Load journal superblock if it is not loaded yet. */
1113e1b504f6e (Patrick J. LoPresti        2010-07-22 15:04:16 -0700 2049) 	if (journal->j_format_version == 0 &&
1113e1b504f6e (Patrick J. LoPresti        2010-07-22 15:04:16 -0700 2050) 	    journal_get_superblock(journal) != 0)
1113e1b504f6e (Patrick J. LoPresti        2010-07-22 15:04:16 -0700 2051) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2052) 	if (journal->j_format_version == 1)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2053) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2054) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2055) 	sb = journal->j_superblock;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2056) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2057) 	if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2058) 	    ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2059) 	    ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2060) 		return 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2061) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2062) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2063) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2064) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2065) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2066)  * jbd2_journal_check_available_features() - Check feature set in journalling layer
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2067)  * @journal: Journal to check.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2068)  * @compat: bitmask of compatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2069)  * @ro: bitmask of features that force read-only mount
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2070)  * @incompat: bitmask of incompatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2071)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2072)  * Check whether the journaling code supports the use of
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2073)  * all of a given set of features on this journal.  Return true
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2074)  * (non-zero) if it can. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2075) 
60ed633f51d0c (Xianting Tian              2020-07-18 08:57:37 -0400 2076) int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2077) 				      unsigned long ro, unsigned long incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2078) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2079) 	if (!compat && !ro && !incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2080) 		return 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2081) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2082) 	/* We can support any known requested features iff the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2083) 	 * superblock is in version 2.  Otherwise we fail to support any
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2084) 	 * extended sb features. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2085) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2086) 	if (journal->j_format_version != 2)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2087) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2088) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2089) 	if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2090) 	    (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2091) 	    (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2092) 		return 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2093) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2094) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2095) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2096) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2097) static int
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2098) jbd2_journal_initialize_fast_commit(journal_t *journal)
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2099) {
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2100) 	journal_superblock_t *sb = journal->j_superblock;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2101) 	unsigned long long num_fc_blks;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2102) 
9bd23c31f392b (Harshad Shirwadkar         2020-11-20 12:22:32 -0800 2103) 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2104) 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2105) 		return -ENOSPC;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2106) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2107) 	/* Are we called twice? */
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2108) 	WARN_ON(journal->j_fc_wbuf != NULL);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2109) 	journal->j_fc_wbuf = kmalloc_array(num_fc_blks,
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2110) 				sizeof(struct buffer_head *), GFP_KERNEL);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2111) 	if (!journal->j_fc_wbuf)
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2112) 		return -ENOMEM;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2113) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2114) 	journal->j_fc_wbufsize = num_fc_blks;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2115) 	journal->j_fc_last = journal->j_last;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2116) 	journal->j_last = journal->j_fc_last - num_fc_blks;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2117) 	journal->j_fc_first = journal->j_last + 1;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2118) 	journal->j_fc_off = 0;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2119) 	journal->j_free = journal->j_last - journal->j_first;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2120) 	journal->j_max_transaction_buffers =
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2121) 		jbd2_journal_get_max_txn_bufs(journal);
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2122) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2123) 	return 0;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2124) }
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2125) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2126) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2127)  * jbd2_journal_set_features() - Mark a given journal feature in the superblock
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2128)  * @journal: Journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2129)  * @compat: bitmask of compatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2130)  * @ro: bitmask of features that force read-only mount
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2131)  * @incompat: bitmask of incompatible features
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2132)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2133)  * Mark a given journal feature as present on the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2134)  * superblock.  Returns true if the requested features could be set.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2135)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2136)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2137) 
60ed633f51d0c (Xianting Tian              2020-07-18 08:57:37 -0400 2138) int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2139) 			  unsigned long ro, unsigned long incompat)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2140) {
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2141) #define INCOMPAT_FEATURE_ON(f) \
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2142) 		((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2143) #define COMPAT_FEATURE_ON(f) \
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2144) 		((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2145) 	journal_superblock_t *sb;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2146) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2147) 	if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2148) 		return 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2149) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2150) 	if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2151) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2152) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2153) 	/* If enabling v2 checksums, turn on v3 instead */
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2154) 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2155) 		incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2156) 		incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2157) 	}
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2158) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2159) 	/* Asking for checksumming v3 and v1?  Only give them v3. */
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2160) 	if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2161) 	    compat & JBD2_FEATURE_COMPAT_CHECKSUM)
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2162) 		compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2163) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2164) 	jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2165) 		  compat, ro, incompat);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2166) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2167) 	sb = journal->j_superblock;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2168) 
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2169) 	if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) {
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2170) 		if (jbd2_journal_initialize_fast_commit(journal)) {
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2171) 			pr_err("JBD2: Cannot enable fast commits.\n");
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2172) 			return 0;
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2173) 		}
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2174) 	}
a1e5e465b31d6 (Harshad Shirwadkar         2020-11-05 19:58:55 -0800 2175) 
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2176) 	/* Load the checksum driver if necessary */
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2177) 	if ((journal->j_chksum_driver == NULL) &&
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2178) 	    INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2179) 		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2180) 		if (IS_ERR(journal->j_chksum_driver)) {
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2181) 			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2182) 			journal->j_chksum_driver = NULL;
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2183) 			return 0;
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2184) 		}
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2185) 		/* Precompute checksum seed for all metadata */
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2186) 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2187) 						   sizeof(sb->s_uuid));
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2188) 	}
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2189) 
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2190) 	lock_buffer(journal->j_sb_buffer);
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2191) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2192) 	/* If enabling v3 checksums, update superblock */
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2193) 	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2194) 		sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2195) 		sb->s_feature_compat &=
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2196) 			~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2197) 	}
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2198) 
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2199) 	/* If enabling v1 checksums, downgrade superblock */
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2200) 	if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2201) 		sb->s_feature_incompat &=
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2202) 			~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2203) 				     JBD2_FEATURE_INCOMPAT_CSUM_V3);
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2204) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2205) 	sb->s_feature_compat    |= cpu_to_be32(compat);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2206) 	sb->s_feature_ro_compat |= cpu_to_be32(ro);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2207) 	sb->s_feature_incompat  |= cpu_to_be32(incompat);
538bcaa6261b7 (Theodore Ts'o              2019-02-14 16:27:14 -0500 2208) 	unlock_buffer(journal->j_sb_buffer);
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 2209) 	journal->j_revoke_records_per_block =
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 2210) 				journal_revoke_records_per_block(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2211) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2212) 	return 1;
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2213) #undef COMPAT_FEATURE_ON
25ed6e8a54df9 (Darrick J. Wong            2012-05-27 07:48:56 -0400 2214) #undef INCOMPAT_FEATURE_ON
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2215) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2216) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2217) /*
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2218)  * jbd2_journal_clear_features() - Clear a given journal feature in the
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2219)  * 				    superblock
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2220)  * @journal: Journal to act on.
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2221)  * @compat: bitmask of compatible features
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2222)  * @ro: bitmask of features that force read-only mount
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2223)  * @incompat: bitmask of incompatible features
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2224)  *
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2225)  * Clear a given journal feature as present on the
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2226)  * superblock.
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2227)  */
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2228) void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2229) 				unsigned long ro, unsigned long incompat)
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2230) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2231) 	journal_superblock_t *sb;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2232) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2233) 	jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2234) 		  compat, ro, incompat);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2235) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2236) 	sb = journal->j_superblock;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2237) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2238) 	sb->s_feature_compat    &= ~cpu_to_be32(compat);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2239) 	sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2240) 	sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 2241) 	journal->j_revoke_records_per_block =
fdc3ef882a5d5 (Jan Kara                   2019-11-05 17:44:26 +0100 2242) 				journal_revoke_records_per_block(journal);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2243) }
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500 2244) EXPORT_SYMBOL(jbd2_journal_clear_features);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2245) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2246) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2247)  * jbd2_journal_flush() - Flush journal
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2248)  * @journal: Journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2249)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2250)  * Flush all data for a given journal to disk and empty the journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2251)  * Filesystems can use this when remounting readonly to ensure that
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2252)  * recovery does not need to happen on remount.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2253)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2254) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2255) int jbd2_journal_flush(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2256) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2257) 	int err = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2258) 	transaction_t *transaction = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2259) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2260) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2261) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2262) 	/* Force everything buffered to the log... */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2263) 	if (journal->j_running_transaction) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2264) 		transaction = journal->j_running_transaction;
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2265) 		__jbd2_log_start_commit(journal, transaction->t_tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2266) 	} else if (journal->j_committing_transaction)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2267) 		transaction = journal->j_committing_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2268) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2269) 	/* Wait for the log commit to complete... */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2270) 	if (transaction) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2271) 		tid_t tid = transaction->t_tid;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2272) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2273) 		write_unlock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2274) 		jbd2_log_wait_commit(journal, tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2275) 	} else {
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2276) 		write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2277) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2278) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2279) 	/* ...and flush everything in the log out to disk. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2280) 	spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2281) 	while (!err && journal->j_checkpoint_transactions != NULL) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2282) 		spin_unlock(&journal->j_list_lock);
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 2283) 		mutex_lock_io(&journal->j_checkpoint_mutex);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2284) 		err = jbd2_log_do_checkpoint(journal);
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2285) 		mutex_unlock(&journal->j_checkpoint_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2286) 		spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2287) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2288) 	spin_unlock(&journal->j_list_lock);
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2289) 
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2290) 	if (is_journal_aborted(journal))
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2291) 		return -EIO;
44519faf22ad6 (Hidehiro Kawai             2008-10-10 20:29:13 -0400 2292) 
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400 2293) 	mutex_lock_io(&journal->j_checkpoint_mutex);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2294) 	if (!err) {
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2295) 		err = jbd2_cleanup_journal_tail(journal);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2296) 		if (err < 0) {
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2297) 			mutex_unlock(&journal->j_checkpoint_mutex);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2298) 			goto out;
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2299) 		}
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2300) 		err = 0;
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2301) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2302) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2303) 	/* Finally, mark the journal as really needing no recovery.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2304) 	 * This sets s_start==0 in the underlying superblock, which is
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2305) 	 * the magic code for a fully-recovered superblock.  Any future
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2306) 	 * commits of data to the journal will restore the current
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2307) 	 * s_start value. */
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 2308) 	jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2309) 	mutex_unlock(&journal->j_checkpoint_mutex);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2310) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2311) 	J_ASSERT(!journal->j_running_transaction);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2312) 	J_ASSERT(!journal->j_committing_transaction);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2313) 	J_ASSERT(!journal->j_checkpoint_transactions);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2314) 	J_ASSERT(journal->j_head == journal->j_tail);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2315) 	J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2316) 	write_unlock(&journal->j_state_lock);
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2317) out:
6f6a6fda29450 (Joseph Qi                  2015-06-15 14:36:01 -0400 2318) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2319) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2320) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2321) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2322)  * jbd2_journal_wipe() - Wipe journal contents
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2323)  * @journal: Journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2324)  * @write: flag (see below)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2325)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2326)  * Wipe out all of the contents of a journal, safely.  This will produce
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2327)  * a warning if the journal contains any valid recovery information.
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2328)  * Must be called between journal_init_*() and jbd2_journal_load().
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2329)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2330)  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2331)  * we merely suppress recovery.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2332)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2333) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2334) int jbd2_journal_wipe(journal_t *journal, int write)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2335) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2336) 	int err = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2337) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2338) 	J_ASSERT (!(journal->j_flags & JBD2_LOADED));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2339) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2340) 	err = load_superblock(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2341) 	if (err)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2342) 		return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2343) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2344) 	if (!journal->j_tail)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2345) 		goto no_recovery;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2346) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 2347) 	printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2348) 		write ? "Clearing" : "Ignoring");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2349) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2350) 	err = jbd2_journal_skip_recovery(journal);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2351) 	if (write) {
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2352) 		/* Lock to make assertions happy... */
53cf978457325 (Xiaoguang Wang             2019-01-31 23:42:11 -0500 2353) 		mutex_lock_io(&journal->j_checkpoint_mutex);
17f423b516076 (Jan Kara                   2017-05-04 11:01:31 -0400 2354) 		jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2355) 		mutex_unlock(&journal->j_checkpoint_mutex);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400 2356) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2357) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2358)  no_recovery:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2359) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2360) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2361) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2362) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2363)  * jbd2_journal_abort () - Shutdown the journal immediately.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2364)  * @journal: the journal to shutdown.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2365)  * @errno:   an error number to record in the journal indicating
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2366)  *           the reason for the shutdown.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2367)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2368)  * Perform a complete, immediate shutdown of the ENTIRE
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2369)  * journal (not of a single transaction).  This operation cannot be
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2370)  * undone without closing and reopening the journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2371)  *
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2372)  * The jbd2_journal_abort function is intended to support higher level error
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2373)  * recovery mechanisms such as the ext2/ext3 remount-readonly error
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2374)  * mode.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2375)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2376)  * Journal abort has very specific semantics.  Any existing dirty,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2377)  * unjournaled buffers in the main filesystem will still be written to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2378)  * disk by bdflush, but the journaling mechanism will be suspended
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2379)  * immediately and no further transaction commits will be honoured.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2380)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2381)  * Any dirty, journaled buffers will be written back to disk without
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2382)  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2383)  * filesystem, but we _do_ attempt to leave as much data as possible
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2384)  * behind for fsck to use for cleanup.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2385)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2386)  * Any attempt to get a new transaction handle on a journal which is in
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2387)  * ABORT state will just result in an -EROFS error return.  A
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2388)  * jbd2_journal_stop on an existing handle will return -EIO if we have
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2389)  * entered abort state during the update.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2390)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2391)  * Recursive transactions are not disturbed by journal abort until the
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2392)  * final jbd2_journal_stop, which will receive the -EIO error.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2393)  *
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2394)  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2395)  * which will be recorded (if possible) in the journal superblock.  This
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2396)  * allows a client to record failure conditions in the middle of a
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2397)  * transaction without having to complete the transaction to record the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2398)  * failure to disk.  ext3_error, for example, now uses this
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2399)  * functionality.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2400)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2401)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2402) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2403) void jbd2_journal_abort(journal_t *journal, int errno)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2404) {
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2405) 	transaction_t *transaction;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2406) 
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2407) 	/*
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2408) 	 * Lock the aborting procedure until everything is done, this avoid
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2409) 	 * races between filesystem's error handling flow (e.g. ext4_abort()),
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2410) 	 * ensure panic after the error info is written into journal's
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2411) 	 * superblock.
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2412) 	 */
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2413) 	mutex_lock(&journal->j_abort_mutex);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2414) 	/*
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2415) 	 * ESHUTDOWN always takes precedence because a file system check
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2416) 	 * caused by any other journal abort error is not required after
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2417) 	 * a shutdown triggered.
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2418) 	 */
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2419) 	write_lock(&journal->j_state_lock);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2420) 	if (journal->j_flags & JBD2_ABORT) {
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2421) 		int old_errno = journal->j_errno;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2422) 
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2423) 		write_unlock(&journal->j_state_lock);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2424) 		if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) {
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2425) 			journal->j_errno = errno;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2426) 			jbd2_journal_update_sb_errno(journal);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2427) 		}
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2428) 		mutex_unlock(&journal->j_abort_mutex);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2429) 		return;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2430) 	}
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2431) 
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2432) 	/*
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2433) 	 * Mark the abort as occurred and start current running transaction
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2434) 	 * to release all journaled buffer.
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2435) 	 */
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2436) 	pr_err("Aborting journal on device %s.\n", journal->j_devname);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2437) 
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2438) 	journal->j_flags |= JBD2_ABORT;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2439) 	journal->j_errno = errno;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2440) 	transaction = journal->j_running_transaction;
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2441) 	if (transaction)
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2442) 		__jbd2_log_start_commit(journal, transaction->t_tid);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2443) 	write_unlock(&journal->j_state_lock);
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2444) 
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2445) 	/*
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2446) 	 * Record errno to the journal super block, so that fsck and jbd2
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2447) 	 * layer could realise that a filesystem check is needed.
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2448) 	 */
7f6225e446cc8 (zhangyi (F)                2019-12-04 20:46:14 +0800 2449) 	jbd2_journal_update_sb_errno(journal);
7b97d868b7ab2 (zhangyi (F)                2020-06-09 15:35:40 +0800 2450) 	mutex_unlock(&journal->j_abort_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2451) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2452) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2453) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2454)  * jbd2_journal_errno() - returns the journal's error state.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2455)  * @journal: journal to examine.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2456)  *
bfcd3555af478 (Alberto Bertogli           2009-06-09 00:06:20 -0400 2457)  * This is the errno number set with jbd2_journal_abort(), the last
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2458)  * time the journal was mounted - if the journal was stopped
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2459)  * without calling abort this will be 0.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2460)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2461)  * If the journal has been aborted on this mount time -EROFS will
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2462)  * be returned.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2463)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2464) int jbd2_journal_errno(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2465) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2466) 	int err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2467) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2468) 	read_lock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2469) 	if (journal->j_flags & JBD2_ABORT)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2470) 		err = -EROFS;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2471) 	else
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2472) 		err = journal->j_errno;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2473) 	read_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2474) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2475) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2476) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2477) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2478)  * jbd2_journal_clear_err() - clears the journal's error state
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2479)  * @journal: journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2480)  *
bfcd3555af478 (Alberto Bertogli           2009-06-09 00:06:20 -0400 2481)  * An error must be cleared or acked to take a FS out of readonly
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2482)  * mode.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2483)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2484) int jbd2_journal_clear_err(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2485) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2486) 	int err = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2487) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2488) 	write_lock(&journal->j_state_lock);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2489) 	if (journal->j_flags & JBD2_ABORT)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2490) 		err = -EROFS;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2491) 	else
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2492) 		journal->j_errno = 0;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2493) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2494) 	return err;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2495) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2496) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2497) /**
2bf31d94423c8 (Mauro Carvalho Chehab      2020-11-16 11:18:08 +0100 2498)  * jbd2_journal_ack_err() - Ack journal err.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2499)  * @journal: journal to act on.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2500)  *
bfcd3555af478 (Alberto Bertogli           2009-06-09 00:06:20 -0400 2501)  * An error must be cleared or acked to take a FS out of readonly
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2502)  * mode.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2503)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2504) void jbd2_journal_ack_err(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2505) {
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2506) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2507) 	if (journal->j_errno)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2508) 		journal->j_flags |= JBD2_ACK_ERR;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 2509) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2510) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2511) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2512) int jbd2_journal_blocks_per_page(struct inode *inode)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2513) {
09cbfeaf1a5a6 (Kirill A. Shutemov         2016-04-01 15:29:47 +0300 2514) 	return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2515) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2516) 
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2517) /*
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2518)  * helper functions to deal with 32 or 64bit block numbers.
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2519)  */
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2520) size_t journal_tag_bytes(journal_t *journal)
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2521) {
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2522) 	size_t sz;
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2523) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 2524) 	if (jbd2_has_feature_csum3(journal))
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2525) 		return sizeof(journal_block_tag3_t);
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2526) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2527) 	sz = sizeof(journal_block_tag_t);
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400 2528) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 2529) 	if (jbd2_has_feature_csum2(journal))
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2530) 		sz += sizeof(__u16);
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400 2531) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400 2532) 	if (jbd2_has_feature_64bit(journal))
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2533) 		return sz;
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2534) 	else
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400 2535) 		return sz - sizeof(__u32);
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2536) }
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700 2537) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2538) /*
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2539)  * JBD memory management
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2540)  *
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2541)  * These functions are used to allocate block-sized chunks of memory
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2542)  * used for making copies of buffer_head data.  Very often it will be
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2543)  * page-sized chunks of data, but sometimes it will be in
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2544)  * sub-page-size chunks.  (For example, 16k pages on Power systems
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2545)  * with a 4k block file system.)  For blocks smaller than a page, we
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2546)  * use a SLAB allocator.  There are slab caches for each block size,
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2547)  * which are allocated at mount time, if necessary, and we only free
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2548)  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2549)  * this reason we don't need to a mutex to protect access to
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2550)  * jbd2_slab[] allocating or releasing memory; only in
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2551)  * jbd2_journal_create_slab().
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2552)  */
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2553) #define JBD2_MAX_SLABS 8
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2554) static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2555) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2556) static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2557) 	"jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2558) 	"jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2559) };
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2560) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2561) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2562) static void jbd2_journal_destroy_slabs(void)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2563) {
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2564) 	int i;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2565) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2566) 	for (i = 0; i < JBD2_MAX_SLABS; i++) {
8bdd5b60e0273 (Wang Long                  2018-05-20 22:38:26 -0400 2567) 		kmem_cache_destroy(jbd2_slab[i]);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2568) 		jbd2_slab[i] = NULL;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2569) 	}
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2570) }
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2571) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2572) static int jbd2_journal_create_slab(size_t size)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2573) {
51dfacdef38b1 (Thomas Gleixner            2010-10-16 22:34:39 +0200 2574) 	static DEFINE_MUTEX(jbd2_slab_create_mutex);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2575) 	int i = order_base_2(size) - 10;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2576) 	size_t slab_size;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2577) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2578) 	if (size == PAGE_SIZE)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2579) 		return 0;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2580) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2581) 	if (i >= JBD2_MAX_SLABS)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2582) 		return -EINVAL;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2583) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2584) 	if (unlikely(i < 0))
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2585) 		i = 0;
51dfacdef38b1 (Thomas Gleixner            2010-10-16 22:34:39 +0200 2586) 	mutex_lock(&jbd2_slab_create_mutex);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2587) 	if (jbd2_slab[i]) {
51dfacdef38b1 (Thomas Gleixner            2010-10-16 22:34:39 +0200 2588) 		mutex_unlock(&jbd2_slab_create_mutex);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2589) 		return 0;	/* Already created */
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2590) 	}
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2591) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2592) 	slab_size = 1 << (i+10);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2593) 	jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2594) 					 slab_size, 0, NULL);
51dfacdef38b1 (Thomas Gleixner            2010-10-16 22:34:39 +0200 2595) 	mutex_unlock(&jbd2_slab_create_mutex);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2596) 	if (!jbd2_slab[i]) {
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2597) 		printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2598) 		return -ENOMEM;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2599) 	}
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2600) 	return 0;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2601) }
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2602) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2603) static struct kmem_cache *get_slab(size_t size)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2604) {
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2605) 	int i = order_base_2(size) - 10;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2606) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2607) 	BUG_ON(i >= JBD2_MAX_SLABS);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2608) 	if (unlikely(i < 0))
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2609) 		i = 0;
8ac97b74bca1f (Bill Pemberton             2010-04-30 09:34:31 -0400 2610) 	BUG_ON(jbd2_slab[i] == NULL);
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2611) 	return jbd2_slab[i];
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2612) }
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2613) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2614) void *jbd2_alloc(size_t size, gfp_t flags)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2615) {
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2616) 	void *ptr;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2617) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2618) 	BUG_ON(size & (size-1)); /* Must be a power of 2 */
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2619) 
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2620) 	if (size < PAGE_SIZE)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2621) 		ptr = kmem_cache_alloc(get_slab(size), flags);
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2622) 	else
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2623) 		ptr = (void *)__get_free_pages(flags, get_order(size));
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2624) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2625) 	/* Check alignment; SLUB has gotten this wrong in the past,
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2626) 	 * and this can lead to user data corruption! */
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2627) 	BUG_ON(((unsigned long) ptr) & (size-1));
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2628) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2629) 	return ptr;
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2630) }
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2631) 
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2632) void jbd2_free(void *ptr, size_t size)
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2633) {
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2634) 	if (size < PAGE_SIZE)
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2635) 		kmem_cache_free(get_slab(size), ptr);
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2636) 	else
f2db19719a4e7 (Michal Hocko               2016-06-24 14:49:28 -0700 2637) 		free_pages((unsigned long)ptr, get_order(size));
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2638) };
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2639) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2640) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2641)  * Journal_head storage management
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2642)  */
e18b890bb0881 (Christoph Lameter          2006-12-06 20:33:20 -0800 2643) static struct kmem_cache *jbd2_journal_head_cache;
e23291b9120c1 (Jose R. Santos             2007-07-18 08:57:06 -0400 2644) #ifdef CONFIG_JBD2_DEBUG
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2645) static atomic_t nr_journal_heads = ATOMIC_INIT(0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2646) #endif
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2647) 
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2648) static int __init jbd2_journal_init_journal_head_cache(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2649) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2650) 	J_ASSERT(!jbd2_journal_head_cache);
a920e9416b346 (Johann Lombardi            2006-10-11 01:21:00 -0700 2651) 	jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2652) 				sizeof(struct journal_head),
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2653) 				0,		/* offset */
5f0d5a3ae7cff (Paul E. McKenney           2017-01-18 02:53:44 -0800 2654) 				SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU,
20c2df83d25c6 (Paul Mundt                 2007-07-20 10:11:58 +0900 2655) 				NULL);		/* ctor */
1076d17ac70d1 (Al Viro                    2008-03-29 03:07:18 +0000 2656) 	if (!jbd2_journal_head_cache) {
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 2657) 		printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2658) 		return -ENOMEM;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2659) 	}
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2660) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2661) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2662) 
4185a2ac422c7 (Yongqiang Yang             2012-02-20 17:53:03 -0500 2663) static void jbd2_journal_destroy_journal_head_cache(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2664) {
8bdd5b60e0273 (Wang Long                  2018-05-20 22:38:26 -0400 2665) 	kmem_cache_destroy(jbd2_journal_head_cache);
8bdd5b60e0273 (Wang Long                  2018-05-20 22:38:26 -0400 2666) 	jbd2_journal_head_cache = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2667) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2668) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2669) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2670)  * journal_head splicing and dicing
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2671)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2672) static struct journal_head *journal_alloc_journal_head(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2673) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2674) 	struct journal_head *ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2675) 
e23291b9120c1 (Jose R. Santos             2007-07-18 08:57:06 -0400 2676) #ifdef CONFIG_JBD2_DEBUG
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2677) 	atomic_inc(&nr_journal_heads);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2678) #endif
5d9cf9c6254dd (Zheng Liu                  2013-05-28 07:27:11 -0400 2679) 	ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
1076d17ac70d1 (Al Viro                    2008-03-29 03:07:18 +0000 2680) 	if (!ret) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2681) 		jbd_debug(1, "out of memory for journal_head\n");
670be5a78ac7c (Theodore Ts'o              2010-12-17 10:44:16 -0500 2682) 		pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
7b506b1035326 (Michal Hocko               2015-06-15 15:45:58 -0400 2683) 		ret = kmem_cache_zalloc(jbd2_journal_head_cache,
7b506b1035326 (Michal Hocko               2015-06-15 15:45:58 -0400 2684) 				GFP_NOFS | __GFP_NOFAIL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2685) 	}
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200 2686) 	if (ret)
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200 2687) 		spin_lock_init(&ret->b_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2688) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2689) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2690) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2691) static void journal_free_journal_head(struct journal_head *jh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2692) {
e23291b9120c1 (Jose R. Santos             2007-07-18 08:57:06 -0400 2693) #ifdef CONFIG_JBD2_DEBUG
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2694) 	atomic_dec(&nr_journal_heads);
cd02ff0b14b7e (Mingming Cao               2007-10-16 18:38:25 -0400 2695) 	memset(jh, JBD2_POISON_FREE, sizeof(*jh));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2696) #endif
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2697) 	kmem_cache_free(jbd2_journal_head_cache, jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2698) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2699) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2700) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2701)  * A journal_head is attached to a buffer_head whenever JBD has an
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2702)  * interest in the buffer.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2703)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2704)  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2705)  * is set.  This bit is tested in core kernel code where we need to take
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2706)  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2707)  * there.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2708)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2709)  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2710)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2711)  * When a buffer has its BH_JBD bit set it is immune from being released by
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2712)  * core kernel code, mainly via ->b_count.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2713)  *
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2714)  * A journal_head is detached from its buffer_head when the journal_head's
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2715)  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2716)  * transaction (b_cp_transaction) hold their references to b_jcount.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2717)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2718)  * Various places in the kernel want to attach a journal_head to a buffer_head
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2719)  * _before_ attaching the journal_head to a transaction.  To protect the
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2720)  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2721)  * journal_head's b_jcount refcount by one.  The caller must call
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2722)  * jbd2_journal_put_journal_head() to undo this.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2723)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2724)  * So the typical usage would be:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2725)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2726)  *	(Attach a journal_head if needed.  Increments b_jcount)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2727)  *	struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2728)  *	...
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2729)  *      (Get another reference for transaction)
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2730)  *	jbd2_journal_grab_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2731)  *	jh->b_transaction = xxx;
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2732)  *	(Put original reference)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2733)  *	jbd2_journal_put_journal_head(jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2734)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2735) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2736) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2737)  * Give a buffer_head a journal_head.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2738)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2739)  * May sleep.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2740)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2741) struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2742) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2743) 	struct journal_head *jh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2744) 	struct journal_head *new_jh = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2745) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2746) repeat:
5d9cf9c6254dd (Zheng Liu                  2013-05-28 07:27:11 -0400 2747) 	if (!buffer_jbd(bh))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2748) 		new_jh = journal_alloc_journal_head();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2749) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2750) 	jbd_lock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2751) 	if (buffer_jbd(bh)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2752) 		jh = bh2jh(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2753) 	} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2754) 		J_ASSERT_BH(bh,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2755) 			(atomic_read(&bh->b_count) > 0) ||
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2756) 			(bh->b_page && bh->b_page->mapping));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2757) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2758) 		if (!new_jh) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2759) 			jbd_unlock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2760) 			goto repeat;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2761) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2762) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2763) 		jh = new_jh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2764) 		new_jh = NULL;		/* We consumed it */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2765) 		set_buffer_jbd(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2766) 		bh->b_private = jh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2767) 		jh->b_bh = bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2768) 		get_bh(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2769) 		BUFFER_TRACE(bh, "added journal_head");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2770) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2771) 	jh->b_jcount++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2772) 	jbd_unlock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2773) 	if (new_jh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2774) 		journal_free_journal_head(new_jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2775) 	return bh->b_private;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2776) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2777) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2778) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2779)  * Grab a ref against this buffer_head's journal_head.  If it ended up not
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2780)  * having a journal_head, return NULL
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2781)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2782) struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2783) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2784) 	struct journal_head *jh = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2785) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2786) 	jbd_lock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2787) 	if (buffer_jbd(bh)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2788) 		jh = bh2jh(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2789) 		jh->b_jcount++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2790) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2791) 	jbd_unlock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2792) 	return jh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2793) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2794) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2795) static void __journal_remove_journal_head(struct buffer_head *bh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2796) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2797) 	struct journal_head *jh = bh2jh(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2798) 
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2799) 	J_ASSERT_JH(jh, jh->b_transaction == NULL);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2800) 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2801) 	J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2802) 	J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2803) 	J_ASSERT_BH(bh, buffer_jbd(bh));
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2804) 	J_ASSERT_BH(bh, jh2bh(jh) == bh);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2805) 	BUFFER_TRACE(bh, "remove journal_head");
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2806) 
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2807) 	/* Unlink before dropping the lock */
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2808) 	bh->b_private = NULL;
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2809) 	jh->b_bh = NULL;	/* debug, really */
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2810) 	clear_buffer_jbd(bh);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2811) }
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2812) 
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2813) static void journal_release_journal_head(struct journal_head *jh, size_t b_size)
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2814) {
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2815) 	if (jh->b_frozen_data) {
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2816) 		printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2817) 		jbd2_free(jh->b_frozen_data, b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2818) 	}
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2819) 	if (jh->b_committed_data) {
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2820) 		printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2821) 		jbd2_free(jh->b_committed_data, b_size);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2822) 	}
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2823) 	journal_free_journal_head(jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2824) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2825) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2826) /*
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2827)  * Drop a reference on the passed journal_head.  If it fell to zero then
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2828)  * release the journal_head from the buffer_head.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2829)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2830) void jbd2_journal_put_journal_head(struct journal_head *jh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2831) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2832) 	struct buffer_head *bh = jh2bh(jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2833) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2834) 	jbd_lock_bh_journal_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2835) 	J_ASSERT_JH(jh, jh->b_jcount > 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2836) 	--jh->b_jcount;
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2837) 	if (!jh->b_jcount) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2838) 		__journal_remove_journal_head(bh);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2839) 		jbd_unlock_bh_journal_head(bh);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2840) 		journal_release_journal_head(jh, bh->b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2841) 		__brelse(bh);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2842) 	} else {
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 2843) 		jbd_unlock_bh_journal_head(bh);
7855a57d008b2 (Thomas Gleixner            2019-08-09 14:42:33 +0200 2844) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2845) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2846) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2847) /*
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2848)  * Initialize jbd inode head
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2849)  */
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2850) void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2851) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2852) 	jinode->i_transaction = NULL;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2853) 	jinode->i_next_transaction = NULL;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2854) 	jinode->i_vfs_inode = inode;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2855) 	jinode->i_flags = 0;
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400 2856) 	jinode->i_dirty_start = 0;
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400 2857) 	jinode->i_dirty_end = 0;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2858) 	INIT_LIST_HEAD(&jinode->i_list);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2859) }
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2860) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2861) /*
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2862)  * Function to be called before we start removing inode from memory (i.e.,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2863)  * clear_inode() is a fine place to be called from). It removes inode from
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2864)  * transaction's lists.
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2865)  */
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2866) void jbd2_journal_release_jbd_inode(journal_t *journal,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2867) 				    struct jbd2_inode *jinode)
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2868) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2869) 	if (!journal)
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2870) 		return;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2871) restart:
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2872) 	spin_lock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2873) 	/* Is commit writing out inode - we have to wait */
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500 2874) 	if (jinode->i_flags & JI_COMMIT_RUNNING) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2875) 		wait_queue_head_t *wq;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2876) 		DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2877) 		wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2141713616c65 (Ingo Molnar                2017-03-05 11:25:39 +0100 2878) 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2879) 		spin_unlock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2880) 		schedule();
2141713616c65 (Ingo Molnar                2017-03-05 11:25:39 +0100 2881) 		finish_wait(wq, &wait.wq_entry);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2882) 		goto restart;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2883) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2884) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2885) 	if (jinode->i_transaction) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2886) 		list_del(&jinode->i_list);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2887) 		jinode->i_transaction = NULL;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2888) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2889) 	spin_unlock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2890) }
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400 2891) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2892) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2893) #ifdef CONFIG_PROC_FS
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2894) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2895) #define JBD2_STATS_PROC_NAME "fs/jbd2"
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2896) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2897) static void __init jbd2_create_jbd_stats_proc_entry(void)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2898) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2899) 	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2900) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2901) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2902) static void __exit jbd2_remove_jbd_stats_proc_entry(void)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2903) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2904) 	if (proc_jbd2_stats)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2905) 		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2906) }
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2907) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2908) #else
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2909) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2910) #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2911) #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2912) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2913) #endif
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 2914) 
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500 2915) struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2916) 
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2917) static int __init jbd2_journal_init_inode_cache(void)
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2918) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2919) 	J_ASSERT(!jbd2_inode_cache);
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2920) 	jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2921) 	if (!jbd2_inode_cache) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2922) 		pr_emerg("JBD2: failed to create inode cache\n");
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2923) 		return -ENOMEM;
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2924) 	}
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2925) 	return 0;
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2926) }
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2927) 
4185a2ac422c7 (Yongqiang Yang             2012-02-20 17:53:03 -0500 2928) static int __init jbd2_journal_init_handle_cache(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2929) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2930) 	J_ASSERT(!jbd2_handle_cache);
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500 2931) 	jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2932) 	if (!jbd2_handle_cache) {
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500 2933) 		printk(KERN_EMERG "JBD2: failed to create handle cache\n");
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500 2934) 		return -ENOMEM;
8aefcd557d26d (Theodore Ts'o              2011-01-10 12:29:43 -0500 2935) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2936) 	return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2937) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2938) 
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2939) static void jbd2_journal_destroy_inode_cache(void)
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2940) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2941) 	kmem_cache_destroy(jbd2_inode_cache);
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2942) 	jbd2_inode_cache = NULL;
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2943) }
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2944) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2945) static void jbd2_journal_destroy_handle_cache(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2946) {
8bdd5b60e0273 (Wang Long                  2018-05-20 22:38:26 -0400 2947) 	kmem_cache_destroy(jbd2_handle_cache);
8bdd5b60e0273 (Wang Long                  2018-05-20 22:38:26 -0400 2948) 	jbd2_handle_cache = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2949) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2950) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2951) /*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2952)  * Module startup and shutdown
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2953)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2954) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2955) static int __init journal_init_caches(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2956) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2957) 	int ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2958) 
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2959) 	ret = jbd2_journal_init_revoke_record_cache();
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2960) 	if (ret == 0)
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2961) 		ret = jbd2_journal_init_revoke_table_cache();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2962) 	if (ret == 0)
4185a2ac422c7 (Yongqiang Yang             2012-02-20 17:53:03 -0500 2963) 		ret = jbd2_journal_init_journal_head_cache();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2964) 	if (ret == 0)
4185a2ac422c7 (Yongqiang Yang             2012-02-20 17:53:03 -0500 2965) 		ret = jbd2_journal_init_handle_cache();
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2966) 	if (ret == 0)
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2967) 		ret = jbd2_journal_init_inode_cache();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2968) 	if (ret == 0)
0c2022eccb016 (Yongqiang Yang             2012-02-20 17:53:02 -0500 2969) 		ret = jbd2_journal_init_transaction_cache();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2970) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2971) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2972) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2973) static void jbd2_journal_destroy_caches(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2974) {
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2975) 	jbd2_journal_destroy_revoke_record_cache();
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2976) 	jbd2_journal_destroy_revoke_table_cache();
4185a2ac422c7 (Yongqiang Yang             2012-02-20 17:53:03 -0500 2977) 	jbd2_journal_destroy_journal_head_cache();
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2978) 	jbd2_journal_destroy_handle_cache();
0d52154bb0a70 (Chengguang Xu              2019-05-10 21:15:47 -0400 2979) 	jbd2_journal_destroy_inode_cache();
0c2022eccb016 (Yongqiang Yang             2012-02-20 17:53:02 -0500 2980) 	jbd2_journal_destroy_transaction_cache();
d2eecb0393687 (Theodore Ts'o              2009-12-07 10:36:20 -0500 2981) 	jbd2_journal_destroy_slabs();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2982) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2983) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2984) static int __init journal_init(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2985) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2986) 	int ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2987) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2988) 	BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2989) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2990) 	ret = journal_init_caches();
620de4e19890c (Duane Griffin              2008-04-29 22:02:47 -0400 2991) 	if (ret == 0) {
620de4e19890c (Duane Griffin              2008-04-29 22:02:47 -0400 2992) 		jbd2_create_jbd_stats_proc_entry();
620de4e19890c (Duane Griffin              2008-04-29 22:02:47 -0400 2993) 	} else {
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 2994) 		jbd2_journal_destroy_caches();
620de4e19890c (Duane Griffin              2008-04-29 22:02:47 -0400 2995) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2996) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2997) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2998) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 2999) static void __exit journal_exit(void)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3000) {
e23291b9120c1 (Jose R. Santos             2007-07-18 08:57:06 -0400 3001) #ifdef CONFIG_JBD2_DEBUG
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3002) 	int n = atomic_read(&nr_journal_heads);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3003) 	if (n)
75685071cd5b2 (Jan Kara                   2013-12-08 21:13:59 -0500 3004) 		printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3005) #endif
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 3006) 	jbd2_remove_jbd_stats_proc_entry();
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 3007) 	jbd2_journal_destroy_caches();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3008) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3009) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3010) MODULE_LICENSE("GPL");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3011) module_init(journal_init);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3012) module_exit(journal_exit);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 3013)