misc.c 12 KB

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