misc.c 5.3 KB

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