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 <linux/parser.h>
24 #include <asm/unaligned.h>
27 * New FAT inode stuff. We do the following:
28 * a) i_ino is constant and has nothing with on-disk location.
29 * b) FAT manages its own cache of directory entries.
30 * c) *This* cache is indexed by on-disk location.
31 * d) inode has an associated directory entry, all right, but
33 * e) currently entries are stored within struct inode. That should
35 * f) we deal with races in the following way:
36 * 1. readdir() and lookup() do FAT-dir-cache lookup.
37 * 2. rename() unhashes the F-d-c entry and rehashes it in
39 * 3. unlink() and rmdir() unhash F-d-c entry.
40 * 4. fat_write_inode() checks whether the thing is unhashed.
41 * If it is we silently return. If it isn't we do bread(),
42 * check if the location is still valid and retry if it
43 * isn't. Otherwise we do changes.
44 * 5. Spinlock is used to protect hash/unhash/location check/lookup
45 * 6. fat_clear_inode() unhashes the F-d-c entry.
46 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
47 * and consider negative result as cache miss.
50 #define FAT_HASH_BITS 8
51 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
52 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
53 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
54 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
56 void fat_hash_init(void)
59 for(i = 0; i < FAT_HASH_SIZE; i++) {
60 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
64 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
66 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
67 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
68 return tmp & FAT_HASH_MASK;
71 void fat_attach(struct inode *inode, loff_t i_pos)
73 spin_lock(&fat_inode_lock);
74 MSDOS_I(inode)->i_pos = i_pos;
75 list_add(&MSDOS_I(inode)->i_fat_hash,
76 fat_inode_hashtable + fat_hash(inode->i_sb, i_pos));
77 spin_unlock(&fat_inode_lock);
80 void fat_detach(struct inode *inode)
82 spin_lock(&fat_inode_lock);
83 MSDOS_I(inode)->i_pos = 0;
84 list_del_init(&MSDOS_I(inode)->i_fat_hash);
85 spin_unlock(&fat_inode_lock);
88 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
90 struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
91 struct list_head *walk;
92 struct msdos_inode_info *i;
93 struct inode *inode = NULL;
95 spin_lock(&fat_inode_lock);
96 list_for_each(walk, p) {
97 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
98 if (i->vfs_inode.i_sb != sb)
100 if (i->i_pos != i_pos)
102 inode = igrab(&i->vfs_inode);
106 spin_unlock(&fat_inode_lock);
110 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
112 struct inode *fat_build_inode(struct super_block *sb,
113 struct msdos_dir_entry *de, loff_t i_pos, int *res)
117 inode = fat_iget(sb, i_pos);
120 inode = new_inode(sb);
124 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
125 inode->i_version = 1;
126 *res = fat_fill_inode(inode, de);
132 fat_attach(inode, i_pos);
133 insert_inode_hash(inode);
138 void fat_delete_inode(struct inode *inode)
140 if (!is_bad_inode(inode)) {
147 void fat_clear_inode(struct inode *inode)
149 if (is_bad_inode(inode))
152 spin_lock(&fat_inode_lock);
153 fat_cache_inval_inode(inode);
154 list_del_init(&MSDOS_I(inode)->i_fat_hash);
155 spin_unlock(&fat_inode_lock);
159 void fat_put_super(struct super_block *sb)
161 struct msdos_sb_info *sbi = MSDOS_SB(sb);
163 if (!(sb->s_flags & MS_RDONLY))
164 fat_clusters_flush(sb);
167 unload_nls(sbi->nls_disk);
168 sbi->nls_disk = NULL;
169 sbi->options.codepage = 0;
172 unload_nls(sbi->nls_io);
176 * Note: the iocharset option might have been specified
177 * without enabling nls_io, so check for it here.
179 if (sbi->options.iocharset) {
180 kfree(sbi->options.iocharset);
181 sbi->options.iocharset = NULL;
183 sb->s_fs_info = NULL;
187 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
189 struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
190 struct fat_mount_options *opts = &sbi->options;
191 int isvfat = opts->isvfat;
193 if (opts->fs_uid != 0)
194 seq_printf(m, ",uid=%u", opts->fs_uid);
195 if (opts->fs_gid != 0)
196 seq_printf(m, ",gid=%u", opts->fs_gid);
197 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
198 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
200 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
203 && strcmp(sbi->nls_io->charset, CONFIG_NLS_DEFAULT))
204 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
206 switch (opts->shortname) {
207 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
208 seq_puts(m, ",shortname=win95");
210 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
211 seq_puts(m, ",shortname=winnt");
213 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
214 seq_puts(m, ",shortname=mixed");
216 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
217 /* seq_puts(m, ",shortname=lower"); */
220 seq_puts(m, ",shortname=unknown");
224 if (opts->name_check != 'n')
225 seq_printf(m, ",check=%c", opts->name_check);
227 seq_puts(m, ",quiet");
229 seq_puts(m, ",showexec");
230 if (opts->sys_immutable)
231 seq_puts(m, ",sys_immutable");
234 seq_puts(m, ",dotsOK=yes");
236 seq_puts(m, ",nocase");
239 seq_puts(m, ",utf8");
240 if (opts->unicode_xlate)
241 seq_puts(m, ",uni_xlate");
243 seq_puts(m, ",nonumtail");
250 Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
251 Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase,
252 Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable,
253 Opt_dots, Opt_nodots,
254 Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
255 Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
256 Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
257 Opt_obsolate, Opt_err,
260 static match_table_t fat_tokens = {
261 {Opt_check_r, "check=relaxed"},
262 {Opt_check_s, "check=strict"},
263 {Opt_check_n, "check=normal"},
264 {Opt_check_r, "check=r"},
265 {Opt_check_s, "check=s"},
266 {Opt_check_n, "check=n"},
269 {Opt_umask, "umask=%o"},
270 {Opt_dmask, "dmask=%o"},
271 {Opt_fmask, "fmask=%o"},
272 {Opt_codepage, "codepage=%u"},
273 {Opt_nocase, "nocase"},
274 {Opt_quiet, "quiet"},
275 {Opt_showexec, "showexec"},
276 {Opt_debug, "debug"},
277 {Opt_immutable, "sys_immutable"},
278 {Opt_obsolate, "conv=binary"},
279 {Opt_obsolate, "conv=text"},
280 {Opt_obsolate, "conv=auto"},
281 {Opt_obsolate, "conv=b"},
282 {Opt_obsolate, "conv=t"},
283 {Opt_obsolate, "conv=a"},
284 {Opt_obsolate, "fat=%u"},
285 {Opt_obsolate, "blocksize=%u"},
286 {Opt_obsolate, "cvf_format=%20s"},
287 {Opt_obsolate, "cvf_options=%100s"},
288 {Opt_obsolate, "posix"},
291 static match_table_t msdos_tokens = {
292 {Opt_nodots, "nodots"},
293 {Opt_nodots, "dotsOK=no"},
295 {Opt_dots, "dotsOK=yes"},
298 static match_table_t vfat_tokens = {
299 {Opt_charset, "iocharset=%s"},
300 {Opt_shortname_lower, "shortname=lower"},
301 {Opt_shortname_win95, "shortname=win95"},
302 {Opt_shortname_winnt, "shortname=winnt"},
303 {Opt_shortname_mixed, "shortname=mixed"},
304 {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
305 {Opt_utf8_no, "utf8=no"},
306 {Opt_utf8_no, "utf8=false"},
307 {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
308 {Opt_utf8_yes, "utf8=yes"},
309 {Opt_utf8_yes, "utf8=true"},
310 {Opt_utf8_yes, "utf8"},
311 {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
312 {Opt_uni_xl_no, "uni_xlate=no"},
313 {Opt_uni_xl_no, "uni_xlate=false"},
314 {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
315 {Opt_uni_xl_yes, "uni_xlate=yes"},
316 {Opt_uni_xl_yes, "uni_xlate=true"},
317 {Opt_uni_xl_yes, "uni_xlate"},
318 {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
319 {Opt_nonumtail_no, "nonumtail=no"},
320 {Opt_nonumtail_no, "nonumtail=false"},
321 {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
322 {Opt_nonumtail_yes, "nonumtail=yes"},
323 {Opt_nonumtail_yes, "nonumtail=true"},
324 {Opt_nonumtail_yes, "nonumtail"},
328 static int parse_options(char *options, int is_vfat, int *debug,
329 struct fat_mount_options *opts)
332 substring_t args[MAX_OPT_ARGS];
335 opts->isvfat = is_vfat;
337 opts->fs_uid = current->uid;
338 opts->fs_gid = current->gid;
339 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
341 opts->iocharset = NULL;
343 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
346 opts->name_check = 'n';
347 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
348 opts->utf8 = opts->unicode_xlate = 0;
356 while ((p = strsep(&options, ",")) != NULL) {
361 token = match_token(p, fat_tokens, args);
362 if (token == Opt_err) {
364 token = match_token(p, vfat_tokens, args);
366 token = match_token(p, msdos_tokens, args);
370 opts->name_check = 's';
373 opts->name_check = 'r';
376 opts->name_check = 'n';
382 /* for backward compatibility */
383 opts->shortname = VFAT_SFN_DISPLAY_WIN95
384 | VFAT_SFN_CREATE_WIN95;
397 opts->sys_immutable = 1;
400 if (match_int(&args[0], &option))
402 opts->fs_uid = option;
405 if (match_int(&args[0], &option))
407 opts->fs_gid = option;
410 if (match_octal(&args[0], &option))
412 opts->fs_fmask = opts->fs_dmask = option;
415 if (match_octal(&args[0], &option))
417 opts->fs_dmask = option;
420 if (match_octal(&args[0], &option))
422 opts->fs_fmask = option;
425 if (match_int(&args[0], &option))
427 opts->codepage = option;
440 kfree(opts->iocharset);
441 opts->iocharset = match_strdup(&args[0]);
442 if (!opts->iocharset)
445 case Opt_shortname_lower:
446 opts->shortname = VFAT_SFN_DISPLAY_LOWER
447 | VFAT_SFN_CREATE_WIN95;
449 case Opt_shortname_win95:
450 opts->shortname = VFAT_SFN_DISPLAY_WIN95
451 | VFAT_SFN_CREATE_WIN95;
453 case Opt_shortname_winnt:
454 opts->shortname = VFAT_SFN_DISPLAY_WINNT
455 | VFAT_SFN_CREATE_WINNT;
457 case Opt_shortname_mixed:
458 opts->shortname = VFAT_SFN_DISPLAY_WINNT
459 | VFAT_SFN_CREATE_WIN95;
461 case Opt_utf8_no: /* 0 or no or false */
464 case Opt_utf8_yes: /* empty or 1 or yes or true */
467 case Opt_uni_xl_no: /* 0 or no or false */
468 opts->unicode_xlate = 0;
470 case Opt_uni_xl_yes: /* empty or 1 or yes or true */
471 opts->unicode_xlate = 1;
473 case Opt_nonumtail_no: /* 0 or no or false */
474 opts->numtail = 1; /* negated option */
476 case Opt_nonumtail_yes: /* empty or 1 or yes or true */
477 opts->numtail = 0; /* negated option */
480 /* obsolete mount options */
482 printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
483 "not supported now\n", p);
487 printk(KERN_ERR "FAT: Unrecognized mount option \"%s\" "
488 "or missing value\n", p);
493 if (opts->unicode_xlate)
499 static int fat_calc_dir_size(struct inode *inode)
501 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
502 int ret, fclus, dclus;
505 if (MSDOS_I(inode)->i_start == 0)
508 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
511 inode->i_size = (fclus + 1) << sbi->cluster_bits;
516 static int fat_read_root(struct inode *inode)
518 struct super_block *sb = inode->i_sb;
519 struct msdos_sb_info *sbi = MSDOS_SB(sb);
522 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
523 MSDOS_I(inode)->i_pos = 0;
524 inode->i_uid = sbi->options.fs_uid;
525 inode->i_gid = sbi->options.fs_gid;
527 inode->i_generation = 0;
528 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
529 inode->i_op = sbi->dir_ops;
530 inode->i_fop = &fat_dir_operations;
531 if (sbi->fat_bits == 32) {
532 MSDOS_I(inode)->i_start = sbi->root_cluster;
533 error = fat_calc_dir_size(inode);
537 MSDOS_I(inode)->i_start = 0;
538 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
540 inode->i_blksize = sbi->cluster_size;
541 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
542 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
543 MSDOS_I(inode)->i_logstart = 0;
544 MSDOS_I(inode)->mmu_private = inode->i_size;
546 MSDOS_I(inode)->i_attrs = 0;
547 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
548 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
549 MSDOS_I(inode)->i_ctime_ms = 0;
550 inode->i_nlink = fat_subdirs(inode)+2;
556 * a FAT file handle with fhtype 3 is
557 * 0/ i_ino - for fast, reliable lookup if still in the cache
558 * 1/ i_generation - to see if i_ino is still valid
559 * bit 0 == 0 iff directory
560 * 2/ i_pos(8-39) - if ino has changed, but still in cache
561 * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
562 * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
564 * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
565 * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
566 * of i_logstart is used to store the directory entry offset.
569 struct dentry *fat_decode_fh(struct super_block *sb, __u32 *fh,
571 int (*acceptable)(void *context, struct dentry *de),
576 return ERR_PTR(-ESTALE);
578 return ERR_PTR(-ESTALE);
580 return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
583 struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
585 struct inode *inode = NULL;
586 struct dentry *result;
589 inode = iget(sb, fh[0]);
590 if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
597 int i_logstart = fh[3] & 0x0fffffff;
599 i_pos = (loff_t)fh[2] << 8;
600 i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
602 /* try 2 - see if i_pos is in F-d-c
603 * require i_logstart to be the same
604 * Will fail if you truncate and then re-write
607 inode = fat_iget(sb, i_pos);
608 if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
614 /* For now, do nothing
615 * What we could do is:
616 * follow the file starting at fh[4], and record
617 * the ".." entry, and the name of the fh[2] entry.
618 * The follow the ".." file finding the next step up.
619 * This way we build a path to the root of
620 * the tree. If this works, we lookup the path and so
621 * get this inode into the cache.
622 * Finally try the fat_iget lookup again
623 * If that fails, then weare totally out of luck
624 * But all that is for another day
628 return ERR_PTR(-ESTALE);
631 /* now to find a dentry.
632 * If possible, get a well-connected one
634 result = d_alloc_anon(inode);
635 if (result == NULL) {
637 return ERR_PTR(-ENOMEM);
639 result->d_op = sb->s_root->d_op;
643 int fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
646 struct inode *inode = de->d_inode;
647 u32 ipos_h, ipos_m, ipos_l;
650 return 255; /* no room */
652 ipos_h = MSDOS_I(inode)->i_pos >> 8;
653 ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
654 ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
656 fh[0] = inode->i_ino;
657 fh[1] = inode->i_generation;
659 fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
660 spin_lock(&de->d_lock);
661 fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
662 spin_unlock(&de->d_lock);
666 struct dentry *fat_get_parent(struct dentry *child)
668 struct buffer_head *bh=NULL;
669 struct msdos_dir_entry *de = NULL;
670 struct dentry *parent = NULL;
676 res = fat_scan(child->d_inode, MSDOS_DOTDOT, &bh, &de, &i_pos);
680 inode = fat_build_inode(child->d_sb, de, i_pos, &res);
686 parent = d_alloc_anon(inode);
703 static kmem_cache_t *fat_inode_cachep;
705 static struct inode *fat_alloc_inode(struct super_block *sb)
707 struct msdos_inode_info *ei;
708 ei = (struct msdos_inode_info *)kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
711 return &ei->vfs_inode;
714 static void fat_destroy_inode(struct inode *inode)
716 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
719 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
721 struct msdos_inode_info *ei = (struct msdos_inode_info *) foo;
723 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
724 SLAB_CTOR_CONSTRUCTOR) {
725 INIT_LIST_HEAD(&ei->i_fat_hash);
726 inode_init_once(&ei->vfs_inode);
730 int __init fat_init_inodecache(void)
732 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
733 sizeof(struct msdos_inode_info),
734 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
736 if (fat_inode_cachep == NULL)
741 void __exit fat_destroy_inodecache(void)
743 if (kmem_cache_destroy(fat_inode_cachep))
744 printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
747 static int fat_remount(struct super_block *sb, int *flags, char *data)
749 *flags |= MS_NODIRATIME;
753 static struct super_operations fat_sops = {
754 .alloc_inode = fat_alloc_inode,
755 .destroy_inode = fat_destroy_inode,
756 .write_inode = fat_write_inode,
757 .delete_inode = fat_delete_inode,
758 .put_super = fat_put_super,
759 .statfs = fat_statfs,
760 .clear_inode = fat_clear_inode,
761 .remount_fs = fat_remount,
763 .read_inode = make_bad_inode,
765 .show_options = fat_show_options,
768 static struct export_operations fat_export_ops = {
769 .decode_fh = fat_decode_fh,
770 .encode_fh = fat_encode_fh,
771 .get_dentry = fat_get_dentry,
772 .get_parent = fat_get_parent,
776 * Read the super block of an MS-DOS FS.
778 int fat_fill_super(struct super_block *sb, void *data, int silent,
779 struct inode_operations *fs_dir_inode_ops, int isvfat)
781 struct inode *root_inode = NULL;
782 struct buffer_head *bh;
783 struct fat_boot_sector *b;
784 struct msdos_sb_info *sbi;
785 u16 logical_sector_size;
786 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
787 int debug, cp, first;
792 sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
796 memset(sbi, 0, sizeof(struct msdos_sb_info));
798 sb->s_flags |= MS_NODIRATIME;
799 sb->s_magic = MSDOS_SUPER_MAGIC;
800 sb->s_op = &fat_sops;
801 sb->s_export_op = &fat_export_ops;
802 sbi->dir_ops = fs_dir_inode_ops;
805 if (!parse_options(data, isvfat, &debug, &sbi->options))
809 /* set up enough so that it can read an inode */
810 init_MUTEX(&sbi->fat_lock);
813 sb_min_blocksize(sb, 512);
814 bh = sb_bread(sb, 0);
816 printk(KERN_ERR "FAT: unable to read boot sector\n");
820 b = (struct fat_boot_sector *) bh->b_data;
823 printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
829 printk(KERN_ERR "FAT: bogus number of FAT structure\n");
833 if (!b->secs_track) {
835 printk(KERN_ERR "FAT: bogus sectors-per-track value\n");
841 printk(KERN_ERR "FAT: bogus number-of-heads value\n");
846 if (!FAT_VALID_MEDIA(media)) {
848 printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
853 logical_sector_size = CF_LE_W(get_unaligned((u16 *)&b->sector_size));
854 if (!logical_sector_size
855 || (logical_sector_size & (logical_sector_size - 1))
856 || (logical_sector_size < 512)
857 || (PAGE_CACHE_SIZE < logical_sector_size)) {
859 printk(KERN_ERR "FAT: bogus logical sector size %u\n",
860 logical_sector_size);
864 sbi->sec_per_clus = b->sec_per_clus;
865 if (!sbi->sec_per_clus
866 || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
868 printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
874 if (logical_sector_size < sb->s_blocksize) {
875 printk(KERN_ERR "FAT: logical sector size too small for device"
876 " (logical sector size = %u)\n", logical_sector_size);
880 if (logical_sector_size > sb->s_blocksize) {
883 if (!sb_set_blocksize(sb, logical_sector_size)) {
884 printk(KERN_ERR "FAT: unable to set blocksize %u\n",
885 logical_sector_size);
888 bh = sb_bread(sb, 0);
890 printk(KERN_ERR "FAT: unable to read boot sector"
891 " (logical sector size = %lu)\n",
895 b = (struct fat_boot_sector *) bh->b_data;
898 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
899 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
901 sbi->fat_bits = 0; /* Don't know yet */
902 sbi->fat_start = CF_LE_W(b->reserved);
903 sbi->fat_length = CF_LE_W(b->fat_length);
904 sbi->root_cluster = 0;
905 sbi->free_clusters = -1; /* Don't know yet */
908 if (!sbi->fat_length && b->fat32_length) {
909 struct fat_boot_fsinfo *fsinfo;
910 struct buffer_head *fsinfo_bh;
914 sbi->fat_length = CF_LE_L(b->fat32_length);
915 sbi->root_cluster = CF_LE_L(b->root_cluster);
917 sb->s_maxbytes = 0xffffffff;
919 /* MC - if info_sector is 0, don't multiply by 0 */
920 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
921 if (sbi->fsinfo_sector == 0)
922 sbi->fsinfo_sector = 1;
924 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
925 if (fsinfo_bh == NULL) {
926 printk(KERN_ERR "FAT: bread failed, FSINFO block"
927 " (sector = %lu)\n", sbi->fsinfo_sector);
932 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
933 if (!IS_FSINFO(fsinfo)) {
935 "FAT: Did not find valid FSINFO signature.\n"
936 " Found signature1 0x%08x signature2 0x%08x"
938 CF_LE_L(fsinfo->signature1),
939 CF_LE_L(fsinfo->signature2),
942 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
943 sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
949 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
950 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
952 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
953 sbi->dir_entries = CF_LE_W(get_unaligned((u16 *)&b->dir_entries));
954 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
956 printk(KERN_ERR "FAT: bogus directroy-entries per block"
957 " (%u)\n", sbi->dir_entries);
962 rootdir_sectors = sbi->dir_entries
963 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
964 sbi->data_start = sbi->dir_start + rootdir_sectors;
965 total_sectors = CF_LE_W(get_unaligned((u16 *)&b->sectors));
966 if (total_sectors == 0)
967 total_sectors = CF_LE_L(b->total_sect);
969 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
971 if (sbi->fat_bits != 32)
972 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
974 /* check that FAT table does not overflow */
975 fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
976 total_clusters = min(total_clusters, fat_clusters - 2);
977 if (total_clusters > MAX_FAT(sb)) {
979 printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
985 sbi->clusters = total_clusters;
989 /* validity check of FAT */
990 first = __fat_access(sb, 0, -1);
995 if (FAT_FIRST_ENT(sb, media) == first) {
996 /* all is as it should be */
997 } else if (media == 0xf8 && FAT_FIRST_ENT(sb, 0xfe) == first) {
998 /* bad, reported on pc9800 */
999 } else if (media == 0xf0 && FAT_FIRST_ENT(sb, 0xf8) == first) {
1000 /* bad, reported with a MO disk on win95/me */
1001 } else if (first == 0) {
1002 /* bad, reported with a SmartMedia card */
1005 printk(KERN_ERR "FAT: invalid first entry of FAT "
1007 FAT_FIRST_ENT(sb, media), first);
1012 cp = sbi->options.codepage ? sbi->options.codepage : 437;
1013 sprintf(buf, "cp%d", cp);
1014 sbi->nls_disk = load_nls(buf);
1015 if (!sbi->nls_disk) {
1016 /* Fail only if explicit charset specified */
1017 if (sbi->options.codepage != 0) {
1018 printk(KERN_ERR "FAT: codepage %s not found\n", buf);
1021 sbi->options.codepage = 0; /* already 0?? */
1022 sbi->nls_disk = load_nls_default();
1025 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1026 if (sbi->options.isvfat) {
1027 if (sbi->options.iocharset != NULL) {
1028 sbi->nls_io = load_nls(sbi->options.iocharset);
1031 "FAT: IO charset %s not found\n",
1032 sbi->options.iocharset);
1036 sbi->nls_io = load_nls_default();
1040 root_inode = new_inode(sb);
1043 root_inode->i_ino = MSDOS_ROOT_INO;
1044 root_inode->i_version = 1;
1045 error = fat_read_root(root_inode);
1049 insert_inode_hash(root_inode);
1050 sb->s_root = d_alloc_root(root_inode);
1052 printk(KERN_ERR "FAT: get root inode failed\n");
1061 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
1062 " on dev %s.\n", sb->s_id);
1068 unload_nls(sbi->nls_io);
1070 unload_nls(sbi->nls_disk);
1071 if (sbi->options.iocharset)
1072 kfree(sbi->options.iocharset);
1073 sb->s_fs_info = NULL;
1078 int fat_statfs(struct super_block *sb, struct kstatfs *buf)
1082 if (MSDOS_SB(sb)->free_clusters != -1)
1083 free = MSDOS_SB(sb)->free_clusters;
1086 if (MSDOS_SB(sb)->free_clusters != -1)
1087 free = MSDOS_SB(sb)->free_clusters;
1090 for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++) {
1091 ret = fat_access(sb, nr, -1);
1095 } else if (ret == FAT_ENT_FREE)
1098 MSDOS_SB(sb)->free_clusters = free;
1103 buf->f_type = sb->s_magic;
1104 buf->f_bsize = MSDOS_SB(sb)->cluster_size;
1105 buf->f_blocks = MSDOS_SB(sb)->clusters;
1106 buf->f_bfree = free;
1107 buf->f_bavail = free;
1108 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
1113 static int is_exec(unsigned char *extension)
1115 unsigned char *exe_extensions = "EXECOMBAT", *walk;
1117 for (walk = exe_extensions; *walk; walk += 3)
1118 if (!strncmp(extension, walk, 3))
1123 static int fat_writepage(struct page *page, struct writeback_control *wbc)
1125 return block_write_full_page(page,fat_get_block, wbc);
1127 static int fat_readpage(struct file *file, struct page *page)
1129 return block_read_full_page(page,fat_get_block);
1133 fat_prepare_write(struct file *file, struct page *page,
1134 unsigned from, unsigned to)
1137 return cont_prepare_write(page,from,to,fat_get_block,
1138 &MSDOS_I(page->mapping->host)->mmu_private);
1142 fat_commit_write(struct file *file, struct page *page,
1143 unsigned from, unsigned to)
1146 return generic_commit_write(file, page, from, to);
1149 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
1151 return generic_block_bmap(mapping,block,fat_get_block);
1153 static struct address_space_operations fat_aops = {
1154 .readpage = fat_readpage,
1155 .writepage = fat_writepage,
1156 .sync_page = block_sync_page,
1157 .prepare_write = fat_prepare_write,
1158 .commit_write = fat_commit_write,
1162 /* doesn't deal with root inode */
1163 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
1165 struct super_block *sb = inode->i_sb;
1166 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1169 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
1170 MSDOS_I(inode)->i_pos = 0;
1171 inode->i_uid = sbi->options.fs_uid;
1172 inode->i_gid = sbi->options.fs_gid;
1174 inode->i_generation = get_seconds();
1176 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
1177 inode->i_generation &= ~1;
1178 inode->i_mode = MSDOS_MKMODE(de->attr,
1179 S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1180 inode->i_op = sbi->dir_ops;
1181 inode->i_fop = &fat_dir_operations;
1183 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1184 if (sbi->fat_bits == 32)
1185 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1187 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1188 error = fat_calc_dir_size(inode);
1191 MSDOS_I(inode)->mmu_private = inode->i_size;
1193 inode->i_nlink = fat_subdirs(inode);
1194 } else { /* not a directory */
1195 inode->i_generation |= 1;
1196 inode->i_mode = MSDOS_MKMODE(de->attr,
1197 ((sbi->options.showexec &&
1199 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
1200 & ~sbi->options.fs_fmask) | S_IFREG;
1201 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1202 if (sbi->fat_bits == 32)
1203 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1205 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1206 inode->i_size = CF_LE_L(de->size);
1207 inode->i_op = &fat_file_inode_operations;
1208 inode->i_fop = &fat_file_operations;
1209 inode->i_mapping->a_ops = &fat_aops;
1210 MSDOS_I(inode)->mmu_private = inode->i_size;
1212 if(de->attr & ATTR_SYS)
1213 if (sbi->options.sys_immutable)
1214 inode->i_flags |= S_IMMUTABLE;
1215 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
1216 /* this is as close to the truth as we can get ... */
1217 inode->i_blksize = sbi->cluster_size;
1218 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1219 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1220 inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
1221 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
1222 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1223 inode->i_ctime.tv_sec =
1224 MSDOS_SB(sb)->options.isvfat
1225 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
1226 : inode->i_mtime.tv_sec;
1227 inode->i_ctime.tv_nsec = de->ctime_ms * 1000000;
1228 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
1233 void fat_write_inode(struct inode *inode, int wait)
1235 struct super_block *sb = inode->i_sb;
1236 struct buffer_head *bh;
1237 struct msdos_dir_entry *raw_entry;
1241 i_pos = MSDOS_I(inode)->i_pos;
1242 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
1246 if (!(bh = sb_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
1247 printk(KERN_ERR "FAT: unable to read inode block "
1248 "for updating (i_pos %lld)", i_pos);
1252 spin_lock(&fat_inode_lock);
1253 if (i_pos != MSDOS_I(inode)->i_pos) {
1254 spin_unlock(&fat_inode_lock);
1260 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1261 [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1262 if (S_ISDIR(inode->i_mode)) {
1263 raw_entry->attr = ATTR_DIR;
1264 raw_entry->size = 0;
1267 raw_entry->attr = ATTR_NONE;
1268 raw_entry->size = CT_LE_L(inode->i_size);
1270 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1271 MSDOS_I(inode)->i_attrs;
1272 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1273 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1274 fat_date_unix2dos(inode->i_mtime.tv_sec,&raw_entry->time,&raw_entry->date);
1275 raw_entry->time = CT_LE_W(raw_entry->time);
1276 raw_entry->date = CT_LE_W(raw_entry->date);
1277 if (MSDOS_SB(sb)->options.isvfat) {
1278 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
1279 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; /* use i_ctime.tv_nsec? */
1280 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1281 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1283 spin_unlock(&fat_inode_lock);
1284 mark_buffer_dirty(bh);
1290 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1292 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
1293 struct inode *inode = dentry->d_inode;
1294 int mask, error = 0;
1298 /* FAT cannot truncate to a longer file */
1299 if (attr->ia_valid & ATTR_SIZE) {
1300 if (attr->ia_size > inode->i_size) {
1306 error = inode_change_ok(inode, attr);
1308 if (sbi->options.quiet)
1313 if (((attr->ia_valid & ATTR_UID) &&
1314 (attr->ia_uid != sbi->options.fs_uid)) ||
1315 ((attr->ia_valid & ATTR_GID) &&
1316 (attr->ia_gid != sbi->options.fs_gid)) ||
1317 ((attr->ia_valid & ATTR_MODE) &&
1318 (attr->ia_mode & ~MSDOS_VALID_MODE)))
1322 if (sbi->options.quiet)
1326 error = inode_setattr(inode, attr);
1330 if (S_ISDIR(inode->i_mode))
1331 mask = sbi->options.fs_dmask;
1333 mask = sbi->options.fs_fmask;
1334 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
1339 MODULE_LICENSE("GPL");