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