1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_ufs.h" 43 #include "opt_quota.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/namei.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/proc.h> 52 #include <sys/stat.h> 53 #include <sys/mount.h> 54 #include <sys/vnode.h> 55 #include <sys/sysctl.h> 56 57 #include <vm/vm.h> 58 #include <vm/vm_extern.h> 59 60 #include <ufs/ufs/extattr.h> 61 #include <ufs/ufs/quota.h> 62 #include <ufs/ufs/inode.h> 63 #include <ufs/ufs/dir.h> 64 #ifdef UFS_DIRHASH 65 #include <ufs/ufs/dirhash.h> 66 #endif 67 #include <ufs/ufs/ufsmount.h> 68 #include <ufs/ufs/ufs_extern.h> 69 #include <ufs/ffs/ffs_extern.h> 70 71 #ifdef DIAGNOSTIC 72 static int dirchk = 1; 73 #else 74 static int dirchk = 0; 75 #endif 76 77 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, ""); 78 79 static int 80 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred, 81 struct thread *td) 82 { 83 int error; 84 85 #ifdef UFS_ACL 86 /* 87 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt 88 * 89 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD 90 */ 91 92 /* 93 * XXX: Is this check required? 94 */ 95 error = VOP_ACCESS(vdp, VEXEC, cred, td); 96 if (error) 97 return (error); 98 99 error = VOP_ACCESSX(tdp, VDELETE, cred, td); 100 if (error == 0) 101 return (0); 102 103 error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td); 104 if (error == 0) 105 return (0); 106 107 error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td); 108 if (error) 109 return (error); 110 111 #endif /* !UFS_ACL */ 112 113 /* 114 * Standard Unix access control - delete access requires VWRITE. 115 */ 116 error = VOP_ACCESS(vdp, VWRITE, cred, td); 117 if (error) 118 return (error); 119 120 /* 121 * If directory is "sticky", then user must own 122 * the directory, or the file in it, else she 123 * may not delete it (unless she's root). This 124 * implements append-only directories. 125 */ 126 if ((VTOI(vdp)->i_mode & ISVTX) && 127 VOP_ACCESS(vdp, VADMIN, cred, td) && 128 VOP_ACCESS(tdp, VADMIN, cred, td)) 129 return (EPERM); 130 131 return (0); 132 } 133 134 /* 135 * Convert a component of a pathname into a pointer to a locked inode. 136 * This is a very central and rather complicated routine. 137 * If the filesystem is not maintained in a strict tree hierarchy, 138 * this can result in a deadlock situation (see comments in code below). 139 * 140 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending 141 * on whether the name is to be looked up, created, renamed, or deleted. 142 * When CREATE, RENAME, or DELETE is specified, information usable in 143 * creating, renaming, or deleting a directory entry may be calculated. 144 * If flag has LOCKPARENT or'ed into it and the target of the pathname 145 * exists, lookup returns both the target and its parent directory locked. 146 * When creating or renaming and LOCKPARENT is specified, the target may 147 * not be ".". When deleting and LOCKPARENT is specified, the target may 148 * be "."., but the caller must check to ensure it does an vrele and vput 149 * instead of two vputs. 150 * 151 * This routine is actually used as VOP_CACHEDLOOKUP method, and the 152 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP 153 * method. 154 * 155 * vfs_cache_lookup() performs the following for us: 156 * check that it is a directory 157 * check accessibility of directory 158 * check for modification attempts on read-only mounts 159 * if name found in cache 160 * if at end of path and deleting or creating 161 * drop it 162 * else 163 * return name. 164 * return VOP_CACHEDLOOKUP() 165 * 166 * Overall outline of ufs_lookup: 167 * 168 * search for name in directory, to found or notfound 169 * notfound: 170 * if creating, return locked directory, leaving info on available slots 171 * else return error 172 * found: 173 * if at end of path and deleting, return information to allow delete 174 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 175 * inode and return info to allow rewrite 176 * if not at end, add name to cache; if at end and neither creating 177 * nor deleting, add name to cache 178 */ 179 int 180 ufs_lookup( 181 struct vop_cachedlookup_args /* { 182 struct vnode *a_dvp; 183 struct vnode **a_vpp; 184 struct componentname *a_cnp; 185 } */ *ap) 186 { 187 188 return (ufs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL)); 189 } 190 191 int 192 ufs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp, 193 ino_t *dd_ino) 194 { 195 struct inode *dp; /* inode for directory being searched */ 196 struct buf *bp; /* a buffer of directory entries */ 197 struct direct *ep; /* the current directory entry */ 198 int entryoffsetinblock; /* offset of ep in bp's buffer */ 199 enum {NONE, COMPACT, FOUND} slotstatus; 200 doff_t slotoffset; /* offset of area with free space */ 201 doff_t i_diroff; /* cached i_diroff value. */ 202 doff_t i_offset; /* cached i_offset value. */ 203 int slotsize; /* size of area at slotoffset */ 204 int slotfreespace; /* amount of space free in slot */ 205 int slotneeded; /* size of the entry we're seeking */ 206 int numdirpasses; /* strategy for directory search */ 207 doff_t endsearch; /* offset to end directory search */ 208 doff_t prevoff; /* prev entry dp->i_offset */ 209 struct vnode *pdp; /* saved dp during symlink work */ 210 struct vnode *tdp; /* returned by VFS_VGET */ 211 doff_t enduseful; /* pointer past last used dir slot */ 212 u_long bmask; /* block offset mask */ 213 int namlen, error; 214 struct ucred *cred = cnp->cn_cred; 215 int flags = cnp->cn_flags; 216 int nameiop = cnp->cn_nameiop; 217 ino_t ino, ino1; 218 int ltype; 219 220 if (vpp != NULL) 221 *vpp = NULL; 222 223 dp = VTOI(vdp); 224 if (dp->i_effnlink == 0) 225 return (ENOENT); 226 227 /* 228 * Create a vm object if vmiodirenable is enabled. 229 * Alternatively we could call vnode_create_vobject 230 * in VFS_VGET but we could end up creating objects 231 * that are never used. 232 */ 233 vnode_create_vobject(vdp, DIP(dp, i_size), curthread); 234 235 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 236 237 #ifdef DEBUG_VFS_LOCKS 238 /* 239 * Assert that the directory vnode is locked, and locked 240 * exclusively for the last component lookup for modifying 241 * operations. 242 * 243 * The directory-modifying operations need to save 244 * intermediate state in the inode between namei() call and 245 * actual directory manipulations. See fields in the struct 246 * inode marked as 'used during directory lookup'. We must 247 * ensure that upgrade in namei() does not happen, since 248 * upgrade might need to unlock vdp. If quotas are enabled, 249 * getinoquota() also requires exclusive lock to modify inode. 250 */ 251 ASSERT_VOP_LOCKED(vdp, "ufs_lookup1"); 252 if ((nameiop == CREATE || nameiop == DELETE || nameiop == RENAME) && 253 (flags & (LOCKPARENT | ISLASTCN)) == (LOCKPARENT | ISLASTCN)) 254 ASSERT_VOP_ELOCKED(vdp, "ufs_lookup2"); 255 #endif 256 257 restart: 258 bp = NULL; 259 slotoffset = -1; 260 261 /* 262 * We now have a segment name to search for, and a directory to search. 263 * 264 * Suppress search for slots unless creating 265 * file and at end of pathname, in which case 266 * we watch for a place to put the new file in 267 * case it doesn't already exist. 268 */ 269 ino = 0; 270 i_diroff = dp->i_diroff; 271 slotstatus = FOUND; 272 slotfreespace = slotsize = slotneeded = 0; 273 if ((nameiop == CREATE || nameiop == RENAME) && 274 (flags & ISLASTCN)) { 275 slotstatus = NONE; 276 slotneeded = DIRECTSIZ(cnp->cn_namelen); 277 } 278 279 #ifdef UFS_DIRHASH 280 /* 281 * Use dirhash for fast operations on large directories. The logic 282 * to determine whether to hash the directory is contained within 283 * ufsdirhash_build(); a zero return means that it decided to hash 284 * this directory and it successfully built up the hash table. 285 */ 286 if (ufsdirhash_build(dp) == 0) { 287 /* Look for a free slot if needed. */ 288 enduseful = dp->i_size; 289 if (slotstatus != FOUND) { 290 slotoffset = ufsdirhash_findfree(dp, slotneeded, 291 &slotsize); 292 if (slotoffset >= 0) { 293 slotstatus = COMPACT; 294 enduseful = ufsdirhash_enduseful(dp); 295 if (enduseful < 0) 296 enduseful = dp->i_size; 297 } 298 } 299 /* Look up the component. */ 300 numdirpasses = 1; 301 entryoffsetinblock = 0; /* silence compiler warning */ 302 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen, 303 &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) { 304 case 0: 305 ep = (struct direct *)((char *)bp->b_data + 306 (i_offset & bmask)); 307 goto foundentry; 308 case ENOENT: 309 i_offset = roundup2(dp->i_size, DIRBLKSIZ); 310 goto notfound; 311 default: 312 /* Something failed; just do a linear search. */ 313 break; 314 } 315 } 316 #endif /* UFS_DIRHASH */ 317 /* 318 * If there is cached information on a previous search of 319 * this directory, pick up where we last left off. 320 * We cache only lookups as these are the most common 321 * and have the greatest payoff. Caching CREATE has little 322 * benefit as it usually must search the entire directory 323 * to determine that the entry does not exist. Caching the 324 * location of the last DELETE or RENAME has not reduced 325 * profiling time and hence has been removed in the interest 326 * of simplicity. 327 */ 328 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) { 329 entryoffsetinblock = 0; 330 i_offset = 0; 331 numdirpasses = 1; 332 } else { 333 i_offset = i_diroff; 334 if ((entryoffsetinblock = i_offset & bmask) && 335 (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp))) 336 return (error); 337 numdirpasses = 2; 338 nchstats.ncs_2passes++; 339 } 340 prevoff = i_offset; 341 endsearch = roundup2(dp->i_size, DIRBLKSIZ); 342 enduseful = 0; 343 344 searchloop: 345 while (i_offset < endsearch) { 346 /* 347 * If necessary, get the next directory block. 348 */ 349 if ((i_offset & bmask) == 0) { 350 if (bp != NULL) 351 brelse(bp); 352 error = 353 UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp); 354 if (error) 355 return (error); 356 entryoffsetinblock = 0; 357 } 358 /* 359 * If still looking for a slot, and at a DIRBLKSIZE 360 * boundary, have to start looking for free space again. 361 */ 362 if (slotstatus == NONE && 363 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) { 364 slotoffset = -1; 365 slotfreespace = 0; 366 } 367 /* 368 * Get pointer to next entry. 369 * Full validation checks are slow, so we only check 370 * enough to insure forward progress through the 371 * directory. Complete checks can be run by patching 372 * "dirchk" to be true. 373 */ 374 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock); 375 if (ep->d_reclen == 0 || ep->d_reclen > 376 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 377 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) { 378 int i; 379 380 ufs_dirbad(dp, i_offset, "mangled entry"); 381 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 382 i_offset += i; 383 entryoffsetinblock += i; 384 continue; 385 } 386 387 /* 388 * If an appropriate sized slot has not yet been found, 389 * check to see if one is available. Also accumulate space 390 * in the current block so that we can determine if 391 * compaction is viable. 392 */ 393 if (slotstatus != FOUND) { 394 int size = ep->d_reclen; 395 396 if (ep->d_ino != 0) 397 size -= DIRSIZ(OFSFMT(vdp), ep); 398 if (size > 0) { 399 if (size >= slotneeded) { 400 slotstatus = FOUND; 401 slotoffset = i_offset; 402 slotsize = ep->d_reclen; 403 } else if (slotstatus == NONE) { 404 slotfreespace += size; 405 if (slotoffset == -1) 406 slotoffset = i_offset; 407 if (slotfreespace >= slotneeded) { 408 slotstatus = COMPACT; 409 slotsize = i_offset + 410 ep->d_reclen - slotoffset; 411 } 412 } 413 } 414 } 415 416 /* 417 * Check for a name match. 418 */ 419 if (ep->d_ino) { 420 # if (BYTE_ORDER == LITTLE_ENDIAN) 421 if (OFSFMT(vdp)) 422 namlen = ep->d_type; 423 else 424 namlen = ep->d_namlen; 425 # else 426 namlen = ep->d_namlen; 427 # endif 428 if (namlen == cnp->cn_namelen && 429 (cnp->cn_nameptr[0] == ep->d_name[0]) && 430 !bcmp(cnp->cn_nameptr, ep->d_name, 431 (unsigned)namlen)) { 432 #ifdef UFS_DIRHASH 433 foundentry: 434 #endif 435 /* 436 * Save directory entry's inode number and 437 * reclen in ndp->ni_ufs area, and release 438 * directory buffer. 439 */ 440 if (!OFSFMT(vdp) && ep->d_type == DT_WHT) { 441 slotstatus = FOUND; 442 slotoffset = i_offset; 443 slotsize = ep->d_reclen; 444 enduseful = dp->i_size; 445 cnp->cn_flags |= ISWHITEOUT; 446 numdirpasses--; 447 goto notfound; 448 } 449 ino = ep->d_ino; 450 goto found; 451 } 452 } 453 prevoff = i_offset; 454 i_offset += ep->d_reclen; 455 entryoffsetinblock += ep->d_reclen; 456 if (ep->d_ino) 457 enduseful = i_offset; 458 } 459 notfound: 460 /* 461 * If we started in the middle of the directory and failed 462 * to find our target, we must check the beginning as well. 463 */ 464 if (numdirpasses == 2) { 465 numdirpasses--; 466 i_offset = 0; 467 endsearch = i_diroff; 468 goto searchloop; 469 } 470 if (bp != NULL) 471 brelse(bp); 472 /* 473 * If creating, and at end of pathname and current 474 * directory has not been removed, then can consider 475 * allowing file to be created. 476 */ 477 if ((nameiop == CREATE || nameiop == RENAME || 478 (nameiop == DELETE && 479 (cnp->cn_flags & DOWHITEOUT) && 480 (cnp->cn_flags & ISWHITEOUT))) && 481 (flags & ISLASTCN) && dp->i_effnlink != 0) { 482 /* 483 * Access for write is interpreted as allowing 484 * creation of files in the directory. 485 * 486 * XXX: Fix the comment above. 487 */ 488 if (flags & WILLBEDIR) 489 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread); 490 else 491 error = VOP_ACCESS(vdp, VWRITE, cred, curthread); 492 if (error) 493 return (error); 494 /* 495 * Return an indication of where the new directory 496 * entry should be put. If we didn't find a slot, 497 * then set dp->i_count to 0 indicating 498 * that the new slot belongs at the end of the 499 * directory. If we found a slot, then the new entry 500 * can be put in the range from dp->i_offset to 501 * dp->i_offset + dp->i_count. 502 */ 503 if (slotstatus == NONE) { 504 SET_I_OFFSET(dp, roundup2(dp->i_size, DIRBLKSIZ)); 505 SET_I_COUNT(dp, 0); 506 enduseful = I_OFFSET(dp); 507 } else if (nameiop == DELETE) { 508 SET_I_OFFSET(dp, slotoffset); 509 if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0) 510 SET_I_COUNT(dp, 0); 511 else 512 SET_I_COUNT(dp, I_OFFSET(dp) - prevoff); 513 } else { 514 SET_I_OFFSET(dp, slotoffset); 515 SET_I_COUNT(dp, slotsize); 516 if (enduseful < slotoffset + slotsize) 517 enduseful = slotoffset + slotsize; 518 } 519 SET_I_ENDOFF(dp, roundup2(enduseful, DIRBLKSIZ)); 520 /* 521 * We return with the directory locked, so that 522 * the parameters we set up above will still be 523 * valid if we actually decide to do a direnter(). 524 * We return ni_vp == NULL to indicate that the entry 525 * does not currently exist; we leave a pointer to 526 * the (locked) directory inode in ndp->ni_dvp. 527 * 528 * NB - if the directory is unlocked, then this 529 * information cannot be used. 530 */ 531 return (EJUSTRETURN); 532 } 533 /* 534 * Insert name into cache (as non-existent) if appropriate. 535 */ 536 if ((cnp->cn_flags & MAKEENTRY) != 0) 537 cache_enter(vdp, NULL, cnp); 538 return (ENOENT); 539 540 found: 541 if (dd_ino != NULL) 542 *dd_ino = ino; 543 if (numdirpasses == 2) 544 nchstats.ncs_pass2++; 545 /* 546 * Check that directory length properly reflects presence 547 * of this entry. 548 */ 549 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) { 550 ufs_dirbad(dp, i_offset, "i_size too small"); 551 dp->i_size = i_offset + DIRSIZ(OFSFMT(vdp), ep); 552 DIP_SET(dp, i_size, dp->i_size); 553 UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 554 } 555 brelse(bp); 556 557 /* 558 * Found component in pathname. 559 * If the final component of path name, save information 560 * in the cache as to where the entry was found. 561 */ 562 if ((flags & ISLASTCN) && nameiop == LOOKUP) 563 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ); 564 565 /* 566 * If deleting, and at end of pathname, return 567 * parameters which can be used to remove file. 568 */ 569 if (nameiop == DELETE && (flags & ISLASTCN)) { 570 if (flags & LOCKPARENT) 571 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__); 572 573 if (VOP_ISLOCKED(vdp) == LK_EXCLUSIVE) { 574 /* 575 * Return pointer to current entry in 576 * dp->i_offset, and distance past previous 577 * entry (if there is a previous entry in this 578 * block) in dp->i_count. 579 * 580 * We shouldn't be setting these in the 581 * WANTPARENT case (first lookup in rename()), but any 582 * lookups that will result in directory changes will 583 * overwrite these. 584 */ 585 SET_I_OFFSET(dp, i_offset); 586 if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0) 587 SET_I_COUNT(dp, 0); 588 else 589 SET_I_COUNT(dp, I_OFFSET(dp) - prevoff); 590 } 591 if (dd_ino != NULL) 592 return (0); 593 594 /* 595 * Save directory inode pointer in ndp->ni_dvp for 596 * dirremove(). 597 */ 598 if ((error = VFS_VGET(vdp->v_mount, ino, 599 LK_EXCLUSIVE, &tdp)) != 0) 600 return (error); 601 error = ufs_delete_denied(vdp, tdp, cred, curthread); 602 if (error) { 603 vput(tdp); 604 return (error); 605 } 606 if (dp->i_number == ino) { 607 VREF(vdp); 608 *vpp = vdp; 609 vput(tdp); 610 return (0); 611 } 612 613 *vpp = tdp; 614 return (0); 615 } 616 617 /* 618 * If rewriting (RENAME), return the inode and the 619 * information required to rewrite the present directory 620 * Must get inode of directory entry to verify it's a 621 * regular file, or empty directory. 622 */ 623 if (nameiop == RENAME && (flags & ISLASTCN)) { 624 if (flags & WILLBEDIR) 625 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread); 626 else 627 error = VOP_ACCESS(vdp, VWRITE, cred, curthread); 628 if (error) 629 return (error); 630 /* 631 * Careful about locking second inode. 632 * This can only occur if the target is ".". 633 */ 634 SET_I_OFFSET(dp, i_offset); 635 if (dp->i_number == ino) 636 return (EISDIR); 637 if (dd_ino != NULL) 638 return (0); 639 if ((error = VFS_VGET(vdp->v_mount, ino, 640 LK_EXCLUSIVE, &tdp)) != 0) 641 return (error); 642 643 error = ufs_delete_denied(vdp, tdp, cred, curthread); 644 if (error) { 645 vput(tdp); 646 return (error); 647 } 648 649 #ifdef SunOS_doesnt_do_that 650 /* 651 * The only purpose of this check is to return the correct 652 * error. Assume that we want to rename directory "a" 653 * to a file "b", and that we have no ACL_WRITE_DATA on 654 * a containing directory, but we _do_ have ACL_APPEND_DATA. 655 * In that case, the VOP_ACCESS check above will return 0, 656 * and the operation will fail with ENOTDIR instead 657 * of EACCESS. 658 */ 659 if (tdp->v_type == VDIR) 660 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread); 661 else 662 error = VOP_ACCESS(vdp, VWRITE, cred, curthread); 663 if (error) { 664 vput(tdp); 665 return (error); 666 } 667 #endif 668 669 *vpp = tdp; 670 return (0); 671 } 672 if (dd_ino != NULL) 673 return (0); 674 675 /* 676 * Step through the translation in the name. We do not `vput' the 677 * directory because we may need it again if a symbolic link 678 * is relative to the current directory. Instead we save it 679 * unlocked as "pdp". We must get the target inode before unlocking 680 * the directory to insure that the inode will not be removed 681 * before we get it. We prevent deadlock by always fetching 682 * inodes from the root, moving down the directory tree. Thus 683 * when following backward pointers ".." we must unlock the 684 * parent directory before getting the requested directory. 685 * There is a potential race condition here if both the current 686 * and parent directories are removed before the VFS_VGET for the 687 * inode associated with ".." returns. We hope that this occurs 688 * infrequently since we cannot avoid this race condition without 689 * implementing a sophisticated deadlock detection algorithm. 690 * Note also that this simple deadlock detection scheme will not 691 * work if the filesystem has any hard links other than ".." 692 * that point backwards in the directory structure. 693 */ 694 pdp = vdp; 695 if (flags & ISDOTDOT) { 696 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp); 697 if (error) 698 return (error); 699 700 /* 701 * Recheck that ".." entry in the vdp directory points 702 * to the inode we looked up before vdp lock was 703 * dropped. 704 */ 705 error = ufs_lookup_ino(pdp, NULL, cnp, &ino1); 706 if (error) { 707 vput(tdp); 708 return (error); 709 } 710 if (ino1 != ino) { 711 vput(tdp); 712 goto restart; 713 } 714 715 *vpp = tdp; 716 } else if (dp->i_number == ino) { 717 VREF(vdp); /* we want ourself, ie "." */ 718 /* 719 * When we lookup "." we still can be asked to lock it 720 * differently. 721 */ 722 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 723 if (ltype != VOP_ISLOCKED(vdp)) { 724 if (ltype == LK_EXCLUSIVE) 725 vn_lock(vdp, LK_UPGRADE | LK_RETRY); 726 else /* if (ltype == LK_SHARED) */ 727 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY); 728 /* 729 * Relock for the "." case may left us with 730 * reclaimed vnode. 731 */ 732 if (VN_IS_DOOMED(vdp)) { 733 vrele(vdp); 734 return (ENOENT); 735 } 736 } 737 *vpp = vdp; 738 } else { 739 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp); 740 if (error == 0 && VTOI(tdp)->i_mode == 0) { 741 vgone(tdp); 742 vput(tdp); 743 error = ENOENT; 744 } 745 if (error) 746 return (error); 747 *vpp = tdp; 748 } 749 750 /* 751 * Insert name into cache if appropriate. 752 */ 753 if (cnp->cn_flags & MAKEENTRY) 754 cache_enter(vdp, *vpp, cnp); 755 return (0); 756 } 757 758 void 759 ufs_dirbad(struct inode *ip, doff_t offset, char *how) 760 { 761 struct mount *mp; 762 763 mp = ITOV(ip)->v_mount; 764 if ((mp->mnt_flag & MNT_RDONLY) == 0) 765 panic("ufs_dirbad: %s: bad dir ino %ju at offset %ld: %s", 766 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number, 767 (long)offset, how); 768 else 769 (void)printf("%s: bad dir ino %ju at offset %ld: %s\n", 770 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number, 771 (long)offset, how); 772 } 773 774 /* 775 * Do consistency checking on a directory entry: 776 * record length must be multiple of 4 777 * entry must fit in rest of its DIRBLKSIZ block 778 * record must be large enough to contain entry 779 * name is not longer than UFS_MAXNAMLEN 780 * name must be as long as advertised, and null terminated 781 */ 782 int 783 ufs_dirbadentry(struct vnode *dp, struct direct *ep, int entryoffsetinblock) 784 { 785 int i, namlen; 786 787 # if (BYTE_ORDER == LITTLE_ENDIAN) 788 if (OFSFMT(dp)) 789 namlen = ep->d_type; 790 else 791 namlen = ep->d_namlen; 792 # else 793 namlen = ep->d_namlen; 794 # endif 795 if ((ep->d_reclen & 0x3) != 0 || 796 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 797 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > UFS_MAXNAMLEN) { 798 /*return (1); */ 799 printf("First bad\n"); 800 goto bad; 801 } 802 if (ep->d_ino == 0) 803 return (0); 804 for (i = 0; i < namlen; i++) 805 if (ep->d_name[i] == '\0') { 806 /*return (1); */ 807 printf("Second bad\n"); 808 goto bad; 809 } 810 if (ep->d_name[i]) 811 goto bad; 812 return (0); 813 bad: 814 return (1); 815 } 816 817 /* 818 * Construct a new directory entry after a call to namei, using the 819 * parameters that it left in the componentname argument cnp. The 820 * argument ip is the inode to which the new directory entry will refer. 821 */ 822 void 823 ufs_makedirentry(struct inode *ip, struct componentname *cnp, 824 struct direct *newdirp) 825 { 826 u_int namelen; 827 828 namelen = (unsigned)cnp->cn_namelen; 829 KASSERT(namelen <= UFS_MAXNAMLEN, 830 ("ufs_makedirentry: name too long")); 831 newdirp->d_ino = ip->i_number; 832 newdirp->d_namlen = namelen; 833 834 /* Zero out after-name padding */ 835 *(u_int32_t *)(&newdirp->d_name[namelen & ~(DIR_ROUNDUP - 1)]) = 0; 836 837 bcopy(cnp->cn_nameptr, newdirp->d_name, namelen); 838 839 if (!OFSFMT(ITOV(ip))) 840 newdirp->d_type = IFTODT(ip->i_mode); 841 else { 842 newdirp->d_type = 0; 843 # if (BYTE_ORDER == LITTLE_ENDIAN) 844 { u_char tmp = newdirp->d_namlen; 845 newdirp->d_namlen = newdirp->d_type; 846 newdirp->d_type = tmp; } 847 # endif 848 } 849 } 850 851 /* 852 * Write a directory entry after a call to namei, using the parameters 853 * that it left in nameidata. The argument dirp is the new directory 854 * entry contents. Dvp is a pointer to the directory to be written, 855 * which was left locked by namei. Remaining parameters (dp->i_offset, 856 * dp->i_count) indicate how the space for the new entry is to be obtained. 857 * Non-null bp indicates that a directory is being created (for the 858 * soft dependency code). 859 */ 860 int 861 ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp, 862 struct componentname *cnp, struct buf *newdirbp) 863 { 864 struct ucred *cr; 865 struct thread *td; 866 int newentrysize; 867 struct inode *dp; 868 struct buf *bp; 869 u_int dsize; 870 struct direct *ep, *nep; 871 u_int64_t old_isize; 872 int error, ret, blkoff, loc, spacefree, flags, namlen; 873 char *dirbuf; 874 875 td = curthread; /* XXX */ 876 cr = td->td_ucred; 877 878 dp = VTOI(dvp); 879 newentrysize = DIRSIZ(OFSFMT(dvp), dirp); 880 881 if (I_COUNT(dp) == 0) { 882 /* 883 * If dp->i_count is 0, then namei could find no 884 * space in the directory. Here, dp->i_offset will 885 * be on a directory block boundary and we will write the 886 * new entry into a fresh block. 887 */ 888 if (I_OFFSET(dp) & (DIRBLKSIZ - 1)) 889 panic("ufs_direnter: newblk"); 890 flags = BA_CLRBUF; 891 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp)) 892 flags |= IO_SYNC; 893 #ifdef QUOTA 894 if ((error = getinoquota(dp)) != 0) { 895 if (DOINGSOFTDEP(dvp) && newdirbp != NULL) 896 bdwrite(newdirbp); 897 return (error); 898 } 899 #endif 900 old_isize = dp->i_size; 901 vnode_pager_setsize(dvp, (u_long)I_OFFSET(dp) + DIRBLKSIZ); 902 if ((error = UFS_BALLOC(dvp, (off_t)I_OFFSET(dp), DIRBLKSIZ, 903 cr, flags, &bp)) != 0) { 904 if (DOINGSOFTDEP(dvp) && newdirbp != NULL) 905 bdwrite(newdirbp); 906 vnode_pager_setsize(dvp, (u_long)old_isize); 907 return (error); 908 } 909 dp->i_size = I_OFFSET(dp) + DIRBLKSIZ; 910 DIP_SET(dp, i_size, dp->i_size); 911 SET_I_ENDOFF(dp, dp->i_size); 912 UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 913 dirp->d_reclen = DIRBLKSIZ; 914 blkoff = I_OFFSET(dp) & 915 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1); 916 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize); 917 #ifdef UFS_DIRHASH 918 if (dp->i_dirhash != NULL) { 919 ufsdirhash_newblk(dp, I_OFFSET(dp)); 920 ufsdirhash_add(dp, dirp, I_OFFSET(dp)); 921 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff, 922 I_OFFSET(dp)); 923 } 924 #endif 925 if (DOINGSOFTDEP(dvp)) { 926 /* 927 * Ensure that the entire newly allocated block is a 928 * valid directory so that future growth within the 929 * block does not have to ensure that the block is 930 * written before the inode. 931 */ 932 blkoff += DIRBLKSIZ; 933 while (blkoff < bp->b_bcount) { 934 ((struct direct *) 935 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ; 936 blkoff += DIRBLKSIZ; 937 } 938 if (softdep_setup_directory_add(bp, dp, I_OFFSET(dp), 939 dirp->d_ino, newdirbp, 1)) 940 UFS_INODE_SET_FLAG(dp, IN_NEEDSYNC); 941 if (newdirbp) 942 bdwrite(newdirbp); 943 bdwrite(bp); 944 return (UFS_UPDATE(dvp, 0)); 945 } 946 if (DOINGASYNC(dvp)) { 947 bdwrite(bp); 948 return (UFS_UPDATE(dvp, 0)); 949 } 950 error = bwrite(bp); 951 ret = UFS_UPDATE(dvp, 1); 952 if (error == 0) 953 return (ret); 954 return (error); 955 } 956 957 /* 958 * If dp->i_count is non-zero, then namei found space for the new 959 * entry in the range dp->i_offset to dp->i_offset + dp->i_count 960 * in the directory. To use this space, we may have to compact 961 * the entries located there, by copying them together towards the 962 * beginning of the block, leaving the free space in one usable 963 * chunk at the end. 964 */ 965 966 /* 967 * Increase size of directory if entry eats into new space. 968 * This should never push the size past a new multiple of 969 * DIRBLKSIZE. 970 * 971 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 972 */ 973 if (I_OFFSET(dp) + I_COUNT(dp) > dp->i_size) { 974 dp->i_size = I_OFFSET(dp) + I_COUNT(dp); 975 DIP_SET(dp, i_size, dp->i_size); 976 UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_MODIFIED); 977 } 978 /* 979 * Get the block containing the space for the new directory entry. 980 */ 981 error = UFS_BLKATOFF(dvp, (off_t)I_OFFSET(dp), &dirbuf, &bp); 982 if (error) { 983 if (DOINGSOFTDEP(dvp) && newdirbp != NULL) 984 bdwrite(newdirbp); 985 return (error); 986 } 987 /* 988 * Find space for the new entry. In the simple case, the entry at 989 * offset base will have the space. If it does not, then namei 990 * arranged that compacting the region dp->i_offset to 991 * dp->i_offset + dp->i_count would yield the space. 992 */ 993 ep = (struct direct *)dirbuf; 994 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0; 995 spacefree = ep->d_reclen - dsize; 996 for (loc = ep->d_reclen; loc < I_COUNT(dp); ) { 997 nep = (struct direct *)(dirbuf + loc); 998 999 /* Trim the existing slot (NB: dsize may be zero). */ 1000 ep->d_reclen = dsize; 1001 ep = (struct direct *)((char *)ep + dsize); 1002 1003 /* Read nep->d_reclen now as the bcopy() may clobber it. */ 1004 loc += nep->d_reclen; 1005 if (nep->d_ino == 0) { 1006 /* 1007 * A mid-block unused entry. Such entries are 1008 * never created by the kernel, but fsck_ffs 1009 * can create them (and it doesn't fix them). 1010 * 1011 * Add up the free space, and initialise the 1012 * relocated entry since we don't bcopy it. 1013 */ 1014 spacefree += nep->d_reclen; 1015 ep->d_ino = 0; 1016 dsize = 0; 1017 continue; 1018 } 1019 dsize = DIRSIZ(OFSFMT(dvp), nep); 1020 spacefree += nep->d_reclen - dsize; 1021 #ifdef UFS_DIRHASH 1022 if (dp->i_dirhash != NULL) 1023 ufsdirhash_move(dp, nep, 1024 I_OFFSET(dp) + ((char *)nep - dirbuf), 1025 I_OFFSET(dp) + ((char *)ep - dirbuf)); 1026 #endif 1027 if (DOINGSOFTDEP(dvp)) 1028 softdep_change_directoryentry_offset(bp, dp, dirbuf, 1029 (caddr_t)nep, (caddr_t)ep, dsize); 1030 else 1031 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 1032 } 1033 /* 1034 * Here, `ep' points to a directory entry containing `dsize' in-use 1035 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0, 1036 * then the entry is completely unused (dsize == 0). The value 1037 * of ep->d_reclen is always indeterminate. 1038 * 1039 * Update the pointer fields in the previous entry (if any), 1040 * copy in the new entry, and write out the block. 1041 */ 1042 # if (BYTE_ORDER == LITTLE_ENDIAN) 1043 if (OFSFMT(dvp)) 1044 namlen = ep->d_type; 1045 else 1046 namlen = ep->d_namlen; 1047 # else 1048 namlen = ep->d_namlen; 1049 # endif 1050 if (ep->d_ino == 0 || 1051 (ep->d_ino == UFS_WINO && namlen == dirp->d_namlen && 1052 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) { 1053 if (spacefree + dsize < newentrysize) 1054 panic("ufs_direnter: compact1"); 1055 dirp->d_reclen = spacefree + dsize; 1056 } else { 1057 if (spacefree < newentrysize) 1058 panic("ufs_direnter: compact2"); 1059 dirp->d_reclen = spacefree; 1060 ep->d_reclen = dsize; 1061 ep = (struct direct *)((char *)ep + dsize); 1062 } 1063 #ifdef UFS_DIRHASH 1064 if (dp->i_dirhash != NULL && (ep->d_ino == 0 || 1065 dirp->d_reclen == spacefree)) 1066 ufsdirhash_add(dp, dirp, I_OFFSET(dp) + ((char *)ep - dirbuf)); 1067 #endif 1068 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize); 1069 #ifdef UFS_DIRHASH 1070 if (dp->i_dirhash != NULL) 1071 ufsdirhash_checkblock(dp, dirbuf - 1072 (I_OFFSET(dp) & (DIRBLKSIZ - 1)), 1073 rounddown2(I_OFFSET(dp), DIRBLKSIZ)); 1074 #endif 1075 1076 if (DOINGSOFTDEP(dvp)) { 1077 (void) softdep_setup_directory_add(bp, dp, 1078 I_OFFSET(dp) + (caddr_t)ep - dirbuf, 1079 dirp->d_ino, newdirbp, 0); 1080 if (newdirbp != NULL) 1081 bdwrite(newdirbp); 1082 bdwrite(bp); 1083 } else { 1084 if (DOINGASYNC(dvp)) { 1085 bdwrite(bp); 1086 error = 0; 1087 } else { 1088 error = bwrite(bp); 1089 } 1090 } 1091 1092 /* 1093 * If all went well, and the directory can be shortened, 1094 * mark directory inode with the truncation request. 1095 */ 1096 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE | (error == 0 && 1097 I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size ? IN_ENDOFF : 0)); 1098 1099 return (error); 1100 } 1101 1102 /* 1103 * Remove a directory entry after a call to namei, using 1104 * the parameters which it left in nameidata. The entry 1105 * dp->i_offset contains the offset into the directory of the 1106 * entry to be eliminated. The dp->i_count field contains the 1107 * size of the previous record in the directory. If this 1108 * is 0, the first entry is being deleted, so we need only 1109 * zero the inode number to mark the entry as free. If the 1110 * entry is not the first in the directory, we must reclaim 1111 * the space of the now empty record by adding the record size 1112 * to the size of the previous entry. 1113 */ 1114 int 1115 ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir) 1116 { 1117 struct inode *dp; 1118 struct direct *ep, *rep; 1119 struct buf *bp; 1120 off_t offset; 1121 int error; 1122 1123 dp = VTOI(dvp); 1124 1125 /* 1126 * Adjust the link count early so softdep can block if necessary. 1127 */ 1128 if (ip) { 1129 ip->i_effnlink--; 1130 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 1131 if (DOINGSOFTDEP(dvp)) { 1132 softdep_setup_unlink(dp, ip); 1133 } else { 1134 ip->i_nlink--; 1135 DIP_SET(ip, i_nlink, ip->i_nlink); 1136 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 1137 } 1138 } 1139 if (flags & DOWHITEOUT) 1140 offset = I_OFFSET(dp); 1141 else 1142 offset = I_OFFSET(dp) - I_COUNT(dp); 1143 if ((error = UFS_BLKATOFF(dvp, offset, (char **)&ep, &bp)) != 0) { 1144 if (ip) { 1145 ip->i_effnlink++; 1146 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 1147 if (DOINGSOFTDEP(dvp)) { 1148 softdep_change_linkcnt(ip); 1149 } else { 1150 ip->i_nlink++; 1151 DIP_SET(ip, i_nlink, ip->i_nlink); 1152 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 1153 } 1154 } 1155 return (error); 1156 } 1157 if (flags & DOWHITEOUT) { 1158 /* 1159 * Whiteout entry: set d_ino to UFS_WINO. 1160 */ 1161 ep->d_ino = UFS_WINO; 1162 ep->d_type = DT_WHT; 1163 goto out; 1164 } 1165 /* Set 'rep' to the entry being removed. */ 1166 if (I_COUNT(dp) == 0) 1167 rep = ep; 1168 else 1169 rep = (struct direct *)((char *)ep + ep->d_reclen); 1170 #ifdef UFS_DIRHASH 1171 /* 1172 * Remove the dirhash entry. This is complicated by the fact 1173 * that `ep' is the previous entry when dp->i_count != 0. 1174 */ 1175 if (dp->i_dirhash != NULL) 1176 ufsdirhash_remove(dp, rep, I_OFFSET(dp)); 1177 #endif 1178 if (ip && rep->d_ino != ip->i_number) 1179 panic("ufs_dirremove: ip %ju does not match dirent ino %ju\n", 1180 (uintmax_t)ip->i_number, (uintmax_t)rep->d_ino); 1181 /* 1182 * Zero out the file directory entry metadata to reduce disk 1183 * scavenging disclosure. 1184 */ 1185 bzero(&rep->d_name[0], rep->d_namlen); 1186 rep->d_namlen = 0; 1187 rep->d_type = 0; 1188 rep->d_ino = 0; 1189 1190 if (I_COUNT(dp) != 0) { 1191 /* 1192 * Collapse new free space into previous entry. 1193 */ 1194 ep->d_reclen += rep->d_reclen; 1195 rep->d_reclen = 0; 1196 } 1197 #ifdef UFS_DIRHASH 1198 if (dp->i_dirhash != NULL) 1199 ufsdirhash_checkblock(dp, (char *)ep - 1200 ((I_OFFSET(dp) - I_COUNT(dp)) & (DIRBLKSIZ - 1)), 1201 rounddown2(I_OFFSET(dp), DIRBLKSIZ)); 1202 #endif 1203 out: 1204 error = 0; 1205 if (DOINGSOFTDEP(dvp)) { 1206 if (ip) 1207 softdep_setup_remove(bp, dp, ip, isrmdir); 1208 if (softdep_slowdown(dvp)) 1209 error = bwrite(bp); 1210 else 1211 bdwrite(bp); 1212 } else { 1213 if (flags & DOWHITEOUT) 1214 error = bwrite(bp); 1215 else if (DOINGASYNC(dvp)) 1216 bdwrite(bp); 1217 else 1218 error = bwrite(bp); 1219 } 1220 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE); 1221 /* 1222 * If the last named reference to a snapshot goes away, 1223 * drop its snapshot reference so that it will be reclaimed 1224 * when last open reference goes away. 1225 */ 1226 if (ip != NULL && IS_SNAPSHOT(ip) && ip->i_effnlink == 0) 1227 UFS_SNAPGONE(ip); 1228 return (error); 1229 } 1230 1231 /* 1232 * Rewrite an existing directory entry to point at the inode 1233 * supplied. The parameters describing the directory entry are 1234 * set up by a call to namei. 1235 */ 1236 int 1237 ufs_dirrewrite(struct inode *dp, struct inode *oip, ino_t newinum, int newtype, 1238 int isrmdir) 1239 { 1240 struct buf *bp; 1241 struct direct *ep; 1242 struct vnode *vdp = ITOV(dp); 1243 int error; 1244 1245 /* 1246 * Drop the link before we lock the buf so softdep can block if 1247 * necessary. 1248 */ 1249 oip->i_effnlink--; 1250 UFS_INODE_SET_FLAG(oip, IN_CHANGE); 1251 if (DOINGSOFTDEP(vdp)) { 1252 softdep_setup_unlink(dp, oip); 1253 } else { 1254 oip->i_nlink--; 1255 DIP_SET(oip, i_nlink, oip->i_nlink); 1256 UFS_INODE_SET_FLAG(oip, IN_CHANGE); 1257 } 1258 1259 error = UFS_BLKATOFF(vdp, (off_t)I_OFFSET(dp), (char **)&ep, &bp); 1260 if (error == 0 && ep->d_namlen == 2 && ep->d_name[1] == '.' && 1261 ep->d_name[0] == '.' && ep->d_ino != oip->i_number) { 1262 brelse(bp); 1263 error = EIDRM; 1264 } 1265 if (error) { 1266 oip->i_effnlink++; 1267 UFS_INODE_SET_FLAG(oip, IN_CHANGE); 1268 if (DOINGSOFTDEP(vdp)) { 1269 softdep_change_linkcnt(oip); 1270 } else { 1271 oip->i_nlink++; 1272 DIP_SET(oip, i_nlink, oip->i_nlink); 1273 UFS_INODE_SET_FLAG(oip, IN_CHANGE); 1274 } 1275 return (error); 1276 } 1277 ep->d_ino = newinum; 1278 if (!OFSFMT(vdp)) 1279 ep->d_type = newtype; 1280 if (DOINGSOFTDEP(vdp)) { 1281 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir); 1282 bdwrite(bp); 1283 } else { 1284 if (DOINGASYNC(vdp)) { 1285 bdwrite(bp); 1286 error = 0; 1287 } else { 1288 error = bwrite(bp); 1289 } 1290 } 1291 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE); 1292 /* 1293 * If the last named reference to a snapshot goes away, 1294 * drop its snapshot reference so that it will be reclaimed 1295 * when last open reference goes away. 1296 */ 1297 if (IS_SNAPSHOT(oip) && oip->i_effnlink == 0) 1298 UFS_SNAPGONE(oip); 1299 return (error); 1300 } 1301 1302 /* 1303 * Check if a directory is empty or not. 1304 * Inode supplied must be locked. 1305 * 1306 * Using a struct dirtemplate here is not precisely 1307 * what we want, but better than using a struct direct. 1308 * 1309 * NB: does not handle corrupted directories. 1310 */ 1311 int 1312 ufs_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred) 1313 { 1314 doff_t off; 1315 struct dirtemplate dbuf; 1316 struct direct *dp = (struct direct *)&dbuf; 1317 int error, namlen; 1318 ssize_t count; 1319 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 1320 1321 for (off = 0; off < ip->i_size; off += dp->d_reclen) { 1322 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, 1323 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred, 1324 NOCRED, &count, (struct thread *)0); 1325 /* 1326 * Since we read MINDIRSIZ, residual must 1327 * be 0 unless we're at end of file. 1328 */ 1329 if (error || count != 0) 1330 return (0); 1331 /* avoid infinite loops */ 1332 if (dp->d_reclen == 0) 1333 return (0); 1334 /* skip empty entries */ 1335 if (dp->d_ino == 0 || dp->d_ino == UFS_WINO) 1336 continue; 1337 /* accept only "." and ".." */ 1338 # if (BYTE_ORDER == LITTLE_ENDIAN) 1339 if (OFSFMT(ITOV(ip))) 1340 namlen = dp->d_type; 1341 else 1342 namlen = dp->d_namlen; 1343 # else 1344 namlen = dp->d_namlen; 1345 # endif 1346 if (namlen > 2) 1347 return (0); 1348 if (dp->d_name[0] != '.') 1349 return (0); 1350 /* 1351 * At this point namlen must be 1 or 2. 1352 * 1 implies ".", 2 implies ".." if second 1353 * char is also "." 1354 */ 1355 if (namlen == 1 && dp->d_ino == ip->i_number) 1356 continue; 1357 if (dp->d_name[1] == '.' && dp->d_ino == parentino) 1358 continue; 1359 return (0); 1360 } 1361 return (1); 1362 } 1363 1364 static int 1365 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino, 1366 struct vnode **dd_vp) 1367 { 1368 struct dirtemplate dirbuf; 1369 struct vnode *ddvp; 1370 int error, namlen; 1371 1372 ASSERT_VOP_LOCKED(vp, "ufs_dir_dd_ino"); 1373 *dd_vp = NULL; 1374 if (vp->v_type != VDIR) 1375 return (ENOTDIR); 1376 /* 1377 * First check to see if we have it in the name cache. 1378 */ 1379 if ((ddvp = vn_dir_dd_ino(vp)) != NULL) { 1380 KASSERT(ddvp->v_mount == vp->v_mount, 1381 ("ufs_dir_dd_ino: Unexpected mount point crossing")); 1382 *dd_ino = VTOI(ddvp)->i_number; 1383 *dd_vp = ddvp; 1384 return (0); 1385 } 1386 /* 1387 * Have to read the directory. 1388 */ 1389 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 1390 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, 1391 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL); 1392 if (error != 0) 1393 return (error); 1394 #if (BYTE_ORDER == LITTLE_ENDIAN) 1395 if (OFSFMT(vp)) 1396 namlen = dirbuf.dotdot_type; 1397 else 1398 namlen = dirbuf.dotdot_namlen; 1399 #else 1400 namlen = dirbuf.dotdot_namlen; 1401 #endif 1402 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' || 1403 dirbuf.dotdot_name[1] != '.') 1404 return (ENOTDIR); 1405 *dd_ino = dirbuf.dotdot_ino; 1406 return (0); 1407 } 1408 1409 /* 1410 * Check if source directory is in the path of the target directory. 1411 */ 1412 int 1413 ufs_checkpath(ino_t source_ino, ino_t parent_ino, struct inode *target, 1414 struct ucred *cred, ino_t *wait_ino) 1415 { 1416 struct mount *mp; 1417 struct vnode *tvp, *vp, *vp1; 1418 int error; 1419 ino_t dd_ino; 1420 1421 vp = tvp = ITOV(target); 1422 mp = vp->v_mount; 1423 *wait_ino = 0; 1424 sx_assert(&VFSTOUFS(mp)->um_checkpath_lock, SA_XLOCKED); 1425 1426 if (target->i_number == source_ino) 1427 return (EEXIST); 1428 if (target->i_number == parent_ino) 1429 return (0); 1430 if (target->i_number == UFS_ROOTINO) 1431 return (0); 1432 for (;;) { 1433 error = ufs_dir_dd_ino(vp, cred, &dd_ino, &vp1); 1434 if (error != 0) 1435 break; 1436 if (dd_ino == source_ino) { 1437 error = EINVAL; 1438 break; 1439 } 1440 if (dd_ino == UFS_ROOTINO) 1441 break; 1442 if (dd_ino == parent_ino) 1443 break; 1444 if (vp1 == NULL) { 1445 error = VFS_VGET(mp, dd_ino, LK_SHARED | LK_NOWAIT, 1446 &vp1); 1447 if (error != 0) { 1448 *wait_ino = dd_ino; 1449 break; 1450 } 1451 } 1452 KASSERT(dd_ino == VTOI(vp1)->i_number, 1453 ("directory %ju reparented\n", 1454 (uintmax_t)VTOI(vp1)->i_number)); 1455 if (vp != tvp) 1456 vput(vp); 1457 vp = vp1; 1458 } 1459 1460 if (error == ENOTDIR) 1461 panic("checkpath: .. not a directory\n"); 1462 if (vp1 != NULL) 1463 vput(vp1); 1464 if (vp != tvp) 1465 vput(vp); 1466 return (error); 1467 } 1468 1469 #ifdef DIAGNOSTIC 1470 static void 1471 ufs_assert_inode_offset_owner(struct inode *ip, struct iown_tracker *tr, 1472 const char *name, const char *file, int line) 1473 { 1474 char msg[128]; 1475 1476 snprintf(msg, sizeof(msg), "at %s@%d", file, line); 1477 ASSERT_VOP_ELOCKED(ITOV(ip), msg); 1478 MPASS((ip->i_mode & IFMT) == IFDIR); 1479 if (curthread == tr->tr_owner && ip->i_lock_gen == tr->tr_gen) 1480 return; 1481 printf("locked at\n"); 1482 stack_print(&tr->tr_st); 1483 printf("unlocked at\n"); 1484 stack_print(&tr->tr_unlock); 1485 panic("%s ip %p %jd offset owner %p %d gen %d " 1486 "curthread %p %d gen %d at %s@%d\n", 1487 name, ip, (uintmax_t)ip->i_number, tr->tr_owner, 1488 tr->tr_owner->td_tid, tr->tr_gen, 1489 curthread, curthread->td_tid, ip->i_lock_gen, 1490 file, line); 1491 } 1492 1493 static void 1494 ufs_set_inode_offset_owner(struct inode *ip, struct iown_tracker *tr, 1495 const char *file, int line) 1496 { 1497 char msg[128]; 1498 1499 snprintf(msg, sizeof(msg), "at %s@%d", file, line); 1500 ASSERT_VOP_ELOCKED(ITOV(ip), msg); 1501 MPASS((ip->i_mode & IFMT) == IFDIR); 1502 tr->tr_owner = curthread; 1503 tr->tr_gen = ip->i_lock_gen; 1504 stack_save(&tr->tr_st); 1505 } 1506 1507 static void 1508 ufs_init_one_tracker(struct iown_tracker *tr) 1509 { 1510 tr->tr_owner = NULL; 1511 stack_zero(&tr->tr_st); 1512 } 1513 1514 void 1515 ufs_init_trackers(struct inode *ip) 1516 { 1517 ufs_init_one_tracker(&ip->i_offset_tracker); 1518 ufs_init_one_tracker(&ip->i_count_tracker); 1519 ufs_init_one_tracker(&ip->i_endoff_tracker); 1520 } 1521 1522 void 1523 ufs_unlock_tracker(struct inode *ip) 1524 { 1525 if (ip->i_count_tracker.tr_gen == ip->i_lock_gen) 1526 stack_save(&ip->i_count_tracker.tr_unlock); 1527 if (ip->i_offset_tracker.tr_gen == ip->i_lock_gen) 1528 stack_save(&ip->i_offset_tracker.tr_unlock); 1529 if (ip->i_endoff_tracker.tr_gen == ip->i_lock_gen) 1530 stack_save(&ip->i_endoff_tracker.tr_unlock); 1531 ip->i_lock_gen++; 1532 } 1533 1534 doff_t 1535 ufs_get_i_offset(struct inode *ip, const char *file, int line) 1536 { 1537 ufs_assert_inode_offset_owner(ip, &ip->i_offset_tracker, "i_offset", 1538 file, line); 1539 return (ip->i_offset); 1540 } 1541 1542 void 1543 ufs_set_i_offset(struct inode *ip, doff_t off, const char *file, int line) 1544 { 1545 ufs_set_inode_offset_owner(ip, &ip->i_offset_tracker, file, line); 1546 ip->i_offset = off; 1547 } 1548 1549 int32_t 1550 ufs_get_i_count(struct inode *ip, const char *file, int line) 1551 { 1552 ufs_assert_inode_offset_owner(ip, &ip->i_count_tracker, "i_count", 1553 file, line); 1554 return (ip->i_count); 1555 } 1556 1557 void 1558 ufs_set_i_count(struct inode *ip, int32_t cnt, const char *file, int line) 1559 { 1560 ufs_set_inode_offset_owner(ip, &ip->i_count_tracker, file, line); 1561 ip->i_count = cnt; 1562 } 1563 1564 doff_t 1565 ufs_get_i_endoff(struct inode *ip, const char *file, int line) 1566 { 1567 ufs_assert_inode_offset_owner(ip, &ip->i_endoff_tracker, "i_endoff", 1568 file, line); 1569 return (ip->i_endoff); 1570 } 1571 1572 void 1573 ufs_set_i_endoff(struct inode *ip, doff_t off, const char *file, int line) 1574 { 1575 ufs_set_inode_offset_owner(ip, &ip->i_endoff_tracker, file, line); 1576 ip->i_endoff = off; 1577 } 1578 1579 #endif 1580