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> |
Yuezhang.Mo | 1c014cb | 2021-12-21 20:26:03 +0900 | [diff] [blame] | 13 | #include <linux/blk_types.h> |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 14 | |
| 15 | #include "exfat_raw.h" |
| 16 | #include "exfat_fs.h" |
| 17 | |
| 18 | /* |
| 19 | * exfat_fs_error reports a file system problem that might indicate fa data |
| 20 | * corruption/inconsistency. Depending on 'errors' mount option the |
| 21 | * panic() is called, or error message is printed FAT and nothing is done, |
| 22 | * or filesystem is remounted read-only (default behavior). |
| 23 | * In case the file system is remounted read-only, it can be made writable |
| 24 | * again by remounting it. |
| 25 | */ |
| 26 | void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) |
| 27 | { |
| 28 | struct exfat_mount_options *opts = &EXFAT_SB(sb)->options; |
| 29 | va_list args; |
| 30 | struct va_format vaf; |
| 31 | |
| 32 | if (report) { |
| 33 | va_start(args, fmt); |
| 34 | vaf.fmt = fmt; |
| 35 | vaf.va = &args; |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 36 | exfat_err(sb, "error, %pV", &vaf); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 37 | va_end(args); |
| 38 | } |
| 39 | |
| 40 | if (opts->errors == EXFAT_ERRORS_PANIC) { |
| 41 | panic("exFAT-fs (%s): fs panic from previous error\n", |
| 42 | sb->s_id); |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 43 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 44 | } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) { |
| 45 | sb->s_flags |= SB_RDONLY; |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 46 | #else |
| 47 | } else if (opts->errors == EXFAT_ERRORS_RO && |
| 48 | !(sb->s_flags & MS_RDONLY)) { |
| 49 | sb->s_flags |= MS_RDONLY; |
| 50 | #endif |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 51 | exfat_err(sb, "Filesystem has been set read-only"); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 55 | #define SECS_PER_MIN (60) |
| 56 | #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) |
| 57 | |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 58 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 59 | static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 60 | #else |
| 61 | static void exfat_adjust_tz(struct timespec *ts, u8 tz_off) |
| 62 | #endif |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 63 | { |
| 64 | if (tz_off <= 0x3F) |
| 65 | ts->tv_sec -= TIMEZONE_SEC(tz_off); |
| 66 | else /* 0x40 <= (tz_off & 0x7F) <=0x7F */ |
| 67 | ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off); |
| 68 | } |
| 69 | |
Chung-Chiang Cheng | 8dbffe2 | 2022-04-08 22:00:57 +0900 | [diff] [blame] | 70 | static inline int exfat_tz_offset(struct exfat_sb_info *sbi) |
| 71 | { |
| 72 | if (sbi->options.sys_tz) |
| 73 | return -sys_tz.tz_minuteswest; |
| 74 | return sbi->options.time_offset; |
| 75 | } |
| 76 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 77 | /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */ |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 78 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 79 | 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] | 80 | u8 tz, __le16 time, __le16 date, u8 time_cs) |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 81 | #else |
| 82 | void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, |
| 83 | u8 tz, __le16 time, __le16 date, u8 time_cs) |
| 84 | #endif |
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 |
Chung-Chiang Cheng | 8dbffe2 | 2022-04-08 22:00:57 +0900 | [diff] [blame] | 104 | ts->tv_sec -= exfat_tz_offset(sbi) * SECS_PER_MIN; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Convert linear UNIX date to a EXFAT time/date pair. */ |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 108 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 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 | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 111 | #else |
Matthieu CASTET | 2314f3c | 2021-06-07 18:45:07 +0200 | [diff] [blame] | 112 | #undef EXFAT_MAX_TIMESTAMP_SECS |
| 113 | #define EXFAT_MAX_TIMESTAMP_SECS 0xffffffff |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 114 | void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, |
| 115 | u8 *tz, __le16 *time, __le16 *date, u8 *time_cs) |
| 116 | #endif |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 117 | { |
| 118 | struct tm tm; |
| 119 | u16 t, d; |
| 120 | |
Matthieu CASTET | 10e5987 | 2021-03-16 18:48:07 +0100 | [diff] [blame] | 121 | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) |
| 122 | if (ts->tv_sec < EXFAT_MIN_TIMESTAMP_SECS) { |
| 123 | ts->tv_sec = EXFAT_MIN_TIMESTAMP_SECS; |
| 124 | ts->tv_nsec = 0; |
| 125 | } |
| 126 | else if (ts->tv_sec > EXFAT_MAX_TIMESTAMP_SECS) { |
| 127 | ts->tv_sec = EXFAT_MAX_TIMESTAMP_SECS; |
| 128 | ts->tv_nsec = 0; |
| 129 | } |
| 130 | #endif |
| 131 | |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 132 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 133 | time64_to_tm(ts->tv_sec, 0, &tm); |
Namjae Jeon | 4e3295f | 2020-02-08 19:13:34 +0900 | [diff] [blame] | 134 | #else |
| 135 | time_to_tm(ts->tv_sec, 0, &tm); |
| 136 | #endif |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 137 | t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1); |
| 138 | d = ((tm.tm_year - 80) << 9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday; |
| 139 | |
| 140 | *time = cpu_to_le16(t); |
| 141 | *date = cpu_to_le16(d); |
| 142 | |
Tetsuhiro Kohada | 9772742 | 2020-05-03 23:10:31 +0900 | [diff] [blame] | 143 | /* time_cs field represent 0 ~ 199cs(1990 ms) */ |
| 144 | if (time_cs) |
| 145 | *time_cs = (tm.tm_sec & 1) * 100 + |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 146 | ts->tv_nsec / (10 * NSEC_PER_MSEC); |
| 147 | |
| 148 | /* |
| 149 | * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid |
| 150 | * to indicate that local time and UTC are the same. |
| 151 | */ |
| 152 | *tz = EXFAT_TZ_VALID; |
| 153 | } |
| 154 | |
Eric Sandeen | 09241c1 | 2020-04-17 15:47:40 +0900 | [diff] [blame] | 155 | /* atime has only a 2-second resolution */ |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 156 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) |
Eric Sandeen | 09241c1 | 2020-04-17 15:47:40 +0900 | [diff] [blame] | 157 | void exfat_truncate_atime(struct timespec64 *ts) |
Namjae Jeon | 6b09f3c | 2020-07-01 16:43:16 +0900 | [diff] [blame] | 158 | #else |
| 159 | void exfat_truncate_atime(struct timespec *ts) |
| 160 | #endif |
Eric Sandeen | 09241c1 | 2020-04-17 15:47:40 +0900 | [diff] [blame] | 161 | { |
| 162 | ts->tv_sec = round_down(ts->tv_sec, 2); |
| 163 | ts->tv_nsec = 0; |
| 164 | } |
| 165 | |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 166 | u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 167 | { |
| 168 | int i; |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 169 | u8 *c = (u8 *)data; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 170 | |
| 171 | for (i = 0; i < len; i++, c++) { |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 172 | if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3))) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 173 | continue; |
Tetsuhiro Kohada | 149ef04 | 2020-05-31 21:04:00 +0900 | [diff] [blame] | 174 | chksum = ((chksum << 15) | (chksum >> 1)) + *c; |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 175 | } |
| 176 | return chksum; |
| 177 | } |
| 178 | |
Tetsuhiro Kohada | af08f9a | 2020-05-31 21:03:19 +0900 | [diff] [blame] | 179 | u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type) |
| 180 | { |
| 181 | int i; |
| 182 | u8 *c = (u8 *)data; |
| 183 | |
| 184 | for (i = 0; i < len; i++, c++) { |
| 185 | if (unlikely(type == CS_BOOT_SECTOR && |
| 186 | (i == 106 || i == 107 || i == 112))) |
| 187 | continue; |
| 188 | chksum = ((chksum << 31) | (chksum >> 1)) + *c; |
| 189 | } |
| 190 | return chksum; |
| 191 | } |
| 192 | |
Tetsuhiro Kohada | 117088f | 2020-06-16 14:25:07 +0900 | [diff] [blame] | 193 | void exfat_update_bh(struct buffer_head *bh, int sync) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 194 | { |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 195 | set_buffer_uptodate(bh); |
| 196 | mark_buffer_dirty(bh); |
| 197 | |
| 198 | if (sync) |
| 199 | sync_dirty_buffer(bh); |
| 200 | } |
| 201 | |
Tetsuhiro Kohada | 773256e | 2020-06-24 08:32:56 +0900 | [diff] [blame] | 202 | int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync) |
| 203 | { |
| 204 | int i, err = 0; |
| 205 | |
| 206 | for (i = 0; i < nr_bhs; i++) { |
| 207 | set_buffer_uptodate(bhs[i]); |
| 208 | mark_buffer_dirty(bhs[i]); |
| 209 | if (sync) |
Namjae Jeon | fa4f895 | 2022-05-14 08:25:19 +0900 | [diff] [blame] | 210 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0) |
Yuezhang.Mo | 1c014cb | 2021-12-21 20:26:03 +0900 | [diff] [blame] | 211 | write_dirty_buffer(bhs[i], REQ_SYNC); |
Namjae Jeon | fa4f895 | 2022-05-14 08:25:19 +0900 | [diff] [blame] | 212 | #else |
| 213 | write_dirty_buffer(bhs[i], WRITE_SYNC); |
| 214 | #endif |
Tetsuhiro Kohada | 773256e | 2020-06-24 08:32:56 +0900 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | for (i = 0; i < nr_bhs && sync; i++) { |
| 218 | wait_on_buffer(bhs[i]); |
| 219 | if (!err && !buffer_uptodate(bhs[i])) |
| 220 | err = -EIO; |
| 221 | } |
| 222 | return err; |
| 223 | } |
| 224 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 225 | void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, |
| 226 | unsigned int size, unsigned char flags) |
| 227 | { |
| 228 | ec->dir = dir; |
| 229 | ec->size = size; |
| 230 | ec->flags = flags; |
| 231 | } |
| 232 | |
| 233 | void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec) |
| 234 | { |
| 235 | return exfat_chain_set(dup, ec->dir, ec->size, ec->flags); |
| 236 | } |