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/msdos_fs.h>
15 #include <linux/nls.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/bitops.h>
21 #include <linux/major.h>
22 #include <linux/blkdev.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26 #include <linux/fat_cvf.h>
27 #include <linux/slab.h>
28 #include <linux/smp_lock.h>
30 #include <asm/uaccess.h>
31 #include <asm/unaligned.h>
33 extern struct cvf_format default_cvf;
35 /* #define FAT_PARANOIA 1 */
38 # define PRINTK(x) printk x
42 #if (DEBUG_LEVEL >= 1)
43 # define PRINTK1(x) printk x
49 * New FAT inode stuff. We do the following:
50 * a) i_ino is constant and has nothing with on-disk location.
51 * b) FAT manages its own cache of directory entries.
52 * c) *This* cache is indexed by on-disk location.
53 * d) inode has an associated directory entry, all right, but
55 * e) currently entries are stored within struct inode. That should
57 * f) we deal with races in the following way:
58 * 1. readdir() and lookup() do FAT-dir-cache lookup.
59 * 2. rename() unhashes the F-d-c entry and rehashes it in
61 * 3. unlink() and rmdir() unhash F-d-c entry.
62 * 4. fat_write_inode() checks whether the thing is unhashed.
63 * If it is we silently return. If it isn't we do bread(),
64 * check if the location is still valid and retry if it
65 * isn't. Otherwise we do changes.
66 * 5. Spinlock is used to protect hash/unhash/location check/lookup
67 * 6. fat_clear_inode() unhashes the F-d-c entry.
68 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
69 * and consider negative result as cache miss.
72 #define FAT_HASH_BITS 8
73 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
74 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
75 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
76 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
78 void fat_hash_init(void)
81 for(i = 0; i < FAT_HASH_SIZE; i++) {
82 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
86 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
88 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
89 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
90 return tmp & FAT_HASH_MASK;
93 void fat_attach(struct inode *inode, loff_t i_pos)
95 spin_lock(&fat_inode_lock);
96 MSDOS_I(inode)->i_pos = i_pos;
97 list_add(&MSDOS_I(inode)->i_fat_hash,
98 fat_inode_hashtable + fat_hash(inode->i_sb, i_pos));
99 spin_unlock(&fat_inode_lock);
102 void fat_detach(struct inode *inode)
104 spin_lock(&fat_inode_lock);
105 MSDOS_I(inode)->i_pos = 0;
106 list_del(&MSDOS_I(inode)->i_fat_hash);
107 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
108 spin_unlock(&fat_inode_lock);
111 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
113 struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
114 struct list_head *walk;
115 struct msdos_inode_info *i;
116 struct inode *inode = NULL;
118 spin_lock(&fat_inode_lock);
119 list_for_each(walk, p) {
120 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
121 if (i->i_fat_inode->i_sb != sb)
123 if (i->i_pos != i_pos)
125 inode = igrab(i->i_fat_inode);
129 spin_unlock(&fat_inode_lock);
133 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
135 struct inode *fat_build_inode(struct super_block *sb,
136 struct msdos_dir_entry *de, loff_t i_pos, int *res)
140 inode = fat_iget(sb, i_pos);
143 inode = new_inode(sb);
148 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
149 fat_fill_inode(inode, de);
150 fat_attach(inode, i_pos);
151 insert_inode_hash(inode);
156 void fat_delete_inode(struct inode *inode)
158 if (!is_bad_inode(inode)) {
167 void fat_clear_inode(struct inode *inode)
169 if (is_bad_inode(inode))
172 spin_lock(&fat_inode_lock);
173 fat_cache_inval_inode(inode);
174 list_del(&MSDOS_I(inode)->i_fat_hash);
175 spin_unlock(&fat_inode_lock);
179 void fat_put_super(struct super_block *sb)
181 if (MSDOS_SB(sb)->cvf_format->cvf_version) {
182 dec_cvf_format_use_count_by_version(MSDOS_SB(sb)->cvf_format->cvf_version);
183 MSDOS_SB(sb)->cvf_format->unmount_cvf(sb);
185 if (MSDOS_SB(sb)->fat_bits == 32) {
186 fat_clusters_flush(sb);
188 fat_cache_inval_dev(sb->s_dev);
189 set_blocksize (sb->s_dev,BLOCK_SIZE);
190 if (MSDOS_SB(sb)->nls_disk) {
191 unload_nls(MSDOS_SB(sb)->nls_disk);
192 MSDOS_SB(sb)->nls_disk = NULL;
193 MSDOS_SB(sb)->options.codepage = 0;
195 if (MSDOS_SB(sb)->nls_io) {
196 unload_nls(MSDOS_SB(sb)->nls_io);
197 MSDOS_SB(sb)->nls_io = NULL;
200 * Note: the iocharset option might have been specified
201 * without enabling nls_io, so check for it here.
203 if (MSDOS_SB(sb)->options.iocharset) {
204 kfree(MSDOS_SB(sb)->options.iocharset);
205 MSDOS_SB(sb)->options.iocharset = NULL;
210 static int parse_options(char *options,int *fat, int *debug,
211 struct fat_mount_options *opts,
212 char *cvf_format, char *cvf_options)
214 char *this_char,*value,save,*savep;
218 opts->name_check = 'n';
219 opts->conversion = 'b';
220 opts->fs_uid = current->uid;
221 opts->fs_gid = current->gid;
222 opts->fs_umask = current->fs->umask;
223 opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
228 opts->iocharset = NULL;
235 for (this_char = strtok(options,","); this_char;
236 this_char = strtok(NULL,",")) {
237 if ((value = strchr(this_char,'=')) != NULL) {
242 if (!strcmp(this_char,"check") && value) {
243 if (value[0] && !value[1] && strchr("rns",*value))
244 opts->name_check = *value;
245 else if (!strcmp(value,"relaxed"))
246 opts->name_check = 'r';
247 else if (!strcmp(value,"normal"))
248 opts->name_check = 'n';
249 else if (!strcmp(value,"strict"))
250 opts->name_check = 's';
253 else if (!strcmp(this_char,"conv") && value) {
254 if (value[0] && !value[1] && strchr("bta",*value))
255 opts->conversion = *value;
256 else if (!strcmp(value,"binary"))
257 opts->conversion = 'b';
258 else if (!strcmp(value,"text"))
259 opts->conversion = 't';
260 else if (!strcmp(value,"auto"))
261 opts->conversion = 'a';
264 else if (!strcmp(this_char,"dots")) {
267 else if (!strcmp(this_char,"nocase")) {
270 else if (!strcmp(this_char,"nodots")) {
273 else if (!strcmp(this_char,"showexec")) {
276 else if (!strcmp(this_char,"dotsOK") && value) {
277 if (!strcmp(value,"yes")) opts->dotsOK = 1;
278 else if (!strcmp(value,"no")) opts->dotsOK = 0;
281 else if (!strcmp(this_char,"uid")) {
282 if (!value || !*value) ret = 0;
284 opts->fs_uid = simple_strtoul(value,&value,0);
288 else if (!strcmp(this_char,"gid")) {
289 if (!value || !*value) ret= 0;
291 opts->fs_gid = simple_strtoul(value,&value,0);
295 else if (!strcmp(this_char,"umask")) {
296 if (!value || !*value) ret = 0;
298 opts->fs_umask = simple_strtoul(value,&value,8);
302 else if (!strcmp(this_char,"debug")) {
306 else if (!strcmp(this_char,"fat")) {
307 if (!value || !*value) ret = 0;
309 *fat = simple_strtoul(value,&value,0);
310 if (*value || (*fat != 12 && *fat != 16 &&
315 else if (!strcmp(this_char,"quiet")) {
317 else opts->quiet = 1;
319 else if (!strcmp(this_char,"blocksize")) {
320 printk("FAT: blocksize option is obsolete, "
321 "not supported now\n");
323 else if (!strcmp(this_char,"sys_immutable")) {
325 else opts->sys_immutable = 1;
327 else if (!strcmp(this_char,"codepage") && value) {
328 opts->codepage = simple_strtoul(value,&value,0);
330 else printk ("MSDOS FS: Using codepage %d\n",
333 else if (!strcmp(this_char,"iocharset") && value) {
335 while (*value && *value != ',')
341 if (opts->iocharset != NULL) {
342 kfree(opts->iocharset);
343 opts->iocharset = NULL;
345 buffer = kmalloc(len + 1, GFP_KERNEL);
346 if (buffer != NULL) {
347 opts->iocharset = buffer;
348 memcpy(buffer, p, len);
350 printk("MSDOS FS: IO charset %s\n", buffer);
355 else if (!strcmp(this_char,"cvf_format")) {
358 strncpy(cvf_format,value,20);
360 else if (!strcmp(this_char,"cvf_options")) {
363 strncpy(cvf_options,value,100);
366 if (this_char != options) *(this_char-1) = ',';
367 if (value) *savep = save;
375 static void fat_read_root(struct inode *inode)
377 struct super_block *sb = inode->i_sb;
378 struct msdos_sb_info *sbi = MSDOS_SB(sb);
381 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
382 MSDOS_I(inode)->i_pos = 0;
383 MSDOS_I(inode)->i_fat_inode = inode;
384 inode->i_uid = sbi->options.fs_uid;
385 inode->i_gid = sbi->options.fs_gid;
386 inode->i_version = ++event;
387 inode->i_generation = 0;
388 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_umask) | S_IFDIR;
389 inode->i_op = sbi->dir_ops;
390 inode->i_fop = &fat_dir_operations;
391 if (sbi->fat_bits == 32) {
392 MSDOS_I(inode)->i_start = sbi->root_cluster;
393 if ((nr = MSDOS_I(inode)->i_start) != 0) {
395 inode->i_size += 1 << sbi->cluster_bits;
396 if (!(nr = fat_access(sb, nr, -1))) {
397 printk("Directory %ld: bad FAT\n",
404 MSDOS_I(inode)->i_start = 0;
405 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
407 inode->i_blksize = 1 << sbi->cluster_bits;
408 inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
409 & ~(inode->i_blksize - 1)) >> 9;
410 MSDOS_I(inode)->i_logstart = 0;
411 MSDOS_I(inode)->mmu_private = inode->i_size;
413 MSDOS_I(inode)->i_attrs = 0;
414 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
415 MSDOS_I(inode)->i_ctime_ms = 0;
416 inode->i_nlink = fat_subdirs(inode)+2;
420 * a FAT file handle with fhtype 3 is
421 * 0/ i_ino - for fast, reliable lookup if still in the cache
422 * 1/ i_generation - to see if i_ino is still valid
423 * bit 0 == 0 iff directory
424 * 2/ i_pos(8-39) - if ino has changed, but still in cache
425 * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
426 * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
428 * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
429 * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
430 * of i_logstart is used to store the directory entry offset.
432 struct dentry *fat_fh_to_dentry(struct super_block *sb, __u32 *fh,
433 int len, int fhtype, int parent)
435 struct inode *inode = NULL;
436 struct list_head *lp;
437 struct dentry *result;
440 return ERR_PTR(-ESTALE);
442 return ERR_PTR(-ESTALE);
443 /* We cannot find the parent,
444 It better just *be* there */
446 return ERR_PTR(-ESTALE);
448 inode = iget(sb, fh[0]);
449 if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
456 int i_logstart = fh[3] & 0x0fffffff;
458 i_pos = (loff_t)fh[2] << 8;
459 i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
461 /* try 2 - see if i_pos is in F-d-c
462 * require i_logstart to be the same
463 * Will fail if you truncate and then re-write
466 inode = fat_iget(sb, i_pos);
467 if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
473 /* For now, do nothing
474 * What we could do is:
475 * follow the file starting at fh[4], and record
476 * the ".." entry, and the name of the fh[2] entry.
477 * The follow the ".." file finding the next step up.
478 * This way we build a path to the root of
479 * the tree. If this works, we lookup the path and so
480 * get this inode into the cache.
481 * Finally try the fat_iget lookup again
482 * If that fails, then weare totally out of luck
483 * But all that is for another day
487 return ERR_PTR(-ESTALE);
490 /* now to find a dentry.
491 * If possible, get a well-connected one
493 * Given the way that we found the inode, it *MUST* be
494 * well-connected, but it is easiest to just copy the
497 spin_lock(&dcache_lock);
498 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
499 result = list_entry(lp,struct dentry, d_alias);
500 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
502 result->d_vfs_flags |= DCACHE_REFERENCED;
503 spin_unlock(&dcache_lock);
508 spin_unlock(&dcache_lock);
509 result = d_alloc_root(inode);
510 if (result == NULL) {
512 return ERR_PTR(-ENOMEM);
514 result->d_op = sb->s_root->d_op;
515 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
519 int fat_dentry_to_fh(struct dentry *de, __u32 *fh, int *lenp, int needparent)
522 struct inode *inode = de->d_inode;
523 u32 ipos_h, ipos_m, ipos_l;
526 return 255; /* no room */
528 ipos_h = MSDOS_I(inode)->i_pos >> 8;
529 ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
530 ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
532 fh[0] = inode->i_ino;
533 fh[1] = inode->i_generation;
535 fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
536 fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
540 static struct super_operations fat_sops = {
541 write_inode: fat_write_inode,
542 delete_inode: fat_delete_inode,
543 put_super: fat_put_super,
545 clear_inode: fat_clear_inode,
547 read_inode: make_bad_inode,
548 fh_to_dentry: fat_fh_to_dentry,
549 dentry_to_fh: fat_dentry_to_fh,
553 * Read the super block of an MS-DOS FS.
555 * Note that this may be called from vfat_read_super
556 * with some fields already initialized.
559 fat_read_super(struct super_block *sb, void *data, int silent,
560 struct inode_operations *fs_dir_inode_ops)
562 struct inode *root_inode;
563 struct buffer_head *bh;
564 struct fat_boot_sector *b;
565 struct msdos_sb_info *sbi = MSDOS_SB(sb);
567 int logical_sector_size, hard_blksize, fat_clusters = 0;
568 unsigned int total_sectors, rootdir_sectors;
569 int fat32, debug, error, fat, cp;
570 struct fat_mount_options opts;
574 char cvf_options[101];
576 cvf_format[0] = '\0';
577 cvf_options[0] = '\0';
578 sbi->cvf_format = NULL;
579 sbi->private_data = NULL;
581 sbi->dir_ops = fs_dir_inode_ops;
583 sb->s_maxbytes = MAX_NON_LFS;
584 sb->s_op = &fat_sops;
586 hard_blksize = get_hardsect_size(sb->s_dev);
590 opts.isvfat = sbi->options.isvfat;
591 if (!parse_options((char *) data, &fat, &debug, &opts,
592 cvf_format, cvf_options))
594 /* N.B. we should parse directly into the sb structure */
595 memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options));
599 sb->s_blocksize = hard_blksize;
600 set_blocksize(sb->s_dev, hard_blksize);
601 bh = sb_bread(sb, 0);
603 printk("FAT: unable to read boot sector\n");
608 * The DOS3 partition size limit is *not* 32M as many people think.
609 * Instead, it is 64K sectors (with the usual sector size being
610 * 512 bytes, leading to a 32M limit).
612 * DOS 3 partition managers got around this problem by faking a
613 * larger sector size, ie treating multiple physical sectors as
614 * a single logical sector.
616 * We can accommodate this scheme by adjusting our cluster size,
617 * fat_start, and data_start by an appropriate value.
623 b = (struct fat_boot_sector *) bh->b_data;
624 logical_sector_size =
625 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
626 if (!logical_sector_size
627 || (logical_sector_size & (logical_sector_size - 1))) {
628 printk("FAT: bogus logical sector size %d\n",
629 logical_sector_size);
634 sbi->cluster_size = b->cluster_size;
635 if (!sbi->cluster_size
636 || (sbi->cluster_size & (sbi->cluster_size - 1))) {
637 printk("FAT: bogus cluster size %d\n", sbi->cluster_size);
642 if (logical_sector_size < hard_blksize) {
643 printk("FAT: logical sector size too small for device"
644 " (logical sector size = %d)\n", logical_sector_size);
649 sbi->cluster_bits = ffs(logical_sector_size * sbi->cluster_size) - 1;
651 sbi->fat_start = CF_LE_W(b->reserved);
653 if (!b->fat_length && b->fat32_length) {
654 struct fat_boot_fsinfo *fsinfo;
655 struct buffer_head *fsinfo_bh;
656 int fsinfo_block, fsinfo_offset;
660 sbi->fat_length = CF_LE_L(b->fat32_length);
661 sbi->root_cluster = CF_LE_L(b->root_cluster);
663 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
664 /* MC - if info_sector is 0, don't multiply by 0 */
665 if (sbi->fsinfo_sector == 0)
666 sbi->fsinfo_sector = 1;
669 (sbi->fsinfo_sector * logical_sector_size) / hard_blksize;
671 (sbi->fsinfo_sector * logical_sector_size) % hard_blksize;
673 if (fsinfo_block != 0) {
674 fsinfo_bh = sb_bread(sb, fsinfo_block);
675 if (fsinfo_bh == NULL) {
676 printk("FAT: bread failed, FSINFO block"
677 " (blocknr = %d)\n", fsinfo_block);
682 fsinfo = (struct fat_boot_fsinfo *)&fsinfo_bh->b_data[fsinfo_offset];
683 if (!IS_FSINFO(fsinfo)) {
684 printk("FAT: Did not find valid FSINFO signature.\n"
685 "Found signature1 0x%x signature2 0x%x sector=%ld.\n",
686 CF_LE_L(fsinfo->signature1),
687 CF_LE_L(fsinfo->signature2),
690 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
691 sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
694 if (fsinfo_block != 0)
698 sbi->fat_length = CF_LE_W(b->fat_length);
699 sbi->root_cluster = 0;
700 sbi->free_clusters = -1; /* Don't know yet */
703 sbi->dir_per_block = logical_sector_size / sizeof(struct msdos_dir_entry);
704 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
706 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
708 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
709 rootdir_sectors = sbi->dir_entries
710 * sizeof(struct msdos_dir_entry) / logical_sector_size;
711 sbi->data_start = sbi->dir_start + rootdir_sectors;
712 total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
713 if (total_sectors == 0)
714 total_sectors = CF_LE_L(b->total_sect);
715 sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size;
719 sbi->fat_bits = fat32 ? 32 :
721 (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
723 sbi->fat_length * logical_sector_size * 8 / sbi->fat_bits;
724 error = !sbi->fats || (sbi->dir_entries & (sbi->dir_per_block - 1))
725 || sbi->clusters + 2 > fat_clusters + MSDOS_MAX_EXTRA
726 || logical_sector_size < 512
727 || PAGE_CACHE_SIZE < logical_sector_size
728 || !b->secs_track || !b->heads;
735 sb->s_blocksize = logical_sector_size;
736 sb->s_blocksize_bits = ffs(logical_sector_size) - 1;
737 set_blocksize(sb->s_dev, sb->s_blocksize);
738 sbi->cvf_format = &default_cvf;
739 if (!strcmp(cvf_format, "none"))
742 i = detect_cvf(sb,cvf_format);
744 error = cvf_formats[i]->mount_cvf(sb, cvf_options);
745 if (error || debug) {
746 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
747 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
748 "uid=%d,gid=%d,umask=%03o%s]\n",
749 sbi->fat_bits,opts.name_check,
750 opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
751 MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
752 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
753 "se=%u,ts=%u,ls=%d,rc=%ld,fc=%u]\n",
754 b->media, sbi->cluster_size, sbi->fats,
755 sbi->fat_start, sbi->fat_length, sbi->dir_start,
756 sbi->dir_entries, sbi->data_start,
757 CF_LE_W(get_unaligned((unsigned short *)&b->sectors)),
758 CF_LE_L(b->total_sect), logical_sector_size,
759 sbi->root_cluster, sbi->free_clusters);
760 printk ("hard sector size = %d\n", hard_blksize);
763 if (sbi->clusters + 2 > fat_clusters)
764 sbi->clusters = fat_clusters - 2;
768 sb->s_magic = MSDOS_SUPER_MAGIC;
769 /* set up enough so that it can read an inode */
770 init_MUTEX(&sbi->fat_lock);
772 cp = opts.codepage ? opts.codepage : 437;
773 sprintf(buf, "cp%d", cp);
774 sbi->nls_disk = load_nls(buf);
775 if (! sbi->nls_disk) {
776 /* Fail only if explicit charset specified */
777 if (opts.codepage != 0)
779 sbi->options.codepage = 0; /* already 0?? */
780 sbi->nls_disk = load_nls_default();
784 if (sbi->options.isvfat && !opts.utf8) {
785 p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT;
786 sbi->nls_io = load_nls(p);
788 /* Fail only if explicit charset specified */
793 sbi->nls_io = load_nls_default();
795 root_inode = new_inode(sb);
798 root_inode->i_ino = MSDOS_ROOT_INO;
799 fat_read_root(root_inode);
800 insert_inode_hash(root_inode);
801 sb->s_root = d_alloc_root(root_inode);
805 sbi->cvf_format = cvf_formats[i];
806 ++cvf_format_use_count[i];
811 printk("FAT: get root inode failed\n");
813 unload_nls(sbi->nls_io);
815 unload_nls(sbi->nls_disk);
819 printk("VFS: Can't find a valid FAT filesystem on dev %s.\n",
820 kdevname(sb->s_dev));
823 if (opts.iocharset) {
824 printk("FAT: freeing iocharset=%s\n", opts.iocharset);
825 kfree(opts.iocharset);
827 if(sbi->private_data)
828 kfree(sbi->private_data);
829 sbi->private_data = NULL;
834 int fat_statfs(struct super_block *sb,struct statfs *buf)
838 if (MSDOS_SB(sb)->cvf_format &&
839 MSDOS_SB(sb)->cvf_format->cvf_statfs)
840 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
841 sizeof(struct statfs));
844 if (MSDOS_SB(sb)->free_clusters != -1)
845 free = MSDOS_SB(sb)->free_clusters;
848 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
849 if (!fat_access(sb,nr,-1)) free++;
850 MSDOS_SB(sb)->free_clusters = free;
853 buf->f_type = sb->s_magic;
854 buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits;
855 buf->f_blocks = MSDOS_SB(sb)->clusters;
857 buf->f_bavail = free;
858 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
862 static int is_exec(char *extension)
864 char *exe_extensions = "EXECOMBAT", *walk;
866 for (walk = exe_extensions; *walk; walk += 3)
867 if (!strncmp(extension, walk, 3))
872 static int fat_writepage(struct page *page)
874 return block_write_full_page(page,fat_get_block);
876 static int fat_readpage(struct file *file, struct page *page)
878 return block_read_full_page(page,fat_get_block);
880 static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
882 return cont_prepare_write(page,from,to,fat_get_block,
883 &MSDOS_I(page->mapping->host)->mmu_private);
885 static int _fat_bmap(struct address_space *mapping, long block)
887 return generic_block_bmap(mapping,block,fat_get_block);
889 static struct address_space_operations fat_aops = {
890 readpage: fat_readpage,
891 writepage: fat_writepage,
892 sync_page: block_sync_page,
893 prepare_write: fat_prepare_write,
894 commit_write: generic_commit_write,
898 /* doesn't deal with root inode */
899 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
901 struct super_block *sb = inode->i_sb;
902 struct msdos_sb_info *sbi = MSDOS_SB(sb);
905 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
906 MSDOS_I(inode)->i_pos = 0;
907 MSDOS_I(inode)->i_fat_inode = inode;
908 inode->i_uid = sbi->options.fs_uid;
909 inode->i_gid = sbi->options.fs_gid;
910 inode->i_version = ++event;
911 inode->i_generation = CURRENT_TIME;
913 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
914 inode->i_generation &= ~1;
915 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
916 ~sbi->options.fs_umask) | S_IFDIR;
917 inode->i_op = sbi->dir_ops;
918 inode->i_fop = &fat_dir_operations;
920 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
921 if (sbi->fat_bits == 32)
922 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
924 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
925 inode->i_nlink = fat_subdirs(inode);
926 /* includes .., compensating for "self" */
928 if (!inode->i_nlink) {
929 printk("directory %d: i_nlink == 0\n",inode->i_ino);
933 if ((nr = MSDOS_I(inode)->i_start) != 0)
935 inode->i_size += 1 << sbi->cluster_bits;
936 if (!(nr = fat_access(sb, nr, -1))) {
937 printk("Directory %ld: bad FAT\n",
942 MSDOS_I(inode)->mmu_private = inode->i_size;
943 } else { /* not a directory */
944 inode->i_generation |= 1;
945 inode->i_mode = MSDOS_MKMODE(de->attr,
946 ((sbi->options.showexec &&
948 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
949 & ~sbi->options.fs_umask) | S_IFREG;
950 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
951 if (sbi->fat_bits == 32)
952 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
954 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
955 inode->i_size = CF_LE_L(de->size);
956 inode->i_op = &fat_file_inode_operations;
957 inode->i_fop = &fat_file_operations;
958 inode->i_mapping->a_ops = &fat_aops;
959 MSDOS_I(inode)->mmu_private = inode->i_size;
961 if(de->attr & ATTR_SYS)
962 if (sbi->options.sys_immutable)
963 inode->i_flags |= S_IMMUTABLE;
964 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
965 /* this is as close to the truth as we can get ... */
966 inode->i_blksize = 1 << sbi->cluster_bits;
967 inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
968 & ~(inode->i_blksize - 1)) >> 9;
969 inode->i_mtime = inode->i_atime =
970 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
972 MSDOS_SB(sb)->options.isvfat
973 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
975 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
978 void fat_write_inode(struct inode *inode, int wait)
980 struct super_block *sb = inode->i_sb;
981 struct buffer_head *bh;
982 struct msdos_dir_entry *raw_entry;
986 i_pos = MSDOS_I(inode)->i_pos;
987 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
991 if (!(bh = fat_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
992 printk("dev = %s, i_pos = %llu\n", kdevname(inode->i_dev), i_pos);
993 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
997 spin_lock(&fat_inode_lock);
998 if (i_pos != MSDOS_I(inode)->i_pos) {
999 spin_unlock(&fat_inode_lock);
1005 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1006 [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1007 if (S_ISDIR(inode->i_mode)) {
1008 raw_entry->attr = ATTR_DIR;
1009 raw_entry->size = 0;
1012 raw_entry->attr = ATTR_NONE;
1013 raw_entry->size = CT_LE_L(inode->i_size);
1015 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1016 MSDOS_I(inode)->i_attrs;
1017 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1018 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1019 fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
1020 raw_entry->time = CT_LE_W(raw_entry->time);
1021 raw_entry->date = CT_LE_W(raw_entry->date);
1022 if (MSDOS_SB(sb)->options.isvfat) {
1023 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
1024 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
1025 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1026 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1028 spin_unlock(&fat_inode_lock);
1029 fat_mark_buffer_dirty(sb, bh);
1035 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1037 struct super_block *sb = dentry->d_sb;
1038 struct inode *inode = dentry->d_inode;
1041 /* FAT cannot truncate to a longer file */
1042 if (attr->ia_valid & ATTR_SIZE) {
1043 if (attr->ia_size > inode->i_size)
1047 error = inode_change_ok(inode, attr);
1049 return MSDOS_SB(sb)->options.quiet ? 0 : error;
1051 if (((attr->ia_valid & ATTR_UID) &&
1052 (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
1053 ((attr->ia_valid & ATTR_GID) &&
1054 (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
1055 ((attr->ia_valid & ATTR_MODE) &&
1056 (attr->ia_mode & ~MSDOS_VALID_MODE)))
1060 return MSDOS_SB(sb)->options.quiet ? 0 : error;
1062 error = inode_setattr(inode, attr);
1066 if (S_ISDIR(inode->i_mode))
1067 inode->i_mode |= S_IXUGO;
1069 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
1070 & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
1071 ~MSDOS_SB(sb)->options.fs_umask;
1074 MODULE_LICENSE("GPL");