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