vfs.c 19 KB

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