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