Skip to content

Commit 7decd1c

Browse files
OGAWAHirofumitorvalds
authored andcommitted
fat: Fix and cleanup timestamp conversion
This cleans date_dos2unix()/fat_date_unix2dos() up. New code should be much more readable. And this fixes those old functions. Those doesn't handle 2100 correctly. 2100 isn't leap year, but old one handles it as leap year. Also, with this, centi sec is handled and is fixed. Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9e975da commit 7decd1c

6 files changed

Lines changed: 130 additions & 72 deletions

File tree

‎fs/fat/dir.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
10891089
struct msdos_dir_entry *de;
10901090
sector_t blknr;
10911091
__le16 date, time;
1092+
u8 time_cs;
10921093
int err, cluster;
10931094

10941095
err = fat_alloc_clusters(dir, &cluster, 1);
@@ -1102,7 +1103,7 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
11021103
goto error_free;
11031104
}
11041105

1105-
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
1106+
fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
11061107

11071108
de = (struct msdos_dir_entry *)bhs[0]->b_data;
11081109
/* filling the new directory slots ("." and ".." entries) */
@@ -1112,13 +1113,14 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
11121113
de[0].lcase = de[1].lcase = 0;
11131114
de[0].time = de[1].time = time;
11141115
de[0].date = de[1].date = date;
1115-
de[0].ctime_cs = de[1].ctime_cs = 0;
11161116
if (sbi->options.isvfat) {
11171117
/* extra timestamps */
11181118
de[0].ctime = de[1].ctime = time;
1119+
de[0].ctime_cs = de[1].ctime_cs = time_cs;
11191120
de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
11201121
} else {
11211122
de[0].ctime = de[1].ctime = 0;
1123+
de[0].ctime_cs = de[1].ctime_cs = 0;
11221124
de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
11231125
}
11241126
de[0].start = cpu_to_le16(cluster);

‎fs/fat/fat.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
263263
extern void fat_fs_panic(struct super_block *s, const char *fmt, ...);
264264
extern void fat_clusters_flush(struct super_block *sb);
265265
extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
266-
extern int date_dos2unix(unsigned short time, unsigned short date, int tz_utc);
267-
extern void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date,
268-
int tz_utc);
266+
extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
267+
__le16 __time, __le16 __date, u8 time_cs);
268+
extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
269+
__le16 *time, __le16 *date, u8 *time_cs);
269270
extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
270271

271272
int fat_cache_init(void);

‎fs/fat/inode.c‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,12 @@ static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
381381
MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
382382
inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
383383
& ~((loff_t)sbi->cluster_size - 1)) >> 9;
384-
inode->i_mtime.tv_sec =
385-
date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date),
386-
sbi->options.tz_utc);
387-
inode->i_mtime.tv_nsec = 0;
384+
385+
fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
388386
if (sbi->options.isvfat) {
389-
int secs = de->ctime_cs / 100;
390-
int csecs = de->ctime_cs % 100;
391-
inode->i_ctime.tv_sec =
392-
date_dos2unix(le16_to_cpu(de->ctime),
393-
le16_to_cpu(de->cdate),
394-
sbi->options.tz_utc) + secs;
395-
inode->i_ctime.tv_nsec = csecs * 10000000;
396-
inode->i_atime.tv_sec =
397-
date_dos2unix(0, le16_to_cpu(de->adate),
398-
sbi->options.tz_utc);
399-
inode->i_atime.tv_nsec = 0;
387+
fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
388+
de->cdate, de->ctime_cs);
389+
fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
400390
} else
401391
inode->i_ctime = inode->i_atime = inode->i_mtime;
402392

@@ -591,16 +581,14 @@ static int fat_write_inode(struct inode *inode, int wait)
591581
raw_entry->attr = fat_attr(inode);
592582
raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
593583
raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
594-
fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time,
595-
&raw_entry->date, sbi->options.tz_utc);
584+
fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
585+
&raw_entry->date, NULL);
596586
if (sbi->options.isvfat) {
597587
__le16 atime;
598-
fat_date_unix2dos(inode->i_ctime.tv_sec, &raw_entry->ctime,
599-
&raw_entry->cdate, sbi->options.tz_utc);
600-
fat_date_unix2dos(inode->i_atime.tv_sec, &atime,
601-
&raw_entry->adate, sbi->options.tz_utc);
602-
raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 +
603-
inode->i_ctime.tv_nsec / 10000000;
588+
fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
589+
&raw_entry->cdate, &raw_entry->ctime_cs);
590+
fat_time_unix2fat(sbi, &inode->i_atime, &atime,
591+
&raw_entry->adate, NULL);
604592
}
605593
spin_unlock(&sbi->inode_hash_lock);
606594
mark_buffer_dirty(bh);

‎fs/fat/misc.c‎

Lines changed: 107 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,65 +135,131 @@ int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
135135

136136
extern struct timezone sys_tz;
137137

138+
/*
139+
* The epoch of FAT timestamp is 1980.
140+
* : bits : value
141+
* date: 0 - 4: day (1 - 31)
142+
* date: 5 - 8: month (1 - 12)
143+
* date: 9 - 15: year (0 - 127) from 1980
144+
* time: 0 - 4: sec (0 - 29) 2sec counts
145+
* time: 5 - 10: min (0 - 59)
146+
* time: 11 - 15: hour (0 - 23)
147+
*/
148+
#define SECS_PER_MIN 60
149+
#define SECS_PER_HOUR (60 * 60)
150+
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
151+
#define UNIX_SECS_1980 315532800L
152+
#if BITS_PER_LONG == 64
153+
#define UNIX_SECS_2108 4354819200L
154+
#endif
155+
/* days between 1.1.70 and 1.1.80 (2 leap days) */
156+
#define DAYS_DELTA (365 * 10 + 2)
157+
/* 120 (2100 - 1980) isn't leap year */
158+
#define YEAR_2100 120
159+
#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
160+
138161
/* Linear day numbers of the respective 1sts in non-leap years. */
139-
static int day_n[] = {
140-
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
141-
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
162+
static time_t days_in_year[] = {
163+
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
164+
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
142165
};
143166

144-
/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
145-
int date_dos2unix(unsigned short time, unsigned short date, int tz_utc)
167+
/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
168+
void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
169+
__le16 __time, __le16 __date, u8 time_cs)
146170
{
147-
int month, year, secs;
171+
u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
172+
time_t second, day, leap_day, month, year;
148173

149-
/*
150-
* first subtract and mask after that... Otherwise, if
151-
* date == 0, bad things happen
152-
*/
153-
month = ((date >> 5) - 1) & 15;
154-
year = date >> 9;
155-
secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
156-
((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
157-
month < 2 ? 1 : 0)+3653);
158-
/* days since 1.1.70 plus 80's leap day */
159-
if (!tz_utc)
160-
secs += sys_tz.tz_minuteswest*60;
161-
return secs;
174+
year = date >> 9;
175+
month = max(1, (date >> 5) & 0xf);
176+
day = max(1, date & 0x1f) - 1;
177+
178+
leap_day = (year + 3) / 4;
179+
if (year > YEAR_2100) /* 2100 isn't leap year */
180+
leap_day--;
181+
if (IS_LEAP_YEAR(year) && month > 2)
182+
leap_day++;
183+
184+
second = (time & 0x1f) << 1;
185+
second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
186+
second += (time >> 11) * SECS_PER_HOUR;
187+
second += (year * 365 + leap_day
188+
+ days_in_year[month] + day
189+
+ DAYS_DELTA) * SECS_PER_DAY;
190+
191+
if (!sbi->options.tz_utc)
192+
second += sys_tz.tz_minuteswest * SECS_PER_MIN;
193+
194+
if (time_cs) {
195+
ts->tv_sec = second + (time_cs / 100);
196+
ts->tv_nsec = (time_cs % 100) * 10000000;
197+
} else {
198+
ts->tv_sec = second;
199+
ts->tv_nsec = 0;
200+
}
162201
}
163202

164-
/* Convert linear UNIX date to a MS-DOS time/date pair. */
165-
void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date, int tz_utc)
203+
/* Convert linear UNIX date to a FAT time/date pair. */
204+
void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
205+
__le16 *time, __le16 *date, u8 *time_cs)
166206
{
167-
int day, year, nl_day, month;
207+
time_t second = ts->tv_sec;
208+
time_t day, leap_day, month, year;
168209

169-
if (!tz_utc)
170-
unix_date -= sys_tz.tz_minuteswest*60;
210+
if (!sbi->options.tz_utc)
211+
second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
171212

172213
/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
173-
if (unix_date < 315532800)
174-
unix_date = 315532800;
175-
176-
*time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
177-
(((unix_date/3600) % 24) << 11));
178-
day = unix_date/86400-3652;
179-
year = day/365;
180-
if ((year+3)/4+365*year > day)
214+
if (second < UNIX_SECS_1980) {
215+
*time = 0;
216+
*date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
217+
if (time_cs)
218+
*time_cs = 0;
219+
return;
220+
}
221+
#if BITS_PER_LONG == 64
222+
if (second >= UNIX_SECS_2108) {
223+
*time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
224+
*date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
225+
if (time_cs)
226+
*time_cs = 199;
227+
return;
228+
}
229+
#endif
230+
231+
day = second / SECS_PER_DAY - DAYS_DELTA;
232+
year = day / 365;
233+
leap_day = (year + 3) / 4;
234+
if (year > YEAR_2100) /* 2100 isn't leap year */
235+
leap_day--;
236+
if (year * 365 + leap_day > day)
181237
year--;
182-
day -= (year+3)/4+365*year;
183-
if (day == 59 && !(year & 3)) {
184-
nl_day = day;
238+
leap_day = (year + 3) / 4;
239+
if (year > YEAR_2100) /* 2100 isn't leap year */
240+
leap_day--;
241+
day -= year * 365 + leap_day;
242+
243+
if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
185244
month = 2;
186245
} else {
187-
nl_day = (year & 3) || day <= 59 ? day : day-1;
188-
for (month = 0; month < 12; month++) {
189-
if (day_n[month] > nl_day)
246+
if (IS_LEAP_YEAR(year) && day > days_in_year[3])
247+
day--;
248+
for (month = 1; month < 12; month++) {
249+
if (days_in_year[month + 1] > day)
190250
break;
191251
}
192252
}
193-
*date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
194-
}
253+
day -= days_in_year[month];
195254

196-
EXPORT_SYMBOL_GPL(fat_date_unix2dos);
255+
*time = cpu_to_le16(((second / SECS_PER_HOUR) % 24) << 11
256+
| ((second / SECS_PER_MIN) % 60) << 5
257+
| (second % SECS_PER_MIN) >> 1);
258+
*date = cpu_to_le16((year << 9) | (month << 5) | (day + 1));
259+
if (time_cs)
260+
*time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
261+
}
262+
EXPORT_SYMBOL_GPL(fat_time_unix2fat);
197263

198264
int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
199265
{

‎fs/fat/namei_msdos.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static int msdos_add_entry(struct inode *dir, const unsigned char *name,
247247
if (is_hid)
248248
de.attr |= ATTR_HIDDEN;
249249
de.lcase = 0;
250-
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
250+
fat_time_unix2fat(sbi, ts, &time, &date, NULL);
251251
de.cdate = de.adate = 0;
252252
de.ctime = 0;
253253
de.ctime_cs = 0;

‎fs/fat/namei_vfat.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ static int vfat_build_slots(struct inode *dir, const unsigned char *name,
568568
unsigned char msdos_name[MSDOS_NAME];
569569
wchar_t *uname;
570570
__le16 time, date;
571+
u8 time_cs;
571572
int err, ulen, usize, i;
572573
loff_t offset;
573574

@@ -620,10 +621,10 @@ static int vfat_build_slots(struct inode *dir, const unsigned char *name,
620621
memcpy(de->name, msdos_name, MSDOS_NAME);
621622
de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
622623
de->lcase = lcase;
623-
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
624+
fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
624625
de->time = de->ctime = time;
625626
de->date = de->cdate = de->adate = date;
626-
de->ctime_cs = 0;
627+
de->ctime_cs = time_cs;
627628
de->start = cpu_to_le16(cluster);
628629
de->starthi = cpu_to_le16(cluster >> 16);
629630
de->size = 0;

0 commit comments

Comments
 (0)