1 /* $NetBSD: fdesc_vfsops.c,v 1.59 2006/05/14 21:31:52 elad 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.59 2006/05/14 21:31:52 elad 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(mp, path, data, ndp, l) 81 struct mount *mp; 82 const char *path; 83 void *data; 84 struct nameidata *ndp; 85 struct lwp *l; 86 { 87 int error = 0; 88 struct fdescmount *fmp; 89 struct vnode *rvp; 90 91 if (mp->mnt_flag & MNT_GETARGS) 92 return 0; 93 /* 94 * Update is a no-op 95 */ 96 if (mp->mnt_flag & MNT_UPDATE) 97 return (EOPNOTSUPP); 98 99 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp); 100 if (error) 101 return (error); 102 103 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount), 104 M_UFSMNT, M_WAITOK); /* XXX */ 105 rvp->v_type = VDIR; 106 rvp->v_flag |= VROOT; 107 fmp->f_root = rvp; 108 mp->mnt_stat.f_namemax = MAXNAMLEN; 109 mp->mnt_flag |= MNT_LOCAL; 110 mp->mnt_data = fmp; 111 vfs_getnewfsid(mp); 112 113 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE, 114 mp, l); 115 VOP_UNLOCK(rvp, 0); 116 return error; 117 } 118 119 int 120 fdesc_start(mp, flags, l) 121 struct mount *mp; 122 int flags; 123 struct lwp *l; 124 { 125 return (0); 126 } 127 128 int 129 fdesc_unmount(mp, mntflags, l) 130 struct mount *mp; 131 int mntflags; 132 struct lwp *l; 133 { 134 int error; 135 int flags = 0; 136 struct vnode *rtvp = VFSTOFDESC(mp)->f_root; 137 138 if (mntflags & MNT_FORCE) 139 flags |= FORCECLOSE; 140 141 /* 142 * Clear out buffer cache. I don't think we 143 * ever get anything cached at this level at the 144 * moment, but who knows... 145 */ 146 if (rtvp->v_usecount > 1) 147 return (EBUSY); 148 if ((error = vflush(mp, rtvp, flags)) != 0) 149 return (error); 150 151 /* 152 * Release reference on underlying root vnode 153 */ 154 vrele(rtvp); 155 /* 156 * And blow it away for future re-use 157 */ 158 vgone(rtvp); 159 /* 160 * Finally, throw away the fdescmount structure 161 */ 162 free(mp->mnt_data, M_UFSMNT); /* XXX */ 163 mp->mnt_data = 0; 164 165 return (0); 166 } 167 168 int 169 fdesc_root(mp, vpp) 170 struct mount *mp; 171 struct vnode **vpp; 172 { 173 struct vnode *vp; 174 175 /* 176 * Return locked reference to root. 177 */ 178 vp = VFSTOFDESC(mp)->f_root; 179 VREF(vp); 180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 181 *vpp = vp; 182 return (0); 183 } 184 185 int 186 fdesc_quotactl(mp, cmd, uid, arg, l) 187 struct mount *mp; 188 int cmd; 189 uid_t uid; 190 void *arg; 191 struct lwp *l; 192 { 193 194 return (EOPNOTSUPP); 195 } 196 197 int 198 fdesc_statvfs(mp, sbp, l) 199 struct mount *mp; 200 struct statvfs *sbp; 201 struct lwp *l; 202 { 203 struct filedesc *fdp; 204 struct proc *p; 205 int lim; 206 int i; 207 int last; 208 int freefd; 209 210 /* 211 * Compute number of free file descriptors. 212 * [ Strange results will ensue if the open file 213 * limit is ever reduced below the current number 214 * of open files... ] 215 */ 216 p = l->l_proc; 217 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur; 218 fdp = p->p_fd; 219 last = min(fdp->fd_nfiles, lim); 220 freefd = 0; 221 for (i = fdp->fd_freefile; i < last; i++) 222 if (fdp->fd_ofiles[i] == NULL) 223 freefd++; 224 225 /* 226 * Adjust for the fact that the fdesc array may not 227 * have been fully allocated yet. 228 */ 229 if (fdp->fd_nfiles < lim) 230 freefd += (lim - fdp->fd_nfiles); 231 232 sbp->f_bsize = DEV_BSIZE; 233 sbp->f_frsize = DEV_BSIZE; 234 sbp->f_iosize = DEV_BSIZE; 235 sbp->f_blocks = 2; /* 1K to keep df happy */ 236 sbp->f_bfree = 0; 237 sbp->f_bavail = 0; 238 sbp->f_bresvd = 0; 239 sbp->f_files = lim + 1; /* Allow for "." */ 240 sbp->f_ffree = freefd; /* See comments above */ 241 sbp->f_favail = freefd; /* See comments above */ 242 sbp->f_fresvd = 0; 243 copy_statvfs_info(sbp, mp); 244 return (0); 245 } 246 247 /*ARGSUSED*/ 248 int 249 fdesc_sync(mp, waitfor, uc, l) 250 struct mount *mp; 251 int waitfor; 252 kauth_cred_t uc; 253 struct lwp *l; 254 { 255 256 return (0); 257 } 258 259 /* 260 * Fdesc flat namespace lookup. 261 * Currently unsupported. 262 */ 263 int 264 fdesc_vget(mp, ino, vpp) 265 struct mount *mp; 266 ino_t ino; 267 struct vnode **vpp; 268 { 269 270 return (EOPNOTSUPP); 271 } 272 273 274 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup") 275 { 276 277 sysctl_createv(clog, 0, NULL, NULL, 278 CTLFLAG_PERMANENT, 279 CTLTYPE_NODE, "vfs", NULL, 280 NULL, 0, NULL, 0, 281 CTL_VFS, CTL_EOL); 282 sysctl_createv(clog, 0, NULL, NULL, 283 CTLFLAG_PERMANENT, 284 CTLTYPE_NODE, "fdesc", 285 SYSCTL_DESCR("File-descriptor file system"), 286 NULL, 0, NULL, 0, 287 CTL_VFS, 7, CTL_EOL); 288 /* 289 * XXX the "7" above could be dynamic, thereby eliminating one 290 * more instance of the "number to vfs" mapping problem, but 291 * "7" is the order as taken from sys/mount.h 292 */ 293 } 294 295 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc; 296 297 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = { 298 &fdesc_vnodeop_opv_desc, 299 NULL, 300 }; 301 302 struct vfsops fdesc_vfsops = { 303 MOUNT_FDESC, 304 fdesc_mount, 305 fdesc_start, 306 fdesc_unmount, 307 fdesc_root, 308 fdesc_quotactl, 309 fdesc_statvfs, 310 fdesc_sync, 311 fdesc_vget, 312 NULL, /* vfs_fhtovp */ 313 NULL, /* vfs_vptofh */ 314 fdesc_init, 315 NULL, 316 fdesc_done, 317 NULL, /* vfs_mountroot */ 318 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 319 vfs_stdextattrctl, 320 fdesc_vnodeopv_descs, 321 }; 322 VFS_ATTACH(fdesc_vfsops); 323