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