xnu-344.34.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 *
de355530
A
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.
1c79356b 11 *
de355530
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
de355530
A
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.
1c79356b
A
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;
9bccf70c 139 int force = 0;
1c79356b
A
140 struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
141
9bccf70c 142 if (mntflags & MNT_FORCE) {
1c79356b 143 flags |= FORCECLOSE;
9bccf70c
A
144 force = 1;
145 }
1c79356b 146
9bccf70c 147 if ( (rootvp->v_usecount > 1) && !force )
1c79356b 148 return (EBUSY);
9bccf70c 149 if ( (error = vflush(mp, rootvp, flags)) && !force )
1c79356b
A
150 return (error);
151
152 /*
153 * Release reference on underlying root vnode
154 */
155 vrele(rootvp);
156 /*
157 * And blow it away for future re-use
158 */
159 vgone(rootvp);
160 /*
161 * Finally, throw away the fdescmount structure
162 */
163 _FREE(mp->mnt_data, M_UFSMNT); /* XXX */
164 mp->mnt_data = 0;
165
166 return (0);
167}
168
169int
170fdesc_root(mp, vpp)
171 struct mount *mp;
172 struct vnode **vpp;
173{
174 struct proc *p = current_proc(); /* XXX */
175 struct vnode *vp;
176
177 /*
178 * Return locked reference to root.
179 */
180 vp = VFSTOFDESC(mp)->f_root;
181 VREF(vp);
182 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
183 *vpp = vp;
184 return (0);
185}
186
187int
188fdesc_statfs(mp, sbp, p)
189 struct mount *mp;
190 struct statfs *sbp;
191 struct proc *p;
192{
193 struct filedesc *fdp;
194 int lim;
195 int i;
196 int last;
197 int freefd;
198
199 /*
200 * Compute number of free file descriptors.
201 * [ Strange results will ensue if the open file
202 * limit is ever reduced below the current number
203 * of open files... ]
204 */
205 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
206 fdp = p->p_fd;
207 last = min(fdp->fd_nfiles, lim);
208 freefd = 0;
209 for (i = fdp->fd_freefile; i < last; i++)
210 if (fdp->fd_ofiles[i] == NULL &&
211 !(fdp->fd_ofileflags[i] & UF_RESERVED))
212 freefd++;
213
214 /*
215 * Adjust for the fact that the fdesc array may not
216 * have been fully allocated yet.
217 */
218 if (fdp->fd_nfiles < lim)
219 freefd += (lim - fdp->fd_nfiles);
220
221 sbp->f_flags = 0;
222 sbp->f_bsize = DEV_BSIZE;
223 sbp->f_iosize = DEV_BSIZE;
224 sbp->f_blocks = 2; /* 1K to keep df happy */
225 sbp->f_bfree = 0;
226 sbp->f_bavail = 0;
227 sbp->f_files = lim + 1; /* Allow for "." */
228 sbp->f_ffree = freefd; /* See comments above */
229 if (sbp != &mp->mnt_stat) {
230 sbp->f_type = mp->mnt_vfc->vfc_typenum;
231 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
232 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
233 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
234 }
235 return (0);
236}
237
238int
239fdesc_sync(mp, waitfor)
240 struct mount *mp;
241 int waitfor;
242{
243
244 return (0);
245}
246
247#define fdesc_fhtovp ((int (*) __P((struct mount *, struct fid *, \
248 struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
249#define fdesc_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
250 struct proc *)))eopnotsupp)
251#define fdesc_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
252 size_t, struct proc *)))eopnotsupp)
253#define fdesc_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
254 eopnotsupp)
255#define fdesc_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
256
257struct vfsops fdesc_vfsops = {
258 fdesc_mount,
259 fdesc_start,
260 fdesc_unmount,
261 fdesc_root,
262 fdesc_quotactl,
263 fdesc_statfs,
264 fdesc_sync,
265 fdesc_vget,
266 fdesc_fhtovp,
267 fdesc_vptofh,
268 fdesc_init,
269 fdesc_sysctl,
270};