Message ID | 20200102082036.29643-10-namjae.jeon@samsung.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | add the latest exfat driver | expand |
On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > This adds the implementation of misc operations for exfat. > > Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com> > Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> > --- > fs/exfat/misc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 253 insertions(+) > create mode 100644 fs/exfat/misc.c > > diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c > new file mode 100644 > index 000000000000..7f533bcb3b3f > --- /dev/null > +++ b/fs/exfat/misc.c ... > +/* <linux/time.h> externs sys_tz > + * extern struct timezone sys_tz; > + */ > +#define UNIX_SECS_1980 315532800L > + > +#if BITS_PER_LONG == 64 > +#define UNIX_SECS_2108 4354819200L > +#endif ... > +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) > +/* Convert linear UNIX date to a FAT time/date pair. */ > +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 *ts, > + struct exfat_date_time *tp) > +{ > + time_t second = ts->tv_sec; > + time_t day, month, year; > + time_t ld; /* leap day */ Question for other maintainers: Has kernel code already time_t defined as 64bit? Or it is still just 32bit and 32bit systems and some time64_t needs to be used? I remember that there was discussion about these problems, but do not know if it was changed/fixed or not... Just a pointer for possible Y2038 problem. As "ts" is of type timespec64, but "second" of type time_t. > + > + /* Treats as local time with proper time */ > + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; > + tp->timezone.valid = 1; > + tp->timezone.off = TIMEZONE_CUR_OFFSET(); > + > + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ > + if (second < UNIX_SECS_1980) { > + tp->second = 0; > + tp->minute = 0; > + tp->hour = 0; > + tp->day = 1; > + tp->month = 1; > + tp->year = 0; > + return; > + } > + > + if (second >= UNIX_SECS_2108) { Hello, this code cause compile errors on 32bit systems as UNIX_SECS_2108 macro is not defined when BITS_PER_LONG == 32. Value 4354819200 really cannot fit into 32bit signed integer, so you should use 64bit signed integer. I would suggest to define this macro value via LL not just L suffix (and it would work on both 32 and 64bit) #define UNIX_SECS_2108 4354819200LL > + tp->second = 59; > + tp->minute = 59; > + tp->hour = 23; > + tp->day = 31; > + tp->month = 12; > + tp->year = 127; > + return; > + }
2020-01-02 18:19 GMT+09:00, Pali Rohár <pali.rohar@gmail.com>: > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: >> This adds the implementation of misc operations for exfat. >> >> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com> >> Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> >> --- >> fs/exfat/misc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 253 insertions(+) >> create mode 100644 fs/exfat/misc.c >> >> diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c >> new file mode 100644 >> index 000000000000..7f533bcb3b3f >> --- /dev/null >> +++ b/fs/exfat/misc.c > > ... > >> +/* <linux/time.h> externs sys_tz >> + * extern struct timezone sys_tz; >> + */ >> +#define UNIX_SECS_1980 315532800L >> + >> +#if BITS_PER_LONG == 64 >> +#define UNIX_SECS_2108 4354819200L >> +#endif > > ... > >> +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) >> +/* Convert linear UNIX date to a FAT time/date pair. */ >> +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 >> *ts, >> + struct exfat_date_time *tp) >> +{ >> + time_t second = ts->tv_sec; >> + time_t day, month, year; >> + time_t ld; /* leap day */ > > Question for other maintainers: Has kernel code already time_t defined > as 64bit? Or it is still just 32bit and 32bit systems and some time64_t > needs to be used? I remember that there was discussion about these > problems, but do not know if it was changed/fixed or not... Just a > pointer for possible Y2038 problem. As "ts" is of type timespec64, but > "second" of type time_t. My bad, I will change it with time64_t. > >> + >> + /* Treats as local time with proper time */ >> + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; >> + tp->timezone.valid = 1; >> + tp->timezone.off = TIMEZONE_CUR_OFFSET(); >> + >> + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ >> + if (second < UNIX_SECS_1980) { >> + tp->second = 0; >> + tp->minute = 0; >> + tp->hour = 0; >> + tp->day = 1; >> + tp->month = 1; >> + tp->year = 0; >> + return; >> + } >> + >> + if (second >= UNIX_SECS_2108) { > > Hello, this code cause compile errors on 32bit systems as UNIX_SECS_2108 > macro is not defined when BITS_PER_LONG == 32. > > Value 4354819200 really cannot fit into 32bit signed integer, so you > should use 64bit signed integer. I would suggest to define this macro > value via LL not just L suffix (and it would work on both 32 and 64bit) Okay. > > #define UNIX_SECS_2108 4354819200LL > >> + tp->second = 59; >> + tp->minute = 59; >> + tp->hour = 23; >> + tp->day = 31; >> + tp->month = 12; >> + tp->year = 127; >> + return; >> + } Okay, I will check it. Thanks for your review! > > -- > Pali Rohár > pali.rohar@gmail.com >
On Thursday 02 January 2020 20:30:03 Namjae Jeon wrote: > 2020-01-02 18:19 GMT+09:00, Pali Rohár <pali.rohar@gmail.com>: > > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > >> This adds the implementation of misc operations for exfat. > >> > >> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com> > >> Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> > >> --- > >> fs/exfat/misc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ > >> 1 file changed, 253 insertions(+) > >> create mode 100644 fs/exfat/misc.c > >> > >> diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c > >> new file mode 100644 > >> index 000000000000..7f533bcb3b3f > >> --- /dev/null > >> +++ b/fs/exfat/misc.c > > > > ... > > > >> +/* <linux/time.h> externs sys_tz > >> + * extern struct timezone sys_tz; > >> + */ > >> +#define UNIX_SECS_1980 315532800L > >> + > >> +#if BITS_PER_LONG == 64 > >> +#define UNIX_SECS_2108 4354819200L > >> +#endif > > > > ... > > > >> +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) > >> +/* Convert linear UNIX date to a FAT time/date pair. */ > >> +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 > >> *ts, > >> + struct exfat_date_time *tp) > >> +{ > >> + time_t second = ts->tv_sec; > >> + time_t day, month, year; > >> + time_t ld; /* leap day */ > > > > Question for other maintainers: Has kernel code already time_t defined > > as 64bit? Or it is still just 32bit and 32bit systems and some time64_t > > needs to be used? I remember that there was discussion about these > > problems, but do not know if it was changed/fixed or not... Just a > > pointer for possible Y2038 problem. As "ts" is of type timespec64, but > > "second" of type time_t. > My bad, I will change it with time64_t. Now I have looked into sources and time_t is just typedef from __kernel_old_time_t type. So it looks like that time64_t is the type which should be used in new code. But somebody else should confirm this information. > > > >> + > >> + /* Treats as local time with proper time */ > >> + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; > >> + tp->timezone.valid = 1; > >> + tp->timezone.off = TIMEZONE_CUR_OFFSET(); > >> + > >> + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ > >> + if (second < UNIX_SECS_1980) { > >> + tp->second = 0; > >> + tp->minute = 0; > >> + tp->hour = 0; > >> + tp->day = 1; > >> + tp->month = 1; > >> + tp->year = 0; > >> + return; > >> + } > >> + > >> + if (second >= UNIX_SECS_2108) { > > > > Hello, this code cause compile errors on 32bit systems as UNIX_SECS_2108 > > macro is not defined when BITS_PER_LONG == 32. > > > > Value 4354819200 really cannot fit into 32bit signed integer, so you > > should use 64bit signed integer. I would suggest to define this macro > > value via LL not just L suffix (and it would work on both 32 and 64bit) > Okay. > > > > #define UNIX_SECS_2108 4354819200LL > > > >> + tp->second = 59; > >> + tp->minute = 59; > >> + tp->hour = 23; > >> + tp->day = 31; > >> + tp->month = 12; > >> + tp->year = 127; > >> + return; > >> + } > Okay, I will check it. > Thanks for your review! > > > > -- > > Pali Rohár > > pali.rohar@gmail.com > >
On Thursday 02 January 2020 12:40:34 Pali Rohár wrote: > On Thursday 02 January 2020 20:30:03 Namjae Jeon wrote: > > 2020-01-02 18:19 GMT+09:00, Pali Rohár <pali.rohar@gmail.com>: > > > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > > >> This adds the implementation of misc operations for exfat. > > >> > > >> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com> > > >> Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> > > >> --- > > >> fs/exfat/misc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ > > >> 1 file changed, 253 insertions(+) > > >> create mode 100644 fs/exfat/misc.c > > >> > > >> diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c > > >> new file mode 100644 > > >> index 000000000000..7f533bcb3b3f > > >> --- /dev/null > > >> +++ b/fs/exfat/misc.c > > > > > > ... > > > > > >> +/* <linux/time.h> externs sys_tz > > >> + * extern struct timezone sys_tz; > > >> + */ > > >> +#define UNIX_SECS_1980 315532800L > > >> + > > >> +#if BITS_PER_LONG == 64 > > >> +#define UNIX_SECS_2108 4354819200L > > >> +#endif > > > > > > ... > > > > > >> +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) > > >> +/* Convert linear UNIX date to a FAT time/date pair. */ > > >> +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 > > >> *ts, > > >> + struct exfat_date_time *tp) > > >> +{ > > >> + time_t second = ts->tv_sec; > > >> + time_t day, month, year; > > >> + time_t ld; /* leap day */ > > > > > > Question for other maintainers: Has kernel code already time_t defined > > > as 64bit? Or it is still just 32bit and 32bit systems and some time64_t > > > needs to be used? I remember that there was discussion about these > > > problems, but do not know if it was changed/fixed or not... Just a > > > pointer for possible Y2038 problem. As "ts" is of type timespec64, but > > > "second" of type time_t. > > My bad, I will change it with time64_t. > > Now I have looked into sources and time_t is just typedef from > __kernel_old_time_t type. So it looks like that time64_t is the type > which should be used in new code. > > But somebody else should confirm this information. > > > > > > >> + > > >> + /* Treats as local time with proper time */ > > >> + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; > > >> + tp->timezone.valid = 1; > > >> + tp->timezone.off = TIMEZONE_CUR_OFFSET(); > > >> + > > >> + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ > > >> + if (second < UNIX_SECS_1980) { > > >> + tp->second = 0; > > >> + tp->minute = 0; > > >> + tp->hour = 0; > > >> + tp->day = 1; > > >> + tp->month = 1; > > >> + tp->year = 0; > > >> + return; > > >> + } > > >> + > > >> + if (second >= UNIX_SECS_2108) { > > > > > > Hello, this code cause compile errors on 32bit systems as UNIX_SECS_2108 > > > macro is not defined when BITS_PER_LONG == 32. > > > > > > Value 4354819200 really cannot fit into 32bit signed integer, so you > > > should use 64bit signed integer. I would suggest to define this macro > > > value via LL not just L suffix (and it would work on both 32 and 64bit) > > Okay. > > > > > > #define UNIX_SECS_2108 4354819200LL > > > > > >> + tp->second = 59; > > >> + tp->minute = 59; > > >> + tp->hour = 23; > > >> + tp->day = 31; > > >> + tp->month = 12; > > >> + tp->year = 127; > > >> + return; > > >> + } > > Okay, I will check it. > > Thanks for your review! Also I think that you should apply this Arnd's patch: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=44f6b40c225eb8e82eba8fd96d8f3fb843bc5a09 And maybe it is a good idea to look at applied staging patches: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/staging/exfat
> Also I think that you should apply this Arnd's patch: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=44f6b40c225eb8e82eba8fd96d8f3fb843bc5a09 Okay:) > > And maybe it is a good idea to look at applied staging patches: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/staging/exfat I will check them. Thanks a lot! > > -- > Pali Rohár > pali.rohar@gmail.com >
Arnd, can you review the exfat time handling, especially vs y2038 related issues? On Thu, Jan 02, 2020 at 10:19:02AM +0100, Pali Rohár wrote: > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > > This adds the implementation of misc operations for exfat. > > > > Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com> > > Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> > > --- > > fs/exfat/misc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 253 insertions(+) > > create mode 100644 fs/exfat/misc.c > > > > diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c > > new file mode 100644 > > index 000000000000..7f533bcb3b3f > > --- /dev/null > > +++ b/fs/exfat/misc.c > > ... > > > +/* <linux/time.h> externs sys_tz > > + * extern struct timezone sys_tz; > > + */ > > +#define UNIX_SECS_1980 315532800L > > + > > +#if BITS_PER_LONG == 64 > > +#define UNIX_SECS_2108 4354819200L > > +#endif > > ... > > > +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) > > +/* Convert linear UNIX date to a FAT time/date pair. */ > > +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 *ts, > > + struct exfat_date_time *tp) > > +{ > > + time_t second = ts->tv_sec; > > + time_t day, month, year; > > + time_t ld; /* leap day */ > > Question for other maintainers: Has kernel code already time_t defined > as 64bit? Or it is still just 32bit and 32bit systems and some time64_t > needs to be used? I remember that there was discussion about these > problems, but do not know if it was changed/fixed or not... Just a > pointer for possible Y2038 problem. As "ts" is of type timespec64, but > "second" of type time_t. > > > + > > + /* Treats as local time with proper time */ > > + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; > > + tp->timezone.valid = 1; > > + tp->timezone.off = TIMEZONE_CUR_OFFSET(); > > + > > + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ > > + if (second < UNIX_SECS_1980) { > > + tp->second = 0; > > + tp->minute = 0; > > + tp->hour = 0; > > + tp->day = 1; > > + tp->month = 1; > > + tp->year = 0; > > + return; > > + } > > + > > + if (second >= UNIX_SECS_2108) { > > Hello, this code cause compile errors on 32bit systems as UNIX_SECS_2108 > macro is not defined when BITS_PER_LONG == 32. > > Value 4354819200 really cannot fit into 32bit signed integer, so you > should use 64bit signed integer. I would suggest to define this macro > value via LL not just L suffix (and it would work on both 32 and 64bit) > > #define UNIX_SECS_2108 4354819200LL > > > + tp->second = 59; > > + tp->minute = 59; > > + tp->hour = 23; > > + tp->day = 31; > > + tp->month = 12; > > + tp->year = 127; > > + return; > > + } > > -- > Pali Rohár > pali.rohar@gmail.com ---end quoted text---
On Wed, Jan 8, 2020 at 7:03 PM Christoph Hellwig <hch@lst.de> wrote: > > Arnd, can you review the exfat time handling, especially vs y2038 > related issues? Sure, thanks for adding me to the loop > On Thu, Jan 02, 2020 at 10:19:02AM +0100, Pali Rohár wrote: > > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > > > +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) > > > +/* Convert linear UNIX date to a FAT time/date pair. */ > > > +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 *ts, > > > + struct exfat_date_time *tp) > > > +{ > > > + time_t second = ts->tv_sec; > > > + time_t day, month, year; > > > + time_t ld; /* leap day */ > > > > Question for other maintainers: Has kernel code already time_t defined > > as 64bit? Or it is still just 32bit and 32bit systems and some time64_t > > needs to be used? I remember that there was discussion about these > > problems, but do not know if it was changed/fixed or not... Just a > > pointer for possible Y2038 problem. As "ts" is of type timespec64, but > > "second" of type time_t. I am actually very close to sending the patches to remove the time_t definition from the kernel, at least in yesterday's version there were no users. exfat_time_unix2fat() seems to be a copy of the old fat_time_unix2fat() that we fixed a while ago, please have a look at that implementation based on time64_to_tm(), which avoids time_t. Arnd
> On Wed, Jan 8, 2020 at 7:03 PM Christoph Hellwig <hch@lst.de> wrote: > > > > Arnd, can you review the exfat time handling, especially vs y2038 > > related issues? > > Sure, thanks for adding me to the loop > > > On Thu, Jan 02, 2020 at 10:19:02AM +0100, Pali Rohár wrote: > > > On Thursday 02 January 2020 16:20:32 Namjae Jeon wrote: > > > > +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) > & 0x7F) > > > > +/* Convert linear UNIX date to a FAT time/date pair. */ void > > > > +exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 > *ts, > > > > + struct exfat_date_time *tp) { > > > > + time_t second = ts->tv_sec; > > > > + time_t day, month, year; > > > > + time_t ld; /* leap day */ > > > > > > Question for other maintainers: Has kernel code already time_t > > > defined as 64bit? Or it is still just 32bit and 32bit systems and > > > some time64_t needs to be used? I remember that there was discussion > > > about these problems, but do not know if it was changed/fixed or > > > not... Just a pointer for possible Y2038 problem. As "ts" is of type > > > timespec64, but "second" of type time_t. > > I am actually very close to sending the patches to remove the time_t > definition from the kernel, at least in yesterday's version there were no > users. > > exfat_time_unix2fat() seems to be a copy of the old fat_time_unix2fat() > that we fixed a while ago, please have a look at that implementation based > on time64_to_tm(), which avoids time_t. Okay, Pali reported it and suggested to check your patch in staging/exfat. I will fix it on v10. Thanks for your review! > > Arnd
diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c new file mode 100644 index 000000000000..7f533bcb3b3f --- /dev/null +++ b/fs/exfat/misc.c @@ -0,0 +1,253 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Written 1992,1993 by Werner Almesberger + * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980 + * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru) + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. + */ + +#include <linux/time.h> +#include <linux/fs.h> +#include <linux/slab.h> +#include <linux/buffer_head.h> + +#include "exfat_raw.h" +#include "exfat_fs.h" + +/* + * exfat_fs_error reports a file system problem that might indicate fa data + * corruption/inconsistency. Depending on 'errors' mount option the + * panic() is called, or error message is printed FAT and nothing is done, + * or filesystem is remounted read-only (default behavior). + * In case the file system is remounted read-only, it can be made writable + * again by remounting it. + */ +void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) +{ + struct exfat_mount_options *opts = &EXFAT_SB(sb)->options; + va_list args; + struct va_format vaf; + + if (report) { + va_start(args, fmt); + vaf.fmt = fmt; + vaf.va = &args; + exfat_msg(sb, KERN_ERR, "error, %pV\n", &vaf); + va_end(args); + } + + if (opts->errors == EXFAT_ERRORS_PANIC) { + panic("exFAT-fs (%s): fs panic from previous error\n", + sb->s_id); + } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) { + sb->s_flags |= SB_RDONLY; + exfat_msg(sb, KERN_ERR, "Filesystem has been set read-only"); + } +} + +/* + * exfat_msg() - print preformated EXFAT specific messages. + * All logs except what uses exfat_fs_error() should be written by exfat_msg() + */ +void exfat_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; + /* level means KERN_ pacility level */ + printk("%sexFAT-fs (%s): %pV\n", level, sb->s_id, &vaf); + va_end(args); +} + +/* <linux/time.h> externs sys_tz + * extern struct timezone sys_tz; + */ +#define UNIX_SECS_1980 315532800L + +#if BITS_PER_LONG == 64 +#define UNIX_SECS_2108 4354819200L +#endif + +/* days between 1970/01/01 and 1980/01/01 (2 leap days) */ +#define DAYS_DELTA_DECADE (365 * 10 + 2) +/* 120 (2100 - 1980) isn't leap year */ +#define NO_LEAP_YEAR_2100 (120) +#define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100) + +#define SECS_PER_MIN (60) +#define SECS_PER_HOUR (60 * SECS_PER_MIN) +#define SECS_PER_DAY (24 * SECS_PER_HOUR) + +#define MAKE_LEAP_YEAR(leap_year, year) \ + do { \ + /* 2100 isn't leap year */ \ + if (unlikely(year > NO_LEAP_YEAR_2100)) \ + leap_year = ((year + 3) / 4) - 1; \ + else \ + leap_year = ((year + 3) / 4); \ + } while (0) + +/* Linear day numbers of the respective 1sts in non-leap years. */ +static time_t accum_days_in_year[] = { + /* Month : N 01 02 03 04 05 06 07 08 09 10 11 12 */ + 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, +}; + +#define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) +/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */ +void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec64 *ts, + struct exfat_date_time *tp) +{ + time_t year = tp->year; + time_t ld; /* leap day */ + + MAKE_LEAP_YEAR(ld, year); + + if (IS_LEAP_YEAR(year) && (tp->month) > 2) + ld++; + + ts->tv_sec = tp->second + tp->minute * SECS_PER_MIN + + tp->hour * SECS_PER_HOUR + + (year * 365 + ld + accum_days_in_year[tp->month] + + (tp->day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY; + + ts->tv_nsec = 0; + + /* Treat as local time */ + if (!tp->timezone.valid) { + ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN; + return; + } + + /* Treat as UTC time, but need to adjust timezone to UTC0 */ + if (tp->timezone.off <= 0x3F) + ts->tv_sec -= TIMEZONE_SEC(tp->timezone.off); + else /* 0x40 <= (tp->timezone & 0x7F) <=0x7F */ + ts->tv_sec += TIMEZONE_SEC(0x80 - tp->timezone.off); +} + +#define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F) +/* Convert linear UNIX date to a FAT time/date pair. */ +void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec64 *ts, + struct exfat_date_time *tp) +{ + time_t second = ts->tv_sec; + time_t day, month, year; + time_t ld; /* leap day */ + + /* Treats as local time with proper time */ + second -= sys_tz.tz_minuteswest * SECS_PER_MIN; + tp->timezone.valid = 1; + tp->timezone.off = TIMEZONE_CUR_OFFSET(); + + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ + if (second < UNIX_SECS_1980) { + tp->second = 0; + tp->minute = 0; + tp->hour = 0; + tp->day = 1; + tp->month = 1; + tp->year = 0; + return; + } + + if (second >= UNIX_SECS_2108) { + tp->second = 59; + tp->minute = 59; + tp->hour = 23; + tp->day = 31; + tp->month = 12; + tp->year = 127; + return; + } + + day = second / SECS_PER_DAY - DAYS_DELTA_DECADE; + year = day / 365; + + MAKE_LEAP_YEAR(ld, year); + if (year * 365 + ld > day) + year--; + + MAKE_LEAP_YEAR(ld, year); + day -= year * 365 + ld; + + if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) { + month = 2; + } else { + if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3]) + day--; + for (month = 1; month < 12; month++) { + if (accum_days_in_year[month + 1] > day) + break; + } + } + day -= accum_days_in_year[month]; + + tp->second = second % SECS_PER_MIN; + tp->minute = (second / SECS_PER_MIN) % 60; + tp->hour = (second / SECS_PER_HOUR) % 24; + tp->day = day + 1; + tp->month = month; + tp->year = year; +} + +struct exfat_timestamp *exfat_tm_now(struct exfat_sb_info *sbi, + struct exfat_timestamp *tp) +{ + struct timespec64 ts; + struct exfat_date_time dt; + + ktime_get_real_ts64(&ts); + exfat_time_unix2fat(sbi, &ts, &dt); + + tp->year = dt.year; + tp->mon = dt.month; + tp->day = dt.day; + tp->hour = dt.hour; + tp->min = dt.minute; + tp->sec = dt.second; + tp->tz.value = dt.timezone.value; + + return tp; +} + +unsigned short exfat_calc_chksum_2byte(void *data, int len, + unsigned short chksum, int type) +{ + int i; + unsigned char *c = (unsigned char *)data; + + for (i = 0; i < len; i++, c++) { + if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY)) + continue; + chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) + + (unsigned short)*c; + } + return chksum; +} + +void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync) +{ + set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state); + set_buffer_uptodate(bh); + mark_buffer_dirty(bh); + + if (sync) + sync_dirty_buffer(bh); +} + +void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, + unsigned int size, unsigned char flags) +{ + ec->dir = dir; + ec->size = size; + ec->flags = flags; +} + +void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec) +{ + return exfat_chain_set(dup, ec->dir, ec->size, ec->flags); +}