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/vfs.h>
23 #include <linux/parser.h>
24 #include <linux/uio.h>
25 #include <linux/writeback.h>
26 #include <linux/log2.h>
27 #include <linux/hash.h>
28 #include <linux/blkdev.h>
29 #include <asm/unaligned.h>
32 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
33 /* if user don't select VFAT, this is undefined. */
34 #define CONFIG_FAT_DEFAULT_IOCHARSET ""
37 static int fat_default_codepage
= CONFIG_FAT_DEFAULT_CODEPAGE
;
38 static char fat_default_iocharset
[] = CONFIG_FAT_DEFAULT_IOCHARSET
;
41 static int fat_add_cluster(struct inode
*inode
)
45 err
= fat_alloc_clusters(inode
, &cluster
, 1);
48 /* FIXME: this cluster should be added after data of this
49 * cluster is writed */
50 err
= fat_chain_add(inode
, cluster
, 1);
52 fat_free_clusters(inode
, cluster
);
56 static inline int __fat_get_block(struct inode
*inode
, sector_t iblock
,
57 unsigned long *max_blocks
,
58 struct buffer_head
*bh_result
, int create
)
60 struct super_block
*sb
= inode
->i_sb
;
61 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
62 unsigned long mapped_blocks
;
66 err
= fat_bmap(inode
, iblock
, &phys
, &mapped_blocks
, create
);
70 map_bh(bh_result
, sb
, phys
);
71 *max_blocks
= min(mapped_blocks
, *max_blocks
);
77 if (iblock
!= MSDOS_I(inode
)->mmu_private
>> sb
->s_blocksize_bits
) {
78 fat_fs_error(sb
, "corrupted file size (i_pos %lld, %lld)",
79 MSDOS_I(inode
)->i_pos
, MSDOS_I(inode
)->mmu_private
);
83 offset
= (unsigned long)iblock
& (sbi
->sec_per_clus
- 1);
85 /* TODO: multiple cluster allocation would be desirable. */
86 err
= fat_add_cluster(inode
);
90 /* available blocks on this cluster */
91 mapped_blocks
= sbi
->sec_per_clus
- offset
;
93 *max_blocks
= min(mapped_blocks
, *max_blocks
);
94 MSDOS_I(inode
)->mmu_private
+= *max_blocks
<< sb
->s_blocksize_bits
;
96 err
= fat_bmap(inode
, iblock
, &phys
, &mapped_blocks
, create
);
101 BUG_ON(*max_blocks
!= mapped_blocks
);
102 set_buffer_new(bh_result
);
103 map_bh(bh_result
, sb
, phys
);
108 static int fat_get_block(struct inode
*inode
, sector_t iblock
,
109 struct buffer_head
*bh_result
, int create
)
111 struct super_block
*sb
= inode
->i_sb
;
112 unsigned long max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
115 err
= __fat_get_block(inode
, iblock
, &max_blocks
, bh_result
, create
);
118 bh_result
->b_size
= max_blocks
<< sb
->s_blocksize_bits
;
122 static int fat_writepage(struct page
*page
, struct writeback_control
*wbc
)
124 return block_write_full_page(page
, fat_get_block
, wbc
);
127 static int fat_writepages(struct address_space
*mapping
,
128 struct writeback_control
*wbc
)
130 return mpage_writepages(mapping
, wbc
, fat_get_block
);
133 static int fat_readpage(struct file
*file
, struct page
*page
)
135 return mpage_readpage(page
, fat_get_block
);
138 static int fat_readpages(struct file
*file
, struct address_space
*mapping
,
139 struct list_head
*pages
, unsigned nr_pages
)
141 return mpage_readpages(mapping
, pages
, nr_pages
, fat_get_block
);
144 static void fat_write_failed(struct address_space
*mapping
, loff_t to
)
146 struct inode
*inode
= mapping
->host
;
148 if (to
> inode
->i_size
) {
149 truncate_pagecache(inode
, to
, inode
->i_size
);
150 fat_truncate_blocks(inode
, inode
->i_size
);
154 static int fat_write_begin(struct file
*file
, struct address_space
*mapping
,
155 loff_t pos
, unsigned len
, unsigned flags
,
156 struct page
**pagep
, void **fsdata
)
161 err
= cont_write_begin(file
, mapping
, pos
, len
, flags
,
162 pagep
, fsdata
, fat_get_block
,
163 &MSDOS_I(mapping
->host
)->mmu_private
);
165 fat_write_failed(mapping
, pos
+ len
);
169 static int fat_write_end(struct file
*file
, struct address_space
*mapping
,
170 loff_t pos
, unsigned len
, unsigned copied
,
171 struct page
*pagep
, void *fsdata
)
173 struct inode
*inode
= mapping
->host
;
175 err
= generic_write_end(file
, mapping
, pos
, len
, copied
, pagep
, fsdata
);
177 fat_write_failed(mapping
, pos
+ len
);
178 if (!(err
< 0) && !(MSDOS_I(inode
)->i_attrs
& ATTR_ARCH
)) {
179 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
180 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
181 mark_inode_dirty(inode
);
186 static ssize_t
fat_direct_IO(int rw
, struct kiocb
*iocb
,
187 const struct iovec
*iov
,
188 loff_t offset
, unsigned long nr_segs
)
190 struct file
*file
= iocb
->ki_filp
;
191 struct address_space
*mapping
= file
->f_mapping
;
192 struct inode
*inode
= mapping
->host
;
197 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
198 * so we need to update the ->mmu_private to block boundary.
200 * But we must fill the remaining area or hole by nul for
201 * updating ->mmu_private.
203 * Return 0, and fallback to normal buffered write.
205 loff_t size
= offset
+ iov_length(iov
, nr_segs
);
206 if (MSDOS_I(inode
)->mmu_private
< size
)
211 * FAT need to use the DIO_LOCKING for avoiding the race
212 * condition of fat_get_block() and ->truncate().
214 ret
= blockdev_direct_IO(rw
, iocb
, inode
, iov
, offset
, nr_segs
,
216 if (ret
< 0 && (rw
& WRITE
))
217 fat_write_failed(mapping
, offset
+ iov_length(iov
, nr_segs
));
222 static sector_t
_fat_bmap(struct address_space
*mapping
, sector_t block
)
226 /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
227 down_read(&MSDOS_I(mapping
->host
)->truncate_lock
);
228 blocknr
= generic_block_bmap(mapping
, block
, fat_get_block
);
229 up_read(&MSDOS_I(mapping
->host
)->truncate_lock
);
234 static const struct address_space_operations fat_aops
= {
235 .readpage
= fat_readpage
,
236 .readpages
= fat_readpages
,
237 .writepage
= fat_writepage
,
238 .writepages
= fat_writepages
,
239 .write_begin
= fat_write_begin
,
240 .write_end
= fat_write_end
,
241 .direct_IO
= fat_direct_IO
,
246 * New FAT inode stuff. We do the following:
247 * a) i_ino is constant and has nothing with on-disk location.
248 * b) FAT manages its own cache of directory entries.
249 * c) *This* cache is indexed by on-disk location.
250 * d) inode has an associated directory entry, all right, but
251 * it may be unhashed.
252 * e) currently entries are stored within struct inode. That should
254 * f) we deal with races in the following way:
255 * 1. readdir() and lookup() do FAT-dir-cache lookup.
256 * 2. rename() unhashes the F-d-c entry and rehashes it in
258 * 3. unlink() and rmdir() unhash F-d-c entry.
259 * 4. fat_write_inode() checks whether the thing is unhashed.
260 * If it is we silently return. If it isn't we do bread(),
261 * check if the location is still valid and retry if it
262 * isn't. Otherwise we do changes.
263 * 5. Spinlock is used to protect hash/unhash/location check/lookup
264 * 6. fat_evict_inode() unhashes the F-d-c entry.
265 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
266 * and consider negative result as cache miss.
269 static void fat_hash_init(struct super_block
*sb
)
271 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
274 spin_lock_init(&sbi
->inode_hash_lock
);
275 for (i
= 0; i
< FAT_HASH_SIZE
; i
++)
276 INIT_HLIST_HEAD(&sbi
->inode_hashtable
[i
]);
279 static inline unsigned long fat_hash(loff_t i_pos
)
281 return hash_32(i_pos
, FAT_HASH_BITS
);
284 static void dir_hash_init(struct super_block
*sb
)
286 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
289 spin_lock_init(&sbi
->dir_hash_lock
);
290 for (i
= 0; i
< FAT_HASH_SIZE
; i
++)
291 INIT_HLIST_HEAD(&sbi
->dir_hashtable
[i
]);
294 void fat_attach(struct inode
*inode
, loff_t i_pos
)
296 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
298 if (inode
->i_ino
!= MSDOS_ROOT_INO
) {
299 struct hlist_head
*head
= sbi
->inode_hashtable
302 spin_lock(&sbi
->inode_hash_lock
);
303 MSDOS_I(inode
)->i_pos
= i_pos
;
304 hlist_add_head(&MSDOS_I(inode
)->i_fat_hash
, head
);
305 spin_unlock(&sbi
->inode_hash_lock
);
308 /* If NFS support is enabled, cache the mapping of start cluster
309 * to directory inode. This is used during reconnection of
310 * dentries to the filesystem root.
312 if (S_ISDIR(inode
->i_mode
) && sbi
->options
.nfs
) {
313 struct hlist_head
*d_head
= sbi
->dir_hashtable
;
314 d_head
+= fat_dir_hash(MSDOS_I(inode
)->i_logstart
);
316 spin_lock(&sbi
->dir_hash_lock
);
317 hlist_add_head(&MSDOS_I(inode
)->i_dir_hash
, d_head
);
318 spin_unlock(&sbi
->dir_hash_lock
);
321 EXPORT_SYMBOL_GPL(fat_attach
);
323 void fat_detach(struct inode
*inode
)
325 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
326 spin_lock(&sbi
->inode_hash_lock
);
327 MSDOS_I(inode
)->i_pos
= 0;
328 hlist_del_init(&MSDOS_I(inode
)->i_fat_hash
);
329 spin_unlock(&sbi
->inode_hash_lock
);
331 if (S_ISDIR(inode
->i_mode
) && sbi
->options
.nfs
) {
332 spin_lock(&sbi
->dir_hash_lock
);
333 hlist_del_init(&MSDOS_I(inode
)->i_dir_hash
);
334 spin_unlock(&sbi
->dir_hash_lock
);
337 EXPORT_SYMBOL_GPL(fat_detach
);
339 struct inode
*fat_iget(struct super_block
*sb
, loff_t i_pos
)
341 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
342 struct hlist_head
*head
= sbi
->inode_hashtable
+ fat_hash(i_pos
);
343 struct msdos_inode_info
*i
;
344 struct inode
*inode
= NULL
;
346 spin_lock(&sbi
->inode_hash_lock
);
347 hlist_for_each_entry(i
, head
, i_fat_hash
) {
348 BUG_ON(i
->vfs_inode
.i_sb
!= sb
);
349 if (i
->i_pos
!= i_pos
)
351 inode
= igrab(&i
->vfs_inode
);
355 spin_unlock(&sbi
->inode_hash_lock
);
359 static int is_exec(unsigned char *extension
)
361 unsigned char *exe_extensions
= "EXECOMBAT", *walk
;
363 for (walk
= exe_extensions
; *walk
; walk
+= 3)
364 if (!strncmp(extension
, walk
, 3))
369 static int fat_calc_dir_size(struct inode
*inode
)
371 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
372 int ret
, fclus
, dclus
;
375 if (MSDOS_I(inode
)->i_start
== 0)
378 ret
= fat_get_cluster(inode
, FAT_ENT_EOF
, &fclus
, &dclus
);
381 inode
->i_size
= (fclus
+ 1) << sbi
->cluster_bits
;
386 /* doesn't deal with root inode */
387 int fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
)
389 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
392 MSDOS_I(inode
)->i_pos
= 0;
393 inode
->i_uid
= sbi
->options
.fs_uid
;
394 inode
->i_gid
= sbi
->options
.fs_gid
;
396 inode
->i_generation
= get_seconds();
398 if ((de
->attr
& ATTR_DIR
) && !IS_FREE(de
->name
)) {
399 inode
->i_generation
&= ~1;
400 inode
->i_mode
= fat_make_mode(sbi
, de
->attr
, S_IRWXUGO
);
401 inode
->i_op
= sbi
->dir_ops
;
402 inode
->i_fop
= &fat_dir_operations
;
404 MSDOS_I(inode
)->i_start
= fat_get_start(sbi
, de
);
405 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
406 error
= fat_calc_dir_size(inode
);
409 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
411 set_nlink(inode
, fat_subdirs(inode
));
412 } else { /* not a directory */
413 inode
->i_generation
|= 1;
414 inode
->i_mode
= fat_make_mode(sbi
, de
->attr
,
415 ((sbi
->options
.showexec
&& !is_exec(de
->name
+ 8))
416 ? S_IRUGO
|S_IWUGO
: S_IRWXUGO
));
417 MSDOS_I(inode
)->i_start
= fat_get_start(sbi
, de
);
419 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
420 inode
->i_size
= le32_to_cpu(de
->size
);
421 inode
->i_op
= &fat_file_inode_operations
;
422 inode
->i_fop
= &fat_file_operations
;
423 inode
->i_mapping
->a_ops
= &fat_aops
;
424 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
426 if (de
->attr
& ATTR_SYS
) {
427 if (sbi
->options
.sys_immutable
)
428 inode
->i_flags
|= S_IMMUTABLE
;
430 fat_save_attrs(inode
, de
->attr
);
432 inode
->i_blocks
= ((inode
->i_size
+ (sbi
->cluster_size
- 1))
433 & ~((loff_t
)sbi
->cluster_size
- 1)) >> 9;
435 fat_time_fat2unix(sbi
, &inode
->i_mtime
, de
->time
, de
->date
, 0);
436 if (sbi
->options
.isvfat
) {
437 fat_time_fat2unix(sbi
, &inode
->i_ctime
, de
->ctime
,
438 de
->cdate
, de
->ctime_cs
);
439 fat_time_fat2unix(sbi
, &inode
->i_atime
, 0, de
->adate
, 0);
441 inode
->i_ctime
= inode
->i_atime
= inode
->i_mtime
;
446 static inline void fat_lock_build_inode(struct msdos_sb_info
*sbi
)
448 if (sbi
->options
.nfs
== FAT_NFS_NOSTALE_RO
)
449 mutex_lock(&sbi
->nfs_build_inode_lock
);
452 static inline void fat_unlock_build_inode(struct msdos_sb_info
*sbi
)
454 if (sbi
->options
.nfs
== FAT_NFS_NOSTALE_RO
)
455 mutex_unlock(&sbi
->nfs_build_inode_lock
);
458 struct inode
*fat_build_inode(struct super_block
*sb
,
459 struct msdos_dir_entry
*de
, loff_t i_pos
)
464 fat_lock_build_inode(MSDOS_SB(sb
));
465 inode
= fat_iget(sb
, i_pos
);
468 inode
= new_inode(sb
);
470 inode
= ERR_PTR(-ENOMEM
);
473 inode
->i_ino
= iunique(sb
, MSDOS_ROOT_INO
);
474 inode
->i_version
= 1;
475 err
= fat_fill_inode(inode
, de
);
478 inode
= ERR_PTR(err
);
481 fat_attach(inode
, i_pos
);
482 insert_inode_hash(inode
);
484 fat_unlock_build_inode(MSDOS_SB(sb
));
488 EXPORT_SYMBOL_GPL(fat_build_inode
);
490 static void fat_evict_inode(struct inode
*inode
)
492 truncate_inode_pages(&inode
->i_data
, 0);
493 if (!inode
->i_nlink
) {
495 fat_truncate_blocks(inode
, 0);
497 invalidate_inode_buffers(inode
);
499 fat_cache_inval_inode(inode
);
503 static void fat_set_state(struct super_block
*sb
,
504 unsigned int set
, unsigned int force
)
506 struct buffer_head
*bh
;
507 struct fat_boot_sector
*b
;
508 struct msdos_sb_info
*sbi
= sb
->s_fs_info
;
510 /* do not change any thing if mounted read only */
511 if ((sb
->s_flags
& MS_RDONLY
) && !force
)
514 /* do not change state if fs was dirty */
516 /* warn only on set (mount). */
518 fat_msg(sb
, KERN_WARNING
, "Volume was not properly "
519 "unmounted. Some data may be corrupt. "
524 bh
= sb_bread(sb
, 0);
526 fat_msg(sb
, KERN_ERR
, "unable to read boot sector "
527 "to mark fs as dirty");
531 b
= (struct fat_boot_sector
*) bh
->b_data
;
533 if (sbi
->fat_bits
== 32) {
535 b
->fat32
.state
|= FAT_STATE_DIRTY
;
537 b
->fat32
.state
&= ~FAT_STATE_DIRTY
;
538 } else /* fat 16 and 12 */ {
540 b
->fat16
.state
|= FAT_STATE_DIRTY
;
542 b
->fat16
.state
&= ~FAT_STATE_DIRTY
;
545 mark_buffer_dirty(bh
);
546 sync_dirty_buffer(bh
);
550 static void fat_put_super(struct super_block
*sb
)
552 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
554 fat_set_state(sb
, 0, 0);
556 iput(sbi
->fsinfo_inode
);
557 iput(sbi
->fat_inode
);
559 unload_nls(sbi
->nls_disk
);
560 unload_nls(sbi
->nls_io
);
562 if (sbi
->options
.iocharset
!= fat_default_iocharset
)
563 kfree(sbi
->options
.iocharset
);
565 sb
->s_fs_info
= NULL
;
569 static struct kmem_cache
*fat_inode_cachep
;
571 static struct inode
*fat_alloc_inode(struct super_block
*sb
)
573 struct msdos_inode_info
*ei
;
574 ei
= kmem_cache_alloc(fat_inode_cachep
, GFP_NOFS
);
578 init_rwsem(&ei
->truncate_lock
);
579 return &ei
->vfs_inode
;
582 static void fat_i_callback(struct rcu_head
*head
)
584 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
585 kmem_cache_free(fat_inode_cachep
, MSDOS_I(inode
));
588 static void fat_destroy_inode(struct inode
*inode
)
590 call_rcu(&inode
->i_rcu
, fat_i_callback
);
593 static void init_once(void *foo
)
595 struct msdos_inode_info
*ei
= (struct msdos_inode_info
*)foo
;
597 spin_lock_init(&ei
->cache_lru_lock
);
599 ei
->cache_valid_id
= FAT_CACHE_VALID
+ 1;
600 INIT_LIST_HEAD(&ei
->cache_lru
);
601 INIT_HLIST_NODE(&ei
->i_fat_hash
);
602 INIT_HLIST_NODE(&ei
->i_dir_hash
);
603 inode_init_once(&ei
->vfs_inode
);
606 static int __init
fat_init_inodecache(void)
608 fat_inode_cachep
= kmem_cache_create("fat_inode_cache",
609 sizeof(struct msdos_inode_info
),
610 0, (SLAB_RECLAIM_ACCOUNT
|
613 if (fat_inode_cachep
== NULL
)
618 static void __exit
fat_destroy_inodecache(void)
621 * Make sure all delayed rcu free inodes are flushed before we
625 kmem_cache_destroy(fat_inode_cachep
);
628 static int fat_remount(struct super_block
*sb
, int *flags
, char *data
)
631 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
632 *flags
|= MS_NODIRATIME
| (sbi
->options
.isvfat
? 0 : MS_NOATIME
);
634 /* make sure we update state on remount. */
635 new_rdonly
= *flags
& MS_RDONLY
;
636 if (new_rdonly
!= (sb
->s_flags
& MS_RDONLY
)) {
638 fat_set_state(sb
, 0, 0);
640 fat_set_state(sb
, 1, 1);
645 static int fat_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
647 struct super_block
*sb
= dentry
->d_sb
;
648 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
649 u64 id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
651 /* If the count of free cluster is still unknown, counts it here. */
652 if (sbi
->free_clusters
== -1 || !sbi
->free_clus_valid
) {
653 int err
= fat_count_free_clusters(dentry
->d_sb
);
658 buf
->f_type
= dentry
->d_sb
->s_magic
;
659 buf
->f_bsize
= sbi
->cluster_size
;
660 buf
->f_blocks
= sbi
->max_cluster
- FAT_START_ENT
;
661 buf
->f_bfree
= sbi
->free_clusters
;
662 buf
->f_bavail
= sbi
->free_clusters
;
663 buf
->f_fsid
.val
[0] = (u32
)id
;
664 buf
->f_fsid
.val
[1] = (u32
)(id
>> 32);
666 (sbi
->options
.isvfat
? FAT_LFN_LEN
: 12) * NLS_MAX_CHARSET_SIZE
;
671 static int __fat_write_inode(struct inode
*inode
, int wait
)
673 struct super_block
*sb
= inode
->i_sb
;
674 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
675 struct buffer_head
*bh
;
676 struct msdos_dir_entry
*raw_entry
;
681 if (inode
->i_ino
== MSDOS_ROOT_INO
)
685 i_pos
= fat_i_pos_read(sbi
, inode
);
689 fat_get_blknr_offset(sbi
, i_pos
, &blocknr
, &offset
);
690 bh
= sb_bread(sb
, blocknr
);
692 fat_msg(sb
, KERN_ERR
, "unable to read inode block "
693 "for updating (i_pos %lld)", i_pos
);
696 spin_lock(&sbi
->inode_hash_lock
);
697 if (i_pos
!= MSDOS_I(inode
)->i_pos
) {
698 spin_unlock(&sbi
->inode_hash_lock
);
703 raw_entry
= &((struct msdos_dir_entry
*) (bh
->b_data
))[offset
];
704 if (S_ISDIR(inode
->i_mode
))
707 raw_entry
->size
= cpu_to_le32(inode
->i_size
);
708 raw_entry
->attr
= fat_make_attrs(inode
);
709 fat_set_start(raw_entry
, MSDOS_I(inode
)->i_logstart
);
710 fat_time_unix2fat(sbi
, &inode
->i_mtime
, &raw_entry
->time
,
711 &raw_entry
->date
, NULL
);
712 if (sbi
->options
.isvfat
) {
714 fat_time_unix2fat(sbi
, &inode
->i_ctime
, &raw_entry
->ctime
,
715 &raw_entry
->cdate
, &raw_entry
->ctime_cs
);
716 fat_time_unix2fat(sbi
, &inode
->i_atime
, &atime
,
717 &raw_entry
->adate
, NULL
);
719 spin_unlock(&sbi
->inode_hash_lock
);
720 mark_buffer_dirty(bh
);
723 err
= sync_dirty_buffer(bh
);
728 static int fat_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
732 if (inode
->i_ino
== MSDOS_FSINFO_INO
) {
733 struct super_block
*sb
= inode
->i_sb
;
735 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
736 err
= fat_clusters_flush(sb
);
737 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
739 err
= __fat_write_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
744 int fat_sync_inode(struct inode
*inode
)
746 return __fat_write_inode(inode
, 1);
749 EXPORT_SYMBOL_GPL(fat_sync_inode
);
751 static int fat_show_options(struct seq_file
*m
, struct dentry
*root
);
752 static const struct super_operations fat_sops
= {
753 .alloc_inode
= fat_alloc_inode
,
754 .destroy_inode
= fat_destroy_inode
,
755 .write_inode
= fat_write_inode
,
756 .evict_inode
= fat_evict_inode
,
757 .put_super
= fat_put_super
,
758 .statfs
= fat_statfs
,
759 .remount_fs
= fat_remount
,
761 .show_options
= fat_show_options
,
764 static int fat_show_options(struct seq_file
*m
, struct dentry
*root
)
766 struct msdos_sb_info
*sbi
= MSDOS_SB(root
->d_sb
);
767 struct fat_mount_options
*opts
= &sbi
->options
;
768 int isvfat
= opts
->isvfat
;
770 if (!uid_eq(opts
->fs_uid
, GLOBAL_ROOT_UID
))
771 seq_printf(m
, ",uid=%u",
772 from_kuid_munged(&init_user_ns
, opts
->fs_uid
));
773 if (!gid_eq(opts
->fs_gid
, GLOBAL_ROOT_GID
))
774 seq_printf(m
, ",gid=%u",
775 from_kgid_munged(&init_user_ns
, opts
->fs_gid
));
776 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask
);
777 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask
);
778 if (opts
->allow_utime
)
779 seq_printf(m
, ",allow_utime=%04o", opts
->allow_utime
);
781 /* strip "cp" prefix from displayed option */
782 seq_printf(m
, ",codepage=%s", &sbi
->nls_disk
->charset
[2]);
785 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
787 switch (opts
->shortname
) {
788 case VFAT_SFN_DISPLAY_WIN95
| VFAT_SFN_CREATE_WIN95
:
789 seq_puts(m
, ",shortname=win95");
791 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WINNT
:
792 seq_puts(m
, ",shortname=winnt");
794 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WIN95
:
795 seq_puts(m
, ",shortname=mixed");
797 case VFAT_SFN_DISPLAY_LOWER
| VFAT_SFN_CREATE_WIN95
:
798 seq_puts(m
, ",shortname=lower");
801 seq_puts(m
, ",shortname=unknown");
805 if (opts
->name_check
!= 'n')
806 seq_printf(m
, ",check=%c", opts
->name_check
);
808 seq_puts(m
, ",usefree");
810 seq_puts(m
, ",quiet");
812 seq_puts(m
, ",showexec");
813 if (opts
->sys_immutable
)
814 seq_puts(m
, ",sys_immutable");
817 seq_puts(m
, ",dotsOK=yes");
819 seq_puts(m
, ",nocase");
822 seq_puts(m
, ",utf8");
823 if (opts
->unicode_xlate
)
824 seq_puts(m
, ",uni_xlate");
826 seq_puts(m
, ",nonumtail");
828 seq_puts(m
, ",rodir");
831 seq_puts(m
, ",flush");
833 if (opts
->time_offset
)
834 seq_printf(m
, ",time_offset=%d", opts
->time_offset
);
836 seq_puts(m
, ",tz=UTC");
838 if (opts
->errors
== FAT_ERRORS_CONT
)
839 seq_puts(m
, ",errors=continue");
840 else if (opts
->errors
== FAT_ERRORS_PANIC
)
841 seq_puts(m
, ",errors=panic");
843 seq_puts(m
, ",errors=remount-ro");
844 if (opts
->nfs
== FAT_NFS_NOSTALE_RO
)
845 seq_puts(m
, ",nfs=nostale_ro");
847 seq_puts(m
, ",nfs=stale_rw");
849 seq_puts(m
, ",discard");
855 Opt_check_n
, Opt_check_r
, Opt_check_s
, Opt_uid
, Opt_gid
,
856 Opt_umask
, Opt_dmask
, Opt_fmask
, Opt_allow_utime
, Opt_codepage
,
857 Opt_usefree
, Opt_nocase
, Opt_quiet
, Opt_showexec
, Opt_debug
,
858 Opt_immutable
, Opt_dots
, Opt_nodots
,
859 Opt_charset
, Opt_shortname_lower
, Opt_shortname_win95
,
860 Opt_shortname_winnt
, Opt_shortname_mixed
, Opt_utf8_no
, Opt_utf8_yes
,
861 Opt_uni_xl_no
, Opt_uni_xl_yes
, Opt_nonumtail_no
, Opt_nonumtail_yes
,
862 Opt_obsolete
, Opt_flush
, Opt_tz_utc
, Opt_rodir
, Opt_err_cont
,
863 Opt_err_panic
, Opt_err_ro
, Opt_discard
, Opt_nfs
, Opt_time_offset
,
864 Opt_nfs_stale_rw
, Opt_nfs_nostale_ro
, Opt_err
,
867 static const match_table_t fat_tokens
= {
868 {Opt_check_r
, "check=relaxed"},
869 {Opt_check_s
, "check=strict"},
870 {Opt_check_n
, "check=normal"},
871 {Opt_check_r
, "check=r"},
872 {Opt_check_s
, "check=s"},
873 {Opt_check_n
, "check=n"},
876 {Opt_umask
, "umask=%o"},
877 {Opt_dmask
, "dmask=%o"},
878 {Opt_fmask
, "fmask=%o"},
879 {Opt_allow_utime
, "allow_utime=%o"},
880 {Opt_codepage
, "codepage=%u"},
881 {Opt_usefree
, "usefree"},
882 {Opt_nocase
, "nocase"},
883 {Opt_quiet
, "quiet"},
884 {Opt_showexec
, "showexec"},
885 {Opt_debug
, "debug"},
886 {Opt_immutable
, "sys_immutable"},
887 {Opt_flush
, "flush"},
888 {Opt_tz_utc
, "tz=UTC"},
889 {Opt_time_offset
, "time_offset=%d"},
890 {Opt_err_cont
, "errors=continue"},
891 {Opt_err_panic
, "errors=panic"},
892 {Opt_err_ro
, "errors=remount-ro"},
893 {Opt_discard
, "discard"},
894 {Opt_nfs_stale_rw
, "nfs"},
895 {Opt_nfs_stale_rw
, "nfs=stale_rw"},
896 {Opt_nfs_nostale_ro
, "nfs=nostale_ro"},
897 {Opt_obsolete
, "conv=binary"},
898 {Opt_obsolete
, "conv=text"},
899 {Opt_obsolete
, "conv=auto"},
900 {Opt_obsolete
, "conv=b"},
901 {Opt_obsolete
, "conv=t"},
902 {Opt_obsolete
, "conv=a"},
903 {Opt_obsolete
, "fat=%u"},
904 {Opt_obsolete
, "blocksize=%u"},
905 {Opt_obsolete
, "cvf_format=%20s"},
906 {Opt_obsolete
, "cvf_options=%100s"},
907 {Opt_obsolete
, "posix"},
910 static const match_table_t msdos_tokens
= {
911 {Opt_nodots
, "nodots"},
912 {Opt_nodots
, "dotsOK=no"},
914 {Opt_dots
, "dotsOK=yes"},
917 static const match_table_t vfat_tokens
= {
918 {Opt_charset
, "iocharset=%s"},
919 {Opt_shortname_lower
, "shortname=lower"},
920 {Opt_shortname_win95
, "shortname=win95"},
921 {Opt_shortname_winnt
, "shortname=winnt"},
922 {Opt_shortname_mixed
, "shortname=mixed"},
923 {Opt_utf8_no
, "utf8=0"}, /* 0 or no or false */
924 {Opt_utf8_no
, "utf8=no"},
925 {Opt_utf8_no
, "utf8=false"},
926 {Opt_utf8_yes
, "utf8=1"}, /* empty or 1 or yes or true */
927 {Opt_utf8_yes
, "utf8=yes"},
928 {Opt_utf8_yes
, "utf8=true"},
929 {Opt_utf8_yes
, "utf8"},
930 {Opt_uni_xl_no
, "uni_xlate=0"}, /* 0 or no or false */
931 {Opt_uni_xl_no
, "uni_xlate=no"},
932 {Opt_uni_xl_no
, "uni_xlate=false"},
933 {Opt_uni_xl_yes
, "uni_xlate=1"}, /* empty or 1 or yes or true */
934 {Opt_uni_xl_yes
, "uni_xlate=yes"},
935 {Opt_uni_xl_yes
, "uni_xlate=true"},
936 {Opt_uni_xl_yes
, "uni_xlate"},
937 {Opt_nonumtail_no
, "nonumtail=0"}, /* 0 or no or false */
938 {Opt_nonumtail_no
, "nonumtail=no"},
939 {Opt_nonumtail_no
, "nonumtail=false"},
940 {Opt_nonumtail_yes
, "nonumtail=1"}, /* empty or 1 or yes or true */
941 {Opt_nonumtail_yes
, "nonumtail=yes"},
942 {Opt_nonumtail_yes
, "nonumtail=true"},
943 {Opt_nonumtail_yes
, "nonumtail"},
944 {Opt_rodir
, "rodir"},
948 static int parse_options(struct super_block
*sb
, char *options
, int is_vfat
,
949 int silent
, int *debug
, struct fat_mount_options
*opts
)
952 substring_t args
[MAX_OPT_ARGS
];
956 opts
->isvfat
= is_vfat
;
958 opts
->fs_uid
= current_uid();
959 opts
->fs_gid
= current_gid();
960 opts
->fs_fmask
= opts
->fs_dmask
= current_umask();
961 opts
->allow_utime
= -1;
962 opts
->codepage
= fat_default_codepage
;
963 opts
->iocharset
= fat_default_iocharset
;
965 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
|VFAT_SFN_CREATE_WIN95
;
971 opts
->name_check
= 'n';
972 opts
->quiet
= opts
->showexec
= opts
->sys_immutable
= opts
->dotsOK
= 0;
973 opts
->utf8
= opts
->unicode_xlate
= 0;
975 opts
->usefree
= opts
->nocase
= 0;
978 opts
->errors
= FAT_ERRORS_RO
;
984 while ((p
= strsep(&options
, ",")) != NULL
) {
989 token
= match_token(p
, fat_tokens
, args
);
990 if (token
== Opt_err
) {
992 token
= match_token(p
, vfat_tokens
, args
);
994 token
= match_token(p
, msdos_tokens
, args
);
998 opts
->name_check
= 's';
1001 opts
->name_check
= 'r';
1004 opts
->name_check
= 'n';
1013 /* for backward compatibility */
1014 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
1015 | VFAT_SFN_CREATE_WIN95
;
1028 opts
->sys_immutable
= 1;
1031 if (match_int(&args
[0], &option
))
1033 opts
->fs_uid
= make_kuid(current_user_ns(), option
);
1034 if (!uid_valid(opts
->fs_uid
))
1038 if (match_int(&args
[0], &option
))
1040 opts
->fs_gid
= make_kgid(current_user_ns(), option
);
1041 if (!gid_valid(opts
->fs_gid
))
1045 if (match_octal(&args
[0], &option
))
1047 opts
->fs_fmask
= opts
->fs_dmask
= option
;
1050 if (match_octal(&args
[0], &option
))
1052 opts
->fs_dmask
= option
;
1055 if (match_octal(&args
[0], &option
))
1057 opts
->fs_fmask
= option
;
1059 case Opt_allow_utime
:
1060 if (match_octal(&args
[0], &option
))
1062 opts
->allow_utime
= option
& (S_IWGRP
| S_IWOTH
);
1065 if (match_int(&args
[0], &option
))
1067 opts
->codepage
= option
;
1072 case Opt_time_offset
:
1073 if (match_int(&args
[0], &option
))
1075 if (option
< -12 * 60 || option
> 12 * 60)
1078 opts
->time_offset
= option
;
1082 opts
->time_offset
= 0;
1085 opts
->errors
= FAT_ERRORS_CONT
;
1088 opts
->errors
= FAT_ERRORS_PANIC
;
1091 opts
->errors
= FAT_ERRORS_RO
;
1093 case Opt_nfs_stale_rw
:
1094 opts
->nfs
= FAT_NFS_STALE_RW
;
1096 case Opt_nfs_nostale_ro
:
1097 opts
->nfs
= FAT_NFS_NOSTALE_RO
;
1100 /* msdos specific */
1110 if (opts
->iocharset
!= fat_default_iocharset
)
1111 kfree(opts
->iocharset
);
1112 iocharset
= match_strdup(&args
[0]);
1115 opts
->iocharset
= iocharset
;
1117 case Opt_shortname_lower
:
1118 opts
->shortname
= VFAT_SFN_DISPLAY_LOWER
1119 | VFAT_SFN_CREATE_WIN95
;
1121 case Opt_shortname_win95
:
1122 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
1123 | VFAT_SFN_CREATE_WIN95
;
1125 case Opt_shortname_winnt
:
1126 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
1127 | VFAT_SFN_CREATE_WINNT
;
1129 case Opt_shortname_mixed
:
1130 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
1131 | VFAT_SFN_CREATE_WIN95
;
1133 case Opt_utf8_no
: /* 0 or no or false */
1136 case Opt_utf8_yes
: /* empty or 1 or yes or true */
1139 case Opt_uni_xl_no
: /* 0 or no or false */
1140 opts
->unicode_xlate
= 0;
1142 case Opt_uni_xl_yes
: /* empty or 1 or yes or true */
1143 opts
->unicode_xlate
= 1;
1145 case Opt_nonumtail_no
: /* 0 or no or false */
1146 opts
->numtail
= 1; /* negated option */
1148 case Opt_nonumtail_yes
: /* empty or 1 or yes or true */
1149 opts
->numtail
= 0; /* negated option */
1158 /* obsolete mount options */
1160 fat_msg(sb
, KERN_INFO
, "\"%s\" option is obsolete, "
1161 "not supported now", p
);
1163 /* unknown option */
1166 fat_msg(sb
, KERN_ERR
,
1167 "Unrecognized mount option \"%s\" "
1168 "or missing value", p
);
1175 /* UTF-8 doesn't provide FAT semantics */
1176 if (!strcmp(opts
->iocharset
, "utf8")) {
1177 fat_msg(sb
, KERN_WARNING
, "utf8 is not a recommended IO charset"
1178 " for FAT filesystems, filesystem will be "
1182 /* If user doesn't specify allow_utime, it's initialized from dmask. */
1183 if (opts
->allow_utime
== (unsigned short)-1)
1184 opts
->allow_utime
= ~opts
->fs_dmask
& (S_IWGRP
| S_IWOTH
);
1185 if (opts
->unicode_xlate
)
1187 if (opts
->nfs
== FAT_NFS_NOSTALE_RO
) {
1188 sb
->s_flags
|= MS_RDONLY
;
1189 sb
->s_export_op
= &fat_export_ops_nostale
;
1195 static int fat_read_root(struct inode
*inode
)
1197 struct super_block
*sb
= inode
->i_sb
;
1198 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1201 MSDOS_I(inode
)->i_pos
= MSDOS_ROOT_INO
;
1202 inode
->i_uid
= sbi
->options
.fs_uid
;
1203 inode
->i_gid
= sbi
->options
.fs_gid
;
1205 inode
->i_generation
= 0;
1206 inode
->i_mode
= fat_make_mode(sbi
, ATTR_DIR
, S_IRWXUGO
);
1207 inode
->i_op
= sbi
->dir_ops
;
1208 inode
->i_fop
= &fat_dir_operations
;
1209 if (sbi
->fat_bits
== 32) {
1210 MSDOS_I(inode
)->i_start
= sbi
->root_cluster
;
1211 error
= fat_calc_dir_size(inode
);
1215 MSDOS_I(inode
)->i_start
= 0;
1216 inode
->i_size
= sbi
->dir_entries
* sizeof(struct msdos_dir_entry
);
1218 inode
->i_blocks
= ((inode
->i_size
+ (sbi
->cluster_size
- 1))
1219 & ~((loff_t
)sbi
->cluster_size
- 1)) >> 9;
1220 MSDOS_I(inode
)->i_logstart
= 0;
1221 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
1223 fat_save_attrs(inode
, ATTR_DIR
);
1224 inode
->i_mtime
.tv_sec
= inode
->i_atime
.tv_sec
= inode
->i_ctime
.tv_sec
= 0;
1225 inode
->i_mtime
.tv_nsec
= inode
->i_atime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
1226 set_nlink(inode
, fat_subdirs(inode
)+2);
1232 * Read the super block of an MS-DOS FS.
1234 int fat_fill_super(struct super_block
*sb
, void *data
, int silent
, int isvfat
,
1235 void (*setup
)(struct super_block
*))
1237 struct inode
*root_inode
= NULL
, *fat_inode
= NULL
;
1238 struct inode
*fsinfo_inode
= NULL
;
1239 struct buffer_head
*bh
;
1240 struct fat_boot_sector
*b
;
1241 struct msdos_sb_info
*sbi
;
1242 u16 logical_sector_size
;
1243 u32 total_sectors
, total_clusters
, fat_clusters
, rootdir_sectors
;
1250 * GFP_KERNEL is ok here, because while we do hold the
1251 * supeblock lock, memory pressure can't call back into
1252 * the filesystem, since we're only just about to mount
1253 * it and have no inodes etc active!
1255 sbi
= kzalloc(sizeof(struct msdos_sb_info
), GFP_KERNEL
);
1258 sb
->s_fs_info
= sbi
;
1260 sb
->s_flags
|= MS_NODIRATIME
;
1261 sb
->s_magic
= MSDOS_SUPER_MAGIC
;
1262 sb
->s_op
= &fat_sops
;
1263 sb
->s_export_op
= &fat_export_ops
;
1264 mutex_init(&sbi
->nfs_build_inode_lock
);
1265 ratelimit_state_init(&sbi
->ratelimit
, DEFAULT_RATELIMIT_INTERVAL
,
1266 DEFAULT_RATELIMIT_BURST
);
1268 error
= parse_options(sb
, data
, isvfat
, silent
, &debug
, &sbi
->options
);
1272 setup(sb
); /* flavour-specific stuff that needs options */
1275 sb_min_blocksize(sb
, 512);
1276 bh
= sb_bread(sb
, 0);
1278 fat_msg(sb
, KERN_ERR
, "unable to read boot sector");
1282 b
= (struct fat_boot_sector
*) bh
->b_data
;
1285 fat_msg(sb
, KERN_ERR
, "bogus number of reserved sectors");
1291 fat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1297 * Earlier we checked here that b->secs_track and b->head are nonzero,
1298 * but it turns out valid FAT filesystems can have zero there.
1302 if (!fat_valid_media(media
)) {
1304 fat_msg(sb
, KERN_ERR
, "invalid media value (0x%02x)",
1309 logical_sector_size
= get_unaligned_le16(&b
->sector_size
);
1310 if (!is_power_of_2(logical_sector_size
)
1311 || (logical_sector_size
< 512)
1312 || (logical_sector_size
> 4096)) {
1314 fat_msg(sb
, KERN_ERR
, "bogus logical sector size %u",
1315 logical_sector_size
);
1319 sbi
->sec_per_clus
= b
->sec_per_clus
;
1320 if (!is_power_of_2(sbi
->sec_per_clus
)) {
1322 fat_msg(sb
, KERN_ERR
, "bogus sectors per cluster %u",
1328 if (logical_sector_size
< sb
->s_blocksize
) {
1329 fat_msg(sb
, KERN_ERR
, "logical sector size too small for device"
1330 " (logical sector size = %u)", logical_sector_size
);
1334 if (logical_sector_size
> sb
->s_blocksize
) {
1337 if (!sb_set_blocksize(sb
, logical_sector_size
)) {
1338 fat_msg(sb
, KERN_ERR
, "unable to set blocksize %u",
1339 logical_sector_size
);
1342 bh
= sb_bread(sb
, 0);
1344 fat_msg(sb
, KERN_ERR
, "unable to read boot sector"
1345 " (logical sector size = %lu)",
1349 b
= (struct fat_boot_sector
*) bh
->b_data
;
1352 mutex_init(&sbi
->s_lock
);
1353 sbi
->cluster_size
= sb
->s_blocksize
* sbi
->sec_per_clus
;
1354 sbi
->cluster_bits
= ffs(sbi
->cluster_size
) - 1;
1355 sbi
->fats
= b
->fats
;
1356 sbi
->fat_bits
= 0; /* Don't know yet */
1357 sbi
->fat_start
= le16_to_cpu(b
->reserved
);
1358 sbi
->fat_length
= le16_to_cpu(b
->fat_length
);
1359 sbi
->root_cluster
= 0;
1360 sbi
->free_clusters
= -1; /* Don't know yet */
1361 sbi
->free_clus_valid
= 0;
1362 sbi
->prev_free
= FAT_START_ENT
;
1363 sb
->s_maxbytes
= 0xffffffff;
1365 if (!sbi
->fat_length
&& b
->fat32
.length
) {
1366 struct fat_boot_fsinfo
*fsinfo
;
1367 struct buffer_head
*fsinfo_bh
;
1371 sbi
->fat_length
= le32_to_cpu(b
->fat32
.length
);
1372 sbi
->root_cluster
= le32_to_cpu(b
->fat32
.root_cluster
);
1374 /* MC - if info_sector is 0, don't multiply by 0 */
1375 sbi
->fsinfo_sector
= le16_to_cpu(b
->fat32
.info_sector
);
1376 if (sbi
->fsinfo_sector
== 0)
1377 sbi
->fsinfo_sector
= 1;
1379 fsinfo_bh
= sb_bread(sb
, sbi
->fsinfo_sector
);
1380 if (fsinfo_bh
== NULL
) {
1381 fat_msg(sb
, KERN_ERR
, "bread failed, FSINFO block"
1382 " (sector = %lu)", sbi
->fsinfo_sector
);
1387 fsinfo
= (struct fat_boot_fsinfo
*)fsinfo_bh
->b_data
;
1388 if (!IS_FSINFO(fsinfo
)) {
1389 fat_msg(sb
, KERN_WARNING
, "Invalid FSINFO signature: "
1390 "0x%08x, 0x%08x (sector = %lu)",
1391 le32_to_cpu(fsinfo
->signature1
),
1392 le32_to_cpu(fsinfo
->signature2
),
1393 sbi
->fsinfo_sector
);
1395 if (sbi
->options
.usefree
)
1396 sbi
->free_clus_valid
= 1;
1397 sbi
->free_clusters
= le32_to_cpu(fsinfo
->free_clusters
);
1398 sbi
->prev_free
= le32_to_cpu(fsinfo
->next_cluster
);
1404 sbi
->dir_per_block
= sb
->s_blocksize
/ sizeof(struct msdos_dir_entry
);
1405 sbi
->dir_per_block_bits
= ffs(sbi
->dir_per_block
) - 1;
1407 sbi
->dir_start
= sbi
->fat_start
+ sbi
->fats
* sbi
->fat_length
;
1408 sbi
->dir_entries
= get_unaligned_le16(&b
->dir_entries
);
1409 if (sbi
->dir_entries
& (sbi
->dir_per_block
- 1)) {
1411 fat_msg(sb
, KERN_ERR
, "bogus directory-entries per block"
1412 " (%u)", sbi
->dir_entries
);
1417 rootdir_sectors
= sbi
->dir_entries
1418 * sizeof(struct msdos_dir_entry
) / sb
->s_blocksize
;
1419 sbi
->data_start
= sbi
->dir_start
+ rootdir_sectors
;
1420 total_sectors
= get_unaligned_le16(&b
->sectors
);
1421 if (total_sectors
== 0)
1422 total_sectors
= le32_to_cpu(b
->total_sect
);
1424 total_clusters
= (total_sectors
- sbi
->data_start
) / sbi
->sec_per_clus
;
1426 if (sbi
->fat_bits
!= 32)
1427 sbi
->fat_bits
= (total_clusters
> MAX_FAT12
) ? 16 : 12;
1429 /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1430 if (sbi
->fat_bits
== 32)
1431 sbi
->dirty
= b
->fat32
.state
& FAT_STATE_DIRTY
;
1432 else /* fat 16 or 12 */
1433 sbi
->dirty
= b
->fat16
.state
& FAT_STATE_DIRTY
;
1435 /* check that FAT table does not overflow */
1436 fat_clusters
= sbi
->fat_length
* sb
->s_blocksize
* 8 / sbi
->fat_bits
;
1437 total_clusters
= min(total_clusters
, fat_clusters
- FAT_START_ENT
);
1438 if (total_clusters
> MAX_FAT(sb
)) {
1440 fat_msg(sb
, KERN_ERR
, "count of clusters too big (%u)",
1446 sbi
->max_cluster
= total_clusters
+ FAT_START_ENT
;
1447 /* check the free_clusters, it's not necessarily correct */
1448 if (sbi
->free_clusters
!= -1 && sbi
->free_clusters
> total_clusters
)
1449 sbi
->free_clusters
= -1;
1450 /* check the prev_free, it's not necessarily correct */
1451 sbi
->prev_free
%= sbi
->max_cluster
;
1452 if (sbi
->prev_free
< FAT_START_ENT
)
1453 sbi
->prev_free
= FAT_START_ENT
;
1457 /* set up enough so that it can read an inode */
1460 fat_ent_access_init(sb
);
1463 * The low byte of FAT's first entry must have same value with
1464 * media-field. But in real world, too many devices is
1465 * writing wrong value. So, removed that validity check.
1467 * if (FAT_FIRST_ENT(sb, media) != first)
1471 sprintf(buf
, "cp%d", sbi
->options
.codepage
);
1472 sbi
->nls_disk
= load_nls(buf
);
1473 if (!sbi
->nls_disk
) {
1474 fat_msg(sb
, KERN_ERR
, "codepage %s not found", buf
);
1478 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1479 if (sbi
->options
.isvfat
) {
1480 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
1482 fat_msg(sb
, KERN_ERR
, "IO charset %s not found",
1483 sbi
->options
.iocharset
);
1489 fat_inode
= new_inode(sb
);
1492 MSDOS_I(fat_inode
)->i_pos
= 0;
1493 sbi
->fat_inode
= fat_inode
;
1495 fsinfo_inode
= new_inode(sb
);
1498 fsinfo_inode
->i_ino
= MSDOS_FSINFO_INO
;
1499 sbi
->fsinfo_inode
= fsinfo_inode
;
1500 insert_inode_hash(fsinfo_inode
);
1502 root_inode
= new_inode(sb
);
1505 root_inode
->i_ino
= MSDOS_ROOT_INO
;
1506 root_inode
->i_version
= 1;
1507 error
= fat_read_root(root_inode
);
1513 insert_inode_hash(root_inode
);
1514 fat_attach(root_inode
, 0);
1515 sb
->s_root
= d_make_root(root_inode
);
1517 fat_msg(sb
, KERN_ERR
, "get root inode failed");
1521 if (sbi
->options
.discard
) {
1522 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1523 if (!blk_queue_discard(q
))
1524 fat_msg(sb
, KERN_WARNING
,
1525 "mounting with \"discard\" option, but "
1526 "the device does not support discard");
1529 fat_set_state(sb
, 1, 0);
1535 fat_msg(sb
, KERN_INFO
, "Can't find a valid FAT filesystem");
1542 unload_nls(sbi
->nls_io
);
1543 unload_nls(sbi
->nls_disk
);
1544 if (sbi
->options
.iocharset
!= fat_default_iocharset
)
1545 kfree(sbi
->options
.iocharset
);
1546 sb
->s_fs_info
= NULL
;
1551 EXPORT_SYMBOL_GPL(fat_fill_super
);
1554 * helper function for fat_flush_inodes. This writes both the inode
1555 * and the file data blocks, waiting for in flight data blocks before
1556 * the start of the call. It does not wait for any io started
1559 static int writeback_inode(struct inode
*inode
)
1564 /* if we used wait=1, sync_inode_metadata waits for the io for the
1565 * inode to finish. So wait=0 is sent down to sync_inode_metadata
1566 * and filemap_fdatawrite is used for the data blocks
1568 ret
= sync_inode_metadata(inode
, 0);
1570 ret
= filemap_fdatawrite(inode
->i_mapping
);
1575 * write data and metadata corresponding to i1 and i2. The io is
1576 * started but we do not wait for any of it to finish.
1578 * filemap_flush is used for the block device, so if there is a dirty
1579 * page for a block already in flight, we will not wait and start the
1582 int fat_flush_inodes(struct super_block
*sb
, struct inode
*i1
, struct inode
*i2
)
1585 if (!MSDOS_SB(sb
)->options
.flush
)
1588 ret
= writeback_inode(i1
);
1590 ret
= writeback_inode(i2
);
1592 struct address_space
*mapping
= sb
->s_bdev
->bd_inode
->i_mapping
;
1593 ret
= filemap_flush(mapping
);
1597 EXPORT_SYMBOL_GPL(fat_flush_inodes
);
1599 static int __init
init_fat_fs(void)
1603 err
= fat_cache_init();
1607 err
= fat_init_inodecache();
1614 fat_cache_destroy();
1618 static void __exit
exit_fat_fs(void)
1620 fat_cache_destroy();
1621 fat_destroy_inodecache();
1624 module_init(init_fat_fs
)
1625 module_exit(exit_fat_fs
)
1627 MODULE_LICENSE("GPL");