csc 660: advanced operating systemsslide #1 csc 660: advanced os virtual filesystem

25
CSC 660: Advanced Operating Systems Slide #1 CSC 660: Advanced OS Virtual Filesystem

Upload: peter-burke

Post on 19-Jan-2018

237 views

Category:

Documents


0 download

DESCRIPTION

CSC 660: Advanced Operating SystemsSlide #3 Why filesystems? Physical DiskFilesystem Access byBlocksBytes AddressingBlock #Pathname SecurityNoneACLs ReliabilityCorruption on crashRobust response to system failures.

TRANSCRIPT

Page 1: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #1

CSC 660: Advanced OS

Virtual Filesystem

Page 2: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #2

Topics

1. Filesystems2. Filenames and Pathnames3. File Attributes4. File Operations5. Virtual Filesystem6. VFS Objects7. Processes and Files

Page 3: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #3

Why filesystems?Physical Disk Filesystem

Access by Blocks Bytes

Addressing Block # Pathname

Security None ACLs

Reliability Corruption on crash Robust response to system failures.

Page 4: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #4

Tree Structure/

bin boot tmp usr var

ls grub bin lib X11R6

vmlinuzmenu.lst

lesszip bin lib

xclock xterm

Page 5: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #5

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

Page 6: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #6

File Attributes• Filename and Data• Location• File Type

– Regular files– Directories– Device (block + char) files– IPC files

• Ownership• Access Control List• Locking• Size• Timestamps

Page 7: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #7

File Operations• Create• Delete• Open• Close• Read• Write

• Append• Seek• Get Attributes• Set Attributes• Rename

Page 8: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #8

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 STDIN1 STDOUT2 STDERR

Page 9: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #9

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

Page 10: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #10

Open File Tablestruct 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];};

Page 11: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #11

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 */

Page 12: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #12

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

*/

Page 13: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #13

Virtual Filesystem

Page 14: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #14

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

Page 15: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #15

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.

Page 16: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #16

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.

Page 17: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #17

VFS Objects

Page 18: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #18

struct super_blockType Field Descriptionstruct list_head s_list Superblock linked listdev_t s_dev Device identifieru_long s_blocksize Block size in bytesu_char s_dirt Dirty (modified) flagstructsuper_operations *

s_op Superblock methods

struct semaphore s_lock Superblock semaphorestruct list_head s_inodes List of all inodesstruct list_head s_io Inodes waiting for writestruct list_head s_files List of file objects

Page 19: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #19

struct inodeType Field Descriptionstruct list_head i_list Inode linked liststruct list_head i_dentry List of dentries to this inodeinode_operations *i_op Inode methodsu_long i_ino Inode numberatomic_t i_count Reference countumode_t i_mode ACL for fileu_long i_nlink Number of hard linksuid_t,gid_t i_{uid,gid} UID and GID of ownerloff_t i_size File size in bytesstruct timespec i_[amc]time Last access, modify, change

Page 20: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #20

Inode Operationsint 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)

Page 21: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #21

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

Page 22: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #22

struct dentryType Field Descriptionatomic_t d_count Usage countspinlock_t d_lock Lock for this dentrystruct inode *d_inode Corresponding inodestructdentry_operations

*d_op Dentry methods

struct list_head d_lru List of unused dentries.struct list_head d_child Dir dentries of same parent.struct list_head d_subdirs Subdirectory dentries.struct list_head d_alias Dentries w/ same inode.

Page 23: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #23

File Objects

In-memory representation of open files.Created in response to open() call.Destroyed in response to close() call.

Multiple file objects can exist for same file.Different processes can open same file.File object records process-specific status info

(seek point, access mode.)

Page 24: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #24

struct fileType Field Descriptionstruct list_head f_list Linked list of file objectsstruct dentry *f_dentry Associated dentry objectatomic_t f_count Usage countu_int f_flags Flags specified on open()structfile_operations *

f_op File methods (open, read, write, readdir, ioctl)

mode_t f_mode File access modeloff_t f_pos File offsetu_int f_{uid,gid} User’s UID and GIDint f_error Error code

Page 25: CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem

CSC 660: Advanced Operating Systems Slide #25

References1. Daniel P. Bovet and Marco Cesati, Understanding the

Linux Kernel, 3rd edition, O’Reilly, 2005.2. Robert Love, Linux Kernel Development, 2nd edition,

Prentice-Hall, 2005.3. Claudia Rodriguez et al, The Linux Kernel Primer,

Prentice-Hall, 2005.4. Peter Salzman et. al., Linux Kernel Module Programming

Guide, version 2.6.1, 2005.5. Avi Silberchatz et. al., Operating System Concepts, 7th

edition, 2004.6. Andrew S. Tanenbaum, Modern Operating Systems, 3rd

edition, Prentice-Hall, 2005.