1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * %sccs.include.redist.c% 11 * 12 * @(#)ufs_lookup.c 8.9 (Berkeley) 08/11/94 13 */ 14 15 #include <sys/param.h> 16 #include <sys/namei.h> 17 #include <sys/buf.h> 18 #include <sys/file.h> 19 #include <sys/mount.h> 20 #include <sys/vnode.h> 21 22 #include <ufs/ufs/quota.h> 23 #include <ufs/ufs/inode.h> 24 #include <ufs/ufs/dir.h> 25 #include <ufs/ufs/ufsmount.h> 26 #include <ufs/ufs/ufs_extern.h> 27 28 struct nchstats nchstats; 29 #ifdef DIAGNOSTIC 30 int dirchk = 1; 31 #else 32 int dirchk = 0; 33 #endif 34 35 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) 36 37 /* 38 * Convert a component of a pathname into a pointer to a locked inode. 39 * This is a very central and rather complicated routine. 40 * If the file system is not maintained in a strict tree hierarchy, 41 * this can result in a deadlock situation (see comments in code below). 42 * 43 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending 44 * on whether the name is to be looked up, created, renamed, or deleted. 45 * When CREATE, RENAME, or DELETE is specified, information usable in 46 * creating, renaming, or deleting a directory entry may be calculated. 47 * If flag has LOCKPARENT or'ed into it and the target of the pathname 48 * exists, lookup returns both the target and its parent directory locked. 49 * When creating or renaming and LOCKPARENT is specified, the target may 50 * not be ".". When deleting and LOCKPARENT is specified, the target may 51 * be "."., but the caller must check to ensure it does an vrele and vput 52 * instead of two vputs. 53 * 54 * Overall outline of ufs_lookup: 55 * 56 * check accessibility of directory 57 * look for name in cache, if found, then if at end of path 58 * and deleting or creating, drop it, else return name 59 * search for name in directory, to found or notfound 60 * notfound: 61 * if creating, return locked directory, leaving info on available slots 62 * else return error 63 * found: 64 * if at end of path and deleting, return information to allow delete 65 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 66 * inode and return info to allow rewrite 67 * if not at end, add name to cache; if at end and neither creating 68 * nor deleting, add name to cache 69 */ 70 int 71 ufs_lookup(ap) 72 struct vop_lookup_args /* { 73 struct vnode *a_dvp; 74 struct vnode **a_vpp; 75 struct componentname *a_cnp; 76 } */ *ap; 77 { 78 register struct vnode *vdp; /* vnode for directory being searched */ 79 register struct inode *dp; /* inode for directory being searched */ 80 struct buf *bp; /* a buffer of directory entries */ 81 register struct direct *ep; /* the current directory entry */ 82 int entryoffsetinblock; /* offset of ep in bp's buffer */ 83 enum {NONE, COMPACT, FOUND} slotstatus; 84 doff_t slotoffset; /* offset of area with free space */ 85 int slotsize; /* size of area at slotoffset */ 86 int slotfreespace; /* amount of space free in slot */ 87 int slotneeded; /* size of the entry we're seeking */ 88 int numdirpasses; /* strategy for directory search */ 89 doff_t endsearch; /* offset to end directory search */ 90 doff_t prevoff; /* prev entry dp->i_offset */ 91 struct vnode *pdp; /* saved dp during symlink work */ 92 struct vnode *tdp; /* returned by VFS_VGET */ 93 doff_t enduseful; /* pointer past last used dir slot */ 94 u_long bmask; /* block offset mask */ 95 int lockparent; /* 1 => lockparent flag is set */ 96 int wantparent; /* 1 => wantparent or lockparent flag */ 97 int namlen, error; 98 struct vnode **vpp = ap->a_vpp; 99 struct componentname *cnp = ap->a_cnp; 100 struct ucred *cred = cnp->cn_cred; 101 int flags = cnp->cn_flags; 102 int nameiop = cnp->cn_nameiop; 103 104 bp = NULL; 105 slotoffset = -1; 106 *vpp = NULL; 107 vdp = ap->a_dvp; 108 dp = VTOI(vdp); 109 lockparent = flags & LOCKPARENT; 110 wantparent = flags & (LOCKPARENT|WANTPARENT); 111 112 /* 113 * Check accessiblity of directory. 114 */ 115 if ((dp->i_mode & IFMT) != IFDIR) 116 return (ENOTDIR); 117 if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) 118 return (error); 119 120 /* 121 * We now have a segment name to search for, and a directory to search. 122 * 123 * Before tediously performing a linear scan of the directory, 124 * check the name cache to see if the directory/name pair 125 * we are looking for is known already. 126 */ 127 if (error = cache_lookup(vdp, vpp, cnp)) { 128 int vpid; /* capability number of vnode */ 129 130 if (error == ENOENT) 131 return (error); 132 /* 133 * Get the next vnode in the path. 134 * See comment below starting `Step through' for 135 * an explaination of the locking protocol. 136 */ 137 pdp = vdp; 138 dp = VTOI(*vpp); 139 vdp = *vpp; 140 vpid = vdp->v_id; 141 if (pdp == vdp) { /* lookup on "." */ 142 VREF(vdp); 143 error = 0; 144 } else if (flags & ISDOTDOT) { 145 VOP_UNLOCK(pdp); 146 error = vget(vdp, 1); 147 if (!error && lockparent && (flags & ISLASTCN)) 148 error = VOP_LOCK(pdp); 149 } else { 150 error = vget(vdp, 1); 151 if (!lockparent || error || !(flags & ISLASTCN)) 152 VOP_UNLOCK(pdp); 153 } 154 /* 155 * Check that the capability number did not change 156 * while we were waiting for the lock. 157 */ 158 if (!error) { 159 if (vpid == vdp->v_id) 160 return (0); 161 vput(vdp); 162 if (lockparent && pdp != vdp && (flags & ISLASTCN)) 163 VOP_UNLOCK(pdp); 164 } 165 if (error = VOP_LOCK(pdp)) 166 return (error); 167 vdp = pdp; 168 dp = VTOI(pdp); 169 *vpp = NULL; 170 } 171 172 /* 173 * Suppress search for slots unless creating 174 * file and at end of pathname, in which case 175 * we watch for a place to put the new file in 176 * case it doesn't already exist. 177 */ 178 slotstatus = FOUND; 179 slotfreespace = slotsize = slotneeded = 0; 180 if ((nameiop == CREATE || nameiop == RENAME) && 181 (flags & ISLASTCN)) { 182 slotstatus = NONE; 183 slotneeded = (sizeof(struct direct) - MAXNAMLEN + 184 cnp->cn_namelen + 3) &~ 3; 185 } 186 187 /* 188 * If there is cached information on a previous search of 189 * this directory, pick up where we last left off. 190 * We cache only lookups as these are the most common 191 * and have the greatest payoff. Caching CREATE has little 192 * benefit as it usually must search the entire directory 193 * to determine that the entry does not exist. Caching the 194 * location of the last DELETE or RENAME has not reduced 195 * profiling time and hence has been removed in the interest 196 * of simplicity. 197 */ 198 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 199 if (nameiop != LOOKUP || dp->i_diroff == 0 || 200 dp->i_diroff > dp->i_size) { 201 entryoffsetinblock = 0; 202 dp->i_offset = 0; 203 numdirpasses = 1; 204 } else { 205 dp->i_offset = dp->i_diroff; 206 if ((entryoffsetinblock = dp->i_offset & bmask) && 207 (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))) 208 return (error); 209 numdirpasses = 2; 210 nchstats.ncs_2passes++; 211 } 212 prevoff = dp->i_offset; 213 endsearch = roundup(dp->i_size, DIRBLKSIZ); 214 enduseful = 0; 215 216 searchloop: 217 while (dp->i_offset < endsearch) { 218 /* 219 * If necessary, get the next directory block. 220 */ 221 if ((dp->i_offset & bmask) == 0) { 222 if (bp != NULL) 223 brelse(bp); 224 if (error = 225 VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)) 226 return (error); 227 entryoffsetinblock = 0; 228 } 229 /* 230 * If still looking for a slot, and at a DIRBLKSIZE 231 * boundary, have to start looking for free space again. 232 */ 233 if (slotstatus == NONE && 234 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) { 235 slotoffset = -1; 236 slotfreespace = 0; 237 } 238 /* 239 * Get pointer to next entry. 240 * Full validation checks are slow, so we only check 241 * enough to insure forward progress through the 242 * directory. Complete checks can be run by patching 243 * "dirchk" to be true. 244 */ 245 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock); 246 if (ep->d_reclen == 0 || 247 dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock)) { 248 int i; 249 250 ufs_dirbad(dp, dp->i_offset, "mangled entry"); 251 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 252 dp->i_offset += i; 253 entryoffsetinblock += i; 254 continue; 255 } 256 257 /* 258 * If an appropriate sized slot has not yet been found, 259 * check to see if one is available. Also accumulate space 260 * in the current block so that we can determine if 261 * compaction is viable. 262 */ 263 if (slotstatus != FOUND) { 264 int size = ep->d_reclen; 265 266 if (ep->d_ino != 0) 267 size -= DIRSIZ(FSFMT(vdp), ep); 268 if (size > 0) { 269 if (size >= slotneeded) { 270 slotstatus = FOUND; 271 slotoffset = dp->i_offset; 272 slotsize = ep->d_reclen; 273 } else if (slotstatus == NONE) { 274 slotfreespace += size; 275 if (slotoffset == -1) 276 slotoffset = dp->i_offset; 277 if (slotfreespace >= slotneeded) { 278 slotstatus = COMPACT; 279 slotsize = dp->i_offset + 280 ep->d_reclen - slotoffset; 281 } 282 } 283 } 284 } 285 286 /* 287 * Check for a name match. 288 */ 289 if (ep->d_ino) { 290 # if (BYTE_ORDER == LITTLE_ENDIAN) 291 if (vdp->v_mount->mnt_maxsymlinklen > 0) 292 namlen = ep->d_namlen; 293 else 294 namlen = ep->d_type; 295 # else 296 namlen = ep->d_namlen; 297 # endif 298 if (namlen == cnp->cn_namelen && 299 !bcmp(cnp->cn_nameptr, ep->d_name, 300 (unsigned)namlen)) { 301 /* 302 * Save directory entry's inode number and 303 * reclen in ndp->ni_ufs area, and release 304 * directory buffer. 305 */ 306 if (ep->d_type == DT_WHT) { 307 slotstatus = FOUND; 308 slotoffset = dp->i_offset; 309 slotsize = ep->d_reclen; 310 dp->i_reclen = slotsize; 311 enduseful = slotoffset + slotsize; 312 ap->a_cnp->cn_flags |= ISWHITEOUT; 313 numdirpasses--; 314 goto notfound; 315 } 316 dp->i_ino = ep->d_ino; 317 dp->i_reclen = ep->d_reclen; 318 brelse(bp); 319 goto found; 320 } 321 } 322 prevoff = dp->i_offset; 323 dp->i_offset += ep->d_reclen; 324 entryoffsetinblock += ep->d_reclen; 325 if (ep->d_ino) 326 enduseful = dp->i_offset; 327 } 328 notfound: 329 /* 330 * If we started in the middle of the directory and failed 331 * to find our target, we must check the beginning as well. 332 */ 333 if (numdirpasses == 2) { 334 numdirpasses--; 335 dp->i_offset = 0; 336 endsearch = dp->i_diroff; 337 goto searchloop; 338 } 339 if (bp != NULL) 340 brelse(bp); 341 /* 342 * If creating, and at end of pathname and current 343 * directory has not been removed, then can consider 344 * allowing file to be created. 345 */ 346 if ((nameiop == CREATE || nameiop == RENAME || 347 (nameiop == DELETE && 348 (ap->a_cnp->cn_flags & DOWHITEOUT) && 349 (ap->a_cnp->cn_flags & ISWHITEOUT))) && 350 (flags & ISLASTCN) && dp->i_nlink != 0) { 351 /* 352 * Access for write is interpreted as allowing 353 * creation of files in the directory. 354 */ 355 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 356 return (error); 357 /* 358 * Return an indication of where the new directory 359 * entry should be put. If we didn't find a slot, 360 * then set dp->i_count to 0 indicating 361 * that the new slot belongs at the end of the 362 * directory. If we found a slot, then the new entry 363 * can be put in the range from dp->i_offset to 364 * dp->i_offset + dp->i_count. 365 */ 366 if (slotstatus == NONE) { 367 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ); 368 dp->i_count = 0; 369 enduseful = dp->i_offset; 370 } else if (nameiop == DELETE) { 371 dp->i_offset = slotoffset; 372 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 373 dp->i_count = 0; 374 else 375 dp->i_count = dp->i_offset - prevoff; 376 } else { 377 dp->i_offset = slotoffset; 378 dp->i_count = slotsize; 379 if (enduseful < slotoffset + slotsize) 380 enduseful = slotoffset + slotsize; 381 } 382 dp->i_endoff = roundup(enduseful, DIRBLKSIZ); 383 dp->i_flag |= IN_CHANGE | IN_UPDATE; 384 /* 385 * We return with the directory locked, so that 386 * the parameters we set up above will still be 387 * valid if we actually decide to do a direnter(). 388 * We return ni_vp == NULL to indicate that the entry 389 * does not currently exist; we leave a pointer to 390 * the (locked) directory inode in ndp->ni_dvp. 391 * The pathname buffer is saved so that the name 392 * can be obtained later. 393 * 394 * NB - if the directory is unlocked, then this 395 * information cannot be used. 396 */ 397 cnp->cn_flags |= SAVENAME; 398 if (!lockparent) 399 VOP_UNLOCK(vdp); 400 return (EJUSTRETURN); 401 } 402 /* 403 * Insert name into cache (as non-existent) if appropriate. 404 */ 405 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 406 cache_enter(vdp, *vpp, cnp); 407 return (ENOENT); 408 409 found: 410 if (numdirpasses == 2) 411 nchstats.ncs_pass2++; 412 /* 413 * Check that directory length properly reflects presence 414 * of this entry. 415 */ 416 if (entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep) > dp->i_size) { 417 ufs_dirbad(dp, dp->i_offset, "i_size too small"); 418 dp->i_size = entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep); 419 dp->i_flag |= IN_CHANGE | IN_UPDATE; 420 } 421 422 /* 423 * Found component in pathname. 424 * If the final component of path name, save information 425 * in the cache as to where the entry was found. 426 */ 427 if ((flags & ISLASTCN) && nameiop == LOOKUP) 428 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1); 429 430 /* 431 * If deleting, and at end of pathname, return 432 * parameters which can be used to remove file. 433 * If the wantparent flag isn't set, we return only 434 * the directory (in ndp->ni_dvp), otherwise we go 435 * on and lock the inode, being careful with ".". 436 */ 437 if (nameiop == DELETE && (flags & ISLASTCN)) { 438 /* 439 * Write access to directory required to delete files. 440 */ 441 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 442 return (error); 443 /* 444 * Return pointer to current entry in dp->i_offset, 445 * and distance past previous entry (if there 446 * is a previous entry in this block) in dp->i_count. 447 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 448 */ 449 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 450 dp->i_count = 0; 451 else 452 dp->i_count = dp->i_offset - prevoff; 453 if (dp->i_number == dp->i_ino) { 454 VREF(vdp); 455 *vpp = vdp; 456 return (0); 457 } 458 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 459 return (error); 460 /* 461 * If directory is "sticky", then user must own 462 * the directory, or the file in it, else she 463 * may not delete it (unless she's root). This 464 * implements append-only directories. 465 */ 466 if ((dp->i_mode & ISVTX) && 467 cred->cr_uid != 0 && 468 cred->cr_uid != dp->i_uid && 469 VTOI(tdp)->i_uid != cred->cr_uid) { 470 vput(tdp); 471 return (EPERM); 472 } 473 *vpp = tdp; 474 if (!lockparent) 475 VOP_UNLOCK(vdp); 476 return (0); 477 } 478 479 /* 480 * If rewriting (RENAME), return the inode and the 481 * information required to rewrite the present directory 482 * Must get inode of directory entry to verify it's a 483 * regular file, or empty directory. 484 */ 485 if (nameiop == RENAME && wantparent && 486 (flags & ISLASTCN)) { 487 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 488 return (error); 489 /* 490 * Careful about locking second inode. 491 * This can only occur if the target is ".". 492 */ 493 if (dp->i_number == dp->i_ino) 494 return (EISDIR); 495 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 496 return (error); 497 *vpp = tdp; 498 cnp->cn_flags |= SAVENAME; 499 if (!lockparent) 500 VOP_UNLOCK(vdp); 501 return (0); 502 } 503 504 /* 505 * Step through the translation in the name. We do not `vput' the 506 * directory because we may need it again if a symbolic link 507 * is relative to the current directory. Instead we save it 508 * unlocked as "pdp". We must get the target inode before unlocking 509 * the directory to insure that the inode will not be removed 510 * before we get it. We prevent deadlock by always fetching 511 * inodes from the root, moving down the directory tree. Thus 512 * when following backward pointers ".." we must unlock the 513 * parent directory before getting the requested directory. 514 * There is a potential race condition here if both the current 515 * and parent directories are removed before the VFS_VGET for the 516 * inode associated with ".." returns. We hope that this occurs 517 * infrequently since we cannot avoid this race condition without 518 * implementing a sophisticated deadlock detection algorithm. 519 * Note also that this simple deadlock detection scheme will not 520 * work if the file system has any hard links other than ".." 521 * that point backwards in the directory structure. 522 */ 523 pdp = vdp; 524 if (flags & ISDOTDOT) { 525 VOP_UNLOCK(pdp); /* race to get the inode */ 526 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) { 527 VOP_LOCK(pdp); 528 return (error); 529 } 530 if (lockparent && (flags & ISLASTCN) && 531 (error = VOP_LOCK(pdp))) { 532 vput(tdp); 533 return (error); 534 } 535 *vpp = tdp; 536 } else if (dp->i_number == dp->i_ino) { 537 VREF(vdp); /* we want ourself, ie "." */ 538 *vpp = vdp; 539 } else { 540 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 541 return (error); 542 if (!lockparent || !(flags & ISLASTCN)) 543 VOP_UNLOCK(pdp); 544 *vpp = tdp; 545 } 546 547 /* 548 * Insert name into cache if appropriate. 549 */ 550 if (cnp->cn_flags & MAKEENTRY) 551 cache_enter(vdp, *vpp, cnp); 552 return (0); 553 } 554 555 void 556 ufs_dirbad(ip, offset, how) 557 struct inode *ip; 558 doff_t offset; 559 char *how; 560 { 561 struct mount *mp; 562 563 mp = ITOV(ip)->v_mount; 564 (void)printf("%s: bad dir ino %d at offset %d: %s\n", 565 mp->mnt_stat.f_mntonname, ip->i_number, offset, how); 566 if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0) 567 panic("bad dir"); 568 } 569 570 /* 571 * Do consistency checking on a directory entry: 572 * record length must be multiple of 4 573 * entry must fit in rest of its DIRBLKSIZ block 574 * record must be large enough to contain entry 575 * name is not longer than MAXNAMLEN 576 * name must be as long as advertised, and null terminated 577 */ 578 int 579 ufs_dirbadentry(dp, ep, entryoffsetinblock) 580 struct vnode *dp; 581 register struct direct *ep; 582 int entryoffsetinblock; 583 { 584 register int i; 585 int namlen; 586 587 # if (BYTE_ORDER == LITTLE_ENDIAN) 588 if (dp->v_mount->mnt_maxsymlinklen > 0) 589 namlen = ep->d_namlen; 590 else 591 namlen = ep->d_type; 592 # else 593 namlen = ep->d_namlen; 594 # endif 595 if ((ep->d_reclen & 0x3) != 0 || 596 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 597 ep->d_reclen < DIRSIZ(FSFMT(dp), ep) || namlen > MAXNAMLEN) { 598 /*return (1); */ 599 printf("First bad\n"); 600 goto bad; 601 } 602 if (ep->d_ino == 0) 603 return (0); 604 for (i = 0; i < namlen; i++) 605 if (ep->d_name[i] == '\0') { 606 /*return (1); */ 607 printf("Second bad\n"); 608 goto bad; 609 } 610 if (ep->d_name[i]) 611 goto bad; 612 return (ep->d_name[i]); 613 bad: 614 return(1); 615 } 616 617 /* 618 * Write a directory entry after a call to namei, using the parameters 619 * that it left in nameidata. The argument ip is the inode which the new 620 * directory entry will refer to. Dvp is a pointer to the directory to 621 * be written, which was left locked by namei. Remaining parameters 622 * (dp->i_offset, dp->i_count) indicate how the space for the new 623 * entry is to be obtained. 624 */ 625 int 626 ufs_direnter(ip, dvp, cnp) 627 struct inode *ip; 628 struct vnode *dvp; 629 register struct componentname *cnp; 630 { 631 register struct inode *dp; 632 struct direct newdir; 633 634 #ifdef DIAGNOSTIC 635 if ((cnp->cn_flags & SAVENAME) == 0) 636 panic("direnter: missing name"); 637 #endif 638 dp = VTOI(dvp); 639 newdir.d_ino = ip->i_number; 640 newdir.d_namlen = cnp->cn_namelen; 641 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1); 642 if (dvp->v_mount->mnt_maxsymlinklen > 0) 643 newdir.d_type = IFTODT(ip->i_mode); 644 else { 645 newdir.d_type = 0; 646 # if (BYTE_ORDER == LITTLE_ENDIAN) 647 { u_char tmp = newdir.d_namlen; 648 newdir.d_namlen = newdir.d_type; 649 newdir.d_type = tmp; } 650 # endif 651 } 652 return (ufs_direnter2(dvp, &newdir, cnp->cn_cred, cnp->cn_proc)); 653 } 654 655 /* 656 * Common entry point for directory entry removal used by ufs_direnter 657 * and ufs_whiteout 658 */ 659 ufs_direnter2(dvp, dirp, cr, p) 660 struct vnode *dvp; 661 struct direct *dirp; 662 struct ucred *cr; 663 struct proc *p; 664 { 665 int newentrysize; 666 struct inode *dp; 667 struct buf *bp; 668 struct iovec aiov; 669 struct uio auio; 670 u_int dsize; 671 struct direct *ep, *nep; 672 int error, loc, spacefree; 673 char *dirbuf; 674 675 dp = VTOI(dvp); 676 newentrysize = DIRSIZ(FSFMT(dvp), dirp); 677 678 if (dp->i_count == 0) { 679 /* 680 * If dp->i_count is 0, then namei could find no 681 * space in the directory. Here, dp->i_offset will 682 * be on a directory block boundary and we will write the 683 * new entry into a fresh block. 684 */ 685 if (dp->i_offset & (DIRBLKSIZ - 1)) 686 panic("ufs_direnter2: newblk"); 687 auio.uio_offset = dp->i_offset; 688 dirp->d_reclen = DIRBLKSIZ; 689 auio.uio_resid = newentrysize; 690 aiov.iov_len = newentrysize; 691 aiov.iov_base = (caddr_t)dirp; 692 auio.uio_iov = &aiov; 693 auio.uio_iovcnt = 1; 694 auio.uio_rw = UIO_WRITE; 695 auio.uio_segflg = UIO_SYSSPACE; 696 auio.uio_procp = (struct proc *)0; 697 error = VOP_WRITE(dvp, &auio, IO_SYNC, cr); 698 if (DIRBLKSIZ > 699 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 700 /* XXX should grow with balloc() */ 701 panic("ufs_direnter2: frag size"); 702 else if (!error) { 703 dp->i_size = roundup(dp->i_size, DIRBLKSIZ); 704 dp->i_flag |= IN_CHANGE; 705 } 706 return (error); 707 } 708 709 /* 710 * If dp->i_count is non-zero, then namei found space 711 * for the new entry in the range dp->i_offset to 712 * dp->i_offset + dp->i_count in the directory. 713 * To use this space, we may have to compact the entries located 714 * there, by copying them together towards the beginning of the 715 * block, leaving the free space in one usable chunk at the end. 716 */ 717 718 /* 719 * Increase size of directory if entry eats into new space. 720 * This should never push the size past a new multiple of 721 * DIRBLKSIZE. 722 * 723 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 724 */ 725 if (dp->i_offset + dp->i_count > dp->i_size) 726 dp->i_size = dp->i_offset + dp->i_count; 727 /* 728 * Get the block containing the space for the new directory entry. 729 */ 730 if (error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) 731 return (error); 732 /* 733 * Find space for the new entry. In the simple case, the entry at 734 * offset base will have the space. If it does not, then namei 735 * arranged that compacting the region dp->i_offset to 736 * dp->i_offset + dp->i_count would yield the 737 * space. 738 */ 739 ep = (struct direct *)dirbuf; 740 dsize = DIRSIZ(FSFMT(dvp), ep); 741 spacefree = ep->d_reclen - dsize; 742 for (loc = ep->d_reclen; loc < dp->i_count; ) { 743 nep = (struct direct *)(dirbuf + loc); 744 if (ep->d_ino) { 745 /* trim the existing slot */ 746 ep->d_reclen = dsize; 747 ep = (struct direct *)((char *)ep + dsize); 748 } else { 749 /* overwrite; nothing there; header is ours */ 750 spacefree += dsize; 751 } 752 dsize = DIRSIZ(FSFMT(dvp), nep); 753 spacefree += nep->d_reclen - dsize; 754 loc += nep->d_reclen; 755 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 756 } 757 /* 758 * Update the pointer fields in the previous entry (if any), 759 * copy in the new entry, and write out the block. 760 */ 761 if (ep->d_ino == 0 || 762 (ep->d_ino == WINO && 763 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) { 764 if (spacefree + dsize < newentrysize) 765 panic("ufs_direnter2: compact1"); 766 dirp->d_reclen = spacefree + dsize; 767 } else { 768 if (spacefree < newentrysize) 769 panic("ufs_direnter2: compact2"); 770 dirp->d_reclen = spacefree; 771 ep->d_reclen = dsize; 772 ep = (struct direct *)((char *)ep + dsize); 773 } 774 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize); 775 error = VOP_BWRITE(bp); 776 dp->i_flag |= IN_CHANGE | IN_UPDATE; 777 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size) 778 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cr, p); 779 return (error); 780 } 781 782 /* 783 * Remove a directory entry after a call to namei, using 784 * the parameters which it left in nameidata. The entry 785 * dp->i_offset contains the offset into the directory of the 786 * entry to be eliminated. The dp->i_count field contains the 787 * size of the previous record in the directory. If this 788 * is 0, the first entry is being deleted, so we need only 789 * zero the inode number to mark the entry as free. If the 790 * entry is not the first in the directory, we must reclaim 791 * the space of the now empty record by adding the record size 792 * to the size of the previous entry. 793 */ 794 int 795 ufs_dirremove(dvp, cnp) 796 struct vnode *dvp; 797 struct componentname *cnp; 798 { 799 register struct inode *dp; 800 struct direct *ep; 801 struct buf *bp; 802 int error; 803 804 dp = VTOI(dvp); 805 806 if (cnp->cn_flags & DOWHITEOUT) { 807 /* 808 * First entry in block: set d_ino to zero. 809 */ 810 if (error = 811 VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) 812 return (error); 813 ep->d_ino = WINO; 814 ep->d_type = DT_WHT; 815 error = VOP_BWRITE(bp); 816 dp->i_flag |= IN_CHANGE | IN_UPDATE; 817 return (error); 818 } 819 820 if (dp->i_count == 0) { 821 /* 822 * First entry in block: set d_ino to zero. 823 */ 824 if (error = 825 VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) 826 return (error); 827 ep->d_ino = 0; 828 error = VOP_BWRITE(bp); 829 dp->i_flag |= IN_CHANGE | IN_UPDATE; 830 return (error); 831 } 832 /* 833 * Collapse new free space into previous entry. 834 */ 835 if (error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count), 836 (char **)&ep, &bp)) 837 return (error); 838 ep->d_reclen += dp->i_reclen; 839 error = VOP_BWRITE(bp); 840 dp->i_flag |= IN_CHANGE | IN_UPDATE; 841 return (error); 842 } 843 844 /* 845 * Rewrite an existing directory entry to point at the inode 846 * supplied. The parameters describing the directory entry are 847 * set up by a call to namei. 848 */ 849 int 850 ufs_dirrewrite(dp, ip, cnp) 851 struct inode *dp, *ip; 852 struct componentname *cnp; 853 { 854 struct buf *bp; 855 struct direct *ep; 856 struct vnode *vdp = ITOV(dp); 857 int error; 858 859 if (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp)) 860 return (error); 861 ep->d_ino = ip->i_number; 862 if (vdp->v_mount->mnt_maxsymlinklen > 0) 863 ep->d_type = IFTODT(ip->i_mode); 864 error = VOP_BWRITE(bp); 865 dp->i_flag |= IN_CHANGE | IN_UPDATE; 866 return (error); 867 } 868 869 /* 870 * Check if a directory is empty or not. 871 * Inode supplied must be locked. 872 * 873 * Using a struct dirtemplate here is not precisely 874 * what we want, but better than using a struct direct. 875 * 876 * NB: does not handle corrupted directories. 877 */ 878 int 879 ufs_dirempty(ip, parentino, cred) 880 register struct inode *ip; 881 ino_t parentino; 882 struct ucred *cred; 883 { 884 register off_t off; 885 struct dirtemplate dbuf; 886 register struct direct *dp = (struct direct *)&dbuf; 887 int error, count, namlen; 888 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 889 890 for (off = 0; off < ip->i_size; off += dp->d_reclen) { 891 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off, 892 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0); 893 /* 894 * Since we read MINDIRSIZ, residual must 895 * be 0 unless we're at end of file. 896 */ 897 if (error || count != 0) 898 return (0); 899 /* avoid infinite loops */ 900 if (dp->d_reclen == 0) 901 return (0); 902 /* skip empty entries */ 903 if (dp->d_ino == 0 || dp->d_ino == WINO) 904 continue; 905 /* accept only "." and ".." */ 906 # if (BYTE_ORDER == LITTLE_ENDIAN) 907 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0) 908 namlen = dp->d_namlen; 909 else 910 namlen = dp->d_type; 911 # else 912 namlen = dp->d_namlen; 913 # endif 914 if (namlen > 2) 915 return (0); 916 if (dp->d_name[0] != '.') 917 return (0); 918 /* 919 * At this point namlen must be 1 or 2. 920 * 1 implies ".", 2 implies ".." if second 921 * char is also "." 922 */ 923 if (namlen == 1) 924 continue; 925 if (dp->d_name[1] == '.' && dp->d_ino == parentino) 926 continue; 927 return (0); 928 } 929 return (1); 930 } 931 932 /* 933 * Check if source directory is in the path of the target directory. 934 * Target is supplied locked, source is unlocked. 935 * The target is always vput before returning. 936 */ 937 int 938 ufs_checkpath(source, target, cred) 939 struct inode *source, *target; 940 struct ucred *cred; 941 { 942 struct vnode *vp; 943 int error, rootino, namlen; 944 struct dirtemplate dirbuf; 945 946 vp = ITOV(target); 947 if (target->i_number == source->i_number) { 948 error = EEXIST; 949 goto out; 950 } 951 rootino = ROOTINO; 952 error = 0; 953 if (target->i_number == rootino) 954 goto out; 955 956 for (;;) { 957 if (vp->v_type != VDIR) { 958 error = ENOTDIR; 959 break; 960 } 961 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 962 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, 963 IO_NODELOCKED, cred, (int *)0, (struct proc *)0); 964 if (error != 0) 965 break; 966 # if (BYTE_ORDER == LITTLE_ENDIAN) 967 if (vp->v_mount->mnt_maxsymlinklen > 0) 968 namlen = dirbuf.dotdot_namlen; 969 else 970 namlen = dirbuf.dotdot_type; 971 # else 972 namlen = dirbuf.dotdot_namlen; 973 # endif 974 if (namlen != 2 || 975 dirbuf.dotdot_name[0] != '.' || 976 dirbuf.dotdot_name[1] != '.') { 977 error = ENOTDIR; 978 break; 979 } 980 if (dirbuf.dotdot_ino == source->i_number) { 981 error = EINVAL; 982 break; 983 } 984 if (dirbuf.dotdot_ino == rootino) 985 break; 986 vput(vp); 987 if (error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) { 988 vp = NULL; 989 break; 990 } 991 } 992 993 out: 994 if (error == ENOTDIR) 995 printf("checkpath: .. not a directory\n"); 996 if (vp != NULL) 997 vput(vp); 998 return (error); 999 } 1000