misc.c 7.3 KB

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