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