fs.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. return query_dentry(dent, NULL, NULL, NULL);
  316. }
  317. static int __chroot_open (struct shim_dentry * dent,
  318. const char * uri, int len, int flags, mode_t mode,
  319. struct shim_handle * hdl,
  320. struct shim_file_data * data)
  321. {
  322. int ret = 0;
  323. if (!uri) {
  324. uri = qstrgetstr(&data->host_uri);
  325. len = data->host_uri.len;
  326. }
  327. int version = atomic_read(&data->version);
  328. int oldmode = flags & O_ACCMODE;
  329. int accmode = oldmode;
  330. int creat = flags & PAL_CREAT_MASK;
  331. int option = flags & PAL_OPTION_MASK;
  332. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  333. && accmode == O_WRONLY)
  334. accmode = O_RDWR;
  335. PAL_HANDLE palhdl;
  336. if (hdl && hdl->pal_handle) {
  337. palhdl = hdl->pal_handle;
  338. } else {
  339. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  340. if (!palhdl) {
  341. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  342. accmode != oldmode)
  343. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  344. if (!palhdl)
  345. return -PAL_ERRNO;
  346. }
  347. }
  348. if (!data->queried) {
  349. lock(data->lock);
  350. ret = __query_attr(dent, data, palhdl);
  351. unlock(data->lock);
  352. }
  353. if (!hdl) {
  354. DkObjectClose(palhdl);
  355. return 0;
  356. }
  357. hdl->pal_handle = palhdl;
  358. hdl->info.file.type = data->type;
  359. hdl->info.file.version = version;
  360. hdl->info.file.size = atomic_read(&data->size);
  361. hdl->info.file.data = data;
  362. return ret;
  363. }
  364. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  365. int flags)
  366. {
  367. int ret = 0;
  368. struct shim_file_data * data;
  369. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  370. return ret;
  371. if ((ret = __chroot_open(dent, NULL, 0, flags, dent->mode, hdl, data)) < 0)
  372. return ret;
  373. struct shim_file_handle * file = &hdl->info.file;
  374. int size = atomic_read(&data->size);
  375. /* initialize hdl, does not need a lock because no one is sharing */
  376. hdl->type = TYPE_FILE;
  377. file->marker = (flags & O_APPEND) ? size : 0;
  378. file->size = size;
  379. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  380. hdl->flags = flags;
  381. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  382. qstrcopy(&hdl->uri, &data->host_uri);
  383. return 0;
  384. }
  385. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  386. struct shim_dentry * dent, int flags, mode_t mode)
  387. {
  388. int ret = 0;
  389. struct shim_file_data * data;
  390. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  391. return ret;
  392. if ((ret = __chroot_open(dent, NULL, 0, flags|O_CREAT|O_EXCL, mode, hdl,
  393. data)) < 0)
  394. return ret;
  395. if (!hdl)
  396. return 0;
  397. struct shim_file_handle * file = &hdl->info.file;
  398. int size = atomic_read(&data->size);
  399. /* initialize hdl, does not need a lock because no one is sharing */
  400. hdl->type = TYPE_FILE;
  401. file->marker = (flags & O_APPEND) ? size : 0;
  402. file->size = size;
  403. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  404. hdl->flags = flags;
  405. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  406. qstrcopy(&hdl->uri, &data->host_uri);
  407. /* Increment the parent's link count */
  408. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  409. if (parent_data) {
  410. lock(parent_data->lock);
  411. if (parent_data->queried)
  412. parent_data->nlink++;
  413. unlock(parent_data->lock);
  414. }
  415. return 0;
  416. }
  417. static int chroot_mkdir (struct shim_dentry * dir, struct shim_dentry * dent,
  418. mode_t mode)
  419. {
  420. int ret = 0;
  421. struct shim_file_data * data;
  422. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  423. return ret;
  424. if (data->type != FILE_DIR) {
  425. data->type = FILE_DIR;
  426. int ret = make_uri(dent);
  427. if (ret < 0)
  428. return ret;
  429. }
  430. ret = __chroot_open(dent, NULL, 0, O_CREAT|O_EXCL, mode, NULL, data);
  431. /* Increment the parent's link count */
  432. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  433. if (parent_data) {
  434. lock(parent_data->lock);
  435. if (parent_data->queried)
  436. parent_data->nlink++;
  437. unlock(parent_data->lock);
  438. }
  439. return ret;
  440. }
  441. #define NEED_RECREATE(hdl) (!FILE_HANDLE_DATA(hdl))
  442. static int chroot_recreate (struct shim_handle * hdl)
  443. {
  444. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  445. int ret = 0;
  446. /* quickly bail out if the data is created */
  447. if (data)
  448. return 0;
  449. const char * uri = qstrgetstr(&hdl->uri);
  450. int len = hdl->uri.len;
  451. if (hdl->dentry) {
  452. if ((ret = try_create_data(hdl->dentry, uri, len, &data)) < 0)
  453. return ret;
  454. } else {
  455. data = __create_data();
  456. if (!data)
  457. return -ENOMEM;
  458. qstrsetstr(&data->host_uri, uri, len);
  459. }
  460. /*
  461. * Chia-Che Tsai 8/24/2017:
  462. * when recreating a file handle after migration, the file should
  463. * not be created again.
  464. */
  465. return __chroot_open(hdl->dentry, uri, len, hdl->flags & ~(O_CREAT|O_EXCL),
  466. 0, hdl, data);
  467. }
  468. static inline bool check_version (struct shim_handle * hdl)
  469. {
  470. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  471. == hdl->info.file.version;
  472. }
  473. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  474. {
  475. int ret;
  476. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  477. return ret;
  478. if (!check_version(hdl) || !hdl->dentry) {
  479. struct shim_file_handle * file = &hdl->info.file;
  480. struct shim_dentry * dent = hdl->dentry;
  481. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  482. if (dent)
  483. chroot_update_ino(dent);
  484. if (stat) {
  485. memset(stat, 0, sizeof(struct stat));
  486. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  487. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  488. stat->st_size = file->size;
  489. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  490. }
  491. return 0;
  492. }
  493. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  494. }
  495. static int chroot_flush (struct shim_handle * hdl)
  496. {
  497. struct shim_file_handle * file = &hdl->info.file;
  498. if (file->buf_type == FILEBUF_MAP) {
  499. lock(hdl->lock);
  500. void * mapbuf = file->mapbuf;
  501. int mapsize = file->mapsize;
  502. file->mapoffset = 0;
  503. file->mapbuf = NULL;
  504. unlock(hdl->lock);
  505. if (mapbuf) {
  506. DkStreamUnmap(mapbuf, mapsize);
  507. int flags = VMA_INTERNAL;
  508. bkeep_munmap(mapbuf, mapsize, &flags);
  509. }
  510. }
  511. return 0;
  512. }
  513. static inline int __map_buffer (struct shim_handle * hdl, int size)
  514. {
  515. struct shim_file_handle * file = &hdl->info.file;
  516. if (file->mapbuf) {
  517. if (file->marker >= file->mapoffset &&
  518. file->marker + size <= file->mapoffset + file->mapsize)
  519. return 0;
  520. DkStreamUnmap(file->mapbuf, file->mapsize);
  521. int flags = VMA_INTERNAL;
  522. bkeep_munmap(file->mapbuf, file->mapsize, &flags);
  523. file->mapbuf = NULL;
  524. file->mapoffset = 0;
  525. }
  526. /* second, reallocate the buffer */
  527. int bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  528. int prot = PAL_PROT_READ;
  529. unsigned long mapoff = file->marker & ~(bufsize - 1);
  530. unsigned long maplen = bufsize;
  531. if (hdl->acc_mode & MAY_WRITE)
  532. prot |= PAL_PROT_WRITE;
  533. while (mapoff + maplen < file->marker + size)
  534. maplen *= 2;
  535. void * mapbuf =
  536. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  537. if (!mapbuf)
  538. return -PAL_ERRNO;
  539. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  540. hdl, mapoff, NULL);
  541. file->mapbuf = mapbuf;
  542. file->mapoffset = mapoff;
  543. file->mapsize = maplen;
  544. return 0;
  545. }
  546. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  547. {
  548. struct shim_file_handle * file = &hdl->info.file;
  549. int ret = 0;
  550. lock(hdl->lock);
  551. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  552. unsigned int size = atomic_read(&data->size);
  553. if (check_version(hdl) &&
  554. file->size < size)
  555. file->size = size;
  556. int marker = file->marker;
  557. if (marker >= file->size) {
  558. count = 0;
  559. goto out;
  560. }
  561. if ((ret = __map_buffer(hdl, count)) < 0) {
  562. unlock(hdl->lock);
  563. return ret;
  564. }
  565. if (marker + count > file->size)
  566. count = file->size - marker;
  567. if (count) {
  568. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  569. file->marker = marker + count;
  570. }
  571. out:
  572. unlock(hdl->lock);
  573. return count;
  574. }
  575. static int map_write (struct shim_handle * hdl, const void * buf,
  576. size_t count)
  577. {
  578. struct shim_file_handle * file = &hdl->info.file;
  579. int ret = 0;
  580. lock(hdl->lock);
  581. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  582. int marker = file->marker;
  583. if (file->marker + count > file->size) {
  584. file->size = file->marker + count;
  585. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  586. if (!ret) {
  587. ret = -PAL_ERRNO;
  588. goto out;
  589. }
  590. if (ret < count)
  591. file->size -= count - ret;
  592. if (check_version(hdl)) {
  593. int size;
  594. do {
  595. if ((size = atomic_read(&data->size)) >= file->size) {
  596. file->size = size;
  597. break;
  598. }
  599. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  600. }
  601. file->marker = marker + ret;
  602. goto out;
  603. }
  604. if ((ret = __map_buffer(hdl, count)) < 0)
  605. goto out;
  606. if (count) {
  607. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  608. file->marker = marker + count;
  609. }
  610. ret = count;
  611. out:
  612. unlock(hdl->lock);
  613. return ret;
  614. }
  615. static int chroot_read (struct shim_handle * hdl, void * buf,
  616. size_t count)
  617. {
  618. int ret = 0;
  619. if (count == 0)
  620. goto out;
  621. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  622. goto out;
  623. }
  624. struct shim_file_handle * file = &hdl->info.file;
  625. if (file->buf_type == FILEBUF_MAP) {
  626. ret = map_read(hdl, buf, count);
  627. if (ret != -EACCES)
  628. goto out;
  629. lock(hdl->lock);
  630. file->buf_type = FILEBUF_NONE;
  631. } else {
  632. lock(hdl->lock);
  633. }
  634. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  635. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  636. if (ret > 0)
  637. file->marker += ret;
  638. unlock(hdl->lock);
  639. out:
  640. return ret;
  641. }
  642. static int chroot_write (struct shim_handle * hdl, const void * buf,
  643. size_t count)
  644. {
  645. int ret;
  646. if (count == 0)
  647. return 0;
  648. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  649. goto out;
  650. }
  651. struct shim_file_handle * file = &hdl->info.file;
  652. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  653. ret = map_write(hdl, buf, count);
  654. if (ret != -EACCES)
  655. goto out;
  656. lock(hdl->lock);
  657. file->buf_type = FILEBUF_NONE;
  658. } else {
  659. lock(hdl->lock);
  660. }
  661. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  662. -PAL_ERRNO;
  663. if (ret > 0)
  664. file->marker += ret;
  665. unlock(hdl->lock);
  666. out:
  667. return ret;
  668. }
  669. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  670. int prot, int flags, off_t offset)
  671. {
  672. int ret;
  673. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  674. return ret;
  675. int pal_prot = PAL_PROT(prot, flags);
  676. #if MAP_FILE == 0
  677. if (flags & MAP_ANONYMOUS)
  678. #else
  679. if (!(flags & MAP_FILE))
  680. #endif
  681. return -EINVAL;
  682. void * alloc_addr =
  683. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  684. if (!alloc_addr)
  685. return -PAL_ERRNO;
  686. *addr = alloc_addr;
  687. return 0;
  688. }
  689. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  690. {
  691. int ret = -EINVAL;
  692. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  693. return ret;
  694. struct shim_file_handle * file = &hdl->info.file;
  695. lock(hdl->lock);
  696. int marker = file->marker;
  697. int size = file->size;
  698. if (check_version(hdl)) {
  699. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  700. if (data->type != FILE_REGULAR) {
  701. ret = -ESPIPE;
  702. goto out;
  703. }
  704. }
  705. switch (wence) {
  706. case SEEK_SET:
  707. if (offset < 0)
  708. goto out;
  709. marker = offset;
  710. break;
  711. case SEEK_CUR:
  712. marker += offset;
  713. break;
  714. case SEEK_END:
  715. marker = size + offset;
  716. break;
  717. }
  718. ret = file->marker = marker;
  719. out:
  720. unlock(hdl->lock);
  721. return ret;
  722. }
  723. static int chroot_truncate (struct shim_handle * hdl, uint64_t len)
  724. {
  725. uint64_t ret = 0;
  726. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  727. return ret;
  728. if (!(hdl->acc_mode & MAY_WRITE))
  729. return -EINVAL;
  730. struct shim_file_handle * file = &hdl->info.file;
  731. lock(hdl->lock);
  732. file->size = len;
  733. if (check_version(hdl)) {
  734. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  735. atomic_set(&data->size, len);
  736. }
  737. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len) {
  738. goto out;
  739. }
  740. // DEP 10/25/16: Truncate returns 0 on success, not the length
  741. ret = 0;
  742. if (file->marker > len)
  743. file->marker = len;
  744. out:
  745. unlock(hdl->lock);
  746. return ret;
  747. }
  748. static int chroot_dput (struct shim_dentry * dent)
  749. {
  750. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  751. if (data) {
  752. __destroy_data(data);
  753. dent->data = NULL;
  754. }
  755. return 0;
  756. }
  757. #define DEFAULT_DBUF_SIZE 1024
  758. static int chroot_readdir (struct shim_dentry * dent,
  759. struct shim_dirent ** dirent)
  760. {
  761. int ret;
  762. struct shim_file_data * data;
  763. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  764. return ret;
  765. chroot_update_ino(dent);
  766. assert(strpartcmp_static(qstrgetstr(&data->host_uri), "dir:"));
  767. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  768. PAL_ACCESS_RDONLY, 0, 0, 0);
  769. if (!pal_hdl)
  770. return -PAL_ERRNO;
  771. int buf_size = 0, new_size = MAX_PATH;
  772. int bytes;
  773. char * buf = NULL, * new_buf;
  774. int dbufsize = MAX_PATH;
  775. struct shim_dirent * dbuf = malloc(dbufsize);
  776. struct shim_dirent * d = dbuf, ** last = NULL;
  777. retry:
  778. new_buf = __alloca(new_size);
  779. if (buf)
  780. memcpy(new_buf, buf, buf_size);
  781. buf_size = new_size;
  782. buf = new_buf;
  783. while (1) {
  784. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  785. if (bytes == 0) {
  786. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  787. break;
  788. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  789. new_size = buf_size * 2;
  790. goto retry;
  791. }
  792. ret = -PAL_ERRNO;
  793. goto out;
  794. }
  795. char * b = buf, * next_b;
  796. int blen;
  797. while (b < buf + bytes) {
  798. blen = strlen(b);
  799. next_b = b + blen + 1;
  800. bool isdir = false;
  801. if (b[blen - 1] == '/') {
  802. isdir = true;
  803. b[blen - 1] = 0;
  804. blen--;
  805. }
  806. int dsize = sizeof(struct shim_dirent) + blen + 1;
  807. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  808. int newsize = dbufsize * 2;
  809. while ((void *) d + dsize > (void *) dbuf + newsize)
  810. newsize *= 2;
  811. struct shim_dirent * new_dbuf = malloc(newsize);
  812. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  813. struct shim_dirent * d1 = new_dbuf;
  814. struct shim_dirent * d2 = dbuf;
  815. while (d2 != d) {
  816. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  817. d1 = d1->next;
  818. d2 = d2->next;
  819. }
  820. free(dbuf);
  821. dbuf = new_dbuf;
  822. d = d1;
  823. dbufsize = newsize;
  824. }
  825. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  826. d->next = (void *) (d + 1) + blen + 1;
  827. d->ino = hash;
  828. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  829. memcpy(d->name, b, blen + 1);
  830. b = next_b;
  831. last = &d->next;
  832. d = d->next;
  833. }
  834. }
  835. if (!last) {
  836. free(dbuf);
  837. goto out;
  838. }
  839. *last = NULL;
  840. *dirent = dbuf;
  841. out:
  842. DkObjectClose(pal_hdl);
  843. return ret;
  844. }
  845. static int chroot_checkout (struct shim_handle * hdl)
  846. {
  847. if (hdl->fs == &chroot_builtin_fs)
  848. hdl->fs = NULL;
  849. if (hdl->type == TYPE_FILE) {
  850. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  851. if (data)
  852. hdl->info.file.data = NULL;
  853. }
  854. if (hdl->pal_handle) {
  855. /*
  856. * Chia-Che 8/24/2017:
  857. * if the file still exists in the host, no need to send
  858. * the handle over RPC; otherwise, send it.
  859. */
  860. PAL_STREAM_ATTR attr;
  861. if (DkStreamAttributesQuery(qstrgetstr(&hdl->uri), &attr))
  862. hdl->pal_handle = NULL;
  863. }
  864. hdl->info.file.mapsize = 0;
  865. hdl->info.file.mapoffset = 0;
  866. hdl->info.file.mapbuf = NULL;
  867. return 0;
  868. }
  869. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  870. {
  871. struct mount_data * mdata = mount_data;
  872. *checkpoint = mount_data;
  873. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  874. }
  875. static int chroot_migrate (void * checkpoint, void ** mount_data)
  876. {
  877. struct mount_data * mdata = checkpoint;
  878. int alloc_len = mdata->root_uri_len +
  879. sizeof(struct mount_data) + 1;
  880. void * new_data = malloc(alloc_len);
  881. memcpy(new_data, mdata, alloc_len);
  882. *mount_data = new_data;
  883. return 0;
  884. }
  885. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  886. {
  887. int ret;
  888. struct shim_file_data * data;
  889. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  890. return ret;
  891. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  892. if (!pal_hdl)
  893. return -PAL_ERRNO;
  894. DkStreamDelete(pal_hdl, 0);
  895. DkObjectClose(pal_hdl);
  896. dent->mode = NO_MODE;
  897. data->mode = 0;
  898. atomic_inc(&data->version);
  899. atomic_set(&data->size, 0);
  900. /* Drop the parent's link count */
  901. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  902. if (parent_data) {
  903. lock(parent_data->lock);
  904. if (parent_data->queried)
  905. parent_data->nlink--;
  906. unlock(parent_data->lock);
  907. }
  908. return 0;
  909. }
  910. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  911. {
  912. int ret;
  913. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  914. return ret;
  915. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  916. size_t size = atomic_read(&data->size);
  917. if (poll_type == FS_POLL_SZ)
  918. return size;
  919. lock(hdl->lock);
  920. struct shim_file_handle * file = &hdl->info.file;
  921. if (check_version(hdl) &&
  922. file->size < size)
  923. file->size = size;
  924. int marker = file->marker;
  925. if (file->buf_type == FILEBUF_MAP) {
  926. ret = poll_type & FS_POLL_WR;
  927. if ((poll_type & FS_POLL_RD) && file->size > marker)
  928. ret |= FS_POLL_RD;
  929. goto out;
  930. }
  931. ret = -EAGAIN;
  932. out:
  933. unlock(hdl->lock);
  934. return ret;
  935. }
  936. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  937. {
  938. int ret;
  939. struct shim_file_data * old_data;
  940. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  941. return ret;
  942. struct shim_file_data * new_data;
  943. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  944. return ret;
  945. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  946. 0, 0, 0, 0);
  947. if (!pal_hdl)
  948. return -PAL_ERRNO;
  949. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  950. DkObjectClose(pal_hdl);
  951. return -PAL_ERRNO;
  952. }
  953. new->mode = new_data->mode = old_data->mode;
  954. old->mode = NO_MODE;
  955. old_data->mode = 0;
  956. DkObjectClose(pal_hdl);
  957. atomic_inc(&old_data->version);
  958. atomic_set(&old_data->size, 0);
  959. atomic_inc(&new_data->version);
  960. return 0;
  961. }
  962. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  963. {
  964. int ret;
  965. struct shim_file_data * data;
  966. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  967. return ret;
  968. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  969. if (!pal_hdl)
  970. return -PAL_ERRNO;
  971. PAL_STREAM_ATTR attr = { .share_flags = mode };
  972. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  973. DkObjectClose(pal_hdl);
  974. return -PAL_ERRNO;
  975. }
  976. DkObjectClose(pal_hdl);
  977. dent->mode = data->mode = mode;
  978. return 0;
  979. }
  980. struct shim_fs_ops chroot_fs_ops = {
  981. .mount = &chroot_mount,
  982. .unmount = &chroot_unmount,
  983. .flush = &chroot_flush,
  984. .close = &chroot_flush,
  985. .read = &chroot_read,
  986. .write = &chroot_write,
  987. .mmap = &chroot_mmap,
  988. .seek = &chroot_seek,
  989. .hstat = &chroot_hstat,
  990. .truncate = &chroot_truncate,
  991. .checkout = &chroot_checkout,
  992. .checkpoint = &chroot_checkpoint,
  993. .migrate = &chroot_migrate,
  994. .poll = &chroot_poll,
  995. };
  996. struct shim_d_ops chroot_d_ops = {
  997. .open = &chroot_open,
  998. .mode = &chroot_mode,
  999. .lookup = &chroot_lookup,
  1000. .creat = &chroot_creat,
  1001. .mkdir = &chroot_mkdir,
  1002. .stat = &chroot_stat,
  1003. .dput = &chroot_dput,
  1004. .readdir = &chroot_readdir,
  1005. .unlink = &chroot_unlink,
  1006. .rename = &chroot_rename,
  1007. .chmod = &chroot_chmod,
  1008. };
  1009. struct mount_data chroot_data = { .root_uri_len = 5,
  1010. .root_uri = "file:", };
  1011. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  1012. .fs_ops = &chroot_fs_ops,
  1013. .d_ops = &chroot_d_ops,
  1014. .data = &chroot_data, };