misc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. int 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 0;
  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 -EIO;
  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. return 0;
  69. }
  70. /*
  71. * fat_chain_add() adds a new cluster to the chain of clusters represented
  72. * by inode.
  73. */
  74. int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
  75. {
  76. struct super_block *sb = inode->i_sb;
  77. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  78. int ret, new_fclus, last;
  79. /*
  80. * We must locate the last cluster of the file to add this new
  81. * one (new_dclus) to the end of the link list (the FAT).
  82. */
  83. last = new_fclus = 0;
  84. if (MSDOS_I(inode)->i_start) {
  85. int fclus, dclus;
  86. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  87. if (ret < 0)
  88. return ret;
  89. new_fclus = fclus + 1;
  90. last = dclus;
  91. }
  92. /* add new one to the last of the cluster chain */
  93. if (last) {
  94. struct fat_entry fatent;
  95. fatent_init(&fatent);
  96. ret = fat_ent_read(inode, &fatent, last);
  97. if (ret >= 0) {
  98. int wait = inode_needs_sync(inode);
  99. ret = fat_ent_write(inode, &fatent, new_dclus, wait);
  100. fatent_brelse(&fatent);
  101. }
  102. if (ret < 0)
  103. return ret;
  104. // fat_cache_add(inode, new_fclus, new_dclus);
  105. } else {
  106. MSDOS_I(inode)->i_start = new_dclus;
  107. MSDOS_I(inode)->i_logstart = new_dclus;
  108. /*
  109. * Since generic_write_sync() synchronizes regular files later,
  110. * we sync here only directories.
  111. */
  112. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  113. ret = fat_sync_inode(inode);
  114. if (ret)
  115. return ret;
  116. } else
  117. mark_inode_dirty(inode);
  118. }
  119. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  120. fat_fs_error(sb, "clusters badly computed (%d != %llu)",
  121. new_fclus,
  122. (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
  123. fat_cache_inval_inode(inode);
  124. }
  125. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  126. return 0;
  127. }
  128. extern struct timezone sys_tz;
  129. /*
  130. * The epoch of FAT timestamp is 1980.
  131. * : bits : value
  132. * date: 0 - 4: day (1 - 31)
  133. * date: 5 - 8: month (1 - 12)
  134. * date: 9 - 15: year (0 - 127) from 1980
  135. * time: 0 - 4: sec (0 - 29) 2sec counts
  136. * time: 5 - 10: min (0 - 59)
  137. * time: 11 - 15: hour (0 - 23)
  138. */
  139. #define SECS_PER_MIN 60
  140. #define SECS_PER_HOUR (60 * 60)
  141. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  142. #define UNIX_SECS_1980 315532800L
  143. #if BITS_PER_LONG == 64
  144. #define UNIX_SECS_2108 4354819200L
  145. #endif
  146. /* days between 1.1.70 and 1.1.80 (2 leap days) */
  147. #define DAYS_DELTA (365 * 10 + 2)
  148. /* 120 (2100 - 1980) isn't leap year */
  149. #define YEAR_2100 120
  150. #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
  151. /* Linear day numbers of the respective 1sts in non-leap years. */
  152. static time_t days_in_year[] = {
  153. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  154. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
  155. };
  156. /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
  157. void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
  158. __le16 __time, __le16 __date, u8 time_cs)
  159. {
  160. u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
  161. time_t second, day, leap_day, month, year;
  162. year = date >> 9;
  163. month = max(1, (date >> 5) & 0xf);
  164. day = max(1, date & 0x1f) - 1;
  165. leap_day = (year + 3) / 4;
  166. if (year > YEAR_2100) /* 2100 isn't leap year */
  167. leap_day--;
  168. if (IS_LEAP_YEAR(year) && month > 2)
  169. leap_day++;
  170. second = (time & 0x1f) << 1;
  171. second += ((time >> 5) & 0x3f) * SECS_PER_MIN;
  172. second += (time >> 11) * SECS_PER_HOUR;
  173. second += (year * 365 + leap_day
  174. + days_in_year[month] + day
  175. + DAYS_DELTA) * SECS_PER_DAY;
  176. if (!sbi->options.tz_utc)
  177. second += sys_tz.tz_minuteswest * SECS_PER_MIN;
  178. if (time_cs) {
  179. ts->tv_sec = second + (time_cs / 100);
  180. ts->tv_nsec = (time_cs % 100) * 10000000;
  181. } else {
  182. ts->tv_sec = second;
  183. ts->tv_nsec = 0;
  184. }
  185. }
  186. /* Convert linear UNIX date to a FAT time/date pair. */
  187. void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
  188. __le16 *time, __le16 *date, u8 *time_cs)
  189. {
  190. time_t second = ts->tv_sec;
  191. time_t day, leap_day, month, year;
  192. if (!sbi->options.tz_utc)
  193. second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
  194. /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
  195. if (second < UNIX_SECS_1980) {
  196. *time = 0;
  197. *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
  198. if (time_cs)
  199. *time_cs = 0;
  200. return;
  201. }
  202. #if BITS_PER_LONG == 64
  203. if (second >= UNIX_SECS_2108) {
  204. *time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
  205. *date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
  206. if (time_cs)
  207. *time_cs = 199;
  208. return;
  209. }
  210. #endif
  211. day = second / SECS_PER_DAY - DAYS_DELTA;
  212. year = day / 365;
  213. leap_day = (year + 3) / 4;
  214. if (year > YEAR_2100) /* 2100 isn't leap year */
  215. leap_day--;
  216. if (year * 365 + leap_day > day)
  217. year--;
  218. leap_day = (year + 3) / 4;
  219. if (year > YEAR_2100) /* 2100 isn't leap year */
  220. leap_day--;
  221. day -= year * 365 + leap_day;
  222. if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
  223. month = 2;
  224. } else {
  225. if (IS_LEAP_YEAR(year) && day > days_in_year[3])
  226. day--;
  227. for (month = 1; month < 12; month++) {
  228. if (days_in_year[month + 1] > day)
  229. break;
  230. }
  231. }
  232. day -= days_in_year[month];
  233. *time = cpu_to_le16(((second / SECS_PER_HOUR) % 24) << 11
  234. | ((second / SECS_PER_MIN) % 60) << 5
  235. | (second % SECS_PER_MIN) >> 1);
  236. *date = cpu_to_le16((year << 9) | (month << 5) | (day + 1));
  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. ll_rw_block(SWRITE, nr_bhs, bhs);
  245. for (i = 0; i < nr_bhs; i++) {
  246. wait_on_buffer(bhs[i]);
  247. if (buffer_eopnotsupp(bhs[i])) {
  248. clear_buffer_eopnotsupp(bhs[i]);
  249. err = -EOPNOTSUPP;
  250. } else if (!err && !buffer_uptodate(bhs[i]))
  251. err = -EIO;
  252. }
  253. return err;
  254. }