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