2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1992, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software donated to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95
66 #include <sys/param.h>
67 #include <sys/systm.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>
80 * Mount the per-process file descriptors (/dev/fd)
83 fdesc_mount(struct mount
*mp
, vnode_t devvp
, __unused user_addr_t data
, vfs_context_t context
)
86 struct fdescmount
*fmp
;
92 if (mp
->mnt_flag
& MNT_UPDATE
)
95 error
= fdesc_allocvp(Froot
, FD_ROOT
, mp
, &rvp
, VDIR
);
99 MALLOC(fmp
, struct fdescmount
*, sizeof(struct fdescmount
),
100 M_UFSMNT
, M_WAITOK
); /* XXX */
102 vnode_setnoflush(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
;
112 bzero(mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
);
113 bcopy("fdesc", mp
->mnt_vfsstat
.f_mntfromname
, sizeof("fdesc"));
118 fdesc_start(mp
, flags
, context
)
121 vfs_context_t context
;
127 fdesc_unmount(mp
, mntflags
, context
)
130 vfs_context_t context
;
135 struct vnode
*rvp
= VFSTOFDESC(mp
)->f_root
;
137 if (mntflags
& MNT_FORCE
) {
142 if ( vnode_isinuse(rvp
, 1) && !force
)
144 if ( (error
= vflush(mp
, rvp
, flags
|SKIPSYSTEM
)) && !force
)
148 * And mark for recycle after we drop its reference; it away for future re-use
152 * Release reference on underlying root vnode
156 * Finally, throw away the fdescmount structure
158 _FREE(mp
->mnt_data
, M_UFSMNT
); /* XXX */
165 fdesc_root(mp
, vpp
, context
)
168 vfs_context_t context
;
173 * Return locked reference to root.
175 vp
= VFSTOFDESC(mp
)->f_root
;
182 fdesc_statfs(mp
, sbp
, context
)
184 struct vfsstatfs
*sbp
;
185 vfs_context_t context
;
187 struct proc
*p
= vfs_context_proc(context
);
188 struct filedesc
*fdp
;
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
200 lim
= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
;
202 last
= min(fdp
->fd_nfiles
, lim
);
204 for (i
= fdp
->fd_freefile
; i
< last
; i
++)
205 if (fdp
->fd_ofiles
[i
] == NULL
&&
206 !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
))
210 * Adjust for the fact that the fdesc array may not
211 * have been fully allocated yet.
213 if (fdp
->fd_nfiles
< lim
)
214 freefd
+= (lim
- fdp
->fd_nfiles
);
217 sbp
->f_bsize
= DEV_BSIZE
;
218 sbp
->f_iosize
= DEV_BSIZE
;
219 sbp
->f_blocks
= (uint64_t)2; /* 1K to keep df happy */
222 sbp
->f_files
= (uint64_t)((unsigned long)(lim
+ 1)); /* Allow for "." */
223 sbp
->f_ffree
= (uint64_t)((unsigned long)freefd
); /* See comments above */
229 fdesc_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
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);
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
))
243 struct proc
*p
= vfs_context_proc(context
);
244 struct filedesc
*fdp
;
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
256 lim
= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
;
258 last
= min(fdp
->fd_nfiles
, lim
);
260 for (i
= fdp
->fd_freefile
; i
< last
; i
++)
261 if (fdp
->fd_ofiles
[i
] == NULL
&&
262 !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
))
266 * Adjust for the fact that the fdesc array may not
267 * have been fully allocated yet.
269 if (fdp
->fd_nfiles
< lim
)
270 freefd
+= (lim
- fdp
->fd_nfiles
);
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
);
282 fdesc_sync(mp
, waitfor
, context
)
285 vfs_context_t context
;
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
296 struct vfsops fdesc_vfsops
= {