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