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 <sys/queue.h>
22 #include <hashtable.h>
26 /* ghetto preprocessor hacks (since proc includes vfs) */
30 // TODO: temp typedefs, etc. remove when we support this stuff.
34 typedef long off_t; // out there in other .h's, but not in the kernel yet
35 struct io_writeback {int x;};
36 struct event_poll {int x;};
37 struct poll_table_struct {int x;};
38 // end temp typedefs. note ino and off_t are needed in the next include
43 struct super_operations;
45 struct dentry_operations;
47 struct inode_operations;
49 struct file_operations;
58 /* List def's we need */
59 TAILQ_HEAD(sb_tailq, super_block);
60 TAILQ_HEAD(dentry_tailq, dentry);
61 SLIST_HEAD(dentry_slist, dentry);
62 TAILQ_HEAD(inode_tailq, inode);
63 SLIST_HEAD(inode_slist, inode);
64 TAILQ_HEAD(file_tailq, file);
65 TAILQ_HEAD(io_wb_tailq, io_writeback);
66 TAILQ_HEAD(event_poll_tailq, event_poll);
67 TAILQ_HEAD(vfsmount_tailq, vfsmount);
68 TAILQ_HEAD(fs_type_tailq, fs_type);
70 /* Linux's quickstring - saves recomputing the hash and length. Note the length
71 * is the non-null-terminated length, as you'd get from strlen(). (for now) */
78 /* Helpful structure to pass around during lookup operations. At each point,
79 * it tracks the the answer, the name of the previous, how deep the symlink
80 * following has gone, and the symlink pathnames. *dentry and *mnt up the
81 * refcnt of those objects too, so whoever 'receives; this will need to decref.
82 * This is meant to be pinning only the 'answer' to a path_lookup, and not the
83 * intermediate steps. The intermediates get pinned due to the existence of
84 * their children in memory. Internally, the VFS will refcnt any item whenever
85 * it is in this struct. The last_sym is needed to pin the dentry (and thus the
86 * inode and char* storage for the symname) for the duration of a lookup. When
87 * you resolve a pathname, you need to keep its string in memory. */
88 #define MAX_SYMLINK_DEPTH 6 // arbitrary.
90 struct dentry *dentry; /* dentry of the obj */
91 struct vfsmount *mnt; /* its mount pt */
92 struct qstr last; /* last component in search */
93 int flags; /* lookup flags */
94 int last_type; /* type of last component */
95 unsigned int depth; /* search's symlink depth */
96 int intent; /* access type for the file */
97 struct dentry *last_sym; /* pins the symname */
100 /* nameidata lookup flags and access type fields */
101 #define LOOKUP_FOLLOW 0x01 /* if the last is a symlink, follow */
102 #define LOOKUP_DIRECTORY 0x02 /* last component must be a directory */
103 #define LOOKUP_CONTINUE 0x04 /* still filenames to go */
104 #define LOOKUP_PARENT 0x08 /* lookup the dir that includes the item */
105 /* These are the nd's intent */
106 #define LOOKUP_OPEN 0x10 /* intent is to open a file */
107 #define LOOKUP_CREATE 0x11 /* create a file if it doesn't exist */
108 #define LOOKUP_ACCESS 0x12 /* access / check user permissions */
110 /* Superblock: Specific instance of a mounted filesystem. All synchronization
111 * is done with the one spinlock. */
114 TAILQ_ENTRY(super_block) s_list; /* list of all sbs */
115 dev_t s_dev; /* id */
116 unsigned long s_blocksize;
118 unsigned long long s_maxbytes; /* max file size */
119 struct fs_type *s_type;
120 struct super_operations *s_op;
121 unsigned long s_flags;
122 unsigned long s_magic;
123 struct vfsmount *s_mount; /* vfsmount point */
124 spinlock_t s_lock; /* used for all sync */
126 bool s_syncing; /* currently syncing metadata */
127 struct inode_tailq s_inodes; /* all inodes */
128 struct inode_tailq s_dirty_i; /* dirty inodes */
129 struct io_wb_tailq s_io_wb; /* writebacks */
130 struct file_tailq s_files; /* assigned files */
131 struct dentry_tailq s_lru_d; /* unused dentries (in dcache)*/
132 spinlock_t s_lru_lock;
133 struct hashtable *s_dcache; /* dentry cache */
134 spinlock_t s_dcache_lock;
135 struct hashtable *s_icache; /* inode cache */
136 spinlock_t s_icache_lock;
137 struct block_device *s_bdev;
138 TAILQ_ENTRY(super_block) s_instances; /* list of sbs of this fs type*/
143 struct super_operations {
144 struct inode *(*alloc_inode) (struct super_block *sb);
145 void (*dealloc_inode) (struct inode *);
146 void (*read_inode) (struct inode *);
147 void (*dirty_inode) (struct inode *);
148 void (*write_inode) (struct inode *, bool);
149 void (*put_inode) (struct inode *); /* when decreffed */
150 void (*drop_inode) (struct inode *); /* when about to destroy */
151 void (*delete_inode) (struct inode *); /* deleted from disk */
152 void (*put_super) (struct super_block *); /* releases sb */
153 void (*write_super) (struct super_block *); /* sync with sb on disk */
154 int (*sync_fs) (struct super_block *, bool);
155 int (*remount_fs) (struct super_block *, int, char *);
156 void (*umount_begin) (struct super_block *);/* called by NFS */
159 /* Sets the type of file, IAW the bits in ros/fs.h */
160 #define SET_FTYPE(mode, type) ((mode) = ((mode) & ~__S_IFMT) | (type))
162 /* Will need a bunch of states/flags for an inode. TBD */
163 #define I_STATE_DIRTY 0x001
165 /* Inode: represents a specific file */
167 SLIST_ENTRY(inode) i_hash; /* inclusion in a hash table */
168 TAILQ_ENTRY(inode) i_sb_list; /* all inodes in the FS */
169 TAILQ_ENTRY(inode) i_list; /* describes state (dirty) */
170 struct dentry_tailq i_dentry; /* all dentries pointing here*/
173 int i_mode; /* access mode and file type */
174 unsigned int i_nlink; /* hard links */
177 kdev_t i_rdev; /* real device node */
179 unsigned long i_blksize;
180 unsigned long i_blocks; /* filesize in blocks */
181 struct timespec i_atime;
182 struct timespec i_mtime;
183 struct timespec i_ctime;
185 struct inode_operations *i_op;
186 struct file_operations *i_fop;
187 struct super_block *i_sb;
188 struct page_map *i_mapping; /* usually points to i_pm */
189 struct page_map i_pm; /* this inode's page cache */
191 struct pipe_inode_info *i_pipe;
192 struct block_device *i_bdev;
193 struct char_device *i_cdev;
195 unsigned long i_state;
196 unsigned long dirtied_when; /* in jiffies */
197 unsigned int i_flags; /* filesystem mount flags */
199 atomic_t i_writecount; /* number of writers */
203 struct inode_operations {
204 int (*create) (struct inode *, struct dentry *, int, struct nameidata *);
205 struct dentry *(*lookup) (struct inode *, struct dentry *,
207 int (*link) (struct dentry *, struct inode *, struct dentry *);
208 int (*unlink) (struct inode *, struct dentry *);
209 int (*symlink) (struct inode *, struct dentry *, const char *);
210 int (*mkdir) (struct inode *, struct dentry *, int);
211 int (*rmdir) (struct inode *, struct dentry *);
212 int (*mknod) (struct inode *, struct dentry *, int, dev_t);
213 int (*rename) (struct inode *, struct dentry *,
214 struct inode *, struct dentry *);
215 char *(*readlink) (struct dentry *);
216 void (*truncate) (struct inode *); /* set i_size before calling */
217 int (*permission) (struct inode *, int, struct nameidata *);
220 #define DNAME_INLINE_LEN 32
222 /* Dentry flags. All negatives are also unused. */
223 #define DENTRY_USED 0x01 /* has a kref > 0 */
224 #define DENTRY_NEGATIVE 0x02 /* cache of a failed lookup */
225 #define DENTRY_DYING 0x04 /* should be freed on release */
227 /* Dentry: in memory object, corresponding to an element of a path. E.g. /,
228 * usr, bin, and vim are all dentries. All have inodes. Vim happens to be a
229 * file instead of a directory.
230 * They can be used (valid inode, currently in use), unused (valid, not used),
231 * or negative (not a valid inode (deleted or bad path), but kept to resolve
232 * requests quickly. If none of these, dealloc it back to the slab cache.
233 * Unused and negatives go in the LRU list. */
235 struct kref d_kref; /* don't discard when 0 */
236 unsigned long d_flags; /* dentry cache flags */
238 struct inode *d_inode;
239 TAILQ_ENTRY(dentry) d_lru; /* unused list */
240 TAILQ_ENTRY(dentry) d_alias; /* linkage for i_dentry */
241 struct dentry_tailq d_subdirs;
242 TAILQ_ENTRY(dentry) d_subdirs_link;
243 unsigned long d_time; /* revalidate time (jiffies)*/
244 struct dentry_operations *d_op;
245 struct super_block *d_sb;
246 bool d_mount_point; /* is an FS mounted over here */
247 struct vfsmount *d_mounted_fs; /* fs mounted here */
248 struct dentry *d_parent;
249 struct qstr d_name; /* pts to iname and holds hash*/
250 char d_iname[DNAME_INLINE_LEN];
254 /* not sure yet if we want to call delete when refcnt == 0 (move it to LRU) or
255 * when its time to remove it from the dcache. */
256 struct dentry_operations {
257 int (*d_revalidate) (struct dentry *, struct nameidata *);
258 int (*d_hash) (struct dentry *, struct qstr *);
259 int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
260 int (*d_delete) (struct dentry *);
261 int (*d_release) (struct dentry *);
262 void (*d_iput) (struct dentry *, struct inode *);
265 /* Yanked from glibc-2.11.1/posix/unistd.h */
266 #define SEEK_SET 0 /* Seek from beginning of file. */
267 #define SEEK_CUR 1 /* Seek from current position. */
268 #define SEEK_END 2 /* Seek from end of file. */
270 /* File: represents a file opened by a process. */
272 TAILQ_ENTRY(file) f_list; /* list of all files */
273 struct dentry *f_dentry; /* definitely not inode. =( */
274 struct vfsmount *f_vfsmnt;
275 struct file_operations *f_op;
277 unsigned int f_flags; /* O_APPEND, etc */
278 int f_mode; /* O_RDONLY, etc */
279 off_t f_pos; /* offset / file pointer */
283 struct event_poll_tailq f_ep_links;
284 spinlock_t f_ep_lock;
285 void *f_fs_info; /* tty driver hook */
286 struct page_map *f_mapping; /* page cache mapping */
288 /* Ghetto appserver support */
289 int fd; // all it contains is an appserver fd (for pid 0, aka kernel)
294 struct file_operations {
295 off_t (*llseek) (struct file *, off_t, int);
296 ssize_t (*read) (struct file *, char *, size_t, off_t *);
297 ssize_t (*write) (struct file *, const char *, size_t, off_t *);
298 int (*readdir) (struct file *, struct dirent *);
299 int (*mmap) (struct file *, struct vm_region *);
300 int (*open) (struct inode *, struct file *);
301 int (*flush) (struct file *);
302 int (*release) (struct inode *, struct file *);
303 int (*fsync) (struct file *, struct dentry *, int);
304 unsigned int (*poll) (struct file *, struct poll_table_struct *);
305 ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
307 ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
309 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, off_t, int);
310 int (*check_flags) (int flags); /* most FS's ignore this */
313 /* FS structs. One of these per FS (e.g., ext2) */
317 struct super_block *(*get_sb) (struct fs_type *, int,
318 char *, struct vfsmount *);
319 void (*kill_sb) (struct super_block *);
320 TAILQ_ENTRY(fs_type) list;
321 struct sb_tailq fs_supers; /* all of this FS's sbs */
324 /* A mount point: more focused on the mounting, and solely in memory, compared
325 * to the SB which is focused on FS definitions (and exists on disc). */
327 TAILQ_ENTRY(vfsmount) mnt_list;
328 struct vfsmount *mnt_parent;
329 struct dentry *mnt_mountpoint;/* parent dentry where mnted */
330 struct dentry *mnt_root; /* dentry of root of this fs */
331 struct super_block *mnt_sb;
332 struct vfsmount_tailq mnt_child_mounts;
333 TAILQ_ENTRY(vfsmount) mnt_child_link;
334 struct kref mnt_kref;
337 struct namespace *mnt_namespace;
340 /* Per-process structs */
341 #define NR_OPEN_FILES_DEFAULT 32
342 #define NR_FILE_DESC_DEFAULT 32
343 /* keep this in sync with glibc's fd_setsize */
344 #define NR_FILE_DESC_MAX 1024
346 /* Bitmask for file descriptors, big for when we exceed the initial small. We
347 * could just use the fd_array to check for openness instead of the bitmask,
348 * but eventually we might want to use the bitmasks for other things (like
349 * which files are close_on_exec. */
351 uint8_t fds_bits[BYTES_FOR_BITMASK(NR_FILE_DESC_MAX)];
353 struct small_fd_set {
354 uint8_t fds_bits[BYTES_FOR_BITMASK(NR_FILE_DESC_DEFAULT)];
357 /* Describes an open file. We need this, since the FD flags are supposed to be
358 * per file descriptor, not per file (like the file status flags). */
360 struct file *fd_file;
361 unsigned int fd_flags;
364 /* All open files for a process */
365 struct files_struct {
367 int max_files; /* max files ptd to by fd */
368 int max_fdset; /* max of the current fd_set */
369 int next_fd; /* next number available */
370 struct file_desc *fd; /* initially pts to fd_array */
371 struct fd_set *open_fds; /* init, pts to open_fds_init */
372 struct small_fd_set open_fds_init;
373 struct file_desc fd_array[NR_OPEN_FILES_DEFAULT];
376 /* Process specific filesysten info */
384 /* Each process can have its own (eventually), but default to the same NS */
388 struct vfsmount *root;
389 struct vfsmount_tailq vfsmounts; /* all vfsmounts in this ns */
393 extern struct sb_tailq super_blocks; /* list of all sbs */
394 extern spinlock_t super_blocks_lock;
395 extern struct fs_type_tailq file_systems; /* lock this if it's dynamic */
396 extern struct namespace default_ns;
398 /* Slab caches for common objects */
399 extern struct kmem_cache *dentry_kcache;
400 extern struct kmem_cache *inode_kcache;
401 extern struct kmem_cache *file_kcache;
403 /* Misc VFS functions */
405 void qstr_builder(struct dentry *dentry, char *l_name);
406 char *file_name(struct file *file);
407 int path_lookup(char *path, int flags, struct nameidata *nd);
408 void path_release(struct nameidata *nd);
409 int mount_fs(struct fs_type *fs, char *dev_name, char *path, int flags);
411 /* Superblock functions */
412 struct super_block *get_sb(void);
413 void init_sb(struct super_block *sb, struct vfsmount *vmnt,
414 struct dentry_operations *d_op, unsigned long root_ino,
417 /* Dentry Functions */
418 struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
420 void dentry_release(struct kref *kref);
421 void __dentry_free(struct dentry *dentry);
422 struct dentry *lookup_dentry(char *path, int flags);
423 struct dentry *dcache_get(struct super_block *sb, struct dentry *what_i_want);
424 void dcache_put(struct super_block *sb, struct dentry *key_val);
425 struct dentry *dcache_remove(struct super_block *sb, struct dentry *key);
426 void dcache_prune(struct super_block *sb, bool negative_only);
428 /* Inode Functions */
429 struct inode *get_inode(struct dentry *dentry);
430 void load_inode(struct dentry *dentry, unsigned long ino);
431 int create_file(struct inode *dir, struct dentry *dentry, int mode);
432 int create_dir(struct inode *dir, struct dentry *dentry, int mode);
433 int create_symlink(struct inode *dir, struct dentry *dentry,
434 const char *symname, int mode);
435 int check_perms(struct inode *inode, int access_mode);
436 void inode_release(struct kref *kref);
437 void stat_inode(struct inode *inode, struct kstat *kstat);
438 struct inode *icache_get(struct super_block *sb, unsigned long ino);
439 void icache_put(struct super_block *sb, struct inode *inode);
440 struct inode *icache_remove(struct super_block *sb, unsigned long ino);
442 /* File-ish functions */
443 ssize_t generic_file_read(struct file *file, char *buf, size_t count,
445 ssize_t generic_file_write(struct file *file, const char *buf, size_t count,
447 ssize_t generic_dir_read(struct file *file, char *u_buf, size_t count,
449 struct file *do_file_open(char *path, int flags, int mode);
450 int do_symlink(char *path, const char *symname, int mode);
451 int do_link(char *old_path, char *new_path);
452 int do_unlink(char *path);
453 int do_access(char *path, int mode);
454 int do_chmod(char *path, int mode);
455 int do_mkdir(char *path, int mode);
456 int do_rmdir(char *path);
457 struct file *dentry_open(struct dentry *dentry, int flags);
458 void file_release(struct kref *kref);
460 /* Process-related File management functions */
461 struct file *get_file_from_fd(struct files_struct *open_files, int fd);
462 struct file *put_file_from_fd(struct files_struct *open_files, int file_desc);
463 int insert_file(struct files_struct *open_files, struct file *file, int low_fd);
464 void close_all_files(struct files_struct *open_files, bool cloexec);
465 void clone_files(struct files_struct *src, struct files_struct *dst);
466 int do_chdir(struct fs_struct *fs_env, char *path);
467 char *do_getcwd(struct fs_struct *fs_env, char **kfree_this, size_t cwd_l);
470 int ls_dash_r(char *path);
472 #endif /* ROS_KERN_VFS_H */