1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/buffer_head.h>
12#include <linux/time.h>
13#include "fat.h"
14
15
16
17
18
19
20
21
22
23void __fat_fs_error(struct super_block *s, int report, const char *fmt, ...)
24{
25 struct fat_mount_options *opts = &MSDOS_SB(s)->options;
26 va_list args;
27
28 if (report) {
29 printk(KERN_ERR "FAT: Filesystem error (dev %s)\n", s->s_id);
30
31 printk(KERN_ERR " ");
32 va_start(args, fmt);
33 vprintk(fmt, args);
34 va_end(args);
35 printk("\n");
36 }
37
38 if (opts->errors == FAT_ERRORS_PANIC)
39 panic("FAT: fs panic from previous error\n");
40 else if (opts->errors == FAT_ERRORS_RO && !(s->s_flags & MS_RDONLY)) {
41 s->s_flags |= MS_RDONLY;
42 printk(KERN_ERR "FAT: Filesystem has been set read-only\n");
43 }
44}
45EXPORT_SYMBOL_GPL(__fat_fs_error);
46
47
48
49int fat_clusters_flush(struct super_block *sb)
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)
56 return 0;
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");
61 return -EIO;
62 }
63
64 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
65
66 if (!IS_FSINFO(fsinfo)) {
67 printk(KERN_ERR "FAT: Invalid FSINFO signature: "
68 "0x%08x, 0x%08x (sector = %lu)\n",
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);
78 }
79 brelse(bh);
80
81 return 0;
82}
83
84
85
86
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
96
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
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
123 } else {
124 MSDOS_I(inode)->i_start = new_dclus;
125 MSDOS_I(inode)->i_logstart = new_dclus;
126
127
128
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))) {
138 fat_fs_error(sb, "clusters badly computed (%d != %llu)",
139 new_fclus,
140 (llu)(inode->i_blocks >> (sbi->cluster_bits - 9)));
141 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
150
151
152
153
154
155
156
157
158
159
160#define SECS_PER_MIN 60
161#define SECS_PER_HOUR (60 * 60)
162#define SECS_PER_DAY (SECS_PER_HOUR * 24)
163
164#define DAYS_DELTA (365 * 10 + 2)
165
166#define YEAR_2100 120
167#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
168
169
170static time_t days_in_year[] = {
171
172 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
173};
174
175
176void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
177 __le16 __time, __le16 __date, u8 time_cs)
178{
179 u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
180 time_t second, day, leap_day, month, year;
181
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)
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 }
209}
210
211
212void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
213 __le16 *time, __le16 *date, u8 *time_cs)
214{
215 struct tm tm;
216 time_to_tm(ts->tv_sec, sbi->options.tz_utc ? 0 :
217 -sys_tz.tz_minuteswest * 60, &tm);
218
219
220 if (tm.tm_year < 1980 - 1900) {
221 *time = 0;
222 *date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
223 if (time_cs)
224 *time_cs = 0;
225 return;
226 }
227 if (tm.tm_year > 2107 - 1900) {
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 }
234
235
236 tm.tm_year -= 80;
237
238 tm.tm_mon++;
239
240 tm.tm_sec >>= 1;
241
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);
244 if (time_cs)
245 *time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
246}
247EXPORT_SYMBOL_GPL(fat_time_unix2fat);
248
249int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
250{
251 int i, err = 0;
252
253 for (i = 0; i < nr_bhs; i++)
254 write_dirty_buffer(bhs[i], WRITE);
255
256 for (i = 0; i < nr_bhs; i++) {
257 wait_on_buffer(bhs[i]);
258 if (!err && !buffer_uptodate(bhs[i]))
259 err = -EIO;
260 }
261 return err;
262}
263