Commit Graph
94 Commits
Author SHA1 Message Date
Mike Yuan b8df25dcfe xattr-util: try new *xattrat() family syscalls first
Added in https://github.com/torvalds/linux/commit/6140be90ec70c39fa844741ca3cc807dd0866394

However, when O_PATH fds are encountered we'd have to go by
/proc/self/fd/ still, since the kernel people are reluctant
to make the new syscalls work with them
(https://lore.kernel.org/linux-fsdevel/20250206-steril-raumplanung-733224062432@brauner/)
Hence getxattrat() and listxattrat() are not employed.

While at it, remove the discrepancy between path being NULL
and empty - I don't grok the "security issue" claimed earlier,
but nowadays even the kernel treats the two as identical:
https://github.com/torvalds/linux/commit/e896474fe4851ffc4dd860c92daa906783090346
2025-02-09 14:51:04 +01:00
Mike Yuan 7ce2c1bb5f mountpoint-util: rename fd_is_mount_point() to is_mount_point_at()
fd_* functions in our codebase usually mean fd-specific operations,
while this one actually takes openat()-style params. Rename it
accordingly hence.
2025-01-22 01:37:31 +01:00
Lennart Poettering 714b21da88 copy: port things over to XO_REGULAR 2025-01-06 23:37:45 +01:00
Yu Watanabe edd0a3c5e9 copy: do not try to copy zero size data
Hopefully fixes the following sanitizer issue:
===
[ 3754.797377] systemd-journald[776]: ../src/src/shared/copy.c:463:37: runtime error: variable length array bound evaluates to non-positive value 0
[ 3755.101713] systemd-journald[776]: Received SIGTERM from PID 1 (systemd).
[ 3755.124580] systemd-journald[776]: Journal stopped
Found 1 sanitizer issues (0 internal, 0 asan, 0 ubsan, 0 msan).
===
2024-12-10 19:20:50 +00:00
Daan De Meyer b1cfa93080 copy: Introduce COPY_NOCOW_AFTER and use it when copying images
When dealing with copying COW images, we have to make a tradeoff:

- Either we don't touch the NOCOW bit on the copied file COW and get
  an instant copy because we're able to reflink, but we might get
  reduced performance if the source file was COW as COW files and lots
  of random writes don't play well together.
- Or we force NOCOW for the copied file, which means we have to do a
  full copy as reflinking from COW files to NOCOW files or vice versa
  is not supported.

In exec-invoke.c, we've opted for the first option. In nspawn.c and
discover-image.c, we've opted for the second option.

In nspawn, this applies to the --ephemeral option to make ephemeral
copies. In discover-image.c, this applies to cloning images into
/var/lib/machines. Both these features might be used to run many
machines of the same original image. We really don't want to force
a full copy onto users in these scenarios when they're expecting
reflink behavior, leading to them running out of disk space. Instead,
degraded performance in their machines is a much less severe issue,
which they will discover on their own if it affects them, at which
point they can make their original image NOCOW at which point they'll
get both the reflinks and better performance.

Given the above reasoning, let's switch nspawn.c and discover-image.c
to use COPY_NOCOW_AFTER as well instead of enabling NOCOW upfront and
forcing a copy if the original source image is COW.
2024-09-04 19:23:16 +02:00
Daan De Meyer 8af3b12fe0 copy: Copy nocow flag by default
Unless otherwise requested, if we're going to copy a nocow file, make the
target file nocow as well.

Aside from keeping the performance characteristics of the cow or nocow file
intact, reflinking also only works from cow to cow or nocow to nocow files.
Reflinking from cow to nocow or nocow to cow files does not work and can
easily lead to unexpected copies for users, so by keeping the nocow bit
intact across copies by default we also make sure reflinks always work.
2024-09-04 19:23:13 +02:00
Daan De Meyer 74c9606025 Revert "copy: Copy file attributes as well"
This reverts commit 2356104efc.
2024-09-04 18:49:05 +02:00
Daan De Meyer d850a544bc repart: Keep existing directory timestamps intact when copying
Otherwise, when merging multiple directory trees, the output becomes
unreproducible as the directory timestamps will be changed to the current
time when copying identical directories from the second tree.

We introduce a new copy flag to achieve this behavior.
2024-08-30 15:20:40 +01:00
Daan De Meyer 2356104efc copy: Copy file attributes as well
Let's make sure we copy all file attributes that can be copied as
well.
2024-07-25 11:56:05 +02:00
Lennart Poettering 9b2d9b07a1 copy: increase copy buffer from 16K to 64K
In my tests here this tremendously speeds up things when initializing a
1G file from /dev/urandom
2024-06-14 13:44:29 +02:00
Lennart Poettering 0de442ac31 copy: when a progress callback is provided, never copy more than 1M per iteration
Otherwise if we have to fill GB of data we might never call into the
callback, hence put some limit on how much to copy per iteration.
2024-06-14 13:44:29 +02:00
Lennart Poettering 22ed8700c7 copy: rework how we determine the number of bytes to copy in copy_bytes_full()
Let's freshly calculate "m" on each iteration and always start with the maximum
size we can. If sendfile() is used we must adhere to its limit of
SSIZE_MAX minus the current offset. Otherwise we can copy more, i.e.
SSIZE_MAX without any restrictions.

Also, if we get too close to having copied SSIZE_MAX, let's turn off
sendfile() for the rest.
2024-06-14 13:44:28 +02:00
Nick Rosbrook c0bc1e8971 copy: ignore -EOPNOTSUPP from copy_file_range()
According to copy_file_range (2), errno will be set to EOPNOTSUPP when
the file system does not support copy_file_range(). Since there is
already fallback logic in place here for other kinds of errors, add
-EOPNOTSUPP to the list of ignored errors.
2024-04-19 09:57:25 +09:00
Yu Watanabe 72ef2a617f copy: introduce COPY_VERIFY_LINKED flag
If the flag is set, then copy_file() and friends check if the source
file still exists when the copy operation finished.
2024-02-16 03:48:18 +09:00
Yu Watanabe d1553bfd20 copy: use xopenat() to make 'from' argument optional 2024-02-16 00:25:37 +09:00
Yu Watanabe e40b11be04 fs-util: rename xopenat() -> xopanat_full() 2024-02-16 00:25:37 +09:00
Yu Watanabe 0984c3d021 copy: rebreak comment and fix typo 2024-02-15 10:19:02 +09:00
Zbigniew Jędrzejewski-Szmek e3b84b105e shared/copy: rewind dir fd before using it for cleanup
This seems to be the only place where rm_rf_children() is called with a
possibly used fd, which is then passed through to rm_rf_children_impl().

This also fixes #29606.
(Tested on Fedora rawhide with kernel 6.5.6-300.fc39.x86_64.)
2023-10-24 13:37:10 +02:00
Zbigniew Jędrzejewski-Szmek bb44fd0734 various: use _NEG_ macros to reduce indentation
No functional change intended.
2023-08-16 12:52:56 +02:00
Daan De Meyer ad6fae7ffc copy: Add support for creating subvolumes to copy_tree_at()
The subvolumes set is a set of source inodes similar to how the
denylist hashmap contains source inodes as keys. It indicates
directories in the source tree that should become subvolumes in
the target tree.
2023-08-14 18:46:08 +02:00
Michael A Cassaniti c2dfcbd48e file-io: Fix copying sparse files
This change makes sure a data copy using copy_bytes() does not exceed the
max_bytes value when using COPY_HOLES and max_bytes stops before the next
data section.
2023-08-11 14:30:54 +02:00
Khem Raj ac8db36cbc include missing sys/file.h for LOCK_EX 2023-08-02 14:45:27 -07:00
Yu Watanabe b62e2891ef copy: propagate error in fd_copy_directory()
Follow-up for 01428141ba.

Fixes CID#1513190.
2023-06-23 10:11:17 +09:00
Yu Watanabe 440ccf0424 copy: fix fd leak
Follow-up for 01428141ba.
2023-06-23 10:11:06 +09:00
Yu Watanabe dcd407b5e7 copy: add one more assertions about copy_flags
Follow-up for 01428141ba.
2023-06-23 10:07:38 +09:00
Daan De Meyer 01428141ba copy: Add COPY_LOCK_BSD
When making ephemeral copies of files/directories whose cleanup
depends on whether they're locked or not, it's necessary to have the
lock from the very beginning, so let's support that with a new
COPY_LOCK_BSD flag.
2023-06-15 17:14:34 +02:00
Lennart Poettering 27e18c23bc tmpfile-util: turn last parameter of link_tmpfile() into a proper flags
This changes a boolean param into a proper bitflag field.

Given this only defines a single flag for now this doesn't look like
much of an improvement. But we'll add another flag shortly, where it
starts to make more sense.
2023-06-13 09:40:53 +02:00
Zbigniew Jędrzejewski-Szmek 00df55e360 Merge pull request #27882 from DaanDeMeyer/repart-truncate
copy: Add COPY_TRUNCATE
2023-06-07 13:32:11 +02:00
Daan De Meyer f9f70e062d copy: Merge copy_directory() and copy_directory_fd() into copy_directory_at()
Let's merge these two into a single function that can handle both
variants and more.
2023-06-06 14:42:03 +02:00
Daan De Meyer cc45eb592b copy: Add COPY_TRUNCATE
The file might grow more than the amount of data we end up writing,
so let's add a flag to truncate after writing to make sure the file
is only as large as it needs to be.
2023-06-01 12:16:15 +02:00
Lennart Poettering 535358ad2e copy: shortcut reflink_range() to reflink() in some cases 2023-04-28 15:16:30 +02:00
Lennart Poettering 427ad121c2 copy: don't call clone ioctls twice
The btrfs name and the generic name have the same values, hence there's
no point in bothering with the former.
2023-04-28 15:10:24 +02:00
Yu Watanabe 71e84b4be6 copy: rename reflink_full() -> reflink_range()
The commit b640e274a7 introduced reflink()
and reflink_full(). We usually name function xyz_full() for fully
parameterized version of xyz(), and xyz() is typically a inline alias of
xyz_full(). But in this case, reflink() and reflink_full() call
different ioctl().
Moreover, reflink_full() does partial reflink, while reflink() does full
file reflink. That's super confusing.
Let's rename reflink_full() to reflink_range(), the new name is
consistent with ioctl name, and should be fine.
2023-04-28 14:06:00 +09:00
Daan De Meyer b640e274a7 copy: Introduce reflink() and reflink_full()
The kernel has had filesystem independent reflink ioctls for a
while now, let's try to use them and fall back to the btrfs specific
ones if they're not supported.
2023-04-28 00:57:03 +01:00
Yu Watanabe e63d070373 copy: make copy_bytes() support O_PATH fds 2023-04-11 05:17:10 +09:00
Daan De Meyer 427d9c34e6 copy: Add more at() helpers 2023-03-21 20:53:09 +01:00
Lennart Poettering 23e208e742 tmpfile-util: teach link_tmpfile() to optionally replace files 2023-03-03 13:44:12 +01:00
Daan De Meyer b63bd125d4 copy: Support both inode exclusion and contents exclusion
In some cases, we want to exclude a directory's contents but not
the directory itself. In other cases, we want to exclude a directory
and its contents. Let's extend the denylist logic in copy.h to support
both by changing the denylist from a set to hashmap so we can store the
deny type as the value.

We also modify the repart ExcludeFiles= option to make use of this. If
a directory to exclude ends with a "/", we'll only exclude its contents.
Otherwise, we'll exclude the full directory.
2023-02-22 12:44:36 +01:00
Daan De Meyer d9daf0d144 copy: Add copy_file_at() 2023-02-20 12:44:18 +01:00
Daan De Meyer da486c30fe copy: Copy symlink xattrs
Symlinks can have xattrs as well, let's make sure we copy those
as well.
2023-02-14 12:51:40 +01:00
Daan De Meyer c17cfe6ef7 copy: Make copy_xattr() more generic
Let's make copy_xattr() a little more generic in preparation for
copying symlink xattrs.
2023-02-14 12:51:40 +01:00
Yu Watanabe 5bb1d7fbab tree-wide: use -EBADF more 2022-12-21 01:50:33 +09:00
Zbigniew Jędrzejewski-Szmek 254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Zbigniew Jędrzejewski-Szmek 2053593fcc treewide: drop "RUN_" from "RUN_WITH_UMASK"
RUN_WITH_UMASK was initially conceived for spawning externals progs with the
umask set. But nowadays we use it various syscalls and stuff that doesn't "run"
anything, so the "RUN_" prefix has outlived its usefulness.
2022-12-13 14:39:30 +01:00
Daan De Meyer 494f7fc096 copy: Add COPY_GRACEFUL_WARN
When copying between filesystems, sometimes the target filesystem
might not support symlinks/fifos/sockets/... and we want to log and
ignore  any failures to copy such files when copying. Let's introduce
a new flag to enable this behavior.
2022-12-01 10:03:31 +01:00
Daan De Meyer 12e2b70f9b nulstr-util: Declare NULSTR_FOREACH() iterator inline 2022-11-11 16:31:32 +01:00
Daan De Meyer a424958aa6 copy: Support passing a deny list of files/directories to not copy 2022-11-10 16:40:31 +01:00
Alexander Wilson 922409558e copy: Respect COPY_REPLACE flag for copy_tree
- Add a test that asserts that copy_tree on an existing file will fail without COPY_REPLACE
- Add a test that asserts that copy_tree with COPY_MERGE and COPY_REPLACE on an existing directory will overwrite files that already exist.
2022-07-27 08:40:59 -07:00
Alexander Wilson d3e2a7f7e6 copy.[ch]: Refactor
- Refactor: Move HardlinkContext to header file
- Refactor: Create `fd_copy_tree_generic` which isolates the functionality to check stat type and appropriately copy.
- Refactor: Create `fd_copy_leaf` which handles copying leaf nodes of a file tree.
2022-07-27 03:09:35 -07:00
Lennart Poettering db55bbf29b stat-util: fix dir_is_empty() with hidden/backup files
This is a follow-up for f470cb6d13 which in
turn is a follow-up for a068aceafb.

The latter started to honour hidden files when deciding whether a
directory is empty. The former reverted to the old behaviour to fix
issue #23220.

It introduced a bug though: when a directory contains a larger number of
hidden entries the getdents64() buffer will not suffice to read them,
since we just allocate three entries for it (which is definitely enough
if we just ignore the . + .. entries, but not ig we ignore more).

I think it's a bit confusing that dir_is_empty() can return true even if
rmdir() on the dir would return ENOTEMPTY. Hence, let's rework the
function to make it optional whether hidden files are ignored or not.
After all, I looking at the users of this function I am pretty sure in
more cases we want to honour hidden files.
2022-05-04 13:29:14 +02:00