1 /* 2 * linux/fs/fat/inode.c 3 * 4 * Written 1992,1993 by Werner Almesberger 5 * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner 6 * Rewritten for the constant inumbers support by Al Viro 7 * 8 * Fixes: 9 * 10 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 11 */ 12 13 #include <linux/module.h> 14 #include <linux/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> 30 #include "fat.h" 31 32 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET 33 /* if user don't select VFAT, this is undefined. */ 34 #define CONFIG_FAT_DEFAULT_IOCHARSET "" 35 #endif 36 37 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; 38 static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; 39 40 41 static int fat_add_cluster(struct inode *inode) 42 { 43 int err, cluster; 44 45 err = fat_alloc_clusters(inode, &cluster, 1); 46 if (err) 47 return err; 48 /* FIXME: this cluster should be added after data of this 49 * cluster is writed */ 50 err = fat_chain_add(inode, cluster, 1); 51 if (err) 52 fat_free_clusters(inode, cluster); 53 return err; 54 } 55 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) 59 { 60 struct super_block *sb = inode->i_sb; 61 struct msdos_sb_info *sbi = MSDOS_SB(sb); 62 unsigned long mapped_blocks; 63 sector_t phys; 64 int err, offset; 65 66 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create); 67 if (err) 68 return err; 69 if (phys) { 70 map_bh(bh_result, sb, phys); 71 *max_blocks = min(mapped_blocks, *max_blocks); 72 return 0; 73 } 74 if (!create) 75 return 0; 76 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); 80 return -EIO; 81 } 82 83 offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); 84 if (!offset) { 85 /* TODO: multiple cluster allocation would be desirable. */ 86 err = fat_add_cluster(inode); 87 if (err) 88 return err; 89 } 90 /* available blocks on this cluster */ 91 mapped_blocks = sbi->sec_per_clus - offset; 92 93 *max_blocks = min(mapped_blocks, *max_blocks); 94 MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; 95 96 err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create); 97 if (err) 98 return err; 99 100 BUG_ON(!phys); 101 BUG_ON(*max_blocks != mapped_blocks); 102 set_buffer_new(bh_result); 103 map_bh(bh_result, sb, phys); 104 105 return 0; 106 } 107 108 static int fat_get_block(struct inode *inode, sector_t iblock, 109 struct buffer_head *bh_result, int create) 110 { 111 struct super_block *sb = inode->i_sb; 112 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 113 int err; 114 115 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); 116 if (err) 117 return err; 118 bh_result->b_size = max_blocks << sb->s_blocksize_bits; 119 return 0; 120 } 121 122 static int fat_writepage(struct page *page, struct writeback_control *wbc) 123 { 124 return block_write_full_page(page, fat_get_block, wbc); 125 } 126 127 static int fat_writepages(struct address_space *mapping, 128 struct writeback_control *wbc) 129 { 130 return mpage_writepages(mapping, wbc, fat_get_block); 131 } 132 133 static int fat_readpage(struct file *file, struct page *page) 134 { 135 return mpage_readpage(page, fat_get_block); 136 } 137 138 static int fat_readpages(struct file *file, struct address_space *mapping, 139 struct list_head *pages, unsigned nr_pages) 140 { 141 return mpage_readpages(mapping, pages, nr_pages, fat_get_block); 142 } 143 144 static void fat_write_failed(struct address_space *mapping, loff_t to) 145 { 146 struct inode *inode = mapping->host; 147 148 if (to > inode->i_size) { 149 truncate_pagecache(inode, to, inode->i_size); 150 fat_truncate_blocks(inode, inode->i_size); 151 } 152 } 153 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) 157 { 158 int err; 159 160 *pagep = NULL; 161 err = cont_write_begin(file, mapping, pos, len, flags, 162 pagep, fsdata, fat_get_block, 163 &MSDOS_I(mapping->host)->mmu_private); 164 if (err < 0) 165 fat_write_failed(mapping, pos + len); 166 return err; 167 } 168 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) 172 { 173 struct inode *inode = mapping->host; 174 int err; 175 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); 176 if (err < len) 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); 182 } 183 return err; 184 } 185 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) 189 { 190 struct file *file = iocb->ki_filp; 191 struct address_space *mapping = file->f_mapping; 192 struct inode *inode = mapping->host; 193 ssize_t ret; 194 195 if (rw == WRITE) { 196 /* 197 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 198 * so we need to update the ->mmu_private to block boundary. 199 * 200 * But we must fill the remaining area or hole by nul for 201 * updating ->mmu_private. 202 * 203 * Return 0, and fallback to normal buffered write. 204 */ 205 loff_t size = offset + iov_length(iov, nr_segs); 206 if (MSDOS_I(inode)->mmu_private < size) 207 return 0; 208 } 209 210 /* 211 * FAT need to use the DIO_LOCKING for avoiding the race 212 * condition of fat_get_block() and ->truncate(). 213 */ 214 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, 215 fat_get_block); 216 if (ret < 0 && (rw & WRITE)) 217 fat_write_failed(mapping, offset + iov_length(iov, nr_segs)); 218 219 return ret; 220 } 221 222 static sector_t _fat_bmap(struct address_space *mapping, sector_t block) 223 { 224 sector_t blocknr; 225 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); 230 231 return blocknr; 232 } 233 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, 242 .bmap = _fat_bmap 243 }; 244 245 /* 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 253 * change. 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 257 * a new place. 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. 267 */ 268 269 static void fat_hash_init(struct super_block *sb) 270 { 271 struct msdos_sb_info *sbi = MSDOS_SB(sb); 272 int i; 273 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]); 277 } 278 279 static inline unsigned long fat_hash(loff_t i_pos) 280 { 281 return hash_32(i_pos, FAT_HASH_BITS); 282 } 283 284 static void dir_hash_init(struct super_block *sb) 285 { 286 struct msdos_sb_info *sbi = MSDOS_SB(sb); 287 int i; 288 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]); 292 } 293 294 void fat_attach(struct inode *inode, loff_t i_pos) 295 { 296 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 297 298 if (inode->i_ino != MSDOS_ROOT_INO) { 299 struct hlist_head *head = sbi->inode_hashtable 300 + fat_hash(i_pos); 301 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); 306 } 307 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. 311 */ 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); 315 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); 319 } 320 } 321 EXPORT_SYMBOL_GPL(fat_attach); 322 323 void fat_detach(struct inode *inode) 324 { 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); 330 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); 335 } 336 } 337 EXPORT_SYMBOL_GPL(fat_detach); 338 339 struct inode *fat_iget(struct super_block *sb, loff_t i_pos) 340 { 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; 345 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) 350 continue; 351 inode = igrab(&i->vfs_inode); 352 if (inode) 353 break; 354 } 355 spin_unlock(&sbi->inode_hash_lock); 356 return inode; 357 } 358 359 static int is_exec(unsigned char *extension) 360 { 361 unsigned char *exe_extensions = "EXECOMBAT", *walk; 362 363 for (walk = exe_extensions; *walk; walk += 3) 364 if (!strncmp(extension, walk, 3)) 365 return 1; 366 return 0; 367 } 368 369 static int fat_calc_dir_size(struct inode *inode) 370 { 371 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 372 int ret, fclus, dclus; 373 374 inode->i_size = 0; 375 if (MSDOS_I(inode)->i_start == 0) 376 return 0; 377 378 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); 379 if (ret < 0) 380 return ret; 381 inode->i_size = (fclus + 1) << sbi->cluster_bits; 382 383 return 0; 384 } 385 386 /* doesn't deal with root inode */ 387 int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) 388 { 389 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 390 int error; 391 392 MSDOS_I(inode)->i_pos = 0; 393 inode->i_uid = sbi->options.fs_uid; 394 inode->i_gid = sbi->options.fs_gid; 395 inode->i_version++; 396 inode->i_generation = get_seconds(); 397 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; 403 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); 407 if (error < 0) 408 return error; 409 MSDOS_I(inode)->mmu_private = inode->i_size; 410 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); 418 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; 425 } 426 if (de->attr & ATTR_SYS) { 427 if (sbi->options.sys_immutable) 428 inode->i_flags |= S_IMMUTABLE; 429 } 430 fat_save_attrs(inode, de->attr); 431 432 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) 433 & ~((loff_t)sbi->cluster_size - 1)) >> 9; 434 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); 440 } else 441 inode->i_ctime = inode->i_atime = inode->i_mtime; 442 443 return 0; 444 } 445 446 static inline void fat_lock_build_inode(struct msdos_sb_info *sbi) 447 { 448 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 449 mutex_lock(&sbi->nfs_build_inode_lock); 450 } 451 452 static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi) 453 { 454 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 455 mutex_unlock(&sbi->nfs_build_inode_lock); 456 } 457 458 struct inode *fat_build_inode(struct super_block *sb, 459 struct msdos_dir_entry *de, loff_t i_pos) 460 { 461 struct inode *inode; 462 int err; 463 464 fat_lock_build_inode(MSDOS_SB(sb)); 465 inode = fat_iget(sb, i_pos); 466 if (inode) 467 goto out; 468 inode = new_inode(sb); 469 if (!inode) { 470 inode = ERR_PTR(-ENOMEM); 471 goto out; 472 } 473 inode->i_ino = iunique(sb, MSDOS_ROOT_INO); 474 inode->i_version = 1; 475 err = fat_fill_inode(inode, de); 476 if (err) { 477 iput(inode); 478 inode = ERR_PTR(err); 479 goto out; 480 } 481 fat_attach(inode, i_pos); 482 insert_inode_hash(inode); 483 out: 484 fat_unlock_build_inode(MSDOS_SB(sb)); 485 return inode; 486 } 487 488 EXPORT_SYMBOL_GPL(fat_build_inode); 489 490 static void fat_evict_inode(struct inode *inode) 491 { 492 truncate_inode_pages(&inode->i_data, 0); 493 if (!inode->i_nlink) { 494 inode->i_size = 0; 495 fat_truncate_blocks(inode, 0); 496 } 497 invalidate_inode_buffers(inode); 498 clear_inode(inode); 499 fat_cache_inval_inode(inode); 500 fat_detach(inode); 501 } 502 503 static void fat_set_state(struct super_block *sb, 504 unsigned int set, unsigned int force) 505 { 506 struct buffer_head *bh; 507 struct fat_boot_sector *b; 508 struct msdos_sb_info *sbi = sb->s_fs_info; 509 510 /* do not change any thing if mounted read only */ 511 if ((sb->s_flags & MS_RDONLY) && !force) 512 return; 513 514 /* do not change state if fs was dirty */ 515 if (sbi->dirty) { 516 /* warn only on set (mount). */ 517 if (set) 518 fat_msg(sb, KERN_WARNING, "Volume was not properly " 519 "unmounted. Some data may be corrupt. " 520 "Please run fsck."); 521 return; 522 } 523 524 bh = sb_bread(sb, 0); 525 if (bh == NULL) { 526 fat_msg(sb, KERN_ERR, "unable to read boot sector " 527 "to mark fs as dirty"); 528 return; 529 } 530 531 b = (struct fat_boot_sector *) bh->b_data; 532 533 if (sbi->fat_bits == 32) { 534 if (set) 535 b->fat32.state |= FAT_STATE_DIRTY; 536 else 537 b->fat32.state &= ~FAT_STATE_DIRTY; 538 } else /* fat 16 and 12 */ { 539 if (set) 540 b->fat16.state |= FAT_STATE_DIRTY; 541 else 542 b->fat16.state &= ~FAT_STATE_DIRTY; 543 } 544 545 mark_buffer_dirty(bh); 546 sync_dirty_buffer(bh); 547 brelse(bh); 548 } 549 550 static void fat_put_super(struct super_block *sb) 551 { 552 struct msdos_sb_info *sbi = MSDOS_SB(sb); 553 554 fat_set_state(sb, 0, 0); 555 556 iput(sbi->fsinfo_inode); 557 iput(sbi->fat_inode); 558 559 unload_nls(sbi->nls_disk); 560 unload_nls(sbi->nls_io); 561 562 if (sbi->options.iocharset != fat_default_iocharset) 563 kfree(sbi->options.iocharset); 564 565 sb->s_fs_info = NULL; 566 kfree(sbi); 567 } 568 569 static struct kmem_cache *fat_inode_cachep; 570 571 static struct inode *fat_alloc_inode(struct super_block *sb) 572 { 573 struct msdos_inode_info *ei; 574 ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS); 575 if (!ei) 576 return NULL; 577 578 init_rwsem(&ei->truncate_lock); 579 return &ei->vfs_inode; 580 } 581 582 static void fat_i_callback(struct rcu_head *head) 583 { 584 struct inode *inode = container_of(head, struct inode, i_rcu); 585 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); 586 } 587 588 static void fat_destroy_inode(struct inode *inode) 589 { 590 call_rcu(&inode->i_rcu, fat_i_callback); 591 } 592 593 static void init_once(void *foo) 594 { 595 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; 596 597 spin_lock_init(&ei->cache_lru_lock); 598 ei->nr_caches = 0; 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); 604 } 605 606 static int __init fat_init_inodecache(void) 607 { 608 fat_inode_cachep = kmem_cache_create("fat_inode_cache", 609 sizeof(struct msdos_inode_info), 610 0, (SLAB_RECLAIM_ACCOUNT| 611 SLAB_MEM_SPREAD), 612 init_once); 613 if (fat_inode_cachep == NULL) 614 return -ENOMEM; 615 return 0; 616 } 617 618 static void __exit fat_destroy_inodecache(void) 619 { 620 /* 621 * Make sure all delayed rcu free inodes are flushed before we 622 * destroy cache. 623 */ 624 rcu_barrier(); 625 kmem_cache_destroy(fat_inode_cachep); 626 } 627 628 static int fat_remount(struct super_block *sb, int *flags, char *data) 629 { 630 int new_rdonly; 631 struct msdos_sb_info *sbi = MSDOS_SB(sb); 632 *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME); 633 634 /* make sure we update state on remount. */ 635 new_rdonly = *flags & MS_RDONLY; 636 if (new_rdonly != (sb->s_flags & MS_RDONLY)) { 637 if (new_rdonly) 638 fat_set_state(sb, 0, 0); 639 else 640 fat_set_state(sb, 1, 1); 641 } 642 return 0; 643 } 644 645 static int fat_statfs(struct dentry *dentry, struct kstatfs *buf) 646 { 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); 650 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); 654 if (err) 655 return err; 656 } 657 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); 665 buf->f_namelen = 666 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE; 667 668 return 0; 669 } 670 671 static int __fat_write_inode(struct inode *inode, int wait) 672 { 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; 677 loff_t i_pos; 678 sector_t blocknr; 679 int err, offset; 680 681 if (inode->i_ino == MSDOS_ROOT_INO) 682 return 0; 683 684 retry: 685 i_pos = fat_i_pos_read(sbi, inode); 686 if (!i_pos) 687 return 0; 688 689 fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset); 690 bh = sb_bread(sb, blocknr); 691 if (!bh) { 692 fat_msg(sb, KERN_ERR, "unable to read inode block " 693 "for updating (i_pos %lld)", i_pos); 694 return -EIO; 695 } 696 spin_lock(&sbi->inode_hash_lock); 697 if (i_pos != MSDOS_I(inode)->i_pos) { 698 spin_unlock(&sbi->inode_hash_lock); 699 brelse(bh); 700 goto retry; 701 } 702 703 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset]; 704 if (S_ISDIR(inode->i_mode)) 705 raw_entry->size = 0; 706 else 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) { 713 __le16 atime; 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); 718 } 719 spin_unlock(&sbi->inode_hash_lock); 720 mark_buffer_dirty(bh); 721 err = 0; 722 if (wait) 723 err = sync_dirty_buffer(bh); 724 brelse(bh); 725 return err; 726 } 727 728 static int fat_write_inode(struct inode *inode, struct writeback_control *wbc) 729 { 730 int err; 731 732 if (inode->i_ino == MSDOS_FSINFO_INO) { 733 struct super_block *sb = inode->i_sb; 734 735 mutex_lock(&MSDOS_SB(sb)->s_lock); 736 err = fat_clusters_flush(sb); 737 mutex_unlock(&MSDOS_SB(sb)->s_lock); 738 } else 739 err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 740 741 return err; 742 } 743 744 int fat_sync_inode(struct inode *inode) 745 { 746 return __fat_write_inode(inode, 1); 747 } 748 749 EXPORT_SYMBOL_GPL(fat_sync_inode); 750 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, 760 761 .show_options = fat_show_options, 762 }; 763 764 static int fat_show_options(struct seq_file *m, struct dentry *root) 765 { 766 struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb); 767 struct fat_mount_options *opts = &sbi->options; 768 int isvfat = opts->isvfat; 769 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); 780 if (sbi->nls_disk) 781 /* strip "cp" prefix from displayed option */ 782 seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]); 783 if (isvfat) { 784 if (sbi->nls_io) 785 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); 786 787 switch (opts->shortname) { 788 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: 789 seq_puts(m, ",shortname=win95"); 790 break; 791 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: 792 seq_puts(m, ",shortname=winnt"); 793 break; 794 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: 795 seq_puts(m, ",shortname=mixed"); 796 break; 797 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: 798 seq_puts(m, ",shortname=lower"); 799 break; 800 default: 801 seq_puts(m, ",shortname=unknown"); 802 break; 803 } 804 } 805 if (opts->name_check != 'n') 806 seq_printf(m, ",check=%c", opts->name_check); 807 if (opts->usefree) 808 seq_puts(m, ",usefree"); 809 if (opts->quiet) 810 seq_puts(m, ",quiet"); 811 if (opts->showexec) 812 seq_puts(m, ",showexec"); 813 if (opts->sys_immutable) 814 seq_puts(m, ",sys_immutable"); 815 if (!isvfat) { 816 if (opts->dotsOK) 817 seq_puts(m, ",dotsOK=yes"); 818 if (opts->nocase) 819 seq_puts(m, ",nocase"); 820 } else { 821 if (opts->utf8) 822 seq_puts(m, ",utf8"); 823 if (opts->unicode_xlate) 824 seq_puts(m, ",uni_xlate"); 825 if (!opts->numtail) 826 seq_puts(m, ",nonumtail"); 827 if (opts->rodir) 828 seq_puts(m, ",rodir"); 829 } 830 if (opts->flush) 831 seq_puts(m, ",flush"); 832 if (opts->tz_set) { 833 if (opts->time_offset) 834 seq_printf(m, ",time_offset=%d", opts->time_offset); 835 else 836 seq_puts(m, ",tz=UTC"); 837 } 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"); 842 else 843 seq_puts(m, ",errors=remount-ro"); 844 if (opts->nfs == FAT_NFS_NOSTALE_RO) 845 seq_puts(m, ",nfs=nostale_ro"); 846 else if (opts->nfs) 847 seq_puts(m, ",nfs=stale_rw"); 848 if (opts->discard) 849 seq_puts(m, ",discard"); 850 851 return 0; 852 } 853 854 enum { 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, 865 }; 866 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"}, 874 {Opt_uid, "uid=%u"}, 875 {Opt_gid, "gid=%u"}, 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"}, 908 {Opt_err, NULL}, 909 }; 910 static const match_table_t msdos_tokens = { 911 {Opt_nodots, "nodots"}, 912 {Opt_nodots, "dotsOK=no"}, 913 {Opt_dots, "dots"}, 914 {Opt_dots, "dotsOK=yes"}, 915 {Opt_err, NULL} 916 }; 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"}, 945 {Opt_err, NULL} 946 }; 947 948 static int parse_options(struct super_block *sb, char *options, int is_vfat, 949 int silent, int *debug, struct fat_mount_options *opts) 950 { 951 char *p; 952 substring_t args[MAX_OPT_ARGS]; 953 int option; 954 char *iocharset; 955 956 opts->isvfat = is_vfat; 957 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; 964 if (is_vfat) { 965 opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95; 966 opts->rodir = 0; 967 } else { 968 opts->shortname = 0; 969 opts->rodir = 1; 970 } 971 opts->name_check = 'n'; 972 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; 973 opts->utf8 = opts->unicode_xlate = 0; 974 opts->numtail = 1; 975 opts->usefree = opts->nocase = 0; 976 opts->tz_set = 0; 977 opts->nfs = 0; 978 opts->errors = FAT_ERRORS_RO; 979 *debug = 0; 980 981 if (!options) 982 goto out; 983 984 while ((p = strsep(&options, ",")) != NULL) { 985 int token; 986 if (!*p) 987 continue; 988 989 token = match_token(p, fat_tokens, args); 990 if (token == Opt_err) { 991 if (is_vfat) 992 token = match_token(p, vfat_tokens, args); 993 else 994 token = match_token(p, msdos_tokens, args); 995 } 996 switch (token) { 997 case Opt_check_s: 998 opts->name_check = 's'; 999 break; 1000 case Opt_check_r: 1001 opts->name_check = 'r'; 1002 break; 1003 case Opt_check_n: 1004 opts->name_check = 'n'; 1005 break; 1006 case Opt_usefree: 1007 opts->usefree = 1; 1008 break; 1009 case Opt_nocase: 1010 if (!is_vfat) 1011 opts->nocase = 1; 1012 else { 1013 /* for backward compatibility */ 1014 opts->shortname = VFAT_SFN_DISPLAY_WIN95 1015 | VFAT_SFN_CREATE_WIN95; 1016 } 1017 break; 1018 case Opt_quiet: 1019 opts->quiet = 1; 1020 break; 1021 case Opt_showexec: 1022 opts->showexec = 1; 1023 break; 1024 case Opt_debug: 1025 *debug = 1; 1026 break; 1027 case Opt_immutable: 1028 opts->sys_immutable = 1; 1029 break; 1030 case Opt_uid: 1031 if (match_int(&args[0], &option)) 1032 return -EINVAL; 1033 opts->fs_uid = make_kuid(current_user_ns(), option); 1034 if (!uid_valid(opts->fs_uid)) 1035 return -EINVAL; 1036 break; 1037 case Opt_gid: 1038 if (match_int(&args[0], &option)) 1039 return -EINVAL; 1040 opts->fs_gid = make_kgid(current_user_ns(), option); 1041 if (!gid_valid(opts->fs_gid)) 1042 return -EINVAL; 1043 break; 1044 case Opt_umask: 1045 if (match_octal(&args[0], &option)) 1046 return -EINVAL; 1047 opts->fs_fmask = opts->fs_dmask = option; 1048 break; 1049 case Opt_dmask: 1050 if (match_octal(&args[0], &option)) 1051 return -EINVAL; 1052 opts->fs_dmask = option; 1053 break; 1054 case Opt_fmask: 1055 if (match_octal(&args[0], &option)) 1056 return -EINVAL; 1057 opts->fs_fmask = option; 1058 break; 1059 case Opt_allow_utime: 1060 if (match_octal(&args[0], &option)) 1061 return -EINVAL; 1062 opts->allow_utime = option & (S_IWGRP | S_IWOTH); 1063 break; 1064 case Opt_codepage: 1065 if (match_int(&args[0], &option)) 1066 return -EINVAL; 1067 opts->codepage = option; 1068 break; 1069 case Opt_flush: 1070 opts->flush = 1; 1071 break; 1072 case Opt_time_offset: 1073 if (match_int(&args[0], &option)) 1074 return -EINVAL; 1075 if (option < -12 * 60 || option > 12 * 60) 1076 return -EINVAL; 1077 opts->tz_set = 1; 1078 opts->time_offset = option; 1079 break; 1080 case Opt_tz_utc: 1081 opts->tz_set = 1; 1082 opts->time_offset = 0; 1083 break; 1084 case Opt_err_cont: 1085 opts->errors = FAT_ERRORS_CONT; 1086 break; 1087 case Opt_err_panic: 1088 opts->errors = FAT_ERRORS_PANIC; 1089 break; 1090 case Opt_err_ro: 1091 opts->errors = FAT_ERRORS_RO; 1092 break; 1093 case Opt_nfs_stale_rw: 1094 opts->nfs = FAT_NFS_STALE_RW; 1095 break; 1096 case Opt_nfs_nostale_ro: 1097 opts->nfs = FAT_NFS_NOSTALE_RO; 1098 break; 1099 1100 /* msdos specific */ 1101 case Opt_dots: 1102 opts->dotsOK = 1; 1103 break; 1104 case Opt_nodots: 1105 opts->dotsOK = 0; 1106 break; 1107 1108 /* vfat specific */ 1109 case Opt_charset: 1110 if (opts->iocharset != fat_default_iocharset) 1111 kfree(opts->iocharset); 1112 iocharset = match_strdup(&args[0]); 1113 if (!iocharset) 1114 return -ENOMEM; 1115 opts->iocharset = iocharset; 1116 break; 1117 case Opt_shortname_lower: 1118 opts->shortname = VFAT_SFN_DISPLAY_LOWER 1119 | VFAT_SFN_CREATE_WIN95; 1120 break; 1121 case Opt_shortname_win95: 1122 opts->shortname = VFAT_SFN_DISPLAY_WIN95 1123 | VFAT_SFN_CREATE_WIN95; 1124 break; 1125 case Opt_shortname_winnt: 1126 opts->shortname = VFAT_SFN_DISPLAY_WINNT 1127 | VFAT_SFN_CREATE_WINNT; 1128 break; 1129 case Opt_shortname_mixed: 1130 opts->shortname = VFAT_SFN_DISPLAY_WINNT 1131 | VFAT_SFN_CREATE_WIN95; 1132 break; 1133 case Opt_utf8_no: /* 0 or no or false */ 1134 opts->utf8 = 0; 1135 break; 1136 case Opt_utf8_yes: /* empty or 1 or yes or true */ 1137 opts->utf8 = 1; 1138 break; 1139 case Opt_uni_xl_no: /* 0 or no or false */ 1140 opts->unicode_xlate = 0; 1141 break; 1142 case Opt_uni_xl_yes: /* empty or 1 or yes or true */ 1143 opts->unicode_xlate = 1; 1144 break; 1145 case Opt_nonumtail_no: /* 0 or no or false */ 1146 opts->numtail = 1; /* negated option */ 1147 break; 1148 case Opt_nonumtail_yes: /* empty or 1 or yes or true */ 1149 opts->numtail = 0; /* negated option */ 1150 break; 1151 case Opt_rodir: 1152 opts->rodir = 1; 1153 break; 1154 case Opt_discard: 1155 opts->discard = 1; 1156 break; 1157 1158 /* obsolete mount options */ 1159 case Opt_obsolete: 1160 fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, " 1161 "not supported now", p); 1162 break; 1163 /* unknown option */ 1164 default: 1165 if (!silent) { 1166 fat_msg(sb, KERN_ERR, 1167 "Unrecognized mount option \"%s\" " 1168 "or missing value", p); 1169 } 1170 return -EINVAL; 1171 } 1172 } 1173 1174 out: 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 " 1179 "case sensitive!"); 1180 } 1181 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) 1186 opts->utf8 = 0; 1187 if (opts->nfs == FAT_NFS_NOSTALE_RO) { 1188 sb->s_flags |= MS_RDONLY; 1189 sb->s_export_op = &fat_export_ops_nostale; 1190 } 1191 1192 return 0; 1193 } 1194 1195 static int fat_read_root(struct inode *inode) 1196 { 1197 struct super_block *sb = inode->i_sb; 1198 struct msdos_sb_info *sbi = MSDOS_SB(sb); 1199 int error; 1200 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; 1204 inode->i_version++; 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); 1212 if (error < 0) 1213 return error; 1214 } else { 1215 MSDOS_I(inode)->i_start = 0; 1216 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); 1217 } 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; 1222 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); 1227 1228 return 0; 1229 } 1230 1231 /* 1232 * Read the super block of an MS-DOS FS. 1233 */ 1234 int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat, 1235 void (*setup)(struct super_block *)) 1236 { 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; 1244 int debug; 1245 unsigned int media; 1246 long error; 1247 char buf[50]; 1248 1249 /* 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! 1254 */ 1255 sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); 1256 if (!sbi) 1257 return -ENOMEM; 1258 sb->s_fs_info = sbi; 1259 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); 1267 1268 error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options); 1269 if (error) 1270 goto out_fail; 1271 1272 setup(sb); /* flavour-specific stuff that needs options */ 1273 1274 error = -EIO; 1275 sb_min_blocksize(sb, 512); 1276 bh = sb_bread(sb, 0); 1277 if (bh == NULL) { 1278 fat_msg(sb, KERN_ERR, "unable to read boot sector"); 1279 goto out_fail; 1280 } 1281 1282 b = (struct fat_boot_sector *) bh->b_data; 1283 if (!b->reserved) { 1284 if (!silent) 1285 fat_msg(sb, KERN_ERR, "bogus number of reserved sectors"); 1286 brelse(bh); 1287 goto out_invalid; 1288 } 1289 if (!b->fats) { 1290 if (!silent) 1291 fat_msg(sb, KERN_ERR, "bogus number of FAT structure"); 1292 brelse(bh); 1293 goto out_invalid; 1294 } 1295 1296 /* 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. 1299 */ 1300 1301 media = b->media; 1302 if (!fat_valid_media(media)) { 1303 if (!silent) 1304 fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)", 1305 media); 1306 brelse(bh); 1307 goto out_invalid; 1308 } 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)) { 1313 if (!silent) 1314 fat_msg(sb, KERN_ERR, "bogus logical sector size %u", 1315 logical_sector_size); 1316 brelse(bh); 1317 goto out_invalid; 1318 } 1319 sbi->sec_per_clus = b->sec_per_clus; 1320 if (!is_power_of_2(sbi->sec_per_clus)) { 1321 if (!silent) 1322 fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u", 1323 sbi->sec_per_clus); 1324 brelse(bh); 1325 goto out_invalid; 1326 } 1327 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); 1331 brelse(bh); 1332 goto out_fail; 1333 } 1334 if (logical_sector_size > sb->s_blocksize) { 1335 brelse(bh); 1336 1337 if (!sb_set_blocksize(sb, logical_sector_size)) { 1338 fat_msg(sb, KERN_ERR, "unable to set blocksize %u", 1339 logical_sector_size); 1340 goto out_fail; 1341 } 1342 bh = sb_bread(sb, 0); 1343 if (bh == NULL) { 1344 fat_msg(sb, KERN_ERR, "unable to read boot sector" 1345 " (logical sector size = %lu)", 1346 sb->s_blocksize); 1347 goto out_fail; 1348 } 1349 b = (struct fat_boot_sector *) bh->b_data; 1350 } 1351 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; 1364 1365 if (!sbi->fat_length && b->fat32.length) { 1366 struct fat_boot_fsinfo *fsinfo; 1367 struct buffer_head *fsinfo_bh; 1368 1369 /* Must be FAT32 */ 1370 sbi->fat_bits = 32; 1371 sbi->fat_length = le32_to_cpu(b->fat32.length); 1372 sbi->root_cluster = le32_to_cpu(b->fat32.root_cluster); 1373 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; 1378 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); 1383 brelse(bh); 1384 goto out_fail; 1385 } 1386 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); 1394 } else { 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); 1399 } 1400 1401 brelse(fsinfo_bh); 1402 } 1403 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; 1406 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)) { 1410 if (!silent) 1411 fat_msg(sb, KERN_ERR, "bogus directory-entries per block" 1412 " (%u)", sbi->dir_entries); 1413 brelse(bh); 1414 goto out_invalid; 1415 } 1416 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); 1423 1424 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; 1425 1426 if (sbi->fat_bits != 32) 1427 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; 1428 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; 1434 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)) { 1439 if (!silent) 1440 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)", 1441 total_clusters); 1442 brelse(bh); 1443 goto out_invalid; 1444 } 1445 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; 1454 1455 brelse(bh); 1456 1457 /* set up enough so that it can read an inode */ 1458 fat_hash_init(sb); 1459 dir_hash_init(sb); 1460 fat_ent_access_init(sb); 1461 1462 /* 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. 1466 * 1467 * if (FAT_FIRST_ENT(sb, media) != first) 1468 */ 1469 1470 error = -EINVAL; 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); 1475 goto out_fail; 1476 } 1477 1478 /* FIXME: utf8 is using iocharset for upper/lower conversion */ 1479 if (sbi->options.isvfat) { 1480 sbi->nls_io = load_nls(sbi->options.iocharset); 1481 if (!sbi->nls_io) { 1482 fat_msg(sb, KERN_ERR, "IO charset %s not found", 1483 sbi->options.iocharset); 1484 goto out_fail; 1485 } 1486 } 1487 1488 error = -ENOMEM; 1489 fat_inode = new_inode(sb); 1490 if (!fat_inode) 1491 goto out_fail; 1492 MSDOS_I(fat_inode)->i_pos = 0; 1493 sbi->fat_inode = fat_inode; 1494 1495 fsinfo_inode = new_inode(sb); 1496 if (!fsinfo_inode) 1497 goto out_fail; 1498 fsinfo_inode->i_ino = MSDOS_FSINFO_INO; 1499 sbi->fsinfo_inode = fsinfo_inode; 1500 insert_inode_hash(fsinfo_inode); 1501 1502 root_inode = new_inode(sb); 1503 if (!root_inode) 1504 goto out_fail; 1505 root_inode->i_ino = MSDOS_ROOT_INO; 1506 root_inode->i_version = 1; 1507 error = fat_read_root(root_inode); 1508 if (error < 0) { 1509 iput(root_inode); 1510 goto out_fail; 1511 } 1512 error = -ENOMEM; 1513 insert_inode_hash(root_inode); 1514 fat_attach(root_inode, 0); 1515 sb->s_root = d_make_root(root_inode); 1516 if (!sb->s_root) { 1517 fat_msg(sb, KERN_ERR, "get root inode failed"); 1518 goto out_fail; 1519 } 1520 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"); 1527 } 1528 1529 fat_set_state(sb, 1, 0); 1530 return 0; 1531 1532 out_invalid: 1533 error = -EINVAL; 1534 if (!silent) 1535 fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem"); 1536 1537 out_fail: 1538 if (fsinfo_inode) 1539 iput(fsinfo_inode); 1540 if (fat_inode) 1541 iput(fat_inode); 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; 1547 kfree(sbi); 1548 return error; 1549 } 1550 1551 EXPORT_SYMBOL_GPL(fat_fill_super); 1552 1553 /* 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 1557 * during the call 1558 */ 1559 static int writeback_inode(struct inode *inode) 1560 { 1561 1562 int ret; 1563 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 1567 */ 1568 ret = sync_inode_metadata(inode, 0); 1569 if (!ret) 1570 ret = filemap_fdatawrite(inode->i_mapping); 1571 return ret; 1572 } 1573 1574 /* 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. 1577 * 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 1580 * io over again 1581 */ 1582 int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2) 1583 { 1584 int ret = 0; 1585 if (!MSDOS_SB(sb)->options.flush) 1586 return 0; 1587 if (i1) 1588 ret = writeback_inode(i1); 1589 if (!ret && i2) 1590 ret = writeback_inode(i2); 1591 if (!ret) { 1592 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping; 1593 ret = filemap_flush(mapping); 1594 } 1595 return ret; 1596 } 1597 EXPORT_SYMBOL_GPL(fat_flush_inodes); 1598 1599 static int __init init_fat_fs(void) 1600 { 1601 int err; 1602 1603 err = fat_cache_init(); 1604 if (err) 1605 return err; 1606 1607 err = fat_init_inodecache(); 1608 if (err) 1609 goto failed; 1610 1611 return 0; 1612 1613 failed: 1614 fat_cache_destroy(); 1615 return err; 1616 } 1617 1618 static void __exit exit_fat_fs(void) 1619 { 1620 fat_cache_destroy(); 1621 fat_destroy_inodecache(); 1622 } 1623 1624 module_init(init_fat_fs) 1625 module_exit(exit_fat_fs) 1626 1627 MODULE_LICENSE("GPL"); 1628