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