shim_fs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. int shim_do_chown (const char * path, uid_t uid, gid_t gid)
  214. {
  215. struct shim_dentry * dent = NULL;
  216. int ret = 0;
  217. if ((ret = path_lookupat(NULL, path, LOOKUP_OPEN, &dent)) < 0)
  218. return ret;
  219. /* do nothing*/
  220. out:
  221. put_dentry(dent);
  222. return ret;
  223. }
  224. int shim_do_fchownat (int dfd, const char * filename, uid_t uid, gid_t gid,
  225. int flags)
  226. {
  227. if (!filename)
  228. return -EINVAL;
  229. if (*filename == '/')
  230. return shim_do_chown(filename, uid, gid);
  231. struct shim_dentry * dir = NULL, * dent = NULL;
  232. int ret = 0;
  233. if ((ret = path_startat(dfd, &dir)) < 0)
  234. return ret;
  235. if ((ret = path_lookupat(dir, filename, LOOKUP_OPEN, &dent)) < 0)
  236. goto out;
  237. /* do nothing */
  238. out_dent:
  239. put_dentry(dent);
  240. out:
  241. put_dentry(dir);
  242. return ret;
  243. }
  244. int shim_do_fchown (int fd, uid_t uid, gid_t gid)
  245. {
  246. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  247. if (!hdl)
  248. return -EBADF;
  249. struct shim_dentry * dent = hdl->dentry;
  250. int ret = 0;
  251. /* do nothing */
  252. out:
  253. put_handle(hdl);
  254. return ret;
  255. }
  256. #define MAP_SIZE (allocsize * 4)
  257. #define BUF_SIZE (2048)
  258. static ssize_t handle_copy (struct shim_handle * hdli, off_t * offseti,
  259. struct shim_handle * hdlo, off_t * offseto,
  260. ssize_t count)
  261. {
  262. struct shim_mount * fsi = hdli->fs;
  263. struct shim_mount * fso = hdlo->fs;
  264. if (!count)
  265. return 0;
  266. if (!fsi || !fsi->fs_ops || !fso || !fso->fs_ops)
  267. return -EACCES;
  268. bool do_mapi = (fsi->fs_ops->mmap != NULL);
  269. bool do_mapo = (fso->fs_ops->mmap != NULL);
  270. bool do_marki = false, do_marko = false;
  271. int offi = 0, offo = 0;
  272. if (offseti) {
  273. if (!fsi->fs_ops->seek)
  274. return -EACCES;
  275. offi = *offseti;
  276. fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  277. } else {
  278. if (!fsi->fs_ops->seek ||
  279. (offi = fsi->fs_ops->seek(hdli, 0, SEEK_CUR)) < 0)
  280. do_mapi = false;
  281. }
  282. if (offseto) {
  283. if (!fso->fs_ops->seek)
  284. return -EACCES;
  285. offo = *offseto;
  286. fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  287. } else {
  288. if (!fso->fs_ops->seek ||
  289. (offo = fso->fs_ops->seek(hdlo, 0, SEEK_CUR)) < 0)
  290. do_mapo = false;
  291. }
  292. if (do_mapi) {
  293. int size;
  294. if (fsi->fs_ops->poll &&
  295. (size = fsi->fs_ops->poll(hdli, FS_POLL_SZ)) >= 0) {
  296. if (count == -1 ||
  297. count > size - offi)
  298. count = size - offi;
  299. if (!count)
  300. return 0;
  301. } else
  302. do_mapi = false;
  303. }
  304. if (do_mapo && count > 0)
  305. do {
  306. int size;
  307. if (!fso->fs_ops->poll ||
  308. (size = fso->fs_ops->poll(hdlo, FS_POLL_SZ)) < 0) {
  309. do_mapo = false;
  310. break;
  311. }
  312. if (offo + count < size)
  313. break;
  314. if (!fso->fs_ops->truncate ||
  315. fso->fs_ops->truncate(hdlo, offo + count) < 0) {
  316. do_mapo = false;
  317. break;
  318. }
  319. } while(0);
  320. void * bufi = NULL, * bufo = NULL;
  321. int bytes = 0;
  322. int bufsize = MAP_SIZE;
  323. int copysize = 0;
  324. if (!do_mapi && (hdli->flags & O_NONBLOCK) &&
  325. fsi->fs_ops->setflags) {
  326. int ret = fsi->fs_ops->setflags(hdli, 0);
  327. if (!ret) {
  328. debug("mark handle %s as blocking\n", qstrgetstr(&hdli->uri));
  329. do_marki = true;
  330. }
  331. }
  332. if (!do_mapo && (hdlo->flags & O_NONBLOCK) &&
  333. fso->fs_ops->setflags) {
  334. int ret = fso->fs_ops->setflags(hdlo, 0);
  335. if (!ret) {
  336. debug("mark handle %s as blocking\n", qstrgetstr(&hdlo->uri));
  337. do_marko = true;
  338. }
  339. }
  340. assert(count);
  341. do {
  342. int boffi = 0, boffo = 0;
  343. int expectsize = bufsize;
  344. if (count > 0 && bufsize > count - bytes)
  345. expectsize = bufsize = count - bytes;
  346. if (do_mapi && !bufi) {
  347. boffi = offi - ALIGN_DOWN(offi);
  348. if (fsi->fs_ops->mmap(hdli, &bufi, ALIGN_UP(bufsize + boffi),
  349. PROT_READ, MAP_FILE, offi - boffi) < 0) {
  350. do_mapi = false;
  351. boffi = 0;
  352. if ((hdli->flags & O_NONBLOCK) && fsi->fs_ops->setflags) {
  353. int ret = fsi->fs_ops->setflags(hdli, 0);
  354. if (!ret) {
  355. debug("mark handle %s as blocking\n",
  356. qstrgetstr(&hdli->uri));
  357. do_marki = true;
  358. }
  359. }
  360. if (fsi->fs_ops->seek)
  361. offi = fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  362. }
  363. }
  364. if (do_mapo && !bufo) {
  365. boffo = offo - ALIGN_DOWN(offo);
  366. if (fso->fs_ops->mmap(hdlo, &bufo, ALIGN_UP(bufsize + boffo),
  367. PROT_WRITE, MAP_FILE, offo - boffo) < 0) {
  368. do_mapo = false;
  369. boffo = 0;
  370. if ((hdlo->flags & O_NONBLOCK) && fso->fs_ops->setflags) {
  371. int ret = fso->fs_ops->setflags(hdlo, 0);
  372. if (!ret) {
  373. debug("mark handle %s as blocking\n",
  374. qstrgetstr(&hdlo->uri));
  375. do_marko = true;
  376. }
  377. }
  378. if (fso->fs_ops->seek)
  379. offo = fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  380. }
  381. }
  382. if (do_mapi && do_mapo) {
  383. copysize = count - bytes > bufsize ? bufsize :
  384. count - bytes;
  385. memcpy(hdlo + boffo, hdli + boffi, copysize);
  386. DkVirtualMemoryFree(bufi, ALIGN_UP(bufsize + boffi));
  387. bufi = NULL;
  388. DkVirtualMemoryFree(bufo, ALIGN_UP(bufsize + boffo));
  389. bufo = NULL;
  390. goto done_copy;
  391. }
  392. if (do_mapo) {
  393. copysize = fsi->fs_ops->read(hdli, bufo + boffo, bufsize);
  394. DkVirtualMemoryFree(bufo, ALIGN_UP(bufsize + boffo));
  395. bufo = NULL;
  396. if (copysize < 0)
  397. break;
  398. goto done_copy;
  399. }
  400. if (do_mapi) {
  401. copysize = fso->fs_ops->write(hdlo, bufi + boffi, bufsize);
  402. DkVirtualMemoryFree(bufi, ALIGN_UP(bufsize + boffi));
  403. bufi = NULL;
  404. if (copysize < 0)
  405. break;
  406. goto done_copy;
  407. }
  408. if (!bufi)
  409. bufi = __alloca((bufsize = (bufsize > BUF_SIZE) ? BUF_SIZE :
  410. bufsize));
  411. copysize = fsi->fs_ops->read(hdli, bufi, bufsize);
  412. if (copysize <= 0)
  413. break;
  414. expectsize = copysize;
  415. copysize = fso->fs_ops->write(hdlo, bufi, expectsize);
  416. if (copysize < 0)
  417. break;
  418. done_copy:
  419. debug("copy %d bytes\n", copysize);
  420. bytes += copysize;
  421. offi += copysize;
  422. offo += copysize;
  423. if (copysize < expectsize)
  424. break;
  425. } while (bytes < count);
  426. if (copysize < 0 || (count > 0 && bytes < count)) {
  427. int ret = copysize < 0 ? copysize : -EAGAIN;
  428. if (bytes) {
  429. if (fsi->fs_ops->seek)
  430. fsi->fs_ops->seek(hdli, offi - bytes, SEEK_SET);
  431. if (fso->fs_ops->seek)
  432. fso->fs_ops->seek(hdlo, offo - bytes, SEEK_SET);
  433. }
  434. return ret;
  435. }
  436. if (do_marki && (hdli->flags & O_NONBLOCK)) {
  437. debug("mark handle %s as nonblocking\n", qstrgetstr(&hdli->uri));
  438. fsi->fs_ops->setflags(hdli, O_NONBLOCK);
  439. }
  440. if (do_marko && (hdlo->flags & O_NONBLOCK)) {
  441. debug("mark handle %s as nonblocking\n", qstrgetstr(&hdlo->uri));
  442. fso->fs_ops->setflags(hdlo, O_NONBLOCK);
  443. }
  444. if (do_mapi) {
  445. if (fsi->fs_ops->seek)
  446. fsi->fs_ops->seek(hdli, offi, SEEK_SET);
  447. }
  448. if (offseti)
  449. *offseti = offi;
  450. if (do_mapo) {
  451. if (fso->fs_ops->seek)
  452. fso->fs_ops->seek(hdlo, offo, SEEK_SET);
  453. }
  454. if (offseto)
  455. *offseto = offo;
  456. return bytes;
  457. }
  458. static int do_rename (struct shim_dentry * old_dent,
  459. struct shim_dentry * new_dent)
  460. {
  461. int ret = 0;
  462. if (old_dent->fs && old_dent->fs->d_ops &&
  463. old_dent->fs->d_ops->rename &&
  464. old_dent->type == new_dent->type) {
  465. ret = old_dent->fs->d_ops->rename(old_dent, new_dent);
  466. if (!ret) {
  467. old_dent->state |= DENTRY_NEGATIVE;
  468. new_dent->state &= ~DENTRY_NEGATIVE;
  469. goto out;
  470. }
  471. if (ret == -EACCES)
  472. goto out;
  473. }
  474. if (!(new_dent->state & DENTRY_NEGATIVE)) {
  475. if (!new_dent->parent ||
  476. !new_dent->fs || !new_dent->fs->d_ops ||
  477. !new_dent->fs->d_ops->unlink) {
  478. ret = -EACCES;
  479. goto out;
  480. }
  481. if ((ret = new_dent->fs->d_ops->unlink(new_dent->parent,
  482. new_dent)) < 0)
  483. goto out;
  484. new_dent->state |= DENTRY_NEGATIVE;
  485. }
  486. /* TODO: we are not able to handle directory copy yet */
  487. if (old_dent->state & DENTRY_ISDIRECTORY) {
  488. ret = -ENOSYS;
  489. goto out;
  490. }
  491. struct shim_handle * old_hdl = NULL, * new_hdl = NULL;
  492. bool old_opened = false;
  493. bool new_opened = false;
  494. if (!(old_hdl = get_new_handle())) {
  495. ret = -ENOMEM;
  496. goto out_hdl;
  497. }
  498. if ((ret = dentry_open(old_hdl, old_dent, O_RDONLY)) < 0)
  499. goto out_hdl;
  500. old_opened = true;
  501. if (!(new_hdl = get_new_handle())) {
  502. ret = -ENOMEM;
  503. goto out_hdl;
  504. }
  505. if ((ret = dentry_open(new_hdl, new_dent, O_WRONLY|O_CREAT)) < 0)
  506. goto out_hdl;
  507. new_opened = true;
  508. off_t old_offset = 0, new_offset = 0;
  509. if ((ret = handle_copy(old_hdl, &old_offset,
  510. new_hdl, &new_offset, -1)) < 0) {
  511. if (new_dent->fs && new_dent->fs->d_ops &&
  512. new_dent->fs->d_ops->unlink) {
  513. ret = new_dent->fs->d_ops->unlink(new_dent->parent,
  514. new_dent);
  515. }
  516. goto out_hdl;
  517. }
  518. new_dent->state &= ~DENTRY_NEGATIVE;
  519. if (old_dent->fs && old_dent->fs->d_ops &&
  520. old_dent->fs->d_ops->unlink) {
  521. if ((ret = old_dent->fs->d_ops->unlink(old_dent->parent,
  522. old_dent)) < 0)
  523. goto out_hdl;
  524. }
  525. old_dent->state |= DENTRY_NEGATIVE;
  526. out_hdl:
  527. if (old_hdl) {
  528. if (old_opened)
  529. close_handle(old_hdl);
  530. else
  531. put_handle(old_hdl);
  532. }
  533. if (new_hdl) {
  534. if (new_opened)
  535. close_handle(new_hdl);
  536. else
  537. put_handle(new_hdl);
  538. }
  539. out:
  540. return ret;
  541. }
  542. int shim_do_rename (const char * oldname, const char * newname)
  543. {
  544. struct shim_dentry * old_dent = NULL, * new_dent = NULL;
  545. int ret = 0;
  546. if ((ret = path_lookupat(NULL, oldname, LOOKUP_OPEN, &old_dent)) < 0)
  547. goto out;
  548. if (old_dent->state & DENTRY_NEGATIVE) {
  549. ret = -ENOENT;
  550. goto out;
  551. }
  552. if ((ret = path_lookupat(NULL, newname, LOOKUP_OPEN|LOOKUP_CREATE,
  553. &new_dent)) < 0)
  554. goto out;
  555. ret = do_rename(old_dent, new_dent);
  556. out:
  557. if (old_dent)
  558. put_dentry(old_dent);
  559. if (new_dent)
  560. put_dentry(new_dent);
  561. return ret;
  562. }
  563. int shim_do_renameat (int olddfd, const char * pathname, int newdfd,
  564. const char * newname)
  565. {
  566. struct shim_dentry * old_dir = NULL, * old_dent = NULL;
  567. struct shim_dentry * new_dir = NULL, * new_dent = NULL;
  568. int ret = 0;
  569. if ((ret = path_startat(olddfd, &old_dir)) < 0)
  570. goto out;
  571. if ((ret = path_lookupat(old_dir, pathname, LOOKUP_OPEN, &old_dent)) < 0)
  572. goto out;
  573. if (old_dent->state & DENTRY_NEGATIVE) {
  574. ret = -ENOENT;
  575. goto out;
  576. }
  577. if ((ret = path_startat(newdfd, &new_dir)) < 0)
  578. goto out;
  579. if ((ret = path_lookupat(new_dir, newname, LOOKUP_OPEN|LOOKUP_CREATE,
  580. &new_dent)) < 0)
  581. goto out;
  582. ret = do_rename(old_dent, new_dent);
  583. out:
  584. if (old_dir)
  585. put_dentry(old_dir);
  586. if (old_dent)
  587. put_dentry(old_dent);
  588. if (new_dir)
  589. put_dentry(new_dir);
  590. if (new_dent)
  591. put_dentry(new_dent);
  592. return ret;
  593. }
  594. ssize_t shim_do_sendfile (int ofd, int ifd, off_t * offset,
  595. size_t count)
  596. {
  597. struct shim_handle * hdli = get_fd_handle(ifd, NULL, NULL);
  598. struct shim_handle * hdlo = get_fd_handle(ofd, NULL, NULL);
  599. if (!hdli || !hdlo)
  600. return -EBADF;
  601. off_t old_offset = 0;
  602. int ret = -EACCES;
  603. if (offset) {
  604. if (!hdli->fs || !hdli->fs->fs_ops ||
  605. !hdli->fs->fs_ops->seek)
  606. goto out;
  607. old_offset = hdli->fs->fs_ops->seek(hdli, 0, SEEK_CUR);
  608. if (old_offset < 0) {
  609. ret = old_offset;
  610. goto out;
  611. }
  612. }
  613. ret = handle_copy(hdli, offset, hdlo, NULL, count);
  614. if (ret >= 0 && offset)
  615. hdli->fs->fs_ops->seek(hdli, old_offset, SEEK_SET);
  616. out:
  617. put_handle(hdli);
  618. put_handle(hdlo);
  619. return ret;
  620. }
  621. int shim_do_chroot (const char * filename)
  622. {
  623. int ret = 0;
  624. struct shim_dentry * dent = NULL;
  625. if ((ret = path_lookupat(NULL, filename, 0 , &dent)) < 0)
  626. goto out;
  627. if (!dent) {
  628. ret = -ENOENT;
  629. goto out;
  630. }
  631. struct shim_thread * thread = get_cur_thread();
  632. lock(thread->lock);
  633. put_dentry(thread->root);
  634. thread->root = dent;
  635. unlock(thread->lock);
  636. out:
  637. return ret;
  638. }