shim_fs.c 17 KB

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