ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / linux / msdos_fs.h
1 #ifndef _LINUX_MSDOS_FS_H
2 #define _LINUX_MSDOS_FS_H
3
4 /*
5  * The MS-DOS filesystem constants/structures
6  */
7 #include <asm/byteorder.h>
8
9 #define SECTOR_SIZE     512             /* sector size (bytes) */
10 #define SECTOR_BITS     9               /* log2(SECTOR_SIZE) */
11 #define MSDOS_DPB       (MSDOS_DPS)     /* dir entries per block */
12 #define MSDOS_DPB_BITS  4               /* log2(MSDOS_DPB) */
13 #define MSDOS_DPS       (SECTOR_SIZE / sizeof(struct msdos_dir_entry))
14 #define MSDOS_DPS_BITS  4               /* log2(MSDOS_DPS) */
15
16
17 #define MSDOS_SUPER_MAGIC 0x4d44 /* MD */
18
19 #define MSDOS_ROOT_INO  1       /* == MINIX_ROOT_INO */
20 #define MSDOS_DIR_BITS  5       /* log2(sizeof(struct msdos_dir_entry)) */
21
22 /* directory limit */
23 #define FAT_MAX_DIR_ENTRIES     (65536)
24 #define FAT_MAX_DIR_SIZE        (FAT_MAX_DIR_ENTRIES << MSDOS_DIR_BITS)
25
26 #define ATTR_NONE    0 /* no attribute bits */
27 #define ATTR_RO      1  /* read-only */
28 #define ATTR_HIDDEN  2  /* hidden */
29 #define ATTR_SYS     4  /* system */
30 #define ATTR_VOLUME  8  /* volume label */
31 #define ATTR_DIR     16 /* directory */
32 #define ATTR_ARCH    32 /* archived */
33
34 /* attribute bits that are copied "as is" */
35 #define ATTR_UNUSED  (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
36 /* bits that are used by the Windows 95/Windows NT extended FAT */
37 #define ATTR_EXT     (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
38
39 #define CASE_LOWER_BASE 8       /* base is lower case */
40 #define CASE_LOWER_EXT  16      /* extension is lower case */
41
42 #define DELETED_FLAG 0xe5 /* marks file as deleted when in name[0] */
43 #define IS_FREE(n) (!*(n) || *(n) == DELETED_FLAG)
44
45 /* valid file mode bits */
46 #define MSDOS_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO)
47 /* Convert attribute bits and a mask to the UNIX mode. */
48 #define MSDOS_MKMODE(a, m) (m & (a & ATTR_RO ? S_IRUGO|S_IXUGO : S_IRWXUGO))
49 /* Convert the UNIX mode to MS-DOS attribute bits. */
50 #define MSDOS_MKATTR(m) ((m & S_IWUGO) ? ATTR_NONE : ATTR_RO)
51
52 #define MSDOS_NAME 11 /* maximum name length */
53 #define MSDOS_LONGNAME 256 /* maximum name length */
54 #define MSDOS_SLOTS 21  /* max # of slots needed for short and long names */
55 #define MSDOS_DOT    ".          " /* ".", padded to MSDOS_NAME chars */
56 #define MSDOS_DOTDOT "..         " /* "..", padded to MSDOS_NAME chars */
57
58 /* media of boot sector */
59 #define FAT_VALID_MEDIA(x)      ((0xF8 <= (x) && (x) <= 0xFF) || (x) == 0xF0)
60 #define FAT_FIRST_ENT(s, x)     ((MSDOS_SB(s)->fat_bits == 32 ? 0x0FFFFF00 : \
61         MSDOS_SB(s)->fat_bits == 16 ? 0xFF00 : 0xF00) | (x))
62
63 /* maximum number of clusters */
64 #define MAX_FAT12 0xFF4
65 #define MAX_FAT16 0xFFF4
66 #define MAX_FAT32 0x0FFFFFF6
67 #define MAX_FAT(s) (MSDOS_SB(s)->fat_bits == 32 ? MAX_FAT32 : \
68         MSDOS_SB(s)->fat_bits == 16 ? MAX_FAT16 : MAX_FAT12)
69
70 /* bad cluster mark */
71 #define BAD_FAT12 0xFF7
72 #define BAD_FAT16 0xFFF7
73 #define BAD_FAT32 0x0FFFFFF7
74 #define BAD_FAT(s) (MSDOS_SB(s)->fat_bits == 32 ? BAD_FAT32 : \
75         MSDOS_SB(s)->fat_bits == 16 ? BAD_FAT16 : BAD_FAT12)
76
77 /* standard EOF */
78 #define EOF_FAT12 0xFFF
79 #define EOF_FAT16 0xFFFF
80 #define EOF_FAT32 0x0FFFFFFF
81 #define EOF_FAT(s) (MSDOS_SB(s)->fat_bits == 32 ? EOF_FAT32 : \
82         MSDOS_SB(s)->fat_bits == 16 ? EOF_FAT16 : EOF_FAT12)
83
84 #define FAT_ENT_FREE    (0)
85 #define FAT_ENT_BAD     (BAD_FAT32)
86 #define FAT_ENT_EOF     (EOF_FAT32)
87
88 #define FAT_FSINFO_SIG1 0x41615252
89 #define FAT_FSINFO_SIG2 0x61417272
90 #define IS_FSINFO(x)    (CF_LE_L((x)->signature1) == FAT_FSINFO_SIG1    \
91                          && CF_LE_L((x)->signature2) == FAT_FSINFO_SIG2)
92
93 /*
94  * ioctl commands
95  */
96 #define VFAT_IOCTL_READDIR_BOTH         _IOR('r', 1, struct dirent [2])
97 #define VFAT_IOCTL_READDIR_SHORT        _IOR('r', 2, struct dirent [2])
98
99 /* 
100  * vfat shortname flags
101  */
102 #define VFAT_SFN_DISPLAY_LOWER  0x0001 /* convert to lowercase for display */
103 #define VFAT_SFN_DISPLAY_WIN95  0x0002 /* emulate win95 rule for display */
104 #define VFAT_SFN_DISPLAY_WINNT  0x0004 /* emulate winnt rule for display */
105 #define VFAT_SFN_CREATE_WIN95   0x0100 /* emulate win95 rule for create */
106 #define VFAT_SFN_CREATE_WINNT   0x0200 /* emulate winnt rule for create */
107
108 /*
109  * Conversion from and to little-endian byte order. (no-op on i386/i486)
110  *
111  * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
112  * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
113  */
114
115 #define CF_LE_W(v) le16_to_cpu(v)
116 #define CF_LE_L(v) le32_to_cpu(v)
117 #define CT_LE_W(v) cpu_to_le16(v)
118 #define CT_LE_L(v) cpu_to_le32(v)
119
120 struct fat_boot_sector {
121         __u8    ignored[3];     /* Boot strap short or near jump */
122         __u8    system_id[8];   /* Name - can be used to special case
123                                    partition manager volumes */
124         __u8    sector_size[2]; /* bytes per logical sector */
125         __u8    sec_per_clus;   /* sectors/cluster */
126         __u16   reserved;       /* reserved sectors */
127         __u8    fats;           /* number of FATs */
128         __u8    dir_entries[2]; /* root directory entries */
129         __u8    sectors[2];     /* number of sectors */
130         __u8    media;          /* media code */
131         __u16   fat_length;     /* sectors/FAT */
132         __u16   secs_track;     /* sectors per track */
133         __u16   heads;          /* number of heads */
134         __u32   hidden;         /* hidden sectors (unused) */
135         __u32   total_sect;     /* number of sectors (if sectors == 0) */
136
137         /* The following fields are only used by FAT32 */
138         __u32   fat32_length;   /* sectors/FAT */
139         __u16   flags;          /* bit 8: fat mirroring, low 4: active fat */
140         __u8    version[2];     /* major, minor filesystem version */
141         __u32   root_cluster;   /* first cluster in root directory */
142         __u16   info_sector;    /* filesystem info sector */
143         __u16   backup_boot;    /* backup boot sector */
144         __u16   reserved2[6];   /* Unused */
145 };
146
147 struct fat_boot_fsinfo {
148         __u32   signature1;     /* 0x41615252L */
149         __u32   reserved1[120]; /* Nothing as far as I can tell */
150         __u32   signature2;     /* 0x61417272L */
151         __u32   free_clusters;  /* Free cluster count.  -1 if unknown */
152         __u32   next_cluster;   /* Most recently allocated cluster */
153         __u32   reserved2[4];
154 };
155
156 struct msdos_dir_entry {
157         __u8    name[8],ext[3]; /* name and extension */
158         __u8    attr;           /* attribute bits */
159         __u8    lcase;          /* Case for base and extension */
160         __u8    ctime_ms;       /* Creation time, milliseconds */
161         __u16   ctime;          /* Creation time */
162         __u16   cdate;          /* Creation date */
163         __u16   adate;          /* Last access date */
164         __u16   starthi;        /* High 16 bits of cluster in FAT32 */
165         __u16   time,date,start;/* time, date and first cluster */
166         __u32   size;           /* file size (in bytes) */
167 };
168
169 /* Up to 13 characters of the name */
170 struct msdos_dir_slot {
171         __u8    id;             /* sequence number for slot */
172         __u8    name0_4[10];    /* first 5 characters in name */
173         __u8    attr;           /* attribute byte */
174         __u8    reserved;       /* always 0 */
175         __u8    alias_checksum; /* checksum for 8.3 alias */
176         __u8    name5_10[12];   /* 6 more characters in name */
177         __u16   start;          /* starting cluster number, 0 in long slots */
178         __u8    name11_12[4];   /* last 2 characters in name */
179 };
180
181 struct vfat_slot_info {
182         int long_slots;                /* number of long slots in filename */
183         loff_t longname_offset;        /* dir offset for longname start */
184         loff_t i_pos;                  /* on-disk position of directory entry */
185 };
186
187 #ifdef __KERNEL__
188
189 #include <linux/buffer_head.h>
190 #include <linux/string.h>
191 #include <linux/nls.h>
192 #include <linux/msdos_fs_i.h>
193 #include <linux/msdos_fs_sb.h>
194
195 static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
196 {
197         return sb->s_fs_info;
198 }
199
200 static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
201 {
202         return container_of(inode, struct msdos_inode_info, vfs_inode);
203 }
204
205 static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
206 {
207 #ifdef __BIG_ENDIAN
208         while (len--) {
209                 *dst++ = src[0] | (src[1] << 8);
210                 src += 2;
211         }
212 #else
213         memcpy(dst, src, len * 2);
214 #endif
215 }
216
217 static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
218 {
219 #ifdef __BIG_ENDIAN
220         while (len--) {
221                 dst[0] = *src & 0x00FF;
222                 dst[1] = (*src & 0xFF00) >> 8;
223                 dst += 2;
224                 src++;
225         }
226 #else
227         memcpy(dst, src, len * 2);
228 #endif
229 }
230
231 /* fat/cache.c */
232 extern int fat_access(struct super_block *sb, int nr, int new_value);
233 extern int __fat_access(struct super_block *sb, int nr, int new_value);
234 extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys);
235 extern void fat_cache_init(struct super_block *sb);
236 extern void fat_cache_lookup(struct inode *inode, int cluster, int *f_clu,
237                              int *d_clu);
238 extern void fat_cache_add(struct inode *inode, int f_clu, int d_clu);
239 extern void fat_cache_inval_inode(struct inode *inode);
240 extern int fat_get_cluster(struct inode *inode, int cluster,
241                            int *fclus, int *dclus);
242 extern int fat_free(struct inode *inode, int skip);
243
244 /* fat/dir.c */
245 extern struct file_operations fat_dir_operations;
246 extern int fat_search_long(struct inode *inode, const unsigned char *name,
247                            int name_len, int anycase,
248                            loff_t *spos, loff_t *lpos);
249 extern int fat_readdir(struct file *filp, void *dirent, filldir_t filldir);
250 extern int fat_dir_ioctl(struct inode * inode, struct file * filp,
251                          unsigned int cmd, unsigned long arg);
252 extern int fat_add_entries(struct inode *dir, int slots, struct buffer_head **bh,
253                         struct msdos_dir_entry **de, loff_t *i_pos);
254 extern int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat);
255 extern int fat_dir_empty(struct inode *dir);
256 extern int fat_subdirs(struct inode *dir);
257 extern int fat_scan(struct inode *dir, const unsigned char *name,
258                     struct buffer_head **res_bh,
259                     struct msdos_dir_entry **res_de, loff_t *i_pos);
260
261 /* fat/file.c */
262 extern struct file_operations fat_file_operations;
263 extern struct inode_operations fat_file_inode_operations;
264 extern int fat_get_block(struct inode *inode, sector_t iblock,
265                          struct buffer_head *bh_result, int create);
266 extern void fat_truncate(struct inode *inode);
267
268 /* fat/inode.c */
269 extern void fat_hash_init(void);
270 extern void fat_attach(struct inode *inode, loff_t i_pos);
271 extern void fat_detach(struct inode *inode);
272 extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
273 extern struct inode *fat_build_inode(struct super_block *sb,
274                         struct msdos_dir_entry *de, loff_t i_pos, int *res);
275 extern void fat_delete_inode(struct inode *inode);
276 extern void fat_clear_inode(struct inode *inode);
277 extern void fat_put_super(struct super_block *sb);
278 int fat_fill_super(struct super_block *sb, void *data, int silent,
279                    struct inode_operations *fs_dir_inode_ops, int isvfat);
280 extern int fat_statfs(struct super_block *sb, struct kstatfs *buf);
281 extern void fat_write_inode(struct inode *inode, int wait);
282 extern int fat_notify_change(struct dentry * dentry, struct iattr * attr);
283
284 /* fat/misc.c */
285 extern void fat_fs_panic(struct super_block *s, const char *fmt, ...);
286 extern void lock_fat(struct super_block *sb);
287 extern void unlock_fat(struct super_block *sb);
288 extern void fat_clusters_flush(struct super_block *sb);
289 extern int fat_add_cluster(struct inode *inode);
290 extern struct buffer_head *fat_extend_dir(struct inode *inode);
291 extern int date_dos2unix(unsigned short time, unsigned short date);
292 extern void fat_date_unix2dos(int unix_date, unsigned short *time,
293                               unsigned short *date);
294 extern int fat__get_entry(struct inode *dir, loff_t *pos,
295                           struct buffer_head **bh,
296                           struct msdos_dir_entry **de, loff_t *i_pos);
297 static __inline__ int fat_get_entry(struct inode *dir, loff_t *pos,
298                                     struct buffer_head **bh,
299                                     struct msdos_dir_entry **de, loff_t *i_pos)
300 {
301         /* Fast stuff first */
302         if (*bh && *de &&
303             (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
304                 *pos += sizeof(struct msdos_dir_entry);
305                 (*de)++;
306                 (*i_pos)++;
307                 return 0;
308         }
309         return fat__get_entry(dir, pos, bh, de, i_pos);
310 }
311
312 #endif /* __KERNEL__ */
313
314 #endif