shim_fs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. struct shim_mount * find_mount_from_uri (const char * uri)
  299. {
  300. struct shim_mount * mount, * found = NULL;
  301. int longest_path = 0;
  302. lock(mount_list_lock);
  303. list_for_each_entry(mount, &mount_list, list) {
  304. if (qstrempty(&mount->uri))
  305. continue;
  306. if (!memcmp(qstrgetstr(&mount->uri), uri, mount->uri.len) &&
  307. (uri[mount->uri.len] == '/' || uri[mount->uri.len] == '/')) {
  308. if (mount->path.len > longest_path) {
  309. longest_path = mount->path.len;
  310. found = mount;
  311. }
  312. }
  313. }
  314. if (found)
  315. get_mount(found);
  316. unlock(mount_list_lock);
  317. return found;
  318. }
  319. BEGIN_CP_FUNC(mount)
  320. {
  321. assert(size == sizeof(struct shim_mount));
  322. struct shim_mount * mount = (struct shim_mount *) obj;
  323. struct shim_mount * new_mount = NULL;
  324. ptr_t off = GET_FROM_CP_MAP(obj);
  325. if (!off) {
  326. off = ADD_CP_OFFSET(sizeof(struct shim_mount));
  327. ADD_TO_CP_MAP(obj, off);
  328. if (!mount->cpdata &&
  329. mount->fs_ops &&
  330. mount->fs_ops->checkpoint) {
  331. void * cpdata = NULL;
  332. int bytes = mount->fs_ops->checkpoint(&cpdata, mount->data);
  333. if (bytes > 0) {
  334. mount->cpdata = cpdata;
  335. mount->cpsize = bytes;
  336. }
  337. }
  338. new_mount = (struct shim_mount *) (base + off);
  339. *new_mount = *mount;
  340. if (mount->cpdata) {
  341. struct shim_mem_entry * entry;
  342. DO_CP_SIZE(memory, mount->cpdata, mount->cpsize, &entry);
  343. new_mount->cpdata = NULL;
  344. entry->paddr = &new_mount->cpdata;
  345. }
  346. new_mount->data = NULL;
  347. new_mount->mount_point = NULL;
  348. new_mount->root = NULL;
  349. INIT_LIST_HEAD(&new_mount->list);
  350. DO_CP_IN_MEMBER(qstr, new_mount, path);
  351. DO_CP_IN_MEMBER(qstr, new_mount, uri);
  352. if (mount->mount_point)
  353. DO_CP_MEMBER(dentry, mount, new_mount, mount_point);
  354. if (mount->root)
  355. DO_CP_MEMBER(dentry, mount, new_mount, root);
  356. ADD_CP_FUNC_ENTRY(off);
  357. } else {
  358. new_mount = (struct shim_mount *) (base + off);
  359. }
  360. if (objp)
  361. *objp = (void *) new_mount;
  362. }
  363. END_CP_FUNC(mount)
  364. BEGIN_RS_FUNC(mount)
  365. {
  366. struct shim_mount * mount = (void *) (base + GET_CP_FUNC_ENTRY());
  367. CP_REBASE(mount->cpdata);
  368. CP_REBASE(mount->list);
  369. CP_REBASE(mount->mount_point);
  370. CP_REBASE(mount->root);
  371. struct shim_fs * fs = find_fs(mount->type);
  372. if (fs && fs->fs_ops && fs->fs_ops->migrate && mount->cpdata) {
  373. void * mount_data = NULL;
  374. if (fs->fs_ops->migrate(mount->cpdata, &mount_data) == 0)
  375. mount->data = mount_data;
  376. mount->cpdata = NULL;
  377. }
  378. mount->fs_ops = fs->fs_ops;
  379. mount->d_ops = fs->d_ops;
  380. list_add_tail(&mount->list, &mount_list);
  381. if (!qstrempty(&mount->path)) {
  382. DEBUG_RS("type=%s,uri=%s,path=%s", mount->type, qstrgetstr(&mount->uri),
  383. qstrgetstr(&mount->path));
  384. } else {
  385. DEBUG_RS("type=%s,uri=%s", mount->type, qstrgetstr(&mount->uri));
  386. }
  387. }
  388. END_RS_FUNC(mount)
  389. BEGIN_CP_FUNC(all_mounts)
  390. {
  391. struct shim_mount * mount;
  392. lock(mount_list_lock);
  393. list_for_each_entry(mount, &mount_list, list)
  394. DO_CP(mount, mount, NULL);
  395. unlock(mount_list_lock);
  396. /* add an empty entry to mark as migrated */
  397. ADD_CP_FUNC_ENTRY(0);
  398. }
  399. END_CP_FUNC(all_mounts)
  400. BEGIN_RS_FUNC(all_mounts)
  401. {
  402. /* to prevent file system from being mount again */
  403. mount_migrated = true;
  404. }
  405. END_RS_FUNC(all_mounts)
  406. const char * get_file_name (const char * path, size_t len)
  407. {
  408. const char * c = path + len - 1;
  409. while (c > path && *c != '/')
  410. c--;
  411. return *c == '/' ? c + 1 : c;
  412. }
  413. int get_abs_path (const char * cwd, const char * path, char * buf, int size)
  414. {
  415. int cnt = 0;
  416. char c, c1;
  417. const char * p = path;
  418. if (*p != '/') {
  419. cnt = strlen(cwd);
  420. while (cnt >= 0 && cwd[cnt - 1] == '/')
  421. cnt--;
  422. memcpy(buf, cwd, cnt);
  423. }
  424. for (c = '/' ; c ; c = c1, p++) {
  425. c1 = *p;
  426. if (c == '/') {
  427. if (c1 == 0)
  428. break;
  429. if (c1 == '/')
  430. continue;
  431. if (c1 == '.') {
  432. c1 = *(++p);
  433. if (c1 == 0)
  434. break;
  435. if (c1 == '/')
  436. continue;
  437. if (c1 == '.') {
  438. c1 = *(++p);
  439. if (c1 == 0) {
  440. while (cnt > 0 && buf[--cnt] != '/');
  441. break;
  442. }
  443. if (c1 == '/') {
  444. while (cnt > 0 && buf[--cnt] != '/');
  445. continue;
  446. }
  447. return -EINVAL;
  448. }
  449. if (cnt >= size-1)
  450. return -ENAMETOOLONG;
  451. buf[cnt++] = c;
  452. c = '.';
  453. }
  454. }
  455. if (cnt >= size-1)
  456. return -ENAMETOOLONG;
  457. buf[cnt++] = c;
  458. }
  459. if (cnt) {
  460. buf[cnt] = 0;
  461. } else {
  462. buf[0] = '/';
  463. buf[1] = 0;
  464. }
  465. return cnt;
  466. }
  467. int get_norm_path (const char * path, char * buf, int size)
  468. {
  469. int cnt = 0;
  470. char c, c1;
  471. const char * p = path;
  472. for (c = '/' ; c ; c = c1, p++) {
  473. c1 = *p;
  474. if (c == '/') {
  475. if (c1 == 0)
  476. break;
  477. if (c1 == '/')
  478. continue;
  479. if (c1 == '.') {
  480. c1 = *(++p);
  481. if (c1 == 0)
  482. break;
  483. if (c1 == '/')
  484. continue;
  485. if (c1 == '.') {
  486. c1 = *(++p);
  487. if (c1 != 0 && c1 != '/')
  488. return -EINVAL;
  489. if (cnt) {
  490. while (cnt > 0 && buf[--cnt] != '/');
  491. } else {
  492. if (cnt >= size-2)
  493. return -ENAMETOOLONG;
  494. buf[cnt++] = '.';
  495. buf[cnt++] = '.';
  496. }
  497. c = c1;
  498. continue;
  499. }
  500. if (cnt || c != '/') {
  501. if (cnt >= size-1)
  502. return -ENAMETOOLONG;
  503. buf[cnt++] = c;
  504. }
  505. c = '.';
  506. }
  507. }
  508. if (cnt || c != '/') {
  509. if (cnt >= size-1)
  510. return -ENAMETOOLONG;
  511. buf[cnt++] = c;
  512. }
  513. }
  514. buf[cnt] = 0;
  515. return cnt;
  516. }