misc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /*
  123. * FIXME:Although we can add this cache, fat_cache_add() is
  124. * assuming to be called after linear search with fat_cache_id.
  125. */
  126. // fat_cache_add(inode, new_fclus, new_dclus);
  127. } else {
  128. MSDOS_I(inode)->i_start = new_dclus;
  129. MSDOS_I(inode)->i_logstart = new_dclus;
  130. /*
  131. * Since generic_write_sync() synchronizes regular files later,
  132. * we sync here only directories.
  133. */
  134. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  135. ret = fat_sync_inode(inode);
  136. if (ret)
  137. return ret;
  138. } else
  139. mark_inode_dirty(inode);
  140. }
  141. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  142. fat_fs_error(sb, "clusters badly computed (%d != %llu)",
  143. new_fclus,
  144. (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
  145. fat_cache_inval_inode(inode);
  146. }
  147. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  148. return 0;
  149. }
  150. extern struct timezone sys_tz;
  151. /*
  152. * The epoch of FAT timestamp is 1980.
  153. * : bits : value
  154. * date: 0 - 4: day (1 - 31)
  155. * date: 5 - 8: month (1 - 12)
  156. * date: 9 - 15: year (0 - 127) from 1980
  157. * time: 0 - 4: sec (0 - 29) 2sec counts
  158. * time: 5 - 10: min (0 - 59)
  159. * time: 11 - 15: hour (0 - 23)
  160. */
  161. #define SECS_PER_MIN 60
  162. #define SECS_PER_HOUR (60 * 60)
  163. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  164. /* days between 1.1.70 and 1.1.80 (2 leap days) */
  165. #define DAYS_DELTA (365 * 10 + 2)
  166. /* 120 (2100 - 1980) isn't leap year */
  167. #define YEAR_2100 120
  168. #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
  169. /* Linear day numbers of the respective 1sts in non-leap years. */
  170. static time_t days_in_year[] = {
  171. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  172. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
  173. };
  174. /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
  175. void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
  176. __le16 __time, __le16 __date, u8 time_cs)
  177. {
  178. u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
  179. time_t second, day, leap_day, month, year;
  180. year = date >> 9;
  181. month = max(1, (date >> 5) & 0xf);
  182. day = max(1, date & 0x1f) - 1;
  183. leap_day = (year + 3) / 4;
  184. if (year > YEAR_2100) /* 2100 isn't leap year */
  185. leap_day--;
  186. if (IS_LEAP_YEAR(year) && month > 2)
  187. leap_day++;
  188. second = (time & 0x1f) << 1;
  189. second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
  190. second += (time >> 11) * SECS_PER_HOUR;
  191. second += (year * 365 + leap_day
  192. + days_in_year[month] + day
  193. + DAYS_DELTA) * SECS_PER_DAY;
  194. if (!sbi->options.tz_set)
  195. second += sys_tz.tz_minuteswest * SECS_PER_MIN;
  196. else
  197. second -= sbi->options.time_offset * SECS_PER_MIN;
  198. if (time_cs) {
  199. ts->tv_sec = second + (time_cs / 100);
  200. ts->tv_nsec = (time_cs % 100) * 10000000;
  201. } else {
  202. ts->tv_sec = second;
  203. ts->tv_nsec = 0;
  204. }
  205. }
  206. /* Convert linear UNIX date to a FAT time/date pair. */
  207. void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
  208. __le16 *time, __le16 *date, u8 *time_cs)
  209. {
  210. struct tm tm;
  211. time_to_tm(ts->tv_sec,
  212. (sbi->options.tz_set ? sbi->options.time_offset :
  213. -sys_tz.tz_minuteswest) * SECS_PER_MIN, &tm);
  214. /* FAT can only support year between 1980 to 2107 */
  215. if (tm.tm_year < 1980 - 1900) {
  216. *time = 0;
  217. *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
  218. if (time_cs)
  219. *time_cs = 0;
  220. return;
  221. }
  222. if (tm.tm_year > 2107 - 1900) {
  223. *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
  224. *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
  225. if (time_cs)
  226. *time_cs = 199;
  227. return;
  228. }
  229. /* from 1900 -> from 1980 */
  230. tm.tm_year -= 80;
  231. /* 0~11 -> 1~12 */
  232. tm.tm_mon++;
  233. /* 0~59 -> 0~29(2sec counts) */
  234. tm.tm_sec >>= 1;
  235. *time = cpu_to_le16(tm.tm_hour << 11 | tm.tm_min << 5 | tm.tm_sec);
  236. *date = cpu_to_le16(tm.tm_year << 9 | tm.tm_mon << 5 | tm.tm_mday);
  237. if (time_cs)
  238. *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
  239. }
  240. EXPORT_SYMBOL_GPL(fat_time_unix2fat);
  241. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  242. {
  243. int i, err = 0;
  244. for (i = 0; i < nr_bhs; i++)
  245. write_dirty_buffer(bhs[i], WRITE);
  246. for (i = 0; i < nr_bhs; i++) {
  247. wait_on_buffer(bhs[i]);
  248. if (!err && !buffer_uptodate(bhs[i]))
  249. err = -EIO;
  250. }
  251. return err;
  252. }