xref: /freebsd/sys/fs/fdescfs/fdesc_vfsops.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)fdesc_vfsops.c	8.4 (Berkeley) 1/21/94
37  *
38  * $FreeBSD$
39  */
40 
41 /*
42  * /dev/fd Filesystem
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
56 
57 #include <fs/fdescfs/fdesc.h>
58 
59 static MALLOC_DEFINE(M_FDESCMNT, "FDESC mount", "FDESC mount structure");
60 
61 static vfs_nmount_t	fdesc_mount;
62 static vfs_unmount_t	fdesc_unmount;
63 static vfs_statfs_t	fdesc_statfs;
64 
65 /*
66  * Mount the per-process file descriptors (/dev/fd)
67  */
68 static int
69 fdesc_mount(mp, ndp, td)
70 	struct mount *mp;
71 	struct nameidata *ndp;
72 	struct thread *td;
73 {
74 	int error = 0;
75 	struct fdescmount *fmp;
76 	struct vnode *rvp;
77 
78 	/*
79 	 * Update is a no-op
80 	 */
81 	if (mp->mnt_flag & MNT_UPDATE)
82 		return (EOPNOTSUPP);
83 
84 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp, td);
85 	if (error)
86 		return (error);
87 
88 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
89 				M_FDESCMNT, M_WAITOK);	/* XXX */
90 	rvp->v_type = VDIR;
91 	rvp->v_vflag |= VV_ROOT;
92 	fmp->f_root = rvp;
93 	/* XXX -- don't mark as local to work around fts() problems */
94 	/*mp->mnt_flag |= MNT_LOCAL;*/
95 	mp->mnt_data = (qaddr_t) fmp;
96 	vfs_getnewfsid(mp);
97 
98 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
99 	bcopy("fdescfs", mp->mnt_stat.f_mntfromname, sizeof("fdescfs"));
100 	(void)fdesc_statfs(mp, &mp->mnt_stat, td);
101 	return (0);
102 }
103 
104 static int
105 fdesc_unmount(mp, mntflags, td)
106 	struct mount *mp;
107 	int mntflags;
108 	struct thread *td;
109 {
110 	int error;
111 	int flags = 0;
112 
113 	if (mntflags & MNT_FORCE)
114 		flags |= FORCECLOSE;
115 
116 	/*
117 	 * Clear out buffer cache.  I don't think we
118 	 * ever get anything cached at this level at the
119 	 * moment, but who knows...
120 	 *
121 	 * There is 1 extra root vnode reference corresponding
122 	 * to f_root.
123 	 */
124 	if ((error = vflush(mp, 1, flags)) != 0)
125 		return (error);
126 
127 	/*
128 	 * Finally, throw away the fdescmount structure
129 	 */
130 	free(mp->mnt_data, M_FDESCMNT);	/* XXX */
131 	mp->mnt_data = 0;
132 
133 	return (0);
134 }
135 
136 int
137 fdesc_root(mp, vpp)
138 	struct mount *mp;
139 	struct vnode **vpp;
140 {
141 	struct thread *td = curthread;	/* XXX */
142 	struct vnode *vp;
143 
144 	/*
145 	 * Return locked reference to root.
146 	 */
147 	vp = VFSTOFDESC(mp)->f_root;
148 	VREF(vp);
149 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
150 	*vpp = vp;
151 	return (0);
152 }
153 
154 static int
155 fdesc_statfs(mp, sbp, td)
156 	struct mount *mp;
157 	struct statfs *sbp;
158 	struct thread *td;
159 {
160 	struct filedesc *fdp;
161 	int lim;
162 	int i;
163 	int last;
164 	int freefd;
165 
166 	/*
167 	 * Compute number of free file descriptors.
168 	 * [ Strange results will ensue if the open file
169 	 * limit is ever reduced below the current number
170 	 * of open files... ]
171 	 */
172 	lim = td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur;
173 	fdp = td->td_proc->p_fd;
174 	FILEDESC_LOCK(fdp);
175 	last = min(fdp->fd_nfiles, lim);
176 	freefd = 0;
177 	for (i = fdp->fd_freefile; i < last; i++)
178 		if (fdp->fd_ofiles[i] == NULL)
179 			freefd++;
180 
181 	/*
182 	 * Adjust for the fact that the fdesc array may not
183 	 * have been fully allocated yet.
184 	 */
185 	if (fdp->fd_nfiles < lim)
186 		freefd += (lim - fdp->fd_nfiles);
187 	FILEDESC_UNLOCK(fdp);
188 
189 	sbp->f_flags = 0;
190 	sbp->f_bsize = DEV_BSIZE;
191 	sbp->f_iosize = DEV_BSIZE;
192 	sbp->f_blocks = 2;		/* 1K to keep df happy */
193 	sbp->f_bfree = 0;
194 	sbp->f_bavail = 0;
195 	sbp->f_files = lim + 1;		/* Allow for "." */
196 	sbp->f_ffree = freefd;		/* See comments above */
197 	if (sbp != &mp->mnt_stat) {
198 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
199 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
200 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
201 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
202 	}
203 	return (0);
204 }
205 
206 static struct vfsops fdesc_vfsops = {
207 	.vfs_init =		fdesc_init,
208 	.vfs_nmount =		fdesc_mount,
209 	.vfs_root =		fdesc_root,
210 	.vfs_statfs =		fdesc_statfs,
211 	.vfs_unmount =		fdesc_unmount,
212 };
213 
214 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC);
215