1/* 2 * linux/fs/fat/inode.c 3 * 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 7 * 8 * Fixes: 9 * 10 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 11 */ 12 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> 23#include <linux/fs.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> 29 30#include <asm/uaccess.h> 31#include <asm/unaligned.h> 32 33extern struct cvf_format default_cvf; 34 35/* #define FAT_PARANOIA 1 */ 36#define DEBUG_LEVEL 0 37#ifdef FAT_DEBUG 38# define PRINTK(x) printk x 39#else 40# define PRINTK(x) 41#endif 42#if (DEBUG_LEVEL >= 1) 43# define PRINTK1(x) printk x 44#else 45# define PRINTK1(x) 46#endif 47 48/* 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 54 * it may be unhashed. 55 * e) currently entries are stored within struct inode. That should 56 * change. 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 60 * a new place. 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. 70 */ 71 72#define FAT_HASH_BITS 8 73#define FAT_HASH_SIZE (1UL << FAT_HASH_BITS) 74#define FAT_HASH_MASK (FAT_HASH_SIZE-1) 75static struct list_head fat_inode_hashtable[FAT_HASH_SIZE]; 76spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED; 77 78void fat_hash_init(void) 79{ 80 int i; 81 for(i = 0; i < FAT_HASH_SIZE; i++) { 82 INIT_LIST_HEAD(&fat_inode_hashtable[i]); 83 } 84} 85 86static inline unsigned long fat_hash(struct super_block *sb, int i_pos) 87{ 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; 91} 92 93void fat_attach(struct inode *inode, int i_pos) 94{ 95 spin_lock(&fat_inode_lock); 96 MSDOS_I(inode)->i_location = 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); 100} 101 102void fat_detach(struct inode *inode) 103{ 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); 109} 110 111struct inode *fat_iget(struct super_block *sb, int i_pos) 112{ 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; 117 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) 122 continue; 123 if (i->i_location != i_pos) 124 continue; 125 inode = igrab(i->i_fat_inode); 126 if (inode) 127 break; 128 } 129 spin_unlock(&fat_inode_lock); 130 return inode; 131} 132 133static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de); 134 135struct inode *fat_build_inode(struct super_block *sb, 136 struct msdos_dir_entry *de, int ino, int *res) 137{ 138 struct inode *inode; 139 *res = 0; 140 inode = fat_iget(sb, ino); 141 if (inode) 142 goto out; 143 inode = new_inode(sb); 144 *res = -ENOMEM; 145 if (!inode) 146 goto out; 147 *res = 0; 148 inode->i_ino = iunique(sb, MSDOS_ROOT_INO); 149 fat_fill_inode(inode, de); 150 fat_attach(inode, ino); 151 insert_inode_hash(inode); 152out: 153 return inode; 154} 155 156void fat_delete_inode(struct inode *inode) 157{ 158 if (!is_bad_inode(inode)) { 159 lock_kernel(); 160 inode->i_size = 0; 161 fat_truncate(inode); 162 unlock_kernel(); 163 } 164 clear_inode(inode); 165} 166 167void fat_clear_inode(struct inode *inode) 168{ 169 if (is_bad_inode(inode)) 170 return; 171 lock_kernel(); 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); 176 unlock_kernel(); 177} 178 179void fat_put_super(struct super_block *sb) 180{ 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); 184 } 185 if (MSDOS_SB(sb)->fat_bits == 32) { 186 fat_clusters_flush(sb); 187 } 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; 194 } 195 if (MSDOS_SB(sb)->nls_io) { 196 unload_nls(MSDOS_SB(sb)->nls_io); 197 MSDOS_SB(sb)->nls_io = NULL; 198 } 199 /* 200 * Note: the iocharset option might have been specified 201 * without enabling nls_io, so check for it here. 202 */ 203 if (MSDOS_SB(sb)->options.iocharset) { 204 kfree(MSDOS_SB(sb)->options.iocharset); 205 MSDOS_SB(sb)->options.iocharset = NULL; 206 } 207} 208 209 210static int parse_options(char *options,int *fat, int *debug, 211 struct fat_mount_options *opts, 212 char *cvf_format, char *cvf_options) 213{ 214 char *this_char,*value,save,*savep; 215 char *p; 216 int ret = 1, len; 217 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; 224 opts->codepage = 0; 225 opts->nocase = 0; 226 opts->shortname = 0; 227 opts->utf8 = 0; 228 opts->iocharset = NULL; 229 *debug = *fat = 0; 230 231 if (!options) 232 goto out; 233 save = 0; 234 savep = NULL; 235 for (this_char = strtok(options,","); this_char; 236 this_char = strtok(NULL,",")) { 237 if ((value = strchr(this_char,'=')) != NULL) { 238 save = *value; 239 savep = value; 240 *value++ = 0; 241 } 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'; 251 else ret = 0; 252 } 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'; 262 else ret = 0; 263 } 264 else if (!strcmp(this_char,"dots")) { 265 opts->dotsOK = 1; 266 } 267 else if (!strcmp(this_char,"nocase")) { 268 opts->nocase = 1; 269 } 270 else if (!strcmp(this_char,"nodots")) { 271 opts->dotsOK = 0; 272 } 273 else if (!strcmp(this_char,"showexec")) { 274 opts->showexec = 1; 275 } 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; 279 else ret = 0; 280 } 281 else if (!strcmp(this_char,"uid")) { 282 if (!value || !*value) ret = 0; 283 else { 284 opts->fs_uid = simple_strtoul(value,&value,0); 285 if (*value) ret = 0; 286 } 287 } 288 else if (!strcmp(this_char,"gid")) { 289 if (!value || !*value) ret= 0; 290 else { 291 opts->fs_gid = simple_strtoul(value,&value,0); 292 if (*value) ret = 0; 293 } 294 } 295 else if (!strcmp(this_char,"umask")) { 296 if (!value || !*value) ret = 0; 297 else { 298 opts->fs_umask = simple_strtoul(value,&value,8); 299 if (*value) ret = 0; 300 } 301 } 302 else if (!strcmp(this_char,"debug")) { 303 if (value) ret = 0; 304 else *debug = 1; 305 } 306 else if (!strcmp(this_char,"fat")) { 307 if (!value || !*value) ret = 0; 308 else { 309 *fat = simple_strtoul(value,&value,0); 310 if (*value || (*fat != 12 && *fat != 16 && 311 *fat != 32)) 312 ret = 0; 313 } 314 } 315 else if (!strcmp(this_char,"quiet")) { 316 if (value) ret = 0; 317 else opts->quiet = 1; 318 } 319 else if (!strcmp(this_char,"blocksize")) { 320 printk("FAT: blocksize option is obsolete, " 321 "not supported now\n"); 322 } 323 else if (!strcmp(this_char,"sys_immutable")) { 324 if (value) ret = 0; 325 else opts->sys_immutable = 1; 326 } 327 else if (!strcmp(this_char,"codepage") && value) { 328 opts->codepage = simple_strtoul(value,&value,0); 329 if (*value) ret = 0; 330 else printk ("MSDOS FS: Using codepage %d\n", 331 opts->codepage); 332 } 333 else if (!strcmp(this_char,"iocharset") && value) { 334 p = value; 335 while (*value && *value != ',') 336 value++; 337 len = value - p; 338 if (len) { 339 char *buffer; 340 341 if (opts->iocharset != NULL) { 342 kfree(opts->iocharset); 343 opts->iocharset = NULL; 344 } 345 buffer = kmalloc(len + 1, GFP_KERNEL); 346 if (buffer != NULL) { 347 opts->iocharset = buffer; 348 memcpy(buffer, p, len); 349 buffer[len] = 0; 350 printk("MSDOS FS: IO charset %s\n", buffer); 351 } else 352 ret = 0; 353 } 354 } 355 else if (!strcmp(this_char,"cvf_format")) { 356 if (!value) 357 return 0; 358 strncpy(cvf_format,value,20); 359 } 360 else if (!strcmp(this_char,"cvf_options")) { 361 if (!value) 362 return 0; 363 strncpy(cvf_options,value,100); 364 } 365 366 if (this_char != options) *(this_char-1) = ','; 367 if (value) *savep = save; 368 if (ret == 0) 369 break; 370 } 371out: 372 return ret; 373} 374 375static void fat_read_root(struct inode *inode) 376{ 377 struct super_block *sb = inode->i_sb; 378 struct msdos_sb_info *sbi = MSDOS_SB(sb); 379 int nr; 380 381 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash); 382 MSDOS_I(inode)->i_location = 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) { 394 while (nr != -1) { 395 inode->i_size += 1 << sbi->cluster_bits; 396 if (!(nr = fat_access(sb, nr, -1))) { 397 printk("Directory %ld: bad FAT\n", 398 inode->i_ino); 399 break; 400 } 401 } 402 } 403 } else { 404 MSDOS_I(inode)->i_start = 0; 405 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); 406 } 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; 412 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; 417} 418 419/* 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_location - if ino has changed, but still in cache 425 * 3/ i_logstart - to semi-verify inode found at i_location 426 * 4/ parent->i_logstart - maybe used to hunt for the file on disc 427 * 428 */ 429struct dentry *fat_fh_to_dentry(struct super_block *sb, __u32 *fh, 430 int len, int fhtype, int parent) 431{ 432 struct inode *inode = NULL; 433 struct list_head *lp; 434 struct dentry *result; 435 436 if (fhtype != 3) 437 return ERR_PTR(-ESTALE); 438 if (len < 5) 439 return ERR_PTR(-ESTALE); 440 /* We cannot find the parent, 441 It better just *be* there */ 442 if (parent) 443 return ERR_PTR(-ESTALE); 444 445 inode = iget(sb, fh[0]); 446 if (!inode || is_bad_inode(inode) || 447 inode->i_generation != fh[1]) { 448 if (inode) iput(inode); 449 inode = NULL; 450 } 451 if (!inode) { 452 /* try 2 - see if i_location is in F-d-c 453 * require i_logstart to be the same 454 * Will fail if you truncate and then re-write 455 */ 456 457 inode = fat_iget(sb, fh[2]); 458 if (inode && MSDOS_I(inode)->i_logstart != fh[3]) { 459 iput(inode); 460 inode = NULL; 461 } 462 } 463 if (!inode) { 464 /* For now, do nothing 465 * What we could do is: 466 * follow the file starting at fh[4], and record 467 * the ".." entry, and the name of the fh[2] entry. 468 * The follow the ".." file finding the next step up. 469 * This way we build a path to the root of 470 * the tree. If this works, we lookup the path and so 471 * get this inode into the cache. 472 * Finally try the fat_iget lookup again 473 * If that fails, then weare totally out of luck 474 * But all that is for another day 475 */ 476 } 477 if (!inode) 478 return ERR_PTR(-ESTALE); 479 480 481 /* now to find a dentry. 482 * If possible, get a well-connected one 483 * 484 * Given the way that we found the inode, it *MUST* be 485 * well-connected, but it is easiest to just copy the 486 * code. 487 */ 488 spin_lock(&dcache_lock); 489 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) { 490 result = list_entry(lp,struct dentry, d_alias); 491 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) { 492 dget_locked(result); 493 result->d_vfs_flags |= DCACHE_REFERENCED; 494 spin_unlock(&dcache_lock); 495 iput(inode); 496 return result; 497 } 498 } 499 spin_unlock(&dcache_lock); 500 result = d_alloc_root(inode); 501 if (result == NULL) { 502 iput(inode); 503 return ERR_PTR(-ENOMEM); 504 } 505 result->d_op = sb->s_root->d_op; 506 result->d_flags |= DCACHE_NFSD_DISCONNECTED; 507 return result; 508 509 510} 511 512int fat_dentry_to_fh(struct dentry *de, __u32 *fh, int *lenp, int needparent) 513{ 514 int len = *lenp; 515 struct inode *inode = de->d_inode; 516 517 if (len < 5) 518 return 255; /* no room */ 519 *lenp = 5; 520 fh[0] = inode->i_ino; 521 fh[1] = inode->i_generation; 522 fh[2] = MSDOS_I(inode)->i_location; 523 fh[3] = MSDOS_I(inode)->i_logstart; 524 fh[4] = MSDOS_I(de->d_parent->d_inode)->i_logstart; 525 return 3; 526} 527 528static struct super_operations fat_sops = { 529 write_inode: fat_write_inode, 530 delete_inode: fat_delete_inode, 531 put_super: fat_put_super, 532 statfs: fat_statfs, 533 clear_inode: fat_clear_inode, 534 535 read_inode: make_bad_inode, 536 fh_to_dentry: fat_fh_to_dentry, 537 dentry_to_fh: fat_dentry_to_fh, 538}; 539 540/* 541 * Read the super block of an MS-DOS FS. 542 * 543 * Note that this may be called from vfat_read_super 544 * with some fields already initialized. 545 */ 546struct super_block * 547fat_read_super(struct super_block *sb, void *data, int silent, 548 struct inode_operations *fs_dir_inode_ops) 549{ 550 struct inode *root_inode; 551 struct buffer_head *bh; 552 struct fat_boot_sector *b; 553 struct msdos_sb_info *sbi = MSDOS_SB(sb); 554 char *p; 555 int logical_sector_size, hard_blksize, fat_clusters = 0; 556 unsigned int total_sectors, rootdir_sectors; 557 int fat32, debug, error, fat, cp; 558 struct fat_mount_options opts; 559 char buf[50]; 560 int i; 561 char cvf_format[21]; 562 char cvf_options[101]; 563 564 cvf_format[0] = '\0'; 565 cvf_options[0] = '\0'; 566 sbi->cvf_format = NULL; 567 sbi->private_data = NULL; 568 569 sbi->dir_ops = fs_dir_inode_ops; 570 571 sb->s_maxbytes = MAX_NON_LFS; 572 sb->s_op = &fat_sops; 573 574 hard_blksize = get_hardsect_size(sb->s_dev); 575 if (!hard_blksize) 576 hard_blksize = 512; 577 578 opts.isvfat = sbi->options.isvfat; 579 if (!parse_options((char *) data, &fat, &debug, &opts, 580 cvf_format, cvf_options)) 581 goto out_fail; 582 /* N.B. we should parse directly into the sb structure */ 583 memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options)); 584 585 fat_cache_init(); 586 587 sb->s_blocksize = hard_blksize; 588 set_blocksize(sb->s_dev, hard_blksize); 589 bh = sb_bread(sb, 0); 590 if (bh == NULL) { 591 printk("FAT: unable to read boot sector\n"); 592 goto out_fail; 593 } 594 595/* 596 * The DOS3 partition size limit is *not* 32M as many people think. 597 * Instead, it is 64K sectors (with the usual sector size being 598 * 512 bytes, leading to a 32M limit). 599 * 600 * DOS 3 partition managers got around this problem by faking a 601 * larger sector size, ie treating multiple physical sectors as 602 * a single logical sector. 603 * 604 * We can accommodate this scheme by adjusting our cluster size, 605 * fat_start, and data_start by an appropriate value. 606 * 607 * (by Drew Eckhardt) 608 */ 609 610 611 b = (struct fat_boot_sector *) bh->b_data; 612 logical_sector_size = 613 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size)); 614 if (!logical_sector_size 615 || (logical_sector_size & (logical_sector_size - 1))) { 616 printk("FAT: bogus logical sector size %d\n", 617 logical_sector_size); 618 brelse(bh); 619 goto out_invalid; 620 } 621 622 sbi->cluster_size = b->cluster_size; 623 if (!sbi->cluster_size 624 || (sbi->cluster_size & (sbi->cluster_size - 1))) { 625 printk("FAT: bogus cluster size %d\n", sbi->cluster_size); 626 brelse(bh); 627 goto out_invalid; 628 } 629 630 if (logical_sector_size < hard_blksize) { 631 printk("FAT: logical sector size too small for device" 632 " (logical sector size = %d)\n", logical_sector_size); 633 brelse(bh); 634 goto out_invalid; 635 } 636 637 sbi->cluster_bits = ffs(logical_sector_size * sbi->cluster_size) - 1; 638 sbi->fats = b->fats; 639 sbi->fat_start = CF_LE_W(b->reserved); 640 if (!b->fat_length && b->fat32_length) { 641 struct fat_boot_fsinfo *fsinfo; 642 struct buffer_head *fsinfo_bh; 643 int fsinfo_block, fsinfo_offset; 644 645 /* Must be FAT32 */ 646 fat32 = 1; 647 sbi->fat_length = CF_LE_L(b->fat32_length); 648 sbi->root_cluster = CF_LE_L(b->root_cluster); 649 650 sbi->fsinfo_sector = CF_LE_W(b->info_sector); 651 /* MC - if info_sector is 0, don't multiply by 0 */ 652 if (sbi->fsinfo_sector == 0) 653 sbi->fsinfo_sector = 1; 654 655 fsinfo_block = 656 (sbi->fsinfo_sector * logical_sector_size) / hard_blksize; 657 fsinfo_offset = 658 (sbi->fsinfo_sector * logical_sector_size) % hard_blksize; 659 fsinfo_bh = bh; 660 if (fsinfo_block != 0) { 661 fsinfo_bh = sb_bread(sb, fsinfo_block); 662 if (fsinfo_bh == NULL) { 663 printk("FAT: bread failed, FSINFO block" 664 " (blocknr = %d)\n", fsinfo_block); 665 brelse(bh); 666 goto out_invalid; 667 } 668 } 669 fsinfo = (struct fat_boot_fsinfo *)&fsinfo_bh->b_data[fsinfo_offset]; 670 if (!IS_FSINFO(fsinfo)) { 671 printk("FAT: Did not find valid FSINFO signature.\n" 672 "Found signature1 0x%x signature2 0x%x sector=%ld.\n", 673 CF_LE_L(fsinfo->signature1), 674 CF_LE_L(fsinfo->signature2), 675 sbi->fsinfo_sector); 676 } else { 677 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters); 678 } 679 680 if (fsinfo_block != 0) 681 brelse(fsinfo_bh); 682 } else { 683 fat32 = 0; 684 sbi->fat_length = CF_LE_W(b->fat_length); 685 sbi->root_cluster = 0; 686 sbi->free_clusters = -1; /* Don't know yet */ 687 } 688 689 sbi->dir_per_block = logical_sector_size / sizeof(struct msdos_dir_entry); 690 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; 691 692 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; 693 sbi->dir_entries = 694 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries)); 695 rootdir_sectors = sbi->dir_entries 696 * sizeof(struct msdos_dir_entry) / logical_sector_size; 697 sbi->data_start = sbi->dir_start + rootdir_sectors; 698 total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors)); 699 if (total_sectors == 0) 700 total_sectors = CF_LE_L(b->total_sect); 701 sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size; 702 703 error = 0; 704 if (!error) { 705 sbi->fat_bits = fat32 ? 32 : 706 (fat ? fat : 707 (sbi->clusters > MSDOS_FAT12 ? 16 : 12)); 708 fat_clusters = 709 sbi->fat_length * logical_sector_size * 8 / sbi->fat_bits; 710 error = !sbi->fats || (sbi->dir_entries & (sbi->dir_per_block - 1)) 711 || sbi->clusters + 2 > fat_clusters + MSDOS_MAX_EXTRA 712 || logical_sector_size < 512 713 || PAGE_CACHE_SIZE < logical_sector_size 714 || !b->secs_track || !b->heads; 715 } 716 brelse(bh); 717 718 if (error) 719 goto out_invalid; 720 721 sb->s_blocksize = logical_sector_size; 722 sb->s_blocksize_bits = ffs(logical_sector_size) - 1; 723 set_blocksize(sb->s_dev, sb->s_blocksize); 724 sbi->cvf_format = &default_cvf; 725 if (!strcmp(cvf_format, "none")) 726 i = -1; 727 else 728 i = detect_cvf(sb,cvf_format); 729 if (i >= 0) 730 error = cvf_formats[i]->mount_cvf(sb, cvf_options); 731 if (error || debug) { 732 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */ 733 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c," 734 "uid=%d,gid=%d,umask=%03o%s]\n", 735 sbi->fat_bits,opts.name_check, 736 opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask, 737 MSDOS_CAN_BMAP(sbi) ? ",bmap" : ""); 738 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld," 739 "se=%u,ts=%u,ls=%d,rc=%ld,fc=%u]\n", 740 b->media, sbi->cluster_size, sbi->fats, 741 sbi->fat_start, sbi->fat_length, sbi->dir_start, 742 sbi->dir_entries, sbi->data_start, 743 CF_LE_W(get_unaligned((unsigned short *)&b->sectors)), 744 CF_LE_L(b->total_sect), logical_sector_size, 745 sbi->root_cluster, sbi->free_clusters); 746 printk ("hard sector size = %d\n", hard_blksize); 747 } 748 if (i < 0) 749 if (sbi->clusters + 2 > fat_clusters) 750 sbi->clusters = fat_clusters - 2; 751 if (error) 752 goto out_invalid; 753 754 sb->s_magic = MSDOS_SUPER_MAGIC; 755 /* set up enough so that it can read an inode */ 756 init_MUTEX(&sbi->fat_lock); 757 sbi->prev_free = 0; 758 759 cp = opts.codepage ? opts.codepage : 437; 760 sprintf(buf, "cp%d", cp); 761 sbi->nls_disk = load_nls(buf); 762 if (! sbi->nls_disk) { 763 /* Fail only if explicit charset specified */ 764 if (opts.codepage != 0) 765 goto out_fail; 766 sbi->options.codepage = 0; /* already 0?? */ 767 sbi->nls_disk = load_nls_default(); 768 } 769 770 sbi->nls_io = NULL; 771 if (sbi->options.isvfat && !opts.utf8) { 772 p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT; 773 sbi->nls_io = load_nls(p); 774 if (! sbi->nls_io) 775 /* Fail only if explicit charset specified */ 776 if (opts.iocharset) 777 goto out_unload_nls; 778 } 779 if (! sbi->nls_io) 780 sbi->nls_io = load_nls_default(); 781 782 root_inode = new_inode(sb); 783 if (!root_inode) 784 goto out_unload_nls; 785 root_inode->i_ino = MSDOS_ROOT_INO; 786 fat_read_root(root_inode); 787 insert_inode_hash(root_inode); 788 sb->s_root = d_alloc_root(root_inode); 789 if (!sb->s_root) 790 goto out_no_root; 791 if(i >= 0) { 792 sbi->cvf_format = cvf_formats[i]; 793 ++cvf_format_use_count[i]; 794 } 795 return sb; 796 797out_no_root: 798 printk("FAT: get root inode failed\n"); 799 iput(root_inode); 800 unload_nls(sbi->nls_io); 801out_unload_nls: 802 unload_nls(sbi->nls_disk); 803 goto out_fail; 804out_invalid: 805 if (!silent) { 806 printk("VFS: Can't find a valid FAT filesystem on dev %s.\n", 807 kdevname(sb->s_dev)); 808 } 809out_fail: 810 if (opts.iocharset) { 811 printk("FAT: freeing iocharset=%s\n", opts.iocharset); 812 kfree(opts.iocharset); 813 } 814 if(sbi->private_data) 815 kfree(sbi->private_data); 816 sbi->private_data = NULL; 817 818 return NULL; 819} 820 821int fat_statfs(struct super_block *sb,struct statfs *buf) 822{ 823 int free,nr; 824 825 if (MSDOS_SB(sb)->cvf_format && 826 MSDOS_SB(sb)->cvf_format->cvf_statfs) 827 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf, 828 sizeof(struct statfs)); 829 830 lock_fat(sb); 831 if (MSDOS_SB(sb)->free_clusters != -1) 832 free = MSDOS_SB(sb)->free_clusters; 833 else { 834 free = 0; 835 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++) 836 if (!fat_access(sb,nr,-1)) free++; 837 MSDOS_SB(sb)->free_clusters = free; 838 } 839 unlock_fat(sb); 840 buf->f_type = sb->s_magic; 841 buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits; 842 buf->f_blocks = MSDOS_SB(sb)->clusters; 843 buf->f_bfree = free; 844 buf->f_bavail = free; 845 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12; 846 return 0; 847} 848 849static int is_exec(char *extension) 850{ 851 char *exe_extensions = "EXECOMBAT", *walk; 852 853 for (walk = exe_extensions; *walk; walk += 3) 854 if (!strncmp(extension, walk, 3)) 855 return 1; 856 return 0; 857} 858 859static int fat_writepage(struct page *page) 860{ 861 return block_write_full_page(page,fat_get_block); 862} 863static int fat_readpage(struct file *file, struct page *page) 864{ 865 return block_read_full_page(page,fat_get_block); 866} 867static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to) 868{ 869 return cont_prepare_write(page,from,to,fat_get_block, 870 &MSDOS_I(page->mapping->host)->mmu_private); 871} 872static int _fat_bmap(struct address_space *mapping, long block) 873{ 874 return generic_block_bmap(mapping,block,fat_get_block); 875} 876static struct address_space_operations fat_aops = { 877 readpage: fat_readpage, 878 writepage: fat_writepage, 879 sync_page: block_sync_page, 880 prepare_write: fat_prepare_write, 881 commit_write: generic_commit_write, 882 bmap: _fat_bmap 883}; 884 885/* doesn't deal with root inode */ 886static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) 887{ 888 struct super_block *sb = inode->i_sb; 889 struct msdos_sb_info *sbi = MSDOS_SB(sb); 890 int nr; 891 892 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash); 893 MSDOS_I(inode)->i_location = 0; 894 MSDOS_I(inode)->i_fat_inode = inode; 895 inode->i_uid = sbi->options.fs_uid; 896 inode->i_gid = sbi->options.fs_gid; 897 inode->i_version = ++event; 898 inode->i_generation = CURRENT_TIME; 899 900 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { 901 inode->i_generation &= ~1; 902 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO & 903 ~sbi->options.fs_umask) | S_IFDIR; 904 inode->i_op = sbi->dir_ops; 905 inode->i_fop = &fat_dir_operations; 906 907 MSDOS_I(inode)->i_start = CF_LE_W(de->start); 908 if (sbi->fat_bits == 32) { 909 MSDOS_I(inode)->i_start |= 910 (CF_LE_W(de->starthi) << 16); 911 } 912 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 913 inode->i_nlink = fat_subdirs(inode); 914 /* includes .., compensating for "self" */ 915#ifdef DEBUG 916 if (!inode->i_nlink) { 917 printk("directory %d: i_nlink == 0\n",inode->i_ino); 918 inode->i_nlink = 1; 919 } 920#endif 921 if ((nr = MSDOS_I(inode)->i_start) != 0) 922 while (nr != -1) { 923 inode->i_size += 1 << sbi->cluster_bits; 924 if (!(nr = fat_access(sb, nr, -1))) { 925 printk("Directory %ld: bad FAT\n", 926 inode->i_ino); 927 break; 928 } 929 } 930 MSDOS_I(inode)->mmu_private = inode->i_size; 931 } else { /* not a directory */ 932 inode->i_generation |= 1; 933 inode->i_mode = MSDOS_MKMODE(de->attr, 934 ((sbi->options.showexec && 935 !is_exec(de->ext)) 936 ? S_IRUGO|S_IWUGO : S_IRWXUGO) 937 & ~sbi->options.fs_umask) | S_IFREG; 938 MSDOS_I(inode)->i_start = CF_LE_W(de->start); 939 if (sbi->fat_bits == 32) { 940 MSDOS_I(inode)->i_start |= 941 (CF_LE_W(de->starthi) << 16); 942 } 943 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; 944 inode->i_size = CF_LE_L(de->size); 945 inode->i_op = &fat_file_inode_operations; 946 inode->i_fop = &fat_file_operations; 947 inode->i_mapping->a_ops = &fat_aops; 948 MSDOS_I(inode)->mmu_private = inode->i_size; 949 } 950 if(de->attr & ATTR_SYS) 951 if (sbi->options.sys_immutable) 952 inode->i_flags |= S_IMMUTABLE; 953 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED; 954 /* this is as close to the truth as we can get ... */ 955 inode->i_blksize = 1 << sbi->cluster_bits; 956 inode->i_blocks = ((inode->i_size + inode->i_blksize - 1) 957 & ~(inode->i_blksize - 1)) >> 9; 958 inode->i_mtime = inode->i_atime = 959 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date)); 960 inode->i_ctime = 961 MSDOS_SB(sb)->options.isvfat 962 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate)) 963 : inode->i_mtime; 964 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms; 965} 966 967void fat_write_inode(struct inode *inode, int wait) 968{ 969 struct super_block *sb = inode->i_sb; 970 struct buffer_head *bh; 971 struct msdos_dir_entry *raw_entry; 972 unsigned int i_pos; 973 974retry: 975 i_pos = MSDOS_I(inode)->i_location; 976 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) { 977 return; 978 } 979 lock_kernel(); 980 if (!(bh = fat_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) { 981 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos); 982 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block"); 983 unlock_kernel(); 984 return; 985 } 986 spin_lock(&fat_inode_lock); 987 if (i_pos != MSDOS_I(inode)->i_location) { 988 spin_unlock(&fat_inode_lock); 989 fat_brelse(sb, bh); 990 unlock_kernel(); 991 goto retry; 992 } 993 994 raw_entry = &((struct msdos_dir_entry *) (bh->b_data)) 995 [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)]; 996 if (S_ISDIR(inode->i_mode)) { 997 raw_entry->attr = ATTR_DIR; 998 raw_entry->size = 0; 999 } 1000 else { 1001 raw_entry->attr = ATTR_NONE; 1002 raw_entry->size = CT_LE_L(inode->i_size); 1003 } 1004 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) | 1005 MSDOS_I(inode)->i_attrs; 1006 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart); 1007 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16); 1008 fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date); 1009 raw_entry->time = CT_LE_W(raw_entry->time); 1010 raw_entry->date = CT_LE_W(raw_entry->date); 1011 if (MSDOS_SB(sb)->options.isvfat) { 1012 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate); 1013 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; 1014 raw_entry->ctime = CT_LE_W(raw_entry->ctime); 1015 raw_entry->cdate = CT_LE_W(raw_entry->cdate); 1016 } 1017 spin_unlock(&fat_inode_lock); 1018 fat_mark_buffer_dirty(sb, bh); 1019 fat_brelse(sb, bh); 1020 unlock_kernel(); 1021} 1022 1023 1024int fat_notify_change(struct dentry * dentry, struct iattr * attr) 1025{ 1026 struct super_block *sb = dentry->d_sb; 1027 struct inode *inode = dentry->d_inode; 1028 int error; 1029 1030 /* FAT cannot truncate to a longer file */ 1031 if (attr->ia_valid & ATTR_SIZE) { 1032 if (attr->ia_size > inode->i_size) 1033 return -EPERM; 1034 } 1035 1036 error = inode_change_ok(inode, attr); 1037 if (error) 1038 return MSDOS_SB(sb)->options.quiet ? 0 : error; 1039 1040 if (((attr->ia_valid & ATTR_UID) && 1041 (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) || 1042 ((attr->ia_valid & ATTR_GID) && 1043 (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) || 1044 ((attr->ia_valid & ATTR_MODE) && 1045 (attr->ia_mode & ~MSDOS_VALID_MODE))) 1046 error = -EPERM; 1047 1048 if (error) 1049 return MSDOS_SB(sb)->options.quiet ? 0 : error; 1050 1051 error = inode_setattr(inode, attr); 1052 if (error) 1053 return error; 1054 1055 if (S_ISDIR(inode->i_mode)) 1056 inode->i_mode |= S_IXUGO; 1057 1058 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU 1059 & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) & 1060 ~MSDOS_SB(sb)->options.fs_umask; 1061 return 0; 1062} 1063MODULE_LICENSE("GPL"); 1064