Skip to content

Commit b271e06

Browse files
Joe Petersontorvalds
authored andcommitted
fatfs: add UTC timestamp option
Provide a new mount option ("tz=UTC") for DOS (vfat/msdos) filesystems, allowing timestamps to be in coordinated universal time (UTC) rather than local time in applications where doing this is advantageous. In particular, portable devices that use fat/vfat (such as digital cameras) can benefit from using UTC in their internal clocks, thus avoiding daylight saving time errors and general time ambiguity issues. The user of the device does not have to worry about changing the time when moving from place or when daylight saving changes. The new mount option, when set, disables the counter-adjustment that Linux currently makes to FAT timestamp info in anticipation of the normal userspace time zone correction. When used in this new mode, all daylight saving time and time zone handling is done in userspace as is normal for many other filesystems (like ext3). The default mode, which remains unchanged, is still appropriate when mounting volumes written in Windows (because of its use of local time). I originally based this patch on one submitted last year by Paul Collins, but I updated it to work with current source and changed variable/option naming. Ogawa Hirofumi (who maintains these filesystems) and I discussed this patch at length on lkml, and he suggested using the option name in the attached version of the patch. Barry Bouwsma pointed out a good addition to the patch as well. Signed-off-by: Joe Peterson <joe@skyrush.com> Signed-off-by: Paul Collins <paul@ondioline.org> Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Cc: Barry Bouwsma <free_beer_for_all@yahoo.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e8938a6 commit b271e06

6 files changed

Lines changed: 35 additions & 17 deletions

File tree

‎fs/fat/dir.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
11011101
goto error_free;
11021102
}
11031103

1104-
fat_date_unix2dos(ts->tv_sec, &time, &date);
1104+
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
11051105

11061106
de = (struct msdos_dir_entry *)bhs[0]->b_data;
11071107
/* filling the new directory slots ("." and ".." entries) */

‎fs/fat/inode.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,20 @@ static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
382382
inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
383383
& ~((loff_t)sbi->cluster_size - 1)) >> 9;
384384
inode->i_mtime.tv_sec =
385-
date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date));
385+
date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date),
386+
sbi->options.tz_utc);
386387
inode->i_mtime.tv_nsec = 0;
387388
if (sbi->options.isvfat) {
388389
int secs = de->ctime_cs / 100;
389390
int csecs = de->ctime_cs % 100;
390391
inode->i_ctime.tv_sec =
391392
date_dos2unix(le16_to_cpu(de->ctime),
392-
le16_to_cpu(de->cdate)) + secs;
393+
le16_to_cpu(de->cdate),
394+
sbi->options.tz_utc) + secs;
393395
inode->i_ctime.tv_nsec = csecs * 10000000;
394396
inode->i_atime.tv_sec =
395-
date_dos2unix(0, le16_to_cpu(de->adate));
397+
date_dos2unix(0, le16_to_cpu(de->adate),
398+
sbi->options.tz_utc);
396399
inode->i_atime.tv_nsec = 0;
397400
} else
398401
inode->i_ctime = inode->i_atime = inode->i_mtime;
@@ -591,11 +594,14 @@ static int fat_write_inode(struct inode *inode, int wait)
591594
raw_entry->attr = fat_attr(inode);
592595
raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
593596
raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
594-
fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date);
597+
fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time,
598+
&raw_entry->date, sbi->options.tz_utc);
595599
if (sbi->options.isvfat) {
596600
__le16 atime;
597-
fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
598-
fat_date_unix2dos(inode->i_atime.tv_sec,&atime,&raw_entry->adate);
601+
fat_date_unix2dos(inode->i_ctime.tv_sec, &raw_entry->ctime,
602+
&raw_entry->cdate, sbi->options.tz_utc);
603+
fat_date_unix2dos(inode->i_atime.tv_sec, &atime,
604+
&raw_entry->adate, sbi->options.tz_utc);
599605
raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 +
600606
inode->i_ctime.tv_nsec / 10000000;
601607
}
@@ -836,6 +842,8 @@ static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
836842
}
837843
if (sbi->options.flush)
838844
seq_puts(m, ",flush");
845+
if (opts->tz_utc)
846+
seq_puts(m, ",tz=UTC");
839847

840848
return 0;
841849
}
@@ -848,7 +856,7 @@ enum {
848856
Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
849857
Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
850858
Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
851-
Opt_obsolate, Opt_flush, Opt_err,
859+
Opt_obsolate, Opt_flush, Opt_tz_utc, Opt_err,
852860
};
853861

854862
static match_table_t fat_tokens = {
@@ -883,6 +891,7 @@ static match_table_t fat_tokens = {
883891
{Opt_obsolate, "cvf_options=%100s"},
884892
{Opt_obsolate, "posix"},
885893
{Opt_flush, "flush"},
894+
{Opt_tz_utc, "tz=UTC"},
886895
{Opt_err, NULL},
887896
};
888897
static match_table_t msdos_tokens = {
@@ -947,6 +956,7 @@ static int parse_options(char *options, int is_vfat, int silent, int *debug,
947956
opts->utf8 = opts->unicode_xlate = 0;
948957
opts->numtail = 1;
949958
opts->usefree = opts->nocase = 0;
959+
opts->tz_utc = 0;
950960
*debug = 0;
951961

952962
if (!options)
@@ -1036,6 +1046,9 @@ static int parse_options(char *options, int is_vfat, int silent, int *debug,
10361046
case Opt_flush:
10371047
opts->flush = 1;
10381048
break;
1049+
case Opt_tz_utc:
1050+
opts->tz_utc = 1;
1051+
break;
10391052

10401053
/* msdos specific */
10411054
case Opt_dots:

‎fs/fat/misc.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static int day_n[] = {
142142
};
143143

144144
/* 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)
145+
int date_dos2unix(unsigned short time, unsigned short date, int tz_utc)
146146
{
147147
int month, year, secs;
148148

@@ -156,16 +156,18 @@ int date_dos2unix(unsigned short time, unsigned short date)
156156
((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
157157
month < 2 ? 1 : 0)+3653);
158158
/* days since 1.1.70 plus 80's leap day */
159-
secs += sys_tz.tz_minuteswest*60;
159+
if (!tz_utc)
160+
secs += sys_tz.tz_minuteswest*60;
160161
return secs;
161162
}
162163

163164
/* Convert linear UNIX date to a MS-DOS time/date pair. */
164-
void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
165+
void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date, int tz_utc)
165166
{
166167
int day, year, nl_day, month;
167168

168-
unix_date -= sys_tz.tz_minuteswest*60;
169+
if (!tz_utc)
170+
unix_date -= sys_tz.tz_minuteswest*60;
169171

170172
/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
171173
if (unix_date < 315532800)

‎fs/msdos/namei.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ static int msdos_add_entry(struct inode *dir, const unsigned char *name,
237237
int is_dir, int is_hid, int cluster,
238238
struct timespec *ts, struct fat_slot_info *sinfo)
239239
{
240+
struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
240241
struct msdos_dir_entry de;
241242
__le16 time, date;
242243
int err;
@@ -246,7 +247,7 @@ static int msdos_add_entry(struct inode *dir, const unsigned char *name,
246247
if (is_hid)
247248
de.attr |= ATTR_HIDDEN;
248249
de.lcase = 0;
249-
fat_date_unix2dos(ts->tv_sec, &time, &date);
250+
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
250251
de.cdate = de.adate = 0;
251252
de.ctime = 0;
252253
de.ctime_cs = 0;

‎fs/vfat/namei.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ static int vfat_build_slots(struct inode *dir, const unsigned char *name,
621621
memcpy(de->name, msdos_name, MSDOS_NAME);
622622
de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
623623
de->lcase = lcase;
624-
fat_date_unix2dos(ts->tv_sec, &time, &date);
624+
fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
625625
de->time = de->ctime = time;
626626
de->date = de->cdate = de->adate = date;
627627
de->ctime_cs = 0;

‎include/linux/msdos_fs.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ struct fat_mount_options {
203203
numtail:1, /* Does first alias have a numeric '~1' type tail? */
204204
flush:1, /* write things quickly */
205205
nocase:1, /* Does this need case conversion? 0=need case conversion*/
206-
usefree:1; /* Use free_clusters for FAT32 */
206+
usefree:1, /* Use free_clusters for FAT32 */
207+
tz_utc:1; /* Filesystem timestamps are in UTC */
207208
};
208209

209210
#define FAT_HASH_BITS 8
@@ -434,8 +435,9 @@ extern int fat_flush_inodes(struct super_block *sb, struct inode *i1,
434435
extern void fat_fs_panic(struct super_block *s, const char *fmt, ...);
435436
extern void fat_clusters_flush(struct super_block *sb);
436437
extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster);
437-
extern int date_dos2unix(unsigned short time, unsigned short date);
438-
extern void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date);
438+
extern int date_dos2unix(unsigned short time, unsigned short date, int tz_utc);
439+
extern void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date,
440+
int tz_utc);
439441
extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs);
440442

441443
int fat_cache_init(void);

0 commit comments

Comments
 (0)