shim_fs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. * shim_fs.c
  17. *
  18. * Implementation of system call "unlink", "unlinkat", "mkdir", "mkdirat",
  19. * "rmdir", "umask", "chmod", "fchmod", "fchmodat", "rename", "renameat" and
  20. * "sendfile".
  21. */
  22. #include <shim_internal.h>
  23. #include <shim_table.h>
  24. #include <shim_utils.h>
  25. #include <shim_thread.h>
  26. #include <shim_handle.h>
  27. #include <shim_fs.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <errno.h>
  31. #include <linux/fcntl.h>
  32. #include <asm/mman.h>
  33. /* The kernel would look up the parent directory, and remove the child from
  34. * the inode. But we are working with the PAL, so we open the file, truncate
  35. * and close it. */
  36. int shim_do_unlink (const char * file)
  37. {
  38. if (!file)
  39. return -EINVAL;
  40. struct shim_dentry * dent = NULL;
  41. int ret = 0;
  42. if ((ret = path_lookupat(NULL, file, LOOKUP_OPEN, &dent)) < 0)
  43. return ret;
  44. if (!dent->parent)
  45. return -EACCES;
  46. if (dent->state & DENTRY_ISDIRECTORY)
  47. return -EISDIR;
  48. if (dent->fs && dent->fs->d_ops &&
  49. dent->fs->d_ops->unlink) {
  50. if ((ret = dent->fs->d_ops->unlink(dent->parent, dent)) < 0)
  51. return ret;
  52. } else
  53. dent->state |= DENTRY_PERSIST;
  54. dent->state |= DENTRY_NEGATIVE;
  55. put_dentry(dent);
  56. return 0;
  57. }
  58. int shim_do_unlinkat (int dfd, const char * pathname, int flag)
  59. {
  60. if (!pathname)
  61. return -EINVAL;
  62. if (*pathname == '/')
  63. return (flag & AT_REMOVEDIR) ? shim_do_rmdir(pathname) :
  64. shim_do_unlink(pathname);
  65. struct shim_dentry * dir = NULL, * dent = NULL;
  66. int ret = 0;
  67. if ((ret = path_startat(dfd, &dir)) < 0)
  68. return ret;
  69. if ((ret = path_lookupat(dir, pathname, LOOKUP_OPEN, &dent)) < 0)
  70. goto out;
  71. if (!dent->parent) {
  72. ret = -EACCES;
  73. goto out_dent;
  74. }
  75. if (flag & AT_REMOVEDIR) {
  76. if (!(dent->state & DENTRY_ISDIRECTORY))
  77. return -ENOTDIR;
  78. } else {
  79. if (dent->state & DENTRY_ISDIRECTORY)
  80. return -EISDIR;
  81. }
  82. if (dent->fs && dent->fs->d_ops &&
  83. dent->fs->d_ops->unlink) {
  84. if ((ret = dent->fs->d_ops->unlink(dent->parent, dent)) < 0)
  85. return ret;
  86. } else
  87. dent->state |= DENTRY_PERSIST;
  88. if (flag & AT_REMOVEDIR)
  89. dent->state &= ~DENTRY_ISDIRECTORY;
  90. dent->state |= DENTRY_NEGATIVE;
  91. out_dent:
  92. put_dentry(dent);
  93. out:
  94. put_dentry(dir);
  95. return ret;
  96. }
  97. int shim_do_mkdir (const char * pathname, int mode)
  98. {
  99. return open_namei(NULL, NULL, pathname, O_CREAT|O_EXCL|O_DIRECTORY,
  100. mode, NULL);
  101. }
  102. int shim_do_mkdirat (int dfd, const char * pathname, int mode)
  103. {
  104. if (!pathname)
  105. return -EINVAL;
  106. if (*pathname == '/')
  107. return shim_do_mkdir(pathname, mode);
  108. struct shim_dentry * dir = NULL;
  109. int ret = 0;
  110. if ((ret = path_startat(dfd, &dir)) < 0)
  111. return ret;
  112. ret = open_namei(NULL, dir, pathname, O_CREAT|O_EXCL|O_DIRECTORY,
  113. mode, NULL);
  114. put_dentry(dir);
  115. return ret;
  116. }
  117. int shim_do_rmdir (const char * pathname)
  118. {
  119. int ret = 0;
  120. struct shim_dentry * dent = NULL;
  121. if ((ret = path_lookupat(NULL, pathname, LOOKUP_OPEN|LOOKUP_DIRECTORY,
  122. &dent)) < 0)
  123. return ret;
  124. if (!dent->parent) {
  125. ret = -EACCES;
  126. goto out;
  127. }
  128. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  129. ret = -ENOTDIR;
  130. goto out;
  131. }
  132. if (dent->fs && dent->fs->d_ops &&
  133. dent->fs->d_ops->unlink) {
  134. if ((ret = dent->fs->d_ops->unlink(dent->parent, dent)) < 0)
  135. goto out;
  136. } else
  137. dent->state |= DENTRY_PERSIST;
  138. dent->state &= ~DENTRY_ISDIRECTORY;
  139. dent->state |= DENTRY_NEGATIVE;
  140. out:
  141. put_dentry(dent);
  142. return 0;
  143. }
  144. mode_t shim_do_umask (mode_t mask)
  145. {
  146. struct shim_thread * cur = get_cur_thread();
  147. lock(cur->lock);
  148. mode_t old = cur->umask;
  149. cur->umask = mask & 0777;
  150. unlock(cur->lock);
  151. return old;
  152. }
  153. int shim_do_chmod (const char * path, mode_t mode)
  154. {
  155. struct shim_dentry * dent = NULL;
  156. int ret = 0;
  157. if ((ret = path_lookupat(NULL, path, LOOKUP_OPEN, &dent)) < 0)
  158. return ret;
  159. if (dent->fs && dent->fs->d_ops &&
  160. dent->fs->d_ops->chmod) {
  161. if ((ret = dent->fs->d_ops->chmod(dent, mode)) < 0)
  162. goto out;
  163. } else
  164. dent->state |= DENTRY_PERSIST;
  165. dent->mode = mode;
  166. out:
  167. put_dentry(dent);
  168. return ret;
  169. }
  170. int shim_do_fchmodat (int dfd, const char * filename, mode_t mode)
  171. {
  172. if (!filename)
  173. return -EINVAL;
  174. if (*filename == '/')
  175. return shim_do_chmod(filename, mode);
  176. struct shim_dentry * dir = NULL, * dent = NULL;
  177. int ret = 0;
  178. if ((ret = path_startat(dfd, &dir)) < 0)
  179. return ret;
  180. if ((ret = path_lookupat(dir, filename, LOOKUP_OPEN, &dent)) < 0)
  181. goto out;
  182. if (dent->fs && dent->fs->d_ops &&
  183. dent->fs->d_ops->chmod) {
  184. if ((ret = dent->fs->d_ops->chmod(dent, mode)) < 0)
  185. goto out_dent;
  186. } else
  187. dent->state |= DENTRY_PERSIST;
  188. dent->mode = mode;
  189. out_dent:
  190. put_dentry(dent);
  191. out:
  192. put_dentry(dir);
  193. return ret;
  194. }
  195. int shim_do_fchmod (int fd, mode_t mode)
  196. {
  197. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  198. if (!hdl)
  199. return -EBADF;
  200. struct shim_dentry * dent = hdl->dentry;
  201. int ret = 0;
  202. if (dent->fs && dent->fs->d_ops && dent->fs->d_ops->chmod) {
  203. if ((ret = dent->fs->d_ops->chmod(dent, mode)) < 0)
  204. goto out;
  205. } else {
  206. dent->state |= DENTRY_PERSIST;
  207. }
  208. dent->mode = mode;
  209. out:
  210. put_handle(hdl);
  211. return ret;
  212. }
  213. #define MAP_SIZE (allocsize * 4)
  214. #define BUF_SIZE (2048)
  215. static ssize_t handle_copy (struct shim_handle * hdli, off_t * offseti,
  216. struct shim_handle * hdlo, off_t * offseto,
  217. ssize_t count)
  218. {
  219. struct shim_mount * fsi = hdli->fs;
  220. struct shim_mount * fso = hdlo->fs;
  221. if (!count)
  222. return 0;
  223. if (!fsi || !fsi->fs_ops || !fso || !fso->fs_ops)
  224. return -EACCES;
  225. bool do_mapi = (fsi->fs_ops->mmap != NULL);
  226. bool do_mapo = (fso->fs_ops->mmap != NULL);
  227. bool do_marki = false, do_marko = false;
  228. int offi = 0, offo = 0;
  229. if (offseti) {
  230. if (!fsi->fs_ops->seek)
  231. return -EACCES;
  232. offi = *offseti;
  233. fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  234. } else {
  235. if (!fsi->fs_ops->seek ||
  236. (offi = fsi->fs_ops->seek(hdli, 0, SEEK_CUR)) < 0)
  237. do_mapi = false;
  238. }
  239. if (offseto) {
  240. if (!fso->fs_ops->seek)
  241. return -EACCES;
  242. offo = *offseto;
  243. fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  244. } else {
  245. if (!fso->fs_ops->seek ||
  246. (offo = fso->fs_ops->seek(hdlo, 0, SEEK_CUR)) < 0)
  247. do_mapo = false;
  248. }
  249. if (do_mapi) {
  250. int size;
  251. if (fsi->fs_ops->poll &&
  252. (size = fsi->fs_ops->poll(hdli, FS_POLL_SZ)) >= 0) {
  253. if (count == -1 ||
  254. count > size - offi)
  255. count = size - offi;
  256. if (!count)
  257. return 0;
  258. } else
  259. do_mapi = false;
  260. }
  261. if (do_mapo && count > 0)
  262. do {
  263. int size;
  264. if (!fso->fs_ops->poll ||
  265. (size = fso->fs_ops->poll(hdlo, FS_POLL_SZ)) < 0) {
  266. do_mapo = false;
  267. break;
  268. }
  269. if (offo + count < size)
  270. break;
  271. if (!fso->fs_ops->truncate ||
  272. fso->fs_ops->truncate(hdlo, offo + count) < 0) {
  273. do_mapo = false;
  274. break;
  275. }
  276. } while(0);
  277. void * bufi = NULL, * bufo = NULL;
  278. int bytes = 0;
  279. int bufsize = MAP_SIZE;
  280. int copysize = 0;
  281. if (!do_mapi && (hdli->flags & O_NONBLOCK) &&
  282. fsi->fs_ops->setflags) {
  283. int ret = fsi->fs_ops->setflags(hdli, 0);
  284. if (!ret) {
  285. debug("mark handle %s as blocking\n", qstrgetstr(&hdli->uri));
  286. do_marki = true;
  287. }
  288. }
  289. if (!do_mapo && (hdlo->flags & O_NONBLOCK) &&
  290. fso->fs_ops->setflags) {
  291. int ret = fso->fs_ops->setflags(hdlo, 0);
  292. if (!ret) {
  293. debug("mark handle %s as blocking\n", qstrgetstr(&hdlo->uri));
  294. do_marko = true;
  295. }
  296. }
  297. assert(count);
  298. do {
  299. int boffi = 0, boffo = 0;
  300. int expectsize = bufsize;
  301. if (count > 0 && bufsize > count - bytes)
  302. expectsize = bufsize = count - bytes;
  303. if (do_mapi && !bufi) {
  304. boffi = offi - ALIGN_DOWN(offi);
  305. if (fsi->fs_ops->mmap(hdli, &bufi, ALIGN_UP(bufsize + boffi),
  306. PROT_READ, MAP_FILE, offi - boffi) < 0) {
  307. do_mapi = false;
  308. boffi = 0;
  309. if ((hdli->flags & O_NONBLOCK) && fsi->fs_ops->setflags) {
  310. int ret = fsi->fs_ops->setflags(hdli, 0);
  311. if (!ret) {
  312. debug("mark handle %s as blocking\n",
  313. qstrgetstr(&hdli->uri));
  314. do_marki = true;
  315. }
  316. }
  317. if (fsi->fs_ops->seek)
  318. offi = fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  319. }
  320. }
  321. if (do_mapo && !bufo) {
  322. boffo = offo - ALIGN_DOWN(offo);
  323. if (fso->fs_ops->mmap(hdlo, &bufo, ALIGN_UP(bufsize + boffo),
  324. PROT_WRITE, MAP_FILE, offo - boffo) < 0) {
  325. do_mapo = false;
  326. boffo = 0;
  327. if ((hdlo->flags & O_NONBLOCK) && fso->fs_ops->setflags) {
  328. int ret = fso->fs_ops->setflags(hdlo, 0);
  329. if (!ret) {
  330. debug("mark handle %s as blocking\n",
  331. qstrgetstr(&hdlo->uri));
  332. do_marko = true;
  333. }
  334. }
  335. if (fso->fs_ops->seek)
  336. offo = fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  337. }
  338. }
  339. if (do_mapi && do_mapo) {
  340. copysize = count - bytes > bufsize ? bufsize :
  341. count - bytes;
  342. memcpy(hdlo + boffo, hdli + boffi, copysize);
  343. DkVirtualMemoryFree(bufi, ALIGN_UP(bufsize + boffi));
  344. bufi = NULL;
  345. DkVirtualMemoryFree(bufo, ALIGN_UP(bufsize + boffo));
  346. bufo = NULL;
  347. goto done_copy;
  348. }
  349. if (do_mapo) {
  350. copysize = fsi->fs_ops->read(hdli, bufo + boffo, bufsize);
  351. DkVirtualMemoryFree(bufo, ALIGN_UP(bufsize + boffo));
  352. bufo = NULL;
  353. if (copysize < 0)
  354. break;
  355. goto done_copy;
  356. }
  357. if (do_mapi) {
  358. copysize = fso->fs_ops->write(hdlo, bufi + boffi, bufsize);
  359. DkVirtualMemoryFree(bufi, ALIGN_UP(bufsize + boffi));
  360. bufi = NULL;
  361. if (copysize < 0)
  362. break;
  363. goto done_copy;
  364. }
  365. if (!bufi)
  366. bufi = __alloca((bufsize = (bufsize > BUF_SIZE) ? BUF_SIZE :
  367. bufsize));
  368. copysize = fsi->fs_ops->read(hdli, bufi, bufsize);
  369. if (copysize <= 0)
  370. break;
  371. expectsize = copysize;
  372. copysize = fso->fs_ops->write(hdlo, bufi, expectsize);
  373. if (copysize < 0)
  374. break;
  375. done_copy:
  376. debug("copy %d bytes\n", copysize);
  377. bytes += copysize;
  378. offi += copysize;
  379. offo += copysize;
  380. if (copysize < expectsize)
  381. break;
  382. } while (bytes < count);
  383. if (copysize < 0 || (count > 0 && bytes < count)) {
  384. int ret = copysize < 0 ? copysize : -EAGAIN;
  385. if (bytes) {
  386. if (fsi->fs_ops->seek)
  387. fsi->fs_ops->seek(hdli, offi - bytes, SEEK_SET);
  388. if (fso->fs_ops->seek)
  389. fso->fs_ops->seek(hdlo, offo - bytes, SEEK_SET);
  390. }
  391. return ret;
  392. }
  393. if (do_marki && (hdli->flags & O_NONBLOCK)) {
  394. debug("mark handle %s as nonblocking\n", qstrgetstr(&hdli->uri));
  395. fsi->fs_ops->setflags(hdli, O_NONBLOCK);
  396. }
  397. if (do_marko && (hdlo->flags & O_NONBLOCK)) {
  398. debug("mark handle %s as nonblocking\n", qstrgetstr(&hdlo->uri));
  399. fso->fs_ops->setflags(hdlo, O_NONBLOCK);
  400. }
  401. if (do_mapi) {
  402. if (fsi->fs_ops->seek)
  403. fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  404. }
  405. if (offseti)
  406. *offseti = offi;
  407. if (do_mapo) {
  408. if (fso->fs_ops->seek)
  409. fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  410. }
  411. if (offseto)
  412. *offseto = offo;
  413. return bytes;
  414. }
  415. static int do_rename (struct shim_dentry * old_dent,
  416. struct shim_dentry * new_dent)
  417. {
  418. int ret = 0;
  419. if (old_dent->fs && old_dent->fs->d_ops &&
  420. old_dent->fs->d_ops->rename &&
  421. old_dent->type == new_dent->type) {
  422. ret = old_dent->fs->d_ops->rename(old_dent, new_dent);
  423. if (!ret) {
  424. old_dent->state |= DENTRY_NEGATIVE;
  425. new_dent->state &= ~DENTRY_NEGATIVE;
  426. goto out;
  427. }
  428. if (ret == -EACCES)
  429. goto out;
  430. }
  431. if (!(new_dent->state & DENTRY_NEGATIVE)) {
  432. if (!new_dent->parent ||
  433. !new_dent->fs || !new_dent->fs->d_ops ||
  434. !new_dent->fs->d_ops->unlink) {
  435. ret = -EACCES;
  436. goto out;
  437. }
  438. if ((ret = new_dent->fs->d_ops->unlink(new_dent->parent,
  439. new_dent)) < 0)
  440. goto out;
  441. new_dent->state |= DENTRY_NEGATIVE;
  442. }
  443. /* TODO: we are not able to handle directory copy yet */
  444. if (old_dent->state & DENTRY_ISDIRECTORY) {
  445. ret = -ENOSYS;
  446. goto out;
  447. }
  448. struct shim_handle * old_hdl = NULL, * new_hdl = NULL;
  449. bool old_opened = false;
  450. bool new_opened = false;
  451. if (!(old_hdl = get_new_handle())) {
  452. ret = -ENOMEM;
  453. goto out_hdl;
  454. }
  455. if ((ret = dentry_open(old_hdl, old_dent, O_RDONLY)) < 0)
  456. goto out_hdl;
  457. old_opened = true;
  458. if (!(new_hdl = get_new_handle())) {
  459. ret = -ENOMEM;
  460. goto out_hdl;
  461. }
  462. if ((ret = dentry_open(new_hdl, new_dent, O_WRONLY|O_CREAT)) < 0)
  463. goto out_hdl;
  464. new_opened = true;
  465. off_t old_offset = 0, new_offset = 0;
  466. if ((ret = handle_copy(old_hdl, &old_offset,
  467. new_hdl, &new_offset, -1)) < 0) {
  468. if (new_dent->fs && new_dent->fs->d_ops &&
  469. new_dent->fs->d_ops->unlink) {
  470. ret = new_dent->fs->d_ops->unlink(new_dent->parent,
  471. new_dent);
  472. }
  473. goto out_hdl;
  474. }
  475. new_dent->state &= ~DENTRY_NEGATIVE;
  476. if (old_dent->fs && old_dent->fs->d_ops &&
  477. old_dent->fs->d_ops->unlink) {
  478. if ((ret = old_dent->fs->d_ops->unlink(old_dent->parent,
  479. old_dent)) < 0)
  480. goto out_hdl;
  481. }
  482. old_dent->state |= DENTRY_NEGATIVE;
  483. out_hdl:
  484. if (old_hdl) {
  485. if (old_opened)
  486. close_handle(old_hdl);
  487. else
  488. put_handle(old_hdl);
  489. }
  490. if (new_hdl) {
  491. if (new_opened)
  492. close_handle(new_hdl);
  493. else
  494. put_handle(new_hdl);
  495. }
  496. out:
  497. return ret;
  498. }
  499. int shim_do_rename (const char * oldname, const char * newname)
  500. {
  501. struct shim_dentry * old_dent = NULL, * new_dent = NULL;
  502. int ret = 0;
  503. if ((ret = path_lookupat(NULL, oldname, LOOKUP_OPEN, &old_dent)) < 0)
  504. goto out;
  505. if (old_dent->state & DENTRY_NEGATIVE) {
  506. ret = -ENOENT;
  507. goto out;
  508. }
  509. if ((ret = path_lookupat(NULL, newname, LOOKUP_OPEN|LOOKUP_CREATE,
  510. &new_dent)) < 0)
  511. goto out;
  512. ret = do_rename(old_dent, new_dent);
  513. out:
  514. if (old_dent)
  515. put_dentry(old_dent);
  516. if (new_dent)
  517. put_dentry(new_dent);
  518. return ret;
  519. }
  520. int shim_do_renameat (int olddfd, const char * pathname, int newdfd,
  521. const char * newname)
  522. {
  523. struct shim_dentry * old_dir = NULL, * old_dent = NULL;
  524. struct shim_dentry * new_dir = NULL, * new_dent = NULL;
  525. int ret = 0;
  526. if ((ret = path_startat(olddfd, &old_dir)) < 0)
  527. goto out;
  528. if ((ret = path_lookupat(old_dir, pathname, LOOKUP_OPEN, &old_dent)) < 0)
  529. goto out;
  530. if (old_dent->state & DENTRY_NEGATIVE) {
  531. ret = -ENOENT;
  532. goto out;
  533. }
  534. if ((ret = path_startat(newdfd, &new_dir)) < 0)
  535. goto out;
  536. if ((ret = path_lookupat(new_dir, newname, LOOKUP_OPEN|LOOKUP_CREATE,
  537. &new_dent)) < 0)
  538. goto out;
  539. ret = do_rename(old_dent, new_dent);
  540. out:
  541. if (old_dir)
  542. put_dentry(old_dir);
  543. if (old_dent)
  544. put_dentry(old_dent);
  545. if (new_dir)
  546. put_dentry(new_dir);
  547. if (new_dent)
  548. put_dentry(new_dent);
  549. return ret;
  550. }
  551. ssize_t shim_do_sendfile (int ofd, int ifd, off_t * offset,
  552. size_t count)
  553. {
  554. struct shim_handle * hdli = get_fd_handle(ifd, NULL, NULL);
  555. struct shim_handle * hdlo = get_fd_handle(ofd, NULL, NULL);
  556. if (!hdli || !hdlo)
  557. return -EBADF;
  558. off_t old_offset = 0;
  559. int ret = -EACCES;
  560. if (offset) {
  561. if (!hdli->fs || !hdli->fs->fs_ops ||
  562. !hdli->fs->fs_ops->seek)
  563. goto out;
  564. old_offset = hdli->fs->fs_ops->seek(hdli, 0, SEEK_CUR);
  565. if (old_offset < 0) {
  566. ret = old_offset;
  567. goto out;
  568. }
  569. }
  570. ret = handle_copy(hdli, offset, hdlo, NULL, count);
  571. if (ret >= 0 && offset)
  572. hdli->fs->fs_ops->seek(hdli, old_offset, SEEK_SET);
  573. out:
  574. put_handle(hdli);
  575. put_handle(hdlo);
  576. return ret;
  577. }
  578. int shim_do_chroot (const char * filename)
  579. {
  580. int ret = 0;
  581. struct shim_dentry * dent = NULL;
  582. if ((ret = path_lookupat(NULL, filename, 0 , &dent)) < 0)
  583. goto out;
  584. if (!dent) {
  585. ret = -ENOENT;
  586. goto out;
  587. }
  588. struct shim_thread * thread = get_cur_thread();
  589. lock(thread->lock);
  590. put_dentry(thread->root);
  591. thread->root = dent;
  592. unlock(thread->lock);
  593. out:
  594. return ret;
  595. }