xref: /linux/fs/exfat/exfat_fs.h (revision 00b04f4495c354cad82f96d41947aa70f71e2cc4)
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 <linux/backing-dev.h>
14 #include <uapi/linux/exfat.h>
15 #include <linux/buffer_head.h>
16 
17 #define EXFAT_ROOT_INO		1
18 
19 /*
20  * exfat error flags
21  */
22 enum 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  */
31 enum {
32 	NLS_NAME_NO_LOSSY =	0,	/* no lossy */
33 	NLS_NAME_LOSSY =	1 << 0,	/* just detected incorrect filename(s) */
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 ES_IDX_FILE		0
46 #define ES_IDX_STREAM		1
47 #define ES_IDX_FIRST_FILENAME	2
48 #define EXFAT_FILENAME_ENTRY_NUM(name_len) \
49 	DIV_ROUND_UP(name_len, EXFAT_FILE_NAME_LEN)
50 #define ES_IDX_LAST_FILENAME(name_len)	\
51 	(ES_IDX_FIRST_FILENAME + EXFAT_FILENAME_ENTRY_NUM(name_len) - 1)
52 
53 #define DIR_DELETED		0xFFFFFFF7
54 
55 /* type values */
56 #define TYPE_UNUSED		0x0000
57 #define TYPE_DELETED		0x0001
58 #define TYPE_INVALID		0x0002
59 #define TYPE_CRITICAL_PRI	0x0100
60 #define TYPE_BITMAP		0x0101
61 #define TYPE_UPCASE		0x0102
62 #define TYPE_VOLUME		0x0103
63 #define TYPE_DIR		0x0104
64 #define TYPE_FILE		0x011F
65 #define TYPE_CRITICAL_SEC	0x0200
66 #define TYPE_STREAM		0x0201
67 #define TYPE_EXTEND		0x0202
68 #define TYPE_ACL		0x0203
69 #define TYPE_BENIGN_PRI		0x0400
70 #define TYPE_GUID		0x0401
71 #define TYPE_PADDING		0x0402
72 #define TYPE_ACLTAB		0x0403
73 #define TYPE_BENIGN_SEC		0x0800
74 #define TYPE_VENDOR_EXT		0x0801
75 #define TYPE_VENDOR_ALLOC	0x0802
76 
77 #define MAX_CHARSET_SIZE	6 /* max size of multi-byte character */
78 #define MAX_NAME_LENGTH		255 /* max len of file name excluding NULL */
79 #define MAX_VFSNAME_BUF_SIZE	((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
80 
81 #define EXFAT_HINT_NONE		-1
82 #define EXFAT_MIN_SUBDIR	2
83 
84 #define EXFAT_BLK_RA_SIZE(sb)		\
85 	(min_t(blkcnt_t, (sb)->s_bdi->ra_pages, (sb)->s_bdi->io_pages) \
86 	<< (PAGE_SHIFT - (sb)->s_blocksize_bits))
87 
88 /*
89  * helpers for fat entry.
90  */
91 #define FAT_ENT_SIZE (4)
92 #define FAT_ENT_SIZE_BITS (2)
93 #define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
94 	(((u64)(loc) << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
95 #define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc)	\
96 	(((loc) << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
97 
98 /*
99  * helpers for bitmap.
100  */
101 #define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)
102 #define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)
103 #define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)
104 #define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)
105 #define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \
106 	((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)
107 #define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))
108 #define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \
109 	((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))
110 #define IGNORED_BITS_REMAINED(clu, clu_base) ((1UL << ((clu) - (clu_base))) - 1)
111 
112 #define ES_ENTRY_NUM(name_len)	(ES_IDX_LAST_FILENAME(name_len) + 1)
113 /* 19 entries = 1 file entry + 1 stream entry + 17 filename entries */
114 #define ES_MAX_ENTRY_NUM	ES_ENTRY_NUM(MAX_NAME_LENGTH)
115 
116 /*
117  * 19 entries x 32 bytes/entry = 608 bytes.
118  * The 608 bytes are in 3 sectors at most (even 512 Byte sector).
119  */
120 #define DIR_CACHE_SIZE		\
121 	(DIV_ROUND_UP(ES_MAX_ENTRY_NUM << DENTRY_SIZE_BITS, SECTOR_SIZE) + 1)
122 
123 /* Superblock flags */
124 #define EXFAT_FLAGS_SHUTDOWN	1
125 
126 struct exfat_dentry_namebuf {
127 	char *lfn;
128 	int lfnbuf_len; /* usually MAX_UNINAME_BUF_SIZE */
129 };
130 
131 /* unicode name structure */
132 struct exfat_uni_name {
133 	/* +3 for null and for converting */
134 	unsigned short name[MAX_NAME_LENGTH + 3];
135 	u16 name_hash;
136 	unsigned char name_len;
137 };
138 
139 /* directory structure */
140 struct exfat_chain {
141 	unsigned int dir;
142 	unsigned int size;
143 	unsigned char flags;
144 };
145 
146 /* first empty entry hint information */
147 struct exfat_hint_femp {
148 	/* entry index of a directory */
149 	int eidx;
150 	/* count of continuous empty entry */
151 	int count;
152 	/* the cluster that first empty slot exists in */
153 	struct exfat_chain cur;
154 };
155 
156 /* hint structure */
157 struct exfat_hint {
158 	unsigned int clu;
159 	union {
160 		unsigned int off; /* cluster offset */
161 		int eidx; /* entry index */
162 	};
163 };
164 
165 struct exfat_entry_set_cache {
166 	struct super_block *sb;
167 	unsigned int start_off;
168 	int num_bh;
169 	struct buffer_head *__bh[DIR_CACHE_SIZE];
170 	struct buffer_head **bh;
171 	unsigned int num_entries;
172 	bool modified;
173 };
174 
175 #define IS_DYNAMIC_ES(es)	((es)->__bh != (es)->bh)
176 
177 struct exfat_dir_entry {
178 	/* the cluster where file dentry is located */
179 	struct exfat_chain dir;
180 	/* the index of file dentry in ->dir */
181 	int entry;
182 	unsigned int type;
183 	unsigned int start_clu;
184 	unsigned char flags;
185 	unsigned short attr;
186 	loff_t size;
187 	loff_t valid_size;
188 	unsigned int num_subdirs;
189 	struct timespec64 atime;
190 	struct timespec64 mtime;
191 	struct timespec64 crtime;
192 	struct exfat_dentry_namebuf namebuf;
193 };
194 
195 /*
196  * exfat mount in-memory data
197  */
198 struct exfat_mount_options {
199 	kuid_t fs_uid;
200 	kgid_t fs_gid;
201 	unsigned short fs_fmask;
202 	unsigned short fs_dmask;
203 	/* permission for setting the [am]time */
204 	unsigned short allow_utime;
205 	/* charset for filename input/display */
206 	char *iocharset;
207 	/* on error: continue, panic, remount-ro */
208 	enum exfat_error_mode errors;
209 	unsigned utf8:1, /* Use of UTF-8 character set */
210 		 sys_tz:1, /* Use local timezone */
211 		 discard:1, /* Issue discard requests on deletions */
212 		 keep_last_dots:1; /* Keep trailing periods in paths */
213 	int time_offset; /* Offset of timestamps from UTC (in minutes) */
214 	/* Support creating zero-size directory, default: false */
215 	bool zero_size_dir;
216 };
217 
218 /*
219  * EXFAT file system superblock in-memory data
220  */
221 struct exfat_sb_info {
222 	unsigned long long num_sectors; /* num of sectors in volume */
223 	unsigned int num_clusters; /* num of clusters in volume */
224 	unsigned int cluster_size; /* cluster size in bytes */
225 	unsigned int cluster_size_bits;
226 	unsigned int sect_per_clus; /* cluster size in sectors */
227 	unsigned int sect_per_clus_bits;
228 	unsigned long long FAT1_start_sector; /* FAT1 start sector */
229 	unsigned long long FAT2_start_sector; /* FAT2 start sector */
230 	unsigned long long data_start_sector; /* data area start sector */
231 	unsigned long long data_start_bytes;
232 	unsigned int num_FAT_sectors; /* num of FAT sectors */
233 	unsigned int root_dir; /* root dir cluster */
234 	unsigned int dentries_per_clu; /* num of dentries per cluster */
235 	unsigned int vol_flags; /* volume flags */
236 	unsigned int vol_flags_persistent; /* volume flags to retain */
237 	struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
238 
239 	unsigned int map_clu; /* allocation bitmap start cluster */
240 	unsigned int map_sectors; /* num of allocation bitmap sectors */
241 	struct buffer_head **vol_amap; /* allocation bitmap */
242 
243 	unsigned short *vol_utbl; /* upcase table */
244 
245 	unsigned int clu_srch_ptr; /* cluster search pointer */
246 	unsigned int used_clusters; /* number of used clusters */
247 
248 	unsigned long s_exfat_flags; /* Exfat superblock flags */
249 
250 	struct mutex s_lock; /* superblock lock */
251 	struct mutex bitmap_lock; /* bitmap lock */
252 	struct exfat_mount_options options;
253 	struct nls_table *nls_io; /* Charset used for input and display */
254 	struct ratelimit_state ratelimit;
255 
256 	spinlock_t inode_hash_lock;
257 	struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
258 	struct rcu_head rcu;
259 };
260 
261 #define EXFAT_CACHE_VALID	0
262 
263 /*
264  * EXFAT file system inode in-memory data
265  */
266 struct exfat_inode_info {
267 	/* the cluster where file dentry is located */
268 	struct exfat_chain dir;
269 	/* the index of file dentry in ->dir */
270 	int entry;
271 	unsigned int type;
272 	unsigned short attr;
273 	unsigned int start_clu;
274 	unsigned char flags;
275 	/*
276 	 * the copy of low 32bit of i_version to check
277 	 * the validation of hint_stat.
278 	 */
279 	unsigned int version;
280 
281 	/* hint for cluster last accessed */
282 	struct exfat_hint hint_bmap;
283 	/* hint for entry index we try to lookup next time */
284 	struct exfat_hint hint_stat;
285 	/* hint for first empty entry */
286 	struct exfat_hint_femp hint_femp;
287 
288 	spinlock_t cache_lru_lock;
289 	struct list_head cache_lru;
290 	int nr_caches;
291 	/* for avoiding the race between alloc and free */
292 	unsigned int cache_valid_id;
293 
294 	/* on-disk position of directory entry or 0 */
295 	loff_t i_pos;
296 	loff_t valid_size;
297 	/* block-aligned size zeroed in the page cache (>= valid_size) */
298 	loff_t zeroed_size;
299 	/* hash by i_location */
300 	struct hlist_node i_hash_fat;
301 	struct inode vfs_inode;
302 	/* File creation time */
303 	struct timespec64 i_crtime;
304 };
305 
EXFAT_SB(struct super_block * sb)306 static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
307 {
308 	return sb->s_fs_info;
309 }
310 
EXFAT_I(struct inode * inode)311 static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
312 {
313 	return container_of(inode, struct exfat_inode_info, vfs_inode);
314 }
315 
exfat_forced_shutdown(struct super_block * sb)316 static inline int exfat_forced_shutdown(struct super_block *sb)
317 {
318 	return test_bit(EXFAT_FLAGS_SHUTDOWN, &EXFAT_SB(sb)->s_exfat_flags);
319 }
320 
321 /*
322  * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to
323  * save ATTR_RO instead of ->i_mode.
324  *
325  * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
326  * bit, it's just used as flag for app.
327  */
exfat_mode_can_hold_ro(struct inode * inode)328 static inline int exfat_mode_can_hold_ro(struct inode *inode)
329 {
330 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
331 
332 	if (S_ISDIR(inode->i_mode))
333 		return 0;
334 
335 	if ((~sbi->options.fs_fmask) & 0222)
336 		return 1;
337 	return 0;
338 }
339 
340 /* Convert attribute bits and a mask to the UNIX mode. */
exfat_make_mode(struct exfat_sb_info * sbi,unsigned short attr,mode_t mode)341 static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
342 		unsigned short attr, mode_t mode)
343 {
344 	if ((attr & EXFAT_ATTR_READONLY) && !(attr & EXFAT_ATTR_SUBDIR))
345 		mode &= ~0222;
346 
347 	if (attr & EXFAT_ATTR_SUBDIR)
348 		return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
349 
350 	return (mode & ~sbi->options.fs_fmask) | S_IFREG;
351 }
352 
353 /* Return the FAT attribute byte for this inode */
exfat_make_attr(struct inode * inode)354 static inline unsigned short exfat_make_attr(struct inode *inode)
355 {
356 	unsigned short attr = EXFAT_I(inode)->attr;
357 
358 	if (S_ISDIR(inode->i_mode))
359 		attr |= EXFAT_ATTR_SUBDIR;
360 	if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
361 		attr |= EXFAT_ATTR_READONLY;
362 	return attr;
363 }
364 
exfat_save_attr(struct inode * inode,unsigned short attr)365 static inline void exfat_save_attr(struct inode *inode, unsigned short attr)
366 {
367 	if (exfat_mode_can_hold_ro(inode))
368 		EXFAT_I(inode)->attr = attr & (EXFAT_ATTR_RWMASK | EXFAT_ATTR_READONLY);
369 	else
370 		EXFAT_I(inode)->attr = attr & EXFAT_ATTR_RWMASK;
371 }
372 
exfat_is_last_sector_in_cluster(struct exfat_sb_info * sbi,sector_t sec)373 static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
374 		sector_t sec)
375 {
376 	return ((sec - sbi->data_start_sector + 1) &
377 		((1 << sbi->sect_per_clus_bits) - 1)) == 0;
378 }
379 
exfat_cluster_to_sector(struct exfat_sb_info * sbi,unsigned int clus)380 static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
381 		unsigned int clus)
382 {
383 	return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
384 		sbi->data_start_sector;
385 }
386 
exfat_sector_to_cluster(struct exfat_sb_info * sbi,sector_t sec)387 static inline unsigned int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
388 		sector_t sec)
389 {
390 	return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
391 		EXFAT_RESERVED_CLUSTERS;
392 }
393 
is_valid_cluster(struct exfat_sb_info * sbi,unsigned int clus)394 static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
395 		unsigned int clus)
396 {
397 	return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
398 }
399 
exfat_ondisk_size(const struct inode * inode)400 static inline loff_t exfat_ondisk_size(const struct inode *inode)
401 {
402 	return ((loff_t)inode->i_blocks) << 9;
403 }
404 
exfat_cluster_to_phys_bytes(struct exfat_sb_info * sbi,unsigned int clus)405 static inline loff_t exfat_cluster_to_phys_bytes(struct exfat_sb_info *sbi,
406 		unsigned int clus)
407 {
408 	return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bits) +
409 		sbi->data_start_bytes;
410 }
411 
412 /*
413  * helpers for cluster size to byte conversion.
414  */
exfat_cluster_to_bytes(struct exfat_sb_info * sbi,u32 nr_clusters)415 static inline loff_t exfat_cluster_to_bytes(struct exfat_sb_info *sbi,
416 		u32 nr_clusters)
417 {
418 	return (loff_t)nr_clusters << sbi->cluster_size_bits;
419 }
420 
exfat_cluster_to_sectors(struct exfat_sb_info * sbi,u32 nr_clusters)421 static inline blkcnt_t exfat_cluster_to_sectors(struct exfat_sb_info *sbi,
422 		u32 nr_clusters)
423 {
424 	return (blkcnt_t)nr_clusters << (sbi->cluster_size_bits - 9);
425 }
426 
exfat_bytes_to_cluster(struct exfat_sb_info * sbi,loff_t size)427 static inline u32 exfat_bytes_to_cluster(struct exfat_sb_info *sbi, loff_t size)
428 {
429 	return (u32)(size >> sbi->cluster_size_bits);
430 }
431 
exfat_bytes_to_cluster_round_up(struct exfat_sb_info * sbi,loff_t size)432 static inline u32 exfat_bytes_to_cluster_round_up(struct exfat_sb_info *sbi,
433 		loff_t size)
434 {
435 	if (size <= 0)
436 		return 0;
437 	return (u32)((size - 1) >> sbi->cluster_size_bits) + 1;
438 }
439 
exfat_cluster_offset(struct exfat_sb_info * sbi,loff_t off)440 static inline u32 exfat_cluster_offset(struct exfat_sb_info *sbi, loff_t off)
441 {
442 	return off & (sbi->cluster_size - 1);
443 }
444 
445 /*
446  * helpers for block size to byte conversion.
447  */
exfat_block_to_bytes(struct super_block * sb,sector_t block)448 static inline loff_t exfat_block_to_bytes(struct super_block *sb,
449 		sector_t block)
450 {
451 	return (loff_t)block << sb->s_blocksize_bits;
452 }
453 
exfat_bytes_to_block(struct super_block * sb,loff_t size)454 static inline sector_t exfat_bytes_to_block(struct super_block *sb, loff_t size)
455 {
456 	return (sector_t)(size >> sb->s_blocksize_bits);
457 }
458 
exfat_bytes_to_block_round_up(struct super_block * sb,loff_t size)459 static inline sector_t exfat_bytes_to_block_round_up(struct super_block *sb,
460 		loff_t size)
461 {
462 	if (size <= 0)
463 		return 0;
464 	return (sector_t)(((size - 1) >> sb->s_blocksize_bits) + 1);
465 }
466 
exfat_block_offset(struct super_block * sb,loff_t off)467 static inline u32 exfat_block_offset(struct super_block *sb, loff_t off)
468 {
469 	return (u32)(off & (sb->s_blocksize - 1));
470 }
471 
472 /*
473  * helpers for block size to dentry size conversion.
474  */
exfat_bytes_to_dentries(loff_t b)475 static inline u32 exfat_bytes_to_dentries(loff_t b)
476 {
477 	return (u32)(b >> DENTRY_SIZE_BITS);
478 }
479 
exfat_dentries_to_bytes(u32 dentry)480 static inline u32 exfat_dentries_to_bytes(u32 dentry)
481 {
482 	return dentry << DENTRY_SIZE_BITS;
483 }
484 
485 /*
486  * helpers for cluster size to dentry size conversion.
487  */
exfat_cluster_to_dentries(struct exfat_sb_info * sbi,u32 nr_clusters)488 static inline u64 exfat_cluster_to_dentries(struct exfat_sb_info *sbi,
489 		u32 nr_clusters)
490 {
491 	return (u64)nr_clusters << (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
492 }
493 
exfat_dentries_to_cluster(struct exfat_sb_info * sbi,u32 dentry)494 static inline u32 exfat_dentries_to_cluster(struct exfat_sb_info *sbi,
495 		u32 dentry)
496 {
497 	return dentry >> (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
498 }
499 
500 /* super.c */
501 int exfat_set_volume_dirty(struct super_block *sb);
502 int exfat_clear_volume_dirty(struct super_block *sb);
503 
504 /* fatent.c */
505 #define exfat_get_next_cluster(sb, pclu) \
506 	exfat_cluster_walk(sb, (pclu), 1, ALLOC_FAT_CHAIN)
507 
508 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
509 		struct exfat_chain *p_chain, bool sync_bmap, bool contig);
510 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
511 int exfat_ent_get(struct super_block *sb, unsigned int loc,
512 		unsigned int *content, struct buffer_head **last);
513 int exfat_ent_set(struct super_block *sb, unsigned int loc,
514 		unsigned int content);
515 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
516 		unsigned int len);
517 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
518 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
519 		unsigned int *ret_clu);
520 int exfat_count_num_clusters(struct super_block *sb,
521 		struct exfat_chain *p_chain, unsigned int *ret_count);
522 int exfat_blk_readahead(struct super_block *sb, sector_t sec,
523 		sector_t *ra, blkcnt_t *ra_cnt, sector_t end);
524 
525 static inline int
exfat_cluster_walk(struct super_block * sb,unsigned int * clu,unsigned int step,int flags)526 exfat_cluster_walk(struct super_block *sb, unsigned int *clu,
527 		unsigned int step, int flags)
528 {
529 	struct buffer_head *bh = NULL;
530 
531 	if (flags == ALLOC_NO_FAT_CHAIN) {
532 		(*clu) += step;
533 		return 0;
534 	}
535 
536 	while (step--) {
537 		if (exfat_ent_get(sb, *clu, clu, &bh))
538 			return -EIO;
539 	}
540 	brelse(bh);
541 
542 	return 0;
543 }
544 
545 /* balloc.c */
546 int exfat_load_bitmap(struct super_block *sb);
547 void exfat_free_bitmap(struct exfat_sb_info *sbi);
548 int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync);
549 int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync);
550 bool exfat_test_bitmap(struct super_block *sb, unsigned int clu);
551 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
552 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
553 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
554 
555 /* file.c */
556 extern const struct file_operations exfat_file_operations;
557 int __exfat_truncate(struct inode *inode);
558 int exfat_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
559 		  struct iattr *attr);
560 int exfat_getattr(const struct mnt_idmap *idmap, const struct path *path,
561 		  struct kstat *stat, unsigned int request_mask,
562 		  unsigned int query_flags);
563 struct file_kattr;
564 int exfat_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
565 int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
566 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
567 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
568 				unsigned long arg);
569 int exfat_force_shutdown(struct super_block *sb, u32 flags);
570 
571 /* namei.c */
572 extern const struct dentry_operations exfat_dentry_ops;
573 extern const struct dentry_operations exfat_utf8_dentry_ops;
574 int exfat_find_empty_entry(struct inode *inode,
575 		struct exfat_chain *p_dir, int num_entries,
576 			   struct exfat_entry_set_cache *es);
577 
578 /* cache.c */
579 int exfat_cache_init(void);
580 void exfat_cache_shutdown(void);
581 void exfat_cache_inval_inode(struct inode *inode);
582 int exfat_get_cluster(struct inode *inode, unsigned int cluster,
583 		unsigned int *dclus, unsigned int *count, unsigned int *last_dclus);
584 
585 /* dir.c */
586 extern const struct inode_operations exfat_dir_inode_operations;
587 extern const struct file_operations exfat_dir_operations;
588 unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
589 void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
590 		unsigned int type, unsigned int start_clu,
591 		unsigned long long size, struct timespec64 *ts);
592 void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
593 		struct exfat_uni_name *p_uniname,
594 		struct exfat_entry_set_cache *old_es, int num_extra);
595 void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
596 		int order, bool free_benign);
597 void exfat_update_dir_chksum(struct exfat_entry_set_cache *es);
598 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
599 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
600 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
601 		struct exfat_hint *hint_opt);
602 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
603 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
604 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh);
605 struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
606 		int num);
607 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
608 		struct super_block *sb, struct exfat_chain *p_dir, int entry,
609 		unsigned int num_entries);
610 #define exfat_get_dentry_set_by_ei(es, sb, ei)		\
611 	exfat_get_dentry_set(es, sb, &(ei)->dir, (ei)->entry, ES_ALL_ENTRIES)
612 int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
613 		struct super_block *sb, struct exfat_chain *p_dir, int entry,
614 		unsigned int num_entries);
615 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync);
616 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
617 int exfat_read_volume_label(struct super_block *sb,
618 			    struct exfat_uni_name *label_out);
619 int exfat_write_volume_label(struct super_block *sb,
620 			     struct exfat_uni_name *label);
621 
exfat_chain_advance(struct super_block * sb,struct exfat_chain * chain,unsigned int step)622 static inline int exfat_chain_advance(struct super_block *sb,
623 		struct exfat_chain *chain, unsigned int step)
624 {
625 	unsigned int clu = chain->dir;
626 
627 	if (unlikely(chain->size < step))
628 		return -EIO;
629 
630 	if (exfat_cluster_walk(sb, &clu, step, chain->flags))
631 		return -EIO;
632 
633 	chain->size -= step;
634 
635 	if (chain->size == 0 && chain->flags == ALLOC_NO_FAT_CHAIN)
636 		chain->dir = EXFAT_EOF_CLUSTER;
637 	else
638 		chain->dir = clu;
639 
640 	return 0;
641 }
642 
643 /* inode.c */
644 extern const struct inode_operations exfat_file_inode_operations;
645 void exfat_sync_inode(struct inode *inode);
646 struct inode *exfat_build_inode(struct super_block *sb,
647 		struct exfat_dir_entry *info, loff_t i_pos);
648 void exfat_hash_inode(struct inode *inode, loff_t i_pos);
649 void exfat_unhash_inode(struct inode *inode);
650 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
651 int __exfat_write_inode(struct inode *inode, int sync);
652 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
653 void exfat_evict_inode(struct inode *inode);
654 int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
655 		unsigned int *clu, unsigned int *count, int create,
656 		bool *balloc);
657 
658 /* exfat/nls.c */
659 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
660 int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
661 		unsigned short *b, unsigned int len);
662 int exfat_utf16_to_nls(struct super_block *sb,
663 		struct exfat_uni_name *uniname, unsigned char *p_cstring,
664 		int len);
665 int exfat_nls_to_utf16(struct super_block *sb,
666 		const unsigned char *p_cstring, const int len,
667 		struct exfat_uni_name *uniname, int *p_lossy);
668 int exfat_create_upcase_table(struct super_block *sb);
669 void exfat_free_upcase_table(struct exfat_sb_info *sbi);
670 
671 /* exfat/misc.c */
672 void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
673 		__printf(3, 4) __cold;
674 #define exfat_fs_error(sb, fmt, args...)          \
675 		__exfat_fs_error(sb, 1, fmt, ## args)
676 #define exfat_fs_error_ratelimit(sb, fmt, args...) \
677 		__exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
678 		fmt, ## args)
679 
680 /* expand to pr_*() with prefix */
681 #define exfat_err(sb, fmt, ...)						\
682 	pr_err("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
683 #define exfat_warn(sb, fmt, ...)					\
684 	pr_warn("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
685 #define exfat_info(sb, fmt, ...)					\
686 	pr_info("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
687 #define exfat_debug(sb, fmt, ...)					\
688 	pr_debug("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
689 
690 void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
691 		u8 tz, __le16 time, __le16 date, u8 time_cs);
692 void exfat_truncate_atime(struct timespec64 *ts);
693 void exfat_truncate_inode_atime(struct inode *inode);
694 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
695 		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
696 u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
697 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
698 int exfat_update_bh(struct buffer_head *bh, int sync);
699 int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync);
700 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
701 		unsigned int size, unsigned char flags);
702 void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
703 
704 #endif /* !_EXFAT_FS_H */
705