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

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