fs: follow symlinks like Linux; Linux mknod; /proc/partitions
Symlinks are followed in namex_from() now, in the middle of a path as well as at the end, and a relative target resolves from the directory that holds the link rather than from the caller's cwd. `ln -s f d1/s; cat d1/s` said "Symbolic link loop" because it looked for ./f. A loop is ELOOP after 40 links, a dangling link ENOENT, and open(O_CREAT) through a link to a file opens the file. divesymlink() is gone. mknod from a Linux program passes (mode, dev) where the native call passes (major, minor), and was read as the native one: `mknod n c 1 2` made node 432,258, and mkfifo made a character device. Both are decoded now; a FIFO is EPERM, and an existing name is EEXIST. /proc/mounts listed devtmpfs twice: once from a fixed line and once from the mount table. The fixed line is gone, and mount_add() refuses a filesystem mounted on its own root, which is where a second devtmpfs mount lands. A read-only FAT mount says ro. /proc/partitions exists, in Linux's format, and /proc/filesystems lists vfat. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
parent
edb560de66
commit
7a1e63a553
8 changed files with 274 additions and 136 deletions
|
|
@ -372,23 +372,35 @@ putu(char *buf, int cap, int off, uint64 v)
|
|||
return off;
|
||||
}
|
||||
|
||||
// putu(), right-aligned in `w` columns, the way printf("%*llu") would.
|
||||
static int
|
||||
putw(char *buf, int cap, int off, uint64 v, int w)
|
||||
{
|
||||
int n = 1;
|
||||
for (uint64 t = v; t >= 10; t /= 10)
|
||||
n++;
|
||||
for (; w > n; w--)
|
||||
off = put(buf, cap, off, " ");
|
||||
return putu(buf, cap, off, v);
|
||||
}
|
||||
|
||||
// /proc/partitions, in Linux's format to the column, because that is what
|
||||
// reads it: lsblk's fallback, fdisk -l, every installer and rescue script.
|
||||
// Sizes are in 1 KB blocks. The major is 8 for all of them, the one devtmpfs
|
||||
// gives every disk here, and the minor is the device number, as in /dev.
|
||||
int
|
||||
blkdev_info(char *buf, int cap)
|
||||
{
|
||||
int off = put(buf, cap, 0, "dev name MB type\n");
|
||||
int off = put(buf, cap, 0, "major minor #blocks name\n\n");
|
||||
for (int s = 0; s < NBLKDEV; s++) {
|
||||
struct blkdev *d = &devs[s];
|
||||
if (!d->used)
|
||||
continue;
|
||||
off = putu(buf, cap, off, BLKDEV_FIRST + s);
|
||||
off = put(buf, cap, off, " ");
|
||||
off = putw(buf, cap, off, 8, 4);
|
||||
off = putw(buf, cap, off, BLKDEV_FIRST + s, 8);
|
||||
off = putw(buf, cap, off, (d->nsectors * d->secsize) >> 10, 11);
|
||||
off = put(buf, cap, off, " ");
|
||||
off = put(buf, cap, off, d->name);
|
||||
for (int pad = 12 - strlen(d->name); pad > 0; pad--)
|
||||
off = put(buf, cap, off, " ");
|
||||
off = putu(buf, cap, off, (d->nsectors * d->secsize) >> 20);
|
||||
off = put(buf, cap, off, d->parent < 0 ? " disk" :
|
||||
d->schfs > 0 ? " schfs" :
|
||||
d->schfs < 0 ? " fat" : " part");
|
||||
off = put(buf, cap, off, "\n");
|
||||
}
|
||||
return off;
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@
|
|||
#include "sbuf.h"
|
||||
#include "kmem.h"
|
||||
#include "dbg.h"
|
||||
#include "blkdev.h"
|
||||
#include "fat.h"
|
||||
|
||||
#define PROC_BUFSZ 2048
|
||||
|
||||
|
|
@ -241,9 +243,9 @@ gen_mounts(struct sbuf *sb)
|
|||
char path[MAXPATH], source[MAXPATH], type[16];
|
||||
uint fsdev;
|
||||
|
||||
sb_puts(sb, "devtmpfs /dev devtmpfs rw 0 0\n");
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// Every mount is in the table, /dev included. A fixed devtmpfs line here
|
||||
// listed it twice once init mounted it the ordinary way.
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (mount_get(i, path, source, type, &fsdev) != 0)
|
||||
continue;
|
||||
// "/" is emitted last, below, so skip it if it somehow appears here.
|
||||
|
|
@ -256,7 +258,8 @@ gen_mounts(struct sbuf *sb)
|
|||
sb_puts(sb, type[0] ? type : "schfs");
|
||||
// A mounted snapshot is genuinely read-only, and saying so is what stops
|
||||
// anything trying to write to one and reporting a confusing EROFS later.
|
||||
sb_puts(sb, SCHFS_IS_SNAP_DEV(fsdev) ? " ro 0 0\n" : " rw 0 0\n");
|
||||
int ro = SCHFS_IS_SNAP_DEV(fsdev) || (FAT_IS_DEV(fsdev) && fat_readonly(fsdev));
|
||||
sb_puts(sb, ro ? " ro 0 0\n" : " rw 0 0\n");
|
||||
}
|
||||
|
||||
// Root must be last: df keeps the last mount point that prefixes the path
|
||||
|
|
@ -323,6 +326,7 @@ gen_filesystems(struct sbuf *sb)
|
|||
sb_puts(sb, "\tschfs\n");
|
||||
sb_puts(sb, "nodev\tproc\n");
|
||||
sb_puts(sb, "nodev\tdevtmpfs\n");
|
||||
sb_puts(sb, "\tvfat\n");
|
||||
}
|
||||
|
||||
// --- dispatch --------------------------------------------------------------
|
||||
|
|
@ -347,6 +351,7 @@ gen_filesystems(struct sbuf *sb)
|
|||
#define PROC_BOOTMODE 19
|
||||
#define PROC_BOOTDEBUG 30
|
||||
#define PROC_SLAB 20
|
||||
#define PROC_PARTITIONS 21
|
||||
|
||||
// Render global file `id` (a PROC_* constant) into `buf`. Returns its length.
|
||||
//
|
||||
|
|
@ -401,6 +406,8 @@ proc_gen_global(int id, char *buf, int cap)
|
|||
sb_puts(&sb, boot_debug ? "on\n" : "off\n");
|
||||
break;
|
||||
case PROC_DEVICES: gen_devices(&sb); break;
|
||||
// The block layer renders its own table, like PCI and SchFS above.
|
||||
case PROC_PARTITIONS: sb.len = blkdev_info(buf, PROC_BUFSZ); break;
|
||||
default: return 0;
|
||||
}
|
||||
return sb.len;
|
||||
|
|
|
|||
Loading…
Reference in a new issue