]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/exfat/exfat_fs.h
exfat: add boot region verification
[mirror_ubuntu-hirsute-kernel.git] / fs / exfat / exfat_fs.h
CommitLineData
1acf1a56
NJ
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
13#define EXFAT_SUPER_MAGIC 0x2011BAB0UL
14#define EXFAT_ROOT_INO 1
15
16#define EXFAT_SB_DIRTY 0
17
18#define EXFAT_CLUSTERS_UNTRACKED (~0u)
19
20/*
21 * exfat error flags
22 */
23enum 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 */
32enum {
33 NLS_NAME_NO_LOSSY, /* no lossy */
34 NLS_NAME_LOSSY, /* just detected incorrect filename(s) */
35 NLS_NAME_OVERLEN, /* the length is over than its limit */
36};
37
38#define EXFAT_HASH_BITS 8
39#define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS)
40
41/*
42 * Type Definitions
43 */
44#define ES_2_ENTRIES 2
45#define ES_ALL_ENTRIES 0
46
47#define DIR_DELETED 0xFFFF0321
48
49/* type values */
50#define TYPE_UNUSED 0x0000
51#define TYPE_DELETED 0x0001
52#define TYPE_INVALID 0x0002
53#define TYPE_CRITICAL_PRI 0x0100
54#define TYPE_BITMAP 0x0101
55#define TYPE_UPCASE 0x0102
56#define TYPE_VOLUME 0x0103
57#define TYPE_DIR 0x0104
58#define TYPE_FILE 0x011F
59#define TYPE_CRITICAL_SEC 0x0200
60#define TYPE_STREAM 0x0201
61#define TYPE_EXTEND 0x0202
62#define TYPE_ACL 0x0203
63#define TYPE_BENIGN_PRI 0x0400
64#define TYPE_GUID 0x0401
65#define TYPE_PADDING 0x0402
66#define TYPE_ACLTAB 0x0403
67#define TYPE_BENIGN_SEC 0x0800
68#define TYPE_ALL 0x0FFF
69
70#define MAX_CHARSET_SIZE 6 /* max size of multi-byte character */
71#define MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */
72#define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE)
73
943af1fd
TK
74/* Enough size to hold 256 dentry (even 512 Byte sector) */
75#define DIR_CACHE_SIZE (256*sizeof(struct exfat_dentry)/512+1)
1acf1a56
NJ
76
77#define EXFAT_HINT_NONE -1
78#define EXFAT_MIN_SUBDIR 2
79
80/*
81 * helpers for cluster size to byte conversion.
82 */
83#define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits)
84#define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits)
85#define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \
86 (((b - 1) >> (sbi)->cluster_size_bits) + 1)
87#define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1))
88
89/*
90 * helpers for block size to byte conversion.
91 */
92#define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits)
93#define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits)
94#define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \
95 (((b - 1) >> (sb)->s_blocksize_bits) + 1)
96#define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1))
97
98/*
99 * helpers for block size to dentry size conversion.
100 */
101#define EXFAT_B_TO_DEN_IDX(b, sbi) \
102 ((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS))
103#define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS)
104#define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS)
105
106/*
107 * helpers for fat entry.
108 */
109#define FAT_ENT_SIZE (4)
110#define FAT_ENT_SIZE_BITS (2)
111#define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
112 (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
113#define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \
114 ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
115
116/*
117 * helpers for bitmap.
118 */
119#define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS)
120#define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS)
121#define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE)
122#define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1)
123#define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \
124 ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits)
125#define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb))
126#define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \
127 ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1))
128#define BITS_PER_BYTE_MASK 0x7
129#define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1)
130
131struct exfat_dentry_namebuf {
132 char *lfn;
133 int lfnbuf_len; /* usally MAX_UNINAME_BUF_SIZE */
134};
135
136/* unicode name structure */
137struct exfat_uni_name {
138 /* +3 for null and for converting */
139 unsigned short name[MAX_NAME_LENGTH + 3];
140 unsigned short name_hash;
141 unsigned char name_len;
142};
143
144/* directory structure */
145struct exfat_chain {
146 unsigned int dir;
147 unsigned int size;
148 unsigned char flags;
149};
150
151/* first empty entry hint information */
152struct exfat_hint_femp {
153 /* entry index of a directory */
154 int eidx;
155 /* count of continuous empty entry */
156 int count;
157 /* the cluster that first empty slot exists in */
158 struct exfat_chain cur;
159};
160
161/* hint structure */
162struct exfat_hint {
163 unsigned int clu;
164 union {
165 unsigned int off; /* cluster offset */
166 int eidx; /* entry index */
167 };
168};
169
170struct exfat_entry_set_cache {
943af1fd
TK
171 struct super_block *sb;
172 bool modified;
173 unsigned int start_off;
174 int num_bh;
175 struct buffer_head *bh[DIR_CACHE_SIZE];
1acf1a56 176 unsigned int num_entries;
1acf1a56
NJ
177};
178
179struct exfat_dir_entry {
180 struct exfat_chain 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 unsigned int num_subdirs;
188 struct timespec64 atime;
189 struct timespec64 mtime;
190 struct timespec64 crtime;
191 struct exfat_dentry_namebuf namebuf;
192};
193
194/*
195 * exfat mount in-memory data
196 */
197struct exfat_mount_options {
198 kuid_t fs_uid;
199 kgid_t fs_gid;
200 unsigned short fs_fmask;
201 unsigned short fs_dmask;
202 /* permission for setting the [am]time */
203 unsigned short allow_utime;
204 /* charset for filename input/display */
205 char *iocharset;
206 /* on error: continue, panic, remount-ro */
207 enum exfat_error_mode errors;
208 unsigned utf8:1, /* Use of UTF-8 character set */
209 discard:1; /* Issue discard requests on deletions */
210 int time_offset; /* Offset of timestamps from UTC (in minutes) */
211};
212
213/*
214 * EXFAT file system superblock in-memory data
215 */
216struct exfat_sb_info {
217 unsigned long long num_sectors; /* num of sectors in volume */
218 unsigned int num_clusters; /* num of clusters in volume */
219 unsigned int cluster_size; /* cluster size in bytes */
220 unsigned int cluster_size_bits;
221 unsigned int sect_per_clus; /* cluster size in sectors */
222 unsigned int sect_per_clus_bits;
223 unsigned long long FAT1_start_sector; /* FAT1 start sector */
224 unsigned long long FAT2_start_sector; /* FAT2 start sector */
225 unsigned long long data_start_sector; /* data area start sector */
226 unsigned int num_FAT_sectors; /* num of FAT sectors */
227 unsigned int root_dir; /* root dir cluster */
228 unsigned int dentries_per_clu; /* num of dentries per cluster */
229 unsigned int vol_flag; /* volume dirty flag */
181a9e80 230 struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
1acf1a56
NJ
231
232 unsigned int map_clu; /* allocation bitmap start cluster */
233 unsigned int map_sectors; /* num of allocation bitmap sectors */
234 struct buffer_head **vol_amap; /* allocation bitmap */
235
236 unsigned short *vol_utbl; /* upcase table */
237
238 unsigned int clu_srch_ptr; /* cluster search pointer */
239 unsigned int used_clusters; /* number of used clusters */
240
241 unsigned long s_state;
242 struct mutex s_lock; /* superblock lock */
243 struct exfat_mount_options options;
244 struct nls_table *nls_io; /* Charset used for input and display */
245 struct ratelimit_state ratelimit;
246
247 spinlock_t inode_hash_lock;
248 struct hlist_head inode_hashtable[EXFAT_HASH_SIZE];
249
250 struct rcu_head rcu;
251};
252
253/*
254 * EXFAT file system inode in-memory data
255 */
256struct exfat_inode_info {
257 struct exfat_chain dir;
258 int entry;
259 unsigned int type;
260 unsigned short attr;
261 unsigned int start_clu;
262 unsigned char flags;
263 /*
264 * the copy of low 32bit of i_version to check
265 * the validation of hint_stat.
266 */
267 unsigned int version;
268 /* file offset or dentry index for readdir */
269 loff_t rwoffset;
270
271 /* hint for cluster last accessed */
272 struct exfat_hint hint_bmap;
273 /* hint for entry index we try to lookup next time */
274 struct exfat_hint hint_stat;
275 /* hint for first empty entry */
276 struct exfat_hint_femp hint_femp;
277
278 spinlock_t cache_lru_lock;
279 struct list_head cache_lru;
280 int nr_caches;
281 /* for avoiding the race between alloc and free */
282 unsigned int cache_valid_id;
283
284 /*
285 * NOTE: i_size_ondisk is 64bits, so must hold ->inode_lock to access.
286 * physically allocated size.
287 */
288 loff_t i_size_ondisk;
289 /* block-aligned i_size (used in cont_write_begin) */
290 loff_t i_size_aligned;
291 /* on-disk position of directory entry or 0 */
292 loff_t i_pos;
293 /* hash by i_location */
294 struct hlist_node i_hash_fat;
295 /* protect bmap against truncate */
296 struct rw_semaphore truncate_lock;
297 struct inode vfs_inode;
298 /* File creation time */
299 struct timespec64 i_crtime;
300};
301
302static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb)
303{
304 return sb->s_fs_info;
305}
306
307static inline struct exfat_inode_info *EXFAT_I(struct inode *inode)
308{
309 return container_of(inode, struct exfat_inode_info, vfs_inode);
310}
311
312/*
313 * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to
314 * save ATTR_RO instead of ->i_mode.
315 *
316 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
317 * bit, it's just used as flag for app.
318 */
319static inline int exfat_mode_can_hold_ro(struct inode *inode)
320{
321 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
322
323 if (S_ISDIR(inode->i_mode))
324 return 0;
325
326 if ((~sbi->options.fs_fmask) & 0222)
327 return 1;
328 return 0;
329}
330
331/* Convert attribute bits and a mask to the UNIX mode. */
332static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi,
333 unsigned short attr, mode_t mode)
334{
335 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
336 mode &= ~0222;
337
338 if (attr & ATTR_SUBDIR)
339 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
340
341 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
342}
343
344/* Return the FAT attribute byte for this inode */
345static inline unsigned short exfat_make_attr(struct inode *inode)
346{
347 unsigned short attr = EXFAT_I(inode)->attr;
348
349 if (S_ISDIR(inode->i_mode))
350 attr |= ATTR_SUBDIR;
351 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
352 attr |= ATTR_READONLY;
353 return attr;
354}
355
356static inline void exfat_save_attr(struct inode *inode, unsigned short attr)
357{
358 if (exfat_mode_can_hold_ro(inode))
359 EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY);
360 else
361 EXFAT_I(inode)->attr = attr & ATTR_RWMASK;
362}
363
364static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi,
365 sector_t sec)
366{
367 return ((sec - sbi->data_start_sector + 1) &
368 ((1 << sbi->sect_per_clus_bits) - 1)) == 0;
369}
370
371static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi,
372 unsigned int clus)
373{
374 return ((clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) +
375 sbi->data_start_sector;
376}
377
378static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
379 sector_t sec)
380{
381 return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) +
382 EXFAT_RESERVED_CLUSTERS;
383}
384
385/* super.c */
386int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag);
387
388/* fatent.c */
389#define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
390
391int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
392 struct exfat_chain *p_chain);
393int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
394int exfat_ent_get(struct super_block *sb, unsigned int loc,
395 unsigned int *content);
396int exfat_ent_set(struct super_block *sb, unsigned int loc,
397 unsigned int content);
398int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
399 int entry, struct exfat_dentry *p_entry);
400int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
401 unsigned int len);
402int exfat_zeroed_cluster(struct inode *dir, unsigned int clu);
403int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
404 unsigned int *ret_clu);
405int exfat_count_num_clusters(struct super_block *sb,
406 struct exfat_chain *p_chain, unsigned int *ret_count);
407
408/* balloc.c */
409int exfat_load_bitmap(struct super_block *sb);
410void exfat_free_bitmap(struct exfat_sb_info *sbi);
411int exfat_set_bitmap(struct inode *inode, unsigned int clu);
412void exfat_clear_bitmap(struct inode *inode, unsigned int clu);
413unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
414int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
415
416/* file.c */
417extern const struct file_operations exfat_file_operations;
418int __exfat_truncate(struct inode *inode, loff_t new_size);
419void exfat_truncate(struct inode *inode, loff_t size);
420int exfat_setattr(struct dentry *dentry, struct iattr *attr);
421int exfat_getattr(const struct path *path, struct kstat *stat,
422 unsigned int request_mask, unsigned int query_flags);
423
424/* namei.c */
425extern const struct dentry_operations exfat_dentry_ops;
426extern const struct dentry_operations exfat_utf8_dentry_ops;
427
428/* cache.c */
429int exfat_cache_init(void);
430void exfat_cache_shutdown(void);
431void exfat_cache_init_inode(struct inode *inode);
432void exfat_cache_inval_inode(struct inode *inode);
433int exfat_get_cluster(struct inode *inode, unsigned int cluster,
434 unsigned int *fclus, unsigned int *dclus,
435 unsigned int *last_dclus, int allow_eof);
436
437/* dir.c */
438extern const struct inode_operations exfat_dir_inode_operations;
439extern const struct file_operations exfat_dir_operations;
440unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry);
441int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
442 int entry, unsigned int type, unsigned int start_clu,
443 unsigned long long size);
444int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
445 int entry, int num_entries, struct exfat_uni_name *p_uniname);
446int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
447 int entry, int order, int num_entries);
448int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
449 int entry);
943af1fd 450void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
1acf1a56
NJ
451int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
452int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
453 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
454 int num_entries, unsigned int type);
455int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
456int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
457 int entry, sector_t *sector, int *offset);
458struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
459 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
460 sector_t *sector);
943af1fd
TK
461struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es,
462 int num);
1acf1a56 463struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
943af1fd
TK
464 struct exfat_chain *p_dir, int entry, unsigned int type);
465void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync);
1acf1a56
NJ
466int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
467
468/* inode.c */
469extern const struct inode_operations exfat_file_inode_operations;
470void exfat_sync_inode(struct inode *inode);
471struct inode *exfat_build_inode(struct super_block *sb,
472 struct exfat_dir_entry *info, loff_t i_pos);
473void exfat_hash_inode(struct inode *inode, loff_t i_pos);
474void exfat_unhash_inode(struct inode *inode);
475struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
476int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
477void exfat_evict_inode(struct inode *inode);
478int exfat_block_truncate_page(struct inode *inode, loff_t from);
479
480/* exfat/nls.c */
481unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
482int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a,
483 unsigned short *b, unsigned int len);
484int exfat_utf16_to_nls(struct super_block *sb,
485 struct exfat_uni_name *uniname, unsigned char *p_cstring,
486 int len);
487int exfat_nls_to_utf16(struct super_block *sb,
488 const unsigned char *p_cstring, const int len,
489 struct exfat_uni_name *uniname, int *p_lossy);
490int exfat_create_upcase_table(struct super_block *sb);
491void exfat_free_upcase_table(struct exfat_sb_info *sbi);
1acf1a56
NJ
492
493/* exfat/misc.c */
494void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
495 __printf(3, 4) __cold;
496#define exfat_fs_error(sb, fmt, args...) \
497 __exfat_fs_error(sb, 1, fmt, ## args)
498#define exfat_fs_error_ratelimit(sb, fmt, args...) \
499 __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \
500 fmt, ## args)
501void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...)
502 __printf(3, 4) __cold;
d1727d55
JP
503#define exfat_err(sb, fmt, ...) \
504 exfat_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
505#define exfat_warn(sb, fmt, ...) \
506 exfat_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
507#define exfat_info(sb, fmt, ...) \
508 exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__)
509
1acf1a56 510void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
ed0f84d3 511 u8 tz, __le16 time, __le16 date, u8 time_cs);
81df1ad4 512void exfat_truncate_atime(struct timespec64 *ts);
1acf1a56 513void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
ed0f84d3 514 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
1acf1a56
NJ
515unsigned short exfat_calc_chksum_2byte(void *data, int len,
516 unsigned short chksum, int type);
476189c0 517u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
1acf1a56
NJ
518void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync);
519void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
520 unsigned int size, unsigned char flags);
521void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec);
522
523#endif /* !_EXFAT_FS_H */