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>
177 lines
7.4 KiB
C
177 lines
7.4 KiB
C
struct file {
|
|
enum { FD_NONE, FD_PIPE, FD_INODE, FD_SOCKET } type;
|
|
int ref; // reference count
|
|
char readable;
|
|
char writable;
|
|
// O_NONBLOCK. A property of the open file description, not of the
|
|
// descriptor, which is why it lives here and not in myproc()->ofile_flags --
|
|
// dup() shares it and F_SETFL on either fd changes it for both, exactly as
|
|
// POSIX says.
|
|
char nonblock;
|
|
struct pipe *pipe;
|
|
struct inode *ip;
|
|
struct socket *sock;
|
|
uint64 off;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Filesystem operations
|
|
//
|
|
// One table per filesystem, chosen by iget() from the device number and hung
|
|
// off the inode. Everything above this line -- the syscall layer, exec, the
|
|
// page cache -- calls the wrappers in fs.c and never learns which filesystem it
|
|
// is talking to.
|
|
//
|
|
// A vtable rather than a chain of `if (dev == SCHFS_DEV)` because there are two
|
|
// filesystems today and there will be more: the whole point of the exercise is
|
|
// that adding one is a new table, not another branch in fifteen functions.
|
|
//
|
|
// Every hook takes an inode that is already locked, except ialloc (which has no
|
|
// inode yet) and iload (which is what makes one valid in the first place).
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct stat;
|
|
|
|
struct fs_ops {
|
|
const char *name;
|
|
|
|
// Fill in the type/size/link fields of a freshly iget()'d inode.
|
|
void (*iload)(struct inode *);
|
|
// Write those fields back to disk.
|
|
void (*istore)(struct inode *);
|
|
// The last link is gone and nobody has it open: release the object.
|
|
void (*ifree)(struct inode *);
|
|
|
|
int (*read)(struct inode *, int user_dst, uint64 dst, uint64 off, uint n);
|
|
int (*write)(struct inode *, int user_src, uint64 src, uint64 off, uint n);
|
|
// Set the length. Growing is allowed only where the filesystem can represent
|
|
// the gap; v1 cannot and ignores it.
|
|
void (*truncate)(struct inode *, uint64 newsize);
|
|
|
|
// Directory operations. `poff` is an opaque cookie identifying where the
|
|
// entry lives, meaningful only to the same filesystem's dirunlink.
|
|
struct inode *(*dirlookup)(struct inode *dp, char *name, uint *poff);
|
|
int (*dirlink)(struct inode *dp, char *name, uint64 inum);
|
|
int (*dirunlink)(struct inode *dp, char *name, uint off);
|
|
int (*dirempty)(struct inode *dp);
|
|
|
|
// Create an object with no name yet. The caller sets nlink and calls
|
|
// dirlink; until it does, the object is unreachable.
|
|
struct inode *(*ialloc)(uint dev, short type);
|
|
|
|
// Read directory entries into a getdents64-shaped stream. `off` is an opaque
|
|
// position that starts at 0 and is echoed back; returns bytes written, 0 at
|
|
// the end of the directory, or negative on error.
|
|
int (*getdents)(struct inode *dp, char *out, int cap, uint64 *off);
|
|
|
|
// The largest write this filesystem wants in one call, or 0 for "no limit".
|
|
//
|
|
// v1 needs one: a write has to fit in the on-disk log, so filewrite splits
|
|
// anything larger and each piece is its own transaction. That limit is a
|
|
// property of the journal, not of writing, and applying it to SchFS was
|
|
// actively harmful -- SchFS makes one transaction out of whatever it is
|
|
// handed, so chopping a 1 MB write into 6 KB pieces bought 170 commits and
|
|
// the flushes that go with them for nothing.
|
|
uint max_write;
|
|
};
|
|
|
|
// In-memory inode.
|
|
//
|
|
// `inum` and `size` are 64-bit because the filesystem underneath may be: SchFS
|
|
// object ids and file sizes are both uint64 on disk, and a 32-bit copy here
|
|
// would silently truncate them into a different file. The v1 filesystem uses a
|
|
// small fraction of the range and is unaffected.
|
|
struct inode {
|
|
uint dev;
|
|
uint64 inum;
|
|
int ref;
|
|
struct sleeplock lock;
|
|
int valid;
|
|
const struct fs_ops *ops; // chosen by iget from dev; never null once held
|
|
|
|
short type;
|
|
short major;
|
|
short minor;
|
|
// int, not short. SchFS stores a uint32 link count and this truncated it, so
|
|
// a file that passed 32767 names came back with a negative one -- and iput
|
|
// frees an object whose count reaches zero. Signed rather than unsigned on
|
|
// purpose: `nlink--` on a count that is somehow already zero should land
|
|
// somewhere a check can see it, not wrap to four billion.
|
|
int nlink;
|
|
uint64 size;
|
|
|
|
uint rwmode;
|
|
uint supermode;
|
|
uint showmode;
|
|
uint atime;
|
|
uint mtime;
|
|
uint ctime;
|
|
uint addrs[NDIRECT+3];
|
|
};
|
|
#define I_VALID 0x2
|
|
|
|
// Device numbers for filesystems that are not a raw disk. Real disks are 0..3,
|
|
// and these are deliberately far from them so a stray value never looks like a
|
|
// SchFS inode. Nothing on any disk records them -- they are runtime identities
|
|
// for the inode cache, so the numbering can be rearranged freely.
|
|
//
|
|
// Mounted SchFS volumes: SCHFS_DEV through SCHFS_DEV + SCHFS_VOL_SLOTS - 1.
|
|
//
|
|
// This was a single number, because there was a single volume: kmount.c held
|
|
// one `kvol` and every write entry point named it directly, so `mount -t schfs
|
|
// /dev/hdb /mnt` ignored its source argument entirely and handed back whichever
|
|
// disk the kernel had found at boot. Slot 0 is that boot volume, which is why
|
|
// SCHFS_DEV alone still means "the root" everywhere that only ever wanted it.
|
|
#define SCHFS_VOL_SLOTS 4
|
|
#define SCHFS_DEV 64
|
|
#define SCHFS_IS_VOL_DEV(d) \
|
|
((d) >= SCHFS_DEV && (d) < SCHFS_DEV + SCHFS_VOL_SLOTS)
|
|
|
|
// Mounted snapshots. Same block device, an older tree root, read-only.
|
|
//
|
|
// Each needs its own device number because an inode is identified by
|
|
// (dev, inum) and a snapshot's object ids are the *same numbers* as the live
|
|
// tree's -- one number would make the views alias in the inode cache and hand
|
|
// a reader whichever was cached first. So a slot is a device: SCHFS_SNAP_DEV
|
|
// through SCHFS_SNAP_DEV + SCHFS_SNAP_SLOTS - 1.
|
|
//
|
|
// Four rather than one because "one at a time" is an odd thing to explain and
|
|
// the cost is two scratch pages per slot, allocated on first use. Four rather
|
|
// than many because each is a device number and they are a fixed, small space.
|
|
#define SCHFS_SNAP_SLOTS 4
|
|
#define SCHFS_SNAP_DEV 96
|
|
#define SCHFS_IS_SNAP_DEV(d) \
|
|
((d) >= SCHFS_SNAP_DEV && (d) < SCHFS_SNAP_DEV + SCHFS_SNAP_SLOTS)
|
|
|
|
// Anything SchFS answers for, live volume or snapshot.
|
|
#define SCHFS_IS_ANY_DEV(d) (SCHFS_IS_VOL_DEV(d) || SCHFS_IS_SNAP_DEV(d))
|
|
|
|
// The synthetic /proc.
|
|
#define PROCFS_DEV 80
|
|
|
|
// table mapping major device number to
|
|
// device functions
|
|
struct devsw {
|
|
int (*read)(struct inode*, uint64, char*, int);
|
|
int (*write)(struct inode*, uint64, char*, int);
|
|
// Optional. Returns 0 when a read would block, which is the only thing
|
|
// fileread needs to know to honour O_NONBLOCK without every device driver
|
|
// growing a flags argument. A device that omits this is treated as always
|
|
// ready, which is the right answer for a disk and for /dev/zero.
|
|
int (*ready)(struct inode*);
|
|
// Optional. The last descriptor for this open file is going away. A device
|
|
// that hands a program exclusive-feeling control of the hardware needs this
|
|
// to put the hardware back -- /dev/fb0 uses it to give the text console its
|
|
// screen back when whatever was drawing on it exits.
|
|
void (*close)(struct inode*);
|
|
// Optional. A descriptor has just been opened on this device. /dev/input
|
|
// uses it to discard events that arrived while nobody was listening -- see
|
|
// inputopen().
|
|
void (*open)(struct inode*);
|
|
};
|
|
|
|
extern struct devsw devsw[];
|
|
|
|
#define CONSOLE 1
|
|
// Major number of /dev/input/eventN. The minor is the device index.
|
|
#define INPUT_MAJOR 7
|