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.6 1994/10/05 09:48:26 davidg Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <sys/buf.h> 48 #include <sys/proc.h> 49 #include <sys/mount.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/ioctl.h> 53 #include <sys/tty.h> 54 55 #include <vm/vm.h> 56 57 struct fileops vnops = 58 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile }; 59 60 /* 61 * Common code for vnode open operations. 62 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 63 */ 64 int 65 vn_open(ndp, fmode, cmode) 66 register struct nameidata *ndp; 67 int fmode, cmode; 68 { 69 register struct vnode *vp; 70 register struct proc *p = ndp->ni_cnd.cn_proc; 71 register struct ucred *cred = p->p_ucred; 72 struct vattr vat; 73 struct vattr *vap = &vat; 74 int error; 75 76 if (fmode & O_CREAT) { 77 ndp->ni_cnd.cn_nameiop = CREATE; 78 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 79 if ((fmode & O_EXCL) == 0) 80 ndp->ni_cnd.cn_flags |= FOLLOW; 81 error = namei(ndp); 82 if (error) 83 return (error); 84 if (ndp->ni_vp == NULL) { 85 VATTR_NULL(vap); 86 vap->va_type = VREG; 87 vap->va_mode = cmode; 88 LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE); 89 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 90 &ndp->ni_cnd, vap); 91 if (error) 92 return (error); 93 fmode &= ~O_TRUNC; 94 vp = ndp->ni_vp; 95 } else { 96 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 97 if (ndp->ni_dvp == ndp->ni_vp) 98 vrele(ndp->ni_dvp); 99 else 100 vput(ndp->ni_dvp); 101 ndp->ni_dvp = NULL; 102 vp = ndp->ni_vp; 103 if (fmode & O_EXCL) { 104 error = EEXIST; 105 goto bad; 106 } 107 fmode &= ~O_CREAT; 108 } 109 } else { 110 ndp->ni_cnd.cn_nameiop = LOOKUP; 111 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF; 112 error = namei(ndp); 113 if (error) 114 return (error); 115 vp = ndp->ni_vp; 116 } 117 if (vp->v_type == VSOCK) { 118 error = EOPNOTSUPP; 119 goto bad; 120 } 121 if ((fmode & O_CREAT) == 0) { 122 if (fmode & FREAD) { 123 error = VOP_ACCESS(vp, VREAD, cred, p); 124 if (error) 125 goto bad; 126 } 127 if (fmode & (FWRITE | O_TRUNC)) { 128 if (vp->v_type == VDIR) { 129 error = EISDIR; 130 goto bad; 131 } 132 error = vn_writechk(vp); 133 if(error) 134 goto bad; 135 error = VOP_ACCESS(vp, VWRITE, cred, p); 136 if(error) 137 goto bad; 138 } 139 } 140 if (fmode & O_TRUNC) { 141 VOP_UNLOCK(vp); /* XXX */ 142 LEASE_CHECK(vp, p, cred, LEASE_WRITE); 143 VOP_LOCK(vp); /* XXX */ 144 VATTR_NULL(vap); 145 vap->va_size = 0; 146 error = VOP_SETATTR(vp, vap, cred, p); 147 if (error) 148 goto bad; 149 } 150 error = VOP_OPEN(vp, fmode, cred, p); 151 if (error) 152 goto bad; 153 if (fmode & FWRITE) 154 vp->v_writecount++; 155 /* 156 * this is here for VMIO support 157 */ 158 if( vp->v_type == VREG) { 159 vm_object_t object; 160 vm_pager_t pager; 161 retry: 162 if( (vp->v_flag & VVMIO) == 0) { 163 pager = (vm_pager_t) vnode_pager_alloc(vp, 0, 0, 0); 164 object = (vm_object_t) vp->v_vmdata; 165 if( object->pager != pager) 166 panic("vn_open: pager/object mismatch"); 167 (void) vm_object_lookup( pager); 168 pager_cache( object, TRUE); 169 vp->v_flag |= VVMIO; 170 } else { 171 if( (object = (vm_object_t)vp->v_vmdata) && 172 (object->flags & OBJ_DEAD)) { 173 tsleep( (caddr_t) object, PVM, "vodead", 0); 174 goto retry; 175 } 176 if( !object) 177 panic("vn_open: VMIO object missing"); 178 pager = object->pager; 179 if( !pager) 180 panic("vn_open: VMIO pager missing"); 181 (void) vm_object_lookup( pager); 182 } 183 } 184 return (0); 185 bad: 186 vput(vp); 187 return (error); 188 } 189 190 /* 191 * Check for write permissions on the specified vnode. 192 * The read-only status of the file system is checked. 193 * Also, prototype text segments cannot be written. 194 */ 195 int 196 vn_writechk(vp) 197 register struct vnode *vp; 198 { 199 200 /* 201 * Disallow write attempts on read-only file systems; 202 * unless the file is a socket or a block or character 203 * device resident on the file system. 204 */ 205 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 206 switch (vp->v_type) { 207 case VREG: case VDIR: case VLNK: 208 return (EROFS); 209 default: 210 break; 211 } 212 } 213 /* 214 * If there's shared text associated with 215 * the vnode, try to free it up once. If 216 * we fail, we can't allow writing. 217 */ 218 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) 219 return (ETXTBSY); 220 return (0); 221 } 222 223 /* 224 * Vnode close call 225 */ 226 int 227 vn_close(vp, flags, cred, p) 228 register struct vnode *vp; 229 int flags; 230 struct ucred *cred; 231 struct proc *p; 232 { 233 int error; 234 235 if (flags & FWRITE) 236 vp->v_writecount--; 237 error = VOP_CLOSE(vp, flags, cred, p); 238 /* 239 * this code is here for VMIO support, will eventually 240 * be in vfs code. 241 */ 242 if (vp->v_flag & VVMIO) { 243 vrele(vp); 244 if( vp->v_vmdata == NULL) 245 panic("vn_close: VMIO object missing"); 246 vm_object_deallocate( (vm_object_t) vp->v_vmdata); 247 } else 248 vrele(vp); 249 return (error); 250 } 251 252 /* 253 * Package up an I/O request on a vnode into a uio and do it. 254 */ 255 int 256 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 257 enum uio_rw rw; 258 struct vnode *vp; 259 caddr_t base; 260 int len; 261 off_t offset; 262 enum uio_seg segflg; 263 int ioflg; 264 struct ucred *cred; 265 int *aresid; 266 struct proc *p; 267 { 268 struct uio auio; 269 struct iovec aiov; 270 int error; 271 272 if ((ioflg & IO_NODELOCKED) == 0) 273 VOP_LOCK(vp); 274 auio.uio_iov = &aiov; 275 auio.uio_iovcnt = 1; 276 aiov.iov_base = base; 277 aiov.iov_len = len; 278 auio.uio_resid = len; 279 auio.uio_offset = offset; 280 auio.uio_segflg = segflg; 281 auio.uio_rw = rw; 282 auio.uio_procp = p; 283 if (rw == UIO_READ) { 284 error = VOP_READ(vp, &auio, ioflg, cred); 285 } else { 286 error = VOP_WRITE(vp, &auio, ioflg, cred); 287 } 288 if (aresid) 289 *aresid = auio.uio_resid; 290 else 291 if (auio.uio_resid && error == 0) 292 error = EIO; 293 if ((ioflg & IO_NODELOCKED) == 0) 294 VOP_UNLOCK(vp); 295 return (error); 296 } 297 298 /* 299 * File table vnode read routine. 300 */ 301 int 302 vn_read(fp, uio, cred) 303 struct file *fp; 304 struct uio *uio; 305 struct ucred *cred; 306 { 307 register struct vnode *vp = (struct vnode *)fp->f_data; 308 int count, error; 309 310 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ); 311 VOP_LOCK(vp); 312 uio->uio_offset = fp->f_offset; 313 count = uio->uio_resid; 314 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, 315 cred); 316 fp->f_offset += count - uio->uio_resid; 317 VOP_UNLOCK(vp); 318 return (error); 319 } 320 321 /* 322 * File table vnode write routine. 323 */ 324 int 325 vn_write(fp, uio, cred) 326 struct file *fp; 327 struct uio *uio; 328 struct ucred *cred; 329 { 330 register struct vnode *vp = (struct vnode *)fp->f_data; 331 int count, error, ioflag = 0; 332 333 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 334 ioflag |= IO_APPEND; 335 if (fp->f_flag & FNONBLOCK) 336 ioflag |= IO_NDELAY; 337 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE); 338 VOP_LOCK(vp); 339 uio->uio_offset = fp->f_offset; 340 count = uio->uio_resid; 341 error = VOP_WRITE(vp, uio, ioflag, cred); 342 if (ioflag & IO_APPEND) 343 fp->f_offset = uio->uio_offset; 344 else 345 fp->f_offset += count - uio->uio_resid; 346 VOP_UNLOCK(vp); 347 return (error); 348 } 349 350 /* 351 * File table vnode stat routine. 352 */ 353 int 354 vn_stat(vp, sb, p) 355 struct vnode *vp; 356 register struct stat *sb; 357 struct proc *p; 358 { 359 struct vattr vattr; 360 register struct vattr *vap; 361 int error; 362 u_short mode; 363 364 vap = &vattr; 365 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 366 if (error) 367 return (error); 368 /* 369 * Copy from vattr table 370 */ 371 sb->st_dev = vap->va_fsid; 372 sb->st_ino = vap->va_fileid; 373 mode = vap->va_mode; 374 switch (vp->v_type) { 375 case VREG: 376 mode |= S_IFREG; 377 break; 378 case VDIR: 379 mode |= S_IFDIR; 380 break; 381 case VBLK: 382 mode |= S_IFBLK; 383 break; 384 case VCHR: 385 mode |= S_IFCHR; 386 break; 387 case VLNK: 388 mode |= S_IFLNK; 389 break; 390 case VSOCK: 391 mode |= S_IFSOCK; 392 break; 393 case VFIFO: 394 mode |= S_IFIFO; 395 break; 396 default: 397 return (EBADF); 398 }; 399 sb->st_mode = mode; 400 sb->st_nlink = vap->va_nlink; 401 sb->st_uid = vap->va_uid; 402 sb->st_gid = vap->va_gid; 403 sb->st_rdev = vap->va_rdev; 404 sb->st_size = vap->va_size; 405 sb->st_atimespec = vap->va_atime; 406 sb->st_mtimespec= vap->va_mtime; 407 sb->st_ctimespec = vap->va_ctime; 408 sb->st_blksize = vap->va_blocksize; 409 sb->st_flags = vap->va_flags; 410 sb->st_gen = vap->va_gen; 411 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 412 return (0); 413 } 414 415 /* 416 * File table vnode ioctl routine. 417 */ 418 int 419 vn_ioctl(fp, com, data, p) 420 struct file *fp; 421 int com; 422 caddr_t data; 423 struct proc *p; 424 { 425 register struct vnode *vp = ((struct vnode *)fp->f_data); 426 struct vattr vattr; 427 int error; 428 429 switch (vp->v_type) { 430 431 case VREG: 432 case VDIR: 433 if (com == FIONREAD) { 434 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 435 if (error) 436 return (error); 437 *(int *)data = vattr.va_size - fp->f_offset; 438 return (0); 439 } 440 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 441 return (0); /* XXX */ 442 /* fall into ... */ 443 444 default: 445 return (ENOTTY); 446 447 case VFIFO: 448 case VCHR: 449 case VBLK: 450 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 451 if (error == 0 && com == TIOCSCTTY) { 452 453 /* Do nothing if reassigning same control tty */ 454 if (p->p_session->s_ttyvp == vp) 455 return (0); 456 457 /* Get rid of reference to old control tty */ 458 if (p->p_session->s_ttyvp) 459 vrele(p->p_session->s_ttyvp); 460 461 p->p_session->s_ttyvp = vp; 462 VREF(vp); 463 } 464 return (error); 465 } 466 } 467 468 /* 469 * File table vnode select routine. 470 */ 471 int 472 vn_select(fp, which, p) 473 struct file *fp; 474 int which; 475 struct proc *p; 476 { 477 478 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, 479 fp->f_cred, p)); 480 } 481 482 /* 483 * File table vnode close routine. 484 */ 485 int 486 vn_closefile(fp, p) 487 struct file *fp; 488 struct proc *p; 489 { 490 491 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, 492 fp->f_cred, p)); 493 } 494