4 * Written 1992,1993 by Werner Almesberger
5 * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
6 * Rewritten for the constant inumbers support by Al Viro
10 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
13 #include <linux/module.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/smp_lock.h>
17 #include <linux/seq_file.h>
18 #include <linux/msdos_fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <asm/unaligned.h>
26 * New FAT inode stuff. We do the following:
27 * a) i_ino is constant and has nothing with on-disk location.
28 * b) FAT manages its own cache of directory entries.
29 * c) *This* cache is indexed by on-disk location.
30 * d) inode has an associated directory entry, all right, but
32 * e) currently entries are stored within struct inode. That should
34 * f) we deal with races in the following way:
35 * 1. readdir() and lookup() do FAT-dir-cache lookup.
36 * 2. rename() unhashes the F-d-c entry and rehashes it in
38 * 3. unlink() and rmdir() unhash F-d-c entry.
39 * 4. fat_write_inode() checks whether the thing is unhashed.
40 * If it is we silently return. If it isn't we do bread(),
41 * check if the location is still valid and retry if it
42 * isn't. Otherwise we do changes.
43 * 5. Spinlock is used to protect hash/unhash/location check/lookup
44 * 6. fat_clear_inode() unhashes the F-d-c entry.
45 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
46 * and consider negative result as cache miss.
49 #define FAT_HASH_BITS 8
50 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
51 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
52 static struct list_head fat_inode_hashtable
[FAT_HASH_SIZE
];
53 spinlock_t fat_inode_lock
= SPIN_LOCK_UNLOCKED
;
55 void fat_hash_init(void)
58 for(i
= 0; i
< FAT_HASH_SIZE
; i
++) {
59 INIT_LIST_HEAD(&fat_inode_hashtable
[i
]);
63 static inline unsigned long fat_hash(struct super_block
*sb
, loff_t i_pos
)
65 unsigned long tmp
= (unsigned long)i_pos
| (unsigned long) sb
;
66 tmp
= tmp
+ (tmp
>> FAT_HASH_BITS
) + (tmp
>> FAT_HASH_BITS
* 2);
67 return tmp
& FAT_HASH_MASK
;
70 void fat_attach(struct inode
*inode
, loff_t i_pos
)
72 spin_lock(&fat_inode_lock
);
73 MSDOS_I(inode
)->i_pos
= i_pos
;
74 list_add(&MSDOS_I(inode
)->i_fat_hash
,
75 fat_inode_hashtable
+ fat_hash(inode
->i_sb
, i_pos
));
76 spin_unlock(&fat_inode_lock
);
79 void fat_detach(struct inode
*inode
)
81 spin_lock(&fat_inode_lock
);
82 MSDOS_I(inode
)->i_pos
= 0;
83 list_del_init(&MSDOS_I(inode
)->i_fat_hash
);
84 spin_unlock(&fat_inode_lock
);
87 struct inode
*fat_iget(struct super_block
*sb
, loff_t i_pos
)
89 struct list_head
*p
= fat_inode_hashtable
+ fat_hash(sb
, i_pos
);
90 struct list_head
*walk
;
91 struct msdos_inode_info
*i
;
92 struct inode
*inode
= NULL
;
94 spin_lock(&fat_inode_lock
);
95 list_for_each(walk
, p
) {
96 i
= list_entry(walk
, struct msdos_inode_info
, i_fat_hash
);
97 if (i
->vfs_inode
.i_sb
!= sb
)
99 if (i
->i_pos
!= i_pos
)
101 inode
= igrab(&i
->vfs_inode
);
105 spin_unlock(&fat_inode_lock
);
109 static int fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
);
111 struct inode
*fat_build_inode(struct super_block
*sb
,
112 struct msdos_dir_entry
*de
, loff_t i_pos
, int *res
)
116 inode
= fat_iget(sb
, i_pos
);
119 inode
= new_inode(sb
);
123 inode
->i_ino
= iunique(sb
, MSDOS_ROOT_INO
);
124 inode
->i_version
= 1;
125 *res
= fat_fill_inode(inode
, de
);
131 fat_attach(inode
, i_pos
);
132 insert_inode_hash(inode
);
137 void fat_delete_inode(struct inode
*inode
)
139 if (!is_bad_inode(inode
)) {
146 void fat_clear_inode(struct inode
*inode
)
148 if (is_bad_inode(inode
))
151 spin_lock(&fat_inode_lock
);
152 fat_cache_inval_inode(inode
);
153 list_del_init(&MSDOS_I(inode
)->i_fat_hash
);
154 spin_unlock(&fat_inode_lock
);
158 void fat_put_super(struct super_block
*sb
)
160 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
162 if (!(sb
->s_flags
& MS_RDONLY
))
163 fat_clusters_flush(sb
);
166 unload_nls(sbi
->nls_disk
);
167 sbi
->nls_disk
= NULL
;
168 sbi
->options
.codepage
= 0;
171 unload_nls(sbi
->nls_io
);
175 * Note: the iocharset option might have been specified
176 * without enabling nls_io, so check for it here.
178 if (sbi
->options
.iocharset
) {
179 kfree(sbi
->options
.iocharset
);
180 sbi
->options
.iocharset
= NULL
;
182 sb
->s_fs_info
= NULL
;
186 static int simple_getbool(char *s
, int *setval
)
189 if (!strcmp(s
,"1") || !strcmp(s
,"yes") || !strcmp(s
,"true"))
191 else if (!strcmp(s
,"0") || !strcmp(s
,"no") || !strcmp(s
,"false"))
200 static int fat_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
202 struct msdos_sb_info
*sbi
= MSDOS_SB(mnt
->mnt_sb
);
203 struct fat_mount_options
*opts
= &sbi
->options
;
204 int isvfat
= opts
->isvfat
;
206 if (opts
->fs_uid
!= 0)
207 seq_printf(m
, ",uid=%d", opts
->fs_uid
);
208 if (opts
->fs_gid
!= 0)
209 seq_printf(m
, ",gid=%d", opts
->fs_gid
);
210 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask
);
211 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask
);
213 seq_printf(m
, ",codepage=%s", sbi
->nls_disk
->charset
);
216 && strcmp(sbi
->nls_io
->charset
, CONFIG_NLS_DEFAULT
))
217 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
219 switch (opts
->shortname
) {
220 case VFAT_SFN_DISPLAY_WIN95
| VFAT_SFN_CREATE_WIN95
:
221 seq_puts(m
, ",shortname=win95");
223 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WINNT
:
224 seq_puts(m
, ",shortname=winnt");
226 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WIN95
:
227 seq_puts(m
, ",shortname=mixed");
229 case VFAT_SFN_DISPLAY_LOWER
| VFAT_SFN_CREATE_WIN95
:
230 /* seq_puts(m, ",shortname=lower"); */
233 seq_puts(m
, ",shortname=unknown");
237 if (opts
->name_check
!= 'n')
238 seq_printf(m
, ",check=%c", opts
->name_check
);
240 seq_puts(m
, ",quiet");
242 seq_puts(m
, ",showexec");
243 if (opts
->sys_immutable
)
244 seq_puts(m
, ",sys_immutable");
247 seq_puts(m
, ",dotsOK=yes");
249 seq_puts(m
, ",nocase");
252 seq_puts(m
, ",utf8");
253 if (opts
->unicode_xlate
)
254 seq_puts(m
, ",uni_xlate");
256 seq_puts(m
, ",nonumtail");
262 static int parse_options(char *options
, int is_vfat
, int *debug
,
263 struct fat_mount_options
*opts
)
265 char *this_char
, *value
, *p
;
266 int ret
= 1, val
, len
;
268 opts
->isvfat
= is_vfat
;
270 opts
->fs_uid
= current
->uid
;
271 opts
->fs_gid
= current
->gid
;
272 opts
->fs_fmask
= opts
->fs_dmask
= current
->fs
->umask
;
274 opts
->iocharset
= NULL
;
276 opts
->shortname
= VFAT_SFN_DISPLAY_LOWER
|VFAT_SFN_CREATE_WIN95
;
279 opts
->name_check
= 'n';
280 opts
->quiet
= opts
->showexec
= opts
->sys_immutable
= opts
->dotsOK
= 0;
281 opts
->utf8
= opts
->unicode_xlate
= 0;
288 while ((this_char
= strsep(&options
,",")) != NULL
) {
291 if ((value
= strchr(this_char
,'=')) != NULL
)
294 if (!strcmp(this_char
,"check") && value
) {
295 if (value
[0] && !value
[1] && strchr("rns",*value
))
296 opts
->name_check
= *value
;
297 else if (!strcmp(value
,"relaxed"))
298 opts
->name_check
= 'r';
299 else if (!strcmp(value
,"normal"))
300 opts
->name_check
= 'n';
301 else if (!strcmp(value
,"strict"))
302 opts
->name_check
= 's';
305 else if (!strcmp(this_char
,"conv") && value
) {
306 printk(KERN_INFO
"FAT: conv option is obsolete, "
307 "not supported now\n");
309 else if (!strcmp(this_char
,"nocase")) {
313 /* for backward compatible */
314 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
315 | VFAT_SFN_CREATE_WIN95
;
318 else if (!strcmp(this_char
,"showexec")) {
321 else if (!strcmp(this_char
,"uid")) {
322 if (!value
|| !*value
) ret
= 0;
324 opts
->fs_uid
= simple_strtoul(value
,&value
,0);
328 else if (!strcmp(this_char
,"gid")) {
329 if (!value
|| !*value
) ret
= 0;
331 opts
->fs_gid
= simple_strtoul(value
,&value
,0);
335 else if (!strcmp(this_char
,"umask")) {
336 if (!value
|| !*value
) ret
= 0;
338 opts
->fs_fmask
= opts
->fs_dmask
=
339 simple_strtoul(value
,&value
,8);
343 else if (!strcmp(this_char
,"fmask")) {
344 if (!value
|| !*value
) ret
= 0;
346 opts
->fs_fmask
= simple_strtoul(value
,&value
,8);
350 else if (!strcmp(this_char
,"dmask")) {
351 if (!value
|| !*value
) ret
= 0;
353 opts
->fs_dmask
= simple_strtoul(value
,&value
,8);
357 else if (!strcmp(this_char
,"debug")) {
361 else if (!strcmp(this_char
,"fat")) {
362 printk(KERN_INFO
"FAT: fat option is obsolete, "
363 "not supported now\n");
365 else if (!strcmp(this_char
,"quiet")) {
367 else opts
->quiet
= 1;
369 else if (!strcmp(this_char
,"blocksize")) {
370 printk(KERN_INFO
"FAT: blocksize option is obsolete, "
371 "not supported now\n");
373 else if (!strcmp(this_char
,"sys_immutable")) {
375 else opts
->sys_immutable
= 1;
377 else if (!strcmp(this_char
,"codepage") && value
) {
378 opts
->codepage
= simple_strtoul(value
,&value
,0);
383 else if (!is_vfat
&& !strcmp(this_char
,"dots")) {
386 else if (!is_vfat
&& !strcmp(this_char
,"nodots")) {
389 else if (!is_vfat
&& !strcmp(this_char
,"dotsOK") && value
) {
390 if (!strcmp(value
,"yes")) opts
->dotsOK
= 1;
391 else if (!strcmp(value
,"no")) opts
->dotsOK
= 0;
396 else if (is_vfat
&& !strcmp(this_char
,"iocharset") && value
) {
398 while (*value
&& *value
!= ',')
404 if (opts
->iocharset
!= NULL
) {
405 kfree(opts
->iocharset
);
406 opts
->iocharset
= NULL
;
408 buffer
= kmalloc(len
+ 1, GFP_KERNEL
);
409 if (buffer
!= NULL
) {
410 opts
->iocharset
= buffer
;
411 memcpy(buffer
, p
, len
);
417 else if (is_vfat
&& !strcmp(this_char
,"utf8")) {
418 ret
= simple_getbool(value
, &val
);
419 if (ret
) opts
->utf8
= val
;
421 else if (is_vfat
&& !strcmp(this_char
,"uni_xlate")) {
422 ret
= simple_getbool(value
, &val
);
423 if (ret
) opts
->unicode_xlate
= val
;
425 else if (is_vfat
&& !strcmp(this_char
,"posix")) {
426 printk(KERN_INFO
"FAT: posix option is obsolete, "
427 "not supported now\n");
429 else if (is_vfat
&& !strcmp(this_char
,"nonumtail")) {
430 ret
= simple_getbool(value
, &val
);
432 opts
->numtail
= !val
;
435 else if (is_vfat
&& !strcmp(this_char
, "shortname")) {
436 if (!strcmp(value
, "lower"))
437 opts
->shortname
= VFAT_SFN_DISPLAY_LOWER
438 | VFAT_SFN_CREATE_WIN95
;
439 else if (!strcmp(value
, "win95"))
440 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
441 | VFAT_SFN_CREATE_WIN95
;
442 else if (!strcmp(value
, "winnt"))
443 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
444 | VFAT_SFN_CREATE_WINNT
;
445 else if (!strcmp(value
, "mixed"))
446 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
447 | VFAT_SFN_CREATE_WIN95
;
451 printk(KERN_ERR
"FAT: Unrecognized mount option %s\n",
460 if (opts
->unicode_xlate
)
466 static int fat_calc_dir_size(struct inode
*inode
)
468 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
469 int ret
, fclus
, dclus
;
472 if (MSDOS_I(inode
)->i_start
== 0)
475 ret
= fat_get_cluster(inode
, FAT_ENT_EOF
, &fclus
, &dclus
);
478 inode
->i_size
= (fclus
+ 1) << sbi
->cluster_bits
;
483 static int fat_read_root(struct inode
*inode
)
485 struct super_block
*sb
= inode
->i_sb
;
486 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
489 MSDOS_I(inode
)->file_cluster
= MSDOS_I(inode
)->disk_cluster
= 0;
490 MSDOS_I(inode
)->i_pos
= 0;
491 inode
->i_uid
= sbi
->options
.fs_uid
;
492 inode
->i_gid
= sbi
->options
.fs_gid
;
494 inode
->i_generation
= 0;
495 inode
->i_mode
= (S_IRWXUGO
& ~sbi
->options
.fs_dmask
) | S_IFDIR
;
496 inode
->i_op
= sbi
->dir_ops
;
497 inode
->i_fop
= &fat_dir_operations
;
498 if (sbi
->fat_bits
== 32) {
499 MSDOS_I(inode
)->i_start
= sbi
->root_cluster
;
500 error
= fat_calc_dir_size(inode
);
504 MSDOS_I(inode
)->i_start
= 0;
505 inode
->i_size
= sbi
->dir_entries
* sizeof(struct msdos_dir_entry
);
507 inode
->i_blksize
= sbi
->cluster_size
;
508 inode
->i_blocks
= ((inode
->i_size
+ (sbi
->cluster_size
- 1))
509 & ~((loff_t
)sbi
->cluster_size
- 1)) >> 9;
510 MSDOS_I(inode
)->i_logstart
= 0;
511 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
513 MSDOS_I(inode
)->i_attrs
= 0;
514 inode
->i_mtime
.tv_sec
= inode
->i_atime
.tv_sec
= inode
->i_ctime
.tv_sec
= 0;
515 inode
->i_mtime
.tv_nsec
= inode
->i_atime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
516 MSDOS_I(inode
)->i_ctime_ms
= 0;
517 inode
->i_nlink
= fat_subdirs(inode
)+2;
523 * a FAT file handle with fhtype 3 is
524 * 0/ i_ino - for fast, reliable lookup if still in the cache
525 * 1/ i_generation - to see if i_ino is still valid
526 * bit 0 == 0 iff directory
527 * 2/ i_pos - if ino has changed, but still in cache (hi)
528 * 3/ i_pos - if ino has changed, but still in cache (low)
529 * 4/ i_logstart - to semi-verify inode found at i_location
530 * 5/ parent->i_logstart - maybe used to hunt for the file on disc
534 struct dentry
*fat_decode_fh(struct super_block
*sb
, __u32
*fh
,
536 int (*acceptable
)(void *context
, struct dentry
*de
),
541 return ERR_PTR(-ESTALE
);
543 return ERR_PTR(-ESTALE
);
545 return sb
->s_export_op
->find_exported_dentry(sb
, fh
, NULL
, acceptable
, context
);
548 struct dentry
*fat_get_dentry(struct super_block
*sb
, void *inump
)
550 struct inode
*inode
= NULL
;
551 struct dentry
*result
;
554 inode
= iget(sb
, fh
[0]);
555 if (!inode
|| is_bad_inode(inode
) ||
556 inode
->i_generation
!= fh
[1]) {
557 if (inode
) iput(inode
);
561 loff_t i_pos
= ((loff_t
)fh
[2] << 32) | fh
[3];
563 /* try 2 - see if i_pos is in F-d-c
564 * require i_logstart to be the same
565 * Will fail if you truncate and then re-write
568 inode
= fat_iget(sb
, i_pos
);
569 if (inode
&& MSDOS_I(inode
)->i_logstart
!= fh
[4]) {
575 /* For now, do nothing
576 * What we could do is:
577 * follow the file starting at fh[4], and record
578 * the ".." entry, and the name of the fh[2] entry.
579 * The follow the ".." file finding the next step up.
580 * This way we build a path to the root of
581 * the tree. If this works, we lookup the path and so
582 * get this inode into the cache.
583 * Finally try the fat_iget lookup again
584 * If that fails, then weare totally out of luck
585 * But all that is for another day
589 return ERR_PTR(-ESTALE
);
592 /* now to find a dentry.
593 * If possible, get a well-connected one
595 result
= d_alloc_anon(inode
);
596 if (result
== NULL
) {
598 return ERR_PTR(-ENOMEM
);
600 result
->d_op
= sb
->s_root
->d_op
;
604 int fat_encode_fh(struct dentry
*de
, __u32
*fh
, int *lenp
, int connectable
)
607 struct inode
*inode
= de
->d_inode
;
610 return 255; /* no room */
612 fh
[0] = inode
->i_ino
;
613 fh
[1] = inode
->i_generation
;
614 fh
[2] = (__u32
)(MSDOS_I(inode
)->i_pos
>> 32);
615 fh
[3] = (__u32
)MSDOS_I(inode
)->i_pos
;
616 fh
[4] = MSDOS_I(inode
)->i_logstart
;
617 spin_lock(&de
->d_lock
);
618 fh
[5] = MSDOS_I(de
->d_parent
->d_inode
)->i_logstart
;
619 spin_unlock(&de
->d_lock
);
623 struct dentry
*fat_get_parent(struct dentry
*child
)
625 struct buffer_head
*bh
=NULL
;
626 struct msdos_dir_entry
*de
= NULL
;
627 struct dentry
*parent
= NULL
;
633 res
= fat_scan(child
->d_inode
, MSDOS_DOTDOT
, &bh
, &de
, &i_pos
);
637 inode
= fat_build_inode(child
->d_sb
, de
, i_pos
, &res
);
643 parent
= d_alloc_anon(inode
);
660 static kmem_cache_t
*fat_inode_cachep
;
662 static struct inode
*fat_alloc_inode(struct super_block
*sb
)
664 struct msdos_inode_info
*ei
;
665 ei
= (struct msdos_inode_info
*)kmem_cache_alloc(fat_inode_cachep
, SLAB_KERNEL
);
668 return &ei
->vfs_inode
;
671 static void fat_destroy_inode(struct inode
*inode
)
673 kmem_cache_free(fat_inode_cachep
, MSDOS_I(inode
));
676 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
678 struct msdos_inode_info
*ei
= (struct msdos_inode_info
*) foo
;
680 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
681 SLAB_CTOR_CONSTRUCTOR
) {
682 INIT_LIST_HEAD(&ei
->i_fat_hash
);
683 inode_init_once(&ei
->vfs_inode
);
687 int __init
fat_init_inodecache(void)
689 fat_inode_cachep
= kmem_cache_create("fat_inode_cache",
690 sizeof(struct msdos_inode_info
),
691 0, SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
693 if (fat_inode_cachep
== NULL
)
698 void __exit
fat_destroy_inodecache(void)
700 if (kmem_cache_destroy(fat_inode_cachep
))
701 printk(KERN_INFO
"fat_inode_cache: not all structures were freed\n");
704 static struct super_operations fat_sops
= {
705 .alloc_inode
= fat_alloc_inode
,
706 .destroy_inode
= fat_destroy_inode
,
707 .write_inode
= fat_write_inode
,
708 .delete_inode
= fat_delete_inode
,
709 .put_super
= fat_put_super
,
710 .statfs
= fat_statfs
,
711 .clear_inode
= fat_clear_inode
,
713 .read_inode
= make_bad_inode
,
715 .show_options
= fat_show_options
,
718 static struct export_operations fat_export_ops
= {
719 .decode_fh
= fat_decode_fh
,
720 .encode_fh
= fat_encode_fh
,
721 .get_dentry
= fat_get_dentry
,
722 .get_parent
= fat_get_parent
,
726 * Read the super block of an MS-DOS FS.
728 int fat_fill_super(struct super_block
*sb
, void *data
, int silent
,
729 struct inode_operations
*fs_dir_inode_ops
, int isvfat
)
731 struct inode
*root_inode
= NULL
;
732 struct buffer_head
*bh
;
733 struct fat_boot_sector
*b
;
734 struct msdos_sb_info
*sbi
;
735 int logical_sector_size
, fat_clusters
, debug
, cp
, first
;
736 unsigned int total_sectors
, rootdir_sectors
;
741 sbi
= kmalloc(sizeof(struct msdos_sb_info
), GFP_KERNEL
);
745 memset(sbi
, 0, sizeof(struct msdos_sb_info
));
747 sb
->s_magic
= MSDOS_SUPER_MAGIC
;
748 sb
->s_op
= &fat_sops
;
749 sb
->s_export_op
= &fat_export_ops
;
750 sbi
->dir_ops
= fs_dir_inode_ops
;
753 if (!parse_options(data
, isvfat
, &debug
, &sbi
->options
))
757 /* set up enough so that it can read an inode */
758 init_MUTEX(&sbi
->fat_lock
);
761 sb_min_blocksize(sb
, 512);
762 bh
= sb_bread(sb
, 0);
764 printk(KERN_ERR
"FAT: unable to read boot sector\n");
768 b
= (struct fat_boot_sector
*) bh
->b_data
;
771 printk(KERN_ERR
"FAT: bogus number of reserved sectors\n");
777 printk(KERN_ERR
"FAT: bogus number of FAT structure\n");
781 if (!b
->secs_track
) {
783 printk(KERN_ERR
"FAT: bogus sectors-per-track value\n");
789 printk(KERN_ERR
"FAT: bogus number-of-heads value\n");
794 if (!FAT_VALID_MEDIA(media
)) {
796 printk(KERN_ERR
"FAT: invalid media value (0x%02x)\n",
801 logical_sector_size
=
802 CF_LE_W(get_unaligned((unsigned short *) &b
->sector_size
));
803 if (!logical_sector_size
804 || (logical_sector_size
& (logical_sector_size
- 1))
805 || (logical_sector_size
< 512)
806 || (PAGE_CACHE_SIZE
< logical_sector_size
)) {
808 printk(KERN_ERR
"FAT: bogus logical sector size %d\n",
809 logical_sector_size
);
813 sbi
->sec_per_clus
= b
->sec_per_clus
;
814 if (!sbi
->sec_per_clus
815 || (sbi
->sec_per_clus
& (sbi
->sec_per_clus
- 1))) {
817 printk(KERN_ERR
"FAT: bogus sectors per cluster %d\n",
823 if (logical_sector_size
< sb
->s_blocksize
) {
824 printk(KERN_ERR
"FAT: logical sector size too small for device"
825 " (logical sector size = %d)\n", logical_sector_size
);
829 if (logical_sector_size
> sb
->s_blocksize
) {
832 if (!sb_set_blocksize(sb
, logical_sector_size
)) {
833 printk(KERN_ERR
"FAT: unable to set blocksize %d\n",
834 logical_sector_size
);
837 bh
= sb_bread(sb
, 0);
839 printk(KERN_ERR
"FAT: unable to read boot sector"
840 " (logical sector size = %lu)\n",
844 b
= (struct fat_boot_sector
*) bh
->b_data
;
847 sbi
->cluster_size
= sb
->s_blocksize
* sbi
->sec_per_clus
;
848 sbi
->cluster_bits
= ffs(sbi
->cluster_size
) - 1;
850 sbi
->fat_bits
= 0; /* Don't know yet */
851 sbi
->fat_start
= CF_LE_W(b
->reserved
);
852 sbi
->fat_length
= CF_LE_W(b
->fat_length
);
853 sbi
->root_cluster
= 0;
854 sbi
->free_clusters
= -1; /* Don't know yet */
857 if (!sbi
->fat_length
&& b
->fat32_length
) {
858 struct fat_boot_fsinfo
*fsinfo
;
859 struct buffer_head
*fsinfo_bh
;
863 sbi
->fat_length
= CF_LE_L(b
->fat32_length
);
864 sbi
->root_cluster
= CF_LE_L(b
->root_cluster
);
866 sb
->s_maxbytes
= 0xffffffff;
868 /* MC - if info_sector is 0, don't multiply by 0 */
869 sbi
->fsinfo_sector
= CF_LE_W(b
->info_sector
);
870 if (sbi
->fsinfo_sector
== 0)
871 sbi
->fsinfo_sector
= 1;
873 fsinfo_bh
= sb_bread(sb
, sbi
->fsinfo_sector
);
874 if (fsinfo_bh
== NULL
) {
875 printk(KERN_ERR
"FAT: bread failed, FSINFO block"
876 " (sector = %lu)\n", sbi
->fsinfo_sector
);
881 fsinfo
= (struct fat_boot_fsinfo
*)fsinfo_bh
->b_data
;
882 if (!IS_FSINFO(fsinfo
)) {
884 "FAT: Did not find valid FSINFO signature.\n"
885 " Found signature1 0x%08x signature2 0x%08x"
887 CF_LE_L(fsinfo
->signature1
),
888 CF_LE_L(fsinfo
->signature2
),
891 sbi
->free_clusters
= CF_LE_L(fsinfo
->free_clusters
);
892 sbi
->prev_free
= CF_LE_L(fsinfo
->next_cluster
);
898 sbi
->dir_per_block
= sb
->s_blocksize
/ sizeof(struct msdos_dir_entry
);
899 sbi
->dir_per_block_bits
= ffs(sbi
->dir_per_block
) - 1;
901 sbi
->dir_start
= sbi
->fat_start
+ sbi
->fats
* sbi
->fat_length
;
903 CF_LE_W(get_unaligned((unsigned short *)&b
->dir_entries
));
904 if (sbi
->dir_entries
& (sbi
->dir_per_block
- 1)) {
905 printk(KERN_ERR
"FAT: bogus directroy-entries per block\n");
910 rootdir_sectors
= sbi
->dir_entries
911 * sizeof(struct msdos_dir_entry
) / sb
->s_blocksize
;
912 sbi
->data_start
= sbi
->dir_start
+ rootdir_sectors
;
913 total_sectors
= CF_LE_W(get_unaligned((unsigned short *)&b
->sectors
));
914 if (total_sectors
== 0)
915 total_sectors
= CF_LE_L(b
->total_sect
);
916 sbi
->clusters
= (total_sectors
- sbi
->data_start
) / sbi
->sec_per_clus
;
918 if (sbi
->fat_bits
!= 32)
919 sbi
->fat_bits
= (sbi
->clusters
> MSDOS_FAT12
) ? 16 : 12;
921 /* check that FAT table does not overflow */
922 fat_clusters
= sbi
->fat_length
* sb
->s_blocksize
* 8 / sbi
->fat_bits
;
923 if (sbi
->clusters
> fat_clusters
- 2)
924 sbi
->clusters
= fat_clusters
- 2;
928 /* validity check of FAT */
929 first
= __fat_access(sb
, 0, -1);
934 if (FAT_FIRST_ENT(sb
, media
) != first
935 && (media
!= 0xf8 || FAT_FIRST_ENT(sb
, 0xfe) != first
)) {
937 printk(KERN_ERR
"FAT: invalid first entry of FAT "
939 FAT_FIRST_ENT(sb
, media
), first
);
945 cp
= sbi
->options
.codepage
? sbi
->options
.codepage
: 437;
946 sprintf(buf
, "cp%d", cp
);
947 sbi
->nls_disk
= load_nls(buf
);
948 if (!sbi
->nls_disk
) {
949 /* Fail only if explicit charset specified */
950 if (sbi
->options
.codepage
!= 0) {
951 printk(KERN_ERR
"FAT: codepage %s not found\n", buf
);
954 sbi
->options
.codepage
= 0; /* already 0?? */
955 sbi
->nls_disk
= load_nls_default();
958 /* FIXME: utf8 is using iocharset for upper/lower conversion */
959 if (sbi
->options
.isvfat
) {
960 if (sbi
->options
.iocharset
!= NULL
) {
961 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
964 "FAT: IO charset %s not found\n",
965 sbi
->options
.iocharset
);
969 sbi
->nls_io
= load_nls_default();
973 root_inode
= new_inode(sb
);
976 root_inode
->i_ino
= MSDOS_ROOT_INO
;
977 root_inode
->i_version
= 1;
978 error
= fat_read_root(root_inode
);
982 insert_inode_hash(root_inode
);
983 sb
->s_root
= d_alloc_root(root_inode
);
985 printk(KERN_ERR
"FAT: get root inode failed\n");
994 printk(KERN_INFO
"VFS: Can't find a valid FAT filesystem"
995 " on dev %s.\n", sb
->s_id
);
1001 unload_nls(sbi
->nls_io
);
1003 unload_nls(sbi
->nls_disk
);
1004 if (sbi
->options
.iocharset
)
1005 kfree(sbi
->options
.iocharset
);
1006 sb
->s_fs_info
= NULL
;
1011 int fat_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
1015 if (MSDOS_SB(sb
)->free_clusters
!= -1)
1016 free
= MSDOS_SB(sb
)->free_clusters
;
1019 if (MSDOS_SB(sb
)->free_clusters
!= -1)
1020 free
= MSDOS_SB(sb
)->free_clusters
;
1023 for (nr
= 2; nr
< MSDOS_SB(sb
)->clusters
+ 2; nr
++)
1024 if (fat_access(sb
, nr
, -1) == FAT_ENT_FREE
)
1026 MSDOS_SB(sb
)->free_clusters
= free
;
1031 buf
->f_type
= sb
->s_magic
;
1032 buf
->f_bsize
= MSDOS_SB(sb
)->cluster_size
;
1033 buf
->f_blocks
= MSDOS_SB(sb
)->clusters
;
1034 buf
->f_bfree
= free
;
1035 buf
->f_bavail
= free
;
1036 buf
->f_namelen
= MSDOS_SB(sb
)->options
.isvfat
? 260 : 12;
1041 static int is_exec(unsigned char *extension
)
1043 unsigned char *exe_extensions
= "EXECOMBAT", *walk
;
1045 for (walk
= exe_extensions
; *walk
; walk
+= 3)
1046 if (!strncmp(extension
, walk
, 3))
1051 static int fat_writepage(struct page
*page
, struct writeback_control
*wbc
)
1053 return block_write_full_page(page
,fat_get_block
, wbc
);
1055 static int fat_readpage(struct file
*file
, struct page
*page
)
1057 return block_read_full_page(page
,fat_get_block
);
1061 fat_prepare_write(struct file
*file
, struct page
*page
,
1062 unsigned from
, unsigned to
)
1065 return cont_prepare_write(page
,from
,to
,fat_get_block
,
1066 &MSDOS_I(page
->mapping
->host
)->mmu_private
);
1070 fat_commit_write(struct file
*file
, struct page
*page
,
1071 unsigned from
, unsigned to
)
1074 return generic_commit_write(file
, page
, from
, to
);
1077 static sector_t
_fat_bmap(struct address_space
*mapping
, sector_t block
)
1079 return generic_block_bmap(mapping
,block
,fat_get_block
);
1081 static struct address_space_operations fat_aops
= {
1082 .readpage
= fat_readpage
,
1083 .writepage
= fat_writepage
,
1084 .sync_page
= block_sync_page
,
1085 .prepare_write
= fat_prepare_write
,
1086 .commit_write
= fat_commit_write
,
1090 /* doesn't deal with root inode */
1091 static int fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
)
1093 struct super_block
*sb
= inode
->i_sb
;
1094 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1097 MSDOS_I(inode
)->file_cluster
= MSDOS_I(inode
)->disk_cluster
= 0;
1098 MSDOS_I(inode
)->i_pos
= 0;
1099 inode
->i_uid
= sbi
->options
.fs_uid
;
1100 inode
->i_gid
= sbi
->options
.fs_gid
;
1102 inode
->i_generation
= get_seconds();
1104 if ((de
->attr
& ATTR_DIR
) && !IS_FREE(de
->name
)) {
1105 inode
->i_generation
&= ~1;
1106 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,
1107 S_IRWXUGO
& ~sbi
->options
.fs_dmask
) | S_IFDIR
;
1108 inode
->i_op
= sbi
->dir_ops
;
1109 inode
->i_fop
= &fat_dir_operations
;
1111 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
1112 if (sbi
->fat_bits
== 32)
1113 MSDOS_I(inode
)->i_start
|= (CF_LE_W(de
->starthi
) << 16);
1115 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
1116 error
= fat_calc_dir_size(inode
);
1119 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
1121 inode
->i_nlink
= fat_subdirs(inode
);
1122 } else { /* not a directory */
1123 inode
->i_generation
|= 1;
1124 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,
1125 ((sbi
->options
.showexec
&&
1127 ? S_IRUGO
|S_IWUGO
: S_IRWXUGO
)
1128 & ~sbi
->options
.fs_fmask
) | S_IFREG
;
1129 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
1130 if (sbi
->fat_bits
== 32)
1131 MSDOS_I(inode
)->i_start
|= (CF_LE_W(de
->starthi
) << 16);
1133 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
1134 inode
->i_size
= CF_LE_L(de
->size
);
1135 inode
->i_op
= &fat_file_inode_operations
;
1136 inode
->i_fop
= &fat_file_operations
;
1137 inode
->i_mapping
->a_ops
= &fat_aops
;
1138 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
1140 if(de
->attr
& ATTR_SYS
)
1141 if (sbi
->options
.sys_immutable
)
1142 inode
->i_flags
|= S_IMMUTABLE
;
1143 MSDOS_I(inode
)->i_attrs
= de
->attr
& ATTR_UNUSED
;
1144 /* this is as close to the truth as we can get ... */
1145 inode
->i_blksize
= sbi
->cluster_size
;
1146 inode
->i_blocks
= ((inode
->i_size
+ (sbi
->cluster_size
- 1))
1147 & ~((loff_t
)sbi
->cluster_size
- 1)) >> 9;
1148 inode
->i_mtime
.tv_sec
= inode
->i_atime
.tv_sec
=
1149 date_dos2unix(CF_LE_W(de
->time
),CF_LE_W(de
->date
));
1150 inode
->i_mtime
.tv_nsec
= inode
->i_atime
.tv_nsec
= 0;
1151 inode
->i_ctime
.tv_sec
=
1152 MSDOS_SB(sb
)->options
.isvfat
1153 ? date_dos2unix(CF_LE_W(de
->ctime
),CF_LE_W(de
->cdate
))
1154 : inode
->i_mtime
.tv_sec
;
1155 inode
->i_ctime
.tv_nsec
= de
->ctime_ms
* 1000000;
1156 MSDOS_I(inode
)->i_ctime_ms
= de
->ctime_ms
;
1161 void fat_write_inode(struct inode
*inode
, int wait
)
1163 struct super_block
*sb
= inode
->i_sb
;
1164 struct buffer_head
*bh
;
1165 struct msdos_dir_entry
*raw_entry
;
1169 i_pos
= MSDOS_I(inode
)->i_pos
;
1170 if (inode
->i_ino
== MSDOS_ROOT_INO
|| !i_pos
) {
1174 if (!(bh
= sb_bread(sb
, i_pos
>> MSDOS_SB(sb
)->dir_per_block_bits
))) {
1175 fat_fs_panic(sb
, "unable to read i-node block (i_pos %llu)",
1180 spin_lock(&fat_inode_lock
);
1181 if (i_pos
!= MSDOS_I(inode
)->i_pos
) {
1182 spin_unlock(&fat_inode_lock
);
1188 raw_entry
= &((struct msdos_dir_entry
*) (bh
->b_data
))
1189 [i_pos
& (MSDOS_SB(sb
)->dir_per_block
- 1)];
1190 if (S_ISDIR(inode
->i_mode
)) {
1191 raw_entry
->attr
= ATTR_DIR
;
1192 raw_entry
->size
= 0;
1195 raw_entry
->attr
= ATTR_NONE
;
1196 raw_entry
->size
= CT_LE_L(inode
->i_size
);
1198 raw_entry
->attr
|= MSDOS_MKATTR(inode
->i_mode
) |
1199 MSDOS_I(inode
)->i_attrs
;
1200 raw_entry
->start
= CT_LE_W(MSDOS_I(inode
)->i_logstart
);
1201 raw_entry
->starthi
= CT_LE_W(MSDOS_I(inode
)->i_logstart
>> 16);
1202 fat_date_unix2dos(inode
->i_mtime
.tv_sec
,&raw_entry
->time
,&raw_entry
->date
);
1203 raw_entry
->time
= CT_LE_W(raw_entry
->time
);
1204 raw_entry
->date
= CT_LE_W(raw_entry
->date
);
1205 if (MSDOS_SB(sb
)->options
.isvfat
) {
1206 fat_date_unix2dos(inode
->i_ctime
.tv_sec
,&raw_entry
->ctime
,&raw_entry
->cdate
);
1207 raw_entry
->ctime_ms
= MSDOS_I(inode
)->i_ctime_ms
; /* use i_ctime.tv_nsec? */
1208 raw_entry
->ctime
= CT_LE_W(raw_entry
->ctime
);
1209 raw_entry
->cdate
= CT_LE_W(raw_entry
->cdate
);
1211 spin_unlock(&fat_inode_lock
);
1212 mark_buffer_dirty(bh
);
1218 int fat_notify_change(struct dentry
* dentry
, struct iattr
* attr
)
1220 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
1221 struct inode
*inode
= dentry
->d_inode
;
1222 int mask
, error
= 0;
1226 /* FAT cannot truncate to a longer file */
1227 if (attr
->ia_valid
& ATTR_SIZE
) {
1228 if (attr
->ia_size
> inode
->i_size
) {
1234 error
= inode_change_ok(inode
, attr
);
1236 if (sbi
->options
.quiet
)
1241 if (((attr
->ia_valid
& ATTR_UID
) &&
1242 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
1243 ((attr
->ia_valid
& ATTR_GID
) &&
1244 (attr
->ia_gid
!= sbi
->options
.fs_gid
)) ||
1245 ((attr
->ia_valid
& ATTR_MODE
) &&
1246 (attr
->ia_mode
& ~MSDOS_VALID_MODE
)))
1250 if (sbi
->options
.quiet
)
1254 error
= inode_setattr(inode
, attr
);
1258 if (S_ISDIR(inode
->i_mode
))
1259 mask
= sbi
->options
.fs_dmask
;
1261 mask
= sbi
->options
.fs_fmask
;
1262 inode
->i_mode
&= S_IFMT
| (S_IRWXUGO
& ~mask
);
1267 MODULE_LICENSE("GPL");