misc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/fat/misc.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  7. * and date_dos2unix for date==0 by Igor Zhbanov([email protected])
  8. */
  9. #include "fat.h"
  10. #include <linux/iversion.h>
  11. /*
  12. * fat_fs_error reports a file system problem that might indicate fa data
  13. * corruption/inconsistency. Depending on 'errors' mount option the
  14. * panic() is called, or error message is printed FAT and nothing is done,
  15. * or filesystem is remounted read-only (default behavior).
  16. * In case the file system is remounted read-only, it can be made writable
  17. * again by remounting it.
  18. */
  19. void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
  20. {
  21. struct fat_mount_options *opts = &MSDOS_SB(sb)->options;
  22. va_list args;
  23. struct va_format vaf;
  24. if (report) {
  25. va_start(args, fmt);
  26. vaf.fmt = fmt;
  27. vaf.va = &args;
  28. fat_msg(sb, KERN_ERR, "error, %pV", &vaf);
  29. va_end(args);
  30. }
  31. if (opts->errors == FAT_ERRORS_PANIC)
  32. panic("FAT-fs (%s): fs panic from previous error\n", sb->s_id);
  33. else if (opts->errors == FAT_ERRORS_RO && !sb_rdonly(sb)) {
  34. sb->s_flags |= SB_RDONLY;
  35. fat_msg(sb, KERN_ERR, "Filesystem has been set read-only");
  36. }
  37. }
  38. EXPORT_SYMBOL_GPL(__fat_fs_error);
  39. /**
  40. * _fat_msg() - Print a preformatted FAT message based on a superblock.
  41. * @sb: A pointer to a &struct super_block
  42. * @level: A Kernel printk level constant
  43. * @fmt: The printf-style format string to print.
  44. *
  45. * Everything that is not fat_fs_error() should be fat_msg().
  46. *
  47. * fat_msg() wraps _fat_msg() for printk indexing.
  48. */
  49. void _fat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  50. {
  51. struct va_format vaf;
  52. va_list args;
  53. va_start(args, fmt);
  54. vaf.fmt = fmt;
  55. vaf.va = &args;
  56. _printk(FAT_PRINTK_PREFIX "%pV\n", level, sb->s_id, &vaf);
  57. va_end(args);
  58. }
  59. /* Flushes the number of free clusters on FAT32 */
  60. /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
  61. int fat_clusters_flush(struct super_block *sb)
  62. {
  63. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  64. struct buffer_head *bh;
  65. struct fat_boot_fsinfo *fsinfo;
  66. if (!is_fat32(sbi))
  67. return 0;
  68. bh = sb_bread(sb, sbi->fsinfo_sector);
  69. if (bh == NULL) {
  70. fat_msg(sb, KERN_ERR, "bread failed in fat_clusters_flush");
  71. return -EIO;
  72. }
  73. fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
  74. /* Sanity check */
  75. if (!IS_FSINFO(fsinfo)) {
  76. fat_msg(sb, KERN_ERR, "Invalid FSINFO signature: "
  77. "0x%08x, 0x%08x (sector = %lu)",
  78. le32_to_cpu(fsinfo->signature1),
  79. le32_to_cpu(fsinfo->signature2),
  80. sbi->fsinfo_sector);
  81. } else {
  82. if (sbi->free_clusters != -1)
  83. fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
  84. if (sbi->prev_free != -1)
  85. fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
  86. mark_buffer_dirty(bh);
  87. }
  88. brelse(bh);
  89. return 0;
  90. }
  91. /*
  92. * fat_chain_add() adds a new cluster to the chain of clusters represented
  93. * by inode.
  94. */
  95. int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
  96. {
  97. struct super_block *sb = inode->i_sb;
  98. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  99. int ret, new_fclus, last;
  100. /*
  101. * We must locate the last cluster of the file to add this new
  102. * one (new_dclus) to the end of the link list (the FAT).
  103. */
  104. last = new_fclus = 0;
  105. if (MSDOS_I(inode)->i_start) {
  106. int fclus, dclus;
  107. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  108. if (ret < 0)
  109. return ret;
  110. new_fclus = fclus + 1;
  111. last = dclus;
  112. }
  113. /* add new one to the last of the cluster chain */
  114. if (last) {
  115. struct fat_entry fatent;
  116. fatent_init(&fatent);
  117. ret = fat_ent_read(inode, &fatent, last);
  118. if (ret >= 0) {
  119. int wait = inode_needs_sync(inode);
  120. ret = fat_ent_write(inode, &fatent, new_dclus, wait);
  121. fatent_brelse(&fatent);
  122. }
  123. if (ret < 0)
  124. return ret;
  125. /*
  126. * FIXME:Although we can add this cache, fat_cache_add() is
  127. * assuming to be called after linear search with fat_cache_id.
  128. */
  129. // fat_cache_add(inode, new_fclus, new_dclus);
  130. } else {
  131. MSDOS_I(inode)->i_start = new_dclus;
  132. MSDOS_I(inode)->i_logstart = new_dclus;
  133. /*
  134. * Since generic_write_sync() synchronizes regular files later,
  135. * we sync here only directories.
  136. */
  137. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  138. ret = fat_sync_inode(inode);
  139. if (ret)
  140. return ret;
  141. } else
  142. mark_inode_dirty(inode);
  143. }
  144. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  145. fat_fs_error(sb, "clusters badly computed (%d != %llu)",
  146. new_fclus,
  147. (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
  148. fat_cache_inval_inode(inode);
  149. }
  150. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  151. return 0;
  152. }
  153. /*
  154. * The epoch of FAT timestamp is 1980.
  155. * : bits : value
  156. * date: 0 - 4: day (1 - 31)
  157. * date: 5 - 8: month (1 - 12)
  158. * date: 9 - 15: year (0 - 127) from 1980
  159. * time: 0 - 4: sec (0 - 29) 2sec counts
  160. * time: 5 - 10: min (0 - 59)
  161. * time: 11 - 15: hour (0 - 23)
  162. */
  163. #define SECS_PER_MIN 60
  164. #define SECS_PER_HOUR (60 * 60)
  165. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  166. /* days between 1.1.70 and 1.1.80 (2 leap days) */
  167. #define DAYS_DELTA (365 * 10 + 2)
  168. /* 120 (2100 - 1980) isn't leap year */
  169. #define YEAR_2100 120
  170. #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
  171. /* Linear day numbers of the respective 1sts in non-leap years. */
  172. static long days_in_year[] = {
  173. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  174. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
  175. };
  176. static inline int fat_tz_offset(const struct msdos_sb_info *sbi)
  177. {
  178. return (sbi->options.tz_set ?
  179. -sbi->options.time_offset :
  180. sys_tz.tz_minuteswest) * SECS_PER_MIN;
  181. }
  182. /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
  183. void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
  184. __le16 __time, __le16 __date, u8 time_cs)
  185. {
  186. u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
  187. time64_t second;
  188. long day, leap_day, month, year;
  189. year = date >> 9;
  190. month = max(1, (date >> 5) & 0xf);
  191. day = max(1, date & 0x1f) - 1;
  192. leap_day = (year + 3) / 4;
  193. if (year > YEAR_2100) /* 2100 isn't leap year */
  194. leap_day--;
  195. if (IS_LEAP_YEAR(year) && month > 2)
  196. leap_day++;
  197. second = (time & 0x1f) << 1;
  198. second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
  199. second += (time >> 11) * SECS_PER_HOUR;
  200. second += (time64_t)(year * 365 + leap_day
  201. + days_in_year[month] + day
  202. + DAYS_DELTA) * SECS_PER_DAY;
  203. second += fat_tz_offset(sbi);
  204. if (time_cs) {
  205. ts->tv_sec = second + (time_cs / 100);
  206. ts->tv_nsec = (time_cs % 100) * 10000000;
  207. } else {
  208. ts->tv_sec = second;
  209. ts->tv_nsec = 0;
  210. }
  211. }
  212. /* Export fat_time_fat2unix() for the fat_test KUnit tests. */
  213. EXPORT_SYMBOL_GPL(fat_time_fat2unix);
  214. /* Convert linear UNIX date to a FAT time/date pair. */
  215. void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
  216. __le16 *time, __le16 *date, u8 *time_cs)
  217. {
  218. struct tm tm;
  219. time64_to_tm(ts->tv_sec, -fat_tz_offset(sbi), &tm);
  220. /* FAT can only support year between 1980 to 2107 */
  221. if (tm.tm_year < 1980 - 1900) {
  222. *time = 0;
  223. *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
  224. if (time_cs)
  225. *time_cs = 0;
  226. return;
  227. }
  228. if (tm.tm_year > 2107 - 1900) {
  229. *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
  230. *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
  231. if (time_cs)
  232. *time_cs = 199;
  233. return;
  234. }
  235. /* from 1900 -> from 1980 */
  236. tm.tm_year -= 80;
  237. /* 0~11 -> 1~12 */
  238. tm.tm_mon++;
  239. /* 0~59 -> 0~29(2sec counts) */
  240. tm.tm_sec >>= 1;
  241. *time = cpu_to_le16(tm.tm_hour << 11 | tm.tm_min << 5 | tm.tm_sec);
  242. *date = cpu_to_le16(tm.tm_year << 9 | tm.tm_mon << 5 | tm.tm_mday);
  243. if (time_cs)
  244. *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
  245. }
  246. EXPORT_SYMBOL_GPL(fat_time_unix2fat);
  247. static inline struct timespec64 fat_timespec64_trunc_2secs(struct timespec64 ts)
  248. {
  249. return (struct timespec64){ ts.tv_sec & ~1ULL, 0 };
  250. }
  251. /*
  252. * truncate atime to 24 hour granularity (00:00:00 in local timezone)
  253. */
  254. struct timespec64 fat_truncate_atime(const struct msdos_sb_info *sbi,
  255. const struct timespec64 *ts)
  256. {
  257. /* to localtime */
  258. time64_t seconds = ts->tv_sec - fat_tz_offset(sbi);
  259. s32 remainder;
  260. div_s64_rem(seconds, SECS_PER_DAY, &remainder);
  261. /* to day boundary, and back to unix time */
  262. seconds = seconds + fat_tz_offset(sbi) - remainder;
  263. return (struct timespec64){ seconds, 0 };
  264. }
  265. /*
  266. * truncate mtime to 2 second granularity
  267. */
  268. struct timespec64 fat_truncate_mtime(const struct msdos_sb_info *sbi,
  269. const struct timespec64 *ts)
  270. {
  271. return fat_timespec64_trunc_2secs(*ts);
  272. }
  273. /*
  274. * truncate the various times with appropriate granularity:
  275. * all times in root node are always 0
  276. */
  277. int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags)
  278. {
  279. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  280. struct timespec64 ts;
  281. if (inode->i_ino == MSDOS_ROOT_INO)
  282. return 0;
  283. if (now == NULL) {
  284. now = &ts;
  285. ts = current_time(inode);
  286. }
  287. if (flags & S_ATIME)
  288. inode->i_atime = fat_truncate_atime(sbi, now);
  289. /*
  290. * ctime and mtime share the same on-disk field, and should be
  291. * identical in memory. all mtime updates will be applied to ctime,
  292. * but ctime updates are ignored.
  293. */
  294. if (flags & S_MTIME)
  295. inode->i_mtime = inode->i_ctime = fat_truncate_mtime(sbi, now);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(fat_truncate_time);
  299. int fat_update_time(struct inode *inode, struct timespec64 *now, int flags)
  300. {
  301. int dirty_flags = 0;
  302. if (inode->i_ino == MSDOS_ROOT_INO)
  303. return 0;
  304. if (flags & (S_ATIME | S_CTIME | S_MTIME)) {
  305. fat_truncate_time(inode, now, flags);
  306. if (inode->i_sb->s_flags & SB_LAZYTIME)
  307. dirty_flags |= I_DIRTY_TIME;
  308. else
  309. dirty_flags |= I_DIRTY_SYNC;
  310. }
  311. if ((flags & S_VERSION) && inode_maybe_inc_iversion(inode, false))
  312. dirty_flags |= I_DIRTY_SYNC;
  313. __mark_inode_dirty(inode, dirty_flags);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(fat_update_time);
  317. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  318. {
  319. int i, err = 0;
  320. for (i = 0; i < nr_bhs; i++)
  321. write_dirty_buffer(bhs[i], 0);
  322. for (i = 0; i < nr_bhs; i++) {
  323. wait_on_buffer(bhs[i]);
  324. if (!err && !buffer_uptodate(bhs[i]))
  325. err = -EIO;
  326. }
  327. return err;
  328. }