fs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2019 Intel Corporation
  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. * fs.c
  15. *
  16. * This file contains codes for implementation of 'eventfd' filesystem.
  17. */
  18. #include <asm/fcntl.h>
  19. #include <asm/unistd.h>
  20. #include <errno.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <pal.h>
  24. #include <shim_fs.h>
  25. #include <shim_handle.h>
  26. #include <shim_internal.h>
  27. static ssize_t eventfd_read(struct shim_handle* hdl, void* buf, size_t count) {
  28. if (count < sizeof(uint64_t))
  29. return -EINVAL;
  30. PAL_NUM bytes = DkStreamRead(hdl->pal_handle, 0, count, buf, NULL, 0);
  31. if (bytes == PAL_STREAM_ERROR)
  32. return -PAL_ERRNO;
  33. return (ssize_t)bytes;
  34. }
  35. static ssize_t eventfd_write(struct shim_handle* hdl, const void* buf, size_t count) {
  36. if (count < sizeof(uint64_t))
  37. return -EINVAL;
  38. PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void*)buf, NULL);
  39. if (bytes == PAL_STREAM_ERROR)
  40. return -PAL_ERRNO;
  41. return (ssize_t)bytes;
  42. }
  43. static off_t eventfd_poll(struct shim_handle* hdl, int poll_type) {
  44. off_t ret = 0;
  45. lock(&hdl->lock);
  46. if (!hdl->pal_handle) {
  47. ret = -EBADF;
  48. goto out;
  49. }
  50. PAL_STREAM_ATTR attr;
  51. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr)) {
  52. ret = -PAL_ERRNO;
  53. goto out;
  54. }
  55. if (poll_type == FS_POLL_SZ) {
  56. ret = attr.pending_size;
  57. goto out;
  58. }
  59. ret = 0;
  60. if (attr.disconnected)
  61. ret |= FS_POLL_ER;
  62. if ((poll_type & FS_POLL_RD) && attr.readable)
  63. ret |= FS_POLL_RD;
  64. if ((poll_type & FS_POLL_WR) && attr.writable)
  65. ret |= FS_POLL_WR;
  66. out:
  67. unlock(&hdl->lock);
  68. return ret;
  69. }
  70. struct shim_fs_ops eventfd_fs_ops = {
  71. .read = &eventfd_read,
  72. .write = &eventfd_write,
  73. .poll = &eventfd_poll,
  74. };
  75. struct shim_mount eventfd_builtin_fs = {
  76. .type = URI_TYPE_EVENTFD,
  77. .fs_ops = &eventfd_fs_ops,
  78. };