[PATCH e2fsprogs] delete unused nodes in ext2fs_extent_delete

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch causes ext2fs_extent_delete to remove an
extent node if the last record in it has been deleted.

The parent node is also updated, and the inode block count
is decremented.

Passing in the EXT2_EXTENT_DELETE_KEEP_EMPTY flag will
allow the empty node to remain.

(A bit of a quick hack but seems to work for me)

Signed-off-by: Eric Sandeen <sandeen@xxxxxxxxxx>
---

diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index c6ac317..d237bd3 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -336,6 +336,10 @@ typedef struct ext2_extent_path *ext2_extent_path_t;
 #define EXT2_EXTENT_INSERT_NOSPLIT	0x0002 /* insert may not cause split */
 
 /*
+ * Flags used by ext2fs_extent_delete()
+ */
+#define EXT2_EXTENT_DELETE_KEEP_EMPTY	0x001 /* keep node if last extnt gone */
+/*
  * Data structure returned by ext2fs_extent_get_info()
  */
 struct ext2_extent_info {
diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c
index 357ca34..f86bf0d 100644
--- a/lib/ext2fs/extent.c
+++ b/lib/ext2fs/extent.c
@@ -1169,8 +1169,7 @@ done:
 	return retval;
 }
 
-errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, 
-			       int flags EXT2FS_ATTR((unused)))
+errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags)
 {
 	struct extent_path		*path;
 	char 				*cp;
@@ -1201,13 +1200,31 @@ errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle,
 		path->curr = ix;
 	}
 	path->entries--;
-	if (path->entries == 0)
+	/* if non-root node has no entries left, remove it & parent ptr to it */
+	if (path->entries == 0 && handle->level) {
 		path->curr = 0;
 
-	eh = (struct ext3_extent_header *) path->buf;
-	eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
+		if (!(flags & EXT2_EXTENT_DELETE_KEEP_EMPTY)) {
+			struct ext2fs_extent	extent;
 
-	retval = update_path(handle);
+			retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP,
+								&extent);
+			if (retval)
+				return retval;
+
+			retval = ext2fs_extent_delete(handle, 0);
+			handle->inode->i_blocks -= handle->fs->blocksize / 512;
+			retval = ext2fs_write_inode_full(handle->fs,
+					handle->ino, handle->inode,
+					EXT2_INODE_SIZE(handle->fs->super));
+			ext2fs_block_alloc_stats(handle->fs, extent.e_pblk, -1);
+		}
+	} else {
+		eh = (struct ext3_extent_header *) path->buf;
+		eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
+
+		retval = update_path(handle);
+	}
 
 	return retval;
 }

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Reiser Filesystem Development]     [Ceph FS]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite National Park]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]     [Linux Media]

  Powered by Linux