fs.c 30 KB

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