1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 39 * $FreeBSD$ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/fcntl.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <sys/proc.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/vnode.h> 51 #include <sys/buf.h> 52 #include <sys/filio.h> 53 #include <sys/ttycom.h> 54 #include <sys/conf.h> 55 56 static int vn_closefile __P((struct file *fp, struct proc *p)); 57 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data, 58 struct proc *p)); 59 static int vn_read __P((struct file *fp, struct uio *uio, 60 struct ucred *cred, int flags)); 61 static int vn_poll __P((struct file *fp, int events, struct ucred *cred, 62 struct proc *p)); 63 static int vn_write __P((struct file *fp, struct uio *uio, 64 struct ucred *cred, int flags)); 65 66 struct fileops vnops = 67 { vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile }; 68 69 /* 70 * Common code for vnode open operations. 71 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 72 */ 73 int 74 vn_open(ndp, fmode, cmode) 75 register struct nameidata *ndp; 76 int fmode, cmode; 77 { 78 register struct vnode *vp; 79 register struct proc *p = ndp->ni_cnd.cn_proc; 80 register struct ucred *cred = p->p_ucred; 81 struct vattr vat; 82 struct vattr *vap = &vat; 83 int mode, error; 84 85 if (fmode & O_CREAT) { 86 ndp->ni_cnd.cn_nameiop = CREATE; 87 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 88 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 89 ndp->ni_cnd.cn_flags |= FOLLOW; 90 error = namei(ndp); 91 if (error) 92 return (error); 93 if (ndp->ni_vp == NULL) { 94 VATTR_NULL(vap); 95 vap->va_type = VREG; 96 vap->va_mode = cmode; 97 if (fmode & O_EXCL) 98 vap->va_vaflags |= VA_EXCLUSIVE; 99 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE); 100 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 101 &ndp->ni_cnd, vap); 102 vput(ndp->ni_dvp); 103 if (error) 104 return (error); 105 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create"); 106 ASSERT_VOP_LOCKED(ndp->ni_vp, "create"); 107 fmode &= ~O_TRUNC; 108 vp = ndp->ni_vp; 109 } else { 110 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 111 if (ndp->ni_dvp == ndp->ni_vp) 112 vrele(ndp->ni_dvp); 113 else 114 vput(ndp->ni_dvp); 115 ndp->ni_dvp = NULL; 116 vp = ndp->ni_vp; 117 if (fmode & O_EXCL) { 118 error = EEXIST; 119 goto bad; 120 } 121 fmode &= ~O_CREAT; 122 } 123 } else { 124 ndp->ni_cnd.cn_nameiop = LOOKUP; 125 ndp->ni_cnd.cn_flags = 126 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 127 error = namei(ndp); 128 if (error) 129 return (error); 130 vp = ndp->ni_vp; 131 } 132 if (vp->v_type == VLNK) { 133 error = EMLINK; 134 goto bad; 135 } 136 if (vp->v_type == VSOCK) { 137 error = EOPNOTSUPP; 138 goto bad; 139 } 140 if ((fmode & O_CREAT) == 0) { 141 mode = 0; 142 if (fmode & (FWRITE | O_TRUNC)) { 143 if (vp->v_type == VDIR) { 144 error = EISDIR; 145 goto bad; 146 } 147 error = vn_writechk(vp); 148 if (error) 149 goto bad; 150 mode |= VWRITE; 151 } 152 if (fmode & FREAD) 153 mode |= VREAD; 154 if (mode) { 155 error = VOP_ACCESS(vp, mode, cred, p); 156 if (error) 157 goto bad; 158 } 159 } 160 if (fmode & O_TRUNC) { 161 VOP_UNLOCK(vp, 0, p); /* XXX */ 162 VOP_LEASE(vp, p, cred, LEASE_WRITE); 163 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */ 164 VATTR_NULL(vap); 165 vap->va_size = 0; 166 error = VOP_SETATTR(vp, vap, cred, p); 167 if (error) 168 goto bad; 169 } 170 error = VOP_OPEN(vp, fmode, cred, p); 171 if (error) 172 goto bad; 173 /* 174 * Make sure that a VM object is created for VMIO support. 175 */ 176 if (vn_canvmio(vp) == TRUE) { 177 if ((error = vfs_object_create(vp, p, cred)) != 0) 178 goto bad; 179 } 180 181 if (fmode & FWRITE) 182 vp->v_writecount++; 183 return (0); 184 bad: 185 vput(vp); 186 return (error); 187 } 188 189 /* 190 * Check for write permissions on the specified vnode. 191 * Prototype text segments cannot be written. 192 */ 193 int 194 vn_writechk(vp) 195 register struct vnode *vp; 196 { 197 198 /* 199 * If there's shared text associated with 200 * the vnode, try to free it up once. If 201 * we fail, we can't allow writing. 202 */ 203 if (vp->v_flag & VTEXT) 204 return (ETXTBSY); 205 return (0); 206 } 207 208 /* 209 * Vnode close call 210 */ 211 int 212 vn_close(vp, flags, cred, p) 213 register struct vnode *vp; 214 int flags; 215 struct ucred *cred; 216 struct proc *p; 217 { 218 int error; 219 220 if (flags & FWRITE) 221 vp->v_writecount--; 222 error = VOP_CLOSE(vp, flags, cred, p); 223 vrele(vp); 224 return (error); 225 } 226 227 /* 228 * Package up an I/O request on a vnode into a uio and do it. 229 */ 230 int 231 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 232 enum uio_rw rw; 233 struct vnode *vp; 234 caddr_t base; 235 int len; 236 off_t offset; 237 enum uio_seg segflg; 238 int ioflg; 239 struct ucred *cred; 240 int *aresid; 241 struct proc *p; 242 { 243 struct uio auio; 244 struct iovec aiov; 245 int error; 246 247 if ((ioflg & IO_NODELOCKED) == 0) 248 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 249 auio.uio_iov = &aiov; 250 auio.uio_iovcnt = 1; 251 aiov.iov_base = base; 252 aiov.iov_len = len; 253 auio.uio_resid = len; 254 auio.uio_offset = offset; 255 auio.uio_segflg = segflg; 256 auio.uio_rw = rw; 257 auio.uio_procp = p; 258 if (rw == UIO_READ) { 259 error = VOP_READ(vp, &auio, ioflg, cred); 260 } else { 261 error = VOP_WRITE(vp, &auio, ioflg, cred); 262 } 263 if (aresid) 264 *aresid = auio.uio_resid; 265 else 266 if (auio.uio_resid && error == 0) 267 error = EIO; 268 if ((ioflg & IO_NODELOCKED) == 0) 269 VOP_UNLOCK(vp, 0, p); 270 return (error); 271 } 272 273 /* 274 * File table vnode read routine. 275 */ 276 static int 277 vn_read(fp, uio, cred, flags) 278 struct file *fp; 279 struct uio *uio; 280 struct ucred *cred; 281 int flags; 282 { 283 struct vnode *vp = (struct vnode *)fp->f_data; 284 struct proc *p = uio->uio_procp; 285 int error, ioflag; 286 287 ioflag = 0; 288 if (fp->f_flag & FNONBLOCK) 289 ioflag |= IO_NDELAY; 290 VOP_LEASE(vp, p, cred, LEASE_READ); 291 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p); 292 if ((flags & FOF_OFFSET) == 0) 293 uio->uio_offset = fp->f_offset; 294 295 /* 296 * Sequential read heuristic. 297 * If we have been doing sequential input, 298 * a rewind operation doesn't turn off 299 * sequential input mode. 300 */ 301 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 302 uio->uio_offset == fp->f_nextread) { 303 int tmpseq = fp->f_seqcount; 304 /* 305 * XXX we assume that the filesystem block size is 306 * the default. Not true, but still gives us a pretty 307 * good indicator of how sequential the read operations 308 * are. 309 */ 310 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE; 311 if (tmpseq >= 127) 312 tmpseq = 127; 313 fp->f_seqcount = tmpseq; 314 ioflag |= fp->f_seqcount << 16; 315 } else { 316 if (fp->f_seqcount > 1) 317 fp->f_seqcount = 1; 318 else 319 fp->f_seqcount = 0; 320 } 321 error = VOP_READ(vp, uio, ioflag, cred); 322 if ((flags & FOF_OFFSET) == 0) 323 fp->f_offset = uio->uio_offset; 324 fp->f_nextread = uio->uio_offset; 325 VOP_UNLOCK(vp, 0, p); 326 return (error); 327 } 328 329 /* 330 * File table vnode write routine. 331 */ 332 static int 333 vn_write(fp, uio, cred, flags) 334 struct file *fp; 335 struct uio *uio; 336 struct ucred *cred; 337 int flags; 338 { 339 struct vnode *vp; 340 struct proc *p = uio->uio_procp; 341 int error, ioflag; 342 343 vp = (struct vnode *)fp->f_data; 344 if (vp->v_type == VREG) 345 bwillwrite(); 346 vp = (struct vnode *)fp->f_data; /* XXX needed? */ 347 ioflag = IO_UNIT; 348 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 349 ioflag |= IO_APPEND; 350 if (fp->f_flag & FNONBLOCK) 351 ioflag |= IO_NDELAY; 352 if ((fp->f_flag & O_FSYNC) || 353 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 354 ioflag |= IO_SYNC; 355 VOP_LEASE(vp, p, cred, LEASE_WRITE); 356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 357 if ((flags & FOF_OFFSET) == 0) 358 uio->uio_offset = fp->f_offset; 359 error = VOP_WRITE(vp, uio, ioflag, cred); 360 if ((flags & FOF_OFFSET) == 0) 361 fp->f_offset = uio->uio_offset; 362 VOP_UNLOCK(vp, 0, p); 363 return (error); 364 } 365 366 /* 367 * File table vnode stat routine. 368 */ 369 int 370 vn_stat(vp, sb, p) 371 struct vnode *vp; 372 register struct stat *sb; 373 struct proc *p; 374 { 375 struct vattr vattr; 376 register struct vattr *vap; 377 int error; 378 u_short mode; 379 380 vap = &vattr; 381 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 382 if (error) 383 return (error); 384 /* 385 * Copy from vattr table 386 */ 387 if (vap->va_fsid != VNOVAL) 388 sb->st_dev = vap->va_fsid; 389 else 390 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 391 sb->st_ino = vap->va_fileid; 392 mode = vap->va_mode; 393 switch (vap->va_type) { 394 case VREG: 395 mode |= S_IFREG; 396 break; 397 case VDIR: 398 mode |= S_IFDIR; 399 break; 400 case VBLK: 401 mode |= S_IFBLK; 402 break; 403 case VCHR: 404 mode |= S_IFCHR; 405 break; 406 case VLNK: 407 mode |= S_IFLNK; 408 /* This is a cosmetic change, symlinks do not have a mode. */ 409 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 410 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 411 else 412 sb->st_mode |= ACCESSPERMS; /* 0777 */ 413 break; 414 case VSOCK: 415 mode |= S_IFSOCK; 416 break; 417 case VFIFO: 418 mode |= S_IFIFO; 419 break; 420 default: 421 return (EBADF); 422 }; 423 sb->st_mode = mode; 424 sb->st_nlink = vap->va_nlink; 425 sb->st_uid = vap->va_uid; 426 sb->st_gid = vap->va_gid; 427 sb->st_rdev = vap->va_rdev; 428 sb->st_size = vap->va_size; 429 sb->st_atimespec = vap->va_atime; 430 sb->st_mtimespec = vap->va_mtime; 431 sb->st_ctimespec = vap->va_ctime; 432 /* 433 * For block and char device nodes we don't really care 434 * about what the filesystem told us, we want to know 435 * what the device told us 436 */ 437 switch (vap->va_type) { 438 case VBLK: 439 sb->st_blksize = vp->v_rdev->si_bsize_best; 440 break; 441 case VCHR: 442 sb->st_blksize = vp->v_rdev->si_bsize_max; 443 break; 444 default: 445 sb->st_blksize = vap->va_blocksize; 446 break; 447 } 448 sb->st_flags = vap->va_flags; 449 if (suser_xxx(p->p_ucred, 0, 0)) 450 sb->st_gen = 0; 451 else 452 sb->st_gen = vap->va_gen; 453 454 #if (S_BLKSIZE == 512) 455 /* Optimize this case */ 456 sb->st_blocks = vap->va_bytes >> 9; 457 #else 458 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 459 #endif 460 return (0); 461 } 462 463 /* 464 * File table vnode ioctl routine. 465 */ 466 static int 467 vn_ioctl(fp, com, data, p) 468 struct file *fp; 469 u_long com; 470 caddr_t data; 471 struct proc *p; 472 { 473 register struct vnode *vp = ((struct vnode *)fp->f_data); 474 struct vattr vattr; 475 int error; 476 477 switch (vp->v_type) { 478 479 case VREG: 480 case VDIR: 481 if (com == FIONREAD) { 482 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 483 if (error) 484 return (error); 485 *(int *)data = vattr.va_size - fp->f_offset; 486 return (0); 487 } 488 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 489 return (0); /* XXX */ 490 /* fall into ... */ 491 492 default: 493 #if 0 494 return (ENOTTY); 495 #endif 496 case VFIFO: 497 case VCHR: 498 case VBLK: 499 if (com == FIODTYPE) { 500 if (vp->v_type != VCHR && vp->v_type != VBLK) 501 return (ENOTTY); 502 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK; 503 return (0); 504 } 505 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 506 if (error == 0 && com == TIOCSCTTY) { 507 508 /* Do nothing if reassigning same control tty */ 509 if (p->p_session->s_ttyvp == vp) 510 return (0); 511 512 /* Get rid of reference to old control tty */ 513 if (p->p_session->s_ttyvp) 514 vrele(p->p_session->s_ttyvp); 515 516 p->p_session->s_ttyvp = vp; 517 VREF(vp); 518 } 519 return (error); 520 } 521 } 522 523 /* 524 * File table vnode poll routine. 525 */ 526 static int 527 vn_poll(fp, events, cred, p) 528 struct file *fp; 529 int events; 530 struct ucred *cred; 531 struct proc *p; 532 { 533 534 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p)); 535 } 536 537 /* 538 * Check that the vnode is still valid, and if so 539 * acquire requested lock. 540 */ 541 int 542 #ifndef DEBUG_LOCKS 543 vn_lock(vp, flags, p) 544 #else 545 debug_vn_lock(vp, flags, p, filename, line) 546 #endif 547 struct vnode *vp; 548 int flags; 549 struct proc *p; 550 #ifdef DEBUG_LOCKS 551 const char *filename; 552 int line; 553 #endif 554 { 555 int error; 556 557 do { 558 if ((flags & LK_INTERLOCK) == 0) 559 simple_lock(&vp->v_interlock); 560 if (vp->v_flag & VXLOCK) { 561 vp->v_flag |= VXWANT; 562 simple_unlock(&vp->v_interlock); 563 tsleep((caddr_t)vp, PINOD, "vn_lock", 0); 564 error = ENOENT; 565 } else { 566 #ifdef DEBUG_LOCKS 567 vp->filename = filename; 568 vp->line = line; 569 #endif 570 error = VOP_LOCK(vp, 571 flags | LK_NOPAUSE | LK_INTERLOCK, p); 572 if (error == 0) 573 return (error); 574 } 575 flags &= ~LK_INTERLOCK; 576 } while (flags & LK_RETRY); 577 return (error); 578 } 579 580 /* 581 * File table vnode close routine. 582 */ 583 static int 584 vn_closefile(fp, p) 585 struct file *fp; 586 struct proc *p; 587 { 588 589 fp->f_ops = &badfileops; 590 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, 591 fp->f_cred, p)); 592 } 593