109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/fat/inode.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Written 1992,1993 by Werner Almesberger 61da177e4SLinus Torvalds * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner 71da177e4SLinus Torvalds * Rewritten for the constant inumbers support by Al Viro 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Fixes: 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 16a5425d29SOGAWA Hirofumi #include <linux/mpage.h> 171da177e4SLinus Torvalds #include <linux/vfs.h> 1858932ef7SAlexander Kuleshov #include <linux/seq_file.h> 19e5174baaSOGAWA Hirofumi #include <linux/uio.h> 20f562146aSNamjae Jeon #include <linux/blkdev.h> 2166114cadSTejun Heo #include <linux/backing-dev.h> 221da177e4SLinus Torvalds #include <asm/unaligned.h> 2374b5cab6SArnd Bergmann #include <linux/random.h> 242489dbabSJeff Layton #include <linux/iversion.h> 259e975daeSOGAWA Hirofumi #include "fat.h" 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #ifndef CONFIG_FAT_DEFAULT_IOCHARSET 281da177e4SLinus Torvalds /* if user don't select VFAT, this is undefined. */ 291da177e4SLinus Torvalds #define CONFIG_FAT_DEFAULT_IOCHARSET "" 301da177e4SLinus Torvalds #endif 311da177e4SLinus Torvalds 32190a8843SConrad Meyer #define KB_IN_SECTORS 2 33190a8843SConrad Meyer 34c0da64f6SDeepa Dinamani /* DOS dates from 1980/1/1 through 2107/12/31 */ 35c0da64f6SDeepa Dinamani #define FAT_DATE_MIN (0<<9 | 1<<5 | 1) 36c0da64f6SDeepa Dinamani #define FAT_DATE_MAX (127<<9 | 12<<5 | 31) 37c0da64f6SDeepa Dinamani #define FAT_TIME_MAX (23<<11 | 59<<5 | 29) 38c0da64f6SDeepa Dinamani 39190a8843SConrad Meyer /* 40190a8843SConrad Meyer * A deserialized copy of the on-disk structure laid out in struct 41190a8843SConrad Meyer * fat_boot_sector. 42190a8843SConrad Meyer */ 43190a8843SConrad Meyer struct fat_bios_param_block { 44190a8843SConrad Meyer u16 fat_sector_size; 45190a8843SConrad Meyer u8 fat_sec_per_clus; 46190a8843SConrad Meyer u16 fat_reserved; 47190a8843SConrad Meyer u8 fat_fats; 48190a8843SConrad Meyer u16 fat_dir_entries; 49190a8843SConrad Meyer u16 fat_sectors; 50190a8843SConrad Meyer u16 fat_fat_length; 51190a8843SConrad Meyer u32 fat_total_sect; 52190a8843SConrad Meyer 53190a8843SConrad Meyer u8 fat16_state; 54190a8843SConrad Meyer u32 fat16_vol_id; 55190a8843SConrad Meyer 56190a8843SConrad Meyer u32 fat32_length; 57190a8843SConrad Meyer u32 fat32_root_cluster; 58190a8843SConrad Meyer u16 fat32_info_sector; 59190a8843SConrad Meyer u8 fat32_state; 60190a8843SConrad Meyer u32 fat32_vol_id; 61190a8843SConrad Meyer }; 62190a8843SConrad Meyer 631da177e4SLinus Torvalds static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; 641da177e4SLinus Torvalds static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; 651da177e4SLinus Torvalds 66190a8843SConrad Meyer static struct fat_floppy_defaults { 67190a8843SConrad Meyer unsigned nr_sectors; 68190a8843SConrad Meyer unsigned sec_per_clus; 69190a8843SConrad Meyer unsigned dir_entries; 70190a8843SConrad Meyer unsigned media; 71190a8843SConrad Meyer unsigned fat_length; 72190a8843SConrad Meyer } floppy_defaults[] = { 73190a8843SConrad Meyer { 74190a8843SConrad Meyer .nr_sectors = 160 * KB_IN_SECTORS, 75190a8843SConrad Meyer .sec_per_clus = 1, 76190a8843SConrad Meyer .dir_entries = 64, 77190a8843SConrad Meyer .media = 0xFE, 78190a8843SConrad Meyer .fat_length = 1, 79190a8843SConrad Meyer }, 80190a8843SConrad Meyer { 81190a8843SConrad Meyer .nr_sectors = 180 * KB_IN_SECTORS, 82190a8843SConrad Meyer .sec_per_clus = 1, 83190a8843SConrad Meyer .dir_entries = 64, 84190a8843SConrad Meyer .media = 0xFC, 85190a8843SConrad Meyer .fat_length = 2, 86190a8843SConrad Meyer }, 87190a8843SConrad Meyer { 88190a8843SConrad Meyer .nr_sectors = 320 * KB_IN_SECTORS, 89190a8843SConrad Meyer .sec_per_clus = 2, 90190a8843SConrad Meyer .dir_entries = 112, 91190a8843SConrad Meyer .media = 0xFF, 92190a8843SConrad Meyer .fat_length = 1, 93190a8843SConrad Meyer }, 94190a8843SConrad Meyer { 95190a8843SConrad Meyer .nr_sectors = 360 * KB_IN_SECTORS, 96190a8843SConrad Meyer .sec_per_clus = 2, 97190a8843SConrad Meyer .dir_entries = 112, 98190a8843SConrad Meyer .media = 0xFD, 99190a8843SConrad Meyer .fat_length = 2, 100190a8843SConrad Meyer }, 101190a8843SConrad Meyer }; 1021da177e4SLinus Torvalds 103b13bb33eSNamjae Jeon int fat_add_cluster(struct inode *inode) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds int err, cluster; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds err = fat_alloc_clusters(inode, &cluster, 1); 1081da177e4SLinus Torvalds if (err) 1091da177e4SLinus Torvalds return err; 1101da177e4SLinus Torvalds /* FIXME: this cluster should be added after data of this 1111da177e4SLinus Torvalds * cluster is writed */ 1121da177e4SLinus Torvalds err = fat_chain_add(inode, cluster, 1); 1131da177e4SLinus Torvalds if (err) 1141da177e4SLinus Torvalds fat_free_clusters(inode, cluster); 1151da177e4SLinus Torvalds return err; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1186a1d9805SOGAWA Hirofumi static inline int __fat_get_block(struct inode *inode, sector_t iblock, 119e5174baaSOGAWA Hirofumi unsigned long *max_blocks, 1201da177e4SLinus Torvalds struct buffer_head *bh_result, int create) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 123e5174baaSOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 124e5174baaSOGAWA Hirofumi unsigned long mapped_blocks; 1257e0f236bSNamjae Jeon sector_t phys, last_block; 126e5174baaSOGAWA Hirofumi int err, offset; 1271da177e4SLinus Torvalds 12816fab201SNamjae Jeon err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); 1291da177e4SLinus Torvalds if (err) 1301da177e4SLinus Torvalds return err; 1311da177e4SLinus Torvalds if (phys) { 1321da177e4SLinus Torvalds map_bh(bh_result, sb, phys); 133e5174baaSOGAWA Hirofumi *max_blocks = min(mapped_blocks, *max_blocks); 1341da177e4SLinus Torvalds return 0; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds if (!create) 1371da177e4SLinus Torvalds return 0; 138e5174baaSOGAWA Hirofumi 1391da177e4SLinus Torvalds if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { 14085c78591SDenis Karpov fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)", 1411da177e4SLinus Torvalds MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); 1421da177e4SLinus Torvalds return -EIO; 1431da177e4SLinus Torvalds } 144e5174baaSOGAWA Hirofumi 1457e0f236bSNamjae Jeon last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9); 146e5174baaSOGAWA Hirofumi offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); 1477e0f236bSNamjae Jeon /* 1487e0f236bSNamjae Jeon * allocate a cluster according to the following. 1497e0f236bSNamjae Jeon * 1) no more available blocks 1507e0f236bSNamjae Jeon * 2) not part of fallocate region 1517e0f236bSNamjae Jeon */ 1527e0f236bSNamjae Jeon if (!offset && !(iblock < last_block)) { 153e5174baaSOGAWA Hirofumi /* TODO: multiple cluster allocation would be desirable. */ 1541da177e4SLinus Torvalds err = fat_add_cluster(inode); 1551da177e4SLinus Torvalds if (err) 1561da177e4SLinus Torvalds return err; 1571da177e4SLinus Torvalds } 158e5174baaSOGAWA Hirofumi /* available blocks on this cluster */ 159e5174baaSOGAWA Hirofumi mapped_blocks = sbi->sec_per_clus - offset; 160e5174baaSOGAWA Hirofumi 161e5174baaSOGAWA Hirofumi *max_blocks = min(mapped_blocks, *max_blocks); 162e5174baaSOGAWA Hirofumi MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; 163e5174baaSOGAWA Hirofumi 16416fab201SNamjae Jeon err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); 1651da177e4SLinus Torvalds if (err) 1661da177e4SLinus Torvalds return err; 167c2574aaaSOGAWA Hirofumi if (!phys) { 168c2574aaaSOGAWA Hirofumi fat_fs_error(sb, 169c2574aaaSOGAWA Hirofumi "invalid FAT chain (i_pos %lld, last_block %llu)", 170c2574aaaSOGAWA Hirofumi MSDOS_I(inode)->i_pos, 171c2574aaaSOGAWA Hirofumi (unsigned long long)last_block); 172c2574aaaSOGAWA Hirofumi return -EIO; 173c2574aaaSOGAWA Hirofumi } 1746a1d9805SOGAWA Hirofumi 175e5174baaSOGAWA Hirofumi BUG_ON(*max_blocks != mapped_blocks); 1761da177e4SLinus Torvalds set_buffer_new(bh_result); 1771da177e4SLinus Torvalds map_bh(bh_result, sb, phys); 1781da177e4SLinus Torvalds 179e5174baaSOGAWA Hirofumi return 0; 180e5174baaSOGAWA Hirofumi } 181e5174baaSOGAWA Hirofumi 182e5174baaSOGAWA Hirofumi static int fat_get_block(struct inode *inode, sector_t iblock, 183e5174baaSOGAWA Hirofumi struct buffer_head *bh_result, int create) 184e5174baaSOGAWA Hirofumi { 1856a1d9805SOGAWA Hirofumi struct super_block *sb = inode->i_sb; 1866a1d9805SOGAWA Hirofumi unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 1876a1d9805SOGAWA Hirofumi int err; 1886a1d9805SOGAWA Hirofumi 1896a1d9805SOGAWA Hirofumi err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); 1906a1d9805SOGAWA Hirofumi if (err) 1916a1d9805SOGAWA Hirofumi return err; 1926a1d9805SOGAWA Hirofumi bh_result->b_size = max_blocks << sb->s_blocksize_bits; 1936a1d9805SOGAWA Hirofumi return 0; 194e5174baaSOGAWA Hirofumi } 195e5174baaSOGAWA Hirofumi 196a5425d29SOGAWA Hirofumi static int fat_writepages(struct address_space *mapping, 197a5425d29SOGAWA Hirofumi struct writeback_control *wbc) 198a5425d29SOGAWA Hirofumi { 199a5425d29SOGAWA Hirofumi return mpage_writepages(mapping, wbc, fat_get_block); 200a5425d29SOGAWA Hirofumi } 201a5425d29SOGAWA Hirofumi 202f132ab7dSMatthew Wilcox (Oracle) static int fat_read_folio(struct file *file, struct folio *folio) 2031da177e4SLinus Torvalds { 204f132ab7dSMatthew Wilcox (Oracle) return mpage_read_folio(folio, fat_get_block); 205a5425d29SOGAWA Hirofumi } 206a5425d29SOGAWA Hirofumi 207d4388340SMatthew Wilcox (Oracle) static void fat_readahead(struct readahead_control *rac) 208a5425d29SOGAWA Hirofumi { 209d4388340SMatthew Wilcox (Oracle) mpage_readahead(rac, fat_get_block); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 212459f6ed3Snpiggin@suse.de static void fat_write_failed(struct address_space *mapping, loff_t to) 213459f6ed3Snpiggin@suse.de { 214459f6ed3Snpiggin@suse.de struct inode *inode = mapping->host; 215459f6ed3Snpiggin@suse.de 216459f6ed3Snpiggin@suse.de if (to > inode->i_size) { 2177caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 218459f6ed3Snpiggin@suse.de fat_truncate_blocks(inode, inode->i_size); 219459f6ed3Snpiggin@suse.de } 220459f6ed3Snpiggin@suse.de } 221459f6ed3Snpiggin@suse.de 222d7777a25SNick Piggin static int fat_write_begin(struct file *file, struct address_space *mapping, 2239d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 224*1da86618SMatthew Wilcox (Oracle) struct folio **foliop, void **fsdata) 2251da177e4SLinus Torvalds { 226459f6ed3Snpiggin@suse.de int err; 227459f6ed3Snpiggin@suse.de 228be3bbbc5SMatthew Wilcox (Oracle) err = cont_write_begin(file, mapping, pos, len, 229*1da86618SMatthew Wilcox (Oracle) foliop, fsdata, fat_get_block, 230d7777a25SNick Piggin &MSDOS_I(mapping->host)->mmu_private); 231459f6ed3Snpiggin@suse.de if (err < 0) 232459f6ed3Snpiggin@suse.de fat_write_failed(mapping, pos + len); 233459f6ed3Snpiggin@suse.de return err; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 236d7777a25SNick Piggin static int fat_write_end(struct file *file, struct address_space *mapping, 237d7777a25SNick Piggin loff_t pos, unsigned len, unsigned copied, 238a225800fSMatthew Wilcox (Oracle) struct folio *folio, void *fsdata) 239ef402268SOGAWA Hirofumi { 240d7777a25SNick Piggin struct inode *inode = mapping->host; 241d7777a25SNick Piggin int err; 242a225800fSMatthew Wilcox (Oracle) err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata); 243459f6ed3Snpiggin@suse.de if (err < len) 244459f6ed3Snpiggin@suse.de fat_write_failed(mapping, pos + len); 245d7777a25SNick Piggin if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { 246cd83f6b1SFrank Sorenson fat_truncate_time(inode, NULL, S_CTIME|S_MTIME); 247ef402268SOGAWA Hirofumi MSDOS_I(inode)->i_attrs |= ATTR_ARCH; 248ef402268SOGAWA Hirofumi mark_inode_dirty(inode); 249ef402268SOGAWA Hirofumi } 250ef402268SOGAWA Hirofumi return err; 251ef402268SOGAWA Hirofumi } 252ef402268SOGAWA Hirofumi 253c8b8e32dSChristoph Hellwig static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 254e5174baaSOGAWA Hirofumi { 255e5174baaSOGAWA Hirofumi struct file *file = iocb->ki_filp; 256459f6ed3Snpiggin@suse.de struct address_space *mapping = file->f_mapping; 257459f6ed3Snpiggin@suse.de struct inode *inode = mapping->host; 258a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 259c8b8e32dSChristoph Hellwig loff_t offset = iocb->ki_pos; 260459f6ed3Snpiggin@suse.de ssize_t ret; 261e5174baaSOGAWA Hirofumi 2626f673763SOmar Sandoval if (iov_iter_rw(iter) == WRITE) { 263e5174baaSOGAWA Hirofumi /* 2644e02ed4bSNick Piggin * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 265e5174baaSOGAWA Hirofumi * so we need to update the ->mmu_private to block boundary. 266e5174baaSOGAWA Hirofumi * 267e5174baaSOGAWA Hirofumi * But we must fill the remaining area or hole by nul for 268e5174baaSOGAWA Hirofumi * updating ->mmu_private. 26994412a96SOGAWA Hirofumi * 27094412a96SOGAWA Hirofumi * Return 0, and fallback to normal buffered write. 271e5174baaSOGAWA Hirofumi */ 272a6cbcd4aSAl Viro loff_t size = offset + count; 273e5174baaSOGAWA Hirofumi if (MSDOS_I(inode)->mmu_private < size) 27494412a96SOGAWA Hirofumi return 0; 275e5174baaSOGAWA Hirofumi } 276e5174baaSOGAWA Hirofumi 277e5174baaSOGAWA Hirofumi /* 278e5174baaSOGAWA Hirofumi * FAT need to use the DIO_LOCKING for avoiding the race 279e5174baaSOGAWA Hirofumi * condition of fat_get_block() and ->truncate(). 280e5174baaSOGAWA Hirofumi */ 281c8b8e32dSChristoph Hellwig ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block); 2826f673763SOmar Sandoval if (ret < 0 && iov_iter_rw(iter) == WRITE) 283a6cbcd4aSAl Viro fat_write_failed(mapping, offset + count); 284459f6ed3Snpiggin@suse.de 285459f6ed3Snpiggin@suse.de return ret; 286e5174baaSOGAWA Hirofumi } 287e5174baaSOGAWA Hirofumi 28816fab201SNamjae Jeon static int fat_get_block_bmap(struct inode *inode, sector_t iblock, 28916fab201SNamjae Jeon struct buffer_head *bh_result, int create) 29016fab201SNamjae Jeon { 29116fab201SNamjae Jeon struct super_block *sb = inode->i_sb; 29216fab201SNamjae Jeon unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 29316fab201SNamjae Jeon int err; 29416fab201SNamjae Jeon sector_t bmap; 29516fab201SNamjae Jeon unsigned long mapped_blocks; 29616fab201SNamjae Jeon 29716fab201SNamjae Jeon BUG_ON(create != 0); 29816fab201SNamjae Jeon 29916fab201SNamjae Jeon err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true); 30016fab201SNamjae Jeon if (err) 30116fab201SNamjae Jeon return err; 30216fab201SNamjae Jeon 30316fab201SNamjae Jeon if (bmap) { 30416fab201SNamjae Jeon map_bh(bh_result, sb, bmap); 30516fab201SNamjae Jeon max_blocks = min(mapped_blocks, max_blocks); 30616fab201SNamjae Jeon } 30716fab201SNamjae Jeon 30816fab201SNamjae Jeon bh_result->b_size = max_blocks << sb->s_blocksize_bits; 30916fab201SNamjae Jeon 31016fab201SNamjae Jeon return 0; 31116fab201SNamjae Jeon } 31216fab201SNamjae Jeon 3131da177e4SLinus Torvalds static sector_t _fat_bmap(struct address_space *mapping, sector_t block) 3141da177e4SLinus Torvalds { 315fa93ca18SOGAWA Hirofumi sector_t blocknr; 316fa93ca18SOGAWA Hirofumi 317fa93ca18SOGAWA Hirofumi /* fat_get_cluster() assumes the requested blocknr isn't truncated. */ 31858268691SChristoph Hellwig down_read(&MSDOS_I(mapping->host)->truncate_lock); 31916fab201SNamjae Jeon blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap); 32058268691SChristoph Hellwig up_read(&MSDOS_I(mapping->host)->truncate_lock); 321fa93ca18SOGAWA Hirofumi 322fa93ca18SOGAWA Hirofumi return blocknr; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 325c0ef0cc9SNamjae Jeon /* 326c0ef0cc9SNamjae Jeon * fat_block_truncate_page() zeroes out a mapping from file offset `from' 327c0ef0cc9SNamjae Jeon * up to the end of the block which corresponds to `from'. 328c0ef0cc9SNamjae Jeon * This is required during truncate to physically zeroout the tail end 329c0ef0cc9SNamjae Jeon * of that block so it doesn't yield old data if the file is later grown. 330c0ef0cc9SNamjae Jeon * Also, avoid causing failure from fsx for cases of "data past EOF" 331c0ef0cc9SNamjae Jeon */ 332c0ef0cc9SNamjae Jeon int fat_block_truncate_page(struct inode *inode, loff_t from) 333c0ef0cc9SNamjae Jeon { 334c0ef0cc9SNamjae Jeon return block_truncate_page(inode->i_mapping, from, fat_get_block); 335c0ef0cc9SNamjae Jeon } 336c0ef0cc9SNamjae Jeon 337f5e54d6eSChristoph Hellwig static const struct address_space_operations fat_aops = { 338e621900aSMatthew Wilcox (Oracle) .dirty_folio = block_dirty_folio, 3397ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = block_invalidate_folio, 340f132ab7dSMatthew Wilcox (Oracle) .read_folio = fat_read_folio, 341d4388340SMatthew Wilcox (Oracle) .readahead = fat_readahead, 342a5425d29SOGAWA Hirofumi .writepages = fat_writepages, 343d7777a25SNick Piggin .write_begin = fat_write_begin, 344d7777a25SNick Piggin .write_end = fat_write_end, 345e5174baaSOGAWA Hirofumi .direct_IO = fat_direct_IO, 346ee649af0SChristoph Hellwig .bmap = _fat_bmap, 347ee649af0SChristoph Hellwig .migrate_folio = buffer_migrate_folio, 3481da177e4SLinus Torvalds }; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* 3511da177e4SLinus Torvalds * New FAT inode stuff. We do the following: 3521da177e4SLinus Torvalds * a) i_ino is constant and has nothing with on-disk location. 3531da177e4SLinus Torvalds * b) FAT manages its own cache of directory entries. 3541da177e4SLinus Torvalds * c) *This* cache is indexed by on-disk location. 3551da177e4SLinus Torvalds * d) inode has an associated directory entry, all right, but 3561da177e4SLinus Torvalds * it may be unhashed. 3571da177e4SLinus Torvalds * e) currently entries are stored within struct inode. That should 3581da177e4SLinus Torvalds * change. 3591da177e4SLinus Torvalds * f) we deal with races in the following way: 3601da177e4SLinus Torvalds * 1. readdir() and lookup() do FAT-dir-cache lookup. 3611da177e4SLinus Torvalds * 2. rename() unhashes the F-d-c entry and rehashes it in 3621da177e4SLinus Torvalds * a new place. 3631da177e4SLinus Torvalds * 3. unlink() and rmdir() unhash F-d-c entry. 3641da177e4SLinus Torvalds * 4. fat_write_inode() checks whether the thing is unhashed. 3651da177e4SLinus Torvalds * If it is we silently return. If it isn't we do bread(), 3661da177e4SLinus Torvalds * check if the location is still valid and retry if it 3671da177e4SLinus Torvalds * isn't. Otherwise we do changes. 3681da177e4SLinus Torvalds * 5. Spinlock is used to protect hash/unhash/location check/lookup 369deee3ce4SAl Viro * 6. fat_evict_inode() unhashes the F-d-c entry. 3701da177e4SLinus Torvalds * 7. lookup() and readdir() do igrab() if they find a F-d-c entry 3711da177e4SLinus Torvalds * and consider negative result as cache miss. 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds static void fat_hash_init(struct super_block *sb) 3751da177e4SLinus Torvalds { 3761da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 3771da177e4SLinus Torvalds int i; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds spin_lock_init(&sbi->inode_hash_lock); 3801da177e4SLinus Torvalds for (i = 0; i < FAT_HASH_SIZE; i++) 3811da177e4SLinus Torvalds INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 384d3dfa822SOGAWA Hirofumi static inline unsigned long fat_hash(loff_t i_pos) 3851da177e4SLinus Torvalds { 386d3dfa822SOGAWA Hirofumi return hash_32(i_pos, FAT_HASH_BITS); 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3897669e8fbSSteven J. Magnani static void dir_hash_init(struct super_block *sb) 3907669e8fbSSteven J. Magnani { 3917669e8fbSSteven J. Magnani struct msdos_sb_info *sbi = MSDOS_SB(sb); 3927669e8fbSSteven J. Magnani int i; 3937669e8fbSSteven J. Magnani 3947669e8fbSSteven J. Magnani spin_lock_init(&sbi->dir_hash_lock); 3957669e8fbSSteven J. Magnani for (i = 0; i < FAT_HASH_SIZE; i++) 3967669e8fbSSteven J. Magnani INIT_HLIST_HEAD(&sbi->dir_hashtable[i]); 3977669e8fbSSteven J. Magnani } 3987669e8fbSSteven J. Magnani 3991da177e4SLinus Torvalds void fat_attach(struct inode *inode, loff_t i_pos) 4001da177e4SLinus Torvalds { 401d3dfa822SOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4027669e8fbSSteven J. Magnani 4037669e8fbSSteven J. Magnani if (inode->i_ino != MSDOS_ROOT_INO) { 4047669e8fbSSteven J. Magnani struct hlist_head *head = sbi->inode_hashtable 4057669e8fbSSteven J. Magnani + fat_hash(i_pos); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 4081da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = i_pos; 409d3dfa822SOGAWA Hirofumi hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head); 4101da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4111da177e4SLinus Torvalds } 4127669e8fbSSteven J. Magnani 4137669e8fbSSteven J. Magnani /* If NFS support is enabled, cache the mapping of start cluster 4147669e8fbSSteven J. Magnani * to directory inode. This is used during reconnection of 4157669e8fbSSteven J. Magnani * dentries to the filesystem root. 4167669e8fbSSteven J. Magnani */ 4177669e8fbSSteven J. Magnani if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { 4187669e8fbSSteven J. Magnani struct hlist_head *d_head = sbi->dir_hashtable; 4197669e8fbSSteven J. Magnani d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart); 4207669e8fbSSteven J. Magnani 4217669e8fbSSteven J. Magnani spin_lock(&sbi->dir_hash_lock); 4227669e8fbSSteven J. Magnani hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head); 4237669e8fbSSteven J. Magnani spin_unlock(&sbi->dir_hash_lock); 4247669e8fbSSteven J. Magnani } 4257669e8fbSSteven J. Magnani } 4267c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_attach); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds void fat_detach(struct inode *inode) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4311da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 4321da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = 0; 4331da177e4SLinus Torvalds hlist_del_init(&MSDOS_I(inode)->i_fat_hash); 4341da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4357669e8fbSSteven J. Magnani 4367669e8fbSSteven J. Magnani if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { 4377669e8fbSSteven J. Magnani spin_lock(&sbi->dir_hash_lock); 4387669e8fbSSteven J. Magnani hlist_del_init(&MSDOS_I(inode)->i_dir_hash); 4397669e8fbSSteven J. Magnani spin_unlock(&sbi->dir_hash_lock); 4407669e8fbSSteven J. Magnani } 4411da177e4SLinus Torvalds } 4427c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_detach); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds struct inode *fat_iget(struct super_block *sb, loff_t i_pos) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 447d3dfa822SOGAWA Hirofumi struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); 4481da177e4SLinus Torvalds struct msdos_inode_info *i; 4491da177e4SLinus Torvalds struct inode *inode = NULL; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 452b67bfe0dSSasha Levin hlist_for_each_entry(i, head, i_fat_hash) { 4531da177e4SLinus Torvalds BUG_ON(i->vfs_inode.i_sb != sb); 4541da177e4SLinus Torvalds if (i->i_pos != i_pos) 4551da177e4SLinus Torvalds continue; 4561da177e4SLinus Torvalds inode = igrab(&i->vfs_inode); 4571da177e4SLinus Torvalds if (inode) 4581da177e4SLinus Torvalds break; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4611da177e4SLinus Torvalds return inode; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds static int is_exec(unsigned char *extension) 4651da177e4SLinus Torvalds { 466ef19470eSManuel Schölling unsigned char exe_extensions[] = "EXECOMBAT", *walk; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds for (walk = exe_extensions; *walk; walk += 3) 4691da177e4SLinus Torvalds if (!strncmp(extension, walk, 3)) 4701da177e4SLinus Torvalds return 1; 4711da177e4SLinus Torvalds return 0; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds static int fat_calc_dir_size(struct inode *inode) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4771da177e4SLinus Torvalds int ret, fclus, dclus; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds inode->i_size = 0; 4801da177e4SLinus Torvalds if (MSDOS_I(inode)->i_start == 0) 4811da177e4SLinus Torvalds return 0; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); 4841da177e4SLinus Torvalds if (ret < 0) 4851da177e4SLinus Torvalds return ret; 4861da177e4SLinus Torvalds inode->i_size = (fclus + 1) << sbi->cluster_bits; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 491a3082d52SOGAWA Hirofumi static int fat_validate_dir(struct inode *dir) 492a3082d52SOGAWA Hirofumi { 493a3082d52SOGAWA Hirofumi struct super_block *sb = dir->i_sb; 494a3082d52SOGAWA Hirofumi 495a3082d52SOGAWA Hirofumi if (dir->i_nlink < 2) { 496a3082d52SOGAWA Hirofumi /* Directory should have "."/".." entries at least. */ 497a3082d52SOGAWA Hirofumi fat_fs_error(sb, "corrupted directory (invalid entries)"); 498a3082d52SOGAWA Hirofumi return -EIO; 499a3082d52SOGAWA Hirofumi } 500a3082d52SOGAWA Hirofumi if (MSDOS_I(dir)->i_start == 0 || 501a3082d52SOGAWA Hirofumi MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) { 502a3082d52SOGAWA Hirofumi /* Directory should point valid cluster. */ 503a3082d52SOGAWA Hirofumi fat_fs_error(sb, "corrupted directory (invalid i_start)"); 504a3082d52SOGAWA Hirofumi return -EIO; 505a3082d52SOGAWA Hirofumi } 506a3082d52SOGAWA Hirofumi return 0; 507a3082d52SOGAWA Hirofumi } 508a3082d52SOGAWA Hirofumi 5091da177e4SLinus Torvalds /* doesn't deal with root inode */ 510f1e6fb0aSNamjae Jeon int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) 5111da177e4SLinus Torvalds { 5121da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 513daaf2bf0SJeff Layton struct timespec64 mtime; 5141da177e4SLinus Torvalds int error; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = 0; 5171da177e4SLinus Torvalds inode->i_uid = sbi->options.fs_uid; 5181da177e4SLinus Torvalds inode->i_gid = sbi->options.fs_gid; 5192489dbabSJeff Layton inode_inc_iversion(inode); 520a251c17aSJason A. Donenfeld inode->i_generation = get_random_u32(); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { 5231da177e4SLinus Torvalds inode->i_generation &= ~1; 5249c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO); 5251da177e4SLinus Torvalds inode->i_op = sbi->dir_ops; 5261da177e4SLinus Torvalds inode->i_fop = &fat_dir_operations; 5271da177e4SLinus Torvalds 528a943ed71SSteven J. Magnani MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 5291da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 5301da177e4SLinus Torvalds error = fat_calc_dir_size(inode); 5311da177e4SLinus Torvalds if (error < 0) 5321da177e4SLinus Torvalds return error; 5331da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 5341da177e4SLinus Torvalds 535bfe86848SMiklos Szeredi set_nlink(inode, fat_subdirs(inode)); 536a3082d52SOGAWA Hirofumi 537a3082d52SOGAWA Hirofumi error = fat_validate_dir(inode); 538a3082d52SOGAWA Hirofumi if (error < 0) 539a3082d52SOGAWA Hirofumi return error; 5401da177e4SLinus Torvalds } else { /* not a directory */ 5411da177e4SLinus Torvalds inode->i_generation |= 1; 5429c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, de->attr, 5439aacd599SOGAWA Hirofumi ((sbi->options.showexec && !is_exec(de->name + 8)) 5449c0aa1b8SOGAWA Hirofumi ? S_IRUGO|S_IWUGO : S_IRWXUGO)); 545a943ed71SSteven J. Magnani MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 5481da177e4SLinus Torvalds inode->i_size = le32_to_cpu(de->size); 5491da177e4SLinus Torvalds inode->i_op = &fat_file_inode_operations; 5501da177e4SLinus Torvalds inode->i_fop = &fat_file_operations; 5511da177e4SLinus Torvalds inode->i_mapping->a_ops = &fat_aops; 5521da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds if (de->attr & ATTR_SYS) { 5551da177e4SLinus Torvalds if (sbi->options.sys_immutable) 5561da177e4SLinus Torvalds inode->i_flags |= S_IMMUTABLE; 5571da177e4SLinus Torvalds } 5589c0aa1b8SOGAWA Hirofumi fat_save_attrs(inode, de->attr); 5599c0aa1b8SOGAWA Hirofumi 5601da177e4SLinus Torvalds inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) 5611da177e4SLinus Torvalds & ~((loff_t)sbi->cluster_size - 1)) >> 9; 5627decd1cbSOGAWA Hirofumi 563daaf2bf0SJeff Layton fat_time_fat2unix(sbi, &mtime, de->time, de->date, 0); 564daaf2bf0SJeff Layton inode_set_mtime_to_ts(inode, mtime); 565daaf2bf0SJeff Layton inode_set_ctime_to_ts(inode, mtime); 5661da177e4SLinus Torvalds if (sbi->options.isvfat) { 567daaf2bf0SJeff Layton struct timespec64 atime; 568daaf2bf0SJeff Layton 569daaf2bf0SJeff Layton fat_time_fat2unix(sbi, &atime, 0, de->adate, 0); 570daaf2bf0SJeff Layton inode_set_atime_to_ts(inode, atime); 57130abce05SChung-Chiang Cheng fat_time_fat2unix(sbi, &MSDOS_I(inode)->i_crtime, de->ctime, 57230abce05SChung-Chiang Cheng de->cdate, de->ctime_cs); 5731da177e4SLinus Torvalds } else 574daaf2bf0SJeff Layton inode_set_atime_to_ts(inode, fat_truncate_atime(sbi, &mtime)); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds return 0; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5798fceb4e0SNamjae Jeon static inline void fat_lock_build_inode(struct msdos_sb_info *sbi) 5808fceb4e0SNamjae Jeon { 5818fceb4e0SNamjae Jeon if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 5828fceb4e0SNamjae Jeon mutex_lock(&sbi->nfs_build_inode_lock); 5838fceb4e0SNamjae Jeon } 5848fceb4e0SNamjae Jeon 5858fceb4e0SNamjae Jeon static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi) 5868fceb4e0SNamjae Jeon { 5878fceb4e0SNamjae Jeon if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 5888fceb4e0SNamjae Jeon mutex_unlock(&sbi->nfs_build_inode_lock); 5898fceb4e0SNamjae Jeon } 5908fceb4e0SNamjae Jeon 5911da177e4SLinus Torvalds struct inode *fat_build_inode(struct super_block *sb, 5921da177e4SLinus Torvalds struct msdos_dir_entry *de, loff_t i_pos) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds struct inode *inode; 5951da177e4SLinus Torvalds int err; 5961da177e4SLinus Torvalds 5978fceb4e0SNamjae Jeon fat_lock_build_inode(MSDOS_SB(sb)); 5981da177e4SLinus Torvalds inode = fat_iget(sb, i_pos); 5991da177e4SLinus Torvalds if (inode) 6001da177e4SLinus Torvalds goto out; 6011da177e4SLinus Torvalds inode = new_inode(sb); 6021da177e4SLinus Torvalds if (!inode) { 6031da177e4SLinus Torvalds inode = ERR_PTR(-ENOMEM); 6041da177e4SLinus Torvalds goto out; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds inode->i_ino = iunique(sb, MSDOS_ROOT_INO); 6072489dbabSJeff Layton inode_set_iversion(inode, 1); 6081da177e4SLinus Torvalds err = fat_fill_inode(inode, de); 6091da177e4SLinus Torvalds if (err) { 6101da177e4SLinus Torvalds iput(inode); 6111da177e4SLinus Torvalds inode = ERR_PTR(err); 6121da177e4SLinus Torvalds goto out; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds fat_attach(inode, i_pos); 6151da177e4SLinus Torvalds insert_inode_hash(inode); 6161da177e4SLinus Torvalds out: 6178fceb4e0SNamjae Jeon fat_unlock_build_inode(MSDOS_SB(sb)); 6181da177e4SLinus Torvalds return inode; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6217c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_build_inode); 6221da177e4SLinus Torvalds 623b13bb33eSNamjae Jeon static int __fat_write_inode(struct inode *inode, int wait); 624b13bb33eSNamjae Jeon 625b13bb33eSNamjae Jeon static void fat_free_eofblocks(struct inode *inode) 626b13bb33eSNamjae Jeon { 627b13bb33eSNamjae Jeon /* Release unwritten fallocated blocks on inode eviction. */ 628b13bb33eSNamjae Jeon if ((inode->i_blocks << 9) > 629b13bb33eSNamjae Jeon round_up(MSDOS_I(inode)->mmu_private, 630b13bb33eSNamjae Jeon MSDOS_SB(inode->i_sb)->cluster_size)) { 631b13bb33eSNamjae Jeon int err; 632b13bb33eSNamjae Jeon 633b13bb33eSNamjae Jeon fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private); 634b13bb33eSNamjae Jeon /* Fallocate results in updating the i_start/iogstart 635b13bb33eSNamjae Jeon * for the zero byte file. So, make it return to 636b13bb33eSNamjae Jeon * original state during evict and commit it to avoid 637b13bb33eSNamjae Jeon * any corruption on the next access to the cluster 638b13bb33eSNamjae Jeon * chain for the file. 639b13bb33eSNamjae Jeon */ 640b13bb33eSNamjae Jeon err = __fat_write_inode(inode, inode_needs_sync(inode)); 641b13bb33eSNamjae Jeon if (err) { 642b13bb33eSNamjae Jeon fat_msg(inode->i_sb, KERN_WARNING, "Failed to " 643b13bb33eSNamjae Jeon "update on disk inode for unused " 644b13bb33eSNamjae Jeon "fallocated blocks, inode could be " 645b13bb33eSNamjae Jeon "corrupted. Please run fsck"); 646b13bb33eSNamjae Jeon } 647b13bb33eSNamjae Jeon 648b13bb33eSNamjae Jeon } 649b13bb33eSNamjae Jeon } 650b13bb33eSNamjae Jeon 651deee3ce4SAl Viro static void fat_evict_inode(struct inode *inode) 6521da177e4SLinus Torvalds { 65391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 654deee3ce4SAl Viro if (!inode->i_nlink) { 6551da177e4SLinus Torvalds inode->i_size = 0; 656459f6ed3Snpiggin@suse.de fat_truncate_blocks(inode, 0); 657b13bb33eSNamjae Jeon } else 658b13bb33eSNamjae Jeon fat_free_eofblocks(inode); 659b13bb33eSNamjae Jeon 660deee3ce4SAl Viro invalidate_inode_buffers(inode); 661dbd5768fSJan Kara clear_inode(inode); 6621da177e4SLinus Torvalds fat_cache_inval_inode(inode); 663a993b542SOGAWA Hirofumi fat_detach(inode); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 666b88a1058SOleksij Rempel static void fat_set_state(struct super_block *sb, 667b88a1058SOleksij Rempel unsigned int set, unsigned int force) 668b88a1058SOleksij Rempel { 669b88a1058SOleksij Rempel struct buffer_head *bh; 670b88a1058SOleksij Rempel struct fat_boot_sector *b; 671d6bd4282SFred Chou struct msdos_sb_info *sbi = MSDOS_SB(sb); 672b88a1058SOleksij Rempel 673b88a1058SOleksij Rempel /* do not change any thing if mounted read only */ 674bc98a42cSDavid Howells if (sb_rdonly(sb) && !force) 675b88a1058SOleksij Rempel return; 676b88a1058SOleksij Rempel 677b88a1058SOleksij Rempel /* do not change state if fs was dirty */ 678b88a1058SOleksij Rempel if (sbi->dirty) { 679b88a1058SOleksij Rempel /* warn only on set (mount). */ 680b88a1058SOleksij Rempel if (set) 681b88a1058SOleksij Rempel fat_msg(sb, KERN_WARNING, "Volume was not properly " 682b88a1058SOleksij Rempel "unmounted. Some data may be corrupt. " 683b88a1058SOleksij Rempel "Please run fsck."); 684b88a1058SOleksij Rempel return; 685b88a1058SOleksij Rempel } 686b88a1058SOleksij Rempel 687b88a1058SOleksij Rempel bh = sb_bread(sb, 0); 688b88a1058SOleksij Rempel if (bh == NULL) { 689b88a1058SOleksij Rempel fat_msg(sb, KERN_ERR, "unable to read boot sector " 690b88a1058SOleksij Rempel "to mark fs as dirty"); 691b88a1058SOleksij Rempel return; 692b88a1058SOleksij Rempel } 693b88a1058SOleksij Rempel 694b88a1058SOleksij Rempel b = (struct fat_boot_sector *) bh->b_data; 695b88a1058SOleksij Rempel 696306790f7SCarmeli Tamir if (is_fat32(sbi)) { 697b88a1058SOleksij Rempel if (set) 698b88a1058SOleksij Rempel b->fat32.state |= FAT_STATE_DIRTY; 699b88a1058SOleksij Rempel else 700b88a1058SOleksij Rempel b->fat32.state &= ~FAT_STATE_DIRTY; 701b88a1058SOleksij Rempel } else /* fat 16 and 12 */ { 702b88a1058SOleksij Rempel if (set) 703b88a1058SOleksij Rempel b->fat16.state |= FAT_STATE_DIRTY; 704b88a1058SOleksij Rempel else 705b88a1058SOleksij Rempel b->fat16.state &= ~FAT_STATE_DIRTY; 706b88a1058SOleksij Rempel } 707b88a1058SOleksij Rempel 708b88a1058SOleksij Rempel mark_buffer_dirty(bh); 709b88a1058SOleksij Rempel sync_dirty_buffer(bh); 710b88a1058SOleksij Rempel brelse(bh); 711b88a1058SOleksij Rempel } 712b88a1058SOleksij Rempel 71335033ab9SOGAWA Hirofumi static void fat_reset_iocharset(struct fat_mount_options *opts) 71435033ab9SOGAWA Hirofumi { 71535033ab9SOGAWA Hirofumi if (opts->iocharset != fat_default_iocharset) { 71635033ab9SOGAWA Hirofumi /* Note: opts->iocharset can be NULL here */ 71735033ab9SOGAWA Hirofumi kfree(opts->iocharset); 71835033ab9SOGAWA Hirofumi opts->iocharset = fat_default_iocharset; 71935033ab9SOGAWA Hirofumi } 72035033ab9SOGAWA Hirofumi } 72135033ab9SOGAWA Hirofumi 722cac45b06SAl Viro static void delayed_free(struct rcu_head *p) 723cac45b06SAl Viro { 724cac45b06SAl Viro struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu); 725cac45b06SAl Viro unload_nls(sbi->nls_disk); 726cac45b06SAl Viro unload_nls(sbi->nls_io); 72735033ab9SOGAWA Hirofumi fat_reset_iocharset(&sbi->options); 728cac45b06SAl Viro kfree(sbi); 729cac45b06SAl Viro } 730cac45b06SAl Viro 731a6bf6b21SOGAWA Hirofumi static void fat_put_super(struct super_block *sb) 732a6bf6b21SOGAWA Hirofumi { 733a6bf6b21SOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 7341da177e4SLinus Torvalds 735b88a1058SOleksij Rempel fat_set_state(sb, 0, 0); 736b88a1058SOleksij Rempel 737020ac5b6SArtem Bityutskiy iput(sbi->fsinfo_inode); 738b522412aSAl Viro iput(sbi->fat_inode); 739b522412aSAl Viro 740cac45b06SAl Viro call_rcu(&sbi->rcu, delayed_free); 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 743e18b890bSChristoph Lameter static struct kmem_cache *fat_inode_cachep; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds static struct inode *fat_alloc_inode(struct super_block *sb) 7461da177e4SLinus Torvalds { 7471da177e4SLinus Torvalds struct msdos_inode_info *ei; 748fd60b288SMuchun Song ei = alloc_inode_sb(sb, fat_inode_cachep, GFP_NOFS); 7491da177e4SLinus Torvalds if (!ei) 7501da177e4SLinus Torvalds return NULL; 75158268691SChristoph Hellwig 75258268691SChristoph Hellwig init_rwsem(&ei->truncate_lock); 753bc87302aSOGAWA Hirofumi /* Zeroing to allow iput() even if partial initialized inode. */ 754bc87302aSOGAWA Hirofumi ei->mmu_private = 0; 755bc87302aSOGAWA Hirofumi ei->i_start = 0; 756bc87302aSOGAWA Hirofumi ei->i_logstart = 0; 757bc87302aSOGAWA Hirofumi ei->i_attrs = 0; 758bc87302aSOGAWA Hirofumi ei->i_pos = 0; 75930abce05SChung-Chiang Cheng ei->i_crtime.tv_sec = 0; 76030abce05SChung-Chiang Cheng ei->i_crtime.tv_nsec = 0; 761bc87302aSOGAWA Hirofumi 7621da177e4SLinus Torvalds return &ei->vfs_inode; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 765f9ec991dSAl Viro static void fat_free_inode(struct inode *inode) 766fa0d7e3dSNick Piggin { 767fa0d7e3dSNick Piggin kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); 768fa0d7e3dSNick Piggin } 769fa0d7e3dSNick Piggin 77051cc5068SAlexey Dobriyan static void init_once(void *foo) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds spin_lock_init(&ei->cache_lru_lock); 7751da177e4SLinus Torvalds ei->nr_caches = 0; 7761da177e4SLinus Torvalds ei->cache_valid_id = FAT_CACHE_VALID + 1; 7771da177e4SLinus Torvalds INIT_LIST_HEAD(&ei->cache_lru); 7781da177e4SLinus Torvalds INIT_HLIST_NODE(&ei->i_fat_hash); 7797669e8fbSSteven J. Magnani INIT_HLIST_NODE(&ei->i_dir_hash); 7801da177e4SLinus Torvalds inode_init_once(&ei->vfs_inode); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds static int __init fat_init_inodecache(void) 7841da177e4SLinus Torvalds { 7851da177e4SLinus Torvalds fat_inode_cachep = kmem_cache_create("fat_inode_cache", 7861da177e4SLinus Torvalds sizeof(struct msdos_inode_info), 787fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 788f88c3fb8SLinus Torvalds SLAB_ACCOUNT), 78920c2df83SPaul Mundt init_once); 7901da177e4SLinus Torvalds if (fat_inode_cachep == NULL) 7911da177e4SLinus Torvalds return -ENOMEM; 7921da177e4SLinus Torvalds return 0; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds static void __exit fat_destroy_inodecache(void) 7961da177e4SLinus Torvalds { 7978c0a8537SKirill A. Shutemov /* 7988c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we 7998c0a8537SKirill A. Shutemov * destroy cache. 8008c0a8537SKirill A. Shutemov */ 8018c0a8537SKirill A. Shutemov rcu_barrier(); 8021a1d92c1SAlexey Dobriyan kmem_cache_destroy(fat_inode_cachep); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 805634440b6SEric Sandeen int fat_reconfigure(struct fs_context *fc) 8061da177e4SLinus Torvalds { 807b6e8e12cSOGAWA Hirofumi bool new_rdonly; 808634440b6SEric Sandeen struct super_block *sb = fc->root->d_sb; 8091da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 810634440b6SEric Sandeen fc->sb_flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME); 811b88a1058SOleksij Rempel 81202b9984dSTheodore Ts'o sync_filesystem(sb); 81302b9984dSTheodore Ts'o 814b88a1058SOleksij Rempel /* make sure we update state on remount. */ 815634440b6SEric Sandeen new_rdonly = fc->sb_flags & SB_RDONLY; 816bc98a42cSDavid Howells if (new_rdonly != sb_rdonly(sb)) { 817b88a1058SOleksij Rempel if (new_rdonly) 818b88a1058SOleksij Rempel fat_set_state(sb, 0, 0); 819b88a1058SOleksij Rempel else 820b88a1058SOleksij Rempel fat_set_state(sb, 1, 1); 821b88a1058SOleksij Rempel } 8221da177e4SLinus Torvalds return 0; 8231da177e4SLinus Torvalds } 824634440b6SEric Sandeen EXPORT_SYMBOL_GPL(fat_reconfigure); 8251da177e4SLinus Torvalds 826726c3342SDavid Howells static int fat_statfs(struct dentry *dentry, struct kstatfs *buf) 8271da177e4SLinus Torvalds { 828aac49b75SColy Li struct super_block *sb = dentry->d_sb; 829aac49b75SColy Li struct msdos_sb_info *sbi = MSDOS_SB(sb); 830aac49b75SColy Li u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* If the count of free cluster is still unknown, counts it here. */ 833606e423eSOGAWA Hirofumi if (sbi->free_clusters == -1 || !sbi->free_clus_valid) { 834726c3342SDavid Howells int err = fat_count_free_clusters(dentry->d_sb); 8351da177e4SLinus Torvalds if (err) 8361da177e4SLinus Torvalds return err; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 839726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic; 8401da177e4SLinus Torvalds buf->f_bsize = sbi->cluster_size; 8411da177e4SLinus Torvalds buf->f_blocks = sbi->max_cluster - FAT_START_ENT; 8421da177e4SLinus Torvalds buf->f_bfree = sbi->free_clusters; 8431da177e4SLinus Torvalds buf->f_bavail = sbi->free_clusters; 8446d1349c7SAl Viro buf->f_fsid = u64_to_fsid(id); 845f68e542fSOGAWA Hirofumi buf->f_namelen = 846f68e542fSOGAWA Hirofumi (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds return 0; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 851a9185b41SChristoph Hellwig static int __fat_write_inode(struct inode *inode, int wait) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 8541da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 8551da177e4SLinus Torvalds struct buffer_head *bh; 8561da177e4SLinus Torvalds struct msdos_dir_entry *raw_entry; 857daaf2bf0SJeff Layton struct timespec64 mtime; 8581da177e4SLinus Torvalds loff_t i_pos; 859e22a4442SNamjae Jeon sector_t blocknr; 860e22a4442SNamjae Jeon int err, offset; 8611da177e4SLinus Torvalds 8629ca59f4cSOGAWA Hirofumi if (inode->i_ino == MSDOS_ROOT_INO) 8639ca59f4cSOGAWA Hirofumi return 0; 8649ca59f4cSOGAWA Hirofumi 8651da177e4SLinus Torvalds retry: 8669ca59f4cSOGAWA Hirofumi i_pos = fat_i_pos_read(sbi, inode); 8679ca59f4cSOGAWA Hirofumi if (!i_pos) 8681da177e4SLinus Torvalds return 0; 8691da177e4SLinus Torvalds 870e22a4442SNamjae Jeon fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset); 871e22a4442SNamjae Jeon bh = sb_bread(sb, blocknr); 8721da177e4SLinus Torvalds if (!bh) { 873869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read inode block " 874869f58c0SAlexey Fisher "for updating (i_pos %lld)", i_pos); 8755f22ca9bSLinus Torvalds return -EIO; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 8781da177e4SLinus Torvalds if (i_pos != MSDOS_I(inode)->i_pos) { 8791da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 8801da177e4SLinus Torvalds brelse(bh); 8811da177e4SLinus Torvalds goto retry; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 884e22a4442SNamjae Jeon raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset]; 8851da177e4SLinus Torvalds if (S_ISDIR(inode->i_mode)) 8861da177e4SLinus Torvalds raw_entry->size = 0; 8871da177e4SLinus Torvalds else 8881da177e4SLinus Torvalds raw_entry->size = cpu_to_le32(inode->i_size); 8899c0aa1b8SOGAWA Hirofumi raw_entry->attr = fat_make_attrs(inode); 890a943ed71SSteven J. Magnani fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart); 891daaf2bf0SJeff Layton mtime = inode_get_mtime(inode); 892daaf2bf0SJeff Layton fat_time_unix2fat(sbi, &mtime, &raw_entry->time, 8937decd1cbSOGAWA Hirofumi &raw_entry->date, NULL); 8941da177e4SLinus Torvalds if (sbi->options.isvfat) { 895daaf2bf0SJeff Layton struct timespec64 ts = inode_get_atime(inode); 89662a36c43SStephane Kardas __le16 atime; 897daaf2bf0SJeff Layton 898daaf2bf0SJeff Layton fat_time_unix2fat(sbi, &ts, &atime, &raw_entry->adate, NULL); 89930abce05SChung-Chiang Cheng fat_time_unix2fat(sbi, &MSDOS_I(inode)->i_crtime, &raw_entry->ctime, 90030abce05SChung-Chiang Cheng &raw_entry->cdate, &raw_entry->ctime_cs); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 9031da177e4SLinus Torvalds mark_buffer_dirty(bh); 9045f22ca9bSLinus Torvalds err = 0; 9051da177e4SLinus Torvalds if (wait) 9061da177e4SLinus Torvalds err = sync_dirty_buffer(bh); 9071da177e4SLinus Torvalds brelse(bh); 9081da177e4SLinus Torvalds return err; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 911a9185b41SChristoph Hellwig static int fat_write_inode(struct inode *inode, struct writeback_control *wbc) 912a9185b41SChristoph Hellwig { 91378491189SArtem Bityutskiy int err; 91478491189SArtem Bityutskiy 91578491189SArtem Bityutskiy if (inode->i_ino == MSDOS_FSINFO_INO) { 91678491189SArtem Bityutskiy struct super_block *sb = inode->i_sb; 91778491189SArtem Bityutskiy 918e40b34c7SMarco Stornelli mutex_lock(&MSDOS_SB(sb)->s_lock); 91978491189SArtem Bityutskiy err = fat_clusters_flush(sb); 920e40b34c7SMarco Stornelli mutex_unlock(&MSDOS_SB(sb)->s_lock); 92178491189SArtem Bityutskiy } else 92278491189SArtem Bityutskiy err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 92378491189SArtem Bityutskiy 92478491189SArtem Bityutskiy return err; 925a9185b41SChristoph Hellwig } 926a9185b41SChristoph Hellwig 9271da177e4SLinus Torvalds int fat_sync_inode(struct inode *inode) 9281da177e4SLinus Torvalds { 929a9185b41SChristoph Hellwig return __fat_write_inode(inode, 1); 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9327c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_sync_inode); 9331da177e4SLinus Torvalds 93434c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root); 935ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fat_sops = { 9361da177e4SLinus Torvalds .alloc_inode = fat_alloc_inode, 937f9ec991dSAl Viro .free_inode = fat_free_inode, 9381da177e4SLinus Torvalds .write_inode = fat_write_inode, 939deee3ce4SAl Viro .evict_inode = fat_evict_inode, 9401da177e4SLinus Torvalds .put_super = fat_put_super, 9411da177e4SLinus Torvalds .statfs = fat_statfs, 9421da177e4SLinus Torvalds .show_options = fat_show_options, 9431da177e4SLinus Torvalds }; 9441da177e4SLinus Torvalds 94534c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root) 9461da177e4SLinus Torvalds { 94734c80b1dSAl Viro struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb); 9481da177e4SLinus Torvalds struct fat_mount_options *opts = &sbi->options; 9491da177e4SLinus Torvalds int isvfat = opts->isvfat; 9501da177e4SLinus Torvalds 951170782ebSEric W. Biederman if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) 952170782ebSEric W. Biederman seq_printf(m, ",uid=%u", 953170782ebSEric W. Biederman from_kuid_munged(&init_user_ns, opts->fs_uid)); 954170782ebSEric W. Biederman if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) 955170782ebSEric W. Biederman seq_printf(m, ",gid=%u", 956170782ebSEric W. Biederman from_kgid_munged(&init_user_ns, opts->fs_gid)); 9571da177e4SLinus Torvalds seq_printf(m, ",fmask=%04o", opts->fs_fmask); 9581da177e4SLinus Torvalds seq_printf(m, ",dmask=%04o", opts->fs_dmask); 9591ae43f82SOGAWA Hirofumi if (opts->allow_utime) 9601ae43f82SOGAWA Hirofumi seq_printf(m, ",allow_utime=%04o", opts->allow_utime); 9611da177e4SLinus Torvalds if (sbi->nls_disk) 962c6c20372SDave Reisner /* strip "cp" prefix from displayed option */ 963c6c20372SDave Reisner seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]); 9641da177e4SLinus Torvalds if (isvfat) { 9651da177e4SLinus Torvalds if (sbi->nls_io) 9661da177e4SLinus Torvalds seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds switch (opts->shortname) { 9691da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: 9701da177e4SLinus Torvalds seq_puts(m, ",shortname=win95"); 9711da177e4SLinus Torvalds break; 9721da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: 9731da177e4SLinus Torvalds seq_puts(m, ",shortname=winnt"); 9741da177e4SLinus Torvalds break; 9751da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: 9761da177e4SLinus Torvalds seq_puts(m, ",shortname=mixed"); 9771da177e4SLinus Torvalds break; 9781da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: 97995523475SPaul Wise seq_puts(m, ",shortname=lower"); 9801da177e4SLinus Torvalds break; 9811da177e4SLinus Torvalds default: 9821da177e4SLinus Torvalds seq_puts(m, ",shortname=unknown"); 9831da177e4SLinus Torvalds break; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds if (opts->name_check != 'n') 9871da177e4SLinus Torvalds seq_printf(m, ",check=%c", opts->name_check); 98828ec039cSOGAWA Hirofumi if (opts->usefree) 98928ec039cSOGAWA Hirofumi seq_puts(m, ",usefree"); 9901da177e4SLinus Torvalds if (opts->quiet) 9911da177e4SLinus Torvalds seq_puts(m, ",quiet"); 9921da177e4SLinus Torvalds if (opts->showexec) 9931da177e4SLinus Torvalds seq_puts(m, ",showexec"); 9941da177e4SLinus Torvalds if (opts->sys_immutable) 9951da177e4SLinus Torvalds seq_puts(m, ",sys_immutable"); 9961da177e4SLinus Torvalds if (!isvfat) { 9971da177e4SLinus Torvalds if (opts->dotsOK) 9981da177e4SLinus Torvalds seq_puts(m, ",dotsOK=yes"); 9991da177e4SLinus Torvalds if (opts->nocase) 10001da177e4SLinus Torvalds seq_puts(m, ",nocase"); 10011da177e4SLinus Torvalds } else { 10021da177e4SLinus Torvalds if (opts->utf8) 10031da177e4SLinus Torvalds seq_puts(m, ",utf8"); 10041da177e4SLinus Torvalds if (opts->unicode_xlate) 10051da177e4SLinus Torvalds seq_puts(m, ",uni_xlate"); 10061da177e4SLinus Torvalds if (!opts->numtail) 10071da177e4SLinus Torvalds seq_puts(m, ",nonumtail"); 1008dfc209c0SOGAWA Hirofumi if (opts->rodir) 1009dfc209c0SOGAWA Hirofumi seq_puts(m, ",rodir"); 10101da177e4SLinus Torvalds } 1011dfc209c0SOGAWA Hirofumi if (opts->flush) 1012c1fca3b6SMiklos Szeredi seq_puts(m, ",flush"); 101358156c8fSJan Kara if (opts->tz_set) { 101458156c8fSJan Kara if (opts->time_offset) 101558156c8fSJan Kara seq_printf(m, ",time_offset=%d", opts->time_offset); 101658156c8fSJan Kara else 1017b271e067SJoe Peterson seq_puts(m, ",tz=UTC"); 101858156c8fSJan Kara } 101985c78591SDenis Karpov if (opts->errors == FAT_ERRORS_CONT) 102085c78591SDenis Karpov seq_puts(m, ",errors=continue"); 102185c78591SDenis Karpov else if (opts->errors == FAT_ERRORS_PANIC) 102285c78591SDenis Karpov seq_puts(m, ",errors=panic"); 102385c78591SDenis Karpov else 102485c78591SDenis Karpov seq_puts(m, ",errors=remount-ro"); 10252628b7a6SNamjae Jeon if (opts->nfs == FAT_NFS_NOSTALE_RO) 10262628b7a6SNamjae Jeon seq_puts(m, ",nfs=nostale_ro"); 10272628b7a6SNamjae Jeon else if (opts->nfs) 10282628b7a6SNamjae Jeon seq_puts(m, ",nfs=stale_rw"); 1029681142f9SChristoph Hellwig if (opts->discard) 1030681142f9SChristoph Hellwig seq_puts(m, ",discard"); 1031190a8843SConrad Meyer if (opts->dos1xfloppy) 1032190a8843SConrad Meyer seq_puts(m, ",dos1xfloppy"); 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds return 0; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds enum { 1038634440b6SEric Sandeen Opt_check, Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, 1039634440b6SEric Sandeen Opt_allow_utime, Opt_codepage, Opt_usefree, Opt_nocase, Opt_quiet, 1040634440b6SEric Sandeen Opt_showexec, Opt_debug, Opt_immutable, Opt_dots, Opt_dotsOK, 1041634440b6SEric Sandeen Opt_charset, Opt_shortname, Opt_utf8, Opt_utf8_bool, 1042634440b6SEric Sandeen Opt_uni_xl, Opt_uni_xl_bool, Opt_nonumtail, Opt_nonumtail_bool, 1043634440b6SEric Sandeen Opt_obsolete, Opt_flush, Opt_tz, Opt_rodir, Opt_errors, Opt_discard, 1044634440b6SEric Sandeen Opt_nfs, Opt_nfs_enum, Opt_time_offset, Opt_dos1xfloppy, 10451da177e4SLinus Torvalds }; 10461da177e4SLinus Torvalds 1047634440b6SEric Sandeen static const struct constant_table fat_param_check[] = { 1048634440b6SEric Sandeen {"relaxed", 'r'}, 1049634440b6SEric Sandeen {"r", 'r'}, 1050634440b6SEric Sandeen {"strict", 's'}, 1051634440b6SEric Sandeen {"s", 's'}, 1052634440b6SEric Sandeen {"normal", 'n'}, 1053634440b6SEric Sandeen {"n", 'n'}, 1054634440b6SEric Sandeen {} 10551da177e4SLinus Torvalds }; 10561da177e4SLinus Torvalds 1057634440b6SEric Sandeen static const struct constant_table fat_param_tz[] = { 1058634440b6SEric Sandeen {"UTC", 0}, 1059634440b6SEric Sandeen {} 1060634440b6SEric Sandeen }; 1061634440b6SEric Sandeen 1062634440b6SEric Sandeen static const struct constant_table fat_param_errors[] = { 1063634440b6SEric Sandeen {"continue", FAT_ERRORS_CONT}, 1064634440b6SEric Sandeen {"panic", FAT_ERRORS_PANIC}, 1065634440b6SEric Sandeen {"remount-ro", FAT_ERRORS_RO}, 1066634440b6SEric Sandeen {} 1067634440b6SEric Sandeen }; 1068634440b6SEric Sandeen 1069634440b6SEric Sandeen 1070634440b6SEric Sandeen static const struct constant_table fat_param_nfs[] = { 1071634440b6SEric Sandeen {"stale_rw", FAT_NFS_STALE_RW}, 1072634440b6SEric Sandeen {"nostale_ro", FAT_NFS_NOSTALE_RO}, 1073634440b6SEric Sandeen {} 1074634440b6SEric Sandeen }; 1075634440b6SEric Sandeen 1076634440b6SEric Sandeen /* 1077634440b6SEric Sandeen * These are all obsolete but we still reject invalid options. 1078634440b6SEric Sandeen * The corresponding values are therefore meaningless. 1079634440b6SEric Sandeen */ 1080634440b6SEric Sandeen static const struct constant_table fat_param_conv[] = { 1081634440b6SEric Sandeen {"binary", 0}, 1082634440b6SEric Sandeen {"text", 0}, 1083634440b6SEric Sandeen {"auto", 0}, 1084634440b6SEric Sandeen {"b", 0}, 1085634440b6SEric Sandeen {"t", 0}, 1086634440b6SEric Sandeen {"a", 0}, 1087634440b6SEric Sandeen {} 1088634440b6SEric Sandeen }; 1089634440b6SEric Sandeen 1090634440b6SEric Sandeen /* Core options. See below for vfat and msdos extras */ 1091634440b6SEric Sandeen const struct fs_parameter_spec fat_param_spec[] = { 1092634440b6SEric Sandeen fsparam_enum ("check", Opt_check, fat_param_check), 1093d02f0bb3SEric Sandeen fsparam_uid ("uid", Opt_uid), 1094d02f0bb3SEric Sandeen fsparam_gid ("gid", Opt_gid), 1095634440b6SEric Sandeen fsparam_u32oct ("umask", Opt_umask), 1096634440b6SEric Sandeen fsparam_u32oct ("dmask", Opt_dmask), 1097634440b6SEric Sandeen fsparam_u32oct ("fmask", Opt_fmask), 1098634440b6SEric Sandeen fsparam_u32oct ("allow_utime", Opt_allow_utime), 1099634440b6SEric Sandeen fsparam_u32 ("codepage", Opt_codepage), 1100634440b6SEric Sandeen fsparam_flag ("usefree", Opt_usefree), 1101634440b6SEric Sandeen fsparam_flag ("nocase", Opt_nocase), 1102634440b6SEric Sandeen fsparam_flag ("quiet", Opt_quiet), 1103634440b6SEric Sandeen fsparam_flag ("showexec", Opt_showexec), 1104634440b6SEric Sandeen fsparam_flag ("debug", Opt_debug), 1105634440b6SEric Sandeen fsparam_flag ("sys_immutable", Opt_immutable), 1106634440b6SEric Sandeen fsparam_flag ("flush", Opt_flush), 1107634440b6SEric Sandeen fsparam_enum ("tz", Opt_tz, fat_param_tz), 1108634440b6SEric Sandeen fsparam_s32 ("time_offset", Opt_time_offset), 1109634440b6SEric Sandeen fsparam_enum ("errors", Opt_errors, fat_param_errors), 1110634440b6SEric Sandeen fsparam_flag ("discard", Opt_discard), 1111634440b6SEric Sandeen fsparam_flag ("nfs", Opt_nfs), 1112634440b6SEric Sandeen fsparam_enum ("nfs", Opt_nfs_enum, fat_param_nfs), 1113634440b6SEric Sandeen fsparam_flag ("dos1xfloppy", Opt_dos1xfloppy), 1114634440b6SEric Sandeen __fsparam(fs_param_is_enum, "conv", 1115634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, fat_param_conv), 1116634440b6SEric Sandeen __fsparam(fs_param_is_u32, "fat", 1117634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, NULL), 1118634440b6SEric Sandeen __fsparam(fs_param_is_u32, "blocksize", 1119634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, NULL), 1120634440b6SEric Sandeen __fsparam(fs_param_is_string, "cvf_format", 1121634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, NULL), 1122634440b6SEric Sandeen __fsparam(fs_param_is_string, "cvf_options", 1123634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, NULL), 1124634440b6SEric Sandeen __fsparam(NULL, "posix", 1125634440b6SEric Sandeen Opt_obsolete, fs_param_deprecated, NULL), 1126634440b6SEric Sandeen {} 1127634440b6SEric Sandeen }; 1128634440b6SEric Sandeen EXPORT_SYMBOL_GPL(fat_param_spec); 1129634440b6SEric Sandeen 1130634440b6SEric Sandeen static const struct fs_parameter_spec msdos_param_spec[] = { 1131634440b6SEric Sandeen fsparam_flag_no ("dots", Opt_dots), 1132634440b6SEric Sandeen fsparam_bool ("dotsOK", Opt_dotsOK), 1133634440b6SEric Sandeen {} 1134634440b6SEric Sandeen }; 1135634440b6SEric Sandeen 1136634440b6SEric Sandeen static const struct constant_table fat_param_shortname[] = { 1137634440b6SEric Sandeen {"lower", VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95}, 1138634440b6SEric Sandeen {"win95", VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95}, 1139634440b6SEric Sandeen {"winnt", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT}, 1140634440b6SEric Sandeen {"mixed", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95}, 1141634440b6SEric Sandeen {} 1142634440b6SEric Sandeen }; 1143634440b6SEric Sandeen 1144634440b6SEric Sandeen static const struct fs_parameter_spec vfat_param_spec[] = { 1145634440b6SEric Sandeen fsparam_string ("iocharset", Opt_charset), 1146634440b6SEric Sandeen fsparam_enum ("shortname", Opt_shortname, fat_param_shortname), 1147634440b6SEric Sandeen fsparam_flag ("utf8", Opt_utf8), 1148634440b6SEric Sandeen fsparam_bool ("utf8", Opt_utf8_bool), 1149634440b6SEric Sandeen fsparam_flag ("uni_xlate", Opt_uni_xl), 1150634440b6SEric Sandeen fsparam_bool ("uni_xlate", Opt_uni_xl_bool), 1151634440b6SEric Sandeen fsparam_flag ("nonumtail", Opt_nonumtail), 1152634440b6SEric Sandeen fsparam_bool ("nonumtail", Opt_nonumtail_bool), 1153634440b6SEric Sandeen fsparam_flag ("rodir", Opt_rodir), 1154634440b6SEric Sandeen {} 1155634440b6SEric Sandeen }; 1156634440b6SEric Sandeen 1157634440b6SEric Sandeen int fat_parse_param(struct fs_context *fc, struct fs_parameter *param, 1158634440b6SEric Sandeen bool is_vfat) 11591da177e4SLinus Torvalds { 1160634440b6SEric Sandeen struct fat_mount_options *opts = fc->fs_private; 1161634440b6SEric Sandeen struct fs_parse_result result; 1162634440b6SEric Sandeen int opt; 11631da177e4SLinus Torvalds 1164634440b6SEric Sandeen /* remount options have traditionally been ignored */ 1165634440b6SEric Sandeen if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) 1166634440b6SEric Sandeen return 0; 11671da177e4SLinus Torvalds 1168634440b6SEric Sandeen opt = fs_parse(fc, fat_param_spec, param, &result); 1169634440b6SEric Sandeen /* If option not found in fat_param_spec, try vfat/msdos options */ 1170634440b6SEric Sandeen if (opt == -ENOPARAM) { 11711da177e4SLinus Torvalds if (is_vfat) 1172634440b6SEric Sandeen opt = fs_parse(fc, vfat_param_spec, param, &result); 11731da177e4SLinus Torvalds else 1174634440b6SEric Sandeen opt = fs_parse(fc, msdos_param_spec, param, &result); 11751da177e4SLinus Torvalds } 1176634440b6SEric Sandeen 1177634440b6SEric Sandeen if (opt < 0) 1178634440b6SEric Sandeen return opt; 1179634440b6SEric Sandeen 1180634440b6SEric Sandeen switch (opt) { 1181634440b6SEric Sandeen case Opt_check: 1182634440b6SEric Sandeen opts->name_check = result.uint_32; 11831da177e4SLinus Torvalds break; 118428ec039cSOGAWA Hirofumi case Opt_usefree: 118528ec039cSOGAWA Hirofumi opts->usefree = 1; 118628ec039cSOGAWA Hirofumi break; 11871da177e4SLinus Torvalds case Opt_nocase: 11881da177e4SLinus Torvalds if (!is_vfat) 11891da177e4SLinus Torvalds opts->nocase = 1; 11901da177e4SLinus Torvalds else { 11911da177e4SLinus Torvalds /* for backward compatibility */ 11921da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_WIN95 11931da177e4SLinus Torvalds | VFAT_SFN_CREATE_WIN95; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds break; 11961da177e4SLinus Torvalds case Opt_quiet: 11971da177e4SLinus Torvalds opts->quiet = 1; 11981da177e4SLinus Torvalds break; 11991da177e4SLinus Torvalds case Opt_showexec: 12001da177e4SLinus Torvalds opts->showexec = 1; 12011da177e4SLinus Torvalds break; 12021da177e4SLinus Torvalds case Opt_debug: 1203206d3d8eSEric Sandeen opts->debug = 1; 12041da177e4SLinus Torvalds break; 12051da177e4SLinus Torvalds case Opt_immutable: 12061da177e4SLinus Torvalds opts->sys_immutable = 1; 12071da177e4SLinus Torvalds break; 12081da177e4SLinus Torvalds case Opt_uid: 1209d02f0bb3SEric Sandeen opts->fs_uid = result.uid; 12101da177e4SLinus Torvalds break; 12111da177e4SLinus Torvalds case Opt_gid: 1212d02f0bb3SEric Sandeen opts->fs_gid = result.gid; 12131da177e4SLinus Torvalds break; 12141da177e4SLinus Torvalds case Opt_umask: 1215634440b6SEric Sandeen opts->fs_fmask = opts->fs_dmask = result.uint_32; 12161da177e4SLinus Torvalds break; 12171da177e4SLinus Torvalds case Opt_dmask: 1218634440b6SEric Sandeen opts->fs_dmask = result.uint_32; 12191da177e4SLinus Torvalds break; 12201da177e4SLinus Torvalds case Opt_fmask: 1221634440b6SEric Sandeen opts->fs_fmask = result.uint_32; 12221da177e4SLinus Torvalds break; 12231ae43f82SOGAWA Hirofumi case Opt_allow_utime: 1224634440b6SEric Sandeen opts->allow_utime = result.uint_32 & (S_IWGRP | S_IWOTH); 12251ae43f82SOGAWA Hirofumi break; 12261da177e4SLinus Torvalds case Opt_codepage: 1227634440b6SEric Sandeen opts->codepage = result.uint_32; 12281da177e4SLinus Torvalds break; 1229ae78bf9cSChris Mason case Opt_flush: 1230ae78bf9cSChris Mason opts->flush = 1; 1231ae78bf9cSChris Mason break; 123258156c8fSJan Kara case Opt_time_offset: 1233a513d869SJan Kara /* 1234a513d869SJan Kara * GMT+-12 zones may have DST corrections so at least 1235a513d869SJan Kara * 13 hours difference is needed. Make the limit 24 1236a513d869SJan Kara * just in case someone invents something unusual. 1237a513d869SJan Kara */ 1238634440b6SEric Sandeen if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60) 12395b3d5aeaSJan Kara return -EINVAL; 124058156c8fSJan Kara opts->tz_set = 1; 1241634440b6SEric Sandeen opts->time_offset = result.int_32; 124258156c8fSJan Kara break; 1243634440b6SEric Sandeen case Opt_tz: 124458156c8fSJan Kara opts->tz_set = 1; 1245634440b6SEric Sandeen opts->time_offset = result.uint_32; 1246b271e067SJoe Peterson break; 1247634440b6SEric Sandeen case Opt_errors: 1248634440b6SEric Sandeen opts->errors = result.uint_32; 124985c78591SDenis Karpov break; 1250634440b6SEric Sandeen case Opt_nfs: 12512628b7a6SNamjae Jeon opts->nfs = FAT_NFS_STALE_RW; 12522628b7a6SNamjae Jeon break; 1253634440b6SEric Sandeen case Opt_nfs_enum: 1254634440b6SEric Sandeen opts->nfs = result.uint_32; 12552628b7a6SNamjae Jeon break; 1256190a8843SConrad Meyer case Opt_dos1xfloppy: 1257190a8843SConrad Meyer opts->dos1xfloppy = 1; 1258190a8843SConrad Meyer break; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds /* msdos specific */ 1261634440b6SEric Sandeen case Opt_dots: /* dots / nodots */ 1262634440b6SEric Sandeen opts->dotsOK = !result.negated; 12631da177e4SLinus Torvalds break; 1264634440b6SEric Sandeen case Opt_dotsOK: /* dotsOK = yes/no */ 1265634440b6SEric Sandeen opts->dotsOK = result.boolean; 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* vfat specific */ 12691da177e4SLinus Torvalds case Opt_charset: 127035033ab9SOGAWA Hirofumi fat_reset_iocharset(opts); 1271634440b6SEric Sandeen opts->iocharset = param->string; 1272634440b6SEric Sandeen param->string = NULL; /* Steal string */ 12731da177e4SLinus Torvalds break; 1274634440b6SEric Sandeen case Opt_shortname: 1275634440b6SEric Sandeen opts->shortname = result.uint_32; 12761da177e4SLinus Torvalds break; 1277634440b6SEric Sandeen case Opt_utf8: 12781da177e4SLinus Torvalds opts->utf8 = 1; 12791da177e4SLinus Torvalds break; 1280634440b6SEric Sandeen case Opt_utf8_bool: 1281634440b6SEric Sandeen opts->utf8 = result.boolean; 12821da177e4SLinus Torvalds break; 1283634440b6SEric Sandeen case Opt_uni_xl: 12841da177e4SLinus Torvalds opts->unicode_xlate = 1; 12851da177e4SLinus Torvalds break; 1286634440b6SEric Sandeen case Opt_uni_xl_bool: 1287634440b6SEric Sandeen opts->unicode_xlate = result.boolean; 12881da177e4SLinus Torvalds break; 1289634440b6SEric Sandeen case Opt_nonumtail: 12901da177e4SLinus Torvalds opts->numtail = 0; /* negated option */ 12911da177e4SLinus Torvalds break; 1292634440b6SEric Sandeen case Opt_nonumtail_bool: 1293634440b6SEric Sandeen opts->numtail = !result.boolean; /* negated option */ 1294634440b6SEric Sandeen break; 1295dfc209c0SOGAWA Hirofumi case Opt_rodir: 1296dfc209c0SOGAWA Hirofumi opts->rodir = 1; 1297dfc209c0SOGAWA Hirofumi break; 1298681142f9SChristoph Hellwig case Opt_discard: 1299681142f9SChristoph Hellwig opts->discard = 1; 1300681142f9SChristoph Hellwig break; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds /* obsolete mount options */ 1303d7a83c0fSGeert Uytterhoeven case Opt_obsolete: 1304634440b6SEric Sandeen printk(KERN_INFO "FAT-fs: \"%s\" option is obsolete, " 1305634440b6SEric Sandeen "not supported now", param->key); 13061da177e4SLinus Torvalds break; 13071da177e4SLinus Torvalds default: 13081da177e4SLinus Torvalds return -EINVAL; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds return 0; 13121da177e4SLinus Torvalds } 1313634440b6SEric Sandeen EXPORT_SYMBOL_GPL(fat_parse_param); 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds static int fat_read_root(struct inode *inode) 13161da177e4SLinus Torvalds { 1317a40a7d9dSAlexander Kuleshov struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 13181da177e4SLinus Torvalds int error; 13191da177e4SLinus Torvalds 1320ea3983acSNamjae Jeon MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO; 13211da177e4SLinus Torvalds inode->i_uid = sbi->options.fs_uid; 13221da177e4SLinus Torvalds inode->i_gid = sbi->options.fs_gid; 13232489dbabSJeff Layton inode_inc_iversion(inode); 13241da177e4SLinus Torvalds inode->i_generation = 0; 13259c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); 13261da177e4SLinus Torvalds inode->i_op = sbi->dir_ops; 13271da177e4SLinus Torvalds inode->i_fop = &fat_dir_operations; 1328306790f7SCarmeli Tamir if (is_fat32(sbi)) { 13291da177e4SLinus Torvalds MSDOS_I(inode)->i_start = sbi->root_cluster; 13301da177e4SLinus Torvalds error = fat_calc_dir_size(inode); 13311da177e4SLinus Torvalds if (error < 0) 13321da177e4SLinus Torvalds return error; 13331da177e4SLinus Torvalds } else { 13341da177e4SLinus Torvalds MSDOS_I(inode)->i_start = 0; 13351da177e4SLinus Torvalds inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) 13381da177e4SLinus Torvalds & ~((loff_t)sbi->cluster_size - 1)) >> 9; 13391da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = 0; 13401da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 13411da177e4SLinus Torvalds 13429c0aa1b8SOGAWA Hirofumi fat_save_attrs(inode, ATTR_DIR); 1343daaf2bf0SJeff Layton inode_set_mtime_to_ts(inode, 1344daaf2bf0SJeff Layton inode_set_atime_to_ts(inode, inode_set_ctime(inode, 0, 0))); 1345bfe86848SMiklos Szeredi set_nlink(inode, fat_subdirs(inode)+2); 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds return 0; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13507b92d03cSOGAWA Hirofumi static unsigned long calc_fat_clusters(struct super_block *sb) 13517b92d03cSOGAWA Hirofumi { 13527b92d03cSOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 13537b92d03cSOGAWA Hirofumi 13547b92d03cSOGAWA Hirofumi /* Divide first to avoid overflow */ 1355306790f7SCarmeli Tamir if (!is_fat12(sbi)) { 13567b92d03cSOGAWA Hirofumi unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits; 13577b92d03cSOGAWA Hirofumi return ent_per_sec * sbi->fat_length; 13587b92d03cSOGAWA Hirofumi } 13597b92d03cSOGAWA Hirofumi 13607b92d03cSOGAWA Hirofumi return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits; 13617b92d03cSOGAWA Hirofumi } 13627b92d03cSOGAWA Hirofumi 1363190a8843SConrad Meyer static bool fat_bpb_is_zero(struct fat_boot_sector *b) 1364190a8843SConrad Meyer { 1365190a8843SConrad Meyer if (get_unaligned_le16(&b->sector_size)) 1366190a8843SConrad Meyer return false; 1367190a8843SConrad Meyer if (b->sec_per_clus) 1368190a8843SConrad Meyer return false; 1369190a8843SConrad Meyer if (b->reserved) 1370190a8843SConrad Meyer return false; 1371190a8843SConrad Meyer if (b->fats) 1372190a8843SConrad Meyer return false; 1373190a8843SConrad Meyer if (get_unaligned_le16(&b->dir_entries)) 1374190a8843SConrad Meyer return false; 1375190a8843SConrad Meyer if (get_unaligned_le16(&b->sectors)) 1376190a8843SConrad Meyer return false; 1377190a8843SConrad Meyer if (b->media) 1378190a8843SConrad Meyer return false; 1379190a8843SConrad Meyer if (b->fat_length) 1380190a8843SConrad Meyer return false; 1381190a8843SConrad Meyer if (b->secs_track) 1382190a8843SConrad Meyer return false; 1383190a8843SConrad Meyer if (b->heads) 1384190a8843SConrad Meyer return false; 1385190a8843SConrad Meyer return true; 1386190a8843SConrad Meyer } 1387190a8843SConrad Meyer 1388190a8843SConrad Meyer static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b, 1389190a8843SConrad Meyer int silent, struct fat_bios_param_block *bpb) 1390190a8843SConrad Meyer { 1391190a8843SConrad Meyer int error = -EINVAL; 1392190a8843SConrad Meyer 1393190a8843SConrad Meyer /* Read in BPB ... */ 1394190a8843SConrad Meyer memset(bpb, 0, sizeof(*bpb)); 1395190a8843SConrad Meyer bpb->fat_sector_size = get_unaligned_le16(&b->sector_size); 1396190a8843SConrad Meyer bpb->fat_sec_per_clus = b->sec_per_clus; 1397190a8843SConrad Meyer bpb->fat_reserved = le16_to_cpu(b->reserved); 1398190a8843SConrad Meyer bpb->fat_fats = b->fats; 1399190a8843SConrad Meyer bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries); 1400190a8843SConrad Meyer bpb->fat_sectors = get_unaligned_le16(&b->sectors); 1401190a8843SConrad Meyer bpb->fat_fat_length = le16_to_cpu(b->fat_length); 1402190a8843SConrad Meyer bpb->fat_total_sect = le32_to_cpu(b->total_sect); 1403190a8843SConrad Meyer 1404190a8843SConrad Meyer bpb->fat16_state = b->fat16.state; 1405190a8843SConrad Meyer bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id); 1406190a8843SConrad Meyer 1407190a8843SConrad Meyer bpb->fat32_length = le32_to_cpu(b->fat32.length); 1408190a8843SConrad Meyer bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster); 1409190a8843SConrad Meyer bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector); 1410190a8843SConrad Meyer bpb->fat32_state = b->fat32.state; 1411190a8843SConrad Meyer bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id); 1412190a8843SConrad Meyer 1413190a8843SConrad Meyer /* Validate this looks like a FAT filesystem BPB */ 1414190a8843SConrad Meyer if (!bpb->fat_reserved) { 1415190a8843SConrad Meyer if (!silent) 1416190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1417190a8843SConrad Meyer "bogus number of reserved sectors"); 1418190a8843SConrad Meyer goto out; 1419190a8843SConrad Meyer } 1420190a8843SConrad Meyer if (!bpb->fat_fats) { 1421190a8843SConrad Meyer if (!silent) 1422190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus number of FAT structure"); 1423190a8843SConrad Meyer goto out; 1424190a8843SConrad Meyer } 1425190a8843SConrad Meyer 1426190a8843SConrad Meyer /* 1427190a8843SConrad Meyer * Earlier we checked here that b->secs_track and b->head are nonzero, 1428190a8843SConrad Meyer * but it turns out valid FAT filesystems can have zero there. 1429190a8843SConrad Meyer */ 1430190a8843SConrad Meyer 1431190a8843SConrad Meyer if (!fat_valid_media(b->media)) { 1432190a8843SConrad Meyer if (!silent) 1433190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)", 1434190a8843SConrad Meyer (unsigned)b->media); 1435190a8843SConrad Meyer goto out; 1436190a8843SConrad Meyer } 1437190a8843SConrad Meyer 1438190a8843SConrad Meyer if (!is_power_of_2(bpb->fat_sector_size) 1439190a8843SConrad Meyer || (bpb->fat_sector_size < 512) 1440190a8843SConrad Meyer || (bpb->fat_sector_size > 4096)) { 1441190a8843SConrad Meyer if (!silent) 1442190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus logical sector size %u", 1443190a8843SConrad Meyer (unsigned)bpb->fat_sector_size); 1444190a8843SConrad Meyer goto out; 1445190a8843SConrad Meyer } 1446190a8843SConrad Meyer 1447190a8843SConrad Meyer if (!is_power_of_2(bpb->fat_sec_per_clus)) { 1448190a8843SConrad Meyer if (!silent) 1449190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u", 1450190a8843SConrad Meyer (unsigned)bpb->fat_sec_per_clus); 1451190a8843SConrad Meyer goto out; 1452190a8843SConrad Meyer } 1453190a8843SConrad Meyer 1454b1b65750SOGAWA Hirofumi if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) { 1455b1b65750SOGAWA Hirofumi if (!silent) 1456b1b65750SOGAWA Hirofumi fat_msg(sb, KERN_ERR, "bogus number of FAT sectors"); 1457b1b65750SOGAWA Hirofumi goto out; 1458b1b65750SOGAWA Hirofumi } 1459b1b65750SOGAWA Hirofumi 1460190a8843SConrad Meyer error = 0; 1461190a8843SConrad Meyer 1462190a8843SConrad Meyer out: 1463190a8843SConrad Meyer return error; 1464190a8843SConrad Meyer } 1465190a8843SConrad Meyer 1466190a8843SConrad Meyer static int fat_read_static_bpb(struct super_block *sb, 1467190a8843SConrad Meyer struct fat_boot_sector *b, int silent, 1468190a8843SConrad Meyer struct fat_bios_param_block *bpb) 1469190a8843SConrad Meyer { 1470190a8843SConrad Meyer static const char *notdos1x = "This doesn't look like a DOS 1.x volume"; 14719e48243bSChristoph Hellwig sector_t bd_sects = bdev_nr_sectors(sb->s_bdev); 1472190a8843SConrad Meyer struct fat_floppy_defaults *fdefaults = NULL; 1473190a8843SConrad Meyer int error = -EINVAL; 1474190a8843SConrad Meyer unsigned i; 1475190a8843SConrad Meyer 1476190a8843SConrad Meyer /* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */ 1477190a8843SConrad Meyer if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) { 1478190a8843SConrad Meyer if (!silent) 1479190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1480190a8843SConrad Meyer "%s; no bootstrapping code", notdos1x); 1481190a8843SConrad Meyer goto out; 1482190a8843SConrad Meyer } 1483190a8843SConrad Meyer 1484190a8843SConrad Meyer /* 1485190a8843SConrad Meyer * If any value in this region is non-zero, it isn't archaic 1486190a8843SConrad Meyer * DOS. 1487190a8843SConrad Meyer */ 1488190a8843SConrad Meyer if (!fat_bpb_is_zero(b)) { 1489190a8843SConrad Meyer if (!silent) 1490190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1491190a8843SConrad Meyer "%s; DOS 2.x BPB is non-zero", notdos1x); 1492190a8843SConrad Meyer goto out; 1493190a8843SConrad Meyer } 1494190a8843SConrad Meyer 1495190a8843SConrad Meyer for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) { 1496190a8843SConrad Meyer if (floppy_defaults[i].nr_sectors == bd_sects) { 1497190a8843SConrad Meyer fdefaults = &floppy_defaults[i]; 1498190a8843SConrad Meyer break; 1499190a8843SConrad Meyer } 1500190a8843SConrad Meyer } 1501190a8843SConrad Meyer 1502190a8843SConrad Meyer if (fdefaults == NULL) { 1503190a8843SConrad Meyer if (!silent) 1504190a8843SConrad Meyer fat_msg(sb, KERN_WARNING, 1505190a8843SConrad Meyer "This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)", 1506190a8843SConrad Meyer (u64)bd_sects); 1507190a8843SConrad Meyer goto out; 1508190a8843SConrad Meyer } 1509190a8843SConrad Meyer 1510190a8843SConrad Meyer if (!silent) 1511190a8843SConrad Meyer fat_msg(sb, KERN_INFO, 1512190a8843SConrad Meyer "This looks like a DOS 1.x volume; assuming default BPB values"); 1513190a8843SConrad Meyer 1514190a8843SConrad Meyer memset(bpb, 0, sizeof(*bpb)); 1515190a8843SConrad Meyer bpb->fat_sector_size = SECTOR_SIZE; 1516190a8843SConrad Meyer bpb->fat_sec_per_clus = fdefaults->sec_per_clus; 1517190a8843SConrad Meyer bpb->fat_reserved = 1; 1518190a8843SConrad Meyer bpb->fat_fats = 2; 1519190a8843SConrad Meyer bpb->fat_dir_entries = fdefaults->dir_entries; 1520190a8843SConrad Meyer bpb->fat_sectors = fdefaults->nr_sectors; 1521190a8843SConrad Meyer bpb->fat_fat_length = fdefaults->fat_length; 1522190a8843SConrad Meyer 1523190a8843SConrad Meyer error = 0; 1524190a8843SConrad Meyer 1525190a8843SConrad Meyer out: 1526190a8843SConrad Meyer return error; 1527190a8843SConrad Meyer } 1528190a8843SConrad Meyer 15291da177e4SLinus Torvalds /* 15301da177e4SLinus Torvalds * Read the super block of an MS-DOS FS. 15311da177e4SLinus Torvalds */ 1532634440b6SEric Sandeen int fat_fill_super(struct super_block *sb, struct fs_context *fc, 15333d23985dSAl Viro void (*setup)(struct super_block *)) 15341da177e4SLinus Torvalds { 1535634440b6SEric Sandeen struct fat_mount_options *opts = fc->fs_private; 1536634440b6SEric Sandeen int silent = fc->sb_flags & SB_SILENT; 1537b522412aSAl Viro struct inode *root_inode = NULL, *fat_inode = NULL; 1538020ac5b6SArtem Bityutskiy struct inode *fsinfo_inode = NULL; 15391da177e4SLinus Torvalds struct buffer_head *bh; 1540190a8843SConrad Meyer struct fat_bios_param_block bpb; 15411da177e4SLinus Torvalds struct msdos_sb_info *sbi; 15421da177e4SLinus Torvalds u16 logical_sector_size; 15431da177e4SLinus Torvalds u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; 15441da177e4SLinus Torvalds long error; 15451da177e4SLinus Torvalds char buf[50]; 1546c0da64f6SDeepa Dinamani struct timespec64 ts; 15471da177e4SLinus Torvalds 15488f593427SLinus Torvalds /* 15498f593427SLinus Torvalds * GFP_KERNEL is ok here, because while we do hold the 1550dcc381e8SZheng Lv * superblock lock, memory pressure can't call back into 15518f593427SLinus Torvalds * the filesystem, since we're only just about to mount 15528f593427SLinus Torvalds * it and have no inodes etc active! 15538f593427SLinus Torvalds */ 1554f8314dc6SPanagiotis Issaris sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); 15551da177e4SLinus Torvalds if (!sbi) 15561da177e4SLinus Torvalds return -ENOMEM; 15571da177e4SLinus Torvalds sb->s_fs_info = sbi; 15581da177e4SLinus Torvalds 15591751e8a6SLinus Torvalds sb->s_flags |= SB_NODIRATIME; 15601da177e4SLinus Torvalds sb->s_magic = MSDOS_SUPER_MAGIC; 15611da177e4SLinus Torvalds sb->s_op = &fat_sops; 15621da177e4SLinus Torvalds sb->s_export_op = &fat_export_ops; 15636bb885ecSFrank Sorenson /* 15646bb885ecSFrank Sorenson * fat timestamps are complex and truncated by fat itself, so 15656bb885ecSFrank Sorenson * we set 1 here to be fast 15666bb885ecSFrank Sorenson */ 15676bb885ecSFrank Sorenson sb->s_time_gran = 1; 15688fceb4e0SNamjae Jeon mutex_init(&sbi->nfs_build_inode_lock); 1569aaa04b48SOGAWA Hirofumi ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, 1570aaa04b48SOGAWA Hirofumi DEFAULT_RATELIMIT_BURST); 15711da177e4SLinus Torvalds 1572634440b6SEric Sandeen /* UTF-8 doesn't provide FAT semantics */ 1573634440b6SEric Sandeen if (!strcmp(opts->iocharset, "utf8")) { 1574634440b6SEric Sandeen fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset" 1575634440b6SEric Sandeen " for FAT filesystems, filesystem will be" 1576634440b6SEric Sandeen " case sensitive!"); 1577634440b6SEric Sandeen } 1578634440b6SEric Sandeen 1579634440b6SEric Sandeen /* If user doesn't specify allow_utime, it's initialized from dmask. */ 1580634440b6SEric Sandeen if (opts->allow_utime == (unsigned short)-1) 1581634440b6SEric Sandeen opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH); 1582634440b6SEric Sandeen if (opts->unicode_xlate) 1583634440b6SEric Sandeen opts->utf8 = 0; 1584634440b6SEric Sandeen if (opts->nfs == FAT_NFS_NOSTALE_RO) { 1585634440b6SEric Sandeen sb->s_flags |= SB_RDONLY; 1586634440b6SEric Sandeen sb->s_export_op = &fat_export_ops_nostale; 1587634440b6SEric Sandeen } 1588634440b6SEric Sandeen 1589634440b6SEric Sandeen /* Apply parsed options to sbi (structure copy) */ 1590634440b6SEric Sandeen sbi->options = *opts; 1591634440b6SEric Sandeen /* Transfer ownership of iocharset to sbi->options */ 1592634440b6SEric Sandeen opts->iocharset = NULL; 15931da177e4SLinus Torvalds 15943d23985dSAl Viro setup(sb); /* flavour-specific stuff that needs options */ 15953d23985dSAl Viro 15961da177e4SLinus Torvalds error = -EIO; 15971da177e4SLinus Torvalds sb_min_blocksize(sb, 512); 15981da177e4SLinus Torvalds bh = sb_bread(sb, 0); 15991da177e4SLinus Torvalds if (bh == NULL) { 1600869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read boot sector"); 16011da177e4SLinus Torvalds goto out_fail; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 1604190a8843SConrad Meyer error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent, 1605190a8843SConrad Meyer &bpb); 1606190a8843SConrad Meyer if (error == -EINVAL && sbi->options.dos1xfloppy) 1607190a8843SConrad Meyer error = fat_read_static_bpb(sb, 1608190a8843SConrad Meyer (struct fat_boot_sector *)bh->b_data, silent, &bpb); 16091da177e4SLinus Torvalds brelse(bh); 16101da177e4SLinus Torvalds 1611190a8843SConrad Meyer if (error == -EINVAL) 1612190a8843SConrad Meyer goto out_invalid; 1613190a8843SConrad Meyer else if (error) 1614190a8843SConrad Meyer goto out_fail; 16151da177e4SLinus Torvalds 1616190a8843SConrad Meyer logical_sector_size = bpb.fat_sector_size; 1617190a8843SConrad Meyer sbi->sec_per_clus = bpb.fat_sec_per_clus; 16181da177e4SLinus Torvalds 1619190a8843SConrad Meyer error = -EIO; 16201da177e4SLinus Torvalds if (logical_sector_size < sb->s_blocksize) { 1621869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "logical sector size too small for device" 1622869f58c0SAlexey Fisher " (logical sector size = %u)", logical_sector_size); 16231da177e4SLinus Torvalds goto out_fail; 16241da177e4SLinus Torvalds } 1625190a8843SConrad Meyer 16261da177e4SLinus Torvalds if (logical_sector_size > sb->s_blocksize) { 1627190a8843SConrad Meyer struct buffer_head *bh_resize; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds if (!sb_set_blocksize(sb, logical_sector_size)) { 1630869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to set blocksize %u", 16311da177e4SLinus Torvalds logical_sector_size); 16321da177e4SLinus Torvalds goto out_fail; 16331da177e4SLinus Torvalds } 1634190a8843SConrad Meyer 1635190a8843SConrad Meyer /* Verify that the larger boot sector is fully readable */ 1636190a8843SConrad Meyer bh_resize = sb_bread(sb, 0); 1637190a8843SConrad Meyer if (bh_resize == NULL) { 1638869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read boot sector" 1639869f58c0SAlexey Fisher " (logical sector size = %lu)", 16401da177e4SLinus Torvalds sb->s_blocksize); 16411da177e4SLinus Torvalds goto out_fail; 16421da177e4SLinus Torvalds } 1643190a8843SConrad Meyer brelse(bh_resize); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 1646e40b34c7SMarco Stornelli mutex_init(&sbi->s_lock); 16471da177e4SLinus Torvalds sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus; 16481da177e4SLinus Torvalds sbi->cluster_bits = ffs(sbi->cluster_size) - 1; 1649190a8843SConrad Meyer sbi->fats = bpb.fat_fats; 16501da177e4SLinus Torvalds sbi->fat_bits = 0; /* Don't know yet */ 1651190a8843SConrad Meyer sbi->fat_start = bpb.fat_reserved; 1652190a8843SConrad Meyer sbi->fat_length = bpb.fat_fat_length; 16531da177e4SLinus Torvalds sbi->root_cluster = 0; 16541da177e4SLinus Torvalds sbi->free_clusters = -1; /* Don't know yet */ 1655606e423eSOGAWA Hirofumi sbi->free_clus_valid = 0; 16561da177e4SLinus Torvalds sbi->prev_free = FAT_START_ENT; 1657710d4403SNamjae Jeon sb->s_maxbytes = 0xffffffff; 1658c0da64f6SDeepa Dinamani fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0); 1659c0da64f6SDeepa Dinamani sb->s_time_min = ts.tv_sec; 1660c0da64f6SDeepa Dinamani 1661c0da64f6SDeepa Dinamani fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX), 1662c0da64f6SDeepa Dinamani cpu_to_le16(FAT_DATE_MAX), 0); 1663c0da64f6SDeepa Dinamani sb->s_time_max = ts.tv_sec; 16641da177e4SLinus Torvalds 1665190a8843SConrad Meyer if (!sbi->fat_length && bpb.fat32_length) { 16661da177e4SLinus Torvalds struct fat_boot_fsinfo *fsinfo; 16671da177e4SLinus Torvalds struct buffer_head *fsinfo_bh; 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds /* Must be FAT32 */ 16701da177e4SLinus Torvalds sbi->fat_bits = 32; 1671190a8843SConrad Meyer sbi->fat_length = bpb.fat32_length; 1672190a8843SConrad Meyer sbi->root_cluster = bpb.fat32_root_cluster; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* MC - if info_sector is 0, don't multiply by 0 */ 1675190a8843SConrad Meyer sbi->fsinfo_sector = bpb.fat32_info_sector; 16761da177e4SLinus Torvalds if (sbi->fsinfo_sector == 0) 16771da177e4SLinus Torvalds sbi->fsinfo_sector = 1; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector); 16801da177e4SLinus Torvalds if (fsinfo_bh == NULL) { 1681869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "bread failed, FSINFO block" 1682869f58c0SAlexey Fisher " (sector = %lu)", sbi->fsinfo_sector); 16831da177e4SLinus Torvalds goto out_fail; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; 16871da177e4SLinus Torvalds if (!IS_FSINFO(fsinfo)) { 1688869f58c0SAlexey Fisher fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: " 1689869f58c0SAlexey Fisher "0x%08x, 0x%08x (sector = %lu)", 16901da177e4SLinus Torvalds le32_to_cpu(fsinfo->signature1), 16911da177e4SLinus Torvalds le32_to_cpu(fsinfo->signature2), 16921da177e4SLinus Torvalds sbi->fsinfo_sector); 16931da177e4SLinus Torvalds } else { 169428ec039cSOGAWA Hirofumi if (sbi->options.usefree) 1695606e423eSOGAWA Hirofumi sbi->free_clus_valid = 1; 1696606e423eSOGAWA Hirofumi sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters); 16971da177e4SLinus Torvalds sbi->prev_free = le32_to_cpu(fsinfo->next_cluster); 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds brelse(fsinfo_bh); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17036e5b93eeSMike Lockwood /* interpret volume ID as a little endian 32 bit integer */ 1704306790f7SCarmeli Tamir if (is_fat32(sbi)) 1705190a8843SConrad Meyer sbi->vol_id = bpb.fat32_vol_id; 17066e5b93eeSMike Lockwood else /* fat 16 or 12 */ 1707190a8843SConrad Meyer sbi->vol_id = bpb.fat16_vol_id; 17086e5b93eeSMike Lockwood 1709e2f7dd6eSKent Overstreet __le32 vol_id_le = cpu_to_le32(sbi->vol_id); 1710e2f7dd6eSKent Overstreet super_set_uuid(sb, (void *) &vol_id_le, sizeof(vol_id_le)); 1711e2f7dd6eSKent Overstreet 17121da177e4SLinus Torvalds sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); 17131da177e4SLinus Torvalds sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; 1716190a8843SConrad Meyer sbi->dir_entries = bpb.fat_dir_entries; 17171da177e4SLinus Torvalds if (sbi->dir_entries & (sbi->dir_per_block - 1)) { 17181da177e4SLinus Torvalds if (!silent) 171934df1174SZheng Lv fat_msg(sb, KERN_ERR, "bogus number of directory entries" 1720869f58c0SAlexey Fisher " (%u)", sbi->dir_entries); 17211da177e4SLinus Torvalds goto out_invalid; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds rootdir_sectors = sbi->dir_entries 17251da177e4SLinus Torvalds * sizeof(struct msdos_dir_entry) / sb->s_blocksize; 17261da177e4SLinus Torvalds sbi->data_start = sbi->dir_start + rootdir_sectors; 1727190a8843SConrad Meyer total_sectors = bpb.fat_sectors; 17281da177e4SLinus Torvalds if (total_sectors == 0) 1729190a8843SConrad Meyer total_sectors = bpb.fat_total_sect; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; 17321da177e4SLinus Torvalds 1733306790f7SCarmeli Tamir if (!is_fat32(sbi)) 17341da177e4SLinus Torvalds sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; 17351da177e4SLinus Torvalds 1736b88a1058SOleksij Rempel /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */ 1737306790f7SCarmeli Tamir if (is_fat32(sbi)) 1738190a8843SConrad Meyer sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY; 1739b88a1058SOleksij Rempel else /* fat 16 or 12 */ 1740190a8843SConrad Meyer sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY; 1741b88a1058SOleksij Rempel 17421da177e4SLinus Torvalds /* check that FAT table does not overflow */ 17437b92d03cSOGAWA Hirofumi fat_clusters = calc_fat_clusters(sb); 17441da177e4SLinus Torvalds total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); 1745d19dc016SCarmeli Tamir if (total_clusters > max_fat(sb)) { 17461da177e4SLinus Torvalds if (!silent) 1747869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "count of clusters too big (%u)", 17481da177e4SLinus Torvalds total_clusters); 17491da177e4SLinus Torvalds goto out_invalid; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds sbi->max_cluster = total_clusters + FAT_START_ENT; 17531da177e4SLinus Torvalds /* check the free_clusters, it's not necessarily correct */ 17541da177e4SLinus Torvalds if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters) 17551da177e4SLinus Torvalds sbi->free_clusters = -1; 17561da177e4SLinus Torvalds /* check the prev_free, it's not necessarily correct */ 17571da177e4SLinus Torvalds sbi->prev_free %= sbi->max_cluster; 17581da177e4SLinus Torvalds if (sbi->prev_free < FAT_START_ENT) 17591da177e4SLinus Torvalds sbi->prev_free = FAT_START_ENT; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* set up enough so that it can read an inode */ 17621da177e4SLinus Torvalds fat_hash_init(sb); 17637669e8fbSSteven J. Magnani dir_hash_init(sb); 17641da177e4SLinus Torvalds fat_ent_access_init(sb); 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* 1767b553337aSCarmeli Tamir * The low byte of the first FAT entry must have the same value as 1768b553337aSCarmeli Tamir * the media field of the boot sector. But in real world, too many 1769b553337aSCarmeli Tamir * devices are writing wrong values. So, removed that validity check. 17701da177e4SLinus Torvalds * 1771b553337aSCarmeli Tamir * The removed check compared the first FAT entry to a value dependent 1772b553337aSCarmeli Tamir * on the media field like this: 1773b553337aSCarmeli Tamir * == (0x0F00 | media), for FAT12 1774b553337aSCarmeli Tamir * == (0XFF00 | media), for FAT16 1775b553337aSCarmeli Tamir * == (0x0FFFFF | media), for FAT32 17761da177e4SLinus Torvalds */ 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds error = -EINVAL; 17791da177e4SLinus Torvalds sprintf(buf, "cp%d", sbi->options.codepage); 17801da177e4SLinus Torvalds sbi->nls_disk = load_nls(buf); 17811da177e4SLinus Torvalds if (!sbi->nls_disk) { 1782869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "codepage %s not found", buf); 17831da177e4SLinus Torvalds goto out_fail; 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds /* FIXME: utf8 is using iocharset for upper/lower conversion */ 17871da177e4SLinus Torvalds if (sbi->options.isvfat) { 17881da177e4SLinus Torvalds sbi->nls_io = load_nls(sbi->options.iocharset); 17891da177e4SLinus Torvalds if (!sbi->nls_io) { 1790869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "IO charset %s not found", 17911da177e4SLinus Torvalds sbi->options.iocharset); 17921da177e4SLinus Torvalds goto out_fail; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds } 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds error = -ENOMEM; 1797b522412aSAl Viro fat_inode = new_inode(sb); 1798b522412aSAl Viro if (!fat_inode) 1799b522412aSAl Viro goto out_fail; 1800b522412aSAl Viro sbi->fat_inode = fat_inode; 1801020ac5b6SArtem Bityutskiy 1802020ac5b6SArtem Bityutskiy fsinfo_inode = new_inode(sb); 1803020ac5b6SArtem Bityutskiy if (!fsinfo_inode) 1804020ac5b6SArtem Bityutskiy goto out_fail; 1805020ac5b6SArtem Bityutskiy fsinfo_inode->i_ino = MSDOS_FSINFO_INO; 1806020ac5b6SArtem Bityutskiy sbi->fsinfo_inode = fsinfo_inode; 1807020ac5b6SArtem Bityutskiy insert_inode_hash(fsinfo_inode); 1808020ac5b6SArtem Bityutskiy 18091da177e4SLinus Torvalds root_inode = new_inode(sb); 18101da177e4SLinus Torvalds if (!root_inode) 18111da177e4SLinus Torvalds goto out_fail; 18121da177e4SLinus Torvalds root_inode->i_ino = MSDOS_ROOT_INO; 18132489dbabSJeff Layton inode_set_iversion(root_inode, 1); 18141da177e4SLinus Torvalds error = fat_read_root(root_inode); 18151688f860SAl Viro if (error < 0) { 18161688f860SAl Viro iput(root_inode); 18171da177e4SLinus Torvalds goto out_fail; 18181688f860SAl Viro } 18191da177e4SLinus Torvalds error = -ENOMEM; 18201da177e4SLinus Torvalds insert_inode_hash(root_inode); 18217669e8fbSSteven J. Magnani fat_attach(root_inode, 0); 18221688f860SAl Viro sb->s_root = d_make_root(root_inode); 18231da177e4SLinus Torvalds if (!sb->s_root) { 1824869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "get root inode failed"); 18251da177e4SLinus Torvalds goto out_fail; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 182870200574SChristoph Hellwig if (sbi->options.discard && !bdev_max_discard_sectors(sb->s_bdev)) 1829f562146aSNamjae Jeon fat_msg(sb, KERN_WARNING, 183070200574SChristoph Hellwig "mounting with \"discard\" option, but the device does not support discard"); 1831f562146aSNamjae Jeon 1832b88a1058SOleksij Rempel fat_set_state(sb, 1, 0); 18331da177e4SLinus Torvalds return 0; 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds out_invalid: 18361da177e4SLinus Torvalds error = -EINVAL; 18371da177e4SLinus Torvalds if (!silent) 1838869f58c0SAlexey Fisher fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem"); 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds out_fail: 1841020ac5b6SArtem Bityutskiy iput(fsinfo_inode); 1842b522412aSAl Viro iput(fat_inode); 18431da177e4SLinus Torvalds unload_nls(sbi->nls_io); 18441da177e4SLinus Torvalds unload_nls(sbi->nls_disk); 184535033ab9SOGAWA Hirofumi fat_reset_iocharset(&sbi->options); 18461da177e4SLinus Torvalds sb->s_fs_info = NULL; 18471da177e4SLinus Torvalds kfree(sbi); 18481da177e4SLinus Torvalds return error; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18517c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_fill_super); 18521da177e4SLinus Torvalds 1853ae78bf9cSChris Mason /* 1854ae78bf9cSChris Mason * helper function for fat_flush_inodes. This writes both the inode 1855ae78bf9cSChris Mason * and the file data blocks, waiting for in flight data blocks before 1856ae78bf9cSChris Mason * the start of the call. It does not wait for any io started 1857ae78bf9cSChris Mason * during the call 1858ae78bf9cSChris Mason */ 1859ae78bf9cSChris Mason static int writeback_inode(struct inode *inode) 1860ae78bf9cSChris Mason { 1861ae78bf9cSChris Mason 1862ae78bf9cSChris Mason int ret; 186314864655SNamjae Jeon 186414864655SNamjae Jeon /* if we used wait=1, sync_inode_metadata waits for the io for the 186514864655SNamjae Jeon * inode to finish. So wait=0 is sent down to sync_inode_metadata 1866ae78bf9cSChris Mason * and filemap_fdatawrite is used for the data blocks 1867ae78bf9cSChris Mason */ 186814864655SNamjae Jeon ret = sync_inode_metadata(inode, 0); 1869ae78bf9cSChris Mason if (!ret) 187014864655SNamjae Jeon ret = filemap_fdatawrite(inode->i_mapping); 1871ae78bf9cSChris Mason return ret; 1872ae78bf9cSChris Mason } 1873ae78bf9cSChris Mason 1874ae78bf9cSChris Mason /* 1875ae78bf9cSChris Mason * write data and metadata corresponding to i1 and i2. The io is 1876ae78bf9cSChris Mason * started but we do not wait for any of it to finish. 1877ae78bf9cSChris Mason * 1878ae78bf9cSChris Mason * filemap_flush is used for the block device, so if there is a dirty 1879ae78bf9cSChris Mason * page for a block already in flight, we will not wait and start the 1880ae78bf9cSChris Mason * io over again 1881ae78bf9cSChris Mason */ 1882ae78bf9cSChris Mason int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2) 1883ae78bf9cSChris Mason { 1884ae78bf9cSChris Mason int ret = 0; 1885ae78bf9cSChris Mason if (!MSDOS_SB(sb)->options.flush) 1886ae78bf9cSChris Mason return 0; 1887ae78bf9cSChris Mason if (i1) 1888ae78bf9cSChris Mason ret = writeback_inode(i1); 1889ae78bf9cSChris Mason if (!ret && i2) 1890ae78bf9cSChris Mason ret = writeback_inode(i2); 1891cb9568eeSChristoph Hellwig if (!ret) 1892cb9568eeSChristoph Hellwig ret = sync_blockdev_nowait(sb->s_bdev); 1893ae78bf9cSChris Mason return ret; 1894ae78bf9cSChris Mason } 1895ae78bf9cSChris Mason EXPORT_SYMBOL_GPL(fat_flush_inodes); 1896ae78bf9cSChris Mason 1897634440b6SEric Sandeen int fat_init_fs_context(struct fs_context *fc, bool is_vfat) 1898634440b6SEric Sandeen { 1899634440b6SEric Sandeen struct fat_mount_options *opts; 1900634440b6SEric Sandeen 1901634440b6SEric Sandeen opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1902634440b6SEric Sandeen if (!opts) 1903634440b6SEric Sandeen return -ENOMEM; 1904634440b6SEric Sandeen 1905634440b6SEric Sandeen opts->isvfat = is_vfat; 1906634440b6SEric Sandeen opts->fs_uid = current_uid(); 1907634440b6SEric Sandeen opts->fs_gid = current_gid(); 1908634440b6SEric Sandeen opts->fs_fmask = opts->fs_dmask = current_umask(); 1909634440b6SEric Sandeen opts->allow_utime = -1; 1910634440b6SEric Sandeen opts->codepage = fat_default_codepage; 1911634440b6SEric Sandeen fat_reset_iocharset(opts); 1912634440b6SEric Sandeen if (is_vfat) { 1913634440b6SEric Sandeen opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95; 1914634440b6SEric Sandeen opts->rodir = 0; 1915634440b6SEric Sandeen } else { 1916634440b6SEric Sandeen opts->shortname = 0; 1917634440b6SEric Sandeen opts->rodir = 1; 1918634440b6SEric Sandeen } 1919634440b6SEric Sandeen opts->name_check = 'n'; 1920634440b6SEric Sandeen opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; 1921634440b6SEric Sandeen opts->unicode_xlate = 0; 1922634440b6SEric Sandeen opts->numtail = 1; 1923634440b6SEric Sandeen opts->usefree = opts->nocase = 0; 1924634440b6SEric Sandeen opts->tz_set = 0; 1925634440b6SEric Sandeen opts->nfs = 0; 1926634440b6SEric Sandeen opts->errors = FAT_ERRORS_RO; 1927634440b6SEric Sandeen opts->debug = 0; 1928634440b6SEric Sandeen 1929634440b6SEric Sandeen opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat; 1930634440b6SEric Sandeen 1931634440b6SEric Sandeen fc->fs_private = opts; 1932634440b6SEric Sandeen /* fc->ops assigned by caller */ 1933634440b6SEric Sandeen 1934634440b6SEric Sandeen return 0; 1935634440b6SEric Sandeen } 1936634440b6SEric Sandeen EXPORT_SYMBOL_GPL(fat_init_fs_context); 1937634440b6SEric Sandeen 1938634440b6SEric Sandeen void fat_free_fc(struct fs_context *fc) 1939634440b6SEric Sandeen { 1940634440b6SEric Sandeen struct fat_mount_options *opts = fc->fs_private; 1941634440b6SEric Sandeen 1942634440b6SEric Sandeen if (opts->iocharset != fat_default_iocharset) 1943634440b6SEric Sandeen kfree(opts->iocharset); 1944634440b6SEric Sandeen kfree(fc->fs_private); 1945634440b6SEric Sandeen } 1946634440b6SEric Sandeen EXPORT_SYMBOL_GPL(fat_free_fc); 1947634440b6SEric Sandeen 19481da177e4SLinus Torvalds static int __init init_fat_fs(void) 19491da177e4SLinus Torvalds { 1950532a39a3SPekka J Enberg int err; 19511da177e4SLinus Torvalds 1952532a39a3SPekka J Enberg err = fat_cache_init(); 1953532a39a3SPekka J Enberg if (err) 1954532a39a3SPekka J Enberg return err; 1955532a39a3SPekka J Enberg 1956532a39a3SPekka J Enberg err = fat_init_inodecache(); 1957532a39a3SPekka J Enberg if (err) 1958532a39a3SPekka J Enberg goto failed; 1959532a39a3SPekka J Enberg 1960532a39a3SPekka J Enberg return 0; 1961532a39a3SPekka J Enberg 1962532a39a3SPekka J Enberg failed: 1963532a39a3SPekka J Enberg fat_cache_destroy(); 1964532a39a3SPekka J Enberg return err; 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds static void __exit exit_fat_fs(void) 19681da177e4SLinus Torvalds { 19691da177e4SLinus Torvalds fat_cache_destroy(); 19701da177e4SLinus Torvalds fat_destroy_inodecache(); 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds module_init(init_fat_fs) 19741da177e4SLinus Torvalds module_exit(exit_fat_fs) 19751da177e4SLinus Torvalds 1976c8e0f1ecSJeff Johnson MODULE_DESCRIPTION("Core FAT filesystem support"); 19771da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1978