misc.c 7.5 KB

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