schfs: the commit barrier, reflink, and attributes with an index over them
The last three phases of the filesystem, which were written together and
touch the same files throughout -- kmount.c, objw.c and vfs.c are each
rewritten by all three -- so they land as one commit rather than three
that could not be built separately.
Durability. A transaction that reaches the platter out of order is worse
than one that never reached it: the uberblock can name a tree whose
blocks are still in the drive's write cache. bdirty/bflush give the
buffer layer an explicit barrier, ideflush issues ATA FLUSH CACHE and
usb_storage_flush the SCSI SYNCHRONIZE CACHE that means the same thing,
and a commit now waits for the data before it writes the root that
points at it. The uberblock groups are read newest-first with fallback,
so a volume whose newest roots are torn mounts at an older one and says
which generations were lost.
Reflink. Extents carry a refcount, so two objects can name the same
blocks and neither frees them while the other holds them. Exposed as
FICLONE -- Linux's ioctl value, so `cp --reflink` works unmodified --
and as cmd/reflink for the case where there is no cp to hand.
Attributes, and the one genuinely new idea here. attr.c stores named
values on an object, reachable through setxattr/getxattr/removexattr at
Linux's syscall numbers. On top of that, schfs_index declares an index
over an attribute name and schfs_query answers "which objects carry this
value" from the B-tree rather than by walking. A query against an
unindexed attribute is refused with an error, never silently answered by
a full scan: a query that quietly becomes a scan is worse than one that
admits it cannot help. cmd/schattr drives both.
Three bugs fixed on the way, each found by the numbers being wrong
rather than by inspection:
- statfs's f_fsid was declared int64[2]. Linux's fsid_t is two *ints*,
so every field after it landed one slot late: userland read f_frsize
out of f_namelen, got 255, and df reported a 32 MB root filesystem
as 1992 KB. It hid for as long as it did because the fields used to
be invented constants, so wrong numbers looked like the usual ones.
- struct inode's nlink was a short against SchFS's uint32, so a file
past 32767 names came back negative -- and iput frees an object
whose count reaches zero. It is an int now, signed on purpose, so an
underflow lands somewhere a check can see rather than wrapping.
- filewrite split every write to fit v1's log. That limit belongs to
the journal, not to writing, and SchFS makes one transaction of
whatever it is handed: a 1 MB write was becoming 170 commits and
170 flushes for nothing. fs_ops carries max_write now, and SchFS
sets no limit.
Also here: a real mount table, so `mount -t schfs /dev/hdb /mnt` shows
up in /proc/mounts -- df looks a path up there before it calls statfs at
all, so a mount missing from that list was invisible to it -- several
volumes mounted at once, snapshots mounted by name, and /proc/fsstat
with an rdtsc-based counter, which is the only instrument fine enough to
see inside a single syscall.
Verified: `make schfs-check` passes in full, including the sweep that
crashes the volume at each of 119 points mid-transaction and requires
every one to mount with exactly its committed data, and the case where
both newest uberblocks are rubbish and the volume still comes back. The
headless boot check passes with 27 [ok] and no failures, on a SchFS root.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
f4d5c671a7
commit
03cc316001
39 changed files with 3942 additions and 550 deletions
59
cmd/reflink/main.c
Normal file
59
cmd/reflink/main.c
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// reflink -- copy a file by sharing its blocks instead of duplicating them.
|
||||
//
|
||||
// reflink <src> <dst>
|
||||
//
|
||||
// The copy is instant and free whatever the file's size: only metadata is
|
||||
// written. Both files are ordinary and writable afterwards; the first write to
|
||||
// either copies just the run it touches. `df` before and after is the check
|
||||
// that it really shared rather than quietly copying.
|
||||
//
|
||||
// Uses FICLONE, which is Linux's ioctl for exactly this, so the same call works
|
||||
// against any filesystem that supports cloning.
|
||||
|
||||
#include "types.h"
|
||||
#include "user.h"
|
||||
#include "fcntl.h"
|
||||
|
||||
#define FICLONE 0x40049409
|
||||
|
||||
static void
|
||||
puts_(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n]) n++;
|
||||
write(2, (void *)s, n);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 3) {
|
||||
puts_("usage: reflink <src> <dst>\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
int src = open(argv[1], O_RDONLY);
|
||||
if (src < 0) {
|
||||
puts_("reflink: cannot open "); puts_(argv[1]); puts_("\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
// O_TRUNC matters: FICLONE replaces the destination's contents entirely,
|
||||
// and creating it non-empty would leave blocks to release mid-clone.
|
||||
int dst = open(argv[2], O_CREATE | O_WRONLY | O_TRUNC);
|
||||
if (dst < 0) {
|
||||
puts_("reflink: cannot create "); puts_(argv[2]); puts_("\n");
|
||||
close(src);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (ioctl(dst, FICLONE, (void *)(long)src) < 0) {
|
||||
puts_("reflink: FICLONE failed\n");
|
||||
close(src); close(dst);
|
||||
exit();
|
||||
}
|
||||
|
||||
close(src);
|
||||
close(dst);
|
||||
exit();
|
||||
}
|
||||
116
cmd/schattr/main.c
Normal file
116
cmd/schattr/main.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
// schattr -- attach metadata to files, and ask the filesystem about it.
|
||||
//
|
||||
// schattr set <file> <name> <value> attach a value
|
||||
// schattr get <file> <name> read it back
|
||||
// schattr rm <file> <name> remove it
|
||||
// schattr index <name> start indexing that attribute
|
||||
// schattr find <name> <value> every object carrying that value
|
||||
//
|
||||
// `find` is the point of the rest. Without an index it is refused rather than
|
||||
// answered by walking every file, because a query that silently degrades into a
|
||||
// full scan is worse than one that says it cannot help: the first teaches you to
|
||||
// use it on a filesystem where it will not scale.
|
||||
//
|
||||
// Indexing covers values written *after* the index is declared. There is no
|
||||
// reindexing pass, so declare the index first.
|
||||
|
||||
#include "types.h"
|
||||
#include "user.h"
|
||||
|
||||
static void
|
||||
puts_(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n]) n++;
|
||||
write(1, (void *)s, n);
|
||||
}
|
||||
|
||||
static void
|
||||
putn_(unsigned long long v)
|
||||
{
|
||||
char buf[24];
|
||||
int i = 0;
|
||||
if (v == 0) { write(1, "0", 1); return; }
|
||||
while (v > 0 && i < (int)sizeof(buf)) { buf[i++] = '0' + (int)(v % 10); v /= 10; }
|
||||
while (i--) write(1, &buf[i], 1);
|
||||
}
|
||||
|
||||
static int
|
||||
streq(const char *a, const char *b)
|
||||
{
|
||||
while (*a && *a == *b) { a++; b++; }
|
||||
return *a == *b;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
puts_("usage: schattr set|get|rm <file> <name> [value]\n");
|
||||
puts_(" schattr index <name>\n");
|
||||
puts_(" schattr find <name> <value>\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (streq(argv[1], "set") && argc == 5) {
|
||||
int len = 0;
|
||||
while (argv[4][len]) len++;
|
||||
if (setxattr(argv[2], argv[3], argv[4], len, 0) < 0) {
|
||||
puts_("schattr: set failed\n");
|
||||
exit();
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
if (streq(argv[1], "get") && argc == 4) {
|
||||
char val[256];
|
||||
int n = getxattr(argv[2], argv[3], val, sizeof(val) - 1);
|
||||
if (n < 0) {
|
||||
puts_("schattr: no such attribute\n");
|
||||
exit();
|
||||
}
|
||||
val[n] = '\0';
|
||||
puts_(val);
|
||||
puts_("\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (streq(argv[1], "rm") && argc == 4) {
|
||||
if (removexattr(argv[2], argv[3]) < 0) {
|
||||
puts_("schattr: remove failed\n");
|
||||
exit();
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
if (streq(argv[1], "index") && argc == 3) {
|
||||
if (schfs_index(argv[2]) < 0) {
|
||||
puts_("schattr: could not create the index\n");
|
||||
exit();
|
||||
}
|
||||
puts_("schattr: indexing "); puts_(argv[2]);
|
||||
puts_(" from now on\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (streq(argv[1], "find") && argc == 4) {
|
||||
unsigned long long ids[64];
|
||||
int n = schfs_query(argv[2], argv[3], ids, 64);
|
||||
if (n < 0) {
|
||||
puts_("schattr: no index on "); puts_(argv[2]);
|
||||
puts_(" -- declare one with `schattr index` first\n");
|
||||
exit();
|
||||
}
|
||||
putn_((unsigned long long)n);
|
||||
puts_(n == 1 ? " object:\n" : " objects:\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
puts_(" #");
|
||||
putn_(ids[i]);
|
||||
puts_("\n");
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
puts_("schattr: bad usage\n");
|
||||
exit();
|
||||
}
|
||||
Loading…
Reference in a new issue