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