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.5 (Berkeley) 01/21/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 dp->i_ino = ep->d_ino; 307 dp->i_reclen = ep->d_reclen; 308 brelse(bp); 309 goto found; 310 } 311 } 312 prevoff = dp->i_offset; 313 dp->i_offset += ep->d_reclen; 314 entryoffsetinblock += ep->d_reclen; 315 if (ep->d_ino) 316 enduseful = dp->i_offset; 317 } 318 /* notfound: */ 319 /* 320 * If we started in the middle of the directory and failed 321 * to find our target, we must check the beginning as well. 322 */ 323 if (numdirpasses == 2) { 324 numdirpasses--; 325 dp->i_offset = 0; 326 endsearch = dp->i_diroff; 327 goto searchloop; 328 } 329 if (bp != NULL) 330 brelse(bp); 331 /* 332 * If creating, and at end of pathname and current 333 * directory has not been removed, then can consider 334 * allowing file to be created. 335 */ 336 if ((nameiop == CREATE || nameiop == RENAME) && 337 (flags & ISLASTCN) && dp->i_nlink != 0) { 338 /* 339 * Access for write is interpreted as allowing 340 * creation of files in the directory. 341 */ 342 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 343 return (error); 344 /* 345 * Return an indication of where the new directory 346 * entry should be put. If we didn't find a slot, 347 * then set dp->i_count to 0 indicating 348 * that the new slot belongs at the end of the 349 * directory. If we found a slot, then the new entry 350 * can be put in the range from dp->i_offset to 351 * dp->i_offset + dp->i_count. 352 */ 353 if (slotstatus == NONE) { 354 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ); 355 dp->i_count = 0; 356 enduseful = dp->i_offset; 357 } else { 358 dp->i_offset = slotoffset; 359 dp->i_count = slotsize; 360 if (enduseful < slotoffset + slotsize) 361 enduseful = slotoffset + slotsize; 362 } 363 dp->i_endoff = roundup(enduseful, DIRBLKSIZ); 364 dp->i_flag |= IN_CHANGE | IN_UPDATE; 365 /* 366 * We return with the directory locked, so that 367 * the parameters we set up above will still be 368 * valid if we actually decide to do a direnter(). 369 * We return ni_vp == NULL to indicate that the entry 370 * does not currently exist; we leave a pointer to 371 * the (locked) directory inode in ndp->ni_dvp. 372 * The pathname buffer is saved so that the name 373 * can be obtained later. 374 * 375 * NB - if the directory is unlocked, then this 376 * information cannot be used. 377 */ 378 cnp->cn_flags |= SAVENAME; 379 if (!lockparent) 380 VOP_UNLOCK(vdp); 381 return (EJUSTRETURN); 382 } 383 /* 384 * Insert name into cache (as non-existent) if appropriate. 385 */ 386 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 387 cache_enter(vdp, *vpp, cnp); 388 return (ENOENT); 389 390 found: 391 if (numdirpasses == 2) 392 nchstats.ncs_pass2++; 393 /* 394 * Check that directory length properly reflects presence 395 * of this entry. 396 */ 397 if (entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep) > dp->i_size) { 398 ufs_dirbad(dp, dp->i_offset, "i_size too small"); 399 dp->i_size = entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep); 400 dp->i_flag |= IN_CHANGE | IN_UPDATE; 401 } 402 403 /* 404 * Found component in pathname. 405 * If the final component of path name, save information 406 * in the cache as to where the entry was found. 407 */ 408 if ((flags & ISLASTCN) && nameiop == LOOKUP) 409 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1); 410 411 /* 412 * If deleting, and at end of pathname, return 413 * parameters which can be used to remove file. 414 * If the wantparent flag isn't set, we return only 415 * the directory (in ndp->ni_dvp), otherwise we go 416 * on and lock the inode, being careful with ".". 417 */ 418 if (nameiop == DELETE && (flags & ISLASTCN)) { 419 /* 420 * Write access to directory required to delete files. 421 */ 422 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 423 return (error); 424 /* 425 * Return pointer to current entry in dp->i_offset, 426 * and distance past previous entry (if there 427 * is a previous entry in this block) in dp->i_count. 428 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 429 */ 430 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 431 dp->i_count = 0; 432 else 433 dp->i_count = dp->i_offset - prevoff; 434 if (dp->i_number == dp->i_ino) { 435 VREF(vdp); 436 *vpp = vdp; 437 return (0); 438 } 439 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 440 return (error); 441 /* 442 * If directory is "sticky", then user must own 443 * the directory, or the file in it, else she 444 * may not delete it (unless she's root). This 445 * implements append-only directories. 446 */ 447 if ((dp->i_mode & ISVTX) && 448 cred->cr_uid != 0 && 449 cred->cr_uid != dp->i_uid && 450 VTOI(tdp)->i_uid != cred->cr_uid) { 451 vput(tdp); 452 return (EPERM); 453 } 454 *vpp = tdp; 455 if (!lockparent) 456 VOP_UNLOCK(vdp); 457 return (0); 458 } 459 460 /* 461 * If rewriting (RENAME), return the inode and the 462 * information required to rewrite the present directory 463 * Must get inode of directory entry to verify it's a 464 * regular file, or empty directory. 465 */ 466 if (nameiop == RENAME && wantparent && 467 (flags & ISLASTCN)) { 468 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) 469 return (error); 470 /* 471 * Careful about locking second inode. 472 * This can only occur if the target is ".". 473 */ 474 if (dp->i_number == dp->i_ino) 475 return (EISDIR); 476 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 477 return (error); 478 *vpp = tdp; 479 cnp->cn_flags |= SAVENAME; 480 if (!lockparent) 481 VOP_UNLOCK(vdp); 482 return (0); 483 } 484 485 /* 486 * Step through the translation in the name. We do not `vput' the 487 * directory because we may need it again if a symbolic link 488 * is relative to the current directory. Instead we save it 489 * unlocked as "pdp". We must get the target inode before unlocking 490 * the directory to insure that the inode will not be removed 491 * before we get it. We prevent deadlock by always fetching 492 * inodes from the root, moving down the directory tree. Thus 493 * when following backward pointers ".." we must unlock the 494 * parent directory before getting the requested directory. 495 * There is a potential race condition here if both the current 496 * and parent directories are removed before the `iget' for the 497 * inode associated with ".." returns. We hope that this occurs 498 * infrequently since we cannot avoid this race condition without 499 * implementing a sophisticated deadlock detection algorithm. 500 * Note also that this simple deadlock detection scheme will not 501 * work if the file system has any hard links other than ".." 502 * that point backwards in the directory structure. 503 */ 504 pdp = vdp; 505 if (flags & ISDOTDOT) { 506 VOP_UNLOCK(pdp); /* race to get the inode */ 507 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) { 508 VOP_LOCK(pdp); 509 return (error); 510 } 511 if (lockparent && (flags & ISLASTCN) && 512 (error = VOP_LOCK(pdp))) { 513 vput(tdp); 514 return (error); 515 } 516 *vpp = tdp; 517 } else if (dp->i_number == dp->i_ino) { 518 VREF(vdp); /* we want ourself, ie "." */ 519 *vpp = vdp; 520 } else { 521 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) 522 return (error); 523 if (!lockparent || !(flags & ISLASTCN)) 524 VOP_UNLOCK(pdp); 525 *vpp = tdp; 526 } 527 528 /* 529 * Insert name into cache if appropriate. 530 */ 531 if (cnp->cn_flags & MAKEENTRY) 532 cache_enter(vdp, *vpp, cnp); 533 return (0); 534 } 535 536 void 537 ufs_dirbad(ip, offset, how) 538 struct inode *ip; 539 doff_t offset; 540 char *how; 541 { 542 struct mount *mp; 543 544 mp = ITOV(ip)->v_mount; 545 (void)printf("%s: bad dir ino %d at offset %d: %s\n", 546 mp->mnt_stat.f_mntonname, ip->i_number, offset, how); 547 if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0) 548 panic("bad dir"); 549 } 550 551 /* 552 * Do consistency checking on a directory entry: 553 * record length must be multiple of 4 554 * entry must fit in rest of its DIRBLKSIZ block 555 * record must be large enough to contain entry 556 * name is not longer than MAXNAMLEN 557 * name must be as long as advertised, and null terminated 558 */ 559 int 560 ufs_dirbadentry(dp, ep, entryoffsetinblock) 561 struct vnode *dp; 562 register struct direct *ep; 563 int entryoffsetinblock; 564 { 565 register int i; 566 int namlen; 567 568 # if (BYTE_ORDER == LITTLE_ENDIAN) 569 if (dp->v_mount->mnt_maxsymlinklen > 0) 570 namlen = ep->d_namlen; 571 else 572 namlen = ep->d_type; 573 # else 574 namlen = ep->d_namlen; 575 # endif 576 if ((ep->d_reclen & 0x3) != 0 || 577 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 578 ep->d_reclen < DIRSIZ(FSFMT(dp), ep) || namlen > MAXNAMLEN) { 579 /*return (1); */ 580 printf("First bad\n"); 581 goto bad; 582 } 583 for (i = 0; i < namlen; i++) 584 if (ep->d_name[i] == '\0') { 585 /*return (1); */ 586 printf("Second bad\n"); 587 goto bad; 588 } 589 if (ep->d_name[i]) 590 goto bad; 591 return (ep->d_name[i]); 592 bad: 593 return(1); 594 } 595 596 /* 597 * Write a directory entry after a call to namei, using the parameters 598 * that it left in nameidata. The argument ip is the inode which the new 599 * directory entry will refer to. Dvp is a pointer to the directory to 600 * be written, which was left locked by namei. Remaining parameters 601 * (dp->i_offset, dp->i_count) indicate how the space for the new 602 * entry is to be obtained. 603 */ 604 int 605 ufs_direnter(ip, dvp, cnp) 606 struct inode *ip; 607 struct vnode *dvp; 608 register struct componentname *cnp; 609 { 610 register struct direct *ep, *nep; 611 register struct inode *dp; 612 struct buf *bp; 613 struct direct newdir; 614 struct iovec aiov; 615 struct uio auio; 616 u_int dsize; 617 int error, loc, newentrysize, spacefree; 618 char *dirbuf; 619 620 #ifdef DIAGNOSTIC 621 if ((cnp->cn_flags & SAVENAME) == 0) 622 panic("direnter: missing name"); 623 #endif 624 dp = VTOI(dvp); 625 newdir.d_ino = ip->i_number; 626 newdir.d_namlen = cnp->cn_namelen; 627 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1); 628 if (dvp->v_mount->mnt_maxsymlinklen > 0) 629 newdir.d_type = IFTODT(ip->i_mode); 630 else { 631 newdir.d_type = 0; 632 # if (BYTE_ORDER == LITTLE_ENDIAN) 633 { u_char tmp = newdir.d_namlen; 634 newdir.d_namlen = newdir.d_type; 635 newdir.d_type = tmp; } 636 # endif 637 } 638 newentrysize = DIRSIZ(FSFMT(dvp), &newdir); 639 if (dp->i_count == 0) { 640 /* 641 * If dp->i_count is 0, then namei could find no 642 * space in the directory. Here, dp->i_offset will 643 * be on a directory block boundary and we will write the 644 * new entry into a fresh block. 645 */ 646 if (dp->i_offset & (DIRBLKSIZ - 1)) 647 panic("ufs_direnter: newblk"); 648 auio.uio_offset = dp->i_offset; 649 newdir.d_reclen = DIRBLKSIZ; 650 auio.uio_resid = newentrysize; 651 aiov.iov_len = newentrysize; 652 aiov.iov_base = (caddr_t)&newdir; 653 auio.uio_iov = &aiov; 654 auio.uio_iovcnt = 1; 655 auio.uio_rw = UIO_WRITE; 656 auio.uio_segflg = UIO_SYSSPACE; 657 auio.uio_procp = (struct proc *)0; 658 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred); 659 if (DIRBLKSIZ > 660 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 661 /* XXX should grow with balloc() */ 662 panic("ufs_direnter: frag size"); 663 else if (!error) { 664 dp->i_size = roundup(dp->i_size, DIRBLKSIZ); 665 dp->i_flag |= IN_CHANGE; 666 } 667 return (error); 668 } 669 670 /* 671 * If dp->i_count is non-zero, then namei found space 672 * for the new entry in the range dp->i_offset to 673 * dp->i_offset + dp->i_count in the directory. 674 * To use this space, we may have to compact the entries located 675 * there, by copying them together towards the beginning of the 676 * block, leaving the free space in one usable chunk at the end. 677 */ 678 679 /* 680 * Increase size of directory if entry eats into new space. 681 * This should never push the size past a new multiple of 682 * DIRBLKSIZE. 683 * 684 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 685 */ 686 if (dp->i_offset + dp->i_count > dp->i_size) 687 dp->i_size = dp->i_offset + dp->i_count; 688 /* 689 * Get the block containing the space for the new directory entry. 690 */ 691 if (error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) 692 return (error); 693 /* 694 * Find space for the new entry. In the simple case, the entry at 695 * offset base will have the space. If it does not, then namei 696 * arranged that compacting the region dp->i_offset to 697 * dp->i_offset + dp->i_count would yield the 698 * space. 699 */ 700 ep = (struct direct *)dirbuf; 701 dsize = DIRSIZ(FSFMT(dvp), ep); 702 spacefree = ep->d_reclen - dsize; 703 for (loc = ep->d_reclen; loc < dp->i_count; ) { 704 nep = (struct direct *)(dirbuf + loc); 705 if (ep->d_ino) { 706 /* trim the existing slot */ 707 ep->d_reclen = dsize; 708 ep = (struct direct *)((char *)ep + dsize); 709 } else { 710 /* overwrite; nothing there; header is ours */ 711 spacefree += dsize; 712 } 713 dsize = DIRSIZ(FSFMT(dvp), nep); 714 spacefree += nep->d_reclen - dsize; 715 loc += nep->d_reclen; 716 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 717 } 718 /* 719 * Update the pointer fields in the previous entry (if any), 720 * copy in the new entry, and write out the block. 721 */ 722 if (ep->d_ino == 0) { 723 if (spacefree + dsize < newentrysize) 724 panic("ufs_direnter: compact1"); 725 newdir.d_reclen = spacefree + dsize; 726 } else { 727 if (spacefree < newentrysize) 728 panic("ufs_direnter: compact2"); 729 newdir.d_reclen = spacefree; 730 ep->d_reclen = dsize; 731 ep = (struct direct *)((char *)ep + dsize); 732 } 733 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize); 734 error = VOP_BWRITE(bp); 735 dp->i_flag |= IN_CHANGE | IN_UPDATE; 736 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size) 737 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, 738 cnp->cn_cred, cnp->cn_proc); 739 return (error); 740 } 741 742 /* 743 * Remove a directory entry after a call to namei, using 744 * the parameters which it left in nameidata. The entry 745 * dp->i_offset contains the offset into the directory of the 746 * entry to be eliminated. The dp->i_count field contains the 747 * size of the previous record in the directory. If this 748 * is 0, the first entry is being deleted, so we need only 749 * zero the inode number to mark the entry as free. If the 750 * entry is not the first in the directory, we must reclaim 751 * the space of the now empty record by adding the record size 752 * to the size of the previous entry. 753 */ 754 int 755 ufs_dirremove(dvp, cnp) 756 struct vnode *dvp; 757 struct componentname *cnp; 758 { 759 register struct inode *dp; 760 struct direct *ep; 761 struct buf *bp; 762 int error; 763 764 dp = VTOI(dvp); 765 if (dp->i_count == 0) { 766 /* 767 * First entry in block: set d_ino to zero. 768 */ 769 if (error = 770 VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) 771 return (error); 772 ep->d_ino = 0; 773 error = VOP_BWRITE(bp); 774 dp->i_flag |= IN_CHANGE | IN_UPDATE; 775 return (error); 776 } 777 /* 778 * Collapse new free space into previous entry. 779 */ 780 if (error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count), 781 (char **)&ep, &bp)) 782 return (error); 783 ep->d_reclen += dp->i_reclen; 784 error = VOP_BWRITE(bp); 785 dp->i_flag |= IN_CHANGE | IN_UPDATE; 786 return (error); 787 } 788 789 /* 790 * Rewrite an existing directory entry to point at the inode 791 * supplied. The parameters describing the directory entry are 792 * set up by a call to namei. 793 */ 794 int 795 ufs_dirrewrite(dp, ip, cnp) 796 struct inode *dp, *ip; 797 struct componentname *cnp; 798 { 799 struct buf *bp; 800 struct direct *ep; 801 struct vnode *vdp = ITOV(dp); 802 int error; 803 804 if (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp)) 805 return (error); 806 ep->d_ino = ip->i_number; 807 if (vdp->v_mount->mnt_maxsymlinklen > 0) 808 ep->d_type = IFTODT(ip->i_mode); 809 error = VOP_BWRITE(bp); 810 dp->i_flag |= IN_CHANGE | IN_UPDATE; 811 return (error); 812 } 813 814 /* 815 * Check if a directory is empty or not. 816 * Inode supplied must be locked. 817 * 818 * Using a struct dirtemplate here is not precisely 819 * what we want, but better than using a struct direct. 820 * 821 * NB: does not handle corrupted directories. 822 */ 823 int 824 ufs_dirempty(ip, parentino, cred) 825 register struct inode *ip; 826 ino_t parentino; 827 struct ucred *cred; 828 { 829 register off_t off; 830 struct dirtemplate dbuf; 831 register struct direct *dp = (struct direct *)&dbuf; 832 int error, count, namlen; 833 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 834 835 for (off = 0; off < ip->i_size; off += dp->d_reclen) { 836 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off, 837 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0); 838 /* 839 * Since we read MINDIRSIZ, residual must 840 * be 0 unless we're at end of file. 841 */ 842 if (error || count != 0) 843 return (0); 844 /* avoid infinite loops */ 845 if (dp->d_reclen == 0) 846 return (0); 847 /* skip empty entries */ 848 if (dp->d_ino == 0) 849 continue; 850 /* accept only "." and ".." */ 851 # if (BYTE_ORDER == LITTLE_ENDIAN) 852 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0) 853 namlen = dp->d_namlen; 854 else 855 namlen = dp->d_type; 856 # else 857 namlen = dp->d_namlen; 858 # endif 859 if (namlen > 2) 860 return (0); 861 if (dp->d_name[0] != '.') 862 return (0); 863 /* 864 * At this point namlen must be 1 or 2. 865 * 1 implies ".", 2 implies ".." if second 866 * char is also "." 867 */ 868 if (namlen == 1) 869 continue; 870 if (dp->d_name[1] == '.' && dp->d_ino == parentino) 871 continue; 872 return (0); 873 } 874 return (1); 875 } 876 877 /* 878 * Check if source directory is in the path of the target directory. 879 * Target is supplied locked, source is unlocked. 880 * The target is always vput before returning. 881 */ 882 int 883 ufs_checkpath(source, target, cred) 884 struct inode *source, *target; 885 struct ucred *cred; 886 { 887 struct vnode *vp; 888 int error, rootino, namlen; 889 struct dirtemplate dirbuf; 890 891 vp = ITOV(target); 892 if (target->i_number == source->i_number) { 893 error = EEXIST; 894 goto out; 895 } 896 rootino = ROOTINO; 897 error = 0; 898 if (target->i_number == rootino) 899 goto out; 900 901 for (;;) { 902 if (vp->v_type != VDIR) { 903 error = ENOTDIR; 904 break; 905 } 906 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 907 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, 908 IO_NODELOCKED, cred, (int *)0, (struct proc *)0); 909 if (error != 0) 910 break; 911 # if (BYTE_ORDER == LITTLE_ENDIAN) 912 if (vp->v_mount->mnt_maxsymlinklen > 0) 913 namlen = dirbuf.dotdot_namlen; 914 else 915 namlen = dirbuf.dotdot_type; 916 # else 917 namlen = dirbuf.dotdot_namlen; 918 # endif 919 if (namlen != 2 || 920 dirbuf.dotdot_name[0] != '.' || 921 dirbuf.dotdot_name[1] != '.') { 922 error = ENOTDIR; 923 break; 924 } 925 if (dirbuf.dotdot_ino == source->i_number) { 926 error = EINVAL; 927 break; 928 } 929 if (dirbuf.dotdot_ino == rootino) 930 break; 931 vput(vp); 932 if (error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) { 933 vp = NULL; 934 break; 935 } 936 } 937 938 out: 939 if (error == ENOTDIR) 940 printf("checkpath: .. not a directory\n"); 941 if (vp != NULL) 942 vput(vp); 943 return (error); 944 } 945