fs.c 30 KB

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