ufs_vnops.c revision 38292
1/* 2 * Copyright (c) 1982, 1986, 1989, 1993, 1995 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 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95 39 * $Id: ufs_vnops.c,v 1.98 1998/08/12 20:46:47 julian Exp $ 40 */ 41 42#include "opt_quota.h" 43#include "opt_suiddir.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/namei.h> 48#include <sys/kernel.h> 49#include <sys/fcntl.h> 50#include <sys/stat.h> 51#include <sys/buf.h> 52#include <sys/proc.h> 53#include <sys/mount.h> 54#include <sys/unistd.h> 55#include <sys/vnode.h> 56#include <sys/malloc.h> 57#include <sys/dirent.h> 58#include <sys/lockf.h> 59#include <sys/poll.h> 60 61#include <vm/vm_zone.h> 62#include <vm/vm.h> 63#include <vm/vm_extern.h> 64 65#include <miscfs/specfs/specdev.h> 66#include <miscfs/fifofs/fifo.h> 67 68#include <ufs/ufs/quota.h> 69#include <ufs/ufs/inode.h> 70#include <ufs/ufs/dir.h> 71#include <ufs/ufs/ufsmount.h> 72#include <ufs/ufs/ufs_extern.h> 73 74static int ufs_abortop __P((struct vop_abortop_args *)); 75static int ufs_access __P((struct vop_access_args *)); 76static int ufs_advlock __P((struct vop_advlock_args *)); 77static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *)); 78static int ufs_chown __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *)); 79static int ufs_close __P((struct vop_close_args *)); 80static int ufs_create __P((struct vop_create_args *)); 81static int ufs_getattr __P((struct vop_getattr_args *)); 82static int ufs_link __P((struct vop_link_args *)); 83static int ufs_makeinode __P((int mode, struct vnode *, struct vnode **, struct componentname *)); 84static int ufs_missingop __P((struct vop_generic_args *ap)); 85static int ufs_mkdir __P((struct vop_mkdir_args *)); 86static int ufs_mknod __P((struct vop_mknod_args *)); 87static int ufs_mmap __P((struct vop_mmap_args *)); 88static int ufs_open __P((struct vop_open_args *)); 89static int ufs_pathconf __P((struct vop_pathconf_args *)); 90static int ufs_print __P((struct vop_print_args *)); 91static int ufs_readdir __P((struct vop_readdir_args *)); 92static int ufs_readlink __P((struct vop_readlink_args *)); 93static int ufs_remove __P((struct vop_remove_args *)); 94static int ufs_rename __P((struct vop_rename_args *)); 95static int ufs_rmdir __P((struct vop_rmdir_args *)); 96static int ufs_setattr __P((struct vop_setattr_args *)); 97static int ufs_strategy __P((struct vop_strategy_args *)); 98static int ufs_symlink __P((struct vop_symlink_args *)); 99static int ufs_whiteout __P((struct vop_whiteout_args *)); 100static int ufsfifo_close __P((struct vop_close_args *)); 101static int ufsfifo_read __P((struct vop_read_args *)); 102static int ufsfifo_write __P((struct vop_write_args *)); 103static int ufsspec_close __P((struct vop_close_args *)); 104static int ufsspec_read __P((struct vop_read_args *)); 105static int ufsspec_write __P((struct vop_write_args *)); 106 107union _qcvt { 108 int64_t qcvt; 109 int32_t val[2]; 110}; 111#define SETHIGH(q, h) { \ 112 union _qcvt tmp; \ 113 tmp.qcvt = (q); \ 114 tmp.val[_QUAD_HIGHWORD] = (h); \ 115 (q) = tmp.qcvt; \ 116} 117#define SETLOW(q, l) { \ 118 union _qcvt tmp; \ 119 tmp.qcvt = (q); \ 120 tmp.val[_QUAD_LOWWORD] = (l); \ 121 (q) = tmp.qcvt; \ 122} 123 124/* 125 * A virgin directory (no blushing please). 126 */ 127static struct dirtemplate mastertemplate = { 128 0, 12, DT_DIR, 1, ".", 129 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 130}; 131static struct odirtemplate omastertemplate = { 132 0, 12, 1, ".", 133 0, DIRBLKSIZ - 12, 2, ".." 134}; 135 136void 137ufs_itimes(vp) 138 struct vnode *vp; 139{ 140 struct inode *ip; 141 time_t tv_sec; 142 143 ip = VTOI(vp); 144 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0) 145 return; 146 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 147 tv_sec = time_second; 148 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 149 !DOINGSOFTDEP(vp)) 150 ip->i_flag |= IN_LAZYMOD; 151 else 152 ip->i_flag |= IN_MODIFIED; 153 if (ip->i_flag & IN_ACCESS) 154 ip->i_atime = tv_sec; 155 if (ip->i_flag & IN_UPDATE) { 156 ip->i_mtime = tv_sec; 157 ip->i_modrev++; 158 } 159 if (ip->i_flag & IN_CHANGE) 160 ip->i_ctime = tv_sec; 161 } 162 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); 163} 164 165/* 166 * Create a regular file 167 */ 168int 169ufs_create(ap) 170 struct vop_create_args /* { 171 struct vnode *a_dvp; 172 struct vnode **a_vpp; 173 struct componentname *a_cnp; 174 struct vattr *a_vap; 175 } */ *ap; 176{ 177 int error; 178 179 error = 180 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), 181 ap->a_dvp, ap->a_vpp, ap->a_cnp); 182 if (error) 183 return (error); 184 VN_POLLEVENT(ap->a_dvp, POLLWRITE); 185 return (0); 186} 187 188/* 189 * Mknod vnode call 190 */ 191/* ARGSUSED */ 192int 193ufs_mknod(ap) 194 struct vop_mknod_args /* { 195 struct vnode *a_dvp; 196 struct vnode **a_vpp; 197 struct componentname *a_cnp; 198 struct vattr *a_vap; 199 } */ *ap; 200{ 201 struct vattr *vap = ap->a_vap; 202 struct vnode **vpp = ap->a_vpp; 203 struct inode *ip; 204 int error; 205 206 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 207 ap->a_dvp, vpp, ap->a_cnp); 208 if (error) 209 return (error); 210 VN_POLLEVENT(ap->a_dvp, POLLWRITE); 211 ip = VTOI(*vpp); 212 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 213 if (vap->va_rdev != VNOVAL) { 214 /* 215 * Want to be able to use this to make badblock 216 * inodes, so don't truncate the dev number. 217 */ 218 ip->i_rdev = vap->va_rdev; 219 } 220 /* 221 * Remove inode so that it will be reloaded by VFS_VGET and 222 * checked to see if it is an alias of an existing entry in 223 * the inode cache. 224 */ 225 vput(*vpp); 226 (*vpp)->v_type = VNON; 227 vgone(*vpp); 228 *vpp = 0; 229 return (0); 230} 231 232/* 233 * Open called. 234 * 235 * Nothing to do. 236 */ 237/* ARGSUSED */ 238int 239ufs_open(ap) 240 struct vop_open_args /* { 241 struct vnode *a_vp; 242 int a_mode; 243 struct ucred *a_cred; 244 struct proc *a_p; 245 } */ *ap; 246{ 247 248 /* 249 * Files marked append-only must be opened for appending. 250 */ 251 if ((VTOI(ap->a_vp)->i_flags & APPEND) && 252 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 253 return (EPERM); 254 return (0); 255} 256 257/* 258 * Close called. 259 * 260 * Update the times on the inode. 261 */ 262/* ARGSUSED */ 263int 264ufs_close(ap) 265 struct vop_close_args /* { 266 struct vnode *a_vp; 267 int a_fflag; 268 struct ucred *a_cred; 269 struct proc *a_p; 270 } */ *ap; 271{ 272 register struct vnode *vp = ap->a_vp; 273 274 simple_lock(&vp->v_interlock); 275 if (vp->v_usecount > 1) 276 ufs_itimes(vp); 277 simple_unlock(&vp->v_interlock); 278 return (0); 279} 280 281int 282ufs_access(ap) 283 struct vop_access_args /* { 284 struct vnode *a_vp; 285 int a_mode; 286 struct ucred *a_cred; 287 struct proc *a_p; 288 } */ *ap; 289{ 290 struct vnode *vp = ap->a_vp; 291 struct inode *ip = VTOI(vp); 292 struct ucred *cred = ap->a_cred; 293 mode_t mask, mode = ap->a_mode; 294 register gid_t *gp; 295 int i; 296#ifdef QUOTA 297 int error; 298#endif 299 300 /* 301 * Disallow write attempts on read-only file systems; 302 * unless the file is a socket, fifo, or a block or 303 * character device resident on the file system. 304 */ 305 if (mode & VWRITE) { 306 switch (vp->v_type) { 307 case VDIR: 308 case VLNK: 309 case VREG: 310 if (vp->v_mount->mnt_flag & MNT_RDONLY) 311 return (EROFS); 312#ifdef QUOTA 313 if (error = getinoquota(ip)) 314 return (error); 315#endif 316 break; 317 default: 318 break; 319 } 320 } 321 322 /* If immutable bit set, nobody gets to write it. */ 323 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE)) 324 return (EPERM); 325 326 /* Otherwise, user id 0 always gets access. */ 327 if (cred->cr_uid == 0) 328 return (0); 329 330 mask = 0; 331 332 /* Otherwise, check the owner. */ 333 if (cred->cr_uid == ip->i_uid) { 334 if (mode & VEXEC) 335 mask |= S_IXUSR; 336 if (mode & VREAD) 337 mask |= S_IRUSR; 338 if (mode & VWRITE) 339 mask |= S_IWUSR; 340 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 341 } 342 343 /* Otherwise, check the groups. */ 344 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) 345 if (ip->i_gid == *gp) { 346 if (mode & VEXEC) 347 mask |= S_IXGRP; 348 if (mode & VREAD) 349 mask |= S_IRGRP; 350 if (mode & VWRITE) 351 mask |= S_IWGRP; 352 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 353 } 354 355 /* Otherwise, check everyone else. */ 356 if (mode & VEXEC) 357 mask |= S_IXOTH; 358 if (mode & VREAD) 359 mask |= S_IROTH; 360 if (mode & VWRITE) 361 mask |= S_IWOTH; 362 return ((ip->i_mode & mask) == mask ? 0 : EACCES); 363} 364 365/* ARGSUSED */ 366int 367ufs_getattr(ap) 368 struct vop_getattr_args /* { 369 struct vnode *a_vp; 370 struct vattr *a_vap; 371 struct ucred *a_cred; 372 struct proc *a_p; 373 } */ *ap; 374{ 375 register struct vnode *vp = ap->a_vp; 376 register struct inode *ip = VTOI(vp); 377 register struct vattr *vap = ap->a_vap; 378 379 ufs_itimes(vp); 380 /* 381 * Copy from inode table 382 */ 383 vap->va_fsid = ip->i_dev; 384 vap->va_fileid = ip->i_number; 385 vap->va_mode = ip->i_mode & ~IFMT; 386 vap->va_nlink = ip->i_effnlink; 387 vap->va_uid = ip->i_uid; 388 vap->va_gid = ip->i_gid; 389 vap->va_rdev = (dev_t)ip->i_rdev; 390 vap->va_size = ip->i_din.di_size; 391 vap->va_atime.tv_sec = ip->i_atime; 392 vap->va_atime.tv_nsec = ip->i_atimensec; 393 vap->va_mtime.tv_sec = ip->i_mtime; 394 vap->va_mtime.tv_nsec = ip->i_mtimensec; 395 vap->va_ctime.tv_sec = ip->i_ctime; 396 vap->va_ctime.tv_nsec = ip->i_ctimensec; 397 vap->va_flags = ip->i_flags; 398 vap->va_gen = ip->i_gen; 399 /* this doesn't belong here */ 400 if (vp->v_type == VBLK) 401 vap->va_blocksize = BLKDEV_IOSIZE; 402 else if (vp->v_type == VCHR) 403 vap->va_blocksize = MAXBSIZE; 404 else 405 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 406 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks); 407 vap->va_type = IFTOVT(ip->i_mode); 408 vap->va_filerev = ip->i_modrev; 409 return (0); 410} 411 412/* 413 * Set attribute vnode op. called from several syscalls 414 */ 415int 416ufs_setattr(ap) 417 struct vop_setattr_args /* { 418 struct vnode *a_vp; 419 struct vattr *a_vap; 420 struct ucred *a_cred; 421 struct proc *a_p; 422 } */ *ap; 423{ 424 struct vattr *vap = ap->a_vap; 425 struct vnode *vp = ap->a_vp; 426 struct inode *ip = VTOI(vp); 427 struct ucred *cred = ap->a_cred; 428 struct proc *p = ap->a_p; 429 int error; 430 431 /* 432 * Check for unsettable attributes. 433 */ 434 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 435 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 436 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 437 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 438 return (EINVAL); 439 } 440 if (vap->va_flags != VNOVAL) { 441 if (vp->v_mount->mnt_flag & MNT_RDONLY) 442 return (EROFS); 443 if (cred->cr_uid != ip->i_uid && 444 (error = suser(cred, &p->p_acflag))) 445 return (error); 446 if (cred->cr_uid == 0) { 447 if ((ip->i_flags 448 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) && 449 securelevel > 0) 450 return (EPERM); 451 ip->i_flags = vap->va_flags; 452 } else { 453 if (ip->i_flags 454 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 455 (vap->va_flags & UF_SETTABLE) != vap->va_flags) 456 return (EPERM); 457 ip->i_flags &= SF_SETTABLE; 458 ip->i_flags |= (vap->va_flags & UF_SETTABLE); 459 } 460 ip->i_flag |= IN_CHANGE; 461 if (vap->va_flags & (IMMUTABLE | APPEND)) 462 return (0); 463 } 464 if (ip->i_flags & (IMMUTABLE | APPEND)) 465 return (EPERM); 466 /* 467 * Go through the fields and update iff not VNOVAL. 468 */ 469 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 470 if (vp->v_mount->mnt_flag & MNT_RDONLY) 471 return (EROFS); 472 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p)) 473 return (error); 474 } 475 if (vap->va_size != VNOVAL) { 476 /* 477 * Disallow write attempts on read-only file systems; 478 * unless the file is a socket, fifo, or a block or 479 * character device resident on the file system. 480 */ 481 switch (vp->v_type) { 482 case VDIR: 483 return (EISDIR); 484 case VLNK: 485 case VREG: 486 if (vp->v_mount->mnt_flag & MNT_RDONLY) 487 return (EROFS); 488 break; 489 default: 490 break; 491 } 492 if (error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, p)) 493 return (error); 494 } 495 ip = VTOI(vp); 496 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 497 if (vp->v_mount->mnt_flag & MNT_RDONLY) 498 return (EROFS); 499 if (cred->cr_uid != ip->i_uid && 500 (error = suser(cred, &p->p_acflag)) && 501 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 502 (error = VOP_ACCESS(vp, VWRITE, cred, p)))) 503 return (error); 504 if (vap->va_atime.tv_sec != VNOVAL) 505 ip->i_flag |= IN_ACCESS; 506 if (vap->va_mtime.tv_sec != VNOVAL) 507 ip->i_flag |= IN_CHANGE | IN_UPDATE; 508 ufs_itimes(vp); 509 if (vap->va_mtime.tv_sec != VNOVAL) 510 ip->i_atime = vap->va_atime.tv_sec; 511 if (vap->va_atime.tv_sec != VNOVAL) 512 ip->i_mtime = vap->va_mtime.tv_sec; 513 error = UFS_UPDATE(vp, (struct timeval *)0, 514 (struct timeval *)0, 0); 515 if (error) 516 return (error); 517 } 518 error = 0; 519 if (vap->va_mode != (mode_t)VNOVAL) { 520 if (vp->v_mount->mnt_flag & MNT_RDONLY) 521 return (EROFS); 522 error = ufs_chmod(vp, (int)vap->va_mode, cred, p); 523 } 524 VN_POLLEVENT(vp, POLLATTRIB); 525 return (error); 526} 527 528/* 529 * Change the mode on a file. 530 * Inode must be locked before calling. 531 */ 532static int 533ufs_chmod(vp, mode, cred, p) 534 register struct vnode *vp; 535 register int mode; 536 register struct ucred *cred; 537 struct proc *p; 538{ 539 register struct inode *ip = VTOI(vp); 540 int error; 541 542 if (cred->cr_uid != ip->i_uid) { 543 error = suser(cred, &p->p_acflag); 544 if (error) 545 return (error); 546 } 547 if (cred->cr_uid) { 548 if (vp->v_type != VDIR && (mode & S_ISTXT)) 549 return (EFTYPE); 550 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) 551 return (EPERM); 552 } 553 ip->i_mode &= ~ALLPERMS; 554 ip->i_mode |= (mode & ALLPERMS); 555 ip->i_flag |= IN_CHANGE; 556 return (0); 557} 558 559/* 560 * Perform chown operation on inode ip; 561 * inode must be locked prior to call. 562 */ 563static int 564ufs_chown(vp, uid, gid, cred, p) 565 register struct vnode *vp; 566 uid_t uid; 567 gid_t gid; 568 struct ucred *cred; 569 struct proc *p; 570{ 571 register struct inode *ip = VTOI(vp); 572 uid_t ouid; 573 gid_t ogid; 574 int error = 0; 575#ifdef QUOTA 576 register int i; 577 long change; 578#endif 579 580 if (uid == (uid_t)VNOVAL) 581 uid = ip->i_uid; 582 if (gid == (gid_t)VNOVAL) 583 gid = ip->i_gid; 584 /* 585 * If we don't own the file, are trying to change the owner 586 * of the file, or are not a member of the target group, 587 * the caller must be superuser or the call fails. 588 */ 589 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 590 (gid != ip->i_gid && !groupmember((gid_t)gid, cred))) && 591 (error = suser(cred, &p->p_acflag))) 592 return (error); 593 ogid = ip->i_gid; 594 ouid = ip->i_uid; 595#ifdef QUOTA 596 if (error = getinoquota(ip)) 597 return (error); 598 if (ouid == uid) { 599 dqrele(vp, ip->i_dquot[USRQUOTA]); 600 ip->i_dquot[USRQUOTA] = NODQUOT; 601 } 602 if (ogid == gid) { 603 dqrele(vp, ip->i_dquot[GRPQUOTA]); 604 ip->i_dquot[GRPQUOTA] = NODQUOT; 605 } 606 change = ip->i_blocks; 607 (void) chkdq(ip, -change, cred, CHOWN); 608 (void) chkiq(ip, -1, cred, CHOWN); 609 for (i = 0; i < MAXQUOTAS; i++) { 610 dqrele(vp, ip->i_dquot[i]); 611 ip->i_dquot[i] = NODQUOT; 612 } 613#endif 614 ip->i_gid = gid; 615 ip->i_uid = uid; 616#ifdef QUOTA 617 if ((error = getinoquota(ip)) == 0) { 618 if (ouid == uid) { 619 dqrele(vp, ip->i_dquot[USRQUOTA]); 620 ip->i_dquot[USRQUOTA] = NODQUOT; 621 } 622 if (ogid == gid) { 623 dqrele(vp, ip->i_dquot[GRPQUOTA]); 624 ip->i_dquot[GRPQUOTA] = NODQUOT; 625 } 626 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { 627 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) 628 goto good; 629 else 630 (void) chkdq(ip, -change, cred, CHOWN|FORCE); 631 } 632 for (i = 0; i < MAXQUOTAS; i++) { 633 dqrele(vp, ip->i_dquot[i]); 634 ip->i_dquot[i] = NODQUOT; 635 } 636 } 637 ip->i_gid = ogid; 638 ip->i_uid = ouid; 639 if (getinoquota(ip) == 0) { 640 if (ouid == uid) { 641 dqrele(vp, ip->i_dquot[USRQUOTA]); 642 ip->i_dquot[USRQUOTA] = NODQUOT; 643 } 644 if (ogid == gid) { 645 dqrele(vp, ip->i_dquot[GRPQUOTA]); 646 ip->i_dquot[GRPQUOTA] = NODQUOT; 647 } 648 (void) chkdq(ip, change, cred, FORCE|CHOWN); 649 (void) chkiq(ip, 1, cred, FORCE|CHOWN); 650 (void) getinoquota(ip); 651 } 652 return (error); 653good: 654 if (getinoquota(ip)) 655 panic("ufs_chown: lost quota"); 656#endif /* QUOTA */ 657 ip->i_flag |= IN_CHANGE; 658 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid)) 659 ip->i_mode &= ~(ISUID | ISGID); 660 return (0); 661} 662 663/* 664 * Mmap a file 665 * 666 * NB Currently unsupported. 667 */ 668/* ARGSUSED */ 669int 670ufs_mmap(ap) 671 struct vop_mmap_args /* { 672 struct vnode *a_vp; 673 int a_fflags; 674 struct ucred *a_cred; 675 struct proc *a_p; 676 } */ *ap; 677{ 678 679 return (EINVAL); 680} 681 682int 683ufs_remove(ap) 684 struct vop_remove_args /* { 685 struct vnode *a_dvp; 686 struct vnode *a_vp; 687 struct componentname *a_cnp; 688 } */ *ap; 689{ 690 struct inode *ip; 691 struct vnode *vp = ap->a_vp; 692 struct vnode *dvp = ap->a_dvp; 693 int error; 694 695 ip = VTOI(vp); 696 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 697 (VTOI(dvp)->i_flags & APPEND)) { 698 error = EPERM; 699 goto out; 700 } 701 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0); 702 VN_POLLEVENT(vp, POLLNLINK); 703 VN_POLLEVENT(dvp, POLLWRITE); 704out: 705 return (error); 706} 707 708/* 709 * link vnode call 710 */ 711int 712ufs_link(ap) 713 struct vop_link_args /* { 714 struct vnode *a_tdvp; 715 struct vnode *a_vp; 716 struct componentname *a_cnp; 717 } */ *ap; 718{ 719 struct vnode *vp = ap->a_vp; 720 struct vnode *tdvp = ap->a_tdvp; 721 struct componentname *cnp = ap->a_cnp; 722 struct proc *p = cnp->cn_proc; 723 struct inode *ip; 724 struct timeval tv; 725 struct direct newdir; 726 int error; 727 728#ifdef DIAGNOSTIC 729 if ((cnp->cn_flags & HASBUF) == 0) 730 panic("ufs_link: no name"); 731#endif 732 if (tdvp->v_mount != vp->v_mount) { 733 VOP_ABORTOP(tdvp, cnp); 734 error = EXDEV; 735 goto out2; 736 } 737 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) { 738 VOP_ABORTOP(tdvp, cnp); 739 goto out2; 740 } 741 ip = VTOI(vp); 742 if ((nlink_t)ip->i_nlink >= LINK_MAX) { 743 VOP_ABORTOP(tdvp, cnp); 744 error = EMLINK; 745 goto out1; 746 } 747 if (ip->i_flags & (IMMUTABLE | APPEND)) { 748 VOP_ABORTOP(tdvp, cnp); 749 error = EPERM; 750 goto out1; 751 } 752 ip->i_effnlink++; 753 ip->i_nlink++; 754 ip->i_flag |= IN_CHANGE; 755 if (DOINGSOFTDEP(vp)) 756 softdep_increase_linkcnt(ip); 757 getmicrotime(&tv); 758 error = UFS_UPDATE(vp, &tv, &tv, !DOINGSOFTDEP(vp)); 759 if (!error) { 760 ufs_makedirentry(ip, cnp, &newdir); 761 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL); 762 } 763 764 if (error) { 765 ip->i_effnlink--; 766 ip->i_nlink--; 767 ip->i_flag |= IN_CHANGE; 768 } 769 zfree(namei_zone, cnp->cn_pnbuf); 770out1: 771 if (tdvp != vp) 772 VOP_UNLOCK(vp, 0, p); 773out2: 774 VN_POLLEVENT(vp, POLLNLINK); 775 VN_POLLEVENT(tdvp, POLLWRITE); 776 return (error); 777} 778 779/* 780 * whiteout vnode call 781 */ 782int 783ufs_whiteout(ap) 784 struct vop_whiteout_args /* { 785 struct vnode *a_dvp; 786 struct componentname *a_cnp; 787 int a_flags; 788 } */ *ap; 789{ 790 struct vnode *dvp = ap->a_dvp; 791 struct componentname *cnp = ap->a_cnp; 792 struct direct newdir; 793 int error = 0; 794 795 switch (ap->a_flags) { 796 case LOOKUP: 797 /* 4.4 format directories support whiteout operations */ 798 if (dvp->v_mount->mnt_maxsymlinklen > 0) 799 return (0); 800 return (EOPNOTSUPP); 801 802 case CREATE: 803 /* create a new directory whiteout */ 804#ifdef DIAGNOSTIC 805 if ((cnp->cn_flags & SAVENAME) == 0) 806 panic("ufs_whiteout: missing name"); 807 if (dvp->v_mount->mnt_maxsymlinklen <= 0) 808 panic("ufs_whiteout: old format filesystem"); 809#endif 810 811 newdir.d_ino = WINO; 812 newdir.d_namlen = cnp->cn_namelen; 813 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1); 814 newdir.d_type = DT_WHT; 815 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL); 816 break; 817 818 case DELETE: 819 /* remove an existing directory whiteout */ 820#ifdef DIAGNOSTIC 821 if (dvp->v_mount->mnt_maxsymlinklen <= 0) 822 panic("ufs_whiteout: old format filesystem"); 823#endif 824 825 cnp->cn_flags &= ~DOWHITEOUT; 826 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0); 827 break; 828 default: 829 panic("ufs_whiteout: unknown op"); 830 } 831 if (cnp->cn_flags & HASBUF) { 832 zfree(namei_zone, cnp->cn_pnbuf); 833 cnp->cn_flags &= ~HASBUF; 834 } 835 return (error); 836} 837 838/* 839 * Rename system call. 840 * rename("foo", "bar"); 841 * is essentially 842 * unlink("bar"); 843 * link("foo", "bar"); 844 * unlink("foo"); 845 * but ``atomically''. Can't do full commit without saving state in the 846 * inode on disk which isn't feasible at this time. Best we can do is 847 * always guarantee the target exists. 848 * 849 * Basic algorithm is: 850 * 851 * 1) Bump link count on source while we're linking it to the 852 * target. This also ensure the inode won't be deleted out 853 * from underneath us while we work (it may be truncated by 854 * a concurrent `trunc' or `open' for creation). 855 * 2) Link source to destination. If destination already exists, 856 * delete it first. 857 * 3) Unlink source reference to inode if still around. If a 858 * directory was moved and the parent of the destination 859 * is different from the source, patch the ".." entry in the 860 * directory. 861 */ 862int 863ufs_rename(ap) 864 struct vop_rename_args /* { 865 struct vnode *a_fdvp; 866 struct vnode *a_fvp; 867 struct componentname *a_fcnp; 868 struct vnode *a_tdvp; 869 struct vnode *a_tvp; 870 struct componentname *a_tcnp; 871 } */ *ap; 872{ 873 struct vnode *tvp = ap->a_tvp; 874 register struct vnode *tdvp = ap->a_tdvp; 875 struct vnode *fvp = ap->a_fvp; 876 struct vnode *fdvp = ap->a_fdvp; 877 struct componentname *tcnp = ap->a_tcnp; 878 struct componentname *fcnp = ap->a_fcnp; 879 struct proc *p = fcnp->cn_proc; 880 struct inode *ip, *xp, *dp; 881 struct direct newdir; 882 struct timeval tv; 883 int doingdirectory = 0, oldparent = 0, newparent = 0; 884 int error = 0; 885 886#ifdef DIAGNOSTIC 887 if ((tcnp->cn_flags & HASBUF) == 0 || 888 (fcnp->cn_flags & HASBUF) == 0) 889 panic("ufs_rename: no name"); 890#endif 891 /* 892 * Check for cross-device rename. 893 */ 894 if ((fvp->v_mount != tdvp->v_mount) || 895 (tvp && (fvp->v_mount != tvp->v_mount))) { 896 error = EXDEV; 897abortit: 898 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */ 899 if (tdvp == tvp) 900 vrele(tdvp); 901 else 902 vput(tdvp); 903 if (tvp) 904 vput(tvp); 905 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */ 906 vrele(fdvp); 907 vrele(fvp); 908 return (error); 909 } 910 911 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 912 (VTOI(tdvp)->i_flags & APPEND))) { 913 error = EPERM; 914 goto abortit; 915 } 916 917 /* 918 * Check if just deleting a link name or if we've lost a race. 919 * If another process completes the same rename after we've looked 920 * up the source and have blocked looking up the target, then the 921 * source and target inodes may be identical now although the 922 * names were never linked. 923 */ 924 if (fvp == tvp) { 925 if (fvp->v_type == VDIR) { 926 /* 927 * Linked directories are impossible, so we must 928 * have lost the race. Pretend that the rename 929 * completed before the lookup. 930 */ 931#ifdef UFS_RENAME_DEBUG 932 printf("ufs_rename: fvp == tvp for directories\n"); 933#endif 934 error = ENOENT; 935 goto abortit; 936 } 937 938 /* Release destination completely. */ 939 VOP_ABORTOP(tdvp, tcnp); 940 vput(tdvp); 941 vput(tvp); 942 943 /* 944 * Delete source. There is another race now that everything 945 * is unlocked, but this doesn't cause any new complications. 946 * Relookup() may find a file that is unrelated to the 947 * original one, or it may fail. Too bad. 948 */ 949 vrele(fdvp); 950 vrele(fvp); 951 fcnp->cn_flags &= ~MODMASK; 952 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 953 if ((fcnp->cn_flags & SAVESTART) == 0) 954 panic("ufs_rename: lost from startdir"); 955 fcnp->cn_nameiop = DELETE; 956 VREF(fdvp); 957 error = relookup(fdvp, &fvp, fcnp); 958 if (error == 0) 959 vrele(fdvp); 960 if (fvp == NULL) { 961#ifdef UFS_RENAME_DEBUG 962 printf("ufs_rename: from name disappeared\n"); 963#endif 964 return (ENOENT); 965 } 966 error = VOP_REMOVE(fdvp, fvp, fcnp); 967 if (fdvp == fvp) 968 vrele(fdvp); 969 else 970 vput(fdvp); 971 vput(fvp); 972 return (error); 973 } 974 if (error = vn_lock(fvp, LK_EXCLUSIVE, p)) 975 goto abortit; 976 dp = VTOI(fdvp); 977 ip = VTOI(fvp); 978 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) 979 || (dp->i_flags & APPEND)) { 980 VOP_UNLOCK(fvp, 0, p); 981 error = EPERM; 982 goto abortit; 983 } 984 if ((ip->i_mode & IFMT) == IFDIR) { 985 /* 986 * Avoid ".", "..", and aliases of "." for obvious reasons. 987 */ 988 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 989 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT || 990 (ip->i_flag & IN_RENAME)) { 991 VOP_UNLOCK(fvp, 0, p); 992 error = EINVAL; 993 goto abortit; 994 } 995 ip->i_flag |= IN_RENAME; 996 oldparent = dp->i_number; 997 doingdirectory = 1; 998 } 999 VN_POLLEVENT(fdvp, POLLWRITE); 1000 vrele(fdvp); 1001 1002 /* 1003 * When the target exists, both the directory 1004 * and target vnodes are returned locked. 1005 */ 1006 dp = VTOI(tdvp); 1007 xp = NULL; 1008 if (tvp) 1009 xp = VTOI(tvp); 1010 1011 /* 1012 * 1) Bump link count while we're moving stuff 1013 * around. If we crash somewhere before 1014 * completing our work, the link count 1015 * may be wrong, but correctable. 1016 */ 1017 ip->i_effnlink++; 1018 ip->i_nlink++; 1019 ip->i_flag |= IN_CHANGE; 1020 if (DOINGSOFTDEP(fvp)) 1021 softdep_increase_linkcnt(ip); 1022 getmicrotime(&tv); 1023 if (error = UFS_UPDATE(fvp, &tv, &tv, !DOINGSOFTDEP(fvp))) { 1024 VOP_UNLOCK(fvp, 0, p); 1025 goto bad; 1026 } 1027 1028 /* 1029 * If ".." must be changed (ie the directory gets a new 1030 * parent) then the source directory must not be in the 1031 * directory heirarchy above the target, as this would 1032 * orphan everything below the source directory. Also 1033 * the user must have write permission in the source so 1034 * as to be able to change "..". We must repeat the call 1035 * to namei, as the parent directory is unlocked by the 1036 * call to checkpath(). 1037 */ 1038 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); 1039 VOP_UNLOCK(fvp, 0, p); 1040 if (oldparent != dp->i_number) 1041 newparent = dp->i_number; 1042 if (doingdirectory && newparent) { 1043 if (error) /* write access check above */ 1044 goto bad; 1045 if (xp != NULL) 1046 vput(tvp); 1047 error = ufs_checkpath(ip, dp, tcnp->cn_cred); 1048 if (error) 1049 goto out; 1050 if ((tcnp->cn_flags & SAVESTART) == 0) 1051 panic("ufs_rename: lost to startdir"); 1052 VREF(tdvp); 1053 error = relookup(tdvp, &tvp, tcnp); 1054 if (error) 1055 goto out; 1056 vrele(tdvp); 1057 dp = VTOI(tdvp); 1058 xp = NULL; 1059 if (tvp) 1060 xp = VTOI(tvp); 1061 } 1062 /* 1063 * 2) If target doesn't exist, link the target 1064 * to the source and unlink the source. 1065 * Otherwise, rewrite the target directory 1066 * entry to reference the source inode and 1067 * expunge the original entry's existence. 1068 */ 1069 if (xp == NULL) { 1070 if (dp->i_dev != ip->i_dev) 1071 panic("ufs_rename: EXDEV"); 1072 /* 1073 * Account for ".." in new directory. 1074 * When source and destination have the same 1075 * parent we don't fool with the link count. 1076 */ 1077 if (doingdirectory && newparent) { 1078 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1079 error = EMLINK; 1080 goto bad; 1081 } 1082 dp->i_effnlink++; 1083 dp->i_nlink++; 1084 dp->i_flag |= IN_CHANGE; 1085 if (DOINGSOFTDEP(tdvp)) 1086 softdep_increase_linkcnt(dp); 1087 error = UFS_UPDATE(tdvp, &tv, &tv, !DOINGSOFTDEP(tdvp)); 1088 if (error) 1089 goto bad; 1090 } 1091 ufs_makedirentry(ip, tcnp, &newdir); 1092 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL); 1093 if (error) { 1094 if (doingdirectory && newparent) { 1095 dp->i_effnlink--; 1096 dp->i_nlink--; 1097 dp->i_flag |= IN_CHANGE; 1098 (void)UFS_UPDATE(tdvp, &tv, &tv, 1); 1099 } 1100 goto bad; 1101 } 1102 VN_POLLEVENT(tdvp, POLLWRITE); 1103 vput(tdvp); 1104 } else { 1105 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 1106 panic("ufs_rename: EXDEV"); 1107 /* 1108 * Short circuit rename(foo, foo). 1109 */ 1110 if (xp->i_number == ip->i_number) 1111 panic("ufs_rename: same file"); 1112 /* 1113 * If the parent directory is "sticky", then the user must 1114 * own the parent directory, or the destination of the rename, 1115 * otherwise the destination may not be changed (except by 1116 * root). This implements append-only directories. 1117 */ 1118 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && 1119 tcnp->cn_cred->cr_uid != dp->i_uid && 1120 xp->i_uid != tcnp->cn_cred->cr_uid) { 1121 error = EPERM; 1122 goto bad; 1123 } 1124 /* 1125 * Target must be empty if a directory and have no links 1126 * to it. Also, ensure source and target are compatible 1127 * (both directories, or both not directories). 1128 */ 1129 if ((xp->i_mode&IFMT) == IFDIR) { 1130 if ((xp->i_effnlink > 2) || 1131 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) { 1132 error = ENOTEMPTY; 1133 goto bad; 1134 } 1135 if (!doingdirectory) { 1136 error = ENOTDIR; 1137 goto bad; 1138 } 1139 cache_purge(tdvp); 1140 } else if (doingdirectory) { 1141 error = EISDIR; 1142 goto bad; 1143 } 1144 error = ufs_dirrewrite(dp, xp, ip->i_number, 1145 IFTODT(ip->i_mode), 1146 (doingdirectory && newparent) ? newparent : doingdirectory); 1147 if (error) 1148 goto bad; 1149 if (doingdirectory) { 1150 if (!newparent) { 1151 dp->i_effnlink--; 1152 dp->i_flag |= IN_CHANGE; 1153 } 1154 xp->i_effnlink--; 1155 xp->i_flag |= IN_CHANGE; 1156 } 1157 VN_POLLEVENT(tdvp, POLLWRITE); 1158 if (doingdirectory && !DOINGSOFTDEP(tvp)) { 1159 /* 1160 * Truncate inode. The only stuff left in the directory 1161 * is "." and "..". The "." reference is inconsequential 1162 * since we are quashing it. We have removed the "." 1163 * reference and the reference in the parent directory, 1164 * but there may be other hard links. The soft 1165 * dependency code will arrange to do these operations 1166 * after the parent directory entry has been deleted on 1167 * disk, so when running with that code we avoid doing 1168 * them now. 1169 */ 1170 if (!newparent) 1171 dp->i_nlink--; 1172 xp->i_nlink--; 1173 if ((error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC, 1174 tcnp->cn_cred, tcnp->cn_proc)) != 0) 1175 goto bad; 1176 } 1177 vput(tdvp); 1178 VN_POLLEVENT(tvp, POLLNLINK); /* XXX this right? */ 1179 vput(tvp); 1180 xp = NULL; 1181 } 1182 1183 /* 1184 * 3) Unlink the source. 1185 */ 1186 fcnp->cn_flags &= ~MODMASK; 1187 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 1188 if ((fcnp->cn_flags & SAVESTART) == 0) 1189 panic("ufs_rename: lost from startdir"); 1190 VREF(fdvp); 1191 error = relookup(fdvp, &fvp, fcnp); 1192 if (error == 0) 1193 vrele(fdvp); 1194 if (fvp != NULL) { 1195 xp = VTOI(fvp); 1196 dp = VTOI(fdvp); 1197 } else { 1198 /* 1199 * From name has disappeared. 1200 */ 1201 if (doingdirectory) 1202 panic("ufs_rename: lost dir entry"); 1203 vrele(ap->a_fvp); 1204 return (0); 1205 } 1206 /* 1207 * Ensure that the directory entry still exists and has not 1208 * changed while the new name has been entered. If the source is 1209 * a file then the entry may have been unlinked or renamed. In 1210 * either case there is no further work to be done. If the source 1211 * is a directory then it cannot have been rmdir'ed; the IN_RENAME 1212 * flag ensures that it cannot be moved by another rename or removed 1213 * by a rmdir. 1214 */ 1215 if (xp != ip) { 1216 if (doingdirectory) 1217 panic("ufs_rename: lost dir entry"); 1218 } else { 1219 /* 1220 * If the source is a directory with a 1221 * new parent, the link count of the old 1222 * parent directory must be decremented 1223 * and ".." set to point to the new parent. 1224 */ 1225 if (doingdirectory && newparent) { 1226 xp->i_offset = mastertemplate.dot_reclen; 1227 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0); 1228 cache_purge(fdvp); 1229 } 1230 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0); 1231 xp->i_flag &= ~IN_RENAME; 1232 } 1233 if (dp) 1234 vput(fdvp); 1235 if (xp) 1236 vput(fvp); 1237 vrele(ap->a_fvp); 1238 return (error); 1239 1240bad: 1241 if (xp) 1242 vput(ITOV(xp)); 1243 vput(ITOV(dp)); 1244out: 1245 if (doingdirectory) 1246 ip->i_flag &= ~IN_RENAME; 1247 if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) { 1248 ip->i_effnlink--; 1249 ip->i_nlink--; 1250 ip->i_flag |= IN_CHANGE; 1251 ip->i_flag &= ~IN_RENAME; 1252 vput(fvp); 1253 } else 1254 vrele(fvp); 1255 return (error); 1256} 1257 1258/* 1259 * Mkdir system call 1260 */ 1261int 1262ufs_mkdir(ap) 1263 struct vop_mkdir_args /* { 1264 struct vnode *a_dvp; 1265 struct vnode **a_vpp; 1266 struct componentname *a_cnp; 1267 struct vattr *a_vap; 1268 } */ *ap; 1269{ 1270 register struct vnode *dvp = ap->a_dvp; 1271 register struct vattr *vap = ap->a_vap; 1272 register struct componentname *cnp = ap->a_cnp; 1273 register struct inode *ip, *dp; 1274 struct vnode *tvp; 1275 struct buf *bp; 1276 struct dirtemplate dirtemplate, *dtp; 1277 struct direct newdir; 1278 struct timeval tv; 1279 int error, dmode; 1280 long blkoff; 1281 1282#ifdef DIAGNOSTIC 1283 if ((cnp->cn_flags & HASBUF) == 0) 1284 panic("ufs_mkdir: no name"); 1285#endif 1286 dp = VTOI(dvp); 1287 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1288 error = EMLINK; 1289 goto out; 1290 } 1291 dmode = vap->va_mode & 0777; 1292 dmode |= IFDIR; 1293 /* 1294 * Must simulate part of ufs_makeinode here to acquire the inode, 1295 * but not have it entered in the parent directory. The entry is 1296 * made later after writing "." and ".." entries. 1297 */ 1298 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp); 1299 if (error) 1300 goto out; 1301 ip = VTOI(tvp); 1302 ip->i_gid = dp->i_gid; 1303#ifdef SUIDDIR 1304 { 1305#ifdef QUOTA 1306 struct ucred ucred, *ucp; 1307 ucp = cnp->cn_cred; 1308#endif I 1309 /* 1310 * If we are hacking owners here, (only do this where told to) 1311 * and we are not giving it TOO root, (would subvert quotas) 1312 * then go ahead and give it to the other user. 1313 * The new directory also inherits the SUID bit. 1314 * If user's UID and dir UID are the same, 1315 * 'give it away' so that the SUID is still forced on. 1316 */ 1317 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && 1318 (dp->i_mode & ISUID) && dp->i_uid) { 1319 dmode |= ISUID; 1320 ip->i_uid = dp->i_uid; 1321#ifdef QUOTA 1322 if (dp->i_uid != cnp->cn_cred->cr_uid) { 1323 /* 1324 * Make sure the correct user gets charged 1325 * for the space. 1326 * Make a dummy credential for the victim. 1327 * XXX This seems to never be accessed out of 1328 * our context so a stack variable is ok. 1329 */ 1330 ucred.cr_ref = 1; 1331 ucred.cr_uid = ip->i_uid; 1332 ucred.cr_ngroups = 1; 1333 ucred.cr_groups[0] = dp->i_gid; 1334 ucp = &ucred; 1335 } 1336#endif 1337 } else 1338 ip->i_uid = cnp->cn_cred->cr_uid; 1339#ifdef QUOTA 1340 if ((error = getinoquota(ip)) || 1341 (error = chkiq(ip, 1, ucp, 0))) { 1342 zfree(namei_zone, cnp->cn_pnbuf); 1343 UFS_VFREE(tvp, ip->i_number, dmode); 1344 vput(tvp); 1345 return (error); 1346 } 1347#endif 1348 } 1349#else /* !SUIDDIR */ 1350 ip->i_uid = cnp->cn_cred->cr_uid; 1351#ifdef QUOTA 1352 if ((error = getinoquota(ip)) || 1353 (error = chkiq(ip, 1, cnp->cn_cred, 0))) { 1354 zfree(namei_zone, cnp->cn_pnbuf); 1355 UFS_VFREE(tvp, ip->i_number, dmode); 1356 vput(tvp); 1357 return (error); 1358 } 1359#endif 1360#endif /* !SUIDDIR */ 1361 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1362 ip->i_mode = dmode; 1363 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ 1364 ip->i_effnlink = 2; 1365 ip->i_nlink = 2; 1366 if (DOINGSOFTDEP(tvp)) 1367 softdep_increase_linkcnt(ip); 1368 if (cnp->cn_flags & ISWHITEOUT) 1369 ip->i_flags |= UF_OPAQUE; 1370 1371 /* 1372 * Bump link count in parent directory to reflect work done below. 1373 * Should be done before reference is created so cleanup is 1374 * possible if we crash. 1375 */ 1376 dp->i_effnlink++; 1377 dp->i_nlink++; 1378 dp->i_flag |= IN_CHANGE; 1379 if (DOINGSOFTDEP(dvp)) 1380 softdep_increase_linkcnt(dp); 1381 getmicrotime(&tv); 1382 error = UFS_UPDATE(tvp, &tv, &tv, !DOINGSOFTDEP(dvp)); 1383 if (error) 1384 goto bad; 1385 1386 /* 1387 * Initialize directory with "." and ".." from static template. 1388 */ 1389 if (dvp->v_mount->mnt_maxsymlinklen > 0 1390 ) 1391 dtp = &mastertemplate; 1392 else 1393 dtp = (struct dirtemplate *)&omastertemplate; 1394 dirtemplate = *dtp; 1395 dirtemplate.dot_ino = ip->i_number; 1396 dirtemplate.dotdot_ino = dp->i_number; 1397 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred, 1398 B_CLRBUF, &bp)) != 0) 1399 goto bad; 1400 ip->i_size = DIRBLKSIZ; 1401 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1402 vnode_pager_setsize(tvp, (u_long)ip->i_size); 1403 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate); 1404 if (DOINGSOFTDEP(tvp)) { 1405 /* 1406 * Ensure that the entire newly allocated block is a 1407 * valid directory so that future growth within the 1408 * block does not have to ensure that the block is 1409 * written before the inode. 1410 */ 1411 blkoff = DIRBLKSIZ; 1412 while (blkoff < bp->b_bcount) { 1413 ((struct direct *) 1414 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ; 1415 blkoff += DIRBLKSIZ; 1416 } 1417 } 1418 if ((error = UFS_UPDATE(tvp, &tv, &tv, !DOINGSOFTDEP(tvp))) != 0) { 1419 (void)VOP_BWRITE(bp); 1420 goto bad; 1421 } 1422 VN_POLLEVENT(dvp, POLLWRITE); /* XXX right place? */ 1423 /* 1424 * Directory set up, now install its entry in the parent directory. 1425 * 1426 * If we are not doing soft dependencies, then we must write out the 1427 * buffer containing the new directory body before entering the new 1428 * name in the parent. If we are doing soft dependencies, then the 1429 * buffer containing the new directory body will be passed to and 1430 * released in the soft dependency code after the code has attached 1431 * an appropriate ordering dependency to the buffer which ensures that 1432 * the buffer is written before the new name is written in the parent. 1433 */ 1434 if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp)) != 0)) 1435 goto bad; 1436 ufs_makedirentry(ip, cnp, &newdir); 1437 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp); 1438 1439bad: 1440 if (error == 0) { 1441 *ap->a_vpp = tvp; 1442 } else { 1443 dp->i_effnlink--; 1444 dp->i_nlink--; 1445 dp->i_flag |= IN_CHANGE; 1446 /* 1447 * No need to do an explicit VOP_TRUNCATE here, vrele will 1448 * do this for us because we set the link count to 0. 1449 */ 1450 ip->i_effnlink = 0; 1451 ip->i_nlink = 0; 1452 ip->i_flag |= IN_CHANGE; 1453 vput(tvp); 1454 } 1455out: 1456 zfree(namei_zone, cnp->cn_pnbuf); 1457 return (error); 1458} 1459 1460/* 1461 * Rmdir system call. 1462 */ 1463int 1464ufs_rmdir(ap) 1465 struct vop_rmdir_args /* { 1466 struct vnode *a_dvp; 1467 struct vnode *a_vp; 1468 struct componentname *a_cnp; 1469 } */ *ap; 1470{ 1471 struct vnode *vp = ap->a_vp; 1472 struct vnode *dvp = ap->a_dvp; 1473 struct componentname *cnp = ap->a_cnp; 1474 struct inode *ip, *dp; 1475 int error; 1476 1477 ip = VTOI(vp); 1478 dp = VTOI(dvp); 1479 1480 /* 1481 * Do not remove a directory that is in the process of being renamed. 1482 * Verify the directory is empty (and valid). Rmdir ".." will not be 1483 * valid since ".." will contain a reference to the current directory 1484 * and thus be non-empty. 1485 */ 1486 error = 0; 1487 if (ip->i_flag & IN_RENAME) { 1488 error = EINVAL; 1489 goto out; 1490 } 1491 if (ip->i_effnlink != 2 || 1492 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1493 error = ENOTEMPTY; 1494 goto out; 1495 } 1496 if ((dp->i_flags & APPEND) 1497 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1498 error = EPERM; 1499 goto out; 1500 } 1501 /* 1502 * Delete reference to directory before purging 1503 * inode. If we crash in between, the directory 1504 * will be reattached to lost+found, 1505 */ 1506 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1); 1507 if (error) 1508 goto out; 1509 VN_POLLEVENT(dvp, POLLWRITE|POLLNLINK); 1510 cache_purge(dvp); 1511 /* 1512 * Truncate inode. The only stuff left in the directory is "." and 1513 * "..". The "." reference is inconsequential since we are quashing 1514 * it. We have removed the "." reference and the reference in the 1515 * parent directory, but there may be other hard links. So, 1516 * ufs_dirremove will set the UF_IMMUTABLE flag to ensure that no 1517 * new entries are made. The soft dependency code will arrange to 1518 * do these operations after the parent directory entry has been 1519 * deleted on disk, so when running with that code we avoid doing 1520 * them now. 1521 */ 1522 dp->i_effnlink--; 1523 dp->i_flag |= IN_CHANGE; 1524 ip->i_effnlink--; 1525 ip->i_flag |= IN_CHANGE; 1526 if (!DOINGSOFTDEP(vp)) { 1527 dp->i_nlink--; 1528 ip->i_nlink--; 1529 error = UFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred, 1530 cnp->cn_proc); 1531 } 1532 cache_purge(vp); 1533out: 1534 VN_POLLEVENT(vp, POLLNLINK); 1535 return (error); 1536} 1537 1538/* 1539 * symlink -- make a symbolic link 1540 */ 1541int 1542ufs_symlink(ap) 1543 struct vop_symlink_args /* { 1544 struct vnode *a_dvp; 1545 struct vnode **a_vpp; 1546 struct componentname *a_cnp; 1547 struct vattr *a_vap; 1548 char *a_target; 1549 } */ *ap; 1550{ 1551 register struct vnode *vp, **vpp = ap->a_vpp; 1552 register struct inode *ip; 1553 int len, error; 1554 1555 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, 1556 vpp, ap->a_cnp); 1557 if (error) 1558 return (error); 1559 VN_POLLEVENT(ap->a_dvp, POLLWRITE); 1560 vp = *vpp; 1561 len = strlen(ap->a_target); 1562 if (len < vp->v_mount->mnt_maxsymlinklen) { 1563 ip = VTOI(vp); 1564 bcopy(ap->a_target, (char *)ip->i_shortlink, len); 1565 ip->i_size = len; 1566 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1567 } else 1568 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1569 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0, 1570 (struct proc *)0); 1571 vput(vp); 1572 return (error); 1573} 1574 1575/* 1576 * Vnode op for reading directories. 1577 * 1578 * The routine below assumes that the on-disk format of a directory 1579 * is the same as that defined by <sys/dirent.h>. If the on-disk 1580 * format changes, then it will be necessary to do a conversion 1581 * from the on-disk format that read returns to the format defined 1582 * by <sys/dirent.h>. 1583 */ 1584int 1585ufs_readdir(ap) 1586 struct vop_readdir_args /* { 1587 struct vnode *a_vp; 1588 struct uio *a_uio; 1589 struct ucred *a_cred; 1590 int *a_eofflag; 1591 int *ncookies; 1592 u_long **a_cookies; 1593 } */ *ap; 1594{ 1595 register struct uio *uio = ap->a_uio; 1596 int error; 1597 size_t count, lost; 1598 off_t off; 1599 1600 if (ap->a_ncookies != NULL) 1601 /* 1602 * Ensure that the block is aligned. The caller can use 1603 * the cookies to determine where in the block to start. 1604 */ 1605 uio->uio_offset &= ~(DIRBLKSIZ - 1); 1606 off = uio->uio_offset; 1607 count = uio->uio_resid; 1608 /* Make sure we don't return partial entries. */ 1609 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1); 1610 if (count <= 0) 1611 return (EINVAL); 1612 lost = uio->uio_resid - count; 1613 uio->uio_resid = count; 1614 uio->uio_iov->iov_len = count; 1615# if (BYTE_ORDER == LITTLE_ENDIAN) 1616 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) { 1617 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); 1618 } else { 1619 struct dirent *dp, *edp; 1620 struct uio auio; 1621 struct iovec aiov; 1622 caddr_t dirbuf; 1623 int readcnt; 1624 u_char tmp; 1625 1626 auio = *uio; 1627 auio.uio_iov = &aiov; 1628 auio.uio_iovcnt = 1; 1629 auio.uio_segflg = UIO_SYSSPACE; 1630 aiov.iov_len = count; 1631 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK); 1632 aiov.iov_base = dirbuf; 1633 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred); 1634 if (error == 0) { 1635 readcnt = count - auio.uio_resid; 1636 edp = (struct dirent *)&dirbuf[readcnt]; 1637 for (dp = (struct dirent *)dirbuf; dp < edp; ) { 1638 tmp = dp->d_namlen; 1639 dp->d_namlen = dp->d_type; 1640 dp->d_type = tmp; 1641 if (dp->d_reclen > 0) { 1642 dp = (struct dirent *) 1643 ((char *)dp + dp->d_reclen); 1644 } else { 1645 error = EIO; 1646 break; 1647 } 1648 } 1649 if (dp >= edp) 1650 error = uiomove(dirbuf, readcnt, uio); 1651 } 1652 FREE(dirbuf, M_TEMP); 1653 } 1654# else 1655 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); 1656# endif 1657 if (!error && ap->a_ncookies != NULL) { 1658 struct dirent* dpStart; 1659 struct dirent* dpEnd; 1660 struct dirent* dp; 1661 int ncookies; 1662 u_long *cookies; 1663 u_long *cookiep; 1664 1665 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1666 panic("ufs_readdir: unexpected uio from NFS server"); 1667 dpStart = (struct dirent *) 1668 (uio->uio_iov->iov_base - (uio->uio_offset - off)); 1669 dpEnd = (struct dirent *) uio->uio_iov->iov_base; 1670 for (dp = dpStart, ncookies = 0; 1671 dp < dpEnd; 1672 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) 1673 ncookies++; 1674 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP, 1675 M_WAITOK); 1676 for (dp = dpStart, cookiep = cookies; 1677 dp < dpEnd; 1678 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) { 1679 off += dp->d_reclen; 1680 *cookiep++ = (u_long) off; 1681 } 1682 *ap->a_ncookies = ncookies; 1683 *ap->a_cookies = cookies; 1684 } 1685 uio->uio_resid += lost; 1686 if (ap->a_eofflag) 1687 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset; 1688 return (error); 1689} 1690 1691/* 1692 * Return target name of a symbolic link 1693 */ 1694int 1695ufs_readlink(ap) 1696 struct vop_readlink_args /* { 1697 struct vnode *a_vp; 1698 struct uio *a_uio; 1699 struct ucred *a_cred; 1700 } */ *ap; 1701{ 1702 register struct vnode *vp = ap->a_vp; 1703 register struct inode *ip = VTOI(vp); 1704 int isize; 1705 1706 isize = ip->i_size; 1707 if ((isize < vp->v_mount->mnt_maxsymlinklen) || 1708 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */ 1709 uiomove((char *)ip->i_shortlink, isize, ap->a_uio); 1710 return (0); 1711 } 1712 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1713} 1714 1715/* 1716 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 1717 * done. If a buffer has been saved in anticipation of a CREATE, delete it. 1718 */ 1719/* ARGSUSED */ 1720int 1721ufs_abortop(ap) 1722 struct vop_abortop_args /* { 1723 struct vnode *a_dvp; 1724 struct componentname *a_cnp; 1725 } */ *ap; 1726{ 1727 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) 1728 zfree(namei_zone, ap->a_cnp->cn_pnbuf); 1729 return (0); 1730} 1731 1732/* 1733 * Calculate the logical to physical mapping if not done already, 1734 * then call the device strategy routine. 1735 */ 1736int 1737ufs_strategy(ap) 1738 struct vop_strategy_args /* { 1739 struct vnode *a_vp; 1740 struct buf *a_bp; 1741 } */ *ap; 1742{ 1743 register struct buf *bp = ap->a_bp; 1744 register struct vnode *vp = ap->a_vp; 1745 register struct inode *ip; 1746 int error; 1747 1748 ip = VTOI(vp); 1749 if (vp->v_type == VBLK || vp->v_type == VCHR) 1750 panic("ufs_strategy: spec"); 1751 if (bp->b_blkno == bp->b_lblkno) { 1752 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 1753 if (error) { 1754 bp->b_error = error; 1755 bp->b_flags |= B_ERROR; 1756 biodone(bp); 1757 return (error); 1758 } 1759 if ((long)bp->b_blkno == -1) 1760 vfs_bio_clrbuf(bp); 1761 } 1762 if ((long)bp->b_blkno == -1) { 1763 biodone(bp); 1764 return (0); 1765 } 1766 vp = ip->i_devvp; 1767 bp->b_dev = vp->v_rdev; 1768 VOP_STRATEGY(vp, bp); 1769 return (0); 1770} 1771 1772/* 1773 * Print out the contents of an inode. 1774 */ 1775int 1776ufs_print(ap) 1777 struct vop_print_args /* { 1778 struct vnode *a_vp; 1779 } */ *ap; 1780{ 1781 register struct vnode *vp = ap->a_vp; 1782 register struct inode *ip = VTOI(vp); 1783 1784 printf("tag VT_UFS, ino %lu, on dev %#lx (%d, %d)", 1785 (u_long)ip->i_number, (u_long)ip->i_dev, major(ip->i_dev), 1786 minor(ip->i_dev)); 1787 if (vp->v_type == VFIFO) 1788 fifo_printinfo(vp); 1789 lockmgr_printinfo(&ip->i_lock); 1790 printf("\n"); 1791 return (0); 1792} 1793 1794/* 1795 * Read wrapper for special devices. 1796 */ 1797int 1798ufsspec_read(ap) 1799 struct vop_read_args /* { 1800 struct vnode *a_vp; 1801 struct uio *a_uio; 1802 int a_ioflag; 1803 struct ucred *a_cred; 1804 } */ *ap; 1805{ 1806 int error, resid; 1807 struct uio *uio; 1808 1809 uio = ap->a_uio; 1810 resid = uio->uio_resid; 1811 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap); 1812 if (uio->uio_resid != resid) 1813 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1814 return (error); 1815} 1816 1817/* 1818 * Write wrapper for special devices. 1819 */ 1820int 1821ufsspec_write(ap) 1822 struct vop_write_args /* { 1823 struct vnode *a_vp; 1824 struct uio *a_uio; 1825 int a_ioflag; 1826 struct ucred *a_cred; 1827 } */ *ap; 1828{ 1829 int error, resid; 1830 struct uio *uio; 1831 1832 uio = ap->a_uio; 1833 resid = uio->uio_resid; 1834 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap); 1835 if (uio->uio_resid != resid) 1836 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; 1837 return (error); 1838} 1839 1840/* 1841 * Close wrapper for special devices. 1842 * 1843 * Update the times on the inode then do device close. 1844 */ 1845int 1846ufsspec_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 struct vnode *vp = ap->a_vp; 1855 1856 simple_lock(&vp->v_interlock); 1857 if (vp->v_usecount > 1) 1858 ufs_itimes(vp); 1859 simple_unlock(&vp->v_interlock); 1860 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap)); 1861} 1862 1863/* 1864 * Read wrapper for fifos. 1865 */ 1866int 1867ufsfifo_read(ap) 1868 struct vop_read_args /* { 1869 struct vnode *a_vp; 1870 struct uio *a_uio; 1871 int a_ioflag; 1872 struct ucred *a_cred; 1873 } */ *ap; 1874{ 1875 int error, resid; 1876 struct uio *uio; 1877 1878 uio = ap->a_uio; 1879 resid = uio->uio_resid; 1880 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap); 1881 if (uio->uio_resid != resid && 1882 (ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1883 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1884 return (error); 1885} 1886 1887/* 1888 * Write wrapper for fifos. 1889 */ 1890int 1891ufsfifo_write(ap) 1892 struct vop_write_args /* { 1893 struct vnode *a_vp; 1894 struct uio *a_uio; 1895 int a_ioflag; 1896 struct ucred *a_cred; 1897 } */ *ap; 1898{ 1899 int error, resid; 1900 struct uio *uio; 1901 1902 uio = ap->a_uio; 1903 resid = uio->uio_resid; 1904 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap); 1905 if (uio->uio_resid != resid) 1906 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; 1907 return (error); 1908} 1909 1910/* 1911 * Close wrapper for fifos. 1912 * 1913 * Update the times on the inode then do device close. 1914 */ 1915int 1916ufsfifo_close(ap) 1917 struct vop_close_args /* { 1918 struct vnode *a_vp; 1919 int a_fflag; 1920 struct ucred *a_cred; 1921 struct proc *a_p; 1922 } */ *ap; 1923{ 1924 struct vnode *vp = ap->a_vp; 1925 1926 simple_lock(&vp->v_interlock); 1927 if (vp->v_usecount > 1) 1928 ufs_itimes(vp); 1929 simple_unlock(&vp->v_interlock); 1930 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap)); 1931} 1932 1933/* 1934 * Return POSIX pathconf information applicable to ufs filesystems. 1935 */ 1936int 1937ufs_pathconf(ap) 1938 struct vop_pathconf_args /* { 1939 struct vnode *a_vp; 1940 int a_name; 1941 int *a_retval; 1942 } */ *ap; 1943{ 1944 1945 switch (ap->a_name) { 1946 case _PC_LINK_MAX: 1947 *ap->a_retval = LINK_MAX; 1948 return (0); 1949 case _PC_NAME_MAX: 1950 *ap->a_retval = NAME_MAX; 1951 return (0); 1952 case _PC_PATH_MAX: 1953 *ap->a_retval = PATH_MAX; 1954 return (0); 1955 case _PC_PIPE_BUF: 1956 *ap->a_retval = PIPE_BUF; 1957 return (0); 1958 case _PC_CHOWN_RESTRICTED: 1959 *ap->a_retval = 1; 1960 return (0); 1961 case _PC_NO_TRUNC: 1962 *ap->a_retval = 1; 1963 return (0); 1964 default: 1965 return (EINVAL); 1966 } 1967 /* NOTREACHED */ 1968} 1969 1970/* 1971 * Advisory record locking support 1972 */ 1973int 1974ufs_advlock(ap) 1975 struct vop_advlock_args /* { 1976 struct vnode *a_vp; 1977 caddr_t a_id; 1978 int a_op; 1979 struct flock *a_fl; 1980 int a_flags; 1981 } */ *ap; 1982{ 1983 register struct inode *ip = VTOI(ap->a_vp); 1984 1985 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size)); 1986} 1987 1988/* 1989 * Initialize the vnode associated with a new inode, handle aliased 1990 * vnodes. 1991 */ 1992int 1993ufs_vinit(mntp, specops, fifoops, vpp) 1994 struct mount *mntp; 1995 vop_t **specops; 1996 vop_t **fifoops; 1997 struct vnode **vpp; 1998{ 1999 struct inode *ip; 2000 struct vnode *vp, *nvp; 2001 struct timeval tv; 2002 2003 vp = *vpp; 2004 ip = VTOI(vp); 2005 switch(vp->v_type = IFTOVT(ip->i_mode)) { 2006 case VCHR: 2007 case VBLK: 2008 vp->v_op = specops; 2009 nvp = checkalias(vp, ip->i_rdev, mntp); 2010 if (nvp) { 2011 /* 2012 * Discard unneeded vnode, but save its inode. 2013 * Note that the lock is carried over in the inode 2014 * to the replacement vnode. 2015 */ 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 } 2027 break; 2028 case VFIFO: 2029 vp->v_op = fifoops; 2030 break; 2031 default: 2032 break; 2033 2034 } 2035 if (ip->i_number == ROOTINO) 2036 vp->v_flag |= VROOT; 2037 /* 2038 * Initialize modrev times 2039 */ 2040 getmicrouptime(&tv); 2041 SETHIGH(ip->i_modrev, tv.tv_sec); 2042 SETLOW(ip->i_modrev, tv.tv_usec * 4294); 2043 *vpp = vp; 2044 return (0); 2045} 2046 2047/* 2048 * Allocate a new inode. 2049 */ 2050int 2051ufs_makeinode(mode, dvp, vpp, cnp) 2052 int mode; 2053 struct vnode *dvp; 2054 struct vnode **vpp; 2055 struct componentname *cnp; 2056{ 2057 register struct inode *ip, *pdir; 2058 struct direct newdir; 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 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp); 2073 if (error) { 2074 zfree(namei_zone, cnp->cn_pnbuf); 2075 return (error); 2076 } 2077 ip = VTOI(tvp); 2078 ip->i_gid = pdir->i_gid; 2079#ifdef SUIDDIR 2080 { 2081#ifdef QUOTA 2082 struct ucred ucred, *ucp; 2083 ucp = cnp->cn_cred; 2084#endif I 2085 /* 2086 * If we are not the owner of the directory, 2087 * and we are hacking owners here, (only do this where told to) 2088 * and we are not giving it TOO root, (would subvert quotas) 2089 * then go ahead and give it to the other user. 2090 * Note that this drops off the execute bits for security. 2091 */ 2092 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && 2093 (pdir->i_mode & ISUID) && 2094 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) { 2095 ip->i_uid = pdir->i_uid; 2096 mode &= ~07111; 2097#ifdef QUOTA 2098 /* 2099 * Make sure the correct user gets charged 2100 * for the space. 2101 * Quickly knock up a dummy credential for the victim. 2102 * XXX This seems to never be accessed out of our 2103 * context so a stack variable is ok. 2104 */ 2105 ucred.cr_ref = 1; 2106 ucred.cr_uid = ip->i_uid; 2107 ucred.cr_ngroups = 1; 2108 ucred.cr_groups[0] = pdir->i_gid; 2109 ucp = &ucred; 2110#endif 2111 } else 2112 ip->i_uid = cnp->cn_cred->cr_uid; 2113 2114#ifdef QUOTA 2115 if ((error = getinoquota(ip)) || 2116 (error = chkiq(ip, 1, ucp, 0))) { 2117 zfree(namei_zone, cnp->cn_pnbuf); 2118 UFS_VFREE(tvp, ip->i_number, mode); 2119 vput(tvp); 2120 return (error); 2121 } 2122#endif 2123 } 2124#else /* !SUIDDIR */ 2125 ip->i_uid = cnp->cn_cred->cr_uid; 2126#ifdef QUOTA 2127 if ((error = getinoquota(ip)) || 2128 (error = chkiq(ip, 1, cnp->cn_cred, 0))) { 2129 zfree(namei_zone, cnp->cn_pnbuf); 2130 UFS_VFREE(tvp, ip->i_number, mode); 2131 vput(tvp); 2132 return (error); 2133 } 2134#endif 2135#endif /* !SUIDDIR */ 2136 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 2137 ip->i_mode = mode; 2138 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ 2139 ip->i_effnlink = 1; 2140 ip->i_nlink = 1; 2141 if (DOINGSOFTDEP(tvp)) 2142 softdep_increase_linkcnt(ip); 2143 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && 2144 suser(cnp->cn_cred, NULL)) 2145 ip->i_mode &= ~ISGID; 2146 2147 if (cnp->cn_flags & ISWHITEOUT) 2148 ip->i_flags |= UF_OPAQUE; 2149 2150 /* 2151 * Make sure inode goes to disk before directory entry. 2152 */ 2153 getmicrotime(&tv); 2154 error = UFS_UPDATE(tvp, &tv, &tv, !DOINGSOFTDEP(tvp)); 2155 if (error) 2156 goto bad; 2157 ufs_makedirentry(ip, cnp, &newdir); 2158 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL); 2159 if (error) 2160 goto bad; 2161 2162 if ((cnp->cn_flags & SAVESTART) == 0) 2163 zfree(namei_zone, cnp->cn_pnbuf); 2164 *vpp = tvp; 2165 return (0); 2166 2167bad: 2168 /* 2169 * Write error occurred trying to update the inode 2170 * or the directory so must deallocate the inode. 2171 */ 2172 zfree(namei_zone, cnp->cn_pnbuf); 2173 ip->i_effnlink = 0; 2174 ip->i_nlink = 0; 2175 ip->i_flag |= IN_CHANGE; 2176 vput(tvp); 2177 return (error); 2178} 2179 2180static int 2181ufs_missingop(ap) 2182 struct vop_generic_args *ap; 2183{ 2184 2185 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name); 2186 return (EOPNOTSUPP); 2187} 2188 2189/* Global vfs data structures for ufs. */ 2190static vop_t **ufs_vnodeop_p; 2191static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = { 2192 { &vop_default_desc, (vop_t *) vop_defaultop }, 2193 { &vop_fsync_desc, (vop_t *) ufs_missingop }, 2194 { &vop_read_desc, (vop_t *) ufs_missingop }, 2195 { &vop_reallocblks_desc, (vop_t *) ufs_missingop }, 2196 { &vop_write_desc, (vop_t *) ufs_missingop }, 2197 { &vop_abortop_desc, (vop_t *) ufs_abortop }, 2198 { &vop_access_desc, (vop_t *) ufs_access }, 2199 { &vop_advlock_desc, (vop_t *) ufs_advlock }, 2200 { &vop_bmap_desc, (vop_t *) ufs_bmap }, 2201 { &vop_cachedlookup_desc, (vop_t *) ufs_lookup }, 2202 { &vop_close_desc, (vop_t *) ufs_close }, 2203 { &vop_create_desc, (vop_t *) ufs_create }, 2204 { &vop_getattr_desc, (vop_t *) ufs_getattr }, 2205 { &vop_inactive_desc, (vop_t *) ufs_inactive }, 2206 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, 2207 { &vop_link_desc, (vop_t *) ufs_link }, 2208 { &vop_lock_desc, (vop_t *) vop_stdlock }, 2209 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup }, 2210 { &vop_mkdir_desc, (vop_t *) ufs_mkdir }, 2211 { &vop_mknod_desc, (vop_t *) ufs_mknod }, 2212 { &vop_mmap_desc, (vop_t *) ufs_mmap }, 2213 { &vop_open_desc, (vop_t *) ufs_open }, 2214 { &vop_pathconf_desc, (vop_t *) ufs_pathconf }, 2215 { &vop_poll_desc, (vop_t *) vop_stdpoll }, 2216 { &vop_print_desc, (vop_t *) ufs_print }, 2217 { &vop_readdir_desc, (vop_t *) ufs_readdir }, 2218 { &vop_readlink_desc, (vop_t *) ufs_readlink }, 2219 { &vop_reclaim_desc, (vop_t *) ufs_reclaim }, 2220 { &vop_remove_desc, (vop_t *) ufs_remove }, 2221 { &vop_rename_desc, (vop_t *) ufs_rename }, 2222 { &vop_rmdir_desc, (vop_t *) ufs_rmdir }, 2223 { &vop_setattr_desc, (vop_t *) ufs_setattr }, 2224 { &vop_strategy_desc, (vop_t *) ufs_strategy }, 2225 { &vop_symlink_desc, (vop_t *) ufs_symlink }, 2226 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, 2227 { &vop_whiteout_desc, (vop_t *) ufs_whiteout }, 2228 { NULL, NULL } 2229}; 2230static struct vnodeopv_desc ufs_vnodeop_opv_desc = 2231 { &ufs_vnodeop_p, ufs_vnodeop_entries }; 2232 2233static vop_t **ufs_specop_p; 2234static struct vnodeopv_entry_desc ufs_specop_entries[] = { 2235 { &vop_default_desc, (vop_t *) spec_vnoperate }, 2236 { &vop_fsync_desc, (vop_t *) ufs_missingop }, 2237 { &vop_access_desc, (vop_t *) ufs_access }, 2238 { &vop_close_desc, (vop_t *) ufsspec_close }, 2239 { &vop_getattr_desc, (vop_t *) ufs_getattr }, 2240 { &vop_inactive_desc, (vop_t *) ufs_inactive }, 2241 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, 2242 { &vop_lock_desc, (vop_t *) vop_stdlock }, 2243 { &vop_print_desc, (vop_t *) ufs_print }, 2244 { &vop_read_desc, (vop_t *) ufsspec_read }, 2245 { &vop_reclaim_desc, (vop_t *) ufs_reclaim }, 2246 { &vop_setattr_desc, (vop_t *) ufs_setattr }, 2247 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, 2248 { &vop_write_desc, (vop_t *) ufsspec_write }, 2249 { NULL, NULL } 2250}; 2251static struct vnodeopv_desc ufs_specop_opv_desc = 2252 { &ufs_specop_p, ufs_specop_entries }; 2253 2254static vop_t **ufs_fifoop_p; 2255static struct vnodeopv_entry_desc ufs_fifoop_entries[] = { 2256 { &vop_default_desc, (vop_t *) fifo_vnoperate }, 2257 { &vop_fsync_desc, (vop_t *) ufs_missingop }, 2258 { &vop_access_desc, (vop_t *) ufs_access }, 2259 { &vop_close_desc, (vop_t *) ufsfifo_close }, 2260 { &vop_getattr_desc, (vop_t *) ufs_getattr }, 2261 { &vop_inactive_desc, (vop_t *) ufs_inactive }, 2262 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, 2263 { &vop_lock_desc, (vop_t *) vop_stdlock }, 2264 { &vop_print_desc, (vop_t *) ufs_print }, 2265 { &vop_read_desc, (vop_t *) ufsfifo_read }, 2266 { &vop_reclaim_desc, (vop_t *) ufs_reclaim }, 2267 { &vop_setattr_desc, (vop_t *) ufs_setattr }, 2268 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, 2269 { &vop_write_desc, (vop_t *) ufsfifo_write }, 2270 { NULL, NULL } 2271}; 2272static struct vnodeopv_desc ufs_fifoop_opv_desc = 2273 { &ufs_fifoop_p, ufs_fifoop_entries }; 2274 2275VNODEOP_SET(ufs_vnodeop_opv_desc); 2276VNODEOP_SET(ufs_specop_opv_desc); 2277VNODEOP_SET(ufs_fifoop_opv_desc); 2278 2279int 2280ufs_vnoperate(ap) 2281 struct vop_generic_args /* { 2282 struct vnodeop_desc *a_desc; 2283 } */ *ap; 2284{ 2285 return (VOCALL(ufs_vnodeop_p, ap->a_desc->vdesc_offset, ap)); 2286} 2287 2288int 2289ufs_vnoperatefifo(ap) 2290 struct vop_generic_args /* { 2291 struct vnodeop_desc *a_desc; 2292 } */ *ap; 2293{ 2294 return (VOCALL(ufs_fifoop_p, ap->a_desc->vdesc_offset, ap)); 2295} 2296 2297int 2298ufs_vnoperatespec(ap) 2299 struct vop_generic_args /* { 2300 struct vnodeop_desc *a_desc; 2301 } */ *ap; 2302{ 2303 return (VOCALL(ufs_specop_p, ap->a_desc->vdesc_offset, ap)); 2304} 2305 2306 2307