xref: /linux/fs/fat/fat.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
29e975daeSOGAWA Hirofumi #ifndef _FAT_H
39e975daeSOGAWA Hirofumi #define _FAT_H
49e975daeSOGAWA Hirofumi 
59e975daeSOGAWA Hirofumi #include <linux/buffer_head.h>
69e975daeSOGAWA Hirofumi #include <linux/nls.h>
77669e8fbSSteven J. Magnani #include <linux/hash.h>
8aaa04b48SOGAWA Hirofumi #include <linux/ratelimit.h>
99e975daeSOGAWA Hirofumi #include <linux/msdos_fs.h>
10*634440b6SEric Sandeen #include <linux/fs_context.h>
11*634440b6SEric Sandeen #include <linux/fs_parser.h>
129e975daeSOGAWA Hirofumi 
139e975daeSOGAWA Hirofumi /*
149e975daeSOGAWA Hirofumi  * vfat shortname flags
159e975daeSOGAWA Hirofumi  */
169e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_LOWER	0x0001 /* convert to lowercase for display */
179e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_WIN95	0x0002 /* emulate win95 rule for display */
189e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_WINNT	0x0004 /* emulate winnt rule for display */
199e975daeSOGAWA Hirofumi #define VFAT_SFN_CREATE_WIN95	0x0100 /* emulate win95 rule for create */
209e975daeSOGAWA Hirofumi #define VFAT_SFN_CREATE_WINNT	0x0200 /* emulate winnt rule for create */
219e975daeSOGAWA Hirofumi 
2285c78591SDenis Karpov #define FAT_ERRORS_CONT		1      /* ignore error and continue */
2385c78591SDenis Karpov #define FAT_ERRORS_PANIC	2      /* panic on error */
2485c78591SDenis Karpov #define FAT_ERRORS_RO		3      /* remount r/o on error */
2585c78591SDenis Karpov 
262628b7a6SNamjae Jeon #define FAT_NFS_STALE_RW	1      /* NFS RW support, can cause ESTALE */
272628b7a6SNamjae Jeon #define FAT_NFS_NOSTALE_RO	2      /* NFS RO support, no ESTALE issue */
282628b7a6SNamjae Jeon 
299e975daeSOGAWA Hirofumi struct fat_mount_options {
30170782ebSEric W. Biederman 	kuid_t fs_uid;
31170782ebSEric W. Biederman 	kgid_t fs_gid;
329e975daeSOGAWA Hirofumi 	unsigned short fs_fmask;
339e975daeSOGAWA Hirofumi 	unsigned short fs_dmask;
349e975daeSOGAWA Hirofumi 	unsigned short codepage;   /* Codepage for shortname conversions */
3558156c8fSJan Kara 	int time_offset;	   /* Offset of timestamps from UTC (in minutes) */
369e975daeSOGAWA Hirofumi 	char *iocharset;           /* Charset used for filename input/display */
379e975daeSOGAWA Hirofumi 	unsigned short shortname;  /* flags for shortname display/create rule */
389e975daeSOGAWA Hirofumi 	unsigned char name_check;  /* r = relaxed, n = normal, s = strict */
3985c78591SDenis Karpov 	unsigned char errors;	   /* On error: continue, panic, remount-ro */
402628b7a6SNamjae Jeon 	unsigned char nfs;	  /* NFS support: nostale_ro, stale_rw */
419e975daeSOGAWA Hirofumi 	unsigned short allow_utime;/* permission for setting the [am]time */
429e975daeSOGAWA Hirofumi 	unsigned quiet:1,          /* set = fake successful chmods and chowns */
439e975daeSOGAWA Hirofumi 		 showexec:1,       /* set = only set x bit for com/exe/bat */
449e975daeSOGAWA Hirofumi 		 sys_immutable:1,  /* set = system files are immutable */
459e975daeSOGAWA Hirofumi 		 dotsOK:1,         /* set = hidden and system files are named '.filename' */
469e975daeSOGAWA Hirofumi 		 isvfat:1,         /* 0=no vfat long filename support, 1=vfat support */
479e975daeSOGAWA Hirofumi 		 utf8:1,	   /* Use of UTF-8 character set (Default) */
489e975daeSOGAWA Hirofumi 		 unicode_xlate:1,  /* create escape sequences for unhandled Unicode */
499e975daeSOGAWA Hirofumi 		 numtail:1,        /* Does first alias have a numeric '~1' type tail? */
509e975daeSOGAWA Hirofumi 		 flush:1,	   /* write things quickly */
519e975daeSOGAWA Hirofumi 		 nocase:1,	   /* Does this need case conversion? 0=need case conversion*/
529e975daeSOGAWA Hirofumi 		 usefree:1,	   /* Use free_clusters for FAT32 */
5358156c8fSJan Kara 		 tz_set:1,	   /* Filesystem timestamps' offset set */
54681142f9SChristoph Hellwig 		 rodir:1,	   /* allow ATTR_RO for directory */
55190a8843SConrad Meyer 		 discard:1,	   /* Issue discard requests on deletions */
56206d3d8eSEric Sandeen 		 dos1xfloppy:1,	   /* Assume default BPB for DOS 1.x floppies */
57206d3d8eSEric Sandeen 		 debug:1;	   /* Not currently used */
589e975daeSOGAWA Hirofumi };
599e975daeSOGAWA Hirofumi 
609e975daeSOGAWA Hirofumi #define FAT_HASH_BITS	8
619e975daeSOGAWA Hirofumi #define FAT_HASH_SIZE	(1UL << FAT_HASH_BITS)
629e975daeSOGAWA Hirofumi 
639e975daeSOGAWA Hirofumi /*
649e975daeSOGAWA Hirofumi  * MS-DOS file system in-core superblock data
659e975daeSOGAWA Hirofumi  */
669e975daeSOGAWA Hirofumi struct msdos_sb_info {
679e975daeSOGAWA Hirofumi 	unsigned short sec_per_clus;  /* sectors/cluster */
689e975daeSOGAWA Hirofumi 	unsigned short cluster_bits;  /* log2(cluster_size) */
699e975daeSOGAWA Hirofumi 	unsigned int cluster_size;    /* cluster size */
708de560deSAlexander Kuleshov 	unsigned char fats, fat_bits; /* number of FATs, FAT bits (12,16 or 32) */
719e975daeSOGAWA Hirofumi 	unsigned short fat_start;
729e975daeSOGAWA Hirofumi 	unsigned long fat_length;     /* FAT start & length (sec.) */
739e975daeSOGAWA Hirofumi 	unsigned long dir_start;
749e975daeSOGAWA Hirofumi 	unsigned short dir_entries;   /* root dir start & entries */
759e975daeSOGAWA Hirofumi 	unsigned long data_start;     /* first data sector */
769e975daeSOGAWA Hirofumi 	unsigned long max_cluster;    /* maximum cluster number */
779e975daeSOGAWA Hirofumi 	unsigned long root_cluster;   /* first cluster of the root directory */
789e975daeSOGAWA Hirofumi 	unsigned long fsinfo_sector;  /* sector number of FAT32 fsinfo */
799e975daeSOGAWA Hirofumi 	struct mutex fat_lock;
808fceb4e0SNamjae Jeon 	struct mutex nfs_build_inode_lock;
81e40b34c7SMarco Stornelli 	struct mutex s_lock;
829e975daeSOGAWA Hirofumi 	unsigned int prev_free;      /* previously allocated cluster number */
839e975daeSOGAWA Hirofumi 	unsigned int free_clusters;  /* -1 if undefined */
849e975daeSOGAWA Hirofumi 	unsigned int free_clus_valid; /* is free_clusters valid? */
859e975daeSOGAWA Hirofumi 	struct fat_mount_options options;
869e975daeSOGAWA Hirofumi 	struct nls_table *nls_disk;   /* Codepage used on disk */
879e975daeSOGAWA Hirofumi 	struct nls_table *nls_io;     /* Charset used for input and display */
889e975daeSOGAWA Hirofumi 	const void *dir_ops;	      /* Opaque; default directory operations */
899e975daeSOGAWA Hirofumi 	int dir_per_block;	      /* dir entries per block */
909e975daeSOGAWA Hirofumi 	int dir_per_block_bits;	      /* log2(dir_per_block) */
916e5b93eeSMike Lockwood 	unsigned int vol_id;		/*volume ID*/
929e975daeSOGAWA Hirofumi 
939e975daeSOGAWA Hirofumi 	int fatent_shift;
948992de4cSJulia Lawall 	const struct fatent_operations *fatent_ops;
95b522412aSAl Viro 	struct inode *fat_inode;
96020ac5b6SArtem Bityutskiy 	struct inode *fsinfo_inode;
979e975daeSOGAWA Hirofumi 
98aaa04b48SOGAWA Hirofumi 	struct ratelimit_state ratelimit;
99aaa04b48SOGAWA Hirofumi 
1009e975daeSOGAWA Hirofumi 	spinlock_t inode_hash_lock;
1019e975daeSOGAWA Hirofumi 	struct hlist_head inode_hashtable[FAT_HASH_SIZE];
1027669e8fbSSteven J. Magnani 
1037669e8fbSSteven J. Magnani 	spinlock_t dir_hash_lock;
1047669e8fbSSteven J. Magnani 	struct hlist_head dir_hashtable[FAT_HASH_SIZE];
105b88a1058SOleksij Rempel 
106b88a1058SOleksij Rempel 	unsigned int dirty;           /* fs state before mount */
107cac45b06SAl Viro 	struct rcu_head rcu;
1089e975daeSOGAWA Hirofumi };
1099e975daeSOGAWA Hirofumi 
1109e975daeSOGAWA Hirofumi #define FAT_CACHE_VALID	0	/* special case for valid cache */
1119e975daeSOGAWA Hirofumi 
1129e975daeSOGAWA Hirofumi /*
1139e975daeSOGAWA Hirofumi  * MS-DOS file system inode data in memory
1149e975daeSOGAWA Hirofumi  */
1159e975daeSOGAWA Hirofumi struct msdos_inode_info {
1169e975daeSOGAWA Hirofumi 	spinlock_t cache_lru_lock;
1179e975daeSOGAWA Hirofumi 	struct list_head cache_lru;
1189e975daeSOGAWA Hirofumi 	int nr_caches;
1199e975daeSOGAWA Hirofumi 	/* for avoiding the race between fat_free() and fat_get_cluster() */
1209e975daeSOGAWA Hirofumi 	unsigned int cache_valid_id;
1219e975daeSOGAWA Hirofumi 
1222bdf67ebSOGAWA Hirofumi 	/* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */
1232bdf67ebSOGAWA Hirofumi 	loff_t mmu_private;	/* physically allocated size */
1242bdf67ebSOGAWA Hirofumi 
1259e975daeSOGAWA Hirofumi 	int i_start;		/* first cluster or 0 */
1269e975daeSOGAWA Hirofumi 	int i_logstart;		/* logical first cluster */
1279e975daeSOGAWA Hirofumi 	int i_attrs;		/* unused attribute bits */
1289e975daeSOGAWA Hirofumi 	loff_t i_pos;		/* on-disk position of directory entry or 0 */
1299e975daeSOGAWA Hirofumi 	struct hlist_node i_fat_hash;	/* hash by i_location */
1307669e8fbSSteven J. Magnani 	struct hlist_node i_dir_hash;	/* hash by i_logstart */
13158268691SChristoph Hellwig 	struct rw_semaphore truncate_lock; /* protect bmap against truncate */
13230abce05SChung-Chiang Cheng 	struct timespec64 i_crtime;	/* File creation (birth) time */
1339e975daeSOGAWA Hirofumi 	struct inode vfs_inode;
1349e975daeSOGAWA Hirofumi };
1359e975daeSOGAWA Hirofumi 
1369e975daeSOGAWA Hirofumi struct fat_slot_info {
1379e975daeSOGAWA Hirofumi 	loff_t i_pos;		/* on-disk position of directory entry */
1389e975daeSOGAWA Hirofumi 	loff_t slot_off;	/* offset for slot or de start */
1399e975daeSOGAWA Hirofumi 	int nr_slots;		/* number of slots + 1(de) in filename */
1409e975daeSOGAWA Hirofumi 	struct msdos_dir_entry *de;
1419e975daeSOGAWA Hirofumi 	struct buffer_head *bh;
1429e975daeSOGAWA Hirofumi };
1439e975daeSOGAWA Hirofumi 
MSDOS_SB(struct super_block * sb)1449e975daeSOGAWA Hirofumi static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
1459e975daeSOGAWA Hirofumi {
1469e975daeSOGAWA Hirofumi 	return sb->s_fs_info;
1479e975daeSOGAWA Hirofumi }
1489e975daeSOGAWA Hirofumi 
149306790f7SCarmeli Tamir /*
150306790f7SCarmeli Tamir  * Functions that determine the variant of the FAT file system (i.e.,
151306790f7SCarmeli Tamir  * whether this is FAT12, FAT16 or FAT32.
152306790f7SCarmeli Tamir  */
is_fat12(const struct msdos_sb_info * sbi)153306790f7SCarmeli Tamir static inline bool is_fat12(const struct msdos_sb_info *sbi)
154306790f7SCarmeli Tamir {
155306790f7SCarmeli Tamir 	return sbi->fat_bits == 12;
156306790f7SCarmeli Tamir }
157306790f7SCarmeli Tamir 
is_fat16(const struct msdos_sb_info * sbi)158306790f7SCarmeli Tamir static inline bool is_fat16(const struct msdos_sb_info *sbi)
159306790f7SCarmeli Tamir {
160306790f7SCarmeli Tamir 	return sbi->fat_bits == 16;
161306790f7SCarmeli Tamir }
162306790f7SCarmeli Tamir 
is_fat32(const struct msdos_sb_info * sbi)163306790f7SCarmeli Tamir static inline bool is_fat32(const struct msdos_sb_info *sbi)
164306790f7SCarmeli Tamir {
165306790f7SCarmeli Tamir 	return sbi->fat_bits == 32;
166306790f7SCarmeli Tamir }
167306790f7SCarmeli Tamir 
168d19dc016SCarmeli Tamir /* Maximum number of clusters */
max_fat(struct super_block * sb)169d19dc016SCarmeli Tamir static inline u32 max_fat(struct super_block *sb)
170d19dc016SCarmeli Tamir {
171d19dc016SCarmeli Tamir 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
172d19dc016SCarmeli Tamir 
173306790f7SCarmeli Tamir 	return is_fat32(sbi) ? MAX_FAT32 :
174306790f7SCarmeli Tamir 		is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
175d19dc016SCarmeli Tamir }
176d19dc016SCarmeli Tamir 
MSDOS_I(struct inode * inode)1779e975daeSOGAWA Hirofumi static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
1789e975daeSOGAWA Hirofumi {
1799e975daeSOGAWA Hirofumi 	return container_of(inode, struct msdos_inode_info, vfs_inode);
1809e975daeSOGAWA Hirofumi }
1819e975daeSOGAWA Hirofumi 
1829183482fSOGAWA Hirofumi /*
1839183482fSOGAWA Hirofumi  * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
1849183482fSOGAWA Hirofumi  * save ATTR_RO instead of ->i_mode.
185dfc209c0SOGAWA Hirofumi  *
186dfc209c0SOGAWA Hirofumi  * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
187dfc209c0SOGAWA Hirofumi  * bit, it's just used as flag for app.
1889183482fSOGAWA Hirofumi  */
fat_mode_can_hold_ro(struct inode * inode)1899183482fSOGAWA Hirofumi static inline int fat_mode_can_hold_ro(struct inode *inode)
1909183482fSOGAWA Hirofumi {
1919183482fSOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
192dacd0e7bSAl Viro 	umode_t mask;
1939183482fSOGAWA Hirofumi 
194dfc209c0SOGAWA Hirofumi 	if (S_ISDIR(inode->i_mode)) {
195dfc209c0SOGAWA Hirofumi 		if (!sbi->options.rodir)
196dfc209c0SOGAWA Hirofumi 			return 0;
1979183482fSOGAWA Hirofumi 		mask = ~sbi->options.fs_dmask;
198dfc209c0SOGAWA Hirofumi 	} else
1999183482fSOGAWA Hirofumi 		mask = ~sbi->options.fs_fmask;
2009183482fSOGAWA Hirofumi 
2019183482fSOGAWA Hirofumi 	if (!(mask & S_IWUGO))
2029183482fSOGAWA Hirofumi 		return 0;
2039183482fSOGAWA Hirofumi 	return 1;
2049183482fSOGAWA Hirofumi }
2059183482fSOGAWA Hirofumi 
2069c0aa1b8SOGAWA Hirofumi /* Convert attribute bits and a mask to the UNIX mode. */
fat_make_mode(struct msdos_sb_info * sbi,u8 attrs,umode_t mode)207dacd0e7bSAl Viro static inline umode_t fat_make_mode(struct msdos_sb_info *sbi,
208dacd0e7bSAl Viro 				   u8 attrs, umode_t mode)
2099c0aa1b8SOGAWA Hirofumi {
210dfc209c0SOGAWA Hirofumi 	if (attrs & ATTR_RO && !((attrs & ATTR_DIR) && !sbi->options.rodir))
2119c0aa1b8SOGAWA Hirofumi 		mode &= ~S_IWUGO;
2129c0aa1b8SOGAWA Hirofumi 
2139c0aa1b8SOGAWA Hirofumi 	if (attrs & ATTR_DIR)
2149c0aa1b8SOGAWA Hirofumi 		return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
2159c0aa1b8SOGAWA Hirofumi 	else
2169c0aa1b8SOGAWA Hirofumi 		return (mode & ~sbi->options.fs_fmask) | S_IFREG;
2179c0aa1b8SOGAWA Hirofumi }
2189c0aa1b8SOGAWA Hirofumi 
2199e975daeSOGAWA Hirofumi /* Return the FAT attribute byte for this inode */
fat_make_attrs(struct inode * inode)2209c0aa1b8SOGAWA Hirofumi static inline u8 fat_make_attrs(struct inode *inode)
2219e975daeSOGAWA Hirofumi {
2229183482fSOGAWA Hirofumi 	u8 attrs = MSDOS_I(inode)->i_attrs;
2239183482fSOGAWA Hirofumi 	if (S_ISDIR(inode->i_mode))
2249183482fSOGAWA Hirofumi 		attrs |= ATTR_DIR;
2259183482fSOGAWA Hirofumi 	if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO))
2269183482fSOGAWA Hirofumi 		attrs |= ATTR_RO;
2279183482fSOGAWA Hirofumi 	return attrs;
2289e975daeSOGAWA Hirofumi }
2299e975daeSOGAWA Hirofumi 
fat_save_attrs(struct inode * inode,u8 attrs)2309c0aa1b8SOGAWA Hirofumi static inline void fat_save_attrs(struct inode *inode, u8 attrs)
2319c0aa1b8SOGAWA Hirofumi {
2329183482fSOGAWA Hirofumi 	if (fat_mode_can_hold_ro(inode))
2339c0aa1b8SOGAWA Hirofumi 		MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED;
2349183482fSOGAWA Hirofumi 	else
2359183482fSOGAWA Hirofumi 		MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO);
2369c0aa1b8SOGAWA Hirofumi }
2379c0aa1b8SOGAWA Hirofumi 
fat_checksum(const __u8 * name)2389e975daeSOGAWA Hirofumi static inline unsigned char fat_checksum(const __u8 *name)
2399e975daeSOGAWA Hirofumi {
2409e975daeSOGAWA Hirofumi 	unsigned char s = name[0];
2419e975daeSOGAWA Hirofumi 	s = (s<<7) + (s>>1) + name[1];	s = (s<<7) + (s>>1) + name[2];
2429e975daeSOGAWA Hirofumi 	s = (s<<7) + (s>>1) + name[3];	s = (s<<7) + (s>>1) + name[4];
2439e975daeSOGAWA Hirofumi 	s = (s<<7) + (s>>1) + name[5];	s = (s<<7) + (s>>1) + name[6];
2449e975daeSOGAWA Hirofumi 	s = (s<<7) + (s>>1) + name[7];	s = (s<<7) + (s>>1) + name[8];
2459e975daeSOGAWA Hirofumi 	s = (s<<7) + (s>>1) + name[9];	s = (s<<7) + (s>>1) + name[10];
2469e975daeSOGAWA Hirofumi 	return s;
2479e975daeSOGAWA Hirofumi }
2489e975daeSOGAWA Hirofumi 
fat_clus_to_blknr(struct msdos_sb_info * sbi,int clus)2499e975daeSOGAWA Hirofumi static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
2509e975daeSOGAWA Hirofumi {
2519e975daeSOGAWA Hirofumi 	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
2529e975daeSOGAWA Hirofumi 		+ sbi->data_start;
2539e975daeSOGAWA Hirofumi }
2549e975daeSOGAWA Hirofumi 
fat_get_blknr_offset(struct msdos_sb_info * sbi,loff_t i_pos,sector_t * blknr,int * offset)255e22a4442SNamjae Jeon static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
256e22a4442SNamjae Jeon 				loff_t i_pos, sector_t *blknr, int *offset)
257e22a4442SNamjae Jeon {
258e22a4442SNamjae Jeon 	*blknr = i_pos >> sbi->dir_per_block_bits;
259e22a4442SNamjae Jeon 	*offset = i_pos & (sbi->dir_per_block - 1);
260e22a4442SNamjae Jeon }
261e22a4442SNamjae Jeon 
fat_i_pos_read(struct msdos_sb_info * sbi,struct inode * inode)262f21735d5SNamjae Jeon static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
263f21735d5SNamjae Jeon 					struct inode *inode)
264f21735d5SNamjae Jeon {
265f21735d5SNamjae Jeon 	loff_t i_pos;
266f21735d5SNamjae Jeon #if BITS_PER_LONG == 32
267f21735d5SNamjae Jeon 	spin_lock(&sbi->inode_hash_lock);
268f21735d5SNamjae Jeon #endif
269f21735d5SNamjae Jeon 	i_pos = MSDOS_I(inode)->i_pos;
270f21735d5SNamjae Jeon #if BITS_PER_LONG == 32
271f21735d5SNamjae Jeon 	spin_unlock(&sbi->inode_hash_lock);
272f21735d5SNamjae Jeon #endif
273f21735d5SNamjae Jeon 	return i_pos;
274f21735d5SNamjae Jeon }
275f21735d5SNamjae Jeon 
fat16_towchar(wchar_t * dst,const __u8 * src,size_t len)2769e975daeSOGAWA Hirofumi static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
2779e975daeSOGAWA Hirofumi {
2789e975daeSOGAWA Hirofumi #ifdef __BIG_ENDIAN
2799e975daeSOGAWA Hirofumi 	while (len--) {
2809e975daeSOGAWA Hirofumi 		*dst++ = src[0] | (src[1] << 8);
2819e975daeSOGAWA Hirofumi 		src += 2;
2829e975daeSOGAWA Hirofumi 	}
2839e975daeSOGAWA Hirofumi #else
2849e975daeSOGAWA Hirofumi 	memcpy(dst, src, len * 2);
2859e975daeSOGAWA Hirofumi #endif
2869e975daeSOGAWA Hirofumi }
2879e975daeSOGAWA Hirofumi 
fat_get_start(const struct msdos_sb_info * sbi,const struct msdos_dir_entry * de)288a943ed71SSteven J. Magnani static inline int fat_get_start(const struct msdos_sb_info *sbi,
289a943ed71SSteven J. Magnani 				const struct msdos_dir_entry *de)
290a943ed71SSteven J. Magnani {
291a943ed71SSteven J. Magnani 	int cluster = le16_to_cpu(de->start);
292306790f7SCarmeli Tamir 	if (is_fat32(sbi))
293a943ed71SSteven J. Magnani 		cluster |= (le16_to_cpu(de->starthi) << 16);
294a943ed71SSteven J. Magnani 	return cluster;
295a943ed71SSteven J. Magnani }
296a943ed71SSteven J. Magnani 
fat_set_start(struct msdos_dir_entry * de,int cluster)297a943ed71SSteven J. Magnani static inline void fat_set_start(struct msdos_dir_entry *de, int cluster)
298a943ed71SSteven J. Magnani {
299a943ed71SSteven J. Magnani 	de->start   = cpu_to_le16(cluster);
300a943ed71SSteven J. Magnani 	de->starthi = cpu_to_le16(cluster >> 16);
301a943ed71SSteven J. Magnani }
302a943ed71SSteven J. Magnani 
fatwchar_to16(__u8 * dst,const wchar_t * src,size_t len)3039e975daeSOGAWA Hirofumi static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
3049e975daeSOGAWA Hirofumi {
3059e975daeSOGAWA Hirofumi #ifdef __BIG_ENDIAN
3069e975daeSOGAWA Hirofumi 	while (len--) {
3079e975daeSOGAWA Hirofumi 		dst[0] = *src & 0x00FF;
3089e975daeSOGAWA Hirofumi 		dst[1] = (*src & 0xFF00) >> 8;
3099e975daeSOGAWA Hirofumi 		dst += 2;
3109e975daeSOGAWA Hirofumi 		src++;
3119e975daeSOGAWA Hirofumi 	}
3129e975daeSOGAWA Hirofumi #else
3139e975daeSOGAWA Hirofumi 	memcpy(dst, src, len * 2);
3149e975daeSOGAWA Hirofumi #endif
3159e975daeSOGAWA Hirofumi }
3169e975daeSOGAWA Hirofumi 
3179e975daeSOGAWA Hirofumi /* fat/cache.c */
3189e975daeSOGAWA Hirofumi extern void fat_cache_inval_inode(struct inode *inode);
3199e975daeSOGAWA Hirofumi extern int fat_get_cluster(struct inode *inode, int cluster,
3209e975daeSOGAWA Hirofumi 			   int *fclus, int *dclus);
32116fab201SNamjae Jeon extern int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
32216fab201SNamjae Jeon 				  sector_t last_block,
32316fab201SNamjae Jeon 				  unsigned long *mapped_blocks, sector_t *bmap);
3249e975daeSOGAWA Hirofumi extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
32516fab201SNamjae Jeon 		    unsigned long *mapped_blocks, int create, bool from_bmap);
3269e975daeSOGAWA Hirofumi 
3279e975daeSOGAWA Hirofumi /* fat/dir.c */
3289e975daeSOGAWA Hirofumi extern const struct file_operations fat_dir_operations;
3299e975daeSOGAWA Hirofumi extern int fat_search_long(struct inode *inode, const unsigned char *name,
3309e975daeSOGAWA Hirofumi 			   int name_len, struct fat_slot_info *sinfo);
3319e975daeSOGAWA Hirofumi extern int fat_dir_empty(struct inode *dir);
3329e975daeSOGAWA Hirofumi extern int fat_subdirs(struct inode *dir);
3339e975daeSOGAWA Hirofumi extern int fat_scan(struct inode *dir, const unsigned char *name,
3349e975daeSOGAWA Hirofumi 		    struct fat_slot_info *sinfo);
335f1e6fb0aSNamjae Jeon extern int fat_scan_logstart(struct inode *dir, int i_logstart,
336f1e6fb0aSNamjae Jeon 			     struct fat_slot_info *sinfo);
3379e975daeSOGAWA Hirofumi extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
3387669e8fbSSteven J. Magnani 				struct msdos_dir_entry **de);
339f423420cSArnd Bergmann extern int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts);
3409e975daeSOGAWA Hirofumi extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
3419e975daeSOGAWA Hirofumi 			   struct fat_slot_info *sinfo);
3429e975daeSOGAWA Hirofumi extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
3439e975daeSOGAWA Hirofumi 
3449e975daeSOGAWA Hirofumi /* fat/fatent.c */
3459e975daeSOGAWA Hirofumi struct fat_entry {
3469e975daeSOGAWA Hirofumi 	int entry;
3479e975daeSOGAWA Hirofumi 	union {
3489e975daeSOGAWA Hirofumi 		u8 *ent12_p[2];
3499e975daeSOGAWA Hirofumi 		__le16 *ent16_p;
3509e975daeSOGAWA Hirofumi 		__le32 *ent32_p;
3519e975daeSOGAWA Hirofumi 	} u;
3529e975daeSOGAWA Hirofumi 	int nr_bhs;
3539e975daeSOGAWA Hirofumi 	struct buffer_head *bhs[2];
354b522412aSAl Viro 	struct inode *fat_inode;
3559e975daeSOGAWA Hirofumi };
3569e975daeSOGAWA Hirofumi 
fatent_init(struct fat_entry * fatent)3579e975daeSOGAWA Hirofumi static inline void fatent_init(struct fat_entry *fatent)
3589e975daeSOGAWA Hirofumi {
3599e975daeSOGAWA Hirofumi 	fatent->nr_bhs = 0;
3609e975daeSOGAWA Hirofumi 	fatent->entry = 0;
3619e975daeSOGAWA Hirofumi 	fatent->u.ent32_p = NULL;
3629e975daeSOGAWA Hirofumi 	fatent->bhs[0] = fatent->bhs[1] = NULL;
363b522412aSAl Viro 	fatent->fat_inode = NULL;
3649e975daeSOGAWA Hirofumi }
3659e975daeSOGAWA Hirofumi 
fatent_set_entry(struct fat_entry * fatent,int entry)3669e975daeSOGAWA Hirofumi static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
3679e975daeSOGAWA Hirofumi {
3689e975daeSOGAWA Hirofumi 	fatent->entry = entry;
3699e975daeSOGAWA Hirofumi 	fatent->u.ent32_p = NULL;
3709e975daeSOGAWA Hirofumi }
3719e975daeSOGAWA Hirofumi 
fatent_brelse(struct fat_entry * fatent)3729e975daeSOGAWA Hirofumi static inline void fatent_brelse(struct fat_entry *fatent)
3739e975daeSOGAWA Hirofumi {
3749e975daeSOGAWA Hirofumi 	int i;
3759e975daeSOGAWA Hirofumi 	fatent->u.ent32_p = NULL;
3769e975daeSOGAWA Hirofumi 	for (i = 0; i < fatent->nr_bhs; i++)
3779e975daeSOGAWA Hirofumi 		brelse(fatent->bhs[i]);
3789e975daeSOGAWA Hirofumi 	fatent->nr_bhs = 0;
3799e975daeSOGAWA Hirofumi 	fatent->bhs[0] = fatent->bhs[1] = NULL;
380b522412aSAl Viro 	fatent->fat_inode = NULL;
3819e975daeSOGAWA Hirofumi }
3829e975daeSOGAWA Hirofumi 
fat_valid_entry(struct msdos_sb_info * sbi,int entry)3830afa9626SOGAWA Hirofumi static inline bool fat_valid_entry(struct msdos_sb_info *sbi, int entry)
3840afa9626SOGAWA Hirofumi {
3850afa9626SOGAWA Hirofumi 	return FAT_START_ENT <= entry && entry < sbi->max_cluster;
3860afa9626SOGAWA Hirofumi }
3870afa9626SOGAWA Hirofumi 
3889e975daeSOGAWA Hirofumi extern void fat_ent_access_init(struct super_block *sb);
3899e975daeSOGAWA Hirofumi extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
3909e975daeSOGAWA Hirofumi 			int entry);
3919e975daeSOGAWA Hirofumi extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
3929e975daeSOGAWA Hirofumi 			 int new, int wait);
3939e975daeSOGAWA Hirofumi extern int fat_alloc_clusters(struct inode *inode, int *cluster,
3949e975daeSOGAWA Hirofumi 			      int nr_cluster);
3959e975daeSOGAWA Hirofumi extern int fat_free_clusters(struct inode *inode, int cluster);
3969e975daeSOGAWA Hirofumi extern int fat_count_free_clusters(struct super_block *sb);
397f663b5b3SWentao Wang extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
3989e975daeSOGAWA Hirofumi 
3999e975daeSOGAWA Hirofumi /* fat/file.c */
4007845bc3eSArnd Bergmann extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
4017845bc3eSArnd Bergmann 			      unsigned long arg);
4029e975daeSOGAWA Hirofumi extern const struct file_operations fat_file_operations;
4039e975daeSOGAWA Hirofumi extern const struct inode_operations fat_file_inode_operations;
404c1632a0fSChristian Brauner extern int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
405549c7297SChristian Brauner 		       struct iattr *attr);
406459f6ed3Snpiggin@suse.de extern void fat_truncate_blocks(struct inode *inode, loff_t offset);
407b74d24f7SChristian Brauner extern int fat_getattr(struct mnt_idmap *idmap,
408549c7297SChristian Brauner 		       const struct path *path, struct kstat *stat,
409a528d35eSDavid Howells 		       u32 request_mask, unsigned int flags);
41002c24a82SJosef Bacik extern int fat_file_fsync(struct file *file, loff_t start, loff_t end,
41102c24a82SJosef Bacik 			  int datasync);
4129e975daeSOGAWA Hirofumi 
4139e975daeSOGAWA Hirofumi /* fat/inode.c */
414c0ef0cc9SNamjae Jeon extern int fat_block_truncate_page(struct inode *inode, loff_t from);
4159e975daeSOGAWA Hirofumi extern void fat_attach(struct inode *inode, loff_t i_pos);
4169e975daeSOGAWA Hirofumi extern void fat_detach(struct inode *inode);
4179e975daeSOGAWA Hirofumi extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
4189e975daeSOGAWA Hirofumi extern struct inode *fat_build_inode(struct super_block *sb,
4199e975daeSOGAWA Hirofumi 			struct msdos_dir_entry *de, loff_t i_pos);
4209e975daeSOGAWA Hirofumi extern int fat_sync_inode(struct inode *inode);
421*634440b6SEric Sandeen extern int fat_fill_super(struct super_block *sb, struct fs_context *fc,
422*634440b6SEric Sandeen 			  void (*setup)(struct super_block *));
423f1e6fb0aSNamjae Jeon extern int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
4249e975daeSOGAWA Hirofumi 
4259e975daeSOGAWA Hirofumi extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
4269e975daeSOGAWA Hirofumi 			    struct inode *i2);
427*634440b6SEric Sandeen 
428*634440b6SEric Sandeen extern const struct fs_parameter_spec fat_param_spec[];
429*634440b6SEric Sandeen int fat_init_fs_context(struct fs_context *fc, bool is_vfat);
430*634440b6SEric Sandeen void fat_free_fc(struct fs_context *fc);
431*634440b6SEric Sandeen 
432*634440b6SEric Sandeen int fat_parse_param(struct fs_context *fc, struct fs_parameter *param,
433*634440b6SEric Sandeen 		    bool is_vfat);
434*634440b6SEric Sandeen int fat_reconfigure(struct fs_context *fc);
435*634440b6SEric Sandeen 
fat_dir_hash(int logstart)4367669e8fbSSteven J. Magnani static inline unsigned long fat_dir_hash(int logstart)
43721b6633dSSteven J. Magnani {
4387669e8fbSSteven J. Magnani 	return hash_32(logstart, FAT_HASH_BITS);
43921b6633dSSteven J. Magnani }
440b13bb33eSNamjae Jeon extern int fat_add_cluster(struct inode *inode);
44121b6633dSSteven J. Magnani 
4429e975daeSOGAWA Hirofumi /* fat/misc.c */
443b9075fa9SJoe Perches extern __printf(3, 4) __cold
444b9075fa9SJoe Perches void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...);
4452c8a5ffbSAlexey Fisher #define fat_fs_error(sb, fmt, args...)		\
4462c8a5ffbSAlexey Fisher 	__fat_fs_error(sb, 1, fmt , ## args)
4472c8a5ffbSAlexey Fisher #define fat_fs_error_ratelimit(sb, fmt, args...) \
4482c8a5ffbSAlexey Fisher 	__fat_fs_error(sb, __ratelimit(&MSDOS_SB(sb)->ratelimit), fmt , ## args)
449e057aaecSJonathan Lassoff 
450e057aaecSJonathan Lassoff #define FAT_PRINTK_PREFIX "%sFAT-fs (%s): "
451e057aaecSJonathan Lassoff #define fat_msg(sb, level, fmt, args...)				\
452e057aaecSJonathan Lassoff do {									\
453e057aaecSJonathan Lassoff 	printk_index_subsys_emit(FAT_PRINTK_PREFIX, level, fmt, ##args);\
454e057aaecSJonathan Lassoff 	_fat_msg(sb, level, fmt, ##args);				\
455e057aaecSJonathan Lassoff } while (0)
456b9075fa9SJoe Perches __printf(3, 4) __cold
457e057aaecSJonathan Lassoff void _fat_msg(struct super_block *sb, const char *level, const char *fmt, ...);
458b742c341SNamjae Jeon #define fat_msg_ratelimit(sb, level, fmt, args...)	\
459b742c341SNamjae Jeon 	do {	\
460b742c341SNamjae Jeon 			if (__ratelimit(&MSDOS_SB(sb)->ratelimit))	\
461b742c341SNamjae Jeon 				fat_msg(sb, level, fmt, ## args);	\
462b742c341SNamjae Jeon 	 } while (0)
463ed248b29SOGAWA Hirofumi extern int fat_clusters_flush(struct super_block *sb);
4649e975daeSOGAWA Hirofumi extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
465f423420cSArnd Bergmann extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
4667decd1cbSOGAWA Hirofumi 			      __le16 __time, __le16 __date, u8 time_cs);
467f423420cSArnd Bergmann extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
4687decd1cbSOGAWA Hirofumi 			      __le16 *time, __le16 *date, u8 *time_cs);
4694dcc3f96SChung-Chiang Cheng extern struct timespec64 fat_truncate_atime(const struct msdos_sb_info *sbi,
4704dcc3f96SChung-Chiang Cheng 					    const struct timespec64 *ts);
4714dcc3f96SChung-Chiang Cheng extern struct timespec64 fat_truncate_mtime(const struct msdos_sb_info *sbi,
4724dcc3f96SChung-Chiang Cheng 					    const struct timespec64 *ts);
4736bb885ecSFrank Sorenson extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
4746bb885ecSFrank Sorenson 			     int flags);
475913e9928SJeff Layton extern int fat_update_time(struct inode *inode, int flags);
4769e975daeSOGAWA Hirofumi extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
4779e975daeSOGAWA Hirofumi 
4789e975daeSOGAWA Hirofumi int fat_cache_init(void);
4799e975daeSOGAWA Hirofumi void fat_cache_destroy(void);
4809e975daeSOGAWA Hirofumi 
48121b6633dSSteven J. Magnani /* fat/nfs.c */
482ea3983acSNamjae Jeon extern const struct export_operations fat_export_ops;
483ea3983acSNamjae Jeon extern const struct export_operations fat_export_ops_nostale;
48421b6633dSSteven J. Magnani 
485c3302931SOGAWA Hirofumi /* helper for printk */
486c3302931SOGAWA Hirofumi typedef unsigned long long	llu;
487c3302931SOGAWA Hirofumi 
4889e975daeSOGAWA Hirofumi #endif /* !_FAT_H */
489