1 /*- 2 * modified for Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7 /*- 8 * SPDX-License-Identifier: BSD-3-Clause 9 * 10 * Copyright (c) 1989, 1993 11 * The Regents of the University of California. All rights reserved. 12 * (c) UNIX System Laboratories, Inc. 13 * All or some portions of this file are derived from material licensed 14 * to the University of California by American Telephone and Telegraph 15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 16 * the permission of UNIX System Laboratories, Inc. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/namei.h> 46 #include <sys/bio.h> 47 #include <sys/buf.h> 48 #include <sys/endian.h> 49 #include <sys/mount.h> 50 #include <sys/vnode.h> 51 #include <sys/malloc.h> 52 #include <sys/dirent.h> 53 #include <sys/sdt.h> 54 #include <sys/sysctl.h> 55 56 #include <ufs/ufs/dir.h> 57 58 #include <fs/ext2fs/fs.h> 59 #include <fs/ext2fs/inode.h> 60 #include <fs/ext2fs/ext2_mount.h> 61 #include <fs/ext2fs/ext2fs.h> 62 #include <fs/ext2fs/ext2_dinode.h> 63 #include <fs/ext2fs/ext2_dir.h> 64 #include <fs/ext2fs/ext2_extern.h> 65 #include <fs/ext2fs/fs.h> 66 67 SDT_PROVIDER_DECLARE(ext2fs); 68 /* 69 * ext2fs trace probe: 70 * arg0: verbosity. Higher numbers give more verbose messages 71 * arg1: Textual message 72 */ 73 SDT_PROBE_DEFINE2(ext2fs, , lookup, trace, "int", "char*"); 74 SDT_PROBE_DEFINE4(ext2fs, , trace, ext2_dirbad_error, 75 "char*", "ino_t", "doff_t", "char*"); 76 SDT_PROBE_DEFINE5(ext2fs, , trace, ext2_dirbadentry_error, 77 "char*", "int", "uint32_t", "uint16_t", "uint8_t"); 78 79 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 80 "EXT2FS filesystem"); 81 82 /* 83 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512) 84 while it is the native blocksize in ext2fs - thus, a #define 85 is no longer appropriate 86 */ 87 #undef DIRBLKSIZ 88 89 static u_char ext2_ft_to_dt[] = { 90 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */ 91 DT_REG, /* EXT2_FT_REG_FILE */ 92 DT_DIR, /* EXT2_FT_DIR */ 93 DT_CHR, /* EXT2_FT_CHRDEV */ 94 DT_BLK, /* EXT2_FT_BLKDEV */ 95 DT_FIFO, /* EXT2_FT_FIFO */ 96 DT_SOCK, /* EXT2_FT_SOCK */ 97 DT_LNK, /* EXT2_FT_SYMLINK */ 98 }; 99 #define FTTODT(ft) \ 100 ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN) 101 102 static u_char dt_to_ext2_ft[] = { 103 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */ 104 EXT2_FT_FIFO, /* DT_FIFO */ 105 EXT2_FT_CHRDEV, /* DT_CHR */ 106 EXT2_FT_UNKNOWN, /* unused */ 107 EXT2_FT_DIR, /* DT_DIR */ 108 EXT2_FT_UNKNOWN, /* unused */ 109 EXT2_FT_BLKDEV, /* DT_BLK */ 110 EXT2_FT_UNKNOWN, /* unused */ 111 EXT2_FT_REG_FILE, /* DT_REG */ 112 EXT2_FT_UNKNOWN, /* unused */ 113 EXT2_FT_SYMLINK, /* DT_LNK */ 114 EXT2_FT_UNKNOWN, /* unused */ 115 EXT2_FT_SOCK, /* DT_SOCK */ 116 EXT2_FT_UNKNOWN, /* unused */ 117 EXT2_FT_UNKNOWN, /* DT_WHT */ 118 }; 119 #define DTTOFT(dt) \ 120 ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN) 121 122 static int ext2_check_direntry(struct vnode *dp, 123 struct ext2fs_direct_2 *de, int entryoffsetinblock); 124 static int ext2_is_dot_entry(struct componentname *cnp); 125 static int ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, 126 struct componentname *cnp, ino_t *dd_ino); 127 128 static int 129 ext2_is_dot_entry(struct componentname *cnp) 130 { 131 if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' && 132 (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0')) 133 return (1); 134 return (0); 135 } 136 137 /* 138 * Vnode op for reading directories. 139 */ 140 int 141 ext2_readdir(struct vop_readdir_args *ap) 142 { 143 struct vnode *vp = ap->a_vp; 144 struct uio *uio = ap->a_uio; 145 struct buf *bp; 146 struct inode *ip; 147 struct ext2fs_direct_2 *dp, *edp; 148 uint64_t *cookies; 149 struct dirent dstdp; 150 off_t offset, startoffset; 151 size_t readcnt, skipcnt; 152 ssize_t startresid; 153 u_int ncookies; 154 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize; 155 int error; 156 157 if (uio->uio_offset < 0) 158 return (EINVAL); 159 ip = VTOI(vp); 160 if (ap->a_ncookies != NULL) { 161 if (uio->uio_resid < 0) 162 ncookies = 0; 163 else 164 ncookies = uio->uio_resid; 165 if (uio->uio_offset >= ip->i_size) 166 ncookies = 0; 167 else if (ip->i_size - uio->uio_offset < ncookies) 168 ncookies = ip->i_size - uio->uio_offset; 169 ncookies = ncookies / (offsetof(struct ext2fs_direct_2, 170 e2d_namlen) + 4) + 1; 171 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK); 172 *ap->a_ncookies = ncookies; 173 *ap->a_cookies = cookies; 174 } else { 175 ncookies = 0; 176 cookies = NULL; 177 } 178 offset = startoffset = uio->uio_offset; 179 startresid = uio->uio_resid; 180 error = 0; 181 while (error == 0 && uio->uio_resid > 0 && 182 uio->uio_offset < ip->i_size) { 183 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp); 184 if (error) 185 break; 186 if (bp->b_offset + bp->b_bcount > ip->i_size) 187 readcnt = ip->i_size - bp->b_offset; 188 else 189 readcnt = bp->b_bcount; 190 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) & 191 ~(size_t)(DIRBLKSIZ - 1); 192 offset = bp->b_offset + skipcnt; 193 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt]; 194 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt]; 195 while (error == 0 && uio->uio_resid > 0 && dp < edp) { 196 if (le16toh(dp->e2d_reclen) <= offsetof(struct ext2fs_direct_2, 197 e2d_namlen) || (caddr_t)dp + le16toh(dp->e2d_reclen) > 198 (caddr_t)edp) { 199 error = EIO; 200 break; 201 } 202 /*- 203 * "New" ext2fs directory entries differ in 3 ways 204 * from ufs on-disk ones: 205 * - the name is not necessarily NUL-terminated. 206 * - the file type field always exists and always 207 * follows the name length field. 208 * - the file type is encoded in a different way. 209 * 210 * "Old" ext2fs directory entries need no special 211 * conversions, since they are binary compatible 212 * with "new" entries having a file type of 0 (i.e., 213 * EXT2_FT_UNKNOWN). Splitting the old name length 214 * field didn't make a mess like it did in ufs, 215 * because ext2fs uses a machine-independent disk 216 * layout. 217 */ 218 dstdp.d_namlen = dp->e2d_namlen; 219 dstdp.d_type = FTTODT(dp->e2d_type); 220 if (offsetof(struct ext2fs_direct_2, e2d_namlen) + 221 dstdp.d_namlen > le16toh(dp->e2d_reclen)) { 222 error = EIO; 223 break; 224 } 225 if (offset < startoffset || le32toh(dp->e2d_ino) == 0) 226 goto nextentry; 227 dstdp.d_fileno = le32toh(dp->e2d_ino); 228 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp); 229 bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen); 230 /* NOTE: d_off is the offset of the *next* entry. */ 231 dstdp.d_off = offset + le16toh(dp->e2d_reclen); 232 dirent_terminate(&dstdp); 233 if (dstdp.d_reclen > uio->uio_resid) { 234 if (uio->uio_resid == startresid) 235 error = EINVAL; 236 else 237 error = EJUSTRETURN; 238 break; 239 } 240 /* Advance dp. */ 241 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio); 242 if (error) 243 break; 244 if (cookies != NULL) { 245 KASSERT(ncookies > 0, 246 ("ext2_readdir: cookies buffer too small")); 247 *cookies = offset + le16toh(dp->e2d_reclen); 248 cookies++; 249 ncookies--; 250 } 251 nextentry: 252 offset += le16toh(dp->e2d_reclen); 253 dp = (struct ext2fs_direct_2 *)((caddr_t)dp + 254 le16toh(dp->e2d_reclen)); 255 } 256 bqrelse(bp); 257 uio->uio_offset = offset; 258 } 259 /* We need to correct uio_offset. */ 260 uio->uio_offset = offset; 261 if (error == EJUSTRETURN) 262 error = 0; 263 if (ap->a_ncookies != NULL) { 264 if (error == 0) { 265 *ap->a_ncookies -= ncookies; 266 } else { 267 free(*ap->a_cookies, M_TEMP); 268 *ap->a_ncookies = 0; 269 *ap->a_cookies = NULL; 270 } 271 } 272 if (error == 0 && ap->a_eofflag) 273 *ap->a_eofflag = ip->i_size <= uio->uio_offset; 274 return (error); 275 } 276 277 /* 278 * Convert a component of a pathname into a pointer to a locked inode. 279 * This is a very central and rather complicated routine. 280 * If the file system is not maintained in a strict tree hierarchy, 281 * this can result in a deadlock situation (see comments in code below). 282 * 283 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending 284 * on whether the name is to be looked up, created, renamed, or deleted. 285 * When CREATE, RENAME, or DELETE is specified, information usable in 286 * creating, renaming, or deleting a directory entry may be calculated. 287 * If flag has LOCKPARENT or'ed into it and the target of the pathname 288 * exists, lookup returns both the target and its parent directory locked. 289 * When creating or renaming and LOCKPARENT is specified, the target may 290 * not be ".". When deleting and LOCKPARENT is specified, the target may 291 * be "."., but the caller must check to ensure it does an vrele and vput 292 * instead of two vputs. 293 * 294 * Overall outline of ext2_lookup: 295 * 296 * search for name in directory, to found or notfound 297 * notfound: 298 * if creating, return locked directory, leaving info on available slots 299 * else return error 300 * found: 301 * if at end of path and deleting, return information to allow delete 302 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 303 * inode and return info to allow rewrite 304 * if not at end, add name to cache; if at end and neither creating 305 * nor deleting, add name to cache 306 */ 307 int 308 ext2_lookup(struct vop_cachedlookup_args *ap) 309 { 310 311 return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL)); 312 } 313 314 static int 315 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp, 316 ino_t *dd_ino) 317 { 318 struct inode *dp; /* inode for directory being searched */ 319 struct buf *bp; /* a buffer of directory entries */ 320 struct ext2fs_direct_2 *ep; /* the current directory entry */ 321 int entryoffsetinblock; /* offset of ep in bp's buffer */ 322 struct ext2fs_searchslot ss; 323 doff_t i_diroff; /* cached i_diroff value */ 324 doff_t i_offset; /* cached i_offset value */ 325 int numdirpasses; /* strategy for directory search */ 326 doff_t endsearch; /* offset to end directory search */ 327 doff_t prevoff; /* prev entry dp->i_offset */ 328 struct vnode *pdp; /* saved dp during symlink work */ 329 struct vnode *tdp; /* returned by VFS_VGET */ 330 doff_t enduseful; /* pointer past last used dir slot */ 331 u_long bmask; /* block offset mask */ 332 int error; 333 struct ucred *cred = cnp->cn_cred; 334 int flags = cnp->cn_flags; 335 int nameiop = cnp->cn_nameiop; 336 ino_t ino, ino1; 337 int ltype; 338 int entry_found = 0; 339 340 int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize; 341 342 if (vpp != NULL) 343 *vpp = NULL; 344 345 dp = VTOI(vdp); 346 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; 347 restart: 348 bp = NULL; 349 ss.slotoffset = -1; 350 351 /* 352 * We now have a segment name to search for, and a directory to search. 353 * 354 * Suppress search for slots unless creating 355 * file and at end of pathname, in which case 356 * we watch for a place to put the new file in 357 * case it doesn't already exist. 358 */ 359 i_diroff = dp->i_diroff; 360 ss.slotstatus = FOUND; 361 ss.slotfreespace = ss.slotsize = ss.slotneeded = 0; 362 if ((nameiop == CREATE || nameiop == RENAME) && 363 (flags & ISLASTCN)) { 364 ss.slotstatus = NONE; 365 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen); 366 /* 367 * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN + 368 * cnp->cn_namelen + 3) &~ 3; 369 */ 370 } 371 /* 372 * Try to lookup dir entry using htree directory index. 373 * 374 * If we got an error or we want to find '.' or '..' entry, 375 * we will fall back to linear search. 376 */ 377 if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) { 378 numdirpasses = 1; 379 entryoffsetinblock = 0; 380 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen, 381 &bp, &entryoffsetinblock, &i_offset, &prevoff, 382 &enduseful, &ss)) { 383 case 0: 384 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data + 385 (i_offset & bmask)); 386 goto foundentry; 387 case ENOENT: 388 i_offset = roundup2(dp->i_size, DIRBLKSIZ); 389 goto notfound; 390 default: 391 /* 392 * Something failed; just fallback to do a linear 393 * search. 394 */ 395 break; 396 } 397 } 398 399 /* 400 * If there is cached information on a previous search of 401 * this directory, pick up where we last left off. 402 * We cache only lookups as these are the most common 403 * and have the greatest payoff. Caching CREATE has little 404 * benefit as it usually must search the entire directory 405 * to determine that the entry does not exist. Caching the 406 * location of the last DELETE or RENAME has not reduced 407 * profiling time and hence has been removed in the interest 408 * of simplicity. 409 */ 410 if (nameiop != LOOKUP || i_diroff == 0 || 411 i_diroff > dp->i_size) { 412 entryoffsetinblock = 0; 413 i_offset = 0; 414 numdirpasses = 1; 415 } else { 416 i_offset = i_diroff; 417 if ((entryoffsetinblock = i_offset & bmask) && 418 (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, 419 &bp))) 420 return (error); 421 numdirpasses = 2; 422 nchstats.ncs_2passes++; 423 } 424 prevoff = i_offset; 425 endsearch = roundup2(dp->i_size, DIRBLKSIZ); 426 enduseful = 0; 427 428 searchloop: 429 while (i_offset < endsearch) { 430 /* 431 * If necessary, get the next directory block. 432 */ 433 if (bp != NULL) 434 brelse(bp); 435 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp); 436 if (error != 0) 437 return (error); 438 439 entryoffsetinblock = 0; 440 if (ss.slotstatus == NONE) { 441 ss.slotoffset = -1; 442 ss.slotfreespace = 0; 443 } 444 445 error = ext2_search_dirblock(dp, bp->b_data, &entry_found, 446 cnp->cn_nameptr, cnp->cn_namelen, 447 &entryoffsetinblock, &i_offset, &prevoff, 448 &enduseful, &ss); 449 if (error != 0) { 450 brelse(bp); 451 return (error); 452 } 453 if (entry_found) { 454 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data + 455 (entryoffsetinblock & bmask)); 456 foundentry: 457 ino = le32toh(ep->e2d_ino); 458 goto found; 459 } 460 } 461 notfound: 462 /* 463 * If we started in the middle of the directory and failed 464 * to find our target, we must check the beginning as well. 465 */ 466 if (numdirpasses == 2) { 467 numdirpasses--; 468 i_offset = 0; 469 endsearch = i_diroff; 470 goto searchloop; 471 } 472 if (bp != NULL) 473 brelse(bp); 474 /* 475 * If creating, and at end of pathname and current 476 * directory has not been removed, then can consider 477 * allowing file to be created. 478 */ 479 if ((nameiop == CREATE || nameiop == RENAME) && 480 (flags & ISLASTCN) && dp->i_nlink != 0) { 481 /* 482 * Access for write is interpreted as allowing 483 * creation of files in the directory. 484 */ 485 if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0) 486 return (error); 487 /* 488 * Return an indication of where the new directory 489 * entry should be put. If we didn't find a slot, 490 * then set dp->i_count to 0 indicating 491 * that the new slot belongs at the end of the 492 * directory. If we found a slot, then the new entry 493 * can be put in the range from dp->i_offset to 494 * dp->i_offset + dp->i_count. 495 */ 496 if (ss.slotstatus == NONE) { 497 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ); 498 dp->i_count = 0; 499 enduseful = dp->i_offset; 500 } else { 501 dp->i_offset = ss.slotoffset; 502 dp->i_count = ss.slotsize; 503 if (enduseful < ss.slotoffset + ss.slotsize) 504 enduseful = ss.slotoffset + ss.slotsize; 505 } 506 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ); 507 /* 508 * We return with the directory locked, so that 509 * the parameters we set up above will still be 510 * valid if we actually decide to do a direnter(). 511 * We return ni_vp == NULL to indicate that the entry 512 * does not currently exist; we leave a pointer to 513 * the (locked) directory inode in ndp->ni_dvp. 514 * 515 * NB - if the directory is unlocked, then this 516 * information cannot be used. 517 */ 518 return (EJUSTRETURN); 519 } 520 /* 521 * Insert name into cache (as non-existent) if appropriate. 522 */ 523 if ((cnp->cn_flags & MAKEENTRY) != 0) 524 cache_enter(vdp, NULL, cnp); 525 return (ENOENT); 526 527 found: 528 if (dd_ino != NULL) 529 *dd_ino = ino; 530 if (numdirpasses == 2) 531 nchstats.ncs_pass2++; 532 /* 533 * Check that directory length properly reflects presence 534 * of this entry. 535 */ 536 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen) > 537 dp->i_size) { 538 ext2_dirbad(dp, i_offset, "i_size too small"); 539 brelse(bp); 540 return (EIO); 541 } 542 brelse(bp); 543 544 /* 545 * Found component in pathname. 546 * If the final component of path name, save information 547 * in the cache as to where the entry was found. 548 */ 549 if ((flags & ISLASTCN) && nameiop == LOOKUP) 550 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ); 551 /* 552 * If deleting, and at end of pathname, return 553 * parameters which can be used to remove file. 554 */ 555 if (nameiop == DELETE && (flags & ISLASTCN)) { 556 if (flags & LOCKPARENT) 557 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__); 558 /* 559 * Write access to directory required to delete files. 560 */ 561 if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0) 562 return (error); 563 /* 564 * Return pointer to current entry in dp->i_offset, 565 * and distance past previous entry (if there 566 * is a previous entry in this block) in dp->i_count. 567 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 568 * 569 * Technically we shouldn't be setting these in the 570 * WANTPARENT case (first lookup in rename()), but any 571 * lookups that will result in directory changes will 572 * overwrite these. 573 */ 574 dp->i_offset = i_offset; 575 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0) 576 dp->i_count = 0; 577 else 578 dp->i_count = dp->i_offset - prevoff; 579 if (dd_ino != NULL) 580 return (0); 581 if (dp->i_number == ino) { 582 VREF(vdp); 583 *vpp = vdp; 584 return (0); 585 } 586 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE, 587 &tdp)) != 0) 588 return (error); 589 /* 590 * If directory is "sticky", then user must own 591 * the directory, or the file in it, else she 592 * may not delete it (unless she's root). This 593 * implements append-only directories. 594 */ 595 if ((dp->i_mode & ISVTX) && 596 cred->cr_uid != 0 && 597 cred->cr_uid != dp->i_uid && 598 VTOI(tdp)->i_uid != cred->cr_uid) { 599 vput(tdp); 600 return (EPERM); 601 } 602 *vpp = tdp; 603 return (0); 604 } 605 606 /* 607 * If rewriting (RENAME), return the inode and the 608 * information required to rewrite the present directory 609 * Must get inode of directory entry to verify it's a 610 * regular file, or empty directory. 611 */ 612 if (nameiop == RENAME && (flags & ISLASTCN)) { 613 if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0) 614 return (error); 615 /* 616 * Careful about locking second inode. 617 * This can only occur if the target is ".". 618 */ 619 dp->i_offset = i_offset; 620 if (dp->i_number == ino) 621 return (EISDIR); 622 if (dd_ino != NULL) 623 return (0); 624 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE, 625 &tdp)) != 0) 626 return (error); 627 *vpp = tdp; 628 return (0); 629 } 630 if (dd_ino != NULL) 631 return (0); 632 633 /* 634 * Step through the translation in the name. We do not `vput' the 635 * directory because we may need it again if a symbolic link 636 * is relative to the current directory. Instead we save it 637 * unlocked as "pdp". We must get the target inode before unlocking 638 * the directory to insure that the inode will not be removed 639 * before we get it. We prevent deadlock by always fetching 640 * inodes from the root, moving down the directory tree. Thus 641 * when following backward pointers ".." we must unlock the 642 * parent directory before getting the requested directory. 643 * There is a potential race condition here if both the current 644 * and parent directories are removed before the VFS_VGET for the 645 * inode associated with ".." returns. We hope that this occurs 646 * infrequently since we cannot avoid this race condition without 647 * implementing a sophisticated deadlock detection algorithm. 648 * Note also that this simple deadlock detection scheme will not 649 * work if the file system has any hard links other than ".." 650 * that point backwards in the directory structure. 651 */ 652 pdp = vdp; 653 if (flags & ISDOTDOT) { 654 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp); 655 if (VN_IS_DOOMED(pdp)) { 656 if (error == 0) 657 vput(tdp); 658 error = ENOENT; 659 } 660 if (error) 661 return (error); 662 /* 663 * Recheck that ".." entry in the vdp directory points 664 * to the inode we looked up before vdp lock was 665 * dropped. 666 */ 667 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1); 668 if (error) { 669 vput(tdp); 670 return (error); 671 } 672 if (ino1 != ino) { 673 vput(tdp); 674 goto restart; 675 } 676 *vpp = tdp; 677 } else if (dp->i_number == ino) { 678 VREF(vdp); /* we want ourself, ie "." */ 679 /* 680 * When we lookup "." we still can be asked to lock it 681 * differently. 682 */ 683 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 684 if (ltype != VOP_ISLOCKED(vdp)) { 685 if (ltype == LK_EXCLUSIVE) 686 vn_lock(vdp, LK_UPGRADE | LK_RETRY); 687 else /* if (ltype == LK_SHARED) */ 688 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY); 689 } 690 *vpp = vdp; 691 } else { 692 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags, 693 &tdp)) != 0) 694 return (error); 695 *vpp = tdp; 696 } 697 698 /* 699 * Insert name into cache if appropriate. 700 */ 701 if (cnp->cn_flags & MAKEENTRY) 702 cache_enter(vdp, *vpp, cnp); 703 return (0); 704 } 705 706 int 707 ext2_search_dirblock(struct inode *ip, void *data, int *foundp, 708 const char *name, int namelen, int *entryoffsetinblockp, 709 doff_t *offp, doff_t *prevoffp, doff_t *endusefulp, 710 struct ext2fs_searchslot *ssp) 711 { 712 struct vnode *vdp; 713 struct ext2fs_direct_2 *ep, *top; 714 uint32_t bsize = ip->i_e2fs->e2fs_bsize; 715 int offset = *entryoffsetinblockp; 716 int namlen; 717 718 vdp = ITOV(ip); 719 720 ep = (struct ext2fs_direct_2 *)((char *)data + offset); 721 top = (struct ext2fs_direct_2 *)((char *)data + bsize); 722 while (ep < top) { 723 if (ext2_check_direntry(vdp, ep, offset)) { 724 int i; 725 726 ext2_dirbad(ip, *offp, "mangled entry"); 727 i = bsize - (offset & (bsize - 1)); 728 *offp += i; 729 offset += i; 730 ep = (struct ext2fs_direct_2 *)((char *)data + offset); 731 continue; 732 } 733 734 /* 735 * If an appropriate sized slot has not yet been found, 736 * check to see if one is available. Also accumulate space 737 * in the current block so that we can determine if 738 * compaction is viable. 739 */ 740 if (ssp->slotstatus != FOUND) { 741 int size = le16toh(ep->e2d_reclen); 742 743 if (ep->e2d_ino != 0) 744 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen); 745 else if (ext2_is_dirent_tail(ip, ep)) 746 size -= sizeof(struct ext2fs_direct_tail); 747 if (size > 0) { 748 if (size >= ssp->slotneeded) { 749 ssp->slotstatus = FOUND; 750 ssp->slotoffset = *offp; 751 ssp->slotsize = le16toh(ep->e2d_reclen); 752 } else if (ssp->slotstatus == NONE) { 753 ssp->slotfreespace += size; 754 if (ssp->slotoffset == -1) 755 ssp->slotoffset = *offp; 756 if (ssp->slotfreespace >= ssp->slotneeded) { 757 ssp->slotstatus = COMPACT; 758 ssp->slotsize = *offp + 759 le16toh(ep->e2d_reclen) - 760 ssp->slotoffset; 761 } 762 } 763 } 764 } 765 /* 766 * Check for a name match. 767 */ 768 if (ep->e2d_ino != 0) { 769 namlen = ep->e2d_namlen; 770 if (namlen == namelen && 771 !bcmp(name, ep->e2d_name, (unsigned)namlen)) { 772 /* 773 * Save directory entry's inode number and 774 * reclen in ndp->ni_ufs area, and release 775 * directory buffer. 776 */ 777 *foundp = 1; 778 return (0); 779 } 780 } 781 *prevoffp = *offp; 782 *offp += le16toh(ep->e2d_reclen); 783 offset += le16toh(ep->e2d_reclen); 784 *entryoffsetinblockp = offset; 785 if (ep->e2d_ino != 0) 786 *endusefulp = *offp; 787 /* 788 * Get pointer to the next entry. 789 */ 790 ep = (struct ext2fs_direct_2 *)((char *)data + offset); 791 } 792 793 return (0); 794 } 795 796 void 797 ext2_dirbad(struct inode *ip, doff_t offset, char *how) 798 { 799 800 SDT_PROBE4(ext2fs, , trace, ext2_dirbad_error, 801 ITOV(ip)->v_mount->mnt_stat.f_mntonname, ip->i_number, offset, how); 802 } 803 804 /* 805 * Do consistency checking on a directory entry: 806 * record length must be multiple of 4 807 * entry must fit in rest of its DIRBLKSIZ block 808 * record must be large enough to contain entry 809 * name is not longer than MAXNAMLEN 810 * name must be as long as advertised, and null terminated 811 * inode number less then inode count 812 * if root inode entry, it have correct name 813 */ 814 static int 815 ext2_check_direntry(struct vnode *dp, struct ext2fs_direct_2 *de, 816 int entryoffsetinblock) 817 { 818 struct m_ext2fs *fs = VTOI(dp)->i_e2fs; 819 char *error_msg = NULL; 820 821 if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(1)) 822 error_msg = "rec_len is smaller than minimal"; 823 else if (le16toh(de->e2d_reclen) % 4 != 0) 824 error_msg = "rec_len % 4 != 0"; 825 else if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(de->e2d_namlen)) 826 error_msg = "reclen is too small for name_len"; 827 else if (entryoffsetinblock + le16toh(de->e2d_reclen)> fs->e2fs_bsize) 828 error_msg = "directory entry across blocks"; 829 else if (le32toh(de->e2d_ino) > fs->e2fs->e2fs_icount) 830 error_msg = "directory entry inode out of bounds"; 831 else if (le32toh(de->e2d_ino) == EXT2_ROOTINO && 832 ((de->e2d_namlen != 1 && de->e2d_namlen != 2) || 833 (de->e2d_name[0] != '.') || 834 (de->e2d_namlen == 2 && de->e2d_name[1] != '.'))) 835 error_msg = "bad root directory entry"; 836 837 if (error_msg != NULL) { 838 SDT_PROBE5(ext2fs, , trace, ext2_dirbadentry_error, 839 error_msg, entryoffsetinblock, 840 le32toh(de->e2d_ino), le16toh(de->e2d_reclen), 841 de->e2d_namlen); 842 } 843 return (error_msg == NULL ? 0 : EINVAL); 844 } 845 846 /* 847 * Insert an entry into the fresh directory block. 848 * Initialize entry tail if the metadata_csum feature is turned on. 849 */ 850 static int 851 ext2_add_first_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry, 852 struct componentname *cnp) 853 { 854 struct inode *dp; 855 struct iovec aiov; 856 struct uio auio; 857 char* buf = NULL; 858 int dirblksize, error; 859 860 dp = VTOI(dvp); 861 dirblksize = dp->i_e2fs->e2fs_bsize; 862 863 if (dp->i_offset & (dirblksize - 1)) 864 panic("ext2_add_first_entry: bad directory offset"); 865 866 if (EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs, 867 EXT2F_ROCOMPAT_METADATA_CKSUM)) { 868 entry->e2d_reclen = htole16(dirblksize - 869 sizeof(struct ext2fs_direct_tail)); 870 buf = malloc(dirblksize, M_TEMP, M_WAITOK); 871 memcpy(buf, entry, EXT2_DIR_REC_LEN(entry->e2d_namlen)); 872 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, dirblksize)); 873 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf); 874 875 auio.uio_offset = dp->i_offset; 876 auio.uio_resid = dirblksize; 877 aiov.iov_len = auio.uio_resid; 878 aiov.iov_base = (caddr_t)buf; 879 } else { 880 entry->e2d_reclen = htole16(dirblksize); 881 auio.uio_offset = dp->i_offset; 882 auio.uio_resid = EXT2_DIR_REC_LEN(entry->e2d_namlen); 883 aiov.iov_len = auio.uio_resid; 884 aiov.iov_base = (caddr_t)entry; 885 } 886 887 auio.uio_iov = &aiov; 888 auio.uio_iovcnt = 1; 889 auio.uio_rw = UIO_WRITE; 890 auio.uio_segflg = UIO_SYSSPACE; 891 auio.uio_td = (struct thread *)0; 892 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred); 893 if (error) 894 goto out; 895 896 dp->i_size = roundup2(dp->i_size, dirblksize); 897 dp->i_flag |= IN_CHANGE; 898 899 out: 900 free(buf, M_TEMP); 901 return (error); 902 903 } 904 905 /* 906 * Write a directory entry after a call to namei, using the parameters 907 * that it left in nameidata. The argument ip is the inode which the new 908 * directory entry will refer to. Dvp is a pointer to the directory to 909 * be written, which was left locked by namei. Remaining parameters 910 * (dp->i_offset, dp->i_count) indicate how the space for the new 911 * entry is to be obtained. 912 */ 913 int 914 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp) 915 { 916 struct inode *dp; 917 struct ext2fs_direct_2 newdir; 918 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize; 919 int error; 920 921 dp = VTOI(dvp); 922 newdir.e2d_ino = htole32(ip->i_number); 923 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs, 924 EXT2F_INCOMPAT_FTYPE)) { 925 newdir.e2d_namlen = cnp->cn_namelen; 926 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode)); 927 } else 928 newdir.e2d_namlen = htole16(cnp->cn_namelen); 929 930 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1); 931 932 if (ext2_htree_has_idx(dp)) { 933 error = ext2_htree_add_entry(dvp, &newdir, cnp); 934 if (error) { 935 dp->i_flag &= ~IN_E3INDEX; 936 dp->i_flag |= IN_CHANGE | IN_UPDATE; 937 } 938 return (error); 939 } 940 941 if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) && 942 !ext2_htree_has_idx(dp)) { 943 if ((dp->i_size / DIRBLKSIZ) == 1 && 944 dp->i_offset == DIRBLKSIZ) { 945 /* 946 * Making indexed directory when one block is not 947 * enough to save all entries. 948 */ 949 return ext2_htree_create_index(dvp, cnp, &newdir); 950 } 951 } 952 953 /* 954 * If dp->i_count is 0, then namei could find no 955 * space in the directory. Here, dp->i_offset will 956 * be on a directory block boundary and we will write the 957 * new entry into a fresh block. 958 */ 959 if (dp->i_count == 0) 960 return ext2_add_first_entry(dvp, &newdir, cnp); 961 962 error = ext2_add_entry(dvp, &newdir); 963 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size) 964 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC, 965 cnp->cn_cred, curthread); 966 return (error); 967 } 968 969 /* 970 * Insert an entry into the directory block. 971 * Compact the contents. 972 */ 973 int 974 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry) 975 { 976 struct ext2fs_direct_2 *ep, *nep; 977 struct inode *dp; 978 struct buf *bp; 979 u_int dsize; 980 int error, loc, newentrysize, spacefree; 981 char *dirbuf; 982 983 dp = VTOI(dvp); 984 985 /* 986 * If dp->i_count is non-zero, then namei found space 987 * for the new entry in the range dp->i_offset to 988 * dp->i_offset + dp->i_count in the directory. 989 * To use this space, we may have to compact the entries located 990 * there, by copying them together towards the beginning of the 991 * block, leaving the free space in one usable chunk at the end. 992 */ 993 994 /* 995 * Increase size of directory if entry eats into new space. 996 * This should never push the size past a new multiple of 997 * DIRBLKSIZE. 998 * 999 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 1000 */ 1001 if (dp->i_offset + dp->i_count > dp->i_size) 1002 dp->i_size = dp->i_offset + dp->i_count; 1003 /* 1004 * Get the block containing the space for the new directory entry. 1005 */ 1006 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf, 1007 &bp)) != 0) 1008 return (error); 1009 /* 1010 * Find space for the new entry. In the simple case, the entry at 1011 * offset base will have the space. If it does not, then namei 1012 * arranged that compacting the region dp->i_offset to 1013 * dp->i_offset + dp->i_count would yield the 1014 * space. 1015 */ 1016 newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen); 1017 ep = (struct ext2fs_direct_2 *)dirbuf; 1018 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen); 1019 spacefree = le16toh(ep->e2d_reclen) - dsize; 1020 for (loc = le16toh(ep->e2d_reclen); loc < dp->i_count; ) { 1021 nep = (struct ext2fs_direct_2 *)(dirbuf + loc); 1022 if (le32toh(ep->e2d_ino)) { 1023 /* trim the existing slot */ 1024 ep->e2d_reclen = htole16(dsize); 1025 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize); 1026 } else { 1027 /* overwrite; nothing there; header is ours */ 1028 spacefree += dsize; 1029 } 1030 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen); 1031 spacefree += le16toh(nep->e2d_reclen) - dsize; 1032 loc += le16toh(nep->e2d_reclen); 1033 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 1034 } 1035 /* 1036 * Update the pointer fields in the previous entry (if any), 1037 * copy in the new entry, and write out the block. 1038 */ 1039 if (ep->e2d_ino == 0) { 1040 if (spacefree + dsize < newentrysize) 1041 panic("ext2_direnter: compact1"); 1042 entry->e2d_reclen = htole16(spacefree + dsize); 1043 } else { 1044 if (spacefree < newentrysize) 1045 panic("ext2_direnter: compact2"); 1046 entry->e2d_reclen = htole16(spacefree); 1047 ep->e2d_reclen = htole16(dsize); 1048 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize); 1049 } 1050 bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize); 1051 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data); 1052 if (DOINGASYNC(dvp)) { 1053 bdwrite(bp); 1054 error = 0; 1055 } else { 1056 error = bwrite(bp); 1057 } 1058 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1059 return (error); 1060 } 1061 1062 /* 1063 * Remove a directory entry after a call to namei, using 1064 * the parameters which it left in nameidata. The entry 1065 * dp->i_offset contains the offset into the directory of the 1066 * entry to be eliminated. The dp->i_count field contains the 1067 * size of the previous record in the directory. If this 1068 * is 0, the first entry is being deleted, so we need only 1069 * zero the inode number to mark the entry as free. If the 1070 * entry is not the first in the directory, we must reclaim 1071 * the space of the now empty record by adding the record size 1072 * to the size of the previous entry. 1073 */ 1074 int 1075 ext2_dirremove(struct vnode *dvp, struct componentname *cnp) 1076 { 1077 struct inode *dp; 1078 struct ext2fs_direct_2 *ep, *rep; 1079 struct buf *bp; 1080 int error; 1081 1082 dp = VTOI(dvp); 1083 if (dp->i_count == 0) { 1084 /* 1085 * First entry in block: set d_ino to zero. 1086 */ 1087 if ((error = 1088 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep, 1089 &bp)) != 0) 1090 return (error); 1091 ep->e2d_ino = 0; 1092 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data); 1093 error = bwrite(bp); 1094 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1095 return (error); 1096 } 1097 /* 1098 * Collapse new free space into previous entry. 1099 */ 1100 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count), 1101 (char **)&ep, &bp)) != 0) 1102 return (error); 1103 1104 /* Set 'rep' to the entry being removed. */ 1105 if (dp->i_count == 0) 1106 rep = ep; 1107 else 1108 rep = (struct ext2fs_direct_2 *)((char *)ep + 1109 le16toh(ep->e2d_reclen)); 1110 ep->e2d_reclen += rep->e2d_reclen; 1111 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data); 1112 if (DOINGASYNC(dvp) && dp->i_count != 0) 1113 bdwrite(bp); 1114 else 1115 error = bwrite(bp); 1116 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1117 return (error); 1118 } 1119 1120 /* 1121 * Rewrite an existing directory entry to point at the inode 1122 * supplied. The parameters describing the directory entry are 1123 * set up by a call to namei. 1124 */ 1125 int 1126 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp) 1127 { 1128 struct buf *bp; 1129 struct ext2fs_direct_2 *ep; 1130 struct vnode *vdp = ITOV(dp); 1131 int error; 1132 1133 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep, 1134 &bp)) != 0) 1135 return (error); 1136 ep->e2d_ino = htole32(ip->i_number); 1137 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs, 1138 EXT2F_INCOMPAT_FTYPE)) 1139 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode)); 1140 else 1141 ep->e2d_type = EXT2_FT_UNKNOWN; 1142 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data); 1143 error = bwrite(bp); 1144 dp->i_flag |= IN_CHANGE | IN_UPDATE; 1145 return (error); 1146 } 1147 1148 /* 1149 * Check if a directory is empty or not. 1150 * Inode supplied must be locked. 1151 * 1152 * Using a struct dirtemplate here is not precisely 1153 * what we want, but better than using a struct direct. 1154 * 1155 * NB: does not handle corrupted directories. 1156 */ 1157 int 1158 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred) 1159 { 1160 off_t off; 1161 struct dirtemplate dbuf; 1162 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf; 1163 int error, namlen; 1164 ssize_t count; 1165 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2) 1166 1167 for (off = 0; off < ip->i_size; off += le16toh(dp->e2d_reclen)) { 1168 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, 1169 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred, 1170 NOCRED, &count, (struct thread *)0); 1171 /* 1172 * Since we read MINDIRSIZ, residual must 1173 * be 0 unless we're at end of file. 1174 */ 1175 if (error || count != 0) 1176 return (0); 1177 /* avoid infinite loops */ 1178 if (dp->e2d_reclen == 0) 1179 return (0); 1180 /* skip empty entries */ 1181 if (dp->e2d_ino == 0) 1182 continue; 1183 /* accept only "." and ".." */ 1184 namlen = dp->e2d_namlen; 1185 if (namlen > 2) 1186 return (0); 1187 if (dp->e2d_name[0] != '.') 1188 return (0); 1189 /* 1190 * At this point namlen must be 1 or 2. 1191 * 1 implies ".", 2 implies ".." if second 1192 * char is also "." 1193 */ 1194 if (namlen == 1) 1195 continue; 1196 if (dp->e2d_name[1] == '.' && le32toh(dp->e2d_ino) == parentino) 1197 continue; 1198 return (0); 1199 } 1200 return (1); 1201 } 1202 1203 /* 1204 * Check if source directory is in the path of the target directory. 1205 * Target is supplied locked, source is unlocked. 1206 * The target is always vput before returning. 1207 */ 1208 int 1209 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred) 1210 { 1211 struct vnode *vp; 1212 int error, namlen; 1213 struct dirtemplate dirbuf; 1214 1215 vp = ITOV(target); 1216 if (target->i_number == source->i_number) { 1217 error = EEXIST; 1218 goto out; 1219 } 1220 if (target->i_number == EXT2_ROOTINO) { 1221 error = 0; 1222 goto out; 1223 } 1224 1225 for (;;) { 1226 if (vp->v_type != VDIR) { 1227 error = ENOTDIR; 1228 break; 1229 } 1230 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 1231 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE, 1232 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, 1233 NULL); 1234 if (error != 0) 1235 break; 1236 namlen = dirbuf.dotdot_type; /* like ufs little-endian */ 1237 if (namlen != 2 || 1238 dirbuf.dotdot_name[0] != '.' || 1239 dirbuf.dotdot_name[1] != '.') { 1240 error = ENOTDIR; 1241 break; 1242 } 1243 if (le32toh(dirbuf.dotdot_ino) == source->i_number) { 1244 error = EINVAL; 1245 break; 1246 } 1247 if (le32toh(dirbuf.dotdot_ino) == EXT2_ROOTINO) 1248 break; 1249 vput(vp); 1250 if ((error = VFS_VGET(vp->v_mount, le32toh(dirbuf.dotdot_ino), 1251 LK_EXCLUSIVE, &vp)) != 0) { 1252 vp = NULL; 1253 break; 1254 } 1255 } 1256 1257 out: 1258 if (error == ENOTDIR) 1259 SDT_PROBE2(ext2fs, , lookup, trace, 1, 1260 "checkpath: .. not a directory"); 1261 if (vp != NULL) 1262 vput(vp); 1263 return (error); 1264 } 1265