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 fat_clusters_flush(sb
);
164 unload_nls(sbi
->nls_disk
);
165 sbi
->nls_disk
= NULL
;
166 sbi
->options
.codepage
= 0;
169 unload_nls(sbi
->nls_io
);
173 * Note: the iocharset option might have been specified
174 * without enabling nls_io, so check for it here.
176 if (sbi
->options
.iocharset
) {
177 kfree(sbi
->options
.iocharset
);
178 sbi
->options
.iocharset
= NULL
;
180 sb
->s_fs_info
= NULL
;
184 static int simple_getbool(char *s
, int *setval
)
187 if (!strcmp(s
,"1") || !strcmp(s
,"yes") || !strcmp(s
,"true"))
189 else if (!strcmp(s
,"0") || !strcmp(s
,"no") || !strcmp(s
,"false"))
198 static int fat_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
200 struct msdos_sb_info
*sbi
= MSDOS_SB(mnt
->mnt_sb
);
201 struct fat_mount_options
*opts
= &sbi
->options
;
202 int isvfat
= opts
->isvfat
;
204 if (opts
->fs_uid
!= 0)
205 seq_printf(m
, ",uid=%d", opts
->fs_uid
);
206 if (opts
->fs_gid
!= 0)
207 seq_printf(m
, ",gid=%d", opts
->fs_gid
);
208 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask
);
209 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask
);
211 seq_printf(m
, ",codepage=%s", sbi
->nls_disk
->charset
);
214 && strcmp(sbi
->nls_io
->charset
, CONFIG_NLS_DEFAULT
))
215 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
217 switch (opts
->shortname
) {
218 case VFAT_SFN_DISPLAY_WIN95
| VFAT_SFN_CREATE_WIN95
:
219 seq_puts(m
, ",shortname=win95");
221 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WINNT
:
222 seq_puts(m
, ",shortname=winnt");
224 case VFAT_SFN_DISPLAY_WINNT
| VFAT_SFN_CREATE_WIN95
:
225 seq_puts(m
, ",shortname=mixed");
227 case VFAT_SFN_DISPLAY_LOWER
| VFAT_SFN_CREATE_WIN95
:
228 /* seq_puts(m, ",shortname=lower"); */
231 seq_puts(m
, ",shortname=unknown");
235 if (opts
->name_check
!= 'n')
236 seq_printf(m
, ",check=%c", opts
->name_check
);
238 seq_puts(m
, ",quiet");
240 seq_puts(m
, ",showexec");
241 if (opts
->sys_immutable
)
242 seq_puts(m
, ",sys_immutable");
245 seq_puts(m
, ",dotsOK=yes");
247 seq_puts(m
, ",nocase");
250 seq_puts(m
, ",utf8");
251 if (opts
->unicode_xlate
)
252 seq_puts(m
, ",uni_xlate");
254 seq_puts(m
, ",nonumtail");
260 static int parse_options(char *options
, int is_vfat
, int *debug
,
261 struct fat_mount_options
*opts
)
263 char *this_char
, *value
, *p
;
264 int ret
= 1, val
, len
;
266 opts
->isvfat
= is_vfat
;
268 opts
->fs_uid
= current
->uid
;
269 opts
->fs_gid
= current
->gid
;
270 opts
->fs_fmask
= opts
->fs_dmask
= current
->fs
->umask
;
272 opts
->iocharset
= NULL
;
274 opts
->shortname
= VFAT_SFN_DISPLAY_LOWER
|VFAT_SFN_CREATE_WIN95
;
277 opts
->name_check
= 'n';
278 opts
->quiet
= opts
->showexec
= opts
->sys_immutable
= opts
->dotsOK
= 0;
279 opts
->utf8
= opts
->unicode_xlate
= 0;
286 while ((this_char
= strsep(&options
,",")) != NULL
) {
289 if ((value
= strchr(this_char
,'=')) != NULL
)
292 if (!strcmp(this_char
,"check") && value
) {
293 if (value
[0] && !value
[1] && strchr("rns",*value
))
294 opts
->name_check
= *value
;
295 else if (!strcmp(value
,"relaxed"))
296 opts
->name_check
= 'r';
297 else if (!strcmp(value
,"normal"))
298 opts
->name_check
= 'n';
299 else if (!strcmp(value
,"strict"))
300 opts
->name_check
= 's';
303 else if (!strcmp(this_char
,"conv") && value
) {
304 printk(KERN_INFO
"FAT: conv option is obsolete, "
305 "not supported now\n");
307 else if (!strcmp(this_char
,"nocase")) {
311 /* for backward compatible */
312 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
313 | VFAT_SFN_CREATE_WIN95
;
316 else if (!strcmp(this_char
,"showexec")) {
319 else if (!strcmp(this_char
,"uid")) {
320 if (!value
|| !*value
) ret
= 0;
322 opts
->fs_uid
= simple_strtoul(value
,&value
,0);
326 else if (!strcmp(this_char
,"gid")) {
327 if (!value
|| !*value
) ret
= 0;
329 opts
->fs_gid
= simple_strtoul(value
,&value
,0);
333 else if (!strcmp(this_char
,"umask")) {
334 if (!value
|| !*value
) ret
= 0;
336 opts
->fs_fmask
= opts
->fs_dmask
=
337 simple_strtoul(value
,&value
,8);
341 else if (!strcmp(this_char
,"fmask")) {
342 if (!value
|| !*value
) ret
= 0;
344 opts
->fs_fmask
= simple_strtoul(value
,&value
,8);
348 else if (!strcmp(this_char
,"dmask")) {
349 if (!value
|| !*value
) ret
= 0;
351 opts
->fs_dmask
= simple_strtoul(value
,&value
,8);
355 else if (!strcmp(this_char
,"debug")) {
359 else if (!strcmp(this_char
,"fat")) {
360 printk(KERN_INFO
"FAT: fat option is obsolete, "
361 "not supported now\n");
363 else if (!strcmp(this_char
,"quiet")) {
365 else opts
->quiet
= 1;
367 else if (!strcmp(this_char
,"blocksize")) {
368 printk(KERN_INFO
"FAT: blocksize option is obsolete, "
369 "not supported now\n");
371 else if (!strcmp(this_char
,"sys_immutable")) {
373 else opts
->sys_immutable
= 1;
375 else if (!strcmp(this_char
,"codepage") && value
) {
376 opts
->codepage
= simple_strtoul(value
,&value
,0);
381 else if (!is_vfat
&& !strcmp(this_char
,"dots")) {
384 else if (!is_vfat
&& !strcmp(this_char
,"nodots")) {
387 else if (!is_vfat
&& !strcmp(this_char
,"dotsOK") && value
) {
388 if (!strcmp(value
,"yes")) opts
->dotsOK
= 1;
389 else if (!strcmp(value
,"no")) opts
->dotsOK
= 0;
394 else if (is_vfat
&& !strcmp(this_char
,"iocharset") && value
) {
396 while (*value
&& *value
!= ',')
402 if (opts
->iocharset
!= NULL
) {
403 kfree(opts
->iocharset
);
404 opts
->iocharset
= NULL
;
406 buffer
= kmalloc(len
+ 1, GFP_KERNEL
);
407 if (buffer
!= NULL
) {
408 opts
->iocharset
= buffer
;
409 memcpy(buffer
, p
, len
);
415 else if (is_vfat
&& !strcmp(this_char
,"utf8")) {
416 ret
= simple_getbool(value
, &val
);
417 if (ret
) opts
->utf8
= val
;
419 else if (is_vfat
&& !strcmp(this_char
,"uni_xlate")) {
420 ret
= simple_getbool(value
, &val
);
421 if (ret
) opts
->unicode_xlate
= val
;
423 else if (is_vfat
&& !strcmp(this_char
,"posix")) {
424 printk(KERN_INFO
"FAT: posix option is obsolete, "
425 "not supported now\n");
427 else if (is_vfat
&& !strcmp(this_char
,"nonumtail")) {
428 ret
= simple_getbool(value
, &val
);
430 opts
->numtail
= !val
;
433 else if (is_vfat
&& !strcmp(this_char
, "shortname")) {
434 if (!strcmp(value
, "lower"))
435 opts
->shortname
= VFAT_SFN_DISPLAY_LOWER
436 | VFAT_SFN_CREATE_WIN95
;
437 else if (!strcmp(value
, "win95"))
438 opts
->shortname
= VFAT_SFN_DISPLAY_WIN95
439 | VFAT_SFN_CREATE_WIN95
;
440 else if (!strcmp(value
, "winnt"))
441 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
442 | VFAT_SFN_CREATE_WINNT
;
443 else if (!strcmp(value
, "mixed"))
444 opts
->shortname
= VFAT_SFN_DISPLAY_WINNT
445 | VFAT_SFN_CREATE_WIN95
;
449 printk(KERN_ERR
"FAT: Unrecognized mount option %s\n",
458 if (opts
->unicode_xlate
)
464 static int fat_calc_dir_size(struct inode
*inode
)
466 struct super_block
*sb
= inode
->i_sb
;
470 if (MSDOS_I(inode
)->i_start
== 0)
473 nr
= MSDOS_I(inode
)->i_start
;
475 inode
->i_size
+= 1 << MSDOS_SB(sb
)->cluster_bits
;
476 nr
= fat_access(sb
, nr
, -1);
479 else if (nr
== FAT_ENT_FREE
) {
480 fat_fs_panic(sb
, "Directory %lu: invalid cluster chain",
484 if (inode
->i_size
> FAT_MAX_DIR_SIZE
) {
485 fat_fs_panic(sb
, "Directory %lu: "
486 "exceeded the maximum size of directory",
488 inode
->i_size
= FAT_MAX_DIR_SIZE
;
491 } while (nr
!= FAT_ENT_EOF
);
496 static int fat_read_root(struct inode
*inode
)
498 struct super_block
*sb
= inode
->i_sb
;
499 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
502 MSDOS_I(inode
)->i_pos
= 0;
503 inode
->i_uid
= sbi
->options
.fs_uid
;
504 inode
->i_gid
= sbi
->options
.fs_gid
;
506 inode
->i_generation
= 0;
507 inode
->i_mode
= (S_IRWXUGO
& ~sbi
->options
.fs_dmask
) | S_IFDIR
;
508 inode
->i_op
= sbi
->dir_ops
;
509 inode
->i_fop
= &fat_dir_operations
;
510 if (sbi
->fat_bits
== 32) {
511 MSDOS_I(inode
)->i_start
= sbi
->root_cluster
;
512 error
= fat_calc_dir_size(inode
);
516 MSDOS_I(inode
)->i_start
= 0;
517 inode
->i_size
= sbi
->dir_entries
* sizeof(struct msdos_dir_entry
);
519 inode
->i_blksize
= 1 << sbi
->cluster_bits
;
520 inode
->i_blocks
= ((inode
->i_size
+ inode
->i_blksize
- 1)
521 & ~((loff_t
)inode
->i_blksize
- 1)) >> 9;
522 MSDOS_I(inode
)->i_logstart
= 0;
523 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
525 MSDOS_I(inode
)->i_attrs
= 0;
526 inode
->i_mtime
.tv_sec
= inode
->i_atime
.tv_sec
= inode
->i_ctime
.tv_sec
= 0;
527 inode
->i_mtime
.tv_nsec
= inode
->i_atime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
528 MSDOS_I(inode
)->i_ctime_ms
= 0;
529 inode
->i_nlink
= fat_subdirs(inode
)+2;
535 * a FAT file handle with fhtype 3 is
536 * 0/ i_ino - for fast, reliable lookup if still in the cache
537 * 1/ i_generation - to see if i_ino is still valid
538 * bit 0 == 0 iff directory
539 * 2/ i_pos - if ino has changed, but still in cache (hi)
540 * 3/ i_pos - if ino has changed, but still in cache (low)
541 * 4/ i_logstart - to semi-verify inode found at i_location
542 * 5/ parent->i_logstart - maybe used to hunt for the file on disc
546 struct dentry
*fat_decode_fh(struct super_block
*sb
, __u32
*fh
,
548 int (*acceptable
)(void *context
, struct dentry
*de
),
553 return ERR_PTR(-ESTALE
);
555 return ERR_PTR(-ESTALE
);
557 return sb
->s_export_op
->find_exported_dentry(sb
, fh
, NULL
, acceptable
, context
);
560 struct dentry
*fat_get_dentry(struct super_block
*sb
, void *inump
)
562 struct inode
*inode
= NULL
;
563 struct dentry
*result
;
566 inode
= iget(sb
, fh
[0]);
567 if (!inode
|| is_bad_inode(inode
) ||
568 inode
->i_generation
!= fh
[1]) {
569 if (inode
) iput(inode
);
573 loff_t i_pos
= ((loff_t
)fh
[2] << 32) | fh
[3];
575 /* try 2 - see if i_pos is in F-d-c
576 * require i_logstart to be the same
577 * Will fail if you truncate and then re-write
580 inode
= fat_iget(sb
, i_pos
);
581 if (inode
&& MSDOS_I(inode
)->i_logstart
!= fh
[4]) {
587 /* For now, do nothing
588 * What we could do is:
589 * follow the file starting at fh[4], and record
590 * the ".." entry, and the name of the fh[2] entry.
591 * The follow the ".." file finding the next step up.
592 * This way we build a path to the root of
593 * the tree. If this works, we lookup the path and so
594 * get this inode into the cache.
595 * Finally try the fat_iget lookup again
596 * If that fails, then weare totally out of luck
597 * But all that is for another day
601 return ERR_PTR(-ESTALE
);
604 /* now to find a dentry.
605 * If possible, get a well-connected one
607 result
= d_alloc_anon(inode
);
608 if (result
== NULL
) {
610 return ERR_PTR(-ENOMEM
);
612 result
->d_op
= sb
->s_root
->d_op
;
616 int fat_encode_fh(struct dentry
*de
, __u32
*fh
, int *lenp
, int connectable
)
619 struct inode
*inode
= de
->d_inode
;
622 return 255; /* no room */
624 fh
[0] = inode
->i_ino
;
625 fh
[1] = inode
->i_generation
;
626 fh
[2] = (__u32
)(MSDOS_I(inode
)->i_pos
>> 32);
627 fh
[3] = (__u32
)MSDOS_I(inode
)->i_pos
;
628 fh
[4] = MSDOS_I(inode
)->i_logstart
;
629 spin_lock(&de
->d_lock
);
630 fh
[5] = MSDOS_I(de
->d_parent
->d_inode
)->i_logstart
;
631 spin_unlock(&de
->d_lock
);
635 struct dentry
*fat_get_parent(struct dentry
*child
)
637 struct buffer_head
*bh
=NULL
;
638 struct msdos_dir_entry
*de
= NULL
;
639 struct dentry
*parent
= NULL
;
645 res
= fat_scan(child
->d_inode
, MSDOS_DOTDOT
, &bh
, &de
, &i_pos
);
649 inode
= fat_build_inode(child
->d_sb
, de
, i_pos
, &res
);
655 parent
= d_alloc_anon(inode
);
672 static kmem_cache_t
*fat_inode_cachep
;
674 static struct inode
*fat_alloc_inode(struct super_block
*sb
)
676 struct msdos_inode_info
*ei
;
677 ei
= (struct msdos_inode_info
*)kmem_cache_alloc(fat_inode_cachep
, SLAB_KERNEL
);
680 return &ei
->vfs_inode
;
683 static void fat_destroy_inode(struct inode
*inode
)
685 kmem_cache_free(fat_inode_cachep
, MSDOS_I(inode
));
688 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
690 struct msdos_inode_info
*ei
= (struct msdos_inode_info
*) foo
;
692 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
693 SLAB_CTOR_CONSTRUCTOR
) {
694 INIT_LIST_HEAD(&ei
->i_fat_hash
);
695 inode_init_once(&ei
->vfs_inode
);
699 int __init
fat_init_inodecache(void)
701 fat_inode_cachep
= kmem_cache_create("fat_inode_cache",
702 sizeof(struct msdos_inode_info
),
703 0, SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
705 if (fat_inode_cachep
== NULL
)
710 void __exit
fat_destroy_inodecache(void)
712 if (kmem_cache_destroy(fat_inode_cachep
))
713 printk(KERN_INFO
"fat_inode_cache: not all structures were freed\n");
716 static struct super_operations fat_sops
= {
717 .alloc_inode
= fat_alloc_inode
,
718 .destroy_inode
= fat_destroy_inode
,
719 .write_inode
= fat_write_inode
,
720 .delete_inode
= fat_delete_inode
,
721 .put_super
= fat_put_super
,
722 .statfs
= fat_statfs
,
723 .clear_inode
= fat_clear_inode
,
725 .read_inode
= make_bad_inode
,
727 .show_options
= fat_show_options
,
730 static struct export_operations fat_export_ops
= {
731 .decode_fh
= fat_decode_fh
,
732 .encode_fh
= fat_encode_fh
,
733 .get_dentry
= fat_get_dentry
,
734 .get_parent
= fat_get_parent
,
738 * Read the super block of an MS-DOS FS.
740 int fat_fill_super(struct super_block
*sb
, void *data
, int silent
,
741 struct inode_operations
*fs_dir_inode_ops
, int isvfat
)
743 struct inode
*root_inode
= NULL
;
744 struct buffer_head
*bh
;
745 struct fat_boot_sector
*b
;
746 struct msdos_sb_info
*sbi
;
747 int logical_sector_size
, fat_clusters
, debug
, cp
, first
;
748 unsigned int total_sectors
, rootdir_sectors
;
753 sbi
= kmalloc(sizeof(struct msdos_sb_info
), GFP_KERNEL
);
757 memset(sbi
, 0, sizeof(struct msdos_sb_info
));
759 sb
->s_magic
= MSDOS_SUPER_MAGIC
;
760 sb
->s_op
= &fat_sops
;
761 sb
->s_export_op
= &fat_export_ops
;
762 sbi
->dir_ops
= fs_dir_inode_ops
;
765 if (!parse_options(data
, isvfat
, &debug
, &sbi
->options
))
769 /* set up enough so that it can read an inode */
770 init_MUTEX(&sbi
->fat_lock
);
773 sb_min_blocksize(sb
, 512);
774 bh
= sb_bread(sb
, 0);
776 printk(KERN_ERR
"FAT: unable to read boot sector\n");
780 b
= (struct fat_boot_sector
*) bh
->b_data
;
783 printk(KERN_ERR
"FAT: bogus number of reserved sectors\n");
789 printk(KERN_ERR
"FAT: bogus number of FAT structure\n");
793 if (!b
->secs_track
) {
795 printk(KERN_ERR
"FAT: bogus sectors-per-track value\n");
801 printk(KERN_ERR
"FAT: bogus number-of-heads value\n");
806 if (!FAT_VALID_MEDIA(media
)) {
808 printk(KERN_ERR
"FAT: invalid media value (0x%02x)\n",
813 logical_sector_size
=
814 CF_LE_W(get_unaligned((unsigned short *) &b
->sector_size
));
815 if (!logical_sector_size
816 || (logical_sector_size
& (logical_sector_size
- 1))
817 || (logical_sector_size
< 512)
818 || (PAGE_CACHE_SIZE
< logical_sector_size
)) {
820 printk(KERN_ERR
"FAT: bogus logical sector size %d\n",
821 logical_sector_size
);
825 sbi
->cluster_size
= b
->cluster_size
;
826 if (!sbi
->cluster_size
827 || (sbi
->cluster_size
& (sbi
->cluster_size
- 1))) {
829 printk(KERN_ERR
"FAT: bogus cluster size %d\n",
835 if (logical_sector_size
< sb
->s_blocksize
) {
836 printk(KERN_ERR
"FAT: logical sector size too small for device"
837 " (logical sector size = %d)\n", logical_sector_size
);
841 if (logical_sector_size
> sb
->s_blocksize
) {
844 if (!sb_set_blocksize(sb
, logical_sector_size
)) {
845 printk(KERN_ERR
"FAT: unable to set blocksize %d\n",
846 logical_sector_size
);
849 bh
= sb_bread(sb
, 0);
851 printk(KERN_ERR
"FAT: unable to read boot sector"
852 " (logical sector size = %lu)\n",
856 b
= (struct fat_boot_sector
*) bh
->b_data
;
859 sbi
->cluster_bits
= ffs(sb
->s_blocksize
* sbi
->cluster_size
) - 1;
861 sbi
->fat_bits
= 0; /* Don't know yet */
862 sbi
->fat_start
= CF_LE_W(b
->reserved
);
863 sbi
->fat_length
= CF_LE_W(b
->fat_length
);
864 sbi
->root_cluster
= 0;
865 sbi
->free_clusters
= -1; /* Don't know yet */
868 if (!sbi
->fat_length
&& b
->fat32_length
) {
869 struct fat_boot_fsinfo
*fsinfo
;
870 struct buffer_head
*fsinfo_bh
;
874 sbi
->fat_length
= CF_LE_L(b
->fat32_length
);
875 sbi
->root_cluster
= CF_LE_L(b
->root_cluster
);
877 sb
->s_maxbytes
= 0xffffffff;
879 /* MC - if info_sector is 0, don't multiply by 0 */
880 sbi
->fsinfo_sector
= CF_LE_W(b
->info_sector
);
881 if (sbi
->fsinfo_sector
== 0)
882 sbi
->fsinfo_sector
= 1;
884 fsinfo_bh
= sb_bread(sb
, sbi
->fsinfo_sector
);
885 if (fsinfo_bh
== NULL
) {
886 printk(KERN_ERR
"FAT: bread failed, FSINFO block"
887 " (sector = %lu)\n", sbi
->fsinfo_sector
);
892 fsinfo
= (struct fat_boot_fsinfo
*)fsinfo_bh
->b_data
;
893 if (!IS_FSINFO(fsinfo
)) {
895 "FAT: Did not find valid FSINFO signature.\n"
896 " Found signature1 0x%08x signature2 0x%08x"
898 CF_LE_L(fsinfo
->signature1
),
899 CF_LE_L(fsinfo
->signature2
),
902 sbi
->free_clusters
= CF_LE_L(fsinfo
->free_clusters
);
903 sbi
->prev_free
= CF_LE_L(fsinfo
->next_cluster
);
909 sbi
->dir_per_block
= sb
->s_blocksize
/ sizeof(struct msdos_dir_entry
);
910 sbi
->dir_per_block_bits
= ffs(sbi
->dir_per_block
) - 1;
912 sbi
->dir_start
= sbi
->fat_start
+ sbi
->fats
* sbi
->fat_length
;
914 CF_LE_W(get_unaligned((unsigned short *)&b
->dir_entries
));
915 if (sbi
->dir_entries
& (sbi
->dir_per_block
- 1)) {
916 printk(KERN_ERR
"FAT: bogus directroy-entries per block\n");
921 rootdir_sectors
= sbi
->dir_entries
922 * sizeof(struct msdos_dir_entry
) / sb
->s_blocksize
;
923 sbi
->data_start
= sbi
->dir_start
+ rootdir_sectors
;
924 total_sectors
= CF_LE_W(get_unaligned((unsigned short *)&b
->sectors
));
925 if (total_sectors
== 0)
926 total_sectors
= CF_LE_L(b
->total_sect
);
927 sbi
->clusters
= (total_sectors
- sbi
->data_start
) / sbi
->cluster_size
;
929 if (sbi
->fat_bits
!= 32)
930 sbi
->fat_bits
= (sbi
->clusters
> MSDOS_FAT12
) ? 16 : 12;
932 /* check that FAT table does not overflow */
933 fat_clusters
= sbi
->fat_length
* sb
->s_blocksize
* 8 / sbi
->fat_bits
;
934 if (sbi
->clusters
> fat_clusters
- 2)
935 sbi
->clusters
= fat_clusters
- 2;
939 /* validity check of FAT */
940 first
= __fat_access(sb
, 0, -1);
945 if (FAT_FIRST_ENT(sb
, media
) != first
946 && (media
!= 0xf8 || FAT_FIRST_ENT(sb
, 0xfe) != first
)) {
948 printk(KERN_ERR
"FAT: invalid first entry of FAT "
950 FAT_FIRST_ENT(sb
, media
), first
);
956 cp
= sbi
->options
.codepage
? sbi
->options
.codepage
: 437;
957 sprintf(buf
, "cp%d", cp
);
958 sbi
->nls_disk
= load_nls(buf
);
959 if (!sbi
->nls_disk
) {
960 /* Fail only if explicit charset specified */
961 if (sbi
->options
.codepage
!= 0) {
962 printk(KERN_ERR
"FAT: codepage %s not found\n", buf
);
965 sbi
->options
.codepage
= 0; /* already 0?? */
966 sbi
->nls_disk
= load_nls_default();
969 /* FIXME: utf8 is using iocharset for upper/lower conversion */
970 if (sbi
->options
.isvfat
) {
971 if (sbi
->options
.iocharset
!= NULL
) {
972 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
975 "FAT: IO charset %s not found\n",
976 sbi
->options
.iocharset
);
980 sbi
->nls_io
= load_nls_default();
984 root_inode
= new_inode(sb
);
987 root_inode
->i_ino
= MSDOS_ROOT_INO
;
988 root_inode
->i_version
= 1;
989 error
= fat_read_root(root_inode
);
993 insert_inode_hash(root_inode
);
994 sb
->s_root
= d_alloc_root(root_inode
);
996 printk(KERN_ERR
"FAT: get root inode failed\n");
1005 printk(KERN_INFO
"VFS: Can't find a valid FAT filesystem"
1006 " on dev %s.\n", sb
->s_id
);
1012 unload_nls(sbi
->nls_io
);
1014 unload_nls(sbi
->nls_disk
);
1015 if (sbi
->options
.iocharset
)
1016 kfree(sbi
->options
.iocharset
);
1017 sb
->s_fs_info
= NULL
;
1022 int fat_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
1027 if (MSDOS_SB(sb
)->free_clusters
!= -1)
1028 free
= MSDOS_SB(sb
)->free_clusters
;
1031 for (nr
= 2; nr
< MSDOS_SB(sb
)->clusters
+ 2; nr
++)
1032 if (fat_access(sb
, nr
, -1) == FAT_ENT_FREE
)
1034 MSDOS_SB(sb
)->free_clusters
= free
;
1038 buf
->f_type
= sb
->s_magic
;
1039 buf
->f_bsize
= 1 << MSDOS_SB(sb
)->cluster_bits
;
1040 buf
->f_blocks
= MSDOS_SB(sb
)->clusters
;
1041 buf
->f_bfree
= free
;
1042 buf
->f_bavail
= free
;
1043 buf
->f_namelen
= MSDOS_SB(sb
)->options
.isvfat
? 260 : 12;
1048 static int is_exec(char *extension
)
1050 char *exe_extensions
= "EXECOMBAT", *walk
;
1052 for (walk
= exe_extensions
; *walk
; walk
+= 3)
1053 if (!strncmp(extension
, walk
, 3))
1058 static int fat_writepage(struct page
*page
, struct writeback_control
*wbc
)
1060 return block_write_full_page(page
,fat_get_block
, wbc
);
1062 static int fat_readpage(struct file
*file
, struct page
*page
)
1064 return block_read_full_page(page
,fat_get_block
);
1068 fat_prepare_write(struct file
*file
, struct page
*page
,
1069 unsigned from
, unsigned to
)
1072 return cont_prepare_write(page
,from
,to
,fat_get_block
,
1073 &MSDOS_I(page
->mapping
->host
)->mmu_private
);
1077 fat_commit_write(struct file
*file
, struct page
*page
,
1078 unsigned from
, unsigned to
)
1081 return generic_commit_write(file
, page
, from
, to
);
1084 static sector_t
_fat_bmap(struct address_space
*mapping
, sector_t block
)
1086 return generic_block_bmap(mapping
,block
,fat_get_block
);
1088 static struct address_space_operations fat_aops
= {
1089 .readpage
= fat_readpage
,
1090 .writepage
= fat_writepage
,
1091 .sync_page
= block_sync_page
,
1092 .prepare_write
= fat_prepare_write
,
1093 .commit_write
= fat_commit_write
,
1097 /* doesn't deal with root inode */
1098 static int fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
)
1100 struct super_block
*sb
= inode
->i_sb
;
1101 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1104 MSDOS_I(inode
)->file_cluster
= MSDOS_I(inode
)->disk_cluster
= 0;
1105 MSDOS_I(inode
)->i_pos
= 0;
1106 inode
->i_uid
= sbi
->options
.fs_uid
;
1107 inode
->i_gid
= sbi
->options
.fs_gid
;
1109 inode
->i_generation
= get_seconds();
1111 if ((de
->attr
& ATTR_DIR
) && !IS_FREE(de
->name
)) {
1112 inode
->i_generation
&= ~1;
1113 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,
1114 S_IRWXUGO
& ~sbi
->options
.fs_dmask
) | S_IFDIR
;
1115 inode
->i_op
= sbi
->dir_ops
;
1116 inode
->i_fop
= &fat_dir_operations
;
1118 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
1119 if (sbi
->fat_bits
== 32)
1120 MSDOS_I(inode
)->i_start
|= (CF_LE_W(de
->starthi
) << 16);
1122 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
1123 error
= fat_calc_dir_size(inode
);
1126 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
1128 inode
->i_nlink
= fat_subdirs(inode
);
1129 } else { /* not a directory */
1130 inode
->i_generation
|= 1;
1131 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,
1132 ((sbi
->options
.showexec
&&
1134 ? S_IRUGO
|S_IWUGO
: S_IRWXUGO
)
1135 & ~sbi
->options
.fs_fmask
) | S_IFREG
;
1136 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
1137 if (sbi
->fat_bits
== 32)
1138 MSDOS_I(inode
)->i_start
|= (CF_LE_W(de
->starthi
) << 16);
1140 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
1141 inode
->i_size
= CF_LE_L(de
->size
);
1142 inode
->i_op
= &fat_file_inode_operations
;
1143 inode
->i_fop
= &fat_file_operations
;
1144 inode
->i_mapping
->a_ops
= &fat_aops
;
1145 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
1147 if(de
->attr
& ATTR_SYS
)
1148 if (sbi
->options
.sys_immutable
)
1149 inode
->i_flags
|= S_IMMUTABLE
;
1150 MSDOS_I(inode
)->i_attrs
= de
->attr
& ATTR_UNUSED
;
1151 /* this is as close to the truth as we can get ... */
1152 inode
->i_blksize
= 1 << sbi
->cluster_bits
;
1153 inode
->i_blocks
= ((inode
->i_size
+ inode
->i_blksize
- 1)
1154 & ~((loff_t
)inode
->i_blksize
- 1)) >> 9;
1155 inode
->i_mtime
.tv_sec
= inode
->i_atime
.tv_sec
=
1156 date_dos2unix(CF_LE_W(de
->time
),CF_LE_W(de
->date
));
1157 inode
->i_mtime
.tv_nsec
= inode
->i_atime
.tv_nsec
= 0;
1158 inode
->i_ctime
.tv_sec
=
1159 MSDOS_SB(sb
)->options
.isvfat
1160 ? date_dos2unix(CF_LE_W(de
->ctime
),CF_LE_W(de
->cdate
))
1161 : inode
->i_mtime
.tv_sec
;
1162 inode
->i_ctime
.tv_nsec
= de
->ctime_ms
* 1000000;
1163 MSDOS_I(inode
)->i_ctime_ms
= de
->ctime_ms
;
1168 void fat_write_inode(struct inode
*inode
, int wait
)
1170 struct super_block
*sb
= inode
->i_sb
;
1171 struct buffer_head
*bh
;
1172 struct msdos_dir_entry
*raw_entry
;
1176 i_pos
= MSDOS_I(inode
)->i_pos
;
1177 if (inode
->i_ino
== MSDOS_ROOT_INO
|| !i_pos
) {
1181 if (!(bh
= sb_bread(sb
, i_pos
>> MSDOS_SB(sb
)->dir_per_block_bits
))) {
1182 fat_fs_panic(sb
, "unable to read i-node block (i_pos %llu)",
1187 spin_lock(&fat_inode_lock
);
1188 if (i_pos
!= MSDOS_I(inode
)->i_pos
) {
1189 spin_unlock(&fat_inode_lock
);
1195 raw_entry
= &((struct msdos_dir_entry
*) (bh
->b_data
))
1196 [i_pos
& (MSDOS_SB(sb
)->dir_per_block
- 1)];
1197 if (S_ISDIR(inode
->i_mode
)) {
1198 raw_entry
->attr
= ATTR_DIR
;
1199 raw_entry
->size
= 0;
1202 raw_entry
->attr
= ATTR_NONE
;
1203 raw_entry
->size
= CT_LE_L(inode
->i_size
);
1205 raw_entry
->attr
|= MSDOS_MKATTR(inode
->i_mode
) |
1206 MSDOS_I(inode
)->i_attrs
;
1207 raw_entry
->start
= CT_LE_W(MSDOS_I(inode
)->i_logstart
);
1208 raw_entry
->starthi
= CT_LE_W(MSDOS_I(inode
)->i_logstart
>> 16);
1209 fat_date_unix2dos(inode
->i_mtime
.tv_sec
,&raw_entry
->time
,&raw_entry
->date
);
1210 raw_entry
->time
= CT_LE_W(raw_entry
->time
);
1211 raw_entry
->date
= CT_LE_W(raw_entry
->date
);
1212 if (MSDOS_SB(sb
)->options
.isvfat
) {
1213 fat_date_unix2dos(inode
->i_ctime
.tv_sec
,&raw_entry
->ctime
,&raw_entry
->cdate
);
1214 raw_entry
->ctime_ms
= MSDOS_I(inode
)->i_ctime_ms
; /* use i_ctime.tv_nsec? */
1215 raw_entry
->ctime
= CT_LE_W(raw_entry
->ctime
);
1216 raw_entry
->cdate
= CT_LE_W(raw_entry
->cdate
);
1218 spin_unlock(&fat_inode_lock
);
1219 mark_buffer_dirty(bh
);
1225 int fat_notify_change(struct dentry
* dentry
, struct iattr
* attr
)
1227 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
1228 struct inode
*inode
= dentry
->d_inode
;
1229 int mask
, error
= 0;
1233 /* FAT cannot truncate to a longer file */
1234 if (attr
->ia_valid
& ATTR_SIZE
) {
1235 if (attr
->ia_size
> inode
->i_size
) {
1241 error
= inode_change_ok(inode
, attr
);
1243 if (sbi
->options
.quiet
)
1248 if (((attr
->ia_valid
& ATTR_UID
) &&
1249 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
1250 ((attr
->ia_valid
& ATTR_GID
) &&
1251 (attr
->ia_gid
!= sbi
->options
.fs_gid
)) ||
1252 ((attr
->ia_valid
& ATTR_MODE
) &&
1253 (attr
->ia_mode
& ~MSDOS_VALID_MODE
)))
1257 if (sbi
->options
.quiet
)
1261 error
= inode_setattr(inode
, attr
);
1265 if (S_ISDIR(inode
->i_mode
))
1266 mask
= sbi
->options
.fs_dmask
;
1268 mask
= sbi
->options
.fs_fmask
;
1269 inode
->i_mode
&= S_IFMT
| (S_IRWXUGO
& ~mask
);
1274 MODULE_LICENSE("GPL");