misc.c 5.5 KB

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