fs.c 10 KB

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