MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / fat / prince-fat / fat-patch / misc.c
blobafb1228cc8446b7e48dcee2a2242e6803c04be20
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/fs.h>
10 #include <linux/msdos_fs.h>
11 #include <linux/buffer_head.h>
14 * fat_fs_panic reports a severe file system problem and sets the file system
15 * read-only. The file system can be made writable again by remounting it.
18 static char panic_msg[512];
20 void fat_fs_panic(struct super_block *s, const char *fmt, ...)
22 int not_ro;
23 va_list args;
25 va_start (args, fmt);
26 vsnprintf (panic_msg, sizeof(panic_msg), fmt, args);
27 va_end (args);
29 not_ro = !(s->s_flags & MS_RDONLY);
30 if (not_ro)
31 s->s_flags |= MS_RDONLY;
33 printk(KERN_ERR "FAT: Filesystem panic (dev %s)\n"
34 " %s\n", s->s_id, panic_msg);
35 if (not_ro)
36 printk(KERN_ERR " File system has been set read-only\n");
39 void lock_fat(struct super_block *sb)
41 down(&(MSDOS_SB(sb)->fat_lock));
44 void unlock_fat(struct super_block *sb)
46 up(&(MSDOS_SB(sb)->fat_lock));
49 /* Flushes the number of free clusters on FAT32 */
50 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
51 void fat_clusters_flush(struct super_block *sb)
53 struct msdos_sb_info *sbi = MSDOS_SB(sb);
54 struct buffer_head *bh;
55 struct fat_boot_fsinfo *fsinfo;
57 if (sbi->fat_bits != 32)
58 return;
60 bh = sb_bread(sb, sbi->fsinfo_sector);
61 if (bh == NULL) {
62 printk(KERN_ERR "FAT bread failed in fat_clusters_flush\n");
63 return;
66 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
67 /* Sanity check */
68 if (!IS_FSINFO(fsinfo)) {
69 printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n"
70 " Found signature1 0x%08x signature2 0x%08x"
71 " (sector = %lu)\n",
72 CF_LE_L(fsinfo->signature1), CF_LE_L(fsinfo->signature2),
73 sbi->fsinfo_sector);
74 } else {
75 if (sbi->free_clusters != -1)
76 fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
77 if (sbi->prev_free != -1)
78 fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
79 mark_buffer_dirty(bh);
81 brelse(bh);
85 * fat_add_cluster tries to allocate a new cluster and adds it to the
86 * file represented by inode.
88 int fat_add_cluster(struct inode *inode)
90 struct super_block *sb = inode->i_sb;
91 int ret, count, limit, new_dclus, new_fclus, last;
92 int cluster_bits = MSDOS_SB(sb)->cluster_bits;
94 /*
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 * In order to confirm that the cluster chain is valid, we
99 * find out EOF first.
103 last = new_fclus = 0;
104 if (MSDOS_I(inode)->i_start) {
105 int ret, fclus, dclus;
107 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
108 if (ret < 0)
109 return ret;
110 new_fclus = fclus + 1;
111 last = dclus;
114 /* find free FAT entry */
115 lock_fat(sb);
117 if (MSDOS_SB(sb)->free_clusters == 0) {
118 unlock_fat(sb);
119 return -ENOSPC;
122 limit = MSDOS_SB(sb)->clusters + 2;
123 new_dclus = MSDOS_SB(sb)->prev_free + 1;
124 for (count = 0; count < MSDOS_SB(sb)->clusters; count++, new_dclus++) {
125 new_dclus = new_dclus % limit;
126 if (new_dclus < 2)
127 new_dclus = 2;
129 ret = fat_access(sb, new_dclus, -1);
130 if (ret < 0) {
131 unlock_fat(sb);
132 return ret;
133 } else if (ret == FAT_ENT_FREE)
134 break;
136 if (count >= MSDOS_SB(sb)->clusters) {
137 MSDOS_SB(sb)->free_clusters = 0;
138 unlock_fat(sb);
139 return -ENOSPC;
142 ret = fat_access(sb, new_dclus, FAT_ENT_EOF);
143 if (ret < 0) {
144 unlock_fat(sb);
145 return ret;
148 MSDOS_SB(sb)->prev_free = new_dclus;
149 if (MSDOS_SB(sb)->free_clusters != -1)
150 MSDOS_SB(sb)->free_clusters--;
151 fat_clusters_flush(sb);
153 unlock_fat(sb);
155 /* add new one to the last of the cluster chain */
156 if (last) {
157 ret = fat_access(sb, last, new_dclus);
158 if (ret < 0)
159 return ret;
160 /*===prince delete debug begin
161 fat_cache_add(inode, new_fclus, new_dclus);
162 =====prince delte debug end*/
163 //prince add debug begin
164 // fat_cache_add(inode, new_fclus, new_dclus);
165 //prince add debug end
166 } else {
167 MSDOS_I(inode)->i_start = new_dclus;
168 MSDOS_I(inode)->i_logstart = new_dclus;
169 mark_inode_dirty(inode);
171 if (new_fclus != (inode->i_blocks >> (cluster_bits - 9))) {
172 fat_fs_panic(sb, "clusters badly computed (%d != %lu)",
173 new_fclus, inode->i_blocks >> (cluster_bits - 9));
174 fat_cache_inval_inode(inode);
176 inode->i_blocks += MSDOS_SB(sb)->cluster_size >> 9;
178 return new_dclus;
181 struct buffer_head *fat_extend_dir(struct inode *inode)
183 struct super_block *sb = inode->i_sb;
184 struct buffer_head *bh, *res = NULL;
185 int nr, sec_per_clus = MSDOS_SB(sb)->sec_per_clus;
186 sector_t sector, last_sector;
189 if (MSDOS_SB(sb)->fat_bits != 32) {
190 if (inode->i_ino == MSDOS_ROOT_INO)
191 return ERR_PTR(-ENOSPC);
194 nr = fat_add_cluster(inode);
195 if (nr < 0)
196 return ERR_PTR(nr);
198 sector = ((sector_t)nr - 2) * sec_per_clus + MSDOS_SB(sb)->data_start;
199 last_sector = sector + sec_per_clus;
200 for ( ; sector < last_sector; sector++) {
201 if ((bh = sb_getblk(sb, sector))) {
202 memset(bh->b_data, 0, sb->s_blocksize);
203 set_buffer_uptodate(bh);
204 mark_buffer_dirty(bh);
205 if (!res)
206 res = bh;
207 else
208 brelse(bh);
211 if (res == NULL)
212 res = ERR_PTR(-EIO);
213 if (inode->i_size & (sb->s_blocksize - 1)) {
214 fat_fs_panic(sb, "Odd directory size");
215 inode->i_size = (inode->i_size + sb->s_blocksize)
216 & ~((loff_t)sb->s_blocksize - 1);
218 inode->i_size += MSDOS_SB(sb)->cluster_size;
219 MSDOS_I(inode)->mmu_private += MSDOS_SB(sb)->cluster_size;
221 return res;
224 /* Linear day numbers of the respective 1sts in non-leap years. */
226 static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
227 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
230 extern struct timezone sys_tz;
233 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
235 int date_dos2unix(unsigned short time,unsigned short date)
237 int month,year,secs;
239 /* first subtract and mask after that... Otherwise, if
240 date == 0, bad things happen */
241 month = ((date >> 5) - 1) & 15;
242 year = date >> 9;
243 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
244 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
245 month < 2 ? 1 : 0)+3653);
246 /* days since 1.1.70 plus 80's leap day */
247 secs += sys_tz.tz_minuteswest*60;
248 return secs;
252 /* Convert linear UNIX date to a MS-DOS time/date pair. */
254 void fat_date_unix2dos(int unix_date,__le16 *time, __le16 *date)
256 int day,year,nl_day,month;
258 unix_date -= sys_tz.tz_minuteswest*60;
260 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
261 if (unix_date < 315532800)
262 unix_date = 315532800;
264 *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
265 (((unix_date/3600) % 24) << 11));
266 day = unix_date/86400-3652;
267 year = day/365;
268 if ((year+3)/4+365*year > day) year--;
269 day -= (year+3)/4+365*year;
270 if (day == 59 && !(year & 3)) {
271 nl_day = day;
272 month = 2;
274 else {
275 nl_day = (year & 3) || day <= 59 ? day : day-1;
276 for (month = 0; month < 12; month++)
277 if (day_n[month] > nl_day) break;
279 *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
283 /* Returns the inode number of the directory entry at offset pos. If bh is
284 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
285 returned in bh.
286 AV. Most often we do it item-by-item. Makes sense to optimize.
287 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
288 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
289 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
290 AV. Additionally, when we return -1 (i.e. reached the end of directory)
291 AV. we make bh NULL.
294 int fat__get_entry(struct inode *dir, loff_t *pos,struct buffer_head **bh,
295 struct msdos_dir_entry **de, loff_t *i_pos)
297 struct super_block *sb = dir->i_sb;
298 struct msdos_sb_info *sbi = MSDOS_SB(sb);
299 sector_t phys, iblock;
300 loff_t offset;
301 int err;
303 next:
304 offset = *pos;
305 if (*bh)
306 brelse(*bh);
308 *bh = NULL;
309 iblock = *pos >> sb->s_blocksize_bits;
310 err = fat_bmap(dir, iblock, &phys);
311 if (err || !phys)
312 return -1; /* beyond EOF or error */
314 *bh = sb_bread(sb, phys);
315 if (*bh == NULL) {
316 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
317 (unsigned long long)phys);
318 /* skip this block */
319 *pos = (iblock + 1) << sb->s_blocksize_bits;
320 goto next;
323 offset &= sb->s_blocksize - 1;
324 *pos += sizeof(struct msdos_dir_entry);
325 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
326 *i_pos = ((loff_t)phys << sbi->dir_per_block_bits) + (offset >> MSDOS_DIR_BITS);
328 return 0;