vfs.c 22 KB

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