Вы находитесь на странице: 1из 41

LINUX File System

Slides adopted from:


Understanding Linux Kernel. Bovet and Cesati

CCE651..12

Hard Disk Layout


It consists of sectors. The first sector contains the MBR (Master Boot record). The MBR contains the Boot code and partition information. There could be a maximum of 4 partitions.

Hard Disk Layout


Boot code 1 2 3 4 Boot Sector

Partition table Partitions within an extended partition

Partition 1

Partition 2

Partition 3 (Extended)

Partition 4
3

Introduction
Data used by a computer system is stored in a variety of different storage medium A file system provides a logical view of this data/information The OS provides a mapping between the logical and physical units of storage
4

Introduction

Mapping from logical to physical units of storage


5

Files
A named collection of related info Consists of a sequence of bits, bytes, lines, or records For each file, the OS keeps the following info:
Name Type Location Size Important dates
6

Directories
A file system may be divided into partitions Each partition will have a directory structure Directories store information such as name, size, and location of a file Operations performed on directories include: search for a file, create a file, list a directory, traverse a file system
7

A Typical File System Organization

Directory Structure
Collection of nodes that contains information about the all files
Directory

Files

F1

F2

F3

F4

Fn

Both directory structures and files reside on disk


9

General Graph Directory

10

Directory Implementation
Linear list of file names with pointer to the data blocks
simple to program time-consuming to execute

Hash Table linear list with hash data structure


decreases directory search time collisions situations where two file names hash to the same location fixed size
11

Allocation Methods Refers to how disk blocks are allocated for files:
Contiguous allocation Linked allocation Indexed allocation

12

Contiguous Allocation

First Fit and Best Fit

13

Contiguous Allocation
Each file occupies a set of contiguous blocks on the disk Simple, only starting location and length (number of blocks) are required Random access External fragmentation (dynamic storage-allocation problem) Files hard to grow
14

Linked Allocation

15

Linked Allocation
Each file is a linked list of disk blocks that may be scattered anywhere on the disk Simple need only starting address and no size-declaration is necessary No fragmentation or waste of space Sequential access only (no random access) Each block consumes 4 bytes to point to the next block (4B in 512B/B.78% waste) If one pointer is lost, the file state is not guaranteed
16

File-Allocation Table

17

File-Allocation Table (FAT)


Used by MS-DOS and OS2 A section of space at the beginning of each partition is set a side to contain the table The table has entry for each disk block and is indexed by block number Directory entry contains the block number of the first block of the file The table entry contains the block number of the next block in the file The last block of the file has a special end of file entry
18

Indexed Allocation

19

Indexed Allocation
Solves the problem of direct access
Brings the pointers into one location: the index block Index block is an array of disk-block addresses The ith entry in the index block points to the ith block in the file

Directory contains the address of the index block


20

Indexed Allocation
Each file has an index block
If file is small few non null pointers (i.e. space is wasted more than with a linked list) If file is big Index block may not be sufficient to hold enough pointers to file blocks

21

Virtual File System in Linux


There is large number of file systems supported by LINUX. The range of file systems supported is made possible by the unified interface to the LINUX kernel. This is the Virtual File System Switch (VFS). The Virtual file system supplies the applications with the system calls for the management, maintains internal structures and passes tasks onto the appropriate actual file system.

22

Virtual File System


The idea behind the Virtual Filesystem is to put a wide range of information in the kernel to represent many different types of filesystems; there is a field or function to support each operation provided by any real filesystem supported by Linux. For each read, write, or other function called, the kernel substitutes the actual function that supports a native Linux filesystem, the NT filesystem, or whatever other filesystem the file is on.
23

Layers in the file system


Process 1 Process 2

.....

Process n

User mode
System mode

Virtual File System


ext2 msdos minix proc Buffer cache Device drivers

File system

24

Structure of a UNIX inode Access rights Owner Size Times Data Data Data

Direct references to data blocks Indirect block


Two- step indirect reference Three-step indirect reference

Data

Data Data Data

Data Data
Data

Data
25

File Access Permissions


Every file has the following attributes: Owners user ID, Owners group ID user-read permission, user-write permission, user-execute permission group-read permission, group-write permission, group-execute permission other-read permission, other-write permission, other-execute permission
26

Representation of file system in the kernel


Mounting: Before a file can be accessed, the file system containing the file must be mounted. This can be done using the system command mount. Superblock operations: The superblock structure provides, functions for accessing the file system. Inode: The inode structure holds the information on the file and remainder contains management information and the file system dependent union. File operations: The file_operations structure is the general interface for work on files, and contains the functions to open, close, read and write files.
27

Schematic structure of a UNIX file system

Boot block Superblock Inode blocks 1 2 0

Data blocks

28

Ext2 File System


Ext2 file system added by Remy Card in 1993 data held in files is kept in data blocks
block size is 1024 bytes

structure of Ext2 file system: block groups, data blocks close to their inodes and file nodes close to their directory node

29

Structure of the Ext2 file system

Boot block

Block group 0 Block group 1

Block group n

Super Group block descriptors 1 Blk n Blks

Block Inode Inode bitmap bitmap table 1Blk 1Blk n Blks

Data blocks n Blks

30

The superblock in the ext2 filesystem Number of inodes, number of blocks Number of reserved blocks, number of free blocks Number of free inodes, first data block Block size Blocks per group Inodes per group Time of mounting Ext2 signature

31

struct ext2_super_block { unsigned long s_inodes_count; /* Inodes count */ unsigned long s_blocks_count; /* Blocks count */ unsigned long s_r_blocks_count; /* Reserved blocks count */ unsigned long s_free_blocks_count;/* Free blocks count */ unsigned long s_free_inodes_count;/* Free inodes count */ unsigned long s_first_data_block; /* First Data Block */ unsigned long s_log_block_size; /* log(Block size) - 10 */ long s_log_frag_size; /* Fragment size */ unsigned long s_blocks_per_group; /* # Blocks per group */ unsigned long s_frags_per_group; /* # Fragments per group */ unsigned long s_inodes_per_group; /* # Inodes per group */ unsigned long s_mtime; /* Mount time */ unsigned long s_wtime; /* Write time */ unsigned long s_pad; /* Padding to get the magic signature at the same offset as in the previous ext fs */ unsigned short s_magic; /* Magic signature */ unsigned short s_valid; /* Flag */ unsigned long s_reserved[243]; /* Padding to the end of the block */};
32

Block group descriptor


Type
_ _u32
_ _u32 _ _u32 _ _u16 _ _u16 _ _u16 _ _u16 _ _u32 [3]

Field

Description

bg_block_bitmap
bg_inode_bitmap bg_inode_table bg_free_blocks_count bg_free_inodes_count bg_used_dirs_count bg_pad bg_reserved

Block number of block bitmap


Block number of inode bitmap Block number of first inode table block Number of free blocks in the group Number of free inodes in the group Number of directories in the group Alignment to word Nulls to pad out 24 bytes

Its size is 32 bytes.


33

inode in the Ext2 file system


Type/Permissions, User(UID), File size Access time, time of creation Time of modification, time of deletion Group (GID), link counter, no. of blocks File Attributes, Direct blocks, one/two/three stage indirect blocks File ACL, Directory ACL

Its size is 128 bytes


34

struct ext2_inode { unsigned short i_mode; /* File mode */ unsigned short i_uid; /* Owner Uid */ unsigned long i_size; /* Size in bytes */ unsigned long i_atime; /* Access time */ unsigned long i_ctime; /* Creation time */ unsigned long i_mtime; /* Modification time */ unsigned long i_dtime; /* Deletion Time */ unsigned short i_gid; /* Group Id */ unsigned short i_links_count; /* Links count, max 32000 */ unsigned long i_blocks; /* Blocks count */ unsigned long i_flags; /* File flags */ unsigned long i_reserved1; unsigned long i_block[15]; /* Pointers to blocks */ unsigned long i_version; /* File version (for NFS) */ unsigned long i_file_acl; /* File ACL */ unsigned long i_dir_acl; /* Directory ACL */ unsigned long i_faddr; /* Fragment address */ unsigned char i_frag; /* Fragment number */ unsigned char i_fsize; /* Fragment size */ unsigned short i_pad1; unsigned long i_reserved2[2]; };

35

Reserved inodes
Inode 1 is reserved for a list of bad blocks on the device. The root directory has inode number 2. This number must be fixed in advance, since the mount() system call must be able to find the root directory. A few other inode numbers have a special meaning. Inode 11 is the first one for ordinary use.
36

Directory entry
struct ext2_dir_entry_2 { __u32 inode; /* Inode number; 0: unused */ __u16 rec_len; /* Directory entry length */ __u8 name_len; /* Name length */ __u8 file_type; char name[up to 255]; /* File name */ };

37

Ext2 File Types

File_type 0 1 2 3 4 5 6 7

Description Unknown Regular file Directory Character Device Block device Named pipe Socket Symbolic link

38

Creating the File system


Creating a filesystem means setting up the structures. Ext2 filesystems are created by mke2fs utility. Default options are: Block size: 1024 bytes Fragment size: block size Number of allocated inodes: one for each group of 4096 bytes Percentage of reserved blocks: 5% 1. Initializes the superblock and group descriptors 2. For each bock group, reserves all the disk blocks needed to store the superblock, group descriptors, inode table, bitmaps etc. 3. Initializes the inode and data bitmap 4. Initializes the inode table of each block group 5. Creates the / root directory 6. Creates the lost+found directory, which is used by e2fsck to link the lost and defective blocks.
39

Ext3 File system


The enhanced file system evolved from Ext2, so is compatible with Ext2. The most significant feature is: Journaling

40

Ext3 Journaling File system


The goal of a journaling file system is to avoid running timeconsuming consistency checks on the whole file system by Looking instead in a special disk area that contains the most recent disk write operations named journal. The Ext3 journaling is to perform any high-level change to the File system in two steps: First, a copy of the blocks to be written is stored in the journal; then, when the I/O data transfer to the journal is completed (in short, data is committed to the journal), the blocks are written in the file system.
41

Вам также может понравиться