Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6/kmemtrace.git] / fs / fat / misc.c
blob61f23511eacf41ec34649a84f1cc7e601ff61a68
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/msdos_fs.h>
12 #include <linux/buffer_head.h>
15 * fat_fs_panic reports a severe file system problem and sets the file system
16 * read-only. The file system can be made writable again by remounting it.
18 void fat_fs_panic(struct super_block *s, const char *fmt, ...)
20 va_list args;
22 printk(KERN_ERR "FAT: Filesystem panic (dev %s)\n", s->s_id);
24 printk(KERN_ERR " ");
25 va_start(args, fmt);
26 vprintk(fmt, args);
27 va_end(args);
28 printk("\n");
30 if (!(s->s_flags & MS_RDONLY)) {
31 s->s_flags |= MS_RDONLY;
32 printk(KERN_ERR " File system has been set read-only\n");
36 EXPORT_SYMBOL_GPL(fat_fs_panic);
38 /* Flushes the number of free clusters on FAT32 */
39 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
40 void fat_clusters_flush(struct super_block *sb)
42 struct msdos_sb_info *sbi = MSDOS_SB(sb);
43 struct buffer_head *bh;
44 struct fat_boot_fsinfo *fsinfo;
46 if (sbi->fat_bits != 32)
47 return;
49 bh = sb_bread(sb, sbi->fsinfo_sector);
50 if (bh == NULL) {
51 printk(KERN_ERR "FAT: bread failed in fat_clusters_flush\n");
52 return;
55 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
56 /* Sanity check */
57 if (!IS_FSINFO(fsinfo)) {
58 printk(KERN_ERR "FAT: Invalid FSINFO signature: "
59 "0x%08x, 0x%08x (sector = %lu)\n",
60 le32_to_cpu(fsinfo->signature1),
61 le32_to_cpu(fsinfo->signature2),
62 sbi->fsinfo_sector);
63 } else {
64 if (sbi->free_clusters != -1)
65 fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
66 if (sbi->prev_free != -1)
67 fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
68 mark_buffer_dirty(bh);
70 brelse(bh);
74 * fat_chain_add() adds a new cluster to the chain of clusters represented
75 * by inode.
77 int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
79 struct super_block *sb = inode->i_sb;
80 struct msdos_sb_info *sbi = MSDOS_SB(sb);
81 int ret, new_fclus, last;
84 * We must locate the last cluster of the file to add this new
85 * one (new_dclus) to the end of the link list (the FAT).
87 last = new_fclus = 0;
88 if (MSDOS_I(inode)->i_start) {
89 int fclus, dclus;
91 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
92 if (ret < 0)
93 return ret;
94 new_fclus = fclus + 1;
95 last = dclus;
98 /* add new one to the last of the cluster chain */
99 if (last) {
100 struct fat_entry fatent;
102 fatent_init(&fatent);
103 ret = fat_ent_read(inode, &fatent, last);
104 if (ret >= 0) {
105 int wait = inode_needs_sync(inode);
106 ret = fat_ent_write(inode, &fatent, new_dclus, wait);
107 fatent_brelse(&fatent);
109 if (ret < 0)
110 return ret;
111 // fat_cache_add(inode, new_fclus, new_dclus);
112 } else {
113 MSDOS_I(inode)->i_start = new_dclus;
114 MSDOS_I(inode)->i_logstart = new_dclus;
116 * Since generic_osync_inode() synchronize later if
117 * this is not directory, we don't here.
119 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) {
120 ret = fat_sync_inode(inode);
121 if (ret)
122 return ret;
123 } else
124 mark_inode_dirty(inode);
126 if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) {
127 fat_fs_panic(sb, "clusters badly computed (%d != %lu)",
128 new_fclus, inode->i_blocks >> (sbi->cluster_bits - 9));
129 fat_cache_inval_inode(inode);
131 inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9);
133 return 0;
136 extern struct timezone sys_tz;
138 /* Linear day numbers of the respective 1sts in non-leap years. */
139 static int day_n[] = {
140 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
141 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
144 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
145 int date_dos2unix(unsigned short time, unsigned short date)
147 int month, year, secs;
150 * first subtract and mask after that... Otherwise, if
151 * date == 0, bad things happen
153 month = ((date >> 5) - 1) & 15;
154 year = date >> 9;
155 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
156 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
157 month < 2 ? 1 : 0)+3653);
158 /* days since 1.1.70 plus 80's leap day */
159 secs += sys_tz.tz_minuteswest*60;
160 return secs;
163 /* Convert linear UNIX date to a MS-DOS time/date pair. */
164 void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
166 int day, year, nl_day, month;
168 unix_date -= sys_tz.tz_minuteswest*60;
170 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
171 if (unix_date < 315532800)
172 unix_date = 315532800;
174 *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
175 (((unix_date/3600) % 24) << 11));
176 day = unix_date/86400-3652;
177 year = day/365;
178 if ((year+3)/4+365*year > day)
179 year--;
180 day -= (year+3)/4+365*year;
181 if (day == 59 && !(year & 3)) {
182 nl_day = day;
183 month = 2;
184 } else {
185 nl_day = (year & 3) || day <= 59 ? day : day-1;
186 for (month = 0; month < 12; month++) {
187 if (day_n[month] > nl_day)
188 break;
191 *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
194 EXPORT_SYMBOL_GPL(fat_date_unix2dos);
196 int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
198 int i, err = 0;
200 ll_rw_block(SWRITE, nr_bhs, bhs);
201 for (i = 0; i < nr_bhs; i++) {
202 wait_on_buffer(bhs[i]);
203 if (buffer_eopnotsupp(bhs[i])) {
204 clear_buffer_eopnotsupp(bhs[i]);
205 err = -EOPNOTSUPP;
206 } else if (!err && !buffer_uptodate(bhs[i]))
207 err = -EIO;
209 return err;