JDK-8264744 : (fs) Use file cloning in Linux version of Files::copy method
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 20
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86_64,aarch64
  • Submitted: 2021-04-02
  • Updated: 2022-09-20
  • Resolved: 2022-09-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 20
20 b14Fixed
Related Reports
Blocks :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Modern Linux file systems like XFS, BTRFS and so on support copy-on-write for files. A file copy made using this support to a destination on the same filesystem completes "instantly" and uses no additional disk space. If/when the new or old copy is changed, only then will disk space used go up. This differs from hard-linking because the new file is indistinguishable from a unique, independent file save for the effects on disk space.

Ref-linking is basically always a superior option for file copies when available. For this reason coreutils changed the default for the GNU "cp" command in June 2020, with the remark that it was "long overdue":

https://github.com/coreutils/coreutils/commit/25725f9d41735d176d73a757430739fb71c7d043

Although there was an argument that changing the default in this way may cause problems due to the altered rules around disk space usage, it was eventually decided that the huge speed and storage improvements this feature makes available outweighed it. Given the default copy behavior for Linux shell scripts is now to reflink when possible, Java should follow and do the same thing. 

The underlying kernel API is calling ioctl(FICLONE) as can be seen here:

https://github.com/coreutils/coreutils/blob/e3766c5db176ca7abbb8212d5b0b7862fb98a5be/src/copy.c#L405

A reasonable implementation might be to introduce a new StandardCopyOption called ALWAYS_DUPLICATE. When not specified, COW is used if possible. If specified, COW will never be used, even on a system that supports it. The new behaviour could also be controlled using a system property. This would be backwards compatible and allow apps to opt-out if for some reason it's very important to them that COW not be used (e.g. they want to be sure there will be sufficient free disk space to rewrite the file in place, up front).



Comments
Changeset: da596182 Author: Brian Burkhalter <bpb@openjdk.org> Date: 2022-09-06 18:10:58 +0000 URL: https://git.openjdk.org/jdk/commit/da596182a494a36d37030f18328e52e525fc3565
06-09-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/9486 Date: 2022-07-13 23:25:00 +0000
30-08-2022

On Linux, file cloning is supported by the copy_file_range(2) system call since Linux 4.5 (March, 2016), and more particularly since version 5.3 (September, 2019), and by ioctl(2) with the FICLONE and FICLONERANGE request codes since earlier versions. It is supported for the Btrfs and XFS file systems, among others. https://man7.org/linux/man-pages/man2/copy_file_range.2.html https://man7.org/linux/man-pages/man2/ioctl_ficlonerange.2.html https://github.com/torvalds/linux/blob/0adc313c4f20639f7e235b8d6719d96a2024cf91/fs/read_write.c#L1471 On macOS, file cloning is supported on APFS (since version 10.12, June, 2016) by the clonefile(2) system call and by the copyfile(3) system call when either COPYFILE_CLONE or COPYFILE_CLONE_FORCE is set in the flags parameter.
13-07-2022

Close with future project.
11-05-2021

I am not sure that it is possible to address this suggestion at this time as it requires a Linux kernel of version 4.5 or newer which, insofar as I know, is newer than the oldest kernel that we support. [1] https://www.man7.org/linux/man-pages/man2/ioctl_ficlonerange.2.html
06-04-2021

Potentially something for the JDK-specific ExtendedCopyOption is there is significant interest.
06-04-2021

Moved to JDK to discuss the enhancement.
06-04-2021