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/version.h>
14 #define __NO_VERSION__
15 #include <linux/module.h>
17 #include <linux/msdos_fs.h>
18 #include <linux/nls.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
23 #include <linux/major.h>
24 #include <linux/blkdev.h>
26 #include <linux/stat.h>
27 #include <linux/locks.h>
28 #include <linux/fat_cvf.h>
29 #include <linux/malloc.h>
33 #include <asm/uaccess.h>
34 #include <asm/unaligned.h>
36 extern struct cvf_format default_cvf
, bigblock_cvf
;
38 /* #define FAT_PARANOIA 1 */
41 # define PRINTK(x) printk x
45 #if (DEBUG_LEVEL >= 1)
46 # define PRINTK1(x) printk x
52 * New FAT inode stuff. We do the following:
53 * a) i_ino is constant and has nothing with on-disk location.
54 * b) FAT manages its own cache of directory entries.
55 * c) *This* cache is indexed by on-disk location.
56 * d) inode has an associated directory entry, all right, but
58 * e) currently entries are stored within struct inode. That should
60 * f) we deal with races in the following way:
61 * 1. readdir() and lookup() do FAT-dir-cache lookup.
62 * 2. rename() unhashes the F-d-c entry and rehashes it in
64 * 3. unlink() and rmdir() unhash F-d-c entry.
65 * 4. fat_write_inode() checks whether the thing is unhashed.
66 * If it is we silently return. If it isn't we do bread(),
67 * check if the location is still valid and retry if it
68 * isn't. Otherwise we do changes.
69 * 5. Spinlock is used to protect hash/unhash/location check/lookup
70 * 6. fat_clear_inode() unhashes the F-d-c entry.
71 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
72 * and consider negative result as cache miss.
75 #define FAT_HASH_BITS 8
76 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
77 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
78 static struct list_head fat_inode_hashtable
[FAT_HASH_SIZE
];
79 spinlock_t fat_inode_lock
= SPIN_LOCK_UNLOCKED
;
81 void fat_hash_init(void) {
83 for(i
=0;i
<FAT_HASH_SIZE
;i
++) {
84 INIT_LIST_HEAD(&fat_inode_hashtable
[i
]);
88 static inline unsigned long fat_hash(struct super_block
*sb
, int i_pos
)
90 unsigned long tmp
= (unsigned long)i_pos
| (unsigned long) sb
;
91 tmp
= tmp
+ (tmp
>> FAT_HASH_BITS
) + (tmp
>> FAT_HASH_BITS
*2);
92 return tmp
& FAT_HASH_MASK
;
95 void fat_attach(struct inode
*inode
, int i_pos
) {
96 spin_lock(&fat_inode_lock
);
97 MSDOS_I(inode
)->i_location
= i_pos
;
98 list_add(&MSDOS_I(inode
)->i_fat_hash
,
99 fat_inode_hashtable
+fat_hash(inode
->i_sb
, i_pos
));
100 spin_unlock(&fat_inode_lock
);
103 void fat_detach(struct inode
*inode
) {
104 spin_lock(&fat_inode_lock
);
105 MSDOS_I(inode
)->i_location
= 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
, int i_pos
) {
112 struct list_head
*p
= fat_inode_hashtable
+ fat_hash(sb
, i_pos
);
113 struct list_head
*walk
;
114 struct msdos_inode_info
*i
;
115 struct inode
*inode
= NULL
;
116 spin_lock(&fat_inode_lock
);
117 for(walk
=p
->next
;walk
!=p
;walk
=walk
->next
) {
118 i
= list_entry(walk
, struct msdos_inode_info
, i_fat_hash
);
119 if (i
->i_fat_inode
->i_sb
!= sb
)
121 if (i
->i_location
!= i_pos
)
123 inode
= igrab(i
->i_fat_inode
);
125 spin_unlock(&fat_inode_lock
);
129 static void fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
);
131 struct inode
*fat_build_inode(struct super_block
*sb
,
132 struct msdos_dir_entry
*de
, int ino
, int *res
)
136 inode
= fat_iget(sb
, ino
);
139 inode
= get_empty_inode();
145 inode
->i_dev
= sb
->s_dev
;
146 inode
->i_ino
= iunique(sb
, MSDOS_ROOT_INO
);
147 fat_fill_inode(inode
, de
);
148 fat_attach(inode
, ino
);
149 insert_inode_hash(inode
);
154 void fat_delete_inode(struct inode
*inode
)
161 void fat_clear_inode(struct inode
*inode
)
163 spin_lock(&fat_inode_lock
);
164 fat_cache_inval_inode(inode
);
165 list_del(&MSDOS_I(inode
)->i_fat_hash
);
166 spin_unlock(&fat_inode_lock
);
169 void fat_put_super(struct super_block
*sb
)
171 if (MSDOS_SB(sb
)->cvf_format
->cvf_version
) {
172 dec_cvf_format_use_count_by_version(MSDOS_SB(sb
)->cvf_format
->cvf_version
);
173 MSDOS_SB(sb
)->cvf_format
->unmount_cvf(sb
);
175 if (MSDOS_SB(sb
)->fat_bits
== 32) {
176 fat_clusters_flush(sb
);
178 fat_cache_inval_dev(sb
->s_dev
);
179 set_blocksize (sb
->s_dev
,BLOCK_SIZE
);
180 if (MSDOS_SB(sb
)->nls_disk
) {
181 unload_nls(MSDOS_SB(sb
)->nls_disk
);
182 MSDOS_SB(sb
)->nls_disk
= NULL
;
183 MSDOS_SB(sb
)->options
.codepage
= 0;
185 if (MSDOS_SB(sb
)->nls_io
) {
186 unload_nls(MSDOS_SB(sb
)->nls_io
);
187 MSDOS_SB(sb
)->nls_io
= NULL
;
190 * Note: the iocharset option might have been specified
191 * without enabling nls_io, so check for it here.
193 if (MSDOS_SB(sb
)->options
.iocharset
) {
194 kfree(MSDOS_SB(sb
)->options
.iocharset
);
195 MSDOS_SB(sb
)->options
.iocharset
= NULL
;
200 static int parse_options(char *options
,int *fat
, int *blksize
, int *debug
,
201 struct fat_mount_options
*opts
,
202 char *cvf_format
, char *cvf_options
)
204 char *this_char
,*value
,save
,*savep
;
208 opts
->name_check
= 'n';
209 opts
->conversion
= 'b';
210 opts
->fs_uid
= current
->uid
;
211 opts
->fs_gid
= current
->gid
;
212 opts
->fs_umask
= current
->fs
->umask
;
213 opts
->quiet
= opts
->sys_immutable
= opts
->dotsOK
= opts
->showexec
= 0;
216 opts
->iocharset
= NULL
;
223 for (this_char
= strtok(options
,","); this_char
;
224 this_char
= strtok(NULL
,",")) {
225 if ((value
= strchr(this_char
,'=')) != NULL
) {
230 if (!strcmp(this_char
,"check") && value
) {
231 if (value
[0] && !value
[1] && strchr("rns",*value
))
232 opts
->name_check
= *value
;
233 else if (!strcmp(value
,"relaxed"))
234 opts
->name_check
= 'r';
235 else if (!strcmp(value
,"normal"))
236 opts
->name_check
= 'n';
237 else if (!strcmp(value
,"strict"))
238 opts
->name_check
= 's';
241 else if (!strcmp(this_char
,"conv") && value
) {
242 if (value
[0] && !value
[1] && strchr("bta",*value
))
243 opts
->conversion
= *value
;
244 else if (!strcmp(value
,"binary"))
245 opts
->conversion
= 'b';
246 else if (!strcmp(value
,"text"))
247 opts
->conversion
= 't';
248 else if (!strcmp(value
,"auto"))
249 opts
->conversion
= 'a';
252 else if (!strcmp(this_char
,"dots")) {
255 else if (!strcmp(this_char
,"nodots")) {
258 else if (!strcmp(this_char
,"showexec")) {
261 else if (!strcmp(this_char
,"dotsOK") && value
) {
262 if (!strcmp(value
,"yes")) opts
->dotsOK
= 1;
263 else if (!strcmp(value
,"no")) opts
->dotsOK
= 0;
266 else if (!strcmp(this_char
,"uid")) {
267 if (!value
|| !*value
) ret
= 0;
269 opts
->fs_uid
= simple_strtoul(value
,&value
,0);
273 else if (!strcmp(this_char
,"gid")) {
274 if (!value
|| !*value
) ret
= 0;
276 opts
->fs_gid
= simple_strtoul(value
,&value
,0);
280 else if (!strcmp(this_char
,"umask")) {
281 if (!value
|| !*value
) ret
= 0;
283 opts
->fs_umask
= simple_strtoul(value
,&value
,8);
287 else if (!strcmp(this_char
,"debug")) {
291 else if (!strcmp(this_char
,"fat")) {
292 if (!value
|| !*value
) ret
= 0;
294 *fat
= simple_strtoul(value
,&value
,0);
295 if (*value
|| (*fat
!= 12 && *fat
!= 16 &&
300 else if (!strcmp(this_char
,"quiet")) {
302 else opts
->quiet
= 1;
304 else if (!strcmp(this_char
,"blocksize")) {
305 if (!value
|| !*value
) ret
= 0;
307 *blksize
= simple_strtoul(value
,&value
,0);
308 if (*value
|| (*blksize
!= 512 &&
309 *blksize
!= 1024 && *blksize
!= 2048))
313 else if (!strcmp(this_char
,"sys_immutable")) {
315 else opts
->sys_immutable
= 1;
317 else if (!strcmp(this_char
,"codepage") && value
) {
318 opts
->codepage
= simple_strtoul(value
,&value
,0);
320 else printk ("MSDOS FS: Using codepage %d\n",
323 else if (!strcmp(this_char
,"iocharset") && value
) {
325 while (*value
&& *value
!= ',') value
++;
328 char * buffer
= kmalloc(len
+1, GFP_KERNEL
);
330 opts
->iocharset
= buffer
;
331 memcpy(buffer
, p
, len
);
333 printk("MSDOS FS: IO charset %s\n",
339 else if (!strcmp(this_char
,"cvf_format")) {
342 strncpy(cvf_format
,value
,20);
344 else if (!strcmp(this_char
,"cvf_options")) {
347 strncpy(cvf_options
,value
,100);
350 if (this_char
!= options
) *(this_char
-1) = ',';
351 if (value
) *savep
= save
;
359 static void fat_read_root(struct inode
*inode
)
361 struct super_block
*sb
= inode
->i_sb
;
362 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
365 INIT_LIST_HEAD(&MSDOS_I(inode
)->i_fat_hash
);
366 MSDOS_I(inode
)->i_location
= 0;
367 MSDOS_I(inode
)->i_fat_inode
= inode
;
368 inode
->i_uid
= sbi
->options
.fs_uid
;
369 inode
->i_gid
= sbi
->options
.fs_gid
;
370 inode
->i_version
= ++event
;
371 inode
->i_mode
= (S_IRWXUGO
& ~sbi
->options
.fs_umask
) | S_IFDIR
;
372 inode
->i_op
= sbi
->dir_ops
;
373 inode
->i_fop
= &fat_dir_operations
;
374 if (sbi
->fat_bits
== 32) {
375 MSDOS_I(inode
)->i_start
= sbi
->root_cluster
;
376 if ((nr
= MSDOS_I(inode
)->i_start
) != 0) {
378 inode
->i_size
+= SECTOR_SIZE
*sbi
->cluster_size
;
379 if (!(nr
= fat_access(sb
,nr
,-1))) {
380 printk("Directory %ld: bad FAT\n",
387 MSDOS_I(inode
)->i_start
= 0;
388 inode
->i_size
= sbi
->dir_entries
*
389 sizeof(struct msdos_dir_entry
);
391 inode
->i_blksize
= sbi
->cluster_size
* SECTOR_SIZE
;
393 ((inode
->i_size
+inode
->i_blksize
-1)>>sbi
->cluster_bits
) *
395 MSDOS_I(inode
)->i_logstart
= 0;
396 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
398 MSDOS_I(inode
)->i_attrs
= 0;
399 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= 0;
400 MSDOS_I(inode
)->i_ctime_ms
= 0;
401 inode
->i_nlink
= fat_subdirs(inode
)+2;
404 static struct super_operations fat_sops
= {
405 write_inode
: fat_write_inode
,
406 delete_inode
: fat_delete_inode
,
407 put_super
: fat_put_super
,
409 clear_inode
: fat_clear_inode
,
413 * Read the super block of an MS-DOS FS.
415 * Note that this may be called from vfat_read_super
416 * with some fields already initialized.
419 fat_read_super(struct super_block
*sb
, void *data
, int silent
,
420 struct inode_operations
*fs_dir_inode_ops
)
422 struct inode
*root_inode
;
423 struct buffer_head
*bh
;
424 struct fat_boot_sector
*b
;
425 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
427 int data_sectors
,logical_sector_size
,sector_mult
,fat_clusters
=0;
428 int debug
,error
,fat
,cp
;
431 struct fat_mount_options opts
;
435 char cvf_options
[101];
437 cvf_format
[0] = '\0';
438 cvf_options
[0] = '\0';
439 sbi
->cvf_format
= NULL
;
440 sbi
->private_data
= NULL
;
442 sbi
->dir_ops
= fs_dir_inode_ops
;
443 sb
->s_op
= &fat_sops
;
444 if (hardsect_size
[MAJOR(sb
->s_dev
)] != NULL
){
445 blksize
= hardsect_size
[MAJOR(sb
->s_dev
)][MINOR(sb
->s_dev
)];
447 printk ("MSDOS: Hardware sector size is %d\n",blksize
);
452 opts
.isvfat
= sbi
->options
.isvfat
;
453 if (!parse_options((char *) data
, &fat
, &blksize
, &debug
, &opts
,
454 cvf_format
, cvf_options
)
455 || (blksize
!= 512 && blksize
!= 1024 && blksize
!= 2048))
457 /* N.B. we should parse directly into the sb structure */
458 memcpy(&(sbi
->options
), &opts
, sizeof(struct fat_mount_options
));
463 /* Force the superblock to a larger size here. */
464 sb
->s_blocksize
= blksize
;
465 set_blocksize(sb
->s_dev
, blksize
);
469 /* The first read is always 1024 bytes */
470 sb
->s_blocksize
= 1024;
471 set_blocksize(sb
->s_dev
, 1024);
473 bh
= bread(sb
->s_dev
, 0, sb
->s_blocksize
);
474 if (bh
== NULL
|| !buffer_uptodate(bh
)) {
480 * The DOS3 partition size limit is *not* 32M as many people think.
481 * Instead, it is 64K sectors (with the usual sector size being
482 * 512 bytes, leading to a 32M limit).
484 * DOS 3 partition managers got around this problem by faking a
485 * larger sector size, ie treating multiple physical sectors as
486 * a single logical sector.
488 * We can accommodate this scheme by adjusting our cluster size,
489 * fat_start, and data_start by an appropriate value.
494 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
495 /* don't divide by zero */
497 b
= (struct fat_boot_sector
*) bh
->b_data
;
498 logical_sector_size
=
499 CF_LE_W(get_unaligned((unsigned short *) &b
->sector_size
));
500 sector_mult
= logical_sector_size
>> SECTOR_BITS
;
501 sbi
->cluster_size
= b
->cluster_size
*sector_mult
;
502 if (!sbi
->cluster_size
|| (sbi
->cluster_size
& (sbi
->cluster_size
-1))) {
503 printk("fatfs: bogus cluster size\n");
507 for (sbi
->cluster_bits
=0;
508 1<<sbi
->cluster_bits
<sbi
->cluster_size
;
511 sbi
->cluster_bits
+= SECTOR_BITS
;
513 sbi
->fat_start
= CF_LE_W(b
->reserved
)*sector_mult
;
514 if (!b
->fat_length
&& b
->fat32_length
) {
515 struct fat_boot_fsinfo
*fsinfo
;
519 sbi
->fat_length
= CF_LE_L(b
->fat32_length
)*sector_mult
;
520 sbi
->root_cluster
= CF_LE_L(b
->root_cluster
);
522 /* MC - if info_sector is 0, don't multiply by 0 */
523 if(CF_LE_W(b
->info_sector
) == 0) {
525 logical_sector_size
+ 0x1e0;
528 (CF_LE_W(b
->info_sector
) * logical_sector_size
)
531 if (sbi
->fsinfo_offset
+ sizeof(struct fat_boot_fsinfo
) > sb
->s_blocksize
) {
532 printk("fat_read_super: Bad fsinfo_offset\n");
536 fsinfo
= (struct fat_boot_fsinfo
*)
537 &bh
->b_data
[sbi
->fsinfo_offset
];
538 if (CF_LE_L(fsinfo
->signature
) != 0x61417272) {
539 printk("fat_read_super: Did not find valid FSINFO "
540 "signature. Found 0x%x\n",
541 CF_LE_L(fsinfo
->signature
));
543 sbi
->free_clusters
= CF_LE_L(fsinfo
->free_clusters
);
547 sbi
->fat_length
= CF_LE_W(b
->fat_length
)*sector_mult
;
548 sbi
->root_cluster
= 0;
549 sbi
->free_clusters
= -1; /* Don't know yet */
551 sbi
->dir_start
= CF_LE_W(b
->reserved
)*sector_mult
+
552 b
->fats
*sbi
->fat_length
;
554 CF_LE_W(get_unaligned((unsigned short *) &b
->dir_entries
));
555 sbi
->data_start
= sbi
->dir_start
+ROUND_TO_MULTIPLE((
556 sbi
->dir_entries
<< MSDOS_DIR_BITS
) >> SECTOR_BITS
,
558 data_sectors
= CF_LE_W(get_unaligned((unsigned short *) &b
->sectors
));
560 data_sectors
= CF_LE_L(b
->total_sect
);
562 data_sectors
= data_sectors
* sector_mult
- sbi
->data_start
;
563 error
= !b
->cluster_size
|| !sector_mult
;
565 sbi
->clusters
= b
->cluster_size
? data_sectors
/
566 b
->cluster_size
/sector_mult
: 0;
567 sbi
->fat_bits
= fat32
? 32 :
569 (sbi
->clusters
> MSDOS_FAT12
? 16 : 12));
570 fat_clusters
= sbi
->fat_length
*SECTOR_SIZE
*8/
572 error
= !sbi
->fats
|| (sbi
->dir_entries
&
573 (MSDOS_DPS
-1)) || sbi
->clusters
+2 > fat_clusters
+
574 MSDOS_MAX_EXTRA
|| (logical_sector_size
& (SECTOR_SIZE
-1))
575 || !b
->secs_track
|| !b
->heads
;
578 set_blocksize(sb
->s_dev
, blksize
);
580 This must be done after the brelse because the bh is a dummy
581 allocated by fat_bread (see buffer.c)
583 sb
->s_blocksize
= blksize
; /* Using this small block size solves */
584 /* the misfit with buffer cache and cluster */
585 /* because clusters (DOS) are often aligned */
586 /* on odd sectors. */
587 sb
->s_blocksize_bits
= blksize
== 512 ? 9 : (blksize
== 1024 ? 10 : 11);
588 if (!strcmp(cvf_format
,"none"))
591 i
= detect_cvf(sb
,cvf_format
);
593 error
= cvf_formats
[i
]->mount_cvf(sb
,cvf_options
);
594 else if (sb
->s_blocksize
== 512)
595 sbi
->cvf_format
= &default_cvf
;
597 sbi
->cvf_format
= &bigblock_cvf
;
598 if (error
|| debug
) {
599 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
600 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
601 "uid=%d,gid=%d,umask=%03o%s]\n",
602 sbi
->fat_bits
,opts
.name_check
,
603 opts
.conversion
,opts
.fs_uid
,opts
.fs_gid
,opts
.fs_umask
,
604 MSDOS_CAN_BMAP(sbi
) ? ",bmap" : "");
605 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
606 "se=%d,ts=%ld,ls=%d,rc=%ld,fc=%u]\n",
607 b
->media
,sbi
->cluster_size
,
608 sbi
->fats
,sbi
->fat_start
,
610 sbi
->dir_start
,sbi
->dir_entries
,
612 CF_LE_W(*(unsigned short *) &b
->sectors
),
613 (unsigned long)b
->total_sect
,logical_sector_size
,
614 sbi
->root_cluster
,sbi
->free_clusters
);
615 printk ("Transaction block size = %d\n",blksize
);
617 if (i
<0) if (sbi
->clusters
+2 > fat_clusters
)
618 sbi
->clusters
= fat_clusters
-2;
622 sb
->s_magic
= MSDOS_SUPER_MAGIC
;
623 /* set up enough so that it can read an inode */
624 init_waitqueue_head(&sbi
->fat_wait
);
625 init_MUTEX(&sbi
->fat_lock
);
628 cp
= opts
.codepage
? opts
.codepage
: 437;
629 sprintf(buf
, "cp%d", cp
);
630 sbi
->nls_disk
= load_nls(buf
);
631 if (! sbi
->nls_disk
) {
632 /* Fail only if explicit charset specified */
633 if (opts
.codepage
!= 0)
635 sbi
->options
.codepage
= 0; /* already 0?? */
636 sbi
->nls_disk
= load_nls_default();
640 if (sbi
->options
.isvfat
&& !opts
.utf8
) {
641 p
= opts
.iocharset
? opts
.iocharset
: "iso8859-1";
642 sbi
->nls_io
= load_nls(p
);
644 /* Fail only if explicit charset specified */
649 sbi
->nls_io
= load_nls_default();
651 root_inode
=get_empty_inode();
654 root_inode
->i_sb
= sb
;
655 root_inode
->i_dev
= sb
->s_dev
;
656 root_inode
->i_ino
= MSDOS_ROOT_INO
;
657 fat_read_root(root_inode
);
658 insert_inode_hash(root_inode
);
659 sb
->s_root
= d_alloc_root(root_inode
);
663 sbi
->cvf_format
= cvf_formats
[i
];
664 ++cvf_format_use_count
[i
];
669 printk("get root inode failed\n");
671 unload_nls(sbi
->nls_io
);
673 unload_nls(sbi
->nls_disk
);
677 printk("VFS: Can't find a valid MSDOS filesystem on dev %s.\n",
678 kdevname(sb
->s_dev
));
681 printk("FAT bread failed\n");
683 if (opts
.iocharset
) {
684 printk("VFS: freeing iocharset=%s\n", opts
.iocharset
);
685 kfree(opts
.iocharset
);
687 if(sbi
->private_data
)
688 kfree(sbi
->private_data
);
689 sbi
->private_data
=NULL
;
694 int fat_statfs(struct super_block
*sb
,struct statfs
*buf
)
698 if (MSDOS_SB(sb
)->cvf_format
&&
699 MSDOS_SB(sb
)->cvf_format
->cvf_statfs
)
700 return MSDOS_SB(sb
)->cvf_format
->cvf_statfs(sb
,buf
,
701 sizeof(struct statfs
));
704 if (MSDOS_SB(sb
)->free_clusters
!= -1)
705 free
= MSDOS_SB(sb
)->free_clusters
;
708 for (nr
= 2; nr
< MSDOS_SB(sb
)->clusters
+2; nr
++)
709 if (!fat_access(sb
,nr
,-1)) free
++;
710 MSDOS_SB(sb
)->free_clusters
= free
;
713 buf
->f_type
= sb
->s_magic
;
714 buf
->f_bsize
= MSDOS_SB(sb
)->cluster_size
*SECTOR_SIZE
;
715 buf
->f_blocks
= MSDOS_SB(sb
)->clusters
;
717 buf
->f_bavail
= free
;
718 buf
->f_namelen
= MSDOS_SB(sb
)->options
.isvfat
? 260 : 12;
722 static int is_exec(char *extension
)
724 char *exe_extensions
= "EXECOMBAT", *walk
;
726 for (walk
= exe_extensions
; *walk
; walk
+= 3)
727 if (!strncmp(extension
, walk
, 3))
732 static int fat_writepage(struct dentry
*dentry
, struct page
*page
)
734 return block_write_full_page(page
,fat_get_block
);
736 static int fat_readpage(struct dentry
*dentry
, struct page
*page
)
738 return block_read_full_page(page
,fat_get_block
);
740 static int fat_prepare_write(struct page
*page
, unsigned from
, unsigned to
)
742 return cont_prepare_write(page
,from
,to
,fat_get_block
,
743 &MSDOS_I((struct inode
*)page
->mapping
->host
)->mmu_private
);
745 static int _fat_bmap(struct address_space
*mapping
, long block
)
747 return generic_block_bmap(mapping
,block
,fat_get_block
);
749 static struct address_space_operations fat_aops
= {
750 readpage
: fat_readpage
,
751 writepage
: fat_writepage
,
752 prepare_write
: fat_prepare_write
,
753 commit_write
: generic_commit_write
,
757 /* doesn't deal with root inode */
758 static void fat_fill_inode(struct inode
*inode
, struct msdos_dir_entry
*de
)
760 struct super_block
*sb
= inode
->i_sb
;
761 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
764 INIT_LIST_HEAD(&MSDOS_I(inode
)->i_fat_hash
);
765 MSDOS_I(inode
)->i_location
= 0;
766 MSDOS_I(inode
)->i_fat_inode
= inode
;
767 inode
->i_uid
= sbi
->options
.fs_uid
;
768 inode
->i_gid
= sbi
->options
.fs_gid
;
769 inode
->i_version
= ++event
;
770 if ((de
->attr
& ATTR_DIR
) && !IS_FREE(de
->name
)) {
771 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,S_IRWXUGO
&
772 ~sbi
->options
.fs_umask
) | S_IFDIR
;
773 inode
->i_op
= sbi
->dir_ops
;
774 inode
->i_fop
= &fat_dir_operations
;
776 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
777 if (sbi
->fat_bits
== 32) {
778 MSDOS_I(inode
)->i_start
|=
779 (CF_LE_W(de
->starthi
) << 16);
781 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
782 inode
->i_nlink
= fat_subdirs(inode
);
783 /* includes .., compensating for "self" */
785 if (!inode
->i_nlink
) {
786 printk("directory %d: i_nlink == 0\n",inode
->i_ino
);
791 if ((nr
= MSDOS_I(inode
)->i_start
) != 0)
793 inode
->i_size
+= SECTOR_SIZE
*sbi
->cluster_size
;
794 if (!(nr
= fat_access(sb
,nr
,-1))) {
795 printk("Directory %ld: bad FAT\n",
800 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
801 } else { /* not a directory */
802 inode
->i_mode
= MSDOS_MKMODE(de
->attr
,
803 ((IS_NOEXEC(inode
) ||
804 (sbi
->options
.showexec
&&
806 ? S_IRUGO
|S_IWUGO
: S_IRWXUGO
)
807 & ~sbi
->options
.fs_umask
) | S_IFREG
;
808 MSDOS_I(inode
)->i_start
= CF_LE_W(de
->start
);
809 if (sbi
->fat_bits
== 32) {
810 MSDOS_I(inode
)->i_start
|=
811 (CF_LE_W(de
->starthi
) << 16);
813 MSDOS_I(inode
)->i_logstart
= MSDOS_I(inode
)->i_start
;
815 inode
->i_size
= CF_LE_L(de
->size
);
816 inode
->i_op
= &fat_file_inode_operations
;
817 inode
->i_fop
= &fat_file_operations
;
818 inode
->i_mapping
->a_ops
= &fat_aops
;
819 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
821 if(de
->attr
& ATTR_SYS
)
822 if (sbi
->options
.sys_immutable
)
823 inode
->i_flags
|= S_IMMUTABLE
;
824 MSDOS_I(inode
)->i_attrs
= de
->attr
& ATTR_UNUSED
;
825 /* this is as close to the truth as we can get ... */
826 inode
->i_blksize
= sbi
->cluster_size
*SECTOR_SIZE
;
828 ((inode
->i_size
+inode
->i_blksize
-1)>>sbi
->cluster_bits
) *
830 inode
->i_mtime
= inode
->i_atime
=
831 date_dos2unix(CF_LE_W(de
->time
),CF_LE_W(de
->date
));
833 MSDOS_SB(sb
)->options
.isvfat
834 ? date_dos2unix(CF_LE_W(de
->ctime
),CF_LE_W(de
->cdate
))
836 MSDOS_I(inode
)->i_ctime_ms
= de
->ctime_ms
;
839 void fat_write_inode(struct inode
*inode
)
841 struct super_block
*sb
= inode
->i_sb
;
842 struct buffer_head
*bh
;
843 struct msdos_dir_entry
*raw_entry
;
847 i_pos
= MSDOS_I(inode
)->i_location
;
848 if (inode
->i_ino
== MSDOS_ROOT_INO
|| !i_pos
) return;
849 if (!(bh
= fat_bread(sb
, i_pos
>> MSDOS_DPB_BITS
))) {
850 printk("dev = %s, ino = %d\n", kdevname(inode
->i_dev
), i_pos
);
851 fat_fs_panic(sb
, "msdos_write_inode: unable to read i-node block");
854 spin_lock(&fat_inode_lock
);
855 if (i_pos
!= MSDOS_I(inode
)->i_location
) {
856 spin_unlock(&fat_inode_lock
);
861 raw_entry
= &((struct msdos_dir_entry
*) (bh
->b_data
))
862 [i_pos
& (MSDOS_DPB
-1)];
863 if (S_ISDIR(inode
->i_mode
)) {
864 raw_entry
->attr
= ATTR_DIR
;
868 raw_entry
->attr
= ATTR_NONE
;
869 raw_entry
->size
= CT_LE_L(inode
->i_size
);
871 raw_entry
->attr
|= MSDOS_MKATTR(inode
->i_mode
) |
872 MSDOS_I(inode
)->i_attrs
;
873 raw_entry
->start
= CT_LE_W(MSDOS_I(inode
)->i_logstart
);
874 raw_entry
->starthi
= CT_LE_W(MSDOS_I(inode
)->i_logstart
>> 16);
875 fat_date_unix2dos(inode
->i_mtime
,&raw_entry
->time
,&raw_entry
->date
);
876 raw_entry
->time
= CT_LE_W(raw_entry
->time
);
877 raw_entry
->date
= CT_LE_W(raw_entry
->date
);
878 if (MSDOS_SB(sb
)->options
.isvfat
) {
879 fat_date_unix2dos(inode
->i_ctime
,&raw_entry
->ctime
,&raw_entry
->cdate
);
880 raw_entry
->ctime_ms
= MSDOS_I(inode
)->i_ctime_ms
;
881 raw_entry
->ctime
= CT_LE_W(raw_entry
->ctime
);
882 raw_entry
->cdate
= CT_LE_W(raw_entry
->cdate
);
884 spin_unlock(&fat_inode_lock
);
885 fat_mark_buffer_dirty(sb
, bh
, 1);
890 int fat_notify_change(struct dentry
* dentry
, struct iattr
* attr
)
892 struct super_block
*sb
= dentry
->d_sb
;
893 struct inode
*inode
= dentry
->d_inode
;
896 error
= inode_change_ok(inode
, attr
);
898 return MSDOS_SB(sb
)->options
.quiet
? 0 : error
;
900 if (((attr
->ia_valid
& ATTR_UID
) &&
901 (attr
->ia_uid
!= MSDOS_SB(sb
)->options
.fs_uid
)) ||
902 ((attr
->ia_valid
& ATTR_GID
) &&
903 (attr
->ia_gid
!= MSDOS_SB(sb
)->options
.fs_gid
)) ||
904 ((attr
->ia_valid
& ATTR_MODE
) &&
905 (attr
->ia_mode
& ~MSDOS_VALID_MODE
)))
909 return MSDOS_SB(sb
)->options
.quiet
? 0 : error
;
911 inode_setattr(inode
, attr
);
913 if (IS_NOEXEC(inode
) && !S_ISDIR(inode
->i_mode
))
914 inode
->i_mode
&= S_IFMT
| S_IRUGO
| S_IWUGO
;
916 inode
->i_mode
|= S_IXUGO
;
918 inode
->i_mode
= ((inode
->i_mode
& S_IFMT
) | ((((inode
->i_mode
& S_IRWXU
919 & ~MSDOS_SB(sb
)->options
.fs_umask
) | S_IRUSR
) >> 6)*S_IXUGO
)) &
920 ~MSDOS_SB(sb
)->options
.fs_umask
;