| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * linux/fs/fat/inode.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 20 | 21 | #include <linux/blkdev.h> |
|---|
| 21 | 22 | #include <linux/backing-dev.h> |
|---|
| 22 | 23 | #include <asm/unaligned.h> |
|---|
| 24 | +#include <linux/random.h> |
|---|
| 23 | 25 | #include <linux/iversion.h> |
|---|
| 24 | 26 | #include "fat.h" |
|---|
| 25 | 27 | |
|---|
| .. | .. |
|---|
| 29 | 31 | #endif |
|---|
| 30 | 32 | |
|---|
| 31 | 33 | #define KB_IN_SECTORS 2 |
|---|
| 34 | + |
|---|
| 35 | +/* DOS dates from 1980/1/1 through 2107/12/31 */ |
|---|
| 36 | +#define FAT_DATE_MIN (0<<9 | 1<<5 | 1) |
|---|
| 37 | +#define FAT_DATE_MAX (127<<9 | 12<<5 | 31) |
|---|
| 38 | +#define FAT_TIME_MAX (23<<11 | 59<<5 | 29) |
|---|
| 32 | 39 | |
|---|
| 33 | 40 | /* |
|---|
| 34 | 41 | * A deserialized copy of the on-disk structure laid out in struct |
|---|
| .. | .. |
|---|
| 203 | 210 | return mpage_readpage(page, fat_get_block); |
|---|
| 204 | 211 | } |
|---|
| 205 | 212 | |
|---|
| 206 | | -static int fat_readpages(struct file *file, struct address_space *mapping, |
|---|
| 207 | | - struct list_head *pages, unsigned nr_pages) |
|---|
| 213 | +static void fat_readahead(struct readahead_control *rac) |
|---|
| 208 | 214 | { |
|---|
| 209 | | - return mpage_readpages(mapping, pages, nr_pages, fat_get_block); |
|---|
| 215 | + mpage_readahead(rac, fat_get_block); |
|---|
| 210 | 216 | } |
|---|
| 211 | 217 | |
|---|
| 212 | 218 | static void fat_write_failed(struct address_space *mapping, loff_t to) |
|---|
| .. | .. |
|---|
| 244 | 250 | if (err < len) |
|---|
| 245 | 251 | fat_write_failed(mapping, pos + len); |
|---|
| 246 | 252 | if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { |
|---|
| 247 | | - inode->i_mtime = inode->i_ctime = current_time(inode); |
|---|
| 253 | + fat_truncate_time(inode, NULL, S_CTIME|S_MTIME); |
|---|
| 248 | 254 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; |
|---|
| 249 | 255 | mark_inode_dirty(inode); |
|---|
| 250 | 256 | } |
|---|
| .. | .. |
|---|
| 337 | 343 | |
|---|
| 338 | 344 | static const struct address_space_operations fat_aops = { |
|---|
| 339 | 345 | .readpage = fat_readpage, |
|---|
| 340 | | - .readpages = fat_readpages, |
|---|
| 346 | + .readahead = fat_readahead, |
|---|
| 341 | 347 | .writepage = fat_writepage, |
|---|
| 342 | 348 | .writepages = fat_writepages, |
|---|
| 343 | 349 | .write_begin = fat_write_begin, |
|---|
| .. | .. |
|---|
| 515 | 521 | inode->i_uid = sbi->options.fs_uid; |
|---|
| 516 | 522 | inode->i_gid = sbi->options.fs_gid; |
|---|
| 517 | 523 | inode_inc_iversion(inode); |
|---|
| 518 | | - inode->i_generation = get_seconds(); |
|---|
| 524 | + inode->i_generation = prandom_u32(); |
|---|
| 519 | 525 | |
|---|
| 520 | 526 | if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { |
|---|
| 521 | 527 | inode->i_generation &= ~1; |
|---|
| .. | .. |
|---|
| 564 | 570 | de->cdate, de->ctime_cs); |
|---|
| 565 | 571 | fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0); |
|---|
| 566 | 572 | } else |
|---|
| 567 | | - inode->i_ctime = inode->i_atime = inode->i_mtime; |
|---|
| 573 | + fat_truncate_time(inode, &inode->i_mtime, S_ATIME|S_CTIME); |
|---|
| 568 | 574 | |
|---|
| 569 | 575 | return 0; |
|---|
| 570 | 576 | } |
|---|
| .. | .. |
|---|
| 686 | 692 | |
|---|
| 687 | 693 | b = (struct fat_boot_sector *) bh->b_data; |
|---|
| 688 | 694 | |
|---|
| 689 | | - if (sbi->fat_bits == 32) { |
|---|
| 695 | + if (is_fat32(sbi)) { |
|---|
| 690 | 696 | if (set) |
|---|
| 691 | 697 | b->fat32.state |= FAT_STATE_DIRTY; |
|---|
| 692 | 698 | else |
|---|
| .. | .. |
|---|
| 753 | 759 | return &ei->vfs_inode; |
|---|
| 754 | 760 | } |
|---|
| 755 | 761 | |
|---|
| 756 | | -static void fat_i_callback(struct rcu_head *head) |
|---|
| 762 | +static void fat_free_inode(struct inode *inode) |
|---|
| 757 | 763 | { |
|---|
| 758 | | - struct inode *inode = container_of(head, struct inode, i_rcu); |
|---|
| 759 | 764 | kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); |
|---|
| 760 | | -} |
|---|
| 761 | | - |
|---|
| 762 | | -static void fat_destroy_inode(struct inode *inode) |
|---|
| 763 | | -{ |
|---|
| 764 | | - call_rcu(&inode->i_rcu, fat_i_callback); |
|---|
| 765 | 765 | } |
|---|
| 766 | 766 | |
|---|
| 767 | 767 | static void init_once(void *foo) |
|---|
| .. | .. |
|---|
| 836 | 836 | buf->f_blocks = sbi->max_cluster - FAT_START_ENT; |
|---|
| 837 | 837 | buf->f_bfree = sbi->free_clusters; |
|---|
| 838 | 838 | buf->f_bavail = sbi->free_clusters; |
|---|
| 839 | | - buf->f_fsid.val[0] = (u32)id; |
|---|
| 840 | | - buf->f_fsid.val[1] = (u32)(id >> 32); |
|---|
| 839 | + buf->f_fsid = u64_to_fsid(id); |
|---|
| 841 | 840 | buf->f_namelen = |
|---|
| 842 | 841 | (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE; |
|---|
| 843 | 842 | |
|---|
| .. | .. |
|---|
| 927 | 926 | static int fat_show_options(struct seq_file *m, struct dentry *root); |
|---|
| 928 | 927 | static const struct super_operations fat_sops = { |
|---|
| 929 | 928 | .alloc_inode = fat_alloc_inode, |
|---|
| 930 | | - .destroy_inode = fat_destroy_inode, |
|---|
| 929 | + .free_inode = fat_free_inode, |
|---|
| 931 | 930 | .write_inode = fat_write_inode, |
|---|
| 932 | 931 | .evict_inode = fat_evict_inode, |
|---|
| 933 | 932 | .put_super = fat_put_super, |
|---|
| .. | .. |
|---|
| 1393 | 1392 | inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); |
|---|
| 1394 | 1393 | inode->i_op = sbi->dir_ops; |
|---|
| 1395 | 1394 | inode->i_fop = &fat_dir_operations; |
|---|
| 1396 | | - if (sbi->fat_bits == 32) { |
|---|
| 1395 | + if (is_fat32(sbi)) { |
|---|
| 1397 | 1396 | MSDOS_I(inode)->i_start = sbi->root_cluster; |
|---|
| 1398 | 1397 | error = fat_calc_dir_size(inode); |
|---|
| 1399 | 1398 | if (error < 0) |
|---|
| .. | .. |
|---|
| 1420 | 1419 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
|---|
| 1421 | 1420 | |
|---|
| 1422 | 1421 | /* Divide first to avoid overflow */ |
|---|
| 1423 | | - if (sbi->fat_bits != 12) { |
|---|
| 1422 | + if (!is_fat12(sbi)) { |
|---|
| 1424 | 1423 | unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits; |
|---|
| 1425 | 1424 | return ent_per_sec * sbi->fat_length; |
|---|
| 1426 | 1425 | } |
|---|
| .. | .. |
|---|
| 1613 | 1612 | int debug; |
|---|
| 1614 | 1613 | long error; |
|---|
| 1615 | 1614 | char buf[50]; |
|---|
| 1615 | + struct timespec64 ts; |
|---|
| 1616 | 1616 | |
|---|
| 1617 | 1617 | /* |
|---|
| 1618 | 1618 | * GFP_KERNEL is ok here, because while we do hold the |
|---|
| .. | .. |
|---|
| 1629 | 1629 | sb->s_magic = MSDOS_SUPER_MAGIC; |
|---|
| 1630 | 1630 | sb->s_op = &fat_sops; |
|---|
| 1631 | 1631 | sb->s_export_op = &fat_export_ops; |
|---|
| 1632 | + /* |
|---|
| 1633 | + * fat timestamps are complex and truncated by fat itself, so |
|---|
| 1634 | + * we set 1 here to be fast |
|---|
| 1635 | + */ |
|---|
| 1636 | + sb->s_time_gran = 1; |
|---|
| 1632 | 1637 | mutex_init(&sbi->nfs_build_inode_lock); |
|---|
| 1633 | 1638 | ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
|---|
| 1634 | 1639 | DEFAULT_RATELIMIT_BURST); |
|---|
| .. | .. |
|---|
| 1701 | 1706 | sbi->free_clus_valid = 0; |
|---|
| 1702 | 1707 | sbi->prev_free = FAT_START_ENT; |
|---|
| 1703 | 1708 | sb->s_maxbytes = 0xffffffff; |
|---|
| 1709 | + fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0); |
|---|
| 1710 | + sb->s_time_min = ts.tv_sec; |
|---|
| 1711 | + |
|---|
| 1712 | + fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX), |
|---|
| 1713 | + cpu_to_le16(FAT_DATE_MAX), 0); |
|---|
| 1714 | + sb->s_time_max = ts.tv_sec; |
|---|
| 1704 | 1715 | |
|---|
| 1705 | 1716 | if (!sbi->fat_length && bpb.fat32_length) { |
|---|
| 1706 | 1717 | struct fat_boot_fsinfo *fsinfo; |
|---|
| .. | .. |
|---|
| 1741 | 1752 | } |
|---|
| 1742 | 1753 | |
|---|
| 1743 | 1754 | /* interpret volume ID as a little endian 32 bit integer */ |
|---|
| 1744 | | - if (sbi->fat_bits == 32) |
|---|
| 1755 | + if (is_fat32(sbi)) |
|---|
| 1745 | 1756 | sbi->vol_id = bpb.fat32_vol_id; |
|---|
| 1746 | 1757 | else /* fat 16 or 12 */ |
|---|
| 1747 | 1758 | sbi->vol_id = bpb.fat16_vol_id; |
|---|
| .. | .. |
|---|
| 1767 | 1778 | |
|---|
| 1768 | 1779 | total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; |
|---|
| 1769 | 1780 | |
|---|
| 1770 | | - if (sbi->fat_bits != 32) |
|---|
| 1781 | + if (!is_fat32(sbi)) |
|---|
| 1771 | 1782 | sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; |
|---|
| 1772 | 1783 | |
|---|
| 1773 | 1784 | /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */ |
|---|
| 1774 | | - if (sbi->fat_bits == 32) |
|---|
| 1785 | + if (is_fat32(sbi)) |
|---|
| 1775 | 1786 | sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY; |
|---|
| 1776 | 1787 | else /* fat 16 or 12 */ |
|---|
| 1777 | 1788 | sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY; |
|---|
| .. | .. |
|---|
| 1779 | 1790 | /* check that FAT table does not overflow */ |
|---|
| 1780 | 1791 | fat_clusters = calc_fat_clusters(sb); |
|---|
| 1781 | 1792 | total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); |
|---|
| 1782 | | - if (total_clusters > MAX_FAT(sb)) { |
|---|
| 1793 | + if (total_clusters > max_fat(sb)) { |
|---|
| 1783 | 1794 | if (!silent) |
|---|
| 1784 | 1795 | fat_msg(sb, KERN_ERR, "count of clusters too big (%u)", |
|---|
| 1785 | 1796 | total_clusters); |
|---|
| .. | .. |
|---|
| 1801 | 1812 | fat_ent_access_init(sb); |
|---|
| 1802 | 1813 | |
|---|
| 1803 | 1814 | /* |
|---|
| 1804 | | - * The low byte of FAT's first entry must have same value with |
|---|
| 1805 | | - * media-field. But in real world, too many devices is |
|---|
| 1806 | | - * writing wrong value. So, removed that validity check. |
|---|
| 1815 | + * The low byte of the first FAT entry must have the same value as |
|---|
| 1816 | + * the media field of the boot sector. But in real world, too many |
|---|
| 1817 | + * devices are writing wrong values. So, removed that validity check. |
|---|
| 1807 | 1818 | * |
|---|
| 1808 | | - * if (FAT_FIRST_ENT(sb, media) != first) |
|---|
| 1819 | + * The removed check compared the first FAT entry to a value dependent |
|---|
| 1820 | + * on the media field like this: |
|---|
| 1821 | + * == (0x0F00 | media), for FAT12 |
|---|
| 1822 | + * == (0XFF00 | media), for FAT16 |
|---|
| 1823 | + * == (0x0FFFFF | media), for FAT32 |
|---|
| 1809 | 1824 | */ |
|---|
| 1810 | 1825 | |
|---|
| 1811 | 1826 | error = -EINVAL; |
|---|
| .. | .. |
|---|
| 1964 | 1979 | module_exit(exit_fat_fs) |
|---|
| 1965 | 1980 | |
|---|
| 1966 | 1981 | MODULE_LICENSE("GPL"); |
|---|
| 1982 | +MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY); |
|---|