<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Frameworks on KDE Blogs</title><link>https://blogs.kde.org/categories/frameworks/</link><description>Recent content in Frameworks on KDE Blogs</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Fri, 06 Nov 2026 17:11:49 -0500</lastBuildDate><atom:link href="https://blogs.kde.org/categories/frameworks/index.xml" rel="self" type="application/rss+xml"/><item><title>Fixing KIO copy to USB stick and network filesystems</title><link>https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/</link><pubDate>Tue, 27 Oct 2026 00:00:01 +0000</pubDate><author>Méven Car</author><guid>https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/</guid><description>&lt;p&gt;I am going to tell the story of fixing a very high priority bug that was 15 years old. Arguably the worst KIO bug.&lt;/p&gt;
&lt;p&gt;In September 2011, against KDE 4.7, someone opened &lt;a href="https://bugs.kde.org/show_bug.cgi?id=281270"&gt;bug
281270&lt;/a&gt;: &amp;quot;When copying files to a removable disk,
speed and progress reflect copying to the local cache not to the disk itself&amp;quot;. The report notes
the copy notification showed about 50 MB/s to a USB 1.1 device, which cannot go faster than
12 Mbit/s. The reporter had asked a kernel developer, Alan Stern, what was going on, and summed up
his answer:&lt;/p&gt;
&lt;blockquote&gt;
 &lt;p&gt;it shows that the copying operation is finished when it has not even been started.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Seventy-seven comments and 17 duplicates later, that is still how a copy to a USB stick behaves in the
current release of KIO. My &lt;a href="https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/"&gt;last post
about copying&lt;/a&gt; was about making KIO copy faster.
This one is about the opposite case, a destination that is slower than KIO, and about making the
progress bar and copy completion accurate.&lt;/p&gt;
&lt;p&gt;The changes are in four KIO merge requests:
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2506"&gt;MR 2506&lt;/a&gt; finds out which devices are
removable, &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2517"&gt;MR 2517&lt;/a&gt; and
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2518"&gt;MR 2518&lt;/a&gt; flush the device at the end of
a copy or a move, and &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2283"&gt;MR 2283&lt;/a&gt; adds the
writeback window. All of it is merged, and will be in KDE Frameworks 6.32, released &lt;a href="https://community.kde.org/Schedules/Frameworks"&gt;November 13th 2026&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We will see the cause of the bug, its history, and what it took to fix it.&lt;/p&gt;
&lt;h2 id="why-a-copy-finishes-before-it-has-started"&gt;Why a copy finishes before it has started&lt;/h2&gt;
&lt;p&gt;The file worker copies a file by handing its data to &lt;code&gt;write()&lt;/code&gt;. &lt;code&gt;write()&lt;/code&gt; returns as soon as the
data is in the page cache, in memory. The kernel writes it to the device later, when it decides to.
On a local SSD, later means very soon, and you never notice. On a USB 2.0 stick that writes at
2.3 MB/s, it is another story. Here is a 128 MiB file:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/usb-before.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="A 128 MiB copy to a USB 2.0 stick before the change: the copy reports complete after 0.06 to 0.13 s, and the data is on the stick after 68 to 91 s, over four runs." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/usb-before.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For more than a minute, Dolphin considers the copy finished while the stick is still writing. The
progress bar was showing how fast KIO filled memory. Pull the stick out during that minute, and your
data is gone.&lt;/p&gt;
&lt;p&gt;Network filesystems have a different symptom from the same cause. A large copy to an NFS or SMB
share leaves hundreds of MiB in memory to send to the server, and the progress runs far ahead of
it. On SMB it is worse than a wrong number: with the default &lt;code&gt;cache=strict&lt;/code&gt;, looking at a file
waits until its dirty data is written. Listing the folder you are copying into looks at that file.
Over a 5 MB/s link, almost every listing during a 1 GiB copy waited 15 to 20 seconds.&lt;/p&gt;
&lt;h2 id="fifteen-years-in-one-bug-report"&gt;Fifteen years in one bug report&lt;/h2&gt;
&lt;p&gt;The right tool actually showed up very early. Two days after the report, Christoph Feck asked whether there was a
way to learn when the data is really on the disk &amp;quot;without causing massive slow downs that any sync
type calls would cause&amp;quot;. Two weeks later, the reporter passed on Alan Stern's answer,
&lt;code&gt;sync_file_range()&lt;/code&gt;, and Christoph replied that it looked like &amp;quot;exactly what we need&amp;quot;. That was
September 2011, and it is the call KIO now uses.&lt;/p&gt;
&lt;p&gt;Then not much happened for years, apart from users confirming it and seventeen duplicates. In 2021, Nate
Graham raised it to the highest priority, because a move deletes the source files while the copies
are still in memory. In 2022, he changed the Disks &amp;amp; Devices applet, so that ejecting a device
explains that it is still writing. In 2024, Christoph tested the &lt;code&gt;cachestat()&lt;/code&gt; call of Linux 6.5,
which reports how much of a file is still dirty in memory, but only for one file at a time.&lt;/p&gt;
&lt;p&gt;This bug has been my white whale. I have been contributing to KIO since 2018, and I am still learning
how the Linux storage stack behaves underneath it. I tried twice in 2024 and did not get
&lt;code&gt;sync_file_range()&lt;/code&gt; right. In &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/1718"&gt;MR 1718&lt;/a&gt;,
the &lt;code&gt;sync_file_range()&lt;/code&gt; path stayed commented out. In &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/1764"&gt;KIO
MR 1764&lt;/a&gt; and &lt;a href="https://invent.kde.org/system/dolphin/-/merge_requests/862"&gt;Dolphin
MR 862&lt;/a&gt;, the worker waited for the
writeback of the chunk it had just written, which had not started, so it never waited at all. Waiting
for the window before the one just written is what makes it work. I didn't spot the issue, nor any
reviewer unfortunately.&lt;/p&gt;
&lt;p&gt;Since then, another contributor proposed turning off the cache for copies, in
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2162"&gt;MR 2162&lt;/a&gt;, which was closed.&lt;/p&gt;
&lt;h2 id="why-it-took-so-long"&gt;Why it took so long&lt;/h2&gt;
&lt;p&gt;Making the copy correct is the easy part: call &lt;code&gt;fsync()&lt;/code&gt; after every file, or open the destination with
&lt;code&gt;O_DIRECT&lt;/code&gt; and skip the cache. Both make copies several times slower, and they would slow down
the fast local disks too, where nothing was wrong. Making it
correct without making it slow is what took fifteen years, and it needed several things that were
not there before.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A way to wait for part of a file. POSIX only offers &lt;code&gt;fsync()&lt;/code&gt;, which waits for everything.
&lt;code&gt;sync_file_range()&lt;/code&gt; is Linux only, and its own manual calls it &amp;quot;extremely dangerous&amp;quot;: it does not
write the file's metadata, so on its own it guarantees nothing after a crash. It can bound how
much is in memory, but the end of the copy still needs a real flush.&lt;/li&gt;
&lt;li&gt;Knowing which destinations need it, without asking them. The filesystem type does not show
whether a device can be unplugged, and asking a network filesystem anything is a round trip to a
server that may be slow. KIO reads the answer from the
mount table and from sysfs instead, and two earlier changes to &lt;code&gt;KMountPoint&lt;/code&gt; made that cheap
enough to do for every copied file:
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2326"&gt;MR 2326&lt;/a&gt;, in KIO 6.30, caches
mounts by the mount id that &lt;code&gt;statx()&lt;/code&gt; returns since Linux 6.8, instead of parsing
&lt;code&gt;/proc/self/mountinfo&lt;/code&gt; on every lookup. A lookup by id became about 700 times faster.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2487"&gt;MR 2487&lt;/a&gt;, in KIO 6.32, reads a
missing mount with &lt;code&gt;statmount()&lt;/code&gt;, which does not walk any path, so a network server that does
not answer no longer blocks the lookup. In that case, the lookup went from more than 15 seconds
to about 1 ms.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A place to flush at the end of a copy. The file worker copies one file per command, in its own
process, and never knows which file is the last one. Only the &lt;code&gt;CopyJob&lt;/code&gt;, in the application, knows
when the whole copy is over. So the flush needed a new worker command, and a way for the worker to
notify the job that a flush is needed. On the way, I found that &lt;code&gt;FileCopyJob&lt;/code&gt; dropped the metadata
that the worker sent with a copy, so the &lt;code&gt;CopyJob&lt;/code&gt; could never have seen it.&lt;/li&gt;
&lt;li&gt;A window size that fits every destination. A window too small slows a fast link, and a window
too large never fills on a slow one. That is why it now adapts, and why the progress has to stay
monotonic when the window shrinks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="a-writeback-window"&gt;A writeback window&lt;/h2&gt;
&lt;p&gt;Waiting for the device after every write would be correct but very slow. Waiting only at the end
leaves the progress wrong for the whole copy. KIO now does something in between, with
&lt;code&gt;sync_file_range()&lt;/code&gt;, which lets a program start the writeback of part of a file and wait for it.&lt;/p&gt;
&lt;p&gt;The data is written in windows. When a window is full, the worker hands it to a helper thread,
which writes it back and waits for it, while the copy fills the next window. The copy only waits
when the window before the one it hands over is not written yet. So at most two windows of the copy
are ever in memory and not on the device, and the progress bar is never more than one window ahead
of what is really written.&lt;/p&gt;
&lt;p&gt;The helper thread was needed to accommodate CIFS, the kernel client for Samba and Windows shares. On CIFS,
the call that starts the writeback of a window does more than start it: it sends the data to the
server, and waits for the server to grant the credits to do so, blocking the calling thread for about 157 ms per 128 MiB window.
So the copy could not fill the next window while the previous one was sent. With the helper thread making that call, sending a window and
filling the next one overlap.&lt;/p&gt;
&lt;p&gt;A window holds about 200 ms of writeback, at the speed measured on the window before. It starts at
1 MiB, can at most double from one window to the next, and stays below 16 MiB, or 128 MiB on CIFS.
On a fast link the windows grow, so the copy keeps its speed. On a slow link they stay at about
1 MiB, and anything that has to wait for the file waits a fraction of a second, not for the whole
file.&lt;/p&gt;
&lt;h2 id="complete-means-on-the-device"&gt;Complete means on the device&lt;/h2&gt;
&lt;p&gt;A window bounds how much is left in memory, but there is still something left at the end. For a
removable device, KIO now also makes the end of the copy wait for it.&lt;/p&gt;
&lt;p&gt;A file larger than the largest window, 16 MiB, is flushed with &lt;code&gt;fsync()&lt;/code&gt;, and then its folder, before
the copy reports it done. By then the window has already written all but the last two windows, so the flush is
short.&lt;/p&gt;
&lt;p&gt;Small files are the hard part. The obvious fix, an &lt;code&gt;fsync()&lt;/code&gt; for every file, made 3000 files of
16 KiB take 10 seconds instead of 0.04 seconds on a local disk. So the worker flushes no small file
by itself. After a copy to a removable device, it notifies the job, and the &lt;code&gt;CopyJob&lt;/code&gt; sends it one
more command before reporting success: write back the whole filesystem of the destination, with
one &lt;code&gt;syncfs()&lt;/code&gt;. While that runs, the copy notification shows &amp;quot;Writing the copied data to the device...&amp;quot;.
If the flush fails, the copy fails, because the data may not be on the device.&lt;/p&gt;
&lt;p&gt;A move needed more than that. Before, KIO deleted each source file right after its copy, while
the copy could still be only in memory, which is why the bug got its highest priority. Now a move to
a removable device keeps its sources until the flush has succeeded, and deletes them after it. If the
flush fails, or the stick is unplugged during it, every source stays. A source that changed while it
waited for the flush stays too, with a warning. A copy or a move that stops on an error, or that you
cancel, still flushes the device, and a move then deletes only the sources of the files that are on
it.&lt;/p&gt;
&lt;p&gt;Here is the same USB stick again, with the small files added:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/usb.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Copies to a USB 2.0 stick, before and after the change. 128 MiB file: the copy reports complete after 0.06 to 0.13 s before, over four runs, and 28.1 s after; the data is on the stick after 68 to 91 s before and 28.1 s after. 3000 files of 16 KiB: complete after 1.2 s before and 183.9 s after; on the stick after 185.2 s before and 183.9 s after. Progress ahead of the stick: 128 MiB before, 3 MiB after." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/usb.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Yes, the copies now take longer by the clock, but only because the clock now includes the time the
stick spends writing. The time until the data is really on the stick did not get worse. The speed of this stick
varies a lot from run to run, so the 28 seconds against 68 to 91 seconds for the large file is not a
speedup I would count on. The small files never fill a window, so they only depend on the flush at
the end.&lt;/p&gt;
&lt;h2 id="which-destinations-get-a-window"&gt;Which destinations get a window&lt;/h2&gt;
&lt;p&gt;A local fixed disk does not need any of this, and keeps the current behaviour exactly. With the
removable policy forced onto a local disk for testing, the 3000 small files took 1.55 s against
1.54 s before the change.&lt;/p&gt;
&lt;p&gt;Deciding what is removable is less obvious than it sounds. The filesystem type does not expose it: exFAT
can be a memory card, and FAT is also the EFI partition of your laptop. Most USB disks do not even
report removable media. &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2506"&gt;MR 2506&lt;/a&gt; adds a private function to KIO that asks sysfs: the disk's own
removable flag, then the USB port or PCIe slot it hangs off, which the kernel marks removable when
it can be reached from outside the machine, and SD cards. It follows encrypted and LVM volumes down
to the disk under them.&lt;/p&gt;
&lt;p&gt;Network filesystems are easier: every mount that KIO already considers slow gets a window. That is
NFS and CIFS, but also Ceph, GlusterFS, 9p, sshfs, and FUSE filesystems such as rclone. The worker
finds the mount from the mount id of the destination folder, without a round trip to the server.&lt;/p&gt;
&lt;h2 id="numbers-on-network-shares"&gt;Numbers on network shares&lt;/h2&gt;
&lt;p&gt;To measure this, I used KIO's own copy job, comparing KIO master just before the change with KIO
master after it, while another thread listed the destination folder every 50 ms. The SMB server ran on the same machine, with 2 ms of
delay added to its traffic, and for the slow link a limit of 40 Mbit/s, about 5 MB/s.&lt;/p&gt;
&lt;p&gt;On the slow link, the listing is the difference you would feel:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-listing.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Listing a folder during a copy of 256 MiB to an SMB share at 5 MB/s, median seconds. Round 1: 11.3 s before the change, 1.7 s after. Round 2: 5.5 s and 1.7 s." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-listing.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-slow.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Copy of 256 MiB to an SMB share at 5 MB/s, 2 rounds. Time until complete: 54.2 and 54.4 s before, 55.9 and 55.9 s after, 4.95 MB/s against 4.80 MB/s. Progress ahead of the server: 256 MiB before, 1 MiB after. Slowest listing during the copy: 27.4 and 15.3 s before, 1.70 and 1.70 s after." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-slow.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The copy runs about 3% slower with the window. On a fast link, the copy keeps up:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-fast.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Copy of 4 GiB to an SMB share with 2 ms of delay, 5 rounds. Median time until complete: 7.15 s before, 6.06 s after. Largest progress ahead of the server: 1334 MiB before, 128 MiB after. Listing during the copy: median 60.9 ms before and 59.6 ms after, slowest 731 ms before and 111 ms after." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/smb-fast.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the five copies before the change failed with a write error from the share, so these
numbers come from four rounds. The times vary between runs: in an earlier run, the copy with the
window was 4% slower.&lt;/p&gt;
&lt;p&gt;Getting there took me four tries. My first version was 10 to 20% slower on the fast link, because
of the CIFS behaviour described above. Then, with the windows aimed at 500 ms, listings on the slow
link went up to 4.2 seconds, because each request of a listing waits behind the window being sent.
Aiming the windows at 200 ms brought the listings down to 1.7 seconds.&lt;/p&gt;
&lt;p&gt;NFS, on the same machine with 2 ms of delay:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/nfs.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Copy of 4 GiB to an NFS share with 2 ms of delay, medians of 3 rounds. Time until complete: 9.8 s before, 10.4 s after. Progress ahead of the server: 14 to 23 MiB before, 1 to 5 MiB after. Listing during the copy: median 24 ms before and 11 ms after, 95th percentile 63 ms before and 40 ms after." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/nfs.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The copy times are close: 11% slower in the first round, and slightly faster in the two others.&lt;/p&gt;
&lt;h2 id="part-of-a-frozen-dolphin"&gt;Part of a frozen Dolphin&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://bugs.kde.org/show_bug.cgi?id=423187"&gt;Bug 423187&lt;/a&gt; collects many ways in which a slow mount
can freeze Dolphin. In one of its comments, Frank Gehann copied a 100 MB file to an SMB share while
browsing it, and found two causes: Dolphin's main thread waiting for answers from the share, and
folder listings waiting behind the queued upload.&lt;/p&gt;
&lt;p&gt;The window helps with the second cause: each request of a listing now waits behind at most two
windows, about 400 ms of writeback, and a whole listing took 1.7 seconds on the 5 MB/s link instead
of 5 to 11. The &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2487"&gt;&lt;code&gt;statmount()&lt;/code&gt; change&lt;/a&gt;
helps with part of the first one. &lt;code&gt;KFileItem::isSlow()&lt;/code&gt;, which Dolphin calls on its main thread
before it reads a file to find its type, no longer waits for any server, on Linux 6.8 or later. Work is underway to fix the
rest of the first cause in Frank's open
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2484"&gt;MR 2484&lt;/a&gt; and
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2485"&gt;MR 2485&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="the-stick-that-pauses"&gt;The stick that pauses&lt;/h2&gt;
&lt;p&gt;One measurement sent me down a rabbit hole. During a large copy to the USB stick, listing a folder on
the stick sometimes took about 15 seconds, before the change as much as after it. It looked like the
writeback window was making something wait.&lt;/p&gt;
&lt;p&gt;It was the stick. Roughly every 8 seconds, a single 120 KiB write to it took 4 to 7 seconds to
complete, and the listing waited for a page that was stuck behind such a write. KIO cannot fix a
device that stops for seconds at a time, but at least we now know the copy is not to blame. In the runs for this post, the
slowest listing took 3.6 ms during the copy of the large file, but 7 seconds during the copy of the
small files, against 15 seconds before the change.&lt;/p&gt;
&lt;h2 id="copying-between-two-filesystems"&gt;Copying between two filesystems&lt;/h2&gt;
&lt;p&gt;One more thing landed along the way, and this one is a plain speedup. In 2021, in Frameworks 5.88, I replaced &lt;code&gt;sendfile()&lt;/code&gt; with
&lt;code&gt;copy_file_range()&lt;/code&gt; in the file worker, in
&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/602"&gt;MR 602&lt;/a&gt;. At the time, Linux copied with &lt;code&gt;copy_file_range()&lt;/code&gt; across
filesystems too. Linux 5.19 changed that back: between two filesystems, the call fails again with
&lt;code&gt;EXDEV&lt;/code&gt;. Since then, a copy from one filesystem to another went through &lt;code&gt;read()&lt;/code&gt; and &lt;code&gt;write()&lt;/code&gt; in the
worker, one buffer at a time.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2520"&gt;MR 2520&lt;/a&gt; now falls back to
&lt;code&gt;sendfile()&lt;/code&gt; in that case, which copies from one file to another inside the kernel:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/sendfile.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Copy of 1 GiB between two filesystems, medians of 5 rounds. tmpfs to ext4: 443 ms before, 272 ms after. ext4 to tmpfs: 531 ms before, 278 ms after." src="https://blogs.kde.org/2026/10/27/fixing-kio-copy-to-usb-stick-and-network-filesystems/sendfile.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A copy to the USB stick takes as long as before, because its write
speed is the limit. This one is merged too, and will also be in KDE Frameworks 6.32.&lt;/p&gt;
&lt;h2 id="closing"&gt;Closing&lt;/h2&gt;
&lt;p&gt;With all this, a copy to a USB stick now finishes when the data is actually on the stick.
On network filesystems, the progress now follows what the server has received, and the share stays
usable during the copy.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sync_file_range()&lt;/code&gt; is mainly used by databases and storage engines. No other file manager that I
know of uses it. Now KIO, and so Dolphin, uses it too.&lt;/p&gt;
&lt;p&gt;Happy unplugging.&lt;/p&gt;</description></item><item><title>Making KIO copy many files fast</title><link>https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/</link><pubDate>Tue, 28 Jul 2026 00:00:01 +0000</pubDate><author>Méven Car</author><guid>https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/</guid><description>&lt;p&gt;There is a bug in KDE's bug tracker that is almost as old as some of our
contributors: &lt;a href="https://bugs.kde.org/show_bug.cgi?id=342056"&gt;bug 342056&lt;/a&gt;,
&amp;quot;Ridiculously slow file copy (multiple small files)&amp;quot;, reported by Alexander
Nestorov back in 2014. The report is blunt: copying a 15 GB folder of roughly 3
million small files took 5 to 10 hours in KDE, versus about 20 minutes with
&lt;code&gt;rsync&lt;/code&gt;. One commenter summed up the mood:&lt;/p&gt;
&lt;blockquote&gt;
 &lt;p&gt;I generally use cp and rsync instead of dolphin for anything more than a few files.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;That bug has been stuck in my head for a while, so this post is about finally
fixing it. I have been working on KIO's copy path, and I want to walk
through where the time was going, some history that explains why, and show
numbers across KIO versions including a plain &lt;code&gt;cp&lt;/code&gt; for reference.&lt;/p&gt;
&lt;p&gt;At with any optimization, let's measure first:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/offcpu-socket.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Off-CPU flame graph of a KIO 6.28 copy of 5000 small files, with the socket round-trip frames highlighted" src="https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/offcpu-socket.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Where a KIO 6.28 copy of 5000 small files spends its blocking time, and there
is no single hotspot. Each file blocks in a burst of short syscalls: reading the
mount table (&lt;code&gt;/proc/self/mountinfo&lt;/code&gt;), opening the source and destination, and
round-tripping a command to the worker over an internal socket. The socket
round-trips, the send and receive plus the waits for each reply, are the frames
highlighted in red, about 15% of the blocking here, spread as thin slivers
across both threads because every one of the thousands of files pays them. The
frames in green are the real filesystem work the copy cannot avoid: the &lt;code&gt;statx&lt;/code&gt;
and &lt;code&gt;openat&lt;/code&gt; calls, &lt;code&gt;copy_file_range&lt;/code&gt; moving the bytes, and the &lt;code&gt;ext4&lt;/code&gt; metadata
updates beneath them. That per-file storm, not any single call, is what the rest
of this post discusses.
(Off-CPU flame graph from &lt;code&gt;sched:sched_switch&lt;/code&gt;, weighted by number of blocking
switches; click to open the full SVG.)&lt;/em&gt;&lt;/p&gt;
&lt;h2 id="a-little-history"&gt;A little history&lt;/h2&gt;
&lt;p&gt;KIO is the layer behind almost every file operation in KDE software, from Dolphin's copy
and paste to the network transparency that lets you open &lt;code&gt;sftp://&lt;/code&gt; or &lt;code&gt;smb://&lt;/code&gt;
URLs as if they were local. Its design goes back to the KDE 2 days, around the
year 2000. Back then the answer to &amp;quot;how do I do I/O without freezing the UI&amp;quot; was
not a thread but a separate process: a &lt;em&gt;worker&lt;/em&gt; (we used to call them kioslaves)
is launched for a protocol and talks to the application over a socket. This
predates usable threading on Linux (&lt;a href="https://en.wikipedia.org/wiki/Native_POSIX_Thread_Library"&gt;Native POSIX Thread
Library&lt;/a&gt; only landed
in Linux 2.6 in 2003), so processes plus socket IPC was the portable, robust
choice, and it still is for network protocols today: if an &lt;code&gt;smb://&lt;/code&gt; worker
crashes, your file manager does not go down with it.&lt;/p&gt;
&lt;p&gt;The flip side is cost. Every request and its reply are serialized and sent over
that socket, with an event-loop hop on each side. For &lt;code&gt;file://&lt;/code&gt; that overhead
buys you nothing, because there is no untrusted network on the other end, just
the local disk.&lt;/p&gt;
&lt;p&gt;In 2022 David Faure changed that: &lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/740"&gt; Implement running KIO workers in-process
using a thread &lt;/a&gt;
landed in Frameworks 5.95. Since then the &lt;code&gt;file&lt;/code&gt; worker runs on a thread inside
the application instead of as a separate process (other protocols stay
out-of-process for robustness). That removed process launch and context-switch
cost.&lt;/p&gt;
&lt;h2 id="no-socket-between-the-thread-and-the-app-629"&gt;No socket between the thread and the app (6.29)&lt;/h2&gt;
&lt;p&gt;There was still a catch that had been hiding in plain sight: even in-process,
the worker thread and the application talked to each other over a socketpair,
serializing every command as if they were separate processes. Once the thread
was used, that socket was pure overhead.&lt;/p&gt;
&lt;p&gt;That's the red highlight in the first flamegraph.&lt;/p&gt;
&lt;p&gt;So in-process workers now use a real in-memory transport instead of a loopback
socket. A new &lt;code&gt;ThreadConnectionBackend&lt;/code&gt; handles commands, and for reads the actual
data buffer, straight to the peer thread, with no serialization and no syscall,
which makes in-process file reads zero-copy. This is the biggest single jump in
the numbers below, and it has landed on master for 6.29
(&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2279"&gt;kio!2279&lt;/a&gt;).&lt;/p&gt;
&lt;h2 id="next-batching-the-copy-and-the-stat-under-review"&gt;Next: batching the copy and the stat (under review)&lt;/h2&gt;
&lt;p&gt;The changes above make each command cheap. The remaining cost is that there are
still so many of them: copying N files is N separate &lt;code&gt;file_copy&lt;/code&gt; jobs, each one
going through the job scheduler and taking its own round-trip to the worker. For
many small files that fixed per-file cost, not the data, is what dominates.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CopyJob&lt;/code&gt; can dispatch a whole batch of plain local-to-local files to the worker
in one command, and the worker copies them (using &lt;code&gt;copy_file_range&lt;/code&gt;,
and reflink on filesystems like Btrfs or XFS that support it). Each file still
gets its progress signal, its byte accounting, its undo entry, and anything
the worker cannot do blindly — an existing destination, an unreadable source, a
rename or skip decision — is handed back to the normal per-file path. The gate is
conservative on purpose so a hung mount can never freeze the batch
(&lt;a href="https://invent.kde.org/frameworks/kio/-/merge_requests/2282"&gt;kio!2282&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/offcpu-batch.svg"&gt;&lt;figure&gt;
 &lt;img class="img-fluid" alt="Off-CPU flame graph after batch-copy, with the socket round-trip frames gone" src="https://blogs.kde.org/2026/07/28/making-kio-copy-many-files-fast/offcpu-batch.svg"
 style="max-width: 100%; height: auto"
 /&gt;
&lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The same copy on 6.29 with batch-copy: the red socket round-trips are gone. Two
things removed them: the in-memory transport from the previous section (no
socketpair at all) and batch-copy folding a whole run of files into one command
instead of one round-trip each. What is left is real filesystem work: the green
frames are the copy itself, &lt;code&gt;copy_file_range&lt;/code&gt; moving the bytes, the &lt;code&gt;openat&lt;/code&gt; and
&lt;code&gt;statx&lt;/code&gt; calls, and the &lt;code&gt;ext4&lt;/code&gt; metadata beneath them. The wide plateau that is not
green is the benchmark loop deleting each pass's files before the next run
(&lt;code&gt;unlink&lt;/code&gt;), which is teardown rather than part of the copy.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Before copying, &lt;code&gt;CopyJob&lt;/code&gt; stats every source, which is another per-file
round-trip. The follow-up batches the stat phase over the same in-process lane.
This is the &lt;code&gt;batch-copy-stat&lt;/code&gt; column below.&lt;/p&gt;
&lt;p&gt;Both of these are under review and targeted at a release after 6.29, so treat
their columns in the table as a preview of what is coming rather than something
you can install today.&lt;/p&gt;
&lt;h2 id="numbers"&gt;Numbers&lt;/h2&gt;
&lt;p&gt;Methodology: a 13th-gen Intel Core i7-1365U laptop, everything built
&lt;code&gt;RelWithDebInfo&lt;/code&gt; without any sanitizer, copying onto a real ext4 volume (so no
reflink shortcut, &lt;code&gt;copy_file_range&lt;/code&gt; moves real bytes). Each KIO version supplies
both its own &lt;code&gt;libKIOCore&lt;/code&gt; and its own &lt;code&gt;kio_file&lt;/code&gt; worker. Times are the median of
5 runs (3 for the large set) after a discarded warm-up, so caches are warm and
equal for every version. &lt;code&gt;cp -r --preserve=mode,timestamps&lt;/code&gt; is included as the
floor, since that is what the bug reporter reached for instead of Dolphin.&lt;/p&gt;
&lt;p&gt;5000 files of 4 KB each (the many-small-files case from the bug):&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Version&lt;/th&gt;
 &lt;th&gt;Median time&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;cp -r&lt;/code&gt; (coreutils)&lt;/td&gt;
 &lt;td&gt;81 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.28.0&lt;/td&gt;
 &lt;td&gt;1616 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev (master)&lt;/td&gt;
 &lt;td&gt;396 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev + batch-copy&lt;/td&gt;
 &lt;td&gt;88 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev + batch-copy-stat&lt;/td&gt;
 &lt;td&gt;93 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;1000 files of 5 MB each (bulk data, where the copy is I/O-bound):&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Version&lt;/th&gt;
 &lt;th&gt;Median time&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;cp -r&lt;/code&gt; (coreutils)&lt;/td&gt;
 &lt;td&gt;1312 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.28.0&lt;/td&gt;
 &lt;td&gt;1997 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev (master)&lt;/td&gt;
 &lt;td&gt;1687 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev + batch-copy&lt;/td&gt;
 &lt;td&gt;1506 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;KIO 6.29-dev + batch-copy-stat&lt;/td&gt;
 &lt;td&gt;1454 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The small-files case is the story. KIO 6.28 was about 20x slower than &lt;code&gt;cp&lt;/code&gt;,
which is exactly the ratio the 2014 report complained about. Removing the socket
for in-process workers, together with no longer probing the destination
filesystem once per file, takes it from about 1.6 s to 0.4 s (roughly 4x).
Batching the copy takes it to 88 ms, essentially &lt;code&gt;cp&lt;/code&gt; speed and about 18x faster
than 6.28. That &amp;quot;I use cp instead of dolphin&amp;quot; line finally has an answer.&lt;/p&gt;
&lt;p&gt;The large-file case was already well covered before any of this work, and the
table shows it: the copy is bound by moving the bytes, so even KIO 6.28 stayed
close to &lt;code&gt;cp&lt;/code&gt; there, and the per-file optimizations barely move the number
(batch-copy lands within about 15% of &lt;code&gt;cp&lt;/code&gt;). That is the whole reason this
effort targets many small files instead, where the fixed per-file cost, not the
data, is what dominates.&lt;/p&gt;
&lt;p&gt;batch-copy and batch-copy-stat come out tied here: the benchmark copies a single
directory, so its entries arrive in one listing and batch-stat has nothing to
batch. It helps the other case, copying a large selection of individual files
that would otherwise be one stat round-trip each. Since it removes the
round-trip and not the &lt;code&gt;stat&lt;/code&gt; call, the win is largest when stats are cheap, not
when the disk is slow. Read the two as equal on this test.&lt;/p&gt;
&lt;p&gt;And &lt;code&gt;cp&lt;/code&gt; will almost certainly stay a little ahead, by design: KIO does more
than copy the bytes. It reports per-file progress, can be paused and cancelled,
keeps an undo record, preserves permissions and timestamps, resolves conflicts,
and tells any open file manager what changed so its view refreshes. That work is
not free, and the goal was never to beat &lt;code&gt;cp&lt;/code&gt;, only to make the local case fast
enough that reaching for &lt;code&gt;cp&lt;/code&gt; instead of Dolphin stops being the obvious choice.&lt;/p&gt;
&lt;h2 id="caveats-and-what-is-next"&gt;Caveats and what is next&lt;/h2&gt;
&lt;p&gt;These are laptop numbers on one ext4 disk with warm caches, meant to compare
versions, not to be absolute. On Btrfs or XFS the batch path reflinks instead of
copying, which changes the large-file picture entirely. The batch fast path only
engages for plain local-to-local copies on a responsive filesystem; anything
else (network, FAT/NTFS, conflicts needing a dialog) falls back to the existing
per-file path, unchanged.&lt;/p&gt;
&lt;p&gt;Thanks to David Faure for the in-process worker threads that made all of this
possible, to Frank Reininghaus for the earlier listing fixes, and to Alexander
Nestorov for a bug report that aged well enough to still be worth closing.&lt;/p&gt;
&lt;p&gt;That's all, folks.&lt;/p&gt;</description></item></channel></rss>