misc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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: 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 != %lu)",
  113. new_fclus, inode->i_blocks >> (sbi->cluster_bits - 9));
  114. fat_cache_inval_inode(inode);
  115. }
  116. inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
  117. return 0;
  118. }
  119. extern struct timezone sys_tz;
  120. /* Linear day numbers of the respective 1sts in non-leap years. */
  121. static int day_n[] = {
  122. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  123. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
  124. };
  125. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  126. int date_dos2unix(unsigned short time, unsigned short date)
  127. {
  128. int month, year, secs;
  129. /*
  130. * first subtract and mask after that... Otherwise, if
  131. * date == 0, bad things happen
  132. */
  133. month = ((date >> 5) - 1) & 15;
  134. year = date >> 9;
  135. secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
  136. ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
  137. month < 2 ? 1 : 0)+3653);
  138. /* days since 1.1.70 plus 80's leap day */
  139. secs += sys_tz.tz_minuteswest*60;
  140. return secs;
  141. }
  142. /* Convert linear UNIX date to a MS-DOS time/date pair. */
  143. void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
  144. {
  145. int day, year, nl_day, month;
  146. unix_date -= sys_tz.tz_minuteswest*60;
  147. /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
  148. if (unix_date < 315532800)
  149. unix_date = 315532800;
  150. *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
  151. (((unix_date/3600) % 24) << 11));
  152. day = unix_date/86400-3652;
  153. year = day/365;
  154. if ((year+3)/4+365*year > day)
  155. year--;
  156. day -= (year+3)/4+365*year;
  157. if (day == 59 && !(year & 3)) {
  158. nl_day = day;
  159. month = 2;
  160. } else {
  161. nl_day = (year & 3) || day <= 59 ? day : day-1;
  162. for (month = 0; month < 12; month++) {
  163. if (day_n[month] > nl_day)
  164. break;
  165. }
  166. }
  167. *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
  168. }
  169. EXPORT_SYMBOL_GPL(fat_date_unix2dos);
  170. int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
  171. {
  172. int i, err = 0;
  173. ll_rw_block(SWRITE, nr_bhs, bhs);
  174. for (i = 0; i < nr_bhs; i++) {
  175. wait_on_buffer(bhs[i]);
  176. if (buffer_eopnotsupp(bhs[i])) {
  177. clear_buffer_eopnotsupp(bhs[i]);
  178. err = -EOPNOTSUPP;
  179. } else if (!err && !buffer_uptodate(bhs[i]))
  180. err = -EIO;
  181. }
  182. return err;
  183. }