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)
9 #include <linux/module.h>
11 #include <linux/buffer_head.h>
12 #include <linux/genhd.h>
15 /* Copied from block/gendisk.c */
16 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
18 char event
[] = "DISK_RO=1";
19 char *envp
[] = { event
, NULL
};
23 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
27 * fat_fs_error reports a file system problem that might indicate fa data
28 * corruption/inconsistency. Depending on 'errors' mount option the
29 * panic() is called, or error message is printed FAT and nothing is done,
30 * or filesystem is remounted read-only (default behavior).
31 * In case the file system is remounted read-only, it can be made writable
32 * again by remounting it.
34 void fat_fs_error(struct super_block
*s
, const char *fmt
, ...)
36 struct fat_mount_options
*opts
= &MSDOS_SB(s
)->options
;
39 printk(KERN_ERR
"FAT: Filesystem error (dev %s)\n", s
->s_id
);
47 if (opts
->errors
== FAT_ERRORS_PANIC
)
48 panic(" FAT fs panic from previous error\n");
49 else if (opts
->errors
== FAT_ERRORS_RO
&& !(s
->s_flags
& MS_RDONLY
)) {
50 s
->s_flags
|= MS_RDONLY
;
51 printk(KERN_ERR
" File system has been set read-only\n");
52 set_disk_ro_uevent(s
->s_bdev
->bd_disk
, 1);
55 EXPORT_SYMBOL_GPL(fat_fs_error
);
57 /* Flushes the number of free clusters on FAT32 */
58 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
59 int fat_clusters_flush(struct super_block
*sb
)
61 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
62 struct buffer_head
*bh
;
63 struct fat_boot_fsinfo
*fsinfo
;
65 if (sbi
->fat_bits
!= 32)
68 bh
= sb_bread(sb
, sbi
->fsinfo_sector
);
70 printk(KERN_ERR
"FAT: bread failed in fat_clusters_flush\n");
74 fsinfo
= (struct fat_boot_fsinfo
*)bh
->b_data
;
76 if (!IS_FSINFO(fsinfo
)) {
77 printk(KERN_ERR
"FAT: Invalid FSINFO signature: "
78 "0x%08x, 0x%08x (sector = %lu)\n",
79 le32_to_cpu(fsinfo
->signature1
),
80 le32_to_cpu(fsinfo
->signature2
),
83 if (sbi
->free_clusters
!= -1)
84 fsinfo
->free_clusters
= cpu_to_le32(sbi
->free_clusters
);
85 if (sbi
->prev_free
!= -1)
86 fsinfo
->next_cluster
= cpu_to_le32(sbi
->prev_free
);
87 mark_buffer_dirty(bh
);
95 * fat_chain_add() adds a new cluster to the chain of clusters represented
98 int fat_chain_add(struct inode
*inode
, int new_dclus
, int nr_cluster
)
100 struct super_block
*sb
= inode
->i_sb
;
101 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
102 int ret
, new_fclus
, last
;
105 * We must locate the last cluster of the file to add this new
106 * one (new_dclus) to the end of the link list (the FAT).
108 last
= new_fclus
= 0;
109 if (MSDOS_I(inode
)->i_start
) {
112 ret
= fat_get_cluster(inode
, FAT_ENT_EOF
, &fclus
, &dclus
);
115 new_fclus
= fclus
+ 1;
119 /* add new one to the last of the cluster chain */
121 struct fat_entry fatent
;
123 fatent_init(&fatent
);
124 ret
= fat_ent_read(inode
, &fatent
, last
);
126 int wait
= inode_needs_sync(inode
);
127 ret
= fat_ent_write(inode
, &fatent
, new_dclus
, wait
);
128 fatent_brelse(&fatent
);
132 // fat_cache_add(inode, new_fclus, new_dclus);
134 MSDOS_I(inode
)->i_start
= new_dclus
;
135 MSDOS_I(inode
)->i_logstart
= new_dclus
;
137 * Since generic_write_sync() synchronizes regular files later,
138 * we sync here only directories.
140 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
)) {
141 ret
= fat_sync_inode(inode
);
145 mark_inode_dirty(inode
);
147 if (new_fclus
!= (inode
->i_blocks
>> (sbi
->cluster_bits
- 9))) {
148 fat_fs_error(sb
, "clusters badly computed (%d != %llu)",
150 (llu
)(inode
->i_blocks
>> (sbi
->cluster_bits
- 9)));
151 fat_cache_inval_inode(inode
);
153 inode
->i_blocks
+= nr_cluster
<< (sbi
->cluster_bits
- 9);
158 extern struct timezone sys_tz
;
161 * The epoch of FAT timestamp is 1980.
163 * date: 0 - 4: day (1 - 31)
164 * date: 5 - 8: month (1 - 12)
165 * date: 9 - 15: year (0 - 127) from 1980
166 * time: 0 - 4: sec (0 - 29) 2sec counts
167 * time: 5 - 10: min (0 - 59)
168 * time: 11 - 15: hour (0 - 23)
170 #define SECS_PER_MIN 60
171 #define SECS_PER_HOUR (60 * 60)
172 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
173 #define UNIX_SECS_1980 315532800L
174 #if BITS_PER_LONG == 64
175 #define UNIX_SECS_2108 4354819200L
177 /* days between 1.1.70 and 1.1.80 (2 leap days) */
178 #define DAYS_DELTA (365 * 10 + 2)
179 /* 120 (2100 - 1980) isn't leap year */
180 #define YEAR_2100 120
181 #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
183 /* Linear day numbers of the respective 1sts in non-leap years. */
184 static time_t days_in_year
[] = {
185 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
186 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
189 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
190 void fat_time_fat2unix(struct msdos_sb_info
*sbi
, struct timespec
*ts
,
191 __le16 __time
, __le16 __date
, u8 time_cs
)
193 u16 time
= le16_to_cpu(__time
), date
= le16_to_cpu(__date
);
194 time_t second
, day
, leap_day
, month
, year
;
197 month
= max(1, (date
>> 5) & 0xf);
198 day
= max(1, date
& 0x1f) - 1;
200 leap_day
= (year
+ 3) / 4;
201 if (year
> YEAR_2100
) /* 2100 isn't leap year */
203 if (IS_LEAP_YEAR(year
) && month
> 2)
206 second
= (time
& 0x1f) << 1;
207 second
+= ((time
>> 5) & 0x3f) * SECS_PER_MIN
;
208 second
+= (time
>> 11) * SECS_PER_HOUR
;
209 second
+= (year
* 365 + leap_day
210 + days_in_year
[month
] + day
211 + DAYS_DELTA
) * SECS_PER_DAY
;
213 if (!sbi
->options
.tz_utc
)
214 second
+= sys_tz
.tz_minuteswest
* SECS_PER_MIN
;
217 ts
->tv_sec
= second
+ (time_cs
/ 100);
218 ts
->tv_nsec
= (time_cs
% 100) * 10000000;
225 /* Convert linear UNIX date to a FAT time/date pair. */
226 void fat_time_unix2fat(struct msdos_sb_info
*sbi
, struct timespec
*ts
,
227 __le16
*time
, __le16
*date
, u8
*time_cs
)
229 time_t second
= ts
->tv_sec
;
230 time_t day
, leap_day
, month
, year
;
232 if (!sbi
->options
.tz_utc
)
233 second
-= sys_tz
.tz_minuteswest
* SECS_PER_MIN
;
235 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
236 if (second
< UNIX_SECS_1980
) {
238 *date
= cpu_to_le16((0 << 9) | (1 << 5) | 1);
243 #if BITS_PER_LONG == 64
244 if (second
>= UNIX_SECS_2108
) {
245 *time
= cpu_to_le16((23 << 11) | (59 << 5) | 29);
246 *date
= cpu_to_le16((127 << 9) | (12 << 5) | 31);
253 day
= second
/ SECS_PER_DAY
- DAYS_DELTA
;
255 leap_day
= (year
+ 3) / 4;
256 if (year
> YEAR_2100
) /* 2100 isn't leap year */
258 if (year
* 365 + leap_day
> day
)
260 leap_day
= (year
+ 3) / 4;
261 if (year
> YEAR_2100
) /* 2100 isn't leap year */
263 day
-= year
* 365 + leap_day
;
265 if (IS_LEAP_YEAR(year
) && day
== days_in_year
[3]) {
268 if (IS_LEAP_YEAR(year
) && day
> days_in_year
[3])
270 for (month
= 1; month
< 12; month
++) {
271 if (days_in_year
[month
+ 1] > day
)
275 day
-= days_in_year
[month
];
277 *time
= cpu_to_le16(((second
/ SECS_PER_HOUR
) % 24) << 11
278 | ((second
/ SECS_PER_MIN
) % 60) << 5
279 | (second
% SECS_PER_MIN
) >> 1);
280 *date
= cpu_to_le16((year
<< 9) | (month
<< 5) | (day
+ 1));
282 *time_cs
= (ts
->tv_sec
& 1) * 100 + ts
->tv_nsec
/ 10000000;
284 EXPORT_SYMBOL_GPL(fat_time_unix2fat
);
286 int fat_sync_bhs(struct buffer_head
**bhs
, int nr_bhs
)
290 ll_rw_block(SWRITE
, nr_bhs
, bhs
);
291 for (i
= 0; i
< nr_bhs
; i
++) {
292 wait_on_buffer(bhs
[i
]);
293 if (buffer_eopnotsupp(bhs
[i
])) {
294 clear_buffer_eopnotsupp(bhs
[i
]);
296 } else if (!err
&& !buffer_uptodate(bhs
[i
]))