fs.c 35 KB

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