blob: e7ae6f629d6eb716c5ac8de0dee608bf3ceb4a40 [file] [log] [blame]
Namjae Jeon9273f3d2020-02-03 21:47:19 +09001// 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.Mo1c014cb2021-12-21 20:26:03 +090013#include <linux/blk_types.h>
Namjae Jeon9273f3d2020-02-03 21:47:19 +090014
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 */
26void __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 Perchese08ed902020-04-06 12:49:36 +090036 exfat_err(sb, "error, %pV", &vaf);
Namjae Jeon9273f3d2020-02-03 21:47:19 +090037 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 Jeon4e3295f2020-02-08 19:13:34 +090043#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
Namjae Jeon9273f3d2020-02-03 21:47:19 +090044 } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) {
45 sb->s_flags |= SB_RDONLY;
Namjae Jeon4e3295f2020-02-08 19:13:34 +090046#else
47 } else if (opts->errors == EXFAT_ERRORS_RO &&
48 !(sb->s_flags & MS_RDONLY)) {
49 sb->s_flags |= MS_RDONLY;
50#endif
Joe Perchese08ed902020-04-06 12:49:36 +090051 exfat_err(sb, "Filesystem has been set read-only");
Namjae Jeon9273f3d2020-02-03 21:47:19 +090052 }
53}
54
Namjae Jeon9273f3d2020-02-03 21:47:19 +090055#define SECS_PER_MIN (60)
56#define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN)
57
Namjae Jeon6b09f3c2020-07-01 16:43:16 +090058#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
Namjae Jeon9273f3d2020-02-03 21:47:19 +090059static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off)
Namjae Jeon6b09f3c2020-07-01 16:43:16 +090060#else
61static void exfat_adjust_tz(struct timespec *ts, u8 tz_off)
62#endif
Namjae Jeon9273f3d2020-02-03 21:47:19 +090063{
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 Cheng8dbffe22022-04-08 22:00:57 +090070static 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 Jeon9273f3d2020-02-03 21:47:19 +090077/* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */
Namjae Jeon6b09f3c2020-07-01 16:43:16 +090078#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
Namjae Jeon9273f3d2020-02-03 21:47:19 +090079void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
Tetsuhiro Kohada97727422020-05-03 23:10:31 +090080 u8 tz, __le16 time, __le16 date, u8 time_cs)
Namjae Jeon6b09f3c2020-07-01 16:43:16 +090081#else
82void 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 Jeon9273f3d2020-02-03 21:47:19 +090085{
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 Kohada97727422020-05-03 23:10:31 +090093 /* 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 Sandeen09241c12020-04-17 15:47:40 +090097 } else
98 ts->tv_nsec = 0;
Namjae Jeon9273f3d2020-02-03 21:47:19 +090099
100 if (tz & EXFAT_TZ_VALID)
101 /* Adjust timezone to UTC0. */
102 exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID);
103 else
Chung-Chiang Cheng8dbffe22022-04-08 22:00:57 +0900104 ts->tv_sec -= exfat_tz_offset(sbi) * SECS_PER_MIN;
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900105}
106
107/* Convert linear UNIX date to a EXFAT time/date pair. */
Namjae Jeon6b09f3c2020-07-01 16:43:16 +0900108#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900109void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
Tetsuhiro Kohada97727422020-05-03 23:10:31 +0900110 u8 *tz, __le16 *time, __le16 *date, u8 *time_cs)
Namjae Jeon6b09f3c2020-07-01 16:43:16 +0900111#else
Matthieu CASTET2314f3c2021-06-07 18:45:07 +0200112#undef EXFAT_MAX_TIMESTAMP_SECS
113#define EXFAT_MAX_TIMESTAMP_SECS 0xffffffff
Namjae Jeon6b09f3c2020-07-01 16:43:16 +0900114void 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 Jeon9273f3d2020-02-03 21:47:19 +0900117{
118 struct tm tm;
119 u16 t, d;
120
Matthieu CASTET10e59872021-03-16 18:48:07 +0100121#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 Jeon4e3295f2020-02-08 19:13:34 +0900132#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900133 time64_to_tm(ts->tv_sec, 0, &tm);
Namjae Jeon4e3295f2020-02-08 19:13:34 +0900134#else
135 time_to_tm(ts->tv_sec, 0, &tm);
136#endif
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900137 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 Kohada97727422020-05-03 23:10:31 +0900143 /* time_cs field represent 0 ~ 199cs(1990 ms) */
144 if (time_cs)
145 *time_cs = (tm.tm_sec & 1) * 100 +
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900146 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 Sandeen09241c12020-04-17 15:47:40 +0900155/* atime has only a 2-second resolution */
Namjae Jeon6b09f3c2020-07-01 16:43:16 +0900156#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
Eric Sandeen09241c12020-04-17 15:47:40 +0900157void exfat_truncate_atime(struct timespec64 *ts)
Namjae Jeon6b09f3c2020-07-01 16:43:16 +0900158#else
159void exfat_truncate_atime(struct timespec *ts)
160#endif
Eric Sandeen09241c12020-04-17 15:47:40 +0900161{
162 ts->tv_sec = round_down(ts->tv_sec, 2);
163 ts->tv_nsec = 0;
164}
165
Tetsuhiro Kohada149ef042020-05-31 21:04:00 +0900166u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900167{
168 int i;
Tetsuhiro Kohada149ef042020-05-31 21:04:00 +0900169 u8 *c = (u8 *)data;
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900170
171 for (i = 0; i < len; i++, c++) {
Tetsuhiro Kohada149ef042020-05-31 21:04:00 +0900172 if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900173 continue;
Tetsuhiro Kohada149ef042020-05-31 21:04:00 +0900174 chksum = ((chksum << 15) | (chksum >> 1)) + *c;
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900175 }
176 return chksum;
177}
178
Tetsuhiro Kohadaaf08f9a2020-05-31 21:03:19 +0900179u32 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 Kohada117088f2020-06-16 14:25:07 +0900193void exfat_update_bh(struct buffer_head *bh, int sync)
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900194{
Namjae Jeon9273f3d2020-02-03 21:47:19 +0900195 set_buffer_uptodate(bh);
196 mark_buffer_dirty(bh);
197
198 if (sync)
199 sync_dirty_buffer(bh);
200}
201
Tetsuhiro Kohada773256e2020-06-24 08:32:56 +0900202int 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 Jeonfa4f8952022-05-14 08:25:19 +0900210#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
Yuezhang.Mo1c014cb2021-12-21 20:26:03 +0900211 write_dirty_buffer(bhs[i], REQ_SYNC);
Namjae Jeonfa4f8952022-05-14 08:25:19 +0900212#else
213 write_dirty_buffer(bhs[i], WRITE_SYNC);
214#endif
Tetsuhiro Kohada773256e2020-06-24 08:32:56 +0900215 }
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 Jeon9273f3d2020-02-03 21:47:19 +0900225void 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
233void 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}