fs.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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(alloc_align) * 4)
  37. #define FILE_BUF_SIZE (PAL_CB(alloc_align))
  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 (strstartswith_static(uri, URI_PREFIX_FILE)) {
  51. type = FILE_UNKNOWN;
  52. uri += 5;
  53. } else if (strstartswith_static(uri, URI_PREFIX_DEV)) {
  54. type = strstartswith_static(uri + static_strlen(URI_PREFIX_DEV), "tty") ? FILE_TTY : FILE_DEV;
  55. uri += 4;
  56. } else {
  57. return -EINVAL;
  58. }
  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, URI_PREFIX_FILE, size);
  86. break;
  87. case FILE_DIR:
  88. tmp = strcpy_static(buffer, URI_PREFIX_DIR, size);
  89. break;
  90. case FILE_DEV:
  91. case FILE_TTY:
  92. tmp = strcpy_static(buffer, URI_PREFIX_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. }
  214. data->nlink = nlink;
  215. } else {
  216. /* DEP 3/18/17: Right now, we don't support hard links,
  217. * so just return 1;
  218. */
  219. data->nlink = 1;
  220. }
  221. data->queried = true;
  222. return 0;
  223. }
  224. /* do not need any lock */
  225. static void chroot_update_ino (struct shim_dentry * dent)
  226. {
  227. if (dent->state & DENTRY_INO_UPDATED)
  228. return;
  229. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  230. unsigned long ino = mdata->ino_base;
  231. if (!qstrempty(&dent->rel_path))
  232. ino = rehash_path(mdata->ino_base, qstrgetstr(&dent->rel_path),
  233. dent->rel_path.len);
  234. dent->ino = ino;
  235. dent->state |= DENTRY_INO_UPDATED;
  236. }
  237. static inline int try_create_data (struct shim_dentry * dent,
  238. const char * uri, size_t len,
  239. struct shim_file_data ** dataptr)
  240. {
  241. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  242. if (!data) {
  243. lock(&dent->lock);
  244. int ret = create_data(dent, uri, len);
  245. data = FILE_DENTRY_DATA(dent);
  246. unlock(&dent->lock);
  247. if (ret < 0) {
  248. return ret;
  249. }
  250. }
  251. *dataptr = data;
  252. return 0;
  253. }
  254. static int query_dentry (struct shim_dentry * dent, PAL_HANDLE pal_handle,
  255. mode_t * mode, struct stat * stat)
  256. {
  257. int ret = 0;
  258. struct shim_file_data * data;
  259. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  260. return ret;
  261. lock(&data->lock);
  262. if (!data->queried && (ret = __query_attr(dent, data, pal_handle)) < 0) {
  263. unlock(&data->lock);
  264. return ret;
  265. }
  266. if (mode)
  267. *mode = data->mode;
  268. if (stat) {
  269. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  270. chroot_update_ino(dent);
  271. memset(stat, 0, sizeof(struct stat));
  272. stat->st_mode = (mode_t) data->mode;
  273. stat->st_dev = (dev_t) mdata->ino_base;
  274. stat->st_ino = (ino_t) dent->ino;
  275. stat->st_size = (off_t) atomic_read(&data->size);
  276. stat->st_atime = (time_t) data->atime;
  277. stat->st_mtime = (time_t) data->mtime;
  278. stat->st_ctime = (time_t) data->ctime;
  279. stat->st_nlink = data->nlink;
  280. switch (data->type) {
  281. case FILE_REGULAR:
  282. stat->st_mode |= S_IFREG;
  283. break;
  284. case FILE_DIR:
  285. stat->st_mode |= S_IFDIR;
  286. break;
  287. case FILE_DEV:
  288. case FILE_TTY:
  289. stat->st_mode |= S_IFCHR;
  290. break;
  291. default: break;
  292. }
  293. }
  294. unlock(&data->lock);
  295. return 0;
  296. }
  297. static int chroot_mode (struct shim_dentry * dent, mode_t * mode)
  298. {
  299. return query_dentry(dent, NULL, mode, NULL);
  300. }
  301. static int chroot_stat (struct shim_dentry * dent, struct stat * statbuf)
  302. {
  303. return query_dentry(dent, NULL, NULL, statbuf);
  304. }
  305. static int chroot_lookup (struct shim_dentry * dent)
  306. {
  307. return query_dentry(dent, NULL, NULL, NULL);
  308. }
  309. static int __chroot_open (struct shim_dentry * dent,
  310. const char * uri, int flags, mode_t mode,
  311. struct shim_handle * hdl,
  312. struct shim_file_data * data)
  313. {
  314. int ret = 0;
  315. if (!uri) {
  316. uri = qstrgetstr(&data->host_uri);
  317. }
  318. int version = atomic_read(&data->version);
  319. int oldmode = flags & O_ACCMODE;
  320. int accmode = oldmode;
  321. int creat = flags & PAL_CREATE_MASK;
  322. int option = flags & PAL_OPTION_MASK;
  323. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  324. && accmode == O_WRONLY)
  325. accmode = O_RDWR;
  326. PAL_HANDLE palhdl;
  327. if (hdl && hdl->pal_handle) {
  328. palhdl = hdl->pal_handle;
  329. } else {
  330. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  331. if (!palhdl) {
  332. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  333. accmode != oldmode)
  334. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  335. if (!palhdl)
  336. return -PAL_ERRNO;
  337. }
  338. }
  339. if (!data->queried) {
  340. lock(&data->lock);
  341. ret = __query_attr(dent, data, palhdl);
  342. unlock(&data->lock);
  343. }
  344. if (!hdl) {
  345. DkObjectClose(palhdl);
  346. return 0;
  347. }
  348. hdl->pal_handle = palhdl;
  349. hdl->info.file.type = data->type;
  350. hdl->info.file.version = version;
  351. hdl->info.file.size = atomic_read(&data->size);
  352. hdl->info.file.data = data;
  353. return ret;
  354. }
  355. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  356. int flags)
  357. {
  358. int ret = 0;
  359. struct shim_file_data * data;
  360. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  361. return ret;
  362. if (dent->mode == NO_MODE) {
  363. lock(&data->lock);
  364. ret = __query_attr(dent, data, NULL);
  365. dent->mode = data->mode;
  366. unlock(&data->lock);
  367. }
  368. if ((ret = __chroot_open(dent, NULL, flags, dent->mode, hdl, data)) < 0)
  369. return ret;
  370. struct shim_file_handle * file = &hdl->info.file;
  371. off_t size = atomic_read(&data->size);
  372. /* initialize hdl, does not need a lock because no one is sharing */
  373. hdl->type = TYPE_FILE;
  374. file->marker = (flags & O_APPEND) ? size : 0;
  375. file->size = size;
  376. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  377. hdl->flags = flags;
  378. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  379. qstrcopy(&hdl->uri, &data->host_uri);
  380. return 0;
  381. }
  382. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  383. struct shim_dentry * dent, int flags, mode_t mode)
  384. {
  385. int ret = 0;
  386. struct shim_file_data * data;
  387. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  388. return ret;
  389. if ((ret = __chroot_open(dent, NULL, flags|O_CREAT|O_EXCL, mode, hdl,
  390. data)) < 0)
  391. return ret;
  392. if (!hdl)
  393. return 0;
  394. struct shim_file_handle * file = &hdl->info.file;
  395. off_t size = atomic_read(&data->size);
  396. /* initialize hdl, does not need a lock because no one is sharing */
  397. hdl->type = TYPE_FILE;
  398. file->marker = (flags & O_APPEND) ? size : 0;
  399. file->size = size;
  400. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  401. hdl->flags = flags;
  402. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  403. qstrcopy(&hdl->uri, &data->host_uri);
  404. /* Increment the parent's link count */
  405. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  406. if (parent_data) {
  407. lock(&parent_data->lock);
  408. if (parent_data->queried)
  409. parent_data->nlink++;
  410. unlock(&parent_data->lock);
  411. }
  412. return 0;
  413. }
  414. static int chroot_mkdir (struct shim_dentry * dir, struct shim_dentry * dent,
  415. mode_t mode)
  416. {
  417. int ret = 0;
  418. struct shim_file_data * data;
  419. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  420. return ret;
  421. if (data->type != FILE_DIR) {
  422. data->type = FILE_DIR;
  423. int ret = make_uri(dent);
  424. if (ret < 0)
  425. return ret;
  426. }
  427. ret = __chroot_open(dent, NULL, O_CREAT|O_EXCL, mode, NULL, data);
  428. /* Increment the parent's link count */
  429. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  430. if (parent_data) {
  431. lock(&parent_data->lock);
  432. if (parent_data->queried)
  433. parent_data->nlink++;
  434. unlock(&parent_data->lock);
  435. }
  436. return ret;
  437. }
  438. #define NEED_RECREATE(hdl) (!FILE_HANDLE_DATA(hdl))
  439. static int chroot_recreate (struct shim_handle * hdl)
  440. {
  441. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  442. int ret = 0;
  443. /* quickly bail out if the data is created */
  444. if (data)
  445. return 0;
  446. const char * uri = qstrgetstr(&hdl->uri);
  447. size_t len = hdl->uri.len;
  448. if (hdl->dentry) {
  449. if ((ret = try_create_data(hdl->dentry, uri, len, &data)) < 0)
  450. return ret;
  451. } else {
  452. data = __create_data();
  453. if (!data)
  454. return -ENOMEM;
  455. qstrsetstr(&data->host_uri, uri, len);
  456. }
  457. /*
  458. * when recreating a file handle after migration, the file should
  459. * not be created again.
  460. */
  461. return __chroot_open(hdl->dentry, uri, hdl->flags & ~(O_CREAT|O_EXCL),
  462. 0, hdl, data);
  463. }
  464. static inline bool check_version (struct shim_handle * hdl)
  465. {
  466. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  467. == hdl->info.file.version;
  468. }
  469. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  470. {
  471. int ret;
  472. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  473. return ret;
  474. if (!check_version(hdl) || !hdl->dentry) {
  475. struct shim_file_handle * file = &hdl->info.file;
  476. struct shim_dentry * dent = hdl->dentry;
  477. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  478. if (dent)
  479. chroot_update_ino(dent);
  480. if (stat) {
  481. memset(stat, 0, sizeof(struct stat));
  482. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  483. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  484. stat->st_size = file->size;
  485. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  486. }
  487. return 0;
  488. }
  489. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  490. }
  491. static void chroot_flush_map(struct shim_handle* hdl) {
  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. }
  507. static int chroot_flush(struct shim_handle* hdl) {
  508. int ret = DkStreamFlush(hdl->pal_handle);
  509. if (ret < 0)
  510. return ret;
  511. chroot_flush_map(hdl);
  512. return 0;
  513. }
  514. static int chroot_close(struct shim_handle* hdl) {
  515. chroot_flush_map(hdl);
  516. return 0;
  517. }
  518. static inline int __map_buffer (struct shim_handle * hdl, size_t size)
  519. {
  520. struct shim_file_handle * file = &hdl->info.file;
  521. if (file->mapbuf) {
  522. if (file->marker >= file->mapoffset &&
  523. file->marker + size <= file->mapoffset + file->mapsize)
  524. return 0;
  525. DkStreamUnmap(file->mapbuf, file->mapsize);
  526. if (bkeep_munmap(file->mapbuf, file->mapsize, VMA_INTERNAL) < 0)
  527. BUG();
  528. file->mapbuf = NULL;
  529. file->mapoffset = 0;
  530. }
  531. /* second, reallocate the buffer */
  532. size_t bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  533. assert(IS_POWER_OF_2(bufsize));
  534. off_t mapoff = ALIGN_DOWN_POW2(file->marker, bufsize);
  535. size_t maplen = bufsize;
  536. int flags = MAP_FILE | MAP_PRIVATE | VMA_INTERNAL;
  537. int prot = PROT_READ;
  538. if (hdl->acc_mode & MAY_WRITE) {
  539. flags = MAP_FILE | MAP_SHARED | VMA_INTERNAL;
  540. prot |= PROT_WRITE;
  541. }
  542. while (mapoff + maplen < file->marker + size)
  543. maplen *= 2;
  544. /* Create the bookkeeping before allocating the memory. */
  545. void * mapbuf = bkeep_unmapped_any(maplen, prot, flags, mapoff, "filebuf");
  546. if (!mapbuf)
  547. return -ENOMEM;
  548. PAL_PTR mapped = DkStreamMap(hdl->pal_handle, mapbuf, PAL_PROT(prot, flags),
  549. mapoff, maplen);
  550. if (!mapped) {
  551. bkeep_munmap(mapbuf, maplen, flags);
  552. return -PAL_ERRNO;
  553. }
  554. assert((void*)mapped == mapbuf);
  555. file->mapbuf = mapbuf;
  556. file->mapoffset = mapoff;
  557. file->mapsize = maplen;
  558. return 0;
  559. }
  560. static ssize_t map_read (struct shim_handle * hdl, void * buf, size_t count)
  561. {
  562. struct shim_file_handle * file = &hdl->info.file;
  563. ssize_t ret = 0;
  564. lock(&hdl->lock);
  565. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  566. off_t size = atomic_read(&data->size);
  567. if (check_version(hdl) &&
  568. file->size < size)
  569. file->size = size;
  570. off_t marker = file->marker;
  571. if (marker >= file->size) {
  572. count = 0;
  573. goto out;
  574. }
  575. if ((ret = __map_buffer(hdl, count)) < 0) {
  576. unlock(&hdl->lock);
  577. return ret;
  578. }
  579. size_t bytes_left;
  580. if (!__builtin_sub_overflow(file->size, marker, &bytes_left) && bytes_left < count)
  581. count = bytes_left;
  582. if (count) {
  583. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  584. file->marker = marker + count;
  585. }
  586. out:
  587. unlock(&hdl->lock);
  588. return count;
  589. }
  590. static ssize_t map_write (struct shim_handle * hdl, const void * buf, size_t count)
  591. {
  592. struct shim_file_handle * file = &hdl->info.file;
  593. ssize_t ret = 0;
  594. lock(&hdl->lock);
  595. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  596. off_t marker = file->marker;
  597. off_t new_marker;
  598. if (__builtin_add_overflow(marker, count, &new_marker)) {
  599. // We can't handle this case reasonably.
  600. ret = -EFBIG;
  601. goto out;
  602. }
  603. if (new_marker > file->size) {
  604. file->size = new_marker;
  605. PAL_NUM pal_ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  606. if (pal_ret == PAL_STREAM_ERROR) {
  607. ret = -PAL_ERRNO;
  608. goto out;
  609. }
  610. if (pal_ret < count) {
  611. file->size -= count - pal_ret;
  612. }
  613. if (check_version(hdl)) {
  614. off_t size;
  615. do {
  616. if ((size = atomic_read(&data->size)) >= file->size) {
  617. file->size = size;
  618. break;
  619. }
  620. } while ((off_t) atomic_cmpxchg(&data->size, size, file->size) != size);
  621. }
  622. if (__builtin_add_overflow(marker, pal_ret, &file->marker)) {
  623. // Should never happen. Even if it would, we couldn't recover from this condition.
  624. BUG();
  625. }
  626. ret = (ssize_t) pal_ret;
  627. goto out;
  628. }
  629. if ((ret = __map_buffer(hdl, count)) < 0)
  630. goto out;
  631. if (count) {
  632. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  633. file->marker = new_marker;
  634. }
  635. ret = count;
  636. out:
  637. unlock(&hdl->lock);
  638. return ret;
  639. }
  640. static ssize_t chroot_read (struct shim_handle * hdl, void * buf, size_t count)
  641. {
  642. ssize_t ret = 0;
  643. if (count == 0)
  644. goto out;
  645. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  646. goto out;
  647. }
  648. if (!(hdl->acc_mode & MAY_READ)) {
  649. ret = -EBADF;
  650. goto out;
  651. }
  652. struct shim_file_handle * file = &hdl->info.file;
  653. off_t dummy_off_t;
  654. if (file->type != FILE_TTY && __builtin_add_overflow(file->marker, count, &dummy_off_t)) {
  655. ret = -EFBIG;
  656. goto out;
  657. }
  658. if (file->buf_type == FILEBUF_MAP) {
  659. ret = map_read(hdl, buf, count);
  660. if (ret != -EACCES)
  661. goto out;
  662. lock(&hdl->lock);
  663. file->buf_type = FILEBUF_NONE;
  664. } else {
  665. lock(&hdl->lock);
  666. }
  667. PAL_NUM pal_ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0);
  668. if (pal_ret != PAL_STREAM_ERROR) {
  669. if (__builtin_add_overflow(pal_ret, 0, &ret))
  670. BUG();
  671. if (file->type != FILE_TTY && __builtin_add_overflow(file->marker, pal_ret, &file->marker))
  672. BUG();
  673. } else {
  674. ret = PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO;
  675. }
  676. unlock(&hdl->lock);
  677. out:
  678. return ret;
  679. }
  680. static ssize_t chroot_write (struct shim_handle * hdl, const void * buf, size_t count)
  681. {
  682. ssize_t ret;
  683. if (count == 0)
  684. return 0;
  685. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  686. goto out;
  687. }
  688. if (!(hdl->acc_mode & MAY_WRITE)) {
  689. ret = -EBADF;
  690. goto out;
  691. }
  692. struct shim_file_handle * file = &hdl->info.file;
  693. off_t dummy_off_t;
  694. if (file->type != FILE_TTY && __builtin_add_overflow(file->marker, count, &dummy_off_t)) {
  695. ret = -EFBIG;
  696. goto out;
  697. }
  698. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  699. ret = map_write(hdl, buf, count);
  700. if (ret != -EACCES)
  701. goto out;
  702. lock(&hdl->lock);
  703. file->buf_type = FILEBUF_NONE;
  704. } else {
  705. lock(&hdl->lock);
  706. }
  707. PAL_NUM pal_ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  708. if (pal_ret != PAL_STREAM_ERROR) {
  709. if (__builtin_add_overflow(pal_ret, 0, &ret))
  710. BUG();
  711. if (file->type != FILE_TTY && __builtin_add_overflow(file->marker, pal_ret, &file->marker))
  712. BUG();
  713. } else {
  714. ret = PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO;
  715. }
  716. unlock(&hdl->lock);
  717. out:
  718. return ret;
  719. }
  720. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  721. int prot, int flags, off_t offset)
  722. {
  723. int ret;
  724. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  725. return ret;
  726. int pal_prot = PAL_PROT(prot, flags);
  727. #if MAP_FILE == 0
  728. if (flags & MAP_ANONYMOUS)
  729. #else
  730. if (!(flags & MAP_FILE))
  731. #endif
  732. return -EINVAL;
  733. void * alloc_addr =
  734. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  735. if (!alloc_addr)
  736. return -PAL_ERRNO;
  737. *addr = alloc_addr;
  738. return 0;
  739. }
  740. static off_t chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  741. {
  742. off_t ret = -EINVAL;
  743. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  744. return ret;
  745. struct shim_file_handle * file = &hdl->info.file;
  746. lock(&hdl->lock);
  747. off_t marker = file->marker;
  748. off_t size = file->size;
  749. if (check_version(hdl)) {
  750. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  751. if (data->type != FILE_REGULAR) {
  752. ret = -ESPIPE;
  753. goto out;
  754. }
  755. }
  756. switch (wence) {
  757. case SEEK_SET:
  758. if (offset < 0)
  759. goto out;
  760. marker = offset;
  761. break;
  762. case SEEK_CUR:
  763. marker += offset;
  764. break;
  765. case SEEK_END:
  766. marker = size + offset;
  767. break;
  768. }
  769. ret = file->marker = marker;
  770. out:
  771. unlock(&hdl->lock);
  772. return ret;
  773. }
  774. static int chroot_truncate (struct shim_handle * hdl, off_t len)
  775. {
  776. int ret = 0;
  777. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  778. return ret;
  779. if (!(hdl->acc_mode & MAY_WRITE))
  780. return -EINVAL;
  781. struct shim_file_handle * file = &hdl->info.file;
  782. lock(&hdl->lock);
  783. file->size = len;
  784. if (check_version(hdl)) {
  785. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  786. atomic_set(&data->size, len);
  787. }
  788. PAL_NUM rv = DkStreamSetLength(hdl->pal_handle, len);
  789. if (rv) {
  790. // For an error, cast it back down to an int return code
  791. ret = -((int)rv);
  792. goto out;
  793. }
  794. // DEP 10/25/16: Truncate returns 0 on success, not the length
  795. ret = 0;
  796. if (file->marker > len)
  797. file->marker = len;
  798. out:
  799. unlock(&hdl->lock);
  800. return ret;
  801. }
  802. static int chroot_dput (struct shim_dentry * dent)
  803. {
  804. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  805. if (data) {
  806. __destroy_data(data);
  807. dent->data = NULL;
  808. }
  809. return 0;
  810. }
  811. static int chroot_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
  812. struct shim_file_data* data = NULL;
  813. int ret = 0;
  814. PAL_HANDLE pal_hdl = NULL;
  815. size_t buf_size = MAX_PATH,
  816. dirent_buf_size = 0;
  817. char* buf = NULL;
  818. char* dirent_buf = NULL;
  819. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  820. return ret;
  821. chroot_update_ino(dent);
  822. const char* uri = qstrgetstr(&data->host_uri);
  823. assert(strstartswith_static(uri, URI_PREFIX_DIR));
  824. pal_hdl = DkStreamOpen(uri, PAL_ACCESS_RDONLY, 0, 0, 0);
  825. if (!pal_hdl)
  826. return -PAL_ERRNO;
  827. buf = malloc(buf_size);
  828. if (!buf) {
  829. ret = -ENOMEM;
  830. goto out;
  831. }
  832. while (1) {
  833. /* DkStreamRead for directory will return as many entries as fits into the buffer. */
  834. PAL_NUM bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  835. if (bytes == PAL_STREAM_ERROR) {
  836. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM) {
  837. /* End of directory listing */
  838. ret = 0;
  839. break;
  840. }
  841. ret = -PAL_ERRNO;
  842. goto out;
  843. }
  844. /* Last entry must be null-terminated */
  845. assert(buf[bytes - 1] == '\0');
  846. size_t dirent_cur_off = dirent_buf_size;
  847. /* Calculate needed buffer size */
  848. size_t len = buf[0] != '\0' ? 1 : 0;
  849. for (size_t i = 1; i < bytes; i++) {
  850. if (buf[i] == '\0') {
  851. /* The PAL convention: if a name ends with '/', it is a directory.
  852. * struct shim_dirent has a field for a type, hence trailing slash
  853. * can be safely discarded. */
  854. if (buf[i - 1] == '/') {
  855. len--;
  856. }
  857. dirent_buf_size += SHIM_DIRENT_ALIGNED_SIZE(len + 1);
  858. len = 0;
  859. } else {
  860. len++;
  861. }
  862. }
  863. /* TODO: If realloc gets enabled delete following and uncomment rest */
  864. char* tmp = malloc(dirent_buf_size);
  865. if (!tmp) {
  866. ret = -ENOMEM;
  867. goto out;
  868. }
  869. memcpy(tmp, dirent_buf, dirent_cur_off);
  870. free(dirent_buf);
  871. dirent_buf = tmp;
  872. /*
  873. dirent_buf = realloc(dirent_buf, dirent_buf_size);
  874. if (!dirent_buf) {
  875. ret = -ENOMEM;
  876. goto out;
  877. }
  878. */
  879. size_t i = 0;
  880. while (i < bytes) {
  881. char* name = buf + i;
  882. size_t len = strnlen(name, bytes - i);
  883. i += len + 1;
  884. bool is_dir = false;
  885. /* Skipping trailing slash - explained above */
  886. if (name[len - 1] == '/') {
  887. is_dir = true;
  888. name[--len] = '\0';
  889. }
  890. struct shim_dirent* dptr = (struct shim_dirent*)(dirent_buf + dirent_cur_off);
  891. dptr->ino = rehash_name(dent->ino, name, len);
  892. dptr->type = is_dir ? LINUX_DT_DIR : LINUX_DT_REG;
  893. memcpy(dptr->name, name, len + 1);
  894. dirent_cur_off += SHIM_DIRENT_ALIGNED_SIZE(len + 1);
  895. }
  896. }
  897. *dirent = (struct shim_dirent*)dirent_buf;
  898. /*
  899. * Fix next field of struct shim_dirent to point to the next entry.
  900. * Since all entries are assumed to come from single allocation
  901. * (as free gets called just on the head of this list) this should have
  902. * been just entry size instead of a pointer (and probably needs to be
  903. * rewritten as such one day).
  904. */
  905. struct shim_dirent** last = NULL;
  906. for (size_t dirent_cur_off = 0; dirent_cur_off < dirent_buf_size; ) {
  907. struct shim_dirent* dptr = (struct shim_dirent*)(dirent_buf + dirent_cur_off);
  908. size_t len = SHIM_DIRENT_ALIGNED_SIZE(strlen(dptr->name) + 1);
  909. dptr->next = (struct shim_dirent*)(dirent_buf + dirent_cur_off + len);
  910. last = &dptr->next;
  911. dirent_cur_off += len;
  912. }
  913. if (last) {
  914. *last = NULL;
  915. }
  916. out:
  917. /* Need to free output buffer if error is returned */
  918. if (ret) {
  919. free(dirent_buf);
  920. }
  921. free(buf);
  922. DkObjectClose(pal_hdl);
  923. return ret;
  924. }
  925. static int chroot_checkout (struct shim_handle * hdl)
  926. {
  927. if (hdl->fs == &chroot_builtin_fs)
  928. hdl->fs = NULL;
  929. if (hdl->type == TYPE_FILE) {
  930. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  931. if (data)
  932. hdl->info.file.data = NULL;
  933. }
  934. if (hdl->pal_handle) {
  935. /*
  936. * if the file still exists in the host, no need to send
  937. * the handle over RPC; otherwise, send it.
  938. */
  939. PAL_STREAM_ATTR attr;
  940. if (DkStreamAttributesQuery(qstrgetstr(&hdl->uri), &attr))
  941. hdl->pal_handle = NULL;
  942. }
  943. hdl->info.file.mapsize = 0;
  944. hdl->info.file.mapoffset = 0;
  945. hdl->info.file.mapbuf = NULL;
  946. return 0;
  947. }
  948. static ssize_t chroot_checkpoint (void ** checkpoint, void * mount_data)
  949. {
  950. struct mount_data * mdata = mount_data;
  951. *checkpoint = mount_data;
  952. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  953. }
  954. static int chroot_migrate (void * checkpoint, void ** mount_data)
  955. {
  956. struct mount_data * mdata = checkpoint;
  957. size_t alloc_len = mdata->root_uri_len + sizeof(struct mount_data) + 1;
  958. void * new_data = malloc(alloc_len);
  959. if (!new_data)
  960. return -ENOMEM;
  961. memcpy(new_data, mdata, alloc_len);
  962. *mount_data = new_data;
  963. return 0;
  964. }
  965. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  966. {
  967. int ret;
  968. struct shim_file_data * data;
  969. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  970. return ret;
  971. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  972. if (!pal_hdl)
  973. return -PAL_ERRNO;
  974. DkStreamDelete(pal_hdl, 0);
  975. DkObjectClose(pal_hdl);
  976. dent->mode = NO_MODE;
  977. data->mode = 0;
  978. atomic_inc(&data->version);
  979. atomic_set(&data->size, 0);
  980. /* Drop the parent's link count */
  981. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  982. if (parent_data) {
  983. lock(&parent_data->lock);
  984. if (parent_data->queried)
  985. parent_data->nlink--;
  986. unlock(&parent_data->lock);
  987. }
  988. return 0;
  989. }
  990. static off_t chroot_poll (struct shim_handle * hdl, int poll_type)
  991. {
  992. int ret;
  993. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  994. return ret;
  995. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  996. off_t size = atomic_read(&data->size);
  997. if (poll_type == FS_POLL_SZ)
  998. return size;
  999. lock(&hdl->lock);
  1000. struct shim_file_handle * file = &hdl->info.file;
  1001. if (check_version(hdl) &&
  1002. file->size < size)
  1003. file->size = size;
  1004. off_t marker = file->marker;
  1005. if (file->buf_type == FILEBUF_MAP) {
  1006. ret = poll_type & FS_POLL_WR;
  1007. if ((poll_type & FS_POLL_RD) && file->size > marker)
  1008. ret |= FS_POLL_RD;
  1009. goto out;
  1010. }
  1011. ret = -EAGAIN;
  1012. out:
  1013. unlock(&hdl->lock);
  1014. return ret;
  1015. }
  1016. static int chroot_rename(struct shim_dentry* old, struct shim_dentry* new) {
  1017. int ret;
  1018. struct shim_file_data* old_data;
  1019. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0) {
  1020. return ret;
  1021. }
  1022. struct shim_file_data* new_data;
  1023. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0) {
  1024. return ret;
  1025. }
  1026. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri), 0, 0, 0, 0);
  1027. if (!pal_hdl) {
  1028. return -PAL_ERRNO;
  1029. }
  1030. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  1031. DkObjectClose(pal_hdl);
  1032. return -PAL_ERRNO;
  1033. }
  1034. new->mode = new_data->mode = old_data->mode;
  1035. old->mode = NO_MODE;
  1036. old_data->mode = 0;
  1037. new->type = old->type;
  1038. DkObjectClose(pal_hdl);
  1039. atomic_inc(&old_data->version);
  1040. atomic_set(&old_data->size, 0);
  1041. atomic_inc(&new_data->version);
  1042. return 0;
  1043. }
  1044. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  1045. {
  1046. int ret;
  1047. struct shim_file_data * data;
  1048. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  1049. return ret;
  1050. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  1051. if (!pal_hdl)
  1052. return -PAL_ERRNO;
  1053. PAL_STREAM_ATTR attr = { .share_flags = mode };
  1054. if (!DkStreamAttributesSetByHandle(pal_hdl, &attr)) {
  1055. DkObjectClose(pal_hdl);
  1056. return -PAL_ERRNO;
  1057. }
  1058. DkObjectClose(pal_hdl);
  1059. dent->mode = data->mode = mode;
  1060. return 0;
  1061. }
  1062. struct shim_fs_ops chroot_fs_ops = {
  1063. .mount = &chroot_mount,
  1064. .unmount = &chroot_unmount,
  1065. .flush = &chroot_flush,
  1066. .close = &chroot_close,
  1067. .read = &chroot_read,
  1068. .write = &chroot_write,
  1069. .mmap = &chroot_mmap,
  1070. .seek = &chroot_seek,
  1071. .hstat = &chroot_hstat,
  1072. .truncate = &chroot_truncate,
  1073. .checkout = &chroot_checkout,
  1074. .checkpoint = &chroot_checkpoint,
  1075. .migrate = &chroot_migrate,
  1076. .poll = &chroot_poll,
  1077. };
  1078. struct shim_d_ops chroot_d_ops = {
  1079. .open = &chroot_open,
  1080. .mode = &chroot_mode,
  1081. .lookup = &chroot_lookup,
  1082. .creat = &chroot_creat,
  1083. .mkdir = &chroot_mkdir,
  1084. .stat = &chroot_stat,
  1085. .dput = &chroot_dput,
  1086. .readdir = &chroot_readdir,
  1087. .unlink = &chroot_unlink,
  1088. .rename = &chroot_rename,
  1089. .chmod = &chroot_chmod,
  1090. };
  1091. struct mount_data chroot_data = { .root_uri_len = 5,
  1092. .root_uri = URI_PREFIX_FILE, };
  1093. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  1094. .fs_ops = &chroot_fs_ops,
  1095. .d_ops = &chroot_d_ops,
  1096. .data = &chroot_data, };