xref: /OK3568_Linux_fs/kernel/fs/fat/fat.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _FAT_H
3*4882a593Smuzhiyun #define _FAT_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/buffer_head.h>
6*4882a593Smuzhiyun #include <linux/nls.h>
7*4882a593Smuzhiyun #include <linux/hash.h>
8*4882a593Smuzhiyun #include <linux/ratelimit.h>
9*4882a593Smuzhiyun #include <linux/msdos_fs.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * vfat shortname flags
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #define VFAT_SFN_DISPLAY_LOWER	0x0001 /* convert to lowercase for display */
15*4882a593Smuzhiyun #define VFAT_SFN_DISPLAY_WIN95	0x0002 /* emulate win95 rule for display */
16*4882a593Smuzhiyun #define VFAT_SFN_DISPLAY_WINNT	0x0004 /* emulate winnt rule for display */
17*4882a593Smuzhiyun #define VFAT_SFN_CREATE_WIN95	0x0100 /* emulate win95 rule for create */
18*4882a593Smuzhiyun #define VFAT_SFN_CREATE_WINNT	0x0200 /* emulate winnt rule for create */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define FAT_ERRORS_CONT		1      /* ignore error and continue */
21*4882a593Smuzhiyun #define FAT_ERRORS_PANIC	2      /* panic on error */
22*4882a593Smuzhiyun #define FAT_ERRORS_RO		3      /* remount r/o on error */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define FAT_NFS_STALE_RW	1      /* NFS RW support, can cause ESTALE */
25*4882a593Smuzhiyun #define FAT_NFS_NOSTALE_RO	2      /* NFS RO support, no ESTALE issue */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct fat_mount_options {
28*4882a593Smuzhiyun 	kuid_t fs_uid;
29*4882a593Smuzhiyun 	kgid_t fs_gid;
30*4882a593Smuzhiyun 	unsigned short fs_fmask;
31*4882a593Smuzhiyun 	unsigned short fs_dmask;
32*4882a593Smuzhiyun 	unsigned short codepage;   /* Codepage for shortname conversions */
33*4882a593Smuzhiyun 	int time_offset;	   /* Offset of timestamps from UTC (in minutes) */
34*4882a593Smuzhiyun 	char *iocharset;           /* Charset used for filename input/display */
35*4882a593Smuzhiyun 	unsigned short shortname;  /* flags for shortname display/create rule */
36*4882a593Smuzhiyun 	unsigned char name_check;  /* r = relaxed, n = normal, s = strict */
37*4882a593Smuzhiyun 	unsigned char errors;	   /* On error: continue, panic, remount-ro */
38*4882a593Smuzhiyun 	unsigned char nfs;	  /* NFS support: nostale_ro, stale_rw */
39*4882a593Smuzhiyun 	unsigned short allow_utime;/* permission for setting the [am]time */
40*4882a593Smuzhiyun 	unsigned quiet:1,          /* set = fake successful chmods and chowns */
41*4882a593Smuzhiyun 		 showexec:1,       /* set = only set x bit for com/exe/bat */
42*4882a593Smuzhiyun 		 sys_immutable:1,  /* set = system files are immutable */
43*4882a593Smuzhiyun 		 dotsOK:1,         /* set = hidden and system files are named '.filename' */
44*4882a593Smuzhiyun 		 isvfat:1,         /* 0=no vfat long filename support, 1=vfat support */
45*4882a593Smuzhiyun 		 utf8:1,	   /* Use of UTF-8 character set (Default) */
46*4882a593Smuzhiyun 		 unicode_xlate:1,  /* create escape sequences for unhandled Unicode */
47*4882a593Smuzhiyun 		 numtail:1,        /* Does first alias have a numeric '~1' type tail? */
48*4882a593Smuzhiyun 		 flush:1,	   /* write things quickly */
49*4882a593Smuzhiyun 		 nocase:1,	   /* Does this need case conversion? 0=need case conversion*/
50*4882a593Smuzhiyun 		 usefree:1,	   /* Use free_clusters for FAT32 */
51*4882a593Smuzhiyun 		 tz_set:1,	   /* Filesystem timestamps' offset set */
52*4882a593Smuzhiyun 		 rodir:1,	   /* allow ATTR_RO for directory */
53*4882a593Smuzhiyun 		 discard:1,	   /* Issue discard requests on deletions */
54*4882a593Smuzhiyun 		 dos1xfloppy:1;	   /* Assume default BPB for DOS 1.x floppies */
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define FAT_HASH_BITS	8
58*4882a593Smuzhiyun #define FAT_HASH_SIZE	(1UL << FAT_HASH_BITS)
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun  * MS-DOS file system in-core superblock data
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun struct msdos_sb_info {
64*4882a593Smuzhiyun 	unsigned short sec_per_clus;  /* sectors/cluster */
65*4882a593Smuzhiyun 	unsigned short cluster_bits;  /* log2(cluster_size) */
66*4882a593Smuzhiyun 	unsigned int cluster_size;    /* cluster size */
67*4882a593Smuzhiyun 	unsigned char fats, fat_bits; /* number of FATs, FAT bits (12,16 or 32) */
68*4882a593Smuzhiyun 	unsigned short fat_start;
69*4882a593Smuzhiyun 	unsigned long fat_length;     /* FAT start & length (sec.) */
70*4882a593Smuzhiyun 	unsigned long dir_start;
71*4882a593Smuzhiyun 	unsigned short dir_entries;   /* root dir start & entries */
72*4882a593Smuzhiyun 	unsigned long data_start;     /* first data sector */
73*4882a593Smuzhiyun 	unsigned long max_cluster;    /* maximum cluster number */
74*4882a593Smuzhiyun 	unsigned long root_cluster;   /* first cluster of the root directory */
75*4882a593Smuzhiyun 	unsigned long fsinfo_sector;  /* sector number of FAT32 fsinfo */
76*4882a593Smuzhiyun 	struct mutex fat_lock;
77*4882a593Smuzhiyun 	struct mutex nfs_build_inode_lock;
78*4882a593Smuzhiyun 	struct mutex s_lock;
79*4882a593Smuzhiyun 	unsigned int prev_free;      /* previously allocated cluster number */
80*4882a593Smuzhiyun 	unsigned int free_clusters;  /* -1 if undefined */
81*4882a593Smuzhiyun 	unsigned int free_clus_valid; /* is free_clusters valid? */
82*4882a593Smuzhiyun 	struct fat_mount_options options;
83*4882a593Smuzhiyun 	struct nls_table *nls_disk;   /* Codepage used on disk */
84*4882a593Smuzhiyun 	struct nls_table *nls_io;     /* Charset used for input and display */
85*4882a593Smuzhiyun 	const void *dir_ops;	      /* Opaque; default directory operations */
86*4882a593Smuzhiyun 	int dir_per_block;	      /* dir entries per block */
87*4882a593Smuzhiyun 	int dir_per_block_bits;	      /* log2(dir_per_block) */
88*4882a593Smuzhiyun 	unsigned int vol_id;		/*volume ID*/
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	int fatent_shift;
91*4882a593Smuzhiyun 	const struct fatent_operations *fatent_ops;
92*4882a593Smuzhiyun 	struct inode *fat_inode;
93*4882a593Smuzhiyun 	struct inode *fsinfo_inode;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	struct ratelimit_state ratelimit;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	spinlock_t inode_hash_lock;
98*4882a593Smuzhiyun 	struct hlist_head inode_hashtable[FAT_HASH_SIZE];
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	spinlock_t dir_hash_lock;
101*4882a593Smuzhiyun 	struct hlist_head dir_hashtable[FAT_HASH_SIZE];
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	unsigned int dirty;           /* fs state before mount */
104*4882a593Smuzhiyun 	struct rcu_head rcu;
105*4882a593Smuzhiyun };
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #define FAT_CACHE_VALID	0	/* special case for valid cache */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun  * MS-DOS file system inode data in memory
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun struct msdos_inode_info {
113*4882a593Smuzhiyun 	spinlock_t cache_lru_lock;
114*4882a593Smuzhiyun 	struct list_head cache_lru;
115*4882a593Smuzhiyun 	int nr_caches;
116*4882a593Smuzhiyun 	/* for avoiding the race between fat_free() and fat_get_cluster() */
117*4882a593Smuzhiyun 	unsigned int cache_valid_id;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */
120*4882a593Smuzhiyun 	loff_t mmu_private;	/* physically allocated size */
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	int i_start;		/* first cluster or 0 */
123*4882a593Smuzhiyun 	int i_logstart;		/* logical first cluster */
124*4882a593Smuzhiyun 	int i_attrs;		/* unused attribute bits */
125*4882a593Smuzhiyun 	loff_t i_pos;		/* on-disk position of directory entry or 0 */
126*4882a593Smuzhiyun 	struct hlist_node i_fat_hash;	/* hash by i_location */
127*4882a593Smuzhiyun 	struct hlist_node i_dir_hash;	/* hash by i_logstart */
128*4882a593Smuzhiyun 	struct rw_semaphore truncate_lock; /* protect bmap against truncate */
129*4882a593Smuzhiyun 	struct inode vfs_inode;
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun struct fat_slot_info {
133*4882a593Smuzhiyun 	loff_t i_pos;		/* on-disk position of directory entry */
134*4882a593Smuzhiyun 	loff_t slot_off;	/* offset for slot or de start */
135*4882a593Smuzhiyun 	int nr_slots;		/* number of slots + 1(de) in filename */
136*4882a593Smuzhiyun 	struct msdos_dir_entry *de;
137*4882a593Smuzhiyun 	struct buffer_head *bh;
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun 
MSDOS_SB(struct super_block * sb)140*4882a593Smuzhiyun static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	return sb->s_fs_info;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun  * Functions that determine the variant of the FAT file system (i.e.,
147*4882a593Smuzhiyun  * whether this is FAT12, FAT16 or FAT32.
148*4882a593Smuzhiyun  */
is_fat12(const struct msdos_sb_info * sbi)149*4882a593Smuzhiyun static inline bool is_fat12(const struct msdos_sb_info *sbi)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	return sbi->fat_bits == 12;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
is_fat16(const struct msdos_sb_info * sbi)154*4882a593Smuzhiyun static inline bool is_fat16(const struct msdos_sb_info *sbi)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	return sbi->fat_bits == 16;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
is_fat32(const struct msdos_sb_info * sbi)159*4882a593Smuzhiyun static inline bool is_fat32(const struct msdos_sb_info *sbi)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	return sbi->fat_bits == 32;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* Maximum number of clusters */
max_fat(struct super_block * sb)165*4882a593Smuzhiyun static inline u32 max_fat(struct super_block *sb)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return is_fat32(sbi) ? MAX_FAT32 :
170*4882a593Smuzhiyun 		is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
MSDOS_I(struct inode * inode)173*4882a593Smuzhiyun static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	return container_of(inode, struct msdos_inode_info, vfs_inode);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun  * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
180*4882a593Smuzhiyun  * save ATTR_RO instead of ->i_mode.
181*4882a593Smuzhiyun  *
182*4882a593Smuzhiyun  * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
183*4882a593Smuzhiyun  * bit, it's just used as flag for app.
184*4882a593Smuzhiyun  */
fat_mode_can_hold_ro(struct inode * inode)185*4882a593Smuzhiyun static inline int fat_mode_can_hold_ro(struct inode *inode)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
188*4882a593Smuzhiyun 	umode_t mask;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode)) {
191*4882a593Smuzhiyun 		if (!sbi->options.rodir)
192*4882a593Smuzhiyun 			return 0;
193*4882a593Smuzhiyun 		mask = ~sbi->options.fs_dmask;
194*4882a593Smuzhiyun 	} else
195*4882a593Smuzhiyun 		mask = ~sbi->options.fs_fmask;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (!(mask & S_IWUGO))
198*4882a593Smuzhiyun 		return 0;
199*4882a593Smuzhiyun 	return 1;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /* Convert attribute bits and a mask to the UNIX mode. */
fat_make_mode(struct msdos_sb_info * sbi,u8 attrs,umode_t mode)203*4882a593Smuzhiyun static inline umode_t fat_make_mode(struct msdos_sb_info *sbi,
204*4882a593Smuzhiyun 				   u8 attrs, umode_t mode)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	if (attrs & ATTR_RO && !((attrs & ATTR_DIR) && !sbi->options.rodir))
207*4882a593Smuzhiyun 		mode &= ~S_IWUGO;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (attrs & ATTR_DIR)
210*4882a593Smuzhiyun 		return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
211*4882a593Smuzhiyun 	else
212*4882a593Smuzhiyun 		return (mode & ~sbi->options.fs_fmask) | S_IFREG;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /* Return the FAT attribute byte for this inode */
fat_make_attrs(struct inode * inode)216*4882a593Smuzhiyun static inline u8 fat_make_attrs(struct inode *inode)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	u8 attrs = MSDOS_I(inode)->i_attrs;
219*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode))
220*4882a593Smuzhiyun 		attrs |= ATTR_DIR;
221*4882a593Smuzhiyun 	if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO))
222*4882a593Smuzhiyun 		attrs |= ATTR_RO;
223*4882a593Smuzhiyun 	return attrs;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
fat_save_attrs(struct inode * inode,u8 attrs)226*4882a593Smuzhiyun static inline void fat_save_attrs(struct inode *inode, u8 attrs)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	if (fat_mode_can_hold_ro(inode))
229*4882a593Smuzhiyun 		MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED;
230*4882a593Smuzhiyun 	else
231*4882a593Smuzhiyun 		MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
fat_checksum(const __u8 * name)234*4882a593Smuzhiyun static inline unsigned char fat_checksum(const __u8 *name)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	unsigned char s = name[0];
237*4882a593Smuzhiyun 	s = (s<<7) + (s>>1) + name[1];	s = (s<<7) + (s>>1) + name[2];
238*4882a593Smuzhiyun 	s = (s<<7) + (s>>1) + name[3];	s = (s<<7) + (s>>1) + name[4];
239*4882a593Smuzhiyun 	s = (s<<7) + (s>>1) + name[5];	s = (s<<7) + (s>>1) + name[6];
240*4882a593Smuzhiyun 	s = (s<<7) + (s>>1) + name[7];	s = (s<<7) + (s>>1) + name[8];
241*4882a593Smuzhiyun 	s = (s<<7) + (s>>1) + name[9];	s = (s<<7) + (s>>1) + name[10];
242*4882a593Smuzhiyun 	return s;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
fat_clus_to_blknr(struct msdos_sb_info * sbi,int clus)245*4882a593Smuzhiyun static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
248*4882a593Smuzhiyun 		+ sbi->data_start;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
fat_get_blknr_offset(struct msdos_sb_info * sbi,loff_t i_pos,sector_t * blknr,int * offset)251*4882a593Smuzhiyun static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
252*4882a593Smuzhiyun 				loff_t i_pos, sector_t *blknr, int *offset)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	*blknr = i_pos >> sbi->dir_per_block_bits;
255*4882a593Smuzhiyun 	*offset = i_pos & (sbi->dir_per_block - 1);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
fat_i_pos_read(struct msdos_sb_info * sbi,struct inode * inode)258*4882a593Smuzhiyun static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
259*4882a593Smuzhiyun 					struct inode *inode)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	loff_t i_pos;
262*4882a593Smuzhiyun #if BITS_PER_LONG == 32
263*4882a593Smuzhiyun 	spin_lock(&sbi->inode_hash_lock);
264*4882a593Smuzhiyun #endif
265*4882a593Smuzhiyun 	i_pos = MSDOS_I(inode)->i_pos;
266*4882a593Smuzhiyun #if BITS_PER_LONG == 32
267*4882a593Smuzhiyun 	spin_unlock(&sbi->inode_hash_lock);
268*4882a593Smuzhiyun #endif
269*4882a593Smuzhiyun 	return i_pos;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
fat16_towchar(wchar_t * dst,const __u8 * src,size_t len)272*4882a593Smuzhiyun static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun #ifdef __BIG_ENDIAN
275*4882a593Smuzhiyun 	while (len--) {
276*4882a593Smuzhiyun 		*dst++ = src[0] | (src[1] << 8);
277*4882a593Smuzhiyun 		src += 2;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun #else
280*4882a593Smuzhiyun 	memcpy(dst, src, len * 2);
281*4882a593Smuzhiyun #endif
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
fat_get_start(const struct msdos_sb_info * sbi,const struct msdos_dir_entry * de)284*4882a593Smuzhiyun static inline int fat_get_start(const struct msdos_sb_info *sbi,
285*4882a593Smuzhiyun 				const struct msdos_dir_entry *de)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	int cluster = le16_to_cpu(de->start);
288*4882a593Smuzhiyun 	if (is_fat32(sbi))
289*4882a593Smuzhiyun 		cluster |= (le16_to_cpu(de->starthi) << 16);
290*4882a593Smuzhiyun 	return cluster;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
fat_set_start(struct msdos_dir_entry * de,int cluster)293*4882a593Smuzhiyun static inline void fat_set_start(struct msdos_dir_entry *de, int cluster)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	de->start   = cpu_to_le16(cluster);
296*4882a593Smuzhiyun 	de->starthi = cpu_to_le16(cluster >> 16);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
fatwchar_to16(__u8 * dst,const wchar_t * src,size_t len)299*4882a593Smuzhiyun static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun #ifdef __BIG_ENDIAN
302*4882a593Smuzhiyun 	while (len--) {
303*4882a593Smuzhiyun 		dst[0] = *src & 0x00FF;
304*4882a593Smuzhiyun 		dst[1] = (*src & 0xFF00) >> 8;
305*4882a593Smuzhiyun 		dst += 2;
306*4882a593Smuzhiyun 		src++;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun #else
309*4882a593Smuzhiyun 	memcpy(dst, src, len * 2);
310*4882a593Smuzhiyun #endif
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /* fat/cache.c */
314*4882a593Smuzhiyun extern void fat_cache_inval_inode(struct inode *inode);
315*4882a593Smuzhiyun extern int fat_get_cluster(struct inode *inode, int cluster,
316*4882a593Smuzhiyun 			   int *fclus, int *dclus);
317*4882a593Smuzhiyun extern int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
318*4882a593Smuzhiyun 				  sector_t last_block,
319*4882a593Smuzhiyun 				  unsigned long *mapped_blocks, sector_t *bmap);
320*4882a593Smuzhiyun extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
321*4882a593Smuzhiyun 		    unsigned long *mapped_blocks, int create, bool from_bmap);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /* fat/dir.c */
324*4882a593Smuzhiyun extern const struct file_operations fat_dir_operations;
325*4882a593Smuzhiyun extern int fat_search_long(struct inode *inode, const unsigned char *name,
326*4882a593Smuzhiyun 			   int name_len, struct fat_slot_info *sinfo);
327*4882a593Smuzhiyun extern int fat_dir_empty(struct inode *dir);
328*4882a593Smuzhiyun extern int fat_subdirs(struct inode *dir);
329*4882a593Smuzhiyun extern int fat_scan(struct inode *dir, const unsigned char *name,
330*4882a593Smuzhiyun 		    struct fat_slot_info *sinfo);
331*4882a593Smuzhiyun extern int fat_scan_logstart(struct inode *dir, int i_logstart,
332*4882a593Smuzhiyun 			     struct fat_slot_info *sinfo);
333*4882a593Smuzhiyun extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
334*4882a593Smuzhiyun 				struct msdos_dir_entry **de);
335*4882a593Smuzhiyun extern int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts);
336*4882a593Smuzhiyun extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
337*4882a593Smuzhiyun 			   struct fat_slot_info *sinfo);
338*4882a593Smuzhiyun extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /* fat/fatent.c */
341*4882a593Smuzhiyun struct fat_entry {
342*4882a593Smuzhiyun 	int entry;
343*4882a593Smuzhiyun 	union {
344*4882a593Smuzhiyun 		u8 *ent12_p[2];
345*4882a593Smuzhiyun 		__le16 *ent16_p;
346*4882a593Smuzhiyun 		__le32 *ent32_p;
347*4882a593Smuzhiyun 	} u;
348*4882a593Smuzhiyun 	int nr_bhs;
349*4882a593Smuzhiyun 	struct buffer_head *bhs[2];
350*4882a593Smuzhiyun 	struct inode *fat_inode;
351*4882a593Smuzhiyun };
352*4882a593Smuzhiyun 
fatent_init(struct fat_entry * fatent)353*4882a593Smuzhiyun static inline void fatent_init(struct fat_entry *fatent)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	fatent->nr_bhs = 0;
356*4882a593Smuzhiyun 	fatent->entry = 0;
357*4882a593Smuzhiyun 	fatent->u.ent32_p = NULL;
358*4882a593Smuzhiyun 	fatent->bhs[0] = fatent->bhs[1] = NULL;
359*4882a593Smuzhiyun 	fatent->fat_inode = NULL;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
fatent_set_entry(struct fat_entry * fatent,int entry)362*4882a593Smuzhiyun static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	fatent->entry = entry;
365*4882a593Smuzhiyun 	fatent->u.ent32_p = NULL;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
fatent_brelse(struct fat_entry * fatent)368*4882a593Smuzhiyun static inline void fatent_brelse(struct fat_entry *fatent)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	int i;
371*4882a593Smuzhiyun 	fatent->u.ent32_p = NULL;
372*4882a593Smuzhiyun 	for (i = 0; i < fatent->nr_bhs; i++)
373*4882a593Smuzhiyun 		brelse(fatent->bhs[i]);
374*4882a593Smuzhiyun 	fatent->nr_bhs = 0;
375*4882a593Smuzhiyun 	fatent->bhs[0] = fatent->bhs[1] = NULL;
376*4882a593Smuzhiyun 	fatent->fat_inode = NULL;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
fat_valid_entry(struct msdos_sb_info * sbi,int entry)379*4882a593Smuzhiyun static inline bool fat_valid_entry(struct msdos_sb_info *sbi, int entry)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	return FAT_START_ENT <= entry && entry < sbi->max_cluster;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun extern void fat_ent_access_init(struct super_block *sb);
385*4882a593Smuzhiyun extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
386*4882a593Smuzhiyun 			int entry);
387*4882a593Smuzhiyun extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
388*4882a593Smuzhiyun 			 int new, int wait);
389*4882a593Smuzhiyun extern int fat_alloc_clusters(struct inode *inode, int *cluster,
390*4882a593Smuzhiyun 			      int nr_cluster);
391*4882a593Smuzhiyun extern int fat_free_clusters(struct inode *inode, int cluster);
392*4882a593Smuzhiyun extern int fat_count_free_clusters(struct super_block *sb);
393*4882a593Smuzhiyun extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /* fat/file.c */
396*4882a593Smuzhiyun extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
397*4882a593Smuzhiyun 			      unsigned long arg);
398*4882a593Smuzhiyun extern const struct file_operations fat_file_operations;
399*4882a593Smuzhiyun extern const struct inode_operations fat_file_inode_operations;
400*4882a593Smuzhiyun extern int fat_setattr(struct dentry *dentry, struct iattr *attr);
401*4882a593Smuzhiyun extern void fat_truncate_blocks(struct inode *inode, loff_t offset);
402*4882a593Smuzhiyun extern int fat_getattr(const struct path *path, struct kstat *stat,
403*4882a593Smuzhiyun 		       u32 request_mask, unsigned int flags);
404*4882a593Smuzhiyun extern int fat_file_fsync(struct file *file, loff_t start, loff_t end,
405*4882a593Smuzhiyun 			  int datasync);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun /* fat/inode.c */
408*4882a593Smuzhiyun extern int fat_block_truncate_page(struct inode *inode, loff_t from);
409*4882a593Smuzhiyun extern void fat_attach(struct inode *inode, loff_t i_pos);
410*4882a593Smuzhiyun extern void fat_detach(struct inode *inode);
411*4882a593Smuzhiyun extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
412*4882a593Smuzhiyun extern struct inode *fat_build_inode(struct super_block *sb,
413*4882a593Smuzhiyun 			struct msdos_dir_entry *de, loff_t i_pos);
414*4882a593Smuzhiyun extern int fat_sync_inode(struct inode *inode);
415*4882a593Smuzhiyun extern int fat_fill_super(struct super_block *sb, void *data, int silent,
416*4882a593Smuzhiyun 			  int isvfat, void (*setup)(struct super_block *));
417*4882a593Smuzhiyun extern int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
420*4882a593Smuzhiyun 			    struct inode *i2);
fat_dir_hash(int logstart)421*4882a593Smuzhiyun static inline unsigned long fat_dir_hash(int logstart)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	return hash_32(logstart, FAT_HASH_BITS);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun extern int fat_add_cluster(struct inode *inode);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun /* fat/misc.c */
428*4882a593Smuzhiyun extern __printf(3, 4) __cold
429*4882a593Smuzhiyun void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...);
430*4882a593Smuzhiyun #define fat_fs_error(sb, fmt, args...)		\
431*4882a593Smuzhiyun 	__fat_fs_error(sb, 1, fmt , ## args)
432*4882a593Smuzhiyun #define fat_fs_error_ratelimit(sb, fmt, args...) \
433*4882a593Smuzhiyun 	__fat_fs_error(sb, __ratelimit(&MSDOS_SB(sb)->ratelimit), fmt , ## args)
434*4882a593Smuzhiyun __printf(3, 4) __cold
435*4882a593Smuzhiyun void fat_msg(struct super_block *sb, const char *level, const char *fmt, ...);
436*4882a593Smuzhiyun #define fat_msg_ratelimit(sb, level, fmt, args...)	\
437*4882a593Smuzhiyun 	do {	\
438*4882a593Smuzhiyun 			if (__ratelimit(&MSDOS_SB(sb)->ratelimit))	\
439*4882a593Smuzhiyun 				fat_msg(sb, level, fmt, ## args);	\
440*4882a593Smuzhiyun 	 } while (0)
441*4882a593Smuzhiyun extern int fat_clusters_flush(struct super_block *sb);
442*4882a593Smuzhiyun extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
443*4882a593Smuzhiyun extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
444*4882a593Smuzhiyun 			      __le16 __time, __le16 __date, u8 time_cs);
445*4882a593Smuzhiyun extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
446*4882a593Smuzhiyun 			      __le16 *time, __le16 *date, u8 *time_cs);
447*4882a593Smuzhiyun extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
448*4882a593Smuzhiyun 			     int flags);
449*4882a593Smuzhiyun extern int fat_update_time(struct inode *inode, struct timespec64 *now,
450*4882a593Smuzhiyun 			   int flags);
451*4882a593Smuzhiyun extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun int fat_cache_init(void);
454*4882a593Smuzhiyun void fat_cache_destroy(void);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /* fat/nfs.c */
457*4882a593Smuzhiyun extern const struct export_operations fat_export_ops;
458*4882a593Smuzhiyun extern const struct export_operations fat_export_ops_nostale;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun /* helper for printk */
461*4882a593Smuzhiyun typedef unsigned long long	llu;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun #endif /* !_FAT_H */
464