added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.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 <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                                  * Unused under Linux. */
150         __u32   reserved2[4];
151 };
152
153 struct msdos_dir_entry {
154         __s8    name[8],ext[3]; /* name and extension */
155         __u8    attr;           /* attribute bits */
156         __u8    lcase;          /* Case for base and extension */
157         __u8    ctime_ms;       /* Creation time, milliseconds */
158         __u16   ctime;          /* Creation time */
159         __u16   cdate;          /* Creation date */
160         __u16   adate;          /* Last access date */
161         __u16   starthi;        /* High 16 bits of cluster in FAT32 */
162         __u16   time,date,start;/* time, date and first cluster */
163         __u32   size;           /* file size (in bytes) */
164 };
165
166 /* Up to 13 characters of the name */
167 struct msdos_dir_slot {
168         __u8    id;             /* sequence number for slot */
169         __u8    name0_4[10];    /* first 5 characters in name */
170         __u8    attr;           /* attribute byte */
171         __u8    reserved;       /* always 0 */
172         __u8    alias_checksum; /* checksum for 8.3 alias */
173         __u8    name5_10[12];   /* 6 more characters in name */
174         __u16   start;          /* starting cluster number, 0 in long slots */
175         __u8    name11_12[4];   /* last 2 characters in name */
176 };
177
178 struct vfat_slot_info {
179         int is_long;                   /* was the found entry long */
180         int long_slots;                /* number of long slots in filename */
181         int total_slots;               /* total slots (long and short) */
182         loff_t longname_offset;        /* dir offset for longname start */
183         loff_t shortname_offset;       /* dir offset for shortname start */
184         int ino;                       /* ino for the file */
185 };
186
187 /* Determine whether this FS has kB-aligned data. */
188 #define MSDOS_CAN_BMAP(mib) (!(((mib)->cluster_size & 1) || \
189     ((mib)->data_start & 1)))
190
191 /* Convert attribute bits and a mask to the UNIX mode. */
192 #define MSDOS_MKMODE(a,m) (m & (a & ATTR_RO ? S_IRUGO|S_IXUGO : S_IRWXUGO))
193
194 /* Convert the UNIX mode to MS-DOS attribute bits. */
195 #define MSDOS_MKATTR(m) ((m & S_IWUGO) ? ATTR_NONE : ATTR_RO)
196
197
198 #ifdef __KERNEL__
199
200 #include <linux/nls.h>
201
202 struct fat_cache {
203         kdev_t device; /* device number. 0 means unused. */
204         int start_cluster; /* first cluster of the chain. */
205         int file_cluster; /* cluster number in the file. */
206         int disk_cluster; /* cluster number on disk. */
207         struct fat_cache *next; /* next cache entry */
208 };
209
210 static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
211 {
212 #ifdef __BIG_ENDIAN
213         while (len--) {
214                 *dst++ = src[0] | (src[1] << 8);
215                 src += 2;
216         }
217 #else
218         memcpy(dst, src, len * 2);
219 #endif
220 }
221
222 static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
223 {
224 #ifdef __BIG_ENDIAN
225         while (len--) {
226                 dst[0] = *src & 0x00FF;
227                 dst[1] = (*src & 0xFF00) >> 8;
228                 dst += 2;
229                 src++;
230         }
231 #else
232         memcpy(dst, src, len * 2);
233 #endif
234 }
235
236 /* fat/buffer.c */
237 extern struct buffer_head *fat_bread(struct super_block *sb, int block);
238 extern struct buffer_head *fat_getblk(struct super_block *sb, int block);
239 extern void fat_brelse(struct super_block *sb, struct buffer_head *bh);
240 extern void fat_mark_buffer_dirty(struct super_block *sb, struct buffer_head *bh);
241 extern void fat_set_uptodate(struct super_block *sb, struct buffer_head *bh,
242                              int val);
243 extern int fat_is_uptodate(struct super_block *sb, struct buffer_head *bh);
244 extern void fat_ll_rw_block(struct super_block *sb, int opr, int nbreq,
245                             struct buffer_head *bh[32]);
246
247 /* fat/cache.c */
248 extern int fat_access(struct super_block *sb, int nr, int new_value);
249 extern int fat_bmap(struct inode *inode, int sector);
250 extern void fat_cache_init(void);
251 extern void fat_cache_lookup(struct inode *inode, int cluster, int *f_clu,
252                              int *d_clu);
253 extern void fat_cache_add(struct inode *inode, int f_clu, int d_clu);
254 extern void fat_cache_inval_inode(struct inode *inode);
255 extern void fat_cache_inval_dev(kdev_t device);
256 extern int fat_get_cluster(struct inode *inode, int cluster);
257 extern int fat_free(struct inode *inode, int skip);
258
259 /* fat/dir.c */
260 extern struct file_operations fat_dir_operations;
261 extern int fat_search_long(struct inode *inode, const char *name, int name_len,
262                            int anycase, loff_t *spos, loff_t *lpos);
263 extern int fat_readdir(struct file *filp, void *dirent, filldir_t filldir);
264 extern int fat_dir_ioctl(struct inode * inode, struct file * filp,
265                          unsigned int cmd, unsigned long arg);
266 extern int fat_dir_empty(struct inode *dir);
267 extern int fat_add_entries(struct inode *dir, int slots, struct buffer_head **bh,
268                            struct msdos_dir_entry **de, int *ino);
269 extern int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat);
270
271 /* fat/file.c */
272 extern struct file_operations fat_file_operations;
273 extern struct inode_operations fat_file_inode_operations;
274 extern ssize_t fat_file_read(struct file *filp, char *buf, size_t count,
275                              loff_t *ppos);
276 extern int fat_get_block(struct inode *inode, long iblock,
277                          struct buffer_head *bh_result, int create);
278 extern ssize_t fat_file_write(struct file *filp, const char *buf, size_t count,
279                               loff_t *ppos);
280 extern void fat_truncate(struct inode *inode);
281
282 /* fat/inode.c */
283 extern void fat_hash_init(void);
284 extern void fat_attach(struct inode *inode, int i_pos);
285 extern void fat_detach(struct inode *inode);
286 extern struct inode *fat_iget(struct super_block *sb, int i_pos);
287 extern struct inode *fat_build_inode(struct super_block *sb,
288                                      struct msdos_dir_entry *de, int ino, int *res);
289 extern void fat_delete_inode(struct inode *inode);
290 extern void fat_clear_inode(struct inode *inode);
291 extern void fat_put_super(struct super_block *sb);
292 extern struct super_block *
293 fat_read_super(struct super_block *sb, void *data, int silent,
294                struct inode_operations *fs_dir_inode_ops);
295 extern int fat_statfs(struct super_block *sb, struct statfs *buf);
296 extern void fat_write_inode(struct inode *inode, int wait);
297 extern int fat_notify_change(struct dentry * dentry, struct iattr * attr);
298
299 /* fat/misc.c */
300 extern void fat_fs_panic(struct super_block *s, const char *msg);
301 extern int fat_is_binary(char conversion, char *extension);
302 extern void lock_fat(struct super_block *sb);
303 extern void unlock_fat(struct super_block *sb);
304 extern void fat_clusters_flush(struct super_block *sb);
305 extern int fat_add_cluster(struct inode *inode);
306 extern struct buffer_head *fat_extend_dir(struct inode *inode);
307 extern int date_dos2unix(unsigned short time, unsigned short date);
308 extern void fat_date_unix2dos(int unix_date, unsigned short *time,
309                               unsigned short *date);
310 extern int fat__get_entry(struct inode *dir, loff_t *pos, struct buffer_head **bh,
311                           struct msdos_dir_entry **de, int *ino);
312 static __inline__ int fat_get_entry(struct inode *dir, loff_t *pos,
313                                     struct buffer_head **bh,
314                                     struct msdos_dir_entry **de, int *ino)
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                 (*ino)++;
322                 return 0;
323         }
324         return fat__get_entry(dir,pos,bh,de,ino);
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, int *ino);
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