misc.c 7.1 KB

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