18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef _EXFAT_FS_H
78c2ecf20Sopenharmony_ci#define _EXFAT_FS_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include <linux/ratelimit.h>
118c2ecf20Sopenharmony_ci#include <linux/nls.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define EXFAT_SUPER_MAGIC       0x2011BAB0UL
148c2ecf20Sopenharmony_ci#define EXFAT_ROOT_INO		1
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define EXFAT_CLUSTERS_UNTRACKED (~0u)
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * exfat error flags
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cienum exfat_error_mode {
228c2ecf20Sopenharmony_ci	EXFAT_ERRORS_CONT,	/* ignore error and continue */
238c2ecf20Sopenharmony_ci	EXFAT_ERRORS_PANIC,	/* panic on error */
248c2ecf20Sopenharmony_ci	EXFAT_ERRORS_RO,	/* remount r/o on error */
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * exfat nls lossy flag
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_cienum {
318c2ecf20Sopenharmony_ci	NLS_NAME_NO_LOSSY,	/* no lossy */
328c2ecf20Sopenharmony_ci	NLS_NAME_LOSSY,		/* just detected incorrect filename(s) */
338c2ecf20Sopenharmony_ci	NLS_NAME_OVERLEN,	/* the length is over than its limit */
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define EXFAT_HASH_BITS		8
378c2ecf20Sopenharmony_ci#define EXFAT_HASH_SIZE		(1UL << EXFAT_HASH_BITS)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Type Definitions
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#define ES_2_ENTRIES		2
438c2ecf20Sopenharmony_ci#define ES_ALL_ENTRIES		0
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define DIR_DELETED		0xFFFFFFF7
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* type values */
488c2ecf20Sopenharmony_ci#define TYPE_UNUSED		0x0000
498c2ecf20Sopenharmony_ci#define TYPE_DELETED		0x0001
508c2ecf20Sopenharmony_ci#define TYPE_INVALID		0x0002
518c2ecf20Sopenharmony_ci#define TYPE_CRITICAL_PRI	0x0100
528c2ecf20Sopenharmony_ci#define TYPE_BITMAP		0x0101
538c2ecf20Sopenharmony_ci#define TYPE_UPCASE		0x0102
548c2ecf20Sopenharmony_ci#define TYPE_VOLUME		0x0103
558c2ecf20Sopenharmony_ci#define TYPE_DIR		0x0104
568c2ecf20Sopenharmony_ci#define TYPE_FILE		0x011F
578c2ecf20Sopenharmony_ci#define TYPE_CRITICAL_SEC	0x0200
588c2ecf20Sopenharmony_ci#define TYPE_STREAM		0x0201
598c2ecf20Sopenharmony_ci#define TYPE_EXTEND		0x0202
608c2ecf20Sopenharmony_ci#define TYPE_ACL		0x0203
618c2ecf20Sopenharmony_ci#define TYPE_BENIGN_PRI		0x0400
628c2ecf20Sopenharmony_ci#define TYPE_GUID		0x0401
638c2ecf20Sopenharmony_ci#define TYPE_PADDING		0x0402
648c2ecf20Sopenharmony_ci#define TYPE_ACLTAB		0x0403
658c2ecf20Sopenharmony_ci#define TYPE_BENIGN_SEC		0x0800
668c2ecf20Sopenharmony_ci#define TYPE_ALL		0x0FFF
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define MAX_CHARSET_SIZE	6 /* max size of multi-byte character */
698c2ecf20Sopenharmony_ci#define MAX_NAME_LENGTH		255 /* max len of file name excluding NULL */
708c2ecf20Sopenharmony_ci#define MAX_VFSNAME_BUF_SIZE	((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* Enough size to hold 256 dentry (even 512 Byte sector) */
738c2ecf20Sopenharmony_ci#define DIR_CACHE_SIZE		(256*sizeof(struct exfat_dentry)/512+1)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define EXFAT_HINT_NONE		-1
768c2ecf20Sopenharmony_ci#define EXFAT_MIN_SUBDIR	2
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * helpers for cluster size to byte conversion.
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ci#define EXFAT_CLU_TO_B(b, sbi)		((b) << (sbi)->cluster_size_bits)
828c2ecf20Sopenharmony_ci#define EXFAT_B_TO_CLU(b, sbi)		((b) >> (sbi)->cluster_size_bits)
838c2ecf20Sopenharmony_ci#define EXFAT_B_TO_CLU_ROUND_UP(b, sbi)	\
848c2ecf20Sopenharmony_ci	(((b - 1) >> (sbi)->cluster_size_bits) + 1)
858c2ecf20Sopenharmony_ci#define EXFAT_CLU_OFFSET(off, sbi)	((off) & ((sbi)->cluster_size - 1))
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * helpers for block size to byte conversion.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_ci#define EXFAT_BLK_TO_B(b, sb)		((b) << (sb)->s_blocksize_bits)
918c2ecf20Sopenharmony_ci#define EXFAT_B_TO_BLK(b, sb)		((b) >> (sb)->s_blocksize_bits)
928c2ecf20Sopenharmony_ci#define EXFAT_B_TO_BLK_ROUND_UP(b, sb)	\
938c2ecf20Sopenharmony_ci	(((b - 1) >> (sb)->s_blocksize_bits) + 1)
948c2ecf20Sopenharmony_ci#define EXFAT_BLK_OFFSET(off, sb)	((off) & ((sb)->s_blocksize - 1))
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/*
978c2ecf20Sopenharmony_ci * helpers for block size to dentry size conversion.
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ci#define EXFAT_B_TO_DEN_IDX(b, sbi)	\
1008c2ecf20Sopenharmony_ci	((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))
1018c2ecf20Sopenharmony_ci#define EXFAT_B_TO_DEN(b)		((b) >> DENTRY_SIZE_BITS)
1028c2ecf20Sopenharmony_ci#define EXFAT_DEN_TO_B(b)		((b) << DENTRY_SIZE_BITS)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * helpers for fat entry.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci#define FAT_ENT_SIZE (4)
1088c2ecf20Sopenharmony_ci#define FAT_ENT_SIZE_BITS (2)
1098c2ecf20Sopenharmony_ci#define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
1108c2ecf20Sopenharmony_ci	(((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
1118c2ecf20Sopenharmony_ci#define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc)	\
1128c2ecf20Sopenharmony_ci	((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/*
1158c2ecf20Sopenharmony_ci * helpers for bitmap.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ci#define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)
1188c2ecf20Sopenharmony_ci#define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)
1198c2ecf20Sopenharmony_ci#define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)
1208c2ecf20Sopenharmony_ci#define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)
1218c2ecf20Sopenharmony_ci#define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \
1228c2ecf20Sopenharmony_ci	((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)
1238c2ecf20Sopenharmony_ci#define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))
1248c2ecf20Sopenharmony_ci#define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \
1258c2ecf20Sopenharmony_ci	((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))
1268c2ecf20Sopenharmony_ci#define BITS_PER_BYTE_MASK	0x7
1278c2ecf20Sopenharmony_ci#define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistruct exfat_dentry_namebuf {
1308c2ecf20Sopenharmony_ci	char *lfn;
1318c2ecf20Sopenharmony_ci	int lfnbuf_len; /* usually MAX_UNINAME_BUF_SIZE */
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* unicode name structure */
1358c2ecf20Sopenharmony_cistruct exfat_uni_name {
1368c2ecf20Sopenharmony_ci	/* +3 for null and for converting */
1378c2ecf20Sopenharmony_ci	unsigned short name[MAX_NAME_LENGTH + 3];
1388c2ecf20Sopenharmony_ci	u16 name_hash;
1398c2ecf20Sopenharmony_ci	unsigned char name_len;
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/* directory structure */
1438c2ecf20Sopenharmony_cistruct exfat_chain {
1448c2ecf20Sopenharmony_ci	unsigned int dir;
1458c2ecf20Sopenharmony_ci	unsigned int size;
1468c2ecf20Sopenharmony_ci	unsigned char flags;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* first empty entry hint information */
1508c2ecf20Sopenharmony_cistruct exfat_hint_femp {
1518c2ecf20Sopenharmony_ci	/* entry index of a directory */
1528c2ecf20Sopenharmony_ci	int eidx;
1538c2ecf20Sopenharmony_ci	/* count of continuous empty entry */
1548c2ecf20Sopenharmony_ci	int count;
1558c2ecf20Sopenharmony_ci	/* the cluster that first empty slot exists in */
1568c2ecf20Sopenharmony_ci	struct exfat_chain cur;
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* hint structure */
1608c2ecf20Sopenharmony_cistruct exfat_hint {
1618c2ecf20Sopenharmony_ci	unsigned int clu;
1628c2ecf20Sopenharmony_ci	union {
1638c2ecf20Sopenharmony_ci		unsigned int off; /* cluster offset */
1648c2ecf20Sopenharmony_ci		int eidx; /* entry index */
1658c2ecf20Sopenharmony_ci	};
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistruct exfat_entry_set_cache {
1698c2ecf20Sopenharmony_ci	struct super_block *sb;
1708c2ecf20Sopenharmony_ci	bool modified;
1718c2ecf20Sopenharmony_ci	unsigned int start_off;
1728c2ecf20Sopenharmony_ci	int num_bh;
1738c2ecf20Sopenharmony_ci	struct buffer_head *__bh[DIR_CACHE_SIZE];
1748c2ecf20Sopenharmony_ci	struct buffer_head **bh;
1758c2ecf20Sopenharmony_ci	unsigned int num_entries;
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#define IS_DYNAMIC_ES(es)	((es)->__bh != (es)->bh)
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistruct exfat_dir_entry {
1818c2ecf20Sopenharmony_ci	struct exfat_chain dir;
1828c2ecf20Sopenharmony_ci	int entry;
1838c2ecf20Sopenharmony_ci	unsigned int type;
1848c2ecf20Sopenharmony_ci	unsigned int start_clu;
1858c2ecf20Sopenharmony_ci	unsigned char flags;
1868c2ecf20Sopenharmony_ci	unsigned short attr;
1878c2ecf20Sopenharmony_ci	loff_t size;
1888c2ecf20Sopenharmony_ci	unsigned int num_subdirs;
1898c2ecf20Sopenharmony_ci	struct timespec64 atime;
1908c2ecf20Sopenharmony_ci	struct timespec64 mtime;
1918c2ecf20Sopenharmony_ci	struct timespec64 crtime;
1928c2ecf20Sopenharmony_ci	struct exfat_dentry_namebuf namebuf;
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/*
1968c2ecf20Sopenharmony_ci * exfat mount in-memory data
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistruct exfat_mount_options {
1998c2ecf20Sopenharmony_ci	kuid_t fs_uid;
2008c2ecf20Sopenharmony_ci	kgid_t fs_gid;
2018c2ecf20Sopenharmony_ci	unsigned short fs_fmask;
2028c2ecf20Sopenharmony_ci	unsigned short fs_dmask;
2038c2ecf20Sopenharmony_ci	/* permission for setting the [am]time */
2048c2ecf20Sopenharmony_ci	unsigned short allow_utime;
2058c2ecf20Sopenharmony_ci	/* charset for filename input/display */
2068c2ecf20Sopenharmony_ci	char *iocharset;
2078c2ecf20Sopenharmony_ci	/* on error: continue, panic, remount-ro */
2088c2ecf20Sopenharmony_ci	enum exfat_error_mode errors;
2098c2ecf20Sopenharmony_ci	unsigned utf8:1, /* Use of UTF-8 character set */
2108c2ecf20Sopenharmony_ci		 discard:1; /* Issue discard requests on deletions */
2118c2ecf20Sopenharmony_ci	int time_offset; /* Offset of timestamps from UTC (in minutes) */
2128c2ecf20Sopenharmony_ci};
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/*
2158c2ecf20Sopenharmony_ci * EXFAT file system superblock in-memory data
2168c2ecf20Sopenharmony_ci */
2178c2ecf20Sopenharmony_cistruct exfat_sb_info {
2188c2ecf20Sopenharmony_ci	unsigned long long num_sectors; /* num of sectors in volume */
2198c2ecf20Sopenharmony_ci	unsigned int num_clusters; /* num of clusters in volume */
2208c2ecf20Sopenharmony_ci	unsigned int cluster_size; /* cluster size in bytes */
2218c2ecf20Sopenharmony_ci	unsigned int cluster_size_bits;
2228c2ecf20Sopenharmony_ci	unsigned int sect_per_clus; /* cluster size in sectors */
2238c2ecf20Sopenharmony_ci	unsigned int sect_per_clus_bits;
2248c2ecf20Sopenharmony_ci	unsigned long long FAT1_start_sector; /* FAT1 start sector */
2258c2ecf20Sopenharmony_ci	unsigned long long FAT2_start_sector; /* FAT2 start sector */
2268c2ecf20Sopenharmony_ci	unsigned long long data_start_sector; /* data area start sector */
2278c2ecf20Sopenharmony_ci	unsigned int num_FAT_sectors; /* num of FAT sectors */
2288c2ecf20Sopenharmony_ci	unsigned int root_dir; /* root dir cluster */
2298c2ecf20Sopenharmony_ci	unsigned int dentries_per_clu; /* num of dentries per cluster */
2308c2ecf20Sopenharmony_ci	unsigned int vol_flags; /* volume flags */
2318c2ecf20Sopenharmony_ci	unsigned int vol_flags_persistent; /* volume flags to retain */
2328c2ecf20Sopenharmony_ci	struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	unsigned int map_clu; /* allocation bitmap start cluster */
2358c2ecf20Sopenharmony_ci	unsigned int map_sectors; /* num of allocation bitmap sectors */
2368c2ecf20Sopenharmony_ci	struct buffer_head **vol_amap; /* allocation bitmap */
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	unsigned short *vol_utbl; /* upcase table */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	unsigned int clu_srch_ptr; /* cluster search pointer */
2418c2ecf20Sopenharmony_ci	unsigned int used_clusters; /* number of used clusters */
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	struct mutex s_lock; /* superblock lock */
2448c2ecf20Sopenharmony_ci	struct exfat_mount_options options;
2458c2ecf20Sopenharmony_ci	struct nls_table *nls_io; /* Charset used for input and display */
2468c2ecf20Sopenharmony_ci	struct ratelimit_state ratelimit;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	spinlock_t inode_hash_lock;
2498c2ecf20Sopenharmony_ci	struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	struct rcu_head rcu;
2528c2ecf20Sopenharmony_ci};
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define EXFAT_CACHE_VALID	0
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/*
2578c2ecf20Sopenharmony_ci * EXFAT file system inode in-memory data
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_cistruct exfat_inode_info {
2608c2ecf20Sopenharmony_ci	struct exfat_chain dir;
2618c2ecf20Sopenharmony_ci	int entry;
2628c2ecf20Sopenharmony_ci	unsigned int type;
2638c2ecf20Sopenharmony_ci	unsigned short attr;
2648c2ecf20Sopenharmony_ci	unsigned int start_clu;
2658c2ecf20Sopenharmony_ci	unsigned char flags;
2668c2ecf20Sopenharmony_ci	/*
2678c2ecf20Sopenharmony_ci	 * the copy of low 32bit of i_version to check
2688c2ecf20Sopenharmony_ci	 * the validation of hint_stat.
2698c2ecf20Sopenharmony_ci	 */
2708c2ecf20Sopenharmony_ci	unsigned int version;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* hint for cluster last accessed */
2738c2ecf20Sopenharmony_ci	struct exfat_hint hint_bmap;
2748c2ecf20Sopenharmony_ci	/* hint for entry index we try to lookup next time */
2758c2ecf20Sopenharmony_ci	struct exfat_hint hint_stat;
2768c2ecf20Sopenharmony_ci	/* hint for first empty entry */
2778c2ecf20Sopenharmony_ci	struct exfat_hint_femp hint_femp;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	spinlock_t cache_lru_lock;
2808c2ecf20Sopenharmony_ci	struct list_head cache_lru;
2818c2ecf20Sopenharmony_ci	int nr_caches;
2828c2ecf20Sopenharmony_ci	/* for avoiding the race between alloc and free */
2838c2ecf20Sopenharmony_ci	unsigned int cache_valid_id;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/*
2868c2ecf20Sopenharmony_ci	 * NOTE: i_size_ondisk is 64bits, so must hold ->inode_lock to access.
2878c2ecf20Sopenharmony_ci	 * physically allocated size.
2888c2ecf20Sopenharmony_ci	 */
2898c2ecf20Sopenharmony_ci	loff_t i_size_ondisk;
2908c2ecf20Sopenharmony_ci	/* block-aligned i_size (used in cont_write_begin) */
2918c2ecf20Sopenharmony_ci	loff_t i_size_aligned;
2928c2ecf20Sopenharmony_ci	/* on-disk position of directory entry or 0 */
2938c2ecf20Sopenharmony_ci	loff_t i_pos;
2948c2ecf20Sopenharmony_ci	/* hash by i_location */
2958c2ecf20Sopenharmony_ci	struct hlist_node i_hash_fat;
2968c2ecf20Sopenharmony_ci	/* protect bmap against truncate */
2978c2ecf20Sopenharmony_ci	struct rw_semaphore truncate_lock;
2988c2ecf20Sopenharmony_ci	struct inode vfs_inode;
2998c2ecf20Sopenharmony_ci	/* File creation time */
3008c2ecf20Sopenharmony_ci	struct timespec64 i_crtime;
3018c2ecf20Sopenharmony_ci};
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	return sb->s_fs_info;
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	return container_of(inode, struct exfat_inode_info, vfs_inode);
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/*
3148c2ecf20Sopenharmony_ci * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to
3158c2ecf20Sopenharmony_ci * save ATTR_RO instead of ->i_mode.
3168c2ecf20Sopenharmony_ci *
3178c2ecf20Sopenharmony_ci * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
3188c2ecf20Sopenharmony_ci * bit, it's just used as flag for app.
3198c2ecf20Sopenharmony_ci */
3208c2ecf20Sopenharmony_cistatic inline int exfat_mode_can_hold_ro(struct inode *inode)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (S_ISDIR(inode->i_mode))
3258c2ecf20Sopenharmony_ci		return 0;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if ((~sbi->options.fs_fmask) & 0222)
3288c2ecf20Sopenharmony_ci		return 1;
3298c2ecf20Sopenharmony_ci	return 0;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci/* Convert attribute bits and a mask to the UNIX mode. */
3338c2ecf20Sopenharmony_cistatic inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
3348c2ecf20Sopenharmony_ci		unsigned short attr, mode_t mode)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
3378c2ecf20Sopenharmony_ci		mode &= ~0222;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	if (attr & ATTR_SUBDIR)
3408c2ecf20Sopenharmony_ci		return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	return (mode & ~sbi->options.fs_fmask) | S_IFREG;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/* Return the FAT attribute byte for this inode */
3468c2ecf20Sopenharmony_cistatic inline unsigned short exfat_make_attr(struct inode *inode)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	unsigned short attr = EXFAT_I(inode)->attr;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (S_ISDIR(inode->i_mode))
3518c2ecf20Sopenharmony_ci		attr |= ATTR_SUBDIR;
3528c2ecf20Sopenharmony_ci	if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
3538c2ecf20Sopenharmony_ci		attr |= ATTR_READONLY;
3548c2ecf20Sopenharmony_ci	return attr;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic inline void exfat_save_attr(struct inode *inode, unsigned short attr)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	if (exfat_mode_can_hold_ro(inode))
3608c2ecf20Sopenharmony_ci		EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY);
3618c2ecf20Sopenharmony_ci	else
3628c2ecf20Sopenharmony_ci		EXFAT_I(inode)->attr = attr & ATTR_RWMASK;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
3668c2ecf20Sopenharmony_ci		sector_t sec)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	return ((sec - sbi->data_start_sector + 1) &
3698c2ecf20Sopenharmony_ci		((1 << sbi->sect_per_clus_bits) - 1)) == 0;
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
3738c2ecf20Sopenharmony_ci		unsigned int clus)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
3768c2ecf20Sopenharmony_ci		sbi->data_start_sector;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
3808c2ecf20Sopenharmony_ci		sector_t sec)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
3838c2ecf20Sopenharmony_ci		EXFAT_RESERVED_CLUSTERS;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic inline bool is_valid_cluster(struct exfat_sb_info *sbi,
3878c2ecf20Sopenharmony_ci		unsigned int clus)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus)
3908c2ecf20Sopenharmony_ci		return false;
3918c2ecf20Sopenharmony_ci	return true;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/* super.c */
3958c2ecf20Sopenharmony_ciint exfat_set_volume_dirty(struct super_block *sb);
3968c2ecf20Sopenharmony_ciint exfat_clear_volume_dirty(struct super_block *sb);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci/* fatent.c */
3998c2ecf20Sopenharmony_ci#define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ciint exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
4028c2ecf20Sopenharmony_ci		struct exfat_chain *p_chain);
4038c2ecf20Sopenharmony_ciint exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
4048c2ecf20Sopenharmony_ciint exfat_ent_get(struct super_block *sb, unsigned int loc,
4058c2ecf20Sopenharmony_ci		unsigned int *content);
4068c2ecf20Sopenharmony_ciint exfat_ent_set(struct super_block *sb, unsigned int loc,
4078c2ecf20Sopenharmony_ci		unsigned int content);
4088c2ecf20Sopenharmony_ciint exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
4098c2ecf20Sopenharmony_ci		int entry, struct exfat_dentry *p_entry);
4108c2ecf20Sopenharmony_ciint exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
4118c2ecf20Sopenharmony_ci		unsigned int len);
4128c2ecf20Sopenharmony_ciint exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
4138c2ecf20Sopenharmony_ciint exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
4148c2ecf20Sopenharmony_ci		unsigned int *ret_clu);
4158c2ecf20Sopenharmony_ciint exfat_count_num_clusters(struct super_block *sb,
4168c2ecf20Sopenharmony_ci		struct exfat_chain *p_chain, unsigned int *ret_count);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci/* balloc.c */
4198c2ecf20Sopenharmony_ciint exfat_load_bitmap(struct super_block *sb);
4208c2ecf20Sopenharmony_civoid exfat_free_bitmap(struct exfat_sb_info *sbi);
4218c2ecf20Sopenharmony_ciint exfat_set_bitmap(struct inode *inode, unsigned int clu);
4228c2ecf20Sopenharmony_civoid exfat_clear_bitmap(struct inode *inode, unsigned int clu);
4238c2ecf20Sopenharmony_ciunsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
4248c2ecf20Sopenharmony_ciint exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/* file.c */
4278c2ecf20Sopenharmony_ciextern const struct file_operations exfat_file_operations;
4288c2ecf20Sopenharmony_ciint __exfat_truncate(struct inode *inode, loff_t new_size);
4298c2ecf20Sopenharmony_civoid exfat_truncate(struct inode *inode, loff_t size);
4308c2ecf20Sopenharmony_ciint exfat_setattr(struct dentry *dentry, struct iattr *attr);
4318c2ecf20Sopenharmony_ciint exfat_getattr(const struct path *path, struct kstat *stat,
4328c2ecf20Sopenharmony_ci		unsigned int request_mask, unsigned int query_flags);
4338c2ecf20Sopenharmony_ciint exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci/* namei.c */
4368c2ecf20Sopenharmony_ciextern const struct dentry_operations exfat_dentry_ops;
4378c2ecf20Sopenharmony_ciextern const struct dentry_operations exfat_utf8_dentry_ops;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci/* cache.c */
4408c2ecf20Sopenharmony_ciint exfat_cache_init(void);
4418c2ecf20Sopenharmony_civoid exfat_cache_shutdown(void);
4428c2ecf20Sopenharmony_civoid exfat_cache_inval_inode(struct inode *inode);
4438c2ecf20Sopenharmony_ciint exfat_get_cluster(struct inode *inode, unsigned int cluster,
4448c2ecf20Sopenharmony_ci		unsigned int *fclus, unsigned int *dclus,
4458c2ecf20Sopenharmony_ci		unsigned int *last_dclus, int allow_eof);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci/* dir.c */
4488c2ecf20Sopenharmony_ciextern const struct inode_operations exfat_dir_inode_operations;
4498c2ecf20Sopenharmony_ciextern const struct file_operations exfat_dir_operations;
4508c2ecf20Sopenharmony_ciunsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
4518c2ecf20Sopenharmony_ciint exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
4528c2ecf20Sopenharmony_ci		int entry, unsigned int type, unsigned int start_clu,
4538c2ecf20Sopenharmony_ci		unsigned long long size);
4548c2ecf20Sopenharmony_ciint exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
4558c2ecf20Sopenharmony_ci		int entry, int num_entries, struct exfat_uni_name *p_uniname);
4568c2ecf20Sopenharmony_ciint exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
4578c2ecf20Sopenharmony_ci		int entry, int order, int num_entries);
4588c2ecf20Sopenharmony_ciint exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
4598c2ecf20Sopenharmony_ci		int entry);
4608c2ecf20Sopenharmony_civoid exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
4618c2ecf20Sopenharmony_ciint exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
4628c2ecf20Sopenharmony_ciint exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
4638c2ecf20Sopenharmony_ci		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
4648c2ecf20Sopenharmony_ci		int num_entries, unsigned int type, struct exfat_hint *hint_opt);
4658c2ecf20Sopenharmony_ciint exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
4668c2ecf20Sopenharmony_ciint exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
4678c2ecf20Sopenharmony_ci		int entry, sector_t *sector, int *offset);
4688c2ecf20Sopenharmony_cistruct exfat_dentry *exfat_get_dentry(struct super_block *sb,
4698c2ecf20Sopenharmony_ci		struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
4708c2ecf20Sopenharmony_ci		sector_t *sector);
4718c2ecf20Sopenharmony_cistruct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
4728c2ecf20Sopenharmony_ci		int num);
4738c2ecf20Sopenharmony_cistruct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
4748c2ecf20Sopenharmony_ci		struct exfat_chain *p_dir, int entry, unsigned int type);
4758c2ecf20Sopenharmony_ciint exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync);
4768c2ecf20Sopenharmony_ciint exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/* inode.c */
4798c2ecf20Sopenharmony_ciextern const struct inode_operations exfat_file_inode_operations;
4808c2ecf20Sopenharmony_civoid exfat_sync_inode(struct inode *inode);
4818c2ecf20Sopenharmony_cistruct inode *exfat_build_inode(struct super_block *sb,
4828c2ecf20Sopenharmony_ci		struct exfat_dir_entry *info, loff_t i_pos);
4838c2ecf20Sopenharmony_civoid exfat_hash_inode(struct inode *inode, loff_t i_pos);
4848c2ecf20Sopenharmony_civoid exfat_unhash_inode(struct inode *inode);
4858c2ecf20Sopenharmony_cistruct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
4868c2ecf20Sopenharmony_ciint exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
4878c2ecf20Sopenharmony_civoid exfat_evict_inode(struct inode *inode);
4888c2ecf20Sopenharmony_ciint exfat_block_truncate_page(struct inode *inode, loff_t from);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/* exfat/nls.c */
4918c2ecf20Sopenharmony_ciunsigned short exfat_toupper(struct super_block *sb, unsigned short a);
4928c2ecf20Sopenharmony_ciint exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
4938c2ecf20Sopenharmony_ci		unsigned short *b, unsigned int len);
4948c2ecf20Sopenharmony_ciint exfat_utf16_to_nls(struct super_block *sb,
4958c2ecf20Sopenharmony_ci		struct exfat_uni_name *uniname, unsigned char *p_cstring,
4968c2ecf20Sopenharmony_ci		int len);
4978c2ecf20Sopenharmony_ciint exfat_nls_to_utf16(struct super_block *sb,
4988c2ecf20Sopenharmony_ci		const unsigned char *p_cstring, const int len,
4998c2ecf20Sopenharmony_ci		struct exfat_uni_name *uniname, int *p_lossy);
5008c2ecf20Sopenharmony_ciint exfat_create_upcase_table(struct super_block *sb);
5018c2ecf20Sopenharmony_civoid exfat_free_upcase_table(struct exfat_sb_info *sbi);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/* exfat/misc.c */
5048c2ecf20Sopenharmony_civoid __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
5058c2ecf20Sopenharmony_ci		__printf(3, 4) __cold;
5068c2ecf20Sopenharmony_ci#define exfat_fs_error(sb, fmt, args...)          \
5078c2ecf20Sopenharmony_ci		__exfat_fs_error(sb, 1, fmt, ## args)
5088c2ecf20Sopenharmony_ci#define exfat_fs_error_ratelimit(sb, fmt, args...) \
5098c2ecf20Sopenharmony_ci		__exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
5108c2ecf20Sopenharmony_ci		fmt, ## args)
5118c2ecf20Sopenharmony_civoid exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...)
5128c2ecf20Sopenharmony_ci		__printf(3, 4) __cold;
5138c2ecf20Sopenharmony_ci#define exfat_err(sb, fmt, ...)						\
5148c2ecf20Sopenharmony_ci	exfat_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
5158c2ecf20Sopenharmony_ci#define exfat_warn(sb, fmt, ...)					\
5168c2ecf20Sopenharmony_ci	exfat_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
5178c2ecf20Sopenharmony_ci#define exfat_info(sb, fmt, ...)					\
5188c2ecf20Sopenharmony_ci	exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__)
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_civoid exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
5218c2ecf20Sopenharmony_ci		u8 tz, __le16 time, __le16 date, u8 time_cs);
5228c2ecf20Sopenharmony_civoid exfat_truncate_atime(struct timespec64 *ts);
5238c2ecf20Sopenharmony_civoid exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
5248c2ecf20Sopenharmony_ci		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
5258c2ecf20Sopenharmony_ciu16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
5268c2ecf20Sopenharmony_ciu32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
5278c2ecf20Sopenharmony_civoid exfat_update_bh(struct buffer_head *bh, int sync);
5288c2ecf20Sopenharmony_ciint exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);
5298c2ecf20Sopenharmony_civoid exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
5308c2ecf20Sopenharmony_ci		unsigned int size, unsigned char flags);
5318c2ecf20Sopenharmony_civoid exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci#endif /* !_EXFAT_FS_H */
534