vfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. /* 1 for a block-device-backed mount (the FAT driver). A destructive format
  346. * rewrites such a mount; RAM-backed mounts stay usable while it runs. */
  347. int vfs_mount_is_disk(const vfs_mount_t *m)
  348. {
  349. return m && m->ops == &fat_ops;
  350. }
  351. /* ---- ramfs implementation ---- */
  352. static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
  353. void *buf, unsigned int max)
  354. {
  355. const void *data;
  356. unsigned int size = 0, n;
  357. (void)m;
  358. data = ramfs_lookup(path, &size);
  359. if (!data)
  360. return VFS_ENOENT;
  361. n = size > max ? max : size;
  362. mem_copy(buf, data, n);
  363. return (int)n;
  364. }
  365. static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
  366. struct vfs_file *out)
  367. {
  368. const void *data;
  369. unsigned int size = 0;
  370. (void)m;
  371. data = ramfs_lookup(path, &size);
  372. if (!data)
  373. return VFS_ENOENT;
  374. out->data = data;
  375. out->size = size;
  376. return 0;
  377. }
  378. static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  379. {
  380. (void)m;
  381. (void)f;
  382. }
  383. static int ramfs_op_stat(struct vfs_mount *m, const char *path,
  384. unsigned int *size, int *is_dir)
  385. {
  386. const void *data;
  387. unsigned int sz = 0;
  388. struct ramfs_dirent e;
  389. (void)m;
  390. if (path[0] == '\0') {
  391. if (size)
  392. *size = 0;
  393. if (is_dir)
  394. *is_dir = 1;
  395. return 0;
  396. }
  397. data = ramfs_lookup(path, &sz);
  398. if (data) {
  399. if (size)
  400. *size = sz;
  401. if (is_dir)
  402. *is_dir = 0;
  403. return 0;
  404. }
  405. if (ramfs_list(path, 0, &e) == 1) {
  406. if (size)
  407. *size = 0;
  408. if (is_dir)
  409. *is_dir = 1;
  410. return 0;
  411. }
  412. return VFS_ENOENT;
  413. }
  414. static int ramfs_op_ro(struct vfs_mount *m, const char *path)
  415. {
  416. (void)m;
  417. (void)path;
  418. return VFS_EROFS;
  419. }
  420. static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
  421. const void *buf, unsigned int len)
  422. {
  423. (void)buf;
  424. (void)len;
  425. return ramfs_op_ro(m, path);
  426. }
  427. static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
  428. {
  429. return ramfs_op_ro(m, path);
  430. }
  431. static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
  432. {
  433. return ramfs_op_ro(m, path);
  434. }
  435. static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
  436. {
  437. return ramfs_op_ro(m, path);
  438. }
  439. static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
  440. unsigned int index, struct vfs_dirent *out)
  441. {
  442. struct ramfs_dirent e;
  443. (void)m;
  444. if (ramfs_list(path, index, &e) != 1) {
  445. if (index == 0 && path[0] != '\0') {
  446. unsigned int size = 0;
  447. /* Nothing matched: report a missing path instead of an
  448. * empty directory (the pack has no empty directories --
  449. * one exists exactly as a prefix of file paths), and a
  450. * file as "not a directory". */
  451. if (ramfs_lookup(path, &size))
  452. return VFS_EINVAL;
  453. return VFS_ENOENT;
  454. }
  455. return 0; /* end of directory */
  456. }
  457. copy_bounded(out->name, e.name, sizeof(out->name));
  458. out->size = e.size;
  459. out->is_dir = e.is_dir;
  460. out->raw_83 = 0;
  461. return 1;
  462. }
  463. static const struct fs_ops ramfs_ops = {
  464. .raw_83 = 0,
  465. .read_file = ramfs_op_read_file,
  466. .load_file = ramfs_op_load_file,
  467. .release_file = ramfs_op_release_file,
  468. .stat = ramfs_op_stat,
  469. .write_file = ramfs_op_write_file,
  470. .unlink = ramfs_op_unlink,
  471. .mkdir = ramfs_op_mkdir,
  472. .rmdir = ramfs_op_rmdir,
  473. .list_dir = ramfs_op_list_dir,
  474. };
  475. /* ---- mounts ---- */
  476. static vfs_mount_t *mount_slot(const char *name)
  477. {
  478. unsigned int i;
  479. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  480. return 0;
  481. {
  482. vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
  483. for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
  484. m->name[i] = name[i];
  485. m->name[i] = '\0';
  486. m->mounted = 1;
  487. return m;
  488. }
  489. }
  490. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  491. {
  492. fat_fs_t fs;
  493. vfs_mount_t *m;
  494. int r;
  495. fat_cache_clear();
  496. r = fat_mount(dev, part_lba, &fs);
  497. if (r != 0) {
  498. serial_puts(SERIAL_COM1, "vfs: ");
  499. serial_puts(SERIAL_COM1, name);
  500. serial_puts(SERIAL_COM1, " no FAT\n");
  501. return;
  502. }
  503. m = mount_slot(name);
  504. if (!m)
  505. return;
  506. m->ops = &fat_ops;
  507. m->fs = fs;
  508. m->ro = 0;
  509. m->total_sectors = fs.total_sectors;
  510. copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
  511. copy_bounded(m->label, fs.label, sizeof(m->label));
  512. vfs_mount_count++;
  513. serial_puts(SERIAL_COM1, "vfs: ");
  514. serial_puts(SERIAL_COM1, name);
  515. serial_puts(SERIAL_COM1, " ");
  516. serial_puts(SERIAL_COM1, fat_name(fs.type));
  517. serial_puts(SERIAL_COM1, "\n");
  518. }
  519. void vfs_mount_ramfs(void)
  520. {
  521. vfs_mount_t *m;
  522. /* Mount even when the pack is empty: the runtime `cmds-dev` path can
  523. * install a pack later and vfs_refresh_ramfs() then updates the mount. */
  524. m = mount_slot("ramfs");
  525. if (!m)
  526. return;
  527. m->ops = &ramfs_ops;
  528. m->ro = 1;
  529. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  530. copy_bounded(m->type, "ramfs", sizeof(m->type));
  531. m->label[0] = '\0';
  532. vfs_mount_count++;
  533. }
  534. void vfs_refresh_ramfs(void)
  535. {
  536. vfs_mount_t *m = vfs_find("ramfs");
  537. if (m)
  538. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  539. }
  540. /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a
  541. * block device and its contents survive vfs_mount_all() refreshes. */
  542. void vfs_mount_share(void)
  543. {
  544. vfs_mount_t *m;
  545. if (share_init() != 0)
  546. return;
  547. m = mount_slot("share");
  548. if (!m)
  549. return;
  550. m->ops = &share_ops;
  551. m->ro = 0;
  552. m->total_sectors = SHARE_MAX_BYTES / 512u;
  553. copy_bounded(m->type, "share", sizeof(m->type));
  554. m->label[0] = '\0';
  555. vfs_mount_count++;
  556. }
  557. static void part_name(char *out, const char *base, unsigned int idx)
  558. {
  559. unsigned int i = 0;
  560. while (base[i] && i < 13) {
  561. out[i] = base[i];
  562. i++;
  563. }
  564. out[i++] = 'p';
  565. out[i++] = (char)('0' + idx + 1);
  566. out[i] = '\0';
  567. }
  568. static void mount_device(blk_dev_t *d, unsigned char *buf)
  569. {
  570. mbr_part_t parts[4];
  571. int n, j;
  572. int any_fat = 0;
  573. if (!d->present)
  574. return;
  575. if (d->kind == BLK_FLOPPY) {
  576. mount_one(d, 0, d->name);
  577. return;
  578. }
  579. /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
  580. if (blk_read(d, 0, buf, 1) != 0)
  581. return;
  582. n = mbr_parse(buf, parts);
  583. for (j = 0; j < n; j++) {
  584. char pname[16];
  585. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  586. parts[j].sectors == 0)
  587. continue;
  588. any_fat = 1;
  589. part_name(pname, d->name, parts[j].index);
  590. mount_one(d, parts[j].start_lba, pname);
  591. }
  592. if (!any_fat)
  593. mount_one(d, 0, d->name);
  594. }
  595. /* Mount names for a device are the device itself or "<device>pN". */
  596. static int mount_belongs_to(const char *mount, const char *dev)
  597. {
  598. unsigned int i = 0;
  599. while (dev[i] && mount[i] == dev[i])
  600. i++;
  601. if (dev[i] != '\0')
  602. return 0;
  603. return mount[i] == '\0' || mount[i] == 'p';
  604. }
  605. static void unmount_device(const char *dev)
  606. {
  607. unsigned int i, j;
  608. for (i = 0; i < vfs_mount_count; ) {
  609. if (mount_belongs_to(vfs_mounts[i].name, dev)) {
  610. for (j = i + 1; j < vfs_mount_count; j++)
  611. vfs_mounts[j - 1] = vfs_mounts[j];
  612. vfs_mount_count--;
  613. } else {
  614. i++;
  615. }
  616. }
  617. }
  618. void vfs_mount_all(void)
  619. {
  620. static unsigned char buf[512];
  621. unsigned int i;
  622. /* Publish the scan before touching the table: a ring-3 file syscall
  623. * on another CPU waits for it instead of resolving a path halfway
  624. * through the rebuild. At boot current_proc() is idle (pid 0). */
  625. vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
  626. vfs_scan_busy = 1;
  627. vfs_mount_count = 0;
  628. /* Non-block filesystems first. The WM and terminal live on /ramfs,
  629. * so they stay usable while the (possibly slow) disk scan runs. */
  630. vfs_mount_ramfs();
  631. vfs_mount_share();
  632. blk_scan();
  633. for (i = 0; i < blk_dev_count; i++) {
  634. unsigned int t0 = get_ticks_ms();
  635. mount_device(&blk_devs[i], buf);
  636. serial_puts(SERIAL_COM1, "vfs: ");
  637. serial_puts(SERIAL_COM1, blk_devs[i].name);
  638. serial_puts(SERIAL_COM1, " mount ");
  639. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  640. serial_puts(SERIAL_COM1, " ms\n");
  641. }
  642. serial_puts(SERIAL_COM1, "vfs: ");
  643. serial_putdec(SERIAL_COM1, vfs_mount_count);
  644. serial_puts(SERIAL_COM1, " mount(s)\n");
  645. vfs_scan_busy = 0;
  646. }
  647. static int device_is_mounted(const char *dev)
  648. {
  649. unsigned int i;
  650. for (i = 0; i < vfs_mount_count; i++)
  651. if (mount_belongs_to(vfs_mounts[i].name, dev))
  652. return 1;
  653. return 0;
  654. }
  655. void vfs_remount_device(blk_dev_t *d)
  656. {
  657. unsigned char buf[512];
  658. if (!d)
  659. return;
  660. fat_cache_clear();
  661. unmount_device(d->name);
  662. mount_device(d, buf);
  663. if (!device_is_mounted(d->name)) {
  664. io_putstr("vfs: remount of ");
  665. io_putstr(d->name);
  666. io_putstr(" found no filesystem\n");
  667. }
  668. }
  669. vfs_mount_t *vfs_find(const char *name)
  670. {
  671. unsigned int i;
  672. for (i = 0; i < vfs_mount_count; i++) {
  673. unsigned int j = 0;
  674. while (vfs_mounts[i].name[j] && name[j] &&
  675. vfs_mounts[i].name[j] == name[j])
  676. j++;
  677. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  678. return &vfs_mounts[i];
  679. }
  680. return 0;
  681. }
  682. void vfs_list_root(void)
  683. {
  684. unsigned int i;
  685. for (i = 0; i < vfs_mount_count; i++) {
  686. vfs_mount_t *m = &vfs_mounts[i];
  687. io_putstr(m->name);
  688. io_putstr(" ");
  689. io_putstr(m->type);
  690. io_putchar(' ');
  691. put_size_sectors(m->total_sectors);
  692. if (m->label[0]) {
  693. io_putstr(" \"");
  694. io_putstr(m->label);
  695. io_putchar('"');
  696. }
  697. if (m->ro)
  698. io_putstr(" (ro)");
  699. io_putchar('\n');
  700. }
  701. }
  702. int vfs_resolve(const char *path, struct vfs_mount **m_out,
  703. char *rel_path, unsigned int rel_size)
  704. {
  705. unsigned int i = 0, j = 0, o = 0;
  706. char name[16];
  707. vfs_mount_t *m;
  708. if (!path || path[0] != '/' || rel_size == 0)
  709. return -1;
  710. i = 1;
  711. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  712. name[j++] = path[i++];
  713. name[j] = '\0';
  714. if (j == 0)
  715. return -1; /* path is just "/" */
  716. m = vfs_find(name);
  717. if (!m)
  718. return -1;
  719. /* Copy the remainder as a normalized relative path: skip slashes, drop
  720. * "." components and never leave a trailing one. */
  721. while (path[i]) {
  722. unsigned int clen = 0;
  723. const char *comp;
  724. while (path[i] == '/')
  725. i++;
  726. if (!path[i])
  727. break;
  728. comp = path + i;
  729. while (path[i] && path[i] != '/') {
  730. i++;
  731. clen++;
  732. }
  733. if (clen == 1 && comp[0] == '.')
  734. continue;
  735. if (o + clen + 2 > rel_size)
  736. return -1;
  737. if (o > 0)
  738. rel_path[o++] = '/';
  739. {
  740. unsigned int k;
  741. for (k = 0; k < clen; k++)
  742. rel_path[o++] = comp[k];
  743. }
  744. }
  745. rel_path[o] = '\0';
  746. *m_out = m;
  747. return 0;
  748. }
  749. int vfs_load_file(const char *path, struct vfs_file *out,
  750. struct vfs_mount **m_out)
  751. {
  752. struct vfs_mount *m;
  753. char rel[VFS_PATH_MAX];
  754. int r;
  755. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  756. return VFS_ENOENT;
  757. r = m->ops->load_file(m, rel, out);
  758. if (r == 0 && m_out)
  759. *m_out = m;
  760. return r;
  761. }
  762. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
  763. {
  764. if (m && m->ops->release_file)
  765. m->ops->release_file(m, f);
  766. }
  767. int vfs_stat(const char *path, unsigned int *size, int *is_dir)
  768. {
  769. struct vfs_mount *m;
  770. char rel[VFS_PATH_MAX];
  771. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  772. return VFS_ENOENT;
  773. return m->ops->stat(m, rel, size, is_dir);
  774. }
  775. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
  776. {
  777. struct vfs_mount *m;
  778. char rel[VFS_PATH_MAX];
  779. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  780. return VFS_ENOENT;
  781. return m->ops->list_dir(m, rel, index, out);
  782. }
  783. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  784. {
  785. char buf[VFS_PATH_MAX * 2 + 2];
  786. unsigned int bi = 0;
  787. unsigned int len;
  788. const char *p;
  789. /* Build the combined (un-normalized) path. */
  790. if (arg[0] == '/') {
  791. p = arg;
  792. } else {
  793. p = cwd;
  794. while (*p && bi < sizeof(buf) - 1)
  795. buf[bi++] = *p++;
  796. if (bi > 0 && buf[bi - 1] != '/') {
  797. if (bi < sizeof(buf) - 1)
  798. buf[bi++] = '/';
  799. }
  800. p = arg;
  801. }
  802. while (*p && bi < sizeof(buf) - 1)
  803. buf[bi++] = *p++;
  804. buf[bi] = '\0';
  805. if (out_size < 2)
  806. return -1;
  807. /* Normalize into out (always begins with "/"). */
  808. out[0] = '/';
  809. out[1] = '\0';
  810. len = 1;
  811. p = buf;
  812. while (*p) {
  813. unsigned int clen = 0;
  814. const char *comp;
  815. while (*p == '/')
  816. p++;
  817. if (!*p)
  818. break;
  819. comp = p;
  820. while (*p && *p != '/') {
  821. p++;
  822. clen++;
  823. }
  824. if (clen == 1 && comp[0] == '.')
  825. continue;
  826. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  827. /* Pop the previous component (may leave "/"). */
  828. while (len > 1 && out[len - 1] != '/')
  829. len--;
  830. if (len > 1)
  831. len--;
  832. out[len] = '\0';
  833. continue;
  834. }
  835. /* Append "/component". */
  836. if (len + 1 + clen + 1 > out_size)
  837. return -1;
  838. if (len > 1)
  839. out[len++] = '/';
  840. {
  841. unsigned int k;
  842. for (k = 0; k < clen; k++)
  843. out[len++] = comp[k];
  844. }
  845. out[len] = '\0';
  846. }
  847. return 0;
  848. }
  849. int vfs_device_known(const char *path)
  850. {
  851. unsigned int i = 0, j = 0;
  852. char name[16];
  853. if (!path || path[0] != '/')
  854. return 0;
  855. i = 1;
  856. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  857. name[j++] = path[i++];
  858. name[j] = '\0';
  859. if (j == 0)
  860. return 0;
  861. return blk_find(name) != 0;
  862. }