1 /* $NetBSD: fdesc_vfsops.c,v 1.61 2006/10/12 01:32:26 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95 35 * 36 * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp # 37 */ 38 39 /* 40 * /dev/fd Filesystem 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.61 2006/10/12 01:32:26 christos Exp $"); 45 46 #if defined(_KERNEL_OPT) 47 #include "opt_compat_netbsd.h" 48 #endif 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/sysctl.h> 53 #include <sys/time.h> 54 #include <sys/proc.h> 55 #include <sys/resourcevar.h> 56 #include <sys/filedesc.h> 57 #include <sys/vnode.h> 58 #include <sys/mount.h> 59 #include <sys/dirent.h> 60 #include <sys/namei.h> 61 #include <sys/malloc.h> 62 #include <sys/kauth.h> 63 64 #include <miscfs/fdesc/fdesc.h> 65 66 int fdesc_mount(struct mount *, const char *, void *, 67 struct nameidata *, struct lwp *); 68 int fdesc_start(struct mount *, int, struct lwp *); 69 int fdesc_unmount(struct mount *, int, struct lwp *); 70 int fdesc_quotactl(struct mount *, int, uid_t, void *, 71 struct lwp *); 72 int fdesc_statvfs(struct mount *, struct statvfs *, struct lwp *); 73 int fdesc_sync(struct mount *, int, kauth_cred_t, struct lwp *); 74 int fdesc_vget(struct mount *, ino_t, struct vnode **); 75 76 /* 77 * Mount the per-process file descriptors (/dev/fd) 78 */ 79 int 80 fdesc_mount(struct mount *mp, const char *path, void *data __unused, 81 struct nameidata *ndp __unused, struct lwp *l) 82 { 83 int error = 0; 84 struct fdescmount *fmp; 85 struct vnode *rvp; 86 87 if (mp->mnt_flag & MNT_GETARGS) 88 return 0; 89 /* 90 * Update is a no-op 91 */ 92 if (mp->mnt_flag & MNT_UPDATE) 93 return (EOPNOTSUPP); 94 95 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp); 96 if (error) 97 return (error); 98 99 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount), 100 M_UFSMNT, M_WAITOK); /* XXX */ 101 rvp->v_type = VDIR; 102 rvp->v_flag |= VROOT; 103 fmp->f_root = rvp; 104 mp->mnt_stat.f_namemax = MAXNAMLEN; 105 mp->mnt_flag |= MNT_LOCAL; 106 mp->mnt_data = fmp; 107 vfs_getnewfsid(mp); 108 109 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE, 110 mp, l); 111 VOP_UNLOCK(rvp, 0); 112 return error; 113 } 114 115 int 116 fdesc_start(struct mount *mp __unused, int flags __unused, 117 struct lwp *l __unused) 118 { 119 return (0); 120 } 121 122 int 123 fdesc_unmount(struct mount *mp, int mntflags, struct lwp *l __unused) 124 { 125 int error; 126 int flags = 0; 127 struct vnode *rtvp = VFSTOFDESC(mp)->f_root; 128 129 if (mntflags & MNT_FORCE) 130 flags |= FORCECLOSE; 131 132 /* 133 * Clear out buffer cache. I don't think we 134 * ever get anything cached at this level at the 135 * moment, but who knows... 136 */ 137 if (rtvp->v_usecount > 1) 138 return (EBUSY); 139 if ((error = vflush(mp, rtvp, flags)) != 0) 140 return (error); 141 142 /* 143 * Release reference on underlying root vnode 144 */ 145 vrele(rtvp); 146 /* 147 * And blow it away for future re-use 148 */ 149 vgone(rtvp); 150 /* 151 * Finally, throw away the fdescmount structure 152 */ 153 free(mp->mnt_data, M_UFSMNT); /* XXX */ 154 mp->mnt_data = 0; 155 156 return (0); 157 } 158 159 int 160 fdesc_root(mp, vpp) 161 struct mount *mp; 162 struct vnode **vpp; 163 { 164 struct vnode *vp; 165 166 /* 167 * Return locked reference to root. 168 */ 169 vp = VFSTOFDESC(mp)->f_root; 170 VREF(vp); 171 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 172 *vpp = vp; 173 return (0); 174 } 175 176 int 177 fdesc_quotactl(struct mount *mp __unused, int cmd __unused, uid_t uid __unused, 178 void *arg __unused, struct lwp *l __unused) 179 { 180 181 return (EOPNOTSUPP); 182 } 183 184 int 185 fdesc_statvfs(mp, sbp, l) 186 struct mount *mp; 187 struct statvfs *sbp; 188 struct lwp *l; 189 { 190 struct filedesc *fdp; 191 struct proc *p; 192 int lim; 193 int i; 194 int last; 195 int freefd; 196 197 /* 198 * Compute number of free file descriptors. 199 * [ Strange results will ensue if the open file 200 * limit is ever reduced below the current number 201 * of open files... ] 202 */ 203 p = l->l_proc; 204 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur; 205 fdp = p->p_fd; 206 last = min(fdp->fd_nfiles, lim); 207 freefd = 0; 208 for (i = fdp->fd_freefile; i < last; i++) 209 if (fdp->fd_ofiles[i] == NULL) 210 freefd++; 211 212 /* 213 * Adjust for the fact that the fdesc array may not 214 * have been fully allocated yet. 215 */ 216 if (fdp->fd_nfiles < lim) 217 freefd += (lim - fdp->fd_nfiles); 218 219 sbp->f_bsize = DEV_BSIZE; 220 sbp->f_frsize = DEV_BSIZE; 221 sbp->f_iosize = DEV_BSIZE; 222 sbp->f_blocks = 2; /* 1K to keep df happy */ 223 sbp->f_bfree = 0; 224 sbp->f_bavail = 0; 225 sbp->f_bresvd = 0; 226 sbp->f_files = lim + 1; /* Allow for "." */ 227 sbp->f_ffree = freefd; /* See comments above */ 228 sbp->f_favail = freefd; /* See comments above */ 229 sbp->f_fresvd = 0; 230 copy_statvfs_info(sbp, mp); 231 return (0); 232 } 233 234 /*ARGSUSED*/ 235 int 236 fdesc_sync(struct mount *mp __unused, int waitfor __unused, 237 kauth_cred_t uc __unused, struct lwp *l __unused) 238 { 239 240 return (0); 241 } 242 243 /* 244 * Fdesc flat namespace lookup. 245 * Currently unsupported. 246 */ 247 int 248 fdesc_vget(struct mount *mp __unused, ino_t ino __unused, 249 struct vnode **vpp __unused) 250 { 251 252 return (EOPNOTSUPP); 253 } 254 255 256 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup") 257 { 258 259 sysctl_createv(clog, 0, NULL, NULL, 260 CTLFLAG_PERMANENT, 261 CTLTYPE_NODE, "vfs", NULL, 262 NULL, 0, NULL, 0, 263 CTL_VFS, CTL_EOL); 264 sysctl_createv(clog, 0, NULL, NULL, 265 CTLFLAG_PERMANENT, 266 CTLTYPE_NODE, "fdesc", 267 SYSCTL_DESCR("File-descriptor file system"), 268 NULL, 0, NULL, 0, 269 CTL_VFS, 7, CTL_EOL); 270 /* 271 * XXX the "7" above could be dynamic, thereby eliminating one 272 * more instance of the "number to vfs" mapping problem, but 273 * "7" is the order as taken from sys/mount.h 274 */ 275 } 276 277 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc; 278 279 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = { 280 &fdesc_vnodeop_opv_desc, 281 NULL, 282 }; 283 284 struct vfsops fdesc_vfsops = { 285 MOUNT_FDESC, 286 fdesc_mount, 287 fdesc_start, 288 fdesc_unmount, 289 fdesc_root, 290 fdesc_quotactl, 291 fdesc_statvfs, 292 fdesc_sync, 293 fdesc_vget, 294 NULL, /* vfs_fhtovp */ 295 NULL, /* vfs_vptofh */ 296 fdesc_init, 297 NULL, 298 fdesc_done, 299 NULL, /* vfs_mountroot */ 300 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 301 vfs_stdextattrctl, 302 fdesc_vnodeopv_descs, 303 0, 304 { NULL, NULL}, 305 }; 306 VFS_ATTACH(fdesc_vfsops); 307