0001
0002
0003
0004
0005
0006 #ifndef _EXFAT_FS_H
0007 #define _EXFAT_FS_H
0008
0009 #include <linux/fs.h>
0010 #include <linux/ratelimit.h>
0011 #include <linux/nls.h>
0012
0013 #define EXFAT_ROOT_INO 1
0014
0015 #define EXFAT_CLUSTERS_UNTRACKED (~0u)
0016
0017
0018
0019
0020 enum exfat_error_mode {
0021 EXFAT_ERRORS_CONT,
0022 EXFAT_ERRORS_PANIC,
0023 EXFAT_ERRORS_RO,
0024 };
0025
0026
0027
0028
0029 enum {
0030 NLS_NAME_NO_LOSSY = 0,
0031 NLS_NAME_LOSSY = 1 << 0,
0032 NLS_NAME_OVERLEN = 1 << 1,
0033 };
0034
0035 #define EXFAT_HASH_BITS 8
0036 #define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS)
0037
0038
0039
0040
0041 #define ES_2_ENTRIES 2
0042 #define ES_ALL_ENTRIES 0
0043
0044 #define DIR_DELETED 0xFFFF0321
0045
0046
0047 #define TYPE_UNUSED 0x0000
0048 #define TYPE_DELETED 0x0001
0049 #define TYPE_INVALID 0x0002
0050 #define TYPE_CRITICAL_PRI 0x0100
0051 #define TYPE_BITMAP 0x0101
0052 #define TYPE_UPCASE 0x0102
0053 #define TYPE_VOLUME 0x0103
0054 #define TYPE_DIR 0x0104
0055 #define TYPE_FILE 0x011F
0056 #define TYPE_CRITICAL_SEC 0x0200
0057 #define TYPE_STREAM 0x0201
0058 #define TYPE_EXTEND 0x0202
0059 #define TYPE_ACL 0x0203
0060 #define TYPE_BENIGN_PRI 0x0400
0061 #define TYPE_GUID 0x0401
0062 #define TYPE_PADDING 0x0402
0063 #define TYPE_ACLTAB 0x0403
0064 #define TYPE_BENIGN_SEC 0x0800
0065 #define TYPE_ALL 0x0FFF
0066
0067 #define MAX_CHARSET_SIZE 6
0068 #define MAX_NAME_LENGTH 255
0069 #define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
0070
0071
0072 #define DIR_CACHE_SIZE (256*sizeof(struct exfat_dentry)/512+1)
0073
0074 #define EXFAT_HINT_NONE -1
0075 #define EXFAT_MIN_SUBDIR 2
0076
0077
0078
0079
0080 #define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits)
0081 #define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits)
0082 #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \
0083 (((b - 1) >> (sbi)->cluster_size_bits) + 1)
0084 #define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1))
0085
0086
0087
0088
0089 #define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits)
0090 #define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits)
0091 #define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \
0092 (((b - 1) >> (sb)->s_blocksize_bits) + 1)
0093 #define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1))
0094
0095
0096
0097
0098 #define EXFAT_B_TO_DEN_IDX(b, sbi) \
0099 ((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))
0100 #define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS)
0101 #define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS)
0102
0103
0104
0105
0106 #define FAT_ENT_SIZE (4)
0107 #define FAT_ENT_SIZE_BITS (2)
0108 #define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
0109 (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
0110 #define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \
0111 ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
0112
0113
0114
0115
0116 #define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)
0117 #define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)
0118 #define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)
0119 #define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)
0120 #define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \
0121 ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)
0122 #define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))
0123 #define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \
0124 ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))
0125 #define BITS_PER_BYTE_MASK 0x7
0126 #define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1)
0127
0128 struct exfat_dentry_namebuf {
0129 char *lfn;
0130 int lfnbuf_len;
0131 };
0132
0133
0134 struct exfat_uni_name {
0135
0136 unsigned short name[MAX_NAME_LENGTH + 3];
0137 u16 name_hash;
0138 unsigned char name_len;
0139 };
0140
0141
0142 struct exfat_chain {
0143 unsigned int dir;
0144 unsigned int size;
0145 unsigned char flags;
0146 };
0147
0148
0149 struct exfat_hint_femp {
0150
0151 int eidx;
0152
0153 int count;
0154
0155 struct exfat_chain cur;
0156 };
0157
0158
0159 struct exfat_hint {
0160 unsigned int clu;
0161 union {
0162 unsigned int off;
0163 int eidx;
0164 };
0165 };
0166
0167 struct exfat_entry_set_cache {
0168 struct super_block *sb;
0169 bool modified;
0170 unsigned int start_off;
0171 int num_bh;
0172 struct buffer_head *bh[DIR_CACHE_SIZE];
0173 unsigned int num_entries;
0174 };
0175
0176 struct exfat_dir_entry {
0177 struct exfat_chain dir;
0178 int entry;
0179 unsigned int type;
0180 unsigned int start_clu;
0181 unsigned char flags;
0182 unsigned short attr;
0183 loff_t size;
0184 unsigned int num_subdirs;
0185 struct timespec64 atime;
0186 struct timespec64 mtime;
0187 struct timespec64 crtime;
0188 struct exfat_dentry_namebuf namebuf;
0189 };
0190
0191
0192
0193
0194 struct exfat_mount_options {
0195 kuid_t fs_uid;
0196 kgid_t fs_gid;
0197 unsigned short fs_fmask;
0198 unsigned short fs_dmask;
0199
0200 unsigned short allow_utime;
0201
0202 char *iocharset;
0203
0204 enum exfat_error_mode errors;
0205 unsigned utf8:1,
0206 sys_tz:1,
0207 discard:1,
0208 keep_last_dots:1;
0209 int time_offset;
0210 };
0211
0212
0213
0214
0215 struct exfat_sb_info {
0216 unsigned long long num_sectors;
0217 unsigned int num_clusters;
0218 unsigned int cluster_size;
0219 unsigned int cluster_size_bits;
0220 unsigned int sect_per_clus;
0221 unsigned int sect_per_clus_bits;
0222 unsigned long long FAT1_start_sector;
0223 unsigned long long FAT2_start_sector;
0224 unsigned long long data_start_sector;
0225 unsigned int num_FAT_sectors;
0226 unsigned int root_dir;
0227 unsigned int dentries_per_clu;
0228 unsigned int vol_flags;
0229 unsigned int vol_flags_persistent;
0230 struct buffer_head *boot_bh;
0231
0232 unsigned int map_clu;
0233 unsigned int map_sectors;
0234 struct buffer_head **vol_amap;
0235
0236 unsigned short *vol_utbl;
0237
0238 unsigned int clu_srch_ptr;
0239 unsigned int used_clusters;
0240
0241 struct mutex s_lock;
0242 struct mutex bitmap_lock;
0243 struct exfat_mount_options options;
0244 struct nls_table *nls_io;
0245 struct ratelimit_state ratelimit;
0246
0247 spinlock_t inode_hash_lock;
0248 struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
0249
0250 struct rcu_head rcu;
0251 };
0252
0253 #define EXFAT_CACHE_VALID 0
0254
0255
0256
0257
0258 struct exfat_inode_info {
0259 struct exfat_chain dir;
0260 int entry;
0261 unsigned int type;
0262 unsigned short attr;
0263 unsigned int start_clu;
0264 unsigned char flags;
0265
0266
0267
0268
0269 unsigned int version;
0270
0271
0272 struct exfat_hint hint_bmap;
0273
0274 struct exfat_hint hint_stat;
0275
0276 struct exfat_hint_femp hint_femp;
0277
0278 spinlock_t cache_lru_lock;
0279 struct list_head cache_lru;
0280 int nr_caches;
0281
0282 unsigned int cache_valid_id;
0283
0284
0285
0286
0287
0288 loff_t i_size_ondisk;
0289
0290 loff_t i_size_aligned;
0291
0292 loff_t i_pos;
0293
0294 struct hlist_node i_hash_fat;
0295
0296 struct rw_semaphore truncate_lock;
0297 struct inode vfs_inode;
0298
0299 struct timespec64 i_crtime;
0300 };
0301
0302 static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
0303 {
0304 return sb->s_fs_info;
0305 }
0306
0307 static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
0308 {
0309 return container_of(inode, struct exfat_inode_info, vfs_inode);
0310 }
0311
0312
0313
0314
0315
0316
0317
0318
0319 static inline int exfat_mode_can_hold_ro(struct inode *inode)
0320 {
0321 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
0322
0323 if (S_ISDIR(inode->i_mode))
0324 return 0;
0325
0326 if ((~sbi->options.fs_fmask) & 0222)
0327 return 1;
0328 return 0;
0329 }
0330
0331
0332 static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
0333 unsigned short attr, mode_t mode)
0334 {
0335 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
0336 mode &= ~0222;
0337
0338 if (attr & ATTR_SUBDIR)
0339 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
0340
0341 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
0342 }
0343
0344
0345 static inline unsigned short exfat_make_attr(struct inode *inode)
0346 {
0347 unsigned short attr = EXFAT_I(inode)->attr;
0348
0349 if (S_ISDIR(inode->i_mode))
0350 attr |= ATTR_SUBDIR;
0351 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
0352 attr |= ATTR_READONLY;
0353 return attr;
0354 }
0355
0356 static inline void exfat_save_attr(struct inode *inode, unsigned short attr)
0357 {
0358 if (exfat_mode_can_hold_ro(inode))
0359 EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY);
0360 else
0361 EXFAT_I(inode)->attr = attr & ATTR_RWMASK;
0362 }
0363
0364 static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
0365 sector_t sec)
0366 {
0367 return ((sec - sbi->data_start_sector + 1) &
0368 ((1 << sbi->sect_per_clus_bits) - 1)) == 0;
0369 }
0370
0371 static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
0372 unsigned int clus)
0373 {
0374 return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
0375 sbi->data_start_sector;
0376 }
0377
0378 static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
0379 sector_t sec)
0380 {
0381 return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
0382 EXFAT_RESERVED_CLUSTERS;
0383 }
0384
0385 static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
0386 unsigned int clus)
0387 {
0388 return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
0389 }
0390
0391
0392 int exfat_set_volume_dirty(struct super_block *sb);
0393 int exfat_clear_volume_dirty(struct super_block *sb);
0394
0395
0396 #define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
0397
0398 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
0399 struct exfat_chain *p_chain, bool sync_bmap);
0400 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
0401 int exfat_ent_get(struct super_block *sb, unsigned int loc,
0402 unsigned int *content);
0403 int exfat_ent_set(struct super_block *sb, unsigned int loc,
0404 unsigned int content);
0405 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
0406 int entry, struct exfat_dentry *p_entry);
0407 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
0408 unsigned int len);
0409 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
0410 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
0411 unsigned int *ret_clu);
0412 int exfat_count_num_clusters(struct super_block *sb,
0413 struct exfat_chain *p_chain, unsigned int *ret_count);
0414
0415
0416 int exfat_load_bitmap(struct super_block *sb);
0417 void exfat_free_bitmap(struct exfat_sb_info *sbi);
0418 int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);
0419 void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);
0420 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
0421 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
0422 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
0423
0424
0425 extern const struct file_operations exfat_file_operations;
0426 int __exfat_truncate(struct inode *inode, loff_t new_size);
0427 void exfat_truncate(struct inode *inode, loff_t size);
0428 int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0429 struct iattr *attr);
0430 int exfat_getattr(struct user_namespace *mnt_userns, const struct path *path,
0431 struct kstat *stat, unsigned int request_mask,
0432 unsigned int query_flags);
0433 int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
0434 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
0435 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
0436 unsigned long arg);
0437
0438
0439 extern const struct dentry_operations exfat_dentry_ops;
0440 extern const struct dentry_operations exfat_utf8_dentry_ops;
0441
0442
0443 int exfat_cache_init(void);
0444 void exfat_cache_shutdown(void);
0445 void exfat_cache_inval_inode(struct inode *inode);
0446 int exfat_get_cluster(struct inode *inode, unsigned int cluster,
0447 unsigned int *fclus, unsigned int *dclus,
0448 unsigned int *last_dclus, int allow_eof);
0449
0450
0451 extern const struct inode_operations exfat_dir_inode_operations;
0452 extern const struct file_operations exfat_dir_operations;
0453 unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
0454 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
0455 int entry, unsigned int type, unsigned int start_clu,
0456 unsigned long long size);
0457 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
0458 int entry, int num_entries, struct exfat_uni_name *p_uniname);
0459 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
0460 int entry, int order, int num_entries);
0461 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
0462 int entry);
0463 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
0464 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
0465 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
0466 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
0467 int num_entries, unsigned int type, struct exfat_hint *hint_opt);
0468 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
0469 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
0470 struct exfat_chain *p_dir, int entry, struct buffer_head **bh);
0471 struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
0472 int num);
0473 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
0474 struct exfat_chain *p_dir, int entry, unsigned int type);
0475 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync);
0476 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
0477
0478
0479 extern const struct inode_operations exfat_file_inode_operations;
0480 void exfat_sync_inode(struct inode *inode);
0481 struct inode *exfat_build_inode(struct super_block *sb,
0482 struct exfat_dir_entry *info, loff_t i_pos);
0483 void exfat_hash_inode(struct inode *inode, loff_t i_pos);
0484 void exfat_unhash_inode(struct inode *inode);
0485 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
0486 int __exfat_write_inode(struct inode *inode, int sync);
0487 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
0488 void exfat_evict_inode(struct inode *inode);
0489 int exfat_block_truncate_page(struct inode *inode, loff_t from);
0490
0491
0492 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
0493 int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
0494 unsigned short *b, unsigned int len);
0495 int exfat_utf16_to_nls(struct super_block *sb,
0496 struct exfat_uni_name *uniname, unsigned char *p_cstring,
0497 int len);
0498 int exfat_nls_to_utf16(struct super_block *sb,
0499 const unsigned char *p_cstring, const int len,
0500 struct exfat_uni_name *uniname, int *p_lossy);
0501 int exfat_create_upcase_table(struct super_block *sb);
0502 void exfat_free_upcase_table(struct exfat_sb_info *sbi);
0503
0504
0505 void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
0506 __printf(3, 4) __cold;
0507 #define exfat_fs_error(sb, fmt, args...) \
0508 __exfat_fs_error(sb, 1, fmt, ## args)
0509 #define exfat_fs_error_ratelimit(sb, fmt, args...) \
0510 __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
0511 fmt, ## args)
0512
0513
0514 #define exfat_err(sb, fmt, ...) \
0515 pr_err("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
0516 #define exfat_warn(sb, fmt, ...) \
0517 pr_warn("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
0518 #define exfat_info(sb, fmt, ...) \
0519 pr_info("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
0520 #define exfat_debug(sb, fmt, ...) \
0521 pr_debug("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
0522
0523 void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
0524 u8 tz, __le16 time, __le16 date, u8 time_cs);
0525 void exfat_truncate_atime(struct timespec64 *ts);
0526 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
0527 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
0528 u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
0529 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
0530 void exfat_update_bh(struct buffer_head *bh, int sync);
0531 int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);
0532 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
0533 unsigned int size, unsigned char flags);
0534 void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
0535
0536 #endif