[PATCH] fix make rpm versioning
[linux-2.6/history.git] / fs / fat / misc.c
blob7490bc844c1466fedd768db57a146063905a5101
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 buffer_head *bh;
54 struct fat_boot_fsinfo *fsinfo;
56 if (MSDOS_SB(sb)->fat_bits != 32)
57 return;
58 if (MSDOS_SB(sb)->free_clusters == -1)
59 return;
61 bh = sb_bread(sb, MSDOS_SB(sb)->fsinfo_sector);
62 if (bh == NULL) {
63 printk(KERN_ERR "FAT bread failed in fat_clusters_flush\n");
64 return;
67 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
68 /* Sanity check */
69 if (!IS_FSINFO(fsinfo)) {
70 printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n"
71 " Found signature1 0x%08x signature2 0x%08x"
72 " (sector = %lu)\n",
73 CF_LE_L(fsinfo->signature1), CF_LE_L(fsinfo->signature2),
74 MSDOS_SB(sb)->fsinfo_sector);
75 } else {
76 fsinfo->free_clusters = CF_LE_L(MSDOS_SB(sb)->free_clusters);
77 fsinfo->next_cluster = CF_LE_L(MSDOS_SB(sb)->prev_free);
78 mark_buffer_dirty(bh);
80 brelse(bh);
84 * fat_add_cluster tries to allocate a new cluster and adds it to the
85 * file represented by inode.
87 int fat_add_cluster(struct inode *inode)
89 struct super_block *sb = inode->i_sb;
90 int count, nr, limit, last, curr, file_cluster;
91 int cluster_bits = MSDOS_SB(sb)->cluster_bits;
93 /*
94 * We must locate the last cluster of the file to add this new
95 * one (nr) to the end of the link list (the FAT).
97 * In order to confirm that the cluster chain is valid, we
98 * find out EOF first.
100 nr = -EIO;
101 last = file_cluster = 0;
102 if ((curr = MSDOS_I(inode)->i_start) != 0) {
103 int max_cluster = MSDOS_I(inode)->mmu_private >> cluster_bits;
105 fat_cache_lookup(inode, INT_MAX, &last, &curr);
106 file_cluster = last;
107 while (curr && curr != FAT_ENT_EOF) {
108 file_cluster++;
109 curr = fat_access(sb, last = curr, -1);
110 if (curr < 0)
111 return curr;
112 else if (curr == FAT_ENT_FREE) {
113 fat_fs_panic(sb, "%s: invalid cluster chain"
114 " (ino %lu)",
115 __FUNCTION__, inode->i_ino);
116 goto out;
118 if (file_cluster > max_cluster) {
119 fat_fs_panic(sb, "%s: bad cluster counts"
120 " (ino %lu)",
121 __FUNCTION__, inode->i_ino);
122 goto out;
127 /* find free FAT entry */
128 lock_fat(sb);
130 if (MSDOS_SB(sb)->free_clusters == 0) {
131 unlock_fat(sb);
132 return -ENOSPC;
135 limit = MSDOS_SB(sb)->clusters + 2;
136 nr = MSDOS_SB(sb)->prev_free + 1;
137 for (count = 0; count < MSDOS_SB(sb)->clusters; count++, nr++) {
138 nr = nr % limit;
139 if (nr < 2)
140 nr = 2;
141 if (fat_access(sb, nr, -1) == FAT_ENT_FREE)
142 break;
144 if (count >= MSDOS_SB(sb)->clusters) {
145 MSDOS_SB(sb)->free_clusters = 0;
146 unlock_fat(sb);
147 return -ENOSPC;
149 MSDOS_SB(sb)->prev_free = nr;
151 fat_access(sb, nr, FAT_ENT_EOF);
152 if (MSDOS_SB(sb)->free_clusters != -1)
153 MSDOS_SB(sb)->free_clusters--;
154 fat_clusters_flush(sb);
156 unlock_fat(sb);
158 /* add new one to the last of the cluster chain */
159 if (last) {
160 fat_access(sb, last, nr);
161 fat_cache_add(inode, file_cluster, nr);
162 } else {
163 MSDOS_I(inode)->i_start = nr;
164 MSDOS_I(inode)->i_logstart = nr;
165 mark_inode_dirty(inode);
167 if (file_cluster != (inode->i_blocks >> (cluster_bits - 9))) {
168 printk (KERN_ERR "file_cluster badly computed!!! %d <> %ld\n",
169 file_cluster, inode->i_blocks >> (cluster_bits - 9));
170 fat_cache_inval_inode(inode);
172 inode->i_blocks += (1 << cluster_bits) >> 9;
174 out:
175 return nr;
178 struct buffer_head *fat_extend_dir(struct inode *inode)
180 struct super_block *sb = inode->i_sb;
181 struct buffer_head *bh, *res = NULL;
182 int nr, cluster_size = MSDOS_SB(sb)->cluster_size;
183 sector_t sector, last_sector;
185 if (MSDOS_SB(sb)->fat_bits != 32) {
186 if (inode->i_ino == MSDOS_ROOT_INO)
187 return ERR_PTR(-ENOSPC);
190 nr = fat_add_cluster(inode);
191 if (nr < 0)
192 return ERR_PTR(nr);
194 sector = ((sector_t)nr - 2) * cluster_size + MSDOS_SB(sb)->data_start;
195 last_sector = sector + cluster_size;
196 for ( ; sector < last_sector; sector++) {
197 if ((bh = sb_getblk(sb, sector))) {
198 memset(bh->b_data, 0, sb->s_blocksize);
199 set_buffer_uptodate(bh);
200 mark_buffer_dirty(bh);
201 if (!res)
202 res = bh;
203 else
204 brelse(bh);
207 if (res == NULL)
208 res = ERR_PTR(-EIO);
209 if (inode->i_size & (sb->s_blocksize - 1)) {
210 fat_fs_panic(sb, "Odd directory size");
211 inode->i_size = (inode->i_size + sb->s_blocksize)
212 & ~(sb->s_blocksize - 1);
214 inode->i_size += 1 << MSDOS_SB(sb)->cluster_bits;
215 MSDOS_I(inode)->mmu_private += 1 << MSDOS_SB(sb)->cluster_bits;
217 return res;
220 /* Linear day numbers of the respective 1sts in non-leap years. */
222 static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
223 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
226 extern struct timezone sys_tz;
229 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
231 int date_dos2unix(unsigned short time,unsigned short date)
233 int month,year,secs;
235 /* first subtract and mask after that... Otherwise, if
236 date == 0, bad things happen */
237 month = ((date >> 5) - 1) & 15;
238 year = date >> 9;
239 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
240 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
241 month < 2 ? 1 : 0)+3653);
242 /* days since 1.1.70 plus 80's leap day */
243 secs += sys_tz.tz_minuteswest*60;
244 return secs;
248 /* Convert linear UNIX date to a MS-DOS time/date pair. */
250 void fat_date_unix2dos(int unix_date,unsigned short *time,
251 unsigned short *date)
253 int day,year,nl_day,month;
255 unix_date -= sys_tz.tz_minuteswest*60;
257 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
258 if (unix_date < 315532800)
259 unix_date = 315532800;
261 *time = (unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
262 (((unix_date/3600) % 24) << 11);
263 day = unix_date/86400-3652;
264 year = day/365;
265 if ((year+3)/4+365*year > day) year--;
266 day -= (year+3)/4+365*year;
267 if (day == 59 && !(year & 3)) {
268 nl_day = day;
269 month = 2;
271 else {
272 nl_day = (year & 3) || day <= 59 ? day : day-1;
273 for (month = 0; month < 12; month++)
274 if (day_n[month] > nl_day) break;
276 *date = nl_day-day_n[month-1]+1+(month << 5)+(year << 9);
280 /* Returns the inode number of the directory entry at offset pos. If bh is
281 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
282 returned in bh.
283 AV. Most often we do it item-by-item. Makes sense to optimize.
284 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
285 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
286 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
287 AV. Additionally, when we return -1 (i.e. reached the end of directory)
288 AV. we make bh NULL.
291 int fat__get_entry(struct inode *dir, loff_t *pos,struct buffer_head **bh,
292 struct msdos_dir_entry **de, loff_t *i_pos)
294 struct super_block *sb = dir->i_sb;
295 struct msdos_sb_info *sbi = MSDOS_SB(sb);
296 sector_t phys, iblock;
297 loff_t offset;
298 int err;
300 next:
301 offset = *pos;
302 if (*bh)
303 brelse(*bh);
305 *bh = NULL;
306 iblock = *pos >> sb->s_blocksize_bits;
307 err = fat_bmap(dir, iblock, &phys);
308 if (err || !phys)
309 return -1; /* beyond EOF or error */
311 *bh = sb_bread(sb, phys);
312 if (*bh == NULL) {
313 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
314 (unsigned long long)phys);
315 /* skip this block */
316 *pos = (iblock + 1) << sb->s_blocksize_bits;
317 goto next;
320 offset &= sb->s_blocksize - 1;
321 *pos += sizeof(struct msdos_dir_entry);
322 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
323 *i_pos = ((loff_t)phys << sbi->dir_per_block_bits) + (offset >> MSDOS_DIR_BITS);
325 return 0;
330 * Now an ugly part: this set of directory scan routines works on clusters
331 * rather than on inodes and sectors. They are necessary to locate the '..'
332 * directory "inode". raw_scan_sector operates in four modes:
334 * name number ino action
335 * -------- -------- -------- -------------------------------------------------
336 * non-NULL - X Find an entry with that name
337 * NULL non-NULL non-NULL Find an entry whose data starts at *number
338 * NULL non-NULL NULL Count subdirectories in *number. (*)
339 * NULL NULL non-NULL Find an empty entry
341 * (*) The return code should be ignored. It DOES NOT indicate success or
342 * failure. *number has to be initialized to zero.
344 * - = not used, X = a value is returned unless NULL
346 * If res_bh is non-NULL, the buffer is not deallocated but returned to the
347 * caller on success. res_de is set accordingly.
349 * If cont is non-zero, raw_found continues with the entry after the one
350 * res_bh/res_de point to.
354 #define RSS_NAME /* search for name */ \
355 done = !strncmp(data[entry].name,name,MSDOS_NAME) && \
356 !(data[entry].attr & ATTR_VOLUME);
358 #define RSS_START /* search for start cluster */ \
359 done = !IS_FREE(data[entry].name) \
360 && ( \
362 (sbi->fat_bits != 32) ? 0 : (CF_LE_W(data[entry].starthi) << 16) \
364 | CF_LE_W(data[entry].start) \
365 ) == *number;
367 #define RSS_FREE /* search for free entry */ \
369 done = IS_FREE(data[entry].name); \
372 #define RSS_COUNT /* count subdirectories */ \
374 done = 0; \
375 if (!IS_FREE(data[entry].name) && (data[entry].attr & ATTR_DIR)) \
376 (*number)++; \
379 static int raw_scan_sector(struct super_block *sb, sector_t sector,
380 const char *name, int *number, loff_t *i_pos,
381 struct buffer_head **res_bh,
382 struct msdos_dir_entry **res_de)
384 struct msdos_sb_info *sbi = MSDOS_SB(sb);
385 struct buffer_head *bh;
386 struct msdos_dir_entry *data;
387 int entry,start,done;
389 if (!(bh = sb_bread(sb, sector)))
390 return -EIO;
391 data = (struct msdos_dir_entry *) bh->b_data;
392 for (entry = 0; entry < sbi->dir_per_block; entry++) {
393 /* RSS_COUNT: if (data[entry].name == name) done=true else done=false. */
394 if (name) {
395 RSS_NAME
396 } else {
397 if (!i_pos) RSS_COUNT
398 else {
399 if (number) RSS_START
400 else RSS_FREE
403 if (done) {
404 if (i_pos) {
405 *i_pos = ((loff_t)sector << sbi->dir_per_block_bits) + entry;
407 start = CF_LE_W(data[entry].start);
408 if (sbi->fat_bits == 32)
409 start |= (CF_LE_W(data[entry].starthi) << 16);
411 if (!res_bh)
412 brelse(bh);
413 else {
414 *res_bh = bh;
415 *res_de = &data[entry];
417 return start;
420 brelse(bh);
421 return -ENOENT;
426 * raw_scan_root performs raw_scan_sector on the root directory until the
427 * requested entry is found or the end of the directory is reached.
430 static int raw_scan_root(struct super_block *sb, const char *name,
431 int *number, loff_t *i_pos,
432 struct buffer_head **res_bh,
433 struct msdos_dir_entry **res_de)
435 int count,cluster;
437 for (count = 0;
438 count < MSDOS_SB(sb)->dir_entries / MSDOS_SB(sb)->dir_per_block;
439 count++) {
440 cluster = raw_scan_sector(sb, MSDOS_SB(sb)->dir_start + count,
441 name, number, i_pos, res_bh, res_de);
442 if (cluster >= 0)
443 return cluster;
445 return -ENOENT;
450 * raw_scan_nonroot performs raw_scan_sector on a non-root directory until the
451 * requested entry is found or the end of the directory is reached.
454 static int raw_scan_nonroot(struct super_block *sb, int start, const char *name,
455 int *number, loff_t *i_pos,
456 struct buffer_head **res_bh,
457 struct msdos_dir_entry **res_de)
459 struct msdos_sb_info *sbi = MSDOS_SB(sb);
460 int count, cluster;
461 unsigned long dir_size = 0;
462 sector_t sector;
464 #ifdef DEBUG
465 printk("raw_scan_nonroot: start=%d\n",start);
466 #endif
467 do {
468 for (count = 0; count < sbi->cluster_size; count++) {
469 sector = ((sector_t)start - 2) * sbi->cluster_size
470 + count + sbi->data_start;
471 cluster = raw_scan_sector(sb, sector, name, number,
472 i_pos, res_bh, res_de);
473 if (cluster >= 0)
474 return cluster;
476 dir_size += 1 << sbi->cluster_bits;
477 if (dir_size > FAT_MAX_DIR_SIZE) {
478 fat_fs_panic(sb, "Directory %d: "
479 "exceeded the maximum size of directory",
480 start);
481 break;
483 start = fat_access(sb, start, -1);
484 if (start < 0)
485 return start;
486 else if (start == FAT_ENT_FREE) {
487 fat_fs_panic(sb, "%s: invalid cluster chain",
488 __FUNCTION__);
489 break;
491 #ifdef DEBUG
492 printk("next start: %d\n",start);
493 #endif
494 } while (start != FAT_ENT_EOF);
496 return -ENOENT;
501 * raw_scan performs raw_scan_sector on any sector.
503 * NOTE: raw_scan must not be used on a directory that is is the process of
504 * being created.
507 static int raw_scan(struct super_block *sb, int start, const char *name,
508 loff_t *i_pos, struct buffer_head **res_bh,
509 struct msdos_dir_entry **res_de)
511 if (start)
512 return raw_scan_nonroot(sb,start,name,NULL,i_pos,res_bh,res_de);
513 else
514 return raw_scan_root(sb,name,NULL,i_pos,res_bh,res_de);
518 * fat_subdirs counts the number of sub-directories of dir. It can be run
519 * on directories being created.
521 int fat_subdirs(struct inode *dir)
523 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
524 int number;
526 number = 0;
527 if ((dir->i_ino == MSDOS_ROOT_INO) && (sbi->fat_bits != 32))
528 raw_scan_root(dir->i_sb, NULL, &number, NULL, NULL, NULL);
529 else {
530 if ((dir->i_ino != MSDOS_ROOT_INO) && !MSDOS_I(dir)->i_start)
531 return 0; /* in mkdir */
532 else {
533 raw_scan_nonroot(dir->i_sb, MSDOS_I(dir)->i_start,
534 NULL, &number, NULL, NULL, NULL);
537 return number;
542 * Scans a directory for a given file (name points to its formatted name) or
543 * for an empty directory slot (name is NULL). Returns an error code or zero.
546 int fat_scan(struct inode *dir, const char *name, struct buffer_head **res_bh,
547 struct msdos_dir_entry **res_de, loff_t *i_pos)
549 int res;
551 res = raw_scan(dir->i_sb, MSDOS_I(dir)->i_start, name, i_pos,
552 res_bh, res_de);
553 return (res < 0) ? res : 0;