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
  9. msdos_notify_change

   1 /*
   2  *  linux/fs/msdos/inode.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/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 
  19 void msdos_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21         struct inode *depend;
  22         struct super_block *sb;
  23 
  24         if (inode->i_nlink) {
  25                 if (MSDOS_I(inode)->i_busy) cache_inval_inode(inode);
  26                 return;
  27         }
  28         inode->i_size = 0;
  29         msdos_truncate(inode);
  30         depend = MSDOS_I(inode)->i_depend;
  31         sb = inode->i_sb;
  32         clear_inode(inode);
  33         if (depend) {
  34                 if (MSDOS_I(depend)->i_old != inode) {
  35                         printk("Invalid link (0x%X): expected 0x%X, got 0x%X\n",
  36                             (int) depend,(int) inode,(int) MSDOS_I(depend)->
  37                             i_old);
  38                         fs_panic(sb,"...");
  39                         return;
  40                 }
  41                 MSDOS_I(depend)->i_old = NULL;
  42                 iput(depend);
  43         }
  44 }
  45 
  46 
  47 void msdos_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         cache_inval_dev(sb->s_dev);
  50         lock_super(sb);
  51         sb->s_dev = 0;
  52         unlock_super(sb);
  53         return;
  54 }
  55 
  56 
  57 static struct super_operations msdos_sops = { 
  58         msdos_read_inode,
  59         msdos_notify_change,
  60         msdos_write_inode,
  61         msdos_put_inode,
  62         msdos_put_super,
  63         NULL, /* added in 0.96c */
  64         msdos_statfs
  65 };
  66 
  67 
  68 static int parse_options(char *options,char *check,char *conversion,uid_t *uid,
     /* [previous][next][first][last][top][bottom][index][help] */
  69     gid_t *gid,int *umask,int *debug,int *fat)
  70 {
  71         char *this,*value;
  72 
  73         *check = 'n';
  74         *conversion = 'b';
  75         *uid = current->uid;
  76         *gid = current->gid;
  77         *umask = current->umask;
  78         *debug = *fat = 0;
  79         if (!options) return 1;
  80         for (this = strtok(options,","); this; this = strtok(NULL,",")) {
  81                 if ((value = strchr(this,'=')) != NULL)
  82                         *value++ = 0;
  83                 if (!strcmp(this,"check") && value) {
  84                         if (value[0] && !value[1] && strchr("rns",*value))
  85                                 *check = *value;
  86                         else if (!strcmp(value,"relaxed")) *check = 'r';
  87                         else if (!strcmp(value,"normal")) *check = 'n';
  88                         else if (!strcmp(value,"strict")) *check = 's';
  89                         else return 0;
  90                 }
  91                 else if (!strcmp(this,"conv") && value) {
  92                         if (value[0] && !value[1] && strchr("bta",*value))
  93                                 *conversion = *value;
  94                         else if (!strcmp(value,"binary")) *conversion = 'b';
  95                         else if (!strcmp(value,"text")) *conversion = 't';
  96                         else if (!strcmp(value,"auto")) *conversion = 'a';
  97                         else return 0;
  98                 }
  99                 else if (!strcmp(this,"uid")) {
 100                         if (!value || !*value)
 101                                 return 0;
 102                         *uid = simple_strtoul(value,&value,0);
 103                         if (*value)
 104                                 return 0;
 105                 }
 106                 else if (!strcmp(this,"gid")) {
 107                         if (!value || !*value)
 108                                 return 0;
 109                         *gid = simple_strtoul(value,&value,0);
 110                         if (*value)
 111                                 return 0;
 112                 }
 113                 else if (!strcmp(this,"umask")) {
 114                         if (!value || !*value)
 115                                 return 0;
 116                         *umask = simple_strtoul(value,&value,8);
 117                         if (*value)
 118                                 return 0;
 119                 }
 120                 else if (!strcmp(this,"debug")) {
 121                         if (value) return 0;
 122                         *debug = 1;
 123                 }
 124                 else if (!strcmp(this,"fat")) {
 125                         if (!value || !*value)
 126                                 return 0;
 127                         *fat = simple_strtoul(value,&value,0);
 128                         if (*value || (*fat != 12 && *fat != 16))
 129                                 return 0;
 130                 }
 131                 else return 0;
 132         }
 133         return 1;
 134 }
 135 
 136 
 137 /* Read the super block of an MS-DOS FS. */
 138 
 139 struct super_block *msdos_read_super(struct super_block *s,void *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         struct buffer_head *bh;
 142         struct msdos_boot_sector *b;
 143         int data_sectors,logical_sector_size,sector_mult;
 144         int debug,error,fat;
 145         char check,conversion;
 146         uid_t uid;
 147         gid_t gid;
 148         int umask;
 149 
 150         if (!parse_options((char *) data,&check,&conversion,&uid,&gid,&umask,
 151             &debug,&fat)) {
 152                 s->s_dev = 0;
 153                 return NULL;
 154         }
 155         cache_init();
 156         lock_super(s);
 157         bh = bread(s->s_dev, 0, BLOCK_SIZE);
 158         unlock_super(s);
 159         if (bh == NULL) {
 160                 s->s_dev = 0;
 161                 printk("MSDOS bread failed\n");
 162                 return NULL;
 163         }
 164         b = (struct msdos_boot_sector *) bh->b_data;
 165         s->s_blocksize = 1024;  /* we cannot handle anything else yet */
 166 
 167 /*
 168  * The DOS3 partition size limit is *not* 32M as many people think.  
 169  * Instead, it is 64K sectors (with the usual sector size being
 170  * 512 bytes, leading to a 32M limit).
 171  * 
 172  * DOS 3 partition managers got around this problem by faking a 
 173  * larger sector size, ie treating multiple physical sectors as 
 174  * a single logical sector.
 175  * 
 176  * We can accommodate this scheme by adjusting our cluster size,
 177  * fat_start, and data_start by an appropriate value.
 178  *
 179  * (by Drew Eckhardt)
 180  */
 181 
 182 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
 183     /* don't divide by zero */
 184 
 185         logical_sector_size = CF_LE_W(*(unsigned short *) &b->sector_size);
 186         sector_mult = logical_sector_size >> SECTOR_BITS;
 187         MSDOS_SB(s)->cluster_size = b->cluster_size*sector_mult;
 188         MSDOS_SB(s)->fats = b->fats;
 189         MSDOS_SB(s)->fat_start = CF_LE_W(b->reserved)*sector_mult;
 190         MSDOS_SB(s)->fat_length = CF_LE_W(b->fat_length)*sector_mult;
 191         MSDOS_SB(s)->dir_start = (CF_LE_W(b->reserved)+b->fats*CF_LE_W(
 192             b->fat_length))*sector_mult;
 193         MSDOS_SB(s)->dir_entries = CF_LE_W(*((unsigned short *) &b->dir_entries
 194             ));
 195         MSDOS_SB(s)->data_start = MSDOS_SB(s)->dir_start+ROUND_TO_MULTIPLE((
 196             MSDOS_SB(s)->dir_entries << MSDOS_DIR_BITS) >> SECTOR_BITS,
 197             sector_mult);
 198         data_sectors = (CF_LE_W(*((unsigned short *) &b->sectors)) ?
 199             CF_LE_W(*((unsigned short *) &b->sectors)) :
 200             CF_LE_L(b->total_sect))*sector_mult-MSDOS_SB(s)->data_start;
 201         MSDOS_SB(s)->clusters = b->cluster_size ? data_sectors/b->cluster_size/
 202             sector_mult : 0;
 203         MSDOS_SB(s)->fat_bits = fat ? fat : MSDOS_SB(s)->clusters > MSDOS_FAT12
 204             ? 16 : 12;
 205         error = !MSDOS_SB(s)->fats || (MSDOS_SB(s)->dir_entries & (MSDOS_DPS-1))
 206             || !b->cluster_size || MSDOS_SB(s)->clusters+2 > MSDOS_SB(s)->
 207             fat_length*SECTOR_SIZE*8/MSDOS_SB(s)->fat_bits || !sector_mult ||
 208             (logical_sector_size & (SECTOR_SIZE-1)) || !b->secs_track ||
 209             !b->heads;
 210         brelse(bh);
 211         if (error || debug) {
 212                 printk("[MS-DOS FS Rel. alpha.10,FAT %d,check=%c,conv=%c,"
 213                     "uid=%d,gid=%d,umask=%03o%s]\n",MSDOS_SB(s)->fat_bits,check,
 214                     conversion,uid,gid,umask,MSDOS_CAN_BMAP(MSDOS_SB(s)) ? ",
 215                     bmap" : "");
 216                 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%d,ds=%d,de=%d,data=%d,"
 217                     "se=%d,ts=%d,ls=%d]\n",b->media,MSDOS_SB(s)->cluster_size,
 218                     MSDOS_SB(s)->fats,MSDOS_SB(s)->fat_start,MSDOS_SB(s)->
 219                     fat_length,MSDOS_SB(s)->dir_start,MSDOS_SB(s)->dir_entries,
 220                     MSDOS_SB(s)->data_start,CF_LE_W(*(unsigned short *) &b->
 221                     sectors),b->total_sect,logical_sector_size);
 222         }
 223         if (error) {
 224                 s->s_dev = 0;
 225                 printk("Unsupported FS parameters\n");
 226                 return NULL;
 227         }
 228         s->s_magic = MSDOS_SUPER_MAGIC;
 229         MSDOS_SB(s)->name_check = check;
 230         MSDOS_SB(s)->conversion = conversion;
 231         /* set up enough so that it can read an inode */
 232         s->s_op = &msdos_sops;
 233         MSDOS_SB(s)->fs_uid = uid;
 234         MSDOS_SB(s)->fs_gid = gid;
 235         MSDOS_SB(s)->fs_umask = umask;
 236         MSDOS_SB(s)->free_clusters = -1; /* don't know yet */
 237         MSDOS_SB(s)->fat_wait = NULL;
 238         MSDOS_SB(s)->fat_lock = 0;
 239         MSDOS_SB(s)->prev_free = 0;
 240         if (!(s->s_mounted = iget(s,MSDOS_ROOT_INO))) {
 241                 s->s_dev = 0;
 242                 printk("get root inode failed\n");
 243                 return NULL;
 244         }
 245         return s;
 246 }
 247 
 248 
 249 void msdos_statfs(struct super_block *sb,struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         int free,this;
 252 
 253         put_fs_long(sb->s_magic,&buf->f_type);
 254         put_fs_long(MSDOS_SB(sb)->cluster_size*SECTOR_SIZE,&buf->f_bsize);
 255         put_fs_long(MSDOS_SB(sb)->clusters,&buf->f_blocks);
 256         lock_fat(sb);
 257         if (MSDOS_SB(sb)->free_clusters != -1)
 258                 free = MSDOS_SB(sb)->free_clusters;
 259         else {
 260                 free = 0;
 261                 for (this = 2; this < MSDOS_SB(sb)->clusters+2; this++)
 262                         if (!fat_access(sb,this,-1)) free++;
 263                 MSDOS_SB(sb)->free_clusters = free;
 264         }
 265         unlock_fat(sb);
 266         put_fs_long(free,&buf->f_bfree);
 267         put_fs_long(free,&buf->f_bavail);
 268         put_fs_long(0,&buf->f_files);
 269         put_fs_long(0,&buf->f_ffree);
 270 }
 271 
 272 
 273 int msdos_bmap(struct inode *inode,int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 274 {
 275         struct msdos_sb_info *sb;
 276         int cluster,offset;
 277 
 278         sb = MSDOS_SB(inode->i_sb);
 279         if ((sb->cluster_size & 1) || (sb->data_start & 1)) return 0;
 280         if (inode->i_ino == MSDOS_ROOT_INO) {
 281                 if (sb->dir_start & 1) return 0;
 282                 return (sb->dir_start >> 1)+block;
 283         }
 284         cluster = (block*2)/sb->cluster_size;
 285         offset = (block*2) % sb->cluster_size;
 286         if (!(cluster = get_cluster(inode,cluster))) return 0;
 287         return ((cluster-2)*sb->cluster_size+sb->data_start+offset) >> 1;
 288 }
 289 
 290 
 291 void msdos_read_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293         struct buffer_head *bh;
 294         struct msdos_dir_entry *raw_entry;
 295         int this;
 296 
 297 /* printk("read inode %d\n",inode->i_ino); */
 298         MSDOS_I(inode)->i_busy = 0;
 299         MSDOS_I(inode)->i_depend = MSDOS_I(inode)->i_old = NULL;
 300         MSDOS_I(inode)->i_binary = 1;
 301         inode->i_uid = MSDOS_SB(inode->i_sb)->fs_uid;
 302         inode->i_gid = MSDOS_SB(inode->i_sb)->fs_gid;
 303         if (inode->i_ino == MSDOS_ROOT_INO) {
 304                 inode->i_mode = (0777 & ~MSDOS_SB(inode->i_sb)->fs_umask) |
 305                     S_IFDIR;
 306                 inode->i_op = &msdos_dir_inode_operations;
 307                 inode->i_nlink = msdos_subdirs(inode)+2;
 308                     /* subdirs (neither . nor ..) plus . and "self" */
 309                 inode->i_size = MSDOS_SB(inode->i_sb)->dir_entries*
 310                     sizeof(struct msdos_dir_entry);
 311                 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*
 312                     SECTOR_SIZE;
 313                 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
 314                     inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
 315                 MSDOS_I(inode)->i_start = 0;
 316                 MSDOS_I(inode)->i_attrs = 0;
 317                 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
 318                 return;
 319         }
 320         if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
 321             BLOCK_SIZE))) {
 322                 printk("dev = 0x%04X, ino = %d\n",inode->i_dev,inode->i_ino);
 323                 panic("msdos_read_inode: unable to read i-node block");
 324         }
 325         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
 326             [inode->i_ino & (MSDOS_DPB-1)];
 327         if ((raw_entry->attr & ATTR_DIR) && !IS_FREE(raw_entry->name)) {
 328                 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,0777 &
 329                     ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IFDIR;
 330                 inode->i_op = &msdos_dir_inode_operations;
 331                 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
 332                 inode->i_nlink = msdos_subdirs(inode);
 333                     /* includes .., compensating for "self" */
 334 #ifdef DEBUG
 335                 if (!inode->i_nlink) {
 336                         printk("directory %d: i_nlink == 0\n",inode->i_ino);
 337                         inode->i_nlink = 1;
 338                 }
 339 #endif
 340                 inode->i_size = 0;
 341                 if ((this = CF_LE_W(raw_entry->start)) != 0)
 342                         while (this != -1) {
 343                                 inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
 344                                     i_sb)->cluster_size;
 345                                 if (!(this = fat_access(inode->i_sb,this,-1)))
 346                                         printk("Directory %d: bad FAT\n",
 347                                             inode->i_ino);
 348                         }
 349         }
 350         else {
 351                 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,(IS_NOEXEC(inode)
 352                     ? 0666 : 0777) & ~MSDOS_SB(inode->i_sb)->fs_umask) |
 353                     S_IFREG;
 354                 inode->i_op = MSDOS_CAN_BMAP(MSDOS_SB(inode->i_sb)) ? 
 355                     &msdos_file_inode_operations :
 356                     &msdos_file_inode_operations_no_bmap;
 357                 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
 358                 inode->i_nlink = 1;
 359                 inode->i_size = CF_LE_L(raw_entry->size);
 360         }
 361         MSDOS_I(inode)->i_binary = is_binary(MSDOS_SB(inode->i_sb)->conversion,
 362             raw_entry->ext);
 363         MSDOS_I(inode)->i_attrs = raw_entry->attr & ATTR_UNUSED;
 364         /* this is as close to the truth as we can get ... */
 365         inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*SECTOR_SIZE;
 366         inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
 367             inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
 368         inode->i_mtime = inode->i_atime = inode->i_ctime =
 369             date_dos2unix(CF_LE_W(raw_entry->time),CF_LE_W(raw_entry->date));
 370         brelse(bh);
 371 }
 372 
 373 
 374 void msdos_write_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 375 {
 376         struct buffer_head *bh;
 377         struct msdos_dir_entry *raw_entry;
 378 
 379         inode->i_dirt = 0;
 380         if (inode->i_ino == MSDOS_ROOT_INO || !inode->i_nlink) return;
 381         if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
 382             BLOCK_SIZE))) {
 383                 printk("dev = 0x%04X, ino = %d\n",inode->i_dev,inode->i_ino);
 384                 panic("msdos_write_inode: unable to read i-node block");
 385         }
 386         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
 387             [inode->i_ino & (MSDOS_DPB-1)];
 388         if (S_ISDIR(inode->i_mode)) {
 389                 raw_entry->attr = ATTR_DIR;
 390                 raw_entry->size = 0;
 391         }
 392         else {
 393                 raw_entry->attr = ATTR_NONE;
 394                 raw_entry->size = CT_LE_L(inode->i_size);
 395         }
 396         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
 397             MSDOS_I(inode)->i_attrs;
 398         raw_entry->start = CT_LE_L(MSDOS_I(inode)->i_start);
 399         date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
 400         raw_entry->time = CT_LE_W(raw_entry->time);
 401         raw_entry->date = CT_LE_W(raw_entry->date);
 402         bh->b_dirt = 1;
 403         brelse(bh);
 404 }
 405 
 406 
 407 int msdos_notify_change(int flags,struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409         int error;
 410 
 411         error = 0;
 412         if ((flags & NOTIFY_UIDGID) && (inode->i_uid != MSDOS_SB(inode->i_sb)->
 413             fs_uid || inode->i_gid != MSDOS_SB(inode->i_sb)->fs_gid)) {
 414                 inode->i_uid = MSDOS_SB(inode->i_sb)->fs_uid;
 415                 inode->i_gid = MSDOS_SB(inode->i_sb)->fs_gid;
 416                 error = -EPERM;
 417         }
 418         if (!(flags & NOTIFY_MODE))
 419                 return error;
 420         if (inode->i_mode & ~MSDOS_VALID_MODE) {
 421                 inode->i_mode &= MSDOS_VALID_MODE;
 422                 error = -EPERM;
 423         }
 424         if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
 425                 inode->i_mode &= S_IFMT | 0666;
 426         else inode->i_mode |= 0111;
 427         inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
 428             & ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IRUSR) >> 6)*0111)) &
 429             ~MSDOS_SB(inode->i_sb)->fs_umask;
 430         return error;
 431 }

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