Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Written 1992,1993 by Werner Almesberger |
| 4 | * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980 |
| 5 | * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru) |
| 6 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/time.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/buffer_head.h> |
| 13 | |
| 14 | #include "exfat_raw.h" |
| 15 | #include "exfat_fs.h" |
| 16 | |
| 17 | /* |
| 18 | * exfat_fs_error reports a file system problem that might indicate fa data |
| 19 | * corruption/inconsistency. Depending on 'errors' mount option the |
| 20 | * panic() is called, or error message is printed FAT and nothing is done, |
| 21 | * or filesystem is remounted read-only (default behavior). |
| 22 | * In case the file system is remounted read-only, it can be made writable |
| 23 | * again by remounting it. |
| 24 | */ |
| 25 | void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) |
| 26 | { |
| 27 | struct exfat_mount_options *opts = &EXFAT_SB(sb)->options; |
| 28 | va_list args; |
| 29 | struct va_format vaf; |
| 30 | |
| 31 | if (report) { |
| 32 | va_start(args, fmt); |
| 33 | vaf.fmt = fmt; |
| 34 | vaf.va = &args; |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 35 | exfat_err(sb, "error, %pV", &vaf); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 36 | va_end(args); |
| 37 | } |
| 38 | |
| 39 | if (opts->errors == EXFAT_ERRORS_PANIC) { |
| 40 | panic("exFAT-fs (%s): fs panic from previous error\n", |
| 41 | sb->s_id); |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 42 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 43 | } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) { |
| 44 | sb->s_flags |= SB_RDONLY; |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 45 | #else |
| 46 | } else if (opts->errors == EXFAT_ERRORS_RO && |
| 47 | !(sb->s_flags & MS_RDONLY)) { |
| 48 | sb->s_flags |= MS_RDONLY; |
| 49 | #endif |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 50 | exfat_err(sb, "Filesystem has been set read-only"); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * exfat_msg() - print preformated EXFAT specific messages. |
| 56 | * All logs except what uses exfat_fs_error() should be written by exfat_msg() |
| 57 | */ |
| 58 | void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...) |
| 59 | { |
| 60 | struct va_format vaf; |
| 61 | va_list args; |
| 62 | |
| 63 | va_start(args, fmt); |
| 64 | vaf.fmt = fmt; |
| 65 | vaf.va = &args; |
| 66 | /* level means KERN_ pacility level */ |
| 67 | printk("%sexFAT-fs (%s): %pV\n", level, sb->s_id, &vaf); |
| 68 | va_end(args); |
| 69 | } |
| 70 | |
| 71 | #define SECS_PER_MIN (60) |
| 72 | #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) |
| 73 | |
| 74 | static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) |
| 75 | { |
| 76 | if (tz_off <= 0x3F) |
| 77 | ts->tv_sec -= TIMEZONE_SEC(tz_off); |
| 78 | else /* 0x40 <= (tz_off & 0x7F) <=0x7F */ |
| 79 | ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off); |
| 80 | } |
| 81 | |
| 82 | /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */ |
| 83 | void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, |
Tetsuhiro Kohada | 9772742 | 2020-05-03 23:10:31 +0900 | [diff] [blame] | 84 | u8 tz, __le16 time, __le16 date, u8 time_cs) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 85 | { |
| 86 | u16 t = le16_to_cpu(time); |
| 87 | u16 d = le16_to_cpu(date); |
| 88 | |
| 89 | ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F, |
| 90 | t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1); |
| 91 | |
| 92 | |
Tetsuhiro Kohada | 9772742 | 2020-05-03 23:10:31 +0900 | [diff] [blame] | 93 | /* time_cs field represent 0 ~ 199cs(1990 ms) */ |
| 94 | if (time_cs) { |
| 95 | ts->tv_sec += time_cs / 100; |
| 96 | ts->tv_nsec = (time_cs % 100) * 10 * NSEC_PER_MSEC; |
Eric Sandeen | 09241c1 | 2020-04-17 15:47:40 +0900 | [diff] [blame] | 97 | } else |
| 98 | ts->tv_nsec = 0; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 99 | |
| 100 | if (tz & EXFAT_TZ_VALID) |
| 101 | /* Adjust timezone to UTC0. */ |
| 102 | exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID); |
| 103 | else |
| 104 | /* Convert from local time to UTC using time_offset. */ |
| 105 | ts->tv_sec -= sbi->options.time_offset * SECS_PER_MIN; |
| 106 | } |
| 107 | |
| 108 | /* Convert linear UNIX date to a EXFAT time/date pair. */ |
| 109 | void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, |
Tetsuhiro Kohada | 9772742 | 2020-05-03 23:10:31 +0900 | [diff] [blame] | 110 | u8 *tz, __le16 *time, __le16 *date, u8 *time_cs) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 111 | { |
| 112 | struct tm tm; |
| 113 | u16 t, d; |
| 114 | |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 115 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 116 | time64_to_tm(ts->tv_sec, 0, &tm); |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 117 | #else |
| 118 | time_to_tm(ts->tv_sec, 0, &tm); |
| 119 | #endif |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 120 | t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1); |
| 121 | d = ((tm.tm_year - 80) << 9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday; |
| 122 | |
| 123 | *time = cpu_to_le16(t); |
| 124 | *date = cpu_to_le16(d); |
| 125 | |
Tetsuhiro Kohada | 9772742 | 2020-05-03 23:10:31 +0900 | [diff] [blame] | 126 | /* time_cs field represent 0 ~ 199cs(1990 ms) */ |
| 127 | if (time_cs) |
| 128 | *time_cs = (tm.tm_sec & 1) * 100 + |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 129 | ts->tv_nsec / (10 * NSEC_PER_MSEC); |
| 130 | |
| 131 | /* |
| 132 | * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid |
| 133 | * to indicate that local time and UTC are the same. |
| 134 | */ |
| 135 | *tz = EXFAT_TZ_VALID; |
| 136 | } |
| 137 | |
Eric Sandeen | 09241c1 | 2020-04-17 15:47:40 +0900 | [diff] [blame] | 138 | /* atime has only a 2-second resolution */ |
| 139 | void exfat_truncate_atime(struct timespec64 *ts) |
| 140 | { |
| 141 | ts->tv_sec = round_down(ts->tv_sec, 2); |
| 142 | ts->tv_nsec = 0; |
| 143 | } |
| 144 | |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 145 | u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 146 | { |
| 147 | int i; |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 148 | u8 *c = (u8 *)data; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 149 | |
| 150 | for (i = 0; i < len; i++, c++) { |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 151 | if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3))) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 152 | continue; |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 153 | chksum = ((chksum << 15) | (chksum >> 1)) + *c; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 154 | } |
| 155 | return chksum; |
| 156 | } |
| 157 | |
Tetsuhiro Kohada | af08f9a | 2020-05-31 21:03:19 +0900 | [diff] [blame] | 158 | u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type) |
| 159 | { |
| 160 | int i; |
| 161 | u8 *c = (u8 *)data; |
| 162 | |
| 163 | for (i = 0; i < len; i++, c++) { |
| 164 | if (unlikely(type == CS_BOOT_SECTOR && |
| 165 | (i == 106 || i == 107 || i == 112))) |
| 166 | continue; |
| 167 | chksum = ((chksum << 31) | (chksum >> 1)) + *c; |
| 168 | } |
| 169 | return chksum; |
| 170 | } |
| 171 | |
Tetsuhiro Kohada | 117088f | 2020-06-16 14:25:07 +0900 | [diff] [blame^] | 172 | void exfat_update_bh(struct buffer_head *bh, int sync) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 173 | { |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 174 | set_buffer_uptodate(bh); |
| 175 | mark_buffer_dirty(bh); |
| 176 | |
| 177 | if (sync) |
| 178 | sync_dirty_buffer(bh); |
| 179 | } |
| 180 | |
| 181 | void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, |
| 182 | unsigned int size, unsigned char flags) |
| 183 | { |
| 184 | ec->dir = dir; |
| 185 | ec->size = size; |
| 186 | ec->flags = flags; |
| 187 | } |
| 188 | |
| 189 | void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec) |
| 190 | { |
| 191 | return exfat_chain_set(dup, ec->dir, ec->size, ec->flags); |
| 192 | } |