Linux-XFS Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] xfs_io: copy_range don't truncate dst_file, and add smart length.
@ 2019-09-06  5:39 Jianhong.Yin
  2019-09-06 16:17 ` Darrick J. Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Jianhong.Yin @ 2019-09-06  5:39 UTC (permalink / raw)
  To: linux-xfs; +Cc: jiyin, darrick.wong, Jianhong.Yin

1. copy_range should be a simple wrapper for copy_file_range(2)
and nothing else. and there's already -t option for truncate.
so here we remove the truncate action in copy_range.
see: https://patchwork.kernel.org/comment/22863587/#1

2. improve the default length value generation:
if -l option is omitted use the length that from src_offset to end
(src_file's size - src_offset) instead.
if src_offset is greater than file size, length is 0.

3. update manpage

and have confirmed that this change will not affect xfstests.

Signed-off-by: Jianhong Yin <yin-jianhong@163.com>
---
 io/copy_file_range.c | 22 +++++-----------------
 man/man8/xfs_io.8    |  9 +++------
 2 files changed, 8 insertions(+), 23 deletions(-)

diff --git a/io/copy_file_range.c b/io/copy_file_range.c
index b7b9fd88..02d50e53 100644
--- a/io/copy_file_range.c
+++ b/io/copy_file_range.c
@@ -66,21 +66,13 @@ copy_src_filesize(int fd)
 	return st.st_size;
 }
 
-static int
-copy_dst_truncate(void)
-{
-	int ret = ftruncate(file->fd, 0);
-	if (ret < 0)
-		perror("ftruncate");
-	return ret;
-}
-
 static int
 copy_range_f(int argc, char **argv)
 {
 	long long src = 0;
 	long long dst = 0;
 	size_t len = 0;
+	int len_ommited = 1;
 	int opt;
 	int ret;
 	int fd;
@@ -112,6 +104,7 @@ copy_range_f(int argc, char **argv)
 				printf(_("invalid length -- %s\n"), optarg);
 				return 0;
 			}
+			len_ommited = 0;
 			break;
 		case 'f':
 			src_file_nr = atoi(argv[1]);
@@ -137,7 +130,7 @@ copy_range_f(int argc, char **argv)
 		fd = filetable[src_file_nr].fd;
 	}
 
-	if (src == 0 && dst == 0 && len == 0) {
+	if (len_ommited) {
 		off64_t	sz;
 
 		sz = copy_src_filesize(fd);
@@ -145,13 +138,8 @@ copy_range_f(int argc, char **argv)
 			ret = 1;
 			goto out;
 		}
-		len = sz;
-
-		ret = copy_dst_truncate();
-		if (ret < 0) {
-			ret = 1;
-			goto out;
-		}
+		if (sz > src)
+			len = sz - src;
 	}
 
 	ret = copy_file_range_cmd(fd, &src, &dst, len);
diff --git a/man/man8/xfs_io.8 b/man/man8/xfs_io.8
index 6e064bdd..8bfaeeba 100644
--- a/man/man8/xfs_io.8
+++ b/man/man8/xfs_io.8
@@ -669,13 +669,10 @@ The source must be specified either by path
 or as another open file
 .RB ( \-f ).
 If
-.I src_file
-.IR src_offset ,
-.IR dst_offset ,
-and
 .I length
-are omitted the contents of src_file will be copied to the beginning of the
-open file, overwriting any data already there.
+is omitted will use ( src_size - 
+.I src_offset
+) instead.
 .RS 1.0i
 .PD 0
 .TP 0.4i
-- 
2.21.0


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

* Re: [PATCH] xfs_io: copy_range don't truncate dst_file, and add smart length.
  2019-09-06  5:39 [PATCH] xfs_io: copy_range don't truncate dst_file, and add smart length Jianhong.Yin
@ 2019-09-06 16:17 ` Darrick J. Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Darrick J. Wong @ 2019-09-06 16:17 UTC (permalink / raw)
  To: Jianhong.Yin; +Cc: linux-xfs, jiyin

On Fri, Sep 06, 2019 at 01:39:27PM +0800, Jianhong.Yin wrote:
> 1. copy_range should be a simple wrapper for copy_file_range(2)
> and nothing else. and there's already -t option for truncate.
> so here we remove the truncate action in copy_range.
> see: https://patchwork.kernel.org/comment/22863587/#1
> 
> 2. improve the default length value generation:
> if -l option is omitted use the length that from src_offset to end
> (src_file's size - src_offset) instead.
> if src_offset is greater than file size, length is 0.
> 
> 3. update manpage
> 
> and have confirmed that this change will not affect xfstests.
> 
> Signed-off-by: Jianhong Yin <yin-jianhong@163.com>
> ---
>  io/copy_file_range.c | 22 +++++-----------------
>  man/man8/xfs_io.8    |  9 +++------
>  2 files changed, 8 insertions(+), 23 deletions(-)
> 
> diff --git a/io/copy_file_range.c b/io/copy_file_range.c
> index b7b9fd88..02d50e53 100644
> --- a/io/copy_file_range.c
> +++ b/io/copy_file_range.c
> @@ -66,21 +66,13 @@ copy_src_filesize(int fd)
>  	return st.st_size;
>  }
>  
> -static int
> -copy_dst_truncate(void)
> -{
> -	int ret = ftruncate(file->fd, 0);
> -	if (ret < 0)
> -		perror("ftruncate");
> -	return ret;
> -}
> -
>  static int
>  copy_range_f(int argc, char **argv)
>  {
>  	long long src = 0;
>  	long long dst = 0;
>  	size_t len = 0;
> +	int len_ommited = 1;

Nit: The correct spelling is "omitted", not "ommited".  As in,

bool len_omitted = true;

(You could also call it "len_specified" since it's a little odd to
declare that it's ommitted before we even parse the arguments but now
we're just splitting hairs...)

>  	int opt;
>  	int ret;
>  	int fd;
> @@ -112,6 +104,7 @@ copy_range_f(int argc, char **argv)
>  				printf(_("invalid length -- %s\n"), optarg);
>  				return 0;
>  			}
> +			len_ommited = 0;
>  			break;
>  		case 'f':
>  			src_file_nr = atoi(argv[1]);
> @@ -137,7 +130,7 @@ copy_range_f(int argc, char **argv)
>  		fd = filetable[src_file_nr].fd;
>  	}
>  
> -	if (src == 0 && dst == 0 && len == 0) {
> +	if (len_ommited) {
>  		off64_t	sz;
>  
>  		sz = copy_src_filesize(fd);
> @@ -145,13 +138,8 @@ copy_range_f(int argc, char **argv)
>  			ret = 1;
>  			goto out;
>  		}
> -		len = sz;
> -
> -		ret = copy_dst_truncate();
> -		if (ret < 0) {
> -			ret = 1;
> -			goto out;
> -		}
> +		if (sz > src)
> +			len = sz - src;
>  	}
>  
>  	ret = copy_file_range_cmd(fd, &src, &dst, len);
> diff --git a/man/man8/xfs_io.8 b/man/man8/xfs_io.8
> index 6e064bdd..8bfaeeba 100644
> --- a/man/man8/xfs_io.8
> +++ b/man/man8/xfs_io.8
> @@ -669,13 +669,10 @@ The source must be specified either by path
>  or as another open file
>  .RB ( \-f ).
>  If
> -.I src_file
> -.IR src_offset ,
> -.IR dst_offset ,
> -and
>  .I length
> -are omitted the contents of src_file will be copied to the beginning of the
> -open file, overwriting any data already there.
> +is omitted will use ( src_size - 
> +.I src_offset
> +) instead.

"If length is not specified, this command copies data from src_offset to
the end of the src_file into the dst_file at dst_offset." ?

--D

>  .RS 1.0i
>  .PD 0
>  .TP 0.4i
> -- 
> 2.21.0
> 

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-06  5:39 [PATCH] xfs_io: copy_range don't truncate dst_file, and add smart length Jianhong.Yin
2019-09-06 16:17 ` Darrick J. Wong

Linux-XFS Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-xfs/0 linux-xfs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-xfs linux-xfs/ https://lore.kernel.org/linux-xfs \
		linux-xfs@vger.kernel.org linux-xfs@archiver.kernel.org
	public-inbox-index linux-xfs


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-xfs


AGPL code for this site: git clone https://public-inbox.org/ public-inbox