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*67375Smkm  *	@(#)cd9660_lookup.c	8.3 (Berkeley) 06/14/94
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 */
8265789Smckusick 	struct iso_node *pdp;		/* saved dp during symlink work */
8365789Smckusick 	struct iso_node *tdp;		/* returned by iget */
8465789Smckusick 	int lockparent;			/* 1 => lockparent flag is set */
8565789Smckusick 	int wantparent;			/* 1 => wantparent or lockparent flag */
8665789Smckusick 	int error;
8765789Smckusick 	ino_t ino = 0;
8865789Smckusick 	int reclen;
8965789Smckusick 	u_short namelen;
9065789Smckusick 	char altname[NAME_MAX];
9165789Smckusick 	int res;
9265789Smckusick 	int assoc, len;
9365789Smckusick 	char *name;
9465789Smckusick 	struct vnode **vpp = ap->a_vpp;
9565789Smckusick 	struct componentname *cnp = ap->a_cnp;
9665789Smckusick 	struct ucred *cred = cnp->cn_cred;
9765789Smckusick 	int flags = cnp->cn_flags;
9865789Smckusick 	int nameiop = cnp->cn_nameiop;
9965789Smckusick 
10065789Smckusick 	bp = NULL;
10165789Smckusick 	*vpp = NULL;
10265789Smckusick 	vdp = ap->a_dvp;
10365789Smckusick 	dp = VTOI(vdp);
10465789Smckusick 	imp = dp->i_mnt;
10565789Smckusick 	lockparent = flags & LOCKPARENT;
10665789Smckusick 	wantparent = flags & (LOCKPARENT|WANTPARENT);
10765789Smckusick 
10865789Smckusick 	/*
10965789Smckusick 	 * Check accessiblity of directory.
11065789Smckusick 	 */
11165789Smckusick 	if (vdp->v_type != VDIR)
112*67375Smkm 		return (ENOTDIR);
11365789Smckusick 	if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))
11465789Smckusick 		return (error);
11565789Smckusick 
11665789Smckusick 	/*
11765789Smckusick 	 * We now have a segment name to search for, and a directory to search.
11865789Smckusick 	 *
11965789Smckusick 	 * Before tediously performing a linear scan of the directory,
12065789Smckusick 	 * check the name cache to see if the directory/name pair
12165789Smckusick 	 * we are looking for is known already.
12265789Smckusick 	 */
12365789Smckusick 	if (error = cache_lookup(vdp, vpp, cnp)) {
12465789Smckusick 		int vpid;	/* capability number of vnode */
12565789Smckusick 
12665789Smckusick 		if (error == ENOENT)
12765789Smckusick 			return (error);
12865789Smckusick #ifdef PARANOID
12965789Smckusick 		if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT))
13065789Smckusick 			panic("ufs_lookup: .. through root");
13165789Smckusick #endif
13265789Smckusick 		/*
13365789Smckusick 		 * Get the next vnode in the path.
13465789Smckusick 		 * See comment below starting `Step through' for
13565789Smckusick 		 * an explaination of the locking protocol.
13665789Smckusick 		 */
13765789Smckusick 		pdp = dp;
13865789Smckusick 		dp = VTOI(*vpp);
13965789Smckusick 		vdp = *vpp;
14065789Smckusick 		vpid = vdp->v_id;
14165789Smckusick 		if (pdp == dp) {
14265789Smckusick 			VREF(vdp);
14365789Smckusick 			error = 0;
14465789Smckusick 		} else if (flags & ISDOTDOT) {
14565789Smckusick 			ISO_IUNLOCK(pdp);
14665789Smckusick 			error = vget(vdp, 1);
14765789Smckusick 			if (!error && lockparent && (flags & ISLASTCN))
14865789Smckusick 				ISO_ILOCK(pdp);
14965789Smckusick 		} else {
15065789Smckusick 			error = vget(vdp, 1);
15165789Smckusick 			if (!lockparent || error || !(flags & ISLASTCN))
15265789Smckusick 				ISO_IUNLOCK(pdp);
15365789Smckusick 		}
15465789Smckusick 		/*
15565789Smckusick 		 * Check that the capability number did not change
15665789Smckusick 		 * while we were waiting for the lock.
15765789Smckusick 		 */
15865789Smckusick 		if (!error) {
15965789Smckusick 			if (vpid == vdp->v_id)
16065789Smckusick 				return (0);
16165789Smckusick 			iso_iput(dp);
16265789Smckusick 			if (lockparent && pdp != dp && (flags & ISLASTCN))
16365789Smckusick 				ISO_IUNLOCK(pdp);
16465789Smckusick 		}
16565789Smckusick 		ISO_ILOCK(pdp);
16665789Smckusick 		dp = pdp;
16765789Smckusick 		vdp = ITOV(dp);
16865789Smckusick 		*vpp = NULL;
16965789Smckusick 	}
17065789Smckusick 
17165789Smckusick 	len = cnp->cn_namelen;
17265789Smckusick 	name = cnp->cn_nameptr;
17365789Smckusick 	/*
17465789Smckusick 	 * A leading `=' means, we are looking for an associated file
17565789Smckusick 	 */
17665789Smckusick 	if (assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)) {
17765789Smckusick 		len--;
17865789Smckusick 		name++;
17965789Smckusick 	}
18065789Smckusick 
18165789Smckusick 	/*
18265789Smckusick 	 * If there is cached information on a previous search of
18365789Smckusick 	 * this directory, pick up where we last left off.
18465789Smckusick 	 * We cache only lookups as these are the most common
18565789Smckusick 	 * and have the greatest payoff. Caching CREATE has little
18665789Smckusick 	 * benefit as it usually must search the entire directory
18765789Smckusick 	 * to determine that the entry does not exist. Caching the
18865789Smckusick 	 * location of the last DELETE or RENAME has not reduced
18965789Smckusick 	 * profiling time and hence has been removed in the interest
19065789Smckusick 	 * of simplicity.
19165789Smckusick 	 */
19265789Smckusick 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
19365789Smckusick 	    dp->i_diroff > dp->i_size) {
19465789Smckusick 		entryoffsetinblock = 0;
19565789Smckusick 		dp->i_offset = 0;
19665789Smckusick 		numdirpasses = 1;
19765789Smckusick 	} else {
19865789Smckusick 		dp->i_offset = dp->i_diroff;
19965789Smckusick 		entryoffsetinblock = iso_blkoff(imp, dp->i_offset);
20065789Smckusick 		if (entryoffsetinblock != 0) {
20165789Smckusick 			if (error = iso_blkatoff(dp, dp->i_offset, &bp))
20265789Smckusick 				return (error);
20365789Smckusick 		}
20465789Smckusick 		numdirpasses = 2;
20565789Smckusick 		iso_nchstats.ncs_2passes++;
20665789Smckusick 	}
20765789Smckusick 	endsearch = roundup(dp->i_size, imp->logical_block_size);
20865789Smckusick 
20965789Smckusick searchloop:
21065789Smckusick 	while (dp->i_offset < endsearch) {
21165789Smckusick 		/*
21265789Smckusick 		 * If offset is on a block boundary,
21365789Smckusick 		 * read the next directory block.
21465789Smckusick 		 * Release previous if it exists.
21565789Smckusick 		 */
21665789Smckusick 		if (iso_blkoff(imp, dp->i_offset) == 0) {
21765789Smckusick 			if (bp != NULL)
21865789Smckusick 				brelse(bp);
21965789Smckusick 			if (error = iso_blkatoff(dp, dp->i_offset, &bp))
22065789Smckusick 				return (error);
22165789Smckusick 			entryoffsetinblock = 0;
22265789Smckusick 		}
22365789Smckusick 		/*
22465789Smckusick 		 * Get pointer to next entry.
22565789Smckusick 		 */
22665789Smckusick 		ep = (struct iso_directory_record *)
22765789Smckusick 			(bp->b_un.b_addr + entryoffsetinblock);
22865789Smckusick 
22965789Smckusick 		reclen = isonum_711 (ep->length);
23065789Smckusick 		if (reclen == 0) {
23165789Smckusick 			/* skip to next block, if any */
23265789Smckusick 			dp->i_offset =
23365789Smckusick 				roundup(dp->i_offset, imp->logical_block_size);
23465789Smckusick 			continue;
23565789Smckusick 		}
23665789Smckusick 
23765789Smckusick 		if (reclen < ISO_DIRECTORY_RECORD_SIZE)
23865789Smckusick 			/* illegal entry, stop */
23965789Smckusick 			break;
24065789Smckusick 
24165789Smckusick 		if (entryoffsetinblock + reclen > imp->logical_block_size)
24265789Smckusick 			/* entries are not allowed to cross boundaries */
24365789Smckusick 			break;
24465789Smckusick 
24565789Smckusick 		/*
24665789Smckusick 		 * Check for a name match.
24765789Smckusick 		 */
24865789Smckusick 		namelen = isonum_711(ep->name_len);
24965789Smckusick 
25065789Smckusick 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
25165789Smckusick 			/* illegal entry, stop */
25265789Smckusick 			break;
25365789Smckusick 
25465789Smckusick 		switch (imp->iso_ftype) {
25565789Smckusick 		default:
25665789Smckusick 			if ((!(isonum_711(ep->flags)&4)) == !assoc) {
25765789Smckusick 				if ((len == 1
25865789Smckusick 				     && *name == '.')
25965789Smckusick 				    || (flags & ISDOTDOT)) {
26065789Smckusick 					if (namelen == 1
26165789Smckusick 					    && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
26265789Smckusick 						/*
26365789Smckusick 						 * Save directory entry's inode number and
26465789Smckusick 						 * reclen in ndp->ni_ufs area, and release
26565789Smckusick 						 * directory buffer.
26665789Smckusick 						 */
26765789Smckusick 						isodirino(&dp->i_ino,ep,imp);
26865789Smckusick 						goto found;
26965789Smckusick 					}
27065789Smckusick 					if (namelen != 1
27165789Smckusick 					    || ep->name[0] != 0)
27265789Smckusick 						goto notfound;
27365789Smckusick 				} else if (!(res = isofncmp(name,len,
27465789Smckusick 							    ep->name,namelen))) {
27565789Smckusick 					if (isonum_711(ep->flags)&2)
27665789Smckusick 						isodirino(&ino,ep,imp);
27765789Smckusick 					else
27865789Smckusick 						ino = dbtob(bp->b_blkno)
27965789Smckusick 							+ entryoffsetinblock;
28065789Smckusick 					saveoffset = dp->i_offset;
28165789Smckusick 				} else if (ino)
28265789Smckusick 					goto foundino;
28365789Smckusick #ifdef	NOSORTBUG	/* On some CDs directory entries are not sorted correctly */
28465789Smckusick 				else if (res < 0)
28565789Smckusick 					goto notfound;
28665789Smckusick 				else if (res > 0 && numdirpasses == 2)
28765789Smckusick 					numdirpasses++;
28865789Smckusick #endif
28965789Smckusick 			}
29065789Smckusick 			break;
29165789Smckusick 		case ISO_FTYPE_RRIP:
29265789Smckusick 			if (isonum_711(ep->flags)&2)
29365789Smckusick 				isodirino(&ino,ep,imp);
29465789Smckusick 			else
29565789Smckusick 				ino = dbtob(bp->b_blkno) + entryoffsetinblock;
29665789Smckusick 			dp->i_ino = ino;
29765855Smckusick 			cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
29865789Smckusick 			if (namelen == cnp->cn_namelen
29965789Smckusick 			    && !bcmp(name,altname,namelen))
30065789Smckusick 				goto found;
30165789Smckusick 			ino = 0;
30265789Smckusick 			break;
30365789Smckusick 		}
30465789Smckusick 		dp->i_offset += reclen;
30565789Smckusick 		entryoffsetinblock += reclen;
30665789Smckusick 	}
30765789Smckusick 	if (ino) {
30865789Smckusick foundino:
30965789Smckusick 		dp->i_ino = ino;
31065789Smckusick 		if (saveoffset != dp->i_offset) {
31165789Smckusick 			if (iso_lblkno(imp,dp->i_offset)
31265789Smckusick 			    != iso_lblkno(imp,saveoffset)) {
31365789Smckusick 				if (bp != NULL)
31465789Smckusick 					brelse(bp);
31565789Smckusick 				if (error = iso_blkatoff(dp, saveoffset, &bp))
31665789Smckusick 					return (error);
31765789Smckusick 			}
31865789Smckusick 			ep = (struct iso_directory_record *)(bp->b_un.b_addr
31965789Smckusick 							     + iso_blkoff(imp,saveoffset));
32065789Smckusick 			dp->i_offset = saveoffset;
32165789Smckusick 		}
32265789Smckusick 		goto found;
32365789Smckusick 	}
32465789Smckusick notfound:
32565789Smckusick 	/*
32665789Smckusick 	 * If we started in the middle of the directory and failed
32765789Smckusick 	 * to find our target, we must check the beginning as well.
32865789Smckusick 	 */
32965789Smckusick 	if (numdirpasses == 2) {
33065789Smckusick 		numdirpasses--;
33165789Smckusick 		dp->i_offset = 0;
33265789Smckusick 		endsearch = dp->i_diroff;
33365789Smckusick 		goto searchloop;
33465789Smckusick 	}
33565789Smckusick 	if (bp != NULL)
33665789Smckusick 		brelse(bp);
33765789Smckusick 	/*
33865789Smckusick 	 * Insert name into cache (as non-existent) if appropriate.
33965789Smckusick 	 */
34065789Smckusick 	if (cnp->cn_flags & MAKEENTRY)
34165789Smckusick 		cache_enter(vdp, *vpp, cnp);
34265789Smckusick 	if (nameiop == CREATE || nameiop == RENAME)
34365789Smckusick 		return (EJUSTRETURN);
34465789Smckusick 	return (ENOENT);
34565789Smckusick 
34665789Smckusick found:
34765789Smckusick 	if (numdirpasses == 2)
34865789Smckusick 		iso_nchstats.ncs_pass2++;
34965789Smckusick 	if (bp != NULL)
35065789Smckusick 		brelse(bp);
35165789Smckusick 
35265789Smckusick 	/*
35365789Smckusick 	 * Found component in pathname.
35465789Smckusick 	 * If the final component of path name, save information
35565789Smckusick 	 * in the cache as to where the entry was found.
35665789Smckusick 	 */
35765789Smckusick 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
35865789Smckusick 		dp->i_diroff = dp->i_offset;
35965789Smckusick 
36065789Smckusick 	/*
36165789Smckusick 	 * Step through the translation in the name.  We do not `iput' the
36265789Smckusick 	 * directory because we may need it again if a symbolic link
36365789Smckusick 	 * is relative to the current directory.  Instead we save it
36465789Smckusick 	 * unlocked as "pdp".  We must get the target inode before unlocking
36565789Smckusick 	 * the directory to insure that the inode will not be removed
36665789Smckusick 	 * before we get it.  We prevent deadlock by always fetching
36765789Smckusick 	 * inodes from the root, moving down the directory tree. Thus
36865789Smckusick 	 * when following backward pointers ".." we must unlock the
36965789Smckusick 	 * parent directory before getting the requested directory.
37065789Smckusick 	 * There is a potential race condition here if both the current
37165789Smckusick 	 * and parent directories are removed before the `iget' for the
37265789Smckusick 	 * inode associated with ".." returns.  We hope that this occurs
37365789Smckusick 	 * infrequently since we cannot avoid this race condition without
37465789Smckusick 	 * implementing a sophisticated deadlock detection algorithm.
37565789Smckusick 	 * Note also that this simple deadlock detection scheme will not
37665789Smckusick 	 * work if the file system has any hard links other than ".."
37765789Smckusick 	 * that point backwards in the directory structure.
37865789Smckusick 	 */
37965789Smckusick 	pdp = dp;
38065789Smckusick 	/*
38165789Smckusick 	 * If ino is different from dp->i_ino,
38265789Smckusick 	 * it's a relocated directory.
38365789Smckusick 	 */
38465789Smckusick 	if (flags & ISDOTDOT) {
38565789Smckusick 		ISO_IUNLOCK(pdp);	/* race to get the inode */
38665789Smckusick 		if (error = iso_iget(dp,dp->i_ino,
38765789Smckusick 				     dp->i_ino != ino,
38865789Smckusick 				     &tdp,ep)) {
38965789Smckusick 			ISO_ILOCK(pdp);
39065789Smckusick 			return (error);
39165789Smckusick 		}
39265789Smckusick 		if (lockparent && (flags & ISLASTCN))
39365789Smckusick 			ISO_ILOCK(pdp);
39465789Smckusick 		*vpp = ITOV(tdp);
39565789Smckusick 	} else if (dp->i_number == dp->i_ino) {
39665789Smckusick 		VREF(vdp);	/* we want ourself, ie "." */
39765789Smckusick 		*vpp = vdp;
39865789Smckusick 	} else {
39965789Smckusick 		if (error = iso_iget(dp,dp->i_ino,dp->i_ino!=ino,&tdp,ep))
40065789Smckusick 			return (error);
40165789Smckusick 		if (!lockparent || !(flags & ISLASTCN))
40265789Smckusick 			ISO_IUNLOCK(pdp);
40365789Smckusick 		*vpp = ITOV(tdp);
40465789Smckusick 	}
40565789Smckusick 
40665789Smckusick 	/*
40765789Smckusick 	 * Insert name into cache if appropriate.
40865789Smckusick 	 */
40965789Smckusick 	if (cnp->cn_flags & MAKEENTRY)
41065789Smckusick 		cache_enter(vdp, *vpp, cnp);
41165789Smckusick 	return (0);
41265789Smckusick }
41365789Smckusick 
41465789Smckusick /*
41565789Smckusick  * Return buffer with contents of block "offset"
41665789Smckusick  * from the beginning of directory "ip".  If "res"
41765789Smckusick  * is non-zero, fill it in with a pointer to the
41865789Smckusick  * remaining space in the directory.
41965789Smckusick  */
42065789Smckusick iso_blkatoff(ip, offset, bpp)
42165789Smckusick 	struct iso_node *ip;
42265789Smckusick 	doff_t offset;
42365789Smckusick 	struct buf **bpp;
42465789Smckusick {
42565789Smckusick 	register struct iso_mnt *imp = ip->i_mnt;
42665789Smckusick 	daddr_t lbn = iso_lblkno(imp,offset);
42765789Smckusick 	int bsize = iso_blksize(imp,ip,lbn);
42865789Smckusick 	struct buf *bp;
42965789Smckusick 	int error;
43065789Smckusick 
43165789Smckusick 	if (error = bread(ITOV(ip),lbn,bsize,NOCRED,&bp)) {
43265789Smckusick 		brelse(bp);
43365789Smckusick 		*bpp = 0;
43465789Smckusick 		return (error);
43565789Smckusick 	}
43665789Smckusick 	*bpp = bp;
43765789Smckusick 
43865789Smckusick 	return (0);
43965789Smckusick }
440