]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/fs/fdescfs/fdesc_vfsops.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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  * 4. 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/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/proc.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, int flags)
68 {
69         return kernel_mount(ma, flags);
70 }
71
72 /*
73  * Mount the per-process file descriptors (/dev/fd)
74  */
75 static int
76 fdesc_mount(struct mount *mp)
77 {
78         int error = 0;
79         struct fdescmount *fmp;
80         struct vnode *rvp;
81
82         /*
83          * Update is a no-op
84          */
85         if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
86                 return (EOPNOTSUPP);
87
88         fmp = malloc(sizeof(struct fdescmount),
89                                 M_FDESCMNT, M_WAITOK);  /* XXX */
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 = (qaddr_t) fmp;
96         fmp->flags = 0;
97         error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
98         if (error) {
99                 free(fmp, M_FDESCMNT);
100                 mp->mnt_data = 0;
101                 return (error);
102         }
103         rvp->v_type = VDIR;
104         rvp->v_vflag |= VV_ROOT;
105         fmp->f_root = rvp;
106         VOP_UNLOCK(rvp, 0);
107         /* XXX -- don't mark as local to work around fts() problems */
108         /*mp->mnt_flag |= MNT_LOCAL;*/
109         MNT_ILOCK(mp);
110         mp->mnt_kern_flag |= MNTK_MPSAFE;
111         MNT_IUNLOCK(mp);
112         vfs_getnewfsid(mp);
113
114         vfs_mountedfrom(mp, "fdescfs");
115         return (0);
116 }
117
118 static int
119 fdesc_unmount(mp, mntflags)
120         struct mount *mp;
121         int mntflags;
122 {
123         struct fdescmount *fmp;
124         caddr_t data;
125         int error;
126         int flags = 0;
127
128         fmp = (struct fdescmount *)mp->mnt_data;
129         if (mntflags & MNT_FORCE) {
130                 /* The hash mutex protects the private mount flags. */
131                 mtx_lock(&fdesc_hashmtx);
132                 fmp->flags |= FMNT_UNMOUNTF;
133                 mtx_unlock(&fdesc_hashmtx);
134                 flags |= FORCECLOSE;
135         }
136
137         /*
138          * Clear out buffer cache.  I don't think we
139          * ever get anything cached at this level at the
140          * moment, but who knows...
141          *
142          * There is 1 extra root vnode reference corresponding
143          * to f_root.
144          */
145         if ((error = vflush(mp, 1, flags, curthread)) != 0)
146                 return (error);
147
148         /*
149          * Finally, throw away the fdescmount structure. Hold the hashmtx to
150          * protect the fdescmount structure.
151          */
152         mtx_lock(&fdesc_hashmtx);
153         data = mp->mnt_data;
154         mp->mnt_data = 0;
155         mtx_unlock(&fdesc_hashmtx);
156         free(data, M_FDESCMNT); /* XXX */
157
158         return (0);
159 }
160
161 static int
162 fdesc_root(mp, flags, vpp)
163         struct mount *mp;
164         int flags;
165         struct vnode **vpp;
166 {
167         struct vnode *vp;
168
169         /*
170          * Return locked reference to root.
171          */
172         vp = VFSTOFDESC(mp)->f_root;
173         vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
174         *vpp = vp;
175         return (0);
176 }
177
178 static int
179 fdesc_statfs(mp, sbp)
180         struct mount *mp;
181         struct statfs *sbp;
182 {
183         struct thread *td;
184         struct filedesc *fdp;
185         int lim;
186         int i;
187         int last;
188         int freefd;
189
190         td = curthread;
191
192         /*
193          * Compute number of free file descriptors.
194          * [ Strange results will ensue if the open file
195          * limit is ever reduced below the current number
196          * of open files... ]
197          */
198         PROC_LOCK(td->td_proc);
199         lim = lim_cur(td->td_proc, RLIMIT_NOFILE);
200         PROC_UNLOCK(td->td_proc);
201         fdp = td->td_proc->p_fd;
202         FILEDESC_SLOCK(fdp);
203         last = min(fdp->fd_nfiles, lim);
204         freefd = 0;
205         for (i = fdp->fd_freefile; i < last; i++)
206                 if (fdp->fd_ofiles[i] == NULL)
207                         freefd++;
208
209         /*
210          * Adjust for the fact that the fdesc array may not
211          * have been fully allocated yet.
212          */
213         if (fdp->fd_nfiles < lim)
214                 freefd += (lim - fdp->fd_nfiles);
215         FILEDESC_SUNLOCK(fdp);
216
217         sbp->f_flags = 0;
218         sbp->f_bsize = DEV_BSIZE;
219         sbp->f_iosize = DEV_BSIZE;
220         sbp->f_blocks = 2;              /* 1K to keep df happy */
221         sbp->f_bfree = 0;
222         sbp->f_bavail = 0;
223         sbp->f_files = lim + 1;         /* Allow for "." */
224         sbp->f_ffree = freefd;          /* See comments above */
225         return (0);
226 }
227
228 static struct vfsops fdesc_vfsops = {
229         .vfs_cmount =           fdesc_cmount,
230         .vfs_init =             fdesc_init,
231         .vfs_mount =            fdesc_mount,
232         .vfs_root =             fdesc_root,
233         .vfs_statfs =           fdesc_statfs,
234         .vfs_uninit =           fdesc_uninit,
235         .vfs_unmount =          fdesc_unmount,
236 };
237
238 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC);