fs.c 34 KB

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