28
Operating Systems Practical Session 10 File Systems 1

Operating Systems Practical Session 10 File Systems 1

Embed Size (px)

Citation preview

Page 1: Operating Systems Practical Session 10 File Systems 1

Operating Systems

Practical Session 10 File Systems

1

Page 2: Operating Systems Practical Session 10 File Systems 1

Quick recap

• Files are an abstraction mechanism• Several file types: User files (regular),Directory

files, Special files (Block, Char)• Access: sequentially (e.g. tapes) or random

access (disk)• Data: structured (records) or unstructured (set

of bits and bytes)

2

Page 3: Operating Systems Practical Session 10 File Systems 1

File system layout (Tanenbaum)

3

Page 4: Operating Systems Practical Session 10 File Systems 1

Quick recap: Index-Nodes (i-nodes)

• The superblock object represents the entire file system.

• Each i-node is a data structure containing pointers to the disk blocks that contain the actual file contents.

• An i-node corresponds to a single file.• An i-node needs to be in the main memory only if

the correspondent file is open.• Besides the data blocks pointers, the i-node also

contains information on the file permissions, owner, etc

4

Page 5: Operating Systems Practical Session 10 File Systems 1

Quick recap: i-Nodes

General file attributes

The number of hard-links to the file

Usually between 10

and 12

File Size

HardLink count

5

Page 6: Operating Systems Practical Session 10 File Systems 1

Quick recap: FAT

• A FAT (File allocation table) contains the chains of disk blocks. Each chain defines a different file.

• Every entry in the FAT points to the next disk block of the file.

• Chains of blocks are terminated by a special marker – end_of_file character.

• Directory entry points to first block in a file (i.e. specifies the block number).

• The FAT is held in Main Memory and its size is proportional to the disk size.

Page 7: Operating Systems Practical Session 10 File Systems 1

Recap: MS-DOS directory entry• Tree structure (no links)• directories provide information about location of file blocks (directly or

indirectly..)• Both names and attributes are IN the directory

MS-DOS uses fixed size 32-byte directory entries

• read-only• hidden• system• archive

An index into the 64K –entry FAT

7

Page 8: Operating Systems Practical Session 10 File Systems 1

NTFS• Each file is represented by a record in a special file called

the master file table (MFT).• Directories are also files.• The first 16 records of the table are reserved for special

information and the first record of this table describes the master file table itself, followed by a MFT mirror record.

• The seventeenth and following records of the master file table are for user files and directories.

• The master file table allocates a certain amount of space for each file record (for attributes and initial data).

• Small files can be entirely contained within the master file table record in this case file access is very fast.

Page 9: Operating Systems Practical Session 10 File Systems 1

The MFT record of a long file

A file that requires three MFT records to store its runs

Page 10: Operating Systems Practical Session 10 File Systems 1

NFS – Network File System

• Allows sharing of file Systems.• A server exports part of its file system.• A client can mount an exported file system.• A stateless protocol. The server remembers

nothing about previous requests from a client.

Page 11: Operating Systems Practical Session 10 File Systems 1

Question 1: i-nodesHow many time will the disk be accessed when a user executes the following command:more /usr/tmp/a.txt

Assume that: 1. The size of 'a.txt' is 1 block. 2. The i-node of the root directory is not in the

memory. 3. Entries 'usr', 'tmp' and 'a.txt' are all located in

the first block of their directories.12

Page 12: Operating Systems Practical Session 10 File Systems 1

Question 1: i-nodes

Accessing each directory requires at least 2 disk accesses: reading the i-node and the first block. In our case the entry we are looking for is always in the first block so we need exactly 2 disk accesses. According to assumption 2 the root directory's i-node is located on the disk so we need 6 disk accesses (3 directories) until we reach a.txt's i-node index. Since "more" displays the file's content, for a.txt we need its i-node + all the blocks of the file (1 block, according to assumption). Total disk accesses: 6 + 2 = 8.

13

Page 13: Operating Systems Practical Session 10 File Systems 1

Question 1: i-nodesA similar problem

14

Page 14: Operating Systems Practical Session 10 File Systems 1

Question 2: FAT

A FAT starts with the following values:

Assuming that slots with -1 indicates an end of file and slots with 0 indicates a free slot:1. If the directory containing a certain file has 7 as

the starting block, how many blocks does that file contain?

2. The owner of the file has written two more blocks to the file. Adjust the FAT to the change.

Page 15: Operating Systems Practical Session 10 File Systems 1

Question 2: FAT

1. 42. 14 , 19 , 8 , -1 , -1 , 9 , 3 , 2 , 5 , 10 , -1

0 X1 X2 EOF3 134 25 96 87 Free8 Y9 12

10 311 FREE12 EOF13 EOF14 FREE15 BAD

Not allocated

End of file

Marked as a bad block

6 8 4 2

5 9 12

10 3 13

File A:

File B:

File C:

Page 16: Operating Systems Practical Session 10 File Systems 1

Question 3: i-nodes

The Ofer2000 Operating Systems, based on UNIX, provides the following system call:rename(char *old, char *new)This call changes a file’s name from ‘old’ to ‘new’. What is the difference between using this call, and just copying ‘old’ to a new file, ‘new’, followed by deleting ‘old’? Answer in terms of disk access and allocation.

17

Page 17: Operating Systems Practical Session 10 File Systems 1

Question 3: i-nodes

• rename - simply changes the file name in the entry of its directory.

• copy - will allocate a new i-node and the blocks for the new file, and copy the contents of the old file blocks to the new ones.

• delete - will release the i-node and blocks of the old file. • copy + delete - is a much more complicated operation for

the Operating System, note that you will not be able to execute it if you do not have enough free blocks or i-nodes left on your disk.

18

Page 18: Operating Systems Practical Session 10 File Systems 1

Question 4: i-nodes

What would be the maximal size of a file in a UNIX system with an address size of 32 bits if :

1. The block size is 1K2. The block size is 4K

(The i-node has 10 direct block entries, one single, double & triple indirect)

19

Page 19: Operating Systems Practical Session 10 File Systems 1

Question 4: i-nodes

1. Block size: 1K– Direct: 10·1K– Single indirect: each address is 32 bit = 4 byte then

we have 256 pointers to blocks of size 1K (i.e. 256·1K)

– The same idea is applied for double and triple indirect.

In total: 10·1K+256·1K+256·256·1K+256·256·256·1K

20

Page 20: Operating Systems Practical Session 10 File Systems 1

Question 4: i-nodes

1. Block size: 4K– Direct: 10·4K– Single indirect: each address is 32 bit = 4 byte then

we have 1024 pointers to blocks of size 4K (i.e. 1024·4K)

– The same idea is applied for double and triple indirect

In total: 10·4K+1024·4K+1024·1024·4K+1024·1024·1024·4K

21

Page 21: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a)

בשאלה זו עליכם להציע שינויים )במבני הנתונים על מנת MS-DOSובקוד( של מערכת הקבצים של

לאחר השינוי, תתמוך . linksלהוסיף לה תמיכה ב-MS-DOS:בשתי הפקודות החדשות הבאות

הפקודה:– soft-link old-path new-path

.old-path אל new-path חדש מ-soft link יוצרת הפקודה:– hard-link old-path new-path

.old-path אל new-path חדש מ-hard link יוצרת

Page 22: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a) MS-DOS נקודות( אנו רוצים להוסיף למערכת הקבצים של 13)א.

מבלי לבצע שינוי כלשהוא בטבלת , soft linksתמיכה ב-. ומבלי להוסיף מבני נתונים חדשיםFATה-

)a תארו בפירוט אילו מבני נתונים שלMS-DOS.ישתנו וכיצד

, המציין directory entry ב-flagהשינוי היחיד הנדרש הינו תוספת או לא. symbolic link הינו entryהאם הקובץ המוצבע ע"י ה-

הינו entry מורם, תוכנו של הקובץ ששמו מופיע ב-flagבמידה וה-(.old-path )כלומר, linkה-

Page 23: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a))bכתבו פסאודו-קוד המתאר את פעולתה של פרוצדורה

soft-link(old-path, new-path) .soft-linkהמממשת את הפקודה

Procedure soft-link(old-path, new-path) } // get directory-name and entry-name Convert new-path to <dirName, entryName> // allocate a new directory entry in directory dirName new-entry ← allocate-new-entry(dirName) new-entry.filename ← entryName new-entry.link ← true // indicate that this is a symbolic link allocate a single block, initialize with old-path,

set it as new-entry's single block new-entry.data ← old-path {

Page 24: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a)c האם נדרשים שינויים נוספים בקוד של )MS-DOS.אם כן, ציינו אותם בקצרה ?

שבו )אם ישנם directories עוברים על ה-pathname ל-parsingכאשר מבצעים )בתלות current directory או ה-root directoryכאלה( על פי הסדר, החל מן ה

בו נתקלים, יש לבדוק name(. עבור כל שם relative path או absoluteאם זהו שהוספנו. אם כן, יש לקרוא את תוכן flag, ע"י בדיקת ה-linkאם הוא מיצג

, ולהמשיך את פעולת link ב-name, להחליף את link למשתנה nameהקובץ .link החל מתחילת parsingה-

. יש למנוע לולאה link ל-link. מרשה הצבעות של bהמימוש שלנו בסעיף , למשל ע"י ספירה של מספר הפעמים בהן parsingאינסופית במהלך פעולת ה-

ויציאה עם הודעת שגיאה מתאימה במידה והמונה מגיע linkהתהליך נתקל ב-לערך סף שנקבע מראש. אפשרות נוספת היא למנוע מראש הצבעות בין

לינקים.

.false ל-linkכאשר יוצרים קובץ רגיל, יש לאתחל את דגל

Page 25: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a) תמיכה MS-DOS נקודות( כעת נוסיף למערכת הקבצים של 12)ב.)הניחו כי FATמבלי לבצע שינוי כלשהוא בטבלת ה-, hard linksב-

ניתן להיעזר במבני נתונים נוספים(.a תארו בפירוט אילו מבני נתונים של )MS-DOS ישתנו וכיצד, ואילו

מבני נתונים יתווספו.

. Unix ב-inodesנוסיף טבלה הדומה מבחינות מסוימות לטבלת ה- directory. שינוי נוסף הוא במבנה של dnodesנקרא לה טבלת

entries כל .directory entry-יצביע לכניסה בטבלת ה dnodes במקום להצביע לבלוק הראשון של הקובץ. בכל כניסה כזו יהיו שני

שדות. השדה הראשון מונה את מספר ההצבעות לרשומת FATה- והשדה השני מצביע לבלוק הראשון של הקובץ. dnodeה-

.נשאר ללא שינוי

Page 26: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a)

Page 27: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a)b) כתבו פסאודו-קוד המתאר אל פעולתה של פרוצדורהhard-link(old-path, new-path) ,

.hard-linkהמממשת את הפקודה

Procedure hard-link(old-path, new-path) } // get directory-name and entry-name Convert new-path to <dname, ename> // allocate a new directory entry in directory dname new-entry ← allocate-new-entry(dname) new-entry.filename ← ename // locate old-path dnote entry dnode-entry-num ← locate-dnote-entry(old-path) // point from directory entry to dnode entry new-entry.dnode ← dnode-entry-num // Increment links number dnodes[dnodes-entry-num].links++ {

Page 28: Operating Systems Practical Session 10 File Systems 1

Question 5: FAT (2007a)

c) האם נדרשים שינויים נוספים בקוד שלMS-DOS ?.אם כן, ציינו אותם בקצרה

מובן כי כעת כל הפרוצדורות הפועלות על קבצים נדרשות לעבוד עם מבני הנתונים החדשים.

כאשר נוצר קובץ חדש, יש לאתחל את מספר . כאשר מוחקים 1 החדש ל-dnodeהלינקים של ה-

קובץ יש להפחית את מספר הלינקים של שלו באחד. רק אם מספר זה מתאפס, dnodeה-

יש למחוק את הקובץ עצמו.