]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/fdesc/fdesc_vfsops.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / miscfs / fdesc / fdesc_vfsops.c
CommitLineData
1c79356b
A
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.h>
71#include <sys/resourcevar.h>
72#include <sys/filedesc.h>
73#include <sys/vnode.h>
74#include <sys/mount.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 */
82int
83fdesc_mount(mp, path, data, ndp, p)
84 struct mount *mp;
85 char *path;
86 caddr_t data;
87 struct nameidata *ndp;
88 struct proc *p;
89{
90 int error = 0;
91 u_int size;
92 struct fdescmount *fmp;
93 struct vnode *rvp;
94
95 /*
96 * Update is a no-op
97 */
98 if (mp->mnt_flag & MNT_UPDATE)
99 return (EOPNOTSUPP);
100
101 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
102 if (error)
103 return (error);
104
105 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
106 M_UFSMNT, M_WAITOK); /* XXX */
107 rvp->v_type = VDIR;
108 rvp->v_flag |= VROOT;
109 fmp->f_root = rvp;
110 /* XXX -- don't mark as local to work around fts() problems */
111 /*mp->mnt_flag |= MNT_LOCAL;*/
112 mp->mnt_data = (qaddr_t) fmp;
113 vfs_getnewfsid(mp);
114
115 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
116 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
117 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
118 bcopy("fdesc", mp->mnt_stat.f_mntfromname, sizeof("fdesc"));
119 return (0);
120}
121
122int
123fdesc_start(mp, flags, p)
124 struct mount *mp;
125 int flags;
126 struct proc *p;
127{
128 return (0);
129}
130
131int
132fdesc_unmount(mp, mntflags, p)
133 struct mount *mp;
134 int mntflags;
135 struct proc *p;
136{
137 int error;
138 int flags = 0;
139 struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
140
141 if (mntflags & MNT_FORCE)
142 flags |= FORCECLOSE;
143
144 /*
145 * Clear out buffer cache. I don't think we
146 * ever get anything cached at this level at the
147 * moment, but who knows...
148 */
149 if (rootvp->v_usecount > 1)
150 return (EBUSY);
151 if (error = vflush(mp, rootvp, flags))
152 return (error);
153
154 /*
155 * Release reference on underlying root vnode
156 */
157 vrele(rootvp);
158 /*
159 * And blow it away for future re-use
160 */
161 vgone(rootvp);
162 /*
163 * Finally, throw away the fdescmount structure
164 */
165 _FREE(mp->mnt_data, M_UFSMNT); /* XXX */
166 mp->mnt_data = 0;
167
168 return (0);
169}
170
171int
172fdesc_root(mp, vpp)
173 struct mount *mp;
174 struct vnode **vpp;
175{
176 struct proc *p = current_proc(); /* XXX */
177 struct vnode *vp;
178
179 /*
180 * Return locked reference to root.
181 */
182 vp = VFSTOFDESC(mp)->f_root;
183 VREF(vp);
184 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
185 *vpp = vp;
186 return (0);
187}
188
189int
190fdesc_statfs(mp, sbp, p)
191 struct mount *mp;
192 struct statfs *sbp;
193 struct proc *p;
194{
195 struct filedesc *fdp;
196 int lim;
197 int i;
198 int last;
199 int freefd;
200
201 /*
202 * Compute number of free file descriptors.
203 * [ Strange results will ensue if the open file
204 * limit is ever reduced below the current number
205 * of open files... ]
206 */
207 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
208 fdp = p->p_fd;
209 last = min(fdp->fd_nfiles, lim);
210 freefd = 0;
211 for (i = fdp->fd_freefile; i < last; i++)
212 if (fdp->fd_ofiles[i] == NULL &&
213 !(fdp->fd_ofileflags[i] & UF_RESERVED))
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
223 sbp->f_flags = 0;
224 sbp->f_bsize = DEV_BSIZE;
225 sbp->f_iosize = DEV_BSIZE;
226 sbp->f_blocks = 2; /* 1K to keep df happy */
227 sbp->f_bfree = 0;
228 sbp->f_bavail = 0;
229 sbp->f_files = lim + 1; /* Allow for "." */
230 sbp->f_ffree = freefd; /* See comments above */
231 if (sbp != &mp->mnt_stat) {
232 sbp->f_type = mp->mnt_vfc->vfc_typenum;
233 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
234 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
235 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
236 }
237 return (0);
238}
239
240int
241fdesc_sync(mp, waitfor)
242 struct mount *mp;
243 int waitfor;
244{
245
246 return (0);
247}
248
249#define fdesc_fhtovp ((int (*) __P((struct mount *, struct fid *, \
250 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
251#define fdesc_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
252 struct proc *)))eopnotsupp)
253#define fdesc_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
254 size_t, struct proc *)))eopnotsupp)
255#define fdesc_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
256 eopnotsupp)
257#define fdesc_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
258
259struct vfsops fdesc_vfsops = {
260 fdesc_mount,
261 fdesc_start,
262 fdesc_unmount,
263 fdesc_root,
264 fdesc_quotactl,
265 fdesc_statfs,
266 fdesc_sync,
267 fdesc_vget,
268 fdesc_fhtovp,
269 fdesc_vptofh,
270 fdesc_init,
271 fdesc_sysctl,
272};