The UNIX File System

Similar documents
The UNIX File System

Unix System Architecture, File System, and Shell Commands

Input & Output 1: File systems

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

File Systems. File system interface (logical view) File system implementation (physical view)

UNIX File Hierarchy: Structure and Commands

CptS 360 (System Programming) Unit 6: Files and Directories

File Management. Ezio Bartocci.

1 / 22. CS 135: File Systems. General Filesystem Design

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

Files and Directories

Unix Filesystem. January 26 th, 2004 Class Meeting 2

Files and File Systems

UNIX File System. UNIX File System. The UNIX file system has a hierarchical tree structure with the top in root.

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

File Management 1/34

Permission and Ownership

Principles of Operating Systems

History. 5/1/2006 Computer System Software CS012 BE 7th Semester 2

Long-term Information Storage Must store large amounts of data Information stored must survive the termination of the process using it Multiple proces

Operating Systems Lab

Operating System Labs. Yuanbin Wu

1 / 23. CS 137: File Systems. General Filesystem Design

Files & I/O. Today. Comp 104: Operating Systems Concepts. Operating System An Abstract View. Files and Filestore Allocation

(32 KB) 216 * 215 = 231 = 2GB

Filesystem. Disclaimer: some slides are adopted from book authors slides with permission

Introduction to OS. File Management. MOS Ch. 4. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

File Systems. CSE 2431: Introduction to Operating Systems Reading: Chap. 11, , 18.7, [OSC]

Files and File Systems

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

5/8/2012. Creating and Changing Directories Chapter 7

File System Definition: file. File management: File attributes: Name: Type: Location: Size: Protection: Time, date and user identification:

Exploring the file system. Johan Montelius HT2016

Introduction to the Linux Command Line

CSI3131 Operating Systems Tutorial 9 Winter 2015 File Systems

Arvind Krishnamurthy Spring Implementing file system abstraction on top of raw disks

Links, basic file manipulation, environmental variables, executing programs out of $PATH

OPERATING SYSTEMS: Lesson 12: Directories

CMSC421: Principles of Operating Systems

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

Implementation should be efficient. Provide an abstraction to the user. Abstraction should be useful. Ownership and permissions.

File Systems Ch 4. 1 CS 422 T W Bennet Mississippi College

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

Chapter 1 - Introduction. September 8, 2016

UNIX File Systems. How UNIX Organizes and Accesses Files on Disk

3/26/2014. Contents. Concepts (1) Disk: Device that stores information (files) Many files x many users: OS management

commandname flags arguments

Welcome to Linux. Lecture 1.1

Lecture 2b. Pathnames, files, special characters in filenames, and file permissions. COP 3353 Introduction to UNIX, FALL 2013

CS 4284 Systems Capstone

Permissions and Links

Chapter 10: File-System Interface

PERSISTENCE: FSCK, JOURNALING. Shivaram Venkataraman CS 537, Spring 2019

Chapter 10: File-System Interface

Chapter 11: File System Implementation. Objectives

Files (review) and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

CSCE 313 Introduction to Computer Systems

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

ICS Principles of Operating Systems

File Systems: Naming

File System Internals. Jo, Heeseung

There is a general need for long-term and shared data storage: Files meet these requirements The file manager or file system within the OS

Files

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

ECE 598 Advanced Operating Systems Lecture 18

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

Operating Systems CMPSC 473. File System Implementation April 1, Lecture 19 Instructor: Trent Jaeger

CS4500/5500 Operating Systems File Systems and Implementations

File Systems. What do we need to know?

Directory. File. Chunk. Disk

Operating System Labs. Yuanbin Wu

Logical disks. Bach 2.2.1

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

File system links. Week Overview. Hard and symbolic links Process management

find Command as Admin Security Tool

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Operating Systems: Lecture 12. File-System Interface and Implementation

Typical File Extensions File Structure

Filesystem. Disclaimer: some slides are adopted from book authors slides with permission 1

Inode. Local filesystems. The operations defined for local filesystems are divided in two parts:

Virtual File System. Don Porter CSE 306

INTERNAL REPRESENTATION OF FILES:

F 4. Both the directory structure and the files reside on disk Backups of these two structures are kept on tapes

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

File System Case Studies. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

W4118 Operating Systems. Instructor: Junfeng Yang

Week 2 Lecture 3. Unix

File Systems 1. File Systems

File Systems 1. File Systems

Local File Stores. Job of a File Store. Physical Disk Layout CIS657

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

File Systems: Allocation Issues, Naming, and Performance CS 111. Operating Systems Peter Reiher

CMSC421: Principles of Operating Systems

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Operating Systems, Unix Files and Commands SEEM

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Transcription:

The UNIX File System Magnus Johansson May 9, 2007 1 UNIX file system A file system is created with mkfs. It defines a number of parameters for the system, such as: bootblock - contains a primary boot program superblock - static parameters of the file system, like total size, block and fragment sizes of data blocks inodes - stands for index node data blocks - each block has typically a size of 4Kbyte or 8Kbyte fragment data block size - typically of size 512byte or 1024byte The number of inodes determines the maximum number of files in the file system. Bootblock Superblock Inodes Disc Fragment Figure 1: Disk layout 1.1 File system structure Files have no structure at all, they are only flat sequences of bytes. Directories are files that contain information on how to find other files. Directories are arranged in a familiar tree structure. Different disks (machines) may have different filesystems and we need a way of accessing files located on different disks. One solution is to do it like Windows does it, where we give each disk a separate name, like C: or D:. In UNIX a part or all of a disk s file system can be mounted in another disk s file system. The user sees a single file tree and no longer has to be aware of which file resides on which device. The root file system / is always available on a machine while other parts can be integrated (mounted) into the file system. 1

Hard disk / Hard disk / Diskette / a b a b x y z c d x y z c d The diskette is mounted on the directory b p q r a) How it looks like p q r b) How it is implemented Figure 2: Mounting 1.2 Files and inodes A file consists of exactly one inode, and zero or more data blocks. An inode is a structure used to maintain information about the file. It includes fields for the following: file mode owner (user and group) timestamps (three different) size (bytes, blocks) reference count pointers to data Important: A file does NOT have a name. The file is uniquely identified by its inode number. The file mode is normally referred to as permissions, but it also contains information about the type of file. Normally when you do a ls -l, you see something like this: lrwxrwxrwx 1 user group file1 -rw-r--r-- 1 adavid docs file2 The first bits identify as one of: a regular file: - a directory: d a symbolic link: l some other things, see the manual page for stat Permissions are the low-order 9 bits of the mode bytes and they define who may do what with the file. The bits are normally presented like rwx, where r, w and x stand for read, write, and execute, respectively. For each file this is defined for: the owner (the first 3 bits) 2

Bootblock Superblock Inodes Inode number: 15 Fragment Filemode Owner Timestamps Size Reference Count Pointers to data Figure 3: An inode the owner s group (the next 3 bits) everyone else (the last 3 bits) chmod lets you change the permission mode of a file. There are three timestamps defined: modification time - when the file was last changed access time - when the file was last read status time - when certain changes was made to the inode Through pointers in the inode we can access the file s data blocks. For reasons of disk space efficiency, there are four different types of pointers: direct (12) single indirect (1) double indirect (1) triple indirect (1) If the file consists of 12 or fewer data blocks we can access them directly from the 12 direct pointers in the inode. For block sizes 4Kbyte or 8Kbyte, this means that files up to 48Kbyte or 96Kbyte, respectively, can be accessed entirely from the information in the inode. The single indirect pointer is necessary in order to create files of more than 12 data blocks. The single indirect pointer points to a single data block, whose contents are treated as direct pointers to data blocks. We can now use a couple of hundred data blocks (files of a few MB). The double indirect pointer is necessary in order to create file of more than a few MB. The double indirect pointer points to a data block whose contents are treated as single indirect pointers. Each of these pointers points to a data block, whose contents are treated as direct pointers to data blocks. This is enough to reach the filesize limit on most systems. 1.3 Directories Directories are files, but we treat them differently. A directory can be identified by its mode bytes. A directory is a file theat consists of a number of records, each of which contains the following fields: 3

inode data blocks direct pointers (12) single indirect double indirect triple indirect Figure 4: Direct pointers inode data blocks direct pointers (12) data blocks single indirect direct pointers double indirect triple indirect Figure 5: Single indirect pointers a pointer to the next record a number identifying an inode (i.e. another file) a number identifying the length of the record a string containing the name of the record (max 255 chars). It is this name we usually refer to as a filename. Note that it is part of the directory, and not part of the file. (possibly some padding) If you type ls -li in the directory in figure 6, you will see: 17 -rw-r--r-- 1 (...) foo.c 29 -rwxr-xr-x 1 (...) hej 1.4 Links By associating a name in a directory with a file we get what is known as a link. Don t confuse these links with symbolic links. The directory in figure 7 contains links to inodes 27 and 51, so we can refer to file 27 as test and file 51 as bar.c. 17 5 foo.c 29 3 hej Figure 6: A directory structure 4

27 4 test 51 5 bar.c inode 27 inode 51 Figure 7: Links 27 4 test 27 5 bar.c inode 27 ref=2 Figure 8: Hard links > ls -li 51 lrwxrwxrwx 1 (...) bar.c 27 -rw-r--r-- 1 (...) test We can have more than one link to a file. This kind of link is known as a hard link. Do not confuse it with a symbolic link. In figure 8, the reference counter in inode 27 will be 2 because there are two links to the inode, and we can use either name ( test or bar.c ) to refer to the same file. > ls -li 27 -rw-r--r-- 2 (...) bar.c 27 -rw-r--r-- 2 (...) test There is absolutely no difference between the two, and the links do not need to be in the same directory. To create a hard link you use ln. Symbolic links are quite different from hard links. A symbolic link is actually a separate file, whose contents is the name of another file or directory. A mode bit indicates that a file is to be interpreted as a symbolic link. When you type ls -li in the directory in figure 9 you get: 51 lrwxrwxrwx 1 (...) bar.c -> test 27 -rw-r--r-- 1 (...) test Symbolic links are created with the shell command ln or the system call symlink. If we have a file that we know is a symbolic link, we can use the system call readlink to get the contents of the link, i.e. determine where it points. 27 4 test 51 5 bar.c inode 27 inode 51 "link" test Figure 9: Symbolic link 5

27 4 test 51 4 mail inode 27 inode 51 ref=2 (to parent) 51 1... Figure 10: Directory links 1.5 Creating, using, and destroying stuff 1.5.1 Creating a file To create a file we use the open system call with some arguments to tell it to create a file. When you do this a free inode is found and initialized and an entry is created in the current directory to point to the inode. Initially the file is empty, that is, there are no data blocks. 1.5.2 Creating a directory To create a directory we can use either the shell command mkdir or the system call with the same name. When creating a directory the following happens: A file is created (i.e. an inode is allocated), and it is identified as a directory. Then a link to the inode is created in the current directory, and in the new directory two entries are created:. points to the directory s own inode.. points to the parent s inode Figure 10 shows the situation. 1.5.3 Removing files To remove a file we use the shell command rm or the system call unlink. When you do this the following happens: First the directory entry is freed and the record pointer of the previous entry is reset. Then the file reference counter is decreasedby one, and if it now is zero, the data blocks and the inode are freed. 1.5.4 Reading a directory It is possible to open a directory directly like any other file and read the data structures it contains. This is not the recommended method since the actual structures and the order they appear in may vary among systems or among disks within a single system. It is also inconvenient. The easy and portable method is to use the three standard functions opendir, readdir, and closedir. This is how they are used in C: DIR *d; struct dirent *f; d=opendir( ); while (f=readdir(d)) { (use f) } closedir(d); 6

1.5.5 Reading an inode We can read an inode with one of the stat system calls. There is more than one stat function. Use the right one! We can now extend the C example: struct stat s; d=opendir( ); while (f=readdir(d)) { (use f) stat(f->d_name, &s); (use s) } closedir(d); 7