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