shim_open.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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_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. /* If we haven't listed the directory, do this first */
  267. if (!(dent->state & DENTRY_LISTED)) {
  268. ret = list_directory_dentry(dent);
  269. if (ret) goto out;
  270. }
  271. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent) + \
  272. sizeof(struct linux_dirent_tail) + (len) + 1)
  273. #define ASSIGN_DIRENT(dent, name, type) \
  274. do { \
  275. int len = strlen(name); \
  276. if (bytes + DIRENT_SIZE(len) > count) \
  277. goto done; \
  278. \
  279. struct linux_dirent_tail * bt \
  280. = (void *) b + sizeof(struct linux_dirent) + len + 1; \
  281. \
  282. b->d_ino = dent->ino; \
  283. b->d_off = ++dirhdl->offset; \
  284. b->d_reclen = DIRENT_SIZE(len); \
  285. \
  286. memcpy(b->d_name, name, len + 1); \
  287. \
  288. bt->pad = 0; \
  289. bt->d_type = type ? : get_dirent_type(dent->mode); \
  290. \
  291. b = (void *) bt + sizeof(struct linux_dirent_tail); \
  292. bytes += DIRENT_SIZE(len); \
  293. } while(0)
  294. if (dirhdl->dot) {
  295. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  296. put_dentry(dirhdl->dot);
  297. dirhdl->dot = NULL;
  298. }
  299. if (dirhdl->dotdot) {
  300. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  301. put_dentry(dirhdl->dotdot);
  302. dirhdl->dotdot = NULL;
  303. }
  304. if (dirhdl->ptr == (void *) -1) {
  305. ret = list_directory_handle(dent, hdl);
  306. if (ret) goto out;
  307. }
  308. while (dirhdl->ptr && *dirhdl->ptr) {
  309. dent = *dirhdl->ptr;
  310. /* DEP 3/3/17: We need to filter negative dentries */
  311. if (!(dent->state & DENTRY_NEGATIVE))
  312. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  313. put_dentry(dent);
  314. *(dirhdl->ptr++) = NULL;
  315. }
  316. #undef DIRENT_SIZE
  317. #undef ASSIGN_DIRENT
  318. done:
  319. ret = bytes;
  320. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  321. * hold anything */
  322. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  323. (dirhdl->ptr && *dirhdl->ptr)))
  324. ret = -EINVAL;
  325. unlock(hdl->lock);
  326. out:
  327. put_handle(hdl);
  328. return ret;
  329. }
  330. size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count)
  331. {
  332. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  333. if (!hdl)
  334. return -EBADF;
  335. int ret = -EACCES;
  336. if (hdl->type != TYPE_DIR) {
  337. ret = -ENOTDIR;
  338. goto out;
  339. }
  340. /* DEP 3/3/17: Properly handle an unlinked directory */
  341. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  342. ret = -ENOENT;
  343. goto out;
  344. }
  345. lock(hdl->lock);
  346. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  347. struct shim_dentry * dent = hdl->dentry;
  348. struct linux_dirent64 * b = buf;
  349. int bytes = 0;
  350. /* If we haven't listed the directory, do this first */
  351. if (!(dent->state & DENTRY_LISTED)) {
  352. ret = list_directory_dentry(dent);
  353. if (ret) goto out;
  354. }
  355. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent64) + (len) + 1)
  356. #define ASSIGN_DIRENT(dent, name, type) \
  357. do { \
  358. int len = strlen(name); \
  359. if (bytes + DIRENT_SIZE(len) > count) \
  360. goto done; \
  361. \
  362. b->d_ino = dent->ino; \
  363. b->d_off = ++dirhdl->offset; \
  364. b->d_reclen = DIRENT_SIZE(len); \
  365. b->d_type = type ? : get_dirent_type(dent->mode); \
  366. \
  367. memcpy(b->d_name, name, len + 1); \
  368. \
  369. b = (void *) b + DIRENT_SIZE(len); \
  370. bytes += DIRENT_SIZE(len); \
  371. } while(0)
  372. if (dirhdl->dot) {
  373. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  374. put_dentry(dirhdl->dot);
  375. dirhdl->dot = NULL;
  376. }
  377. if (dirhdl->dotdot) {
  378. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  379. put_dentry(dirhdl->dotdot);
  380. dirhdl->dotdot = NULL;
  381. }
  382. if (dirhdl->ptr == (void *) -1) {
  383. ret = list_directory_handle(dent, hdl);
  384. if (ret) goto out;
  385. }
  386. while (dirhdl->ptr && *dirhdl->ptr) {
  387. dent = *dirhdl->ptr;
  388. /* DEP 3/3/17: We need to filter negative dentries */
  389. if (!(dent->state & DENTRY_NEGATIVE))
  390. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  391. put_dentry(dent);
  392. *(dirhdl->ptr++) = NULL;
  393. }
  394. #undef DIRENT_SIZE
  395. #undef ASSIGN_DIRENT
  396. done:
  397. ret = bytes;
  398. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  399. * hold anything */
  400. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  401. (dirhdl->ptr && *dirhdl->ptr)))
  402. ret = -EINVAL;
  403. unlock(hdl->lock);
  404. out:
  405. put_handle(hdl);
  406. return ret;
  407. }
  408. int shim_do_fsync (int fd)
  409. {
  410. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  411. if (!hdl)
  412. return -EBADF;
  413. int ret = -EACCES;
  414. struct shim_mount * fs = hdl->fs;
  415. if (!fs || !fs->fs_ops)
  416. goto out;
  417. if (hdl->type == TYPE_DIR)
  418. goto out;
  419. if (!fs->fs_ops->flush) {
  420. ret = -EROFS;
  421. goto out;
  422. }
  423. ret = fs->fs_ops->flush(hdl);
  424. out:
  425. put_handle(hdl);
  426. return ret;
  427. }
  428. // DEP 10/20/16: Assuming fsync >> fdatasync for now
  429. // and no app depends on only syncing data for correctness.
  430. int shim_do_fdatasync (int fd)
  431. {
  432. return shim_do_fsync(fd);
  433. }
  434. int shim_do_truncate (const char * path, loff_t length)
  435. {
  436. struct shim_dentry * dent = NULL;
  437. int ret = 0;
  438. if ((ret = path_lookupat(NULL, path, 0, &dent, NULL)) < 0)
  439. return ret;
  440. struct shim_mount * fs = dent->fs;
  441. if (!fs || !fs->d_ops || !fs->d_ops->open) {
  442. ret = -EBADF;
  443. goto out;
  444. }
  445. if (!fs->fs_ops->truncate) {
  446. ret = -EROFS;
  447. goto out;
  448. }
  449. struct shim_handle * hdl = get_new_handle();
  450. if (!hdl) {
  451. ret = -ENOMEM;
  452. goto out;
  453. }
  454. hdl->fs = fs;
  455. if ((ret = fs->d_ops->open(hdl, dent, O_WRONLY)) < 0)
  456. goto out_handle;
  457. ret = fs->fs_ops->truncate(hdl, length);
  458. flush_handle(hdl);
  459. out_handle:
  460. put_handle(hdl);
  461. out:
  462. return ret;
  463. }
  464. int shim_do_ftruncate (int fd, loff_t length)
  465. {
  466. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  467. if (!hdl)
  468. return -EBADF;
  469. struct shim_mount * fs = hdl->fs;
  470. int ret = -EINVAL;
  471. if (!fs || !fs->fs_ops)
  472. goto out;
  473. if (hdl->type == TYPE_DIR ||
  474. !fs->fs_ops->truncate)
  475. goto out;
  476. ret = fs->fs_ops->truncate(hdl, length);
  477. out:
  478. put_handle(hdl);
  479. return ret;
  480. }