shim_fs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_fs.h
  17. *
  18. * Definitions of types and functions for file system bookkeeping.
  19. */
  20. #ifndef _SHIM_FS_H_
  21. #define _SHIM_FS_H_
  22. #include <shim_types.h>
  23. #include <shim_defs.h>
  24. #include <shim_handle.h>
  25. #include <shim_utils.h>
  26. #include <pal.h>
  27. #include <linux_list.h>
  28. struct shim_handle;
  29. #define FS_POLL_RD 0x01
  30. #define FS_POLL_WR 0x02
  31. #define FS_POLL_ER 0x04
  32. #define FS_POLL_SZ 0x08
  33. struct shim_fs_ops {
  34. /* mount: moun an uri to the certain location */
  35. int (*mount) (const char * uri, const char * root, void ** mount_data);
  36. int (*unmount) (void * mount_data);
  37. /* close: clean up the file state inside the handle */
  38. int (*close) (struct shim_handle * hdl);
  39. /* read: the content from the file opened as handle */
  40. int (*read) (struct shim_handle * hdl, void * buf, size_t count);
  41. /* write: the content from the file opened as handle */
  42. int (*write) (struct shim_handle * hdl, const void * buf, size_t count);
  43. /* mmap: mmap handle to address */
  44. int (*mmap) (struct shim_handle * hdl, void ** addr, size_t size,
  45. int prot, int flags, off_t offset);
  46. /* flush: flush out user buffer */
  47. int (*flush) (struct shim_handle * hdl);
  48. /* seek: the content from the file opened as handle */
  49. int (*seek) (struct shim_handle * hdl, off_t offset, int wence);
  50. /* move, copy: rename or duplicate the file */
  51. int (*move) (const char * trim_old_name, const char * trim_new_name);
  52. int (*copy) (const char * trim_old_name, const char * trim_new_name);
  53. int (*truncate) (struct shim_handle * hdl, int len);
  54. /* hstat: get status of the file */
  55. int (*hstat) (struct shim_handle * hdl, struct stat * buf);
  56. /* setflags: set flags of the file */
  57. int (*setflags) (struct shim_handle * hdl, int flags);
  58. /* hput: delete the handle and close the PAL handle. */
  59. void (*hput) (struct shim_handle * hdl);
  60. /* lock and unlock the file */
  61. int (*lock) (const char * trim_name);
  62. int (*unlock) (const char * trim_name);
  63. /* lock and unlock the file system */
  64. int (*lockfs) (void);
  65. int (*unlockfs) (void);
  66. /* checkout/reowned/checkin a single handle for migration */
  67. int (*checkout) (struct shim_handle * hdl);
  68. int (*checkin) (struct shim_handle * hdl);
  69. /* poll a single handle */
  70. /* POLL_RD|POLL_WR: return POLL_RD|POLL_WR for readable|writeable,
  71. POLL_ER for failure, -EAGAIN for unknown. */
  72. /* POLL_SZ: return total size */
  73. int (*poll) (struct shim_handle * hdl, int poll_type);
  74. /* checkpoint/migrate the filesystem */
  75. int (*checkpoint) (void ** checkpoint, void * mount_data);
  76. int (*migrate) (void * checkpoint, void ** mount_data);
  77. };
  78. #define DENTRY_VALID 0x0001 /* this dentry is verified to be valid */
  79. #define DENTRY_NEGATIVE 0x0002 /* negative, recently deleted */
  80. #define DENTRY_RECENTLY 0x0004 /* recently used */
  81. #define DENTRY_PERSIST 0x0008 /* added as a persistent dentry */
  82. #define DENTRY_HASHED 0x0010 /* added in the dcache */
  83. #define DENTRY_MOUNTPOINT 0x0040 /* this dentry is a mount point */
  84. #define DENTRY_ISLINK 0x0080 /* this dentry is a link */
  85. #define DENTRY_ISDIRECTORY 0x0100 /* this dentry is a directory */
  86. #define DENTRY_LOCKED 0x0200 /* locked by mountpoints at children */
  87. #define DENTRY_REACHABLE 0x0400 /* permission checked to be reachable */
  88. #define DENTRY_UNREACHABLE 0x0800 /* permission checked to be unreachable */
  89. #define DENTRY_LISTED 0x1000 /* children in directory listed */
  90. #define DENTRY_INO_UPDATED 0x2000 /* ino updated */
  91. #define DENTRY_ANCESTER 0x4000
  92. #define DCACHE_HASH_SIZE 1024
  93. #define DCACHE_HASH(hash) ((hash) & (DCACHE_HASH_SIZE - 1))
  94. struct shim_dentry {
  95. int state; /* flags for managing state */
  96. struct shim_mount * fs; /* this dentry's mounted fs */
  97. struct shim_qstr rel_path; /* the path is relative to
  98. its mount point */
  99. struct shim_qstr name; /* caching the file's name. */
  100. struct hlist_node hlist; /* to resolve collisions in
  101. the hash table */
  102. struct list_head list; /* put dentry to different list
  103. according to its availability,
  104. persistent or freeable */
  105. struct shim_dentry * parent;
  106. int nchildren;
  107. struct list_head children;
  108. struct list_head siblings;
  109. struct shim_mount * mounted;
  110. void * data;
  111. unsigned long ino;
  112. mode_t type;
  113. mode_t mode;
  114. LOCKTYPE lock;
  115. REFTYPE ref_count;
  116. };
  117. struct shim_d_ops {
  118. /* open: provide a filename relative to the mount point and flags,
  119. modify the shim handle, file_data is "inode" equivalent */
  120. int (*open) (struct shim_handle * hdl, struct shim_dentry * dent,
  121. int flags);
  122. /* look up dentry and allocate internal data */
  123. int (*lookup) (struct shim_dentry * dent, bool force);
  124. /* this is to check file type and access, returning the stat.st_mode */
  125. int (*mode) (struct shim_dentry * dent, mode_t * mode, bool force);
  126. /* detach internal data from dentry */
  127. int (*dput) (struct shim_dentry * dent);
  128. /* create a dentry inside a directory */
  129. int (*creat) (struct shim_handle * hdl, struct shim_dentry * dir,
  130. struct shim_dentry * dent, int flags, mode_t mode);
  131. /* unlink a dentry inside a directory */
  132. int (*unlink) (struct shim_dentry * dir, struct shim_dentry * dent);
  133. /* create a directory inside a directory */
  134. int (*mkdir) (struct shim_dentry * dir, struct shim_dentry * dent,
  135. mode_t mode);
  136. /* stat: get status of the file */
  137. int (*stat) (struct shim_dentry * dent, struct stat * buf);
  138. /* extracts the symlink name and saves in link */
  139. int (*follow_link) (struct shim_dentry * dent, struct shim_qstr * link);
  140. /* set up symlink name to a dentry */
  141. int (*set_link) (struct shim_dentry * dent, const char * link);
  142. /* change the mode or owner of a dentry */
  143. int (*chmod) (struct shim_dentry * dent, mode_t mode);
  144. int (*chown) (struct shim_dentry * dent, int uid, int gid);
  145. /* change the name of a dentry */
  146. int (*rename) (struct shim_dentry * old, struct shim_dentry * new);
  147. /* readdir: given the path relative to the mount point, read the childs
  148. into the the buffer */
  149. int (*readdir) (struct shim_dentry * dent, struct shim_dirent ** dirent);
  150. };
  151. #define MAX_PATH 4096
  152. struct shim_mount {
  153. char type[8];
  154. struct shim_dentry * mount_point;
  155. struct shim_qstr path;
  156. struct shim_qstr uri;
  157. struct shim_fs_ops * fs_ops;
  158. struct shim_d_ops * d_ops;
  159. struct shim_dentry * root;
  160. void * data;
  161. void * cpdata;
  162. size_t cpsize;
  163. REFTYPE ref_count;
  164. struct hlist_node hlist;
  165. struct list_head list;
  166. };
  167. extern struct shim_dentry * dentry_root;
  168. #define LOOKUP_FOLLOW 001
  169. #define LOOKUP_DIRECTORY 002
  170. #define LOOKUP_CONTINUE 004
  171. #define LOOKUP_PARENT 010
  172. #define MAY_EXEC 001
  173. #define MAY_WRITE 002
  174. #define MAY_READ 004
  175. #if 0
  176. #define MAY_APPEND 010
  177. #endif
  178. #define NO_MODE ((mode_t) -1)
  179. #define ACC_MODE(x) ((((x) == O_RDONLY || (x) == O_RDWR) ? MAY_READ : 0) | \
  180. (((x) == O_WRONLY || (x) == O_RDWR) ? MAY_WRITE : 0))
  181. #define LOOKUP_OPEN 0100
  182. #define LOOKUP_CREATE 0200
  183. #define LOOKUP_ACCESS 0400
  184. #define LOOKUP_SYNC (LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_ACCESS)
  185. enum lookup_type {
  186. LAST_NORM,
  187. LAST_ROOT,
  188. LAST_DOT,
  189. LAST_DOTDOT,
  190. LAST_BIND
  191. };
  192. struct lookup {
  193. struct shim_dentry * dentry;
  194. struct shim_mount * mount;
  195. const char * last;
  196. int depth;
  197. int flags;
  198. enum lookup_type last_type;
  199. };
  200. long get_dcache_stats (const char * name);
  201. void path_acquire (struct lookup * look);
  202. void path_release (struct lookup * look);
  203. /* initialization for fs and mounts */
  204. int init_config (const char ** envp);
  205. int init_fs (void);
  206. int reinit_fs (void);
  207. int init_mount_root (void);
  208. int init_mount (void);
  209. /* path utilities */
  210. const char * get_file_name (const char * path, size_t len);
  211. int get_abs_path (const char * cwd, const char * path, char * buf,
  212. int size);
  213. int get_norm_path (const char * path, char * buf, int size);
  214. /* file system operations */
  215. int mount_fs (const char * mount_type, const char * mount_uri,
  216. const char * mount_point);
  217. int unmount_fs (const char * mount_point);
  218. int readdir_fs (HASHTYPE hash, struct shim_dirent ** dirent);
  219. int search_builtin_fs (const char * type, struct shim_mount ** fs);
  220. void get_mount (struct shim_mount * mount);
  221. void put_mount (struct shim_mount * mount);
  222. #include <shim_utils.h>
  223. static inline void set_handle_fs (struct shim_handle * hdl,
  224. struct shim_mount * fs)
  225. {
  226. get_mount(fs);
  227. hdl->fs = fs;
  228. memcpy(hdl->fs_type, fs->type, sizeof(hdl->fs_type));
  229. }
  230. int walk_mounts (int (*walk) (struct shim_mount * mount, void * arg),
  231. void * arg);
  232. /* functions for dcache supports */
  233. int init_dcache (void);
  234. int reinit_dcache (void);
  235. extern LOCKTYPE dcache_lock;
  236. int permission (struct shim_dentry * dent, int mask, bool force);
  237. int lookup_dentry (struct shim_dentry * base, const char * name, int namelen,
  238. bool force, struct shim_dentry ** new);
  239. int __path_lookupat (struct shim_dentry * start, const char * path, int flags,
  240. struct shim_dentry ** dent);
  241. int path_lookupat (struct shim_dentry * start, const char * name, int flags,
  242. struct shim_dentry ** dent);
  243. int path_startat (int dfd, struct shim_dentry ** dir);
  244. int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
  245. const char * path, int flags, int mode,
  246. struct shim_dentry ** dent);
  247. int dentry_open (struct shim_handle * hdl, struct shim_dentry * dent,
  248. int flags);
  249. int directory_open (struct shim_handle * hdl, struct shim_dentry * dent,
  250. int flags);
  251. void get_dentry (struct shim_dentry * dent);
  252. void put_dentry (struct shim_dentry * dent);
  253. static inline __attribute__((always_inline))
  254. void fast_pathcpy (char * dst, const char * src, int size, char ** ptr)
  255. {
  256. char * d = dst;
  257. const char * s = src;
  258. for (int i = 0 ; i < size ; i++, s++, d++)
  259. *d = *s;
  260. *ptr = d;
  261. }
  262. static inline __attribute__((always_inline))
  263. char * dentry_get_path (struct shim_dentry * dent, bool on_stack,
  264. int * sizeptr)
  265. {
  266. struct shim_mount * fs = dent->fs;
  267. char * buffer, * c;
  268. int bufsize = dent->rel_path.len + 1;
  269. if (fs)
  270. bufsize += fs->path.len + 1;
  271. if (on_stack) {
  272. c = buffer = __alloca(bufsize);
  273. } else {
  274. if (!(c = buffer = malloc(bufsize)))
  275. return NULL;
  276. }
  277. if (fs && !qstrempty(&fs->path))
  278. fast_pathcpy(c, qstrgetstr(&fs->path), fs->path.len, &c);
  279. if (dent->rel_path.len) {
  280. const char * path = qstrgetstr(&dent->rel_path);
  281. int len = dent->rel_path.len;
  282. if (c > buffer && *(c - 1) == '/') {
  283. if (*path == '/')
  284. path++;
  285. } else {
  286. if (*path != '/')
  287. *(c++) = '/';
  288. }
  289. fast_pathcpy(c, path, len, &c);
  290. }
  291. if (sizeptr)
  292. *sizeptr = c - buffer;
  293. *c = 0;
  294. return buffer;
  295. }
  296. static inline __attribute__((always_inline))
  297. const char * dentry_get_name (struct shim_dentry * dent)
  298. {
  299. return qstrgetstr(&dent->name);
  300. }
  301. struct shim_dentry * get_new_dentry (struct shim_dentry * parent,
  302. const char * name, int namelen);
  303. void __set_parent_dentry (struct shim_dentry * child,
  304. struct shim_dentry * parent);
  305. void __unset_parent_dentry (struct shim_dentry * child,
  306. struct shim_dentry * parent);
  307. void __add_dcache (struct shim_dentry * dent, HASHTYPE * hashptr);
  308. void add_dcache (struct shim_dentry * dent, HASHTYPE * hashptr);
  309. void __del_dcache (struct shim_dentry * dent);
  310. void del_dcache (struct shim_dentry * dent);
  311. struct shim_dentry *
  312. __lookup_dcache (struct shim_dentry * start, const char * name, int namelen,
  313. const char * path, int pathlen, HASHTYPE * hashptr);
  314. struct shim_dentry *
  315. lookup_dcache (struct shim_dentry * start, const char * name, int namelen,
  316. const char * path, int pathlen, HASHTYPE * hashptr);
  317. int __del_dentry_tree(struct shim_dentry * root);
  318. /* hashing utilities */
  319. #define MOUNT_HASH_BYTE 1
  320. #define MOUNT_HASH_WIDTH 8
  321. #define MOUNT_HASH_SIZE 256
  322. #define MOUNT_HASH(hash) ((hash) & (MOUNT_HASH_SIZE - 1))
  323. HASHTYPE hash_path (const char * path, int size,
  324. const char * sep);
  325. HASHTYPE hash_parent_path (HASHTYPE hbuf, const char * name,
  326. int * size, const char * sep);
  327. HASHTYPE rehash_name (HASHTYPE parent_hbuf,
  328. const char * name, int size);
  329. HASHTYPE rehash_path (HASHTYPE ancester_hbuf,
  330. const char * path, int size, const char * sep);
  331. extern struct shim_fs_ops chroot_fs_ops;
  332. extern struct shim_d_ops chroot_d_ops;
  333. extern struct shim_fs_ops str_fs_ops;
  334. extern struct shim_d_ops str_d_ops;
  335. extern struct shim_fs_ops dev_fs_ops;
  336. extern struct shim_d_ops dev_d_ops;
  337. extern struct shim_fs_ops config_fs_ops;
  338. extern struct shim_d_ops config_d_ops;
  339. extern struct shim_fs_ops proc_fs_ops;
  340. extern struct shim_d_ops proc_d_ops;
  341. extern struct shim_mount chroot_builtin_fs;
  342. extern struct shim_mount pipe_builtin_fs;
  343. extern struct shim_mount socket_builtin_fs;
  344. extern struct shim_mount epoll_builtin_fs;
  345. /* proc file system */
  346. struct proc_nm_ops {
  347. int (*match_name) (const char * name);
  348. int (*list_name) (const char * name, struct shim_dirent ** buf,
  349. int count);
  350. };
  351. struct proc_fs_ops {
  352. int (*open) (struct shim_handle * hdl, const char * name, int flags);
  353. int (*mode) (const char * name, mode_t * mode);
  354. int (*stat) (const char * name, struct stat * buf);
  355. int (*follow_link) (const char * name, struct shim_qstr * link);
  356. };
  357. struct proc_dir;
  358. struct proc_ent {
  359. const char * name; /* a proc_callback should at least
  360. have a name or nm_ops.
  361. Otherwise, it is a NULL-end. */
  362. const struct proc_nm_ops * nm_ops;
  363. const struct proc_fs_ops * fs_ops;
  364. const struct proc_dir * dir;
  365. };
  366. struct proc_dir {
  367. int size;
  368. const struct proc_ent ent[];
  369. };
  370. /* string-type file system */
  371. int str_add_dir (const char * path, mode_t mode, struct shim_dentry ** dent);
  372. int str_add_file (const char * path, mode_t mode, struct shim_dentry ** dent);
  373. int str_open (struct shim_handle * hdl, struct shim_dentry * dent, int flags);
  374. int str_dput (struct shim_dentry * dent);
  375. int str_close (struct shim_handle * hdl);
  376. int str_read (struct shim_handle * hdl, void * buf, size_t count);
  377. int str_write (struct shim_handle * hdl, const void * buf, size_t count);
  378. int str_seek (struct shim_handle * hdl, off_t offset, int whence);
  379. int str_flush (struct shim_handle * hdl);
  380. #endif /* _SHIM_FS_H_ */