shim_fs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_fs.c
  15. *
  16. * This file contains codes for creating filesystems in library OS.
  17. */
  18. #include <linux/fcntl.h>
  19. #include <list.h>
  20. #include <pal.h>
  21. #include <pal_debug.h>
  22. #include <pal_error.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_fs.h>
  25. #include <shim_internal.h>
  26. #include <shim_utils.h>
  27. struct shim_fs {
  28. char name[8];
  29. struct shim_fs_ops* fs_ops;
  30. struct shim_d_ops* d_ops;
  31. };
  32. #define NUM_MOUNTABLE_FS 3
  33. struct shim_fs mountable_fs[NUM_MOUNTABLE_FS] = {
  34. {
  35. .name = "chroot",
  36. .fs_ops = &chroot_fs_ops,
  37. .d_ops = &chroot_d_ops,
  38. },
  39. {
  40. .name = "proc",
  41. .fs_ops = &proc_fs_ops,
  42. .d_ops = &proc_d_ops,
  43. },
  44. {
  45. .name = "dev",
  46. .fs_ops = &dev_fs_ops,
  47. .d_ops = &dev_d_ops,
  48. },
  49. };
  50. #define NUM_BUILTIN_FS 5
  51. struct shim_mount* builtin_fs[NUM_BUILTIN_FS] = {
  52. &chroot_builtin_fs,
  53. &pipe_builtin_fs,
  54. &socket_builtin_fs,
  55. &epoll_builtin_fs,
  56. &eventfd_builtin_fs,
  57. };
  58. static struct shim_lock mount_mgr_lock;
  59. #define SYSTEM_LOCK() lock(&mount_mgr_lock)
  60. #define SYSTEM_UNLOCK() unlock(&mount_mgr_lock)
  61. #define SYSTEM_LOCKED() locked(&mount_mgr_lock)
  62. #define MOUNT_MGR_ALLOC 64
  63. #define OBJ_TYPE struct shim_mount
  64. #include <memmgr.h>
  65. static MEM_MGR mount_mgr = NULL;
  66. DEFINE_LISTP(shim_mount);
  67. /* Links to mount->list */
  68. static LISTP_TYPE(shim_mount) mount_list;
  69. static struct shim_lock mount_list_lock;
  70. int init_fs(void) {
  71. mount_mgr = create_mem_mgr(init_align_up(MOUNT_MGR_ALLOC));
  72. if (!mount_mgr)
  73. return -ENOMEM;
  74. create_lock(&mount_mgr_lock);
  75. create_lock(&mount_list_lock);
  76. return 0;
  77. }
  78. static struct shim_mount* alloc_mount(void) {
  79. return get_mem_obj_from_mgr_enlarge(mount_mgr, size_align_up(MOUNT_MGR_ALLOC));
  80. }
  81. static bool mount_migrated = false;
  82. static int __mount_root(struct shim_dentry** root) {
  83. char type[CONFIG_MAX];
  84. char uri[CONFIG_MAX];
  85. int ret = 0;
  86. if (root_config && get_config(root_config, "fs.root.type", type, sizeof(type)) > 0 &&
  87. get_config(root_config, "fs.root.uri", uri, sizeof(uri)) > 0) {
  88. debug("mounting root filesystem: %s from %s\n", type, uri);
  89. if ((ret = mount_fs(type, uri, "/", NULL, root, 0)) < 0) {
  90. debug("mounting root filesystem failed (%d)\n", ret);
  91. return ret;
  92. }
  93. return ret;
  94. }
  95. debug("mounting default root filesystem\n");
  96. if ((ret = mount_fs("chroot", URI_PREFIX_FILE, "/", NULL, root, 0)) < 0) {
  97. debug("mounting root filesystem failed (%d)\n", ret);
  98. }
  99. return ret;
  100. }
  101. static int __mount_sys(struct shim_dentry* root) {
  102. int ret;
  103. debug("mounting as proc filesystem: /proc\n");
  104. if ((ret = mount_fs("proc", NULL, "/proc", root, NULL, 0)) < 0) {
  105. debug("mounting proc filesystem failed (%d)\n", ret);
  106. return ret;
  107. }
  108. debug("mounting as dev filesystem: /dev\n");
  109. struct shim_dentry* dev_dent = NULL;
  110. if ((ret = mount_fs("dev", NULL, "/dev", root, &dev_dent, 0)) < 0) {
  111. debug("mounting dev filesystem failed (%d)\n", ret);
  112. return ret;
  113. }
  114. debug("mounting as chroot filesystem: from dev:tty to /dev\n");
  115. if ((ret = mount_fs("chroot", URI_PREFIX_DEV "tty", "/dev/tty", dev_dent, NULL, 0)) < 0) {
  116. debug("mounting terminal device failed (%d)\n", ret);
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. static int __mount_one_other(const char* key, int keylen) {
  122. if (!root_config)
  123. return 0;
  124. char k[CONFIG_MAX];
  125. char p[CONFIG_MAX];
  126. char u[CONFIG_MAX];
  127. char t[CONFIG_MAX];
  128. char* uri = NULL;
  129. int ret;
  130. memcpy(k, "fs.mount.", 9);
  131. memcpy(k + 9, key, keylen);
  132. char* kp = k + 9 + keylen;
  133. memcpy(kp, ".path", 6);
  134. if (get_config(root_config, k, p, sizeof(p)) <= 0)
  135. return -EINVAL;
  136. memcpy(kp, ".type", 6);
  137. if (get_config(root_config, k, t, sizeof(t)) <= 0)
  138. return -EINVAL;
  139. memcpy(kp, ".uri", 5);
  140. if (get_config(root_config, k, u, sizeof(u)) > 0)
  141. uri = u;
  142. debug("mounting as %s filesystem: from %s to %s\n", t, uri, p);
  143. if ((ret = mount_fs(t, uri, p, NULL, NULL, 1)) < 0) {
  144. debug("mounting %s on %s (type=%s) failed (%d)\n", uri, p, t, -ret);
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. static int __mount_others(void) {
  150. char* keybuf;
  151. int ret = 0;
  152. if (!root_config)
  153. return 0;
  154. int nkeys;
  155. ssize_t keybuf_size;
  156. keybuf_size = get_config_entries_size(root_config, "fs.mount");
  157. if (keybuf_size < 0)
  158. return 0;
  159. keybuf = malloc(keybuf_size);
  160. if (!keybuf)
  161. return -ENOMEM;
  162. nkeys = get_config_entries(root_config, "fs.mount", keybuf, keybuf_size);
  163. if (nkeys <= 0)
  164. goto out;
  165. const char *key = keybuf;
  166. const char *next = NULL;
  167. for (int n = 0; n < nkeys; key = next, n++) {
  168. for (next = key; *next; next++)
  169. ;
  170. next++;
  171. ret = __mount_one_other(key, next - key - 1);
  172. if (ret < 0)
  173. goto out;
  174. }
  175. out:
  176. free(keybuf);
  177. return ret;
  178. }
  179. int init_mount_root(void) {
  180. if (mount_migrated)
  181. return 0;
  182. int ret;
  183. struct shim_dentry* root = NULL;
  184. if ((ret = __mount_root(&root)) < 0)
  185. return ret;
  186. if ((ret = __mount_sys(root)) < 0)
  187. return ret;
  188. return 0;
  189. }
  190. int init_mount(void) {
  191. if (mount_migrated)
  192. return 0;
  193. int ret;
  194. if ((ret = __mount_others()) < 0)
  195. return ret;
  196. return 0;
  197. }
  198. static inline struct shim_fs* find_fs(const char* type) {
  199. struct shim_fs* fs = NULL;
  200. size_t len = strlen(type);
  201. for (int i = 0; i < NUM_MOUNTABLE_FS; i++)
  202. if (!memcmp(type, mountable_fs[i].name, len + 1)) {
  203. fs = &mountable_fs[i];
  204. break;
  205. }
  206. return fs;
  207. }
  208. int search_builtin_fs(const char* type, struct shim_mount** fs) {
  209. size_t len = strlen(type);
  210. for (int i = 0; i < NUM_BUILTIN_FS; i++)
  211. if (!memcmp(type, builtin_fs[i]->type, len + 1)) {
  212. *fs = builtin_fs[i];
  213. return 0;
  214. }
  215. return -ENOENT;
  216. }
  217. int __mount_fs(struct shim_mount* mount, struct shim_dentry* dent) {
  218. assert(locked(&dcache_lock));
  219. int ret = 0;
  220. dent->state |= DENTRY_MOUNTPOINT;
  221. get_dentry(dent);
  222. mount->mount_point = dent;
  223. dent->mounted = mount;
  224. struct shim_dentry* mount_root = mount->root;
  225. if (!mount_root) {
  226. /* mount_root->state |= DENTRY_VALID; */
  227. mount_root = get_new_dentry(mount, NULL, "", 0, NULL);
  228. assert(mount->d_ops && mount->d_ops->lookup);
  229. ret = mount->d_ops->lookup(mount_root);
  230. if (ret < 0) {
  231. /* Try getting rid of ESKIPPED case */
  232. assert(ret != -ESKIPPED);
  233. put_dentry(mount_root);
  234. return ret;
  235. }
  236. mount->root = mount_root;
  237. }
  238. /* DEP 7/1/17: If the mount is a directory, make sure the mount
  239. * point is marked as a directory */
  240. if (mount_root->state & DENTRY_ISDIRECTORY)
  241. dent->state |= DENTRY_ISDIRECTORY;
  242. /* DEP 6/16/17: In the dcache redesign, we don't use the *REACHABLE flags, but
  243. * leaving this commented for documentation, in case there is a problem
  244. * I over-simplified */
  245. // mount_root->state |= dent->state & (DENTRY_REACHABLE|DENTRY_UNREACHABLE);
  246. /* DEP 6/16/17: In the dcache redesign, I don't believe we need to manually
  247. * rehash the path; this should be handled by get_new_dentry, or already be
  248. * hashed if mount_root exists. I'm going to leave this line here for now
  249. * as documentation in case there is a problem later.
  250. */
  251. //__add_dcache(mount_root, &mount->path.hash);
  252. if ((ret = __del_dentry_tree(dent)) < 0)
  253. return ret;
  254. lock(&mount_list_lock);
  255. get_mount(mount);
  256. LISTP_ADD_TAIL(mount, &mount_list, list);
  257. unlock(&mount_list_lock);
  258. do {
  259. struct shim_dentry* parent = dent->parent;
  260. if (dent->state & DENTRY_ANCESTOR) {
  261. put_dentry(dent);
  262. break;
  263. }
  264. dent->state |= DENTRY_ANCESTOR;
  265. if (parent)
  266. get_dentry(parent);
  267. put_dentry(dent);
  268. dent = parent;
  269. } while (dent);
  270. return 0;
  271. }
  272. /* Extracts the last component of the `path`. If there's none, `*last_elem_len` is set to 0 and
  273. * `*last_elem` is set to NULL. */
  274. static void find_last_component(const char* path, const char** last_comp, size_t* last_comp_len) {
  275. *last_comp = NULL;
  276. size_t last_len = 0;
  277. size_t path_len = strlen(path);
  278. if (path_len == 0)
  279. goto out;
  280. // Drop any trailing slashes.
  281. const char* last = path + path_len - 1;
  282. while (last > path && *last == '/')
  283. last--;
  284. if (*last == '/')
  285. goto out;
  286. // Skip the last component.
  287. last_len = 1;
  288. while (last > path && *(last - 1) != '/') {
  289. last--;
  290. last_len++;
  291. }
  292. *last_comp = last;
  293. out:
  294. *last_comp_len = last_len;
  295. }
  296. /* Parent is optional, but helpful.
  297. * dentp (optional) memoizes the dentry of the newly-mounted FS, on success.
  298. *
  299. * The make_ancestor flag creates pseudo-dentries for any missing paths (passed to __path_lookupat).
  300. * This is only intended for use to connect mounts specified in the manifest when an intervening
  301. * path is missing.
  302. */
  303. int mount_fs(const char* type, const char* uri, const char* mount_point, struct shim_dentry* parent,
  304. struct shim_dentry** dentp, bool make_ancestor) {
  305. int ret = 0;
  306. struct shim_fs* fs = find_fs(type);
  307. if (!fs || !fs->fs_ops || !fs->fs_ops->mount) {
  308. ret = -ENODEV;
  309. goto out;
  310. }
  311. /* Split the mount point into the prefix and atom */
  312. size_t mount_point_len = strlen(mount_point);
  313. if (mount_point_len == 0) {
  314. ret = -EINVAL;
  315. goto out;
  316. }
  317. const char* last;
  318. size_t last_len;
  319. find_last_component(mount_point, &last, &last_len);
  320. if (!parent) {
  321. // See if we are not at the root mount
  322. if (last_len > 0) {
  323. // Look up the parent
  324. size_t parent_len = last - mount_point;
  325. char* parent_path = __alloca(parent_len + 1);
  326. memcpy(parent_path, mount_point, parent_len);
  327. parent_path[parent_len] = 0;
  328. if ((ret = __path_lookupat(dentry_root, parent_path, 0, &parent, 0, dentry_root->fs,
  329. make_ancestor)) < 0) {
  330. debug("Path lookup failed %d\n", ret);
  331. goto out;
  332. }
  333. }
  334. }
  335. if (parent && last_len > 0) {
  336. /* Newly created dentry's relative path will be a concatenation of parent
  337. * + last strings (see get_new_dentry), make sure it fits into qstr */
  338. if (parent->rel_path.len + 1 + last_len >= STR_SIZE) { /* +1 for '/' */
  339. debug("Relative path exceeds the limit %d\n", STR_SIZE);
  340. ret = -ENAMETOOLONG;
  341. goto out;
  342. }
  343. }
  344. lock(&dcache_lock);
  345. struct shim_mount* mount = alloc_mount();
  346. void* mount_data = NULL;
  347. /* call fs-specific mount to allocate mount_data */
  348. if ((ret = fs->fs_ops->mount(uri, &mount_data)) < 0)
  349. goto out_with_unlock;
  350. size_t uri_len = uri ? strlen(uri) : 0;
  351. qstrsetstr(&mount->path, mount_point, mount_point_len);
  352. qstrsetstr(&mount->uri, uri, uri_len);
  353. memcpy(mount->type, fs->name, sizeof(fs->name));
  354. mount->fs_ops = fs->fs_ops;
  355. mount->d_ops = fs->d_ops;
  356. mount->data = mount_data;
  357. /* Get the negative dentry from the cache, if one exists */
  358. struct shim_dentry* dent;
  359. struct shim_dentry* dent2;
  360. /* Special case the root */
  361. if (last_len == 0)
  362. dent = dentry_root;
  363. else {
  364. dent = __lookup_dcache(parent, last, last_len, NULL);
  365. if (!dent) {
  366. dent = get_new_dentry(mount, parent, last, last_len, NULL);
  367. }
  368. }
  369. assert(dent == dentry_root || !(dent->state & DENTRY_VALID));
  370. // We need to fix up the relative path to this mount, but only for
  371. // directories.
  372. qstrsetstr(&dent->rel_path, "", 0);
  373. mount->path.hash = dent->rel_path.hash;
  374. /*Now go ahead and do a lookup so the dentry is valid */
  375. if ((ret = __path_lookupat(dentry_root, mount_point, 0, &dent2, 0, parent ? parent->fs : mount,
  376. make_ancestor)) < 0)
  377. goto out_with_unlock;
  378. assert(dent == dent2);
  379. /* We want the net impact of mounting to increment the ref count on the
  380. * entry (until the unmount). But we shouldn't also hold the reference on
  381. * dent from the validation step. Drop it here */
  382. put_dentry(dent2);
  383. ret = __mount_fs(mount, dent);
  384. // If we made it this far and the dentry is still negative, clear
  385. // the negative flag from the denry.
  386. if (!ret && (dent->state & DENTRY_NEGATIVE))
  387. dent->state &= ~DENTRY_NEGATIVE;
  388. /* Set the file system at the mount point properly */
  389. dent->fs = mount;
  390. if (dentp && !ret)
  391. *dentp = dent;
  392. out_with_unlock:
  393. unlock(&dcache_lock);
  394. out:
  395. return ret;
  396. }
  397. void get_mount(struct shim_mount* mount) {
  398. REF_INC(mount->ref_count);
  399. }
  400. void put_mount(struct shim_mount* mount) {
  401. REF_DEC(mount->ref_count);
  402. }
  403. int walk_mounts(int (*walk)(struct shim_mount* mount, void* arg), void* arg) {
  404. struct shim_mount* mount;
  405. struct shim_mount* n;
  406. int ret = 0;
  407. int nsrched = 0;
  408. lock(&mount_list_lock);
  409. LISTP_FOR_EACH_ENTRY_SAFE(mount, n, &mount_list, list) {
  410. if ((ret = (*walk)(mount, arg)) < 0)
  411. break;
  412. if (ret > 0)
  413. nsrched++;
  414. }
  415. unlock(&mount_list_lock);
  416. return ret < 0 ? ret : (nsrched ? 0 : -ESRCH);
  417. }
  418. struct shim_mount* find_mount_from_uri(const char* uri) {
  419. struct shim_mount* mount;
  420. struct shim_mount* found = NULL;
  421. size_t longest_path = 0;
  422. lock(&mount_list_lock);
  423. LISTP_FOR_EACH_ENTRY(mount, &mount_list, list) {
  424. if (qstrempty(&mount->uri))
  425. continue;
  426. if (!memcmp(qstrgetstr(&mount->uri), uri, mount->uri.len) && uri[mount->uri.len] == '/') {
  427. if (mount->path.len > longest_path) {
  428. longest_path = mount->path.len;
  429. found = mount;
  430. }
  431. }
  432. }
  433. if (found)
  434. get_mount(found);
  435. unlock(&mount_list_lock);
  436. return found;
  437. }
  438. BEGIN_CP_FUNC(mount) {
  439. __UNUSED(size);
  440. assert(size == sizeof(struct shim_mount));
  441. struct shim_mount* mount = (struct shim_mount*)obj;
  442. struct shim_mount* new_mount = NULL;
  443. ptr_t off = GET_FROM_CP_MAP(obj);
  444. if (!off) {
  445. off = ADD_CP_OFFSET(sizeof(struct shim_mount));
  446. ADD_TO_CP_MAP(obj, off);
  447. mount->cpdata = NULL;
  448. if (mount->fs_ops && mount->fs_ops->checkpoint) {
  449. void* cpdata = NULL;
  450. int bytes = mount->fs_ops->checkpoint(&cpdata, mount->data);
  451. if (bytes > 0) {
  452. mount->cpdata = cpdata;
  453. mount->cpsize = bytes;
  454. }
  455. }
  456. new_mount = (struct shim_mount*)(base + off);
  457. *new_mount = *mount;
  458. if (mount->cpdata) {
  459. struct shim_mem_entry* entry;
  460. DO_CP_SIZE(memory, mount->cpdata, mount->cpsize, &entry);
  461. new_mount->cpdata = NULL;
  462. entry->paddr = &new_mount->cpdata;
  463. }
  464. new_mount->data = NULL;
  465. new_mount->mount_point = NULL;
  466. new_mount->root = NULL;
  467. INIT_LIST_HEAD(new_mount, list);
  468. DO_CP_IN_MEMBER(qstr, new_mount, path);
  469. DO_CP_IN_MEMBER(qstr, new_mount, uri);
  470. if (mount->mount_point)
  471. DO_CP_MEMBER(dentry, mount, new_mount, mount_point);
  472. if (mount->root)
  473. DO_CP_MEMBER(dentry, mount, new_mount, root);
  474. ADD_CP_FUNC_ENTRY(off);
  475. } else {
  476. new_mount = (struct shim_mount*)(base + off);
  477. }
  478. if (objp)
  479. *objp = (void*)new_mount;
  480. }
  481. END_CP_FUNC(mount)
  482. BEGIN_RS_FUNC(mount) {
  483. __UNUSED(offset);
  484. struct shim_mount* mount = (void*)(base + GET_CP_FUNC_ENTRY());
  485. CP_REBASE(mount->cpdata);
  486. CP_REBASE(mount->list);
  487. CP_REBASE(mount->mount_point);
  488. CP_REBASE(mount->root);
  489. struct shim_fs* fs = find_fs(mount->type);
  490. if (fs && fs->fs_ops && fs->fs_ops->migrate && mount->cpdata) {
  491. void* mount_data = NULL;
  492. if (fs->fs_ops->migrate(mount->cpdata, &mount_data) == 0)
  493. mount->data = mount_data;
  494. mount->cpdata = NULL;
  495. }
  496. mount->fs_ops = fs->fs_ops;
  497. mount->d_ops = fs->d_ops;
  498. LISTP_ADD_TAIL(mount, &mount_list, list);
  499. if (!qstrempty(&mount->path)) {
  500. DEBUG_RS("type=%s,uri=%s,path=%s", mount->type, qstrgetstr(&mount->uri),
  501. qstrgetstr(&mount->path));
  502. } else {
  503. DEBUG_RS("type=%s,uri=%s", mount->type, qstrgetstr(&mount->uri));
  504. }
  505. }
  506. END_RS_FUNC(mount)
  507. BEGIN_CP_FUNC(all_mounts) {
  508. __UNUSED(obj);
  509. __UNUSED(size);
  510. __UNUSED(objp);
  511. struct shim_mount* mount;
  512. lock(&mount_list_lock);
  513. LISTP_FOR_EACH_ENTRY(mount, &mount_list, list) {
  514. DO_CP(mount, mount, NULL);
  515. }
  516. unlock(&mount_list_lock);
  517. /* add an empty entry to mark as migrated */
  518. ADD_CP_FUNC_ENTRY(0UL);
  519. }
  520. END_CP_FUNC(all_mounts)
  521. BEGIN_RS_FUNC(all_mounts) {
  522. __UNUSED(entry);
  523. __UNUSED(base);
  524. __UNUSED(offset);
  525. __UNUSED(rebase);
  526. /* to prevent file system from being mount again */
  527. mount_migrated = true;
  528. }
  529. END_RS_FUNC(all_mounts)
  530. const char* get_file_name(const char* path, size_t len) {
  531. const char* c = path + len - 1;
  532. while (c > path && *c != '/')
  533. c--;
  534. return *c == '/' ? c + 1 : c;
  535. }