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