1*65789Smckusick /*- 2*65789Smckusick * Copyright (c) 1989, 1993, 1994 3*65789Smckusick * The Regents of the University of California. All rights reserved. 4*65789Smckusick * 5*65789Smckusick * This code is derived from software contributed to Berkeley 6*65789Smckusick * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 7*65789Smckusick * Support code is derived from software contributed to Berkeley 8*65789Smckusick * by Atsushi Murai (amurai@spec.co.jp). 9*65789Smckusick * 10*65789Smckusick * %sccs.include.redist.c% 11*65789Smckusick * 12*65789Smckusick * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91 13*65789Smckusick * 14*65789Smckusick * @(#)cd9660_lookup.c 8.1 (Berkeley) 01/21/94 15*65789Smckusick */ 16*65789Smckusick 17*65789Smckusick #include <sys/param.h> 18*65789Smckusick #include <sys/namei.h> 19*65789Smckusick #include <sys/buf.h> 20*65789Smckusick #include <sys/file.h> 21*65789Smckusick #include <sys/vnode.h> 22*65789Smckusick #include <sys/mount.h> 23*65789Smckusick 24*65789Smckusick #include <isofs/cd9660/iso.h> 25*65789Smckusick #include <isofs/cd9660/isofs_node.h> 26*65789Smckusick #include <isofs/cd9660/iso_rrip.h> 27*65789Smckusick #include <isofs/cd9660/isofs_rrip.h> 28*65789Smckusick 29*65789Smckusick struct nchstats iso_nchstats; 30*65789Smckusick 31*65789Smckusick /* 32*65789Smckusick * Convert a component of a pathname into a pointer to a locked inode. 33*65789Smckusick * This is a very central and rather complicated routine. 34*65789Smckusick * If the file system is not maintained in a strict tree hierarchy, 35*65789Smckusick * this can result in a deadlock situation (see comments in code below). 36*65789Smckusick * 37*65789Smckusick * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 38*65789Smckusick * whether the name is to be looked up, created, renamed, or deleted. 39*65789Smckusick * When CREATE, RENAME, or DELETE is specified, information usable in 40*65789Smckusick * creating, renaming, or deleting a directory entry may be calculated. 41*65789Smckusick * If flag has LOCKPARENT or'ed into it and the target of the pathname 42*65789Smckusick * exists, lookup returns both the target and its parent directory locked. 43*65789Smckusick * When creating or renaming and LOCKPARENT is specified, the target may 44*65789Smckusick * not be ".". When deleting and LOCKPARENT is specified, the target may 45*65789Smckusick * be "."., but the caller must check to ensure it does an vrele and iput 46*65789Smckusick * instead of two iputs. 47*65789Smckusick * 48*65789Smckusick * Overall outline of ufs_lookup: 49*65789Smckusick * 50*65789Smckusick * check accessibility of directory 51*65789Smckusick * look for name in cache, if found, then if at end of path 52*65789Smckusick * and deleting or creating, drop it, else return name 53*65789Smckusick * search for name in directory, to found or notfound 54*65789Smckusick * notfound: 55*65789Smckusick * if creating, return locked directory, leaving info on available slots 56*65789Smckusick * else return error 57*65789Smckusick * found: 58*65789Smckusick * if at end of path and deleting, return information to allow delete 59*65789Smckusick * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 60*65789Smckusick * inode and return info to allow rewrite 61*65789Smckusick * if not at end, add name to cache; if at end and neither creating 62*65789Smckusick * nor deleting, add name to cache 63*65789Smckusick * 64*65789Smckusick * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 65*65789Smckusick */ 66*65789Smckusick isofs_lookup(ap) 67*65789Smckusick struct vop_lookup_args /* { 68*65789Smckusick struct vnode *a_dvp; 69*65789Smckusick struct vnode **a_vpp; 70*65789Smckusick struct componentname *a_cnp; 71*65789Smckusick } */ *ap; 72*65789Smckusick { 73*65789Smckusick register struct vnode *vdp; /* vnode for directory being searched */ 74*65789Smckusick register struct iso_node *dp; /* inode for directory being searched */ 75*65789Smckusick register struct iso_mnt *imp; /* file system that directory is in */ 76*65789Smckusick struct buf *bp; /* a buffer of directory entries */ 77*65789Smckusick struct iso_directory_record *ep;/* the current directory entry */ 78*65789Smckusick int entryoffsetinblock; /* offset of ep in bp's buffer */ 79*65789Smckusick int saveoffset; /* offset of last directory entry in dir */ 80*65789Smckusick int numdirpasses; /* strategy for directory search */ 81*65789Smckusick doff_t endsearch; /* offset to end directory search */ 82*65789Smckusick struct iso_node *pdp; /* saved dp during symlink work */ 83*65789Smckusick struct iso_node *tdp; /* returned by iget */ 84*65789Smckusick int lockparent; /* 1 => lockparent flag is set */ 85*65789Smckusick int wantparent; /* 1 => wantparent or lockparent flag */ 86*65789Smckusick int error; 87*65789Smckusick ino_t ino = 0; 88*65789Smckusick int reclen; 89*65789Smckusick u_short namelen; 90*65789Smckusick char altname[NAME_MAX]; 91*65789Smckusick int res; 92*65789Smckusick int assoc, len; 93*65789Smckusick char *name; 94*65789Smckusick struct vnode **vpp = ap->a_vpp; 95*65789Smckusick struct componentname *cnp = ap->a_cnp; 96*65789Smckusick struct ucred *cred = cnp->cn_cred; 97*65789Smckusick int flags = cnp->cn_flags; 98*65789Smckusick int nameiop = cnp->cn_nameiop; 99*65789Smckusick 100*65789Smckusick bp = NULL; 101*65789Smckusick *vpp = NULL; 102*65789Smckusick vdp = ap->a_dvp; 103*65789Smckusick dp = VTOI(vdp); 104*65789Smckusick imp = dp->i_mnt; 105*65789Smckusick lockparent = flags & LOCKPARENT; 106*65789Smckusick wantparent = flags & (LOCKPARENT|WANTPARENT); 107*65789Smckusick 108*65789Smckusick /* 109*65789Smckusick * Check accessiblity of directory. 110*65789Smckusick */ 111*65789Smckusick if (vdp->v_type != VDIR) 112*65789Smckusick return (ENOTDIR); 113*65789Smckusick if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) 114*65789Smckusick return (error); 115*65789Smckusick 116*65789Smckusick /* 117*65789Smckusick * We now have a segment name to search for, and a directory to search. 118*65789Smckusick * 119*65789Smckusick * Before tediously performing a linear scan of the directory, 120*65789Smckusick * check the name cache to see if the directory/name pair 121*65789Smckusick * we are looking for is known already. 122*65789Smckusick */ 123*65789Smckusick if (error = cache_lookup(vdp, vpp, cnp)) { 124*65789Smckusick int vpid; /* capability number of vnode */ 125*65789Smckusick 126*65789Smckusick if (error == ENOENT) 127*65789Smckusick return (error); 128*65789Smckusick #ifdef PARANOID 129*65789Smckusick if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT)) 130*65789Smckusick panic("ufs_lookup: .. through root"); 131*65789Smckusick #endif 132*65789Smckusick /* 133*65789Smckusick * Get the next vnode in the path. 134*65789Smckusick * See comment below starting `Step through' for 135*65789Smckusick * an explaination of the locking protocol. 136*65789Smckusick */ 137*65789Smckusick pdp = dp; 138*65789Smckusick dp = VTOI(*vpp); 139*65789Smckusick vdp = *vpp; 140*65789Smckusick vpid = vdp->v_id; 141*65789Smckusick if (pdp == dp) { 142*65789Smckusick VREF(vdp); 143*65789Smckusick error = 0; 144*65789Smckusick } else if (flags & ISDOTDOT) { 145*65789Smckusick ISO_IUNLOCK(pdp); 146*65789Smckusick error = vget(vdp, 1); 147*65789Smckusick if (!error && lockparent && (flags & ISLASTCN)) 148*65789Smckusick ISO_ILOCK(pdp); 149*65789Smckusick } else { 150*65789Smckusick error = vget(vdp, 1); 151*65789Smckusick if (!lockparent || error || !(flags & ISLASTCN)) 152*65789Smckusick ISO_IUNLOCK(pdp); 153*65789Smckusick } 154*65789Smckusick /* 155*65789Smckusick * Check that the capability number did not change 156*65789Smckusick * while we were waiting for the lock. 157*65789Smckusick */ 158*65789Smckusick if (!error) { 159*65789Smckusick if (vpid == vdp->v_id) 160*65789Smckusick return (0); 161*65789Smckusick iso_iput(dp); 162*65789Smckusick if (lockparent && pdp != dp && (flags & ISLASTCN)) 163*65789Smckusick ISO_IUNLOCK(pdp); 164*65789Smckusick } 165*65789Smckusick ISO_ILOCK(pdp); 166*65789Smckusick dp = pdp; 167*65789Smckusick vdp = ITOV(dp); 168*65789Smckusick *vpp = NULL; 169*65789Smckusick } 170*65789Smckusick 171*65789Smckusick len = cnp->cn_namelen; 172*65789Smckusick name = cnp->cn_nameptr; 173*65789Smckusick /* 174*65789Smckusick * A leading `=' means, we are looking for an associated file 175*65789Smckusick */ 176*65789Smckusick if (assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)) { 177*65789Smckusick len--; 178*65789Smckusick name++; 179*65789Smckusick } 180*65789Smckusick 181*65789Smckusick /* 182*65789Smckusick * If there is cached information on a previous search of 183*65789Smckusick * this directory, pick up where we last left off. 184*65789Smckusick * We cache only lookups as these are the most common 185*65789Smckusick * and have the greatest payoff. Caching CREATE has little 186*65789Smckusick * benefit as it usually must search the entire directory 187*65789Smckusick * to determine that the entry does not exist. Caching the 188*65789Smckusick * location of the last DELETE or RENAME has not reduced 189*65789Smckusick * profiling time and hence has been removed in the interest 190*65789Smckusick * of simplicity. 191*65789Smckusick */ 192*65789Smckusick if (nameiop != LOOKUP || dp->i_diroff == 0 || 193*65789Smckusick dp->i_diroff > dp->i_size) { 194*65789Smckusick entryoffsetinblock = 0; 195*65789Smckusick dp->i_offset = 0; 196*65789Smckusick numdirpasses = 1; 197*65789Smckusick } else { 198*65789Smckusick dp->i_offset = dp->i_diroff; 199*65789Smckusick entryoffsetinblock = iso_blkoff(imp, dp->i_offset); 200*65789Smckusick if (entryoffsetinblock != 0) { 201*65789Smckusick if (error = iso_blkatoff(dp, dp->i_offset, &bp)) 202*65789Smckusick return (error); 203*65789Smckusick } 204*65789Smckusick numdirpasses = 2; 205*65789Smckusick iso_nchstats.ncs_2passes++; 206*65789Smckusick } 207*65789Smckusick endsearch = roundup(dp->i_size, imp->logical_block_size); 208*65789Smckusick 209*65789Smckusick searchloop: 210*65789Smckusick while (dp->i_offset < endsearch) { 211*65789Smckusick /* 212*65789Smckusick * If offset is on a block boundary, 213*65789Smckusick * read the next directory block. 214*65789Smckusick * Release previous if it exists. 215*65789Smckusick */ 216*65789Smckusick if (iso_blkoff(imp, dp->i_offset) == 0) { 217*65789Smckusick if (bp != NULL) 218*65789Smckusick brelse(bp); 219*65789Smckusick if (error = iso_blkatoff(dp, dp->i_offset, &bp)) 220*65789Smckusick return (error); 221*65789Smckusick entryoffsetinblock = 0; 222*65789Smckusick } 223*65789Smckusick /* 224*65789Smckusick * Get pointer to next entry. 225*65789Smckusick */ 226*65789Smckusick ep = (struct iso_directory_record *) 227*65789Smckusick (bp->b_un.b_addr + entryoffsetinblock); 228*65789Smckusick 229*65789Smckusick reclen = isonum_711 (ep->length); 230*65789Smckusick if (reclen == 0) { 231*65789Smckusick /* skip to next block, if any */ 232*65789Smckusick dp->i_offset = 233*65789Smckusick roundup(dp->i_offset, imp->logical_block_size); 234*65789Smckusick continue; 235*65789Smckusick } 236*65789Smckusick 237*65789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE) 238*65789Smckusick /* illegal entry, stop */ 239*65789Smckusick break; 240*65789Smckusick 241*65789Smckusick if (entryoffsetinblock + reclen > imp->logical_block_size) 242*65789Smckusick /* entries are not allowed to cross boundaries */ 243*65789Smckusick break; 244*65789Smckusick 245*65789Smckusick /* 246*65789Smckusick * Check for a name match. 247*65789Smckusick */ 248*65789Smckusick namelen = isonum_711(ep->name_len); 249*65789Smckusick 250*65789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 251*65789Smckusick /* illegal entry, stop */ 252*65789Smckusick break; 253*65789Smckusick 254*65789Smckusick switch (imp->iso_ftype) { 255*65789Smckusick default: 256*65789Smckusick if ((!(isonum_711(ep->flags)&4)) == !assoc) { 257*65789Smckusick if ((len == 1 258*65789Smckusick && *name == '.') 259*65789Smckusick || (flags & ISDOTDOT)) { 260*65789Smckusick if (namelen == 1 261*65789Smckusick && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) { 262*65789Smckusick /* 263*65789Smckusick * Save directory entry's inode number and 264*65789Smckusick * reclen in ndp->ni_ufs area, and release 265*65789Smckusick * directory buffer. 266*65789Smckusick */ 267*65789Smckusick isodirino(&dp->i_ino,ep,imp); 268*65789Smckusick goto found; 269*65789Smckusick } 270*65789Smckusick if (namelen != 1 271*65789Smckusick || ep->name[0] != 0) 272*65789Smckusick goto notfound; 273*65789Smckusick } else if (!(res = isofncmp(name,len, 274*65789Smckusick ep->name,namelen))) { 275*65789Smckusick if (isonum_711(ep->flags)&2) 276*65789Smckusick isodirino(&ino,ep,imp); 277*65789Smckusick else 278*65789Smckusick ino = dbtob(bp->b_blkno) 279*65789Smckusick + entryoffsetinblock; 280*65789Smckusick saveoffset = dp->i_offset; 281*65789Smckusick } else if (ino) 282*65789Smckusick goto foundino; 283*65789Smckusick #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 284*65789Smckusick else if (res < 0) 285*65789Smckusick goto notfound; 286*65789Smckusick else if (res > 0 && numdirpasses == 2) 287*65789Smckusick numdirpasses++; 288*65789Smckusick #endif 289*65789Smckusick } 290*65789Smckusick break; 291*65789Smckusick case ISO_FTYPE_RRIP: 292*65789Smckusick if (isonum_711(ep->flags)&2) 293*65789Smckusick isodirino(&ino,ep,imp); 294*65789Smckusick else 295*65789Smckusick ino = dbtob(bp->b_blkno) + entryoffsetinblock; 296*65789Smckusick dp->i_ino = ino; 297*65789Smckusick isofs_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 298*65789Smckusick if (namelen == cnp->cn_namelen 299*65789Smckusick && !bcmp(name,altname,namelen)) 300*65789Smckusick goto found; 301*65789Smckusick ino = 0; 302*65789Smckusick break; 303*65789Smckusick } 304*65789Smckusick dp->i_offset += reclen; 305*65789Smckusick entryoffsetinblock += reclen; 306*65789Smckusick } 307*65789Smckusick if (ino) { 308*65789Smckusick foundino: 309*65789Smckusick dp->i_ino = ino; 310*65789Smckusick if (saveoffset != dp->i_offset) { 311*65789Smckusick if (iso_lblkno(imp,dp->i_offset) 312*65789Smckusick != iso_lblkno(imp,saveoffset)) { 313*65789Smckusick if (bp != NULL) 314*65789Smckusick brelse(bp); 315*65789Smckusick if (error = iso_blkatoff(dp, saveoffset, &bp)) 316*65789Smckusick return (error); 317*65789Smckusick } 318*65789Smckusick ep = (struct iso_directory_record *)(bp->b_un.b_addr 319*65789Smckusick + iso_blkoff(imp,saveoffset)); 320*65789Smckusick dp->i_offset = saveoffset; 321*65789Smckusick } 322*65789Smckusick goto found; 323*65789Smckusick } 324*65789Smckusick notfound: 325*65789Smckusick /* 326*65789Smckusick * If we started in the middle of the directory and failed 327*65789Smckusick * to find our target, we must check the beginning as well. 328*65789Smckusick */ 329*65789Smckusick if (numdirpasses == 2) { 330*65789Smckusick numdirpasses--; 331*65789Smckusick dp->i_offset = 0; 332*65789Smckusick endsearch = dp->i_diroff; 333*65789Smckusick goto searchloop; 334*65789Smckusick } 335*65789Smckusick if (bp != NULL) 336*65789Smckusick brelse(bp); 337*65789Smckusick /* 338*65789Smckusick * Insert name into cache (as non-existent) if appropriate. 339*65789Smckusick */ 340*65789Smckusick if (cnp->cn_flags & MAKEENTRY) 341*65789Smckusick cache_enter(vdp, *vpp, cnp); 342*65789Smckusick if (nameiop == CREATE || nameiop == RENAME) 343*65789Smckusick return (EJUSTRETURN); 344*65789Smckusick return (ENOENT); 345*65789Smckusick 346*65789Smckusick found: 347*65789Smckusick if (numdirpasses == 2) 348*65789Smckusick iso_nchstats.ncs_pass2++; 349*65789Smckusick if (bp != NULL) 350*65789Smckusick brelse(bp); 351*65789Smckusick 352*65789Smckusick /* 353*65789Smckusick * Found component in pathname. 354*65789Smckusick * If the final component of path name, save information 355*65789Smckusick * in the cache as to where the entry was found. 356*65789Smckusick */ 357*65789Smckusick if ((flags & ISLASTCN) && nameiop == LOOKUP) 358*65789Smckusick dp->i_diroff = dp->i_offset; 359*65789Smckusick 360*65789Smckusick /* 361*65789Smckusick * Step through the translation in the name. We do not `iput' the 362*65789Smckusick * directory because we may need it again if a symbolic link 363*65789Smckusick * is relative to the current directory. Instead we save it 364*65789Smckusick * unlocked as "pdp". We must get the target inode before unlocking 365*65789Smckusick * the directory to insure that the inode will not be removed 366*65789Smckusick * before we get it. We prevent deadlock by always fetching 367*65789Smckusick * inodes from the root, moving down the directory tree. Thus 368*65789Smckusick * when following backward pointers ".." we must unlock the 369*65789Smckusick * parent directory before getting the requested directory. 370*65789Smckusick * There is a potential race condition here if both the current 371*65789Smckusick * and parent directories are removed before the `iget' for the 372*65789Smckusick * inode associated with ".." returns. We hope that this occurs 373*65789Smckusick * infrequently since we cannot avoid this race condition without 374*65789Smckusick * implementing a sophisticated deadlock detection algorithm. 375*65789Smckusick * Note also that this simple deadlock detection scheme will not 376*65789Smckusick * work if the file system has any hard links other than ".." 377*65789Smckusick * that point backwards in the directory structure. 378*65789Smckusick */ 379*65789Smckusick pdp = dp; 380*65789Smckusick /* 381*65789Smckusick * If ino is different from dp->i_ino, 382*65789Smckusick * it's a relocated directory. 383*65789Smckusick */ 384*65789Smckusick if (flags & ISDOTDOT) { 385*65789Smckusick ISO_IUNLOCK(pdp); /* race to get the inode */ 386*65789Smckusick if (error = iso_iget(dp,dp->i_ino, 387*65789Smckusick dp->i_ino != ino, 388*65789Smckusick &tdp,ep)) { 389*65789Smckusick ISO_ILOCK(pdp); 390*65789Smckusick return (error); 391*65789Smckusick } 392*65789Smckusick if (lockparent && (flags & ISLASTCN)) 393*65789Smckusick ISO_ILOCK(pdp); 394*65789Smckusick *vpp = ITOV(tdp); 395*65789Smckusick } else if (dp->i_number == dp->i_ino) { 396*65789Smckusick VREF(vdp); /* we want ourself, ie "." */ 397*65789Smckusick *vpp = vdp; 398*65789Smckusick } else { 399*65789Smckusick if (error = iso_iget(dp,dp->i_ino,dp->i_ino!=ino,&tdp,ep)) 400*65789Smckusick return (error); 401*65789Smckusick if (!lockparent || !(flags & ISLASTCN)) 402*65789Smckusick ISO_IUNLOCK(pdp); 403*65789Smckusick *vpp = ITOV(tdp); 404*65789Smckusick } 405*65789Smckusick 406*65789Smckusick /* 407*65789Smckusick * Insert name into cache if appropriate. 408*65789Smckusick */ 409*65789Smckusick if (cnp->cn_flags & MAKEENTRY) 410*65789Smckusick cache_enter(vdp, *vpp, cnp); 411*65789Smckusick return (0); 412*65789Smckusick } 413*65789Smckusick 414*65789Smckusick /* 415*65789Smckusick * Return buffer with contents of block "offset" 416*65789Smckusick * from the beginning of directory "ip". If "res" 417*65789Smckusick * is non-zero, fill it in with a pointer to the 418*65789Smckusick * remaining space in the directory. 419*65789Smckusick */ 420*65789Smckusick iso_blkatoff(ip, offset, bpp) 421*65789Smckusick struct iso_node *ip; 422*65789Smckusick doff_t offset; 423*65789Smckusick struct buf **bpp; 424*65789Smckusick { 425*65789Smckusick register struct iso_mnt *imp = ip->i_mnt; 426*65789Smckusick daddr_t lbn = iso_lblkno(imp,offset); 427*65789Smckusick int bsize = iso_blksize(imp,ip,lbn); 428*65789Smckusick struct buf *bp; 429*65789Smckusick int error; 430*65789Smckusick 431*65789Smckusick if (error = bread(ITOV(ip),lbn,bsize,NOCRED,&bp)) { 432*65789Smckusick brelse(bp); 433*65789Smckusick *bpp = 0; 434*65789Smckusick return (error); 435*65789Smckusick } 436*65789Smckusick *bpp = bp; 437*65789Smckusick 438*65789Smckusick return (0); 439*65789Smckusick } 440