shim_fs.c 21 KB

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