shim_open.c 15 KB

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