shim_fs.c 13 KB

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