shim_fs.c 18 KB

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