1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)vfs_vnops.c 7.8 (Berkeley) 07/03/89 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "user.h" 23 #include "kernel.h" 24 #include "file.h" 25 #include "stat.h" 26 #include "buf.h" 27 #include "proc.h" 28 #include "uio.h" 29 #include "socket.h" 30 #include "socketvar.h" 31 #include "mount.h" 32 #include "vnode.h" 33 #include "../ufs/inode.h" 34 #include "../ufs/fs.h" 35 #include "../ufs/quota.h" 36 #include "ioctl.h" 37 #include "tty.h" 38 39 int vn_read(), vn_write(), vn_ioctl(), vn_select(), vn_close(); 40 struct fileops vnops = 41 { vn_read, vn_write, vn_ioctl, vn_select, vn_close }; 42 43 /* 44 * Common code for vnode open operations. 45 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 46 */ 47 vn_open(ndp, fmode, cmode) 48 register struct nameidata *ndp; 49 int fmode, cmode; 50 { 51 register struct vnode *vp; 52 struct vattr vat; 53 struct vattr *vap = &vat; 54 int error; 55 56 if (fmode & FCREAT) { 57 ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF; 58 if ((fmode & FEXCL) == 0) 59 ndp->ni_nameiop |= FOLLOW; 60 if (error = namei(ndp)) 61 return (error); 62 if (ndp->ni_vp == NULL) { 63 vattr_null(vap); 64 vap->va_type = VREG; 65 vap->va_mode = cmode; 66 if (error = VOP_CREATE(ndp, vap)) 67 return (error); 68 fmode &= ~FTRUNC; 69 vp = ndp->ni_vp; 70 } else { 71 vp = ndp->ni_vp; 72 ndp->ni_vp = 0; 73 VOP_ABORTOP(ndp); 74 ndp->ni_vp = vp; 75 if (fmode & FEXCL) { 76 error = EEXIST; 77 goto bad; 78 } 79 fmode &= ~FCREAT; 80 } 81 } else { 82 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 83 if (error = namei(ndp)) 84 return (error); 85 vp = ndp->ni_vp; 86 } 87 if (vp->v_type == VSOCK) { 88 error = EOPNOTSUPP; 89 goto bad; 90 } 91 if ((fmode & FCREAT) == 0) { 92 if (fmode & FREAD) { 93 if (error = VOP_ACCESS(vp, VREAD, ndp->ni_cred)) 94 goto bad; 95 } 96 if (fmode & (FWRITE|FTRUNC)) { 97 if (vp->v_type == VDIR) { 98 error = EISDIR; 99 goto bad; 100 } 101 if ((error = vn_writechk(vp)) || 102 (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred))) 103 goto bad; 104 } 105 } 106 if (fmode & FTRUNC) { 107 vattr_null(vap); 108 vap->va_size = 0; 109 if (error = VOP_SETATTR(vp, vap, ndp->ni_cred)) 110 goto bad; 111 } 112 VOP_UNLOCK(vp); 113 if (setjmp(&u.u_qsave)) { 114 if (error == 0) 115 error = EINTR; 116 return (error); 117 } 118 error = VOP_OPEN(vp, fmode, ndp->ni_cred); 119 if (error) 120 vrele(vp); 121 return (error); 122 123 bad: 124 vput(vp); 125 return(error); 126 } 127 128 /* 129 * Check for write permissions on the specified vnode. 130 * The read-only status of the file system is checked. 131 * Also, prototype text segments cannot be written. 132 */ 133 vn_writechk(vp) 134 register struct vnode *vp; 135 { 136 137 /* 138 * Disallow write attempts on read-only file systems; 139 * unless the file is a socket or a block or character 140 * device resident on the file system. 141 */ 142 if ((vp->v_mount->m_flag & M_RDONLY) && vp->v_type != VCHR && 143 vp->v_type != VBLK && vp->v_type != VSOCK) 144 return (EROFS); 145 /* 146 * If there's shared text associated with 147 * the vnode, try to free it up once. If 148 * we fail, we can't allow writing. 149 */ 150 if (vp->v_flag & VTEXT) 151 xrele(vp); 152 if (vp->v_flag & VTEXT) 153 return (ETXTBSY); 154 return (0); 155 } 156 157 /* 158 * Vnode version of rdwri() for calls on file systems. 159 */ 160 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid) 161 enum uio_rw rw; 162 struct vnode *vp; 163 caddr_t base; 164 int len; 165 off_t offset; 166 enum uio_seg segflg; 167 int ioflg; 168 struct ucred *cred; 169 int *aresid; 170 { 171 struct uio auio; 172 struct iovec aiov; 173 int error; 174 175 auio.uio_iov = &aiov; 176 auio.uio_iovcnt = 1; 177 aiov.iov_base = base; 178 aiov.iov_len = len; 179 auio.uio_resid = len; 180 auio.uio_offset = offset; 181 auio.uio_segflg = segflg; 182 auio.uio_rw = rw; 183 if (rw == UIO_READ) 184 error = VOP_READ(vp, &auio, &offset, ioflg, cred); 185 else 186 error = VOP_WRITE(vp, &auio, &offset, ioflg, cred); 187 if (aresid) 188 *aresid = auio.uio_resid; 189 else 190 if (auio.uio_resid && error == 0) 191 error = EIO; 192 return (error); 193 } 194 195 vn_read(fp, uio, cred) 196 struct file *fp; 197 struct uio *uio; 198 struct ucred *cred; 199 { 200 201 return (VOP_READ((struct vnode *)fp->f_data, uio, &(fp->f_offset), 202 (fp->f_flag & FNDELAY) ? IO_NDELAY : 0, cred)); 203 } 204 205 vn_write(fp, uio, cred) 206 struct file *fp; 207 struct uio *uio; 208 struct ucred *cred; 209 { 210 register struct vnode *vp = (struct vnode *)fp->f_data; 211 int ioflag = 0; 212 213 if (vp->v_type == VREG && (fp->f_flag & FAPPEND)) 214 ioflag |= IO_APPEND; 215 if (fp->f_flag & FNDELAY) 216 ioflag |= IO_NDELAY; 217 return (VOP_WRITE(vp, uio, &(fp->f_offset), ioflag, cred)); 218 } 219 220 /* 221 * Get stat info for a vnode. 222 */ 223 vn_stat(vp, sb) 224 struct vnode *vp; 225 register struct stat *sb; 226 { 227 struct vattr vattr; 228 register struct vattr *vap; 229 int error; 230 u_short mode; 231 232 vap = &vattr; 233 error = VOP_GETATTR(vp, vap, u.u_cred); 234 if (error) 235 return (error); 236 /* 237 * Copy from vattr table 238 */ 239 sb->st_dev = vap->va_fsid; 240 sb->st_ino = vap->va_fileid; 241 mode = vap->va_mode; 242 switch (vp->v_type) { 243 case VREG: 244 mode |= IFREG; 245 break; 246 case VDIR: 247 mode |= IFDIR; 248 break; 249 case VBLK: 250 mode |= IFBLK; 251 break; 252 case VCHR: 253 mode |= IFCHR; 254 break; 255 case VLNK: 256 mode |= IFLNK; 257 break; 258 case VSOCK: 259 mode |= IFSOCK; 260 break; 261 default: 262 return (EBADF); 263 }; 264 sb->st_mode = mode; 265 sb->st_nlink = vap->va_nlink; 266 sb->st_uid = vap->va_uid; 267 sb->st_gid = vap->va_gid; 268 sb->st_rdev = vap->va_rdev; 269 sb->st_size = vap->va_size; 270 sb->st_atime = vap->va_atime.tv_sec; 271 sb->st_spare1 = 0; 272 sb->st_mtime = vap->va_mtime.tv_sec; 273 sb->st_spare2 = 0; 274 sb->st_ctime = vap->va_ctime.tv_sec; 275 sb->st_spare3 = 0; 276 sb->st_blksize = vap->va_blocksize; 277 sb->st_flags = vap->va_flags; 278 sb->st_gen = vap->va_gen; 279 /* 280 * XXX THIS IS NOT CORRECT!!, but be sure to change ufs_getattr() 281 * if you change it. 282 */ 283 sb->st_blocks = vap->va_bytes; 284 return (0); 285 } 286 287 /* 288 * Vnode ioctl call 289 */ 290 vn_ioctl(fp, com, data) 291 struct file *fp; 292 int com; 293 caddr_t data; 294 { 295 register struct vnode *vp = ((struct vnode *)fp->f_data); 296 struct vattr vattr; 297 int error; 298 299 switch (vp->v_type) { 300 301 case VREG: 302 case VDIR: 303 if (com == FIONREAD) { 304 if (error = VOP_GETATTR(vp, &vattr, u.u_cred)) 305 return (error); 306 *(off_t *)data = vattr.va_size - fp->f_offset; 307 return (0); 308 } 309 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 310 return (0); /* XXX */ 311 /* fall into ... */ 312 313 default: 314 return (ENOTTY); 315 316 case VCHR: 317 case VBLK: 318 u.u_r.r_val1 = 0; 319 if (setjmp(&u.u_qsave)) { 320 if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0) 321 return(EINTR); 322 u.u_eosys = RESTARTSYS; 323 return (0); 324 } 325 return (VOP_IOCTL(vp, com, data, fp->f_flag, u.u_cred)); 326 } 327 } 328 329 /* 330 * Vnode select call 331 */ 332 vn_select(fp, which) 333 struct file *fp; 334 int which; 335 { 336 return(VOP_SELECT(((struct vnode *)fp->f_data), which, u.u_cred)); 337 } 338 339 /* 340 * Vnode close call 341 */ 342 vn_close(fp) 343 register struct file *fp; 344 { 345 struct vnode *vp = ((struct vnode *)fp->f_data); 346 int error; 347 348 if (fp->f_flag & (FSHLOCK|FEXLOCK)) 349 vn_unlock(fp, FSHLOCK|FEXLOCK); 350 /* 351 * Must delete vnode reference from this file entry 352 * before VOP_CLOSE, so that only other references 353 * will prevent close. 354 */ 355 fp->f_data = (caddr_t) 0; 356 error = VOP_CLOSE(vp, fp->f_flag, u.u_cred); 357 vrele(vp); 358 return (error); 359 } 360 361 /* 362 * Place an advisory lock on a vnode. 363 * !! THIS IMPLIES THAT ALL STATEFUL FILE SERVERS WILL USE file table entries 364 */ 365 vn_lock(fp, cmd) 366 register struct file *fp; 367 int cmd; 368 { 369 register int priority = PLOCK; 370 register struct vnode *vp = (struct vnode *)fp->f_data; 371 372 if ((cmd & LOCK_EX) == 0) 373 priority += 4; 374 if (setjmp(&u.u_qsave)) { 375 if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0) 376 return(EINTR); 377 u.u_eosys = RESTARTSYS; 378 return (0); 379 } 380 /* 381 * If there's a exclusive lock currently applied 382 * to the file, then we've gotta wait for the 383 * lock with everyone else. 384 */ 385 again: 386 while (vp->v_flag & VEXLOCK) { 387 /* 388 * If we're holding an exclusive 389 * lock, then release it. 390 */ 391 if (fp->f_flag & FEXLOCK) { 392 vn_unlock(fp, FEXLOCK); 393 continue; 394 } 395 if (cmd & LOCK_NB) 396 return (EWOULDBLOCK); 397 vp->v_flag |= VLWAIT; 398 sleep((caddr_t)&vp->v_exlockc, priority); 399 } 400 if ((cmd & LOCK_EX) && (vp->v_flag & VSHLOCK)) { 401 /* 402 * Must wait for any shared locks to finish 403 * before we try to apply a exclusive lock. 404 * 405 * If we're holding a shared 406 * lock, then release it. 407 */ 408 if (fp->f_flag & FSHLOCK) { 409 vn_unlock(fp, FSHLOCK); 410 goto again; 411 } 412 if (cmd & LOCK_NB) 413 return (EWOULDBLOCK); 414 vp->v_flag |= VLWAIT; 415 sleep((caddr_t)&vp->v_shlockc, PLOCK); 416 goto again; 417 } 418 if (fp->f_flag & FEXLOCK) 419 panic("vn_lock"); 420 if (cmd & LOCK_EX) { 421 cmd &= ~LOCK_SH; 422 vp->v_exlockc++; 423 vp->v_flag |= VEXLOCK; 424 fp->f_flag |= FEXLOCK; 425 } 426 if ((cmd & LOCK_SH) && (fp->f_flag & FSHLOCK) == 0) { 427 vp->v_shlockc++; 428 vp->v_flag |= VSHLOCK; 429 fp->f_flag |= FSHLOCK; 430 } 431 return (0); 432 } 433 434 /* 435 * Unlock a file. 436 */ 437 vn_unlock(fp, kind) 438 register struct file *fp; 439 int kind; 440 { 441 register struct vnode *vp = (struct vnode *)fp->f_data; 442 int flags; 443 444 kind &= fp->f_flag; 445 if (vp == NULL || kind == 0) 446 return; 447 flags = vp->v_flag; 448 if (kind & FSHLOCK) { 449 if ((flags & VSHLOCK) == 0) 450 panic("vn_unlock: SHLOCK"); 451 if (--vp->v_shlockc == 0) { 452 vp->v_flag &= ~VSHLOCK; 453 if (flags & VLWAIT) 454 wakeup((caddr_t)&vp->v_shlockc); 455 } 456 fp->f_flag &= ~FSHLOCK; 457 } 458 if (kind & FEXLOCK) { 459 if ((flags & VEXLOCK) == 0) 460 panic("vn_unlock: EXLOCK"); 461 if (--vp->v_exlockc == 0) { 462 vp->v_flag &= ~(VEXLOCK|VLWAIT); 463 if (flags & VLWAIT) 464 wakeup((caddr_t)&vp->v_exlockc); 465 } 466 fp->f_flag &= ~FEXLOCK; 467 } 468 } 469 470 /* 471 * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) 472 * - look up fsid in mount list (if not found ret error) 473 * - get vp by calling VFS_FHTOVP() macro 474 * - if lockflag lock it with VOP_LOCK() 475 */ 476 vn_fhtovp(fhp, lockflag, vpp) 477 fhandle_t *fhp; 478 int lockflag; 479 struct vnode **vpp; 480 { 481 register struct mount *mp; 482 int error; 483 484 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 485 return (ESTALE); 486 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 487 return (ESTALE); 488 if (!lockflag) 489 VOP_UNLOCK(*vpp); 490 return (0); 491 } 492 493 /* 494 * Revoke access the current tty by all processes. 495 * Used only by the super-user in init 496 * to give ``clean'' terminals at login. 497 */ 498 vhangup() 499 { 500 501 if (u.u_error = suser(u.u_cred, &u.u_acflag)) 502 return; 503 if (u.u_ttyp == NULL) 504 return; 505 forceclose(u.u_ttyd); 506 if ((u.u_ttyp->t_state) & TS_ISOPEN) 507 gsignal(u.u_ttyp->t_pgid, SIGHUP); 508 u.u_ttyp->t_session = 0; 509 u.u_ttyp->t_pgid = 0; 510 } 511 512 forceclose(dev) 513 dev_t dev; 514 { 515 register struct file *fp; 516 register struct vnode *vp; 517 518 for (fp = file; fp < fileNFILE; fp++) { 519 if (fp->f_count == 0) 520 continue; 521 if (fp->f_type != DTYPE_VNODE) 522 continue; 523 vp = (struct vnode *)fp->f_data; 524 if (vp == 0) 525 continue; 526 if (vp->v_type != VCHR) 527 continue; 528 if (vp->v_rdev != dev) 529 continue; 530 fp->f_flag &= ~(FREAD|FWRITE); 531 } 532 } 533 534 /* 535 * Vnode reference, just increment the count 536 */ 537 void vref(vp) 538 struct vnode *vp; 539 { 540 541 vp->v_count++; 542 } 543 544 /* 545 * Vnode release, just decrement the count and call VOP_INACTIVE() 546 */ 547 void vrele(vp) 548 register struct vnode *vp; 549 { 550 551 if (vp == NULL) 552 return; 553 vp->v_count--; 554 if (vp->v_count < 0) 555 printf("inode %d, bad ref count %d\n", 556 VTOI(vp)->i_number, vp->v_count); 557 if (vp->v_count == 0) 558 VOP_INACTIVE(vp); 559 } 560 561 /* 562 * vput(), just unlock and vrele() 563 */ 564 vput(vp) 565 register struct vnode *vp; 566 { 567 VOP_UNLOCK(vp); 568 vrele(vp); 569 } 570 571 /* 572 * Noop 573 */ 574 vfs_noop() 575 { 576 577 return (ENXIO); 578 } 579 580 /* 581 * Null op 582 */ 583 vfs_nullop() 584 { 585 586 return (0); 587 } 588