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

📄 vfs_syscalls.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (c) 1989, 1993 *	The Regents of the University of California.  All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *	@(#)vfs_syscalls.c	8.13 (Berkeley) 4/15/94 */#include <sys/param.h>#include <sys/systm.h>#include <sys/namei.h>#include <sys/filedesc.h>#include <sys/kernel.h>#include <sys/file.h>#include <sys/stat.h>#include <sys/vnode.h>#include <sys/mount.h>#include <sys/proc.h>#include <sys/uio.h>#include <sys/malloc.h>#include <sys/dirent.h>#include <vm/vm.h>#include <sys/sysctl.h>static int change_dir __P((struct nameidata *ndp, struct proc *p));/* * Virtual File System System Calls *//* * Mount a file system. */struct mount_args {	int	type;	char	*path;	int	flags;	caddr_t	data;};/* ARGSUSED */mount(p, uap, retval)	struct proc *p;	register struct mount_args *uap;	int *retval;{	register struct vnode *vp;	register struct mount *mp;	int error, flag;	struct nameidata nd;	/*	 * Must be super user	 */	if (error = suser(p->p_ucred, &p->p_acflag))		return (error);	/*	 * Get vnode to be covered	 */	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);	if (error = namei(&nd))		return (error);	vp = nd.ni_vp;	if (uap->flags & MNT_UPDATE) {		if ((vp->v_flag & VROOT) == 0) {			vput(vp);			return (EINVAL);		}		mp = vp->v_mount;		flag = mp->mnt_flag;		/*		 * We only allow the filesystem to be reloaded if it		 * is currently mounted read-only.		 */		if ((uap->flags & MNT_RELOAD) &&		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {			vput(vp);			return (EOPNOTSUPP);	/* Needs translation */		}		mp->mnt_flag |=		    uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);		VOP_UNLOCK(vp);		goto update;	}	if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))		return (error);	if (vp->v_type != VDIR) {		vput(vp);		return (ENOTDIR);	}	if ((u_long)uap->type > MOUNT_MAXTYPE || vfssw[uap->type] == NULL) {		vput(vp);		return (ENODEV);	}	/*	 * Allocate and initialize the file system.	 */	mp = (struct mount *)malloc((u_long)sizeof(struct mount),		M_MOUNT, M_WAITOK);	bzero((char *)mp, (u_long)sizeof(struct mount));	mp->mnt_op = vfssw[uap->type];	if (error = vfs_lock(mp)) {		free((caddr_t)mp, M_MOUNT);		vput(vp);		return (error);	}	if (vp->v_mountedhere != NULL) {		vfs_unlock(mp);		free((caddr_t)mp, M_MOUNT);		vput(vp);		return (EBUSY);	}	vp->v_mountedhere = mp;	mp->mnt_vnodecovered = vp;update:	/*	 * Set the mount level flags.	 */	if (uap->flags & MNT_RDONLY)		mp->mnt_flag |= MNT_RDONLY;	else if (mp->mnt_flag & MNT_RDONLY)		mp->mnt_flag |= MNT_WANTRDWR;	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);	mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);	/*	 * Mount the filesystem.	 */	error = VFS_MOUNT(mp, uap->path, uap->data, &nd, p);	if (mp->mnt_flag & MNT_UPDATE) {		vrele(vp);		if (mp->mnt_flag & MNT_WANTRDWR)			mp->mnt_flag &= ~MNT_RDONLY;		mp->mnt_flag &=~		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);		if (error)			mp->mnt_flag = flag;		return (error);	}	/*	 * Put the new filesystem on the mount list after root.	 */	cache_purge(vp);	if (!error) {		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);		VOP_UNLOCK(vp);		vfs_unlock(mp);		error = VFS_START(mp, 0, p);	} else {		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;		vfs_unlock(mp);		free((caddr_t)mp, M_MOUNT);		vput(vp);	}	return (error);}/* * Unmount a file system. * * Note: unmount takes a path to the vnode mounted on as argument, * not special file (as before). */struct unmount_args {	char	*path;	int	flags;};/* ARGSUSED */unmount(p, uap, retval)	struct proc *p;	register struct unmount_args *uap;	int *retval;{	register struct vnode *vp;	struct mount *mp;	int error;	struct nameidata nd;	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);	if (error = namei(&nd))		return (error);	vp = nd.ni_vp;	/*	 * Unless this is a user mount, then must	 * have suser privilege.	 */	if (((vp->v_mount->mnt_flag & MNT_USER) == 0) &&	    (error = suser(p->p_ucred, &p->p_acflag))) {		vput(vp);		return (error);	}	/*	 * Must be the root of the filesystem	 */	if ((vp->v_flag & VROOT) == 0) {		vput(vp);		return (EINVAL);	}	mp = vp->v_mount;	vput(vp);	return (dounmount(mp, uap->flags, p));}/* * Do the actual file system unmount. */dounmount(mp, flags, p)	register struct mount *mp;	int flags;	struct proc *p;{	struct vnode *coveredvp;	int error;	coveredvp = mp->mnt_vnodecovered;	if (vfs_busy(mp))		return (EBUSY);	mp->mnt_flag |= MNT_UNMOUNT;	if (error = vfs_lock(mp))		return (error);	mp->mnt_flag &=~ MNT_ASYNC;	vnode_pager_umount(mp);	/* release cached vnodes */	cache_purgevfs(mp);	/* remove cache entries for this file sys */	if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||	    (flags & MNT_FORCE))		error = VFS_UNMOUNT(mp, flags, p);	mp->mnt_flag &= ~MNT_UNMOUNT;	vfs_unbusy(mp);	if (error) {		vfs_unlock(mp);	} else {		vrele(coveredvp);		TAILQ_REMOVE(&mountlist, mp, mnt_list);		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;		vfs_unlock(mp);		if (mp->mnt_vnodelist.lh_first != NULL)			panic("unmount: dangling vnode");		free((caddr_t)mp, M_MOUNT);	}	return (error);}/* * Sync each mounted filesystem. */#ifdef DIAGNOSTICint syncprt = 0;struct ctldebug debug0 = { "syncprt", &syncprt };#endifstruct sync_args {	int	dummy;};/* ARGSUSED */sync(p, uap, retval)	struct proc *p;	struct sync_args *uap;	int *retval;{	register struct mount *mp, *nmp;	int asyncflag;	for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {		nmp = mp->mnt_list.tqe_next;		/*		 * The lock check below is to avoid races with mount		 * and unmount.		 */		if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&		    !vfs_busy(mp)) {			asyncflag = mp->mnt_flag & MNT_ASYNC;			mp->mnt_flag &= ~MNT_ASYNC;			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);			if (asyncflag)				mp->mnt_flag |= MNT_ASYNC;			vfs_unbusy(mp);		}	}#ifdef DIAGNOSTIC	if (syncprt)		vfs_bufstats();#endif /* DIAGNOSTIC */	return (0);}/* * Change filesystem quotas. */struct quotactl_args {	char *path;	int cmd;	int uid;	caddr_t arg;};/* ARGSUSED */quotactl(p, uap, retval)	struct proc *p;	register struct quotactl_args *uap;	int *retval;{	register struct mount *mp;	int error;	struct nameidata nd;	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);	if (error = namei(&nd))		return (error);	mp = nd.ni_vp->v_mount;	vrele(nd.ni_vp);	return (VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, p));}/* * Get filesystem statistics. */struct statfs_args {	char *path;	struct statfs *buf;};/* ARGSUSED */statfs(p, uap, retval)	struct proc *p;	register struct statfs_args *uap;	int *retval;{	register struct mount *mp;	register struct statfs *sp;	int error;	struct nameidata nd;	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);	if (error = namei(&nd))		return (error);	mp = nd.ni_vp->v_mount;	sp = &mp->mnt_stat;	vrele(nd.ni_vp);	if (error = VFS_STATFS(mp, sp, p))		return (error);	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;	return (copyout((caddr_t)sp, (caddr_t)uap->buf, sizeof(*sp)));}/* * Get filesystem statistics. */struct fstatfs_args {	int fd;	struct statfs *buf;};/* ARGSUSED */fstatfs(p, uap, retval)	struct proc *p;	register struct fstatfs_args *uap;	int *retval;{	struct file *fp;	struct mount *mp;	register struct statfs *sp;	int error;	if (error = getvnode(p->p_fd, uap->fd, &fp))		return (error);	mp = ((struct vnode *)fp->f_data)->v_mount;	sp = &mp->mnt_stat;	if (error = VFS_STATFS(mp, sp, p))		return (error);	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;	return (copyout((caddr_t)sp, (caddr_t)uap->buf, sizeof(*sp)));}/* * Get statistics on all filesystems. */struct getfsstat_args {	struct statfs *buf;	long bufsize;	int flags;};getfsstat(p, uap, retval)	struct proc *p;	register struct getfsstat_args *uap;	int *retval;{	register struct mount *mp, *nmp;	register struct statfs *sp;	caddr_t sfsp;	long count, maxcount, error;	maxcount = uap->bufsize / sizeof(struct statfs);	sfsp = (caddr_t)uap->buf;	for (count = 0, mp = mountlist.tqh_first; mp != NULL; mp = nmp) {		nmp = mp->mnt_list.tqe_next;		if (sfsp && count < maxcount &&		    ((mp->mnt_flag & MNT_MLOCK) == 0)) {			sp = &mp->mnt_stat;			/*			 * If MNT_NOWAIT is specified, do not refresh the			 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.			 */			if (((uap->flags & MNT_NOWAIT) == 0 ||			    (uap->flags & MNT_WAIT)) &&			    (error = VFS_STATFS(mp, sp, p)))				continue;			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;			if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))				return (error);			sfsp += sizeof(*sp);		}		count++;	}	if (sfsp && count > maxcount)		*retval = maxcount;	else		*retval = count;	return (0);}/* * Change current working directory to a given file descriptor. */struct fchdir_args {	int	fd;};/* ARGSUSED */fchdir(p, uap, retval)	struct proc *p;	struct fchdir_args *uap;	int *retval;{	register struct filedesc *fdp = p->p_fd;	register struct vnode *vp;	struct file *fp;	int error;	if (error = getvnode(fdp, uap->fd, &fp))		return (error);	vp = (struct vnode *)fp->f_data;	VOP_LOCK(vp);	if (vp->v_type != VDIR)		error = ENOTDIR;	else		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);	VOP_UNLOCK(vp);	if (error)		return (error);	VREF(vp);	vrele(fdp->fd_cdir);	fdp->fd_cdir = vp;	return (0);}/* * Change current working directory (``.''). */struct chdir_args {	char	*path;};/* ARGSUSED */chdir(p, uap, retval)	struct proc *p;	struct chdir_args *uap;	int *retval;{	register struct filedesc *fdp = p->p_fd;	int error;	struct nameidata nd;	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);	if (error = change_dir(&nd, p))		return (error);	vrele(fdp->fd_cdir);	fdp->fd_cdir = nd.ni_vp;	return (0);}/* * Change notion of root (``/'') directory. */struct chroot_args {	char	*path;};/* ARGSUSED */chroot(p, uap, retval)	struct proc *p;	struct chroot_args *uap;	int *retval;{	register struct filedesc *fdp = p->p_fd;	int error;	struct nameidata nd;	if (error = suser(p->p_ucred, &p->p_acflag))		return (error);	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);	if (error = change_dir(&nd, p))		return (error);	if (fdp->fd_rdir != NULL)		vrele(fdp->fd_rdir);	fdp->fd_rdir = nd.ni_vp;	return (0);}/* * Common routine for chroot and chdir. */static intchange_dir(ndp, p)	register struct nameidata *ndp;	struct proc *p;{	struct vnode *vp;	int error;	if (error = namei(ndp))		return (error);	vp = ndp->ni_vp;	if (vp->v_type != VDIR)		error = ENOTDIR;	else		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);	VOP_UNLOCK(vp);	if (error)		vrele(vp);	return (error);}/* * Check permissions, allocate an open file structure, * and call the device open routine if any. */struct open_args {	char	*path;	int	flags;	int	mode;};open(p, uap, retval)	struct proc *p;	register struct open_args *uap;	int *retval;{	register struct filedesc *fdp = p->p_fd;	register struct file *fp;	register struct vnode *vp;	int flags, cmode;	struct file *nfp;	int type, indx, error;	struct flock lf;	struct nameidata nd;	extern struct fileops vnops;	if (error = falloc(p, &nfp, &indx))		return (error);	fp = nfp;	flags = FFLAGS(uap->flags);	cmode = ((uap->mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */	if (error = vn_open(&nd, flags, cmode)) {		ffree(fp);		if ((error == ENODEV || error == ENXIO) &&		    p->p_dupfd >= 0 && 			/* XXX from fdopen */		    (error =		        dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {			*retval = indx;			return (0);		}		if (error == ERESTART)			error = EINTR;		fdp->fd_ofiles[indx] = NULL;		return (error);	}	p->p_dupfd = 0;	vp = nd.ni_vp;	fp->f_flag = flags & FMASK;	fp->f_type = DTYPE_VNODE;	fp->f_ops = &vnops;	fp->f_data = (caddr_t)vp;	if (flags & (O_EXLOCK | O_SHLOCK)) {		lf.l_whence = SEEK_SET;		lf.l_start = 0;		lf.l_len = 0;		if (flags & O_EXLOCK)			lf.l_type = F_WRLCK;		else			lf.l_type = F_RDLCK;		type = F_FLOCK;		if ((flags & FNONBLOCK) == 0)			type |= F_WAIT;		VOP_UNLOCK(vp);		if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);			ffree(fp);			fdp->fd_ofiles[indx] = NULL;			return (error);		}		VOP_LOCK(vp);		fp->f_flag |= FHASLOCK;	}	VOP_UNLOCK(vp);	*retval = indx;	return (0);}#ifdef COMPAT_43/* * Create a file. */struct ocreat_args {	char	*path;	int	mode;};ocreat(p, uap, retval)	struct proc *p;	register struct ocreat_args *uap;	int *retval;{	struct open_args openuap;	openuap.path = uap->path;	openuap.mode = uap->mode;	openuap.flags = O_WRONLY | O_CREAT | O_TRUNC;	return (open(p, &openuap, retval));}#endif /* COMPAT_43 *//* * Create a special file. */struct mknod_args {	char	*path;	int	mode;	int	dev;};/* ARGSUSED */mknod(p, uap, retval)	struct proc *p;	register struct mknod_args *uap;	int *retval;{	register struct vnode *vp;	struct vattr vattr;	int error;	struct nameidata nd;	if (error = suser(p->p_ucred, &p->p_acflag))		return (error);	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->path, p);	if (error = namei(&nd))		return (error);	vp = nd.ni_vp;	if (vp != NULL)		error = EEXIST;	else {		VATTR_NULL(&vattr);		vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;		vattr.va_rdev = uap->dev;		switch (uap->mode & S_IFMT) {		case S_IFMT:	/* used by badsect to flag bad sectors */			vattr.va_type = VBAD;			break;		case S_IFCHR:			vattr.va_type = VCHR;			break;		case S_IFBLK:			vattr.va_type = VBLK;

⌨️ 快捷键说明

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