123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- #include "fat.h"
- #include <linux/iversion.h>
- void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
- {
- struct fat_mount_options *opts = &MSDOS_SB(sb)->options;
- va_list args;
- struct va_format vaf;
- if (report) {
- va_start(args, fmt);
- vaf.fmt = fmt;
- vaf.va = &args;
- fat_msg(sb, KERN_ERR, "error, %pV", &vaf);
- va_end(args);
- }
- if (opts->errors == FAT_ERRORS_PANIC)
- panic("FAT-fs (%s): fs panic from previous error\n", sb->s_id);
- else if (opts->errors == FAT_ERRORS_RO && !sb_rdonly(sb)) {
- sb->s_flags |= SB_RDONLY;
- fat_msg(sb, KERN_ERR, "Filesystem has been set read-only");
- }
- }
- EXPORT_SYMBOL_GPL(__fat_fs_error);
- void _fat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
- {
- struct va_format vaf;
- va_list args;
- va_start(args, fmt);
- vaf.fmt = fmt;
- vaf.va = &args;
- _printk(FAT_PRINTK_PREFIX "%pV\n", level, sb->s_id, &vaf);
- va_end(args);
- }
- int fat_clusters_flush(struct super_block *sb)
- {
- struct msdos_sb_info *sbi = MSDOS_SB(sb);
- struct buffer_head *bh;
- struct fat_boot_fsinfo *fsinfo;
- if (!is_fat32(sbi))
- return 0;
- bh = sb_bread(sb, sbi->fsinfo_sector);
- if (bh == NULL) {
- fat_msg(sb, KERN_ERR, "bread failed in fat_clusters_flush");
- return -EIO;
- }
- fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
-
- if (!IS_FSINFO(fsinfo)) {
- fat_msg(sb, KERN_ERR, "Invalid FSINFO signature: "
- "0x%08x, 0x%08x (sector = %lu)",
- le32_to_cpu(fsinfo->signature1),
- le32_to_cpu(fsinfo->signature2),
- sbi->fsinfo_sector);
- } else {
- if (sbi->free_clusters != -1)
- fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
- if (sbi->prev_free != -1)
- fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
- mark_buffer_dirty(bh);
- }
- brelse(bh);
- return 0;
- }
- int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
- {
- struct super_block *sb = inode->i_sb;
- struct msdos_sb_info *sbi = MSDOS_SB(sb);
- int ret, new_fclus, last;
-
- last = new_fclus = 0;
- if (MSDOS_I(inode)->i_start) {
- int fclus, dclus;
- ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
- if (ret < 0)
- return ret;
- new_fclus = fclus + 1;
- last = dclus;
- }
-
- if (last) {
- struct fat_entry fatent;
- fatent_init(&fatent);
- ret = fat_ent_read(inode, &fatent, last);
- if (ret >= 0) {
- int wait = inode_needs_sync(inode);
- ret = fat_ent_write(inode, &fatent, new_dclus, wait);
- fatent_brelse(&fatent);
- }
- if (ret < 0)
- return ret;
-
- } else {
- MSDOS_I(inode)->i_start = new_dclus;
- MSDOS_I(inode)->i_logstart = new_dclus;
-
- if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
- ret = fat_sync_inode(inode);
- if (ret)
- return ret;
- } else
- mark_inode_dirty(inode);
- }
- if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
- fat_fs_error(sb, "clusters badly computed (%d != %llu)",
- new_fclus,
- (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
- fat_cache_inval_inode(inode);
- }
- inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
- return 0;
- }
- #define SECS_PER_MIN 60
- #define SECS_PER_HOUR (60 * 60)
- #define SECS_PER_DAY (SECS_PER_HOUR * 24)
- #define DAYS_DELTA (365 * 10 + 2)
- #define YEAR_2100 120
- #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
- static long days_in_year[] = {
-
- 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
- };
- static inline int fat_tz_offset(const struct msdos_sb_info *sbi)
- {
- return (sbi->options.tz_set ?
- -sbi->options.time_offset :
- sys_tz.tz_minuteswest) * SECS_PER_MIN;
- }
- void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
- __le16 __time, __le16 __date, u8 time_cs)
- {
- u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
- time64_t second;
- long day, leap_day, month, year;
- year = date >> 9;
- month = max(1, (date >> 5) & 0xf);
- day = max(1, date & 0x1f) - 1;
- leap_day = (year + 3) / 4;
- if (year > YEAR_2100)
- leap_day--;
- if (IS_LEAP_YEAR(year) && month > 2)
- leap_day++;
- second = (time & 0x1f) << 1;
- second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
- second += (time >> 11) * SECS_PER_HOUR;
- second += (time64_t)(year * 365 + leap_day
- + days_in_year[month] + day
- + DAYS_DELTA) * SECS_PER_DAY;
- second += fat_tz_offset(sbi);
- if (time_cs) {
- ts->tv_sec = second + (time_cs / 100);
- ts->tv_nsec = (time_cs % 100) * 10000000;
- } else {
- ts->tv_sec = second;
- ts->tv_nsec = 0;
- }
- }
- EXPORT_SYMBOL_GPL(fat_time_fat2unix);
- void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
- __le16 *time, __le16 *date, u8 *time_cs)
- {
- struct tm tm;
- time64_to_tm(ts->tv_sec, -fat_tz_offset(sbi), &tm);
-
- if (tm.tm_year < 1980 - 1900) {
- *time = 0;
- *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
- if (time_cs)
- *time_cs = 0;
- return;
- }
- if (tm.tm_year > 2107 - 1900) {
- *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
- *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
- if (time_cs)
- *time_cs = 199;
- return;
- }
-
- tm.tm_year -= 80;
-
- tm.tm_mon++;
-
- tm.tm_sec >>= 1;
- *time = cpu_to_le16(tm.tm_hour << 11 | tm.tm_min << 5 | tm.tm_sec);
- *date = cpu_to_le16(tm.tm_year << 9 | tm.tm_mon << 5 | tm.tm_mday);
- if (time_cs)
- *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
- }
- EXPORT_SYMBOL_GPL(fat_time_unix2fat);
- static inline struct timespec64 fat_timespec64_trunc_2secs(struct timespec64 ts)
- {
- return (struct timespec64){ ts.tv_sec & ~1ULL, 0 };
- }
- struct timespec64 fat_truncate_atime(const struct msdos_sb_info *sbi,
- const struct timespec64 *ts)
- {
-
- time64_t seconds = ts->tv_sec - fat_tz_offset(sbi);
- s32 remainder;
- div_s64_rem(seconds, SECS_PER_DAY, &remainder);
-
- seconds = seconds + fat_tz_offset(sbi) - remainder;
- return (struct timespec64){ seconds, 0 };
- }
- struct timespec64 fat_truncate_mtime(const struct msdos_sb_info *sbi,
- const struct timespec64 *ts)
- {
- return fat_timespec64_trunc_2secs(*ts);
- }
- int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags)
- {
- struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
- struct timespec64 ts;
- if (inode->i_ino == MSDOS_ROOT_INO)
- return 0;
- if (now == NULL) {
- now = &ts;
- ts = current_time(inode);
- }
- if (flags & S_ATIME)
- inode->i_atime = fat_truncate_atime(sbi, now);
-
- if (flags & S_MTIME)
- inode->i_mtime = inode->i_ctime = fat_truncate_mtime(sbi, now);
- return 0;
- }
- EXPORT_SYMBOL_GPL(fat_truncate_time);
- int fat_update_time(struct inode *inode, struct timespec64 *now, int flags)
- {
- int dirty_flags = 0;
- if (inode->i_ino == MSDOS_ROOT_INO)
- return 0;
- if (flags & (S_ATIME | S_CTIME | S_MTIME)) {
- fat_truncate_time(inode, now, flags);
- if (inode->i_sb->s_flags & SB_LAZYTIME)
- dirty_flags |= I_DIRTY_TIME;
- else
- dirty_flags |= I_DIRTY_SYNC;
- }
- if ((flags & S_VERSION) && inode_maybe_inc_iversion(inode, false))
- dirty_flags |= I_DIRTY_SYNC;
- __mark_inode_dirty(inode, dirty_flags);
- return 0;
- }
- EXPORT_SYMBOL_GPL(fat_update_time);
- int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
- {
- int i, err = 0;
- for (i = 0; i < nr_bhs; i++)
- write_dirty_buffer(bhs[i], 0);
- for (i = 0; i < nr_bhs; i++) {
- wait_on_buffer(bhs[i]);
- if (!err && !buffer_uptodate(bhs[i]))
- err = -EIO;
- }
- return err;
- }
|