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 VOP_UNLOCK(dvp, 0, td); 197 if (cnp->cn_namelen == 1 && *pname == '.') { 198 *vpp = dvp; 199 VREF(dvp); 200 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 201 return (0); 202 } 203 204 if (VTOFDESC(dvp)->fd_type != Froot) { 205 error = ENOTDIR; 206 goto bad; 207 } 208 209 fd = 0; 210 /* the only time a leading 0 is acceptable is if it's "0" */ 211 if (*pname == '0' && nlen != 1) { 212 error = ENOENT; 213 goto bad; 214 } 215 while (nlen--) { 216 if (*pname < '0' || *pname > '9') { 217 error = ENOENT; 218 goto bad; 219 } 220 fd = 10 * fd + *pname++ - '0'; 221 } 222 223 if ((error = fget(td, fd, &fp)) != 0) 224 goto bad; 225 226 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td); 227 fdrop(fp, td); 228 if (error) 229 goto bad; 230 VTOFDESC(fvp)->fd_fd = fd; 231 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td); 232 *vpp = fvp; 233 return (0); 234 235 bad: 236 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 237 *vpp = NULL; 238 return (error); 239 } 240 241 static int 242 fdesc_open(ap) 243 struct vop_open_args /* { 244 struct vnode *a_vp; 245 int a_mode; 246 struct ucred *a_cred; 247 struct thread *a_td; 248 } */ *ap; 249 { 250 struct vnode *vp = ap->a_vp; 251 252 if (VTOFDESC(vp)->fd_type == Froot) 253 return (0); 254 255 /* 256 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the the file 257 * descriptor being sought for duplication. The error return ensures 258 * that the vnode for this device will be released by vn_open. Open 259 * will detect this special error and take the actions in dupfdopen. 260 * Other callers of vn_open or VOP_OPEN will simply report the 261 * error. 262 */ 263 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ 264 return (ENODEV); 265 } 266 267 static int 268 fdesc_getattr(ap) 269 struct vop_getattr_args /* { 270 struct vnode *a_vp; 271 struct vattr *a_vap; 272 struct ucred *a_cred; 273 struct thread *a_td; 274 } */ *ap; 275 { 276 struct vnode *vp = ap->a_vp; 277 struct vattr *vap = ap->a_vap; 278 struct file *fp; 279 struct stat stb; 280 u_int fd; 281 int error = 0; 282 283 switch (VTOFDESC(vp)->fd_type) { 284 case Froot: 285 VATTR_NULL(vap); 286 287 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 288 vap->va_type = VDIR; 289 vap->va_nlink = 2; 290 vap->va_size = DEV_BSIZE; 291 vap->va_fileid = VTOFDESC(vp)->fd_ix; 292 vap->va_uid = 0; 293 vap->va_gid = 0; 294 vap->va_blocksize = DEV_BSIZE; 295 vap->va_atime.tv_sec = boottime.tv_sec; 296 vap->va_atime.tv_nsec = 0; 297 vap->va_mtime = vap->va_atime; 298 vap->va_ctime = vap->va_mtime; 299 vap->va_gen = 0; 300 vap->va_flags = 0; 301 vap->va_rdev = 0; 302 vap->va_bytes = 0; 303 break; 304 305 case Fdesc: 306 fd = VTOFDESC(vp)->fd_fd; 307 308 if ((error = fget(ap->a_td, fd, &fp)) != 0) 309 return (error); 310 311 bzero(&stb, sizeof(stb)); 312 error = fo_stat(fp, &stb, ap->a_td->td_ucred, ap->a_td); 313 fdrop(fp, ap->a_td); 314 if (error == 0) { 315 VATTR_NULL(vap); 316 vap->va_type = IFTOVT(stb.st_mode); 317 vap->va_mode = stb.st_mode; 318 #define FDRX (VREAD|VEXEC) 319 if (vap->va_type == VDIR) 320 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6)); 321 #undef FDRX 322 vap->va_nlink = 1; 323 vap->va_flags = 0; 324 vap->va_bytes = stb.st_blocks * stb.st_blksize; 325 vap->va_fileid = VTOFDESC(vp)->fd_ix; 326 vap->va_size = stb.st_size; 327 vap->va_blocksize = stb.st_blksize; 328 vap->va_rdev = stb.st_rdev; 329 330 /* 331 * If no time data is provided, use the current time. 332 */ 333 if (stb.st_atimespec.tv_sec == 0 && 334 stb.st_atimespec.tv_nsec == 0) 335 nanotime(&stb.st_atimespec); 336 337 if (stb.st_ctimespec.tv_sec == 0 && 338 stb.st_ctimespec.tv_nsec == 0) 339 nanotime(&stb.st_ctimespec); 340 341 if (stb.st_mtimespec.tv_sec == 0 && 342 stb.st_mtimespec.tv_nsec == 0) 343 nanotime(&stb.st_mtimespec); 344 345 vap->va_atime = stb.st_atimespec; 346 vap->va_mtime = stb.st_mtimespec; 347 vap->va_ctime = stb.st_ctimespec; 348 vap->va_uid = stb.st_uid; 349 vap->va_gid = stb.st_gid; 350 } 351 break; 352 353 default: 354 panic("fdesc_getattr"); 355 break; 356 } 357 358 if (error == 0) 359 vp->v_type = vap->va_type; 360 return (error); 361 } 362 363 static int 364 fdesc_setattr(ap) 365 struct vop_setattr_args /* { 366 struct vnode *a_vp; 367 struct vattr *a_vap; 368 struct ucred *a_cred; 369 struct thread *a_td; 370 } */ *ap; 371 { 372 struct vattr *vap = ap->a_vap; 373 struct vnode *vp; 374 struct mount *mp; 375 struct file *fp; 376 unsigned fd; 377 int error; 378 379 /* 380 * Can't mess with the root vnode 381 */ 382 if (VTOFDESC(ap->a_vp)->fd_type == Froot) 383 return (EACCES); 384 385 fd = VTOFDESC(ap->a_vp)->fd_fd; 386 387 /* 388 * Allow setattr where there is an underlying vnode. 389 */ 390 error = getvnode(ap->a_td->td_proc->p_fd, fd, &fp); 391 if (error) { 392 /* 393 * getvnode() returns EINVAL if the file descriptor is not 394 * backed by a vnode. Silently drop all changes except 395 * chflags(2) in this case. 396 */ 397 if (error == EINVAL) { 398 if (vap->va_flags != VNOVAL) 399 error = EOPNOTSUPP; 400 else 401 error = 0; 402 } 403 return (error); 404 } 405 vp = fp->f_vnode; 406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td); 407 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) { 408 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td); 409 vn_finished_write(mp); 410 } 411 VOP_UNLOCK(vp, 0, ap->a_td); 412 fdrop(fp, ap->a_td); 413 return (error); 414 } 415 416 #define UIO_MX 16 417 418 static int 419 fdesc_readdir(ap) 420 struct vop_readdir_args /* { 421 struct vnode *a_vp; 422 struct uio *a_uio; 423 struct ucred *a_cred; 424 int *a_eofflag; 425 u_long *a_cookies; 426 int a_ncookies; 427 } */ *ap; 428 { 429 struct uio *uio = ap->a_uio; 430 struct filedesc *fdp; 431 struct dirent d; 432 struct dirent *dp = &d; 433 int error, i, off, fcnt; 434 435 /* 436 * We don't allow exporting fdesc mounts, and currently local 437 * requests do not need cookies. 438 */ 439 if (ap->a_ncookies) 440 panic("fdesc_readdir: not hungry"); 441 442 if (VTOFDESC(ap->a_vp)->fd_type != Froot) 443 panic("fdesc_readdir: not dir"); 444 445 off = (int)uio->uio_offset; 446 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 || 447 uio->uio_resid < UIO_MX) 448 return (EINVAL); 449 i = (u_int)off / UIO_MX; 450 fdp = uio->uio_td->td_proc->p_fd; 451 error = 0; 452 453 fcnt = i - 2; /* The first two nodes are `.' and `..' */ 454 455 FILEDESC_LOCK_FAST(fdp); 456 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) { 457 switch (i) { 458 case 0: /* `.' */ 459 case 1: /* `..' */ 460 bzero((caddr_t)dp, UIO_MX); 461 462 dp->d_fileno = i + FD_ROOT; 463 dp->d_namlen = i + 1; 464 dp->d_reclen = UIO_MX; 465 bcopy("..", dp->d_name, dp->d_namlen); 466 dp->d_name[i + 1] = '\0'; 467 dp->d_type = DT_DIR; 468 break; 469 default: 470 if (fdp->fd_ofiles[fcnt] == NULL) { 471 FILEDESC_UNLOCK_FAST(fdp); 472 goto done; 473 } 474 475 bzero((caddr_t) dp, UIO_MX); 476 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt); 477 dp->d_reclen = UIO_MX; 478 dp->d_type = DT_UNKNOWN; 479 dp->d_fileno = i + FD_DESC; 480 break; 481 } 482 /* 483 * And ship to userland 484 */ 485 FILEDESC_UNLOCK_FAST(fdp); 486 error = uiomove(dp, UIO_MX, uio); 487 if (error) 488 goto done; 489 FILEDESC_LOCK_FAST(fdp); 490 i++; 491 fcnt++; 492 } 493 FILEDESC_UNLOCK_FAST(fdp); 494 495 done: 496 uio->uio_offset = i * UIO_MX; 497 return (error); 498 } 499 500 static int 501 fdesc_inactive(ap) 502 struct vop_inactive_args /* { 503 struct vnode *a_vp; 504 struct thread *a_td; 505 } */ *ap; 506 { 507 struct vnode *vp = ap->a_vp; 508 509 /* 510 * Clear out the v_type field to avoid 511 * nasty things happening in vgone(). 512 */ 513 vp->v_type = VNON; 514 return (0); 515 } 516 517 static int 518 fdesc_reclaim(ap) 519 struct vop_reclaim_args /* { 520 struct vnode *a_vp; 521 } */ *ap; 522 { 523 struct vnode *vp = ap->a_vp; 524 struct fdescnode *fd = VTOFDESC(vp); 525 526 LIST_REMOVE(fd, fd_hash); 527 FREE(vp->v_data, M_TEMP); 528 vp->v_data = 0; 529 530 return (0); 531 } 532