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

📄 checkpoint.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * linux/fs/jbd2/checkpoint.c * * Written by Stephen C. Tweedie <sct@redhat.com>, 1999 * * Copyright 1999 Red Hat Software --- All Rights Reserved * * This file is part of the Linux kernel and is made available under * the terms of the GNU General Public License, version 2, or at your * option, any later version, incorporated herein by reference. * * Checkpoint routines for the generic filesystem journaling code. * Part of the ext2fs journaling system. * * Checkpointing is the process of ensuring that a section of the log is * committed fully to disk, so that that portion of the log can be * reused. */#include <linux/time.h>#include <linux/fs.h>#include <linux/jbd2.h>#include <linux/errno.h>#include <linux/slab.h>/* * Unlink a buffer from a transaction checkpoint list. * * Called with j_list_lock held. */static inline void __buffer_unlink_first(struct journal_head *jh){	transaction_t *transaction = jh->b_cp_transaction;	jh->b_cpnext->b_cpprev = jh->b_cpprev;	jh->b_cpprev->b_cpnext = jh->b_cpnext;	if (transaction->t_checkpoint_list == jh) {		transaction->t_checkpoint_list = jh->b_cpnext;		if (transaction->t_checkpoint_list == jh)			transaction->t_checkpoint_list = NULL;	}}/* * Unlink a buffer from a transaction checkpoint(io) list. * * Called with j_list_lock held. */static inline void __buffer_unlink(struct journal_head *jh){	transaction_t *transaction = jh->b_cp_transaction;	__buffer_unlink_first(jh);	if (transaction->t_checkpoint_io_list == jh) {		transaction->t_checkpoint_io_list = jh->b_cpnext;		if (transaction->t_checkpoint_io_list == jh)			transaction->t_checkpoint_io_list = NULL;	}}/* * Move a buffer from the checkpoint list to the checkpoint io list * * Called with j_list_lock held */static inline void __buffer_relink_io(struct journal_head *jh){	transaction_t *transaction = jh->b_cp_transaction;	__buffer_unlink_first(jh);	if (!transaction->t_checkpoint_io_list) {		jh->b_cpnext = jh->b_cpprev = jh;	} else {		jh->b_cpnext = transaction->t_checkpoint_io_list;		jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;		jh->b_cpprev->b_cpnext = jh;		jh->b_cpnext->b_cpprev = jh;	}	transaction->t_checkpoint_io_list = jh;}/* * Try to release a checkpointed buffer from its transaction. * Returns 1 if we released it and 2 if we also released the * whole transaction. * * Requires j_list_lock * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it */static int __try_to_free_cp_buf(struct journal_head *jh){	int ret = 0;	struct buffer_head *bh = jh2bh(jh);	if (jh->b_jlist == BJ_None && !buffer_locked(bh) && !buffer_dirty(bh)) {		JBUFFER_TRACE(jh, "remove from checkpoint list");		ret = __jbd2_journal_remove_checkpoint(jh) + 1;		jbd_unlock_bh_state(bh);		jbd2_journal_remove_journal_head(bh);		BUFFER_TRACE(bh, "release");		__brelse(bh);	} else {		jbd_unlock_bh_state(bh);	}	return ret;}/* * __jbd2_log_wait_for_space: wait until there is space in the journal. * * Called under j-state_lock *only*.  It will be unlocked if we have to wait * for a checkpoint to free up some space in the log. */void __jbd2_log_wait_for_space(journal_t *journal){	int nblocks;	assert_spin_locked(&journal->j_state_lock);	nblocks = jbd_space_needed(journal);	while (__jbd2_log_space_left(journal) < nblocks) {		if (journal->j_flags & JBD2_ABORT)			return;		spin_unlock(&journal->j_state_lock);		mutex_lock(&journal->j_checkpoint_mutex);		/*		 * Test again, another process may have checkpointed while we		 * were waiting for the checkpoint lock		 */		spin_lock(&journal->j_state_lock);		nblocks = jbd_space_needed(journal);		if (__jbd2_log_space_left(journal) < nblocks) {			spin_unlock(&journal->j_state_lock);			jbd2_log_do_checkpoint(journal);			spin_lock(&journal->j_state_lock);		}		mutex_unlock(&journal->j_checkpoint_mutex);	}}/* * We were unable to perform jbd_trylock_bh_state() inside j_list_lock. * The caller must restart a list walk.  Wait for someone else to run * jbd_unlock_bh_state(). */static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)	__releases(journal->j_list_lock){	get_bh(bh);	spin_unlock(&journal->j_list_lock);	jbd_lock_bh_state(bh);	jbd_unlock_bh_state(bh);	put_bh(bh);}/* * Clean up transaction's list of buffers submitted for io. * We wait for any pending IO to complete and remove any clean * buffers. Note that we take the buffers in the opposite ordering * from the one in which they were submitted for IO. * * Called with j_list_lock held. */static void __wait_cp_io(journal_t *journal, transaction_t *transaction){	struct journal_head *jh;	struct buffer_head *bh;	tid_t this_tid;	int released = 0;	this_tid = transaction->t_tid;restart:	/* Did somebody clean up the transaction in the meanwhile? */	if (journal->j_checkpoint_transactions != transaction ||			transaction->t_tid != this_tid)		return;	while (!released && transaction->t_checkpoint_io_list) {		jh = transaction->t_checkpoint_io_list;		bh = jh2bh(jh);		if (!jbd_trylock_bh_state(bh)) {			jbd_sync_bh(journal, bh);			spin_lock(&journal->j_list_lock);			goto restart;		}		if (buffer_locked(bh)) {			atomic_inc(&bh->b_count);			spin_unlock(&journal->j_list_lock);			jbd_unlock_bh_state(bh);			wait_on_buffer(bh);			/* the journal_head may have gone by now */			BUFFER_TRACE(bh, "brelse");			__brelse(bh);			spin_lock(&journal->j_list_lock);			goto restart;		}		/*		 * Now in whatever state the buffer currently is, we know that		 * it has been written out and so we can drop it from the list		 */		released = __jbd2_journal_remove_checkpoint(jh);		jbd_unlock_bh_state(bh);		jbd2_journal_remove_journal_head(bh);		__brelse(bh);	}}#define NR_BATCH	64static void__flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count){	int i;	ll_rw_block(SWRITE, *batch_count, bhs);	for (i = 0; i < *batch_count; i++) {		struct buffer_head *bh = bhs[i];		clear_buffer_jwrite(bh);		BUFFER_TRACE(bh, "brelse");		__brelse(bh);	}	*batch_count = 0;}/* * Try to flush one buffer from the checkpoint list to disk. * * Return 1 if something happened which requires us to abort the current * scan of the checkpoint list. * * Called with j_list_lock held and drops it if 1 is returned * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it */static int __process_buffer(journal_t *journal, struct journal_head *jh,			struct buffer_head **bhs, int *batch_count){	struct buffer_head *bh = jh2bh(jh);	int ret = 0;	if (buffer_locked(bh)) {		atomic_inc(&bh->b_count);		spin_unlock(&journal->j_list_lock);		jbd_unlock_bh_state(bh);		wait_on_buffer(bh);		/* the journal_head may have gone by now */		BUFFER_TRACE(bh, "brelse");		__brelse(bh);		ret = 1;	} else if (jh->b_transaction != NULL) {		transaction_t *t = jh->b_transaction;		tid_t tid = t->t_tid;		spin_unlock(&journal->j_list_lock);		jbd_unlock_bh_state(bh);		jbd2_log_start_commit(journal, tid);		jbd2_log_wait_commit(journal, tid);		ret = 1;	} else if (!buffer_dirty(bh)) {		J_ASSERT_JH(jh, !buffer_jbddirty(bh));		BUFFER_TRACE(bh, "remove from checkpoint");		__jbd2_journal_remove_checkpoint(jh);		spin_unlock(&journal->j_list_lock);		jbd_unlock_bh_state(bh);		jbd2_journal_remove_journal_head(bh);		__brelse(bh);		ret = 1;	} else {		/*		 * Important: we are about to write the buffer, and		 * possibly block, while still holding the journal lock.		 * We cannot afford to let the transaction logic start		 * messing around with this buffer before we write it to		 * disk, as that would break recoverability.		 */		BUFFER_TRACE(bh, "queue");		get_bh(bh);		J_ASSERT_BH(bh, !buffer_jwrite(bh));		set_buffer_jwrite(bh);		bhs[*batch_count] = bh;		__buffer_relink_io(jh);		jbd_unlock_bh_state(bh);		(*batch_count)++;		if (*batch_count == NR_BATCH) {			spin_unlock(&journal->j_list_lock);			__flush_batch(journal, bhs, batch_count);			ret = 1;		}	}	return ret;}/* * Perform an actual checkpoint. We take the first transaction on the * list of transactions to be checkpointed and send all its buffers * to disk. We submit larger chunks of data at once. * * The journal should be locked before calling this function. */int jbd2_log_do_checkpoint(journal_t *journal){	transaction_t *transaction;	tid_t this_tid;	int result;	jbd_debug(1, "Start checkpoint\n");	/*	 * First thing: if there are any transactions in the log which	 * don't need checkpointing, just eliminate them from the	 * journal straight away.	 */	result = jbd2_cleanup_journal_tail(journal);	jbd_debug(1, "cleanup_journal_tail returned %d\n", result);	if (result <= 0)		return result;	/*	 * OK, we need to start writing disk blocks.  Take one transaction	 * and write it.	 */	spin_lock(&journal->j_list_lock);	if (!journal->j_checkpoint_transactions)		goto out;	transaction = journal->j_checkpoint_transactions;	this_tid = transaction->t_tid;restart:	/*	 * If someone cleaned up this transaction while we slept, we're	 * done (maybe it's a new transaction, but it fell at the same	 * address).	 */	if (journal->j_checkpoint_transactions == transaction &&			transaction->t_tid == this_tid) {		int batch_count = 0;		struct buffer_head *bhs[NR_BATCH];		struct journal_head *jh;		int retry = 0;		while (!retry && transaction->t_checkpoint_list) {			struct buffer_head *bh;			jh = transaction->t_checkpoint_list;			bh = jh2bh(jh);			if (!jbd_trylock_bh_state(bh)) {				jbd_sync_bh(journal, bh);				retry = 1;				break;			}			retry = __process_buffer(journal, jh, bhs,&batch_count);

⌨️ 快捷键说明

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