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