165789Smckusick /*- 265789Smckusick * Copyright (c) 1989, 1993, 1994 365789Smckusick * The Regents of the University of California. All rights reserved. 465789Smckusick * 565789Smckusick * This code is derived from software contributed to Berkeley 665789Smckusick * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 765789Smckusick * Support code is derived from software contributed to Berkeley 865789Smckusick * by Atsushi Murai (amurai@spec.co.jp). 965789Smckusick * 1065789Smckusick * %sccs.include.redist.c% 1165789Smckusick * 1265789Smckusick * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91 1365789Smckusick * 14*69428Smckusick * @(#)cd9660_lookup.c 8.6 (Berkeley) 05/14/95 1565789Smckusick */ 1665789Smckusick 1765789Smckusick #include <sys/param.h> 1865789Smckusick #include <sys/namei.h> 1965789Smckusick #include <sys/buf.h> 2065789Smckusick #include <sys/file.h> 2165789Smckusick #include <sys/vnode.h> 2265789Smckusick #include <sys/mount.h> 2365789Smckusick 2465789Smckusick #include <isofs/cd9660/iso.h> 2565855Smckusick #include <isofs/cd9660/cd9660_node.h> 2665789Smckusick #include <isofs/cd9660/iso_rrip.h> 2765855Smckusick #include <isofs/cd9660/cd9660_rrip.h> 2865789Smckusick 2965789Smckusick struct nchstats iso_nchstats; 3065789Smckusick 3165789Smckusick /* 3265789Smckusick * Convert a component of a pathname into a pointer to a locked inode. 3365789Smckusick * This is a very central and rather complicated routine. 3465789Smckusick * If the file system is not maintained in a strict tree hierarchy, 3565789Smckusick * this can result in a deadlock situation (see comments in code below). 3665789Smckusick * 3765789Smckusick * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 3865789Smckusick * whether the name is to be looked up, created, renamed, or deleted. 3965789Smckusick * When CREATE, RENAME, or DELETE is specified, information usable in 4065789Smckusick * creating, renaming, or deleting a directory entry may be calculated. 4165789Smckusick * If flag has LOCKPARENT or'ed into it and the target of the pathname 4265789Smckusick * exists, lookup returns both the target and its parent directory locked. 4365789Smckusick * When creating or renaming and LOCKPARENT is specified, the target may 4465789Smckusick * not be ".". When deleting and LOCKPARENT is specified, the target may 4565789Smckusick * be "."., but the caller must check to ensure it does an vrele and iput 4665789Smckusick * instead of two iputs. 4765789Smckusick * 4865789Smckusick * Overall outline of ufs_lookup: 4965789Smckusick * 5065789Smckusick * check accessibility of directory 5165789Smckusick * look for name in cache, if found, then if at end of path 5265789Smckusick * and deleting or creating, drop it, else return name 5365789Smckusick * search for name in directory, to found or notfound 5465789Smckusick * notfound: 5565789Smckusick * if creating, return locked directory, leaving info on available slots 5665789Smckusick * else return error 5765789Smckusick * found: 5865789Smckusick * if at end of path and deleting, return information to allow delete 5965789Smckusick * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 6065789Smckusick * inode and return info to allow rewrite 6165789Smckusick * if not at end, add name to cache; if at end and neither creating 6265789Smckusick * nor deleting, add name to cache 6365789Smckusick * 6465789Smckusick * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 6565789Smckusick */ 6665855Smckusick cd9660_lookup(ap) 6765789Smckusick struct vop_lookup_args /* { 6865789Smckusick struct vnode *a_dvp; 6965789Smckusick struct vnode **a_vpp; 7065789Smckusick struct componentname *a_cnp; 7165789Smckusick } */ *ap; 7265789Smckusick { 7365789Smckusick register struct vnode *vdp; /* vnode for directory being searched */ 7465789Smckusick register struct iso_node *dp; /* inode for directory being searched */ 7565789Smckusick register struct iso_mnt *imp; /* file system that directory is in */ 7665789Smckusick struct buf *bp; /* a buffer of directory entries */ 7765789Smckusick struct iso_directory_record *ep;/* the current directory entry */ 7865789Smckusick int entryoffsetinblock; /* offset of ep in bp's buffer */ 7965789Smckusick int saveoffset; /* offset of last directory entry in dir */ 8065789Smckusick int numdirpasses; /* strategy for directory search */ 8165789Smckusick doff_t endsearch; /* offset to end directory search */ 8267522Smckusick struct vnode *pdp; /* saved dp during symlink work */ 8367522Smckusick struct vnode *tdp; /* returned by cd9660_vget_internal */ 8468047Smckusick u_long bmask; /* block offset mask */ 8565789Smckusick int lockparent; /* 1 => lockparent flag is set */ 8665789Smckusick int wantparent; /* 1 => wantparent or lockparent flag */ 8765789Smckusick int error; 8865789Smckusick ino_t ino = 0; 8965789Smckusick int reclen; 9065789Smckusick u_short namelen; 9165789Smckusick char altname[NAME_MAX]; 9265789Smckusick int res; 9365789Smckusick int assoc, len; 9465789Smckusick char *name; 9565789Smckusick struct vnode **vpp = ap->a_vpp; 9665789Smckusick struct componentname *cnp = ap->a_cnp; 9765789Smckusick struct ucred *cred = cnp->cn_cred; 9865789Smckusick int flags = cnp->cn_flags; 9965789Smckusick int nameiop = cnp->cn_nameiop; 100*69428Smckusick struct proc *p = cnp->cn_proc; 10165789Smckusick 10265789Smckusick bp = NULL; 10365789Smckusick *vpp = NULL; 10465789Smckusick vdp = ap->a_dvp; 10565789Smckusick dp = VTOI(vdp); 10665789Smckusick imp = dp->i_mnt; 10765789Smckusick lockparent = flags & LOCKPARENT; 10865789Smckusick wantparent = flags & (LOCKPARENT|WANTPARENT); 10965789Smckusick 11065789Smckusick /* 11165789Smckusick * Check accessiblity of directory. 11265789Smckusick */ 11365789Smckusick if (vdp->v_type != VDIR) 11467375Smkm return (ENOTDIR); 11565789Smckusick if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) 11665789Smckusick return (error); 11765789Smckusick 11865789Smckusick /* 11965789Smckusick * We now have a segment name to search for, and a directory to search. 12065789Smckusick * 12165789Smckusick * Before tediously performing a linear scan of the directory, 12265789Smckusick * check the name cache to see if the directory/name pair 12365789Smckusick * we are looking for is known already. 12465789Smckusick */ 12565789Smckusick if (error = cache_lookup(vdp, vpp, cnp)) { 12665789Smckusick int vpid; /* capability number of vnode */ 12765789Smckusick 12865789Smckusick if (error == ENOENT) 12965789Smckusick return (error); 13065789Smckusick #ifdef PARANOID 13165789Smckusick if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT)) 13268047Smckusick panic("cd9660_lookup: .. through root"); 13365789Smckusick #endif 13465789Smckusick /* 13565789Smckusick * Get the next vnode in the path. 13665789Smckusick * See comment below starting `Step through' for 13765789Smckusick * an explaination of the locking protocol. 13865789Smckusick */ 13967522Smckusick pdp = vdp; 14065789Smckusick dp = VTOI(*vpp); 14165789Smckusick vdp = *vpp; 14265789Smckusick vpid = vdp->v_id; 14367522Smckusick if (pdp == vdp) { 14465789Smckusick VREF(vdp); 14565789Smckusick error = 0; 14665789Smckusick } else if (flags & ISDOTDOT) { 147*69428Smckusick VOP_UNLOCK(pdp, 0, p); 148*69428Smckusick error = vget(vdp, LK_EXCLUSIVE, p); 14965789Smckusick if (!error && lockparent && (flags & ISLASTCN)) 150*69428Smckusick error = vn_lock(pdp, LK_EXCLUSIVE, p); 15165789Smckusick } else { 152*69428Smckusick error = vget(vdp, LK_EXCLUSIVE, p); 15365789Smckusick if (!lockparent || error || !(flags & ISLASTCN)) 154*69428Smckusick VOP_UNLOCK(pdp, 0, p); 15565789Smckusick } 15665789Smckusick /* 15765789Smckusick * Check that the capability number did not change 15865789Smckusick * while we were waiting for the lock. 15965789Smckusick */ 16065789Smckusick if (!error) { 16165789Smckusick if (vpid == vdp->v_id) 16265789Smckusick return (0); 16367522Smckusick vput(vdp); 16467522Smckusick if (lockparent && pdp != vdp && (flags & ISLASTCN)) 165*69428Smckusick VOP_UNLOCK(pdp, 0, p); 16665789Smckusick } 167*69428Smckusick if (error = vn_lock(pdp, LK_EXCLUSIVE, p)) 16867522Smckusick return (error); 16967522Smckusick vdp = pdp; 17067522Smckusick dp = VTOI(pdp); 17165789Smckusick *vpp = NULL; 17265789Smckusick } 17365789Smckusick 17465789Smckusick len = cnp->cn_namelen; 17565789Smckusick name = cnp->cn_nameptr; 17665789Smckusick /* 17765789Smckusick * A leading `=' means, we are looking for an associated file 17865789Smckusick */ 17965789Smckusick if (assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)) { 18065789Smckusick len--; 18165789Smckusick name++; 18265789Smckusick } 18365789Smckusick 18465789Smckusick /* 18565789Smckusick * If there is cached information on a previous search of 18665789Smckusick * this directory, pick up where we last left off. 18765789Smckusick * We cache only lookups as these are the most common 18865789Smckusick * and have the greatest payoff. Caching CREATE has little 18965789Smckusick * benefit as it usually must search the entire directory 19065789Smckusick * to determine that the entry does not exist. Caching the 19165789Smckusick * location of the last DELETE or RENAME has not reduced 19265789Smckusick * profiling time and hence has been removed in the interest 19365789Smckusick * of simplicity. 19465789Smckusick */ 19568047Smckusick bmask = imp->im_bmask; 19665789Smckusick if (nameiop != LOOKUP || dp->i_diroff == 0 || 19765789Smckusick dp->i_diroff > dp->i_size) { 19865789Smckusick entryoffsetinblock = 0; 19965789Smckusick dp->i_offset = 0; 20065789Smckusick numdirpasses = 1; 20165789Smckusick } else { 20265789Smckusick dp->i_offset = dp->i_diroff; 20368047Smckusick if ((entryoffsetinblock = dp->i_offset & bmask) && 20468047Smckusick (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))) 20565789Smckusick return (error); 20665789Smckusick numdirpasses = 2; 20765789Smckusick iso_nchstats.ncs_2passes++; 20865789Smckusick } 20968047Smckusick endsearch = dp->i_size; 21065789Smckusick 21165789Smckusick searchloop: 21265789Smckusick while (dp->i_offset < endsearch) { 21365789Smckusick /* 21465789Smckusick * If offset is on a block boundary, 21565789Smckusick * read the next directory block. 21665789Smckusick * Release previous if it exists. 21765789Smckusick */ 21868047Smckusick if ((dp->i_offset & bmask) == 0) { 21965789Smckusick if (bp != NULL) 22065789Smckusick brelse(bp); 22168047Smckusick if (error = 22268047Smckusick VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)) 22365789Smckusick return (error); 22465789Smckusick entryoffsetinblock = 0; 22565789Smckusick } 22665789Smckusick /* 22765789Smckusick * Get pointer to next entry. 22865789Smckusick */ 22965789Smckusick ep = (struct iso_directory_record *) 23068047Smckusick ((char *)bp->b_data + entryoffsetinblock); 23165789Smckusick 23268047Smckusick reclen = isonum_711(ep->length); 23365789Smckusick if (reclen == 0) { 23465789Smckusick /* skip to next block, if any */ 23565789Smckusick dp->i_offset = 23668047Smckusick (dp->i_offset & ~bmask) + imp->logical_block_size; 23765789Smckusick continue; 23865789Smckusick } 23965789Smckusick 24065789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE) 24165789Smckusick /* illegal entry, stop */ 24265789Smckusick break; 24365789Smckusick 24465789Smckusick if (entryoffsetinblock + reclen > imp->logical_block_size) 24565789Smckusick /* entries are not allowed to cross boundaries */ 24665789Smckusick break; 24765789Smckusick 24865789Smckusick namelen = isonum_711(ep->name_len); 24965789Smckusick 25065789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 25165789Smckusick /* illegal entry, stop */ 25265789Smckusick break; 25365789Smckusick 25468047Smckusick /* 25568047Smckusick * Check for a name match. 25668047Smckusick */ 25765789Smckusick switch (imp->iso_ftype) { 25865789Smckusick default: 25965789Smckusick if ((!(isonum_711(ep->flags)&4)) == !assoc) { 26065789Smckusick if ((len == 1 26165789Smckusick && *name == '.') 26265789Smckusick || (flags & ISDOTDOT)) { 26365789Smckusick if (namelen == 1 26465789Smckusick && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) { 26565789Smckusick /* 26665789Smckusick * Save directory entry's inode number and 26768047Smckusick * release directory buffer. 26865789Smckusick */ 26968047Smckusick dp->i_ino = isodirino(ep, imp); 27065789Smckusick goto found; 27165789Smckusick } 27265789Smckusick if (namelen != 1 27365789Smckusick || ep->name[0] != 0) 27465789Smckusick goto notfound; 27565789Smckusick } else if (!(res = isofncmp(name,len, 27665789Smckusick ep->name,namelen))) { 27765789Smckusick if (isonum_711(ep->flags)&2) 27868047Smckusick ino = isodirino(ep, imp); 27965789Smckusick else 28065789Smckusick ino = dbtob(bp->b_blkno) 28165789Smckusick + entryoffsetinblock; 28265789Smckusick saveoffset = dp->i_offset; 28365789Smckusick } else if (ino) 28465789Smckusick goto foundino; 28565789Smckusick #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 28665789Smckusick else if (res < 0) 28765789Smckusick goto notfound; 28865789Smckusick else if (res > 0 && numdirpasses == 2) 28965789Smckusick numdirpasses++; 29065789Smckusick #endif 29165789Smckusick } 29265789Smckusick break; 29365789Smckusick case ISO_FTYPE_RRIP: 29465789Smckusick if (isonum_711(ep->flags)&2) 29568047Smckusick ino = isodirino(ep, imp); 29665789Smckusick else 29765789Smckusick ino = dbtob(bp->b_blkno) + entryoffsetinblock; 29865789Smckusick dp->i_ino = ino; 29965855Smckusick cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 30065789Smckusick if (namelen == cnp->cn_namelen 30165789Smckusick && !bcmp(name,altname,namelen)) 30265789Smckusick goto found; 30365789Smckusick ino = 0; 30465789Smckusick break; 30565789Smckusick } 30665789Smckusick dp->i_offset += reclen; 30765789Smckusick entryoffsetinblock += reclen; 30865789Smckusick } 30965789Smckusick if (ino) { 31065789Smckusick foundino: 31165789Smckusick dp->i_ino = ino; 31265789Smckusick if (saveoffset != dp->i_offset) { 31368047Smckusick if (lblkno(imp, dp->i_offset) != 31468047Smckusick lblkno(imp, saveoffset)) { 31565789Smckusick if (bp != NULL) 31665789Smckusick brelse(bp); 31768047Smckusick if (error = VOP_BLKATOFF(vdp, 31868047Smckusick (off_t)saveoffset, NULL, &bp)) 31965789Smckusick return (error); 32065789Smckusick } 32168047Smckusick entryoffsetinblock = saveoffset & bmask; 32268047Smckusick ep = (struct iso_directory_record *) 32368047Smckusick ((char *)bp->b_data + entryoffsetinblock); 32465789Smckusick dp->i_offset = saveoffset; 32565789Smckusick } 32665789Smckusick goto found; 32765789Smckusick } 32865789Smckusick notfound: 32965789Smckusick /* 33065789Smckusick * If we started in the middle of the directory and failed 33165789Smckusick * to find our target, we must check the beginning as well. 33265789Smckusick */ 33365789Smckusick if (numdirpasses == 2) { 33465789Smckusick numdirpasses--; 33565789Smckusick dp->i_offset = 0; 33665789Smckusick endsearch = dp->i_diroff; 33765789Smckusick goto searchloop; 33865789Smckusick } 33965789Smckusick if (bp != NULL) 34065789Smckusick brelse(bp); 34168047Smckusick 34265789Smckusick /* 34365789Smckusick * Insert name into cache (as non-existent) if appropriate. 34465789Smckusick */ 34565789Smckusick if (cnp->cn_flags & MAKEENTRY) 34665789Smckusick cache_enter(vdp, *vpp, cnp); 34765789Smckusick if (nameiop == CREATE || nameiop == RENAME) 34865789Smckusick return (EJUSTRETURN); 34965789Smckusick return (ENOENT); 35065789Smckusick 35165789Smckusick found: 35265789Smckusick if (numdirpasses == 2) 35365789Smckusick iso_nchstats.ncs_pass2++; 35465789Smckusick 35565789Smckusick /* 35665789Smckusick * Found component in pathname. 35765789Smckusick * If the final component of path name, save information 35865789Smckusick * in the cache as to where the entry was found. 35965789Smckusick */ 36065789Smckusick if ((flags & ISLASTCN) && nameiop == LOOKUP) 36165789Smckusick dp->i_diroff = dp->i_offset; 36265789Smckusick 36365789Smckusick /* 36465789Smckusick * Step through the translation in the name. We do not `iput' the 36565789Smckusick * directory because we may need it again if a symbolic link 36665789Smckusick * is relative to the current directory. Instead we save it 36765789Smckusick * unlocked as "pdp". We must get the target inode before unlocking 36865789Smckusick * the directory to insure that the inode will not be removed 36965789Smckusick * before we get it. We prevent deadlock by always fetching 37065789Smckusick * inodes from the root, moving down the directory tree. Thus 37165789Smckusick * when following backward pointers ".." we must unlock the 37265789Smckusick * parent directory before getting the requested directory. 37365789Smckusick * There is a potential race condition here if both the current 37465789Smckusick * and parent directories are removed before the `iget' for the 37565789Smckusick * inode associated with ".." returns. We hope that this occurs 37665789Smckusick * infrequently since we cannot avoid this race condition without 37765789Smckusick * implementing a sophisticated deadlock detection algorithm. 37865789Smckusick * Note also that this simple deadlock detection scheme will not 37965789Smckusick * work if the file system has any hard links other than ".." 38065789Smckusick * that point backwards in the directory structure. 38165789Smckusick */ 38267522Smckusick pdp = vdp; 38365789Smckusick /* 38465789Smckusick * If ino is different from dp->i_ino, 38565789Smckusick * it's a relocated directory. 38665789Smckusick */ 38765789Smckusick if (flags & ISDOTDOT) { 388*69428Smckusick VOP_UNLOCK(pdp, 0, p); /* race to get the inode */ 38968047Smckusick error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 39068047Smckusick dp->i_ino != ino, ep); 39168047Smckusick brelse(bp); 39268047Smckusick if (error) { 393*69428Smckusick vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p); 39465789Smckusick return (error); 39565789Smckusick } 39667522Smckusick if (lockparent && (flags & ISLASTCN) && 397*69428Smckusick (error = vn_lock(pdp, LK_EXCLUSIVE, p))) { 39867522Smckusick vput(tdp); 39967522Smckusick return (error); 40067522Smckusick } 40167522Smckusick *vpp = tdp; 40265789Smckusick } else if (dp->i_number == dp->i_ino) { 40368047Smckusick brelse(bp); 40465789Smckusick VREF(vdp); /* we want ourself, ie "." */ 40565789Smckusick *vpp = vdp; 40665789Smckusick } else { 40768047Smckusick error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 40868047Smckusick dp->i_ino != ino, ep); 40968047Smckusick brelse(bp); 41068047Smckusick if (error) 41165789Smckusick return (error); 41265789Smckusick if (!lockparent || !(flags & ISLASTCN)) 413*69428Smckusick VOP_UNLOCK(pdp, 0, p); 41467522Smckusick *vpp = tdp; 41565789Smckusick } 41665789Smckusick 41765789Smckusick /* 41865789Smckusick * Insert name into cache if appropriate. 41965789Smckusick */ 42065789Smckusick if (cnp->cn_flags & MAKEENTRY) 42165789Smckusick cache_enter(vdp, *vpp, cnp); 42265789Smckusick return (0); 42365789Smckusick } 42465789Smckusick 42565789Smckusick /* 42668047Smckusick * Return buffer with the contents of block "offset" from the beginning of 42768047Smckusick * directory "ip". If "res" is non-zero, fill it in with a pointer to the 42865789Smckusick * remaining space in the directory. 42965789Smckusick */ 43068047Smckusick int 43168047Smckusick cd9660_blkatoff(ap) 43268047Smckusick struct vop_blkatoff_args /* { 43368047Smckusick struct vnode *a_vp; 43468047Smckusick off_t a_offset; 43568047Smckusick char **a_res; 43668047Smckusick struct buf **a_bpp; 43768047Smckusick } */ *ap; 43868047Smckusick { 43965789Smckusick struct iso_node *ip; 44068047Smckusick register struct iso_mnt *imp; 44165789Smckusick struct buf *bp; 44268047Smckusick daddr_t lbn; 44368047Smckusick int bsize, error; 44468047Smckusick 44568047Smckusick ip = VTOI(ap->a_vp); 44668047Smckusick imp = ip->i_mnt; 44768047Smckusick lbn = lblkno(imp, ap->a_offset); 44868047Smckusick bsize = blksize(imp, ip, lbn); 44965789Smckusick 45068047Smckusick if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) { 45165789Smckusick brelse(bp); 45268047Smckusick *ap->a_bpp = NULL; 45365789Smckusick return (error); 45465789Smckusick } 45568047Smckusick if (ap->a_res) 45668047Smckusick *ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset); 45768047Smckusick *ap->a_bpp = bp; 45865789Smckusick return (0); 45965789Smckusick } 460