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