📄 vfs_syscalls.c
字号:
break; default: error = EINVAL; break; } } if (!error) { LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); } else { VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); if (nd.ni_dvp == vp) vrele(nd.ni_dvp); else vput(nd.ni_dvp); if (vp) vrele(vp); } return (error);}/* * Create named pipe. */struct mkfifo_args { char *path; int mode;};/* ARGSUSED */mkfifo(p, uap, retval) struct proc *p; register struct mkfifo_args *uap; int *retval;{ struct vattr vattr; int error; struct nameidata nd;#ifndef FIFO return (EOPNOTSUPP);#else NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); if (nd.ni_vp != NULL) { VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); if (nd.ni_dvp == nd.ni_vp) vrele(nd.ni_dvp); else vput(nd.ni_dvp); vrele(nd.ni_vp); return (EEXIST); } VATTR_NULL(&vattr); vattr.va_type = VFIFO; vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask; LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));#endif /* FIFO */}/* * Make a hard file link. */struct link_args { char *path; char *link;};/* ARGSUSED */link(p, uap, retval) struct proc *p; register struct link_args *uap; int *retval;{ register struct vnode *vp; struct nameidata nd; int error; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); vp = nd.ni_vp; if (vp->v_type != VDIR || (error = suser(p->p_ucred, &p->p_acflag)) == 0) { nd.ni_cnd.cn_nameiop = CREATE; nd.ni_cnd.cn_flags = LOCKPARENT; nd.ni_dirp = uap->link; if ((error = namei(&nd)) == 0) { if (nd.ni_vp != NULL) error = EEXIST; if (!error) { LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); } else { VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); if (nd.ni_dvp == nd.ni_vp) vrele(nd.ni_dvp); else vput(nd.ni_dvp); if (nd.ni_vp) vrele(nd.ni_vp); } } } vrele(vp); return (error);}/* * Make a symbolic link. */struct symlink_args { char *path; char *link;};/* ARGSUSED */symlink(p, uap, retval) struct proc *p; register struct symlink_args *uap; int *retval;{ struct vattr vattr; char *path; int error; struct nameidata nd; MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); if (error = copyinstr(uap->path, path, MAXPATHLEN, NULL)) goto out; NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->link, p); if (error = namei(&nd)) goto out; if (nd.ni_vp) { VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); if (nd.ni_dvp == nd.ni_vp) vrele(nd.ni_dvp); else vput(nd.ni_dvp); vrele(nd.ni_vp); error = EEXIST; goto out; } VATTR_NULL(&vattr); vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask; LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);out: FREE(path, M_NAMEI); return (error);}/* * Delete a name from the filesystem. */struct unlink_args { char *path;};/* ARGSUSED */unlink(p, uap, retval) struct proc *p; struct unlink_args *uap; int *retval;{ register struct vnode *vp; int error; struct nameidata nd; NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); vp = nd.ni_vp; LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); VOP_LOCK(vp); if (vp->v_type != VDIR || (error = suser(p->p_ucred, &p->p_acflag)) == 0) { /* * The root of a mounted filesystem cannot be deleted. */ if (vp->v_flag & VROOT) error = EBUSY; else (void)vnode_pager_uncache(vp); } if (!error) { LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); } else { VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); if (nd.ni_dvp == vp) vrele(nd.ni_dvp); else vput(nd.ni_dvp); vput(vp); } return (error);}/* * Reposition read/write file offset. */struct lseek_args { int fd; int pad; off_t offset; int whence;};lseek(p, uap, retval) struct proc *p; register struct lseek_args *uap; int *retval;{ struct ucred *cred = p->p_ucred; register struct filedesc *fdp = p->p_fd; register struct file *fp; struct vattr vattr; int error; if ((u_int)uap->fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[uap->fd]) == NULL) return (EBADF); if (fp->f_type != DTYPE_VNODE) return (ESPIPE); switch (uap->whence) { case L_INCR: fp->f_offset += uap->offset; break; case L_XTND: if (error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p)) return (error); fp->f_offset = uap->offset + vattr.va_size; break; case L_SET: fp->f_offset = uap->offset; break; default: return (EINVAL); } *(off_t *)retval = fp->f_offset; return (0);}#if defined(COMPAT_43) || defined(COMPAT_SUNOS)/* * Reposition read/write file offset. */struct olseek_args { int fd; long offset; int whence;};olseek(p, uap, retval) struct proc *p; register struct olseek_args *uap; int *retval;{ struct lseek_args nuap; off_t qret; int error; nuap.fd = uap->fd; nuap.offset = uap->offset; nuap.whence = uap->whence; error = lseek(p, &nuap, &qret); *(long *)retval = qret; return (error);}#endif /* COMPAT_43 *//* * Check access permissions. */struct access_args { char *path; int flags;};access(p, uap, retval) struct proc *p; register struct access_args *uap; int *retval;{ register struct ucred *cred = p->p_ucred; register struct vnode *vp; int error, flags, t_gid, t_uid; struct nameidata nd; t_uid = cred->cr_uid; t_gid = cred->cr_groups[0]; cred->cr_uid = p->p_cred->p_ruid; cred->cr_groups[0] = p->p_cred->p_rgid; NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) goto out1; vp = nd.ni_vp; /* Flags == 0 means only check for existence. */ if (uap->flags) { flags = 0; if (uap->flags & R_OK) flags |= VREAD; if (uap->flags & W_OK) flags |= VWRITE; if (uap->flags & X_OK) flags |= VEXEC; if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) error = VOP_ACCESS(vp, flags, cred, p); } vput(vp);out1: cred->cr_uid = t_uid; cred->cr_groups[0] = t_gid; return (error);}#if defined(COMPAT_43) || defined(COMPAT_SUNOS)/* * Get file status; this version follows links. */struct ostat_args { char *path; struct ostat *ub;};/* ARGSUSED */ostat(p, uap, retval) struct proc *p; register struct ostat_args *uap; int *retval;{ struct stat sb; struct ostat osb; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); error = vn_stat(nd.ni_vp, &sb, p); vput(nd.ni_vp); if (error) return (error); cvtstat(&sb, &osb); error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb)); return (error);}/* * Get file status; this version does not follow links. */struct olstat_args { char *path; struct ostat *ub;};/* ARGSUSED */olstat(p, uap, retval) struct proc *p; register struct olstat_args *uap; int *retval;{ struct stat sb; struct ostat osb; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); error = vn_stat(nd.ni_vp, &sb, p); vput(nd.ni_vp); if (error) return (error); cvtstat(&sb, &osb); error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb)); return (error);}/* * Convert from an old to a new stat structure. */cvtstat(st, ost) struct stat *st; struct ostat *ost;{ ost->st_dev = st->st_dev; ost->st_ino = st->st_ino; ost->st_mode = st->st_mode; ost->st_nlink = st->st_nlink; ost->st_uid = st->st_uid; ost->st_gid = st->st_gid; ost->st_rdev = st->st_rdev; if (st->st_size < (quad_t)1 << 32) ost->st_size = st->st_size; else ost->st_size = -2; ost->st_atime = st->st_atime; ost->st_mtime = st->st_mtime; ost->st_ctime = st->st_ctime; ost->st_blksize = st->st_blksize; ost->st_blocks = st->st_blocks; ost->st_flags = st->st_flags; ost->st_gen = st->st_gen;}#endif /* COMPAT_43 || COMPAT_SUNOS *//* * Get file status; this version follows links. */struct stat_args { char *path; struct stat *ub;};/* ARGSUSED */stat(p, uap, retval) struct proc *p; register struct stat_args *uap; int *retval;{ struct stat sb; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); error = vn_stat(nd.ni_vp, &sb, p); vput(nd.ni_vp); if (error) return (error); error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb)); return (error);}/* * Get file status; this version does not follow links. */struct lstat_args { char *path; struct stat *ub;};/* ARGSUSED */lstat(p, uap, retval) struct proc *p; register struct lstat_args *uap; int *retval;{ int error; struct vnode *vp, *dvp; struct stat sb, sb1; struct nameidata nd; NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); /* * For symbolic links, always return the attributes of its * containing directory, except for mode, size, and links. */ vp = nd.ni_vp; dvp = nd.ni_dvp; if (vp->v_type != VLNK) { if (dvp == vp) vrele(dvp); else vput(dvp); error = vn_stat(vp, &sb, p); vput(vp); if (error) return (error); } else { error = vn_stat(dvp, &sb, p); vput(dvp); if (error) { vput(vp); return (error); } error = vn_stat(vp, &sb1, p); vput(vp); if (error) return (error); sb.st_mode &= ~S_IFDIR; sb.st_mode |= S_IFLNK; sb.st_nlink = sb1.st_nlink; sb.st_size = sb1.st_size; sb.st_blocks = sb1.st_blocks; } error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb)); return (error);}/* * Get configurable pathname variables. */struct pathconf_args { char *path; int name;};/* ARGSUSED */pathconf(p, uap, retval) struct proc *p; register struct pathconf_args *uap; int *retval;{ int error; struct nameidata nd; NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); error = VOP_PATHCONF(nd.ni_vp, uap->name, retval); vput(nd.ni_vp); return (error);}/* * Return target name of a symbolic link. */struct readlink_args { char *path; char *buf; int count;};/* ARGSUSED */readlink(p, uap, retval) struct proc *p; register struct readlink_args *uap; int *retval;{ register struct vnode *vp; struct iovec aiov; struct uio auio; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); vp = nd.ni_vp; if (vp->v_type != VLNK) error = EINVAL; else { aiov.iov_base = uap->buf; aiov.iov_len = uap->count; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_rw = UIO_READ; auio.uio_segflg = UIO_USERSPACE; auio.uio_procp = p; auio.uio_resid = uap->count; error = VOP_READLINK(vp, &auio, p->p_ucred); } vput(vp); *retval = uap->count - auio.uio_resid; return (error);}/* * Change flags of a file given a path name. */struct chflags_args { char *path; int flags;};/* ARGSUSED */chflags(p, uap, retval) struct proc *p; register struct chflags_args *uap; int *retval;{ register struct vnode *vp; struct vattr vattr; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); vp = nd.ni_vp; LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); VOP_LOCK(vp); if (vp->v_mount->mnt_flag & MNT_RDONLY) error = EROFS; else { VATTR_NULL(&vattr); vattr.va_flags = uap->flags; error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); } vput(vp); return (error);}/* * Change flags of a file given a file descriptor. */struct fchflags_args { int fd; int flags;};/* ARGSUSED */fchflags(p, uap, retval) struct proc *p; register struct fchflags_args *uap; int *retval;{ struct vattr vattr; struct vnode *vp; struct file *fp; int error; if (error = getvnode(p->p_fd, uap->fd, &fp)) return (error); vp = (struct vnode *)fp->f_data; LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); VOP_LOCK(vp); if (vp->v_mount->mnt_flag & MNT_RDONLY) error = EROFS; else { VATTR_NULL(&vattr); vattr.va_flags = uap->flags; error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); } VOP_UNLOCK(vp); return (error);}/* * Change mode of a file given path name. */struct chmod_args { char *path; int mode;};/* ARGSUSED */chmod(p, uap, retval) struct proc *p; register struct chmod_args *uap; int *retval;{ register struct vnode *vp; struct vattr vattr; int error; struct nameidata nd; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p); if (error = namei(&nd)) return (error); vp = nd.ni_vp; LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); VOP_LOCK(vp); if (vp->v_mount->mnt_flag & MNT_RDONLY) error = EROFS; else { VATTR_NULL(&vattr); vattr.va_mode = uap->mode & ALLPERMS; error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); } vput(vp); return (error);}/* * Change mode of a file given a file descriptor. */struct fchmod_args { int fd; int mode;};/* ARGSUSED */fchmod(p, uap, retval) struct proc *p; register struct fchmod_args *uap; int *retval;{ struct vattr vattr; struct vnode *vp; struct file *fp; int error; if (error = getvnode(p->p_fd, uap->fd, &fp)) return (error); vp = (struct vnode *)fp->f_data; LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE); VOP_LOCK(vp); if (vp->v_mount->mnt_flag & MNT_RDONLY) error = EROFS; else { VATTR_NULL(&vattr); vattr.va_mode = uap->mode & ALLPERMS; error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); } VOP_UNLOCK(vp); return (error);}/* * Set ownership given a path name. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -