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)
10 #include <linux/msdos_fs.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
18 # define PRINTK(x) printk x
22 #define Printk(x) printk x
24 /* Well-known binary file extensions - of course there are many more */
26 static char ascii_extensions[] =
27 "TXT" "ME " "HTM" "1ST" "LOG" " " /* text files */
28 "C " "H " "CPP" "LIS" "PAS" "FOR" /* programming languages */
29 "F " "MAK" "INC" "BAS" /* programming languages */
30 "BAT" "SH " /* program code :) */
31 "INI" /* config files */
32 "PBM" "PGM" "DXF" /* graphics */
37 * fat_fs_panic reports a severe file system problem and sets the file system
38 * read-only. The file system can be made writable again by remounting it.
41 void fat_fs_panic(struct super_block *s,const char *msg)
45 not_ro = !(s->s_flags & MS_RDONLY);
46 if (not_ro) s->s_flags |= MS_RDONLY;
47 printk("Filesystem panic (dev %s).\n %s\n", kdevname(s->s_dev), msg);
49 printk(" File system has been set read-only\n");
54 * fat_is_binary selects optional text conversion based on the conversion mode
55 * and the extension part of the file name.
58 int fat_is_binary(char conversion,char *extension)
68 for (walk = ascii_extensions; *walk; walk += 3)
69 if (!strncmp(extension,walk,3)) return 0;
70 return 1; /* default binary conversion */
72 printk("Invalid conversion mode - defaulting to "
78 void lock_fat(struct super_block *sb)
80 down(&(MSDOS_SB(sb)->fat_lock));
83 void unlock_fat(struct super_block *sb)
85 up(&(MSDOS_SB(sb)->fat_lock));
88 /* Flushes the number of free clusters on FAT32 */
89 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
90 void fat_clusters_flush(struct super_block *sb)
92 struct buffer_head *bh;
93 struct fat_boot_fsinfo *fsinfo;
95 bh = fat_bread(sb, MSDOS_SB(sb)->fsinfo_sector);
97 printk("FAT bread failed in fat_clusters_flush\n");
101 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
103 if (!IS_FSINFO(fsinfo)) {
104 printk("FAT: Did not find valid FSINFO signature.\n"
105 "Found signature1 0x%x signature2 0x%x sector=%ld.\n",
106 CF_LE_L(fsinfo->signature1), CF_LE_L(fsinfo->signature2),
107 MSDOS_SB(sb)->fsinfo_sector);
110 fsinfo->free_clusters = CF_LE_L(MSDOS_SB(sb)->free_clusters);
111 fsinfo->next_cluster = CF_LE_L(MSDOS_SB(sb)->prev_free);
112 fat_mark_buffer_dirty(sb, bh);
117 * fat_add_cluster tries to allocate a new cluster and adds it to the
118 * file represented by inode.
120 int fat_add_cluster(struct inode *inode)
122 struct super_block *sb = inode->i_sb;
123 int count, nr, limit, last, curr, file_cluster;
124 int cluster_size = MSDOS_SB(sb)->cluster_size;
129 if (MSDOS_SB(sb)->free_clusters == 0) {
133 limit = MSDOS_SB(sb)->clusters;
134 nr = limit; /* to keep GCC happy */
135 for (count = 0; count < limit; count++) {
136 nr = ((count + MSDOS_SB(sb)->prev_free) % limit) + 2;
137 if (fat_access(sb, nr, -1) == 0)
140 if (count >= limit) {
141 MSDOS_SB(sb)->free_clusters = 0;
146 MSDOS_SB(sb)->prev_free = (count + MSDOS_SB(sb)->prev_free + 1) % limit;
147 fat_access(sb, nr, EOF_FAT(sb));
148 if (MSDOS_SB(sb)->free_clusters != -1)
149 MSDOS_SB(sb)->free_clusters--;
150 if (MSDOS_SB(sb)->fat_bits == 32)
151 fat_clusters_flush(sb);
155 /* We must locate the last cluster of the file to add this
156 new one (nr) to the end of the link list (the FAT).
158 Here file_cluster will be the number of the last cluster of the
159 file (before we add nr).
161 last is the corresponding cluster number on the disk. We will
162 use last to plug the nr cluster. We will use file_cluster to
165 last = file_cluster = 0;
166 if ((curr = MSDOS_I(inode)->i_start) != 0) {
167 fat_cache_lookup(inode, INT_MAX, &last, &curr);
169 while (curr && curr != -1){
171 if (!(curr = fat_access(sb, last = curr,-1))) {
172 fat_fs_panic(sb, "File without EOF");
178 fat_access(sb, last, nr);
179 fat_cache_add(inode, file_cluster, nr);
181 MSDOS_I(inode)->i_start = nr;
182 MSDOS_I(inode)->i_logstart = nr;
183 mark_inode_dirty(inode);
186 != inode->i_blocks / cluster_size / (sb->s_blocksize / 512)) {
187 printk ("file_cluster badly computed!!! %d <> %ld\n",
189 inode->i_blocks / cluster_size / (sb->s_blocksize / 512));
190 fat_cache_inval_inode(inode);
192 inode->i_blocks += (1 << MSDOS_SB(sb)->cluster_bits) / 512;
197 struct buffer_head *fat_extend_dir(struct inode *inode)
199 struct super_block *sb = inode->i_sb;
200 int nr, sector, last_sector;
201 struct buffer_head *bh, *res = NULL;
202 int cluster_size = MSDOS_SB(sb)->cluster_size;
204 if (MSDOS_SB(sb)->fat_bits != 32) {
205 if (inode->i_ino == MSDOS_ROOT_INO)
209 nr = fat_add_cluster(inode);
213 sector = MSDOS_SB(sb)->data_start + (nr - 2) * cluster_size;
214 last_sector = sector + cluster_size;
215 if (MSDOS_SB(sb)->cvf_format && MSDOS_SB(sb)->cvf_format->zero_out_cluster)
216 MSDOS_SB(sb)->cvf_format->zero_out_cluster(inode, nr);
218 for ( ; sector < last_sector; sector++) {
220 printk("zeroing sector %d\n", sector);
222 if (!(bh = fat_getblk(sb, sector)))
223 printk("getblk failed\n");
225 memset(bh->b_data, 0, sb->s_blocksize);
226 fat_set_uptodate(sb, bh, 1);
227 fat_mark_buffer_dirty(sb, bh);
235 if (inode->i_size & (sb->s_blocksize - 1)) {
236 fat_fs_panic(sb, "Odd directory size");
237 inode->i_size = (inode->i_size + sb->s_blocksize)
238 & ~(sb->s_blocksize - 1);
240 inode->i_size += 1 << MSDOS_SB(sb)->cluster_bits;
241 MSDOS_I(inode)->mmu_private += 1 << MSDOS_SB(sb)->cluster_bits;
242 mark_inode_dirty(inode);
247 /* Linear day numbers of the respective 1sts in non-leap years. */
249 static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
250 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
253 extern struct timezone sys_tz;
256 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
258 int date_dos2unix(unsigned short time,unsigned short date)
262 /* first subtract and mask after that... Otherwise, if
263 date == 0, bad things happen */
264 month = ((date >> 5) - 1) & 15;
266 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
267 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
268 month < 2 ? 1 : 0)+3653);
269 /* days since 1.1.70 plus 80's leap day */
270 secs += sys_tz.tz_minuteswest*60;
275 /* Convert linear UNIX date to a MS-DOS time/date pair. */
277 void fat_date_unix2dos(int unix_date,unsigned short *time,
278 unsigned short *date)
280 int day,year,nl_day,month;
282 unix_date -= sys_tz.tz_minuteswest*60;
284 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
285 if (unix_date < 315532800)
286 unix_date = 315532800;
288 *time = (unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
289 (((unix_date/3600) % 24) << 11);
290 day = unix_date/86400-3652;
292 if ((year+3)/4+365*year > day) year--;
293 day -= (year+3)/4+365*year;
294 if (day == 59 && !(year & 3)) {
299 nl_day = (year & 3) || day <= 59 ? day : day-1;
300 for (month = 0; month < 12; month++)
301 if (day_n[month] > nl_day) break;
303 *date = nl_day-day_n[month-1]+1+(month << 5)+(year << 9);
307 /* Returns the inode number of the directory entry at offset pos. If bh is
308 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
310 AV. Most often we do it item-by-item. Makes sense to optimize.
311 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
312 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
313 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
314 AV. Additionally, when we return -1 (i.e. reached the end of directory)
318 int fat__get_entry(struct inode *dir, loff_t *pos,struct buffer_head **bh,
319 struct msdos_dir_entry **de, loff_t *i_pos)
321 struct super_block *sb = dir->i_sb;
322 struct msdos_sb_info *sbi = MSDOS_SB(sb);
328 PRINTK (("get_entry offset %d\n",offset));
332 if ((sector = fat_bmap(dir,offset >> sb->s_blocksize_bits)) == -1)
334 PRINTK (("get_entry sector %d %p\n",sector,*bh));
335 PRINTK (("get_entry sector apres brelse\n"));
337 return -1; /* beyond EOF */
338 *pos += sizeof(struct msdos_dir_entry);
339 if (!(*bh = fat_bread(sb, sector))) {
340 printk("Directory sread (sector 0x%x) failed\n",sector);
343 PRINTK (("get_entry apres sread\n"));
345 offset &= sb->s_blocksize - 1;
346 *de = (struct msdos_dir_entry *) ((*bh)->b_data + offset);
347 *i_pos = ((loff_t)sector << sbi->dir_per_block_bits) + (offset >> MSDOS_DIR_BITS);
355 * Now an ugly part: this set of directory scan routines works on clusters
356 * rather than on inodes and sectors. They are necessary to locate the '..'
357 * directory "inode". raw_scan_sector operates in four modes:
359 * name number ino action
360 * -------- -------- -------- -------------------------------------------------
361 * non-NULL - X Find an entry with that name
362 * NULL non-NULL non-NULL Find an entry whose data starts at *number
363 * NULL non-NULL NULL Count subdirectories in *number. (*)
364 * NULL NULL non-NULL Find an empty entry
366 * (*) The return code should be ignored. It DOES NOT indicate success or
367 * failure. *number has to be initialized to zero.
369 * - = not used, X = a value is returned unless NULL
371 * If res_bh is non-NULL, the buffer is not deallocated but returned to the
372 * caller on success. res_de is set accordingly.
374 * If cont is non-zero, raw_found continues with the entry after the one
375 * res_bh/res_de point to.
379 #define RSS_NAME /* search for name */ \
380 done = !strncmp(data[entry].name,name,MSDOS_NAME) && \
381 !(data[entry].attr & ATTR_VOLUME);
383 #define RSS_START /* search for start cluster */ \
384 done = !IS_FREE(data[entry].name) \
387 (sbi->fat_bits != 32) ? 0 : (CF_LE_W(data[entry].starthi) << 16) \
389 | CF_LE_W(data[entry].start) \
392 #define RSS_FREE /* search for free entry */ \
394 done = IS_FREE(data[entry].name); \
397 #define RSS_COUNT /* count subdirectories */ \
400 if (!IS_FREE(data[entry].name) && (data[entry].attr & ATTR_DIR)) \
404 static int raw_scan_sector(struct super_block *sb, int sector,
405 const char *name, int *number, loff_t *i_pos,
406 struct buffer_head **res_bh,
407 struct msdos_dir_entry **res_de)
409 struct msdos_sb_info *sbi = MSDOS_SB(sb);
410 struct buffer_head *bh;
411 struct msdos_dir_entry *data;
412 int entry,start,done;
414 if (!(bh = fat_bread(sb, sector)))
416 data = (struct msdos_dir_entry *) bh->b_data;
417 for (entry = 0; entry < sbi->dir_per_block; entry++) {
418 /* RSS_COUNT: if (data[entry].name == name) done=true else done=false. */
422 if (!i_pos) RSS_COUNT
424 if (number) RSS_START
430 *i_pos = ((loff_t)sector << sbi->dir_per_block_bits) + entry;
432 start = CF_LE_W(data[entry].start);
433 if (sbi->fat_bits == 32)
434 start |= (CF_LE_W(data[entry].starthi) << 16);
440 *res_de = &data[entry];
451 * raw_scan_root performs raw_scan_sector on the root directory until the
452 * requested entry is found or the end of the directory is reached.
455 static int raw_scan_root(struct super_block *sb, const char *name,
456 int *number, loff_t *i_pos,
457 struct buffer_head **res_bh,
458 struct msdos_dir_entry **res_de)
463 count < MSDOS_SB(sb)->dir_entries / MSDOS_SB(sb)->dir_per_block;
465 cluster = raw_scan_sector(sb, MSDOS_SB(sb)->dir_start + count,
466 name, number, i_pos, res_bh, res_de);
475 * raw_scan_nonroot performs raw_scan_sector on a non-root directory until the
476 * requested entry is found or the end of the directory is reached.
479 static int raw_scan_nonroot(struct super_block *sb, int start, const char *name,
480 int *number, loff_t *i_pos,
481 struct buffer_head **res_bh,
482 struct msdos_dir_entry **res_de)
484 struct msdos_sb_info *sbi = MSDOS_SB(sb);
485 int count, cluster, sector;
488 printk("raw_scan_nonroot: start=%d\n",start);
491 for (count = 0; count < sbi->cluster_size; count++) {
492 sector = (start - 2) * sbi->cluster_size
493 + count + sbi->data_start;
494 cluster = raw_scan_sector(sb, sector, name, number,
495 i_pos, res_bh, res_de);
499 if (!(start = fat_access(sb,start,-1))) {
500 fat_fs_panic(sb,"FAT error");
504 printk("next start: %d\n",start);
513 * raw_scan performs raw_scan_sector on any sector.
515 * NOTE: raw_scan must not be used on a directory that is is the process of
519 static int raw_scan(struct super_block *sb, int start, const char *name,
520 loff_t *i_pos, struct buffer_head **res_bh,
521 struct msdos_dir_entry **res_de)
524 return raw_scan_nonroot(sb,start,name,NULL,i_pos,res_bh,res_de);
526 return raw_scan_root(sb,name,NULL,i_pos,res_bh,res_de);
530 * fat_subdirs counts the number of sub-directories of dir. It can be run
531 * on directories being created.
533 int fat_subdirs(struct inode *dir)
535 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
539 if ((dir->i_ino == MSDOS_ROOT_INO) && (sbi->fat_bits != 32))
540 raw_scan_root(dir->i_sb, NULL, &number, NULL, NULL, NULL);
542 if ((dir->i_ino != MSDOS_ROOT_INO) && !MSDOS_I(dir)->i_start)
543 return 0; /* in mkdir */
545 raw_scan_nonroot(dir->i_sb, MSDOS_I(dir)->i_start,
546 NULL, &number, NULL, NULL, NULL);
554 * Scans a directory for a given file (name points to its formatted name) or
555 * for an empty directory slot (name is NULL). Returns an error code or zero.
558 int fat_scan(struct inode *dir, const char *name, struct buffer_head **res_bh,
559 struct msdos_dir_entry **res_de, loff_t *i_pos)
563 res = raw_scan(dir->i_sb, MSDOS_I(dir)->i_start, name, i_pos,
565 return (res < 0) ? res : 0;