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