shim_open.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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_open.c
  17. *
  18. * Implementation of system call "read", "write", "open", "creat", "openat",
  19. * "close", "lseek", "pread64", "pwrite64", "getdents", "getdents64",
  20. * "fsync", "truncate" and "ftruncate".
  21. */
  22. #include <shim_internal.h>
  23. #include <shim_utils.h>
  24. #include <shim_table.h>
  25. #include <shim_thread.h>
  26. #include <shim_handle.h>
  27. #include <shim_fs.h>
  28. #include <shim_profile.h>
  29. #include <pal.h>
  30. #include <pal_error.h>
  31. #include <errno.h>
  32. #include <dirent.h>
  33. #include <linux/stat.h>
  34. #include <linux/fcntl.h>
  35. int do_handle_read (struct shim_handle * hdl, void * buf, int count)
  36. {
  37. if (!(hdl->acc_mode & MAY_READ))
  38. return -EACCES;
  39. struct shim_mount * fs = hdl->fs;
  40. assert (fs && fs->fs_ops);
  41. if (!fs->fs_ops->read)
  42. return -EBADF;
  43. if (hdl->type == TYPE_DIR)
  44. return -EISDIR;
  45. return fs->fs_ops->read(hdl, buf, count);
  46. }
  47. size_t shim_do_read (int fd, void * buf, size_t count)
  48. {
  49. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  50. if (!hdl)
  51. return -EBADF;
  52. int ret = do_handle_read(hdl, buf, count);
  53. put_handle(hdl);
  54. return ret;
  55. }
  56. int do_handle_write (struct shim_handle * hdl, const void * buf, int count)
  57. {
  58. if (!(hdl->acc_mode & MAY_WRITE))
  59. return -EACCES;
  60. struct shim_mount * fs = hdl->fs;
  61. assert (fs && fs->fs_ops);
  62. if (!fs->fs_ops->write)
  63. return -EBADF;
  64. if (hdl->type == TYPE_DIR)
  65. return -EISDIR;
  66. return fs->fs_ops->write(hdl, buf, count);
  67. }
  68. size_t shim_do_write (int fd, const void * buf, size_t count)
  69. {
  70. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  71. if (!hdl)
  72. return -EBADF;
  73. int ret = do_handle_write(hdl, buf, count);
  74. put_handle(hdl);
  75. return ret;
  76. }
  77. int shim_do_open (const char * file, int flags, mode_t mode)
  78. {
  79. if (!file || !(*file))
  80. return -EINVAL;
  81. struct shim_handle * hdl = get_new_handle();
  82. if (!hdl)
  83. return -ENOMEM;
  84. int ret = 0;
  85. ret = open_namei(hdl, NULL, file, flags, mode, NULL);
  86. if (ret < 0)
  87. goto out;
  88. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  89. out:
  90. put_handle(hdl);
  91. return ret;
  92. }
  93. int shim_do_creat (const char * path, mode_t mode)
  94. {
  95. return shim_do_open(path, O_CREAT|O_TRUNC|O_WRONLY, mode);
  96. }
  97. int shim_do_openat (int dfd, const char * filename, int flags, int mode)
  98. {
  99. if (!filename)
  100. return -EINVAL;
  101. if (*filename == '/')
  102. return shim_do_open(filename, flags, mode);
  103. struct shim_dentry * dir = NULL;
  104. int ret = 0;
  105. if ((ret = path_startat(dfd, &dir)) < 0)
  106. return ret;
  107. struct shim_handle * hdl = get_new_handle();
  108. if (!hdl) {
  109. ret = -ENOMEM;
  110. goto out;
  111. }
  112. ret = open_namei(hdl, dir, filename, flags, mode, NULL);
  113. if (ret < 0)
  114. goto out_hdl;
  115. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  116. out_hdl:
  117. put_handle(hdl);
  118. out:
  119. put_dentry(dir);
  120. return ret;
  121. }
  122. int shim_do_close (int fd)
  123. {
  124. struct shim_handle * handle = detach_fd_handle(fd, NULL, NULL);
  125. if (!handle)
  126. return -EBADF;
  127. close_handle(handle);
  128. return 0;
  129. }
  130. /* lseek is simply doing arithmetic on the offset, no PAL call here */
  131. off_t shim_do_lseek (int fd, off_t offset, int origin)
  132. {
  133. if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END)
  134. return -EINVAL;
  135. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  136. if (!hdl)
  137. return -EBADF;
  138. int ret = 0;
  139. struct shim_mount * fs = hdl->fs;
  140. assert(fs && fs->fs_ops);
  141. if (!fs->fs_ops->seek) {
  142. ret = -ESPIPE;
  143. goto out;
  144. }
  145. if (hdl->type == TYPE_DIR) {
  146. /* TODO: handle lseek'ing of directories */
  147. ret = -ENOSYS;
  148. goto out;
  149. }
  150. ret = fs->fs_ops->seek(hdl, offset, origin);
  151. out:
  152. put_handle(hdl);
  153. return ret;
  154. }
  155. size_t shim_do_pread64 (int fd, char * buf, size_t count, loff_t pos)
  156. {
  157. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  158. if (!hdl)
  159. return -EBADF;
  160. struct shim_mount * fs = hdl->fs;
  161. size_t ret = -EACCES;
  162. if (!fs || !fs->fs_ops)
  163. goto out;
  164. if (!fs->fs_ops->seek) {
  165. ret = -ESPIPE;
  166. goto out;
  167. }
  168. if (!fs->fs_ops->read)
  169. goto out;
  170. if (hdl->type == TYPE_DIR)
  171. goto out;
  172. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  173. if (offset < 0) {
  174. ret = offset;
  175. goto out;
  176. }
  177. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  178. if (ret < 0)
  179. goto out;
  180. int bytes = fs->fs_ops->read(hdl, buf, count);
  181. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  182. if (ret < 0)
  183. goto out;
  184. ret = bytes;
  185. out:
  186. put_handle(hdl);
  187. return ret;
  188. }
  189. size_t shim_do_pwrite64 (int fd, char * buf, size_t count, loff_t pos)
  190. {
  191. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  192. if (!hdl)
  193. return -EBADF;
  194. struct shim_mount * fs = hdl->fs;
  195. size_t ret = -EACCES;
  196. if (!fs || !fs->fs_ops)
  197. goto out;
  198. if (!fs->fs_ops->seek) {
  199. ret = -ESPIPE;
  200. goto out;
  201. }
  202. if (!fs->fs_ops->write)
  203. goto out;
  204. if (hdl->type == TYPE_DIR)
  205. goto out;
  206. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  207. if (offset < 0) {
  208. ret = offset;
  209. goto out;
  210. }
  211. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  212. if (ret < 0)
  213. goto out;
  214. int bytes = fs->fs_ops->write(hdl, buf, count);
  215. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  216. if (ret < 0)
  217. goto out;
  218. ret = bytes;
  219. out:
  220. put_handle(hdl);
  221. return ret;
  222. }
  223. static inline int get_dirent_type (mode_t type)
  224. {
  225. switch (type) {
  226. case S_IFLNK:
  227. return LINUX_DT_LNK;
  228. case S_IFREG:
  229. return LINUX_DT_REG;
  230. case S_IFDIR:
  231. return LINUX_DT_DIR;
  232. case S_IFCHR:
  233. return LINUX_DT_CHR;
  234. case S_IFBLK:
  235. return LINUX_DT_BLK;
  236. case S_IFIFO:
  237. return LINUX_DT_FIFO;
  238. case S_IFSOCK:
  239. return LINUX_DT_SOCK;
  240. default:
  241. return LINUX_DT_UNKNOWN;
  242. }
  243. }
  244. size_t shim_do_getdents (int fd, struct linux_dirent * buf, size_t count)
  245. {
  246. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  247. if (!hdl)
  248. return -EBADF;
  249. int ret = -EACCES;
  250. if (hdl->type != TYPE_DIR) {
  251. ret = -ENOTDIR;
  252. goto out;
  253. }
  254. /* DEP 3/3/17: Properly handle an unlinked directory */
  255. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  256. ret = -ENOENT;
  257. goto out;
  258. }
  259. /* we are grabbing the lock because the handle content is actually
  260. updated */
  261. lock(hdl->lock);
  262. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  263. struct shim_dentry * dent = hdl->dentry;
  264. struct linux_dirent * b = buf;
  265. int bytes = 0;
  266. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent) + \
  267. sizeof(struct linux_dirent_tail) + (len) + 1)
  268. #define ASSIGN_DIRENT(dent, name, type) \
  269. do { \
  270. int len = strlen(name); \
  271. if (bytes + DIRENT_SIZE(len) > count) \
  272. goto done; \
  273. \
  274. struct linux_dirent_tail * bt \
  275. = (void *) b + sizeof(struct linux_dirent) + len + 1; \
  276. \
  277. b->d_ino = dent->ino; \
  278. b->d_off = ++dirhdl->offset; \
  279. b->d_reclen = DIRENT_SIZE(len); \
  280. \
  281. memcpy(b->d_name, name, len + 1); \
  282. \
  283. bt->pad = 0; \
  284. bt->d_type = type ? : get_dirent_type(dent->mode); \
  285. \
  286. b = (void *) bt + sizeof(struct linux_dirent_tail); \
  287. bytes += DIRENT_SIZE(len); \
  288. } while(0)
  289. if (dirhdl->dot) {
  290. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  291. put_dentry(dirhdl->dot);
  292. dirhdl->dot = NULL;
  293. }
  294. if (dirhdl->dotdot) {
  295. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  296. put_dentry(dirhdl->dotdot);
  297. dirhdl->dotdot = NULL;
  298. }
  299. while (dirhdl->ptr && *dirhdl->ptr) {
  300. dent = *dirhdl->ptr;
  301. /* DEP 3/3/17: We need to filter negative dentries */
  302. if (!(dent->state & DENTRY_NEGATIVE))
  303. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  304. put_dentry(dent);
  305. *(dirhdl->ptr++) = NULL;
  306. }
  307. #undef DIRENT_SIZE
  308. #undef ASSIGN_DIRENT
  309. done:
  310. ret = bytes;
  311. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  312. * hold anything */
  313. if (bytes == 0 && ((dirhdl->ptr && *dirhdl->ptr)
  314. || dirhdl->dotdot || dirhdl->dot))
  315. ret = -EINVAL;
  316. unlock(hdl->lock);
  317. out:
  318. put_handle(hdl);
  319. return ret;
  320. }
  321. size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count)
  322. {
  323. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  324. if (!hdl)
  325. return -EBADF;
  326. int ret = -EACCES;
  327. if (hdl->type != TYPE_DIR) {
  328. ret = -ENOTDIR;
  329. goto out;
  330. }
  331. /* DEP 3/3/17: Properly handle an unlinked directory */
  332. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  333. ret = -ENOENT;
  334. goto out;
  335. }
  336. lock(hdl->lock);
  337. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  338. struct shim_dentry * dent = hdl->dentry;
  339. struct linux_dirent64 * b = buf;
  340. int bytes = 0;
  341. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent64) + (len) + 1)
  342. #define ASSIGN_DIRENT(dent, name, type) \
  343. do { \
  344. int len = strlen(name); \
  345. if (bytes + DIRENT_SIZE(len) > count) \
  346. goto done; \
  347. \
  348. b->d_ino = dent->ino; \
  349. b->d_off = ++dirhdl->offset; \
  350. b->d_reclen = DIRENT_SIZE(len); \
  351. b->d_type = type ? : get_dirent_type(dent->mode); \
  352. \
  353. memcpy(b->d_name, name, len + 1); \
  354. \
  355. b = (void *) b + DIRENT_SIZE(len); \
  356. bytes += DIRENT_SIZE(len); \
  357. } while(0)
  358. if (dirhdl->dot) {
  359. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  360. put_dentry(dirhdl->dot);
  361. dirhdl->dot = NULL;
  362. }
  363. if (dirhdl->dotdot) {
  364. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  365. put_dentry(dirhdl->dotdot);
  366. dirhdl->dotdot = NULL;
  367. }
  368. while (dirhdl->ptr && *dirhdl->ptr) {
  369. dent = *dirhdl->ptr;
  370. /* DEP 3/3/17: We need to filter negative dentries */
  371. if (!(dent->state & DENTRY_NEGATIVE))
  372. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  373. put_dentry(dent);
  374. *(dirhdl->ptr++) = NULL;
  375. }
  376. #undef DIRENT_SIZE
  377. #undef ASSIGN_DIRENT
  378. done:
  379. ret = bytes;
  380. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  381. * hold anything */
  382. if (bytes == 0 && ((dirhdl->ptr && *dirhdl->ptr)
  383. || dirhdl->dotdot || dirhdl->dot))
  384. ret = -EINVAL;
  385. unlock(hdl->lock);
  386. out:
  387. put_handle(hdl);
  388. return ret;
  389. }
  390. int shim_do_fsync (int fd)
  391. {
  392. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  393. if (!hdl)
  394. return -EBADF;
  395. int ret = -EACCES;
  396. struct shim_mount * fs = hdl->fs;
  397. if (!fs || !fs->fs_ops)
  398. goto out;
  399. if (hdl->type == TYPE_DIR)
  400. goto out;
  401. if (!fs->fs_ops->flush) {
  402. ret = -EROFS;
  403. goto out;
  404. }
  405. ret = fs->fs_ops->flush(hdl);
  406. out:
  407. put_handle(hdl);
  408. return ret;
  409. }
  410. // DEP 10/20/16: Assuming fsync >> fdatasync for now
  411. // and no app depends on only syncing data for correctness.
  412. int shim_do_fdatasync (int fd)
  413. {
  414. return shim_do_fsync(fd);
  415. }
  416. int shim_do_truncate (const char * path, loff_t length)
  417. {
  418. struct shim_dentry * dent = NULL;
  419. int ret = 0;
  420. if ((ret = path_lookupat(NULL, path, 0, &dent)) < 0)
  421. return ret;
  422. struct shim_mount * fs = dent->fs;
  423. if (!fs || !fs->d_ops || !fs->d_ops->open) {
  424. ret = -EBADF;
  425. goto out;
  426. }
  427. if (!fs->fs_ops->truncate) {
  428. ret = -EROFS;
  429. goto out;
  430. }
  431. struct shim_handle * hdl = get_new_handle();
  432. if (!hdl) {
  433. ret = -ENOMEM;
  434. goto out;
  435. }
  436. hdl->fs = fs;
  437. if ((ret = fs->d_ops->open(hdl, dent, O_WRONLY)) < 0)
  438. goto out_handle;
  439. ret = fs->fs_ops->truncate(hdl, length);
  440. flush_handle(hdl);
  441. out_handle:
  442. put_handle(hdl);
  443. out:
  444. return ret;
  445. }
  446. int shim_do_ftruncate (int fd, loff_t length)
  447. {
  448. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  449. if (!hdl)
  450. return -EBADF;
  451. struct shim_mount * fs = hdl->fs;
  452. int ret = -EACCES;
  453. if (!fs || !fs->fs_ops)
  454. goto out;
  455. if (hdl->type == TYPE_DIR)
  456. goto out;
  457. if (!fs->fs_ops->truncate) {
  458. ret = -EROFS;
  459. goto out;
  460. }
  461. ret = fs->fs_ops->truncate(hdl, length);
  462. out:
  463. put_handle(hdl);
  464. return ret;
  465. }