fs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 'dev' filesystem.
  17. */
  18. #define __KERNEL__
  19. #include <asm/fcntl.h>
  20. #include <asm/mman.h>
  21. #include <asm/prctl.h>
  22. #include <asm/unistd.h>
  23. #include <errno.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/stat.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <shim_fs.h>
  29. #include <shim_handle.h>
  30. #include <shim_internal.h>
  31. #include <shim_profile.h>
  32. #include <shim_utils.h>
  33. #define EMPTY_DEV_OPS \
  34. { \
  35. .open = NULL, \
  36. .close = NULL, \
  37. .read = NULL, \
  38. .write = NULL, \
  39. .flush = NULL, \
  40. .seek = NULL, \
  41. .truncate = NULL, \
  42. .mode = NULL, \
  43. .stat = NULL, \
  44. .hstat = NULL, \
  45. }
  46. #define DEV_INO_BASE 1025
  47. static ssize_t dev_null_read(struct shim_handle* hdl, void* buf, size_t count) {
  48. // Arguments for compatibility
  49. __UNUSED(hdl);
  50. __UNUSED(buf);
  51. __UNUSED(count);
  52. return 0;
  53. }
  54. static ssize_t dev_zero_read(struct shim_handle* hdl, void* buf, size_t count) {
  55. // Argument for compatibility
  56. __UNUSED(hdl);
  57. memset(buf, 0, count);
  58. return count;
  59. }
  60. static ssize_t dev_null_write(struct shim_handle* hdl, const void* buf, size_t count) {
  61. // Arguments for compatibility
  62. __UNUSED(hdl);
  63. __UNUSED(buf);
  64. __UNUSED(count);
  65. return count;
  66. }
  67. static int dev_null_mode(const char* name, mode_t* mode) {
  68. __UNUSED(name); // We know it is /dev/null
  69. *mode = 0666 | S_IFCHR;
  70. return 0;
  71. }
  72. static int dev_null_stat(const char* name, struct stat* stat) {
  73. __UNUSED(name); // We know it is /dev/null
  74. stat->st_mode = 0666 | S_IFCHR;
  75. stat->st_uid = 0;
  76. stat->st_gid = 0;
  77. stat->st_size = 0;
  78. stat->st_blksize = 0;
  79. return 0;
  80. }
  81. static int dev_null_hstat(struct shim_handle* hdl, struct stat* stat) {
  82. __UNUSED(hdl); // We know it is /dev/null
  83. stat->st_mode = 0666 | S_IFCHR;
  84. stat->st_uid = 0;
  85. stat->st_gid = 0;
  86. stat->st_size = 0;
  87. stat->st_blksize = 0;
  88. return 0;
  89. }
  90. static int dev_null_truncate(struct shim_handle* hdl, uint64_t size) {
  91. // Arguments for compatibility
  92. __UNUSED(hdl);
  93. __UNUSED(size);
  94. return 0;
  95. }
  96. static int dev_random_mode(const char* name, mode_t* mode) {
  97. __UNUSED(name); // We know it is /dev/random
  98. *mode = 0666 | S_IFCHR;
  99. return 0;
  100. }
  101. static ssize_t dev_urandom_read(struct shim_handle* hdl, void* buf, size_t count) {
  102. __UNUSED(hdl);
  103. ssize_t ret = DkRandomBitsRead(buf, count);
  104. if (ret < 0)
  105. return -convert_pal_errno(-ret);
  106. return count;
  107. }
  108. static ssize_t dev_random_read(struct shim_handle* hdl, void* buf, size_t count) {
  109. return dev_urandom_read(hdl, buf, count);
  110. }
  111. static int dev_random_stat(const char* name, struct stat* stat) {
  112. __UNUSED(name); // we know it is /dev/random
  113. stat->st_mode = 0666 | S_IFCHR;
  114. stat->st_uid = 0;
  115. stat->st_gid = 0;
  116. stat->st_size = 0;
  117. stat->st_blksize = 0;
  118. return 0;
  119. }
  120. static int dev_random_hstat(struct shim_handle* hdl, struct stat* stat) {
  121. __UNUSED(hdl); // pseudo-device
  122. stat->st_mode = 0444 | S_IFCHR;
  123. stat->st_uid = 0;
  124. stat->st_gid = 0;
  125. stat->st_size = 0;
  126. stat->st_blksize = 0;
  127. return 0;
  128. }
  129. static int search_dev_driver(const char* name, struct shim_dev_ops* ops) {
  130. if (!strcmp_static(name, "null") || !strcmp_static(name, "tty")) {
  131. if (ops)
  132. ops->read = &dev_null_read;
  133. null_dev:
  134. if (ops) {
  135. ops->write = &dev_null_write;
  136. ops->truncate = &dev_null_truncate;
  137. ops->mode = &dev_null_mode;
  138. ops->stat = &dev_null_stat;
  139. ops->hstat = &dev_null_hstat;
  140. }
  141. return 0;
  142. }
  143. if (!strcmp_static(name, "zero")) {
  144. if (ops)
  145. ops->read = &dev_zero_read;
  146. goto null_dev;
  147. }
  148. if (!strcmp_static(name, "random")) {
  149. if (ops)
  150. ops->read = &dev_random_read;
  151. random_dev:
  152. if (ops) {
  153. ops->mode = &dev_random_mode;
  154. ops->stat = &dev_random_stat;
  155. ops->hstat = &dev_random_hstat;
  156. }
  157. return 0;
  158. }
  159. if (!strcmp_static(name, "urandom")) {
  160. if (ops)
  161. ops->read = &dev_urandom_read;
  162. goto random_dev;
  163. }
  164. if (!strcmp_static(name, "stdin") || !strcmp_static(name, "stdout") ||
  165. !strcmp_static(name, "stderr"))
  166. return -EISLINK;
  167. return -ENOENT;
  168. }
  169. static int dev_mount(const char* uri, void** mount_data) {
  170. // Arguments for compatibility
  171. __UNUSED(uri);
  172. __UNUSED(mount_data);
  173. /* do nothing */
  174. return 0;
  175. }
  176. static int dev_unmount(void* mount_data) {
  177. // Arguments for compatibility
  178. __UNUSED(mount_data);
  179. /* do nothing */
  180. return 0;
  181. }
  182. static int dev_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
  183. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  184. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  185. if (ret < 0)
  186. return ret;
  187. hdl->type = TYPE_DEV;
  188. hdl->flags = flags & ~O_ACCMODE;
  189. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  190. memcpy(&hdl->info.dev.dev_ops, &ops_buf, sizeof(struct shim_dev_ops));
  191. if (!ops_buf.read && (hdl->acc_mode & MAY_READ))
  192. return -EACCES;
  193. if (!ops_buf.write && (hdl->acc_mode & MAY_WRITE))
  194. return -EACCES;
  195. if (ops_buf.open)
  196. return ops_buf.open(hdl, qstrgetstr(&dent->rel_path), flags);
  197. return 0;
  198. }
  199. static int dev_lookup(struct shim_dentry* dent) {
  200. if (qstrempty(&dent->rel_path)) {
  201. dent->state |= DENTRY_ISDIRECTORY;
  202. dent->ino = DEV_INO_BASE;
  203. return 0;
  204. }
  205. /* we don't care about forced or not */
  206. return search_dev_driver(qstrgetstr(&dent->rel_path), NULL);
  207. }
  208. static int dev_mode(struct shim_dentry* dent, mode_t* mode) {
  209. if (qstrempty(&dent->rel_path)) {
  210. dent->ino = DEV_INO_BASE;
  211. *mode = 0555 | S_IFDIR;
  212. return 0;
  213. }
  214. /* we don't care about forced or not */
  215. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  216. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  217. if (ret < 0)
  218. return ret;
  219. return ops_buf.mode(qstrgetstr(&dent->rel_path), mode);
  220. }
  221. static int dev_flush(struct shim_handle* hdl) {
  222. if (!hdl->info.dev.dev_ops.flush)
  223. return 0;
  224. return hdl->info.dev.dev_ops.flush(hdl);
  225. }
  226. static int dev_close(struct shim_handle* hdl) {
  227. if (!hdl->info.dev.dev_ops.close)
  228. return 0;
  229. return hdl->info.dev.dev_ops.close(hdl);
  230. }
  231. static ssize_t dev_read(struct shim_handle* hdl, void* buf, size_t count) {
  232. if (!hdl->info.dev.dev_ops.read)
  233. return -EACCES;
  234. return hdl->info.dev.dev_ops.read(hdl, buf, count);
  235. }
  236. static ssize_t dev_write(struct shim_handle* hdl, const void* buf, size_t count) {
  237. if (!hdl->info.dev.dev_ops.write)
  238. return -EACCES;
  239. return hdl->info.dev.dev_ops.write(hdl, buf, count);
  240. }
  241. static off_t dev_seek(struct shim_handle* hdl, off_t offset, int wence) {
  242. if (!hdl->info.dev.dev_ops.seek)
  243. return -EACCES;
  244. return hdl->info.dev.dev_ops.seek(hdl, offset, wence);
  245. }
  246. static int dev_truncate(struct shim_handle* hdl, off_t len) {
  247. if (!hdl->info.dev.dev_ops.truncate)
  248. return -EACCES;
  249. return hdl->info.dev.dev_ops.truncate(hdl, len);
  250. }
  251. static int dev_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
  252. if (!qstrempty(&dent->rel_path)) {
  253. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  254. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  255. if (ret < 0 && ret != -EISLINK)
  256. return ret;
  257. return -ENOTDIR;
  258. }
  259. struct shim_dirent *buf, *ptr;
  260. int buf_size = MAX_PATH;
  261. retry:
  262. buf = malloc(buf_size);
  263. *dirent = ptr = buf;
  264. struct shim_dirent** last = dirent;
  265. #define COPY_ENTRY(devname, devtype) \
  266. do { \
  267. int name_len = strlen(devname); \
  268. \
  269. if ((void*)(ptr + 1) + name_len + 1 > (void*)buf + buf_size) \
  270. goto nomem; \
  271. \
  272. ptr->next = (void*)(ptr + 1) + name_len + 1; \
  273. ptr->ino = 1; \
  274. ptr->type = (devtype); \
  275. memcpy(ptr->name, (devname), name_len + 1); \
  276. last = &ptr->next; \
  277. ptr = ptr->next; \
  278. } while (0)
  279. COPY_ENTRY("null", LINUX_DT_CHR);
  280. COPY_ENTRY("zero", LINUX_DT_CHR);
  281. COPY_ENTRY("stdin", LINUX_DT_LNK);
  282. COPY_ENTRY("stdout", LINUX_DT_LNK);
  283. COPY_ENTRY("stderr", LINUX_DT_LNK);
  284. #undef COPY_ENTRY
  285. *last = NULL;
  286. return 0;
  287. nomem:
  288. buf_size *= 2;
  289. free(buf);
  290. goto retry;
  291. }
  292. static int dev_stat(struct shim_dentry* dent, struct stat* buf) {
  293. if (qstrempty(&dent->rel_path)) {
  294. buf->st_dev = DEV_INO_BASE;
  295. buf->st_ino = DEV_INO_BASE;
  296. buf->st_mode = 0777 | S_IFDIR;
  297. buf->st_size = 4096;
  298. buf->st_blksize = 4096;
  299. return 0;
  300. }
  301. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  302. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  303. if (ret < 0 && ret != -EISLINK)
  304. return ret;
  305. if (ret == -EISLINK) {
  306. buf->st_dev = DEV_INO_BASE;
  307. buf->st_ino = DEV_INO_BASE;
  308. buf->st_mode = 0777 | S_IFLNK;
  309. buf->st_size = 0;
  310. buf->st_blksize = 0;
  311. return 0;
  312. }
  313. buf->st_dev = DEV_INO_BASE;
  314. buf->st_ino = DEV_INO_BASE;
  315. return ops_buf.stat ? ops_buf.stat(qstrgetstr(&dent->rel_path), buf) : -EACCES;
  316. }
  317. static int dev_hstat(struct shim_handle* hdl, struct stat* buf) {
  318. if (!hdl->info.dev.dev_ops.hstat)
  319. return -EACCES;
  320. return hdl->info.dev.dev_ops.hstat(hdl, buf);
  321. }
  322. static off_t dev_poll(struct shim_handle* hdl, int poll_type) {
  323. if (poll_type == FS_POLL_SZ)
  324. return 0;
  325. off_t ret = 0;
  326. if ((poll_type & FS_POLL_RD) && hdl->info.dev.dev_ops.read)
  327. ret |= FS_POLL_RD;
  328. if ((poll_type & FS_POLL_WR) && hdl->info.dev.dev_ops.write)
  329. ret |= FS_POLL_WR;
  330. return ret;
  331. }
  332. static int dev_follow_link(struct shim_dentry* dent, struct shim_qstr* link) {
  333. const char* name = qstrgetstr(&dent->rel_path);
  334. if (!strcmp_static(name, "stdin")) {
  335. qstrsetstr(link, "/proc/self/0", static_strlen("/proc/self/0"));
  336. return 0;
  337. } else if (!strcmp_static(name, "stdout")) {
  338. qstrsetstr(link, "/proc/self/1", static_strlen("/proc/self/1"));
  339. return 0;
  340. } else if (!strcmp_static(name, "stderr")) {
  341. qstrsetstr(link, "/proc/self/2", static_strlen("/proc/self/2"));
  342. return 0;
  343. }
  344. if (!strcmp_static(name, "null") || !strcmp_static(name, "zero"))
  345. return -ENOTLINK;
  346. return -ENOENT;
  347. }
  348. int dev_update_dev_ops(struct shim_handle* hdl) {
  349. int ret;
  350. char buf[STR_SIZE];
  351. size_t bufsize = sizeof(buf);
  352. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  353. assert(hdl && hdl->type == TYPE_DEV);
  354. ret = get_base_name(qstrgetstr(&hdl->path), buf, &bufsize);
  355. if (ret < 0)
  356. return -ENOENT;
  357. ret = search_dev_driver(buf, &ops_buf);
  358. if (ret < 0)
  359. return ret;
  360. memcpy(&hdl->info.dev.dev_ops, &ops_buf, sizeof(ops_buf));
  361. return 0;
  362. }
  363. struct shim_fs_ops dev_fs_ops = {
  364. .mount = &dev_mount,
  365. .unmount = &dev_unmount,
  366. .flush = &dev_flush,
  367. .close = &dev_close,
  368. .read = &dev_read,
  369. .write = &dev_write,
  370. .seek = &dev_seek,
  371. .hstat = &dev_hstat,
  372. .poll = &dev_poll,
  373. .truncate = &dev_truncate,
  374. };
  375. struct shim_d_ops dev_d_ops = {
  376. .open = &dev_open,
  377. .lookup = &dev_lookup,
  378. .mode = &dev_mode,
  379. .readdir = &dev_readdir,
  380. .stat = &dev_stat,
  381. .follow_link = &dev_follow_link,
  382. };