Courses/Computer Science/CPSC 457.F2014/Lecture Notes/Filesystems

From wiki.ucalgary.ca
< Courses‎ | Computer Science‎ | CPSC 457.F2014‎ | Lecture Notes
Revision as of 19:41, 9 December 2014 by Locasto (talk | contribs) (Created page with "= File Systems = In this session, we will consider what a file system is and what its role is as a data storage and namespace management infrastructure. We will look at how...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

File Systems

In this session, we will consider what a file system is and what its role is as a data storage and namespace management infrastructure.

We will look at how it supports indexing file structure, using FAT, the Unix File System, and the ext2/3/4 file systems as examples.

Notes

Slides

Major Kernel Data Structures supporting Files and File Systems

struct inode

struct super_block

struct file_operations

struct inode_operations

struct super_operations

scribe notes

  • s1
  • s2
  • s3

File Holes Code

(eye@mordor files)$ cat seek.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define ONE_GB  (1 * 1024 * 1024 * 1024)
char* message1 = "I swear by my life and my love of it that I will never live for the sake of another man, ";
char* message2 = "nor ask another man to live for mine.\n";
int main(int argc,
	  char* argv[])
{
  int fd;
  off_t offset = ONE_GB;
  int whence = SEEK_END;
  fd = open("moralcodecastinsteel.txt", O_WRONLY | O_CREAT, S_IRWXU);
  if (-1==fd)
  {
    perror("main");
    return -1;
  }
  write(fd, message1, strlen(message1));
  lseek(fd, offset, whence);
  write(fd, message2, strlen(message2));
  sync(); 
  close(fd);
  return 0;
}
(eye@mordor files)$

Hard and Soft Links Example

The question was: does a hard link use the same inode or a new one? In contrast, what does a soft link require?

(This also demonstrates the use of the stat(1) command for examining file metadata).

(eye@mordor files)$ cd
(eye@mordor ~)$ man ln
(eye@mordor ~)$ ln /home/eye/457/lectures/files/seek.c s.c
(eye@mordor ~)$ ll -h
total 236K
drwxr-xr-x. 35 eye  cpsc   4.0K Nov 18 12:45 ./
drwxr-xr-x.  4 root root   4.0K Oct  8 21:00 ../
drwxrwxr-x.  9 eye  sauron 4.0K Oct 23 13:44 457/
... 
-rw-r--r--.  2 eye  sauron  741 Nov 18 11:32 s.c
... 
(eye@mordor ~)$ more s.c
...
(eye@mordor ~)$ stat s.c
  File: `s.c'
  Size: 741       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 438978      Links: 2
Access: (0644/-rw-r--r--)  Uid: (  500/     eye)   Gid: (  502/  sauron)
Access: 2013-11-18 12:45:55.077096047 -0700
Modify: 2013-11-18 11:32:50.860726198 -0700
Change: 2013-11-18 12:45:41.631749590 -0700
(eye@mordor ~)$ stat seek-soft.c 
  File: `seek-soft.c' -> `/home/eye/457/lectures/files/seek.c'
  Size: 35        	Blocks: 0          IO Block: 4096   symbolic link
Device: 803h/2051d	Inode: 438878      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (  500/     eye)   Gid: (  502/  sauron)
Access: 2013-11-18 12:46:40.482265607 -0700
Modify: 2013-11-18 12:46:36.621166153 -0700
Change: 2013-11-18 12:46:36.621166153 -0700
(eye@mordor files)$ stat /home/eye/457/lectures/files/seek.c
  File: `/home/eye/457/lectures/files/seek.c'
  Size: 741       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 438978      Links: 2
Access: (0644/-rw-r--r--)  Uid: (  500/     eye)   Gid: (  502/  sauron)
Access: 2013-11-18 12:45:55.077096047 -0700
Modify: 2013-11-18 11:32:50.860726198 -0700
Change: 2013-11-18 12:45:41.631749590 -0700
(eye@mordor ~)$

Note how the soft link has its own (new) inode, and how the link count for 'seek.c' has been increased by 1 by virtue of the hard link at ~/s.c

Trivia

http://pages.cpsc.ucalgary.ca/~locasto/teaching/2012/CPSC457/code/jailed.txt

  • you need to copy at least the dynamically linked libraries that the app needs; you can use the ldd utility to list them
  • or you can statically compile and link your program so it brings what it needs.
  • programs may also depend on other utilities in /usr/bin/ or /bin (i.e., they may fork off a process to accomplish some task)

http://pages.cpsc.ucalgary.ca/~locasto/teaching/2012/CPSC457/code/escapejail.txt

  • note that using a symbolic link to get out doesn't work, but a hard link does; however:
  • this link needs to be set up beforehand -- you can't create such a link once chroot'd

script demonstrating semantics of Directory Permissions

Scribe Notes

Reading

  • MOS, Chapter 4.3: File System Implementation
  • MOS, Chapter 4.5: Example File Systems
  • MOS, Chapter 10.6: The Linux File System

Optional Reading

  • (if you have it) ULK, Chapter 1: SS: An Overview of the Unix Filesystem (pp 12---19)