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 msdos_sb_info
*sbi
= MSDOS_SB(sb
);
54 struct buffer_head
*bh
;
55 struct fat_boot_fsinfo
*fsinfo
;
57 if (sbi
->fat_bits
!= 32)
60 bh
= sb_bread(sb
, sbi
->fsinfo_sector
);
62 printk(KERN_ERR
"FAT bread failed in fat_clusters_flush\n");
66 fsinfo
= (struct fat_boot_fsinfo
*)bh
->b_data
;
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"
72 CF_LE_L(fsinfo
->signature1
), CF_LE_L(fsinfo
->signature2
),
75 if (sbi
->free_clusters
!= -1)
76 fsinfo
->free_clusters
= CF_LE_L(sbi
->free_clusters
);
78 fsinfo
->next_cluster
= CF_LE_L(sbi
->prev_free
);
79 mark_buffer_dirty(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 count
, limit
, new_dclus
, new_fclus
, last
;
92 int cluster_bits
= MSDOS_SB(sb
)->cluster_bits
;
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
101 last
= new_fclus
= 0;
102 if (MSDOS_I(inode
)->i_start
) {
103 int ret
, fclus
, dclus
;
105 ret
= fat_get_cluster(inode
, FAT_ENT_EOF
, &fclus
, &dclus
);
108 new_fclus
= fclus
+ 1;
112 /* find free FAT entry */
115 if (MSDOS_SB(sb
)->free_clusters
== 0) {
120 limit
= MSDOS_SB(sb
)->clusters
+ 2;
121 new_dclus
= MSDOS_SB(sb
)->prev_free
+ 1;
122 for (count
= 0; count
< MSDOS_SB(sb
)->clusters
; count
++, new_dclus
++) {
123 new_dclus
= new_dclus
% limit
;
126 if (fat_access(sb
, new_dclus
, -1) == FAT_ENT_FREE
)
129 if (count
>= MSDOS_SB(sb
)->clusters
) {
130 MSDOS_SB(sb
)->free_clusters
= 0;
134 MSDOS_SB(sb
)->prev_free
= new_dclus
;
136 fat_access(sb
, new_dclus
, FAT_ENT_EOF
);
137 if (MSDOS_SB(sb
)->free_clusters
!= -1)
138 MSDOS_SB(sb
)->free_clusters
--;
139 fat_clusters_flush(sb
);
143 /* add new one to the last of the cluster chain */
145 fat_access(sb
, last
, new_dclus
);
146 fat_cache_add(inode
, new_fclus
, new_dclus
);
148 MSDOS_I(inode
)->i_start
= new_dclus
;
149 MSDOS_I(inode
)->i_logstart
= new_dclus
;
150 mark_inode_dirty(inode
);
152 if (new_fclus
!= (inode
->i_blocks
>> (cluster_bits
- 9))) {
153 fat_fs_panic(sb
, "clusters badly computed (%d != %ld)",
154 new_fclus
, inode
->i_blocks
>> (cluster_bits
- 9));
155 fat_cache_inval_inode(inode
);
157 inode
->i_blocks
+= MSDOS_SB(sb
)->cluster_size
>> 9;
162 struct buffer_head
*fat_extend_dir(struct inode
*inode
)
164 struct super_block
*sb
= inode
->i_sb
;
165 struct buffer_head
*bh
, *res
= NULL
;
166 int nr
, sec_per_clus
= MSDOS_SB(sb
)->sec_per_clus
;
167 sector_t sector
, last_sector
;
169 if (MSDOS_SB(sb
)->fat_bits
!= 32) {
170 if (inode
->i_ino
== MSDOS_ROOT_INO
)
171 return ERR_PTR(-ENOSPC
);
174 nr
= fat_add_cluster(inode
);
178 sector
= ((sector_t
)nr
- 2) * sec_per_clus
+ MSDOS_SB(sb
)->data_start
;
179 last_sector
= sector
+ sec_per_clus
;
180 for ( ; sector
< last_sector
; sector
++) {
181 if ((bh
= sb_getblk(sb
, sector
))) {
182 memset(bh
->b_data
, 0, sb
->s_blocksize
);
183 set_buffer_uptodate(bh
);
184 mark_buffer_dirty(bh
);
193 if (inode
->i_size
& (sb
->s_blocksize
- 1)) {
194 fat_fs_panic(sb
, "Odd directory size");
195 inode
->i_size
= (inode
->i_size
+ sb
->s_blocksize
)
196 & ~(sb
->s_blocksize
- 1);
198 inode
->i_size
+= MSDOS_SB(sb
)->cluster_size
;
199 MSDOS_I(inode
)->mmu_private
+= MSDOS_SB(sb
)->cluster_size
;
204 /* Linear day numbers of the respective 1sts in non-leap years. */
206 static int day_n
[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
207 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
210 extern struct timezone sys_tz
;
213 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
215 int date_dos2unix(unsigned short time
,unsigned short date
)
219 /* first subtract and mask after that... Otherwise, if
220 date == 0, bad things happen */
221 month
= ((date
>> 5) - 1) & 15;
223 secs
= (time
& 31)*2+60*((time
>> 5) & 63)+(time
>> 11)*3600+86400*
224 ((date
& 31)-1+day_n
[month
]+(year
/4)+year
*365-((year
& 3) == 0 &&
225 month
< 2 ? 1 : 0)+3653);
226 /* days since 1.1.70 plus 80's leap day */
227 secs
+= sys_tz
.tz_minuteswest
*60;
232 /* Convert linear UNIX date to a MS-DOS time/date pair. */
234 void fat_date_unix2dos(int unix_date
,unsigned short *time
,
235 unsigned short *date
)
237 int day
,year
,nl_day
,month
;
239 unix_date
-= sys_tz
.tz_minuteswest
*60;
241 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
242 if (unix_date
< 315532800)
243 unix_date
= 315532800;
245 *time
= (unix_date
% 60)/2+(((unix_date
/60) % 60) << 5)+
246 (((unix_date
/3600) % 24) << 11);
247 day
= unix_date
/86400-3652;
249 if ((year
+3)/4+365*year
> day
) year
--;
250 day
-= (year
+3)/4+365*year
;
251 if (day
== 59 && !(year
& 3)) {
256 nl_day
= (year
& 3) || day
<= 59 ? day
: day
-1;
257 for (month
= 0; month
< 12; month
++)
258 if (day_n
[month
] > nl_day
) break;
260 *date
= nl_day
-day_n
[month
-1]+1+(month
<< 5)+(year
<< 9);
264 /* Returns the inode number of the directory entry at offset pos. If bh is
265 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
267 AV. Most often we do it item-by-item. Makes sense to optimize.
268 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
269 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
270 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
271 AV. Additionally, when we return -1 (i.e. reached the end of directory)
275 int fat__get_entry(struct inode
*dir
, loff_t
*pos
,struct buffer_head
**bh
,
276 struct msdos_dir_entry
**de
, loff_t
*i_pos
)
278 struct super_block
*sb
= dir
->i_sb
;
279 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
280 sector_t phys
, iblock
;
290 iblock
= *pos
>> sb
->s_blocksize_bits
;
291 err
= fat_bmap(dir
, iblock
, &phys
);
293 return -1; /* beyond EOF or error */
295 *bh
= sb_bread(sb
, phys
);
297 printk(KERN_ERR
"FAT: Directory bread(block %llu) failed\n",
298 (unsigned long long)phys
);
299 /* skip this block */
300 *pos
= (iblock
+ 1) << sb
->s_blocksize_bits
;
304 offset
&= sb
->s_blocksize
- 1;
305 *pos
+= sizeof(struct msdos_dir_entry
);
306 *de
= (struct msdos_dir_entry
*)((*bh
)->b_data
+ offset
);
307 *i_pos
= ((loff_t
)phys
<< sbi
->dir_per_block_bits
) + (offset
>> MSDOS_DIR_BITS
);
312 static int fat_get_short_entry(struct inode
*dir
, loff_t
*pos
,
313 struct buffer_head
**bh
,
314 struct msdos_dir_entry
**de
, loff_t
*i_pos
)
316 while (fat_get_entry(dir
, pos
, bh
, de
, i_pos
) >= 0) {
317 /* free entry or long name entry or volume label */
318 if (!IS_FREE((*de
)->name
) && !((*de
)->attr
& ATTR_VOLUME
))
325 * fat_subdirs counts the number of sub-directories of dir. It can be run
326 * on directories being created.
328 int fat_subdirs(struct inode
*dir
)
330 struct buffer_head
*bh
;
331 struct msdos_dir_entry
*de
;
337 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
, &i_pos
) >= 0) {
338 if (de
->attr
& ATTR_DIR
)
346 * Scans a directory for a given file (name points to its formatted name).
347 * Returns an error code or zero.
349 int fat_scan(struct inode
*dir
, const unsigned char *name
,
350 struct buffer_head
**bh
, struct msdos_dir_entry
**de
,
357 while (fat_get_short_entry(dir
, &cpos
, bh
, de
, i_pos
) >= 0) {
358 if (!strncmp((*de
)->name
, name
, MSDOS_NAME
))