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

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