misc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/msdos_fs.h>
  11. #include <linux/buffer_head.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(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: Did not find valid FSINFO signature.\n"
  49. " Found signature1 0x%08x signature2 0x%08x"
  50. " (sector = %lu)\n",
  51. le32_to_cpu(fsinfo->signature1),
  52. le32_to_cpu(fsinfo->signature2),
  53. sbi->fsinfo_sector);
  54. } else {
  55. if (sbi->free_clusters != -1)
  56. fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
  57. if (sbi->prev_free != -1)
  58. fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
  59. mark_buffer_dirty(bh);
  60. if (sb->s_flags & MS_SYNCHRONOUS)
  61. sync_dirty_buffer(bh);
  62. }
  63. brelse(bh);
  64. }
  65. /*
  66. * fat_chain_add() adds a new cluster to the chain of clusters represented
  67. * by inode.
  68. */
  69. int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
  70. {
  71. struct super_block *sb = inode->i_sb;
  72. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  73. int ret, new_fclus, last;
  74. /*
  75. * We must locate the last cluster of the file to add this new
  76. * one (new_dclus) to the end of the link list (the FAT).
  77. */
  78. last = new_fclus = 0;
  79. if (MSDOS_I(inode)->i_start) {
  80. int fclus, dclus;
  81. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  82. if (ret < 0)
  83. return ret;
  84. new_fclus = fclus + 1;
  85. last = dclus;
  86. }
  87. /* add new one to the last of the cluster chain */
  88. if (last) {
  89. struct fat_entry fatent;
  90. fatent_init(&fatent);
  91. ret = fat_ent_read(inode, &fatent, last);
  92. if (ret >= 0) {
  93. int wait = inode_needs_sync(inode);
  94. ret = fat_ent_write(inode, &fatent, new_dclus, wait);
  95. fatent_brelse(&fatent);
  96. }
  97. if (ret < 0)
  98. return ret;
  99. // fat_cache_add(inode, new_fclus, new_dclus);
  100. } else {
  101. MSDOS_I(inode)->i_start = new_dclus;
  102. MSDOS_I(inode)->i_logstart = new_dclus;
  103. /*
  104. * Since generic_osync_inode() synchronize later if
  105. * this is not directory, we don't here.
  106. */
  107. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
  108. ret = fat_sync_inode(inode);
  109. if (ret)
  110. return ret;
  111. } else
  112. mark_inode_dirty(inode);
  113. }
  114. if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
  115. fat_fs_panic(sb, "clusters badly computed (%d != %lu)",
  116. new_fclus, inode->i_blocks >> (sbi->cluster_bits - 9));
  117. fat_cache_inval_inode(inode);
  118. }
  119. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  120. return 0;
  121. }
  122. extern struct timezone sys_tz;
  123. /* Linear day numbers of the respective 1sts in non-leap years. */
  124. static int day_n[] = {
  125. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  126. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
  127. };
  128. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  129. int date_dos2unix(unsigned short time, unsigned short date)
  130. {
  131. int month, year, secs;
  132. /*
  133. * first subtract and mask after that... Otherwise, if
  134. * date == 0, bad things happen
  135. */
  136. month = ((date >> 5) - 1) & 15;
  137. year = date >> 9;
  138. secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
  139. ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
  140. month < 2 ? 1 : 0)+3653);
  141. /* days since 1.1.70 plus 80's leap day */
  142. secs += sys_tz.tz_minuteswest*60;
  143. return secs;
  144. }
  145. /* Convert linear UNIX date to a MS-DOS time/date pair. */
  146. void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
  147. {
  148. int day, year, nl_day, month;
  149. unix_date -= sys_tz.tz_minuteswest*60;
  150. /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
  151. if (unix_date < 315532800)
  152. unix_date = 315532800;
  153. *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
  154. (((unix_date/3600) % 24) << 11));
  155. day = unix_date/86400-3652;
  156. year = day/365;
  157. if ((year+3)/4+365*year > day)
  158. year--;
  159. day -= (year+3)/4+365*year;
  160. if (day == 59 && !(year & 3)) {
  161. nl_day = day;
  162. month = 2;
  163. } else {
  164. nl_day = (year & 3) || day <= 59 ? day : day-1;
  165. for (month = 0; month < 12; month++) {
  166. if (day_n[month] > nl_day)
  167. break;
  168. }
  169. }
  170. *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
  171. }
  172. EXPORT_SYMBOL(fat_date_unix2dos);
  173. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  174. {
  175. int i, e, err = 0;
  176. for (i = 0; i < nr_bhs; i++) {
  177. lock_buffer(bhs[i]);
  178. if (test_clear_buffer_dirty(bhs[i])) {
  179. get_bh(bhs[i]);
  180. bhs[i]->b_end_io = end_buffer_write_sync;
  181. e = submit_bh(WRITE, bhs[i]);
  182. if (!err && e)
  183. err = e;
  184. } else
  185. unlock_buffer(bhs[i]);
  186. }
  187. for (i = 0; i < nr_bhs; i++) {
  188. wait_on_buffer(bhs[i]);
  189. if (buffer_eopnotsupp(bhs[i])) {
  190. clear_buffer_eopnotsupp(bhs[i]);
  191. err = -EOPNOTSUPP;
  192. } else if (!err && !buffer_uptodate(bhs[i]))
  193. err = -EIO;
  194. }
  195. return err;
  196. }
  197. EXPORT_SYMBOL(fat_sync_bhs);