root/fs/fat/cache.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. fat_cache_inval_inode
  7. fat_cache_inval_dev
  8. get_cluster
  9. fat_smap
  10. fat_free

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

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