fs.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. struct shim_file_handle * file = &hdl->info.file;
  629. if (file->buf_type == FILEBUF_MAP) {
  630. ret = map_read(hdl, buf, count);
  631. if (ret != -EACCES)
  632. goto out;
  633. lock(hdl->lock);
  634. file->buf_type = FILEBUF_NONE;
  635. } else {
  636. lock(hdl->lock);
  637. }
  638. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  639. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  640. if (ret > 0)
  641. file->marker += ret;
  642. unlock(hdl->lock);
  643. out:
  644. return ret;
  645. }
  646. static int chroot_write (struct shim_handle * hdl, const void * buf,
  647. size_t count)
  648. {
  649. int ret;
  650. if (count == 0)
  651. return 0;
  652. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  653. goto out;
  654. }
  655. struct shim_file_handle * file = &hdl->info.file;
  656. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  657. ret = map_write(hdl, buf, count);
  658. if (ret != -EACCES)
  659. goto out;
  660. lock(hdl->lock);
  661. file->buf_type = FILEBUF_NONE;
  662. } else {
  663. lock(hdl->lock);
  664. }
  665. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  666. -PAL_ERRNO;
  667. if (ret > 0)
  668. file->marker += ret;
  669. unlock(hdl->lock);
  670. out:
  671. return ret;
  672. }
  673. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  674. int prot, int flags, off_t offset)
  675. {
  676. int ret;
  677. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  678. return ret;
  679. int pal_prot = PAL_PROT(prot, flags);
  680. #if MAP_FILE == 0
  681. if (flags & MAP_ANONYMOUS)
  682. #else
  683. if (!(flags & MAP_FILE))
  684. #endif
  685. return -EINVAL;
  686. void * alloc_addr =
  687. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  688. if (!alloc_addr)
  689. return -PAL_ERRNO;
  690. *addr = alloc_addr;
  691. return 0;
  692. }
  693. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  694. {
  695. int ret = -EINVAL;
  696. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  697. return ret;
  698. struct shim_file_handle * file = &hdl->info.file;
  699. lock(hdl->lock);
  700. int marker = file->marker;
  701. int size = file->size;
  702. if (check_version(hdl)) {
  703. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  704. if (data->type != FILE_REGULAR) {
  705. ret = -ESPIPE;
  706. goto out;
  707. }
  708. }
  709. switch (wence) {
  710. case SEEK_SET:
  711. if (offset < 0)
  712. goto out;
  713. marker = offset;
  714. break;
  715. case SEEK_CUR:
  716. marker += offset;
  717. break;
  718. case SEEK_END:
  719. marker = size + offset;
  720. break;
  721. }
  722. ret = file->marker = marker;
  723. out:
  724. unlock(hdl->lock);
  725. return ret;
  726. }
  727. static int chroot_truncate (struct shim_handle * hdl, uint64_t len)
  728. {
  729. uint64_t ret = 0;
  730. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  731. return ret;
  732. if (!(hdl->acc_mode & MAY_WRITE))
  733. return -EINVAL;
  734. struct shim_file_handle * file = &hdl->info.file;
  735. lock(hdl->lock);
  736. file->size = len;
  737. if (check_version(hdl)) {
  738. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  739. atomic_set(&data->size, len);
  740. }
  741. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len) {
  742. goto out;
  743. }
  744. // DEP 10/25/16: Truncate returns 0 on success, not the length
  745. ret = 0;
  746. if (file->marker > len)
  747. file->marker = len;
  748. out:
  749. unlock(hdl->lock);
  750. return ret;
  751. }
  752. static int chroot_dput (struct shim_dentry * dent)
  753. {
  754. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  755. if (data) {
  756. __destroy_data(data);
  757. dent->data = NULL;
  758. }
  759. return 0;
  760. }
  761. static int chroot_readdir (struct shim_dentry * dent,
  762. struct shim_dirent ** dirent)
  763. {
  764. struct shim_file_data * data;
  765. int ret;
  766. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  767. return ret;
  768. chroot_update_ino(dent);
  769. const char * uri = qstrgetstr(&data->host_uri);
  770. assert(strpartcmp_static(uri, "dir:"));
  771. PAL_HANDLE pal_hdl = DkStreamOpen(uri, PAL_ACCESS_RDONLY, 0, 0, 0);
  772. if (!pal_hdl)
  773. return -PAL_ERRNO;
  774. size_t buf_size = MAX_PATH, bytes = 0;
  775. char * buf = malloc(buf_size);
  776. if (!buf) {
  777. ret = -ENOMEM;
  778. goto out_hdl;
  779. }
  780. /*
  781. * Try to read the directory list from the host. DkStreamRead
  782. * does not accept offset for directory listing. Therefore, we retry
  783. * several times if the buffer is not large enough.
  784. */
  785. retry_read:
  786. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  787. if (!bytes) {
  788. ret = 0;
  789. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  790. goto out;
  791. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  792. char * new_buf = malloc(buf_size * 2);
  793. if (!new_buf) {
  794. ret = -ENOMEM;
  795. goto out;
  796. }
  797. free(buf);
  798. buf_size *= 2;
  799. buf = new_buf;
  800. goto retry_read;
  801. }
  802. ret = -PAL_ERRNO;
  803. goto out;
  804. }
  805. /* Now emitting the dirent data */
  806. size_t dbuf_size = MAX_PATH;
  807. struct shim_dirent * dbuf = malloc(dbuf_size);
  808. if (!dbuf)
  809. goto out;
  810. struct shim_dirent * d = dbuf, ** last = NULL;
  811. char * b = buf, * next_b;
  812. int blen;
  813. /* Scanning the directory names in the buffer */
  814. while (b < buf + bytes) {
  815. blen = strlen(b);
  816. next_b = b + blen + 1;
  817. bool isdir = false;
  818. /* The PAL convention: if the name is ended with "/",
  819. it is a directory. */
  820. if (b[blen - 1] == '/') {
  821. isdir = true;
  822. b[blen - 1] = 0;
  823. blen--;
  824. }
  825. /* Populating a dirent */
  826. int dsize = sizeof(struct shim_dirent) + blen + 1;
  827. /* dbuf is not large enough, reallocate the dirent buffer */
  828. if ((void *) d + dsize > (void *) dbuf + dbuf_size) {
  829. int newsize = dbuf_size * 2;
  830. while ((void *) d + dsize > (void *) dbuf + newsize)
  831. newsize *= 2;
  832. struct shim_dirent * new_dbuf = malloc(newsize);
  833. if (!new_dbuf) {
  834. ret = -ENOMEM;
  835. free(dbuf);
  836. goto out;
  837. }
  838. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  839. struct shim_dirent * d1 = new_dbuf;
  840. struct shim_dirent * d2 = dbuf;
  841. while (d2 != d) {
  842. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  843. d1 = d1->next;
  844. d2 = d2->next;
  845. }
  846. free(dbuf);
  847. dbuf = new_dbuf;
  848. d = d1;
  849. dbuf_size = newsize;
  850. }
  851. /* Fill up the dirent buffer */
  852. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  853. d->next = (void *) (d + 1) + blen + 1;
  854. d->ino = hash;
  855. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  856. memcpy(d->name, b, blen + 1);
  857. b = next_b;
  858. last = &d->next;
  859. d = d->next;
  860. }
  861. *last = NULL;
  862. *dirent = dbuf;
  863. out:
  864. free(buf);
  865. out_hdl:
  866. DkObjectClose(pal_hdl);
  867. return ret;
  868. }
  869. static int chroot_checkout (struct shim_handle * hdl)
  870. {
  871. if (hdl->fs == &chroot_builtin_fs)
  872. hdl->fs = NULL;
  873. if (hdl->type == TYPE_FILE) {
  874. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  875. if (data)
  876. hdl->info.file.data = NULL;
  877. }
  878. if (hdl->pal_handle) {
  879. /*
  880. * if the file still exists in the host, no need to send
  881. * the handle over RPC; otherwise, send it.
  882. */
  883. PAL_STREAM_ATTR attr;
  884. if (DkStreamAttributesQuery(qstrgetstr(&hdl->uri), &attr))
  885. hdl->pal_handle = NULL;
  886. }
  887. hdl->info.file.mapsize = 0;
  888. hdl->info.file.mapoffset = 0;
  889. hdl->info.file.mapbuf = NULL;
  890. return 0;
  891. }
  892. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  893. {
  894. struct mount_data * mdata = mount_data;
  895. *checkpoint = mount_data;
  896. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  897. }
  898. static int chroot_migrate (void * checkpoint, void ** mount_data)
  899. {
  900. struct mount_data * mdata = checkpoint;
  901. int alloc_len = mdata->root_uri_len +
  902. sizeof(struct mount_data) + 1;
  903. void * new_data = malloc(alloc_len);
  904. if (!new_data)
  905. return -ENOMEM;
  906. memcpy(new_data, mdata, alloc_len);
  907. *mount_data = new_data;
  908. return 0;
  909. }
  910. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  911. {
  912. int ret;
  913. struct shim_file_data * data;
  914. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  915. return ret;
  916. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  917. if (!pal_hdl)
  918. return -PAL_ERRNO;
  919. DkStreamDelete(pal_hdl, 0);
  920. DkObjectClose(pal_hdl);
  921. dent->mode = NO_MODE;
  922. data->mode = 0;
  923. atomic_inc(&data->version);
  924. atomic_set(&data->size, 0);
  925. /* Drop the parent's link count */
  926. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  927. if (parent_data) {
  928. lock(parent_data->lock);
  929. if (parent_data->queried)
  930. parent_data->nlink--;
  931. unlock(parent_data->lock);
  932. }
  933. return 0;
  934. }
  935. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  936. {
  937. int ret;
  938. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  939. return ret;
  940. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  941. size_t size = atomic_read(&data->size);
  942. if (poll_type == FS_POLL_SZ)
  943. return size;
  944. lock(hdl->lock);
  945. struct shim_file_handle * file = &hdl->info.file;
  946. if (check_version(hdl) &&
  947. file->size < size)
  948. file->size = size;
  949. int marker = file->marker;
  950. if (file->buf_type == FILEBUF_MAP) {
  951. ret = poll_type & FS_POLL_WR;
  952. if ((poll_type & FS_POLL_RD) && file->size > marker)
  953. ret |= FS_POLL_RD;
  954. goto out;
  955. }
  956. ret = -EAGAIN;
  957. out:
  958. unlock(hdl->lock);
  959. return ret;
  960. }
  961. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  962. {
  963. int ret;
  964. struct shim_file_data * old_data;
  965. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  966. return ret;
  967. struct shim_file_data * new_data;
  968. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  969. return ret;
  970. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  971. 0, 0, 0, 0);
  972. if (!pal_hdl)
  973. return -PAL_ERRNO;
  974. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  975. DkObjectClose(pal_hdl);
  976. return -PAL_ERRNO;
  977. }
  978. new->mode = new_data->mode = old_data->mode;
  979. old->mode = NO_MODE;
  980. old_data->mode = 0;
  981. DkObjectClose(pal_hdl);
  982. atomic_inc(&old_data->version);
  983. atomic_set(&old_data->size, 0);
  984. atomic_inc(&new_data->version);
  985. return 0;
  986. }
  987. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  988. {
  989. int ret;
  990. struct shim_file_data * data;
  991. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  992. return ret;
  993. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  994. if (!pal_hdl)
  995. return -PAL_ERRNO;
  996. PAL_STREAM_ATTR attr = { .share_flags = mode };
  997. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  998. DkObjectClose(pal_hdl);
  999. return -PAL_ERRNO;
  1000. }
  1001. DkObjectClose(pal_hdl);
  1002. dent->mode = data->mode = mode;
  1003. return 0;
  1004. }
  1005. struct shim_fs_ops chroot_fs_ops = {
  1006. .mount = &chroot_mount,
  1007. .unmount = &chroot_unmount,
  1008. .flush = &chroot_flush,
  1009. .close = &chroot_flush,
  1010. .read = &chroot_read,
  1011. .write = &chroot_write,
  1012. .mmap = &chroot_mmap,
  1013. .seek = &chroot_seek,
  1014. .hstat = &chroot_hstat,
  1015. .truncate = &chroot_truncate,
  1016. .checkout = &chroot_checkout,
  1017. .checkpoint = &chroot_checkpoint,
  1018. .migrate = &chroot_migrate,
  1019. .poll = &chroot_poll,
  1020. };
  1021. struct shim_d_ops chroot_d_ops = {
  1022. .open = &chroot_open,
  1023. .mode = &chroot_mode,
  1024. .lookup = &chroot_lookup,
  1025. .creat = &chroot_creat,
  1026. .mkdir = &chroot_mkdir,
  1027. .stat = &chroot_stat,
  1028. .dput = &chroot_dput,
  1029. .readdir = &chroot_readdir,
  1030. .unlink = &chroot_unlink,
  1031. .rename = &chroot_rename,
  1032. .chmod = &chroot_chmod,
  1033. };
  1034. struct mount_data chroot_data = { .root_uri_len = 5,
  1035. .root_uri = "file:", };
  1036. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  1037. .fs_ops = &chroot_fs_ops,
  1038. .d_ops = &chroot_d_ops,
  1039. .data = &chroot_data, };