Syllabus   Blank Homework   Quizzes  
Notes   Labs   Scores   Blank

Lecture Notes
Dr. Tong Lai Yu, 2010

  1. Introduction
  2. Processes
  3. Inter Process Communication
  4. Deadlocks
  5. Memory Management
 
  1. File Systems
  2. Protection and Security
  3. I/O Systems


To fight a disease after it has occurred is like trying to dig a well
when one is thirsty or forging a weapon once a war has begun.

						Chinese Medicine

File Systems

  1. Storage Systems

    Disks

    Disk Interleaving

    Tapes

  2. Files
     
  3. A file is an abstraction mechanism that provides a way to store information on a disk and read it back
    Operations:
      create, delete, open, close, read, write, append, seek, ..
     
  4. File Structure
    Three kinds of files. (a) Byte Sequence (b) Record Sequence (c) Tree
     
  5. File Types
    (a) An Executable File   (b) An archive
     
  6. An xv6 example program using file-system calls
      //copyFile.c
      #include "types.h"
      #include "stat.h"
      #include "user.h"
      #include "fcntl.h"
      
      char buf[512];
      
      int
      main(int argc, char *argv[])
      {
        int fd0, fd1, n, n1;
      
        if(argc <= 2){
          printf(1, "Need 2 arguments\n");
          exit();
        }
      
        // Open output file and create the output file
        if((fd0 = open(argv[1], O_RDONLY)) < 0){
            printf(1, "copyFile: cannot open %s\n", argv[1]);
            exit();
        }
        if((fd1 = open(argv[2], O_CREATE|O_RDWR)) < 0){
            printf(1, "copyFile: cannot create %s\n", argv[2]);
            exit();
        }
        while ( ( n = read(fd0, buf, sizeof(buf))) > 0 ) {
          n1 = write ( fd1, buf, n );   // write data
          if ( n1 <= 0 )                // write error
             exit();
        }
        close(fd0);
        close(fd1);
        exit();
      }
      
  7. Disk Space Management

    drive #, surface ( side ), the track and the sector

    all the tracks on one drive that can be accessed without being moved constitute a cylinder

    within a track, data are stored in sectors
    in general,

    a sector is 512 bytes
    4 - 32 sectors / track
    20 - 1500 tracks/disk surface
    block ~ one or more sectors
    Let s = # of sectors / track
    t = # of tracks / cylinder
    cylinder i, surface j, sector k (all start from 0)
  8. number of sectors in a cylinder = s x t
  9. starting sector number in i-th cylinders = i x s x t = s x i x t
  10. number of sectors in j surfaces = s x j
  11. if only one sector / block,

    block number b = k + s x ( j + i x t )

    For examples, s = 4, and t = 16:

    (i, j, k)   b  
    (0, 1, 1 ) 5
    (1, 12, 3 )113

    File Control Block ( FCB ) -- storage structure consisting of information about a file

    Typical File Control Block
    file permissions
    file dates ( create, access, write )
    file owner, group, ACL ( access control list )
    file size
    file data blocks

    Allocation Methods -- an allocation method refers to how disk ` blocks are allocated for files:

  12. Contiguous Allocation
  13. Linked Allocation ( e.g. FAT )
  14. Indexed Allocation

    Studies of UNIX environment shows that median file size is about 1K

    if block size too large ( e.g. 32K ) => wasteful
    if too small => a file consists of many blocks
          => data transfer too slow

    in general, block size ~ 512, 1K, or 2K

    Contiguous Allocation

    • Reduce seek time for contiguous access
    • Access is easy
    • Supports both sequential and direct access
    • Dynamic allocation problem ( may use first fit, best fit, worst fit .. )
    • External fragmentation -- need compaction : expensive
    • Widely used in CD-ROMs, DVDs where final file sizes are known in advance and won't change

    Linked Allocation

    • Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk
    • simple -- need only starting address
    • free space management system -- no waste space
    • no external fragmentation
    • easy to create a file ( no allocation problem )
    • only better for sequential access
    • overhead -- pointers require space
    • reliability -- damaged pointers

      File Allocation Table (FAT) is an important variation of Linked Allocation.
    • A section of a disk partition is set aside to contain the table.
    • Each block has one entry in the table and is indexed by block number.

    Indexed Allocation

    Bring all the pointers together into one location: the index block ( one for each file )

    • direct access
    • no external fragmentation
    • easy to create a file ( no allocation problem )
    • support both sequential and direct access
    • overhead: space for index blocks

    For Large File:
    Block 19, 24 and 8 are index blocks.

    Free-Space Management

    The system maintains a free-space list (not necessary a physical list) to keep track of all the free disk blocks.

    linked list, grouping, bit map, counting

    • Linked List
      link all free blocks, keep a pointer pointing to the first free list block in a special location on the disk and caching it in memory;

    • Grouping
      store the addresses of n free blocks in the first free block; the final block contains the addresses of another n free blocks, and so on

      16-bit block #

      1K block can have 512 16-bit entries

      Thus, it can hold 512 free block #

      Therefore, 128 M disk requires 256 blocks to hold all 128K disk block#

    • Bit Map

      each block is represented by 1 bit

      1 => block free
      0 => block allocated
      e.g. blocks 2, 3, 4, 5, 8, 9,10, 11, 12, 13, 17, 18, 25, 26, 27 are free, others are allocated 00111100111111000110000001110000

      A disk with n blocks requires n bits
      128 M disk with 1K blocks requires 128K bits or 16K bytes ( i.e. 16 blocks )

    • Counting
      makes use of the clustering properties of file blocks: (first free block, number of free blocks in the cluster)
      (1, 3), (7, 3), (12, 1), (14, 5)
  15. Storage Systems Examples

    File Allocation Table ( FAT ) -- MSDOS, OS/2

    UNIX

  16. Directory Structures

  17. The directory entry provides the information needed to find the disk blocks;
  18. it maps the the ASCII name of the file onto the information needed to locate the data
  19. CP/M

    only one directory ( thus only need to search one directory ), which contains fixed-size ( 32 bytes ) entries
    when the OS finds the entry, it also has the disk block numbers,
    if the file uses more disk blocks than fit in one entry ( max four disk block numbers; each 16-bit ), the file is allocated additional entry

    1 8 3 1 2 1 16
    owner
    id
    file name file type
    (extension)
    extent
    (multi dir)
    reserved block
    count
    disk block numbers

    block count -- tells how many bytes this file entry contains, measured in 128 bytes

    Hierarchical Directory

    MS-DOS


    Directory entry: 32 bytes

    8 3 1 10 2 2 2 4
    filename extension attr-
    ibutes
    reserved time of
    last
    change
    date of
    last change
    first
    block #
    size

    A directory is just a file, so can have many

    contains filename and first block number ( cluster # ), indexed to FAT

    early UNIX

  20. a UNIX dirctory entry contains one entry for each file in that directory
  21. very simple
  22. 16 bytes -- 14 bytes for filename, 2 bytes for inode
    2 14
    I-node
    number
    file name

    An i-node in UNIX is located at a fixed space on the disk.

    Example

    Looking up /usr/ast/mbox
  23. The file system first locates the root directory i-node.
  24. From this i-node, it locates the root directory.
  25. From root directory, it looks up the first component of path, usr, to find its i-node number.
  26. ....
  27. See also Disk Scheduling

    Class Exercises

    1. Explain the benefits of using a contiguous file allocation scheme.
        Locating file data is straightforward. Also, files can be accessed quickly, because the storage does not need to perform lengthy seeks after it locates the first block.
    2. Explain the disadvantages of using a contiguous file allocation scheme.
        They can lead to a significant external fragmentation and poor performance when a file grows tool large to be stored contiguously at the current location and must be moved.
    3. How do user classes reduce the storage overhead by access control information?
        User classes enable the owner to grant permissions to a group of users using one entity.
    4. Discuss the advantages and disadvantages of storing access control data as part of the file control block.
        Advantage: There is little overhead if the user is granted access to a file, because the file system needs the file control block before opening the file anyway. Disadvantage: If access is denied, the system will have wastefully performed several lengthy seeks to access the file control block for a file that the user cannot open.
    5. Why is a single file server impractical for environment such as a large organizations?
        Ans. If the server fails, the whole file system becomes unavailable. A central server can also become a bottleneck.
    6. Why are physical safeguards insufficient for preventing loss of data in the event of disaster?
        Physical safeguards prevent access to data but do not prevent loss of data due to natural disasters, such as fires and earthquakes, or to hardware and power failure.