《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.6 KiB

  1. Use this tool, vsfs.py, to study how file system state changes as various
  2. operations take place. The file system begins in an empty state, with just a
  3. root directory. As the simulation takes place, various operations are
  4. performed, thus slowly changing the on-disk state of the file system.
  5. The possible operations are:
  6. - mkdir() - creates a new directory
  7. - creat() - creates a new (empty) file
  8. - open(), write(), close() - appends a block to a file
  9. - link() - creates a hard link to a file
  10. - unlink() - unlinks a file (removing it if linkcnt==0)
  11. To understand how this homework functions, you must first understand how the
  12. on-disk state of this file system is represented. The state of the file
  13. system is shown by printing the contents of four different data structures:
  14. inode bitmap - indicates which inodes are allocated
  15. inodes - table of inodes and their contents
  16. data bitmap - indicates which data blocks are allocated
  17. data - indicates contents of data blocks
  18. The bitmaps should be fairly straightforward to understand, with a 1
  19. indicating that the corresponding inode or data block is allocated, and a 0
  20. indicating said inode or data block is free.
  21. The inodes each have three fields: the first field indicates the type of file
  22. (e.g., f for a regular file, d for a directory); the second indicates which
  23. data block belongs to a file (here, files can only be empty, which would have
  24. the address of the data block set to -1, or one block in size, which would
  25. have a non-negative address); the third shows the reference count for the file
  26. or directory. For example, the following inode is a regular file, which is
  27. empty (address field set to -1), and has just one link in the file system:
  28. [f a:-1 r:1]
  29. If the same file had a block allocated to it (say block 10), it would be shown
  30. as follows:
  31. [f a:10 r:1]
  32. If someone then created a hard link to this inode, it would then become:
  33. [f a:10 r:2]
  34. Finally, data blocks can either retain user data or directory data. If filled
  35. with directory data, each entry within the block is of the form (name,
  36. inumber), where "name" is the name of the file or directory, and "inumber" is
  37. the inode number of the file. Thus, an empty root directory looks like this,
  38. assuming the root inode is 0:
  39. [(.,0) (..,0)]
  40. If we add a single file "f" to the root directory, which has been allocated
  41. inode number 1, the root directory contents would then become:
  42. [(.,0) (..,0) (f,1)]
  43. If a data block contains user data, it is shown as just a single character
  44. within the block, e.g., "h". If it is empty and unallocated, just a pair of
  45. empty brackets ([]) are shown.
  46. An entire file system is thus depicted as follows:
  47. inode bitmap 11110000
  48. inodes [d a:0 r:6] [f a:1 r:1] [f a:-1 r:1] [d a:2 r:2] [] ...
  49. data bitmap 11100000
  50. data [(.,0) (..,0) (y,1) (z,2) (f,3)] [u] [(.,3) (..,0)] [] ...
  51. This file system has eight inodes and eight data blocks. The root directory
  52. contains three entries (other than "." and ".."), to "y", "z", and "f". By
  53. looking up inode 1, we can see that "y" is a regular file (type f), with a
  54. single data block allocated to it (address 1). In that data block 1 are the
  55. contents of the file "y": namely, "u". We can also see that "z" is an empty
  56. regular file (address field set to -1), and that "f" (inode number 3) is a
  57. directory, also empty. You can also see from the bitmaps that the first four
  58. inode bitmap entries are marked as allocated, as well as the first three data
  59. bitmap entries.
  60. The simulator can be run with the following flags:
  61. prompt> vsfs.py -h
  62. Usage: vsfs.py [options]
  63. Options:
  64. -h, --help show this help message and exit
  65. -s SEED, --seed=SEED the random seed
  66. -i NUMINODES, --numInodes=NUMINODES
  67. number of inodes in file system
  68. -d NUMDATA, --numData=NUMDATA
  69. number of data blocks in file system
  70. -n NUMREQUESTS, --numRequests=NUMREQUESTS
  71. number of requests to simulate
  72. -r, --reverse instead of printing state, print ops
  73. -p, --printFinal print the final set of files/dirs
  74. -c, --compute compute answers for me
  75. ]
  76. A typical usage would simply specify a random seed (to generate a different
  77. problem), and the number of requests to simulate. In this default mode, the
  78. simulator prints out the state of the file system at each step, and asks you
  79. which operation must have taken place to take the file system from one state
  80. to another. For example:
  81. prompt> vsfs.py -n 6 -s 16
  82. ...
  83. Initial state
  84. inode bitmap 10000000
  85. inodes [d a:0 r:2] [] [] [] [] [] [] []
  86. data bitmap 10000000
  87. data [(.,0) (..,0)] [] [] [] [] [] [] []
  88. Which operation took place?
  89. inode bitmap 11000000
  90. inodes [d a:0 r:3] [f a:-1 r:1] [] [] [] [] [] []
  91. data bitmap 10000000
  92. data [(.,0) (..,0) (y,1)] [] [] [] [] [] [] []
  93. Which operation took place?
  94. inode bitmap 11000000
  95. inodes [d a:0 r:3] [f a:1 r:1] [] [] [] [] [] []
  96. data bitmap 11000000
  97. data [(.,0) (..,0) (y,1)] [u] [] [] [] [] [] []
  98. Which operation took place?
  99. inode bitmap 11000000
  100. inodes [d a:0 r:4] [f a:1 r:2] [] [] [] [] [] []
  101. data bitmap 11000000
  102. data [(.,0) (..,0) (y,1) (m,1)] [u] [] [] [] [] [] []
  103. Which operation took place?
  104. inode bitmap 11000000
  105. inodes [d a:0 r:4] [f a:1 r:1] [] [] [] [] [] []
  106. data bitmap 11000000
  107. data [(.,0) (..,0) (y,1)] [u] [] [] [] [] [] []
  108. Which operation took place?
  109. inode bitmap 11100000
  110. inodes [d a:0 r:5] [f a:1 r:1] [f a:-1 r:1] [] [] [] [] []
  111. data bitmap 11000000
  112. data [(.,0) (..,0) (y,1) (z,2)] [u] [] [] [] [] [] []
  113. Which operation took place?
  114. inode bitmap 11110000
  115. inodes [d a:0 r:6] [f a:1 r:1] [f a:-1 r:1] [d a:2 r:2] [] ...
  116. data bitmap 11100000
  117. data [(.,0) (..,0) (y,1) (z,2) (f,3)] [u] [(.,3) (..,0)] [] ...
  118. ]
  119. When run in this mode, the simulator just shows a series of states, and asks
  120. what operations caused these transitions to occur. Running with the "-c" flag
  121. shows us the answers. Specifically, file "/y" was created, a single block
  122. appended to it, a hard link from "/m" to "/y" created, "/m" removed via a call
  123. to unlink, the file "/z" created, and the directory "/f" created:
  124. prompt> vsfs.py -n 6 -s 16 -c
  125. ...
  126. Initial state
  127. inode bitmap 10000000
  128. inodes [d a:0 r:2] [] [] [] [] [] [] []
  129. data bitmap 10000000
  130. data [(.,0) (..,0)] [] [] [] [] [] [] []
  131. creat("/y");
  132. inode bitmap 11000000
  133. inodes [d a:0 r:3] [f a:-1 r:1] [] [] [] [] [] []
  134. data bitmap 10000000
  135. data [(.,0) (..,0) (y,1)] [] [] [] [] [] [] []
  136. fd=open("/y", O_WRONLY|O_APPEND); write(fd, buf, BLOCKSIZE); close(fd);
  137. inode bitmap 11000000
  138. inodes [d a:0 r:3] [f a:1 r:1] [] [] [] [] [] []
  139. data bitmap 11000000
  140. data [(.,0) (..,0) (y,1)] [u] [] [] [] [] [] []
  141. link("/y", "/m");
  142. inode bitmap 11000000
  143. inodes [d a:0 r:4] [f a:1 r:2] [] [] [] [] [] []
  144. data bitmap 11000000
  145. data [(.,0) (..,0) (y,1) (m,1)] [u] [] [] [] [] [] []
  146. unlink("/m")
  147. inode bitmap 11000000
  148. inodes [d a:0 r:4] [f a:1 r:1] [] [] [] [] [] []
  149. data bitmap 11000000
  150. data [(.,0) (..,0) (y,1)] [u] [] [] [] [] [] []
  151. creat("/z");
  152. inode bitmap 11100000
  153. inodes [d a:0 r:5] [f a:1 r:1] [f a:-1 r:1] [] [] [] [] []
  154. data bitmap 11000000
  155. data [(.,0) (..,0) (y,1) (z,2)] [u] [] [] [] [] [] []
  156. mkdir("/f");
  157. inode bitmap 11110000
  158. inodes [d a:0 r:6] [f a:1 r:1] [f a:-1 r:1] [d a:2 r:2] [] ...
  159. data bitmap 11100000
  160. data [(.,0) (..,0) (y,1) (z,2) (f,3)] [u] [(.,3) (..,0)] [] ...
  161. ]
  162. You can also run the simulator in "reverse" mode (with the "-r" flag),
  163. printing the operations instead of the states to see if you can predict the
  164. state changes from the given operations:
  165. prompt> ./vsfs.py -n 6 -s 16 -r
  166. Initial state
  167. inode bitmap 10000000
  168. inodes [d a:0 r:2] [] [] [] [] [] [] []
  169. data bitmap 10000000
  170. data [(.,0) (..,0)] [] [] [] [] [] [] []
  171. creat("/y");
  172. State of file system (inode bitmap, inodes, data bitmap, data)?
  173. fd=open("/y", O_WRONLY|O_APPEND); write(fd, buf, BLOCKSIZE); close(fd);
  174. State of file system (inode bitmap, inodes, data bitmap, data)?
  175. link("/y", "/m");
  176. State of file system (inode bitmap, inodes, data bitmap, data)?
  177. unlink("/m")
  178. State of file system (inode bitmap, inodes, data bitmap, data)?
  179. creat("/z");
  180. State of file system (inode bitmap, inodes, data bitmap, data)?
  181. mkdir("/f");
  182. State of file system (inode bitmap, inodes, data bitmap, data)?
  183. ]
  184. A few other flags control various aspects of the simulation, including the
  185. number of inodes ("-i"), the number of data blocks ("-d"), and whether to
  186. print the final list of all directories and files in the file system ("-p").