1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ufs_vnops.c 8.7 (Berkeley) 01/04/94 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/namei.h> 13 #include <sys/resourcevar.h> 14 #include <sys/kernel.h> 15 #include <sys/file.h> 16 #include <sys/stat.h> 17 #include <sys/buf.h> 18 #include <sys/proc.h> 19 #include <sys/conf.h> 20 #include <sys/mount.h> 21 #include <sys/vnode.h> 22 #include <sys/malloc.h> 23 #include <sys/dirent.h> 24 25 #include <vm/vm.h> 26 27 #include <miscfs/specfs/specdev.h> 28 29 #include <ufs/ufs/lockf.h> 30 #include <ufs/ufs/quota.h> 31 #include <ufs/ufs/inode.h> 32 #include <ufs/ufs/dir.h> 33 #include <ufs/ufs/ufsmount.h> 34 #include <ufs/ufs/ufs_extern.h> 35 36 static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *)); 37 static int ufs_chown 38 __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *)); 39 40 union _qcvt { 41 quad_t qcvt; 42 long val[2]; 43 }; 44 #define SETHIGH(q, h) { \ 45 union _qcvt tmp; \ 46 tmp.qcvt = (q); \ 47 tmp.val[_QUAD_HIGHWORD] = (h); \ 48 (q) = tmp.qcvt; \ 49 } 50 #define SETLOW(q, l) { \ 51 union _qcvt tmp; \ 52 tmp.qcvt = (q); \ 53 tmp.val[_QUAD_LOWWORD] = (l); \ 54 (q) = tmp.qcvt; \ 55 } 56 57 /* 58 * Create a regular file 59 */ 60 int 61 ufs_create(ap) 62 struct vop_create_args /* { 63 struct vnode *a_dvp; 64 struct vnode **a_vpp; 65 struct componentname *a_cnp; 66 struct vattr *a_vap; 67 } */ *ap; 68 { 69 int error; 70 71 if (error = 72 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), 73 ap->a_dvp, ap->a_vpp, ap->a_cnp)) 74 return (error); 75 return (0); 76 } 77 78 /* 79 * Mknod vnode call 80 */ 81 /* ARGSUSED */ 82 int 83 ufs_mknod(ap) 84 struct vop_mknod_args /* { 85 struct vnode *a_dvp; 86 struct vnode **a_vpp; 87 struct componentname *a_cnp; 88 struct vattr *a_vap; 89 } */ *ap; 90 { 91 register struct vattr *vap = ap->a_vap; 92 register struct vnode **vpp = ap->a_vpp; 93 register struct inode *ip; 94 int error; 95 96 if (error = 97 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 98 ap->a_dvp, vpp, ap->a_cnp)) 99 return (error); 100 ip = VTOI(*vpp); 101 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 102 if (vap->va_rdev != VNOVAL) { 103 /* 104 * Want to be able to use this to make badblock 105 * inodes, so don't truncate the dev number. 106 */ 107 ip->i_rdev = vap->va_rdev; 108 } 109 /* 110 * Remove inode so that it will be reloaded by iget and 111 * checked to see if it is an alias of an existing entry 112 * in the inode cache. 113 */ 114 vput(*vpp); 115 (*vpp)->v_type = VNON; 116 vgone(*vpp); 117 *vpp = 0; 118 return (0); 119 } 120 121 /* 122 * Open called. 123 * 124 * Nothing to do. 125 */ 126 /* ARGSUSED */ 127 int 128 ufs_open(ap) 129 struct vop_open_args /* { 130 struct vnode *a_vp; 131 int a_mode; 132 struct ucred *a_cred; 133 struct proc *a_p; 134 } */ *ap; 135 { 136 137 /* 138 * Files marked append-only must be opened for appending. 139 */ 140 if ((VTOI(ap->a_vp)->i_flags & APPEND) && 141 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 142 return (EPERM); 143 return (0); 144 } 145 146 /* 147 * Close called. 148 * 149 * Update the times on the inode. 150 */ 151 /* ARGSUSED */ 152 int 153 ufs_close(ap) 154 struct vop_close_args /* { 155 struct vnode *a_vp; 156 int a_fflag; 157 struct ucred *a_cred; 158 struct proc *a_p; 159 } */ *ap; 160 { 161 register struct vnode *vp = ap->a_vp; 162 register struct inode *ip = VTOI(vp); 163 164 if (vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED)) 165 ITIMES(ip, &time, &time); 166 return (0); 167 } 168 169 int 170 ufs_access(ap) 171 struct vop_access_args /* { 172 struct vnode *a_vp; 173 int a_mode; 174 struct ucred *a_cred; 175 struct proc *a_p; 176 } */ *ap; 177 { 178 register struct vnode *vp = ap->a_vp; 179 register struct inode *ip = VTOI(vp); 180 register struct ucred *cred = ap->a_cred; 181 mode_t mask, mode = ap->a_mode; 182 register gid_t *gp; 183 int i, error; 184 185 #ifdef DIAGNOSTIC 186 if (!VOP_ISLOCKED(vp)) { 187 vprint("ufs_access: not locked", vp); 188 panic("ufs_access: not locked"); 189 } 190 #endif 191 #ifdef QUOTA 192 if (mode & VWRITE) 193 switch (vp->v_type) { 194 case VDIR: 195 case VLNK: 196 case VREG: 197 if (error = getinoquota(ip)) 198 return (error); 199 break; 200 } 201 #endif 202 203 /* If immutable bit set, nobody gets to write it. */ 204 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE)) 205 return (EPERM); 206 207 /* Otherwise, user id 0 always gets access. */ 208 if (cred->cr_uid == 0) 209 return (0); 210 211 mask = 0; 212 213 /* Otherwise, check the owner. */ 214 if (cred->cr_uid == ip->i_uid) { 215 if (mode & VEXEC) 216 mask |= S_IXUSR; 217 if (mode & VREAD) 218 mask |= S_IRUSR; 219 if (mode & VWRITE) 220 mask |= S_IWUSR; 221 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 222 } 223 224 /* Otherwise, check the groups. */ 225 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) 226 if (ip->i_gid == *gp) { 227 if (mode & VEXEC) 228 mask |= S_IXGRP; 229 if (mode & VREAD) 230 mask |= S_IRGRP; 231 if (mode & VWRITE) 232 mask |= S_IWGRP; 233 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 234 } 235 236 /* Otherwise, check everyone else. */ 237 if (mode & VEXEC) 238 mask |= S_IXOTH; 239 if (mode & VREAD) 240 mask |= S_IROTH; 241 if (mode & VWRITE) 242 mask |= S_IWOTH; 243 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 244 } 245 246 /* ARGSUSED */ 247 int 248 ufs_getattr(ap) 249 struct vop_getattr_args /* { 250 struct vnode *a_vp; 251 struct vattr *a_vap; 252 struct ucred *a_cred; 253 struct proc *a_p; 254 } */ *ap; 255 { 256 register struct vnode *vp = ap->a_vp; 257 register struct inode *ip = VTOI(vp); 258 register struct vattr *vap = ap->a_vap; 259 260 ITIMES(ip, &time, &time); 261 /* 262 * Copy from inode table 263 */ 264 vap->va_fsid = ip->i_dev; 265 vap->va_fileid = ip->i_number; 266 vap->va_mode = ip->i_mode & ~IFMT; 267 vap->va_nlink = ip->i_nlink; 268 vap->va_uid = ip->i_uid; 269 vap->va_gid = ip->i_gid; 270 vap->va_rdev = (dev_t)ip->i_rdev; 271 vap->va_size = ip->i_din.di_size; 272 vap->va_atime = ip->i_atime; 273 vap->va_mtime = ip->i_mtime; 274 vap->va_ctime = ip->i_ctime; 275 vap->va_flags = ip->i_flags; 276 vap->va_gen = ip->i_gen; 277 /* this doesn't belong here */ 278 if (vp->v_type == VBLK) 279 vap->va_blocksize = BLKDEV_IOSIZE; 280 else if (vp->v_type == VCHR) 281 vap->va_blocksize = MAXBSIZE; 282 else 283 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 284 vap->va_bytes = dbtob(ip->i_blocks); 285 vap->va_type = vp->v_type; 286 vap->va_filerev = ip->i_modrev; 287 return (0); 288 } 289 290 /* 291 * Set attribute vnode op. called from several syscalls 292 */ 293 int 294 ufs_setattr(ap) 295 struct vop_setattr_args /* { 296 struct vnode *a_vp; 297 struct vattr *a_vap; 298 struct ucred *a_cred; 299 struct proc *a_p; 300 } */ *ap; 301 { 302 register struct vattr *vap = ap->a_vap; 303 register struct vnode *vp = ap->a_vp; 304 register struct inode *ip = VTOI(vp); 305 register struct ucred *cred = ap->a_cred; 306 register struct proc *p = ap->a_p; 307 struct timeval atimeval, mtimeval; 308 int error; 309 310 /* 311 * Check for unsettable attributes. 312 */ 313 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 314 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 315 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 316 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 317 return (EINVAL); 318 } 319 if (vap->va_flags != VNOVAL) { 320 if (cred->cr_uid != ip->i_uid && 321 (error = suser(cred, &p->p_acflag))) 322 return (error); 323 if (cred->cr_uid == 0) { 324 if ((ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) && 325 securelevel > 0) 326 return (EPERM); 327 ip->i_flags = vap->va_flags; 328 } else { 329 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) 330 return (EPERM); 331 ip->i_flags &= SF_SETTABLE; 332 ip->i_flags |= (vap->va_flags & UF_SETTABLE); 333 } 334 ip->i_flag |= IN_CHANGE; 335 if (vap->va_flags & (IMMUTABLE | APPEND)) 336 return (0); 337 } 338 if (ip->i_flags & (IMMUTABLE | APPEND)) 339 return (EPERM); 340 /* 341 * Go through the fields and update iff not VNOVAL. 342 */ 343 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) 344 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p)) 345 return (error); 346 if (vap->va_size != VNOVAL) { 347 if (vp->v_type == VDIR) 348 return (EISDIR); 349 if (error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p)) 350 return (error); 351 } 352 ip = VTOI(vp); 353 if (vap->va_atime.ts_sec != VNOVAL || vap->va_mtime.ts_sec != VNOVAL) { 354 if (cred->cr_uid != ip->i_uid && 355 (error = suser(cred, &p->p_acflag)) && 356 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 357 (error = VOP_ACCESS(vp, VWRITE, cred, p)))) 358 return (error); 359 if (vap->va_atime.ts_sec != VNOVAL) 360 ip->i_flag |= IN_ACCESS; 361 if (vap->va_mtime.ts_sec != VNOVAL) 362 ip->i_flag |= IN_CHANGE | IN_UPDATE; 363 atimeval.tv_sec = vap->va_atime.ts_sec; 364 atimeval.tv_usec = vap->va_atime.ts_nsec / 1000; 365 mtimeval.tv_sec = vap->va_mtime.ts_sec; 366 mtimeval.tv_usec = vap->va_mtime.ts_nsec / 1000; 367 if (error = VOP_UPDATE(vp, &atimeval, &mtimeval, 1)) 368 return (error); 369 } 370 error = 0; 371 if (vap->va_mode != (mode_t)VNOVAL) 372 error = ufs_chmod(vp, (int)vap->va_mode, cred, p); 373 return (error); 374 } 375 376 /* 377 * Change the mode on a file. 378 * Inode must be locked before calling. 379 */ 380 static int 381 ufs_chmod(vp, mode, cred, p) 382 register struct vnode *vp; 383 register int mode; 384 register struct ucred *cred; 385 struct proc *p; 386 { 387 register struct inode *ip = VTOI(vp); 388 int error; 389 390 if (cred->cr_uid != ip->i_uid && 391 (error = suser(cred, &p->p_acflag))) 392 return (error); 393 if (cred->cr_uid) { 394 if (vp->v_type != VDIR && (mode & S_ISTXT)) 395 return (EFTYPE); 396 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) 397 return (EPERM); 398 } 399 ip->i_mode &= ~ALLPERMS; 400 ip->i_mode |= (mode & ALLPERMS); 401 ip->i_flag |= IN_CHANGE; 402 if ((vp->v_flag & VTEXT) && (ip->i_mode & S_ISTXT) == 0) 403 (void) vnode_pager_uncache(vp); 404 return (0); 405 } 406 407 /* 408 * Perform chown operation on inode ip; 409 * inode must be locked prior to call. 410 */ 411 static int 412 ufs_chown(vp, uid, gid, cred, p) 413 register struct vnode *vp; 414 uid_t uid; 415 gid_t gid; 416 struct ucred *cred; 417 struct proc *p; 418 { 419 register struct inode *ip = VTOI(vp); 420 uid_t ouid; 421 gid_t ogid; 422 int error = 0; 423 #ifdef QUOTA 424 register int i; 425 long change; 426 #endif 427 428 if (uid == (uid_t)VNOVAL) 429 uid = ip->i_uid; 430 if (gid == (gid_t)VNOVAL) 431 gid = ip->i_gid; 432 /* 433 * If we don't own the file, are trying to change the owner 434 * of the file, or are not a member of the target group, 435 * the caller must be superuser or the call fails. 436 */ 437 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 438 !groupmember((gid_t)gid, cred)) && 439 (error = suser(cred, &p->p_acflag))) 440 return (error); 441 ogid = ip->i_gid; 442 ouid = ip->i_uid; 443 #ifdef QUOTA 444 if (error = getinoquota(ip)) 445 return (error); 446 if (ouid == uid) { 447 dqrele(vp, ip->i_dquot[USRQUOTA]); 448 ip->i_dquot[USRQUOTA] = NODQUOT; 449 } 450 if (ogid == gid) { 451 dqrele(vp, ip->i_dquot[GRPQUOTA]); 452 ip->i_dquot[GRPQUOTA] = NODQUOT; 453 } 454 change = ip->i_blocks; 455 (void) chkdq(ip, -change, cred, CHOWN); 456 (void) chkiq(ip, -1, cred, CHOWN); 457 for (i = 0; i < MAXQUOTAS; i++) { 458 dqrele(vp, ip->i_dquot[i]); 459 ip->i_dquot[i] = NODQUOT; 460 } 461 #endif 462 ip->i_gid = gid; 463 ip->i_uid = uid; 464 #ifdef QUOTA 465 if ((error = getinoquota(ip)) == 0) { 466 if (ouid == uid) { 467 dqrele(vp, ip->i_dquot[USRQUOTA]); 468 ip->i_dquot[USRQUOTA] = NODQUOT; 469 } 470 if (ogid == gid) { 471 dqrele(vp, ip->i_dquot[GRPQUOTA]); 472 ip->i_dquot[GRPQUOTA] = NODQUOT; 473 } 474 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { 475 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) 476 goto good; 477 else 478 (void) chkdq(ip, -change, cred, CHOWN|FORCE); 479 } 480 for (i = 0; i < MAXQUOTAS; i++) { 481 dqrele(vp, ip->i_dquot[i]); 482 ip->i_dquot[i] = NODQUOT; 483 } 484 } 485 ip->i_gid = ogid; 486 ip->i_uid = ouid; 487 if (getinoquota(ip) == 0) { 488 if (ouid == uid) { 489 dqrele(vp, ip->i_dquot[USRQUOTA]); 490 ip->i_dquot[USRQUOTA] = NODQUOT; 491 } 492 if (ogid == gid) { 493 dqrele(vp, ip->i_dquot[GRPQUOTA]); 494 ip->i_dquot[GRPQUOTA] = NODQUOT; 495 } 496 (void) chkdq(ip, change, cred, FORCE|CHOWN); 497 (void) chkiq(ip, 1, cred, FORCE|CHOWN); 498 (void) getinoquota(ip); 499 } 500 return (error); 501 good: 502 if (getinoquota(ip)) 503 panic("chown: lost quota"); 504 #endif /* QUOTA */ 505 if (ouid != uid || ogid != gid) 506 ip->i_flag |= IN_CHANGE; 507 if (ouid != uid && cred->cr_uid != 0) 508 ip->i_mode &= ~ISUID; 509 if (ogid != gid && cred->cr_uid != 0) 510 ip->i_mode &= ~ISGID; 511 return (0); 512 } 513 514 /* ARGSUSED */ 515 int 516 ufs_ioctl(ap) 517 struct vop_ioctl_args /* { 518 struct vnode *a_vp; 519 int a_command; 520 caddr_t a_data; 521 int a_fflag; 522 struct ucred *a_cred; 523 struct proc *a_p; 524 } */ *ap; 525 { 526 527 return (ENOTTY); 528 } 529 530 /* ARGSUSED */ 531 int 532 ufs_select(ap) 533 struct vop_select_args /* { 534 struct vnode *a_vp; 535 int a_which; 536 int a_fflags; 537 struct ucred *a_cred; 538 struct proc *a_p; 539 } */ *ap; 540 { 541 542 /* 543 * We should really check to see if I/O is possible. 544 */ 545 return (1); 546 } 547 548 /* 549 * Mmap a file 550 * 551 * NB Currently unsupported. 552 */ 553 /* ARGSUSED */ 554 int 555 ufs_mmap(ap) 556 struct vop_mmap_args /* { 557 struct vnode *a_vp; 558 int a_fflags; 559 struct ucred *a_cred; 560 struct proc *a_p; 561 } */ *ap; 562 { 563 564 return (EINVAL); 565 } 566 567 /* 568 * Seek on a file 569 * 570 * Nothing to do, so just return. 571 */ 572 /* ARGSUSED */ 573 int 574 ufs_seek(ap) 575 struct vop_seek_args /* { 576 struct vnode *a_vp; 577 off_t a_oldoff; 578 off_t a_newoff; 579 struct ucred *a_cred; 580 } */ *ap; 581 { 582 583 return (0); 584 } 585 586 int 587 ufs_remove(ap) 588 struct vop_remove_args /* { 589 struct vnode *a_dvp; 590 struct vnode *a_vp; 591 struct componentname *a_cnp; 592 } */ *ap; 593 { 594 register struct inode *ip; 595 register struct vnode *vp = ap->a_vp; 596 register struct vnode *dvp = ap->a_dvp; 597 int error; 598 599 ip = VTOI(vp); 600 if ((ip->i_flags & (IMMUTABLE | APPEND)) || 601 (VTOI(dvp)->i_flags & APPEND)) { 602 error = EPERM; 603 goto out; 604 } 605 if ((error = ufs_dirremove(dvp, ap->a_cnp)) == 0) { 606 ip->i_nlink--; 607 ip->i_flag |= IN_CHANGE; 608 } 609 out: 610 if (dvp == vp) 611 vrele(vp); 612 else 613 vput(vp); 614 vput(dvp); 615 return (error); 616 } 617 618 /* 619 * link vnode call 620 */ 621 int 622 ufs_link(ap) 623 struct vop_link_args /* { 624 struct vnode *a_vp; 625 struct vnode *a_tdvp; 626 struct componentname *a_cnp; 627 } */ *ap; 628 { 629 register struct vnode *vp = ap->a_vp; 630 register struct vnode *tdvp = ap->a_tdvp; 631 register struct componentname *cnp = ap->a_cnp; 632 register struct inode *ip; 633 struct timeval tv; 634 int error; 635 636 #ifdef DIAGNOSTIC 637 if ((cnp->cn_flags & HASBUF) == 0) 638 panic("ufs_link: no name"); 639 #endif 640 if (vp->v_mount != tdvp->v_mount) { 641 VOP_ABORTOP(vp, cnp); 642 error = EXDEV; 643 goto out2; 644 } 645 if (vp != tdvp && (error = VOP_LOCK(tdvp))) { 646 VOP_ABORTOP(vp, cnp); 647 goto out2; 648 } 649 ip = VTOI(tdvp); 650 if ((nlink_t)ip->i_nlink >= LINK_MAX) { 651 VOP_ABORTOP(vp, cnp); 652 error = EMLINK; 653 goto out1; 654 } 655 if (ip->i_flags & (IMMUTABLE | APPEND)) { 656 VOP_ABORTOP(vp, cnp); 657 error = EPERM; 658 goto out1; 659 } 660 ip->i_nlink++; 661 ip->i_flag |= IN_CHANGE; 662 tv = time; 663 error = VOP_UPDATE(tdvp, &tv, &tv, 1); 664 if (!error) 665 error = ufs_direnter(ip, vp, cnp); 666 if (error) { 667 ip->i_nlink--; 668 ip->i_flag |= IN_CHANGE; 669 } 670 FREE(cnp->cn_pnbuf, M_NAMEI); 671 out1: 672 if (vp != tdvp) 673 VOP_UNLOCK(tdvp); 674 out2: 675 vput(vp); 676 return (error); 677 } 678 679 680 681 /* 682 * relookup - lookup a path name component 683 * Used by lookup to re-aquire things. 684 */ 685 int 686 relookup(dvp, vpp, cnp) 687 struct vnode *dvp, **vpp; 688 struct componentname *cnp; 689 { 690 register struct vnode *dp = 0; /* the directory we are searching */ 691 int docache; /* == 0 do not cache last component */ 692 int wantparent; /* 1 => wantparent or lockparent flag */ 693 int rdonly; /* lookup read-only flag bit */ 694 char *cp; /* DEBUG: check name ptr/len */ 695 int newhash; /* DEBUG: check name hash */ 696 int error = 0; 697 698 /* 699 * Setup: break out flag bits into variables. 700 */ 701 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 702 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 703 if (cnp->cn_nameiop == DELETE || 704 (wantparent && cnp->cn_nameiop != CREATE)) 705 docache = 0; 706 rdonly = cnp->cn_flags & RDONLY; 707 cnp->cn_flags &= ~ISSYMLINK; 708 dp = dvp; 709 VOP_LOCK(dp); 710 711 /* dirloop: */ 712 /* 713 * Search a new directory. 714 * 715 * The cn_hash value is for use by vfs_cache. 716 * The last component of the filename is left accessible via 717 * cnp->cn_nameptr for callers that need the name. Callers needing 718 * the name set the SAVENAME flag. When done, they assume 719 * responsibility for freeing the pathname buffer. 720 */ 721 #ifdef NAMEI_DIAGNOSTIC 722 for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 723 newhash += (unsigned char)*cp; 724 if (newhash != cnp->cn_hash) 725 panic("relookup: bad hash"); 726 if (cnp->cn_namelen != cp - cnp->cn_nameptr) 727 panic ("relookup: bad len"); 728 if (*cp != 0) 729 panic("relookup: not last component"); 730 printf("{%s}: ", cnp->cn_nameptr); 731 #endif 732 733 /* 734 * Check for degenerate name (e.g. / or "") 735 * which is a way of talking about a directory, 736 * e.g. like "/." or ".". 737 */ 738 if (cnp->cn_nameptr[0] == '\0') { 739 if (cnp->cn_nameiop != LOOKUP || wantparent) { 740 error = EISDIR; 741 goto bad; 742 } 743 if (dp->v_type != VDIR) { 744 error = ENOTDIR; 745 goto bad; 746 } 747 if (!(cnp->cn_flags & LOCKLEAF)) 748 VOP_UNLOCK(dp); 749 *vpp = dp; 750 if (cnp->cn_flags & SAVESTART) 751 panic("lookup: SAVESTART"); 752 return (0); 753 } 754 755 if (cnp->cn_flags & ISDOTDOT) 756 panic ("relookup: lookup on dot-dot"); 757 758 /* 759 * We now have a segment name to search for, and a directory to search. 760 */ 761 if (error = VOP_LOOKUP(dp, vpp, cnp)) { 762 #ifdef DIAGNOSTIC 763 if (*vpp != NULL) 764 panic("leaf should be empty"); 765 #endif 766 if (error != EJUSTRETURN) 767 goto bad; 768 /* 769 * If creating and at end of pathname, then can consider 770 * allowing file to be created. 771 */ 772 if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) { 773 error = EROFS; 774 goto bad; 775 } 776 /* ASSERT(dvp == ndp->ni_startdir) */ 777 if (cnp->cn_flags & SAVESTART) 778 VREF(dvp); 779 /* 780 * We return with ni_vp NULL to indicate that the entry 781 * doesn't currently exist, leaving a pointer to the 782 * (possibly locked) directory inode in ndp->ni_dvp. 783 */ 784 return (0); 785 } 786 dp = *vpp; 787 788 #ifdef DIAGNOSTIC 789 /* 790 * Check for symbolic link 791 */ 792 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) 793 panic ("relookup: symlink found.\n"); 794 #endif 795 796 /* 797 * Check for read-only file systems. 798 */ 799 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) { 800 /* 801 * Disallow directory write attempts on read-only 802 * file systems. 803 */ 804 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) || 805 (wantparent && 806 (dvp->v_mount->mnt_flag & MNT_RDONLY))) { 807 error = EROFS; 808 goto bad2; 809 } 810 } 811 /* ASSERT(dvp == ndp->ni_startdir) */ 812 if (cnp->cn_flags & SAVESTART) 813 VREF(dvp); 814 815 if (!wantparent) 816 vrele(dvp); 817 if ((cnp->cn_flags & LOCKLEAF) == 0) 818 VOP_UNLOCK(dp); 819 return (0); 820 821 bad2: 822 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 823 VOP_UNLOCK(dvp); 824 vrele(dvp); 825 bad: 826 vput(dp); 827 *vpp = NULL; 828 return (error); 829 } 830 831 832 /* 833 * Rename system call. 834 * rename("foo", "bar"); 835 * is essentially 836 * unlink("bar"); 837 * link("foo", "bar"); 838 * unlink("foo"); 839 * but ``atomically''. Can't do full commit without saving state in the 840 * inode on disk which isn't feasible at this time. Best we can do is 841 * always guarantee the target exists. 842 * 843 * Basic algorithm is: 844 * 845 * 1) Bump link count on source while we're linking it to the 846 * target. This also ensure the inode won't be deleted out 847 * from underneath us while we work (it may be truncated by 848 * a concurrent `trunc' or `open' for creation). 849 * 2) Link source to destination. If destination already exists, 850 * delete it first. 851 * 3) Unlink source reference to inode if still around. If a 852 * directory was moved and the parent of the destination 853 * is different from the source, patch the ".." entry in the 854 * directory. 855 */ 856 int 857 ufs_rename(ap) 858 struct vop_rename_args /* { 859 struct vnode *a_fdvp; 860 struct vnode *a_fvp; 861 struct componentname *a_fcnp; 862 struct vnode *a_tdvp; 863 struct vnode *a_tvp; 864 struct componentname *a_tcnp; 865 } */ *ap; 866 { 867 struct vnode *tvp = ap->a_tvp; 868 register struct vnode *tdvp = ap->a_tdvp; 869 struct vnode *fvp = ap->a_fvp; 870 register struct vnode *fdvp = ap->a_fdvp; 871 register struct componentname *tcnp = ap->a_tcnp; 872 register struct componentname *fcnp = ap->a_fcnp; 873 register struct inode *ip, *xp, *dp; 874 struct dirtemplate dirbuf; 875 struct timeval tv; 876 int doingdirectory = 0, oldparent = 0, newparent = 0; 877 int error = 0; 878 u_char namlen; 879 880 #ifdef DIAGNOSTIC 881 if ((tcnp->cn_flags & HASBUF) == 0 || 882 (fcnp->cn_flags & HASBUF) == 0) 883 panic("ufs_rename: no name"); 884 #endif 885 /* 886 * Check for cross-device rename. 887 */ 888 if ((fvp->v_mount != tdvp->v_mount) || 889 (tvp && (fvp->v_mount != tvp->v_mount))) { 890 error = EXDEV; 891 abortit: 892 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */ 893 if (tdvp == tvp) 894 vrele(tdvp); 895 else 896 vput(tdvp); 897 if (tvp) 898 vput(tvp); 899 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */ 900 vrele(fdvp); 901 vrele(fvp); 902 return (error); 903 } 904 905 /* 906 * Check if just deleting a link name. 907 */ 908 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) || 909 (VTOI(tdvp)->i_flags & APPEND))) { 910 error = EPERM; 911 goto abortit; 912 } 913 if (fvp == tvp) { 914 if (fvp->v_type == VDIR) { 915 error = EINVAL; 916 goto abortit; 917 } 918 VOP_ABORTOP(fdvp, fcnp); 919 vrele(fdvp); 920 vrele(fvp); 921 vput(tdvp); 922 vput(tvp); 923 tcnp->cn_flags &= ~MODMASK; 924 tcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 925 if ((tcnp->cn_flags & SAVESTART) == 0) 926 panic("ufs_rename: lost from startdir"); 927 tcnp->cn_nameiop = DELETE; 928 (void) relookup(tdvp, &tvp, tcnp); 929 return (VOP_REMOVE(tdvp, tvp, tcnp)); 930 } 931 if (error = VOP_LOCK(fvp)) 932 goto abortit; 933 dp = VTOI(fdvp); 934 ip = VTOI(fvp); 935 if ((ip->i_flags & (IMMUTABLE | APPEND)) || (dp->i_flags & APPEND)) { 936 VOP_UNLOCK(fvp); 937 error = EPERM; 938 goto abortit; 939 } 940 if ((ip->i_mode & IFMT) == IFDIR) { 941 /* 942 * Avoid ".", "..", and aliases of "." for obvious reasons. 943 */ 944 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 945 dp == ip || (fcnp->cn_flags&ISDOTDOT) || 946 (ip->i_flag & IN_RENAME)) { 947 VOP_UNLOCK(fvp); 948 error = EINVAL; 949 goto abortit; 950 } 951 ip->i_flag |= IN_RENAME; 952 oldparent = dp->i_number; 953 doingdirectory++; 954 } 955 vrele(fdvp); 956 957 /* 958 * When the target exists, both the directory 959 * and target vnodes are returned locked. 960 */ 961 dp = VTOI(tdvp); 962 xp = NULL; 963 if (tvp) 964 xp = VTOI(tvp); 965 966 /* 967 * 1) Bump link count while we're moving stuff 968 * around. If we crash somewhere before 969 * completing our work, the link count 970 * may be wrong, but correctable. 971 */ 972 ip->i_nlink++; 973 ip->i_flag |= IN_CHANGE; 974 tv = time; 975 if (error = VOP_UPDATE(fvp, &tv, &tv, 1)) { 976 VOP_UNLOCK(fvp); 977 goto bad; 978 } 979 980 /* 981 * If ".." must be changed (ie the directory gets a new 982 * parent) then the source directory must not be in the 983 * directory heirarchy above the target, as this would 984 * orphan everything below the source directory. Also 985 * the user must have write permission in the source so 986 * as to be able to change "..". We must repeat the call 987 * to namei, as the parent directory is unlocked by the 988 * call to checkpath(). 989 */ 990 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); 991 VOP_UNLOCK(fvp); 992 if (oldparent != dp->i_number) 993 newparent = dp->i_number; 994 if (doingdirectory && newparent) { 995 if (error) /* write access check above */ 996 goto bad; 997 if (xp != NULL) 998 vput(tvp); 999 if (error = ufs_checkpath(ip, dp, tcnp->cn_cred)) 1000 goto out; 1001 if ((tcnp->cn_flags & SAVESTART) == 0) 1002 panic("ufs_rename: lost to startdir"); 1003 if (error = relookup(tdvp, &tvp, tcnp)) 1004 goto out; 1005 dp = VTOI(tdvp); 1006 xp = NULL; 1007 if (tvp) 1008 xp = VTOI(tvp); 1009 } 1010 /* 1011 * 2) If target doesn't exist, link the target 1012 * to the source and unlink the source. 1013 * Otherwise, rewrite the target directory 1014 * entry to reference the source inode and 1015 * expunge the original entry's existence. 1016 */ 1017 if (xp == NULL) { 1018 if (dp->i_dev != ip->i_dev) 1019 panic("rename: EXDEV"); 1020 /* 1021 * Account for ".." in new directory. 1022 * When source and destination have the same 1023 * parent we don't fool with the link count. 1024 */ 1025 if (doingdirectory && newparent) { 1026 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1027 error = EMLINK; 1028 goto bad; 1029 } 1030 dp->i_nlink++; 1031 dp->i_flag |= IN_CHANGE; 1032 if (error = VOP_UPDATE(tdvp, &tv, &tv, 1)) 1033 goto bad; 1034 } 1035 if (error = ufs_direnter(ip, tdvp, tcnp)) { 1036 if (doingdirectory && newparent) { 1037 dp->i_nlink--; 1038 dp->i_flag |= IN_CHANGE; 1039 (void)VOP_UPDATE(tdvp, &tv, &tv, 1); 1040 } 1041 goto bad; 1042 } 1043 vput(tdvp); 1044 } else { 1045 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 1046 panic("rename: EXDEV"); 1047 /* 1048 * Short circuit rename(foo, foo). 1049 */ 1050 if (xp->i_number == ip->i_number) 1051 panic("rename: same file"); 1052 /* 1053 * If the parent directory is "sticky", then the user must 1054 * own the parent directory, or the destination of the rename, 1055 * otherwise the destination may not be changed (except by 1056 * root). This implements append-only directories. 1057 */ 1058 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && 1059 tcnp->cn_cred->cr_uid != dp->i_uid && 1060 xp->i_uid != tcnp->cn_cred->cr_uid) { 1061 error = EPERM; 1062 goto bad; 1063 } 1064 /* 1065 * Target must be empty if a directory and have no links 1066 * to it. Also, ensure source and target are compatible 1067 * (both directories, or both not directories). 1068 */ 1069 if ((xp->i_mode&IFMT) == IFDIR) { 1070 if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) || 1071 xp->i_nlink > 2) { 1072 error = ENOTEMPTY; 1073 goto bad; 1074 } 1075 if (!doingdirectory) { 1076 error = ENOTDIR; 1077 goto bad; 1078 } 1079 cache_purge(tdvp); 1080 } else if (doingdirectory) { 1081 error = EISDIR; 1082 goto bad; 1083 } 1084 if (error = ufs_dirrewrite(dp, ip, tcnp)) 1085 goto bad; 1086 /* 1087 * If the target directory is in the same 1088 * directory as the source directory, 1089 * decrement the link count on the parent 1090 * of the target directory. 1091 */ 1092 if (doingdirectory && !newparent) { 1093 dp->i_nlink--; 1094 dp->i_flag |= IN_CHANGE; 1095 } 1096 vput(tdvp); 1097 /* 1098 * Adjust the link count of the target to 1099 * reflect the dirrewrite above. If this is 1100 * a directory it is empty and there are 1101 * no links to it, so we can squash the inode and 1102 * any space associated with it. We disallowed 1103 * renaming over top of a directory with links to 1104 * it above, as the remaining link would point to 1105 * a directory without "." or ".." entries. 1106 */ 1107 xp->i_nlink--; 1108 if (doingdirectory) { 1109 if (--xp->i_nlink != 0) 1110 panic("rename: linked directory"); 1111 error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC, 1112 tcnp->cn_cred, tcnp->cn_proc); 1113 } 1114 xp->i_flag |= IN_CHANGE; 1115 vput(tvp); 1116 xp = NULL; 1117 } 1118 1119 /* 1120 * 3) Unlink the source. 1121 */ 1122 fcnp->cn_flags &= ~MODMASK; 1123 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 1124 if ((fcnp->cn_flags & SAVESTART) == 0) 1125 panic("ufs_rename: lost from startdir"); 1126 (void) relookup(fdvp, &fvp, fcnp); 1127 if (fvp != NULL) { 1128 xp = VTOI(fvp); 1129 dp = VTOI(fdvp); 1130 } else { 1131 /* 1132 * From name has disappeared. 1133 */ 1134 if (doingdirectory) 1135 panic("rename: lost dir entry"); 1136 vrele(ap->a_fvp); 1137 return (0); 1138 } 1139 /* 1140 * Ensure that the directory entry still exists and has not 1141 * changed while the new name has been entered. If the source is 1142 * a file then the entry may have been unlinked or renamed. In 1143 * either case there is no further work to be done. If the source 1144 * is a directory then it cannot have been rmdir'ed; its link 1145 * count of three would cause a rmdir to fail with ENOTEMPTY. 1146 * The IRENAME flag ensures that it cannot be moved by another 1147 * rename. 1148 */ 1149 if (xp != ip) { 1150 if (doingdirectory) 1151 panic("rename: lost dir entry"); 1152 } else { 1153 /* 1154 * If the source is a directory with a 1155 * new parent, the link count of the old 1156 * parent directory must be decremented 1157 * and ".." set to point to the new parent. 1158 */ 1159 if (doingdirectory && newparent) { 1160 dp->i_nlink--; 1161 dp->i_flag |= IN_CHANGE; 1162 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf, 1163 sizeof (struct dirtemplate), (off_t)0, 1164 UIO_SYSSPACE, IO_NODELOCKED, 1165 tcnp->cn_cred, (int *)0, (struct proc *)0); 1166 if (error == 0) { 1167 # if (BYTE_ORDER == LITTLE_ENDIAN) 1168 if (fvp->v_mount->mnt_maxsymlinklen <= 0) 1169 namlen = dirbuf.dotdot_type; 1170 else 1171 namlen = dirbuf.dotdot_namlen; 1172 # else 1173 namlen = dirbuf.dotdot_namlen; 1174 # endif 1175 if (namlen != 2 || 1176 dirbuf.dotdot_name[0] != '.' || 1177 dirbuf.dotdot_name[1] != '.') { 1178 ufs_dirbad(xp, (doff_t)12, 1179 "rename: mangled dir"); 1180 } else { 1181 dirbuf.dotdot_ino = newparent; 1182 (void) vn_rdwr(UIO_WRITE, fvp, 1183 (caddr_t)&dirbuf, 1184 sizeof (struct dirtemplate), 1185 (off_t)0, UIO_SYSSPACE, 1186 IO_NODELOCKED|IO_SYNC, 1187 tcnp->cn_cred, (int *)0, 1188 (struct proc *)0); 1189 cache_purge(fdvp); 1190 } 1191 } 1192 } 1193 error = ufs_dirremove(fdvp, fcnp); 1194 if (!error) { 1195 xp->i_nlink--; 1196 xp->i_flag |= IN_CHANGE; 1197 } 1198 xp->i_flag &= ~IN_RENAME; 1199 } 1200 if (dp) 1201 vput(fdvp); 1202 if (xp) 1203 vput(fvp); 1204 vrele(ap->a_fvp); 1205 return (error); 1206 1207 bad: 1208 if (xp) 1209 vput(ITOV(xp)); 1210 vput(ITOV(dp)); 1211 out: 1212 if (VOP_LOCK(fvp) == 0) { 1213 ip->i_nlink--; 1214 ip->i_flag |= IN_CHANGE; 1215 vput(fvp); 1216 } else 1217 vrele(fvp); 1218 return (error); 1219 } 1220 1221 /* 1222 * A virgin directory (no blushing please). 1223 */ 1224 static struct dirtemplate mastertemplate = { 1225 0, 12, DT_DIR, 1, ".", 1226 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 1227 }; 1228 static struct odirtemplate omastertemplate = { 1229 0, 12, 1, ".", 1230 0, DIRBLKSIZ - 12, 2, ".." 1231 }; 1232 1233 /* 1234 * Mkdir system call 1235 */ 1236 int 1237 ufs_mkdir(ap) 1238 struct vop_mkdir_args /* { 1239 struct vnode *a_dvp; 1240 struct vnode **a_vpp; 1241 struct componentname *a_cnp; 1242 struct vattr *a_vap; 1243 } */ *ap; 1244 { 1245 register struct vnode *dvp = ap->a_dvp; 1246 register struct vattr *vap = ap->a_vap; 1247 register struct componentname *cnp = ap->a_cnp; 1248 register struct inode *ip, *dp; 1249 struct vnode *tvp; 1250 struct dirtemplate dirtemplate, *dtp; 1251 struct timeval tv; 1252 int error, dmode; 1253 1254 #ifdef DIAGNOSTIC 1255 if ((cnp->cn_flags & HASBUF) == 0) 1256 panic("ufs_mkdir: no name"); 1257 #endif 1258 dp = VTOI(dvp); 1259 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1260 error = EMLINK; 1261 goto out; 1262 } 1263 dmode = vap->va_mode & 0777; 1264 dmode |= IFDIR; 1265 /* 1266 * Must simulate part of ufs_makeinode here to acquire the inode, 1267 * but not have it entered in the parent directory. The entry is 1268 * made later after writing "." and ".." entries. 1269 */ 1270 if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) 1271 goto out; 1272 ip = VTOI(tvp); 1273 ip->i_uid = cnp->cn_cred->cr_uid; 1274 ip->i_gid = dp->i_gid; 1275 #ifdef QUOTA 1276 if ((error = getinoquota(ip)) || 1277 (error = chkiq(ip, 1, cnp->cn_cred, 0))) { 1278 free(cnp->cn_pnbuf, M_NAMEI); 1279 VOP_VFREE(tvp, ip->i_number, dmode); 1280 vput(tvp); 1281 vput(dvp); 1282 return (error); 1283 } 1284 #endif 1285 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1286 ip->i_mode = dmode; 1287 tvp->v_type = VDIR; /* Rest init'd in iget() */ 1288 ip->i_nlink = 2; 1289 tv = time; 1290 error = VOP_UPDATE(tvp, &tv, &tv, 1); 1291 1292 /* 1293 * Bump link count in parent directory 1294 * to reflect work done below. Should 1295 * be done before reference is created 1296 * so reparation is possible if we crash. 1297 */ 1298 dp->i_nlink++; 1299 dp->i_flag |= IN_CHANGE; 1300 if (error = VOP_UPDATE(dvp, &tv, &tv, 1)) 1301 goto bad; 1302 1303 /* Initialize directory with "." and ".." from static template. */ 1304 if (dvp->v_mount->mnt_maxsymlinklen > 0) 1305 dtp = &mastertemplate; 1306 else 1307 dtp = (struct dirtemplate *)&omastertemplate; 1308 dirtemplate = *dtp; 1309 dirtemplate.dot_ino = ip->i_number; 1310 dirtemplate.dotdot_ino = dp->i_number; 1311 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate, 1312 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 1313 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0); 1314 if (error) { 1315 dp->i_nlink--; 1316 dp->i_flag |= IN_CHANGE; 1317 goto bad; 1318 } 1319 if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 1320 panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */ 1321 else { 1322 ip->i_size = DIRBLKSIZ; 1323 ip->i_flag |= IN_CHANGE; 1324 } 1325 1326 /* Directory set up, now install it's entry in the parent directory. */ 1327 if (error = ufs_direnter(ip, dvp, cnp)) { 1328 dp->i_nlink--; 1329 dp->i_flag |= IN_CHANGE; 1330 } 1331 bad: 1332 /* 1333 * No need to do an explicit VOP_TRUNCATE here, vrele will do this 1334 * for us because we set the link count to 0. 1335 */ 1336 if (error) { 1337 ip->i_nlink = 0; 1338 ip->i_flag |= IN_CHANGE; 1339 vput(tvp); 1340 } else 1341 *ap->a_vpp = tvp; 1342 out: 1343 FREE(cnp->cn_pnbuf, M_NAMEI); 1344 vput(dvp); 1345 return (error); 1346 } 1347 1348 /* 1349 * Rmdir system call. 1350 */ 1351 int 1352 ufs_rmdir(ap) 1353 struct vop_rmdir_args /* { 1354 struct vnode *a_dvp; 1355 struct vnode *a_vp; 1356 struct componentname *a_cnp; 1357 } */ *ap; 1358 { 1359 register struct vnode *vp = ap->a_vp; 1360 register struct vnode *dvp = ap->a_dvp; 1361 register struct componentname *cnp = ap->a_cnp; 1362 register struct inode *ip, *dp; 1363 int error; 1364 1365 ip = VTOI(vp); 1366 dp = VTOI(dvp); 1367 /* 1368 * No rmdir "." please. 1369 */ 1370 if (dp == ip) { 1371 vrele(dvp); 1372 vput(vp); 1373 return (EINVAL); 1374 } 1375 /* 1376 * Verify the directory is empty (and valid). 1377 * (Rmdir ".." won't be valid since 1378 * ".." will contain a reference to 1379 * the current directory and thus be 1380 * non-empty.) 1381 */ 1382 error = 0; 1383 if (ip->i_nlink != 2 || 1384 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1385 error = ENOTEMPTY; 1386 goto out; 1387 } 1388 if ((dp->i_flags & APPEND) || (ip->i_flags & (IMMUTABLE | APPEND))) { 1389 error = EPERM; 1390 goto out; 1391 } 1392 /* 1393 * Delete reference to directory before purging 1394 * inode. If we crash in between, the directory 1395 * will be reattached to lost+found, 1396 */ 1397 if (error = ufs_dirremove(dvp, cnp)) 1398 goto out; 1399 dp->i_nlink--; 1400 dp->i_flag |= IN_CHANGE; 1401 cache_purge(dvp); 1402 vput(dvp); 1403 dvp = NULL; 1404 /* 1405 * Truncate inode. The only stuff left 1406 * in the directory is "." and "..". The 1407 * "." reference is inconsequential since 1408 * we're quashing it. The ".." reference 1409 * has already been adjusted above. We've 1410 * removed the "." reference and the reference 1411 * in the parent directory, but there may be 1412 * other hard links so decrement by 2 and 1413 * worry about them later. 1414 */ 1415 ip->i_nlink -= 2; 1416 error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred, 1417 cnp->cn_proc); 1418 cache_purge(ITOV(ip)); 1419 out: 1420 if (dvp) 1421 vput(dvp); 1422 vput(vp); 1423 return (error); 1424 } 1425 1426 /* 1427 * symlink -- make a symbolic link 1428 */ 1429 int 1430 ufs_symlink(ap) 1431 struct vop_symlink_args /* { 1432 struct vnode *a_dvp; 1433 struct vnode **a_vpp; 1434 struct componentname *a_cnp; 1435 struct vattr *a_vap; 1436 char *a_target; 1437 } */ *ap; 1438 { 1439 register struct vnode *vp, **vpp = ap->a_vpp; 1440 register struct inode *ip; 1441 int len, error; 1442 1443 if (error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, 1444 vpp, ap->a_cnp)) 1445 return (error); 1446 vp = *vpp; 1447 len = strlen(ap->a_target); 1448 if (len < vp->v_mount->mnt_maxsymlinklen) { 1449 ip = VTOI(vp); 1450 bcopy(ap->a_target, (char *)ip->i_shortlink, len); 1451 ip->i_size = len; 1452 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1453 } else 1454 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1455 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0, 1456 (struct proc *)0); 1457 vput(vp); 1458 return (error); 1459 } 1460 1461 /* 1462 * Vnode op for reading directories. 1463 * 1464 * The routine below assumes that the on-disk format of a directory 1465 * is the same as that defined by <sys/dirent.h>. If the on-disk 1466 * format changes, then it will be necessary to do a conversion 1467 * from the on-disk format that read returns to the format defined 1468 * by <sys/dirent.h>. 1469 */ 1470 int 1471 ufs_readdir(ap) 1472 struct vop_readdir_args /* { 1473 struct vnode *a_vp; 1474 struct uio *a_uio; 1475 struct ucred *a_cred; 1476 } */ *ap; 1477 { 1478 register struct uio *uio = ap->a_uio; 1479 int count, lost, error; 1480 1481 count = uio->uio_resid; 1482 count &= ~(DIRBLKSIZ - 1); 1483 lost = uio->uio_resid - count; 1484 if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 1485 return (EINVAL); 1486 uio->uio_resid = count; 1487 uio->uio_iov->iov_len = count; 1488 # if (BYTE_ORDER == LITTLE_ENDIAN) 1489 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) { 1490 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); 1491 } else { 1492 struct dirent *dp, *edp; 1493 struct uio auio; 1494 struct iovec aiov; 1495 caddr_t dirbuf; 1496 int readcnt; 1497 u_char tmp; 1498 1499 auio = *uio; 1500 auio.uio_iov = &aiov; 1501 auio.uio_iovcnt = 1; 1502 auio.uio_segflg = UIO_SYSSPACE; 1503 aiov.iov_len = count; 1504 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK); 1505 aiov.iov_base = dirbuf; 1506 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred); 1507 if (error == 0) { 1508 readcnt = count - auio.uio_resid; 1509 edp = (struct dirent *)&dirbuf[readcnt]; 1510 for (dp = (struct dirent *)dirbuf; dp < edp; ) { 1511 tmp = dp->d_namlen; 1512 dp->d_namlen = dp->d_type; 1513 dp->d_type = tmp; 1514 if (dp->d_reclen > 0) { 1515 dp = (struct dirent *) 1516 ((char *)dp + dp->d_reclen); 1517 } else { 1518 error = EIO; 1519 break; 1520 } 1521 } 1522 if (dp >= edp) 1523 error = uiomove(dirbuf, readcnt, uio); 1524 } 1525 FREE(dirbuf, M_TEMP); 1526 } 1527 # else 1528 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); 1529 # endif 1530 uio->uio_resid += lost; 1531 return (error); 1532 } 1533 1534 /* 1535 * Return target name of a symbolic link 1536 */ 1537 int 1538 ufs_readlink(ap) 1539 struct vop_readlink_args /* { 1540 struct vnode *a_vp; 1541 struct uio *a_uio; 1542 struct ucred *a_cred; 1543 } */ *ap; 1544 { 1545 register struct vnode *vp = ap->a_vp; 1546 register struct inode *ip = VTOI(vp); 1547 int isize; 1548 1549 isize = ip->i_size; 1550 if (isize < vp->v_mount->mnt_maxsymlinklen) { 1551 uiomove((char *)ip->i_shortlink, isize, ap->a_uio); 1552 return (0); 1553 } 1554 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1555 } 1556 1557 /* 1558 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 1559 * done. If a buffer has been saved in anticipation of a CREATE, delete it. 1560 */ 1561 /* ARGSUSED */ 1562 int 1563 ufs_abortop(ap) 1564 struct vop_abortop_args /* { 1565 struct vnode *a_dvp; 1566 struct componentname *a_cnp; 1567 } */ *ap; 1568 { 1569 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) 1570 FREE(ap->a_cnp->cn_pnbuf, M_NAMEI); 1571 return (0); 1572 } 1573 1574 /* 1575 * Lock an inode. If its already locked, set the WANT bit and sleep. 1576 */ 1577 int 1578 ufs_lock(ap) 1579 struct vop_lock_args /* { 1580 struct vnode *a_vp; 1581 } */ *ap; 1582 { 1583 register struct vnode *vp = ap->a_vp; 1584 register struct inode *ip; 1585 struct proc *p = curproc; /* XXX */ 1586 1587 start: 1588 while (vp->v_flag & VXLOCK) { 1589 vp->v_flag |= VXWANT; 1590 sleep((caddr_t)vp, PINOD); 1591 } 1592 if (vp->v_tag == VT_NON) 1593 return (ENOENT); 1594 ip = VTOI(vp); 1595 if (ip->i_flag & IN_LOCKED) { 1596 ip->i_flag |= IN_WANTED; 1597 #ifdef DIAGNOSTIC 1598 if (p) { 1599 if (p->p_pid == ip->i_lockholder) 1600 panic("locking against myself"); 1601 ip->i_lockwaiter = p->p_pid; 1602 } else 1603 ip->i_lockwaiter = -1; 1604 #endif 1605 (void) sleep((caddr_t)ip, PINOD); 1606 goto start; 1607 } 1608 #ifdef DIAGNOSTIC 1609 ip->i_lockwaiter = 0; 1610 if (ip->i_lockholder != 0) 1611 panic("lockholder (%d) != 0", ip->i_lockholder); 1612 if (p && p->p_pid == 0) 1613 printf("locking by process 0\n"); 1614 if (p) 1615 ip->i_lockholder = p->p_pid; 1616 else 1617 ip->i_lockholder = -1; 1618 #endif 1619 ip->i_flag |= IN_LOCKED; 1620 return (0); 1621 } 1622 1623 /* 1624 * Unlock an inode. If WANT bit is on, wakeup. 1625 */ 1626 int lockcount = 90; 1627 int 1628 ufs_unlock(ap) 1629 struct vop_unlock_args /* { 1630 struct vnode *a_vp; 1631 } */ *ap; 1632 { 1633 register struct inode *ip = VTOI(ap->a_vp); 1634 struct proc *p = curproc; /* XXX */ 1635 1636 #ifdef DIAGNOSTIC 1637 if ((ip->i_flag & IN_LOCKED) == 0) { 1638 vprint("ufs_unlock: unlocked inode", ap->a_vp); 1639 panic("ufs_unlock NOT LOCKED"); 1640 } 1641 if (p && p->p_pid != ip->i_lockholder && p->p_pid > -1 && 1642 ip->i_lockholder > -1 && lockcount++ < 100) 1643 panic("unlocker (%d) != lock holder (%d)", 1644 p->p_pid, ip->i_lockholder); 1645 ip->i_lockholder = 0; 1646 #endif 1647 ip->i_flag &= ~IN_LOCKED; 1648 if (ip->i_flag & IN_WANTED) { 1649 ip->i_flag &= ~IN_WANTED; 1650 wakeup((caddr_t)ip); 1651 } 1652 return (0); 1653 } 1654 1655 /* 1656 * Check for a locked inode. 1657 */ 1658 int 1659 ufs_islocked(ap) 1660 struct vop_islocked_args /* { 1661 struct vnode *a_vp; 1662 } */ *ap; 1663 { 1664 1665 if (VTOI(ap->a_vp)->i_flag & IN_LOCKED) 1666 return (1); 1667 return (0); 1668 } 1669 1670 /* 1671 * Calculate the logical to physical mapping if not done already, 1672 * then call the device strategy routine. 1673 */ 1674 int 1675 ufs_strategy(ap) 1676 struct vop_strategy_args /* { 1677 struct buf *a_bp; 1678 } */ *ap; 1679 { 1680 register struct buf *bp = ap->a_bp; 1681 register struct vnode *vp = bp->b_vp; 1682 register struct inode *ip; 1683 int error; 1684 1685 ip = VTOI(vp); 1686 if (vp->v_type == VBLK || vp->v_type == VCHR) 1687 panic("ufs_strategy: spec"); 1688 if (bp->b_blkno == bp->b_lblkno) { 1689 if (error = 1690 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL)) { 1691 bp->b_error = error; 1692 bp->b_flags |= B_ERROR; 1693 biodone(bp); 1694 return (error); 1695 } 1696 if ((long)bp->b_blkno == -1) 1697 clrbuf(bp); 1698 } 1699 if ((long)bp->b_blkno == -1) { 1700 biodone(bp); 1701 return (0); 1702 } 1703 vp = ip->i_devvp; 1704 bp->b_dev = vp->v_rdev; 1705 VOCALL (vp->v_op, VOFFSET(vop_strategy), ap); 1706 return (0); 1707 } 1708 1709 /* 1710 * Print out the contents of an inode. 1711 */ 1712 int 1713 ufs_print(ap) 1714 struct vop_print_args /* { 1715 struct vnode *a_vp; 1716 } */ *ap; 1717 { 1718 register struct vnode *vp = ap->a_vp; 1719 register struct inode *ip = VTOI(vp); 1720 1721 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 1722 major(ip->i_dev), minor(ip->i_dev)); 1723 #ifdef FIFO 1724 if (vp->v_type == VFIFO) 1725 fifo_printinfo(vp); 1726 #endif /* FIFO */ 1727 printf("%s\n", (ip->i_flag & IN_LOCKED) ? " (LOCKED)" : ""); 1728 if (ip->i_lockholder == 0) 1729 return (0); 1730 printf("\towner pid %d", ip->i_lockholder); 1731 if (ip->i_lockwaiter) 1732 printf(" waiting pid %d", ip->i_lockwaiter); 1733 printf("\n"); 1734 return (0); 1735 } 1736 1737 /* 1738 * Read wrapper for special devices. 1739 */ 1740 int 1741 ufsspec_read(ap) 1742 struct vop_read_args /* { 1743 struct vnode *a_vp; 1744 struct uio *a_uio; 1745 int a_ioflag; 1746 struct ucred *a_cred; 1747 } */ *ap; 1748 { 1749 1750 /* 1751 * Set access flag. 1752 */ 1753 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1754 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap)); 1755 } 1756 1757 /* 1758 * Write wrapper for special devices. 1759 */ 1760 int 1761 ufsspec_write(ap) 1762 struct vop_write_args /* { 1763 struct vnode *a_vp; 1764 struct uio *a_uio; 1765 int a_ioflag; 1766 struct ucred *a_cred; 1767 } */ *ap; 1768 { 1769 1770 /* 1771 * Set update and change flags. 1772 */ 1773 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; 1774 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap)); 1775 } 1776 1777 /* 1778 * Close wrapper for special devices. 1779 * 1780 * Update the times on the inode then do device close. 1781 */ 1782 int 1783 ufsspec_close(ap) 1784 struct vop_close_args /* { 1785 struct vnode *a_vp; 1786 int a_fflag; 1787 struct ucred *a_cred; 1788 struct proc *a_p; 1789 } */ *ap; 1790 { 1791 register struct inode *ip = VTOI(ap->a_vp); 1792 1793 if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED)) 1794 ITIMES(ip, &time, &time); 1795 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap)); 1796 } 1797 1798 #ifdef FIFO 1799 /* 1800 * Read wrapper for fifo's 1801 */ 1802 int 1803 ufsfifo_read(ap) 1804 struct vop_read_args /* { 1805 struct vnode *a_vp; 1806 struct uio *a_uio; 1807 int a_ioflag; 1808 struct ucred *a_cred; 1809 } */ *ap; 1810 { 1811 extern int (**fifo_vnodeop_p)(); 1812 1813 /* 1814 * Set access flag. 1815 */ 1816 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1817 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap)); 1818 } 1819 1820 /* 1821 * Write wrapper for fifo's. 1822 */ 1823 int 1824 ufsfifo_write(ap) 1825 struct vop_write_args /* { 1826 struct vnode *a_vp; 1827 struct uio *a_uio; 1828 int a_ioflag; 1829 struct ucred *a_cred; 1830 } */ *ap; 1831 { 1832 extern int (**fifo_vnodeop_p)(); 1833 1834 /* 1835 * Set update and change flags. 1836 */ 1837 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; 1838 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap)); 1839 } 1840 1841 /* 1842 * Close wrapper for fifo's. 1843 * 1844 * Update the times on the inode then do device close. 1845 */ 1846 ufsfifo_close(ap) 1847 struct vop_close_args /* { 1848 struct vnode *a_vp; 1849 int a_fflag; 1850 struct ucred *a_cred; 1851 struct proc *a_p; 1852 } */ *ap; 1853 { 1854 extern int (**fifo_vnodeop_p)(); 1855 register struct inode *ip = VTOI(ap->a_vp); 1856 1857 if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED)) 1858 ITIMES(ip, &time, &time); 1859 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap)); 1860 } 1861 #endif /* FIFO */ 1862 1863 /* 1864 * Return POSIX pathconf information applicable to ufs filesystems. 1865 */ 1866 ufs_pathconf(ap) 1867 struct vop_pathconf_args /* { 1868 struct vnode *a_vp; 1869 int a_name; 1870 int *a_retval; 1871 } */ *ap; 1872 { 1873 1874 switch (ap->a_name) { 1875 case _PC_LINK_MAX: 1876 *ap->a_retval = LINK_MAX; 1877 return (0); 1878 case _PC_NAME_MAX: 1879 *ap->a_retval = NAME_MAX; 1880 return (0); 1881 case _PC_PATH_MAX: 1882 *ap->a_retval = PATH_MAX; 1883 return (0); 1884 case _PC_PIPE_BUF: 1885 *ap->a_retval = PIPE_BUF; 1886 return (0); 1887 case _PC_CHOWN_RESTRICTED: 1888 *ap->a_retval = 1; 1889 return (0); 1890 case _PC_NO_TRUNC: 1891 *ap->a_retval = 1; 1892 return (0); 1893 default: 1894 return (EINVAL); 1895 } 1896 /* NOTREACHED */ 1897 } 1898 1899 /* 1900 * Advisory record locking support 1901 */ 1902 int 1903 ufs_advlock(ap) 1904 struct vop_advlock_args /* { 1905 struct vnode *a_vp; 1906 caddr_t a_id; 1907 int a_op; 1908 struct flock *a_fl; 1909 int a_flags; 1910 } */ *ap; 1911 { 1912 register struct inode *ip = VTOI(ap->a_vp); 1913 register struct flock *fl = ap->a_fl; 1914 register struct lockf *lock; 1915 off_t start, end; 1916 int error; 1917 1918 /* 1919 * Avoid the common case of unlocking when inode has no locks. 1920 */ 1921 if (ip->i_lockf == (struct lockf *)0) { 1922 if (ap->a_op != F_SETLK) { 1923 fl->l_type = F_UNLCK; 1924 return (0); 1925 } 1926 } 1927 /* 1928 * Convert the flock structure into a start and end. 1929 */ 1930 switch (fl->l_whence) { 1931 1932 case SEEK_SET: 1933 case SEEK_CUR: 1934 /* 1935 * Caller is responsible for adding any necessary offset 1936 * when SEEK_CUR is used. 1937 */ 1938 start = fl->l_start; 1939 break; 1940 1941 case SEEK_END: 1942 start = ip->i_size + fl->l_start; 1943 break; 1944 1945 default: 1946 return (EINVAL); 1947 } 1948 if (start < 0) 1949 return (EINVAL); 1950 if (fl->l_len == 0) 1951 end = -1; 1952 else 1953 end = start + fl->l_len - 1; 1954 /* 1955 * Create the lockf structure 1956 */ 1957 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); 1958 lock->lf_start = start; 1959 lock->lf_end = end; 1960 lock->lf_id = ap->a_id; 1961 lock->lf_inode = ip; 1962 lock->lf_type = fl->l_type; 1963 lock->lf_next = (struct lockf *)0; 1964 lock->lf_block = (struct lockf *)0; 1965 lock->lf_flags = ap->a_flags; 1966 /* 1967 * Do the requested operation. 1968 */ 1969 switch(ap->a_op) { 1970 case F_SETLK: 1971 return (lf_setlock(lock)); 1972 1973 case F_UNLCK: 1974 error = lf_clearlock(lock); 1975 FREE(lock, M_LOCKF); 1976 return (error); 1977 1978 case F_GETLK: 1979 error = lf_getlock(lock, fl); 1980 FREE(lock, M_LOCKF); 1981 return (error); 1982 1983 default: 1984 free(lock, M_LOCKF); 1985 return (EINVAL); 1986 } 1987 /* NOTREACHED */ 1988 } 1989 1990 /* 1991 * Initialize the vnode associated with a new inode, handle aliased 1992 * vnodes. 1993 */ 1994 int 1995 ufs_vinit(mntp, specops, fifoops, vpp) 1996 struct mount *mntp; 1997 int (**specops)(); 1998 int (**fifoops)(); 1999 struct vnode **vpp; 2000 { 2001 struct inode *ip; 2002 struct vnode *vp, *nvp; 2003 2004 vp = *vpp; 2005 ip = VTOI(vp); 2006 switch(vp->v_type = IFTOVT(ip->i_mode)) { 2007 case VCHR: 2008 case VBLK: 2009 vp->v_op = specops; 2010 if (nvp = checkalias(vp, ip->i_rdev, mntp)) { 2011 /* 2012 * Discard unneeded vnode, but save its inode. 2013 */ 2014 ufs_ihashrem(ip); 2015 VOP_UNLOCK(vp); 2016 nvp->v_data = vp->v_data; 2017 vp->v_data = NULL; 2018 vp->v_op = spec_vnodeop_p; 2019 vrele(vp); 2020 vgone(vp); 2021 /* 2022 * Reinitialize aliased inode. 2023 */ 2024 vp = nvp; 2025 ip->i_vnode = vp; 2026 ufs_ihashins(ip); 2027 } 2028 break; 2029 case VFIFO: 2030 #ifdef FIFO 2031 vp->v_op = fifoops; 2032 break; 2033 #else 2034 return (EOPNOTSUPP); 2035 #endif 2036 } 2037 if (ip->i_number == ROOTINO) 2038 vp->v_flag |= VROOT; 2039 /* 2040 * Initialize modrev times 2041 */ 2042 SETHIGH(ip->i_modrev, mono_time.tv_sec); 2043 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294); 2044 *vpp = vp; 2045 return (0); 2046 } 2047 2048 /* 2049 * Allocate a new inode. 2050 */ 2051 int 2052 ufs_makeinode(mode, dvp, vpp, cnp) 2053 int mode; 2054 struct vnode *dvp; 2055 struct vnode **vpp; 2056 struct componentname *cnp; 2057 { 2058 register struct inode *ip, *pdir; 2059 struct timeval tv; 2060 struct vnode *tvp; 2061 int error; 2062 2063 pdir = VTOI(dvp); 2064 #ifdef DIAGNOSTIC 2065 if ((cnp->cn_flags & HASBUF) == 0) 2066 panic("ufs_makeinode: no name"); 2067 #endif 2068 *vpp = NULL; 2069 if ((mode & IFMT) == 0) 2070 mode |= IFREG; 2071 2072 if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) { 2073 free(cnp->cn_pnbuf, M_NAMEI); 2074 vput(dvp); 2075 return (error); 2076 } 2077 ip = VTOI(tvp); 2078 ip->i_gid = pdir->i_gid; 2079 if ((mode & IFMT) == IFLNK) 2080 ip->i_uid = pdir->i_uid; 2081 else 2082 ip->i_uid = cnp->cn_cred->cr_uid; 2083 #ifdef QUOTA 2084 if ((error = getinoquota(ip)) || 2085 (error = chkiq(ip, 1, cnp->cn_cred, 0))) { 2086 free(cnp->cn_pnbuf, M_NAMEI); 2087 VOP_VFREE(tvp, ip->i_number, mode); 2088 vput(tvp); 2089 vput(dvp); 2090 return (error); 2091 } 2092 #endif 2093 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 2094 ip->i_mode = mode; 2095 tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 2096 ip->i_nlink = 1; 2097 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && 2098 suser(cnp->cn_cred, NULL)) 2099 ip->i_mode &= ~ISGID; 2100 2101 /* 2102 * Make sure inode goes to disk before directory entry. 2103 */ 2104 tv = time; 2105 if (error = VOP_UPDATE(tvp, &tv, &tv, 1)) 2106 goto bad; 2107 if (error = ufs_direnter(ip, dvp, cnp)) 2108 goto bad; 2109 if ((cnp->cn_flags & SAVESTART) == 0) 2110 FREE(cnp->cn_pnbuf, M_NAMEI); 2111 vput(dvp); 2112 *vpp = tvp; 2113 return (0); 2114 2115 bad: 2116 /* 2117 * Write error occurred trying to update the inode 2118 * or the directory so must deallocate the inode. 2119 */ 2120 free(cnp->cn_pnbuf, M_NAMEI); 2121 vput(dvp); 2122 ip->i_nlink = 0; 2123 ip->i_flag |= IN_CHANGE; 2124 vput(tvp); 2125 return (error); 2126 } 2127