fs.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 (!memcmp(uri, "file:", 5)) {
  54. type = FILE_UNKNOWN;
  55. uri += 5;
  56. } else if (!memcmp(uri, "dev:", 4)) {
  57. type = memcmp(uri + 4, "tty", 3) ? FILE_DEV : FILE_TTY;
  58. uri += 4;
  59. } else
  60. return -EINVAL;
  61. if (!(*uri))
  62. uri = ".";
  63. int uri_len = strlen(uri);
  64. int data_size = uri_len + 1 + sizeof(struct mount_data);
  65. struct mount_data * mdata = (struct mount_data *) malloc(data_size);
  66. mdata->data_size = data_size;
  67. mdata->base_type = type;
  68. mdata->ino_base = hash_path(uri, uri_len, NULL);
  69. mdata->root_uri_len = uri_len;
  70. memcpy(mdata->root_uri, uri, uri_len + 1);
  71. *mount_data = mdata;
  72. return 0;
  73. }
  74. static int chroot_unmount (void * mount_data)
  75. {
  76. free(mount_data);
  77. return 0;
  78. }
  79. static inline int concat_uri (char * buffer, int size, int type,
  80. const char * root, int root_len,
  81. const char * trim, int trim_len)
  82. {
  83. int len = 0;
  84. switch (type) {
  85. case FILE_UNKNOWN:
  86. case FILE_REGULAR:
  87. if (size < 7 + root_len + trim_len)
  88. return -ENAMETOOLONG;
  89. memcpy(buffer, "file:", 6);
  90. len += 5;
  91. break;
  92. case FILE_DIR:
  93. if (size < 6 + root_len + trim_len)
  94. return -ENAMETOOLONG;
  95. memcpy(buffer, "dir:", 5);
  96. len += 4;
  97. break;
  98. case FILE_DEV:
  99. case FILE_TTY:
  100. if (size < 6 + root_len + trim_len)
  101. return -ENAMETOOLONG;
  102. memcpy(buffer, "dev:", 5);
  103. len += 4;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. if (root_len) {
  109. memcpy(buffer + len, root, root_len + 1);
  110. len += root_len;
  111. }
  112. if (trim_len) {
  113. buffer[len++] = '/';
  114. memcpy(buffer + len, trim, trim_len + 1);
  115. len += trim_len;
  116. }
  117. return len;
  118. }
  119. /* simply just create data, sometimes it is individually called when the
  120. handle is not linked to a dentry */
  121. static struct shim_file_data * __create_data (void)
  122. {
  123. struct shim_file_data * data = malloc(sizeof(struct shim_file_data));
  124. if (!data)
  125. return NULL;
  126. memset(data, 0, sizeof(struct shim_file_data));
  127. create_lock(data->lock);
  128. return data;
  129. }
  130. static void __destroy_data (struct shim_file_data * data)
  131. {
  132. qstrfree(&data->host_uri);
  133. destroy_lock(data->lock);
  134. free(data);
  135. }
  136. static int make_uri (struct shim_dentry * dent)
  137. {
  138. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  139. assert(mdata);
  140. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  141. char * uri = __alloca(URI_MAX_SIZE);
  142. int len = concat_uri(uri, URI_MAX_SIZE, data->type,
  143. mdata->root_uri,
  144. mdata->root_uri_len,
  145. qstrgetstr(&dent->rel_path),
  146. dent->rel_path.len);
  147. if (len >= 0)
  148. qstrsetstr(&data->host_uri, uri, len);
  149. return len;
  150. }
  151. /* create a data in the dentry and compose it's uri. dent->lock needs to
  152. be held */
  153. static int create_data (struct shim_dentry * dent, const char * uri, int len)
  154. {
  155. if (dent->data)
  156. return 0;
  157. struct shim_file_data * data = __create_data();
  158. if (!data)
  159. return -ENOMEM;
  160. dent->data = data;
  161. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  162. assert(mdata);
  163. data->type = (dent->state & DENTRY_ISDIRECTORY) ?
  164. FILE_DIR : mdata->base_type;
  165. if (uri) {
  166. qstrsetstr(&data->host_uri, uri, len);
  167. } else {
  168. int ret = make_uri(dent);
  169. if (ret < 0)
  170. return ret;
  171. }
  172. atomic_set(&data->version, 0);
  173. return 0;
  174. }
  175. static int __query_attr (struct shim_file_data * data, PAL_HANDLE pal_handle)
  176. {
  177. PAL_STREAM_ATTR pal_attr;
  178. if (pal_handle ?
  179. !DkStreamAttributesQuerybyHandle(pal_handle, &pal_attr) :
  180. !DkStreamAttributesQuery(qstrgetstr(&data->host_uri), &pal_attr))
  181. return -PAL_ERRNO;
  182. /* need to correct the data type */
  183. if (data->type == FILE_UNKNOWN)
  184. switch (pal_attr.handle_type) {
  185. case pal_type_file: data->type = FILE_REGULAR; break;
  186. case pal_type_dir: data->type = FILE_DIR; break;
  187. case pal_type_dev: data->type = FILE_DEV; break;
  188. }
  189. data->mode = (pal_attr.readable ? S_IRUSR : 0) |
  190. (pal_attr.writeable ? S_IWUSR : 0) |
  191. (pal_attr.runnable ? S_IXUSR : 0);
  192. atomic_set(&data->size, pal_attr.pending_size);
  193. data->queried = true;
  194. return 0;
  195. }
  196. /* do not need any lock */
  197. static void chroot_update_ino (struct shim_dentry * dent)
  198. {
  199. if (dent->state & DENTRY_INO_UPDATED)
  200. return;
  201. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  202. unsigned long ino = mdata->ino_base;
  203. if (!qstrempty(&dent->rel_path))
  204. ino = rehash_path(mdata->ino_base, qstrgetstr(&dent->rel_path),
  205. dent->rel_path.len, NULL);
  206. dent->ino = ino;
  207. dent->state |= DENTRY_INO_UPDATED;
  208. }
  209. static inline int try_create_data (struct shim_dentry * dent,
  210. const char * uri, int len,
  211. struct shim_file_data ** dataptr)
  212. {
  213. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  214. if (!data) {
  215. lock(dent->lock);
  216. int ret = create_data(dent, uri, len);
  217. data = FILE_DENTRY_DATA(dent);
  218. unlock(dent->lock);
  219. if (ret < 0) {
  220. return ret;
  221. }
  222. }
  223. *dataptr = data;
  224. return 0;
  225. }
  226. static int query_dentry (struct shim_dentry * dent, PAL_HANDLE pal_handle,
  227. mode_t * mode, struct stat * stat)
  228. {
  229. int ret = 0;
  230. struct shim_file_data * data;
  231. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  232. return ret;
  233. lock(data->lock);
  234. enum shim_file_type old_type = data->type;
  235. if (!data->queried && (ret = __query_attr(data, pal_handle)) < 0) {
  236. unlock(data->lock);
  237. return ret;
  238. }
  239. if (data->type == FILE_DIR && old_type != FILE_DIR) {
  240. dent->state |= DENTRY_ISDIRECTORY;
  241. if ((ret = make_uri(dent)) < 0) {
  242. unlock(data->lock);
  243. return ret;
  244. }
  245. }
  246. if (mode)
  247. *mode = data->mode;
  248. if (stat) {
  249. struct mount_data * mdata = DENTRY_MOUNT_DATA(dent);
  250. chroot_update_ino(dent);
  251. memset(stat, 0, sizeof(struct stat));
  252. stat->st_mode = (mode_t) data->mode;
  253. stat->st_dev = (dev_t) mdata->ino_base;
  254. stat->st_ino = (ino_t) dent->ino;
  255. stat->st_size = (off_t) atomic_read(&data->size);
  256. stat->st_atime = (time_t) data->atime;
  257. stat->st_mtime = (time_t) data->mtime;
  258. stat->st_ctime = (time_t) data->ctime;
  259. switch (data->type) {
  260. case FILE_REGULAR: stat->st_mode |= S_IFREG; break;
  261. case FILE_DIR: stat->st_mode |= S_IFDIR; break;
  262. case FILE_DEV:
  263. case FILE_TTY: stat->st_mode |= S_IFCHR; break;
  264. default: break;
  265. }
  266. }
  267. unlock(data->lock);
  268. return 0;
  269. }
  270. static int chroot_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  271. {
  272. if (!force)
  273. return -ESKIPPED;
  274. return query_dentry(dent, NULL, mode, NULL);
  275. }
  276. static int chroot_stat (struct shim_dentry * dent, struct stat * statbuf)
  277. {
  278. return query_dentry(dent, NULL, NULL, statbuf);
  279. }
  280. static int chroot_lookup (struct shim_dentry * dent, bool force)
  281. {
  282. if (!force)
  283. return -ESKIPPED;
  284. if (dent->fs && dent == dent->fs->root)
  285. return 0;
  286. return query_dentry(dent, NULL, NULL, NULL);
  287. }
  288. static int __chroot_open (const char * uri, int len, int flags, mode_t mode,
  289. struct shim_handle * hdl,
  290. struct shim_file_data * data)
  291. {
  292. int ret = 0;
  293. if (!uri) {
  294. uri = qstrgetstr(&data->host_uri);
  295. len = data->host_uri.len;
  296. }
  297. int version = atomic_read(&data->version);
  298. int oldmode = flags & O_ACCMODE;
  299. int accmode = oldmode;
  300. int creat = flags & PAL_CREAT_MASK;
  301. int option = flags & PAL_OPTION_MASK;
  302. if ((data->type == FILE_REGULAR || data->type == FILE_UNKNOWN)
  303. && accmode == O_WRONLY)
  304. accmode = O_RDWR;
  305. PAL_HANDLE palhdl;
  306. if (hdl && hdl->pal_handle) {
  307. palhdl = hdl->pal_handle;
  308. } else {
  309. palhdl = DkStreamOpen(uri, accmode, mode, creat, option);
  310. if (!palhdl) {
  311. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED &&
  312. accmode != oldmode)
  313. palhdl = DkStreamOpen(uri, oldmode, mode, creat, option);
  314. if (!palhdl)
  315. return -PAL_ERRNO;
  316. }
  317. }
  318. if (!data->queried) {
  319. lock(data->lock);
  320. ret = __query_attr(data, palhdl);
  321. unlock(data->lock);
  322. }
  323. if (!hdl) {
  324. DkObjectClose(palhdl);
  325. return 0;
  326. }
  327. hdl->pal_handle = palhdl;
  328. hdl->info.file.type = data->type;
  329. hdl->info.file.version = version;
  330. hdl->info.file.size = atomic_read(&data->size);
  331. hdl->info.file.data = data;
  332. return ret;
  333. }
  334. static int chroot_open (struct shim_handle * hdl, struct shim_dentry * dent,
  335. int flags)
  336. {
  337. int ret = 0;
  338. struct shim_file_data * data;
  339. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  340. return ret;
  341. if ((ret = __chroot_open(NULL, 0, flags, dent->mode, hdl, data)) < 0)
  342. return ret;
  343. struct shim_file_handle * file = &hdl->info.file;
  344. int size = atomic_read(&data->size);
  345. /* initialize hdl, does not need a lock because no one is sharing */
  346. hdl->type = TYPE_FILE;
  347. file->marker = (flags & O_APPEND) ? size : 0;
  348. file->size = size;
  349. file->buf_type = (data->type == FILE_REGULAR) ? FILEBUF_MAP : FILEBUF_NONE;
  350. hdl->flags = flags;
  351. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  352. qstrcopy(&hdl->uri, &data->host_uri);
  353. return 0;
  354. }
  355. static int chroot_creat (struct shim_handle * hdl, struct shim_dentry * dir,
  356. struct shim_dentry * dent, int flags, mode_t mode)
  357. {
  358. int ret = 0;
  359. struct shim_file_data * data;
  360. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  361. return ret;
  362. if ((ret = __chroot_open(NULL, 0, flags|O_CREAT|O_EXCL, mode, hdl,
  363. data)) < 0)
  364. return ret;
  365. if (!hdl)
  366. return 0;
  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_mkdir (struct shim_dentry * dir, struct shim_dentry * dent,
  380. 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 (data->type != FILE_DIR) {
  387. data->type = FILE_DIR;
  388. int ret = make_uri(dent);
  389. if (ret < 0)
  390. return ret;
  391. }
  392. return __chroot_open(NULL, 0, O_CREAT|O_EXCL, mode, NULL, data);
  393. }
  394. #define NEED_RECREATE(hdl) (!FILE_HANDLE_DATA(hdl))
  395. static int chroot_recreate (struct shim_handle * hdl)
  396. {
  397. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  398. int ret = 0;
  399. /* quickly bail out if the data is created */
  400. if (data)
  401. return 0;
  402. const char * uri = qstrgetstr(&hdl->uri);
  403. int len = hdl->uri.len;
  404. if (hdl->dentry) {
  405. if ((ret = try_create_data(hdl->dentry, uri, len, &data)) < 0)
  406. return ret;
  407. } else {
  408. data = __create_data();
  409. if (!data)
  410. return -ENOMEM;
  411. qstrsetstr(&data->host_uri, uri, len);
  412. }
  413. return __chroot_open(uri, len, hdl->flags, 0, hdl, data);
  414. }
  415. static inline bool check_version (struct shim_handle * hdl)
  416. {
  417. return atomic_read(&FILE_HANDLE_DATA(hdl)->version)
  418. == hdl->info.file.version;
  419. }
  420. static int chroot_hstat (struct shim_handle * hdl, struct stat * stat)
  421. {
  422. int ret;
  423. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  424. return ret;
  425. if (!check_version(hdl) || !hdl->dentry) {
  426. struct shim_file_handle * file = &hdl->info.file;
  427. struct shim_dentry * dent = hdl->dentry;
  428. struct mount_data * mdata = dent ? DENTRY_MOUNT_DATA(dent) : NULL;
  429. if (dent)
  430. chroot_update_ino(dent);
  431. if (stat) {
  432. memset(stat, 0, sizeof(struct stat));
  433. stat->st_dev = mdata ? (dev_t) mdata->ino_base : 0;
  434. stat->st_ino = dent ? (ino_t) dent->ino : 0;
  435. stat->st_size = file->size;
  436. stat->st_mode |= (file->buf_type == FILEBUF_MAP) ? S_IFREG : S_IFCHR;
  437. }
  438. return 0;
  439. }
  440. return query_dentry(hdl->dentry, hdl->pal_handle, NULL, stat);
  441. }
  442. static int chroot_flush (struct shim_handle * hdl)
  443. {
  444. struct shim_file_handle * file = &hdl->info.file;
  445. if (file->buf_type == FILEBUF_MAP) {
  446. lock(hdl->lock);
  447. void * mapbuf = file->mapbuf;
  448. int mapsize = file->mapsize;
  449. file->mapoffset = 0;
  450. file->mapbuf = NULL;
  451. unlock(hdl->lock);
  452. if (mapbuf) {
  453. DkStreamUnmap(mapbuf, mapsize);
  454. int flags = VMA_INTERNAL;
  455. bkeep_munmap(mapbuf, mapsize, &flags);
  456. }
  457. }
  458. return 0;
  459. }
  460. static inline int __map_buffer (struct shim_handle * hdl, int size)
  461. {
  462. struct shim_file_handle * file = &hdl->info.file;
  463. if (file->mapbuf) {
  464. if (file->marker >= file->mapoffset &&
  465. file->marker + size <= file->mapoffset + file->mapsize)
  466. return 0;
  467. DkStreamUnmap(file->mapbuf, file->mapsize);
  468. int flags = VMA_INTERNAL;
  469. bkeep_munmap(file->mapbuf, file->mapsize, &flags);
  470. file->mapbuf = NULL;
  471. file->mapoffset = 0;
  472. }
  473. /* second, reallocate the buffer */
  474. int bufsize = file->mapsize ? : FILE_BUFMAP_SIZE;
  475. int prot = PROT_READ;
  476. unsigned long mapoff = file->marker & ~(bufsize - 1);
  477. unsigned long maplen = bufsize;
  478. if (hdl->acc_mode & MAY_WRITE)
  479. prot |= PROT_WRITE;
  480. while (mapoff + maplen < file->marker + size)
  481. maplen *= 2;
  482. void * mapbuf =
  483. (void *) DkStreamMap(hdl->pal_handle, NULL, prot, mapoff, maplen);
  484. if (!mapbuf)
  485. return -PAL_ERRNO;
  486. bkeep_mmap(mapbuf, maplen, prot, MAP_FILE|MAP_SHARED|VMA_INTERNAL,
  487. hdl, mapoff, NULL);
  488. file->mapbuf = mapbuf;
  489. file->mapoffset = mapoff;
  490. file->mapsize = maplen;
  491. return 0;
  492. }
  493. static int map_read (struct shim_handle * hdl, void * buf, size_t count)
  494. {
  495. struct shim_file_handle * file = &hdl->info.file;
  496. int ret = 0;
  497. lock(hdl->lock);
  498. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  499. unsigned int size = atomic_read(&data->size);
  500. if (check_version(hdl) &&
  501. file->size < size)
  502. file->size = size;
  503. int marker = file->marker;
  504. if (marker >= file->size) {
  505. count = 0;
  506. goto out;
  507. }
  508. if ((ret = __map_buffer(hdl, count)) < 0) {
  509. unlock(hdl->lock);
  510. return ret;
  511. }
  512. if (marker + count > file->size)
  513. count = file->size - marker;
  514. if (count) {
  515. memcpy(buf, file->mapbuf + (marker - file->mapoffset), count);
  516. file->marker = marker + count;
  517. }
  518. out:
  519. unlock(hdl->lock);
  520. return count;
  521. }
  522. static int map_write (struct shim_handle * hdl, const void * buf,
  523. size_t count)
  524. {
  525. struct shim_file_handle * file = &hdl->info.file;
  526. int ret = 0;
  527. lock(hdl->lock);
  528. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  529. int marker = file->marker;
  530. if (file->marker + count > file->size) {
  531. file->size = file->marker + count;
  532. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, buf, NULL);
  533. if (!ret) {
  534. ret = -PAL_ERRNO;
  535. goto out;
  536. }
  537. if (ret < count)
  538. file->size -= count - ret;
  539. if (check_version(hdl)) {
  540. int size;
  541. do {
  542. if ((size = atomic_read(&data->size)) >= file->size) {
  543. file->size = size;
  544. break;
  545. }
  546. } while (atomic_cmpxchg(&data->size, size, file->size) != size);
  547. }
  548. file->marker = marker + ret;
  549. goto out;
  550. }
  551. if ((ret = __map_buffer(hdl, count)) < 0)
  552. goto out;
  553. if (count) {
  554. memcpy(file->mapbuf + (marker - file->mapoffset), buf, count);
  555. file->marker = marker + count;
  556. }
  557. ret = count;
  558. out:
  559. unlock(hdl->lock);
  560. return ret;
  561. }
  562. static int chroot_read (struct shim_handle * hdl, void * buf,
  563. size_t count)
  564. {
  565. int ret = 0;
  566. if (count == 0)
  567. goto out;
  568. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  569. goto out;
  570. }
  571. struct shim_file_handle * file = &hdl->info.file;
  572. if (file->buf_type == FILEBUF_MAP) {
  573. ret = map_read(hdl, buf, count);
  574. if (ret != -EACCES)
  575. goto out;
  576. lock(hdl->lock);
  577. file->buf_type = FILEBUF_NONE;
  578. } else {
  579. lock(hdl->lock);
  580. }
  581. ret = DkStreamRead(hdl->pal_handle, file->marker, count, buf, NULL, 0) ? :
  582. (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM ? 0 : -PAL_ERRNO);
  583. if (ret > 0)
  584. file->marker += ret;
  585. unlock(hdl->lock);
  586. out:
  587. return ret;
  588. }
  589. static int chroot_write (struct shim_handle * hdl, const void * buf,
  590. size_t count)
  591. {
  592. int ret;
  593. if (count == 0)
  594. return 0;
  595. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0) {
  596. goto out;
  597. }
  598. struct shim_file_handle * file = &hdl->info.file;
  599. if (hdl->info.file.buf_type == FILEBUF_MAP) {
  600. ret = map_write(hdl, buf, count);
  601. if (ret != -EACCES)
  602. goto out;
  603. lock(hdl->lock);
  604. file->buf_type = FILEBUF_NONE;
  605. } else {
  606. lock(hdl->lock);
  607. }
  608. ret = DkStreamWrite(hdl->pal_handle, file->marker, count, buf, NULL) ? :
  609. -PAL_ERRNO;
  610. if (ret > 0)
  611. file->marker += ret;
  612. unlock(hdl->lock);
  613. out:
  614. return ret;
  615. }
  616. static int chroot_mmap (struct shim_handle * hdl, void ** addr, size_t size,
  617. int prot, int flags, off_t offset)
  618. {
  619. int ret;
  620. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  621. return ret;
  622. int pal_prot = PAL_PROT(prot, flags);
  623. #if MAP_FILE == 0
  624. if (flags & MAP_ANONYMOUS)
  625. #else
  626. if (!(flags & MAP_FILE))
  627. #endif
  628. return -EINVAL;
  629. void * alloc_addr =
  630. (void *) DkStreamMap(hdl->pal_handle, *addr, pal_prot, offset, size);
  631. if (!alloc_addr)
  632. return -PAL_ERRNO;
  633. *addr = alloc_addr;
  634. return 0;
  635. }
  636. static int chroot_seek (struct shim_handle * hdl, off_t offset, int wence)
  637. {
  638. int ret = -EINVAL;
  639. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  640. return ret;
  641. struct shim_file_handle * file = &hdl->info.file;
  642. lock(hdl->lock);
  643. int marker = file->marker;
  644. int size = file->size;
  645. if (check_version(hdl)) {
  646. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  647. if (data->type != FILE_REGULAR) {
  648. ret = -ESPIPE;
  649. goto out;
  650. }
  651. }
  652. switch (wence) {
  653. case SEEK_SET:
  654. if (offset < 0)
  655. goto out;
  656. marker = offset;
  657. break;
  658. case SEEK_CUR:
  659. marker += offset;
  660. break;
  661. case SEEK_END:
  662. marker = size + offset;
  663. break;
  664. }
  665. ret = file->marker = marker;
  666. out:
  667. unlock(hdl->lock);
  668. return ret;
  669. }
  670. static int chroot_truncate (struct shim_handle * hdl, int len)
  671. {
  672. int ret = 0;
  673. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  674. return ret;
  675. struct shim_file_handle * file = &hdl->info.file;
  676. lock(hdl->lock);
  677. file->size = len;
  678. if (check_version(hdl)) {
  679. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  680. atomic_set(&data->size, len);
  681. }
  682. if ((ret = DkStreamSetLength(hdl->pal_handle, len)) != len)
  683. goto out;
  684. if (file->marker > len)
  685. file->marker = len;
  686. out:
  687. unlock(hdl->lock);
  688. return ret;
  689. }
  690. static int chroot_dput (struct shim_dentry * dent)
  691. {
  692. struct shim_file_data * data = FILE_DENTRY_DATA(dent);
  693. if (data) {
  694. __destroy_data(data);
  695. dent->data = NULL;
  696. }
  697. return 0;
  698. }
  699. #define DEFAULT_DBUF_SIZE 1024
  700. static int chroot_readdir (struct shim_dentry * dent,
  701. struct shim_dirent ** dirent)
  702. {
  703. int ret;
  704. struct shim_file_data * data;
  705. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  706. return ret;
  707. chroot_update_ino(dent);
  708. assert(!memcmp(qstrgetstr(&data->host_uri), "dir:", 4));
  709. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri),
  710. PAL_ACCESS_RDONLY, 0, 0, 0);
  711. if (!pal_hdl)
  712. return -PAL_ERRNO;
  713. int buf_size = 0, new_size = MAX_PATH;
  714. int bytes;
  715. char * buf = NULL, * new_buf;
  716. int dbufsize = MAX_PATH;
  717. struct shim_dirent * dbuf = malloc(dbufsize);
  718. struct shim_dirent * d = dbuf, ** last = NULL;
  719. retry:
  720. new_buf = __alloca(new_size);
  721. if (buf)
  722. memcpy(new_buf, buf, buf_size);
  723. buf_size = new_size;
  724. buf = new_buf;
  725. while (1) {
  726. bytes = DkStreamRead(pal_hdl, 0, buf_size, buf, NULL, 0);
  727. if (bytes == 0) {
  728. if (PAL_NATIVE_ERRNO == PAL_ERROR_ENDOFSTREAM)
  729. break;
  730. if (PAL_NATIVE_ERRNO == PAL_ERROR_OVERFLOW) {
  731. new_size = buf_size * 2;
  732. goto retry;
  733. }
  734. ret = -PAL_ERRNO;
  735. goto out;
  736. }
  737. char * b = buf, * next_b;
  738. int blen;
  739. while (b < buf + bytes) {
  740. blen = strlen(b);
  741. next_b = b + blen + 1;
  742. bool isdir = false;
  743. if (b[blen - 1] == '/') {
  744. isdir = true;
  745. b[blen - 1] = 0;
  746. blen--;
  747. }
  748. int dsize = sizeof(struct shim_dirent) + blen + 1;
  749. if ((void *) d + dsize > (void *) dbuf + dbufsize) {
  750. int newsize = dbufsize * 2;
  751. while ((void *) d + dsize > (void *) dbuf + newsize)
  752. newsize *= 2;
  753. struct shim_dirent * new_dbuf = malloc(newsize);
  754. memcpy(new_dbuf, dbuf, (void *) d - (void *) dbuf);
  755. struct shim_dirent * d1 = new_dbuf;
  756. struct shim_dirent * d2 = dbuf;
  757. while (d2 != d) {
  758. d1->next = (void *) d1 + ((void *) d2->next - (void *) d2);
  759. d1 = d1->next;
  760. d2 = d2->next;
  761. }
  762. free(dbuf);
  763. dbuf = new_dbuf;
  764. d = d1;
  765. dbufsize = newsize;
  766. }
  767. HASHTYPE hash = rehash_name(dent->ino, b, blen);
  768. d->next = (void *) (d + 1) + blen + 1;
  769. d->ino = hash;
  770. d->type = isdir ? LINUX_DT_DIR : LINUX_DT_REG;
  771. memcpy(d->name, b, blen + 1);
  772. b = next_b;
  773. last = &d->next;
  774. d = d->next;
  775. }
  776. }
  777. if (!last) {
  778. free(dbuf);
  779. goto out;
  780. }
  781. *last = NULL;
  782. *dirent = dbuf;
  783. out:
  784. DkObjectClose(pal_hdl);
  785. return ret;
  786. }
  787. static int chroot_checkout (struct shim_handle * hdl)
  788. {
  789. if (hdl->fs == &chroot_builtin_fs)
  790. hdl->fs = NULL;
  791. if (hdl->type == TYPE_FILE) {
  792. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  793. if (data)
  794. hdl->info.file.data = NULL;
  795. }
  796. hdl->info.file.mapsize = 0;
  797. hdl->info.file.mapoffset = 0;
  798. hdl->info.file.mapbuf = NULL;
  799. hdl->pal_handle = NULL;
  800. return 0;
  801. }
  802. static int chroot_checkpoint (void ** checkpoint, void * mount_data)
  803. {
  804. struct mount_data * mdata = mount_data;
  805. *checkpoint = mount_data;
  806. return mdata->root_uri_len + sizeof(struct mount_data) + 1;
  807. }
  808. static int chroot_migrate (void * checkpoint, void ** mount_data)
  809. {
  810. struct mount_data * mdata = checkpoint;
  811. int alloc_len = mdata->root_uri_len +
  812. sizeof(struct mount_data) + 1;
  813. void * new_data = malloc(alloc_len);
  814. memcpy(new_data, mdata, alloc_len);
  815. *mount_data = new_data;
  816. return 0;
  817. }
  818. static int chroot_unlink (struct shim_dentry * dir, struct shim_dentry * dent)
  819. {
  820. int ret;
  821. struct shim_file_data * data;
  822. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  823. return ret;
  824. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  825. if (!pal_hdl)
  826. return -PAL_ERRNO;
  827. DkStreamDelete(pal_hdl, 0);
  828. DkObjectClose(pal_hdl);
  829. dent->mode = NO_MODE;
  830. data->mode = 0;
  831. atomic_inc(&data->version);
  832. atomic_set(&data->size, 0);
  833. return 0;
  834. }
  835. static int chroot_poll (struct shim_handle * hdl, int poll_type)
  836. {
  837. int ret;
  838. if (NEED_RECREATE(hdl) && (ret = chroot_recreate(hdl)) < 0)
  839. return ret;
  840. struct shim_file_data * data = FILE_HANDLE_DATA(hdl);
  841. size_t size = atomic_read(&data->size);
  842. if (poll_type == FS_POLL_SZ)
  843. return size;
  844. lock(hdl->lock);
  845. struct shim_file_handle * file = &hdl->info.file;
  846. if (check_version(hdl) &&
  847. file->size < size)
  848. file->size = size;
  849. int marker = file->marker;
  850. if (file->buf_type == FILEBUF_MAP) {
  851. ret = poll_type & FS_POLL_WR;
  852. if ((poll_type & FS_POLL_RD) && file->size > marker)
  853. ret |= FS_POLL_RD;
  854. goto out;
  855. }
  856. ret = -EAGAIN;
  857. out:
  858. unlock(hdl->lock);
  859. return ret;
  860. }
  861. static int chroot_rename (struct shim_dentry * old, struct shim_dentry * new)
  862. {
  863. int ret;
  864. struct shim_file_data * old_data;
  865. if ((ret = try_create_data(old, NULL, 0, &old_data)) < 0)
  866. return ret;
  867. struct shim_file_data * new_data;
  868. if ((ret = try_create_data(new, NULL, 0, &new_data)) < 0)
  869. return ret;
  870. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&old_data->host_uri),
  871. 0, 0, 0, 0);
  872. if (!pal_hdl)
  873. return -PAL_ERRNO;
  874. if (!DkStreamChangeName(pal_hdl, qstrgetstr(&new_data->host_uri))) {
  875. DkObjectClose(pal_hdl);
  876. return -PAL_ERRNO;
  877. }
  878. new->mode = new_data->mode = old_data->mode;
  879. old->mode = NO_MODE;
  880. old_data->mode = 0;
  881. DkObjectClose(pal_hdl);
  882. atomic_inc(&old_data->version);
  883. atomic_set(&old_data->size, 0);
  884. atomic_inc(&new_data->version);
  885. return 0;
  886. }
  887. static int chroot_chmod (struct shim_dentry * dent, mode_t mode)
  888. {
  889. int ret;
  890. struct shim_file_data * data;
  891. if ((ret = try_create_data(dent, NULL, 0, &data)) < 0)
  892. return ret;
  893. PAL_HANDLE pal_hdl = DkStreamOpen(qstrgetstr(&data->host_uri), 0, 0, 0, 0);
  894. if (!pal_hdl)
  895. return -PAL_ERRNO;
  896. PAL_STREAM_ATTR attr = { .share_flags = mode };
  897. if (!DkStreamAttributesSetbyHandle(pal_hdl, &attr)) {
  898. DkObjectClose(pal_hdl);
  899. return -PAL_ERRNO;
  900. }
  901. DkObjectClose(pal_hdl);
  902. dent->mode = data->mode = mode;
  903. return 0;
  904. }
  905. struct shim_fs_ops chroot_fs_ops = {
  906. .mount = &chroot_mount,
  907. .unmount = &chroot_unmount,
  908. .flush = &chroot_flush,
  909. .close = &chroot_flush,
  910. .read = &chroot_read,
  911. .write = &chroot_write,
  912. .mmap = &chroot_mmap,
  913. .seek = &chroot_seek,
  914. .hstat = &chroot_hstat,
  915. .truncate = &chroot_truncate,
  916. .checkout = &chroot_checkout,
  917. .checkpoint = &chroot_checkpoint,
  918. .migrate = &chroot_migrate,
  919. .poll = &chroot_poll,
  920. };
  921. struct shim_d_ops chroot_d_ops = {
  922. .open = &chroot_open,
  923. .mode = &chroot_mode,
  924. .lookup = &chroot_lookup,
  925. .creat = &chroot_creat,
  926. .mkdir = &chroot_mkdir,
  927. .stat = &chroot_stat,
  928. .dput = &chroot_dput,
  929. .readdir = &chroot_readdir,
  930. .unlink = &chroot_unlink,
  931. .rename = &chroot_rename,
  932. .chmod = &chroot_chmod,
  933. };
  934. struct mount_data chroot_data = { .root_uri_len = 5,
  935. .root_uri = "file:", };
  936. struct shim_mount chroot_builtin_fs = { .type = "chroot",
  937. .fs_ops = &chroot_fs_ops,
  938. .d_ops = &chroot_d_ops,
  939. .data = &chroot_data, };