xref: /linux/fs/fat/inode.c (revision 6b46419b0462ae565880f02e9cd0baf9b25ea71f)
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/init.h>
151da177e4SLinus Torvalds #include <linux/time.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/seq_file.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
19a5425d29SOGAWA Hirofumi #include <linux/mpage.h>
201da177e4SLinus Torvalds #include <linux/buffer_head.h>
21a5694255SChristoph Hellwig #include <linux/exportfs.h>
221da177e4SLinus Torvalds #include <linux/mount.h>
231da177e4SLinus Torvalds #include <linux/vfs.h>
241da177e4SLinus Torvalds #include <linux/parser.h>
25e5174baaSOGAWA Hirofumi #include <linux/uio.h>
26ae78bf9cSChris Mason #include <linux/writeback.h>
27e7d709c0SVignesh Babu BM #include <linux/log2.h>
28d3dfa822SOGAWA Hirofumi #include <linux/hash.h>
29f562146aSNamjae Jeon #include <linux/blkdev.h>
301da177e4SLinus Torvalds #include <asm/unaligned.h>
319e975daeSOGAWA Hirofumi #include "fat.h"
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
341da177e4SLinus Torvalds /* if user don't select VFAT, this is undefined. */
351da177e4SLinus Torvalds #define CONFIG_FAT_DEFAULT_IOCHARSET	""
361da177e4SLinus Torvalds #endif
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
391da177e4SLinus Torvalds static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static int fat_add_cluster(struct inode *inode)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	int err, cluster;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	err = fat_alloc_clusters(inode, &cluster, 1);
471da177e4SLinus Torvalds 	if (err)
481da177e4SLinus Torvalds 		return err;
491da177e4SLinus Torvalds 	/* FIXME: this cluster should be added after data of this
501da177e4SLinus Torvalds 	 * cluster is writed */
511da177e4SLinus Torvalds 	err = fat_chain_add(inode, cluster, 1);
521da177e4SLinus Torvalds 	if (err)
531da177e4SLinus Torvalds 		fat_free_clusters(inode, cluster);
541da177e4SLinus Torvalds 	return err;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
576a1d9805SOGAWA Hirofumi static inline int __fat_get_block(struct inode *inode, sector_t iblock,
58e5174baaSOGAWA Hirofumi 				  unsigned long *max_blocks,
591da177e4SLinus Torvalds 				  struct buffer_head *bh_result, int create)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
62e5174baaSOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
63e5174baaSOGAWA Hirofumi 	unsigned long mapped_blocks;
646a1d9805SOGAWA Hirofumi 	sector_t phys;
65e5174baaSOGAWA Hirofumi 	int err, offset;
661da177e4SLinus Torvalds 
672bdf67ebSOGAWA Hirofumi 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
681da177e4SLinus Torvalds 	if (err)
691da177e4SLinus Torvalds 		return err;
701da177e4SLinus Torvalds 	if (phys) {
711da177e4SLinus Torvalds 		map_bh(bh_result, sb, phys);
72e5174baaSOGAWA Hirofumi 		*max_blocks = min(mapped_blocks, *max_blocks);
731da177e4SLinus Torvalds 		return 0;
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 	if (!create)
761da177e4SLinus Torvalds 		return 0;
77e5174baaSOGAWA Hirofumi 
781da177e4SLinus Torvalds 	if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
7985c78591SDenis Karpov 		fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
801da177e4SLinus Torvalds 			MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
811da177e4SLinus Torvalds 		return -EIO;
821da177e4SLinus Torvalds 	}
83e5174baaSOGAWA Hirofumi 
84e5174baaSOGAWA Hirofumi 	offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
85e5174baaSOGAWA Hirofumi 	if (!offset) {
86e5174baaSOGAWA Hirofumi 		/* TODO: multiple cluster allocation would be desirable. */
871da177e4SLinus Torvalds 		err = fat_add_cluster(inode);
881da177e4SLinus Torvalds 		if (err)
891da177e4SLinus Torvalds 			return err;
901da177e4SLinus Torvalds 	}
91e5174baaSOGAWA Hirofumi 	/* available blocks on this cluster */
92e5174baaSOGAWA Hirofumi 	mapped_blocks = sbi->sec_per_clus - offset;
93e5174baaSOGAWA Hirofumi 
94e5174baaSOGAWA Hirofumi 	*max_blocks = min(mapped_blocks, *max_blocks);
95e5174baaSOGAWA Hirofumi 	MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
96e5174baaSOGAWA Hirofumi 
972bdf67ebSOGAWA Hirofumi 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
981da177e4SLinus Torvalds 	if (err)
991da177e4SLinus Torvalds 		return err;
1006a1d9805SOGAWA Hirofumi 
101e5174baaSOGAWA Hirofumi 	BUG_ON(!phys);
102e5174baaSOGAWA Hirofumi 	BUG_ON(*max_blocks != mapped_blocks);
1031da177e4SLinus Torvalds 	set_buffer_new(bh_result);
1041da177e4SLinus Torvalds 	map_bh(bh_result, sb, phys);
1051da177e4SLinus Torvalds 
106e5174baaSOGAWA Hirofumi 	return 0;
107e5174baaSOGAWA Hirofumi }
108e5174baaSOGAWA Hirofumi 
109e5174baaSOGAWA Hirofumi static int fat_get_block(struct inode *inode, sector_t iblock,
110e5174baaSOGAWA Hirofumi 			 struct buffer_head *bh_result, int create)
111e5174baaSOGAWA Hirofumi {
1126a1d9805SOGAWA Hirofumi 	struct super_block *sb = inode->i_sb;
1136a1d9805SOGAWA Hirofumi 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1146a1d9805SOGAWA Hirofumi 	int err;
1156a1d9805SOGAWA Hirofumi 
1166a1d9805SOGAWA Hirofumi 	err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
1176a1d9805SOGAWA Hirofumi 	if (err)
1186a1d9805SOGAWA Hirofumi 		return err;
1196a1d9805SOGAWA Hirofumi 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1206a1d9805SOGAWA Hirofumi 	return 0;
121e5174baaSOGAWA Hirofumi }
122e5174baaSOGAWA Hirofumi 
1231da177e4SLinus Torvalds static int fat_writepage(struct page *page, struct writeback_control *wbc)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	return block_write_full_page(page, fat_get_block, wbc);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128a5425d29SOGAWA Hirofumi static int fat_writepages(struct address_space *mapping,
129a5425d29SOGAWA Hirofumi 			  struct writeback_control *wbc)
130a5425d29SOGAWA Hirofumi {
131a5425d29SOGAWA Hirofumi 	return mpage_writepages(mapping, wbc, fat_get_block);
132a5425d29SOGAWA Hirofumi }
133a5425d29SOGAWA Hirofumi 
1341da177e4SLinus Torvalds static int fat_readpage(struct file *file, struct page *page)
1351da177e4SLinus Torvalds {
136a5425d29SOGAWA Hirofumi 	return mpage_readpage(page, fat_get_block);
137a5425d29SOGAWA Hirofumi }
138a5425d29SOGAWA Hirofumi 
139a5425d29SOGAWA Hirofumi static int fat_readpages(struct file *file, struct address_space *mapping,
140a5425d29SOGAWA Hirofumi 			 struct list_head *pages, unsigned nr_pages)
141a5425d29SOGAWA Hirofumi {
142a5425d29SOGAWA Hirofumi 	return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
145459f6ed3Snpiggin@suse.de static void fat_write_failed(struct address_space *mapping, loff_t to)
146459f6ed3Snpiggin@suse.de {
147459f6ed3Snpiggin@suse.de 	struct inode *inode = mapping->host;
148459f6ed3Snpiggin@suse.de 
149459f6ed3Snpiggin@suse.de 	if (to > inode->i_size) {
150459f6ed3Snpiggin@suse.de 		truncate_pagecache(inode, to, inode->i_size);
151459f6ed3Snpiggin@suse.de 		fat_truncate_blocks(inode, inode->i_size);
152459f6ed3Snpiggin@suse.de 	}
153459f6ed3Snpiggin@suse.de }
154459f6ed3Snpiggin@suse.de 
155d7777a25SNick Piggin static int fat_write_begin(struct file *file, struct address_space *mapping,
156d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
157d7777a25SNick Piggin 			struct page **pagep, void **fsdata)
1581da177e4SLinus Torvalds {
159459f6ed3Snpiggin@suse.de 	int err;
160459f6ed3Snpiggin@suse.de 
161d7777a25SNick Piggin 	*pagep = NULL;
162282dc178SChristoph Hellwig 	err = cont_write_begin(file, mapping, pos, len, flags,
163459f6ed3Snpiggin@suse.de 				pagep, fsdata, fat_get_block,
164d7777a25SNick Piggin 				&MSDOS_I(mapping->host)->mmu_private);
165459f6ed3Snpiggin@suse.de 	if (err < 0)
166459f6ed3Snpiggin@suse.de 		fat_write_failed(mapping, pos + len);
167459f6ed3Snpiggin@suse.de 	return err;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
170d7777a25SNick Piggin static int fat_write_end(struct file *file, struct address_space *mapping,
171d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
172d7777a25SNick Piggin 			struct page *pagep, void *fsdata)
173ef402268SOGAWA Hirofumi {
174d7777a25SNick Piggin 	struct inode *inode = mapping->host;
175d7777a25SNick Piggin 	int err;
176d7777a25SNick Piggin 	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
177459f6ed3Snpiggin@suse.de 	if (err < len)
178459f6ed3Snpiggin@suse.de 		fat_write_failed(mapping, pos + len);
179d7777a25SNick Piggin 	if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
180ef402268SOGAWA Hirofumi 		inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
181ef402268SOGAWA Hirofumi 		MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
182ef402268SOGAWA Hirofumi 		mark_inode_dirty(inode);
183ef402268SOGAWA Hirofumi 	}
184ef402268SOGAWA Hirofumi 	return err;
185ef402268SOGAWA Hirofumi }
186ef402268SOGAWA Hirofumi 
187e5174baaSOGAWA Hirofumi static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
188e5174baaSOGAWA Hirofumi 			     const struct iovec *iov,
189e5174baaSOGAWA Hirofumi 			     loff_t offset, unsigned long nr_segs)
190e5174baaSOGAWA Hirofumi {
191e5174baaSOGAWA Hirofumi 	struct file *file = iocb->ki_filp;
192459f6ed3Snpiggin@suse.de 	struct address_space *mapping = file->f_mapping;
193459f6ed3Snpiggin@suse.de 	struct inode *inode = mapping->host;
194459f6ed3Snpiggin@suse.de 	ssize_t ret;
195e5174baaSOGAWA Hirofumi 
196e5174baaSOGAWA Hirofumi 	if (rw == WRITE) {
197e5174baaSOGAWA Hirofumi 		/*
1984e02ed4bSNick Piggin 		 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
199e5174baaSOGAWA Hirofumi 		 * so we need to update the ->mmu_private to block boundary.
200e5174baaSOGAWA Hirofumi 		 *
201e5174baaSOGAWA Hirofumi 		 * But we must fill the remaining area or hole by nul for
202e5174baaSOGAWA Hirofumi 		 * updating ->mmu_private.
20394412a96SOGAWA Hirofumi 		 *
20494412a96SOGAWA Hirofumi 		 * Return 0, and fallback to normal buffered write.
205e5174baaSOGAWA Hirofumi 		 */
206e5174baaSOGAWA Hirofumi 		loff_t size = offset + iov_length(iov, nr_segs);
207e5174baaSOGAWA Hirofumi 		if (MSDOS_I(inode)->mmu_private < size)
20894412a96SOGAWA Hirofumi 			return 0;
209e5174baaSOGAWA Hirofumi 	}
210e5174baaSOGAWA Hirofumi 
211e5174baaSOGAWA Hirofumi 	/*
212e5174baaSOGAWA Hirofumi 	 * FAT need to use the DIO_LOCKING for avoiding the race
213e5174baaSOGAWA Hirofumi 	 * condition of fat_get_block() and ->truncate().
214e5174baaSOGAWA Hirofumi 	 */
215aacfc19cSChristoph Hellwig 	ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
216aacfc19cSChristoph Hellwig 				 fat_get_block);
217459f6ed3Snpiggin@suse.de 	if (ret < 0 && (rw & WRITE))
218459f6ed3Snpiggin@suse.de 		fat_write_failed(mapping, offset + iov_length(iov, nr_segs));
219459f6ed3Snpiggin@suse.de 
220459f6ed3Snpiggin@suse.de 	return ret;
221e5174baaSOGAWA Hirofumi }
222e5174baaSOGAWA Hirofumi 
2231da177e4SLinus Torvalds static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
2241da177e4SLinus Torvalds {
225fa93ca18SOGAWA Hirofumi 	sector_t blocknr;
226fa93ca18SOGAWA Hirofumi 
227fa93ca18SOGAWA Hirofumi 	/* fat_get_cluster() assumes the requested blocknr isn't truncated. */
22858268691SChristoph Hellwig 	down_read(&MSDOS_I(mapping->host)->truncate_lock);
229fa93ca18SOGAWA Hirofumi 	blocknr = generic_block_bmap(mapping, block, fat_get_block);
23058268691SChristoph Hellwig 	up_read(&MSDOS_I(mapping->host)->truncate_lock);
231fa93ca18SOGAWA Hirofumi 
232fa93ca18SOGAWA Hirofumi 	return blocknr;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
235f5e54d6eSChristoph Hellwig static const struct address_space_operations fat_aops = {
2361da177e4SLinus Torvalds 	.readpage	= fat_readpage,
237a5425d29SOGAWA Hirofumi 	.readpages	= fat_readpages,
2381da177e4SLinus Torvalds 	.writepage	= fat_writepage,
239a5425d29SOGAWA Hirofumi 	.writepages	= fat_writepages,
240d7777a25SNick Piggin 	.write_begin	= fat_write_begin,
241d7777a25SNick Piggin 	.write_end	= fat_write_end,
242e5174baaSOGAWA Hirofumi 	.direct_IO	= fat_direct_IO,
2431da177e4SLinus Torvalds 	.bmap		= _fat_bmap
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /*
2471da177e4SLinus Torvalds  * New FAT inode stuff. We do the following:
2481da177e4SLinus Torvalds  *	a) i_ino is constant and has nothing with on-disk location.
2491da177e4SLinus Torvalds  *	b) FAT manages its own cache of directory entries.
2501da177e4SLinus Torvalds  *	c) *This* cache is indexed by on-disk location.
2511da177e4SLinus Torvalds  *	d) inode has an associated directory entry, all right, but
2521da177e4SLinus Torvalds  *		it may be unhashed.
2531da177e4SLinus Torvalds  *	e) currently entries are stored within struct inode. That should
2541da177e4SLinus Torvalds  *		change.
2551da177e4SLinus Torvalds  *	f) we deal with races in the following way:
2561da177e4SLinus Torvalds  *		1. readdir() and lookup() do FAT-dir-cache lookup.
2571da177e4SLinus Torvalds  *		2. rename() unhashes the F-d-c entry and rehashes it in
2581da177e4SLinus Torvalds  *			a new place.
2591da177e4SLinus Torvalds  *		3. unlink() and rmdir() unhash F-d-c entry.
2601da177e4SLinus Torvalds  *		4. fat_write_inode() checks whether the thing is unhashed.
2611da177e4SLinus Torvalds  *			If it is we silently return. If it isn't we do bread(),
2621da177e4SLinus Torvalds  *			check if the location is still valid and retry if it
2631da177e4SLinus Torvalds  *			isn't. Otherwise we do changes.
2641da177e4SLinus Torvalds  *		5. Spinlock is used to protect hash/unhash/location check/lookup
265deee3ce4SAl Viro  *		6. fat_evict_inode() unhashes the F-d-c entry.
2661da177e4SLinus Torvalds  *		7. lookup() and readdir() do igrab() if they find a F-d-c entry
2671da177e4SLinus Torvalds  *			and consider negative result as cache miss.
2681da177e4SLinus Torvalds  */
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static void fat_hash_init(struct super_block *sb)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2731da177e4SLinus Torvalds 	int i;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	spin_lock_init(&sbi->inode_hash_lock);
2761da177e4SLinus Torvalds 	for (i = 0; i < FAT_HASH_SIZE; i++)
2771da177e4SLinus Torvalds 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280d3dfa822SOGAWA Hirofumi static inline unsigned long fat_hash(loff_t i_pos)
2811da177e4SLinus Torvalds {
282d3dfa822SOGAWA Hirofumi 	return hash_32(i_pos, FAT_HASH_BITS);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2857669e8fbSSteven J. Magnani static void dir_hash_init(struct super_block *sb)
2867669e8fbSSteven J. Magnani {
2877669e8fbSSteven J. Magnani 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2887669e8fbSSteven J. Magnani 	int i;
2897669e8fbSSteven J. Magnani 
2907669e8fbSSteven J. Magnani 	spin_lock_init(&sbi->dir_hash_lock);
2917669e8fbSSteven J. Magnani 	for (i = 0; i < FAT_HASH_SIZE; i++)
2927669e8fbSSteven J. Magnani 		INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
2937669e8fbSSteven J. Magnani }
2947669e8fbSSteven J. Magnani 
2951da177e4SLinus Torvalds void fat_attach(struct inode *inode, loff_t i_pos)
2961da177e4SLinus Torvalds {
297d3dfa822SOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
2987669e8fbSSteven J. Magnani 
2997669e8fbSSteven J. Magnani 	if (inode->i_ino != MSDOS_ROOT_INO) {
3007669e8fbSSteven J. Magnani 		struct hlist_head *head =   sbi->inode_hashtable
3017669e8fbSSteven J. Magnani 					  + fat_hash(i_pos);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 		spin_lock(&sbi->inode_hash_lock);
3041da177e4SLinus Torvalds 		MSDOS_I(inode)->i_pos = i_pos;
305d3dfa822SOGAWA Hirofumi 		hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
3061da177e4SLinus Torvalds 		spin_unlock(&sbi->inode_hash_lock);
3071da177e4SLinus Torvalds 	}
3087669e8fbSSteven J. Magnani 
3097669e8fbSSteven J. Magnani 	/* If NFS support is enabled, cache the mapping of start cluster
3107669e8fbSSteven J. Magnani 	 * to directory inode. This is used during reconnection of
3117669e8fbSSteven J. Magnani 	 * dentries to the filesystem root.
3127669e8fbSSteven J. Magnani 	 */
3137669e8fbSSteven J. Magnani 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
3147669e8fbSSteven J. Magnani 		struct hlist_head *d_head = sbi->dir_hashtable;
3157669e8fbSSteven J. Magnani 		d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
3167669e8fbSSteven J. Magnani 
3177669e8fbSSteven J. Magnani 		spin_lock(&sbi->dir_hash_lock);
3187669e8fbSSteven J. Magnani 		hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
3197669e8fbSSteven J. Magnani 		spin_unlock(&sbi->dir_hash_lock);
3207669e8fbSSteven J. Magnani 	}
3217669e8fbSSteven J. Magnani }
3227c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_attach);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds void fat_detach(struct inode *inode)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3271da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
3281da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
3291da177e4SLinus Torvalds 	hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
3301da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
3317669e8fbSSteven J. Magnani 
3327669e8fbSSteven J. Magnani 	if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
3337669e8fbSSteven J. Magnani 		spin_lock(&sbi->dir_hash_lock);
3347669e8fbSSteven J. Magnani 		hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
3357669e8fbSSteven J. Magnani 		spin_unlock(&sbi->dir_hash_lock);
3367669e8fbSSteven J. Magnani 	}
3371da177e4SLinus Torvalds }
3387c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_detach);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
343d3dfa822SOGAWA Hirofumi 	struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
3441da177e4SLinus Torvalds 	struct hlist_node *_p;
3451da177e4SLinus Torvalds 	struct msdos_inode_info *i;
3461da177e4SLinus Torvalds 	struct inode *inode = NULL;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
3491da177e4SLinus Torvalds 	hlist_for_each_entry(i, _p, head, i_fat_hash) {
3501da177e4SLinus Torvalds 		BUG_ON(i->vfs_inode.i_sb != sb);
3511da177e4SLinus Torvalds 		if (i->i_pos != i_pos)
3521da177e4SLinus Torvalds 			continue;
3531da177e4SLinus Torvalds 		inode = igrab(&i->vfs_inode);
3541da177e4SLinus Torvalds 		if (inode)
3551da177e4SLinus Torvalds 			break;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
3581da177e4SLinus Torvalds 	return inode;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds static int is_exec(unsigned char *extension)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	unsigned char *exe_extensions = "EXECOMBAT", *walk;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	for (walk = exe_extensions; *walk; walk += 3)
3661da177e4SLinus Torvalds 		if (!strncmp(extension, walk, 3))
3671da177e4SLinus Torvalds 			return 1;
3681da177e4SLinus Torvalds 	return 0;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static int fat_calc_dir_size(struct inode *inode)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3741da177e4SLinus Torvalds 	int ret, fclus, dclus;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	inode->i_size = 0;
3771da177e4SLinus Torvalds 	if (MSDOS_I(inode)->i_start == 0)
3781da177e4SLinus Torvalds 		return 0;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
3811da177e4SLinus Torvalds 	if (ret < 0)
3821da177e4SLinus Torvalds 		return ret;
3831da177e4SLinus Torvalds 	inode->i_size = (fclus + 1) << sbi->cluster_bits;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	return 0;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /* doesn't deal with root inode */
3891da177e4SLinus Torvalds static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3921da177e4SLinus Torvalds 	int error;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
3951da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
3961da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
3971da177e4SLinus Torvalds 	inode->i_version++;
3981da177e4SLinus Torvalds 	inode->i_generation = get_seconds();
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
4011da177e4SLinus Torvalds 		inode->i_generation &= ~1;
4029c0aa1b8SOGAWA Hirofumi 		inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
4031da177e4SLinus Torvalds 		inode->i_op = sbi->dir_ops;
4041da177e4SLinus Torvalds 		inode->i_fop = &fat_dir_operations;
4051da177e4SLinus Torvalds 
406a943ed71SSteven J. Magnani 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
4071da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
4081da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
4091da177e4SLinus Torvalds 		if (error < 0)
4101da177e4SLinus Torvalds 			return error;
4111da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
4121da177e4SLinus Torvalds 
413bfe86848SMiklos Szeredi 		set_nlink(inode, fat_subdirs(inode));
4141da177e4SLinus Torvalds 	} else { /* not a directory */
4151da177e4SLinus Torvalds 		inode->i_generation |= 1;
4169c0aa1b8SOGAWA Hirofumi 		inode->i_mode = fat_make_mode(sbi, de->attr,
4179aacd599SOGAWA Hirofumi 			((sbi->options.showexec && !is_exec(de->name + 8))
4189c0aa1b8SOGAWA Hirofumi 			 ? S_IRUGO|S_IWUGO : S_IRWXUGO));
419a943ed71SSteven J. Magnani 		MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
4221da177e4SLinus Torvalds 		inode->i_size = le32_to_cpu(de->size);
4231da177e4SLinus Torvalds 		inode->i_op = &fat_file_inode_operations;
4241da177e4SLinus Torvalds 		inode->i_fop = &fat_file_operations;
4251da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &fat_aops;
4261da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 	if (de->attr & ATTR_SYS) {
4291da177e4SLinus Torvalds 		if (sbi->options.sys_immutable)
4301da177e4SLinus Torvalds 			inode->i_flags |= S_IMMUTABLE;
4311da177e4SLinus Torvalds 	}
4329c0aa1b8SOGAWA Hirofumi 	fat_save_attrs(inode, de->attr);
4339c0aa1b8SOGAWA Hirofumi 
4341da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
4351da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
4367decd1cbSOGAWA Hirofumi 
4377decd1cbSOGAWA Hirofumi 	fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
4381da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
4397decd1cbSOGAWA Hirofumi 		fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
4407decd1cbSOGAWA Hirofumi 				  de->cdate, de->ctime_cs);
4417decd1cbSOGAWA Hirofumi 		fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
4421da177e4SLinus Torvalds 	} else
44362a36c43SStephane Kardas 		inode->i_ctime = inode->i_atime = inode->i_mtime;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	return 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds struct inode *fat_build_inode(struct super_block *sb,
4491da177e4SLinus Torvalds 			struct msdos_dir_entry *de, loff_t i_pos)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct inode *inode;
4521da177e4SLinus Torvalds 	int err;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	inode = fat_iget(sb, i_pos);
4551da177e4SLinus Torvalds 	if (inode)
4561da177e4SLinus Torvalds 		goto out;
4571da177e4SLinus Torvalds 	inode = new_inode(sb);
4581da177e4SLinus Torvalds 	if (!inode) {
4591da177e4SLinus Torvalds 		inode = ERR_PTR(-ENOMEM);
4601da177e4SLinus Torvalds 		goto out;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
4631da177e4SLinus Torvalds 	inode->i_version = 1;
4641da177e4SLinus Torvalds 	err = fat_fill_inode(inode, de);
4651da177e4SLinus Torvalds 	if (err) {
4661da177e4SLinus Torvalds 		iput(inode);
4671da177e4SLinus Torvalds 		inode = ERR_PTR(err);
4681da177e4SLinus Torvalds 		goto out;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 	fat_attach(inode, i_pos);
4711da177e4SLinus Torvalds 	insert_inode_hash(inode);
4721da177e4SLinus Torvalds out:
4731da177e4SLinus Torvalds 	return inode;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4767c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_build_inode);
4771da177e4SLinus Torvalds 
478deee3ce4SAl Viro static void fat_evict_inode(struct inode *inode)
4791da177e4SLinus Torvalds {
480fef26658SMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
481deee3ce4SAl Viro 	if (!inode->i_nlink) {
4821da177e4SLinus Torvalds 		inode->i_size = 0;
483459f6ed3Snpiggin@suse.de 		fat_truncate_blocks(inode, 0);
4841da177e4SLinus Torvalds 	}
485deee3ce4SAl Viro 	invalidate_inode_buffers(inode);
486dbd5768fSJan Kara 	clear_inode(inode);
4871da177e4SLinus Torvalds 	fat_cache_inval_inode(inode);
488a993b542SOGAWA Hirofumi 	fat_detach(inode);
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
491a6bf6b21SOGAWA Hirofumi static void fat_put_super(struct super_block *sb)
492a6bf6b21SOGAWA Hirofumi {
493a6bf6b21SOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
4941da177e4SLinus Torvalds 
495020ac5b6SArtem Bityutskiy 	iput(sbi->fsinfo_inode);
496b522412aSAl Viro 	iput(sbi->fat_inode);
497b522412aSAl Viro 
4981da177e4SLinus Torvalds 	unload_nls(sbi->nls_disk);
4991da177e4SLinus Torvalds 	unload_nls(sbi->nls_io);
5006d729e44SThomas Gleixner 
5016d729e44SThomas Gleixner 	if (sbi->options.iocharset != fat_default_iocharset)
5021da177e4SLinus Torvalds 		kfree(sbi->options.iocharset);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
5051da177e4SLinus Torvalds 	kfree(sbi);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
508e18b890bSChristoph Lameter static struct kmem_cache *fat_inode_cachep;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static struct inode *fat_alloc_inode(struct super_block *sb)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	struct msdos_inode_info *ei;
5138f593427SLinus Torvalds 	ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
5141da177e4SLinus Torvalds 	if (!ei)
5151da177e4SLinus Torvalds 		return NULL;
51658268691SChristoph Hellwig 
51758268691SChristoph Hellwig 	init_rwsem(&ei->truncate_lock);
5181da177e4SLinus Torvalds 	return &ei->vfs_inode;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
521fa0d7e3dSNick Piggin static void fat_i_callback(struct rcu_head *head)
522fa0d7e3dSNick Piggin {
523fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
524fa0d7e3dSNick Piggin 	kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
525fa0d7e3dSNick Piggin }
526fa0d7e3dSNick Piggin 
5271da177e4SLinus Torvalds static void fat_destroy_inode(struct inode *inode)
5281da177e4SLinus Torvalds {
529fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, fat_i_callback);
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
53251cc5068SAlexey Dobriyan static void init_once(void *foo)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	spin_lock_init(&ei->cache_lru_lock);
5371da177e4SLinus Torvalds 	ei->nr_caches = 0;
5381da177e4SLinus Torvalds 	ei->cache_valid_id = FAT_CACHE_VALID + 1;
5391da177e4SLinus Torvalds 	INIT_LIST_HEAD(&ei->cache_lru);
5401da177e4SLinus Torvalds 	INIT_HLIST_NODE(&ei->i_fat_hash);
5417669e8fbSSteven J. Magnani 	INIT_HLIST_NODE(&ei->i_dir_hash);
5421da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static int __init fat_init_inodecache(void)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	fat_inode_cachep = kmem_cache_create("fat_inode_cache",
5481da177e4SLinus Torvalds 					     sizeof(struct msdos_inode_info),
549fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
550fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
55120c2df83SPaul Mundt 					     init_once);
5521da177e4SLinus Torvalds 	if (fat_inode_cachep == NULL)
5531da177e4SLinus Torvalds 		return -ENOMEM;
5541da177e4SLinus Torvalds 	return 0;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds static void __exit fat_destroy_inodecache(void)
5581da177e4SLinus Torvalds {
5598c0a8537SKirill A. Shutemov 	/*
5608c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
5618c0a8537SKirill A. Shutemov 	 * destroy cache.
5628c0a8537SKirill A. Shutemov 	 */
5638c0a8537SKirill A. Shutemov 	rcu_barrier();
5641a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(fat_inode_cachep);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static int fat_remount(struct super_block *sb, int *flags, char *data)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
5701da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
5711da177e4SLinus Torvalds 	return 0;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574726c3342SDavid Howells static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
5751da177e4SLinus Torvalds {
576aac49b75SColy Li 	struct super_block *sb = dentry->d_sb;
577aac49b75SColy Li 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
578aac49b75SColy Li 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* If the count of free cluster is still unknown, counts it here. */
581606e423eSOGAWA Hirofumi 	if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
582726c3342SDavid Howells 		int err = fat_count_free_clusters(dentry->d_sb);
5831da177e4SLinus Torvalds 		if (err)
5841da177e4SLinus Torvalds 			return err;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
587726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
5881da177e4SLinus Torvalds 	buf->f_bsize = sbi->cluster_size;
5891da177e4SLinus Torvalds 	buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
5901da177e4SLinus Torvalds 	buf->f_bfree = sbi->free_clusters;
5911da177e4SLinus Torvalds 	buf->f_bavail = sbi->free_clusters;
592aac49b75SColy Li 	buf->f_fsid.val[0] = (u32)id;
593aac49b75SColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
594f68e542fSOGAWA Hirofumi 	buf->f_namelen =
595f68e542fSOGAWA Hirofumi 		(sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	return 0;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6007669e8fbSSteven J. Magnani static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
6017669e8fbSSteven J. Magnani 				    struct inode *inode)
6027669e8fbSSteven J. Magnani {
6037669e8fbSSteven J. Magnani 	loff_t i_pos;
6047669e8fbSSteven J. Magnani #if BITS_PER_LONG == 32
6057669e8fbSSteven J. Magnani 	spin_lock(&sbi->inode_hash_lock);
6067669e8fbSSteven J. Magnani #endif
6077669e8fbSSteven J. Magnani 	i_pos = MSDOS_I(inode)->i_pos;
6087669e8fbSSteven J. Magnani #if BITS_PER_LONG == 32
6097669e8fbSSteven J. Magnani 	spin_unlock(&sbi->inode_hash_lock);
6107669e8fbSSteven J. Magnani #endif
6117669e8fbSSteven J. Magnani 	return i_pos;
6127669e8fbSSteven J. Magnani }
6137669e8fbSSteven J. Magnani 
614a9185b41SChristoph Hellwig static int __fat_write_inode(struct inode *inode, int wait)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
6171da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
6181da177e4SLinus Torvalds 	struct buffer_head *bh;
6191da177e4SLinus Torvalds 	struct msdos_dir_entry *raw_entry;
6201da177e4SLinus Torvalds 	loff_t i_pos;
6215f22ca9bSLinus Torvalds 	int err;
6221da177e4SLinus Torvalds 
6239ca59f4cSOGAWA Hirofumi 	if (inode->i_ino == MSDOS_ROOT_INO)
6249ca59f4cSOGAWA Hirofumi 		return 0;
6259ca59f4cSOGAWA Hirofumi 
6261da177e4SLinus Torvalds retry:
6279ca59f4cSOGAWA Hirofumi 	i_pos = fat_i_pos_read(sbi, inode);
6289ca59f4cSOGAWA Hirofumi 	if (!i_pos)
6291da177e4SLinus Torvalds 		return 0;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
6321da177e4SLinus Torvalds 	if (!bh) {
633869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "unable to read inode block "
634869f58c0SAlexey Fisher 		       "for updating (i_pos %lld)", i_pos);
6355f22ca9bSLinus Torvalds 		return -EIO;
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
6381da177e4SLinus Torvalds 	if (i_pos != MSDOS_I(inode)->i_pos) {
6391da177e4SLinus Torvalds 		spin_unlock(&sbi->inode_hash_lock);
6401da177e4SLinus Torvalds 		brelse(bh);
6411da177e4SLinus Torvalds 		goto retry;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
6451da177e4SLinus Torvalds 	    [i_pos & (sbi->dir_per_block - 1)];
6461da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode))
6471da177e4SLinus Torvalds 		raw_entry->size = 0;
6481da177e4SLinus Torvalds 	else
6491da177e4SLinus Torvalds 		raw_entry->size = cpu_to_le32(inode->i_size);
6509c0aa1b8SOGAWA Hirofumi 	raw_entry->attr = fat_make_attrs(inode);
651a943ed71SSteven J. Magnani 	fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
6527decd1cbSOGAWA Hirofumi 	fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
6537decd1cbSOGAWA Hirofumi 			  &raw_entry->date, NULL);
6541da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
65562a36c43SStephane Kardas 		__le16 atime;
6567decd1cbSOGAWA Hirofumi 		fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
6577decd1cbSOGAWA Hirofumi 				  &raw_entry->cdate, &raw_entry->ctime_cs);
6587decd1cbSOGAWA Hirofumi 		fat_time_unix2fat(sbi, &inode->i_atime, &atime,
6597decd1cbSOGAWA Hirofumi 				  &raw_entry->adate, NULL);
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
6621da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
6635f22ca9bSLinus Torvalds 	err = 0;
6641da177e4SLinus Torvalds 	if (wait)
6651da177e4SLinus Torvalds 		err = sync_dirty_buffer(bh);
6661da177e4SLinus Torvalds 	brelse(bh);
6671da177e4SLinus Torvalds 	return err;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
670a9185b41SChristoph Hellwig static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
671a9185b41SChristoph Hellwig {
67278491189SArtem Bityutskiy 	int err;
67378491189SArtem Bityutskiy 
67478491189SArtem Bityutskiy 	if (inode->i_ino == MSDOS_FSINFO_INO) {
67578491189SArtem Bityutskiy 		struct super_block *sb = inode->i_sb;
67678491189SArtem Bityutskiy 
677e40b34c7SMarco Stornelli 		mutex_lock(&MSDOS_SB(sb)->s_lock);
67878491189SArtem Bityutskiy 		err = fat_clusters_flush(sb);
679e40b34c7SMarco Stornelli 		mutex_unlock(&MSDOS_SB(sb)->s_lock);
68078491189SArtem Bityutskiy 	} else
68178491189SArtem Bityutskiy 		err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
68278491189SArtem Bityutskiy 
68378491189SArtem Bityutskiy 	return err;
684a9185b41SChristoph Hellwig }
685a9185b41SChristoph Hellwig 
6861da177e4SLinus Torvalds int fat_sync_inode(struct inode *inode)
6871da177e4SLinus Torvalds {
688a9185b41SChristoph Hellwig 	return __fat_write_inode(inode, 1);
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6917c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_sync_inode);
6921da177e4SLinus Torvalds 
69334c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root);
694ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fat_sops = {
6951da177e4SLinus Torvalds 	.alloc_inode	= fat_alloc_inode,
6961da177e4SLinus Torvalds 	.destroy_inode	= fat_destroy_inode,
6971da177e4SLinus Torvalds 	.write_inode	= fat_write_inode,
698deee3ce4SAl Viro 	.evict_inode	= fat_evict_inode,
6991da177e4SLinus Torvalds 	.put_super	= fat_put_super,
7001da177e4SLinus Torvalds 	.statfs		= fat_statfs,
7011da177e4SLinus Torvalds 	.remount_fs	= fat_remount,
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	.show_options	= fat_show_options,
7041da177e4SLinus Torvalds };
7051da177e4SLinus Torvalds 
70639655164SChristoph Hellwig static const struct export_operations fat_export_ops = {
7071305edadSChristoph Hellwig 	.fh_to_dentry	= fat_fh_to_dentry,
7087669e8fbSSteven J. Magnani 	.fh_to_parent	= fat_fh_to_parent,
7091da177e4SLinus Torvalds 	.get_parent	= fat_get_parent,
7101da177e4SLinus Torvalds };
7111da177e4SLinus Torvalds 
71234c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root)
7131da177e4SLinus Torvalds {
71434c80b1dSAl Viro 	struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
7151da177e4SLinus Torvalds 	struct fat_mount_options *opts = &sbi->options;
7161da177e4SLinus Torvalds 	int isvfat = opts->isvfat;
7171da177e4SLinus Torvalds 
718170782ebSEric W. Biederman 	if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
719170782ebSEric W. Biederman 		seq_printf(m, ",uid=%u",
720170782ebSEric W. Biederman 				from_kuid_munged(&init_user_ns, opts->fs_uid));
721170782ebSEric W. Biederman 	if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
722170782ebSEric W. Biederman 		seq_printf(m, ",gid=%u",
723170782ebSEric W. Biederman 				from_kgid_munged(&init_user_ns, opts->fs_gid));
7241da177e4SLinus Torvalds 	seq_printf(m, ",fmask=%04o", opts->fs_fmask);
7251da177e4SLinus Torvalds 	seq_printf(m, ",dmask=%04o", opts->fs_dmask);
7261ae43f82SOGAWA Hirofumi 	if (opts->allow_utime)
7271ae43f82SOGAWA Hirofumi 		seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
7281da177e4SLinus Torvalds 	if (sbi->nls_disk)
729c6c20372SDave Reisner 		/* strip "cp" prefix from displayed option */
730c6c20372SDave Reisner 		seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
7311da177e4SLinus Torvalds 	if (isvfat) {
7321da177e4SLinus Torvalds 		if (sbi->nls_io)
7331da177e4SLinus Torvalds 			seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 		switch (opts->shortname) {
7361da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
7371da177e4SLinus Torvalds 			seq_puts(m, ",shortname=win95");
7381da177e4SLinus Torvalds 			break;
7391da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
7401da177e4SLinus Torvalds 			seq_puts(m, ",shortname=winnt");
7411da177e4SLinus Torvalds 			break;
7421da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
7431da177e4SLinus Torvalds 			seq_puts(m, ",shortname=mixed");
7441da177e4SLinus Torvalds 			break;
7451da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
74695523475SPaul Wise 			seq_puts(m, ",shortname=lower");
7471da177e4SLinus Torvalds 			break;
7481da177e4SLinus Torvalds 		default:
7491da177e4SLinus Torvalds 			seq_puts(m, ",shortname=unknown");
7501da177e4SLinus Torvalds 			break;
7511da177e4SLinus Torvalds 		}
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 	if (opts->name_check != 'n')
7541da177e4SLinus Torvalds 		seq_printf(m, ",check=%c", opts->name_check);
75528ec039cSOGAWA Hirofumi 	if (opts->usefree)
75628ec039cSOGAWA Hirofumi 		seq_puts(m, ",usefree");
7571da177e4SLinus Torvalds 	if (opts->quiet)
7581da177e4SLinus Torvalds 		seq_puts(m, ",quiet");
7597669e8fbSSteven J. Magnani 	if (opts->nfs)
7607669e8fbSSteven J. Magnani 		seq_puts(m, ",nfs");
7611da177e4SLinus Torvalds 	if (opts->showexec)
7621da177e4SLinus Torvalds 		seq_puts(m, ",showexec");
7631da177e4SLinus Torvalds 	if (opts->sys_immutable)
7641da177e4SLinus Torvalds 		seq_puts(m, ",sys_immutable");
7651da177e4SLinus Torvalds 	if (!isvfat) {
7661da177e4SLinus Torvalds 		if (opts->dotsOK)
7671da177e4SLinus Torvalds 			seq_puts(m, ",dotsOK=yes");
7681da177e4SLinus Torvalds 		if (opts->nocase)
7691da177e4SLinus Torvalds 			seq_puts(m, ",nocase");
7701da177e4SLinus Torvalds 	} else {
7711da177e4SLinus Torvalds 		if (opts->utf8)
7721da177e4SLinus Torvalds 			seq_puts(m, ",utf8");
7731da177e4SLinus Torvalds 		if (opts->unicode_xlate)
7741da177e4SLinus Torvalds 			seq_puts(m, ",uni_xlate");
7751da177e4SLinus Torvalds 		if (!opts->numtail)
7761da177e4SLinus Torvalds 			seq_puts(m, ",nonumtail");
777dfc209c0SOGAWA Hirofumi 		if (opts->rodir)
778dfc209c0SOGAWA Hirofumi 			seq_puts(m, ",rodir");
7791da177e4SLinus Torvalds 	}
780dfc209c0SOGAWA Hirofumi 	if (opts->flush)
781c1fca3b6SMiklos Szeredi 		seq_puts(m, ",flush");
78258156c8fSJan Kara 	if (opts->tz_set) {
78358156c8fSJan Kara 		if (opts->time_offset)
78458156c8fSJan Kara 			seq_printf(m, ",time_offset=%d", opts->time_offset);
78558156c8fSJan Kara 		else
786b271e067SJoe Peterson 			seq_puts(m, ",tz=UTC");
78758156c8fSJan Kara 	}
78885c78591SDenis Karpov 	if (opts->errors == FAT_ERRORS_CONT)
78985c78591SDenis Karpov 		seq_puts(m, ",errors=continue");
79085c78591SDenis Karpov 	else if (opts->errors == FAT_ERRORS_PANIC)
79185c78591SDenis Karpov 		seq_puts(m, ",errors=panic");
79285c78591SDenis Karpov 	else
79385c78591SDenis Karpov 		seq_puts(m, ",errors=remount-ro");
794681142f9SChristoph Hellwig 	if (opts->discard)
795681142f9SChristoph Hellwig 		seq_puts(m, ",discard");
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	return 0;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds enum {
8011da177e4SLinus Torvalds 	Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
8021ae43f82SOGAWA Hirofumi 	Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
8031ae43f82SOGAWA Hirofumi 	Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
8041ae43f82SOGAWA Hirofumi 	Opt_immutable, Opt_dots, Opt_nodots,
8051da177e4SLinus Torvalds 	Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
8061da177e4SLinus Torvalds 	Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
8071da177e4SLinus Torvalds 	Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
808d7a83c0fSGeert Uytterhoeven 	Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
80958156c8fSJan Kara 	Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
81058156c8fSJan Kara 	Opt_err,
8111da177e4SLinus Torvalds };
8121da177e4SLinus Torvalds 
813a447c093SSteven Whitehouse static const match_table_t fat_tokens = {
8141da177e4SLinus Torvalds 	{Opt_check_r, "check=relaxed"},
8151da177e4SLinus Torvalds 	{Opt_check_s, "check=strict"},
8161da177e4SLinus Torvalds 	{Opt_check_n, "check=normal"},
8171da177e4SLinus Torvalds 	{Opt_check_r, "check=r"},
8181da177e4SLinus Torvalds 	{Opt_check_s, "check=s"},
8191da177e4SLinus Torvalds 	{Opt_check_n, "check=n"},
8201da177e4SLinus Torvalds 	{Opt_uid, "uid=%u"},
8211da177e4SLinus Torvalds 	{Opt_gid, "gid=%u"},
8221da177e4SLinus Torvalds 	{Opt_umask, "umask=%o"},
8231da177e4SLinus Torvalds 	{Opt_dmask, "dmask=%o"},
8241da177e4SLinus Torvalds 	{Opt_fmask, "fmask=%o"},
8251ae43f82SOGAWA Hirofumi 	{Opt_allow_utime, "allow_utime=%o"},
8261da177e4SLinus Torvalds 	{Opt_codepage, "codepage=%u"},
82728ec039cSOGAWA Hirofumi 	{Opt_usefree, "usefree"},
8281da177e4SLinus Torvalds 	{Opt_nocase, "nocase"},
8291da177e4SLinus Torvalds 	{Opt_quiet, "quiet"},
8301da177e4SLinus Torvalds 	{Opt_showexec, "showexec"},
8311da177e4SLinus Torvalds 	{Opt_debug, "debug"},
8321da177e4SLinus Torvalds 	{Opt_immutable, "sys_immutable"},
83385c78591SDenis Karpov 	{Opt_flush, "flush"},
83485c78591SDenis Karpov 	{Opt_tz_utc, "tz=UTC"},
83558156c8fSJan Kara 	{Opt_time_offset, "time_offset=%d"},
83685c78591SDenis Karpov 	{Opt_err_cont, "errors=continue"},
83785c78591SDenis Karpov 	{Opt_err_panic, "errors=panic"},
83885c78591SDenis Karpov 	{Opt_err_ro, "errors=remount-ro"},
839681142f9SChristoph Hellwig 	{Opt_discard, "discard"},
8407669e8fbSSteven J. Magnani 	{Opt_nfs, "nfs"},
841d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=binary"},
842d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=text"},
843d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=auto"},
844d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=b"},
845d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=t"},
846d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "conv=a"},
847d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "fat=%u"},
848d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "blocksize=%u"},
849d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "cvf_format=%20s"},
850d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "cvf_options=%100s"},
851d7a83c0fSGeert Uytterhoeven 	{Opt_obsolete, "posix"},
852ae78bf9cSChris Mason 	{Opt_err, NULL},
8531da177e4SLinus Torvalds };
854a447c093SSteven Whitehouse static const match_table_t msdos_tokens = {
8551da177e4SLinus Torvalds 	{Opt_nodots, "nodots"},
8561da177e4SLinus Torvalds 	{Opt_nodots, "dotsOK=no"},
8571da177e4SLinus Torvalds 	{Opt_dots, "dots"},
8581da177e4SLinus Torvalds 	{Opt_dots, "dotsOK=yes"},
8591da177e4SLinus Torvalds 	{Opt_err, NULL}
8601da177e4SLinus Torvalds };
861a447c093SSteven Whitehouse static const match_table_t vfat_tokens = {
8621da177e4SLinus Torvalds 	{Opt_charset, "iocharset=%s"},
8631da177e4SLinus Torvalds 	{Opt_shortname_lower, "shortname=lower"},
8641da177e4SLinus Torvalds 	{Opt_shortname_win95, "shortname=win95"},
8651da177e4SLinus Torvalds 	{Opt_shortname_winnt, "shortname=winnt"},
8661da177e4SLinus Torvalds 	{Opt_shortname_mixed, "shortname=mixed"},
8671da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=0"},		/* 0 or no or false */
8681da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=no"},
8691da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=false"},
8701da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=1"},		/* empty or 1 or yes or true */
8711da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=yes"},
8721da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=true"},
8731da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8"},
8741da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=0"},		/* 0 or no or false */
8751da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=no"},
8761da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=false"},
8771da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=1"},	/* empty or 1 or yes or true */
8781da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=yes"},
8791da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=true"},
8801da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate"},
8811da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=0"},	/* 0 or no or false */
8821da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=no"},
8831da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=false"},
8841da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=1"},	/* empty or 1 or yes or true */
8851da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=yes"},
8861da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=true"},
8871da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail"},
888dfc209c0SOGAWA Hirofumi 	{Opt_rodir, "rodir"},
8891da177e4SLinus Torvalds 	{Opt_err, NULL}
8901da177e4SLinus Torvalds };
8911da177e4SLinus Torvalds 
892869f58c0SAlexey Fisher static int parse_options(struct super_block *sb, char *options, int is_vfat,
893869f58c0SAlexey Fisher 			 int silent, int *debug, struct fat_mount_options *opts)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	char *p;
8961da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
8971da177e4SLinus Torvalds 	int option;
8981da177e4SLinus Torvalds 	char *iocharset;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	opts->isvfat = is_vfat;
9011da177e4SLinus Torvalds 
902f0ce7ee3SDavid Howells 	opts->fs_uid = current_uid();
903f0ce7ee3SDavid Howells 	opts->fs_gid = current_gid();
9043e107603SOGAWA Hirofumi 	opts->fs_fmask = opts->fs_dmask = current_umask();
9051ae43f82SOGAWA Hirofumi 	opts->allow_utime = -1;
9061da177e4SLinus Torvalds 	opts->codepage = fat_default_codepage;
9071da177e4SLinus Torvalds 	opts->iocharset = fat_default_iocharset;
908dfc209c0SOGAWA Hirofumi 	if (is_vfat) {
90995523475SPaul Wise 		opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
910dfc209c0SOGAWA Hirofumi 		opts->rodir = 0;
911dfc209c0SOGAWA Hirofumi 	} else {
9121da177e4SLinus Torvalds 		opts->shortname = 0;
913dfc209c0SOGAWA Hirofumi 		opts->rodir = 1;
914dfc209c0SOGAWA Hirofumi 	}
9151da177e4SLinus Torvalds 	opts->name_check = 'n';
9161da177e4SLinus Torvalds 	opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
9171da177e4SLinus Torvalds 	opts->utf8 = opts->unicode_xlate = 0;
9181da177e4SLinus Torvalds 	opts->numtail = 1;
91928ec039cSOGAWA Hirofumi 	opts->usefree = opts->nocase = 0;
92058156c8fSJan Kara 	opts->tz_set = 0;
9217669e8fbSSteven J. Magnani 	opts->nfs = 0;
92285c78591SDenis Karpov 	opts->errors = FAT_ERRORS_RO;
9231da177e4SLinus Torvalds 	*debug = 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (!options)
9268d44d974SOGAWA Hirofumi 		goto out;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
9291da177e4SLinus Torvalds 		int token;
9301da177e4SLinus Torvalds 		if (!*p)
9311da177e4SLinus Torvalds 			continue;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 		token = match_token(p, fat_tokens, args);
9341da177e4SLinus Torvalds 		if (token == Opt_err) {
9351da177e4SLinus Torvalds 			if (is_vfat)
9361da177e4SLinus Torvalds 				token = match_token(p, vfat_tokens, args);
9371da177e4SLinus Torvalds 			else
9381da177e4SLinus Torvalds 				token = match_token(p, msdos_tokens, args);
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 		switch (token) {
9411da177e4SLinus Torvalds 		case Opt_check_s:
9421da177e4SLinus Torvalds 			opts->name_check = 's';
9431da177e4SLinus Torvalds 			break;
9441da177e4SLinus Torvalds 		case Opt_check_r:
9451da177e4SLinus Torvalds 			opts->name_check = 'r';
9461da177e4SLinus Torvalds 			break;
9471da177e4SLinus Torvalds 		case Opt_check_n:
9481da177e4SLinus Torvalds 			opts->name_check = 'n';
9491da177e4SLinus Torvalds 			break;
95028ec039cSOGAWA Hirofumi 		case Opt_usefree:
95128ec039cSOGAWA Hirofumi 			opts->usefree = 1;
95228ec039cSOGAWA Hirofumi 			break;
9531da177e4SLinus Torvalds 		case Opt_nocase:
9541da177e4SLinus Torvalds 			if (!is_vfat)
9551da177e4SLinus Torvalds 				opts->nocase = 1;
9561da177e4SLinus Torvalds 			else {
9571da177e4SLinus Torvalds 				/* for backward compatibility */
9581da177e4SLinus Torvalds 				opts->shortname = VFAT_SFN_DISPLAY_WIN95
9591da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
9601da177e4SLinus Torvalds 			}
9611da177e4SLinus Torvalds 			break;
9621da177e4SLinus Torvalds 		case Opt_quiet:
9631da177e4SLinus Torvalds 			opts->quiet = 1;
9641da177e4SLinus Torvalds 			break;
9651da177e4SLinus Torvalds 		case Opt_showexec:
9661da177e4SLinus Torvalds 			opts->showexec = 1;
9671da177e4SLinus Torvalds 			break;
9681da177e4SLinus Torvalds 		case Opt_debug:
9691da177e4SLinus Torvalds 			*debug = 1;
9701da177e4SLinus Torvalds 			break;
9711da177e4SLinus Torvalds 		case Opt_immutable:
9721da177e4SLinus Torvalds 			opts->sys_immutable = 1;
9731da177e4SLinus Torvalds 			break;
9741da177e4SLinus Torvalds 		case Opt_uid:
9751da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
9765b3d5aeaSJan Kara 				return -EINVAL;
977170782ebSEric W. Biederman 			opts->fs_uid = make_kuid(current_user_ns(), option);
978170782ebSEric W. Biederman 			if (!uid_valid(opts->fs_uid))
9795b3d5aeaSJan Kara 				return -EINVAL;
9801da177e4SLinus Torvalds 			break;
9811da177e4SLinus Torvalds 		case Opt_gid:
9821da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
9835b3d5aeaSJan Kara 				return -EINVAL;
984170782ebSEric W. Biederman 			opts->fs_gid = make_kgid(current_user_ns(), option);
985170782ebSEric W. Biederman 			if (!gid_valid(opts->fs_gid))
9865b3d5aeaSJan Kara 				return -EINVAL;
9871da177e4SLinus Torvalds 			break;
9881da177e4SLinus Torvalds 		case Opt_umask:
9891da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
9905b3d5aeaSJan Kara 				return -EINVAL;
9911da177e4SLinus Torvalds 			opts->fs_fmask = opts->fs_dmask = option;
9921da177e4SLinus Torvalds 			break;
9931da177e4SLinus Torvalds 		case Opt_dmask:
9941da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
9955b3d5aeaSJan Kara 				return -EINVAL;
9961da177e4SLinus Torvalds 			opts->fs_dmask = option;
9971da177e4SLinus Torvalds 			break;
9981da177e4SLinus Torvalds 		case Opt_fmask:
9991da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
10005b3d5aeaSJan Kara 				return -EINVAL;
10011da177e4SLinus Torvalds 			opts->fs_fmask = option;
10021da177e4SLinus Torvalds 			break;
10031ae43f82SOGAWA Hirofumi 		case Opt_allow_utime:
10041ae43f82SOGAWA Hirofumi 			if (match_octal(&args[0], &option))
10055b3d5aeaSJan Kara 				return -EINVAL;
10061ae43f82SOGAWA Hirofumi 			opts->allow_utime = option & (S_IWGRP | S_IWOTH);
10071ae43f82SOGAWA Hirofumi 			break;
10081da177e4SLinus Torvalds 		case Opt_codepage:
10091da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
10105b3d5aeaSJan Kara 				return -EINVAL;
10111da177e4SLinus Torvalds 			opts->codepage = option;
10121da177e4SLinus Torvalds 			break;
1013ae78bf9cSChris Mason 		case Opt_flush:
1014ae78bf9cSChris Mason 			opts->flush = 1;
1015ae78bf9cSChris Mason 			break;
101658156c8fSJan Kara 		case Opt_time_offset:
101758156c8fSJan Kara 			if (match_int(&args[0], &option))
10185b3d5aeaSJan Kara 				return -EINVAL;
101958156c8fSJan Kara 			if (option < -12 * 60 || option > 12 * 60)
10205b3d5aeaSJan Kara 				return -EINVAL;
102158156c8fSJan Kara 			opts->tz_set = 1;
102258156c8fSJan Kara 			opts->time_offset = option;
102358156c8fSJan Kara 			break;
1024b271e067SJoe Peterson 		case Opt_tz_utc:
102558156c8fSJan Kara 			opts->tz_set = 1;
102658156c8fSJan Kara 			opts->time_offset = 0;
1027b271e067SJoe Peterson 			break;
102885c78591SDenis Karpov 		case Opt_err_cont:
102985c78591SDenis Karpov 			opts->errors = FAT_ERRORS_CONT;
103085c78591SDenis Karpov 			break;
103185c78591SDenis Karpov 		case Opt_err_panic:
103285c78591SDenis Karpov 			opts->errors = FAT_ERRORS_PANIC;
103385c78591SDenis Karpov 			break;
103485c78591SDenis Karpov 		case Opt_err_ro:
103585c78591SDenis Karpov 			opts->errors = FAT_ERRORS_RO;
103685c78591SDenis Karpov 			break;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 		/* msdos specific */
10391da177e4SLinus Torvalds 		case Opt_dots:
10401da177e4SLinus Torvalds 			opts->dotsOK = 1;
10411da177e4SLinus Torvalds 			break;
10421da177e4SLinus Torvalds 		case Opt_nodots:
10431da177e4SLinus Torvalds 			opts->dotsOK = 0;
10441da177e4SLinus Torvalds 			break;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 		/* vfat specific */
10471da177e4SLinus Torvalds 		case Opt_charset:
10481da177e4SLinus Torvalds 			if (opts->iocharset != fat_default_iocharset)
10491da177e4SLinus Torvalds 				kfree(opts->iocharset);
10501da177e4SLinus Torvalds 			iocharset = match_strdup(&args[0]);
10511da177e4SLinus Torvalds 			if (!iocharset)
10521da177e4SLinus Torvalds 				return -ENOMEM;
10531da177e4SLinus Torvalds 			opts->iocharset = iocharset;
10541da177e4SLinus Torvalds 			break;
10551da177e4SLinus Torvalds 		case Opt_shortname_lower:
10561da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_LOWER
10571da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10581da177e4SLinus Torvalds 			break;
10591da177e4SLinus Torvalds 		case Opt_shortname_win95:
10601da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WIN95
10611da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10621da177e4SLinus Torvalds 			break;
10631da177e4SLinus Torvalds 		case Opt_shortname_winnt:
10641da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
10651da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WINNT;
10661da177e4SLinus Torvalds 			break;
10671da177e4SLinus Torvalds 		case Opt_shortname_mixed:
10681da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
10691da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10701da177e4SLinus Torvalds 			break;
10711da177e4SLinus Torvalds 		case Opt_utf8_no:		/* 0 or no or false */
10721da177e4SLinus Torvalds 			opts->utf8 = 0;
10731da177e4SLinus Torvalds 			break;
10741da177e4SLinus Torvalds 		case Opt_utf8_yes:		/* empty or 1 or yes or true */
10751da177e4SLinus Torvalds 			opts->utf8 = 1;
10761da177e4SLinus Torvalds 			break;
10771da177e4SLinus Torvalds 		case Opt_uni_xl_no:		/* 0 or no or false */
10781da177e4SLinus Torvalds 			opts->unicode_xlate = 0;
10791da177e4SLinus Torvalds 			break;
10801da177e4SLinus Torvalds 		case Opt_uni_xl_yes:		/* empty or 1 or yes or true */
10811da177e4SLinus Torvalds 			opts->unicode_xlate = 1;
10821da177e4SLinus Torvalds 			break;
10831da177e4SLinus Torvalds 		case Opt_nonumtail_no:		/* 0 or no or false */
10841da177e4SLinus Torvalds 			opts->numtail = 1;	/* negated option */
10851da177e4SLinus Torvalds 			break;
10861da177e4SLinus Torvalds 		case Opt_nonumtail_yes:		/* empty or 1 or yes or true */
10871da177e4SLinus Torvalds 			opts->numtail = 0;	/* negated option */
10881da177e4SLinus Torvalds 			break;
1089dfc209c0SOGAWA Hirofumi 		case Opt_rodir:
1090dfc209c0SOGAWA Hirofumi 			opts->rodir = 1;
1091dfc209c0SOGAWA Hirofumi 			break;
1092681142f9SChristoph Hellwig 		case Opt_discard:
1093681142f9SChristoph Hellwig 			opts->discard = 1;
1094681142f9SChristoph Hellwig 			break;
10957669e8fbSSteven J. Magnani 		case Opt_nfs:
10967669e8fbSSteven J. Magnani 			opts->nfs = 1;
10977669e8fbSSteven J. Magnani 			break;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		/* obsolete mount options */
1100d7a83c0fSGeert Uytterhoeven 		case Opt_obsolete:
1101869f58c0SAlexey Fisher 			fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
1102869f58c0SAlexey Fisher 			       "not supported now", p);
11031da177e4SLinus Torvalds 			break;
11041da177e4SLinus Torvalds 		/* unknown option */
11051da177e4SLinus Torvalds 		default:
110641a34a4fSChristoph Hellwig 			if (!silent) {
1107869f58c0SAlexey Fisher 				fat_msg(sb, KERN_ERR,
1108869f58c0SAlexey Fisher 				       "Unrecognized mount option \"%s\" "
1109869f58c0SAlexey Fisher 				       "or missing value", p);
111041a34a4fSChristoph Hellwig 			}
11111da177e4SLinus Torvalds 			return -EINVAL;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 	}
11148d44d974SOGAWA Hirofumi 
11158d44d974SOGAWA Hirofumi out:
11164de151d8SAlexey Dobriyan 	/* UTF-8 doesn't provide FAT semantics */
11171da177e4SLinus Torvalds 	if (!strcmp(opts->iocharset, "utf8")) {
1118186b5370SMihai Moldovan 		fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
11198d44d974SOGAWA Hirofumi 		       " for FAT filesystems, filesystem will be "
1120186b5370SMihai Moldovan 		       "case sensitive!");
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231ae43f82SOGAWA Hirofumi 	/* If user doesn't specify allow_utime, it's initialized from dmask. */
11241ae43f82SOGAWA Hirofumi 	if (opts->allow_utime == (unsigned short)-1)
11251ae43f82SOGAWA Hirofumi 		opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
11261da177e4SLinus Torvalds 	if (opts->unicode_xlate)
11271da177e4SLinus Torvalds 		opts->utf8 = 0;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	return 0;
11301da177e4SLinus Torvalds }
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds static int fat_read_root(struct inode *inode)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
11351da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
11361da177e4SLinus Torvalds 	int error;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
11391da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
11401da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
11411da177e4SLinus Torvalds 	inode->i_version++;
11421da177e4SLinus Torvalds 	inode->i_generation = 0;
11439c0aa1b8SOGAWA Hirofumi 	inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
11441da177e4SLinus Torvalds 	inode->i_op = sbi->dir_ops;
11451da177e4SLinus Torvalds 	inode->i_fop = &fat_dir_operations;
11461da177e4SLinus Torvalds 	if (sbi->fat_bits == 32) {
11471da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = sbi->root_cluster;
11481da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
11491da177e4SLinus Torvalds 		if (error < 0)
11501da177e4SLinus Torvalds 			return error;
11511da177e4SLinus Torvalds 	} else {
11521da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = 0;
11531da177e4SLinus Torvalds 		inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
11561da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
11571da177e4SLinus Torvalds 	MSDOS_I(inode)->i_logstart = 0;
11581da177e4SLinus Torvalds 	MSDOS_I(inode)->mmu_private = inode->i_size;
11591da177e4SLinus Torvalds 
11609c0aa1b8SOGAWA Hirofumi 	fat_save_attrs(inode, ATTR_DIR);
11611da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
11621da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1163bfe86848SMiklos Szeredi 	set_nlink(inode, fat_subdirs(inode)+2);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	return 0;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds /*
11691da177e4SLinus Torvalds  * Read the super block of an MS-DOS FS.
11701da177e4SLinus Torvalds  */
1171384f5c96SOGAWA Hirofumi int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
11723d23985dSAl Viro 		   void (*setup)(struct super_block *))
11731da177e4SLinus Torvalds {
1174b522412aSAl Viro 	struct inode *root_inode = NULL, *fat_inode = NULL;
1175020ac5b6SArtem Bityutskiy 	struct inode *fsinfo_inode = NULL;
11761da177e4SLinus Torvalds 	struct buffer_head *bh;
11771da177e4SLinus Torvalds 	struct fat_boot_sector *b;
11781da177e4SLinus Torvalds 	struct msdos_sb_info *sbi;
11791da177e4SLinus Torvalds 	u16 logical_sector_size;
11801da177e4SLinus Torvalds 	u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
11811da177e4SLinus Torvalds 	int debug;
11821da177e4SLinus Torvalds 	unsigned int media;
11831da177e4SLinus Torvalds 	long error;
11841da177e4SLinus Torvalds 	char buf[50];
11851da177e4SLinus Torvalds 
11868f593427SLinus Torvalds 	/*
11878f593427SLinus Torvalds 	 * GFP_KERNEL is ok here, because while we do hold the
11888f593427SLinus Torvalds 	 * supeblock lock, memory pressure can't call back into
11898f593427SLinus Torvalds 	 * the filesystem, since we're only just about to mount
11908f593427SLinus Torvalds 	 * it and have no inodes etc active!
11918f593427SLinus Torvalds 	 */
1192f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
11931da177e4SLinus Torvalds 	if (!sbi)
11941da177e4SLinus Torvalds 		return -ENOMEM;
11951da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
11981da177e4SLinus Torvalds 	sb->s_magic = MSDOS_SUPER_MAGIC;
11991da177e4SLinus Torvalds 	sb->s_op = &fat_sops;
12001da177e4SLinus Torvalds 	sb->s_export_op = &fat_export_ops;
1201aaa04b48SOGAWA Hirofumi 	ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1202aaa04b48SOGAWA Hirofumi 			     DEFAULT_RATELIMIT_BURST);
12031da177e4SLinus Torvalds 
1204869f58c0SAlexey Fisher 	error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
12051da177e4SLinus Torvalds 	if (error)
12061da177e4SLinus Torvalds 		goto out_fail;
12071da177e4SLinus Torvalds 
12083d23985dSAl Viro 	setup(sb); /* flavour-specific stuff that needs options */
12093d23985dSAl Viro 
12101da177e4SLinus Torvalds 	error = -EIO;
12111da177e4SLinus Torvalds 	sb_min_blocksize(sb, 512);
12121da177e4SLinus Torvalds 	bh = sb_bread(sb, 0);
12131da177e4SLinus Torvalds 	if (bh == NULL) {
1214869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "unable to read boot sector");
12151da177e4SLinus Torvalds 		goto out_fail;
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	b = (struct fat_boot_sector *) bh->b_data;
12191da177e4SLinus Torvalds 	if (!b->reserved) {
12201da177e4SLinus Torvalds 		if (!silent)
1221869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
12221da177e4SLinus Torvalds 		brelse(bh);
12231da177e4SLinus Torvalds 		goto out_invalid;
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 	if (!b->fats) {
12261da177e4SLinus Torvalds 		if (!silent)
1227869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
12281da177e4SLinus Torvalds 		brelse(bh);
12291da177e4SLinus Torvalds 		goto out_invalid;
12301da177e4SLinus Torvalds 	}
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	/*
12331da177e4SLinus Torvalds 	 * Earlier we checked here that b->secs_track and b->head are nonzero,
12341da177e4SLinus Torvalds 	 * but it turns out valid FAT filesystems can have zero there.
12351da177e4SLinus Torvalds 	 */
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	media = b->media;
123873f20e58SAndrew Morton 	if (!fat_valid_media(media)) {
12391da177e4SLinus Torvalds 		if (!silent)
1240869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
12411da177e4SLinus Torvalds 			       media);
12421da177e4SLinus Torvalds 		brelse(bh);
12431da177e4SLinus Torvalds 		goto out_invalid;
12441da177e4SLinus Torvalds 	}
1245803f445fSHarvey Harrison 	logical_sector_size = get_unaligned_le16(&b->sector_size);
1246e7d709c0SVignesh Babu BM 	if (!is_power_of_2(logical_sector_size)
12471da177e4SLinus Torvalds 	    || (logical_sector_size < 512)
12489f354858SOlof Johansson 	    || (logical_sector_size > 4096)) {
12491da177e4SLinus Torvalds 		if (!silent)
1250869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
12511da177e4SLinus Torvalds 			       logical_sector_size);
12521da177e4SLinus Torvalds 		brelse(bh);
12531da177e4SLinus Torvalds 		goto out_invalid;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 	sbi->sec_per_clus = b->sec_per_clus;
1256e7d709c0SVignesh Babu BM 	if (!is_power_of_2(sbi->sec_per_clus)) {
12571da177e4SLinus Torvalds 		if (!silent)
1258869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
12591da177e4SLinus Torvalds 			       sbi->sec_per_clus);
12601da177e4SLinus Torvalds 		brelse(bh);
12611da177e4SLinus Torvalds 		goto out_invalid;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	if (logical_sector_size < sb->s_blocksize) {
1265869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1266869f58c0SAlexey Fisher 		       " (logical sector size = %u)", logical_sector_size);
12671da177e4SLinus Torvalds 		brelse(bh);
12681da177e4SLinus Torvalds 		goto out_fail;
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 	if (logical_sector_size > sb->s_blocksize) {
12711da177e4SLinus Torvalds 		brelse(bh);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 		if (!sb_set_blocksize(sb, logical_sector_size)) {
1274869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
12751da177e4SLinus Torvalds 			       logical_sector_size);
12761da177e4SLinus Torvalds 			goto out_fail;
12771da177e4SLinus Torvalds 		}
12781da177e4SLinus Torvalds 		bh = sb_bread(sb, 0);
12791da177e4SLinus Torvalds 		if (bh == NULL) {
1280869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "unable to read boot sector"
1281869f58c0SAlexey Fisher 			       " (logical sector size = %lu)",
12821da177e4SLinus Torvalds 			       sb->s_blocksize);
12831da177e4SLinus Torvalds 			goto out_fail;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 		b = (struct fat_boot_sector *) bh->b_data;
12861da177e4SLinus Torvalds 	}
12871da177e4SLinus Torvalds 
1288e40b34c7SMarco Stornelli 	mutex_init(&sbi->s_lock);
12891da177e4SLinus Torvalds 	sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
12901da177e4SLinus Torvalds 	sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
12911da177e4SLinus Torvalds 	sbi->fats = b->fats;
12921da177e4SLinus Torvalds 	sbi->fat_bits = 0;		/* Don't know yet */
12931da177e4SLinus Torvalds 	sbi->fat_start = le16_to_cpu(b->reserved);
12941da177e4SLinus Torvalds 	sbi->fat_length = le16_to_cpu(b->fat_length);
12951da177e4SLinus Torvalds 	sbi->root_cluster = 0;
12961da177e4SLinus Torvalds 	sbi->free_clusters = -1;	/* Don't know yet */
1297606e423eSOGAWA Hirofumi 	sbi->free_clus_valid = 0;
12981da177e4SLinus Torvalds 	sbi->prev_free = FAT_START_ENT;
1299710d4403SNamjae Jeon 	sb->s_maxbytes = 0xffffffff;
13001da177e4SLinus Torvalds 
1301*6b46419bSOleksij Rempel 	if (!sbi->fat_length && b->fat32.length) {
13021da177e4SLinus Torvalds 		struct fat_boot_fsinfo *fsinfo;
13031da177e4SLinus Torvalds 		struct buffer_head *fsinfo_bh;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 		/* Must be FAT32 */
13061da177e4SLinus Torvalds 		sbi->fat_bits = 32;
1307*6b46419bSOleksij Rempel 		sbi->fat_length = le32_to_cpu(b->fat32.length);
1308*6b46419bSOleksij Rempel 		sbi->root_cluster = le32_to_cpu(b->fat32.root_cluster);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 		/* MC - if info_sector is 0, don't multiply by 0 */
1311*6b46419bSOleksij Rempel 		sbi->fsinfo_sector = le16_to_cpu(b->fat32.info_sector);
13121da177e4SLinus Torvalds 		if (sbi->fsinfo_sector == 0)
13131da177e4SLinus Torvalds 			sbi->fsinfo_sector = 1;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
13161da177e4SLinus Torvalds 		if (fsinfo_bh == NULL) {
1317869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1318869f58c0SAlexey Fisher 			       " (sector = %lu)", sbi->fsinfo_sector);
13191da177e4SLinus Torvalds 			brelse(bh);
13201da177e4SLinus Torvalds 			goto out_fail;
13211da177e4SLinus Torvalds 		}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 		fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
13241da177e4SLinus Torvalds 		if (!IS_FSINFO(fsinfo)) {
1325869f58c0SAlexey Fisher 			fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1326869f58c0SAlexey Fisher 			       "0x%08x, 0x%08x (sector = %lu)",
13271da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature1),
13281da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature2),
13291da177e4SLinus Torvalds 			       sbi->fsinfo_sector);
13301da177e4SLinus Torvalds 		} else {
133128ec039cSOGAWA Hirofumi 			if (sbi->options.usefree)
1332606e423eSOGAWA Hirofumi 				sbi->free_clus_valid = 1;
1333606e423eSOGAWA Hirofumi 			sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
13341da177e4SLinus Torvalds 			sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
13351da177e4SLinus Torvalds 		}
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 		brelse(fsinfo_bh);
13381da177e4SLinus Torvalds 	}
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
13411da177e4SLinus Torvalds 	sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1344803f445fSHarvey Harrison 	sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
13451da177e4SLinus Torvalds 	if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
13461da177e4SLinus Torvalds 		if (!silent)
1347c39540c6SRavishankar N 			fat_msg(sb, KERN_ERR, "bogus directory-entries per block"
1348869f58c0SAlexey Fisher 			       " (%u)", sbi->dir_entries);
13491da177e4SLinus Torvalds 		brelse(bh);
13501da177e4SLinus Torvalds 		goto out_invalid;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	rootdir_sectors = sbi->dir_entries
13541da177e4SLinus Torvalds 		* sizeof(struct msdos_dir_entry) / sb->s_blocksize;
13551da177e4SLinus Torvalds 	sbi->data_start = sbi->dir_start + rootdir_sectors;
1356803f445fSHarvey Harrison 	total_sectors = get_unaligned_le16(&b->sectors);
13571da177e4SLinus Torvalds 	if (total_sectors == 0)
13581da177e4SLinus Torvalds 		total_sectors = le32_to_cpu(b->total_sect);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	if (sbi->fat_bits != 32)
13631da177e4SLinus Torvalds 		sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	/* check that FAT table does not overflow */
13661da177e4SLinus Torvalds 	fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
13671da177e4SLinus Torvalds 	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
13681da177e4SLinus Torvalds 	if (total_clusters > MAX_FAT(sb)) {
13691da177e4SLinus Torvalds 		if (!silent)
1370869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
13711da177e4SLinus Torvalds 			       total_clusters);
13721da177e4SLinus Torvalds 		brelse(bh);
13731da177e4SLinus Torvalds 		goto out_invalid;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	sbi->max_cluster = total_clusters + FAT_START_ENT;
13771da177e4SLinus Torvalds 	/* check the free_clusters, it's not necessarily correct */
13781da177e4SLinus Torvalds 	if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
13791da177e4SLinus Torvalds 		sbi->free_clusters = -1;
13801da177e4SLinus Torvalds 	/* check the prev_free, it's not necessarily correct */
13811da177e4SLinus Torvalds 	sbi->prev_free %= sbi->max_cluster;
13821da177e4SLinus Torvalds 	if (sbi->prev_free < FAT_START_ENT)
13831da177e4SLinus Torvalds 		sbi->prev_free = FAT_START_ENT;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	brelse(bh);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
13881da177e4SLinus Torvalds 	fat_hash_init(sb);
13897669e8fbSSteven J. Magnani 	dir_hash_init(sb);
13901da177e4SLinus Torvalds 	fat_ent_access_init(sb);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	/*
13931da177e4SLinus Torvalds 	 * The low byte of FAT's first entry must have same value with
13941da177e4SLinus Torvalds 	 * media-field.  But in real world, too many devices is
13951da177e4SLinus Torvalds 	 * writing wrong value.  So, removed that validity check.
13961da177e4SLinus Torvalds 	 *
13971da177e4SLinus Torvalds 	 * if (FAT_FIRST_ENT(sb, media) != first)
13981da177e4SLinus Torvalds 	 */
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	error = -EINVAL;
14011da177e4SLinus Torvalds 	sprintf(buf, "cp%d", sbi->options.codepage);
14021da177e4SLinus Torvalds 	sbi->nls_disk = load_nls(buf);
14031da177e4SLinus Torvalds 	if (!sbi->nls_disk) {
1404869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
14051da177e4SLinus Torvalds 		goto out_fail;
14061da177e4SLinus Torvalds 	}
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	/* FIXME: utf8 is using iocharset for upper/lower conversion */
14091da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
14101da177e4SLinus Torvalds 		sbi->nls_io = load_nls(sbi->options.iocharset);
14111da177e4SLinus Torvalds 		if (!sbi->nls_io) {
1412869f58c0SAlexey Fisher 			fat_msg(sb, KERN_ERR, "IO charset %s not found",
14131da177e4SLinus Torvalds 			       sbi->options.iocharset);
14141da177e4SLinus Torvalds 			goto out_fail;
14151da177e4SLinus Torvalds 		}
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	error = -ENOMEM;
1419b522412aSAl Viro 	fat_inode = new_inode(sb);
1420b522412aSAl Viro 	if (!fat_inode)
1421b522412aSAl Viro 		goto out_fail;
1422b522412aSAl Viro 	MSDOS_I(fat_inode)->i_pos = 0;
1423b522412aSAl Viro 	sbi->fat_inode = fat_inode;
1424020ac5b6SArtem Bityutskiy 
1425020ac5b6SArtem Bityutskiy 	fsinfo_inode = new_inode(sb);
1426020ac5b6SArtem Bityutskiy 	if (!fsinfo_inode)
1427020ac5b6SArtem Bityutskiy 		goto out_fail;
1428020ac5b6SArtem Bityutskiy 	fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1429020ac5b6SArtem Bityutskiy 	sbi->fsinfo_inode = fsinfo_inode;
1430020ac5b6SArtem Bityutskiy 	insert_inode_hash(fsinfo_inode);
1431020ac5b6SArtem Bityutskiy 
14321da177e4SLinus Torvalds 	root_inode = new_inode(sb);
14331da177e4SLinus Torvalds 	if (!root_inode)
14341da177e4SLinus Torvalds 		goto out_fail;
14351da177e4SLinus Torvalds 	root_inode->i_ino = MSDOS_ROOT_INO;
14361da177e4SLinus Torvalds 	root_inode->i_version = 1;
14371da177e4SLinus Torvalds 	error = fat_read_root(root_inode);
14381688f860SAl Viro 	if (error < 0) {
14391688f860SAl Viro 		iput(root_inode);
14401da177e4SLinus Torvalds 		goto out_fail;
14411688f860SAl Viro 	}
14421da177e4SLinus Torvalds 	error = -ENOMEM;
14431da177e4SLinus Torvalds 	insert_inode_hash(root_inode);
14447669e8fbSSteven J. Magnani 	fat_attach(root_inode, 0);
14451688f860SAl Viro 	sb->s_root = d_make_root(root_inode);
14461da177e4SLinus Torvalds 	if (!sb->s_root) {
1447869f58c0SAlexey Fisher 		fat_msg(sb, KERN_ERR, "get root inode failed");
14481da177e4SLinus Torvalds 		goto out_fail;
14491da177e4SLinus Torvalds 	}
14501da177e4SLinus Torvalds 
1451f562146aSNamjae Jeon 	if (sbi->options.discard) {
1452f562146aSNamjae Jeon 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
1453f562146aSNamjae Jeon 		if (!blk_queue_discard(q))
1454f562146aSNamjae Jeon 			fat_msg(sb, KERN_WARNING,
1455f562146aSNamjae Jeon 					"mounting with \"discard\" option, but "
1456f562146aSNamjae Jeon 					"the device does not support discard");
1457f562146aSNamjae Jeon 	}
1458f562146aSNamjae Jeon 
14591da177e4SLinus Torvalds 	return 0;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds out_invalid:
14621da177e4SLinus Torvalds 	error = -EINVAL;
14631da177e4SLinus Torvalds 	if (!silent)
1464869f58c0SAlexey Fisher 		fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds out_fail:
1467020ac5b6SArtem Bityutskiy 	if (fsinfo_inode)
1468020ac5b6SArtem Bityutskiy 		iput(fsinfo_inode);
1469b522412aSAl Viro 	if (fat_inode)
1470b522412aSAl Viro 		iput(fat_inode);
14711da177e4SLinus Torvalds 	unload_nls(sbi->nls_io);
14721da177e4SLinus Torvalds 	unload_nls(sbi->nls_disk);
14731da177e4SLinus Torvalds 	if (sbi->options.iocharset != fat_default_iocharset)
14741da177e4SLinus Torvalds 		kfree(sbi->options.iocharset);
14751da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
14761da177e4SLinus Torvalds 	kfree(sbi);
14771da177e4SLinus Torvalds 	return error;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14807c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_fill_super);
14811da177e4SLinus Torvalds 
1482ae78bf9cSChris Mason /*
1483ae78bf9cSChris Mason  * helper function for fat_flush_inodes.  This writes both the inode
1484ae78bf9cSChris Mason  * and the file data blocks, waiting for in flight data blocks before
1485ae78bf9cSChris Mason  * the start of the call.  It does not wait for any io started
1486ae78bf9cSChris Mason  * during the call
1487ae78bf9cSChris Mason  */
1488ae78bf9cSChris Mason static int writeback_inode(struct inode *inode)
1489ae78bf9cSChris Mason {
1490ae78bf9cSChris Mason 
1491ae78bf9cSChris Mason 	int ret;
149214864655SNamjae Jeon 
149314864655SNamjae Jeon 	/* if we used wait=1, sync_inode_metadata waits for the io for the
149414864655SNamjae Jeon 	* inode to finish.  So wait=0 is sent down to sync_inode_metadata
1495ae78bf9cSChris Mason 	* and filemap_fdatawrite is used for the data blocks
1496ae78bf9cSChris Mason 	*/
149714864655SNamjae Jeon 	ret = sync_inode_metadata(inode, 0);
1498ae78bf9cSChris Mason 	if (!ret)
149914864655SNamjae Jeon 		ret = filemap_fdatawrite(inode->i_mapping);
1500ae78bf9cSChris Mason 	return ret;
1501ae78bf9cSChris Mason }
1502ae78bf9cSChris Mason 
1503ae78bf9cSChris Mason /*
1504ae78bf9cSChris Mason  * write data and metadata corresponding to i1 and i2.  The io is
1505ae78bf9cSChris Mason  * started but we do not wait for any of it to finish.
1506ae78bf9cSChris Mason  *
1507ae78bf9cSChris Mason  * filemap_flush is used for the block device, so if there is a dirty
1508ae78bf9cSChris Mason  * page for a block already in flight, we will not wait and start the
1509ae78bf9cSChris Mason  * io over again
1510ae78bf9cSChris Mason  */
1511ae78bf9cSChris Mason int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1512ae78bf9cSChris Mason {
1513ae78bf9cSChris Mason 	int ret = 0;
1514ae78bf9cSChris Mason 	if (!MSDOS_SB(sb)->options.flush)
1515ae78bf9cSChris Mason 		return 0;
1516ae78bf9cSChris Mason 	if (i1)
1517ae78bf9cSChris Mason 		ret = writeback_inode(i1);
1518ae78bf9cSChris Mason 	if (!ret && i2)
1519ae78bf9cSChris Mason 		ret = writeback_inode(i2);
152097e860d3SEric Sesterhenn 	if (!ret) {
1521ae78bf9cSChris Mason 		struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1522ae78bf9cSChris Mason 		ret = filemap_flush(mapping);
1523ae78bf9cSChris Mason 	}
1524ae78bf9cSChris Mason 	return ret;
1525ae78bf9cSChris Mason }
1526ae78bf9cSChris Mason EXPORT_SYMBOL_GPL(fat_flush_inodes);
1527ae78bf9cSChris Mason 
15281da177e4SLinus Torvalds static int __init init_fat_fs(void)
15291da177e4SLinus Torvalds {
1530532a39a3SPekka J Enberg 	int err;
15311da177e4SLinus Torvalds 
1532532a39a3SPekka J Enberg 	err = fat_cache_init();
1533532a39a3SPekka J Enberg 	if (err)
1534532a39a3SPekka J Enberg 		return err;
1535532a39a3SPekka J Enberg 
1536532a39a3SPekka J Enberg 	err = fat_init_inodecache();
1537532a39a3SPekka J Enberg 	if (err)
1538532a39a3SPekka J Enberg 		goto failed;
1539532a39a3SPekka J Enberg 
1540532a39a3SPekka J Enberg 	return 0;
1541532a39a3SPekka J Enberg 
1542532a39a3SPekka J Enberg failed:
1543532a39a3SPekka J Enberg 	fat_cache_destroy();
1544532a39a3SPekka J Enberg 	return err;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static void __exit exit_fat_fs(void)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	fat_cache_destroy();
15501da177e4SLinus Torvalds 	fat_destroy_inodecache();
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds module_init(init_fat_fs)
15541da177e4SLinus Torvalds module_exit(exit_fat_fs)
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1557