shim_fs.c 15 KB

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