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