copyfilerange: new command to call copy-file-range
Subsystems (2)
cover copyfilerange: new command to call copy-file-range
Hello!
I finally finished my work on a command to create reflinked files. Since my last post on this mailing list I found the copy_file_range system call and I named this command after it.
I decided to contribute this command first, when this is done I will post another command to find duplicate blocks in two files, which can serve as input of this command.
This command should be usable for other use cases as well, it's also possible to create sparse files and it can be used instead of how many people use dd if= of= skip= count=
I tried to implement everything in a similar way as used by the other sources in this project (and I learned a lot from it).
Nothing is set in stone although I want to keep to the Unix philosophy and not any wild features to this command.
Please let me know what can (or must) be added or improved.
Dick Marinus (1):
copyfilerange: new command to call copy-file-range
.gitignore | 1 + configure.ac | 3 + meson.build | 14 ++ meson_options.txt | 2 + misc-utils/Makemodule.am | 9 + misc-utils/copyfilerange.1.adoc | 78 +++++++ misc-utils/copyfilerange.c | 226 +++++++++++++++++++++ misc-utils/meson.build | 5 + tests/commands.sh | 1 + tests/expected/copyfilerange/copyfilerange | 4 + tests/ts/copyfilerange/copyfilerange | 40 ++++ 11 files changed, 383 insertions(+) create mode 100644 misc-utils/copyfilerange.1.adoc create mode 100644 misc-utils/copyfilerange.c create mode 100644 tests/expected/copyfilerange/copyfilerange create mode 100755 tests/ts/copyfilerange/copyfilerange
Signature · 1 line
2.51.1
PATCH 1/1 copyfilerange: new command to call copy-file-range
This command allows you to call the copy-file range system call which performs an in-kernel copy between two files without the additional cost of transferring data from the kernel to user space and then back into the kernel.
It gives filesystems an opportunity to implement "copy acceleration" techniques, such as the use of reflinks (i.e., two or more inodes that share pointers to the same copy-on-write disk blocks) or server-side-copy (in the case of NFS).
A purpose of this command is to in-place deduplicate equal file blocks in two different files.
When calling this command the source and destination file are required and ranges can be supplied using command line arguments or from a commands file. The source and destination filename can be supplied using the optional --source and --destination options.
A file range contains the offset in the source and destination file and the length of the range in bytes. The offsets can be omitted so that the current file pointer is used. When the size is omitted or set to zero the remainder of the file is copied.
If the file range is too long to fit in SIZE_MAX subsequential calls to copy_file_range will be performed to copy the whole range.
The following command will copy/reflink a file:
$ copyfilerange file1 file2 0::
The following command will copy the second block from file1 into file2:
$ copyfilerange file1 file2 4096:4096:4096
--- .gitignore | 1 + configure.ac | 3 + meson.build | 14 ++ meson_options.txt | 2 + misc-utils/Makemodule.am | 9 + misc-utils/copyfilerange.1.adoc | 78 +++++++ misc-utils/copyfilerange.c | 226 +++++++++++++++++++++ misc-utils/meson.build | 5 + tests/commands.sh | 1 + tests/expected/copyfilerange/copyfilerange | 4 + tests/ts/copyfilerange/copyfilerange | 40 ++++ 11 files changed, 383 insertions(+) create mode 100644 misc-utils/copyfilerange.1.adoc create mode 100644 misc-utils/copyfilerange.c create mode 100644 tests/expected/copyfilerange/copyfilerange create mode 100755 tests/ts/copyfilerange/copyfilerange
.gitignore +1 -0
@@ -99,6 +99,7 @@ ylwrap /colcrt /colrm /column +/copyfilerange /coresched /ctrlaltdel /delpart
configure.ac +3 -0
@@ -2650,6 +2650,9 @@ UL_REQUIRES_HAVE([write], [utmpx_h], [utmpx.h header]) UL_REQUIRES_HAVE([write], [gnu_utmpx], [GNU utmpx functions]) AM_CONDITIONAL([BUILD_WRITE], [test "x$build_write" = xyes]) +UL_BUILD_INIT([copyfilerange], [check]) +UL_REQUIRES_LINUX([copyfilerange]) +AM_CONDITIONAL([BUILD_COPYFILERANGE], [test "x$build_copyfilerange" = xyes]) AC_ARG_WITH([btrfs], AS_HELP_STRING([--without-btrfs], [do not build with btrfs support]),
meson.build +14 -0
@@ -3076,6 +3076,20 @@ if opt and not is_disabler(exe) exes += exe endif +opt = opt and 'copyfilerange' in static_programs +exe = executable( + 'copyfilerange', + copyfilerange_sources, + include_directories : includes, + link_with : [lib_common], + dependencies : [copyfilerange_dep], + install_dir : sbindir, + install : opt, + build_by_default : opt) +if opt and not is_disabler(exe) + exes += exe +endif + exe = executable( 'sample-mkfs', 'libblkid/samples/mkfs.c',
meson_options.txt +2 -0
@@ -259,6 +259,8 @@ option('build-hexdump', type : 'feature', description : 'build hexdump') option('build-findfs', type : 'feature', description : 'build findfs') +option('build-copyfilerange', type : 'feature', + description : 'build copyfilerange') # static programs
misc-utils/Makemodule.am +9 -0
@@ -337,3 +337,12 @@ lsclocks_SOURCES = misc-utils/lsclocks.c lsclocks_LDADD = $(LDADD) libcommon.la libsmartcols.la lsclocks_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir) endif + +if BUILD_COPYFILERANGE +usrbin_exec_PROGRAMS += copyfilerange +MANPAGES += misc-utils/copyfilerange.1 +dist_noinst_DATA += misc-utils/copyfilerange.1.adoc +copyfilerange_SOURCES = misc-utils/copyfilerange.c +copyfilerange_LDADD = $(LDADD) libcommon.la +copyfilerange_CFLAGS = $(AM_CFLAGS) +endif
misc-utils/copyfilerange.1.adoc new file +78 -0
78 lines not on this page · raw message
misc-utils/copyfilerange.c new file +226 -0
226 lines not on this page · raw message
misc-utils/meson.build +5 -0
@@ -229,3 +229,8 @@ lsclocks_sources = files( 'lsclocks.c', ) lsclocks_manadocs = files('lsclocks.1.adoc') + +copyfilerange_sources = files( + 'copyfilerange.c', +) +copyfilerange_manadocs = files('copyfilerange.1.adoc')
tests/commands.sh +1 -0
@@ -78,6 +78,7 @@ TS_CMD_COLCRT=${TS_CMD_COLCRT:-"${ts_commandsdir}colcrt"} TS_CMD_COLRM=${TS_CMD_COLRM:-"${ts_commandsdir}colrm"} TS_CMD_COL=${TS_CMD_COL:-"${ts_commandsdir}col"} TS_CMD_COLUMN=${TS_CMD_COLUMN:-"${ts_commandsdir}column"} +TS_CMD_COPYFILERANGE=${TS_CMD_COPYFILERANGE:-"${ts_commandsdir}copyfilerange"} TS_CMD_CORESCHED=${TS_CMD_CORESCHED:-"${ts_commandsdir}coresched"} TS_CMD_ENOSYS=${TS_CMD_ENOSYS-"${ts_commandsdir}enosys"} TS_CMD_EJECT=${TS_CMD_EJECT-"${ts_commandsdir}eject"}
tests/expected/copyfilerange/copyfilerange new file +4 -0
@@ -0,0 +1,4 @@ +float + +float +aloof
tests/ts/copyfilerange/copyfilerange new file +40 -0
@@ -0,0 +1,40 @@ +#!/bin/bash +# +# Copyright (C) 2025 Dick Marinus <[email protected]> +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +TS_TOPDIR="${0%/*}/../.." +TS_DESC="copyfilerange" + +. "$TS_TOPDIR"/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_CMD_COPYFILERANGE" + +ts_cd "$TS_OUTDIR" + +{ + echo float > a + echo > b + + cat a # float + cat b # empty + + "$TS_CMD_COPYFILERANGE" a b 3::1 1::2 2::1 0::1 5::1 + + cat a # float + cat b # aloof, tribe to Donald Knuth five letter words +} > "$TS_OUTPUT" 2>&1 + +ts_finalize
Signature · 1 line
2.51.1
cover copyfilerange: new command to call copy-file-range
In reply to Dick MarinusHello!
Sorry for the repost, I noticed my previous mail wasn't properly signed and was probably blocked as spam.
I finally finished my work on a command to create reflinked files. Since my last post on this mailing list I found the copy_file_range system call and I named this command after it.
I decided to contribute this command first, when this is done I will post another command to find duplicate blocks in two files, which can serve as input of this command.
This command should be usable for other use cases as well, it's also possible to create sparse files and it can be used instead of how many people use dd if= of= skip= count=
I tried to implement everything in a similar way as used by the other sources in this project (and I learned a lot from it).
Nothing is set in stone although I want to keep to the Unix philosophy and not any wild features to this command.
Please let me know what can (or must) be added or improved.
Dick Marinus (1):
copyfilerange: new command to call copy-file-range
.gitignore | 1 + configure.ac | 3 + meson.build | 14 ++ meson_options.txt | 2 + misc-utils/Makemodule.am | 9 + misc-utils/copyfilerange.1.adoc | 78 +++++++ misc-utils/copyfilerange.c | 226 +++++++++++++++++++++ misc-utils/meson.build | 5 + tests/commands.sh | 1 + tests/expected/copyfilerange/copyfilerange | 4 + tests/ts/copyfilerange/copyfilerange | 40 ++++ 11 files changed, 383 insertions(+) create mode 100644 misc-utils/copyfilerange.1.adoc create mode 100644 misc-utils/copyfilerange.c create mode 100644 tests/expected/copyfilerange/copyfilerange create mode 100755 tests/ts/copyfilerange/copyfilerange
Signature · 1 line
2.51.1
PATCH 1/1 copyfilerange: new command to call copy-file-range
This command allows you to call the copy-file range system call which performs an in-kernel copy between two files without the additional cost of transferring data from the kernel to user space and then back into the kernel.
It gives filesystems an opportunity to implement "copy acceleration" techniques, such as the use of reflinks (i.e., two or more inodes that share pointers to the same copy-on-write disk blocks) or server-side-copy (in the case of NFS).
A purpose of this command is to in-place deduplicate equal file blocks in two different files.
When calling this command the source and destination file are required and ranges can be supplied using command line arguments or from a commands file. The source and destination filename can be supplied using the optional --source and --destination options.
A file range contains the offset in the source and destination file and the length of the range in bytes. The offsets can be omitted so that the current file pointer is used. When the size is omitted or set to zero the remainder of the file is copied.
If the file range is too long to fit in SIZE_MAX subsequential calls to copy_file_range will be performed to copy the whole range.
The following command will copy/reflink a file:
$ copyfilerange file1 file2 0::
The following command will copy the second block from file1 into file2:
$ copyfilerange file1 file2 4096:4096:4096
--- .gitignore | 1 + configure.ac | 3 + meson.build | 14 ++ meson_options.txt | 2 + misc-utils/Makemodule.am | 9 + misc-utils/copyfilerange.1.adoc | 78 +++++++ misc-utils/copyfilerange.c | 226 +++++++++++++++++++++ misc-utils/meson.build | 5 + tests/commands.sh | 1 + tests/expected/copyfilerange/copyfilerange | 4 + tests/ts/copyfilerange/copyfilerange | 40 ++++ 11 files changed, 383 insertions(+) create mode 100644 misc-utils/copyfilerange.1.adoc create mode 100644 misc-utils/copyfilerange.c create mode 100644 tests/expected/copyfilerange/copyfilerange create mode 100755 tests/ts/copyfilerange/copyfilerange
.gitignore +1 -0
@@ -99,6 +99,7 @@ ylwrap /colcrt /colrm /column +/copyfilerange /coresched /ctrlaltdel /delpart
configure.ac +3 -0
@@ -2650,6 +2650,9 @@ UL_REQUIRES_HAVE([write], [utmpx_h], [utmpx.h header]) UL_REQUIRES_HAVE([write], [gnu_utmpx], [GNU utmpx functions]) AM_CONDITIONAL([BUILD_WRITE], [test "x$build_write" = xyes]) +UL_BUILD_INIT([copyfilerange], [check]) +UL_REQUIRES_LINUX([copyfilerange]) +AM_CONDITIONAL([BUILD_COPYFILERANGE], [test "x$build_copyfilerange" = xyes]) AC_ARG_WITH([btrfs], AS_HELP_STRING([--without-btrfs], [do not build with btrfs support]),
meson.build +14 -0
@@ -3076,6 +3076,20 @@ if opt and not is_disabler(exe) exes += exe endif +opt = opt and 'copyfilerange' in static_programs +exe = executable( + 'copyfilerange', + copyfilerange_sources, + include_directories : includes, + link_with : [lib_common], + dependencies : [copyfilerange_dep], + install_dir : sbindir, + install : opt, + build_by_default : opt) +if opt and not is_disabler(exe) + exes += exe +endif + exe = executable( 'sample-mkfs', 'libblkid/samples/mkfs.c',
meson_options.txt +2 -0
@@ -259,6 +259,8 @@ option('build-hexdump', type : 'feature', description : 'build hexdump') option('build-findfs', type : 'feature', description : 'build findfs') +option('build-copyfilerange', type : 'feature', + description : 'build copyfilerange') # static programs
misc-utils/Makemodule.am +9 -0
@@ -337,3 +337,12 @@ lsclocks_SOURCES = misc-utils/lsclocks.c lsclocks_LDADD = $(LDADD) libcommon.la libsmartcols.la lsclocks_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir) endif + +if BUILD_COPYFILERANGE +usrbin_exec_PROGRAMS += copyfilerange +MANPAGES += misc-utils/copyfilerange.1 +dist_noinst_DATA += misc-utils/copyfilerange.1.adoc +copyfilerange_SOURCES = misc-utils/copyfilerange.c +copyfilerange_LDADD = $(LDADD) libcommon.la +copyfilerange_CFLAGS = $(AM_CFLAGS) +endif
misc-utils/copyfilerange.1.adoc new file +78 -0
78 lines not on this page · raw message
misc-utils/copyfilerange.c new file +226 -0
226 lines not on this page · raw message
misc-utils/meson.build +5 -0
@@ -229,3 +229,8 @@ lsclocks_sources = files( 'lsclocks.c', ) lsclocks_manadocs = files('lsclocks.1.adoc') + +copyfilerange_sources = files( + 'copyfilerange.c', +) +copyfilerange_manadocs = files('copyfilerange.1.adoc')
tests/commands.sh +1 -0
@@ -78,6 +78,7 @@ TS_CMD_COLCRT=${TS_CMD_COLCRT:-"${ts_commandsdir}colcrt"} TS_CMD_COLRM=${TS_CMD_COLRM:-"${ts_commandsdir}colrm"} TS_CMD_COL=${TS_CMD_COL:-"${ts_commandsdir}col"} TS_CMD_COLUMN=${TS_CMD_COLUMN:-"${ts_commandsdir}column"} +TS_CMD_COPYFILERANGE=${TS_CMD_COPYFILERANGE:-"${ts_commandsdir}copyfilerange"} TS_CMD_CORESCHED=${TS_CMD_CORESCHED:-"${ts_commandsdir}coresched"} TS_CMD_ENOSYS=${TS_CMD_ENOSYS-"${ts_commandsdir}enosys"} TS_CMD_EJECT=${TS_CMD_EJECT-"${ts_commandsdir}eject"}
tests/expected/copyfilerange/copyfilerange new file +4 -0
@@ -0,0 +1,4 @@ +float + +float +aloof
tests/ts/copyfilerange/copyfilerange new file +40 -0
@@ -0,0 +1,40 @@ +#!/bin/bash +# +# Copyright (C) 2025 Dick Marinus <[email protected]> +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +TS_TOPDIR="${0%/*}/../.." +TS_DESC="copyfilerange" + +. "$TS_TOPDIR"/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_CMD_COPYFILERANGE" + +ts_cd "$TS_OUTDIR" + +{ + echo float > a + echo > b + + cat a # float + cat b # empty + + "$TS_CMD_COPYFILERANGE" a b 3::1 1::2 2::1 0::1 5::1 + + cat a # float + cat b # aloof, tribe to Donald Knuth five letter words +} > "$TS_OUTPUT" 2>&1 + +ts_finalize
Signature · 1 line
2.51.1