shim_fs.c 18 KB

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