xref: /linux/fs/fat/inode.c (revision 8d44d9741f6808c107a144f469fb89e6fe7c55e3)
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/smp_lock.h>
181da177e4SLinus Torvalds #include <linux/seq_file.h>
191da177e4SLinus Torvalds #include <linux/msdos_fs.h>
201da177e4SLinus Torvalds #include <linux/pagemap.h>
21a5425d29SOGAWA Hirofumi #include <linux/mpage.h>
221da177e4SLinus Torvalds #include <linux/buffer_head.h>
23a5694255SChristoph Hellwig #include <linux/exportfs.h>
241da177e4SLinus Torvalds #include <linux/mount.h>
251da177e4SLinus Torvalds #include <linux/vfs.h>
261da177e4SLinus Torvalds #include <linux/parser.h>
27e5174baaSOGAWA Hirofumi #include <linux/uio.h>
28ae78bf9cSChris Mason #include <linux/writeback.h>
29e7d709c0SVignesh Babu BM #include <linux/log2.h>
301da177e4SLinus Torvalds #include <asm/unaligned.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
331da177e4SLinus Torvalds /* if user don't select VFAT, this is undefined. */
341da177e4SLinus Torvalds #define CONFIG_FAT_DEFAULT_IOCHARSET	""
351da177e4SLinus Torvalds #endif
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
381da177e4SLinus Torvalds static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static int fat_add_cluster(struct inode *inode)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	int err, cluster;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	err = fat_alloc_clusters(inode, &cluster, 1);
461da177e4SLinus Torvalds 	if (err)
471da177e4SLinus Torvalds 		return err;
481da177e4SLinus Torvalds 	/* FIXME: this cluster should be added after data of this
491da177e4SLinus Torvalds 	 * cluster is writed */
501da177e4SLinus Torvalds 	err = fat_chain_add(inode, cluster, 1);
511da177e4SLinus Torvalds 	if (err)
521da177e4SLinus Torvalds 		fat_free_clusters(inode, cluster);
531da177e4SLinus Torvalds 	return err;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
566a1d9805SOGAWA Hirofumi static inline int __fat_get_block(struct inode *inode, sector_t iblock,
57e5174baaSOGAWA Hirofumi 				  unsigned long *max_blocks,
581da177e4SLinus Torvalds 				  struct buffer_head *bh_result, int create)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
61e5174baaSOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
62e5174baaSOGAWA Hirofumi 	unsigned long mapped_blocks;
636a1d9805SOGAWA Hirofumi 	sector_t phys;
64e5174baaSOGAWA Hirofumi 	int err, offset;
651da177e4SLinus Torvalds 
66e5174baaSOGAWA Hirofumi 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks);
671da177e4SLinus Torvalds 	if (err)
681da177e4SLinus Torvalds 		return err;
691da177e4SLinus Torvalds 	if (phys) {
701da177e4SLinus Torvalds 		map_bh(bh_result, sb, phys);
71e5174baaSOGAWA Hirofumi 		*max_blocks = min(mapped_blocks, *max_blocks);
721da177e4SLinus Torvalds 		return 0;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 	if (!create)
751da177e4SLinus Torvalds 		return 0;
76e5174baaSOGAWA Hirofumi 
771da177e4SLinus Torvalds 	if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
781da177e4SLinus Torvalds 		fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)",
791da177e4SLinus Torvalds 			MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
801da177e4SLinus Torvalds 		return -EIO;
811da177e4SLinus Torvalds 	}
82e5174baaSOGAWA Hirofumi 
83e5174baaSOGAWA Hirofumi 	offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
84e5174baaSOGAWA Hirofumi 	if (!offset) {
85e5174baaSOGAWA Hirofumi 		/* TODO: multiple cluster allocation would be desirable. */
861da177e4SLinus Torvalds 		err = fat_add_cluster(inode);
871da177e4SLinus Torvalds 		if (err)
881da177e4SLinus Torvalds 			return err;
891da177e4SLinus Torvalds 	}
90e5174baaSOGAWA Hirofumi 	/* available blocks on this cluster */
91e5174baaSOGAWA Hirofumi 	mapped_blocks = sbi->sec_per_clus - offset;
92e5174baaSOGAWA Hirofumi 
93e5174baaSOGAWA Hirofumi 	*max_blocks = min(mapped_blocks, *max_blocks);
94e5174baaSOGAWA Hirofumi 	MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
95e5174baaSOGAWA Hirofumi 
96e5174baaSOGAWA Hirofumi 	err = fat_bmap(inode, iblock, &phys, &mapped_blocks);
971da177e4SLinus Torvalds 	if (err)
981da177e4SLinus Torvalds 		return err;
996a1d9805SOGAWA Hirofumi 
100e5174baaSOGAWA Hirofumi 	BUG_ON(!phys);
101e5174baaSOGAWA Hirofumi 	BUG_ON(*max_blocks != mapped_blocks);
1021da177e4SLinus Torvalds 	set_buffer_new(bh_result);
1031da177e4SLinus Torvalds 	map_bh(bh_result, sb, phys);
1041da177e4SLinus Torvalds 
105e5174baaSOGAWA Hirofumi 	return 0;
106e5174baaSOGAWA Hirofumi }
107e5174baaSOGAWA Hirofumi 
108e5174baaSOGAWA Hirofumi static int fat_get_block(struct inode *inode, sector_t iblock,
109e5174baaSOGAWA Hirofumi 			 struct buffer_head *bh_result, int create)
110e5174baaSOGAWA Hirofumi {
1116a1d9805SOGAWA Hirofumi 	struct super_block *sb = inode->i_sb;
1126a1d9805SOGAWA Hirofumi 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1136a1d9805SOGAWA Hirofumi 	int err;
1146a1d9805SOGAWA Hirofumi 
1156a1d9805SOGAWA Hirofumi 	err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
1166a1d9805SOGAWA Hirofumi 	if (err)
1176a1d9805SOGAWA Hirofumi 		return err;
1186a1d9805SOGAWA Hirofumi 	bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1196a1d9805SOGAWA Hirofumi 	return 0;
120e5174baaSOGAWA Hirofumi }
121e5174baaSOGAWA Hirofumi 
1221da177e4SLinus Torvalds static int fat_writepage(struct page *page, struct writeback_control *wbc)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	return block_write_full_page(page, fat_get_block, wbc);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127a5425d29SOGAWA Hirofumi static int fat_writepages(struct address_space *mapping,
128a5425d29SOGAWA Hirofumi 			  struct writeback_control *wbc)
129a5425d29SOGAWA Hirofumi {
130a5425d29SOGAWA Hirofumi 	return mpage_writepages(mapping, wbc, fat_get_block);
131a5425d29SOGAWA Hirofumi }
132a5425d29SOGAWA Hirofumi 
1331da177e4SLinus Torvalds static int fat_readpage(struct file *file, struct page *page)
1341da177e4SLinus Torvalds {
135a5425d29SOGAWA Hirofumi 	return mpage_readpage(page, fat_get_block);
136a5425d29SOGAWA Hirofumi }
137a5425d29SOGAWA Hirofumi 
138a5425d29SOGAWA Hirofumi static int fat_readpages(struct file *file, struct address_space *mapping,
139a5425d29SOGAWA Hirofumi 			 struct list_head *pages, unsigned nr_pages)
140a5425d29SOGAWA Hirofumi {
141a5425d29SOGAWA Hirofumi 	return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
144d7777a25SNick Piggin static int fat_write_begin(struct file *file, struct address_space *mapping,
145d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
146d7777a25SNick Piggin 			struct page **pagep, void **fsdata)
1471da177e4SLinus Torvalds {
148d7777a25SNick Piggin 	*pagep = NULL;
149d7777a25SNick Piggin 	return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
150d7777a25SNick Piggin 				fat_get_block,
151d7777a25SNick Piggin 				&MSDOS_I(mapping->host)->mmu_private);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
154d7777a25SNick Piggin static int fat_write_end(struct file *file, struct address_space *mapping,
155d7777a25SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
156d7777a25SNick Piggin 			struct page *pagep, void *fsdata)
157ef402268SOGAWA Hirofumi {
158d7777a25SNick Piggin 	struct inode *inode = mapping->host;
159d7777a25SNick Piggin 	int err;
160d7777a25SNick Piggin 	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
161d7777a25SNick Piggin 	if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
162ef402268SOGAWA Hirofumi 		inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
163ef402268SOGAWA Hirofumi 		MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
164ef402268SOGAWA Hirofumi 		mark_inode_dirty(inode);
165ef402268SOGAWA Hirofumi 	}
166ef402268SOGAWA Hirofumi 	return err;
167ef402268SOGAWA Hirofumi }
168ef402268SOGAWA Hirofumi 
169e5174baaSOGAWA Hirofumi static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
170e5174baaSOGAWA Hirofumi 			     const struct iovec *iov,
171e5174baaSOGAWA Hirofumi 			     loff_t offset, unsigned long nr_segs)
172e5174baaSOGAWA Hirofumi {
173e5174baaSOGAWA Hirofumi 	struct file *file = iocb->ki_filp;
174e5174baaSOGAWA Hirofumi 	struct inode *inode = file->f_mapping->host;
175e5174baaSOGAWA Hirofumi 
176e5174baaSOGAWA Hirofumi 	if (rw == WRITE) {
177e5174baaSOGAWA Hirofumi 		/*
178e5174baaSOGAWA Hirofumi 		 * FIXME: blockdev_direct_IO() doesn't use ->prepare_write(),
179e5174baaSOGAWA Hirofumi 		 * so we need to update the ->mmu_private to block boundary.
180e5174baaSOGAWA Hirofumi 		 *
181e5174baaSOGAWA Hirofumi 		 * But we must fill the remaining area or hole by nul for
182e5174baaSOGAWA Hirofumi 		 * updating ->mmu_private.
18394412a96SOGAWA Hirofumi 		 *
18494412a96SOGAWA Hirofumi 		 * Return 0, and fallback to normal buffered write.
185e5174baaSOGAWA Hirofumi 		 */
186e5174baaSOGAWA Hirofumi 		loff_t size = offset + iov_length(iov, nr_segs);
187e5174baaSOGAWA Hirofumi 		if (MSDOS_I(inode)->mmu_private < size)
18894412a96SOGAWA Hirofumi 			return 0;
189e5174baaSOGAWA Hirofumi 	}
190e5174baaSOGAWA Hirofumi 
191e5174baaSOGAWA Hirofumi 	/*
192e5174baaSOGAWA Hirofumi 	 * FAT need to use the DIO_LOCKING for avoiding the race
193e5174baaSOGAWA Hirofumi 	 * condition of fat_get_block() and ->truncate().
194e5174baaSOGAWA Hirofumi 	 */
195e5174baaSOGAWA Hirofumi 	return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1966a1d9805SOGAWA Hirofumi 				  offset, nr_segs, fat_get_block, NULL);
197e5174baaSOGAWA Hirofumi }
198e5174baaSOGAWA Hirofumi 
1991da177e4SLinus Torvalds static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	return generic_block_bmap(mapping, block, fat_get_block);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204f5e54d6eSChristoph Hellwig static const struct address_space_operations fat_aops = {
2051da177e4SLinus Torvalds 	.readpage	= fat_readpage,
206a5425d29SOGAWA Hirofumi 	.readpages	= fat_readpages,
2071da177e4SLinus Torvalds 	.writepage	= fat_writepage,
208a5425d29SOGAWA Hirofumi 	.writepages	= fat_writepages,
2091da177e4SLinus Torvalds 	.sync_page	= block_sync_page,
210d7777a25SNick Piggin 	.write_begin	= fat_write_begin,
211d7777a25SNick Piggin 	.write_end	= fat_write_end,
212e5174baaSOGAWA Hirofumi 	.direct_IO	= fat_direct_IO,
2131da177e4SLinus Torvalds 	.bmap		= _fat_bmap
2141da177e4SLinus Torvalds };
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  * New FAT inode stuff. We do the following:
2181da177e4SLinus Torvalds  *	a) i_ino is constant and has nothing with on-disk location.
2191da177e4SLinus Torvalds  *	b) FAT manages its own cache of directory entries.
2201da177e4SLinus Torvalds  *	c) *This* cache is indexed by on-disk location.
2211da177e4SLinus Torvalds  *	d) inode has an associated directory entry, all right, but
2221da177e4SLinus Torvalds  *		it may be unhashed.
2231da177e4SLinus Torvalds  *	e) currently entries are stored within struct inode. That should
2241da177e4SLinus Torvalds  *		change.
2251da177e4SLinus Torvalds  *	f) we deal with races in the following way:
2261da177e4SLinus Torvalds  *		1. readdir() and lookup() do FAT-dir-cache lookup.
2271da177e4SLinus Torvalds  *		2. rename() unhashes the F-d-c entry and rehashes it in
2281da177e4SLinus Torvalds  *			a new place.
2291da177e4SLinus Torvalds  *		3. unlink() and rmdir() unhash F-d-c entry.
2301da177e4SLinus Torvalds  *		4. fat_write_inode() checks whether the thing is unhashed.
2311da177e4SLinus Torvalds  *			If it is we silently return. If it isn't we do bread(),
2321da177e4SLinus Torvalds  *			check if the location is still valid and retry if it
2331da177e4SLinus Torvalds  *			isn't. Otherwise we do changes.
2341da177e4SLinus Torvalds  *		5. Spinlock is used to protect hash/unhash/location check/lookup
2351da177e4SLinus Torvalds  *		6. fat_clear_inode() unhashes the F-d-c entry.
2361da177e4SLinus Torvalds  *		7. lookup() and readdir() do igrab() if they find a F-d-c entry
2371da177e4SLinus Torvalds  *			and consider negative result as cache miss.
2381da177e4SLinus Torvalds  */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static void fat_hash_init(struct super_block *sb)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2431da177e4SLinus Torvalds 	int i;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	spin_lock_init(&sbi->inode_hash_lock);
2461da177e4SLinus Torvalds 	for (i = 0; i < FAT_HASH_SIZE; i++)
2471da177e4SLinus Torvalds 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
2531da177e4SLinus Torvalds 	tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
2541da177e4SLinus Torvalds 	return tmp & FAT_HASH_MASK;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds void fat_attach(struct inode *inode, loff_t i_pos)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
2601da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
2631da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = i_pos;
2641da177e4SLinus Torvalds 	hlist_add_head(&MSDOS_I(inode)->i_fat_hash,
2651da177e4SLinus Torvalds 			sbi->inode_hashtable + fat_hash(sb, i_pos));
2661da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2697c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_attach);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds void fat_detach(struct inode *inode)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
2741da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
2751da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
2761da177e4SLinus Torvalds 	hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
2771da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2807c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_detach);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2851da177e4SLinus Torvalds 	struct hlist_head *head = sbi->inode_hashtable + fat_hash(sb, i_pos);
2861da177e4SLinus Torvalds 	struct hlist_node *_p;
2871da177e4SLinus Torvalds 	struct msdos_inode_info *i;
2881da177e4SLinus Torvalds 	struct inode *inode = NULL;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
2911da177e4SLinus Torvalds 	hlist_for_each_entry(i, _p, head, i_fat_hash) {
2921da177e4SLinus Torvalds 		BUG_ON(i->vfs_inode.i_sb != sb);
2931da177e4SLinus Torvalds 		if (i->i_pos != i_pos)
2941da177e4SLinus Torvalds 			continue;
2951da177e4SLinus Torvalds 		inode = igrab(&i->vfs_inode);
2961da177e4SLinus Torvalds 		if (inode)
2971da177e4SLinus Torvalds 			break;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
3001da177e4SLinus Torvalds 	return inode;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static int is_exec(unsigned char *extension)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	unsigned char *exe_extensions = "EXECOMBAT", *walk;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	for (walk = exe_extensions; *walk; walk += 3)
3081da177e4SLinus Torvalds 		if (!strncmp(extension, walk, 3))
3091da177e4SLinus Torvalds 			return 1;
3101da177e4SLinus Torvalds 	return 0;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds static int fat_calc_dir_size(struct inode *inode)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3161da177e4SLinus Torvalds 	int ret, fclus, dclus;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	inode->i_size = 0;
3191da177e4SLinus Torvalds 	if (MSDOS_I(inode)->i_start == 0)
3201da177e4SLinus Torvalds 		return 0;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
3231da177e4SLinus Torvalds 	if (ret < 0)
3241da177e4SLinus Torvalds 		return ret;
3251da177e4SLinus Torvalds 	inode->i_size = (fclus + 1) << sbi->cluster_bits;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	return 0;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds /* doesn't deal with root inode */
3311da177e4SLinus Torvalds static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
3341da177e4SLinus Torvalds 	int error;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
3371da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
3381da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
3391da177e4SLinus Torvalds 	inode->i_version++;
3401da177e4SLinus Torvalds 	inode->i_generation = get_seconds();
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
3431da177e4SLinus Torvalds 		inode->i_generation &= ~1;
3441da177e4SLinus Torvalds 		inode->i_mode = MSDOS_MKMODE(de->attr,
3451da177e4SLinus Torvalds 			S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
3461da177e4SLinus Torvalds 		inode->i_op = sbi->dir_ops;
3471da177e4SLinus Torvalds 		inode->i_fop = &fat_dir_operations;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
3501da177e4SLinus Torvalds 		if (sbi->fat_bits == 32)
3511da177e4SLinus Torvalds 			MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
3541da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
3551da177e4SLinus Torvalds 		if (error < 0)
3561da177e4SLinus Torvalds 			return error;
3571da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 		inode->i_nlink = fat_subdirs(inode);
3601da177e4SLinus Torvalds 	} else { /* not a directory */
3611da177e4SLinus Torvalds 		inode->i_generation |= 1;
3621da177e4SLinus Torvalds 		inode->i_mode = MSDOS_MKMODE(de->attr,
3639aacd599SOGAWA Hirofumi 		    ((sbi->options.showexec && !is_exec(de->name + 8))
3641da177e4SLinus Torvalds 			? S_IRUGO|S_IWUGO : S_IRWXUGO)
3651da177e4SLinus Torvalds 		    & ~sbi->options.fs_fmask) | S_IFREG;
3661da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
3671da177e4SLinus Torvalds 		if (sbi->fat_bits == 32)
3681da177e4SLinus Torvalds 			MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
3711da177e4SLinus Torvalds 		inode->i_size = le32_to_cpu(de->size);
3721da177e4SLinus Torvalds 		inode->i_op = &fat_file_inode_operations;
3731da177e4SLinus Torvalds 		inode->i_fop = &fat_file_operations;
3741da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &fat_aops;
3751da177e4SLinus Torvalds 		MSDOS_I(inode)->mmu_private = inode->i_size;
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 	if (de->attr & ATTR_SYS) {
3781da177e4SLinus Torvalds 		if (sbi->options.sys_immutable)
3791da177e4SLinus Torvalds 			inode->i_flags |= S_IMMUTABLE;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
3821da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
3831da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
38462a36c43SStephane Kardas 	inode->i_mtime.tv_sec =
3851da177e4SLinus Torvalds 		date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date));
38662a36c43SStephane Kardas 	inode->i_mtime.tv_nsec = 0;
3871da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
3881da177e4SLinus Torvalds 		int secs = de->ctime_cs / 100;
3891da177e4SLinus Torvalds 		int csecs = de->ctime_cs % 100;
3901da177e4SLinus Torvalds 		inode->i_ctime.tv_sec  =
3911da177e4SLinus Torvalds 			date_dos2unix(le16_to_cpu(de->ctime),
3921da177e4SLinus Torvalds 				      le16_to_cpu(de->cdate)) + secs;
3931da177e4SLinus Torvalds 		inode->i_ctime.tv_nsec = csecs * 10000000;
39462a36c43SStephane Kardas 		inode->i_atime.tv_sec =
39531b1f85bSAl Viro 			date_dos2unix(0, le16_to_cpu(de->adate));
39662a36c43SStephane Kardas 		inode->i_atime.tv_nsec = 0;
3971da177e4SLinus Torvalds 	} else
39862a36c43SStephane Kardas 		inode->i_ctime = inode->i_atime = inode->i_mtime;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	return 0;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds struct inode *fat_build_inode(struct super_block *sb,
4041da177e4SLinus Torvalds 			struct msdos_dir_entry *de, loff_t i_pos)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct inode *inode;
4071da177e4SLinus Torvalds 	int err;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	inode = fat_iget(sb, i_pos);
4101da177e4SLinus Torvalds 	if (inode)
4111da177e4SLinus Torvalds 		goto out;
4121da177e4SLinus Torvalds 	inode = new_inode(sb);
4131da177e4SLinus Torvalds 	if (!inode) {
4141da177e4SLinus Torvalds 		inode = ERR_PTR(-ENOMEM);
4151da177e4SLinus Torvalds 		goto out;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 	inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
4181da177e4SLinus Torvalds 	inode->i_version = 1;
4191da177e4SLinus Torvalds 	err = fat_fill_inode(inode, de);
4201da177e4SLinus Torvalds 	if (err) {
4211da177e4SLinus Torvalds 		iput(inode);
4221da177e4SLinus Torvalds 		inode = ERR_PTR(err);
4231da177e4SLinus Torvalds 		goto out;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	fat_attach(inode, i_pos);
4261da177e4SLinus Torvalds 	insert_inode_hash(inode);
4271da177e4SLinus Torvalds out:
4281da177e4SLinus Torvalds 	return inode;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4317c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_build_inode);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static void fat_delete_inode(struct inode *inode)
4341da177e4SLinus Torvalds {
435fef26658SMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
4361da177e4SLinus Torvalds 	inode->i_size = 0;
4371da177e4SLinus Torvalds 	fat_truncate(inode);
4381da177e4SLinus Torvalds 	clear_inode(inode);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static void fat_clear_inode(struct inode *inode)
4421da177e4SLinus Torvalds {
4438f593427SLinus Torvalds 	struct super_block *sb = inode->i_sb;
4448f593427SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
4471da177e4SLinus Torvalds 	fat_cache_inval_inode(inode);
4481da177e4SLinus Torvalds 	hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
4491da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452a6bf6b21SOGAWA Hirofumi static void fat_write_super(struct super_block *sb)
4531da177e4SLinus Torvalds {
454a6bf6b21SOGAWA Hirofumi 	sb->s_dirt = 0;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_RDONLY))
4571da177e4SLinus Torvalds 		fat_clusters_flush(sb);
458a6bf6b21SOGAWA Hirofumi }
459a6bf6b21SOGAWA Hirofumi 
460a6bf6b21SOGAWA Hirofumi static void fat_put_super(struct super_block *sb)
461a6bf6b21SOGAWA Hirofumi {
462a6bf6b21SOGAWA Hirofumi 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (sbi->nls_disk) {
4651da177e4SLinus Torvalds 		unload_nls(sbi->nls_disk);
4661da177e4SLinus Torvalds 		sbi->nls_disk = NULL;
4671da177e4SLinus Torvalds 		sbi->options.codepage = fat_default_codepage;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 	if (sbi->nls_io) {
4701da177e4SLinus Torvalds 		unload_nls(sbi->nls_io);
4711da177e4SLinus Torvalds 		sbi->nls_io = NULL;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	if (sbi->options.iocharset != fat_default_iocharset) {
4741da177e4SLinus Torvalds 		kfree(sbi->options.iocharset);
4751da177e4SLinus Torvalds 		sbi->options.iocharset = fat_default_iocharset;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
4791da177e4SLinus Torvalds 	kfree(sbi);
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
482e18b890bSChristoph Lameter static struct kmem_cache *fat_inode_cachep;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds static struct inode *fat_alloc_inode(struct super_block *sb)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct msdos_inode_info *ei;
4878f593427SLinus Torvalds 	ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
4881da177e4SLinus Torvalds 	if (!ei)
4891da177e4SLinus Torvalds 		return NULL;
4901da177e4SLinus Torvalds 	return &ei->vfs_inode;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static void fat_destroy_inode(struct inode *inode)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4984ba9b9d0SChristoph Lameter static void init_once(struct kmem_cache *cachep, void *foo)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	spin_lock_init(&ei->cache_lru_lock);
5031da177e4SLinus Torvalds 	ei->nr_caches = 0;
5041da177e4SLinus Torvalds 	ei->cache_valid_id = FAT_CACHE_VALID + 1;
5051da177e4SLinus Torvalds 	INIT_LIST_HEAD(&ei->cache_lru);
5061da177e4SLinus Torvalds 	INIT_HLIST_NODE(&ei->i_fat_hash);
5071da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static int __init fat_init_inodecache(void)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	fat_inode_cachep = kmem_cache_create("fat_inode_cache",
5131da177e4SLinus Torvalds 					     sizeof(struct msdos_inode_info),
514fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
515fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
51620c2df83SPaul Mundt 					     init_once);
5171da177e4SLinus Torvalds 	if (fat_inode_cachep == NULL)
5181da177e4SLinus Torvalds 		return -ENOMEM;
5191da177e4SLinus Torvalds 	return 0;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds static void __exit fat_destroy_inodecache(void)
5231da177e4SLinus Torvalds {
5241a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(fat_inode_cachep);
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds static int fat_remount(struct super_block *sb, int *flags, char *data)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
5301da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
5311da177e4SLinus Torvalds 	return 0;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
534726c3342SDavid Howells static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
5351da177e4SLinus Torvalds {
536726c3342SDavid Howells 	struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	/* If the count of free cluster is still unknown, counts it here. */
539606e423eSOGAWA Hirofumi 	if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
540726c3342SDavid Howells 		int err = fat_count_free_clusters(dentry->d_sb);
5411da177e4SLinus Torvalds 		if (err)
5421da177e4SLinus Torvalds 			return err;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 
545726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
5461da177e4SLinus Torvalds 	buf->f_bsize = sbi->cluster_size;
5471da177e4SLinus Torvalds 	buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
5481da177e4SLinus Torvalds 	buf->f_bfree = sbi->free_clusters;
5491da177e4SLinus Torvalds 	buf->f_bavail = sbi->free_clusters;
5501da177e4SLinus Torvalds 	buf->f_namelen = sbi->options.isvfat ? 260 : 12;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	return 0;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds static int fat_write_inode(struct inode *inode, int wait)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
5581da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
5591da177e4SLinus Torvalds 	struct buffer_head *bh;
5601da177e4SLinus Torvalds 	struct msdos_dir_entry *raw_entry;
5611da177e4SLinus Torvalds 	loff_t i_pos;
5621da177e4SLinus Torvalds 	int err = 0;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds retry:
5651da177e4SLinus Torvalds 	i_pos = MSDOS_I(inode)->i_pos;
5661da177e4SLinus Torvalds 	if (inode->i_ino == MSDOS_ROOT_INO || !i_pos)
5671da177e4SLinus Torvalds 		return 0;
5681da177e4SLinus Torvalds 
5698f593427SLinus Torvalds 	lock_super(sb);
5701da177e4SLinus Torvalds 	bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
5711da177e4SLinus Torvalds 	if (!bh) {
5721da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: unable to read inode block "
5731da177e4SLinus Torvalds 		       "for updating (i_pos %lld)\n", i_pos);
5741da177e4SLinus Torvalds 		err = -EIO;
5751da177e4SLinus Torvalds 		goto out;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 	spin_lock(&sbi->inode_hash_lock);
5781da177e4SLinus Torvalds 	if (i_pos != MSDOS_I(inode)->i_pos) {
5791da177e4SLinus Torvalds 		spin_unlock(&sbi->inode_hash_lock);
5801da177e4SLinus Torvalds 		brelse(bh);
5818f593427SLinus Torvalds 		unlock_super(sb);
5821da177e4SLinus Torvalds 		goto retry;
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
5861da177e4SLinus Torvalds 	    [i_pos & (sbi->dir_per_block - 1)];
5871da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode))
5881da177e4SLinus Torvalds 		raw_entry->size = 0;
5891da177e4SLinus Torvalds 	else
5901da177e4SLinus Torvalds 		raw_entry->size = cpu_to_le32(inode->i_size);
5911da177e4SLinus Torvalds 	raw_entry->attr = fat_attr(inode);
5921da177e4SLinus Torvalds 	raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
5931da177e4SLinus Torvalds 	raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
5941da177e4SLinus Torvalds 	fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date);
5951da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
59662a36c43SStephane Kardas 		__le16 atime;
5971da177e4SLinus Torvalds 		fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
59862a36c43SStephane Kardas 		fat_date_unix2dos(inode->i_atime.tv_sec,&atime,&raw_entry->adate);
5991da177e4SLinus Torvalds 		raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 +
6001da177e4SLinus Torvalds 			inode->i_ctime.tv_nsec / 10000000;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	spin_unlock(&sbi->inode_hash_lock);
6031da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
6041da177e4SLinus Torvalds 	if (wait)
6051da177e4SLinus Torvalds 		err = sync_dirty_buffer(bh);
6061da177e4SLinus Torvalds 	brelse(bh);
6071da177e4SLinus Torvalds out:
6088f593427SLinus Torvalds 	unlock_super(sb);
6091da177e4SLinus Torvalds 	return err;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds int fat_sync_inode(struct inode *inode)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	return fat_write_inode(inode, 1);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6177c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_sync_inode);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds static int fat_show_options(struct seq_file *m, struct vfsmount *mnt);
620ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fat_sops = {
6211da177e4SLinus Torvalds 	.alloc_inode	= fat_alloc_inode,
6221da177e4SLinus Torvalds 	.destroy_inode	= fat_destroy_inode,
6231da177e4SLinus Torvalds 	.write_inode	= fat_write_inode,
6241da177e4SLinus Torvalds 	.delete_inode	= fat_delete_inode,
6251da177e4SLinus Torvalds 	.put_super	= fat_put_super,
626a6bf6b21SOGAWA Hirofumi 	.write_super	= fat_write_super,
6271da177e4SLinus Torvalds 	.statfs		= fat_statfs,
6281da177e4SLinus Torvalds 	.clear_inode	= fat_clear_inode,
6291da177e4SLinus Torvalds 	.remount_fs	= fat_remount,
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	.show_options	= fat_show_options,
6321da177e4SLinus Torvalds };
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds /*
6351da177e4SLinus Torvalds  * a FAT file handle with fhtype 3 is
6361da177e4SLinus Torvalds  *  0/  i_ino - for fast, reliable lookup if still in the cache
6371da177e4SLinus Torvalds  *  1/  i_generation - to see if i_ino is still valid
6381da177e4SLinus Torvalds  *          bit 0 == 0 iff directory
6391da177e4SLinus Torvalds  *  2/  i_pos(8-39) - if ino has changed, but still in cache
6401da177e4SLinus Torvalds  *  3/  i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
6411da177e4SLinus Torvalds  *  4/  i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
6421da177e4SLinus Torvalds  *
6431da177e4SLinus Torvalds  * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
6441da177e4SLinus Torvalds  * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
6451da177e4SLinus Torvalds  * of i_logstart is used to store the directory entry offset.
6461da177e4SLinus Torvalds  */
6471da177e4SLinus Torvalds 
6481305edadSChristoph Hellwig static struct dentry *fat_fh_to_dentry(struct super_block *sb,
6491305edadSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
6501da177e4SLinus Torvalds {
6511da177e4SLinus Torvalds 	struct inode *inode = NULL;
6521da177e4SLinus Torvalds 	struct dentry *result;
6531305edadSChristoph Hellwig 	u32 *fh = fid->raw;
6541305edadSChristoph Hellwig 
6551305edadSChristoph Hellwig 	if (fh_len < 5 || fh_type != 3)
6561305edadSChristoph Hellwig 		return NULL;
6571da177e4SLinus Torvalds 
65817f95a7bSDavid Howells 	inode = ilookup(sb, fh[0]);
65917f95a7bSDavid Howells 	if (!inode || inode->i_generation != fh[1]) {
6601da177e4SLinus Torvalds 		if (inode)
6611da177e4SLinus Torvalds 			iput(inode);
6621da177e4SLinus Torvalds 		inode = NULL;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 	if (!inode) {
6651da177e4SLinus Torvalds 		loff_t i_pos;
6661da177e4SLinus Torvalds 		int i_logstart = fh[3] & 0x0fffffff;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 		i_pos = (loff_t)fh[2] << 8;
6691da177e4SLinus Torvalds 		i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		/* try 2 - see if i_pos is in F-d-c
6721da177e4SLinus Torvalds 		 * require i_logstart to be the same
6731da177e4SLinus Torvalds 		 * Will fail if you truncate and then re-write
6741da177e4SLinus Torvalds 		 */
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 		inode = fat_iget(sb, i_pos);
6771da177e4SLinus Torvalds 		if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
6781da177e4SLinus Torvalds 			iput(inode);
6791da177e4SLinus Torvalds 			inode = NULL;
6801da177e4SLinus Torvalds 		}
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds 	if (!inode) {
6831da177e4SLinus Torvalds 		/* For now, do nothing
6841da177e4SLinus Torvalds 		 * What we could do is:
6851da177e4SLinus Torvalds 		 * follow the file starting at fh[4], and record
6861da177e4SLinus Torvalds 		 * the ".." entry, and the name of the fh[2] entry.
6871da177e4SLinus Torvalds 		 * The follow the ".." file finding the next step up.
6881da177e4SLinus Torvalds 		 * This way we build a path to the root of
6891da177e4SLinus Torvalds 		 * the tree. If this works, we lookup the path and so
6901da177e4SLinus Torvalds 		 * get this inode into the cache.
6911da177e4SLinus Torvalds 		 * Finally try the fat_iget lookup again
6921da177e4SLinus Torvalds 		 * If that fails, then weare totally out of luck
6931da177e4SLinus Torvalds 		 * But all that is for another day
6941da177e4SLinus Torvalds 		 */
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 	if (!inode)
6971da177e4SLinus Torvalds 		return ERR_PTR(-ESTALE);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/* now to find a dentry.
7011da177e4SLinus Torvalds 	 * If possible, get a well-connected one
7021da177e4SLinus Torvalds 	 */
7031da177e4SLinus Torvalds 	result = d_alloc_anon(inode);
7041da177e4SLinus Torvalds 	if (result == NULL) {
7051da177e4SLinus Torvalds 		iput(inode);
7061da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 	result->d_op = sb->s_root->d_op;
7091da177e4SLinus Torvalds 	return result;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static int
7131da177e4SLinus Torvalds fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	int len = *lenp;
7161da177e4SLinus Torvalds 	struct inode *inode =  de->d_inode;
7171da177e4SLinus Torvalds 	u32 ipos_h, ipos_m, ipos_l;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	if (len < 5)
7201da177e4SLinus Torvalds 		return 255; /* no room */
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	ipos_h = MSDOS_I(inode)->i_pos >> 8;
7231da177e4SLinus Torvalds 	ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
7241da177e4SLinus Torvalds 	ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
7251da177e4SLinus Torvalds 	*lenp = 5;
7261da177e4SLinus Torvalds 	fh[0] = inode->i_ino;
7271da177e4SLinus Torvalds 	fh[1] = inode->i_generation;
7281da177e4SLinus Torvalds 	fh[2] = ipos_h;
7291da177e4SLinus Torvalds 	fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
7301da177e4SLinus Torvalds 	spin_lock(&de->d_lock);
7311da177e4SLinus Torvalds 	fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
7321da177e4SLinus Torvalds 	spin_unlock(&de->d_lock);
7331da177e4SLinus Torvalds 	return 3;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds static struct dentry *fat_get_parent(struct dentry *child)
7371da177e4SLinus Torvalds {
7388f593427SLinus Torvalds 	struct super_block *sb = child->d_sb;
7391da177e4SLinus Torvalds 	struct buffer_head *bh;
7401da177e4SLinus Torvalds 	struct msdos_dir_entry *de;
7411da177e4SLinus Torvalds 	loff_t i_pos;
7421da177e4SLinus Torvalds 	struct dentry *parent;
7431da177e4SLinus Torvalds 	struct inode *inode;
7441da177e4SLinus Torvalds 	int err;
7451da177e4SLinus Torvalds 
7468f593427SLinus Torvalds 	lock_super(sb);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos);
7491da177e4SLinus Torvalds 	if (err) {
7501da177e4SLinus Torvalds 		parent = ERR_PTR(err);
7511da177e4SLinus Torvalds 		goto out;
7521da177e4SLinus Torvalds 	}
7538f593427SLinus Torvalds 	inode = fat_build_inode(sb, de, i_pos);
7541da177e4SLinus Torvalds 	brelse(bh);
7551da177e4SLinus Torvalds 	if (IS_ERR(inode)) {
756e231c2eeSDavid Howells 		parent = ERR_CAST(inode);
7571da177e4SLinus Torvalds 		goto out;
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 	parent = d_alloc_anon(inode);
7601da177e4SLinus Torvalds 	if (!parent) {
7611da177e4SLinus Torvalds 		iput(inode);
7621da177e4SLinus Torvalds 		parent = ERR_PTR(-ENOMEM);
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds out:
7658f593427SLinus Torvalds 	unlock_super(sb);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	return parent;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
77039655164SChristoph Hellwig static const struct export_operations fat_export_ops = {
7711da177e4SLinus Torvalds 	.encode_fh	= fat_encode_fh,
7721305edadSChristoph Hellwig 	.fh_to_dentry	= fat_fh_to_dentry,
7731da177e4SLinus Torvalds 	.get_parent	= fat_get_parent,
7741da177e4SLinus Torvalds };
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
7791da177e4SLinus Torvalds 	struct fat_mount_options *opts = &sbi->options;
7801da177e4SLinus Torvalds 	int isvfat = opts->isvfat;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (opts->fs_uid != 0)
7831da177e4SLinus Torvalds 		seq_printf(m, ",uid=%u", opts->fs_uid);
7841da177e4SLinus Torvalds 	if (opts->fs_gid != 0)
7851da177e4SLinus Torvalds 		seq_printf(m, ",gid=%u", opts->fs_gid);
7861da177e4SLinus Torvalds 	seq_printf(m, ",fmask=%04o", opts->fs_fmask);
7871da177e4SLinus Torvalds 	seq_printf(m, ",dmask=%04o", opts->fs_dmask);
7881ae43f82SOGAWA Hirofumi 	if (opts->allow_utime)
7891ae43f82SOGAWA Hirofumi 		seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
7901da177e4SLinus Torvalds 	if (sbi->nls_disk)
7911da177e4SLinus Torvalds 		seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
7921da177e4SLinus Torvalds 	if (isvfat) {
7931da177e4SLinus Torvalds 		if (sbi->nls_io)
7941da177e4SLinus Torvalds 			seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 		switch (opts->shortname) {
7971da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
7981da177e4SLinus Torvalds 			seq_puts(m, ",shortname=win95");
7991da177e4SLinus Torvalds 			break;
8001da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
8011da177e4SLinus Torvalds 			seq_puts(m, ",shortname=winnt");
8021da177e4SLinus Torvalds 			break;
8031da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
8041da177e4SLinus Torvalds 			seq_puts(m, ",shortname=mixed");
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 		case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
8071da177e4SLinus Torvalds 			/* seq_puts(m, ",shortname=lower"); */
8081da177e4SLinus Torvalds 			break;
8091da177e4SLinus Torvalds 		default:
8101da177e4SLinus Torvalds 			seq_puts(m, ",shortname=unknown");
8111da177e4SLinus Torvalds 			break;
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	if (opts->name_check != 'n')
8151da177e4SLinus Torvalds 		seq_printf(m, ",check=%c", opts->name_check);
81628ec039cSOGAWA Hirofumi 	if (opts->usefree)
81728ec039cSOGAWA Hirofumi 		seq_puts(m, ",usefree");
8181da177e4SLinus Torvalds 	if (opts->quiet)
8191da177e4SLinus Torvalds 		seq_puts(m, ",quiet");
8201da177e4SLinus Torvalds 	if (opts->showexec)
8211da177e4SLinus Torvalds 		seq_puts(m, ",showexec");
8221da177e4SLinus Torvalds 	if (opts->sys_immutable)
8231da177e4SLinus Torvalds 		seq_puts(m, ",sys_immutable");
8241da177e4SLinus Torvalds 	if (!isvfat) {
8251da177e4SLinus Torvalds 		if (opts->dotsOK)
8261da177e4SLinus Torvalds 			seq_puts(m, ",dotsOK=yes");
8271da177e4SLinus Torvalds 		if (opts->nocase)
8281da177e4SLinus Torvalds 			seq_puts(m, ",nocase");
8291da177e4SLinus Torvalds 	} else {
8301da177e4SLinus Torvalds 		if (opts->utf8)
8311da177e4SLinus Torvalds 			seq_puts(m, ",utf8");
8321da177e4SLinus Torvalds 		if (opts->unicode_xlate)
8331da177e4SLinus Torvalds 			seq_puts(m, ",uni_xlate");
8341da177e4SLinus Torvalds 		if (!opts->numtail)
8351da177e4SLinus Torvalds 			seq_puts(m, ",nonumtail");
8361da177e4SLinus Torvalds 	}
837c1fca3b6SMiklos Szeredi 	if (sbi->options.flush)
838c1fca3b6SMiklos Szeredi 		seq_puts(m, ",flush");
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	return 0;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds enum {
8441da177e4SLinus Torvalds 	Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
8451ae43f82SOGAWA Hirofumi 	Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
8461ae43f82SOGAWA Hirofumi 	Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
8471ae43f82SOGAWA Hirofumi 	Opt_immutable, Opt_dots, Opt_nodots,
8481da177e4SLinus Torvalds 	Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
8491da177e4SLinus Torvalds 	Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
8501da177e4SLinus Torvalds 	Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
851ae78bf9cSChris Mason 	Opt_obsolate, Opt_flush, Opt_err,
8521da177e4SLinus Torvalds };
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static match_table_t fat_tokens = {
8551da177e4SLinus Torvalds 	{Opt_check_r, "check=relaxed"},
8561da177e4SLinus Torvalds 	{Opt_check_s, "check=strict"},
8571da177e4SLinus Torvalds 	{Opt_check_n, "check=normal"},
8581da177e4SLinus Torvalds 	{Opt_check_r, "check=r"},
8591da177e4SLinus Torvalds 	{Opt_check_s, "check=s"},
8601da177e4SLinus Torvalds 	{Opt_check_n, "check=n"},
8611da177e4SLinus Torvalds 	{Opt_uid, "uid=%u"},
8621da177e4SLinus Torvalds 	{Opt_gid, "gid=%u"},
8631da177e4SLinus Torvalds 	{Opt_umask, "umask=%o"},
8641da177e4SLinus Torvalds 	{Opt_dmask, "dmask=%o"},
8651da177e4SLinus Torvalds 	{Opt_fmask, "fmask=%o"},
8661ae43f82SOGAWA Hirofumi 	{Opt_allow_utime, "allow_utime=%o"},
8671da177e4SLinus Torvalds 	{Opt_codepage, "codepage=%u"},
86828ec039cSOGAWA Hirofumi 	{Opt_usefree, "usefree"},
8691da177e4SLinus Torvalds 	{Opt_nocase, "nocase"},
8701da177e4SLinus Torvalds 	{Opt_quiet, "quiet"},
8711da177e4SLinus Torvalds 	{Opt_showexec, "showexec"},
8721da177e4SLinus Torvalds 	{Opt_debug, "debug"},
8731da177e4SLinus Torvalds 	{Opt_immutable, "sys_immutable"},
8741da177e4SLinus Torvalds 	{Opt_obsolate, "conv=binary"},
8751da177e4SLinus Torvalds 	{Opt_obsolate, "conv=text"},
8761da177e4SLinus Torvalds 	{Opt_obsolate, "conv=auto"},
8771da177e4SLinus Torvalds 	{Opt_obsolate, "conv=b"},
8781da177e4SLinus Torvalds 	{Opt_obsolate, "conv=t"},
8791da177e4SLinus Torvalds 	{Opt_obsolate, "conv=a"},
8801da177e4SLinus Torvalds 	{Opt_obsolate, "fat=%u"},
8811da177e4SLinus Torvalds 	{Opt_obsolate, "blocksize=%u"},
8821da177e4SLinus Torvalds 	{Opt_obsolate, "cvf_format=%20s"},
8831da177e4SLinus Torvalds 	{Opt_obsolate, "cvf_options=%100s"},
8841da177e4SLinus Torvalds 	{Opt_obsolate, "posix"},
885ae78bf9cSChris Mason 	{Opt_flush, "flush"},
886ae78bf9cSChris Mason 	{Opt_err, NULL},
8871da177e4SLinus Torvalds };
8881da177e4SLinus Torvalds static match_table_t msdos_tokens = {
8891da177e4SLinus Torvalds 	{Opt_nodots, "nodots"},
8901da177e4SLinus Torvalds 	{Opt_nodots, "dotsOK=no"},
8911da177e4SLinus Torvalds 	{Opt_dots, "dots"},
8921da177e4SLinus Torvalds 	{Opt_dots, "dotsOK=yes"},
8931da177e4SLinus Torvalds 	{Opt_err, NULL}
8941da177e4SLinus Torvalds };
8951da177e4SLinus Torvalds static match_table_t vfat_tokens = {
8961da177e4SLinus Torvalds 	{Opt_charset, "iocharset=%s"},
8971da177e4SLinus Torvalds 	{Opt_shortname_lower, "shortname=lower"},
8981da177e4SLinus Torvalds 	{Opt_shortname_win95, "shortname=win95"},
8991da177e4SLinus Torvalds 	{Opt_shortname_winnt, "shortname=winnt"},
9001da177e4SLinus Torvalds 	{Opt_shortname_mixed, "shortname=mixed"},
9011da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=0"},		/* 0 or no or false */
9021da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=no"},
9031da177e4SLinus Torvalds 	{Opt_utf8_no, "utf8=false"},
9041da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=1"},		/* empty or 1 or yes or true */
9051da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=yes"},
9061da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8=true"},
9071da177e4SLinus Torvalds 	{Opt_utf8_yes, "utf8"},
9081da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=0"},		/* 0 or no or false */
9091da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=no"},
9101da177e4SLinus Torvalds 	{Opt_uni_xl_no, "uni_xlate=false"},
9111da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=1"},	/* empty or 1 or yes or true */
9121da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=yes"},
9131da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate=true"},
9141da177e4SLinus Torvalds 	{Opt_uni_xl_yes, "uni_xlate"},
9151da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=0"},	/* 0 or no or false */
9161da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=no"},
9171da177e4SLinus Torvalds 	{Opt_nonumtail_no, "nonumtail=false"},
9181da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=1"},	/* empty or 1 or yes or true */
9191da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=yes"},
9201da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail=true"},
9211da177e4SLinus Torvalds 	{Opt_nonumtail_yes, "nonumtail"},
9221da177e4SLinus Torvalds 	{Opt_err, NULL}
9231da177e4SLinus Torvalds };
9241da177e4SLinus Torvalds 
92541a34a4fSChristoph Hellwig static int parse_options(char *options, int is_vfat, int silent, int *debug,
9261da177e4SLinus Torvalds 			 struct fat_mount_options *opts)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	char *p;
9291da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
9301da177e4SLinus Torvalds 	int option;
9311da177e4SLinus Torvalds 	char *iocharset;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	opts->isvfat = is_vfat;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	opts->fs_uid = current->uid;
9361da177e4SLinus Torvalds 	opts->fs_gid = current->gid;
9371da177e4SLinus Torvalds 	opts->fs_fmask = opts->fs_dmask = current->fs->umask;
9381ae43f82SOGAWA Hirofumi 	opts->allow_utime = -1;
9391da177e4SLinus Torvalds 	opts->codepage = fat_default_codepage;
9401da177e4SLinus Torvalds 	opts->iocharset = fat_default_iocharset;
9411da177e4SLinus Torvalds 	if (is_vfat)
9421da177e4SLinus Torvalds 		opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
9431da177e4SLinus Torvalds 	else
9441da177e4SLinus Torvalds 		opts->shortname = 0;
9451da177e4SLinus Torvalds 	opts->name_check = 'n';
9461da177e4SLinus Torvalds 	opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
9471da177e4SLinus Torvalds 	opts->utf8 = opts->unicode_xlate = 0;
9481da177e4SLinus Torvalds 	opts->numtail = 1;
94928ec039cSOGAWA Hirofumi 	opts->usefree = opts->nocase = 0;
9501da177e4SLinus Torvalds 	*debug = 0;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	if (!options)
953*8d44d974SOGAWA Hirofumi 		goto out;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
9561da177e4SLinus Torvalds 		int token;
9571da177e4SLinus Torvalds 		if (!*p)
9581da177e4SLinus Torvalds 			continue;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 		token = match_token(p, fat_tokens, args);
9611da177e4SLinus Torvalds 		if (token == Opt_err) {
9621da177e4SLinus Torvalds 			if (is_vfat)
9631da177e4SLinus Torvalds 				token = match_token(p, vfat_tokens, args);
9641da177e4SLinus Torvalds 			else
9651da177e4SLinus Torvalds 				token = match_token(p, msdos_tokens, args);
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		switch (token) {
9681da177e4SLinus Torvalds 		case Opt_check_s:
9691da177e4SLinus Torvalds 			opts->name_check = 's';
9701da177e4SLinus Torvalds 			break;
9711da177e4SLinus Torvalds 		case Opt_check_r:
9721da177e4SLinus Torvalds 			opts->name_check = 'r';
9731da177e4SLinus Torvalds 			break;
9741da177e4SLinus Torvalds 		case Opt_check_n:
9751da177e4SLinus Torvalds 			opts->name_check = 'n';
9761da177e4SLinus Torvalds 			break;
97728ec039cSOGAWA Hirofumi 		case Opt_usefree:
97828ec039cSOGAWA Hirofumi 			opts->usefree = 1;
97928ec039cSOGAWA Hirofumi 			break;
9801da177e4SLinus Torvalds 		case Opt_nocase:
9811da177e4SLinus Torvalds 			if (!is_vfat)
9821da177e4SLinus Torvalds 				opts->nocase = 1;
9831da177e4SLinus Torvalds 			else {
9841da177e4SLinus Torvalds 				/* for backward compatibility */
9851da177e4SLinus Torvalds 				opts->shortname = VFAT_SFN_DISPLAY_WIN95
9861da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
9871da177e4SLinus Torvalds 			}
9881da177e4SLinus Torvalds 			break;
9891da177e4SLinus Torvalds 		case Opt_quiet:
9901da177e4SLinus Torvalds 			opts->quiet = 1;
9911da177e4SLinus Torvalds 			break;
9921da177e4SLinus Torvalds 		case Opt_showexec:
9931da177e4SLinus Torvalds 			opts->showexec = 1;
9941da177e4SLinus Torvalds 			break;
9951da177e4SLinus Torvalds 		case Opt_debug:
9961da177e4SLinus Torvalds 			*debug = 1;
9971da177e4SLinus Torvalds 			break;
9981da177e4SLinus Torvalds 		case Opt_immutable:
9991da177e4SLinus Torvalds 			opts->sys_immutable = 1;
10001da177e4SLinus Torvalds 			break;
10011da177e4SLinus Torvalds 		case Opt_uid:
10021da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
10031da177e4SLinus Torvalds 				return 0;
10041da177e4SLinus Torvalds 			opts->fs_uid = option;
10051da177e4SLinus Torvalds 			break;
10061da177e4SLinus Torvalds 		case Opt_gid:
10071da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
10081da177e4SLinus Torvalds 				return 0;
10091da177e4SLinus Torvalds 			opts->fs_gid = option;
10101da177e4SLinus Torvalds 			break;
10111da177e4SLinus Torvalds 		case Opt_umask:
10121da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
10131da177e4SLinus Torvalds 				return 0;
10141da177e4SLinus Torvalds 			opts->fs_fmask = opts->fs_dmask = option;
10151da177e4SLinus Torvalds 			break;
10161da177e4SLinus Torvalds 		case Opt_dmask:
10171da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
10181da177e4SLinus Torvalds 				return 0;
10191da177e4SLinus Torvalds 			opts->fs_dmask = option;
10201da177e4SLinus Torvalds 			break;
10211da177e4SLinus Torvalds 		case Opt_fmask:
10221da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
10231da177e4SLinus Torvalds 				return 0;
10241da177e4SLinus Torvalds 			opts->fs_fmask = option;
10251da177e4SLinus Torvalds 			break;
10261ae43f82SOGAWA Hirofumi 		case Opt_allow_utime:
10271ae43f82SOGAWA Hirofumi 			if (match_octal(&args[0], &option))
10281ae43f82SOGAWA Hirofumi 				return 0;
10291ae43f82SOGAWA Hirofumi 			opts->allow_utime = option & (S_IWGRP | S_IWOTH);
10301ae43f82SOGAWA Hirofumi 			break;
10311da177e4SLinus Torvalds 		case Opt_codepage:
10321da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
10331da177e4SLinus Torvalds 				return 0;
10341da177e4SLinus Torvalds 			opts->codepage = option;
10351da177e4SLinus Torvalds 			break;
1036ae78bf9cSChris Mason 		case Opt_flush:
1037ae78bf9cSChris Mason 			opts->flush = 1;
1038ae78bf9cSChris Mason 			break;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		/* msdos specific */
10411da177e4SLinus Torvalds 		case Opt_dots:
10421da177e4SLinus Torvalds 			opts->dotsOK = 1;
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 		case Opt_nodots:
10451da177e4SLinus Torvalds 			opts->dotsOK = 0;
10461da177e4SLinus Torvalds 			break;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 		/* vfat specific */
10491da177e4SLinus Torvalds 		case Opt_charset:
10501da177e4SLinus Torvalds 			if (opts->iocharset != fat_default_iocharset)
10511da177e4SLinus Torvalds 				kfree(opts->iocharset);
10521da177e4SLinus Torvalds 			iocharset = match_strdup(&args[0]);
10531da177e4SLinus Torvalds 			if (!iocharset)
10541da177e4SLinus Torvalds 				return -ENOMEM;
10551da177e4SLinus Torvalds 			opts->iocharset = iocharset;
10561da177e4SLinus Torvalds 			break;
10571da177e4SLinus Torvalds 		case Opt_shortname_lower:
10581da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_LOWER
10591da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10601da177e4SLinus Torvalds 			break;
10611da177e4SLinus Torvalds 		case Opt_shortname_win95:
10621da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WIN95
10631da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10641da177e4SLinus Torvalds 			break;
10651da177e4SLinus Torvalds 		case Opt_shortname_winnt:
10661da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
10671da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WINNT;
10681da177e4SLinus Torvalds 			break;
10691da177e4SLinus Torvalds 		case Opt_shortname_mixed:
10701da177e4SLinus Torvalds 			opts->shortname = VFAT_SFN_DISPLAY_WINNT
10711da177e4SLinus Torvalds 					| VFAT_SFN_CREATE_WIN95;
10721da177e4SLinus Torvalds 			break;
10731da177e4SLinus Torvalds 		case Opt_utf8_no:		/* 0 or no or false */
10741da177e4SLinus Torvalds 			opts->utf8 = 0;
10751da177e4SLinus Torvalds 			break;
10761da177e4SLinus Torvalds 		case Opt_utf8_yes:		/* empty or 1 or yes or true */
10771da177e4SLinus Torvalds 			opts->utf8 = 1;
10781da177e4SLinus Torvalds 			break;
10791da177e4SLinus Torvalds 		case Opt_uni_xl_no:		/* 0 or no or false */
10801da177e4SLinus Torvalds 			opts->unicode_xlate = 0;
10811da177e4SLinus Torvalds 			break;
10821da177e4SLinus Torvalds 		case Opt_uni_xl_yes:		/* empty or 1 or yes or true */
10831da177e4SLinus Torvalds 			opts->unicode_xlate = 1;
10841da177e4SLinus Torvalds 			break;
10851da177e4SLinus Torvalds 		case Opt_nonumtail_no:		/* 0 or no or false */
10861da177e4SLinus Torvalds 			opts->numtail = 1;	/* negated option */
10871da177e4SLinus Torvalds 			break;
10881da177e4SLinus Torvalds 		case Opt_nonumtail_yes:		/* empty or 1 or yes or true */
10891da177e4SLinus Torvalds 			opts->numtail = 0;	/* negated option */
10901da177e4SLinus Torvalds 			break;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		/* obsolete mount options */
10931da177e4SLinus Torvalds 		case Opt_obsolate:
10941da177e4SLinus Torvalds 			printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
10951da177e4SLinus Torvalds 			       "not supported now\n", p);
10961da177e4SLinus Torvalds 			break;
10971da177e4SLinus Torvalds 		/* unknown option */
10981da177e4SLinus Torvalds 		default:
109941a34a4fSChristoph Hellwig 			if (!silent) {
110041a34a4fSChristoph Hellwig 				printk(KERN_ERR
110141a34a4fSChristoph Hellwig 				       "FAT: Unrecognized mount option \"%s\" "
11021da177e4SLinus Torvalds 				       "or missing value\n", p);
110341a34a4fSChristoph Hellwig 			}
11041da177e4SLinus Torvalds 			return -EINVAL;
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 	}
1107*8d44d974SOGAWA Hirofumi 
1108*8d44d974SOGAWA Hirofumi out:
11094de151d8SAlexey Dobriyan 	/* UTF-8 doesn't provide FAT semantics */
11101da177e4SLinus Torvalds 	if (!strcmp(opts->iocharset, "utf8")) {
11111da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: utf8 is not a recommended IO charset"
1112*8d44d974SOGAWA Hirofumi 		       " for FAT filesystems, filesystem will be "
1113*8d44d974SOGAWA Hirofumi 		       "case sensitive!\n");
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11161ae43f82SOGAWA Hirofumi 	/* If user doesn't specify allow_utime, it's initialized from dmask. */
11171ae43f82SOGAWA Hirofumi 	if (opts->allow_utime == (unsigned short)-1)
11181ae43f82SOGAWA Hirofumi 		opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
11191da177e4SLinus Torvalds 	if (opts->unicode_xlate)
11201da177e4SLinus Torvalds 		opts->utf8 = 0;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	return 0;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds static int fat_read_root(struct inode *inode)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
11281da177e4SLinus Torvalds 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
11291da177e4SLinus Torvalds 	int error;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	MSDOS_I(inode)->i_pos = 0;
11321da177e4SLinus Torvalds 	inode->i_uid = sbi->options.fs_uid;
11331da177e4SLinus Torvalds 	inode->i_gid = sbi->options.fs_gid;
11341da177e4SLinus Torvalds 	inode->i_version++;
11351da177e4SLinus Torvalds 	inode->i_generation = 0;
11361da177e4SLinus Torvalds 	inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
11371da177e4SLinus Torvalds 	inode->i_op = sbi->dir_ops;
11381da177e4SLinus Torvalds 	inode->i_fop = &fat_dir_operations;
11391da177e4SLinus Torvalds 	if (sbi->fat_bits == 32) {
11401da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = sbi->root_cluster;
11411da177e4SLinus Torvalds 		error = fat_calc_dir_size(inode);
11421da177e4SLinus Torvalds 		if (error < 0)
11431da177e4SLinus Torvalds 			return error;
11441da177e4SLinus Torvalds 	} else {
11451da177e4SLinus Torvalds 		MSDOS_I(inode)->i_start = 0;
11461da177e4SLinus Torvalds 		inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 	inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
11491da177e4SLinus Torvalds 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
11501da177e4SLinus Torvalds 	MSDOS_I(inode)->i_logstart = 0;
11511da177e4SLinus Torvalds 	MSDOS_I(inode)->mmu_private = inode->i_size;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	MSDOS_I(inode)->i_attrs = ATTR_NONE;
11541da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
11551da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
11561da177e4SLinus Torvalds 	inode->i_nlink = fat_subdirs(inode)+2;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	return 0;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds /*
11621da177e4SLinus Torvalds  * Read the super block of an MS-DOS FS.
11631da177e4SLinus Torvalds  */
11641da177e4SLinus Torvalds int fat_fill_super(struct super_block *sb, void *data, int silent,
1165754661f1SArjan van de Ven 		   const struct inode_operations *fs_dir_inode_ops, int isvfat)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	struct inode *root_inode = NULL;
11681da177e4SLinus Torvalds 	struct buffer_head *bh;
11691da177e4SLinus Torvalds 	struct fat_boot_sector *b;
11701da177e4SLinus Torvalds 	struct msdos_sb_info *sbi;
11711da177e4SLinus Torvalds 	u16 logical_sector_size;
11721da177e4SLinus Torvalds 	u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
11731da177e4SLinus Torvalds 	int debug;
11741da177e4SLinus Torvalds 	unsigned int media;
11751da177e4SLinus Torvalds 	long error;
11761da177e4SLinus Torvalds 	char buf[50];
11771da177e4SLinus Torvalds 
11788f593427SLinus Torvalds 	/*
11798f593427SLinus Torvalds 	 * GFP_KERNEL is ok here, because while we do hold the
11808f593427SLinus Torvalds 	 * supeblock lock, memory pressure can't call back into
11818f593427SLinus Torvalds 	 * the filesystem, since we're only just about to mount
11828f593427SLinus Torvalds 	 * it and have no inodes etc active!
11838f593427SLinus Torvalds 	 */
1184f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
11851da177e4SLinus Torvalds 	if (!sbi)
11861da177e4SLinus Torvalds 		return -ENOMEM;
11871da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
11901da177e4SLinus Torvalds 	sb->s_magic = MSDOS_SUPER_MAGIC;
11911da177e4SLinus Torvalds 	sb->s_op = &fat_sops;
11921da177e4SLinus Torvalds 	sb->s_export_op = &fat_export_ops;
11931da177e4SLinus Torvalds 	sbi->dir_ops = fs_dir_inode_ops;
11941da177e4SLinus Torvalds 
119541a34a4fSChristoph Hellwig 	error = parse_options(data, isvfat, silent, &debug, &sbi->options);
11961da177e4SLinus Torvalds 	if (error)
11971da177e4SLinus Torvalds 		goto out_fail;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	error = -EIO;
12001da177e4SLinus Torvalds 	sb_min_blocksize(sb, 512);
12011da177e4SLinus Torvalds 	bh = sb_bread(sb, 0);
12021da177e4SLinus Torvalds 	if (bh == NULL) {
12031da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: unable to read boot sector\n");
12041da177e4SLinus Torvalds 		goto out_fail;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	b = (struct fat_boot_sector *) bh->b_data;
12081da177e4SLinus Torvalds 	if (!b->reserved) {
12091da177e4SLinus Torvalds 		if (!silent)
12101da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
12111da177e4SLinus Torvalds 		brelse(bh);
12121da177e4SLinus Torvalds 		goto out_invalid;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 	if (!b->fats) {
12151da177e4SLinus Torvalds 		if (!silent)
12161da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bogus number of FAT structure\n");
12171da177e4SLinus Torvalds 		brelse(bh);
12181da177e4SLinus Torvalds 		goto out_invalid;
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	/*
12221da177e4SLinus Torvalds 	 * Earlier we checked here that b->secs_track and b->head are nonzero,
12231da177e4SLinus Torvalds 	 * but it turns out valid FAT filesystems can have zero there.
12241da177e4SLinus Torvalds 	 */
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	media = b->media;
122773f20e58SAndrew Morton 	if (!fat_valid_media(media)) {
12281da177e4SLinus Torvalds 		if (!silent)
12291da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
12301da177e4SLinus Torvalds 			       media);
12311da177e4SLinus Torvalds 		brelse(bh);
12321da177e4SLinus Torvalds 		goto out_invalid;
12331da177e4SLinus Torvalds 	}
1234803f445fSHarvey Harrison 	logical_sector_size = get_unaligned_le16(&b->sector_size);
1235e7d709c0SVignesh Babu BM 	if (!is_power_of_2(logical_sector_size)
12361da177e4SLinus Torvalds 	    || (logical_sector_size < 512)
12379f354858SOlof Johansson 	    || (logical_sector_size > 4096)) {
12381da177e4SLinus Torvalds 		if (!silent)
12391da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bogus logical sector size %u\n",
12401da177e4SLinus Torvalds 			       logical_sector_size);
12411da177e4SLinus Torvalds 		brelse(bh);
12421da177e4SLinus Torvalds 		goto out_invalid;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 	sbi->sec_per_clus = b->sec_per_clus;
1245e7d709c0SVignesh Babu BM 	if (!is_power_of_2(sbi->sec_per_clus)) {
12461da177e4SLinus Torvalds 		if (!silent)
12471da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
12481da177e4SLinus Torvalds 			       sbi->sec_per_clus);
12491da177e4SLinus Torvalds 		brelse(bh);
12501da177e4SLinus Torvalds 		goto out_invalid;
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	if (logical_sector_size < sb->s_blocksize) {
12541da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: logical sector size too small for device"
12551da177e4SLinus Torvalds 		       " (logical sector size = %u)\n", logical_sector_size);
12561da177e4SLinus Torvalds 		brelse(bh);
12571da177e4SLinus Torvalds 		goto out_fail;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds 	if (logical_sector_size > sb->s_blocksize) {
12601da177e4SLinus Torvalds 		brelse(bh);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		if (!sb_set_blocksize(sb, logical_sector_size)) {
12631da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: unable to set blocksize %u\n",
12641da177e4SLinus Torvalds 			       logical_sector_size);
12651da177e4SLinus Torvalds 			goto out_fail;
12661da177e4SLinus Torvalds 		}
12671da177e4SLinus Torvalds 		bh = sb_bread(sb, 0);
12681da177e4SLinus Torvalds 		if (bh == NULL) {
12691da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: unable to read boot sector"
12701da177e4SLinus Torvalds 			       " (logical sector size = %lu)\n",
12711da177e4SLinus Torvalds 			       sb->s_blocksize);
12721da177e4SLinus Torvalds 			goto out_fail;
12731da177e4SLinus Torvalds 		}
12741da177e4SLinus Torvalds 		b = (struct fat_boot_sector *) bh->b_data;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
12781da177e4SLinus Torvalds 	sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
12791da177e4SLinus Torvalds 	sbi->fats = b->fats;
12801da177e4SLinus Torvalds 	sbi->fat_bits = 0;		/* Don't know yet */
12811da177e4SLinus Torvalds 	sbi->fat_start = le16_to_cpu(b->reserved);
12821da177e4SLinus Torvalds 	sbi->fat_length = le16_to_cpu(b->fat_length);
12831da177e4SLinus Torvalds 	sbi->root_cluster = 0;
12841da177e4SLinus Torvalds 	sbi->free_clusters = -1;	/* Don't know yet */
1285606e423eSOGAWA Hirofumi 	sbi->free_clus_valid = 0;
12861da177e4SLinus Torvalds 	sbi->prev_free = FAT_START_ENT;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	if (!sbi->fat_length && b->fat32_length) {
12891da177e4SLinus Torvalds 		struct fat_boot_fsinfo *fsinfo;
12901da177e4SLinus Torvalds 		struct buffer_head *fsinfo_bh;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		/* Must be FAT32 */
12931da177e4SLinus Torvalds 		sbi->fat_bits = 32;
12941da177e4SLinus Torvalds 		sbi->fat_length = le32_to_cpu(b->fat32_length);
12951da177e4SLinus Torvalds 		sbi->root_cluster = le32_to_cpu(b->root_cluster);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 		sb->s_maxbytes = 0xffffffff;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		/* MC - if info_sector is 0, don't multiply by 0 */
13001da177e4SLinus Torvalds 		sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
13011da177e4SLinus Torvalds 		if (sbi->fsinfo_sector == 0)
13021da177e4SLinus Torvalds 			sbi->fsinfo_sector = 1;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
13051da177e4SLinus Torvalds 		if (fsinfo_bh == NULL) {
13061da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bread failed, FSINFO block"
13071da177e4SLinus Torvalds 			       " (sector = %lu)\n", sbi->fsinfo_sector);
13081da177e4SLinus Torvalds 			brelse(bh);
13091da177e4SLinus Torvalds 			goto out_fail;
13101da177e4SLinus Torvalds 		}
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
13131da177e4SLinus Torvalds 		if (!IS_FSINFO(fsinfo)) {
1314b4cf9c34SVegard Nossum 			printk(KERN_WARNING "FAT: Invalid FSINFO signature: "
1315b4cf9c34SVegard Nossum 			       "0x%08x, 0x%08x (sector = %lu)\n",
13161da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature1),
13171da177e4SLinus Torvalds 			       le32_to_cpu(fsinfo->signature2),
13181da177e4SLinus Torvalds 			       sbi->fsinfo_sector);
13191da177e4SLinus Torvalds 		} else {
132028ec039cSOGAWA Hirofumi 			if (sbi->options.usefree)
1321606e423eSOGAWA Hirofumi 				sbi->free_clus_valid = 1;
1322606e423eSOGAWA Hirofumi 			sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
13231da177e4SLinus Torvalds 			sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		brelse(fsinfo_bh);
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
13301da177e4SLinus Torvalds 	sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1333803f445fSHarvey Harrison 	sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
13341da177e4SLinus Torvalds 	if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
13351da177e4SLinus Torvalds 		if (!silent)
13361da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: bogus directroy-entries per block"
13371da177e4SLinus Torvalds 			       " (%u)\n", sbi->dir_entries);
13381da177e4SLinus Torvalds 		brelse(bh);
13391da177e4SLinus Torvalds 		goto out_invalid;
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	rootdir_sectors = sbi->dir_entries
13431da177e4SLinus Torvalds 		* sizeof(struct msdos_dir_entry) / sb->s_blocksize;
13441da177e4SLinus Torvalds 	sbi->data_start = sbi->dir_start + rootdir_sectors;
1345803f445fSHarvey Harrison 	total_sectors = get_unaligned_le16(&b->sectors);
13461da177e4SLinus Torvalds 	if (total_sectors == 0)
13471da177e4SLinus Torvalds 		total_sectors = le32_to_cpu(b->total_sect);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	if (sbi->fat_bits != 32)
13521da177e4SLinus Torvalds 		sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	/* check that FAT table does not overflow */
13551da177e4SLinus Torvalds 	fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
13561da177e4SLinus Torvalds 	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
13571da177e4SLinus Torvalds 	if (total_clusters > MAX_FAT(sb)) {
13581da177e4SLinus Torvalds 		if (!silent)
13591da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
13601da177e4SLinus Torvalds 			       total_clusters);
13611da177e4SLinus Torvalds 		brelse(bh);
13621da177e4SLinus Torvalds 		goto out_invalid;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	sbi->max_cluster = total_clusters + FAT_START_ENT;
13661da177e4SLinus Torvalds 	/* check the free_clusters, it's not necessarily correct */
13671da177e4SLinus Torvalds 	if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
13681da177e4SLinus Torvalds 		sbi->free_clusters = -1;
13691da177e4SLinus Torvalds 	/* check the prev_free, it's not necessarily correct */
13701da177e4SLinus Torvalds 	sbi->prev_free %= sbi->max_cluster;
13711da177e4SLinus Torvalds 	if (sbi->prev_free < FAT_START_ENT)
13721da177e4SLinus Torvalds 		sbi->prev_free = FAT_START_ENT;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	brelse(bh);
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
13771da177e4SLinus Torvalds 	fat_hash_init(sb);
13781da177e4SLinus Torvalds 	fat_ent_access_init(sb);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	/*
13811da177e4SLinus Torvalds 	 * The low byte of FAT's first entry must have same value with
13821da177e4SLinus Torvalds 	 * media-field.  But in real world, too many devices is
13831da177e4SLinus Torvalds 	 * writing wrong value.  So, removed that validity check.
13841da177e4SLinus Torvalds 	 *
13851da177e4SLinus Torvalds 	 * if (FAT_FIRST_ENT(sb, media) != first)
13861da177e4SLinus Torvalds 	 */
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	error = -EINVAL;
13891da177e4SLinus Torvalds 	sprintf(buf, "cp%d", sbi->options.codepage);
13901da177e4SLinus Torvalds 	sbi->nls_disk = load_nls(buf);
13911da177e4SLinus Torvalds 	if (!sbi->nls_disk) {
13921da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: codepage %s not found\n", buf);
13931da177e4SLinus Torvalds 		goto out_fail;
13941da177e4SLinus Torvalds 	}
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	/* FIXME: utf8 is using iocharset for upper/lower conversion */
13971da177e4SLinus Torvalds 	if (sbi->options.isvfat) {
13981da177e4SLinus Torvalds 		sbi->nls_io = load_nls(sbi->options.iocharset);
13991da177e4SLinus Torvalds 		if (!sbi->nls_io) {
14001da177e4SLinus Torvalds 			printk(KERN_ERR "FAT: IO charset %s not found\n",
14011da177e4SLinus Torvalds 			       sbi->options.iocharset);
14021da177e4SLinus Torvalds 			goto out_fail;
14031da177e4SLinus Torvalds 		}
14041da177e4SLinus Torvalds 	}
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	error = -ENOMEM;
14071da177e4SLinus Torvalds 	root_inode = new_inode(sb);
14081da177e4SLinus Torvalds 	if (!root_inode)
14091da177e4SLinus Torvalds 		goto out_fail;
14101da177e4SLinus Torvalds 	root_inode->i_ino = MSDOS_ROOT_INO;
14111da177e4SLinus Torvalds 	root_inode->i_version = 1;
14121da177e4SLinus Torvalds 	error = fat_read_root(root_inode);
14131da177e4SLinus Torvalds 	if (error < 0)
14141da177e4SLinus Torvalds 		goto out_fail;
14151da177e4SLinus Torvalds 	error = -ENOMEM;
14161da177e4SLinus Torvalds 	insert_inode_hash(root_inode);
14171da177e4SLinus Torvalds 	sb->s_root = d_alloc_root(root_inode);
14181da177e4SLinus Torvalds 	if (!sb->s_root) {
14191da177e4SLinus Torvalds 		printk(KERN_ERR "FAT: get root inode failed\n");
14201da177e4SLinus Torvalds 		goto out_fail;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds out_invalid:
14261da177e4SLinus Torvalds 	error = -EINVAL;
14271da177e4SLinus Torvalds 	if (!silent)
14281da177e4SLinus Torvalds 		printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
14291da177e4SLinus Torvalds 		       " on dev %s.\n", sb->s_id);
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds out_fail:
14321da177e4SLinus Torvalds 	if (root_inode)
14331da177e4SLinus Torvalds 		iput(root_inode);
14341da177e4SLinus Torvalds 	if (sbi->nls_io)
14351da177e4SLinus Torvalds 		unload_nls(sbi->nls_io);
14361da177e4SLinus Torvalds 	if (sbi->nls_disk)
14371da177e4SLinus Torvalds 		unload_nls(sbi->nls_disk);
14381da177e4SLinus Torvalds 	if (sbi->options.iocharset != fat_default_iocharset)
14391da177e4SLinus Torvalds 		kfree(sbi->options.iocharset);
14401da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
14411da177e4SLinus Torvalds 	kfree(sbi);
14421da177e4SLinus Torvalds 	return error;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14457c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_fill_super);
14461da177e4SLinus Torvalds 
1447ae78bf9cSChris Mason /*
1448ae78bf9cSChris Mason  * helper function for fat_flush_inodes.  This writes both the inode
1449ae78bf9cSChris Mason  * and the file data blocks, waiting for in flight data blocks before
1450ae78bf9cSChris Mason  * the start of the call.  It does not wait for any io started
1451ae78bf9cSChris Mason  * during the call
1452ae78bf9cSChris Mason  */
1453ae78bf9cSChris Mason static int writeback_inode(struct inode *inode)
1454ae78bf9cSChris Mason {
1455ae78bf9cSChris Mason 
1456ae78bf9cSChris Mason 	int ret;
1457ae78bf9cSChris Mason 	struct address_space *mapping = inode->i_mapping;
1458ae78bf9cSChris Mason 	struct writeback_control wbc = {
1459ae78bf9cSChris Mason 	       .sync_mode = WB_SYNC_NONE,
1460ae78bf9cSChris Mason 	      .nr_to_write = 0,
1461ae78bf9cSChris Mason 	};
1462ae78bf9cSChris Mason 	/* if we used WB_SYNC_ALL, sync_inode waits for the io for the
1463ae78bf9cSChris Mason 	* inode to finish.  So WB_SYNC_NONE is sent down to sync_inode
1464ae78bf9cSChris Mason 	* and filemap_fdatawrite is used for the data blocks
1465ae78bf9cSChris Mason 	*/
1466ae78bf9cSChris Mason 	ret = sync_inode(inode, &wbc);
1467ae78bf9cSChris Mason 	if (!ret)
1468ae78bf9cSChris Mason 	       ret = filemap_fdatawrite(mapping);
1469ae78bf9cSChris Mason 	return ret;
1470ae78bf9cSChris Mason }
1471ae78bf9cSChris Mason 
1472ae78bf9cSChris Mason /*
1473ae78bf9cSChris Mason  * write data and metadata corresponding to i1 and i2.  The io is
1474ae78bf9cSChris Mason  * started but we do not wait for any of it to finish.
1475ae78bf9cSChris Mason  *
1476ae78bf9cSChris Mason  * filemap_flush is used for the block device, so if there is a dirty
1477ae78bf9cSChris Mason  * page for a block already in flight, we will not wait and start the
1478ae78bf9cSChris Mason  * io over again
1479ae78bf9cSChris Mason  */
1480ae78bf9cSChris Mason int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1481ae78bf9cSChris Mason {
1482ae78bf9cSChris Mason 	int ret = 0;
1483ae78bf9cSChris Mason 	if (!MSDOS_SB(sb)->options.flush)
1484ae78bf9cSChris Mason 		return 0;
1485ae78bf9cSChris Mason 	if (i1)
1486ae78bf9cSChris Mason 		ret = writeback_inode(i1);
1487ae78bf9cSChris Mason 	if (!ret && i2)
1488ae78bf9cSChris Mason 		ret = writeback_inode(i2);
148997e860d3SEric Sesterhenn 	if (!ret) {
1490ae78bf9cSChris Mason 		struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1491ae78bf9cSChris Mason 		ret = filemap_flush(mapping);
1492ae78bf9cSChris Mason 	}
1493ae78bf9cSChris Mason 	return ret;
1494ae78bf9cSChris Mason }
1495ae78bf9cSChris Mason EXPORT_SYMBOL_GPL(fat_flush_inodes);
1496ae78bf9cSChris Mason 
14971da177e4SLinus Torvalds static int __init init_fat_fs(void)
14981da177e4SLinus Torvalds {
1499532a39a3SPekka J Enberg 	int err;
15001da177e4SLinus Torvalds 
1501532a39a3SPekka J Enberg 	err = fat_cache_init();
1502532a39a3SPekka J Enberg 	if (err)
1503532a39a3SPekka J Enberg 		return err;
1504532a39a3SPekka J Enberg 
1505532a39a3SPekka J Enberg 	err = fat_init_inodecache();
1506532a39a3SPekka J Enberg 	if (err)
1507532a39a3SPekka J Enberg 		goto failed;
1508532a39a3SPekka J Enberg 
1509532a39a3SPekka J Enberg 	return 0;
1510532a39a3SPekka J Enberg 
1511532a39a3SPekka J Enberg failed:
1512532a39a3SPekka J Enberg 	fat_cache_destroy();
1513532a39a3SPekka J Enberg 	return err;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds static void __exit exit_fat_fs(void)
15171da177e4SLinus Torvalds {
15181da177e4SLinus Torvalds 	fat_cache_destroy();
15191da177e4SLinus Torvalds 	fat_destroy_inodecache();
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds module_init(init_fat_fs)
15231da177e4SLinus Torvalds module_exit(exit_fat_fs)
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1526