⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transaction.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/** * int journal_get_undo_access() -  Notify intent to modify metadata with *     non-rewindable consequences * @handle: transaction * @bh: buffer to undo * @credits: store the number of taken credits here (if not NULL) * * 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){	int err;	struct journal_head *jh = journal_add_journal_head(bh);	char *committed_data = NULL;	JBUFFER_TRACE(jh, "entry");	/*	 * 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;repeat:	if (!jh->b_committed_data) {		committed_data = jbd_alloc(jh2bh(jh)->b_size, GFP_NOFS);		if (!committed_data) {			printk(KERN_EMERG "%s: No memory for committed data\n",				__FUNCTION__);			err = -ENOMEM;			goto out;		}	}	jbd_lock_bh_state(bh);	if (!jh->b_committed_data) {		/* Copy out the current buffer contents into the		 * preserved, committed copy. */		JBUFFER_TRACE(jh, "generate b_committed data");		if (!committed_data) {			jbd_unlock_bh_state(bh);			goto repeat;		}		jh->b_committed_data = committed_data;		committed_data = NULL;		memcpy(jh->b_committed_data, bh->b_data, bh->b_size);	}	jbd_unlock_bh_state(bh);out:	journal_put_journal_head(jh);	if (unlikely(committed_data))		jbd_free(committed_data, bh->b_size);	return err;}/** * int journal_dirty_data() -  mark a buffer as containing dirty data which *                             needs to be flushed before we can commit the *                             current transaction. * @handle: transaction * @bh: bufferhead to mark * * The buffer is placed on the transaction's data list and is marked as * belonging to the transaction. * * Returns error number or 0 on success. * * journal_dirty_data() can be called via page_launder->ext3_writepage * by kswapd. */int journal_dirty_data(handle_t *handle, struct buffer_head *bh){	journal_t *journal = handle->h_transaction->t_journal;	int need_brelse = 0;	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.	 */	jbd_lock_bh_state(bh);	spin_lock(&journal->j_list_lock);	/* Now that we have bh_state locked, are we really still mapped? */	if (!buffer_mapped(bh)) {		JBUFFER_TRACE(jh, "unmapped buffer, bailing out");		goto no_journal;	}	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_Locked) {				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 (buffer_dirty(bh)) {				get_bh(bh);				spin_unlock(&journal->j_list_lock);				jbd_unlock_bh_state(bh);				need_brelse = 1;				sync_dirty_buffer(bh);				jbd_lock_bh_state(bh);				spin_lock(&journal->j_list_lock);				/* Since we dropped the lock... */				if (!buffer_mapped(bh)) {					JBUFFER_TRACE(jh, "buffer got unmapped");					goto no_journal;				}				/* 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_temp_unlink_buffer(jh);				/* It still points to the committing				 * transaction; move it to this one so				 * that the refile assert checks are				 * happy. */				jh->b_transaction = handle->h_transaction;			}			/* 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 != BJ_SyncData && jh->b_jlist != BJ_Locked) {			JBUFFER_TRACE(jh, "not on correct data list: unfile");			J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);			__journal_temp_unlink_buffer(jh);			jh->b_transaction = handle->h_transaction;			JBUFFER_TRACE(jh, "file as data");			__journal_file_buffer(jh, handle->h_transaction,						BJ_SyncData);		}	} else {		JBUFFER_TRACE(jh, "not on a transaction");		__journal_file_buffer(jh, handle->h_transaction, BJ_SyncData);	}no_journal:	spin_unlock(&journal->j_list_lock);	jbd_unlock_bh_state(bh);	if (need_brelse) {		BUFFER_TRACE(bh, "brelse");		__brelse(bh);	}	JBUFFER_TRACE(jh, "exit");	journal_put_journal_head(jh);	return 0;}/** * int journal_dirty_metadata() -  mark a buffer as containing dirty metadata * @handle: transaction to add buffer to. * @bh: buffer to mark * * mark 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. * * Returns error number or 0 on success. * * 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. */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");	if (is_handle_aborted(handle))		goto out;	jbd_lock_bh_state(bh);	if (jh->b_modified == 0) {		/*		 * This buffer's got modified and becoming part		 * of the transaction. This needs to be done		 * once a transaction -bzzz		 */		jh->b_modified = 1;		J_ASSERT_JH(jh, handle->h_buffer_credits > 0);		handle->h_buffer_credits--;	}	/*	 * fastpath, to avoid expensive locking.  If this buffer is already	 * on the running transaction's metadata list there is nothing to do.	 * Nobody can take it off again because there is a handle open.	 * I _think_ we're OK here with SMP barriers - a mistaken decision will	 * result in this test being false, so we go in and take the locks.	 */	if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {		JBUFFER_TRACE(jh, "fastpath");		J_ASSERT_JH(jh, jh->b_transaction ==					journal->j_running_transaction);		goto out_unlock_bh;	}	set_buffer_jbddirty(bh);	/*	 * 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. */		goto out_unlock_bh;	}	/* That test should have eliminated the following case: */	J_ASSERT_JH(jh, jh->b_frozen_data == NULL);	JBUFFER_TRACE(jh, "file as BJ_Metadata");	spin_lock(&journal->j_list_lock);	__journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);	spin_unlock(&journal->j_list_lock);out_unlock_bh:	jbd_unlock_bh_state(bh);out:	JBUFFER_TRACE(jh, "exit");	return 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. * */voidjournal_release_buffer(handle_t *handle, struct buffer_head *bh){	BUFFER_TRACE(bh, "entry");}/** * void journal_forget() - bforget() for potentially-journaled buffers. * @handle: transaction handle * @bh:     bh to 'forget' * * 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. */int 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;	int drop_reserve = 0;	int err = 0;	BUFFER_TRACE(bh, "entry");	jbd_lock_bh_state(bh);	spin_lock(&journal->j_list_lock);	if (!buffer_jbd(bh))		goto not_jbd;	jh = bh2jh(bh);	/* Critical error: attempting to delete a bitmap buffer, maybe?	 * Don't do any jbd operations, and return an error. */	if (!J_EXPECT_JH(jh, !jh->b_committed_data,			 "inconsistent data on disk")) {		err = -EIO;		goto not_jbd;	}	/*	 * The buffer's going from the transaction, we must drop	 * all references -bzzz	 */	jh->b_modified = 0;	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_buffer_dirty(bh);		clear_buffer_jbddirty(bh);		JBUFFER_TRACE(jh, "belongs to current transaction: unfile");		drop_reserve = 1;		/*		 * 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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -