mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: hugang <hugang@soulinfo.com>
To: linux-kernel@vger.kernel.org
Subject: back port 2.5.65-mm4 ext3 patch to 2.4.20
Date: Mon, 24 Mar 2003 20:55:02 +0800	[thread overview]
Message-ID: <20030324205502.7d4b015d.hugang@soulinfo.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 227 bytes --]

Hello:

This patch base on Andrew Morton's 2.5.65-mm4. 

-- 
Hu Gang / Steve
Email        : huagng@soulinfo.com, linuxbest@sina.com
GPG FinePrint: 4099 3F1D AE01 1817 68F7  D499 A6C2 C418 86C8 610E
Registered Linux User 204016

[-- Attachment #2: 2_4_20_ext3_orlov_no_blk.patch --]
[-- Type: application/octet-stream, Size: 33495 bytes --]

diff -ru ./fs/ext3/ialloc.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/ialloc.c
--- ./fs/ext3/ialloc.c	Mon Mar 24 20:27:58 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/ialloc.c	Sun Dec  1 12:43:55 2002
@@ -21,6 +21,7 @@
 #include <linux/string.h>
 #include <linux/locks.h>
 #include <linux/quotaops.h>
+#include <linux/random.h>
 
 #include <asm/bitops.h>
 #include <asm/byteorder.h>
@@ -262,9 +263,11 @@
 		if (gdp) {
 			gdp->bg_free_inodes_count = cpu_to_le16(
 				le16_to_cpu(gdp->bg_free_inodes_count) + 1);
-			if (is_directory)
+			if (is_directory) {
 				gdp->bg_used_dirs_count = cpu_to_le16(
 				  le16_to_cpu(gdp->bg_used_dirs_count) - 1);
+				EXT3_SB(sb)->s_dir_count--;
+			}
 		}
 		BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
 		err = ext3_journal_dirty_metadata(handle, bh2);
@@ -293,20 +296,228 @@
  * the groups with above-average free space, that group with the fewest
  * directories already is chosen.
  *
+ * For other inodes, search forward from the parent directory\'s block
+ * group to find a free inode.
+ */
+static int find_group_dir(struct super_block *sb, struct inode *parent)
+{
+	struct ext3_super_block * es = EXT3_SB(sb)->s_es;
+	int ngroups = EXT3_SB(sb)->s_groups_count;
+	int avefreei = le32_to_cpu(es->s_free_inodes_count) / ngroups;
+	struct ext3_group_desc *desc, *best_desc = NULL;
+	struct buffer_head *bh, *best_bh = NULL;
+	int group, best_group = -1;
+
+	for (group = 0; group < ngroups; group++) {
+		desc = ext3_get_group_desc (sb, group, &bh);
+		if (!desc || !desc->bg_free_inodes_count)
+			continue;
+		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
+			continue;
+		if (!best_desc || 
+		    (le16_to_cpu(desc->bg_free_blocks_count) >
+		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
+			best_group = group;
+			best_desc = desc;
+			best_bh = bh;
+		}
+	}
+	if (!best_desc)
+		return -1;
+	return best_group;
+}
+
+/* 
+ * Orlov's allocator for directories.
+ * 
+ * We always try to spread first-level directories.
+ * 
+ * If there are blockgroups with both free inodes and free blocks counts
+ * not worse than average we return one with smallest directory count.
+ * Otherwise we simply return a random group.
+ * 
+ * For the rest rules look so:
+ * 
+ * It's OK to put directory into a group unless 
+ * it has too many directories already (max_dirs) or
+ * it has too few free inodes left (min_inodes) or
+ * it has too few free blocks left (min_blocks) or
+ * it's already running too large debt (max_debt).
+ * Parent's group is prefered, if it doesn't satisfy these
+ * conditions we search cyclically through the rest. If none
+ * of the groups look good we just look for a group with more
+ * free inodes than average (starting at parent's group).
+ *
+ * Debt is incremented each time we allocate a directory and decremented
+ * when we allocate an inode, within 0--255.
+ */
+
+#define INODE_COST 64
+#define BLOCK_COST 256
+
+static int find_group_orlov(struct super_block *sb, const struct inode *parent)
+{
+	int parent_group = EXT3_I(parent)->i_block_group;
+	struct ext3_sb_info *sbi = EXT3_SB(sb);
+	struct ext3_super_block *es = sbi->s_es;
+	int ngroups = sbi->s_groups_count;
+	int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
+	int avefreei = le32_to_cpu(es->s_free_inodes_count) / ngroups;
+	int avefreeb = le32_to_cpu(es->s_free_blocks_count) / ngroups;
+	int blocks_per_dir;
+	int ndirs = sbi->s_dir_count;
+	int max_debt, max_dirs, min_blocks, min_inodes;
+	int group = -1, i;
+	struct ext3_group_desc *desc;
+	struct buffer_head *bh;
+
+	if ((parent == sb->s_root->d_inode) ||
+	    (parent->i_flags & EXT3_TOPDIR_FL)) {
+		struct ext3_group_desc *best_desc = NULL;
+		struct buffer_head *best_bh = NULL;
+		int best_ndir = inodes_per_group;
+		int best_group = -1;
+		
+		get_random_bytes(&group, sizeof(group));
+		parent_group = (unsigned)group % ngroups;
+		for (i = 0; i < ngroups; i++) {
+			group = (parent_group + i) % ngroups;
+			desc = ext3_get_group_desc (sb, group, &bh);
+			if (!desc || !desc->bg_free_inodes_count)
+				continue;
+			if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
+				continue;
+			if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
+				continue;
+			if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
+				continue;
+			best_group = group;
+			best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
+			best_desc = desc;
+			best_bh = bh;
+		}
+		if (best_group >= 0) {
+			desc = best_desc;
+			bh = best_bh;
+			group = best_group;
+			goto found;
+		}
+		goto fallback;
+	}
+
+	blocks_per_dir = (le32_to_cpu(es->s_blocks_count) -
+			  le32_to_cpu(es->s_free_blocks_count)) / ndirs;
+
+	max_dirs = ndirs / ngroups + inodes_per_group / 16;
+	min_inodes = avefreei - inodes_per_group / 4;
+	min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
+
+	max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
+	if (max_debt * INODE_COST > inodes_per_group)
+		max_debt = inodes_per_group / INODE_COST;
+	if (max_debt > 255)
+		max_debt = 255;
+	if (max_debt == 0)
+		max_debt = 1;
+
+	for (i = 0; i < ngroups; i++) {
+		group = (parent_group + i) % ngroups;
+		desc = ext3_get_group_desc (sb, group, &bh);
+		if (!desc || !desc->bg_free_inodes_count)
+			continue;
+		if (sbi->s_debts[group] >= max_debt)
+			continue;
+		if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
+			continue;
+		if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
+			continue;
+		if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
+			continue;
+		goto found;
+	}
+
+ fallback:
+	for (i = 0; i < ngroups; i++) {
+		group = (parent_group + i) % ngroups;
+		desc = ext3_get_group_desc (sb, group, &bh);
+		if (!desc || !desc->bg_free_inodes_count)
+			continue;
+		if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
+			goto found;
+	}
+
+	return -1;
+ found:
+	return group;
+}
+
+static int find_group_other(struct super_block *sb, struct inode *parent)
+{
+	int parent_group = EXT3_I(parent)->i_block_group;
+	int ngroups = EXT3_SB(sb)->s_groups_count;
+	struct ext3_group_desc *desc;
+	struct buffer_head *bh;
+	int group, i;
+	
+	/*
+	 * Try to place the inode in its parent directory
+	 */
+	group = parent_group;
+	desc = ext3_get_group_desc (sb, group, &bh);
+	if (desc && le16_to_cpu(desc->bg_free_inodes_count))
+		goto found;
+
+	/*
+	 * Use a quadratic hash to find a group with a
+	 * free inode
+	 */
+	for (i = 1; i < ngroups; i <<= 1) {
+		group += i;
+		if (group >= ngroups)
+			group -= ngroups;
+		desc = ext3_get_group_desc (sb, group, &bh);
+		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
+			goto found;
+	}
+
+	/*
+	 * That failed: try linear search for a free inode
+	 */
+	group = parent_group + 1;
+	for (i = 2; i < ngroups; i++) {
+		if (++group >= ngroups)
+			group = 0;
+		desc = ext3_get_group_desc (sb, group, &bh);
+		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
+			goto found;
+	}
+	
+	return -1;
+	
+ found:
+	return group;
+}
+
+/*
+ * There are two policies for allocating an inode.  If the new inode is
+ * a directory, then a forward search is made for a block group with both
+ * free space and a low directory-to-inode ratio; if that fails, then of
+ * the groups with above-average free space, that group with the fewest
+ * directories already is chosen.
+ *
  * For other inodes, search forward from the parent directory's block
  * group to find a free inode.
  */
-struct inode * ext3_new_inode (handle_t *handle,
-				const struct inode * dir, int mode)
+struct inode * ext3_new_inode (handle_t *handle, struct inode * dir, int mode)
 {
 	struct super_block * sb;
 	struct buffer_head * bh;
 	struct buffer_head * bh2;
-	int i, j, avefreei;
-	struct inode * inode;
+	int group;
+	ino_t ino;
 	int bitmap_nr;
+	struct inode * inode;
 	struct ext3_group_desc * gdp;
-	struct ext3_group_desc * tmp;
 	struct ext3_super_block * es;
 	int err = 0;
 
@@ -323,94 +534,36 @@
 	lock_super (sb);
 	es = sb->u.ext3_sb.s_es;
 repeat:
-	gdp = NULL;
-	i = 0;
-
 	if (S_ISDIR(mode)) {
-		avefreei = le32_to_cpu(es->s_free_inodes_count) /
-			sb->u.ext3_sb.s_groups_count;
-		if (!gdp) {
-			for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
-				struct buffer_head *temp_buffer;
-				tmp = ext3_get_group_desc (sb, j, &temp_buffer);
-				if (tmp &&
-				    le16_to_cpu(tmp->bg_free_inodes_count) &&
-				    le16_to_cpu(tmp->bg_free_inodes_count) >=
-							avefreei) {
-					if (!gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
-						le16_to_cpu(gdp->bg_free_blocks_count))) {
-						i = j;
-						gdp = tmp;
-						bh2 = temp_buffer;
-					}
-				}
-			}
-		}
-	} else {
-		/*
-		 * Try to place the inode in its parent directory
-		 */
-		i = dir->u.ext3_i.i_block_group;
-		tmp = ext3_get_group_desc (sb, i, &bh2);
-		if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
-			gdp = tmp;
+		if (test_opt (sb, OLDALLOC))
+			group = find_group_dir(sb, dir);
 		else
-		{
-			/*
-			 * Use a quadratic hash to find a group with a
-			 * free inode
-			 */
-			for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
-				i += j;
-				if (i >= sb->u.ext3_sb.s_groups_count)
-					i -= sb->u.ext3_sb.s_groups_count;
-				tmp = ext3_get_group_desc (sb, i, &bh2);
-				if (tmp &&
-				    le16_to_cpu(tmp->bg_free_inodes_count)) {
-					gdp = tmp;
-					break;
-				}
-			}
-		}
-		if (!gdp) {
-			/*
-			 * That failed: try linear search for a free inode
-			 */
-			i = dir->u.ext3_i.i_block_group + 1;
-			for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
-				if (++i >= sb->u.ext3_sb.s_groups_count)
-					i = 0;
-				tmp = ext3_get_group_desc (sb, i, &bh2);
-				if (tmp &&
-				    le16_to_cpu(tmp->bg_free_inodes_count)) {
-					gdp = tmp;
-					break;
-				}
-			}
-		}
-	}
+			group = find_group_orlov(sb, dir);
+	} else 
+		group = find_group_other(sb, dir);
 
 	err = -ENOSPC;
-	if (!gdp)
+	if (group == -1)
 		goto out;
 
 	err = -EIO;
-	bitmap_nr = load_inode_bitmap (sb, i);
+	bitmap_nr = load_inode_bitmap (sb, group);
 	if (bitmap_nr < 0)
 		goto fail;
 
+	gdp = ext3_get_group_desc (sb, group, &bh2);
 	bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
 
-	if ((j = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
+	if ((ino = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
 				      EXT3_INODES_PER_GROUP(sb))) <
 	    EXT3_INODES_PER_GROUP(sb)) {
 		BUFFER_TRACE(bh, "get_write_access");
 		err = ext3_journal_get_write_access(handle, bh);
 		if (err) goto fail;
 		
-		if (ext3_set_bit (j, bh->b_data)) {
+		if (ext3_set_bit (ino, bh->b_data)) {
 			ext3_error (sb, "ext3_new_inode",
-				      "bit already set for inode %d", j);
+				      "bit already set for inode %lu", ino);
 			goto repeat;
 		}
 		BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
@@ -420,7 +573,7 @@
 		if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
 			ext3_error (sb, "ext3_new_inode",
 				    "Free inodes count corrupted in group %d",
-				    i);
+				    group);
 			/* Is it really ENOSPC? */
 			err = -ENOSPC;
 			if (sb->s_flags & MS_RDONLY)
@@ -436,11 +589,11 @@
 		}
 		goto repeat;
 	}
-	j += i * EXT3_INODES_PER_GROUP(sb) + 1;
-	if (j < EXT3_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
+		ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
+		if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
 		ext3_error (sb, "ext3_new_inode",
 			    "reserved inode or inode > inodes count - "
-			    "block_group = %d,inode=%d", i, j);
+			    "block_group = %d,inode=%lu", group, ino);
 		err = -EIO;
 		goto fail;
 	}
@@ -450,9 +603,11 @@
 	if (err) goto fail;
 	gdp->bg_free_inodes_count =
 		cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
-	if (S_ISDIR(mode))
+	if (S_ISDIR(mode)) {
 		gdp->bg_used_dirs_count =
 			cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
+		sb->u.ext3_sb.s_dir_count++;
+	}
 	BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
 	err = ext3_journal_dirty_metadata(handle, bh2);
 	if (err) goto fail;
@@ -478,7 +633,7 @@
 		inode->i_gid = current->fsgid;
 	inode->i_mode = mode;
 
-	inode->i_ino = j;
+	inode->i_ino = ino;
 	/* This is the optimal IO size (for stat), not the fs block size */
 	inode->i_blksize = PAGE_SIZE;
 	inode->i_blocks = 0;
@@ -498,7 +653,7 @@
 #ifdef EXT3_PREALLOCATE
 	inode->u.ext3_i.i_prealloc_count = 0;
 #endif
-	inode->u.ext3_i.i_block_group = i;
+	inode->u.ext3_i.i_block_group = group;
 	
 	if (inode->u.ext3_i.i_flags & EXT3_SYNC_FL)
 		inode->i_flags |= S_SYNC;
@@ -620,6 +775,21 @@
 #else
 	return le32_to_cpu(sb->u.ext3_sb.s_es->s_free_inodes_count);
 #endif
+}
+
+/* Called at mount-time, super-block is locked */
+unsigned long ext3_count_dirs (struct super_block * sb)
+{
+   unsigned long count = 0;
+   int i;
+
+   for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
+       struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
+       if (!gdp)
+           continue;
+       count += le16_to_cpu(gdp->bg_used_dirs_count);
+   }
+   return count;
 }
 
 #ifdef CONFIG_EXT3_CHECK
diff -ru ./fs/ext3/inode.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/inode.c
--- ./fs/ext3/inode.c	Mon Mar 24 20:27:58 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/inode.c	Mon Mar 24 18:34:31 2003
@@ -169,7 +169,6 @@
 	    inode->i_ino == EXT3_ACL_DATA_INO)
 		goto no_delete;
 
-	lock_kernel();
 	handle = start_transaction(inode);
 	if (IS_ERR(handle)) {
 		/* If we're going to skip the normal cleanup, we still
@@ -178,7 +177,6 @@
 		ext3_orphan_del(NULL, inode);
 
 		ext3_std_error(inode->i_sb, PTR_ERR(handle));
-		unlock_kernel();
 		goto no_delete;
 	}
 	
@@ -211,7 +209,6 @@
 	else
 		ext3_free_inode(handle, inode);
 	ext3_journal_stop(handle, inode);
-	unlock_kernel();
 	return;
 no_delete:
 	clear_inode(inode);	/* We must guarantee clearing of inode... */
@@ -220,7 +217,6 @@
 void ext3_discard_prealloc (struct inode * inode)
 {
 #ifdef EXT3_PREALLOCATE
-	lock_kernel();
 	/* Writer: ->i_prealloc* */
 	if (inode->u.ext3_i.i_prealloc_count) {
 		unsigned short total = inode->u.ext3_i.i_prealloc_count;
@@ -230,7 +226,6 @@
 		/* Writer: end */
 		ext3_free_blocks (inode, block, total);
 	}
-	unlock_kernel();
 #endif
 }
 
@@ -735,7 +730,6 @@
 	if (depth == 0)
 		goto out;
 
-	lock_kernel();
 reread:
 	partial = ext3_get_branch(inode, depth, offsets, chain, &err);
 
@@ -760,7 +754,6 @@
 			partial--;
 		}
 		BUFFER_TRACE(bh_result, "returned");
-		unlock_kernel();
 out:
 		return err;
 	}
@@ -867,7 +860,6 @@
 			   For now, regular file writes use
 			   ext3_get_block instead, so it's not a
 			   problem. */
-			lock_kernel();
 			lock_buffer(bh);
 			BUFFER_TRACE(bh, "call get_create_access");
 			fatal = ext3_journal_get_create_access(handle, bh);
@@ -880,7 +872,6 @@
 			BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
 			err = ext3_journal_dirty_metadata(handle, bh);
 			if (!fatal) fatal = err;
-			unlock_kernel();
 		} else {
 			BUFFER_TRACE(bh, "not a new buffer");
 		}
@@ -1019,15 +1010,12 @@
 	int ret, needed_blocks = ext3_writepage_trans_blocks(inode);
 	handle_t *handle;
 
-	lock_kernel();
 	handle = ext3_journal_start(inode, needed_blocks);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		goto out;
 	}
-	unlock_kernel();
 	ret = block_prepare_write(page, from, to, ext3_get_block);
-	lock_kernel();
 	if (ret != 0)
 		goto prepare_write_failed;
 
@@ -1049,7 +1037,6 @@
 	if (ret)
 		ext3_journal_stop(handle, inode);
 out:
-	unlock_kernel();
 	return ret;
 }
 
@@ -1103,7 +1090,6 @@
 	struct inode *inode = page->mapping->host;
 	int ret = 0, ret2;
 
-	lock_kernel();
 	if (ext3_should_journal_data(inode)) {
 		/*
 		 * Here we duplicate the generic_commit_write() functionality
@@ -1145,7 +1131,6 @@
 			ret = ret2;
 	}
 	ret2 = ext3_journal_stop(handle, inode);
-	unlock_kernel();
 	if (!ret)
 		ret = ret2;
 	return ret;
@@ -1275,7 +1260,6 @@
 	 * for a different filesystem.  One *could* look for a
 	 * nested transaction opportunity.
 	 */
-	lock_kernel();
 	if (ext3_journal_current_handle())
 		goto out_fail;
 
@@ -1293,8 +1277,6 @@
 	order_data = ext3_should_order_data(inode) ||
 			ext3_should_journal_data(inode);
 
-	unlock_kernel();
-
 	page_buffers = NULL;	/* Purely to prevent compiler warning */
 
 	/* bget() all the buffers */
@@ -1317,7 +1299,6 @@
 	 */
 
 	handle = ext3_journal_current_handle();
-	lock_kernel();
 
 	/* And attach them to the current transaction */
 	if (order_data) {
@@ -1330,12 +1311,10 @@
 	err = ext3_journal_stop(handle, inode);
 	if (!ret)
 		ret = err;
-	unlock_kernel();
 	return ret;
 
 out_fail:
 	
-	unlock_kernel();
 	SetPageDirty(page);
 	UnlockPage(page);
 	return ret;
@@ -2552,7 +2531,6 @@
 	handle_t *current_handle = ext3_journal_current_handle();
 	handle_t *handle;
 
-	lock_kernel();
 	handle = ext3_journal_start(inode, 1);
 	if (IS_ERR(handle))
 		goto out;
@@ -2568,7 +2546,7 @@
 	}
 	ext3_journal_stop(handle, inode);
 out:
-	unlock_kernel();
+	return;
 }
 
 #ifdef AKPM
diff -ru ./fs/ext3/super.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/super.c
--- ./fs/ext3/super.c	Mon Mar 24 20:27:58 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/ext3/super.c	Mon Mar 24 18:33:08 2003
@@ -416,6 +416,7 @@
 	for (i = 0; i < sbi->s_gdb_count; i++)
 		brelse(sbi->s_group_desc[i]);
 	kfree(sbi->s_group_desc);
+	kfree(sbi->s_debts);
 	for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
 		brelse(sbi->s_inode_bitmap[i]);
 	for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
@@ -582,6 +583,10 @@
 			if (want_numeric(value, "sb", sb_block))
 				return 0;
 		}
+		else if (!strcmp (this_char, "oldalloc"))
+			set_opt (sbi->s_mount_opt, OLDALLOC);
+		else if (!strcmp (this_char, "orlov"))
+			clear_opt (sbi->s_mount_opt, OLDALLOC);
 #ifdef CONFIG_JBD_DEBUG
 		else if (!strcmp (this_char, "ro-after")) {
 			unsigned long v;
@@ -1098,6 +1103,13 @@
 		printk (KERN_ERR "EXT3-fs: not enough memory\n");
 		goto failed_mount;
 	}
+	sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
+			GFP_KERNEL);
+	if (!sbi->s_debts) {
+		printk ("EXT3-fs: not enough memory\n");
+		goto failed_mount2;
+	}
+	memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
 	for (i = 0; i < db_count; i++) {
 		sbi->s_group_desc[i] = sb_bread(sb, logic_sb_block + i + 1);
 		if (!sbi->s_group_desc[i]) {
@@ -1120,6 +1132,7 @@
 	sbi->s_loaded_inode_bitmaps = 0;
 	sbi->s_loaded_block_bitmaps = 0;
 	sbi->s_gdb_count = db_count;
+	sbi->s_dir_count = ext3_count_dirs(sb);
 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
 	/*
 	 * set up enough so that it can read an inode
@@ -1223,6 +1236,8 @@
 failed_mount3:
 	journal_destroy(sbi->s_journal);
 failed_mount2:
+	if (sbi->s_debts)
+		kfree(sbi->s_debts);
 	for (i = 0; i < db_count; i++)
 		brelse(sbi->s_group_desc[i]);
 	kfree(sbi->s_group_desc);
@@ -1562,9 +1577,7 @@
 
 	journal = EXT3_SB(sb)->s_journal;
 	sb->s_dirt = 0;
-	lock_kernel();	/* important: lock down j_running_transaction */
 	ret = ext3_journal_force_commit(journal);
-	unlock_kernel();
 	return ret;
 }
 
@@ -1605,7 +1618,6 @@
 {
 	sb->s_dirt = 0;
 
-	lock_kernel();		/* 2.4.5 forgot to do this for us */
 	if (!(sb->s_flags & MS_RDONLY)) {
 		journal_t *journal = EXT3_SB(sb)->s_journal;
 
@@ -1619,7 +1631,6 @@
 		EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
 		ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
 	}
-	unlock_kernel();
 }
 
 /*
@@ -1629,14 +1640,12 @@
 void ext3_unlockfs(struct super_block *sb)
 {
 	if (!(sb->s_flags & MS_RDONLY)) {
-		lock_kernel();
 		lock_super(sb);
 		/* Reser the needs_recovery flag before the fs is unlocked. */
 		EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
 		ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
 		unlock_super(sb);
 		journal_unlock_updates(EXT3_SB(sb)->s_journal);
-		unlock_kernel();
 	}
 }
 
diff -ru ./fs/jbd/commit.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/commit.c
--- ./fs/jbd/commit.c	Mon Mar 24 20:28:00 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/commit.c	Mon Mar 24 18:49:31 2003
@@ -19,6 +19,8 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/locks.h>
+#include <linux/mm.h>
+#include <linux/pagemap.h>
 #include <linux/smp_lock.h>
 
 extern spinlock_t journal_datalist_lock;
@@ -34,6 +36,49 @@
 }
 
 /*
+ * When an ext3-ordered file is truncated, it is possible that many pages are
+ * not sucessfully freed, because they are attached to a committing transaction.
+ * After the transaction commits, these pages are left on the LRU, with no
+ * ->mapping, and with attached buffers.  These pages are trivially reclaimable
+ * by the VM, but their apparent absence upsets the VM accounting, and it makes
+ * the numbers in /proc/meminfo look odd.
+ *
+ * So here, we have a buffer which has just come off the forget list.  Look to
+ * see if we can strip all buffers from the backing page.
+ *
+ * Called under lock_journal(), and possibly under journal_datalist_lock.  The
+ * caller provided us with a ref against the buffer, and we drop that here.
+ */
+static void release_buffer_page(struct buffer_head *bh)
+{
+	struct page *page;
+
+	if (buffer_dirty(bh))
+		goto nope;
+	if (atomic_read(&bh->b_count) != 1)
+		goto nope;
+	page = bh->b_page;
+	if (!page)
+		goto nope;
+	if (page->mapping)
+		goto nope;
+
+	/* OK, it's a truncated page */
+	if (TryLockPage(page))
+		goto nope;
+
+	page_cache_get(page);
+	__brelse(bh);
+	try_to_free_buffers(page, GFP_NOFS);
+	unlock_page(page);
+	page_cache_release(page);
+	return;
+
+nope:
+	__brelse(bh);
+}
+
+/*
  * journal_commit_transaction
  *
  * The primary function for committing a transaction to the log.  This
@@ -211,7 +256,7 @@
 				jh->b_transaction = NULL;
 				__journal_remove_journal_head(bh);
 				refile_buffer(bh);
-				__brelse(bh);
+				release_buffer_page(bh);
 			}
 		}
 		if (bufs == ARRAY_SIZE(wbuf)) {
@@ -709,7 +754,9 @@
 			__journal_unfile_buffer(jh);
 			jh->b_transaction = 0;
 			__journal_remove_journal_head(bh);
-			__brelse(bh);
+			spin_unlock(&journal_datalist_lock);
+			release_buffer_page(bh);
+			continue;
 		}
 		spin_unlock(&journal_datalist_lock);
 	}
diff -ru ./fs/jbd/journal.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/journal.c
--- ./fs/jbd/journal.c	Mon Mar 24 20:28:00 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/journal.c	Mon Mar 24 18:33:09 2003
@@ -539,9 +539,11 @@
  */
 tid_t log_start_commit (journal_t *journal, transaction_t *transaction)
 {
-	tid_t target = journal->j_commit_request;
+	tid_t target;
 
 	lock_kernel(); /* Protect journal->j_running_transaction */
+
+	target = journal->j_commit_request;
 	
 	/*
 	 * A NULL transaction asks us to commit the currently running
@@ -943,10 +945,12 @@
 	 * any future commit will have to be careful to update the
 	 * superblock again to re-record the true start of the log. */
 
+	lock_kernel();
 	if (sb->s_start)
 		journal->j_flags &= ~JFS_FLUSHED;
 	else
 		journal->j_flags |= JFS_FLUSHED;
+	unlock_kernel();
 }
 
 
@@ -1422,10 +1426,12 @@
 	printk (KERN_ERR "Aborting journal on device %s.\n",
 		journal_dev_name(journal));
 
+	lock_kernel();
 	journal->j_flags |= JFS_ABORT;
 	transaction = journal->j_running_transaction;
 	if (transaction)
 		log_start_commit(journal, transaction);
+	unlock_kernel();
 }
 
 /* Soft abort: record the abort error status in the journal superblock,
@@ -1435,6 +1441,7 @@
 	if (journal->j_flags & JFS_ABORT)
 		return;
 
+	lock_kernel();
 	if (!journal->j_errno)
 		journal->j_errno = errno;
 
@@ -1442,6 +1449,7 @@
 
 	if (errno)
 		journal_update_superblock(journal, 1);
+	unlock_kernel();
 }
 
 /* Full version for external use */
@@ -1457,10 +1465,12 @@
 	int err;
 
 	lock_journal(journal);
+	lock_kernel();
 	if (journal->j_flags & JFS_ABORT)
 		err = -EROFS;
 	else
 		err = journal->j_errno;
+	unlock_kernel();
 	unlock_journal(journal);
 	return err;
 }
@@ -1470,10 +1480,12 @@
 	int err = 0;
 
 	lock_journal(journal);
+	lock_kernel();
 	if (journal->j_flags & JFS_ABORT)
 		err = -EROFS;
 	else
 		journal->j_errno = 0;
+	unlock_kernel();
 	unlock_journal(journal);
 	return err;
 }
@@ -1481,8 +1493,10 @@
 void journal_ack_err (journal_t *journal)
 {
 	lock_journal(journal);
+	lock_kernel();
 	if (journal->j_errno)
 		journal->j_flags |= JFS_ACK_ERR;
+	unlock_kernel();
 	unlock_journal(journal);
 }
 
@@ -1867,6 +1881,29 @@
 
 #endif
 
+kmem_cache_t *jbd_handle_cache;
+
+static int __init journal_init_handle_cache(void)
+{
+	jbd_handle_cache = kmem_cache_create("journal_handle",
+				sizeof(handle_t),
+				0,		/* offset */
+				0,		/* flags */
+				NULL,		/* ctor */
+				NULL);		/* dtor */
+	if (jbd_handle_cache == NULL) {
+		printk(KERN_EMERG "JBD: failed to create handle cache\n");
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static void journal_destroy_handle_cache(void)
+{
+	if (jbd_handle_cache)
+		kmem_cache_destroy(jbd_handle_cache);
+}
+
 /*
  * Module startup and shutdown
  */
@@ -1878,6 +1915,8 @@
 	ret = journal_init_revoke_caches();
 	if (ret == 0)
 		ret = journal_init_journal_head_cache();
+	if (ret == 0)
+		ret = journal_init_handle_cache();
 	return ret;
 }
 
@@ -1885,6 +1924,7 @@
 {
 	journal_destroy_revoke_caches();
 	journal_destroy_journal_head_cache();
+	journal_destroy_handle_cache();
 }
 
 static int __init journal_init(void)
diff -ru ./fs/jbd/transaction.c /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/transaction.c
--- ./fs/jbd/transaction.c	Mon Mar 24 20:28:00 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/fs/jbd/transaction.c	Mon Mar 24 18:33:09 2003
@@ -212,10 +212,10 @@
 /* Allocate a new handle.  This should probably be in a slab... */
 static handle_t *new_handle(int nblocks)
 {
-	handle_t *handle = jbd_kmalloc(sizeof (handle_t), GFP_NOFS);
+	handle_t *handle = jbd_alloc_handle(GFP_NOFS);
 	if (!handle)
 		return NULL;
-	memset(handle, 0, sizeof (handle_t));
+	memset(handle, 0, sizeof(*handle));
 	handle->h_buffer_credits = nblocks;
 	handle->h_ref = 1;
 	INIT_LIST_HEAD(&handle->h_jcb);
@@ -256,13 +256,14 @@
 
 	current->journal_info = handle;
 
+	lock_kernel();
 	err = start_this_handle(journal, handle);
+	unlock_kernel();
 	if (err < 0) {
-		kfree(handle);
+		jbd_free_handle(handle);
 		current->journal_info = NULL;
-		return ERR_PTR(err);
+		handle = ERR_PTR(err);
 	}
-
 	return handle;
 }
 
@@ -409,19 +410,20 @@
 			  "transaction not running\n", handle, nblocks);
 		goto error_out;
 	}
-	
+
+	lock_kernel();	
 	wanted = transaction->t_outstanding_credits + nblocks;
 	
 	if (wanted > journal->j_max_transaction_buffers) {
 		jbd_debug(3, "denied handle %p %d blocks: "
 			  "transaction too large\n", handle, nblocks);
-		goto error_out;
+		goto unlock;
 	}
 
 	if (wanted > log_space_left(journal)) {
 		jbd_debug(3, "denied handle %p %d blocks: "
 			  "insufficient log space\n", handle, nblocks);
-		goto error_out;
+		goto unlock;
 	}
 	
 	handle->h_buffer_credits += nblocks;
@@ -429,7 +431,8 @@
 	result = 0;
 
 	jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
-	
+unlock:
+	unlock_kernel();
 error_out:
 	unlock_journal (journal);
 	return result;
@@ -464,6 +467,7 @@
 	J_ASSERT (transaction->t_updates > 0);
 	J_ASSERT (journal_current_handle() == handle);
 
+	lock_kernel();
 	transaction->t_outstanding_credits -= handle->h_buffer_credits;
 	transaction->t_updates--;
 
@@ -475,6 +479,7 @@
 
 	handle->h_buffer_credits = nblocks;
 	ret = start_this_handle(journal, handle);
+	unlock_kernel();
 	return ret;
 }
 
@@ -492,7 +497,10 @@
 void journal_lock_updates (journal_t *journal)
 {
 	lock_journal(journal);
+
+	lock_kernel();
 	++journal->j_barrier_count;
+	unlock_kernel();
 
 	/* Wait until there are no running updates */
 	while (1) {
@@ -529,7 +537,9 @@
 	J_ASSERT (journal->j_barrier_count != 0);
 	
 	up(&journal->j_barrier);
+	lock_kernel();
 	--journal->j_barrier_count;
+	unlock_kernel();
 	wake_up(&journal->j_wait_transaction_locked);
 	unlock_journal(journal);
 }
@@ -767,7 +777,9 @@
 	 * log thread also manipulates.  Make sure that the buffer
 	 * completes any outstanding IO before proceeding. */
 	lock_journal(journal);
+	lock_kernel();
 	rc = do_get_write_access(handle, jh, 0);
+	unlock_kernel();
 	journal_unlock_journal_head(jh);
 	unlock_journal(journal);
 	return rc;
@@ -838,8 +850,10 @@
 	 * which hits an assertion error.
 	 */
 	JBUFFER_TRACE(jh, "cancelling revoke");
+	lock_kernel();
 	journal_cancel_revoke(handle, jh);
 	journal_unlock_journal_head(jh);
+	unlock_kernel();
 out:
 	unlock_journal(journal);
 	return err;
@@ -884,6 +898,7 @@
 	/* 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. */
+	lock_kernel();
 	err = do_get_write_access (handle, jh, 1);
 	if (err)
 		goto out;
@@ -906,6 +921,7 @@
 	}
 
 out:
+	unlock_kernel();
 	if (!err)
 		J_ASSERT_JH(jh, jh->b_committed_data);
 	journal_unlock_journal_head(jh);
@@ -1128,7 +1144,6 @@
 		/* 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;
 	}
 
@@ -1353,8 +1368,10 @@
 			  void (*func)(struct journal_callback *jcb, int error),
 			  struct journal_callback *jcb)
 {
+	lock_kernel();
 	list_add_tail(&jcb->jcb_list, &handle->h_jcb);
 	jcb->jcb_func = func;
+	unlock_kernel();
 }
 
 /*
@@ -1413,6 +1430,7 @@
 	}
 
 	current->journal_info = NULL;
+	lock_kernel();
 	transaction->t_outstanding_credits -= handle->h_buffer_credits;
 	transaction->t_updates--;
 	if (!transaction->t_updates) {
@@ -1451,7 +1469,8 @@
 		if (handle->h_sync && !(current->flags & PF_MEMALLOC))
 			log_wait_commit(journal, tid);
 	}
-	kfree(handle);
+	unlock_kernel();
+	jbd_free_handle(handle);
 	return err;
 }
 
diff -ru ./include/linux/ext3_fs.h /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/ext3_fs.h
--- ./include/linux/ext3_fs.h	Mon Mar 24 20:28:09 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/ext3_fs.h	Mon Mar 24 18:08:08 2003
@@ -203,11 +203,11 @@
 #define EXT3_INDEX_FL			0x00001000 /* hash-indexed directory */
 #define EXT3_IMAGIC_FL			0x00002000 /* AFS directory */
 #define EXT3_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
+#define EXT3_TOPDIR_FL         0x00020000 /* Top of directory hierarchies*/
 #define EXT3_RESERVED_FL		0x80000000 /* reserved for ext3 lib */
 
-#define EXT3_FL_USER_VISIBLE		0x00005FFF /* User visible flags */
-#define EXT3_FL_USER_MODIFIABLE		0x000000FF /* User modifiable flags */
-
+#define EXT3_FL_USER_VISIBLE       0x0003DFFF /* User visible flags */
+#define EXT3_FL_USER_MODIFIABLE        0x000380FF /* User modifiable flags */
 /*
  * Inode dynamic state flags
  */
@@ -325,6 +325,7 @@
  * Mount flags
  */
 #define EXT3_MOUNT_CHECK		0x0001	/* Do mount-time checks */
+#define EXT3_MOUNT_OLDALLOC        0x0002  /* Don't use the new Orlov allocator */
 #define EXT3_MOUNT_GRPID		0x0004	/* Create files with directory's group */
 #define EXT3_MOUNT_DEBUG		0x0008	/* Some debugging messages */
 #define EXT3_MOUNT_ERRORS_CONT		0x0010	/* Continue on errors */
@@ -607,6 +608,7 @@
 extern void ext3_free_blocks (handle_t *, struct inode *, unsigned long,
 			      unsigned long);
 extern unsigned long ext3_count_free_blocks (struct super_block *);
+extern unsigned long ext3_count_dirs (struct super_block *);
 extern void ext3_check_blocks_bitmap (struct super_block *);
 extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
 						    unsigned int block_group,
@@ -620,7 +622,7 @@
 extern int ext3_sync_file (struct file *, struct dentry *, int);
 
 /* ialloc.c */
-extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
+extern struct inode * ext3_new_inode (handle_t *, struct inode *, int);
 extern void ext3_free_inode (handle_t *, struct inode *);
 extern struct inode * ext3_orphan_get (struct super_block *, ino_t);
 extern unsigned long ext3_count_free_inodes (struct super_block *);
diff -ru ./include/linux/ext3_fs_sb.h /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/ext3_fs_sb.h
--- ./include/linux/ext3_fs_sb.h	Tue Feb 26 03:38:13 2002
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/ext3_fs_sb.h	Mon Mar 24 18:07:20 2003
@@ -62,6 +62,8 @@
 	int s_inode_size;
 	int s_first_ino;
 	u32 s_next_generation;
+	unsigned long s_dir_count;
+	u8 *s_debts;
 
 	/* Journaling */
 	struct inode * s_journal_inode;
diff -ru ./include/linux/jbd.h /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/jbd.h
--- ./include/linux/jbd.h	Mon Mar 24 20:28:09 2003
+++ /home/hugang/deve/cvs/local/kernel/ext3_24/include/linux/jbd.h	Mon Mar 24 18:23:12 2003
@@ -17,7 +17,7 @@
 #define _LINUX_JBD_H
 
 #if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || !defined(__KERNEL__)
-
+#include <linux/slab.h>
 /* Allow this file to be included directly into e2fsprogs */
 #ifndef __KERNEL__
 #include "jfs_compat.h"
@@ -712,6 +712,21 @@
 extern void	journal_remove_journal_head(struct buffer_head *bh);
 extern void	__journal_remove_journal_head(struct buffer_head *bh);
 extern void	journal_unlock_journal_head(struct journal_head *jh);
+
+/*
+ * handle management
+ */
+extern kmem_cache_t *jbd_handle_cache;
+
+static inline handle_t *jbd_alloc_handle(int gfp_flags)
+{
+	return kmem_cache_alloc(jbd_handle_cache, gfp_flags);
+}
+
+static inline void jbd_free_handle(handle_t *handle)
+{
+	kmem_cache_free(jbd_handle_cache, handle);
+}
 
 /* Primary revoke support */
 #define JOURNAL_REVOKE_DEFAULT_HASH 256

                 reply	other threads:[~2003-03-24 12:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030324205502.7d4b015d.hugang@soulinfo.com \
    --to=hugang@soulinfo.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®