blob: 970e682ea7548afa8ae807c409772724a398b0d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/buffer_head.h>
Zhaolei1d81a182009-12-15 16:46:57 -080012#include <linux/time.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080013#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15/*
Denis Karpov85c78592009-06-04 02:34:22 +090016 * 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 Torvalds1da177e2005-04-16 15:20:36 -070022 */
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070023void __fat_fs_error(struct super_block *s, int report, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Denis Karpov85c78592009-06-04 02:34:22 +090025 struct fat_mount_options *opts = &MSDOS_SB(s)->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 va_list args;
27
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070028 if (report) {
29 printk(KERN_ERR "FAT: Filesystem error (dev %s)\n", s->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070031 printk(KERN_ERR " ");
32 va_start(args, fmt);
33 vprintk(fmt, args);
34 va_end(args);
35 printk("\n");
36 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Denis Karpov85c78592009-06-04 02:34:22 +090038 if (opts->errors == FAT_ERRORS_PANIC)
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070039 panic("FAT: fs panic from previous error\n");
Denis Karpov85c78592009-06-04 02:34:22 +090040 else if (opts->errors == FAT_ERRORS_RO && !(s->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 s->s_flags |= MS_RDONLY;
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070042 printk(KERN_ERR "FAT: Filesystem has been set read-only\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
44}
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -070045EXPORT_SYMBOL_GPL(__fat_fs_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* Flushes the number of free clusters on FAT32 */
48/* XXX: Need to write one per FSINFO block. Currently only writes 1 */
OGAWA Hirofumied248b22009-09-20 01:31:58 +090049int fat_clusters_flush(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
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 Hirofumied248b22009-09-20 01:31:58 +090056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
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 Hirofumied248b22009-09-20 01:31:58 +090061 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
64 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
65 /* Sanity check */
66 if (!IS_FSINFO(fsinfo)) {
Vegard Nossumb4cf9c32008-02-06 01:36:36 -080067 printk(KERN_ERR "FAT: Invalid FSINFO signature: "
68 "0x%08x, 0x%08x (sector = %lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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 Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 brelse(bh);
OGAWA Hirofumied248b22009-09-20 01:31:58 +090080
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*
85 * fat_chain_add() adds a new cluster to the chain of clusters represented
86 * by inode.
87 */
88int 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 Kara2f3d6752009-08-17 17:00:02 +0200127 * Since generic_write_sync() synchronizes regular files later,
128 * we sync here only directories.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
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 Karpov85c78592009-06-04 02:34:22 +0900138 fat_fs_error(sb, "clusters badly computed (%d != %llu)",
OGAWA Hirofumic3302932008-11-06 12:53:58 -0800139 new_fclus,
140 (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 fat_cache_inval_inode(inode);
142 }
143 inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
144
145 return 0;
146}
147
148extern struct timezone sys_tz;
149
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800150/*
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 Hirofumi7decd1c2008-11-06 12:53:47 -0800163/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700169/* Linear day numbers of the respective 1sts in non-leap years. */
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800170static 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 Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800175/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
176void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
177 __le16 __time, __le16 __date, u8 time_cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800179 u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
180 time_t second, day, leap_day, month, year;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800182 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 Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800211/* Convert linear UNIX date to a FAT time/date pair. */
212void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
213 __le16 *time, __le16 *date, u8 *time_cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Zhaolei1d81a182009-12-15 16:46:57 -0800215 struct tm tm;
216 time_to_tm(ts->tv_sec, sbi->options.tz_utc ? 0 :
217 -sys_tz.tz_minuteswest * 60, &tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Zhaolei1d81a182009-12-15 16:46:57 -0800219 /* FAT can only support year between 1980 to 2107 */
220 if (tm.tm_year < 1980 - 1900) {
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800221 *time = 0;
222 *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
223 if (time_cs)
224 *time_cs = 0;
225 return;
226 }
Zhaolei1d81a182009-12-15 16:46:57 -0800227 if (tm.tm_year > 2107 - 1900) {
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -0800228 *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 Torvalds1da177e2005-04-16 15:20:36 -0700234
Zhaolei1d81a182009-12-15 16:46:57 -0800235 /* 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 Hirofumi7decd1c2008-11-06 12:53:47 -0800241
Zhaolei1d81a182009-12-15 16:46:57 -0800242 *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 Hirofumi7decd1c2008-11-06 12:53:47 -0800244 if (time_cs)
245 *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
246}
247EXPORT_SYMBOL_GPL(fat_time_unix2fat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
250{
OGAWA Hirofumi5b002262006-02-03 03:04:42 -0800251 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200253 for (i = 0; i < nr_bhs; i++)
254 write_dirty_buffer(bhs[i], WRITE);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 for (i = 0; i < nr_bhs; i++) {
257 wait_on_buffer(bhs[i]);
Christoph Hellwig0edd55f2010-08-18 05:29:23 -0400258 if (!err && !buffer_uptodate(bhs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 err = -EIO;
260 }
261 return err;
262}