vfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /* Virtual filesystem: mount every device with a valid FAT filesystem under `/`,
  2. * and the command pack (ramfs) at /ramfs. File operations go through the
  3. * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */
  4. #include "vfs.h"
  5. #include "fatdetect.h"
  6. #include "io.h"
  7. #include "kmalloc.h"
  8. #include "process.h"
  9. #include "sched.h"
  10. #include "ramfs.h"
  11. #include "rootfs.h"
  12. #include "serial.h"
  13. #include "share.h"
  14. #include "timer.h"
  15. vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  16. unsigned int vfs_mount_count;
  17. /* Set while vfs_mount_all() rebuilds the mount table. File syscalls wait on
  18. * this (sys_fs.c) so they never resolve a path in a half-rebuilt table or use
  19. * the FAT sector buffer while the scan is reading disks. The owner (a
  20. * `storage` syscall) is exempt. */
  21. volatile int vfs_scan_busy;
  22. volatile unsigned int vfs_scan_owner_pid;
  23. static void put_dec(unsigned int n)
  24. {
  25. char buf[12];
  26. unsigned int i = sizeof(buf) - 1;
  27. buf[i] = '\0';
  28. if (n == 0) {
  29. io_putchar('0');
  30. return;
  31. }
  32. while (n && i > 0) {
  33. buf[--i] = (char)('0' + (n % 10));
  34. n /= 10;
  35. }
  36. io_putstr(buf + i);
  37. }
  38. /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
  39. * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
  40. static void put_size_sectors(unsigned int sectors)
  41. {
  42. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  43. unsigned int gb = sectors >> 21;
  44. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  45. put_dec(gb);
  46. if (tenths) {
  47. io_putchar('.');
  48. put_dec(tenths);
  49. }
  50. io_putstr(" GB");
  51. } else if (sectors >= 2048u) { /* >= 1 MiB */
  52. unsigned int mb = sectors >> 11;
  53. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  54. put_dec(mb);
  55. if (tenths) {
  56. io_putchar('.');
  57. put_dec(tenths);
  58. }
  59. io_putstr(" MB");
  60. } else {
  61. put_dec(sectors / 2u);
  62. io_putstr(" KB");
  63. }
  64. }
  65. static const char *fat_name(fat_type_t t)
  66. {
  67. switch (t) {
  68. case FAT12: return "FAT12";
  69. case FAT16: return "FAT16";
  70. case FAT32: return "FAT32";
  71. default: return "FAT";
  72. }
  73. }
  74. static void mem_copy(void *dst, const void *src, unsigned int n)
  75. {
  76. unsigned char *d = (unsigned char *)dst;
  77. const unsigned char *s = (const unsigned char *)src;
  78. while (n--)
  79. *d++ = *s++;
  80. }
  81. static void copy_bounded(char *dst, const char *src, unsigned int size)
  82. {
  83. unsigned int i = 0;
  84. while (i + 1 < size && src[i]) {
  85. dst[i] = src[i];
  86. i++;
  87. }
  88. dst[i] = '\0';
  89. }
  90. /* ---- FAT implementation ---- */
  91. static void fat_cache_clear(void);
  92. static int fat_op_read_file(struct vfs_mount *m, const char *path,
  93. void *buf, unsigned int max)
  94. {
  95. unsigned int dir;
  96. char name[12];
  97. int r = fat_resolve_path(&m->fs, path, &dir, name);
  98. if (r == -2)
  99. return VFS_EIO;
  100. if (r != 0)
  101. return VFS_ENOENT;
  102. r = fat_read_file_in(&m->fs, dir, name, buf, max);
  103. if (r == -2)
  104. return VFS_EIO;
  105. if (r < 0)
  106. return VFS_ENOENT;
  107. return r;
  108. }
  109. static int fat_op_stat(struct vfs_mount *m, const char *path,
  110. unsigned int *size, int *is_dir)
  111. {
  112. int r = fat_stat(&m->fs, path, size, is_dir);
  113. if (r == -2)
  114. return VFS_EIO;
  115. if (r != 0)
  116. return VFS_ENOENT;
  117. return 0;
  118. }
  119. static int fat_op_load_file(struct vfs_mount *m, const char *path,
  120. struct vfs_file *out)
  121. {
  122. unsigned int size = 0;
  123. int is_dir = 0, r;
  124. void *buf;
  125. r = fat_op_stat(m, path, &size, &is_dir);
  126. if (r != 0)
  127. return r;
  128. if (is_dir || size == 0 || size > VFS_FILE_MAX)
  129. return VFS_EINVAL;
  130. /* One command is loaded at a time; the ELF loader has copied the
  131. * segments out before release_file() runs, so a heap buffer is safe. */
  132. buf = kmalloc(size);
  133. if (!buf)
  134. return VFS_EINVAL;
  135. r = fat_op_read_file(m, path, buf, size);
  136. if (r < 0) {
  137. kfree(buf);
  138. return r;
  139. }
  140. out->data = buf;
  141. out->size = (unsigned int)r;
  142. return 0;
  143. }
  144. static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  145. {
  146. (void)m;
  147. kfree((void *)f->data);
  148. f->data = 0;
  149. f->size = 0;
  150. }
  151. static int fat_op_write_file(struct vfs_mount *m, const char *path,
  152. const void *buf, unsigned int len)
  153. {
  154. fat_cache_clear();
  155. unsigned int dir;
  156. char name[12];
  157. int r = fat_resolve_path(&m->fs, path, &dir, name);
  158. if (r == -2)
  159. return VFS_EIO;
  160. if (r != 0)
  161. return VFS_EINVAL;
  162. r = fat_write_file_in(&m->fs, dir, name, buf, len);
  163. if (r == -2)
  164. return VFS_EIO;
  165. if (r != 0)
  166. return VFS_EINVAL;
  167. return 0;
  168. }
  169. static int fat_op_unlink(struct vfs_mount *m, const char *path)
  170. {
  171. fat_cache_clear();
  172. unsigned int dir;
  173. char name[12];
  174. int r = fat_resolve_path(&m->fs, path, &dir, name);
  175. if (r == -2)
  176. return VFS_EIO;
  177. if (r != 0)
  178. return VFS_ENOENT;
  179. r = fat_delete_file_in(&m->fs, dir, name);
  180. if (r == -2)
  181. return VFS_EIO;
  182. if (r != 0)
  183. return VFS_ENOENT;
  184. return 0;
  185. }
  186. static int fat_op_mkdir(struct vfs_mount *m, const char *path)
  187. {
  188. fat_cache_clear();
  189. unsigned int dir;
  190. char name[12];
  191. int r = fat_resolve_path(&m->fs, path, &dir, name);
  192. if (r == -2)
  193. return VFS_EIO;
  194. if (r != 0)
  195. return VFS_EINVAL;
  196. r = fat_mkdir(&m->fs, dir, name);
  197. if (r == -2)
  198. return VFS_EIO;
  199. if (r != 0)
  200. return VFS_EINVAL; /* the FAT layer printed its own reason */
  201. return 0;
  202. }
  203. static int fat_op_rmdir(struct vfs_mount *m, const char *path)
  204. {
  205. fat_cache_clear();
  206. unsigned int dir;
  207. char name[12];
  208. int r = fat_resolve_path(&m->fs, path, &dir, name);
  209. if (r == -2)
  210. return VFS_EIO;
  211. if (r != 0)
  212. return VFS_EINVAL;
  213. r = fat_rmdir(&m->fs, dir, name);
  214. if (r == -2)
  215. return VFS_EIO;
  216. if (r != 0)
  217. return VFS_EINVAL;
  218. return 0;
  219. }
  220. /* Index-based listing used to re-walk the FAT directory for every index, and
  221. * the WebDAV server asks for 0..N-1 in one PROPFIND. Cache the first 24
  222. * entries of the last directory so a listing is one directory walk; a write
  223. * invalidates it. Larger directories fall back to the old walk past 24. */
  224. #define FAT_LIST_CACHE 24
  225. static struct vfs_dirent fat_cache[FAT_LIST_CACHE];
  226. static unsigned int fat_cache_count;
  227. static int fat_cache_valid;
  228. static int fat_cache_truncated;
  229. static struct vfs_mount *fat_cache_mount;
  230. static char fat_cache_path[VFS_PATH_MAX];
  231. static void fat_cache_clear(void)
  232. {
  233. fat_cache_valid = 0;
  234. }
  235. static int fat_path_eq(const char *a, const char *b)
  236. {
  237. unsigned int i = 0;
  238. while (a[i] && b[i] && a[i] == b[i])
  239. i++;
  240. return a[i] == '\0' && b[i] == '\0';
  241. }
  242. static void fat_cache_cb(const char *name, int is_dir, unsigned int size)
  243. {
  244. struct vfs_dirent *d;
  245. unsigned int i;
  246. if (fat_cache_count >= FAT_LIST_CACHE) {
  247. fat_cache_truncated = 1;
  248. return;
  249. }
  250. d = &fat_cache[fat_cache_count++];
  251. for (i = 0; i < 11; i++)
  252. d->name[i] = name[i] ? name[i] : ' ';
  253. d->name[11] = '\0';
  254. d->size = size;
  255. d->is_dir = (unsigned int)is_dir;
  256. d->raw_83 = 1;
  257. }
  258. /* Old behaviour: one directory walk, counting to `index`. */
  259. static unsigned int fat_list_index;
  260. static struct vfs_dirent *fat_list_out;
  261. static int fat_list_done;
  262. static unsigned int fat_list_seen;
  263. static void fat_list_cb(const char *name, int is_dir, unsigned int size)
  264. {
  265. unsigned int i;
  266. if (fat_list_done || fat_list_seen < fat_list_index) {
  267. fat_list_seen++;
  268. return;
  269. }
  270. for (i = 0; i < 11; i++)
  271. fat_list_out->name[i] = name[i] ? name[i] : ' ';
  272. fat_list_out->name[11] = '\0';
  273. fat_list_out->size = size;
  274. fat_list_out->is_dir = (unsigned int)is_dir;
  275. fat_list_out->raw_83 = 1;
  276. fat_list_done = 1;
  277. fat_list_seen++;
  278. }
  279. static int fat_list_walk(struct vfs_mount *m, const char *path,
  280. unsigned int index, struct vfs_dirent *out)
  281. {
  282. unsigned int dir;
  283. int r = fat_resolve_dir(&m->fs, path, &dir);
  284. if (r == -2)
  285. return VFS_EIO;
  286. if (r != 0)
  287. return VFS_ENOENT;
  288. fat_list_index = index;
  289. fat_list_seen = 0;
  290. fat_list_out = out;
  291. fat_list_done = 0;
  292. r = fat_list_dir(&m->fs, dir, fat_list_cb);
  293. if (r == -2)
  294. return VFS_EIO;
  295. return fat_list_done ? 1 : 0;
  296. }
  297. static int fat_op_list_dir(struct vfs_mount *m, const char *path,
  298. unsigned int index, struct vfs_dirent *out)
  299. {
  300. int same = fat_cache_valid && fat_cache_mount == m &&
  301. fat_path_eq(fat_cache_path, path);
  302. if (same) {
  303. if (index < fat_cache_count) {
  304. *out = fat_cache[index];
  305. return 1;
  306. }
  307. if (!fat_cache_truncated)
  308. return 0;
  309. /* Beyond the cached prefix: use the old walk. */
  310. return fat_list_walk(m, path, index, out);
  311. }
  312. {
  313. unsigned int dir;
  314. int r = fat_resolve_dir(&m->fs, path, &dir);
  315. if (r == -2)
  316. return VFS_EIO;
  317. if (r != 0)
  318. return VFS_ENOENT; /* not found / not a directory */
  319. fat_cache_count = 0;
  320. fat_cache_truncated = 0;
  321. fat_cache_mount = m;
  322. copy_bounded(fat_cache_path, path, sizeof(fat_cache_path));
  323. fat_cache_valid = 0;
  324. r = fat_list_dir(&m->fs, dir, fat_cache_cb);
  325. if (r == -2)
  326. return VFS_EIO;
  327. fat_cache_valid = 1;
  328. }
  329. if (index < fat_cache_count) {
  330. *out = fat_cache[index];
  331. return 1;
  332. }
  333. return 0;
  334. }
  335. static const struct fs_ops fat_ops = {
  336. .raw_83 = 1,
  337. .read_file = fat_op_read_file,
  338. .load_file = fat_op_load_file,
  339. .release_file = fat_op_release_file,
  340. .stat = fat_op_stat,
  341. .write_file = fat_op_write_file,
  342. .unlink = fat_op_unlink,
  343. .mkdir = fat_op_mkdir,
  344. .rmdir = fat_op_rmdir,
  345. .list_dir = fat_op_list_dir,
  346. };
  347. /* 1 for a block-device-backed mount (the FAT driver). A destructive format
  348. * rewrites such a mount; RAM-backed mounts stay usable while it runs. */
  349. int vfs_mount_is_disk(const vfs_mount_t *m)
  350. {
  351. return m && m->ops == &fat_ops;
  352. }
  353. void vfs_scan_poll(unsigned int t0)
  354. {
  355. if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2)
  356. vfs_scan_relax(1);
  357. }
  358. int vfs_scan_relax(unsigned int ms)
  359. {
  360. struct process *p;
  361. if (!vfs_scan_busy || !vfs_scan_owner_pid)
  362. return 0;
  363. p = current_proc();
  364. if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count)
  365. return 0;
  366. {
  367. unsigned int flags;
  368. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  369. if (!(flags & 0x200u))
  370. return 0; /* interrupts off: cannot sleep */
  371. }
  372. process_sleep_ms(ms ? ms : 1);
  373. return 1;
  374. }
  375. /* ---- ramfs implementation ---- */
  376. static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
  377. void *buf, unsigned int max)
  378. {
  379. const void *data;
  380. unsigned int size = 0, n;
  381. (void)m;
  382. data = ramfs_lookup(path, &size);
  383. if (!data)
  384. return VFS_ENOENT;
  385. n = size > max ? max : size;
  386. mem_copy(buf, data, n);
  387. return (int)n;
  388. }
  389. static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
  390. struct vfs_file *out)
  391. {
  392. const void *data;
  393. unsigned int size = 0;
  394. (void)m;
  395. data = ramfs_lookup(path, &size);
  396. if (!data)
  397. return VFS_ENOENT;
  398. out->data = data;
  399. out->size = size;
  400. return 0;
  401. }
  402. static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  403. {
  404. (void)m;
  405. (void)f;
  406. }
  407. static int ramfs_op_stat(struct vfs_mount *m, const char *path,
  408. unsigned int *size, int *is_dir)
  409. {
  410. const void *data;
  411. unsigned int sz = 0;
  412. struct ramfs_dirent e;
  413. (void)m;
  414. if (path[0] == '\0') {
  415. if (size)
  416. *size = 0;
  417. if (is_dir)
  418. *is_dir = 1;
  419. return 0;
  420. }
  421. data = ramfs_lookup(path, &sz);
  422. if (data) {
  423. if (size)
  424. *size = sz;
  425. if (is_dir)
  426. *is_dir = 0;
  427. return 0;
  428. }
  429. if (ramfs_list(path, 0, &e) == 1) {
  430. if (size)
  431. *size = 0;
  432. if (is_dir)
  433. *is_dir = 1;
  434. return 0;
  435. }
  436. return VFS_ENOENT;
  437. }
  438. static int ramfs_op_ro(struct vfs_mount *m, const char *path)
  439. {
  440. (void)m;
  441. (void)path;
  442. return VFS_EROFS;
  443. }
  444. static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
  445. const void *buf, unsigned int len)
  446. {
  447. (void)buf;
  448. (void)len;
  449. return ramfs_op_ro(m, path);
  450. }
  451. static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
  452. {
  453. return ramfs_op_ro(m, path);
  454. }
  455. static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
  456. {
  457. return ramfs_op_ro(m, path);
  458. }
  459. static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
  460. {
  461. return ramfs_op_ro(m, path);
  462. }
  463. static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
  464. unsigned int index, struct vfs_dirent *out)
  465. {
  466. struct ramfs_dirent e;
  467. (void)m;
  468. if (ramfs_list(path, index, &e) != 1) {
  469. if (index == 0 && path[0] != '\0') {
  470. unsigned int size = 0;
  471. /* Nothing matched: report a missing path instead of an
  472. * empty directory (the pack has no empty directories --
  473. * one exists exactly as a prefix of file paths), and a
  474. * file as "not a directory". */
  475. if (ramfs_lookup(path, &size))
  476. return VFS_EINVAL;
  477. return VFS_ENOENT;
  478. }
  479. return 0; /* end of directory */
  480. }
  481. copy_bounded(out->name, e.name, sizeof(out->name));
  482. out->size = e.size;
  483. out->is_dir = e.is_dir;
  484. out->raw_83 = 0;
  485. return 1;
  486. }
  487. static const struct fs_ops ramfs_ops = {
  488. .raw_83 = 0,
  489. .read_file = ramfs_op_read_file,
  490. .load_file = ramfs_op_load_file,
  491. .release_file = ramfs_op_release_file,
  492. .stat = ramfs_op_stat,
  493. .write_file = ramfs_op_write_file,
  494. .unlink = ramfs_op_unlink,
  495. .mkdir = ramfs_op_mkdir,
  496. .rmdir = ramfs_op_rmdir,
  497. .list_dir = ramfs_op_list_dir,
  498. };
  499. int vfs_mount_is_ram(const vfs_mount_t *m)
  500. {
  501. return m && (m->ops == &ramfs_ops || m->ops == &share_ops);
  502. }
  503. /* ---- mounts ---- */
  504. static vfs_mount_t *mount_slot(const char *name, const char *dev)
  505. {
  506. unsigned int i;
  507. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  508. return 0;
  509. {
  510. vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
  511. for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
  512. m->name[i] = name[i];
  513. m->name[i] = '\0';
  514. m->dev[0] = '\0';
  515. if (dev) {
  516. for (i = 0; dev[i] && i < sizeof(m->dev) - 1; i++)
  517. m->dev[i] = dev[i];
  518. m->dev[i] = '\0';
  519. }
  520. m->mounted = 1;
  521. return m;
  522. }
  523. }
  524. static int name_eq(const char *a, const char *b)
  525. {
  526. unsigned int i = 0;
  527. while (a[i] && b[i] && a[i] == b[i])
  528. i++;
  529. return a[i] == '\0' && b[i] == '\0';
  530. }
  531. /* The P1 boot sector keeps only the NebOS header fields; a real FAT volume
  532. * never carries these values. Older installed media still has the original
  533. * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead
  534. * of mounting the raw kernel partition as a fake filesystem. */
  535. static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
  536. {
  537. unsigned char s[512];
  538. unsigned int i;
  539. if (blk_read(d, start_lba, s, 1) != 0)
  540. return 0;
  541. if (s[510] != 0x55 || s[511] != 0xAA)
  542. return 0;
  543. if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' ||
  544. s[7] != 'S')
  545. return 0;
  546. if ((s[0x40] | (s[0x41] << 8)) != NEBOS_KERNEL_LBA)
  547. return 0;
  548. i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) |
  549. ((unsigned int)s[0x45] << 24);
  550. return i == ROOTFS_LBA;
  551. }
  552. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  553. {
  554. fat_fs_t fs;
  555. vfs_mount_t *m;
  556. const char *use_name = name;
  557. int r;
  558. fat_cache_clear();
  559. r = fat_mount(dev, part_lba, &fs);
  560. if (r != 0) {
  561. serial_puts(SERIAL_COM1, "vfs: ");
  562. serial_puts(SERIAL_COM1, name);
  563. serial_puts(SERIAL_COM1, " no FAT\n");
  564. return;
  565. }
  566. /* The labelled data partition has a stable path independent of the
  567. * device name. Only the first NEBOSDATA volume becomes /data. */
  568. if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
  569. use_name = "data";
  570. m = mount_slot(use_name, dev->name);
  571. if (!m)
  572. return;
  573. m->ops = &fat_ops;
  574. m->fs = fs;
  575. m->ro = 0;
  576. m->total_sectors = fs.total_sectors;
  577. copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
  578. copy_bounded(m->label, fs.label, sizeof(m->label));
  579. vfs_mount_count++;
  580. serial_puts(SERIAL_COM1, "vfs: ");
  581. serial_puts(SERIAL_COM1, use_name);
  582. serial_puts(SERIAL_COM1, " ");
  583. serial_puts(SERIAL_COM1, fat_name(fs.type));
  584. serial_puts(SERIAL_COM1, "\n");
  585. }
  586. void vfs_mount_ramfs(void)
  587. {
  588. vfs_mount_t *m;
  589. /* Mount even when the pack is empty: the runtime `cmds-dev` path can
  590. * install a pack later and vfs_refresh_ramfs() then updates the mount. */
  591. m = mount_slot("ramfs", 0);
  592. if (!m)
  593. return;
  594. m->ops = &ramfs_ops;
  595. m->ro = 1;
  596. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  597. copy_bounded(m->type, "ramfs", sizeof(m->type));
  598. m->label[0] = '\0';
  599. vfs_mount_count++;
  600. }
  601. void vfs_refresh_ramfs(void)
  602. {
  603. vfs_mount_t *m = vfs_find("ramfs");
  604. if (m)
  605. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  606. }
  607. /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a
  608. * block device and its contents survive vfs_mount_all() refreshes. */
  609. void vfs_mount_share(void)
  610. {
  611. vfs_mount_t *m;
  612. if (share_init() != 0)
  613. return;
  614. m = mount_slot("share", 0);
  615. if (!m)
  616. return;
  617. m->ops = &share_ops;
  618. m->ro = 0;
  619. m->total_sectors = SHARE_MAX_BYTES / 512u;
  620. copy_bounded(m->type, "share", sizeof(m->type));
  621. m->label[0] = '\0';
  622. vfs_mount_count++;
  623. }
  624. static void part_name(char *out, const char *base, unsigned int idx)
  625. {
  626. unsigned int i = 0;
  627. while (base[i] && i < 13) {
  628. out[i] = base[i];
  629. i++;
  630. }
  631. out[i++] = 'p';
  632. out[i++] = (char)('0' + idx + 1);
  633. out[i] = '\0';
  634. }
  635. static void mount_device(blk_dev_t *d, unsigned char *buf)
  636. {
  637. mbr_part_t parts[4];
  638. int n, j;
  639. int any_fat = 0;
  640. if (!d->present)
  641. return;
  642. if (d->kind == BLK_FLOPPY) {
  643. mount_one(d, 0, d->name);
  644. return;
  645. }
  646. /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
  647. if (blk_read(d, 0, buf, 1) != 0)
  648. return;
  649. n = mbr_parse(buf, parts);
  650. for (j = 0; j < n; j++) {
  651. char pname[16];
  652. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  653. parts[j].sectors == 0)
  654. continue;
  655. /* The active NebOS partition holds the raw boot sector and
  656. * kernel, not a filesystem (old images still look like FAT). */
  657. if (is_nebos_boot_partition(d, parts[j].start_lba))
  658. continue;
  659. any_fat = 1;
  660. part_name(pname, d->name, parts[j].index);
  661. mount_one(d, parts[j].start_lba, pname);
  662. }
  663. if (!any_fat)
  664. mount_one(d, 0, d->name);
  665. }
  666. /* Mounts remember their source device, so /data can be named independently
  667. * of <device>pN and still be found by a remount. */
  668. static int mount_belongs_to(const vfs_mount_t *m, const char *dev)
  669. {
  670. return m->dev[0] != '\0' && name_eq(m->dev, dev);
  671. }
  672. static void unmount_device(const char *dev)
  673. {
  674. unsigned int i, j;
  675. for (i = 0; i < vfs_mount_count; ) {
  676. if (mount_belongs_to(&vfs_mounts[i], dev)) {
  677. for (j = i + 1; j < vfs_mount_count; j++)
  678. vfs_mounts[j - 1] = vfs_mounts[j];
  679. vfs_mount_count--;
  680. } else {
  681. i++;
  682. }
  683. }
  684. }
  685. void vfs_mount_all(void)
  686. {
  687. static unsigned char buf[512];
  688. unsigned int i;
  689. /* Publish the scan before touching the table: a ring-3 file syscall
  690. * on another CPU waits for it instead of resolving a path halfway
  691. * through the rebuild. At boot current_proc() is idle (pid 0). */
  692. vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
  693. vfs_scan_busy = 1;
  694. vfs_mount_count = 0;
  695. /* Non-block filesystems first. The WM and terminal live on /ramfs,
  696. * so they stay usable while the (possibly slow) disk scan runs. */
  697. vfs_mount_ramfs();
  698. vfs_mount_share();
  699. blk_scan();
  700. for (i = 0; i < blk_dev_count; i++) {
  701. unsigned int t0 = get_ticks_ms();
  702. mount_device(&blk_devs[i], buf);
  703. serial_puts(SERIAL_COM1, "vfs: ");
  704. serial_puts(SERIAL_COM1, blk_devs[i].name);
  705. serial_puts(SERIAL_COM1, " mount ");
  706. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  707. serial_puts(SERIAL_COM1, " ms\n");
  708. }
  709. serial_puts(SERIAL_COM1, "vfs: ");
  710. serial_putdec(SERIAL_COM1, vfs_mount_count);
  711. serial_puts(SERIAL_COM1, " mount(s)\n");
  712. vfs_scan_busy = 0;
  713. }
  714. static int device_is_mounted(const char *dev)
  715. {
  716. unsigned int i;
  717. for (i = 0; i < vfs_mount_count; i++)
  718. if (mount_belongs_to(&vfs_mounts[i], dev))
  719. return 1;
  720. return 0;
  721. }
  722. void vfs_remount_device(blk_dev_t *d)
  723. {
  724. unsigned char buf[512];
  725. if (!d)
  726. return;
  727. fat_cache_clear();
  728. unmount_device(d->name);
  729. mount_device(d, buf);
  730. if (!device_is_mounted(d->name)) {
  731. io_putstr("vfs: remount of ");
  732. io_putstr(d->name);
  733. io_putstr(" found no filesystem\n");
  734. }
  735. }
  736. vfs_mount_t *vfs_find(const char *name)
  737. {
  738. unsigned int i;
  739. for (i = 0; i < vfs_mount_count; i++) {
  740. unsigned int j = 0;
  741. while (vfs_mounts[i].name[j] && name[j] &&
  742. vfs_mounts[i].name[j] == name[j])
  743. j++;
  744. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  745. return &vfs_mounts[i];
  746. }
  747. return 0;
  748. }
  749. void vfs_list_root(void)
  750. {
  751. unsigned int i;
  752. for (i = 0; i < vfs_mount_count; i++) {
  753. vfs_mount_t *m = &vfs_mounts[i];
  754. io_putstr(m->name);
  755. io_putstr(" ");
  756. io_putstr(m->type);
  757. io_putchar(' ');
  758. put_size_sectors(m->total_sectors);
  759. if (m->label[0]) {
  760. io_putstr(" \"");
  761. io_putstr(m->label);
  762. io_putchar('"');
  763. }
  764. if (m->ro)
  765. io_putstr(" (ro)");
  766. io_putchar('\n');
  767. }
  768. }
  769. int vfs_resolve(const char *path, struct vfs_mount **m_out,
  770. char *rel_path, unsigned int rel_size)
  771. {
  772. unsigned int i = 0, j = 0, o = 0;
  773. char name[16];
  774. vfs_mount_t *m;
  775. if (!path || path[0] != '/' || rel_size == 0)
  776. return -1;
  777. i = 1;
  778. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  779. name[j++] = path[i++];
  780. name[j] = '\0';
  781. if (j == 0)
  782. return -1; /* path is just "/" */
  783. m = vfs_find(name);
  784. if (!m)
  785. return -1;
  786. /* Copy the remainder as a normalized relative path: skip slashes, drop
  787. * "." components and never leave a trailing one. */
  788. while (path[i]) {
  789. unsigned int clen = 0;
  790. const char *comp;
  791. while (path[i] == '/')
  792. i++;
  793. if (!path[i])
  794. break;
  795. comp = path + i;
  796. while (path[i] && path[i] != '/') {
  797. i++;
  798. clen++;
  799. }
  800. if (clen == 1 && comp[0] == '.')
  801. continue;
  802. if (o + clen + 2 > rel_size)
  803. return -1;
  804. if (o > 0)
  805. rel_path[o++] = '/';
  806. {
  807. unsigned int k;
  808. for (k = 0; k < clen; k++)
  809. rel_path[o++] = comp[k];
  810. }
  811. }
  812. rel_path[o] = '\0';
  813. *m_out = m;
  814. return 0;
  815. }
  816. int vfs_load_file(const char *path, struct vfs_file *out,
  817. struct vfs_mount **m_out)
  818. {
  819. struct vfs_mount *m;
  820. char rel[VFS_PATH_MAX];
  821. int r;
  822. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  823. return VFS_ENOENT;
  824. r = m->ops->load_file(m, rel, out);
  825. if (r == 0 && m_out)
  826. *m_out = m;
  827. return r;
  828. }
  829. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
  830. {
  831. if (m && m->ops->release_file)
  832. m->ops->release_file(m, f);
  833. }
  834. int vfs_stat(const char *path, unsigned int *size, int *is_dir)
  835. {
  836. struct vfs_mount *m;
  837. char rel[VFS_PATH_MAX];
  838. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  839. return VFS_ENOENT;
  840. return m->ops->stat(m, rel, size, is_dir);
  841. }
  842. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
  843. {
  844. struct vfs_mount *m;
  845. char rel[VFS_PATH_MAX];
  846. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  847. return VFS_ENOENT;
  848. return m->ops->list_dir(m, rel, index, out);
  849. }
  850. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  851. {
  852. char buf[VFS_PATH_MAX * 2 + 2];
  853. unsigned int bi = 0;
  854. unsigned int len;
  855. const char *p;
  856. /* Build the combined (un-normalized) path. */
  857. if (arg[0] == '/') {
  858. p = arg;
  859. } else {
  860. p = cwd;
  861. while (*p && bi < sizeof(buf) - 1)
  862. buf[bi++] = *p++;
  863. if (bi > 0 && buf[bi - 1] != '/') {
  864. if (bi < sizeof(buf) - 1)
  865. buf[bi++] = '/';
  866. }
  867. p = arg;
  868. }
  869. while (*p && bi < sizeof(buf) - 1)
  870. buf[bi++] = *p++;
  871. buf[bi] = '\0';
  872. if (out_size < 2)
  873. return -1;
  874. /* Normalize into out (always begins with "/"). */
  875. out[0] = '/';
  876. out[1] = '\0';
  877. len = 1;
  878. p = buf;
  879. while (*p) {
  880. unsigned int clen = 0;
  881. const char *comp;
  882. while (*p == '/')
  883. p++;
  884. if (!*p)
  885. break;
  886. comp = p;
  887. while (*p && *p != '/') {
  888. p++;
  889. clen++;
  890. }
  891. if (clen == 1 && comp[0] == '.')
  892. continue;
  893. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  894. /* Pop the previous component (may leave "/"). */
  895. while (len > 1 && out[len - 1] != '/')
  896. len--;
  897. if (len > 1)
  898. len--;
  899. out[len] = '\0';
  900. continue;
  901. }
  902. /* Append "/component". */
  903. if (len + 1 + clen + 1 > out_size)
  904. return -1;
  905. if (len > 1)
  906. out[len++] = '/';
  907. {
  908. unsigned int k;
  909. for (k = 0; k < clen; k++)
  910. out[len++] = comp[k];
  911. }
  912. out[len] = '\0';
  913. }
  914. return 0;
  915. }
  916. int vfs_device_known(const char *path)
  917. {
  918. unsigned int i = 0, j = 0;
  919. char name[16];
  920. if (!path || path[0] != '/')
  921. return 0;
  922. i = 1;
  923. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  924. name[j++] = path[i++];
  925. name[j] = '\0';
  926. if (j == 0)
  927. return 0;
  928. return blk_find(name) != 0;
  929. }