fs.c 34 KB

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