misc.c 7.1 KB

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