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