File Implementation in the Linux

File Implementation in the Linux Disk space are divided into a small size of space called disk block.  A block size varies in different system. Numbe...
Author: Miranda Pitts
0 downloads 2 Views 1019KB Size
File Implementation in the Linux Disk space are divided into a small size of space called disk block.  A block size varies in different system. Numbers of blocks are used for saving a file.  To manage a file system, information on which blocks are used for a file must be saved for file management. 

10/12/2015

COSC350 System Software, Fall 2015

1

File Implementation in the Linux Linux system use i-Node file implementation method.  Each file is associated with an i-node.  Information for a file is saved in its inode: such as attributes, block numbers used by the file 

10/12/2015

COSC350 System Software, Fall 2015

2

File Implementation in the Linux

10/12/2015

COSC350 System Software, Fall 2015

3

Directory Linux use Hierarchical directory System  A Linux directory is a special file that acts as a container for other files and even other directories.  The directory entry provide the information needed to find the file disk blocks or subdirectory 

10/12/2015

COSC350 System Software, Fall 2015

4

Linux Hierarchical Directory Root directory of File System

/

bin

dev

tom

10/12/2015

etc

home

mary

scot

sbin

mike

COSC350 System Software, Fall 2015

root

usr

Directory Below root

User home directory

5

File System partition

partition

i-node list

partition

Directory and data blocks

Boot block Super block i-node

10/12/2015

i-node



i-node

COSC350 System Software, Fall 2015

6

File System Data and Directory Block

i-node list

Data block

Direct block

Data block

Direct Block

i-node number

i-node

10/12/2015

i-node



Data block

File name

i-node

COSC350 System Software, Fall 2015

i-node number

File name

7

File Implementation in the Linux

10/12/2015

COSC350 System Software, Fall 2015

8

File System 

   

Every i-node has link count that contains the number of directory entries that point to the inode. A file be deleted when the link count is 0. Unlinking means delete a file entry from a directory. If link count is > 1, unlinking file does not delete the file. The link count is contained in the st_nlink in the stat data structure

10/12/2015

COSC350 System Software, Fall 2015

9

File System struct stat { mode_t st_mode; /*file type & mode (permissions) */ ino_t st_ino; /* i-node number */ dev_t st_dev; /* device number (file system) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ blksize_t st_blksize; /* best I/O block size */ blkcnt_t st_blocks; /*number of 512 byte blocks allocated */ mode_t st_attr; /* The DOS-style attributes for this file */ }; 10/12/2015

COSC350 System Software, Fall 2015

10

File System The i-node contains most of information (attributes) about the file: the file type, the file’s access permission bit,…and so on.  But only two information are stored in the directory entry: the filename and the inode number.  When move a file from one directory to another directory need only change directory entry point. 

10/12/2015

COSC350 System Software, Fall 2015

11

File System Data and Directory Block

i-node list

Data block

Direct block

Data block

Direct block

sxwang

home

/ i-node number

Direct Block

home

i-node number

sxwang i-node number

i-node

i-node



i-node

foo

i-node

Move /home/sxwang/foo to /home/foo 10/12/2015

COSC350 System Software, Fall 2015

12

File System Data and Directory Block

i-node list

Data block

Direct block

Data block

Direct block

i-node

i-node



i-node

sxwang

home

/ i-node number

Direct Block

home

i-node number

sxwang

i-node number

foo

i-node

Move /home/sxwang/foo to /home/foo 10/12/2015

COSC350 System Software, Fall 2015

13

The link() System Call Any file can have multiple directory entries pointing to its i-node.  The way we can create a link to an existing file is with the link system call.  Prototype: 

#include int link (const char *existingpath, const char *newpath) Returns: 0 if OK,

10/12/2015

COSC350 System Software, Fall 2015

return -1 on error

14

The link() System Call #include int link (const char *existingpath, const char *newpath) Returns: 0 if OK, return -1 on error    

The link system call create a new directory entry newpath that references the existing file existingpath. The creation and increment of the link count be done automatically by kernel. Only a super-user process can create a new link that points to a directory. Because link system calls to a directory can cause loops in the file system.

10/12/2015

COSC350 System Software, Fall 2015

15

The link() System Call /home/sxwang

Lecture

cosc 220

cosc350

lecture

chapter2

lec11

link.c

10/12/2015

COSC350 System Software, Fall 2015

16

The link() System Call /home/sxwang

Lecture

cosc 220

cosc350

lecture

chapter2

lec11

link.c

10/12/2015

COSC350 System Software, Fall 2015

17

/* link.c */ #include #include #include #include #include void err_sys(char *str) { printf ("%s\n",str); exit (1); } int main () { struct stat buff; /* get attributes for the file link.c*/ if (stat ("link.c", &buff) < 0) err_sys("stat error for foo"); printf ("link count for a file link.c was %d \n", buff.st_nlink); /* increase the number of link by one */ if (link ("/home/sxwang/Lecture/cosc350/lecture/lec11/link.c", "/home/sxwang/Lecture/cosc350/lecture/link.c")