Thursday, September 9, 2010

Unix File System

Unix filesystem
  1. Unix has only files (directory,special file or device file) and processes
  2. Tree like structure (hierarchy)
  3. root node topmost (only one root)
  4. all data in files is a stream of bytes, for the kernel
  5. files dynamically grow
Internal View
  1. made up of blocks (eg 512 or 1024 bytes blocks)
  2. block 0 is boot block (used for booting)
  3. block 1 is super block (store info about file system, like type,free and used space)
  4. then comes the i-list blocks (contains the i-node blocks for all the files in the system)
  5. then comes the data block (storing the actual data)



Directory stores the mapping between the files it contains and the corresponding i-node
  1. I-NODE (store metadata about file)
  2. Evey file has a i-node
  3. i-list containes the list of inodes
  4. i node containes
  •  file owner and group identifier
  •  file type
  •  file access permission
  •  file access/modified times,and inode modified time
  •  number of hard links pointing to this inode
  •  file address (pointing to the data disk blocks)
  •  file size (in bytes)
  • Name of file is stored as data of its parent directory
File Address
  •  15 pointers to the data in disc block
  •  first 12 pointers to block of data
  •  13th pointer to singly indirect pointer
  •  14th pointer to doubly indirect pointer
  •  15th pointer to triply indirect pointer
  •  the number of pointers in the indirect depends on the block size and size of block pointers with a 1024 bytes blocks and 4 bytes pointer each indirect block can have 1024/4 ie 256 pointers.
  1.  Virtual file system (abstraction layer over the real FS)
  • so that kernel can support multiple file systems (eg, ext2,ext3,fat,nfs,/proc)
  • handles system calls related to file system access
  • each lower layer FS presents an interface conforming to VFS
  1. Permissions for the file
  • user categories, u(user),g(group),o(others)
  • permission levels, r(read),w(write),x(execute),s(set-UID or set-groupID),t(sticky bit)
  • thus -rwxrw-r-- means this file has rwx for the owner, rw for group, and r for others
  • the first charcter in this case is '-'(file) it can be 'd'(directory) or l(link)
  • execute on directory means list the files in that dir
  • chmode u+s used for setting SUID, if the program or shell script has the SUID set then it will run with the previleges of the program/shell script owner, not the user running it 
  • chmode g+s used for setting SGID, if SGID is set then the program/script run with the permissions of the group of the owner, not the privileges of the user running it

  1. the last bit in the permission is the sticky bit S_ISVTX (t or T)(save text bit),
  2.  sticky bit on directories
  3.  files inside the directory can be rename or removed by
  •   the owner of the file
  •   the owner of the directory
  •   the super user
  1. sticky bit on executables
  •  prevents the system from removing the program image from the swap space when the last user terminats the executable, this helps in saving time (as the image text is already in the swap space) when the next user runs the executable.
  1. Hard Links(ln file1 hlink1) and Soft links(ln -s file2 slink2)(symbolic links, sym links)
  •  soft link containes the complete path to the file(like windows shortcut)
  •  deleting soft link will not have any effect on the file, but deleting the actual file will make the link useless
  •  softlink can be linked to directories
  •  inode are different for the softlinks
  •  hard links are the links to the inode(the metadata about file), and the link count in the inode represent the number of hard liks to the inode.
  •  hard link to directories cant be created
  •  the original file data will only be deleted only when the hard link count on the inode is zero
  •  inodes are same for the hard links


No comments: