cd9660_lookup.c revision 115542
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 * $FreeBSD: head/sys/fs/cd9660/cd9660_lookup.c 115542 2003-05-31 20:19:13Z phk $
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/namei.h>
47#include <sys/bio.h>
48#include <sys/buf.h>
49#include <sys/vnode.h>
50#include <sys/mount.h>
51
52#include <isofs/cd9660/iso.h>
53#include <isofs/cd9660/cd9660_node.h>
54#include <isofs/cd9660/iso_rrip.h>
55
56/*
57 * Convert a component of a pathname into a pointer to a locked inode.
58 * This is a very central and rather complicated routine.
59 * If the filesystem is not maintained in a strict tree hierarchy,
60 * this can result in a deadlock situation (see comments in code below).
61 *
62 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
63 * whether the name is to be looked up, created, renamed, or deleted.
64 * When CREATE, RENAME, or DELETE is specified, information usable in
65 * creating, renaming, or deleting a directory entry may be calculated.
66 * If flag has LOCKPARENT or'ed into it and the target of the pathname
67 * exists, lookup returns both the target and its parent directory locked.
68 * When creating or renaming and LOCKPARENT is specified, the target may
69 * not be ".".  When deleting and LOCKPARENT is specified, the target may
70 * be "."., but the caller must check to ensure it does an vrele and iput
71 * instead of two iputs.
72 *
73 * Overall outline of ufs_lookup:
74 *
75 *	search for name in directory, to found or notfound
76 * notfound:
77 *	if creating, return locked directory, leaving info on available slots
78 *	else return error
79 * found:
80 *	if at end of path and deleting, return information to allow delete
81 *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
82 *	  inode and return info to allow rewrite
83 *	if not at end, add name to cache; if at end and neither creating
84 *	  nor deleting, add name to cache
85 *
86 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
87 */
88int
89cd9660_lookup(ap)
90	struct vop_cachedlookup_args /* {
91		struct vnode *a_dvp;
92		struct vnode **a_vpp;
93		struct componentname *a_cnp;
94	} */ *ap;
95{
96	register struct vnode *vdp;	/* vnode for directory being searched */
97	register struct iso_node *dp;	/* inode for directory being searched */
98	register struct iso_mnt *imp;	/* filesystem that directory is in */
99	struct buf *bp;			/* a buffer of directory entries */
100	struct iso_directory_record *ep = 0;/* the current directory entry */
101	int entryoffsetinblock;		/* offset of ep in bp's buffer */
102	int saveoffset = 0;		/* offset of last directory entry in dir */
103	int numdirpasses;		/* strategy for directory search */
104	doff_t endsearch;		/* offset to end directory search */
105	struct vnode *pdp;		/* saved dp during symlink work */
106	struct vnode *tdp;		/* returned by cd9660_vget_internal */
107	u_long bmask;			/* block offset mask */
108	int lockparent;			/* 1 => lockparent flag is set */
109	int error;
110	ino_t ino = 0;
111	int reclen;
112	u_short namelen;
113	int isoflags;
114	char altname[NAME_MAX];
115	int res;
116	int assoc, len;
117	char *name;
118	struct vnode **vpp = ap->a_vpp;
119	struct componentname *cnp = ap->a_cnp;
120	int flags = cnp->cn_flags;
121	int nameiop = cnp->cn_nameiop;
122	struct thread *td = cnp->cn_thread;
123
124	bp = NULL;
125	*vpp = NULL;
126	vdp = ap->a_dvp;
127	dp = VTOI(vdp);
128	imp = dp->i_mnt;
129	lockparent = flags & LOCKPARENT;
130	cnp->cn_flags &= ~PDIRUNLOCK;
131
132	/*
133	 * We now have a segment name to search for, and a directory to search.
134	 */
135
136	len = cnp->cn_namelen;
137	name = cnp->cn_nameptr;
138	/*
139	 * A leading `=' means, we are looking for an associated file
140	 */
141	if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
142	{
143		len--;
144		name++;
145	}
146
147	/*
148	 * If there is cached information on a previous search of
149	 * this directory, pick up where we last left off.
150	 * We cache only lookups as these are the most common
151	 * and have the greatest payoff. Caching CREATE has little
152	 * benefit as it usually must search the entire directory
153	 * to determine that the entry does not exist. Caching the
154	 * location of the last DELETE or RENAME has not reduced
155	 * profiling time and hence has been removed in the interest
156	 * of simplicity.
157	 */
158	bmask = imp->im_bmask;
159	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
160	    dp->i_diroff > dp->i_size) {
161		entryoffsetinblock = 0;
162		dp->i_offset = 0;
163		numdirpasses = 1;
164	} else {
165		dp->i_offset = dp->i_diroff;
166		if ((entryoffsetinblock = dp->i_offset & bmask) &&
167		    (error = cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
168				return (error);
169		numdirpasses = 2;
170		nchstats.ncs_2passes++;
171	}
172	endsearch = dp->i_size;
173
174searchloop:
175	while (dp->i_offset < endsearch) {
176		/*
177		 * If offset is on a block boundary,
178		 * read the next directory block.
179		 * Release previous if it exists.
180		 */
181		if ((dp->i_offset & bmask) == 0) {
182			if (bp != NULL)
183				brelse(bp);
184			if ((error =
185			    cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
186				return (error);
187			entryoffsetinblock = 0;
188		}
189		/*
190		 * Get pointer to next entry.
191		 */
192		ep = (struct iso_directory_record *)
193			((char *)bp->b_data + entryoffsetinblock);
194
195		reclen = isonum_711(ep->length);
196		if (reclen == 0) {
197			/* skip to next block, if any */
198			dp->i_offset =
199			    (dp->i_offset & ~bmask) + imp->logical_block_size;
200			continue;
201		}
202
203		if (reclen < ISO_DIRECTORY_RECORD_SIZE)
204			/* illegal entry, stop */
205			break;
206
207		if (entryoffsetinblock + reclen > imp->logical_block_size)
208			/* entries are not allowed to cross boundaries */
209			break;
210
211		namelen = isonum_711(ep->name_len);
212		isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
213				      &ep->date[6]: ep->flags);
214
215		if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
216			/* illegal entry, stop */
217			break;
218
219		/*
220		 * Check for a name match.
221		 */
222		switch (imp->iso_ftype) {
223		default:
224			if (!(isoflags & 4) == !assoc) {
225				if ((len == 1
226				     && *name == '.')
227				    || (flags & ISDOTDOT)) {
228					if (namelen == 1
229					    && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
230						/*
231						 * Save directory entry's inode number and
232						 * release directory buffer.
233						 */
234						dp->i_ino = isodirino(ep, imp);
235						goto found;
236					}
237					if (namelen != 1
238					    || ep->name[0] != 0)
239						goto notfound;
240				} else if (!(res = isofncmp(name, len, ep->name, namelen, imp->joliet_level))) {
241					if (isoflags & 2)
242						ino = isodirino(ep, imp);
243					else
244						ino = dbtob(bp->b_blkno)
245							+ entryoffsetinblock;
246					saveoffset = dp->i_offset;
247				} else if (ino)
248					goto foundino;
249#ifdef	NOSORTBUG	/* On some CDs directory entries are not sorted correctly */
250				else if (res < 0)
251					goto notfound;
252				else if (res > 0 && numdirpasses == 2)
253					numdirpasses++;
254#endif
255			}
256			break;
257		case ISO_FTYPE_RRIP:
258			if (isonum_711(ep->flags)&2)
259				ino = isodirino(ep, imp);
260			else
261				ino = dbtob(bp->b_blkno) + entryoffsetinblock;
262			dp->i_ino = ino;
263			cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
264			if (namelen == cnp->cn_namelen
265			    && !bcmp(name,altname,namelen))
266				goto found;
267			ino = 0;
268			break;
269		}
270		dp->i_offset += reclen;
271		entryoffsetinblock += reclen;
272	}
273	if (ino) {
274foundino:
275		dp->i_ino = ino;
276		if (saveoffset != dp->i_offset) {
277			if (lblkno(imp, dp->i_offset) !=
278			    lblkno(imp, saveoffset)) {
279				if (bp != NULL)
280					brelse(bp);
281				if ((error = cd9660_blkatoff(vdp,
282				    (off_t)saveoffset, NULL, &bp)) != 0)
283					return (error);
284			}
285			entryoffsetinblock = saveoffset & bmask;
286			ep = (struct iso_directory_record *)
287				((char *)bp->b_data + entryoffsetinblock);
288			dp->i_offset = saveoffset;
289		}
290		goto found;
291	}
292notfound:
293	/*
294	 * If we started in the middle of the directory and failed
295	 * to find our target, we must check the beginning as well.
296	 */
297	if (numdirpasses == 2) {
298		numdirpasses--;
299		dp->i_offset = 0;
300		endsearch = dp->i_diroff;
301		goto searchloop;
302	}
303	if (bp != NULL)
304		brelse(bp);
305
306	/*
307	 * Insert name into cache (as non-existent) if appropriate.
308	 */
309	if (cnp->cn_flags & MAKEENTRY)
310		cache_enter(vdp, *vpp, cnp);
311	if (nameiop == CREATE || nameiop == RENAME)
312		return (EROFS);
313	return (ENOENT);
314
315found:
316	if (numdirpasses == 2)
317		nchstats.ncs_pass2++;
318
319	/*
320	 * Found component in pathname.
321	 * If the final component of path name, save information
322	 * in the cache as to where the entry was found.
323	 */
324	if ((flags & ISLASTCN) && nameiop == LOOKUP)
325		dp->i_diroff = dp->i_offset;
326
327	/*
328	 * Step through the translation in the name.  We do not `iput' the
329	 * directory because we may need it again if a symbolic link
330	 * is relative to the current directory.  Instead we save it
331	 * unlocked as "pdp".  We must get the target inode before unlocking
332	 * the directory to insure that the inode will not be removed
333	 * before we get it.  We prevent deadlock by always fetching
334	 * inodes from the root, moving down the directory tree. Thus
335	 * when following backward pointers ".." we must unlock the
336	 * parent directory before getting the requested directory.
337	 * There is a potential race condition here if both the current
338	 * and parent directories are removed before the `iget' for the
339	 * inode associated with ".." returns.  We hope that this occurs
340	 * infrequently since we cannot avoid this race condition without
341	 * implementing a sophisticated deadlock detection algorithm.
342	 * Note also that this simple deadlock detection scheme will not
343	 * work if the filesystem has any hard links other than ".."
344	 * that point backwards in the directory structure.
345	 */
346	pdp = vdp;
347	/*
348	 * If ino is different from dp->i_ino,
349	 * it's a relocated directory.
350	 */
351	if (flags & ISDOTDOT) {
352		VOP_UNLOCK(pdp, 0, td);	/* race to get the inode */
353		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino,
354					     LK_EXCLUSIVE, &tdp,
355					     dp->i_ino != ino, ep);
356		brelse(bp);
357		if (error) {
358			vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
359			return (error);
360		}
361		if (lockparent && (flags & ISLASTCN)) {
362			if ((error = vn_lock(pdp, LK_EXCLUSIVE, td)) != 0) {
363				cnp->cn_flags |= PDIRUNLOCK;
364				vput(tdp);
365				return (error);
366			}
367		} else
368			cnp->cn_flags |= PDIRUNLOCK;
369		*vpp = tdp;
370	} else if (dp->i_number == dp->i_ino) {
371		brelse(bp);
372		VREF(vdp);	/* we want ourself, ie "." */
373		*vpp = vdp;
374	} else {
375		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino,
376					     LK_EXCLUSIVE, &tdp,
377					     dp->i_ino != ino, ep);
378		brelse(bp);
379		if (error)
380			return (error);
381		if (!lockparent || !(flags & ISLASTCN)) {
382			cnp->cn_flags |= PDIRUNLOCK;
383			VOP_UNLOCK(pdp, 0, td);
384		}
385		*vpp = tdp;
386	}
387
388	/*
389	 * Insert name into cache if appropriate.
390	 */
391	if (cnp->cn_flags & MAKEENTRY)
392		cache_enter(vdp, *vpp, cnp);
393	return (0);
394}
395
396/*
397 * Return buffer with the contents of block "offset" from the beginning of
398 * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
399 * remaining space in the directory.
400 */
401int
402cd9660_blkatoff(vp, offset, res, bpp)
403	struct vnode *vp;
404	off_t offset;
405	char **res;
406	struct buf **bpp;
407{
408	struct iso_node *ip;
409	register struct iso_mnt *imp;
410	struct buf *bp;
411	daddr_t lbn;
412	int bsize, error;
413
414	ip = VTOI(vp);
415	imp = ip->i_mnt;
416	lbn = lblkno(imp, offset);
417	bsize = blksize(imp, ip, lbn);
418
419	if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
420		brelse(bp);
421		*bpp = NULL;
422		return (error);
423	}
424
425	/*
426	 * We must BMAP the buffer because the directory code may use b_blkno
427	 * to calculate the inode for certain types of directory entries.
428	 * We could get away with not doing it before we VMIO-backed the
429	 * directories because the buffers would get freed atomically with
430	 * the invalidation of their data.  But with VMIO-backed buffers
431	 * the buffers may be freed and then later reconstituted - and the
432	 * reconstituted buffer will have no knowledge of b_blkno.
433	 */
434	if (bp->b_blkno == bp->b_lblkno) {
435		error = VOP_BMAP(vp, bp->b_lblkno, NULL,
436			    &bp->b_blkno, NULL, NULL);
437		if (error) {
438                        bp->b_error = error;
439                        bp->b_ioflags |= BIO_ERROR;
440                        brelse(bp);
441			*bpp = NULL;
442                        return (error);
443                }
444        }
445
446	if (res)
447		*res = (char *)bp->b_data + blkoff(imp, offset);
448	*bpp = bp;
449	return (0);
450}
451