xref: /freebsd/sys/fs/fdescfs/fdesc_vfsops.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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 
35 /*
36  * /dev/fd Filesystem
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/filedesc.h>
42 #include <sys/kernel.h>
43 #include <sys/jail.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/proc.h>
49 #include <sys/racct.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vnode.h>
52 
53 #include <fs/fdescfs/fdesc.h>
54 
55 static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure");
56 
57 static vfs_cmount_t	fdesc_cmount;
58 static vfs_mount_t	fdesc_mount;
59 static vfs_unmount_t	fdesc_unmount;
60 static vfs_statfs_t	fdesc_statfs;
61 static vfs_root_t	fdesc_root;
62 
63 /*
64  * Compatibility shim for old mount(2) system call.
65  */
66 int
67 fdesc_cmount(struct mntarg *ma, void *data, uint64_t flags)
68 {
69 
70 	return kernel_mount(ma, flags);
71 }
72 
73 /*
74  * Mount the per-process file descriptors (/dev/fd)
75  */
76 static int
77 fdesc_mount(struct mount *mp)
78 {
79 	struct fdescmount *fmp;
80 	struct vnode *rvp;
81 	int error;
82 
83 	/*
84 	 * Update is a no-op
85 	 */
86 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
87 		return (EOPNOTSUPP);
88 
89 	fmp = malloc(sizeof(struct fdescmount), M_FDESCMNT, M_WAITOK);
90 
91 	/*
92 	 * We need to initialize a few bits of our local mount point struct to
93 	 * avoid confusion in allocvp.
94 	 */
95 	mp->mnt_data = fmp;
96 	fmp->flags = 0;
97 	if (vfs_getopt(mp->mnt_optnew, "linrdlnk", NULL, NULL) == 0)
98 		fmp->flags |= FMNT_LINRDLNKF;
99 	if (vfs_getopt(mp->mnt_optnew, "rdlnk", NULL, NULL) == 0)
100 		fmp->flags |= FMNT_RDLNKF;
101 	if (vfs_getopt(mp->mnt_optnew, "nodup", NULL, NULL) == 0)
102 		fmp->flags |= FMNT_NODUP;
103 	error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
104 	if (error) {
105 		free(fmp, M_FDESCMNT);
106 		mp->mnt_data = NULL;
107 		return (error);
108 	}
109 	VN_LOCK_ASHARE(rvp);
110 	rvp->v_type = VDIR;
111 	rvp->v_vflag |= VV_ROOT;
112 	fmp->f_root = rvp;
113 	VOP_UNLOCK(rvp);
114 
115 	MNT_ILOCK(mp);
116 	/* XXX -- don't mark as local to work around fts() problems */
117 	/*mp->mnt_flag |= MNT_LOCAL;*/
118 	/*
119 	 * Enable shared locking so that there is no contention on the root
120 	 * vnode. Note only root vnode enables shared locking for itself,
121 	 * so this end up being a nop for the rest.
122 	 */
123 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
124 	MNT_IUNLOCK(mp);
125 
126 	vfs_getnewfsid(mp);
127 
128 	vfs_mountedfrom(mp, "fdescfs");
129 	return (0);
130 }
131 
132 static int
133 fdesc_unmount(struct mount *mp, int mntflags)
134 {
135 	struct fdescmount *fmp;
136 	int error, flags;
137 
138 	flags = 0;
139 	fmp = mp->mnt_data;
140 	if (mntflags & MNT_FORCE) {
141 		/* The hash mutex protects the private mount flags. */
142 		mtx_lock(&fdesc_hashmtx);
143 		fmp->flags |= FMNT_UNMOUNTF;
144 		mtx_unlock(&fdesc_hashmtx);
145 		flags |= FORCECLOSE;
146 	}
147 
148 	/*
149 	 * Clear out buffer cache.  I don't think we
150 	 * ever get anything cached at this level at the
151 	 * moment, but who knows...
152 	 *
153 	 * There is 1 extra root vnode reference corresponding
154 	 * to f_root.
155 	 */
156 	if ((error = vflush(mp, 1, flags, curthread)) != 0)
157 		return (error);
158 
159 	/*
160 	 * Finally, throw away the fdescmount structure.
161 	 */
162 	mp->mnt_data = NULL;
163 	free(fmp, M_FDESCMNT);
164 	return (0);
165 }
166 
167 static int
168 fdesc_root(struct mount *mp, int flags, struct vnode **vpp)
169 {
170 	struct vnode *vp;
171 
172 	/*
173 	 * Return locked reference to root.
174 	 */
175 	vp = VFSTOFDESC(mp)->f_root;
176 	vget(vp, flags | LK_RETRY);
177 	*vpp = vp;
178 	return (0);
179 }
180 
181 static int
182 fdesc_statfs(struct mount *mp, struct statfs *sbp)
183 {
184 	struct thread *td;
185 	struct filedesc *fdp;
186 	int lim;
187 	int i;
188 	int last;
189 	int freefd;
190 	uint64_t limit;
191 
192 	td = curthread;
193 
194 	/*
195 	 * Compute number of free file descriptors.
196 	 * [ Strange results will ensue if the open file
197 	 * limit is ever reduced below the current number
198 	 * of open files... ]
199 	 */
200 	lim = lim_cur(td, RLIMIT_NOFILE);
201 	fdp = td->td_proc->p_fd;
202 	FILEDESC_SLOCK(fdp);
203 	limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
204 	if (lim > limit)
205 		lim = limit;
206 	last = min(fdp->fd_nfiles, lim);
207 	freefd = 0;
208 	for (i = fdp->fd_freefile; i < last; i++)
209 		if (fdp->fd_ofiles[i].fde_file == NULL)
210 			freefd++;
211 
212 	/*
213 	 * Adjust for the fact that the fdesc array may not
214 	 * have been fully allocated yet.
215 	 */
216 	if (fdp->fd_nfiles < lim)
217 		freefd += (lim - fdp->fd_nfiles);
218 	FILEDESC_SUNLOCK(fdp);
219 
220 	sbp->f_flags = mp->mnt_flag & MNT_IGNORE;
221 	sbp->f_bsize = DEV_BSIZE;
222 	sbp->f_iosize = DEV_BSIZE;
223 	sbp->f_blocks = 2;		/* 1K to keep df happy */
224 	sbp->f_bfree = 2;
225 	sbp->f_bavail = 2;
226 	sbp->f_files = lim + 1;		/* Allow for "." */
227 	sbp->f_ffree = freefd;		/* See comments above */
228 	return (0);
229 }
230 
231 static struct vfsops fdesc_vfsops = {
232 	.vfs_cmount =		fdesc_cmount,
233 	.vfs_init =		fdesc_init,
234 	.vfs_mount =		fdesc_mount,
235 	.vfs_root =		fdesc_root,
236 	.vfs_statfs =		fdesc_statfs,
237 	.vfs_uninit =		fdesc_uninit,
238 	.vfs_unmount =		fdesc_unmount,
239 };
240 
241 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC | VFCF_JAIL);
242