fs.c 32 KB

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