《操作系统》的实验代码。
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.

129 lines
5.8 KiB

12 years ago
  1. #ifndef __KERN_FS_SFS_SFS_H__
  2. #define __KERN_FS_SFS_SFS_H__
  3. #include <defs.h>
  4. #include <mmu.h>
  5. #include <list.h>
  6. #include <sem.h>
  7. #include <unistd.h>
  8. /*
  9. * Simple FS (SFS) definitions visible to ucore. This covers the on-disk format
  10. * and is used by tools that work on SFS volumes, such as mksfs.
  11. */
  12. #define SFS_MAGIC 0x2f8dbe2a /* magic number for sfs */
  13. #define SFS_BLKSIZE PGSIZE /* size of block */
  14. #define SFS_NDIRECT 12 /* # of direct blocks in inode */
  15. #define SFS_MAX_INFO_LEN 31 /* max length of infomation */
  16. #define SFS_MAX_FNAME_LEN FS_MAX_FNAME_LEN /* max length of filename */
  17. #define SFS_MAX_FILE_SIZE (1024UL * 1024 * 128) /* max file size (128M) */
  18. #define SFS_BLKN_SUPER 0 /* block the superblock lives in */
  19. #define SFS_BLKN_ROOT 1 /* location of the root dir inode */
  20. #define SFS_BLKN_FREEMAP 2 /* 1st block of the freemap */
  21. /* # of bits in a block */
  22. #define SFS_BLKBITS (SFS_BLKSIZE * CHAR_BIT)
  23. /* # of entries in a block */
  24. #define SFS_BLK_NENTRY (SFS_BLKSIZE / sizeof(uint32_t))
  25. /* file types */
  26. #define SFS_TYPE_INVAL 0 /* Should not appear on disk */
  27. #define SFS_TYPE_FILE 1
  28. #define SFS_TYPE_DIR 2
  29. #define SFS_TYPE_LINK 3
  30. /*
  31. * On-disk superblock
  32. */
  33. struct sfs_super {
  34. uint32_t magic; /* magic number, should be SFS_MAGIC */
  35. uint32_t blocks; /* # of blocks in fs */
  36. uint32_t unused_blocks; /* # of unused blocks in fs */
  37. char info[SFS_MAX_INFO_LEN + 1]; /* infomation for sfs */
  38. };
  39. /* inode (on disk) */
  40. struct sfs_disk_inode {
  41. uint32_t size; /* size of the file (in bytes) */
  42. uint16_t type; /* one of SYS_TYPE_* above */
  43. uint16_t nlinks; /* # of hard links to this file */
  44. uint32_t blocks; /* # of blocks */
  45. uint32_t direct[SFS_NDIRECT]; /* direct blocks */
  46. uint32_t indirect; /* indirect blocks */
  47. // uint32_t db_indirect; /* double indirect blocks */
  48. // unused
  49. };
  50. /* file entry (on disk) */
  51. struct sfs_disk_entry {
  52. uint32_t ino; /* inode number */
  53. char name[SFS_MAX_FNAME_LEN + 1]; /* file name */
  54. };
  55. #define sfs_dentry_size \
  56. sizeof(((struct sfs_disk_entry *)0)->name)
  57. /* inode for sfs */
  58. struct sfs_inode {
  59. struct sfs_disk_inode *din; /* on-disk inode */
  60. uint32_t ino; /* inode number */
  61. bool dirty; /* true if inode modified */
  62. int reclaim_count; /* kill inode if it hits zero */
  63. semaphore_t sem; /* semaphore for din */
  64. list_entry_t inode_link; /* entry for linked-list in sfs_fs */
  65. list_entry_t hash_link; /* entry for hash linked-list in sfs_fs */
  66. };
  67. #define le2sin(le, member) \
  68. to_struct((le), struct sfs_inode, member)
  69. /* filesystem for sfs */
  70. struct sfs_fs {
  71. struct sfs_super super; /* on-disk superblock */
  72. struct device *dev; /* device mounted on */
  73. struct bitmap *freemap; /* blocks in use are mared 0 */
  74. bool super_dirty; /* true if super/freemap modified */
  75. void *sfs_buffer; /* buffer for non-block aligned io */
  76. semaphore_t fs_sem; /* semaphore for fs */
  77. semaphore_t io_sem; /* semaphore for io */
  78. semaphore_t mutex_sem; /* semaphore for link/unlink and rename */
  79. list_entry_t inode_list; /* inode linked-list */
  80. list_entry_t *hash_list; /* inode hash linked-list */
  81. };
  82. /* hash for sfs */
  83. #define SFS_HLIST_SHIFT 10
  84. #define SFS_HLIST_SIZE (1 << SFS_HLIST_SHIFT)
  85. #define sin_hashfn(x) (hash32(x, SFS_HLIST_SHIFT))
  86. /* size of freemap (in bits) */
  87. #define sfs_freemap_bits(super) ROUNDUP((super)->blocks, SFS_BLKBITS)
  88. /* size of freemap (in blocks) */
  89. #define sfs_freemap_blocks(super) ROUNDUP_DIV((super)->blocks, SFS_BLKBITS)
  90. struct fs;
  91. struct inode;
  92. void sfs_init(void);
  93. int sfs_mount(const char *devname);
  94. void lock_sfs_fs(struct sfs_fs *sfs);
  95. void lock_sfs_io(struct sfs_fs *sfs);
  96. void unlock_sfs_fs(struct sfs_fs *sfs);
  97. void unlock_sfs_io(struct sfs_fs *sfs);
  98. int sfs_rblock(struct sfs_fs *sfs, void *buf, uint32_t blkno, uint32_t nblks);
  99. int sfs_wblock(struct sfs_fs *sfs, void *buf, uint32_t blkno, uint32_t nblks);
  100. int sfs_rbuf(struct sfs_fs *sfs, void *buf, size_t len, uint32_t blkno, off_t offset);
  101. int sfs_wbuf(struct sfs_fs *sfs, void *buf, size_t len, uint32_t blkno, off_t offset);
  102. int sfs_sync_super(struct sfs_fs *sfs);
  103. int sfs_sync_freemap(struct sfs_fs *sfs);
  104. int sfs_clear_block(struct sfs_fs *sfs, uint32_t blkno, uint32_t nblks);
  105. int sfs_load_inode(struct sfs_fs *sfs, struct inode **node_store, uint32_t ino);
  106. #endif /* !__KERN_FS_SFS_SFS_H__ */