1 /* $OpenBSD: vfs_vnops.c,v 1.62 2009/01/29 22:08:45 guenther Exp $ */ 2 /* $NetBSD: vfs_vnops.c,v 1.20 1996/02/04 02:18:41 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)vfs_vnops.c 8.5 (Berkeley) 12/8/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/file.h> 45 #include <sys/stat.h> 46 #include <sys/buf.h> 47 #include <sys/proc.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/vnode.h> 51 #include <sys/ioctl.h> 52 #include <sys/tty.h> 53 #include <sys/cdio.h> 54 #include <sys/poll.h> 55 #include <sys/filedesc.h> 56 57 #include <uvm/uvm_extern.h> 58 #include <miscfs/specfs/specdev.h> 59 60 int vn_read(struct file *, off_t *, struct uio *, struct ucred *); 61 int vn_write(struct file *, off_t *, struct uio *, struct ucred *); 62 int vn_poll(struct file *, int, struct proc *); 63 int vn_kqfilter(struct file *, struct knote *); 64 int vn_closefile(struct file *, struct proc *); 65 66 struct fileops vnops = 67 { vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter, vn_statfile, 68 vn_closefile }; 69 70 /* 71 * Common code for vnode open operations. 72 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 73 */ 74 int 75 vn_open(struct nameidata *ndp, int fmode, int cmode) 76 { 77 struct vnode *vp; 78 struct proc *p = ndp->ni_cnd.cn_proc; 79 struct ucred *cred = p->p_ucred; 80 struct vattr va; 81 struct cloneinfo *cip; 82 int error; 83 84 if ((fmode & (FREAD|FWRITE)) == 0) 85 return (EINVAL); 86 if ((fmode & (O_TRUNC | FWRITE)) == O_TRUNC) 87 return (EINVAL); 88 if (fmode & O_CREAT) { 89 ndp->ni_cnd.cn_nameiop = CREATE; 90 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 91 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 92 ndp->ni_cnd.cn_flags |= FOLLOW; 93 if ((error = namei(ndp)) != 0) 94 return (error); 95 96 if (ndp->ni_vp == NULL) { 97 VATTR_NULL(&va); 98 va.va_type = VREG; 99 va.va_mode = cmode; 100 if (fmode & O_EXCL) 101 va.va_vaflags |= VA_EXCLUSIVE; 102 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 103 &ndp->ni_cnd, &va); 104 if (error) 105 return (error); 106 fmode &= ~O_TRUNC; 107 vp = ndp->ni_vp; 108 } else { 109 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 110 if (ndp->ni_dvp == ndp->ni_vp) 111 vrele(ndp->ni_dvp); 112 else 113 vput(ndp->ni_dvp); 114 ndp->ni_dvp = NULL; 115 vp = ndp->ni_vp; 116 if (fmode & O_EXCL) { 117 error = EEXIST; 118 goto bad; 119 } 120 fmode &= ~O_CREAT; 121 } 122 } else { 123 ndp->ni_cnd.cn_nameiop = LOOKUP; 124 ndp->ni_cnd.cn_flags = 125 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 126 if ((error = namei(ndp)) != 0) 127 return (error); 128 vp = ndp->ni_vp; 129 } 130 if (vp->v_type == VSOCK) { 131 error = EOPNOTSUPP; 132 goto bad; 133 } 134 if (vp->v_type == VLNK) { 135 error = EMLINK; 136 goto bad; 137 } 138 if ((fmode & O_CREAT) == 0) { 139 if (fmode & FREAD) { 140 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0) 141 goto bad; 142 } 143 if (fmode & FWRITE) { 144 if (vp->v_type == VDIR) { 145 error = EISDIR; 146 goto bad; 147 } 148 if ((error = vn_writechk(vp)) != 0 || 149 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0) 150 goto bad; 151 } 152 } 153 if ((fmode & O_TRUNC) && vp->v_type == VREG) { 154 VATTR_NULL(&va); 155 va.va_size = 0; 156 if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0) 157 goto bad; 158 } 159 if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0) 160 goto bad; 161 162 if (vp->v_flag & VCLONED) { 163 cip = (struct cloneinfo *)vp->v_data; 164 165 vp->v_flag &= ~VCLONED; 166 167 ndp->ni_vp = cip->ci_vp; /* return cloned vnode */ 168 vp->v_data = cip->ci_data; /* restore v_data */ 169 VOP_UNLOCK(vp, 0, p); /* keep a reference */ 170 vp = ndp->ni_vp; /* for the increment below */ 171 172 free(cip, M_TEMP); 173 } 174 175 if (fmode & FWRITE) 176 vp->v_writecount++; 177 return (0); 178 bad: 179 vput(vp); 180 return (error); 181 } 182 183 /* 184 * Check for write permissions on the specified vnode. 185 * Prototype text segments cannot be written. 186 */ 187 int 188 vn_writechk(struct vnode *vp) 189 { 190 /* 191 * Disallow write attempts on read-only file systems; 192 * unless the file is a socket or a block or character 193 * device resident on the file system. 194 */ 195 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 196 switch (vp->v_type) { 197 case VREG: 198 case VDIR: 199 case VLNK: 200 return (EROFS); 201 case VNON: 202 case VCHR: 203 case VSOCK: 204 case VFIFO: 205 case VBAD: 206 case VBLK: 207 break; 208 } 209 } 210 /* 211 * If there's shared text associated with 212 * the vnode, try to free it up once. If 213 * we fail, we can't allow writing. 214 */ 215 if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp)) 216 return (ETXTBSY); 217 218 return (0); 219 } 220 221 /* 222 * Mark a vnode as being the text image of a running process. 223 */ 224 void 225 vn_marktext(struct vnode *vp) 226 { 227 vp->v_flag |= VTEXT; 228 } 229 230 /* 231 * Vnode close call 232 */ 233 int 234 vn_close(struct vnode *vp, int flags, struct ucred *cred, struct proc *p) 235 { 236 int error; 237 238 if (flags & FWRITE) 239 vp->v_writecount--; 240 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 241 error = VOP_CLOSE(vp, flags, cred, p); 242 vput(vp); 243 return (error); 244 } 245 246 /* 247 * Package up an I/O request on a vnode into a uio and do it. 248 */ 249 int 250 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset, 251 enum uio_seg segflg, int ioflg, struct ucred *cred, size_t *aresid, 252 struct proc *p) 253 { 254 struct uio auio; 255 struct iovec aiov; 256 int error; 257 258 if ((ioflg & IO_NODELOCKED) == 0) 259 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 260 auio.uio_iov = &aiov; 261 auio.uio_iovcnt = 1; 262 aiov.iov_base = base; 263 aiov.iov_len = len; 264 auio.uio_resid = len; 265 auio.uio_offset = offset; 266 auio.uio_segflg = segflg; 267 auio.uio_rw = rw; 268 auio.uio_procp = p; 269 if (rw == UIO_READ) { 270 error = VOP_READ(vp, &auio, ioflg, cred); 271 } else { 272 error = VOP_WRITE(vp, &auio, ioflg, cred); 273 } 274 if (aresid) 275 *aresid = auio.uio_resid; 276 else 277 if (auio.uio_resid && error == 0) 278 error = EIO; 279 if ((ioflg & IO_NODELOCKED) == 0) 280 VOP_UNLOCK(vp, 0, p); 281 return (error); 282 } 283 284 /* 285 * File table vnode read routine. 286 */ 287 int 288 vn_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 289 { 290 struct vnode *vp = (struct vnode *)fp->f_data; 291 int error = 0; 292 size_t count; 293 struct proc *p = uio->uio_procp; 294 295 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 296 uio->uio_offset = *poff; 297 count = uio->uio_resid; 298 if (vp->v_type != VDIR) 299 error = VOP_READ(vp, uio, 300 (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, cred); 301 *poff += count - uio->uio_resid; 302 VOP_UNLOCK(vp, 0, p); 303 return (error); 304 } 305 306 /* 307 * File table vnode write routine. 308 */ 309 int 310 vn_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 311 { 312 struct vnode *vp = (struct vnode *)fp->f_data; 313 struct proc *p = uio->uio_procp; 314 int error, ioflag = IO_UNIT; 315 size_t count; 316 317 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 318 ioflag |= IO_APPEND; 319 if (fp->f_flag & FNONBLOCK) 320 ioflag |= IO_NDELAY; 321 if ((fp->f_flag & FFSYNC) || 322 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 323 ioflag |= IO_SYNC; 324 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 325 uio->uio_offset = *poff; 326 count = uio->uio_resid; 327 error = VOP_WRITE(vp, uio, ioflag, cred); 328 if (ioflag & IO_APPEND) 329 *poff = uio->uio_offset; 330 else 331 *poff += count - uio->uio_resid; 332 VOP_UNLOCK(vp, 0, p); 333 return (error); 334 } 335 336 /* 337 * File table wrapper for vn_stat 338 */ 339 int 340 vn_statfile(struct file *fp, struct stat *sb, struct proc *p) 341 { 342 struct vnode *vp = (struct vnode *)fp->f_data; 343 return vn_stat(vp, sb, p); 344 } 345 346 /* 347 * vnode stat routine. 348 */ 349 int 350 vn_stat(struct vnode *vp, struct stat *sb, struct proc *p) 351 { 352 struct vattr va; 353 int error; 354 mode_t mode; 355 356 error = VOP_GETATTR(vp, &va, p->p_ucred, p); 357 if (error) 358 return (error); 359 /* 360 * Copy from vattr table 361 */ 362 sb->st_dev = va.va_fsid; 363 sb->st_ino = va.va_fileid; 364 mode = va.va_mode; 365 switch (vp->v_type) { 366 case VREG: 367 mode |= S_IFREG; 368 break; 369 case VDIR: 370 mode |= S_IFDIR; 371 break; 372 case VBLK: 373 mode |= S_IFBLK; 374 break; 375 case VCHR: 376 mode |= S_IFCHR; 377 break; 378 case VLNK: 379 mode |= S_IFLNK; 380 break; 381 case VSOCK: 382 mode |= S_IFSOCK; 383 break; 384 case VFIFO: 385 mode |= S_IFIFO; 386 break; 387 default: 388 return (EBADF); 389 } 390 sb->st_mode = mode; 391 sb->st_nlink = va.va_nlink; 392 sb->st_uid = va.va_uid; 393 sb->st_gid = va.va_gid; 394 sb->st_rdev = va.va_rdev; 395 sb->st_size = va.va_size; 396 sb->st_atim = va.va_atime; 397 sb->st_mtim = va.va_mtime; 398 sb->st_ctim = va.va_ctime; 399 sb->st_blksize = va.va_blocksize; 400 sb->st_flags = va.va_flags; 401 sb->st_gen = va.va_gen; 402 sb->st_blocks = va.va_bytes / S_BLKSIZE; 403 return (0); 404 } 405 406 /* 407 * File table vnode ioctl routine. 408 */ 409 int 410 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p) 411 { 412 struct vnode *vp = ((struct vnode *)fp->f_data); 413 struct vattr vattr; 414 int error; 415 416 switch (vp->v_type) { 417 418 case VREG: 419 case VDIR: 420 if (com == FIONREAD) { 421 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 422 if (error) 423 return (error); 424 *(int *)data = vattr.va_size - fp->f_offset; 425 return (0); 426 } 427 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 428 return (0); /* XXX */ 429 /* FALLTHROUGH */ 430 default: 431 return (ENOTTY); 432 433 case VFIFO: 434 case VCHR: 435 case VBLK: 436 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 437 if (error == 0 && com == TIOCSCTTY) { 438 if (p->p_session->s_ttyvp) 439 vrele(p->p_session->s_ttyvp); 440 p->p_session->s_ttyvp = vp; 441 VREF(vp); 442 } 443 return (error); 444 } 445 } 446 447 /* 448 * File table vnode poll routine. 449 */ 450 int 451 vn_poll(struct file *fp, int events, struct proc *p) 452 { 453 return (VOP_POLL(((struct vnode *)fp->f_data), events, p)); 454 } 455 456 /* 457 * Check that the vnode is still valid, and if so 458 * acquire requested lock. 459 */ 460 int 461 vn_lock(struct vnode *vp, int flags, struct proc *p) 462 { 463 int error; 464 465 if ((flags & LK_RECURSEFAIL) == 0) 466 flags |= LK_CANRECURSE; 467 468 do { 469 if (vp->v_flag & VXLOCK) { 470 vp->v_flag |= VXWANT; 471 tsleep(vp, PINOD, "vn_lock", 0); 472 error = ENOENT; 473 } else { 474 error = VOP_LOCK(vp, flags, p); 475 if (error == 0) 476 return (error); 477 } 478 } while (flags & LK_RETRY); 479 return (error); 480 } 481 482 /* 483 * File table vnode close routine. 484 */ 485 int 486 vn_closefile(struct file *fp, struct proc *p) 487 { 488 struct vnode *vp = fp->f_data; 489 struct flock lf; 490 491 if ((fp->f_flag & FHASLOCK)) { 492 lf.l_whence = SEEK_SET; 493 lf.l_start = 0; 494 lf.l_len = 0; 495 lf.l_type = F_UNLCK; 496 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 497 } 498 499 return (vn_close(vp, fp->f_flag, fp->f_cred, p)); 500 } 501 502 int 503 vn_kqfilter(struct file *fp, struct knote *kn) 504 { 505 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn)); 506 } 507 508 /* 509 * Common code for vnode access operations. 510 */ 511 512 /* Check if a directory can be found inside another in the hierarchy */ 513 int 514 vn_isunder(struct vnode *lvp, struct vnode *rvp, struct proc *p) 515 { 516 int error; 517 518 error = vfs_getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p); 519 520 if (!error) 521 return (1); 522 523 return (0); 524 } 525