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