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