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