misc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * linux/fs/fat/misc.c
  3. *
  4. * Written 1992,1993 by Werner Almesberger
  5. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  6. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/time.h>
  12. #include "fat.h"
  13. /*
  14. * fat_fs_error reports a file system problem that might indicate fa data
  15. * corruption/inconsistency. Depending on 'errors' mount option the
  16. * panic() is called, or error message is printed FAT and nothing is done,
  17. * or filesystem is remounted read-only (default behavior).
  18. * In case the file system is remounted read-only, it can be made writable
  19. * again by remounting it.
  20. */
  21. void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
  22. {
  23. struct fat_mount_options *opts = &MSDOS_SB(sb)->options;
  24. va_list args;
  25. struct va_format vaf;
  26. struct block_device *bdev = sb->s_bdev;
  27. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  28. if (report) {
  29. va_start(args, fmt);
  30. vaf.fmt = fmt;
  31. vaf.va = &args;
  32. printk(KERN_ERR "FAT-fs (%s[%d:%d]): error, %pV\n",
  33. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  34. if (opts->errors == FAT_ERRORS_RO && !(sb->s_flags & MS_RDONLY))
  35. ST_LOG("FAT-fs (%s[%d:%d]): error, %pV\n",
  36. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  37. va_end(args);
  38. }
  39. if (opts->errors == FAT_ERRORS_PANIC)
  40. panic("FAT-fs (%s[%d:%d]): fs panic from previous error\n",
  41. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  42. else if (opts->errors == FAT_ERRORS_RO && !(sb->s_flags & MS_RDONLY)) {
  43. sb->s_flags |= MS_RDONLY;
  44. printk(KERN_ERR "FAT-fs (%s[%d:%d]): Filesystem has been "
  45. "set read-only\n",
  46. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  47. ST_LOG("FAT-fs (%s[%d:%d]): Filesystem has been set read-only\n",
  48. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  49. }
  50. }
  51. EXPORT_SYMBOL_GPL(__fat_fs_error);
  52. /**
  53. * fat_msg() - print preformated FAT specific messages. Every thing what is
  54. * not fat_fs_error() should be fat_msg().
  55. */
  56. void fat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  57. {
  58. struct va_format vaf;
  59. va_list args;
  60. struct block_device *bdev = sb->s_bdev;
  61. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  62. va_start(args, fmt);
  63. vaf.fmt = fmt;
  64. vaf.va = &args;
  65. if (!strncmp(level, KERN_ERR, sizeof(KERN_ERR)))
  66. printk_ratelimited("%sFAT-fs (%s[%d:%d]): %pV\n", level,
  67. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  68. else
  69. printk("%sFAT-fs (%s[%d:%d]): %pV\n", level,
  70. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  71. va_end(args);
  72. }
  73. /* Flushes the number of free clusters on FAT32 */
  74. /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
  75. int fat_clusters_flush(struct super_block *sb)
  76. {
  77. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  78. struct buffer_head *bh;
  79. struct fat_boot_fsinfo *fsinfo;
  80. if (sbi->fat_bits != 32)
  81. return 0;
  82. bh = sb_bread(sb, sbi->fsinfo_sector);
  83. if (bh == NULL) {
  84. fat_msg(sb, KERN_ERR, "bread failed in fat_clusters_flush");
  85. return -EIO;
  86. }
  87. fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
  88. /* Sanity check */
  89. if (!IS_FSINFO(fsinfo)) {
  90. fat_msg(sb, KERN_ERR, "Invalid FSINFO signature: "
  91. "0x%08x, 0x%08x (sector = %lu)",
  92. le32_to_cpu(fsinfo->signature1),
  93. le32_to_cpu(fsinfo->signature2),
  94. sbi->fsinfo_sector);
  95. } else {
  96. if (sbi->free_clusters != -1)
  97. fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
  98. if (sbi->prev_free != -1)
  99. fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
  100. mark_buffer_dirty_sync(bh);
  101. }
  102. brelse(bh);
  103. return 0;
  104. }
  105. /*
  106. * fat_chain_add() adds a new cluster to the chain of clusters represented
  107. * by inode.
  108. */
  109. int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
  110. {
  111. struct super_block *sb = inode->i_sb;
  112. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  113. int ret, new_fclus, last;
  114. /*
  115. * We must locate the last cluster of the file to add this new
  116. * one (new_dclus) to the end of the link list (the FAT).
  117. */
  118. last = new_fclus = 0;
  119. if (MSDOS_I(inode)->i_start) {
  120. int fclus, dclus;
  121. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  122. if (ret < 0)
  123. return ret;
  124. new_fclus = fclus + 1;
  125. last = dclus;
  126. }
  127. /* add new one to the last of the cluster chain */
  128. if (last) {
  129. struct fat_entry fatent;
  130. fatent_init(&fatent);
  131. ret = fat_ent_read(inode, &fatent, last);
  132. if (ret >= 0) {
  133. int wait = inode_needs_sync(inode);
  134. ret = fat_ent_write(inode, &fatent, new_dclus, wait);
  135. fatent_brelse(&fatent);
  136. }
  137. if (ret < 0)
  138. return ret;
  139. // fat_cache_add(inode, new_fclus, new_dclus);
  140. } else {
  141. MSDOS_I(inode)->i_start = new_dclus;
  142. MSDOS_I(inode)->i_logstart = new_dclus;
  143. /*
  144. * Since generic_write_sync() synchronizes regular files later,
  145. * we sync here only directories.
  146. */
  147. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  148. ret = fat_sync_inode(inode);
  149. if (ret)
  150. return ret;
  151. } else
  152. mark_inode_dirty(inode);
  153. }
  154. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  155. fat_fs_error(sb, "clusters badly computed (%d != %llu)",
  156. new_fclus,
  157. (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
  158. fat_cache_inval_inode(inode);
  159. }
  160. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  161. return 0;
  162. }
  163. extern struct timezone sys_tz;
  164. /*
  165. * The epoch of FAT timestamp is 1980.
  166. * : bits : value
  167. * date: 0 - 4: day (1 - 31)
  168. * date: 5 - 8: month (1 - 12)
  169. * date: 9 - 15: year (0 - 127) from 1980
  170. * time: 0 - 4: sec (0 - 29) 2sec counts
  171. * time: 5 - 10: min (0 - 59)
  172. * time: 11 - 15: hour (0 - 23)
  173. */
  174. #define SECS_PER_MIN 60
  175. #define SECS_PER_HOUR (60 * 60)
  176. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  177. /* days between 1.1.70 and 1.1.80 (2 leap days) */
  178. #define DAYS_DELTA (365 * 10 + 2)
  179. /* 120 (2100 - 1980) isn't leap year */
  180. #define YEAR_2100 120
  181. #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
  182. /* Linear day numbers of the respective 1sts in non-leap years. */
  183. static time_t days_in_year[] = {
  184. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  185. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
  186. };
  187. /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
  188. void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
  189. __le16 __time, __le16 __date, u8 time_cs)
  190. {
  191. u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
  192. time_t second, day, leap_day, month, year;
  193. year = date >> 9;
  194. month = max(1, (date >> 5) & 0xf);
  195. day = max(1, date & 0x1f) - 1;
  196. leap_day = (year + 3) / 4;
  197. if (year > YEAR_2100) /* 2100 isn't leap year */
  198. leap_day--;
  199. if (IS_LEAP_YEAR(year) && month > 2)
  200. leap_day++;
  201. second = (time & 0x1f) << 1;
  202. second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
  203. second += (time >> 11) * SECS_PER_HOUR;
  204. second += (year * 365 + leap_day
  205. + days_in_year[month] + day
  206. + DAYS_DELTA) * SECS_PER_DAY;
  207. if (!sbi->options.tz_utc)
  208. second += sys_tz.tz_minuteswest * SECS_PER_MIN;
  209. if (time_cs) {
  210. ts->tv_sec = second + (time_cs / 100);
  211. ts->tv_nsec = (time_cs % 100) * 10000000;
  212. } else {
  213. ts->tv_sec = second;
  214. ts->tv_nsec = 0;
  215. }
  216. }
  217. /* Convert linear UNIX date to a FAT time/date pair. */
  218. void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
  219. __le16 *time, __le16 *date, u8 *time_cs)
  220. {
  221. struct tm tm;
  222. time_to_tm(ts->tv_sec, sbi->options.tz_utc ? 0 :
  223. -sys_tz.tz_minuteswest * 60, &tm);
  224. /* FAT can only support year between 1980 to 2107 */
  225. if (tm.tm_year < 1980 - 1900) {
  226. *time = 0;
  227. *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
  228. if (time_cs)
  229. *time_cs = 0;
  230. return;
  231. }
  232. if (tm.tm_year > 2107 - 1900) {
  233. *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
  234. *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
  235. if (time_cs)
  236. *time_cs = 199;
  237. return;
  238. }
  239. /* from 1900 -> from 1980 */
  240. tm.tm_year -= 80;
  241. /* 0~11 -> 1~12 */
  242. tm.tm_mon++;
  243. /* 0~59 -> 0~29(2sec counts) */
  244. tm.tm_sec >>= 1;
  245. *time = cpu_to_le16(tm.tm_hour << 11 | tm.tm_min << 5 | tm.tm_sec);
  246. *date = cpu_to_le16(tm.tm_year << 9 | tm.tm_mon << 5 | tm.tm_mday);
  247. if (time_cs)
  248. *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
  249. }
  250. EXPORT_SYMBOL_GPL(fat_time_unix2fat);
  251. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  252. {
  253. int i, err = 0;
  254. for (i = 0; i < nr_bhs; i++)
  255. write_dirty_buffer(bhs[i], WRITE_SYNC);
  256. for (i = 0; i < nr_bhs; i++) {
  257. wait_on_buffer(bhs[i]);
  258. if (!err && !buffer_uptodate(bhs[i]))
  259. err = -EIO;
  260. }
  261. return err;
  262. }