hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/fat/inode.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/fat/inode.c
34 *
....@@ -20,6 +21,7 @@
2021 #include <linux/blkdev.h>
2122 #include <linux/backing-dev.h>
2223 #include <asm/unaligned.h>
24
+#include <linux/random.h>
2325 #include <linux/iversion.h>
2426 #include "fat.h"
2527
....@@ -29,6 +31,11 @@
2931 #endif
3032
3133 #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)
3239
3340 /*
3441 * A deserialized copy of the on-disk structure laid out in struct
....@@ -203,10 +210,9 @@
203210 return mpage_readpage(page, fat_get_block);
204211 }
205212
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)
208214 {
209
- return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
215
+ mpage_readahead(rac, fat_get_block);
210216 }
211217
212218 static void fat_write_failed(struct address_space *mapping, loff_t to)
....@@ -244,7 +250,7 @@
244250 if (err < len)
245251 fat_write_failed(mapping, pos + len);
246252 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);
248254 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
249255 mark_inode_dirty(inode);
250256 }
....@@ -337,7 +343,7 @@
337343
338344 static const struct address_space_operations fat_aops = {
339345 .readpage = fat_readpage,
340
- .readpages = fat_readpages,
346
+ .readahead = fat_readahead,
341347 .writepage = fat_writepage,
342348 .writepages = fat_writepages,
343349 .write_begin = fat_write_begin,
....@@ -515,7 +521,7 @@
515521 inode->i_uid = sbi->options.fs_uid;
516522 inode->i_gid = sbi->options.fs_gid;
517523 inode_inc_iversion(inode);
518
- inode->i_generation = get_seconds();
524
+ inode->i_generation = prandom_u32();
519525
520526 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
521527 inode->i_generation &= ~1;
....@@ -564,7 +570,7 @@
564570 de->cdate, de->ctime_cs);
565571 fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
566572 } else
567
- inode->i_ctime = inode->i_atime = inode->i_mtime;
573
+ fat_truncate_time(inode, &inode->i_mtime, S_ATIME|S_CTIME);
568574
569575 return 0;
570576 }
....@@ -686,7 +692,7 @@
686692
687693 b = (struct fat_boot_sector *) bh->b_data;
688694
689
- if (sbi->fat_bits == 32) {
695
+ if (is_fat32(sbi)) {
690696 if (set)
691697 b->fat32.state |= FAT_STATE_DIRTY;
692698 else
....@@ -753,15 +759,9 @@
753759 return &ei->vfs_inode;
754760 }
755761
756
-static void fat_i_callback(struct rcu_head *head)
762
+static void fat_free_inode(struct inode *inode)
757763 {
758
- struct inode *inode = container_of(head, struct inode, i_rcu);
759764 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);
765765 }
766766
767767 static void init_once(void *foo)
....@@ -836,8 +836,7 @@
836836 buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
837837 buf->f_bfree = sbi->free_clusters;
838838 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);
841840 buf->f_namelen =
842841 (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
843842
....@@ -927,7 +926,7 @@
927926 static int fat_show_options(struct seq_file *m, struct dentry *root);
928927 static const struct super_operations fat_sops = {
929928 .alloc_inode = fat_alloc_inode,
930
- .destroy_inode = fat_destroy_inode,
929
+ .free_inode = fat_free_inode,
931930 .write_inode = fat_write_inode,
932931 .evict_inode = fat_evict_inode,
933932 .put_super = fat_put_super,
....@@ -1393,7 +1392,7 @@
13931392 inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
13941393 inode->i_op = sbi->dir_ops;
13951394 inode->i_fop = &fat_dir_operations;
1396
- if (sbi->fat_bits == 32) {
1395
+ if (is_fat32(sbi)) {
13971396 MSDOS_I(inode)->i_start = sbi->root_cluster;
13981397 error = fat_calc_dir_size(inode);
13991398 if (error < 0)
....@@ -1420,7 +1419,7 @@
14201419 struct msdos_sb_info *sbi = MSDOS_SB(sb);
14211420
14221421 /* Divide first to avoid overflow */
1423
- if (sbi->fat_bits != 12) {
1422
+ if (!is_fat12(sbi)) {
14241423 unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
14251424 return ent_per_sec * sbi->fat_length;
14261425 }
....@@ -1613,6 +1612,7 @@
16131612 int debug;
16141613 long error;
16151614 char buf[50];
1615
+ struct timespec64 ts;
16161616
16171617 /*
16181618 * GFP_KERNEL is ok here, because while we do hold the
....@@ -1629,6 +1629,11 @@
16291629 sb->s_magic = MSDOS_SUPER_MAGIC;
16301630 sb->s_op = &fat_sops;
16311631 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;
16321637 mutex_init(&sbi->nfs_build_inode_lock);
16331638 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
16341639 DEFAULT_RATELIMIT_BURST);
....@@ -1701,6 +1706,12 @@
17011706 sbi->free_clus_valid = 0;
17021707 sbi->prev_free = FAT_START_ENT;
17031708 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;
17041715
17051716 if (!sbi->fat_length && bpb.fat32_length) {
17061717 struct fat_boot_fsinfo *fsinfo;
....@@ -1741,7 +1752,7 @@
17411752 }
17421753
17431754 /* interpret volume ID as a little endian 32 bit integer */
1744
- if (sbi->fat_bits == 32)
1755
+ if (is_fat32(sbi))
17451756 sbi->vol_id = bpb.fat32_vol_id;
17461757 else /* fat 16 or 12 */
17471758 sbi->vol_id = bpb.fat16_vol_id;
....@@ -1767,11 +1778,11 @@
17671778
17681779 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
17691780
1770
- if (sbi->fat_bits != 32)
1781
+ if (!is_fat32(sbi))
17711782 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
17721783
17731784 /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1774
- if (sbi->fat_bits == 32)
1785
+ if (is_fat32(sbi))
17751786 sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
17761787 else /* fat 16 or 12 */
17771788 sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
....@@ -1779,7 +1790,7 @@
17791790 /* check that FAT table does not overflow */
17801791 fat_clusters = calc_fat_clusters(sb);
17811792 total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1782
- if (total_clusters > MAX_FAT(sb)) {
1793
+ if (total_clusters > max_fat(sb)) {
17831794 if (!silent)
17841795 fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
17851796 total_clusters);
....@@ -1801,11 +1812,15 @@
18011812 fat_ent_access_init(sb);
18021813
18031814 /*
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.
18071818 *
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
18091824 */
18101825
18111826 error = -EINVAL;
....@@ -1964,3 +1979,4 @@
19641979 module_exit(exit_fat_fs)
19651980
19661981 MODULE_LICENSE("GPL");
1982
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);