misc.c 7.7 KB

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