ext2_lookup.c revision 252677
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: stable/9/sys/fs/ext2fs/ext2_lookup.c 252677 2013-07-04 01:40:32Z pfg $ 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 INVARIANTS 66static int dirchk = 1; 67#else 68static int dirchk = 0; 69#endif 70 71static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem"); 72SYSCTL_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 81static 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 94static 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 114static int ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de, 115 int entryoffsetinblock); 116static 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 */ 131int 132ext2_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 */ 275int 276ext2_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 282static int 283ext2_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 doff_t i_diroff; /* cached i_diroff value */ 293 doff_t i_offset; /* cached i_offset value */ 294 int slotsize; /* size of area at slotoffset */ 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; 318restart: 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 374searchloop: 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 if (bp != NULL) 480 brelse(bp); 481 /* 482 * If creating, and at end of pathname and current 483 * directory has not been removed, then can consider 484 * allowing file to be created. 485 */ 486 if ((nameiop == CREATE || nameiop == RENAME) && 487 (flags & ISLASTCN) && dp->i_nlink != 0) { 488 /* 489 * Access for write is interpreted as allowing 490 * creation of files in the directory. 491 */ 492 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0) 493 return (error); 494 /* 495 * Return an indication of where the new directory 496 * entry should be put. If we didn't find a slot, 497 * then set dp->i_count to 0 indicating 498 * that the new slot belongs at the end of the 499 * directory. If we found a slot, then the new entry 500 * can be put in the range from dp->i_offset to 501 * dp->i_offset + dp->i_count. 502 */ 503 if (slotstatus == NONE) { 504 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ); 505 dp->i_count = 0; 506 enduseful = dp->i_offset; 507 } else { 508 dp->i_offset = slotoffset; 509 dp->i_count = slotsize; 510 if (enduseful < slotoffset + slotsize) 511 enduseful = slotoffset + slotsize; 512 } 513 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ); 514 /* 515 * We return with the directory locked, so that 516 * the parameters we set up above will still be 517 * valid if we actually decide to do a direnter(). 518 * We return ni_vp == NULL to indicate that the entry 519 * does not currently exist; we leave a pointer to 520 * the (locked) directory inode in ndp->ni_dvp. 521 * The pathname buffer is saved so that the name 522 * can be obtained later. 523 * 524 * NB - if the directory is unlocked, then this 525 * information cannot be used. 526 */ 527 cnp->cn_flags |= SAVENAME; 528 return (EJUSTRETURN); 529 } 530 /* 531 * Insert name into cache (as non-existent) if appropriate. 532 */ 533 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 534 cache_enter(vdp, NULL, cnp); 535 return (ENOENT); 536 537found: 538 if (dd_ino != NULL) 539 *dd_ino = ino; 540 if (numdirpasses == 2) 541 nchstats.ncs_pass2++; 542 /* 543 * Check that directory length properly reflects presence 544 * of this entry. 545 */ 546 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen) 547 > dp->i_size) { 548 ext2_dirbad(dp, i_offset, "i_size too small"); 549 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->e2d_namlen); 550 dp->i_flag |= IN_CHANGE | IN_UPDATE; 551 } 552 brelse(bp); 553 554 /* 555 * Found component in pathname. 556 * If the final component of path name, save information 557 * in the cache as to where the entry was found. 558 */ 559 if ((flags & ISLASTCN) && nameiop == LOOKUP) 560 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1); 561 /* 562 * If deleting, and at end of pathname, return 563 * parameters which can be used to remove file. 564 */ 565 if (nameiop == DELETE && (flags & ISLASTCN)) { 566 if (flags & LOCKPARENT) 567 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__); 568 /* 569 * Write access to directory required to delete files. 570 */ 571 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0) 572 return (error); 573 /* 574 * Return pointer to current entry in dp->i_offset, 575 * and distance past previous entry (if there 576 * is a previous entry in this block) in dp->i_count. 577 * Save directory inode pointer in ndp->ni_dvp for dirremove(). 578 * 579 * Technically we shouldn't be setting these in the 580 * WANTPARENT case (first lookup in rename()), but any 581 * lookups that will result in directory changes will 582 * overwrite these. 583 */ 584 dp->i_offset = i_offset; 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 (dd_ino != NULL) 590 return (0); 591 if (dp->i_number == ino) { 592 VREF(vdp); 593 *vpp = vdp; 594 return (0); 595 } 596 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE, 597 &tdp)) != 0) 598 return (error); 599 /* 600 * If directory is "sticky", then user must own 601 * the directory, or the file in it, else she 602 * may not delete it (unless she's root). This 603 * implements append-only directories. 604 */ 605 if ((dp->i_mode & ISVTX) && 606 cred->cr_uid != 0 && 607 cred->cr_uid != dp->i_uid && 608 VTOI(tdp)->i_uid != cred->cr_uid) { 609 vput(tdp); 610 return (EPERM); 611 } 612 *vpp = tdp; 613 return (0); 614 } 615 616 /* 617 * If rewriting (RENAME), return the inode and the 618 * information required to rewrite the present directory 619 * Must get inode of directory entry to verify it's a 620 * regular file, or empty directory. 621 */ 622 if (nameiop == RENAME && (flags & ISLASTCN)) { 623 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0) 624 return (error); 625 /* 626 * Careful about locking second inode. 627 * This can only occur if the target is ".". 628 */ 629 dp->i_offset = i_offset; 630 if (dp->i_number == ino) 631 return (EISDIR); 632 if (dd_ino != NULL) 633 return (0); 634 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE, 635 &tdp)) != 0) 636 return (error); 637 *vpp = tdp; 638 cnp->cn_flags |= SAVENAME; 639 return (0); 640 } 641 if (dd_ino != NULL) 642 return (0); 643 644 /* 645 * Step through the translation in the name. We do not `vput' the 646 * directory because we may need it again if a symbolic link 647 * is relative to the current directory. Instead we save it 648 * unlocked as "pdp". We must get the target inode before unlocking 649 * the directory to insure that the inode will not be removed 650 * before we get it. We prevent deadlock by always fetching 651 * inodes from the root, moving down the directory tree. Thus 652 * when following backward pointers ".." we must unlock the 653 * parent directory before getting the requested directory. 654 * There is a potential race condition here if both the current 655 * and parent directories are removed before the VFS_VGET for the 656 * inode associated with ".." returns. We hope that this occurs 657 * infrequently since we cannot avoid this race condition without 658 * implementing a sophisticated deadlock detection algorithm. 659 * Note also that this simple deadlock detection scheme will not 660 * work if the file system has any hard links other than ".." 661 * that point backwards in the directory structure. 662 */ 663 pdp = vdp; 664 if (flags & ISDOTDOT) { 665 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp); 666 if (pdp->v_iflag & VI_DOOMED) { 667 if (error == 0) 668 vput(tdp); 669 error = ENOENT; 670 } 671 if (error) 672 return (error); 673 /* 674 * Recheck that ".." entry in the vdp directory points 675 * to the inode we looked up before vdp lock was 676 * dropped. 677 */ 678 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1); 679 if (error) { 680 vput(tdp); 681 return (error); 682 } 683 if (ino1 != ino) { 684 vput(tdp); 685 goto restart; 686 } 687 *vpp = tdp; 688 } else if (dp->i_number == ino) { 689 VREF(vdp); /* we want ourself, ie "." */ 690 /* 691 * When we lookup "." we still can be asked to lock it 692 * differently. 693 */ 694 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 695 if (ltype != VOP_ISLOCKED(vdp)) { 696 if (ltype == LK_EXCLUSIVE) 697 vn_lock(vdp, LK_UPGRADE | LK_RETRY); 698 else /* if (ltype == LK_SHARED) */ 699 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY); 700 } 701 *vpp = vdp; 702 } else { 703 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags, 704 &tdp)) != 0) 705 return (error); 706 *vpp = tdp; 707 } 708 709 /* 710 * Insert name into cache if appropriate. 711 */ 712 if (cnp->cn_flags & MAKEENTRY) 713 cache_enter(vdp, *vpp, cnp); 714 return (0); 715} 716 717void 718ext2_dirbad(struct inode *ip, doff_t offset, char *how) 719{ 720 struct mount *mp; 721 722 mp = ITOV(ip)->v_mount; 723 if ((mp->mnt_flag & MNT_RDONLY) == 0) 724 panic("ext2_dirbad: %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 else 727 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n", 728 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how); 729 730} 731 732/* 733 * Do consistency checking on a directory entry: 734 * record length must be multiple of 4 735 * entry must fit in rest of its DIRBLKSIZ block 736 * record must be large enough to contain entry 737 * name is not longer than MAXNAMLEN 738 * name must be as long as advertised, and null terminated 739 */ 740/* 741 * changed so that it confirms to ext2_check_dir_entry 742 */ 743static int 744ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de, 745 int entryoffsetinblock) 746{ 747 int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize; 748 749 char * error_msg = NULL; 750 751 if (de->e2d_reclen < EXT2_DIR_REC_LEN(1)) 752 error_msg = "rec_len is smaller than minimal"; 753 else if (de->e2d_reclen % 4 != 0) 754 error_msg = "rec_len % 4 != 0"; 755 else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen)) 756 error_msg = "reclen is too small for name_len"; 757 else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ) 758 error_msg = "directory entry across blocks"; 759 /* else LATER 760 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count) 761 error_msg = "inode out of bounds"; 762 */ 763 764 if (error_msg != NULL) { 765 printf("bad directory entry: %s\n", error_msg); 766 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n", 767 entryoffsetinblock, (unsigned long)de->e2d_ino, 768 de->e2d_reclen, de->e2d_namlen); 769 } 770 return error_msg == NULL ? 0 : 1; 771} 772 773/* 774 * Write a directory entry after a call to namei, using the parameters 775 * that it left in nameidata. The argument ip is the inode which the new 776 * directory entry will refer to. Dvp is a pointer to the directory to 777 * be written, which was left locked by namei. Remaining parameters 778 * (dp->i_offset, dp->i_count) indicate how the space for the new 779 * entry is to be obtained. 780 */ 781int 782ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp) 783{ 784 struct ext2fs_direct_2 *ep, *nep; 785 struct inode *dp; 786 struct buf *bp; 787 struct ext2fs_direct_2 newdir; 788 struct iovec aiov; 789 struct uio auio; 790 u_int dsize; 791 int error, loc, newentrysize, spacefree; 792 char *dirbuf; 793 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize; 794 795 796#ifdef INVARIANTS 797 if ((cnp->cn_flags & SAVENAME) == 0) 798 panic("ext2_direnter: missing name"); 799#endif 800 dp = VTOI(dvp); 801 newdir.e2d_ino = ip->i_number; 802 newdir.e2d_namlen = cnp->cn_namelen; 803 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs, 804 EXT2F_INCOMPAT_FTYPE)) 805 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode)); 806 else 807 newdir.e2d_type = EXT2_FT_UNKNOWN; 808 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1); 809 newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen); 810 if (dp->i_count == 0) { 811 /* 812 * If dp->i_count is 0, then namei could find no 813 * space in the directory. Here, dp->i_offset will 814 * be on a directory block boundary and we will write the 815 * new entry into a fresh block. 816 */ 817 if (dp->i_offset & (DIRBLKSIZ - 1)) 818 panic("ext2_direnter: newblk"); 819 auio.uio_offset = dp->i_offset; 820 newdir.e2d_reclen = DIRBLKSIZ; 821 auio.uio_resid = newentrysize; 822 aiov.iov_len = newentrysize; 823 aiov.iov_base = (caddr_t)&newdir; 824 auio.uio_iov = &aiov; 825 auio.uio_iovcnt = 1; 826 auio.uio_rw = UIO_WRITE; 827 auio.uio_segflg = UIO_SYSSPACE; 828 auio.uio_td = (struct thread *)0; 829 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred); 830 if (DIRBLKSIZ > 831 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 832 /* XXX should grow with balloc() */ 833 panic("ext2_direnter: frag size"); 834 else if (!error) { 835 dp->i_size = roundup2(dp->i_size, DIRBLKSIZ); 836 dp->i_flag |= IN_CHANGE; 837 } 838 return (error); 839 } 840 841 /* 842 * If dp->i_count is non-zero, then namei found space 843 * for the new entry in the range dp->i_offset to 844 * dp->i_offset + dp->i_count in the directory. 845 * To use this space, we may have to compact the entries located 846 * there, by copying them together towards the beginning of the 847 * block, leaving the free space in one usable chunk at the end. 848 */ 849 850 /* 851 * Increase size of directory if entry eats into new space. 852 * This should never push the size past a new multiple of 853 * DIRBLKSIZE. 854 * 855 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 856 */ 857 if (dp->i_offset + dp->i_count > dp->i_size) 858 dp->i_size = dp->i_offset + dp->i_count; 859 /* 860 * Get the block containing the space for the new directory entry. 861 */ 862 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf, 863 &bp)) != 0) 864 return (error); 865 /* 866 * Find space for the new entry. In the simple case, the entry at 867 * offset base will have the space. If it does not, then namei 868 * arranged that compacting the region dp->i_offset to 869 * dp->i_offset + dp->i_count would yield the 870 * space. 871 */ 872 ep = (struct ext2fs_direct_2 *)dirbuf; 873 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen); 874 spacefree = ep->e2d_reclen - dsize; 875 for (loc = ep->e2d_reclen; loc < dp->i_count; ) { 876 nep = (struct ext2fs_direct_2 *)(dirbuf + loc); 877 if (ep->e2d_ino) { 878 /* trim the existing slot */ 879 ep->e2d_reclen = dsize; 880 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize); 881 } else { 882 /* overwrite; nothing there; header is ours */ 883 spacefree += dsize; 884 } 885 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen); 886 spacefree += nep->e2d_reclen - dsize; 887 loc += nep->e2d_reclen; 888 bcopy((caddr_t)nep, (caddr_t)ep, dsize); 889 } 890 /* 891 * Update the pointer fields in the previous entry (if any), 892 * copy in the new entry, and write out the block. 893 */ 894 if (ep->e2d_ino == 0) { 895 if (spacefree + dsize < newentrysize) 896 panic("ext2_direnter: compact1"); 897 newdir.e2d_reclen = spacefree + dsize; 898 } else { 899 if (spacefree < newentrysize) 900 panic("ext2_direnter: compact2"); 901 newdir.e2d_reclen = spacefree; 902 ep->e2d_reclen = dsize; 903 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize); 904 } 905 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize); 906 if (DOINGASYNC(dvp)) { 907 bdwrite(bp); 908 error = 0; 909 } else { 910 error = bwrite(bp); 911 } 912 dp->i_flag |= IN_CHANGE | IN_UPDATE; 913 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size) 914 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC, 915 cnp->cn_cred, cnp->cn_thread); 916 return (error); 917} 918 919/* 920 * Remove a directory entry after a call to namei, using 921 * the parameters which it left in nameidata. The entry 922 * dp->i_offset contains the offset into the directory of the 923 * entry to be eliminated. The dp->i_count field contains the 924 * size of the previous record in the directory. If this 925 * is 0, the first entry is being deleted, so we need only 926 * zero the inode number to mark the entry as free. If the 927 * entry is not the first in the directory, we must reclaim 928 * the space of the now empty record by adding the record size 929 * to the size of the previous entry. 930 */ 931int 932ext2_dirremove(struct vnode *dvp, struct componentname *cnp) 933{ 934 struct inode *dp; 935 struct ext2fs_direct_2 *ep, *rep; 936 struct buf *bp; 937 int error; 938 939 dp = VTOI(dvp); 940 if (dp->i_count == 0) { 941 /* 942 * First entry in block: set d_ino to zero. 943 */ 944 if ((error = 945 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep, 946 &bp)) != 0) 947 return (error); 948 ep->e2d_ino = 0; 949 error = bwrite(bp); 950 dp->i_flag |= IN_CHANGE | IN_UPDATE; 951 return (error); 952 } 953 /* 954 * Collapse new free space into previous entry. 955 */ 956 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count), 957 (char **)&ep, &bp)) != 0) 958 return (error); 959 960 /* Set 'rep' to the entry being removed. */ 961 if (dp->i_count == 0) 962 rep = ep; 963 else 964 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen); 965 ep->e2d_reclen += rep->e2d_reclen; 966 if (DOINGASYNC(dvp) && dp->i_count != 0) 967 bdwrite(bp); 968 else 969 error = bwrite(bp); 970 dp->i_flag |= IN_CHANGE | IN_UPDATE; 971 return (error); 972} 973 974/* 975 * Rewrite an existing directory entry to point at the inode 976 * supplied. The parameters describing the directory entry are 977 * set up by a call to namei. 978 */ 979int 980ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp) 981{ 982 struct buf *bp; 983 struct ext2fs_direct_2 *ep; 984 struct vnode *vdp = ITOV(dp); 985 int error; 986 987 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep, 988 &bp)) != 0) 989 return (error); 990 ep->e2d_ino = ip->i_number; 991 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs, 992 EXT2F_INCOMPAT_FTYPE)) 993 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode)); 994 else 995 ep->e2d_type = EXT2_FT_UNKNOWN; 996 error = bwrite(bp); 997 dp->i_flag |= IN_CHANGE | IN_UPDATE; 998 return (error); 999} 1000 1001/* 1002 * Check if a directory is empty or not. 1003 * Inode supplied must be locked. 1004 * 1005 * Using a struct dirtemplate here is not precisely 1006 * what we want, but better than using a struct direct. 1007 * 1008 * NB: does not handle corrupted directories. 1009 */ 1010int 1011ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred) 1012{ 1013 off_t off; 1014 struct dirtemplate dbuf; 1015 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf; 1016 int error, namlen; 1017 ssize_t count; 1018#define MINDIRSIZ (sizeof(struct dirtemplate) / 2) 1019 1020 for (off = 0; off < ip->i_size; off += dp->e2d_reclen) { 1021 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, 1022 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred, 1023 NOCRED, &count, (struct thread *)0); 1024 /* 1025 * Since we read MINDIRSIZ, residual must 1026 * be 0 unless we're at end of file. 1027 */ 1028 if (error || count != 0) 1029 return (0); 1030 /* avoid infinite loops */ 1031 if (dp->e2d_reclen == 0) 1032 return (0); 1033 /* skip empty entries */ 1034 if (dp->e2d_ino == 0) 1035 continue; 1036 /* accept only "." and ".." */ 1037 namlen = dp->e2d_namlen; 1038 if (namlen > 2) 1039 return (0); 1040 if (dp->e2d_name[0] != '.') 1041 return (0); 1042 /* 1043 * At this point namlen must be 1 or 2. 1044 * 1 implies ".", 2 implies ".." if second 1045 * char is also "." 1046 */ 1047 if (namlen == 1) 1048 continue; 1049 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino) 1050 continue; 1051 return (0); 1052 } 1053 return (1); 1054} 1055 1056/* 1057 * Check if source directory is in the path of the target directory. 1058 * Target is supplied locked, source is unlocked. 1059 * The target is always vput before returning. 1060 */ 1061int 1062ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred) 1063{ 1064 struct vnode *vp; 1065 int error, namlen; 1066 struct dirtemplate dirbuf; 1067 1068 vp = ITOV(target); 1069 if (target->i_number == source->i_number) { 1070 error = EEXIST; 1071 goto out; 1072 } 1073 if (target->i_number == EXT2_ROOTINO) { 1074 error = 0; 1075 goto out; 1076 } 1077 1078 for (;;) { 1079 if (vp->v_type != VDIR) { 1080 error = ENOTDIR; 1081 break; 1082 } 1083 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, 1084 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE, 1085 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, 1086 NULL); 1087 if (error != 0) 1088 break; 1089 namlen = dirbuf.dotdot_type; /* like ufs little-endian */ 1090 if (namlen != 2 || 1091 dirbuf.dotdot_name[0] != '.' || 1092 dirbuf.dotdot_name[1] != '.') { 1093 error = ENOTDIR; 1094 break; 1095 } 1096 if (dirbuf.dotdot_ino == source->i_number) { 1097 error = EINVAL; 1098 break; 1099 } 1100 if (dirbuf.dotdot_ino == EXT2_ROOTINO) 1101 break; 1102 vput(vp); 1103 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, 1104 LK_EXCLUSIVE, &vp)) != 0) { 1105 vp = NULL; 1106 break; 1107 } 1108 } 1109 1110out: 1111 if (error == ENOTDIR) 1112 printf("checkpath: .. not a directory\n"); 1113 if (vp != NULL) 1114 vput(vp); 1115 return (error); 1116} 1117