file subsystem in linux by dr p.padmanabham professor (cse)&director bharat institute of...

25
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 [email protected]

Upload: magnus-garrett

Post on 20-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

Types of Files 1. Regular files 2. Directory files 3.block and character device files (also called "special files") The following also use fd sockets, fifos, symbolic links

TRANSCRIPT

Page 1: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

File Subsystem in Linux

byDr P.Padmanabham

Professor (CSE)&Director Bharat Institute

of Engineering &Technology Hyderabad

Mobile 9866245898 [email protected]

Page 2: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Topics• Types of files• System calls and library functions• File I/O with system calls (low level I/O)• File descriptor , file and vnode tables• Error handling- perror and strerror• Read directory contents opendir and readdir• Inode structure and stat system call• Reading i-node• Use access to test file access rights• link , symlink , unlink and rename system calls• file and record locking

Page 3: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Types of Files

1. Regular files2. Directory files3.block and character device files (also called "special files")The following also use fd sockets, fifos, symbolic links

Page 4: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

System calls and library functions• system calls are provided by the system and are executed in

the system kernel. • They are entry points into the kernel and are therefore NOT

linked into your program.• These are not portable calls. • Because system calls are part of the O/S , the program has to

make a context switch to the kernel and are executed by OS and not the user program.

• Library calls include the ANSI C standard library and are therefore portable.

• The code for these functions is linked into your program.

Page 5: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Review of File I/O with library calls• Several c-library functions are used for file I/o• Files are manipulated by FILE (strcture) pointer . • All the library calls need this FILE pointer to be passed as

parameter• fopen() , fclose() , are used to open and close a file.• fgetc() and fputc for character I/o • fscanf() and fprintf for formated I/O• fread() and fwrite() for reading and writing a block of bytes. • fseek() to position the file pointer to any where within the file

Page 6: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Basic System calls on files• int open(const char *path, int oflags, …)• open system call returns an int >= 0 (in practice >2 as 0,1,2

are stdin,stdout and stderr) known as file descriptor and -1 when in error

• The file descriptor(fd) is needed in other system calls that manipulate open files.

• The oflags are defined in fcntl.h.• Use #include<fcntl.h> in your program• open is a variable argument call and more flags along with

oflags can also be specified if needed.• int fclose(int fd) closes the file and returns 0 if successful

otherwise -1

Page 7: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

O_flags• O_RDONLY opens file for reading• O_WRONLY opens file for writing• O_RDWR opens file for reading and writing• Only one of the above flags should be used

Page 8: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

O_flags contd• One or more of the fllowing flags can be use along

with one of the oflag of previous slide if neaded• O_APPEND open file with append mode• O_TRUNC truncate the file to zero bytes• O_CREAT create file if it does not exist• O_EXCL generates error if O_CREAT is used and the

file exits• O_SYNC sync read and write

Page 9: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

SFLAGS FOR FILE PERMISSIONS• If O_CREAT flag is used and a new file is

crated ,the permissions can be specified either with octal number (ex:0644) or S_flags

• Example• fd= open(“my.txt”, O_WRONLY | O_CREAT |

O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP | S_IROTH);

• The above Sflags are same as 0644

Page 10: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

read and write system calls

• int read(int fd,void *buff, int nbyts);• read attempts to read nbyts from the file opened in

read mode with the file descriptor fd into buff and returns bytes read 0 when eof is encountered.

• Int write(int fd,void *buff, int nbyts);• write attempts to write nbyts to the file opened in

wite mode with the file descriptor fd from the buff and returns actual bytes written

Page 11: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

lseek system call• lseek call moves the file offset to a specified point• long lseek(int fd, long offset, int whence);• the offset and whence together control the

positioning of the file• Whence can be one of the following• SEEK_SET beginning of the file• SEEK_CUR current location• SEEK_END end of file• EX: lseek(fd,-2,SEEK_END);

Page 12: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

umask and chmod system calls

• Do not confuse with the shell commands umank and chmod

• int umask(int nmask);• Umask sets nmask as new mask and returns

old mask.• Int chmod(const char * path , octal mode);• fchmode works with fd instead of path

Page 13: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Three table structure

The Kernel maintains three tables in main memory that contains all information about open files that a process needs to access.

1. file-descriptor table: contains all file descriptors and each point to file-table.

2. file-table: contains all parameters that are specified at the time of file opening and alo the file offset. This table points to a vnode table

3.Vnode table: this contains all inode information•

Page 14: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Inode structure

1. The size of the file in bytes.2. Device ID (this identifies the device containing the file).3. The User ID of the file's owner.4. The Group ID of the file.5. The file mode which determines the file type and how the file's owner, its group, and others can access the file.6. Additional system and user flags to further protect the file (limit its use and modification).

Page 15: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Inode structure contd.

7. Timestamps telling when the inode itself was last modified (ctime, inode change time), the file content last modified (mtime, modification time), and last accessed (atime, access time).8. A link count telling how many hard links point to the inode.9. Pointers to the disk blocks that store the file's contents . The stat system call retrieves a file's inode number and some of the information in the inode.

Page 16: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

stat system call stat system call is used to access the inode

information It reads this information into a structure stat defined

in sys/stat.h 3 types of calls are there and all of them called with a

pointer to struct stat Int stat(const char *path, struct stat * buff); Int fstat( int fd, struct stat * buff); Int lstat(const char *path, struct stat * buff); returns

link attributes

Page 17: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

stat structurestruct stat {

dev_t st_dev; /* ID of device containing file */

ino_t st_ino; /* inode number */

mode_t st_mode; /* protection */

nlink_t st_nlink; /* number of hard links */

uid_t st_uid; /* user ID of owner */

gid_t st_gid; /* group ID of owner */

dev_t st_rdev; /* device ID (if special file) */

off_t st_size; /* total size, in bytes */

blksize_t st_blksize; /* blocksize for file system I/O */

blkcnt_t st_blocks; /* number of 512B blocks allocated */

time_t st_atime; /* time of last access */

time_t st_mtime; /* time of last modification */

time_t st_ctime; /* time of last status change */

};

Page 18: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

How to determine file type? • The following Macros on st_mode can be used:• S_ISREG regular file• S_ISDIR directory• S_ISBLK block special file• S_ISCHR character special files• S_ISLNK symbolic link• S_ISFIFO FIFO• S_ISSOCK socket• Ex: if(S_ISDIR(st_mode)) printf(”is a directory”);

Page 19: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

opendir and readdir system calls Directory is file in which the file names and

the inode numbers are stored However you cannot use open/read/write

system calls for security reasons. The following system calls are used: DIR *opendir(const char *dirname); struct dirent *readdir(DIR *dirp); int closedir(DIR *dirp);

Page 20: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Use of access system calls• One method to check file permissions is to use stat system call • A more simpler way is to use access system call• #include <unistd.h>• int access(const char *pathname, int amode); • where amode can be one of the following:• R_OK read ok• W_OK write ok• X_OK execute ok• F_OK file exists• Access return 0 on success else -1.

Page 21: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

rename,mkdir,rmdir system calls

• int mkdir(const char*path, mode_t mode);• Int rmdir(const *path);• int rename(const *old, cont *new);

Page 22: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Soft links or Symbolic links• A softlink plays the same role as a Windows shortcut.• A softlink is a file that points to another file.• When you create a softlink, you are creating a new file that

exists only as a pointer to a file elsewhere on the system.• soft links are created with the ln command.• if you don’t use -s, you’ll get a hardlink, which .• The syntax is:• ln -s [target] [link name]

Page 23: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Hard links• A hard link is another instance of the original file. • when a hard link is created , another pointer to the data location on disk is

created and not a pointer to the existing file.• That means that editing a hard link of a file is equivalent to editing the

original instance of the file.• A hard link is created by using ln command without –s flag• A count of hard links is maintained by kernel in the i-node• Hard links cannot link directories where as soft link can• If the original file is deleted soft link cannot recover it where as a hard link

can• Because a hard link is a direct reference to the underlying file system, you

can’t hard link across file systems but soft link can be used• Hard links don’t take up any additional space .(same i-node)• Creating a soft link is creating a new file.(Requires to create a new i-node)

Page 24: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

Link, symlink, and unlik system calls

• Though only one command ln creates both hard and soft links , two separate system calls are needed (link and symlink)

• int link(const char *path1, const char *path2);• int symlink(const char *path1, const char *path2);• Unlink system call reduces the link count by 1 and

the kernel frees the disc space when count is zero• int unlink(const char *path);

Page 25: File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering…

File and Record locking There are occasions where multiple processes want

to share a file (resource) we have to be sure that only one process access the

file at a time. Int lockf(int fd, int flg ,long size); Where flg has one of the following values: 1. F_ULOCK unlocks previously locked region. 2. F_LOCK locks the region specified(blocking) 3. F_TLOCK test and lock region(non blocking) 4. F_TEST test a region to see if it is locked