misc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "fat.h"
  12. /*
  13. * fat_fs_error reports a file system problem that might indicate fa data
  14. * corruption/inconsistency. Depending on 'errors' mount option the
  15. * panic() is called, or error message is printed FAT and nothing is done,
  16. * or filesystem is remounted read-only (default behavior).
  17. * In case the file system is remounted read-only, it can be made writable
  18. * again by remounting it.
  19. */
  20. void fat_fs_error(struct super_block *s, const char *fmt, ...)
  21. {
  22. struct fat_mount_options *opts = &MSDOS_SB(s)->options;
  23. va_list args;
  24. printk(KERN_ERR "FAT: Filesystem error (dev %s)\n", s->s_id);
  25. printk(KERN_ERR " ");
  26. va_start(args, fmt);
  27. vprintk(fmt, args);
  28. va_end(args);
  29. printk("\n");
  30. if (opts->errors == FAT_ERRORS_PANIC)
  31. panic(" FAT fs panic from previous error\n");
  32. else if (opts->errors == FAT_ERRORS_RO && !(s->s_flags & MS_RDONLY)) {
  33. s->s_flags |= MS_RDONLY;
  34. printk(KERN_ERR " File system has been set read-only\n");
  35. }
  36. }
  37. EXPORT_SYMBOL_GPL(fat_fs_error);
  38. /* Flushes the number of free clusters on FAT32 */
  39. /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
  40. void fat_clusters_flush(struct super_block *sb)
  41. {
  42. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  43. struct buffer_head *bh;
  44. struct fat_boot_fsinfo *fsinfo;
  45. if (sbi->fat_bits != 32)
  46. return;
  47. bh = sb_bread(sb, sbi->fsinfo_sector);
  48. if (bh == NULL) {
  49. printk(KERN_ERR "FAT: bread failed in fat_clusters_flush\n");
  50. return;
  51. }
  52. fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
  53. /* Sanity check */
  54. if (!IS_FSINFO(fsinfo)) {
  55. printk(KERN_ERR "FAT: Invalid FSINFO signature: "
  56. "0x%08x, 0x%08x (sector = %lu)\n",
  57. le32_to_cpu(fsinfo->signature1),
  58. le32_to_cpu(fsinfo->signature2),
  59. sbi->fsinfo_sector);
  60. } else {
  61. if (sbi->free_clusters != -1)
  62. fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
  63. if (sbi->prev_free != -1)
  64. fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
  65. mark_buffer_dirty(bh);
  66. }
  67. brelse(bh);
  68. }
  69. /*
  70. * fat_chain_add() adds a new cluster to the chain of clusters represented
  71. * by inode.
  72. */
  73. int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
  74. {
  75. struct super_block *sb = inode->i_sb;
  76. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  77. int ret, new_fclus, last;
  78. /*
  79. * We must locate the last cluster of the file to add this new
  80. * one (new_dclus) to the end of the link list (the FAT).
  81. */
  82. last = new_fclus = 0;
  83. if (MSDOS_I(inode)->i_start) {
  84. int fclus, dclus;
  85. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  86. if (ret < 0)
  87. return ret;
  88. new_fclus = fclus + 1;
  89. last = dclus;
  90. }
  91. /* add new one to the last of the cluster chain */
  92. if (last) {
  93. struct fat_entry fatent;
  94. fatent_init(&fatent);
  95. ret = fat_ent_read(inode, &fatent, last);
  96. if (ret >= 0) {
  97. int wait = inode_needs_sync(inode);
  98. ret = fat_ent_write(inode, &fatent, new_dclus, wait);
  99. fatent_brelse(&fatent);
  100. }
  101. if (ret < 0)
  102. return ret;
  103. // fat_cache_add(inode, new_fclus, new_dclus);
  104. } else {
  105. MSDOS_I(inode)->i_start = new_dclus;
  106. MSDOS_I(inode)->i_logstart = new_dclus;
  107. /*
  108. * Since generic_osync_inode() synchronize later if
  109. * this is not directory, we don't here.
  110. */
  111. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  112. ret = fat_sync_inode(inode);
  113. if (ret)
  114. return ret;
  115. } else
  116. mark_inode_dirty(inode);
  117. }
  118. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  119. fat_fs_error(sb, "clusters badly computed (%d != %llu)",
  120. new_fclus,
  121. (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
  122. fat_cache_inval_inode(inode);
  123. }
  124. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  125. return 0;
  126. }
  127. extern struct timezone sys_tz;
  128. /*
  129. * The epoch of FAT timestamp is 1980.
  130. * : bits : value
  131. * date: 0 - 4: day (1 - 31)
  132. * date: 5 - 8: month (1 - 12)
  133. * date: 9 - 15: year (0 - 127) from 1980
  134. * time: 0 - 4: sec (0 - 29) 2sec counts
  135. * time: 5 - 10: min (0 - 59)
  136. * time: 11 - 15: hour (0 - 23)
  137. */
  138. #define SECS_PER_MIN 60
  139. #define SECS_PER_HOUR (60 * 60)
  140. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  141. #define UNIX_SECS_1980 315532800L
  142. #if BITS_PER_LONG == 64
  143. #define UNIX_SECS_2108 4354819200L
  144. #endif
  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. time_t second = ts->tv_sec;
  190. time_t day, leap_day, month, year;
  191. if (!sbi->options.tz_utc)
  192. second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
  193. /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
  194. if (second < UNIX_SECS_1980) {
  195. *time = 0;
  196. *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
  197. if (time_cs)
  198. *time_cs = 0;
  199. return;
  200. }
  201. #if BITS_PER_LONG == 64
  202. if (second >= UNIX_SECS_2108) {
  203. *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
  204. *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
  205. if (time_cs)
  206. *time_cs = 199;
  207. return;
  208. }
  209. #endif
  210. day = second / SECS_PER_DAY - DAYS_DELTA;
  211. year = day / 365;
  212. leap_day = (year + 3) / 4;
  213. if (year > YEAR_2100) /* 2100 isn't leap year */
  214. leap_day--;
  215. if (year * 365 + leap_day > day)
  216. year--;
  217. leap_day = (year + 3) / 4;
  218. if (year > YEAR_2100) /* 2100 isn't leap year */
  219. leap_day--;
  220. day -= year * 365 + leap_day;
  221. if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
  222. month = 2;
  223. } else {
  224. if (IS_LEAP_YEAR(year) && day > days_in_year[3])
  225. day--;
  226. for (month = 1; month < 12; month++) {
  227. if (days_in_year[month + 1] > day)
  228. break;
  229. }
  230. }
  231. day -= days_in_year[month];
  232. *time = cpu_to_le16(((second / SECS_PER_HOUR) % 24) << 11
  233. | ((second / SECS_PER_MIN) % 60) << 5
  234. | (second % SECS_PER_MIN) >> 1);
  235. *date = cpu_to_le16((year << 9) | (month << 5) | (day + 1));
  236. if (time_cs)
  237. *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
  238. }
  239. EXPORT_SYMBOL_GPL(fat_time_unix2fat);
  240. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  241. {
  242. int i, err = 0;
  243. ll_rw_block(SWRITE, nr_bhs, bhs);
  244. for (i = 0; i < nr_bhs; i++) {
  245. wait_on_buffer(bhs[i]);
  246. if (buffer_eopnotsupp(bhs[i])) {
  247. clear_buffer_eopnotsupp(bhs[i]);
  248. err = -EOPNOTSUPP;
  249. } else if (!err && !buffer_uptodate(bhs[i]))
  250. err = -EIO;
  251. }
  252. return err;
  253. }