• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4  */
5 
6 #ifndef _EXFAT_H
7 #define _EXFAT_H
8 
9 #include <linux/types.h>
10 #include <linux/buffer_head.h>
11 
12 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
13   /* For Debugging Purpose */
14 	/* IOCTL code 'f' used by
15 	 *   - file systems typically #0~0x1F
16 	 *   - embedded terminal devices #128~
17 	 *   - exts for debugging purpose #99
18 	 * number 100 and 101 is available now but has possible conflicts
19 	 */
20 #define EXFAT_IOC_GET_DEBUGFLAGS	_IOR('f', 100, long)
21 #define EXFAT_IOC_SET_DEBUGFLAGS	_IOW('f', 101, long)
22 
23 #define EXFAT_DEBUGFLAGS_INVALID_UMOUNT	0x01
24 #define EXFAT_DEBUGFLAGS_ERROR_RW	0x02
25 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
26 
27 #ifdef CONFIG_EXFAT_DEBUG_MSG
28 #define DEBUG	1
29 #else
30 #undef DEBUG
31 #endif
32 
33 #define DENTRY_SIZE		32	/* dir entry size */
34 #define DENTRY_SIZE_BITS	5
35 
36 /* PBR entries */
37 #define PBR_SIGNATURE	0xAA55
38 #define EXT_SIGNATURE	0xAA550000
39 #define VOL_LABEL	"NO NAME    "	/* size should be 11 */
40 #define OEM_NAME	"MSWIN4.1"	/* size should be 8 */
41 #define STR_FAT12	"FAT12   "	/* size should be 8 */
42 #define STR_FAT16	"FAT16   "	/* size should be 8 */
43 #define STR_FAT32	"FAT32   "	/* size should be 8 */
44 #define STR_EXFAT	"EXFAT   "	/* size should be 8 */
45 #define VOL_CLEAN	0x0000
46 #define VOL_DIRTY	0x0002
47 
48 /* max number of clusters */
49 #define FAT12_THRESHOLD		4087		/* 2^12 - 1 + 2 (clu 0 & 1) */
50 #define FAT16_THRESHOLD		65527		/* 2^16 - 1 + 2 */
51 #define FAT32_THRESHOLD		268435457	/* 2^28 - 1 + 2 */
52 #define EXFAT_THRESHOLD		268435457	/* 2^28 - 1 + 2 */
53 
54 /* file types */
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_SYMLINK		0x015F
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_ALL		0x0FFF
75 
76 /* time modes */
77 #define TM_CREATE		0
78 #define TM_MODIFY		1
79 #define TM_ACCESS		2
80 
81 /* checksum types */
82 #define CS_DIR_ENTRY		0
83 #define CS_PBR_SECTOR		1
84 #define CS_DEFAULT		2
85 
86 #define CLUSTER_16(x)		((u16)(x))
87 #define CLUSTER_32(x)		((u32)(x))
88 
89 #define START_SECTOR(x)							\
90 	((((sector_t)((x) - 2)) << p_fs->sectors_per_clu_bits) +	\
91 	 p_fs->data_start_sector)
92 
93 #define IS_LAST_SECTOR_IN_CLUSTER(sec)				\
94 	((((sec) - p_fs->data_start_sector + 1) &		\
95 	  ((1 <<  p_fs->sectors_per_clu_bits) - 1)) == 0)
96 
97 #define GET_CLUSTER_FROM_SECTOR(sec)				\
98 	((u32)((((sec) - p_fs->data_start_sector) >>		\
99 		p_fs->sectors_per_clu_bits) + 2))
100 
101 #define GET16(p_src)						\
102 	(((u16)(p_src)[0]) | (((u16)(p_src)[1]) << 8))
103 #define GET32(p_src)						\
104 	(((u32)(p_src)[0]) | (((u32)(p_src)[1]) << 8) |		\
105 	(((u32)(p_src)[2]) << 16) | (((u32)(p_src)[3]) << 24))
106 #define GET64(p_src) \
107 	(((u64)(p_src)[0]) | (((u64)(p_src)[1]) << 8) |		\
108 	(((u64)(p_src)[2]) << 16) | (((u64)(p_src)[3]) << 24) |	\
109 	(((u64)(p_src)[4]) << 32) | (((u64)(p_src)[5]) << 40) |	\
110 	(((u64)(p_src)[6]) << 48) | (((u64)(p_src)[7]) << 56))
111 
112 #define SET16(p_dst, src)					\
113 	do {							\
114 		(p_dst)[0] = (u8)(src);				\
115 		(p_dst)[1] = (u8)(((u16)(src)) >> 8);		\
116 	} while (0)
117 #define SET32(p_dst, src)					\
118 	do {							\
119 		(p_dst)[0] = (u8)(src);				\
120 		(p_dst)[1] = (u8)(((u32)(src)) >> 8);		\
121 		(p_dst)[2] = (u8)(((u32)(src)) >> 16);		\
122 		(p_dst)[3] = (u8)(((u32)(src)) >> 24);		\
123 	} while (0)
124 #define SET64(p_dst, src)					\
125 	do {							\
126 		(p_dst)[0] = (u8)(src);				\
127 		(p_dst)[1] = (u8)(((u64)(src)) >> 8);		\
128 		(p_dst)[2] = (u8)(((u64)(src)) >> 16);		\
129 		(p_dst)[3] = (u8)(((u64)(src)) >> 24);		\
130 		(p_dst)[4] = (u8)(((u64)(src)) >> 32);		\
131 		(p_dst)[5] = (u8)(((u64)(src)) >> 40);		\
132 		(p_dst)[6] = (u8)(((u64)(src)) >> 48);		\
133 		(p_dst)[7] = (u8)(((u64)(src)) >> 56);		\
134 	} while (0)
135 
136 #ifdef __LITTLE_ENDIAN
137 #define GET16_A(p_src)		(*((u16 *)(p_src)))
138 #define GET32_A(p_src)		(*((u32 *)(p_src)))
139 #define GET64_A(p_src)		(*((u64 *)(p_src)))
140 #define SET16_A(p_dst, src)	(*((u16 *)(p_dst)) = (u16)(src))
141 #define SET32_A(p_dst, src)	(*((u32 *)(p_dst)) = (u32)(src))
142 #define SET64_A(p_dst, src)	(*((u64 *)(p_dst)) = (u64)(src))
143 #else /* BIG_ENDIAN */
144 #define GET16_A(p_src)		GET16(p_src)
145 #define GET32_A(p_src)		GET32(p_src)
146 #define GET64_A(p_src)		GET64(p_src)
147 #define SET16_A(p_dst, src)	SET16(p_dst, src)
148 #define SET32_A(p_dst, src)	SET32(p_dst, src)
149 #define SET64_A(p_dst, src)	SET64(p_dst, src)
150 #endif
151 
152 /* cache size (in number of sectors) */
153 /* (should be an exponential value of 2) */
154 #define FAT_CACHE_SIZE		128
155 #define FAT_CACHE_HASH_SIZE	64
156 #define BUF_CACHE_SIZE		256
157 #define BUF_CACHE_HASH_SIZE	64
158 
159 /* Upcase table macro */
160 #define HIGH_INDEX_BIT	(8)
161 #define HIGH_INDEX_MASK	(0xFF00)
162 #define LOW_INDEX_BIT	(16 - HIGH_INDEX_BIT)
163 #define UTBL_ROW_COUNT	BIT(LOW_INDEX_BIT)
164 #define UTBL_COL_COUNT	BIT(HIGH_INDEX_BIT)
165 
get_col_index(u16 i)166 static inline u16 get_col_index(u16 i)
167 {
168 	return i >> LOW_INDEX_BIT;
169 }
170 
get_row_index(u16 i)171 static inline u16 get_row_index(u16 i)
172 {
173 	return i & ~HIGH_INDEX_MASK;
174 }
175 
176 #define EXFAT_SUPER_MAGIC       (0x2011BAB0L)
177 #define EXFAT_ROOT_INO          1
178 
179 /* FAT types */
180 #define FAT12			0x01	/* FAT12 */
181 #define FAT16			0x0E	/* Win95 FAT16 (LBA) */
182 #define FAT32			0x0C	/* Win95 FAT32 (LBA) */
183 #define EXFAT			0x07	/* exFAT */
184 
185 /* file name lengths */
186 #define MAX_CHARSET_SIZE	3	/* max size of multi-byte character */
187 #define MAX_PATH_DEPTH		15	/* max depth of path name */
188 #define MAX_NAME_LENGTH		256	/* max len of filename including NULL */
189 #define MAX_PATH_LENGTH		260	/* max len of pathname including NULL */
190 #define DOS_NAME_LENGTH		11	/* DOS filename length excluding NULL */
191 #define DOS_PATH_LENGTH		80	/* DOS pathname length excluding NULL */
192 
193 /* file attributes */
194 #define ATTR_NORMAL		0x0000
195 #define ATTR_READONLY		0x0001
196 #define ATTR_HIDDEN		0x0002
197 #define ATTR_SYSTEM		0x0004
198 #define ATTR_VOLUME		0x0008
199 #define ATTR_SUBDIR		0x0010
200 #define ATTR_ARCHIVE		0x0020
201 #define ATTR_SYMLINK		0x0040
202 #define ATTR_EXTEND		0x000F
203 #define ATTR_RWMASK		0x007E
204 
205 /* file creation modes */
206 #define FM_REGULAR              0x00
207 #define FM_SYMLINK              0x40
208 
209 /* return values */
210 #define FFS_SUCCESS             0
211 #define FFS_MEDIAERR            1
212 #define FFS_FORMATERR           2
213 #define FFS_MOUNTED             3
214 #define FFS_NOTMOUNTED          4
215 #define FFS_ALIGNMENTERR        5
216 #define FFS_SEMAPHOREERR        6
217 #define FFS_INVALIDPATH         7
218 #define FFS_INVALIDFID          8
219 #define FFS_NOTFOUND            9
220 #define FFS_FILEEXIST           10
221 #define FFS_PERMISSIONERR       11
222 #define FFS_NOTOPENED           12
223 #define FFS_MAXOPENED           13
224 #define FFS_FULL                14
225 #define FFS_EOF                 15
226 #define FFS_DIRBUSY             16
227 #define FFS_MEMORYERR           17
228 #define FFS_NAMETOOLONG		18
229 #define FFS_ERROR               19
230 
231 #define NUM_UPCASE              2918
232 
233 #define DOS_CUR_DIR_NAME        ".          "
234 #define DOS_PAR_DIR_NAME        "..         "
235 
236 #ifdef __LITTLE_ENDIAN
237 #define UNI_CUR_DIR_NAME        ".\0"
238 #define UNI_PAR_DIR_NAME        ".\0.\0"
239 #else
240 #define UNI_CUR_DIR_NAME        "\0."
241 #define UNI_PAR_DIR_NAME        "\0.\0."
242 #endif
243 
244 struct date_time_t {
245 	u16      Year;
246 	u16      Month;
247 	u16      Day;
248 	u16      Hour;
249 	u16      Minute;
250 	u16      Second;
251 	u16      MilliSecond;
252 };
253 
254 struct part_info_t {
255 	u32      Offset;    /* start sector number of the partition */
256 	u32      Size;      /* in sectors */
257 };
258 
259 struct dev_info_t {
260 	u32      SecSize;    /* sector size in bytes */
261 	u32      DevSize;    /* block device size in sectors */
262 };
263 
264 struct vol_info_t {
265 	u32      FatType;
266 	u32      ClusterSize;
267 	u32      NumClusters;
268 	u32      FreeClusters;
269 	u32      UsedClusters;
270 };
271 
272 /* directory structure */
273 struct chain_t {
274 	u32      dir;
275 	s32       size;
276 	u8       flags;
277 };
278 
279 struct file_id_t {
280 	struct chain_t     dir;
281 	s32       entry;
282 	u32      type;
283 	u32      attr;
284 	u32      start_clu;
285 	u64      size;
286 	u8       flags;
287 	s64       rwoffset;
288 	s32       hint_last_off;
289 	u32      hint_last_clu;
290 };
291 
292 struct dir_entry_t {
293 	char Name[MAX_NAME_LENGTH * MAX_CHARSET_SIZE];
294 
295 	/* used only for FAT12/16/32, not used for exFAT */
296 	char ShortName[DOS_NAME_LENGTH + 2];
297 
298 	u32 Attr;
299 	u64 Size;
300 	u32 NumSubdirs;
301 	struct date_time_t CreateTimestamp;
302 	struct date_time_t ModifyTimestamp;
303 	struct date_time_t AccessTimestamp;
304 };
305 
306 struct timestamp_t {
307 	u16      sec;        /* 0 ~ 59               */
308 	u16      min;        /* 0 ~ 59               */
309 	u16      hour;       /* 0 ~ 23               */
310 	u16      day;        /* 1 ~ 31               */
311 	u16      mon;        /* 1 ~ 12               */
312 	u16      year;       /* 0 ~ 127 (since 1980) */
313 };
314 
315 /* MS_DOS FAT partition boot record (512 bytes) */
316 struct pbr_sector_t {
317 	u8       jmp_boot[3];
318 	u8       oem_name[8];
319 	u8       bpb[109];
320 	u8       boot_code[390];
321 	u8       signature[2];
322 };
323 
324 /* MS-DOS FAT12/16 BIOS parameter block (51 bytes) */
325 struct bpb16_t {
326 	u8       sector_size[2];
327 	u8       sectors_per_clu;
328 	u8       num_reserved[2];
329 	u8       num_fats;
330 	u8       num_root_entries[2];
331 	u8       num_sectors[2];
332 	u8       media_type;
333 	u8       num_fat_sectors[2];
334 	u8       sectors_in_track[2];
335 	u8       num_heads[2];
336 	u8       num_hid_sectors[4];
337 	u8       num_huge_sectors[4];
338 
339 	u8       phy_drv_no;
340 	u8       reserved;
341 	u8       ext_signature;
342 	u8       vol_serial[4];
343 	u8       vol_label[11];
344 	u8       vol_type[8];
345 };
346 
347 /* MS-DOS FAT32 BIOS parameter block (79 bytes) */
348 struct bpb32_t {
349 	u8       sector_size[2];
350 	u8       sectors_per_clu;
351 	u8       num_reserved[2];
352 	u8       num_fats;
353 	u8       num_root_entries[2];
354 	u8       num_sectors[2];
355 	u8       media_type;
356 	u8       num_fat_sectors[2];
357 	u8       sectors_in_track[2];
358 	u8       num_heads[2];
359 	u8       num_hid_sectors[4];
360 	u8       num_huge_sectors[4];
361 	u8       num_fat32_sectors[4];
362 	u8       ext_flags[2];
363 	u8       fs_version[2];
364 	u8       root_cluster[4];
365 	u8       fsinfo_sector[2];
366 	u8       backup_sector[2];
367 	u8       reserved[12];
368 
369 	u8       phy_drv_no;
370 	u8       ext_reserved;
371 	u8       ext_signature;
372 	u8       vol_serial[4];
373 	u8       vol_label[11];
374 	u8       vol_type[8];
375 };
376 
377 /* MS-DOS EXFAT BIOS parameter block (109 bytes) */
378 struct bpbex_t {
379 	u8       reserved1[53];
380 	u8       vol_offset[8];
381 	u8       vol_length[8];
382 	u8       fat_offset[4];
383 	u8       fat_length[4];
384 	u8       clu_offset[4];
385 	u8       clu_count[4];
386 	u8       root_cluster[4];
387 	u8       vol_serial[4];
388 	u8       fs_version[2];
389 	u8       vol_flags[2];
390 	u8       sector_size_bits;
391 	u8       sectors_per_clu_bits;
392 	u8       num_fats;
393 	u8       phy_drv_no;
394 	u8       perc_in_use;
395 	u8       reserved2[7];
396 };
397 
398 /* MS-DOS FAT file system information sector (512 bytes) */
399 struct fsi_sector_t {
400 	u8       signature1[4];
401 	u8       reserved1[480];
402 	u8       signature2[4];
403 	u8       free_cluster[4];
404 	u8       next_cluster[4];
405 	u8       reserved2[14];
406 	u8       signature3[2];
407 };
408 
409 /* MS-DOS FAT directory entry (32 bytes) */
410 struct dentry_t {
411 	u8       dummy[32];
412 };
413 
414 struct dos_dentry_t {
415 	u8       name[DOS_NAME_LENGTH];
416 	u8       attr;
417 	u8       lcase;
418 	u8       create_time_ms;
419 	u8       create_time[2];
420 	u8       create_date[2];
421 	u8       access_date[2];
422 	u8       start_clu_hi[2];
423 	u8       modify_time[2];
424 	u8       modify_date[2];
425 	u8       start_clu_lo[2];
426 	u8       size[4];
427 };
428 
429 /* MS-DOS FAT extended directory entry (32 bytes) */
430 struct ext_dentry_t {
431 	u8       order;
432 	u8       unicode_0_4[10];
433 	u8       attr;
434 	u8       sysid;
435 	u8       checksum;
436 	u8       unicode_5_10[12];
437 	u8       start_clu[2];
438 	u8       unicode_11_12[4];
439 };
440 
441 /* MS-DOS EXFAT file directory entry (32 bytes) */
442 struct file_dentry_t {
443 	u8       type;
444 	u8       num_ext;
445 	u8       checksum[2];
446 	u8       attr[2];
447 	u8       reserved1[2];
448 	u8       create_time[2];
449 	u8       create_date[2];
450 	u8       modify_time[2];
451 	u8       modify_date[2];
452 	u8       access_time[2];
453 	u8       access_date[2];
454 	u8       create_time_ms;
455 	u8       modify_time_ms;
456 	u8       access_time_ms;
457 	u8       reserved2[9];
458 };
459 
460 /* MS-DOS EXFAT stream extension directory entry (32 bytes) */
461 struct strm_dentry_t {
462 	u8       type;
463 	u8       flags;
464 	u8       reserved1;
465 	u8       name_len;
466 	u8       name_hash[2];
467 	u8       reserved2[2];
468 	u8       valid_size[8];
469 	u8       reserved3[4];
470 	u8       start_clu[4];
471 	u8       size[8];
472 };
473 
474 /* MS-DOS EXFAT file name directory entry (32 bytes) */
475 struct name_dentry_t {
476 	u8       type;
477 	u8       flags;
478 	u8       unicode_0_14[30];
479 };
480 
481 /* MS-DOS EXFAT allocation bitmap directory entry (32 bytes) */
482 struct bmap_dentry_t {
483 	u8       type;
484 	u8       flags;
485 	u8       reserved[18];
486 	u8       start_clu[4];
487 	u8       size[8];
488 };
489 
490 /* MS-DOS EXFAT up-case table directory entry (32 bytes) */
491 struct case_dentry_t {
492 	u8       type;
493 	u8       reserved1[3];
494 	u8       checksum[4];
495 	u8       reserved2[12];
496 	u8       start_clu[4];
497 	u8       size[8];
498 };
499 
500 /* MS-DOS EXFAT volume label directory entry (32 bytes) */
501 struct volm_dentry_t {
502 	u8       type;
503 	u8       label_len;
504 	u8       unicode_0_10[22];
505 	u8       reserved[8];
506 };
507 
508 /* unused entry hint information */
509 struct uentry_t {
510 	u32      dir;
511 	s32       entry;
512 	struct chain_t     clu;
513 };
514 
515 /* DOS name structure */
516 struct dos_name_t {
517 	u8       name[DOS_NAME_LENGTH];
518 	u8       name_case;
519 };
520 
521 /* unicode name structure */
522 struct uni_name_t {
523 	u16      name[MAX_NAME_LENGTH];
524 	u16      name_hash;
525 	u8       name_len;
526 };
527 
528 struct buf_cache_t {
529 	struct buf_cache_t *next;
530 	struct buf_cache_t *prev;
531 	struct buf_cache_t *hash_next;
532 	struct buf_cache_t *hash_prev;
533 	s32                drv;
534 	sector_t          sec;
535 	u32               flag;
536 	struct buffer_head   *buf_bh;
537 };
538 
539 struct fs_func {
540 	s32	(*alloc_cluster)(struct super_block *sb, s32 num_alloc,
541 				 struct chain_t *p_chain);
542 	void	(*free_cluster)(struct super_block *sb, struct chain_t *p_chain,
543 				s32 do_relse);
544 	s32	(*count_used_clusters)(struct super_block *sb);
545 
546 	s32	(*init_dir_entry)(struct super_block *sb, struct chain_t *p_dir,
547 				  s32 entry, u32 type, u32 start_clu, u64 size);
548 	s32	(*init_ext_entry)(struct super_block *sb, struct chain_t *p_dir,
549 				  s32 entry, s32 num_entries,
550 				  struct uni_name_t *p_uniname,
551 				  struct dos_name_t *p_dosname);
552 	s32	(*find_dir_entry)(struct super_block *sb, struct chain_t *p_dir,
553 				  struct uni_name_t *p_uniname, s32 num_entries,
554 				  struct dos_name_t *p_dosname, u32 type);
555 	void	(*delete_dir_entry)(struct super_block *sb,
556 				    struct chain_t *p_dir, s32 entry,
557 				    s32 offset, s32 num_entries);
558 	void	(*get_uni_name_from_ext_entry)(struct super_block *sb,
559 					       struct chain_t *p_dir, s32 entry,
560 					       u16 *uniname);
561 	s32	(*count_ext_entries)(struct super_block *sb,
562 				     struct chain_t *p_dir, s32 entry,
563 				     struct dentry_t *p_entry);
564 	s32	(*calc_num_entries)(struct uni_name_t *p_uniname);
565 
566 	u32	(*get_entry_type)(struct dentry_t *p_entry);
567 	void	(*set_entry_type)(struct dentry_t *p_entry, u32 type);
568 	u32	(*get_entry_attr)(struct dentry_t *p_entry);
569 	void	(*set_entry_attr)(struct dentry_t *p_entry, u32 attr);
570 	u8	(*get_entry_flag)(struct dentry_t *p_entry);
571 	void	(*set_entry_flag)(struct dentry_t *p_entry, u8 flag);
572 	u32	(*get_entry_clu0)(struct dentry_t *p_entry);
573 	void	(*set_entry_clu0)(struct dentry_t *p_entry, u32 clu0);
574 	u64	(*get_entry_size)(struct dentry_t *p_entry);
575 	void	(*set_entry_size)(struct dentry_t *p_entry, u64 size);
576 	void	(*get_entry_time)(struct dentry_t *p_entry,
577 				  struct timestamp_t *tp, u8 mode);
578 	void	(*set_entry_time)(struct dentry_t *p_entry,
579 				  struct timestamp_t *tp, u8 mode);
580 };
581 
582 struct fs_info_t {
583 	u32      drv;                    /* drive ID */
584 	u32      vol_type;               /* volume FAT type */
585 	u32      vol_id;                 /* volume serial number */
586 
587 	u64      num_sectors;            /* num of sectors in volume */
588 	u32      num_clusters;           /* num of clusters in volume */
589 	u32      cluster_size;           /* cluster size in bytes */
590 	u32      cluster_size_bits;
591 	u32      sectors_per_clu;        /* cluster size in sectors */
592 	u32      sectors_per_clu_bits;
593 
594 	u32      PBR_sector;             /* PBR sector */
595 	u32      FAT1_start_sector;      /* FAT1 start sector */
596 	u32      FAT2_start_sector;      /* FAT2 start sector */
597 	u32      root_start_sector;      /* root dir start sector */
598 	u32      data_start_sector;      /* data area start sector */
599 	u32      num_FAT_sectors;        /* num of FAT sectors */
600 
601 	u32      root_dir;               /* root dir cluster */
602 	u32      dentries_in_root;       /* num of dentries in root dir */
603 	u32      dentries_per_clu;       /* num of dentries per cluster */
604 
605 	u32      vol_flag;               /* volume dirty flag */
606 	struct buffer_head *pbr_bh;         /* PBR sector */
607 
608 	u32      map_clu;                /* allocation bitmap start cluster */
609 	u32      map_sectors;            /* num of allocation bitmap sectors */
610 	struct buffer_head **vol_amap;      /* allocation bitmap */
611 
612 	u16 **vol_utbl;			/* upcase table */
613 
614 	u32 clu_srch_ptr;		/* cluster search pointer */
615 	u32 used_clusters;		/* number of used clusters */
616 	struct uentry_t hint_uentry;	/* unused entry hint information */
617 
618 	u32 dev_ejected;	/* block device operation error flag */
619 
620 	struct fs_func *fs_func;
621 	struct semaphore v_sem;
622 
623 	/* FAT cache */
624 	struct buf_cache_t FAT_cache_array[FAT_CACHE_SIZE];
625 	struct buf_cache_t FAT_cache_lru_list;
626 	struct buf_cache_t FAT_cache_hash_list[FAT_CACHE_HASH_SIZE];
627 
628 	/* buf cache */
629 	struct buf_cache_t buf_cache_array[BUF_CACHE_SIZE];
630 	struct buf_cache_t buf_cache_lru_list;
631 	struct buf_cache_t buf_cache_hash_list[BUF_CACHE_HASH_SIZE];
632 };
633 
634 #define ES_2_ENTRIES		2
635 #define ES_3_ENTRIES		3
636 #define ES_ALL_ENTRIES		0
637 
638 struct entry_set_cache_t {
639 	/* sector number that contains file_entry */
640 	sector_t sector;
641 
642 	/* byte offset in the sector */
643 	s32 offset;
644 
645 	/*
646 	 * flag in stream entry.
647 	 * 01 for cluster chain,
648 	 * 03 for contig. clusteres.
649 	 */
650 	s32 alloc_flag;
651 
652 	u32 num_entries;
653 
654 	/* __buf should be the last member */
655 	void *__buf;
656 };
657 
658 #define EXFAT_ERRORS_CONT	1	/* ignore error and continue */
659 #define EXFAT_ERRORS_PANIC	2	/* panic on error */
660 #define EXFAT_ERRORS_RO		3	/* remount r/o on error */
661 
662 /* ioctl command */
663 #define EXFAT_IOCTL_GET_VOLUME_ID _IOR('r', 0x12, __u32)
664 
665 struct exfat_mount_options {
666 	kuid_t fs_uid;
667 	kgid_t fs_gid;
668 	unsigned short fs_fmask;
669 	unsigned short fs_dmask;
670 
671 	/* permission for setting the [am]time */
672 	unsigned short allow_utime;
673 
674 	/* codepage for shortname conversions */
675 	unsigned short codepage;
676 
677 	/* charset for filename input/display */
678 	char *iocharset;
679 
680 	unsigned char casesensitive;
681 
682 	/* on error: continue, panic, remount-ro */
683 	unsigned char errors;
684 #ifdef CONFIG_EXFAT_DISCARD
685 	/* flag on if -o dicard specified and device support discard() */
686 	unsigned char discard;
687 #endif /* CONFIG_EXFAT_DISCARD */
688 };
689 
690 #define EXFAT_HASH_BITS		8
691 #define EXFAT_HASH_SIZE		BIT(EXFAT_HASH_BITS)
692 
693 /*
694  * EXFAT file system in-core superblock data
695  */
696 struct bd_info_t {
697 	s32 sector_size;	/* in bytes */
698 	s32 sector_size_bits;
699 	s32 sector_size_mask;
700 
701 	/* total number of sectors in this block device */
702 	s32 num_sectors;
703 
704 	/* opened or not */
705 	bool opened;
706 };
707 
708 struct exfat_sb_info {
709 	struct fs_info_t fs_info;
710 	struct bd_info_t bd_info;
711 
712 	struct exfat_mount_options options;
713 
714 	int s_dirt;
715 	struct mutex s_lock;
716 	struct nls_table *nls_disk; /* Codepage used on disk */
717 	struct nls_table *nls_io;   /* Charset used for input and display */
718 
719 	struct inode *fat_inode;
720 
721 	spinlock_t inode_hash_lock;
722 	struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
723 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
724 	long debug_flags;
725 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
726 };
727 
728 /*
729  * EXFAT file system inode data in memory
730  */
731 struct exfat_inode_info {
732 	struct file_id_t fid;
733 	char  *target;
734 	/* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */
735 	loff_t mmu_private;	/* physically allocated size */
736 	loff_t i_pos;		/* on-disk position of directory entry or 0 */
737 	struct hlist_node i_hash_fat;	/* hash by i_location */
738 	struct rw_semaphore truncate_lock;
739 	struct inode vfs_inode;
740 	struct rw_semaphore i_alloc_sem; /* protect bmap against truncate */
741 };
742 
743 #define EXFAT_SB(sb)		((struct exfat_sb_info *)((sb)->s_fs_info))
744 
EXFAT_I(struct inode * inode)745 static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
746 {
747 	return container_of(inode, struct exfat_inode_info, vfs_inode);
748 }
749 
750 /* NLS management function */
751 u16 nls_upper(struct super_block *sb, u16 a);
752 int nls_dosname_cmp(struct super_block *sb, u8 *a, u8 *b);
753 int nls_uniname_cmp(struct super_block *sb, u16 *a, u16 *b);
754 void nls_uniname_to_dosname(struct super_block *sb,
755 			    struct dos_name_t *p_dosname,
756 			    struct uni_name_t *p_uniname, bool *p_lossy);
757 void nls_dosname_to_uniname(struct super_block *sb,
758 			    struct uni_name_t *p_uniname,
759 			    struct dos_name_t *p_dosname);
760 void nls_uniname_to_cstring(struct super_block *sb, u8 *p_cstring,
761 			    struct uni_name_t *p_uniname);
762 void nls_cstring_to_uniname(struct super_block *sb,
763 			    struct uni_name_t *p_uniname, u8 *p_cstring,
764 			    bool *p_lossy);
765 
766 /* buffer cache management */
767 void buf_init(struct super_block *sb);
768 void buf_shutdown(struct super_block *sb);
769 int FAT_read(struct super_block *sb, u32 loc, u32 *content);
770 s32 FAT_write(struct super_block *sb, u32 loc, u32 content);
771 u8 *FAT_getblk(struct super_block *sb, sector_t sec);
772 void FAT_modify(struct super_block *sb, sector_t sec);
773 void FAT_release_all(struct super_block *sb);
774 void FAT_sync(struct super_block *sb);
775 u8 *buf_getblk(struct super_block *sb, sector_t sec);
776 void buf_modify(struct super_block *sb, sector_t sec);
777 void buf_lock(struct super_block *sb, sector_t sec);
778 void buf_unlock(struct super_block *sb, sector_t sec);
779 void buf_release(struct super_block *sb, sector_t sec);
780 void buf_release_all(struct super_block *sb);
781 void buf_sync(struct super_block *sb);
782 
783 /* fs management functions */
784 void fs_set_vol_flags(struct super_block *sb, u32 new_flag);
785 void fs_error(struct super_block *sb);
786 
787 /* cluster management functions */
788 s32 clear_cluster(struct super_block *sb, u32 clu);
789 s32 fat_alloc_cluster(struct super_block *sb, s32 num_alloc,
790 		      struct chain_t *p_chain);
791 s32 exfat_alloc_cluster(struct super_block *sb, s32 num_alloc,
792 			struct chain_t *p_chain);
793 void fat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
794 		      s32 do_relse);
795 void exfat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
796 			s32 do_relse);
797 u32 find_last_cluster(struct super_block *sb, struct chain_t *p_chain);
798 s32 count_num_clusters(struct super_block *sb, struct chain_t *dir);
799 s32 fat_count_used_clusters(struct super_block *sb);
800 s32 exfat_count_used_clusters(struct super_block *sb);
801 void exfat_chain_cont_cluster(struct super_block *sb, u32 chain, s32 len);
802 
803 /* allocation bitmap management functions */
804 s32 load_alloc_bitmap(struct super_block *sb);
805 void free_alloc_bitmap(struct super_block *sb);
806 s32 set_alloc_bitmap(struct super_block *sb, u32 clu);
807 s32 clr_alloc_bitmap(struct super_block *sb, u32 clu);
808 u32 test_alloc_bitmap(struct super_block *sb, u32 clu);
809 void sync_alloc_bitmap(struct super_block *sb);
810 
811 /* upcase table management functions */
812 s32 load_upcase_table(struct super_block *sb);
813 void free_upcase_table(struct super_block *sb);
814 
815 /* dir entry management functions */
816 u32 fat_get_entry_type(struct dentry_t *p_entry);
817 u32 exfat_get_entry_type(struct dentry_t *p_entry);
818 void fat_set_entry_type(struct dentry_t *p_entry, u32 type);
819 void exfat_set_entry_type(struct dentry_t *p_entry, u32 type);
820 u32 fat_get_entry_attr(struct dentry_t *p_entry);
821 u32 exfat_get_entry_attr(struct dentry_t *p_entry);
822 void fat_set_entry_attr(struct dentry_t *p_entry, u32 attr);
823 void exfat_set_entry_attr(struct dentry_t *p_entry, u32 attr);
824 u8 fat_get_entry_flag(struct dentry_t *p_entry);
825 u8 exfat_get_entry_flag(struct dentry_t *p_entry);
826 void fat_set_entry_flag(struct dentry_t *p_entry, u8 flag);
827 void exfat_set_entry_flag(struct dentry_t *p_entry, u8 flag);
828 u32 fat_get_entry_clu0(struct dentry_t *p_entry);
829 u32 exfat_get_entry_clu0(struct dentry_t *p_entry);
830 void fat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu);
831 void exfat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu);
832 u64 fat_get_entry_size(struct dentry_t *p_entry);
833 u64 exfat_get_entry_size(struct dentry_t *p_entry);
834 void fat_set_entry_size(struct dentry_t *p_entry, u64 size);
835 void exfat_set_entry_size(struct dentry_t *p_entry, u64 size);
836 struct timestamp_t *tm_current(struct timestamp_t *tm);
837 void fat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
838 			u8 mode);
839 void exfat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
840 			  u8 mode);
841 void fat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
842 			u8 mode);
843 void exfat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
844 			  u8 mode);
845 s32 fat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir, s32 entry,
846 		       u32 type, u32 start_clu, u64 size);
847 s32 exfat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir,
848 			 s32 entry, u32 type, u32 start_clu, u64 size);
849 s32 fat_init_ext_dir_entry(struct super_block *sb, struct chain_t *p_dir,
850 			   s32 entry, s32 num_entries,
851 			   struct uni_name_t *p_uniname,
852 			   struct dos_name_t *p_dosname);
853 s32 exfat_init_ext_dir_entry(struct super_block *sb, struct chain_t *p_dir,
854 			     s32 entry, s32 num_entries,
855 			     struct uni_name_t *p_uniname,
856 		struct dos_name_t *p_dosname);
857 void init_dos_entry(struct dos_dentry_t *ep, u32 type, u32 start_clu);
858 void init_ext_entry(struct ext_dentry_t *ep, s32 order, u8 chksum,
859 		    u16 *uniname);
860 void init_file_entry(struct file_dentry_t *ep, u32 type);
861 void init_strm_entry(struct strm_dentry_t *ep, u8 flags, u32 start_clu,
862 		     u64 size);
863 void init_name_entry(struct name_dentry_t *ep, u16 *uniname);
864 void fat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
865 			  s32 entry, s32 order, s32 num_entries);
866 void exfat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
867 			    s32 entry, s32 order, s32 num_entries);
868 
869 s32 find_location(struct super_block *sb, struct chain_t *p_dir, s32 entry,
870 		  sector_t *sector, s32 *offset);
871 struct dentry_t *get_entry_with_sector(struct super_block *sb, sector_t sector,
872 				       s32 offset);
873 struct dentry_t *get_entry_in_dir(struct super_block *sb, struct chain_t *p_dir,
874 				  s32 entry, sector_t *sector);
875 struct entry_set_cache_t *get_entry_set_in_dir(struct super_block *sb,
876 					       struct chain_t *p_dir, s32 entry,
877 					       u32 type,
878 					       struct dentry_t **file_ep);
879 void release_entry_set(struct entry_set_cache_t *es);
880 s32 write_whole_entry_set(struct super_block *sb, struct entry_set_cache_t *es);
881 s32 write_partial_entries_in_entry_set(struct super_block *sb,
882 				       struct entry_set_cache_t *es,
883 				       struct dentry_t *ep, u32 count);
884 s32 search_deleted_or_unused_entry(struct super_block *sb,
885 				   struct chain_t *p_dir, s32 num_entries);
886 s32 find_empty_entry(struct inode *inode, struct chain_t *p_dir,
887 		     s32 num_entries);
888 s32 fat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
889 		       struct uni_name_t *p_uniname, s32 num_entries,
890 		       struct dos_name_t *p_dosname, u32 type);
891 s32 exfat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
892 			 struct uni_name_t *p_uniname, s32 num_entries,
893 			 struct dos_name_t *p_dosname, u32 type);
894 s32 fat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
895 			  s32 entry, struct dentry_t *p_entry);
896 s32 exfat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
897 			    s32 entry, struct dentry_t *p_entry);
898 s32 count_dos_name_entries(struct super_block *sb, struct chain_t *p_dir,
899 			   u32 type);
900 void update_dir_checksum(struct super_block *sb, struct chain_t *p_dir,
901 			 s32 entry);
902 void update_dir_checksum_with_entry_set(struct super_block *sb,
903 					struct entry_set_cache_t *es);
904 bool is_dir_empty(struct super_block *sb, struct chain_t *p_dir);
905 
906 /* name conversion functions */
907 s32 get_num_entries_and_dos_name(struct super_block *sb, struct chain_t *p_dir,
908 				 struct uni_name_t *p_uniname, s32 *entries,
909 				 struct dos_name_t *p_dosname);
910 void get_uni_name_from_dos_entry(struct super_block *sb,
911 				 struct dos_dentry_t *ep,
912 				 struct uni_name_t *p_uniname, u8 mode);
913 void fat_get_uni_name_from_ext_entry(struct super_block *sb,
914 				     struct chain_t *p_dir, s32 entry,
915 				     u16 *uniname);
916 void exfat_get_uni_name_from_ext_entry(struct super_block *sb,
917 				       struct chain_t *p_dir, s32 entry,
918 				       u16 *uniname);
919 s32 extract_uni_name_from_ext_entry(struct ext_dentry_t *ep,
920 				    u16 *uniname, s32 order);
921 s32 extract_uni_name_from_name_entry(struct name_dentry_t *ep,
922 				     u16 *uniname, s32 order);
923 s32 fat_generate_dos_name(struct super_block *sb, struct chain_t *p_dir,
924 			  struct dos_name_t *p_dosname);
925 void fat_attach_count_to_dos_name(u8 *dosname, s32 count);
926 s32 fat_calc_num_entries(struct uni_name_t *p_uniname);
927 s32 exfat_calc_num_entries(struct uni_name_t *p_uniname);
928 u8 calc_checksum_1byte(void *data, s32 len, u8 chksum);
929 u16 calc_checksum_2byte(void *data, s32 len, u16 chksum, s32 type);
930 u32 calc_checksum_4byte(void *data, s32 len, u32 chksum, s32 type);
931 
932 /* name resolution functions */
933 s32 resolve_path(struct inode *inode, char *path, struct chain_t *p_dir,
934 		 struct uni_name_t *p_uniname);
935 s32 resolve_name(u8 *name, u8 **arg);
936 
937 /* file operation functions */
938 s32 fat16_mount(struct super_block *sb, struct pbr_sector_t *p_pbr);
939 s32 fat32_mount(struct super_block *sb, struct pbr_sector_t *p_pbr);
940 s32 exfat_mount(struct super_block *sb, struct pbr_sector_t *p_pbr);
941 s32 create_dir(struct inode *inode, struct chain_t *p_dir,
942 	       struct uni_name_t *p_uniname, struct file_id_t *fid);
943 s32 create_file(struct inode *inode, struct chain_t *p_dir,
944 		struct uni_name_t *p_uniname, u8 mode, struct file_id_t *fid);
945 void remove_file(struct inode *inode, struct chain_t *p_dir, s32 entry);
946 s32 exfat_rename_file(struct inode *inode, struct chain_t *p_dir, s32 old_entry,
947 		      struct uni_name_t *p_uniname, struct file_id_t *fid);
948 s32 move_file(struct inode *inode, struct chain_t *p_olddir, s32 oldentry,
949 	      struct chain_t *p_newdir, struct uni_name_t *p_uniname,
950 	      struct file_id_t *fid);
951 
952 /* sector read/write functions */
953 int sector_read(struct super_block *sb, sector_t sec,
954 		struct buffer_head **bh, bool read);
955 int sector_write(struct super_block *sb, sector_t sec,
956 		 struct buffer_head *bh, bool sync);
957 int multi_sector_read(struct super_block *sb, sector_t sec,
958 		      struct buffer_head **bh, s32 num_secs, bool read);
959 int multi_sector_write(struct super_block *sb, sector_t sec,
960 		       struct buffer_head *bh, s32 num_secs, bool sync);
961 
962 void bdev_open(struct super_block *sb);
963 void bdev_close(struct super_block *sb);
964 int bdev_read(struct super_block *sb, sector_t secno,
965 	      struct buffer_head **bh, u32 num_secs, bool read);
966 int bdev_write(struct super_block *sb, sector_t secno,
967 	       struct buffer_head *bh, u32 num_secs, bool sync);
968 int bdev_sync(struct super_block *sb);
969 
970 extern const u8 uni_upcase[];
971 #endif /* _EXFAT_H */
972