From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: tytso@mit.edu, darrick.wong@oracle.com
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 11/47] libext2fs: find inode goal when allocating blocks
Date: Fri, 07 Nov 2014 13:51:59 -0800 [thread overview]
Message-ID: <20141107215159.883.90273.stgit@birch.djwong.org> (raw)
In-Reply-To: <20141107215042.883.49888.stgit@birch.djwong.org>
Try to be a little smarter about where we go to allocate blocks for a
inode. For a given inode and logical offset, set the goal as if the
file were physically continuous. If it's bmapped, just start looking
at wherever lblk 0 is. If that's not possible (the file has no
lblk>pblk mappings, inline data, etc.) then start looking in the
inode's block group.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
e2fsck/pass2.c | 3 ++-
lib/ext2fs/alloc.c | 43 ++++++++++++++++++++++++++++++++++++++++
lib/ext2fs/bmap.c | 5 +++--
lib/ext2fs/expanddir.c | 6 +++++-
lib/ext2fs/ext2fs.h | 2 ++
lib/ext2fs/ext_attr.c | 4 +---
lib/ext2fs/extent.c | 11 +++-------
lib/ext2fs/mkdir.c | 7 +++++--
lib/ext2fs/symlink.c | 7 +++++--
tests/f_extent_oobounds/script | 6 +++---
tests/f_extents2/expect.1 | 2 +-
11 files changed, 73 insertions(+), 23 deletions(-)
diff --git a/e2fsck/pass2.c b/e2fsck/pass2.c
index c354e4a..cd0762a 100644
--- a/e2fsck/pass2.c
+++ b/e2fsck/pass2.c
@@ -1772,7 +1772,8 @@ static int allocate_dir_block(e2fsck_t ctx,
pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
db->blockcnt, &blk);
if (pctx->errcode || blk == 0) {
- pctx->errcode = ext2fs_new_block2(fs, 0,
+ blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
+ pctx->errcode = ext2fs_new_block2(fs, blk,
ctx->block_found_map, &blk);
if (pctx->errcode) {
pctx->str = "ext2fs_new_block";
diff --git a/lib/ext2fs/alloc.c b/lib/ext2fs/alloc.c
index cb778f7..fda45f9 100644
--- a/lib/ext2fs/alloc.c
+++ b/lib/ext2fs/alloc.c
@@ -288,3 +288,46 @@ void ext2fs_set_alloc_block_callback(ext2_filsys fs,
fs->get_alloc_block = func;
}
+
+blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
+ struct ext2_inode *inode, blk64_t lblk)
+{
+ dgrp_t group;
+ __u8 log_flex;
+ struct ext2fs_extent extent;
+ ext2_extent_handle_t handle;
+ errcode_t err;
+
+ if (inode == NULL || ext2fs_inode_data_blocks2(fs, inode) == 0)
+ goto no_blocks;
+
+ if (inode->i_flags & EXT4_INLINE_DATA_FL)
+ goto no_blocks;
+
+ if (inode->i_flags & EXT4_EXTENTS_FL) {
+ err = ext2fs_extent_open2(fs, ino, inode, &handle);
+ if (err)
+ goto no_blocks;
+ err = ext2fs_extent_goto2(handle, 0, lblk);
+ if (err)
+ goto extent_err;
+ err = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
+ if (err)
+ goto extent_err;
+ return extent.e_pblk + (lblk - extent.e_lblk);
+extent_err:
+ ext2fs_extent_free(handle);
+ goto no_blocks;
+ }
+
+ /* block mapped file; see if block zero is mapped? */
+ if (inode->i_block[0])
+ return inode->i_block[0];
+
+no_blocks:
+ log_flex = fs->super->s_log_groups_per_flex;
+ group = ext2fs_group_of_ino(fs, ino);
+ if (log_flex)
+ group = group & ~((1 << (log_flex)) - 1);
+ return ext2fs_group_first_block2(fs, group);
+}
diff --git a/lib/ext2fs/bmap.c b/lib/ext2fs/bmap.c
index f819e9c..cb3f5a1 100644
--- a/lib/ext2fs/bmap.c
+++ b/lib/ext2fs/bmap.c
@@ -244,7 +244,7 @@ got_block:
retval = extent_bmap(fs, ino, inode, handle, block_buf,
0, block-1, 0, blocks_alloc, &blk64);
if (retval)
- blk64 = 0;
+ blk64 = ext2fs_find_inode_goal(fs, ino, inode, block);
retval = ext2fs_alloc_block2(fs, blk64, block_buf,
&blk64);
if (retval)
@@ -354,7 +354,8 @@ errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
}
*phys_blk = inode_bmap(inode, block);
- b = block ? inode_bmap(inode, block-1) : 0;
+ b = block ? inode_bmap(inode, block - 1) :
+ ext2fs_find_inode_goal(fs, ino, inode, block);
if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
retval = ext2fs_alloc_block(fs, b, block_buf, &b);
diff --git a/lib/ext2fs/expanddir.c b/lib/ext2fs/expanddir.c
index ecc13ae..9f02312 100644
--- a/lib/ext2fs/expanddir.c
+++ b/lib/ext2fs/expanddir.c
@@ -102,9 +102,13 @@ errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir)
if (retval)
return retval;
+ retval = ext2fs_read_inode(fs, dir, &inode);
+ if (retval)
+ return retval;
+
es.done = 0;
es.err = 0;
- es.goal = 0;
+ es.goal = ext2fs_find_inode_goal(fs, dir, &inode, 0);
es.newblocks = 0;
es.dir = dir;
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index f9599a3..afe4fd5 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -689,6 +689,8 @@ extern void ext2fs_set_alloc_block_callback(ext2_filsys fs,
errcode_t (**old)(ext2_filsys fs,
blk64_t goal,
blk64_t *ret));
+blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
+ struct ext2_inode *inode, blk64_t lblk);
/* alloc_sb.c */
extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
diff --git a/lib/ext2fs/ext_attr.c b/lib/ext2fs/ext_attr.c
index c397e00..70bc3f9 100644
--- a/lib/ext2fs/ext_attr.c
+++ b/lib/ext2fs/ext_attr.c
@@ -374,7 +374,6 @@ static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
{
struct ext2_ext_attr_header *header;
void *block_buf = NULL;
- dgrp_t grp;
blk64_t blk, goal;
errcode_t err;
@@ -420,8 +419,7 @@ static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
}
/* Allocate a block */
- grp = ext2fs_group_of_ino(fs, ino);
- goal = ext2fs_inode_table_loc(fs, grp);
+ goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
if (err)
goto out2;
diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c
index ca5b78b..b6e4fbd 100644
--- a/lib/ext2fs/extent.c
+++ b/lib/ext2fs/extent.c
@@ -1012,14 +1012,9 @@ static errcode_t extent_node_split(ext2_extent_handle_t handle,
goto done;
}
- if (!goal_blk) {
- dgrp_t group = ext2fs_group_of_ino(handle->fs, handle->ino);
- __u8 log_flex = handle->fs->super->s_log_groups_per_flex;
-
- if (log_flex)
- group = group & ~((1 << (log_flex)) - 1);
- goal_blk = ext2fs_group_first_block2(handle->fs, group);
- }
+ if (!goal_blk)
+ goal_blk = ext2fs_find_inode_goal(handle->fs, handle->ino,
+ handle->inode, 0);
retval = ext2fs_alloc_block2(handle->fs, goal_blk, block_buf,
&new_node_pblk);
if (retval)
diff --git a/lib/ext2fs/mkdir.c b/lib/ext2fs/mkdir.c
index c4c7967..433f3b4 100644
--- a/lib/ext2fs/mkdir.c
+++ b/lib/ext2fs/mkdir.c
@@ -68,8 +68,12 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
/*
* Allocate a data block for the directory
*/
+ memset(&inode, 0, sizeof(struct ext2_inode));
if (!inline_data) {
- retval = ext2fs_new_block2(fs, 0, 0, &blk);
+ retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
+ &inode,
+ 0),
+ NULL, &blk);
if (retval)
goto cleanup;
}
@@ -77,7 +81,6 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
/*
* Create a scratch template for the directory
*/
- memset(&inode, 0, sizeof(struct ext2_inode));
if (inline_data)
retval = ext2fs_new_dir_inline_data(fs, ino, parent,
inode.i_block);
diff --git a/lib/ext2fs/symlink.c b/lib/ext2fs/symlink.c
index f6eb6b6..0732afe 100644
--- a/lib/ext2fs/symlink.c
+++ b/lib/ext2fs/symlink.c
@@ -51,9 +51,13 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
/*
* Allocate a data block for slow links
*/
+ memset(&inode, 0, sizeof(struct ext2_inode));
fastlink = (target_len < sizeof(inode.i_block));
if (!fastlink) {
- retval = ext2fs_new_block2(fs, 0, 0, &blk);
+ retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
+ &inode,
+ 0),
+ NULL, &blk);
if (retval)
goto cleanup;
retval = ext2fs_get_mem(fs->blocksize, &block_buf);
@@ -74,7 +78,6 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
/*
* Create the inode structure....
*/
- memset(&inode, 0, sizeof(struct ext2_inode));
inode.i_mode = LINUX_S_IFLNK | 0777;
inode.i_uid = inode.i_gid = 0;
inode.i_links_count = 1;
diff --git a/tests/f_extent_oobounds/script b/tests/f_extent_oobounds/script
index b00b031..54674ca 100644
--- a/tests/f_extent_oobounds/script
+++ b/tests/f_extent_oobounds/script
@@ -7,6 +7,9 @@ dd if=/dev/zero of=$TMPFILE bs=1k count=256 > /dev/null 2>&1
$MKE2FS -Ft ext4 $TMPFILE > /dev/null 2>&1
$DEBUGFS -w $TMPFILE << EOF > /dev/null 2>&1
write /dev/null testfile
+setb 100 15
+setb 130 30
+setb 200 30
extent_open testfile
insert_node 0 15 100
insert_node --after 15 15 115
@@ -22,9 +25,6 @@ extent_open testfile
extent_close
set_inode_field testfile i_size 61400
set_inode_field testfile i_blocks 154
-setb 100 15
-setb 130 30
-setb 200 30
set_bg 0 free_blocks_count 156
set_bg 0 bg_checksum calc
set_super_value free_blocks_count 156
diff --git a/tests/f_extents2/expect.1 b/tests/f_extents2/expect.1
index fa7f6eb..51e36ff 100644
--- a/tests/f_extents2/expect.1
+++ b/tests/f_extents2/expect.1
@@ -55,7 +55,7 @@ Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
-Block bitmap differences: -(26--34) -(154--199)
+Block bitmap differences: -(25--34) -(155--199)
Fix? yes
Free blocks count wrong for group #0 (65535, counted=55).
next prev parent reply other threads:[~2014-11-07 21:52 UTC|newest]
Thread overview: 104+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-07 21:50 [PATCH 00/47] e2fsprogs November 2014 patchbomb Darrick J. Wong
2014-11-07 21:50 ` [PATCH 01/47] tests: fix test scripts that don't work on non-Linux systems Darrick J. Wong
2014-11-08 1:55 ` Theodore Ts'o
2014-11-07 21:50 ` [PATCH 02/47] misc: fix compiler warnings and minor build errors Darrick J. Wong
2014-11-08 2:25 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 03/47] dumpe2fs: don't crash when the user provides no block device argument Darrick J. Wong
2014-11-08 2:26 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 04/47] libext2fs: fix endian handling error; reduce fragmentation some Darrick J. Wong
2014-11-17 21:26 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 05/47] libext2fs: set BLOCK_UNINIT for non-last blockgroups if all blocks are free Darrick J. Wong
2014-11-17 22:47 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 06/47] libext2fs: don't allow alloc_stats on bad inode/block numbers Darrick J. Wong
2014-11-17 23:00 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 07/47] libext2fs: refactor extent head creation Darrick J. Wong
2014-12-03 3:55 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 08/47] libext2fs: file IO routines should handle uninit blocks Darrick J. Wong
2014-12-03 3:57 ` Theodore Ts'o
2014-11-07 21:51 ` [PATCH 09/47] libext2fs: use a dynamically sized (or caller-provided) block zeroing buffer Darrick J. Wong
2014-12-12 2:37 ` Theodore Ts'o
2014-12-12 5:02 ` Darrick J. Wong
2014-12-13 16:24 ` Theodore Ts'o
2014-12-13 16:25 ` Theodore Ts'o
2014-12-15 16:09 ` Andreas Dilger
2014-11-07 21:51 ` [PATCH 10/47] libext2fs: support BLKZEROOUT/FALLOC_FL_ZERO_RANGE in ext2fs_zero_blocks Darrick J. Wong
2014-12-13 16:29 ` Theodore Ts'o
2014-12-14 3:08 ` Darrick J. Wong
2014-11-07 21:51 ` Darrick J. Wong [this message]
2014-12-14 1:13 ` [PATCH 11/47] libext2fs: find inode goal when allocating blocks Theodore Ts'o
2014-12-14 21:02 ` Darrick J. Wong
2014-11-07 21:52 ` [PATCH 12/47] libext2fs: set interior tree block goal more intelligently Darrick J. Wong
2014-12-14 1:17 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 13/47] libext2fs: add a way to check the theoretical maximum extent tree depth Darrick J. Wong
2014-12-14 2:23 ` Theodore Ts'o
2014-12-14 3:11 ` Darrick J. Wong
2014-12-15 3:48 ` Theodore Ts'o
2014-12-15 6:38 ` Darrick J. Wong
2014-11-07 21:52 ` [PATCH 14/47] libext2fs: ext2fs_new_block2() should call alloc_block hook Darrick J. Wong
2014-11-07 21:52 ` [PATCH 15/47] misc: don't allow mk_hugefiles unless the fs supports extents Darrick J. Wong
2014-12-14 2:51 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 16/47] dumpe2fs: 80 column outputs, please Darrick J. Wong
2014-12-14 2:52 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 17/47] dumpe2fs: output cleanup Darrick J. Wong
2014-12-14 2:53 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 18/47] e2fsck: only complain about no-checksum directory blocks once Darrick J. Wong
2014-12-14 2:55 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 19/47] e2fsck: don't complain about root dir csum failures when getting lnf Darrick J. Wong
2014-12-14 2:57 ` Theodore Ts'o
2014-11-07 21:52 ` [PATCH 20/47] tune2fs: warn if extents are not enabled when turning on metadata_csum Darrick J. Wong
2014-12-14 2:58 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 21/47] tune2fs: enable uninit_bg when disabling metadata_csum Darrick J. Wong
2014-12-14 2:58 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 22/47] tests: testcases for enabling/disabling metadata_csum via tune2fs Darrick J. Wong
2014-12-14 3:00 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 23/47] mke2fs: don't zero inode table blocks that are already zeroed Darrick J. Wong
2014-12-14 3:01 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 24/47] resize2fs: don't exit if shrinking sparse_super2 fs to one bg Darrick J. Wong
2014-12-14 3:06 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 25/47] resize2fs: quickly rewrite extent blocks when moving an inode w/ metadata_csum Darrick J. Wong
2014-12-14 3:09 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 26/47] resize2fs: use old_fs to detect per-bg metadata blocks to free Darrick J. Wong
2014-12-15 2:09 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 27/47] resize2fs: don't interpret bitmap shift while crossing flexbg as raid stride Darrick J. Wong
2014-12-15 2:11 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 28/47] resize2fs: set bg flags and unused inode count when resizing Darrick J. Wong
2014-12-15 2:12 ` Theodore Ts'o
2014-11-07 21:53 ` [PATCH 29/47] resize2fs: don't mark unallocated bg metadata blocks when fixing bg flags Darrick J. Wong
2014-12-15 2:17 ` Theodore Ts'o
2014-12-15 7:09 ` Darrick J. Wong
2014-12-19 19:15 ` Darrick J. Wong
2014-11-07 21:54 ` [PATCH 30/47] resize2fs: don't play stupid games with the block count Darrick J. Wong
2014-12-15 3:13 ` Theodore Ts'o
2014-11-07 21:54 ` [PATCH 31/47] libext2fs/e2fsck: provide routines to read-ahead metadata Darrick J. Wong
2014-12-15 17:36 ` Theodore Ts'o
2014-12-15 19:19 ` Darrick J. Wong
2014-11-07 21:54 ` [PATCH 32/47] e2fsck: read-ahead metadata during passes 1, 2, and 4 Darrick J. Wong
2014-12-10 20:27 ` Darrick J. Wong
2014-12-10 22:00 ` Theodore Ts'o
2014-11-07 21:54 ` [PATCH 33/47] e2fsck: rebuild sparse extent trees/convert non-extent ext3 files Darrick J. Wong
2014-12-04 23:20 ` Andreas Dilger
2014-12-04 23:45 ` Darrick J. Wong
2014-12-11 22:05 ` [PATCH v2 " Darrick J. Wong
2014-11-07 21:54 ` [PATCH 34/47] tests: verify proper rebuilding of sparse extent trees and block map file conversion Darrick J. Wong
2014-11-07 21:54 ` [PATCH 35/47] resize2fs: convert fs to and from 64bit mode Darrick J. Wong
2014-12-15 3:32 ` Theodore Ts'o
2014-12-15 6:41 ` Darrick J. Wong
2014-12-15 17:46 ` Theodore Ts'o
2014-11-07 21:54 ` [PATCH 36/47] tests: test resize2fs 32->64 and 64->32bit conversion code Darrick J. Wong
2014-12-15 17:46 ` Theodore Ts'o
2014-11-07 21:54 ` [PATCH 37/47] libext2fs: support allocating uninit blocks in bmap2() Darrick J. Wong
2014-11-07 21:54 ` [PATCH 38/47] libext2fs: find/alloc a range of empty blocks Darrick J. Wong
2014-11-07 21:54 ` [PATCH 39/47] libext2fs: add new hooks to support large allocations Darrick J. Wong
2014-11-07 21:55 ` [PATCH 40/47] libext2fs: implement fallocate Darrick J. Wong
2014-11-07 21:55 ` [PATCH 41/47] libext2fs: use fallocate for creating journals and hugefiles Darrick J. Wong
2014-11-07 21:55 ` [PATCH 42/47] debugfs: implement fallocate Darrick J. Wong
2014-11-07 21:55 ` [PATCH 43/47] tests: test debugfs punch command Darrick J. Wong
2014-11-07 21:55 ` [PATCH 45/47] fuse2fs: translate ACL structures Darrick J. Wong
2014-11-07 21:55 ` [PATCH 46/47] fuse2fs: handle 64-bit dates correctly Darrick J. Wong
2014-11-07 21:55 ` [PATCH 47/47] fuse2fs: implement fallocate Darrick J. Wong
2014-11-12 22:43 ` [PATCH 48/47] misc: fix infinite loop when finding the start of the hugefile start range Darrick J. Wong
2014-12-03 3:06 ` Theodore Ts'o
2014-11-27 0:01 ` [PATCH 49/47] libext2fs: don't report garbage inodes with really large inodes Darrick J. Wong
2014-12-03 3:18 ` Theodore Ts'o
2014-12-04 20:39 ` [PATCH 51/47] e2fsck: force-reread of inode from disk when re-checking a checksum error Darrick J. Wong
2014-12-11 22:49 ` Theodore Ts'o
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=20141107215159.883.90273.stgit@birch.djwong.org \
--to=darrick.wong@oracle.com \
--cc=linux-ext4@vger.kernel.org \
--cc=tytso@mit.edu \
/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;
as well as URLs for NNTP newsgroup(s).