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 * $Id: vfs_vnops.c,v 1.58 1998/06/07 17:11:48 dfr Exp $ 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/filio.h> 52 #include <sys/ttycom.h> 53 54 static int vn_closefile __P((struct file *fp, struct proc *p)); 55 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data, 56 struct proc *p)); 57 static int vn_read __P((struct file *fp, struct uio *uio, 58 struct ucred *cred)); 59 static int vn_poll __P((struct file *fp, int events, struct ucred *cred, 60 struct proc *p)); 61 static int vn_write __P((struct file *fp, struct uio *uio, 62 struct ucred *cred)); 63 64 struct fileops vnops = 65 { vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile }; 66 67 /* 68 * Common code for vnode open operations. 69 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 70 */ 71 int 72 vn_open(ndp, fmode, cmode) 73 register struct nameidata *ndp; 74 int fmode, cmode; 75 { 76 register struct vnode *vp; 77 register struct proc *p = ndp->ni_cnd.cn_proc; 78 register struct ucred *cred = p->p_ucred; 79 struct vattr vat; 80 struct vattr *vap = &vat; 81 int error; 82 83 if (fmode & O_CREAT) { 84 ndp->ni_cnd.cn_nameiop = CREATE; 85 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 86 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 87 ndp->ni_cnd.cn_flags |= FOLLOW; 88 error = namei(ndp); 89 if (error) 90 return (error); 91 if (ndp->ni_vp == NULL) { 92 VATTR_NULL(vap); 93 vap->va_type = VREG; 94 vap->va_mode = cmode; 95 if (fmode & O_EXCL) 96 vap->va_vaflags |= VA_EXCLUSIVE; 97 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE); 98 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 99 &ndp->ni_cnd, vap); 100 vput(ndp->ni_dvp); 101 if (error) 102 return (error); 103 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create"); 104 ASSERT_VOP_LOCKED(ndp->ni_vp, "create"); 105 fmode &= ~O_TRUNC; 106 vp = ndp->ni_vp; 107 } else { 108 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 109 if (ndp->ni_dvp == ndp->ni_vp) 110 vrele(ndp->ni_dvp); 111 else 112 vput(ndp->ni_dvp); 113 ndp->ni_dvp = NULL; 114 vp = ndp->ni_vp; 115 if (fmode & O_EXCL) { 116 error = EEXIST; 117 goto bad; 118 } 119 fmode &= ~O_CREAT; 120 } 121 } else { 122 ndp->ni_cnd.cn_nameiop = LOOKUP; 123 ndp->ni_cnd.cn_flags = 124 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 125 error = namei(ndp); 126 if (error) 127 return (error); 128 vp = ndp->ni_vp; 129 } 130 if (vp->v_type == VLNK) { 131 error = EMLINK; 132 goto bad; 133 } 134 if (vp->v_type == VSOCK) { 135 error = EOPNOTSUPP; 136 goto bad; 137 } 138 if ((fmode & O_CREAT) == 0) { 139 if (fmode & FREAD) { 140 error = VOP_ACCESS(vp, VREAD, cred, p); 141 if (error) 142 goto bad; 143 } 144 if (fmode & (FWRITE | O_TRUNC)) { 145 if (vp->v_type == VDIR) { 146 error = EISDIR; 147 goto bad; 148 } 149 error = vn_writechk(vp); 150 if (error) 151 goto bad; 152 error = VOP_ACCESS(vp, VWRITE, cred, p); 153 if (error) 154 goto bad; 155 } 156 } 157 if (fmode & O_TRUNC) { 158 VOP_UNLOCK(vp, 0, p); /* XXX */ 159 VOP_LEASE(vp, p, cred, LEASE_WRITE); 160 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */ 161 VATTR_NULL(vap); 162 vap->va_size = 0; 163 error = VOP_SETATTR(vp, vap, cred, p); 164 if (error) 165 goto bad; 166 } 167 error = VOP_OPEN(vp, fmode, cred, p); 168 if (error) 169 goto bad; 170 /* 171 * Make sure that a VM object is created for VMIO support. 172 */ 173 if (vp->v_type == VREG) { 174 if ((error = vfs_object_create(vp, p, cred, 1)) != 0) 175 goto bad; 176 } 177 178 if (fmode & FWRITE) 179 vp->v_writecount++; 180 return (0); 181 bad: 182 vput(vp); 183 return (error); 184 } 185 186 /* 187 * Check for write permissions on the specified vnode. 188 * Prototype text segments cannot be written. 189 */ 190 int 191 vn_writechk(vp) 192 register struct vnode *vp; 193 { 194 195 /* 196 * If there's shared text associated with 197 * the vnode, try to free it up once. If 198 * we fail, we can't allow writing. 199 */ 200 if (vp->v_flag & VTEXT) 201 return (ETXTBSY); 202 return (0); 203 } 204 205 /* 206 * Vnode close call 207 */ 208 int 209 vn_close(vp, flags, cred, p) 210 register struct vnode *vp; 211 int flags; 212 struct ucred *cred; 213 struct proc *p; 214 { 215 int error; 216 217 if (flags & FWRITE) 218 vp->v_writecount--; 219 error = VOP_CLOSE(vp, flags, cred, p); 220 vrele(vp); 221 return (error); 222 } 223 224 /* 225 * Package up an I/O request on a vnode into a uio and do it. 226 */ 227 int 228 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 229 enum uio_rw rw; 230 struct vnode *vp; 231 caddr_t base; 232 int len; 233 off_t offset; 234 enum uio_seg segflg; 235 int ioflg; 236 struct ucred *cred; 237 int *aresid; 238 struct proc *p; 239 { 240 struct uio auio; 241 struct iovec aiov; 242 int error; 243 244 if ((ioflg & IO_NODELOCKED) == 0) 245 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 246 auio.uio_iov = &aiov; 247 auio.uio_iovcnt = 1; 248 aiov.iov_base = base; 249 aiov.iov_len = len; 250 auio.uio_resid = len; 251 auio.uio_offset = offset; 252 auio.uio_segflg = segflg; 253 auio.uio_rw = rw; 254 auio.uio_procp = p; 255 if (rw == UIO_READ) { 256 error = VOP_READ(vp, &auio, ioflg, cred); 257 } else { 258 error = VOP_WRITE(vp, &auio, ioflg, cred); 259 } 260 if (aresid) 261 *aresid = auio.uio_resid; 262 else 263 if (auio.uio_resid && error == 0) 264 error = EIO; 265 if ((ioflg & IO_NODELOCKED) == 0) 266 VOP_UNLOCK(vp, 0, p); 267 return (error); 268 } 269 270 /* 271 * File table vnode read routine. 272 */ 273 static int 274 vn_read(fp, uio, cred) 275 struct file *fp; 276 struct uio *uio; 277 struct ucred *cred; 278 { 279 struct vnode *vp = (struct vnode *)fp->f_data; 280 struct proc *p = uio->uio_procp; 281 int count, error; 282 int flag; 283 284 VOP_LEASE(vp, p, cred, LEASE_READ); 285 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p); 286 if (uio->uio_offset == -1) 287 uio->uio_offset = fp->f_offset; 288 count = uio->uio_resid; 289 flag = 0; 290 if (fp->f_flag & FNONBLOCK) 291 flag |= IO_NDELAY; 292 293 /* 294 * Sequential read heuristic. 295 * If we have been doing sequential input, 296 * a rewind operation doesn't turn off 297 * sequential input mode. 298 */ 299 if (((fp->f_offset == 0) && (fp->f_seqcount > 0)) || 300 (fp->f_offset == fp->f_nextread)) { 301 int tmpseq = fp->f_seqcount; 302 /* 303 * XXX we assume that the filesystem block size is 304 * the default. Not true, but still gives us a pretty 305 * good indicator of how sequential the read operations 306 * are. 307 */ 308 tmpseq += ((count + BKVASIZE - 1) / BKVASIZE); 309 if (tmpseq >= 127) 310 tmpseq = 127; 311 fp->f_seqcount = tmpseq; 312 flag |= (fp->f_seqcount << 16); 313 } else { 314 if (fp->f_seqcount > 1) 315 fp->f_seqcount = 1; 316 else 317 fp->f_seqcount = 0; 318 } 319 320 error = VOP_READ(vp, uio, flag, cred); 321 fp->f_offset += count - uio->uio_resid; 322 fp->f_nextread = fp->f_offset; 323 VOP_UNLOCK(vp, 0, p); 324 return (error); 325 } 326 327 /* 328 * File table vnode write routine. 329 */ 330 static int 331 vn_write(fp, uio, cred) 332 struct file *fp; 333 struct uio *uio; 334 struct ucred *cred; 335 { 336 struct vnode *vp = (struct vnode *)fp->f_data; 337 struct proc *p = uio->uio_procp; 338 int count, error, ioflag = IO_UNIT; 339 340 if (uio->uio_offset == -1 && vp->v_type == VREG && (fp->f_flag & O_APPEND)) 341 ioflag |= IO_APPEND; 342 if (fp->f_flag & FNONBLOCK) 343 ioflag |= IO_NDELAY; 344 if ((fp->f_flag & O_FSYNC) || 345 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 346 ioflag |= IO_SYNC; 347 VOP_LEASE(vp, p, cred, LEASE_WRITE); 348 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 349 uio->uio_offset = fp->f_offset; 350 count = uio->uio_resid; 351 error = VOP_WRITE(vp, uio, ioflag, cred); 352 if (ioflag & IO_APPEND) 353 fp->f_offset = uio->uio_offset; 354 else 355 fp->f_offset += count - uio->uio_resid; 356 VOP_UNLOCK(vp, 0, p); 357 return (error); 358 } 359 360 /* 361 * File table vnode stat routine. 362 */ 363 int 364 vn_stat(vp, sb, p) 365 struct vnode *vp; 366 register struct stat *sb; 367 struct proc *p; 368 { 369 struct vattr vattr; 370 register struct vattr *vap; 371 int error; 372 u_short mode; 373 374 vap = &vattr; 375 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 376 if (error) 377 return (error); 378 /* 379 * Copy from vattr table 380 */ 381 sb->st_dev = vap->va_fsid; 382 sb->st_ino = vap->va_fileid; 383 mode = vap->va_mode; 384 switch (vap->va_type) { 385 case VREG: 386 mode |= S_IFREG; 387 break; 388 case VDIR: 389 mode |= S_IFDIR; 390 break; 391 case VBLK: 392 mode |= S_IFBLK; 393 break; 394 case VCHR: 395 mode |= S_IFCHR; 396 break; 397 case VLNK: 398 mode |= S_IFLNK; 399 /* This is a cosmetic change, symlinks do not have a mode. */ 400 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 401 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 402 else 403 sb->st_mode |= ACCESSPERMS; /* 0777 */ 404 break; 405 case VSOCK: 406 mode |= S_IFSOCK; 407 break; 408 case VFIFO: 409 mode |= S_IFIFO; 410 break; 411 default: 412 return (EBADF); 413 }; 414 sb->st_mode = mode; 415 sb->st_nlink = vap->va_nlink; 416 sb->st_uid = vap->va_uid; 417 sb->st_gid = vap->va_gid; 418 sb->st_rdev = vap->va_rdev; 419 sb->st_size = vap->va_size; 420 sb->st_atimespec = vap->va_atime; 421 sb->st_mtimespec = vap->va_mtime; 422 sb->st_ctimespec = vap->va_ctime; 423 sb->st_blksize = vap->va_blocksize; 424 sb->st_flags = vap->va_flags; 425 if (p->p_ucred->cr_uid != 0) 426 sb->st_gen = 0; 427 else 428 sb->st_gen = vap->va_gen; 429 430 #if (S_BLKSIZE == 512) 431 /* Optimize this case */ 432 sb->st_blocks = vap->va_bytes >> 9; 433 #else 434 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 435 #endif 436 return (0); 437 } 438 439 /* 440 * File table vnode ioctl routine. 441 */ 442 static int 443 vn_ioctl(fp, com, data, p) 444 struct file *fp; 445 u_long com; 446 caddr_t data; 447 struct proc *p; 448 { 449 register struct vnode *vp = ((struct vnode *)fp->f_data); 450 struct vattr vattr; 451 int error; 452 453 switch (vp->v_type) { 454 455 case VREG: 456 case VDIR: 457 if (com == FIONREAD) { 458 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 459 if (error) 460 return (error); 461 *(int *)data = vattr.va_size - fp->f_offset; 462 return (0); 463 } 464 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 465 return (0); /* XXX */ 466 /* fall into ... */ 467 468 default: 469 #if 0 470 return (ENOTTY); 471 #endif 472 case VFIFO: 473 case VCHR: 474 case VBLK: 475 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 476 if (error == 0 && com == TIOCSCTTY) { 477 478 /* Do nothing if reassigning same control tty */ 479 if (p->p_session->s_ttyvp == vp) 480 return (0); 481 482 /* Get rid of reference to old control tty */ 483 if (p->p_session->s_ttyvp) 484 vrele(p->p_session->s_ttyvp); 485 486 p->p_session->s_ttyvp = vp; 487 VREF(vp); 488 } 489 return (error); 490 } 491 } 492 493 /* 494 * File table vnode poll routine. 495 */ 496 static int 497 vn_poll(fp, events, cred, p) 498 struct file *fp; 499 int events; 500 struct ucred *cred; 501 struct proc *p; 502 { 503 504 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p)); 505 } 506 507 /* 508 * Check that the vnode is still valid, and if so 509 * acquire requested lock. 510 */ 511 int 512 vn_lock(vp, flags, p) 513 struct vnode *vp; 514 int flags; 515 struct proc *p; 516 { 517 int error; 518 519 do { 520 if ((flags & LK_INTERLOCK) == 0) 521 simple_lock(&vp->v_interlock); 522 if (vp->v_flag & VXLOCK) { 523 vp->v_flag |= VXWANT; 524 simple_unlock(&vp->v_interlock); 525 tsleep((caddr_t)vp, PINOD, "vn_lock", 0); 526 error = ENOENT; 527 } else { 528 error = VOP_LOCK(vp, flags | LK_NOPAUSE | LK_INTERLOCK, p); 529 if (error == 0) 530 return (error); 531 } 532 flags &= ~LK_INTERLOCK; 533 } while (flags & LK_RETRY); 534 return (error); 535 } 536 537 /* 538 * File table vnode close routine. 539 */ 540 static int 541 vn_closefile(fp, p) 542 struct file *fp; 543 struct proc *p; 544 { 545 546 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, 547 fp->f_cred, p)); 548 } 549