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