xref: /freebsd/sys/fs/cd9660/cd9660_lookup.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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: cd9660_lookup.c,v 1.14 1997/02/22 09:38:48 peter Exp $
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/buf.h>
48 #include <sys/file.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 #include <isofs/cd9660/cd9660_rrip.h>
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 vnode *pdp;		/* saved dp during symlink work */
110 	struct vnode *tdp;		/* returned by cd9660_vget_internal */
111 	u_long bmask;			/* block offset mask */
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 	int isoflags;
119 	char altname[NAME_MAX];
120 	int res;
121 	int assoc, len;
122 	char *name;
123 	struct vnode **vpp = ap->a_vpp;
124 	struct componentname *cnp = ap->a_cnp;
125 	struct ucred *cred = cnp->cn_cred;
126 	int flags = cnp->cn_flags;
127 	int nameiop = cnp->cn_nameiop;
128 	struct proc *p = cnp->cn_proc;
129 
130 	bp = NULL;
131 	*vpp = NULL;
132 	vdp = ap->a_dvp;
133 	dp = VTOI(vdp);
134 	imp = dp->i_mnt;
135 	lockparent = flags & LOCKPARENT;
136 	wantparent = flags & (LOCKPARENT|WANTPARENT);
137 
138 	/*
139 	 * Check accessiblity of directory.
140 	 */
141 	if (vdp->v_type != VDIR)
142 		return (ENOTDIR);
143 	if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))
144 		return (error);
145 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
146 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
147 		return (EROFS);
148 
149 	/*
150 	 * We now have a segment name to search for, and a directory to search.
151 	 *
152 	 * Before tediously performing a linear scan of the directory,
153 	 * check the name cache to see if the directory/name pair
154 	 * we are looking for is known already.
155 	 */
156 	if ((error = cache_lookup(vdp, vpp, cnp))) {
157 		u_long vpid;	/* capability number of vnode */
158 
159 		if (error == ENOENT)
160 			return (error);
161 #ifdef PARANOID
162 		if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT))
163 			panic("cd9660_lookup: .. through root");
164 #endif
165 		/*
166 		 * Get the next vnode in the path.
167 		 * See comment below starting `Step through' for
168 		 * an explaination of the locking protocol.
169 		 */
170 		pdp = vdp;
171 		dp = VTOI(*vpp);
172 		vdp = *vpp;
173 		vpid = vdp->v_id;
174 		if (pdp == vdp) {
175 			VREF(vdp);
176 			error = 0;
177 		} else if (flags & ISDOTDOT) {
178 			VOP_UNLOCK(pdp, 0, p);
179 			error = vget(vdp, LK_EXCLUSIVE, p);
180 			if (!error && lockparent && (flags & ISLASTCN))
181 				error = vn_lock(pdp, LK_EXCLUSIVE, p);
182 		} else {
183 			error = vget(vdp, LK_EXCLUSIVE, p);
184 			if (!lockparent || error || !(flags & ISLASTCN))
185 				VOP_UNLOCK(pdp, 0, p);
186 		}
187 		/*
188 		 * Check that the capability number did not change
189 		 * while we were waiting for the lock.
190 		 */
191 		if (!error) {
192 			if (vpid == vdp->v_id)
193 				return (0);
194 			vput(vdp);
195 			if (lockparent && pdp != vdp && (flags & ISLASTCN))
196 				VOP_UNLOCK(pdp, 0, p);
197 		}
198 		if (error = vn_lock(pdp, LK_EXCLUSIVE, p))
199 			return (error);
200 		vdp = pdp;
201 		dp = VTOI(pdp);
202 		*vpp = NULL;
203 	}
204 
205 	len = cnp->cn_namelen;
206 	name = cnp->cn_nameptr;
207 	/*
208 	 * A leading `=' means, we are looking for an associated file
209 	 */
210 	if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
211 	{
212 		len--;
213 		name++;
214 	}
215 
216 	/*
217 	 * If there is cached information on a previous search of
218 	 * this directory, pick up where we last left off.
219 	 * We cache only lookups as these are the most common
220 	 * and have the greatest payoff. Caching CREATE has little
221 	 * benefit as it usually must search the entire directory
222 	 * to determine that the entry does not exist. Caching the
223 	 * location of the last DELETE or RENAME has not reduced
224 	 * profiling time and hence has been removed in the interest
225 	 * of simplicity.
226 	 */
227 	bmask = imp->im_bmask;
228 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
229 	    dp->i_diroff > dp->i_size) {
230 		entryoffsetinblock = 0;
231 		dp->i_offset = 0;
232 		numdirpasses = 1;
233 	} else {
234 		dp->i_offset = dp->i_diroff;
235 		if ((entryoffsetinblock = dp->i_offset & bmask) &&
236 		    (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
237 				return (error);
238 		numdirpasses = 2;
239 		nchstats.ncs_2passes++;
240 	}
241 	endsearch = dp->i_size;
242 
243 searchloop:
244 	while (dp->i_offset < endsearch) {
245 		/*
246 		 * If offset is on a block boundary,
247 		 * read the next directory block.
248 		 * Release previous if it exists.
249 		 */
250 		if ((dp->i_offset & bmask) == 0) {
251 			if (bp != NULL)
252 				brelse(bp);
253 			if (error =
254 			    VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))
255 				return (error);
256 			entryoffsetinblock = 0;
257 		}
258 		/*
259 		 * Get pointer to next entry.
260 		 */
261 		ep = (struct iso_directory_record *)
262 			((char *)bp->b_data + entryoffsetinblock);
263 
264 		reclen = isonum_711(ep->length);
265 		if (reclen == 0) {
266 			/* skip to next block, if any */
267 			dp->i_offset =
268 			    (dp->i_offset & ~bmask) + imp->logical_block_size;
269 			continue;
270 		}
271 
272 		if (reclen < ISO_DIRECTORY_RECORD_SIZE)
273 			/* illegal entry, stop */
274 			break;
275 
276 		if (entryoffsetinblock + reclen > imp->logical_block_size)
277 			/* entries are not allowed to cross boundaries */
278 			break;
279 
280 		namelen = isonum_711(ep->name_len);
281 		isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
282 				      &ep->date[6]: ep->flags);
283 
284 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
285 			/* illegal entry, stop */
286 			break;
287 
288 		/*
289 		 * Check for a name match.
290 		 */
291 		switch (imp->iso_ftype) {
292 		default:
293 			if (!(isoflags & 4) == !assoc) {
294 				if ((len == 1
295 				     && *name == '.')
296 				    || (flags & ISDOTDOT)) {
297 					if (namelen == 1
298 					    && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
299 						/*
300 						 * Save directory entry's inode number and
301 						 * release directory buffer.
302 						 */
303 						dp->i_ino = isodirino(ep, imp);
304 						goto found;
305 					}
306 					if (namelen != 1
307 					    || ep->name[0] != 0)
308 						goto notfound;
309 				} else if (!(res = isofncmp(name,len,
310 							    ep->name,namelen))) {
311 					if (isoflags & 2)
312 						ino = isodirino(ep, imp);
313 					else
314 						ino = dbtob(bp->b_blkno)
315 							+ entryoffsetinblock;
316 					saveoffset = dp->i_offset;
317 				} else if (ino)
318 					goto foundino;
319 #ifdef	NOSORTBUG	/* On some CDs directory entries are not sorted correctly */
320 				else if (res < 0)
321 					goto notfound;
322 				else if (res > 0 && numdirpasses == 2)
323 					numdirpasses++;
324 #endif
325 			}
326 			break;
327 		case ISO_FTYPE_RRIP:
328 			if (isonum_711(ep->flags)&2)
329 				ino = isodirino(ep, imp);
330 			else
331 				ino = dbtob(bp->b_blkno) + entryoffsetinblock;
332 			dp->i_ino = ino;
333 			cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
334 			if (namelen == cnp->cn_namelen
335 			    && !bcmp(name,altname,namelen))
336 				goto found;
337 			ino = 0;
338 			break;
339 		}
340 		dp->i_offset += reclen;
341 		entryoffsetinblock += reclen;
342 	}
343 	if (ino) {
344 foundino:
345 		dp->i_ino = ino;
346 		if (saveoffset != dp->i_offset) {
347 			if (lblkno(imp, dp->i_offset) !=
348 			    lblkno(imp, saveoffset)) {
349 				if (bp != NULL)
350 					brelse(bp);
351 				if (error = VOP_BLKATOFF(vdp,
352 				    (off_t)saveoffset, NULL, &bp))
353 					return (error);
354 			}
355 			entryoffsetinblock = saveoffset & bmask;
356 			ep = (struct iso_directory_record *)
357 				((char *)bp->b_data + entryoffsetinblock);
358 			dp->i_offset = saveoffset;
359 		}
360 		goto found;
361 	}
362 notfound:
363 	/*
364 	 * If we started in the middle of the directory and failed
365 	 * to find our target, we must check the beginning as well.
366 	 */
367 	if (numdirpasses == 2) {
368 		numdirpasses--;
369 		dp->i_offset = 0;
370 		endsearch = dp->i_diroff;
371 		goto searchloop;
372 	}
373 	if (bp != NULL)
374 		brelse(bp);
375 
376 	/*
377 	 * Insert name into cache (as non-existent) if appropriate.
378 	 */
379 	if (cnp->cn_flags & MAKEENTRY)
380 		cache_enter(vdp, *vpp, cnp);
381 	if (nameiop == CREATE || nameiop == RENAME)
382 		return (EROFS);
383 	return (ENOENT);
384 
385 found:
386 	if (numdirpasses == 2)
387 		nchstats.ncs_pass2++;
388 
389 	/*
390 	 * Found component in pathname.
391 	 * If the final component of path name, save information
392 	 * in the cache as to where the entry was found.
393 	 */
394 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
395 		dp->i_diroff = dp->i_offset;
396 
397 	/*
398 	 * Step through the translation in the name.  We do not `iput' the
399 	 * directory because we may need it again if a symbolic link
400 	 * is relative to the current directory.  Instead we save it
401 	 * unlocked as "pdp".  We must get the target inode before unlocking
402 	 * the directory to insure that the inode will not be removed
403 	 * before we get it.  We prevent deadlock by always fetching
404 	 * inodes from the root, moving down the directory tree. Thus
405 	 * when following backward pointers ".." we must unlock the
406 	 * parent directory before getting the requested directory.
407 	 * There is a potential race condition here if both the current
408 	 * and parent directories are removed before the `iget' for the
409 	 * inode associated with ".." returns.  We hope that this occurs
410 	 * infrequently since we cannot avoid this race condition without
411 	 * implementing a sophisticated deadlock detection algorithm.
412 	 * Note also that this simple deadlock detection scheme will not
413 	 * work if the file system has any hard links other than ".."
414 	 * that point backwards in the directory structure.
415 	 */
416 	pdp = vdp;
417 	/*
418 	 * If ino is different from dp->i_ino,
419 	 * it's a relocated directory.
420 	 */
421 	if (flags & ISDOTDOT) {
422 		VOP_UNLOCK(pdp, 0, p);	/* race to get the inode */
423 		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
424 					     dp->i_ino != ino, ep);
425 		brelse(bp);
426 		if (error) {
427 			vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p);
428 			return (error);
429 		}
430 		if (lockparent && (flags & ISLASTCN) &&
431 		    (error = vn_lock(pdp, LK_EXCLUSIVE, p))) {
432 			vput(tdp);
433 			return (error);
434 		}
435 		*vpp = tdp;
436 	} else if (dp->i_number == dp->i_ino) {
437 		brelse(bp);
438 		VREF(vdp);	/* we want ourself, ie "." */
439 		*vpp = vdp;
440 	} else {
441 		error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
442 					     dp->i_ino != ino, ep);
443 		brelse(bp);
444 		if (error)
445 			return (error);
446 		if (!lockparent || !(flags & ISLASTCN))
447 			VOP_UNLOCK(pdp, 0, p);
448 		*vpp = tdp;
449 	}
450 
451 	/*
452 	 * Insert name into cache if appropriate.
453 	 */
454 	if (cnp->cn_flags & MAKEENTRY)
455 		cache_enter(vdp, *vpp, cnp);
456 	return (0);
457 }
458 
459 /*
460  * Return buffer with the contents of block "offset" from the beginning of
461  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
462  * remaining space in the directory.
463  */
464 int
465 cd9660_blkatoff(ap)
466 	struct vop_blkatoff_args /* {
467 		struct vnode *a_vp;
468 		off_t a_offset;
469 		char **a_res;
470 		struct buf **a_bpp;
471 	} */ *ap;
472 {
473 	struct iso_node *ip;
474 	register struct iso_mnt *imp;
475 	struct buf *bp;
476 	daddr_t lbn;
477 	int bsize, error;
478 
479 	ip = VTOI(ap->a_vp);
480 	imp = ip->i_mnt;
481 	lbn = lblkno(imp, ap->a_offset);
482 	bsize = blksize(imp, ip, lbn);
483 
484 	if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
485 		brelse(bp);
486 		*ap->a_bpp = NULL;
487 		return (error);
488 	}
489 	if (ap->a_res)
490 		*ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset);
491 	*ap->a_bpp = bp;
492 	return (0);
493 }
494