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