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