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/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, ...)
22 printk(KERN_ERR "FAT: Filesystem panic (dev %s)\n", s->s_id);
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 void lock_fat(struct super_block *sb)
38 down(&(MSDOS_SB(sb)->fat_lock));
41 void unlock_fat(struct super_block *sb)
43 up(&(MSDOS_SB(sb)->fat_lock));
46 /* Flushes the number of free clusters on FAT32 */
47 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
48 void fat_clusters_flush(struct super_block *sb)
50 struct msdos_sb_info *sbi = MSDOS_SB(sb);
51 struct buffer_head *bh;
52 struct fat_boot_fsinfo *fsinfo;
54 if (sbi->fat_bits != 32)
57 bh = sb_bread(sb, sbi->fsinfo_sector);
59 printk(KERN_ERR "FAT bread failed in fat_clusters_flush\n");
63 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
65 if (!IS_FSINFO(fsinfo)) {
66 printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n"
67 " Found signature1 0x%08x signature2 0x%08x"
69 le32_to_cpu(fsinfo->signature1),
70 le32_to_cpu(fsinfo->signature2),
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);
83 * fat_add_cluster tries to allocate a new cluster and adds it to the
84 * file represented by inode.
86 int fat_add_cluster(struct inode *inode)
88 struct super_block *sb = inode->i_sb;
89 struct msdos_sb_info *sbi = MSDOS_SB(sb);
90 int ret, count, limit, new_dclus, new_fclus, last;
91 int cluster_bits = sbi->cluster_bits;
94 * We must locate the last cluster of the file to add this new
95 * one (new_dclus) to the end of the link list (the FAT).
97 * In order to confirm that the cluster chain is valid, we
100 last = new_fclus = 0;
101 if (MSDOS_I(inode)->i_start) {
102 int ret, fclus, dclus;
104 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
107 new_fclus = fclus + 1;
111 /* find free FAT entry */
114 if (sbi->free_clusters == 0) {
119 limit = sbi->max_cluster;
120 new_dclus = sbi->prev_free + 1;
121 for (count = FAT_START_ENT; count < limit; count++, new_dclus++) {
122 new_dclus = new_dclus % limit;
123 if (new_dclus < FAT_START_ENT)
124 new_dclus = FAT_START_ENT;
126 ret = fat_access(sb, new_dclus, -1);
130 } else if (ret == FAT_ENT_FREE)
133 if (count >= limit) {
134 sbi->free_clusters = 0;
139 ret = fat_access(sb, new_dclus, FAT_ENT_EOF);
145 sbi->prev_free = new_dclus;
146 if (sbi->free_clusters != -1)
147 sbi->free_clusters--;
148 fat_clusters_flush(sb);
152 /* add new one to the last of the cluster chain */
154 ret = fat_access(sb, last, new_dclus);
157 // fat_cache_add(inode, new_fclus, new_dclus);
159 MSDOS_I(inode)->i_start = new_dclus;
160 MSDOS_I(inode)->i_logstart = new_dclus;
161 mark_inode_dirty(inode);
163 if (new_fclus != (inode->i_blocks >> (cluster_bits - 9))) {
164 fat_fs_panic(sb, "clusters badly computed (%d != %lu)",
165 new_fclus, inode->i_blocks >> (cluster_bits - 9));
166 fat_cache_inval_inode(inode);
168 inode->i_blocks += sbi->cluster_size >> 9;
173 extern struct timezone sys_tz;
175 /* Linear day numbers of the respective 1sts in non-leap years. */
176 static int day_n[] = {
177 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
178 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
181 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
182 int date_dos2unix(unsigned short time, unsigned short date)
184 int month, year, secs;
187 * first subtract and mask after that... Otherwise, if
188 * date == 0, bad things happen
190 month = ((date >> 5) - 1) & 15;
192 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
193 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
194 month < 2 ? 1 : 0)+3653);
195 /* days since 1.1.70 plus 80's leap day */
196 secs += sys_tz.tz_minuteswest*60;
200 /* Convert linear UNIX date to a MS-DOS time/date pair. */
201 void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
203 int day, year, nl_day, month;
205 unix_date -= sys_tz.tz_minuteswest*60;
207 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
208 if (unix_date < 315532800)
209 unix_date = 315532800;
211 *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
212 (((unix_date/3600) % 24) << 11));
213 day = unix_date/86400-3652;
215 if ((year+3)/4+365*year > day)
217 day -= (year+3)/4+365*year;
218 if (day == 59 && !(year & 3)) {
222 nl_day = (year & 3) || day <= 59 ? day : day-1;
223 for (month = 0; month < 12; month++) {
224 if (day_n[month] > nl_day)
228 *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
231 EXPORT_SYMBOL(fat_date_unix2dos);
233 /* Returns the inode number of the directory entry at offset pos. If bh is
234 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
236 AV. Most often we do it item-by-item. Makes sense to optimize.
237 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
238 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
239 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
240 AV. Additionally, when we return -1 (i.e. reached the end of directory)
244 int fat__get_entry(struct inode *dir, loff_t *pos, struct buffer_head **bh,
245 struct msdos_dir_entry **de, loff_t *i_pos)
247 struct super_block *sb = dir->i_sb;
248 struct msdos_sb_info *sbi = MSDOS_SB(sb);
249 sector_t phys, iblock;
259 iblock = *pos >> sb->s_blocksize_bits;
260 err = fat_bmap(dir, iblock, &phys);
262 return -1; /* beyond EOF or error */
264 *bh = sb_bread(sb, phys);
266 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
267 (unsigned long long)phys);
268 /* skip this block */
269 *pos = (iblock + 1) << sb->s_blocksize_bits;
273 offset &= sb->s_blocksize - 1;
274 *pos += sizeof(struct msdos_dir_entry);
275 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
276 *i_pos = ((loff_t)phys << sbi->dir_per_block_bits) + (offset >> MSDOS_DIR_BITS);
281 EXPORT_SYMBOL(fat__get_entry);