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