1 #ifndef _LINUX_MSDOS_FS_H
2 #define _LINUX_MSDOS_FS_H
3
4 /*
5 * The MS-DOS filesystem constants/structures
6 */
7 #include <linux/fs.h>
8 #include <linux/stat.h>
9 #include <linux/fd.h>
10
11 #include <asm/byteorder.h>
12
13 #define MSDOS_ROOT_INO 1 /* == MINIX_ROOT_INO */
14 #define SECTOR_SIZE 512 /* sector size (bytes) */
15 #define SECTOR_BITS 9 /* log2(SECTOR_SIZE) */
16 #define MSDOS_DPB (MSDOS_DPS) /* dir entries per block */
17 #define MSDOS_DPB_BITS 4 /* log2(MSDOS_DPB) */
18 #define MSDOS_DPS (SECTOR_SIZE/sizeof(struct msdos_dir_entry))
19 #define MSDOS_DPS_BITS 4 /* log2(MSDOS_DPS) */
20 #define MSDOS_DIR_BITS 5 /* log2(sizeof(struct msdos_dir_entry)) */
21
22 #define MSDOS_SUPER_MAGIC 0x4d44 /* MD */
23
24 #define FAT_CACHE 8 /* FAT cache size */
25
26 #define MSDOS_MAX_EXTRA 3 /* tolerate up to that number of clusters which are
27 inaccessible because the FAT is too short */
28
29 #define ATTR_RO 1 /* read-only */
30 #define ATTR_HIDDEN 2 /* hidden */
31 #define ATTR_SYS 4 /* system */
32 #define ATTR_VOLUME 8 /* volume label */
33 #define ATTR_DIR 16 /* directory */
34 #define ATTR_ARCH 32 /* archived */
35
36 #define ATTR_NONE 0 /* no attribute bits */
37 #define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
38 /* attribute bits that are copied "as is" */
39 #define ATTR_EXT (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
40 /* bits that are used by the Windows 95/Windows NT extended FAT */
41
42 #define ATTR_DIR_READ_BOTH 512 /* read both short and long names from the
43 * vfat filesystem. This is used by Samba
44 * to export the vfat filesystem with correct
45 * shortnames. */
46 #define ATTR_DIR_READ_SHORT 1024
47
48 #define CASE_LOWER_BASE 8 /* base is lower case */
49 #define CASE_LOWER_EXT 16 /* extension is lower case */
50
51 #define SCAN_ANY 0 /* either hidden or not */
52 #define SCAN_HID 1 /* only hidden */
53 #define SCAN_NOTHID 2 /* only not hidden */
54 #define SCAN_NOTANY 3 /* test name, then use SCAN_HID or SCAN_NOTHID */
55
56 #define DELETED_FLAG 0xe5 /* marks file as deleted when in name[0] */
57 #define IS_FREE(n) (!*(n) || *(const unsigned char *) (n) == DELETED_FLAG)
58
59 #define MSDOS_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO)
60 /* valid file mode bits */
61
62 #define MSDOS_SB(s) (&((s)->u.msdos_sb))
63 #define MSDOS_I(i) (&((i)->u.msdos_i))
64
65 #define MSDOS_NAME 11 /* maximum name length */
66 #define MSDOS_LONGNAME 256 /* maximum name length */
67 #define MSDOS_SLOTS 21 /* max # of slots needed for short and long names */
68 #define MSDOS_DOT ". " /* ".", padded to MSDOS_NAME chars */
69 #define MSDOS_DOTDOT ".. " /* "..", padded to MSDOS_NAME chars */
70
71 #define MSDOS_FAT12 4084 /* maximum number of clusters in a 12 bit FAT */
72
73 #define EOF_FAT12 0xFFF /* standard EOF */
74 #define EOF_FAT16 0xFFFF
75 #define EOF_FAT32 0xFFFFFFF
76 #define EOF_FAT(s) (MSDOS_SB(s)->fat_bits == 32 ? EOF_FAT32 : \
77 MSDOS_SB(s)->fat_bits == 16 ? EOF_FAT16 : EOF_FAT12)
78
79 #define FAT_FSINFO_SIG1 0x41615252
80 #define FAT_FSINFO_SIG2 0x61417272
81 #define IS_FSINFO(x) (CF_LE_L((x)->signature1) == FAT_FSINFO_SIG1 \
82 && CF_LE_L((x)->signature2) == FAT_FSINFO_SIG2)
83
84 /*
85 * Inode flags
86 */
87 #define FAT_BINARY_FL 0x00000001 /* File contains binary data */
88
89 /*
90 * ioctl commands
91 */
92 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
93 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
94
95 /*
96 * vfat shortname flags
97 */
98 #define VFAT_SFN_DISPLAY_LOWER 0x0001 /* convert to lowercase for display */
99 #define VFAT_SFN_DISPLAY_WIN95 0x0002 /* emulate win95 rule for display */
100 #define VFAT_SFN_DISPLAY_WINNT 0x0004 /* emulate winnt rule for display */
101 #define VFAT_SFN_CREATE_WIN95 0x0100 /* emulate win95 rule for create */
102 #define VFAT_SFN_CREATE_WINNT 0x0200 /* emulate winnt rule for create */
103
104 /*
105 * Conversion from and to little-endian byte order. (no-op on i386/i486)
106 *
107 * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
108 * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
109 */
110
111 #define CF_LE_W(v) le16_to_cpu(v)
112 #define CF_LE_L(v) le32_to_cpu(v)
113 #define CT_LE_W(v) cpu_to_le16(v)
114 #define CT_LE_L(v) cpu_to_le32(v)
115
116 struct fat_boot_sector {
117 __s8 ignored[3]; /* Boot strap short or near jump */
118 __s8 system_id[8]; /* Name - can be used to special case
119 partition manager volumes */
120 __u8 sector_size[2]; /* bytes per logical sector */
121 __u8 cluster_size; /* sectors/cluster */
122 __u16 reserved; /* reserved sectors */
123 __u8 fats; /* number of FATs */
124 __u8 dir_entries[2]; /* root directory entries */
125 __u8 sectors[2]; /* number of sectors */
126 __u8 media; /* media code (unused) */
127 __u16 fat_length; /* sectors/FAT */
128 __u16 secs_track; /* sectors per track */
129 __u16 heads; /* number of heads */
130 __u32 hidden; /* hidden sectors (unused) */
131 __u32 total_sect; /* number of sectors (if sectors == 0) */
132
133 /* The following fields are only used by FAT32 */
134 __u32 fat32_length; /* sectors/FAT */
135 __u16 flags; /* bit 8: fat mirroring, low 4: active fat */
136 __u8 version[2]; /* major, minor filesystem version */
137 __u32 root_cluster; /* first cluster in root directory */
138 __u16 info_sector; /* filesystem info sector */
139 __u16 backup_boot; /* backup boot sector */
140 __u16 reserved2[6]; /* Unused */
141 };
142
143 struct fat_boot_fsinfo {
144 __u32 signature1; /* 0x41615252L */
145 __u32 reserved1[120]; /* Nothing as far as I can tell */
146 __u32 signature2; /* 0x61417272L */
147 __u32 free_clusters; /* Free cluster count. -1 if unknown */
148 __u32 next_cluster; /* Most recently allocated cluster */
149 __u32 reserved2[4];
150 };
151
152 struct msdos_dir_entry {
153 __s8 name[8],ext[3]; /* name and extension */
154 __u8 attr; /* attribute bits */
155 __u8 lcase; /* Case for base and extension */
156 __u8 ctime_ms; /* Creation time, milliseconds */
157 __u16 ctime; /* Creation time */
158 __u16 cdate; /* Creation date */
159 __u16 adate; /* Last access date */
160 __u16 starthi; /* High 16 bits of cluster in FAT32 */
161 __u16 time,date,start;/* time, date and first cluster */
162 __u32 size; /* file size (in bytes) */
163 };
164
165 /* Up to 13 characters of the name */
166 struct msdos_dir_slot {
167 __u8 id; /* sequence number for slot */
168 __u8 name0_4[10]; /* first 5 characters in name */
169 __u8 attr; /* attribute byte */
170 __u8 reserved; /* always 0 */
171 __u8 alias_checksum; /* checksum for 8.3 alias */
172 __u8 name5_10[12]; /* 6 more characters in name */
173 __u16 start; /* starting cluster number, 0 in long slots */
174 __u8 name11_12[4]; /* last 2 characters in name */
175 };
176
177 struct vfat_slot_info {
178 int is_long; /* was the found entry long */
179 int long_slots; /* number of long slots in filename */
180 int total_slots; /* total slots (long and short) */
181 loff_t longname_offset; /* dir offset for longname start */
182 loff_t shortname_offset; /* dir offset for shortname start */
183 loff_t i_pos; /* on-disk position of directory entry */
184 };
185
186 /* Determine whether this FS has kB-aligned data. */
187 #define MSDOS_CAN_BMAP(mib) (!(((mib)->cluster_size & 1) || \
188 ((mib)->data_start & 1)))
189
190 /* Convert attribute bits and a mask to the UNIX mode. */
191 #define MSDOS_MKMODE(a,m) (m & (a & ATTR_RO ? S_IRUGO|S_IXUGO : S_IRWXUGO))
192
193 /* Convert the UNIX mode to MS-DOS attribute bits. */
194 #define MSDOS_MKATTR(m) ((m & S_IWUGO) ? ATTR_NONE : ATTR_RO)
195
196
197 #ifdef __KERNEL__
198
199 #include <linux/nls.h>
200
201 struct fat_cache {
202 kdev_t device; /* device number. 0 means unused. */
203 int start_cluster; /* first cluster of the chain. */
204 int file_cluster; /* cluster number in the file. */
205 int disk_cluster; /* cluster number on disk. */
206 struct fat_cache *next; /* next cache entry */
207 };
208
fat16_towchar(wchar_t * dst,const __u8 * src,size_t len)209 static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
210 {
211 #ifdef __BIG_ENDIAN
212 while (len--) {
213 *dst++ = src[0] | (src[1] << 8);
214 src += 2;
215 }
216 #else
217 memcpy(dst, src, len * 2);
218 #endif
219 }
220
fatwchar_to16(__u8 * dst,const wchar_t * src,size_t len)221 static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
222 {
223 #ifdef __BIG_ENDIAN
224 while (len--) {
225 dst[0] = *src & 0x00FF;
226 dst[1] = (*src & 0xFF00) >> 8;
227 dst += 2;
228 src++;
229 }
230 #else
231 memcpy(dst, src, len * 2);
232 #endif
233 }
234
235 /* fat/buffer.c */
236 extern struct buffer_head *fat_bread(struct super_block *sb, int block);
237 extern struct buffer_head *fat_getblk(struct super_block *sb, int block);
238 extern void fat_brelse(struct super_block *sb, struct buffer_head *bh);
239 extern void fat_mark_buffer_dirty(struct super_block *sb, struct buffer_head *bh);
240 extern void fat_set_uptodate(struct super_block *sb, struct buffer_head *bh,
241 int val);
242 extern int fat_is_uptodate(struct super_block *sb, struct buffer_head *bh);
243 extern void fat_ll_rw_block(struct super_block *sb, int opr, int nbreq,
244 struct buffer_head *bh[32]);
245
246 /* fat/cache.c */
247 extern int fat_access(struct super_block *sb, int nr, int new_value);
248 extern int fat_bmap(struct inode *inode, int sector);
249 extern void fat_cache_init(void);
250 extern void fat_cache_lookup(struct inode *inode, int cluster, int *f_clu,
251 int *d_clu);
252 extern void fat_cache_add(struct inode *inode, int f_clu, int d_clu);
253 extern void fat_cache_inval_inode(struct inode *inode);
254 extern void fat_cache_inval_dev(kdev_t device);
255 extern int fat_get_cluster(struct inode *inode, int cluster);
256 extern int fat_free(struct inode *inode, int skip);
257
258 /* fat/dir.c */
259 extern struct file_operations fat_dir_operations;
260 extern int fat_search_long(struct inode *inode, const char *name, int name_len,
261 int anycase, loff_t *spos, loff_t *lpos);
262 extern int fat_readdir(struct file *filp, void *dirent, filldir_t filldir);
263 extern int fat_dir_ioctl(struct inode * inode, struct file * filp,
264 unsigned int cmd, unsigned long arg);
265 extern int fat_dir_empty(struct inode *dir);
266 extern int fat_add_entries(struct inode *dir, int slots, struct buffer_head **bh,
267 struct msdos_dir_entry **de, loff_t *i_pos);
268 extern int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat);
269
270 /* fat/file.c */
271 extern struct file_operations fat_file_operations;
272 extern struct inode_operations fat_file_inode_operations;
273 extern ssize_t fat_file_read(struct file *filp, char *buf, size_t count,
274 loff_t *ppos);
275 extern int fat_get_block(struct inode *inode, long iblock,
276 struct buffer_head *bh_result, int create);
277 extern ssize_t fat_file_write(struct file *filp, const char *buf, size_t count,
278 loff_t *ppos);
279 extern void fat_truncate(struct inode *inode);
280
281 /* fat/inode.c */
282 extern void fat_hash_init(void);
283 extern void fat_attach(struct inode *inode, loff_t i_pos);
284 extern void fat_detach(struct inode *inode);
285 extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
286 extern struct inode *fat_build_inode(struct super_block *sb,
287 struct msdos_dir_entry *de, loff_t i_pos, int *res);
288 extern void fat_delete_inode(struct inode *inode);
289 extern void fat_clear_inode(struct inode *inode);
290 extern void fat_put_super(struct super_block *sb);
291 extern struct super_block *
292 fat_read_super(struct super_block *sb, void *data, int silent,
293 struct inode_operations *fs_dir_inode_ops);
294 extern int fat_statfs(struct super_block *sb, struct statfs *buf);
295 extern void fat_write_inode(struct inode *inode, int wait);
296 extern int fat_notify_change(struct dentry * dentry, struct iattr * attr);
297
298 /* fat/misc.c */
299 extern void fat_fs_panic(struct super_block *s, const char *msg);
300 extern int fat_is_binary(char conversion, char *extension);
301 extern void lock_fat(struct super_block *sb);
302 extern void unlock_fat(struct super_block *sb);
303 extern void fat_clusters_flush(struct super_block *sb);
304 extern int fat_add_cluster(struct inode *inode);
305 extern struct buffer_head *fat_extend_dir(struct inode *inode);
306 extern int date_dos2unix(unsigned short time, unsigned short date);
307 extern void fat_date_unix2dos(int unix_date, unsigned short *time,
308 unsigned short *date);
309 extern int fat__get_entry(struct inode *dir, loff_t *pos,
310 struct buffer_head **bh,
311 struct msdos_dir_entry **de, loff_t *i_pos);
fat_get_entry(struct inode * dir,loff_t * pos,struct buffer_head ** bh,struct msdos_dir_entry ** de,loff_t * i_pos)312 static __inline__ int fat_get_entry(struct inode *dir, loff_t *pos,
313 struct buffer_head **bh,
314 struct msdos_dir_entry **de, loff_t *i_pos)
315 {
316 /* Fast stuff first */
317 if (*bh && *de &&
318 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
319 *pos += sizeof(struct msdos_dir_entry);
320 (*de)++;
321 (*i_pos)++;
322 return 0;
323 }
324 return fat__get_entry(dir, pos, bh, de, i_pos);
325 }
326 extern int fat_subdirs(struct inode *dir);
327 extern int fat_scan(struct inode *dir, const char *name,
328 struct buffer_head **res_bh,
329 struct msdos_dir_entry **res_de, loff_t *i_pos);
330
331 /* msdos/namei.c - these are for Umsdos */
332 extern void msdos_put_super(struct super_block *sb);
333 extern struct dentry *msdos_lookup(struct inode *dir, struct dentry *);
334 extern int msdos_create(struct inode *dir, struct dentry *dentry, int mode);
335 extern int msdos_rmdir(struct inode *dir, struct dentry *dentry);
336 extern int msdos_mkdir(struct inode *dir, struct dentry *dentry, int mode);
337 extern int msdos_unlink(struct inode *dir, struct dentry *dentry);
338 extern int msdos_rename(struct inode *old_dir, struct dentry *old_dentry,
339 struct inode *new_dir, struct dentry *new_dentry);
340 extern struct super_block *msdos_read_super(struct super_block *sb,
341 void *data, int silent);
342
343 /* vfat/namei.c - these are for dmsdos */
344 extern struct dentry *vfat_lookup(struct inode *dir, struct dentry *);
345 extern int vfat_create(struct inode *dir, struct dentry *dentry, int mode);
346 extern int vfat_rmdir(struct inode *dir, struct dentry *dentry);
347 extern int vfat_unlink(struct inode *dir, struct dentry *dentry);
348 extern int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode);
349 extern int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
350 struct inode *new_dir, struct dentry *new_dentry);
351 extern struct super_block *vfat_read_super(struct super_block *sb, void *data,
352 int silent);
353
354 /* vfat/vfatfs_syms.c */
355 extern struct file_system_type vfat_fs_type;
356
357 #endif /* __KERNEL__ */
358
359 #endif
360