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 9#include <linux/module.h> 10#include <linux/fs.h> 11#include <linux/buffer_head.h> 12#include <linux/time.h> 13#include "fat.h" 14 15/* 16 * fat_fs_error reports a file system problem that might indicate fa data 17 * corruption/inconsistency. Depending on 'errors' mount option the 18 * panic() is called, or error message is printed FAT and nothing is done, 19 * or filesystem is remounted read-only (default behavior). 20 * In case the file system is remounted read-only, it can be made writable 21 * again by remounting it. 22 */ 23void __fat_fs_error(struct super_block *s, int report, const char *fmt, ...) 24{ 25 struct fat_mount_options *opts = &MSDOS_SB(s)->options; 26 va_list args; 27 28 if (report) { 29 printk(KERN_ERR "FAT: Filesystem error (dev %s)\n", s->s_id); 30 31 printk(KERN_ERR " "); 32 va_start(args, fmt); 33 vprintk(fmt, args); 34 va_end(args); 35 printk("\n"); 36 } 37 38 if (opts->errors == FAT_ERRORS_PANIC) 39 panic("FAT: fs panic from previous error\n"); 40 else if (opts->errors == FAT_ERRORS_RO && !(s->s_flags & MS_RDONLY)) { 41 s->s_flags |= MS_RDONLY; 42 printk(KERN_ERR "FAT: Filesystem has been set read-only\n"); 43 } 44} 45EXPORT_SYMBOL_GPL(__fat_fs_error); 46 47/* Flushes the number of free clusters on FAT32 */ 48int fat_clusters_flush(struct super_block *sb) 49{ 50 struct msdos_sb_info *sbi = MSDOS_SB(sb); 51 struct buffer_head *bh; 52 struct fat_boot_fsinfo *fsinfo; 53 54 if (sbi->fat_bits != 32) 55 return 0; 56 57 bh = sb_bread(sb, sbi->fsinfo_sector); 58 if (bh == NULL) { 59 printk(KERN_ERR "FAT: bread failed in fat_clusters_flush\n"); 60 return -EIO; 61 } 62 63 fsinfo = (struct fat_boot_fsinfo *)bh->b_data; 64 /* Sanity check */ 65 if (!IS_FSINFO(fsinfo)) { 66 printk(KERN_ERR "FAT: Invalid FSINFO signature: " 67 "0x%08x, 0x%08x (sector = %lu)\n", 68 le32_to_cpu(fsinfo->signature1), 69 le32_to_cpu(fsinfo->signature2), 70 sbi->fsinfo_sector); 71 } else { 72 if (sbi->free_clusters != -1) 73 fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters); 74 if (sbi->prev_free != -1) 75 fsinfo->next_cluster = cpu_to_le32(sbi->prev_free); 76 mark_buffer_dirty(bh); 77 } 78 brelse(bh); 79 80 return 0; 81} 82 83/* 84 * fat_chain_add() adds a new cluster to the chain of clusters represented 85 * by inode. 86 */ 87int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster) 88{ 89 struct super_block *sb = inode->i_sb; 90 struct msdos_sb_info *sbi = MSDOS_SB(sb); 91 int ret, new_fclus, last; 92 93 /* 94 * We must locate the last cluster of the file to add this new 95 * one (new_dclus) to the end of the link list (the FAT). 96 */ 97 last = new_fclus = 0; 98 if (MSDOS_I(inode)->i_start) { 99 int fclus, dclus; 100 101 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); 102 if (ret < 0) 103 return ret; 104 new_fclus = fclus + 1; 105 last = dclus; 106 } 107 108 /* add new one to the last of the cluster chain */ 109 if (last) { 110 struct fat_entry fatent; 111 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// fat_cache_add(inode, new_fclus, new_dclus); 122 } else { 123 MSDOS_I(inode)->i_start = new_dclus; 124 MSDOS_I(inode)->i_logstart = new_dclus; 125 /* 126 * Since generic_write_sync() synchronizes regular files later, 127 * we sync here only directories. 128 */ 129 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) { 130 ret = fat_sync_inode(inode); 131 if (ret) 132 return ret; 133 } else 134 mark_inode_dirty(inode); 135 } 136 if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) { 137 fat_fs_error(sb, "clusters badly computed (%d != %llu)", 138 new_fclus, 139 (llu)(inode->i_blocks >> (sbi->cluster_bits - 9))); 140 fat_cache_inval_inode(inode); 141 } 142 inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9); 143 144 return 0; 145} 146 147extern struct timezone sys_tz; 148 149/* 150 * The epoch of FAT timestamp is 1980. 151 * : bits : value 152 * date: 0 - 4: day (1 - 31) 153 * date: 5 - 8: month (1 - 12) 154 * date: 9 - 15: year (0 - 127) from 1980 155 * time: 0 - 4: sec (0 - 29) 2sec counts 156 * time: 5 - 10: min (0 - 59) 157 * time: 11 - 15: hour (0 - 23) 158 */ 159#define SECS_PER_MIN 60 160#define SECS_PER_HOUR (60 * 60) 161#define SECS_PER_DAY (SECS_PER_HOUR * 24) 162/* days between 1.1.70 and 1.1.80 (2 leap days) */ 163#define DAYS_DELTA (365 * 10 + 2) 164/* 120 (2100 - 1980) isn't leap year */ 165#define YEAR_2100 120 166#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100) 167 168/* Linear day numbers of the respective 1sts in non-leap years. */ 169static 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 174/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */ 175void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, 176 __le16 __time, __le16 __date, u8 time_cs) 177{ 178 u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date); 179 time_t second, day, leap_day, month, year; 180 181 year = date >> 9; 182 month = max(1, (date >> 5) & 0xf); 183 day = max(1, date & 0x1f) - 1; 184 185 leap_day = (year + 3) / 4; 186 if (year > YEAR_2100) /* 2100 isn't leap year */ 187 leap_day--; 188 if (IS_LEAP_YEAR(year) && month > 2) 189 leap_day++; 190 191 second = (time & 0x1f) << 1; 192 second += ((time >> 5) & 0x3f) * SECS_PER_MIN; 193 second += (time >> 11) * SECS_PER_HOUR; 194 second += (year * 365 + leap_day 195 + days_in_year[month] + day 196 + DAYS_DELTA) * SECS_PER_DAY; 197 198 if (!sbi->options.tz_utc) 199 second += sys_tz.tz_minuteswest * SECS_PER_MIN; 200 201 if (time_cs) { 202 ts->tv_sec = second + (time_cs / 100); 203 ts->tv_nsec = (time_cs % 100) * 10000000; 204 } else { 205 ts->tv_sec = second; 206 ts->tv_nsec = 0; 207 } 208} 209 210/* Convert linear UNIX date to a FAT time/date pair. */ 211void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts, 212 __le16 *time, __le16 *date, u8 *time_cs) 213{ 214 struct tm tm; 215 time_to_tm(ts->tv_sec, sbi->options.tz_utc ? 0 : 216 -sys_tz.tz_minuteswest * 60, &tm); 217 218 /* FAT can only support year between 1980 to 2107 */ 219 if (tm.tm_year < 1980 - 1900) { 220 *time = 0; 221 *date = cpu_to_le16((0 << 9) | (1 << 5) | 1); 222 if (time_cs) 223 *time_cs = 0; 224 return; 225 } 226 if (tm.tm_year > 2107 - 1900) { 227 *time = cpu_to_le16((23 << 11) | (59 << 5) | 29); 228 *date = cpu_to_le16((127 << 9) | (12 << 5) | 31); 229 if (time_cs) 230 *time_cs = 199; 231 return; 232 } 233 234 /* from 1900 -> from 1980 */ 235 tm.tm_year -= 80; 236 /* 0~11 -> 1~12 */ 237 tm.tm_mon++; 238 /* 0~59 -> 0~29(2sec counts) */ 239 tm.tm_sec >>= 1; 240 241 *time = cpu_to_le16(tm.tm_hour << 11 | tm.tm_min << 5 | tm.tm_sec); 242 *date = cpu_to_le16(tm.tm_year << 9 | tm.tm_mon << 5 | tm.tm_mday); 243 if (time_cs) 244 *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000; 245} 246EXPORT_SYMBOL_GPL(fat_time_unix2fat); 247 248int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) 249{ 250 int i, err = 0; 251 252 for (i = 0; i < nr_bhs; i++) 253 write_dirty_buffer(bhs[i], WRITE); 254 255 for (i = 0; i < nr_bhs; i++) { 256 wait_on_buffer(bhs[i]); 257 if (buffer_eopnotsupp(bhs[i])) { 258 clear_buffer_eopnotsupp(bhs[i]); 259 err = -EOPNOTSUPP; 260 } else if (!err && !buffer_uptodate(bhs[i])) 261 err = -EIO; 262 } 263 return err; 264} 265