summaryrefslogtreecommitdiffstats
path: root/fs/jbd2/transaction.c
diff options
context:
space:
mode:
authorJan Kara <jack@suse.cz>2013-06-04 12:35:11 -0400
committerTheodore Ts'o <tytso@mit.edu>2013-06-04 12:35:11 -0400
commit8f7d89f36829b9061a14f9040cda1372f264c4fe (patch)
tree2864747255fb15044c376519c0cb10bb223e9361 /fs/jbd2/transaction.c
parentf29fad72105287e6899d9128a9d494514f220e77 (diff)
jbd2: transaction reservation support
In some cases we cannot start a transaction because of locking constraints and passing started transaction into those places is not handy either because we could block transaction commit for too long. Transaction reservation is designed to solve these issues. It reserves a handle with given number of credits in the journal and the handle can be later attached to the running transaction without blocking on commit or checkpointing. Reserved handles do not block transaction commit in any way, they only reduce maximum size of the running transaction (because we have to always be prepared to accomodate request for attaching reserved handle). Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Diffstat (limited to 'fs/jbd2/transaction.c')
-rw-r--r--fs/jbd2/transaction.c328
1 files changed, 240 insertions, 88 deletions
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index f14288b04042..f33342a2a95e 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -89,7 +89,8 @@ jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
89 transaction->t_expires = jiffies + journal->j_commit_interval; 89 transaction->t_expires = jiffies + journal->j_commit_interval;
90 spin_lock_init(&transaction->t_handle_lock); 90 spin_lock_init(&transaction->t_handle_lock);
91 atomic_set(&transaction->t_updates, 0); 91 atomic_set(&transaction->t_updates, 0);
92 atomic_set(&transaction->t_outstanding_credits, 0); 92 atomic_set(&transaction->t_outstanding_credits,
93 atomic_read(&journal->j_reserved_credits));
93 atomic_set(&transaction->t_handle_count, 0); 94 atomic_set(&transaction->t_handle_count, 0);
94 INIT_LIST_HEAD(&transaction->t_inode_list); 95 INIT_LIST_HEAD(&transaction->t_inode_list);
95 INIT_LIST_HEAD(&transaction->t_private_list); 96 INIT_LIST_HEAD(&transaction->t_private_list);
@@ -141,6 +142,112 @@ static inline void update_t_max_wait(transaction_t *transaction,
141} 142}
142 143
143/* 144/*
145 * Wait until running transaction passes T_LOCKED state. Also starts the commit
146 * if needed. The function expects running transaction to exist and releases
147 * j_state_lock.
148 */
149static void wait_transaction_locked(journal_t *journal)
150 __releases(journal->j_state_lock)
151{
152 DEFINE_WAIT(wait);
153 int need_to_start;
154 tid_t tid = journal->j_running_transaction->t_tid;
155
156 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
157 TASK_UNINTERRUPTIBLE);
158 need_to_start = !tid_geq(journal->j_commit_request, tid);
159 read_unlock(&journal->j_state_lock);
160 if (need_to_start)
161 jbd2_log_start_commit(journal, tid);
162 schedule();
163 finish_wait(&journal->j_wait_transaction_locked, &wait);
164}
165
166static void sub_reserved_credits(journal_t *journal, int blocks)
167{
168 atomic_sub(blocks, &journal->j_reserved_credits);
169 wake_up(&journal->j_wait_reserved);
170}
171
172/*
173 * Wait until we can add credits for handle to the running transaction. Called
174 * with j_state_lock held for reading. Returns 0 if handle joined the running
175 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
176 * caller must retry.
177 */
178static int add_transaction_credits(journal_t *journal, int blocks,
179 int rsv_blocks)
180{
181 transaction_t *t = journal->j_running_transaction;
182 int needed;
183 int total = blocks + rsv_blocks;
184
185 /*
186 * If the current transaction is locked down for commit, wait
187 * for the lock to be released.
188 */
189 if (t->t_state == T_LOCKED) {
190 wait_transaction_locked(journal);
191 return 1;
192 }
193
194 /*
195 * If there is not enough space left in the log to write all
196 * potential buffers requested by this operation, we need to
197 * stall pending a log checkpoint to free some more log space.
198 */
199 needed = atomic_add_return(total, &t->t_outstanding_credits);
200 if (needed > journal->j_max_transaction_buffers) {
201 /*
202 * If the current transaction is already too large,
203 * then start to commit it: we can then go back and
204 * attach this handle to a new transaction.
205 */
206 atomic_sub(total, &t->t_outstanding_credits);
207 wait_transaction_locked(journal);
208 return 1;
209 }
210
211 /*
212 * The commit code assumes that it can get enough log space
213 * without forcing a checkpoint. This is *critical* for
214 * correctness: a checkpoint of a buffer which is also
215 * associated with a committing transaction creates a deadlock,
216 * so commit simply cannot force through checkpoints.
217 *
218 * We must therefore ensure the necessary space in the journal
219 * *before* starting to dirty potentially checkpointed buffers
220 * in the new transaction.
221 */
222 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
223 atomic_sub(total, &t->t_outstanding_credits);
224 read_unlock(&journal->j_state_lock);
225 write_lock(&journal->j_state_lock);
226 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
227 __jbd2_log_wait_for_space(journal);
228 write_unlock(&journal->j_state_lock);
229 return 1;
230 }
231
232 /* No reservation? We are done... */
233 if (!rsv_blocks)
234 return 0;
235
236 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
237 /* We allow at most half of a transaction to be reserved */
238 if (needed > journal->j_max_transaction_buffers / 2) {
239 sub_reserved_credits(journal, rsv_blocks);
240 atomic_sub(total, &t->t_outstanding_credits);
241 read_unlock(&journal->j_state_lock);
242 wait_event(journal->j_wait_reserved,
243 atomic_read(&journal->j_reserved_credits) + rsv_blocks
244 <= journal->j_max_transaction_buffers / 2);
245 return 1;
246 }
247 return 0;
248}
249
250/*
144 * start_this_handle: Given a handle, deal with any locking or stalling 251 * start_this_handle: Given a handle, deal with any locking or stalling
145 * needed to make sure that there is enough journal space for the handle 252 * needed to make sure that there is enough journal space for the handle
146 * to begin. Attach the handle to a transaction and set up the 253 * to begin. Attach the handle to a transaction and set up the
@@ -151,18 +258,24 @@ static int start_this_handle(journal_t *journal, handle_t *handle,
151 gfp_t gfp_mask) 258 gfp_t gfp_mask)
152{ 259{
153 transaction_t *transaction, *new_transaction = NULL; 260 transaction_t *transaction, *new_transaction = NULL;
154 tid_t tid; 261 int blocks = handle->h_buffer_credits;
155 int needed, need_to_start; 262 int rsv_blocks = 0;
156 int nblocks = handle->h_buffer_credits;
157 unsigned long ts = jiffies; 263 unsigned long ts = jiffies;
158 264
159 if (nblocks > journal->j_max_transaction_buffers) { 265 /*
266 * 1/2 of transaction can be reserved so we can practically handle
267 * only 1/2 of maximum transaction size per operation
268 */
269 if (WARN_ON(blocks > journal->j_max_transaction_buffers / 2)) {
160 printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n", 270 printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
161 current->comm, nblocks, 271 current->comm, blocks,
162 journal->j_max_transaction_buffers); 272 journal->j_max_transaction_buffers / 2);
163 return -ENOSPC; 273 return -ENOSPC;
164 } 274 }
165 275
276 if (handle->h_rsv_handle)
277 rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
278
166alloc_transaction: 279alloc_transaction:
167 if (!journal->j_running_transaction) { 280 if (!journal->j_running_transaction) {
168 new_transaction = kmem_cache_zalloc(transaction_cache, 281 new_transaction = kmem_cache_zalloc(transaction_cache,
@@ -199,8 +312,12 @@ repeat:
199 return -EROFS; 312 return -EROFS;
200 } 313 }
201 314
202 /* Wait on the journal's transaction barrier if necessary */ 315 /*
203 if (journal->j_barrier_count) { 316 * Wait on the journal's transaction barrier if necessary. Specifically
317 * we allow reserved handles to proceed because otherwise commit could
318 * deadlock on page writeback not being able to complete.
319 */
320 if (!handle->h_reserved && journal->j_barrier_count) {
204 read_unlock(&journal->j_state_lock); 321 read_unlock(&journal->j_state_lock);
205 wait_event(journal->j_wait_transaction_locked, 322 wait_event(journal->j_wait_transaction_locked,
206 journal->j_barrier_count == 0); 323 journal->j_barrier_count == 0);
@@ -213,7 +330,7 @@ repeat:
213 goto alloc_transaction; 330 goto alloc_transaction;
214 write_lock(&journal->j_state_lock); 331 write_lock(&journal->j_state_lock);
215 if (!journal->j_running_transaction && 332 if (!journal->j_running_transaction &&
216 !journal->j_barrier_count) { 333 (handle->h_reserved || !journal->j_barrier_count)) {
217 jbd2_get_transaction(journal, new_transaction); 334 jbd2_get_transaction(journal, new_transaction);
218 new_transaction = NULL; 335 new_transaction = NULL;
219 } 336 }
@@ -223,75 +340,18 @@ repeat:
223 340
224 transaction = journal->j_running_transaction; 341 transaction = journal->j_running_transaction;
225 342
226 /* 343 if (!handle->h_reserved) {
227 * If the current transaction is locked down for commit, wait for the 344 /* We may have dropped j_state_lock - restart in that case */
228 * lock to be released. 345 if (add_transaction_credits(journal, blocks, rsv_blocks))
229 */ 346 goto repeat;
230 if (transaction->t_state == T_LOCKED) { 347 } else {
231 DEFINE_WAIT(wait);
232
233 prepare_to_wait(&journal->j_wait_transaction_locked,
234 &wait, TASK_UNINTERRUPTIBLE);
235 read_unlock(&journal->j_state_lock);
236 schedule();
237 finish_wait(&journal->j_wait_transaction_locked, &wait);
238 goto repeat;
239 }
240
241 /*
242 * If there is not enough space left in the log to write all potential
243 * buffers requested by this operation, we need to stall pending a log
244 * checkpoint to free some more log space.
245 */
246 needed = atomic_add_return(nblocks,
247 &transaction->t_outstanding_credits);
248
249 if (needed > journal->j_max_transaction_buffers) {
250 /* 348 /*
251 * If the current transaction is already too large, then start 349 * We have handle reserved so we are allowed to join T_LOCKED
252 * to commit it: we can then go back and attach this handle to 350 * transaction and we don't have to check for transaction size
253 * a new transaction. 351 * and journal space.
254 */ 352 */
255 DEFINE_WAIT(wait); 353 sub_reserved_credits(journal, blocks);
256 354 handle->h_reserved = 0;
257 jbd_debug(2, "Handle %p starting new commit...\n", handle);
258 atomic_sub(nblocks, &transaction->t_outstanding_credits);
259 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
260 TASK_UNINTERRUPTIBLE);
261 tid = transaction->t_tid;
262 need_to_start = !tid_geq(journal->j_commit_request, tid);
263 read_unlock(&journal->j_state_lock);
264 if (need_to_start)
265 jbd2_log_start_commit(journal, tid);
266 schedule();
267 finish_wait(&journal->j_wait_transaction_locked, &wait);
268 goto repeat;
269 }
270
271 /*
272 * The commit code assumes that it can get enough log space
273 * without forcing a checkpoint. This is *critical* for
274 * correctness: a checkpoint of a buffer which is also
275 * associated with a committing transaction creates a deadlock,
276 * so commit simply cannot force through checkpoints.
277 *
278 * We must therefore ensure the necessary space in the journal
279 * *before* starting to dirty potentially checkpointed buffers
280 * in the new transaction.
281 *
282 * The worst part is, any transaction currently committing can
283 * reduce the free space arbitrarily. Be careful to account for
284 * those buffers when checkpointing.
285 */
286 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
287 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
288 atomic_sub(nblocks, &transaction->t_outstanding_credits);
289 read_unlock(&journal->j_state_lock);
290 write_lock(&journal->j_state_lock);
291 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
292 __jbd2_log_wait_for_space(journal);
293 write_unlock(&journal->j_state_lock);
294 goto repeat;
295 } 355 }
296 356
297 /* OK, account for the buffers that this operation expects to 357 /* OK, account for the buffers that this operation expects to
@@ -299,12 +359,12 @@ repeat:
299 */ 359 */
300 update_t_max_wait(transaction, ts); 360 update_t_max_wait(transaction, ts);
301 handle->h_transaction = transaction; 361 handle->h_transaction = transaction;
302 handle->h_requested_credits = nblocks; 362 handle->h_requested_credits = blocks;
303 handle->h_start_jiffies = jiffies; 363 handle->h_start_jiffies = jiffies;
304 atomic_inc(&transaction->t_updates); 364 atomic_inc(&transaction->t_updates);
305 atomic_inc(&transaction->t_handle_count); 365 atomic_inc(&transaction->t_handle_count);
306 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n", 366 jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
307 handle, nblocks, 367 handle, blocks,
308 atomic_read(&transaction->t_outstanding_credits), 368 atomic_read(&transaction->t_outstanding_credits),
309 jbd2_log_space_left(journal)); 369 jbd2_log_space_left(journal));
310 read_unlock(&journal->j_state_lock); 370 read_unlock(&journal->j_state_lock);
@@ -338,16 +398,21 @@ static handle_t *new_handle(int nblocks)
338 * 398 *
339 * We make sure that the transaction can guarantee at least nblocks of 399 * We make sure that the transaction can guarantee at least nblocks of
340 * modified buffers in the log. We block until the log can guarantee 400 * modified buffers in the log. We block until the log can guarantee
341 * that much space. 401 * that much space. Additionally, if rsv_blocks > 0, we also create another
342 * 402 * handle with rsv_blocks reserved blocks in the journal. This handle is
343 * This function is visible to journal users (like ext3fs), so is not 403 * is stored in h_rsv_handle. It is not attached to any particular transaction
344 * called with the journal already locked. 404 * and thus doesn't block transaction commit. If the caller uses this reserved
405 * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
406 * on the parent handle will dispose the reserved one. Reserved handle has to
407 * be converted to a normal handle using jbd2_journal_start_reserved() before
408 * it can be used.
345 * 409 *
346 * Return a pointer to a newly allocated handle, or an ERR_PTR() value 410 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
347 * on failure. 411 * on failure.
348 */ 412 */
349handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask, 413handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
350 unsigned int type, unsigned int line_no) 414 gfp_t gfp_mask, unsigned int type,
415 unsigned int line_no)
351{ 416{
352 handle_t *handle = journal_current_handle(); 417 handle_t *handle = journal_current_handle();
353 int err; 418 int err;
@@ -364,11 +429,25 @@ handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask,
364 handle = new_handle(nblocks); 429 handle = new_handle(nblocks);
365 if (!handle) 430 if (!handle)
366 return ERR_PTR(-ENOMEM); 431 return ERR_PTR(-ENOMEM);
432 if (rsv_blocks) {
433 handle_t *rsv_handle;
434
435 rsv_handle = new_handle(rsv_blocks);
436 if (!rsv_handle) {
437 jbd2_free_handle(handle);
438 return ERR_PTR(-ENOMEM);
439 }
440 rsv_handle->h_reserved = 1;
441 rsv_handle->h_journal = journal;
442 handle->h_rsv_handle = rsv_handle;
443 }
367 444
368 current->journal_info = handle; 445 current->journal_info = handle;
369 446
370 err = start_this_handle(journal, handle, gfp_mask); 447 err = start_this_handle(journal, handle, gfp_mask);
371 if (err < 0) { 448 if (err < 0) {
449 if (handle->h_rsv_handle)
450 jbd2_free_handle(handle->h_rsv_handle);
372 jbd2_free_handle(handle); 451 jbd2_free_handle(handle);
373 current->journal_info = NULL; 452 current->journal_info = NULL;
374 return ERR_PTR(err); 453 return ERR_PTR(err);
@@ -385,10 +464,68 @@ EXPORT_SYMBOL(jbd2__journal_start);
385 464
386handle_t *jbd2_journal_start(journal_t *journal, int nblocks) 465handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
387{ 466{
388 return jbd2__journal_start(journal, nblocks, GFP_NOFS, 0, 0); 467 return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
389} 468}
390EXPORT_SYMBOL(jbd2_journal_start); 469EXPORT_SYMBOL(jbd2_journal_start);
391 470
471void jbd2_journal_free_reserved(handle_t *handle)
472{
473 journal_t *journal = handle->h_journal;
474
475 WARN_ON(!handle->h_reserved);
476 sub_reserved_credits(journal, handle->h_buffer_credits);
477 jbd2_free_handle(handle);
478}
479EXPORT_SYMBOL(jbd2_journal_free_reserved);
480
481/**
482 * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle
483 * @handle: handle to start
484 *
485 * Start handle that has been previously reserved with jbd2_journal_reserve().
486 * This attaches @handle to the running transaction (or creates one if there's
487 * not transaction running). Unlike jbd2_journal_start() this function cannot
488 * block on journal commit, checkpointing, or similar stuff. It can block on
489 * memory allocation or frozen journal though.
490 *
491 * Return 0 on success, non-zero on error - handle is freed in that case.
492 */
493int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
494 unsigned int line_no)
495{
496 journal_t *journal = handle->h_journal;
497 int ret = -EIO;
498
499 if (WARN_ON(!handle->h_reserved)) {
500 /* Someone passed in normal handle? Just stop it. */
501 jbd2_journal_stop(handle);
502 return ret;
503 }
504 /*
505 * Usefulness of mixing of reserved and unreserved handles is
506 * questionable. So far nobody seems to need it so just error out.
507 */
508 if (WARN_ON(current->journal_info)) {
509 jbd2_journal_free_reserved(handle);
510 return ret;
511 }
512
513 handle->h_journal = NULL;
514 current->journal_info = handle;
515 /*
516 * GFP_NOFS is here because callers are likely from writeback or
517 * similarly constrained call sites
518 */
519 ret = start_this_handle(journal, handle, GFP_NOFS);
520 if (ret < 0) {
521 current->journal_info = NULL;
522 jbd2_journal_free_reserved(handle);
523 }
524 handle->h_type = type;
525 handle->h_line_no = line_no;
526 return ret;
527}
528EXPORT_SYMBOL(jbd2_journal_start_reserved);
392 529
393/** 530/**
394 * int jbd2_journal_extend() - extend buffer credits. 531 * int jbd2_journal_extend() - extend buffer credits.
@@ -483,7 +620,8 @@ out:
483 * to a running handle, a call to jbd2_journal_restart will commit the 620 * to a running handle, a call to jbd2_journal_restart will commit the
484 * handle's transaction so far and reattach the handle to a new 621 * handle's transaction so far and reattach the handle to a new
485 * transaction capabable of guaranteeing the requested number of 622 * transaction capabable of guaranteeing the requested number of
486 * credits. 623 * credits. We preserve reserved handle if there's any attached to the
624 * passed in handle.
487 */ 625 */
488int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) 626int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
489{ 627{
@@ -508,6 +646,10 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
508 spin_lock(&transaction->t_handle_lock); 646 spin_lock(&transaction->t_handle_lock);
509 atomic_sub(handle->h_buffer_credits, 647 atomic_sub(handle->h_buffer_credits,
510 &transaction->t_outstanding_credits); 648 &transaction->t_outstanding_credits);
649 if (handle->h_rsv_handle) {
650 sub_reserved_credits(journal,
651 handle->h_rsv_handle->h_buffer_credits);
652 }
511 if (atomic_dec_and_test(&transaction->t_updates)) 653 if (atomic_dec_and_test(&transaction->t_updates))
512 wake_up(&journal->j_wait_updates); 654 wake_up(&journal->j_wait_updates);
513 spin_unlock(&transaction->t_handle_lock); 655 spin_unlock(&transaction->t_handle_lock);
@@ -550,6 +692,14 @@ void jbd2_journal_lock_updates(journal_t *journal)
550 write_lock(&journal->j_state_lock); 692 write_lock(&journal->j_state_lock);
551 ++journal->j_barrier_count; 693 ++journal->j_barrier_count;
552 694
695 /* Wait until there are no reserved handles */
696 if (atomic_read(&journal->j_reserved_credits)) {
697 write_unlock(&journal->j_state_lock);
698 wait_event(journal->j_wait_reserved,
699 atomic_read(&journal->j_reserved_credits) == 0);
700 write_lock(&journal->j_state_lock);
701 }
702
553 /* Wait until there are no running updates */ 703 /* Wait until there are no running updates */
554 while (1) { 704 while (1) {
555 transaction_t *transaction = journal->j_running_transaction; 705 transaction_t *transaction = journal->j_running_transaction;
@@ -1505,6 +1655,8 @@ int jbd2_journal_stop(handle_t *handle)
1505 1655
1506 lock_map_release(&handle->h_lockdep_map); 1656 lock_map_release(&handle->h_lockdep_map);
1507 1657
1658 if (handle->h_rsv_handle)
1659 jbd2_journal_free_reserved(handle->h_rsv_handle);
1508 jbd2_free_handle(handle); 1660 jbd2_free_handle(handle);
1509 return err; 1661 return err;
1510} 1662}