fs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * fs.c
  17. *
  18. * This file contains codes for implementation of 'chroot' filesystem.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_fs.h>
  25. #include <shim_utils.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <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 = malloc(sizeof(struct shim_file_data));
  118. if (!data)
  119. return NULL;
  120. memset(data, 0, sizeof(struct shim_file_data));
  121. create_lock(data->lock);
  122. return data;
  123. }
  124. static void __destroy_data (struct shim_file_data * data)
  125. {
  126. qstrfree(&data->host_uri);
  127. destroy_lock(data->lock);
  128. free(data);
  129. }
  130. static int make_uri (struct shim_dentry * dent)
  131. {
  132. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  133. assert(mdata);
  134. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  135. char * uri = __alloca(URI_MAX_SIZE);
  136. int len = concat_uri(uri, URI_MAX_SIZE, data->type,
  137. mdata->root_uri,
  138. mdata->root_uri_len,
  139. qstrgetstr(&dent->rel_path),
  140. dent->rel_path.len);
  141. if (len >= 0)
  142. qstrsetstr(&data->host_uri, uri, len);
  143. return len;
  144. }
  145. /* create a data in the dentry and compose it's uri. dent->lock needs to
  146. be held */
  147. static int create_data (struct shim_dentry * dent, const char * uri, int len)
  148. {
  149. if (dent->data)
  150. return 0;
  151. struct shim_file_data * data = __create_data();
  152. if (!data)
  153. return -ENOMEM;
  154. dent->data = data;
  155. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  156. assert(mdata);
  157. data->type = (dent->state & DENTRY_ISDIRECTORY) ?
  158. FILE_DIR : mdata->base_type;
  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 __query_attr (struct shim_file_data * data, PAL_HANDLE pal_handle)
  170. {
  171. PAL_STREAM_ATTR pal_attr;
  172. if (pal_handle ?
  173. !DkStreamAttributesQuerybyHandle(pal_handle, &pal_attr) :
  174. !DkStreamAttributesQuery(qstrgetstr(&data->host_uri), &pal_attr))
  175. return -PAL_ERRNO;
  176. /* need to correct the data type */
  177. if (data->type == FILE_UNKNOWN)
  178. switch (pal_attr.handle_type) {
  179. case pal_type_file: data->type = FILE_REGULAR; break;
  180. case pal_type_dir: data->type = FILE_DIR; break;
  181. case pal_type_dev: data->type = FILE_DEV; break;
  182. }
  183. data->mode = (pal_attr.readable ? S_IRUSR : 0) |
  184. (pal_attr.writeable ? S_IWUSR : 0) |
  185. (pal_attr.runnable ? S_IXUSR : 0);
  186. atomic_set(&data->size, pal_attr.pending_size);
  187. data->queried = true;
  188. return 0;
  189. }
  190. /* do not need any lock */
  191. static void chroot_update_ino (struct shim_dentry * dent)
  192. {
  193. if (dent->state & DENTRY_INO_UPDATED)
  194. return;
  195. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  196. unsigned long ino = mdata->ino_base;
  197. if (!qstrempty(&dent->rel_path))
  198. ino = rehash_path(mdata->ino_base, qstrgetstr(&dent->rel_path),
  199. dent->rel_path.len, NULL);
  200. dent->ino = ino;
  201. dent->state |= DENTRY_INO_UPDATED;
  202. }
  203. static inline int try_create_data (struct shim_dentry * dent,
  204. const char * uri, int len,
  205. struct shim_file_data ** dataptr)
  206. {
  207. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  208. if (!data) {
  209. lock(dent->lock);
  210. int ret = create_data(dent, uri, len);
  211. data = FILE_DENTRY_DATA(dent);
  212. unlock(dent->lock);
  213. if (ret < 0) {
  214. return ret;
  215. }
  216. }
  217. *dataptr = data;
  218. return 0;
  219. }
  220. static int query_dentry (struct shim_dentry * dent, PAL_HANDLE pal_handle,
  221. mode_t * mode, struct stat * stat)
  222. {
  223. int ret = 0;
  224. struct shim_file_data * data;
  225. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  226. return ret;
  227. lock(data->lock);
  228. enum shim_file_type old_type = data->type;
  229. if (!data->queried && (ret = __query_attr(data, pal_handle)) < 0) {
  230. unlock(data->lock);
  231. return ret;
  232. }
  233. if (data->type == FILE_DIR && old_type != FILE_DIR) {
  234. dent->state |= DENTRY_ISDIRECTORY;
  235. if ((ret = make_uri(dent)) < 0) {
  236. unlock(data->lock);
  237. return ret;
  238. }
  239. }
  240. if (mode)
  241. *mode = data->mode;
  242. if (stat) {
  243. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  244. chroot_update_ino(dent);
  245. memset(stat, 0, sizeof(struct stat));
  246. stat->st_mode = (mode_t) data->mode;
  247. stat->st_dev = (dev_t) mdata->ino_base;
  248. stat->st_ino = (ino_t) dent->ino;
  249. stat->st_size = (off_t) atomic_read(&data->size);
  250. stat->st_atime = (time_t) data->atime;
  251. stat->st_mtime = (time_t) data->mtime;
  252. stat->st_ctime = (time_t) data->ctime;
  253. switch (data->type) {
  254. case FILE_REGULAR: stat->st_mode |= S_IFREG; break;
  255. case FILE_DIR: stat->st_mode |= S_IFDIR; break;
  256. case FILE_DEV:
  257. case FILE_TTY: stat->st_mode |= S_IFCHR; break;
  258. default: break;
  259. }
  260. }
  261. unlock(data->lock);
  262. return 0;
  263. }
  264. static int chroot_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  265. {
  266. if (!force)
  267. return -ESKIPPED;
  268. return query_dentry(dent, NULL, mode, NULL);
  269. }
  270. static int chroot_stat (struct shim_dentry * dent, struct stat * statbuf)
  271. {
  272. return query_dentry(dent, NULL, NULL, statbuf);
  273. }
  274. static int chroot_lookup (struct shim_dentry * dent, bool force)
  275. {
  276. if (!force)
  277. return -ESKIPPED;
  278. if (dent->fs && dent == dent->fs->root)
  279. return 0;
  280. return query_dentry(dent, NULL, NULL, NULL);
  281. }
  282. static int __chroot_open (const char * uri, int len, int flags, mode_t mode,
  283. struct shim_handle * hdl,
  284. struct shim_file_data * data)
  285. {
  286. int ret = 0;
  287. if (!uri) {
  288. uri = qstrgetstr(&data->host_uri);
  289. len = data->host_uri.len;
  290. }
  291. int version = atomic_read(&data->version);
  292. int oldmode = flags & O_ACCMODE;
  293. int accmode = oldmode;
  294. int creat = flags & PAL_CREAT_MASK;
  295. int option = flags & PAL_OPTION_MASK;
  296. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  297. && accmode == O_WRONLY)
  298. accmode = O_RDWR;
  299. PAL_HANDLE palhdl;
  300. if (hdl && hdl->pal_handle) {
  301. palhdl = hdl->pal_handle;
  302. } else {
  303. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  304. if (!palhdl) {
  305. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  306. accmode != oldmode)
  307. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  308. if (!palhdl)
  309. return -PAL_ERRNO;
  310. }
  311. }
  312. if (!data->queried) {
  313. lock(data->lock);
  314. ret = __query_attr(data, palhdl);
  315. unlock(data->lock);
  316. }
  317. if (!hdl) {
  318. DkObjectClose(palhdl);
  319. return 0;
  320. }
  321. hdl->pal_handle = palhdl;
  322. hdl->info.file.type = data->type;
  323. hdl->info.file.version = version;
  324. hdl->info.file.size = atomic_read(&data->size);
  325. hdl->info.file.data = data;
  326. return ret;
  327. }
  328. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  329. int flags)
  330. {
  331. int ret = 0;
  332. struct shim_file_data * data;
  333. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  334. return ret;
  335. if ((ret = __chroot_open(NULL, 0, flags, dent->mode, hdl, data)) < 0)
  336. return ret;
  337. struct shim_file_handle * file = &hdl->info.file;
  338. int size = atomic_read(&data->size);
  339. /* initialize hdl, does not need a lock because no one is sharing */
  340. hdl->type = TYPE_FILE;
  341. file->marker = (flags & O_APPEND) ? size : 0;
  342. file->size = size;
  343. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  344. hdl->flags = flags;
  345. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  346. qstrcopy(&hdl->uri, &data->host_uri);
  347. return 0;
  348. }
  349. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  350. struct shim_dentry * dent, int flags, mode_t mode)
  351. {
  352. int ret = 0;
  353. struct shim_file_data * data;
  354. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  355. return ret;
  356. if ((ret = __chroot_open(NULL, 0, flags|O_CREAT|O_EXCL, mode, hdl,
  357. data)) < 0)
  358. return ret;
  359. if (!hdl)
  360. return 0;
  361. struct shim_file_handle * file = &hdl->info.file;
  362. int size = atomic_read(&data->size);
  363. /* initialize hdl, does not need a lock because no one is sharing */
  364. hdl->type = TYPE_FILE;
  365. file->marker = (flags & O_APPEND) ? size : 0;
  366. file->size = size;
  367. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  368. hdl->flags = flags;
  369. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  370. qstrcopy(&hdl->uri, &data->host_uri);
  371. return 0;
  372. }
  373. static int chroot_mkdir (struct shim_dentry * dir, struct shim_dentry * dent,
  374. mode_t mode)
  375. {
  376. int ret = 0;
  377. struct shim_file_data * data;
  378. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  379. return ret;
  380. if (data->type != FILE_DIR) {
  381. data->type = FILE_DIR;
  382. int ret = make_uri(dent);
  383. if (ret < 0)
  384. return ret;
  385. }
  386. return __chroot_open(NULL, 0, O_CREAT|O_EXCL, mode, NULL, data);
  387. }
  388. #define NEED_RECREATE(hdl) (!FILE_HANDLE_DATA(hdl))
  389. static int chroot_recreate (struct shim_handle * hdl)
  390. {
  391. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  392. int ret = 0;
  393. /* quickly bail out if the data is created */
  394. if (data)
  395. return 0;
  396. const char * uri = qstrgetstr(&hdl->uri);
  397. int len = hdl->uri.len;
  398. if (hdl->dentry) {
  399. if ((ret = try_create_data(hdl->dentry, uri, len, &data)) < 0)
  400. return ret;
  401. } else {
  402. data = __create_data();
  403. if (!data)
  404. return -ENOMEM;
  405. qstrsetstr(&data->host_uri, uri, len);
  406. }
  407. return __chroot_open(uri, len, hdl->flags, 0, hdl, data);
  408. }
  409. static inline bool check_version (struct shim_handle * hdl)
  410. {
  411. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  412. == hdl->info.file.version;
  413. }
  414. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  415. {
  416. int ret;
  417. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  418. return ret;
  419. if (!check_version(hdl) || !hdl->dentry) {
  420. struct shim_file_handle * file = &hdl->info.file;
  421. struct shim_dentry * dent = hdl->dentry;
  422. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  423. if (dent)
  424. chroot_update_ino(dent);
  425. if (stat) {
  426. memset(stat, 0, sizeof(struct stat));
  427. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  428. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  429. stat->st_size = file->size;
  430. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  431. }
  432. return 0;
  433. }
  434. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  435. }
  436. static int chroot_flush (struct shim_handle * hdl)
  437. {
  438. struct shim_file_handle * file = &hdl->info.file;
  439. if (file->buf_type == FILEBUF_MAP) {
  440. lock(hdl->lock);
  441. void * mapbuf = file->mapbuf;
  442. int mapsize = file->mapsize;
  443. file->mapoffset = 0;
  444. file->mapbuf = NULL;
  445. unlock(hdl->lock);
  446. if (mapbuf) {
  447. DkStreamUnmap(mapbuf, mapsize);
  448. int flags = VMA_INTERNAL;
  449. bkeep_munmap(mapbuf, mapsize, &flags);
  450. }
  451. }
  452. return 0;
  453. }
  454. static inline int __map_buffer (struct shim_handle * hdl, int size)
  455. {
  456. struct shim_file_handle * file = &hdl->info.file;
  457. if (file->mapbuf) {
  458. if (file->marker >= file->mapoffset &&
  459. file->marker + size <= file->mapoffset + file->mapsize)
  460. return 0;
  461. DkStreamUnmap(file->mapbuf, file->mapsize);
  462. int flags = VMA_INTERNAL;
  463. bkeep_munmap(file->mapbuf, file->mapsize, &flags);
  464. file->mapbuf = NULL;
  465. file->mapoffset = 0;
  466. }
  467. /* second, reallocate the buffer */
  468. int bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  469. int prot = PROT_READ;
  470. unsigned long mapoff = file->marker & ~(bufsize - 1);
  471. unsigned long maplen = bufsize;
  472. if (hdl->acc_mode & MAY_WRITE)
  473. prot |= PROT_WRITE;
  474. while (mapoff + maplen < file->marker + size)
  475. maplen *= 2;
  476. void * mapbuf =
  477. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  478. if (!mapbuf)
  479. return -PAL_ERRNO;
  480. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  481. hdl, mapoff, NULL);
  482. file->mapbuf = mapbuf;
  483. file->mapoffset = mapoff;
  484. file->mapsize = maplen;
  485. return 0;
  486. }
  487. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  488. {
  489. struct shim_file_handle * file = &hdl->info.file;
  490. int ret = 0;
  491. lock(hdl->lock);
  492. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  493. unsigned int size = atomic_read(&data->size);
  494. if (check_version(hdl) &&
  495. file->size < size)
  496. file->size = size;
  497. int marker = file->marker;
  498. if (marker >= file->size) {
  499. count = 0;
  500. goto out;
  501. }
  502. if ((ret = __map_buffer(hdl, count)) < 0) {
  503. unlock(hdl->lock);
  504. return ret;
  505. }
  506. if (marker + count > file->size)
  507. count = file->size - marker;
  508. if (count) {
  509. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  510. file->marker = marker + count;
  511. }
  512. out:
  513. unlock(hdl->lock);
  514. return count;
  515. }
  516. static int map_write (struct shim_handle * hdl, const void * buf,
  517. size_t count)
  518. {
  519. struct shim_file_handle * file = &hdl->info.file;
  520. int ret = 0;
  521. lock(hdl->lock);
  522. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  523. int marker = file->marker;
  524. if (file->marker + count > file->size) {
  525. file->size = file->marker + count;
  526. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  527. if (!ret) {
  528. ret = -PAL_ERRNO;
  529. goto out;
  530. }
  531. if (ret < count)
  532. file->size -= count - ret;
  533. if (check_version(hdl)) {
  534. int size;
  535. do {
  536. if ((size = atomic_read(&data->size)) >= file->size) {
  537. file->size = size;
  538. break;
  539. }
  540. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  541. }
  542. file->marker = marker + ret;
  543. goto out;
  544. }
  545. if ((ret = __map_buffer(hdl, count)) < 0)
  546. goto out;
  547. if (count) {
  548. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  549. file->marker = marker + count;
  550. }
  551. ret = count;
  552. out:
  553. unlock(hdl->lock);
  554. return ret;
  555. }
  556. static int chroot_read (struct shim_handle * hdl, void * buf,
  557. size_t count)
  558. {
  559. int ret = 0;
  560. if (count == 0)
  561. goto out;
  562. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  563. goto out;
  564. }
  565. struct shim_file_handle * file = &hdl->info.file;
  566. if (file->buf_type == FILEBUF_MAP) {
  567. ret = map_read(hdl, buf, count);
  568. if (ret != -EACCES)
  569. goto out;
  570. lock(hdl->lock);
  571. file->buf_type = FILEBUF_NONE;
  572. } else {
  573. lock(hdl->lock);
  574. }
  575. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  576. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  577. if (ret > 0)
  578. file->marker += ret;
  579. unlock(hdl->lock);
  580. out:
  581. return ret;
  582. }
  583. static int chroot_write (struct shim_handle * hdl, const void * buf,
  584. size_t count)
  585. {
  586. int ret;
  587. if (count == 0)
  588. return 0;
  589. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  590. goto out;
  591. }
  592. struct shim_file_handle * file = &hdl->info.file;
  593. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  594. ret = map_write(hdl, buf, count);
  595. if (ret != -EACCES)
  596. goto out;
  597. lock(hdl->lock);
  598. file->buf_type = FILEBUF_NONE;
  599. } else {
  600. lock(hdl->lock);
  601. }
  602. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  603. -PAL_ERRNO;
  604. if (ret > 0)
  605. file->marker += ret;
  606. unlock(hdl->lock);
  607. out:
  608. return ret;
  609. }
  610. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  611. int prot, int flags, off_t offset)
  612. {
  613. int ret;
  614. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  615. return ret;
  616. int pal_prot = PAL_PROT(prot, flags);
  617. #if MAP_FILE == 0
  618. if (flags & MAP_ANONYMOUS)
  619. #else
  620. if (!(flags & MAP_FILE))
  621. #endif
  622. return -EINVAL;
  623. void * alloc_addr =
  624. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  625. if (!alloc_addr)
  626. return -PAL_ERRNO;
  627. *addr = alloc_addr;
  628. return 0;
  629. }
  630. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  631. {
  632. int ret = -EINVAL;
  633. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  634. return ret;
  635. struct shim_file_handle * file = &hdl->info.file;
  636. lock(hdl->lock);
  637. int marker = file->marker;
  638. int size = file->size;
  639. if (check_version(hdl)) {
  640. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  641. if (data->type != FILE_REGULAR) {
  642. ret = -ESPIPE;
  643. goto out;
  644. }
  645. }
  646. switch (wence) {
  647. case SEEK_SET:
  648. if (offset < 0)
  649. goto out;
  650. marker = offset;
  651. break;
  652. case SEEK_CUR:
  653. marker += offset;
  654. break;
  655. case SEEK_END:
  656. marker = size + offset;
  657. break;
  658. }
  659. ret = file->marker = marker;
  660. out:
  661. unlock(hdl->lock);
  662. return ret;
  663. }
  664. static int chroot_truncate (struct shim_handle * hdl, int len)
  665. {
  666. int ret = 0;
  667. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  668. return ret;
  669. struct shim_file_handle * file = &hdl->info.file;
  670. lock(hdl->lock);
  671. file->size = len;
  672. if (check_version(hdl)) {
  673. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  674. atomic_set(&data->size, len);
  675. }
  676. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len)
  677. goto out;
  678. if (file->marker > len)
  679. file->marker = len;
  680. out:
  681. unlock(hdl->lock);
  682. return ret;
  683. }
  684. static int chroot_dput (struct shim_dentry * dent)
  685. {
  686. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  687. if (data) {
  688. __destroy_data(data);
  689. dent->data = NULL;
  690. }
  691. return 0;
  692. }
  693. #define DEFAULT_DBUF_SIZE 1024
  694. static int chroot_readdir (struct shim_dentry * dent,
  695. struct shim_dirent ** dirent)
  696. {
  697. int ret;
  698. struct shim_file_data * data;
  699. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  700. return ret;
  701. chroot_update_ino(dent);
  702. assert(strpartcmp_static(qstrgetstr(&data->host_uri), "dir:"));
  703. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  704. PAL_ACCESS_RDONLY, 0, 0, 0);
  705. if (!pal_hdl)
  706. return -PAL_ERRNO;
  707. int buf_size = 0, new_size = MAX_PATH;
  708. int bytes;
  709. char * buf = NULL, * new_buf;
  710. int dbufsize = MAX_PATH;
  711. struct shim_dirent * dbuf = malloc(dbufsize);
  712. struct shim_dirent * d = dbuf, ** last = NULL;
  713. retry:
  714. new_buf = __alloca(new_size);
  715. if (buf)
  716. memcpy(new_buf, buf, buf_size);
  717. buf_size = new_size;
  718. buf = new_buf;
  719. while (1) {
  720. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  721. if (bytes == 0) {
  722. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  723. break;
  724. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  725. new_size = buf_size * 2;
  726. goto retry;
  727. }
  728. ret = -PAL_ERRNO;
  729. goto out;
  730. }
  731. char * b = buf, * next_b;
  732. int blen;
  733. while (b < buf + bytes) {
  734. blen = strlen(b);
  735. next_b = b + blen + 1;
  736. bool isdir = false;
  737. if (b[blen - 1] == '/') {
  738. isdir = true;
  739. b[blen - 1] = 0;
  740. blen--;
  741. }
  742. int dsize = sizeof(struct shim_dirent) + blen + 1;
  743. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  744. int newsize = dbufsize * 2;
  745. while ((void *) d + dsize > (void *) dbuf + newsize)
  746. newsize *= 2;
  747. struct shim_dirent * new_dbuf = malloc(newsize);
  748. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  749. struct shim_dirent * d1 = new_dbuf;
  750. struct shim_dirent * d2 = dbuf;
  751. while (d2 != d) {
  752. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  753. d1 = d1->next;
  754. d2 = d2->next;
  755. }
  756. free(dbuf);
  757. dbuf = new_dbuf;
  758. d = d1;
  759. dbufsize = newsize;
  760. }
  761. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  762. d->next = (void *) (d + 1) + blen + 1;
  763. d->ino = hash;
  764. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  765. memcpy(d->name, b, blen + 1);
  766. b = next_b;
  767. last = &d->next;
  768. d = d->next;
  769. }
  770. }
  771. if (!last) {
  772. free(dbuf);
  773. goto out;
  774. }
  775. *last = NULL;
  776. *dirent = dbuf;
  777. out:
  778. DkObjectClose(pal_hdl);
  779. return ret;
  780. }
  781. static int chroot_checkout (struct shim_handle * hdl)
  782. {
  783. if (hdl->fs == &chroot_builtin_fs)
  784. hdl->fs = NULL;
  785. if (hdl->type == TYPE_FILE) {
  786. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  787. if (data)
  788. hdl->info.file.data = NULL;
  789. }
  790. hdl->info.file.mapsize = 0;
  791. hdl->info.file.mapoffset = 0;
  792. hdl->info.file.mapbuf = NULL;
  793. hdl->pal_handle = NULL;
  794. return 0;
  795. }
  796. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  797. {
  798. struct mount_data * mdata = mount_data;
  799. *checkpoint = mount_data;
  800. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  801. }
  802. static int chroot_migrate (void * checkpoint, void ** mount_data)
  803. {
  804. struct mount_data * mdata = checkpoint;
  805. int alloc_len = mdata->root_uri_len +
  806. sizeof(struct mount_data) + 1;
  807. void * new_data = malloc(alloc_len);
  808. memcpy(new_data, mdata, alloc_len);
  809. *mount_data = new_data;
  810. return 0;
  811. }
  812. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  813. {
  814. int ret;
  815. struct shim_file_data * data;
  816. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  817. return ret;
  818. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  819. if (!pal_hdl)
  820. return -PAL_ERRNO;
  821. DkStreamDelete(pal_hdl, 0);
  822. DkObjectClose(pal_hdl);
  823. dent->mode = NO_MODE;
  824. data->mode = 0;
  825. atomic_inc(&data->version);
  826. atomic_set(&data->size, 0);
  827. return 0;
  828. }
  829. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  830. {
  831. int ret;
  832. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  833. return ret;
  834. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  835. size_t size = atomic_read(&data->size);
  836. if (poll_type == FS_POLL_SZ)
  837. return size;
  838. lock(hdl->lock);
  839. struct shim_file_handle * file = &hdl->info.file;
  840. if (check_version(hdl) &&
  841. file->size < size)
  842. file->size = size;
  843. int marker = file->marker;
  844. if (file->buf_type == FILEBUF_MAP) {
  845. ret = poll_type & FS_POLL_WR;
  846. if ((poll_type & FS_POLL_RD) && file->size > marker)
  847. ret |= FS_POLL_RD;
  848. goto out;
  849. }
  850. ret = -EAGAIN;
  851. out:
  852. unlock(hdl->lock);
  853. return ret;
  854. }
  855. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  856. {
  857. int ret;
  858. struct shim_file_data * old_data;
  859. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  860. return ret;
  861. struct shim_file_data * new_data;
  862. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  863. return ret;
  864. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  865. 0, 0, 0, 0);
  866. if (!pal_hdl)
  867. return -PAL_ERRNO;
  868. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  869. DkObjectClose(pal_hdl);
  870. return -PAL_ERRNO;
  871. }
  872. new->mode = new_data->mode = old_data->mode;
  873. old->mode = NO_MODE;
  874. old_data->mode = 0;
  875. DkObjectClose(pal_hdl);
  876. atomic_inc(&old_data->version);
  877. atomic_set(&old_data->size, 0);
  878. atomic_inc(&new_data->version);
  879. return 0;
  880. }
  881. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  882. {
  883. int ret;
  884. struct shim_file_data * data;
  885. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  886. return ret;
  887. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  888. if (!pal_hdl)
  889. return -PAL_ERRNO;
  890. PAL_STREAM_ATTR attr = { .share_flags = mode };
  891. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  892. DkObjectClose(pal_hdl);
  893. return -PAL_ERRNO;
  894. }
  895. DkObjectClose(pal_hdl);
  896. dent->mode = data->mode = mode;
  897. return 0;
  898. }
  899. struct shim_fs_ops chroot_fs_ops = {
  900. .mount = &chroot_mount,
  901. .unmount = &chroot_unmount,
  902. .flush = &chroot_flush,
  903. .close = &chroot_flush,
  904. .read = &chroot_read,
  905. .write = &chroot_write,
  906. .mmap = &chroot_mmap,
  907. .seek = &chroot_seek,
  908. .hstat = &chroot_hstat,
  909. .truncate = &chroot_truncate,
  910. .checkout = &chroot_checkout,
  911. .checkpoint = &chroot_checkpoint,
  912. .migrate = &chroot_migrate,
  913. .poll = &chroot_poll,
  914. };
  915. struct shim_d_ops chroot_d_ops = {
  916. .open = &chroot_open,
  917. .mode = &chroot_mode,
  918. .lookup = &chroot_lookup,
  919. .creat = &chroot_creat,
  920. .mkdir = &chroot_mkdir,
  921. .stat = &chroot_stat,
  922. .dput = &chroot_dput,
  923. .readdir = &chroot_readdir,
  924. .unlink = &chroot_unlink,
  925. .rename = &chroot_rename,
  926. .chmod = &chroot_chmod,
  927. };
  928. struct mount_data chroot_data = { .root_uri_len = 5,
  929. .root_uri = "file:", };
  930. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  931. .fs_ops = &chroot_fs_ops,
  932. .d_ops = &chroot_d_ops,
  933. .data = &chroot_data, };