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 count %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. bkeep_munmap(mapbuf, mapsize, VMA_INTERNAL);
  506. }
  507. }
  508. return 0;
  509. }
  510. static inline int __map_buffer (struct shim_handle * hdl, int size)
  511. {
  512. struct shim_file_handle * file = &hdl->info.file;
  513. if (file->mapbuf) {
  514. if (file->marker >= file->mapoffset &&
  515. file->marker + size <= file->mapoffset + file->mapsize)
  516. return 0;
  517. DkStreamUnmap(file->mapbuf, file->mapsize);
  518. bkeep_munmap(file->mapbuf, file->mapsize, VMA_INTERNAL);
  519. file->mapbuf = NULL;
  520. file->mapoffset = 0;
  521. }
  522. /* second, reallocate the buffer */
  523. uint64_t bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  524. int prot = PAL_PROT_READ;
  525. uint64_t mapoff = file->marker & ~(bufsize - 1);
  526. uint64_t maplen = bufsize;
  527. if (hdl->acc_mode & MAY_WRITE)
  528. prot |= PAL_PROT_WRITE;
  529. while (mapoff + maplen < file->marker + size)
  530. maplen *= 2;
  531. void * mapbuf =
  532. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  533. if (!mapbuf)
  534. return -PAL_ERRNO;
  535. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  536. hdl, mapoff, NULL);
  537. file->mapbuf = mapbuf;
  538. file->mapoffset = mapoff;
  539. file->mapsize = maplen;
  540. return 0;
  541. }
  542. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  543. {
  544. struct shim_file_handle * file = &hdl->info.file;
  545. int ret = 0;
  546. lock(hdl->lock);
  547. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  548. uint64_t size = atomic_read(&data->size);
  549. if (check_version(hdl) &&
  550. file->size < size)
  551. file->size = size;
  552. uint64_t marker = file->marker;
  553. if (marker >= file->size) {
  554. count = 0;
  555. goto out;
  556. }
  557. if ((ret = __map_buffer(hdl, count)) < 0) {
  558. unlock(hdl->lock);
  559. return ret;
  560. }
  561. if (marker + count > file->size)
  562. count = file->size - marker;
  563. if (count) {
  564. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  565. file->marker = marker + count;
  566. }
  567. out:
  568. unlock(hdl->lock);
  569. return count;
  570. }
  571. static int map_write (struct shim_handle * hdl, const void * buf,
  572. size_t count)
  573. {
  574. struct shim_file_handle * file = &hdl->info.file;
  575. int ret = 0;
  576. lock(hdl->lock);
  577. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  578. uint64_t marker = file->marker;
  579. if (file->marker + count > file->size) {
  580. file->size = file->marker + count;
  581. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL);
  582. if (!ret) {
  583. ret = -PAL_ERRNO;
  584. goto out;
  585. }
  586. if (ret < count) {
  587. file->size -= count - ret;
  588. }
  589. if (check_version(hdl)) {
  590. uint64_t size;
  591. do {
  592. if ((size = atomic_read(&data->size)) >= file->size) {
  593. file->size = size;
  594. break;
  595. }
  596. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  597. }
  598. file->marker = marker + ret;
  599. goto out;
  600. }
  601. if ((ret = __map_buffer(hdl, count)) < 0)
  602. goto out;
  603. if (count) {
  604. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  605. file->marker = marker + count;
  606. }
  607. ret = count;
  608. out:
  609. unlock(hdl->lock);
  610. return ret;
  611. }
  612. static int chroot_read (struct shim_handle * hdl, void * buf,
  613. size_t count)
  614. {
  615. int ret = 0;
  616. if (count == 0)
  617. goto out;
  618. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  619. goto out;
  620. }
  621. struct shim_file_handle * file = &hdl->info.file;
  622. if (file->buf_type == FILEBUF_MAP) {
  623. ret = map_read(hdl, buf, count);
  624. if (ret != -EACCES)
  625. goto out;
  626. lock(hdl->lock);
  627. file->buf_type = FILEBUF_NONE;
  628. } else {
  629. lock(hdl->lock);
  630. }
  631. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  632. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  633. if (ret > 0)
  634. file->marker += ret;
  635. unlock(hdl->lock);
  636. out:
  637. return ret;
  638. }
  639. static int chroot_write (struct shim_handle * hdl, const void * buf,
  640. size_t count)
  641. {
  642. int ret;
  643. if (count == 0)
  644. return 0;
  645. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  646. goto out;
  647. }
  648. struct shim_file_handle * file = &hdl->info.file;
  649. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  650. ret = map_write(hdl, buf, count);
  651. if (ret != -EACCES)
  652. goto out;
  653. lock(hdl->lock);
  654. file->buf_type = FILEBUF_NONE;
  655. } else {
  656. lock(hdl->lock);
  657. }
  658. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, (void *) buf, NULL) ? :
  659. -PAL_ERRNO;
  660. if (ret > 0)
  661. file->marker += ret;
  662. unlock(hdl->lock);
  663. out:
  664. return ret;
  665. }
  666. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  667. int prot, int flags, off_t offset)
  668. {
  669. int ret;
  670. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  671. return ret;
  672. int pal_prot = PAL_PROT(prot, flags);
  673. #if MAP_FILE == 0
  674. if (flags & MAP_ANONYMOUS)
  675. #else
  676. if (!(flags & MAP_FILE))
  677. #endif
  678. return -EINVAL;
  679. void * alloc_addr =
  680. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  681. if (!alloc_addr)
  682. return -PAL_ERRNO;
  683. *addr = alloc_addr;
  684. return 0;
  685. }
  686. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  687. {
  688. int ret = -EINVAL;
  689. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  690. return ret;
  691. struct shim_file_handle * file = &hdl->info.file;
  692. lock(hdl->lock);
  693. int marker = file->marker;
  694. int size = file->size;
  695. if (check_version(hdl)) {
  696. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  697. if (data->type != FILE_REGULAR) {
  698. ret = -ESPIPE;
  699. goto out;
  700. }
  701. }
  702. switch (wence) {
  703. case SEEK_SET:
  704. if (offset < 0)
  705. goto out;
  706. marker = offset;
  707. break;
  708. case SEEK_CUR:
  709. marker += offset;
  710. break;
  711. case SEEK_END:
  712. marker = size + offset;
  713. break;
  714. }
  715. ret = file->marker = marker;
  716. out:
  717. unlock(hdl->lock);
  718. return ret;
  719. }
  720. static int chroot_truncate (struct shim_handle * hdl, uint64_t len)
  721. {
  722. uint64_t ret = 0;
  723. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  724. return ret;
  725. if (!(hdl->acc_mode & MAY_WRITE))
  726. return -EINVAL;
  727. struct shim_file_handle * file = &hdl->info.file;
  728. lock(hdl->lock);
  729. file->size = len;
  730. if (check_version(hdl)) {
  731. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  732. atomic_set(&data->size, len);
  733. }
  734. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len) {
  735. goto out;
  736. }
  737. // DEP 10/25/16: Truncate returns 0 on success, not the length
  738. ret = 0;
  739. if (file->marker > len)
  740. file->marker = len;
  741. out:
  742. unlock(hdl->lock);
  743. return ret;
  744. }
  745. static int chroot_dput (struct shim_dentry * dent)
  746. {
  747. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  748. if (data) {
  749. __destroy_data(data);
  750. dent->data = NULL;
  751. }
  752. return 0;
  753. }
  754. #define DEFAULT_DBUF_SIZE 1024
  755. static int chroot_readdir (struct shim_dentry * dent,
  756. struct shim_dirent ** dirent)
  757. {
  758. int ret;
  759. struct shim_file_data * data;
  760. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  761. return ret;
  762. chroot_update_ino(dent);
  763. assert(strpartcmp_static(qstrgetstr(&data->host_uri), "dir:"));
  764. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  765. PAL_ACCESS_RDONLY, 0, 0, 0);
  766. if (!pal_hdl)
  767. return -PAL_ERRNO;
  768. int buf_size = 0, new_size = MAX_PATH;
  769. int bytes;
  770. char * buf = NULL, * new_buf;
  771. int dbufsize = MAX_PATH;
  772. struct shim_dirent * dbuf = malloc(dbufsize);
  773. struct shim_dirent * d = dbuf, ** last = NULL;
  774. retry:
  775. new_buf = __alloca(new_size);
  776. if (buf)
  777. memcpy(new_buf, buf, buf_size);
  778. buf_size = new_size;
  779. buf = new_buf;
  780. while (1) {
  781. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  782. if (bytes == 0) {
  783. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  784. break;
  785. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  786. new_size = buf_size * 2;
  787. goto retry;
  788. }
  789. ret = -PAL_ERRNO;
  790. goto out;
  791. }
  792. char * b = buf, * next_b;
  793. int blen;
  794. while (b < buf + bytes) {
  795. blen = strlen(b);
  796. next_b = b + blen + 1;
  797. bool isdir = false;
  798. if (b[blen - 1] == '/') {
  799. isdir = true;
  800. b[blen - 1] = 0;
  801. blen--;
  802. }
  803. int dsize = sizeof(struct shim_dirent) + blen + 1;
  804. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  805. int newsize = dbufsize * 2;
  806. while ((void *) d + dsize > (void *) dbuf + newsize)
  807. newsize *= 2;
  808. struct shim_dirent * new_dbuf = malloc(newsize);
  809. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  810. struct shim_dirent * d1 = new_dbuf;
  811. struct shim_dirent * d2 = dbuf;
  812. while (d2 != d) {
  813. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  814. d1 = d1->next;
  815. d2 = d2->next;
  816. }
  817. free(dbuf);
  818. dbuf = new_dbuf;
  819. d = d1;
  820. dbufsize = newsize;
  821. }
  822. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  823. d->next = (void *) (d + 1) + blen + 1;
  824. d->ino = hash;
  825. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  826. memcpy(d->name, b, blen + 1);
  827. b = next_b;
  828. last = &d->next;
  829. d = d->next;
  830. }
  831. }
  832. if (!last) {
  833. free(dbuf);
  834. goto out;
  835. }
  836. *last = NULL;
  837. *dirent = dbuf;
  838. out:
  839. DkObjectClose(pal_hdl);
  840. return ret;
  841. }
  842. static int chroot_checkout (struct shim_handle * hdl)
  843. {
  844. if (hdl->fs == &chroot_builtin_fs)
  845. hdl->fs = NULL;
  846. if (hdl->type == TYPE_FILE) {
  847. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  848. if (data)
  849. hdl->info.file.data = NULL;
  850. }
  851. if (hdl->pal_handle) {
  852. /*
  853. * Chia-Che 8/24/2017:
  854. * if the file still exists in the host, no need to send
  855. * the handle over RPC; otherwise, send it.
  856. */
  857. PAL_STREAM_ATTR attr;
  858. if (DkStreamAttributesQuery(qstrgetstr(&hdl->uri), &attr))
  859. hdl->pal_handle = NULL;
  860. }
  861. hdl->info.file.mapsize = 0;
  862. hdl->info.file.mapoffset = 0;
  863. hdl->info.file.mapbuf = NULL;
  864. return 0;
  865. }
  866. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  867. {
  868. struct mount_data * mdata = mount_data;
  869. *checkpoint = mount_data;
  870. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  871. }
  872. static int chroot_migrate (void * checkpoint, void ** mount_data)
  873. {
  874. struct mount_data * mdata = checkpoint;
  875. int alloc_len = mdata->root_uri_len +
  876. sizeof(struct mount_data) + 1;
  877. void * new_data = malloc(alloc_len);
  878. memcpy(new_data, mdata, alloc_len);
  879. *mount_data = new_data;
  880. return 0;
  881. }
  882. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  883. {
  884. int ret;
  885. struct shim_file_data * data;
  886. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  887. return ret;
  888. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  889. if (!pal_hdl)
  890. return -PAL_ERRNO;
  891. DkStreamDelete(pal_hdl, 0);
  892. DkObjectClose(pal_hdl);
  893. dent->mode = NO_MODE;
  894. data->mode = 0;
  895. atomic_inc(&data->version);
  896. atomic_set(&data->size, 0);
  897. /* Drop the parent's link count */
  898. struct shim_file_data *parent_data = FILE_DENTRY_DATA(dir);
  899. if (parent_data) {
  900. lock(parent_data->lock);
  901. if (parent_data->queried)
  902. parent_data->nlink--;
  903. unlock(parent_data->lock);
  904. }
  905. return 0;
  906. }
  907. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  908. {
  909. int ret;
  910. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  911. return ret;
  912. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  913. size_t size = atomic_read(&data->size);
  914. if (poll_type == FS_POLL_SZ)
  915. return size;
  916. lock(hdl->lock);
  917. struct shim_file_handle * file = &hdl->info.file;
  918. if (check_version(hdl) &&
  919. file->size < size)
  920. file->size = size;
  921. int marker = file->marker;
  922. if (file->buf_type == FILEBUF_MAP) {
  923. ret = poll_type & FS_POLL_WR;
  924. if ((poll_type & FS_POLL_RD) && file->size > marker)
  925. ret |= FS_POLL_RD;
  926. goto out;
  927. }
  928. ret = -EAGAIN;
  929. out:
  930. unlock(hdl->lock);
  931. return ret;
  932. }
  933. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  934. {
  935. int ret;
  936. struct shim_file_data * old_data;
  937. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  938. return ret;
  939. struct shim_file_data * new_data;
  940. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  941. return ret;
  942. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  943. 0, 0, 0, 0);
  944. if (!pal_hdl)
  945. return -PAL_ERRNO;
  946. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  947. DkObjectClose(pal_hdl);
  948. return -PAL_ERRNO;
  949. }
  950. new->mode = new_data->mode = old_data->mode;
  951. old->mode = NO_MODE;
  952. old_data->mode = 0;
  953. DkObjectClose(pal_hdl);
  954. atomic_inc(&old_data->version);
  955. atomic_set(&old_data->size, 0);
  956. atomic_inc(&new_data->version);
  957. return 0;
  958. }
  959. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  960. {
  961. int ret;
  962. struct shim_file_data * data;
  963. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  964. return ret;
  965. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  966. if (!pal_hdl)
  967. return -PAL_ERRNO;
  968. PAL_STREAM_ATTR attr = { .share_flags = mode };
  969. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  970. DkObjectClose(pal_hdl);
  971. return -PAL_ERRNO;
  972. }
  973. DkObjectClose(pal_hdl);
  974. dent->mode = data->mode = mode;
  975. return 0;
  976. }
  977. struct shim_fs_ops chroot_fs_ops = {
  978. .mount = &chroot_mount,
  979. .unmount = &chroot_unmount,
  980. .flush = &chroot_flush,
  981. .close = &chroot_flush,
  982. .read = &chroot_read,
  983. .write = &chroot_write,
  984. .mmap = &chroot_mmap,
  985. .seek = &chroot_seek,
  986. .hstat = &chroot_hstat,
  987. .truncate = &chroot_truncate,
  988. .checkout = &chroot_checkout,
  989. .checkpoint = &chroot_checkpoint,
  990. .migrate = &chroot_migrate,
  991. .poll = &chroot_poll,
  992. };
  993. struct shim_d_ops chroot_d_ops = {
  994. .open = &chroot_open,
  995. .mode = &chroot_mode,
  996. .lookup = &chroot_lookup,
  997. .creat = &chroot_creat,
  998. .mkdir = &chroot_mkdir,
  999. .stat = &chroot_stat,
  1000. .dput = &chroot_dput,
  1001. .readdir = &chroot_readdir,
  1002. .unlink = &chroot_unlink,
  1003. .rename = &chroot_rename,
  1004. .chmod = &chroot_chmod,
  1005. };
  1006. struct mount_data chroot_data = { .root_uri_len = 5,
  1007. .root_uri = "file:", };
  1008. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  1009. .fs_ops = &chroot_fs_ops,
  1010. .d_ops = &chroot_d_ops,
  1011. .data = &chroot_data, };