msdos_fs.h
上传用户:szlgq88
上传日期:2009-04-28
资源大小:48287k
文件大小:14k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #ifndef _LINUX_MSDOS_FS_H
  2. #define _LINUX_MSDOS_FS_H
  3. /*
  4.  * The MS-DOS filesystem constants/structures
  5.  */
  6. #include <asm/byteorder.h>
  7. #define SECTOR_SIZE 512 /* sector size (bytes) */
  8. #define SECTOR_BITS 9 /* log2(SECTOR_SIZE) */
  9. #define MSDOS_DPB (MSDOS_DPS) /* dir entries per block */
  10. #define MSDOS_DPB_BITS 4 /* log2(MSDOS_DPB) */
  11. #define MSDOS_DPS (SECTOR_SIZE / sizeof(struct msdos_dir_entry))
  12. #define MSDOS_DPS_BITS 4 /* log2(MSDOS_DPS) */
  13. #define CF_LE_W(v) le16_to_cpu(v)
  14. #define CF_LE_L(v) le32_to_cpu(v)
  15. #define CT_LE_W(v) cpu_to_le16(v)
  16. #define CT_LE_L(v) cpu_to_le32(v)
  17. #define MSDOS_SUPER_MAGIC 0x4d44 /* MD */
  18. #define MSDOS_ROOT_INO 1 /* == MINIX_ROOT_INO */
  19. #define MSDOS_DIR_BITS 5 /* log2(sizeof(struct msdos_dir_entry)) */
  20. /* directory limit */
  21. #define FAT_MAX_DIR_ENTRIES (65536)
  22. #define FAT_MAX_DIR_SIZE (FAT_MAX_DIR_ENTRIES << MSDOS_DIR_BITS)
  23. #define ATTR_NONE 0 /* no attribute bits */
  24. #define ATTR_RO 1 /* read-only */
  25. #define ATTR_HIDDEN 2 /* hidden */
  26. #define ATTR_SYS 4 /* system */
  27. #define ATTR_VOLUME 8 /* volume label */
  28. #define ATTR_DIR 16 /* directory */
  29. #define ATTR_ARCH 32 /* archived */
  30. /* attribute bits that are copied "as is" */
  31. #define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
  32. /* bits that are used by the Windows 95/Windows NT extended FAT */
  33. #define ATTR_EXT (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
  34. #define CASE_LOWER_BASE 8 /* base is lower case */
  35. #define CASE_LOWER_EXT 16 /* extension is lower case */
  36. #define DELETED_FLAG 0xe5 /* marks file as deleted when in name[0] */
  37. #define IS_FREE(n) (!*(n) || *(n) == DELETED_FLAG)
  38. /* valid file mode bits */
  39. #define MSDOS_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO)
  40. /* Convert attribute bits and a mask to the UNIX mode. */
  41. #define MSDOS_MKMODE(a, m) (m & (a & ATTR_RO ? S_IRUGO|S_IXUGO : S_IRWXUGO))
  42. #define MSDOS_NAME 11 /* maximum name length */
  43. #define MSDOS_LONGNAME 256 /* maximum name length */
  44. #define MSDOS_SLOTS 21 /* max # of slots for short and long names */
  45. #define MSDOS_DOT ".          " /* ".", padded to MSDOS_NAME chars */
  46. #define MSDOS_DOTDOT "..         " /* "..", padded to MSDOS_NAME chars */
  47. /* media of boot sector */
  48. #define FAT_VALID_MEDIA(x) ((0xF8 <= (x) && (x) <= 0xFF) || (x) == 0xF0)
  49. #define FAT_FIRST_ENT(s, x) ((MSDOS_SB(s)->fat_bits == 32 ? 0x0FFFFF00 : 
  50. MSDOS_SB(s)->fat_bits == 16 ? 0xFF00 : 0xF00) | (x))
  51. /* start of data cluster's entry (number of reserved clusters) */
  52. #define FAT_START_ENT 2
  53. /* maximum number of clusters */
  54. #define MAX_FAT12 0xFF4
  55. #define MAX_FAT16 0xFFF4
  56. #define MAX_FAT32 0x0FFFFFF6
  57. #define MAX_FAT(s) (MSDOS_SB(s)->fat_bits == 32 ? MAX_FAT32 : 
  58. MSDOS_SB(s)->fat_bits == 16 ? MAX_FAT16 : MAX_FAT12)
  59. /* bad cluster mark */
  60. #define BAD_FAT12 0xFF7
  61. #define BAD_FAT16 0xFFF7
  62. #define BAD_FAT32 0x0FFFFFF7
  63. /* standard EOF */
  64. #define EOF_FAT12 0xFFF
  65. #define EOF_FAT16 0xFFFF
  66. #define EOF_FAT32 0x0FFFFFFF
  67. #define FAT_ENT_FREE (0)
  68. #define FAT_ENT_BAD (BAD_FAT32)
  69. #define FAT_ENT_EOF (EOF_FAT32)
  70. #define FAT_FSINFO_SIG1 0x41615252
  71. #define FAT_FSINFO_SIG2 0x61417272
  72. #define IS_FSINFO(x) (le32_to_cpu((x)->signature1) == FAT_FSINFO_SIG1 
  73.  && le32_to_cpu((x)->signature2) == FAT_FSINFO_SIG2)
  74. /*
  75.  * ioctl commands
  76.  */
  77. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
  78. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
  79. /* <linux/videotext.h> has used 0x72 ('r') in collision, so skip a few */
  80. #define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32)
  81. #define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, __u32)
  82. /*
  83.  * vfat shortname flags
  84.  */
  85. #define VFAT_SFN_DISPLAY_LOWER 0x0001 /* convert to lowercase for display */
  86. #define VFAT_SFN_DISPLAY_WIN95 0x0002 /* emulate win95 rule for display */
  87. #define VFAT_SFN_DISPLAY_WINNT 0x0004 /* emulate winnt rule for display */
  88. #define VFAT_SFN_CREATE_WIN95 0x0100 /* emulate win95 rule for create */
  89. #define VFAT_SFN_CREATE_WINNT 0x0200 /* emulate winnt rule for create */
  90. struct fat_boot_sector {
  91. __u8 ignored[3]; /* Boot strap short or near jump */
  92. __u8 system_id[8]; /* Name - can be used to special case
  93.    partition manager volumes */
  94. __u8 sector_size[2]; /* bytes per logical sector */
  95. __u8 sec_per_clus; /* sectors/cluster */
  96. __le16 reserved; /* reserved sectors */
  97. __u8 fats; /* number of FATs */
  98. __u8 dir_entries[2]; /* root directory entries */
  99. __u8 sectors[2]; /* number of sectors */
  100. __u8 media; /* media code */
  101. __le16 fat_length; /* sectors/FAT */
  102. __le16 secs_track; /* sectors per track */
  103. __le16 heads; /* number of heads */
  104. __le32 hidden; /* hidden sectors (unused) */
  105. __le32 total_sect; /* number of sectors (if sectors == 0) */
  106. /* The following fields are only used by FAT32 */
  107. __le32 fat32_length; /* sectors/FAT */
  108. __le16 flags; /* bit 8: fat mirroring, low 4: active fat */
  109. __u8 version[2]; /* major, minor filesystem version */
  110. __le32 root_cluster; /* first cluster in root directory */
  111. __le16 info_sector; /* filesystem info sector */
  112. __le16 backup_boot; /* backup boot sector */
  113. __le16 reserved2[6]; /* Unused */
  114. };
  115. struct fat_boot_fsinfo {
  116. __le32   signature1; /* 0x41615252L */
  117. __le32   reserved1[120]; /* Nothing as far as I can tell */
  118. __le32   signature2; /* 0x61417272L */
  119. __le32   free_clusters; /* Free cluster count.  -1 if unknown */
  120. __le32   next_cluster; /* Most recently allocated cluster */
  121. __le32   reserved2[4];
  122. };
  123. struct msdos_dir_entry {
  124. __u8 name[8],ext[3]; /* name and extension */
  125. __u8 attr; /* attribute bits */
  126. __u8    lcase; /* Case for base and extension */
  127. __u8 ctime_cs; /* Creation time, centiseconds (0-199) */
  128. __le16 ctime; /* Creation time */
  129. __le16 cdate; /* Creation date */
  130. __le16 adate; /* Last access date */
  131. __le16 starthi; /* High 16 bits of cluster in FAT32 */
  132. __le16 time,date,start;/* time, date and first cluster */
  133. __le32 size; /* file size (in bytes) */
  134. };
  135. /* Up to 13 characters of the name */
  136. struct msdos_dir_slot {
  137. __u8    id; /* sequence number for slot */
  138. __u8    name0_4[10]; /* first 5 characters in name */
  139. __u8    attr; /* attribute byte */
  140. __u8    reserved; /* always 0 */
  141. __u8    alias_checksum; /* checksum for 8.3 alias */
  142. __u8    name5_10[12]; /* 6 more characters in name */
  143. __le16   start; /* starting cluster number, 0 in long slots */
  144. __u8    name11_12[4]; /* last 2 characters in name */
  145. };
  146. struct fat_slot_info {
  147. loff_t i_pos; /* on-disk position of directory entry */
  148. loff_t slot_off; /* offset for slot or de start */
  149. int nr_slots; /* number of slots + 1(de) in filename */
  150. struct msdos_dir_entry *de;
  151. struct buffer_head *bh;
  152. };
  153. #ifdef __KERNEL__
  154. #include <linux/buffer_head.h>
  155. #include <linux/string.h>
  156. #include <linux/nls.h>
  157. #include <linux/fs.h>
  158. struct fat_mount_options {
  159. uid_t fs_uid;
  160. gid_t fs_gid;
  161. unsigned short fs_fmask;
  162. unsigned short fs_dmask;
  163. unsigned short codepage;  /* Codepage for shortname conversions */
  164. char *iocharset;          /* Charset used for filename input/display */
  165. unsigned short shortname; /* flags for shortname display/create rule */
  166. unsigned char name_check; /* r = relaxed, n = normal, s = strict */
  167. unsigned quiet:1,         /* set = fake successful chmods and chowns */
  168.  showexec:1,      /* set = only set x bit for com/exe/bat */
  169.  sys_immutable:1, /* set = system files are immutable */
  170.  dotsOK:1,        /* set = hidden and system files are named '.filename' */
  171.  isvfat:1,        /* 0=no vfat long filename support, 1=vfat support */
  172.  utf8:1,   /* Use of UTF8 character set (Default) */
  173.  unicode_xlate:1, /* create escape sequences for unhandled Unicode */
  174.  numtail:1,       /* Does first alias have a numeric '~1' type tail? */
  175.  atari:1,         /* Use Atari GEMDOS variation of MS-DOS fs */
  176.  nocase:1;   /* Does this need case conversion? 0=need case conversion*/
  177. };
  178. #define FAT_HASH_BITS 8
  179. #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
  180. #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
  181. /*
  182.  * MS-DOS file system in-core superblock data
  183.  */
  184. struct msdos_sb_info {
  185. unsigned short sec_per_clus; /* sectors/cluster */
  186. unsigned short cluster_bits; /* log2(cluster_size) */
  187. unsigned int cluster_size;   /* cluster size */
  188. unsigned char fats,fat_bits; /* number of FATs, FAT bits (12 or 16) */
  189. unsigned short fat_start;
  190. unsigned long fat_length;    /* FAT start & length (sec.) */
  191. unsigned long dir_start;
  192. unsigned short dir_entries;  /* root dir start & entries */
  193. unsigned long data_start;    /* first data sector */
  194. unsigned long max_cluster;   /* maximum cluster number */
  195. unsigned long root_cluster;  /* first cluster of the root directory */
  196. unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */
  197. struct semaphore fat_lock;
  198. unsigned int prev_free;      /* previously allocated cluster number */
  199. unsigned int free_clusters;  /* -1 if undefined */
  200. struct fat_mount_options options;
  201. struct nls_table *nls_disk;  /* Codepage used on disk */
  202. struct nls_table *nls_io;    /* Charset used for input and display */
  203. void *dir_ops;      /* Opaque; default directory operations */
  204. int dir_per_block;      /* dir entries per block */
  205. int dir_per_block_bits;      /* log2(dir_per_block) */
  206. int fatent_shift;
  207. struct fatent_operations *fatent_ops;
  208. spinlock_t inode_hash_lock;
  209. struct hlist_head inode_hashtable[FAT_HASH_SIZE];
  210. };
  211. #define FAT_CACHE_VALID 0 /* special case for valid cache */
  212. /*
  213.  * MS-DOS file system inode data in memory
  214.  */
  215. struct msdos_inode_info {
  216. spinlock_t cache_lru_lock;
  217. struct list_head cache_lru;
  218. int nr_caches;
  219. /* for avoiding the race between fat_free() and fat_get_cluster() */
  220. unsigned int cache_valid_id;
  221. loff_t mmu_private;
  222. int i_start; /* first cluster or 0 */
  223. int i_logstart; /* logical first cluster */
  224. int i_attrs; /* unused attribute bits */
  225. loff_t i_pos; /* on-disk position of directory entry or 0 */
  226. struct hlist_node i_fat_hash; /* hash by i_location */
  227. struct inode vfs_inode;
  228. };
  229. static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
  230. {
  231. return sb->s_fs_info;
  232. }
  233. static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
  234. {
  235. return container_of(inode, struct msdos_inode_info, vfs_inode);
  236. }
  237. /* Return the FAT attribute byte for this inode */
  238. static inline u8 fat_attr(struct inode *inode)
  239. {
  240. return ((inode->i_mode & S_IWUGO) ? ATTR_NONE : ATTR_RO) |
  241. (S_ISDIR(inode->i_mode) ? ATTR_DIR : ATTR_NONE) |
  242. MSDOS_I(inode)->i_attrs;
  243. }
  244. static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
  245. {
  246. return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
  247. + sbi->data_start;
  248. }
  249. static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
  250. {
  251. #ifdef __BIG_ENDIAN
  252. while (len--) {
  253. *dst++ = src[0] | (src[1] << 8);
  254. src += 2;
  255. }
  256. #else
  257. memcpy(dst, src, len * 2);
  258. #endif
  259. }
  260. static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
  261. {
  262. #ifdef __BIG_ENDIAN
  263. while (len--) {
  264. dst[0] = *src & 0x00FF;
  265. dst[1] = (*src & 0xFF00) >> 8;
  266. dst += 2;
  267. src++;
  268. }
  269. #else
  270. memcpy(dst, src, len * 2);
  271. #endif
  272. }
  273. /* fat/cache.c */
  274. extern void fat_cache_inval_inode(struct inode *inode);
  275. extern int fat_get_cluster(struct inode *inode, int cluster,
  276.    int *fclus, int *dclus);
  277. extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys);
  278. /* fat/dir.c */
  279. extern struct file_operations fat_dir_operations;
  280. extern int fat_search_long(struct inode *inode, const unsigned char *name,
  281.    int name_len, struct fat_slot_info *sinfo);
  282. extern int fat_dir_empty(struct inode *dir);
  283. extern int fat_subdirs(struct inode *dir);
  284. extern int fat_scan(struct inode *dir, const unsigned char *name,
  285.     struct fat_slot_info *sinfo);
  286. extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  287. struct msdos_dir_entry **de, loff_t *i_pos);
  288. extern int fat_alloc_new_dir(struct inode *dir, struct timespec *ts);
  289. extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  290.    struct fat_slot_info *sinfo);
  291. extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
  292. /* fat/fatent.c */
  293. struct fat_entry {
  294. int entry;
  295. union {
  296. u8 *ent12_p[2];
  297. __le16 *ent16_p;
  298. __le32 *ent32_p;
  299. } u;
  300. int nr_bhs;
  301. struct buffer_head *bhs[2];
  302. };
  303. static inline void fatent_init(struct fat_entry *fatent)
  304. {
  305. fatent->nr_bhs = 0;
  306. fatent->entry = 0;
  307. fatent->u.ent32_p = NULL;
  308. fatent->bhs[0] = fatent->bhs[1] = NULL;
  309. }
  310. static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
  311. {
  312. fatent->entry = entry;
  313. fatent->u.ent32_p = NULL;
  314. }
  315. static inline void fatent_brelse(struct fat_entry *fatent)
  316. {
  317. int i;
  318. fatent->u.ent32_p = NULL;
  319. for (i = 0; i < fatent->nr_bhs; i++)
  320. brelse(fatent->bhs[i]);
  321. fatent->nr_bhs = 0;
  322. fatent->bhs[0] = fatent->bhs[1] = NULL;
  323. }
  324. extern void fat_ent_access_init(struct super_block *sb);
  325. extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
  326. int entry);
  327. extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
  328.  int new, int wait);
  329. extern int fat_alloc_clusters(struct inode *inode, int *cluster,
  330.       int nr_cluster);
  331. extern int fat_free_clusters(struct inode *inode, int cluster);
  332. extern int fat_count_free_clusters(struct super_block *sb);
  333. /* fat/file.c */
  334. extern int fat_generic_ioctl(struct inode *inode, struct file *filp,
  335.      unsigned int cmd, unsigned long arg);
  336. extern struct file_operations fat_file_operations;
  337. extern struct inode_operations fat_file_inode_operations;
  338. extern int fat_notify_change(struct dentry * dentry, struct iattr * attr);
  339. extern void fat_truncate(struct inode *inode);
  340. /* fat/inode.c */
  341. extern void fat_attach(struct inode *inode, loff_t i_pos);
  342. extern void fat_detach(struct inode *inode);
  343. extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
  344. extern struct inode *fat_build_inode(struct super_block *sb,
  345. struct msdos_dir_entry *de, loff_t i_pos);
  346. extern int fat_sync_inode(struct inode *inode);
  347. extern int fat_fill_super(struct super_block *sb, void *data, int silent,
  348. struct inode_operations *fs_dir_inode_ops, int isvfat);
  349. /* fat/misc.c */
  350. extern void fat_fs_panic(struct super_block *s, const char *fmt, ...);
  351. extern void fat_clusters_flush(struct super_block *sb);
  352. extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
  353. extern int date_dos2unix(unsigned short time, unsigned short date);
  354. extern void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date);
  355. extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
  356. #endif /* __KERNEL__ */
  357. #endif