root/fs/msdos/inode.c

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

DEFINITIONS

This source file includes following definitions.
  1. msdos_put_inode
  2. msdos_put_super
  3. parse_options
  4. msdos_read_super
  5. msdos_statfs
  6. msdos_bmap
  7. msdos_read_inode
  8. msdos_write_inode

   1 /*
   2  *  linux/fs/msdos/inode.c
   3  *
   4  *  Written 1992 by Werner Almesberger
   5  */
   6 
   7 #include <linux/msdos_fs.h>
   8 #include <linux/kernel.h>
   9 #include <linux/sched.h>
  10 #include <linux/errno.h>
  11 #include <linux/string.h>
  12 #include <linux/stat.h>
  13 
  14 #include <asm/segment.h>
  15 
  16 void msdos_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  17 {
  18         struct inode *depend;
  19 
  20         inode->i_size = 0;
  21         msdos_truncate(inode);
  22         depend = MSDOS_I(inode)->i_depend;
  23         memset(inode,0,sizeof(struct inode));
  24         if (depend) {
  25                 if (MSDOS_I(depend)->i_old != inode) {
  26                         printk("Invalid link (0x%X): expected 0x%X, got "
  27                             "0x%X\n",(int) depend,(int) inode,(int)
  28                             MSDOS_I(depend)->i_old);
  29                         panic("That's fatal");
  30                 }
  31                 MSDOS_I(depend)->i_old = NULL;
  32                 iput(depend);
  33         }
  34 }
  35 
  36 
  37 void msdos_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39         cache_inval_dev(sb->s_dev);
  40         lock_super(sb);
  41         sb->s_dev = 0;
  42         free_super(sb);
  43         return;
  44 }
  45 
  46 
  47 static struct super_operations msdos_sops = { 
  48         msdos_read_inode,
  49         msdos_write_inode,
  50         msdos_put_inode,
  51         msdos_put_super,
  52         NULL, /* added in 0.96c */
  53         msdos_statfs
  54 };
  55 
  56 
  57 static int parse_options(char *options,char *check,char *conversion)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         char *this,*value;
  60 
  61         *check = 'n';
  62         *conversion = 'b';
  63         if (!options) return 1;
  64         for (this = strtok(options,","); this; this = strtok(NULL,",")) {
  65                 if (value = strchr(this,'=')) *value++ = 0;
  66                 if (!strcmp(this,"check") && value) {
  67                         if (value[0] && !value[1] && strchr("rns",*value))
  68                                 *check = *value;
  69                         else if (!strcmp(value,"relaxed")) *check = 'r';
  70                         else if (!strcmp(value,"normal")) *check = 'n';
  71                         else if (!strcmp(value,"strict")) *check = 's';
  72                         else return 0;
  73                 }
  74                 else if (!strcmp(this,"conv") && value) {
  75                         if (value[0] && !value[1] && strchr("bta",*value))
  76                                 *conversion = *value;
  77                         else if (!strcmp(value,"binary")) *conversion = 'b';
  78                         else if (!strcmp(value,"text")) *conversion = 't';
  79                         else if (!strcmp(value,"auto")) *conversion = 'a';
  80                         else return 0;
  81                 }
  82                 else return 0;
  83         }
  84         return 1;
  85 }
  86 
  87 
  88 /* Read the super block of an MS-DOS FS. */
  89 
  90 struct super_block *msdos_read_super(struct super_block *s,void *data)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         struct buffer_head *bh;
  93         struct msdos_boot_sector *b;
  94         int data_sectors;
  95         char check,conversion;
  96 
  97         if (!parse_options((char *) data,&check,&conversion)) {
  98                 s->s_dev = 0;
  99                 return NULL;
 100         }
 101         cache_init();
 102         lock_super(s);
 103         bh = bread(s->s_dev, 0, BLOCK_SIZE);
 104         free_super(s);
 105         if (bh == NULL) {
 106                 s->s_dev = 0;
 107                 printk("MSDOS bread failed\n");
 108                 return NULL;
 109         }
 110         b = (struct msdos_boot_sector *) bh->b_data;
 111         s->s_blocksize = 1024;  /* we cannot handle anything else yet */
 112         MSDOS_SB(s)->cluster_size = b->cluster_size;
 113         MSDOS_SB(s)->fats = b->fats;
 114         MSDOS_SB(s)->fat_start = b->reserved;
 115         MSDOS_SB(s)->fat_length = b->fat_length;
 116         MSDOS_SB(s)->dir_start = b->reserved+b->fats*b->fat_length;
 117         MSDOS_SB(s)->dir_entries = *((unsigned short *) &b->dir_entries);
 118         MSDOS_SB(s)->data_start = MSDOS_SB(s)->dir_start+((MSDOS_SB(s)->
 119             dir_entries << 5) >> 9);
 120         data_sectors = (*((unsigned short *) &b->sectors) ? *((unsigned short *)
 121             &b->sectors) : b->total_sect)-MSDOS_SB(s)->data_start;
 122         MSDOS_SB(s)->clusters = b->cluster_size ? data_sectors/b->cluster_size :
 123             0;
 124         MSDOS_SB(s)->fat_bits = MSDOS_SB(s)->clusters > MSDOS_FAT12 ? 16 : 12;
 125         brelse(bh);
 126 printk("[MS-DOS FS Rel. alpha.8, FAT %d, check=%c, conv=%c]\n",
 127   MSDOS_SB(s)->fat_bits,check,conversion);
 128 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%d,ds=%d,de=%d,data=%d,se=%d,ts=%d]\n",
 129   b->media,MSDOS_SB(s)->cluster_size,MSDOS_SB(s)->fats,MSDOS_SB(s)->fat_start,
 130   MSDOS_SB(s)->fat_length,MSDOS_SB(s)->dir_start,MSDOS_SB(s)->dir_entries,
 131   MSDOS_SB(s)->data_start,*(unsigned short *) &b->sectors,b->total_sect);
 132         if (!MSDOS_SB(s)->fats || (MSDOS_SB(s)->dir_entries & (MSDOS_DPS-1))
 133             || !b->cluster_size || MSDOS_SB(s)->clusters+2 > MSDOS_SB(s)->
 134                 fat_length*SECTOR_SIZE*8/MSDOS_SB(s)->fat_bits) {
 135                 s->s_dev = 0;
 136                 printk("Unsupported FS parameters\n");
 137                 return NULL;
 138         }
 139         if (!MSDOS_CAN_BMAP(MSDOS_SB(s))) printk("No bmap support\n");
 140         s->s_magic = MSDOS_SUPER_MAGIC;
 141         MSDOS_SB(s)->name_check = check;
 142         MSDOS_SB(s)->conversion = conversion;
 143         /* set up enough so that it can read an inode */
 144         s->s_op = &msdos_sops;
 145         MSDOS_SB(s)->fs_uid = current->uid;
 146         MSDOS_SB(s)->fs_gid = current->gid;
 147         MSDOS_SB(s)->fs_umask = current->umask;
 148         MSDOS_SB(s)->free_clusters = -1; /* don't know yet */
 149         MSDOS_SB(s)->fat_wait = NULL;
 150         MSDOS_SB(s)->fat_lock = 0;
 151         if (!(s->s_mounted = iget(s->s_dev,MSDOS_ROOT_INO))) {
 152                 s->s_dev = 0;
 153                 printk("get root inode failed\n");
 154                 return NULL;
 155         }
 156         return s;
 157 }
 158 
 159 
 160 void msdos_statfs(struct super_block *sb,struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162         int free,this;
 163 
 164         put_fs_long(sb->s_magic,&buf->f_type);
 165         put_fs_long(MSDOS_SB(sb)->cluster_size*SECTOR_SIZE,&buf->f_bsize);
 166         put_fs_long(MSDOS_SB(sb)->clusters,&buf->f_blocks);
 167         lock_fat(sb);
 168         if (MSDOS_SB(sb)->free_clusters != -1)
 169                 free = MSDOS_SB(sb)->free_clusters;
 170         else {
 171                 free = 0;
 172                 for (this = 2; this < MSDOS_SB(sb)->clusters+2; this++)
 173                         if (!fat_access(sb,this,-1)) free++;
 174                 MSDOS_SB(sb)->free_clusters = free;
 175         }
 176         unlock_fat(sb);
 177         put_fs_long(free,&buf->f_bfree);
 178         put_fs_long(free,&buf->f_bavail);
 179         put_fs_long(0,&buf->f_files);
 180         put_fs_long(0,&buf->f_ffree);
 181 }
 182 
 183 
 184 int msdos_bmap(struct inode *inode,int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         struct msdos_sb_info *sb;
 187         int cluster,offset;
 188 
 189         sb = MSDOS_SB(inode->i_sb);
 190         if ((sb->cluster_size & 1) || (sb->data_start & 1)) return 0;
 191         if (inode->i_ino == MSDOS_ROOT_INO) {
 192                 if (sb->dir_start & 1) return 0;
 193                 return (sb->dir_start >> 1)+block;
 194         }
 195         cluster = (block*2)/sb->cluster_size;
 196         offset = (block*2) % sb->cluster_size;
 197         if (!(cluster = get_cluster(inode,cluster))) return 0;
 198         return ((cluster-2)*sb->cluster_size+sb->data_start+offset) >> 1;
 199 }
 200 
 201 
 202 void msdos_read_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 203 {
 204         struct buffer_head *bh;
 205         struct msdos_dir_entry *raw_entry;
 206         int this;
 207 
 208 /* printk("read inode %d\n",inode->i_ino); */
 209         MSDOS_I(inode)->i_busy = 0;
 210         MSDOS_I(inode)->i_depend = MSDOS_I(inode)->i_old = NULL;
 211         MSDOS_I(inode)->i_binary = 1;
 212         inode->i_uid = MSDOS_SB(inode->i_sb)->fs_uid;
 213         inode->i_gid = MSDOS_SB(inode->i_sb)->fs_gid;
 214         if (inode->i_ino == MSDOS_ROOT_INO) {
 215                 inode->i_mode = (0777 & ~MSDOS_SB(inode->i_sb)->fs_umask) |
 216                     S_IFDIR;
 217                 inode->i_op = &msdos_dir_inode_operations;
 218                 inode->i_nlink = msdos_subdirs(inode)+2;
 219                     /* subdirs (neither . nor ..) plus . and "self" */
 220                 inode->i_size = MSDOS_SB(inode->i_sb)->dir_entries*
 221                     sizeof(struct msdos_dir_entry);
 222                 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*
 223                     SECTOR_SIZE;
 224                 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
 225                     inode->i_blksize;
 226                 MSDOS_I(inode)->i_start = 0;
 227                 MSDOS_I(inode)->i_attrs = 0;
 228                 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
 229                 return;
 230         }
 231         if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS, BLOCK_SIZE)))
 232             panic("unable to read i-node block");
 233         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
 234             [inode->i_ino & (MSDOS_DPB-1)];
 235         if ((raw_entry->attr & ATTR_DIR) && *raw_entry->name && *(unsigned char *)
 236             raw_entry->name != DELETED_FLAG) {
 237                 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,0777 &
 238                     ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IFDIR;
 239                 inode->i_op = &msdos_dir_inode_operations;
 240                 MSDOS_I(inode)->i_start = raw_entry->start;
 241                 inode->i_nlink = msdos_subdirs(inode);
 242                     /* includes .., compensating for "self" */
 243 #ifdef DEBUG
 244                 if (!inode->i_nlink) {
 245                         printk("directory %d: i_nlink == 0\n",inode->i_ino);
 246                         inode->i_nlink = 1;
 247                 }
 248 #endif
 249                 inode->i_size = 0;
 250                 if (this = raw_entry->start)
 251                         while (this != -1) {
 252                                 inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
 253                                     i_sb)->cluster_size;
 254                                 if (!(this = fat_access(inode->i_sb,this,-1)))
 255                                         printk("Directory %d: bad FAT\n",
 256                                             inode->i_ino);
 257                         }
 258         }
 259         else {
 260                 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,0666 &
 261                     ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IFREG;
 262                 inode->i_op = MSDOS_CAN_BMAP(MSDOS_SB(inode->i_sb)) ? 
 263                     &msdos_file_inode_operations :
 264                     &msdos_file_inode_operations_no_bmap;
 265                 MSDOS_I(inode)->i_start = raw_entry->start;
 266                 inode->i_nlink = 1;
 267                 inode->i_size = raw_entry->size;
 268         }
 269         MSDOS_I(inode)->i_binary = is_binary(MSDOS_SB(inode->i_sb)->conversion,
 270             raw_entry->ext);
 271         MSDOS_I(inode)->i_attrs = raw_entry->attr & ATTR_UNUSED;
 272         /* this is as close to the truth as we can get ... */
 273         inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*SECTOR_SIZE;
 274         inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
 275             inode->i_blksize;
 276         inode->i_mtime = inode->i_atime = inode->i_ctime =
 277             date_dos2unix(raw_entry->time,raw_entry->date);
 278         brelse(bh);
 279 }
 280 
 281 
 282 void msdos_write_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284         struct buffer_head *bh;
 285         struct msdos_dir_entry *raw_entry;
 286 
 287         inode->i_dirt = 0;
 288         if (inode->i_ino == MSDOS_ROOT_INO || !inode->i_nlink) return;
 289         if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS, BLOCK_SIZE)))
 290             panic("unable to read i-node block");
 291         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
 292             [inode->i_ino & (MSDOS_DPB-1)];
 293         if (S_ISDIR(inode->i_mode)) {
 294                 raw_entry->attr = ATTR_DIR;
 295                 raw_entry->size = 0;
 296         }
 297         else {
 298                 raw_entry->attr = ATTR_NONE;
 299                 raw_entry->size = inode->i_size;
 300         }
 301         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
 302             MSDOS_I(inode)->i_attrs;
 303         raw_entry->start = MSDOS_I(inode)->i_start;
 304         date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
 305         bh->b_dirt = 1;
 306         brelse(bh);
 307 }

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