vfs.c 25 KB

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