misc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Written 1992,1993 by Werner Almesberger
  4. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  5. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  6. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  7. */
  8. #include <linux/time.h>
  9. #include <linux/fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/blk_types.h>
  13. #include "exfat_raw.h"
  14. #include "exfat_fs.h"
  15. /*
  16. * exfat_fs_error reports a file system problem that might indicate fa data
  17. * corruption/inconsistency. Depending on 'errors' mount option the
  18. * panic() is called, or error message is printed FAT and nothing is done,
  19. * or filesystem is remounted read-only (default behavior).
  20. * In case the file system is remounted read-only, it can be made writable
  21. * again by remounting it.
  22. */
  23. void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
  24. {
  25. struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
  26. va_list args;
  27. struct va_format vaf;
  28. if (report) {
  29. va_start(args, fmt);
  30. vaf.fmt = fmt;
  31. vaf.va = &args;
  32. exfat_err(sb, "error, %pV", &vaf);
  33. va_end(args);
  34. }
  35. if (opts->errors == EXFAT_ERRORS_PANIC) {
  36. panic("exFAT-fs (%s): fs panic from previous error\n",
  37. sb->s_id);
  38. } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) {
  39. sb->s_flags |= SB_RDONLY;
  40. exfat_err(sb, "Filesystem has been set read-only");
  41. }
  42. }
  43. #define SECS_PER_MIN (60)
  44. #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN)
  45. static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off)
  46. {
  47. if (tz_off <= 0x3F)
  48. ts->tv_sec -= TIMEZONE_SEC(tz_off);
  49. else /* 0x40 <= (tz_off & 0x7F) <=0x7F */
  50. ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off);
  51. }
  52. static inline int exfat_tz_offset(struct exfat_sb_info *sbi)
  53. {
  54. if (sbi->options.sys_tz)
  55. return -sys_tz.tz_minuteswest;
  56. return sbi->options.time_offset;
  57. }
  58. /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */
  59. void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
  60. u8 tz, __le16 time, __le16 date, u8 time_cs)
  61. {
  62. u16 t = le16_to_cpu(time);
  63. u16 d = le16_to_cpu(date);
  64. ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F,
  65. t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1);
  66. /* time_cs field represent 0 ~ 199cs(1990 ms) */
  67. if (time_cs) {
  68. ts->tv_sec += time_cs / 100;
  69. ts->tv_nsec = (time_cs % 100) * 10 * NSEC_PER_MSEC;
  70. } else
  71. ts->tv_nsec = 0;
  72. if (tz & EXFAT_TZ_VALID)
  73. /* Adjust timezone to UTC0. */
  74. exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID);
  75. else
  76. ts->tv_sec -= exfat_tz_offset(sbi) * SECS_PER_MIN;
  77. }
  78. /* Convert linear UNIX date to a EXFAT time/date pair. */
  79. void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
  80. u8 *tz, __le16 *time, __le16 *date, u8 *time_cs)
  81. {
  82. struct tm tm;
  83. u16 t, d;
  84. time64_to_tm(ts->tv_sec, 0, &tm);
  85. t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1);
  86. d = ((tm.tm_year - 80) << 9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday;
  87. *time = cpu_to_le16(t);
  88. *date = cpu_to_le16(d);
  89. /* time_cs field represent 0 ~ 199cs(1990 ms) */
  90. if (time_cs)
  91. *time_cs = (tm.tm_sec & 1) * 100 +
  92. ts->tv_nsec / (10 * NSEC_PER_MSEC);
  93. /*
  94. * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid
  95. * to indicate that local time and UTC are the same.
  96. */
  97. *tz = EXFAT_TZ_VALID;
  98. }
  99. /*
  100. * The timestamp for access_time has double seconds granularity.
  101. * (There is no 10msIncrement field for access_time unlike create/modify_time)
  102. * atime also has only a 2-second resolution.
  103. */
  104. void exfat_truncate_atime(struct timespec64 *ts)
  105. {
  106. ts->tv_sec = round_down(ts->tv_sec, 2);
  107. ts->tv_nsec = 0;
  108. }
  109. void exfat_truncate_inode_atime(struct inode *inode)
  110. {
  111. struct timespec64 atime = inode_get_atime(inode);
  112. exfat_truncate_atime(&atime);
  113. inode_set_atime_to_ts(inode, atime);
  114. }
  115. u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
  116. {
  117. int i;
  118. u8 *c = (u8 *)data;
  119. for (i = 0; i < len; i++, c++) {
  120. if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
  121. continue;
  122. chksum = ((chksum << 15) | (chksum >> 1)) + *c;
  123. }
  124. return chksum;
  125. }
  126. u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)
  127. {
  128. int i;
  129. u8 *c = (u8 *)data;
  130. for (i = 0; i < len; i++, c++) {
  131. if (unlikely(type == CS_BOOT_SECTOR &&
  132. (i == 106 || i == 107 || i == 112)))
  133. continue;
  134. chksum = ((chksum << 31) | (chksum >> 1)) + *c;
  135. }
  136. return chksum;
  137. }
  138. void exfat_update_bh(struct buffer_head *bh, int sync)
  139. {
  140. set_buffer_uptodate(bh);
  141. mark_buffer_dirty(bh);
  142. if (sync)
  143. sync_dirty_buffer(bh);
  144. }
  145. int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync)
  146. {
  147. int i, err = 0;
  148. for (i = 0; i < nr_bhs; i++) {
  149. set_buffer_uptodate(bhs[i]);
  150. mark_buffer_dirty(bhs[i]);
  151. if (sync)
  152. write_dirty_buffer(bhs[i], REQ_SYNC);
  153. }
  154. for (i = 0; i < nr_bhs && sync; i++) {
  155. wait_on_buffer(bhs[i]);
  156. if (!err && !buffer_uptodate(bhs[i]))
  157. err = -EIO;
  158. }
  159. return err;
  160. }
  161. void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
  162. unsigned int size, unsigned char flags)
  163. {
  164. ec->dir = dir;
  165. ec->size = size;
  166. ec->flags = flags;
  167. }
  168. void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec)
  169. {
  170. return exfat_chain_set(dup, ec->dir, ec->size, ec->flags);
  171. }