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

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