240 likes | 351 Views
CSC 660: Advanced OS. Virtual Filesystem. Topics. Filesystems Filenames and Pathnames File Attributes File Operations Virtual Filesystem VFS Objects Processes and Files. Why filesystems?. Tree Structure. /. bin. boot. tmp. usr. var. bin. lib. X11R6. ls. grub. less. vmlinuz.
E N D
CSC 660: Advanced OS Virtual Filesystem CSC 660: Advanced Operating Systems
Topics • Filesystems • Filenames and Pathnames • File Attributes • File Operations • Virtual Filesystem • VFS Objects • Processes and Files CSC 660: Advanced Operating Systems
Why filesystems? CSC 660: Advanced Operating Systems
Tree Structure / bin boot tmp usr var bin lib X11R6 ls grub less vmlinuz bin lib zip menu.lst xclock xterm CSC 660: Advanced Operating Systems
Filenames and Pathnames • Filenames • Human-readable identifier for data. • File suffixes have special meanings in Windows. • Directories • Store lists of filename/location mappings. • Pathnames • Identify file in directory hierarchy. • Ex: /usr/X11R6/bin/xclock CSC 660: Advanced Operating Systems
File Attributes • Filename and Data • Location • File Type • Regular files • Directories • Device (block + char) files • IPC files • Ownership • Access Control List • Locking • Size • Timestamps CSC 660: Advanced Operating Systems
Create Delete Open Close Read Write Append Seek Get Attributes Set Attributes Rename File Operations CSC 660: Advanced Operating Systems
File Descriptors User process reference to a file. A non-negative integer unique to process. Returned by open or creat system calls. Used as argument to other file system calls. View with: ls –l /proc/self/fd Common file descriptors: 0 STDIN 1 STDOUT 2 STDERR CSC 660: Advanced Operating Systems
Process File Context • Root directory • Usually /, but can change for security. • Current working directory • Default location for file commands. • Relative pathnames are relative to CWD. • Open file table • Maps file descriptors to kernel file objects. • files_struct member of task_struct CSC 660: Advanced Operating Systems
Open File Table struct files_struct { atomic_t count; spinlock_t file_lock; int max_fds; int max_fdset; int next_fd; struct file ** fd; fd_set *close_on_exec; fd_set *open_fds; fd_set close_on_exec_init; fd_set open_fds_init; struct file * fd_array[NR_OPEN_DEFAULT]; }; CSC 660: Advanced Operating Systems
Opening and Closing #include <fcntl.h> #include <unistd.h> int open(char *path, int oflag); /* Common flags: O_RDONLY, O_WRONLY, O_CREAT Returns FD on success, -1 on failure */ int close(int filedes); /* Returns 0 on success, -1 on failure */ CSC 660: Advanced Operating Systems
Reading and Writing #include <unistd.h> ssize_t read(int fd, void *buf, size_t nbytes); /* Returns # bytes read, 0 if EOF, -1 error */ ssize_t write(int fd, void *buf, size_t nbytes); /* Returns # bytes written, -1 on error */ off_t lseek(int fd, off_t offset, int whence) /* Whence: SEEK_SET (f/ 0) or SEEK_CUR (f/ current) Returns new file offset, -1 on error */ CSC 660: Advanced Operating Systems
Virtual Filesystem CSC 660: Advanced Operating Systems
Classes of Filesystems • Disk-based filesystems • Linux (ext2, ext3, reiserfs) • UNIX (ufs, minix, JFS, XFS) • MS (FAT, VFAT, NTFS) and other proprietary • ISO9660 CD-ROM, UDF DVD • Network filesystems • NFS, AFS, CIFS • Special filesystems • Procfs, sysfs CSC 660: Advanced Operating Systems
VFS Objects • superblock • Represents a mounted filesystem. • inode • Represents a specific file. • dentry • Represents a directory entry, a single path comp. • file • Represents an open file associated w/ a process. CSC 660: Advanced Operating Systems
VFS Objects • Directories are files • Directories are handled by file objects. • dentry objects are path components, not dirs. • Each object contains an operations object • Define methods kernel invokes on object. • Defined as struct of function pointers. • What if a fs doesn’t have a type of object? • Objects of that type made on the fly for VFS. CSC 660: Advanced Operating Systems
VFS Objects CSC 660: Advanced Operating Systems
struct super_block CSC 660: Advanced Operating Systems
struct inode CSC 660: Advanced Operating Systems
Inode Operations int create(struct inode *dir, struct dentry *dentry, int mode) struct dentry *lookup(struct inode *dir, struct dentry *dentry) int link(struct dentry *old, struct inode *dir, struct dentry *dentry) int unlink(struct inode *dir, struct dentry *dentry) int symlink(struct inode *dir, struct dentry *dentry, const char *symname) int mkdir(struct inode *dir, struct dentry *dentry, int mode) int rmdir(struct inode *dir, struct dentry *dentry) int rename(struct inode *old_dir, struct dentry *old, struct inode *new_dir, struct dentry *new) int readlink(struct dentry *dentry, char *buffer, int buflen) CSC 660: Advanced Operating Systems
Dentry Objects • Path components • Ex: /bin/vi has 3 dentries: /, etc, and vi • Not an on-disk data structure • Constructed on fly from pathname string. • Cached by the kernel to avoid re-construction. • States • Used: corresponds to valid inode in use. • Unused: valid inode not currently used (cached). • Negative: invalid path, no corresponding inode CSC 660: Advanced Operating Systems
struct dentry CSC 660: Advanced Operating Systems
struct file CSC 660: Advanced Operating Systems
References • Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3rd edition, O’Reilly, 2005. • Robert Love, Linux Kernel Development, 2nd edition, Prentice-Hall, 2005. • Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, 2005. • Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, 2005. • Avi Silberchatz et. al., Operating System Concepts, 7th edition, 2004. • Andrew S. Tanenbaum, Modern Operating Systems, 3rd edition, Prentice-Hall, 2005. CSC 660: Advanced Operating Systems