| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1 | /* | 
| Uwe Kleine-König | 5886269 | 2007-05-09 07:51:49 +0200 | [diff] [blame] | 2 |  * linux/fs/jbd2/transaction.c | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 3 |  * | 
 | 4 |  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | 
 | 5 |  * | 
 | 6 |  * Copyright 1998 Red Hat corp --- All Rights Reserved | 
 | 7 |  * | 
 | 8 |  * This file is part of the Linux kernel and is made available under | 
 | 9 |  * the terms of the GNU General Public License, version 2, or at your | 
 | 10 |  * option, any later version, incorporated herein by reference. | 
 | 11 |  * | 
 | 12 |  * Generic filesystem transaction handling code; part of the ext2fs | 
 | 13 |  * journaling system. | 
 | 14 |  * | 
 | 15 |  * This file manages transactions (compound commits managed by the | 
 | 16 |  * journaling code) and handles (individual atomic operations by the | 
 | 17 |  * filesystem). | 
 | 18 |  */ | 
 | 19 |  | 
 | 20 | #include <linux/time.h> | 
 | 21 | #include <linux/fs.h> | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 22 | #include <linux/jbd2.h> | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 23 | #include <linux/errno.h> | 
 | 24 | #include <linux/slab.h> | 
 | 25 | #include <linux/timer.h> | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> | 
 | 27 | #include <linux/highmem.h> | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 28 | #include <linux/hrtimer.h> | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 29 |  | 
| Adrian Bunk | 7ddae86 | 2006-12-06 20:38:27 -0800 | [diff] [blame] | 30 | static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh); | 
 | 31 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 32 | /* | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 33 |  * jbd2_get_transaction: obtain a new transaction_t object. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 34 |  * | 
 | 35 |  * Simply allocate and initialise a new transaction.  Create it in | 
 | 36 |  * RUNNING state and add it to the current journal (which should not | 
 | 37 |  * have an existing running transaction: we only make a new transaction | 
 | 38 |  * once we have started to commit the old one). | 
 | 39 |  * | 
 | 40 |  * Preconditions: | 
 | 41 |  *	The journal MUST be locked.  We don't perform atomic mallocs on the | 
 | 42 |  *	new transaction	and we can't block without protecting against other | 
 | 43 |  *	processes trying to touch the journal while it is in transition. | 
 | 44 |  * | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 45 |  */ | 
 | 46 |  | 
 | 47 | static transaction_t * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 48 | jbd2_get_transaction(journal_t *journal, transaction_t *transaction) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 49 | { | 
 | 50 | 	transaction->t_journal = journal; | 
 | 51 | 	transaction->t_state = T_RUNNING; | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 52 | 	transaction->t_start_time = ktime_get(); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 53 | 	transaction->t_tid = journal->j_transaction_sequence++; | 
 | 54 | 	transaction->t_expires = jiffies + journal->j_commit_interval; | 
 | 55 | 	spin_lock_init(&transaction->t_handle_lock); | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 56 | 	INIT_LIST_HEAD(&transaction->t_inode_list); | 
| Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 57 | 	INIT_LIST_HEAD(&transaction->t_private_list); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 58 |  | 
 | 59 | 	/* Set up the commit timer for the new transaction. */ | 
| Mingming Cao | db857da | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 60 | 	journal->j_commit_timer.expires = round_jiffies(transaction->t_expires); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 61 | 	add_timer(&journal->j_commit_timer); | 
 | 62 |  | 
 | 63 | 	J_ASSERT(journal->j_running_transaction == NULL); | 
 | 64 | 	journal->j_running_transaction = transaction; | 
| Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 65 | 	transaction->t_max_wait = 0; | 
 | 66 | 	transaction->t_start = jiffies; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 67 |  | 
 | 68 | 	return transaction; | 
 | 69 | } | 
 | 70 |  | 
 | 71 | /* | 
 | 72 |  * Handle management. | 
 | 73 |  * | 
 | 74 |  * A handle_t is an object which represents a single atomic update to a | 
 | 75 |  * filesystem, and which tracks all of the modifications which form part | 
 | 76 |  * of that one update. | 
 | 77 |  */ | 
 | 78 |  | 
 | 79 | /* | 
 | 80 |  * start_this_handle: Given a handle, deal with any locking or stalling | 
 | 81 |  * needed to make sure that there is enough journal space for the handle | 
 | 82 |  * to begin.  Attach the handle to a transaction and set up the | 
 | 83 |  * transaction's buffer credits. | 
 | 84 |  */ | 
 | 85 |  | 
 | 86 | static int start_this_handle(journal_t *journal, handle_t *handle) | 
 | 87 | { | 
 | 88 | 	transaction_t *transaction; | 
 | 89 | 	int needed; | 
 | 90 | 	int nblocks = handle->h_buffer_credits; | 
 | 91 | 	transaction_t *new_transaction = NULL; | 
 | 92 | 	int ret = 0; | 
| Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 93 | 	unsigned long ts = jiffies; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 94 |  | 
 | 95 | 	if (nblocks > journal->j_max_transaction_buffers) { | 
 | 96 | 		printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n", | 
 | 97 | 		       current->comm, nblocks, | 
 | 98 | 		       journal->j_max_transaction_buffers); | 
 | 99 | 		ret = -ENOSPC; | 
 | 100 | 		goto out; | 
 | 101 | 	} | 
 | 102 |  | 
 | 103 | alloc_transaction: | 
 | 104 | 	if (!journal->j_running_transaction) { | 
| Mingming Cao | d802ffa | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 105 | 		new_transaction = kzalloc(sizeof(*new_transaction), | 
| Mingming Cao | 2d917969 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 106 | 						GFP_NOFS|__GFP_NOFAIL); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 107 | 		if (!new_transaction) { | 
 | 108 | 			ret = -ENOMEM; | 
 | 109 | 			goto out; | 
 | 110 | 		} | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 111 | 	} | 
 | 112 |  | 
 | 113 | 	jbd_debug(3, "New handle %p going live.\n", handle); | 
 | 114 |  | 
 | 115 | repeat: | 
 | 116 |  | 
 | 117 | 	/* | 
 | 118 | 	 * We need to hold j_state_lock until t_updates has been incremented, | 
 | 119 | 	 * for proper journal barrier handling | 
 | 120 | 	 */ | 
 | 121 | 	spin_lock(&journal->j_state_lock); | 
 | 122 | repeat_locked: | 
 | 123 | 	if (is_journal_aborted(journal) || | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 124 | 	    (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) { | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 125 | 		spin_unlock(&journal->j_state_lock); | 
 | 126 | 		ret = -EROFS; | 
 | 127 | 		goto out; | 
 | 128 | 	} | 
 | 129 |  | 
 | 130 | 	/* Wait on the journal's transaction barrier if necessary */ | 
 | 131 | 	if (journal->j_barrier_count) { | 
 | 132 | 		spin_unlock(&journal->j_state_lock); | 
 | 133 | 		wait_event(journal->j_wait_transaction_locked, | 
 | 134 | 				journal->j_barrier_count == 0); | 
 | 135 | 		goto repeat; | 
 | 136 | 	} | 
 | 137 |  | 
 | 138 | 	if (!journal->j_running_transaction) { | 
 | 139 | 		if (!new_transaction) { | 
 | 140 | 			spin_unlock(&journal->j_state_lock); | 
 | 141 | 			goto alloc_transaction; | 
 | 142 | 		} | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 143 | 		jbd2_get_transaction(journal, new_transaction); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 144 | 		new_transaction = NULL; | 
 | 145 | 	} | 
 | 146 |  | 
 | 147 | 	transaction = journal->j_running_transaction; | 
 | 148 |  | 
 | 149 | 	/* | 
 | 150 | 	 * If the current transaction is locked down for commit, wait for the | 
 | 151 | 	 * lock to be released. | 
 | 152 | 	 */ | 
 | 153 | 	if (transaction->t_state == T_LOCKED) { | 
 | 154 | 		DEFINE_WAIT(wait); | 
 | 155 |  | 
 | 156 | 		prepare_to_wait(&journal->j_wait_transaction_locked, | 
 | 157 | 					&wait, TASK_UNINTERRUPTIBLE); | 
 | 158 | 		spin_unlock(&journal->j_state_lock); | 
 | 159 | 		schedule(); | 
 | 160 | 		finish_wait(&journal->j_wait_transaction_locked, &wait); | 
 | 161 | 		goto repeat; | 
 | 162 | 	} | 
 | 163 |  | 
 | 164 | 	/* | 
 | 165 | 	 * If there is not enough space left in the log to write all potential | 
 | 166 | 	 * buffers requested by this operation, we need to stall pending a log | 
 | 167 | 	 * checkpoint to free some more log space. | 
 | 168 | 	 */ | 
 | 169 | 	spin_lock(&transaction->t_handle_lock); | 
 | 170 | 	needed = transaction->t_outstanding_credits + nblocks; | 
 | 171 |  | 
 | 172 | 	if (needed > journal->j_max_transaction_buffers) { | 
 | 173 | 		/* | 
 | 174 | 		 * If the current transaction is already too large, then start | 
 | 175 | 		 * to commit it: we can then go back and attach this handle to | 
 | 176 | 		 * a new transaction. | 
 | 177 | 		 */ | 
 | 178 | 		DEFINE_WAIT(wait); | 
 | 179 |  | 
 | 180 | 		jbd_debug(2, "Handle %p starting new commit...\n", handle); | 
 | 181 | 		spin_unlock(&transaction->t_handle_lock); | 
 | 182 | 		prepare_to_wait(&journal->j_wait_transaction_locked, &wait, | 
 | 183 | 				TASK_UNINTERRUPTIBLE); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 184 | 		__jbd2_log_start_commit(journal, transaction->t_tid); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 185 | 		spin_unlock(&journal->j_state_lock); | 
 | 186 | 		schedule(); | 
 | 187 | 		finish_wait(&journal->j_wait_transaction_locked, &wait); | 
 | 188 | 		goto repeat; | 
 | 189 | 	} | 
 | 190 |  | 
 | 191 | 	/* | 
 | 192 | 	 * The commit code assumes that it can get enough log space | 
 | 193 | 	 * without forcing a checkpoint.  This is *critical* for | 
 | 194 | 	 * correctness: a checkpoint of a buffer which is also | 
 | 195 | 	 * associated with a committing transaction creates a deadlock, | 
 | 196 | 	 * so commit simply cannot force through checkpoints. | 
 | 197 | 	 * | 
 | 198 | 	 * We must therefore ensure the necessary space in the journal | 
 | 199 | 	 * *before* starting to dirty potentially checkpointed buffers | 
 | 200 | 	 * in the new transaction. | 
 | 201 | 	 * | 
 | 202 | 	 * The worst part is, any transaction currently committing can | 
 | 203 | 	 * reduce the free space arbitrarily.  Be careful to account for | 
 | 204 | 	 * those buffers when checkpointing. | 
 | 205 | 	 */ | 
 | 206 |  | 
 | 207 | 	/* | 
 | 208 | 	 * @@@ AKPM: This seems rather over-defensive.  We're giving commit | 
 | 209 | 	 * a _lot_ of headroom: 1/4 of the journal plus the size of | 
 | 210 | 	 * the committing transaction.  Really, we only need to give it | 
 | 211 | 	 * committing_transaction->t_outstanding_credits plus "enough" for | 
 | 212 | 	 * the log control blocks. | 
 | 213 | 	 * Also, this test is inconsitent with the matching one in | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 214 | 	 * jbd2_journal_extend(). | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 215 | 	 */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 216 | 	if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) { | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 217 | 		jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle); | 
 | 218 | 		spin_unlock(&transaction->t_handle_lock); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 219 | 		__jbd2_log_wait_for_space(journal); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 220 | 		goto repeat_locked; | 
 | 221 | 	} | 
 | 222 |  | 
 | 223 | 	/* OK, account for the buffers that this operation expects to | 
 | 224 | 	 * use and add the handle to the running transaction. */ | 
 | 225 |  | 
| Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 226 | 	if (time_after(transaction->t_start, ts)) { | 
 | 227 | 		ts = jbd2_time_diff(ts, transaction->t_start); | 
 | 228 | 		if (ts > transaction->t_max_wait) | 
 | 229 | 			transaction->t_max_wait = ts; | 
 | 230 | 	} | 
 | 231 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 232 | 	handle->h_transaction = transaction; | 
 | 233 | 	transaction->t_outstanding_credits += nblocks; | 
 | 234 | 	transaction->t_updates++; | 
 | 235 | 	transaction->t_handle_count++; | 
 | 236 | 	jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n", | 
 | 237 | 		  handle, nblocks, transaction->t_outstanding_credits, | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 238 | 		  __jbd2_log_space_left(journal)); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 239 | 	spin_unlock(&transaction->t_handle_lock); | 
 | 240 | 	spin_unlock(&journal->j_state_lock); | 
 | 241 | out: | 
 | 242 | 	if (unlikely(new_transaction))		/* It's usually NULL */ | 
 | 243 | 		kfree(new_transaction); | 
 | 244 | 	return ret; | 
 | 245 | } | 
 | 246 |  | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 247 | static struct lock_class_key jbd2_handle_key; | 
 | 248 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 249 | /* Allocate a new handle.  This should probably be in a slab... */ | 
 | 250 | static handle_t *new_handle(int nblocks) | 
 | 251 | { | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 252 | 	handle_t *handle = jbd2_alloc_handle(GFP_NOFS); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 253 | 	if (!handle) | 
 | 254 | 		return NULL; | 
 | 255 | 	memset(handle, 0, sizeof(*handle)); | 
 | 256 | 	handle->h_buffer_credits = nblocks; | 
 | 257 | 	handle->h_ref = 1; | 
 | 258 |  | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 259 | 	lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle", | 
 | 260 | 						&jbd2_handle_key, 0); | 
 | 261 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 262 | 	return handle; | 
 | 263 | } | 
 | 264 |  | 
 | 265 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 266 |  * handle_t *jbd2_journal_start() - Obtain a new handle. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 267 |  * @journal: Journal to start transaction on. | 
 | 268 |  * @nblocks: number of block buffer we might modify | 
 | 269 |  * | 
 | 270 |  * We make sure that the transaction can guarantee at least nblocks of | 
 | 271 |  * modified buffers in the log.  We block until the log can guarantee | 
 | 272 |  * that much space. | 
 | 273 |  * | 
 | 274 |  * This function is visible to journal users (like ext3fs), so is not | 
 | 275 |  * called with the journal already locked. | 
 | 276 |  * | 
 | 277 |  * Return a pointer to a newly allocated handle, or NULL on failure | 
 | 278 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 279 | handle_t *jbd2_journal_start(journal_t *journal, int nblocks) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 280 | { | 
 | 281 | 	handle_t *handle = journal_current_handle(); | 
 | 282 | 	int err; | 
 | 283 |  | 
 | 284 | 	if (!journal) | 
 | 285 | 		return ERR_PTR(-EROFS); | 
 | 286 |  | 
 | 287 | 	if (handle) { | 
 | 288 | 		J_ASSERT(handle->h_transaction->t_journal == journal); | 
 | 289 | 		handle->h_ref++; | 
 | 290 | 		return handle; | 
 | 291 | 	} | 
 | 292 |  | 
 | 293 | 	handle = new_handle(nblocks); | 
 | 294 | 	if (!handle) | 
 | 295 | 		return ERR_PTR(-ENOMEM); | 
 | 296 |  | 
 | 297 | 	current->journal_info = handle; | 
 | 298 |  | 
 | 299 | 	err = start_this_handle(journal, handle); | 
 | 300 | 	if (err < 0) { | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 301 | 		jbd2_free_handle(handle); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 302 | 		current->journal_info = NULL; | 
 | 303 | 		handle = ERR_PTR(err); | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 304 | 		goto out; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 305 | 	} | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 306 |  | 
| Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 307 | 	lock_map_acquire(&handle->h_lockdep_map); | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 308 | out: | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 309 | 	return handle; | 
 | 310 | } | 
 | 311 |  | 
 | 312 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 313 |  * int jbd2_journal_extend() - extend buffer credits. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 314 |  * @handle:  handle to 'extend' | 
 | 315 |  * @nblocks: nr blocks to try to extend by. | 
 | 316 |  * | 
 | 317 |  * Some transactions, such as large extends and truncates, can be done | 
 | 318 |  * atomically all at once or in several stages.  The operation requests | 
 | 319 |  * a credit for a number of buffer modications in advance, but can | 
 | 320 |  * extend its credit if it needs more. | 
 | 321 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 322 |  * jbd2_journal_extend tries to give the running handle more buffer credits. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 323 |  * It does not guarantee that allocation - this is a best-effort only. | 
 | 324 |  * The calling process MUST be able to deal cleanly with a failure to | 
 | 325 |  * extend here. | 
 | 326 |  * | 
 | 327 |  * Return 0 on success, non-zero on failure. | 
 | 328 |  * | 
 | 329 |  * return code < 0 implies an error | 
 | 330 |  * return code > 0 implies normal transaction-full status. | 
 | 331 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 332 | int jbd2_journal_extend(handle_t *handle, int nblocks) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 333 | { | 
 | 334 | 	transaction_t *transaction = handle->h_transaction; | 
 | 335 | 	journal_t *journal = transaction->t_journal; | 
 | 336 | 	int result; | 
 | 337 | 	int wanted; | 
 | 338 |  | 
 | 339 | 	result = -EIO; | 
 | 340 | 	if (is_handle_aborted(handle)) | 
 | 341 | 		goto out; | 
 | 342 |  | 
 | 343 | 	result = 1; | 
 | 344 |  | 
 | 345 | 	spin_lock(&journal->j_state_lock); | 
 | 346 |  | 
 | 347 | 	/* Don't extend a locked-down transaction! */ | 
 | 348 | 	if (handle->h_transaction->t_state != T_RUNNING) { | 
 | 349 | 		jbd_debug(3, "denied handle %p %d blocks: " | 
 | 350 | 			  "transaction not running\n", handle, nblocks); | 
 | 351 | 		goto error_out; | 
 | 352 | 	} | 
 | 353 |  | 
 | 354 | 	spin_lock(&transaction->t_handle_lock); | 
 | 355 | 	wanted = transaction->t_outstanding_credits + nblocks; | 
 | 356 |  | 
 | 357 | 	if (wanted > journal->j_max_transaction_buffers) { | 
 | 358 | 		jbd_debug(3, "denied handle %p %d blocks: " | 
 | 359 | 			  "transaction too large\n", handle, nblocks); | 
 | 360 | 		goto unlock; | 
 | 361 | 	} | 
 | 362 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 363 | 	if (wanted > __jbd2_log_space_left(journal)) { | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 364 | 		jbd_debug(3, "denied handle %p %d blocks: " | 
 | 365 | 			  "insufficient log space\n", handle, nblocks); | 
 | 366 | 		goto unlock; | 
 | 367 | 	} | 
 | 368 |  | 
 | 369 | 	handle->h_buffer_credits += nblocks; | 
 | 370 | 	transaction->t_outstanding_credits += nblocks; | 
 | 371 | 	result = 0; | 
 | 372 |  | 
 | 373 | 	jbd_debug(3, "extended handle %p by %d\n", handle, nblocks); | 
 | 374 | unlock: | 
 | 375 | 	spin_unlock(&transaction->t_handle_lock); | 
 | 376 | error_out: | 
 | 377 | 	spin_unlock(&journal->j_state_lock); | 
 | 378 | out: | 
 | 379 | 	return result; | 
 | 380 | } | 
 | 381 |  | 
 | 382 |  | 
 | 383 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 384 |  * int jbd2_journal_restart() - restart a handle . | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 385 |  * @handle:  handle to restart | 
 | 386 |  * @nblocks: nr credits requested | 
 | 387 |  * | 
 | 388 |  * Restart a handle for a multi-transaction filesystem | 
 | 389 |  * operation. | 
 | 390 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 391 |  * If the jbd2_journal_extend() call above fails to grant new buffer credits | 
 | 392 |  * to a running handle, a call to jbd2_journal_restart will commit the | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 393 |  * handle's transaction so far and reattach the handle to a new | 
 | 394 |  * transaction capabable of guaranteeing the requested number of | 
 | 395 |  * credits. | 
 | 396 |  */ | 
 | 397 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 398 | int jbd2_journal_restart(handle_t *handle, int nblocks) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 399 | { | 
 | 400 | 	transaction_t *transaction = handle->h_transaction; | 
 | 401 | 	journal_t *journal = transaction->t_journal; | 
 | 402 | 	int ret; | 
 | 403 |  | 
 | 404 | 	/* If we've had an abort of any type, don't even think about | 
 | 405 | 	 * actually doing the restart! */ | 
 | 406 | 	if (is_handle_aborted(handle)) | 
 | 407 | 		return 0; | 
 | 408 |  | 
 | 409 | 	/* | 
 | 410 | 	 * First unlink the handle from its current transaction, and start the | 
 | 411 | 	 * commit on that. | 
 | 412 | 	 */ | 
 | 413 | 	J_ASSERT(transaction->t_updates > 0); | 
 | 414 | 	J_ASSERT(journal_current_handle() == handle); | 
 | 415 |  | 
 | 416 | 	spin_lock(&journal->j_state_lock); | 
 | 417 | 	spin_lock(&transaction->t_handle_lock); | 
 | 418 | 	transaction->t_outstanding_credits -= handle->h_buffer_credits; | 
 | 419 | 	transaction->t_updates--; | 
 | 420 |  | 
 | 421 | 	if (!transaction->t_updates) | 
 | 422 | 		wake_up(&journal->j_wait_updates); | 
 | 423 | 	spin_unlock(&transaction->t_handle_lock); | 
 | 424 |  | 
 | 425 | 	jbd_debug(2, "restarting handle %p\n", handle); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 426 | 	__jbd2_log_start_commit(journal, transaction->t_tid); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 427 | 	spin_unlock(&journal->j_state_lock); | 
 | 428 |  | 
 | 429 | 	handle->h_buffer_credits = nblocks; | 
 | 430 | 	ret = start_this_handle(journal, handle); | 
 | 431 | 	return ret; | 
 | 432 | } | 
 | 433 |  | 
 | 434 |  | 
 | 435 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 436 |  * void jbd2_journal_lock_updates () - establish a transaction barrier. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 437 |  * @journal:  Journal to establish a barrier on. | 
 | 438 |  * | 
 | 439 |  * This locks out any further updates from being started, and blocks | 
 | 440 |  * until all existing updates have completed, returning only once the | 
 | 441 |  * journal is in a quiescent state with no updates running. | 
 | 442 |  * | 
 | 443 |  * The journal lock should not be held on entry. | 
 | 444 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 445 | void jbd2_journal_lock_updates(journal_t *journal) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 446 | { | 
 | 447 | 	DEFINE_WAIT(wait); | 
 | 448 |  | 
 | 449 | 	spin_lock(&journal->j_state_lock); | 
 | 450 | 	++journal->j_barrier_count; | 
 | 451 |  | 
 | 452 | 	/* Wait until there are no running updates */ | 
 | 453 | 	while (1) { | 
 | 454 | 		transaction_t *transaction = journal->j_running_transaction; | 
 | 455 |  | 
 | 456 | 		if (!transaction) | 
 | 457 | 			break; | 
 | 458 |  | 
 | 459 | 		spin_lock(&transaction->t_handle_lock); | 
 | 460 | 		if (!transaction->t_updates) { | 
 | 461 | 			spin_unlock(&transaction->t_handle_lock); | 
 | 462 | 			break; | 
 | 463 | 		} | 
 | 464 | 		prepare_to_wait(&journal->j_wait_updates, &wait, | 
 | 465 | 				TASK_UNINTERRUPTIBLE); | 
 | 466 | 		spin_unlock(&transaction->t_handle_lock); | 
 | 467 | 		spin_unlock(&journal->j_state_lock); | 
 | 468 | 		schedule(); | 
 | 469 | 		finish_wait(&journal->j_wait_updates, &wait); | 
 | 470 | 		spin_lock(&journal->j_state_lock); | 
 | 471 | 	} | 
 | 472 | 	spin_unlock(&journal->j_state_lock); | 
 | 473 |  | 
 | 474 | 	/* | 
 | 475 | 	 * We have now established a barrier against other normal updates, but | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 476 | 	 * we also need to barrier against other jbd2_journal_lock_updates() calls | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 477 | 	 * to make sure that we serialise special journal-locked operations | 
 | 478 | 	 * too. | 
 | 479 | 	 */ | 
 | 480 | 	mutex_lock(&journal->j_barrier); | 
 | 481 | } | 
 | 482 |  | 
 | 483 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 484 |  * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 485 |  * @journal:  Journal to release the barrier on. | 
 | 486 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 487 |  * Release a transaction barrier obtained with jbd2_journal_lock_updates(). | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 488 |  * | 
 | 489 |  * Should be called without the journal lock held. | 
 | 490 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 491 | void jbd2_journal_unlock_updates (journal_t *journal) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 492 | { | 
 | 493 | 	J_ASSERT(journal->j_barrier_count != 0); | 
 | 494 |  | 
 | 495 | 	mutex_unlock(&journal->j_barrier); | 
 | 496 | 	spin_lock(&journal->j_state_lock); | 
 | 497 | 	--journal->j_barrier_count; | 
 | 498 | 	spin_unlock(&journal->j_state_lock); | 
 | 499 | 	wake_up(&journal->j_wait_transaction_locked); | 
 | 500 | } | 
 | 501 |  | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 502 | static void warn_dirty_buffer(struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 503 | { | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 504 | 	char b[BDEVNAME_SIZE]; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 505 |  | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 506 | 	printk(KERN_WARNING | 
 | 507 | 	       "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). " | 
 | 508 | 	       "There's a risk of filesystem corruption in case of system " | 
 | 509 | 	       "crash.\n", | 
 | 510 | 	       bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 511 | } | 
 | 512 |  | 
 | 513 | /* | 
 | 514 |  * If the buffer is already part of the current transaction, then there | 
 | 515 |  * is nothing we need to do.  If it is already part of a prior | 
 | 516 |  * transaction which we are still committing to disk, then we need to | 
 | 517 |  * make sure that we do not overwrite the old copy: we do copy-out to | 
 | 518 |  * preserve the copy going to disk.  We also account the buffer against | 
 | 519 |  * the handle's metadata buffer credits (unless the buffer is already | 
 | 520 |  * part of the transaction, that is). | 
 | 521 |  * | 
 | 522 |  */ | 
 | 523 | static int | 
 | 524 | do_get_write_access(handle_t *handle, struct journal_head *jh, | 
 | 525 | 			int force_copy) | 
 | 526 | { | 
 | 527 | 	struct buffer_head *bh; | 
 | 528 | 	transaction_t *transaction; | 
 | 529 | 	journal_t *journal; | 
 | 530 | 	int error; | 
 | 531 | 	char *frozen_buffer = NULL; | 
 | 532 | 	int need_copy = 0; | 
 | 533 |  | 
 | 534 | 	if (is_handle_aborted(handle)) | 
 | 535 | 		return -EROFS; | 
 | 536 |  | 
 | 537 | 	transaction = handle->h_transaction; | 
 | 538 | 	journal = transaction->t_journal; | 
 | 539 |  | 
 | 540 | 	jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy); | 
 | 541 |  | 
 | 542 | 	JBUFFER_TRACE(jh, "entry"); | 
 | 543 | repeat: | 
 | 544 | 	bh = jh2bh(jh); | 
 | 545 |  | 
 | 546 | 	/* @@@ Need to check for errors here at some point. */ | 
 | 547 |  | 
 | 548 | 	lock_buffer(bh); | 
 | 549 | 	jbd_lock_bh_state(bh); | 
 | 550 |  | 
 | 551 | 	/* We now hold the buffer lock so it is safe to query the buffer | 
 | 552 | 	 * state.  Is the buffer dirty? | 
 | 553 | 	 * | 
 | 554 | 	 * If so, there are two possibilities.  The buffer may be | 
 | 555 | 	 * non-journaled, and undergoing a quite legitimate writeback. | 
 | 556 | 	 * Otherwise, it is journaled, and we don't expect dirty buffers | 
 | 557 | 	 * in that state (the buffers should be marked JBD_Dirty | 
 | 558 | 	 * instead.)  So either the IO is being done under our own | 
 | 559 | 	 * control and this is a bug, or it's a third party IO such as | 
 | 560 | 	 * dump(8) (which may leave the buffer scheduled for read --- | 
 | 561 | 	 * ie. locked but not dirty) or tune2fs (which may actually have | 
 | 562 | 	 * the buffer dirtied, ugh.)  */ | 
 | 563 |  | 
 | 564 | 	if (buffer_dirty(bh)) { | 
 | 565 | 		/* | 
 | 566 | 		 * First question: is this buffer already part of the current | 
 | 567 | 		 * transaction or the existing committing transaction? | 
 | 568 | 		 */ | 
 | 569 | 		if (jh->b_transaction) { | 
 | 570 | 			J_ASSERT_JH(jh, | 
 | 571 | 				jh->b_transaction == transaction || | 
 | 572 | 				jh->b_transaction == | 
 | 573 | 					journal->j_committing_transaction); | 
 | 574 | 			if (jh->b_next_transaction) | 
 | 575 | 				J_ASSERT_JH(jh, jh->b_next_transaction == | 
 | 576 | 							transaction); | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 577 | 			warn_dirty_buffer(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 578 | 		} | 
 | 579 | 		/* | 
 | 580 | 		 * In any case we need to clean the dirty flag and we must | 
 | 581 | 		 * do it under the buffer lock to be sure we don't race | 
 | 582 | 		 * with running write-out. | 
 | 583 | 		 */ | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 584 | 		JBUFFER_TRACE(jh, "Journalling dirty buffer"); | 
 | 585 | 		clear_buffer_dirty(bh); | 
 | 586 | 		set_buffer_jbddirty(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 587 | 	} | 
 | 588 |  | 
 | 589 | 	unlock_buffer(bh); | 
 | 590 |  | 
 | 591 | 	error = -EROFS; | 
 | 592 | 	if (is_handle_aborted(handle)) { | 
 | 593 | 		jbd_unlock_bh_state(bh); | 
 | 594 | 		goto out; | 
 | 595 | 	} | 
 | 596 | 	error = 0; | 
 | 597 |  | 
 | 598 | 	/* | 
 | 599 | 	 * The buffer is already part of this transaction if b_transaction or | 
 | 600 | 	 * b_next_transaction points to it | 
 | 601 | 	 */ | 
 | 602 | 	if (jh->b_transaction == transaction || | 
 | 603 | 	    jh->b_next_transaction == transaction) | 
 | 604 | 		goto done; | 
 | 605 |  | 
 | 606 | 	/* | 
| Josef Bacik | 9fc7c63 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 607 | 	 * this is the first time this transaction is touching this buffer, | 
 | 608 | 	 * reset the modified flag | 
 | 609 | 	 */ | 
 | 610 |        jh->b_modified = 0; | 
 | 611 |  | 
 | 612 | 	/* | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 613 | 	 * If there is already a copy-out version of this buffer, then we don't | 
 | 614 | 	 * need to make another one | 
 | 615 | 	 */ | 
 | 616 | 	if (jh->b_frozen_data) { | 
 | 617 | 		JBUFFER_TRACE(jh, "has frozen data"); | 
 | 618 | 		J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | 
 | 619 | 		jh->b_next_transaction = transaction; | 
 | 620 | 		goto done; | 
 | 621 | 	} | 
 | 622 |  | 
 | 623 | 	/* Is there data here we need to preserve? */ | 
 | 624 |  | 
 | 625 | 	if (jh->b_transaction && jh->b_transaction != transaction) { | 
 | 626 | 		JBUFFER_TRACE(jh, "owned by older transaction"); | 
 | 627 | 		J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | 
 | 628 | 		J_ASSERT_JH(jh, jh->b_transaction == | 
 | 629 | 					journal->j_committing_transaction); | 
 | 630 |  | 
 | 631 | 		/* There is one case we have to be very careful about. | 
 | 632 | 		 * If the committing transaction is currently writing | 
 | 633 | 		 * this buffer out to disk and has NOT made a copy-out, | 
 | 634 | 		 * then we cannot modify the buffer contents at all | 
 | 635 | 		 * right now.  The essence of copy-out is that it is the | 
 | 636 | 		 * extra copy, not the primary copy, which gets | 
 | 637 | 		 * journaled.  If the primary copy is already going to | 
 | 638 | 		 * disk then we cannot do copy-out here. */ | 
 | 639 |  | 
 | 640 | 		if (jh->b_jlist == BJ_Shadow) { | 
 | 641 | 			DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow); | 
 | 642 | 			wait_queue_head_t *wqh; | 
 | 643 |  | 
 | 644 | 			wqh = bit_waitqueue(&bh->b_state, BH_Unshadow); | 
 | 645 |  | 
 | 646 | 			JBUFFER_TRACE(jh, "on shadow: sleep"); | 
 | 647 | 			jbd_unlock_bh_state(bh); | 
 | 648 | 			/* commit wakes up all shadow buffers after IO */ | 
 | 649 | 			for ( ; ; ) { | 
 | 650 | 				prepare_to_wait(wqh, &wait.wait, | 
 | 651 | 						TASK_UNINTERRUPTIBLE); | 
 | 652 | 				if (jh->b_jlist != BJ_Shadow) | 
 | 653 | 					break; | 
 | 654 | 				schedule(); | 
 | 655 | 			} | 
 | 656 | 			finish_wait(wqh, &wait.wait); | 
 | 657 | 			goto repeat; | 
 | 658 | 		} | 
 | 659 |  | 
 | 660 | 		/* Only do the copy if the currently-owning transaction | 
 | 661 | 		 * still needs it.  If it is on the Forget list, the | 
 | 662 | 		 * committing transaction is past that stage.  The | 
 | 663 | 		 * buffer had better remain locked during the kmalloc, | 
 | 664 | 		 * but that should be true --- we hold the journal lock | 
 | 665 | 		 * still and the buffer is already on the BUF_JOURNAL | 
 | 666 | 		 * list so won't be flushed. | 
 | 667 | 		 * | 
 | 668 | 		 * Subtle point, though: if this is a get_undo_access, | 
 | 669 | 		 * then we will be relying on the frozen_data to contain | 
 | 670 | 		 * the new value of the committed_data record after the | 
 | 671 | 		 * transaction, so we HAVE to force the frozen_data copy | 
 | 672 | 		 * in that case. */ | 
 | 673 |  | 
 | 674 | 		if (jh->b_jlist != BJ_Forget || force_copy) { | 
 | 675 | 			JBUFFER_TRACE(jh, "generate frozen data"); | 
 | 676 | 			if (!frozen_buffer) { | 
 | 677 | 				JBUFFER_TRACE(jh, "allocate memory for buffer"); | 
 | 678 | 				jbd_unlock_bh_state(bh); | 
 | 679 | 				frozen_buffer = | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 680 | 					jbd2_alloc(jh2bh(jh)->b_size, | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 681 | 							 GFP_NOFS); | 
 | 682 | 				if (!frozen_buffer) { | 
 | 683 | 					printk(KERN_EMERG | 
 | 684 | 					       "%s: OOM for frozen_buffer\n", | 
| Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 685 | 					       __func__); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 686 | 					JBUFFER_TRACE(jh, "oom!"); | 
 | 687 | 					error = -ENOMEM; | 
 | 688 | 					jbd_lock_bh_state(bh); | 
 | 689 | 					goto done; | 
 | 690 | 				} | 
 | 691 | 				goto repeat; | 
 | 692 | 			} | 
 | 693 | 			jh->b_frozen_data = frozen_buffer; | 
 | 694 | 			frozen_buffer = NULL; | 
 | 695 | 			need_copy = 1; | 
 | 696 | 		} | 
 | 697 | 		jh->b_next_transaction = transaction; | 
 | 698 | 	} | 
 | 699 |  | 
 | 700 |  | 
 | 701 | 	/* | 
 | 702 | 	 * Finally, if the buffer is not journaled right now, we need to make | 
 | 703 | 	 * sure it doesn't get written to disk before the caller actually | 
 | 704 | 	 * commits the new data | 
 | 705 | 	 */ | 
 | 706 | 	if (!jh->b_transaction) { | 
 | 707 | 		JBUFFER_TRACE(jh, "no transaction"); | 
 | 708 | 		J_ASSERT_JH(jh, !jh->b_next_transaction); | 
 | 709 | 		jh->b_transaction = transaction; | 
 | 710 | 		JBUFFER_TRACE(jh, "file as BJ_Reserved"); | 
 | 711 | 		spin_lock(&journal->j_list_lock); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 712 | 		__jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 713 | 		spin_unlock(&journal->j_list_lock); | 
 | 714 | 	} | 
 | 715 |  | 
 | 716 | done: | 
 | 717 | 	if (need_copy) { | 
 | 718 | 		struct page *page; | 
 | 719 | 		int offset; | 
 | 720 | 		char *source; | 
 | 721 |  | 
 | 722 | 		J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)), | 
 | 723 | 			    "Possible IO failure.\n"); | 
 | 724 | 		page = jh2bh(jh)->b_page; | 
 | 725 | 		offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK; | 
 | 726 | 		source = kmap_atomic(page, KM_USER0); | 
 | 727 | 		memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size); | 
 | 728 | 		kunmap_atomic(source, KM_USER0); | 
| Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 729 |  | 
 | 730 | 		/* | 
 | 731 | 		 * Now that the frozen data is saved off, we need to store | 
 | 732 | 		 * any matching triggers. | 
 | 733 | 		 */ | 
 | 734 | 		jh->b_frozen_triggers = jh->b_triggers; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 735 | 	} | 
 | 736 | 	jbd_unlock_bh_state(bh); | 
 | 737 |  | 
 | 738 | 	/* | 
 | 739 | 	 * If we are about to journal a buffer, then any revoke pending on it is | 
 | 740 | 	 * no longer valid | 
 | 741 | 	 */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 742 | 	jbd2_journal_cancel_revoke(handle, jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 743 |  | 
 | 744 | out: | 
 | 745 | 	if (unlikely(frozen_buffer))	/* It's usually NULL */ | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 746 | 		jbd2_free(frozen_buffer, bh->b_size); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 747 |  | 
 | 748 | 	JBUFFER_TRACE(jh, "exit"); | 
 | 749 | 	return error; | 
 | 750 | } | 
 | 751 |  | 
 | 752 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 753 |  * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 754 |  * @handle: transaction to add buffer modifications to | 
 | 755 |  * @bh:     bh to be used for metadata writes | 
 | 756 |  * @credits: variable that will receive credits for the buffer | 
 | 757 |  * | 
 | 758 |  * Returns an error code or 0 on success. | 
 | 759 |  * | 
 | 760 |  * In full data journalling mode the buffer may be of type BJ_AsyncData, | 
 | 761 |  * because we're write()ing a buffer which is also part of a shared mapping. | 
 | 762 |  */ | 
 | 763 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 764 | int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 765 | { | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 766 | 	struct journal_head *jh = jbd2_journal_add_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 767 | 	int rc; | 
 | 768 |  | 
 | 769 | 	/* We do not want to get caught playing with fields which the | 
 | 770 | 	 * log thread also manipulates.  Make sure that the buffer | 
 | 771 | 	 * completes any outstanding IO before proceeding. */ | 
 | 772 | 	rc = do_get_write_access(handle, jh, 0); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 773 | 	jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 774 | 	return rc; | 
 | 775 | } | 
 | 776 |  | 
 | 777 |  | 
 | 778 | /* | 
 | 779 |  * When the user wants to journal a newly created buffer_head | 
 | 780 |  * (ie. getblk() returned a new buffer and we are going to populate it | 
 | 781 |  * manually rather than reading off disk), then we need to keep the | 
 | 782 |  * buffer_head locked until it has been completely filled with new | 
 | 783 |  * data.  In this case, we should be able to make the assertion that | 
 | 784 |  * the bh is not already part of an existing transaction. | 
 | 785 |  * | 
 | 786 |  * The buffer should already be locked by the caller by this point. | 
 | 787 |  * There is no lock ranking violation: it was a newly created, | 
 | 788 |  * unlocked buffer beforehand. */ | 
 | 789 |  | 
 | 790 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 791 |  * int jbd2_journal_get_create_access () - notify intent to use newly created bh | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 792 |  * @handle: transaction to new buffer to | 
 | 793 |  * @bh: new buffer. | 
 | 794 |  * | 
 | 795 |  * Call this if you create a new bh. | 
 | 796 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 797 | int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 798 | { | 
 | 799 | 	transaction_t *transaction = handle->h_transaction; | 
 | 800 | 	journal_t *journal = transaction->t_journal; | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 801 | 	struct journal_head *jh = jbd2_journal_add_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 802 | 	int err; | 
 | 803 |  | 
 | 804 | 	jbd_debug(5, "journal_head %p\n", jh); | 
 | 805 | 	err = -EROFS; | 
 | 806 | 	if (is_handle_aborted(handle)) | 
 | 807 | 		goto out; | 
 | 808 | 	err = 0; | 
 | 809 |  | 
 | 810 | 	JBUFFER_TRACE(jh, "entry"); | 
 | 811 | 	/* | 
 | 812 | 	 * The buffer may already belong to this transaction due to pre-zeroing | 
 | 813 | 	 * in the filesystem's new_block code.  It may also be on the previous, | 
 | 814 | 	 * committing transaction's lists, but it HAS to be in Forget state in | 
 | 815 | 	 * that case: the transaction must have deleted the buffer for it to be | 
 | 816 | 	 * reused here. | 
 | 817 | 	 */ | 
 | 818 | 	jbd_lock_bh_state(bh); | 
 | 819 | 	spin_lock(&journal->j_list_lock); | 
 | 820 | 	J_ASSERT_JH(jh, (jh->b_transaction == transaction || | 
 | 821 | 		jh->b_transaction == NULL || | 
 | 822 | 		(jh->b_transaction == journal->j_committing_transaction && | 
 | 823 | 			  jh->b_jlist == BJ_Forget))); | 
 | 824 |  | 
 | 825 | 	J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | 
 | 826 | 	J_ASSERT_JH(jh, buffer_locked(jh2bh(jh))); | 
 | 827 |  | 
 | 828 | 	if (jh->b_transaction == NULL) { | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 829 | 		/* | 
 | 830 | 		 * Previous jbd2_journal_forget() could have left the buffer | 
 | 831 | 		 * with jbddirty bit set because it was being committed. When | 
 | 832 | 		 * the commit finished, we've filed the buffer for | 
 | 833 | 		 * checkpointing and marked it dirty. Now we are reallocating | 
 | 834 | 		 * the buffer so the transaction freeing it must have | 
 | 835 | 		 * committed and so it's safe to clear the dirty bit. | 
 | 836 | 		 */ | 
 | 837 | 		clear_buffer_dirty(jh2bh(jh)); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 838 | 		jh->b_transaction = transaction; | 
| Josef Bacik | 9fc7c63 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 839 |  | 
 | 840 | 		/* first access by this transaction */ | 
 | 841 | 		jh->b_modified = 0; | 
 | 842 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 843 | 		JBUFFER_TRACE(jh, "file as BJ_Reserved"); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 844 | 		__jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 845 | 	} else if (jh->b_transaction == journal->j_committing_transaction) { | 
| Josef Bacik | 9fc7c63 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 846 | 		/* first access by this transaction */ | 
 | 847 | 		jh->b_modified = 0; | 
 | 848 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 849 | 		JBUFFER_TRACE(jh, "set next transaction"); | 
 | 850 | 		jh->b_next_transaction = transaction; | 
 | 851 | 	} | 
 | 852 | 	spin_unlock(&journal->j_list_lock); | 
 | 853 | 	jbd_unlock_bh_state(bh); | 
 | 854 |  | 
 | 855 | 	/* | 
 | 856 | 	 * akpm: I added this.  ext3_alloc_branch can pick up new indirect | 
 | 857 | 	 * blocks which contain freed but then revoked metadata.  We need | 
 | 858 | 	 * to cancel the revoke in case we end up freeing it yet again | 
 | 859 | 	 * and the reallocating as data - this would cause a second revoke, | 
 | 860 | 	 * which hits an assertion error. | 
 | 861 | 	 */ | 
 | 862 | 	JBUFFER_TRACE(jh, "cancelling revoke"); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 863 | 	jbd2_journal_cancel_revoke(handle, jh); | 
 | 864 | 	jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 865 | out: | 
 | 866 | 	return err; | 
 | 867 | } | 
 | 868 |  | 
 | 869 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 870 |  * int jbd2_journal_get_undo_access() -  Notify intent to modify metadata with | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 871 |  *     non-rewindable consequences | 
 | 872 |  * @handle: transaction | 
 | 873 |  * @bh: buffer to undo | 
 | 874 |  * @credits: store the number of taken credits here (if not NULL) | 
 | 875 |  * | 
 | 876 |  * Sometimes there is a need to distinguish between metadata which has | 
 | 877 |  * been committed to disk and that which has not.  The ext3fs code uses | 
 | 878 |  * this for freeing and allocating space, we have to make sure that we | 
 | 879 |  * do not reuse freed space until the deallocation has been committed, | 
 | 880 |  * since if we overwrote that space we would make the delete | 
 | 881 |  * un-rewindable in case of a crash. | 
 | 882 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 883 |  * To deal with that, jbd2_journal_get_undo_access requests write access to a | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 884 |  * buffer for parts of non-rewindable operations such as delete | 
 | 885 |  * operations on the bitmaps.  The journaling code must keep a copy of | 
 | 886 |  * the buffer's contents prior to the undo_access call until such time | 
 | 887 |  * as we know that the buffer has definitely been committed to disk. | 
 | 888 |  * | 
 | 889 |  * We never need to know which transaction the committed data is part | 
 | 890 |  * of, buffers touched here are guaranteed to be dirtied later and so | 
 | 891 |  * will be committed to a new transaction in due course, at which point | 
 | 892 |  * we can discard the old committed data pointer. | 
 | 893 |  * | 
 | 894 |  * Returns error number or 0 on success. | 
 | 895 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 896 | int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 897 | { | 
 | 898 | 	int err; | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 899 | 	struct journal_head *jh = jbd2_journal_add_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 900 | 	char *committed_data = NULL; | 
 | 901 |  | 
 | 902 | 	JBUFFER_TRACE(jh, "entry"); | 
 | 903 |  | 
 | 904 | 	/* | 
 | 905 | 	 * Do this first --- it can drop the journal lock, so we want to | 
 | 906 | 	 * make sure that obtaining the committed_data is done | 
 | 907 | 	 * atomically wrt. completion of any outstanding commits. | 
 | 908 | 	 */ | 
 | 909 | 	err = do_get_write_access(handle, jh, 1); | 
 | 910 | 	if (err) | 
 | 911 | 		goto out; | 
 | 912 |  | 
 | 913 | repeat: | 
 | 914 | 	if (!jh->b_committed_data) { | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 915 | 		committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 916 | 		if (!committed_data) { | 
 | 917 | 			printk(KERN_EMERG "%s: No memory for committed data\n", | 
| Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 918 | 				__func__); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 919 | 			err = -ENOMEM; | 
 | 920 | 			goto out; | 
 | 921 | 		} | 
 | 922 | 	} | 
 | 923 |  | 
 | 924 | 	jbd_lock_bh_state(bh); | 
 | 925 | 	if (!jh->b_committed_data) { | 
 | 926 | 		/* Copy out the current buffer contents into the | 
 | 927 | 		 * preserved, committed copy. */ | 
 | 928 | 		JBUFFER_TRACE(jh, "generate b_committed data"); | 
 | 929 | 		if (!committed_data) { | 
 | 930 | 			jbd_unlock_bh_state(bh); | 
 | 931 | 			goto repeat; | 
 | 932 | 		} | 
 | 933 |  | 
 | 934 | 		jh->b_committed_data = committed_data; | 
 | 935 | 		committed_data = NULL; | 
 | 936 | 		memcpy(jh->b_committed_data, bh->b_data, bh->b_size); | 
 | 937 | 	} | 
 | 938 | 	jbd_unlock_bh_state(bh); | 
 | 939 | out: | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 940 | 	jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 941 | 	if (unlikely(committed_data)) | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 942 | 		jbd2_free(committed_data, bh->b_size); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 943 | 	return err; | 
 | 944 | } | 
 | 945 |  | 
 | 946 | /** | 
| Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 947 |  * void jbd2_journal_set_triggers() - Add triggers for commit writeout | 
 | 948 |  * @bh: buffer to trigger on | 
 | 949 |  * @type: struct jbd2_buffer_trigger_type containing the trigger(s). | 
 | 950 |  * | 
 | 951 |  * Set any triggers on this journal_head.  This is always safe, because | 
 | 952 |  * triggers for a committing buffer will be saved off, and triggers for | 
 | 953 |  * a running transaction will match the buffer in that transaction. | 
 | 954 |  * | 
 | 955 |  * Call with NULL to clear the triggers. | 
 | 956 |  */ | 
 | 957 | void jbd2_journal_set_triggers(struct buffer_head *bh, | 
 | 958 | 			       struct jbd2_buffer_trigger_type *type) | 
 | 959 | { | 
 | 960 | 	struct journal_head *jh = bh2jh(bh); | 
 | 961 |  | 
 | 962 | 	jh->b_triggers = type; | 
 | 963 | } | 
 | 964 |  | 
 | 965 | void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data, | 
 | 966 | 				struct jbd2_buffer_trigger_type *triggers) | 
 | 967 | { | 
 | 968 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 969 |  | 
 | 970 | 	if (!triggers || !triggers->t_commit) | 
 | 971 | 		return; | 
 | 972 |  | 
 | 973 | 	triggers->t_commit(triggers, bh, mapped_data, bh->b_size); | 
 | 974 | } | 
 | 975 |  | 
 | 976 | void jbd2_buffer_abort_trigger(struct journal_head *jh, | 
 | 977 | 			       struct jbd2_buffer_trigger_type *triggers) | 
 | 978 | { | 
 | 979 | 	if (!triggers || !triggers->t_abort) | 
 | 980 | 		return; | 
 | 981 |  | 
 | 982 | 	triggers->t_abort(triggers, jh2bh(jh)); | 
 | 983 | } | 
 | 984 |  | 
 | 985 |  | 
 | 986 |  | 
 | 987 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 988 |  * int jbd2_journal_dirty_metadata() -  mark a buffer as containing dirty metadata | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 989 |  * @handle: transaction to add buffer to. | 
 | 990 |  * @bh: buffer to mark | 
 | 991 |  * | 
 | 992 |  * mark dirty metadata which needs to be journaled as part of the current | 
 | 993 |  * transaction. | 
 | 994 |  * | 
 | 995 |  * The buffer is placed on the transaction's metadata list and is marked | 
 | 996 |  * as belonging to the transaction. | 
 | 997 |  * | 
 | 998 |  * Returns error number or 0 on success. | 
 | 999 |  * | 
 | 1000 |  * Special care needs to be taken if the buffer already belongs to the | 
 | 1001 |  * current committing transaction (in which case we should have frozen | 
 | 1002 |  * data present for that commit).  In that case, we don't relink the | 
 | 1003 |  * buffer: that only gets done when the old transaction finally | 
 | 1004 |  * completes its commit. | 
 | 1005 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1006 | int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1007 | { | 
 | 1008 | 	transaction_t *transaction = handle->h_transaction; | 
 | 1009 | 	journal_t *journal = transaction->t_journal; | 
 | 1010 | 	struct journal_head *jh = bh2jh(bh); | 
 | 1011 |  | 
 | 1012 | 	jbd_debug(5, "journal_head %p\n", jh); | 
 | 1013 | 	JBUFFER_TRACE(jh, "entry"); | 
 | 1014 | 	if (is_handle_aborted(handle)) | 
 | 1015 | 		goto out; | 
 | 1016 |  | 
 | 1017 | 	jbd_lock_bh_state(bh); | 
 | 1018 |  | 
 | 1019 | 	if (jh->b_modified == 0) { | 
 | 1020 | 		/* | 
 | 1021 | 		 * This buffer's got modified and becoming part | 
 | 1022 | 		 * of the transaction. This needs to be done | 
 | 1023 | 		 * once a transaction -bzzz | 
 | 1024 | 		 */ | 
 | 1025 | 		jh->b_modified = 1; | 
 | 1026 | 		J_ASSERT_JH(jh, handle->h_buffer_credits > 0); | 
 | 1027 | 		handle->h_buffer_credits--; | 
 | 1028 | 	} | 
 | 1029 |  | 
 | 1030 | 	/* | 
 | 1031 | 	 * fastpath, to avoid expensive locking.  If this buffer is already | 
 | 1032 | 	 * on the running transaction's metadata list there is nothing to do. | 
 | 1033 | 	 * Nobody can take it off again because there is a handle open. | 
 | 1034 | 	 * I _think_ we're OK here with SMP barriers - a mistaken decision will | 
 | 1035 | 	 * result in this test being false, so we go in and take the locks. | 
 | 1036 | 	 */ | 
 | 1037 | 	if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) { | 
 | 1038 | 		JBUFFER_TRACE(jh, "fastpath"); | 
 | 1039 | 		J_ASSERT_JH(jh, jh->b_transaction == | 
 | 1040 | 					journal->j_running_transaction); | 
 | 1041 | 		goto out_unlock_bh; | 
 | 1042 | 	} | 
 | 1043 |  | 
 | 1044 | 	set_buffer_jbddirty(bh); | 
 | 1045 |  | 
 | 1046 | 	/* | 
 | 1047 | 	 * Metadata already on the current transaction list doesn't | 
 | 1048 | 	 * need to be filed.  Metadata on another transaction's list must | 
 | 1049 | 	 * be committing, and will be refiled once the commit completes: | 
 | 1050 | 	 * leave it alone for now. | 
 | 1051 | 	 */ | 
 | 1052 | 	if (jh->b_transaction != transaction) { | 
 | 1053 | 		JBUFFER_TRACE(jh, "already on other transaction"); | 
 | 1054 | 		J_ASSERT_JH(jh, jh->b_transaction == | 
 | 1055 | 					journal->j_committing_transaction); | 
 | 1056 | 		J_ASSERT_JH(jh, jh->b_next_transaction == transaction); | 
 | 1057 | 		/* And this case is illegal: we can't reuse another | 
 | 1058 | 		 * transaction's data buffer, ever. */ | 
 | 1059 | 		goto out_unlock_bh; | 
 | 1060 | 	} | 
 | 1061 |  | 
 | 1062 | 	/* That test should have eliminated the following case: */ | 
| Mingming Cao | 4019191 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1063 | 	J_ASSERT_JH(jh, jh->b_frozen_data == NULL); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1064 |  | 
 | 1065 | 	JBUFFER_TRACE(jh, "file as BJ_Metadata"); | 
 | 1066 | 	spin_lock(&journal->j_list_lock); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1067 | 	__jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1068 | 	spin_unlock(&journal->j_list_lock); | 
 | 1069 | out_unlock_bh: | 
 | 1070 | 	jbd_unlock_bh_state(bh); | 
 | 1071 | out: | 
 | 1072 | 	JBUFFER_TRACE(jh, "exit"); | 
 | 1073 | 	return 0; | 
 | 1074 | } | 
 | 1075 |  | 
 | 1076 | /* | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1077 |  * jbd2_journal_release_buffer: undo a get_write_access without any buffer | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1078 |  * updates, if the update decided in the end that it didn't need access. | 
 | 1079 |  * | 
 | 1080 |  */ | 
 | 1081 | void | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1082 | jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1083 | { | 
 | 1084 | 	BUFFER_TRACE(bh, "entry"); | 
 | 1085 | } | 
 | 1086 |  | 
 | 1087 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1088 |  * void jbd2_journal_forget() - bforget() for potentially-journaled buffers. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1089 |  * @handle: transaction handle | 
 | 1090 |  * @bh:     bh to 'forget' | 
 | 1091 |  * | 
 | 1092 |  * We can only do the bforget if there are no commits pending against the | 
 | 1093 |  * buffer.  If the buffer is dirty in the current running transaction we | 
 | 1094 |  * can safely unlink it. | 
 | 1095 |  * | 
 | 1096 |  * bh may not be a journalled buffer at all - it may be a non-JBD | 
 | 1097 |  * buffer which came off the hashtable.  Check for this. | 
 | 1098 |  * | 
 | 1099 |  * Decrements bh->b_count by one. | 
 | 1100 |  * | 
 | 1101 |  * Allow this call even if the handle has aborted --- it may be part of | 
 | 1102 |  * the caller's cleanup after an abort. | 
 | 1103 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1104 | int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1105 | { | 
 | 1106 | 	transaction_t *transaction = handle->h_transaction; | 
 | 1107 | 	journal_t *journal = transaction->t_journal; | 
 | 1108 | 	struct journal_head *jh; | 
 | 1109 | 	int drop_reserve = 0; | 
 | 1110 | 	int err = 0; | 
| Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1111 | 	int was_modified = 0; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1112 |  | 
 | 1113 | 	BUFFER_TRACE(bh, "entry"); | 
 | 1114 |  | 
 | 1115 | 	jbd_lock_bh_state(bh); | 
 | 1116 | 	spin_lock(&journal->j_list_lock); | 
 | 1117 |  | 
 | 1118 | 	if (!buffer_jbd(bh)) | 
 | 1119 | 		goto not_jbd; | 
 | 1120 | 	jh = bh2jh(bh); | 
 | 1121 |  | 
 | 1122 | 	/* Critical error: attempting to delete a bitmap buffer, maybe? | 
 | 1123 | 	 * Don't do any jbd operations, and return an error. */ | 
 | 1124 | 	if (!J_EXPECT_JH(jh, !jh->b_committed_data, | 
 | 1125 | 			 "inconsistent data on disk")) { | 
 | 1126 | 		err = -EIO; | 
 | 1127 | 		goto not_jbd; | 
 | 1128 | 	} | 
 | 1129 |  | 
| Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1130 | 	/* keep track of wether or not this transaction modified us */ | 
 | 1131 | 	was_modified = jh->b_modified; | 
 | 1132 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1133 | 	/* | 
 | 1134 | 	 * The buffer's going from the transaction, we must drop | 
 | 1135 | 	 * all references -bzzz | 
 | 1136 | 	 */ | 
 | 1137 | 	jh->b_modified = 0; | 
 | 1138 |  | 
 | 1139 | 	if (jh->b_transaction == handle->h_transaction) { | 
 | 1140 | 		J_ASSERT_JH(jh, !jh->b_frozen_data); | 
 | 1141 |  | 
 | 1142 | 		/* If we are forgetting a buffer which is already part | 
 | 1143 | 		 * of this transaction, then we can just drop it from | 
 | 1144 | 		 * the transaction immediately. */ | 
 | 1145 | 		clear_buffer_dirty(bh); | 
 | 1146 | 		clear_buffer_jbddirty(bh); | 
 | 1147 |  | 
 | 1148 | 		JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); | 
 | 1149 |  | 
| Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1150 | 		/* | 
 | 1151 | 		 * we only want to drop a reference if this transaction | 
 | 1152 | 		 * modified the buffer | 
 | 1153 | 		 */ | 
 | 1154 | 		if (was_modified) | 
 | 1155 | 			drop_reserve = 1; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1156 |  | 
 | 1157 | 		/* | 
 | 1158 | 		 * We are no longer going to journal this buffer. | 
 | 1159 | 		 * However, the commit of this transaction is still | 
 | 1160 | 		 * important to the buffer: the delete that we are now | 
 | 1161 | 		 * processing might obsolete an old log entry, so by | 
 | 1162 | 		 * committing, we can satisfy the buffer's checkpoint. | 
 | 1163 | 		 * | 
 | 1164 | 		 * So, if we have a checkpoint on the buffer, we should | 
 | 1165 | 		 * now refile the buffer on our BJ_Forget list so that | 
 | 1166 | 		 * we know to remove the checkpoint after we commit. | 
 | 1167 | 		 */ | 
 | 1168 |  | 
 | 1169 | 		if (jh->b_cp_transaction) { | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1170 | 			__jbd2_journal_temp_unlink_buffer(jh); | 
 | 1171 | 			__jbd2_journal_file_buffer(jh, transaction, BJ_Forget); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1172 | 		} else { | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1173 | 			__jbd2_journal_unfile_buffer(jh); | 
 | 1174 | 			jbd2_journal_remove_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1175 | 			__brelse(bh); | 
 | 1176 | 			if (!buffer_jbd(bh)) { | 
 | 1177 | 				spin_unlock(&journal->j_list_lock); | 
 | 1178 | 				jbd_unlock_bh_state(bh); | 
 | 1179 | 				__bforget(bh); | 
 | 1180 | 				goto drop; | 
 | 1181 | 			} | 
 | 1182 | 		} | 
 | 1183 | 	} else if (jh->b_transaction) { | 
 | 1184 | 		J_ASSERT_JH(jh, (jh->b_transaction == | 
 | 1185 | 				 journal->j_committing_transaction)); | 
 | 1186 | 		/* However, if the buffer is still owned by a prior | 
 | 1187 | 		 * (committing) transaction, we can't drop it yet... */ | 
 | 1188 | 		JBUFFER_TRACE(jh, "belongs to older transaction"); | 
 | 1189 | 		/* ... but we CAN drop it from the new transaction if we | 
 | 1190 | 		 * have also modified it since the original commit. */ | 
 | 1191 |  | 
 | 1192 | 		if (jh->b_next_transaction) { | 
 | 1193 | 			J_ASSERT(jh->b_next_transaction == transaction); | 
 | 1194 | 			jh->b_next_transaction = NULL; | 
| Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1195 |  | 
 | 1196 | 			/* | 
 | 1197 | 			 * only drop a reference if this transaction modified | 
 | 1198 | 			 * the buffer | 
 | 1199 | 			 */ | 
 | 1200 | 			if (was_modified) | 
 | 1201 | 				drop_reserve = 1; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1202 | 		} | 
 | 1203 | 	} | 
 | 1204 |  | 
 | 1205 | not_jbd: | 
 | 1206 | 	spin_unlock(&journal->j_list_lock); | 
 | 1207 | 	jbd_unlock_bh_state(bh); | 
 | 1208 | 	__brelse(bh); | 
 | 1209 | drop: | 
 | 1210 | 	if (drop_reserve) { | 
 | 1211 | 		/* no need to reserve log space for this block -bzzz */ | 
 | 1212 | 		handle->h_buffer_credits++; | 
 | 1213 | 	} | 
 | 1214 | 	return err; | 
 | 1215 | } | 
 | 1216 |  | 
 | 1217 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1218 |  * int jbd2_journal_stop() - complete a transaction | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1219 |  * @handle: tranaction to complete. | 
 | 1220 |  * | 
 | 1221 |  * All done for a particular handle. | 
 | 1222 |  * | 
 | 1223 |  * There is not much action needed here.  We just return any remaining | 
 | 1224 |  * buffer credits to the transaction and remove the handle.  The only | 
 | 1225 |  * complication is that we need to start a commit operation if the | 
 | 1226 |  * filesystem is marked for synchronous update. | 
 | 1227 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1228 |  * jbd2_journal_stop itself will not usually return an error, but it may | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1229 |  * do so in unusual circumstances.  In particular, expect it to | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1230 |  * return -EIO if a jbd2_journal_abort has been executed since the | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1231 |  * transaction began. | 
 | 1232 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1233 | int jbd2_journal_stop(handle_t *handle) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1234 | { | 
 | 1235 | 	transaction_t *transaction = handle->h_transaction; | 
 | 1236 | 	journal_t *journal = transaction->t_journal; | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1237 | 	int err; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1238 | 	pid_t pid; | 
 | 1239 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1240 | 	J_ASSERT(journal_current_handle() == handle); | 
 | 1241 |  | 
 | 1242 | 	if (is_handle_aborted(handle)) | 
 | 1243 | 		err = -EIO; | 
| OGAWA Hirofumi | 3e2a532 | 2006-10-19 23:29:11 -0700 | [diff] [blame] | 1244 | 	else { | 
 | 1245 | 		J_ASSERT(transaction->t_updates > 0); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1246 | 		err = 0; | 
| OGAWA Hirofumi | 3e2a532 | 2006-10-19 23:29:11 -0700 | [diff] [blame] | 1247 | 	} | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1248 |  | 
 | 1249 | 	if (--handle->h_ref > 0) { | 
 | 1250 | 		jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, | 
 | 1251 | 			  handle->h_ref); | 
 | 1252 | 		return err; | 
 | 1253 | 	} | 
 | 1254 |  | 
 | 1255 | 	jbd_debug(4, "Handle %p going down\n", handle); | 
 | 1256 |  | 
 | 1257 | 	/* | 
 | 1258 | 	 * Implement synchronous transaction batching.  If the handle | 
 | 1259 | 	 * was synchronous, don't force a commit immediately.  Let's | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1260 | 	 * yield and let another thread piggyback onto this | 
 | 1261 | 	 * transaction.  Keep doing that while new threads continue to | 
 | 1262 | 	 * arrive.  It doesn't cost much - we're about to run a commit | 
 | 1263 | 	 * and sleep on IO anyway.  Speeds up many-threaded, many-dir | 
 | 1264 | 	 * operations by 30x or more... | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1265 | 	 * | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1266 | 	 * We try and optimize the sleep time against what the | 
 | 1267 | 	 * underlying disk can do, instead of having a static sleep | 
 | 1268 | 	 * time.  This is useful for the case where our storage is so | 
 | 1269 | 	 * fast that it is more optimal to go ahead and force a flush | 
 | 1270 | 	 * and wait for the transaction to be committed than it is to | 
 | 1271 | 	 * wait for an arbitrary amount of time for new writers to | 
 | 1272 | 	 * join the transaction.  We achieve this by measuring how | 
 | 1273 | 	 * long it takes to commit a transaction, and compare it with | 
 | 1274 | 	 * how long this transaction has been running, and if run time | 
 | 1275 | 	 * < commit time then we sleep for the delta and commit.  This | 
 | 1276 | 	 * greatly helps super fast disks that would see slowdowns as | 
 | 1277 | 	 * more threads started doing fsyncs. | 
 | 1278 | 	 * | 
 | 1279 | 	 * But don't do this if this process was the most recent one | 
 | 1280 | 	 * to perform a synchronous write.  We do this to detect the | 
 | 1281 | 	 * case where a single process is doing a stream of sync | 
 | 1282 | 	 * writes.  No point in waiting for joiners in that case. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1283 | 	 */ | 
 | 1284 | 	pid = current->pid; | 
 | 1285 | 	if (handle->h_sync && journal->j_last_sync_writer != pid) { | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1286 | 		u64 commit_time, trans_time; | 
 | 1287 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1288 | 		journal->j_last_sync_writer = pid; | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1289 |  | 
 | 1290 | 		spin_lock(&journal->j_state_lock); | 
 | 1291 | 		commit_time = journal->j_average_commit_time; | 
 | 1292 | 		spin_unlock(&journal->j_state_lock); | 
 | 1293 |  | 
 | 1294 | 		trans_time = ktime_to_ns(ktime_sub(ktime_get(), | 
 | 1295 | 						   transaction->t_start_time)); | 
 | 1296 |  | 
| Theodore Ts'o | 3077384 | 2009-01-03 20:27:38 -0500 | [diff] [blame] | 1297 | 		commit_time = max_t(u64, commit_time, | 
 | 1298 | 				    1000*journal->j_min_batch_time); | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1299 | 		commit_time = min_t(u64, commit_time, | 
| Theodore Ts'o | 3077384 | 2009-01-03 20:27:38 -0500 | [diff] [blame] | 1300 | 				    1000*journal->j_max_batch_time); | 
| Josef Bacik | e07f718 | 2008-11-26 01:14:26 -0500 | [diff] [blame] | 1301 |  | 
 | 1302 | 		if (trans_time < commit_time) { | 
 | 1303 | 			ktime_t expires = ktime_add_ns(ktime_get(), | 
 | 1304 | 						       commit_time); | 
 | 1305 | 			set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 1306 | 			schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); | 
 | 1307 | 		} | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1308 | 	} | 
 | 1309 |  | 
| Theodore Ts'o | 7058548 | 2009-03-25 23:35:46 -0400 | [diff] [blame] | 1310 | 	if (handle->h_sync) | 
 | 1311 | 		transaction->t_synchronous_commit = 1; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1312 | 	current->journal_info = NULL; | 
 | 1313 | 	spin_lock(&journal->j_state_lock); | 
 | 1314 | 	spin_lock(&transaction->t_handle_lock); | 
 | 1315 | 	transaction->t_outstanding_credits -= handle->h_buffer_credits; | 
 | 1316 | 	transaction->t_updates--; | 
 | 1317 | 	if (!transaction->t_updates) { | 
 | 1318 | 		wake_up(&journal->j_wait_updates); | 
 | 1319 | 		if (journal->j_barrier_count) | 
 | 1320 | 			wake_up(&journal->j_wait_transaction_locked); | 
 | 1321 | 	} | 
 | 1322 |  | 
 | 1323 | 	/* | 
 | 1324 | 	 * If the handle is marked SYNC, we need to set another commit | 
 | 1325 | 	 * going!  We also want to force a commit if the current | 
 | 1326 | 	 * transaction is occupying too much of the log, or if the | 
 | 1327 | 	 * transaction is too old now. | 
 | 1328 | 	 */ | 
 | 1329 | 	if (handle->h_sync || | 
 | 1330 | 			transaction->t_outstanding_credits > | 
 | 1331 | 				journal->j_max_transaction_buffers || | 
 | 1332 | 			time_after_eq(jiffies, transaction->t_expires)) { | 
 | 1333 | 		/* Do this even for aborted journals: an abort still | 
 | 1334 | 		 * completes the commit thread, it just doesn't write | 
 | 1335 | 		 * anything to disk. */ | 
 | 1336 | 		tid_t tid = transaction->t_tid; | 
 | 1337 |  | 
 | 1338 | 		spin_unlock(&transaction->t_handle_lock); | 
 | 1339 | 		jbd_debug(2, "transaction too old, requesting commit for " | 
 | 1340 | 					"handle %p\n", handle); | 
 | 1341 | 		/* This is non-blocking */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1342 | 		__jbd2_log_start_commit(journal, transaction->t_tid); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1343 | 		spin_unlock(&journal->j_state_lock); | 
 | 1344 |  | 
 | 1345 | 		/* | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1346 | 		 * Special case: JBD2_SYNC synchronous updates require us | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1347 | 		 * to wait for the commit to complete. | 
 | 1348 | 		 */ | 
 | 1349 | 		if (handle->h_sync && !(current->flags & PF_MEMALLOC)) | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1350 | 			err = jbd2_log_wait_commit(journal, tid); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1351 | 	} else { | 
 | 1352 | 		spin_unlock(&transaction->t_handle_lock); | 
 | 1353 | 		spin_unlock(&journal->j_state_lock); | 
 | 1354 | 	} | 
 | 1355 |  | 
| Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 1356 | 	lock_map_release(&handle->h_lockdep_map); | 
| Mingming Cao | 7b75106 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1357 |  | 
| Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1358 | 	jbd2_free_handle(handle); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1359 | 	return err; | 
 | 1360 | } | 
 | 1361 |  | 
| Randy Dunlap | 5648ba5 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1362 | /** | 
 | 1363 |  * int jbd2_journal_force_commit() - force any uncommitted transactions | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1364 |  * @journal: journal to force | 
 | 1365 |  * | 
 | 1366 |  * For synchronous operations: force any uncommitted transactions | 
 | 1367 |  * to disk.  May seem kludgy, but it reuses all the handle batching | 
 | 1368 |  * code in a very simple manner. | 
 | 1369 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1370 | int jbd2_journal_force_commit(journal_t *journal) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1371 | { | 
 | 1372 | 	handle_t *handle; | 
 | 1373 | 	int ret; | 
 | 1374 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1375 | 	handle = jbd2_journal_start(journal, 1); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1376 | 	if (IS_ERR(handle)) { | 
 | 1377 | 		ret = PTR_ERR(handle); | 
 | 1378 | 	} else { | 
 | 1379 | 		handle->h_sync = 1; | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1380 | 		ret = jbd2_journal_stop(handle); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1381 | 	} | 
 | 1382 | 	return ret; | 
 | 1383 | } | 
 | 1384 |  | 
 | 1385 | /* | 
 | 1386 |  * | 
 | 1387 |  * List management code snippets: various functions for manipulating the | 
 | 1388 |  * transaction buffer lists. | 
 | 1389 |  * | 
 | 1390 |  */ | 
 | 1391 |  | 
 | 1392 | /* | 
 | 1393 |  * Append a buffer to a transaction list, given the transaction's list head | 
 | 1394 |  * pointer. | 
 | 1395 |  * | 
 | 1396 |  * j_list_lock is held. | 
 | 1397 |  * | 
 | 1398 |  * jbd_lock_bh_state(jh2bh(jh)) is held. | 
 | 1399 |  */ | 
 | 1400 |  | 
 | 1401 | static inline void | 
 | 1402 | __blist_add_buffer(struct journal_head **list, struct journal_head *jh) | 
 | 1403 | { | 
 | 1404 | 	if (!*list) { | 
 | 1405 | 		jh->b_tnext = jh->b_tprev = jh; | 
 | 1406 | 		*list = jh; | 
 | 1407 | 	} else { | 
 | 1408 | 		/* Insert at the tail of the list to preserve order */ | 
 | 1409 | 		struct journal_head *first = *list, *last = first->b_tprev; | 
 | 1410 | 		jh->b_tprev = last; | 
 | 1411 | 		jh->b_tnext = first; | 
 | 1412 | 		last->b_tnext = first->b_tprev = jh; | 
 | 1413 | 	} | 
 | 1414 | } | 
 | 1415 |  | 
 | 1416 | /* | 
 | 1417 |  * Remove a buffer from a transaction list, given the transaction's list | 
 | 1418 |  * head pointer. | 
 | 1419 |  * | 
 | 1420 |  * Called with j_list_lock held, and the journal may not be locked. | 
 | 1421 |  * | 
 | 1422 |  * jbd_lock_bh_state(jh2bh(jh)) is held. | 
 | 1423 |  */ | 
 | 1424 |  | 
 | 1425 | static inline void | 
 | 1426 | __blist_del_buffer(struct journal_head **list, struct journal_head *jh) | 
 | 1427 | { | 
 | 1428 | 	if (*list == jh) { | 
 | 1429 | 		*list = jh->b_tnext; | 
 | 1430 | 		if (*list == jh) | 
 | 1431 | 			*list = NULL; | 
 | 1432 | 	} | 
 | 1433 | 	jh->b_tprev->b_tnext = jh->b_tnext; | 
 | 1434 | 	jh->b_tnext->b_tprev = jh->b_tprev; | 
 | 1435 | } | 
 | 1436 |  | 
 | 1437 | /* | 
 | 1438 |  * Remove a buffer from the appropriate transaction list. | 
 | 1439 |  * | 
 | 1440 |  * Note that this function can *change* the value of | 
| Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1441 |  * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list, | 
 | 1442 |  * t_log_list or t_reserved_list.  If the caller is holding onto a copy of one | 
 | 1443 |  * of these pointers, it could go bad.  Generally the caller needs to re-read | 
 | 1444 |  * the pointer from the transaction_t. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1445 |  * | 
 | 1446 |  * Called under j_list_lock.  The journal may not be locked. | 
 | 1447 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1448 | void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1449 | { | 
 | 1450 | 	struct journal_head **list = NULL; | 
 | 1451 | 	transaction_t *transaction; | 
 | 1452 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 1453 |  | 
 | 1454 | 	J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | 
 | 1455 | 	transaction = jh->b_transaction; | 
 | 1456 | 	if (transaction) | 
 | 1457 | 		assert_spin_locked(&transaction->t_journal->j_list_lock); | 
 | 1458 |  | 
 | 1459 | 	J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); | 
 | 1460 | 	if (jh->b_jlist != BJ_None) | 
| Mingming Cao | 4019191 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1461 | 		J_ASSERT_JH(jh, transaction != NULL); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1462 |  | 
 | 1463 | 	switch (jh->b_jlist) { | 
 | 1464 | 	case BJ_None: | 
 | 1465 | 		return; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1466 | 	case BJ_Metadata: | 
 | 1467 | 		transaction->t_nr_buffers--; | 
 | 1468 | 		J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0); | 
 | 1469 | 		list = &transaction->t_buffers; | 
 | 1470 | 		break; | 
 | 1471 | 	case BJ_Forget: | 
 | 1472 | 		list = &transaction->t_forget; | 
 | 1473 | 		break; | 
 | 1474 | 	case BJ_IO: | 
 | 1475 | 		list = &transaction->t_iobuf_list; | 
 | 1476 | 		break; | 
 | 1477 | 	case BJ_Shadow: | 
 | 1478 | 		list = &transaction->t_shadow_list; | 
 | 1479 | 		break; | 
 | 1480 | 	case BJ_LogCtl: | 
 | 1481 | 		list = &transaction->t_log_list; | 
 | 1482 | 		break; | 
 | 1483 | 	case BJ_Reserved: | 
 | 1484 | 		list = &transaction->t_reserved_list; | 
 | 1485 | 		break; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1486 | 	} | 
 | 1487 |  | 
 | 1488 | 	__blist_del_buffer(list, jh); | 
 | 1489 | 	jh->b_jlist = BJ_None; | 
 | 1490 | 	if (test_clear_buffer_jbddirty(bh)) | 
 | 1491 | 		mark_buffer_dirty(bh);	/* Expose it to the VM */ | 
 | 1492 | } | 
 | 1493 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1494 | void __jbd2_journal_unfile_buffer(struct journal_head *jh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1495 | { | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1496 | 	__jbd2_journal_temp_unlink_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1497 | 	jh->b_transaction = NULL; | 
 | 1498 | } | 
 | 1499 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1500 | void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1501 | { | 
 | 1502 | 	jbd_lock_bh_state(jh2bh(jh)); | 
 | 1503 | 	spin_lock(&journal->j_list_lock); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1504 | 	__jbd2_journal_unfile_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1505 | 	spin_unlock(&journal->j_list_lock); | 
 | 1506 | 	jbd_unlock_bh_state(jh2bh(jh)); | 
 | 1507 | } | 
 | 1508 |  | 
 | 1509 | /* | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1510 |  * Called from jbd2_journal_try_to_free_buffers(). | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1511 |  * | 
 | 1512 |  * Called under jbd_lock_bh_state(bh) | 
 | 1513 |  */ | 
 | 1514 | static void | 
 | 1515 | __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh) | 
 | 1516 | { | 
 | 1517 | 	struct journal_head *jh; | 
 | 1518 |  | 
 | 1519 | 	jh = bh2jh(bh); | 
 | 1520 |  | 
 | 1521 | 	if (buffer_locked(bh) || buffer_dirty(bh)) | 
 | 1522 | 		goto out; | 
 | 1523 |  | 
| Mingming Cao | 4019191 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1524 | 	if (jh->b_next_transaction != NULL) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1525 | 		goto out; | 
 | 1526 |  | 
 | 1527 | 	spin_lock(&journal->j_list_lock); | 
| Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1528 | 	if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) { | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1529 | 		/* written-back checkpointed metadata buffer */ | 
 | 1530 | 		if (jh->b_jlist == BJ_None) { | 
 | 1531 | 			JBUFFER_TRACE(jh, "remove from checkpoint list"); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1532 | 			__jbd2_journal_remove_checkpoint(jh); | 
 | 1533 | 			jbd2_journal_remove_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1534 | 			__brelse(bh); | 
 | 1535 | 		} | 
 | 1536 | 	} | 
 | 1537 | 	spin_unlock(&journal->j_list_lock); | 
 | 1538 | out: | 
 | 1539 | 	return; | 
 | 1540 | } | 
 | 1541 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1542 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1543 |  * int jbd2_journal_try_to_free_buffers() - try to free page buffers. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1544 |  * @journal: journal for operation | 
 | 1545 |  * @page: to try and free | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1546 |  * @gfp_mask: we use the mask to detect how hard should we try to release | 
 | 1547 |  * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to | 
 | 1548 |  * release the buffers. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1549 |  * | 
 | 1550 |  * | 
 | 1551 |  * For all the buffers on this page, | 
 | 1552 |  * if they are fully written out ordered data, move them onto BUF_CLEAN | 
 | 1553 |  * so try_to_free_buffers() can reap them. | 
 | 1554 |  * | 
 | 1555 |  * This function returns non-zero if we wish try_to_free_buffers() | 
 | 1556 |  * to be called. We do this if the page is releasable by try_to_free_buffers(). | 
 | 1557 |  * We also do it if the page has locked or dirty buffers and the caller wants | 
 | 1558 |  * us to perform sync or async writeout. | 
 | 1559 |  * | 
 | 1560 |  * This complicates JBD locking somewhat.  We aren't protected by the | 
 | 1561 |  * BKL here.  We wish to remove the buffer from its committing or | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1562 |  * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer. | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1563 |  * | 
 | 1564 |  * This may *change* the value of transaction_t->t_datalist, so anyone | 
 | 1565 |  * who looks at t_datalist needs to lock against this function. | 
 | 1566 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1567 |  * Even worse, someone may be doing a jbd2_journal_dirty_data on this | 
 | 1568 |  * buffer.  So we need to lock against that.  jbd2_journal_dirty_data() | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1569 |  * will come out of the lock with the buffer dirty, which makes it | 
 | 1570 |  * ineligible for release here. | 
 | 1571 |  * | 
 | 1572 |  * Who else is affected by this?  hmm...  Really the only contender | 
 | 1573 |  * is do_get_write_access() - it could be looking at the buffer while | 
 | 1574 |  * journal_try_to_free_buffer() is changing its state.  But that | 
 | 1575 |  * cannot happen because we never reallocate freed data as metadata | 
 | 1576 |  * while the data is part of a transaction.  Yes? | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1577 |  * | 
 | 1578 |  * Return 0 on failure, 1 on success | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1579 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1580 | int jbd2_journal_try_to_free_buffers(journal_t *journal, | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1581 | 				struct page *page, gfp_t gfp_mask) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1582 | { | 
 | 1583 | 	struct buffer_head *head; | 
 | 1584 | 	struct buffer_head *bh; | 
 | 1585 | 	int ret = 0; | 
 | 1586 |  | 
 | 1587 | 	J_ASSERT(PageLocked(page)); | 
 | 1588 |  | 
 | 1589 | 	head = page_buffers(page); | 
 | 1590 | 	bh = head; | 
 | 1591 | 	do { | 
 | 1592 | 		struct journal_head *jh; | 
 | 1593 |  | 
 | 1594 | 		/* | 
 | 1595 | 		 * We take our own ref against the journal_head here to avoid | 
 | 1596 | 		 * having to add tons of locking around each instance of | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1597 | 		 * jbd2_journal_remove_journal_head() and | 
 | 1598 | 		 * jbd2_journal_put_journal_head(). | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1599 | 		 */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1600 | 		jh = jbd2_journal_grab_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1601 | 		if (!jh) | 
 | 1602 | 			continue; | 
 | 1603 |  | 
 | 1604 | 		jbd_lock_bh_state(bh); | 
 | 1605 | 		__journal_try_to_free_buffer(journal, bh); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1606 | 		jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1607 | 		jbd_unlock_bh_state(bh); | 
 | 1608 | 		if (buffer_jbd(bh)) | 
 | 1609 | 			goto busy; | 
 | 1610 | 	} while ((bh = bh->b_this_page) != head); | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1611 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1612 | 	ret = try_to_free_buffers(page); | 
| Mingming Cao | 530576b | 2008-07-13 21:06:39 -0400 | [diff] [blame] | 1613 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1614 | busy: | 
 | 1615 | 	return ret; | 
 | 1616 | } | 
 | 1617 |  | 
 | 1618 | /* | 
 | 1619 |  * This buffer is no longer needed.  If it is on an older transaction's | 
 | 1620 |  * checkpoint list we need to record it on this transaction's forget list | 
 | 1621 |  * to pin this buffer (and hence its checkpointing transaction) down until | 
 | 1622 |  * this transaction commits.  If the buffer isn't on a checkpoint list, we | 
 | 1623 |  * release it. | 
 | 1624 |  * Returns non-zero if JBD no longer has an interest in the buffer. | 
 | 1625 |  * | 
 | 1626 |  * Called under j_list_lock. | 
 | 1627 |  * | 
 | 1628 |  * Called under jbd_lock_bh_state(bh). | 
 | 1629 |  */ | 
 | 1630 | static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction) | 
 | 1631 | { | 
 | 1632 | 	int may_free = 1; | 
 | 1633 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 1634 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1635 | 	__jbd2_journal_unfile_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1636 |  | 
 | 1637 | 	if (jh->b_cp_transaction) { | 
 | 1638 | 		JBUFFER_TRACE(jh, "on running+cp transaction"); | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 1639 | 		/* | 
 | 1640 | 		 * We don't want to write the buffer anymore, clear the | 
 | 1641 | 		 * bit so that we don't confuse checks in | 
 | 1642 | 		 * __journal_file_buffer | 
 | 1643 | 		 */ | 
 | 1644 | 		clear_buffer_dirty(bh); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1645 | 		__jbd2_journal_file_buffer(jh, transaction, BJ_Forget); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1646 | 		may_free = 0; | 
 | 1647 | 	} else { | 
 | 1648 | 		JBUFFER_TRACE(jh, "on running transaction"); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1649 | 		jbd2_journal_remove_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1650 | 		__brelse(bh); | 
 | 1651 | 	} | 
 | 1652 | 	return may_free; | 
 | 1653 | } | 
 | 1654 |  | 
 | 1655 | /* | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1656 |  * jbd2_journal_invalidatepage | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1657 |  * | 
 | 1658 |  * This code is tricky.  It has a number of cases to deal with. | 
 | 1659 |  * | 
 | 1660 |  * There are two invariants which this code relies on: | 
 | 1661 |  * | 
 | 1662 |  * i_size must be updated on disk before we start calling invalidatepage on the | 
 | 1663 |  * data. | 
 | 1664 |  * | 
 | 1665 |  *  This is done in ext3 by defining an ext3_setattr method which | 
 | 1666 |  *  updates i_size before truncate gets going.  By maintaining this | 
 | 1667 |  *  invariant, we can be sure that it is safe to throw away any buffers | 
 | 1668 |  *  attached to the current transaction: once the transaction commits, | 
 | 1669 |  *  we know that the data will not be needed. | 
 | 1670 |  * | 
 | 1671 |  *  Note however that we can *not* throw away data belonging to the | 
 | 1672 |  *  previous, committing transaction! | 
 | 1673 |  * | 
 | 1674 |  * Any disk blocks which *are* part of the previous, committing | 
 | 1675 |  * transaction (and which therefore cannot be discarded immediately) are | 
 | 1676 |  * not going to be reused in the new running transaction | 
 | 1677 |  * | 
 | 1678 |  *  The bitmap committed_data images guarantee this: any block which is | 
 | 1679 |  *  allocated in one transaction and removed in the next will be marked | 
 | 1680 |  *  as in-use in the committed_data bitmap, so cannot be reused until | 
 | 1681 |  *  the next transaction to delete the block commits.  This means that | 
 | 1682 |  *  leaving committing buffers dirty is quite safe: the disk blocks | 
 | 1683 |  *  cannot be reallocated to a different file and so buffer aliasing is | 
 | 1684 |  *  not possible. | 
 | 1685 |  * | 
 | 1686 |  * | 
 | 1687 |  * The above applies mainly to ordered data mode.  In writeback mode we | 
 | 1688 |  * don't make guarantees about the order in which data hits disk --- in | 
 | 1689 |  * particular we don't guarantee that new dirty data is flushed before | 
 | 1690 |  * transaction commit --- so it is always safe just to discard data | 
 | 1691 |  * immediately in that mode.  --sct | 
 | 1692 |  */ | 
 | 1693 |  | 
 | 1694 | /* | 
 | 1695 |  * The journal_unmap_buffer helper function returns zero if the buffer | 
 | 1696 |  * concerned remains pinned as an anonymous buffer belonging to an older | 
 | 1697 |  * transaction. | 
 | 1698 |  * | 
 | 1699 |  * We're outside-transaction here.  Either or both of j_running_transaction | 
 | 1700 |  * and j_committing_transaction may be NULL. | 
 | 1701 |  */ | 
 | 1702 | static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh) | 
 | 1703 | { | 
 | 1704 | 	transaction_t *transaction; | 
 | 1705 | 	struct journal_head *jh; | 
 | 1706 | 	int may_free = 1; | 
 | 1707 | 	int ret; | 
 | 1708 |  | 
 | 1709 | 	BUFFER_TRACE(bh, "entry"); | 
 | 1710 |  | 
 | 1711 | 	/* | 
 | 1712 | 	 * It is safe to proceed here without the j_list_lock because the | 
 | 1713 | 	 * buffers cannot be stolen by try_to_free_buffers as long as we are | 
 | 1714 | 	 * holding the page lock. --sct | 
 | 1715 | 	 */ | 
 | 1716 |  | 
 | 1717 | 	if (!buffer_jbd(bh)) | 
 | 1718 | 		goto zap_buffer_unlocked; | 
 | 1719 |  | 
| Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1720 | 	/* OK, we have data buffer in journaled mode */ | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1721 | 	spin_lock(&journal->j_state_lock); | 
 | 1722 | 	jbd_lock_bh_state(bh); | 
 | 1723 | 	spin_lock(&journal->j_list_lock); | 
 | 1724 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1725 | 	jh = jbd2_journal_grab_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1726 | 	if (!jh) | 
 | 1727 | 		goto zap_buffer_no_jh; | 
 | 1728 |  | 
 | 1729 | 	transaction = jh->b_transaction; | 
 | 1730 | 	if (transaction == NULL) { | 
 | 1731 | 		/* First case: not on any transaction.  If it | 
 | 1732 | 		 * has no checkpoint link, then we can zap it: | 
 | 1733 | 		 * it's a writeback-mode buffer so we don't care | 
 | 1734 | 		 * if it hits disk safely. */ | 
 | 1735 | 		if (!jh->b_cp_transaction) { | 
 | 1736 | 			JBUFFER_TRACE(jh, "not on any transaction: zap"); | 
 | 1737 | 			goto zap_buffer; | 
 | 1738 | 		} | 
 | 1739 |  | 
 | 1740 | 		if (!buffer_dirty(bh)) { | 
 | 1741 | 			/* bdflush has written it.  We can drop it now */ | 
 | 1742 | 			goto zap_buffer; | 
 | 1743 | 		} | 
 | 1744 |  | 
 | 1745 | 		/* OK, it must be in the journal but still not | 
 | 1746 | 		 * written fully to disk: it's metadata or | 
 | 1747 | 		 * journaled data... */ | 
 | 1748 |  | 
 | 1749 | 		if (journal->j_running_transaction) { | 
 | 1750 | 			/* ... and once the current transaction has | 
 | 1751 | 			 * committed, the buffer won't be needed any | 
 | 1752 | 			 * longer. */ | 
 | 1753 | 			JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget"); | 
 | 1754 | 			ret = __dispose_buffer(jh, | 
 | 1755 | 					journal->j_running_transaction); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1756 | 			jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1757 | 			spin_unlock(&journal->j_list_lock); | 
 | 1758 | 			jbd_unlock_bh_state(bh); | 
 | 1759 | 			spin_unlock(&journal->j_state_lock); | 
 | 1760 | 			return ret; | 
 | 1761 | 		} else { | 
 | 1762 | 			/* There is no currently-running transaction. So the | 
 | 1763 | 			 * orphan record which we wrote for this file must have | 
 | 1764 | 			 * passed into commit.  We must attach this buffer to | 
 | 1765 | 			 * the committing transaction, if it exists. */ | 
 | 1766 | 			if (journal->j_committing_transaction) { | 
 | 1767 | 				JBUFFER_TRACE(jh, "give to committing trans"); | 
 | 1768 | 				ret = __dispose_buffer(jh, | 
 | 1769 | 					journal->j_committing_transaction); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1770 | 				jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1771 | 				spin_unlock(&journal->j_list_lock); | 
 | 1772 | 				jbd_unlock_bh_state(bh); | 
 | 1773 | 				spin_unlock(&journal->j_state_lock); | 
 | 1774 | 				return ret; | 
 | 1775 | 			} else { | 
 | 1776 | 				/* The orphan record's transaction has | 
 | 1777 | 				 * committed.  We can cleanse this buffer */ | 
 | 1778 | 				clear_buffer_jbddirty(bh); | 
 | 1779 | 				goto zap_buffer; | 
 | 1780 | 			} | 
 | 1781 | 		} | 
 | 1782 | 	} else if (transaction == journal->j_committing_transaction) { | 
| Eric Sandeen | 9b57988 | 2006-10-28 10:38:28 -0700 | [diff] [blame] | 1783 | 		JBUFFER_TRACE(jh, "on committing transaction"); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1784 | 		/* | 
 | 1785 | 		 * If it is committing, we simply cannot touch it.  We | 
 | 1786 | 		 * can remove it's next_transaction pointer from the | 
 | 1787 | 		 * running transaction if that is set, but nothing | 
 | 1788 | 		 * else. */ | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1789 | 		set_buffer_freed(bh); | 
 | 1790 | 		if (jh->b_next_transaction) { | 
 | 1791 | 			J_ASSERT(jh->b_next_transaction == | 
 | 1792 | 					journal->j_running_transaction); | 
 | 1793 | 			jh->b_next_transaction = NULL; | 
 | 1794 | 		} | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1795 | 		jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1796 | 		spin_unlock(&journal->j_list_lock); | 
 | 1797 | 		jbd_unlock_bh_state(bh); | 
 | 1798 | 		spin_unlock(&journal->j_state_lock); | 
 | 1799 | 		return 0; | 
 | 1800 | 	} else { | 
 | 1801 | 		/* Good, the buffer belongs to the running transaction. | 
 | 1802 | 		 * We are writing our own transaction's data, not any | 
 | 1803 | 		 * previous one's, so it is safe to throw it away | 
 | 1804 | 		 * (remember that we expect the filesystem to have set | 
 | 1805 | 		 * i_size already for this truncate so recovery will not | 
 | 1806 | 		 * expose the disk blocks we are discarding here.) */ | 
 | 1807 | 		J_ASSERT_JH(jh, transaction == journal->j_running_transaction); | 
| Eric Sandeen | 9b57988 | 2006-10-28 10:38:28 -0700 | [diff] [blame] | 1808 | 		JBUFFER_TRACE(jh, "on running transaction"); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1809 | 		may_free = __dispose_buffer(jh, transaction); | 
 | 1810 | 	} | 
 | 1811 |  | 
 | 1812 | zap_buffer: | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1813 | 	jbd2_journal_put_journal_head(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1814 | zap_buffer_no_jh: | 
 | 1815 | 	spin_unlock(&journal->j_list_lock); | 
 | 1816 | 	jbd_unlock_bh_state(bh); | 
 | 1817 | 	spin_unlock(&journal->j_state_lock); | 
 | 1818 | zap_buffer_unlocked: | 
 | 1819 | 	clear_buffer_dirty(bh); | 
 | 1820 | 	J_ASSERT_BH(bh, !buffer_jbddirty(bh)); | 
 | 1821 | 	clear_buffer_mapped(bh); | 
 | 1822 | 	clear_buffer_req(bh); | 
 | 1823 | 	clear_buffer_new(bh); | 
 | 1824 | 	bh->b_bdev = NULL; | 
 | 1825 | 	return may_free; | 
 | 1826 | } | 
 | 1827 |  | 
 | 1828 | /** | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1829 |  * void jbd2_journal_invalidatepage() | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1830 |  * @journal: journal to use for flush... | 
 | 1831 |  * @page:    page to flush | 
 | 1832 |  * @offset:  length of page to invalidate. | 
 | 1833 |  * | 
 | 1834 |  * Reap page buffers containing data after offset in page. | 
 | 1835 |  * | 
 | 1836 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1837 | void jbd2_journal_invalidatepage(journal_t *journal, | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1838 | 		      struct page *page, | 
 | 1839 | 		      unsigned long offset) | 
 | 1840 | { | 
 | 1841 | 	struct buffer_head *head, *bh, *next; | 
 | 1842 | 	unsigned int curr_off = 0; | 
 | 1843 | 	int may_free = 1; | 
 | 1844 |  | 
 | 1845 | 	if (!PageLocked(page)) | 
 | 1846 | 		BUG(); | 
 | 1847 | 	if (!page_has_buffers(page)) | 
 | 1848 | 		return; | 
 | 1849 |  | 
 | 1850 | 	/* We will potentially be playing with lists other than just the | 
 | 1851 | 	 * data lists (especially for journaled data mode), so be | 
 | 1852 | 	 * cautious in our locking. */ | 
 | 1853 |  | 
 | 1854 | 	head = bh = page_buffers(page); | 
 | 1855 | 	do { | 
 | 1856 | 		unsigned int next_off = curr_off + bh->b_size; | 
 | 1857 | 		next = bh->b_this_page; | 
 | 1858 |  | 
 | 1859 | 		if (offset <= curr_off) { | 
 | 1860 | 			/* This block is wholly outside the truncation point */ | 
 | 1861 | 			lock_buffer(bh); | 
 | 1862 | 			may_free &= journal_unmap_buffer(journal, bh); | 
 | 1863 | 			unlock_buffer(bh); | 
 | 1864 | 		} | 
 | 1865 | 		curr_off = next_off; | 
 | 1866 | 		bh = next; | 
 | 1867 |  | 
 | 1868 | 	} while (bh != head); | 
 | 1869 |  | 
 | 1870 | 	if (!offset) { | 
 | 1871 | 		if (may_free && try_to_free_buffers(page)) | 
 | 1872 | 			J_ASSERT(!page_has_buffers(page)); | 
 | 1873 | 	} | 
 | 1874 | } | 
 | 1875 |  | 
 | 1876 | /* | 
 | 1877 |  * File a buffer on the given transaction list. | 
 | 1878 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1879 | void __jbd2_journal_file_buffer(struct journal_head *jh, | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1880 | 			transaction_t *transaction, int jlist) | 
 | 1881 | { | 
 | 1882 | 	struct journal_head **list = NULL; | 
 | 1883 | 	int was_dirty = 0; | 
 | 1884 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 1885 |  | 
 | 1886 | 	J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | 
 | 1887 | 	assert_spin_locked(&transaction->t_journal->j_list_lock); | 
 | 1888 |  | 
 | 1889 | 	J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); | 
 | 1890 | 	J_ASSERT_JH(jh, jh->b_transaction == transaction || | 
| Mingming Cao | 4019191 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1891 | 				jh->b_transaction == NULL); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1892 |  | 
 | 1893 | 	if (jh->b_transaction && jh->b_jlist == jlist) | 
 | 1894 | 		return; | 
 | 1895 |  | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1896 | 	if (jlist == BJ_Metadata || jlist == BJ_Reserved || | 
 | 1897 | 	    jlist == BJ_Shadow || jlist == BJ_Forget) { | 
| Jan Kara | f91d1d0 | 2009-07-13 16:16:20 -0400 | [diff] [blame] | 1898 | 		/* | 
 | 1899 | 		 * For metadata buffers, we track dirty bit in buffer_jbddirty | 
 | 1900 | 		 * instead of buffer_dirty. We should not see a dirty bit set | 
 | 1901 | 		 * here because we clear it in do_get_write_access but e.g. | 
 | 1902 | 		 * tune2fs can modify the sb and set the dirty bit at any time | 
 | 1903 | 		 * so we try to gracefully handle that. | 
 | 1904 | 		 */ | 
 | 1905 | 		if (buffer_dirty(bh)) | 
 | 1906 | 			warn_dirty_buffer(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1907 | 		if (test_clear_buffer_dirty(bh) || | 
 | 1908 | 		    test_clear_buffer_jbddirty(bh)) | 
 | 1909 | 			was_dirty = 1; | 
 | 1910 | 	} | 
 | 1911 |  | 
 | 1912 | 	if (jh->b_transaction) | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1913 | 		__jbd2_journal_temp_unlink_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1914 | 	jh->b_transaction = transaction; | 
 | 1915 |  | 
 | 1916 | 	switch (jlist) { | 
 | 1917 | 	case BJ_None: | 
 | 1918 | 		J_ASSERT_JH(jh, !jh->b_committed_data); | 
 | 1919 | 		J_ASSERT_JH(jh, !jh->b_frozen_data); | 
 | 1920 | 		return; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1921 | 	case BJ_Metadata: | 
 | 1922 | 		transaction->t_nr_buffers++; | 
 | 1923 | 		list = &transaction->t_buffers; | 
 | 1924 | 		break; | 
 | 1925 | 	case BJ_Forget: | 
 | 1926 | 		list = &transaction->t_forget; | 
 | 1927 | 		break; | 
 | 1928 | 	case BJ_IO: | 
 | 1929 | 		list = &transaction->t_iobuf_list; | 
 | 1930 | 		break; | 
 | 1931 | 	case BJ_Shadow: | 
 | 1932 | 		list = &transaction->t_shadow_list; | 
 | 1933 | 		break; | 
 | 1934 | 	case BJ_LogCtl: | 
 | 1935 | 		list = &transaction->t_log_list; | 
 | 1936 | 		break; | 
 | 1937 | 	case BJ_Reserved: | 
 | 1938 | 		list = &transaction->t_reserved_list; | 
 | 1939 | 		break; | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1940 | 	} | 
 | 1941 |  | 
 | 1942 | 	__blist_add_buffer(list, jh); | 
 | 1943 | 	jh->b_jlist = jlist; | 
 | 1944 |  | 
 | 1945 | 	if (was_dirty) | 
 | 1946 | 		set_buffer_jbddirty(bh); | 
 | 1947 | } | 
 | 1948 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1949 | void jbd2_journal_file_buffer(struct journal_head *jh, | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1950 | 				transaction_t *transaction, int jlist) | 
 | 1951 | { | 
 | 1952 | 	jbd_lock_bh_state(jh2bh(jh)); | 
 | 1953 | 	spin_lock(&transaction->t_journal->j_list_lock); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1954 | 	__jbd2_journal_file_buffer(jh, transaction, jlist); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1955 | 	spin_unlock(&transaction->t_journal->j_list_lock); | 
 | 1956 | 	jbd_unlock_bh_state(jh2bh(jh)); | 
 | 1957 | } | 
 | 1958 |  | 
 | 1959 | /* | 
 | 1960 |  * Remove a buffer from its current buffer list in preparation for | 
 | 1961 |  * dropping it from its current transaction entirely.  If the buffer has | 
 | 1962 |  * already started to be used by a subsequent transaction, refile the | 
 | 1963 |  * buffer on that transaction's metadata list. | 
 | 1964 |  * | 
 | 1965 |  * Called under journal->j_list_lock | 
 | 1966 |  * | 
 | 1967 |  * Called under jbd_lock_bh_state(jh2bh(jh)) | 
 | 1968 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1969 | void __jbd2_journal_refile_buffer(struct journal_head *jh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1970 | { | 
 | 1971 | 	int was_dirty; | 
 | 1972 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 1973 |  | 
 | 1974 | 	J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | 
 | 1975 | 	if (jh->b_transaction) | 
 | 1976 | 		assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock); | 
 | 1977 |  | 
 | 1978 | 	/* If the buffer is now unused, just drop it. */ | 
 | 1979 | 	if (jh->b_next_transaction == NULL) { | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1980 | 		__jbd2_journal_unfile_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1981 | 		return; | 
 | 1982 | 	} | 
 | 1983 |  | 
 | 1984 | 	/* | 
 | 1985 | 	 * It has been modified by a later transaction: add it to the new | 
 | 1986 | 	 * transaction's metadata list. | 
 | 1987 | 	 */ | 
 | 1988 |  | 
 | 1989 | 	was_dirty = test_clear_buffer_jbddirty(bh); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1990 | 	__jbd2_journal_temp_unlink_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1991 | 	jh->b_transaction = jh->b_next_transaction; | 
 | 1992 | 	jh->b_next_transaction = NULL; | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1993 | 	__jbd2_journal_file_buffer(jh, jh->b_transaction, | 
| Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1994 | 				jh->b_modified ? BJ_Metadata : BJ_Reserved); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1995 | 	J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING); | 
 | 1996 |  | 
 | 1997 | 	if (was_dirty) | 
 | 1998 | 		set_buffer_jbddirty(bh); | 
 | 1999 | } | 
 | 2000 |  | 
 | 2001 | /* | 
 | 2002 |  * For the unlocked version of this call, also make sure that any | 
 | 2003 |  * hanging journal_head is cleaned up if necessary. | 
 | 2004 |  * | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2005 |  * __jbd2_journal_refile_buffer is usually called as part of a single locked | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2006 |  * operation on a buffer_head, in which the caller is probably going to | 
 | 2007 |  * be hooking the journal_head onto other lists.  In that case it is up | 
 | 2008 |  * to the caller to remove the journal_head if necessary.  For the | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2009 |  * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2010 |  * doing anything else to the buffer so we need to do the cleanup | 
 | 2011 |  * ourselves to avoid a jh leak. | 
 | 2012 |  * | 
 | 2013 |  * *** The journal_head may be freed by this call! *** | 
 | 2014 |  */ | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2015 | void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh) | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2016 | { | 
 | 2017 | 	struct buffer_head *bh = jh2bh(jh); | 
 | 2018 |  | 
 | 2019 | 	jbd_lock_bh_state(bh); | 
 | 2020 | 	spin_lock(&journal->j_list_lock); | 
 | 2021 |  | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2022 | 	__jbd2_journal_refile_buffer(jh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2023 | 	jbd_unlock_bh_state(bh); | 
| Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2024 | 	jbd2_journal_remove_journal_head(bh); | 
| Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2025 |  | 
 | 2026 | 	spin_unlock(&journal->j_list_lock); | 
 | 2027 | 	__brelse(bh); | 
 | 2028 | } | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2029 |  | 
 | 2030 | /* | 
 | 2031 |  * File inode in the inode list of the handle's transaction | 
 | 2032 |  */ | 
 | 2033 | int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode) | 
 | 2034 | { | 
 | 2035 | 	transaction_t *transaction = handle->h_transaction; | 
 | 2036 | 	journal_t *journal = transaction->t_journal; | 
 | 2037 |  | 
 | 2038 | 	if (is_handle_aborted(handle)) | 
 | 2039 | 		return -EIO; | 
 | 2040 |  | 
 | 2041 | 	jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino, | 
 | 2042 | 			transaction->t_tid); | 
 | 2043 |  | 
 | 2044 | 	/* | 
 | 2045 | 	 * First check whether inode isn't already on the transaction's | 
 | 2046 | 	 * lists without taking the lock. Note that this check is safe | 
 | 2047 | 	 * without the lock as we cannot race with somebody removing inode | 
 | 2048 | 	 * from the transaction. The reason is that we remove inode from the | 
 | 2049 | 	 * transaction only in journal_release_jbd_inode() and when we commit | 
 | 2050 | 	 * the transaction. We are guarded from the first case by holding | 
 | 2051 | 	 * a reference to the inode. We are safe against the second case | 
 | 2052 | 	 * because if jinode->i_transaction == transaction, commit code | 
 | 2053 | 	 * cannot touch the transaction because we hold reference to it, | 
 | 2054 | 	 * and if jinode->i_next_transaction == transaction, commit code | 
 | 2055 | 	 * will only file the inode where we want it. | 
 | 2056 | 	 */ | 
 | 2057 | 	if (jinode->i_transaction == transaction || | 
 | 2058 | 	    jinode->i_next_transaction == transaction) | 
 | 2059 | 		return 0; | 
 | 2060 |  | 
 | 2061 | 	spin_lock(&journal->j_list_lock); | 
 | 2062 |  | 
 | 2063 | 	if (jinode->i_transaction == transaction || | 
 | 2064 | 	    jinode->i_next_transaction == transaction) | 
 | 2065 | 		goto done; | 
 | 2066 |  | 
 | 2067 | 	/* On some different transaction's list - should be | 
 | 2068 | 	 * the committing one */ | 
 | 2069 | 	if (jinode->i_transaction) { | 
 | 2070 | 		J_ASSERT(jinode->i_next_transaction == NULL); | 
 | 2071 | 		J_ASSERT(jinode->i_transaction == | 
 | 2072 | 					journal->j_committing_transaction); | 
 | 2073 | 		jinode->i_next_transaction = transaction; | 
 | 2074 | 		goto done; | 
 | 2075 | 	} | 
 | 2076 | 	/* Not on any transaction list... */ | 
 | 2077 | 	J_ASSERT(!jinode->i_next_transaction); | 
 | 2078 | 	jinode->i_transaction = transaction; | 
 | 2079 | 	list_add(&jinode->i_list, &transaction->t_inode_list); | 
 | 2080 | done: | 
 | 2081 | 	spin_unlock(&journal->j_list_lock); | 
 | 2082 |  | 
 | 2083 | 	return 0; | 
 | 2084 | } | 
 | 2085 |  | 
 | 2086 | /* | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2087 |  * File truncate and transaction commit interact with each other in a | 
 | 2088 |  * non-trivial way.  If a transaction writing data block A is | 
 | 2089 |  * committing, we cannot discard the data by truncate until we have | 
 | 2090 |  * written them.  Otherwise if we crashed after the transaction with | 
 | 2091 |  * write has committed but before the transaction with truncate has | 
 | 2092 |  * committed, we could see stale data in block A.  This function is a | 
 | 2093 |  * helper to solve this problem.  It starts writeout of the truncated | 
 | 2094 |  * part in case it is in the committing transaction. | 
 | 2095 |  * | 
 | 2096 |  * Filesystem code must call this function when inode is journaled in | 
 | 2097 |  * ordered mode before truncation happens and after the inode has been | 
 | 2098 |  * placed on orphan list with the new inode size. The second condition | 
 | 2099 |  * avoids the race that someone writes new data and we start | 
 | 2100 |  * committing the transaction after this function has been called but | 
 | 2101 |  * before a transaction for truncate is started (and furthermore it | 
 | 2102 |  * allows us to optimize the case where the addition to orphan list | 
 | 2103 |  * happens in the same transaction as write --- we don't have to write | 
 | 2104 |  * any data in such case). | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2105 |  */ | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2106 | int jbd2_journal_begin_ordered_truncate(journal_t *journal, | 
 | 2107 | 					struct jbd2_inode *jinode, | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2108 | 					loff_t new_size) | 
 | 2109 | { | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2110 | 	transaction_t *inode_trans, *commit_trans; | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2111 | 	int ret = 0; | 
 | 2112 |  | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2113 | 	/* This is a quick check to avoid locking if not necessary */ | 
 | 2114 | 	if (!jinode->i_transaction) | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2115 | 		goto out; | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2116 | 	/* Locks are here just to force reading of recent values, it is | 
 | 2117 | 	 * enough that the transaction was not committing before we started | 
 | 2118 | 	 * a transaction adding the inode to orphan list */ | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2119 | 	spin_lock(&journal->j_state_lock); | 
 | 2120 | 	commit_trans = journal->j_committing_transaction; | 
 | 2121 | 	spin_unlock(&journal->j_state_lock); | 
| Jan Kara | 7f5aa21 | 2009-02-10 11:15:34 -0500 | [diff] [blame] | 2122 | 	spin_lock(&journal->j_list_lock); | 
 | 2123 | 	inode_trans = jinode->i_transaction; | 
 | 2124 | 	spin_unlock(&journal->j_list_lock); | 
 | 2125 | 	if (inode_trans == commit_trans) { | 
 | 2126 | 		ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping, | 
| Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2127 | 			new_size, LLONG_MAX); | 
 | 2128 | 		if (ret) | 
 | 2129 | 			jbd2_journal_abort(journal, ret); | 
 | 2130 | 	} | 
 | 2131 | out: | 
 | 2132 | 	return ret; | 
 | 2133 | } |