msdos_fs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #ifndef _LINUX_MSDOS_FS_H
  2. #define _LINUX_MSDOS_FS_H
  3. #include <linux/magic.h>
  4. /*
  5. * The MS-DOS filesystem constants/structures
  6. */
  7. #include <asm/byteorder.h>
  8. #define SECTOR_SIZE 512 /* sector size (bytes) */
  9. #define SECTOR_BITS 9 /* log2(SECTOR_SIZE) */
  10. #define MSDOS_DPB (MSDOS_DPS) /* dir entries per block */
  11. #define MSDOS_DPB_BITS 4 /* log2(MSDOS_DPB) */
  12. #define MSDOS_DPS (SECTOR_SIZE / sizeof(struct msdos_dir_entry))
  13. #define MSDOS_DPS_BITS 4 /* log2(MSDOS_DPS) */
  14. #define CF_LE_W(v) le16_to_cpu(v)
  15. #define CF_LE_L(v) le32_to_cpu(v)
  16. #define CT_LE_W(v) cpu_to_le16(v)
  17. #define CT_LE_L(v) cpu_to_le32(v)
  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. #include <linux/mutex.h>
  159. struct fat_mount_options {
  160. uid_t fs_uid;
  161. gid_t fs_gid;
  162. unsigned short fs_fmask;
  163. unsigned short fs_dmask;
  164. unsigned short codepage; /* Codepage for shortname conversions */
  165. char *iocharset; /* Charset used for filename input/display */
  166. unsigned short shortname; /* flags for shortname display/create rule */
  167. unsigned char name_check; /* r = relaxed, n = normal, s = strict */
  168. unsigned quiet:1, /* set = fake successful chmods and chowns */
  169. showexec:1, /* set = only set x bit for com/exe/bat */
  170. sys_immutable:1, /* set = system files are immutable */
  171. dotsOK:1, /* set = hidden and system files are named '.filename' */
  172. isvfat:1, /* 0=no vfat long filename support, 1=vfat support */
  173. utf8:1, /* Use of UTF-8 character set (Default) */
  174. unicode_xlate:1, /* create escape sequences for unhandled Unicode */
  175. numtail:1, /* Does first alias have a numeric '~1' type tail? */
  176. atari:1, /* Use Atari GEMDOS variation of MS-DOS fs */
  177. flush:1, /* write things quickly */
  178. nocase:1; /* Does this need case conversion? 0=need case conversion*/
  179. };
  180. #define FAT_HASH_BITS 8
  181. #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
  182. #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
  183. /*
  184. * MS-DOS file system in-core superblock data
  185. */
  186. struct msdos_sb_info {
  187. unsigned short sec_per_clus; /* sectors/cluster */
  188. unsigned short cluster_bits; /* log2(cluster_size) */
  189. unsigned int cluster_size; /* cluster size */
  190. unsigned char fats,fat_bits; /* number of FATs, FAT bits (12 or 16) */
  191. unsigned short fat_start;
  192. unsigned long fat_length; /* FAT start & length (sec.) */
  193. unsigned long dir_start;
  194. unsigned short dir_entries; /* root dir start & entries */
  195. unsigned long data_start; /* first data sector */
  196. unsigned long max_cluster; /* maximum cluster number */
  197. unsigned long root_cluster; /* first cluster of the root directory */
  198. unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */
  199. struct mutex fat_lock;
  200. unsigned int prev_free; /* previously allocated cluster number */
  201. unsigned int free_clusters; /* -1 if undefined */
  202. struct fat_mount_options options;
  203. struct nls_table *nls_disk; /* Codepage used on disk */
  204. struct nls_table *nls_io; /* Charset used for input and display */
  205. const void *dir_ops; /* Opaque; default directory operations */
  206. int dir_per_block; /* dir entries per block */
  207. int dir_per_block_bits; /* log2(dir_per_block) */
  208. int fatent_shift;
  209. struct fatent_operations *fatent_ops;
  210. spinlock_t inode_hash_lock;
  211. struct hlist_head inode_hashtable[FAT_HASH_SIZE];
  212. };
  213. #define FAT_CACHE_VALID 0 /* special case for valid cache */
  214. /*
  215. * MS-DOS file system inode data in memory
  216. */
  217. struct msdos_inode_info {
  218. spinlock_t cache_lru_lock;
  219. struct list_head cache_lru;
  220. int nr_caches;
  221. /* for avoiding the race between fat_free() and fat_get_cluster() */
  222. unsigned int cache_valid_id;
  223. loff_t mmu_private;
  224. int i_start; /* first cluster or 0 */
  225. int i_logstart; /* logical first cluster */
  226. int i_attrs; /* unused attribute bits */
  227. loff_t i_pos; /* on-disk position of directory entry or 0 */
  228. struct hlist_node i_fat_hash; /* hash by i_location */
  229. struct inode vfs_inode;
  230. };
  231. static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
  232. {
  233. return sb->s_fs_info;
  234. }
  235. static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
  236. {
  237. return container_of(inode, struct msdos_inode_info, vfs_inode);
  238. }
  239. /* Return the FAT attribute byte for this inode */
  240. static inline u8 fat_attr(struct inode *inode)
  241. {
  242. return ((inode->i_mode & S_IWUGO) ? ATTR_NONE : ATTR_RO) |
  243. (S_ISDIR(inode->i_mode) ? ATTR_DIR : ATTR_NONE) |
  244. MSDOS_I(inode)->i_attrs;
  245. }
  246. static inline unsigned char fat_checksum(const __u8 *name)
  247. {
  248. unsigned char s = name[0];
  249. s = (s<<7) + (s>>1) + name[1]; s = (s<<7) + (s>>1) + name[2];
  250. s = (s<<7) + (s>>1) + name[3]; s = (s<<7) + (s>>1) + name[4];
  251. s = (s<<7) + (s>>1) + name[5]; s = (s<<7) + (s>>1) + name[6];
  252. s = (s<<7) + (s>>1) + name[7]; s = (s<<7) + (s>>1) + name[8];
  253. s = (s<<7) + (s>>1) + name[9]; s = (s<<7) + (s>>1) + name[10];
  254. return s;
  255. }
  256. static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
  257. {
  258. return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
  259. + sbi->data_start;
  260. }
  261. static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
  262. {
  263. #ifdef __BIG_ENDIAN
  264. while (len--) {
  265. *dst++ = src[0] | (src[1] << 8);
  266. src += 2;
  267. }
  268. #else
  269. memcpy(dst, src, len * 2);
  270. #endif
  271. }
  272. static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
  273. {
  274. #ifdef __BIG_ENDIAN
  275. while (len--) {
  276. dst[0] = *src & 0x00FF;
  277. dst[1] = (*src & 0xFF00) >> 8;
  278. dst += 2;
  279. src++;
  280. }
  281. #else
  282. memcpy(dst, src, len * 2);
  283. #endif
  284. }
  285. /* fat/cache.c */
  286. extern void fat_cache_inval_inode(struct inode *inode);
  287. extern int fat_get_cluster(struct inode *inode, int cluster,
  288. int *fclus, int *dclus);
  289. extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
  290. unsigned long *mapped_blocks);
  291. /* fat/dir.c */
  292. extern const struct file_operations fat_dir_operations;
  293. extern int fat_search_long(struct inode *inode, const unsigned char *name,
  294. int name_len, struct fat_slot_info *sinfo);
  295. extern int fat_dir_empty(struct inode *dir);
  296. extern int fat_subdirs(struct inode *dir);
  297. extern int fat_scan(struct inode *dir, const unsigned char *name,
  298. struct fat_slot_info *sinfo);
  299. extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  300. struct msdos_dir_entry **de, loff_t *i_pos);
  301. extern int fat_alloc_new_dir(struct inode *dir, struct timespec *ts);
  302. extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  303. struct fat_slot_info *sinfo);
  304. extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
  305. /* fat/fatent.c */
  306. struct fat_entry {
  307. int entry;
  308. union {
  309. u8 *ent12_p[2];
  310. __le16 *ent16_p;
  311. __le32 *ent32_p;
  312. } u;
  313. int nr_bhs;
  314. struct buffer_head *bhs[2];
  315. };
  316. static inline void fatent_init(struct fat_entry *fatent)
  317. {
  318. fatent->nr_bhs = 0;
  319. fatent->entry = 0;
  320. fatent->u.ent32_p = NULL;
  321. fatent->bhs[0] = fatent->bhs[1] = NULL;
  322. }
  323. static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
  324. {
  325. fatent->entry = entry;
  326. fatent->u.ent32_p = NULL;
  327. }
  328. static inline void fatent_brelse(struct fat_entry *fatent)
  329. {
  330. int i;
  331. fatent->u.ent32_p = NULL;
  332. for (i = 0; i < fatent->nr_bhs; i++)
  333. brelse(fatent->bhs[i]);
  334. fatent->nr_bhs = 0;
  335. fatent->bhs[0] = fatent->bhs[1] = NULL;
  336. }
  337. extern void fat_ent_access_init(struct super_block *sb);
  338. extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
  339. int entry);
  340. extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
  341. int new, int wait);
  342. extern int fat_alloc_clusters(struct inode *inode, int *cluster,
  343. int nr_cluster);
  344. extern int fat_free_clusters(struct inode *inode, int cluster);
  345. extern int fat_count_free_clusters(struct super_block *sb);
  346. /* fat/file.c */
  347. extern int fat_generic_ioctl(struct inode *inode, struct file *filp,
  348. unsigned int cmd, unsigned long arg);
  349. extern const struct file_operations fat_file_operations;
  350. extern const struct inode_operations fat_file_inode_operations;
  351. extern int fat_notify_change(struct dentry * dentry, struct iattr * attr);
  352. extern void fat_truncate(struct inode *inode);
  353. extern int fat_getattr(struct vfsmount *mnt, struct dentry *dentry,
  354. struct kstat *stat);
  355. /* fat/inode.c */
  356. extern void fat_attach(struct inode *inode, loff_t i_pos);
  357. extern void fat_detach(struct inode *inode);
  358. extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
  359. extern struct inode *fat_build_inode(struct super_block *sb,
  360. struct msdos_dir_entry *de, loff_t i_pos);
  361. extern int fat_sync_inode(struct inode *inode);
  362. extern int fat_fill_super(struct super_block *sb, void *data, int silent,
  363. const struct inode_operations *fs_dir_inode_ops, int isvfat);
  364. extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
  365. struct inode *i2);
  366. /* fat/misc.c */
  367. extern void fat_fs_panic(struct super_block *s, const char *fmt, ...);
  368. extern void fat_clusters_flush(struct super_block *sb);
  369. extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
  370. extern int date_dos2unix(unsigned short time, unsigned short date);
  371. extern void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date);
  372. extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
  373. int fat_cache_init(void);
  374. void fat_cache_destroy(void);
  375. #endif /* __KERNEL__ */
  376. #endif