linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3 V4] e2fsprogs/debugfs: do sparse copy when src is a sparse file
@ 2013-08-26  6:22 Robert Yang
  2013-08-26  6:22 ` [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short Robert Yang
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Robert Yang @ 2013-08-26  6:22 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, dvhart, darrick.wong

=== V4:
* Add a free(buf) in debugfs.c to fix a potential memory leak issue.

* Add the contrib/populate-extfs.sh to populate the ext2/3/4 filesystem
  from a given directory.

=== V3: 
* Use BUFSIZ (which is 8192 on Linux systems) for the max argument
  length as Ted suggested.

* Use ext2fs_get_memzero() and move it out of the while loop to get a
  better performance as Darrick suggested.

=== V2: 
* Use 64K for the IO_BUFSIZE and use malloc() to allocate the memory for 
  the buffer respect to Darrick's comments.

* Use calloc() and memcmp() to check the sparse block as Darrick
  suggested.

* Adjust the frame and remove a few un-needed code as Darrick
  suggested.

=== V1: 
* There are two patches, one is used for fixing the max length of the 
  argument, the other one is for sparsing copy when src is a sparse
  file.

// Robert

Robert Yang (3):
  debugfs.c: the max length of debugfs argument is too short
  debugfs.c: do sparse copy when src is a sparse file
  contrib/populate-extfs.sh: use debugfs to populate extX fs

 contrib/populate-extfs.sh | 105 ++++++++++++++++++++++++++++++++++++++++++++++
 debugfs/debugfs.c         |  68 +++++++++++++++++++++++++++---
 2 files changed, 168 insertions(+), 5 deletions(-)
 create mode 100755 contrib/populate-extfs.sh

-- 
1.8.1.2


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short
  2013-08-26  6:22 [PATCH 0/3 V4] e2fsprogs/debugfs: do sparse copy when src is a sparse file Robert Yang
@ 2013-08-26  6:22 ` Robert Yang
  2013-10-14  2:49   ` Theodore Ts'o
  2013-08-26  6:22 ` [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file Robert Yang
  2013-08-26  6:22 ` [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs Robert Yang
  2 siblings, 1 reply; 9+ messages in thread
From: Robert Yang @ 2013-08-26  6:22 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, dvhart, darrick.wong

The max length of debugfs argument is 256 which is too short, the
arguments are two paths, the PATH_MAX is 4096 according to
/usr/include/linux/limits.h, so use BUFSIZ (which is 8192 on Linux
systems), that's also what the ss library uses.

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Acked-by: Darren Hart <dvhart@linux.intel.com>
---
 debugfs/debugfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
index 2660218..a6bc932 100644
--- a/debugfs/debugfs.c
+++ b/debugfs/debugfs.c
@@ -37,6 +37,10 @@ extern char *optarg;
 #include "../version.h"
 #include "jfs_user.h"
 
+#ifndef BUFSIZ
+#define BUFSIZ 8192
+#endif
+
 ss_request_table *extra_cmds;
 const char *debug_prog_name;
 int sci_idx;
@@ -2311,7 +2315,7 @@ void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
 static int source_file(const char *cmd_file, int ss_idx)
 {
 	FILE		*f;
-	char		buf[256];
+	char		buf[BUFSIZ];
 	char		*cp;
 	int		exit_status = 0;
 	int		retval;
-- 
1.8.1.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file
  2013-08-26  6:22 [PATCH 0/3 V4] e2fsprogs/debugfs: do sparse copy when src is a sparse file Robert Yang
  2013-08-26  6:22 ` [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short Robert Yang
@ 2013-08-26  6:22 ` Robert Yang
  2013-10-14  2:49   ` Theodore Ts'o
  2013-10-15  3:10   ` Darrick J. Wong
  2013-08-26  6:22 ` [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs Robert Yang
  2 siblings, 2 replies; 9+ messages in thread
From: Robert Yang @ 2013-08-26  6:22 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, dvhart, darrick.wong

Let debugfs do sparse copy when src is a sparse file, just like
"cp --sparse=auto"

* For the:
  #define IO_BUFSIZE 64*1024
  this is a suggested value from gnu coreutils:
  http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/ioblksize.h;h=1ae93255e7d0ccf0855208c7ae5888209997bf16;hb=HEAD

* Use malloc() to allocate memory for the buffer since put 64K (or
  more) on the stack seems not a good idea.

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Acked-by: Darren Hart <dvhart@linux.intel.com>
---
 debugfs/debugfs.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 4 deletions(-)

diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
index a6bc932..8c32eff 100644
--- a/debugfs/debugfs.c
+++ b/debugfs/debugfs.c
@@ -41,6 +41,16 @@ extern char *optarg;
 #define BUFSIZ 8192
 #endif
 
+/* 64KiB is the minimium blksize to best minimize system call overhead. */
+#ifndef IO_BUFSIZE
+#define IO_BUFSIZE 64*1024
+#endif
+
+/* Block size for `st_blocks' */
+#ifndef S_BLKSIZE
+#define S_BLKSIZE 512
+#endif
+
 ss_request_table *extra_cmds;
 const char *debug_prog_name;
 int sci_idx;
@@ -1575,22 +1585,37 @@ void do_find_free_inode(int argc, char *argv[])
 }
 
 #ifndef READ_ONLY
-static errcode_t copy_file(int fd, ext2_ino_t newfile)
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
 {
 	ext2_file_t	e2_file;
 	errcode_t	retval;
 	int		got;
 	unsigned int	written;
-	char		buf[8192];
+	char		*buf;
 	char		*ptr;
+	char		*zero_buf;
+	int		cmp;
 
 	retval = ext2fs_file_open(current_fs, newfile,
 				  EXT2_FILE_WRITE, &e2_file);
 	if (retval)
 		return retval;
 
+	if (!(buf = (char *) malloc(bufsize))){
+		com_err("copy_file", errno, "can't allocate buffer\n");
+		return;
+	}
+
+	/* This is used for checking whether the whole block is zero */
+	retval = ext2fs_get_memzero(bufsize, &zero_buf);
+	if (retval) {
+		com_err("copy_file", retval, "can't allocate buffer\n");
+		free(buf);
+		return retval;
+	}
+
 	while (1) {
-		got = read(fd, buf, sizeof(buf));
+		got = read(fd, buf, bufsize);
 		if (got == 0)
 			break;
 		if (got < 0) {
@@ -1598,6 +1623,21 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
 			goto fail;
 		}
 		ptr = buf;
+
+		/* Sparse copy */
+		if (make_holes) {
+			/* Check whether all is zero */
+			cmp = memcmp(ptr, zero_buf, got);
+			if (cmp == 0) {
+				 /* The whole block is zero, make a hole */
+				retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
+				if (retval)
+					goto fail;
+				got = 0;
+			}
+		}
+
+		/* Normal copy */
 		while (got > 0) {
 			retval = ext2fs_file_write(e2_file, ptr,
 						   got, &written);
@@ -1608,10 +1648,14 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
 			ptr += written;
 		}
 	}
+	free(buf);
+	ext2fs_free_mem(&zero_buf);
 	retval = ext2fs_file_close(e2_file);
 	return retval;
 
 fail:
+	free(buf);
+	ext2fs_free_mem(&zero_buf);
 	(void) ext2fs_file_close(e2_file);
 	return retval;
 }
@@ -1624,6 +1668,8 @@ void do_write(int argc, char *argv[])
 	ext2_ino_t	newfile;
 	errcode_t	retval;
 	struct ext2_inode inode;
+	int		bufsize = IO_BUFSIZE;
+	int		make_holes = 0;
 
 	if (common_args_process(argc, argv, 3, 3, "write",
 				"<native file> <new file>", CHECK_FS_RW))
@@ -1699,7 +1745,15 @@ void do_write(int argc, char *argv[])
 		return;
 	}
 	if (LINUX_S_ISREG(inode.i_mode)) {
-		retval = copy_file(fd, newfile);
+		if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
+			make_holes = 1;
+			/*
+			 * Use I/O blocksize as buffer size when
+			 * copying sparse files.
+			 */
+			bufsize = statbuf.st_blksize;
+		}
+		retval = copy_file(fd, newfile, bufsize, make_holes);
 		if (retval)
 			com_err("copy_file", retval, 0);
 	}
-- 
1.8.1.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs
  2013-08-26  6:22 [PATCH 0/3 V4] e2fsprogs/debugfs: do sparse copy when src is a sparse file Robert Yang
  2013-08-26  6:22 ` [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short Robert Yang
  2013-08-26  6:22 ` [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file Robert Yang
@ 2013-08-26  6:22 ` Robert Yang
  2013-10-14  2:50   ` Theodore Ts'o
  2 siblings, 1 reply; 9+ messages in thread
From: Robert Yang @ 2013-08-26  6:22 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, dvhart, darrick.wong

This script uses debugfs command to populate the ext2/3/4 filesystem
from a given directory, it is a little similar to genext2fs, but this
one fully supports ext3 and ext4.

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Acked-by: Darren Hart <dvhart@linux.intel.com>
---
 contrib/populate-extfs.sh | 105 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100755 contrib/populate-extfs.sh

diff --git a/contrib/populate-extfs.sh b/contrib/populate-extfs.sh
new file mode 100755
index 0000000..b1d3d1f
--- /dev/null
+++ b/contrib/populate-extfs.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+#
+# This script uses debugfs command to populate the ext2/3/4 filesystem
+# from a given directory.
+#
+
+do_usage () {
+	cat << _EOF
+Usage: populate-extfs.sh <source> <device>
+Create an ext2/ext3/ext4 filesystem from a directory or file
+
+  source: The source directory or file
+  device: The target device
+
+_EOF
+	exit 1
+}
+
+[ $# -ne 2 ] && do_usage
+
+SRCDIR=${1%%/}
+DEVICE=$2
+
+# Find where is the debugfs command if not found in the env.
+if [ -z "$DEBUGFS" ]; then
+	CONTRIB_DIR=$(dirname $(readlink -f $0))
+	DEBUGFS="$CONTRIB_DIR/../debugfs/debugfs"
+fi
+
+{
+	CWD="/"
+	find $SRCDIR | while read FILE; do
+                TGT="${FILE##*/}"
+                DIR="${FILE#$SRCDIR}"
+                DIR="${DIR%$TGT}"
+
+		# Skip the root dir
+		[ ! -z "$DIR" ] || continue
+		[ ! -z "$TGT" ] || continue
+
+		if [ "$DIR" != "$CWD" ]; then
+			echo "cd $DIR"
+			CWD="$DIR"
+		fi
+
+		# Only stat once since stat is a time consuming command
+		STAT=$(stat -c "TYPE=\"%F\";DEVNO=\"0x%t 0x%T\";MODE=\"%f\";U=\"%u\";G=\"%g\"" $FILE)
+		eval $STAT
+
+		case $TYPE in
+		"directory")
+			echo "mkdir $TGT"
+			;;
+		"regular file" | "regular empty file")
+			echo "write $FILE $TGT"
+			;;
+		"symbolic link")
+			LINK_TGT=$(readlink $FILE)
+			echo "symlink $TGT $LINK_TGT"
+			;;
+		"block special file")
+			echo "mknod $TGT b $DEVNO"
+			;;
+		"character special file")
+			echo "mknod $TGT c $DEVNO"
+			;;
+		"fifo")
+			echo "mknod $TGT p"
+			;;
+		*)
+			echo "Unknown/unhandled file type '$TYPE' file: $FILE" 1>&2
+			;;
+		esac
+
+		# Set the file mode
+		echo "sif $TGT mode 0x$MODE"
+
+		# Set uid and gid
+		echo "sif $TGT uid $U"
+		echo "sif $TGT gid $G"
+	done
+
+	# Handle the hard links.
+	# Save the hard links to a file, use the inode number as the filename, for example:
+	# If a and b's inode number is 6775928, save a and b to /tmp/tmp.VrCwHh5gdt/6775928.
+	INODE_DIR=`mktemp -d` || exit 1
+	for i in `find $SRCDIR -type f -links +1 -printf 'INODE=%i###FN=%p\n'`; do
+		eval `echo $i | sed 's$###$ $'`
+		echo ${FN#$SRCDIR} >>$INODE_DIR/$INODE
+	done
+	# Use the debugfs' ln and "sif links_count" to handle them.
+	for i in `ls $INODE_DIR`; do
+		# The link source
+		SRC=`head -1 $INODE_DIR/$i`
+		# Remove the files and link them again except the first one
+		for TGT in `sed -n -e '1!p' $INODE_DIR/$i`; do
+			echo "rm $TGT"
+			echo "ln $SRC $TGT"
+		done
+		LN_CNT=`cat $INODE_DIR/$i | wc -l`
+		# Set the links count
+		echo "sif $SRC links_count $LN_CNT"
+	done
+	rm -fr $INODE_DIR
+} | $DEBUGFS -w -f - $DEVICE
-- 
1.8.1.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short
  2013-08-26  6:22 ` [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short Robert Yang
@ 2013-10-14  2:49   ` Theodore Ts'o
  0 siblings, 0 replies; 9+ messages in thread
From: Theodore Ts'o @ 2013-10-14  2:49 UTC (permalink / raw)
  To: Robert Yang; +Cc: linux-ext4, dvhart, darrick.wong

On Mon, Aug 26, 2013 at 02:22:02PM +0800, Robert Yang wrote:
> The max length of debugfs argument is 256 which is too short, the
> arguments are two paths, the PATH_MAX is 4096 according to
> /usr/include/linux/limits.h, so use BUFSIZ (which is 8192 on Linux
> systems), that's also what the ss library uses.
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> Acked-by: Darren Hart <dvhart@linux.intel.com>

Applied, thanks.

					- Ted

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file
  2013-08-26  6:22 ` [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file Robert Yang
@ 2013-10-14  2:49   ` Theodore Ts'o
  2013-10-15  3:10   ` Darrick J. Wong
  1 sibling, 0 replies; 9+ messages in thread
From: Theodore Ts'o @ 2013-10-14  2:49 UTC (permalink / raw)
  To: Robert Yang; +Cc: linux-ext4, dvhart, darrick.wong

On Mon, Aug 26, 2013 at 02:22:03PM +0800, Robert Yang wrote:
> Let debugfs do sparse copy when src is a sparse file, just like
> "cp --sparse=auto"
> 
> * For the:
>   #define IO_BUFSIZE 64*1024
>   this is a suggested value from gnu coreutils:
>   http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/ioblksize.h;h=1ae93255e7d0ccf0855208c7ae5888209997bf16;hb=HEAD
> 
> * Use malloc() to allocate memory for the buffer since put 64K (or
>   more) on the stack seems not a good idea.
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> Acked-by: Darren Hart <dvhart@linux.intel.com>

Applied, thanks.

					- Ted

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs
  2013-08-26  6:22 ` [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs Robert Yang
@ 2013-10-14  2:50   ` Theodore Ts'o
  0 siblings, 0 replies; 9+ messages in thread
From: Theodore Ts'o @ 2013-10-14  2:50 UTC (permalink / raw)
  To: Robert Yang; +Cc: linux-ext4, dvhart, darrick.wong

On Mon, Aug 26, 2013 at 02:22:04PM +0800, Robert Yang wrote:
> This script uses debugfs command to populate the ext2/3/4 filesystem
> from a given directory, it is a little similar to genext2fs, but this
> one fully supports ext3 and ext4.
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> Acked-by: Darren Hart <dvhart@linux.intel.com>

Applied, thanks.

					- Ted

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file
  2013-08-26  6:22 ` [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file Robert Yang
  2013-10-14  2:49   ` Theodore Ts'o
@ 2013-10-15  3:10   ` Darrick J. Wong
  2013-10-15  4:52     ` Robert Yang
  1 sibling, 1 reply; 9+ messages in thread
From: Darrick J. Wong @ 2013-10-15  3:10 UTC (permalink / raw)
  To: Robert Yang; +Cc: linux-ext4, tytso, dvhart

On Mon, Aug 26, 2013 at 02:22:03PM +0800, Robert Yang wrote:
> Let debugfs do sparse copy when src is a sparse file, just like
> "cp --sparse=auto"
> 
> * For the:
>   #define IO_BUFSIZE 64*1024
>   this is a suggested value from gnu coreutils:
>   http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/ioblksize.h;h=1ae93255e7d0ccf0855208c7ae5888209997bf16;hb=HEAD
> 
> * Use malloc() to allocate memory for the buffer since put 64K (or
>   more) on the stack seems not a good idea.
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> Acked-by: Darren Hart <dvhart@linux.intel.com>
> ---
>  debugfs/debugfs.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 58 insertions(+), 4 deletions(-)
> 
> diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
> index a6bc932..8c32eff 100644
> --- a/debugfs/debugfs.c
> +++ b/debugfs/debugfs.c
> @@ -41,6 +41,16 @@ extern char *optarg;
>  #define BUFSIZ 8192
>  #endif
>  
> +/* 64KiB is the minimium blksize to best minimize system call overhead. */
> +#ifndef IO_BUFSIZE
> +#define IO_BUFSIZE 64*1024
> +#endif
> +
> +/* Block size for `st_blocks' */
> +#ifndef S_BLKSIZE
> +#define S_BLKSIZE 512
> +#endif
> +
>  ss_request_table *extra_cmds;
>  const char *debug_prog_name;
>  int sci_idx;
> @@ -1575,22 +1585,37 @@ void do_find_free_inode(int argc, char *argv[])
>  }
>  
>  #ifndef READ_ONLY
> -static errcode_t copy_file(int fd, ext2_ino_t newfile)
> +static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
>  {
>  	ext2_file_t	e2_file;
>  	errcode_t	retval;
>  	int		got;
>  	unsigned int	written;
> -	char		buf[8192];
> +	char		*buf;
>  	char		*ptr;
> +	char		*zero_buf;
> +	int		cmp;
>  
>  	retval = ext2fs_file_open(current_fs, newfile,
>  				  EXT2_FILE_WRITE, &e2_file);
>  	if (retval)
>  		return retval;
>  
> +	if (!(buf = (char *) malloc(bufsize))){
> +		com_err("copy_file", errno, "can't allocate buffer\n");
> +		return;
> +	}

Ugh.  I probably could have whined about this not using ext2fs_get_mem, but
more importantly clang whines about the undefined return value.

I'll fix it and send out a patch with my next patchbomb.

--D
> +
> +	/* This is used for checking whether the whole block is zero */
> +	retval = ext2fs_get_memzero(bufsize, &zero_buf);
> +	if (retval) {
> +		com_err("copy_file", retval, "can't allocate buffer\n");
> +		free(buf);
> +		return retval;
> +	}
> +
>  	while (1) {
> -		got = read(fd, buf, sizeof(buf));
> +		got = read(fd, buf, bufsize);
>  		if (got == 0)
>  			break;
>  		if (got < 0) {
> @@ -1598,6 +1623,21 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
>  			goto fail;
>  		}
>  		ptr = buf;
> +
> +		/* Sparse copy */
> +		if (make_holes) {
> +			/* Check whether all is zero */
> +			cmp = memcmp(ptr, zero_buf, got);
> +			if (cmp == 0) {
> +				 /* The whole block is zero, make a hole */
> +				retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
> +				if (retval)
> +					goto fail;
> +				got = 0;
> +			}
> +		}
> +
> +		/* Normal copy */
>  		while (got > 0) {
>  			retval = ext2fs_file_write(e2_file, ptr,
>  						   got, &written);
> @@ -1608,10 +1648,14 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
>  			ptr += written;
>  		}
>  	}
> +	free(buf);
> +	ext2fs_free_mem(&zero_buf);
>  	retval = ext2fs_file_close(e2_file);
>  	return retval;
>  
>  fail:
> +	free(buf);
> +	ext2fs_free_mem(&zero_buf);
>  	(void) ext2fs_file_close(e2_file);
>  	return retval;
>  }
> @@ -1624,6 +1668,8 @@ void do_write(int argc, char *argv[])
>  	ext2_ino_t	newfile;
>  	errcode_t	retval;
>  	struct ext2_inode inode;
> +	int		bufsize = IO_BUFSIZE;
> +	int		make_holes = 0;
>  
>  	if (common_args_process(argc, argv, 3, 3, "write",
>  				"<native file> <new file>", CHECK_FS_RW))
> @@ -1699,7 +1745,15 @@ void do_write(int argc, char *argv[])
>  		return;
>  	}
>  	if (LINUX_S_ISREG(inode.i_mode)) {
> -		retval = copy_file(fd, newfile);
> +		if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
> +			make_holes = 1;
> +			/*
> +			 * Use I/O blocksize as buffer size when
> +			 * copying sparse files.
> +			 */
> +			bufsize = statbuf.st_blksize;
> +		}
> +		retval = copy_file(fd, newfile, bufsize, make_holes);
>  		if (retval)
>  			com_err("copy_file", retval, 0);
>  	}
> -- 
> 1.8.1.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file
  2013-10-15  3:10   ` Darrick J. Wong
@ 2013-10-15  4:52     ` Robert Yang
  0 siblings, 0 replies; 9+ messages in thread
From: Robert Yang @ 2013-10-15  4:52 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: linux-ext4, tytso, dvhart



On 10/15/2013 11:10 AM, Darrick J. Wong wrote:
> On Mon, Aug 26, 2013 at 02:22:03PM +0800, Robert Yang wrote:
>> Let debugfs do sparse copy when src is a sparse file, just like
>> "cp --sparse=auto"
>>
>> * For the:
>>    #define IO_BUFSIZE 64*1024
>>    this is a suggested value from gnu coreutils:
>>    http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/ioblksize.h;h=1ae93255e7d0ccf0855208c7ae5888209997bf16;hb=HEAD
>>
>> * Use malloc() to allocate memory for the buffer since put 64K (or
>>    more) on the stack seems not a good idea.
>>
>> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
>> Acked-by: Darren Hart <dvhart@linux.intel.com>
>> ---
>>   debugfs/debugfs.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++----
>>   1 file changed, 58 insertions(+), 4 deletions(-)
>>
>> diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
>> index a6bc932..8c32eff 100644
>> --- a/debugfs/debugfs.c
>> +++ b/debugfs/debugfs.c
>> @@ -41,6 +41,16 @@ extern char *optarg;
>>   #define BUFSIZ 8192
>>   #endif
>>
>> +/* 64KiB is the minimium blksize to best minimize system call overhead. */
>> +#ifndef IO_BUFSIZE
>> +#define IO_BUFSIZE 64*1024
>> +#endif
>> +
>> +/* Block size for `st_blocks' */
>> +#ifndef S_BLKSIZE
>> +#define S_BLKSIZE 512
>> +#endif
>> +
>>   ss_request_table *extra_cmds;
>>   const char *debug_prog_name;
>>   int sci_idx;
>> @@ -1575,22 +1585,37 @@ void do_find_free_inode(int argc, char *argv[])
>>   }
>>
>>   #ifndef READ_ONLY
>> -static errcode_t copy_file(int fd, ext2_ino_t newfile)
>> +static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
>>   {
>>   	ext2_file_t	e2_file;
>>   	errcode_t	retval;
>>   	int		got;
>>   	unsigned int	written;
>> -	char		buf[8192];
>> +	char		*buf;
>>   	char		*ptr;
>> +	char		*zero_buf;
>> +	int		cmp;
>>
>>   	retval = ext2fs_file_open(current_fs, newfile,
>>   				  EXT2_FILE_WRITE, &e2_file);
>>   	if (retval)
>>   		return retval;
>>
>> +	if (!(buf = (char *) malloc(bufsize))){
>> +		com_err("copy_file", errno, "can't allocate buffer\n");
>> +		return;
>> +	}
>
> Ugh.  I probably could have whined about this not using ext2fs_get_mem, but
> more importantly clang whines about the undefined return value.
>

Thanks, Darrick:-)

// Robert

> I'll fix it and send out a patch with my next patchbomb.
>
> --D
>> +
>> +	/* This is used for checking whether the whole block is zero */
>> +	retval = ext2fs_get_memzero(bufsize, &zero_buf);
>> +	if (retval) {
>> +		com_err("copy_file", retval, "can't allocate buffer\n");
>> +		free(buf);
>> +		return retval;
>> +	}
>> +
>>   	while (1) {
>> -		got = read(fd, buf, sizeof(buf));
>> +		got = read(fd, buf, bufsize);
>>   		if (got == 0)
>>   			break;
>>   		if (got < 0) {
>> @@ -1598,6 +1623,21 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
>>   			goto fail;
>>   		}
>>   		ptr = buf;
>> +
>> +		/* Sparse copy */
>> +		if (make_holes) {
>> +			/* Check whether all is zero */
>> +			cmp = memcmp(ptr, zero_buf, got);
>> +			if (cmp == 0) {
>> +				 /* The whole block is zero, make a hole */
>> +				retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
>> +				if (retval)
>> +					goto fail;
>> +				got = 0;
>> +			}
>> +		}
>> +
>> +		/* Normal copy */
>>   		while (got > 0) {
>>   			retval = ext2fs_file_write(e2_file, ptr,
>>   						   got, &written);
>> @@ -1608,10 +1648,14 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
>>   			ptr += written;
>>   		}
>>   	}
>> +	free(buf);
>> +	ext2fs_free_mem(&zero_buf);
>>   	retval = ext2fs_file_close(e2_file);
>>   	return retval;
>>
>>   fail:
>> +	free(buf);
>> +	ext2fs_free_mem(&zero_buf);
>>   	(void) ext2fs_file_close(e2_file);
>>   	return retval;
>>   }
>> @@ -1624,6 +1668,8 @@ void do_write(int argc, char *argv[])
>>   	ext2_ino_t	newfile;
>>   	errcode_t	retval;
>>   	struct ext2_inode inode;
>> +	int		bufsize = IO_BUFSIZE;
>> +	int		make_holes = 0;
>>
>>   	if (common_args_process(argc, argv, 3, 3, "write",
>>   				"<native file> <new file>", CHECK_FS_RW))
>> @@ -1699,7 +1745,15 @@ void do_write(int argc, char *argv[])
>>   		return;
>>   	}
>>   	if (LINUX_S_ISREG(inode.i_mode)) {
>> -		retval = copy_file(fd, newfile);
>> +		if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
>> +			make_holes = 1;
>> +			/*
>> +			 * Use I/O blocksize as buffer size when
>> +			 * copying sparse files.
>> +			 */
>> +			bufsize = statbuf.st_blksize;
>> +		}
>> +		retval = copy_file(fd, newfile, bufsize, make_holes);
>>   		if (retval)
>>   			com_err("copy_file", retval, 0);
>>   	}
>> --
>> 1.8.1.2
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-10-15  4:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-26  6:22 [PATCH 0/3 V4] e2fsprogs/debugfs: do sparse copy when src is a sparse file Robert Yang
2013-08-26  6:22 ` [PATCH 1/3 V4] debugfs.c: the max length of debugfs argument is too short Robert Yang
2013-10-14  2:49   ` Theodore Ts'o
2013-08-26  6:22 ` [PATCH 2/3 V4] debugfs.c: do sparse copy when src is a sparse file Robert Yang
2013-10-14  2:49   ` Theodore Ts'o
2013-10-15  3:10   ` Darrick J. Wong
2013-10-15  4:52     ` Robert Yang
2013-08-26  6:22 ` [PATCH 3/3 V4] contrib/populate-extfs.sh: use debugfs to populate extX fs Robert Yang
2013-10-14  2:50   ` Theodore Ts'o

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).