Chinaunix首页 | 论坛 | 博客
  • 博客访问: 192281
  • 博文数量: 45
  • 博客积分: 1577
  • 博客等级: 上尉
  • 技术积分: 476
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-01 16:40
个人简介

xxx

文章分类

全部博文(45)

文章存档

2012年(4)

2011年(14)

2010年(8)

2009年(19)

我的朋友

分类:

2011-06-13 23:32:17

本文参照《操作系统:设计与实现》,文章中很多语句与图表来自此书。
在POSIX中,OPEN调用可以用于创建一个新文件和截断一个老文件,因此CREAT调用实现的是OPEN的一个子集。MINIX中CREAT和OPEN的过程分别是do_creat()和do_open()来实现,这些函数位于/src/fs/open.c中。
打开或创建一个文件包括三步:
1)找到i-节点(创建文件时需要进行分配和初始化)
2)找到或创建目录项
3)为文件建立并返回一个描述符
这两个函数都做两件事:获取文件名,并调用common_open()。
此函数如下所示:
PRIVATE int common_open(oflags, omode)
register int oflags;
mode_t omode;
{
/* Common code from do_creat and do_open. */
  register struct inode *rip;
  int r, b, major, task, exist = TRUE;
  dev_t dev;
  mode_t bits;
  off_t pos;
  struct filp *fil_ptr, *filp2;
  /* Remap the bottom two bits of oflags. */
  bits = (mode_t) mode_map[oflags & O_ACCMODE];
  /* See if file descriptor and filp slots are available. 是否有可用的文件描述符*/
  if ( (r = get_fd(0, bits, &fd, &fil_ptr)) != OK) return(r);
  /* If O_CREATE is set, try to make the file. */
  if (oflags & O_CREAT) {
   /* Create a new inode by calling new_node(). 为文件创建i节点,i节点唯一的标识和描述着一个文件,这是最重要的一步*/
        omode = I_REGULAR | (omode & ALL_MODES & fp->fp_umask);
     rip = new_node(user_path, omode, NO_ZONE);
     r = err_code;
     if (r == OK) exist = FALSE;      /* we just created the file */
 else if (r != EEXIST) return(r); /* other error */
 else exist = !(oflags & O_EXCL); /* file exists, if the O_EXCL
         flag is set this is an error */
  } else {
  /* Scan path name. */
     if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
  }
  /* Claim the file descriptor and filp slot and fill them in.
   * 设置文件描述符,我们知道一个进程在minix中有三部分,分别是:struct proc ,
   * struct  fproc和struct mproc。这三者共同描述了一个进程,第一个主要描述了CPU的
   * 寄存器栈帧结构,第二个描述了其所拥有的文件的相关信息,最后一个描述了其所用有的
   * 内存信息。此处的struct  fproc中的*fp_file[OPEN_MAX]数组则是其文件描述符指针表,
   *其标记* 了文件所拥有的文件描述符,此表中的文件描述符指针指向系统唯一的文件描述符数
   *组: filp[NR_FILPS]其中NR_FILPS为128。
   */
  fp->fp_filp[fd] = fil_ptr;
  fil_ptr->filp_count = 1;
  fil_ptr->filp_ino = rip;
  fil_ptr->filp_flags = oflags;
  /* Only do the normal open code if we didn't just create the file. */
  if (exist) {
   /* Check protections. 接下来就是检查文件的类型、模式等*/
   if ((r = forbidden(rip, bits)) == OK) {
    /* Opening reg. files directories and special files differ. */
    switch (rip->i_mode & I_TYPE) {
         case I_REGULAR:
   /* Truncate regular file if O_TRUNC. */
   if (oflags & O_TRUNC) {
    if ((r = forbidden(rip, W_BIT)) !=OK) break;
    truncate(rip);
    wipe_inode(rip);
    /* Send the inode from the inode cache to the
     * block cache, so it gets written on the next
     * cache flush.
     */
    rw_inode(rip, WRITING);
   }
   break;
 
         case I_DIRECTORY:
   /* Directories may be read but not written. */
   r = (bits & W_BIT ? EISDIR : OK);
   break;
          case I_CHAR_SPECIAL:
          case I_BLOCK_SPECIAL:
   /* Invoke the driver for special processing. */
   dev_mess.m_type = DEV_OPEN;
   dev = (dev_t) rip->i_zone[0];
   dev_mess.DEVICE = dev;
   dev_mess.COUNT = bits | (oflags & ~O_ACCMODE);
   major = (dev >> MAJOR) & BYTE; /* major device nr */
   if (major <= 0 || major >= max_major) {
    r = ENODEV;
    break;
   }
   task = dmap[major].dmap_task; /* device task nr */
   (*dmap[major].dmap_open)(task, &dev_mess);
   r = dev_mess.REP_STATUS;
   break;
     case I_NAMED_PIPE:
   oflags |= O_APPEND; /* force append mode */
   fil_ptr->filp_flags = oflags;
   r = pipe_open(rip, bits, oflags);
   if (r == OK) {
    /* See if someone else is doing a rd or wt on
     * the FIFO.  If so, use its filp entry so the
     * file position will be automatically shared.
     */
    b = (bits & R_BIT ? R_BIT : W_BIT);
    fil_ptr->filp_count = 0; /* don't find self */
    if ((filp2 = find_filp(rip, b)) != NIL_FILP) {
     /* Co-reader or writer found. Use it.*/
     fp->fp_filp[fd] = filp2;
     filp2->filp_count++;
     filp2->filp_ino = rip;
     filp2->filp_flags = oflags;
     /* i_count was incremented incorrectly
      * by eatpath above, not knowing that
      * we were going to use an existing
      * filp entry.  Correct this error.
      */
     rip->i_count--;
    } else {
     /* Nobody else found.  Restore filp. */
     fil_ptr->filp_count = 1;
     if (b == R_BIT)
          pos = rip->i_zone[V2_NR_DZONES+1];
     else
          pos = rip->i_zone[V2_NR_DZONES+2];
     fil_ptr->filp_pos = pos;
    }
   }
   break;
   }
   }
  }
  /* If error, release inode. */
  if (r != OK) {
 fp->fp_filp[fd] = NIL_FILP;
 fil_ptr->filp_count= 0;
 put_inode(rip);
 return(r);
  }
  /*返回创建文件的文件描述符,此文件描述符为,fproc[]中的数组*fp_filp[OPEN_MAX]的某个索引
  * 此处的OPEN_MAX位20.即一个进程可以拥有20个文件描述符
  */
  return(fd);
}
 
 
 
此处的process table 实为fproc[]数组。通过该结构体中的*fp_filp[OPEN_MAX]数组指针来指向
filp[NR_FILPS]文件描述符数组,最终指向具体的i节点,此处的i节点是在内存的i节点数组
inode[NR_INODES]中。最终创建的新文件的i节点设置好后会立刻写入到磁盘中去。所以创建文件的主要操作就是在磁盘中为其创建i节点。因为一个磁盘中的i节点才唯一的描述一个文件或目录。下面来看看创建i节点函数:new_node()
此函数负责分配i-节点,并为CREAT和OPEN把路径名加到文件系统中。例如我们创建一个函数调用如下操作:
fd=creat("/usr/ast/foobar",0755);
 
PRIVATE struct inode *new_node(path, bits, z0)
char *path;   /* pointer to path name */
mode_t bits;   /* mode of the new inode */
zone_t z0;   /* zone number 0 for new inode */
{
/* New_node() is called by common_open(), do_mknod(), and do_mkdir(). 
 * In all cases it allocates a new inode, makes a directory entry for it on
 * the path 'path', and initializes it.  It returns a pointer to the inode if
 * it can do this; otherwise it returns NIL_INODE.  It always sets 'err_code'
 * to an appropriate value (OK or an error code).
 */
  register struct inode *rlast_dir_ptr, *rip;
  register int r;
  char string[NAME_MAX];
  /* See if the path can be opened down to the last directory.
   * last_dir()函数,逐步对上述目录/usr/ast/foobar进行迭代,过程是这样的,先从内存中得到
   * 根目录/的i节点或当前目录的i节点,因为有创建文件时会用相对目录即进程当前运行目录
   * 如:../ast/foobar进行创建。此处是得到根目录/的i节点,不管是目录文件还是普通文件,都是
   * 用i节点来唯一标示,通过i节点才能找到其真正的数据块.last_dir()通过get_name()函数,将
   * 字符串/usr/ast/foobar分解为/ast/foobar和usr,然后通过advance()函数的到根目录的数据块
   *,我们知道目录文件的数据块中存储的是目录,然后在此数据块中找到字符串为usr的目录项,找到了
   * 该目录项我们就知道了其对应的i节点号,然后加载其i节点,继续通过get_name()将字符串分解为
   * /foobar和ast。重复上一过程,加载usr目录的数据块,从而得到ast目录的i节点号,最后一次调用
   * get_name()函数分解/foobar,函数会碰到字符串结束符'\0'.从而last_dir()的主要工作完成,
   * 即得到了将要创建的/foobar目录的上一目录ast的i节点。
   */
  if ((rlast_dir_ptr = last_dir(path, string)) == NIL_INODE) return(NIL_INODE);
  /* The final directory is accessible. Get final component of the path. */
  rip = advance(rlast_dir_ptr, string);
  if ( rip == NIL_INODE && err_code == ENOENT) {
 /* Last path component does not exist.  Make new directory entry. */
 if ( (rip = alloc_inode(rlast_dir_ptr->i_dev, bits)) == NIL_INODE) {
  /* Can't creat new inode: out of inodes. */
  put_inode(rlast_dir_ptr);
  return(NIL_INODE);
 }
 /* Force inode to the disk before making directory entry to make
  * the system more robust in the face of a crash: an inode with
  * no directory entry is much better than the opposite.
  */
 rip->i_nlinks++;
 rip->i_zone[0] = z0;  /* major/minor device numbers */
 rw_inode(rip, WRITING);  /* force inode to disk now */
 /* New inode acquired.  Try to make directory entry. */
 if ((r = search_dir(rlast_dir_ptr, string, &rip->i_num,ENTER)) != OK) {
  put_inode(rlast_dir_ptr);
  rip->i_nlinks--; /* pity, have to free disk inode */
  rip->i_dirt = DIRTY; /* dirty inodes are written out */
  put_inode(rip); /* this call frees the inode */
  err_code = r;
  return(NIL_INODE);
 }
  } else {
 /* Either last component exists, or there is some problem. */
 if (rip != NIL_INODE)
  r = EEXIST;
 else
  r = err_code;
  }
  /* Return the directory inode and exit. */
  put_inode(rlast_dir_ptr);
  err_code = r;
  return(rip);
}
目录如下所示:
 
last_dir函数代码如下所示:
PUBLIC struct inode *last_dir(path, string)
char *path;   /* the path name to be parsed */
char string[NAME_MAX];  /* the final component is returned here */
{
/* Given a path, 'path', located in the fs address space, parse it as
 * far as the last directory, fetch the inode for the last directory into
 * the inode table, and return a pointer to the inode.
  In
 * addition, return the final component of the path in 'string'.
 * If the last directory can't be opened, return NIL_INODE and
 * the reason for failure in 'err_code'.
 */
  register struct inode *rip;
  register char *new_name;
  register struct inode *new_ip;
  /* Is the path absolute or relative?  Initialize 'rip' accordingly. ,得到根目录或
   * 当前目录的i节点。*/
  rip = (*path == '/' ? fp->fp_rootdir : fp->fp_workdir);
  /* If dir has been removed or path is empty, return ENOENT. */
  if (rip->i_nlinks == 0 || *path == '\0') {
 err_code = ENOENT;
 return(NIL_INODE);
  }
  dup_inode(rip);  /* inode will be returned with put_inode ,增加i节点的引用索引*/
  /* Scan the path component by component. */
  while (TRUE) {
 /* Extract one component. 逐步从头至尾解析路径*/
 if ( (new_name = get_name(path, string)) == (char*) 0) {
  put_inode(rip); /* bad path in user space */
  return(NIL_INODE);
 }
 if (*new_name == '\0')/*路径解析完成,指向字符数组的末尾*/
  if ( (rip->i_mode & I_TYPE) == I_DIRECTORY)
   return(rip); /* normal exit ,返回/usr/ast路径的i节点,即目录ast的i节点*/
  else {
   /* last file of path prefix is not a directory */
   put_inode(rip);
   err_code = ENOTDIR;   
   return(NIL_INODE);
  }
 /* There is more path.  Keep parsing,在当前i节点中查找string字符串所指向路径的i节点
 */
 new_ip = advance(rip, string);
 put_inode(rip);  /* rip either obsolete or irrelevant */
 if (new_ip == NIL_INODE) return(NIL_INODE);
 /* The call to advance() succeeded.  Fetch next component. */
 path = new_name;
 rip = new_ip;
  }
}
get_name()函数如下所示:
PRIVATE char *get_name(old_name, string)
char *old_name;   /* path name to parse */
char string[NAME_MAX];  /* component extracted from 'old_name' */
{
/* Given a pointer to a path name in fs space, 'old_name', copy the next
 * component to 'string' and pad with zeros.  A pointer to that part of
 * the name as yet unparsed is returned.  Roughly speaking,
 * 'get_name' = 'old_name' - 'string'.
 *
 * This routine follows the standard convention that /usr/ast, /usr//ast,
 * //usr///ast and /usr/ast/ are all equivalent.
 * 解析路径,比如字符串“/usr/ast/foobar”,将解析如下/ast/foobar和字符串usr,usr字符串
 * 保存在string字符数组中。
 */
  register int c;
  register char *np, *rnp;
  np = string;   /* 'np' points to current position */
  rnp = old_name;  /* 'rnp' points to unparsed string */
  while ( (c = *rnp) == '/') rnp++; /* skip leading slashes */
  /* Copy the unparsed path, 'old_name', to the array, 'string'. */
  while ( rnp < &old_name[PATH_MAX]  &&  c != '/'   &&  c != '\0') {
 if (np < &string[NAME_MAX]) *np++ = c;
 c = *++rnp;  /* advance to next character */
  }
  /* To make /usr/ast/ equivalent to /usr/ast, skip trailing slashes. */
  while (c == '/' && rnp < &old_name[PATH_MAX]) c = *++rnp;
  if (np < &string[NAME_MAX]) *np = '\0'; /* Terminate string */
  if (rnp >= &old_name[PATH_MAX]) {
 err_code = ENAMETOOLONG;
 return((char *) 0);
  }
  return(rnp);/*此处get_name()函数会被迭代调用直到将路径全部解析完成,如果正常解析,那么当路
               *径全部解析完后,rnp会指向old_name[]数组的最后一个字符,即'\0'
               */
}
 
advance()函数在给定的i节点中查找某一目录,若查找到该目录则返回其i节点。该函数如下所示:
 
PUBLIC struct inode *advance(dirp, string)
struct inode *dirp;  /* inode for directory to be searched */
char string[NAME_MAX];  /* component name to look for */
{
/* Given a directory and a component of a path, look up the component in
 * the directory, find the inode, open it, and return a pointer to its inode
 * slot.  If it can't be done, return NIL_INODE.
 */
  register struct inode *rip;
  struct inode *rip2;
  register struct super_block *sp;
  int r, inumb;
  dev_t mnt_dev;
  ino_t numb;
  /* If 'string' is empty, yield same inode straight away. */
  if (string[0] == '\0') return(get_inode(dirp->i_dev, (int) dirp->i_num));
  /* Check for NIL_INODE. */
  if (dirp == NIL_INODE) return(NIL_INODE);
  /* If 'string' is not present in the directory, signal error.
   *在i节点中查找string所指向的目录,如找到,numb则返回其i节点号。
   */
  if ( (r = search_dir(dirp, string, &numb, LOOK_UP)) != OK) {
 err_code = r;
 return(NIL_INODE);
  }
  /* Don't go beyond the current root directory, unless the string is dot2. */
  if (dirp == fp->fp_rootdir && strcmp(string, "..") == 0 && string != dot2)
  return(get_inode(dirp->i_dev, (int) dirp->i_num));
  /* The component has been found in the directory.  Get inode. 取得对应
   * i节点号的i节点。*/
  if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NIL_INODE)
        return(NIL_INODE);
  if (rip->i_num == ROOT_INODE)
 if (dirp->i_num == ROOT_INODE) {
     if (string[1] == '.') {
  for (sp = &super_block[1]; sp < &super_block[NR_SUPERS]; sp++){
   if (sp->s_dev == rip->i_dev) {
    /* Release the root inode.  Replace by the
     * inode mounted on.
     */
    put_inode(rip);
    mnt_dev = sp->s_imount->i_dev;
    inumb = (int) sp->s_imount->i_num;
    rip2 = get_inode(mnt_dev, inumb);
    rip = advance(rip2, string);
    put_inode(rip2);
    break;
   }
  }
     }
 }
  if (rip == NIL_INODE) return(NIL_INODE);
  /* See if the inode is mounted on.  If so, switch to root directory of the
   * mounted file system.  The super_block provides the linkage between the
   * inode mounted on and the root directory of the mounted file system.
   */
  while (rip != NIL_INODE && rip->i_mount == I_MOUNT) {
 /* The inode is indeed mounted on. */
 for (sp = &super_block[0]; sp < &super_block[NR_SUPERS]; sp++) {
  if (sp->s_imount == rip) {
   /* Release the inode mounted on.  Replace by the
    * inode of the root inode of the mounted device.
    */
   put_inode(rip);
   rip = get_inode(sp->s_dev, ROOT_INODE);
   break;
  }
 }
  }
  return(rip);  /* return pointer to inode's component */
}
在某一i节点所代表的目录中查找某一目录,是通过调用search_dir()来完成的。该函数如下所示:
PUBLIC int search_dir(ldir_ptr, string, numb, flag)
register struct inode *ldir_ptr; /* ptr to inode for dir to search */
char string[NAME_MAX];  /* component to search for */
ino_t *numb;   /* pointer to inode number */
int flag;   /* LOOK_UP, ENTER, DELETE or IS_EMPTY */
{
/* This function searches the directory whose inode is pointed to by 'ldip':
 * if (flag == ENTER)  enter 'string' in the directory with inode # '*numb';
 * if (flag == DELETE) delete 'string' from the directory;
 * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
 * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
 *
 *    if 'string' is dot1 or dot2, no access permissions are checked.
 */
  register struct direct *dp;
  register struct buf *bp;
  int i, r, e_hit, t, match;
  mode_t bits;
  off_t pos;
  unsigned new_slots, old_slots;
  block_t b;
  struct super_block *sp;
  int extended = 0;
  /* If 'ldir_ptr' is not a pointer to a dir inode, error. */
  if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY) return(ENOTDIR);
  r = OK;
  if (flag != IS_EMPTY) {
 bits = (flag == LOOK_UP ? X_BIT : W_BIT | X_BIT);
 if (string == dot1 || string == dot2) {
  if (flag != LOOK_UP) r = read_only(ldir_ptr);
         /* only a writable device is required. */
        }
 else r = forbidden(ldir_ptr, bits); /* check access permissions */
  }
  if (r != OK) return(r);
 
  /* Step through the directory one block at a time. */
  old_slots = (unsigned) (ldir_ptr->i_size/DIR_ENTRY_SIZE);
  new_slots = 0;
  e_hit = FALSE;
  match = 0;   /* set when a string match occurs */
  /*read_map()函数以i节点为基础,根据文件的偏移来确定对应偏移的数据块块号,此处的i节点是
   *指向目录文件的i节点,找到对应偏移的数据块号后,返回其块号。
   */
  for (pos = 0; pos < ldir_ptr->i_size; pos += BLOCK_SIZE) {
 b = read_map(ldir_ptr, pos); /* get block number */
 /* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
 bp = get_block(ldir_ptr->i_dev, b, NORMAL); /* get a dir block 加载该数据块*/
 /* Search a directory block. */
 for (dp = &bp->b_dir[0]; dp < &bp->b_dir[NR_DIR_ENTRIES]; dp++) {
  if (++new_slots > old_slots) { /* not found, but room left */
   if (flag == ENTER) e_hit = TRUE;
   break;
  }
  /* Match occurs if string found. */
  if (flag != ENTER && dp->d_ino != 0) {
   if (flag == IS_EMPTY) {
    /* If this test succeeds, dir is not empty. */
    if (strcmp(dp->d_name, "." ) != 0 &&
        strcmp(dp->d_name, "..") != 0) match = 1;
   } else {/*在该数据块中查找string字符串对应的目录,找到则match置1,dp指向该目录项*/
    if (strncmp(dp->d_name, string, NAME_MAX) == 0)
     match = 1;
   }
  }
  if (match) {
   /* LOOK_UP or DELETE found what it wanted. */
   r = OK;
   if (flag == IS_EMPTY) r = ENOTEMPTY;
   else if (flag == DELETE) {
    /* Save d_ino for recovery. */
    t = NAME_MAX - sizeof(ino_t);
    *((ino_t *) &dp->d_name[t]) = dp->d_ino;
    dp->d_ino = 0; /* erase entry */
    bp->b_dirt = DIRTY;
    ldir_ptr->i_update |= CTIME | MTIME;
    ldir_ptr->i_dirt = DIRTY;
   } else {
    /*通过conv2()函数判断是否需要大小端互换,*numb中最终存储了找到的目录的i节点号
     *将以指针参数的形式返回*/
    sp = ldir_ptr->i_sp; /* 'flag' is LOOK_UP */
    *numb = conv2(sp->s_native, (int) dp->d_ino);
   }
   put_block(bp, DIRECTORY_BLOCK);
   return(r);
  }

  /* Check for free slot for the benefit of ENTER. */
  if (flag == ENTER && dp->d_ino == 0) {
   e_hit = TRUE; /* we found a free slot */
   break;
  }
 }
 /* The whole block has been searched or ENTER has a free slot. */
 if (e_hit) break; /* e_hit set if ENTER can be performed now */
 put_block(bp, DIRECTORY_BLOCK); /* otherwise, continue searching dir */
  }
  /* The whole directory has now been searched. */
  if (flag != ENTER) return(flag == IS_EMPTY ? OK : ENOENT);
  /* This call is for ENTER.  If no free slot has been found so far, try to
   * extend directory.
   */
  if (e_hit == FALSE) { /* directory is full and no room left in last block */
 new_slots++;  /* increase directory size by 1 entry */
 if (new_slots == 0) return(EFBIG); /* dir size limited by slot count */
 if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NIL_BUF)
  return(err_code);
 dp = &bp->b_dir[0];
 extended = 1;
  }
  /* 'bp' now points to a directory block with space. 'dp' points to slot. */
  (void) memset(dp->d_name, 0, (size_t) NAME_MAX); /* clear entry */
  for (i = 0; string[i] && i < NAME_MAX; i++) dp->d_name[i] = string[i];
  sp = ldir_ptr->i_sp;
  dp->d_ino = conv2(sp->s_native, (int) *numb);
  bp->b_dirt = DIRTY;
  put_block(bp, DIRECTORY_BLOCK);
  ldir_ptr->i_update |= CTIME | MTIME; /* mark mtime for update later */
  ldir_ptr->i_dirt = DIRTY;
  if (new_slots > old_slots) {
 ldir_ptr->i_size = (off_t) new_slots * DIR_ENTRY_SIZE;
 /* Send the change to disk if the directory is extended. */
 if (extended) rw_inode(ldir_ptr, WRITING);
  }
  return(OK);
}
我们知道不管是目录文件还是普通文件都是通过i节点来唯一标识,read_map()函数会根据文件中的位移来确
定该位移是位于哪一数据块中。i节点中有一个表项是zone_t i_zone[V2_NR_TZONES];此处
V2_NR_TZONES=10在V1版本中为9,前7个表项用于直接区段,第八个用于一次间接区段,第九个用于二次
间接区段,第十个表现保留扩展。前七个用于数据位移小于七KB的文件,以此类推,read_map()就是通过计
算文件位移来确定它是位于这是个表项中的哪一个,如果是位于一次间接区段中,则需要根据第八个表项中存
储的数据块号,加载该数据块,在该数据块中继续计算该位移是属于该数据块中的第几个表项,该表项中存储
的便是要查找的数据块块号,从而返回该数据块号。二次间接块原理类似。原理如图所示:
 
该函数具体如下:
 
PUBLIC block_t read_map(rip, position)
register struct inode *rip; /* ptr to inode to map from */
off_t position;   /* position in file whose blk wanted */
{
/* Given an inode and a position within the corresponding file, locate the
 * block (not zone) number
in which that position is to be found and return it.
 */
  register struct buf *bp;
  register zone_t z;
  int scale, boff, dzones, nr_indirects, index, zind, ex;
  block_t b;
  long excess, zone, block_pos;
 
  scale = rip->i_sp->s_log_zone_size; /* for block-zone conversion */
  block_pos = position/BLOCK_SIZE; /* relative blk # in file */
  zone = block_pos >> scale; /* position's zone */
  boff = (int) (block_pos - (zone << scale) ); /* relative blk # within zone */
  dzones = rip->i_ndzones;
  nr_indirects = rip->i_nindirs;
  /* Is 'position' to be found in the inode itself? */
  if (zone < dzones) {
 zind = (int) zone; /* index should be an int */
 z = rip->i_zone[zind];
 if (z == NO_ZONE) return(NO_BLOCK);
 b = ((block_t) z << scale) + boff;
 return(b);/*返回找到的块号*/
  }
  /* It is not in the inode, so it must be single or double indirect. */
  excess = zone - dzones; /* first Vx_NR_DZONES don't count */
  if (excess < nr_indirects) {
 /* 'position' can be located via the single indirect block. 一次间接块*/
 z = rip->i_zone[dzones];/*得到一次间接块的的块号*/
  } else {
 /* 'position' can be located via the double indirect block.二次间接块操作 */
    if ( (z = rip->i_zone[dzones+1]) == NO_ZONE) return(NO_BLOCK);
    excess -= nr_indirects;   /* single indir doesn't count*/
    b = (block_t) z << scale;
    bp = get_block(rip->i_dev, b, NORMAL); /* get double indirect block */
    /*找到其在第一次间接块中的偏移,此偏移的条目指向着二次间接块,因为除法是按块的N的
    *整数倍来计算,此处nr_indirects应该等于一个块中所存储的目录数乘以块的大小,此处块的大小
    *为1024
    */
    index = (int) (excess/nr_indirects);  
    z = rd_indir(bp, index);  /* z= zone for single*/
    put_block(bp, INDIRECT_BLOCK);  /* release double ind block */
    /* index into single ind blk ,找到其在第二次间接块中的偏移*/
    excess = excess % nr_indirects;  
  }
  /*接下来的操作一次和二次间接块操作一样,因为经过上面的操作二次间接块在逻辑上已经和一次间接
   *块一样
   */
  /* 'z' is zone num for single indirect block; 'excess' is index into it. */
  if (z == NO_ZONE) return(NO_BLOCK);
  b = (block_t) z << scale;   /* b is blk # for single ind */
  bp = get_block(rip->i_dev, b, NORMAL); /* get single indirect block */
  ex = (int) excess;    /* need an integer */
  z = rd_indir(bp, ex);    /* get block pointed to */
  put_block(bp, INDIRECT_BLOCK);  /* release single indir blk */
  if (z == NO_ZONE) return(NO_BLOCK);
  b = ((block_t) z << scale) + boff;
  return(b);/*返回找到的块号*/
}
 
conv2()函数根据机器类型交换大小端数据。
PUBLIC unsigned conv2(norm, w)
int norm;   /* TRUE if no swap, FALSE for byte swap */
int w;    /* promotion of 16-bit word to be swapped */
{
/* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
  if (norm) return( (unsigned) w & 0xFFFF);
  return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
}
 
 
 
 
 
 
 
阅读(2638) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~