xref: /linux/fs/fat/inode.c (revision dcc381e8330e087402084647d048a168fb948e1c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/fat/inode.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Written 1992,1993 by Werner Almesberger
51da177e4SLinus Torvalds  *  VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
61da177e4SLinus Torvalds  *  Rewritten for the constant inumbers support by Al Viro
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Fixes:
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
15a5425d29SOGAWA Hirofumi #include <linux/mpage.h>
161da177e4SLinus Torvalds #include <linux/vfs.h>
1758932ef7SAlexander Kuleshov #include <linux/seq_file.h>
181da177e4SLinus Torvalds #include <linux/parser.h>
19e5174baaSOGAWA Hirofumi #include <linux/uio.h>
20f562146aSNamjae Jeon #include <linux/blkdev.h>
2166114cadSTejun Heo #include <linux/backing-dev.h>
221da177e4SLinus Torvalds #include <asm/unaligned.h>
239e975daeSOGAWA Hirofumi #include "fat.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
261da177e4SLinus Torvalds /* if user don't select VFAT, this is undefined. */
271da177e4SLinus Torvalds #define CONFIG_FAT_DEFAULT_IOCHARSET	""
281da177e4SLinus Torvalds #endif
291da177e4SLinus Torvalds 
30190a8843SConrad Meyer #define KB_IN_SECTORS 2
31190a8843SConrad Meyer 
32190a8843SConrad Meyer /*
33190a8843SConrad Meyer  * A deserialized copy of the on-disk structure laid out in struct
34190a8843SConrad Meyer  * fat_boot_sector.
35190a8843SConrad Meyer  */
36190a8843SConrad Meyer struct fat_bios_param_block {
37190a8843SConrad Meyer 	u16	fat_sector_size;
38190a8843SConrad Meyer 	u8	fat_sec_per_clus;
39190a8843SConrad Meyer 	u16	fat_reserved;
40190a8843SConrad Meyer 	u8	fat_fats;
41190a8843SConrad Meyer 	u16	fat_dir_entries;
42190a8843SConrad Meyer 	u16	fat_sectors;
43190a8843SConrad Meyer 	u16	fat_fat_length;
44190a8843SConrad Meyer 	u32	fat_total_sect;
45190a8843SConrad Meyer 
46190a8843SConrad Meyer 	u8	fat16_state;
47190a8843SConrad Meyer 	u32	fat16_vol_id;
48190a8843SConrad Meyer 
49190a8843SConrad Meyer 	u32	fat32_length;
50190a8843SConrad Meyer 	u32	fat32_root_cluster;
51190a8843SConrad Meyer 	u16	fat32_info_sector;
52190a8843SConrad Meyer 	u8	fat32_state;
53190a8843SConrad Meyer 	u32	fat32_vol_id;
54190a8843SConrad Meyer };
55190a8843SConrad Meyer 
561da177e4SLinus Torvalds static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
571da177e4SLinus Torvalds static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
581da177e4SLinus Torvalds 
59190a8843SConrad Meyer static struct fat_floppy_defaults {
60190a8843SConrad Meyer 	unsigned nr_sectors;
61190a8843SConrad Meyer 	unsigned sec_per_clus;
62190a8843SConrad Meyer 	unsigned dir_entries;
63190a8843SConrad Meyer 	unsigned media;
64190a8843SConrad Meyer 	unsigned fat_length;
65190a8843SConrad Meyer } floppy_defaults[] = {
66190a8843SConrad Meyer {
67190a8843SConrad Meyer 	.nr_sectors = 160 * KB_IN_SECTORS,
68190a8843SConrad Meyer 	.sec_per_clus = 1,
69190a8843SConrad Meyer 	.dir_entries = 64,
70190a8843SConrad Meyer 	.media = 0xFE,
71190a8843SConrad Meyer 	.fat_length = 1,
72190a8843SConrad Meyer },
73190a8843SConrad Meyer {
74190a8843SConrad Meyer 	.nr_sectors = 180 * KB_IN_SECTORS,
75190a8843SConrad Meyer 	.sec_per_clus = 1,
76190a8843SConrad Meyer 	.dir_entries = 64,
77190a8843SConrad Meyer 	.media = 0xFC,
78190a8843SConrad Meyer 	.fat_length = 2,
79190a8843SConrad Meyer },
80190a8843SConrad Meyer {
81190a8843SConrad Meyer 	.nr_sectors = 320 * KB_IN_SECTORS,
82190a8843SConrad Meyer 	.sec_per_clus = 2,
83190a8843SConrad Meyer 	.dir_entries = 112,
84190a8843SConrad Meyer 	.media = 0xFF,
85190a8843SConrad Meyer 	.fat_length = 1,
86190a8843SConrad Meyer },
87190a8843SConrad Meyer {
88190a8843SConrad Meyer 	.nr_sectors = 360 * KB_IN_SECTORS,
89190a8843SConrad Meyer 	.sec_per_clus = 2,
90190a8843SConrad Meyer 	.dir_entries = 112,
91190a8843SConrad Meyer 	.media = 0xFD,
92190a8843SConrad Meyer 	.fat_length = 2,
93190a8843SConrad Meyer },
94190a8843SConrad Meyer };
951da177e4SLinus Torvalds 
96b13bb33eSNamjae Jeon int fat_add_cluster(struct inode *inode)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	int err, cluster;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	err = fat_alloc_clusters(inode, &cluster, 1);
1011da177e4SLinus Torvalds 	if (err)
1021da177e4SLinus Torvalds 		return err;
1031da177e4SLinus Torvalds 	/* FIXME: this cluster should be added after data of this
1041da177e4SLinus Torvalds 	 * cluster is writed */
1051da177e4SLinus Torvalds 	err = fat_chain_add(inode, cluster, 1);
1061da177e4SLinus Torvalds 	if (err)
1071da177e4SLinus Torvalds 		fat_free_clusters(inode, cluster);
1081da177e4SLinus Torvalds 	return err;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1116a1d9805SOGAWA Hirofumi static inline int __fat_get_block(struct inode *inode, sector_t iblock,
112e5174baaSOGAWA Hirofumi 				  unsigned long *max_blocks,
1131da177e4SLinus Torvalds 				  struct buffer_head *bh_result, int create)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
116e5174baaSOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
117e5174baaSOGAWA Hirofumi 	unsigned long mapped_blocks;
1187e0f236bSNamjae Jeon 	sector_t phys, last_block;
119e5174baaSOGAWA Hirofumi 	int err, offset;
1201da177e4SLinus Torvalds 
12116fab201SNamjae Jeon 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
1221da177e4SLinus Torvalds 	if (err)
1231da177e4SLinus Torvalds 		return err;
1241da177e4SLinus Torvalds 	if (phys) {
1251da177e4SLinus Torvalds 		map_bh(bh_result, sb, phys);
126e5174baaSOGAWA Hirofumi 		*max_blocks = min(mapped_blocks, *max_blocks);
1271da177e4SLinus Torvalds 		return 0;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	if (!create)
1301da177e4SLinus Torvalds 		return 0;
131e5174baaSOGAWA Hirofumi 
1321da177e4SLinus Torvalds 	if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
13385c78591SDenis Karpov 		fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
1341da177e4SLinus Torvalds 			MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
1351da177e4SLinus Torvalds 		return -EIO;
1361da177e4SLinus Torvalds 	}
137e5174baaSOGAWA Hirofumi 
1387e0f236bSNamjae Jeon 	last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9);
139e5174baaSOGAWA Hirofumi 	offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
1407e0f236bSNamjae Jeon 	/*
1417e0f236bSNamjae Jeon 	 * allocate a cluster according to the following.
1427e0f236bSNamjae Jeon 	 * 1) no more available blocks
1437e0f236bSNamjae Jeon 	 * 2) not part of fallocate region
1447e0f236bSNamjae Jeon 	 */
1457e0f236bSNamjae Jeon 	if (!offset && !(iblock < last_block)) {
146e5174baaSOGAWA Hirofumi 		/* TODO: multiple cluster allocation would be desirable. */
1471da177e4SLinus Torvalds 		err = fat_add_cluster(inode);
1481da177e4SLinus Torvalds 		if (err)
1491da177e4SLinus Torvalds 			return err;
1501da177e4SLinus Torvalds 	}
151e5174baaSOGAWA Hirofumi 	/* available blocks on this cluster */
152e5174baaSOGAWA Hirofumi 	mapped_blocks = sbi->sec_per_clus - offset;
153e5174baaSOGAWA Hirofumi 
154e5174baaSOGAWA Hirofumi 	*max_blocks = min(mapped_blocks, *max_blocks);
155e5174baaSOGAWA Hirofumi 	MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
156e5174baaSOGAWA Hirofumi 
15716fab201SNamjae Jeon 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
1581da177e4SLinus Torvalds 	if (err)
1591da177e4SLinus Torvalds 		return err;
1606a1d9805SOGAWA Hirofumi 
161e5174baaSOGAWA Hirofumi 	BUG_ON(!phys);
162e5174baaSOGAWA Hirofumi 	BUG_ON(*max_blocks != mapped_blocks);
1631da177e4SLinus Torvalds 	set_buffer_new(bh_result);
1641da177e4SLinus Torvalds 	map_bh(bh_result, sb, phys);
1651da177e4SLinus Torvalds 
166e5174baaSOGAWA Hirofumi 	return 0;
167e5174baaSOGAWA Hirofumi }
168e5174baaSOGAWA Hirofumi 
169e5174baaSOGAWA Hirofumi static int fat_get_block(struct inode *inode, sector_t iblock,
170e5174baaSOGAWA Hirofumi 			 struct buffer_head *bh_result, int create)
171e5174baaSOGAWA Hirofumi {
1726a1d9805SOGAWA Hirofumi 	struct super_block *sb = inode->i_sb;
1736a1d9805SOGAWA Hirofumi 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1746a1d9805SOGAWA Hirofumi 	int err;
1756a1d9805SOGAWA Hirofumi 
1766a1d9805SOGAWA Hirofumi 	err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
1776a1d9805SOGAWA Hirofumi 	if (err)
1786a1d9805SOGAWA Hirofumi 		return err;
1796a1d9805SOGAWA Hirofumi 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1806a1d9805SOGAWA Hirofumi 	return 0;
181e5174baaSOGAWA Hirofumi }
182e5174baaSOGAWA Hirofumi 
1831da177e4SLinus Torvalds static int fat_writepage(struct page *page, struct writeback_control *wbc)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	return block_write_full_page(page, fat_get_block, wbc);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188a5425d29SOGAWA Hirofumi static int fat_writepages(struct address_space *mapping,
189a5425d29SOGAWA Hirofumi 			  struct writeback_control *wbc)
190a5425d29SOGAWA Hirofumi {
191a5425d29SOGAWA Hirofumi 	return mpage_writepages(mapping, wbc, fat_get_block);
192a5425d29SOGAWA Hirofumi }
193a5425d29SOGAWA Hirofumi 
1941da177e4SLinus Torvalds static int fat_readpage(struct file *file, struct page *page)
1951da177e4SLinus Torvalds {
196a5425d29SOGAWA Hirofumi 	return mpage_readpage(page, fat_get_block);
197a5425d29SOGAWA Hirofumi }
198a5425d29SOGAWA Hirofumi 
199a5425d29SOGAWA Hirofumi static int fat_readpages(struct file *file, struct address_space *mapping,
200a5425d29SOGAWA Hirofumi 			 struct list_head *pages, unsigned nr_pages)
201a5425d29SOGAWA Hirofumi {
202a5425d29SOGAWA Hirofumi 	return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
205459f6ed3Snpiggin@suse.de static void fat_write_failed(struct address_space *mapping, loff_t to)
206459f6ed3Snpiggin@suse.de {
207459f6ed3Snpiggin@suse.de 	struct inode *inode = mapping->host;
208459f6ed3Snpiggin@suse.de 
209459f6ed3Snpiggin@suse.de 	if (to > inode->i_size) {
2107caef267SKirill A. Shutemov 		truncate_pagecache(inode, inode->i_size);
211459f6ed3Snpiggin@suse.de 		fat_truncate_blocks(inode, inode->i_size);
212459f6ed3Snpiggin@suse.de 	}
213459f6ed3Snpiggin@suse.de }
214459f6ed3Snpiggin@suse.de 
215d7777a25SNick Piggin static int fat_write_begin(struct file *file, struct address_space *mapping,
216d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
217d7777a25SNick Piggin 			struct page **pagep, void **fsdata)
2181da177e4SLinus Torvalds {
219459f6ed3Snpiggin@suse.de 	int err;
220459f6ed3Snpiggin@suse.de 
221d7777a25SNick Piggin 	*pagep = NULL;
222282dc178SChristoph Hellwig 	err = cont_write_begin(file, mapping, pos, len, flags,
223459f6ed3Snpiggin@suse.de 				pagep, fsdata, fat_get_block,
224d7777a25SNick Piggin 				&MSDOS_I(mapping->host)->mmu_private);
225459f6ed3Snpiggin@suse.de 	if (err < 0)
226459f6ed3Snpiggin@suse.de 		fat_write_failed(mapping, pos + len);
227459f6ed3Snpiggin@suse.de 	return err;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230d7777a25SNick Piggin static int fat_write_end(struct file *file, struct address_space *mapping,
231d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
232d7777a25SNick Piggin 			struct page *pagep, void *fsdata)
233ef402268SOGAWA Hirofumi {
234d7777a25SNick Piggin 	struct inode *inode = mapping->host;
235d7777a25SNick Piggin 	int err;
236d7777a25SNick Piggin 	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
237459f6ed3Snpiggin@suse.de 	if (err < len)
238459f6ed3Snpiggin@suse.de 		fat_write_failed(mapping, pos + len);
239d7777a25SNick Piggin 	if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
240ef402268SOGAWA Hirofumi 		inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
241ef402268SOGAWA Hirofumi 		MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
242ef402268SOGAWA Hirofumi 		mark_inode_dirty(inode);
243ef402268SOGAWA Hirofumi 	}
244ef402268SOGAWA Hirofumi 	return err;
245ef402268SOGAWA Hirofumi }
246ef402268SOGAWA Hirofumi 
247c8b8e32dSChristoph Hellwig static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
248e5174baaSOGAWA Hirofumi {
249e5174baaSOGAWA Hirofumi 	struct file *file = iocb->ki_filp;
250459f6ed3Snpiggin@suse.de 	struct address_space *mapping = file->f_mapping;
251459f6ed3Snpiggin@suse.de 	struct inode *inode = mapping->host;
252a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
253c8b8e32dSChristoph Hellwig 	loff_t offset = iocb->ki_pos;
254459f6ed3Snpiggin@suse.de 	ssize_t ret;
255e5174baaSOGAWA Hirofumi 
2566f673763SOmar Sandoval 	if (iov_iter_rw(iter) == WRITE) {
257e5174baaSOGAWA Hirofumi 		/*
2584e02ed4bSNick Piggin 		 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
259e5174baaSOGAWA Hirofumi 		 * so we need to update the ->mmu_private to block boundary.
260e5174baaSOGAWA Hirofumi 		 *
261e5174baaSOGAWA Hirofumi 		 * But we must fill the remaining area or hole by nul for
262e5174baaSOGAWA Hirofumi 		 * updating ->mmu_private.
26394412a96SOGAWA Hirofumi 		 *
26494412a96SOGAWA Hirofumi 		 * Return 0, and fallback to normal buffered write.
265e5174baaSOGAWA Hirofumi 		 */
266a6cbcd4aSAl Viro 		loff_t size = offset + count;
267e5174baaSOGAWA Hirofumi 		if (MSDOS_I(inode)->mmu_private < size)
26894412a96SOGAWA Hirofumi 			return 0;
269e5174baaSOGAWA Hirofumi 	}
270e5174baaSOGAWA Hirofumi 
271e5174baaSOGAWA Hirofumi 	/*
272e5174baaSOGAWA Hirofumi 	 * FAT need to use the DIO_LOCKING for avoiding the race
273e5174baaSOGAWA Hirofumi 	 * condition of fat_get_block() and ->truncate().
274e5174baaSOGAWA Hirofumi 	 */
275c8b8e32dSChristoph Hellwig 	ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block);
2766f673763SOmar Sandoval 	if (ret < 0 && iov_iter_rw(iter) == WRITE)
277a6cbcd4aSAl Viro 		fat_write_failed(mapping, offset + count);
278459f6ed3Snpiggin@suse.de 
279459f6ed3Snpiggin@suse.de 	return ret;
280e5174baaSOGAWA Hirofumi }
281e5174baaSOGAWA Hirofumi 
28216fab201SNamjae Jeon static int fat_get_block_bmap(struct inode *inode, sector_t iblock,
28316fab201SNamjae Jeon 		struct buffer_head *bh_result, int create)
28416fab201SNamjae Jeon {
28516fab201SNamjae Jeon 	struct super_block *sb = inode->i_sb;
28616fab201SNamjae Jeon 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
28716fab201SNamjae Jeon 	int err;
28816fab201SNamjae Jeon 	sector_t bmap;
28916fab201SNamjae Jeon 	unsigned long mapped_blocks;
29016fab201SNamjae Jeon 
29116fab201SNamjae Jeon 	BUG_ON(create != 0);
29216fab201SNamjae Jeon 
29316fab201SNamjae Jeon 	err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true);
29416fab201SNamjae Jeon 	if (err)
29516fab201SNamjae Jeon 		return err;
29616fab201SNamjae Jeon 
29716fab201SNamjae Jeon 	if (bmap) {
29816fab201SNamjae Jeon 		map_bh(bh_result, sb, bmap);
29916fab201SNamjae Jeon 		max_blocks = min(mapped_blocks, max_blocks);
30016fab201SNamjae Jeon 	}
30116fab201SNamjae Jeon 
30216fab201SNamjae Jeon 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
30316fab201SNamjae Jeon 
30416fab201SNamjae Jeon 	return 0;
30516fab201SNamjae Jeon }
30616fab201SNamjae Jeon 
3071da177e4SLinus Torvalds static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
3081da177e4SLinus Torvalds {
309fa93ca18SOGAWA Hirofumi 	sector_t blocknr;
310fa93ca18SOGAWA Hirofumi 
311fa93ca18SOGAWA Hirofumi 	/* fat_get_cluster() assumes the requested blocknr isn't truncated. */
31258268691SChristoph Hellwig 	down_read(&MSDOS_I(mapping->host)->truncate_lock);
31316fab201SNamjae Jeon 	blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap);
31458268691SChristoph Hellwig 	up_read(&MSDOS_I(mapping->host)->truncate_lock);
315fa93ca18SOGAWA Hirofumi 
316fa93ca18SOGAWA Hirofumi 	return blocknr;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
319c0ef0cc9SNamjae Jeon /*
320c0ef0cc9SNamjae Jeon  * fat_block_truncate_page() zeroes out a mapping from file offset `from'
321c0ef0cc9SNamjae Jeon  * up to the end of the block which corresponds to `from'.
322c0ef0cc9SNamjae Jeon  * This is required during truncate to physically zeroout the tail end
323c0ef0cc9SNamjae Jeon  * of that block so it doesn't yield old data if the file is later grown.
324c0ef0cc9SNamjae Jeon  * Also, avoid causing failure from fsx for cases of "data past EOF"
325c0ef0cc9SNamjae Jeon  */
326c0ef0cc9SNamjae Jeon int fat_block_truncate_page(struct inode *inode, loff_t from)
327c0ef0cc9SNamjae Jeon {
328c0ef0cc9SNamjae Jeon 	return block_truncate_page(inode->i_mapping, from, fat_get_block);
329c0ef0cc9SNamjae Jeon }
330c0ef0cc9SNamjae Jeon 
331f5e54d6eSChristoph Hellwig static const struct address_space_operations fat_aops = {
3321da177e4SLinus Torvalds 	.readpage	= fat_readpage,
333a5425d29SOGAWA Hirofumi 	.readpages	= fat_readpages,
3341da177e4SLinus Torvalds 	.writepage	= fat_writepage,
335a5425d29SOGAWA Hirofumi 	.writepages	= fat_writepages,
336d7777a25SNick Piggin 	.write_begin	= fat_write_begin,
337d7777a25SNick Piggin 	.write_end	= fat_write_end,
338e5174baaSOGAWA Hirofumi 	.direct_IO	= fat_direct_IO,
3391da177e4SLinus Torvalds 	.bmap		= _fat_bmap
3401da177e4SLinus Torvalds };
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /*
3431da177e4SLinus Torvalds  * New FAT inode stuff. We do the following:
3441da177e4SLinus Torvalds  *	a) i_ino is constant and has nothing with on-disk location.
3451da177e4SLinus Torvalds  *	b) FAT manages its own cache of directory entries.
3461da177e4SLinus Torvalds  *	c) *This* cache is indexed by on-disk location.
3471da177e4SLinus Torvalds  *	d) inode has an associated directory entry, all right, but
3481da177e4SLinus Torvalds  *		it may be unhashed.
3491da177e4SLinus Torvalds  *	e) currently entries are stored within struct inode. That should
3501da177e4SLinus Torvalds  *		change.
3511da177e4SLinus Torvalds  *	f) we deal with races in the following way:
3521da177e4SLinus Torvalds  *		1. readdir() and lookup() do FAT-dir-cache lookup.
3531da177e4SLinus Torvalds  *		2. rename() unhashes the F-d-c entry and rehashes it in
3541da177e4SLinus Torvalds  *			a new place.
3551da177e4SLinus Torvalds  *		3. unlink() and rmdir() unhash F-d-c entry.
3561da177e4SLinus Torvalds  *		4. fat_write_inode() checks whether the thing is unhashed.
3571da177e4SLinus Torvalds  *			If it is we silently return. If it isn't we do bread(),
3581da177e4SLinus Torvalds  *			check if the location is still valid and retry if it
3591da177e4SLinus Torvalds  *			isn't. Otherwise we do changes.
3601da177e4SLinus Torvalds  *		5. Spinlock is used to protect hash/unhash/location check/lookup
361deee3ce4SAl Viro  *		6. fat_evict_inode() unhashes the F-d-c entry.
3621da177e4SLinus Torvalds  *		7. lookup() and readdir() do igrab() if they find a F-d-c entry
3631da177e4SLinus Torvalds  *			and consider negative result as cache miss.
3641da177e4SLinus Torvalds  */
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds static void fat_hash_init(struct super_block *sb)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
3691da177e4SLinus Torvalds 	int i;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	spin_lock_init(&sbi->inode_hash_lock);
3721da177e4SLinus Torvalds 	for (i = 0; i < FAT_HASH_SIZE; i++)
3731da177e4SLinus Torvalds 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376d3dfa822SOGAWA Hirofumi static inline unsigned long fat_hash(loff_t i_pos)
3771da177e4SLinus Torvalds {
378d3dfa822SOGAWA Hirofumi 	return hash_32(i_pos, FAT_HASH_BITS);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3817669e8fbSSteven J. Magnani static void dir_hash_init(struct super_block *sb)
3827669e8fbSSteven J. Magnani {
3837669e8fbSSteven J. Magnani 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
3847669e8fbSSteven J. Magnani 	int i;
3857669e8fbSSteven J. Magnani 
3867669e8fbSSteven J. Magnani 	spin_lock_init(&sbi->dir_hash_lock);
3877669e8fbSSteven J. Magnani 	for (i = 0; i < FAT_HASH_SIZE; i++)
3887669e8fbSSteven J. Magnani 		INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
3897669e8fbSSteven J. Magnani }
3907669e8fbSSteven J. Magnani 
3911da177e4SLinus Torvalds void fat_attach(struct inode *inode, loff_t i_pos)
3921da177e4SLinus Torvalds {
393d3dfa822SOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3947669e8fbSSteven J. Magnani 
3957669e8fbSSteven J. Magnani 	if (inode->i_ino != MSDOS_ROOT_INO) {
3967669e8fbSSteven J. Magnani 		struct hlist_head *head =   sbi->inode_hashtable
3977669e8fbSSteven J. Magnani 					  + fat_hash(i_pos);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		spin_lock(&sbi->inode_hash_lock);
4001da177e4SLinus Torvalds 		MSDOS_I(inode)->i_pos = i_pos;
401d3dfa822SOGAWA Hirofumi 		hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
4021da177e4SLinus Torvalds 		spin_unlock(&sbi->inode_hash_lock);
4031da177e4SLinus Torvalds 	}
4047669e8fbSSteven J. Magnani 
4057669e8fbSSteven J. Magnani 	/* If NFS support is enabled, cache the mapping of start cluster
4067669e8fbSSteven J. Magnani 	 * to directory inode. This is used during reconnection of
4077669e8fbSSteven J. Magnani 	 * dentries to the filesystem root.
4087669e8fbSSteven J. Magnani 	 */
4097669e8fbSSteven J. Magnani 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
4107669e8fbSSteven J. Magnani 		struct hlist_head *d_head = sbi->dir_hashtable;
4117669e8fbSSteven J. Magnani 		d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
4127669e8fbSSteven J. Magnani 
4137669e8fbSSteven J. Magnani 		spin_lock(&sbi->dir_hash_lock);
4147669e8fbSSteven J. Magnani 		hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
4157669e8fbSSteven J. Magnani 		spin_unlock(&sbi->dir_hash_lock);
4167669e8fbSSteven J. Magnani 	}
4177669e8fbSSteven J. Magnani }
4187c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_attach);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds void fat_detach(struct inode *inode)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
4231da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
4241da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
4251da177e4SLinus Torvalds 	hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
4261da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
4277669e8fbSSteven J. Magnani 
4287669e8fbSSteven J. Magnani 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
4297669e8fbSSteven J. Magnani 		spin_lock(&sbi->dir_hash_lock);
4307669e8fbSSteven J. Magnani 		hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
4317669e8fbSSteven J. Magnani 		spin_unlock(&sbi->dir_hash_lock);
4327669e8fbSSteven J. Magnani 	}
4331da177e4SLinus Torvalds }
4347c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_detach);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
439d3dfa822SOGAWA Hirofumi 	struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
4401da177e4SLinus Torvalds 	struct msdos_inode_info *i;
4411da177e4SLinus Torvalds 	struct inode *inode = NULL;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
444b67bfe0dSSasha Levin 	hlist_for_each_entry(i, head, i_fat_hash) {
4451da177e4SLinus Torvalds 		BUG_ON(i->vfs_inode.i_sb != sb);
4461da177e4SLinus Torvalds 		if (i->i_pos != i_pos)
4471da177e4SLinus Torvalds 			continue;
4481da177e4SLinus Torvalds 		inode = igrab(&i->vfs_inode);
4491da177e4SLinus Torvalds 		if (inode)
4501da177e4SLinus Torvalds 			break;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
4531da177e4SLinus Torvalds 	return inode;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static int is_exec(unsigned char *extension)
4571da177e4SLinus Torvalds {
458ef19470eSManuel Schölling 	unsigned char exe_extensions[] = "EXECOMBAT", *walk;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	for (walk = exe_extensions; *walk; walk += 3)
4611da177e4SLinus Torvalds 		if (!strncmp(extension, walk, 3))
4621da177e4SLinus Torvalds 			return 1;
4631da177e4SLinus Torvalds 	return 0;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds static int fat_calc_dir_size(struct inode *inode)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
4691da177e4SLinus Torvalds 	int ret, fclus, dclus;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	inode->i_size = 0;
4721da177e4SLinus Torvalds 	if (MSDOS_I(inode)->i_start == 0)
4731da177e4SLinus Torvalds 		return 0;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
4761da177e4SLinus Torvalds 	if (ret < 0)
4771da177e4SLinus Torvalds 		return ret;
4781da177e4SLinus Torvalds 	inode->i_size = (fclus + 1) << sbi->cluster_bits;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	return 0;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
483a3082d52SOGAWA Hirofumi static int fat_validate_dir(struct inode *dir)
484a3082d52SOGAWA Hirofumi {
485a3082d52SOGAWA Hirofumi 	struct super_block *sb = dir->i_sb;
486a3082d52SOGAWA Hirofumi 
487a3082d52SOGAWA Hirofumi 	if (dir->i_nlink < 2) {
488a3082d52SOGAWA Hirofumi 		/* Directory should have "."/".." entries at least. */
489a3082d52SOGAWA Hirofumi 		fat_fs_error(sb, "corrupted directory (invalid entries)");
490a3082d52SOGAWA Hirofumi 		return -EIO;
491a3082d52SOGAWA Hirofumi 	}
492a3082d52SOGAWA Hirofumi 	if (MSDOS_I(dir)->i_start == 0 ||
493a3082d52SOGAWA Hirofumi 	    MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) {
494a3082d52SOGAWA Hirofumi 		/* Directory should point valid cluster. */
495a3082d52SOGAWA Hirofumi 		fat_fs_error(sb, "corrupted directory (invalid i_start)");
496a3082d52SOGAWA Hirofumi 		return -EIO;
497a3082d52SOGAWA Hirofumi 	}
498a3082d52SOGAWA Hirofumi 	return 0;
499a3082d52SOGAWA Hirofumi }
500a3082d52SOGAWA Hirofumi 
5011da177e4SLinus Torvalds /* doesn't deal with root inode */
502f1e6fb0aSNamjae Jeon int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
5051da177e4SLinus Torvalds 	int error;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
5081da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
5091da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
5101da177e4SLinus Torvalds 	inode->i_version++;
5111da177e4SLinus Torvalds 	inode->i_generation = get_seconds();
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
5141da177e4SLinus Torvalds 		inode->i_generation &= ~1;
5159c0aa1b8SOGAWA Hirofumi 		inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
5161da177e4SLinus Torvalds 		inode->i_op = sbi->dir_ops;
5171da177e4SLinus Torvalds 		inode->i_fop = &fat_dir_operations;
5181da177e4SLinus Torvalds 
519a943ed71SSteven J. Magnani 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
5201da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
5211da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
5221da177e4SLinus Torvalds 		if (error < 0)
5231da177e4SLinus Torvalds 			return error;
5241da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
5251da177e4SLinus Torvalds 
526bfe86848SMiklos Szeredi 		set_nlink(inode, fat_subdirs(inode));
527a3082d52SOGAWA Hirofumi 
528a3082d52SOGAWA Hirofumi 		error = fat_validate_dir(inode);
529a3082d52SOGAWA Hirofumi 		if (error < 0)
530a3082d52SOGAWA Hirofumi 			return error;
5311da177e4SLinus Torvalds 	} else { /* not a directory */
5321da177e4SLinus Torvalds 		inode->i_generation |= 1;
5339c0aa1b8SOGAWA Hirofumi 		inode->i_mode = fat_make_mode(sbi, de->attr,
5349aacd599SOGAWA Hirofumi 			((sbi->options.showexec && !is_exec(de->name + 8))
5359c0aa1b8SOGAWA Hirofumi 			 ? S_IRUGO|S_IWUGO : S_IRWXUGO));
536a943ed71SSteven J. Magnani 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
5391da177e4SLinus Torvalds 		inode->i_size = le32_to_cpu(de->size);
5401da177e4SLinus Torvalds 		inode->i_op = &fat_file_inode_operations;
5411da177e4SLinus Torvalds 		inode->i_fop = &fat_file_operations;
5421da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &fat_aops;
5431da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 	if (de->attr & ATTR_SYS) {
5461da177e4SLinus Torvalds 		if (sbi->options.sys_immutable)
5471da177e4SLinus Torvalds 			inode->i_flags |= S_IMMUTABLE;
5481da177e4SLinus Torvalds 	}
5499c0aa1b8SOGAWA Hirofumi 	fat_save_attrs(inode, de->attr);
5509c0aa1b8SOGAWA Hirofumi 
5511da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
5521da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
5537decd1cbSOGAWA Hirofumi 
5547decd1cbSOGAWA Hirofumi 	fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
5551da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
5567decd1cbSOGAWA Hirofumi 		fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
5577decd1cbSOGAWA Hirofumi 				  de->cdate, de->ctime_cs);
5587decd1cbSOGAWA Hirofumi 		fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
5591da177e4SLinus Torvalds 	} else
56062a36c43SStephane Kardas 		inode->i_ctime = inode->i_atime = inode->i_mtime;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	return 0;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5658fceb4e0SNamjae Jeon static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
5668fceb4e0SNamjae Jeon {
5678fceb4e0SNamjae Jeon 	if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
5688fceb4e0SNamjae Jeon 		mutex_lock(&sbi->nfs_build_inode_lock);
5698fceb4e0SNamjae Jeon }
5708fceb4e0SNamjae Jeon 
5718fceb4e0SNamjae Jeon static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
5728fceb4e0SNamjae Jeon {
5738fceb4e0SNamjae Jeon 	if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
5748fceb4e0SNamjae Jeon 		mutex_unlock(&sbi->nfs_build_inode_lock);
5758fceb4e0SNamjae Jeon }
5768fceb4e0SNamjae Jeon 
5771da177e4SLinus Torvalds struct inode *fat_build_inode(struct super_block *sb,
5781da177e4SLinus Torvalds 			struct msdos_dir_entry *de, loff_t i_pos)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	struct inode *inode;
5811da177e4SLinus Torvalds 	int err;
5821da177e4SLinus Torvalds 
5838fceb4e0SNamjae Jeon 	fat_lock_build_inode(MSDOS_SB(sb));
5841da177e4SLinus Torvalds 	inode = fat_iget(sb, i_pos);
5851da177e4SLinus Torvalds 	if (inode)
5861da177e4SLinus Torvalds 		goto out;
5871da177e4SLinus Torvalds 	inode = new_inode(sb);
5881da177e4SLinus Torvalds 	if (!inode) {
5891da177e4SLinus Torvalds 		inode = ERR_PTR(-ENOMEM);
5901da177e4SLinus Torvalds 		goto out;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
5931da177e4SLinus Torvalds 	inode->i_version = 1;
5941da177e4SLinus Torvalds 	err = fat_fill_inode(inode, de);
5951da177e4SLinus Torvalds 	if (err) {
5961da177e4SLinus Torvalds 		iput(inode);
5971da177e4SLinus Torvalds 		inode = ERR_PTR(err);
5981da177e4SLinus Torvalds 		goto out;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 	fat_attach(inode, i_pos);
6011da177e4SLinus Torvalds 	insert_inode_hash(inode);
6021da177e4SLinus Torvalds out:
6038fceb4e0SNamjae Jeon 	fat_unlock_build_inode(MSDOS_SB(sb));
6041da177e4SLinus Torvalds 	return inode;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6077c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_build_inode);
6081da177e4SLinus Torvalds 
609b13bb33eSNamjae Jeon static int __fat_write_inode(struct inode *inode, int wait);
610b13bb33eSNamjae Jeon 
611b13bb33eSNamjae Jeon static void fat_free_eofblocks(struct inode *inode)
612b13bb33eSNamjae Jeon {
613b13bb33eSNamjae Jeon 	/* Release unwritten fallocated blocks on inode eviction. */
614b13bb33eSNamjae Jeon 	if ((inode->i_blocks << 9) >
615b13bb33eSNamjae Jeon 			round_up(MSDOS_I(inode)->mmu_private,
616b13bb33eSNamjae Jeon 				MSDOS_SB(inode->i_sb)->cluster_size)) {
617b13bb33eSNamjae Jeon 		int err;
618b13bb33eSNamjae Jeon 
619b13bb33eSNamjae Jeon 		fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
620b13bb33eSNamjae Jeon 		/* Fallocate results in updating the i_start/iogstart
621b13bb33eSNamjae Jeon 		 * for the zero byte file. So, make it return to
622b13bb33eSNamjae Jeon 		 * original state during evict and commit it to avoid
623b13bb33eSNamjae Jeon 		 * any corruption on the next access to the cluster
624b13bb33eSNamjae Jeon 		 * chain for the file.
625b13bb33eSNamjae Jeon 		 */
626b13bb33eSNamjae Jeon 		err = __fat_write_inode(inode, inode_needs_sync(inode));
627b13bb33eSNamjae Jeon 		if (err) {
628b13bb33eSNamjae Jeon 			fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
629b13bb33eSNamjae Jeon 					"update on disk inode for unused "
630b13bb33eSNamjae Jeon 					"fallocated blocks, inode could be "
631b13bb33eSNamjae Jeon 					"corrupted. Please run fsck");
632b13bb33eSNamjae Jeon 		}
633b13bb33eSNamjae Jeon 
634b13bb33eSNamjae Jeon 	}
635b13bb33eSNamjae Jeon }
636b13bb33eSNamjae Jeon 
637deee3ce4SAl Viro static void fat_evict_inode(struct inode *inode)
6381da177e4SLinus Torvalds {
63991b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
640deee3ce4SAl Viro 	if (!inode->i_nlink) {
6411da177e4SLinus Torvalds 		inode->i_size = 0;
642459f6ed3Snpiggin@suse.de 		fat_truncate_blocks(inode, 0);
643b13bb33eSNamjae Jeon 	} else
644b13bb33eSNamjae Jeon 		fat_free_eofblocks(inode);
645b13bb33eSNamjae Jeon 
646deee3ce4SAl Viro 	invalidate_inode_buffers(inode);
647dbd5768fSJan Kara 	clear_inode(inode);
6481da177e4SLinus Torvalds 	fat_cache_inval_inode(inode);
649a993b542SOGAWA Hirofumi 	fat_detach(inode);
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
652b88a1058SOleksij Rempel static void fat_set_state(struct super_block *sb,
653b88a1058SOleksij Rempel 			unsigned int set, unsigned int force)
654b88a1058SOleksij Rempel {
655b88a1058SOleksij Rempel 	struct buffer_head *bh;
656b88a1058SOleksij Rempel 	struct fat_boot_sector *b;
657d6bd4282SFred Chou 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
658b88a1058SOleksij Rempel 
659b88a1058SOleksij Rempel 	/* do not change any thing if mounted read only */
660b88a1058SOleksij Rempel 	if ((sb->s_flags & MS_RDONLY) && !force)
661b88a1058SOleksij Rempel 		return;
662b88a1058SOleksij Rempel 
663b88a1058SOleksij Rempel 	/* do not change state if fs was dirty */
664b88a1058SOleksij Rempel 	if (sbi->dirty) {
665b88a1058SOleksij Rempel 		/* warn only on set (mount). */
666b88a1058SOleksij Rempel 		if (set)
667b88a1058SOleksij Rempel 			fat_msg(sb, KERN_WARNING, "Volume was not properly "
668b88a1058SOleksij Rempel 				"unmounted. Some data may be corrupt. "
669b88a1058SOleksij Rempel 				"Please run fsck.");
670b88a1058SOleksij Rempel 		return;
671b88a1058SOleksij Rempel 	}
672b88a1058SOleksij Rempel 
673b88a1058SOleksij Rempel 	bh = sb_bread(sb, 0);
674b88a1058SOleksij Rempel 	if (bh == NULL) {
675b88a1058SOleksij Rempel 		fat_msg(sb, KERN_ERR, "unable to read boot sector "
676b88a1058SOleksij Rempel 			"to mark fs as dirty");
677b88a1058SOleksij Rempel 		return;
678b88a1058SOleksij Rempel 	}
679b88a1058SOleksij Rempel 
680b88a1058SOleksij Rempel 	b = (struct fat_boot_sector *) bh->b_data;
681b88a1058SOleksij Rempel 
682b88a1058SOleksij Rempel 	if (sbi->fat_bits == 32) {
683b88a1058SOleksij Rempel 		if (set)
684b88a1058SOleksij Rempel 			b->fat32.state |= FAT_STATE_DIRTY;
685b88a1058SOleksij Rempel 		else
686b88a1058SOleksij Rempel 			b->fat32.state &= ~FAT_STATE_DIRTY;
687b88a1058SOleksij Rempel 	} else /* fat 16 and 12 */ {
688b88a1058SOleksij Rempel 		if (set)
689b88a1058SOleksij Rempel 			b->fat16.state |= FAT_STATE_DIRTY;
690b88a1058SOleksij Rempel 		else
691b88a1058SOleksij Rempel 			b->fat16.state &= ~FAT_STATE_DIRTY;
692b88a1058SOleksij Rempel 	}
693b88a1058SOleksij Rempel 
694b88a1058SOleksij Rempel 	mark_buffer_dirty(bh);
695b88a1058SOleksij Rempel 	sync_dirty_buffer(bh);
696b88a1058SOleksij Rempel 	brelse(bh);
697b88a1058SOleksij Rempel }
698b88a1058SOleksij Rempel 
699cac45b06SAl Viro static void delayed_free(struct rcu_head *p)
700cac45b06SAl Viro {
701cac45b06SAl Viro 	struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
702cac45b06SAl Viro 	unload_nls(sbi->nls_disk);
703cac45b06SAl Viro 	unload_nls(sbi->nls_io);
704cac45b06SAl Viro 	if (sbi->options.iocharset != fat_default_iocharset)
705cac45b06SAl Viro 		kfree(sbi->options.iocharset);
706cac45b06SAl Viro 	kfree(sbi);
707cac45b06SAl Viro }
708cac45b06SAl Viro 
709a6bf6b21SOGAWA Hirofumi static void fat_put_super(struct super_block *sb)
710a6bf6b21SOGAWA Hirofumi {
711a6bf6b21SOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
7121da177e4SLinus Torvalds 
713b88a1058SOleksij Rempel 	fat_set_state(sb, 0, 0);
714b88a1058SOleksij Rempel 
715020ac5b6SArtem Bityutskiy 	iput(sbi->fsinfo_inode);
716b522412aSAl Viro 	iput(sbi->fat_inode);
717b522412aSAl Viro 
718cac45b06SAl Viro 	call_rcu(&sbi->rcu, delayed_free);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721e18b890bSChristoph Lameter static struct kmem_cache *fat_inode_cachep;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds static struct inode *fat_alloc_inode(struct super_block *sb)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	struct msdos_inode_info *ei;
7268f593427SLinus Torvalds 	ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
7271da177e4SLinus Torvalds 	if (!ei)
7281da177e4SLinus Torvalds 		return NULL;
72958268691SChristoph Hellwig 
73058268691SChristoph Hellwig 	init_rwsem(&ei->truncate_lock);
7311da177e4SLinus Torvalds 	return &ei->vfs_inode;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734fa0d7e3dSNick Piggin static void fat_i_callback(struct rcu_head *head)
735fa0d7e3dSNick Piggin {
736fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
737fa0d7e3dSNick Piggin 	kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
738fa0d7e3dSNick Piggin }
739fa0d7e3dSNick Piggin 
7401da177e4SLinus Torvalds static void fat_destroy_inode(struct inode *inode)
7411da177e4SLinus Torvalds {
742fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, fat_i_callback);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
74551cc5068SAlexey Dobriyan static void init_once(void *foo)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	spin_lock_init(&ei->cache_lru_lock);
7501da177e4SLinus Torvalds 	ei->nr_caches = 0;
7511da177e4SLinus Torvalds 	ei->cache_valid_id = FAT_CACHE_VALID + 1;
7521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&ei->cache_lru);
7531da177e4SLinus Torvalds 	INIT_HLIST_NODE(&ei->i_fat_hash);
7547669e8fbSSteven J. Magnani 	INIT_HLIST_NODE(&ei->i_dir_hash);
7551da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds static int __init fat_init_inodecache(void)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	fat_inode_cachep = kmem_cache_create("fat_inode_cache",
7611da177e4SLinus Torvalds 					     sizeof(struct msdos_inode_info),
762fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
7635d097056SVladimir Davydov 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
76420c2df83SPaul Mundt 					     init_once);
7651da177e4SLinus Torvalds 	if (fat_inode_cachep == NULL)
7661da177e4SLinus Torvalds 		return -ENOMEM;
7671da177e4SLinus Torvalds 	return 0;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static void __exit fat_destroy_inodecache(void)
7711da177e4SLinus Torvalds {
7728c0a8537SKirill A. Shutemov 	/*
7738c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
7748c0a8537SKirill A. Shutemov 	 * destroy cache.
7758c0a8537SKirill A. Shutemov 	 */
7768c0a8537SKirill A. Shutemov 	rcu_barrier();
7771a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(fat_inode_cachep);
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds static int fat_remount(struct super_block *sb, int *flags, char *data)
7811da177e4SLinus Torvalds {
782b88a1058SOleksij Rempel 	int new_rdonly;
7831da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
7841da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
785b88a1058SOleksij Rempel 
78602b9984dSTheodore Ts'o 	sync_filesystem(sb);
78702b9984dSTheodore Ts'o 
788b88a1058SOleksij Rempel 	/* make sure we update state on remount. */
789b88a1058SOleksij Rempel 	new_rdonly = *flags & MS_RDONLY;
790b88a1058SOleksij Rempel 	if (new_rdonly != (sb->s_flags & MS_RDONLY)) {
791b88a1058SOleksij Rempel 		if (new_rdonly)
792b88a1058SOleksij Rempel 			fat_set_state(sb, 0, 0);
793b88a1058SOleksij Rempel 		else
794b88a1058SOleksij Rempel 			fat_set_state(sb, 1, 1);
795b88a1058SOleksij Rempel 	}
7961da177e4SLinus Torvalds 	return 0;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799726c3342SDavid Howells static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
8001da177e4SLinus Torvalds {
801aac49b75SColy Li 	struct super_block *sb = dentry->d_sb;
802aac49b75SColy Li 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
803aac49b75SColy Li 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	/* If the count of free cluster is still unknown, counts it here. */
806606e423eSOGAWA Hirofumi 	if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
807726c3342SDavid Howells 		int err = fat_count_free_clusters(dentry->d_sb);
8081da177e4SLinus Torvalds 		if (err)
8091da177e4SLinus Torvalds 			return err;
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 
812726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
8131da177e4SLinus Torvalds 	buf->f_bsize = sbi->cluster_size;
8141da177e4SLinus Torvalds 	buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
8151da177e4SLinus Torvalds 	buf->f_bfree = sbi->free_clusters;
8161da177e4SLinus Torvalds 	buf->f_bavail = sbi->free_clusters;
817aac49b75SColy Li 	buf->f_fsid.val[0] = (u32)id;
818aac49b75SColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
819f68e542fSOGAWA Hirofumi 	buf->f_namelen =
820f68e542fSOGAWA Hirofumi 		(sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	return 0;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
825a9185b41SChristoph Hellwig static int __fat_write_inode(struct inode *inode, int wait)
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
8281da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
8291da177e4SLinus Torvalds 	struct buffer_head *bh;
8301da177e4SLinus Torvalds 	struct msdos_dir_entry *raw_entry;
8311da177e4SLinus Torvalds 	loff_t i_pos;
832e22a4442SNamjae Jeon 	sector_t blocknr;
833e22a4442SNamjae Jeon 	int err, offset;
8341da177e4SLinus Torvalds 
8359ca59f4cSOGAWA Hirofumi 	if (inode->i_ino == MSDOS_ROOT_INO)
8369ca59f4cSOGAWA Hirofumi 		return 0;
8379ca59f4cSOGAWA Hirofumi 
8381da177e4SLinus Torvalds retry:
8399ca59f4cSOGAWA Hirofumi 	i_pos = fat_i_pos_read(sbi, inode);
8409ca59f4cSOGAWA Hirofumi 	if (!i_pos)
8411da177e4SLinus Torvalds 		return 0;
8421da177e4SLinus Torvalds 
843e22a4442SNamjae Jeon 	fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
844e22a4442SNamjae Jeon 	bh = sb_bread(sb, blocknr);
8451da177e4SLinus Torvalds 	if (!bh) {
846869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "unable to read inode block "
847869f58c0SAlexey Fisher 		       "for updating (i_pos %lld)", i_pos);
8485f22ca9bSLinus Torvalds 		return -EIO;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
8511da177e4SLinus Torvalds 	if (i_pos != MSDOS_I(inode)->i_pos) {
8521da177e4SLinus Torvalds 		spin_unlock(&sbi->inode_hash_lock);
8531da177e4SLinus Torvalds 		brelse(bh);
8541da177e4SLinus Torvalds 		goto retry;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 
857e22a4442SNamjae Jeon 	raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
8581da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode))
8591da177e4SLinus Torvalds 		raw_entry->size = 0;
8601da177e4SLinus Torvalds 	else
8611da177e4SLinus Torvalds 		raw_entry->size = cpu_to_le32(inode->i_size);
8629c0aa1b8SOGAWA Hirofumi 	raw_entry->attr = fat_make_attrs(inode);
863a943ed71SSteven J. Magnani 	fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
8647decd1cbSOGAWA Hirofumi 	fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
8657decd1cbSOGAWA Hirofumi 			  &raw_entry->date, NULL);
8661da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
86762a36c43SStephane Kardas 		__le16 atime;
8687decd1cbSOGAWA Hirofumi 		fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
8697decd1cbSOGAWA Hirofumi 				  &raw_entry->cdate, &raw_entry->ctime_cs);
8707decd1cbSOGAWA Hirofumi 		fat_time_unix2fat(sbi, &inode->i_atime, &atime,
8717decd1cbSOGAWA Hirofumi 				  &raw_entry->adate, NULL);
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
8741da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
8755f22ca9bSLinus Torvalds 	err = 0;
8761da177e4SLinus Torvalds 	if (wait)
8771da177e4SLinus Torvalds 		err = sync_dirty_buffer(bh);
8781da177e4SLinus Torvalds 	brelse(bh);
8791da177e4SLinus Torvalds 	return err;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
882a9185b41SChristoph Hellwig static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
883a9185b41SChristoph Hellwig {
88478491189SArtem Bityutskiy 	int err;
88578491189SArtem Bityutskiy 
88678491189SArtem Bityutskiy 	if (inode->i_ino == MSDOS_FSINFO_INO) {
88778491189SArtem Bityutskiy 		struct super_block *sb = inode->i_sb;
88878491189SArtem Bityutskiy 
889e40b34c7SMarco Stornelli 		mutex_lock(&MSDOS_SB(sb)->s_lock);
89078491189SArtem Bityutskiy 		err = fat_clusters_flush(sb);
891e40b34c7SMarco Stornelli 		mutex_unlock(&MSDOS_SB(sb)->s_lock);
89278491189SArtem Bityutskiy 	} else
89378491189SArtem Bityutskiy 		err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
89478491189SArtem Bityutskiy 
89578491189SArtem Bityutskiy 	return err;
896a9185b41SChristoph Hellwig }
897a9185b41SChristoph Hellwig 
8981da177e4SLinus Torvalds int fat_sync_inode(struct inode *inode)
8991da177e4SLinus Torvalds {
900a9185b41SChristoph Hellwig 	return __fat_write_inode(inode, 1);
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9037c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_sync_inode);
9041da177e4SLinus Torvalds 
90534c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root);
906ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fat_sops = {
9071da177e4SLinus Torvalds 	.alloc_inode	= fat_alloc_inode,
9081da177e4SLinus Torvalds 	.destroy_inode	= fat_destroy_inode,
9091da177e4SLinus Torvalds 	.write_inode	= fat_write_inode,
910deee3ce4SAl Viro 	.evict_inode	= fat_evict_inode,
9111da177e4SLinus Torvalds 	.put_super	= fat_put_super,
9121da177e4SLinus Torvalds 	.statfs		= fat_statfs,
9131da177e4SLinus Torvalds 	.remount_fs	= fat_remount,
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	.show_options	= fat_show_options,
9161da177e4SLinus Torvalds };
9171da177e4SLinus Torvalds 
91834c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root)
9191da177e4SLinus Torvalds {
92034c80b1dSAl Viro 	struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
9211da177e4SLinus Torvalds 	struct fat_mount_options *opts = &sbi->options;
9221da177e4SLinus Torvalds 	int isvfat = opts->isvfat;
9231da177e4SLinus Torvalds 
924170782ebSEric W. Biederman 	if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
925170782ebSEric W. Biederman 		seq_printf(m, ",uid=%u",
926170782ebSEric W. Biederman 				from_kuid_munged(&init_user_ns, opts->fs_uid));
927170782ebSEric W. Biederman 	if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
928170782ebSEric W. Biederman 		seq_printf(m, ",gid=%u",
929170782ebSEric W. Biederman 				from_kgid_munged(&init_user_ns, opts->fs_gid));
9301da177e4SLinus Torvalds 	seq_printf(m, ",fmask=%04o", opts->fs_fmask);
9311da177e4SLinus Torvalds 	seq_printf(m, ",dmask=%04o", opts->fs_dmask);
9321ae43f82SOGAWA Hirofumi 	if (opts->allow_utime)
9331ae43f82SOGAWA Hirofumi 		seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
9341da177e4SLinus Torvalds 	if (sbi->nls_disk)
935c6c20372SDave Reisner 		/* strip "cp" prefix from displayed option */
936c6c20372SDave Reisner 		seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
9371da177e4SLinus Torvalds 	if (isvfat) {
9381da177e4SLinus Torvalds 		if (sbi->nls_io)
9391da177e4SLinus Torvalds 			seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 		switch (opts->shortname) {
9421da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
9431da177e4SLinus Torvalds 			seq_puts(m, ",shortname=win95");
9441da177e4SLinus Torvalds 			break;
9451da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
9461da177e4SLinus Torvalds 			seq_puts(m, ",shortname=winnt");
9471da177e4SLinus Torvalds 			break;
9481da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
9491da177e4SLinus Torvalds 			seq_puts(m, ",shortname=mixed");
9501da177e4SLinus Torvalds 			break;
9511da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
95295523475SPaul Wise 			seq_puts(m, ",shortname=lower");
9531da177e4SLinus Torvalds 			break;
9541da177e4SLinus Torvalds 		default:
9551da177e4SLinus Torvalds 			seq_puts(m, ",shortname=unknown");
9561da177e4SLinus Torvalds 			break;
9571da177e4SLinus Torvalds 		}
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 	if (opts->name_check != 'n')
9601da177e4SLinus Torvalds 		seq_printf(m, ",check=%c", opts->name_check);
96128ec039cSOGAWA Hirofumi 	if (opts->usefree)
96228ec039cSOGAWA Hirofumi 		seq_puts(m, ",usefree");
9631da177e4SLinus Torvalds 	if (opts->quiet)
9641da177e4SLinus Torvalds 		seq_puts(m, ",quiet");
9651da177e4SLinus Torvalds 	if (opts->showexec)
9661da177e4SLinus Torvalds 		seq_puts(m, ",showexec");
9671da177e4SLinus Torvalds 	if (opts->sys_immutable)
9681da177e4SLinus Torvalds 		seq_puts(m, ",sys_immutable");
9691da177e4SLinus Torvalds 	if (!isvfat) {
9701da177e4SLinus Torvalds 		if (opts->dotsOK)
9711da177e4SLinus Torvalds 			seq_puts(m, ",dotsOK=yes");
9721da177e4SLinus Torvalds 		if (opts->nocase)
9731da177e4SLinus Torvalds 			seq_puts(m, ",nocase");
9741da177e4SLinus Torvalds 	} else {
9751da177e4SLinus Torvalds 		if (opts->utf8)
9761da177e4SLinus Torvalds 			seq_puts(m, ",utf8");
9771da177e4SLinus Torvalds 		if (opts->unicode_xlate)
9781da177e4SLinus Torvalds 			seq_puts(m, ",uni_xlate");
9791da177e4SLinus Torvalds 		if (!opts->numtail)
9801da177e4SLinus Torvalds 			seq_puts(m, ",nonumtail");
981dfc209c0SOGAWA Hirofumi 		if (opts->rodir)
982dfc209c0SOGAWA Hirofumi 			seq_puts(m, ",rodir");
9831da177e4SLinus Torvalds 	}
984dfc209c0SOGAWA Hirofumi 	if (opts->flush)
985c1fca3b6SMiklos Szeredi 		seq_puts(m, ",flush");
98658156c8fSJan Kara 	if (opts->tz_set) {
98758156c8fSJan Kara 		if (opts->time_offset)
98858156c8fSJan Kara 			seq_printf(m, ",time_offset=%d", opts->time_offset);
98958156c8fSJan Kara 		else
990b271e067SJoe Peterson 			seq_puts(m, ",tz=UTC");
99158156c8fSJan Kara 	}
99285c78591SDenis Karpov 	if (opts->errors == FAT_ERRORS_CONT)
99385c78591SDenis Karpov 		seq_puts(m, ",errors=continue");
99485c78591SDenis Karpov 	else if (opts->errors == FAT_ERRORS_PANIC)
99585c78591SDenis Karpov 		seq_puts(m, ",errors=panic");
99685c78591SDenis Karpov 	else
99785c78591SDenis Karpov 		seq_puts(m, ",errors=remount-ro");
9982628b7a6SNamjae Jeon 	if (opts->nfs == FAT_NFS_NOSTALE_RO)
9992628b7a6SNamjae Jeon 		seq_puts(m, ",nfs=nostale_ro");
10002628b7a6SNamjae Jeon 	else if (opts->nfs)
10012628b7a6SNamjae Jeon 		seq_puts(m, ",nfs=stale_rw");
1002681142f9SChristoph Hellwig 	if (opts->discard)
1003681142f9SChristoph Hellwig 		seq_puts(m, ",discard");
1004190a8843SConrad Meyer 	if (opts->dos1xfloppy)
1005190a8843SConrad Meyer 		seq_puts(m, ",dos1xfloppy");
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	return 0;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds enum {
10111da177e4SLinus Torvalds 	Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
10121ae43f82SOGAWA Hirofumi 	Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
10131ae43f82SOGAWA Hirofumi 	Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
10141ae43f82SOGAWA Hirofumi 	Opt_immutable, Opt_dots, Opt_nodots,
10151da177e4SLinus Torvalds 	Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
10161da177e4SLinus Torvalds 	Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
10171da177e4SLinus Torvalds 	Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
1018d7a83c0fSGeert Uytterhoeven 	Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
101958156c8fSJan Kara 	Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
1020190a8843SConrad Meyer 	Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err, Opt_dos1xfloppy,
10211da177e4SLinus Torvalds };
10221da177e4SLinus Torvalds 
1023a447c093SSteven Whitehouse static const match_table_t fat_tokens = {
10241da177e4SLinus Torvalds 	{Opt_check_r, "check=relaxed"},
10251da177e4SLinus Torvalds 	{Opt_check_s, "check=strict"},
10261da177e4SLinus Torvalds 	{Opt_check_n, "check=normal"},
10271da177e4SLinus Torvalds 	{Opt_check_r, "check=r"},
10281da177e4SLinus Torvalds 	{Opt_check_s, "check=s"},
10291da177e4SLinus Torvalds 	{Opt_check_n, "check=n"},
10301da177e4SLinus Torvalds 	{Opt_uid, "uid=%u"},
10311da177e4SLinus Torvalds 	{Opt_gid, "gid=%u"},
10321da177e4SLinus Torvalds 	{Opt_umask, "umask=%o"},
10331da177e4SLinus Torvalds 	{Opt_dmask, "dmask=%o"},
10341da177e4SLinus Torvalds 	{Opt_fmask, "fmask=%o"},
10351ae43f82SOGAWA Hirofumi 	{Opt_allow_utime, "allow_utime=%o"},
10361da177e4SLinus Torvalds 	{Opt_codepage, "codepage=%u"},
103728ec039cSOGAWA Hirofumi 	{Opt_usefree, "usefree"},
10381da177e4SLinus Torvalds 	{Opt_nocase, "nocase"},
10391da177e4SLinus Torvalds 	{Opt_quiet, "quiet"},
10401da177e4SLinus Torvalds 	{Opt_showexec, "showexec"},
10411da177e4SLinus Torvalds 	{Opt_debug, "debug"},
10421da177e4SLinus Torvalds 	{Opt_immutable, "sys_immutable"},
104385c78591SDenis Karpov 	{Opt_flush, "flush"},
104485c78591SDenis Karpov 	{Opt_tz_utc, "tz=UTC"},
104558156c8fSJan Kara 	{Opt_time_offset, "time_offset=%d"},
104685c78591SDenis Karpov 	{Opt_err_cont, "errors=continue"},
104785c78591SDenis Karpov 	{Opt_err_panic, "errors=panic"},
104885c78591SDenis Karpov 	{Opt_err_ro, "errors=remount-ro"},
1049681142f9SChristoph Hellwig 	{Opt_discard, "discard"},
10502628b7a6SNamjae Jeon 	{Opt_nfs_stale_rw, "nfs"},
10512628b7a6SNamjae Jeon 	{Opt_nfs_stale_rw, "nfs=stale_rw"},
10522628b7a6SNamjae Jeon 	{Opt_nfs_nostale_ro, "nfs=nostale_ro"},
1053190a8843SConrad Meyer 	{Opt_dos1xfloppy, "dos1xfloppy"},
1054d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=binary"},
1055d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=text"},
1056d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=auto"},
1057d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=b"},
1058d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=t"},
1059d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=a"},
1060d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "fat=%u"},
1061d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "blocksize=%u"},
1062d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "cvf_format=%20s"},
1063d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "cvf_options=%100s"},
1064d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "posix"},
1065ae78bf9cSChris Mason 	{Opt_err, NULL},
10661da177e4SLinus Torvalds };
1067a447c093SSteven Whitehouse static const match_table_t msdos_tokens = {
10681da177e4SLinus Torvalds 	{Opt_nodots, "nodots"},
10691da177e4SLinus Torvalds 	{Opt_nodots, "dotsOK=no"},
10701da177e4SLinus Torvalds 	{Opt_dots, "dots"},
10711da177e4SLinus Torvalds 	{Opt_dots, "dotsOK=yes"},
10721da177e4SLinus Torvalds 	{Opt_err, NULL}
10731da177e4SLinus Torvalds };
1074a447c093SSteven Whitehouse static const match_table_t vfat_tokens = {
10751da177e4SLinus Torvalds 	{Opt_charset, "iocharset=%s"},
10761da177e4SLinus Torvalds 	{Opt_shortname_lower, "shortname=lower"},
10771da177e4SLinus Torvalds 	{Opt_shortname_win95, "shortname=win95"},
10781da177e4SLinus Torvalds 	{Opt_shortname_winnt, "shortname=winnt"},
10791da177e4SLinus Torvalds 	{Opt_shortname_mixed, "shortname=mixed"},
10801da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=0"},		/* 0 or no or false */
10811da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=no"},
10821da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=false"},
10831da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=1"},		/* empty or 1 or yes or true */
10841da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=yes"},
10851da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=true"},
10861da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8"},
10871da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=0"},		/* 0 or no or false */
10881da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=no"},
10891da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=false"},
10901da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=1"},	/* empty or 1 or yes or true */
10911da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=yes"},
10921da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=true"},
10931da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate"},
10941da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=0"},	/* 0 or no or false */
10951da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=no"},
10961da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=false"},
10971da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=1"},	/* empty or 1 or yes or true */
10981da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=yes"},
10991da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=true"},
11001da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail"},
1101dfc209c0SOGAWA Hirofumi 	{Opt_rodir, "rodir"},
11021da177e4SLinus Torvalds 	{Opt_err, NULL}
11031da177e4SLinus Torvalds };
11041da177e4SLinus Torvalds 
1105869f58c0SAlexey Fisher static int parse_options(struct super_block *sb, char *options, int is_vfat,
1106869f58c0SAlexey Fisher 			 int silent, int *debug, struct fat_mount_options *opts)
11071da177e4SLinus Torvalds {
11081da177e4SLinus Torvalds 	char *p;
11091da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
11101da177e4SLinus Torvalds 	int option;
11111da177e4SLinus Torvalds 	char *iocharset;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	opts->isvfat = is_vfat;
11141da177e4SLinus Torvalds 
1115f0ce7ee3SDavid Howells 	opts->fs_uid = current_uid();
1116f0ce7ee3SDavid Howells 	opts->fs_gid = current_gid();
11173e107603SOGAWA Hirofumi 	opts->fs_fmask = opts->fs_dmask = current_umask();
11181ae43f82SOGAWA Hirofumi 	opts->allow_utime = -1;
11191da177e4SLinus Torvalds 	opts->codepage = fat_default_codepage;
11201da177e4SLinus Torvalds 	opts->iocharset = fat_default_iocharset;
1121dfc209c0SOGAWA Hirofumi 	if (is_vfat) {
112295523475SPaul Wise 		opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
1123dfc209c0SOGAWA Hirofumi 		opts->rodir = 0;
1124dfc209c0SOGAWA Hirofumi 	} else {
11251da177e4SLinus Torvalds 		opts->shortname = 0;
1126dfc209c0SOGAWA Hirofumi 		opts->rodir = 1;
1127dfc209c0SOGAWA Hirofumi 	}
11281da177e4SLinus Torvalds 	opts->name_check = 'n';
11291da177e4SLinus Torvalds 	opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
113038739380SMaciej S. Szmigiero 	opts->unicode_xlate = 0;
11311da177e4SLinus Torvalds 	opts->numtail = 1;
113228ec039cSOGAWA Hirofumi 	opts->usefree = opts->nocase = 0;
113358156c8fSJan Kara 	opts->tz_set = 0;
11347669e8fbSSteven J. Magnani 	opts->nfs = 0;
113585c78591SDenis Karpov 	opts->errors = FAT_ERRORS_RO;
11361da177e4SLinus Torvalds 	*debug = 0;
11371da177e4SLinus Torvalds 
113838739380SMaciej S. Szmigiero 	opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat;
113938739380SMaciej S. Szmigiero 
11401da177e4SLinus Torvalds 	if (!options)
11418d44d974SOGAWA Hirofumi 		goto out;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
11441da177e4SLinus Torvalds 		int token;
11451da177e4SLinus Torvalds 		if (!*p)
11461da177e4SLinus Torvalds 			continue;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 		token = match_token(p, fat_tokens, args);
11491da177e4SLinus Torvalds 		if (token == Opt_err) {
11501da177e4SLinus Torvalds 			if (is_vfat)
11511da177e4SLinus Torvalds 				token = match_token(p, vfat_tokens, args);
11521da177e4SLinus Torvalds 			else
11531da177e4SLinus Torvalds 				token = match_token(p, msdos_tokens, args);
11541da177e4SLinus Torvalds 		}
11551da177e4SLinus Torvalds 		switch (token) {
11561da177e4SLinus Torvalds 		case Opt_check_s:
11571da177e4SLinus Torvalds 			opts->name_check = 's';
11581da177e4SLinus Torvalds 			break;
11591da177e4SLinus Torvalds 		case Opt_check_r:
11601da177e4SLinus Torvalds 			opts->name_check = 'r';
11611da177e4SLinus Torvalds 			break;
11621da177e4SLinus Torvalds 		case Opt_check_n:
11631da177e4SLinus Torvalds 			opts->name_check = 'n';
11641da177e4SLinus Torvalds 			break;
116528ec039cSOGAWA Hirofumi 		case Opt_usefree:
116628ec039cSOGAWA Hirofumi 			opts->usefree = 1;
116728ec039cSOGAWA Hirofumi 			break;
11681da177e4SLinus Torvalds 		case Opt_nocase:
11691da177e4SLinus Torvalds 			if (!is_vfat)
11701da177e4SLinus Torvalds 				opts->nocase = 1;
11711da177e4SLinus Torvalds 			else {
11721da177e4SLinus Torvalds 				/* for backward compatibility */
11731da177e4SLinus Torvalds 				opts->shortname = VFAT_SFN_DISPLAY_WIN95
11741da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
11751da177e4SLinus Torvalds 			}
11761da177e4SLinus Torvalds 			break;
11771da177e4SLinus Torvalds 		case Opt_quiet:
11781da177e4SLinus Torvalds 			opts->quiet = 1;
11791da177e4SLinus Torvalds 			break;
11801da177e4SLinus Torvalds 		case Opt_showexec:
11811da177e4SLinus Torvalds 			opts->showexec = 1;
11821da177e4SLinus Torvalds 			break;
11831da177e4SLinus Torvalds 		case Opt_debug:
11841da177e4SLinus Torvalds 			*debug = 1;
11851da177e4SLinus Torvalds 			break;
11861da177e4SLinus Torvalds 		case Opt_immutable:
11871da177e4SLinus Torvalds 			opts->sys_immutable = 1;
11881da177e4SLinus Torvalds 			break;
11891da177e4SLinus Torvalds 		case Opt_uid:
11901da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
11915b3d5aeaSJan Kara 				return -EINVAL;
1192170782ebSEric W. Biederman 			opts->fs_uid = make_kuid(current_user_ns(), option);
1193170782ebSEric W. Biederman 			if (!uid_valid(opts->fs_uid))
11945b3d5aeaSJan Kara 				return -EINVAL;
11951da177e4SLinus Torvalds 			break;
11961da177e4SLinus Torvalds 		case Opt_gid:
11971da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
11985b3d5aeaSJan Kara 				return -EINVAL;
1199170782ebSEric W. Biederman 			opts->fs_gid = make_kgid(current_user_ns(), option);
1200170782ebSEric W. Biederman 			if (!gid_valid(opts->fs_gid))
12015b3d5aeaSJan Kara 				return -EINVAL;
12021da177e4SLinus Torvalds 			break;
12031da177e4SLinus Torvalds 		case Opt_umask:
12041da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
12055b3d5aeaSJan Kara 				return -EINVAL;
12061da177e4SLinus Torvalds 			opts->fs_fmask = opts->fs_dmask = option;
12071da177e4SLinus Torvalds 			break;
12081da177e4SLinus Torvalds 		case Opt_dmask:
12091da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
12105b3d5aeaSJan Kara 				return -EINVAL;
12111da177e4SLinus Torvalds 			opts->fs_dmask = option;
12121da177e4SLinus Torvalds 			break;
12131da177e4SLinus Torvalds 		case Opt_fmask:
12141da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
12155b3d5aeaSJan Kara 				return -EINVAL;
12161da177e4SLinus Torvalds 			opts->fs_fmask = option;
12171da177e4SLinus Torvalds 			break;
12181ae43f82SOGAWA Hirofumi 		case Opt_allow_utime:
12191ae43f82SOGAWA Hirofumi 			if (match_octal(&args[0], &option))
12205b3d5aeaSJan Kara 				return -EINVAL;
12211ae43f82SOGAWA Hirofumi 			opts->allow_utime = option & (S_IWGRP | S_IWOTH);
12221ae43f82SOGAWA Hirofumi 			break;
12231da177e4SLinus Torvalds 		case Opt_codepage:
12241da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
12255b3d5aeaSJan Kara 				return -EINVAL;
12261da177e4SLinus Torvalds 			opts->codepage = option;
12271da177e4SLinus Torvalds 			break;
1228ae78bf9cSChris Mason 		case Opt_flush:
1229ae78bf9cSChris Mason 			opts->flush = 1;
1230ae78bf9cSChris Mason 			break;
123158156c8fSJan Kara 		case Opt_time_offset:
123258156c8fSJan Kara 			if (match_int(&args[0], &option))
12335b3d5aeaSJan Kara 				return -EINVAL;
1234a513d869SJan Kara 			/*
1235a513d869SJan Kara 			 * GMT+-12 zones may have DST corrections so at least
1236a513d869SJan Kara 			 * 13 hours difference is needed. Make the limit 24
1237a513d869SJan Kara 			 * just in case someone invents something unusual.
1238a513d869SJan Kara 			 */
1239a513d869SJan Kara 			if (option < -24 * 60 || option > 24 * 60)
12405b3d5aeaSJan Kara 				return -EINVAL;
124158156c8fSJan Kara 			opts->tz_set = 1;
124258156c8fSJan Kara 			opts->time_offset = option;
124358156c8fSJan Kara 			break;
1244b271e067SJoe Peterson 		case Opt_tz_utc:
124558156c8fSJan Kara 			opts->tz_set = 1;
124658156c8fSJan Kara 			opts->time_offset = 0;
1247b271e067SJoe Peterson 			break;
124885c78591SDenis Karpov 		case Opt_err_cont:
124985c78591SDenis Karpov 			opts->errors = FAT_ERRORS_CONT;
125085c78591SDenis Karpov 			break;
125185c78591SDenis Karpov 		case Opt_err_panic:
125285c78591SDenis Karpov 			opts->errors = FAT_ERRORS_PANIC;
125385c78591SDenis Karpov 			break;
125485c78591SDenis Karpov 		case Opt_err_ro:
125585c78591SDenis Karpov 			opts->errors = FAT_ERRORS_RO;
125685c78591SDenis Karpov 			break;
12572628b7a6SNamjae Jeon 		case Opt_nfs_stale_rw:
12582628b7a6SNamjae Jeon 			opts->nfs = FAT_NFS_STALE_RW;
12592628b7a6SNamjae Jeon 			break;
12602628b7a6SNamjae Jeon 		case Opt_nfs_nostale_ro:
12612628b7a6SNamjae Jeon 			opts->nfs = FAT_NFS_NOSTALE_RO;
12622628b7a6SNamjae Jeon 			break;
1263190a8843SConrad Meyer 		case Opt_dos1xfloppy:
1264190a8843SConrad Meyer 			opts->dos1xfloppy = 1;
1265190a8843SConrad Meyer 			break;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		/* msdos specific */
12681da177e4SLinus Torvalds 		case Opt_dots:
12691da177e4SLinus Torvalds 			opts->dotsOK = 1;
12701da177e4SLinus Torvalds 			break;
12711da177e4SLinus Torvalds 		case Opt_nodots:
12721da177e4SLinus Torvalds 			opts->dotsOK = 0;
12731da177e4SLinus Torvalds 			break;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 		/* vfat specific */
12761da177e4SLinus Torvalds 		case Opt_charset:
12771da177e4SLinus Torvalds 			if (opts->iocharset != fat_default_iocharset)
12781da177e4SLinus Torvalds 				kfree(opts->iocharset);
12791da177e4SLinus Torvalds 			iocharset = match_strdup(&args[0]);
12801da177e4SLinus Torvalds 			if (!iocharset)
12811da177e4SLinus Torvalds 				return -ENOMEM;
12821da177e4SLinus Torvalds 			opts->iocharset = iocharset;
12831da177e4SLinus Torvalds 			break;
12841da177e4SLinus Torvalds 		case Opt_shortname_lower:
12851da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_LOWER
12861da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
12871da177e4SLinus Torvalds 			break;
12881da177e4SLinus Torvalds 		case Opt_shortname_win95:
12891da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WIN95
12901da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
12911da177e4SLinus Torvalds 			break;
12921da177e4SLinus Torvalds 		case Opt_shortname_winnt:
12931da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
12941da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WINNT;
12951da177e4SLinus Torvalds 			break;
12961da177e4SLinus Torvalds 		case Opt_shortname_mixed:
12971da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
12981da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
12991da177e4SLinus Torvalds 			break;
13001da177e4SLinus Torvalds 		case Opt_utf8_no:		/* 0 or no or false */
13011da177e4SLinus Torvalds 			opts->utf8 = 0;
13021da177e4SLinus Torvalds 			break;
13031da177e4SLinus Torvalds 		case Opt_utf8_yes:		/* empty or 1 or yes or true */
13041da177e4SLinus Torvalds 			opts->utf8 = 1;
13051da177e4SLinus Torvalds 			break;
13061da177e4SLinus Torvalds 		case Opt_uni_xl_no:		/* 0 or no or false */
13071da177e4SLinus Torvalds 			opts->unicode_xlate = 0;
13081da177e4SLinus Torvalds 			break;
13091da177e4SLinus Torvalds 		case Opt_uni_xl_yes:		/* empty or 1 or yes or true */
13101da177e4SLinus Torvalds 			opts->unicode_xlate = 1;
13111da177e4SLinus Torvalds 			break;
13121da177e4SLinus Torvalds 		case Opt_nonumtail_no:		/* 0 or no or false */
13131da177e4SLinus Torvalds 			opts->numtail = 1;	/* negated option */
13141da177e4SLinus Torvalds 			break;
13151da177e4SLinus Torvalds 		case Opt_nonumtail_yes:		/* empty or 1 or yes or true */
13161da177e4SLinus Torvalds 			opts->numtail = 0;	/* negated option */
13171da177e4SLinus Torvalds 			break;
1318dfc209c0SOGAWA Hirofumi 		case Opt_rodir:
1319dfc209c0SOGAWA Hirofumi 			opts->rodir = 1;
1320dfc209c0SOGAWA Hirofumi 			break;
1321681142f9SChristoph Hellwig 		case Opt_discard:
1322681142f9SChristoph Hellwig 			opts->discard = 1;
1323681142f9SChristoph Hellwig 			break;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		/* obsolete mount options */
1326d7a83c0fSGeert Uytterhoeven 		case Opt_obsolete:
1327869f58c0SAlexey Fisher 			fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
1328869f58c0SAlexey Fisher 			       "not supported now", p);
13291da177e4SLinus Torvalds 			break;
13301da177e4SLinus Torvalds 		/* unknown option */
13311da177e4SLinus Torvalds 		default:
133241a34a4fSChristoph Hellwig 			if (!silent) {
1333869f58c0SAlexey Fisher 				fat_msg(sb, KERN_ERR,
1334869f58c0SAlexey Fisher 				       "Unrecognized mount option \"%s\" "
1335869f58c0SAlexey Fisher 				       "or missing value", p);
133641a34a4fSChristoph Hellwig 			}
13371da177e4SLinus Torvalds 			return -EINVAL;
13381da177e4SLinus Torvalds 		}
13391da177e4SLinus Torvalds 	}
13408d44d974SOGAWA Hirofumi 
13418d44d974SOGAWA Hirofumi out:
13424de151d8SAlexey Dobriyan 	/* UTF-8 doesn't provide FAT semantics */
13431da177e4SLinus Torvalds 	if (!strcmp(opts->iocharset, "utf8")) {
1344186b5370SMihai Moldovan 		fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
13458d44d974SOGAWA Hirofumi 		       " for FAT filesystems, filesystem will be "
1346186b5370SMihai Moldovan 		       "case sensitive!");
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 
13491ae43f82SOGAWA Hirofumi 	/* If user doesn't specify allow_utime, it's initialized from dmask. */
13501ae43f82SOGAWA Hirofumi 	if (opts->allow_utime == (unsigned short)-1)
13511ae43f82SOGAWA Hirofumi 		opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
13521da177e4SLinus Torvalds 	if (opts->unicode_xlate)
13531da177e4SLinus Torvalds 		opts->utf8 = 0;
1354ea3983acSNamjae Jeon 	if (opts->nfs == FAT_NFS_NOSTALE_RO) {
13552628b7a6SNamjae Jeon 		sb->s_flags |= MS_RDONLY;
1356ea3983acSNamjae Jeon 		sb->s_export_op = &fat_export_ops_nostale;
1357ea3983acSNamjae Jeon 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	return 0;
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds static int fat_read_root(struct inode *inode)
13631da177e4SLinus Torvalds {
1364a40a7d9dSAlexander Kuleshov 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
13651da177e4SLinus Torvalds 	int error;
13661da177e4SLinus Torvalds 
1367ea3983acSNamjae Jeon 	MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
13681da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
13691da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
13701da177e4SLinus Torvalds 	inode->i_version++;
13711da177e4SLinus Torvalds 	inode->i_generation = 0;
13729c0aa1b8SOGAWA Hirofumi 	inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
13731da177e4SLinus Torvalds 	inode->i_op = sbi->dir_ops;
13741da177e4SLinus Torvalds 	inode->i_fop = &fat_dir_operations;
13751da177e4SLinus Torvalds 	if (sbi->fat_bits == 32) {
13761da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = sbi->root_cluster;
13771da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
13781da177e4SLinus Torvalds 		if (error < 0)
13791da177e4SLinus Torvalds 			return error;
13801da177e4SLinus Torvalds 	} else {
13811da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = 0;
13821da177e4SLinus Torvalds 		inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
13851da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
13861da177e4SLinus Torvalds 	MSDOS_I(inode)->i_logstart = 0;
13871da177e4SLinus Torvalds 	MSDOS_I(inode)->mmu_private = inode->i_size;
13881da177e4SLinus Torvalds 
13899c0aa1b8SOGAWA Hirofumi 	fat_save_attrs(inode, ATTR_DIR);
13901da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
13911da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1392bfe86848SMiklos Szeredi 	set_nlink(inode, fat_subdirs(inode)+2);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	return 0;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13977b92d03cSOGAWA Hirofumi static unsigned long calc_fat_clusters(struct super_block *sb)
13987b92d03cSOGAWA Hirofumi {
13997b92d03cSOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
14007b92d03cSOGAWA Hirofumi 
14017b92d03cSOGAWA Hirofumi 	/* Divide first to avoid overflow */
14027b92d03cSOGAWA Hirofumi 	if (sbi->fat_bits != 12) {
14037b92d03cSOGAWA Hirofumi 		unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
14047b92d03cSOGAWA Hirofumi 		return ent_per_sec * sbi->fat_length;
14057b92d03cSOGAWA Hirofumi 	}
14067b92d03cSOGAWA Hirofumi 
14077b92d03cSOGAWA Hirofumi 	return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
14087b92d03cSOGAWA Hirofumi }
14097b92d03cSOGAWA Hirofumi 
1410190a8843SConrad Meyer static bool fat_bpb_is_zero(struct fat_boot_sector *b)
1411190a8843SConrad Meyer {
1412190a8843SConrad Meyer 	if (get_unaligned_le16(&b->sector_size))
1413190a8843SConrad Meyer 		return false;
1414190a8843SConrad Meyer 	if (b->sec_per_clus)
1415190a8843SConrad Meyer 		return false;
1416190a8843SConrad Meyer 	if (b->reserved)
1417190a8843SConrad Meyer 		return false;
1418190a8843SConrad Meyer 	if (b->fats)
1419190a8843SConrad Meyer 		return false;
1420190a8843SConrad Meyer 	if (get_unaligned_le16(&b->dir_entries))
1421190a8843SConrad Meyer 		return false;
1422190a8843SConrad Meyer 	if (get_unaligned_le16(&b->sectors))
1423190a8843SConrad Meyer 		return false;
1424190a8843SConrad Meyer 	if (b->media)
1425190a8843SConrad Meyer 		return false;
1426190a8843SConrad Meyer 	if (b->fat_length)
1427190a8843SConrad Meyer 		return false;
1428190a8843SConrad Meyer 	if (b->secs_track)
1429190a8843SConrad Meyer 		return false;
1430190a8843SConrad Meyer 	if (b->heads)
1431190a8843SConrad Meyer 		return false;
1432190a8843SConrad Meyer 	return true;
1433190a8843SConrad Meyer }
1434190a8843SConrad Meyer 
1435190a8843SConrad Meyer static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b,
1436190a8843SConrad Meyer 	int silent, struct fat_bios_param_block *bpb)
1437190a8843SConrad Meyer {
1438190a8843SConrad Meyer 	int error = -EINVAL;
1439190a8843SConrad Meyer 
1440190a8843SConrad Meyer 	/* Read in BPB ... */
1441190a8843SConrad Meyer 	memset(bpb, 0, sizeof(*bpb));
1442190a8843SConrad Meyer 	bpb->fat_sector_size = get_unaligned_le16(&b->sector_size);
1443190a8843SConrad Meyer 	bpb->fat_sec_per_clus = b->sec_per_clus;
1444190a8843SConrad Meyer 	bpb->fat_reserved = le16_to_cpu(b->reserved);
1445190a8843SConrad Meyer 	bpb->fat_fats = b->fats;
1446190a8843SConrad Meyer 	bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries);
1447190a8843SConrad Meyer 	bpb->fat_sectors = get_unaligned_le16(&b->sectors);
1448190a8843SConrad Meyer 	bpb->fat_fat_length = le16_to_cpu(b->fat_length);
1449190a8843SConrad Meyer 	bpb->fat_total_sect = le32_to_cpu(b->total_sect);
1450190a8843SConrad Meyer 
1451190a8843SConrad Meyer 	bpb->fat16_state = b->fat16.state;
1452190a8843SConrad Meyer 	bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id);
1453190a8843SConrad Meyer 
1454190a8843SConrad Meyer 	bpb->fat32_length = le32_to_cpu(b->fat32.length);
1455190a8843SConrad Meyer 	bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster);
1456190a8843SConrad Meyer 	bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector);
1457190a8843SConrad Meyer 	bpb->fat32_state = b->fat32.state;
1458190a8843SConrad Meyer 	bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id);
1459190a8843SConrad Meyer 
1460190a8843SConrad Meyer 	/* Validate this looks like a FAT filesystem BPB */
1461190a8843SConrad Meyer 	if (!bpb->fat_reserved) {
1462190a8843SConrad Meyer 		if (!silent)
1463190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR,
1464190a8843SConrad Meyer 				"bogus number of reserved sectors");
1465190a8843SConrad Meyer 		goto out;
1466190a8843SConrad Meyer 	}
1467190a8843SConrad Meyer 	if (!bpb->fat_fats) {
1468190a8843SConrad Meyer 		if (!silent)
1469190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1470190a8843SConrad Meyer 		goto out;
1471190a8843SConrad Meyer 	}
1472190a8843SConrad Meyer 
1473190a8843SConrad Meyer 	/*
1474190a8843SConrad Meyer 	 * Earlier we checked here that b->secs_track and b->head are nonzero,
1475190a8843SConrad Meyer 	 * but it turns out valid FAT filesystems can have zero there.
1476190a8843SConrad Meyer 	 */
1477190a8843SConrad Meyer 
1478190a8843SConrad Meyer 	if (!fat_valid_media(b->media)) {
1479190a8843SConrad Meyer 		if (!silent)
1480190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
1481190a8843SConrad Meyer 				(unsigned)b->media);
1482190a8843SConrad Meyer 		goto out;
1483190a8843SConrad Meyer 	}
1484190a8843SConrad Meyer 
1485190a8843SConrad Meyer 	if (!is_power_of_2(bpb->fat_sector_size)
1486190a8843SConrad Meyer 	    || (bpb->fat_sector_size < 512)
1487190a8843SConrad Meyer 	    || (bpb->fat_sector_size > 4096)) {
1488190a8843SConrad Meyer 		if (!silent)
1489190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
1490190a8843SConrad Meyer 			       (unsigned)bpb->fat_sector_size);
1491190a8843SConrad Meyer 		goto out;
1492190a8843SConrad Meyer 	}
1493190a8843SConrad Meyer 
1494190a8843SConrad Meyer 	if (!is_power_of_2(bpb->fat_sec_per_clus)) {
1495190a8843SConrad Meyer 		if (!silent)
1496190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
1497190a8843SConrad Meyer 				(unsigned)bpb->fat_sec_per_clus);
1498190a8843SConrad Meyer 		goto out;
1499190a8843SConrad Meyer 	}
1500190a8843SConrad Meyer 
1501190a8843SConrad Meyer 	error = 0;
1502190a8843SConrad Meyer 
1503190a8843SConrad Meyer out:
1504190a8843SConrad Meyer 	return error;
1505190a8843SConrad Meyer }
1506190a8843SConrad Meyer 
1507190a8843SConrad Meyer static int fat_read_static_bpb(struct super_block *sb,
1508190a8843SConrad Meyer 	struct fat_boot_sector *b, int silent,
1509190a8843SConrad Meyer 	struct fat_bios_param_block *bpb)
1510190a8843SConrad Meyer {
1511190a8843SConrad Meyer 	static const char *notdos1x = "This doesn't look like a DOS 1.x volume";
1512190a8843SConrad Meyer 
1513190a8843SConrad Meyer 	struct fat_floppy_defaults *fdefaults = NULL;
1514190a8843SConrad Meyer 	int error = -EINVAL;
1515190a8843SConrad Meyer 	sector_t bd_sects;
1516190a8843SConrad Meyer 	unsigned i;
1517190a8843SConrad Meyer 
1518190a8843SConrad Meyer 	bd_sects = i_size_read(sb->s_bdev->bd_inode) / SECTOR_SIZE;
1519190a8843SConrad Meyer 
1520190a8843SConrad Meyer 	/* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */
1521190a8843SConrad Meyer 	if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) {
1522190a8843SConrad Meyer 		if (!silent)
1523190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR,
1524190a8843SConrad Meyer 				"%s; no bootstrapping code", notdos1x);
1525190a8843SConrad Meyer 		goto out;
1526190a8843SConrad Meyer 	}
1527190a8843SConrad Meyer 
1528190a8843SConrad Meyer 	/*
1529190a8843SConrad Meyer 	 * If any value in this region is non-zero, it isn't archaic
1530190a8843SConrad Meyer 	 * DOS.
1531190a8843SConrad Meyer 	 */
1532190a8843SConrad Meyer 	if (!fat_bpb_is_zero(b)) {
1533190a8843SConrad Meyer 		if (!silent)
1534190a8843SConrad Meyer 			fat_msg(sb, KERN_ERR,
1535190a8843SConrad Meyer 				"%s; DOS 2.x BPB is non-zero", notdos1x);
1536190a8843SConrad Meyer 		goto out;
1537190a8843SConrad Meyer 	}
1538190a8843SConrad Meyer 
1539190a8843SConrad Meyer 	for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) {
1540190a8843SConrad Meyer 		if (floppy_defaults[i].nr_sectors == bd_sects) {
1541190a8843SConrad Meyer 			fdefaults = &floppy_defaults[i];
1542190a8843SConrad Meyer 			break;
1543190a8843SConrad Meyer 		}
1544190a8843SConrad Meyer 	}
1545190a8843SConrad Meyer 
1546190a8843SConrad Meyer 	if (fdefaults == NULL) {
1547190a8843SConrad Meyer 		if (!silent)
1548190a8843SConrad Meyer 			fat_msg(sb, KERN_WARNING,
1549190a8843SConrad Meyer 				"This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)",
1550190a8843SConrad Meyer 				(u64)bd_sects);
1551190a8843SConrad Meyer 		goto out;
1552190a8843SConrad Meyer 	}
1553190a8843SConrad Meyer 
1554190a8843SConrad Meyer 	if (!silent)
1555190a8843SConrad Meyer 		fat_msg(sb, KERN_INFO,
1556190a8843SConrad Meyer 			"This looks like a DOS 1.x volume; assuming default BPB values");
1557190a8843SConrad Meyer 
1558190a8843SConrad Meyer 	memset(bpb, 0, sizeof(*bpb));
1559190a8843SConrad Meyer 	bpb->fat_sector_size = SECTOR_SIZE;
1560190a8843SConrad Meyer 	bpb->fat_sec_per_clus = fdefaults->sec_per_clus;
1561190a8843SConrad Meyer 	bpb->fat_reserved = 1;
1562190a8843SConrad Meyer 	bpb->fat_fats = 2;
1563190a8843SConrad Meyer 	bpb->fat_dir_entries = fdefaults->dir_entries;
1564190a8843SConrad Meyer 	bpb->fat_sectors = fdefaults->nr_sectors;
1565190a8843SConrad Meyer 	bpb->fat_fat_length = fdefaults->fat_length;
1566190a8843SConrad Meyer 
1567190a8843SConrad Meyer 	error = 0;
1568190a8843SConrad Meyer 
1569190a8843SConrad Meyer out:
1570190a8843SConrad Meyer 	return error;
1571190a8843SConrad Meyer }
1572190a8843SConrad Meyer 
15731da177e4SLinus Torvalds /*
15741da177e4SLinus Torvalds  * Read the super block of an MS-DOS FS.
15751da177e4SLinus Torvalds  */
1576384f5c96SOGAWA Hirofumi int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
15773d23985dSAl Viro 		   void (*setup)(struct super_block *))
15781da177e4SLinus Torvalds {
1579b522412aSAl Viro 	struct inode *root_inode = NULL, *fat_inode = NULL;
1580020ac5b6SArtem Bityutskiy 	struct inode *fsinfo_inode = NULL;
15811da177e4SLinus Torvalds 	struct buffer_head *bh;
1582190a8843SConrad Meyer 	struct fat_bios_param_block bpb;
15831da177e4SLinus Torvalds 	struct msdos_sb_info *sbi;
15841da177e4SLinus Torvalds 	u16 logical_sector_size;
15851da177e4SLinus Torvalds 	u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
15861da177e4SLinus Torvalds 	int debug;
15871da177e4SLinus Torvalds 	long error;
15881da177e4SLinus Torvalds 	char buf[50];
15891da177e4SLinus Torvalds 
15908f593427SLinus Torvalds 	/*
15918f593427SLinus Torvalds 	 * GFP_KERNEL is ok here, because while we do hold the
1592*dcc381e8SZheng Lv 	 * superblock lock, memory pressure can't call back into
15938f593427SLinus Torvalds 	 * the filesystem, since we're only just about to mount
15948f593427SLinus Torvalds 	 * it and have no inodes etc active!
15958f593427SLinus Torvalds 	 */
1596f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
15971da177e4SLinus Torvalds 	if (!sbi)
15981da177e4SLinus Torvalds 		return -ENOMEM;
15991da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
16021da177e4SLinus Torvalds 	sb->s_magic = MSDOS_SUPER_MAGIC;
16031da177e4SLinus Torvalds 	sb->s_op = &fat_sops;
16041da177e4SLinus Torvalds 	sb->s_export_op = &fat_export_ops;
16058fceb4e0SNamjae Jeon 	mutex_init(&sbi->nfs_build_inode_lock);
1606aaa04b48SOGAWA Hirofumi 	ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1607aaa04b48SOGAWA Hirofumi 			     DEFAULT_RATELIMIT_BURST);
16081da177e4SLinus Torvalds 
1609869f58c0SAlexey Fisher 	error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
16101da177e4SLinus Torvalds 	if (error)
16111da177e4SLinus Torvalds 		goto out_fail;
16121da177e4SLinus Torvalds 
16133d23985dSAl Viro 	setup(sb); /* flavour-specific stuff that needs options */
16143d23985dSAl Viro 
16151da177e4SLinus Torvalds 	error = -EIO;
16161da177e4SLinus Torvalds 	sb_min_blocksize(sb, 512);
16171da177e4SLinus Torvalds 	bh = sb_bread(sb, 0);
16181da177e4SLinus Torvalds 	if (bh == NULL) {
1619869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "unable to read boot sector");
16201da177e4SLinus Torvalds 		goto out_fail;
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds 
1623190a8843SConrad Meyer 	error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent,
1624190a8843SConrad Meyer 		&bpb);
1625190a8843SConrad Meyer 	if (error == -EINVAL && sbi->options.dos1xfloppy)
1626190a8843SConrad Meyer 		error = fat_read_static_bpb(sb,
1627190a8843SConrad Meyer 			(struct fat_boot_sector *)bh->b_data, silent, &bpb);
16281da177e4SLinus Torvalds 	brelse(bh);
16291da177e4SLinus Torvalds 
1630190a8843SConrad Meyer 	if (error == -EINVAL)
1631190a8843SConrad Meyer 		goto out_invalid;
1632190a8843SConrad Meyer 	else if (error)
1633190a8843SConrad Meyer 		goto out_fail;
16341da177e4SLinus Torvalds 
1635190a8843SConrad Meyer 	logical_sector_size = bpb.fat_sector_size;
1636190a8843SConrad Meyer 	sbi->sec_per_clus = bpb.fat_sec_per_clus;
16371da177e4SLinus Torvalds 
1638190a8843SConrad Meyer 	error = -EIO;
16391da177e4SLinus Torvalds 	if (logical_sector_size < sb->s_blocksize) {
1640869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1641869f58c0SAlexey Fisher 		       " (logical sector size = %u)", logical_sector_size);
16421da177e4SLinus Torvalds 		goto out_fail;
16431da177e4SLinus Torvalds 	}
1644190a8843SConrad Meyer 
16451da177e4SLinus Torvalds 	if (logical_sector_size > sb->s_blocksize) {
1646190a8843SConrad Meyer 		struct buffer_head *bh_resize;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 		if (!sb_set_blocksize(sb, logical_sector_size)) {
1649869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
16501da177e4SLinus Torvalds 			       logical_sector_size);
16511da177e4SLinus Torvalds 			goto out_fail;
16521da177e4SLinus Torvalds 		}
1653190a8843SConrad Meyer 
1654190a8843SConrad Meyer 		/* Verify that the larger boot sector is fully readable */
1655190a8843SConrad Meyer 		bh_resize = sb_bread(sb, 0);
1656190a8843SConrad Meyer 		if (bh_resize == NULL) {
1657869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "unable to read boot sector"
1658869f58c0SAlexey Fisher 			       " (logical sector size = %lu)",
16591da177e4SLinus Torvalds 			       sb->s_blocksize);
16601da177e4SLinus Torvalds 			goto out_fail;
16611da177e4SLinus Torvalds 		}
1662190a8843SConrad Meyer 		brelse(bh_resize);
16631da177e4SLinus Torvalds 	}
16641da177e4SLinus Torvalds 
1665e40b34c7SMarco Stornelli 	mutex_init(&sbi->s_lock);
16661da177e4SLinus Torvalds 	sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
16671da177e4SLinus Torvalds 	sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1668190a8843SConrad Meyer 	sbi->fats = bpb.fat_fats;
16691da177e4SLinus Torvalds 	sbi->fat_bits = 0;		/* Don't know yet */
1670190a8843SConrad Meyer 	sbi->fat_start = bpb.fat_reserved;
1671190a8843SConrad Meyer 	sbi->fat_length = bpb.fat_fat_length;
16721da177e4SLinus Torvalds 	sbi->root_cluster = 0;
16731da177e4SLinus Torvalds 	sbi->free_clusters = -1;	/* Don't know yet */
1674606e423eSOGAWA Hirofumi 	sbi->free_clus_valid = 0;
16751da177e4SLinus Torvalds 	sbi->prev_free = FAT_START_ENT;
1676710d4403SNamjae Jeon 	sb->s_maxbytes = 0xffffffff;
16771da177e4SLinus Torvalds 
1678190a8843SConrad Meyer 	if (!sbi->fat_length && bpb.fat32_length) {
16791da177e4SLinus Torvalds 		struct fat_boot_fsinfo *fsinfo;
16801da177e4SLinus Torvalds 		struct buffer_head *fsinfo_bh;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		/* Must be FAT32 */
16831da177e4SLinus Torvalds 		sbi->fat_bits = 32;
1684190a8843SConrad Meyer 		sbi->fat_length = bpb.fat32_length;
1685190a8843SConrad Meyer 		sbi->root_cluster = bpb.fat32_root_cluster;
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 		/* MC - if info_sector is 0, don't multiply by 0 */
1688190a8843SConrad Meyer 		sbi->fsinfo_sector = bpb.fat32_info_sector;
16891da177e4SLinus Torvalds 		if (sbi->fsinfo_sector == 0)
16901da177e4SLinus Torvalds 			sbi->fsinfo_sector = 1;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 		fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
16931da177e4SLinus Torvalds 		if (fsinfo_bh == NULL) {
1694869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1695869f58c0SAlexey Fisher 			       " (sector = %lu)", sbi->fsinfo_sector);
16961da177e4SLinus Torvalds 			goto out_fail;
16971da177e4SLinus Torvalds 		}
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 		fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
17001da177e4SLinus Torvalds 		if (!IS_FSINFO(fsinfo)) {
1701869f58c0SAlexey Fisher 			fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1702869f58c0SAlexey Fisher 			       "0x%08x, 0x%08x (sector = %lu)",
17031da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature1),
17041da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature2),
17051da177e4SLinus Torvalds 			       sbi->fsinfo_sector);
17061da177e4SLinus Torvalds 		} else {
170728ec039cSOGAWA Hirofumi 			if (sbi->options.usefree)
1708606e423eSOGAWA Hirofumi 				sbi->free_clus_valid = 1;
1709606e423eSOGAWA Hirofumi 			sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
17101da177e4SLinus Torvalds 			sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
17111da177e4SLinus Torvalds 		}
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 		brelse(fsinfo_bh);
17141da177e4SLinus Torvalds 	}
17151da177e4SLinus Torvalds 
17166e5b93eeSMike Lockwood 	/* interpret volume ID as a little endian 32 bit integer */
17176e5b93eeSMike Lockwood 	if (sbi->fat_bits == 32)
1718190a8843SConrad Meyer 		sbi->vol_id = bpb.fat32_vol_id;
17196e5b93eeSMike Lockwood 	else /* fat 16 or 12 */
1720190a8843SConrad Meyer 		sbi->vol_id = bpb.fat16_vol_id;
17216e5b93eeSMike Lockwood 
17221da177e4SLinus Torvalds 	sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
17231da177e4SLinus Torvalds 	sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1726190a8843SConrad Meyer 	sbi->dir_entries = bpb.fat_dir_entries;
17271da177e4SLinus Torvalds 	if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
17281da177e4SLinus Torvalds 		if (!silent)
1729c39540c6SRavishankar N 			fat_msg(sb, KERN_ERR, "bogus directory-entries per block"
1730869f58c0SAlexey Fisher 			       " (%u)", sbi->dir_entries);
17311da177e4SLinus Torvalds 		goto out_invalid;
17321da177e4SLinus Torvalds 	}
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	rootdir_sectors = sbi->dir_entries
17351da177e4SLinus Torvalds 		* sizeof(struct msdos_dir_entry) / sb->s_blocksize;
17361da177e4SLinus Torvalds 	sbi->data_start = sbi->dir_start + rootdir_sectors;
1737190a8843SConrad Meyer 	total_sectors = bpb.fat_sectors;
17381da177e4SLinus Torvalds 	if (total_sectors == 0)
1739190a8843SConrad Meyer 		total_sectors = bpb.fat_total_sect;
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	if (sbi->fat_bits != 32)
17441da177e4SLinus Torvalds 		sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
17451da177e4SLinus Torvalds 
1746b88a1058SOleksij Rempel 	/* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1747b88a1058SOleksij Rempel 	if (sbi->fat_bits == 32)
1748190a8843SConrad Meyer 		sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
1749b88a1058SOleksij Rempel 	else /* fat 16 or 12 */
1750190a8843SConrad Meyer 		sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
1751b88a1058SOleksij Rempel 
17521da177e4SLinus Torvalds 	/* check that FAT table does not overflow */
17537b92d03cSOGAWA Hirofumi 	fat_clusters = calc_fat_clusters(sb);
17541da177e4SLinus Torvalds 	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
17551da177e4SLinus Torvalds 	if (total_clusters > MAX_FAT(sb)) {
17561da177e4SLinus Torvalds 		if (!silent)
1757869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
17581da177e4SLinus Torvalds 			       total_clusters);
17591da177e4SLinus Torvalds 		goto out_invalid;
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	sbi->max_cluster = total_clusters + FAT_START_ENT;
17631da177e4SLinus Torvalds 	/* check the free_clusters, it's not necessarily correct */
17641da177e4SLinus Torvalds 	if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
17651da177e4SLinus Torvalds 		sbi->free_clusters = -1;
17661da177e4SLinus Torvalds 	/* check the prev_free, it's not necessarily correct */
17671da177e4SLinus Torvalds 	sbi->prev_free %= sbi->max_cluster;
17681da177e4SLinus Torvalds 	if (sbi->prev_free < FAT_START_ENT)
17691da177e4SLinus Torvalds 		sbi->prev_free = FAT_START_ENT;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
17721da177e4SLinus Torvalds 	fat_hash_init(sb);
17737669e8fbSSteven J. Magnani 	dir_hash_init(sb);
17741da177e4SLinus Torvalds 	fat_ent_access_init(sb);
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	/*
17771da177e4SLinus Torvalds 	 * The low byte of FAT's first entry must have same value with
17781da177e4SLinus Torvalds 	 * media-field.  But in real world, too many devices is
17791da177e4SLinus Torvalds 	 * writing wrong value.  So, removed that validity check.
17801da177e4SLinus Torvalds 	 *
17811da177e4SLinus Torvalds 	 * if (FAT_FIRST_ENT(sb, media) != first)
17821da177e4SLinus Torvalds 	 */
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	error = -EINVAL;
17851da177e4SLinus Torvalds 	sprintf(buf, "cp%d", sbi->options.codepage);
17861da177e4SLinus Torvalds 	sbi->nls_disk = load_nls(buf);
17871da177e4SLinus Torvalds 	if (!sbi->nls_disk) {
1788869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
17891da177e4SLinus Torvalds 		goto out_fail;
17901da177e4SLinus Torvalds 	}
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	/* FIXME: utf8 is using iocharset for upper/lower conversion */
17931da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
17941da177e4SLinus Torvalds 		sbi->nls_io = load_nls(sbi->options.iocharset);
17951da177e4SLinus Torvalds 		if (!sbi->nls_io) {
1796869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "IO charset %s not found",
17971da177e4SLinus Torvalds 			       sbi->options.iocharset);
17981da177e4SLinus Torvalds 			goto out_fail;
17991da177e4SLinus Torvalds 		}
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	error = -ENOMEM;
1803b522412aSAl Viro 	fat_inode = new_inode(sb);
1804b522412aSAl Viro 	if (!fat_inode)
1805b522412aSAl Viro 		goto out_fail;
1806b522412aSAl Viro 	MSDOS_I(fat_inode)->i_pos = 0;
1807b522412aSAl Viro 	sbi->fat_inode = fat_inode;
1808020ac5b6SArtem Bityutskiy 
1809020ac5b6SArtem Bityutskiy 	fsinfo_inode = new_inode(sb);
1810020ac5b6SArtem Bityutskiy 	if (!fsinfo_inode)
1811020ac5b6SArtem Bityutskiy 		goto out_fail;
1812020ac5b6SArtem Bityutskiy 	fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1813020ac5b6SArtem Bityutskiy 	sbi->fsinfo_inode = fsinfo_inode;
1814020ac5b6SArtem Bityutskiy 	insert_inode_hash(fsinfo_inode);
1815020ac5b6SArtem Bityutskiy 
18161da177e4SLinus Torvalds 	root_inode = new_inode(sb);
18171da177e4SLinus Torvalds 	if (!root_inode)
18181da177e4SLinus Torvalds 		goto out_fail;
18191da177e4SLinus Torvalds 	root_inode->i_ino = MSDOS_ROOT_INO;
18201da177e4SLinus Torvalds 	root_inode->i_version = 1;
18211da177e4SLinus Torvalds 	error = fat_read_root(root_inode);
18221688f860SAl Viro 	if (error < 0) {
18231688f860SAl Viro 		iput(root_inode);
18241da177e4SLinus Torvalds 		goto out_fail;
18251688f860SAl Viro 	}
18261da177e4SLinus Torvalds 	error = -ENOMEM;
18271da177e4SLinus Torvalds 	insert_inode_hash(root_inode);
18287669e8fbSSteven J. Magnani 	fat_attach(root_inode, 0);
18291688f860SAl Viro 	sb->s_root = d_make_root(root_inode);
18301da177e4SLinus Torvalds 	if (!sb->s_root) {
1831869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "get root inode failed");
18321da177e4SLinus Torvalds 		goto out_fail;
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds 
1835f562146aSNamjae Jeon 	if (sbi->options.discard) {
1836f562146aSNamjae Jeon 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
1837f562146aSNamjae Jeon 		if (!blk_queue_discard(q))
1838f562146aSNamjae Jeon 			fat_msg(sb, KERN_WARNING,
1839f562146aSNamjae Jeon 					"mounting with \"discard\" option, but "
1840f562146aSNamjae Jeon 					"the device does not support discard");
1841f562146aSNamjae Jeon 	}
1842f562146aSNamjae Jeon 
1843b88a1058SOleksij Rempel 	fat_set_state(sb, 1, 0);
18441da177e4SLinus Torvalds 	return 0;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds out_invalid:
18471da177e4SLinus Torvalds 	error = -EINVAL;
18481da177e4SLinus Torvalds 	if (!silent)
1849869f58c0SAlexey Fisher 		fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds out_fail:
1852020ac5b6SArtem Bityutskiy 	if (fsinfo_inode)
1853020ac5b6SArtem Bityutskiy 		iput(fsinfo_inode);
1854b522412aSAl Viro 	if (fat_inode)
1855b522412aSAl Viro 		iput(fat_inode);
18561da177e4SLinus Torvalds 	unload_nls(sbi->nls_io);
18571da177e4SLinus Torvalds 	unload_nls(sbi->nls_disk);
18581da177e4SLinus Torvalds 	if (sbi->options.iocharset != fat_default_iocharset)
18591da177e4SLinus Torvalds 		kfree(sbi->options.iocharset);
18601da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
18611da177e4SLinus Torvalds 	kfree(sbi);
18621da177e4SLinus Torvalds 	return error;
18631da177e4SLinus Torvalds }
18641da177e4SLinus Torvalds 
18657c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_fill_super);
18661da177e4SLinus Torvalds 
1867ae78bf9cSChris Mason /*
1868ae78bf9cSChris Mason  * helper function for fat_flush_inodes.  This writes both the inode
1869ae78bf9cSChris Mason  * and the file data blocks, waiting for in flight data blocks before
1870ae78bf9cSChris Mason  * the start of the call.  It does not wait for any io started
1871ae78bf9cSChris Mason  * during the call
1872ae78bf9cSChris Mason  */
1873ae78bf9cSChris Mason static int writeback_inode(struct inode *inode)
1874ae78bf9cSChris Mason {
1875ae78bf9cSChris Mason 
1876ae78bf9cSChris Mason 	int ret;
187714864655SNamjae Jeon 
187814864655SNamjae Jeon 	/* if we used wait=1, sync_inode_metadata waits for the io for the
187914864655SNamjae Jeon 	* inode to finish.  So wait=0 is sent down to sync_inode_metadata
1880ae78bf9cSChris Mason 	* and filemap_fdatawrite is used for the data blocks
1881ae78bf9cSChris Mason 	*/
188214864655SNamjae Jeon 	ret = sync_inode_metadata(inode, 0);
1883ae78bf9cSChris Mason 	if (!ret)
188414864655SNamjae Jeon 		ret = filemap_fdatawrite(inode->i_mapping);
1885ae78bf9cSChris Mason 	return ret;
1886ae78bf9cSChris Mason }
1887ae78bf9cSChris Mason 
1888ae78bf9cSChris Mason /*
1889ae78bf9cSChris Mason  * write data and metadata corresponding to i1 and i2.  The io is
1890ae78bf9cSChris Mason  * started but we do not wait for any of it to finish.
1891ae78bf9cSChris Mason  *
1892ae78bf9cSChris Mason  * filemap_flush is used for the block device, so if there is a dirty
1893ae78bf9cSChris Mason  * page for a block already in flight, we will not wait and start the
1894ae78bf9cSChris Mason  * io over again
1895ae78bf9cSChris Mason  */
1896ae78bf9cSChris Mason int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1897ae78bf9cSChris Mason {
1898ae78bf9cSChris Mason 	int ret = 0;
1899ae78bf9cSChris Mason 	if (!MSDOS_SB(sb)->options.flush)
1900ae78bf9cSChris Mason 		return 0;
1901ae78bf9cSChris Mason 	if (i1)
1902ae78bf9cSChris Mason 		ret = writeback_inode(i1);
1903ae78bf9cSChris Mason 	if (!ret && i2)
1904ae78bf9cSChris Mason 		ret = writeback_inode(i2);
190597e860d3SEric Sesterhenn 	if (!ret) {
1906ae78bf9cSChris Mason 		struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1907ae78bf9cSChris Mason 		ret = filemap_flush(mapping);
1908ae78bf9cSChris Mason 	}
1909ae78bf9cSChris Mason 	return ret;
1910ae78bf9cSChris Mason }
1911ae78bf9cSChris Mason EXPORT_SYMBOL_GPL(fat_flush_inodes);
1912ae78bf9cSChris Mason 
19131da177e4SLinus Torvalds static int __init init_fat_fs(void)
19141da177e4SLinus Torvalds {
1915532a39a3SPekka J Enberg 	int err;
19161da177e4SLinus Torvalds 
1917532a39a3SPekka J Enberg 	err = fat_cache_init();
1918532a39a3SPekka J Enberg 	if (err)
1919532a39a3SPekka J Enberg 		return err;
1920532a39a3SPekka J Enberg 
1921532a39a3SPekka J Enberg 	err = fat_init_inodecache();
1922532a39a3SPekka J Enberg 	if (err)
1923532a39a3SPekka J Enberg 		goto failed;
1924532a39a3SPekka J Enberg 
1925532a39a3SPekka J Enberg 	return 0;
1926532a39a3SPekka J Enberg 
1927532a39a3SPekka J Enberg failed:
1928532a39a3SPekka J Enberg 	fat_cache_destroy();
1929532a39a3SPekka J Enberg 	return err;
19301da177e4SLinus Torvalds }
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds static void __exit exit_fat_fs(void)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	fat_cache_destroy();
19351da177e4SLinus Torvalds 	fat_destroy_inodecache();
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds module_init(init_fat_fs)
19391da177e4SLinus Torvalds module_exit(exit_fat_fs)
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1942