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/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
, ...)
26 vsnprintf (panic_msg
, sizeof(panic_msg
), fmt
, args
);
29 not_ro
= !(s
->s_flags
& MS_RDONLY
);
31 s
->s_flags
|= MS_RDONLY
;
33 printk(KERN_ERR
"FAT: Filesystem panic (dev %s)\n"
34 " %s\n", s
->s_id
, panic_msg
);
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)
58 if (MSDOS_SB(sb
)->free_clusters
== -1)
61 bh
= sb_bread(sb
, MSDOS_SB(sb
)->fsinfo_sector
);
63 printk(KERN_ERR
"FAT bread failed in fat_clusters_flush\n");
67 fsinfo
= (struct fat_boot_fsinfo
*)bh
->b_data
;
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"
73 CF_LE_L(fsinfo
->signature1
), CF_LE_L(fsinfo
->signature2
),
74 MSDOS_SB(sb
)->fsinfo_sector
);
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
);
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
;
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
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
);
107 while (curr
&& curr
!= FAT_ENT_EOF
) {
109 curr
= fat_access(sb
, last
= curr
, -1);
112 else if (curr
== FAT_ENT_FREE
) {
113 fat_fs_panic(sb
, "%s: invalid cluster chain"
115 __FUNCTION__
, inode
->i_ino
);
118 if (file_cluster
> max_cluster
) {
119 fat_fs_panic(sb
, "%s: bad cluster counts"
121 __FUNCTION__
, inode
->i_ino
);
127 /* find free FAT entry */
130 if (MSDOS_SB(sb
)->free_clusters
== 0) {
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
++) {
141 if (fat_access(sb
, nr
, -1) == FAT_ENT_FREE
)
144 if (count
>= MSDOS_SB(sb
)->clusters
) {
145 MSDOS_SB(sb
)->free_clusters
= 0;
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
);
158 /* add new one to the last of the cluster chain */
160 fat_access(sb
, last
, nr
);
161 fat_cache_add(inode
, file_cluster
, nr
);
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;
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
);
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
);
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
;
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
)
235 /* first subtract and mask after that... Otherwise, if
236 date == 0, bad things happen */
237 month
= ((date
>> 5) - 1) & 15;
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;
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;
265 if ((year
+3)/4+365*year
> day
) year
--;
266 day
-= (year
+3)/4+365*year
;
267 if (day
== 59 && !(year
& 3)) {
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
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)
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
;
306 iblock
= *pos
>> sb
->s_blocksize_bits
;
307 err
= fat_bmap(dir
, iblock
, &phys
);
309 return -1; /* beyond EOF or error */
311 *bh
= sb_bread(sb
, phys
);
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
;
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
);
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) \
362 (sbi->fat_bits != 32) ? 0 : (CF_LE_W(data[entry].starthi) << 16) \
364 | CF_LE_W(data[entry].start) \
367 #define RSS_FREE /* search for free entry */ \
369 done = IS_FREE(data[entry].name); \
372 #define RSS_COUNT /* count subdirectories */ \
375 if (!IS_FREE(data[entry].name) && (data[entry].attr & ATTR_DIR)) \
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
)))
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. */
397 if (!i_pos
) RSS_COUNT
399 if (number
) RSS_START
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);
415 *res_de
= &data
[entry
];
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
)
438 count
< MSDOS_SB(sb
)->dir_entries
/ MSDOS_SB(sb
)->dir_per_block
;
440 cluster
= raw_scan_sector(sb
, MSDOS_SB(sb
)->dir_start
+ count
,
441 name
, number
, i_pos
, res_bh
, res_de
);
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
);
461 unsigned long dir_size
= 0;
465 printk("raw_scan_nonroot: start=%d\n",start
);
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
);
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",
483 start
= fat_access(sb
, start
, -1);
486 else if (start
== FAT_ENT_FREE
) {
487 fat_fs_panic(sb
, "%s: invalid cluster chain",
492 printk("next start: %d\n",start
);
494 } while (start
!= FAT_ENT_EOF
);
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
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
)
512 return raw_scan_nonroot(sb
,start
,name
,NULL
,i_pos
,res_bh
,res_de
);
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
);
527 if ((dir
->i_ino
== MSDOS_ROOT_INO
) && (sbi
->fat_bits
!= 32))
528 raw_scan_root(dir
->i_sb
, NULL
, &number
, NULL
, NULL
, NULL
);
530 if ((dir
->i_ino
!= MSDOS_ROOT_INO
) && !MSDOS_I(dir
)->i_start
)
531 return 0; /* in mkdir */
533 raw_scan_nonroot(dir
->i_sb
, MSDOS_I(dir
)->i_start
,
534 NULL
, &number
, NULL
, NULL
, NULL
);
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
)
551 res
= raw_scan(dir
->i_sb
, MSDOS_I(dir
)->i_start
, name
, i_pos
,
553 return (res
< 0) ? res
: 0;