Repository navigation
Conversation
Currently uapi16 builds for Linux and for Android. On Android it has the reader, the writer and the stream provider, but neither PinnedDirectory nor DirectoryProvider, since pathrs only has code for Linux. So content.rs and lib.rs are full of #[cfg(target_os = "linux")], the documentation says of both types that they don't exist on Android, and CI lints two Android targets and builds the documentation a second time for one of them. The manifest builder that the next commits add would need more of that: a reopen of its own through procfs where there is no pathrs, a way to take the handle of a file without open_tree(), and a directory for its scratch files where there is no /var/tmp. I don't think we need to really care about Android for now, certainly not enough to build in so many special cases. So let's make the whole crate Linux only: - lib.rs fails the build for every target that isn't Linux. So far it did that for everything but Linux and Android. - pathrs and rustix are dependencies for Linux alone. rustix was one for Android as well. - DirectoryProvider and PinnedDirectory lose their gates, and so does what content.rs only has for them. Their documentation doesn't have to say where they exist anymore. - CI lints twelve targets instead of fourteen and builds the documentation once. - The README says that uapi16 builds for nothing but Linux. Note that a build for an Android target fails with the error of lib.rs now, and with an error for every use of rustix and pathrs after it, since it has neither of them. No functional changes on Linux. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The tests of the manifest builder, which the next commits add, make mounts of their own: bind mounts, a tmpfs over /proc, a file system without O_TMPFILE. They run the test binary again in a user and a mount namespace from unshare -Urm for that, and they skip where unshare can't make the namespaces, so that the suite still passes for a user who may not make them. On the runners that is a problem. AppArmor keeps a program without a profile from using a user namespace there, so every one of these tests would skip, and a test that skips looks like one that passed. So let's turn that restriction off before the tests run, in the four jobs that run them: build, musl, i686 and package. It is the sysctl mkosi's action sets for the same reason (cf. [1]). sysctl gets --ignore since a runner without AppArmor has no such key. Right after it the step runs unshare -Urm true, so that a job fails where the namespaces still can't be made instead of skipping the tests quietly. The file system without O_TMPFILE is a mount of fuse-overlayfs, and the tests that take one skip as well where it can't be mounted. The list of what the ubuntu-26.04 image of the runners has doesn't name fuse-overlayfs as of 20261116, though podman could pull it in. So the four jobs install it, and the step mounts one in a user and a mount namespace right after the unshare, over three empty directories, and unmounts it again. A job fails there where that can't be done, so these tests don't skip for that either. Note that the i686 job runs its tests on the x86_64 kernel with the i386 libraries of the runner. fuse-overlayfs is the runner's own program there as well, so that job installs the same package as the others. Note that this doesn't cover the tests that take an automount point, which the next commits add as well. Only root can make one, so they skip in every job until CI has a job that runs as root. The same goes for the test that takes a file system which keeps a file that was unlinked while it is open, as NFS does. No job mounts one. [1]: https://github.com/systemd/mkosi/blob/66939e9d428ca4f8e9fbc2a46f55a1f131c951f4/action.yaml#L66 Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
FileObject::from_metadata() says that a writer refuses none of the fields it sets, for a manifest that records the same features. That doesn't hold for the owner. A manifest can't have the user and group IDs 65535 and 4294967295, which stand for no ID, and Writer::file() refuses a file object with either. To the kernel 65535 is an ID like any other though, and a file can be owned by it. So from_metadata() made an object of such a file that no writer takes. Instead, leave such an ID out, as we already do for a modification time that mTimeNSec can't express, and as the manifest builder of the later commits does for the files of a tree. The check is the one the writer has. It gets a predicate, FileObject::is_valid_id(), so that both ask the same thing. Note that no stat() gives 4294967295. The kernel shows an owner that it can't map as the overflow ID instead. So the test has a real file for 65535 only. It looks at one of its files from three user namespaces, which unshare(1) makes for anybody: one maps its user to 65535, one its group, and one both. An ID that isn't mapped shows as the overflow ID there, which a manifest can have, and stays. So the user ID and the group ID are each left out for what it is itself, and not for the other. It is skipped where unshare can't do that. The predicate is asked directly for both IDs. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Writer::file(), signature() and finish() all go through send_record().
It has the encoder put the record together and then write_out() writes
it. The encoder takes the record before anything is written. It keeps
the name for the checks of the next object and moves on to the next
record number. Currently, the Writer just carries on when that write
fails. So the encoder has an object that the output doesn't have, or
has only a part of.
This breaks for a caller that writes to a pipe that doesn't block. Say
the pipe is full and file() fails with EAGAIN. A record of at most a
page is written whole or not at all, so the pipe has nothing of it. The
caller waits until the pipe has room and passes the same object again.
Now the Writer refuses it with duplicateName because the encoder has
seen that name. finish() succeeds, and a reader gets a complete
manifest that lacks the object.
A record of more than a page that the pipe has no room for is worse.
The pipe takes a part of it before the write fails. finish() writes the
trailer right behind that part, and a reader fails with notJson.
So let's end the Writer at the first write that fails. It remembers the
number of the record and what the error said, and looks at that before
the encoder is given anything. Every later file(), signature() and
finish() fails with an Io error whose io_error() is ESTALE and whose
record_number() is the record of the write that failed. It names that
write and what it failed with:
the write of record 4 failed earlier: Resource temporarily unavailable (os error 11)
IOW, nothing is written after a write that failed. What the output has
then never ends with a trailer, so no reader takes it for a manifest.
And a caller that tries again on EAGAIN gets ESTALE the next time, not
EAGAIN again.
The call whose write fails returns the error of that write, as before.
An object the Writer refuses doesn't end it. Nothing was written for
it, and the encoder took nothing. begin() returns no Writer when the
root file object can't be written, and finish() consumes the Writer, so
a flush that fails needs nothing. get_ref() and get_mut() still give
the output.
Note that an Io error printed its I/O error and never its message so
far. No Io error had a message. This one has, so Display prints the
message of an Io error that has one. No error kind is new.
The tests cover:
- a full pipe that doesn't block and small records: the EAGAIN of the
write, the same object again once the pipe is empty, signature() and
finish(), that none of them writes, and that a reader of what the
pipe took finds the file objects before and no trailer
- the same with a record that is larger than the pipe, which takes a
part of it
- ENOSPC after a number of bytes, and room again afterwards
The pipe is an anonymous pipe with both ends O_NONBLOCK. The tests only
assume that it takes less than 2 MiB, which is half of the largest
record. They pass with the 64 KiB a pipe has by default and with the
two pages a user with many pipes gets. With 64 KiB pages a pipe has
1 MiB by default, which is still less than 2 MiB. That a refused object
leaves the Writer usable is held by the tests of the largest record and
of the order of the objects already.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A manifest is read and written at two levels. Reader and Writer do their own I/O. Below them, a Decoder is fed the bytes of a manifest and says what comes next with a Step, and an Encoder appends records to a string. Neither does any I/O. None of the users we looked at calls the lower level, and 0.1.0 shouldn't promise what nobody uses. So let's make Decoder, Step and Encoder private to the crate. Reader and Writer stay as they are, on top of the two, and so does what they hand out and take: Object, the root file object and signature objects. Encoder::signature() goes. Writer::signature() doesn't go through it, so nothing would call it. Encoder::begin(), file() and finish(), which append to a string, are only used by the tests until the listing of the manifest builder writes its manifest with them. So they sit in an impl block of their own that expects dead code until then. The crate documentation and the docs of Writer and Object no longer mention the three, and the README no longer says that the crate reads and writes without doing any I/O itself. The doc tests of Decoder and Encoder go as well: a doc test only reaches what is public. Their unit tests stay. While at it, say in the crate documentation what Sha256Context does: it computes the hash of data the caller passes it and does no I/O of its own. The old sentence had it read a file. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A caller that writes a manifest sets all it knows of a file, and the writer refuses an object with a field of a feature the manifest doesn't record. Add FileObject::retain_features() so that such a caller doesn't have to know which field belongs to which feature. It takes away the fields of every feature that isn't among the features it is given: those of the object, of its partition, of its DOS attributes and of its inode attributes, each as its setter does with None. What a reader filled in for such a feature goes as well. The name, the type, the validity window and the contents objects of a file that is no symbolic link belong to no feature and stay. A partition that loses its last field is gone, as a reader has none for a file without any of them. The tests cover: - every feature dropped and kept by itself, the default features, all of them and none - what belongs to no feature, the target of a symbolic link and a partition without a field - what a reader filled in for a feature - that a writer takes what is left of the test's objects, for 65 sets of features Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A tool that copies a sparse file skips its holes, and then it has no bytes to hand to update() for them. Sha256Context::update_zeros() takes a run of zero bytes of any size instead. It hashes the run through a fixed buffer of 4096 zero bytes, so no memory grows with the run. DataCheck::update_zeros() takes such a run for a check. It counts the run against the size of the object as update() counts its data, refuses a run that is too large before any of it is hashed, and hashes it where the object gives a hash. The count and its error move into a helper that the two share. The tests cover: - the hashes of 0, 4096 and 1 MiB zero bytes as sha256sum gives them - runs around one, two and five times the buffer, alone, between other data and split in two, against the same bytes through update() - a sparse layout checked as data and as runs, zero bytes in place of data, too few of them, and a run that is too large, which is refused and not taken - an empty run, and runs of 1 TiB and of u64::MAX on a check without a hash - two runs of u64::MAX, whose count stays at u64::MAX Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A tool that reads the data of a file itself needs the file and not a copy of its data, such as a mkfs tool that skips the holes of a sparse file. PinnedDirectory::open_data() opens the file a file object has its data in, below the directory, the way a DirectoryProvider opens it for a copy: pinned, a regular file, and long enough for the data. The caller reads the data from ExternalFile::offset() on and checks it with a DataCheck. Note that a file is opened with O_NONBLOCK once it is pinned, so that the open waits for nothing. read() and pread() of a regular file don't look at the flag, but io_uring does: on a file system that has no reads that don't wait it fails the read with EAGAIN and doesn't hand it to a worker. So open_data() takes the flag away with F_SETFL before it hands the file out. The file of a copy keeps it. It fails with the errors a copy fails with for the same file. So ExternalFile::check_len() and ExternalFile::unavailable() are visible in the crate now. While at it, let the documentation of PinnedDirectory say both cases of a size mismatch: a file that is too short for the data, and one of another length than ExternalFile::original_size() says. The tests cover: - the file of the object's own name and the one a contents object names, with an offset and a size and without a size - that the caller gets the file open for reading only and without O_NONBLOCK - a file that isn't there, a directory, a FIFO, a symbolic link with and without following, and a file that is too short - an offset and a size whose sum is past u64::MAX, which is a file that is too short as well - two parts of one file with a hash for the whole file, which is left to the caller - a sparse file read without its holes, which go to update_zeros() Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The manifest builder of 0.1.0 walks trees of files on the host, and it fails in ways that reading or writing a manifest doesn't. Let's add what its errors need first, so that the later commits only have to make them. ErrorKind::Tree carries a TreeProblem, which says how the files being added can't be described as asked: the same name for two different files, a file that changed while it was read, two names of one inode that differ, a name a manifest can't hold, a non-directory where a directory is needed, a feature that can't be read or that doesn't apply to a tree, a directory that is its own ancestor, and a parent that no add supplied under features that give a missing field a meaning. TreeProblem is non-exhaustive like the other problems. TreeProblem::as_str() gives each one a stable name, as Problem::as_str() does for the problems of a manifest. ErrorKind is non-exhaustive, so the new kind breaks nobody. An error of the builder says more about itself than the errors so far. So Error gains four accessors: - hardlink_name(), the other name of the inode for a hard-link mismatch - feature(), the feature that can't be read or that was refused - raw_name(), the name of a file that a manifest can't hold, as its directory has it, which need not be UTF-8 - name_problem(), which rule such a name breaks NameProblem gets NotUtf8 for that last one, since the names of files on the host are bytes. A Name is made from a string, so Name::new() never returns it. Name::join_os() does. It is Name::join() for the name of a file on the host, which the walk of the builder needs, and callers that make names of their files get the same checks. name_problem() covers the reader as well. The reader knows why the name of a record or the file a contents object refers to is invalid, and the InvalidName and InvalidContents errors it notes for them keep that NameProblem now. RecordDecoder::note() takes the error to note for the first, and note_error() goes through it. The second is made by Error::invalid_file_reference(), which keeps the problem itself. The writer makes that error for a contents object that refers to the root, which says NameProblem::Empty then. The vectors of refused.jsonl check that an InvalidName has the problem Name::new() gives for the name, and that no other error but those of contents file references has one. The existing kinds, the messages and Error::name() stay as they are. The documentation of name() says what the name is for the errors of the builder. Only name_problem() has something that sets it in this commit. The other three stay empty until the commits that make those errors. Error stays Send, Sync and 'static, which a test now asserts, so that an error can go to another thread. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The manifest builder looks at every file it adds through a descriptor. This adds what it needs for one entry. Nothing outside the tests uses it yet. The system calls go into a module of their own, sys.rs, and everything else into tree.rs. Each call takes the descriptors and the buffers it works on and returns what it found, without any state of its own but what the process found out about O_EMPTYPATH and about open_tree(). The walk, the scratch files and the ioctls of the later commits add theirs to sys.rs as well, so that another way of doing the I/O can take its place later without touching the rest. A system call that fails with EINTR is made again, with rustix's retry_on_intr(), and so is the checked reopen of pathrs. Local file systems never fail these calls with EINTR, but a FUSE server can, when the call is interrupted by a signal. Note that close() is the exception. Linux closes the descriptor also when close() fails with EINTR, and by then the number can be another file's. Nothing in the tests makes a call fail with EINTR, since that takes a FUSE server that is interrupted. - Status::of() runs statx() on a descriptor and keeps what a manifest records and what tells whether a file changed. The request includes STATX_MNT_ID_UNIQUE and STATX_SUBVOL. rustix has no names for those two, so they are named constants made with from_bits_retain(). A kernel before 6.8 passes over STATX_MNT_ID_UNIQUE, and every kernel since 5.8 fills in STATX_MNT_ID unasked, so stx_mask says which of the two IDs we got. MountId keeps them apart. A status without either is an error saying that the kernel is too old. That's how the Linux 5.10 baseline is probed, instead of comparing versions. The error is Tree(FeatureUnavailable) without a feature, so that C will get -EOPNOTSUPP for it, as for a feature that can't be read. - Entry::open_at() opens an entry of a directory as a handle, an O_PATH descriptor, and takes its status through that handle. A symbolic link is the link itself, a FIFO or a device node isn't opened, and the entries below a directory are opened in its handle. The handle is taken with open_tree() and AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLOEXEC. Without OPEN_TREE_CLONE that is an O_PATH open that says outright that nothing is to be mounted. open_tree() is probed and not looked up by version. A kernel before 5.2 fails it with ENOSYS, and a seccomp filter refuses it with ENOSYS or EPERM, as the default filter of a container runtime does for a process that may not mount. Then the handle is taken with openat() and O_PATH|O_NOFOLLOW|O_CLOEXEC instead, which doesn't mount anything for the last component of a name either, and open_tree() isn't called again. We do that whenever open_tree() fails with one of the two, also after it worked, since a process can get its filter at any time. openat() takes the same handle, so an EPERM that is the file's own after all is what openat() fails with as well. - Entry::of() is the entry of a descriptor that a caller gave, a handle or a file that is open for reading. The entry borrows that descriptor. It reads through it, takes no duplicate of it and never closes it. So Entry has a lifetime, and what it holds is a Handle: one that it opened and owns, or one that it borrows. The walk of a later commit takes its directory that way, so it needs no descriptor of its own for it. - Status::changed_since() is the change check: another file, another size, another mtime, another ctime or another link count. The ctime moves with everything else, so it catches an mtime that is set back. Not on FAT, though. A rename over a file changes no time of the file there, only its link count, and an unlink sets its mtime and ctime in steps of two seconds, so one in the same two seconds changes neither. The link count shows both. The comparison of hard links that comes later leaves the ctime and the link count out. - Entry::is_automount() says whether a directory is an automount point that nothing is mounted on yet. The kernel mounts a file system on such a point when the directory is opened by its name or when a path goes through it, and the builder never mounts anything. So an entry has to say so from its handle, before anything is opened, and the later commits neither open such a directory nor look anything up in it. A file system that mounts something on a directory of its own marks the inode, as NFS, SMB, AFS and debugfs do, and statx() reports that as STATX_ATTR_AUTOMOUNT. autofs doesn't. It marks its dentries, and statx() says nothing. So a directory is an automount point as well where fstatfs() on the handle gives AUTOFS_SUPER_MAGIC, as for systemd's CHASE_NO_AUTOFS. Every directory of autofs counts, also the root of an indirect mount: looking a name up in that one asks the daemon to mount it. fstatfs() isn't free. xfs adds up its counters for every CPU, which takes more than 9 us a call on a machine with 512 of them, against 0.2 us for statx(), and NFS asks the server. So only the root of a mount is asked for its file system, which statx() tells with STATX_ATTR_MOUNT_ROOT, or every directory where the kernel doesn't tell. That is enough. A handle is opened in a directory that is no automount point, so that directory isn't on autofs, and what is in it is on its file system unless something is mounted there. A descriptor that a caller gave can be of any directory, so the entry of one is always asked. The tests can't make an automount point, since only root can. So this was tried in a VM, on 7.0 and on 7.3-rc7, with an automount unit of systemd and with "tracing" of a fresh debugfs. Neither the handle nor statx() and fstatfs() on it mount anything, with open_tree() and with openat(). The unit has STATX_ATTR_MOUNT_ROOT and AUTOFS_SUPER_MAGIC, and "tracing" has STATX_ATTR_AUTOMOUNT. A test takes such a point where UAPI16_TEST_AUTOMOUNT names one, and is skipped without it. - DataHash is the hash of the data of a regular file, read a part at a time, since the builder works in bounded steps and a large file has to take several of them. DataHash::read() reads at most the budget it gets into a Sha256Context through the caller's buffer and continues, or breaks with the hash once the data was read up to the size statx() reported. Then it reads one byte more and takes the status again. A file whose status shows a change changed while it was read. One whose data ends early or goes on after its size while its status shows no change gets the same error, Tree(Changed), but its message says that its data doesn't end at its size. That is what a file of procfs or sysfs does, whose size says nothing of its data. For now a regular file of more than 1 TiB isn't hashed. Hashing takes as long as the data is large, a sparse file has data of any size at no cost to whoever made it, and nothing stops a hash once a walk began it. DataHash::admits() fails such a file with EFBIG from its status alone, before it is opened and before anything is read of it, as systemd's sha256_fd() does with its max_size. DataHash::MAX_SIZE is the limit, 2^40 bytes. A file that grows past it while it is read changed, as any file that grows does. An I/O error of a file of the tree is an Error of the kind Io that names the file, with Error::name() and in its message as well, as in "a/closed: Permission denied (os error 13)". Its cause is the error of the system call, so the errno stays. Display prints the message of such an error, as it does for the one of a Writer whose write failed. The data is never opened by name. Between the open of the entry and the read, another file can be renamed over the name, such as a device node of an image with /dev entries. Opening that does whatever its driver does on open, and /dev/watchdog arms the watchdog. So DataHash opens the handle of the entry again, once statx() said it is a regular file, and refuses anything else with EINVAL without opening it. PinnedFile::reopen() does that the way PinnedDirectory has done it so far: through O_EMPTYPATH where the kernel has it, with the probe cached, else through the checked procfs reopen of pathrs. The code moves from directory.rs to sys.rs, so that there is one copy of it, and PinnedDirectory works as before. Nothing but the tests uses most of this until ManifestWriter does, and adding it all together with ManifestWriter would make that commit huge. So the two modules are declared with #[cfg_attr(not(test), expect(dead_code))]. Unlike allow(), expect() warns as soon as nothing in a module is unused any more, so the attributes can't stay around by accident. Each goes in the commit that leaves nothing in its module unused, at the latest in the one that adds ManifestWriter. The tests cover: - the status of every type of file against lstat() - the unique mount ID where the kernel gives one, the ID of /proc/self/mountinfo when it isn't asked for, and no ID at all - a bind mount in a user and mount namespace from unshare -Urm, skipped where unshare can't make one - a file that grows, shrinks, is written over with the same size, or gets its mtime set back after that, also between two steps. The tests wait for the clock of the file system before a change, three seconds at most, since a time of FAT stands still for two. The mtime that is set back is skipped where the change time moves with it, as on FAT - a name that leads to another file, a symbolic link, a FIFO, a directory or nothing - a FIFO renamed over the name before the read, which inotify sees nobody open, and entries that aren't regular files, of which a directory that could be opened isn't opened either - both ways of opening a pinned file again, also after it lost its name - which differences in the status show a change, the link count among them, and what a read comes to with the status after it - a procfs file, whose data goes on after its size of 0, and the message for that - the errors failures become, with the name of the file in the message of an I/O error - buffers from 1 byte to 1 MiB and budgets from 1 byte to all of it - a sparse file of one byte more than is hashed, which fails with EFBIG also where it may not be opened, and one of exactly that size, of which only the first byte is read - handles that openat() takes, as where open_tree() is refused, which are the same handles - the entry of a descriptor of a caller, a handle and a file that is open for reading, which reads through that descriptor and leaves it open - an automount point, whose entry says that it is one and whose handle mounts nothing, with open_tree() and with openat(), where UAPI16_TEST_AUTOMOUNT names one, and a directory that is none - that Status, Entry and DataHash are Send and Sync Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A manifest can record more of a file than statx() tells: its inode
flags, its DOS attributes, whether it is read-only, its extended
attributes and the names of its owners, and for the root of a tree the
label, the UUID and the FAT volume serial number of its file system.
This adds what reads all of that for one entry of a tree. Nothing
outside the tests uses it yet.
Most of these features give a missing field a meaning. Where a manifest
records flag-immutable, a file object without flagImmutable says that
the flag is cleared, and it is the same for the extended attributes and
for the label of a file system. So a feature that was asked for and
that can't be read is an error that names the feature. We never leave
such a field out, because that would claim something nobody read.
Failure::Unavailable carries the feature and the reason, and
Error::about() sets the feature that Error::feature() returns. There
are three exceptions. The name of an owner that the user database
doesn't have is left out and counted, since the specification gives a
missing name no meaning. And whether a file is read-only never fails,
since the mode always tells. systemd does both the same way. The third
is the UUID and the volume serial number of a file system that has
none. They are left out and counted as well, since the specification
leaves a missing one to whoever makes the file system.
FeatureReader in optional.rs puts it together for one entry:
- The inode flags come from FS_IOC_GETFLAGS, for regular files and
directories only. A file system without the ioctl can't say that they
are cleared.
- The DOS attributes come from FAT_IOCTL_GET_ATTRIBUTES. We don't
compare the magic of the file system with that of FAT. We probe
instead. The first regular file or directory of a file system is
asked, and ENOTTY means that the file system has no DOS attributes.
The answer is kept per file system, by mount ID and device, so a file
system without them doesn't get every file opened just to be asked
again. Whatever answers the ioctl is used. In the tree that is fat and
exfat, which has the ioctl since Linux 6.7 and wouldn't be found by
the magic of FAT. Only the four attribute bits we read count,
whatever else an answer has. A file system is only asked where its
answer is used, and the root of a tree has no DOS attributes, as the
root directory of FAT has no directory entry to keep them in.
- readOnly follows systemd. A regular file takes it from the read-only
attribute of DOS where its file system has DOS attributes. Everything
else takes it from the mode: a file that nobody may write is
read-only. The attribute never goes against the mode, since readOnly
must not contradict mode, and fat and exfat take the write permission
away with the attribute anyway. A symbolic link has no mode and gets
none. It is only written where it is true.
- The extended attributes of the user namespace are read for everything
but symbolic links, which can't have any. Nothing is opened for them.
Those of a file that is open for reading anyway are read through that
descriptor, with flistxattr() and fgetxattr(): a directory the caller
opened for its listing, a regular file that was opened for its data,
and either of them where it was opened for its inode flags, its DOS
attributes or the identity of its file system. Those of any other file
are read through its handle, without opening the inode, as systemd
reads them. flistxattr() and fgetxattr() refuse a handle, and so do
listxattrat() and getxattrat() with AT_EMPTY_PATH: they take the
descriptor with fdget(), which doesn't hand out an O_PATH file, and
fail with EBADF. So we go through the link procfs has for the handle,
with listxattr() and getxattr(). Note that it has to be those two.
llistxattr() and lgetxattr() read the link of procfs itself and find
nothing.
FdLink is that link, /proc/thread-self/fd/N. What goes through it is
checked to be the inode of the handle, by its device, its inode number
and its subvolume as statx() gives them, which InodeId holds. The
inode is all that has to be the same, and unlike the whole identity of
a file it takes no mount ID. Note that it is the link of the calling
thread and not /proc/self/fd/N. /proc/self is the thread group leader,
and a thread that has a descriptor table of its own, from
unshare(CLONE_FILES), would get whatever the leader has as descriptor
N there, another file or none.
The link is checked to lead to the file of the handle before anything
is read through it, and once more after. That narrows what a /proc
that isn't what it should be can do, but it doesn't close it: it can
lead the link to the file for the two checks and elsewhere for the
reads in between. Where the file is open anyway that is closed, since
what is read through is the file itself and no path. For any other
file it is a limit we know of, and the documentation of the builder
says so. Without a procfs the attributes of a file that isn't open
can't be read at all, which is FeatureUnavailable.
A file that is gone can look like a /proc that isn't there. The check
of the link fails with ENOENT without a procfs. A FUSE file system
that finds its files by their names answers with ENOENT as well for a
file that was removed, through the link and through the handle alike.
So where the check finds nothing, the handle itself is asked with
statx() once more. ENOENT or ESTALE there is a file that is gone,
which is Changed. Anything else is a /proc that isn't mounted, as
before. That is one call more on an error path and none where the
link is there.
Opening every file for its attributes would close that as well. But
then a file that may not be opened fails its add although it has no
attributes, and every file gets an open that fanotify, a lease or a
FUSE server sees, just to be asked for attributes most files don't
have.
A manifest lists all attributes of a file or none, so a name it can't
hold fails. Attributes that take more than a record has room for fail
as well, before they are all read.
- The names of the owners come from the user database, each ID looked
up once. What was found is kept, also that there is no name, as
systemd's manifest_resolve_name() keeps it. Unlike there the cache is
bounded, so that a tree with millions of owners can't grow it.
- The label, the UUID and the volume serial number of the file system
come from FS_IOC_GETFSLABEL, FS_IOC_GETFSUUID and
FAT_IOCTL_GET_VOLUME_ID on the root of the tree. An empty label is no
label. A label a manifest can't hold fails. The UUID and the serial
number are left out and counted where there is none. That is where
the ioctl fails with ENOTTY, EOPNOTSUPP, ENOSYS or EINVAL, as
FS_IOC_GETFSUUID does on procfs or before Linux 6.9, and
FAT_IOCTL_GET_VOLUME_ID off FAT. And it is where the identifier isn't
a UUID: the 4 bytes fat reports since Linux 6.9 and the 8 of ntfs, or
16 bytes of zeroes. Any other error of the two ioctls fails the walk.
An automount point is never opened, since opening it by its name mounts
what it stands for. So FeatureReader::open() fails for one with
FeatureUnavailable for the feature it was to be opened for, with a
reason of its own, and what is read from the open file can't be read of
it: the inode flags, the DOS attributes and the label of a file system.
The UUID and the serial number of its file system are left out and
counted, as where there is none. Whether it is read-only still follows
its mode, and its extended attributes are read through its handle like
those of any file that isn't open, which mounts nothing.
FdLink::check() takes AT_NO_AUTOMOUNT for that.
The walker of a later commit has to know whether a tree has DOS
attributes before it reads anything else of it.
FeatureReader::check_dos_attributes() asks the file system of a
directory for them through the same cache its first file asks through,
and fails where one of hidden, system and archive is recorded and it
has none. Whether a file is read-only never fails, so it doesn't look
at that.
Two things in here can't be done without unsafe code. Each gets a module
of its own that is allowed to have it. Apart from them and the one cast
of name.rs the crate keeps #![deny(unsafe_code)].
ioctl.rs has the four ioctls rustix has no call for. They go through
rustix's Ioctl trait and rustix::ioctl::ioctl(). An ioctl is whatever
the driver behind a descriptor makes of its number. So the four are only
issued on a regular file or a directory, which the descriptor is
checked to be: theirs go to the file system, where these numbers mean
what the headers say or nothing. The kernel writes its answer into a
value that is zeroed first and that has no padding and takes any bytes,
so it stays a value however much of it the kernel writes. Note that
rustix's Getter can't be used for the label. It reads back all 256 bytes
of a MaybeUninit, and xfs writes 13 of them and ext4 17. The opcode is
made from the type the kernel writes into, so the two can't disagree
about the size.
nss.rs has getpwuid_r() and getgrgid_r(), with a buffer that starts at
what sysconf() says and doubles on ERANGE, like systemd's
getpwuid_malloc(). The two functions point from the entry into the
buffer of the caller. We don't read through such a pointer. It only says
where in the buffer the name starts, and the name is read from the
buffer and has to end in it. A name that isn't UTF-8, that systemd's
relaxed rules for user names refuse or that has what the strings of a
manifest may not have is no name. libc becomes a normal dependency for
this. It was a dev-dependency so far, so Cargo.lock doesn't change.
As in the previous commit, a call that fails with EINTR is made again:
the ioctls, which Readable::answer() makes itself so that this is done
in one place, flistxattr() and fgetxattr(), and listxattr() and
getxattr().
The ioctls need the file open for reading. Entry::reopen() opens a
regular file through the handle of the entry and never through its name,
with PinnedFile::reopen(). Where the kernel has no O_EMPTYPATH, which
Linux has since 7.2, that takes procfs. A directory is opened by its
name in its parent instead, which a Place says:
openat(parent, name, O_RDONLY|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC)
O_DIRECTORY and O_NOFOLLOW refuse anything but a directory with ENOTDIR
before any driver sees an open, and what was opened has to have the
identity of the handle. Otherwise the tree changed, which is
Tree(Changed), as is a name that leads to nothing by then. That takes
one openat() and no procfs. It needs nothing but the permission to read
the directory, which its listing takes anyway, so an empty directory of
mode 0400 opens as well. The one directory without a name is that of a
descriptor a caller gave, such as the top directory of a walk later,
and that one is opened through its handle with PinnedFile::reopen().
Nothing but a regular file or a directory is ever opened, and an
automount point isn't either: Entry::reopen() refuses it with EINVAL as
it refuses a FIFO. DataHash::of() hashes through a file that is open
already, so that a file isn't opened once for its flags and once more
for its data.
Note that the open by name leaves a window. Whether a directory is an
automount point is told when its handle is taken, and the directory is
opened by its name after that. A directory that becomes an automount
point in between has its file system mounted by that open. What comes
back is the root of that file system and not the file of the handle, so
this is Tree(Changed) like any other name that leads elsewhere by then,
but the file system is mounted. That takes someone who can mount in our
mount namespace, or an automount unit that is started at that very
moment. So for now this is a known limit, and the documentation says
so. Where the kernel has O_EMPTYPATH the handle could be opened again
without a lookup, and since Linux 6.18 openat2() with RESOLVE_NO_XDEV
doesn't trigger an automount (cf. [1]). Both are for later.
While at it, give the checks the reader has for the names of owners
and for the names of extended attributes a predicate each that the
builder can ask, FileObject::is_owner_name() and is_xattr_name(), as
the check for IDs has one already. No functional changes for the
reader.
The two FAT ioctls only answer on a mounted FAT file system, and
mounting one takes privileges. So the tests of the real thing only run
where UAPI16_TEST_FAT_DIR names a directory of one, and what is done
with an answer is tested on its own. CI gets a job for them. It makes a
FAT image, mounts it with a regular file, a read-only file and a
directory on it, and points UAPI16_TEST_FAT_DIR there.
The tests cover:
- the four opcodes against the numbers of the kernel headers
- which bits of the DOS attributes count
- labels that are empty, that fill the buffer, that aren't UTF-8 or
have control characters, and what follows the first NUL
- identifiers of every length, and the UUID of zeroes
- that only a regular file and a directory are asked, and what the file
system of the temporary directory and procfs answer
- names against /etc/passwd and /etc/group, IDs nobody has, buffers that
have to grow and one that may not, and pointers that lead out of the
buffer
- inode flags that are set and cleared, on a file, a directory and the
root, held against the flags the file system says each has, and a
file system without them
- a file system without DOS attributes, which is asked once
- readOnly for every mode and every type, and against the attribute
- extended attributes on a file, a directory, a socket and a FIFO, none
of which is opened for them, through a file that is open for reading
and one that is opened for its inode flags, of a file that may not be
opened, after a file lost its name, with names a manifest can't hold,
and more of them than a record takes
- extended attributes with an empty tmpfs over /proc, in a user and
mount namespace from unshare -Urm: those of a directory and a file
that are open for reading, and those of a file that isn't, which
can't be read then. The regular file is left out where the kernel has
no O_EMPTYPATH, since pathrs panics without a procfs. The test asks
the kernel for that itself, with testing::has_empty_path(), since
other tests set what the process found out for a while, and
EmptyPath::FLAG is pub(crate) for it
- the extended attributes of a file that was removed behind a mount of
fuse-overlayfs after its handle was taken, which is a change, skipped
where unshare -Urm or fuse-overlayfs can't mount one, and where the
handle of a file that is gone still has a status
- the extended attributes of a file on fuse-overlayfs whose server was
killed, with an empty tmpfs over /proc, which is a /proc that isn't
there and no change, skipped the same way, and where the handle still
has a status without its server
- a file system that is asked for its DOS attributes once, and not at
all where none of them is recorded
- owners with and without a name, and the bounded cache
- the identity of the file system of the root, for the temporary
directory and for procfs, whose missing label fails and whose missing
UUID and serial number are left out and counted
- which errors of an ioctl say that a file system doesn't have it
- a file opened again after another file took its name, a directory
opened by its name, a name that leads to another directory, to a
file, to a symbolic link or to nothing, which is a change, a directory
that may be read but not searched, one without a name, and that
nothing else is opened
- the link of a descriptor in procfs, which leads to its file also
after that lost its name, and one that leads to another file
- that an automount point isn't opened and that the link of procfs and
the extended attributes mount nothing on it, where
UAPI16_TEST_AUTOMOUNT names one
The check once more after the attributes of a file that isn't open were
read through procfs isn't tested. That takes a /proc that leads
elsewhere between the two checks, and the tests have none. The window
of an automount point isn't tested either, since that takes a mount at
the right moment. So this was tried in a VM, on 7.0 and on 7.3-rc7, with
an automount unit of systemd that is started between the look at a
directory and its open by name: the open mounts the tmpfs of the unit
and comes back as a tree that changed.
A file that is gone when the link is checked takes a FUSE file system
that is asked for its files by their names every time. fuse-overlayfs
with timeout=0 is one, and the jobs of CI have it. So the test takes
the handle of a file there, removes the file in the lower directory
behind the mount, and reads the attributes, which is Changed. Without
the second statx() it was a /proc that isn't mounted. Note that this
needs timeout=0: otherwise the kernel answers both statx() calls from
what it kept, for a second, and the read fails with the ENOENT of
listxattr(). ESTALE isn't tested, since no file system the tests have
says it.
An error that is neither of the two takes a handle that can't be asked
at all. So another test kills the server of such a mount once it has
the handle of a file there. statx() of the handle fails with
ECONNABORTED or ENOTCONN then, which says nothing of the file. With an
empty tmpfs over /proc the read comes back as a /proc that isn't
mounted, and with any error taken for a file that is gone it was
Changed.
This was also tried by hand on 7.4-rc3, on a passthrough on the
high-level interface of libfuse that keeps neither entries nor
attributes, with the name removed or renamed away in its backing
directory right before either call of the first check, for a file of a
walk and for one a caller gave: all eight runs fail with Changed for
the file.
[1]: https://git.kernel.org/torvalds/c/042a60680de43
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The manifest builder sorts the file objects it is given, and it keeps a table of the hard links it has seen. For a large tree neither fits into memory. So they move to disk then, into files without names. This adds those files. The table and the sort that use them come next, and nothing outside the tests uses them yet. A scratch file is made with O_TMPFILE. It never shows up in a directory and it is gone once it is closed, also when the process dies. We pass O_EXCL as well, which for O_TMPFILE means that the file can never be linked into the file system. Only its owner may read and write it. Not every file system has O_TMPFILE. fat doesn't, so a manifest that is created on an ESP runs into this. openat() fails with EOPNOTSUPP then, and with EISDIR on a kernel that doesn't know the flag and tries to open the directory for writing. In both cases we make a file with a name nobody can guess, with O_CREAT|O_EXCL|O_NOFOLLOW, and unlink it right away. That is what systemd's open_tmpfile_unlinkable() does. TemporaryName in sys.rs is such a name, which the new file of a manifest uses later as well. Its open isn't made again where it fails with EINTR. An open that was interrupted can have made the file before it failed, and the same open again would fail with EEXIST on a file of our own then. So the name is removed, and the next one is tried. That doesn't count as one of the 16 names that are tried before the call gives up with EEXIST. Only a name that something else has does, or 16 interrupted opens would say that a name exists where nothing has it. A removal that fails with EINTR is made again, and a name that is gone by then was removed. Every other call of the scratch files is made again on EINTR, as the others are. A scratch file has no name, but a directory can show it all the same. NFS doesn't remove a file that is unlinked while it is open. It renames it to .nfsXXXX and removes that once the file is closed. libfuse does the same for a FUSE file system on its high-level interface, with .fuse_hiddenXXXX, unless the file system sets hard_remove. On those the file that lost its name right away is still in its directory. And that directory can be in a tree the manifest lists: a manifest that is created as a file keeps its scratch files next to it, and TMPDIR can name any directory. A walk that came to such a file would list a file of the builder's own. So every scratch file is one of the files of its builder. OwnFiles in sys.rs has them by their inodes, each with its device, its inode number and its subvolume, as the InodeId of the previous commit has them. A Scratch has one OwnFiles and makes all of its files for it, and own_files() hands out a reference to it. The walker of a later commit looks every entry of a directory up in it and passes over what it finds. That is one lookup per entry in a hash map of a few inodes: the sort that comes later has one scratch file, and the table of the hard links three at most. The inode alone doesn't say that an entry is one of our files, though. A file system can give two files one device and one inode number. A passthrough on libfuse that reports the inode numbers of its backing files does, where another file system is mounted in its backing directory. All of it is one device through the mount, and the two file systems hand out the same numbers. A file of the tree that has the number of one of our files would be passed over then, and the manifest would lack it without a word. With two ext4 images that were made the same way, a manifest that was created on the first one lacked a file of the second one for the name it has until it is complete, and one more for the scratch file of its sort. So OwnFiles keeps each file open next to its inode, and contains() takes the status a walk read of an entry. Where the entry has the inode of one of our files, the status of that open file is read with statx() and the two are held against each other, which Status::matches() does. The type, the mode, the owner, the group, the number of names, the size, the modification time and the change time have to be the same, to the nanosecond, and the birth time where both have one. Only then is the entry passed over. Anything else is a file of the tree and is listed like one. Where that statx() fails, whoever asked fails with its error. Note that the status isn't asked for with AT_STATX_FORCE_SYNC. So a status that is stale can get a file of our own listed, and can't get a file of the tree passed over. A file system that gives two files one number can give it to two of our own files as well. So OwnFiles keeps a list of files for an inode and not a single file. keep() adds to that list, and nothing is ever taken off it. contains() holds the entry against the files on the list one after the other, until one of them is the same. With a single file per inode the second file would take the place of the first one. The first one would be none of our files any more then, and a walk that came to it would list it. With a passthrough on libfuse that reports the inode number 1 for every file, a manifest that was created in the tree it lists listed the name it has until it is complete that way, once its sort had made a scratch file. So that is one statx() for each of our files that has the number of the entry, and none for an entry that has the number of none of them. A builder has few files of its own: its scratch files, four at most as above, and the manifest where it has a name of its own until it is complete. So an entry takes a handful of statx() calls at most, also on a file system that gives every file the same number. On that passthrough a builder with all five took 8 statx() calls for every file it walked, where it takes 3 with the inode numbers of the backing files. A scratch file isn't closed when it is dropped. OwnFiles has the file as well, behind an Arc that it shares with the ScratchFile, and keeps it open until it is dropped itself. That is when its Scratch is dropped. A later commit gives the Scratch to the listing, which the walker has, and from there to what writes the manifest. So a scratch file is open, and one of our files, for as long as a walk can come to it, and then until the manifest is written. Whoever is done with a scratch file gives it back with Scratch::take_back(). That only empties the file, which gives its room back. Scratch::file() hands such a file out again before it makes a new one. So a builder has no more scratch files than it used at one time, whatever it gave back in between. A truncation that fails is ignored. The file stays open as it is then and keeps its room until it is closed, like the room of a hole that can't be punched, and it is never handed out again. Note that a ScratchFile that is dropped and wasn't given back is left as it is. Nothing empties it, and it stays open with the others until they are closed. So a builder that is dropped closes its scratch files and empties none of them first. They have no name, so their room goes back with the last close. A builder that comes to its end gives them back first, though. ManifestWriter::finish() of a later commit does that as it returns, also where it fails, and the generate functions do it where their walk fails. So only a builder that is dropped unfinished closes scratch files that still hold something. The table of the hard links is done with a scratch file every time it doubles on disk. We keep such a file open and don't close it while trees are still walked. A file that was closed can't be one of our files any more. Its inode number is free again, and nothing is left that an entry with the number could be held against. ext4 gave the number to the very next file that was made in the directory, in 200 of 200 tries. xfs gave it out again after 3 to 2913 new files that were made right away, and among the next 35 after a pause of 0.2 s. And once it is none of them, the file systems that rename leave a window. FUSE sends the release of a file without waiting for it, and NFS removes the renamed file with an asynchronous call. So such a name can outlive the close of its file. A walk that comes to it then lists a file of the builder's own, or fails because the file is gone by the time it is opened. With a passthrough on libfuse that answers from several threads the name was still listed right after the close in 2 of 12000 tries. With one that takes 2 ms to release such a file, a builder that closed its scratch files listed some of them in 1986 of 2000 manifests and failed in the other 14, where it walked their directory a few system calls after the table had doubled. A file that stays open stays one of our files, so that can't happen while a builder is there. What is left is the window between two builders. One that walks the directory right after another one was dropped can still find a name of that one's files in it. The files are read with pread() and written with pwrite(), and they aren't mapped. A mapping turns a full disk into a SIGBUS, where pwrite() returns ENOSPC. What was read of a scratch file and isn't read again can go back to the file system before the file is closed. ScratchFile::punch_hole() does that with fallocate(), which takes FALLOC_FL_PUNCH_HOLE only together with FALLOC_FL_KEEP_SIZE. So the file stays as long as it is, and the hole reads as zeroes. A file system gives back whole blocks. What a hole has of a block that isn't all in it is merely written over with zeroes. So ScratchFile::block_size() says how large the blocks of the file are, which it has from statx(). Note that not every file system can punch holes. fat can't and fails with EOPNOTSUPP, and a file system that has no fallocate() at all, such as ramfs, gets the same from the VFS. punch_hole() just reports that, and the file is as it was. What to do about it is up to the sort, which is the one that punches holes. Scratch in scratch.rs says where the files go: - A manifest that is created as a file keeps them in the directory it is created in. That directory has to have room for the manifest anyway. Scratch::beside() takes its descriptor behind an Arc, so that whoever creates the file there shares the descriptor it has with the scratch space, and no second one is taken for it. - Any other manifest keeps them in the directory TMPDIR names, and in /var/tmp without one. We never pick /tmp ourselves. It often is a tmpfs, and what goes into a scratch file is what didn't fit into memory. If TMPDIR points at a tmpfs that is the caller's choice. - A TMPDIR that is set is used as it is. If it can't be opened, or the first file can't be made in it, the error is its own, and we don't fall back to /var/tmp behind the caller's back. Note that systemd's var_tmp_dir() passes over a TMPDIR it can't use. The error names TMPDIR and the directory, and so does the one for /var/tmp where TMPDIR names none, so that the caller knows where to look. The directory is only looked for and opened when the first file is made. So a manifest of a small tree never touches it, and a system without /var/tmp only fails a manifest that needs scratch space, with ENOENT. The system calls are in sys.rs with the others. The errno of that error has to stay, but the code that makes the files hands nothing but I/O errors on. So Error gets io_about(). It makes an I/O error of the same kind that carries an Error of the kind Io with the message, whose cause is the error of the system call. From<io::Error> takes that Error back out, and Display shows its message, as it does for the I/O error of a file of a tree. The tests get TempDir::rerun_on(). It runs a test of the test binary again with a file system mounted over the directory of the test, in a user and a mount namespace from unshare(1), and skips the test where that can't be done. It takes the type of the file system and its mount options, and the line of the skip names the type. Here it mounts a ramfs, a file system that can't punch holes. The table of the hard links and the sort use it later as well. They also get OpenFile::made_in(), which says what a process has open of the files that were made in a directory, with the status of each as a walk finds it, by where procfs says the file of each descriptor was made. That is how a test sees that a file is still open. It takes a handle of every open file of the process for a moment, so one test looks at a time: another one would count that handle as a descriptor of its own files. The tests cover: - that a scratch file has no name, can't be linked in, belongs to its owner alone and isn't inherited across exec, with O_TMPFILE and without - that a scratch file is one of the files of its builder from when it is made, by the inode and the status a walk finds for it, with O_TMPFILE and without, that one that is given back is emptied, that one that is dropped keeps what it has, that both stay open and among them until they are dropped, and that the files of a scratch space are those of its own, also the first one in the directory TMPDIR names - a file that has the inode of one of them and another status, which is none of them, a status of before the file changed, and what makes two statuses with one inode one file, field by field - two of them with one inode, which both are files of the builder and both stay open until the files are dropped, and a file of the tree with that inode, which is none of them - that a file that was given back is handed out again, empty and once, before a new one is made, by its own scratch space alone, and that one that couldn't be emptied, a sealed file in memory, isn't - a file system without O_TMPFILE, /dev/mqueue, where there is one - temporary names, which are removed also once they are gone - reads and writes at any offset, what was never written, and a read past the end - holes that are punched: that they read as zeroes, leave the file as long as it is and give the room of their blocks back, a hole in a part of a block, and a hole of no bytes - a file system that can't punch holes, a ramfs, where one can be mounted - the directory TMPDIR names, an empty TMPDIR, none, and one that isn't there, whose error names it, each in a process of its own - an I/O error that says what it was about, on its way up and back - that nothing is opened before the first file Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The names of one inode share an inodeToken in a manifest. The tokens
are a counter in the order of the manifest, so whoever writes one has
to remember every inode with more than one name it has seen until the
end. The names of an inode can be far apart, and in something like an
ostree deployment every file has a second name outside the tree. So
that is state for as many inodes as the tree has files, and it can't be
kept in memory for a large tree. This adds the table that keeps it. The
manifest builder comes later, and nothing outside the tests uses the
table yet.
A group is what the table keeps for the names of one inode: its key,
its first name, what its names have to agree in, and its token once it
has one. There are two kinds of keys, and they never mix:
- The files of a tree are in one group if statx() gives them the same
mount ID, device, inode number and subvolume. The mount ID is part of
it, so a file reached through a bind mount isn't a hard link of the
file reached through its source.
- The file objects a caller hands in are in one group if they have the
same inodeToken. Those tokens are the caller's own numbers, and the
builder numbers both kinds of groups together when it writes them.
Every name of a group but the first is held against the first, and the
names have to show the same file. For a file of a tree that means the
same type, mode, owner, device numbers, size and modification time, and
the same birth time and hash of the data where both names have one.
The change time stays out, since link() and unlink() of another name
move it. On top of that every group keeps a SHA-256 over the fields the
specification wants to be identical on all file objects of one token:
type, mode, uid, gid, userName, groupName, mTimeNSec, size, sha256,
major, minor, extendedAttributes and the inode flags, as the record of
each name has them. That is all there is to compare for the file
objects of a caller. For the files of a tree it catches what statx()
doesn't show: setxattr() or chattr between two names of an inode moves
nothing but the change time, and without this the two names would go
out under one token with different extended attributes. A name that
doesn't match is Tree(HardlinkMismatch). The error names both names
through Error::linked() and Error::hardlink_name(), which is why a
group keeps its first name. If it is the first name itself that comes
again and doesn't match, the file changed, and that is Tree(Changed).
Any other name of the group that comes again and doesn't match is a
mismatch like a new one, since the first name is the only one a group
keeps.
The table is a hash table of 4 KiB pages with 23 slots each. A group
goes into the page its key hashes to, or into the next page with room
if that one is full, and a full page that had to pass a group on is
marked as overflowed. A lookup only goes on from a page with that mark.
Nothing is ever removed, so there are no tombstones. At three quarters
of its room the table doubles: the old pages are read in order and each
group is placed again in a new table. The keys are hashed with std's
RandomState, which has random keys, so a tree can't pick inode numbers
that all land in one page.
The table starts with 64 KiB in memory. Once the pages and the first
names, with the pages a doubling takes for a while, would need more
than 8 MiB, they are written to two scratch files as they are, and the
table goes on from there. That is at 17664 groups with short names.
From then on memory holds 251 pages of the table in a direct-mapped
cache, a megabyte for reading the old pages when it doubles and 64 KiB
of names that are still to be written. 251 is prime, so the two pages a
page splits into when the table doubles never evict each other. A tree
without hard links never allocates anything here, and a small one never
makes a scratch file.
Groups are kept until the manifest is complete, also once as many names
came as the inode has. Dropping them then would give the room back, and
it would give one inode two tokens whenever the link count is wrong or
changes under us: with a rename into a directory that is still to come,
with a link() during the walk, with a stale count on NFS or one a FUSE
server made up. The specification wants one token for one inode. The
cost is a scratch file that grows by a few hundred bytes for every
inode with more than one name, and there is a note about that in the
code.
Making room is the one thing here that takes long, so it can be done in
parts. make_room() gets a budget of bytes to read and write and says
whether it is done, and the step core of the builder will call it before
it notes a name. The budget isn't exact. A doubling of a table on disk
that begins writes out the pages the cache holds first, about a
megabyte, whatever the budget. note() finishes whatever is left by
itself. Once no more names come settle() finishes what make_room()
began, in parts as well. It begins nothing, so a table that was full
when the last name came doesn't double for a group that never comes.
token() finishes whatever is left by itself, too. A scratch file that
can't be made, read or written is an error that leaves the table as it
was. That includes a doubling that fails half way, on a disk that is
full for example. A doubling only reads the old pages, so the table
drops the new ones and goes back to the old ones, and the next call that
makes room begins the doubling again. The cache of a table on disk goes
to the new pages while it doubles, and it comes back with the old ones.
What comes back from a scratch file is checked like any other input and
never trusted.
On disk the table doubles into a second scratch file and gives the file
of the old pages back to the scratch space once it has doubled. A
doubling that fails gives back the second file instead, and a move to
disk that fails both of its files. None of them is closed there. A
scratch file that is given back is emptied and stays open until the
manifest is written, and the scratch space hands it out again before it
makes a new one, see the commit that adds them. So the next doubling
goes into the file of the pages the table had before the last one. IOW,
a table on disk takes turns between two files for its pages, however
large it gets. It has three descriptors then, for its pages, its names
and the file that is empty, and it makes no file after its first
doubling on disk. Note that the empty file goes to whoever asks the
scratch space next. Where that is the sort, the next doubling makes one
more file. With the file of the sort that is four scratch files for a
builder at most. A doubling can end in settle() or in token() as well,
so those two take the scratch space as make_room() and note() do.
Asking for a token takes I/O once the table is on disk. token() reads
the page of the group from the scratch file where the cache doesn't
hold it, and the page that makes room for it is written out first if it
changed, which it has where one of its groups just got its token. With
many hard links the pages come in no order, so nearly every token is a
page read and a page written. That is 8 KiB for a file object that
counts as about 110 bytes where the manifest is written. Whoever asks
for tokens in steps has to count that, or a step reads and writes about
seventy times its budget. So the table counts the pages it reads and
writes for tokens, and token_cost() says what they come to in bytes of
a budget: PAGE_COST for each, which is the size of a page. A token that
the cache answers costs nothing, and so no token of a table in memory
does. The listing of the next commit takes that out of the budget of
its steps.
The module tree loses its #[expect(dead_code)]. The table uses the last
of what was unused of it, and rustc 1.85 counts a use from a module that
expects dead code itself, so it would call the expectation unfulfilled.
Newer compilers are fine either way.
Noting 1e5, 1e6 and 1e7 names, each inode with two names that are as
far apart as they can be, with the scratch files on xfs:
names time resident table first names
1e5 0.10 s 2.9 MiB 16 MiB 0.9 MiB
1e6 1.4 s 2.9 MiB 128 MiB 9 MiB
1e7 16 s 2.9 MiB 2048 MiB 90 MiB
Resident is RssAnon of the whole test process once the table is on
disk, its memory without the pages of the files it has mapped. It moves
by 16 KiB after that.
The tests cover:
- keys and what is kept of a file, to bytes and back, and bytes that
are neither
- every difference that makes two names two files, and those that don't
- which fields of a file object go into the hash and which don't, and
that extended attributes can't be told apart by moving a byte between
a name and a value
- groups of both kinds against a model, in memory and on disk, through
many doublings and with three pages in memory
- when the table moves to disk, and that long names move it earlier
- first names from memory, from the file and from what isn't written
yet
- tokens in the order they are asked for, across a doubling
- room made a page at a time, and that nothing is done without a budget
- what was begun finished a page at a time once no more names come, and
that nothing begins then
- the errors for a name that doesn't match
- scratch files that were written over or cut short, pages that are
marked as full and aren't, and doublings that fail half way, after
which the table is as it was and doubles again
- that a table on disk takes turns between two files for its pages:
the file of the old pages is empty, open and one of the files of the
builder once the table has doubled, the next doubling goes into it,
and the table has three files while it doubles and after, through
six doublings and until the scratch space is dropped, and the same
for the file of a doubling that failed, which the next one takes
- that a table that is dropped leaves its files as they are, and that
the scratch space closes them
- scratch files that a table took and can't use, in a process of its
own with few descriptors and a limit that prlimit(1) sets: the first
file of a move to disk whose second one can't be made, both files of
a move that can't be written, and the file of each of four doublings
that can't be made as long as it has to be, after each of which the
name that was noted failed, the table is as it was and what it took
is given back, so that it has three files in the end, skipped where
the limit can't be set or fails no write
- the file of a doubling whose old pages can't be written out, with the
scratch files on a tmpfs of 1 MiB that is full for every name, in a
user and mount namespace from unshare -Urm: three doublings fail the
name that was noted with ENOSPC, after each of which the table is as
it was and the file is given back and empty, so that it has three
files in the end, and the first doubling on disk fails nothing, since
its pages are in their file from the move, skipped where unshare
can't make the namespaces or no tmpfs can be mounted in them. The
file that fills the tmpfs is removed however the test ends. Where it
grows past the 1 MiB it is on another file system, and the test
fails there and fills no more of it
- what tokens cost: nothing in memory and where the cache holds every
page, and a page read for nearly every token, and one written for a
new one, where it holds one page, and to the byte for two groups in
two pages: a page for the one that is read, and another for the one
that is written out for it where that one changed
- that the memory of a process that notes 1e5 names stops growing once
the table is on disk, and the same for 1e6 and 1e7 names, which are
ignored unless asked for
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
A manifest lists its file objects in the order of their names. The
manifest builder takes them in any order. A walk hands them over as
readdir() returns them, and add_object() comes in whatever order the
caller has them. No add is refused because of its order. So
everything that is added has to be kept until nothing more comes, and is
written in order then. For a large tree that doesn't fit into memory.
This adds what keeps the file objects and writes them: a sort that moves
to disk, and on top of it the listing, which decides what can only be
decided once the file objects are in order. ManifestWriter comes later,
and nothing outside the tests uses any of this yet.
The sort in sort.rs is an external merge sort. Records gather in 32 MiB
of memory. If the manifest is complete before that, they are sorted
right there and nothing touches a disk. Otherwise they are sorted and
appended to a scratch file as a run, and the next ones gather. In the
end the runs and what is still in memory are merged with a heap, and
each run is read through a buffer of 64 KiB. A merge that couldn't read
a run fails from then on.
A merge takes 512 runs at most, so that the buffers of the runs stay
within another 32 MiB. 512 runs are about 16 GiB of records. A sort
with more runs merges them in passes first. A pass merges runs that
follow each other into one longer run. It appends that run to the
scratch file and puts it where the runs it was made of stood. A pass
doesn't merge every run. It merges just enough of them to leave 512 for
the last merge, in a smaller merge first and then in merges of 512:
runs merges of a pass
600 1 of 89
10000 1 of 291, 18 of 512
262145 1 of 2, and then a pass of 512 of 512
Past 512^2 runs, which is 8 TiB of records, one pass can't leave 512
anymore. It leaves 512^2 then, and the pass after it merges all of
them. So a record is written once and read once, and once more for
every pass it is in.
Making the buffers smaller instead only works for a while. A buffer has
to hold the longest name, which is 4 KiB. So from about 8000 runs on,
or 256 GiB of records, the buffers would take more than the 32 MiB
again, and more with every run.
Records with the same key come out in the order they went in. For that
a merge takes the record of the earlier run where the keys are the
same, and what was still in memory comes last. It also means that a
pass can only merge runs that follow each other, and that the run it
makes has to take their place. GNU sort merges its temporary files in
passes of that kind as well (cf. [1]). It does the full merges first,
though. With our numbers that would write 512 of 600 runs again where
we write 89.
What a merge has read of a run isn't read again. So the merge punches
holes into the scratch file behind what it has read, in the passes and
in the last merge. A run gives its room back a megabyte at a time and
in whole blocks. That keeps the data of the scratch file at about the
room of the records also while a pass writes them a second time, and it
shrinks while the manifest is written. A hole that can't be punched is
never an error, whatever the file system says. It is the last one that
is tried for that file. fat can't punch holes at all, for one. The file
keeps its room until it is closed then, and every pass adds what it
writes.
What the file takes on xfs is more than its data, though. xfs takes room
past the end of a file that grows at its end, up to 8 GiB, for what it
expects to be written there, and it keeps that room until the file is
closed. Once the passes are over nothing is written to the file any
more. So before the last merge begins one more hole goes past the end of
the file, which gives that room back, and it is tried as the others
are. Sorting 2e6 records of 171 bytes in 1337 runs of 256 KiB, four to a
merge and in five passes, the last merge begins with 333 MiB of room
instead of 502 MiB and ends with 6 MiB instead of 175 MiB. 1e7 records
in 52 runs of 32 MiB begin the last merge with 1626 MiB instead of 2047
MiB and end with nothing instead of 421 MiB. 1.2e8 records, whose pass
takes up to 27854 MiB of room for 19711 MiB of data, begin the last
merge with 19606 MiB instead of 25044 MiB and end with 2 MiB instead of
5440 MiB. Their last merge takes 30 s with the hole and without it.
The FAT job of CI sorts on FAT as well, with TMPDIR on the FAT file
system. FAT has no O_TMPFILE and can't punch holes, so that covers the
scratch file with a name that it loses right away and the merge that
keeps the room of its runs.
The sort compares its keys as bytes. Note that the order of a manifest
isn't the order of the bytes of the names. A directory comes right
before what is below it, so the separator sorts before every other
byte. A name has no NUL, so the key of a file object is its name with a
NUL for every '/'.
What the sort keeps of a file object is written by record.rs, in bytes
and not in JSON. A plain file object takes five bytes next to its name,
and what an object doesn't have takes no room. The bytes of an object
are always the same, and what comes back from a scratch file is checked
like any other input. They are written up to a limit, so an object that
is larger than anything a manifest holds doesn't take memory for its
bytes first. A file of a tree with more names than one also keeps which
inode it is, for its inodeToken, and a regular file whose object has no
size keeps the size statx() gave, which the report counts.
The listing in listing.rs takes the root of a tree, the files of a tree
and the file objects a caller made, and hands them to the sort. Hard
links are noted in the table of the previous commit when they are
added, so a name that doesn't match the first name of its inode fails
right away. Everything else waits until the manifest is written. Only
then every file object is next to the others of its name and behind its
directory:
- The same name twice. Two directories are one directory, and the first
one that was added says what it is like. Two files of a tree are one
file if their file objects are equal. If they aren't, the file
changed, which is Tree(Changed), since a walk only comes to a name
twice where its directory changed while it was listed. Where two
files of a tree are one and only one of them keeps which inode it is,
that one stays, whichever came first. Two file objects of a caller
are one if they are equal. Anything else is two files with one name,
and that is Tree(Duplicate).
- A directory that wasn't added. If files were added below it we make
up a plain directory that says nothing but its type. That stops
working once the manifest records read-only, one of the flags or user
extended attributes. A missing field means something there, namely
not read-only, flag cleared or no attributes, and nobody read that
from anywhere. So that is Tree(MissingParent) and names the
directory. A root that wasn't added is made up the same way, and for
the root fs-label counts as well. fs-uuid and fat-serial don't. A
root need not have a UUID or a serial number, and one that is made up
has neither.
- A file where a directory is needed. Files that were added below
something that isn't a directory are Tree(NotDirectory), and the
error names what isn't a directory.
- The inodeTokens. They are handed out now, in the order of the
manifest, from one counter for the inodes of a tree and for the
tokens of a caller's file objects. A file that was added with one
name and again after it got a second one has the token.
- Whatever a writer refuses. The records go through the Encoder, and
the error names the object. A file object of a caller is checked when
it is added already, as Writer::file() checks it but for where it
comes: the size of its record, its strings, its fields and their
features. FileObject::check_alone() is those checks of the Encoder,
which runs them in the same order as before. So an object a writer
refuses is never added, and a second directory object for a name,
which the merge would drop for the first one, can't hide a fault
there. Where an object comes shows when the manifest is written. Two
kinds of objects are refused before that, as they can't even be
checked cheaply. One without a name would be taken for the root, and
one with more contents objects than a manifest may have takes far
more memory than its bytes do. One that takes more than 8 MiB, twice
what a record of a manifest may have, isn't kept either, which
refuses it before its record is written out in full. No object a
writer takes comes near that.
None of this does an unbounded amount of work at once. A Listing has
make_room(), which writes a run or grows the table for a budget of bytes
and says whether it is done. An add finishes whatever is left by itself.
A step can go past its budget, by a bounded amount. The records in
memory are sorted in one go before they go out as a run, 32 MiB of them
at most, and so are the last of them in the end. A step of a merge reads
what the buffers of its runs need when they run low on top of its
budget, a buffer of every run at most. And the step of a Writing in
which what is left of the table or the passes is done goes on with the
next thing for the whole budget. Once nothing more comes the listing
turns into a Writing, which has step(). Turning it into one does no
work. A sort can end with a run half written, and what it has in memory
isn't sorted yet, and both are left to the steps. A step does one thing
at a time, takes a record from the sort or appends one to the manifest,
and returns once its budget is used up. A hard link is appended with
its token, and what the table of the hard links reads and writes for
that token comes out of the budget as well, see token_cost() of the
previous commit. So a step over a table on disk appends as many hard
links as its budget pays the pages for. A name with 2047 directories
that have to be made up takes 2047 turns. What the sort has left to do
runs in the same steps. Merge::prepare() writes the rest of that run,
sorts what is in memory once the run is written, and then does the
passes. It reads and writes for a budget of bytes and says whether it is
done, and step() calls it before it takes the first record, for as many
steps as that needs. The manifest is appended to a String and never
written anywhere from here, so whoever drives this decides where it goes
and when to stop. For the manifest that is the sans-I/O shape the
Decoder and the Encoder of this crate already have, and that rustls and
quinn-proto use for their protocol cores. After an error and after the
end every step fails. So a caller that goes on by mistake can't end a
manifest that lacks what failed.
The listing has the scratch space of its manifest. So
Listing::own_files() hands on the files of the manifest's own that the
scratch space makes its files for, to the walker of the next commit,
which passes over them. The Writing takes the scratch space over from
the listing and keeps it until it is dropped. It hands it to the table
of the hard links, which gives a file back where a doubling ends while
the manifest is written. So turning a listing into a Writing closes
nothing, and the files of the manifest's own stay open until the
manifest is written or given up, also the ones the listing is done
with. Dropping the Writing closes them. Note that where the rest of a
run that was half written can't be written, the merge keeps the sort
and its file. Nothing empties the file there. ManifestWriter::finish()
of a later commit gives it back with the others as it returns that
error.
While at it, Error::too_many_contents() takes whatever names the
object, so that the listing can use it without a record number, and the
flag sets and FilledIn get bits() and from_bits() for record.rs. The
checks of a file object and Error::no_name() take the Subject of the
object instead of its record number, and a Subject can be of a file
object that is in no manifest yet, "file object (name)". That is what
the errors of the check at the add name.
The module hardlink loses its #[expect(dead_code)]. The listing uses
what was left of it, and rustc 1.85 counts a use from a module that
expects dead code itself, so it would call the expectation unfulfilled.
Newer compilers are fine either way. The impl block of Encoder::begin(),
file() and finish() loses its expectation for the same reason, with the
comment about it: the listing writes the manifest with them.
Sorting 1e5, 1e6, 1e7 and 1.2e8 records of 171 bytes that come in
random order, with the scratch file on xfs:
records runs written take passes merge resident
1e5 0 0 0.01 s 0.02 s 19 MiB
1e6 5 156 MiB 0.3 s 0.1 s 33 MiB
1e7 52 1626 MiB 3.2 s 1.9 s 36 MiB
1.2e8 627 19606 MiB 38 s 4.4 s 30 s 65 MiB
The 627 runs take one pass, which merges 116 of them and writes 3624 MiB
a second time. The scratch file of that sort never has more than 19711
MiB of data in it on xfs, and 19839 MiB on a tmpfs with huge pages. On a
ramfs, which can't punch holes, it ends at 23233 MiB. A sort of 1e7
records that is made to take three passes, with runs of 1 MiB and 8 of
them to a merge, writes 1634 MiB and then 4561 MiB in its passes. Its
scratch file never has more than 1648 MiB of data in it on xfs, and ends
at 6197 MiB on a ramfs.
Note that xfs writes out what it still has in the page cache for a hole
before it frees the blocks. A sort that is over within seconds has all
of its runs there. So the holes cost the last merge of 1e7 records
0.3 s of its 1.9 s on xfs, and nothing that shows on tmpfs.
And the listing as a whole, for a tree of 1e5, 1e6 and 1e7 regular
files with twenty to a directory and every fourth name a hard link. The
files are added in random order and the manifest is thrown away a
megabyte at a time:
files add write manifest resident
1e5 0.04 s 0.07 s 13 MiB 16 MiB
1e6 0.7 s 0.9 s 136 MiB 36 MiB
1e7 8.6 s 10 s 1370 MiB 40 MiB
Resident is the largest RssAnon of the whole test process, its memory
without the pages of the files it has mapped. It starts at 0.4 MiB.
The tests cover:
- file objects with every field, to bytes and back, every bit of the
bytes flipped, and a writer that stops at its limit
- the sort against a stable sort, in memory, through many runs and
through passes that merge down to two runs at a time, with records of
every size up to the largest
- which runs the passes merge, for 600 runs, for 10000, for more than
512^2 and for every number of runs up to 1000 with small merges
- runs written a part at a time, also the one a sort ends with half
written, which the merge writes, and that nothing is written or
sorted without a budget
- a run whose every write fails once, which loses no record
- passes done a part at a time, that a step writes no more than its
budget and a record, and that nothing is read or written without a
budget
- that a merge never takes more runs than it may and reads each of them
through a buffer of the one size
- the holes behind what a merge has read, block by block, in a pass and
in the last merge, with holes of a block and holes larger than any
run, which give a run back once it ended, and the same on a ramfs,
where none can be punched and the first that fails is the last one
tried
- the room past the end of the scratch file, which the test takes with
fallocate(), given back before the last merge, in a process of its
own, skipped where a hole past the end of a file gives no room back,
as on ext4, which leaves such a hole alone
- scratch files that were written over or cut short, in the last merge
and in a pass, and that nothing follows a merge or a pass that failed
- a sort whose last run can't be written to its end, which its merge
keeps with the scratch file of the runs, open and as it is, and that
the merge fails from there on
- that every order of the adds gives the same manifest byte for byte,
in memory and with every record in a run of its own and two runs to
a merge, for a small tree and for one of thousands of files
- that a reader takes the manifest as one in the code point order, also
with names that sort differently as bytes
- the same name twice, in every combination: two files of a tree are
one file where their file objects are equal, whichever inodes they
are, and a file that changed where they aren't
- what a record keeps of a file of a tree: the key of its inode only
where the file is a hard link, and its size only where it is a
regular file whose object has none
- directories that are made up, each feature that rules that out, and
the same for the root, which needs no UUID and no serial number
- files below something that isn't a directory
- tokens in the order of the manifest for inodes and for the tokens of
a caller, and hard links that don't match
- the manifest written a turn at a time, the table of the hard links
settled and the runs merged first, and that nothing follows an error
or the end
- the largest record a manifest can hold, and file objects that are
larger, that have too many contents objects or no name
- file objects of a caller that are refused when they are added, one
for each fault a writer finds, a literal that isn't the data of its
hash and a contents object that refers to the root among them, and a
second directory object for a name, after which the listing writes
what it writes without them
- file objects of a caller whose adds fail for the table of the hard
links, every third one with EFBIG from a limit that prlimit(1) sets in
a process of its own, with one page of the table in memory and with
all of them: such an add adds nothing, the adds go on, and the
manifest is that of the adds that went through, skipped where the
limit can't be set or fails no write
- what is counted for the report
- that the listing hands on the files of the manifest's own of its
scratch space, and that what writes its manifest keeps them open
until it is dropped
- that a step pays for the pages the table of the hard links reads and
writes for its tokens: no more than its budget and what its last turn
takes over a table on disk with one page in memory, and nothing over
a table in memory
- that the memory of a process that sorts 2.5e5 records or lists 2e4
files stays within its bounds, also where the sort takes three
passes, and the same for more of them, up to 1.2e8 records and 1e7
files, which are ignored unless asked for, and that the data of the
scratch file of the sort stays within that of the records and what
the merges did not give back yet
[1]: https://github.com/coreutils/coreutils/blob/afc41505e995c0d15ccbdb331e1079f8d558749c/src/sort.c#L3935
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The manifest builder lists a tree of files on the host: the directory a descriptor refers to as the root, and everything below it. This adds what walks that tree. It reads the files and hands their file objects to the listing of the previous commit, and it hands over the file objects a caller made as well. ManifestWriter comes next, and nothing outside the tests uses this yet. Walker does an add in steps, as the listing does. An add is the walk of a tree, or a file object of a caller. Walker::begin() only says what is to be added, and Walker::step() does a part of it, as much as its budget allows, and says whether the add is done. A large file is hashed over as many steps as its data takes. Between two entries a step looks at its budget and returns once it is used up, which is where a walk can be stopped and picked up again. The directories a walk is in are on an explicit stack instead of a recursion, so the stack is where the walk is from one step to the next. That is the shape of walkdir's IntoIter and of fts(3). Where an add is from one step to the next is its Task. Walker::begin() returns it, whoever began the add keeps it and hands it to every step, and the walker keeps none. An add borrows the descriptor it was given for as long as it is being done. It takes no duplicate of it, so it needs no descriptor of its own for what it is given, and the entry of that descriptor is the borrowing one of Entry::of(). A file object of a caller is borrowed as well. The budget of a step counts the bytes of data that are hashed. The turn that opens an entry of a directory counts as 4096 of them on top of the bytes of its name. Opening a file and reading what a manifest records of it takes about 2.4 us on xfs with a warm cache, as long as hashing 3.2 KiB. With every file counted as its name and one byte more, a step of 1 MiB got through about 95,000 empty files and took up to 243 ms, and up to 29 ms over files of 100 bytes. With the weight it takes 0.6 to 0.8 ms over empty files, over files of 100 bytes and over the data of a large file alike, and 1.1 ms at most, but for the step that sorts a full buffer of records, see the previous commit. The records of a tree of 300,000 files fit into one buffer, so no step of these walks sorts a full one. In trees of 600,000 files, that step takes 2.3 to 2.5 ms where the names come in order, as they do in the two trees of small files, and 68 to 70 ms for the files of one directory whose names come in no order. The add takes as long either way: that of 300,000 empty files took 698 to 746 ms in such steps over twelve runs, and 699 to 745 ms without the weight. Both were measured with one binary that takes the weight from the environment. The walk is by descriptor throughout, as pathrs and cap-std walk. Every entry is opened as a handle in the directory it is in and read through that handle. A directory the walk goes into is opened by its name for its listing, as Entry::reopen() opens a directory, and its entries are opened in that descriptor. So a walk keeps one descriptor for every directory it is in. rustix's Dir::read_from() would reopen "." with the flags of a handle, which gives another handle, so sys.rs gets Entries, the listing of a directory that is open for reading. It reads with rustix's RawDir, which can be asked again after getdents() failed with EINTR. With the libc backend, as on powerpc and s390x, Dir ends a listing at its first error. What getdents() reads into starts at 8 KiB and doubles up to 32 KiB, as for Dir, so that the directories a walk is in take less memory while they are small. The directories are read in the order getdents() gives, since the listing sorts everything anyway. Note that the first room has to hold every entry there is. A name of FUSE can have 4095 bytes, and its entry takes 4120. FUSE asks its server for as many bytes as getdents() has room for, but for a page at least, in fuse_readdir_uncached(), and a server that can't fit the first entry into that replies with nothing. getdents() returns that as 0, which nobody can tell from the end of the directory. So with a smaller first room, a name of 4073 bytes or more ends the listing of its directory there on 4 KiB pages, without an error. 8 KiB is enough whatever the size of a page. Note that this holds for what FUSE means to ask for. It reads into pages it allocates for the request, and where it gets fewer than it asked for, fuse_readdir_alloc_buf() makes the request as small as the pages it got, down to one. So under memory pressure such a name can still end the listing of its directory, whatever room getdents() has, and nothing here can tell. Other file systems fail getdents() with EINVAL where the first entry it would give doesn't fit, so where nothing was read yet the room still doubles right away, up to the 32 KiB, and getdents() is asked again. The tests have no FUSE server, so this was tried by hand with a FUSE server on the raw protocol that negotiates FUSE_MAX_PAGES, mounted in unshare -Urm, for names of 1100, 4072, 4073 and 4095 bytes. Each is listed with the name behind it, and with a first room of 1 KiB the two longer ones list nothing. So the memory of a walk grows with the depth of the tree: a descriptor and 8 KiB to 32 KiB of names for every directory it is in, as glibc's opendir() keeps a buffer for every directory that is open. That is 8 MiB to 32 MiB at 1024 levels, and up to 64 MiB at the about 2048 levels a name can have. - The descriptor of a walk has to refer to a directory, which is the root. Anything else is Tree(NotDirectory). - A file on another mount than the one the walk started on is a mount point, by the mount ID statx() gives. Without CROSS_MOUNTS it is not walked into. A directory is then an empty one with the attributes of the mounted root. A file mounted over another one is the file the mount shows, as there is nothing to walk into: a regular file with its attributes and the hash of its data, and a device node with its attributes and its device numbers. A btrfs subvolume reached through its parent's mount has the same mount ID and is a directory like any other. - The walk mounts nothing. An automount point that nothing is mounted on yet is told from its handle, with Entry::is_automount(), before any directory is opened. It is never opened and nothing is looked up in it, since either can mount what it stands for: the open of a directory by its name does, and so does the lookup of a name in the root of an indirect autofs mount. So a walk that comes to one has it as the empty directory it is, with what statx() says of it, like a mount point it doesn't cross, with CROSS_MOUNTS and without. Where it is the directory of the descriptor of a walk it is the root the same way, with nothing below it. And what is read from the open file can't be read of it, so an inode flag is Tree(FeatureUnavailable) there. Note that a walk looks at a directory before it opens it by its name, and that a directory that becomes an automount point in between has its file system mounted by that open, see the commit that adds Entry::reopen(). The walk fails with Tree(Changed) then. That is a known limit, and the documentation of the walk says so. The tests can't make an automount point. So this was tried in a VM as root, on 7.0 and on 7.3-rc7, with an automount unit of systemd and with "tracing" of a fresh debugfs: after every walk of the test systemd still waits for the unit to be used, and nothing is mounted on "tracing". The test runs where UAPI16_TEST_AUTOMOUNT names such a point. - With CROSS_MOUNTS a mount can show a directory again inside itself. The walk keeps a hash set of the device, inode and subvolume of the directories it is in, without the mount ID, since a bind mount has an ID of its own. A directory that is in it already is Tree(Loop), naming the directory below. The same directory mounted twice next to each other is not a loop and is listed twice. - A symbolic link is recorded with its target, which readlinkat() reads through its handle, and never followed. Its size is the length of its target, as for systemd-manifest. - The names the specification reserves at the top of a tree are passed over, whatever the file is, and not counted. Name::is_reserved_component() tells them from the bytes of a name, which need not be UTF-8. Every other name goes through Name::join_os(). A name a manifest can't hold is Tree(UnwritableName), naming the directory, with the name as it is and the rule it breaks, for which Error gets a setter. With GRACEFUL the entry is left out instead, a directory with everything below it, and counted, every time a walk skips it. - The IDs 65535 and 4294967295, which the specification does not permit, and a modification time before 1970 or after 2554, which mTimeNSec can't express, are left out and counted as fields that were left out, like the names of owners the user database doesn't have. So are the UUID and the volume serial number that the file system of the root of a walk doesn't have. - The data of a regular file is hashed where the manifest records hashes, and also where it records hard links and the file has more than one name, since the names of one inode are held against each other with their data. A file that is too large to be hashed fails with EFBIG before it is opened, see DataHash::admits(). Where the manifest records extended attributes as well, a file that is hashed is opened for its data before they are read, so that they are read through the open file and take no procfs. - A file that changes while it is read is Tree(Changed), where statx() shows the change. The hash compares the status of a regular file before and after its data. A directory is compared before each of its entries and once more at the end of its listing, and any other file once more than its status was read of it, its flags, its DOS attributes or its extended attributes. An entry that its directory lists and that is gone when it is opened is Tree(Changed) as well. The rule is that files must not change while they are read, and a directory is read when it is listed. The error says that the file was removed or renamed. Note that comparing a directory at the end of its listing alone isn't enough. A rename can make readdir() return an entry again under its new name, which xfs does, and the walk would go through a directory again with everything below it, or read the data of a file again. A file renamed over the name readdir() returns next would be read again under that name. Compared before each entry, a walk reads only the entries a directory had when it was opened, each once, and a change stops it before the next one. So where statx() shows every rename, a walk does no more than its tree takes. It costs a statx() per entry, 0.2 s or about 8% of the 2.3 s a walk of 1e6 empty files takes. Note that statx() doesn't show every change. FAT keeps its times in steps of two seconds and never updates those of its root directory, and before Linux 6.13, or on a file system without fine-grained timestamps, two changes within a tick of the clock can leave the same times. An entry can come twice or not at all then. The documentation of the walk says so. An add that fails is over. The step that fails closes the directories the walk was in, and every later step of the add does nothing and says that it is done. A file object of a caller goes to the listing with the last turn of its add, so an add of one that fails added nothing, and the walker goes on with the next add as if it had not been. A walk that fails added a part of its tree, which the listing can't take back. Whoever walks a tree gives the manifest up then, and the walker keeps no note of it. A directory that is walked into is opened for its listing before anything else is read of it. A walk also asks the file system of the directory it starts at for the DOS attributes before the root goes to the listing, where the manifest records one of hidden, system and archive. The root has none of its own, so without that the first file below it would be the one to fail. A file on another file system further down still fails where the walk comes to it. readlinkat() is made again where it fails with EINTR, as the other calls of sys.rs are. A walk passes over the files of the manifest's own. It looks every entry up in the OwnFiles of the scratch space, right after it took the handle and the status of the entry and before it does anything else with it. That is one lookup in a hash map of a handful of inodes for every entry. An entry that has the inode of one of them is passed over only if its status is that of the open file as well, which takes one statx() of that file, see the commit that adds them. A file system can give two files one inode number, and a file of the tree that merely has the number of one of our files is listed like any other. Where two of our own files have one number, the entry is held against both, with a statx() of each, and both are passed over. Where that statx() fails, the walk fails with its error, which names the entry, and the message says that a file of the manifest's own with its inode can't be read. The scratch files are among them from when they are made, for the file systems that show them in a directory. The walker has the listing, and with it the scratch space and its OwnFiles, for as long as it is there. That keeps every scratch file open, also one the listing is done with. So no walk comes to a scratch file that was closed. Walker::finish() ends the adds and closes nothing: the files stay open with what writes the manifest, which the previous commit gave the scratch space. And the walker can be told to pass over one more file, with its inode and the open file that an entry with that inode is held against. That is for a manifest that is created in a tree it lists, on a file system that has no O_TMPFILE and gives it a name of its own until it is complete. Note that optional.rs and sys.rs lose their expect(dead_code), and the comment about the expectations moves to the first one that stays. The walker uses everything of them that was unused so far, and rustc 1.85 takes what a module that expects dead code uses as used, so the expectations would be unfulfilled there. The tests cover: - every type of file against what std::fs says of it, without features, with the default ones and with mtime - the reserved names at the top of a tree and below it - names a manifest can't hold, for every rule, skipped and counted - names that get too long in a deep tree - IDs and times a manifest can't hold, and a file from before 1970 - walks in steps of any budget, steps without one, and steps of an add that is done or failed, which do nothing - what the turn that opens an entry of a directory counts as - a directory that changes while it is listed, entries that are gone before the next one is listed and between the listing and the open, and a file that grows while it is hashed - renames a turn at a time: a directory, a file whose data was read, a symbolic link, a file renamed over the name listed next, and a rename while an entry is read, after which nothing more is read - hard links that are held against their data without sha256, with the data of the file written over between its two names - the device numbers of /dev/null against what std::fs says of them, which takes no namespace, and none where the manifest doesn't record them - a tree 300 directories deep - passing over the files of the manifest's own, the one the walker is told and one the scratch space has, which has another name by then, and a file of the tree that has the inode of the first and another status, which is listed - two files of the manifest's own with one inode, which are both passed over and both stay open, and a file of the tree with that inode, which is listed - a file of the manifest's own whose status can't be read, the directory procfs has for the descriptors of a process that is gone, which fails the walk at the first entry that has its inode, with ENOENT, the name of that entry and a message that says which file can't be read, skipped where the kernel still reads that status - a feature that can't be read, which fails the walk at the root, and that whether a file is read-only never fails - a sparse file that is too large to hash, a step of one byte at a time, also where it may not be opened, and one of the most data that is hashed - extended attributes that open nothing, as inotify sees it, and those of a file that may not be opened - a walk with an empty tmpfs over /proc, in a user and mount namespace from unshare -Urm, which reads the extended attributes of the directories it lists and of the files it hashes, and fails for a file it doesn't hash and for a FIFO, skipped where the kernel has no O_EMPTYPATH, since pathrs panics without a procfs - an automount point in a walk and as the directory a walk starts at, with and without CROSS_MOUNTS, an inode flag of it, the UUID and the serial number of its file system, which are left out and counted, and that nothing is mounted on it after all of that, where UAPI16_TEST_AUTOMOUNT names one - that a walk opens nothing but regular files and directories, as inotify sees it - file objects of a caller next to a tree, and that the add of one waits while the listing makes room for it - a file object that is refused, after which the adds go on, and walks that fail a turn at a time: of a file, of a directory of mode 0311, and in the middle of a tree, at a file of mode 000 whose data is hashed, both skipped where the tests may read any file, and at a name a manifest can't hold, after which nothing is open and every later step does nothing - mounts in a user and mount namespace from unshare -Urm: mount points that are not crossed and that are, a regular file and /dev/null mounted over files, a mount that shows a directory again, the same directory mounted twice next to each other, a walk that starts at a mount point, and hard links through a bind mount, skipped where unshare can't make one - an empty directory that may be read but not searched - listings of directories over several calls of getdents(), with a name of 255 bytes, also with a first call that has no room for it, and targets of links in sys.rs Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
This adds the manifest builder of 0.1.0 on top of the walker and the
listing of the previous commits. ManifestOptions says what a manifest
records and builds the manifest of a tree, ManifestWriter builds one
from the file objects a caller made, and ManifestReport says what it
lists:
let report = ManifestOptions::new()
.features(Features::DEFAULT | Features::MTIME)
.generate_at(
out_dir.as_fd(),
Path::new("esp.manifest"),
&esp,
AddFlags::NONE,
)?;
let mut m = ManifestOptions::new().open(out)?;
m.add_root(&inode, &file_system)?;
m.add_object(&object)?;
let (out, report) = m.finish()?;
ManifestOptions::new() gives the default features of the specification
and the code point order, and features() and order() change them.
Default does the same as new(). It is written out since Order has no
default of its own. The features of partitions don't apply to a tree of
files, so create_at(), open() and the two generate functions fail with
Tree(FeatureNotForTrees) for them, naming the first one. They do so
before they take a descriptor, so that they fail the same way where the
process has none left.
create_at() makes a new file that gets its name in finish(), and it
never replaces anything. The work is NewFile in sys.rs:
- The name is checked with statx() right away, so that a name that is
taken fails before the tree is read. Giving the file its name is the
check that counts.
- The file is made with O_TMPFILE in the directory of the name and
linked in with linkat(fd, "", dir, name, AT_EMPTY_PATH). Linux before
6.10 takes that only from a process that may look up any name and
fails with ENOENT for any other, and then the link procfs has for the
descriptor is linked with AT_SYMLINK_FOLLOW, as systemd's link_fd()
does. That is the FdLink of the descriptor, which is checked to lead
to the file first. A /proc that isn't what it should be can link
another file by then. So the link goes to a TemporaryName first, and
that name is checked to lead to the file before it is renamed to the
name of the file, as below. Where it leads to another file, only the
temporary name is removed, which is nobody else's whatever it leads
to, and the call fails. The name of the file only ever gets a link
that was checked to lead to the file, and it is never removed. That
would remove whatever another process renamed over it in the meantime.
A link that was interrupted can have made its name before it failed,
so that name is removed, and the next one is tried, as for the
temporary name of a file that is made. TemporaryName::make() is the
one loop for both now. An interrupted call takes no name that
something else has, so it is tried again as often as it takes, and
only 16 names that are taken give up with EEXIST.
- A file system without O_TMPFILE, such as FAT, gets a file with a name
nobody can guess, a TemporaryName of the scratch files, and renamed
with RENAME_NOREPLACE. Where a file system has no RENAME_NOREPLACE the
new name is linked and the temporary one removed, which replaces
nothing either. That is systemd's rename_noreplace() without the
rename that replaces, which it falls back to last. A walk passes over
the file while it has the temporary name, so that a manifest that is
created in the tree it lists doesn't list itself. For that it goes
into the files of the manifest's own, where its scratch files are,
with its inode and the descriptor its NewFile has of it. NewFile keeps
that descriptor in an Arc, and the two share it. So an entry with the
inode of the manifest is held against the manifest itself, and a
writer takes no descriptor more for that. Reading that inode can fail,
and such a failure is about no file of the manifest, so
Failure::into_unnamed_error() makes its error, without a name.
- The calls that give the file its name are made again where they fail
with EINTR, but one that was interrupted can have given the name
already. linkat() made again fails with EEXIST then, and renameat2()
with ENOENT, unless something else fails first. So the name is looked
at whatever the call fails with after an interruption, and the file
has it where the name leads to the inode of the file. EINVAL, ENOSYS
and EOPNOTSUPP are left out. They say that the file system has no such
call, and the rename falls back on them. Where the file ends up with
both names, because its temporary one can't be removed, it keeps both,
and the call fails. Its own name isn't removed again. Nothing removes
a name only while it leads to a given file, and another process can
rename its file over the name in between. The temporary name is
removed once more when the file is dropped.
- A name that is taken is an Io error of EEXIST, also in finish(), which
leaves the file that took it alone. The I/O errors of the new file say
which file they are of, through Error::io_about(), and keep the errno,
as in "the manifest file 'm': File exists (os error 17)". That holds
where the writer has a NewFile: for making the file, for the writes of
the manifest to it in finish(), and for giving it its name. A manifest
that goes to a writer of the caller has no file to name, and its write
errors are as the writer returns them. A writer that is dropped leaves
nothing: the file without a name is gone once it is closed, and the
temporary name is removed. A finish() that fails before the file has
its name leaves nothing either: the file is emptied before its
temporary name goes. One that fails after the call that gave the file
its name leaves the file under that name, with the whole manifest in
it, see below. NewFile owns the temporary name as soon as there is
one, before the descriptor for writing is duplicated, so a create_at()
that runs out of descriptors there removes it as well.
The file gets the access mode File::create() gives, read and write for
everybody less the umask.
A name with more than one component names a file in a directory below
the directory create_at() is given. We go down the directories on the
way one component at a time with O_NOFOLLOW. A symbolic link on the way
is ELOOP, wherever it leads. An absolute name, and "." or ".." on the
way, are EINVAL, as a name that is empty or ends in a slash, "." or ".."
is, which names no file. NewFile::split() says which directories and
which name a name has, and refuses the rest. A caller that wants the
file in a directory that isn't below theirs opens that directory and
passes it.
Without that the owner of a tree could send its manifest anywhere, and
the scratch files that go next to it. With the whole name in one
openat(), generate_at() of a tree into "meta/root.manifest" would follow
a "meta" that was turned into a symbolic link.
Each component is opened with O_PATH|O_DIRECTORY|O_NOFOLLOW. With
O_DIRECTORY the kernel fails a symbolic link with ENOTDIR, like anything
else that is no directory. So where the open fails with ENOTDIR the name
is looked at once more, with statx(), and a symbolic link is ELOOP. The
errors are errnos that name the manifest file, and not the problems of a
tree: what is on the way to the file is no part of a tree. Nothing on
the way is looked at beyond that. A mount point is gone through, since
the file may well be on another file system below the directory. And an
automount point on the way gets its file system mounted, which
O_DIRECTORY does and the handles of a walk never do. The caller asked
for a file there. That was tried in the VM as well, with the automount
unit of the walk's test.
create_at() keeps a duplicate of the directory for a name of one
component, so the caller can close theirs, and the files the builder
needs while it works go next to the manifest. They are made through the
descriptor the new file has of its directory. NewFile keeps it in an
Arc, which Scratch::beside() shares, so a writer holds one descriptor of
that directory and not two.
The directory is given as a descriptor. A negative number is none and is
refused with EBADF before anything is opened or duplicated. That
includes AT_FDCWD, which rustix has as a descriptor, CWD. Note that
openat() would take it for the first component of a longer name and make
the file below the current directory, while the duplicate for a name of
one component fails with EBADF by itself. A caller that wants the file
below the current directory opens it and passes that. The top of a walk
is refused as well: the walk asks for the file system of the descriptor
it is given, and fstatfs() fails with EBADF for AT_FDCWD.
open() takes any Write and writes nothing to it before finish(). The
builder works in the directory TMPDIR names then, or in /var/tmp. The
docs say that TMPDIR has to name a directory the caller trusts, since
whoever controls it can change the manifest. generate_at() and
generate() are create_at() and open() with the walk of top and finish().
The walk is ManifestWriter::add_all(), which is private: a tree is the
whole manifest, with the directory of the descriptor as its root.
ManifestWriter<W> has the Write bound on its impl and not on the type.
Every descriptor is impl AsFd, taken by value as rustix takes them. Each
function calls as_fd() once and hands the BorrowedFd on, so nothing but
the few lines around it is generic. An add borrows what it is given for
as long as the call runs and duplicates none of it. It is a loop over
Walker::step(), with the Task of the add as a local of the call. So the
walk takes no descriptor of its own for the directory it is given.
No add leaves a writer with a part of what it was given. add_object()
and add_root() hand their object to the listing with the last turn of
the add, so one that fails added nothing, the writer is as it was, and
the caller goes on with it. A walk that fails is the end of
generate_at() or generate(), which return its error. The writer gives
its scratch files back and is dropped.
finish() ends the walker first, since no add can come any more then.
That closes nothing: the scratch files stay open, also the ones the adds
are done with, until the manifest is written. It turns the listing into
the manifest a step at a time, writes what each step appended, flushes,
and gives a file of create_at() its name. add_object() lends its object
to the walker, and the listing checks it as a writer does but for where
it comes. An object that is refused isn't added, so the writer is as it
was, and finish() only checks where it comes.
A writer that ends in a call gives the scratch files it still uses back
first, which empties them. finish() does that as it returns, however it
ends, and the generate functions do it where their walk fails. The
Writing has give_back() for that. It hands the files of the table of the
hard links and of the merge to Scratch::take_back(). A finish() that
fails before the new file has its name empties that file as well, with
NewFile::give_up(). NFS and FUSE keep a file that was unlinked while it
is open under a name until it is released, and a walk of that directory
would read what is left in it. With a passthrough on libfuse that takes
300 ms to release a file, a second builder that walked the directory
right after finish() listed 4 such files with 53,173,440 bytes in all
when finish() only closed them, and 4 empty ones with the give-back.
After a generate_at() whose walk failed it was 5 files with 54,450,276
bytes in all and 5 empty ones. After a finish() that failed because the
name was taken there were 5 files with 88,622,345 bytes in all,
35,448,905 of them the manifest, on which the second builder failed with
EIO, and 5 empty ones, which it listed. Note that a writer that is
dropped unfinished only closes its files, see the commit that adds them.
A file that has its name is never emptied, though. The one exception is
a call that gave the file its name and fails all the same, as link(2)
documents for NFS. NewFile::link() works in two halves for that.
sync_and_name() syncs the file and gives it its name. sync_named() does
what is left once the file has it: it removes the temporary name where a
link gave the file its own, and it syncs the file and its directory.
link() gives the file up where the first half fails, and nowhere else.
IOW, it goes by which half failed and not by a look at the name after
the failure. Such a look can't be trusted. Another process can rename
the manifest as soon as it has its name, and the look finds no name
then. And libfuse shows every name of a file with an inode number of its
own on its high-level interface unless the file system sets use_ino, so
a name that a link gave never looks like the file there. In both cases
the look would get a manifest emptied under its name. So where finish()
fails after the call that gave the file its name, the file keeps the
name and the whole manifest, and finish() fails all the same. Note that
one failure of the first half doesn't give the file up: where a link or
a rename was interrupted and failed when it was made again, and nobody
can tell whether the first call gave the name, the file keeps its data,
and the drop removes its temporary name as before.
add_root() takes the root as the caller describes it, with its inode
attributes and the identity of its file system. The listing makes the
root file object the manifest would begin with of them, with the
features and the order of the options, and checks it as a writer checks
the one it begins a manifest with: the size of its record, its strings,
its fields and their features. That is RootObject::check_alone(), and
RootObject::listed() makes the object for the check and for the start
of the manifest alike. A root that is refused isn't added either. The
first root that is added says what the root is like, as the first add
of any directory does. The add of a root goes through the walker in
steps like the add of a file object, and waits for the listing to make
room the same way.
Debug for ManifestWriter is written out, the way std's BufWriter has
it, with the bound on the impl: the writer the manifest goes to, and
nothing of what it keeps of the files until the end.
ManifestOptions::sync() asks for the new file of create_at() and
generate_at() to be on disk once finish() returned. It is false unless
set, in the shape of features() and of std's OpenOptions::append(). It
is systemd's LINK_TMPFILE_SYNC. link_tmpfile_at() syncs the file before
it gets its name, whether by linkat() or by a rename, and fsync_full()
syncs the file and its directory once it has it. NewFile does the same
for every way it gives the file its name. The directory is opened again
for that, since fsync() takes no handle. open() and generate() write to
a writer of the caller, which the caller syncs, and the scratch files
are never synced.
ManifestReport counts per file object, so a file with three names counts
three times in files() and bytes(). bytes() adds up the sizes statx()
gives, also where the manifest doesn't record sizes. directories()
counts the root. entries_omitted() counts what GRACEFUL left out, a
directory once, and fields_omitted() the fields that were left out. The
UUID and the volume serial number that a walk doesn't get for its root
are two of them. A root of a caller without them isn't counted, and
neither is one that is made up, like a field that a file object of a
caller doesn't have.
AddFlags becomes public, and so do NONE, CROSS_MOUNTS, GRACEFUL and
contains(), with BitOr, BitOrAssign and BitAnd. The documentation of the
four types says what the write-up of the builder promises: the order and
when the output is complete, that create_at() never replaces and which
names it takes, the same name twice, mount points, automount points,
which the walk never mounts, and the one window in which an open still
can, symbolic links, names a manifest can't hold, files that change and
the changes that don't show, that Changed is a best effort and a tree
that other processes change can fail the walk with another error, the
1 TiB a file may have for its data to be hashed, that extended
attributes are read through /proc for a file that isn't open anyway and
what that takes, that a walk passes over what the manifest keeps on
disk, that a manifest which keeps that in a directory of the tree it
describes can fail a large walk there with Changed, what FAT limits,
hard links and their risk on NFS of btrfs snapshots and on an overlayfs
over btrfs before Linux 6.10, the features and what is counted, what a
failed add leaves, and Linux 5.10 or newer. fs-uuid and fat-serial are
read from the root, and a root whose file system has no UUID or no
volume serial number is written without it, which fields_omitted()
counts. The documentation also says what fails where the adds can't help
it. A record that is larger than MAX_RECORD_SIZE, which the extended
attributes of a file can make it, is Manifest(RecordTooLarge), naming
the file, from the walk where the attributes alone are larger and from
finish() otherwise. The crate documentation and the README mention the
builder.
The FAT job of CI builds a manifest there as well, with TMPDIR on the
FAT file system too.
No job of CI has a file system that keeps a file that was unlinked
while it is open. So the test of that only runs where
UAPI16_TEST_SILLY_RENAME_DIR names a directory of one. It was run on a
passthrough on the high-level interface of libfuse, on 7.4-rc3 and in a
VM on 7.0 and on 7.3-rc7. There the scratch file of the sort stays in
the directory of the manifest as .fuse_hidden0000000900000002 while it
is open, and a walk that looks nothing up lists it. The test removes
its files when it is done and leaves its directories where the file
system can't remove them: a libfuse file system without rmdir answers
ENOSYS.
Now that ManifestWriter uses them, the expect(dead_code) of listing,
scratch and walk go, with the comment about them.
The tests cover:
- a new file that appears once the manifest is complete, as what the
same manifest written to a stream holds, with the mode File::create()
gives, also with generate_at() and after the caller closed the
directory
- names that are taken when the writer is made and while the manifest is
written, what their errors say, and a writer that is dropped
- names with more than one component, a symbolic link, "." and ".." on
the way, absolute names, a file on the way, names of no file and a
directory that is not there, with what each error says, and that a
tree whose directory became a symbolic link can't send its manifest
elsewhere
- that rustix's CWD is refused with EBADF as the directory of a new
file, for a name of one component and of more, and as the top of a
walk, and that nothing is made in the current directory, in a process
of its own whose current directory is the directory of the test
- which names are names of a file below a directory, in sys.rs
- a mount point on the way to a new file, in a user and mount namespace
from unshare -Urm, and an automount point on the way, which gets its
file system mounted, where UAPI16_TEST_AUTOMOUNT_OUT names one
- a write of the manifest that fails, which names the file where the
writer made it and doesn't where the caller did, with a limit on the
size of files in a process of its own
- that the scratch space shares the descriptor the new file has of its
directory
- that the manifest of a tree is written a slice at a time, none larger
than a step puts together, and that what it is written to is flushed
once
- that nothing is written to a stream before finish(), and that the
order of the adds doesn't matter
- the features of partitions, for every way to make a writer
- the default options, and what they put into the root file object
- what the report counts
- duplicates, the same file object twice and a missing parent
- no descriptor left, with 64 descriptors at most in a process of its
own: add_object(), which is done, generate(), which fails with EMFILE
at the root, and the features of partitions there as well
- files with and without temporary names, renames that would replace,
linking through procfs, a file with a temporary name that keeps what
it holds when it is dropped, one that is emptied where its name is
taken, one that keeps what it holds where nobody can tell whether it
has its name, and a file that runs out of descriptors once it has its
temporary name, on a file system of message queues from unshare
-Urmi, in sys.rs
- names that are given while the call is interrupted, after the link or
the rename was done, also where the link fails with EIO when it is
made again, those nobody can tell of, where another file took the
name, where the name isn't there and where it can't be looked at, a
call that fails with EINVAL, ENOSYS or EOPNOTSUPP when it is made
again, which gave no name, and a link through procfs that leads to
another file, whose temporary name is the only one that ever appears,
as inotify sees it, and which leaves no name behind, in sys.rs
- temporary names that a link makes, also after a link that was
interrupted, any number of interrupted calls, 16 names that are taken,
and a link of the new name, which loses the temporary name once the
file has its own and keeps both names and what it holds where that
one can't be removed, until the file is dropped, in sys.rs
- file objects of a caller that are refused, a literal that isn't the
data of its hash, a contents object that refers to the root and a hard
link that doesn't match the first name of its inode among them, after
which the writer takes the next one and writes what it writes without
them
- the root as a caller describes it, the first of two, a root that
waits for the listing to make room, in walk.rs, and roots that are
refused: a field of a feature that isn't recorded, an owner and a
label no manifest may have, and a record that is too large, after
which the writer writes what it writes without them
- a manifest that records fs-uuid and fat-serial without a root, with a
root of a caller that has neither and with one that has a UUID, none
of which is counted, and from the walk of a tree, which fails for
neither and counts each one it leaves out
- the walk of a tree on a ramfs from unshare -Urm, which has neither a
UUID nor a serial number: one field left out for each that the
manifest records, and two for both, skipped where the kernel gives a
ramfs a UUID
- what Debug shows of a writer
- that sync is off unless asked for, and that synced files appear as
others do, without O_TMPFILE and with it, and with a rename, in
sys.rs
- a manifest built on FAT, which has no O_TMPFILE, synced and with its
scratch files on FAT as well, as what the same manifest written to a
stream holds, and the UUID that FAT doesn't have, left out and counted
next to its serial number, where UAPI16_TEST_FAT_DIR names a
directory of one
- a manifest created in the tree it lists on fuse-overlayfs, which has
no O_TMPFILE, and that doesn't list its temporary name, skipped where
unshare -Urm or fuse-overlayfs can't mount one
- a file of the tree that has the device and the inode number of the
manifest, which is listed where the manifest is passed over, on
fuse-overlayfs over two layers on a tmpfs each, where the manifest
gets the number of a file of the lower layer, skipped the same way,
and where no file of the lower layer has that number
- two files of the manifest's own with one device and one inode number
on the same mount, the manifest and the file of the lower layer that
has its number, which the writer is told to pass over as well: both
are passed over and both stay open, skipped the same way
- that the builder takes the file object from_metadata() makes of a
file whose user ID, group ID or both are 65535, and refuses it with
the ID that was left out, in metadata.rs
- that the temporary name of a new file is passed over through the
descriptor the new file has of it, and through no other one
- a manifest created in the tree it lists on a file system that keeps a
file that was unlinked while it is open, which lists neither the
scratch file of its sort nor itself and leaves nothing else behind,
where UAPI16_TEST_SILLY_RENAME_DIR names a directory of one
- a writer whose table of the hard links has doubled on disk twice,
which has the scratch file the table gave back open and empty next
to the two it uses, three and never more, and that a writer that is
dropped and finish() close all of them
- that finish() gives the scratch files of its writer back before it
closes them, which empties them, also where a step fails and where
the name is taken, that it empties the new file as well where it
fails before the file has its name, that a generate function whose
walk fails gives them back too, and that a writer that is dropped
closes them as they are, each seen through a handle the test has of
every file, and the same for a writer of open() that is finished and
for one whose walk fails, which have no new file, and for a
generate_at() whose walk fails, with handles that another thread takes
while the tree is walked, in a process of its own whose temporary
directory is the directory of the test
- that a merge that is given up gives the scratch file of the runs
back, from a sort it couldn't wind up and once the sort is wound up
- that a table of the hard links that is given up gives its scratch
files back, also those of a move to disk and of a doubling there that
are under way, and that the scratch space hands each of them out
again
- that a finish() that fails after the call that gave the file its name
leaves the manifest in it, with a directory that takes the name and
can't be opened for the sync of it, skipped where the tests may read
any directory
- that a writer can go to another thread where what it writes to can,
and that errors can
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
brauner
force-pushed
the
manifest-builder
branch
from
November 28, 2026 10:31
eabff96 to
7d09ab1
Compare
brauner
commented
Nov 28, 2026
brauner
force-pushed
the
manifest-builder
branch
2 times, most recently
from
November 28, 2026 16:09
4b87cfd to
705705a
Compare
Add the C API of the crate. It is the cargo feature capi and comes as a C library, libuapi16.so.0, with the header uapi16.h and uapi16.pc. We want it for build tools and for the mkfs tools. A tool that makes a file system shouldn't generate the manifest of it after the fact. It should write the manifest right when it makes the file system, from what it knows of every file it puts in. And a tool that assembles an image from a manifest has to read one. So the C API has what the Rust API has for that and no more: the builder with file objects of the caller, the reader and the writer of records, the file object itself, a way to get at the data of a file and to check it, and SHA-256. That is 125 functions for 11 objects. Two of them are C's alone: uapi16_file_object_contents_file() gives the file the data of an object is in, and where the data starts in it. And uapi16_feature_probe() tells a program which of its features the library it runs with has. The functions are in capi.rs. It turns the pointers of a caller into references, strings, slices and borrowed descriptors where a function begins, and what comes after that is safe code that calls the Rust API. capi.rs has no logic of its own for what a manifest is. The one thing it checks itself is the root, since C has one object for the root and for every other file where Rust has two types. The rules of the API are in the comment at the top of the header. The ones that shaped the code: - A function that can fail returns 0 or a negative errno, and stores an error object for a caller that asks for one. - Descriptors are borrowed and we never close one. An object that keeps a descriptor has it on loan until it is freed, as with sd_event_add_io(). - The two read functions fill an object of the caller, as getline() fills its buffer. We replace the whole object, so nothing of the record before can survive in it. - Every object remembers the process that made it. In a child of fork() a function that can fail fails with -ECHILD. We detect a child of fork() by a page of memory and not by its PID. We map one page with MADV_WIPEONFORK and keep a generation in it, which a child finds wiped and takes anew. A PID can't do that in every case: the first process of a PID namespace that makes the first process of another one has a child with its own PID. Where the kernel refuses the page, as an old one or one behind a seccomp filter does, there is no check and no fallback. A child can't be detected there, so its calls go on as in its parent. Note that this is probed once, at the first call, and that no version is compared. The header promises none of this, and nothing about a free in a child. The free functions work there all the same, and a writer that is freed in a child leaves the file of its manifest and the files it keeps on disk to its parent. A child has only the thread that forked it. So what capi.rs keeps across calls may not wait for a lock that another thread of the parent held at the fork, since a child would wait for it forever. The page is found without a lock, and the strings we hand out are kept in a OnceBox, which is a OnceLock without its lock. A manifest writer has no lock either, and no drop of it empties a file it keeps on disk. So all a child that frees one has to leave alone is the name a new file has until it is complete, which NewFile::abandon() forgets. A write of the writer of records that fails ends the writer. The encoder has counted the record as written by then, so what came after it would pass for a manifest that has it. capi.rs drops its writer at that write, and every later call fails with -ESTALE, as after uapi16_writer_finish(). Rust gains a few crate-private helpers that are only built with the feature, such as RootObject::retain_features(), Writer::next_record() and abandon() for a writer that is freed in a child. DataCheck holds its name as a Cow now, so that a check in C can own it. It is on main already, and its public signature stays as it is. C lends us the directory of a new file where Rust duplicates it, so NewFile has a second constructor for that, create_at_lent(). Split what create_at() and generate_at() do into one helper each that is given the constructor. So the Rust API and the C API share that code and can't drift apart. uapi16.h is generated by cbindgen 0.29.4 from capi.rs and checked in. cbindgen is a tool here and not a dependency. A CI job checks that the header is what cbindgen writes and that it compiles on its own, as C and as C++. build.sh builds the static library with cargo and links the shared one from it with the C compiler and the version script libuapi16.sym, which lists the 125 functions under LIBUAPI16_0.1.0. We don't let cargo build a cdylib. rustc gives the linker a version script of its own for one, and next to that no symbol can get a version from ours. With Rust 1.85 and GNU ld the result is 1.6 MiB, about the size of the cdylib rustc links there. install.sh installs the libraries, the header and uapi16.pc. The two scripts say the rest in their comments. This builds on x86_64 and with Debian's cross compilers for ten more targets, with Rust 1.85 and with 1.100. examples/ has six programs, one for each way to use the library. tests/capi.sh builds and runs them, and runs tests/capi.c against both libraries with the address and the undefined behavior sanitizer. CI does that with 1.85.0 and stable. The tests cover: - the generate functions, the options and what a report counts - NULL, unknown flags, negative descriptors and a dirty reterr - what a walk records, a file that changes while it is read, and what a walk that fails leaves on disk - every error kind with its errno, its path and its record - descriptors on loan, signals, and a process without descriptors - fork(): children and their children, threads, a PID namespace, a fork during a first call, and a kernel without such a page - file objects, the root, and the features an object retains - feature lists with NONE and unknown numbers in them, and the probe - the reader and the two writers, a write and a sync that fail, and one object for a whole manifest - manifests that break a rule, signature objects and validity - SHA-256, checks of data, and the files the data is in - trees on a ramfs and on FAT - a new file that gets its name by a link - the number of every enumerator, and the six examples The tests of capi.rs cover the same from the Rust side, and those that need no file system run under Miri. Miri has no such madvise(), so with cfg!(miri) there is no page and no check, and the page itself is tested from C alone. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
So far the crate documentation and the README mention the builder only
in passing, and nothing describes the C library. Add what a user needs
to get started with either, and an example program.
The crate documentation gets a paragraph on how the manifest of a tree
is written, with a generate_at() call as the example, a paragraph on
the feature capi, and a sentence on the room on disk that building the
manifest of a large tree takes.
The README of the crate gets two sections:
- "Building a manifest" has the same example and points to the example
program. It also says how a tool that builds an image writes the
manifest while it does, with add_root() and add_object().
- "C library" says what libuapi16 is, that uapi16.h describes every
function, what the six programs in examples/ do, how build.sh and
install.sh build and install it, and how a program gets its flags
from pkg-config.
build.sh works from any directory of the tree: its default build
directory is the one cargo builds into, wherever it is run. install.sh
looks in target/release below the current directory, or in the release
directory of CARGO_TARGET_DIR where that is set. So it is run from the
top of the package that crates.io has, in the repository from the top
of the repository, or with --builddir. The README says both. The README
of the repository says that it holds the C library as well, and how to
build and install it from its top.
The mkmanifest program we used to check the 0.1.0 API from a consumer's
side built a manifest by hand. It walked the tree with read_dir(),
sorted the entries of each directory, hashed every regular file,
numbered the hard links, handed every file object to a Writer and
removed the output again when anything failed. With the builder all of
that is one generate_at() call. So add it as examples/mkmanifest.rs:
cargo run --example mkmanifest -- DIRECTORY OUTPUT
It records the default features and the modification time, and prints
how many files, directories and other files the manifest lists and how
many bytes the files hold. generate_at() takes the new file as a name
in a directory. The example opens the directory OUTPUT is in, or the
current one, and passes the last component of OUTPUT as the name.
Errors are printed with Display instead of the Debug output that main()
prints for an error it returns.
The package has examples/ already, for the C programs. So the example
is in the package as well, and the package tests build it.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
We've kept a number of things out of 0.1.0 that we want to do in a later release and there are a few ideas that aren't decided yet. Let's write them down in a TODO.md so they don't get lost. Most of the entries are about the manifest builder, such as an API to add and finish in steps, signing a manifest, and a flag that leaves out mount points. The rest are two small items for the C API, the option to drop pathrs, and whether to accept AT_FDCWD. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
brauner
force-pushed
the
manifest-builder
branch
from
November 28, 2026 17:06
705705a to
7d335ef
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds the manifest builder and the C library, libuapi16. 0.1.0
isn't published yet, so all of it goes into 0.1.0.
Currently the crate reads and writes a manifest one record at a time,
and a Writer wants its file objects in the order of the manifest. So
whoever writes a manifest has to bring its files into that order, hash
them and number the hard links itself. The program we used to check
the API from a consumer's side walked the tree with read_dir(), sorted
the entries of every directory, hashed every regular file, numbered
the hard links and removed its output again when anything failed.
And a tool that makes a file system, such as ufat or the mkfs tools,
shouldn't just generate the manifest after the fact by reading its
image back. It should write the manifest right when it makes the file
system. It stats and copies every file it puts in anyway, so it has
what the manifest records at hand. Such a tool comes to its files in
whatever order it copies them though, and not in the order of the
manifest.
So the builder takes the root and the file objects in any order, and
finish() sorts them, numbers the hard links and writes the manifest:
For a tree of files that is on the host already, generate_at() and
generate() do all of it in one call. They walk the tree, read what the
manifest records of every file and write the manifest:
The program from above is examples/mkmanifest.rs now.
A few notes on the builder:
Memory doesn't grow with the number of files. The file objects go
through an external merge sort and the hard links into a hash table,
and both move to scratch files once they don't fit into memory
anymore. The scratch files are made next to the new manifest file.
For a manifest that is written to a stream they are made in the
directory TMPDIR names, and in /var/tmp without one. For 1e7 files
that are added in random order, with every fourth name a hard link,
the adds take 8.6 s and writing the 1370 MiB of manifest another
10 s, in 40 MiB of memory. That's the listing alone, without reading
the files.
The walk is by descriptor throughout. It never follows a symbolic
link, and it doesn't leave the mount it started on unless it is
asked to.
The walk mounts nothing. An automount point that isn't triggered yet
is listed as an empty directory and never opened. There's one window
left in which an open can still trigger one. It's documented, and
closing it is in TODO.md.
A file that changes while it is read fails the walk where statx()
shows the change. That's a best effort, and the documentation says
which changes don't show.
The new file of create_at() and generate_at() appears once the
manifest is complete, and it never replaces anything.
The C library is the cargo feature capi. It comes as libuapi16.so.0
with uapi16.h and uapi16.pc, and build.sh and install.sh build and
install it. We want it for build tools and for the mkfs tools. So it
has what they need of the Rust API and no more: the builder, the
reader and the writer of records, the file object, a way to get at the
data of a file and to check it, and SHA-256. That's 125 functions for
11 objects. uapi16.h says what each of them does, and examples/ has
six programs that use them.
We want the API of 0.1.0 to be minimal, in Rust and in C. So:
Decoder, Step and Encoder are private now. None of the users we
looked at calls them. Reader and Writer, which sit on top of them,
keep their API.
There's no public API to add and finish in steps. The builder works
in steps inside already, so one can be added later without changing
anything 0.1.0 has. It's in TODO.md.
The crate is Linux only. So far it built for Android as well, without
PinnedDirectory and DirectoryProvider, and the builder would have
needed a lot more special cases there. I don't think we need to care
about Android for now.
A few smaller changes come before the builder:
from_metadata() leaves out an owner a manifest can't have, such as
the user ID 65535, instead of making an object that no writer takes.
A write that fails ends the Writer, and every later call fails with
ESTALE. So far a caller that tried again after EAGAIN could end up
with a complete manifest that lacks a file object.
FileObject::retain_features() takes away the fields of the features
a manifest doesn't record.
Sha256Context and DataCheck get update_zeros() for the holes of a
sparse file, and PinnedDirectory gets open_data() for a tool that
reads the data of a file itself.
This is a large series, about 46000 lines, but well over half of that
is tests. The builder comes in pieces that can be reviewed one at a
time: its errors, reading one entry of a tree, the optional features,
the scratch files, the table of the hard links, the sort and the
listing, and the walk. Each piece comes with its tests, and nothing but
the tests uses it until the commit that puts ManifestOptions and
ManifestWriter on top.
Note that the builder reopens files through pathrs on a kernel without
O_EMPTYPATH, and pathrs 0.2.6 panics where it can't use /proc. I'm
fine with that for now: libpathrs's main branch has dropped both
panics, and a release with that takes care of it. The option to drop
pathrs is in TODO.md, which the last commit adds with everything else
we left for later.
Every commit builds and passes clippy and the tests with rustc 1.85
and the current stable, and for the cross targets, musl and i686. The
tests of the builder make mounts of their own in user namespaces, so
the CI jobs that run the tests allow those now, and a new job runs the
tests that need FAT. The C tests run against the shared and the static
library with the address and the undefined behavior sanitizer. The
tests that take an automount point skip in CI since only root can make
one, and so does the test for a file system that keeps a file that was
unlinked while it is open, as NFS does. Those were run by hand in a
VM.
TODO: add a list of work for later releases
uapi16: document the manifest builder
uapi16: add the C API
uapi16: add the manifest builder
uapi16: walk the trees of the manifest builder
uapi16: sort the file objects of the manifest builder
uapi16: keep the hard links of a tree in a table on disk
uapi16: add scratch files for the manifest builder
uapi16: read what the optional features record of a file
uapi16: read one entry of a tree from a descriptor
uapi16: add the errors of the manifest builder
uapi16: add PinnedDirectory::open_data()
uapi16: add update_zeros() to Sha256Context and DataCheck
uapi16: add FileObject::retain_features()
uapi16: make Decoder, Step and Encoder private
uapi16: let a failed write end the Writer
uapi16: leave IDs a manifest can't have out of from_metadata()
ci: let the tests use user namespaces
uapi16: support Linux only