1 /* Barret Rhoden <brho@cs.berkeley.edu>
3 * VFS, based on the Linux VFS as described in LKD 2nd Ed (Robert Love) and in
4 * UTLK (Bovet/Cesati) , which was probably written by Linus. A lot of it was
5 * changed (reduced) to handle what ROS will need, at least initially.
6 * Hopefully it'll be similar enough to interface with ext2 and other Linux
9 * struct qstr came directly from Linux.
10 * Lawyers can sort out the copyrights and whatnot with these interfaces and
13 #ifndef ROS_KERN_VFS_H
14 #define ROS_KERN_VFS_H
16 #include <ros/common.h>
17 #include <ros/limits.h>
18 #include <sys/queue.h>
24 #include <hashtable.h>
29 /* ghetto preprocessor hacks (since proc includes vfs) */
33 // TODO: temp typedefs, etc. remove when we support this stuff.
37 struct io_writeback {int x;};
38 struct event_poll {int x;};
39 struct poll_table_struct {int x;};
40 // end temp typedefs. note ino and off_t are needed in the next include
44 /* Create flags are those used only during creation, and not saved for later
45 * lookup or use. Everything other than them is viewable via getfl */
46 #define O_CREAT_FLAGS (O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC)
47 /* These flags are those you can attempt to set via setfl for the VFS. */
48 #define O_FCNTL_SET_FLAGS (O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | \
52 struct super_operations;
54 struct dentry_operations;
56 struct inode_operations;
58 struct file_operations;
61 struct pipe_inode_info;
63 /* List def's we need */
64 TAILQ_HEAD(sb_tailq, super_block);
65 TAILQ_HEAD(dentry_tailq, dentry);
66 SLIST_HEAD(dentry_slist, dentry);
67 TAILQ_HEAD(inode_tailq, inode);
68 SLIST_HEAD(inode_slist, inode);
69 TAILQ_HEAD(file_tailq, file);
70 TAILQ_HEAD(io_wb_tailq, io_writeback);
71 TAILQ_HEAD(event_poll_tailq, event_poll);
72 TAILQ_HEAD(vfsmount_tailq, vfsmount);
73 TAILQ_HEAD(fs_type_tailq, fs_type);
75 /* Linux's quickstring - saves recomputing the hash and length. Note the length
76 * is the non-null-terminated length, as you'd get from strlen(). (for now) */
83 /* Helpful structure to pass around during lookup operations. At each point,
84 * it tracks the the answer, the name of the previous, how deep the symlink
85 * following has gone, and the symlink pathnames. *dentry and *mnt up the
86 * refcnt of those objects too, so whoever 'receives; this will need to decref.
87 * This is meant to be pinning only the 'answer' to a path_lookup, and not the
88 * intermediate steps. The intermediates get pinned due to the existence of
89 * their children in memory. Internally, the VFS will refcnt any item whenever
90 * it is in this struct. The last_sym is needed to pin the dentry (and thus the
91 * inode and char* storage for the symname) for the duration of a lookup. When
92 * you resolve a pathname, you need to keep its string in memory. */
93 #define MAX_SYMLINK_DEPTH 6 // arbitrary.
95 struct dentry *dentry; /* dentry of the obj */
96 struct vfsmount *mnt; /* its mount pt */
97 struct qstr last; /* last component in search */
98 int flags; /* lookup flags */
99 int last_type; /* type of last component */
100 unsigned int depth; /* search's symlink depth */
101 int intent; /* access type for the file */
102 struct dentry *last_sym; /* pins the symname */
105 /* nameidata lookup flags and access type fields */
106 #define LOOKUP_FOLLOW 0x01 /* if the last is a symlink, follow */
107 #define LOOKUP_DIRECTORY 0x02 /* last component must be a directory */
108 #define LOOKUP_CONTINUE 0x04 /* still filenames to go */
109 #define LOOKUP_PARENT 0x08 /* lookup the dir that includes the item */
110 /* These are the nd's intent */
111 #define LOOKUP_OPEN 0x10 /* intent is to open a file */
112 #define LOOKUP_CREATE 0x11 /* create a file if it doesn't exist */
113 #define LOOKUP_ACCESS 0x12 /* access / check user permissions */
115 /* Superblock: Specific instance of a mounted filesystem. All synchronization
116 * is done with the one spinlock. */
119 TAILQ_ENTRY(super_block) s_list; /* list of all sbs */
120 dev_t s_dev; /* id */
121 unsigned long s_blocksize;
123 unsigned long long s_maxbytes; /* max file size */
124 struct fs_type *s_type;
125 struct super_operations *s_op;
126 unsigned long s_flags;
127 unsigned long s_magic;
128 struct vfsmount *s_mount; /* vfsmount point */
129 spinlock_t s_lock; /* used for all sync */
131 bool s_syncing; /* currently syncing metadata */
132 struct inode_tailq s_inodes; /* all inodes */
133 struct inode_tailq s_dirty_i; /* dirty inodes */
134 struct io_wb_tailq s_io_wb; /* writebacks */
135 struct file_tailq s_files; /* assigned files */
136 struct dentry_tailq s_lru_d; /* unused dentries (in dcache)*/
137 spinlock_t s_lru_lock;
138 struct hashtable *s_dcache; /* dentry cache */
139 spinlock_t s_dcache_lock;
140 struct hashtable *s_icache; /* inode cache */
141 spinlock_t s_icache_lock;
142 struct block_device *s_bdev;
143 TAILQ_ENTRY(super_block) s_instances; /* list of sbs of this fs type*/
148 struct super_operations {
149 struct inode *(*alloc_inode) (struct super_block *sb);
150 void (*dealloc_inode) (struct inode *);
151 void (*read_inode) (struct inode *);
152 void (*dirty_inode) (struct inode *);
153 void (*write_inode) (struct inode *, bool);
154 void (*put_inode) (struct inode *); /* when decreffed */
155 void (*drop_inode) (struct inode *); /* when about to destroy */
156 void (*delete_inode) (struct inode *); /* deleted from disk */
157 void (*put_super) (struct super_block *); /* releases sb */
158 void (*write_super) (struct super_block *); /* sync with sb on disk */
159 int (*sync_fs) (struct super_block *, bool);
160 int (*remount_fs) (struct super_block *, int, char *);
161 void (*umount_begin) (struct super_block *);/* called by NFS */
164 /* Sets the type of file, IAW the bits in ros/fs.h */
165 #define SET_FTYPE(mode, type) ((mode) = ((mode) & ~__S_IFMT) | (type))
167 /* Will need a bunch of states/flags for an inode. TBD */
168 #define I_STATE_DIRTY 0x001
170 /* Inode: represents a specific file */
172 SLIST_ENTRY(inode) i_hash; /* inclusion in a hash table */
173 TAILQ_ENTRY(inode) i_sb_list; /* all inodes in the FS */
174 TAILQ_ENTRY(inode) i_list; /* describes state (dirty) */
175 struct dentry_tailq i_dentry; /* all dentries pointing here*/
178 int i_mode; /* access mode and file type */
179 unsigned int i_nlink; /* hard links */
182 kdev_t i_rdev; /* real device node */
184 unsigned long i_blksize;
185 unsigned long i_blocks; /* filesize in blocks */
186 struct timespec i_atime;
187 struct timespec i_mtime;
188 struct timespec i_ctime;
190 struct inode_operations *i_op;
191 struct file_operations *i_fop;
192 struct super_block *i_sb;
193 struct page_map *i_mapping; /* usually points to i_pm */
194 struct page_map i_pm; /* this inode's page cache */
196 struct pipe_inode_info *i_pipe;
197 struct block_device *i_bdev;
198 struct char_device *i_cdev;
200 unsigned long i_state;
201 unsigned long dirtied_when; /* in jiffies */
202 unsigned int i_flags; /* filesystem mount flags */
204 atomic_t i_writecount; /* number of writers */
208 struct inode_operations {
209 int (*create) (struct inode *, struct dentry *, int, struct nameidata *);
210 struct dentry *(*lookup) (struct inode *, struct dentry *,
212 int (*link) (struct dentry *, struct inode *, struct dentry *);
213 int (*unlink) (struct inode *, struct dentry *);
214 int (*symlink) (struct inode *, struct dentry *, const char *);
215 int (*mkdir) (struct inode *, struct dentry *, int);
216 int (*rmdir) (struct inode *, struct dentry *);
217 int (*mknod) (struct inode *, struct dentry *, int, dev_t);
218 int (*rename) (struct inode *, struct dentry *,
219 struct inode *, struct dentry *);
220 char *(*readlink) (struct dentry *);
221 void (*truncate) (struct inode *); /* set i_size before calling */
222 int (*permission) (struct inode *, int, struct nameidata *);
225 #define DNAME_INLINE_LEN 32
227 /* Dentry flags. All negatives are also unused. */
228 #define DENTRY_USED 0x01 /* has a kref > 0 */
229 #define DENTRY_NEGATIVE 0x02 /* cache of a failed lookup */
230 #define DENTRY_DYING 0x04 /* should be freed on release */
232 /* Dentry: in memory object, corresponding to an element of a path. E.g. /,
233 * usr, bin, and vim are all dentries. All have inodes. Vim happens to be a
234 * file instead of a directory.
235 * They can be used (valid inode, currently in use), unused (valid, not used),
236 * or negative (not a valid inode (deleted or bad path), but kept to resolve
237 * requests quickly. If none of these, dealloc it back to the slab cache.
238 * Unused and negatives go in the LRU list. */
240 struct kref d_kref; /* don't discard when 0 */
241 unsigned long d_flags; /* dentry cache flags */
243 struct inode *d_inode;
244 TAILQ_ENTRY(dentry) d_lru; /* unused list */
245 TAILQ_ENTRY(dentry) d_alias; /* linkage for i_dentry */
246 struct dentry_tailq d_subdirs;
247 TAILQ_ENTRY(dentry) d_subdirs_link;
248 unsigned long d_time; /* revalidate time (jiffies)*/
249 struct dentry_operations *d_op;
250 struct super_block *d_sb;
251 bool d_mount_point; /* is an FS mounted over here */
252 struct vfsmount *d_mounted_fs; /* fs mounted here */
253 struct dentry *d_parent;
254 struct qstr d_name; /* pts to iname and holds hash*/
255 char d_iname[DNAME_INLINE_LEN];
259 /* not sure yet if we want to call delete when refcnt == 0 (move it to LRU) or
260 * when its time to remove it from the dcache. */
261 struct dentry_operations {
262 int (*d_revalidate) (struct dentry *, struct nameidata *);
263 int (*d_hash) (struct dentry *, struct qstr *);
264 int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
265 int (*d_delete) (struct dentry *);
266 int (*d_release) (struct dentry *);
267 void (*d_iput) (struct dentry *, struct inode *);
270 /* Yanked from glibc-2.11.1/posix/unistd.h */
271 #define SEEK_SET 0 /* Seek from beginning of file. */
272 #define SEEK_CUR 1 /* Seek from current position. */
273 #define SEEK_END 2 /* Seek from end of file. */
275 /* File: represents a file opened by a process. */
277 TAILQ_ENTRY(file) f_list; /* list of all files */
278 struct dentry *f_dentry; /* definitely not inode. =( */
279 struct vfsmount *f_vfsmnt;
280 struct file_operations *f_op;
282 unsigned int f_flags; /* O_APPEND, etc */
283 int f_mode; /* O_RDONLY, etc */
284 off64_t f_pos; /* offset / file pointer */
288 struct event_poll_tailq f_ep_links;
289 spinlock_t f_ep_lock;
290 void *f_privdata; /* tty/socket driver hook */
291 struct page_map *f_mapping; /* page cache mapping */
293 /* Ghetto appserver support */
294 int fd; // all it contains is an appserver fd (for pid 0, aka kernel)
299 struct file_operations {
300 int (*llseek) (struct file *, off64_t, off64_t *, int);
301 ssize_t (*read) (struct file *, char *, size_t, off64_t *);
302 ssize_t (*write) (struct file *, const char *, size_t, off64_t *);
303 int (*readdir) (struct file *, struct dirent *);
304 int (*mmap) (struct file *, struct vm_region *);
305 int (*open) (struct inode *, struct file *);
306 int (*flush) (struct file *);
307 int (*release) (struct inode *, struct file *);
308 int (*fsync) (struct file *, struct dentry *, int);
309 unsigned int (*poll) (struct file *, struct poll_table_struct *);
310 ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
312 ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
314 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, off64_t,
316 int (*check_flags) (int flags); /* most FS's ignore this */
319 /* FS structs. One of these per FS (e.g., ext2) */
323 struct super_block *(*get_sb) (struct fs_type *, int,
324 char *, struct vfsmount *);
325 void (*kill_sb) (struct super_block *);
326 TAILQ_ENTRY(fs_type) list;
327 struct sb_tailq fs_supers; /* all of this FS's sbs */
330 /* A mount point: more focused on the mounting, and solely in memory, compared
331 * to the SB which is focused on FS definitions (and exists on disc). */
333 TAILQ_ENTRY(vfsmount) mnt_list;
334 struct vfsmount *mnt_parent;
335 struct dentry *mnt_mountpoint;/* parent dentry where mnted */
336 struct dentry *mnt_root; /* dentry of root of this fs */
337 struct super_block *mnt_sb;
338 struct vfsmount_tailq mnt_child_mounts;
339 TAILQ_ENTRY(vfsmount) mnt_child_link;
340 struct kref mnt_kref;
343 struct namespace *mnt_namespace;
346 struct pipe_inode_info
351 unsigned int p_nr_readers;
352 unsigned int p_nr_writers;
353 struct cond_var p_cv;
356 /* Per-process structs */
357 #define NR_OPEN_FILES_DEFAULT 32
358 #define NR_FILE_DESC_DEFAULT 32
360 /* Bitmask for file descriptors, big for when we exceed the initial small. We
361 * could just use the fd_array to check for openness instead of the bitmask,
362 * but eventually we might want to use the bitmasks for other things (like
363 * which files are close_on_exec. */
365 typedef struct fd_set {
366 uint8_t fds_bits[BYTES_FOR_BITMASK(NR_FILE_DESC_MAX)];
370 struct small_fd_set {
371 uint8_t fds_bits[BYTES_FOR_BITMASK(NR_FILE_DESC_DEFAULT)];
374 /* Helper macros to manage fd_sets */
375 #define FD_SET(n, p) ((p)->fds_bits[(n)/8] |= (1 << ((n) & 7)))
376 #define FD_CLR(n, p) ((p)->fds_bits[(n)/8] &= ~(1 << ((n) & 7)))
377 #define FD_ISSET(n,p) ((p)->fds_bits[(n)/8] & (1 << ((n) & 7)))
378 #define FD_ZERO(p) memset((void*)(p),0,sizeof(*(p)))
380 /* Describes an open file. We need this, since the FD flags are supposed to be
381 * per file descriptor, not per file (like the file status flags). */
382 struct chan; /* from 9ns */
384 struct file *fd_file;
385 struct chan *fd_chan;
386 unsigned int fd_flags;
387 struct fd_tap *fd_tap;
390 /* All open files for a process */
394 int max_files; /* max files ptd to by fd */
395 int max_fdset; /* max of the current fd_set */
396 int hint_min_fd; /* <= min available fd */
397 struct file_desc *fd; /* initially pts to fd_array */
398 struct fd_set *open_fds; /* init, pts to open_fds_init */
399 struct small_fd_set open_fds_init;
400 struct file_desc fd_array[NR_OPEN_FILES_DEFAULT];
403 /* Process specific filesystem info */
411 /* Each process can have its own (eventually), but default to the same NS */
415 struct vfsmount *root;
416 struct vfsmount_tailq vfsmounts; /* all vfsmounts in this ns */
420 extern struct sb_tailq super_blocks; /* list of all sbs */
421 extern spinlock_t super_blocks_lock;
422 extern struct fs_type_tailq file_systems; /* lock this if it's dynamic */
423 extern struct namespace default_ns;
425 /* Slab caches for common objects */
426 extern struct kmem_cache *dentry_kcache;
427 extern struct kmem_cache *inode_kcache;
428 extern struct kmem_cache *file_kcache;
430 /* Misc VFS functions */
432 void qstr_builder(struct dentry *dentry, char *l_name);
433 char *file_name(struct file *file);
434 int path_lookup(char *path, int flags, struct nameidata *nd);
435 void path_release(struct nameidata *nd);
436 int mount_fs(struct fs_type *fs, char *dev_name, char *path, int flags);
438 /* Superblock functions */
439 struct super_block *get_sb(void);
440 void init_sb(struct super_block *sb, struct vfsmount *vmnt,
441 struct dentry_operations *d_op, unsigned long root_ino,
444 /* Dentry Functions */
445 struct dentry *get_dentry_with_ops(struct super_block *sb,
446 struct dentry *parent, char *name,
447 struct dentry_operations *d_op);
448 struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
450 void dentry_release(struct kref *kref);
451 void __dentry_free(struct dentry *dentry);
452 struct dentry *lookup_dentry(char *path, int flags);
453 struct dentry *dcache_get(struct super_block *sb, struct dentry *what_i_want);
454 void dcache_put(struct super_block *sb, struct dentry *key_val);
455 struct dentry *dcache_remove(struct super_block *sb, struct dentry *key);
456 void dcache_prune(struct super_block *sb, bool negative_only);
457 int generic_dentry_hash(struct dentry *dentry, struct qstr *qstr);
459 /* Inode Functions */
460 struct inode *get_inode(struct dentry *dentry);
461 void load_inode(struct dentry *dentry, unsigned long ino);
462 int create_file(struct inode *dir, struct dentry *dentry, int mode);
463 int create_dir(struct inode *dir, struct dentry *dentry, int mode);
464 int create_symlink(struct inode *dir, struct dentry *dentry,
465 const char *symname, int mode);
466 int check_perms(struct inode *inode, int access_mode);
467 void inode_release(struct kref *kref);
468 void stat_inode(struct inode *inode, struct kstat *kstat);
469 struct inode *icache_get(struct super_block *sb, unsigned long ino);
470 void icache_put(struct super_block *sb, struct inode *inode);
471 struct inode *icache_remove(struct super_block *sb, unsigned long ino);
473 /* File-ish functions */
474 ssize_t generic_file_read(struct file *file, char *buf, size_t count,
476 ssize_t generic_file_write(struct file *file, const char *buf, size_t count,
478 ssize_t generic_dir_read(struct file *file, char *u_buf, size_t count,
480 struct file *alloc_file(void);
481 struct file *do_file_open(char *path, int flags, int mode);
482 int do_symlink(char *path, const char *symname, int mode);
483 int do_link(char *old_path, char *new_path);
484 int do_unlink(char *path);
485 int do_access(char *path, int mode);
486 int do_file_chmod(struct file *file, int mode);
487 int do_mkdir(char *path, int mode);
488 int do_rmdir(char *path);
489 int do_pipe(struct file **pipe_files, int flags);
490 int do_rename(char *old_path, char *new_path);
491 int do_truncate(struct inode *inode, off64_t len);
492 struct file *dentry_open(struct dentry *dentry, int flags);
493 void file_release(struct kref *kref);
494 ssize_t kread_file(struct file *file, void *buf, size_t sz);
495 void *kread_whole_file(struct file *file);
497 /* Process-related File management functions */
498 void *lookup_fd(struct fd_table *fdt, int fd, bool incref, bool vfs);
499 int insert_obj_fdt(struct fd_table *fdt, void *obj, int low_fd, int fd_flags,
500 bool must_use_low, bool vfs);
501 bool close_fd(struct fd_table *fdt, int fd);
502 void close_fdt(struct fd_table *open_files, bool cloexec);
503 void clone_fdt(struct fd_table *src, struct fd_table *dst);
505 struct file *get_file_from_fd(struct fd_table *open_files, int fd);
506 void put_file_from_fd(struct fd_table *open_files, int file_desc);
507 int insert_file(struct fd_table *open_files, struct file *file, int low_fd,
508 bool must, bool cloexec);
509 int do_chdir(struct fs_struct *fs_env, char *path);
510 int do_fchdir(struct fs_struct *fs_env, struct file *file);
511 char *do_getcwd(struct fs_struct *fs_env, char **kfree_this, size_t cwd_l);
514 void print_kstat(struct kstat *kstat);
515 int ls_dash_r(char *path);
516 extern struct inode_operations dummy_i_op;
517 extern struct dentry_operations dummy_d_op;
519 #endif /* ROS_KERN_VFS_H */