1 /*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software donated to Berkeley by 6 * Jan-Simon Pendry. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94 33 * 34 * $FreeBSD$ 35 */ 36 37 /* 38 * /dev/fd Filesystem 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/dirent.h> 45 #include <sys/filedesc.h> 46 #include <sys/kernel.h> /* boottime */ 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/malloc.h> 50 #include <sys/file.h> /* Must come after sys/malloc.h */ 51 #include <sys/mount.h> 52 #include <sys/namei.h> 53 #include <sys/proc.h> 54 #include <sys/stat.h> 55 #include <sys/vnode.h> 56 57 #include <fs/fdescfs/fdesc.h> 58 59 #define FDL_WANT 0x01 60 #define FDL_LOCKED 0x02 61 static int fdcache_lock; 62 63 #define NFDCACHE 4 64 #define FD_NHASH(ix) \ 65 (&fdhashtbl[(ix) & fdhash]) 66 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; 67 static u_long fdhash; 68 69 static vop_getattr_t fdesc_getattr; 70 static vop_inactive_t fdesc_inactive; 71 static vop_lookup_t fdesc_lookup; 72 static vop_open_t fdesc_open; 73 static vop_readdir_t fdesc_readdir; 74 static vop_reclaim_t fdesc_reclaim; 75 static vop_setattr_t fdesc_setattr; 76 77 static struct vop_vector fdesc_vnodeops = { 78 .vop_default = &default_vnodeops, 79 80 .vop_access = VOP_NULL, 81 .vop_getattr = fdesc_getattr, 82 .vop_inactive = fdesc_inactive, 83 .vop_lookup = fdesc_lookup, 84 .vop_open = fdesc_open, 85 .vop_pathconf = vop_stdpathconf, 86 .vop_readdir = fdesc_readdir, 87 .vop_reclaim = fdesc_reclaim, 88 .vop_setattr = fdesc_setattr, 89 }; 90 91 /* 92 * Initialise cache headers 93 */ 94 int 95 fdesc_init(vfsp) 96 struct vfsconf *vfsp; 97 { 98 99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); 100 return (0); 101 } 102 103 int 104 fdesc_allocvp(ftype, ix, mp, vpp, td) 105 fdntype ftype; 106 int ix; 107 struct mount *mp; 108 struct vnode **vpp; 109 struct thread *td; 110 { 111 struct fdhashhead *fc; 112 struct fdescnode *fd; 113 int error = 0; 114 115 fc = FD_NHASH(ix); 116 loop: 117 LIST_FOREACH(fd, fc, fd_hash) { 118 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) { 119 if (vget(fd->fd_vnode, 0, td)) 120 goto loop; 121 *vpp = fd->fd_vnode; 122 return (error); 123 } 124 } 125 126 /* 127 * otherwise lock the array while we call getnewvnode 128 * since that can block. 129 */ 130 if (fdcache_lock & FDL_LOCKED) { 131 fdcache_lock |= FDL_WANT; 132 (void) tsleep( &fdcache_lock, PINOD, "fdalvp", 0); 133 goto loop; 134 } 135 fdcache_lock |= FDL_LOCKED; 136 137 /* 138 * Do the MALLOC before the getnewvnode since doing so afterward 139 * might cause a bogus v_data pointer to get dereferenced 140 * elsewhere if MALLOC should block. 141 */ 142 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK); 143 144 error = getnewvnode("fdesc", mp, &fdesc_vnodeops, vpp); 145 if (error) { 146 FREE(fd, M_TEMP); 147 goto out; 148 } 149 (*vpp)->v_data = fd; 150 fd->fd_vnode = *vpp; 151 fd->fd_type = ftype; 152 fd->fd_fd = -1; 153 fd->fd_ix = ix; 154 LIST_INSERT_HEAD(fc, fd, fd_hash); 155 156 out: 157 fdcache_lock &= ~FDL_LOCKED; 158 159 if (fdcache_lock & FDL_WANT) { 160 fdcache_lock &= ~FDL_WANT; 161 wakeup( &fdcache_lock); 162 } 163 164 return (error); 165 } 166 167 /* 168 * vp is the current namei directory 169 * ndp is the name to locate in that directory... 170 */ 171 static int 172 fdesc_lookup(ap) 173 struct vop_lookup_args /* { 174 struct vnode * a_dvp; 175 struct vnode ** a_vpp; 176 struct componentname * a_cnp; 177 } */ *ap; 178 { 179 struct vnode **vpp = ap->a_vpp; 180 struct vnode *dvp = ap->a_dvp; 181 struct componentname *cnp = ap->a_cnp; 182 char *pname = cnp->cn_nameptr; 183 struct thread *td = cnp->cn_thread; 184 struct file *fp; 185 int nlen = cnp->cn_namelen; 186 u_int fd; 187 int error; 188 struct vnode *fvp; 189 190 if ((cnp->cn_flags & ISLASTCN) && 191 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 192 error = EROFS; 193 goto bad; 194 } 195 196 if (cnp->cn_namelen == 1 && *pname == '.') { 197 *vpp = dvp; 198 VREF(dvp); 199 return (0); 200 } 201 202 if (VTOFDESC(dvp)->fd_type != Froot) { 203 error = ENOTDIR; 204 goto bad; 205 } 206 207 fd = 0; 208 /* the only time a leading 0 is acceptable is if it's "0" */ 209 if (*pname == '0' && nlen != 1) { 210 error = ENOENT; 211 goto bad; 212 } 213 while (nlen--) { 214 if (*pname < '0' || *pname > '9') { 215 error = ENOENT; 216 goto bad; 217 } 218 fd = 10 * fd + *pname++ - '0'; 219 } 220 221 if ((error = fget(td, fd, &fp)) != 0) 222 goto bad; 223 224 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td); 225 fdrop(fp, td); 226 if (error) 227 goto bad; 228 VTOFDESC(fvp)->fd_fd = fd; 229 if (fvp != dvp) 230 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td); 231 *vpp = fvp; 232 return (0); 233 234 bad: 235 *vpp = NULL; 236 return (error); 237 } 238 239 static int 240 fdesc_open(ap) 241 struct vop_open_args /* { 242 struct vnode *a_vp; 243 int a_mode; 244 struct ucred *a_cred; 245 struct thread *a_td; 246 } */ *ap; 247 { 248 struct vnode *vp = ap->a_vp; 249 250 if (VTOFDESC(vp)->fd_type == Froot) 251 return (0); 252 253 /* 254 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the the file 255 * descriptor being sought for duplication. The error return ensures 256 * that the vnode for this device will be released by vn_open. Open 257 * will detect this special error and take the actions in dupfdopen. 258 * Other callers of vn_open or VOP_OPEN will simply report the 259 * error. 260 */ 261 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ 262 return (ENODEV); 263 } 264 265 static int 266 fdesc_getattr(ap) 267 struct vop_getattr_args /* { 268 struct vnode *a_vp; 269 struct vattr *a_vap; 270 struct ucred *a_cred; 271 struct thread *a_td; 272 } */ *ap; 273 { 274 struct vnode *vp = ap->a_vp; 275 struct vattr *vap = ap->a_vap; 276 struct file *fp; 277 struct stat stb; 278 u_int fd; 279 int error = 0; 280 281 switch (VTOFDESC(vp)->fd_type) { 282 case Froot: 283 VATTR_NULL(vap); 284 285 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 286 vap->va_type = VDIR; 287 vap->va_nlink = 2; 288 vap->va_size = DEV_BSIZE; 289 vap->va_fileid = VTOFDESC(vp)->fd_ix; 290 vap->va_uid = 0; 291 vap->va_gid = 0; 292 vap->va_blocksize = DEV_BSIZE; 293 vap->va_atime.tv_sec = boottime.tv_sec; 294 vap->va_atime.tv_nsec = 0; 295 vap->va_mtime = vap->va_atime; 296 vap->va_ctime = vap->va_mtime; 297 vap->va_gen = 0; 298 vap->va_flags = 0; 299 vap->va_rdev = 0; 300 vap->va_bytes = 0; 301 break; 302 303 case Fdesc: 304 fd = VTOFDESC(vp)->fd_fd; 305 306 if ((error = fget(ap->a_td, fd, &fp)) != 0) 307 return (error); 308 309 bzero(&stb, sizeof(stb)); 310 error = fo_stat(fp, &stb, ap->a_td->td_ucred, ap->a_td); 311 fdrop(fp, ap->a_td); 312 if (error == 0) { 313 VATTR_NULL(vap); 314 vap->va_type = IFTOVT(stb.st_mode); 315 vap->va_mode = stb.st_mode; 316 #define FDRX (VREAD|VEXEC) 317 if (vap->va_type == VDIR) 318 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6)); 319 #undef FDRX 320 vap->va_nlink = 1; 321 vap->va_flags = 0; 322 vap->va_bytes = stb.st_blocks * stb.st_blksize; 323 vap->va_fileid = VTOFDESC(vp)->fd_ix; 324 vap->va_size = stb.st_size; 325 vap->va_blocksize = stb.st_blksize; 326 vap->va_rdev = stb.st_rdev; 327 328 /* 329 * If no time data is provided, use the current time. 330 */ 331 if (stb.st_atimespec.tv_sec == 0 && 332 stb.st_atimespec.tv_nsec == 0) 333 nanotime(&stb.st_atimespec); 334 335 if (stb.st_ctimespec.tv_sec == 0 && 336 stb.st_ctimespec.tv_nsec == 0) 337 nanotime(&stb.st_ctimespec); 338 339 if (stb.st_mtimespec.tv_sec == 0 && 340 stb.st_mtimespec.tv_nsec == 0) 341 nanotime(&stb.st_mtimespec); 342 343 vap->va_atime = stb.st_atimespec; 344 vap->va_mtime = stb.st_mtimespec; 345 vap->va_ctime = stb.st_ctimespec; 346 vap->va_uid = stb.st_uid; 347 vap->va_gid = stb.st_gid; 348 } 349 break; 350 351 default: 352 panic("fdesc_getattr"); 353 break; 354 } 355 356 if (error == 0) 357 vp->v_type = vap->va_type; 358 return (error); 359 } 360 361 static int 362 fdesc_setattr(ap) 363 struct vop_setattr_args /* { 364 struct vnode *a_vp; 365 struct vattr *a_vap; 366 struct ucred *a_cred; 367 struct thread *a_td; 368 } */ *ap; 369 { 370 struct vattr *vap = ap->a_vap; 371 struct vnode *vp; 372 struct mount *mp; 373 struct file *fp; 374 unsigned fd; 375 int error; 376 377 /* 378 * Can't mess with the root vnode 379 */ 380 if (VTOFDESC(ap->a_vp)->fd_type == Froot) 381 return (EACCES); 382 383 fd = VTOFDESC(ap->a_vp)->fd_fd; 384 385 /* 386 * Allow setattr where there is an underlying vnode. 387 */ 388 error = getvnode(ap->a_td->td_proc->p_fd, fd, &fp); 389 if (error) { 390 /* 391 * getvnode() returns EINVAL if the file descriptor is not 392 * backed by a vnode. Silently drop all changes except 393 * chflags(2) in this case. 394 */ 395 if (error == EINVAL) { 396 if (vap->va_flags != VNOVAL) 397 error = EOPNOTSUPP; 398 else 399 error = 0; 400 } 401 return (error); 402 } 403 vp = fp->f_vnode; 404 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) { 405 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td); 406 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td); 407 VOP_UNLOCK(vp, 0, ap->a_td); 408 vn_finished_write(mp); 409 } 410 fdrop(fp, ap->a_td); 411 return (error); 412 } 413 414 #define UIO_MX 16 415 416 static int 417 fdesc_readdir(ap) 418 struct vop_readdir_args /* { 419 struct vnode *a_vp; 420 struct uio *a_uio; 421 struct ucred *a_cred; 422 int *a_eofflag; 423 u_long *a_cookies; 424 int a_ncookies; 425 } */ *ap; 426 { 427 struct uio *uio = ap->a_uio; 428 struct filedesc *fdp; 429 struct dirent d; 430 struct dirent *dp = &d; 431 int error, i, off, fcnt; 432 433 /* 434 * We don't allow exporting fdesc mounts, and currently local 435 * requests do not need cookies. 436 */ 437 if (ap->a_ncookies) 438 panic("fdesc_readdir: not hungry"); 439 440 if (VTOFDESC(ap->a_vp)->fd_type != Froot) 441 panic("fdesc_readdir: not dir"); 442 443 off = (int)uio->uio_offset; 444 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 || 445 uio->uio_resid < UIO_MX) 446 return (EINVAL); 447 i = (u_int)off / UIO_MX; 448 fdp = uio->uio_td->td_proc->p_fd; 449 error = 0; 450 451 fcnt = i - 2; /* The first two nodes are `.' and `..' */ 452 453 FILEDESC_LOCK_FAST(fdp); 454 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) { 455 switch (i) { 456 case 0: /* `.' */ 457 case 1: /* `..' */ 458 bzero((caddr_t)dp, UIO_MX); 459 460 dp->d_fileno = i + FD_ROOT; 461 dp->d_namlen = i + 1; 462 dp->d_reclen = UIO_MX; 463 bcopy("..", dp->d_name, dp->d_namlen); 464 dp->d_name[i + 1] = '\0'; 465 dp->d_type = DT_DIR; 466 break; 467 default: 468 if (fdp->fd_ofiles[fcnt] == NULL) { 469 FILEDESC_UNLOCK_FAST(fdp); 470 goto done; 471 } 472 473 bzero((caddr_t) dp, UIO_MX); 474 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt); 475 dp->d_reclen = UIO_MX; 476 dp->d_type = DT_UNKNOWN; 477 dp->d_fileno = i + FD_DESC; 478 break; 479 } 480 /* 481 * And ship to userland 482 */ 483 FILEDESC_UNLOCK_FAST(fdp); 484 error = uiomove(dp, UIO_MX, uio); 485 if (error) 486 goto done; 487 FILEDESC_LOCK_FAST(fdp); 488 i++; 489 fcnt++; 490 } 491 FILEDESC_UNLOCK_FAST(fdp); 492 493 done: 494 uio->uio_offset = i * UIO_MX; 495 return (error); 496 } 497 498 static int 499 fdesc_inactive(ap) 500 struct vop_inactive_args /* { 501 struct vnode *a_vp; 502 struct thread *a_td; 503 } */ *ap; 504 { 505 struct vnode *vp = ap->a_vp; 506 507 /* 508 * Clear out the v_type field to avoid 509 * nasty things happening in vgone(). 510 */ 511 vp->v_type = VNON; 512 return (0); 513 } 514 515 static int 516 fdesc_reclaim(ap) 517 struct vop_reclaim_args /* { 518 struct vnode *a_vp; 519 } */ *ap; 520 { 521 struct vnode *vp = ap->a_vp; 522 struct fdescnode *fd = VTOFDESC(vp); 523 524 LIST_REMOVE(fd, fd_hash); 525 FREE(vp->v_data, M_TEMP); 526 vp->v_data = 0; 527 528 return (0); 529 } 530