ufs_lookup.c revision 306180
1259208Sjmmv/*- 2259208Sjmmv * Copyright (c) 1989, 1993 3264483Sjmmv * The Regents of the University of California. All rights reserved. 4264483Sjmmv * (c) UNIX System Laboratories, Inc. 5259208Sjmmv * All or some portions of this file are derived from material licensed 6259208Sjmmv * to the University of California by American Telephone and Telegraph 7259208Sjmmv * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8259208Sjmmv * the permission of UNIX System Laboratories, Inc. 9259208Sjmmv * 10259208Sjmmv * Redistribution and use in source and binary forms, with or without 11264483Sjmmv * modification, are permitted provided that the following conditions 12264483Sjmmv * are met: 13264483Sjmmv * 1. Redistributions of source code must retain the above copyright 14259208Sjmmv * notice, this list of conditions and the following disclaimer. 15259208Sjmmv * 2. Redistributions in binary form must reproduce the above copyright 16259208Sjmmv * notice, this list of conditions and the following disclaimer in the 17259208Sjmmv * documentation and/or other materials provided with the distribution. 18259208Sjmmv * 4. Neither the name of the University nor the names of its contributors 19259208Sjmmv * may be used to endorse or promote products derived from this software 20259208Sjmmv * without specific prior written permission. 21259208Sjmmv * 22259208Sjmmv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23259208Sjmmv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24259208Sjmmv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25262852Sjmmv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26259208Sjmmv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27259208Sjmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28262852Sjmmv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29292812Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30262852Sjmmv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31259208Sjmmv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32259208Sjmmv * SUCH DAMAGE. 33259208Sjmmv * 34259208Sjmmv * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95 35259208Sjmmv */ 36259208Sjmmv 37259208Sjmmv#include <sys/cdefs.h> 38259208Sjmmv__FBSDID("$FreeBSD: stable/10/sys/ufs/ufs/ufs_lookup.c 306180 2016-09-22 10:51:47Z kib $"); 39259208Sjmmv 40259208Sjmmv#include "opt_ufs.h" 41259208Sjmmv#include "opt_quota.h" 42259208Sjmmv 43259208Sjmmv#include <sys/param.h> 44259208Sjmmv#include <sys/systm.h> 45259208Sjmmv#include <sys/kernel.h> 46259208Sjmmv#include <sys/namei.h> 47259208Sjmmv#include <sys/bio.h> 48259208Sjmmv#include <sys/buf.h> 49259208Sjmmv#include <sys/proc.h> 50259208Sjmmv#include <sys/stat.h> 51259208Sjmmv#include <sys/mount.h> 52259208Sjmmv#include <sys/vnode.h> 53262852Sjmmv#include <sys/sysctl.h> 54262852Sjmmv 55262852Sjmmv#include <vm/vm.h> 56262852Sjmmv#include <vm/vm_extern.h> 57262852Sjmmv 58262852Sjmmv#include <ufs/ufs/extattr.h> 59262852Sjmmv#include <ufs/ufs/quota.h> 60262852Sjmmv#include <ufs/ufs/inode.h> 61262852Sjmmv#include <ufs/ufs/dir.h> 62262852Sjmmv#ifdef UFS_DIRHASH 63262852Sjmmv#include <ufs/ufs/dirhash.h> 64262852Sjmmv#endif 65262852Sjmmv#include <ufs/ufs/ufsmount.h> 66262852Sjmmv#include <ufs/ufs/ufs_extern.h> 67262852Sjmmv 68262852Sjmmv#ifdef DIAGNOSTIC 69271298Sngiestatic int dirchk = 1; 70262852Sjmmv#else 71262852Sjmmvstatic int dirchk = 0; 72262852Sjmmv#endif 73262852Sjmmv 74262852SjmmvSYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, ""); 75262852Sjmmv 76259208Sjmmv/* true if old FS format...*/ 77259208Sjmmv#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 78259208Sjmmv 79259208Sjmmvstatic int 80259208Sjmmvufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred, 81259208Sjmmv struct thread *td) 82259208Sjmmv{ 83259208Sjmmv int error; 84259208Sjmmv 85259208Sjmmv#ifdef UFS_ACL 86259208Sjmmv /* 87259208Sjmmv * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt 88259208Sjmmv * 89276423Sngie * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD 90276423Sngie */ 91276423Sngie 92259208Sjmmv /* 93276423Sngie * XXX: Is this check required? 94259208Sjmmv */ 95259208Sjmmv error = VOP_ACCESS(vdp, VEXEC, cred, td); 96259208Sjmmv if (error) 97259208Sjmmv 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 */ 179int 180ufs_lookup(ap) 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 191int 192ufs_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), cnp->cn_thread); 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 257restart: 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 344searchloop: 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 433foundentry: 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 (vdp->v_mount->mnt_maxsymlinklen > 0 && 441 ep->d_type == DT_WHT) { 442 slotstatus = FOUND; 443 slotoffset = i_offset; 444 slotsize = ep->d_reclen; 445 enduseful = dp->i_size; 446 cnp->cn_flags |= ISWHITEOUT; 447 numdirpasses--; 448 goto notfound; 449 } 450 ino = ep->d_ino; 451 goto found; 452 } 453 } 454 prevoff = i_offset; 455 i_offset += ep->d_reclen; 456 entryoffsetinblock += ep->d_reclen; 457 if (ep->d_ino) 458 enduseful = i_offset; 459 } 460notfound: 461 /* 462 * If we started in the middle of the directory and failed 463 * to find our target, we must check the beginning as well. 464 */ 465 if (numdirpasses == 2) { 466 numdirpasses--; 467 i_offset = 0; 468 endsearch = i_diroff; 469 goto searchloop; 470 } 471 if (bp != NULL) 472 brelse(bp); 473 /* 474 * If creating, and at end of pathname and current 475 * directory has not been removed, then can consider 476 * allowing file to be created. 477 */ 478 if ((nameiop == CREATE || nameiop == RENAME || 479 (nameiop == DELETE && 480 (cnp->cn_flags & DOWHITEOUT) && 481 (cnp->cn_flags & ISWHITEOUT))) && 482 (flags & ISLASTCN) && dp->i_effnlink != 0) { 483 /* 484 * Access for write is interpreted as allowing 485 * creation of files in the directory. 486 * 487 * XXX: Fix the comment above. 488 */ 489 if (flags & WILLBEDIR) 490 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread); 491 else 492 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread); 493 if (error) 494 return (error); 495 /* 496 * Return an indication of where the new directory 497 * entry should be put. If we didn't find a slot, 498 * then set dp->i_count to 0 indicating 499 * that the new slot belongs at the end of the 500 * directory. If we found a slot, then the new entry 501 * can be put in the range from dp->i_offset to 502 * dp->i_offset + dp->i_count. 503 */ 504 if (slotstatus == NONE) { 505 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ); 506 dp->i_count = 0; 507 enduseful = dp->i_offset; 508 } else if (nameiop == DELETE) { 509 dp->i_offset = slotoffset; 510 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 511 dp->i_count = 0; 512 else 513 dp->i_count = dp->i_offset - prevoff; 514 } else { 515 dp->i_offset = slotoffset; 516 dp->i_count = slotsize; 517 if (enduseful < slotoffset + slotsize) 518 enduseful = slotoffset + slotsize; 519 } 520 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ); 521 /* 522 * We return with the directory locked, so that 523 * the parameters we set up above will still be 524 * valid if we actually decide to do a direnter(). 525 * We return ni_vp == NULL to indicate that the entry 526 * does not currently exist; we leave a pointer to 527 * the (locked) directory inode in ndp->ni_dvp. 528 * The pathname buffer is saved so that the name 529 * can be obtained later. 530 * 531 * NB - if the directory is unlocked, then this 532 * information cannot be used. 533 */ 534 cnp->cn_flags |= SAVENAME; 535 return (EJUSTRETURN); 536 } 537 /* 538 * Insert name into cache (as non-existent) if appropriate. 539 */ 540 if ((cnp->cn_flags & MAKEENTRY) != 0) 541 cache_enter(vdp, NULL, cnp); 542 return (ENOENT); 543 544found: 545 if (dd_ino != NULL) 546 *dd_ino = ino; 547 if (numdirpasses == 2) 548 nchstats.ncs_pass2++; 549 /* 550 * Check that directory length properly reflects presence 551 * of this entry. 552 */ 553 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) { 554 ufs_dirbad(dp, i_offset, "i_size too small"); 555 dp->i_size = i_offset + DIRSIZ(OFSFMT(vdp), ep); 556 DIP_SET(dp, i_size, dp->i_size); 557 dp->i_flag |= IN_CHANGE | IN_UPDATE; 558 } 559 brelse(bp); 560 561 /* 562 * Found component in pathname. 563 * If the final component of path name, save information 564 * in the cache as to where the entry was found. 565 */ 566 if ((flags & ISLASTCN) && nameiop == LOOKUP) 567 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1); 568 569 /* 570 * If deleting, and at end of pathname, return 571 * parameters which can be used to remove file. 572 */ 573 if (nameiop == DELETE && (flags & ISLASTCN)) { 574 if (flags & LOCKPARENT) 575 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__); 576 /* 577 * Return pointer to current entry in dp->i_offset, 578 * and distance past previous entry (if there 579 * is a previous entry in this block) in dp->i_count. 580 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 581 * 582 * Technically we shouldn't be setting these in the 583 * WANTPARENT case (first lookup in rename()), but any 584 * lookups that will result in directory changes will 585 * overwrite these. 586 */ 587 dp->i_offset = i_offset; 588 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 589 dp->i_count = 0; 590 else 591 dp->i_count = dp->i_offset - prevoff; 592 if (dd_ino != NULL) 593 return (0); 594 if ((error = VFS_VGET(vdp->v_mount, ino, 595 LK_EXCLUSIVE, &tdp)) != 0) 596 return (error); 597 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread); 598 if (error) { 599 vput(tdp); 600 return (error); 601 } 602 if (dp->i_number == ino) { 603 VREF(vdp); 604 *vpp = vdp; 605 vput(tdp); 606 return (0); 607 } 608 609 *vpp = tdp; 610 return (0); 611 } 612 613 /* 614 * If rewriting (RENAME), return the inode and the 615 * information required to rewrite the present directory 616 * Must get inode of directory entry to verify it's a 617 * regular file, or empty directory. 618 */ 619 if (nameiop == RENAME && (flags & ISLASTCN)) { 620 if (flags & WILLBEDIR) 621 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread); 622 else 623 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread); 624 if (error) 625 return (error); 626 /* 627 * Careful about locking second inode. 628 * This can only occur if the target is ".". 629 */ 630 dp->i_offset = i_offset; 631 if (dp->i_number == ino) 632 return (EISDIR); 633 if (dd_ino != NULL) 634 return (0); 635 if ((error = VFS_VGET(vdp->v_mount, ino, 636 LK_EXCLUSIVE, &tdp)) != 0) 637 return (error); 638 639 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread); 640 if (error) { 641 vput(tdp); 642 return (error); 643 } 644 645#ifdef SunOS_doesnt_do_that 646 /* 647 * The only purpose of this check is to return the correct 648 * error. Assume that we want to rename directory "a" 649 * to a file "b", and that we have no ACL_WRITE_DATA on 650 * a containing directory, but we _do_ have ACL_APPEND_DATA. 651 * In that case, the VOP_ACCESS check above will return 0, 652 * and the operation will fail with ENOTDIR instead 653 * of EACCESS. 654 */ 655 if (tdp->v_type == VDIR) 656 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread); 657 else 658 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread); 659 if (error) { 660 vput(tdp); 661 return (error); 662 } 663#endif 664 665 *vpp = tdp; 666 cnp->cn_flags |= SAVENAME; 667 return (0); 668 } 669 if (dd_ino != NULL) 670 return (0); 671 672 /* 673 * Step through the translation in the name. We do not `vput' the 674 * directory because we may need it again if a symbolic link 675 * is relative to the current directory. Instead we save it 676 * unlocked as "pdp". We must get the target inode before unlocking 677 * the directory to insure that the inode will not be removed 678 * before we get it. We prevent deadlock by always fetching 679 * inodes from the root, moving down the directory tree. Thus 680 * when following backward pointers ".." we must unlock the 681 * parent directory before getting the requested directory. 682 * There is a potential race condition here if both the current 683 * and parent directories are removed before the VFS_VGET for the 684 * inode associated with ".." returns. We hope that this occurs 685 * infrequently since we cannot avoid this race condition without 686 * implementing a sophisticated deadlock detection algorithm. 687 * Note also that this simple deadlock detection scheme will not 688 * work if the filesystem has any hard links other than ".." 689 * that point backwards in the directory structure. 690 */ 691 pdp = vdp; 692 if (flags & ISDOTDOT) { 693 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp); 694 if (error) 695 return (error); 696 697 /* 698 * Recheck that ".." entry in the vdp directory points 699 * to the inode we looked up before vdp lock was 700 * dropped. 701 */ 702 error = ufs_lookup_ino(pdp, NULL, cnp, &ino1); 703 if (error) { 704 vput(tdp); 705 return (error); 706 } 707 if (ino1 != ino) { 708 vput(tdp); 709 goto restart; 710 } 711 712 *vpp = tdp; 713 } else if (dp->i_number == ino) { 714 VREF(vdp); /* we want ourself, ie "." */ 715 /* 716 * When we lookup "." we still can be asked to lock it 717 * differently. 718 */ 719 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 720 if (ltype != VOP_ISLOCKED(vdp)) { 721 if (ltype == LK_EXCLUSIVE) 722 vn_lock(vdp, LK_UPGRADE | LK_RETRY); 723 else /* if (ltype == LK_SHARED) */ 724 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY); 725 /* 726 * Relock for the "." case may left us with 727 * reclaimed vnode. 728 */ 729 if (vdp->v_iflag & VI_DOOMED) { 730 vrele(vdp); 731 return (ENOENT); 732 } 733 } 734 *vpp = vdp; 735 } else { 736 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp); 737 if (error) 738 return (error); 739 *vpp = tdp; 740 } 741 742 /* 743 * Insert name into cache if appropriate. 744 */ 745 if (cnp->cn_flags & MAKEENTRY) 746 cache_enter(vdp, *vpp, cnp); 747 return (0); 748} 749 750void 751ufs_dirbad(ip, offset, how) 752 struct inode *ip; 753 doff_t offset; 754 char *how; 755{ 756 struct mount *mp; 757 758 mp = ITOV(ip)->v_mount; 759 if ((mp->mnt_flag & MNT_RDONLY) == 0) 760 panic("ufs_dirbad: %s: bad dir ino %ju at offset %ld: %s", 761 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number, 762 (long)offset, how); 763 else 764 (void)printf("%s: bad dir ino %ju at offset %ld: %s\n", 765 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number, 766 (long)offset, how); 767} 768 769/* 770 * Do consistency checking on a directory entry: 771 * record length must be multiple of 4 772 * entry must fit in rest of its DIRBLKSIZ block 773 * record must be large enough to contain entry 774 * name is not longer than MAXNAMLEN 775 * name must be as long as advertised, and null terminated 776 */ 777int 778ufs_dirbadentry(dp, ep, entryoffsetinblock) 779 struct vnode *dp; 780 struct direct *ep; 781 int entryoffsetinblock; 782{ 783 int i, namlen; 784 785# if (BYTE_ORDER == LITTLE_ENDIAN) 786 if (OFSFMT(dp)) 787 namlen = ep->d_type; 788 else 789 namlen = ep->d_namlen; 790# else 791 namlen = ep->d_namlen; 792# endif 793 if ((ep->d_reclen & 0x3) != 0 || 794 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 795 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) { 796 /*return (1); */ 797 printf("First bad\n"); 798 goto bad; 799 } 800 if (ep->d_ino == 0) 801 return (0); 802 for (i = 0; i < namlen; i++) 803 if (ep->d_name[i] == '\0') { 804 /*return (1); */ 805 printf("Second bad\n"); 806 goto bad; 807 } 808 if (ep->d_name[i]) 809 goto bad; 810 return (0); 811bad: 812 return (1); 813} 814 815/* 816 * Construct a new directory entry after a call to namei, using the 817 * parameters that it left in the componentname argument cnp. The 818 * argument ip is the inode to which the new directory entry will refer. 819 */ 820void 821ufs_makedirentry(ip, cnp, newdirp) 822 struct inode *ip; 823 struct componentname *cnp; 824 struct direct *newdirp; 825{ 826 827#ifdef INVARIANTS 828 if ((cnp->cn_flags & SAVENAME) == 0) 829 panic("ufs_makedirentry: missing name"); 830#endif 831 newdirp->d_ino = ip->i_number; 832 newdirp->d_namlen = cnp->cn_namelen; 833 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1); 834 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0) 835 newdirp->d_type = IFTODT(ip->i_mode); 836 else { 837 newdirp->d_type = 0; 838# if (BYTE_ORDER == LITTLE_ENDIAN) 839 { u_char tmp = newdirp->d_namlen; 840 newdirp->d_namlen = newdirp->d_type; 841 newdirp->d_type = tmp; } 842# endif 843 } 844} 845 846/* 847 * Write a directory entry after a call to namei, using the parameters 848 * that it left in nameidata. The argument dirp is the new directory 849 * entry contents. Dvp is a pointer to the directory to be written, 850 * which was left locked by namei. Remaining parameters (dp->i_offset, 851 * dp->i_count) indicate how the space for the new entry is to be obtained. 852 * Non-null bp indicates that a directory is being created (for the 853 * soft dependency code). 854 */ 855int 856ufs_direnter(dvp, tvp, dirp, cnp, newdirbp, isrename) 857 struct vnode *dvp; 858 struct vnode *tvp; 859 struct direct *dirp; 860 struct componentname *cnp; 861 struct buf *newdirbp; 862 int isrename; 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 (dp->i_count == 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 (dp->i_offset & (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)dp->i_offset + DIRBLKSIZ); 902 if ((error = UFS_BALLOC(dvp, (off_t)dp->i_offset, 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 = dp->i_offset + DIRBLKSIZ; 910 DIP_SET(dp, i_size, dp->i_size); 911 dp->i_endoff = dp->i_size; 912 dp->i_flag |= IN_CHANGE | IN_UPDATE; 913 dirp->d_reclen = DIRBLKSIZ; 914 blkoff = dp->i_offset & 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, dp->i_offset); 920 ufsdirhash_add(dp, dirp, dp->i_offset); 921 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff, 922 dp->i_offset); 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, dp->i_offset, 939 dirp->d_ino, newdirbp, 1)) 940 dp->i_flag |= IN_NEEDSYNC; 941 if (newdirbp) 942 bdwrite(newdirbp); 943 bdwrite(bp); 944 if ((dp->i_flag & IN_NEEDSYNC) == 0) 945 return (UFS_UPDATE(dvp, 0)); 946 /* 947 * We have just allocated a directory block in an 948 * indirect block. We must prevent holes in the 949 * directory created if directory entries are 950 * written out of order. To accomplish this we 951 * fsync when we extend a directory into indirects. 952 * During rename it's not safe to drop the tvp lock 953 * so sync must be delayed until it is. 954 * 955 * This synchronous step could be removed if fsck and 956 * the kernel were taught to fill in sparse 957 * directories rather than panic. 958 */ 959 if (isrename) 960 return (0); 961 if (tvp != NULL) 962 VOP_UNLOCK(tvp, 0); 963 (void) VOP_FSYNC(dvp, MNT_WAIT, td); 964 if (tvp != NULL) 965 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY); 966 return (error); 967 } 968 if (DOINGASYNC(dvp)) { 969 bdwrite(bp); 970 return (UFS_UPDATE(dvp, 0)); 971 } 972 error = bwrite(bp); 973 ret = UFS_UPDATE(dvp, 1); 974 if (error == 0) 975 return (ret); 976 return (error); 977 } 978 979 /* 980 * If dp->i_count is non-zero, then namei found space for the new 981 * entry in the range dp->i_offset to dp->i_offset + dp->i_count 982 * in the directory. To use this space, we may have to compact 983 * the entries located there, by copying them together towards the 984 * beginning of the block, leaving the free space in one usable 985 * chunk at the end. 986 */ 987 988 /* 989 * Increase size of directory if entry eats into new space. 990 * This should never push the size past a new multiple of 991 * DIRBLKSIZE. 992 * 993 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 994 */ 995 if (dp->i_offset + dp->i_count > dp->i_size) { 996 dp->i_size = dp->i_offset + dp->i_count; 997 DIP_SET(dp, i_size, dp->i_size); 998 } 999 /* 1000 * Get the block containing the space for the new directory entry. 1001 */ 1002 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp); 1003 if (error) { 1004 if (DOINGSOFTDEP(dvp) && newdirbp != NULL) 1005 bdwrite(newdirbp); 1006 return (error); 1007 } 1008 /* 1009 * Find space for the new entry. In the simple case, the entry at 1010 * offset base will have the space. If it does not, then namei 1011 * arranged that compacting the region dp->i_offset to 1012 * dp->i_offset + dp->i_count would yield the space. 1013 */ 1014 ep = (struct direct *)dirbuf; 1015 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0; 1016 spacefree = ep->d_reclen - dsize; 1017 for (loc = ep->d_reclen; loc < dp->i_count; ) { 1018 nep = (struct direct *)(dirbuf + loc); 1019 1020 /* Trim the existing slot (NB: dsize may be zero). */ 1021 ep->d_reclen = dsize; 1022 ep = (struct direct *)((char *)ep + dsize); 1023 1024 /* Read nep->d_reclen now as the bcopy() may clobber it. */ 1025 loc += nep->d_reclen; 1026 if (nep->d_ino == 0) { 1027 /* 1028 * A mid-block unused entry. Such entries are 1029 * never created by the kernel, but fsck_ffs 1030 * can create them (and it doesn't fix them). 1031 * 1032 * Add up the free space, and initialise the 1033 * relocated entry since we don't bcopy it. 1034 */ 1035 spacefree += nep->d_reclen; 1036 ep->d_ino = 0; 1037 dsize = 0; 1038 continue; 1039 } 1040 dsize = DIRSIZ(OFSFMT(dvp), nep); 1041 spacefree += nep->d_reclen - dsize; 1042#ifdef UFS_DIRHASH 1043 if (dp->i_dirhash != NULL) 1044 ufsdirhash_move(dp, nep, 1045 dp->i_offset + ((char *)nep - dirbuf), 1046 dp->i_offset + ((char *)ep - dirbuf)); 1047#endif 1048 if (DOINGSOFTDEP(dvp)) 1049 softdep_change_directoryentry_offset(bp, dp, dirbuf, 1050 (caddr_t)nep, (caddr_t)ep, dsize); 1051 else 1052 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 1053 } 1054 /* 1055 * Here, `ep' points to a directory entry containing `dsize' in-use 1056 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0, 1057 * then the entry is completely unused (dsize == 0). The value 1058 * of ep->d_reclen is always indeterminate. 1059 * 1060 * Update the pointer fields in the previous entry (if any), 1061 * copy in the new entry, and write out the block. 1062 */ 1063# if (BYTE_ORDER == LITTLE_ENDIAN) 1064 if (OFSFMT(dvp)) 1065 namlen = ep->d_type; 1066 else 1067 namlen = ep->d_namlen; 1068# else 1069 namlen = ep->d_namlen; 1070# endif 1071 if (ep->d_ino == 0 || 1072 (ep->d_ino == WINO && namlen == dirp->d_namlen && 1073 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) { 1074 if (spacefree + dsize < newentrysize) 1075 panic("ufs_direnter: compact1"); 1076 dirp->d_reclen = spacefree + dsize; 1077 } else { 1078 if (spacefree < newentrysize) 1079 panic("ufs_direnter: compact2"); 1080 dirp->d_reclen = spacefree; 1081 ep->d_reclen = dsize; 1082 ep = (struct direct *)((char *)ep + dsize); 1083 } 1084#ifdef UFS_DIRHASH 1085 if (dp->i_dirhash != NULL && (ep->d_ino == 0 || 1086 dirp->d_reclen == spacefree)) 1087 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf)); 1088#endif 1089 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize); 1090#ifdef UFS_DIRHASH 1091 if (dp->i_dirhash != NULL) 1092 ufsdirhash_checkblock(dp, dirbuf - 1093 (dp->i_offset & (DIRBLKSIZ - 1)), 1094 dp->i_offset & ~(DIRBLKSIZ - 1)); 1095#endif 1096 1097 if (DOINGSOFTDEP(dvp)) { 1098 (void) softdep_setup_directory_add(bp, dp, 1099 dp->i_offset + (caddr_t)ep - dirbuf, 1100 dirp->d_ino, newdirbp, 0); 1101 if (newdirbp != NULL) 1102 bdwrite(newdirbp); 1103 bdwrite(bp); 1104 } else { 1105 if (DOINGASYNC(dvp)) { 1106 bdwrite(bp); 1107 error = 0; 1108 } else { 1109 error = bwrite(bp); 1110 } 1111 } 1112 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1113 /* 1114 * If all went well, and the directory can be shortened, proceed 1115 * with the truncation. Note that we have to unlock the inode for 1116 * the entry that we just entered, as the truncation may need to 1117 * lock other inodes which can lead to deadlock if we also hold a 1118 * lock on the newly entered node. 1119 */ 1120 if (isrename == 0 && error == 0 && 1121 dp->i_endoff && dp->i_endoff < dp->i_size) { 1122 if (tvp != NULL) 1123 VOP_UNLOCK(tvp, 0); 1124 error = UFS_TRUNCATE(dvp, (off_t)dp->i_endoff, 1125 IO_NORMAL | (DOINGASYNC(dvp) ? 0 : IO_SYNC), cr); 1126 if (error != 0) 1127 vn_printf(dvp, "ufs_direnter: failed to truncate " 1128 "err %d", error); 1129#ifdef UFS_DIRHASH 1130 if (error == 0 && dp->i_dirhash != NULL) 1131 ufsdirhash_dirtrunc(dp, dp->i_endoff); 1132#endif 1133 error = 0; 1134 if (tvp != NULL) 1135 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY); 1136 } 1137 return (error); 1138} 1139 1140/* 1141 * Remove a directory entry after a call to namei, using 1142 * the parameters which it left in nameidata. The entry 1143 * dp->i_offset contains the offset into the directory of the 1144 * entry to be eliminated. The dp->i_count field contains the 1145 * size of the previous record in the directory. If this 1146 * is 0, the first entry is being deleted, so we need only 1147 * zero the inode number to mark the entry as free. If the 1148 * entry is not the first in the directory, we must reclaim 1149 * the space of the now empty record by adding the record size 1150 * to the size of the previous entry. 1151 */ 1152int 1153ufs_dirremove(dvp, ip, flags, isrmdir) 1154 struct vnode *dvp; 1155 struct inode *ip; 1156 int flags; 1157 int isrmdir; 1158{ 1159 struct inode *dp; 1160 struct direct *ep, *rep; 1161 struct buf *bp; 1162 int error; 1163 1164 dp = VTOI(dvp); 1165 1166 /* 1167 * Adjust the link count early so softdep can block if necessary. 1168 */ 1169 if (ip) { 1170 ip->i_effnlink--; 1171 if (DOINGSOFTDEP(dvp)) { 1172 softdep_setup_unlink(dp, ip); 1173 } else { 1174 ip->i_nlink--; 1175 DIP_SET(ip, i_nlink, ip->i_nlink); 1176 ip->i_flag |= IN_CHANGE; 1177 } 1178 } 1179 if (flags & DOWHITEOUT) { 1180 /* 1181 * Whiteout entry: set d_ino to WINO. 1182 */ 1183 if ((error = 1184 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0) 1185 return (error); 1186 ep->d_ino = WINO; 1187 ep->d_type = DT_WHT; 1188 goto out; 1189 } 1190 1191 if ((error = UFS_BLKATOFF(dvp, 1192 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0) 1193 return (error); 1194 1195 /* Set 'rep' to the entry being removed. */ 1196 if (dp->i_count == 0) 1197 rep = ep; 1198 else 1199 rep = (struct direct *)((char *)ep + ep->d_reclen); 1200#ifdef UFS_DIRHASH 1201 /* 1202 * Remove the dirhash entry. This is complicated by the fact 1203 * that `ep' is the previous entry when dp->i_count != 0. 1204 */ 1205 if (dp->i_dirhash != NULL) 1206 ufsdirhash_remove(dp, rep, dp->i_offset); 1207#endif 1208 if (ip && rep->d_ino != ip->i_number) 1209 panic("ufs_dirremove: ip %ju does not match dirent ino %ju\n", 1210 (uintmax_t)ip->i_number, (uintmax_t)rep->d_ino); 1211 if (dp->i_count == 0) { 1212 /* 1213 * First entry in block: set d_ino to zero. 1214 */ 1215 ep->d_ino = 0; 1216 } else { 1217 /* 1218 * Collapse new free space into previous entry. 1219 */ 1220 ep->d_reclen += rep->d_reclen; 1221 } 1222#ifdef UFS_DIRHASH 1223 if (dp->i_dirhash != NULL) 1224 ufsdirhash_checkblock(dp, (char *)ep - 1225 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)), 1226 dp->i_offset & ~(DIRBLKSIZ - 1)); 1227#endif 1228out: 1229 error = 0; 1230 if (DOINGSOFTDEP(dvp)) { 1231 if (ip) 1232 softdep_setup_remove(bp, dp, ip, isrmdir); 1233 if (softdep_slowdown(dvp)) 1234 error = bwrite(bp); 1235 else 1236 bdwrite(bp); 1237 } else { 1238 if (flags & DOWHITEOUT) 1239 error = bwrite(bp); 1240 else if (DOINGASYNC(dvp) && dp->i_count != 0) 1241 bdwrite(bp); 1242 else 1243 error = bwrite(bp); 1244 } 1245 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1246 /* 1247 * If the last named reference to a snapshot goes away, 1248 * drop its snapshot reference so that it will be reclaimed 1249 * when last open reference goes away. 1250 */ 1251 if (ip != NULL && (ip->i_flags & SF_SNAPSHOT) != 0 && 1252 ip->i_effnlink == 0) 1253 UFS_SNAPGONE(ip); 1254 return (error); 1255} 1256 1257/* 1258 * Rewrite an existing directory entry to point at the inode 1259 * supplied. The parameters describing the directory entry are 1260 * set up by a call to namei. 1261 */ 1262int 1263ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir) 1264 struct inode *dp, *oip; 1265 ino_t newinum; 1266 int newtype; 1267 int isrmdir; 1268{ 1269 struct buf *bp; 1270 struct direct *ep; 1271 struct vnode *vdp = ITOV(dp); 1272 int error; 1273 1274 /* 1275 * Drop the link before we lock the buf so softdep can block if 1276 * necessary. 1277 */ 1278 oip->i_effnlink--; 1279 if (DOINGSOFTDEP(vdp)) { 1280 softdep_setup_unlink(dp, oip); 1281 } else { 1282 oip->i_nlink--; 1283 DIP_SET(oip, i_nlink, oip->i_nlink); 1284 oip->i_flag |= IN_CHANGE; 1285 } 1286 1287 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp); 1288 if (error) 1289 return (error); 1290 if (ep->d_namlen == 2 && ep->d_name[1] == '.' && ep->d_name[0] == '.' && 1291 ep->d_ino != oip->i_number) { 1292 brelse(bp); 1293 return (EIDRM); 1294 } 1295 ep->d_ino = newinum; 1296 if (!OFSFMT(vdp)) 1297 ep->d_type = newtype; 1298 if (DOINGSOFTDEP(vdp)) { 1299 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir); 1300 bdwrite(bp); 1301 } else { 1302 if (DOINGASYNC(vdp)) { 1303 bdwrite(bp); 1304 error = 0; 1305 } else { 1306 error = bwrite(bp); 1307 } 1308 } 1309 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1310 /* 1311 * If the last named reference to a snapshot goes away, 1312 * drop its snapshot reference so that it will be reclaimed 1313 * when last open reference goes away. 1314 */ 1315 if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_effnlink == 0) 1316 UFS_SNAPGONE(oip); 1317 return (error); 1318} 1319 1320/* 1321 * Check if a directory is empty or not. 1322 * Inode supplied must be locked. 1323 * 1324 * Using a struct dirtemplate here is not precisely 1325 * what we want, but better than using a struct direct. 1326 * 1327 * NB: does not handle corrupted directories. 1328 */ 1329int 1330ufs_dirempty(ip, parentino, cred) 1331 struct inode *ip; 1332 ino_t parentino; 1333 struct ucred *cred; 1334{ 1335 doff_t off; 1336 struct dirtemplate dbuf; 1337 struct direct *dp = (struct direct *)&dbuf; 1338 int error, namlen; 1339 ssize_t count; 1340#define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 1341 1342 for (off = 0; off < ip->i_size; off += dp->d_reclen) { 1343 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, 1344 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred, 1345 NOCRED, &count, (struct thread *)0); 1346 /* 1347 * Since we read MINDIRSIZ, residual must 1348 * be 0 unless we're at end of file. 1349 */ 1350 if (error || count != 0) 1351 return (0); 1352 /* avoid infinite loops */ 1353 if (dp->d_reclen == 0) 1354 return (0); 1355 /* skip empty entries */ 1356 if (dp->d_ino == 0 || dp->d_ino == WINO) 1357 continue; 1358 /* accept only "." and ".." */ 1359# if (BYTE_ORDER == LITTLE_ENDIAN) 1360 if (OFSFMT(ITOV(ip))) 1361 namlen = dp->d_type; 1362 else 1363 namlen = dp->d_namlen; 1364# else 1365 namlen = dp->d_namlen; 1366# endif 1367 if (namlen > 2) 1368 return (0); 1369 if (dp->d_name[0] != '.') 1370 return (0); 1371 /* 1372 * At this point namlen must be 1 or 2. 1373 * 1 implies ".", 2 implies ".." if second 1374 * char is also "." 1375 */ 1376 if (namlen == 1 && dp->d_ino == ip->i_number) 1377 continue; 1378 if (dp->d_name[1] == '.' && dp->d_ino == parentino) 1379 continue; 1380 return (0); 1381 } 1382 return (1); 1383} 1384 1385static int 1386ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino, 1387 struct vnode **dd_vp) 1388{ 1389 struct dirtemplate dirbuf; 1390 struct vnode *ddvp; 1391 int error, namlen; 1392 1393 ASSERT_VOP_LOCKED(vp, "ufs_dir_dd_ino"); 1394 if (vp->v_type != VDIR) 1395 return (ENOTDIR); 1396 /* 1397 * First check to see if we have it in the name cache. 1398 */ 1399 if ((ddvp = vn_dir_dd_ino(vp)) != NULL) { 1400 KASSERT(ddvp->v_mount == vp->v_mount, 1401 ("ufs_dir_dd_ino: Unexpected mount point crossing")); 1402 *dd_ino = VTOI(ddvp)->i_number; 1403 *dd_vp = ddvp; 1404 return (0); 1405 } 1406 /* 1407 * Have to read the directory. 1408 */ 1409 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 1410 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, 1411 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL); 1412 if (error != 0) 1413 return (error); 1414#if (BYTE_ORDER == LITTLE_ENDIAN) 1415 if (OFSFMT(vp)) 1416 namlen = dirbuf.dotdot_type; 1417 else 1418 namlen = dirbuf.dotdot_namlen; 1419#else 1420 namlen = dirbuf.dotdot_namlen; 1421#endif 1422 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' || 1423 dirbuf.dotdot_name[1] != '.') 1424 return (ENOTDIR); 1425 *dd_ino = dirbuf.dotdot_ino; 1426 *dd_vp = NULL; 1427 return (0); 1428} 1429 1430/* 1431 * Check if source directory is in the path of the target directory. 1432 */ 1433int 1434ufs_checkpath(ino_t source_ino, ino_t parent_ino, struct inode *target, struct ucred *cred, ino_t *wait_ino) 1435{ 1436 struct mount *mp; 1437 struct vnode *tvp, *vp, *vp1; 1438 int error; 1439 ino_t dd_ino; 1440 1441 vp = tvp = ITOV(target); 1442 mp = vp->v_mount; 1443 *wait_ino = 0; 1444 if (target->i_number == source_ino) 1445 return (EEXIST); 1446 if (target->i_number == parent_ino) 1447 return (0); 1448 if (target->i_number == ROOTINO) 1449 return (0); 1450 for (;;) { 1451 error = ufs_dir_dd_ino(vp, cred, &dd_ino, &vp1); 1452 if (error != 0) 1453 break; 1454 if (dd_ino == source_ino) { 1455 error = EINVAL; 1456 break; 1457 } 1458 if (dd_ino == ROOTINO) 1459 break; 1460 if (dd_ino == parent_ino) 1461 break; 1462 if (vp1 == NULL) { 1463 error = VFS_VGET(mp, dd_ino, LK_SHARED | LK_NOWAIT, 1464 &vp1); 1465 if (error != 0) { 1466 *wait_ino = dd_ino; 1467 break; 1468 } 1469 } 1470 KASSERT(dd_ino == VTOI(vp1)->i_number, 1471 ("directory %d reparented\n", VTOI(vp1)->i_number)); 1472 if (vp != tvp) 1473 vput(vp); 1474 vp = vp1; 1475 } 1476 1477 if (error == ENOTDIR) 1478 panic("checkpath: .. not a directory\n"); 1479 if (vp1 != NULL) 1480 vput(vp1); 1481 if (vp != tvp) 1482 vput(vp); 1483 return (error); 1484} 1485