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