home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / fs / msdos / inode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  13.1 KB  |  448 lines

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