misc.c 7.5 KB

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