shim_fs.c 18 KB

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