123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- #ifndef _SHIM_FS_H_
- #define _SHIM_FS_H_
- #include <stdbool.h>
- #include <shim_types.h>
- #include <shim_defs.h>
- #include <shim_handle.h>
- #include <shim_utils.h>
- #include <pal.h>
- #include <list.h>
- struct shim_handle;
- #define FS_POLL_RD 0x01
- #define FS_POLL_WR 0x02
- #define FS_POLL_ER 0x04
- #define FS_POLL_SZ 0x08
- struct shim_fs_ops {
-
- int (*mount) (const char * uri, void ** mount_data);
- int (*unmount) (void * mount_data);
-
- int (*close) (struct shim_handle * hdl);
-
- ssize_t (*read) (struct shim_handle * hdl, void * buf, size_t count);
-
- ssize_t (*write) (struct shim_handle * hdl, const void * buf, size_t count);
-
- int (*mmap) (struct shim_handle * hdl, void ** addr, size_t size,
- int prot, int flags, off_t offset);
-
- int (*flush) (struct shim_handle * hdl);
-
- off_t (*seek) (struct shim_handle * hdl, off_t offset, int wence);
-
- int (*move) (const char * trim_old_name, const char * trim_new_name);
- int (*copy) (const char * trim_old_name, const char * trim_new_name);
-
- int (*truncate) (struct shim_handle * hdl, off_t len);
-
- int (*hstat) (struct shim_handle * hdl, struct stat * buf);
-
- int (*setflags) (struct shim_handle * hdl, int flags);
-
- void (*hput) (struct shim_handle * hdl);
-
- int (*lock) (const char * trim_name);
- int (*unlock) (const char * trim_name);
-
- int (*lockfs) (void);
- int (*unlockfs) (void);
-
- int (*checkout) (struct shim_handle * hdl);
- int (*checkin) (struct shim_handle * hdl);
-
-
-
- off_t (*poll) (struct shim_handle * hdl, int poll_type);
-
- ssize_t (*checkpoint) (void ** checkpoint, void * mount_data);
- int (*migrate) (void * checkpoint, void ** mount_data);
- };
- #define DENTRY_VALID 0x0001
- #define DENTRY_NEGATIVE 0x0002
- #define DENTRY_RECENTLY 0x0004
- #define DENTRY_PERSIST 0x0008
- #define DENTRY_HASHED 0x0010
- #define DENTRY_MOUNTPOINT 0x0040
- #define DENTRY_ISLINK 0x0080
- #define DENTRY_ISDIRECTORY 0x0100
- #define DENTRY_LOCKED 0x0200
- #define DENTRY_LISTED 0x1000
- #define DENTRY_INO_UPDATED 0x2000
- #define DENTRY_ANCESTOR 0x4000
- #define DENTRY_INVALID_FLAGS (~0x7FFF)
- #define DCACHE_HASH_SIZE 1024
- #define DCACHE_HASH(hash) ((hash) & (DCACHE_HASH_SIZE - 1))
- DEFINE_LIST(shim_dentry);
- DEFINE_LISTP(shim_dentry);
- struct shim_dentry {
- int state;
- struct shim_mount * fs;
- struct shim_qstr rel_path;
- struct shim_qstr name;
-
- LIST_TYPE(shim_dentry) hlist;
- LIST_TYPE(shim_dentry) list;
- struct shim_dentry * parent;
- int nchildren;
- LISTP_TYPE(shim_dentry) children;
- LIST_TYPE(shim_dentry) siblings;
- struct shim_mount * mounted;
- void * data;
- unsigned long ino;
- mode_t type;
- mode_t mode;
- struct shim_lock lock;
- REFTYPE ref_count;
- };
- struct shim_d_ops {
-
- int (*open) (struct shim_handle * hdl, struct shim_dentry * dent,
- int flags);
-
- int (*lookup) (struct shim_dentry * dent);
-
- int (*mode) (struct shim_dentry * dent, mode_t * mode);
-
- int (*dput) (struct shim_dentry * dent);
-
- int (*creat) (struct shim_handle * hdl, struct shim_dentry * dir,
- struct shim_dentry * dent, int flags, mode_t mode);
-
- int (*unlink) (struct shim_dentry * dir, struct shim_dentry * dent);
-
- int (*mkdir) (struct shim_dentry * dir, struct shim_dentry * dent,
- mode_t mode);
-
- int (*stat) (struct shim_dentry * dent, struct stat * buf);
-
- int (*follow_link) (struct shim_dentry * dent, struct shim_qstr * link);
-
- int (*set_link) (struct shim_dentry * dent, const char * link);
-
- int (*chmod) (struct shim_dentry * dent, mode_t mode);
- int (*chown) (struct shim_dentry * dent, int uid, int gid);
-
- int (*rename) (struct shim_dentry * old, struct shim_dentry * new);
-
- int (*readdir) (struct shim_dentry * dent, struct shim_dirent ** dirent);
- };
- #define MAX_PATH 4096
- #define MAX_FILENAME 255
- DEFINE_LIST(shim_mount);
- struct shim_mount {
- char type[8];
- struct shim_dentry * mount_point;
- struct shim_qstr path;
- struct shim_qstr uri;
- struct shim_fs_ops * fs_ops;
- struct shim_d_ops * d_ops;
- struct shim_dentry * root;
- void * data;
- void * cpdata;
- size_t cpsize;
- REFTYPE ref_count;
- LIST_TYPE(shim_mount) hlist;
- LIST_TYPE(shim_mount) list;
- };
- extern struct shim_dentry * dentry_root;
- #define LOOKUP_FOLLOW 001
- #define LOOKUP_DIRECTORY 002
- #define LOOKUP_CONTINUE 004
- #define LOOKUP_PARENT 010
- #define F_OK 0
- #define MAY_EXEC 001
- #define MAY_WRITE 002
- #define MAY_READ 004
- #if 0
- #define MAY_APPEND 010
- #endif
- #define NO_MODE ((mode_t) -1)
- #define ACC_MODE(x) ((((x) == O_RDONLY || (x) == O_RDWR) ? MAY_READ : 0) | \
- (((x) == O_WRONLY || (x) == O_RDWR) ? MAY_WRITE : 0))
- #define LOOKUP_OPEN 0100
- #define LOOKUP_CREATE 0200
- #define LOOKUP_ACCESS 0400
- #define LOOKUP_SYNC (LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_ACCESS)
- enum lookup_type {
- LAST_NORM,
- LAST_ROOT,
- LAST_DOT,
- LAST_DOTDOT,
- LAST_BIND
- };
- struct lookup {
- struct shim_dentry * dentry;
- struct shim_mount * mount;
- const char * last;
- int depth;
- int flags;
- enum lookup_type last_type;
- };
- int init_fs (void);
- int init_mount_root (void);
- int init_mount (void);
- const char * get_file_name (const char * path, size_t len);
- int mount_fs (const char * mount_type, const char * mount_uri,
- const char * mount_point, struct shim_dentry *parent,
- struct shim_dentry **dentp, bool make_ancestor);
- int unmount_fs (const char * mount_point);
- int search_builtin_fs (const char * type, struct shim_mount ** fs);
- void get_mount (struct shim_mount * mount);
- void put_mount (struct shim_mount * mount);
- struct shim_mount * find_mount_from_uri (const char * uri);
- #include <shim_utils.h>
- static inline void set_handle_fs (struct shim_handle * hdl,
- struct shim_mount * fs)
- {
- get_mount(fs);
- hdl->fs = fs;
- memcpy(hdl->fs_type, fs->type, sizeof(hdl->fs_type));
- }
- int walk_mounts (int (*walk) (struct shim_mount * mount, void * arg),
- void * arg);
- int init_dcache (void);
- extern struct shim_lock dcache_lock;
- int permission (struct shim_dentry * dent, mode_t mask);
- int lookup_dentry (struct shim_dentry * parent, const char * name, int namelen,
- struct shim_dentry ** new, struct shim_mount * fs);
- int __path_lookupat (struct shim_dentry * start, const char * path, int flags,
- struct shim_dentry ** dent, int link_depth,
- struct shim_mount *fs, bool make_ancestor);
- int path_lookupat (struct shim_dentry * start, const char * name, int flags,
- struct shim_dentry ** dent, struct shim_mount *fs);
- int get_dirfd_dentry(int dirfd, struct shim_dentry** dir);
- int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
- const char * path, int flags, int mode,
- struct shim_dentry ** dent);
- int dentry_open (struct shim_handle * hdl, struct shim_dentry * dent,
- int flags);
- int list_directory_dentry (struct shim_dentry *dir);
- int list_directory_handle (struct shim_dentry *dent, struct shim_handle *hdl );
- void get_dentry (struct shim_dentry * dent);
- void put_dentry (struct shim_dentry * dent);
- static_always_inline
- void fast_pathcpy (char * dst, const char * src, size_t size, char ** ptr)
- {
- char * d = dst;
- const char * s = src;
- for (size_t i = 0 ; i < size ; i++, s++, d++)
- *d = *s;
- *ptr = d;
- }
- static_always_inline
- char * dentry_get_path (struct shim_dentry * dent, bool on_stack,
- size_t * sizeptr)
- {
- struct shim_mount * fs = dent->fs;
- char * buffer, * c;
- size_t bufsize = dent->rel_path.len + 1;
- if (fs)
- bufsize += fs->path.len + 1;
- if (on_stack) {
- c = buffer = __alloca(bufsize);
- } else {
- if (!(c = buffer = malloc(bufsize)))
- return NULL;
- }
- if (fs && !qstrempty(&fs->path))
- fast_pathcpy(c, qstrgetstr(&fs->path), fs->path.len, &c);
- if (dent->rel_path.len) {
- const char * path = qstrgetstr(&dent->rel_path);
- int len = dent->rel_path.len;
- if (c > buffer && *(c - 1) == '/') {
- if (*path == '/')
- path++;
- } else {
- if (*path != '/')
- *(c++) = '/';
- }
- fast_pathcpy(c, path, len, &c);
- }
- if (sizeptr)
- *sizeptr = c - buffer;
- *c = 0;
- return buffer;
- }
- static_always_inline
- const char * dentry_get_name (struct shim_dentry * dent)
- {
- return qstrgetstr(&dent->name);
- }
- struct shim_dentry * get_new_dentry (struct shim_mount *mount,
- struct shim_dentry * parent,
- const char * name, int namelen,
- HASHTYPE * hashptr);
- struct shim_dentry *
- __lookup_dcache (struct shim_dentry * start, const char * name, int namelen,
- HASHTYPE * hashptr);
- int __del_dentry_tree(struct shim_dentry * root);
- bool dentry_is_ancestor(struct shim_dentry* anc, struct shim_dentry* dent);
- #define MOUNT_HASH_BYTE 1
- #define MOUNT_HASH_WIDTH 8
- #define MOUNT_HASH_SIZE 256
- #define MOUNT_HASH(hash) ((hash) & (MOUNT_HASH_SIZE - 1))
- HASHTYPE hash_path(const char* path, size_t size);
- HASHTYPE rehash_name(HASHTYPE parent_hbuf, const char* name, size_t size);
- HASHTYPE rehash_path(HASHTYPE ancester_hbuf, const char* path, size_t size);
- extern struct shim_fs_ops chroot_fs_ops;
- extern struct shim_d_ops chroot_d_ops;
- extern struct shim_fs_ops str_fs_ops;
- extern struct shim_d_ops str_d_ops;
- extern struct shim_fs_ops dev_fs_ops;
- extern struct shim_d_ops dev_d_ops;
- extern struct shim_fs_ops config_fs_ops;
- extern struct shim_d_ops config_d_ops;
- extern struct shim_fs_ops proc_fs_ops;
- extern struct shim_d_ops proc_d_ops;
- extern struct shim_mount chroot_builtin_fs;
- extern struct shim_mount pipe_builtin_fs;
- extern struct shim_mount socket_builtin_fs;
- extern struct shim_mount epoll_builtin_fs;
- struct proc_nm_ops {
- int (*match_name) (const char * name);
- int (*list_name) (const char * name, struct shim_dirent ** buf,
- int count);
- };
- struct proc_fs_ops {
- int (*open) (struct shim_handle * hdl, const char * name, int flags);
- int (*mode) (const char * name, mode_t * mode);
- int (*stat) (const char * name, struct stat * buf);
- int (*follow_link) (const char * name, struct shim_qstr * link);
- };
- struct proc_dir;
- struct proc_ent {
- const char * name;
- const struct proc_nm_ops * nm_ops;
- const struct proc_fs_ops * fs_ops;
- const struct proc_dir * dir;
- };
- struct proc_dir {
- int size;
- const struct proc_ent ent[];
- };
- int str_add_dir (const char * path, mode_t mode, struct shim_dentry ** dent);
- int str_add_file (const char * path, mode_t mode, struct shim_dentry ** dent);
- int str_open (struct shim_handle * hdl, struct shim_dentry * dent, int flags);
- int str_dput (struct shim_dentry * dent);
- int str_close (struct shim_handle * hdl);
- ssize_t str_read (struct shim_handle * hdl, void * buf, size_t count);
- ssize_t str_write (struct shim_handle * hdl, const void * buf, size_t count);
- off_t str_seek (struct shim_handle * hdl, off_t offset, int whence);
- int str_flush (struct shim_handle * hdl);
- #endif
|