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