fs.c 34 KB

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