|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | #include "fat.h" |
| 10 | +#include <linux/iversion.h> |
10 | 11 |
|
11 | 12 | /* |
12 | 13 | * fat_fs_error reports a file system problem that might indicate fa data |
@@ -265,6 +266,80 @@ void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts, |
265 | 266 | } |
266 | 267 | EXPORT_SYMBOL_GPL(fat_time_unix2fat); |
267 | 268 |
|
| 269 | +static inline struct timespec64 fat_timespec64_trunc_2secs(struct timespec64 ts) |
| 270 | +{ |
| 271 | + return (struct timespec64){ ts.tv_sec & ~1ULL, 0 }; |
| 272 | +} |
| 273 | +/* |
| 274 | + * truncate the various times with appropriate granularity: |
| 275 | + * root inode: |
| 276 | + * all times always 0 |
| 277 | + * all other inodes: |
| 278 | + * mtime - 2 seconds |
| 279 | + * ctime |
| 280 | + * msdos - 2 seconds |
| 281 | + * vfat - 10 milliseconds |
| 282 | + * atime - 24 hours (00:00:00 in local timezone) |
| 283 | + */ |
| 284 | +int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags) |
| 285 | +{ |
| 286 | + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 287 | + struct timespec64 ts; |
| 288 | + |
| 289 | + if (inode->i_ino == MSDOS_ROOT_INO) |
| 290 | + return 0; |
| 291 | + |
| 292 | + if (now == NULL) { |
| 293 | + now = &ts; |
| 294 | + ts = current_time(inode); |
| 295 | + } |
| 296 | + |
| 297 | + if (flags & S_ATIME) { |
| 298 | + /* to localtime */ |
| 299 | + time64_t seconds = now->tv_sec - fat_tz_offset(sbi); |
| 300 | + s32 remainder; |
| 301 | + |
| 302 | + div_s64_rem(seconds, SECS_PER_DAY, &remainder); |
| 303 | + /* to day boundary, and back to unix time */ |
| 304 | + seconds = seconds + fat_tz_offset(sbi) - remainder; |
| 305 | + |
| 306 | + inode->i_atime = (struct timespec64){ seconds, 0 }; |
| 307 | + } |
| 308 | + if (flags & S_CTIME) { |
| 309 | + if (sbi->options.isvfat) |
| 310 | + inode->i_ctime = timespec64_trunc(*now, 10000000); |
| 311 | + else |
| 312 | + inode->i_ctime = fat_timespec64_trunc_2secs(*now); |
| 313 | + } |
| 314 | + if (flags & S_MTIME) |
| 315 | + inode->i_mtime = fat_timespec64_trunc_2secs(*now); |
| 316 | + |
| 317 | + return 0; |
| 318 | +} |
| 319 | +EXPORT_SYMBOL_GPL(fat_truncate_time); |
| 320 | + |
| 321 | +int fat_update_time(struct inode *inode, struct timespec64 *now, int flags) |
| 322 | +{ |
| 323 | + int iflags = I_DIRTY_TIME; |
| 324 | + bool dirty = false; |
| 325 | + |
| 326 | + if (inode->i_ino == MSDOS_ROOT_INO) |
| 327 | + return 0; |
| 328 | + |
| 329 | + fat_truncate_time(inode, now, flags); |
| 330 | + if (flags & S_VERSION) |
| 331 | + dirty = inode_maybe_inc_iversion(inode, false); |
| 332 | + if ((flags & (S_ATIME | S_CTIME | S_MTIME)) && |
| 333 | + !(inode->i_sb->s_flags & SB_LAZYTIME)) |
| 334 | + dirty = true; |
| 335 | + |
| 336 | + if (dirty) |
| 337 | + iflags |= I_DIRTY_SYNC; |
| 338 | + __mark_inode_dirty(inode, iflags); |
| 339 | + return 0; |
| 340 | +} |
| 341 | +EXPORT_SYMBOL_GPL(fat_update_time); |
| 342 | + |
268 | 343 | int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) |
269 | 344 | { |
270 | 345 | int i, err = 0; |
|
0 commit comments