root/fs/msdos/fat.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fat_access
  2. cache_init
  3. cache_lookup
  4. list_cache
  5. cache_add
  6. cache_inval_inode
  7. cache_inval_dev
  8. get_cluster
  9. msdos_smap
  10. fat_free

   1 /*
   2  *  linux/fs/msdos/fat.c
   3  *
   4  *  Written 1992,1993 by Werner Almesberger
   5  */
   6 
   7 #ifdef MODULE
   8 #include <linux/module.h>
   9 #endif
  10 
  11 #include <linux/msdos_fs.h>
  12 #include <linux/kernel.h>
  13 #include <linux/errno.h>
  14 #include <linux/string.h>
  15 #include <linux/stat.h>
  16 
  17 #include "msbuffer.h"
  18 
  19 static struct fat_cache *fat_cache,cache[FAT_CACHE];
  20 
  21 /* Returns the this'th FAT entry, -1 if it is an end-of-file entry. If
  22    new_value is != -1, that FAT entry is replaced by it. */
  23 
  24 int fat_access(struct super_block *sb,int nr,int new_value)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         struct buffer_head *bh,*bh2,*c_bh,*c_bh2;
  27         unsigned char *p_first,*p_last;
  28         int first,last,next,copy;
  29 
  30         if ((unsigned) (nr-2) >= MSDOS_SB(sb)->clusters) return 0;
  31         if (MSDOS_SB(sb)->fat_bits == 16) first = last = nr*2;
  32         else {
  33                 first = nr*3/2;
  34                 last = first+1;
  35         }
  36         if (!(bh = bread(sb->s_dev,MSDOS_SB(sb)->fat_start+(first >>
  37             SECTOR_BITS),SECTOR_SIZE))) {
  38                 printk("bread in fat_access failed\n");
  39                 return 0;
  40         }
  41         if ((first >> SECTOR_BITS) == (last >> SECTOR_BITS))
  42                 bh2 = bh;
  43         else {
  44                 if (!(bh2 = bread(sb->s_dev,MSDOS_SB(sb)->fat_start+(last
  45                     >> SECTOR_BITS),SECTOR_SIZE))) {
  46                         brelse(bh);
  47                         printk("bread in fat_access failed\n");
  48                         return 0;
  49                 }
  50         }
  51         if (MSDOS_SB(sb)->fat_bits == 16) {
  52                 p_first = p_last = NULL; /* GCC needs that stuff */
  53                 next = CF_LE_W(((unsigned short *) bh->b_data)[(first &
  54                     (SECTOR_SIZE-1)) >> 1]);
  55                 if (next >= 0xfff7) next = -1;
  56         }
  57         else {
  58                 p_first = &((unsigned char *) bh->b_data)[first & (SECTOR_SIZE-1)];
  59                 p_last = &((unsigned char *) bh2->b_data)[(first+1) &
  60                     (SECTOR_SIZE-1)];
  61                 if (nr & 1) next = ((*p_first >> 4) | (*p_last << 4)) & 0xfff;
  62                 else next = (*p_first+(*p_last << 8)) & 0xfff;
  63                 if (next >= 0xff7) next = -1;
  64         }
  65         if (new_value != -1) {
  66                 if (MSDOS_SB(sb)->fat_bits == 16)
  67                         ((unsigned short *) bh->b_data)[(first & (SECTOR_SIZE-1)) >>
  68                             1] = CT_LE_W(new_value);
  69                 else {
  70                         if (nr & 1) {
  71                                 *p_first = (*p_first & 0xf) | (new_value << 4);
  72                                 *p_last = new_value >> 4;
  73                         }
  74                         else {
  75                                 *p_first = new_value & 0xff;
  76                                 *p_last = (*p_last & 0xf0) | (new_value >> 8);
  77                         }
  78                         mark_buffer_dirty(bh2, 1);
  79                 }
  80                 mark_buffer_dirty(bh, 1);
  81                 for (copy = 1; copy < MSDOS_SB(sb)->fats; copy++) {
  82                         if (!(c_bh = bread(sb->s_dev,MSDOS_SB(sb)->
  83                             fat_start+(first >> SECTOR_BITS)+MSDOS_SB(sb)->
  84                             fat_length*copy,SECTOR_SIZE))) break;
  85                         memcpy(c_bh->b_data,bh->b_data,SECTOR_SIZE);
  86                         mark_buffer_dirty(c_bh, 1);
  87                         if (bh != bh2) {
  88                                 if (!(c_bh2 = bread(sb->s_dev,
  89                                     MSDOS_SB(sb)->fat_start+(first >>
  90                                     SECTOR_BITS)+MSDOS_SB(sb)->fat_length*copy
  91                                     +1,SECTOR_SIZE))) {
  92                                         brelse(c_bh);
  93                                         break;
  94                                 }
  95                                 memcpy(c_bh2->b_data,bh2->b_data,SECTOR_SIZE);
  96                                 brelse(c_bh2);
  97                         }
  98                         brelse(c_bh);
  99                 }
 100         }
 101         brelse(bh);
 102         if (bh != bh2) brelse(bh2);
 103         return next;
 104 }
 105 
 106 
 107 void cache_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         static int initialized = 0;
 110         int count;
 111 
 112         if (initialized) return;
 113         fat_cache = &cache[0];
 114         for (count = 0; count < FAT_CACHE; count++) {
 115                 cache[count].device = 0;
 116                 cache[count].next = count == FAT_CACHE-1 ? NULL :
 117                     &cache[count+1];
 118         }
 119         initialized = 1;
 120 }
 121 
 122 
 123 void cache_lookup(struct inode *inode,int cluster,int *f_clu,int *d_clu)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         struct fat_cache *walk;
 126 
 127 #ifdef DEBUG
 128 printk("cache lookup: <%s,%d> %d (%d,%d) -> ", kdevname(inode->i_dev),
 129        inode->i_ino, cluster, *f_clu, *d_clu);
 130 #endif
 131         for (walk = fat_cache; walk; walk = walk->next)
 132                 if (inode->i_dev == walk->device
 133                     && walk->ino == inode->i_ino
 134                     && walk->file_cluster <= cluster
 135                     && walk->file_cluster > *f_clu) {
 136                         *d_clu = walk->disk_cluster;
 137 #ifdef DEBUG
 138 printk("cache hit: %d (%d)\n",walk->file_cluster,*d_clu);
 139 #endif
 140                         if ((*f_clu = walk->file_cluster) == cluster) return;
 141                 }
 142 #ifdef DEBUG
 143 printk("cache miss\n");
 144 #endif
 145 }
 146 
 147 
 148 #ifdef DEBUG
 149 static void list_cache(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         struct fat_cache *walk;
 152 
 153         for (walk = fat_cache; walk; walk = walk->next) {
 154                 if (walk->device)
 155                         printk("<%s,%d>(%d,%d) ", kdevname(walk->device),
 156                                walk->ino, walk->file_cluster, walk->disk_cluster);
 157                 else printk("-- ");
 158         }
 159         printk("\n");
 160 }
 161 #endif
 162 
 163 
 164 void cache_add(struct inode *inode,int f_clu,int d_clu)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166         struct fat_cache *walk,*last;
 167 
 168 #ifdef DEBUG
 169 printk("cache add: <%s,%d> %d (%d)\n", kdevname(inode->i_dev),
 170        inode->i_ino, f_clu, d_clu);
 171 #endif
 172         last = NULL;
 173         for (walk = fat_cache; walk->next; walk = (last = walk)->next)
 174                 if (inode->i_dev == walk->device
 175                     && walk->ino == inode->i_ino
 176                     && walk->file_cluster == f_clu) {
 177                         if (walk->disk_cluster != d_clu) {
 178                                 printk("FAT cache corruption");
 179                                 cache_inval_inode(inode);
 180                                 return;
 181                         }
 182                         /* update LRU */
 183                         if (last == NULL) return;
 184                         last->next = walk->next;
 185                         walk->next = fat_cache;
 186                         fat_cache = walk;
 187 #ifdef DEBUG
 188 list_cache();
 189 #endif
 190                         return;
 191                 }
 192         walk->device = inode->i_dev;
 193         walk->ino = inode->i_ino;
 194         walk->file_cluster = f_clu;
 195         walk->disk_cluster = d_clu;
 196         last->next = NULL;
 197         walk->next = fat_cache;
 198         fat_cache = walk;
 199 #ifdef DEBUG
 200 list_cache();
 201 #endif
 202 }
 203 
 204 
 205 /* Cache invalidation occurs rarely, thus the LRU chain is not updated. It
 206    fixes itself after a while. */
 207 
 208 void cache_inval_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210         struct fat_cache *walk;
 211 
 212         for (walk = fat_cache; walk; walk = walk->next)
 213                 if (walk->device == inode->i_dev
 214                     && walk->ino == inode->i_ino)
 215                         walk->device = 0;
 216 }
 217 
 218 
 219 void cache_inval_dev(kdev_t device)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         struct fat_cache *walk;
 222 
 223         for (walk = fat_cache; walk; walk = walk->next)
 224                 if (walk->device == device)
 225                         walk->device = 0;
 226 }
 227 
 228 
 229 int get_cluster(struct inode *inode,int cluster)
     /* [previous][next][first][last][top][bottom][index][help] */
 230 {
 231         int nr,count;
 232 
 233         if (!(nr = MSDOS_I(inode)->i_start)) return 0;
 234         if (!cluster) return nr;
 235         count = 0;
 236         for (cache_lookup(inode,cluster,&count,&nr); count < cluster;
 237             count++) {
 238                 if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
 239                 if (!nr) return 0;
 240         }
 241         cache_add(inode,cluster,nr);
 242         return nr;
 243 }
 244 
 245 
 246 int msdos_smap(struct inode *inode,int sector)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {
 248         struct msdos_sb_info *sb;
 249         int cluster,offset;
 250 
 251         sb = MSDOS_SB(inode->i_sb);
 252         if (inode->i_ino == MSDOS_ROOT_INO || (S_ISDIR(inode->i_mode) &&
 253             !MSDOS_I(inode)->i_start)) {
 254                 if (sector >= sb->dir_entries >> MSDOS_DPS_BITS) return 0;
 255                 return sector+sb->dir_start;
 256         }
 257         cluster = sector/sb->cluster_size;
 258         offset = sector % sb->cluster_size;
 259         if (!(cluster = get_cluster(inode,cluster))) return 0;
 260         return (cluster-2)*sb->cluster_size+sb->data_start+offset;
 261 }
 262 
 263 
 264 /* Free all clusters after the skip'th cluster. Doesn't use the cache,
 265    because this way we get an additional sanity check. */
 266 
 267 int fat_free(struct inode *inode,int skip)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int nr,last;
 270 
 271         if (!(nr = MSDOS_I(inode)->i_start)) return 0;
 272         last = 0;
 273         while (skip--) {
 274                 last = nr;
 275                 if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
 276                 if (!nr) {
 277                         printk("fat_free: skipped EOF\n");
 278                         return -EIO;
 279                 }
 280         }
 281         if (last)
 282                 fat_access(inode->i_sb,last,MSDOS_SB(inode->i_sb)->fat_bits ==
 283                     12 ? 0xff8 : 0xfff8);
 284         else {
 285                 MSDOS_I(inode)->i_start = 0;
 286                 inode->i_dirt = 1;
 287         }
 288         lock_fat(inode->i_sb);
 289         while (nr != -1) {
 290                 if (!(nr = fat_access(inode->i_sb,nr,0))) {
 291                         fs_panic(inode->i_sb,"fat_free: deleting beyond EOF");
 292                         break;
 293                 }
 294                 if (MSDOS_SB(inode->i_sb)->free_clusters != -1)
 295                         MSDOS_SB(inode->i_sb)->free_clusters++;
 296                 inode->i_blocks -= MSDOS_SB(inode->i_sb)->cluster_size;
 297         }
 298         unlock_fat(inode->i_sb);
 299         cache_inval_inode(inode);
 300         return 0;
 301 }

/* [previous][next][first][last][top][bottom][index][help] */