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