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