xref: /freebsd/sys/fs/fdescfs/fdesc_vfsops.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)fdesc_vfsops.c	8.4 (Berkeley) 1/21/94
35  *
36  * $FreeBSD$
37  */
38 
39 /*
40  * /dev/fd Filesystem
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
47 #include <sys/jail.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/racct.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_cmount_t	fdesc_cmount;
62 static vfs_mount_t	fdesc_mount;
63 static vfs_unmount_t	fdesc_unmount;
64 static vfs_statfs_t	fdesc_statfs;
65 static vfs_root_t	fdesc_root;
66 
67 /*
68  * Compatibility shim for old mount(2) system call.
69  */
70 int
71 fdesc_cmount(struct mntarg *ma, void *data, uint64_t flags)
72 {
73 
74 	return kernel_mount(ma, flags);
75 }
76 
77 /*
78  * Mount the per-process file descriptors (/dev/fd)
79  */
80 static int
81 fdesc_mount(struct mount *mp)
82 {
83 	struct fdescmount *fmp;
84 	struct vnode *rvp;
85 	int error;
86 
87 	/*
88 	 * Update is a no-op
89 	 */
90 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
91 		return (EOPNOTSUPP);
92 
93 	fmp = malloc(sizeof(struct fdescmount), M_FDESCMNT, M_WAITOK);
94 
95 	/*
96 	 * We need to initialize a few bits of our local mount point struct to
97 	 * avoid confusion in allocvp.
98 	 */
99 	mp->mnt_data = fmp;
100 	fmp->flags = 0;
101 	if (vfs_getopt(mp->mnt_optnew, "linrdlnk", NULL, NULL) == 0)
102 		fmp->flags |= FMNT_LINRDLNKF;
103 	if (vfs_getopt(mp->mnt_optnew, "rdlnk", NULL, NULL) == 0)
104 		fmp->flags |= FMNT_RDLNKF;
105 	if (vfs_getopt(mp->mnt_optnew, "nodup", NULL, NULL) == 0)
106 		fmp->flags |= FMNT_NODUP;
107 	error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
108 	if (error) {
109 		free(fmp, M_FDESCMNT);
110 		mp->mnt_data = NULL;
111 		return (error);
112 	}
113 	VN_LOCK_ASHARE(rvp);
114 	rvp->v_type = VDIR;
115 	rvp->v_vflag |= VV_ROOT;
116 	fmp->f_root = rvp;
117 	VOP_UNLOCK(rvp);
118 
119 	MNT_ILOCK(mp);
120 	/* XXX -- don't mark as local to work around fts() problems */
121 	/*mp->mnt_flag |= MNT_LOCAL;*/
122 	/*
123 	 * Enable shared locking so that there is no contention on the root
124 	 * vnode. Note only root vnode enables shared locking for itself,
125 	 * so this end up being a nop for the rest.
126 	 */
127 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
128 	MNT_IUNLOCK(mp);
129 
130 	vfs_getnewfsid(mp);
131 
132 	vfs_mountedfrom(mp, "fdescfs");
133 	return (0);
134 }
135 
136 static int
137 fdesc_unmount(struct mount *mp, int mntflags)
138 {
139 	struct fdescmount *fmp;
140 	int error, flags;
141 
142 	flags = 0;
143 	fmp = mp->mnt_data;
144 	if (mntflags & MNT_FORCE) {
145 		/* The hash mutex protects the private mount flags. */
146 		mtx_lock(&fdesc_hashmtx);
147 		fmp->flags |= FMNT_UNMOUNTF;
148 		mtx_unlock(&fdesc_hashmtx);
149 		flags |= FORCECLOSE;
150 	}
151 
152 	/*
153 	 * Clear out buffer cache.  I don't think we
154 	 * ever get anything cached at this level at the
155 	 * moment, but who knows...
156 	 *
157 	 * There is 1 extra root vnode reference corresponding
158 	 * to f_root.
159 	 */
160 	if ((error = vflush(mp, 1, flags, curthread)) != 0)
161 		return (error);
162 
163 	/*
164 	 * Finally, throw away the fdescmount structure.
165 	 */
166 	mp->mnt_data = NULL;
167 	free(fmp, M_FDESCMNT);
168 	return (0);
169 }
170 
171 static int
172 fdesc_root(struct mount *mp, int flags, struct vnode **vpp)
173 {
174 	struct vnode *vp;
175 
176 	/*
177 	 * Return locked reference to root.
178 	 */
179 	vp = VFSTOFDESC(mp)->f_root;
180 	vget(vp, flags | LK_RETRY);
181 	*vpp = vp;
182 	return (0);
183 }
184 
185 static int
186 fdesc_statfs(struct mount *mp, struct statfs *sbp)
187 {
188 	struct thread *td;
189 	struct filedesc *fdp;
190 	int lim;
191 	int i;
192 	int last;
193 	int freefd;
194 	uint64_t limit;
195 
196 	td = curthread;
197 
198 	/*
199 	 * Compute number of free file descriptors.
200 	 * [ Strange results will ensue if the open file
201 	 * limit is ever reduced below the current number
202 	 * of open files... ]
203 	 */
204 	lim = lim_cur(td, RLIMIT_NOFILE);
205 	fdp = td->td_proc->p_fd;
206 	FILEDESC_SLOCK(fdp);
207 	limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
208 	if (lim > limit)
209 		lim = limit;
210 	last = min(fdp->fd_nfiles, lim);
211 	freefd = 0;
212 	for (i = fdp->fd_freefile; i < last; i++)
213 		if (fdp->fd_ofiles[i].fde_file == NULL)
214 			freefd++;
215 
216 	/*
217 	 * Adjust for the fact that the fdesc array may not
218 	 * have been fully allocated yet.
219 	 */
220 	if (fdp->fd_nfiles < lim)
221 		freefd += (lim - fdp->fd_nfiles);
222 	FILEDESC_SUNLOCK(fdp);
223 
224 	sbp->f_flags = 0;
225 	sbp->f_bsize = DEV_BSIZE;
226 	sbp->f_iosize = DEV_BSIZE;
227 	sbp->f_blocks = 2;		/* 1K to keep df happy */
228 	sbp->f_bfree = 2;
229 	sbp->f_bavail = 2;
230 	sbp->f_files = lim + 1;		/* Allow for "." */
231 	sbp->f_ffree = freefd;		/* See comments above */
232 	return (0);
233 }
234 
235 static struct vfsops fdesc_vfsops = {
236 	.vfs_cmount =		fdesc_cmount,
237 	.vfs_init =		fdesc_init,
238 	.vfs_mount =		fdesc_mount,
239 	.vfs_root =		fdesc_root,
240 	.vfs_statfs =		fdesc_statfs,
241 	.vfs_uninit =		fdesc_uninit,
242 	.vfs_unmount =		fdesc_unmount,
243 };
244 
245 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC | VFCF_JAIL);
246