0001
0002 #ifndef _FAT_H
0003 #define _FAT_H
0004
0005 #include <linux/buffer_head.h>
0006 #include <linux/nls.h>
0007 #include <linux/hash.h>
0008 #include <linux/ratelimit.h>
0009 #include <linux/msdos_fs.h>
0010
0011
0012
0013
0014 #define VFAT_SFN_DISPLAY_LOWER 0x0001
0015 #define VFAT_SFN_DISPLAY_WIN95 0x0002
0016 #define VFAT_SFN_DISPLAY_WINNT 0x0004
0017 #define VFAT_SFN_CREATE_WIN95 0x0100
0018 #define VFAT_SFN_CREATE_WINNT 0x0200
0019
0020 #define FAT_ERRORS_CONT 1
0021 #define FAT_ERRORS_PANIC 2
0022 #define FAT_ERRORS_RO 3
0023
0024 #define FAT_NFS_STALE_RW 1
0025 #define FAT_NFS_NOSTALE_RO 2
0026
0027 struct fat_mount_options {
0028 kuid_t fs_uid;
0029 kgid_t fs_gid;
0030 unsigned short fs_fmask;
0031 unsigned short fs_dmask;
0032 unsigned short codepage;
0033 int time_offset;
0034 char *iocharset;
0035 unsigned short shortname;
0036 unsigned char name_check;
0037 unsigned char errors;
0038 unsigned char nfs;
0039 unsigned short allow_utime;
0040 unsigned quiet:1,
0041 showexec:1,
0042 sys_immutable:1,
0043 dotsOK:1,
0044 isvfat:1,
0045 utf8:1,
0046 unicode_xlate:1,
0047 numtail:1,
0048 flush:1,
0049 nocase:1,
0050 usefree:1,
0051 tz_set:1,
0052 rodir:1,
0053 discard:1,
0054 dos1xfloppy:1;
0055 };
0056
0057 #define FAT_HASH_BITS 8
0058 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
0059
0060
0061
0062
0063 struct msdos_sb_info {
0064 unsigned short sec_per_clus;
0065 unsigned short cluster_bits;
0066 unsigned int cluster_size;
0067 unsigned char fats, fat_bits;
0068 unsigned short fat_start;
0069 unsigned long fat_length;
0070 unsigned long dir_start;
0071 unsigned short dir_entries;
0072 unsigned long data_start;
0073 unsigned long max_cluster;
0074 unsigned long root_cluster;
0075 unsigned long fsinfo_sector;
0076 struct mutex fat_lock;
0077 struct mutex nfs_build_inode_lock;
0078 struct mutex s_lock;
0079 unsigned int prev_free;
0080 unsigned int free_clusters;
0081 unsigned int free_clus_valid;
0082 struct fat_mount_options options;
0083 struct nls_table *nls_disk;
0084 struct nls_table *nls_io;
0085 const void *dir_ops;
0086 int dir_per_block;
0087 int dir_per_block_bits;
0088 unsigned int vol_id;
0089
0090 int fatent_shift;
0091 const struct fatent_operations *fatent_ops;
0092 struct inode *fat_inode;
0093 struct inode *fsinfo_inode;
0094
0095 struct ratelimit_state ratelimit;
0096
0097 spinlock_t inode_hash_lock;
0098 struct hlist_head inode_hashtable[FAT_HASH_SIZE];
0099
0100 spinlock_t dir_hash_lock;
0101 struct hlist_head dir_hashtable[FAT_HASH_SIZE];
0102
0103 unsigned int dirty;
0104 struct rcu_head rcu;
0105 };
0106
0107 #define FAT_CACHE_VALID 0
0108
0109
0110
0111
0112 struct msdos_inode_info {
0113 spinlock_t cache_lru_lock;
0114 struct list_head cache_lru;
0115 int nr_caches;
0116
0117 unsigned int cache_valid_id;
0118
0119
0120 loff_t mmu_private;
0121
0122 int i_start;
0123 int i_logstart;
0124 int i_attrs;
0125 loff_t i_pos;
0126 struct hlist_node i_fat_hash;
0127 struct hlist_node i_dir_hash;
0128 struct rw_semaphore truncate_lock;
0129 struct timespec64 i_crtime;
0130 struct inode vfs_inode;
0131 };
0132
0133 struct fat_slot_info {
0134 loff_t i_pos;
0135 loff_t slot_off;
0136 int nr_slots;
0137 struct msdos_dir_entry *de;
0138 struct buffer_head *bh;
0139 };
0140
0141 static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
0142 {
0143 return sb->s_fs_info;
0144 }
0145
0146
0147
0148
0149
0150 static inline bool is_fat12(const struct msdos_sb_info *sbi)
0151 {
0152 return sbi->fat_bits == 12;
0153 }
0154
0155 static inline bool is_fat16(const struct msdos_sb_info *sbi)
0156 {
0157 return sbi->fat_bits == 16;
0158 }
0159
0160 static inline bool is_fat32(const struct msdos_sb_info *sbi)
0161 {
0162 return sbi->fat_bits == 32;
0163 }
0164
0165
0166 static inline u32 max_fat(struct super_block *sb)
0167 {
0168 struct msdos_sb_info *sbi = MSDOS_SB(sb);
0169
0170 return is_fat32(sbi) ? MAX_FAT32 :
0171 is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
0172 }
0173
0174 static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
0175 {
0176 return container_of(inode, struct msdos_inode_info, vfs_inode);
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186 static inline int fat_mode_can_hold_ro(struct inode *inode)
0187 {
0188 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
0189 umode_t mask;
0190
0191 if (S_ISDIR(inode->i_mode)) {
0192 if (!sbi->options.rodir)
0193 return 0;
0194 mask = ~sbi->options.fs_dmask;
0195 } else
0196 mask = ~sbi->options.fs_fmask;
0197
0198 if (!(mask & S_IWUGO))
0199 return 0;
0200 return 1;
0201 }
0202
0203
0204 static inline umode_t fat_make_mode(struct msdos_sb_info *sbi,
0205 u8 attrs, umode_t mode)
0206 {
0207 if (attrs & ATTR_RO && !((attrs & ATTR_DIR) && !sbi->options.rodir))
0208 mode &= ~S_IWUGO;
0209
0210 if (attrs & ATTR_DIR)
0211 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
0212 else
0213 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
0214 }
0215
0216
0217 static inline u8 fat_make_attrs(struct inode *inode)
0218 {
0219 u8 attrs = MSDOS_I(inode)->i_attrs;
0220 if (S_ISDIR(inode->i_mode))
0221 attrs |= ATTR_DIR;
0222 if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO))
0223 attrs |= ATTR_RO;
0224 return attrs;
0225 }
0226
0227 static inline void fat_save_attrs(struct inode *inode, u8 attrs)
0228 {
0229 if (fat_mode_can_hold_ro(inode))
0230 MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED;
0231 else
0232 MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO);
0233 }
0234
0235 static inline unsigned char fat_checksum(const __u8 *name)
0236 {
0237 unsigned char s = name[0];
0238 s = (s<<7) + (s>>1) + name[1]; s = (s<<7) + (s>>1) + name[2];
0239 s = (s<<7) + (s>>1) + name[3]; s = (s<<7) + (s>>1) + name[4];
0240 s = (s<<7) + (s>>1) + name[5]; s = (s<<7) + (s>>1) + name[6];
0241 s = (s<<7) + (s>>1) + name[7]; s = (s<<7) + (s>>1) + name[8];
0242 s = (s<<7) + (s>>1) + name[9]; s = (s<<7) + (s>>1) + name[10];
0243 return s;
0244 }
0245
0246 static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
0247 {
0248 return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
0249 + sbi->data_start;
0250 }
0251
0252 static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
0253 loff_t i_pos, sector_t *blknr, int *offset)
0254 {
0255 *blknr = i_pos >> sbi->dir_per_block_bits;
0256 *offset = i_pos & (sbi->dir_per_block - 1);
0257 }
0258
0259 static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
0260 struct inode *inode)
0261 {
0262 loff_t i_pos;
0263 #if BITS_PER_LONG == 32
0264 spin_lock(&sbi->inode_hash_lock);
0265 #endif
0266 i_pos = MSDOS_I(inode)->i_pos;
0267 #if BITS_PER_LONG == 32
0268 spin_unlock(&sbi->inode_hash_lock);
0269 #endif
0270 return i_pos;
0271 }
0272
0273 static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
0274 {
0275 #ifdef __BIG_ENDIAN
0276 while (len--) {
0277 *dst++ = src[0] | (src[1] << 8);
0278 src += 2;
0279 }
0280 #else
0281 memcpy(dst, src, len * 2);
0282 #endif
0283 }
0284
0285 static inline int fat_get_start(const struct msdos_sb_info *sbi,
0286 const struct msdos_dir_entry *de)
0287 {
0288 int cluster = le16_to_cpu(de->start);
0289 if (is_fat32(sbi))
0290 cluster |= (le16_to_cpu(de->starthi) << 16);
0291 return cluster;
0292 }
0293
0294 static inline void fat_set_start(struct msdos_dir_entry *de, int cluster)
0295 {
0296 de->start = cpu_to_le16(cluster);
0297 de->starthi = cpu_to_le16(cluster >> 16);
0298 }
0299
0300 static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
0301 {
0302 #ifdef __BIG_ENDIAN
0303 while (len--) {
0304 dst[0] = *src & 0x00FF;
0305 dst[1] = (*src & 0xFF00) >> 8;
0306 dst += 2;
0307 src++;
0308 }
0309 #else
0310 memcpy(dst, src, len * 2);
0311 #endif
0312 }
0313
0314
0315 extern void fat_cache_inval_inode(struct inode *inode);
0316 extern int fat_get_cluster(struct inode *inode, int cluster,
0317 int *fclus, int *dclus);
0318 extern int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
0319 sector_t last_block,
0320 unsigned long *mapped_blocks, sector_t *bmap);
0321 extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
0322 unsigned long *mapped_blocks, int create, bool from_bmap);
0323
0324
0325 extern const struct file_operations fat_dir_operations;
0326 extern int fat_search_long(struct inode *inode, const unsigned char *name,
0327 int name_len, struct fat_slot_info *sinfo);
0328 extern int fat_dir_empty(struct inode *dir);
0329 extern int fat_subdirs(struct inode *dir);
0330 extern int fat_scan(struct inode *dir, const unsigned char *name,
0331 struct fat_slot_info *sinfo);
0332 extern int fat_scan_logstart(struct inode *dir, int i_logstart,
0333 struct fat_slot_info *sinfo);
0334 extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
0335 struct msdos_dir_entry **de);
0336 extern int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts);
0337 extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
0338 struct fat_slot_info *sinfo);
0339 extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
0340
0341
0342 struct fat_entry {
0343 int entry;
0344 union {
0345 u8 *ent12_p[2];
0346 __le16 *ent16_p;
0347 __le32 *ent32_p;
0348 } u;
0349 int nr_bhs;
0350 struct buffer_head *bhs[2];
0351 struct inode *fat_inode;
0352 };
0353
0354 static inline void fatent_init(struct fat_entry *fatent)
0355 {
0356 fatent->nr_bhs = 0;
0357 fatent->entry = 0;
0358 fatent->u.ent32_p = NULL;
0359 fatent->bhs[0] = fatent->bhs[1] = NULL;
0360 fatent->fat_inode = NULL;
0361 }
0362
0363 static inline void fatent_set_entry(struct fat_entry *fatent, int entry)
0364 {
0365 fatent->entry = entry;
0366 fatent->u.ent32_p = NULL;
0367 }
0368
0369 static inline void fatent_brelse(struct fat_entry *fatent)
0370 {
0371 int i;
0372 fatent->u.ent32_p = NULL;
0373 for (i = 0; i < fatent->nr_bhs; i++)
0374 brelse(fatent->bhs[i]);
0375 fatent->nr_bhs = 0;
0376 fatent->bhs[0] = fatent->bhs[1] = NULL;
0377 fatent->fat_inode = NULL;
0378 }
0379
0380 static inline bool fat_valid_entry(struct msdos_sb_info *sbi, int entry)
0381 {
0382 return FAT_START_ENT <= entry && entry < sbi->max_cluster;
0383 }
0384
0385 extern void fat_ent_access_init(struct super_block *sb);
0386 extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
0387 int entry);
0388 extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
0389 int new, int wait);
0390 extern int fat_alloc_clusters(struct inode *inode, int *cluster,
0391 int nr_cluster);
0392 extern int fat_free_clusters(struct inode *inode, int cluster);
0393 extern int fat_count_free_clusters(struct super_block *sb);
0394 extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
0395
0396
0397 extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
0398 unsigned long arg);
0399 extern const struct file_operations fat_file_operations;
0400 extern const struct inode_operations fat_file_inode_operations;
0401 extern int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0402 struct iattr *attr);
0403 extern void fat_truncate_blocks(struct inode *inode, loff_t offset);
0404 extern int fat_getattr(struct user_namespace *mnt_userns,
0405 const struct path *path, struct kstat *stat,
0406 u32 request_mask, unsigned int flags);
0407 extern int fat_file_fsync(struct file *file, loff_t start, loff_t end,
0408 int datasync);
0409
0410
0411 extern int fat_block_truncate_page(struct inode *inode, loff_t from);
0412 extern void fat_attach(struct inode *inode, loff_t i_pos);
0413 extern void fat_detach(struct inode *inode);
0414 extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos);
0415 extern struct inode *fat_build_inode(struct super_block *sb,
0416 struct msdos_dir_entry *de, loff_t i_pos);
0417 extern int fat_sync_inode(struct inode *inode);
0418 extern int fat_fill_super(struct super_block *sb, void *data, int silent,
0419 int isvfat, void (*setup)(struct super_block *));
0420 extern int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
0421
0422 extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
0423 struct inode *i2);
0424 static inline unsigned long fat_dir_hash(int logstart)
0425 {
0426 return hash_32(logstart, FAT_HASH_BITS);
0427 }
0428 extern int fat_add_cluster(struct inode *inode);
0429
0430
0431 extern __printf(3, 4) __cold
0432 void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...);
0433 #define fat_fs_error(sb, fmt, args...) \
0434 __fat_fs_error(sb, 1, fmt , ## args)
0435 #define fat_fs_error_ratelimit(sb, fmt, args...) \
0436 __fat_fs_error(sb, __ratelimit(&MSDOS_SB(sb)->ratelimit), fmt , ## args)
0437
0438 #define FAT_PRINTK_PREFIX "%sFAT-fs (%s): "
0439 #define fat_msg(sb, level, fmt, args...) \
0440 do { \
0441 printk_index_subsys_emit(FAT_PRINTK_PREFIX, level, fmt, ##args);\
0442 _fat_msg(sb, level, fmt, ##args); \
0443 } while (0)
0444 __printf(3, 4) __cold
0445 void _fat_msg(struct super_block *sb, const char *level, const char *fmt, ...);
0446 #define fat_msg_ratelimit(sb, level, fmt, args...) \
0447 do { \
0448 if (__ratelimit(&MSDOS_SB(sb)->ratelimit)) \
0449 fat_msg(sb, level, fmt, ## args); \
0450 } while (0)
0451 extern int fat_clusters_flush(struct super_block *sb);
0452 extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
0453 extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
0454 __le16 __time, __le16 __date, u8 time_cs);
0455 extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
0456 __le16 *time, __le16 *date, u8 *time_cs);
0457 extern struct timespec64 fat_truncate_atime(const struct msdos_sb_info *sbi,
0458 const struct timespec64 *ts);
0459 extern struct timespec64 fat_truncate_mtime(const struct msdos_sb_info *sbi,
0460 const struct timespec64 *ts);
0461 extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
0462 int flags);
0463 extern int fat_update_time(struct inode *inode, struct timespec64 *now,
0464 int flags);
0465 extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
0466
0467 int fat_cache_init(void);
0468 void fat_cache_destroy(void);
0469
0470
0471 extern const struct export_operations fat_export_ops;
0472 extern const struct export_operations fat_export_ops_nostale;
0473
0474
0475 typedef unsigned long long llu;
0476
0477 #endif