fs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (C) 2014 Stony Brook University
  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 'str' filesystem.
  17. */
  18. #include <asm/fcntl.h>
  19. #include <asm/mman.h>
  20. #include <asm/prctl.h>
  21. #include <asm/unistd.h>
  22. #include <errno.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/stat.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <shim_fs.h>
  28. #include <shim_internal.h>
  29. #include <shim_profile.h>
  30. int str_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
  31. struct shim_str_data* data = dent->data;
  32. /* when str file is opened, it must have a data */
  33. if (!dent->data)
  34. return -ENOENT;
  35. REF_INC(data->ref_count);
  36. hdl->dentry = dent;
  37. hdl->flags = flags;
  38. return 0;
  39. }
  40. int str_dput(struct shim_dentry* dent) {
  41. struct shim_str_data* data = dent->data;
  42. if (!data || REF_DEC(data->ref_count) > 1)
  43. return 0;
  44. if (data->str) {
  45. free(data->str);
  46. data->str = NULL;
  47. }
  48. data->len = 0;
  49. data->buf_size = 0;
  50. free(dent->data);
  51. dent->data = NULL;
  52. return 0;
  53. }
  54. int str_close(struct shim_handle* hdl) {
  55. if (hdl->flags & (O_WRONLY | O_RDWR)) {
  56. int ret = str_flush(hdl);
  57. if (ret < 0)
  58. return ret;
  59. }
  60. str_dput(hdl->dentry);
  61. return 0;
  62. }
  63. ssize_t str_read(struct shim_handle* hdl, void* buf, size_t count) {
  64. ssize_t ret = 0;
  65. if (!(hdl->acc_mode & MAY_READ)) {
  66. ret = -EACCES;
  67. goto out;
  68. }
  69. struct shim_str_handle* strhdl = &hdl->info.str;
  70. assert(hdl->dentry);
  71. assert(strhdl->data);
  72. struct shim_str_data* data = strhdl->data;
  73. if (!data->str) {
  74. debug("str_data has no str\n");
  75. ret = -EACCES;
  76. goto out;
  77. }
  78. if (!strhdl->ptr)
  79. strhdl->ptr = data->str;
  80. off_t offset = strhdl->ptr - data->str;
  81. size_t remain = data->len - offset;
  82. if (count >= remain) {
  83. memcpy(buf, strhdl->ptr, remain);
  84. strhdl->ptr += remain;
  85. ret = remain;
  86. goto out;
  87. }
  88. memcpy(buf, strhdl->ptr, count);
  89. strhdl->ptr += count;
  90. ret = count;
  91. out:
  92. return ret;
  93. }
  94. ssize_t str_write(struct shim_handle* hdl, const void* buf, size_t count) {
  95. if (!(hdl->acc_mode & MAY_WRITE))
  96. return -EACCES;
  97. struct shim_str_handle* strhdl = &hdl->info.str;
  98. assert(hdl->dentry);
  99. assert(strhdl->data);
  100. struct shim_str_data* data = strhdl->data;
  101. if (!data->str || strhdl->ptr + count > data->str + data->buf_size) {
  102. int newlen = 0;
  103. if (data->str) {
  104. newlen = data->buf_size * 2;
  105. while (strhdl->ptr + count > data->str + newlen) {
  106. newlen *= 2;
  107. }
  108. } else {
  109. newlen = count;
  110. }
  111. char* newbuf = malloc(newlen);
  112. if (!newbuf)
  113. return -ENOMEM;
  114. if (data->str) {
  115. memcpy(newbuf, data->str, data->len);
  116. free(data->str);
  117. }
  118. strhdl->ptr = newbuf + (strhdl->ptr - data->str);
  119. data->str = newbuf;
  120. data->buf_size = newlen;
  121. }
  122. memcpy(strhdl->ptr, buf, count);
  123. strhdl->ptr += count;
  124. data->dirty = true;
  125. if (strhdl->ptr >= data->str + data->len)
  126. data->len = strhdl->ptr - data->str;
  127. return count;
  128. }
  129. off_t str_seek(struct shim_handle* hdl, off_t offset, int whence) {
  130. struct shim_str_handle* strhdl = &hdl->info.str;
  131. assert(hdl->dentry);
  132. assert(strhdl->data);
  133. struct shim_str_data* data = strhdl->data;
  134. switch (whence) {
  135. case SEEK_SET:
  136. if (offset < 0)
  137. return -EINVAL;
  138. strhdl->ptr = data->str;
  139. if (strhdl->ptr > data->str + data->len)
  140. strhdl->ptr = data->str + data->len;
  141. break;
  142. case SEEK_CUR:
  143. if (offset >= 0) {
  144. strhdl->ptr += offset;
  145. if (strhdl->ptr > data->str + data->len)
  146. strhdl->ptr = data->str + data->len;
  147. } else {
  148. strhdl->ptr -= offset;
  149. if (strhdl->ptr < data->str)
  150. strhdl->ptr = data->str;
  151. }
  152. break;
  153. case SEEK_END:
  154. if (offset < 0)
  155. return -EINVAL;
  156. strhdl->ptr = data->str + data->len - offset;
  157. if (strhdl->ptr < data->str)
  158. strhdl->ptr = data->str;
  159. break;
  160. }
  161. return strhdl->ptr - data->str;
  162. }
  163. int str_flush(struct shim_handle* hdl) {
  164. struct shim_str_handle* strhdl = &hdl->info.str;
  165. assert(hdl->dentry);
  166. assert(strhdl->data);
  167. struct shim_str_data* data = strhdl->data;
  168. if (!data->dirty)
  169. return 0;
  170. if (!data->modify)
  171. return -EACCES;
  172. return data->modify(hdl);
  173. }
  174. struct shim_fs_ops str_fs_ops = {
  175. .close = &str_close,
  176. .read = &str_read,
  177. .write = &str_write,
  178. .seek = &str_seek,
  179. .flush = &str_flush,
  180. };
  181. struct shim_d_ops str_d_ops = {
  182. .open = &str_open,
  183. .dput = &str_dput,
  184. };