Add time_offset=<UTC offset> to mount arguments

Add time_offset=<UTC offset> to mount arguments for the vfat driver.

This is not being release flagged as it's a fix for a regression but is
a cosmetic fix that shouldn't affect anything besides reported file
timestamps.

Changes for issue 246256335 in Android U stopped Android syncing the
current time zone UTC offset to the kernel because doing so is
discouraged. It is discouraged because the current offset alone is not
very useful - it tells the kernel nothing of DST or historic UTC
offsets. Converting to and from local times are are best left to
userspace where time zone rules information is available, and different
users can use different time zones.

However, because FAT32 is poorly designed WRT timestamps, the kernel
FAT32 driver, vfat, does use the kernel offset when available and when
it isn't given a fixed offset to use at volume mount time. This means
that Android devices after the change from issue 246256335 displayed
more obviously incorrect times.

This change adds the argument necessary to vold when mounting a FAT32
volume to set a fixed UTC offset to adjust FAT32 local times
to a UTC-like time ("UTC time" from now on). Userspace then uses the UTC
offset for that UTC time, calculated using TZDB rules, to convert back
to a local time. This is still prone to generating some incorrect times,
e.g. due to DST or other historic offset changes, or a user time zone
change on device after mounting the volume. FAT32 lacks the information
about "what was the UTC offset at file time X?" (unlike exFAT) AND the
vfat driver has no way to look up the time zone rules itself. This
change is a reasonable "better than nothing" change to address times
being obviously wrong after the change from issue 246256335, especially
when a user copies a file from a desktop computer to USB / sd card
storage and immediately plugs the device into an Android device. It does
this without reverting to kernel UTC offset syncing, which is flawed
(i.e. it would never work completely), discouraged, and more effort/code
to improve, e.g. because userspace would have to schedule alarms for
offset changes.

Testing:

1) Obtain a USB FAT32 formatted USB storage device that can be plugged into
   a pixel device, e.g. with an OTG USB adapter.
2) On a desktop computer, mount the device and write some files / note
   times associated with existing files. These times will already be
   adjusted by this OS to be "local time" based on its own logic, but if
   it's working correctly that time will be exactly the local time value
   stored in the FAT32 volume itself.
3) On a rooted Android device where you can use adb via Wifi (adb tcpip
   / adb connect), leaving the USB port free for external USB devices....
   a) $ adb root
   b) Insert the USB storage
   c) $ mount | grep 'fat'
   d) For the USB storage drive, observe the time_offset argument (or
   tz=UTC when time_offset == 0) reported (this would not be reported
   without this patch)
   e) ls -l /mnt/<mount location from (3c)>
   f) Confirm the local time displayed is as expected. e.g. the time
   should be the same as shown in (2), regardless of the device's time
   zone.
4) To observe the "fixed offset behavior" at mount time, alter the time
   zone setting on the device via Settings -> System -> Date & Time
   a) Repeat 3c-3e.
   b) The times shown will have changed by the difference between
   the original and new time zone chosen.
   c) Extract / re-insert the USB storage device.
   d) Repeat 3c-3e
   e) The times shown should match the times from (2) again
5) Confirm the write behavior:
   a) $ touch /mnt/<mount location from (3c)>/foobar
   b) $ ls -l /mnt/<mount location from (3c)>
   c) The time should match the device's displayed local time (status
   bar)
   d) Unmount the USB device and insert the USB device into a desktop computer
   e) Confirm the timestamp matches the Android device's local time when
   (5a) took place, e.g. using "ls -lT" on MacOS.

Testing was done with numerous zones with positive, negative and zero offsets.
Interesting zones like India (UTC+5:30), Kiribati (UTC+14), Wake Island
(UTC-11), the various fixed offset zones like Etc/GMT+12, Etc/GMT-14
were tried.

Note: Depending on the time zones being used on devices (Android and
desktop) and when the files were written / testing took place during the
year, you may see file times shifting by 1 hour from the "ls -l" step
depending on whether they were written in summer or winter time.  This
is because the userspace code for rendering times knows about DST but
the kernel driver is applying a fixed offset and does not.  This is
expected and illustrates the points at the top of this comment about
FAT32 integration never being perfect.
See https://www.google.com/search?q=fat32+dst for other examples.

Bug: 319417938
Bug: 315058275
Bug: 246256335
Test: See above
Change-Id: Ic7ce159d88db5d5cf5894bcc26ea60bd7c44917d
diff --git a/fs/Vfat.cpp b/fs/Vfat.cpp
index f3f04d8..c0cd918 100644
--- a/fs/Vfat.cpp
+++ b/fs/Vfat.cpp
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <linux/fs.h>
@@ -118,6 +119,16 @@
     return 0;
 }
 
+int16_t currentUtcOffsetMinutes() {
+    time_t now = time(NULL);
+
+    struct tm nowTm;
+    localtime_r(&now, &nowTm);
+
+    int32_t utcOffsetSeconds = nowTm.tm_gmtoff;
+    return (int16_t)(utcOffsetSeconds / 60);
+}
+
 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
                bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
     int rc;
@@ -136,6 +147,33 @@
         android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
                                     ownerUid, ownerGid, permMask, permMask);
 
+    // b/315058275: Set this to false if you don't want to use a fixed offset
+    // determined at mount time. When this is false, the vfat driver will fall
+    // back to using sys_tz, which Android does not set by default, then assume
+    // local time == UTC.
+    if (true) {
+        // Calculate the offset to use to adjust FAT timestamps to convert them
+        // from "local time" into unix epoch time. This assumes the current UTC
+        // offset of this device is the same as the device that wrote them. User
+        // space code, e.g. ls -l, will then apply the UTC offset for the UTC
+        // time to convert times from unix epoch time to local time for display.
+        // Before Android U (b/246256335), Android platform code informed the
+        // Linux kernel about the UTC offset under some circumstances, but not
+        // for all, e.g. DST changes. The kernel vfat driver is one of the few
+        // things in the kernel that tries to use kernel UTC offset information.
+        // Setting time zone offset in the Linux kernel is discouraged and so
+        // Android no longer informs the kernel. Instead, the offset for vfat
+        // to use is now set at volume mount time. This means that if the time
+        // zone offset changes while the device is mounted, or if files were
+        // written in opposing daylight saving time, then incorrect file times
+        // will be displayed until the volume is remounted. Even then, the vfat
+        // driver has to assume a fixed offset to apply to all files, so files
+        // written at different times of the year can have incorrect times
+        // calculated, e.g. offset incorrectly by one hour.
+        int16_t timeOffsetArg = currentUtcOffsetMinutes();
+        mountData += android::base::StringPrintf(",time_offset=%d", timeOffsetArg);
+    }
+
     rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
 
     if (rc && errno == EROFS) {