13 #include <linux/module.h>
15 #include <linux/time.h>
16 #include <linux/slab.h>
25 #include <linux/uio.h>
28 #include <linux/hash.h>
29 #include <asm/unaligned.h>
32 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
34 #define CONFIG_FAT_DEFAULT_IOCHARSET ""
37 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
41 static int fat_add_cluster(
struct inode *
inode)
57 unsigned long *max_blocks,
58 struct buffer_head *bh_result,
int create)
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);
78 fat_fs_error(sb,
"corrupted file size (i_pos %lld, %lld)",
79 MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
86 err = fat_add_cluster(inode);
93 *max_blocks =
min(mapped_blocks, *max_blocks);
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)
112 unsigned long max_blocks = bh_result->b_size >> inode->
i_blkbits;
115 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
146 struct inode *inode = mapping->
host;
155 loff_t
pos,
unsigned len,
unsigned flags,
156 struct page **pagep,
void **fsdata)
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;
177 fat_write_failed(mapping, pos + len);
178 if (!(err < 0) && !(MSDOS_I(inode)->i_attrs &
ATTR_ARCH)) {
181 mark_inode_dirty(inode);
187 const struct iovec *iov,
188 loff_t offset,
unsigned long nr_segs)
190 struct file *file = iocb->
ki_filp;
192 struct inode *inode = mapping->
host;
205 loff_t
size = offset + iov_length(iov, nr_segs);
206 if (MSDOS_I(inode)->mmu_private < size)
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));
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,
279 static inline unsigned long fat_hash(loff_t i_pos)
303 MSDOS_I(inode)->i_pos = i_pos;
304 hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
314 d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
317 hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
327 MSDOS_I(inode)->i_pos = 0;
328 hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
333 hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
345 struct inode *inode =
NULL;
350 if (i->
i_pos != i_pos)
360 static int is_exec(
unsigned char *
extension)
362 unsigned char *exe_extensions =
"EXECOMBAT", *walk;
364 for (walk = exe_extensions; *walk; walk += 3)
365 if (!
strncmp(extension, walk, 3))
370 static int fat_calc_dir_size(
struct inode *inode)
373 int ret, fclus, dclus;
376 if (MSDOS_I(inode)->i_start == 0)
388 static int fat_fill_inode(
struct inode *inode,
struct msdos_dir_entry *de)
393 MSDOS_I(inode)->i_pos = 0;
405 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
406 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
407 error = fat_calc_dir_size(inode);
410 MSDOS_I(inode)->mmu_private = inode->
i_size;
418 MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
420 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
425 MSDOS_I(inode)->mmu_private = inode->
i_size;
428 if (sbi->
options.sys_immutable)
431 fat_save_attrs(inode, de->
attr);
463 err = fat_fill_inode(inode, de);
466 inode = ERR_PTR(err);
470 insert_inode_hash(inode);
477 static void fat_evict_inode(
struct inode *inode)
500 if (sbi->
options.iocharset != fat_default_iocharset)
509 static struct inode *fat_alloc_inode(
struct super_block *sb)
526 static void fat_destroy_inode(
struct inode *inode)
531 static void init_once(
void *
foo)
544 static int __init fat_init_inodecache(
void)
551 if (fat_inode_cachep ==
NULL)
556 static void __exit fat_destroy_inodecache(
void)
577 u64 id = huge_encode_dev(sb->
s_bdev->bd_dev);
599 static inline loff_t fat_i_pos_read(
struct msdos_sb_info *sbi,
603 #if BITS_PER_LONG == 32
606 i_pos = MSDOS_I(inode)->i_pos;
607 #if BITS_PER_LONG == 32
613 static int __fat_write_inode(
struct inode *inode,
int wait)
617 struct buffer_head *bh;
626 i_pos = fat_i_pos_read(sbi, inode);
633 "for updating (i_pos %lld)", i_pos);
637 if (i_pos != MSDOS_I(inode)->i_pos) {
649 raw_entry->
attr = fat_make_attrs(inode);
650 fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
687 return __fat_write_inode(inode, 1);
692 static int fat_show_options(
struct seq_file *
m,
struct dentry *root);
694 .alloc_inode = fat_alloc_inode,
695 .destroy_inode = fat_destroy_inode,
696 .write_inode = fat_write_inode,
697 .evict_inode = fat_evict_inode,
698 .put_super = fat_put_super,
699 .statfs = fat_statfs,
700 .remount_fs = fat_remount,
702 .show_options = fat_show_options,
711 static int fat_show_options(
struct seq_file *
m,
struct dentry *root)
898 opts->
codepage = fat_default_codepage;
970 if (!uid_valid(opts->
fs_uid))
977 if (!gid_valid(opts->
fs_gid))
1031 if (opts->
iocharset != fat_default_iocharset)
1085 "not supported now", p);
1091 "Unrecognized mount option \"%s\" "
1092 "or missing value", p);
1102 " for FAT filesystems, filesystem will be "
1115 static int fat_read_root(
struct inode *inode)
1121 MSDOS_I(inode)->i_pos = 0;
1131 error = fat_calc_dir_size(inode);
1135 MSDOS_I(inode)->i_start = 0;
1140 MSDOS_I(inode)->i_logstart = 0;
1141 MSDOS_I(inode)->mmu_private = inode->
i_size;
1157 struct inode *root_inode =
NULL, *fat_inode =
NULL;
1158 struct inode *fsinfo_inode =
NULL;
1159 struct buffer_head *bh;
1162 u16 logical_sector_size;
1163 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1182 sb->
s_op = &fat_sops;
1187 error = parse_options(sb, data, isvfat, silent, &debug, &sbi->
options);
1195 bh = sb_bread(sb, 0);
1221 if (!fat_valid_media(media)) {
1228 logical_sector_size = get_unaligned_le16(&b->
sector_size);
1230 || (logical_sector_size < 512)
1231 || (logical_sector_size > 4096)) {
1234 logical_sector_size);
1249 " (logical sector size = %u)", logical_sector_size);
1258 logical_sector_size);
1261 bh = sb_bread(sb, 0);
1264 " (logical sector size = %lu)",
1286 struct buffer_head *fsinfo_bh;
1299 if (fsinfo_bh ==
NULL) {
1309 "0x%08x, 0x%08x (sector = %lu)",
1339 total_sectors = get_unaligned_le16(&b->
sectors);
1340 if (total_sectors == 0)
1351 if (total_clusters >
MAX_FAT(sb)) {
1405 MSDOS_I(fat_inode)->i_pos = 0;
1413 insert_inode_hash(fsinfo_inode);
1420 error = fat_read_root(root_inode);
1426 insert_inode_hash(root_inode);
1448 if (sbi->
options.iocharset != fat_default_iocharset)
1450 sb->s_fs_info =
NULL;
1463 static int writeback_inode(
struct inode *inode)
1489 if (!MSDOS_SB(sb)->options.flush)
1492 ret = writeback_inode(i1);
1494 ret = writeback_inode(i2);
1503 static int __init init_fat_fs(
void)
1511 err = fat_init_inodecache();
1522 static void __exit exit_fat_fs(
void)
1525 fat_destroy_inodecache();