vfs.c 19 KB

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