| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169 |
- /* Virtual filesystem: mount every device with a valid FAT filesystem under `/`,
- * and the command pack (ramfs) at /ramfs. File operations go through the
- * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */
- #include "vfs.h"
- #include "fatdetect.h"
- #include "io.h"
- #include "kmalloc.h"
- #include "process.h"
- #include "sched.h"
- #include "ramfs.h"
- #include "rootfs.h"
- #include "serial.h"
- #include "share.h"
- #include "syscall.h"
- #include "timer.h"
- vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
- unsigned int vfs_mount_count;
- /* Set while vfs_mount_all() rebuilds the mount table. File syscalls wait on
- * this (sys_fs.c) so they never resolve a path in a half-rebuilt table or use
- * the FAT sector buffer while the scan is reading disks. The owner (a
- * `storage` syscall) is exempt. */
- volatile int vfs_scan_busy;
- volatile unsigned int vfs_scan_owner_pid;
- static void put_dec(unsigned int n)
- {
- char buf[12];
- unsigned int i = sizeof(buf) - 1;
- buf[i] = '\0';
- if (n == 0) {
- io_putchar('0');
- return;
- }
- while (n && i > 0) {
- buf[--i] = (char)('0' + (n % 10));
- n /= 10;
- }
- io_putstr(buf + i);
- }
- /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
- * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
- static void put_size_sectors(unsigned int sectors)
- {
- if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
- unsigned int gb = sectors >> 21;
- unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
- put_dec(gb);
- if (tenths) {
- io_putchar('.');
- put_dec(tenths);
- }
- io_putstr(" GB");
- } else if (sectors >= 2048u) { /* >= 1 MiB */
- unsigned int mb = sectors >> 11;
- unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
- put_dec(mb);
- if (tenths) {
- io_putchar('.');
- put_dec(tenths);
- }
- io_putstr(" MB");
- } else {
- put_dec(sectors / 2u);
- io_putstr(" KB");
- }
- }
- static const char *fat_name(fat_type_t t)
- {
- switch (t) {
- case FAT12: return "FAT12";
- case FAT16: return "FAT16";
- case FAT32: return "FAT32";
- default: return "FAT";
- }
- }
- static void mem_copy(void *dst, const void *src, unsigned int n)
- {
- unsigned char *d = (unsigned char *)dst;
- const unsigned char *s = (const unsigned char *)src;
- while (n--)
- *d++ = *s++;
- }
- static void copy_bounded(char *dst, const char *src, unsigned int size)
- {
- unsigned int i = 0;
- while (i + 1 < size && src[i]) {
- dst[i] = src[i];
- i++;
- }
- dst[i] = '\0';
- }
- /* ---- FAT implementation ---- */
- static void fat_cache_clear(void);
- static int fat_op_read_file(struct vfs_mount *m, const char *path,
- void *buf, unsigned int max)
- {
- unsigned int dir;
- char name[12];
- int r = fat_resolve_path(&m->fs, path, &dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT;
- r = fat_read_file_in(&m->fs, dir, name, buf, max);
- if (r == -2)
- return VFS_EIO;
- if (r < 0)
- return VFS_ENOENT;
- return r;
- }
- static int fat_op_stat(struct vfs_mount *m, const char *path,
- unsigned int *size, int *is_dir)
- {
- int r = fat_stat(&m->fs, path, size, is_dir);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT;
- return 0;
- }
- static int fat_op_load_file(struct vfs_mount *m, const char *path,
- struct vfs_file *out)
- {
- unsigned int size = 0;
- int is_dir = 0, r;
- void *buf;
- r = fat_op_stat(m, path, &size, &is_dir);
- if (r != 0)
- return r;
- if (is_dir || size == 0 || size > VFS_FILE_MAX)
- return VFS_EINVAL;
- /* One command is loaded at a time; the ELF loader has copied the
- * segments out before release_file() runs, so a heap buffer is safe. */
- buf = kmalloc(size);
- if (!buf)
- return VFS_EINVAL;
- r = fat_op_read_file(m, path, buf, size);
- if (r < 0) {
- kfree(buf);
- return r;
- }
- out->data = buf;
- out->size = (unsigned int)r;
- return 0;
- }
- static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
- {
- (void)m;
- kfree((void *)f->data);
- f->data = 0;
- f->size = 0;
- }
- static int fat_op_write_file(struct vfs_mount *m, const char *path,
- const void *buf, unsigned int len)
- {
- fat_cache_clear();
- unsigned int dir;
- char name[12];
- int r = fat_resolve_path(&m->fs, path, &dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL;
- r = fat_write_file_in(&m->fs, dir, name, buf, len);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL;
- return 0;
- }
- static int fat_op_unlink(struct vfs_mount *m, const char *path)
- {
- fat_cache_clear();
- unsigned int dir;
- char name[12];
- int r = fat_resolve_path(&m->fs, path, &dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT;
- r = fat_delete_file_in(&m->fs, dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT;
- return 0;
- }
- static int fat_op_mkdir(struct vfs_mount *m, const char *path)
- {
- fat_cache_clear();
- unsigned int dir;
- char name[12];
- int r = fat_resolve_path(&m->fs, path, &dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL;
- r = fat_mkdir(&m->fs, dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL; /* the FAT layer printed its own reason */
- return 0;
- }
- static int fat_op_rmdir(struct vfs_mount *m, const char *path)
- {
- fat_cache_clear();
- unsigned int dir;
- char name[12];
- int r = fat_resolve_path(&m->fs, path, &dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL;
- r = fat_rmdir(&m->fs, dir, name);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_EINVAL;
- return 0;
- }
- /* Index-based listing used to re-walk the FAT directory for every index, and
- * the WebDAV server asks for 0..N-1 in one PROPFIND. Cache the first 24
- * entries of the last directory so a listing is one directory walk; a write
- * invalidates it. Larger directories fall back to the old walk past 24. */
- #define FAT_LIST_CACHE 24
- static struct vfs_dirent fat_cache[FAT_LIST_CACHE];
- static unsigned int fat_cache_count;
- static int fat_cache_valid;
- static int fat_cache_truncated;
- static struct vfs_mount *fat_cache_mount;
- static char fat_cache_path[VFS_PATH_MAX];
- static void fat_cache_clear(void)
- {
- fat_cache_valid = 0;
- }
- static int fat_path_eq(const char *a, const char *b)
- {
- unsigned int i = 0;
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- return a[i] == '\0' && b[i] == '\0';
- }
- static void fat_cache_cb(const char *name, int is_dir, unsigned int size)
- {
- struct vfs_dirent *d;
- unsigned int i;
- if (fat_cache_count >= FAT_LIST_CACHE) {
- fat_cache_truncated = 1;
- return;
- }
- d = &fat_cache[fat_cache_count++];
- for (i = 0; i < 11; i++)
- d->name[i] = name[i] ? name[i] : ' ';
- d->name[11] = '\0';
- d->size = size;
- d->is_dir = (unsigned int)is_dir;
- d->raw_83 = 1;
- }
- /* Old behaviour: one directory walk, counting to `index`. */
- static unsigned int fat_list_index;
- static struct vfs_dirent *fat_list_out;
- static int fat_list_done;
- static unsigned int fat_list_seen;
- static void fat_list_cb(const char *name, int is_dir, unsigned int size)
- {
- unsigned int i;
- if (fat_list_done || fat_list_seen < fat_list_index) {
- fat_list_seen++;
- return;
- }
- for (i = 0; i < 11; i++)
- fat_list_out->name[i] = name[i] ? name[i] : ' ';
- fat_list_out->name[11] = '\0';
- fat_list_out->size = size;
- fat_list_out->is_dir = (unsigned int)is_dir;
- fat_list_out->raw_83 = 1;
- fat_list_done = 1;
- fat_list_seen++;
- }
- static int fat_list_walk(struct vfs_mount *m, const char *path,
- unsigned int index, struct vfs_dirent *out)
- {
- unsigned int dir;
- int r = fat_resolve_dir(&m->fs, path, &dir);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT;
- fat_list_index = index;
- fat_list_seen = 0;
- fat_list_out = out;
- fat_list_done = 0;
- r = fat_list_dir(&m->fs, dir, fat_list_cb);
- if (r == -2)
- return VFS_EIO;
- return fat_list_done ? 1 : 0;
- }
- static int fat_op_list_dir(struct vfs_mount *m, const char *path,
- unsigned int index, struct vfs_dirent *out)
- {
- int same = fat_cache_valid && fat_cache_mount == m &&
- fat_path_eq(fat_cache_path, path);
- if (same) {
- if (index < fat_cache_count) {
- *out = fat_cache[index];
- return 1;
- }
- if (!fat_cache_truncated)
- return 0;
- /* Beyond the cached prefix: use the old walk. */
- return fat_list_walk(m, path, index, out);
- }
- {
- unsigned int dir;
- int r = fat_resolve_dir(&m->fs, path, &dir);
- if (r == -2)
- return VFS_EIO;
- if (r != 0)
- return VFS_ENOENT; /* not found / not a directory */
- fat_cache_count = 0;
- fat_cache_truncated = 0;
- fat_cache_mount = m;
- copy_bounded(fat_cache_path, path, sizeof(fat_cache_path));
- fat_cache_valid = 0;
- r = fat_list_dir(&m->fs, dir, fat_cache_cb);
- if (r == -2)
- return VFS_EIO;
- fat_cache_valid = 1;
- }
- if (index < fat_cache_count) {
- *out = fat_cache[index];
- return 1;
- }
- return 0;
- }
- static const struct fs_ops fat_ops = {
- .raw_83 = 1,
- .read_file = fat_op_read_file,
- .load_file = fat_op_load_file,
- .release_file = fat_op_release_file,
- .stat = fat_op_stat,
- .write_file = fat_op_write_file,
- .unlink = fat_op_unlink,
- .mkdir = fat_op_mkdir,
- .rmdir = fat_op_rmdir,
- .list_dir = fat_op_list_dir,
- };
- /* 1 for a block-device-backed mount (the FAT driver). A destructive format
- * rewrites such a mount; RAM-backed mounts stay usable while it runs. */
- int vfs_mount_is_disk(const vfs_mount_t *m)
- {
- return m && m->ops == &fat_ops;
- }
- void vfs_scan_poll(unsigned int t0)
- {
- if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2)
- vfs_scan_relax(1);
- }
- int vfs_scan_relax(unsigned int ms)
- {
- struct process *p;
- if (!vfs_scan_busy || !vfs_scan_owner_pid)
- return 0;
- p = current_proc();
- if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count)
- return 0;
- {
- unsigned int flags;
- __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
- if (!(flags & 0x200u))
- return 0; /* interrupts off: cannot sleep */
- }
- process_sleep_ms(ms ? ms : 1);
- return 1;
- }
- /* ---- ramfs implementation ---- */
- static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
- void *buf, unsigned int max)
- {
- const void *data;
- unsigned int size = 0, n;
- (void)m;
- data = ramfs_lookup(path, &size);
- if (!data)
- return VFS_ENOENT;
- n = size > max ? max : size;
- mem_copy(buf, data, n);
- return (int)n;
- }
- static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
- struct vfs_file *out)
- {
- const void *data;
- unsigned int size = 0;
- (void)m;
- data = ramfs_lookup(path, &size);
- if (!data)
- return VFS_ENOENT;
- out->data = data;
- out->size = size;
- return 0;
- }
- static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
- {
- (void)m;
- (void)f;
- }
- static int ramfs_op_stat(struct vfs_mount *m, const char *path,
- unsigned int *size, int *is_dir)
- {
- const void *data;
- unsigned int sz = 0;
- struct ramfs_dirent e;
- (void)m;
- if (path[0] == '\0') {
- if (size)
- *size = 0;
- if (is_dir)
- *is_dir = 1;
- return 0;
- }
- data = ramfs_lookup(path, &sz);
- if (data) {
- if (size)
- *size = sz;
- if (is_dir)
- *is_dir = 0;
- return 0;
- }
- if (ramfs_list(path, 0, &e) == 1) {
- if (size)
- *size = 0;
- if (is_dir)
- *is_dir = 1;
- return 0;
- }
- return VFS_ENOENT;
- }
- static int ramfs_op_ro(struct vfs_mount *m, const char *path)
- {
- (void)m;
- (void)path;
- return VFS_EROFS;
- }
- static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
- const void *buf, unsigned int len)
- {
- (void)buf;
- (void)len;
- return ramfs_op_ro(m, path);
- }
- static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
- {
- return ramfs_op_ro(m, path);
- }
- static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
- {
- return ramfs_op_ro(m, path);
- }
- static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
- {
- return ramfs_op_ro(m, path);
- }
- static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
- unsigned int index, struct vfs_dirent *out)
- {
- struct ramfs_dirent e;
- (void)m;
- if (ramfs_list(path, index, &e) != 1) {
- if (index == 0 && path[0] != '\0') {
- unsigned int size = 0;
- /* Nothing matched: report a missing path instead of an
- * empty directory (the pack has no empty directories --
- * one exists exactly as a prefix of file paths), and a
- * file as "not a directory". */
- if (ramfs_lookup(path, &size))
- return VFS_EINVAL;
- return VFS_ENOENT;
- }
- return 0; /* end of directory */
- }
- copy_bounded(out->name, e.name, sizeof(out->name));
- out->size = e.size;
- out->is_dir = e.is_dir;
- out->raw_83 = 0;
- return 1;
- }
- static const struct fs_ops ramfs_ops = {
- .raw_83 = 0,
- .read_file = ramfs_op_read_file,
- .load_file = ramfs_op_load_file,
- .release_file = ramfs_op_release_file,
- .stat = ramfs_op_stat,
- .write_file = ramfs_op_write_file,
- .unlink = ramfs_op_unlink,
- .mkdir = ramfs_op_mkdir,
- .rmdir = ramfs_op_rmdir,
- .list_dir = ramfs_op_list_dir,
- };
- int vfs_mount_is_ram(const vfs_mount_t *m)
- {
- return m && (m->ops == &ramfs_ops || m->ops == &share_ops);
- }
- /* ---- mounts ---- */
- static vfs_mount_t *mount_slot(const char *name, const char *dev)
- {
- unsigned int i;
- if (vfs_mount_count >= VFS_MAX_MOUNTS)
- return 0;
- {
- vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
- for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
- m->name[i] = name[i];
- m->name[i] = '\0';
- m->dev[0] = '\0';
- if (dev) {
- for (i = 0; dev[i] && i < sizeof(m->dev) - 1; i++)
- m->dev[i] = dev[i];
- m->dev[i] = '\0';
- }
- m->key[0] = '\0';
- m->mounted = 1;
- return m;
- }
- }
- static int name_eq(const char *a, const char *b)
- {
- unsigned int i = 0;
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- return a[i] == '\0' && b[i] == '\0';
- }
- /* The P1 boot sector keeps only the NebOS header fields; a real FAT volume
- * never carries these values. Older installed media still has the original
- * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead
- * of mounting the raw kernel partition as a fake filesystem. */
- static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
- {
- unsigned char s[512];
- unsigned int i;
- if (blk_read(d, start_lba, s, 1) != 0)
- return 0;
- if (s[510] != 0x55 || s[511] != 0xAA)
- return 0;
- if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' ||
- s[7] != 'S')
- return 0;
- if ((s[0x40] | (s[0x41] << 8)) != NEBOS_KERNEL_LBA)
- return 0;
- i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) |
- ((unsigned int)s[0x45] << 24);
- return i == ROOTFS_LBA;
- }
- /* ---- mount policies ---- */
- static struct sys_mount_policy vfs_policies[SYS_MOUNT_POLICY_MAX];
- static unsigned int vfs_policy_count;
- static struct sys_mount_policy *policy_find(const char *key)
- {
- unsigned int i;
- for (i = 0; i < vfs_policy_count; i++)
- if (name_eq(vfs_policies[i].key, key))
- return &vfs_policies[i];
- return 0;
- }
- /* A mount name or key: 1..15 of [a-z0-9_-]. */
- static int policy_name_ok(const char *s)
- {
- unsigned int i;
- for (i = 0; s[i]; i++) {
- char c = s[i];
- if (i >= 15 || !((c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9') || c == '_' || c == '-'))
- return 0;
- }
- return i > 0;
- }
- /* The block device a policy key names: "<dev>" or "<dev>p<1-4>". */
- static blk_dev_t *policy_device(const char *key)
- {
- unsigned int i, n;
- for (i = 0; i < blk_dev_count; i++) {
- const char *d = blk_devs[i].name;
- for (n = 0; d[n] && key[n] == d[n]; n++)
- ;
- if (d[n])
- continue;
- if (key[n] == '\0' ||
- (key[n] == 'p' && key[n + 1] >= '1' && key[n + 1] <= '4' &&
- key[n + 2] == '\0'))
- return &blk_devs[i];
- }
- return 0;
- }
- static void remount(blk_dev_t *d);
- int vfs_policy_set(const struct sys_mount_policy *p)
- {
- struct sys_mount_policy *e;
- blk_dev_t *d;
- if (!policy_name_ok(p->key) || p->name[sizeof(p->name) - 1] ||
- (p->name[0] && (!policy_name_ok(p->name) ||
- name_eq(p->name, "ramfs") ||
- name_eq(p->name, "share"))) ||
- (p->flags & ~(SYS_MOUNT_NOAUTO | SYS_MOUNT_RO)))
- return -1;
- d = policy_device(p->key);
- if (!d)
- return -1;
- e = policy_find(p->key);
- if (!p->name[0] && !p->flags) {
- if (e)
- *e = vfs_policies[--vfs_policy_count];
- } else {
- if (!e) {
- if (vfs_policy_count >= SYS_MOUNT_POLICY_MAX)
- return -1;
- e = &vfs_policies[vfs_policy_count++];
- }
- *e = *p;
- }
- remount(d);
- return 0;
- }
- int vfs_policy_get(unsigned int index, struct sys_mount_policy *out)
- {
- if (index >= vfs_policy_count)
- return 0;
- *out = vfs_policies[index];
- return 1;
- }
- static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
- {
- fat_fs_t fs;
- vfs_mount_t *m;
- const struct sys_mount_policy *pol = policy_find(name);
- const char *use_name = name;
- int r;
- if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
- return;
- fat_cache_clear();
- r = fat_mount(dev, part_lba, &fs);
- if (r != 0) {
- serial_puts(SERIAL_COM1, "vfs: ");
- serial_puts(SERIAL_COM1, name);
- serial_puts(SERIAL_COM1, " no FAT\n");
- return;
- }
- /* The labelled data partition has a stable path independent of the
- * device name. Only the first NEBOSDATA volume becomes /data. */
- if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
- use_name = "data";
- /* A policy name wins unless another volume already holds it. */
- if (pol && pol->name[0] && !vfs_find(pol->name))
- use_name = pol->name;
- m = mount_slot(use_name, dev->name);
- if (!m)
- return;
- copy_bounded(m->key, name, sizeof(m->key));
- m->ops = &fat_ops;
- m->fs = fs;
- m->ro = (pol && (pol->flags & SYS_MOUNT_RO)) ? 1 : 0;
- m->total_sectors = fs.total_sectors;
- copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
- copy_bounded(m->label, fs.label, sizeof(m->label));
- vfs_mount_count++;
- serial_puts(SERIAL_COM1, "vfs: ");
- serial_puts(SERIAL_COM1, use_name);
- serial_puts(SERIAL_COM1, " ");
- serial_puts(SERIAL_COM1, fat_name(fs.type));
- serial_puts(SERIAL_COM1, "\n");
- }
- void vfs_mount_ramfs(void)
- {
- vfs_mount_t *m;
- /* Mount even when the pack is empty: the runtime `cmds-dev` path can
- * install a pack later and vfs_refresh_ramfs() then updates the mount. */
- m = mount_slot("ramfs", 0);
- if (!m)
- return;
- m->ops = &ramfs_ops;
- m->ro = 1;
- m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
- copy_bounded(m->type, "ramfs", sizeof(m->type));
- m->label[0] = '\0';
- vfs_mount_count++;
- }
- void vfs_refresh_ramfs(void)
- {
- vfs_mount_t *m = vfs_find("ramfs");
- if (m)
- m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
- }
- /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a
- * block device and its contents survive vfs_mount_all() refreshes. */
- void vfs_mount_share(void)
- {
- vfs_mount_t *m;
- if (share_init() != 0)
- return;
- m = mount_slot("share", 0);
- if (!m)
- return;
- m->ops = &share_ops;
- m->ro = 0;
- m->total_sectors = SHARE_MAX_BYTES / 512u;
- copy_bounded(m->type, "share", sizeof(m->type));
- m->label[0] = '\0';
- vfs_mount_count++;
- }
- static void part_name(char *out, const char *base, unsigned int idx)
- {
- unsigned int i = 0;
- while (base[i] && i < 13) {
- out[i] = base[i];
- i++;
- }
- out[i++] = 'p';
- out[i++] = (char)('0' + idx + 1);
- out[i] = '\0';
- }
- static void mount_device(blk_dev_t *d, unsigned char *buf)
- {
- mbr_part_t parts[4];
- int n, j;
- int any_fat = 0;
- if (!d->present)
- return;
- if (d->kind == BLK_FLOPPY) {
- mount_one(d, 0, d->name);
- return;
- }
- /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
- if (blk_read(d, 0, buf, 1) != 0)
- return;
- n = mbr_parse(buf, parts);
- for (j = 0; j < n; j++) {
- char pname[16];
- if (!parts[j].is_fat || parts[j].start_lba == 0 ||
- parts[j].sectors == 0)
- continue;
- /* The active NebOS partition holds the raw boot sector and
- * kernel, not a filesystem (old images still look like FAT). */
- if (is_nebos_boot_partition(d, parts[j].start_lba))
- continue;
- any_fat = 1;
- part_name(pname, d->name, parts[j].index);
- mount_one(d, parts[j].start_lba, pname);
- }
- if (!any_fat)
- mount_one(d, 0, d->name);
- }
- /* Mounts remember their source device, so /data can be named independently
- * of <device>pN and still be found by a remount. */
- static int mount_belongs_to(const vfs_mount_t *m, const char *dev)
- {
- return m->dev[0] != '\0' && name_eq(m->dev, dev);
- }
- static void unmount_device(const char *dev)
- {
- unsigned int i, j;
- for (i = 0; i < vfs_mount_count; ) {
- if (mount_belongs_to(&vfs_mounts[i], dev)) {
- for (j = i + 1; j < vfs_mount_count; j++)
- vfs_mounts[j - 1] = vfs_mounts[j];
- vfs_mount_count--;
- } else {
- i++;
- }
- }
- }
- void vfs_mount_all(void)
- {
- static unsigned char buf[512];
- unsigned int i;
- /* Publish the scan before touching the table: a ring-3 file syscall
- * on another CPU waits for it instead of resolving a path halfway
- * through the rebuild. At boot current_proc() is idle (pid 0). */
- vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
- vfs_scan_busy = 1;
- vfs_mount_count = 0;
- /* Non-block filesystems first. The WM and terminal live on /ramfs,
- * so they stay usable while the (possibly slow) disk scan runs. */
- vfs_mount_ramfs();
- vfs_mount_share();
- blk_scan();
- for (i = 0; i < blk_dev_count; i++) {
- unsigned int t0 = get_ticks_ms();
- mount_device(&blk_devs[i], buf);
- serial_puts(SERIAL_COM1, "vfs: ");
- serial_puts(SERIAL_COM1, blk_devs[i].name);
- serial_puts(SERIAL_COM1, " mount ");
- serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
- serial_puts(SERIAL_COM1, " ms\n");
- }
- serial_puts(SERIAL_COM1, "vfs: ");
- serial_putdec(SERIAL_COM1, vfs_mount_count);
- serial_puts(SERIAL_COM1, " mount(s)\n");
- vfs_scan_busy = 0;
- }
- static int device_is_mounted(const char *dev)
- {
- unsigned int i;
- for (i = 0; i < vfs_mount_count; i++)
- if (mount_belongs_to(&vfs_mounts[i], dev))
- return 1;
- return 0;
- }
- static void remount(blk_dev_t *d)
- {
- unsigned char buf[512];
- fat_cache_clear();
- unmount_device(d->name);
- mount_device(d, buf);
- }
- void vfs_remount_device(blk_dev_t *d)
- {
- if (!d)
- return;
- remount(d);
- if (!device_is_mounted(d->name)) {
- io_putstr("vfs: remount of ");
- io_putstr(d->name);
- io_putstr(" found no filesystem\n");
- }
- }
- vfs_mount_t *vfs_find(const char *name)
- {
- unsigned int i;
- for (i = 0; i < vfs_mount_count; i++) {
- unsigned int j = 0;
- while (vfs_mounts[i].name[j] && name[j] &&
- vfs_mounts[i].name[j] == name[j])
- j++;
- if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
- return &vfs_mounts[i];
- }
- return 0;
- }
- void vfs_list_root(void)
- {
- unsigned int i;
- for (i = 0; i < vfs_mount_count; i++) {
- vfs_mount_t *m = &vfs_mounts[i];
- io_putstr(m->name);
- io_putstr(" ");
- io_putstr(m->type);
- io_putchar(' ');
- put_size_sectors(m->total_sectors);
- if (m->label[0]) {
- io_putstr(" \"");
- io_putstr(m->label);
- io_putchar('"');
- }
- if (m->ro)
- io_putstr(" (ro)");
- io_putchar('\n');
- }
- }
- int vfs_resolve(const char *path, struct vfs_mount **m_out,
- char *rel_path, unsigned int rel_size)
- {
- unsigned int i = 0, j = 0, o = 0;
- char name[16];
- vfs_mount_t *m;
- if (!path || path[0] != '/' || rel_size == 0)
- return -1;
- i = 1;
- while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
- name[j++] = path[i++];
- name[j] = '\0';
- if (j == 0)
- return -1; /* path is just "/" */
- m = vfs_find(name);
- if (!m)
- return -1;
- /* Copy the remainder as a normalized relative path: skip slashes, drop
- * "." components and never leave a trailing one. */
- while (path[i]) {
- unsigned int clen = 0;
- const char *comp;
- while (path[i] == '/')
- i++;
- if (!path[i])
- break;
- comp = path + i;
- while (path[i] && path[i] != '/') {
- i++;
- clen++;
- }
- if (clen == 1 && comp[0] == '.')
- continue;
- if (o + clen + 2 > rel_size)
- return -1;
- if (o > 0)
- rel_path[o++] = '/';
- {
- unsigned int k;
- for (k = 0; k < clen; k++)
- rel_path[o++] = comp[k];
- }
- }
- rel_path[o] = '\0';
- *m_out = m;
- return 0;
- }
- int vfs_load_file(const char *path, struct vfs_file *out,
- struct vfs_mount **m_out)
- {
- struct vfs_mount *m;
- char rel[VFS_PATH_MAX];
- int r;
- if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
- return VFS_ENOENT;
- r = m->ops->load_file(m, rel, out);
- if (r == 0 && m_out)
- *m_out = m;
- return r;
- }
- void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
- {
- if (m && m->ops->release_file)
- m->ops->release_file(m, f);
- }
- int vfs_stat(const char *path, unsigned int *size, int *is_dir)
- {
- struct vfs_mount *m;
- char rel[VFS_PATH_MAX];
- if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
- return VFS_ENOENT;
- return m->ops->stat(m, rel, size, is_dir);
- }
- int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
- {
- struct vfs_mount *m;
- char rel[VFS_PATH_MAX];
- if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
- return VFS_ENOENT;
- return m->ops->list_dir(m, rel, index, out);
- }
- int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
- {
- char buf[VFS_PATH_MAX * 2 + 2];
- unsigned int bi = 0;
- unsigned int len;
- const char *p;
- /* Build the combined (un-normalized) path. */
- if (arg[0] == '/') {
- p = arg;
- } else {
- p = cwd;
- while (*p && bi < sizeof(buf) - 1)
- buf[bi++] = *p++;
- if (bi > 0 && buf[bi - 1] != '/') {
- if (bi < sizeof(buf) - 1)
- buf[bi++] = '/';
- }
- p = arg;
- }
- while (*p && bi < sizeof(buf) - 1)
- buf[bi++] = *p++;
- buf[bi] = '\0';
- if (out_size < 2)
- return -1;
- /* Normalize into out (always begins with "/"). */
- out[0] = '/';
- out[1] = '\0';
- len = 1;
- p = buf;
- while (*p) {
- unsigned int clen = 0;
- const char *comp;
- while (*p == '/')
- p++;
- if (!*p)
- break;
- comp = p;
- while (*p && *p != '/') {
- p++;
- clen++;
- }
- if (clen == 1 && comp[0] == '.')
- continue;
- if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
- /* Pop the previous component (may leave "/"). */
- while (len > 1 && out[len - 1] != '/')
- len--;
- if (len > 1)
- len--;
- out[len] = '\0';
- continue;
- }
- /* Append "/component". */
- if (len + 1 + clen + 1 > out_size)
- return -1;
- if (len > 1)
- out[len++] = '/';
- {
- unsigned int k;
- for (k = 0; k < clen; k++)
- out[len++] = comp[k];
- }
- out[len] = '\0';
- }
- return 0;
- }
- int vfs_device_known(const char *path)
- {
- unsigned int i = 0, j = 0;
- char name[16];
- if (!path || path[0] != '/')
- return 0;
- i = 1;
- while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
- name[j++] = path[i++];
- name[j] = '\0';
- if (j == 0)
- return 0;
- return blk_find(name) != 0;
- }
|