2019-09-09 13:21:57

by Valentin Vidić

[permalink] [raw]
Subject: [PATCH v3 2/4] staging: exfat: drop duplicate date_time_t struct

Use timestamp_t for everything and cleanup duplicate code.

Signed-off-by: Valentin Vidic <[email protected]>
---
drivers/staging/exfat/exfat.h | 35 +++---
drivers/staging/exfat/exfat_super.c | 158 ++++++++--------------------
2 files changed, 55 insertions(+), 138 deletions(-)

diff --git a/drivers/staging/exfat/exfat.h b/drivers/staging/exfat/exfat.h
index 0aa14dea4e09..58e1e889779f 100644
--- a/drivers/staging/exfat/exfat.h
+++ b/drivers/staging/exfat/exfat.h
@@ -241,16 +241,6 @@ static inline u16 get_row_index(u16 i)
#define UNI_PAR_DIR_NAME "\0.\0."
#endif

-struct date_time_t {
- u16 Year;
- u16 Month;
- u16 Day;
- u16 Hour;
- u16 Minute;
- u16 Second;
- u16 MilliSecond;
-};
-
struct part_info_t {
u32 Offset; /* start sector number of the partition */
u32 Size; /* in sectors */
@@ -289,6 +279,16 @@ struct file_id_t {
u32 hint_last_clu;
};

+struct timestamp_t {
+ u16 millisec; /* 0 ~ 999 */
+ u16 sec; /* 0 ~ 59 */
+ u16 min; /* 0 ~ 59 */
+ u16 hour; /* 0 ~ 23 */
+ u16 day; /* 1 ~ 31 */
+ u16 mon; /* 1 ~ 12 */
+ u16 year; /* 0 ~ 127 (since 1980) */
+};
+
struct dir_entry_t {
char Name[MAX_NAME_LENGTH * MAX_CHARSET_SIZE];

@@ -298,18 +298,9 @@ struct dir_entry_t {
u32 Attr;
u64 Size;
u32 NumSubdirs;
- struct date_time_t CreateTimestamp;
- struct date_time_t ModifyTimestamp;
- struct date_time_t AccessTimestamp;
-};
-
-struct timestamp_t {
- u16 sec; /* 0 ~ 59 */
- u16 min; /* 0 ~ 59 */
- u16 hour; /* 0 ~ 23 */
- u16 day; /* 1 ~ 31 */
- u16 mon; /* 1 ~ 12 */
- u16 year; /* 0 ~ 127 (since 1980) */
+ struct timestamp_t CreateTimestamp;
+ struct timestamp_t ModifyTimestamp;
+ struct timestamp_t AccessTimestamp;
};

/* MS_DOS FAT partition boot record (512 bytes) */
diff --git a/drivers/staging/exfat/exfat_super.c b/drivers/staging/exfat/exfat_super.c
index 3a58907caf7c..54b6c2ff3c96 100644
--- a/drivers/staging/exfat/exfat_super.c
+++ b/drivers/staging/exfat/exfat_super.c
@@ -56,16 +56,16 @@ static void exfat_write_super(struct super_block *sb);
#define UNIX_SECS_2108 4354819200L

/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
-static void exfat_time_fat2unix(struct timespec64 *ts, struct date_time_t *tp)
+static void exfat_time_fat2unix(struct timespec64 *ts, struct timestamp_t *tp)
{
- ts->tv_sec = mktime64(tp->Year + 1980, tp->Month + 1, tp->Day,
- tp->Hour, tp->Minute, tp->Second);
+ ts->tv_sec = mktime64(tp->year + 1980, tp->mon + 1, tp->day,
+ tp->hour, tp->min, tp->sec);

- ts->tv_nsec = tp->MilliSecond * NSEC_PER_MSEC;
+ ts->tv_nsec = tp->millisec * NSEC_PER_MSEC;
}

/* Convert linear UNIX date to a FAT time/date pair. */
-static void exfat_time_unix2fat(struct timespec64 *ts, struct date_time_t *tp)
+static void exfat_time_unix2fat(struct timespec64 *ts, struct timestamp_t *tp)
{
time64_t second = ts->tv_sec;
struct tm tm;
@@ -73,69 +73,42 @@ static void exfat_time_unix2fat(struct timespec64 *ts, struct date_time_t *tp)
time64_to_tm(second, 0, &tm);

if (second < UNIX_SECS_1980) {
- tp->MilliSecond = 0;
- tp->Second = 0;
- tp->Minute = 0;
- tp->Hour = 0;
- tp->Day = 1;
- tp->Month = 1;
- tp->Year = 0;
+ tp->millisec = 0;
+ tp->sec = 0;
+ tp->min = 0;
+ tp->hour = 0;
+ tp->day = 1;
+ tp->mon = 1;
+ tp->year = 0;
return;
}

if (second >= UNIX_SECS_2108) {
- tp->MilliSecond = 999;
- tp->Second = 59;
- tp->Minute = 59;
- tp->Hour = 23;
- tp->Day = 31;
- tp->Month = 12;
- tp->Year = 127;
+ tp->millisec = 999;
+ tp->sec = 59;
+ tp->min = 59;
+ tp->hour = 23;
+ tp->day = 31;
+ tp->mon = 12;
+ tp->year = 127;
return;
}

- tp->MilliSecond = ts->tv_nsec / NSEC_PER_MSEC;
- tp->Second = tm.tm_sec;
- tp->Minute = tm.tm_min;
- tp->Hour = tm.tm_hour;
- tp->Day = tm.tm_mday;
- tp->Month = tm.tm_mon + 1;
- tp->Year = tm.tm_year + 1900 - 1980;
+ tp->millisec = ts->tv_nsec / NSEC_PER_MSEC;
+ tp->sec = tm.tm_sec;
+ tp->min = tm.tm_min;
+ tp->hour = tm.tm_hour;
+ tp->day = tm.tm_mday;
+ tp->mon = tm.tm_mon + 1;
+ tp->year = tm.tm_year + 1900 - 1980;
}

struct timestamp_t *tm_current(struct timestamp_t *tp)
{
- time64_t second = ktime_get_real_seconds();
- struct tm tm;
-
- time64_to_tm(second, 0, &tm);
-
- if (second < UNIX_SECS_1980) {
- tp->sec = 0;
- tp->min = 0;
- tp->hour = 0;
- tp->day = 1;
- tp->mon = 1;
- tp->year = 0;
- return tp;
- }
+ struct timespec64 ts;

- if (second >= UNIX_SECS_2108) {
- tp->sec = 59;
- tp->min = 59;
- tp->hour = 23;
- tp->day = 31;
- tp->mon = 12;
- tp->year = 127;
- return tp;
- }
-
- tp->sec = tm.tm_sec;
- tp->min = tm.tm_min;
- tp->hour = tm.tm_hour;
- tp->day = tm.tm_mday;
- tp->mon = tm.tm_mon + 1;
- tp->year = tm.tm_year + 1900 - 1980;
+ ktime_get_real_ts64(&ts);
+ exfat_time_unix2fat(&ts, tp);

return tp;
}
@@ -1500,7 +1473,6 @@ static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
int ret = FFS_SUCCESS;
struct chain_t dir;
struct uni_name_t uni_name;
- struct timestamp_t tm;
struct dentry_t *ep, *ep2;
struct super_block *sb = inode->i_sb;
struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
@@ -1518,11 +1490,11 @@ static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
(fid->entry == -1)) {
info->Attr = ATTR_SUBDIR;
memset((char *)&info->CreateTimestamp, 0,
- sizeof(struct date_time_t));
+ sizeof(struct timestamp_t));
memset((char *)&info->ModifyTimestamp, 0,
- sizeof(struct date_time_t));
+ sizeof(struct timestamp_t));
memset((char *)&info->AccessTimestamp, 0,
- sizeof(struct date_time_t));
+ sizeof(struct timestamp_t));
strcpy(info->ShortName, ".");
strcpy(info->Name, ".");

@@ -1573,25 +1545,9 @@ static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
/* set FILE_INFO structure using the acquired struct dentry_t */
info->Attr = p_fs->fs_func->get_entry_attr(ep);

- p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE);
- info->CreateTimestamp.Year = tm.year;
- info->CreateTimestamp.Month = tm.mon;
- info->CreateTimestamp.Day = tm.day;
- info->CreateTimestamp.Hour = tm.hour;
- info->CreateTimestamp.Minute = tm.min;
- info->CreateTimestamp.Second = tm.sec;
- info->CreateTimestamp.MilliSecond = 0;
-
- p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
- info->ModifyTimestamp.Year = tm.year;
- info->ModifyTimestamp.Month = tm.mon;
- info->ModifyTimestamp.Day = tm.day;
- info->ModifyTimestamp.Hour = tm.hour;
- info->ModifyTimestamp.Minute = tm.min;
- info->ModifyTimestamp.Second = tm.sec;
- info->ModifyTimestamp.MilliSecond = 0;
-
- memset((char *) &info->AccessTimestamp, 0, sizeof(struct date_time_t));
+ p_fs->fs_func->get_entry_time(ep, &info->CreateTimestamp, TM_CREATE);
+ p_fs->fs_func->get_entry_time(ep, &info->ModifyTimestamp, TM_MODIFY);
+ memset((char *)&info->AccessTimestamp, 0, sizeof(struct timestamp_t));

*(uni_name.name) = 0x0;
/* XXX this is very bad for exfat cuz name is already included in es.
@@ -1650,7 +1606,6 @@ static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info)
{
sector_t sector = 0;
int ret = FFS_SUCCESS;
- struct timestamp_t tm;
struct dentry_t *ep, *ep2;
struct entry_set_cache_t *es = NULL;
struct super_block *sb = inode->i_sb;
@@ -1697,22 +1652,8 @@ static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info)
p_fs->fs_func->set_entry_attr(ep, info->Attr);

/* set FILE_INFO structure using the acquired struct dentry_t */
- tm.sec = info->CreateTimestamp.Second;
- tm.min = info->CreateTimestamp.Minute;
- tm.hour = info->CreateTimestamp.Hour;
- tm.day = info->CreateTimestamp.Day;
- tm.mon = info->CreateTimestamp.Month;
- tm.year = info->CreateTimestamp.Year;
- p_fs->fs_func->set_entry_time(ep, &tm, TM_CREATE);
-
- tm.sec = info->ModifyTimestamp.Second;
- tm.min = info->ModifyTimestamp.Minute;
- tm.hour = info->ModifyTimestamp.Hour;
- tm.day = info->ModifyTimestamp.Day;
- tm.mon = info->ModifyTimestamp.Month;
- tm.year = info->ModifyTimestamp.Year;
- p_fs->fs_func->set_entry_time(ep, &tm, TM_MODIFY);
-
+ p_fs->fs_func->set_entry_time(ep, &info->CreateTimestamp, TM_CREATE);
+ p_fs->fs_func->set_entry_time(ep, &info->ModifyTimestamp, TM_MODIFY);
p_fs->fs_func->set_entry_size(ep2, info->Size);

if (p_fs->vol_type != EXFAT) {
@@ -1939,7 +1880,6 @@ static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry)
sector_t sector;
struct chain_t dir, clu;
struct uni_name_t uni_name;
- struct timestamp_t tm;
struct dentry_t *ep;
struct super_block *sb = inode->i_sb;
struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
@@ -2036,26 +1976,12 @@ static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry)
buf_lock(sb, sector);
dir_entry->Attr = fs_func->get_entry_attr(ep);

- fs_func->get_entry_time(ep, &tm, TM_CREATE);
- dir_entry->CreateTimestamp.Year = tm.year;
- dir_entry->CreateTimestamp.Month = tm.mon;
- dir_entry->CreateTimestamp.Day = tm.day;
- dir_entry->CreateTimestamp.Hour = tm.hour;
- dir_entry->CreateTimestamp.Minute = tm.min;
- dir_entry->CreateTimestamp.Second = tm.sec;
- dir_entry->CreateTimestamp.MilliSecond = 0;
-
- fs_func->get_entry_time(ep, &tm, TM_MODIFY);
- dir_entry->ModifyTimestamp.Year = tm.year;
- dir_entry->ModifyTimestamp.Month = tm.mon;
- dir_entry->ModifyTimestamp.Day = tm.day;
- dir_entry->ModifyTimestamp.Hour = tm.hour;
- dir_entry->ModifyTimestamp.Minute = tm.min;
- dir_entry->ModifyTimestamp.Second = tm.sec;
- dir_entry->ModifyTimestamp.MilliSecond = 0;
-
+ fs_func->get_entry_time(ep, &dir_entry->CreateTimestamp,
+ TM_CREATE);
+ fs_func->get_entry_time(ep, &dir_entry->ModifyTimestamp,
+ TM_MODIFY);
memset((char *)&dir_entry->AccessTimestamp, 0,
- sizeof(struct date_time_t));
+ sizeof(struct timestamp_t));

*(uni_name.name) = 0x0;
fs_func->get_uni_name_from_ext_entry(sb, &dir, dentry,
--
2.20.1


2019-09-09 13:32:49

by Valentin Vidić

[permalink] [raw]
Subject: Re: [PATCH v3 2/4] staging: exfat: drop duplicate date_time_t struct

On Sun, Sep 08, 2019 at 07:50:31PM +0100, Greg Kroah-Hartman wrote:
> Wait, how are these "duplicate"? The fields are in different order,
> don't these refer to things on-disk?

On-disk combines the values from these structures in a different form:

offset bits
DoubleSeconds 0 5
Minute 5 6
Hour 11 5
Day 16 5
Month 21 4
Year 25 7

> Did you test this?

Just compile tested for now.

> > -struct date_time_t {
> > - u16 Year;
> > - u16 Month;
> > - u16 Day;
> > - u16 Hour;
> > - u16 Minute;
> > - u16 Second;
> > - u16 MilliSecond;
> > -};
> > -
> > struct part_info_t {
> > u32 Offset; /* start sector number of the partition */
> > u32 Size; /* in sectors */
> > @@ -289,6 +279,16 @@ struct file_id_t {
> > u32 hint_last_clu;
> > };
> >
> > +struct timestamp_t {
> > + u16 millisec; /* 0 ~ 999 */
> > + u16 sec; /* 0 ~ 59 */
> > + u16 min; /* 0 ~ 59 */
> > + u16 hour; /* 0 ~ 23 */
> > + u16 day; /* 1 ~ 31 */
> > + u16 mon; /* 1 ~ 12 */
> > + u16 year; /* 0 ~ 127 (since 1980) */
> > +};
>
> They really look "backwards" to me, how are these the same? What am I
> missing?

date_time_t was only used in a few functions and there was a lot of
copying of the same fields between the two structs. Also some code was
duplicated to do the same thing for each of the structs.

--
Valentin

2019-09-09 17:58:33

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3 2/4] staging: exfat: drop duplicate date_time_t struct

On Sun, Sep 08, 2019 at 05:35:37PM +0000, Valentin Vidic wrote:
> Use timestamp_t for everything and cleanup duplicate code.

Wait, how are these "duplicate"? The fields are in different order,
don't these refer to things on-disk?

Did you test this?

> -struct date_time_t {
> - u16 Year;
> - u16 Month;
> - u16 Day;
> - u16 Hour;
> - u16 Minute;
> - u16 Second;
> - u16 MilliSecond;
> -};
> -
> struct part_info_t {
> u32 Offset; /* start sector number of the partition */
> u32 Size; /* in sectors */
> @@ -289,6 +279,16 @@ struct file_id_t {
> u32 hint_last_clu;
> };
>
> +struct timestamp_t {
> + u16 millisec; /* 0 ~ 999 */
> + u16 sec; /* 0 ~ 59 */
> + u16 min; /* 0 ~ 59 */
> + u16 hour; /* 0 ~ 23 */
> + u16 day; /* 1 ~ 31 */
> + u16 mon; /* 1 ~ 12 */
> + u16 year; /* 0 ~ 127 (since 1980) */
> +};

They really look "backwards" to me, how are these the same? What am I
missing?

thanks,

greg k-h

2019-09-09 18:13:26

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3 2/4] staging: exfat: drop duplicate date_time_t struct

On Sun, Sep 08, 2019 at 05:35:37PM +0000, Valentin Vidic wrote:
> Use timestamp_t for everything and cleanup duplicate code.
>
> Signed-off-by: Valentin Vidic <[email protected]>
> ---
> drivers/staging/exfat/exfat.h | 35 +++---
> drivers/staging/exfat/exfat_super.c | 158 ++++++++--------------------
> 2 files changed, 55 insertions(+), 138 deletions(-)
>
> diff --git a/drivers/staging/exfat/exfat.h b/drivers/staging/exfat/exfat.h
> index 0aa14dea4e09..58e1e889779f 100644
> --- a/drivers/staging/exfat/exfat.h
> +++ b/drivers/staging/exfat/exfat.h
> @@ -241,16 +241,6 @@ static inline u16 get_row_index(u16 i)
> #define UNI_PAR_DIR_NAME "\0.\0."
> #endif
>
> -struct date_time_t {
> - u16 Year;
> - u16 Month;
> - u16 Day;
> - u16 Hour;
> - u16 Minute;
> - u16 Second;
> - u16 MilliSecond;
> -};
> -
> struct part_info_t {
> u32 Offset; /* start sector number of the partition */
> u32 Size; /* in sectors */
> @@ -289,6 +279,16 @@ struct file_id_t {
> u32 hint_last_clu;
> };
>
> +struct timestamp_t {
> + u16 millisec; /* 0 ~ 999 */

You added this field to this structure, why? You did not document that
in the changelog text above. Are you _sure_ you can do this and that
this does not refer to an on-disk layout?

thanks,

greg k-h

2019-09-09 18:15:47

by Valentin Vidić

[permalink] [raw]
Subject: Re: [PATCH v3 2/4] staging: exfat: drop duplicate date_time_t struct

On Sun, Sep 08, 2019 at 07:54:24PM +0100, Greg Kroah-Hartman wrote:
> On Sun, Sep 08, 2019 at 05:35:37PM +0000, Valentin Vidic wrote:
> > +struct timestamp_t {
> > + u16 millisec; /* 0 ~ 999 */
>
> You added this field to this structure, why? You did not document that
> in the changelog text above. Are you _sure_ you can do this and that
> this does not refer to an on-disk layout?

Both date_time_t and timestamp_t were used in memory only, but
date_time_t had the additional MilliSecond field. To keep the
functionality I added the millisec field to timestamp_t and
replaced all usages of date_time_t with timestamp_t.

For storing on disk the values from timestamp_t get shifted
and combined (exfat_set_entry_time).

--
Valentin