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> 191da177e4SLinus Torvalds #include <linux/parser.h> 20e5174baaSOGAWA Hirofumi #include <linux/uio.h> 21f562146aSNamjae Jeon #include <linux/blkdev.h> 2266114cadSTejun Heo #include <linux/backing-dev.h> 231da177e4SLinus Torvalds #include <asm/unaligned.h> 2474b5cab6SArnd Bergmann #include <linux/random.h> 252489dbabSJeff Layton #include <linux/iversion.h> 269e975daeSOGAWA Hirofumi #include "fat.h" 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #ifndef CONFIG_FAT_DEFAULT_IOCHARSET 291da177e4SLinus Torvalds /* if user don't select VFAT, this is undefined. */ 301da177e4SLinus Torvalds #define CONFIG_FAT_DEFAULT_IOCHARSET "" 311da177e4SLinus Torvalds #endif 321da177e4SLinus Torvalds 33190a8843SConrad Meyer #define KB_IN_SECTORS 2 34190a8843SConrad Meyer 35c0da64f6SDeepa Dinamani /* DOS dates from 1980/1/1 through 2107/12/31 */ 36c0da64f6SDeepa Dinamani #define FAT_DATE_MIN (0<<9 | 1<<5 | 1) 37c0da64f6SDeepa Dinamani #define FAT_DATE_MAX (127<<9 | 12<<5 | 31) 38c0da64f6SDeepa Dinamani #define FAT_TIME_MAX (23<<11 | 59<<5 | 29) 39c0da64f6SDeepa Dinamani 40190a8843SConrad Meyer /* 41190a8843SConrad Meyer * A deserialized copy of the on-disk structure laid out in struct 42190a8843SConrad Meyer * fat_boot_sector. 43190a8843SConrad Meyer */ 44190a8843SConrad Meyer struct fat_bios_param_block { 45190a8843SConrad Meyer u16 fat_sector_size; 46190a8843SConrad Meyer u8 fat_sec_per_clus; 47190a8843SConrad Meyer u16 fat_reserved; 48190a8843SConrad Meyer u8 fat_fats; 49190a8843SConrad Meyer u16 fat_dir_entries; 50190a8843SConrad Meyer u16 fat_sectors; 51190a8843SConrad Meyer u16 fat_fat_length; 52190a8843SConrad Meyer u32 fat_total_sect; 53190a8843SConrad Meyer 54190a8843SConrad Meyer u8 fat16_state; 55190a8843SConrad Meyer u32 fat16_vol_id; 56190a8843SConrad Meyer 57190a8843SConrad Meyer u32 fat32_length; 58190a8843SConrad Meyer u32 fat32_root_cluster; 59190a8843SConrad Meyer u16 fat32_info_sector; 60190a8843SConrad Meyer u8 fat32_state; 61190a8843SConrad Meyer u32 fat32_vol_id; 62190a8843SConrad Meyer }; 63190a8843SConrad Meyer 641da177e4SLinus Torvalds static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; 651da177e4SLinus Torvalds static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; 661da177e4SLinus Torvalds 67190a8843SConrad Meyer static struct fat_floppy_defaults { 68190a8843SConrad Meyer unsigned nr_sectors; 69190a8843SConrad Meyer unsigned sec_per_clus; 70190a8843SConrad Meyer unsigned dir_entries; 71190a8843SConrad Meyer unsigned media; 72190a8843SConrad Meyer unsigned fat_length; 73190a8843SConrad Meyer } floppy_defaults[] = { 74190a8843SConrad Meyer { 75190a8843SConrad Meyer .nr_sectors = 160 * KB_IN_SECTORS, 76190a8843SConrad Meyer .sec_per_clus = 1, 77190a8843SConrad Meyer .dir_entries = 64, 78190a8843SConrad Meyer .media = 0xFE, 79190a8843SConrad Meyer .fat_length = 1, 80190a8843SConrad Meyer }, 81190a8843SConrad Meyer { 82190a8843SConrad Meyer .nr_sectors = 180 * KB_IN_SECTORS, 83190a8843SConrad Meyer .sec_per_clus = 1, 84190a8843SConrad Meyer .dir_entries = 64, 85190a8843SConrad Meyer .media = 0xFC, 86190a8843SConrad Meyer .fat_length = 2, 87190a8843SConrad Meyer }, 88190a8843SConrad Meyer { 89190a8843SConrad Meyer .nr_sectors = 320 * KB_IN_SECTORS, 90190a8843SConrad Meyer .sec_per_clus = 2, 91190a8843SConrad Meyer .dir_entries = 112, 92190a8843SConrad Meyer .media = 0xFF, 93190a8843SConrad Meyer .fat_length = 1, 94190a8843SConrad Meyer }, 95190a8843SConrad Meyer { 96190a8843SConrad Meyer .nr_sectors = 360 * KB_IN_SECTORS, 97190a8843SConrad Meyer .sec_per_clus = 2, 98190a8843SConrad Meyer .dir_entries = 112, 99190a8843SConrad Meyer .media = 0xFD, 100190a8843SConrad Meyer .fat_length = 2, 101190a8843SConrad Meyer }, 102190a8843SConrad Meyer }; 1031da177e4SLinus Torvalds 104b13bb33eSNamjae Jeon int fat_add_cluster(struct inode *inode) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds int err, cluster; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds err = fat_alloc_clusters(inode, &cluster, 1); 1091da177e4SLinus Torvalds if (err) 1101da177e4SLinus Torvalds return err; 1111da177e4SLinus Torvalds /* FIXME: this cluster should be added after data of this 1121da177e4SLinus Torvalds * cluster is writed */ 1131da177e4SLinus Torvalds err = fat_chain_add(inode, cluster, 1); 1141da177e4SLinus Torvalds if (err) 1151da177e4SLinus Torvalds fat_free_clusters(inode, cluster); 1161da177e4SLinus Torvalds return err; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1196a1d9805SOGAWA Hirofumi static inline int __fat_get_block(struct inode *inode, sector_t iblock, 120e5174baaSOGAWA Hirofumi unsigned long *max_blocks, 1211da177e4SLinus Torvalds struct buffer_head *bh_result, int create) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 124e5174baaSOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 125e5174baaSOGAWA Hirofumi unsigned long mapped_blocks; 1267e0f236bSNamjae Jeon sector_t phys, last_block; 127e5174baaSOGAWA Hirofumi int err, offset; 1281da177e4SLinus Torvalds 12916fab201SNamjae Jeon err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); 1301da177e4SLinus Torvalds if (err) 1311da177e4SLinus Torvalds return err; 1321da177e4SLinus Torvalds if (phys) { 1331da177e4SLinus Torvalds map_bh(bh_result, sb, phys); 134e5174baaSOGAWA Hirofumi *max_blocks = min(mapped_blocks, *max_blocks); 1351da177e4SLinus Torvalds return 0; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds if (!create) 1381da177e4SLinus Torvalds return 0; 139e5174baaSOGAWA Hirofumi 1401da177e4SLinus Torvalds if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { 14185c78591SDenis Karpov fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)", 1421da177e4SLinus Torvalds MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); 1431da177e4SLinus Torvalds return -EIO; 1441da177e4SLinus Torvalds } 145e5174baaSOGAWA Hirofumi 1467e0f236bSNamjae Jeon last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9); 147e5174baaSOGAWA Hirofumi offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); 1487e0f236bSNamjae Jeon /* 1497e0f236bSNamjae Jeon * allocate a cluster according to the following. 1507e0f236bSNamjae Jeon * 1) no more available blocks 1517e0f236bSNamjae Jeon * 2) not part of fallocate region 1527e0f236bSNamjae Jeon */ 1537e0f236bSNamjae Jeon if (!offset && !(iblock < last_block)) { 154e5174baaSOGAWA Hirofumi /* TODO: multiple cluster allocation would be desirable. */ 1551da177e4SLinus Torvalds err = fat_add_cluster(inode); 1561da177e4SLinus Torvalds if (err) 1571da177e4SLinus Torvalds return err; 1581da177e4SLinus Torvalds } 159e5174baaSOGAWA Hirofumi /* available blocks on this cluster */ 160e5174baaSOGAWA Hirofumi mapped_blocks = sbi->sec_per_clus - offset; 161e5174baaSOGAWA Hirofumi 162e5174baaSOGAWA Hirofumi *max_blocks = min(mapped_blocks, *max_blocks); 163e5174baaSOGAWA Hirofumi MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; 164e5174baaSOGAWA Hirofumi 16516fab201SNamjae Jeon err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); 1661da177e4SLinus Torvalds if (err) 1671da177e4SLinus Torvalds return err; 168c2574aaaSOGAWA Hirofumi if (!phys) { 169c2574aaaSOGAWA Hirofumi fat_fs_error(sb, 170c2574aaaSOGAWA Hirofumi "invalid FAT chain (i_pos %lld, last_block %llu)", 171c2574aaaSOGAWA Hirofumi MSDOS_I(inode)->i_pos, 172c2574aaaSOGAWA Hirofumi (unsigned long long)last_block); 173c2574aaaSOGAWA Hirofumi return -EIO; 174c2574aaaSOGAWA Hirofumi } 1756a1d9805SOGAWA Hirofumi 176e5174baaSOGAWA Hirofumi BUG_ON(*max_blocks != mapped_blocks); 1771da177e4SLinus Torvalds set_buffer_new(bh_result); 1781da177e4SLinus Torvalds map_bh(bh_result, sb, phys); 1791da177e4SLinus Torvalds 180e5174baaSOGAWA Hirofumi return 0; 181e5174baaSOGAWA Hirofumi } 182e5174baaSOGAWA Hirofumi 183e5174baaSOGAWA Hirofumi static int fat_get_block(struct inode *inode, sector_t iblock, 184e5174baaSOGAWA Hirofumi struct buffer_head *bh_result, int create) 185e5174baaSOGAWA Hirofumi { 1866a1d9805SOGAWA Hirofumi struct super_block *sb = inode->i_sb; 1876a1d9805SOGAWA Hirofumi unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 1886a1d9805SOGAWA Hirofumi int err; 1896a1d9805SOGAWA Hirofumi 1906a1d9805SOGAWA Hirofumi err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); 1916a1d9805SOGAWA Hirofumi if (err) 1926a1d9805SOGAWA Hirofumi return err; 1936a1d9805SOGAWA Hirofumi bh_result->b_size = max_blocks << sb->s_blocksize_bits; 1946a1d9805SOGAWA Hirofumi return 0; 195e5174baaSOGAWA Hirofumi } 196e5174baaSOGAWA Hirofumi 1971da177e4SLinus Torvalds static int fat_writepage(struct page *page, struct writeback_control *wbc) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds return block_write_full_page(page, fat_get_block, wbc); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202a5425d29SOGAWA Hirofumi static int fat_writepages(struct address_space *mapping, 203a5425d29SOGAWA Hirofumi struct writeback_control *wbc) 204a5425d29SOGAWA Hirofumi { 205a5425d29SOGAWA Hirofumi return mpage_writepages(mapping, wbc, fat_get_block); 206a5425d29SOGAWA Hirofumi } 207a5425d29SOGAWA Hirofumi 2081da177e4SLinus Torvalds static int fat_readpage(struct file *file, struct page *page) 2091da177e4SLinus Torvalds { 210a5425d29SOGAWA Hirofumi return mpage_readpage(page, fat_get_block); 211a5425d29SOGAWA Hirofumi } 212a5425d29SOGAWA Hirofumi 213d4388340SMatthew Wilcox (Oracle) static void fat_readahead(struct readahead_control *rac) 214a5425d29SOGAWA Hirofumi { 215d4388340SMatthew Wilcox (Oracle) mpage_readahead(rac, fat_get_block); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 218459f6ed3Snpiggin@suse.de static void fat_write_failed(struct address_space *mapping, loff_t to) 219459f6ed3Snpiggin@suse.de { 220459f6ed3Snpiggin@suse.de struct inode *inode = mapping->host; 221459f6ed3Snpiggin@suse.de 222459f6ed3Snpiggin@suse.de if (to > inode->i_size) { 2237caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 224459f6ed3Snpiggin@suse.de fat_truncate_blocks(inode, inode->i_size); 225459f6ed3Snpiggin@suse.de } 226459f6ed3Snpiggin@suse.de } 227459f6ed3Snpiggin@suse.de 228d7777a25SNick Piggin static int fat_write_begin(struct file *file, struct address_space *mapping, 229d7777a25SNick Piggin loff_t pos, unsigned len, unsigned flags, 230d7777a25SNick Piggin struct page **pagep, void **fsdata) 2311da177e4SLinus Torvalds { 232459f6ed3Snpiggin@suse.de int err; 233459f6ed3Snpiggin@suse.de 234d7777a25SNick Piggin *pagep = NULL; 235282dc178SChristoph Hellwig err = cont_write_begin(file, mapping, pos, len, flags, 236459f6ed3Snpiggin@suse.de pagep, fsdata, fat_get_block, 237d7777a25SNick Piggin &MSDOS_I(mapping->host)->mmu_private); 238459f6ed3Snpiggin@suse.de if (err < 0) 239459f6ed3Snpiggin@suse.de fat_write_failed(mapping, pos + len); 240459f6ed3Snpiggin@suse.de return err; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243d7777a25SNick Piggin static int fat_write_end(struct file *file, struct address_space *mapping, 244d7777a25SNick Piggin loff_t pos, unsigned len, unsigned copied, 245d7777a25SNick Piggin struct page *pagep, void *fsdata) 246ef402268SOGAWA Hirofumi { 247d7777a25SNick Piggin struct inode *inode = mapping->host; 248d7777a25SNick Piggin int err; 249d7777a25SNick Piggin err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); 250459f6ed3Snpiggin@suse.de if (err < len) 251459f6ed3Snpiggin@suse.de fat_write_failed(mapping, pos + len); 252d7777a25SNick Piggin if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { 253cd83f6b1SFrank Sorenson fat_truncate_time(inode, NULL, S_CTIME|S_MTIME); 254ef402268SOGAWA Hirofumi MSDOS_I(inode)->i_attrs |= ATTR_ARCH; 255ef402268SOGAWA Hirofumi mark_inode_dirty(inode); 256ef402268SOGAWA Hirofumi } 257ef402268SOGAWA Hirofumi return err; 258ef402268SOGAWA Hirofumi } 259ef402268SOGAWA Hirofumi 260c8b8e32dSChristoph Hellwig static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 261e5174baaSOGAWA Hirofumi { 262e5174baaSOGAWA Hirofumi struct file *file = iocb->ki_filp; 263459f6ed3Snpiggin@suse.de struct address_space *mapping = file->f_mapping; 264459f6ed3Snpiggin@suse.de struct inode *inode = mapping->host; 265a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 266c8b8e32dSChristoph Hellwig loff_t offset = iocb->ki_pos; 267459f6ed3Snpiggin@suse.de ssize_t ret; 268e5174baaSOGAWA Hirofumi 2696f673763SOmar Sandoval if (iov_iter_rw(iter) == WRITE) { 270e5174baaSOGAWA Hirofumi /* 2714e02ed4bSNick Piggin * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 272e5174baaSOGAWA Hirofumi * so we need to update the ->mmu_private to block boundary. 273e5174baaSOGAWA Hirofumi * 274e5174baaSOGAWA Hirofumi * But we must fill the remaining area or hole by nul for 275e5174baaSOGAWA Hirofumi * updating ->mmu_private. 27694412a96SOGAWA Hirofumi * 27794412a96SOGAWA Hirofumi * Return 0, and fallback to normal buffered write. 278e5174baaSOGAWA Hirofumi */ 279a6cbcd4aSAl Viro loff_t size = offset + count; 280e5174baaSOGAWA Hirofumi if (MSDOS_I(inode)->mmu_private < size) 28194412a96SOGAWA Hirofumi return 0; 282e5174baaSOGAWA Hirofumi } 283e5174baaSOGAWA Hirofumi 284e5174baaSOGAWA Hirofumi /* 285e5174baaSOGAWA Hirofumi * FAT need to use the DIO_LOCKING for avoiding the race 286e5174baaSOGAWA Hirofumi * condition of fat_get_block() and ->truncate(). 287e5174baaSOGAWA Hirofumi */ 288c8b8e32dSChristoph Hellwig ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block); 2896f673763SOmar Sandoval if (ret < 0 && iov_iter_rw(iter) == WRITE) 290a6cbcd4aSAl Viro fat_write_failed(mapping, offset + count); 291459f6ed3Snpiggin@suse.de 292459f6ed3Snpiggin@suse.de return ret; 293e5174baaSOGAWA Hirofumi } 294e5174baaSOGAWA Hirofumi 29516fab201SNamjae Jeon static int fat_get_block_bmap(struct inode *inode, sector_t iblock, 29616fab201SNamjae Jeon struct buffer_head *bh_result, int create) 29716fab201SNamjae Jeon { 29816fab201SNamjae Jeon struct super_block *sb = inode->i_sb; 29916fab201SNamjae Jeon unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 30016fab201SNamjae Jeon int err; 30116fab201SNamjae Jeon sector_t bmap; 30216fab201SNamjae Jeon unsigned long mapped_blocks; 30316fab201SNamjae Jeon 30416fab201SNamjae Jeon BUG_ON(create != 0); 30516fab201SNamjae Jeon 30616fab201SNamjae Jeon err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true); 30716fab201SNamjae Jeon if (err) 30816fab201SNamjae Jeon return err; 30916fab201SNamjae Jeon 31016fab201SNamjae Jeon if (bmap) { 31116fab201SNamjae Jeon map_bh(bh_result, sb, bmap); 31216fab201SNamjae Jeon max_blocks = min(mapped_blocks, max_blocks); 31316fab201SNamjae Jeon } 31416fab201SNamjae Jeon 31516fab201SNamjae Jeon bh_result->b_size = max_blocks << sb->s_blocksize_bits; 31616fab201SNamjae Jeon 31716fab201SNamjae Jeon return 0; 31816fab201SNamjae Jeon } 31916fab201SNamjae Jeon 3201da177e4SLinus Torvalds static sector_t _fat_bmap(struct address_space *mapping, sector_t block) 3211da177e4SLinus Torvalds { 322fa93ca18SOGAWA Hirofumi sector_t blocknr; 323fa93ca18SOGAWA Hirofumi 324fa93ca18SOGAWA Hirofumi /* fat_get_cluster() assumes the requested blocknr isn't truncated. */ 32558268691SChristoph Hellwig down_read(&MSDOS_I(mapping->host)->truncate_lock); 32616fab201SNamjae Jeon blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap); 32758268691SChristoph Hellwig up_read(&MSDOS_I(mapping->host)->truncate_lock); 328fa93ca18SOGAWA Hirofumi 329fa93ca18SOGAWA Hirofumi return blocknr; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332c0ef0cc9SNamjae Jeon /* 333c0ef0cc9SNamjae Jeon * fat_block_truncate_page() zeroes out a mapping from file offset `from' 334c0ef0cc9SNamjae Jeon * up to the end of the block which corresponds to `from'. 335c0ef0cc9SNamjae Jeon * This is required during truncate to physically zeroout the tail end 336c0ef0cc9SNamjae Jeon * of that block so it doesn't yield old data if the file is later grown. 337c0ef0cc9SNamjae Jeon * Also, avoid causing failure from fsx for cases of "data past EOF" 338c0ef0cc9SNamjae Jeon */ 339c0ef0cc9SNamjae Jeon int fat_block_truncate_page(struct inode *inode, loff_t from) 340c0ef0cc9SNamjae Jeon { 341c0ef0cc9SNamjae Jeon return block_truncate_page(inode->i_mapping, from, fat_get_block); 342c0ef0cc9SNamjae Jeon } 343c0ef0cc9SNamjae Jeon 344f5e54d6eSChristoph Hellwig static const struct address_space_operations fat_aops = { 3450af57378SChristoph Hellwig .set_page_dirty = __set_page_dirty_buffers, 346*7ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = block_invalidate_folio, 3471da177e4SLinus Torvalds .readpage = fat_readpage, 348d4388340SMatthew Wilcox (Oracle) .readahead = fat_readahead, 3491da177e4SLinus Torvalds .writepage = fat_writepage, 350a5425d29SOGAWA Hirofumi .writepages = fat_writepages, 351d7777a25SNick Piggin .write_begin = fat_write_begin, 352d7777a25SNick Piggin .write_end = fat_write_end, 353e5174baaSOGAWA Hirofumi .direct_IO = fat_direct_IO, 3541da177e4SLinus Torvalds .bmap = _fat_bmap 3551da177e4SLinus Torvalds }; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * New FAT inode stuff. We do the following: 3591da177e4SLinus Torvalds * a) i_ino is constant and has nothing with on-disk location. 3601da177e4SLinus Torvalds * b) FAT manages its own cache of directory entries. 3611da177e4SLinus Torvalds * c) *This* cache is indexed by on-disk location. 3621da177e4SLinus Torvalds * d) inode has an associated directory entry, all right, but 3631da177e4SLinus Torvalds * it may be unhashed. 3641da177e4SLinus Torvalds * e) currently entries are stored within struct inode. That should 3651da177e4SLinus Torvalds * change. 3661da177e4SLinus Torvalds * f) we deal with races in the following way: 3671da177e4SLinus Torvalds * 1. readdir() and lookup() do FAT-dir-cache lookup. 3681da177e4SLinus Torvalds * 2. rename() unhashes the F-d-c entry and rehashes it in 3691da177e4SLinus Torvalds * a new place. 3701da177e4SLinus Torvalds * 3. unlink() and rmdir() unhash F-d-c entry. 3711da177e4SLinus Torvalds * 4. fat_write_inode() checks whether the thing is unhashed. 3721da177e4SLinus Torvalds * If it is we silently return. If it isn't we do bread(), 3731da177e4SLinus Torvalds * check if the location is still valid and retry if it 3741da177e4SLinus Torvalds * isn't. Otherwise we do changes. 3751da177e4SLinus Torvalds * 5. Spinlock is used to protect hash/unhash/location check/lookup 376deee3ce4SAl Viro * 6. fat_evict_inode() unhashes the F-d-c entry. 3771da177e4SLinus Torvalds * 7. lookup() and readdir() do igrab() if they find a F-d-c entry 3781da177e4SLinus Torvalds * and consider negative result as cache miss. 3791da177e4SLinus Torvalds */ 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds static void fat_hash_init(struct super_block *sb) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 3841da177e4SLinus Torvalds int i; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds spin_lock_init(&sbi->inode_hash_lock); 3871da177e4SLinus Torvalds for (i = 0; i < FAT_HASH_SIZE; i++) 3881da177e4SLinus Torvalds INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 391d3dfa822SOGAWA Hirofumi static inline unsigned long fat_hash(loff_t i_pos) 3921da177e4SLinus Torvalds { 393d3dfa822SOGAWA Hirofumi return hash_32(i_pos, FAT_HASH_BITS); 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3967669e8fbSSteven J. Magnani static void dir_hash_init(struct super_block *sb) 3977669e8fbSSteven J. Magnani { 3987669e8fbSSteven J. Magnani struct msdos_sb_info *sbi = MSDOS_SB(sb); 3997669e8fbSSteven J. Magnani int i; 4007669e8fbSSteven J. Magnani 4017669e8fbSSteven J. Magnani spin_lock_init(&sbi->dir_hash_lock); 4027669e8fbSSteven J. Magnani for (i = 0; i < FAT_HASH_SIZE; i++) 4037669e8fbSSteven J. Magnani INIT_HLIST_HEAD(&sbi->dir_hashtable[i]); 4047669e8fbSSteven J. Magnani } 4057669e8fbSSteven J. Magnani 4061da177e4SLinus Torvalds void fat_attach(struct inode *inode, loff_t i_pos) 4071da177e4SLinus Torvalds { 408d3dfa822SOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4097669e8fbSSteven J. Magnani 4107669e8fbSSteven J. Magnani if (inode->i_ino != MSDOS_ROOT_INO) { 4117669e8fbSSteven J. Magnani struct hlist_head *head = sbi->inode_hashtable 4127669e8fbSSteven J. Magnani + fat_hash(i_pos); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 4151da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = i_pos; 416d3dfa822SOGAWA Hirofumi hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head); 4171da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4181da177e4SLinus Torvalds } 4197669e8fbSSteven J. Magnani 4207669e8fbSSteven J. Magnani /* If NFS support is enabled, cache the mapping of start cluster 4217669e8fbSSteven J. Magnani * to directory inode. This is used during reconnection of 4227669e8fbSSteven J. Magnani * dentries to the filesystem root. 4237669e8fbSSteven J. Magnani */ 4247669e8fbSSteven J. Magnani if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { 4257669e8fbSSteven J. Magnani struct hlist_head *d_head = sbi->dir_hashtable; 4267669e8fbSSteven J. Magnani d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart); 4277669e8fbSSteven J. Magnani 4287669e8fbSSteven J. Magnani spin_lock(&sbi->dir_hash_lock); 4297669e8fbSSteven J. Magnani hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head); 4307669e8fbSSteven J. Magnani spin_unlock(&sbi->dir_hash_lock); 4317669e8fbSSteven J. Magnani } 4327669e8fbSSteven J. Magnani } 4337c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_attach); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds void fat_detach(struct inode *inode) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4381da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 4391da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = 0; 4401da177e4SLinus Torvalds hlist_del_init(&MSDOS_I(inode)->i_fat_hash); 4411da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4427669e8fbSSteven J. Magnani 4437669e8fbSSteven J. Magnani if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { 4447669e8fbSSteven J. Magnani spin_lock(&sbi->dir_hash_lock); 4457669e8fbSSteven J. Magnani hlist_del_init(&MSDOS_I(inode)->i_dir_hash); 4467669e8fbSSteven J. Magnani spin_unlock(&sbi->dir_hash_lock); 4477669e8fbSSteven J. Magnani } 4481da177e4SLinus Torvalds } 4497c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_detach); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds struct inode *fat_iget(struct super_block *sb, loff_t i_pos) 4521da177e4SLinus Torvalds { 4531da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 454d3dfa822SOGAWA Hirofumi struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); 4551da177e4SLinus Torvalds struct msdos_inode_info *i; 4561da177e4SLinus Torvalds struct inode *inode = NULL; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 459b67bfe0dSSasha Levin hlist_for_each_entry(i, head, i_fat_hash) { 4601da177e4SLinus Torvalds BUG_ON(i->vfs_inode.i_sb != sb); 4611da177e4SLinus Torvalds if (i->i_pos != i_pos) 4621da177e4SLinus Torvalds continue; 4631da177e4SLinus Torvalds inode = igrab(&i->vfs_inode); 4641da177e4SLinus Torvalds if (inode) 4651da177e4SLinus Torvalds break; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 4681da177e4SLinus Torvalds return inode; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds static int is_exec(unsigned char *extension) 4721da177e4SLinus Torvalds { 473ef19470eSManuel Schölling unsigned char exe_extensions[] = "EXECOMBAT", *walk; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds for (walk = exe_extensions; *walk; walk += 3) 4761da177e4SLinus Torvalds if (!strncmp(extension, walk, 3)) 4771da177e4SLinus Torvalds return 1; 4781da177e4SLinus Torvalds return 0; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static int fat_calc_dir_size(struct inode *inode) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 4841da177e4SLinus Torvalds int ret, fclus, dclus; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds inode->i_size = 0; 4871da177e4SLinus Torvalds if (MSDOS_I(inode)->i_start == 0) 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); 4911da177e4SLinus Torvalds if (ret < 0) 4921da177e4SLinus Torvalds return ret; 4931da177e4SLinus Torvalds inode->i_size = (fclus + 1) << sbi->cluster_bits; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds return 0; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 498a3082d52SOGAWA Hirofumi static int fat_validate_dir(struct inode *dir) 499a3082d52SOGAWA Hirofumi { 500a3082d52SOGAWA Hirofumi struct super_block *sb = dir->i_sb; 501a3082d52SOGAWA Hirofumi 502a3082d52SOGAWA Hirofumi if (dir->i_nlink < 2) { 503a3082d52SOGAWA Hirofumi /* Directory should have "."/".." entries at least. */ 504a3082d52SOGAWA Hirofumi fat_fs_error(sb, "corrupted directory (invalid entries)"); 505a3082d52SOGAWA Hirofumi return -EIO; 506a3082d52SOGAWA Hirofumi } 507a3082d52SOGAWA Hirofumi if (MSDOS_I(dir)->i_start == 0 || 508a3082d52SOGAWA Hirofumi MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) { 509a3082d52SOGAWA Hirofumi /* Directory should point valid cluster. */ 510a3082d52SOGAWA Hirofumi fat_fs_error(sb, "corrupted directory (invalid i_start)"); 511a3082d52SOGAWA Hirofumi return -EIO; 512a3082d52SOGAWA Hirofumi } 513a3082d52SOGAWA Hirofumi return 0; 514a3082d52SOGAWA Hirofumi } 515a3082d52SOGAWA Hirofumi 5161da177e4SLinus Torvalds /* doesn't deal with root inode */ 517f1e6fb0aSNamjae Jeon int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) 5181da177e4SLinus Torvalds { 5191da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 5201da177e4SLinus Torvalds int error; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds MSDOS_I(inode)->i_pos = 0; 5231da177e4SLinus Torvalds inode->i_uid = sbi->options.fs_uid; 5241da177e4SLinus Torvalds inode->i_gid = sbi->options.fs_gid; 5252489dbabSJeff Layton inode_inc_iversion(inode); 52674b5cab6SArnd Bergmann inode->i_generation = prandom_u32(); 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { 5291da177e4SLinus Torvalds inode->i_generation &= ~1; 5309c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO); 5311da177e4SLinus Torvalds inode->i_op = sbi->dir_ops; 5321da177e4SLinus Torvalds inode->i_fop = &fat_dir_operations; 5331da177e4SLinus Torvalds 534a943ed71SSteven J. Magnani MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 5351da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 5361da177e4SLinus Torvalds error = fat_calc_dir_size(inode); 5371da177e4SLinus Torvalds if (error < 0) 5381da177e4SLinus Torvalds return error; 5391da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 5401da177e4SLinus Torvalds 541bfe86848SMiklos Szeredi set_nlink(inode, fat_subdirs(inode)); 542a3082d52SOGAWA Hirofumi 543a3082d52SOGAWA Hirofumi error = fat_validate_dir(inode); 544a3082d52SOGAWA Hirofumi if (error < 0) 545a3082d52SOGAWA Hirofumi return error; 5461da177e4SLinus Torvalds } else { /* not a directory */ 5471da177e4SLinus Torvalds inode->i_generation |= 1; 5489c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, de->attr, 5499aacd599SOGAWA Hirofumi ((sbi->options.showexec && !is_exec(de->name + 8)) 5509c0aa1b8SOGAWA Hirofumi ? S_IRUGO|S_IWUGO : S_IRWXUGO)); 551a943ed71SSteven J. Magnani MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 5541da177e4SLinus Torvalds inode->i_size = le32_to_cpu(de->size); 5551da177e4SLinus Torvalds inode->i_op = &fat_file_inode_operations; 5561da177e4SLinus Torvalds inode->i_fop = &fat_file_operations; 5571da177e4SLinus Torvalds inode->i_mapping->a_ops = &fat_aops; 5581da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds if (de->attr & ATTR_SYS) { 5611da177e4SLinus Torvalds if (sbi->options.sys_immutable) 5621da177e4SLinus Torvalds inode->i_flags |= S_IMMUTABLE; 5631da177e4SLinus Torvalds } 5649c0aa1b8SOGAWA Hirofumi fat_save_attrs(inode, de->attr); 5659c0aa1b8SOGAWA Hirofumi 5661da177e4SLinus Torvalds inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) 5671da177e4SLinus Torvalds & ~((loff_t)sbi->cluster_size - 1)) >> 9; 5687decd1cbSOGAWA Hirofumi 569f423420cSArnd Bergmann fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0); 5701da177e4SLinus Torvalds if (sbi->options.isvfat) { 571f423420cSArnd Bergmann fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime, 5727decd1cbSOGAWA Hirofumi de->cdate, de->ctime_cs); 573f423420cSArnd Bergmann fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0); 5741da177e4SLinus Torvalds } else 575cd83f6b1SFrank Sorenson fat_truncate_time(inode, &inode->i_mtime, S_ATIME|S_CTIME); 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds return 0; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5808fceb4e0SNamjae Jeon static inline void fat_lock_build_inode(struct msdos_sb_info *sbi) 5818fceb4e0SNamjae Jeon { 5828fceb4e0SNamjae Jeon if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 5838fceb4e0SNamjae Jeon mutex_lock(&sbi->nfs_build_inode_lock); 5848fceb4e0SNamjae Jeon } 5858fceb4e0SNamjae Jeon 5868fceb4e0SNamjae Jeon static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi) 5878fceb4e0SNamjae Jeon { 5888fceb4e0SNamjae Jeon if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) 5898fceb4e0SNamjae Jeon mutex_unlock(&sbi->nfs_build_inode_lock); 5908fceb4e0SNamjae Jeon } 5918fceb4e0SNamjae Jeon 5921da177e4SLinus Torvalds struct inode *fat_build_inode(struct super_block *sb, 5931da177e4SLinus Torvalds struct msdos_dir_entry *de, loff_t i_pos) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds struct inode *inode; 5961da177e4SLinus Torvalds int err; 5971da177e4SLinus Torvalds 5988fceb4e0SNamjae Jeon fat_lock_build_inode(MSDOS_SB(sb)); 5991da177e4SLinus Torvalds inode = fat_iget(sb, i_pos); 6001da177e4SLinus Torvalds if (inode) 6011da177e4SLinus Torvalds goto out; 6021da177e4SLinus Torvalds inode = new_inode(sb); 6031da177e4SLinus Torvalds if (!inode) { 6041da177e4SLinus Torvalds inode = ERR_PTR(-ENOMEM); 6051da177e4SLinus Torvalds goto out; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds inode->i_ino = iunique(sb, MSDOS_ROOT_INO); 6082489dbabSJeff Layton inode_set_iversion(inode, 1); 6091da177e4SLinus Torvalds err = fat_fill_inode(inode, de); 6101da177e4SLinus Torvalds if (err) { 6111da177e4SLinus Torvalds iput(inode); 6121da177e4SLinus Torvalds inode = ERR_PTR(err); 6131da177e4SLinus Torvalds goto out; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds fat_attach(inode, i_pos); 6161da177e4SLinus Torvalds insert_inode_hash(inode); 6171da177e4SLinus Torvalds out: 6188fceb4e0SNamjae Jeon fat_unlock_build_inode(MSDOS_SB(sb)); 6191da177e4SLinus Torvalds return inode; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6227c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_build_inode); 6231da177e4SLinus Torvalds 624b13bb33eSNamjae Jeon static int __fat_write_inode(struct inode *inode, int wait); 625b13bb33eSNamjae Jeon 626b13bb33eSNamjae Jeon static void fat_free_eofblocks(struct inode *inode) 627b13bb33eSNamjae Jeon { 628b13bb33eSNamjae Jeon /* Release unwritten fallocated blocks on inode eviction. */ 629b13bb33eSNamjae Jeon if ((inode->i_blocks << 9) > 630b13bb33eSNamjae Jeon round_up(MSDOS_I(inode)->mmu_private, 631b13bb33eSNamjae Jeon MSDOS_SB(inode->i_sb)->cluster_size)) { 632b13bb33eSNamjae Jeon int err; 633b13bb33eSNamjae Jeon 634b13bb33eSNamjae Jeon fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private); 635b13bb33eSNamjae Jeon /* Fallocate results in updating the i_start/iogstart 636b13bb33eSNamjae Jeon * for the zero byte file. So, make it return to 637b13bb33eSNamjae Jeon * original state during evict and commit it to avoid 638b13bb33eSNamjae Jeon * any corruption on the next access to the cluster 639b13bb33eSNamjae Jeon * chain for the file. 640b13bb33eSNamjae Jeon */ 641b13bb33eSNamjae Jeon err = __fat_write_inode(inode, inode_needs_sync(inode)); 642b13bb33eSNamjae Jeon if (err) { 643b13bb33eSNamjae Jeon fat_msg(inode->i_sb, KERN_WARNING, "Failed to " 644b13bb33eSNamjae Jeon "update on disk inode for unused " 645b13bb33eSNamjae Jeon "fallocated blocks, inode could be " 646b13bb33eSNamjae Jeon "corrupted. Please run fsck"); 647b13bb33eSNamjae Jeon } 648b13bb33eSNamjae Jeon 649b13bb33eSNamjae Jeon } 650b13bb33eSNamjae Jeon } 651b13bb33eSNamjae Jeon 652deee3ce4SAl Viro static void fat_evict_inode(struct inode *inode) 6531da177e4SLinus Torvalds { 65491b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 655deee3ce4SAl Viro if (!inode->i_nlink) { 6561da177e4SLinus Torvalds inode->i_size = 0; 657459f6ed3Snpiggin@suse.de fat_truncate_blocks(inode, 0); 658b13bb33eSNamjae Jeon } else 659b13bb33eSNamjae Jeon fat_free_eofblocks(inode); 660b13bb33eSNamjae Jeon 661deee3ce4SAl Viro invalidate_inode_buffers(inode); 662dbd5768fSJan Kara clear_inode(inode); 6631da177e4SLinus Torvalds fat_cache_inval_inode(inode); 664a993b542SOGAWA Hirofumi fat_detach(inode); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 667b88a1058SOleksij Rempel static void fat_set_state(struct super_block *sb, 668b88a1058SOleksij Rempel unsigned int set, unsigned int force) 669b88a1058SOleksij Rempel { 670b88a1058SOleksij Rempel struct buffer_head *bh; 671b88a1058SOleksij Rempel struct fat_boot_sector *b; 672d6bd4282SFred Chou struct msdos_sb_info *sbi = MSDOS_SB(sb); 673b88a1058SOleksij Rempel 674b88a1058SOleksij Rempel /* do not change any thing if mounted read only */ 675bc98a42cSDavid Howells if (sb_rdonly(sb) && !force) 676b88a1058SOleksij Rempel return; 677b88a1058SOleksij Rempel 678b88a1058SOleksij Rempel /* do not change state if fs was dirty */ 679b88a1058SOleksij Rempel if (sbi->dirty) { 680b88a1058SOleksij Rempel /* warn only on set (mount). */ 681b88a1058SOleksij Rempel if (set) 682b88a1058SOleksij Rempel fat_msg(sb, KERN_WARNING, "Volume was not properly " 683b88a1058SOleksij Rempel "unmounted. Some data may be corrupt. " 684b88a1058SOleksij Rempel "Please run fsck."); 685b88a1058SOleksij Rempel return; 686b88a1058SOleksij Rempel } 687b88a1058SOleksij Rempel 688b88a1058SOleksij Rempel bh = sb_bread(sb, 0); 689b88a1058SOleksij Rempel if (bh == NULL) { 690b88a1058SOleksij Rempel fat_msg(sb, KERN_ERR, "unable to read boot sector " 691b88a1058SOleksij Rempel "to mark fs as dirty"); 692b88a1058SOleksij Rempel return; 693b88a1058SOleksij Rempel } 694b88a1058SOleksij Rempel 695b88a1058SOleksij Rempel b = (struct fat_boot_sector *) bh->b_data; 696b88a1058SOleksij Rempel 697306790f7SCarmeli Tamir if (is_fat32(sbi)) { 698b88a1058SOleksij Rempel if (set) 699b88a1058SOleksij Rempel b->fat32.state |= FAT_STATE_DIRTY; 700b88a1058SOleksij Rempel else 701b88a1058SOleksij Rempel b->fat32.state &= ~FAT_STATE_DIRTY; 702b88a1058SOleksij Rempel } else /* fat 16 and 12 */ { 703b88a1058SOleksij Rempel if (set) 704b88a1058SOleksij Rempel b->fat16.state |= FAT_STATE_DIRTY; 705b88a1058SOleksij Rempel else 706b88a1058SOleksij Rempel b->fat16.state &= ~FAT_STATE_DIRTY; 707b88a1058SOleksij Rempel } 708b88a1058SOleksij Rempel 709b88a1058SOleksij Rempel mark_buffer_dirty(bh); 710b88a1058SOleksij Rempel sync_dirty_buffer(bh); 711b88a1058SOleksij Rempel brelse(bh); 712b88a1058SOleksij Rempel } 713b88a1058SOleksij Rempel 71435033ab9SOGAWA Hirofumi static void fat_reset_iocharset(struct fat_mount_options *opts) 71535033ab9SOGAWA Hirofumi { 71635033ab9SOGAWA Hirofumi if (opts->iocharset != fat_default_iocharset) { 71735033ab9SOGAWA Hirofumi /* Note: opts->iocharset can be NULL here */ 71835033ab9SOGAWA Hirofumi kfree(opts->iocharset); 71935033ab9SOGAWA Hirofumi opts->iocharset = fat_default_iocharset; 72035033ab9SOGAWA Hirofumi } 72135033ab9SOGAWA Hirofumi } 72235033ab9SOGAWA Hirofumi 723cac45b06SAl Viro static void delayed_free(struct rcu_head *p) 724cac45b06SAl Viro { 725cac45b06SAl Viro struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu); 726cac45b06SAl Viro unload_nls(sbi->nls_disk); 727cac45b06SAl Viro unload_nls(sbi->nls_io); 72835033ab9SOGAWA Hirofumi fat_reset_iocharset(&sbi->options); 729cac45b06SAl Viro kfree(sbi); 730cac45b06SAl Viro } 731cac45b06SAl Viro 732a6bf6b21SOGAWA Hirofumi static void fat_put_super(struct super_block *sb) 733a6bf6b21SOGAWA Hirofumi { 734a6bf6b21SOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 7351da177e4SLinus Torvalds 736b88a1058SOleksij Rempel fat_set_state(sb, 0, 0); 737b88a1058SOleksij Rempel 738020ac5b6SArtem Bityutskiy iput(sbi->fsinfo_inode); 739b522412aSAl Viro iput(sbi->fat_inode); 740b522412aSAl Viro 741cac45b06SAl Viro call_rcu(&sbi->rcu, delayed_free); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 744e18b890bSChristoph Lameter static struct kmem_cache *fat_inode_cachep; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds static struct inode *fat_alloc_inode(struct super_block *sb) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds struct msdos_inode_info *ei; 7498f593427SLinus Torvalds ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS); 7501da177e4SLinus Torvalds if (!ei) 7511da177e4SLinus Torvalds return NULL; 75258268691SChristoph Hellwig 75358268691SChristoph Hellwig init_rwsem(&ei->truncate_lock); 754bc87302aSOGAWA Hirofumi /* Zeroing to allow iput() even if partial initialized inode. */ 755bc87302aSOGAWA Hirofumi ei->mmu_private = 0; 756bc87302aSOGAWA Hirofumi ei->i_start = 0; 757bc87302aSOGAWA Hirofumi ei->i_logstart = 0; 758bc87302aSOGAWA Hirofumi ei->i_attrs = 0; 759bc87302aSOGAWA Hirofumi ei->i_pos = 0; 760bc87302aSOGAWA Hirofumi 7611da177e4SLinus Torvalds return &ei->vfs_inode; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 764f9ec991dSAl Viro static void fat_free_inode(struct inode *inode) 765fa0d7e3dSNick Piggin { 766fa0d7e3dSNick Piggin kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); 767fa0d7e3dSNick Piggin } 768fa0d7e3dSNick Piggin 76951cc5068SAlexey Dobriyan static void init_once(void *foo) 7701da177e4SLinus Torvalds { 7711da177e4SLinus Torvalds struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds spin_lock_init(&ei->cache_lru_lock); 7741da177e4SLinus Torvalds ei->nr_caches = 0; 7751da177e4SLinus Torvalds ei->cache_valid_id = FAT_CACHE_VALID + 1; 7761da177e4SLinus Torvalds INIT_LIST_HEAD(&ei->cache_lru); 7771da177e4SLinus Torvalds INIT_HLIST_NODE(&ei->i_fat_hash); 7787669e8fbSSteven J. Magnani INIT_HLIST_NODE(&ei->i_dir_hash); 7791da177e4SLinus Torvalds inode_init_once(&ei->vfs_inode); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds static int __init fat_init_inodecache(void) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds fat_inode_cachep = kmem_cache_create("fat_inode_cache", 7851da177e4SLinus Torvalds sizeof(struct msdos_inode_info), 786fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 7875d097056SVladimir Davydov SLAB_MEM_SPREAD|SLAB_ACCOUNT), 78820c2df83SPaul Mundt init_once); 7891da177e4SLinus Torvalds if (fat_inode_cachep == NULL) 7901da177e4SLinus Torvalds return -ENOMEM; 7911da177e4SLinus Torvalds return 0; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds static void __exit fat_destroy_inodecache(void) 7951da177e4SLinus Torvalds { 7968c0a8537SKirill A. Shutemov /* 7978c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we 7988c0a8537SKirill A. Shutemov * destroy cache. 7998c0a8537SKirill A. Shutemov */ 8008c0a8537SKirill A. Shutemov rcu_barrier(); 8011a1d92c1SAlexey Dobriyan kmem_cache_destroy(fat_inode_cachep); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds static int fat_remount(struct super_block *sb, int *flags, char *data) 8051da177e4SLinus Torvalds { 806b6e8e12cSOGAWA Hirofumi bool new_rdonly; 8071da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 8081751e8a6SLinus Torvalds *flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME); 809b88a1058SOleksij Rempel 81002b9984dSTheodore Ts'o sync_filesystem(sb); 81102b9984dSTheodore Ts'o 812b88a1058SOleksij Rempel /* make sure we update state on remount. */ 8131751e8a6SLinus Torvalds new_rdonly = *flags & SB_RDONLY; 814bc98a42cSDavid Howells if (new_rdonly != sb_rdonly(sb)) { 815b88a1058SOleksij Rempel if (new_rdonly) 816b88a1058SOleksij Rempel fat_set_state(sb, 0, 0); 817b88a1058SOleksij Rempel else 818b88a1058SOleksij Rempel fat_set_state(sb, 1, 1); 819b88a1058SOleksij Rempel } 8201da177e4SLinus Torvalds return 0; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 823726c3342SDavid Howells static int fat_statfs(struct dentry *dentry, struct kstatfs *buf) 8241da177e4SLinus Torvalds { 825aac49b75SColy Li struct super_block *sb = dentry->d_sb; 826aac49b75SColy Li struct msdos_sb_info *sbi = MSDOS_SB(sb); 827aac49b75SColy Li u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* If the count of free cluster is still unknown, counts it here. */ 830606e423eSOGAWA Hirofumi if (sbi->free_clusters == -1 || !sbi->free_clus_valid) { 831726c3342SDavid Howells int err = fat_count_free_clusters(dentry->d_sb); 8321da177e4SLinus Torvalds if (err) 8331da177e4SLinus Torvalds return err; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 836726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic; 8371da177e4SLinus Torvalds buf->f_bsize = sbi->cluster_size; 8381da177e4SLinus Torvalds buf->f_blocks = sbi->max_cluster - FAT_START_ENT; 8391da177e4SLinus Torvalds buf->f_bfree = sbi->free_clusters; 8401da177e4SLinus Torvalds buf->f_bavail = sbi->free_clusters; 8416d1349c7SAl Viro buf->f_fsid = u64_to_fsid(id); 842f68e542fSOGAWA Hirofumi buf->f_namelen = 843f68e542fSOGAWA Hirofumi (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds return 0; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 848a9185b41SChristoph Hellwig static int __fat_write_inode(struct inode *inode, int wait) 8491da177e4SLinus Torvalds { 8501da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 8511da177e4SLinus Torvalds struct msdos_sb_info *sbi = MSDOS_SB(sb); 8521da177e4SLinus Torvalds struct buffer_head *bh; 8531da177e4SLinus Torvalds struct msdos_dir_entry *raw_entry; 8541da177e4SLinus Torvalds loff_t i_pos; 855e22a4442SNamjae Jeon sector_t blocknr; 856e22a4442SNamjae Jeon int err, offset; 8571da177e4SLinus Torvalds 8589ca59f4cSOGAWA Hirofumi if (inode->i_ino == MSDOS_ROOT_INO) 8599ca59f4cSOGAWA Hirofumi return 0; 8609ca59f4cSOGAWA Hirofumi 8611da177e4SLinus Torvalds retry: 8629ca59f4cSOGAWA Hirofumi i_pos = fat_i_pos_read(sbi, inode); 8639ca59f4cSOGAWA Hirofumi if (!i_pos) 8641da177e4SLinus Torvalds return 0; 8651da177e4SLinus Torvalds 866e22a4442SNamjae Jeon fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset); 867e22a4442SNamjae Jeon bh = sb_bread(sb, blocknr); 8681da177e4SLinus Torvalds if (!bh) { 869869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read inode block " 870869f58c0SAlexey Fisher "for updating (i_pos %lld)", i_pos); 8715f22ca9bSLinus Torvalds return -EIO; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds spin_lock(&sbi->inode_hash_lock); 8741da177e4SLinus Torvalds if (i_pos != MSDOS_I(inode)->i_pos) { 8751da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 8761da177e4SLinus Torvalds brelse(bh); 8771da177e4SLinus Torvalds goto retry; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 880e22a4442SNamjae Jeon raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset]; 8811da177e4SLinus Torvalds if (S_ISDIR(inode->i_mode)) 8821da177e4SLinus Torvalds raw_entry->size = 0; 8831da177e4SLinus Torvalds else 8841da177e4SLinus Torvalds raw_entry->size = cpu_to_le32(inode->i_size); 8859c0aa1b8SOGAWA Hirofumi raw_entry->attr = fat_make_attrs(inode); 886a943ed71SSteven J. Magnani fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart); 887f423420cSArnd Bergmann fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time, 8887decd1cbSOGAWA Hirofumi &raw_entry->date, NULL); 8891da177e4SLinus Torvalds if (sbi->options.isvfat) { 89062a36c43SStephane Kardas __le16 atime; 891f423420cSArnd Bergmann fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime, 8927decd1cbSOGAWA Hirofumi &raw_entry->cdate, &raw_entry->ctime_cs); 893f423420cSArnd Bergmann fat_time_unix2fat(sbi, &inode->i_atime, &atime, 8947decd1cbSOGAWA Hirofumi &raw_entry->adate, NULL); 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds spin_unlock(&sbi->inode_hash_lock); 8971da177e4SLinus Torvalds mark_buffer_dirty(bh); 8985f22ca9bSLinus Torvalds err = 0; 8991da177e4SLinus Torvalds if (wait) 9001da177e4SLinus Torvalds err = sync_dirty_buffer(bh); 9011da177e4SLinus Torvalds brelse(bh); 9021da177e4SLinus Torvalds return err; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 905a9185b41SChristoph Hellwig static int fat_write_inode(struct inode *inode, struct writeback_control *wbc) 906a9185b41SChristoph Hellwig { 90778491189SArtem Bityutskiy int err; 90878491189SArtem Bityutskiy 90978491189SArtem Bityutskiy if (inode->i_ino == MSDOS_FSINFO_INO) { 91078491189SArtem Bityutskiy struct super_block *sb = inode->i_sb; 91178491189SArtem Bityutskiy 912e40b34c7SMarco Stornelli mutex_lock(&MSDOS_SB(sb)->s_lock); 91378491189SArtem Bityutskiy err = fat_clusters_flush(sb); 914e40b34c7SMarco Stornelli mutex_unlock(&MSDOS_SB(sb)->s_lock); 91578491189SArtem Bityutskiy } else 91678491189SArtem Bityutskiy err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 91778491189SArtem Bityutskiy 91878491189SArtem Bityutskiy return err; 919a9185b41SChristoph Hellwig } 920a9185b41SChristoph Hellwig 9211da177e4SLinus Torvalds int fat_sync_inode(struct inode *inode) 9221da177e4SLinus Torvalds { 923a9185b41SChristoph Hellwig return __fat_write_inode(inode, 1); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9267c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_sync_inode); 9271da177e4SLinus Torvalds 92834c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root); 929ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fat_sops = { 9301da177e4SLinus Torvalds .alloc_inode = fat_alloc_inode, 931f9ec991dSAl Viro .free_inode = fat_free_inode, 9321da177e4SLinus Torvalds .write_inode = fat_write_inode, 933deee3ce4SAl Viro .evict_inode = fat_evict_inode, 9341da177e4SLinus Torvalds .put_super = fat_put_super, 9351da177e4SLinus Torvalds .statfs = fat_statfs, 9361da177e4SLinus Torvalds .remount_fs = fat_remount, 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds .show_options = fat_show_options, 9391da177e4SLinus Torvalds }; 9401da177e4SLinus Torvalds 94134c80b1dSAl Viro static int fat_show_options(struct seq_file *m, struct dentry *root) 9421da177e4SLinus Torvalds { 94334c80b1dSAl Viro struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb); 9441da177e4SLinus Torvalds struct fat_mount_options *opts = &sbi->options; 9451da177e4SLinus Torvalds int isvfat = opts->isvfat; 9461da177e4SLinus Torvalds 947170782ebSEric W. Biederman if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) 948170782ebSEric W. Biederman seq_printf(m, ",uid=%u", 949170782ebSEric W. Biederman from_kuid_munged(&init_user_ns, opts->fs_uid)); 950170782ebSEric W. Biederman if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) 951170782ebSEric W. Biederman seq_printf(m, ",gid=%u", 952170782ebSEric W. Biederman from_kgid_munged(&init_user_ns, opts->fs_gid)); 9531da177e4SLinus Torvalds seq_printf(m, ",fmask=%04o", opts->fs_fmask); 9541da177e4SLinus Torvalds seq_printf(m, ",dmask=%04o", opts->fs_dmask); 9551ae43f82SOGAWA Hirofumi if (opts->allow_utime) 9561ae43f82SOGAWA Hirofumi seq_printf(m, ",allow_utime=%04o", opts->allow_utime); 9571da177e4SLinus Torvalds if (sbi->nls_disk) 958c6c20372SDave Reisner /* strip "cp" prefix from displayed option */ 959c6c20372SDave Reisner seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]); 9601da177e4SLinus Torvalds if (isvfat) { 9611da177e4SLinus Torvalds if (sbi->nls_io) 9621da177e4SLinus Torvalds seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds switch (opts->shortname) { 9651da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: 9661da177e4SLinus Torvalds seq_puts(m, ",shortname=win95"); 9671da177e4SLinus Torvalds break; 9681da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: 9691da177e4SLinus Torvalds seq_puts(m, ",shortname=winnt"); 9701da177e4SLinus Torvalds break; 9711da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: 9721da177e4SLinus Torvalds seq_puts(m, ",shortname=mixed"); 9731da177e4SLinus Torvalds break; 9741da177e4SLinus Torvalds case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: 97595523475SPaul Wise seq_puts(m, ",shortname=lower"); 9761da177e4SLinus Torvalds break; 9771da177e4SLinus Torvalds default: 9781da177e4SLinus Torvalds seq_puts(m, ",shortname=unknown"); 9791da177e4SLinus Torvalds break; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds if (opts->name_check != 'n') 9831da177e4SLinus Torvalds seq_printf(m, ",check=%c", opts->name_check); 98428ec039cSOGAWA Hirofumi if (opts->usefree) 98528ec039cSOGAWA Hirofumi seq_puts(m, ",usefree"); 9861da177e4SLinus Torvalds if (opts->quiet) 9871da177e4SLinus Torvalds seq_puts(m, ",quiet"); 9881da177e4SLinus Torvalds if (opts->showexec) 9891da177e4SLinus Torvalds seq_puts(m, ",showexec"); 9901da177e4SLinus Torvalds if (opts->sys_immutable) 9911da177e4SLinus Torvalds seq_puts(m, ",sys_immutable"); 9921da177e4SLinus Torvalds if (!isvfat) { 9931da177e4SLinus Torvalds if (opts->dotsOK) 9941da177e4SLinus Torvalds seq_puts(m, ",dotsOK=yes"); 9951da177e4SLinus Torvalds if (opts->nocase) 9961da177e4SLinus Torvalds seq_puts(m, ",nocase"); 9971da177e4SLinus Torvalds } else { 9981da177e4SLinus Torvalds if (opts->utf8) 9991da177e4SLinus Torvalds seq_puts(m, ",utf8"); 10001da177e4SLinus Torvalds if (opts->unicode_xlate) 10011da177e4SLinus Torvalds seq_puts(m, ",uni_xlate"); 10021da177e4SLinus Torvalds if (!opts->numtail) 10031da177e4SLinus Torvalds seq_puts(m, ",nonumtail"); 1004dfc209c0SOGAWA Hirofumi if (opts->rodir) 1005dfc209c0SOGAWA Hirofumi seq_puts(m, ",rodir"); 10061da177e4SLinus Torvalds } 1007dfc209c0SOGAWA Hirofumi if (opts->flush) 1008c1fca3b6SMiklos Szeredi seq_puts(m, ",flush"); 100958156c8fSJan Kara if (opts->tz_set) { 101058156c8fSJan Kara if (opts->time_offset) 101158156c8fSJan Kara seq_printf(m, ",time_offset=%d", opts->time_offset); 101258156c8fSJan Kara else 1013b271e067SJoe Peterson seq_puts(m, ",tz=UTC"); 101458156c8fSJan Kara } 101585c78591SDenis Karpov if (opts->errors == FAT_ERRORS_CONT) 101685c78591SDenis Karpov seq_puts(m, ",errors=continue"); 101785c78591SDenis Karpov else if (opts->errors == FAT_ERRORS_PANIC) 101885c78591SDenis Karpov seq_puts(m, ",errors=panic"); 101985c78591SDenis Karpov else 102085c78591SDenis Karpov seq_puts(m, ",errors=remount-ro"); 10212628b7a6SNamjae Jeon if (opts->nfs == FAT_NFS_NOSTALE_RO) 10222628b7a6SNamjae Jeon seq_puts(m, ",nfs=nostale_ro"); 10232628b7a6SNamjae Jeon else if (opts->nfs) 10242628b7a6SNamjae Jeon seq_puts(m, ",nfs=stale_rw"); 1025681142f9SChristoph Hellwig if (opts->discard) 1026681142f9SChristoph Hellwig seq_puts(m, ",discard"); 1027190a8843SConrad Meyer if (opts->dos1xfloppy) 1028190a8843SConrad Meyer seq_puts(m, ",dos1xfloppy"); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds return 0; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds enum { 10341da177e4SLinus Torvalds Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid, 10351ae43f82SOGAWA Hirofumi Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage, 10361ae43f82SOGAWA Hirofumi Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug, 10371ae43f82SOGAWA Hirofumi Opt_immutable, Opt_dots, Opt_nodots, 10381da177e4SLinus Torvalds Opt_charset, Opt_shortname_lower, Opt_shortname_win95, 10391da177e4SLinus Torvalds Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes, 10401da177e4SLinus Torvalds Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes, 1041d7a83c0fSGeert Uytterhoeven Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont, 104258156c8fSJan Kara Opt_err_panic, Opt_err_ro, Opt_discard, Opt_nfs, Opt_time_offset, 1043190a8843SConrad Meyer Opt_nfs_stale_rw, Opt_nfs_nostale_ro, Opt_err, Opt_dos1xfloppy, 10441da177e4SLinus Torvalds }; 10451da177e4SLinus Torvalds 1046a447c093SSteven Whitehouse static const match_table_t fat_tokens = { 10471da177e4SLinus Torvalds {Opt_check_r, "check=relaxed"}, 10481da177e4SLinus Torvalds {Opt_check_s, "check=strict"}, 10491da177e4SLinus Torvalds {Opt_check_n, "check=normal"}, 10501da177e4SLinus Torvalds {Opt_check_r, "check=r"}, 10511da177e4SLinus Torvalds {Opt_check_s, "check=s"}, 10521da177e4SLinus Torvalds {Opt_check_n, "check=n"}, 10531da177e4SLinus Torvalds {Opt_uid, "uid=%u"}, 10541da177e4SLinus Torvalds {Opt_gid, "gid=%u"}, 10551da177e4SLinus Torvalds {Opt_umask, "umask=%o"}, 10561da177e4SLinus Torvalds {Opt_dmask, "dmask=%o"}, 10571da177e4SLinus Torvalds {Opt_fmask, "fmask=%o"}, 10581ae43f82SOGAWA Hirofumi {Opt_allow_utime, "allow_utime=%o"}, 10591da177e4SLinus Torvalds {Opt_codepage, "codepage=%u"}, 106028ec039cSOGAWA Hirofumi {Opt_usefree, "usefree"}, 10611da177e4SLinus Torvalds {Opt_nocase, "nocase"}, 10621da177e4SLinus Torvalds {Opt_quiet, "quiet"}, 10631da177e4SLinus Torvalds {Opt_showexec, "showexec"}, 10641da177e4SLinus Torvalds {Opt_debug, "debug"}, 10651da177e4SLinus Torvalds {Opt_immutable, "sys_immutable"}, 106685c78591SDenis Karpov {Opt_flush, "flush"}, 106785c78591SDenis Karpov {Opt_tz_utc, "tz=UTC"}, 106858156c8fSJan Kara {Opt_time_offset, "time_offset=%d"}, 106985c78591SDenis Karpov {Opt_err_cont, "errors=continue"}, 107085c78591SDenis Karpov {Opt_err_panic, "errors=panic"}, 107185c78591SDenis Karpov {Opt_err_ro, "errors=remount-ro"}, 1072681142f9SChristoph Hellwig {Opt_discard, "discard"}, 10732628b7a6SNamjae Jeon {Opt_nfs_stale_rw, "nfs"}, 10742628b7a6SNamjae Jeon {Opt_nfs_stale_rw, "nfs=stale_rw"}, 10752628b7a6SNamjae Jeon {Opt_nfs_nostale_ro, "nfs=nostale_ro"}, 1076190a8843SConrad Meyer {Opt_dos1xfloppy, "dos1xfloppy"}, 1077d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=binary"}, 1078d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=text"}, 1079d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=auto"}, 1080d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=b"}, 1081d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=t"}, 1082d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "conv=a"}, 1083d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "fat=%u"}, 1084d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "blocksize=%u"}, 1085d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "cvf_format=%20s"}, 1086d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "cvf_options=%100s"}, 1087d7a83c0fSGeert Uytterhoeven {Opt_obsolete, "posix"}, 1088ae78bf9cSChris Mason {Opt_err, NULL}, 10891da177e4SLinus Torvalds }; 1090a447c093SSteven Whitehouse static const match_table_t msdos_tokens = { 10911da177e4SLinus Torvalds {Opt_nodots, "nodots"}, 10921da177e4SLinus Torvalds {Opt_nodots, "dotsOK=no"}, 10931da177e4SLinus Torvalds {Opt_dots, "dots"}, 10941da177e4SLinus Torvalds {Opt_dots, "dotsOK=yes"}, 10951da177e4SLinus Torvalds {Opt_err, NULL} 10961da177e4SLinus Torvalds }; 1097a447c093SSteven Whitehouse static const match_table_t vfat_tokens = { 10981da177e4SLinus Torvalds {Opt_charset, "iocharset=%s"}, 10991da177e4SLinus Torvalds {Opt_shortname_lower, "shortname=lower"}, 11001da177e4SLinus Torvalds {Opt_shortname_win95, "shortname=win95"}, 11011da177e4SLinus Torvalds {Opt_shortname_winnt, "shortname=winnt"}, 11021da177e4SLinus Torvalds {Opt_shortname_mixed, "shortname=mixed"}, 11031da177e4SLinus Torvalds {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */ 11041da177e4SLinus Torvalds {Opt_utf8_no, "utf8=no"}, 11051da177e4SLinus Torvalds {Opt_utf8_no, "utf8=false"}, 11061da177e4SLinus Torvalds {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */ 11071da177e4SLinus Torvalds {Opt_utf8_yes, "utf8=yes"}, 11081da177e4SLinus Torvalds {Opt_utf8_yes, "utf8=true"}, 11091da177e4SLinus Torvalds {Opt_utf8_yes, "utf8"}, 11101da177e4SLinus Torvalds {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */ 11111da177e4SLinus Torvalds {Opt_uni_xl_no, "uni_xlate=no"}, 11121da177e4SLinus Torvalds {Opt_uni_xl_no, "uni_xlate=false"}, 11131da177e4SLinus Torvalds {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */ 11141da177e4SLinus Torvalds {Opt_uni_xl_yes, "uni_xlate=yes"}, 11151da177e4SLinus Torvalds {Opt_uni_xl_yes, "uni_xlate=true"}, 11161da177e4SLinus Torvalds {Opt_uni_xl_yes, "uni_xlate"}, 11171da177e4SLinus Torvalds {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */ 11181da177e4SLinus Torvalds {Opt_nonumtail_no, "nonumtail=no"}, 11191da177e4SLinus Torvalds {Opt_nonumtail_no, "nonumtail=false"}, 11201da177e4SLinus Torvalds {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */ 11211da177e4SLinus Torvalds {Opt_nonumtail_yes, "nonumtail=yes"}, 11221da177e4SLinus Torvalds {Opt_nonumtail_yes, "nonumtail=true"}, 11231da177e4SLinus Torvalds {Opt_nonumtail_yes, "nonumtail"}, 1124dfc209c0SOGAWA Hirofumi {Opt_rodir, "rodir"}, 11251da177e4SLinus Torvalds {Opt_err, NULL} 11261da177e4SLinus Torvalds }; 11271da177e4SLinus Torvalds 1128869f58c0SAlexey Fisher static int parse_options(struct super_block *sb, char *options, int is_vfat, 1129869f58c0SAlexey Fisher int silent, int *debug, struct fat_mount_options *opts) 11301da177e4SLinus Torvalds { 11311da177e4SLinus Torvalds char *p; 11321da177e4SLinus Torvalds substring_t args[MAX_OPT_ARGS]; 11331da177e4SLinus Torvalds int option; 11341da177e4SLinus Torvalds char *iocharset; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds opts->isvfat = is_vfat; 11371da177e4SLinus Torvalds 1138f0ce7ee3SDavid Howells opts->fs_uid = current_uid(); 1139f0ce7ee3SDavid Howells opts->fs_gid = current_gid(); 11403e107603SOGAWA Hirofumi opts->fs_fmask = opts->fs_dmask = current_umask(); 11411ae43f82SOGAWA Hirofumi opts->allow_utime = -1; 11421da177e4SLinus Torvalds opts->codepage = fat_default_codepage; 114335033ab9SOGAWA Hirofumi fat_reset_iocharset(opts); 1144dfc209c0SOGAWA Hirofumi if (is_vfat) { 114595523475SPaul Wise opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95; 1146dfc209c0SOGAWA Hirofumi opts->rodir = 0; 1147dfc209c0SOGAWA Hirofumi } else { 11481da177e4SLinus Torvalds opts->shortname = 0; 1149dfc209c0SOGAWA Hirofumi opts->rodir = 1; 1150dfc209c0SOGAWA Hirofumi } 11511da177e4SLinus Torvalds opts->name_check = 'n'; 11521da177e4SLinus Torvalds opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; 115338739380SMaciej S. Szmigiero opts->unicode_xlate = 0; 11541da177e4SLinus Torvalds opts->numtail = 1; 115528ec039cSOGAWA Hirofumi opts->usefree = opts->nocase = 0; 115658156c8fSJan Kara opts->tz_set = 0; 11577669e8fbSSteven J. Magnani opts->nfs = 0; 115885c78591SDenis Karpov opts->errors = FAT_ERRORS_RO; 11591da177e4SLinus Torvalds *debug = 0; 11601da177e4SLinus Torvalds 116138739380SMaciej S. Szmigiero opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat; 116238739380SMaciej S. Szmigiero 11631da177e4SLinus Torvalds if (!options) 11648d44d974SOGAWA Hirofumi goto out; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds while ((p = strsep(&options, ",")) != NULL) { 11671da177e4SLinus Torvalds int token; 11681da177e4SLinus Torvalds if (!*p) 11691da177e4SLinus Torvalds continue; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds token = match_token(p, fat_tokens, args); 11721da177e4SLinus Torvalds if (token == Opt_err) { 11731da177e4SLinus Torvalds if (is_vfat) 11741da177e4SLinus Torvalds token = match_token(p, vfat_tokens, args); 11751da177e4SLinus Torvalds else 11761da177e4SLinus Torvalds token = match_token(p, msdos_tokens, args); 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds switch (token) { 11791da177e4SLinus Torvalds case Opt_check_s: 11801da177e4SLinus Torvalds opts->name_check = 's'; 11811da177e4SLinus Torvalds break; 11821da177e4SLinus Torvalds case Opt_check_r: 11831da177e4SLinus Torvalds opts->name_check = 'r'; 11841da177e4SLinus Torvalds break; 11851da177e4SLinus Torvalds case Opt_check_n: 11861da177e4SLinus Torvalds opts->name_check = 'n'; 11871da177e4SLinus Torvalds break; 118828ec039cSOGAWA Hirofumi case Opt_usefree: 118928ec039cSOGAWA Hirofumi opts->usefree = 1; 119028ec039cSOGAWA Hirofumi break; 11911da177e4SLinus Torvalds case Opt_nocase: 11921da177e4SLinus Torvalds if (!is_vfat) 11931da177e4SLinus Torvalds opts->nocase = 1; 11941da177e4SLinus Torvalds else { 11951da177e4SLinus Torvalds /* for backward compatibility */ 11961da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_WIN95 11971da177e4SLinus Torvalds | VFAT_SFN_CREATE_WIN95; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds break; 12001da177e4SLinus Torvalds case Opt_quiet: 12011da177e4SLinus Torvalds opts->quiet = 1; 12021da177e4SLinus Torvalds break; 12031da177e4SLinus Torvalds case Opt_showexec: 12041da177e4SLinus Torvalds opts->showexec = 1; 12051da177e4SLinus Torvalds break; 12061da177e4SLinus Torvalds case Opt_debug: 12071da177e4SLinus Torvalds *debug = 1; 12081da177e4SLinus Torvalds break; 12091da177e4SLinus Torvalds case Opt_immutable: 12101da177e4SLinus Torvalds opts->sys_immutable = 1; 12111da177e4SLinus Torvalds break; 12121da177e4SLinus Torvalds case Opt_uid: 12131da177e4SLinus Torvalds if (match_int(&args[0], &option)) 12145b3d5aeaSJan Kara return -EINVAL; 1215170782ebSEric W. Biederman opts->fs_uid = make_kuid(current_user_ns(), option); 1216170782ebSEric W. Biederman if (!uid_valid(opts->fs_uid)) 12175b3d5aeaSJan Kara return -EINVAL; 12181da177e4SLinus Torvalds break; 12191da177e4SLinus Torvalds case Opt_gid: 12201da177e4SLinus Torvalds if (match_int(&args[0], &option)) 12215b3d5aeaSJan Kara return -EINVAL; 1222170782ebSEric W. Biederman opts->fs_gid = make_kgid(current_user_ns(), option); 1223170782ebSEric W. Biederman if (!gid_valid(opts->fs_gid)) 12245b3d5aeaSJan Kara return -EINVAL; 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds case Opt_umask: 12271da177e4SLinus Torvalds if (match_octal(&args[0], &option)) 12285b3d5aeaSJan Kara return -EINVAL; 12291da177e4SLinus Torvalds opts->fs_fmask = opts->fs_dmask = option; 12301da177e4SLinus Torvalds break; 12311da177e4SLinus Torvalds case Opt_dmask: 12321da177e4SLinus Torvalds if (match_octal(&args[0], &option)) 12335b3d5aeaSJan Kara return -EINVAL; 12341da177e4SLinus Torvalds opts->fs_dmask = option; 12351da177e4SLinus Torvalds break; 12361da177e4SLinus Torvalds case Opt_fmask: 12371da177e4SLinus Torvalds if (match_octal(&args[0], &option)) 12385b3d5aeaSJan Kara return -EINVAL; 12391da177e4SLinus Torvalds opts->fs_fmask = option; 12401da177e4SLinus Torvalds break; 12411ae43f82SOGAWA Hirofumi case Opt_allow_utime: 12421ae43f82SOGAWA Hirofumi if (match_octal(&args[0], &option)) 12435b3d5aeaSJan Kara return -EINVAL; 12441ae43f82SOGAWA Hirofumi opts->allow_utime = option & (S_IWGRP | S_IWOTH); 12451ae43f82SOGAWA Hirofumi break; 12461da177e4SLinus Torvalds case Opt_codepage: 12471da177e4SLinus Torvalds if (match_int(&args[0], &option)) 12485b3d5aeaSJan Kara return -EINVAL; 12491da177e4SLinus Torvalds opts->codepage = option; 12501da177e4SLinus Torvalds break; 1251ae78bf9cSChris Mason case Opt_flush: 1252ae78bf9cSChris Mason opts->flush = 1; 1253ae78bf9cSChris Mason break; 125458156c8fSJan Kara case Opt_time_offset: 125558156c8fSJan Kara if (match_int(&args[0], &option)) 12565b3d5aeaSJan Kara return -EINVAL; 1257a513d869SJan Kara /* 1258a513d869SJan Kara * GMT+-12 zones may have DST corrections so at least 1259a513d869SJan Kara * 13 hours difference is needed. Make the limit 24 1260a513d869SJan Kara * just in case someone invents something unusual. 1261a513d869SJan Kara */ 1262a513d869SJan Kara if (option < -24 * 60 || option > 24 * 60) 12635b3d5aeaSJan Kara return -EINVAL; 126458156c8fSJan Kara opts->tz_set = 1; 126558156c8fSJan Kara opts->time_offset = option; 126658156c8fSJan Kara break; 1267b271e067SJoe Peterson case Opt_tz_utc: 126858156c8fSJan Kara opts->tz_set = 1; 126958156c8fSJan Kara opts->time_offset = 0; 1270b271e067SJoe Peterson break; 127185c78591SDenis Karpov case Opt_err_cont: 127285c78591SDenis Karpov opts->errors = FAT_ERRORS_CONT; 127385c78591SDenis Karpov break; 127485c78591SDenis Karpov case Opt_err_panic: 127585c78591SDenis Karpov opts->errors = FAT_ERRORS_PANIC; 127685c78591SDenis Karpov break; 127785c78591SDenis Karpov case Opt_err_ro: 127885c78591SDenis Karpov opts->errors = FAT_ERRORS_RO; 127985c78591SDenis Karpov break; 12802628b7a6SNamjae Jeon case Opt_nfs_stale_rw: 12812628b7a6SNamjae Jeon opts->nfs = FAT_NFS_STALE_RW; 12822628b7a6SNamjae Jeon break; 12832628b7a6SNamjae Jeon case Opt_nfs_nostale_ro: 12842628b7a6SNamjae Jeon opts->nfs = FAT_NFS_NOSTALE_RO; 12852628b7a6SNamjae Jeon break; 1286190a8843SConrad Meyer case Opt_dos1xfloppy: 1287190a8843SConrad Meyer opts->dos1xfloppy = 1; 1288190a8843SConrad Meyer break; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /* msdos specific */ 12911da177e4SLinus Torvalds case Opt_dots: 12921da177e4SLinus Torvalds opts->dotsOK = 1; 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds case Opt_nodots: 12951da177e4SLinus Torvalds opts->dotsOK = 0; 12961da177e4SLinus Torvalds break; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds /* vfat specific */ 12991da177e4SLinus Torvalds case Opt_charset: 130035033ab9SOGAWA Hirofumi fat_reset_iocharset(opts); 13011da177e4SLinus Torvalds iocharset = match_strdup(&args[0]); 13021da177e4SLinus Torvalds if (!iocharset) 13031da177e4SLinus Torvalds return -ENOMEM; 13041da177e4SLinus Torvalds opts->iocharset = iocharset; 13051da177e4SLinus Torvalds break; 13061da177e4SLinus Torvalds case Opt_shortname_lower: 13071da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_LOWER 13081da177e4SLinus Torvalds | VFAT_SFN_CREATE_WIN95; 13091da177e4SLinus Torvalds break; 13101da177e4SLinus Torvalds case Opt_shortname_win95: 13111da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_WIN95 13121da177e4SLinus Torvalds | VFAT_SFN_CREATE_WIN95; 13131da177e4SLinus Torvalds break; 13141da177e4SLinus Torvalds case Opt_shortname_winnt: 13151da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_WINNT 13161da177e4SLinus Torvalds | VFAT_SFN_CREATE_WINNT; 13171da177e4SLinus Torvalds break; 13181da177e4SLinus Torvalds case Opt_shortname_mixed: 13191da177e4SLinus Torvalds opts->shortname = VFAT_SFN_DISPLAY_WINNT 13201da177e4SLinus Torvalds | VFAT_SFN_CREATE_WIN95; 13211da177e4SLinus Torvalds break; 13221da177e4SLinus Torvalds case Opt_utf8_no: /* 0 or no or false */ 13231da177e4SLinus Torvalds opts->utf8 = 0; 13241da177e4SLinus Torvalds break; 13251da177e4SLinus Torvalds case Opt_utf8_yes: /* empty or 1 or yes or true */ 13261da177e4SLinus Torvalds opts->utf8 = 1; 13271da177e4SLinus Torvalds break; 13281da177e4SLinus Torvalds case Opt_uni_xl_no: /* 0 or no or false */ 13291da177e4SLinus Torvalds opts->unicode_xlate = 0; 13301da177e4SLinus Torvalds break; 13311da177e4SLinus Torvalds case Opt_uni_xl_yes: /* empty or 1 or yes or true */ 13321da177e4SLinus Torvalds opts->unicode_xlate = 1; 13331da177e4SLinus Torvalds break; 13341da177e4SLinus Torvalds case Opt_nonumtail_no: /* 0 or no or false */ 13351da177e4SLinus Torvalds opts->numtail = 1; /* negated option */ 13361da177e4SLinus Torvalds break; 13371da177e4SLinus Torvalds case Opt_nonumtail_yes: /* empty or 1 or yes or true */ 13381da177e4SLinus Torvalds opts->numtail = 0; /* negated option */ 13391da177e4SLinus Torvalds break; 1340dfc209c0SOGAWA Hirofumi case Opt_rodir: 1341dfc209c0SOGAWA Hirofumi opts->rodir = 1; 1342dfc209c0SOGAWA Hirofumi break; 1343681142f9SChristoph Hellwig case Opt_discard: 1344681142f9SChristoph Hellwig opts->discard = 1; 1345681142f9SChristoph Hellwig break; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds /* obsolete mount options */ 1348d7a83c0fSGeert Uytterhoeven case Opt_obsolete: 1349869f58c0SAlexey Fisher fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, " 1350869f58c0SAlexey Fisher "not supported now", p); 13511da177e4SLinus Torvalds break; 13521da177e4SLinus Torvalds /* unknown option */ 13531da177e4SLinus Torvalds default: 135441a34a4fSChristoph Hellwig if (!silent) { 1355869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, 1356869f58c0SAlexey Fisher "Unrecognized mount option \"%s\" " 1357869f58c0SAlexey Fisher "or missing value", p); 135841a34a4fSChristoph Hellwig } 13591da177e4SLinus Torvalds return -EINVAL; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds } 13628d44d974SOGAWA Hirofumi 13638d44d974SOGAWA Hirofumi out: 13644de151d8SAlexey Dobriyan /* UTF-8 doesn't provide FAT semantics */ 13651da177e4SLinus Torvalds if (!strcmp(opts->iocharset, "utf8")) { 1366186b5370SMihai Moldovan fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset" 13678d44d974SOGAWA Hirofumi " for FAT filesystems, filesystem will be " 1368186b5370SMihai Moldovan "case sensitive!"); 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711ae43f82SOGAWA Hirofumi /* If user doesn't specify allow_utime, it's initialized from dmask. */ 13721ae43f82SOGAWA Hirofumi if (opts->allow_utime == (unsigned short)-1) 13731ae43f82SOGAWA Hirofumi opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH); 13741da177e4SLinus Torvalds if (opts->unicode_xlate) 13751da177e4SLinus Torvalds opts->utf8 = 0; 1376ea3983acSNamjae Jeon if (opts->nfs == FAT_NFS_NOSTALE_RO) { 13771751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 1378ea3983acSNamjae Jeon sb->s_export_op = &fat_export_ops_nostale; 1379ea3983acSNamjae Jeon } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds return 0; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds static int fat_read_root(struct inode *inode) 13851da177e4SLinus Torvalds { 1386a40a7d9dSAlexander Kuleshov struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 13871da177e4SLinus Torvalds int error; 13881da177e4SLinus Torvalds 1389ea3983acSNamjae Jeon MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO; 13901da177e4SLinus Torvalds inode->i_uid = sbi->options.fs_uid; 13911da177e4SLinus Torvalds inode->i_gid = sbi->options.fs_gid; 13922489dbabSJeff Layton inode_inc_iversion(inode); 13931da177e4SLinus Torvalds inode->i_generation = 0; 13949c0aa1b8SOGAWA Hirofumi inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); 13951da177e4SLinus Torvalds inode->i_op = sbi->dir_ops; 13961da177e4SLinus Torvalds inode->i_fop = &fat_dir_operations; 1397306790f7SCarmeli Tamir if (is_fat32(sbi)) { 13981da177e4SLinus Torvalds MSDOS_I(inode)->i_start = sbi->root_cluster; 13991da177e4SLinus Torvalds error = fat_calc_dir_size(inode); 14001da177e4SLinus Torvalds if (error < 0) 14011da177e4SLinus Torvalds return error; 14021da177e4SLinus Torvalds } else { 14031da177e4SLinus Torvalds MSDOS_I(inode)->i_start = 0; 14041da177e4SLinus Torvalds inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) 14071da177e4SLinus Torvalds & ~((loff_t)sbi->cluster_size - 1)) >> 9; 14081da177e4SLinus Torvalds MSDOS_I(inode)->i_logstart = 0; 14091da177e4SLinus Torvalds MSDOS_I(inode)->mmu_private = inode->i_size; 14101da177e4SLinus Torvalds 14119c0aa1b8SOGAWA Hirofumi fat_save_attrs(inode, ATTR_DIR); 14121da177e4SLinus Torvalds inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0; 14131da177e4SLinus Torvalds inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0; 1414bfe86848SMiklos Szeredi set_nlink(inode, fat_subdirs(inode)+2); 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds return 0; 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 14197b92d03cSOGAWA Hirofumi static unsigned long calc_fat_clusters(struct super_block *sb) 14207b92d03cSOGAWA Hirofumi { 14217b92d03cSOGAWA Hirofumi struct msdos_sb_info *sbi = MSDOS_SB(sb); 14227b92d03cSOGAWA Hirofumi 14237b92d03cSOGAWA Hirofumi /* Divide first to avoid overflow */ 1424306790f7SCarmeli Tamir if (!is_fat12(sbi)) { 14257b92d03cSOGAWA Hirofumi unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits; 14267b92d03cSOGAWA Hirofumi return ent_per_sec * sbi->fat_length; 14277b92d03cSOGAWA Hirofumi } 14287b92d03cSOGAWA Hirofumi 14297b92d03cSOGAWA Hirofumi return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits; 14307b92d03cSOGAWA Hirofumi } 14317b92d03cSOGAWA Hirofumi 1432190a8843SConrad Meyer static bool fat_bpb_is_zero(struct fat_boot_sector *b) 1433190a8843SConrad Meyer { 1434190a8843SConrad Meyer if (get_unaligned_le16(&b->sector_size)) 1435190a8843SConrad Meyer return false; 1436190a8843SConrad Meyer if (b->sec_per_clus) 1437190a8843SConrad Meyer return false; 1438190a8843SConrad Meyer if (b->reserved) 1439190a8843SConrad Meyer return false; 1440190a8843SConrad Meyer if (b->fats) 1441190a8843SConrad Meyer return false; 1442190a8843SConrad Meyer if (get_unaligned_le16(&b->dir_entries)) 1443190a8843SConrad Meyer return false; 1444190a8843SConrad Meyer if (get_unaligned_le16(&b->sectors)) 1445190a8843SConrad Meyer return false; 1446190a8843SConrad Meyer if (b->media) 1447190a8843SConrad Meyer return false; 1448190a8843SConrad Meyer if (b->fat_length) 1449190a8843SConrad Meyer return false; 1450190a8843SConrad Meyer if (b->secs_track) 1451190a8843SConrad Meyer return false; 1452190a8843SConrad Meyer if (b->heads) 1453190a8843SConrad Meyer return false; 1454190a8843SConrad Meyer return true; 1455190a8843SConrad Meyer } 1456190a8843SConrad Meyer 1457190a8843SConrad Meyer static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b, 1458190a8843SConrad Meyer int silent, struct fat_bios_param_block *bpb) 1459190a8843SConrad Meyer { 1460190a8843SConrad Meyer int error = -EINVAL; 1461190a8843SConrad Meyer 1462190a8843SConrad Meyer /* Read in BPB ... */ 1463190a8843SConrad Meyer memset(bpb, 0, sizeof(*bpb)); 1464190a8843SConrad Meyer bpb->fat_sector_size = get_unaligned_le16(&b->sector_size); 1465190a8843SConrad Meyer bpb->fat_sec_per_clus = b->sec_per_clus; 1466190a8843SConrad Meyer bpb->fat_reserved = le16_to_cpu(b->reserved); 1467190a8843SConrad Meyer bpb->fat_fats = b->fats; 1468190a8843SConrad Meyer bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries); 1469190a8843SConrad Meyer bpb->fat_sectors = get_unaligned_le16(&b->sectors); 1470190a8843SConrad Meyer bpb->fat_fat_length = le16_to_cpu(b->fat_length); 1471190a8843SConrad Meyer bpb->fat_total_sect = le32_to_cpu(b->total_sect); 1472190a8843SConrad Meyer 1473190a8843SConrad Meyer bpb->fat16_state = b->fat16.state; 1474190a8843SConrad Meyer bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id); 1475190a8843SConrad Meyer 1476190a8843SConrad Meyer bpb->fat32_length = le32_to_cpu(b->fat32.length); 1477190a8843SConrad Meyer bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster); 1478190a8843SConrad Meyer bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector); 1479190a8843SConrad Meyer bpb->fat32_state = b->fat32.state; 1480190a8843SConrad Meyer bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id); 1481190a8843SConrad Meyer 1482190a8843SConrad Meyer /* Validate this looks like a FAT filesystem BPB */ 1483190a8843SConrad Meyer if (!bpb->fat_reserved) { 1484190a8843SConrad Meyer if (!silent) 1485190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1486190a8843SConrad Meyer "bogus number of reserved sectors"); 1487190a8843SConrad Meyer goto out; 1488190a8843SConrad Meyer } 1489190a8843SConrad Meyer if (!bpb->fat_fats) { 1490190a8843SConrad Meyer if (!silent) 1491190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus number of FAT structure"); 1492190a8843SConrad Meyer goto out; 1493190a8843SConrad Meyer } 1494190a8843SConrad Meyer 1495190a8843SConrad Meyer /* 1496190a8843SConrad Meyer * Earlier we checked here that b->secs_track and b->head are nonzero, 1497190a8843SConrad Meyer * but it turns out valid FAT filesystems can have zero there. 1498190a8843SConrad Meyer */ 1499190a8843SConrad Meyer 1500190a8843SConrad Meyer if (!fat_valid_media(b->media)) { 1501190a8843SConrad Meyer if (!silent) 1502190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)", 1503190a8843SConrad Meyer (unsigned)b->media); 1504190a8843SConrad Meyer goto out; 1505190a8843SConrad Meyer } 1506190a8843SConrad Meyer 1507190a8843SConrad Meyer if (!is_power_of_2(bpb->fat_sector_size) 1508190a8843SConrad Meyer || (bpb->fat_sector_size < 512) 1509190a8843SConrad Meyer || (bpb->fat_sector_size > 4096)) { 1510190a8843SConrad Meyer if (!silent) 1511190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus logical sector size %u", 1512190a8843SConrad Meyer (unsigned)bpb->fat_sector_size); 1513190a8843SConrad Meyer goto out; 1514190a8843SConrad Meyer } 1515190a8843SConrad Meyer 1516190a8843SConrad Meyer if (!is_power_of_2(bpb->fat_sec_per_clus)) { 1517190a8843SConrad Meyer if (!silent) 1518190a8843SConrad Meyer fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u", 1519190a8843SConrad Meyer (unsigned)bpb->fat_sec_per_clus); 1520190a8843SConrad Meyer goto out; 1521190a8843SConrad Meyer } 1522190a8843SConrad Meyer 1523b1b65750SOGAWA Hirofumi if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) { 1524b1b65750SOGAWA Hirofumi if (!silent) 1525b1b65750SOGAWA Hirofumi fat_msg(sb, KERN_ERR, "bogus number of FAT sectors"); 1526b1b65750SOGAWA Hirofumi goto out; 1527b1b65750SOGAWA Hirofumi } 1528b1b65750SOGAWA Hirofumi 1529190a8843SConrad Meyer error = 0; 1530190a8843SConrad Meyer 1531190a8843SConrad Meyer out: 1532190a8843SConrad Meyer return error; 1533190a8843SConrad Meyer } 1534190a8843SConrad Meyer 1535190a8843SConrad Meyer static int fat_read_static_bpb(struct super_block *sb, 1536190a8843SConrad Meyer struct fat_boot_sector *b, int silent, 1537190a8843SConrad Meyer struct fat_bios_param_block *bpb) 1538190a8843SConrad Meyer { 1539190a8843SConrad Meyer static const char *notdos1x = "This doesn't look like a DOS 1.x volume"; 15409e48243bSChristoph Hellwig sector_t bd_sects = bdev_nr_sectors(sb->s_bdev); 1541190a8843SConrad Meyer struct fat_floppy_defaults *fdefaults = NULL; 1542190a8843SConrad Meyer int error = -EINVAL; 1543190a8843SConrad Meyer unsigned i; 1544190a8843SConrad Meyer 1545190a8843SConrad Meyer /* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */ 1546190a8843SConrad Meyer if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) { 1547190a8843SConrad Meyer if (!silent) 1548190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1549190a8843SConrad Meyer "%s; no bootstrapping code", notdos1x); 1550190a8843SConrad Meyer goto out; 1551190a8843SConrad Meyer } 1552190a8843SConrad Meyer 1553190a8843SConrad Meyer /* 1554190a8843SConrad Meyer * If any value in this region is non-zero, it isn't archaic 1555190a8843SConrad Meyer * DOS. 1556190a8843SConrad Meyer */ 1557190a8843SConrad Meyer if (!fat_bpb_is_zero(b)) { 1558190a8843SConrad Meyer if (!silent) 1559190a8843SConrad Meyer fat_msg(sb, KERN_ERR, 1560190a8843SConrad Meyer "%s; DOS 2.x BPB is non-zero", notdos1x); 1561190a8843SConrad Meyer goto out; 1562190a8843SConrad Meyer } 1563190a8843SConrad Meyer 1564190a8843SConrad Meyer for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) { 1565190a8843SConrad Meyer if (floppy_defaults[i].nr_sectors == bd_sects) { 1566190a8843SConrad Meyer fdefaults = &floppy_defaults[i]; 1567190a8843SConrad Meyer break; 1568190a8843SConrad Meyer } 1569190a8843SConrad Meyer } 1570190a8843SConrad Meyer 1571190a8843SConrad Meyer if (fdefaults == NULL) { 1572190a8843SConrad Meyer if (!silent) 1573190a8843SConrad Meyer fat_msg(sb, KERN_WARNING, 1574190a8843SConrad Meyer "This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)", 1575190a8843SConrad Meyer (u64)bd_sects); 1576190a8843SConrad Meyer goto out; 1577190a8843SConrad Meyer } 1578190a8843SConrad Meyer 1579190a8843SConrad Meyer if (!silent) 1580190a8843SConrad Meyer fat_msg(sb, KERN_INFO, 1581190a8843SConrad Meyer "This looks like a DOS 1.x volume; assuming default BPB values"); 1582190a8843SConrad Meyer 1583190a8843SConrad Meyer memset(bpb, 0, sizeof(*bpb)); 1584190a8843SConrad Meyer bpb->fat_sector_size = SECTOR_SIZE; 1585190a8843SConrad Meyer bpb->fat_sec_per_clus = fdefaults->sec_per_clus; 1586190a8843SConrad Meyer bpb->fat_reserved = 1; 1587190a8843SConrad Meyer bpb->fat_fats = 2; 1588190a8843SConrad Meyer bpb->fat_dir_entries = fdefaults->dir_entries; 1589190a8843SConrad Meyer bpb->fat_sectors = fdefaults->nr_sectors; 1590190a8843SConrad Meyer bpb->fat_fat_length = fdefaults->fat_length; 1591190a8843SConrad Meyer 1592190a8843SConrad Meyer error = 0; 1593190a8843SConrad Meyer 1594190a8843SConrad Meyer out: 1595190a8843SConrad Meyer return error; 1596190a8843SConrad Meyer } 1597190a8843SConrad Meyer 15981da177e4SLinus Torvalds /* 15991da177e4SLinus Torvalds * Read the super block of an MS-DOS FS. 16001da177e4SLinus Torvalds */ 1601384f5c96SOGAWA Hirofumi int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat, 16023d23985dSAl Viro void (*setup)(struct super_block *)) 16031da177e4SLinus Torvalds { 1604b522412aSAl Viro struct inode *root_inode = NULL, *fat_inode = NULL; 1605020ac5b6SArtem Bityutskiy struct inode *fsinfo_inode = NULL; 16061da177e4SLinus Torvalds struct buffer_head *bh; 1607190a8843SConrad Meyer struct fat_bios_param_block bpb; 16081da177e4SLinus Torvalds struct msdos_sb_info *sbi; 16091da177e4SLinus Torvalds u16 logical_sector_size; 16101da177e4SLinus Torvalds u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; 16111da177e4SLinus Torvalds int debug; 16121da177e4SLinus Torvalds long error; 16131da177e4SLinus Torvalds char buf[50]; 1614c0da64f6SDeepa Dinamani struct timespec64 ts; 16151da177e4SLinus Torvalds 16168f593427SLinus Torvalds /* 16178f593427SLinus Torvalds * GFP_KERNEL is ok here, because while we do hold the 1618dcc381e8SZheng Lv * superblock lock, memory pressure can't call back into 16198f593427SLinus Torvalds * the filesystem, since we're only just about to mount 16208f593427SLinus Torvalds * it and have no inodes etc active! 16218f593427SLinus Torvalds */ 1622f8314dc6SPanagiotis Issaris sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); 16231da177e4SLinus Torvalds if (!sbi) 16241da177e4SLinus Torvalds return -ENOMEM; 16251da177e4SLinus Torvalds sb->s_fs_info = sbi; 16261da177e4SLinus Torvalds 16271751e8a6SLinus Torvalds sb->s_flags |= SB_NODIRATIME; 16281da177e4SLinus Torvalds sb->s_magic = MSDOS_SUPER_MAGIC; 16291da177e4SLinus Torvalds sb->s_op = &fat_sops; 16301da177e4SLinus Torvalds sb->s_export_op = &fat_export_ops; 16316bb885ecSFrank Sorenson /* 16326bb885ecSFrank Sorenson * fat timestamps are complex and truncated by fat itself, so 16336bb885ecSFrank Sorenson * we set 1 here to be fast 16346bb885ecSFrank Sorenson */ 16356bb885ecSFrank Sorenson sb->s_time_gran = 1; 16368fceb4e0SNamjae Jeon mutex_init(&sbi->nfs_build_inode_lock); 1637aaa04b48SOGAWA Hirofumi ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, 1638aaa04b48SOGAWA Hirofumi DEFAULT_RATELIMIT_BURST); 16391da177e4SLinus Torvalds 1640869f58c0SAlexey Fisher error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options); 16411da177e4SLinus Torvalds if (error) 16421da177e4SLinus Torvalds goto out_fail; 16431da177e4SLinus Torvalds 16443d23985dSAl Viro setup(sb); /* flavour-specific stuff that needs options */ 16453d23985dSAl Viro 16461da177e4SLinus Torvalds error = -EIO; 16471da177e4SLinus Torvalds sb_min_blocksize(sb, 512); 16481da177e4SLinus Torvalds bh = sb_bread(sb, 0); 16491da177e4SLinus Torvalds if (bh == NULL) { 1650869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read boot sector"); 16511da177e4SLinus Torvalds goto out_fail; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 1654190a8843SConrad Meyer error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent, 1655190a8843SConrad Meyer &bpb); 1656190a8843SConrad Meyer if (error == -EINVAL && sbi->options.dos1xfloppy) 1657190a8843SConrad Meyer error = fat_read_static_bpb(sb, 1658190a8843SConrad Meyer (struct fat_boot_sector *)bh->b_data, silent, &bpb); 16591da177e4SLinus Torvalds brelse(bh); 16601da177e4SLinus Torvalds 1661190a8843SConrad Meyer if (error == -EINVAL) 1662190a8843SConrad Meyer goto out_invalid; 1663190a8843SConrad Meyer else if (error) 1664190a8843SConrad Meyer goto out_fail; 16651da177e4SLinus Torvalds 1666190a8843SConrad Meyer logical_sector_size = bpb.fat_sector_size; 1667190a8843SConrad Meyer sbi->sec_per_clus = bpb.fat_sec_per_clus; 16681da177e4SLinus Torvalds 1669190a8843SConrad Meyer error = -EIO; 16701da177e4SLinus Torvalds if (logical_sector_size < sb->s_blocksize) { 1671869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "logical sector size too small for device" 1672869f58c0SAlexey Fisher " (logical sector size = %u)", logical_sector_size); 16731da177e4SLinus Torvalds goto out_fail; 16741da177e4SLinus Torvalds } 1675190a8843SConrad Meyer 16761da177e4SLinus Torvalds if (logical_sector_size > sb->s_blocksize) { 1677190a8843SConrad Meyer struct buffer_head *bh_resize; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds if (!sb_set_blocksize(sb, logical_sector_size)) { 1680869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to set blocksize %u", 16811da177e4SLinus Torvalds logical_sector_size); 16821da177e4SLinus Torvalds goto out_fail; 16831da177e4SLinus Torvalds } 1684190a8843SConrad Meyer 1685190a8843SConrad Meyer /* Verify that the larger boot sector is fully readable */ 1686190a8843SConrad Meyer bh_resize = sb_bread(sb, 0); 1687190a8843SConrad Meyer if (bh_resize == NULL) { 1688869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "unable to read boot sector" 1689869f58c0SAlexey Fisher " (logical sector size = %lu)", 16901da177e4SLinus Torvalds sb->s_blocksize); 16911da177e4SLinus Torvalds goto out_fail; 16921da177e4SLinus Torvalds } 1693190a8843SConrad Meyer brelse(bh_resize); 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 1696e40b34c7SMarco Stornelli mutex_init(&sbi->s_lock); 16971da177e4SLinus Torvalds sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus; 16981da177e4SLinus Torvalds sbi->cluster_bits = ffs(sbi->cluster_size) - 1; 1699190a8843SConrad Meyer sbi->fats = bpb.fat_fats; 17001da177e4SLinus Torvalds sbi->fat_bits = 0; /* Don't know yet */ 1701190a8843SConrad Meyer sbi->fat_start = bpb.fat_reserved; 1702190a8843SConrad Meyer sbi->fat_length = bpb.fat_fat_length; 17031da177e4SLinus Torvalds sbi->root_cluster = 0; 17041da177e4SLinus Torvalds sbi->free_clusters = -1; /* Don't know yet */ 1705606e423eSOGAWA Hirofumi sbi->free_clus_valid = 0; 17061da177e4SLinus Torvalds sbi->prev_free = FAT_START_ENT; 1707710d4403SNamjae Jeon sb->s_maxbytes = 0xffffffff; 1708c0da64f6SDeepa Dinamani fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0); 1709c0da64f6SDeepa Dinamani sb->s_time_min = ts.tv_sec; 1710c0da64f6SDeepa Dinamani 1711c0da64f6SDeepa Dinamani fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX), 1712c0da64f6SDeepa Dinamani cpu_to_le16(FAT_DATE_MAX), 0); 1713c0da64f6SDeepa Dinamani sb->s_time_max = ts.tv_sec; 17141da177e4SLinus Torvalds 1715190a8843SConrad Meyer if (!sbi->fat_length && bpb.fat32_length) { 17161da177e4SLinus Torvalds struct fat_boot_fsinfo *fsinfo; 17171da177e4SLinus Torvalds struct buffer_head *fsinfo_bh; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /* Must be FAT32 */ 17201da177e4SLinus Torvalds sbi->fat_bits = 32; 1721190a8843SConrad Meyer sbi->fat_length = bpb.fat32_length; 1722190a8843SConrad Meyer sbi->root_cluster = bpb.fat32_root_cluster; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* MC - if info_sector is 0, don't multiply by 0 */ 1725190a8843SConrad Meyer sbi->fsinfo_sector = bpb.fat32_info_sector; 17261da177e4SLinus Torvalds if (sbi->fsinfo_sector == 0) 17271da177e4SLinus Torvalds sbi->fsinfo_sector = 1; 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector); 17301da177e4SLinus Torvalds if (fsinfo_bh == NULL) { 1731869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "bread failed, FSINFO block" 1732869f58c0SAlexey Fisher " (sector = %lu)", sbi->fsinfo_sector); 17331da177e4SLinus Torvalds goto out_fail; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; 17371da177e4SLinus Torvalds if (!IS_FSINFO(fsinfo)) { 1738869f58c0SAlexey Fisher fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: " 1739869f58c0SAlexey Fisher "0x%08x, 0x%08x (sector = %lu)", 17401da177e4SLinus Torvalds le32_to_cpu(fsinfo->signature1), 17411da177e4SLinus Torvalds le32_to_cpu(fsinfo->signature2), 17421da177e4SLinus Torvalds sbi->fsinfo_sector); 17431da177e4SLinus Torvalds } else { 174428ec039cSOGAWA Hirofumi if (sbi->options.usefree) 1745606e423eSOGAWA Hirofumi sbi->free_clus_valid = 1; 1746606e423eSOGAWA Hirofumi sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters); 17471da177e4SLinus Torvalds sbi->prev_free = le32_to_cpu(fsinfo->next_cluster); 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds brelse(fsinfo_bh); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17536e5b93eeSMike Lockwood /* interpret volume ID as a little endian 32 bit integer */ 1754306790f7SCarmeli Tamir if (is_fat32(sbi)) 1755190a8843SConrad Meyer sbi->vol_id = bpb.fat32_vol_id; 17566e5b93eeSMike Lockwood else /* fat 16 or 12 */ 1757190a8843SConrad Meyer sbi->vol_id = bpb.fat16_vol_id; 17586e5b93eeSMike Lockwood 17591da177e4SLinus Torvalds sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); 17601da177e4SLinus Torvalds sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; 1763190a8843SConrad Meyer sbi->dir_entries = bpb.fat_dir_entries; 17641da177e4SLinus Torvalds if (sbi->dir_entries & (sbi->dir_per_block - 1)) { 17651da177e4SLinus Torvalds if (!silent) 176634df1174SZheng Lv fat_msg(sb, KERN_ERR, "bogus number of directory entries" 1767869f58c0SAlexey Fisher " (%u)", sbi->dir_entries); 17681da177e4SLinus Torvalds goto out_invalid; 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds rootdir_sectors = sbi->dir_entries 17721da177e4SLinus Torvalds * sizeof(struct msdos_dir_entry) / sb->s_blocksize; 17731da177e4SLinus Torvalds sbi->data_start = sbi->dir_start + rootdir_sectors; 1774190a8843SConrad Meyer total_sectors = bpb.fat_sectors; 17751da177e4SLinus Torvalds if (total_sectors == 0) 1776190a8843SConrad Meyer total_sectors = bpb.fat_total_sect; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; 17791da177e4SLinus Torvalds 1780306790f7SCarmeli Tamir if (!is_fat32(sbi)) 17811da177e4SLinus Torvalds sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; 17821da177e4SLinus Torvalds 1783b88a1058SOleksij Rempel /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */ 1784306790f7SCarmeli Tamir if (is_fat32(sbi)) 1785190a8843SConrad Meyer sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY; 1786b88a1058SOleksij Rempel else /* fat 16 or 12 */ 1787190a8843SConrad Meyer sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY; 1788b88a1058SOleksij Rempel 17891da177e4SLinus Torvalds /* check that FAT table does not overflow */ 17907b92d03cSOGAWA Hirofumi fat_clusters = calc_fat_clusters(sb); 17911da177e4SLinus Torvalds total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); 1792d19dc016SCarmeli Tamir if (total_clusters > max_fat(sb)) { 17931da177e4SLinus Torvalds if (!silent) 1794869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "count of clusters too big (%u)", 17951da177e4SLinus Torvalds total_clusters); 17961da177e4SLinus Torvalds goto out_invalid; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds sbi->max_cluster = total_clusters + FAT_START_ENT; 18001da177e4SLinus Torvalds /* check the free_clusters, it's not necessarily correct */ 18011da177e4SLinus Torvalds if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters) 18021da177e4SLinus Torvalds sbi->free_clusters = -1; 18031da177e4SLinus Torvalds /* check the prev_free, it's not necessarily correct */ 18041da177e4SLinus Torvalds sbi->prev_free %= sbi->max_cluster; 18051da177e4SLinus Torvalds if (sbi->prev_free < FAT_START_ENT) 18061da177e4SLinus Torvalds sbi->prev_free = FAT_START_ENT; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* set up enough so that it can read an inode */ 18091da177e4SLinus Torvalds fat_hash_init(sb); 18107669e8fbSSteven J. Magnani dir_hash_init(sb); 18111da177e4SLinus Torvalds fat_ent_access_init(sb); 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds /* 1814b553337aSCarmeli Tamir * The low byte of the first FAT entry must have the same value as 1815b553337aSCarmeli Tamir * the media field of the boot sector. But in real world, too many 1816b553337aSCarmeli Tamir * devices are writing wrong values. So, removed that validity check. 18171da177e4SLinus Torvalds * 1818b553337aSCarmeli Tamir * The removed check compared the first FAT entry to a value dependent 1819b553337aSCarmeli Tamir * on the media field like this: 1820b553337aSCarmeli Tamir * == (0x0F00 | media), for FAT12 1821b553337aSCarmeli Tamir * == (0XFF00 | media), for FAT16 1822b553337aSCarmeli Tamir * == (0x0FFFFF | media), for FAT32 18231da177e4SLinus Torvalds */ 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds error = -EINVAL; 18261da177e4SLinus Torvalds sprintf(buf, "cp%d", sbi->options.codepage); 18271da177e4SLinus Torvalds sbi->nls_disk = load_nls(buf); 18281da177e4SLinus Torvalds if (!sbi->nls_disk) { 1829869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "codepage %s not found", buf); 18301da177e4SLinus Torvalds goto out_fail; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* FIXME: utf8 is using iocharset for upper/lower conversion */ 18341da177e4SLinus Torvalds if (sbi->options.isvfat) { 18351da177e4SLinus Torvalds sbi->nls_io = load_nls(sbi->options.iocharset); 18361da177e4SLinus Torvalds if (!sbi->nls_io) { 1837869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "IO charset %s not found", 18381da177e4SLinus Torvalds sbi->options.iocharset); 18391da177e4SLinus Torvalds goto out_fail; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds error = -ENOMEM; 1844b522412aSAl Viro fat_inode = new_inode(sb); 1845b522412aSAl Viro if (!fat_inode) 1846b522412aSAl Viro goto out_fail; 1847b522412aSAl Viro sbi->fat_inode = fat_inode; 1848020ac5b6SArtem Bityutskiy 1849020ac5b6SArtem Bityutskiy fsinfo_inode = new_inode(sb); 1850020ac5b6SArtem Bityutskiy if (!fsinfo_inode) 1851020ac5b6SArtem Bityutskiy goto out_fail; 1852020ac5b6SArtem Bityutskiy fsinfo_inode->i_ino = MSDOS_FSINFO_INO; 1853020ac5b6SArtem Bityutskiy sbi->fsinfo_inode = fsinfo_inode; 1854020ac5b6SArtem Bityutskiy insert_inode_hash(fsinfo_inode); 1855020ac5b6SArtem Bityutskiy 18561da177e4SLinus Torvalds root_inode = new_inode(sb); 18571da177e4SLinus Torvalds if (!root_inode) 18581da177e4SLinus Torvalds goto out_fail; 18591da177e4SLinus Torvalds root_inode->i_ino = MSDOS_ROOT_INO; 18602489dbabSJeff Layton inode_set_iversion(root_inode, 1); 18611da177e4SLinus Torvalds error = fat_read_root(root_inode); 18621688f860SAl Viro if (error < 0) { 18631688f860SAl Viro iput(root_inode); 18641da177e4SLinus Torvalds goto out_fail; 18651688f860SAl Viro } 18661da177e4SLinus Torvalds error = -ENOMEM; 18671da177e4SLinus Torvalds insert_inode_hash(root_inode); 18687669e8fbSSteven J. Magnani fat_attach(root_inode, 0); 18691688f860SAl Viro sb->s_root = d_make_root(root_inode); 18701da177e4SLinus Torvalds if (!sb->s_root) { 1871869f58c0SAlexey Fisher fat_msg(sb, KERN_ERR, "get root inode failed"); 18721da177e4SLinus Torvalds goto out_fail; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 1875f562146aSNamjae Jeon if (sbi->options.discard) { 1876f562146aSNamjae Jeon struct request_queue *q = bdev_get_queue(sb->s_bdev); 1877f562146aSNamjae Jeon if (!blk_queue_discard(q)) 1878f562146aSNamjae Jeon fat_msg(sb, KERN_WARNING, 1879f562146aSNamjae Jeon "mounting with \"discard\" option, but " 1880f562146aSNamjae Jeon "the device does not support discard"); 1881f562146aSNamjae Jeon } 1882f562146aSNamjae Jeon 1883b88a1058SOleksij Rempel fat_set_state(sb, 1, 0); 18841da177e4SLinus Torvalds return 0; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds out_invalid: 18871da177e4SLinus Torvalds error = -EINVAL; 18881da177e4SLinus Torvalds if (!silent) 1889869f58c0SAlexey Fisher fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem"); 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds out_fail: 1892020ac5b6SArtem Bityutskiy if (fsinfo_inode) 1893020ac5b6SArtem Bityutskiy iput(fsinfo_inode); 1894b522412aSAl Viro if (fat_inode) 1895b522412aSAl Viro iput(fat_inode); 18961da177e4SLinus Torvalds unload_nls(sbi->nls_io); 18971da177e4SLinus Torvalds unload_nls(sbi->nls_disk); 189835033ab9SOGAWA Hirofumi fat_reset_iocharset(&sbi->options); 18991da177e4SLinus Torvalds sb->s_fs_info = NULL; 19001da177e4SLinus Torvalds kfree(sbi); 19011da177e4SLinus Torvalds return error; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds 19047c709d00SOGAWA Hirofumi EXPORT_SYMBOL_GPL(fat_fill_super); 19051da177e4SLinus Torvalds 1906ae78bf9cSChris Mason /* 1907ae78bf9cSChris Mason * helper function for fat_flush_inodes. This writes both the inode 1908ae78bf9cSChris Mason * and the file data blocks, waiting for in flight data blocks before 1909ae78bf9cSChris Mason * the start of the call. It does not wait for any io started 1910ae78bf9cSChris Mason * during the call 1911ae78bf9cSChris Mason */ 1912ae78bf9cSChris Mason static int writeback_inode(struct inode *inode) 1913ae78bf9cSChris Mason { 1914ae78bf9cSChris Mason 1915ae78bf9cSChris Mason int ret; 191614864655SNamjae Jeon 191714864655SNamjae Jeon /* if we used wait=1, sync_inode_metadata waits for the io for the 191814864655SNamjae Jeon * inode to finish. So wait=0 is sent down to sync_inode_metadata 1919ae78bf9cSChris Mason * and filemap_fdatawrite is used for the data blocks 1920ae78bf9cSChris Mason */ 192114864655SNamjae Jeon ret = sync_inode_metadata(inode, 0); 1922ae78bf9cSChris Mason if (!ret) 192314864655SNamjae Jeon ret = filemap_fdatawrite(inode->i_mapping); 1924ae78bf9cSChris Mason return ret; 1925ae78bf9cSChris Mason } 1926ae78bf9cSChris Mason 1927ae78bf9cSChris Mason /* 1928ae78bf9cSChris Mason * write data and metadata corresponding to i1 and i2. The io is 1929ae78bf9cSChris Mason * started but we do not wait for any of it to finish. 1930ae78bf9cSChris Mason * 1931ae78bf9cSChris Mason * filemap_flush is used for the block device, so if there is a dirty 1932ae78bf9cSChris Mason * page for a block already in flight, we will not wait and start the 1933ae78bf9cSChris Mason * io over again 1934ae78bf9cSChris Mason */ 1935ae78bf9cSChris Mason int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2) 1936ae78bf9cSChris Mason { 1937ae78bf9cSChris Mason int ret = 0; 1938ae78bf9cSChris Mason if (!MSDOS_SB(sb)->options.flush) 1939ae78bf9cSChris Mason return 0; 1940ae78bf9cSChris Mason if (i1) 1941ae78bf9cSChris Mason ret = writeback_inode(i1); 1942ae78bf9cSChris Mason if (!ret && i2) 1943ae78bf9cSChris Mason ret = writeback_inode(i2); 1944cb9568eeSChristoph Hellwig if (!ret) 1945cb9568eeSChristoph Hellwig ret = sync_blockdev_nowait(sb->s_bdev); 1946ae78bf9cSChris Mason return ret; 1947ae78bf9cSChris Mason } 1948ae78bf9cSChris Mason EXPORT_SYMBOL_GPL(fat_flush_inodes); 1949ae78bf9cSChris Mason 19501da177e4SLinus Torvalds static int __init init_fat_fs(void) 19511da177e4SLinus Torvalds { 1952532a39a3SPekka J Enberg int err; 19531da177e4SLinus Torvalds 1954532a39a3SPekka J Enberg err = fat_cache_init(); 1955532a39a3SPekka J Enberg if (err) 1956532a39a3SPekka J Enberg return err; 1957532a39a3SPekka J Enberg 1958532a39a3SPekka J Enberg err = fat_init_inodecache(); 1959532a39a3SPekka J Enberg if (err) 1960532a39a3SPekka J Enberg goto failed; 1961532a39a3SPekka J Enberg 1962532a39a3SPekka J Enberg return 0; 1963532a39a3SPekka J Enberg 1964532a39a3SPekka J Enberg failed: 1965532a39a3SPekka J Enberg fat_cache_destroy(); 1966532a39a3SPekka J Enberg return err; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds static void __exit exit_fat_fs(void) 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds fat_cache_destroy(); 19721da177e4SLinus Torvalds fat_destroy_inodecache(); 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds module_init(init_fat_fs) 19761da177e4SLinus Torvalds module_exit(exit_fat_fs) 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1979