The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/fat/inode.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 
   33 extern 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)
   75 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
   76 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
   77 
   78 void 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 
   86 static 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 
   93 void 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 
  102 void 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 
  111 struct 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 
  133 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
  134 
  135 struct 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);
  152 out:
  153         return inode;
  154 }
  155 
  156 void 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 
  167 void 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 
  179 void 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 
  210 static 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         }
  371 out:
  372         return ret;
  373 }
  374 
  375 static 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  */
  429 struct 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 
  512 int 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 
  528 static 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  */
  546 struct super_block *
  547 fat_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         sbi->prev_free = 0;
  641         if (!b->fat_length && b->fat32_length) {
  642                 struct fat_boot_fsinfo *fsinfo;
  643                 struct buffer_head *fsinfo_bh;
  644                 int fsinfo_block, fsinfo_offset;
  645 
  646                 /* Must be FAT32 */
  647                 fat32 = 1;
  648                 sbi->fat_length = CF_LE_L(b->fat32_length);
  649                 sbi->root_cluster = CF_LE_L(b->root_cluster);
  650 
  651                 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
  652                 /* MC - if info_sector is 0, don't multiply by 0 */
  653                 if (sbi->fsinfo_sector == 0)
  654                         sbi->fsinfo_sector = 1;
  655 
  656                 fsinfo_block =
  657                         (sbi->fsinfo_sector * logical_sector_size) / hard_blksize;
  658                 fsinfo_offset =
  659                         (sbi->fsinfo_sector * logical_sector_size) % hard_blksize;
  660                 fsinfo_bh = bh;
  661                 if (fsinfo_block != 0) {
  662                         fsinfo_bh = sb_bread(sb, fsinfo_block);
  663                         if (fsinfo_bh == NULL) {
  664                                 printk("FAT: bread failed, FSINFO block"
  665                                        " (blocknr = %d)\n", fsinfo_block);
  666                                 brelse(bh);
  667                                 goto out_invalid;
  668                         }
  669                 }
  670                 fsinfo = (struct fat_boot_fsinfo *)&fsinfo_bh->b_data[fsinfo_offset];
  671                 if (!IS_FSINFO(fsinfo)) {
  672                         printk("FAT: Did not find valid FSINFO signature.\n"
  673                                "Found signature1 0x%x signature2 0x%x sector=%ld.\n",
  674                                CF_LE_L(fsinfo->signature1),
  675                                CF_LE_L(fsinfo->signature2),
  676                                sbi->fsinfo_sector);
  677                 } else {
  678                         sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
  679                         sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
  680                 }
  681 
  682                 if (fsinfo_block != 0)
  683                         brelse(fsinfo_bh);
  684         } else {
  685                 fat32 = 0;
  686                 sbi->fat_length = CF_LE_W(b->fat_length);
  687                 sbi->root_cluster = 0;
  688                 sbi->free_clusters = -1; /* Don't know yet */
  689         }
  690 
  691         sbi->dir_per_block = logical_sector_size / sizeof(struct msdos_dir_entry);
  692         sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
  693 
  694         sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
  695         sbi->dir_entries =
  696                 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
  697         rootdir_sectors = sbi->dir_entries
  698                 * sizeof(struct msdos_dir_entry) / logical_sector_size;
  699         sbi->data_start = sbi->dir_start + rootdir_sectors;
  700         total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
  701         if (total_sectors == 0)
  702                 total_sectors = CF_LE_L(b->total_sect);
  703         sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size;
  704 
  705         error = 0;
  706         if (!error) {
  707                 sbi->fat_bits = fat32 ? 32 :
  708                         (fat ? fat :
  709                          (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
  710                 fat_clusters =
  711                         sbi->fat_length * logical_sector_size * 8 / sbi->fat_bits;
  712                 error = !sbi->fats || (sbi->dir_entries & (sbi->dir_per_block - 1))
  713                         || sbi->clusters + 2 > fat_clusters + MSDOS_MAX_EXTRA
  714                         || logical_sector_size < 512
  715                         || PAGE_CACHE_SIZE < logical_sector_size
  716                         || !b->secs_track || !b->heads;
  717         }
  718         brelse(bh);
  719 
  720         if (error)
  721                 goto out_invalid;
  722 
  723         sb->s_blocksize = logical_sector_size;
  724         sb->s_blocksize_bits = ffs(logical_sector_size) - 1;
  725         set_blocksize(sb->s_dev, sb->s_blocksize);
  726         sbi->cvf_format = &default_cvf;
  727         if (!strcmp(cvf_format, "none"))
  728                 i = -1;
  729         else
  730                 i = detect_cvf(sb,cvf_format);
  731         if (i >= 0)
  732                 error = cvf_formats[i]->mount_cvf(sb, cvf_options);
  733         if (error || debug) {
  734                 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
  735                 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
  736                        "uid=%d,gid=%d,umask=%03o%s]\n",
  737                        sbi->fat_bits,opts.name_check,
  738                        opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
  739                        MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
  740                 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
  741                        "se=%u,ts=%u,ls=%d,rc=%ld,fc=%u]\n",
  742                        b->media, sbi->cluster_size, sbi->fats,
  743                        sbi->fat_start, sbi->fat_length, sbi->dir_start,
  744                        sbi->dir_entries, sbi->data_start,
  745                        CF_LE_W(get_unaligned((unsigned short *)&b->sectors)),
  746                        CF_LE_L(b->total_sect), logical_sector_size,
  747                        sbi->root_cluster, sbi->free_clusters);
  748                 printk ("hard sector size = %d\n", hard_blksize);
  749         }
  750         if (i < 0)
  751                 if (sbi->clusters + 2 > fat_clusters)
  752                         sbi->clusters = fat_clusters - 2;
  753         if (error)
  754                 goto out_invalid;
  755 
  756         sb->s_magic = MSDOS_SUPER_MAGIC;
  757         /* set up enough so that it can read an inode */
  758         init_MUTEX(&sbi->fat_lock);
  759 
  760         cp = opts.codepage ? opts.codepage : 437;
  761         sprintf(buf, "cp%d", cp);
  762         sbi->nls_disk = load_nls(buf);
  763         if (! sbi->nls_disk) {
  764                 /* Fail only if explicit charset specified */
  765                 if (opts.codepage != 0)
  766                         goto out_fail;
  767                 sbi->options.codepage = 0; /* already 0?? */
  768                 sbi->nls_disk = load_nls_default();
  769         }
  770 
  771         sbi->nls_io = NULL;
  772         if (sbi->options.isvfat && !opts.utf8) {
  773                 p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT;
  774                 sbi->nls_io = load_nls(p);
  775                 if (! sbi->nls_io)
  776                         /* Fail only if explicit charset specified */
  777                         if (opts.iocharset)
  778                                 goto out_unload_nls;
  779         }
  780         if (! sbi->nls_io)
  781                 sbi->nls_io = load_nls_default();
  782 
  783         root_inode = new_inode(sb);
  784         if (!root_inode)
  785                 goto out_unload_nls;
  786         root_inode->i_ino = MSDOS_ROOT_INO;
  787         fat_read_root(root_inode);
  788         insert_inode_hash(root_inode);
  789         sb->s_root = d_alloc_root(root_inode);
  790         if (!sb->s_root)
  791                 goto out_no_root;
  792         if(i >= 0) {
  793                 sbi->cvf_format = cvf_formats[i];
  794                 ++cvf_format_use_count[i];
  795         }
  796         return sb;
  797 
  798 out_no_root:
  799         printk("FAT: get root inode failed\n");
  800         iput(root_inode);
  801         unload_nls(sbi->nls_io);
  802 out_unload_nls:
  803         unload_nls(sbi->nls_disk);
  804         goto out_fail;
  805 out_invalid:
  806         if (!silent) {
  807                 printk("VFS: Can't find a valid FAT filesystem on dev %s.\n",
  808                         kdevname(sb->s_dev));
  809         }
  810 out_fail:
  811         if (opts.iocharset) {
  812                 printk("FAT: freeing iocharset=%s\n", opts.iocharset);
  813                 kfree(opts.iocharset);
  814         }
  815         if(sbi->private_data)
  816                 kfree(sbi->private_data);
  817         sbi->private_data = NULL;
  818  
  819         return NULL;
  820 }
  821 
  822 int fat_statfs(struct super_block *sb,struct statfs *buf)
  823 {
  824         int free,nr;
  825        
  826         if (MSDOS_SB(sb)->cvf_format &&
  827             MSDOS_SB(sb)->cvf_format->cvf_statfs)
  828                 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
  829                                                 sizeof(struct statfs));
  830           
  831         lock_fat(sb);
  832         if (MSDOS_SB(sb)->free_clusters != -1)
  833                 free = MSDOS_SB(sb)->free_clusters;
  834         else {
  835                 free = 0;
  836                 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
  837                         if (!fat_access(sb,nr,-1)) free++;
  838                 MSDOS_SB(sb)->free_clusters = free;
  839         }
  840         unlock_fat(sb);
  841         buf->f_type = sb->s_magic;
  842         buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits;
  843         buf->f_blocks = MSDOS_SB(sb)->clusters;
  844         buf->f_bfree = free;
  845         buf->f_bavail = free;
  846         buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
  847         return 0;
  848 }
  849 
  850 static int is_exec(char *extension)
  851 {
  852         char *exe_extensions = "EXECOMBAT", *walk;
  853 
  854         for (walk = exe_extensions; *walk; walk += 3)
  855                 if (!strncmp(extension, walk, 3))
  856                         return 1;
  857         return 0;
  858 }
  859 
  860 static int fat_writepage(struct page *page)
  861 {
  862         return block_write_full_page(page,fat_get_block);
  863 }
  864 static int fat_readpage(struct file *file, struct page *page)
  865 {
  866         return block_read_full_page(page,fat_get_block);
  867 }
  868 static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  869 {
  870         return cont_prepare_write(page,from,to,fat_get_block,
  871                 &MSDOS_I(page->mapping->host)->mmu_private);
  872 }
  873 static int _fat_bmap(struct address_space *mapping, long block)
  874 {
  875         return generic_block_bmap(mapping,block,fat_get_block);
  876 }
  877 static struct address_space_operations fat_aops = {
  878         readpage: fat_readpage,
  879         writepage: fat_writepage,
  880         sync_page: block_sync_page,
  881         prepare_write: fat_prepare_write,
  882         commit_write: generic_commit_write,
  883         bmap: _fat_bmap
  884 };
  885 
  886 /* doesn't deal with root inode */
  887 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
  888 {
  889         struct super_block *sb = inode->i_sb;
  890         struct msdos_sb_info *sbi = MSDOS_SB(sb);
  891         int nr;
  892 
  893         INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
  894         MSDOS_I(inode)->i_location = 0;
  895         MSDOS_I(inode)->i_fat_inode = inode;
  896         inode->i_uid = sbi->options.fs_uid;
  897         inode->i_gid = sbi->options.fs_gid;
  898         inode->i_version = ++event;
  899         inode->i_generation = CURRENT_TIME;
  900         
  901         if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
  902                 inode->i_generation &= ~1;
  903                 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
  904                     ~sbi->options.fs_umask) | S_IFDIR;
  905                 inode->i_op = sbi->dir_ops;
  906                 inode->i_fop = &fat_dir_operations;
  907 
  908                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
  909                 if (sbi->fat_bits == 32) {
  910                         MSDOS_I(inode)->i_start |=
  911                                 (CF_LE_W(de->starthi) << 16);
  912                 }
  913                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  914                 inode->i_nlink = fat_subdirs(inode);
  915                     /* includes .., compensating for "self" */
  916 #ifdef DEBUG
  917                 if (!inode->i_nlink) {
  918                         printk("directory %d: i_nlink == 0\n",inode->i_ino);
  919                         inode->i_nlink = 1;
  920                 }
  921 #endif
  922                 if ((nr = MSDOS_I(inode)->i_start) != 0)
  923                         while (nr != -1) {
  924                                 inode->i_size += 1 << sbi->cluster_bits;
  925                                 if (!(nr = fat_access(sb, nr, -1))) {
  926                                         printk("Directory %ld: bad FAT\n",
  927                                             inode->i_ino);
  928                                         break;
  929                                 }
  930                         }
  931                 MSDOS_I(inode)->mmu_private = inode->i_size;
  932         } else { /* not a directory */
  933                 inode->i_generation |= 1;
  934                 inode->i_mode = MSDOS_MKMODE(de->attr,
  935                     ((sbi->options.showexec &&
  936                        !is_exec(de->ext))
  937                         ? S_IRUGO|S_IWUGO : S_IRWXUGO)
  938                     & ~sbi->options.fs_umask) | S_IFREG;
  939                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
  940                 if (sbi->fat_bits == 32) {
  941                         MSDOS_I(inode)->i_start |=
  942                                 (CF_LE_W(de->starthi) << 16);
  943                 }
  944                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  945                 inode->i_size = CF_LE_L(de->size);
  946                 inode->i_op = &fat_file_inode_operations;
  947                 inode->i_fop = &fat_file_operations;
  948                 inode->i_mapping->a_ops = &fat_aops;
  949                 MSDOS_I(inode)->mmu_private = inode->i_size;
  950         }
  951         if(de->attr & ATTR_SYS)
  952                 if (sbi->options.sys_immutable)
  953                         inode->i_flags |= S_IMMUTABLE;
  954         MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
  955         /* this is as close to the truth as we can get ... */
  956         inode->i_blksize = 1 << sbi->cluster_bits;
  957         inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
  958                            & ~(inode->i_blksize - 1)) >> 9;
  959         inode->i_mtime = inode->i_atime =
  960                 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
  961         inode->i_ctime =
  962                 MSDOS_SB(sb)->options.isvfat
  963                 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
  964                 : inode->i_mtime;
  965         MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
  966 }
  967 
  968 void fat_write_inode(struct inode *inode, int wait)
  969 {
  970         struct super_block *sb = inode->i_sb;
  971         struct buffer_head *bh;
  972         struct msdos_dir_entry *raw_entry;
  973         unsigned int i_pos;
  974 
  975 retry:
  976         i_pos = MSDOS_I(inode)->i_location;
  977         if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
  978                 return;
  979         }
  980         lock_kernel();
  981         if (!(bh = fat_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
  982                 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos);
  983                 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
  984                 unlock_kernel();
  985                 return;
  986         }
  987         spin_lock(&fat_inode_lock);
  988         if (i_pos != MSDOS_I(inode)->i_location) {
  989                 spin_unlock(&fat_inode_lock);
  990                 fat_brelse(sb, bh);
  991                 unlock_kernel();
  992                 goto retry;
  993         }
  994 
  995         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
  996             [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
  997         if (S_ISDIR(inode->i_mode)) {
  998                 raw_entry->attr = ATTR_DIR;
  999                 raw_entry->size = 0;
 1000         }
 1001         else {
 1002                 raw_entry->attr = ATTR_NONE;
 1003                 raw_entry->size = CT_LE_L(inode->i_size);
 1004         }
 1005         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
 1006             MSDOS_I(inode)->i_attrs;
 1007         raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
 1008         raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
 1009         fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
 1010         raw_entry->time = CT_LE_W(raw_entry->time);
 1011         raw_entry->date = CT_LE_W(raw_entry->date);
 1012         if (MSDOS_SB(sb)->options.isvfat) {
 1013                 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
 1014                 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
 1015                 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
 1016                 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
 1017         }
 1018         spin_unlock(&fat_inode_lock);
 1019         fat_mark_buffer_dirty(sb, bh);
 1020         fat_brelse(sb, bh);
 1021         unlock_kernel();
 1022 }
 1023 
 1024 
 1025 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
 1026 {
 1027         struct super_block *sb = dentry->d_sb;
 1028         struct inode *inode = dentry->d_inode;
 1029         int error;
 1030 
 1031         /* FAT cannot truncate to a longer file */
 1032         if (attr->ia_valid & ATTR_SIZE) {
 1033                 if (attr->ia_size > inode->i_size)
 1034                         return -EPERM;
 1035         }
 1036 
 1037         error = inode_change_ok(inode, attr);
 1038         if (error)
 1039                 return MSDOS_SB(sb)->options.quiet ? 0 : error;
 1040 
 1041         if (((attr->ia_valid & ATTR_UID) && 
 1042              (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
 1043             ((attr->ia_valid & ATTR_GID) && 
 1044              (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
 1045             ((attr->ia_valid & ATTR_MODE) &&
 1046              (attr->ia_mode & ~MSDOS_VALID_MODE)))
 1047                 error = -EPERM;
 1048 
 1049         if (error)
 1050                 return MSDOS_SB(sb)->options.quiet ? 0 : error;
 1051 
 1052         error = inode_setattr(inode, attr);
 1053         if (error)
 1054                 return error;
 1055 
 1056         if (S_ISDIR(inode->i_mode))
 1057                 inode->i_mode |= S_IXUGO;
 1058 
 1059         inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
 1060             & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
 1061             ~MSDOS_SB(sb)->options.fs_umask;
 1062         return 0;
 1063 }
 1064 MODULE_LICENSE("GPL");

Cache object: 0ef30aa21484889d610124331a4fa0ba


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.