fs.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. * fs.c
  17. *
  18. * This file contains codes for implementation of 'chroot' filesystem.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_fs.h>
  25. #include <shim_utils.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. #include <linux/stat.h>
  31. #include <linux/fcntl.h>
  32. #include <asm/fcntl.h>
  33. #include <asm/mman.h>
  34. #include <asm/unistd.h>
  35. #include <asm/prctl.h>
  36. #define URI_MAX_SIZE STR_SIZE
  37. #define TTY_FILE_MODE 0666
  38. #define FILE_BUFMAP_SIZE (PAL_CB(pagesize) * 4)
  39. #define FILE_BUF_SIZE (PAL_CB(pagesize))
  40. struct mount_data {
  41. int data_size;
  42. enum shim_file_type base_type;
  43. unsigned long ino_base;
  44. int root_uri_len;
  45. char root_uri[];
  46. };
  47. #define HANDLE_MOUNT_DATA(h) ((struct mount_data *) (h)->fs->data)
  48. #define DENTRY_MOUNT_DATA(d) ((struct mount_data *) (d)->fs->data)
  49. static int chroot_mount (const char * uri, const char * root,
  50. void ** mount_data)
  51. {
  52. enum shim_file_type type;
  53. if (strpartcmp_static(uri, "file:")) {
  54. type = FILE_UNKNOWN;
  55. uri += 5;
  56. } else if (strpartcmp_static(uri, "dev:")) {
  57. type = strpartcmp_static(uri + static_strlen("dev"), "tty") ?
  58. FILE_DEV : FILE_TTY;
  59. uri += 4;
  60. } else
  61. return -EINVAL;
  62. if (!(*uri))
  63. uri = ".";
  64. int uri_len = strlen(uri);
  65. int data_size = uri_len + 1 + sizeof(struct mount_data);
  66. struct mount_data * mdata = (struct mount_data *) malloc(data_size);
  67. mdata->data_size = data_size;
  68. mdata->base_type = type;
  69. mdata->ino_base = hash_path(uri, uri_len, NULL);
  70. mdata->root_uri_len = uri_len;
  71. memcpy(mdata->root_uri, uri, uri_len + 1);
  72. *mount_data = mdata;
  73. return 0;
  74. }
  75. static int chroot_unmount (void * mount_data)
  76. {
  77. free(mount_data);
  78. return 0;
  79. }
  80. static inline int concat_uri (char * buffer, int size, int type,
  81. const char * root, int root_len,
  82. const char * trim, int trim_len)
  83. {
  84. char * tmp = NULL;
  85. switch (type) {
  86. case FILE_UNKNOWN:
  87. case FILE_REGULAR:
  88. tmp = strcpy_static(buffer, "file:", size);
  89. break;
  90. case FILE_DIR:
  91. tmp = strcpy_static(buffer, "dir:", size);
  92. break;
  93. case FILE_DEV:
  94. case FILE_TTY:
  95. tmp = strcpy_static(buffer, "dev:", size);
  96. break;
  97. default:
  98. return -EINVAL;
  99. }
  100. if (!tmp || tmp + root_len + trim_len + 2 > buffer + size)
  101. return -ENAMETOOLONG;
  102. if (root_len) {
  103. memcpy(tmp, root, root_len + 1);
  104. tmp += root_len;
  105. }
  106. if (trim_len) {
  107. *(tmp++) = '/';
  108. memcpy(tmp, trim, trim_len + 1);
  109. tmp += trim_len;
  110. }
  111. return tmp - buffer;
  112. }
  113. /* simply just create data, sometimes it is individually called when the
  114. handle is not linked to a dentry */
  115. static struct shim_file_data * __create_data (void)
  116. {
  117. struct shim_file_data * data = malloc(sizeof(struct shim_file_data));
  118. if (!data)
  119. return NULL;
  120. memset(data, 0, sizeof(struct shim_file_data));
  121. create_lock(data->lock);
  122. return data;
  123. }
  124. static void __destroy_data (struct shim_file_data * data)
  125. {
  126. qstrfree(&data->host_uri);
  127. destroy_lock(data->lock);
  128. free(data);
  129. }
  130. static int make_uri (struct shim_dentry * dent)
  131. {
  132. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  133. assert(mdata);
  134. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  135. char * uri = __alloca(URI_MAX_SIZE);
  136. int len = concat_uri(uri, URI_MAX_SIZE, data->type,
  137. mdata->root_uri,
  138. mdata->root_uri_len,
  139. qstrgetstr(&dent->rel_path),
  140. dent->rel_path.len);
  141. if (len >= 0)
  142. qstrsetstr(&data->host_uri, uri, len);
  143. return len;
  144. }
  145. /* create a data in the dentry and compose it's uri. dent->lock needs to
  146. be held */
  147. static int create_data (struct shim_dentry * dent, const char * uri, int len)
  148. {
  149. if (dent->data)
  150. return 0;
  151. struct shim_file_data * data = __create_data();
  152. if (!data)
  153. return -ENOMEM;
  154. dent->data = data;
  155. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  156. assert(mdata);
  157. data->type = (dent->state & DENTRY_ISDIRECTORY) ?
  158. FILE_DIR : mdata->base_type;
  159. if (uri) {
  160. qstrsetstr(&data->host_uri, uri, len);
  161. } else {
  162. int ret = make_uri(dent);
  163. if (ret < 0)
  164. return ret;
  165. }
  166. atomic_set(&data->version, 0);
  167. return 0;
  168. }
  169. static int chroot_readdir (struct shim_dentry * dent,
  170. struct shim_dirent ** dirent);
  171. static int __query_attr (struct shim_dentry * dent,
  172. struct shim_file_data * data, PAL_HANDLE pal_handle)
  173. {
  174. PAL_STREAM_ATTR pal_attr;
  175. enum shim_file_type old_type = data->type;
  176. if (pal_handle ?
  177. !DkStreamAttributesQuerybyHandle(pal_handle, &pal_attr) :
  178. !DkStreamAttributesQuery(qstrgetstr(&data->host_uri), &pal_attr))
  179. return -PAL_ERRNO;
  180. /* need to correct the data type */
  181. if (data->type == FILE_UNKNOWN)
  182. switch (pal_attr.handle_type) {
  183. case pal_type_file: data->type = FILE_REGULAR; break;
  184. case pal_type_dir: data->type = FILE_DIR; break;
  185. case pal_type_dev: data->type = FILE_DEV; break;
  186. }
  187. data->mode = (pal_attr.readable ? S_IRUSR : 0) |
  188. (pal_attr.writeable ? S_IWUSR : 0) |
  189. (pal_attr.runnable ? S_IXUSR : 0);
  190. atomic_set(&data->size, pal_attr.pending_size);
  191. if (data->type == FILE_DIR) {
  192. int ret;
  193. /* Move up the uri update; need to convert manifest-level file:
  194. * directives to 'dir:' uris */
  195. if (old_type != FILE_DIR) {
  196. dent->state |= DENTRY_ISDIRECTORY;
  197. if ((ret = make_uri(dent)) < 0) {
  198. unlock(data->lock);
  199. return ret;
  200. }
  201. }
  202. /* DEP 3/18/17: If we have a directory, we need to find out how many
  203. * children it has by hand. */
  204. /* XXX: Keep coherent with rmdir/mkdir/creat, etc */
  205. struct shim_dirent *d, *dbuf = NULL;
  206. int nlink = 0;
  207. int rv = chroot_readdir(dent, &dbuf);
  208. if (rv != 0)
  209. return rv;
  210. if (dbuf) {
  211. for (d = dbuf; d; d = d->next)
  212. nlink++;
  213. free(dbuf);
  214. debug("Querying a directory; I count %d links.\n", nlink);
  215. } else
  216. nlink = 2; // Educated guess...
  217. data->nlink = nlink;
  218. } else {
  219. /* DEP 3/18/17: Right now, we don't support hard links,
  220. * so just return 1;
  221. */
  222. data->nlink = 1;
  223. }
  224. data->queried = true;
  225. return 0;
  226. }
  227. /* do not need any lock */
  228. static void chroot_update_ino (struct shim_dentry * dent)
  229. {
  230. if (dent->state & DENTRY_INO_UPDATED)
  231. return;
  232. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  233. unsigned long ino = mdata->ino_base;
  234. if (!qstrempty(&dent->rel_path))
  235. ino = rehash_path(mdata->ino_base, qstrgetstr(&dent->rel_path),
  236. dent->rel_path.len, NULL);
  237. dent->ino = ino;
  238. dent->state |= DENTRY_INO_UPDATED;
  239. }
  240. static inline int try_create_data (struct shim_dentry * dent,
  241. const char * uri, int len,
  242. struct shim_file_data ** dataptr)
  243. {
  244. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  245. if (!data) {
  246. lock(dent->lock);
  247. int ret = create_data(dent, uri, len);
  248. data = FILE_DENTRY_DATA(dent);
  249. unlock(dent->lock);
  250. if (ret < 0) {
  251. return ret;
  252. }
  253. }
  254. *dataptr = data;
  255. return 0;
  256. }
  257. static int query_dentry (struct shim_dentry * dent, PAL_HANDLE pal_handle,
  258. mode_t * mode, struct stat * stat)
  259. {
  260. int ret = 0;
  261. struct shim_file_data * data;
  262. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  263. return ret;
  264. lock(data->lock);
  265. if (!data->queried && (ret = __query_attr(dent, data, pal_handle)) < 0) {
  266. unlock(data->lock);
  267. return ret;
  268. }
  269. if (mode)
  270. *mode = data->mode;
  271. if (stat) {
  272. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  273. chroot_update_ino(dent);
  274. memset(stat, 0, sizeof(struct stat));
  275. stat->st_mode = (mode_t) data->mode;
  276. stat->st_dev = (dev_t) mdata->ino_base;
  277. stat->st_ino = (ino_t) dent->ino;
  278. stat->st_size = (off_t) atomic_read(&data->size);
  279. stat->st_atime = (time_t) data->atime;
  280. stat->st_mtime = (time_t) data->mtime;
  281. stat->st_ctime = (time_t) data->ctime;
  282. stat->st_nlink = data->nlink;
  283. switch (data->type) {
  284. case FILE_REGULAR:
  285. stat->st_mode |= S_IFREG;
  286. break;
  287. case FILE_DIR:
  288. stat->st_mode |= S_IFDIR;
  289. break;
  290. case FILE_DEV:
  291. case FILE_TTY:
  292. stat->st_mode |= S_IFCHR;
  293. break;
  294. default: break;
  295. }
  296. debug("Stat: Returning link cound %d\n", stat->st_nlink);
  297. }
  298. unlock(data->lock);
  299. return 0;
  300. }
  301. static int chroot_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  302. {
  303. if (!force)
  304. return -ESKIPPED;
  305. return query_dentry(dent, NULL, mode, NULL);
  306. }
  307. static int chroot_stat (struct shim_dentry * dent, struct stat * statbuf)
  308. {
  309. return query_dentry(dent, NULL, NULL, statbuf);
  310. }
  311. static int chroot_lookup (struct shim_dentry * dent, bool force)
  312. {
  313. if (!force)
  314. return -ESKIPPED;
  315. if (dent->fs && dent == dent->fs->root)
  316. return 0;
  317. return query_dentry(dent, NULL, NULL, NULL);
  318. }
  319. static int __chroot_open (struct shim_dentry * dent,
  320. const char * uri, int len, int flags, mode_t mode,
  321. struct shim_handle * hdl,
  322. struct shim_file_data * data)
  323. {
  324. int ret = 0;
  325. if (!uri) {
  326. uri = qstrgetstr(&data->host_uri);
  327. len = data->host_uri.len;
  328. }
  329. int version = atomic_read(&data->version);
  330. int oldmode = flags & O_ACCMODE;
  331. int accmode = oldmode;
  332. int creat = flags & PAL_CREAT_MASK;
  333. int option = flags & PAL_OPTION_MASK;
  334. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  335. && accmode == O_WRONLY)
  336. accmode = O_RDWR;
  337. PAL_HANDLE palhdl;
  338. if (hdl && hdl->pal_handle) {
  339. palhdl = hdl->pal_handle;
  340. } else {
  341. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  342. if (!palhdl) {
  343. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  344. accmode != oldmode)
  345. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  346. if (!palhdl)
  347. return -PAL_ERRNO;
  348. }
  349. }
  350. if (!data->queried) {
  351. lock(data->lock);
  352. ret = __query_attr(dent, data, palhdl);
  353. unlock(data->lock);
  354. }
  355. if (!hdl) {
  356. DkObjectClose(palhdl);
  357. return 0;
  358. }
  359. hdl->pal_handle = palhdl;
  360. hdl->info.file.type = data->type;
  361. hdl->info.file.version = version;
  362. hdl->info.file.size = atomic_read(&data->size);
  363. hdl->info.file.data = data;
  364. return ret;
  365. }
  366. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  367. int flags)
  368. {
  369. int ret = 0;
  370. struct shim_file_data * data;
  371. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  372. return ret;
  373. if ((ret = __chroot_open(dent, NULL, 0, flags, dent->mode, hdl, data)) < 0)
  374. return ret;
  375. struct shim_file_handle * file = &hdl->info.file;
  376. int size = atomic_read(&data->size);
  377. /* initialize hdl, does not need a lock because no one is sharing */
  378. hdl->type = TYPE_FILE;
  379. file->marker = (flags & O_APPEND) ? size : 0;
  380. file->size = size;
  381. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  382. hdl->flags = flags;
  383. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  384. qstrcopy(&hdl->uri, &data->host_uri);
  385. return 0;
  386. }
  387. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  388. struct shim_dentry * dent, int flags, mode_t mode)
  389. {
  390. int ret = 0;
  391. struct shim_file_data * data;
  392. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  393. return ret;
  394. if ((ret = __chroot_open(dent, NULL, 0, flags|O_CREAT|O_EXCL, mode, hdl,
  395. data)) < 0)
  396. return ret;
  397. if (!hdl)
  398. return 0;
  399. struct shim_file_handle * file = &hdl->info.file;
  400. int size = atomic_read(&data->size);
  401. /* initialize hdl, does not need a lock because no one is sharing */
  402. hdl->type = TYPE_FILE;
  403. file->marker = (flags & O_APPEND) ? size : 0;
  404. file->size = size;
  405. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  406. hdl->flags = flags;
  407. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  408. qstrcopy(&hdl->uri, &data->host_uri);
  409. /* Increment the parent's link count */
  410. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  411. if (parent_data) {
  412. lock(parent_data->lock);
  413. if (parent_data->queried)
  414. parent_data->nlink++;
  415. unlock(parent_data->lock);
  416. }
  417. return 0;
  418. }
  419. static int chroot_mkdir (struct shim_dentry * dir, struct shim_dentry * dent,
  420. mode_t mode)
  421. {
  422. int ret = 0;
  423. struct shim_file_data * data;
  424. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  425. return ret;
  426. if (data->type != FILE_DIR) {
  427. data->type = FILE_DIR;
  428. int ret = make_uri(dent);
  429. if (ret < 0)
  430. return ret;
  431. }
  432. ret = __chroot_open(dent, NULL, 0, O_CREAT|O_EXCL, mode, NULL, data);
  433. /* Increment the parent's link count */
  434. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  435. if (parent_data) {
  436. lock(parent_data->lock);
  437. if (parent_data->queried)
  438. parent_data->nlink++;
  439. unlock(parent_data->lock);
  440. }
  441. return ret;
  442. }
  443. #define NEED_RECREATE(hdl) (!FILE_HANDLE_DATA(hdl))
  444. static int chroot_recreate (struct shim_handle * hdl)
  445. {
  446. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  447. int ret = 0;
  448. /* quickly bail out if the data is created */
  449. if (data)
  450. return 0;
  451. const char * uri = qstrgetstr(&hdl->uri);
  452. int len = hdl->uri.len;
  453. if (hdl->dentry) {
  454. if ((ret = try_create_data(hdl->dentry, uri, len, &data)) < 0)
  455. return ret;
  456. } else {
  457. data = __create_data();
  458. if (!data)
  459. return -ENOMEM;
  460. qstrsetstr(&data->host_uri, uri, len);
  461. }
  462. /*
  463. * Chia-Che Tsai 8/24/2017:
  464. * when recreating a file handle after migration, the file should
  465. * not be created again.
  466. */
  467. return __chroot_open(hdl->dentry, uri, len, hdl->flags & ~(O_CREAT|O_EXCL),
  468. 0, hdl, data);
  469. }
  470. static inline bool check_version (struct shim_handle * hdl)
  471. {
  472. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  473. == hdl->info.file.version;
  474. }
  475. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  476. {
  477. int ret;
  478. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  479. return ret;
  480. if (!check_version(hdl) || !hdl->dentry) {
  481. struct shim_file_handle * file = &hdl->info.file;
  482. struct shim_dentry * dent = hdl->dentry;
  483. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  484. if (dent)
  485. chroot_update_ino(dent);
  486. if (stat) {
  487. memset(stat, 0, sizeof(struct stat));
  488. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  489. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  490. stat->st_size = file->size;
  491. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  492. }
  493. return 0;
  494. }
  495. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  496. }
  497. static int chroot_flush (struct shim_handle * hdl)
  498. {
  499. struct shim_file_handle * file = &hdl->info.file;
  500. if (file->buf_type == FILEBUF_MAP) {
  501. lock(hdl->lock);
  502. void * mapbuf = file->mapbuf;
  503. int mapsize = file->mapsize;
  504. file->mapoffset = 0;
  505. file->mapbuf = NULL;
  506. unlock(hdl->lock);
  507. if (mapbuf) {
  508. DkStreamUnmap(mapbuf, mapsize);
  509. int flags = VMA_INTERNAL;
  510. bkeep_munmap(mapbuf, mapsize, &flags);
  511. }
  512. }
  513. return 0;
  514. }
  515. static inline int __map_buffer (struct shim_handle * hdl, int size)
  516. {
  517. struct shim_file_handle * file = &hdl->info.file;
  518. if (file->mapbuf) {
  519. if (file->marker >= file->mapoffset &&
  520. file->marker + size <= file->mapoffset + file->mapsize)
  521. return 0;
  522. DkStreamUnmap(file->mapbuf, file->mapsize);
  523. int flags = VMA_INTERNAL;
  524. bkeep_munmap(file->mapbuf, file->mapsize, &flags);
  525. file->mapbuf = NULL;
  526. file->mapoffset = 0;
  527. }
  528. /* second, reallocate the buffer */
  529. int bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  530. int prot = PAL_PROT_READ;
  531. unsigned long mapoff = file->marker & ~(bufsize - 1);
  532. unsigned long maplen = bufsize;
  533. if (hdl->acc_mode & MAY_WRITE)
  534. prot |= PAL_PROT_WRITE;
  535. while (mapoff + maplen < file->marker + size)
  536. maplen *= 2;
  537. void * mapbuf =
  538. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  539. if (!mapbuf)
  540. return -PAL_ERRNO;
  541. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  542. hdl, mapoff, NULL);
  543. file->mapbuf = mapbuf;
  544. file->mapoffset = mapoff;
  545. file->mapsize = maplen;
  546. return 0;
  547. }
  548. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  549. {
  550. struct shim_file_handle * file = &hdl->info.file;
  551. int ret = 0;
  552. lock(hdl->lock);
  553. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  554. unsigned int size = atomic_read(&data->size);
  555. if (check_version(hdl) &&
  556. file->size < size)
  557. file->size = size;
  558. int marker = file->marker;
  559. if (marker >= file->size) {
  560. count = 0;
  561. goto out;
  562. }
  563. if ((ret = __map_buffer(hdl, count)) < 0) {
  564. unlock(hdl->lock);
  565. return ret;
  566. }
  567. if (marker + count > file->size)
  568. count = file->size - marker;
  569. if (count) {
  570. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  571. file->marker = marker + count;
  572. }
  573. out:
  574. unlock(hdl->lock);
  575. return count;
  576. }
  577. static int map_write (struct shim_handle * hdl, const void * buf,
  578. size_t count)
  579. {
  580. struct shim_file_handle * file = &hdl->info.file;
  581. int ret = 0;
  582. lock(hdl->lock);
  583. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  584. int marker = file->marker;
  585. if (file->marker + count > file->size) {
  586. file->size = file->marker + count;
  587. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  588. if (!ret) {
  589. ret = -PAL_ERRNO;
  590. goto out;
  591. }
  592. if (ret < count)
  593. file->size -= count - ret;
  594. if (check_version(hdl)) {
  595. int size;
  596. do {
  597. if ((size = atomic_read(&data->size)) >= file->size) {
  598. file->size = size;
  599. break;
  600. }
  601. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  602. }
  603. file->marker = marker + ret;
  604. goto out;
  605. }
  606. if ((ret = __map_buffer(hdl, count)) < 0)
  607. goto out;
  608. if (count) {
  609. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  610. file->marker = marker + count;
  611. }
  612. ret = count;
  613. out:
  614. unlock(hdl->lock);
  615. return ret;
  616. }
  617. static int chroot_read (struct shim_handle * hdl, void * buf,
  618. size_t count)
  619. {
  620. int ret = 0;
  621. if (count == 0)
  622. goto out;
  623. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  624. goto out;
  625. }
  626. struct shim_file_handle * file = &hdl->info.file;
  627. if (file->buf_type == FILEBUF_MAP) {
  628. ret = map_read(hdl, buf, count);
  629. if (ret != -EACCES)
  630. goto out;
  631. lock(hdl->lock);
  632. file->buf_type = FILEBUF_NONE;
  633. } else {
  634. lock(hdl->lock);
  635. }
  636. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  637. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  638. if (ret > 0)
  639. file->marker += ret;
  640. unlock(hdl->lock);
  641. out:
  642. return ret;
  643. }
  644. static int chroot_write (struct shim_handle * hdl, const void * buf,
  645. size_t count)
  646. {
  647. int ret;
  648. if (count == 0)
  649. return 0;
  650. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  651. goto out;
  652. }
  653. struct shim_file_handle * file = &hdl->info.file;
  654. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  655. ret = map_write(hdl, buf, count);
  656. if (ret != -EACCES)
  657. goto out;
  658. lock(hdl->lock);
  659. file->buf_type = FILEBUF_NONE;
  660. } else {
  661. lock(hdl->lock);
  662. }
  663. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  664. -PAL_ERRNO;
  665. if (ret > 0)
  666. file->marker += ret;
  667. unlock(hdl->lock);
  668. out:
  669. return ret;
  670. }
  671. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  672. int prot, int flags, off_t offset)
  673. {
  674. int ret;
  675. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  676. return ret;
  677. int pal_prot = PAL_PROT(prot, flags);
  678. #if MAP_FILE == 0
  679. if (flags & MAP_ANONYMOUS)
  680. #else
  681. if (!(flags & MAP_FILE))
  682. #endif
  683. return -EINVAL;
  684. void * alloc_addr =
  685. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  686. if (!alloc_addr)
  687. return -PAL_ERRNO;
  688. *addr = alloc_addr;
  689. return 0;
  690. }
  691. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  692. {
  693. int ret = -EINVAL;
  694. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  695. return ret;
  696. struct shim_file_handle * file = &hdl->info.file;
  697. lock(hdl->lock);
  698. int marker = file->marker;
  699. int size = file->size;
  700. if (check_version(hdl)) {
  701. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  702. if (data->type != FILE_REGULAR) {
  703. ret = -ESPIPE;
  704. goto out;
  705. }
  706. }
  707. switch (wence) {
  708. case SEEK_SET:
  709. if (offset < 0)
  710. goto out;
  711. marker = offset;
  712. break;
  713. case SEEK_CUR:
  714. marker += offset;
  715. break;
  716. case SEEK_END:
  717. marker = size + offset;
  718. break;
  719. }
  720. ret = file->marker = marker;
  721. out:
  722. unlock(hdl->lock);
  723. return ret;
  724. }
  725. static int chroot_truncate (struct shim_handle * hdl, uint64_t len)
  726. {
  727. uint64_t ret = 0;
  728. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  729. return ret;
  730. if (!(hdl->acc_mode & MAY_WRITE))
  731. return -EINVAL;
  732. struct shim_file_handle * file = &hdl->info.file;
  733. lock(hdl->lock);
  734. file->size = len;
  735. if (check_version(hdl)) {
  736. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  737. atomic_set(&data->size, len);
  738. }
  739. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len) {
  740. goto out;
  741. }
  742. // DEP 10/25/16: Truncate returns 0 on success, not the length
  743. ret = 0;
  744. if (file->marker > len)
  745. file->marker = len;
  746. out:
  747. unlock(hdl->lock);
  748. return ret;
  749. }
  750. static int chroot_dput (struct shim_dentry * dent)
  751. {
  752. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  753. if (data) {
  754. __destroy_data(data);
  755. dent->data = NULL;
  756. }
  757. return 0;
  758. }
  759. #define DEFAULT_DBUF_SIZE 1024
  760. static int chroot_readdir (struct shim_dentry * dent,
  761. struct shim_dirent ** dirent)
  762. {
  763. int ret;
  764. struct shim_file_data * data;
  765. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  766. return ret;
  767. chroot_update_ino(dent);
  768. assert(strpartcmp_static(qstrgetstr(&data->host_uri), "dir:"));
  769. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  770. PAL_ACCESS_RDONLY, 0, 0, 0);
  771. if (!pal_hdl)
  772. return -PAL_ERRNO;
  773. int buf_size = 0, new_size = MAX_PATH;
  774. int bytes;
  775. char * buf = NULL, * new_buf;
  776. int dbufsize = MAX_PATH;
  777. struct shim_dirent * dbuf = malloc(dbufsize);
  778. struct shim_dirent * d = dbuf, ** last = NULL;
  779. retry:
  780. new_buf = __alloca(new_size);
  781. if (buf)
  782. memcpy(new_buf, buf, buf_size);
  783. buf_size = new_size;
  784. buf = new_buf;
  785. while (1) {
  786. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  787. if (bytes == 0) {
  788. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  789. break;
  790. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  791. new_size = buf_size * 2;
  792. goto retry;
  793. }
  794. ret = -PAL_ERRNO;
  795. goto out;
  796. }
  797. char * b = buf, * next_b;
  798. int blen;
  799. while (b < buf + bytes) {
  800. blen = strlen(b);
  801. next_b = b + blen + 1;
  802. bool isdir = false;
  803. if (b[blen - 1] == '/') {
  804. isdir = true;
  805. b[blen - 1] = 0;
  806. blen--;
  807. }
  808. int dsize = sizeof(struct shim_dirent) + blen + 1;
  809. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  810. int newsize = dbufsize * 2;
  811. while ((void *) d + dsize > (void *) dbuf + newsize)
  812. newsize *= 2;
  813. struct shim_dirent * new_dbuf = malloc(newsize);
  814. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  815. struct shim_dirent * d1 = new_dbuf;
  816. struct shim_dirent * d2 = dbuf;
  817. while (d2 != d) {
  818. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  819. d1 = d1->next;
  820. d2 = d2->next;
  821. }
  822. free(dbuf);
  823. dbuf = new_dbuf;
  824. d = d1;
  825. dbufsize = newsize;
  826. }
  827. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  828. d->next = (void *) (d + 1) + blen + 1;
  829. d->ino = hash;
  830. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  831. memcpy(d->name, b, blen + 1);
  832. b = next_b;
  833. last = &d->next;
  834. d = d->next;
  835. }
  836. }
  837. if (!last) {
  838. free(dbuf);
  839. goto out;
  840. }
  841. *last = NULL;
  842. *dirent = dbuf;
  843. out:
  844. DkObjectClose(pal_hdl);
  845. return ret;
  846. }
  847. static int chroot_checkout (struct shim_handle * hdl)
  848. {
  849. if (hdl->fs == &chroot_builtin_fs)
  850. hdl->fs = NULL;
  851. if (hdl->type == TYPE_FILE) {
  852. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  853. if (data)
  854. hdl->info.file.data = NULL;
  855. }
  856. if (hdl->pal_handle) {
  857. /*
  858. * Chia-Che 8/24/2017:
  859. * if the file still exists in the host, no need to send
  860. * the handle over RPC; otherwise, send it.
  861. */
  862. PAL_STREAM_ATTR attr;
  863. if (DkStreamAttributesQuery(qstrgetstr(&hdl->uri), &attr))
  864. hdl->pal_handle = NULL;
  865. }
  866. hdl->info.file.mapsize = 0;
  867. hdl->info.file.mapoffset = 0;
  868. hdl->info.file.mapbuf = NULL;
  869. return 0;
  870. }
  871. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  872. {
  873. struct mount_data * mdata = mount_data;
  874. *checkpoint = mount_data;
  875. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  876. }
  877. static int chroot_migrate (void * checkpoint, void ** mount_data)
  878. {
  879. struct mount_data * mdata = checkpoint;
  880. int alloc_len = mdata->root_uri_len +
  881. sizeof(struct mount_data) + 1;
  882. void * new_data = malloc(alloc_len);
  883. memcpy(new_data, mdata, alloc_len);
  884. *mount_data = new_data;
  885. return 0;
  886. }
  887. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  888. {
  889. int ret;
  890. struct shim_file_data * data;
  891. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  892. return ret;
  893. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  894. if (!pal_hdl)
  895. return -PAL_ERRNO;
  896. DkStreamDelete(pal_hdl, 0);
  897. DkObjectClose(pal_hdl);
  898. dent->mode = NO_MODE;
  899. data->mode = 0;
  900. atomic_inc(&data->version);
  901. atomic_set(&data->size, 0);
  902. /* Drop the parent's link count */
  903. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  904. if (parent_data) {
  905. lock(parent_data->lock);
  906. if (parent_data->queried)
  907. parent_data->nlink--;
  908. unlock(parent_data->lock);
  909. }
  910. return 0;
  911. }
  912. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  913. {
  914. int ret;
  915. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  916. return ret;
  917. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  918. size_t size = atomic_read(&data->size);
  919. if (poll_type == FS_POLL_SZ)
  920. return size;
  921. lock(hdl->lock);
  922. struct shim_file_handle * file = &hdl->info.file;
  923. if (check_version(hdl) &&
  924. file->size < size)
  925. file->size = size;
  926. int marker = file->marker;
  927. if (file->buf_type == FILEBUF_MAP) {
  928. ret = poll_type & FS_POLL_WR;
  929. if ((poll_type & FS_POLL_RD) && file->size > marker)
  930. ret |= FS_POLL_RD;
  931. goto out;
  932. }
  933. ret = -EAGAIN;
  934. out:
  935. unlock(hdl->lock);
  936. return ret;
  937. }
  938. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  939. {
  940. int ret;
  941. struct shim_file_data * old_data;
  942. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  943. return ret;
  944. struct shim_file_data * new_data;
  945. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  946. return ret;
  947. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  948. 0, 0, 0, 0);
  949. if (!pal_hdl)
  950. return -PAL_ERRNO;
  951. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  952. DkObjectClose(pal_hdl);
  953. return -PAL_ERRNO;
  954. }
  955. new->mode = new_data->mode = old_data->mode;
  956. old->mode = NO_MODE;
  957. old_data->mode = 0;
  958. DkObjectClose(pal_hdl);
  959. atomic_inc(&old_data->version);
  960. atomic_set(&old_data->size, 0);
  961. atomic_inc(&new_data->version);
  962. return 0;
  963. }
  964. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  965. {
  966. int ret;
  967. struct shim_file_data * data;
  968. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  969. return ret;
  970. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  971. if (!pal_hdl)
  972. return -PAL_ERRNO;
  973. PAL_STREAM_ATTR attr = { .share_flags = mode };
  974. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  975. DkObjectClose(pal_hdl);
  976. return -PAL_ERRNO;
  977. }
  978. DkObjectClose(pal_hdl);
  979. dent->mode = data->mode = mode;
  980. return 0;
  981. }
  982. struct shim_fs_ops chroot_fs_ops = {
  983. .mount = &chroot_mount,
  984. .unmount = &chroot_unmount,
  985. .flush = &chroot_flush,
  986. .close = &chroot_flush,
  987. .read = &chroot_read,
  988. .write = &chroot_write,
  989. .mmap = &chroot_mmap,
  990. .seek = &chroot_seek,
  991. .hstat = &chroot_hstat,
  992. .truncate = &chroot_truncate,
  993. .checkout = &chroot_checkout,
  994. .checkpoint = &chroot_checkpoint,
  995. .migrate = &chroot_migrate,
  996. .poll = &chroot_poll,
  997. };
  998. struct shim_d_ops chroot_d_ops = {
  999. .open = &chroot_open,
  1000. .mode = &chroot_mode,
  1001. .lookup = &chroot_lookup,
  1002. .creat = &chroot_creat,
  1003. .mkdir = &chroot_mkdir,
  1004. .stat = &chroot_stat,
  1005. .dput = &chroot_dput,
  1006. .readdir = &chroot_readdir,
  1007. .unlink = &chroot_unlink,
  1008. .rename = &chroot_rename,
  1009. .chmod = &chroot_chmod,
  1010. };
  1011. struct mount_data chroot_data = { .root_uri_len = 5,
  1012. .root_uri = "file:", };
  1013. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  1014. .fs_ops = &chroot_fs_ops,
  1015. .d_ops = &chroot_d_ops,
  1016. .data = &chroot_data, };