shim_fs.c 13 KB

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