📄 transaction.c
字号:
/* * akpm: I added this. ext3_alloc_branch can pick up new indirect * blocks which contain freed but then revoked metadata. We need * to cancel the revoke in case we end up freeing it yet again * and the reallocating as data - this would cause a second revoke, * which hits an assertion error. */ JBUFFER_TRACE(jh, "cancelling revoke"); journal_cancel_revoke(handle, jh); journal_unlock_journal_head(jh);out: unlock_journal(journal); return err;}/* * journal_get_undo_access: Notify intent to modify metadata with non- * rewindable consequences * * Sometimes there is a need to distinguish between metadata which has * been committed to disk and that which has not. The ext3fs code uses * this for freeing and allocating space: we have to make sure that we * do not reuse freed space until the deallocation has been committed, * since if we overwrote that space we would make the delete * un-rewindable in case of a crash. * * To deal with that, journal_get_undo_access requests write access to a * buffer for parts of non-rewindable operations such as delete * operations on the bitmaps. The journaling code must keep a copy of * the buffer's contents prior to the undo_access call until such time * as we know that the buffer has definitely been committed to disk. * * We never need to know which transaction the committed data is part * of: buffers touched here are guaranteed to be dirtied later and so * will be committed to a new transaction in due course, at which point * we can discard the old committed data pointer. * * Returns error number or 0 on success. */int journal_get_undo_access (handle_t *handle, struct buffer_head *bh){ journal_t *journal = handle->h_transaction->t_journal; int err; struct journal_head *jh = journal_add_journal_head(bh); JBUFFER_TRACE(jh, "entry"); lock_journal(journal); /* Do this first --- it can drop the journal lock, so we want to * make sure that obtaining the committed_data is done * atomically wrt. completion of any outstanding commits. */ err = do_get_write_access (handle, jh, 1); if (err) goto out; if (!jh->b_committed_data) { /* Copy out the current buffer contents into the * preserved, committed copy. */ JBUFFER_TRACE(jh, "generate b_committed data"); jh->b_committed_data = jbd_kmalloc(jh2bh(jh)->b_size, GFP_NOFS); if (!jh->b_committed_data) { printk(KERN_EMERG __FUNCTION__ ": No memory for committed data!\n"); err = -ENOMEM; goto out; } memcpy (jh->b_committed_data, jh2bh(jh)->b_data, jh2bh(jh)->b_size); }out: if (!err) J_ASSERT_JH(jh, jh->b_committed_data); journal_unlock_journal_head(jh); unlock_journal(journal); return err;}/* * journal_dirty_data: mark a buffer as containing dirty data which * needs to be flushed before we can commit the current transaction. * * The buffer is placed on the transaction's data list and is marked as * belonging to the transaction. * * If `async' is set then the writebask will be initiated by the caller * using submit_bh -> end_buffer_io_async. We put the buffer onto * t_async_datalist. * * Returns error number or 0 on success. * * journal_dirty_data() can be called via page_launder->ext3_writepage * by kswapd. So it cannot block. Happily, there's nothing here * which needs lock_journal if `async' is set. * * When the buffer is on the current transaction we freely move it * between BJ_AsyncData and BJ_SyncData according to who tried to * change its state last. */int journal_dirty_data (handle_t *handle, struct buffer_head *bh, int async){ journal_t *journal = handle->h_transaction->t_journal; int need_brelse = 0; int wanted_jlist = async ? BJ_AsyncData : BJ_SyncData; struct journal_head *jh; if (is_handle_aborted(handle)) return 0; jh = journal_add_journal_head(bh); JBUFFER_TRACE(jh, "entry"); /* * The buffer could *already* be dirty. Writeout can start * at any time. */ jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid); /* * What if the buffer is already part of a running transaction? * * There are two cases: * 1) It is part of the current running transaction. Refile it, * just in case we have allocated it as metadata, deallocated * it, then reallocated it as data. * 2) It is part of the previous, still-committing transaction. * If all we want to do is to guarantee that the buffer will be * written to disk before this new transaction commits, then * being sure that the *previous* transaction has this same * property is sufficient for us! Just leave it on its old * transaction. * * In case (2), the buffer must not already exist as metadata * --- that would violate write ordering (a transaction is free * to write its data at any point, even before the previous * committing transaction has committed). The caller must * never, ever allow this to happen: there's nothing we can do * about it in this layer. */ spin_lock(&journal_datalist_lock); if (jh->b_transaction) { JBUFFER_TRACE(jh, "has transaction"); if (jh->b_transaction != handle->h_transaction) { JBUFFER_TRACE(jh, "belongs to older transaction"); J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction); /* @@@ IS THIS TRUE ? */ /* * Not any more. Scenario: someone does a write() * in data=journal mode. The buffer's transaction has * moved into commit. Then someone does another * write() to the file. We do the frozen data copyout * and set b_next_transaction to point to j_running_t. * And while we're in that state, someone does a * writepage() in an attempt to pageout the same area * of the file via a shared mapping. At present that * calls journal_dirty_data(), and we get right here. * It may be too late to journal the data. Simply * falling through to the next test will suffice: the * data will be dirty and wil be checkpointed. The * ordering comments in the next comment block still * apply. */ //J_ASSERT_JH(jh, jh->b_next_transaction == NULL); /* * If we're journalling data, and this buffer was * subject to a write(), it could be metadata, forget * or shadow against the committing transaction. Now, * someone has dirtied the same darn page via a mapping * and it is being writepage()'d. * We *could* just steal the page from commit, with some * fancy locking there. Instead, we just skip it - * don't tie the page's buffers to the new transaction * at all. * Implication: if we crash before the writepage() data * is written into the filesystem, recovery will replay * the write() data. */ if (jh->b_jlist != BJ_None && jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_AsyncData) { JBUFFER_TRACE(jh, "Not stealing"); goto no_journal; } /* * This buffer may be undergoing writeout in commit. We * can't return from here and let the caller dirty it * again because that can cause the write-out loop in * commit to never terminate. */ if (!async && buffer_dirty(bh)) { atomic_inc(&bh->b_count); spin_unlock(&journal_datalist_lock); need_brelse = 1; ll_rw_block(WRITE, 1, &bh); wait_on_buffer(bh); spin_lock(&journal_datalist_lock); /* The buffer may become locked again at any time if it is redirtied */ } /* journal_clean_data_list() may have got there first */ if (jh->b_transaction != NULL) { JBUFFER_TRACE(jh, "unfile from commit"); __journal_unfile_buffer(jh); jh->b_transaction = NULL; } /* The buffer will be refiled below */ } /* * Special case --- the buffer might actually have been * allocated and then immediately deallocated in the previous, * committing transaction, so might still be left on that * transaction's metadata lists. */ if (jh->b_jlist != wanted_jlist) { JBUFFER_TRACE(jh, "not on correct data list: unfile"); J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow); __journal_unfile_buffer(jh); jh->b_transaction = NULL; JBUFFER_TRACE(jh, "file as data"); __journal_file_buffer(jh, handle->h_transaction, wanted_jlist); } } else { JBUFFER_TRACE(jh, "not on a transaction"); __journal_file_buffer(jh, handle->h_transaction, wanted_jlist); }no_journal: spin_unlock(&journal_datalist_lock); if (need_brelse) { BUFFER_TRACE(bh, "brelse"); __brelse(bh); } JBUFFER_TRACE(jh, "exit"); journal_unlock_journal_head(jh); return 0;}/* * journal_dirty_metadata: mark a buffer as containing dirty metadata * which needs to be journaled as part of the current transaction. * * The buffer is placed on the transaction's metadata list and is marked * as belonging to the transaction. * * Special care needs to be taken if the buffer already belongs to the * current committing transaction (in which case we should have frozen * data present for that commit). In that case, we don't relink the * buffer: that only gets done when the old transaction finally * completes its commit. * * Returns error number or 0 on success. */int journal_dirty_metadata (handle_t *handle, struct buffer_head *bh){ transaction_t *transaction = handle->h_transaction; journal_t *journal = transaction->t_journal; struct journal_head *jh = bh2jh(bh); jbd_debug(5, "journal_head %p\n", jh); JBUFFER_TRACE(jh, "entry"); lock_journal(journal); if (is_handle_aborted(handle)) goto out_unlock; spin_lock(&journal_datalist_lock); set_bit(BH_JBDDirty, &bh->b_state); set_buffer_flushtime(bh); J_ASSERT_JH(jh, jh->b_transaction != NULL); /* * Metadata already on the current transaction list doesn't * need to be filed. Metadata on another transaction's list must * be committing, and will be refiled once the commit completes: * leave it alone for now. */ if (jh->b_transaction != transaction) { JBUFFER_TRACE(jh, "already on other transaction"); J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction); J_ASSERT_JH(jh, jh->b_next_transaction == transaction); /* And this case is illegal: we can't reuse another * transaction's data buffer, ever. */ /* FIXME: writepage() should be journalled */ J_ASSERT_JH(jh, jh->b_jlist != BJ_SyncData); goto done_locked; } /* That test should have eliminated the following case: */ J_ASSERT_JH(jh, jh->b_frozen_data == 0); JBUFFER_TRACE(jh, "file as BJ_Metadata"); __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);done_locked: spin_unlock(&journal_datalist_lock); JBUFFER_TRACE(jh, "exit");out_unlock: unlock_journal(journal); return 0;}#if 0/* * journal_release_buffer: undo a get_write_access without any buffer * updates, if the update decided in the end that it didn't need access. * * journal_get_write_access() can block, so it is quite possible for a * journaling component to decide after the write access is returned * that global state has changed and the update is no longer required. */void journal_release_buffer (handle_t *handle, struct buffer_head *bh){ transaction_t *transaction = handle->h_transaction; journal_t *journal = transaction->t_journal; struct journal_head *jh = bh2jh(bh); lock_journal(journal); JBUFFER_TRACE(jh, "entry"); /* If the buffer is reserved but not modified by this * transaction, then it is safe to release it. In all other * cases, just leave the buffer as it is. */ spin_lock(&journal_datalist_lock); if (jh->b_jlist == BJ_Reserved && jh->b_transaction == transaction && !buffer_jdirty(jh2bh(jh))) { JBUFFER_TRACE(jh, "unused: refiling it"); handle->h_buffer_credits++; __journal_refile_buffer(jh); } spin_unlock(&journal_datalist_lock); JBUFFER_TRACE(jh, "exit"); unlock_journal(journal);}#endif/* * journal_forget: bforget() for potentially-journaled buffers. We can * only do the bforget if there are no commits pending against the * buffer. If the buffer is dirty in the current running transaction we * can safely unlink it. * * bh may not be a journalled buffer at all - it may be a non-JBD * buffer which came off the hashtable. Check for this. * * Decrements bh->b_count by one. * * Allow this call even if the handle has aborted --- it may be part of * the caller's cleanup after an abort. */void journal_forget (handle_t *handle, struct buffer_head *bh){ transaction_t *transaction = handle->h_transaction; journal_t *journal = transaction->t_journal; struct journal_head *jh; BUFFER_TRACE(bh, "entry"); lock_journal(journal); spin_lock(&journal_datalist_lock); if (!buffer_jbd(bh)) goto not_jbd; jh = bh2jh(bh); if (jh->b_transaction == handle->h_transaction) { J_ASSERT_JH(jh, !jh->b_frozen_data); /* If we are forgetting a buffer which is already part * of this transaction, then we can just drop it from * the transaction immediately. */ clear_bit(BH_Dirty, &bh->b_state); clear_bit(BH_JBDDirty, &bh->b_state); JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); J_ASSERT_JH(jh, !jh->b_committed_data); __journal_unfile_buffer(jh); jh->b_transaction = 0; /* * We are no longer going to journal this buffer. * However, the commit of this transaction is still * important to the buffer: the delete that we are now * processing might obsolete an old log entry, so by * committing, we can satisfy the buffer's checkpoint. * * So, if we have a checkpoint on the buffer, we should * now refile the buffer on our BJ_Forget list so that * we know to remove the checkpoint after we commit. */ if (jh->b_cp_transaction) { __journal_file_buffer(jh, transaction, BJ_Forget); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -