8000 read · yuanfeiz/minix Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
海皮飞 edited this page May 28, 2013 · 1 revision

read系统调用的核心实现定义在vfs中。

PUBLIC int read_write(rw_flag)
int rw_flag;			/* READING or WRITING */
{
  ...
  // 这里会区分三种设备,对于不同的设备用不同的函数读写。
  // 字符设备
  if (char_spec) {			/* Character special files. */
	dev_t dev;
	int suspend_reopen;

	suspend_reopen = (f->filp_state != FS_NORMAL);
	dev = (dev_t) vp->v_sdev;

	r = dev_io(op, dev, who_e, m_in.buffer, position, m_in.nbytes, oflags,
		   suspend_reopen);
	if (r >= 0) {
		cum_io = r;
		position = add64ul(position, r);
		r = OK;
	}
  // 块设备
  } else if (block_spec) {		/* Block special files. */
	r = req_breadwrite(vp->v_bfs_e, who_e, vp->v_sdev, position,
		m_in.nbytes, m_in.buffer, rw_flag, &res_pos, &res_cum_io);
	if (r == OK) {
		position = res_pos;
		cum_io += res_cum_io;
	}
  // 普通文件
  } else {				/* Regular files */
	if (rw_flag == WRITING && block_spec == 0) {
		/* Check for O_APPEND flag. */
		if (oflags & O_APPEND) position = cvul64(vp->v_size);
	}

	/* Issue request */
	r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, position, rw_flag, who_e,
			  m_in.buffer, m_in.nbytes, &new_pos, &cum_io_incr);

	if (r >= 0) {
		if (ex64hi(new_pos))
			panic("read_write: bad new pos");

		position = new_pos;
		cum_io += cum_io_incr;
	}
  }

  /* On write, update file size and access time. */
  // 写的时候,会更新文件大小和更新时间。
  if (rw_flag == WRITING) {
	if (regular || mode_word == I_DIRECTORY) {
		if (cmp64ul(position, vp->v_size) > 0) {
			if (ex64hi(position) != 0) {
				panic("read_write: file size too big ");
			}
			vp->v_size = ex64lo(position);
		}
	}
  }

  f->filp_pos = position;
  if (r == OK) return(cum_io);
  return(r);
}
Clone this wiki locally
0