⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read.c

📁 minix3的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  }  if (!block_spec && b == NO_BLOCK) {	if (rw_flag == READING) {		/* Reading from a nonexistent block.  Must read as all zeros.*/		bp = get_block(NO_DEV, NO_BLOCK, NORMAL);    /* get a buffer */		zero_block(bp);	} else {		/* Writing to a nonexistent block. Create and enter in inode.*/		if ((bp= new_block(rip, position)) == NIL_BUF)return(err_code);	}  } else if (rw_flag == READING) {	/* Read and read ahead if convenient. */	bp = rahead(rip, b, position, left);  } else {	/* Normally an existing block to be partially overwritten is first read	 * in.  However, a full block need not be read in.  If it is already in	 * the cache, acquire it, otherwise just acquire a free buffer.	 */	n = (chunk == block_size ? NO_READ : NORMAL);	if (!block_spec && off == 0 && position >= rip->i_size) n = NO_READ;	bp = get_block(dev, b, n);  }  /* In all cases, bp now points to a valid buffer. */  if (bp == NIL_BUF) {  	panic(__FILE__,"bp not valid in rw_chunk, this can't happen", NO_NUM);  }  if (rw_flag == WRITING && chunk != block_size && !block_spec &&					position >= rip->i_size && off == 0) {	zero_block(bp);  }  if (rw_flag == READING) {	/* Copy a chunk from the block buffer to user space. */	r = sys_vircopy(FS_PROC_NR, D, (phys_bytes) (bp->b_data+off),			usr, seg, (phys_bytes) buff,			(phys_bytes) chunk);  } else {	/* Copy a chunk from user space to the block buffer. */	r = sys_vircopy(usr, seg, (phys_bytes) buff,			FS_PROC_NR, D, (phys_bytes) (bp->b_data+off),			(phys_bytes) chunk);	bp->b_dirt = DIRTY;  }  n = (off + chunk == block_size ? FULL_DATA_BLOCK : PARTIAL_DATA_BLOCK);  put_block(bp, n);  return(r);}/*===========================================================================* *				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/rip->i_sp->s_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 */	index = (int) (excess/nr_indirects);	z = rd_indir(bp, index);		/* z= zone for single*/	put_block(bp, INDIRECT_BLOCK);		/* release double ind block */	excess = excess % nr_indirects;		/* index into single ind blk */  }  /* '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);}/*===========================================================================* *				rd_indir				     * *===========================================================================*/PUBLIC zone_t rd_indir(bp, index)struct buf *bp;			/* pointer to indirect block */int index;			/* index into *bp */{/* Given a pointer to an indirect block, read one entry.  The reason for * making a separate routine out of this is that there are four cases: * V1 (IBM and 68000), and V2 (IBM and 68000). */  struct super_block *sp;  zone_t zone;			/* V2 zones are longs (shorts in V1) */  sp = get_super(bp->b_dev);	/* need super block to find file sys type */  /* read a zone from an indirect block */  if (sp->s_version == V1)	zone = (zone_t) conv2(sp->s_native, (int)  bp->b_v1_ind[index]);  else	zone = (zone_t) conv4(sp->s_native, (long) bp->b_v2_ind[index]);  if (zone != NO_ZONE &&		(zone < (zone_t) sp->s_firstdatazone || zone >= sp->s_zones)) {	printf("Illegal zone number %ld in indirect block, index %d\n",	       (long) zone, index);	panic(__FILE__,"check file system", NO_NUM);  }  return(zone);}/*===========================================================================* *				read_ahead				     * *===========================================================================*/PUBLIC void read_ahead(){/* Read a block into the cache before it is needed. */  int block_size;  register struct inode *rip;  struct buf *bp;  block_t b;  rip = rdahed_inode;		/* pointer to inode to read ahead from */  block_size = get_block_size(rip->i_dev);  rdahed_inode = NIL_INODE;	/* turn off read ahead */  if ( (b = read_map(rip, rdahedpos)) == NO_BLOCK) return;	/* at EOF */  bp = rahead(rip, b, rdahedpos, block_size);  put_block(bp, PARTIAL_DATA_BLOCK);}/*===========================================================================* *				rahead					     * *===========================================================================*/PUBLIC struct buf *rahead(rip, baseblock, position, bytes_ahead)register struct inode *rip;	/* pointer to inode for file to be read */block_t baseblock;		/* block at current position */off_t position;			/* position within file */unsigned bytes_ahead;		/* bytes beyond position for immediate use */{/* Fetch a block from the cache or the device.  If a physical read is * required, prefetch as many more blocks as convenient into the cache. * This usually covers bytes_ahead and is at least BLOCKS_MINIMUM. * The device driver may decide it knows better and stop reading at a * cylinder boundary (or after an error).  Rw_scattered() puts an optional * flag on all reads to allow this. */  int block_size;/* Minimum number of blocks to prefetch. */# define BLOCKS_MINIMUM		(NR_BUFS < 50 ? 18 : 32)  int block_spec, scale, read_q_size;  unsigned int blocks_ahead, fragment;  block_t block, blocks_left;  off_t ind1_pos;  dev_t dev;  struct buf *bp;  static struct buf *read_q[NR_BUFS];  block_spec = (rip->i_mode & I_TYPE) == I_BLOCK_SPECIAL;  if (block_spec) {	dev = (dev_t) rip->i_zone[0];  } else {	dev = rip->i_dev;  }  block_size = get_block_size(dev);  block = baseblock;  bp = get_block(dev, block, PREFETCH);  if (bp->b_dev != NO_DEV) return(bp);  /* The best guess for the number of blocks to prefetch:  A lot.   * It is impossible to tell what the device looks like, so we don't even   * try to guess the geometry, but leave it to the driver.   *   * The floppy driver can read a full track with no rotational delay, and it   * avoids reading partial tracks if it can, so handing it enough buffers to   * read two tracks is perfect.  (Two, because some diskette types have   * an odd number of sectors per track, so a block may span tracks.)   *   * The disk drivers don't try to be smart.  With todays disks it is   * impossible to tell what the real geometry looks like, so it is best to   * read as much as you can.  With luck the caching on the drive allows   * for a little time to start the next read.   *   * The current solution below is a bit of a hack, it just reads blocks from   * the current file position hoping that more of the file can be found.  A   * better solution must look at the already available zone pointers and   * indirect blocks (but don't call read_map!).   */  fragment = position % block_size;  position -= fragment;  bytes_ahead += fragment;  blocks_ahead = (bytes_ahead + block_size - 1) / block_size;  if (block_spec && rip->i_size == 0) {	blocks_left = NR_IOREQS;  } else {	blocks_left = (rip->i_size - position + block_size - 1) / block_size;	/* Go for the first indirect block if we are in its neighborhood. */	if (!block_spec) {		scale = rip->i_sp->s_log_zone_size;		ind1_pos = (off_t) rip->i_ndzones * (block_size << scale);		if (position <= ind1_pos && rip->i_size > ind1_pos) {			blocks_ahead++;			blocks_left++;		}	}  }  /* No more than the maximum request. */  if (blocks_ahead > NR_IOREQS) blocks_ahead = NR_IOREQS;  /* Read at least the minimum number of blocks, but not after a seek. */  if (blocks_ahead < BLOCKS_MINIMUM && rip->i_seek == NO_SEEK)	blocks_ahead = BLOCKS_MINIMUM;  /* Can't go past end of file. */  if (blocks_ahead > blocks_left) blocks_ahead = blocks_left;  read_q_size = 0;  /* Acquire block buffers. */  for (;;) {	read_q[read_q_size++] = bp;	if (--blocks_ahead == 0) break;	/* Don't trash the cache, leave 4 free. */	if (bufs_in_use >= NR_BUFS - 4) break;	block++;	bp = get_block(dev, block, PREFETCH);	if (bp->b_dev != NO_DEV) {		/* Oops, block already in the cache, get out. */		put_block(bp, FULL_DATA_BLOCK);		break;	}  }  rw_scattered(dev, read_q, read_q_size, READING);  return(get_block(dev, baseblock, NORMAL));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -