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

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