]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fdescfs/fdesc_vfsops.c
MFV r306669:
[FreeBSD/FreeBSD.git] / sys / fs / fdescfs / fdesc_vfsops.c
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)fdesc_vfsops.c      8.4 (Berkeley) 1/21/94
33  *
34  * $FreeBSD$
35  */
36
37 /*
38  * /dev/fd Filesystem
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/jail.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/proc.h>
51 #include <sys/racct.h>
52 #include <sys/resourcevar.h>
53 #include <sys/vnode.h>
54
55 #include <fs/fdescfs/fdesc.h>
56
57 static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure");
58
59 static vfs_cmount_t     fdesc_cmount;
60 static vfs_mount_t      fdesc_mount;
61 static vfs_unmount_t    fdesc_unmount;
62 static vfs_statfs_t     fdesc_statfs;
63 static vfs_root_t       fdesc_root;
64
65 /*
66  * Compatibility shim for old mount(2) system call.
67  */
68 int
69 fdesc_cmount(struct mntarg *ma, void *data, uint64_t flags)
70 {
71         return kernel_mount(ma, flags);
72 }
73
74 /*
75  * Mount the per-process file descriptors (/dev/fd)
76  */
77 static int
78 fdesc_mount(struct mount *mp)
79 {
80         int error = 0;
81         struct fdescmount *fmp;
82         struct thread *td = curthread;
83         struct vnode *rvp;
84
85         if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_FDESCFS))
86                 return (EPERM);
87
88         /*
89          * Update is a no-op
90          */
91         if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
92                 return (EOPNOTSUPP);
93
94         fmp = malloc(sizeof(struct fdescmount),
95                                 M_FDESCMNT, M_WAITOK);  /* XXX */
96
97         /*
98          * We need to initialize a few bits of our local mount point struct to
99          * avoid confusion in allocvp.
100          */
101         mp->mnt_data = (qaddr_t) fmp;
102         fmp->flags = 0;
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         rvp->v_type = VDIR;
110         rvp->v_vflag |= VV_ROOT;
111         fmp->f_root = rvp;
112         VOP_UNLOCK(rvp, 0);
113         /* XXX -- don't mark as local to work around fts() problems */
114         /*mp->mnt_flag |= MNT_LOCAL;*/
115         vfs_getnewfsid(mp);
116
117         vfs_mountedfrom(mp, "fdescfs");
118         return (0);
119 }
120
121 static int
122 fdesc_unmount(struct mount *mp, int mntflags)
123 {
124         struct fdescmount *fmp;
125         caddr_t data;
126         int error;
127         int flags = 0;
128
129         fmp = (struct fdescmount *)mp->mnt_data;
130         if (mntflags & MNT_FORCE) {
131                 /* The hash mutex protects the private mount flags. */
132                 mtx_lock(&fdesc_hashmtx);
133                 fmp->flags |= FMNT_UNMOUNTF;
134                 mtx_unlock(&fdesc_hashmtx);
135                 flags |= FORCECLOSE;
136         }
137
138         /*
139          * Clear out buffer cache.  I don't think we
140          * ever get anything cached at this level at the
141          * moment, but who knows...
142          *
143          * There is 1 extra root vnode reference corresponding
144          * to f_root.
145          */
146         if ((error = vflush(mp, 1, flags, curthread)) != 0)
147                 return (error);
148
149         /*
150          * Finally, throw away the fdescmount structure. Hold the hashmtx to
151          * protect the fdescmount structure.
152          */
153         mtx_lock(&fdesc_hashmtx);
154         data = mp->mnt_data;
155         mp->mnt_data = NULL;
156         mtx_unlock(&fdesc_hashmtx);
157         free(data, M_FDESCMNT); /* XXX */
158
159         return (0);
160 }
161
162 static int
163 fdesc_root(struct mount *mp, int flags, struct vnode **vpp)
164 {
165         struct vnode *vp;
166
167         /*
168          * Return locked reference to root.
169          */
170         vp = VFSTOFDESC(mp)->f_root;
171         vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
172         *vpp = vp;
173         return (0);
174 }
175
176 static int
177 fdesc_statfs(struct mount *mp, struct statfs *sbp)
178 {
179         struct thread *td;
180         struct filedesc *fdp;
181         int lim;
182         int i;
183         int last;
184         int freefd;
185         uint64_t limit;
186
187         td = curthread;
188
189         /*
190          * Compute number of free file descriptors.
191          * [ Strange results will ensue if the open file
192          * limit is ever reduced below the current number
193          * of open files... ]
194          */
195         lim = lim_cur(td, RLIMIT_NOFILE);
196         fdp = td->td_proc->p_fd;
197         FILEDESC_SLOCK(fdp);
198         limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
199         if (lim > limit)
200                 lim = limit;
201         last = min(fdp->fd_nfiles, lim);
202         freefd = 0;
203         for (i = fdp->fd_freefile; i < last; i++)
204                 if (fdp->fd_ofiles[i].fde_file == NULL)
205                         freefd++;
206
207         /*
208          * Adjust for the fact that the fdesc array may not
209          * have been fully allocated yet.
210          */
211         if (fdp->fd_nfiles < lim)
212                 freefd += (lim - fdp->fd_nfiles);
213         FILEDESC_SUNLOCK(fdp);
214
215         sbp->f_flags = 0;
216         sbp->f_bsize = DEV_BSIZE;
217         sbp->f_iosize = DEV_BSIZE;
218         sbp->f_blocks = 2;              /* 1K to keep df happy */
219         sbp->f_bfree = 0;
220         sbp->f_bavail = 0;
221         sbp->f_files = lim + 1;         /* Allow for "." */
222         sbp->f_ffree = freefd;          /* See comments above */
223         return (0);
224 }
225
226 static struct vfsops fdesc_vfsops = {
227         .vfs_cmount =           fdesc_cmount,
228         .vfs_init =             fdesc_init,
229         .vfs_mount =            fdesc_mount,
230         .vfs_root =             fdesc_root,
231         .vfs_statfs =           fdesc_statfs,
232         .vfs_uninit =           fdesc_uninit,
233         .vfs_unmount =          fdesc_unmount,
234 };
235
236 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC | VFCF_JAIL);