fs.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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 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; 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.pending_size);
  191. if (data->type == FILE_DIR) {
  192. int ret;
  193. /* Move up the uri update; need to convert manifest-level file:
  194. * directives to 'dir:' uris */
  195. if (old_type != FILE_DIR) {
  196. dent->state |= DENTRY_ISDIRECTORY;
  197. if ((ret = make_uri(dent)) < 0) {
  198. unlock(data->lock);
  199. return ret;
  200. }
  201. }
  202. /* DEP 3/18/17: If we have a directory, we need to find out how many
  203. * children it has by hand. */
  204. /* XXX: Keep coherent with rmdir/mkdir/creat, etc */
  205. struct shim_dirent *d, *dbuf = NULL;
  206. int nlink = 0;
  207. int rv = chroot_readdir(dent, &dbuf);
  208. if (rv != 0)
  209. return rv;
  210. if (dbuf) {
  211. for (d = dbuf; d; d = d->next)
  212. nlink++;
  213. free(dbuf);
  214. debug("Querying a directory; I count %d links.\n", nlink);
  215. } else
  216. nlink = 2; // Educated guess...
  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, NULL);
  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, int 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. debug("Stat: Returning link cound %d\n", stat->st_nlink);
  297. }
  298. unlock(data->lock);
  299. return 0;
  300. }
  301. static int chroot_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  302. {
  303. if (!force)
  304. return -ESKIPPED;
  305. return query_dentry(dent, NULL, mode, NULL);
  306. }
  307. static int chroot_stat (struct shim_dentry * dent, struct stat * statbuf)
  308. {
  309. return query_dentry(dent, NULL, NULL, statbuf);
  310. }
  311. static int chroot_lookup (struct shim_dentry * dent, bool force)
  312. {
  313. if (!force)
  314. return -ESKIPPED;
  315. if (dent->fs && dent == dent->fs->root)
  316. return 0;
  317. return query_dentry(dent, NULL, NULL, NULL);
  318. }
  319. static int __chroot_open (struct shim_dentry * dent,
  320. const char * uri, int len, int flags, mode_t mode,
  321. struct shim_handle * hdl,
  322. struct shim_file_data * data)
  323. {
  324. int ret = 0;
  325. if (!uri) {
  326. uri = qstrgetstr(&data->host_uri);
  327. len = data->host_uri.len;
  328. }
  329. int version = atomic_read(&data->version);
  330. int oldmode = flags & O_ACCMODE;
  331. int accmode = oldmode;
  332. int creat = flags & PAL_CREAT_MASK;
  333. int option = flags & PAL_OPTION_MASK;
  334. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  335. && accmode == O_WRONLY)
  336. accmode = O_RDWR;
  337. PAL_HANDLE palhdl;
  338. if (hdl && hdl->pal_handle) {
  339. palhdl = hdl->pal_handle;
  340. } else {
  341. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  342. if (!palhdl) {
  343. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  344. accmode != oldmode)
  345. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  346. if (!palhdl)
  347. return -PAL_ERRNO;
  348. }
  349. }
  350. if (!data->queried) {
  351. lock(data->lock);
  352. ret = __query_attr(dent, data, palhdl);
  353. unlock(data->lock);
  354. }
  355. if (!hdl) {
  356. DkObjectClose(palhdl);
  357. return 0;
  358. }
  359. hdl->pal_handle = palhdl;
  360. hdl->info.file.type = data->type;
  361. hdl->info.file.version = version;
  362. hdl->info.file.size = atomic_read(&data->size);
  363. hdl->info.file.data = data;
  364. return ret;
  365. }
  366. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  367. int flags)
  368. {
  369. int ret = 0;
  370. struct shim_file_data * data;
  371. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  372. return ret;
  373. if ((ret = __chroot_open(dent, NULL, 0, flags, dent->mode, hdl, data)) < 0)
  374. return ret;
  375. struct shim_file_handle * file = &hdl->info.file;
  376. int size = atomic_read(&data->size);
  377. /* initialize hdl, does not need a lock because no one is sharing */
  378. hdl->type = TYPE_FILE;
  379. file->marker = (flags & O_APPEND) ? size : 0;
  380. file->size = size;
  381. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  382. hdl->flags = flags;
  383. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  384. qstrcopy(&hdl->uri, &data->host_uri);
  385. return 0;
  386. }
  387. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  388. struct shim_dentry * dent, int flags, mode_t mode)
  389. {
  390. int ret = 0;
  391. struct shim_file_data * data;
  392. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  393. return ret;
  394. if ((ret = __chroot_open(dent, NULL, 0, flags|O_CREAT|O_EXCL, mode, hdl,
  395. data)) < 0)
  396. return ret;
  397. if (!hdl)
  398. return 0;
  399. struct shim_file_handle * file = &hdl->info.file;
  400. int size = atomic_read(&data->size);
  401. /* initialize hdl, does not need a lock because no one is sharing */
  402. hdl->type = TYPE_FILE;
  403. file->marker = (flags & O_APPEND) ? size : 0;
  404. file->size = size;
  405. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  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, 0, 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. int 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. return __chroot_open(hdl->dentry, uri, len, hdl->flags, 0, hdl, data);
  463. }
  464. static inline bool check_version (struct shim_handle * hdl)
  465. {
  466. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  467. == hdl->info.file.version;
  468. }
  469. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  470. {
  471. int ret;
  472. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  473. return ret;
  474. if (!check_version(hdl) || !hdl->dentry) {
  475. struct shim_file_handle * file = &hdl->info.file;
  476. struct shim_dentry * dent = hdl->dentry;
  477. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  478. if (dent)
  479. chroot_update_ino(dent);
  480. if (stat) {
  481. memset(stat, 0, sizeof(struct stat));
  482. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  483. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  484. stat->st_size = file->size;
  485. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  486. }
  487. return 0;
  488. }
  489. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  490. }
  491. static int chroot_flush (struct shim_handle * hdl)
  492. {
  493. struct shim_file_handle * file = &hdl->info.file;
  494. if (file->buf_type == FILEBUF_MAP) {
  495. lock(hdl->lock);
  496. void * mapbuf = file->mapbuf;
  497. int mapsize = file->mapsize;
  498. file->mapoffset = 0;
  499. file->mapbuf = NULL;
  500. unlock(hdl->lock);
  501. if (mapbuf) {
  502. DkStreamUnmap(mapbuf, mapsize);
  503. int flags = VMA_INTERNAL;
  504. bkeep_munmap(mapbuf, mapsize, &flags);
  505. }
  506. }
  507. return 0;
  508. }
  509. static inline int __map_buffer (struct shim_handle * hdl, int size)
  510. {
  511. struct shim_file_handle * file = &hdl->info.file;
  512. if (file->mapbuf) {
  513. if (file->marker >= file->mapoffset &&
  514. file->marker + size <= file->mapoffset + file->mapsize)
  515. return 0;
  516. DkStreamUnmap(file->mapbuf, file->mapsize);
  517. int flags = VMA_INTERNAL;
  518. bkeep_munmap(file->mapbuf, file->mapsize, &flags);
  519. file->mapbuf = NULL;
  520. file->mapoffset = 0;
  521. }
  522. /* second, reallocate the buffer */
  523. int bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  524. int prot = PAL_PROT_READ;
  525. unsigned long mapoff = file->marker & ~(bufsize - 1);
  526. unsigned long maplen = bufsize;
  527. if (hdl->acc_mode & MAY_WRITE)
  528. prot |= PAL_PROT_WRITE;
  529. while (mapoff + maplen < file->marker + size)
  530. maplen *= 2;
  531. void * mapbuf =
  532. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  533. if (!mapbuf)
  534. return -PAL_ERRNO;
  535. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  536. hdl, mapoff, NULL);
  537. file->mapbuf = mapbuf;
  538. file->mapoffset = mapoff;
  539. file->mapsize = maplen;
  540. return 0;
  541. }
  542. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  543. {
  544. struct shim_file_handle * file = &hdl->info.file;
  545. int ret = 0;
  546. lock(hdl->lock);
  547. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  548. unsigned int size = atomic_read(&data->size);
  549. if (check_version(hdl) &&
  550. file->size < size)
  551. file->size = size;
  552. int marker = file->marker;
  553. if (marker >= file->size) {
  554. count = 0;
  555. goto out;
  556. }
  557. if ((ret = __map_buffer(hdl, count)) < 0) {
  558. unlock(hdl->lock);
  559. return ret;
  560. }
  561. if (marker + count > file->size)
  562. count = file->size - marker;
  563. if (count) {
  564. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  565. file->marker = marker + count;
  566. }
  567. out:
  568. unlock(hdl->lock);
  569. return count;
  570. }
  571. static int map_write (struct shim_handle * hdl, const void * buf,
  572. size_t count)
  573. {
  574. struct shim_file_handle * file = &hdl->info.file;
  575. int ret = 0;
  576. lock(hdl->lock);
  577. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  578. int marker = file->marker;
  579. if (file->marker + count > file->size) {
  580. file->size = file->marker + count;
  581. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  582. if (!ret) {
  583. ret = -PAL_ERRNO;
  584. goto out;
  585. }
  586. if (ret < count)
  587. file->size -= count - ret;
  588. if (check_version(hdl)) {
  589. int size;
  590. do {
  591. if ((size = atomic_read(&data->size)) >= file->size) {
  592. file->size = size;
  593. break;
  594. }
  595. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  596. }
  597. file->marker = marker + ret;
  598. goto out;
  599. }
  600. if ((ret = __map_buffer(hdl, count)) < 0)
  601. goto out;
  602. if (count) {
  603. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  604. file->marker = marker + count;
  605. }
  606. ret = count;
  607. out:
  608. unlock(hdl->lock);
  609. return ret;
  610. }
  611. static int chroot_read (struct shim_handle * hdl, void * buf,
  612. size_t count)
  613. {
  614. int ret = 0;
  615. if (count == 0)
  616. goto out;
  617. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  618. goto out;
  619. }
  620. struct shim_file_handle * file = &hdl->info.file;
  621. if (file->buf_type == FILEBUF_MAP) {
  622. ret = map_read(hdl, buf, count);
  623. if (ret != -EACCES)
  624. goto out;
  625. lock(hdl->lock);
  626. file->buf_type = FILEBUF_NONE;
  627. } else {
  628. lock(hdl->lock);
  629. }
  630. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  631. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  632. if (ret > 0)
  633. file->marker += ret;
  634. unlock(hdl->lock);
  635. out:
  636. return ret;
  637. }
  638. static int chroot_write (struct shim_handle * hdl, const void * buf,
  639. size_t count)
  640. {
  641. int ret;
  642. if (count == 0)
  643. return 0;
  644. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  645. goto out;
  646. }
  647. struct shim_file_handle * file = &hdl->info.file;
  648. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  649. ret = map_write(hdl, buf, count);
  650. if (ret != -EACCES)
  651. goto out;
  652. lock(hdl->lock);
  653. file->buf_type = FILEBUF_NONE;
  654. } else {
  655. lock(hdl->lock);
  656. }
  657. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  658. -PAL_ERRNO;
  659. if (ret > 0)
  660. file->marker += ret;
  661. unlock(hdl->lock);
  662. out:
  663. return ret;
  664. }
  665. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  666. int prot, int flags, off_t offset)
  667. {
  668. int ret;
  669. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  670. return ret;
  671. int pal_prot = PAL_PROT(prot, flags);
  672. #if MAP_FILE == 0
  673. if (flags & MAP_ANONYMOUS)
  674. #else
  675. if (!(flags & MAP_FILE))
  676. #endif
  677. return -EINVAL;
  678. void * alloc_addr =
  679. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  680. if (!alloc_addr)
  681. return -PAL_ERRNO;
  682. *addr = alloc_addr;
  683. return 0;
  684. }
  685. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  686. {
  687. int ret = -EINVAL;
  688. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  689. return ret;
  690. struct shim_file_handle * file = &hdl->info.file;
  691. lock(hdl->lock);
  692. int marker = file->marker;
  693. int size = file->size;
  694. if (check_version(hdl)) {
  695. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  696. if (data->type != FILE_REGULAR) {
  697. ret = -ESPIPE;
  698. goto out;
  699. }
  700. }
  701. switch (wence) {
  702. case SEEK_SET:
  703. if (offset < 0)
  704. goto out;
  705. marker = offset;
  706. break;
  707. case SEEK_CUR:
  708. marker += offset;
  709. break;
  710. case SEEK_END:
  711. marker = size + offset;
  712. break;
  713. }
  714. ret = file->marker = marker;
  715. out:
  716. unlock(hdl->lock);
  717. return ret;
  718. }
  719. static int chroot_truncate (struct shim_handle * hdl, uint64_t len)
  720. {
  721. uint64_t ret = 0;
  722. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  723. return ret;
  724. struct shim_file_handle * file = &hdl->info.file;
  725. lock(hdl->lock);
  726. file->size = len;
  727. if (check_version(hdl)) {
  728. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  729. atomic_set(&data->size, len);
  730. }
  731. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len) {
  732. goto out;
  733. }
  734. // DEP 10/25/16: Truncate returns 0 on success, not the length
  735. ret = 0;
  736. if (file->marker > len)
  737. file->marker = len;
  738. out:
  739. unlock(hdl->lock);
  740. return ret;
  741. }
  742. static int chroot_dput (struct shim_dentry * dent)
  743. {
  744. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  745. if (data) {
  746. __destroy_data(data);
  747. dent->data = NULL;
  748. }
  749. return 0;
  750. }
  751. #define DEFAULT_DBUF_SIZE 1024
  752. static int chroot_readdir (struct shim_dentry * dent,
  753. struct shim_dirent ** dirent)
  754. {
  755. int ret;
  756. struct shim_file_data * data;
  757. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  758. return ret;
  759. chroot_update_ino(dent);
  760. assert(strpartcmp_static(qstrgetstr(&data->host_uri), "dir:"));
  761. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  762. PAL_ACCESS_RDONLY, 0, 0, 0);
  763. if (!pal_hdl)
  764. return -PAL_ERRNO;
  765. int buf_size = 0, new_size = MAX_PATH;
  766. int bytes;
  767. char * buf = NULL, * new_buf;
  768. int dbufsize = MAX_PATH;
  769. struct shim_dirent * dbuf = malloc(dbufsize);
  770. struct shim_dirent * d = dbuf, ** last = NULL;
  771. retry:
  772. new_buf = __alloca(new_size);
  773. if (buf)
  774. memcpy(new_buf, buf, buf_size);
  775. buf_size = new_size;
  776. buf = new_buf;
  777. while (1) {
  778. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  779. if (bytes == 0) {
  780. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  781. break;
  782. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  783. new_size = buf_size * 2;
  784. goto retry;
  785. }
  786. ret = -PAL_ERRNO;
  787. goto out;
  788. }
  789. char * b = buf, * next_b;
  790. int blen;
  791. while (b < buf + bytes) {
  792. blen = strlen(b);
  793. next_b = b + blen + 1;
  794. bool isdir = false;
  795. if (b[blen - 1] == '/') {
  796. isdir = true;
  797. b[blen - 1] = 0;
  798. blen--;
  799. }
  800. int dsize = sizeof(struct shim_dirent) + blen + 1;
  801. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  802. int newsize = dbufsize * 2;
  803. while ((void *) d + dsize > (void *) dbuf + newsize)
  804. newsize *= 2;
  805. struct shim_dirent * new_dbuf = malloc(newsize);
  806. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  807. struct shim_dirent * d1 = new_dbuf;
  808. struct shim_dirent * d2 = dbuf;
  809. while (d2 != d) {
  810. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  811. d1 = d1->next;
  812. d2 = d2->next;
  813. }
  814. free(dbuf);
  815. dbuf = new_dbuf;
  816. d = d1;
  817. dbufsize = newsize;
  818. }
  819. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  820. d->next = (void *) (d + 1) + blen + 1;
  821. d->ino = hash;
  822. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  823. memcpy(d->name, b, blen + 1);
  824. b = next_b;
  825. last = &d->next;
  826. d = d->next;
  827. }
  828. }
  829. if (!last) {
  830. free(dbuf);
  831. goto out;
  832. }
  833. *last = NULL;
  834. *dirent = dbuf;
  835. out:
  836. DkObjectClose(pal_hdl);
  837. return ret;
  838. }
  839. static int chroot_checkout (struct shim_handle * hdl)
  840. {
  841. if (hdl->fs == &chroot_builtin_fs)
  842. hdl->fs = NULL;
  843. if (hdl->type == TYPE_FILE) {
  844. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  845. if (data)
  846. hdl->info.file.data = NULL;
  847. }
  848. hdl->info.file.mapsize = 0;
  849. hdl->info.file.mapoffset = 0;
  850. hdl->info.file.mapbuf = NULL;
  851. hdl->pal_handle = NULL;
  852. return 0;
  853. }
  854. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  855. {
  856. struct mount_data * mdata = mount_data;
  857. *checkpoint = mount_data;
  858. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  859. }
  860. static int chroot_migrate (void * checkpoint, void ** mount_data)
  861. {
  862. struct mount_data * mdata = checkpoint;
  863. int alloc_len = mdata->root_uri_len +
  864. sizeof(struct mount_data) + 1;
  865. void * new_data = malloc(alloc_len);
  866. memcpy(new_data, mdata, alloc_len);
  867. *mount_data = new_data;
  868. return 0;
  869. }
  870. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  871. {
  872. int ret;
  873. struct shim_file_data * data;
  874. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  875. return ret;
  876. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  877. if (!pal_hdl)
  878. return -PAL_ERRNO;
  879. DkStreamDelete(pal_hdl, 0);
  880. DkObjectClose(pal_hdl);
  881. dent->mode = NO_MODE;
  882. data->mode = 0;
  883. atomic_inc(&data->version);
  884. atomic_set(&data->size, 0);
  885. /* Drop the parent's link count */
  886. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  887. if (parent_data) {
  888. lock(parent_data->lock);
  889. if (parent_data->queried)
  890. parent_data->nlink--;
  891. unlock(parent_data->lock);
  892. }
  893. return 0;
  894. }
  895. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  896. {
  897. int ret;
  898. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  899. return ret;
  900. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  901. size_t size = atomic_read(&data->size);
  902. if (poll_type == FS_POLL_SZ)
  903. return size;
  904. lock(hdl->lock);
  905. struct shim_file_handle * file = &hdl->info.file;
  906. if (check_version(hdl) &&
  907. file->size < size)
  908. file->size = size;
  909. int marker = file->marker;
  910. if (file->buf_type == FILEBUF_MAP) {
  911. ret = poll_type & FS_POLL_WR;
  912. if ((poll_type & FS_POLL_RD) && file->size > marker)
  913. ret |= FS_POLL_RD;
  914. goto out;
  915. }
  916. ret = -EAGAIN;
  917. out:
  918. unlock(hdl->lock);
  919. return ret;
  920. }
  921. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  922. {
  923. int ret;
  924. struct shim_file_data * old_data;
  925. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  926. return ret;
  927. struct shim_file_data * new_data;
  928. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  929. return ret;
  930. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  931. 0, 0, 0, 0);
  932. if (!pal_hdl)
  933. return -PAL_ERRNO;
  934. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  935. DkObjectClose(pal_hdl);
  936. return -PAL_ERRNO;
  937. }
  938. new->mode = new_data->mode = old_data->mode;
  939. old->mode = NO_MODE;
  940. old_data->mode = 0;
  941. DkObjectClose(pal_hdl);
  942. atomic_inc(&old_data->version);
  943. atomic_set(&old_data->size, 0);
  944. atomic_inc(&new_data->version);
  945. return 0;
  946. }
  947. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  948. {
  949. int ret;
  950. struct shim_file_data * data;
  951. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  952. return ret;
  953. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  954. if (!pal_hdl)
  955. return -PAL_ERRNO;
  956. PAL_STREAM_ATTR attr = { .share_flags = mode };
  957. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  958. DkObjectClose(pal_hdl);
  959. return -PAL_ERRNO;
  960. }
  961. DkObjectClose(pal_hdl);
  962. dent->mode = data->mode = mode;
  963. return 0;
  964. }
  965. struct shim_fs_ops chroot_fs_ops = {
  966. .mount = &chroot_mount,
  967. .unmount = &chroot_unmount,
  968. .flush = &chroot_flush,
  969. .close = &chroot_flush,
  970. .read = &chroot_read,
  971. .write = &chroot_write,
  972. .mmap = &chroot_mmap,
  973. .seek = &chroot_seek,
  974. .hstat = &chroot_hstat,
  975. .truncate = &chroot_truncate,
  976. .checkout = &chroot_checkout,
  977. .checkpoint = &chroot_checkpoint,
  978. .migrate = &chroot_migrate,
  979. .poll = &chroot_poll,
  980. };
  981. struct shim_d_ops chroot_d_ops = {
  982. .open = &chroot_open,
  983. .mode = &chroot_mode,
  984. .lookup = &chroot_lookup,
  985. .creat = &chroot_creat,
  986. .mkdir = &chroot_mkdir,
  987. .stat = &chroot_stat,
  988. .dput = &chroot_dput,
  989. .readdir = &chroot_readdir,
  990. .unlink = &chroot_unlink,
  991. .rename = &chroot_rename,
  992. .chmod = &chroot_chmod,
  993. };
  994. struct mount_data chroot_data = { .root_uri_len = 5,
  995. .root_uri = "file:", };
  996. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  997. .fs_ops = &chroot_fs_ops,
  998. .d_ops = &chroot_d_ops,
  999. .data = &chroot_data, };