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