shim_fs.c 17 KB

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