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

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