1
2
3
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
20
21enum exfat_error_mode {
22 EXFAT_ERRORS_CONT,
23 EXFAT_ERRORS_PANIC,
24 EXFAT_ERRORS_RO,
25};
26
27
28
29
30enum {
31 NLS_NAME_NO_LOSSY,
32 NLS_NAME_LOSSY,
33 NLS_NAME_OVERLEN,
34};
35
36#define EXFAT_HASH_BITS 8
37#define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS)
38
39
40
41
42#define ES_2_ENTRIES 2
43#define ES_ALL_ENTRIES 0
44
45#define DIR_DELETED 0xFFFF0321
46
47
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
69#define MAX_NAME_LENGTH 255
70#define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
71
72
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
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
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
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
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
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;
132};
133
134
135struct exfat_uni_name {
136
137 unsigned short name[MAX_NAME_LENGTH + 3];
138 u16 name_hash;
139 unsigned char name_len;
140};
141
142
143struct exfat_chain {
144 unsigned int dir;
145 unsigned int size;
146 unsigned char flags;
147};
148
149
150struct exfat_hint_femp {
151
152 int eidx;
153
154 int count;
155
156 struct exfat_chain cur;
157};
158
159
160struct exfat_hint {
161 unsigned int clu;
162 union {
163 unsigned int off;
164 int eidx;
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
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
201 unsigned short allow_utime;
202
203 char *iocharset;
204
205 enum exfat_error_mode errors;
206 unsigned utf8:1,
207 discard:1;
208 int time_offset;
209};
210
211
212
213
214struct exfat_sb_info {
215 unsigned long long num_sectors;
216 unsigned int num_clusters;
217 unsigned int cluster_size;
218 unsigned int cluster_size_bits;
219 unsigned int sect_per_clus;
220 unsigned int sect_per_clus_bits;
221 unsigned long long FAT1_start_sector;
222 unsigned long long FAT2_start_sector;
223 unsigned long long data_start_sector;
224 unsigned int num_FAT_sectors;
225 unsigned int root_dir;
226 unsigned int dentries_per_clu;
227 unsigned int vol_flags;
228 unsigned int vol_flags_persistent;
229 struct buffer_head *boot_bh;
230
231 unsigned int map_clu;
232 unsigned int map_sectors;
233 struct buffer_head **vol_amap;
234
235 unsigned short *vol_utbl;
236
237 unsigned int clu_srch_ptr;
238 unsigned int used_clusters;
239
240 struct mutex s_lock;
241 struct mutex bitmap_lock;
242 struct exfat_mount_options options;
243 struct nls_table *nls_io;
244 struct ratelimit_state ratelimit;
245
246 spinlock_t inode_hash_lock;
247 struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
248
249 struct rcu_head rcu;
250};
251
252#define EXFAT_CACHE_VALID 0
253
254
255
256
257struct exfat_inode_info {
258 struct exfat_chain dir;
259 int entry;
260 unsigned int type;
261 unsigned short attr;
262 unsigned int start_clu;
263 unsigned char flags;
264
265
266
267
268 unsigned int version;
269
270
271 struct exfat_hint hint_bmap;
272
273 struct exfat_hint hint_stat;
274
275 struct exfat_hint_femp hint_femp;
276
277 spinlock_t cache_lru_lock;
278 struct list_head cache_lru;
279 int nr_caches;
280
281 unsigned int cache_valid_id;
282
283
284
285
286
287 loff_t i_size_ondisk;
288
289 loff_t i_size_aligned;
290
291 loff_t i_pos;
292
293 struct hlist_node i_hash_fat;
294
295 struct rw_semaphore truncate_lock;
296 struct inode vfs_inode;
297
298 struct timespec64 i_crtime;
299};
300
301static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
302{
303 return sb->s_fs_info;
304}
305
306static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
307{
308 return container_of(inode, struct exfat_inode_info, vfs_inode);
309}
310
311
312
313
314
315
316
317
318static inline int exfat_mode_can_hold_ro(struct inode *inode)
319{
320 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
321
322 if (S_ISDIR(inode->i_mode))
323 return 0;
324
325 if ((~sbi->options.fs_fmask) & 0222)
326 return 1;
327 return 0;
328}
329
330
331static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
332 unsigned short attr, mode_t mode)
333{
334 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
335 mode &= ~0222;
336
337 if (attr & ATTR_SUBDIR)
338 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
339
340 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
341}
342
343
344static inline unsigned short exfat_make_attr(struct inode *inode)
345{
346 unsigned short attr = EXFAT_I(inode)->attr;
347
348 if (S_ISDIR(inode->i_mode))
349 attr |= ATTR_SUBDIR;
350 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
351 attr |= ATTR_READONLY;
352 return attr;
353}
354
355static inline void exfat_save_attr(struct inode *inode, unsigned short attr)
356{
357 if (exfat_mode_can_hold_ro(inode))
358 EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY);
359 else
360 EXFAT_I(inode)->attr = attr & ATTR_RWMASK;
361}
362
363static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
364 sector_t sec)
365{
366 return ((sec - sbi->data_start_sector + 1) &
367 ((1 << sbi->sect_per_clus_bits) - 1)) == 0;
368}
369
370static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
371 unsigned int clus)
372{
373 return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
374 sbi->data_start_sector;
375}
376
377static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
378 sector_t sec)
379{
380 return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
381 EXFAT_RESERVED_CLUSTERS;
382}
383
384
385int exfat_set_volume_dirty(struct super_block *sb);
386int exfat_clear_volume_dirty(struct super_block *sb);
387
388
389#define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
390
391int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
392 struct exfat_chain *p_chain, bool sync_bmap);
393int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
394int exfat_ent_get(struct super_block *sb, unsigned int loc,
395 unsigned int *content);
396int exfat_ent_set(struct super_block *sb, unsigned int loc,
397 unsigned int content);
398int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
399 int entry, struct exfat_dentry *p_entry);
400int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
401 unsigned int len);
402int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
403int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
404 unsigned int *ret_clu);
405int exfat_count_num_clusters(struct super_block *sb,
406 struct exfat_chain *p_chain, unsigned int *ret_count);
407
408
409int exfat_load_bitmap(struct super_block *sb);
410void exfat_free_bitmap(struct exfat_sb_info *sbi);
411int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);
412void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);
413unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
414int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
415int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
416
417
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 user_namespace *mnt_userns, struct dentry *dentry,
422 struct iattr *attr);
423int exfat_getattr(struct user_namespace *mnt_userns, const struct path *path,
424 struct kstat *stat, unsigned int request_mask,
425 unsigned int query_flags);
426int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
427long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
428long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
429 unsigned long arg);
430
431
432extern const struct dentry_operations exfat_dentry_ops;
433extern const struct dentry_operations exfat_utf8_dentry_ops;
434
435
436int exfat_cache_init(void);
437void exfat_cache_shutdown(void);
438void exfat_cache_inval_inode(struct inode *inode);
439int exfat_get_cluster(struct inode *inode, unsigned int cluster,
440 unsigned int *fclus, unsigned int *dclus,
441 unsigned int *last_dclus, int allow_eof);
442
443
444extern const struct inode_operations exfat_dir_inode_operations;
445extern const struct file_operations exfat_dir_operations;
446unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
447int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
448 int entry, unsigned int type, unsigned int start_clu,
449 unsigned long long size);
450int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
451 int entry, int num_entries, struct exfat_uni_name *p_uniname);
452int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
453 int entry, int order, int num_entries);
454int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
455 int entry);
456void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
457int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
458int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
459 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
460 int num_entries, unsigned int type, struct exfat_hint *hint_opt);
461int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
462int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
463 int entry, sector_t *sector, int *offset);
464struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
465 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
466 sector_t *sector);
467struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
468 int num);
469struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
470 struct exfat_chain *p_dir, int entry, unsigned int type);
471int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync);
472int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
473
474
475extern const struct inode_operations exfat_file_inode_operations;
476void exfat_sync_inode(struct inode *inode);
477struct inode *exfat_build_inode(struct super_block *sb,
478 struct exfat_dir_entry *info, loff_t i_pos);
479void exfat_hash_inode(struct inode *inode, loff_t i_pos);
480void exfat_unhash_inode(struct inode *inode);
481struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
482int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
483void exfat_evict_inode(struct inode *inode);
484int exfat_block_truncate_page(struct inode *inode, loff_t from);
485
486
487unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
488int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
489 unsigned short *b, unsigned int len);
490int exfat_utf16_to_nls(struct super_block *sb,
491 struct exfat_uni_name *uniname, unsigned char *p_cstring,
492 int len);
493int exfat_nls_to_utf16(struct super_block *sb,
494 const unsigned char *p_cstring, const int len,
495 struct exfat_uni_name *uniname, int *p_lossy);
496int exfat_create_upcase_table(struct super_block *sb);
497void exfat_free_upcase_table(struct exfat_sb_info *sbi);
498
499
500void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
501 __printf(3, 4) __cold;
502#define exfat_fs_error(sb, fmt, args...) \
503 __exfat_fs_error(sb, 1, fmt, ## args)
504#define exfat_fs_error_ratelimit(sb, fmt, args...) \
505 __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
506 fmt, ## args)
507void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...)
508 __printf(3, 4) __cold;
509#define exfat_err(sb, fmt, ...) \
510 exfat_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
511#define exfat_warn(sb, fmt, ...) \
512 exfat_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
513#define exfat_info(sb, fmt, ...) \
514 exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__)
515
516void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
517 u8 tz, __le16 time, __le16 date, u8 time_cs);
518void exfat_truncate_atime(struct timespec64 *ts);
519void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
520 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
521u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
522u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
523void exfat_update_bh(struct buffer_head *bh, int sync);
524int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);
525void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
526 unsigned int size, unsigned char flags);
527void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
528
529#endif
530