1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
10 #include <linux/ratelimit.h>
11 #include <linux/nls.h>
13 #define EXFAT_ROOT_INO 1
15 #define EXFAT_CLUSTERS_UNTRACKED (~0u)
20 enum exfat_error_mode {
21 EXFAT_ERRORS_CONT, /* ignore error and continue */
22 EXFAT_ERRORS_PANIC, /* panic on error */
23 EXFAT_ERRORS_RO, /* remount r/o on error */
27 * exfat nls lossy flag
30 NLS_NAME_NO_LOSSY, /* no lossy */
31 NLS_NAME_LOSSY, /* just detected incorrect filename(s) */
32 NLS_NAME_OVERLEN, /* the length is over than its limit */
35 #define EXFAT_HASH_BITS 8
36 #define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS)
41 #define ES_2_ENTRIES 2
42 #define ES_ALL_ENTRIES 0
44 #define DIR_DELETED 0xFFFF0321
47 #define TYPE_UNUSED 0x0000
48 #define TYPE_DELETED 0x0001
49 #define TYPE_INVALID 0x0002
50 #define TYPE_CRITICAL_PRI 0x0100
51 #define TYPE_BITMAP 0x0101
52 #define TYPE_UPCASE 0x0102
53 #define TYPE_VOLUME 0x0103
54 #define TYPE_DIR 0x0104
55 #define TYPE_FILE 0x011F
56 #define TYPE_CRITICAL_SEC 0x0200
57 #define TYPE_STREAM 0x0201
58 #define TYPE_EXTEND 0x0202
59 #define TYPE_ACL 0x0203
60 #define TYPE_BENIGN_PRI 0x0400
61 #define TYPE_GUID 0x0401
62 #define TYPE_PADDING 0x0402
63 #define TYPE_ACLTAB 0x0403
64 #define TYPE_BENIGN_SEC 0x0800
65 #define TYPE_ALL 0x0FFF
67 #define MAX_CHARSET_SIZE 6 /* max size of multi-byte character */
68 #define MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */
69 #define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
71 /* Enough size to hold 256 dentry (even 512 Byte sector) */
72 #define DIR_CACHE_SIZE (256*sizeof(struct exfat_dentry)/512+1)
74 #define EXFAT_HINT_NONE -1
75 #define EXFAT_MIN_SUBDIR 2
78 * helpers for cluster size to byte conversion.
80 #define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits)
81 #define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits)
82 #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \
83 (((b - 1) >> (sbi)->cluster_size_bits) + 1)
84 #define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1))
87 * helpers for block size to byte conversion.
89 #define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits)
90 #define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits)
91 #define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \
92 (((b - 1) >> (sb)->s_blocksize_bits) + 1)
93 #define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1))
96 * helpers for block size to dentry size conversion.
98 #define EXFAT_B_TO_DEN_IDX(b, sbi) \
99 ((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))
100 #define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS)
101 #define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS)
104 * helpers for fat entry.
106 #define FAT_ENT_SIZE (4)
107 #define FAT_ENT_SIZE_BITS (2)
108 #define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
109 (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
110 #define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \
111 ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
114 * helpers for bitmap.
116 #define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)
117 #define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)
118 #define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)
119 #define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)
120 #define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \
121 ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)
122 #define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))
123 #define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \
124 ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))
125 #define BITS_PER_BYTE_MASK 0x7
126 #define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1)
128 struct exfat_dentry_namebuf {
130 int lfnbuf_len; /* usually MAX_UNINAME_BUF_SIZE */
133 /* unicode name structure */
134 struct exfat_uni_name {
135 /* +3 for null and for converting */
136 unsigned short name[MAX_NAME_LENGTH + 3];
138 unsigned char name_len;
141 /* directory structure */
148 /* first empty entry hint information */
149 struct exfat_hint_femp {
150 /* entry index of a directory */
152 /* count of continuous empty entry */
154 /* the cluster that first empty slot exists in */
155 struct exfat_chain cur;
162 unsigned int off; /* cluster offset */
163 int eidx; /* entry index */
167 struct exfat_entry_set_cache {
168 struct super_block *sb;
170 unsigned int start_off;
172 struct buffer_head *bh[DIR_CACHE_SIZE];
173 unsigned int num_entries;
176 struct exfat_dir_entry {
177 struct exfat_chain dir;
180 unsigned int start_clu;
184 unsigned int num_subdirs;
185 struct timespec64 atime;
186 struct timespec64 mtime;
187 struct timespec64 crtime;
188 struct exfat_dentry_namebuf namebuf;
192 * exfat mount in-memory data
194 struct exfat_mount_options {
197 unsigned short fs_fmask;
198 unsigned short fs_dmask;
199 /* permission for setting the [am]time */
200 unsigned short allow_utime;
201 /* charset for filename input/display */
203 /* on error: continue, panic, remount-ro */
204 enum exfat_error_mode errors;
205 unsigned utf8:1, /* Use of UTF-8 character set */
206 sys_tz:1, /* Use local timezone */
207 discard:1, /* Issue discard requests on deletions */
208 keep_last_dots:1; /* Keep trailing periods in paths */
209 int time_offset; /* Offset of timestamps from UTC (in minutes) */
213 * EXFAT file system superblock in-memory data
215 struct exfat_sb_info {
216 unsigned long long num_sectors; /* num of sectors in volume */
217 unsigned int num_clusters; /* num of clusters in volume */
218 unsigned int cluster_size; /* cluster size in bytes */
219 unsigned int cluster_size_bits;
220 unsigned int sect_per_clus; /* cluster size in sectors */
221 unsigned int sect_per_clus_bits;
222 unsigned long long FAT1_start_sector; /* FAT1 start sector */
223 unsigned long long FAT2_start_sector; /* FAT2 start sector */
224 unsigned long long data_start_sector; /* data area start sector */
225 unsigned int num_FAT_sectors; /* num of FAT sectors */
226 unsigned int root_dir; /* root dir cluster */
227 unsigned int dentries_per_clu; /* num of dentries per cluster */
228 unsigned int vol_flags; /* volume flags */
229 unsigned int vol_flags_persistent; /* volume flags to retain */
230 struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
232 unsigned int map_clu; /* allocation bitmap start cluster */
233 unsigned int map_sectors; /* num of allocation bitmap sectors */
234 struct buffer_head **vol_amap; /* allocation bitmap */
236 unsigned short *vol_utbl; /* upcase table */
238 unsigned int clu_srch_ptr; /* cluster search pointer */
239 unsigned int used_clusters; /* number of used clusters */
241 struct mutex s_lock; /* superblock lock */
242 struct mutex bitmap_lock; /* bitmap lock */
243 struct exfat_mount_options options;
244 struct nls_table *nls_io; /* Charset used for input and display */
245 struct ratelimit_state ratelimit;
247 spinlock_t inode_hash_lock;
248 struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
253 #define EXFAT_CACHE_VALID 0
256 * EXFAT file system inode in-memory data
258 struct exfat_inode_info {
259 struct exfat_chain dir;
263 unsigned int start_clu;
266 * the copy of low 32bit of i_version to check
267 * the validation of hint_stat.
269 unsigned int version;
271 /* hint for cluster last accessed */
272 struct exfat_hint hint_bmap;
273 /* hint for entry index we try to lookup next time */
274 struct exfat_hint hint_stat;
275 /* hint for first empty entry */
276 struct exfat_hint_femp hint_femp;
278 spinlock_t cache_lru_lock;
279 struct list_head cache_lru;
281 /* for avoiding the race between alloc and free */
282 unsigned int cache_valid_id;
285 * NOTE: i_size_ondisk is 64bits, so must hold ->inode_lock to access.
286 * physically allocated size.
288 loff_t i_size_ondisk;
289 /* block-aligned i_size (used in cont_write_begin) */
290 loff_t i_size_aligned;
291 /* on-disk position of directory entry or 0 */
293 /* hash by i_location */
294 struct hlist_node i_hash_fat;
295 /* protect bmap against truncate */
296 struct rw_semaphore truncate_lock;
297 struct inode vfs_inode;
298 /* File creation time */
299 struct timespec64 i_crtime;
302 static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
304 return sb->s_fs_info;
307 static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
309 return container_of(inode, struct exfat_inode_info, vfs_inode);
313 * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to
314 * save ATTR_RO instead of ->i_mode.
316 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
317 * bit, it's just used as flag for app.
319 static inline int exfat_mode_can_hold_ro(struct inode *inode)
321 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
323 if (S_ISDIR(inode->i_mode))
326 if ((~sbi->options.fs_fmask) & 0222)
331 /* Convert attribute bits and a mask to the UNIX mode. */
332 static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
333 unsigned short attr, mode_t mode)
335 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
338 if (attr & ATTR_SUBDIR)
339 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
341 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
344 /* Return the FAT attribute byte for this inode */
345 static inline unsigned short exfat_make_attr(struct inode *inode)
347 unsigned short attr = EXFAT_I(inode)->attr;
349 if (S_ISDIR(inode->i_mode))
351 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
352 attr |= ATTR_READONLY;
356 static inline void exfat_save_attr(struct inode *inode, unsigned short attr)
358 if (exfat_mode_can_hold_ro(inode))
359 EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY);
361 EXFAT_I(inode)->attr = attr & ATTR_RWMASK;
364 static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
367 return ((sec - sbi->data_start_sector + 1) &
368 ((1 << sbi->sect_per_clus_bits) - 1)) == 0;
371 static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
374 return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
375 sbi->data_start_sector;
378 static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
381 return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
382 EXFAT_RESERVED_CLUSTERS;
385 static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
388 return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
392 int exfat_set_volume_dirty(struct super_block *sb);
393 int exfat_clear_volume_dirty(struct super_block *sb);
396 #define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
398 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
399 struct exfat_chain *p_chain, bool sync_bmap);
400 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
401 int exfat_ent_get(struct super_block *sb, unsigned int loc,
402 unsigned int *content);
403 int exfat_ent_set(struct super_block *sb, unsigned int loc,
404 unsigned int content);
405 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
406 int entry, struct exfat_dentry *p_entry);
407 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
409 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
410 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
411 unsigned int *ret_clu);
412 int exfat_count_num_clusters(struct super_block *sb,
413 struct exfat_chain *p_chain, unsigned int *ret_count);
416 int exfat_load_bitmap(struct super_block *sb);
417 void exfat_free_bitmap(struct exfat_sb_info *sbi);
418 int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);
419 void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);
420 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
421 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
422 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
425 extern const struct file_operations exfat_file_operations;
426 int __exfat_truncate(struct inode *inode, loff_t new_size);
427 void exfat_truncate(struct inode *inode, loff_t size);
428 int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
430 int exfat_getattr(struct user_namespace *mnt_userns, const struct path *path,
431 struct kstat *stat, unsigned int request_mask,
432 unsigned int query_flags);
433 int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
434 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
435 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
439 extern const struct dentry_operations exfat_dentry_ops;
440 extern const struct dentry_operations exfat_utf8_dentry_ops;
443 int exfat_cache_init(void);
444 void exfat_cache_shutdown(void);
445 void exfat_cache_inval_inode(struct inode *inode);
446 int exfat_get_cluster(struct inode *inode, unsigned int cluster,
447 unsigned int *fclus, unsigned int *dclus,
448 unsigned int *last_dclus, int allow_eof);
451 extern const struct inode_operations exfat_dir_inode_operations;
452 extern const struct file_operations exfat_dir_operations;
453 unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
454 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
455 int entry, unsigned int type, unsigned int start_clu,
456 unsigned long long size);
457 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
458 int entry, int num_entries, struct exfat_uni_name *p_uniname);
459 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
460 int entry, int order, int num_entries);
461 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
463 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
464 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
465 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
466 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
467 int num_entries, unsigned int type, struct exfat_hint *hint_opt);
468 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
469 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
470 struct exfat_chain *p_dir, int entry, struct buffer_head **bh);
471 struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
473 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
474 struct exfat_chain *p_dir, int entry, unsigned int type);
475 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync);
476 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
479 extern const struct inode_operations exfat_file_inode_operations;
480 void exfat_sync_inode(struct inode *inode);
481 struct inode *exfat_build_inode(struct super_block *sb,
482 struct exfat_dir_entry *info, loff_t i_pos);
483 void exfat_hash_inode(struct inode *inode, loff_t i_pos);
484 void exfat_unhash_inode(struct inode *inode);
485 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
486 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
487 void exfat_evict_inode(struct inode *inode);
488 int exfat_block_truncate_page(struct inode *inode, loff_t from);
491 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
492 int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
493 unsigned short *b, unsigned int len);
494 int exfat_utf16_to_nls(struct super_block *sb,
495 struct exfat_uni_name *uniname, unsigned char *p_cstring,
497 int exfat_nls_to_utf16(struct super_block *sb,
498 const unsigned char *p_cstring, const int len,
499 struct exfat_uni_name *uniname, int *p_lossy);
500 int exfat_create_upcase_table(struct super_block *sb);
501 void exfat_free_upcase_table(struct exfat_sb_info *sbi);
504 void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
505 __printf(3, 4) __cold;
506 #define exfat_fs_error(sb, fmt, args...) \
507 __exfat_fs_error(sb, 1, fmt, ## args)
508 #define exfat_fs_error_ratelimit(sb, fmt, args...) \
509 __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
511 void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...)
512 __printf(3, 4) __cold;
513 #define exfat_err(sb, fmt, ...) \
514 exfat_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
515 #define exfat_warn(sb, fmt, ...) \
516 exfat_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
517 #define exfat_info(sb, fmt, ...) \
518 exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__)
520 void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
521 u8 tz, __le16 time, __le16 date, u8 time_cs);
522 void exfat_truncate_atime(struct timespec64 *ts);
523 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
524 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
525 u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
526 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
527 void exfat_update_bh(struct buffer_head *bh, int sync);
528 int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);
529 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
530 unsigned int size, unsigned char flags);
531 void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
533 #endif /* !_EXFAT_FS_H */