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