share.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* /share: a small writable in-memory filesystem.
  2. *
  3. * Files live in whole pages from the frame allocator (identity mapped, so a
  4. * borrowed pointer works), and the directory namespace is the full relative
  5. * path of every entry, like the pack ramfs. That keeps mkdir/list/rmdir
  6. * simple without an inode tree. A directory exists as an entry of its own,
  7. * so empty directories survive. Everything is volatile.
  8. */
  9. #include "vfs.h"
  10. #include "share.h"
  11. #include "kmalloc.h"
  12. #include "mm.h"
  13. struct share_ent {
  14. char path[SHARE_PATH_MAX];
  15. unsigned int base; /* first page, 0 when the file is empty */
  16. unsigned int size;
  17. unsigned int is_dir;
  18. };
  19. static struct share_ent *ents;
  20. static unsigned int ent_count;
  21. static unsigned int used_bytes;
  22. static int s_eq(const char *a, const char *b)
  23. {
  24. unsigned int i = 0;
  25. while (a[i] && b[i] && a[i] == b[i])
  26. i++;
  27. return a[i] == '\0' && b[i] == '\0';
  28. }
  29. static unsigned int s_len(const char *s)
  30. {
  31. unsigned int n = 0;
  32. while (s[n])
  33. n++;
  34. return n;
  35. }
  36. static void s_copy(char *d, const char *s, unsigned int cap)
  37. {
  38. unsigned int i = 0;
  39. while (i + 1 < cap && s[i]) {
  40. d[i] = s[i];
  41. i++;
  42. }
  43. d[i] = '\0';
  44. }
  45. static void mem_copy(void *d, const void *s, unsigned int n)
  46. {
  47. unsigned char *dd = d;
  48. const unsigned char *ss = s;
  49. while (n--)
  50. *dd++ = *ss++;
  51. }
  52. /* A relative path: components separated by '/', no leading/trailing slash,
  53. * no "." or "..", no control characters, total length < SHARE_PATH_MAX. */
  54. static int path_ok(const char *path)
  55. {
  56. unsigned int i = 0, comp = 0;
  57. if (!path || !path[0])
  58. return 0;
  59. while (path[i]) {
  60. char c = path[i];
  61. if (c == '/') {
  62. if (comp == 0)
  63. return 0;
  64. if (comp == 1 && path[i - 1] == '.')
  65. return 0;
  66. if (comp == 2 && path[i - 1] == '.' &&
  67. path[i - 2] == '.')
  68. return 0;
  69. comp = 0;
  70. } else {
  71. if ((unsigned char)c < 0x20)
  72. return 0;
  73. if (++comp > SHARE_PATH_MAX - 2)
  74. return 0;
  75. }
  76. if (++i >= SHARE_PATH_MAX)
  77. return 0;
  78. }
  79. if (comp == 0)
  80. return 0;
  81. if (comp == 1 && path[i - 1] == '.')
  82. return 0;
  83. if (comp == 2 && path[i - 1] == '.' && path[i - 2] == '.')
  84. return 0;
  85. return 1;
  86. }
  87. static void parent_of(const char *path, char *parent)
  88. {
  89. int i = (int)s_len(path) - 1;
  90. while (i >= 0 && path[i] != '/')
  91. i--;
  92. if (i < 0) {
  93. parent[0] = '\0';
  94. return;
  95. }
  96. s_copy(parent, path, (unsigned int)i + 1);
  97. parent[i] = '\0';
  98. }
  99. static int lookup(const char *path)
  100. {
  101. unsigned int i;
  102. for (i = 0; i < ent_count; i++)
  103. if (s_eq(ents[i].path, path))
  104. return (int)i;
  105. return -1;
  106. }
  107. static void free_data(unsigned int base, unsigned int size)
  108. {
  109. unsigned int pages = (size + 0xFFFu) >> 12;
  110. unsigned int i;
  111. if (!base)
  112. return;
  113. for (i = 0; i < pages; i++)
  114. mm_free_page(base + i * 0x1000);
  115. }
  116. static void remove_ent(unsigned int idx)
  117. {
  118. free_data(ents[idx].base, ents[idx].size);
  119. used_bytes -= ents[idx].size;
  120. ent_count--;
  121. if (idx != ent_count)
  122. ents[idx] = ents[ent_count];
  123. }
  124. /* Full path of the child of `dir` containing `p`, into `name`. Returns 0
  125. * when the entry is not directly inside `dir`. */
  126. static int child_name(const char *p, const char *dir, char *name,
  127. unsigned int cap)
  128. {
  129. unsigned int dl = s_len(dir), n = 0, i;
  130. const char *rest;
  131. if (dl) {
  132. for (i = 0; i < dl; i++)
  133. if (p[i] != dir[i])
  134. return 0;
  135. if (p[dl] != '/')
  136. return 0;
  137. rest = p + dl + 1;
  138. } else {
  139. rest = p;
  140. }
  141. if (!rest[0])
  142. return 0;
  143. while (rest[n] && rest[n] != '/')
  144. n++;
  145. if (n >= cap)
  146. return 0;
  147. mem_copy(name, rest, n);
  148. name[n] = '\0';
  149. return 1;
  150. }
  151. /* ---- fs_ops ---- */
  152. static int share_op_read_file(struct vfs_mount *m, const char *path,
  153. void *buf, unsigned int max)
  154. {
  155. int idx;
  156. (void)m;
  157. idx = lookup(path);
  158. if (idx < 0)
  159. return VFS_ENOENT;
  160. if (ents[idx].is_dir)
  161. return VFS_EINVAL;
  162. if (ents[idx].size > max)
  163. return (int)max;
  164. mem_copy(buf, (const void *)ents[idx].base, ents[idx].size);
  165. return (int)ents[idx].size;
  166. }
  167. static int share_op_load_file(struct vfs_mount *m, const char *path,
  168. struct vfs_file *out)
  169. {
  170. int idx;
  171. (void)m;
  172. idx = lookup(path);
  173. if (idx < 0)
  174. return VFS_ENOENT;
  175. if (ents[idx].is_dir)
  176. return VFS_EINVAL;
  177. out->data = (const void *)ents[idx].base;
  178. out->size = ents[idx].size;
  179. return 0;
  180. }
  181. static void share_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  182. {
  183. (void)m;
  184. (void)f;
  185. }
  186. static int share_op_stat(struct vfs_mount *m, const char *path,
  187. unsigned int *size, int *is_dir)
  188. {
  189. int idx;
  190. (void)m;
  191. if (path[0] == '\0') {
  192. if (size)
  193. *size = 0;
  194. if (is_dir)
  195. *is_dir = 1;
  196. return 0;
  197. }
  198. idx = lookup(path);
  199. if (idx < 0)
  200. return VFS_ENOENT;
  201. if (size)
  202. *size = ents[idx].is_dir ? 0 : ents[idx].size;
  203. if (is_dir)
  204. *is_dir = (int)ents[idx].is_dir;
  205. return 0;
  206. }
  207. static int share_op_write_file(struct vfs_mount *m, const char *path,
  208. const void *buf, unsigned int len)
  209. {
  210. char parent[SHARE_PATH_MAX];
  211. unsigned int base = 0, old_size = 0;
  212. int idx, pidx;
  213. (void)m;
  214. if (!path_ok(path))
  215. return VFS_EINVAL;
  216. idx = lookup(path);
  217. if (idx >= 0 && ents[idx].is_dir)
  218. return VFS_EINVAL;
  219. parent_of(path, parent);
  220. if (parent[0]) {
  221. pidx = lookup(parent);
  222. if (pidx < 0 || !ents[pidx].is_dir)
  223. return VFS_EINVAL;
  224. }
  225. if (idx >= 0)
  226. old_size = ents[idx].size;
  227. if (used_bytes - old_size + len > SHARE_MAX_BYTES)
  228. return VFS_ENOSPC;
  229. if (len) {
  230. unsigned int pages = (len + 0xFFFu) >> 12;
  231. base = mm_alloc_pages(pages);
  232. if (!base)
  233. return VFS_ENOSPC;
  234. mem_copy((void *)base, buf, len);
  235. }
  236. if (idx < 0) {
  237. if (ent_count >= SHARE_MAX_ENTRIES) {
  238. free_data(base, len);
  239. return VFS_ENOSPC;
  240. }
  241. idx = (int)ent_count++;
  242. s_copy(ents[idx].path, path, sizeof(ents[idx].path));
  243. ents[idx].base = 0;
  244. ents[idx].size = 0;
  245. ents[idx].is_dir = 0;
  246. }
  247. free_data(ents[idx].base, ents[idx].size);
  248. used_bytes -= ents[idx].size;
  249. ents[idx].base = base;
  250. ents[idx].size = len;
  251. ents[idx].is_dir = 0;
  252. used_bytes += len;
  253. return 0;
  254. }
  255. static int share_op_unlink(struct vfs_mount *m, const char *path)
  256. {
  257. int idx = lookup(path);
  258. (void)m;
  259. if (idx < 0)
  260. return VFS_ENOENT;
  261. if (ents[idx].is_dir)
  262. return VFS_EINVAL;
  263. remove_ent((unsigned int)idx);
  264. return 0;
  265. }
  266. static int share_op_mkdir(struct vfs_mount *m, const char *path)
  267. {
  268. char parent[SHARE_PATH_MAX];
  269. unsigned int i;
  270. int pidx;
  271. (void)m;
  272. if (!path_ok(path) || lookup(path) >= 0)
  273. return VFS_EINVAL;
  274. parent_of(path, parent);
  275. if (parent[0]) {
  276. pidx = lookup(parent);
  277. if (pidx < 0 || !ents[pidx].is_dir)
  278. return VFS_EINVAL;
  279. }
  280. /* Do not create a directory whose path is already a file prefix. */
  281. for (i = 0; i < ent_count; i++) {
  282. unsigned int pl = s_len(path);
  283. if (s_len(ents[i].path) > pl &&
  284. ents[i].path[pl] == '/') {
  285. unsigned int j;
  286. for (j = 0; j < pl && ents[i].path[j] == path[j]; j++)
  287. ;
  288. if (j == pl)
  289. return VFS_EINVAL;
  290. }
  291. }
  292. if (ent_count >= SHARE_MAX_ENTRIES)
  293. return VFS_ENOSPC;
  294. i = ent_count++;
  295. s_copy(ents[i].path, path, sizeof(ents[i].path));
  296. ents[i].base = 0;
  297. ents[i].size = 0;
  298. ents[i].is_dir = 1;
  299. return 0;
  300. }
  301. static int share_op_rmdir(struct vfs_mount *m, const char *path)
  302. {
  303. unsigned int i, pl;
  304. int idx = lookup(path);
  305. (void)m;
  306. if (idx < 0)
  307. return VFS_ENOENT;
  308. if (!ents[idx].is_dir)
  309. return VFS_EINVAL;
  310. pl = s_len(path);
  311. for (i = 0; i < ent_count; i++) {
  312. unsigned int j;
  313. if (s_len(ents[i].path) <= pl || ents[i].path[pl] != '/')
  314. continue;
  315. for (j = 0; j < pl && ents[i].path[j] == path[j]; j++)
  316. ;
  317. if (j == pl)
  318. return VFS_EINVAL; /* not empty */
  319. }
  320. remove_ent((unsigned int)idx);
  321. return 0;
  322. }
  323. static int share_op_list_dir(struct vfs_mount *m, const char *path,
  324. unsigned int index, struct vfs_dirent *out)
  325. {
  326. char name[SHARE_PATH_MAX], seen_name[SHARE_PATH_MAX];
  327. unsigned int i, seen = 0;
  328. int idx;
  329. (void)m;
  330. if (path[0]) {
  331. idx = lookup(path);
  332. if (idx < 0)
  333. return VFS_ENOENT;
  334. if (!ents[idx].is_dir)
  335. return VFS_EINVAL;
  336. }
  337. for (i = 0; i < ent_count; i++) {
  338. unsigned int j;
  339. int dup = 0, exact;
  340. char full[SHARE_PATH_MAX];
  341. if (!child_name(ents[i].path, path, name, sizeof(name)))
  342. continue;
  343. for (j = 0; j < i; j++) {
  344. if (child_name(ents[j].path, path, seen_name,
  345. sizeof(seen_name)) &&
  346. s_eq(seen_name, name)) {
  347. dup = 1;
  348. break;
  349. }
  350. }
  351. if (dup)
  352. continue;
  353. if (seen++ != index)
  354. continue;
  355. /* The child is a directory when its own entry says so. */
  356. if (path[0]) {
  357. unsigned int k = 0;
  358. for (; path[k] && k + 1 < sizeof(full); k++)
  359. full[k] = path[k];
  360. if (k + 1 < sizeof(full))
  361. full[k++] = '/';
  362. for (j = 0; name[j] && k + 1 < sizeof(full); j++)
  363. full[k++] = name[j];
  364. full[k] = '\0';
  365. } else {
  366. s_copy(full, name, sizeof(full));
  367. }
  368. exact = lookup(full);
  369. s_copy(out->name, name, sizeof(out->name));
  370. out->size = (exact >= 0 && !ents[exact].is_dir) ?
  371. ents[exact].size : 0;
  372. out->is_dir = (exact >= 0 && ents[exact].is_dir) ? 1 : 0;
  373. out->raw_83 = 0;
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. const struct fs_ops share_ops = {
  379. .raw_83 = 0,
  380. .read_file = share_op_read_file,
  381. .load_file = share_op_load_file,
  382. .release_file = share_op_release_file,
  383. .stat = share_op_stat,
  384. .write_file = share_op_write_file,
  385. .unlink = share_op_unlink,
  386. .mkdir = share_op_mkdir,
  387. .rmdir = share_op_rmdir,
  388. .list_dir = share_op_list_dir,
  389. };
  390. int share_init(void)
  391. {
  392. if (ents)
  393. return 0;
  394. ents = kmalloc(sizeof(*ents) * SHARE_MAX_ENTRIES);
  395. if (!ents)
  396. return -1;
  397. ent_count = 0;
  398. used_bytes = 0;
  399. return 0;
  400. }