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