1 /* $NetBSD: ext2fs_lookup.c,v 1.18 2001/11/08 02:39:07 lukem Exp $ */ 2 3 /* 4 * Modified for NetBSD 1.2E 5 * May 1997, Manuel Bouyer 6 * Laboratoire d'informatique de Paris VI 7 */ 8 /* 9 * modified for Lites 1.1 10 * 11 * Aug 1995, Godmar Back (gback@cs.utah.edu) 12 * University of Utah, Department of Computer Science 13 */ 14 /* 15 * Copyright (c) 1989, 1993 16 * The Regents of the University of California. All rights reserved. 17 * (c) UNIX System Laboratories, Inc. 18 * All or some portions of this file are derived from material licensed 19 * to the University of California by American Telephone and Telegraph 20 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 21 * the permission of UNIX System Laboratories, Inc. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * This product includes software developed by the University of 34 * California, Berkeley and its contributors. 35 * 4. Neither the name of the University nor the names of its contributors 36 * may be used to endorse or promote products derived from this software 37 * without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 42 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49 * SUCH DAMAGE. 50 * 51 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94 52 */ 53 54 #include <sys/cdefs.h> 55 __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.18 2001/11/08 02:39:07 lukem Exp $"); 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/namei.h> 60 #include <sys/buf.h> 61 #include <sys/file.h> 62 #include <sys/mount.h> 63 #include <sys/vnode.h> 64 #include <sys/malloc.h> 65 #include <sys/dirent.h> 66 67 #include <ufs/ufs/inode.h> 68 #include <ufs/ufs/ufsmount.h> 69 #include <ufs/ufs/ufs_extern.h> 70 71 #include <ufs/ext2fs/ext2fs_extern.h> 72 #include <ufs/ext2fs/ext2fs_dir.h> 73 #include <ufs/ext2fs/ext2fs.h> 74 75 extern int dirchk; 76 77 static void ext2fs_dirconv2ffs __P((struct ext2fs_direct *e2dir, 78 struct dirent *ffsdir)); 79 static int ext2fs_dirbadentry __P((struct vnode *dp, 80 struct ext2fs_direct *de, 81 int entryoffsetinblock)); 82 83 /* 84 * the problem that is tackled below is the fact that FFS 85 * includes the terminating zero on disk while EXT2FS doesn't 86 * this implies that we need to introduce some padding. 87 * For instance, a filename "sbin" has normally a reclen 12 88 * in EXT2, but 16 in FFS. 89 * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...' 90 * If it wasn't for that, the complete ufs code for directories would 91 * have worked w/o changes (except for the difference in DIRBLKSIZ) 92 */ 93 static void 94 ext2fs_dirconv2ffs( e2dir, ffsdir) 95 struct ext2fs_direct *e2dir; 96 struct dirent *ffsdir; 97 { 98 memset(ffsdir, 0, sizeof(struct dirent)); 99 ffsdir->d_fileno = fs2h32(e2dir->e2d_ino); 100 ffsdir->d_namlen = e2dir->e2d_namlen; 101 102 ffsdir->d_type = DT_UNKNOWN; /* don't know more here */ 103 #ifdef DIAGNOSTIC 104 /* 105 * XXX Rigth now this can't happen, but if one day 106 * MAXNAMLEN != E2FS_MAXNAMLEN we should handle this more gracefully ! 107 */ 108 if (e2dir->e2d_namlen > MAXNAMLEN) 109 panic("ext2fs: e2dir->e2d_namlen\n"); 110 #endif 111 strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen); 112 113 /* Godmar thinks: since e2dir->e2d_reclen can be big and means 114 nothing anyway, we compute our own reclen according to what 115 we think is right 116 */ 117 ffsdir->d_reclen = DIRENT_SIZE(ffsdir); 118 } 119 120 /* 121 * Vnode op for reading directories. 122 * 123 * Convert the on-disk entries to <sys/dirent.h> entries. 124 * the problem is that the conversion will blow up some entries by four bytes, 125 * so it can't be done in place. This is too bad. Right now the conversion is 126 * done entry by entry, the converted entry is sent via uiomove. 127 * 128 * XXX allocate a buffer, convert as many entries as possible, then send 129 * the whole buffer to uiomove 130 */ 131 int 132 ext2fs_readdir(v) 133 void *v; 134 { 135 struct vop_readdir_args /* { 136 struct vnode *a_vp; 137 struct uio *a_uio; 138 struct ucred *a_cred; 139 int **a_eofflag; 140 off_t **a_cookies; 141 int ncookies; 142 } */ *ap = v; 143 struct uio *uio = ap->a_uio; 144 int error; 145 size_t e2fs_count, readcnt; 146 struct vnode *vp = ap->a_vp; 147 struct m_ext2fs *fs = VTOI(vp)->i_e2fs; 148 149 struct ext2fs_direct *dp; 150 struct dirent dstd; 151 struct uio auio; 152 struct iovec aiov; 153 caddr_t dirbuf; 154 off_t off = uio->uio_offset; 155 off_t *cookies = NULL; 156 int nc = 0, ncookies = 0; 157 int e2d_reclen; 158 159 if (vp->v_type != VDIR) 160 return (ENOTDIR); 161 162 e2fs_count = uio->uio_resid; 163 /* Make sure we don't return partial entries. */ 164 e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1); 165 if (e2fs_count <= 0) 166 return (EINVAL); 167 168 auio = *uio; 169 auio.uio_iov = &aiov; 170 auio.uio_iovcnt = 1; 171 auio.uio_segflg = UIO_SYSSPACE; 172 aiov.iov_len = e2fs_count; 173 auio.uio_resid = e2fs_count; 174 MALLOC(dirbuf, caddr_t, e2fs_count, M_TEMP, M_WAITOK); 175 if (ap->a_ncookies) { 176 nc = ncookies = e2fs_count / 16; 177 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK); 178 *ap->a_cookies = cookies; 179 } 180 memset(dirbuf, 0, e2fs_count); 181 aiov.iov_base = dirbuf; 182 183 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred); 184 if (error == 0) { 185 readcnt = e2fs_count - auio.uio_resid; 186 for (dp = (struct ext2fs_direct *)dirbuf; 187 (char *)dp < (char *)dirbuf + readcnt; ) { 188 e2d_reclen = fs2h16(dp->e2d_reclen); 189 if (e2d_reclen == 0) { 190 error = EIO; 191 break; 192 } 193 ext2fs_dirconv2ffs(dp, &dstd); 194 if(dstd.d_reclen > uio->uio_resid) { 195 break; 196 } 197 if ((error = uiomove((caddr_t)&dstd, dstd.d_reclen, uio)) != 0) { 198 break; 199 } 200 off = off + e2d_reclen; 201 if (cookies != NULL) { 202 *cookies++ = off; 203 if (--ncookies <= 0){ 204 break; /* out of cookies */ 205 } 206 } 207 /* advance dp */ 208 dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen); 209 } 210 /* we need to correct uio_offset */ 211 uio->uio_offset = off; 212 } 213 FREE(dirbuf, M_TEMP); 214 *ap->a_eofflag = VTOI(ap->a_vp)->i_e2fs_size <= uio->uio_offset; 215 if (ap->a_ncookies) { 216 if (error) { 217 free(*ap->a_cookies, M_TEMP); 218 *ap->a_ncookies = 0; 219 *ap->a_cookies = NULL; 220 } else 221 *ap->a_ncookies = nc - ncookies; 222 } 223 return (error); 224 } 225 226 /* 227 * Convert a component of a pathname into a pointer to a locked inode. 228 * This is a very central and rather complicated routine. 229 * If the file system is not maintained in a strict tree hierarchy, 230 * this can result in a deadlock situation (see comments in code below). 231 * 232 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending 233 * on whether the name is to be looked up, created, renamed, or deleted. 234 * When CREATE, RENAME, or DELETE is specified, information usable in 235 * creating, renaming, or deleting a directory entry may be calculated. 236 * If flag has LOCKPARENT or'ed into it and the target of the pathname 237 * exists, lookup returns both the target and its parent directory locked. 238 * When creating or renaming and LOCKPARENT is specified, the target may 239 * not be ".". When deleting and LOCKPARENT is specified, the target may 240 * be "."., but the caller must check to ensure it does an vrele and vput 241 * instead of two vputs. 242 * 243 * Overall outline of ext2fs_lookup: 244 * 245 * check accessibility of directory 246 * look for name in cache, if found, then if at end of path 247 * and deleting or creating, drop it, else return name 248 * search for name in directory, to found or notfound 249 * notfound: 250 * if creating, return locked directory, leaving info on available slots 251 * else return error 252 * found: 253 * if at end of path and deleting, return information to allow delete 254 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 255 * inode and return info to allow rewrite 256 * if not at end, add name to cache; if at end and neither creating 257 * nor deleting, add name to cache 258 */ 259 int 260 ext2fs_lookup(v) 261 void *v; 262 { 263 struct vop_lookup_args /* { 264 struct vnode *a_dvp; 265 struct vnode **a_vpp; 266 struct componentname *a_cnp; 267 } */ *ap = v; 268 struct vnode *vdp; /* vnode for directory being searched */ 269 struct inode *dp; /* inode for directory being searched */ 270 struct buf *bp; /* a buffer of directory entries */ 271 struct ext2fs_direct *ep; /* the current directory entry */ 272 int entryoffsetinblock; /* offset of ep in bp's buffer */ 273 enum {NONE, COMPACT, FOUND} slotstatus; 274 doff_t slotoffset; /* offset of area with free space */ 275 int slotsize; /* size of area at slotoffset */ 276 int slotfreespace; /* amount of space free in slot */ 277 int slotneeded; /* size of the entry we're seeking */ 278 int numdirpasses; /* strategy for directory search */ 279 doff_t endsearch; /* offset to end directory search */ 280 doff_t prevoff; /* prev entry dp->i_offset */ 281 struct vnode *pdp; /* saved dp during symlink work */ 282 struct vnode *tdp; /* returned by VFS_VGET */ 283 doff_t enduseful; /* pointer past last used dir slot */ 284 u_long bmask; /* block offset mask */ 285 int lockparent; /* 1 => lockparent flag is set */ 286 int wantparent; /* 1 => wantparent or lockparent flag */ 287 int namlen, error; 288 struct vnode **vpp = ap->a_vpp; 289 struct componentname *cnp = ap->a_cnp; 290 struct ucred *cred = cnp->cn_cred; 291 int flags = cnp->cn_flags; 292 int nameiop = cnp->cn_nameiop; 293 294 int dirblksize = VTOI(ap->a_dvp)->i_e2fs->e2fs_bsize; 295 296 bp = NULL; 297 slotoffset = -1; 298 *vpp = NULL; 299 vdp = ap->a_dvp; 300 dp = VTOI(vdp); 301 lockparent = flags & LOCKPARENT; 302 wantparent = flags & (LOCKPARENT|WANTPARENT); 303 /* 304 * Check accessiblity of directory. 305 */ 306 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0) 307 return (error); 308 309 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) && 310 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 311 return (EROFS); 312 313 /* 314 * We now have a segment name to search for, and a directory to search. 315 * 316 * Before tediously performing a linear scan of the directory, 317 * check the name cache to see if the directory/name pair 318 * we are looking for is known already. 319 */ 320 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0) 321 return (error); 322 323 /* 324 * Suppress search for slots unless creating 325 * file and at end of pathname, in which case 326 * we watch for a place to put the new file in 327 * case it doesn't already exist. 328 */ 329 slotstatus = FOUND; 330 slotfreespace = slotsize = slotneeded = 0; 331 if ((nameiop == CREATE || nameiop == RENAME) && 332 (flags & ISLASTCN)) { 333 slotstatus = NONE; 334 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen); 335 } 336 337 /* 338 * If there is cached information on a previous search of 339 * this directory, pick up where we last left off. 340 * We cache only lookups as these are the most common 341 * and have the greatest payoff. Caching CREATE has little 342 * benefit as it usually must search the entire directory 343 * to determine that the entry does not exist. Caching the 344 * location of the last DELETE or RENAME has not reduced 345 * profiling time and hence has been removed in the interest 346 * of simplicity. 347 */ 348 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 349 if (nameiop != LOOKUP || dp->i_diroff == 0 || 350 dp->i_diroff > dp->i_e2fs_size) { 351 entryoffsetinblock = 0; 352 dp->i_offset = 0; 353 numdirpasses = 1; 354 } else { 355 dp->i_offset = dp->i_diroff; 356 if ((entryoffsetinblock = dp->i_offset & bmask) && 357 (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))) 358 return (error); 359 numdirpasses = 2; 360 } 361 prevoff = dp->i_offset; 362 endsearch = roundup(dp->i_e2fs_size, dirblksize); 363 enduseful = 0; 364 365 searchloop: 366 while (dp->i_offset < endsearch) { 367 /* 368 * If necessary, get the next directory block. 369 */ 370 if ((dp->i_offset & bmask) == 0) { 371 if (bp != NULL) 372 brelse(bp); 373 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, 374 NULL, &bp); 375 if (error != 0) 376 return (error); 377 entryoffsetinblock = 0; 378 } 379 /* 380 * If still looking for a slot, and at a dirblksize 381 * boundary, have to start looking for free space again. 382 */ 383 if (slotstatus == NONE && 384 (entryoffsetinblock & (dirblksize - 1)) == 0) { 385 slotoffset = -1; 386 slotfreespace = 0; 387 } 388 /* 389 * Get pointer to next entry. 390 * Full validation checks are slow, so we only check 391 * enough to insure forward progress through the 392 * directory. Complete checks can be run by patching 393 * "dirchk" to be true. 394 */ 395 ep = (struct ext2fs_direct *) 396 ((char *)bp->b_data + entryoffsetinblock); 397 if (ep->e2d_reclen == 0 || 398 (dirchk && 399 ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) { 400 int i; 401 ufs_dirbad(dp, dp->i_offset, "mangled entry"); 402 i = dirblksize - 403 (entryoffsetinblock & (dirblksize - 1)); 404 dp->i_offset += i; 405 entryoffsetinblock += i; 406 continue; 407 } 408 409 /* 410 * If an appropriate sized slot has not yet been found, 411 * check to see if one is available. Also accumulate space 412 * in the current block so that we can determine if 413 * compaction is viable. 414 */ 415 if (slotstatus != FOUND) { 416 int size = fs2h16(ep->e2d_reclen); 417 418 if (ep->e2d_ino != 0) 419 size -= EXT2FS_DIRSIZ(ep->e2d_namlen); 420 if (size > 0) { 421 if (size >= slotneeded) { 422 slotstatus = FOUND; 423 slotoffset = dp->i_offset; 424 slotsize = fs2h16(ep->e2d_reclen); 425 } else if (slotstatus == NONE) { 426 slotfreespace += size; 427 if (slotoffset == -1) 428 slotoffset = dp->i_offset; 429 if (slotfreespace >= slotneeded) { 430 slotstatus = COMPACT; 431 slotsize = dp->i_offset + 432 fs2h16(ep->e2d_reclen) - slotoffset; 433 } 434 } 435 } 436 } 437 438 /* 439 * Check for a name match. 440 */ 441 if (ep->e2d_ino) { 442 namlen = ep->e2d_namlen; 443 if (namlen == cnp->cn_namelen && 444 !memcmp(cnp->cn_nameptr, ep->e2d_name, 445 (unsigned)namlen)) { 446 /* 447 * Save directory entry's inode number and 448 * reclen in ndp->ni_ufs area, and release 449 * directory buffer. 450 */ 451 dp->i_ino = fs2h32(ep->e2d_ino); 452 dp->i_reclen = fs2h16(ep->e2d_reclen); 453 brelse(bp); 454 goto found; 455 } 456 } 457 prevoff = dp->i_offset; 458 dp->i_offset += fs2h16(ep->e2d_reclen); 459 entryoffsetinblock += fs2h16(ep->e2d_reclen); 460 if (ep->e2d_ino) 461 enduseful = dp->i_offset; 462 } 463 /* notfound: */ 464 /* 465 * If we started in the middle of the directory and failed 466 * to find our target, we must check the beginning as well. 467 */ 468 if (numdirpasses == 2) { 469 numdirpasses--; 470 dp->i_offset = 0; 471 endsearch = dp->i_diroff; 472 goto searchloop; 473 } 474 if (bp != NULL) 475 brelse(bp); 476 /* 477 * If creating, and at end of pathname and current 478 * directory has not been removed, then can consider 479 * allowing file to be created. 480 */ 481 if ((nameiop == CREATE || nameiop == RENAME) && 482 (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) { 483 /* 484 * Access for write is interpreted as allowing 485 * creation of files in the directory. 486 */ 487 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0) 488 return (error); 489 /* 490 * Return an indication of where the new directory 491 * entry should be put. If we didn't find a slot, 492 * then set dp->i_count to 0 indicating 493 * that the new slot belongs at the end of the 494 * directory. If we found a slot, then the new entry 495 * can be put in the range from dp->i_offset to 496 * dp->i_offset + dp->i_count. 497 */ 498 if (slotstatus == NONE) { 499 dp->i_offset = roundup(dp->i_e2fs_size, dirblksize); 500 dp->i_count = 0; 501 enduseful = dp->i_offset; 502 } else { 503 dp->i_offset = slotoffset; 504 dp->i_count = slotsize; 505 if (enduseful < slotoffset + slotsize) 506 enduseful = slotoffset + slotsize; 507 } 508 dp->i_endoff = roundup(enduseful, dirblksize); 509 dp->i_flag |= IN_CHANGE | IN_UPDATE; 510 /* 511 * We return with the directory locked, so that 512 * the parameters we set up above will still be 513 * valid if we actually decide to do a direnter(). 514 * We return ni_vp == NULL to indicate that the entry 515 * does not currently exist; we leave a pointer to 516 * the (locked) directory inode in ndp->ni_dvp. 517 * The pathname buffer is saved so that the name 518 * can be obtained later. 519 * 520 * NB - if the directory is unlocked, then this 521 * information cannot be used. 522 */ 523 cnp->cn_flags |= SAVENAME; 524 if (!lockparent) { 525 VOP_UNLOCK(vdp, 0); 526 cnp->cn_flags |= PDIRUNLOCK; 527 } 528 return (EJUSTRETURN); 529 } 530 /* 531 * Insert name into cache (as non-existent) if appropriate. 532 */ 533 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 534 cache_enter(vdp, *vpp, cnp); 535 return (ENOENT); 536 537 found: 538 /* 539 * Check that directory length properly reflects presence 540 * of this entry. 541 */ 542 if (entryoffsetinblock + EXT2FS_DIRSIZ(ep->e2d_namlen) 543 > dp->i_e2fs_size) { 544 ufs_dirbad(dp, dp->i_offset, "i_size too small"); 545 dp->i_e2fs_size = entryoffsetinblock + 546 EXT2FS_DIRSIZ(ep->e2d_namlen); 547 dp->i_flag |= IN_CHANGE | IN_UPDATE; 548 } 549 550 /* 551 * Found component in pathname. 552 * If the final component of path name, save information 553 * in the cache as to where the entry was found. 554 */ 555 if ((flags & ISLASTCN) && nameiop == LOOKUP) 556 dp->i_diroff = dp->i_offset &~ (dirblksize - 1); 557 558 /* 559 * If deleting, and at end of pathname, return 560 * parameters which can be used to remove file. 561 * If the wantparent flag isn't set, we return only 562 * the directory (in ndp->ni_dvp), otherwise we go 563 * on and lock the inode, being careful with ".". 564 */ 565 if (nameiop == DELETE && (flags & ISLASTCN)) { 566 /* 567 * Write access to directory required to delete files. 568 */ 569 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0) 570 return (error); 571 /* 572 * Return pointer to current entry in dp->i_offset, 573 * and distance past previous entry (if there 574 * is a previous entry in this block) in dp->i_count. 575 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 576 */ 577 if ((dp->i_offset & (dirblksize - 1)) == 0) 578 dp->i_count = 0; 579 else 580 dp->i_count = dp->i_offset - prevoff; 581 if (dp->i_number == dp->i_ino) { 582 VREF(vdp); 583 *vpp = vdp; 584 return (0); 585 } 586 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) 587 return (error); 588 /* 589 * If directory is "sticky", then user must own 590 * the directory, or the file in it, else she 591 * may not delete it (unless she's root). This 592 * implements append-only directories. 593 */ 594 if ((dp->i_e2fs_mode & ISVTX) && 595 cred->cr_uid != 0 && 596 cred->cr_uid != dp->i_e2fs_uid && 597 VTOI(tdp)->i_e2fs_uid != cred->cr_uid) { 598 vput(tdp); 599 return (EPERM); 600 } 601 *vpp = tdp; 602 if (!lockparent) { 603 VOP_UNLOCK(vdp, 0); 604 cnp->cn_flags |= PDIRUNLOCK; 605 } 606 return (0); 607 } 608 609 /* 610 * If rewriting (RENAME), return the inode and the 611 * information required to rewrite the present directory 612 * Must get inode of directory entry to verify it's a 613 * regular file, or empty directory. 614 */ 615 if (nameiop == RENAME && wantparent && 616 (flags & ISLASTCN)) { 617 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc); 618 if (error) 619 return (error); 620 /* 621 * Careful about locking second inode. 622 * This can only occur if the target is ".". 623 */ 624 if (dp->i_number == dp->i_ino) 625 return (EISDIR); 626 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp); 627 if (error) 628 return (error); 629 *vpp = tdp; 630 cnp->cn_flags |= SAVENAME; 631 if (!lockparent) { 632 VOP_UNLOCK(vdp, 0); 633 cnp->cn_flags |= PDIRUNLOCK; 634 } 635 return (0); 636 } 637 638 /* 639 * Step through the translation in the name. We do not `vput' the 640 * directory because we may need it again if a symbolic link 641 * is relative to the current directory. Instead we save it 642 * unlocked as "pdp". We must get the target inode before unlocking 643 * the directory to insure that the inode will not be removed 644 * before we get it. We prevent deadlock by always fetching 645 * inodes from the root, moving down the directory tree. Thus 646 * when following backward pointers ".." we must unlock the 647 * parent directory before getting the requested directory. 648 * There is a potential race condition here if both the current 649 * and parent directories are removed before the VFS_VGET for the 650 * inode associated with ".." returns. We hope that this occurs 651 * infrequently since we cannot avoid this race condition without 652 * implementing a sophisticated deadlock detection algorithm. 653 * Note also that this simple deadlock detection scheme will not 654 * work if the file system has any hard links other than ".." 655 * that point backwards in the directory structure. 656 */ 657 pdp = vdp; 658 if (flags & ISDOTDOT) { 659 VOP_UNLOCK(pdp, 0); /* race to get the inode */ 660 cnp->cn_flags |= PDIRUNLOCK; 661 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp); 662 if (error) { 663 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0) 664 cnp->cn_flags &= ~PDIRUNLOCK; 665 return (error); 666 } 667 if (lockparent && (flags & ISLASTCN)) { 668 if ((error = vn_lock(pdp, LK_EXCLUSIVE))) { 669 vput(tdp); 670 return (error); 671 } 672 cnp->cn_flags &= ~PDIRUNLOCK; 673 } 674 *vpp = tdp; 675 } else if (dp->i_number == dp->i_ino) { 676 VREF(vdp); /* we want ourself, ie "." */ 677 *vpp = vdp; 678 } else { 679 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) 680 return (error); 681 if (!lockparent || !(flags & ISLASTCN)) { 682 VOP_UNLOCK(pdp, 0); 683 cnp->cn_flags |= PDIRUNLOCK; 684 } 685 *vpp = tdp; 686 } 687 688 /* 689 * Insert name into cache if appropriate. 690 */ 691 if (cnp->cn_flags & MAKEENTRY) 692 cache_enter(vdp, *vpp, cnp); 693 return (0); 694 } 695 696 /* 697 * Do consistency checking on a directory entry: 698 * record length must be multiple of 4 699 * entry must fit in rest of its dirblksize block 700 * record must be large enough to contain entry 701 * name is not longer than MAXNAMLEN 702 * name must be as long as advertised, and null terminated 703 */ 704 /* 705 * changed so that it confirms to ext2fs_check_dir_entry 706 */ 707 static int 708 ext2fs_dirbadentry(dp, de, entryoffsetinblock) 709 struct vnode *dp; 710 struct ext2fs_direct *de; 711 int entryoffsetinblock; 712 { 713 int dirblksize = VTOI(dp)->i_e2fs->e2fs_bsize; 714 715 char * error_msg = NULL; 716 int reclen = fs2h16(de->e2d_reclen); 717 int namlen = de->e2d_namlen; 718 719 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */ 720 error_msg = "rec_len is smaller than minimal"; 721 else if (reclen % 4 != 0) 722 error_msg = "rec_len % 4 != 0"; 723 else if (reclen < EXT2FS_DIRSIZ(namlen)) 724 error_msg = "reclen is too small for name_len"; 725 else if (entryoffsetinblock + reclen > dirblksize) 726 error_msg = "directory entry across blocks"; 727 else if (fs2h32(de->e2d_ino) > 728 VTOI(dp)->i_e2fs->e2fs.e2fs_icount) 729 error_msg = "inode out of bounds"; 730 731 if (error_msg != NULL) { 732 printf( "bad directory entry: %s\n" 733 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n", 734 error_msg, entryoffsetinblock, 735 (unsigned long) fs2h32(de->e2d_ino), 736 reclen, namlen); 737 panic("ext2fs_dirbadentry"); 738 } 739 return error_msg == NULL ? 0 : 1; 740 } 741 742 /* 743 * Write a directory entry after a call to namei, using the parameters 744 * that it left in nameidata. The argument ip is the inode which the new 745 * directory entry will refer to. Dvp is a pointer to the directory to 746 * be written, which was left locked by namei. Remaining parameters 747 * (dp->i_offset, dp->i_count) indicate how the space for the new 748 * entry is to be obtained. 749 */ 750 int 751 ext2fs_direnter(ip, dvp, cnp) 752 struct inode *ip; 753 struct vnode *dvp; 754 struct componentname *cnp; 755 { 756 struct ext2fs_direct *ep, *nep; 757 struct inode *dp; 758 struct buf *bp; 759 struct ext2fs_direct newdir; 760 struct iovec aiov; 761 struct uio auio; 762 u_int dsize; 763 int error, loc, newentrysize, spacefree; 764 char *dirbuf; 765 int dirblksize = ip->i_e2fs->e2fs_bsize; 766 767 768 #ifdef DIAGNOSTIC 769 if ((cnp->cn_flags & SAVENAME) == 0) 770 panic("direnter: missing name"); 771 #endif 772 dp = VTOI(dvp); 773 newdir.e2d_ino = h2fs32(ip->i_number); 774 newdir.e2d_namlen = cnp->cn_namelen; 775 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 && 776 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) { 777 newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_ffs_mode)); 778 } else { 779 newdir.e2d_type = 0; 780 }; 781 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1); 782 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen); 783 if (dp->i_count == 0) { 784 /* 785 * If dp->i_count is 0, then namei could find no 786 * space in the directory. Here, dp->i_offset will 787 * be on a directory block boundary and we will write the 788 * new entry into a fresh block. 789 */ 790 if (dp->i_offset & (dirblksize - 1)) 791 panic("ext2fs_direnter: newblk"); 792 auio.uio_offset = dp->i_offset; 793 newdir.e2d_reclen = h2fs16(dirblksize); 794 auio.uio_resid = newentrysize; 795 aiov.iov_len = newentrysize; 796 aiov.iov_base = (caddr_t)&newdir; 797 auio.uio_iov = &aiov; 798 auio.uio_iovcnt = 1; 799 auio.uio_rw = UIO_WRITE; 800 auio.uio_segflg = UIO_SYSSPACE; 801 auio.uio_procp = (struct proc *)0; 802 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred); 803 if (dirblksize > 804 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 805 /* XXX should grow with balloc() */ 806 panic("ext2fs_direnter: frag size"); 807 else if (!error) { 808 dp->i_e2fs_size = roundup(dp->i_e2fs_size, dirblksize); 809 dp->i_flag |= IN_CHANGE; 810 } 811 return (error); 812 } 813 814 /* 815 * If dp->i_count is non-zero, then namei found space 816 * for the new entry in the range dp->i_offset to 817 * dp->i_offset + dp->i_count in the directory. 818 * To use this space, we may have to compact the entries located 819 * there, by copying them together towards the beginning of the 820 * block, leaving the free space in one usable chunk at the end. 821 */ 822 823 /* 824 * Get the block containing the space for the new directory entry. 825 */ 826 if ((error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0) 827 return (error); 828 /* 829 * Find space for the new entry. In the simple case, the entry at 830 * offset base will have the space. If it does not, then namei 831 * arranged that compacting the region dp->i_offset to 832 * dp->i_offset + dp->i_count would yield the 833 * space. 834 */ 835 ep = (struct ext2fs_direct *)dirbuf; 836 dsize = EXT2FS_DIRSIZ(ep->e2d_namlen); 837 spacefree = fs2h16(ep->e2d_reclen) - dsize; 838 for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) { 839 nep = (struct ext2fs_direct *)(dirbuf + loc); 840 if (ep->e2d_ino) { 841 /* trim the existing slot */ 842 ep->e2d_reclen = h2fs16(dsize); 843 ep = (struct ext2fs_direct *)((char *)ep + dsize); 844 } else { 845 /* overwrite; nothing there; header is ours */ 846 spacefree += dsize; 847 } 848 dsize = EXT2FS_DIRSIZ(nep->e2d_namlen); 849 spacefree += fs2h16(nep->e2d_reclen) - dsize; 850 loc += fs2h16(nep->e2d_reclen); 851 memcpy((caddr_t)ep, (caddr_t)nep, dsize); 852 } 853 /* 854 * Update the pointer fields in the previous entry (if any), 855 * copy in the new entry, and write out the block. 856 */ 857 if (ep->e2d_ino == 0) { 858 #ifdef DIAGNOSTIC 859 if (spacefree + dsize < newentrysize) 860 panic("ext2fs_direnter: compact1"); 861 #endif 862 newdir.e2d_reclen = h2fs16(spacefree + dsize); 863 } else { 864 #ifdef DIAGNOSTIC 865 if (spacefree < newentrysize) { 866 printf("ext2fs_direnter: compact2 %u %u", 867 (u_int)spacefree, (u_int)newentrysize); 868 panic("ext2fs_direnter: compact2"); 869 } 870 #endif 871 newdir.e2d_reclen = h2fs16(spacefree); 872 ep->e2d_reclen = h2fs16(dsize); 873 ep = (struct ext2fs_direct *)((char *)ep + dsize); 874 } 875 memcpy((caddr_t)ep, (caddr_t)&newdir, (u_int)newentrysize); 876 error = VOP_BWRITE(bp); 877 dp->i_flag |= IN_CHANGE | IN_UPDATE; 878 if (!error && dp->i_endoff && dp->i_endoff < dp->i_e2fs_size) 879 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, 880 cnp->cn_cred, cnp->cn_proc); 881 return (error); 882 } 883 884 /* 885 * Remove a directory entry after a call to namei, using 886 * the parameters which it left in nameidata. The entry 887 * dp->i_offset contains the offset into the directory of the 888 * entry to be eliminated. The dp->i_count field contains the 889 * size of the previous record in the directory. If this 890 * is 0, the first entry is being deleted, so we need only 891 * zero the inode number to mark the entry as free. If the 892 * entry is not the first in the directory, we must reclaim 893 * the space of the now empty record by adding the record size 894 * to the size of the previous entry. 895 */ 896 int 897 ext2fs_dirremove(dvp, cnp) 898 struct vnode *dvp; 899 struct componentname *cnp; 900 { 901 struct inode *dp; 902 struct ext2fs_direct *ep; 903 struct buf *bp; 904 int error; 905 906 dp = VTOI(dvp); 907 if (dp->i_count == 0) { 908 /* 909 * First entry in block: set d_ino to zero. 910 */ 911 error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, 912 (char **)&ep, &bp); 913 if (error != 0) 914 return (error); 915 ep->e2d_ino = 0; 916 error = VOP_BWRITE(bp); 917 dp->i_flag |= IN_CHANGE | IN_UPDATE; 918 return (error); 919 } 920 /* 921 * Collapse new free space into previous entry. 922 */ 923 error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count), 924 (char **)&ep, &bp); 925 if (error != 0) 926 return (error); 927 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen); 928 error = VOP_BWRITE(bp); 929 dp->i_flag |= IN_CHANGE | IN_UPDATE; 930 return (error); 931 } 932 933 /* 934 * Rewrite an existing directory entry to point at the inode 935 * supplied. The parameters describing the directory entry are 936 * set up by a call to namei. 937 */ 938 int 939 ext2fs_dirrewrite(dp, ip, cnp) 940 struct inode *dp, *ip; 941 struct componentname *cnp; 942 { 943 struct buf *bp; 944 struct ext2fs_direct *ep; 945 struct vnode *vdp = ITOV(dp); 946 int error; 947 948 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp); 949 if (error != 0) 950 return (error); 951 ep->e2d_ino = h2fs32(ip->i_number); 952 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 && 953 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) { 954 ep->e2d_type = inot2ext2dt(IFTODT(ip->i_ffs_mode)); 955 } else { 956 ep->e2d_type = 0; 957 } 958 error = VOP_BWRITE(bp); 959 dp->i_flag |= IN_CHANGE | IN_UPDATE; 960 return (error); 961 } 962 963 /* 964 * Check if a directory is empty or not. 965 * Inode supplied must be locked. 966 * 967 * Using a struct dirtemplate here is not precisely 968 * what we want, but better than using a struct ext2fs_direct. 969 * 970 * NB: does not handle corrupted directories. 971 */ 972 int 973 ext2fs_dirempty(ip, parentino, cred) 974 struct inode *ip; 975 ino_t parentino; 976 struct ucred *cred; 977 { 978 off_t off; 979 struct ext2fs_dirtemplate dbuf; 980 struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf; 981 int error, namlen; 982 size_t count; 983 984 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2) 985 986 for (off = 0; off < ip->i_e2fs_size; off += fs2h16(dp->e2d_reclen)) { 987 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off, 988 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0); 989 /* 990 * Since we read MINDIRSIZ, residual must 991 * be 0 unless we're at end of file. 992 */ 993 if (error || count != 0) 994 return (0); 995 /* avoid infinite loops */ 996 if (dp->e2d_reclen == 0) 997 return (0); 998 /* skip empty entries */ 999 if (dp->e2d_ino == 0) 1000 continue; 1001 /* accept only "." and ".." */ 1002 namlen = dp->e2d_namlen; 1003 if (namlen > 2) 1004 return (0); 1005 if (dp->e2d_name[0] != '.') 1006 return (0); 1007 /* 1008 * At this point namlen must be 1 or 2. 1009 * 1 implies ".", 2 implies ".." if second 1010 * char is also "." 1011 */ 1012 if (namlen == 1) 1013 continue; 1014 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino) 1015 continue; 1016 return (0); 1017 } 1018 return (1); 1019 } 1020 1021 /* 1022 * Check if source directory is in the path of the target directory. 1023 * Target is supplied locked, source is unlocked. 1024 * The target is always vput before returning. 1025 */ 1026 int 1027 ext2fs_checkpath(source, target, cred) 1028 struct inode *source, *target; 1029 struct ucred *cred; 1030 { 1031 struct vnode *vp; 1032 int error, rootino, namlen; 1033 struct ext2fs_dirtemplate dirbuf; 1034 u_int32_t ino; 1035 1036 vp = ITOV(target); 1037 if (target->i_number == source->i_number) { 1038 error = EEXIST; 1039 goto out; 1040 } 1041 rootino = ROOTINO; 1042 error = 0; 1043 if (target->i_number == rootino) 1044 goto out; 1045 1046 for (;;) { 1047 if (vp->v_type != VDIR) { 1048 error = ENOTDIR; 1049 break; 1050 } 1051 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 1052 sizeof (struct ext2fs_dirtemplate), (off_t)0, 1053 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0, 1054 (struct proc *)0); 1055 if (error != 0) 1056 break; 1057 namlen = dirbuf.dotdot_namlen; 1058 if (namlen != 2 || 1059 dirbuf.dotdot_name[0] != '.' || 1060 dirbuf.dotdot_name[1] != '.') { 1061 error = ENOTDIR; 1062 break; 1063 } 1064 ino = fs2h32(dirbuf.dotdot_ino); 1065 if (ino == source->i_number) { 1066 error = EINVAL; 1067 break; 1068 } 1069 if (ino == rootino) 1070 break; 1071 vput(vp); 1072 error = VFS_VGET(vp->v_mount, ino, &vp); 1073 if (error != 0) { 1074 vp = NULL; 1075 break; 1076 } 1077 } 1078 1079 out: 1080 if (error == ENOTDIR) { 1081 printf("checkpath: .. not a directory\n"); 1082 panic("checkpath"); 1083 } 1084 if (vp != NULL) 1085 vput(vp); 1086 return (error); 1087 } 1088