fs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 'proc' 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 <pal.h>
  25. #include <pal_error.h>
  26. #include <shim_fs.h>
  27. #include <shim_handle.h>
  28. #include <shim_internal.h>
  29. #include <shim_thread.h>
  30. #include <shim_utils.h>
  31. // TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
  32. // should investigate and fix this behavior.
  33. #include <linux/stat.h>
  34. extern const struct proc_nm_ops nm_thread;
  35. extern const struct proc_fs_ops fs_thread;
  36. extern const struct proc_dir dir_thread;
  37. extern const struct proc_nm_ops nm_ipc_thread;
  38. extern const struct proc_fs_ops fs_ipc_thread;
  39. extern const struct proc_dir dir_ipc_thread;
  40. extern const struct proc_fs_ops fs_meminfo;
  41. extern const struct proc_fs_ops fs_cpuinfo;
  42. const struct proc_dir proc_root = {
  43. .size = 5,
  44. .ent =
  45. {
  46. {
  47. .name = "self",
  48. .fs_ops = &fs_thread,
  49. .dir = &dir_thread,
  50. },
  51. {
  52. .nm_ops = &nm_thread,
  53. .fs_ops = &fs_thread,
  54. .dir = &dir_thread,
  55. },
  56. {
  57. .nm_ops = &nm_ipc_thread,
  58. .fs_ops = &fs_ipc_thread,
  59. .dir = &dir_ipc_thread,
  60. },
  61. {
  62. .name = "meminfo",
  63. .fs_ops = &fs_meminfo,
  64. },
  65. {
  66. .name = "cpuinfo",
  67. .fs_ops = &fs_cpuinfo,
  68. },
  69. },
  70. };
  71. #define PROC_INO_BASE 1
  72. static int proc_root_mode(const char* name, mode_t* mode) {
  73. __UNUSED(name); // We know this is /proc
  74. *mode = 0555;
  75. return 0;
  76. }
  77. static int proc_root_stat(const char* name, struct stat* buf) {
  78. __UNUSED(name); // We know this is /proc
  79. memset(buf, 0, sizeof(struct stat));
  80. buf->st_dev = buf->st_ino = 1;
  81. buf->st_mode = 0555 | S_IFDIR;
  82. buf->st_uid = 0;
  83. buf->st_gid = 0;
  84. buf->st_size = 4096;
  85. return 0;
  86. }
  87. static int proc_root_open(struct shim_handle* hdl, const char* name, int flags) {
  88. __UNUSED(hdl); // this is a placeholder function
  89. __UNUSED(name); // We know this is /proc
  90. if (flags & (O_WRONLY | O_RDWR))
  91. return -EISDIR;
  92. // Don't really need to do any work here, but keeping as a placeholder,
  93. // just in case.
  94. return 0;
  95. }
  96. struct proc_fs_ops fs_proc_root = {
  97. .open = &proc_root_open,
  98. .mode = &proc_root_mode,
  99. .stat = &proc_root_stat,
  100. };
  101. const struct proc_ent proc_root_ent = {
  102. .name = "",
  103. .fs_ops = &fs_proc_root,
  104. .dir = &proc_root,
  105. };
  106. static inline int token_len(const char* str, const char** next_str) {
  107. const char* t = str;
  108. while (*t && *t != '/') {
  109. t++;
  110. }
  111. if (next_str)
  112. *next_str = *t ? t + 1 : NULL;
  113. return t - str;
  114. }
  115. static int proc_match_name(const char* trim_name, const struct proc_ent** ent) {
  116. if (!trim_name || !trim_name[0]) {
  117. *ent = &proc_root_ent;
  118. return 0;
  119. }
  120. const char* token = trim_name;
  121. const char* next_token;
  122. const struct proc_ent* tmp = proc_root.ent;
  123. const struct proc_ent* last = NULL;
  124. if (*token == '/')
  125. token++;
  126. while (token) {
  127. int tlen = token_len(token, &next_token);
  128. for (; tmp->name || tmp->nm_ops; tmp++) {
  129. if (tmp->name && !memcmp(tmp->name, token, tlen))
  130. goto found;
  131. if (tmp->nm_ops && tmp->nm_ops->match_name && tmp->nm_ops->match_name(trim_name))
  132. goto found;
  133. }
  134. return -ENOENT;
  135. found:
  136. if (!tmp->dir && next_token)
  137. return -ENOENT;
  138. last = tmp;
  139. tmp = tmp->dir->ent;
  140. token = next_token;
  141. }
  142. *ent = last;
  143. return 0;
  144. }
  145. static int proc_mode(struct shim_dentry* dent, mode_t* mode) {
  146. if (qstrempty(&dent->rel_path)) {
  147. dent->ino = PROC_INO_BASE;
  148. *mode = 0555 | S_IFDIR;
  149. return 0;
  150. }
  151. /* don't care about forced or not */
  152. const char* rel_path = qstrgetstr(&dent->rel_path);
  153. const struct proc_ent* ent;
  154. int ret = proc_match_name(rel_path, &ent);
  155. if (ret < 0)
  156. return ret;
  157. if (!ent->fs_ops || !ent->fs_ops->mode)
  158. return -EACCES;
  159. return ent->fs_ops->mode(rel_path, mode);
  160. }
  161. static int proc_lookup(struct shim_dentry* dent) {
  162. if (qstrempty(&dent->rel_path)) {
  163. dent->ino = PROC_INO_BASE;
  164. dent->state |= DENTRY_ISDIRECTORY;
  165. return 0;
  166. }
  167. /* don't care about forced or not */
  168. const struct proc_ent* ent = NULL;
  169. int ret = proc_match_name(qstrgetstr(&dent->rel_path), &ent);
  170. if (!ret && ent->dir)
  171. dent->state |= DENTRY_ISDIRECTORY;
  172. if (ent && ent->fs_ops && ent->fs_ops->follow_link)
  173. dent->state |= DENTRY_ISLINK;
  174. return ret;
  175. }
  176. static int proc_mount(const char* uri, void** mount_data) {
  177. // Arguments for compatibility with other FSes
  178. __UNUSED(uri);
  179. __UNUSED(mount_data);
  180. /* do nothing */
  181. return 0;
  182. }
  183. static int proc_unmount(void* mount_data) {
  184. // Arguments for compatibility with other FSes
  185. __UNUSED(mount_data);
  186. /* do nothing */
  187. return 0;
  188. }
  189. static int proc_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
  190. const char* rel_path = qstrgetstr(&dent->rel_path);
  191. if (flags & (O_CREAT | O_EXCL))
  192. return -EACCES;
  193. const struct proc_ent* ent;
  194. int ret;
  195. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  196. return ret;
  197. if (!ent->fs_ops || !ent->fs_ops->open)
  198. return -EACCES;
  199. hdl->flags = flags;
  200. return ent->fs_ops->open(hdl, rel_path, flags);
  201. }
  202. static int proc_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
  203. const char* rel_path = qstrgetstr(&dent->rel_path);
  204. const struct proc_ent* ent;
  205. int ret;
  206. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  207. return ret;
  208. if (!ent->dir)
  209. return -ENOTDIR;
  210. const struct proc_ent* tmp = ent->dir->ent;
  211. const struct proc_ent* end = tmp + ent->dir->size;
  212. HASHTYPE self_hash = hash_path(rel_path, dent->rel_path.len);
  213. HASHTYPE new_hash;
  214. struct shim_dirent* buf;
  215. struct shim_dirent* ptr;
  216. int buf_size = MAX_PATH;
  217. retry:
  218. buf = malloc(buf_size);
  219. *dirent = ptr = buf;
  220. struct shim_dirent** last = dirent;
  221. for (; tmp < end; tmp++) {
  222. if (tmp->name) {
  223. int name_len = strlen(tmp->name);
  224. if ((void*)(ptr + 1) + name_len + 1 > (void*)buf + buf_size)
  225. goto enlarge;
  226. new_hash = rehash_name(self_hash, tmp->name, name_len);
  227. ptr->next = (void*)(ptr + 1) + name_len + 1;
  228. ptr->ino = new_hash;
  229. ptr->type =
  230. tmp->dir ? LINUX_DT_DIR
  231. : (tmp->fs_ops && tmp->fs_ops->follow_link ? LINUX_DT_LNK : LINUX_DT_REG);
  232. memcpy(ptr->name, tmp->name, name_len + 1);
  233. last = &ptr->next;
  234. ptr = *last;
  235. continue;
  236. }
  237. if (tmp->nm_ops && tmp->nm_ops->list_name) {
  238. struct shim_dirent* d = ptr;
  239. int ret = tmp->nm_ops->list_name(rel_path, &ptr, (void*)buf + buf_size - (void*)ptr);
  240. if (ret == -ENOBUFS)
  241. goto enlarge;
  242. if (ret < 0)
  243. ptr = d;
  244. else
  245. for (; d && d != ptr; d = d->next) {
  246. last = &d->next;
  247. }
  248. continue;
  249. }
  250. }
  251. *last = NULL;
  252. if (!(*dirent))
  253. free(buf);
  254. return 0;
  255. enlarge:
  256. buf_size *= 2;
  257. free(buf);
  258. goto retry;
  259. }
  260. static int proc_stat(struct shim_dentry* dent, struct stat* buf) {
  261. const char* rel_path = qstrgetstr(&dent->rel_path);
  262. const struct proc_ent* ent;
  263. int ret;
  264. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  265. return ret;
  266. if (!ent->fs_ops || !ent->fs_ops->stat)
  267. return -EACCES;
  268. return ent->fs_ops->stat(rel_path, buf);
  269. }
  270. static int proc_follow_link(struct shim_dentry* dent, struct shim_qstr* link) {
  271. const char* rel_path = qstrgetstr(&dent->rel_path);
  272. const struct proc_ent* ent;
  273. int ret;
  274. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  275. return ret;
  276. if (!ent->fs_ops || !ent->fs_ops->follow_link)
  277. return -EINVAL;
  278. return ent->fs_ops->follow_link(rel_path, link);
  279. }
  280. static int proc_hstat(struct shim_handle* hdl, struct stat* buf) {
  281. struct shim_dentry* dent = hdl->dentry;
  282. assert(dent);
  283. const char* rel_path = qstrgetstr(&dent->rel_path);
  284. const struct proc_ent* ent;
  285. int ret;
  286. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  287. return ret;
  288. if (!ent->fs_ops || !ent->fs_ops->stat)
  289. return -EACCES;
  290. return ent->fs_ops->stat(rel_path, buf);
  291. }
  292. struct shim_fs_ops proc_fs_ops = {
  293. .mount = &proc_mount,
  294. .unmount = &proc_unmount,
  295. .close = &str_close,
  296. .read = &str_read,
  297. .write = &str_write,
  298. .seek = &str_seek,
  299. .flush = &str_flush,
  300. .hstat = &proc_hstat,
  301. };
  302. struct shim_d_ops proc_d_ops = {
  303. .open = &proc_open,
  304. .stat = &proc_stat,
  305. .mode = &proc_mode,
  306. .lookup = &proc_lookup,
  307. .follow_link = &proc_follow_link,
  308. .readdir = &proc_readdir,
  309. };