Home | History | Annotate | Line # | Download | only in cd9660
cd9660_lookup.c revision 1.1
      1  1.1  jdolecek /*	$NetBSD: cd9660_lookup.c,v 1.1 2002/12/23 17:52:08 jdolecek Exp $	*/
      2  1.1  jdolecek 
      3  1.1  jdolecek /*-
      4  1.1  jdolecek  * Copyright (c) 1989, 1993, 1994
      5  1.1  jdolecek  *	The Regents of the University of California.  All rights reserved.
      6  1.1  jdolecek  *
      7  1.1  jdolecek  * This code is derived from software contributed to Berkeley
      8  1.1  jdolecek  * by Pace Willisson (pace (at) blitz.com).  The Rock Ridge Extension
      9  1.1  jdolecek  * Support code is derived from software contributed to Berkeley
     10  1.1  jdolecek  * by Atsushi Murai (amurai (at) spec.co.jp).
     11  1.1  jdolecek  *
     12  1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     13  1.1  jdolecek  * modification, are permitted provided that the following conditions
     14  1.1  jdolecek  * are met:
     15  1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     16  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     17  1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     19  1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     20  1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     21  1.1  jdolecek  *    must display the following acknowledgement:
     22  1.1  jdolecek  *	This product includes software developed by the University of
     23  1.1  jdolecek  *	California, Berkeley and its contributors.
     24  1.1  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     25  1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     26  1.1  jdolecek  *    without specific prior written permission.
     27  1.1  jdolecek  *
     28  1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1  jdolecek  * SUCH DAMAGE.
     39  1.1  jdolecek  *
     40  1.1  jdolecek  *	from: @(#)ufs_lookup.c	7.33 (Berkeley) 5/19/91
     41  1.1  jdolecek  *
     42  1.1  jdolecek  *	@(#)cd9660_lookup.c	8.5 (Berkeley) 5/27/95
     43  1.1  jdolecek  */
     44  1.1  jdolecek 
     45  1.1  jdolecek #include <sys/cdefs.h>
     46  1.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: cd9660_lookup.c,v 1.1 2002/12/23 17:52:08 jdolecek Exp $");
     47  1.1  jdolecek 
     48  1.1  jdolecek #include <sys/param.h>
     49  1.1  jdolecek #include <sys/namei.h>
     50  1.1  jdolecek #include <sys/buf.h>
     51  1.1  jdolecek #include <sys/file.h>
     52  1.1  jdolecek #include <sys/vnode.h>
     53  1.1  jdolecek #include <sys/mount.h>
     54  1.1  jdolecek #include <sys/systm.h>
     55  1.1  jdolecek 
     56  1.1  jdolecek #include <fs/cd9660/iso.h>
     57  1.1  jdolecek #include <fs/cd9660/cd9660_extern.h>
     58  1.1  jdolecek #include <fs/cd9660/cd9660_node.h>
     59  1.1  jdolecek #include <fs/cd9660/iso_rrip.h>
     60  1.1  jdolecek #include <fs/cd9660/cd9660_rrip.h>
     61  1.1  jdolecek #include <fs/cd9660/cd9660_mount.h>
     62  1.1  jdolecek 
     63  1.1  jdolecek struct	nchstats iso_nchstats;
     64  1.1  jdolecek 
     65  1.1  jdolecek /*
     66  1.1  jdolecek  * Convert a component of a pathname into a pointer to a locked inode.
     67  1.1  jdolecek  * This is a very central and rather complicated routine.
     68  1.1  jdolecek  * If the file system is not maintained in a strict tree hierarchy,
     69  1.1  jdolecek  * this can result in a deadlock situation (see comments in code below).
     70  1.1  jdolecek  *
     71  1.1  jdolecek  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
     72  1.1  jdolecek  * whether the name is to be looked up, created, renamed, or deleted.
     73  1.1  jdolecek  * When CREATE, RENAME, or DELETE is specified, information usable in
     74  1.1  jdolecek  * creating, renaming, or deleting a directory entry may be calculated.
     75  1.1  jdolecek  * If flag has LOCKPARENT or'ed into it and the target of the pathname
     76  1.1  jdolecek  * exists, lookup returns both the target and its parent directory locked.
     77  1.1  jdolecek  * When creating or renaming and LOCKPARENT is specified, the target may
     78  1.1  jdolecek  * not be ".".  When deleting and LOCKPARENT is specified, the target may
     79  1.1  jdolecek  * be "."., but the caller must check to ensure it does an vrele and iput
     80  1.1  jdolecek  * instead of two iputs.
     81  1.1  jdolecek  *
     82  1.1  jdolecek  * Overall outline of ufs_lookup:
     83  1.1  jdolecek  *
     84  1.1  jdolecek  *	check accessibility of directory
     85  1.1  jdolecek  *	look for name in cache, if found, then if at end of path
     86  1.1  jdolecek  *	  and deleting or creating, drop it, else return name
     87  1.1  jdolecek  *	search for name in directory, to found or notfound
     88  1.1  jdolecek  * notfound:
     89  1.1  jdolecek  *	if creating, return locked directory, leaving info on available slots
     90  1.1  jdolecek  *	else return error
     91  1.1  jdolecek  * found:
     92  1.1  jdolecek  *	if at end of path and deleting, return information to allow delete
     93  1.1  jdolecek  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
     94  1.1  jdolecek  *	  inode and return info to allow rewrite
     95  1.1  jdolecek  *	if not at end, add name to cache; if at end and neither creating
     96  1.1  jdolecek  *	  nor deleting, add name to cache
     97  1.1  jdolecek  *
     98  1.1  jdolecek  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
     99  1.1  jdolecek  */
    100  1.1  jdolecek int
    101  1.1  jdolecek cd9660_lookup(v)
    102  1.1  jdolecek 	void *v;
    103  1.1  jdolecek {
    104  1.1  jdolecek 	struct vop_lookup_args /* {
    105  1.1  jdolecek 		struct vnode *a_dvp;
    106  1.1  jdolecek 		struct vnode **a_vpp;
    107  1.1  jdolecek 		struct componentname *a_cnp;
    108  1.1  jdolecek 	} */ *ap = v;
    109  1.1  jdolecek 	struct vnode *vdp;		/* vnode for directory being searched */
    110  1.1  jdolecek 	struct iso_node *dp;		/* inode for directory being searched */
    111  1.1  jdolecek 	struct iso_mnt *imp;		/* file system that directory is in */
    112  1.1  jdolecek 	struct buf *bp;			/* a buffer of directory entries */
    113  1.1  jdolecek 	struct iso_directory_record *ep = NULL;
    114  1.1  jdolecek 					/* the current directory entry */
    115  1.1  jdolecek 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
    116  1.1  jdolecek 	int saveoffset = -1;		/* offset of last directory entry in dir */
    117  1.1  jdolecek 	int numdirpasses;		/* strategy for directory search */
    118  1.1  jdolecek 	doff_t endsearch;		/* offset to end directory search */
    119  1.1  jdolecek 	struct vnode *pdp;		/* saved dp during symlink work */
    120  1.1  jdolecek 	struct vnode *tdp;		/* returned by cd9660_vget_internal */
    121  1.1  jdolecek 	u_long bmask;			/* block offset mask */
    122  1.1  jdolecek 	int lockparent;			/* 1 => lockparent flag is set */
    123  1.1  jdolecek 	int wantparent;			/* 1 => wantparent or lockparent flag */
    124  1.1  jdolecek 	int error;
    125  1.1  jdolecek 	ino_t ino = 0;
    126  1.1  jdolecek 	int reclen;
    127  1.1  jdolecek 	u_short namelen;
    128  1.1  jdolecek 	char altname[NAME_MAX];
    129  1.1  jdolecek 	int res;
    130  1.1  jdolecek 	int assoc, len;
    131  1.1  jdolecek 	const char *name;
    132  1.1  jdolecek 	struct vnode **vpp = ap->a_vpp;
    133  1.1  jdolecek 	struct componentname *cnp = ap->a_cnp;
    134  1.1  jdolecek 	struct ucred *cred = cnp->cn_cred;
    135  1.1  jdolecek 	int flags;
    136  1.1  jdolecek 	int nameiop = cnp->cn_nameiop;
    137  1.1  jdolecek 
    138  1.1  jdolecek 	cnp->cn_flags &= ~PDIRUNLOCK;
    139  1.1  jdolecek 	flags = cnp->cn_flags;
    140  1.1  jdolecek 
    141  1.1  jdolecek 	bp = NULL;
    142  1.1  jdolecek 	*vpp = NULL;
    143  1.1  jdolecek 	vdp = ap->a_dvp;
    144  1.1  jdolecek 	dp = VTOI(vdp);
    145  1.1  jdolecek 	imp = dp->i_mnt;
    146  1.1  jdolecek 	lockparent = flags & LOCKPARENT;
    147  1.1  jdolecek 	wantparent = flags & (LOCKPARENT|WANTPARENT);
    148  1.1  jdolecek 
    149  1.1  jdolecek 	/*
    150  1.1  jdolecek 	 * Check accessiblity of directory.
    151  1.1  jdolecek 	 */
    152  1.1  jdolecek 	if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
    153  1.1  jdolecek 		return (error);
    154  1.1  jdolecek 
    155  1.1  jdolecek 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    156  1.1  jdolecek 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    157  1.1  jdolecek 		return (EROFS);
    158  1.1  jdolecek 
    159  1.1  jdolecek 	/*
    160  1.1  jdolecek 	 * We now have a segment name to search for, and a directory to search.
    161  1.1  jdolecek 	 *
    162  1.1  jdolecek 	 * Before tediously performing a linear scan of the directory,
    163  1.1  jdolecek 	 * check the name cache to see if the directory/name pair
    164  1.1  jdolecek 	 * we are looking for is known already.
    165  1.1  jdolecek 	 */
    166  1.1  jdolecek 	if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
    167  1.1  jdolecek 		return (error);
    168  1.1  jdolecek 
    169  1.1  jdolecek 	len = cnp->cn_namelen;
    170  1.1  jdolecek 	name = cnp->cn_nameptr;
    171  1.1  jdolecek 	/*
    172  1.1  jdolecek 	 * A leading `=' means, we are looking for an associated file
    173  1.1  jdolecek 	 */
    174  1.1  jdolecek 	assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR);
    175  1.1  jdolecek 	if (assoc) {
    176  1.1  jdolecek 		len--;
    177  1.1  jdolecek 		name++;
    178  1.1  jdolecek 	}
    179  1.1  jdolecek 
    180  1.1  jdolecek 	/*
    181  1.1  jdolecek 	 * If there is cached information on a previous search of
    182  1.1  jdolecek 	 * this directory, pick up where we last left off.
    183  1.1  jdolecek 	 * We cache only lookups as these are the most common
    184  1.1  jdolecek 	 * and have the greatest payoff. Caching CREATE has little
    185  1.1  jdolecek 	 * benefit as it usually must search the entire directory
    186  1.1  jdolecek 	 * to determine that the entry does not exist. Caching the
    187  1.1  jdolecek 	 * location of the last DELETE or RENAME has not reduced
    188  1.1  jdolecek 	 * profiling time and hence has been removed in the interest
    189  1.1  jdolecek 	 * of simplicity.
    190  1.1  jdolecek 	 */
    191  1.1  jdolecek 	bmask = imp->im_bmask;
    192  1.1  jdolecek 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
    193  1.1  jdolecek 	    dp->i_diroff > dp->i_size) {
    194  1.1  jdolecek 		entryoffsetinblock = 0;
    195  1.1  jdolecek 		dp->i_offset = 0;
    196  1.1  jdolecek 		numdirpasses = 1;
    197  1.1  jdolecek 	} else {
    198  1.1  jdolecek 		dp->i_offset = dp->i_diroff;
    199  1.1  jdolecek 		if ((entryoffsetinblock = dp->i_offset & bmask) &&
    200  1.1  jdolecek 		    (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
    201  1.1  jdolecek 				return (error);
    202  1.1  jdolecek 		numdirpasses = 2;
    203  1.1  jdolecek 		iso_nchstats.ncs_2passes++;
    204  1.1  jdolecek 	}
    205  1.1  jdolecek 	endsearch = dp->i_size;
    206  1.1  jdolecek 
    207  1.1  jdolecek searchloop:
    208  1.1  jdolecek 	while (dp->i_offset < endsearch) {
    209  1.1  jdolecek 		/*
    210  1.1  jdolecek 		 * If offset is on a block boundary,
    211  1.1  jdolecek 		 * read the next directory block.
    212  1.1  jdolecek 		 * Release previous if it exists.
    213  1.1  jdolecek 		 */
    214  1.1  jdolecek 		if ((dp->i_offset & bmask) == 0) {
    215  1.1  jdolecek 			if (bp != NULL)
    216  1.1  jdolecek 				brelse(bp);
    217  1.1  jdolecek 			error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset,
    218  1.1  jdolecek 					     NULL, &bp);
    219  1.1  jdolecek 			if (error)
    220  1.1  jdolecek 				return (error);
    221  1.1  jdolecek 			entryoffsetinblock = 0;
    222  1.1  jdolecek 		}
    223  1.1  jdolecek 		/*
    224  1.1  jdolecek 		 * Get pointer to next entry.
    225  1.1  jdolecek 		 */
    226  1.1  jdolecek 		ep = (struct iso_directory_record *)
    227  1.1  jdolecek 			((char *)bp->b_data + entryoffsetinblock);
    228  1.1  jdolecek 
    229  1.1  jdolecek 		reclen = isonum_711(ep->length);
    230  1.1  jdolecek 		if (reclen == 0) {
    231  1.1  jdolecek 			/* skip to next block, if any */
    232  1.1  jdolecek 			dp->i_offset =
    233  1.1  jdolecek 			    (dp->i_offset & ~bmask) + imp->logical_block_size;
    234  1.1  jdolecek 			continue;
    235  1.1  jdolecek 		}
    236  1.1  jdolecek 
    237  1.1  jdolecek 		if (reclen < ISO_DIRECTORY_RECORD_SIZE)
    238  1.1  jdolecek 			/* illegal entry, stop */
    239  1.1  jdolecek 			break;
    240  1.1  jdolecek 
    241  1.1  jdolecek 		if (entryoffsetinblock + reclen > imp->logical_block_size)
    242  1.1  jdolecek 			/* entries are not allowed to cross boundaries */
    243  1.1  jdolecek 			break;
    244  1.1  jdolecek 
    245  1.1  jdolecek 		namelen = isonum_711(ep->name_len);
    246  1.1  jdolecek 
    247  1.1  jdolecek 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
    248  1.1  jdolecek 			/* illegal entry, stop */
    249  1.1  jdolecek 			break;
    250  1.1  jdolecek 
    251  1.1  jdolecek 		/*
    252  1.1  jdolecek 		 * Check for a name match.
    253  1.1  jdolecek 		 */
    254  1.1  jdolecek 		switch (imp->iso_ftype) {
    255  1.1  jdolecek 		default:
    256  1.1  jdolecek 			if ((!(isonum_711(ep->flags)&4)) == !assoc) {
    257  1.1  jdolecek 				if ((len == 1
    258  1.1  jdolecek 				     && *name == '.')
    259  1.1  jdolecek 				    || (flags & ISDOTDOT)) {
    260  1.1  jdolecek 					if (namelen == 1
    261  1.1  jdolecek 					    && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
    262  1.1  jdolecek 						/*
    263  1.1  jdolecek 						 * Save directory entry's inode number and
    264  1.1  jdolecek 						 * release directory buffer.
    265  1.1  jdolecek 						 */
    266  1.1  jdolecek 						dp->i_ino = isodirino(ep, imp);
    267  1.1  jdolecek 						goto found;
    268  1.1  jdolecek 					}
    269  1.1  jdolecek 					if (namelen != 1
    270  1.1  jdolecek 					    || ep->name[0] != 0)
    271  1.1  jdolecek 						goto notfound;
    272  1.1  jdolecek 				} else if (!(res = isofncmp(name,len,
    273  1.1  jdolecek 						   ep->name,namelen,
    274  1.1  jdolecek 						   imp->im_joliet_level))) {
    275  1.1  jdolecek 					if (isonum_711(ep->flags)&2)
    276  1.1  jdolecek 						ino = isodirino(ep, imp);
    277  1.1  jdolecek 					else
    278  1.1  jdolecek 						ino = dbtob(bp->b_blkno)
    279  1.1  jdolecek 							+ entryoffsetinblock;
    280  1.1  jdolecek 					saveoffset = dp->i_offset;
    281  1.1  jdolecek 				} else if (ino)
    282  1.1  jdolecek 					goto foundino;
    283  1.1  jdolecek #ifdef	NOSORTBUG	/* On some CDs directory entries are not sorted correctly */
    284  1.1  jdolecek 				else if (res < 0)
    285  1.1  jdolecek 					goto notfound;
    286  1.1  jdolecek 				else if (res > 0 && numdirpasses == 2)
    287  1.1  jdolecek 					numdirpasses++;
    288  1.1  jdolecek #endif
    289  1.1  jdolecek 			}
    290  1.1  jdolecek 			break;
    291  1.1  jdolecek 		case ISO_FTYPE_RRIP:
    292  1.1  jdolecek 			if (isonum_711(ep->flags)&2)
    293  1.1  jdolecek 				ino = isodirino(ep, imp);
    294  1.1  jdolecek 			else
    295  1.1  jdolecek 				ino = dbtob(bp->b_blkno) + entryoffsetinblock;
    296  1.1  jdolecek 			dp->i_ino = ino;
    297  1.1  jdolecek 			cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
    298  1.1  jdolecek 			if (namelen == cnp->cn_namelen) {
    299  1.1  jdolecek 				if (imp->im_flags & ISOFSMNT_RRCASEINS) {
    300  1.1  jdolecek 					if (strncasecmp(name, altname, namelen) == 0)
    301  1.1  jdolecek 						goto found;
    302  1.1  jdolecek 				} else {
    303  1.1  jdolecek 					if (memcmp(name, altname, namelen) == 0)
    304  1.1  jdolecek 						goto found;
    305  1.1  jdolecek 				}
    306  1.1  jdolecek 			}
    307  1.1  jdolecek 			ino = 0;
    308  1.1  jdolecek 			break;
    309  1.1  jdolecek 		}
    310  1.1  jdolecek 		dp->i_offset += reclen;
    311  1.1  jdolecek 		entryoffsetinblock += reclen;
    312  1.1  jdolecek 	}
    313  1.1  jdolecek 	if (ino) {
    314  1.1  jdolecek foundino:
    315  1.1  jdolecek 		dp->i_ino = ino;
    316  1.1  jdolecek 		if (saveoffset != dp->i_offset) {
    317  1.1  jdolecek 			if (lblkno(imp, dp->i_offset) !=
    318  1.1  jdolecek 			    lblkno(imp, saveoffset)) {
    319  1.1  jdolecek 				if (bp != NULL)
    320  1.1  jdolecek 					brelse(bp);
    321  1.1  jdolecek 				if ((error = VOP_BLKATOFF(vdp,
    322  1.1  jdolecek 					    (off_t)saveoffset, NULL, &bp)) != 0)
    323  1.1  jdolecek 					return (error);
    324  1.1  jdolecek 			}
    325  1.1  jdolecek 			entryoffsetinblock = saveoffset & bmask;
    326  1.1  jdolecek 			ep = (struct iso_directory_record *)
    327  1.1  jdolecek 				((char *)bp->b_data + entryoffsetinblock);
    328  1.1  jdolecek 			dp->i_offset = saveoffset;
    329  1.1  jdolecek 		}
    330  1.1  jdolecek 		goto found;
    331  1.1  jdolecek 	}
    332  1.1  jdolecek notfound:
    333  1.1  jdolecek 	/*
    334  1.1  jdolecek 	 * If we started in the middle of the directory and failed
    335  1.1  jdolecek 	 * to find our target, we must check the beginning as well.
    336  1.1  jdolecek 	 */
    337  1.1  jdolecek 	if (numdirpasses == 2) {
    338  1.1  jdolecek 		numdirpasses--;
    339  1.1  jdolecek 		dp->i_offset = 0;
    340  1.1  jdolecek 		endsearch = dp->i_diroff;
    341  1.1  jdolecek 		goto searchloop;
    342  1.1  jdolecek 	}
    343  1.1  jdolecek 	if (bp != NULL)
    344  1.1  jdolecek 		brelse(bp);
    345  1.1  jdolecek 
    346  1.1  jdolecek 	/*
    347  1.1  jdolecek 	 * Insert name into cache (as non-existent) if appropriate.
    348  1.1  jdolecek 	 */
    349  1.1  jdolecek 	if (cnp->cn_flags & MAKEENTRY)
    350  1.1  jdolecek 		cache_enter(vdp, *vpp, cnp);
    351  1.1  jdolecek 	if (nameiop == CREATE || nameiop == RENAME)
    352  1.1  jdolecek 		return (EROFS);
    353  1.1  jdolecek 	return (ENOENT);
    354  1.1  jdolecek 
    355  1.1  jdolecek found:
    356  1.1  jdolecek 	if (numdirpasses == 2)
    357  1.1  jdolecek 		iso_nchstats.ncs_pass2++;
    358  1.1  jdolecek 
    359  1.1  jdolecek 	/*
    360  1.1  jdolecek 	 * Found component in pathname.
    361  1.1  jdolecek 	 * If the final component of path name, save information
    362  1.1  jdolecek 	 * in the cache as to where the entry was found.
    363  1.1  jdolecek 	 */
    364  1.1  jdolecek 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
    365  1.1  jdolecek 		dp->i_diroff = dp->i_offset;
    366  1.1  jdolecek 
    367  1.1  jdolecek 	/*
    368  1.1  jdolecek 	 * Step through the translation in the name.  We do not `iput' the
    369  1.1  jdolecek 	 * directory because we may need it again if a symbolic link
    370  1.1  jdolecek 	 * is relative to the current directory.  Instead we save it
    371  1.1  jdolecek 	 * unlocked as "pdp".  We must get the target inode before unlocking
    372  1.1  jdolecek 	 * the directory to insure that the inode will not be removed
    373  1.1  jdolecek 	 * before we get it.  We prevent deadlock by always fetching
    374  1.1  jdolecek 	 * inodes from the root, moving down the directory tree. Thus
    375  1.1  jdolecek 	 * when following backward pointers ".." we must unlock the
    376  1.1  jdolecek 	 * parent directory before getting the requested directory.
    377  1.1  jdolecek 	 * There is a potential race condition here if both the current
    378  1.1  jdolecek 	 * and parent directories are removed before the `iget' for the
    379  1.1  jdolecek 	 * inode associated with ".." returns.  We hope that this occurs
    380  1.1  jdolecek 	 * infrequently since we cannot avoid this race condition without
    381  1.1  jdolecek 	 * implementing a sophisticated deadlock detection algorithm.
    382  1.1  jdolecek 	 * Note also that this simple deadlock detection scheme will not
    383  1.1  jdolecek 	 * work if the file system has any hard links other than ".."
    384  1.1  jdolecek 	 * that point backwards in the directory structure.
    385  1.1  jdolecek 	 */
    386  1.1  jdolecek 	pdp = vdp;
    387  1.1  jdolecek 	/*
    388  1.1  jdolecek 	 * If ino is different from dp->i_ino,
    389  1.1  jdolecek 	 * it's a relocated directory.
    390  1.1  jdolecek 	 */
    391  1.1  jdolecek 	if (flags & ISDOTDOT) {
    392  1.1  jdolecek 		VOP_UNLOCK(pdp, 0);	/* race to get the inode */
    393  1.1  jdolecek 		cnp->cn_flags |= PDIRUNLOCK;
    394  1.1  jdolecek 		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
    395  1.1  jdolecek 					     dp->i_ino != ino, ep);
    396  1.1  jdolecek 		brelse(bp);
    397  1.1  jdolecek 		if (error) {
    398  1.1  jdolecek 			if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
    399  1.1  jdolecek 				cnp->cn_flags &= ~PDIRUNLOCK;
    400  1.1  jdolecek 			return (error);
    401  1.1  jdolecek 		}
    402  1.1  jdolecek 		if (lockparent && (flags & ISLASTCN)) {
    403  1.1  jdolecek 			if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
    404  1.1  jdolecek 				vput(tdp);
    405  1.1  jdolecek 				return (error);
    406  1.1  jdolecek 			}
    407  1.1  jdolecek 			cnp->cn_flags &= ~PDIRUNLOCK;
    408  1.1  jdolecek 		}
    409  1.1  jdolecek 		*vpp = tdp;
    410  1.1  jdolecek 	} else if (dp->i_number == dp->i_ino) {
    411  1.1  jdolecek 		brelse(bp);
    412  1.1  jdolecek 		VREF(vdp);	/* we want ourself, ie "." */
    413  1.1  jdolecek 		*vpp = vdp;
    414  1.1  jdolecek 	} else {
    415  1.1  jdolecek 		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
    416  1.1  jdolecek 					     dp->i_ino != ino, ep);
    417  1.1  jdolecek 		brelse(bp);
    418  1.1  jdolecek 		if (error)
    419  1.1  jdolecek 			return (error);
    420  1.1  jdolecek 		if (!lockparent || !(flags & ISLASTCN)) {
    421  1.1  jdolecek 			VOP_UNLOCK(pdp, 0);
    422  1.1  jdolecek 			cnp->cn_flags |= PDIRUNLOCK;
    423  1.1  jdolecek 		}
    424  1.1  jdolecek 		*vpp = tdp;
    425  1.1  jdolecek 	}
    426  1.1  jdolecek 
    427  1.1  jdolecek 	/*
    428  1.1  jdolecek 	 * Insert name into cache if appropriate.
    429  1.1  jdolecek 	 */
    430  1.1  jdolecek 	if (cnp->cn_flags & MAKEENTRY)
    431  1.1  jdolecek 		cache_enter(vdp, *vpp, cnp);
    432  1.1  jdolecek 	return (0);
    433  1.1  jdolecek }
    434  1.1  jdolecek 
    435  1.1  jdolecek /*
    436  1.1  jdolecek  * Return buffer with the contents of block "offset" from the beginning of
    437  1.1  jdolecek  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
    438  1.1  jdolecek  * remaining space in the directory.
    439  1.1  jdolecek  */
    440  1.1  jdolecek int
    441  1.1  jdolecek cd9660_blkatoff(v)
    442  1.1  jdolecek 	void *v;
    443  1.1  jdolecek {
    444  1.1  jdolecek 	struct vop_blkatoff_args /* {
    445  1.1  jdolecek 		struct vnode *a_vp;
    446  1.1  jdolecek 		off_t a_offset;
    447  1.1  jdolecek 		char **a_res;
    448  1.1  jdolecek 		struct buf **a_bpp;
    449  1.1  jdolecek 	} */ *ap = v;
    450  1.1  jdolecek 	struct iso_node *ip;
    451  1.1  jdolecek 	struct iso_mnt *imp;
    452  1.1  jdolecek 	struct buf *bp;
    453  1.1  jdolecek 	daddr_t lbn;
    454  1.1  jdolecek 	int bsize, error;
    455  1.1  jdolecek 
    456  1.1  jdolecek 	ip = VTOI(ap->a_vp);
    457  1.1  jdolecek 	imp = ip->i_mnt;
    458  1.1  jdolecek 	lbn = lblkno(imp, ap->a_offset);
    459  1.1  jdolecek 	bsize = blksize(imp, ip, lbn);
    460  1.1  jdolecek 
    461  1.1  jdolecek 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
    462  1.1  jdolecek 		brelse(bp);
    463  1.1  jdolecek 		*ap->a_bpp = NULL;
    464  1.1  jdolecek 		return (error);
    465  1.1  jdolecek 	}
    466  1.1  jdolecek 	if (ap->a_res)
    467  1.1  jdolecek 		*ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset);
    468  1.1  jdolecek 	*ap->a_bpp = bp;
    469  1.1  jdolecek 	return (0);
    470  1.1  jdolecek }
    471