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