577 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.4 */5 6#ifndef _EXFAT_FS_H7#define _EXFAT_FS_H8 9#include <linux/fs.h>10#include <linux/ratelimit.h>11#include <linux/nls.h>12#include <linux/blkdev.h>13#include <uapi/linux/exfat.h>14 15#define EXFAT_ROOT_INO 116 17#define EXFAT_CLUSTERS_UNTRACKED (~0u)18 19/*20 * exfat error flags21 */22enum exfat_error_mode {23 EXFAT_ERRORS_CONT, /* ignore error and continue */24 EXFAT_ERRORS_PANIC, /* panic on error */25 EXFAT_ERRORS_RO, /* remount r/o on error */26};27 28/*29 * exfat nls lossy flag30 */31enum {32 NLS_NAME_NO_LOSSY = 0, /* no lossy */33 NLS_NAME_LOSSY = 1 << 0, /* just detected incorrect filename(s) */34 NLS_NAME_OVERLEN = 1 << 1, /* the length is over than its limit */35};36 37#define EXFAT_HASH_BITS 838#define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS)39 40/*41 * Type Definitions42 */43#define ES_2_ENTRIES 244#define ES_ALL_ENTRIES 045 46#define ES_IDX_FILE 047#define ES_IDX_STREAM 148#define ES_IDX_FIRST_FILENAME 249#define EXFAT_FILENAME_ENTRY_NUM(name_len) \50 DIV_ROUND_UP(name_len, EXFAT_FILE_NAME_LEN)51#define ES_IDX_LAST_FILENAME(name_len) \52 (ES_IDX_FIRST_FILENAME + EXFAT_FILENAME_ENTRY_NUM(name_len) - 1)53 54#define DIR_DELETED 0xFFFFFFF755 56/* type values */57#define TYPE_UNUSED 0x000058#define TYPE_DELETED 0x000159#define TYPE_INVALID 0x000260#define TYPE_CRITICAL_PRI 0x010061#define TYPE_BITMAP 0x010162#define TYPE_UPCASE 0x010263#define TYPE_VOLUME 0x010364#define TYPE_DIR 0x010465#define TYPE_FILE 0x011F66#define TYPE_CRITICAL_SEC 0x020067#define TYPE_STREAM 0x020168#define TYPE_EXTEND 0x020269#define TYPE_ACL 0x020370#define TYPE_BENIGN_PRI 0x040071#define TYPE_GUID 0x040172#define TYPE_PADDING 0x040273#define TYPE_ACLTAB 0x040374#define TYPE_BENIGN_SEC 0x080075#define TYPE_VENDOR_EXT 0x080176#define TYPE_VENDOR_ALLOC 0x080277 78#define MAX_CHARSET_SIZE 6 /* max size of multi-byte character */79#define MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */80#define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)81 82#define EXFAT_HINT_NONE -183#define EXFAT_MIN_SUBDIR 284 85/*86 * helpers for cluster size to byte conversion.87 */88#define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits)89#define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits)90#define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \91 (((b - 1) >> (sbi)->cluster_size_bits) + 1)92#define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1))93 94/*95 * helpers for block size to byte conversion.96 */97#define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits)98#define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits)99#define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \100 (((b - 1) >> (sb)->s_blocksize_bits) + 1)101#define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1))102 103/*104 * helpers for block size to dentry size conversion.105 */106#define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS)107#define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS)108 109/*110 * helpers for cluster size to dentry size conversion.111 */112#define EXFAT_CLU_TO_DEN(clu, sbi) \113 ((clu) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))114#define EXFAT_DEN_TO_CLU(dentry, sbi) \115 ((dentry) >> ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))116 117/*118 * helpers for fat entry.119 */120#define FAT_ENT_SIZE (4)121#define FAT_ENT_SIZE_BITS (2)122#define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \123 (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))124#define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \125 ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))126 127/*128 * helpers for bitmap.129 */130#define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)131#define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)132#define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)133#define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)134#define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \135 ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)136#define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))137#define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \138 ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))139#define IGNORED_BITS_REMAINED(clu, clu_base) ((1UL << ((clu) - (clu_base))) - 1)140 141#define ES_ENTRY_NUM(name_len) (ES_IDX_LAST_FILENAME(name_len) + 1)142/* 19 entries = 1 file entry + 1 stream entry + 17 filename entries */143#define ES_MAX_ENTRY_NUM ES_ENTRY_NUM(MAX_NAME_LENGTH)144 145/*146 * 19 entries x 32 bytes/entry = 608 bytes.147 * The 608 bytes are in 3 sectors at most (even 512 Byte sector).148 */149#define DIR_CACHE_SIZE \150 (DIV_ROUND_UP(EXFAT_DEN_TO_B(ES_MAX_ENTRY_NUM), SECTOR_SIZE) + 1)151 152/* Superblock flags */153#define EXFAT_FLAGS_SHUTDOWN 1154 155struct exfat_dentry_namebuf {156 char *lfn;157 int lfnbuf_len; /* usually MAX_UNINAME_BUF_SIZE */158};159 160/* unicode name structure */161struct exfat_uni_name {162 /* +3 for null and for converting */163 unsigned short name[MAX_NAME_LENGTH + 3];164 u16 name_hash;165 unsigned char name_len;166};167 168/* directory structure */169struct exfat_chain {170 unsigned int dir;171 unsigned int size;172 unsigned char flags;173};174 175/* first empty entry hint information */176struct exfat_hint_femp {177 /* entry index of a directory */178 int eidx;179 /* count of continuous empty entry */180 int count;181 /* the cluster that first empty slot exists in */182 struct exfat_chain cur;183};184 185/* hint structure */186struct exfat_hint {187 unsigned int clu;188 union {189 unsigned int off; /* cluster offset */190 int eidx; /* entry index */191 };192};193 194struct exfat_entry_set_cache {195 struct super_block *sb;196 unsigned int start_off;197 int num_bh;198 struct buffer_head *__bh[DIR_CACHE_SIZE];199 struct buffer_head **bh;200 unsigned int num_entries;201 bool modified;202};203 204#define IS_DYNAMIC_ES(es) ((es)->__bh != (es)->bh)205 206struct exfat_dir_entry {207 struct exfat_chain dir;208 int entry;209 unsigned int type;210 unsigned int start_clu;211 unsigned char flags;212 unsigned short attr;213 loff_t size;214 loff_t valid_size;215 unsigned int num_subdirs;216 struct timespec64 atime;217 struct timespec64 mtime;218 struct timespec64 crtime;219 struct exfat_dentry_namebuf namebuf;220};221 222/*223 * exfat mount in-memory data224 */225struct exfat_mount_options {226 kuid_t fs_uid;227 kgid_t fs_gid;228 unsigned short fs_fmask;229 unsigned short fs_dmask;230 /* permission for setting the [am]time */231 unsigned short allow_utime;232 /* charset for filename input/display */233 char *iocharset;234 /* on error: continue, panic, remount-ro */235 enum exfat_error_mode errors;236 unsigned utf8:1, /* Use of UTF-8 character set */237 sys_tz:1, /* Use local timezone */238 discard:1, /* Issue discard requests on deletions */239 keep_last_dots:1; /* Keep trailing periods in paths */240 int time_offset; /* Offset of timestamps from UTC (in minutes) */241 /* Support creating zero-size directory, default: false */242 bool zero_size_dir;243};244 245/*246 * EXFAT file system superblock in-memory data247 */248struct exfat_sb_info {249 unsigned long long num_sectors; /* num of sectors in volume */250 unsigned int num_clusters; /* num of clusters in volume */251 unsigned int cluster_size; /* cluster size in bytes */252 unsigned int cluster_size_bits;253 unsigned int sect_per_clus; /* cluster size in sectors */254 unsigned int sect_per_clus_bits;255 unsigned long long FAT1_start_sector; /* FAT1 start sector */256 unsigned long long FAT2_start_sector; /* FAT2 start sector */257 unsigned long long data_start_sector; /* data area start sector */258 unsigned int num_FAT_sectors; /* num of FAT sectors */259 unsigned int root_dir; /* root dir cluster */260 unsigned int dentries_per_clu; /* num of dentries per cluster */261 unsigned int vol_flags; /* volume flags */262 unsigned int vol_flags_persistent; /* volume flags to retain */263 struct buffer_head *boot_bh; /* buffer_head of BOOT sector */264 265 unsigned int map_clu; /* allocation bitmap start cluster */266 unsigned int map_sectors; /* num of allocation bitmap sectors */267 struct buffer_head **vol_amap; /* allocation bitmap */268 269 unsigned short *vol_utbl; /* upcase table */270 271 unsigned int clu_srch_ptr; /* cluster search pointer */272 unsigned int used_clusters; /* number of used clusters */273 274 unsigned long s_exfat_flags; /* Exfat superblock flags */275 276 struct mutex s_lock; /* superblock lock */277 struct mutex bitmap_lock; /* bitmap lock */278 struct exfat_mount_options options;279 struct nls_table *nls_io; /* Charset used for input and display */280 struct ratelimit_state ratelimit;281 282 spinlock_t inode_hash_lock;283 struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];284 struct rcu_head rcu;285};286 287#define EXFAT_CACHE_VALID 0288 289/*290 * EXFAT file system inode in-memory data291 */292struct exfat_inode_info {293 struct exfat_chain dir;294 int entry;295 unsigned int type;296 unsigned short attr;297 unsigned int start_clu;298 unsigned char flags;299 /*300 * the copy of low 32bit of i_version to check301 * the validation of hint_stat.302 */303 unsigned int version;304 305 /* hint for cluster last accessed */306 struct exfat_hint hint_bmap;307 /* hint for entry index we try to lookup next time */308 struct exfat_hint hint_stat;309 /* hint for first empty entry */310 struct exfat_hint_femp hint_femp;311 312 spinlock_t cache_lru_lock;313 struct list_head cache_lru;314 int nr_caches;315 /* for avoiding the race between alloc and free */316 unsigned int cache_valid_id;317 318 /* on-disk position of directory entry or 0 */319 loff_t i_pos;320 loff_t valid_size;321 /* hash by i_location */322 struct hlist_node i_hash_fat;323 /* protect bmap against truncate */324 struct rw_semaphore truncate_lock;325 struct inode vfs_inode;326 /* File creation time */327 struct timespec64 i_crtime;328};329 330static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)331{332 return sb->s_fs_info;333}334 335static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)336{337 return container_of(inode, struct exfat_inode_info, vfs_inode);338}339 340static inline int exfat_forced_shutdown(struct super_block *sb)341{342 return test_bit(EXFAT_FLAGS_SHUTDOWN, &EXFAT_SB(sb)->s_exfat_flags);343}344 345/*346 * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to347 * save ATTR_RO instead of ->i_mode.348 *349 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only350 * bit, it's just used as flag for app.351 */352static inline int exfat_mode_can_hold_ro(struct inode *inode)353{354 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);355 356 if (S_ISDIR(inode->i_mode))357 return 0;358 359 if ((~sbi->options.fs_fmask) & 0222)360 return 1;361 return 0;362}363 364/* Convert attribute bits and a mask to the UNIX mode. */365static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,366 unsigned short attr, mode_t mode)367{368 if ((attr & EXFAT_ATTR_READONLY) && !(attr & EXFAT_ATTR_SUBDIR))369 mode &= ~0222;370 371 if (attr & EXFAT_ATTR_SUBDIR)372 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;373 374 return (mode & ~sbi->options.fs_fmask) | S_IFREG;375}376 377/* Return the FAT attribute byte for this inode */378static inline unsigned short exfat_make_attr(struct inode *inode)379{380 unsigned short attr = EXFAT_I(inode)->attr;381 382 if (S_ISDIR(inode->i_mode))383 attr |= EXFAT_ATTR_SUBDIR;384 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))385 attr |= EXFAT_ATTR_READONLY;386 return attr;387}388 389static inline void exfat_save_attr(struct inode *inode, unsigned short attr)390{391 if (exfat_mode_can_hold_ro(inode))392 EXFAT_I(inode)->attr = attr & (EXFAT_ATTR_RWMASK | EXFAT_ATTR_READONLY);393 else394 EXFAT_I(inode)->attr = attr & EXFAT_ATTR_RWMASK;395}396 397static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,398 sector_t sec)399{400 return ((sec - sbi->data_start_sector + 1) &401 ((1 << sbi->sect_per_clus_bits) - 1)) == 0;402}403 404static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,405 unsigned int clus)406{407 return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +408 sbi->data_start_sector;409}410 411static inline unsigned int exfat_sector_to_cluster(struct exfat_sb_info *sbi,412 sector_t sec)413{414 return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +415 EXFAT_RESERVED_CLUSTERS;416}417 418static inline bool is_valid_cluster(struct exfat_sb_info *sbi,419 unsigned int clus)420{421 return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;422}423 424static inline loff_t exfat_ondisk_size(const struct inode *inode)425{426 return ((loff_t)inode->i_blocks) << 9;427}428 429/* super.c */430int exfat_set_volume_dirty(struct super_block *sb);431int exfat_clear_volume_dirty(struct super_block *sb);432 433/* fatent.c */434#define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)435 436int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,437 struct exfat_chain *p_chain, bool sync_bmap);438int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);439int exfat_ent_get(struct super_block *sb, unsigned int loc,440 unsigned int *content);441int exfat_ent_set(struct super_block *sb, unsigned int loc,442 unsigned int content);443int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,444 unsigned int len);445int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);446int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,447 unsigned int *ret_clu);448int exfat_count_num_clusters(struct super_block *sb,449 struct exfat_chain *p_chain, unsigned int *ret_count);450 451/* balloc.c */452int exfat_load_bitmap(struct super_block *sb);453void exfat_free_bitmap(struct exfat_sb_info *sbi);454int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);455void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);456unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);457int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);458int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);459 460/* file.c */461extern const struct file_operations exfat_file_operations;462int __exfat_truncate(struct inode *inode);463void exfat_truncate(struct inode *inode);464int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,465 struct iattr *attr);466int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,467 struct kstat *stat, unsigned int request_mask,468 unsigned int query_flags);469int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);470long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);471long exfat_compat_ioctl(struct file *filp, unsigned int cmd,472 unsigned long arg);473int exfat_force_shutdown(struct super_block *sb, u32 flags);474 475/* namei.c */476extern const struct dentry_operations exfat_dentry_ops;477extern const struct dentry_operations exfat_utf8_dentry_ops;478 479/* cache.c */480int exfat_cache_init(void);481void exfat_cache_shutdown(void);482void exfat_cache_inval_inode(struct inode *inode);483int exfat_get_cluster(struct inode *inode, unsigned int cluster,484 unsigned int *fclus, unsigned int *dclus,485 unsigned int *last_dclus, int allow_eof);486 487/* dir.c */488extern const struct inode_operations exfat_dir_inode_operations;489extern const struct file_operations exfat_dir_operations;490unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);491void exfat_init_dir_entry(struct exfat_entry_set_cache *es,492 unsigned int type, unsigned int start_clu,493 unsigned long long size, struct timespec64 *ts);494void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,495 struct exfat_uni_name *p_uniname);496void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,497 int order);498void exfat_update_dir_chksum(struct exfat_entry_set_cache *es);499int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);500int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,501 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,502 struct exfat_hint *hint_opt);503int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);504struct exfat_dentry *exfat_get_dentry(struct super_block *sb,505 struct exfat_chain *p_dir, int entry, struct buffer_head **bh);506struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,507 int num);508int exfat_get_dentry_set(struct exfat_entry_set_cache *es,509 struct super_block *sb, struct exfat_chain *p_dir, int entry,510 unsigned int num_entries);511int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,512 struct super_block *sb, struct exfat_chain *p_dir, int entry,513 unsigned int num_entries);514int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync);515int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);516 517/* inode.c */518extern const struct inode_operations exfat_file_inode_operations;519void exfat_sync_inode(struct inode *inode);520struct inode *exfat_build_inode(struct super_block *sb,521 struct exfat_dir_entry *info, loff_t i_pos);522void exfat_hash_inode(struct inode *inode, loff_t i_pos);523void exfat_unhash_inode(struct inode *inode);524struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);525int __exfat_write_inode(struct inode *inode, int sync);526int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);527void exfat_evict_inode(struct inode *inode);528int exfat_block_truncate_page(struct inode *inode, loff_t from);529 530/* exfat/nls.c */531unsigned short exfat_toupper(struct super_block *sb, unsigned short a);532int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,533 unsigned short *b, unsigned int len);534int exfat_utf16_to_nls(struct super_block *sb,535 struct exfat_uni_name *uniname, unsigned char *p_cstring,536 int len);537int exfat_nls_to_utf16(struct super_block *sb,538 const unsigned char *p_cstring, const int len,539 struct exfat_uni_name *uniname, int *p_lossy);540int exfat_create_upcase_table(struct super_block *sb);541void exfat_free_upcase_table(struct exfat_sb_info *sbi);542 543/* exfat/misc.c */544void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)545 __printf(3, 4) __cold;546#define exfat_fs_error(sb, fmt, args...) \547 __exfat_fs_error(sb, 1, fmt, ## args)548#define exfat_fs_error_ratelimit(sb, fmt, args...) \549 __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \550 fmt, ## args)551 552/* expand to pr_*() with prefix */553#define exfat_err(sb, fmt, ...) \554 pr_err("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)555#define exfat_warn(sb, fmt, ...) \556 pr_warn("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)557#define exfat_info(sb, fmt, ...) \558 pr_info("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)559#define exfat_debug(sb, fmt, ...) \560 pr_debug("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)561 562void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,563 u8 tz, __le16 time, __le16 date, u8 time_cs);564void exfat_truncate_atime(struct timespec64 *ts);565void exfat_truncate_inode_atime(struct inode *inode);566void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,567 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);568u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);569u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);570void exfat_update_bh(struct buffer_head *bh, int sync);571int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);572void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,573 unsigned int size, unsigned char flags);574void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);575 576#endif /* !_EXFAT_FS_H */577