vfs.c 21 KB

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