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

📄 fs_yod.c

📁 非常经典的一个分布式系统
💻 C
📖 第 1 页 / 共 2 页
字号:
	return err;}static intyod_inop_getattr(struct pnode *pno, struct inode *ino, struct intnl_stat *stbuf){	char	*path;	int	err;	path = NULL;	if (!ino || I2NI(ino)->ni_fd < 0) {		path = _sysio_pb_path(pno->p_base, '/');		if (!path)			return -ENOMEM;	}	err =	    path	      ? yod_stat(path, stbuf)	      : yod_fstat(I2NI(ino)->ni_fd, stbuf);	if (path)		free(path);	return err;}static intyod_inop_setattr(struct pnode *pno,		    struct inode *ino,		    unsigned mask,		    struct intnl_stat *stbuf){	char	*path;	int	fd;	struct intnl_stat st;	int	err;	path = NULL;	fd = ino ? I2NI(ino)->ni_fd : -1;	if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {		if (!pno)			return -EEXIST;		path = _sysio_pb_path(pno->p_base, '/');		if (!path)			return -ENOMEM;	}	/*	 * Get current status for undo.	 */	err =	    fd < 0	      ? yod_stat(path, &st)	      : yod_fstat(fd, &st);	if (err)		goto out;	if (mask & SETATTR_MODE) {		mode_t	mode;		/*		 * Alter permissions attribute.		 */		mode = stbuf->st_mode & 07777;		err = chmod_yod(path, mode);	}	if (err)		mask &= ~SETATTR_MODE;	if (mask & (SETATTR_UID|SETATTR_GID)) {		/*		 * Alter owner and/or group identifiers.		 */		err = chown_yod(path,				mask & SETATTR_UID				  ? stbuf->st_uid				  : (uid_t )-1,				mask & SETATTR_GID				  ? stbuf->st_gid				  : (gid_t )-1);	}	if (err)		mask &= ~(SETATTR_UID|SETATTR_GID);	else if (mask & SETATTR_LEN) {		/*		 * Do the truncate last. It can't be undone.		 */		 (void )(fd < 0			   ? truncate_yod(path, stbuf->st_size)			   : ftruncate_yod(fd, stbuf->st_size));	}	if (!err)		goto out;	/*	 * Undo after error. Some or all of this might not work... We	 * can but try.	 */	if (mask & (SETATTR_UID|SETATTR_GID)) {		 (void )chown_yod(path,				  mask & SETATTR_UID				    ? st.st_uid				    : (uid_t )-1,				  mask & SETATTR_GID				    ? st.st_gid				    : (gid_t )-1);	}	if (mask & SETATTR_MODE) {		chmod_yod(path, st.st_mode & 07777);	}out:	if (path)		free(path);	return err;}static ssize_tyod_filldirentries(struct inode *ino,		   char *buf,		   _SYSIO_OFF_T *posp,		    size_t nbytes){	struct yod_inode *nino = I2NI(ino);	_SYSIO_OFF_T result;	ssize_t	cc;	assert(nino->ni_fd >= 0);	result = *basep;	if (*basep != nino->ni_fpos &&	    (result = lseek_yod(nino->ni_fd,				*posp,				SEEK_SET) == -1))		return -errno;	nino->ni_fpos = result;	memset(buf, 0, nbytes);	/*	 * This is almost certainly broken. The resulting position parameter	 * points to the block just filled, not the next.	 */	cc = getdirentries_yod(nino->ni_fd, buf, nbytes, &result);	if (cc < 0)		return -errno;	nino->ni_fpos = *posp = result;	return cc;}static intyod_inop_mkdir(struct pnode *pno, mode_t mode){	char	*path;	int	err;	path = _sysio_pb_path(pno->p_base, '/');	if (!path)		return -ENOMEM;	err = mkdir_yod(path, mode);	free(path);	return err;}static intyod_inop_rmdir(struct pnode *pno){	char	*path;	int	err;	path = _sysio_pb_path(pno->p_base, '/');	if (!path)		return -ENOMEM;	err = rmdir_yod(path);	free(path);	return err;}static intyod_inop_symlink(struct pnode *pno, const char *data){	char	*path;	int	err;	path = _sysio_pb_path(pno->p_base, '/');	if (!path)		return -ENOMEM;	err = symlink_yod(data, path);	free(path);	return err;}static intyod_inop_readlink(struct pnode *pno __IS_UNUSED, 		  char *buf __IS_UNUSED, 		  size_t bufsiz __IS_UNUSED){	return -ENOSYS;}static intyod_inop_open(struct pnode *pno, int flags, mode_t mode){	struct yod_inode *nino;	char	*path;	int	fd;	path = _sysio_pb_path(pno->p_base, '/');	if (!path)		return -ENOMEM;	/*	 * Whether the file is already open, or not, makes no difference.	 * Want to always give the host OS a chance to authorize in case	 * something has changed underneath us.	 */	if (flags & O_WRONLY) {		/*		 * Promote write-only attempt to RW.		 */		flags &= ~O_WRONLY;		flags |= O_RDWR;	}	fd = open_yod(path, flags, mode);	if (!pno->p_base->pb_ino && fd >= 0) {		int	err;		/*		 * Success but we need to return an i-node.		 */		err =		    yod_iget(pno->p_mount->mnt_fs,				path,				&pno->p_base->pb_ino,				1);		if (err) {			(void )close_yod(fd);			if (err == -EEXIST)				abort();			fd = err;		}	}	free(path);	if (fd < 0)		return -errno;	/*	 * Remember this new open.	 */	nino = I2NI(pno->p_base->pb_ino);	nino->ni_nopens++;	assert(nino->ni_nopens);	if (nino->ni_fd >= 0) {		if ((nino->ni_oflags & O_RDWR) ||		    (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {			/*			 * Keep existing.			 */			(void )close_yod(fd);			return 0;		}		(void )close_yod(nino->ni_fd);	}	/*	 * Invariant; First open. Must init.	 */	nino->ni_fpos = 0;	nino->ni_fd = fd;        /*         * Need to know whether we can seek on this         * descriptor.         */        nino->ni_seekok =            lseek_yod(nino->ni_fd, 0, SEEK_CUR) != 0 ? 0 : 1;	return 0;}static intyod_inop_close(struct inode *ino){	struct yod_inode *nino = I2NI(ino);	int	err;	if (nino->ni_fd < 0)		abort();	assert(nino->ni_nopens);	if (--nino->ni_nopens)		return 0;	err = close_yod(nino->ni_fd);	if (err)		return -errno;	nino->ni_fd = -1;	nino->ni_fpos = 0;	return 0;}static intyod_inop_link(struct pnode *old, struct pnode *new){	int	err;	char	*opath, *npath;	err = 0;	opath = _sysio_pb_path(old->p_base, '/');	npath = _sysio_pb_path(new->p_base, '/');	if (!(opath && npath)) {		err = -ENOMEM;		goto out;	}	err = link_yod(opath, npath);out:	if (opath)		free(opath);	if (npath)		free(npath);	return err;}static intyod_inop_unlink(struct pnode *pno){	char	*path;	int	err = 0;	path = _sysio_pb_path(pno->p_base, '/');	if (!path)		return -ENOMEM;	/*	 * For this driver, unlink is easy with open files. Since the	 * file remains open to the system, too, the descriptors are still	 * valid.	 *	 * Other drivers will have some difficulty here as the entry in the	 * file system name space must be removed without sacrificing access	 * to the file itself. In NFS this is done with a mechanism referred	 * to as a `silly delete'. The file is moved to a temporary name	 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some	 * unique characters) in order to simulate the proper semantic.	 */	if (unlink_yod(path) != 0)		err = -errno;	free(path);	return err;}/* * A helper function performing the real IO operation work. * * We don't really have async IO. We'll just perform the function * now. */static intdoio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct yod_inode *),	struct ioctx *ioctx){	struct yod_inode *nino = I2NI(ioctx->ioctx_ino);	ioctx->ioctx_cc =		_sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,			    ioctx->ioctx_iov, ioctx->ioctx_iovlen,			    (ssize_t (*)(void *, size_t, 					 _SYSIO_OFF_T, void *))f,			    nino);	if (ioctx->ioctx_cc < 0) {		ioctx->ioctx_errno = -ioctx->ioctx_cc;		ioctx->ioctx_cc = -1;		return -1;	}	nino->ni_fpos += ioctx->ioctx_cc;	ioctx->ioctx_done = 1;	return 0;}       static ssize_tyod_read_simple(void *buf,	 	size_t nbytes,		_SYSIO_OFF_T off,		struct yod_inode *nino){	if (off != nino->ni_fpos) {		_SYSIO_OFF_T rtn;		rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);	 	if (rtn < 0) 			return -1;		nino->ni_fpos = rtn;	}	return read_yod(nino->ni_fd, buf, nbytes);}static intyod_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx){	return doio(yod_read_simple, ioctx);}static intyod_inop_rename(struct pnode *old, struct pnode *new){	int	err;	char	*opath, *npath;	opath = _sysio_pb_path(old->p_base, '/');	npath = _sysio_pb_path(new->p_base, '/');	if (!(opath && npath)) {		err = -ENOMEM;		goto out;	}	err = rename_yod(opath, npath);out:	if (opath)		free(opath);	if (npath)		free(npath);	return err;}static ssize_tyod_write_simple(void *buf,	 	size_t nbytes,		_SYSIO_OFF_T off,		struct yod_inode *nino){	if (off != nino->ni_fpos) {		_SYSIO_OFF_T rtn;		rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);	 	if (rtn < 0) 			return -1;		nino->ni_fpos = rtn;	}	return write_yod(nino->ni_fd, buf, nbytes);}static intyod_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx){	return doio(yod_write_simple, ioctx);}static _SYSIO_OFF_Tyod_inop_pos(struct inode *ino, _SYSIO_OFF_T off){	struct yod_inode *nino = I2NI(ino);	int	err;	err = lseek_yod(nino->ni_fd, off, SEEK_SET);	return err < 0 ? err : off;}static intyod_inop_iodone(struct ioctx *ioctxp __IS_UNUSED){	/*	 * It's always done in this driver. It completed when posted.	 */	return 1;}static intyod_inop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn){	struct yod_inode *nino = I2NI(ino);	long	arg;	int	err;	if (nino->ni_fd < 0)		abort();	err = 0;	switch (cmd) {	case F_GETFD:	case F_GETFL:#ifdef F_GETOWN	case F_GETOWN:#endif		*rtn = syscall(SYS_fcntl, nino->ni_fd, cmd);		if (*rtn == -1)			err = -errno;		break;	case F_DUPFD:	case F_SETFD:	case F_SETFL:	case F_GETLK:	case F_SETLK:	case F_SETLKW:#ifdef F_SETOWN	case F_SETOWN:#endif		arg = va_arg(ap, long);		*rtn = syscall(SYS_fcntl, nino->ni_fd, cmd, arg);		if (*rtn == -1)			err = -errno;		break;	default:		*rtn = -1;		err = -EINVAL;	}	return err;}static intyod_inop_mknod(struct pnode *pno __IS_UNUSED,		  mode_t mode __IS_UNUSED,		  dev_t dev __IS_UNUSED){	return -ENOSYS;}#ifdef _HAVE_STATVFSstatic intyod_inop_statvfs(struct pnode *pno,		    struct inode *ino,		    struct intnl_statvfs *buf){	char	*path;	int    rc;	struct statfs fs;	path = NULL;	if (!ino || I2NI(ino)->ni_fd < 0) {		path = _sysio_pb_path(pno->p_base, '/');		if (!path)			return -ENOMEM;	}	/*	 * The syscall interface does not support SYS_fstatvfs.	 * Should possibly return ENOSYS, but thought it	 * better to use SYS_fstatfs and fill in as much of	 * the statvfs structure as possible.  This allows	 * for more of a test of the sysio user interface.	 */	rc =	    path	      ? statfs_yod(path, &fs)	      : fstatfs_yod(I2NI(ino)->ni_fd, &fs);	if (path)		free(path);	if (rc < 0)		return -errno;	buf->f_bsize = fs.f_bsize;  /* file system block size */	buf->f_frsize = fs.f_bsize; /* file system fundamental block size */	buf->f_blocks = fs.f_blocks;	buf->f_bfree = fs.f_bfree;	buf->f_bavail = fs.f_bavail;	buf->f_files = fs.f_files;  /* Total number serial numbers */	buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */	buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/	buf->f_fsid = fs.f_fsid.__val[1];	buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */	buf->f_namemax = fs.f_namelen;	return 0;}#endifstatic intyod_inop_sync(struct inode *ino){	assert(I2NI(ino)->ni_fd >= 0);	return fsync_yod(I2NI(ino)->ni_fd);}static intyod_inop_datasync(struct inode *ino){	assert(I2NI(ino)->ni_fd >= 0);	return fsync_yod(I2NI(ino)->ni_fd);}static intyod_inop_ioctl(struct inode *ino __IS_UNUSED,		  unsigned long int request __IS_UNUSED,		  va_list ap __IS_UNUSED){	/*	 * I'm lazy. Maybe implemented later.	 */	errno = ENOTTY;	return -1;}static voidyod_inop_gone(struct inode *ino){	struct yod_inode *nino = I2NI(ino);	if (nino->ni_fd)		(void )close(nino->ni_fd);	free(ino->i_private);}static voidyod_fsop_gone(struct filesys *fs __IS_UNUSED){	/*	 * Do nothing. There is no private part maintained for the	 * yod file interface. 	 */}

⌨️ 快捷键说明

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