staging: Use static const char * const where possible
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / fat / misc.c
blob1736f23563888b2f0225a6add77d45c984270231
1 /*
2 * linux/fs/fat/misc.c
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 */
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"
16 * 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.
23 void __fat_fs_error(struct super_block *s, int report, const char *fmt, ...)
25 struct fat_mount_options *opts = &MSDOS_SB(s)->options;
26 va_list args;
28 if (report) {
29 printk(KERN_ERR "FAT: Filesystem error (dev %s)\n", s->s_id);
31 printk(KERN_ERR " ");
32 va_start(args, fmt);
33 vprintk(fmt, args);
34 va_end(args);
35 printk("\n");
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");
45 EXPORT_SYMBOL_GPL(__fat_fs_error);
47 /* Flushes the number of free clusters on FAT32 */
48 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
49 int fat_clusters_flush(struct super_block *sb)
51 struct msdos_sb_info *sbi = MSDOS_SB(sb);
52 struct buffer_head *bh;
53 struct fat_boot_fsinfo *fsinfo;
55 if (sbi->fat_bits != 32)
56 return 0;
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;
64 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
65 /* Sanity check */
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);
79 brelse(bh);
81 return 0;
85 * fat_chain_add() adds a new cluster to the chain of clusters represented
86 * by inode.
88 int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
90 struct super_block *sb = inode->i_sb;
91 struct msdos_sb_info *sbi = MSDOS_SB(sb);
92 int ret, new_fclus, last;
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).
98 last = new_fclus = 0;
99 if (MSDOS_I(inode)->i_start) {
100 int fclus, dclus;
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;
109 /* add new one to the last of the cluster chain */
110 if (last) {
111 struct fat_entry fatent;
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);
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;
127 * Since generic_write_sync() synchronizes regular files later,
128 * we sync here only directories.
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);
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);
143 inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
145 return 0;
148 extern struct timezone sys_tz;
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)
160 #define SECS_PER_MIN 60
161 #define SECS_PER_HOUR (60 * 60)
162 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
163 /* 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)
169 /* Linear day numbers of the respective 1sts in non-leap years. */
170 static 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,
175 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
176 void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,
177 __le16 __time, __le16 __date, u8 time_cs)
179 u16 time = le16_to_cpu(__time), date = le16_to_cpu(__date);
180 time_t second, day, leap_day, month, year;
182 year = date >> 9;
183 month = max(1, (date >> 5) & 0xf);
184 day = max(1, date & 0x1f) - 1;
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++;
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;
199 if (!sbi->options.tz_utc)
200 second += sys_tz.tz_minuteswest * SECS_PER_MIN;
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;
211 /* Convert linear UNIX date to a FAT time/date pair. */
212 void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
213 __le16 *time, __le16 *date, u8 *time_cs)
215 struct tm tm;
216 time_to_tm(ts->tv_sec, sbi->options.tz_utc ? 0 :
217 -sys_tz.tz_minuteswest * 60, &tm);
219 /* FAT can only support year between 1980 to 2107 */
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;
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;
235 /* 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;
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;
247 EXPORT_SYMBOL_GPL(fat_time_unix2fat);
249 int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
251 int i, err = 0;
253 for (i = 0; i < nr_bhs; i++)
254 write_dirty_buffer(bhs[i], WRITE);
256 for (i = 0; i < nr_bhs; i++) {
257 wait_on_buffer(bhs[i]);
258 if (buffer_eopnotsupp(bhs[i])) {
259 clear_buffer_eopnotsupp(bhs[i]);
260 err = -EOPNOTSUPP;
261 } else if (!err && !buffer_uptodate(bhs[i]))
262 err = -EIO;
264 return err;