libext2fs: allow ext2fs_extent_insert to split if needed
PATCH 2/3 libext2fs: allow ext2fs_extent_insert to split if needed
In reply to a message not in the archiveIf ext2fs_extent_insert finds that the requested node for insertion is full, it will currently fail.
With this patch it will split as necessary to make room.
--- lib/ext2fs/extent.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)
lib/ext2fs/extent.c +8 -3
@@ -894,8 +894,13 @@ errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags, path = handle->path + handle->level; -if (path->entries >= path->max_entries) -return EXT2_ET_CANT_INSERT_EXTENT ; +if (path->entries >= path->max_entries) { +dbg_printf ("node full - splitting\n" ); + retval =ext2fs_node_split (handle,0 ); +if (retval) + goto errout; + path = handle->path + handle->level; + } eh = (struct ext3_extent_header *) path->buf;if (path->curr) {
@@ -1245,7 +1250,7 @@ void do_insert_node(int argc, char *argv[]) }if (argc !=4 ) { -fprintf (stderr,"usage: %s <lblk> <len> <pblk>\n" , cmd); +fprintf (stderr,"usage: %s [--after] <lblk> <len> <pblk>\n" , cmd);return ; }
Signature · 1 line
1.5.4.1
PATCH 3/3 libext2fs: add ext2fs_extent_set_bmap
In reply to a message not in the archiveAllows unmapping or remapping single mapped logical blocks, and mapping currently unmapped blocks.
Also implements ext2fs_extent_fix_parents() to fix parent index logical starts, if the first index of a node changes its logical start block.
Currently this results in new single-block extents; I think perhaps ext2fs_extent_insert should grow a flag to request merging with a nearby extent?
--- lib/ext2fs/extent.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++ lib/ext2fs/extent_dbg.ct | 3 + 2 files changed, 266 insertions(+), 0 deletions(-)
lib/ext2fs/extent.c +263 -0
@@ -637,6 +637,64 @@ errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,return extent_goto (handle,0 , blk); } +/* +* Traverse back up to root fixing parents of current node as needed. +* +* If we changed start of first entry in a node, fix parent index start +* and so on. +* +* Safe to call for any position in node; if not at the first entry, +* will simply return. +*/ +errcode_t ext2fs_extent_fix_parents (ext2_extent_handle_t handle) +{ +int retval =0 ; +blk64_t start; +struct extent_path *path; +struct ext2fs_extent extent; + +EXT2_CHECK_MAGIC (handle,EXT2_ET_MAGIC_EXTENT_HANDLE ); + +if (!(handle->fs->flags &EXT2_FLAG_RW )) +return EXT2_ET_RO_FILSYS ; + +if (!handle->path) +return EXT2_ET_NO_CURRENT_NODE ; + + path = handle->path + handle->level; +if (!path->curr) +return EXT2_ET_NO_CURRENT_NODE ; + + retval =ext2fs_extent_get (handle,EXT2_EXTENT_CURRENT , &extent); +if (retval) + goto done; + +/* modified node's start block */ + start = extent.e_lblk; + +/* traverse up until index not first, or startblk matches, or top */ +while (handle->level >0 && + (path->left == path->entries -1 )) { + retval =ext2fs_extent_get (handle,EXT2_EXTENT_UP , &extent); +if (retval) + goto done; +if (extent.e_lblk == start) +break ; + path = handle->path + handle->level; + extent.e_len += (extent.e_lblk - start); + extent.e_lblk = start; + retval =ext2fs_extent_replace (handle,0 , &extent); +if (retval) + goto done; +update_path (handle); + } + +/* put handle back to where we started */ + retval =ext2fs_extent_goto (handle, start); +done: +return retval; +} + errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle, int flags EXT2FS_ATTR((unused)), struct ext2fs_extent *extent)
@@ -938,6 +996,175 @@ errout:return retval; } +/* +* Sets the physical block for a logical file block in the extent tree. +* +* May: map unmapped, unmap mapped, or remap mapped blocks. +* +* Mapping an unmapped block adds a single-block extent. +* +* Unmapping first or last block modifies extent in-place +* - But may need to fix parent's starts too in first-block case +* +* Mapping any unmapped block requires adding a (single-block) extent +* and inserting into proper point in tree. +* +* Modifying (unmapping or remapping) a block in the middle +* of an extent requires splitting the extent. +* - Remapping case requires new single-block extent. +* +* Remapping first or last block adds an extent. +* +* We really need extent adding to be smart about merging. +*/ + +errcode_t ext2fs_extent_set_bmap (ext2_extent_handle_t handle, +blk64_t logical,blk64_t physical) +{ +int retval =0 ; +int mapped =1 ;/* logical is mapped? */ +struct extent_path *path; +struct ext2fs_extent extent; +struct ext2fs_extent newextent; + +EXT2_CHECK_MAGIC (handle,EXT2_ET_MAGIC_EXTENT_HANDLE ); + +if (!(handle->fs->flags &EXT2_FLAG_RW )) +return EXT2_ET_RO_FILSYS ; + +/* go to the logical spot we want to (re/un)map */ + retval =ext2fs_extent_goto (handle, logical); +if (retval) { +if (retval ==EXT2_ET_EXTENT_NOT_FOUND ) { + retval =0 ; + mapped =0 ; +if (!physical) { +dbg_printf ("block already unmapped\n" ); + goto done; + } + }else + goto done; + } + +if (!handle->path) +return EXT2_ET_NO_CURRENT_NODE ; + + path = handle->path + handle->level; +if (!path->curr) +return EXT2_ET_NO_CURRENT_NODE ; + +/* +* This may be the extent *before* the requested logical, +* if it's currently unmapped. +*/ + retval =ext2fs_extent_get (handle,EXT2_EXTENT_CURRENT , &extent); +if (retval) + goto done; + +/* check if already pointing to the requested physical */ +if (mapped && extent.e_pblk + (logical - extent.e_lblk) == physical) { +dbg_printf ("physical block unchanged\n" ); + goto done; + } + +/* if (re)mapping, set up new extent, we'll insert it later */ +if (physical) { + newextent.e_len =1 ; + newextent.e_pblk = physical; + newextent.e_lblk = logical; + newextent.e_flags =EXT2_EXTENT_FLAGS_LEAF ; + } + +if (!mapped) { +dbg_printf ("mapping unmapped logical block\n" ); + retval =ext2fs_extent_insert (handle, +EXT2_EXTENT_INSERT_AFTER , &newextent); +if (retval) + goto done; + retval =ext2fs_extent_fix_parents (handle); +if (retval) + goto done; + }else if (logical == extent.e_lblk + extent.e_len -1 && + extent.e_len ==1 ) { +dbg_printf ("(re/un)mapping only block in extent\n" ); +if (physical) { + extent.e_pblk = physical; + retval =ext2fs_extent_replace (handle,0 , &extent); + }else { + retval =ext2fs_extent_delete (handle,0 ); +if (retval) + goto done; + retval =ext2fs_extent_fix_parents (handle); + } + +if (retval) + goto done; + }else if (logical == extent.e_lblk + extent.e_len -1 ) { +dbg_printf ("(re/un)mapping last block in extent\n" ); + extent.e_len--; + retval =ext2fs_extent_replace (handle,0 , &extent); +if (retval) + goto done; +if (physical) { + retval =ext2fs_extent_insert (handle, +EXT2_EXTENT_INSERT_AFTER , &newextent); +if (retval) + goto done; + } + }else if (logical == extent.e_lblk) { +dbg_printf ("(re/un)mapping first block in extent\n" ); + extent.e_pblk++; + extent.e_lblk++; + extent.e_len--; + retval =ext2fs_extent_replace (handle,0 , &extent); +if (retval) + goto done; +if (physical) { +/* insert new extent ahead of current */ + retval =ext2fs_extent_insert (handle, +0 , &newextent); +if (retval) + goto done; + }else { + retval =ext2fs_extent_fix_parents (handle); +if (retval) + goto done; + } + }else { +__u32 orig_length; + +dbg_printf ("(re/un)mapping in middle of extent\n" ); +/* need to split this extent; later */ + + orig_length = extent.e_len; + +/* shorten pre-split extent */ + extent.e_len = (logical - extent.e_lblk); + retval =ext2fs_extent_replace (handle,0 , &extent); +if (retval) + goto done; +/* insert our new extent, if any */ +if (physical) { +/* insert new extent after current */ + retval =ext2fs_extent_insert (handle, +EXT2_EXTENT_INSERT_AFTER , &newextent); +if (retval) + goto done; + } +/* add post-split extent */ + extent.e_pblk += extent.e_len +1 ; + extent.e_lblk += extent.e_len +1 ; + extent.e_len = orig_length - extent.e_len -1 ; + retval =ext2fs_extent_insert (handle, +EXT2_EXTENT_INSERT_AFTER , &extent); +if (retval) + goto done; + } + +done: +return retval; +} +errcode_t ext2fs_extent_delete (ext2_extent_handle_t handle, int flags EXT2FS_ATTR((unused))) {
@@ -1277,6 +1504,42 @@ void do_insert_node(int argc, char *argv[])do_current_node (argc, argv); } +void do_set_bmap (int argc,char **argv) +{ +errcode_t retval; +blk_t logical; +blk_t physical; +char *cmd; +int err; + +if (check_fs_read_write (argv[0 ])) +return ; + + cmd = argv[0 ]; + +if (argc !=3 ) { +fprintf (stderr,"usage: %s <lblk> <pblk>\n" , cmd); +return ; + } + + logical =parse_ulong (argv[1 ], cmd, +"logical block" , &err); +if (err) +return ; + + physical =parse_ulong (argv[2 ], cmd, +"physical block" , &err); +if (err) +return ; + + retval =ext2fs_extent_set_bmap (current_handle, logical, (blk64_t ) physical); +if (retval) { +com_err (cmd, retval,0 ); +return ; + } +do_current_node (argc, argv); +} +void do_print_all (int argc,char **argv) {struct ext2fs_extent extent;
lib/ext2fs/extent_dbg.ct +3 -0
@@ -55,6 +55,9 @@ request do_insert_node, "Insert node", request do_split_node, "Split node", split_node, split; +request do_set_bmap, "Set block mapping", + set_bmap; + request do_replace_node, "Insert node", replace_node, replace;
Signature · 1 line
1.5.4.1
PATCH 1/3 libext2fs: ext2fs_node_split
In reply to a message not in the archiveWhen called for a given handle, this function will split the current node such that half of the node's entries will be moved to a new tree block. The parent will then be updated to point to the (now smaller) original node as well as the new node.
If the root node is requested to be split, it will move all entries out to a new node, and leave a single entry in the root pointing to that new node.
If the reqested split node's parent is full it will recursively split up to the root to make room for the new node's insertion.
If you ask to split a non-root node with only one entry, it will refuse (we'd have an empty node otherwise).
It also updates the i_blocks count when a new block has successfully been connected to the tree.
--- lib/ext2fs/ext2_err.et.in | 3 + lib/ext2fs/extent.c | 227 +++++++++++++++++++++++++++++++++++++++++++++ lib/ext2fs/extent_dbg.ct | 3 + 3 files changed, 233 insertions(+), 0 deletions(-)
lib/ext2fs/ext2_err.et.in +3 -0
@@ -401,6 +401,9 @@ ec EXT2_ET_OP_NOT_SUPPORTED, ec EXT2_ET_CANT_INSERT_EXTENT, "No room to insert extent in node" +ec EXT2_ET_CANT_SPLIT_EXTENT, + "Splitting would result in empty node" + ec EXT2_ET_EXTENT_NOT_FOUND, "Extent not found"
lib/ext2fs/extent.c +227 -0
239 lines not on this page · raw message
lib/ext2fs/extent_dbg.ct +3 -0
@@ -52,6 +52,9 @@ request do_delete_node, "Delete node", request do_insert_node, "Insert node", insert_node, insert; +request do_split_node, "Split node", + split_node, split; + request do_replace_node, "Insert node", replace_node, replace;
Signature · 1 line
1.5.4.1
Re: [PATCH 1/3] libext2fs: ext2fs_node_split
Quoted 4 lines · Eric Sandeen
When called for a given handle, this function will split the current node such that half of the node's entries will be moved to a new tree block. The parent will then be updated to point to the (now smaller) original node as well as the new node.
This patch looks good. One minor nit; if you're going to define new functions which are intended to be exported, then they need to be defined in the ext2fs.h header file --- otherwise, it should be declared static, to prevent function leakage. Should ext2fs_node_split() be exported? There doesn't seem to be any reason *not* to export it, but at the same time, there doesn't seem to be a good reason to export, either.
I'd tend to keep it static for now; what do other people think?
- Ted
Re: [PATCH 1/3] libext2fs: ext2fs_node_split
In reply to Eric Sandeenlib/ext2fs/extent.c errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle, · line 759
Quoted 7 lines
lib/ext2fs/extent.c +7
-0+/* splitting root level means moving them all out */ +if (handle->level ==0 ) { + new_root =1 ; + tocopy =ext2fs_le16_to_cpu (eh->eh_entries); + }else { + tocopy =ext2fs_le16_to_cpu (eh->eh_entries) /2 ; + }
This is probably in the "would be nice" category, but if we know that we are appending to the file, it might actually be better to move all of the entries into one block, and create an empty block to the right. That way, the average density of the leaf nodes will be much closer to 100%, instead of 50% full.
This optimization is far more important in the kernel, though.
- Ted
Re: [PATCH 1/3] libext2fs: ext2fs_node_split
In reply to Theodore TsoQuoted 14 lines · Theodore Tso
Quoted 4 lines · Eric Sandeen
When called for a given handle, this function will split the current node such that half of the node's entries will be moved to a new tree block. The parent will then be updated to point to the (now smaller) original node as well as the new node.
This patch looks good. One minor nit; if you're going to define new functions which are intended to be exported, then they need to be defined in the
ext2fs.hheader file --- otherwise, it should be declared static, to prevent function leakage. Shouldext2fs_node_split()be exported? There doesn't seem to be any reason *not* to export it, but at the same time, there doesn't seem to be a good reason to export, either.
Quoted 1 line
I'd tend to keep it static for now; what do other people think?
I'd say static until needed otherwise...
an _extent_split might be more useful for a public interface; _node_split is getting awfully low level IMHO.
-Eric