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