Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/fs/fat/inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Written 1992,1993 by Werner Almesberger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  Rewritten for the constant inumbers support by Al Viro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  Fixes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *	Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/mpage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "fat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) /* if user don't select VFAT, this is undefined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define CONFIG_FAT_DEFAULT_IOCHARSET	""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define KB_IN_SECTORS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) /* DOS dates from 1980/1/1 through 2107/12/31 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define FAT_DATE_MIN (0<<9 | 1<<5 | 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define FAT_DATE_MAX (127<<9 | 12<<5 | 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define FAT_TIME_MAX (23<<11 | 59<<5 | 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * A deserialized copy of the on-disk structure laid out in struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * fat_boot_sector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) struct fat_bios_param_block {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	u16	fat_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	u8	fat_sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	u16	fat_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	u8	fat_fats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	u16	fat_dir_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	u16	fat_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	u16	fat_fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	u32	fat_total_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	u8	fat16_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	u32	fat16_vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	u32	fat32_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	u32	fat32_root_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	u16	fat32_info_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	u8	fat32_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	u32	fat32_vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static struct fat_floppy_defaults {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	unsigned nr_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	unsigned sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	unsigned dir_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	unsigned media;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	unsigned fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) } floppy_defaults[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	.nr_sectors = 160 * KB_IN_SECTORS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	.sec_per_clus = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	.dir_entries = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	.media = 0xFE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	.fat_length = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	.nr_sectors = 180 * KB_IN_SECTORS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	.sec_per_clus = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	.dir_entries = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	.media = 0xFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	.fat_length = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	.nr_sectors = 320 * KB_IN_SECTORS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	.sec_per_clus = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	.dir_entries = 112,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	.media = 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	.fat_length = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	.nr_sectors = 360 * KB_IN_SECTORS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	.sec_per_clus = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	.dir_entries = 112,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	.media = 0xFD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	.fat_length = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) int fat_add_cluster(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	int err, cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	err = fat_alloc_clusters(inode, &cluster, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	/* FIXME: this cluster should be added after data of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 * cluster is writed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	err = fat_chain_add(inode, cluster, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		fat_free_clusters(inode, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) static inline int __fat_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 				  unsigned long *max_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 				  struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	unsigned long mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	sector_t phys, last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	int err, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	if (phys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		*max_blocks = min(mapped_blocks, *max_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (!create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 			MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	 * allocate a cluster according to the following.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	 * 1) no more available blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	 * 2) not part of fallocate region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (!offset && !(iblock < last_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		/* TODO: multiple cluster allocation would be desirable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		err = fat_add_cluster(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	/* available blocks on this cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	mapped_blocks = sbi->sec_per_clus - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	*max_blocks = min(mapped_blocks, *max_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	if (!phys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		fat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			     "invalid FAT chain (i_pos %lld, last_block %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			     MSDOS_I(inode)->i_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			     (unsigned long long)last_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	BUG_ON(*max_blocks != mapped_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	set_buffer_new(bh_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) static int fat_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			 struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static int fat_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	return block_write_full_page(page, fat_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) static int fat_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			  struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	return mpage_writepages(mapping, wbc, fat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static int fat_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	return mpage_readpage(page, fat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static void fat_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	mpage_readahead(rac, fat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static void fat_write_failed(struct address_space *mapping, loff_t to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (to > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		truncate_pagecache(inode, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		fat_truncate_blocks(inode, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static int fat_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	*pagep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	err = cont_write_begin(file, mapping, pos, len, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 				pagep, fsdata, fat_get_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 				&MSDOS_I(mapping->host)->mmu_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		fat_write_failed(mapping, pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) static int fat_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			struct page *pagep, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (err < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		fat_write_failed(mapping, pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	struct address_space *mapping = file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	size_t count = iov_iter_count(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	loff_t offset = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (iov_iter_rw(iter) == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		 * so we need to update the ->mmu_private to block boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		 * But we must fill the remaining area or hole by nul for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		 * updating ->mmu_private.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		 * Return 0, and fallback to normal buffered write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		loff_t size = offset + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		if (MSDOS_I(inode)->mmu_private < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	 * FAT need to use the DIO_LOCKING for avoiding the race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	 * condition of fat_get_block() and ->truncate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (ret < 0 && iov_iter_rw(iter) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		fat_write_failed(mapping, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static int fat_get_block_bmap(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	sector_t bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	unsigned long mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	BUG_ON(create != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (bmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		map_bh(bh_result, sb, bmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		max_blocks = min(mapped_blocks, max_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	sector_t blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	/* fat_get_cluster() assumes the requested blocknr isn't truncated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	down_read(&MSDOS_I(mapping->host)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	up_read(&MSDOS_I(mapping->host)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * fat_block_truncate_page() zeroes out a mapping from file offset `from'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * up to the end of the block which corresponds to `from'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * This is required during truncate to physically zeroout the tail end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * of that block so it doesn't yield old data if the file is later grown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * Also, avoid causing failure from fsx for cases of "data past EOF"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) int fat_block_truncate_page(struct inode *inode, loff_t from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	return block_truncate_page(inode->i_mapping, from, fat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) static const struct address_space_operations fat_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	.readpage	= fat_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	.readahead	= fat_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	.writepage	= fat_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	.writepages	= fat_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	.write_begin	= fat_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	.write_end	= fat_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	.direct_IO	= fat_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	.bmap		= _fat_bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * New FAT inode stuff. We do the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  *	a) i_ino is constant and has nothing with on-disk location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  *	b) FAT manages its own cache of directory entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  *	c) *This* cache is indexed by on-disk location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  *	d) inode has an associated directory entry, all right, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  *		it may be unhashed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  *	e) currently entries are stored within struct inode. That should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  *		change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  *	f) we deal with races in the following way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  *		1. readdir() and lookup() do FAT-dir-cache lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366)  *		2. rename() unhashes the F-d-c entry and rehashes it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367)  *			a new place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  *		3. unlink() and rmdir() unhash F-d-c entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  *		4. fat_write_inode() checks whether the thing is unhashed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  *			If it is we silently return. If it isn't we do bread(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  *			check if the location is still valid and retry if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  *			isn't. Otherwise we do changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  *		5. Spinlock is used to protect hash/unhash/location check/lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  *		6. fat_evict_inode() unhashes the F-d-c entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  *		7. lookup() and readdir() do igrab() if they find a F-d-c entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  *			and consider negative result as cache miss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static void fat_hash_init(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	spin_lock_init(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	for (i = 0; i < FAT_HASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static inline unsigned long fat_hash(loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	return hash_32(i_pos, FAT_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) static void dir_hash_init(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	spin_lock_init(&sbi->dir_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	for (i = 0; i < FAT_HASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) void fat_attach(struct inode *inode, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (inode->i_ino != MSDOS_ROOT_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		struct hlist_head *head =   sbi->inode_hashtable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 					  + fat_hash(i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		MSDOS_I(inode)->i_pos = i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	/* If NFS support is enabled, cache the mapping of start cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	 * to directory inode. This is used during reconnection of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	 * dentries to the filesystem root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		struct hlist_head *d_head = sbi->dir_hashtable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		spin_lock(&sbi->dir_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		spin_unlock(&sbi->dir_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) EXPORT_SYMBOL_GPL(fat_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) void fat_detach(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	MSDOS_I(inode)->i_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		spin_lock(&sbi->dir_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		spin_unlock(&sbi->dir_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) EXPORT_SYMBOL_GPL(fat_detach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	struct msdos_inode_info *i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	hlist_for_each_entry(i, head, i_fat_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		BUG_ON(i->vfs_inode.i_sb != sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		if (i->i_pos != i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		inode = igrab(&i->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) static int is_exec(unsigned char *extension)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	unsigned char exe_extensions[] = "EXECOMBAT", *walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	for (walk = exe_extensions; *walk; walk += 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (!strncmp(extension, walk, 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static int fat_calc_dir_size(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	int ret, fclus, dclus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	if (MSDOS_I(inode)->i_start == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	inode->i_size = (fclus + 1) << sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static int fat_validate_dir(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (dir->i_nlink < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		/* Directory should have "."/".." entries at least. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		fat_fs_error(sb, "corrupted directory (invalid entries)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	if (MSDOS_I(dir)->i_start == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	    MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		/* Directory should point valid cluster. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		fat_fs_error(sb, "corrupted directory (invalid i_start)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) /* doesn't deal with root inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	MSDOS_I(inode)->i_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	inode->i_uid = sbi->options.fs_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	inode->i_gid = sbi->options.fs_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	inode->i_generation = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		inode->i_generation &= ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		inode->i_op = sbi->dir_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		inode->i_fop = &fat_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		error = fat_calc_dir_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		MSDOS_I(inode)->mmu_private = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		set_nlink(inode, fat_subdirs(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		error = fat_validate_dir(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	} else { /* not a directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		inode->i_generation |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		inode->i_mode = fat_make_mode(sbi, de->attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			((sbi->options.showexec && !is_exec(de->name + 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			 ? S_IRUGO|S_IWUGO : S_IRWXUGO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		inode->i_size = le32_to_cpu(de->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		inode->i_op = &fat_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		inode->i_fop = &fat_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		inode->i_mapping->a_ops = &fat_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		MSDOS_I(inode)->mmu_private = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (de->attr & ATTR_SYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		if (sbi->options.sys_immutable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			inode->i_flags |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	fat_save_attrs(inode, de->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	if (sbi->options.isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 				  de->cdate, de->ctime_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		fat_truncate_time(inode, &inode->i_mtime, S_ATIME|S_CTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		mutex_lock(&sbi->nfs_build_inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		mutex_unlock(&sbi->nfs_build_inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) struct inode *fat_build_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			struct msdos_dir_entry *de, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	fat_lock_build_inode(MSDOS_SB(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	inode = fat_iget(sb, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		inode = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	inode_set_iversion(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	err = fat_fill_inode(inode, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		inode = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	fat_attach(inode, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	fat_unlock_build_inode(MSDOS_SB(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) EXPORT_SYMBOL_GPL(fat_build_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static int __fat_write_inode(struct inode *inode, int wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) static void fat_free_eofblocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	/* Release unwritten fallocated blocks on inode eviction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if ((inode->i_blocks << 9) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			round_up(MSDOS_I(inode)->mmu_private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 				MSDOS_SB(inode->i_sb)->cluster_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		/* Fallocate results in updating the i_start/iogstart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		 * for the zero byte file. So, make it return to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		 * original state during evict and commit it to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		 * any corruption on the next access to the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		 * chain for the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		err = __fat_write_inode(inode, inode_needs_sync(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 					"update on disk inode for unused "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 					"fallocated blocks, inode could be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 					"corrupted. Please run fsck");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) static void fat_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (!inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		fat_truncate_blocks(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		fat_free_eofblocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	invalidate_inode_buffers(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	fat_cache_inval_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	fat_detach(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) static void fat_set_state(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			unsigned int set, unsigned int force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	struct fat_boot_sector *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	/* do not change any thing if mounted read only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	if (sb_rdonly(sb) && !force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	/* do not change state if fs was dirty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (sbi->dirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		/* warn only on set (mount). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		if (set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			fat_msg(sb, KERN_WARNING, "Volume was not properly "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				"unmounted. Some data may be corrupt. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				"Please run fsck.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	bh = sb_bread(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		fat_msg(sb, KERN_ERR, "unable to read boot sector "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			"to mark fs as dirty");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	b = (struct fat_boot_sector *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	if (is_fat32(sbi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		if (set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			b->fat32.state |= FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			b->fat32.state &= ~FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	} else /* fat 16 and 12 */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		if (set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			b->fat16.state |= FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			b->fat16.state &= ~FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) static void fat_reset_iocharset(struct fat_mount_options *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (opts->iocharset != fat_default_iocharset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		/* Note: opts->iocharset can be NULL here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		kfree(opts->iocharset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		opts->iocharset = fat_default_iocharset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) static void delayed_free(struct rcu_head *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	unload_nls(sbi->nls_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	unload_nls(sbi->nls_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	fat_reset_iocharset(&sbi->options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static void fat_put_super(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	fat_set_state(sb, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	iput(sbi->fsinfo_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	iput(sbi->fat_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	call_rcu(&sbi->rcu, delayed_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) static struct kmem_cache *fat_inode_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) static struct inode *fat_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	struct msdos_inode_info *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (!ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	init_rwsem(&ei->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	/* Zeroing to allow iput() even if partial initialized inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	ei->mmu_private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	ei->i_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	ei->i_logstart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	ei->i_attrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	ei->i_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	return &ei->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) static void fat_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static void init_once(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	spin_lock_init(&ei->cache_lru_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	ei->nr_caches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	ei->cache_valid_id = FAT_CACHE_VALID + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	INIT_LIST_HEAD(&ei->cache_lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	INIT_HLIST_NODE(&ei->i_fat_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	INIT_HLIST_NODE(&ei->i_dir_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	inode_init_once(&ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static int __init fat_init_inodecache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	fat_inode_cachep = kmem_cache_create("fat_inode_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 					     sizeof(struct msdos_inode_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 					     0, (SLAB_RECLAIM_ACCOUNT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 					     init_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (fat_inode_cachep == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static void __exit fat_destroy_inodecache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	 * Make sure all delayed rcu free inodes are flushed before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	 * destroy cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	kmem_cache_destroy(fat_inode_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) static int fat_remount(struct super_block *sb, int *flags, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	bool new_rdonly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	*flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	sync_filesystem(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	/* make sure we update state on remount. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	new_rdonly = *flags & SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (new_rdonly != sb_rdonly(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		if (new_rdonly)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			fat_set_state(sb, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			fat_set_state(sb, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	/* If the count of free cluster is still unknown, counts it here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		int err = fat_count_free_clusters(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	buf->f_type = dentry->d_sb->s_magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	buf->f_bsize = sbi->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	buf->f_bfree = sbi->free_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	buf->f_bavail = sbi->free_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	buf->f_fsid = u64_to_fsid(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	buf->f_namelen =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		(sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) static int __fat_write_inode(struct inode *inode, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct msdos_dir_entry *raw_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	loff_t i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	sector_t blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	int err, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (inode->i_ino == MSDOS_ROOT_INO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	i_pos = fat_i_pos_read(sbi, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (!i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	bh = sb_bread(sb, blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		fat_msg(sb, KERN_ERR, "unable to read inode block "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		       "for updating (i_pos %lld)", i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (i_pos != MSDOS_I(inode)->i_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		raw_entry->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		raw_entry->size = cpu_to_le32(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	raw_entry->attr = fat_make_attrs(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			  &raw_entry->date, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (sbi->options.isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		__le16 atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 				  &raw_entry->cdate, &raw_entry->ctime_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		fat_time_unix2fat(sbi, &inode->i_atime, &atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				  &raw_entry->adate, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	if (wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		err = sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (inode->i_ino == MSDOS_FSINFO_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		mutex_lock(&MSDOS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		err = fat_clusters_flush(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		mutex_unlock(&MSDOS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) int fat_sync_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	return __fat_write_inode(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) EXPORT_SYMBOL_GPL(fat_sync_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) static int fat_show_options(struct seq_file *m, struct dentry *root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) static const struct super_operations fat_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	.alloc_inode	= fat_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	.free_inode	= fat_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	.write_inode	= fat_write_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	.evict_inode	= fat_evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	.put_super	= fat_put_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	.statfs		= fat_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	.remount_fs	= fat_remount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	.show_options	= fat_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) static int fat_show_options(struct seq_file *m, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	struct fat_mount_options *opts = &sbi->options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	int isvfat = opts->isvfat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		seq_printf(m, ",uid=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				from_kuid_munged(&init_user_ns, opts->fs_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		seq_printf(m, ",gid=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				from_kgid_munged(&init_user_ns, opts->fs_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	seq_printf(m, ",fmask=%04o", opts->fs_fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	seq_printf(m, ",dmask=%04o", opts->fs_dmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (opts->allow_utime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	if (sbi->nls_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		/* strip "cp" prefix from displayed option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if (isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		if (sbi->nls_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		switch (opts->shortname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			seq_puts(m, ",shortname=win95");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			seq_puts(m, ",shortname=winnt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			seq_puts(m, ",shortname=mixed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			seq_puts(m, ",shortname=lower");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			seq_puts(m, ",shortname=unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (opts->name_check != 'n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		seq_printf(m, ",check=%c", opts->name_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (opts->usefree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		seq_puts(m, ",usefree");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (opts->quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		seq_puts(m, ",quiet");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (opts->showexec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		seq_puts(m, ",showexec");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	if (opts->sys_immutable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		seq_puts(m, ",sys_immutable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		if (opts->dotsOK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			seq_puts(m, ",dotsOK=yes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		if (opts->nocase)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			seq_puts(m, ",nocase");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		if (opts->utf8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			seq_puts(m, ",utf8");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		if (opts->unicode_xlate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			seq_puts(m, ",uni_xlate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		if (!opts->numtail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			seq_puts(m, ",nonumtail");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		if (opts->rodir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			seq_puts(m, ",rodir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (opts->flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		seq_puts(m, ",flush");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (opts->tz_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		if (opts->time_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			seq_printf(m, ",time_offset=%d", opts->time_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			seq_puts(m, ",tz=UTC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	if (opts->errors == FAT_ERRORS_CONT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		seq_puts(m, ",errors=continue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	else if (opts->errors == FAT_ERRORS_PANIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		seq_puts(m, ",errors=panic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		seq_puts(m, ",errors=remount-ro");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (opts->nfs == FAT_NFS_NOSTALE_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		seq_puts(m, ",nfs=nostale_ro");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	else if (opts->nfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		seq_puts(m, ",nfs=stale_rw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (opts->discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		seq_puts(m, ",discard");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (opts->dos1xfloppy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		seq_puts(m, ",dos1xfloppy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	Opt_immutable, Opt_dots, Opt_nodots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err, Opt_dos1xfloppy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static const match_table_t fat_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	{Opt_check_r, "check=relaxed"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	{Opt_check_s, "check=strict"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	{Opt_check_n, "check=normal"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	{Opt_check_r, "check=r"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	{Opt_check_s, "check=s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	{Opt_check_n, "check=n"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	{Opt_uid, "uid=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	{Opt_gid, "gid=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	{Opt_umask, "umask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	{Opt_dmask, "dmask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	{Opt_fmask, "fmask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	{Opt_allow_utime, "allow_utime=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	{Opt_codepage, "codepage=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	{Opt_usefree, "usefree"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	{Opt_nocase, "nocase"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	{Opt_quiet, "quiet"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	{Opt_showexec, "showexec"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	{Opt_debug, "debug"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	{Opt_immutable, "sys_immutable"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	{Opt_flush, "flush"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	{Opt_tz_utc, "tz=UTC"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	{Opt_time_offset, "time_offset=%d"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	{Opt_err_cont, "errors=continue"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	{Opt_err_panic, "errors=panic"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	{Opt_err_ro, "errors=remount-ro"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	{Opt_discard, "discard"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	{Opt_nfs_stale_rw, "nfs"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	{Opt_nfs_stale_rw, "nfs=stale_rw"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	{Opt_nfs_nostale_ro, "nfs=nostale_ro"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	{Opt_dos1xfloppy, "dos1xfloppy"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	{Opt_obsolete, "conv=binary"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	{Opt_obsolete, "conv=text"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	{Opt_obsolete, "conv=auto"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	{Opt_obsolete, "conv=b"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	{Opt_obsolete, "conv=t"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	{Opt_obsolete, "conv=a"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	{Opt_obsolete, "fat=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	{Opt_obsolete, "blocksize=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	{Opt_obsolete, "cvf_format=%20s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	{Opt_obsolete, "cvf_options=%100s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	{Opt_obsolete, "posix"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	{Opt_err, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) static const match_table_t msdos_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	{Opt_nodots, "nodots"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	{Opt_nodots, "dotsOK=no"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	{Opt_dots, "dots"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	{Opt_dots, "dotsOK=yes"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	{Opt_err, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) static const match_table_t vfat_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	{Opt_charset, "iocharset=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	{Opt_shortname_lower, "shortname=lower"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	{Opt_shortname_win95, "shortname=win95"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	{Opt_shortname_winnt, "shortname=winnt"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	{Opt_shortname_mixed, "shortname=mixed"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	{Opt_utf8_no, "utf8=0"},		/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	{Opt_utf8_no, "utf8=no"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	{Opt_utf8_no, "utf8=false"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	{Opt_utf8_yes, "utf8=1"},		/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	{Opt_utf8_yes, "utf8=yes"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	{Opt_utf8_yes, "utf8=true"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	{Opt_utf8_yes, "utf8"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	{Opt_uni_xl_no, "uni_xlate=0"},		/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	{Opt_uni_xl_no, "uni_xlate=no"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	{Opt_uni_xl_no, "uni_xlate=false"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	{Opt_uni_xl_yes, "uni_xlate=1"},	/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	{Opt_uni_xl_yes, "uni_xlate=yes"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	{Opt_uni_xl_yes, "uni_xlate=true"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	{Opt_uni_xl_yes, "uni_xlate"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	{Opt_nonumtail_no, "nonumtail=0"},	/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	{Opt_nonumtail_no, "nonumtail=no"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	{Opt_nonumtail_no, "nonumtail=false"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	{Opt_nonumtail_yes, "nonumtail=1"},	/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	{Opt_nonumtail_yes, "nonumtail=yes"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	{Opt_nonumtail_yes, "nonumtail=true"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	{Opt_nonumtail_yes, "nonumtail"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	{Opt_rodir, "rodir"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	{Opt_err, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) static int parse_options(struct super_block *sb, char *options, int is_vfat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			 int silent, int *debug, struct fat_mount_options *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	int option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	char *iocharset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	opts->isvfat = is_vfat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	opts->fs_uid = current_uid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	opts->fs_gid = current_gid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	opts->fs_fmask = opts->fs_dmask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	opts->allow_utime = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	opts->codepage = fat_default_codepage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	fat_reset_iocharset(opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (is_vfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		opts->rodir = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		opts->shortname = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		opts->rodir = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	opts->name_check = 'n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	opts->unicode_xlate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	opts->numtail = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	opts->usefree = opts->nocase = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	opts->tz_set = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	opts->nfs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	opts->errors = FAT_ERRORS_RO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	*debug = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (!options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	while ((p = strsep(&options, ",")) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		token = match_token(p, fat_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		if (token == Opt_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			if (is_vfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 				token = match_token(p, vfat_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				token = match_token(p, msdos_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		case Opt_check_s:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			opts->name_check = 's';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		case Opt_check_r:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			opts->name_check = 'r';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		case Opt_check_n:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			opts->name_check = 'n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		case Opt_usefree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			opts->usefree = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		case Opt_nocase:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			if (!is_vfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 				opts->nocase = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 				/* for backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 				opts->shortname = VFAT_SFN_DISPLAY_WIN95
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 					| VFAT_SFN_CREATE_WIN95;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		case Opt_quiet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			opts->quiet = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		case Opt_showexec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			opts->showexec = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		case Opt_debug:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			*debug = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		case Opt_immutable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			opts->sys_immutable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		case Opt_uid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			opts->fs_uid = make_kuid(current_user_ns(), option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			if (!uid_valid(opts->fs_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		case Opt_gid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			opts->fs_gid = make_kgid(current_user_ns(), option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			if (!gid_valid(opts->fs_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		case Opt_umask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			opts->fs_fmask = opts->fs_dmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		case Opt_dmask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			opts->fs_dmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		case Opt_fmask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			opts->fs_fmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		case Opt_allow_utime:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			opts->allow_utime = option & (S_IWGRP | S_IWOTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		case Opt_codepage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			opts->codepage = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		case Opt_flush:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			opts->flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		case Opt_time_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			 * GMT+-12 zones may have DST corrections so at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			 * 13 hours difference is needed. Make the limit 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			 * just in case someone invents something unusual.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			if (option < -24 * 60 || option > 24 * 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			opts->tz_set = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			opts->time_offset = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		case Opt_tz_utc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			opts->tz_set = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			opts->time_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		case Opt_err_cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			opts->errors = FAT_ERRORS_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		case Opt_err_panic:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			opts->errors = FAT_ERRORS_PANIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		case Opt_err_ro:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			opts->errors = FAT_ERRORS_RO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		case Opt_nfs_stale_rw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			opts->nfs = FAT_NFS_STALE_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		case Opt_nfs_nostale_ro:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			opts->nfs = FAT_NFS_NOSTALE_RO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		case Opt_dos1xfloppy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 			opts->dos1xfloppy = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		/* msdos specific */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		case Opt_dots:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			opts->dotsOK = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		case Opt_nodots:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			opts->dotsOK = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		/* vfat specific */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		case Opt_charset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 			fat_reset_iocharset(opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 			iocharset = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			if (!iocharset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			opts->iocharset = iocharset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		case Opt_shortname_lower:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			opts->shortname = VFAT_SFN_DISPLAY_LOWER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 					| VFAT_SFN_CREATE_WIN95;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		case Opt_shortname_win95:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			opts->shortname = VFAT_SFN_DISPLAY_WIN95
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 					| VFAT_SFN_CREATE_WIN95;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		case Opt_shortname_winnt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 					| VFAT_SFN_CREATE_WINNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		case Opt_shortname_mixed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 					| VFAT_SFN_CREATE_WIN95;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		case Opt_utf8_no:		/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			opts->utf8 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		case Opt_utf8_yes:		/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			opts->utf8 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		case Opt_uni_xl_no:		/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			opts->unicode_xlate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		case Opt_uni_xl_yes:		/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			opts->unicode_xlate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		case Opt_nonumtail_no:		/* 0 or no or false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			opts->numtail = 1;	/* negated option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		case Opt_nonumtail_yes:		/* empty or 1 or yes or true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			opts->numtail = 0;	/* negated option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		case Opt_rodir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			opts->rodir = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		case Opt_discard:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			opts->discard = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		/* obsolete mount options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		case Opt_obsolete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			       "not supported now", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		/* unknown option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			if (!silent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 				fat_msg(sb, KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 				       "Unrecognized mount option \"%s\" "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				       "or missing value", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	/* UTF-8 doesn't provide FAT semantics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	if (!strcmp(opts->iocharset, "utf8")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		       " for FAT filesystems, filesystem will be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		       "case sensitive!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	/* If user doesn't specify allow_utime, it's initialized from dmask. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	if (opts->allow_utime == (unsigned short)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	if (opts->unicode_xlate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		opts->utf8 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (opts->nfs == FAT_NFS_NOSTALE_RO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		sb->s_export_op = &fat_export_ops_nostale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static int fat_read_root(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	inode->i_uid = sbi->options.fs_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	inode->i_gid = sbi->options.fs_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	inode->i_generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	inode->i_op = sbi->dir_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	inode->i_fop = &fat_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	if (is_fat32(sbi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		MSDOS_I(inode)->i_start = sbi->root_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		error = fat_calc_dir_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		MSDOS_I(inode)->i_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	MSDOS_I(inode)->i_logstart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	MSDOS_I(inode)->mmu_private = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	fat_save_attrs(inode, ATTR_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	set_nlink(inode, fat_subdirs(inode)+2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) static unsigned long calc_fat_clusters(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	/* Divide first to avoid overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	if (!is_fat12(sbi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		return ent_per_sec * sbi->fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) static bool fat_bpb_is_zero(struct fat_boot_sector *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	if (get_unaligned_le16(&b->sector_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (b->sec_per_clus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	if (b->reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	if (b->fats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	if (get_unaligned_le16(&b->dir_entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (get_unaligned_le16(&b->sectors))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	if (b->media)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	if (b->fat_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (b->secs_track)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	if (b->heads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	int silent, struct fat_bios_param_block *bpb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	int error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	/* Read in BPB ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	memset(bpb, 0, sizeof(*bpb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	bpb->fat_sector_size = get_unaligned_le16(&b->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	bpb->fat_sec_per_clus = b->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	bpb->fat_reserved = le16_to_cpu(b->reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	bpb->fat_fats = b->fats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	bpb->fat_sectors = get_unaligned_le16(&b->sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	bpb->fat_fat_length = le16_to_cpu(b->fat_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	bpb->fat_total_sect = le32_to_cpu(b->total_sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	bpb->fat16_state = b->fat16.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	bpb->fat32_length = le32_to_cpu(b->fat32.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	bpb->fat32_state = b->fat32.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	/* Validate this looks like a FAT filesystem BPB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	if (!bpb->fat_reserved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 			fat_msg(sb, KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 				"bogus number of reserved sectors");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (!bpb->fat_fats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	 * Earlier we checked here that b->secs_track and b->head are nonzero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	 * but it turns out valid FAT filesystems can have zero there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (!fat_valid_media(b->media)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 			fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 				(unsigned)b->media);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	if (!is_power_of_2(bpb->fat_sector_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	    || (bpb->fat_sector_size < 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	    || (bpb->fat_sector_size > 4096)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			       (unsigned)bpb->fat_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (!is_power_of_2(bpb->fat_sec_per_clus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 			fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 				(unsigned)bpb->fat_sec_per_clus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 			fat_msg(sb, KERN_ERR, "bogus number of FAT sectors");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) static int fat_read_static_bpb(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct fat_boot_sector *b, int silent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	struct fat_bios_param_block *bpb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	static const char *notdos1x = "This doesn't look like a DOS 1.x volume";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	struct fat_floppy_defaults *fdefaults = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	int error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	sector_t bd_sects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	bd_sects = i_size_read(sb->s_bdev->bd_inode) / SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	/* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			fat_msg(sb, KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				"%s; no bootstrapping code", notdos1x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 * If any value in this region is non-zero, it isn't archaic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	 * DOS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	if (!fat_bpb_is_zero(b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 			fat_msg(sb, KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 				"%s; DOS 2.x BPB is non-zero", notdos1x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		if (floppy_defaults[i].nr_sectors == bd_sects) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			fdefaults = &floppy_defaults[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	if (fdefaults == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			fat_msg(sb, KERN_WARNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 				"This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 				(u64)bd_sects);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		fat_msg(sb, KERN_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 			"This looks like a DOS 1.x volume; assuming default BPB values");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	memset(bpb, 0, sizeof(*bpb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	bpb->fat_sector_size = SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	bpb->fat_sec_per_clus = fdefaults->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	bpb->fat_reserved = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	bpb->fat_fats = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	bpb->fat_dir_entries = fdefaults->dir_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	bpb->fat_sectors = fdefaults->nr_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	bpb->fat_fat_length = fdefaults->fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)  * Read the super block of an MS-DOS FS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		   void (*setup)(struct super_block *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	struct inode *root_inode = NULL, *fat_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	struct inode *fsinfo_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	struct fat_bios_param_block bpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	struct msdos_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	u16 logical_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	int debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	long error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	char buf[50];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	 * GFP_KERNEL is ok here, because while we do hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	 * superblock lock, memory pressure can't call back into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	 * the filesystem, since we're only just about to mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	 * it and have no inodes etc active!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (!sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	sb->s_fs_info = sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	sb->s_flags |= SB_NODIRATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	sb->s_magic = MSDOS_SUPER_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	sb->s_op = &fat_sops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	sb->s_export_op = &fat_export_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	 * fat timestamps are complex and truncated by fat itself, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	 * we set 1 here to be fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	sb->s_time_gran = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	mutex_init(&sbi->nfs_build_inode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			     DEFAULT_RATELIMIT_BURST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	setup(sb); /* flavour-specific stuff that needs options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	sb_min_blocksize(sb, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	bh = sb_bread(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		fat_msg(sb, KERN_ERR, "unable to read boot sector");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		&bpb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	if (error == -EINVAL && sbi->options.dos1xfloppy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		error = fat_read_static_bpb(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			(struct fat_boot_sector *)bh->b_data, silent, &bpb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	if (error == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		goto out_invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	else if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	logical_sector_size = bpb.fat_sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	sbi->sec_per_clus = bpb.fat_sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	if (logical_sector_size < sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		fat_msg(sb, KERN_ERR, "logical sector size too small for device"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		       " (logical sector size = %u)", logical_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	if (logical_sector_size > sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		struct buffer_head *bh_resize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (!sb_set_blocksize(sb, logical_sector_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			       logical_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		/* Verify that the larger boot sector is fully readable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		bh_resize = sb_bread(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		if (bh_resize == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 			fat_msg(sb, KERN_ERR, "unable to read boot sector"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 			       " (logical sector size = %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			       sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		brelse(bh_resize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	mutex_init(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	sbi->fats = bpb.fat_fats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	sbi->fat_bits = 0;		/* Don't know yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	sbi->fat_start = bpb.fat_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	sbi->fat_length = bpb.fat_fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	sbi->root_cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	sbi->free_clusters = -1;	/* Don't know yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	sbi->free_clus_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	sbi->prev_free = FAT_START_ENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	sb->s_maxbytes = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	sb->s_time_min = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 			  cpu_to_le16(FAT_DATE_MAX), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	sb->s_time_max = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	if (!sbi->fat_length && bpb.fat32_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		struct fat_boot_fsinfo *fsinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		struct buffer_head *fsinfo_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		/* Must be FAT32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		sbi->fat_bits = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		sbi->fat_length = bpb.fat32_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		sbi->root_cluster = bpb.fat32_root_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		/* MC - if info_sector is 0, don't multiply by 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		sbi->fsinfo_sector = bpb.fat32_info_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		if (sbi->fsinfo_sector == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			sbi->fsinfo_sector = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		if (fsinfo_bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			       " (sector = %lu)", sbi->fsinfo_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		if (!IS_FSINFO(fsinfo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			       "0x%08x, 0x%08x (sector = %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 			       le32_to_cpu(fsinfo->signature1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			       le32_to_cpu(fsinfo->signature2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			       sbi->fsinfo_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			if (sbi->options.usefree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 				sbi->free_clus_valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		brelse(fsinfo_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	/* interpret volume ID as a little endian 32 bit integer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	if (is_fat32(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		sbi->vol_id = bpb.fat32_vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	else /* fat 16 or 12 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		sbi->vol_id = bpb.fat16_vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	sbi->dir_entries = bpb.fat_dir_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			fat_msg(sb, KERN_ERR, "bogus number of directory entries"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 			       " (%u)", sbi->dir_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		goto out_invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	rootdir_sectors = sbi->dir_entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		* sizeof(struct msdos_dir_entry) / sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	sbi->data_start = sbi->dir_start + rootdir_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	total_sectors = bpb.fat_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	if (total_sectors == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		total_sectors = bpb.fat_total_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	if (!is_fat32(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	/* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (is_fat32(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	else /* fat 16 or 12 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	/* check that FAT table does not overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	fat_clusters = calc_fat_clusters(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	if (total_clusters > max_fat(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			       total_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		goto out_invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	sbi->max_cluster = total_clusters + FAT_START_ENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	/* check the free_clusters, it's not necessarily correct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		sbi->free_clusters = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	/* check the prev_free, it's not necessarily correct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	sbi->prev_free %= sbi->max_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (sbi->prev_free < FAT_START_ENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		sbi->prev_free = FAT_START_ENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	/* set up enough so that it can read an inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	fat_hash_init(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	dir_hash_init(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	fat_ent_access_init(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	 * The low byte of the first FAT entry must have the same value as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	 * the media field of the boot sector. But in real world, too many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	 * devices are writing wrong values. So, removed that validity check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	 * The removed check compared the first FAT entry to a value dependent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	 * on the media field like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	 * == (0x0F00 | media), for FAT12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	 * == (0XFF00 | media), for FAT16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	 * == (0x0FFFFF | media), for FAT32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	sprintf(buf, "cp%d", sbi->options.codepage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	sbi->nls_disk = load_nls(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	if (!sbi->nls_disk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	/* FIXME: utf8 is using iocharset for upper/lower conversion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	if (sbi->options.isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		sbi->nls_io = load_nls(sbi->options.iocharset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		if (!sbi->nls_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 			fat_msg(sb, KERN_ERR, "IO charset %s not found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 			       sbi->options.iocharset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 			goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	fat_inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	if (!fat_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	sbi->fat_inode = fat_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	fsinfo_inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	if (!fsinfo_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	sbi->fsinfo_inode = fsinfo_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	insert_inode_hash(fsinfo_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	root_inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	if (!root_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	root_inode->i_ino = MSDOS_ROOT_INO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	inode_set_iversion(root_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	error = fat_read_root(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		iput(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	insert_inode_hash(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	fat_attach(root_inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	sb->s_root = d_make_root(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	if (!sb->s_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		fat_msg(sb, KERN_ERR, "get root inode failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	if (sbi->options.discard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		if (!blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 			fat_msg(sb, KERN_WARNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 					"mounting with \"discard\" option, but "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 					"the device does not support discard");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	fat_set_state(sb, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) out_invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	if (fsinfo_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		iput(fsinfo_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	if (fat_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		iput(fat_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	unload_nls(sbi->nls_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	unload_nls(sbi->nls_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	fat_reset_iocharset(&sbi->options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	sb->s_fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) EXPORT_SYMBOL_GPL(fat_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)  * helper function for fat_flush_inodes.  This writes both the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)  * and the file data blocks, waiting for in flight data blocks before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  * the start of the call.  It does not wait for any io started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  * during the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) static int writeback_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	/* if we used wait=1, sync_inode_metadata waits for the io for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	* inode to finish.  So wait=0 is sent down to sync_inode_metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	* and filemap_fdatawrite is used for the data blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	ret = sync_inode_metadata(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		ret = filemap_fdatawrite(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  * write data and metadata corresponding to i1 and i2.  The io is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  * started but we do not wait for any of it to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)  * filemap_flush is used for the block device, so if there is a dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)  * page for a block already in flight, we will not wait and start the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  * io over again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	if (!MSDOS_SB(sb)->options.flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	if (i1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		ret = writeback_inode(i1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	if (!ret && i2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		ret = writeback_inode(i2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		ret = filemap_flush(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) EXPORT_SYMBOL_GPL(fat_flush_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) static int __init init_fat_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	err = fat_cache_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	err = fat_init_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	fat_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) static void __exit exit_fat_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	fat_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	fat_destroy_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) module_init(init_fat_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) module_exit(exit_fat_fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);