shim_fs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 <linux_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. static LIST_HEAD(mount_list);
  56. static LOCKTYPE 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 (void)
  73. {
  74. int ret;
  75. if ((ret = mount_fs("chroot", "file:", "/")) < 0) {
  76. debug("mounting root filesystem failed (%e)\n", ret);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int __mount_sys (void)
  82. {
  83. int ret;
  84. debug("mounting as proc filesystem: /proc\n");
  85. if ((ret = mount_fs("proc", NULL, "/proc")) < 0) {
  86. debug("mounting proc filesystem failed (%e)\n", ret);
  87. return ret;
  88. }
  89. debug("mounting as dev filesystem: /dev\n");
  90. if ((ret = mount_fs("dev", NULL, "/dev")) < 0) {
  91. debug("mounting dev filesystem failed (%e)\n", ret);
  92. return ret;
  93. }
  94. debug("mounting as chroot filesystem: from dev:tty to /dev\n");
  95. if ((ret = mount_fs("chroot", "dev:tty", "/dev/tty")) < 0) {
  96. debug("mounting terminal device failed (%e)\n", ret);
  97. return ret;
  98. }
  99. return 0;
  100. }
  101. static int __mount_one_other (const char * key, int keylen)
  102. {
  103. if (!root_config)
  104. return 0;
  105. char k[CONFIG_MAX], p[CONFIG_MAX], u[CONFIG_MAX],
  106. t[CONFIG_MAX];
  107. char * uri = NULL;
  108. int ret;
  109. memcpy(k, "fs.mount.", 9);
  110. memcpy(k + 9, key, keylen);
  111. char * kp = k + 9 + keylen;
  112. memcpy(kp, ".path", 6);
  113. if (get_config(root_config, k, p, CONFIG_MAX) <= 0)
  114. return -EINVAL;
  115. memcpy(kp, ".type", 6);
  116. if (get_config(root_config, k, t, CONFIG_MAX) <= 0)
  117. return -EINVAL;
  118. memcpy(kp, ".uri", 5);
  119. if (get_config(root_config, k, u, CONFIG_MAX) > 0)
  120. uri = u;
  121. debug("mounting as %s filesystem: from %s to %s\n", t, uri, p);
  122. if ((ret = mount_fs(t, uri, p)) < 0) {
  123. debug("mounting %s on %s (type=%s) failed (%e)\n", t, uri, p,
  124. -ret);
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. static int __mount_others (void)
  130. {
  131. if (!root_config)
  132. return 0;
  133. int nkeys, keybuf_size = CONFIG_MAX;
  134. char * keybuf = __alloca(keybuf_size);
  135. while ((nkeys = get_config_entries(root_config, "fs.mount", keybuf,
  136. keybuf_size)) == -ENAMETOOLONG) {
  137. keybuf = __alloca(keybuf_size);
  138. keybuf_size *= 2;
  139. }
  140. if (nkeys < 0)
  141. return 0;
  142. const char * key = keybuf, * next = NULL;
  143. for (int n = 0 ; n < nkeys ; key = next, n++) {
  144. for (next = key ; *next ; next++);
  145. next++;
  146. int ret = __mount_one_other(key, next - key - 1);
  147. if (ret < 0)
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. int init_mount_root (void)
  153. {
  154. if (mount_migrated)
  155. return 0;
  156. int ret;
  157. if ((ret = __mount_root()) < 0)
  158. return ret;
  159. if ((ret = __mount_sys()) < 0)
  160. return ret;
  161. return 0;
  162. }
  163. int init_mount (void)
  164. {
  165. if (mount_migrated)
  166. return 0;
  167. int ret;
  168. if ((ret = __mount_others()) < 0)
  169. return ret;
  170. return 0;
  171. }
  172. static inline struct shim_fs * find_fs (const char * type)
  173. {
  174. struct shim_fs * fs = NULL;
  175. int len = strlen(type);
  176. for (int i = 0 ; i < NUM_MOUNTABLE_FS ; i++)
  177. if (!memcmp(type, mountable_fs[i].name, len + 1)) {
  178. fs = &mountable_fs[i];
  179. break;
  180. }
  181. return fs;
  182. }
  183. int search_builtin_fs (const char * type, struct shim_mount ** fs)
  184. {
  185. int len = strlen(type);
  186. for (int i = 0 ; i < NUM_BUILTIN_FS ; i++)
  187. if (!memcmp(type, builtin_fs[i]->type, len + 1)) {
  188. *fs = builtin_fs[i];
  189. return 0;
  190. }
  191. return -ENOENT;
  192. }
  193. int __mount_fs (struct shim_mount * mount, struct shim_dentry * dent)
  194. {
  195. int ret = 0;
  196. dent->state |= DENTRY_MOUNTPOINT;
  197. get_dentry(dent);
  198. mount->mount_point = dent;
  199. dent->mounted = mount;
  200. struct shim_dentry * mount_root = mount->root;
  201. if (!mount_root) {
  202. mount_root = get_new_dentry(NULL, "", 0);
  203. mount_root->fs = mount;
  204. /* mount_root->state |= DENTRY_VALID; */
  205. qstrsetstr(&mount_root->name, dentry_get_name(dent),
  206. dent->name.len);
  207. if (mount->d_ops && mount->d_ops->lookup &&
  208. (ret = mount->d_ops->lookup(mount_root, 0)) < 0 &&
  209. ret != -ESKIPPED)
  210. return ret;
  211. mount->root = mount_root;
  212. }
  213. mount_root->state |= dent->state & (DENTRY_REACHABLE|DENTRY_UNREACHABLE);
  214. __add_dcache(mount_root, &mount->path.hash);
  215. if ((ret = __del_dentry_tree(dent)) < 0)
  216. return ret;
  217. lock(mount_list_lock);
  218. get_mount(mount);
  219. list_add_tail(&mount->list, &mount_list);
  220. unlock(mount_list_lock);
  221. do {
  222. struct shim_dentry * parent = dent->parent;
  223. if (dent->state & DENTRY_ANCESTER) {
  224. put_dentry(dent);
  225. break;
  226. }
  227. dent->state |= DENTRY_ANCESTER;
  228. if (parent)
  229. get_dentry(parent);
  230. put_dentry(dent);
  231. dent = parent;
  232. } while (dent);
  233. return 0;
  234. }
  235. int mount_fs (const char * type, const char * uri, const char * mount_point)
  236. {
  237. int ret = 0;
  238. struct shim_fs * fs = find_fs(type);
  239. if (!fs || !fs->fs_ops || !fs->fs_ops->mount) {
  240. ret = -ENODEV;
  241. goto out;
  242. }
  243. lock(dcache_lock);
  244. struct shim_dentry * dent;
  245. if ((ret = __path_lookupat(NULL, mount_point, 0, &dent)) < 0)
  246. goto out;
  247. struct shim_mount * mount = alloc_mount();
  248. void * mount_data = NULL;
  249. /* call fs-specific mount to allocate mount_data */
  250. if ((ret = fs->fs_ops->mount(uri, mount_point, &mount_data)) < 0)
  251. goto out;
  252. int uri_len = uri ? strlen(uri) : 0;
  253. qstrsetstr(&mount->path, mount_point, strlen(mount_point));
  254. qstrsetstr(&mount->uri, uri, uri_len);
  255. memcpy(mount->type, fs->name, sizeof(fs->name));
  256. mount->fs_ops = fs->fs_ops;
  257. mount->d_ops = fs->d_ops;
  258. mount->data = mount_data;
  259. mount->path.hash = dent->rel_path.hash;
  260. ret = __mount_fs(mount, dent);
  261. out:
  262. unlock(dcache_lock);
  263. return ret;
  264. }
  265. void get_mount (struct shim_mount * mount)
  266. {
  267. REF_INC(mount->ref_count);
  268. }
  269. void put_mount (struct shim_mount * mount)
  270. {
  271. REF_DEC(mount->ref_count);
  272. }
  273. int walk_mounts (int (*walk) (struct shim_mount * mount, void * arg),
  274. void * arg)
  275. {
  276. struct shim_mount * mount, * n;
  277. int ret;
  278. int nsrched = 0;
  279. lock(mount_list_lock);
  280. list_for_each_entry_safe(mount, n, &mount_list, list) {
  281. if ((ret = (*walk) (mount, arg)) < 0)
  282. break;
  283. if (ret > 0)
  284. nsrched++;
  285. }
  286. unlock(mount_list_lock);
  287. return ret < 0 ? ret : (nsrched ? 0 : -ESRCH);
  288. }
  289. struct shim_mount * find_mount_from_uri (const char * uri)
  290. {
  291. struct shim_mount * mount, * found = NULL;
  292. int longest_path = 0;
  293. lock(mount_list_lock);
  294. list_for_each_entry(mount, &mount_list, list) {
  295. if (qstrempty(&mount->uri))
  296. continue;
  297. if (!memcmp(qstrgetstr(&mount->uri), uri, mount->uri.len) &&
  298. (uri[mount->uri.len] == '/' || uri[mount->uri.len] == '/')) {
  299. if (mount->path.len > longest_path) {
  300. longest_path = mount->path.len;
  301. found = mount;
  302. }
  303. }
  304. }
  305. if (found)
  306. get_mount(found);
  307. unlock(mount_list_lock);
  308. return found;
  309. }
  310. BEGIN_CP_FUNC(mount)
  311. {
  312. assert(size == sizeof(struct shim_mount));
  313. struct shim_mount * mount = (struct shim_mount *) obj;
  314. struct shim_mount * new_mount = NULL;
  315. ptr_t off = GET_FROM_CP_MAP(obj);
  316. if (!off) {
  317. off = ADD_CP_OFFSET(sizeof(struct shim_mount));
  318. ADD_TO_CP_MAP(obj, off);
  319. if (!mount->cpdata &&
  320. mount->fs_ops &&
  321. mount->fs_ops->checkpoint) {
  322. void * cpdata = NULL;
  323. int bytes = mount->fs_ops->checkpoint(&cpdata, mount->data);
  324. if (bytes > 0) {
  325. mount->cpdata = cpdata;
  326. mount->cpsize = bytes;
  327. }
  328. }
  329. new_mount = (struct shim_mount *) (base + off);
  330. *new_mount = *mount;
  331. if (mount->cpdata) {
  332. struct shim_mem_entry * entry;
  333. DO_CP_SIZE(memory, mount->cpdata, mount->cpsize, &entry);
  334. new_mount->cpdata = NULL;
  335. entry->paddr = &new_mount->cpdata;
  336. }
  337. new_mount->data = NULL;
  338. new_mount->mount_point = NULL;
  339. new_mount->root = NULL;
  340. INIT_LIST_HEAD(&new_mount->list);
  341. DO_CP_IN_MEMBER(qstr, new_mount, path);
  342. DO_CP_IN_MEMBER(qstr, new_mount, uri);
  343. if (mount->mount_point)
  344. DO_CP_MEMBER(dentry, mount, new_mount, mount_point);
  345. if (mount->root)
  346. DO_CP_MEMBER(dentry, mount, new_mount, root);
  347. ADD_CP_FUNC_ENTRY(off);
  348. } else {
  349. new_mount = (struct shim_mount *) (base + off);
  350. }
  351. if (objp)
  352. *objp = (void *) new_mount;
  353. }
  354. END_CP_FUNC(mount)
  355. BEGIN_RS_FUNC(mount)
  356. {
  357. struct shim_mount * mount = (void *) (base + GET_CP_FUNC_ENTRY());
  358. CP_REBASE(mount->cpdata);
  359. CP_REBASE(mount->list);
  360. CP_REBASE(mount->mount_point);
  361. CP_REBASE(mount->root);
  362. struct shim_fs * fs = find_fs(mount->type);
  363. if (fs && fs->fs_ops && fs->fs_ops->migrate && mount->cpdata) {
  364. void * mount_data = NULL;
  365. if (fs->fs_ops->migrate(mount->cpdata, &mount_data) == 0)
  366. mount->data = mount_data;
  367. mount->cpdata = NULL;
  368. }
  369. mount->fs_ops = fs->fs_ops;
  370. mount->d_ops = fs->d_ops;
  371. list_add_tail(&mount->list, &mount_list);
  372. if (!qstrempty(&mount->path)) {
  373. DEBUG_RS("type=%s,uri=%s,path=%s", mount->type, qstrgetstr(&mount->uri),
  374. qstrgetstr(&mount->path));
  375. } else {
  376. DEBUG_RS("type=%s,uri=%s", mount->type, qstrgetstr(&mount->uri));
  377. }
  378. }
  379. END_RS_FUNC(mount)
  380. BEGIN_CP_FUNC(all_mounts)
  381. {
  382. struct shim_mount * mount;
  383. lock(mount_list_lock);
  384. list_for_each_entry(mount, &mount_list, list)
  385. DO_CP(mount, mount, NULL);
  386. unlock(mount_list_lock);
  387. /* add an empty entry to mark as migrated */
  388. ADD_CP_FUNC_ENTRY(0);
  389. }
  390. END_CP_FUNC(all_mounts)
  391. BEGIN_RS_FUNC(all_mounts)
  392. {
  393. /* to prevent file system from being mount again */
  394. mount_migrated = true;
  395. }
  396. END_RS_FUNC(all_mounts)
  397. const char * get_file_name (const char * path, size_t len)
  398. {
  399. const char * c = path + len - 1;
  400. while (c > path && *c != '/')
  401. c--;
  402. return *c == '/' ? c + 1 : c;
  403. }