fs.c 33 KB

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