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

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