shim_fs.c 20 KB

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