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

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