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
10 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/time.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/pagemap.h>
19 #include <linux/mpage.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mount.h>
22 #include <linux/aio.h>
23 #include <linux/vfs.h>
24 #include <linux/parser.h>
25 #include <linux/uio.h>
26 #include <linux/writeback.h>
27 #include <linux/log2.h>
28 #include <linux/hash.h>
29 #include <linux/blkdev.h>
30 #include <asm/unaligned.h>
33 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
34 /* if user don't select VFAT, this is undefined. */
35 #define CONFIG_FAT_DEFAULT_IOCHARSET ""
38 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
39 static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
42 static int fat_add_cluster(struct inode *inode)
46 err = fat_alloc_clusters(inode, &cluster, 1);
49 /* FIXME: this cluster should be added after data of this
50 * cluster is writed */
51 err = fat_chain_add(inode, cluster, 1);
53 fat_free_clusters(inode, cluster);
57 static void check_fallocated_region(struct inode *inode, sector_t iblock,
58 unsigned long *max_blocks, struct buffer_head *bh_result)
60 struct super_block *sb = inode->i_sb;
61 sector_t last_block, disk_block;
62 const unsigned long blocksize = sb->s_blocksize;
63 const unsigned char blocksize_bits = sb->s_blocksize_bits;
65 last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
67 disk_block = (MSDOS_I(inode)->i_disksize + (blocksize - 1))
69 if (iblock >= last_block && iblock <= disk_block) {
70 MSDOS_I(inode)->mmu_private += *max_blocks << blocksize_bits;
71 set_buffer_new(bh_result);
76 static inline int __fat_get_block(struct inode *inode, sector_t iblock,
77 unsigned long *max_blocks,
78 struct buffer_head *bh_result, int create)
80 struct super_block *sb = inode->i_sb;
81 struct msdos_sb_info *sbi = MSDOS_SB(sb);
82 unsigned long mapped_blocks;
86 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
90 *max_blocks = min(mapped_blocks, *max_blocks);
92 check_fallocated_region(inode, iblock, max_blocks,
94 map_bh(bh_result, sb, phys);
100 if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
101 fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
102 MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
106 offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
108 /* TODO: multiple cluster allocation would be desirable. */
109 err = fat_add_cluster(inode);
113 /* available blocks on this cluster */
114 mapped_blocks = sbi->sec_per_clus - offset;
116 *max_blocks = min(mapped_blocks, *max_blocks);
117 MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
118 MSDOS_I(inode)->i_disksize = MSDOS_I(inode)->mmu_private;
120 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create);
125 BUG_ON(*max_blocks != mapped_blocks);
126 set_buffer_new(bh_result);
127 map_bh(bh_result, sb, phys);
132 static int fat_get_block(struct inode *inode, sector_t iblock,
133 struct buffer_head *bh_result, int create)
135 struct super_block *sb = inode->i_sb;
136 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
139 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
142 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
146 static int fat_writepage(struct page *page, struct writeback_control *wbc)
148 return block_write_full_page(page, fat_get_block, wbc);
151 static int fat_writepages(struct address_space *mapping,
152 struct writeback_control *wbc)
154 return mpage_writepages(mapping, wbc, fat_get_block);
157 static int fat_readpage(struct file *file, struct page *page)
159 return mpage_readpage(page, fat_get_block);
162 static int fat_readpages(struct file *file, struct address_space *mapping,
163 struct list_head *pages, unsigned nr_pages)
165 return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
168 static void fat_write_failed(struct address_space *mapping, loff_t to)
170 struct inode *inode = mapping->host;
172 if (to > inode->i_size) {
173 truncate_pagecache(inode, inode->i_size);
174 fat_truncate_blocks(inode, inode->i_size);
178 static int fat_write_begin(struct file *file, struct address_space *mapping,
179 loff_t pos, unsigned len, unsigned flags,
180 struct page **pagep, void **fsdata)
185 err = cont_write_begin(file, mapping, pos, len, flags,
186 pagep, fsdata, fat_get_block,
187 &MSDOS_I(mapping->host)->mmu_private);
189 fat_write_failed(mapping, pos + len);
193 static int fat_write_end(struct file *file, struct address_space *mapping,
194 loff_t pos, unsigned len, unsigned copied,
195 struct page *pagep, void *fsdata)
197 struct inode *inode = mapping->host;
199 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
201 fat_write_failed(mapping, pos + len);
202 if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
203 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
204 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
205 mark_inode_dirty(inode);
210 static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
211 const struct iovec *iov,
212 loff_t offset, unsigned long nr_segs)
214 struct file *file = iocb->ki_filp;
215 struct address_space *mapping = file->f_mapping;
216 struct inode *inode = mapping->host;
221 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
222 * so we need to update the ->mmu_private to block boundary.
224 * But we must fill the remaining area or hole by nul for
225 * updating ->mmu_private.
227 * Return 0, and fallback to normal buffered write.
229 loff_t size = offset + iov_length(iov, nr_segs);
230 if (MSDOS_I(inode)->mmu_private < size)
234 * In case of writing in fallocated region, return 0 and
235 * fallback to buffered write.
237 if (MSDOS_I(inode)->i_disksize > MSDOS_I(inode)->mmu_private)
242 * FAT need to use the DIO_LOCKING for avoiding the race
243 * condition of fat_get_block() and ->truncate().
245 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
247 if (ret < 0 && (rw & WRITE))
248 fat_write_failed(mapping, offset + iov_length(iov, nr_segs));
253 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
257 /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
258 down_read(&MSDOS_I(mapping->host)->truncate_lock);
259 /* To get block number beyond file size in fallocated region */
260 atomic_set(&MSDOS_I(mapping->host)->beyond_isize, 1);
261 blocknr = generic_block_bmap(mapping, block, fat_get_block);
262 atomic_set(&MSDOS_I(mapping->host)->beyond_isize, 0);
263 up_read(&MSDOS_I(mapping->host)->truncate_lock);
268 static const struct address_space_operations fat_aops = {
269 .readpage = fat_readpage,
270 .readpages = fat_readpages,
271 .writepage = fat_writepage,
272 .writepages = fat_writepages,
273 .write_begin = fat_write_begin,
274 .write_end = fat_write_end,
275 .direct_IO = fat_direct_IO,
280 * New FAT inode stuff. We do the following:
281 * a) i_ino is constant and has nothing with on-disk location.
282 * b) FAT manages its own cache of directory entries.
283 * c) *This* cache is indexed by on-disk location.
284 * d) inode has an associated directory entry, all right, but
285 * it may be unhashed.
286 * e) currently entries are stored within struct inode. That should
288 * f) we deal with races in the following way:
289 * 1. readdir() and lookup() do FAT-dir-cache lookup.
290 * 2. rename() unhashes the F-d-c entry and rehashes it in
292 * 3. unlink() and rmdir() unhash F-d-c entry.
293 * 4. fat_write_inode() checks whether the thing is unhashed.
294 * If it is we silently return. If it isn't we do bread(),
295 * check if the location is still valid and retry if it
296 * isn't. Otherwise we do changes.
297 * 5. Spinlock is used to protect hash/unhash/location check/lookup
298 * 6. fat_evict_inode() unhashes the F-d-c entry.
299 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
300 * and consider negative result as cache miss.
303 static void fat_hash_init(struct super_block *sb)
305 struct msdos_sb_info *sbi = MSDOS_SB(sb);
308 spin_lock_init(&sbi->inode_hash_lock);
309 for (i = 0; i < FAT_HASH_SIZE; i++)
310 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
313 static inline unsigned long fat_hash(loff_t i_pos)
315 return hash_32(i_pos, FAT_HASH_BITS);
318 static void dir_hash_init(struct super_block *sb)
320 struct msdos_sb_info *sbi = MSDOS_SB(sb);
323 spin_lock_init(&sbi->dir_hash_lock);
324 for (i = 0; i < FAT_HASH_SIZE; i++)
325 INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
328 void fat_attach(struct inode *inode, loff_t i_pos)
330 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
332 if (inode->i_ino != MSDOS_ROOT_INO) {
333 struct hlist_head *head = sbi->inode_hashtable
336 spin_lock(&sbi->inode_hash_lock);
337 MSDOS_I(inode)->i_pos = i_pos;
338 hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
339 spin_unlock(&sbi->inode_hash_lock);
342 /* If NFS support is enabled, cache the mapping of start cluster
343 * to directory inode. This is used during reconnection of
344 * dentries to the filesystem root.
346 if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
347 struct hlist_head *d_head = sbi->dir_hashtable;
348 d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
350 spin_lock(&sbi->dir_hash_lock);
351 hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
352 spin_unlock(&sbi->dir_hash_lock);
355 EXPORT_SYMBOL_GPL(fat_attach);
357 void fat_detach(struct inode *inode)
359 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
360 spin_lock(&sbi->inode_hash_lock);
361 MSDOS_I(inode)->i_pos = 0;
362 hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
363 spin_unlock(&sbi->inode_hash_lock);
365 if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
366 spin_lock(&sbi->dir_hash_lock);
367 hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
368 spin_unlock(&sbi->dir_hash_lock);
371 EXPORT_SYMBOL_GPL(fat_detach);
373 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
375 struct msdos_sb_info *sbi = MSDOS_SB(sb);
376 struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
377 struct msdos_inode_info *i;
378 struct inode *inode = NULL;
380 spin_lock(&sbi->inode_hash_lock);
381 hlist_for_each_entry(i, head, i_fat_hash) {
382 BUG_ON(i->vfs_inode.i_sb != sb);
383 if (i->i_pos != i_pos)
385 inode = igrab(&i->vfs_inode);
389 spin_unlock(&sbi->inode_hash_lock);
393 static int is_exec(unsigned char *extension)
395 unsigned char *exe_extensions = "EXECOMBAT", *walk;
397 for (walk = exe_extensions; *walk; walk += 3)
398 if (!strncmp(extension, walk, 3))
403 static int fat_calc_dir_size(struct inode *inode)
405 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
406 int ret, fclus, dclus;
409 if (MSDOS_I(inode)->i_start == 0)
412 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
415 inode->i_size = (fclus + 1) << sbi->cluster_bits;
420 /* doesn't deal with root inode */
421 int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
423 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
426 MSDOS_I(inode)->i_pos = 0;
427 inode->i_uid = sbi->options.fs_uid;
428 inode->i_gid = sbi->options.fs_gid;
430 inode->i_generation = get_seconds();
432 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
433 inode->i_generation &= ~1;
434 inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
435 inode->i_op = sbi->dir_ops;
436 inode->i_fop = &fat_dir_operations;
438 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
439 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
440 error = fat_calc_dir_size(inode);
443 MSDOS_I(inode)->mmu_private = inode->i_size;
444 MSDOS_I(inode)->i_disksize = inode->i_size;
446 set_nlink(inode, fat_subdirs(inode));
447 } else { /* not a directory */
448 inode->i_generation |= 1;
449 inode->i_mode = fat_make_mode(sbi, de->attr,
450 ((sbi->options.showexec && !is_exec(de->name + 8))
451 ? S_IRUGO|S_IWUGO : S_IRWXUGO));
452 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
454 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
455 inode->i_size = le32_to_cpu(de->size);
456 inode->i_op = &fat_file_inode_operations;
457 inode->i_fop = &fat_file_operations;
458 inode->i_mapping->a_ops = &fat_aops;
459 MSDOS_I(inode)->mmu_private = inode->i_size;
460 MSDOS_I(inode)->i_disksize = inode->i_size;
462 if (de->attr & ATTR_SYS) {
463 if (sbi->options.sys_immutable)
464 inode->i_flags |= S_IMMUTABLE;
466 fat_save_attrs(inode, de->attr);
468 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
469 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
471 fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
472 if (sbi->options.isvfat) {
473 fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
474 de->cdate, de->ctime_cs);
475 fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
477 inode->i_ctime = inode->i_atime = inode->i_mtime;
482 static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
484 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
485 mutex_lock(&sbi->nfs_build_inode_lock);
488 static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
490 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
491 mutex_unlock(&sbi->nfs_build_inode_lock);
494 struct inode *fat_build_inode(struct super_block *sb,
495 struct msdos_dir_entry *de, loff_t i_pos)
500 fat_lock_build_inode(MSDOS_SB(sb));
501 inode = fat_iget(sb, i_pos);
504 inode = new_inode(sb);
506 inode = ERR_PTR(-ENOMEM);
509 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
510 inode->i_version = 1;
511 err = fat_fill_inode(inode, de);
514 inode = ERR_PTR(err);
517 fat_attach(inode, i_pos);
518 insert_inode_hash(inode);
520 fat_unlock_build_inode(MSDOS_SB(sb));
524 EXPORT_SYMBOL_GPL(fat_build_inode);
526 static void fat_evict_inode(struct inode *inode)
528 truncate_inode_pages(&inode->i_data, 0);
529 if (!inode->i_nlink) {
531 fat_truncate_blocks(inode, 0);
533 /* Release unwritten fallocated blocks on inode eviction. */
534 if (MSDOS_I(inode)->mmu_private < MSDOS_I(inode)->i_disksize) {
536 fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
537 /* Fallocate results in updating the i_start/iogstart
538 * for the zero byte file. So, make it return to
539 * original state during evict and commit it
540 * synchrnously to avoid any corruption on the next
541 * access to the cluster chain for the file.
543 err = fat_sync_inode(inode);
545 fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
546 "update on disk inode for unused fallocated "
547 "blocks, inode could be corrupted. Please run "
552 invalidate_inode_buffers(inode);
554 fat_cache_inval_inode(inode);
558 static void fat_set_state(struct super_block *sb,
559 unsigned int set, unsigned int force)
561 struct buffer_head *bh;
562 struct fat_boot_sector *b;
563 struct msdos_sb_info *sbi = sb->s_fs_info;
565 /* do not change any thing if mounted read only */
566 if ((sb->s_flags & MS_RDONLY) && !force)
569 /* do not change state if fs was dirty */
571 /* warn only on set (mount). */
573 fat_msg(sb, KERN_WARNING, "Volume was not properly "
574 "unmounted. Some data may be corrupt. "
579 bh = sb_bread(sb, 0);
581 fat_msg(sb, KERN_ERR, "unable to read boot sector "
582 "to mark fs as dirty");
586 b = (struct fat_boot_sector *) bh->b_data;
588 if (sbi->fat_bits == 32) {
590 b->fat32.state |= FAT_STATE_DIRTY;
592 b->fat32.state &= ~FAT_STATE_DIRTY;
593 } else /* fat 16 and 12 */ {
595 b->fat16.state |= FAT_STATE_DIRTY;
597 b->fat16.state &= ~FAT_STATE_DIRTY;
600 mark_buffer_dirty(bh);
601 sync_dirty_buffer(bh);
605 static void delayed_free(struct rcu_head *p)
607 struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
608 unload_nls(sbi->nls_disk);
609 unload_nls(sbi->nls_io);
610 if (sbi->options.iocharset != fat_default_iocharset)
611 kfree(sbi->options.iocharset);
615 static void fat_put_super(struct super_block *sb)
617 struct msdos_sb_info *sbi = MSDOS_SB(sb);
619 fat_set_state(sb, 0, 0);
621 iput(sbi->fsinfo_inode);
622 iput(sbi->fat_inode);
624 call_rcu(&sbi->rcu, delayed_free);
627 static struct kmem_cache *fat_inode_cachep;
629 static struct inode *fat_alloc_inode(struct super_block *sb)
631 struct msdos_inode_info *ei;
632 ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
636 init_rwsem(&ei->truncate_lock);
637 return &ei->vfs_inode;
640 static void fat_i_callback(struct rcu_head *head)
642 struct inode *inode = container_of(head, struct inode, i_rcu);
643 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
646 static void fat_destroy_inode(struct inode *inode)
648 call_rcu(&inode->i_rcu, fat_i_callback);
651 static void init_once(void *foo)
653 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
655 spin_lock_init(&ei->cache_lru_lock);
657 ei->cache_valid_id = FAT_CACHE_VALID + 1;
658 INIT_LIST_HEAD(&ei->cache_lru);
659 INIT_HLIST_NODE(&ei->i_fat_hash);
660 INIT_HLIST_NODE(&ei->i_dir_hash);
661 inode_init_once(&ei->vfs_inode);
664 static int __init fat_init_inodecache(void)
666 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
667 sizeof(struct msdos_inode_info),
668 0, (SLAB_RECLAIM_ACCOUNT|
671 if (fat_inode_cachep == NULL)
676 static void __exit fat_destroy_inodecache(void)
679 * Make sure all delayed rcu free inodes are flushed before we
683 kmem_cache_destroy(fat_inode_cachep);
686 static int fat_remount(struct super_block *sb, int *flags, char *data)
689 struct msdos_sb_info *sbi = MSDOS_SB(sb);
690 *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
692 /* make sure we update state on remount. */
693 new_rdonly = *flags & MS_RDONLY;
694 if (new_rdonly != (sb->s_flags & MS_RDONLY)) {
696 fat_set_state(sb, 0, 0);
698 fat_set_state(sb, 1, 1);
703 static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
705 struct super_block *sb = dentry->d_sb;
706 struct msdos_sb_info *sbi = MSDOS_SB(sb);
707 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
709 /* If the count of free cluster is still unknown, counts it here. */
710 if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
711 int err = fat_count_free_clusters(dentry->d_sb);
716 buf->f_type = dentry->d_sb->s_magic;
717 buf->f_bsize = sbi->cluster_size;
718 buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
719 buf->f_bfree = sbi->free_clusters;
720 buf->f_bavail = sbi->free_clusters;
721 buf->f_fsid.val[0] = (u32)id;
722 buf->f_fsid.val[1] = (u32)(id >> 32);
724 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
729 static int __fat_write_inode(struct inode *inode, int wait)
731 struct super_block *sb = inode->i_sb;
732 struct msdos_sb_info *sbi = MSDOS_SB(sb);
733 struct buffer_head *bh;
734 struct msdos_dir_entry *raw_entry;
739 if (inode->i_ino == MSDOS_ROOT_INO)
743 i_pos = fat_i_pos_read(sbi, inode);
747 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
748 bh = sb_bread(sb, blocknr);
750 fat_msg(sb, KERN_ERR, "unable to read inode block "
751 "for updating (i_pos %lld)", i_pos);
754 spin_lock(&sbi->inode_hash_lock);
755 if (i_pos != MSDOS_I(inode)->i_pos) {
756 spin_unlock(&sbi->inode_hash_lock);
761 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
762 if (S_ISDIR(inode->i_mode))
765 raw_entry->size = cpu_to_le32(inode->i_size);
766 raw_entry->attr = fat_make_attrs(inode);
767 fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
768 fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
769 &raw_entry->date, NULL);
770 if (sbi->options.isvfat) {
772 fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
773 &raw_entry->cdate, &raw_entry->ctime_cs);
774 fat_time_unix2fat(sbi, &inode->i_atime, &atime,
775 &raw_entry->adate, NULL);
777 spin_unlock(&sbi->inode_hash_lock);
778 mark_buffer_dirty(bh);
781 err = sync_dirty_buffer(bh);
786 static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
790 if (inode->i_ino == MSDOS_FSINFO_INO) {
791 struct super_block *sb = inode->i_sb;
793 mutex_lock(&MSDOS_SB(sb)->s_lock);
794 err = fat_clusters_flush(sb);
795 mutex_unlock(&MSDOS_SB(sb)->s_lock);
797 err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
802 int fat_sync_inode(struct inode *inode)
804 return __fat_write_inode(inode, 1);
807 EXPORT_SYMBOL_GPL(fat_sync_inode);
809 static int fat_show_options(struct seq_file *m, struct dentry *root);
810 static const struct super_operations fat_sops = {
811 .alloc_inode = fat_alloc_inode,
812 .destroy_inode = fat_destroy_inode,
813 .write_inode = fat_write_inode,
814 .evict_inode = fat_evict_inode,
815 .put_super = fat_put_super,
816 .statfs = fat_statfs,
817 .remount_fs = fat_remount,
819 .show_options = fat_show_options,
822 static int fat_show_options(struct seq_file *m, struct dentry *root)
824 struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
825 struct fat_mount_options *opts = &sbi->options;
826 int isvfat = opts->isvfat;
828 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
829 seq_printf(m, ",uid=%u",
830 from_kuid_munged(&init_user_ns, opts->fs_uid));
831 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
832 seq_printf(m, ",gid=%u",
833 from_kgid_munged(&init_user_ns, opts->fs_gid));
834 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
835 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
836 if (opts->allow_utime)
837 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
839 /* strip "cp" prefix from displayed option */
840 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
843 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
845 switch (opts->shortname) {
846 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
847 seq_puts(m, ",shortname=win95");
849 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
850 seq_puts(m, ",shortname=winnt");
852 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
853 seq_puts(m, ",shortname=mixed");
855 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
856 seq_puts(m, ",shortname=lower");
859 seq_puts(m, ",shortname=unknown");
863 if (opts->name_check != 'n')
864 seq_printf(m, ",check=%c", opts->name_check);
866 seq_puts(m, ",usefree");
868 seq_puts(m, ",quiet");
870 seq_puts(m, ",showexec");
871 if (opts->sys_immutable)
872 seq_puts(m, ",sys_immutable");
875 seq_puts(m, ",dotsOK=yes");
877 seq_puts(m, ",nocase");
880 seq_puts(m, ",utf8");
881 if (opts->unicode_xlate)
882 seq_puts(m, ",uni_xlate");
884 seq_puts(m, ",nonumtail");
886 seq_puts(m, ",rodir");
889 seq_puts(m, ",flush");
891 if (opts->time_offset)
892 seq_printf(m, ",time_offset=%d", opts->time_offset);
894 seq_puts(m, ",tz=UTC");
896 if (opts->errors == FAT_ERRORS_CONT)
897 seq_puts(m, ",errors=continue");
898 else if (opts->errors == FAT_ERRORS_PANIC)
899 seq_puts(m, ",errors=panic");
901 seq_puts(m, ",errors=remount-ro");
902 if (opts->nfs == FAT_NFS_NOSTALE_RO)
903 seq_puts(m, ",nfs=nostale_ro");
905 seq_puts(m, ",nfs=stale_rw");
907 seq_puts(m, ",discard");
913 Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
914 Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
915 Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
916 Opt_immutable, Opt_dots, Opt_nodots,
917 Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
918 Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
919 Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
920 Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
921 Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset,
922 Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err,
925 static const match_table_t fat_tokens = {
926 {Opt_check_r, "check=relaxed"},
927 {Opt_check_s, "check=strict"},
928 {Opt_check_n, "check=normal"},
929 {Opt_check_r, "check=r"},
930 {Opt_check_s, "check=s"},
931 {Opt_check_n, "check=n"},
934 {Opt_umask, "umask=%o"},
935 {Opt_dmask, "dmask=%o"},
936 {Opt_fmask, "fmask=%o"},
937 {Opt_allow_utime, "allow_utime=%o"},
938 {Opt_codepage, "codepage=%u"},
939 {Opt_usefree, "usefree"},
940 {Opt_nocase, "nocase"},
941 {Opt_quiet, "quiet"},
942 {Opt_showexec, "showexec"},
943 {Opt_debug, "debug"},
944 {Opt_immutable, "sys_immutable"},
945 {Opt_flush, "flush"},
946 {Opt_tz_utc, "tz=UTC"},
947 {Opt_time_offset, "time_offset=%d"},
948 {Opt_err_cont, "errors=continue"},
949 {Opt_err_panic, "errors=panic"},
950 {Opt_err_ro, "errors=remount-ro"},
951 {Opt_discard, "discard"},
952 {Opt_nfs_stale_rw, "nfs"},
953 {Opt_nfs_stale_rw, "nfs=stale_rw"},
954 {Opt_nfs_nostale_ro, "nfs=nostale_ro"},
955 {Opt_obsolete, "conv=binary"},
956 {Opt_obsolete, "conv=text"},
957 {Opt_obsolete, "conv=auto"},
958 {Opt_obsolete, "conv=b"},
959 {Opt_obsolete, "conv=t"},
960 {Opt_obsolete, "conv=a"},
961 {Opt_obsolete, "fat=%u"},
962 {Opt_obsolete, "blocksize=%u"},
963 {Opt_obsolete, "cvf_format=%20s"},
964 {Opt_obsolete, "cvf_options=%100s"},
965 {Opt_obsolete, "posix"},
968 static const match_table_t msdos_tokens = {
969 {Opt_nodots, "nodots"},
970 {Opt_nodots, "dotsOK=no"},
972 {Opt_dots, "dotsOK=yes"},
975 static const match_table_t vfat_tokens = {
976 {Opt_charset, "iocharset=%s"},
977 {Opt_shortname_lower, "shortname=lower"},
978 {Opt_shortname_win95, "shortname=win95"},
979 {Opt_shortname_winnt, "shortname=winnt"},
980 {Opt_shortname_mixed, "shortname=mixed"},
981 {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
982 {Opt_utf8_no, "utf8=no"},
983 {Opt_utf8_no, "utf8=false"},
984 {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
985 {Opt_utf8_yes, "utf8=yes"},
986 {Opt_utf8_yes, "utf8=true"},
987 {Opt_utf8_yes, "utf8"},
988 {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
989 {Opt_uni_xl_no, "uni_xlate=no"},
990 {Opt_uni_xl_no, "uni_xlate=false"},
991 {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
992 {Opt_uni_xl_yes, "uni_xlate=yes"},
993 {Opt_uni_xl_yes, "uni_xlate=true"},
994 {Opt_uni_xl_yes, "uni_xlate"},
995 {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
996 {Opt_nonumtail_no, "nonumtail=no"},
997 {Opt_nonumtail_no, "nonumtail=false"},
998 {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
999 {Opt_nonumtail_yes, "nonumtail=yes"},
1000 {Opt_nonumtail_yes, "nonumtail=true"},
1001 {Opt_nonumtail_yes, "nonumtail"},
1002 {Opt_rodir, "rodir"},
1006 static int parse_options(struct super_block *sb, char *options, int is_vfat,
1007 int silent, int *debug, struct fat_mount_options *opts)
1010 substring_t args[MAX_OPT_ARGS];
1014 opts->isvfat = is_vfat;
1016 opts->fs_uid = current_uid();
1017 opts->fs_gid = current_gid();
1018 opts->fs_fmask = opts->fs_dmask = current_umask();
1019 opts->allow_utime = -1;
1020 opts->codepage = fat_default_codepage;
1021 opts->iocharset = fat_default_iocharset;
1023 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
1026 opts->shortname = 0;
1029 opts->name_check = 'n';
1030 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
1031 opts->utf8 = opts->unicode_xlate = 0;
1033 opts->usefree = opts->nocase = 0;
1036 opts->errors = FAT_ERRORS_RO;
1042 while ((p = strsep(&options, ",")) != NULL) {
1047 token = match_token(p, fat_tokens, args);
1048 if (token == Opt_err) {
1050 token = match_token(p, vfat_tokens, args);
1052 token = match_token(p, msdos_tokens, args);
1056 opts->name_check = 's';
1059 opts->name_check = 'r';
1062 opts->name_check = 'n';
1071 /* for backward compatibility */
1072 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1073 | VFAT_SFN_CREATE_WIN95;
1086 opts->sys_immutable = 1;
1089 if (match_int(&args[0], &option))
1091 opts->fs_uid = make_kuid(current_user_ns(), option);
1092 if (!uid_valid(opts->fs_uid))
1096 if (match_int(&args[0], &option))
1098 opts->fs_gid = make_kgid(current_user_ns(), option);
1099 if (!gid_valid(opts->fs_gid))
1103 if (match_octal(&args[0], &option))
1105 opts->fs_fmask = opts->fs_dmask = option;
1108 if (match_octal(&args[0], &option))
1110 opts->fs_dmask = option;
1113 if (match_octal(&args[0], &option))
1115 opts->fs_fmask = option;
1117 case Opt_allow_utime:
1118 if (match_octal(&args[0], &option))
1120 opts->allow_utime = option & (S_IWGRP | S_IWOTH);
1123 if (match_int(&args[0], &option))
1125 opts->codepage = option;
1130 case Opt_time_offset:
1131 if (match_int(&args[0], &option))
1133 if (option < -12 * 60 || option > 12 * 60)
1136 opts->time_offset = option;
1140 opts->time_offset = 0;
1143 opts->errors = FAT_ERRORS_CONT;
1146 opts->errors = FAT_ERRORS_PANIC;
1149 opts->errors = FAT_ERRORS_RO;
1151 case Opt_nfs_stale_rw:
1152 opts->nfs = FAT_NFS_STALE_RW;
1154 case Opt_nfs_nostale_ro:
1155 opts->nfs = FAT_NFS_NOSTALE_RO;
1158 /* msdos specific */
1168 if (opts->iocharset != fat_default_iocharset)
1169 kfree(opts->iocharset);
1170 iocharset = match_strdup(&args[0]);
1173 opts->iocharset = iocharset;
1175 case Opt_shortname_lower:
1176 opts->shortname = VFAT_SFN_DISPLAY_LOWER
1177 | VFAT_SFN_CREATE_WIN95;
1179 case Opt_shortname_win95:
1180 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1181 | VFAT_SFN_CREATE_WIN95;
1183 case Opt_shortname_winnt:
1184 opts->shortname = VFAT_SFN_DISPLAY_WINNT
1185 | VFAT_SFN_CREATE_WINNT;
1187 case Opt_shortname_mixed:
1188 opts->shortname = VFAT_SFN_DISPLAY_WINNT
1189 | VFAT_SFN_CREATE_WIN95;
1191 case Opt_utf8_no: /* 0 or no or false */
1194 case Opt_utf8_yes: /* empty or 1 or yes or true */
1197 case Opt_uni_xl_no: /* 0 or no or false */
1198 opts->unicode_xlate = 0;
1200 case Opt_uni_xl_yes: /* empty or 1 or yes or true */
1201 opts->unicode_xlate = 1;
1203 case Opt_nonumtail_no: /* 0 or no or false */
1204 opts->numtail = 1; /* negated option */
1206 case Opt_nonumtail_yes: /* empty or 1 or yes or true */
1207 opts->numtail = 0; /* negated option */
1216 /* obsolete mount options */
1218 fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
1219 "not supported now", p);
1221 /* unknown option */
1224 fat_msg(sb, KERN_ERR,
1225 "Unrecognized mount option \"%s\" "
1226 "or missing value", p);
1233 /* UTF-8 doesn't provide FAT semantics */
1234 if (!strcmp(opts->iocharset, "utf8")) {
1235 fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
1236 " for FAT filesystems, filesystem will be "
1240 /* If user doesn't specify allow_utime, it's initialized from dmask. */
1241 if (opts->allow_utime == (unsigned short)-1)
1242 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
1243 if (opts->unicode_xlate)
1245 if (opts->nfs == FAT_NFS_NOSTALE_RO) {
1246 sb->s_flags |= MS_RDONLY;
1247 sb->s_export_op = &fat_export_ops_nostale;
1253 static int fat_read_root(struct inode *inode)
1255 struct super_block *sb = inode->i_sb;
1256 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1259 MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
1260 inode->i_uid = sbi->options.fs_uid;
1261 inode->i_gid = sbi->options.fs_gid;
1263 inode->i_generation = 0;
1264 inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
1265 inode->i_op = sbi->dir_ops;
1266 inode->i_fop = &fat_dir_operations;
1267 if (sbi->fat_bits == 32) {
1268 MSDOS_I(inode)->i_start = sbi->root_cluster;
1269 error = fat_calc_dir_size(inode);
1273 MSDOS_I(inode)->i_start = 0;
1274 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
1276 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1277 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1278 MSDOS_I(inode)->i_logstart = 0;
1279 MSDOS_I(inode)->mmu_private = inode->i_size;
1280 MSDOS_I(inode)->i_disksize = inode->i_size;
1282 fat_save_attrs(inode, ATTR_DIR);
1283 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
1284 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1285 set_nlink(inode, fat_subdirs(inode)+2);
1290 static unsigned long calc_fat_clusters(struct super_block *sb)
1292 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1294 /* Divide first to avoid overflow */
1295 if (sbi->fat_bits != 12) {
1296 unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
1297 return ent_per_sec * sbi->fat_length;
1300 return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1304 * Read the super block of an MS-DOS FS.
1306 int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
1307 void (*setup)(struct super_block *))
1309 struct inode *root_inode = NULL, *fat_inode = NULL;
1310 struct inode *fsinfo_inode = NULL;
1311 struct buffer_head *bh;
1312 struct fat_boot_sector *b;
1313 struct msdos_sb_info *sbi;
1314 u16 logical_sector_size;
1315 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1322 * GFP_KERNEL is ok here, because while we do hold the
1323 * supeblock lock, memory pressure can't call back into
1324 * the filesystem, since we're only just about to mount
1325 * it and have no inodes etc active!
1327 sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
1330 sb->s_fs_info = sbi;
1332 sb->s_flags |= MS_NODIRATIME;
1333 sb->s_magic = MSDOS_SUPER_MAGIC;
1334 sb->s_op = &fat_sops;
1335 sb->s_export_op = &fat_export_ops;
1336 mutex_init(&sbi->nfs_build_inode_lock);
1337 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1338 DEFAULT_RATELIMIT_BURST);
1340 error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
1344 setup(sb); /* flavour-specific stuff that needs options */
1347 sb_min_blocksize(sb, 512);
1348 bh = sb_bread(sb, 0);
1350 fat_msg(sb, KERN_ERR, "unable to read boot sector");
1354 b = (struct fat_boot_sector *) bh->b_data;
1357 fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
1363 fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1369 * Earlier we checked here that b->secs_track and b->head are nonzero,
1370 * but it turns out valid FAT filesystems can have zero there.
1374 if (!fat_valid_media(media)) {
1376 fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
1381 logical_sector_size = get_unaligned_le16(&b->sector_size);
1382 if (!is_power_of_2(logical_sector_size)
1383 || (logical_sector_size < 512)
1384 || (logical_sector_size > 4096)) {
1386 fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
1387 logical_sector_size);
1391 sbi->sec_per_clus = b->sec_per_clus;
1392 if (!is_power_of_2(sbi->sec_per_clus)) {
1394 fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
1400 if (logical_sector_size < sb->s_blocksize) {
1401 fat_msg(sb, KERN_ERR, "logical sector size too small for device"
1402 " (logical sector size = %u)", logical_sector_size);
1406 if (logical_sector_size > sb->s_blocksize) {
1409 if (!sb_set_blocksize(sb, logical_sector_size)) {
1410 fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
1411 logical_sector_size);
1414 bh = sb_bread(sb, 0);
1416 fat_msg(sb, KERN_ERR, "unable to read boot sector"
1417 " (logical sector size = %lu)",
1421 b = (struct fat_boot_sector *) bh->b_data;
1424 mutex_init(&sbi->s_lock);
1425 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
1426 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1427 sbi->fats = b->fats;
1428 sbi->fat_bits = 0; /* Don't know yet */
1429 sbi->fat_start = le16_to_cpu(b->reserved);
1430 sbi->fat_length = le16_to_cpu(b->fat_length);
1431 sbi->root_cluster = 0;
1432 sbi->free_clusters = -1; /* Don't know yet */
1433 sbi->free_clus_valid = 0;
1434 sbi->prev_free = FAT_START_ENT;
1435 sb->s_maxbytes = 0xffffffff;
1437 if (!sbi->fat_length && b->fat32.length) {
1438 struct fat_boot_fsinfo *fsinfo;
1439 struct buffer_head *fsinfo_bh;
1443 sbi->fat_length = le32_to_cpu(b->fat32.length);
1444 sbi->root_cluster = le32_to_cpu(b->fat32.root_cluster);
1446 /* MC - if info_sector is 0, don't multiply by 0 */
1447 sbi->fsinfo_sector = le16_to_cpu(b->fat32.info_sector);
1448 if (sbi->fsinfo_sector == 0)
1449 sbi->fsinfo_sector = 1;
1451 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
1452 if (fsinfo_bh == NULL) {
1453 fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
1454 " (sector = %lu)", sbi->fsinfo_sector);
1459 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1460 if (!IS_FSINFO(fsinfo)) {
1461 fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
1462 "0x%08x, 0x%08x (sector = %lu)",
1463 le32_to_cpu(fsinfo->signature1),
1464 le32_to_cpu(fsinfo->signature2),
1465 sbi->fsinfo_sector);
1467 if (sbi->options.usefree)
1468 sbi->free_clus_valid = 1;
1469 sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
1470 sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
1476 /* interpret volume ID as a little endian 32 bit integer */
1477 if (sbi->fat_bits == 32)
1478 sbi->vol_id = (((u32)b->fat32.vol_id[0]) |
1479 ((u32)b->fat32.vol_id[1] << 8) |
1480 ((u32)b->fat32.vol_id[2] << 16) |
1481 ((u32)b->fat32.vol_id[3] << 24));
1482 else /* fat 16 or 12 */
1483 sbi->vol_id = (((u32)b->fat16.vol_id[0]) |
1484 ((u32)b->fat16.vol_id[1] << 8) |
1485 ((u32)b->fat16.vol_id[2] << 16) |
1486 ((u32)b->fat16.vol_id[3] << 24));
1488 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1489 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1491 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1492 sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
1493 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1495 fat_msg(sb, KERN_ERR, "bogus directory-entries per block"
1496 " (%u)", sbi->dir_entries);
1501 rootdir_sectors = sbi->dir_entries
1502 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
1503 sbi->data_start = sbi->dir_start + rootdir_sectors;
1504 total_sectors = get_unaligned_le16(&b->sectors);
1505 if (total_sectors == 0)
1506 total_sectors = le32_to_cpu(b->total_sect);
1508 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
1510 if (sbi->fat_bits != 32)
1511 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
1513 /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1514 if (sbi->fat_bits == 32)
1515 sbi->dirty = b->fat32.state & FAT_STATE_DIRTY;
1516 else /* fat 16 or 12 */
1517 sbi->dirty = b->fat16.state & FAT_STATE_DIRTY;
1519 /* check that FAT table does not overflow */
1520 fat_clusters = calc_fat_clusters(sb);
1521 total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1522 if (total_clusters > MAX_FAT(sb)) {
1524 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
1530 sbi->max_cluster = total_clusters + FAT_START_ENT;
1531 /* check the free_clusters, it's not necessarily correct */
1532 if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
1533 sbi->free_clusters = -1;
1534 /* check the prev_free, it's not necessarily correct */
1535 sbi->prev_free %= sbi->max_cluster;
1536 if (sbi->prev_free < FAT_START_ENT)
1537 sbi->prev_free = FAT_START_ENT;
1541 /* set up enough so that it can read an inode */
1544 fat_ent_access_init(sb);
1547 * The low byte of FAT's first entry must have same value with
1548 * media-field. But in real world, too many devices is
1549 * writing wrong value. So, removed that validity check.
1551 * if (FAT_FIRST_ENT(sb, media) != first)
1555 sprintf(buf, "cp%d", sbi->options.codepage);
1556 sbi->nls_disk = load_nls(buf);
1557 if (!sbi->nls_disk) {
1558 fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
1562 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1563 if (sbi->options.isvfat) {
1564 sbi->nls_io = load_nls(sbi->options.iocharset);
1566 fat_msg(sb, KERN_ERR, "IO charset %s not found",
1567 sbi->options.iocharset);
1573 fat_inode = new_inode(sb);
1576 MSDOS_I(fat_inode)->i_pos = 0;
1577 sbi->fat_inode = fat_inode;
1579 fsinfo_inode = new_inode(sb);
1582 fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
1583 sbi->fsinfo_inode = fsinfo_inode;
1584 insert_inode_hash(fsinfo_inode);
1586 root_inode = new_inode(sb);
1589 root_inode->i_ino = MSDOS_ROOT_INO;
1590 root_inode->i_version = 1;
1591 error = fat_read_root(root_inode);
1597 insert_inode_hash(root_inode);
1598 fat_attach(root_inode, 0);
1599 sb->s_root = d_make_root(root_inode);
1601 fat_msg(sb, KERN_ERR, "get root inode failed");
1605 if (sbi->options.discard) {
1606 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1607 if (!blk_queue_discard(q))
1608 fat_msg(sb, KERN_WARNING,
1609 "mounting with \"discard\" option, but "
1610 "the device does not support discard");
1613 fat_set_state(sb, 1, 0);
1619 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
1626 unload_nls(sbi->nls_io);
1627 unload_nls(sbi->nls_disk);
1628 if (sbi->options.iocharset != fat_default_iocharset)
1629 kfree(sbi->options.iocharset);
1630 sb->s_fs_info = NULL;
1635 EXPORT_SYMBOL_GPL(fat_fill_super);
1638 * helper function for fat_flush_inodes. This writes both the inode
1639 * and the file data blocks, waiting for in flight data blocks before
1640 * the start of the call. It does not wait for any io started
1643 static int writeback_inode(struct inode *inode)
1648 /* if we used wait=1, sync_inode_metadata waits for the io for the
1649 * inode to finish. So wait=0 is sent down to sync_inode_metadata
1650 * and filemap_fdatawrite is used for the data blocks
1652 ret = sync_inode_metadata(inode, 0);
1654 ret = filemap_fdatawrite(inode->i_mapping);
1659 * write data and metadata corresponding to i1 and i2. The io is
1660 * started but we do not wait for any of it to finish.
1662 * filemap_flush is used for the block device, so if there is a dirty
1663 * page for a block already in flight, we will not wait and start the
1666 int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1669 if (!MSDOS_SB(sb)->options.flush)
1672 ret = writeback_inode(i1);
1674 ret = writeback_inode(i2);
1676 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1677 ret = filemap_flush(mapping);
1681 EXPORT_SYMBOL_GPL(fat_flush_inodes);
1683 static int __init init_fat_fs(void)
1687 err = fat_cache_init();
1691 err = fat_init_inodecache();
1698 fat_cache_destroy();
1702 static void __exit exit_fat_fs(void)
1704 fat_cache_destroy();
1705 fat_destroy_inodecache();
1708 module_init(init_fat_fs)
1709 module_exit(exit_fat_fs)
1711 MODULE_LICENSE("GPL");