file: write back copies to removable devices and network filesystems as they go
FileProtocol::copy() hands its data to write(), which only puts it in the page cache. The
kernel writes it back later, in one go.
- On a USB stick or a memory card, the progress bar shows the speed of filling the cache, and the copy reports success while the data is still in memory. Unplugging the device right after loses data (bug 281270).
- On a network filesystem, a large copy leaves hundreds of MiB to write back. The progress runs
ahead of the server, and other requests to the share, such as listing a folder, wait for that
data. On SMB with
cache=strict, a listing during a copy over a 5 MB/s link took 16 s.
What this does
- A writeback window, written back on a helper thread. The copy hands each full window of
written data to a helper thread, which writes it back with one
sync_file_range()call that waits for it, then drops its pages from the cache. The copy fills the next window meanwhile, and waits only for the window before the one it hands over, so at most two windows are left to write back. Only the helper starts writeback: CIFS sends the data from the thread that starts the writeback, and netfs runs one writeback of a file at a time and drops a start that finds one running. - Windows of about 200 ms. A window holds about 200 ms of writeback at the speed of the window before. It starts at 1 MiB, at most doubles from one window to the next, and stays below 128 MiB on CIFS and 16 MiB elsewhere. On a network filesystem every request to the server waits behind the window being sent, so a smaller target makes listings faster on a slow link. On a fast link 200 ms is more than the largest window.
- Which destinations. Removable devices, and every mount that
KMountPoint::probablySlow()reports: NFS, CIFS, Ceph, GlusterFS, 9p, sshfs, and FUSE filesystems that are not on a device, such as rclone. A local fixed disk keeps the current behaviour. The type comes fromKMountPointby unique mount id, through onestatx()withAT_STATX_DONT_SYNC, so the server of a network filesystem is not asked, and the answer is kept for each mount.KIO_FILE_FORCE_WRITEBACK_SYNCturns the window on for any destination. - Progress from what is written back. With a window, the progress is at most one window ahead of the data written back, so it follows the speed of the device. It never goes back.
- A flush for large files on removable devices. A file larger than the window is flushed with
fsync(), then its folder, before the copy reports it done. The window has already written back all but two windows, so the flush is short. Smaller files are not flushed one by one: a flush per file made 3000 files of 16 KiB take 10 s instead of 0.04 s on a local disk. - A flush command in the file worker. Special command 3 takes a folder. When the folder is on
a removable device, the worker writes back its filesystem with
syncfs(), and says so in an info message. For any other folder it does nothing. - A flush at the end of a copy. After a copy to a removable device, the file worker sets the
flush-destinationmetadata. ACopyJobthat received it sends the flush command for its destination folder before it reports success, and a copy to any other folder does not wait for an extra command.FileCopyJobnow keeps the metadata that the worker sent for its copy, so that theCopyJobsees it. Onesyncfs()covers all the small files of the copy. While it runs, the job shows "Writing the copied data to the device...". An error from the flush is the error of the job, because the data may not be on the device. - Tests: one copies a file several windows long with the window forced, and checks the copy
and the progress. The other copies a folder of small files with and without the removable
policy forced, and checks that only the first one is flushed. The removable case is skipped
outside Linux, which has no
syncfs().
This replaces the earlier versions of this MR, which used cachestat() and an fdatasync() for
every file, and later started each window's writeback on the copy thread.
Measurements
SMB on a fast and a slow link, USB stick, NFS and local disk
KIO's own copy job, master against this MR, built without sanitizers, while another thread lists the destination folder every 50 ms. The SMB server runs on the same machine, with 2 ms of delay added to its traffic on the loopback interface, and for the slow link a limit of 40 Mbit/s, about 5 MB/s.
SMB, fast link (2 ms), 4 GiB file, 5 rounds:
| master | MR | |
|---|---|---|
| time to done | 5.93 / 6.40 / 6.39 / 6.91 / 7.21 s | 6.66 / 6.85 / 5.57 / 6.42 / 6.97 s |
| time to done, median | 6.40 s | 6.66 s |
| progress ahead of the server | up to 2074 MiB | up to 128 MiB |
| listing during the copy, median | 58.8 ms | 58.8 ms |
| listing during the copy, 95th percentile | 89.0 ms | 91.8 ms |
The MR was faster in 3 of the 5 rounds. On SMB, master already reports done only once the data is on the server, so the difference is the progress.
SMB, slow link (2 ms, 40 Mbit/s):
| master, 1 GiB file, 3 rounds | MR, 256 MiB file, 3 rounds | |
|---|---|---|
| time to done | 216.4 / 216.3 / 216.7 s | 56.0 / 55.9 / 56.0 s |
| speed | 4.96 MB/s | 4.80 MB/s |
| progress ahead of the server | 1024 MiB | 1 MiB |
| listing during the copy, median | 16.4 / 16.4 / 15.7 s | 1.70 / 1.70 / 1.70 s |
| listing during the copy, slowest | 20.6 / 19.9 / 20.7 s | 1.70 / 1.72 / 1.70 s |
The two builds copied files of different sizes, so compare the speed and the listings rather than
the times. On master, a listing stats the file being written, and with cache=strict that stat
waits for the dirty data of the file. With the MR, the listing waits only for the window being
sent: an strace of the listing thread showed each directory query waiting about one window, and
the open of the folder about two.
USB 2.0 stick with exfat, which writes at about 2.3 MB/s. Measured with an earlier version of the MR, whose 500 ms windows were started on the copy thread, not rerun yet:
| master | MR | |
|---|---|---|
| 128 MiB file, time to done | 0.06 to 0.07 s | 28 / 54 s |
| 128 MiB file, time until the data is on the stick | 68 to 91 s | same as done |
| progress ahead of the stick | 128 MiB | 1 to 3 MiB |
| 3000 files of 16 KiB, time to done | 1.6 to 2.4 s | 179 to 182 s |
| 3000 files of 16 KiB, time until the data is on the stick | 147 to 180 s | same as done |
With the MR, the copy reports done when the data is on the stick. The stick was recognised as
removable without KIO_FILE_FORCE_WRITEBACK_SYNC. The speed of the stick varies a lot between
runs. The small files never fill a window, so they only depend on the flush at the end.
During a large copy, a listing of a folder on the stick sometimes waits about 15 s, with master
and with fixed windows alike. The stick itself pauses: about every 8 s, a single 120 KiB write
takes 4 to 7 s to complete. The listing waits for the lock of a block device page that holds
directory entries. The kernel keeps that page locked until its write is submitted, and the write
waits for one of the two request slots of the stick, so the listing waits for two pauses in a row.
Turning off writeback throttling (wbt_lat_usec 0) did not change this.
NFS, measured with fixed windows of 16 MiB, before the window followed the speed, not rerun yet. Same machine, 2 ms of delay, medians of 3 rounds:
| NFS master | NFS MR | |
|---|---|---|
| 4 GiB file, time to done | 6.0 s | 5.8 s |
| progress ahead of the server | 14 to 42 MiB | 0 MiB |
| listing during the copy, typical | 31 ms | 13 ms |
| listing during the copy, 95th percentile | 109 ms | 71 ms |
| 3000 files of 16 KiB | 108 s | 79 s |
The NFS small-file times vary from 79 to 109 s between rounds in both builds.
On a local fixed disk the MR does not use the window, and behaves as master.
Measured and not kept:
- Starting each window's writeback on the copy thread, as the previous version did: on CIFS that call sends the window itself, about 157 ms per 128 MiB while it waits for credits from the server, so filling and sending never overlapped. Fast link median 8.9 s against 6.7 s now.
- Moving only that start to a helper thread: netfs dropped most of those starts, because the copy thread's wait for the window before held the writeback of the file. Slower in 5 of 5 rounds.
- Windows of 500 ms with the helper thread: listings on the slow link took 4.2 s, against 1.7 s with 200 ms.
- O_DIRECT with two 1 MiB writes in flight, as proposed for CIFS: 2 to 3 times slower on both SMB and NFS on the fast link.
- Waiting for the previous window without a second write pass: the dirty data was no longer bounded on CIFS.
Known limitations
- On a slow link, the copy runs about 3% slower than master in these runs.
- The flush at the end of a copy writes back the whole filesystem of the device, so it also waits for data that other programs wrote to the device at the same time.
- Only
CopyJobflushes at the end. AKIO::file_copy()of a small file to a removable device is not flushed. A large one is flushed by the worker. - A USB disk that neither its disk nor its port reports as removable gets no window and no flush.
- A FUSE filesystem that writes through, the default, leaves nothing in the page cache, so the window does nothing there; what its daemon caches, such as rclone's upload queue, is out of reach. Ceph and FUSE mounts with a writeback cache were not tested.