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>
109e975daeSOGAWA Hirofumi
119e975daeSOGAWA Hirofumi /*
129e975daeSOGAWA Hirofumi * vfat shortname flags
139e975daeSOGAWA Hirofumi */
149e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_LOWER 0x0001 /* convert to lowercase for display */
159e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_WIN95 0x0002 /* emulate win95 rule for display */
169e975daeSOGAWA Hirofumi #define VFAT_SFN_DISPLAY_WINNT 0x0004 /* emulate winnt rule for display */
179e975daeSOGAWA Hirofumi #define VFAT_SFN_CREATE_WIN95 0x0100 /* emulate win95 rule for create */
189e975daeSOGAWA Hirofumi #define VFAT_SFN_CREATE_WINNT 0x0200 /* emulate winnt rule for create */
199e975daeSOGAWA Hirofumi
2085c78591SDenis Karpov #define FAT_ERRORS_CONT 1 /* ignore error and continue */
2185c78591SDenis Karpov #define FAT_ERRORS_PANIC 2 /* panic on error */
2285c78591SDenis Karpov #define FAT_ERRORS_RO 3 /* remount r/o on error */
2385c78591SDenis Karpov
242628b7a6SNamjae Jeon #define FAT_NFS_STALE_RW 1 /* NFS RW support, can cause ESTALE */
252628b7a6SNamjae Jeon #define FAT_NFS_NOSTALE_RO 2 /* NFS RO support, no ESTALE issue */
262628b7a6SNamjae Jeon
279e975daeSOGAWA Hirofumi struct fat_mount_options {
28170782ebSEric W. Biederman kuid_t fs_uid;
29170782ebSEric W. Biederman kgid_t fs_gid;
309e975daeSOGAWA Hirofumi unsigned short fs_fmask;
319e975daeSOGAWA Hirofumi unsigned short fs_dmask;
329e975daeSOGAWA Hirofumi unsigned short codepage; /* Codepage for shortname conversions */
3358156c8fSJan Kara int time_offset; /* Offset of timestamps from UTC (in minutes) */
349e975daeSOGAWA Hirofumi char *iocharset; /* Charset used for filename input/display */
359e975daeSOGAWA Hirofumi unsigned short shortname; /* flags for shortname display/create rule */
369e975daeSOGAWA Hirofumi unsigned char name_check; /* r = relaxed, n = normal, s = strict */
3785c78591SDenis Karpov unsigned char errors; /* On error: continue, panic, remount-ro */
382628b7a6SNamjae Jeon unsigned char nfs; /* NFS support: nostale_ro, stale_rw */
399e975daeSOGAWA Hirofumi unsigned short allow_utime;/* permission for setting the [am]time */
409e975daeSOGAWA Hirofumi unsigned quiet:1, /* set = fake successful chmods and chowns */
419e975daeSOGAWA Hirofumi showexec:1, /* set = only set x bit for com/exe/bat */
429e975daeSOGAWA Hirofumi sys_immutable:1, /* set = system files are immutable */
439e975daeSOGAWA Hirofumi dotsOK:1, /* set = hidden and system files are named '.filename' */
449e975daeSOGAWA Hirofumi isvfat:1, /* 0=no vfat long filename support, 1=vfat support */
459e975daeSOGAWA Hirofumi utf8:1, /* Use of UTF-8 character set (Default) */
469e975daeSOGAWA Hirofumi unicode_xlate:1, /* create escape sequences for unhandled Unicode */
479e975daeSOGAWA Hirofumi numtail:1, /* Does first alias have a numeric '~1' type tail? */
489e975daeSOGAWA Hirofumi flush:1, /* write things quickly */
499e975daeSOGAWA Hirofumi nocase:1, /* Does this need case conversion? 0=need case conversion*/
509e975daeSOGAWA Hirofumi usefree:1, /* Use free_clusters for FAT32 */
5158156c8fSJan Kara tz_set:1, /* Filesystem timestamps' offset set */
52681142f9SChristoph Hellwig rodir:1, /* allow ATTR_RO for directory */
53190a8843SConrad Meyer discard:1, /* Issue discard requests on deletions */
54190a8843SConrad Meyer dos1xfloppy:1; /* Assume default BPB for DOS 1.x floppies */
559e975daeSOGAWA Hirofumi };
569e975daeSOGAWA Hirofumi
579e975daeSOGAWA Hirofumi #define FAT_HASH_BITS 8
589e975daeSOGAWA Hirofumi #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
599e975daeSOGAWA Hirofumi
609e975daeSOGAWA Hirofumi /*
619e975daeSOGAWA Hirofumi * MS-DOS file system in-core superblock data
629e975daeSOGAWA Hirofumi */
639e975daeSOGAWA Hirofumi struct msdos_sb_info {
649e975daeSOGAWA Hirofumi unsigned short sec_per_clus; /* sectors/cluster */
659e975daeSOGAWA Hirofumi unsigned short cluster_bits; /* log2(cluster_size) */
669e975daeSOGAWA Hirofumi unsigned int cluster_size; /* cluster size */
678de560deSAlexander Kuleshov unsigned char fats, fat_bits; /* number of FATs, FAT bits (12,16 or 32) */
689e975daeSOGAWA Hirofumi unsigned short fat_start;
699e975daeSOGAWA Hirofumi unsigned long fat_length; /* FAT start & length (sec.) */
709e975daeSOGAWA Hirofumi unsigned long dir_start;
719e975daeSOGAWA Hirofumi unsigned short dir_entries; /* root dir start & entries */
729e975daeSOGAWA Hirofumi unsigned long data_start; /* first data sector */
739e975daeSOGAWA Hirofumi unsigned long max_cluster; /* maximum cluster number */
749e975daeSOGAWA Hirofumi unsigned long root_cluster; /* first cluster of the root directory */
759e975daeSOGAWA Hirofumi unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */
769e975daeSOGAWA Hirofumi struct mutex fat_lock;
778fceb4e0SNamjae Jeon struct mutex nfs_build_inode_lock;
78e40b34c7SMarco Stornelli struct mutex s_lock;
799e975daeSOGAWA Hirofumi unsigned int prev_free; /* previously allocated cluster number */
809e975daeSOGAWA Hirofumi unsigned int free_clusters; /* -1 if undefined */
819e975daeSOGAWA Hirofumi unsigned int free_clus_valid; /* is free_clusters valid? */
829e975daeSOGAWA Hirofumi struct fat_mount_options options;
839e975daeSOGAWA Hirofumi struct nls_table *nls_disk; /* Codepage used on disk */
849e975daeSOGAWA Hirofumi struct nls_table *nls_io; /* Charset used for input and display */
859e975daeSOGAWA Hirofumi const void *dir_ops; /* Opaque; default directory operations */
869e975daeSOGAWA Hirofumi int dir_per_block; /* dir entries per block */
879e975daeSOGAWA Hirofumi int dir_per_block_bits; /* log2(dir_per_block) */
886e5b93eeSMike Lockwood unsigned int vol_id; /*volume ID*/
899e975daeSOGAWA Hirofumi
909e975daeSOGAWA Hirofumi int fatent_shift;
918992de4cSJulia Lawall const struct fatent_operations *fatent_ops;
92b522412aSAl Viro struct inode *fat_inode;
93020ac5b6SArtem Bityutskiy struct inode *fsinfo_inode;
949e975daeSOGAWA Hirofumi
95aaa04b48SOGAWA Hirofumi struct ratelimit_state ratelimit;
96aaa04b48SOGAWA Hirofumi
979e975daeSOGAWA Hirofumi spinlock_t inode_hash_lock;
989e975daeSOGAWA Hirofumi struct hlist_head inode_hashtable[FAT_HASH_SIZE];
997669e8fbSSteven J. Magnani
1007669e8fbSSteven J. Magnani spinlock_t dir_hash_lock;
1017669e8fbSSteven J. Magnani struct hlist_head dir_hashtable[FAT_HASH_SIZE];
102b88a1058SOleksij Rempel
103b88a1058SOleksij Rempel unsigned int dirty; /* fs state before mount */
104cac45b06SAl Viro struct rcu_head rcu;
1059e975daeSOGAWA Hirofumi };
1069e975daeSOGAWA Hirofumi
1079e975daeSOGAWA Hirofumi #define FAT_CACHE_VALID 0 /* special case for valid cache */
1089e975daeSOGAWA Hirofumi
1099e975daeSOGAWA Hirofumi /*
1109e975daeSOGAWA Hirofumi * MS-DOS file system inode data in memory
1119e975daeSOGAWA Hirofumi */
1129e975daeSOGAWA Hirofumi struct msdos_inode_info {
1139e975daeSOGAWA Hirofumi spinlock_t cache_lru_lock;
1149e975daeSOGAWA Hirofumi struct list_head cache_lru;
1159e975daeSOGAWA Hirofumi int nr_caches;
1169e975daeSOGAWA Hirofumi /* for avoiding the race between fat_free() and fat_get_cluster() */
1179e975daeSOGAWA Hirofumi unsigned int cache_valid_id;
1189e975daeSOGAWA Hirofumi
1192bdf67ebSOGAWA Hirofumi /* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */
1202bdf67ebSOGAWA Hirofumi loff_t mmu_private; /* physically allocated size */
1212bdf67ebSOGAWA Hirofumi
1229e975daeSOGAWA Hirofumi int i_start; /* first cluster or 0 */
1239e975daeSOGAWA Hirofumi int i_logstart; /* logical first cluster */
1249e975daeSOGAWA Hirofumi int i_attrs; /* unused attribute bits */
1259e975daeSOGAWA Hirofumi loff_t i_pos; /* on-disk position of directory entry or 0 */
1269e975daeSOGAWA Hirofumi struct hlist_node i_fat_hash; /* hash by i_location */
1277669e8fbSSteven J. Magnani struct hlist_node i_dir_hash; /* hash by i_logstart */
12858268691SChristoph Hellwig struct rw_semaphore truncate_lock; /* protect bmap against truncate */
12930abce05SChung-Chiang Cheng struct timespec64 i_crtime; /* File creation (birth) time */
1309e975daeSOGAWA Hirofumi struct inode vfs_inode;
1319e975daeSOGAWA Hirofumi };
1329e975daeSOGAWA Hirofumi
1339e975daeSOGAWA Hirofumi struct fat_slot_info {
1349e975daeSOGAWA Hirofumi loff_t i_pos; /* on-disk position of directory entry */
1359e975daeSOGAWA Hirofumi loff_t slot_off; /* offset for slot or de start */
1369e975daeSOGAWA Hirofumi int nr_slots; /* number of slots + 1(de) in filename */
1379e975daeSOGAWA Hirofumi struct msdos_dir_entry *de;
1389e975daeSOGAWA Hirofumi struct buffer_head *bh;
1399e975daeSOGAWA Hirofumi };
1409e975daeSOGAWA Hirofumi
MSDOS_SB(struct super_block * sb)1419e975daeSOGAWA Hirofumi static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
1429e975daeSOGAWA Hirofumi {
1439e975daeSOGAWA Hirofumi return sb->s_fs_info;
1449e975daeSOGAWA Hirofumi }
1459e975daeSOGAWA Hirofumi
146306790f7SCarmeli Tamir /*
147306790f7SCarmeli Tamir * Functions that determine the variant of the FAT file system (i.e.,
148306790f7SCarmeli Tamir * whether this is FAT12, FAT16 or FAT32.
149306790f7SCarmeli Tamir */
is_fat12(const struct msdos_sb_info * sbi)150306790f7SCarmeli Tamir static inline bool is_fat12(const struct msdos_sb_info *sbi)
151306790f7SCarmeli Tamir {
152306790f7SCarmeli Tamir return sbi->fat_bits == 12;
153306790f7SCarmeli Tamir }
154306790f7SCarmeli Tamir
is_fat16(const struct msdos_sb_info * sbi)155306790f7SCarmeli Tamir static inline bool is_fat16(const struct msdos_sb_info *sbi)
156306790f7SCarmeli Tamir {
157306790f7SCarmeli Tamir return sbi->fat_bits == 16;
158306790f7SCarmeli Tamir }
159306790f7SCarmeli Tamir
is_fat32(const struct msdos_sb_info * sbi)160306790f7SCarmeli Tamir static inline bool is_fat32(const struct msdos_sb_info *sbi)
161306790f7SCarmeli Tamir {
162306790f7SCarmeli Tamir return sbi->fat_bits == 32;
163306790f7SCarmeli Tamir }
164306790f7SCarmeli Tamir
165d19dc016SCarmeli Tamir /* Maximum number of clusters */
max_fat(struct super_block * sb)166d19dc016SCarmeli Tamir static inline u32 max_fat(struct super_block *sb)
167d19dc016SCarmeli Tamir {
168d19dc016SCarmeli Tamir struct msdos_sb_info *sbi = MSDOS_SB(sb);
169d19dc016SCarmeli Tamir
170306790f7SCarmeli Tamir return is_fat32(sbi) ? MAX_FAT32 :
171306790f7SCarmeli Tamir is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
172d19dc016SCarmeli Tamir }
173d19dc016SCarmeli Tamir
MSDOS_I(struct inode * inode)1749e975daeSOGAWA Hirofumi static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
1759e975daeSOGAWA Hirofumi {
1769e975daeSOGAWA Hirofumi return container_of(inode, struct msdos_inode_info, vfs_inode);
1779e975daeSOGAWA Hirofumi }
1789e975daeSOGAWA Hirofumi
1799183482fSOGAWA Hirofumi /*
1809183482fSOGAWA Hirofumi * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
1819183482fSOGAWA Hirofumi * save ATTR_RO instead of ->i_mode.
182dfc209c0SOGAWA Hirofumi *
183dfc209c0SOGAWA Hirofumi * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
184dfc209c0SOGAWA Hirofumi * bit, it's just used as flag for app.
1859183482fSOGAWA Hirofumi */
fat_mode_can_hold_ro(struct inode * inode)1869183482fSOGAWA Hirofumi static inline int fat_mode_can_hold_ro(struct inode *inode)
1879183482fSOGAWA Hirofumi {
1889183482fSOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
189dacd0e7bSAl Viro umode_t mask;
1909183482fSOGAWA Hirofumi
191dfc209c0SOGAWA Hirofumi if (S_ISDIR(inode->i_mode)) {
192dfc209c0SOGAWA Hirofumi if (!sbi->options.rodir)
193dfc209c0SOGAWA Hirofumi return 0;
1949183482fSOGAWA Hirofumi mask = ~sbi->options.fs_dmask;
195dfc209c0SOGAWA Hirofumi } else
1969183482fSOGAWA Hirofumi mask = ~sbi->options.fs_fmask;
1979183482fSOGAWA Hirofumi
1989183482fSOGAWA Hirofumi if (!(mask & S_IWUGO))
1999183482fSOGAWA Hirofumi return 0;
2009183482fSOGAWA Hirofumi return 1;
2019183482fSOGAWA Hirofumi }
2029183482fSOGAWA Hirofumi
2039c0aa1b8SOGAWA Hirofumi /* Convert attribute bits and a mask to the UNIX mode. */
fat_make_mode(struct msdos_sb_info * sbi,u8 attrs,umode_t mode)204dacd0e7bSAl Viro static inline umode_t fat_make_mode(struct msdos_sb_info *sbi,
205dacd0e7bSAl Viro u8 attrs, umode_t mode)
2069c0aa1b8SOGAWA Hirofumi {
207dfc209c0SOGAWA Hirofumi if (attrs & ATTR_RO && !((attrs & ATTR_DIR) && !sbi->options.rodir))
2089c0aa1b8SOGAWA Hirofumi mode &= ~S_IWUGO;
2099c0aa1b8SOGAWA Hirofumi
2109c0aa1b8SOGAWA Hirofumi if (attrs & ATTR_DIR)
2119c0aa1b8SOGAWA Hirofumi return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
2129c0aa1b8SOGAWA Hirofumi else
2139c0aa1b8SOGAWA Hirofumi return (mode & ~sbi->options.fs_fmask) | S_IFREG;
2149c0aa1b8SOGAWA Hirofumi }
2159c0aa1b8SOGAWA Hirofumi
2169e975daeSOGAWA Hirofumi /* Return the FAT attribute byte for this inode */
fat_make_attrs(struct inode * inode)2179c0aa1b8SOGAWA Hirofumi static inline u8 fat_make_attrs(struct inode *inode)
2189e975daeSOGAWA Hirofumi {
2199183482fSOGAWA Hirofumi u8 attrs = MSDOS_I(inode)->i_attrs;
2209183482fSOGAWA Hirofumi if (S_ISDIR(inode->i_mode))
2219183482fSOGAWA Hirofumi attrs |= ATTR_DIR;
2229183482fSOGAWA Hirofumi if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO))
2239183482fSOGAWA Hirofumi attrs |= ATTR_RO;
2249183482fSOGAWA Hirofumi return attrs;
2259e975daeSOGAWA Hirofumi }
2269e975daeSOGAWA Hirofumi
fat_save_attrs(struct inode * inode,u8 attrs)2279c0aa1b8SOGAWA Hirofumi static inline void fat_save_attrs(struct inode *inode, u8 attrs)
2289c0aa1b8SOGAWA Hirofumi {
2299183482fSOGAWA Hirofumi if (fat_mode_can_hold_ro(inode))
2309c0aa1b8SOGAWA Hirofumi MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED;
2319183482fSOGAWA Hirofumi else
2329183482fSOGAWA Hirofumi MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO);
2339c0aa1b8SOGAWA Hirofumi }
2349c0aa1b8SOGAWA Hirofumi
fat_checksum(const __u8 * name)2359e975daeSOGAWA Hirofumi static inline unsigned char fat_checksum(const __u8 *name)
2369e975daeSOGAWA Hirofumi {
2379e975daeSOGAWA Hirofumi unsigned char s = name[0];
2389e975daeSOGAWA Hirofumi s = (s<<7) + (s>>1) + name[1]; s = (s<<7) + (s>>1) + name[2];
2399e975daeSOGAWA Hirofumi s = (s<<7) + (s>>1) + name[3]; s = (s<<7) + (s>>1) + name[4];
2409e975daeSOGAWA Hirofumi s = (s<<7) + (s>>1) + name[5]; s = (s<<7) + (s>>1) + name[6];
2419e975daeSOGAWA Hirofumi s = (s<<7) + (s>>1) + name[7]; s = (s<<7) + (s>>1) + name[8];
2429e975daeSOGAWA Hirofumi s = (s<<7) + (s>>1) + name[9]; s = (s<<7) + (s>>1) + name[10];
2439e975daeSOGAWA Hirofumi return s;
2449e975daeSOGAWA Hirofumi }
2459e975daeSOGAWA Hirofumi
fat_clus_to_blknr(struct msdos_sb_info * sbi,int clus)2469e975daeSOGAWA Hirofumi static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
2479e975daeSOGAWA Hirofumi {
2489e975daeSOGAWA Hirofumi return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
2499e975daeSOGAWA Hirofumi + sbi->data_start;
2509e975daeSOGAWA Hirofumi }
2519e975daeSOGAWA Hirofumi
fat_get_blknr_offset(struct msdos_sb_info * sbi,loff_t i_pos,sector_t * blknr,int * offset)252e22a4442SNamjae Jeon static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
253e22a4442SNamjae Jeon loff_t i_pos, sector_t *blknr, int *offset)
254e22a4442SNamjae Jeon {
255e22a4442SNamjae Jeon *blknr = i_pos >> sbi->dir_per_block_bits;
256e22a4442SNamjae Jeon *offset = i_pos & (sbi->dir_per_block - 1);
257e22a4442SNamjae Jeon }
258e22a4442SNamjae Jeon
fat_i_pos_read(struct msdos_sb_info * sbi,struct inode * inode)259f21735d5SNamjae Jeon static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
260f21735d5SNamjae Jeon struct inode *inode)
261f21735d5SNamjae Jeon {
262f21735d5SNamjae Jeon loff_t i_pos;
263f21735d5SNamjae Jeon #if BITS_PER_LONG == 32
264f21735d5SNamjae Jeon spin_lock(&sbi->inode_hash_lock);
265f21735d5SNamjae Jeon #endif
266f21735d5SNamjae Jeon i_pos = MSDOS_I(inode)->i_pos;
267f21735d5SNamjae Jeon #if BITS_PER_LONG == 32
268f21735d5SNamjae Jeon spin_unlock(&sbi->inode_hash_lock);
269f21735d5SNamjae Jeon #endif
270f21735d5SNamjae Jeon return i_pos;
271f21735d5SNamjae Jeon }
272f21735d5SNamjae Jeon
fat16_towchar(wchar_t * dst,const __u8 * src,size_t len)2739e975daeSOGAWA Hirofumi static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
2749e975daeSOGAWA Hirofumi {
2759e975daeSOGAWA Hirofumi #ifdef __BIG_ENDIAN
2769e975daeSOGAWA Hirofumi while (len--) {
2779e975daeSOGAWA Hirofumi *dst++ = src[0] | (src[1] << 8);
2789e975daeSOGAWA Hirofumi src += 2;
2799e975daeSOGAWA Hirofumi }
2809e975daeSOGAWA Hirofumi #else
2819e975daeSOGAWA Hirofumi memcpy(dst, src, len * 2);
2829e975daeSOGAWA Hirofumi #endif
2839e975daeSOGAWA Hirofumi }
2849e975daeSOGAWA Hirofumi
fat_get_start(const struct msdos_sb_info * sbi,const struct msdos_dir_entry * de)285a943ed71SSteven J. Magnani static inline int fat_get_start(const struct msdos_sb_info *sbi,
286a943ed71SSteven J. Magnani const struct msdos_dir_entry *de)
287a943ed71SSteven J. Magnani {
288a943ed71SSteven J. Magnani int cluster = le16_to_cpu(de->start);
289306790f7SCarmeli Tamir if (is_fat32(sbi))
290a943ed71SSteven J. Magnani cluster |= (le16_to_cpu(de->starthi) << 16);
291a943ed71SSteven J. Magnani return cluster;
292a943ed71SSteven J. Magnani }
293a943ed71SSteven J. Magnani
fat_set_start(struct msdos_dir_entry * de,int cluster)294a943ed71SSteven J. Magnani static inline void fat_set_start(struct msdos_dir_entry *de, int cluster)
295a943ed71SSteven J. Magnani {
296a943ed71SSteven J. Magnani de->start = cpu_to_le16(cluster);
297a943ed71SSteven J. Magnani de->starthi = cpu_to_le16(cluster >> 16);
298a943ed71SSteven J. Magnani }
299a943ed71SSteven J. Magnani
fatwchar_to16(__u8 * dst,const wchar_t * src,size_t len)3009e975daeSOGAWA Hirofumi static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
3019e975daeSOGAWA Hirofumi {
3029e975daeSOGAWA Hirofumi #ifdef __BIG_ENDIAN
3039e975daeSOGAWA Hirofumi while (len--) {
3049e975daeSOGAWA Hirofumi dst[0] = *src & 0x00FF;
3059e975daeSOGAWA Hirofumi dst[1] = (*src & 0xFF00) >> 8;
3069e975daeSOGAWA Hirofumi dst += 2;
3079e975daeSOGAWA Hirofumi src++;
3089e975daeSOGAWA Hirofumi }
3099e975daeSOGAWA Hirofumi #else
3109e975daeSOGAWA Hirofumi memcpy(dst, src, len * 2);
3119e975daeSOGAWA Hirofumi #endif
3129e975daeSOGAWA Hirofumi }
3139e975daeSOGAWA Hirofumi
3149e975daeSOGAWA Hirofumi /* fat/cache.c */
3159e975daeSOGAWA Hirofumi extern void fat_cache_inval_inode(struct inode *inode);
3169e975daeSOGAWA Hirofumi extern int fat_get_cluster(struct inode *inode, int cluster,
3179e975daeSOGAWA Hirofumi int *fclus, int *dclus);
31816fab201SNamjae Jeon extern int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
31916fab201SNamjae Jeon sector_t last_block,
32016fab201SNamjae Jeon unsigned long *mapped_blocks, sector_t *bmap);
3219e975daeSOGAWA Hirofumi extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
32216fab201SNamjae Jeon unsigned long *mapped_blocks, int create, bool from_bmap);
3239e975daeSOGAWA Hirofumi
3249e975daeSOGAWA Hirofumi /* fat/dir.c */
3259e975daeSOGAWA Hirofumi extern const struct file_operations fat_dir_operations;
3269e975daeSOGAWA Hirofumi extern int fat_search_long(struct inode *inode, const unsigned char *name,
3279e975daeSOGAWA Hirofumi int name_len, struct fat_slot_info *sinfo);
3289e975daeSOGAWA Hirofumi extern int fat_dir_empty(struct inode *dir);
3299e975daeSOGAWA Hirofumi extern int fat_subdirs(struct inode *dir);
3309e975daeSOGAWA Hirofumi extern int fat_scan(struct inode *dir, const unsigned char *name,
3319e975daeSOGAWA Hirofumi struct fat_slot_info *sinfo);
332f1e6fb0aSNamjae Jeon extern int fat_scan_logstart(struct inode *dir, int i_logstart,
333f1e6fb0aSNamjae Jeon struct fat_slot_info *sinfo);
3349e975daeSOGAWA Hirofumi extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
3357669e8fbSSteven J. Magnani struct msdos_dir_entry **de);
336f423420cSArnd Bergmann extern int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts);
3379e975daeSOGAWA Hirofumi extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
3389e975daeSOGAWA Hirofumi struct fat_slot_info *sinfo);
3399e975daeSOGAWA Hirofumi extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
3409e975daeSOGAWA Hirofumi
3419e975daeSOGAWA Hirofumi /* fat/fatent.c */
3429e975daeSOGAWA Hirofumi struct fat_entry {
3439e975daeSOGAWA Hirofumi int entry;
3449e975daeSOGAWA Hirofumi union {
3459e975daeSOGAWA Hirofumi u8 *ent12_p[2];
3469e975daeSOGAWA Hirofumi __le16 *ent16_p;
3479e975daeSOGAWA Hirofumi __le32 *ent32_p;
3489e975daeSOGAWA Hirofumi } u;
3499e975daeSOGAWA Hirofumi int nr_bhs;
3509e975daeSOGAWA Hirofumi struct buffer_head *bhs[2];
351b522412aSAl Viro struct inode *fat_inode;
3529e975daeSOGAWA Hirofumi };
3539e975daeSOGAWA Hirofumi
fatent_init(struct fat_entry * fatent)3549e975daeSOGAWA Hirofumi static inline void fatent_init(struct fat_entry *fatent)
3559e975daeSOGAWA Hirofumi {
3569e975daeSOGAWA Hirofumi fatent->nr_bhs = 0;
3579e975daeSOGAWA Hirofumi fatent->entry = 0;
3589e975daeSOGAWA Hirofumi fatent->u.ent32_p = NULL;
3599e975daeSOGAWA Hirofumi fatent->bhs[0] = fatent->bhs[1] = NULL;
360b522412aSAl Viro fatent->fat_inode = NULL;
3619e975daeSOGAWA Hirofumi }
3629e975daeSOGAWA Hirofumi
fatent_set_entry(struct fat_entry * fatent,int entry)3639e975daeSOGAWA Hirofumi static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
3649e975daeSOGAWA Hirofumi {
3659e975daeSOGAWA Hirofumi fatent->entry = entry;
3669e975daeSOGAWA Hirofumi fatent->u.ent32_p = NULL;
3679e975daeSOGAWA Hirofumi }
3689e975daeSOGAWA Hirofumi
fatent_brelse(struct fat_entry * fatent)3699e975daeSOGAWA Hirofumi static inline void fatent_brelse(struct fat_entry *fatent)
3709e975daeSOGAWA Hirofumi {
3719e975daeSOGAWA Hirofumi int i;
3729e975daeSOGAWA Hirofumi fatent->u.ent32_p = NULL;
3739e975daeSOGAWA Hirofumi for (i = 0; i < fatent->nr_bhs; i++)
3749e975daeSOGAWA Hirofumi brelse(fatent->bhs[i]);
3759e975daeSOGAWA Hirofumi fatent->nr_bhs = 0;
3769e975daeSOGAWA Hirofumi fatent->bhs[0] = fatent->bhs[1] = NULL;
377b522412aSAl Viro fatent->fat_inode = NULL;
3789e975daeSOGAWA Hirofumi }
3799e975daeSOGAWA Hirofumi
fat_valid_entry(struct msdos_sb_info * sbi,int entry)3800afa9626SOGAWA Hirofumi static inline bool fat_valid_entry(struct msdos_sb_info *sbi, int entry)
3810afa9626SOGAWA Hirofumi {
3820afa9626SOGAWA Hirofumi return FAT_START_ENT <= entry && entry < sbi->max_cluster;
3830afa9626SOGAWA Hirofumi }
3840afa9626SOGAWA Hirofumi
3859e975daeSOGAWA Hirofumi extern void fat_ent_access_init(struct super_block *sb);
3869e975daeSOGAWA Hirofumi extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
3879e975daeSOGAWA Hirofumi int entry);
3889e975daeSOGAWA Hirofumi extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
3899e975daeSOGAWA Hirofumi int new, int wait);
3909e975daeSOGAWA Hirofumi extern int fat_alloc_clusters(struct inode *inode, int *cluster,
3919e975daeSOGAWA Hirofumi int nr_cluster);
3929e975daeSOGAWA Hirofumi extern int fat_free_clusters(struct inode *inode, int cluster);
3939e975daeSOGAWA Hirofumi extern int fat_count_free_clusters(struct super_block *sb);
394f663b5b3SWentao Wang extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
3959e975daeSOGAWA Hirofumi
3969e975daeSOGAWA Hirofumi /* fat/file.c */
3977845bc3eSArnd Bergmann extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
3987845bc3eSArnd Bergmann unsigned long arg);
3999e975daeSOGAWA Hirofumi extern const struct file_operations fat_file_operations;
4009e975daeSOGAWA Hirofumi extern const struct inode_operations fat_file_inode_operations;
401c1632a0fSChristian Brauner extern int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
402549c7297SChristian Brauner struct iattr *attr);
403459f6ed3Snpiggin@suse.de extern void fat_truncate_blocks(struct inode *inode, loff_t offset);
404b74d24f7SChristian Brauner extern int fat_getattr(struct mnt_idmap *idmap,
405549c7297SChristian Brauner const struct path *path, struct kstat *stat,
406a528d35eSDavid Howells u32 request_mask, unsigned int flags);
40702c24a82SJosef Bacik extern int fat_file_fsync(struct file *file, loff_t start, loff_t end,
40802c24a82SJosef Bacik int datasync);
4099e975daeSOGAWA Hirofumi
4109e975daeSOGAWA Hirofumi /* fat/inode.c */
411c0ef0cc9SNamjae Jeon extern int fat_block_truncate_page(struct inode *inode, loff_t from);
4129e975daeSOGAWA Hirofumi extern void fat_attach(struct inode *inode, loff_t i_pos);
4139e975daeSOGAWA Hirofumi extern void fat_detach(struct inode *inode);
4149e975daeSOGAWA Hirofumi extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
4159e975daeSOGAWA Hirofumi extern struct inode *fat_build_inode(struct super_block *sb,
4169e975daeSOGAWA Hirofumi struct msdos_dir_entry *de, loff_t i_pos);
4179e975daeSOGAWA Hirofumi extern int fat_sync_inode(struct inode *inode);
4189e975daeSOGAWA Hirofumi extern int fat_fill_super(struct super_block *sb, void *data, int silent,
4193d23985dSAl Viro int isvfat, void (*setup)(struct super_block *));
420f1e6fb0aSNamjae Jeon extern int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
4219e975daeSOGAWA Hirofumi
4229e975daeSOGAWA Hirofumi extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
4239e975daeSOGAWA Hirofumi struct inode *i2);
fat_dir_hash(int logstart)4247669e8fbSSteven J. Magnani static inline unsigned long fat_dir_hash(int logstart)
42521b6633dSSteven J. Magnani {
4267669e8fbSSteven J. Magnani return hash_32(logstart, FAT_HASH_BITS);
42721b6633dSSteven J. Magnani }
428b13bb33eSNamjae Jeon extern int fat_add_cluster(struct inode *inode);
42921b6633dSSteven J. Magnani
4309e975daeSOGAWA Hirofumi /* fat/misc.c */
431b9075fa9SJoe Perches extern __printf(3, 4) __cold
432b9075fa9SJoe Perches void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...);
4332c8a5ffbSAlexey Fisher #define fat_fs_error(sb, fmt, args...) \
4342c8a5ffbSAlexey Fisher __fat_fs_error(sb, 1, fmt , ## args)
4352c8a5ffbSAlexey Fisher #define fat_fs_error_ratelimit(sb, fmt, args...) \
4362c8a5ffbSAlexey Fisher __fat_fs_error(sb, __ratelimit(&MSDOS_SB(sb)->ratelimit), fmt , ## args)
437e057aaecSJonathan Lassoff
438e057aaecSJonathan Lassoff #define FAT_PRINTK_PREFIX "%sFAT-fs (%s): "
439e057aaecSJonathan Lassoff #define fat_msg(sb, level, fmt, args...) \
440e057aaecSJonathan Lassoff do { \
441e057aaecSJonathan Lassoff printk_index_subsys_emit(FAT_PRINTK_PREFIX, level, fmt, ##args);\
442e057aaecSJonathan Lassoff _fat_msg(sb, level, fmt, ##args); \
443e057aaecSJonathan Lassoff } while (0)
444b9075fa9SJoe Perches __printf(3, 4) __cold
445e057aaecSJonathan Lassoff void _fat_msg(struct super_block *sb, const char *level, const char *fmt, ...);
446b742c341SNamjae Jeon #define fat_msg_ratelimit(sb, level, fmt, args...) \
447b742c341SNamjae Jeon do { \
448b742c341SNamjae Jeon if (__ratelimit(&MSDOS_SB(sb)->ratelimit)) \
449b742c341SNamjae Jeon fat_msg(sb, level, fmt, ## args); \
450b742c341SNamjae Jeon } while (0)
451ed248b29SOGAWA Hirofumi extern int fat_clusters_flush(struct super_block *sb);
4529e975daeSOGAWA Hirofumi extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
453f423420cSArnd Bergmann extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
4547decd1cbSOGAWA Hirofumi __le16 __time, __le16 __date, u8 time_cs);
455f423420cSArnd Bergmann extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
4567decd1cbSOGAWA Hirofumi __le16 *time, __le16 *date, u8 *time_cs);
4574dcc3f96SChung-Chiang Cheng extern struct timespec64 fat_truncate_atime(const struct msdos_sb_info *sbi,
4584dcc3f96SChung-Chiang Cheng const struct timespec64 *ts);
4594dcc3f96SChung-Chiang Cheng extern struct timespec64 fat_truncate_mtime(const struct msdos_sb_info *sbi,
4604dcc3f96SChung-Chiang Cheng const struct timespec64 *ts);
4616bb885ecSFrank Sorenson extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
4626bb885ecSFrank Sorenson int flags);
463*913e9928SJeff Layton extern int fat_update_time(struct inode *inode, int flags);
4649e975daeSOGAWA Hirofumi extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
4659e975daeSOGAWA Hirofumi
4669e975daeSOGAWA Hirofumi int fat_cache_init(void);
4679e975daeSOGAWA Hirofumi void fat_cache_destroy(void);
4689e975daeSOGAWA Hirofumi
46921b6633dSSteven J. Magnani /* fat/nfs.c */
470ea3983acSNamjae Jeon extern const struct export_operations fat_export_ops;
471ea3983acSNamjae Jeon extern const struct export_operations fat_export_ops_nostale;
47221b6633dSSteven J. Magnani
473c3302931SOGAWA Hirofumi /* helper for printk */
474c3302931SOGAWA Hirofumi typedef unsigned long long llu;
475c3302931SOGAWA Hirofumi
4769e975daeSOGAWA Hirofumi #endif /* !_FAT_H */
477