shim_fs.c 21 KB

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