misc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * linux/fs/fat/misc.c
  19. *
  20. * Written 1992,1993 by Werner Almesberger
  21. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  22. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  23. */
  24. /************************************************************************/
  25. /* */
  26. /* PROJECT : exFAT & FAT12/16/32 File System */
  27. /* FILE : misc.c */
  28. /* PURPOSE : Helper function for checksum and handing sdFAT error */
  29. /* */
  30. /*----------------------------------------------------------------------*/
  31. /* NOTES */
  32. /* */
  33. /* */
  34. /************************************************************************/
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/buffer_head.h>
  38. #include <linux/time.h>
  39. #include "sdfat.h"
  40. #include "version.h"
  41. #ifdef CONFIG_SDFAT_SUPPORT_STLOG
  42. #ifdef CONFIG_PROC_FSLOG
  43. #include <linux/fslog.h>
  44. #else
  45. #include <linux/stlog.h>
  46. #endif
  47. #else
  48. #define ST_LOG(fmt, ...)
  49. #endif
  50. /*************************************************************************
  51. * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  52. *************************************************************************/
  53. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
  54. #define CURRENT_TIME_SEC timespec64_trunc(current_kernel_time64(), NSEC_PER_SEC)
  55. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  56. #define CURRENT_TIME_SEC timespec_trunc(current_kernel_time(), NSEC_PER_SEC)
  57. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0) */
  58. /* EMPTY */
  59. #endif
  60. #ifdef CONFIG_SDFAT_UEVENT
  61. static struct kobject sdfat_uevent_kobj;
  62. int sdfat_uevent_init(struct kset *sdfat_kset)
  63. {
  64. int err;
  65. struct kobj_type *ktype = get_ktype(&sdfat_kset->kobj);
  66. sdfat_uevent_kobj.kset = sdfat_kset;
  67. err = kobject_init_and_add(&sdfat_uevent_kobj, ktype, NULL, "uevent");
  68. if (err)
  69. pr_err("[SDFAT] Unable to create sdfat uevent kobj\n");
  70. return err;
  71. }
  72. void sdfat_uevent_uninit(void)
  73. {
  74. kobject_del(&sdfat_uevent_kobj);
  75. memset(&sdfat_uevent_kobj, 0, sizeof(struct kobject));
  76. }
  77. void sdfat_uevent_ro_remount(struct super_block *sb)
  78. {
  79. struct block_device *bdev = sb->s_bdev;
  80. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  81. char major[16], minor[16];
  82. char *envp[] = { major, minor, NULL };
  83. snprintf(major, sizeof(major), "MAJOR=%d", MAJOR(bd_dev));
  84. snprintf(minor, sizeof(minor), "MINOR=%d", MINOR(bd_dev));
  85. kobject_uevent_env(&sdfat_uevent_kobj, KOBJ_CHANGE, envp);
  86. ST_LOG("[SDFAT](%s[%d:%d]): Uevent triggered\n",
  87. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  88. }
  89. #endif
  90. /*
  91. * sdfat_fs_error reports a file system problem that might indicate fa data
  92. * corruption/inconsistency. Depending on 'errors' mount option the
  93. * panic() is called, or error message is printed FAT and nothing is done,
  94. * or filesystem is remounted read-only (default behavior).
  95. * In case the file system is remounted read-only, it can be made writable
  96. * again by remounting it.
  97. */
  98. void __sdfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
  99. {
  100. struct sdfat_mount_options *opts = &SDFAT_SB(sb)->options;
  101. va_list args;
  102. struct va_format vaf;
  103. struct block_device *bdev = sb->s_bdev;
  104. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  105. if (report) {
  106. va_start(args, fmt);
  107. vaf.fmt = fmt;
  108. vaf.va = &args;
  109. pr_err("[SDFAT](%s[%d:%d]):ERR: %pV\n",
  110. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  111. #ifdef CONFIG_SDFAT_SUPPORT_STLOG
  112. if (opts->errors == SDFAT_ERRORS_RO && !(sb->s_flags & MS_RDONLY)) {
  113. ST_LOG("[SDFAT](%s[%d:%d]):ERR: %pV\n",
  114. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  115. }
  116. #endif
  117. va_end(args);
  118. }
  119. if (opts->errors == SDFAT_ERRORS_PANIC) {
  120. panic("[SDFAT](%s[%d:%d]): fs panic from previous error\n",
  121. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  122. } else if (opts->errors == SDFAT_ERRORS_RO && !(sb->s_flags & MS_RDONLY)) {
  123. sb->s_flags |= MS_RDONLY;
  124. sdfat_statistics_set_mnt_ro();
  125. pr_err("[SDFAT](%s[%d:%d]): Filesystem has been set "
  126. "read-only\n", sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  127. #ifdef CONFIG_SDFAT_SUPPORT_STLOG
  128. ST_LOG("[SDFAT](%s[%d:%d]): Filesystem has been set read-only\n",
  129. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  130. #endif
  131. sdfat_uevent_ro_remount(sb);
  132. }
  133. }
  134. EXPORT_SYMBOL(__sdfat_fs_error);
  135. /**
  136. * __sdfat_msg() - print preformated SDFAT specific messages.
  137. * All logs except what uses sdfat_fs_error() should be written by __sdfat_msg()
  138. * If 'st' is set, the log is propagated to ST_LOG.
  139. */
  140. void __sdfat_msg(struct super_block *sb, const char *level, int st, const char *fmt, ...)
  141. {
  142. struct va_format vaf;
  143. va_list args;
  144. struct block_device *bdev = sb->s_bdev;
  145. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  146. va_start(args, fmt);
  147. vaf.fmt = fmt;
  148. vaf.va = &args;
  149. /* level means KERN_ pacility level */
  150. printk("%s[SDFAT](%s[%d:%d]): %pV\n", level,
  151. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  152. #ifdef CONFIG_SDFAT_SUPPORT_STLOG
  153. if (st) {
  154. ST_LOG("[SDFAT](%s[%d:%d]): %pV\n",
  155. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  156. }
  157. #endif
  158. va_end(args);
  159. }
  160. EXPORT_SYMBOL(__sdfat_msg);
  161. void sdfat_log_version(void)
  162. {
  163. pr_info("[SDFAT] Filesystem version %s\n", SDFAT_VERSION);
  164. #ifdef CONFIG_SDFAT_SUPPORT_STLOG
  165. ST_LOG("[SDFAT] Filesystem version %s\n", SDFAT_VERSION);
  166. #endif
  167. }
  168. EXPORT_SYMBOL(sdfat_log_version);
  169. /* <linux/time.h> externs sys_tz
  170. * extern struct timezone sys_tz;
  171. */
  172. #define UNIX_SECS_1980 315532800L
  173. #if BITS_PER_LONG == 64
  174. #define UNIX_SECS_2108 4354819200L
  175. #endif
  176. /* days between 1970/01/01 and 1980/01/01 (2 leap days) */
  177. #define DAYS_DELTA_DECADE (365 * 10 + 2)
  178. /* 120 (2100 - 1980) isn't leap year */
  179. #define NO_LEAP_YEAR_2100 (120)
  180. #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
  181. #define SECS_PER_MIN (60)
  182. #define SECS_PER_HOUR (60 * SECS_PER_MIN)
  183. #define SECS_PER_DAY (24 * SECS_PER_HOUR)
  184. #define MAKE_LEAP_YEAR(leap_year, year) \
  185. do { \
  186. /* 2100 isn't leap year */ \
  187. if (unlikely(year > NO_LEAP_YEAR_2100)) \
  188. leap_year = ((year + 3) / 4) - 1; \
  189. else \
  190. leap_year = ((year + 3) / 4); \
  191. } while (0)
  192. /* Linear day numbers of the respective 1sts in non-leap years. */
  193. static time_t accum_days_in_year[] = {
  194. /* Month : N 01 02 03 04 05 06 07 08 09 10 11 12 */
  195. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
  196. };
  197. #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN)
  198. /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
  199. void sdfat_time_fat2unix(struct sdfat_sb_info *sbi, sdfat_timespec_t *ts,
  200. DATE_TIME_T *tp)
  201. {
  202. time_t year = tp->Year;
  203. time_t ld; /* leap day */
  204. MAKE_LEAP_YEAR(ld, year);
  205. if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
  206. ld++;
  207. ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
  208. + tp->Hour * SECS_PER_HOUR
  209. + (year * 365 + ld + accum_days_in_year[tp->Month]
  210. + (tp->Day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY;
  211. ts->tv_nsec = 0;
  212. /* Treat as local time */
  213. if (!sbi->options.tz_utc && !tp->Timezone.valid) {
  214. ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN;
  215. return;
  216. }
  217. /* Treat as UTC time */
  218. if (!tp->Timezone.valid)
  219. return;
  220. /* Treat as UTC time, but need to adjust timezone to UTC0 */
  221. if (tp->Timezone.off <= 0x3F)
  222. ts->tv_sec -= TIMEZONE_SEC(tp->Timezone.off);
  223. else /* 0x40 <= (tp->Timezone & 0x7F) <=0x7F */
  224. ts->tv_sec += TIMEZONE_SEC(0x80 - tp->Timezone.off);
  225. }
  226. #define TIMEZONE_CUR_OFFSET() ((sys_tz.tz_minuteswest / (-15)) & 0x7F)
  227. /* Convert linear UNIX date to a FAT time/date pair. */
  228. void sdfat_time_unix2fat(struct sdfat_sb_info *sbi, sdfat_timespec_t *ts,
  229. DATE_TIME_T *tp)
  230. {
  231. bool tz_valid = (sbi->fsi.vol_type == EXFAT) ? true : false;
  232. time_t second = ts->tv_sec;
  233. time_t day, month, year;
  234. time_t ld; /* leap day */
  235. tp->Timezone.value = 0x00;
  236. /* Treats as local time with proper time */
  237. if (tz_valid || !sbi->options.tz_utc) {
  238. second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
  239. if (tz_valid) {
  240. tp->Timezone.valid = 1;
  241. tp->Timezone.off = TIMEZONE_CUR_OFFSET();
  242. }
  243. }
  244. /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
  245. if (second < UNIX_SECS_1980) {
  246. tp->Second = 0;
  247. tp->Minute = 0;
  248. tp->Hour = 0;
  249. tp->Day = 1;
  250. tp->Month = 1;
  251. tp->Year = 0;
  252. return;
  253. }
  254. #if (BITS_PER_LONG == 64)
  255. if (second >= UNIX_SECS_2108) {
  256. tp->Second = 59;
  257. tp->Minute = 59;
  258. tp->Hour = 23;
  259. tp->Day = 31;
  260. tp->Month = 12;
  261. tp->Year = 127;
  262. return;
  263. }
  264. #endif
  265. day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
  266. year = day / 365;
  267. MAKE_LEAP_YEAR(ld, year);
  268. if (year * 365 + ld > day)
  269. year--;
  270. MAKE_LEAP_YEAR(ld, year);
  271. day -= year * 365 + ld;
  272. if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
  273. month = 2;
  274. } else {
  275. if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
  276. day--;
  277. for (month = 1; month < 12; month++) {
  278. if (accum_days_in_year[month + 1] > day)
  279. break;
  280. }
  281. }
  282. day -= accum_days_in_year[month];
  283. tp->Second = second % SECS_PER_MIN;
  284. tp->Minute = (second / SECS_PER_MIN) % 60;
  285. tp->Hour = (second / SECS_PER_HOUR) % 24;
  286. tp->Day = day + 1;
  287. tp->Month = month;
  288. tp->Year = year;
  289. }
  290. TIMESTAMP_T *tm_now(struct sdfat_sb_info *sbi, TIMESTAMP_T *tp)
  291. {
  292. sdfat_timespec_t ts = CURRENT_TIME_SEC;
  293. DATE_TIME_T dt;
  294. sdfat_time_unix2fat(sbi, &ts, &dt);
  295. tp->year = dt.Year;
  296. tp->mon = dt.Month;
  297. tp->day = dt.Day;
  298. tp->hour = dt.Hour;
  299. tp->min = dt.Minute;
  300. tp->sec = dt.Second;
  301. tp->tz.value = dt.Timezone.value;
  302. return tp;
  303. }
  304. u8 calc_chksum_1byte(void *data, s32 len, u8 chksum)
  305. {
  306. s32 i;
  307. u8 *c = (u8 *) data;
  308. for (i = 0; i < len; i++, c++)
  309. chksum = (((chksum & 1) << 7) | ((chksum & 0xFE) >> 1)) + *c;
  310. return chksum;
  311. }
  312. u16 calc_chksum_2byte(void *data, s32 len, u16 chksum, s32 type)
  313. {
  314. s32 i;
  315. u8 *c = (u8 *) data;
  316. for (i = 0; i < len; i++, c++) {
  317. if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
  318. continue;
  319. chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) + (u16) *c;
  320. }
  321. return chksum;
  322. }
  323. #ifdef CONFIG_SDFAT_TRACE_ELAPSED_TIME
  324. struct timeval __t1, __t2;
  325. u32 sdfat_time_current_usec(struct timeval *tv)
  326. {
  327. do_gettimeofday(tv);
  328. return (u32)(tv->tv_sec*1000000 + tv->tv_usec);
  329. }
  330. #endif /* CONFIG_SDFAT_TRACE_ELAPSED_TIME */
  331. #ifdef CONFIG_SDFAT_DBG_CAREFUL
  332. /* Check the consistency of i_size_ondisk (FAT32, or flags 0x01 only) */
  333. void sdfat_debug_check_clusters(struct inode *inode)
  334. {
  335. unsigned int num_clusters;
  336. volatile uint32_t tmp_fat_chain[50];
  337. volatile int tmp_i = 0;
  338. volatile unsigned int num_clusters_org, tmp_i = 0;
  339. CHAIN_T clu;
  340. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  341. FS_INFO_T *fsi = &(SDFAT_SB(inode->i_sb)->fsi);
  342. if (SDFAT_I(inode)->i_size_ondisk == 0)
  343. num_clusters = 0;
  344. else
  345. num_clusters = ((SDFAT_I(inode)->i_size_ondisk-1) >> fsi->cluster_size_bits) + 1;
  346. clu.dir = fid->start_clu;
  347. clu.size = num_clusters;
  348. clu.flags = fid->flags;
  349. num_clusters_org = num_clusters;
  350. if (clu.flags == 0x03)
  351. return;
  352. while (num_clusters > 0) {
  353. /* FAT chain logging */
  354. tmp_fat_chain[tmp_i] = clu.dir;
  355. tmp_i++;
  356. if (tmp_i >= 50)
  357. tmp_i = 0;
  358. BUG_ON(IS_CLUS_EOF(clu.dir) || IS_CLUS_FREE(clu.dir));
  359. if (get_next_clus_safe(inode->i_sb, &(clu.dir)))
  360. EMSG("%s: failed to access to FAT\n");
  361. num_clusters--;
  362. }
  363. BUG_ON(!IS_CLUS_EOF(clu.dir));
  364. }
  365. #endif /* CONFIG_SDFAT_DBG_CAREFUL */
  366. #ifdef CONFIG_SDFAT_DBG_MSG
  367. void __sdfat_dmsg(int level, const char *fmt, ...)
  368. {
  369. #ifdef CONFIG_SDFAT_DBG_SHOW_PID
  370. struct va_format vaf;
  371. va_list args;
  372. /* should check type */
  373. if (level > SDFAT_MSG_LEVEL)
  374. return;
  375. va_start(args, fmt);
  376. vaf.fmt = fmt;
  377. vaf.va = &args;
  378. /* fmt already includes KERN_ pacility level */
  379. printk("[%u] %pV", current->pid, &vaf);
  380. va_end(args);
  381. #else
  382. va_list args;
  383. /* should check type */
  384. if (level > SDFAT_MSG_LEVEL)
  385. return;
  386. va_start(args, fmt);
  387. /* fmt already includes KERN_ pacility level */
  388. vprintk(fmt, args);
  389. va_end(args);
  390. #endif
  391. }
  392. #endif