]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/fdesc/fdesc_vfsops.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / miscfs / fdesc / fdesc_vfsops.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1992, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software donated to Berkeley by
28 * Jan-Simon Pendry.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95
59 *
60 */
61
62 /*
63 * /dev/fd Filesystem
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/time.h>
69 #include <sys/types.h>
70 #include <sys/proc_internal.h>
71 #include <sys/resourcevar.h>
72 #include <sys/filedesc.h>
73 #include <sys/vnode.h>
74 #include <sys/mount_internal.h>
75 #include <sys/namei.h>
76 #include <sys/malloc.h>
77 #include <miscfs/fdesc/fdesc.h>
78
79 /*
80 * Mount the per-process file descriptors (/dev/fd)
81 */
82 int
83 fdesc_mount(struct mount *mp, vnode_t devvp, __unused user_addr_t data, vfs_context_t context)
84 {
85 int error = 0;
86 struct fdescmount *fmp;
87 struct vnode *rvp;
88
89 /*
90 * Update is a no-op
91 */
92 if (mp->mnt_flag & MNT_UPDATE)
93 return (ENOTSUP);
94
95 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp, VDIR);
96 if (error)
97 return (error);
98
99 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
100 M_UFSMNT, M_WAITOK); /* XXX */
101
102 vnode_setnoflush(rvp);
103 vnode_ref(rvp);
104 vnode_put(rvp);
105
106 fmp->f_root = rvp;
107 /* XXX -- don't mark as local to work around fts() problems */
108 /*mp->mnt_flag |= MNT_LOCAL;*/
109 mp->mnt_data = (qaddr_t) fmp;
110 vfs_getnewfsid(mp);
111
112 bzero(mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN);
113 bcopy("fdesc", mp->mnt_vfsstat.f_mntfromname, sizeof("fdesc"));
114 return (0);
115 }
116
117 int
118 fdesc_start(mp, flags, context)
119 struct mount *mp;
120 int flags;
121 vfs_context_t context;
122 {
123 return (0);
124 }
125
126 int
127 fdesc_unmount(mp, mntflags, context)
128 struct mount *mp;
129 int mntflags;
130 vfs_context_t context;
131 {
132 int error;
133 int flags = 0;
134 int force = 0;
135 struct vnode *rvp = VFSTOFDESC(mp)->f_root;
136
137 if (mntflags & MNT_FORCE) {
138 flags |= FORCECLOSE;
139 force = 1;
140 }
141
142 if ( vnode_isinuse(rvp, 1) && !force )
143 return (EBUSY);
144 if ( (error = vflush(mp, rvp, flags|SKIPSYSTEM)) && !force )
145 return (error);
146
147 /*
148 * And mark for recycle after we drop its reference; it away for future re-use
149 */
150 vnode_recycle(rvp);
151 /*
152 * Release reference on underlying root vnode
153 */
154 vnode_rele(rvp);
155 /*
156 * Finally, throw away the fdescmount structure
157 */
158 _FREE(mp->mnt_data, M_UFSMNT); /* XXX */
159 mp->mnt_data = 0;
160
161 return (0);
162 }
163
164 int
165 fdesc_root(mp, vpp, context)
166 struct mount *mp;
167 struct vnode **vpp;
168 vfs_context_t context;
169 {
170 struct vnode *vp;
171
172 /*
173 * Return locked reference to root.
174 */
175 vp = VFSTOFDESC(mp)->f_root;
176 vnode_get(vp);
177 *vpp = vp;
178 return (0);
179 }
180
181 int
182 fdesc_statfs(mp, sbp, context)
183 struct mount *mp;
184 struct vfsstatfs *sbp;
185 vfs_context_t context;
186 {
187 struct proc *p = vfs_context_proc(context);
188 struct filedesc *fdp;
189 int lim;
190 int i;
191 int last;
192 int freefd;
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 = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
201 fdp = p->p_fd;
202 last = min(fdp->fd_nfiles, lim);
203 freefd = 0;
204 for (i = fdp->fd_freefile; i < last; i++)
205 if (fdp->fd_ofiles[i] == NULL &&
206 !(fdp->fd_ofileflags[i] & UF_RESERVED))
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
216 sbp->f_flags = 0;
217 sbp->f_bsize = DEV_BSIZE;
218 sbp->f_iosize = DEV_BSIZE;
219 sbp->f_blocks = (uint64_t)2; /* 1K to keep df happy */
220 sbp->f_bfree = 0;
221 sbp->f_bavail = 0;
222 sbp->f_files = (uint64_t)((unsigned long)(lim + 1)); /* Allow for "." */
223 sbp->f_ffree = (uint64_t)((unsigned long)freefd); /* See comments above */
224
225 return (0);
226 }
227
228 static int
229 fdesc_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
230 {
231 VFSATTR_RETURN(fsap, f_bsize, DEV_BSIZE);
232 VFSATTR_RETURN(fsap, f_iosize, DEV_BSIZE);
233 VFSATTR_RETURN(fsap, f_blocks, 2);
234 VFSATTR_RETURN(fsap, f_bfree, 0);
235 VFSATTR_RETURN(fsap, f_bavail, 0);
236 VFSATTR_RETURN(fsap, f_fssubtype, 0);
237
238 if (VFSATTR_IS_ACTIVE(fsap, f_objcount) ||
239 VFSATTR_IS_ACTIVE(fsap, f_maxobjcount) ||
240 VFSATTR_IS_ACTIVE(fsap, f_files) ||
241 VFSATTR_IS_ACTIVE(fsap, f_ffree))
242 {
243 struct proc *p = vfs_context_proc(context);
244 struct filedesc *fdp;
245 int lim;
246 int i;
247 int last;
248 int freefd;
249
250 /*
251 * Compute number of free file descriptors.
252 * [ Strange results will ensue if the open file
253 * limit is ever reduced below the current number
254 * of open files... ]
255 */
256 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
257 fdp = p->p_fd;
258 last = min(fdp->fd_nfiles, lim);
259 freefd = 0;
260 for (i = fdp->fd_freefile; i < last; i++)
261 if (fdp->fd_ofiles[i] == NULL &&
262 !(fdp->fd_ofileflags[i] & UF_RESERVED))
263 freefd++;
264
265 /*
266 * Adjust for the fact that the fdesc array may not
267 * have been fully allocated yet.
268 */
269 if (fdp->fd_nfiles < lim)
270 freefd += (lim - fdp->fd_nfiles);
271
272 VFSATTR_RETURN(fsap, f_objcount, lim+1);
273 VFSATTR_RETURN(fsap, f_maxobjcount, lim+1);
274 VFSATTR_RETURN(fsap, f_files, lim+1);
275 VFSATTR_RETURN(fsap, f_ffree, freefd);
276 }
277
278 return 0;
279 }
280
281 int
282 fdesc_sync(mp, waitfor, context)
283 struct mount *mp;
284 int waitfor;
285 vfs_context_t context;
286 {
287
288 return (0);
289 }
290
291 #define fdesc_fhtovp (int (*) (mount_t, int, unsigned char *, vnode_t *, vfs_context_t))eopnotsupp
292 #define fdesc_sysctl (int (*) (int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t))eopnotsupp
293 #define fdesc_vget (int (*) (mount_t, ino64_t, vnode_t *, vfs_context_t))eopnotsupp
294 #define fdesc_vptofh (int (*) (vnode_t, int *, unsigned char *, vfs_context_t))eopnotsupp
295
296 struct vfsops fdesc_vfsops = {
297 fdesc_mount,
298 fdesc_start,
299 fdesc_unmount,
300 fdesc_root,
301 NULL, /* quotactl */
302 fdesc_vfs_getattr,
303 fdesc_sync,
304 fdesc_vget,
305 fdesc_fhtovp,
306 fdesc_vptofh,
307 fdesc_init,
308 fdesc_sysctl
309 };