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
  10. init_module
  11. cleanup_module

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

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