1 /* $OpenBSD: cd9660_lookup.c,v 1.24 2016/03/19 12:04:15 natano 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/file.h> 46 #include <sys/vnode.h> 47 #include <sys/lock.h> 48 #include <sys/mount.h> 49 #include <sys/systm.h> 50 #include <sys/malloc.h> 51 52 #include <isofs/cd9660/iso.h> 53 #include <isofs/cd9660/cd9660_extern.h> 54 #include <isofs/cd9660/cd9660_node.h> 55 #include <isofs/cd9660/iso_rrip.h> 56 57 struct nchstats iso_nchstats; 58 59 /* 60 * Convert a component of a pathname into a pointer to a locked inode. 61 * This is a very central and rather complicated routine. 62 * If the file system is not maintained in a strict tree hierarchy, 63 * this can result in a deadlock situation (see comments in code below). 64 * 65 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 66 * whether the name is to be looked up, created, renamed, or deleted. 67 * When CREATE, RENAME, or DELETE is specified, information usable in 68 * creating, renaming, or deleting a directory entry may be calculated. 69 * If flag has LOCKPARENT or'ed into it and the target of the pathname 70 * exists, lookup returns both the target and its parent directory locked. 71 * When creating or renaming and LOCKPARENT is specified, the target may 72 * not be ".". When deleting and LOCKPARENT is specified, the target may 73 * be "."., but the caller must check to ensure it does an vrele and iput 74 * instead of two iputs. 75 * 76 * Overall outline of cd9660_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 and LOCKPARENT), 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 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 93 */ 94 int 95 cd9660_lookup(v) 96 void *v; 97 { 98 struct vop_lookup_args *ap = v; 99 register struct vnode *vdp; /* vnode for directory being searched */ 100 register struct iso_node *dp; /* inode for directory being searched */ 101 register struct iso_mnt *imp; /* file system that directory is in */ 102 struct buf *bp; /* a buffer of directory entries */ 103 struct iso_directory_record *ep = NULL; 104 /* the current directory entry */ 105 int entryoffsetinblock; /* offset of ep in bp's buffer */ 106 int saveoffset = -1; /* offset of last directory entry in dir */ 107 int numdirpasses; /* strategy for directory search */ 108 doff_t endsearch; /* offset to end directory search */ 109 struct vnode *pdp; /* saved dp during symlink work */ 110 struct vnode *tdp; /* returned by cd9660_vget_internal */ 111 u_long bmask; /* block offset mask */ 112 int lockparent; /* 1 => lockparent flag is set */ 113 int error; 114 cdino_t ino = 0; 115 int reclen; 116 u_short namelen; 117 char *altname; 118 int res; 119 int assoc, len; 120 char *name; 121 struct vnode **vpp = ap->a_vpp; 122 struct componentname *cnp = ap->a_cnp; 123 struct ucred *cred = cnp->cn_cred; 124 int flags; 125 int nameiop = cnp->cn_nameiop; 126 struct proc *p = cnp->cn_proc; 127 128 cnp->cn_flags &= ~PDIRUNLOCK; 129 flags = cnp->cn_flags; 130 131 bp = NULL; 132 *vpp = NULL; 133 vdp = ap->a_dvp; 134 dp = VTOI(vdp); 135 imp = dp->i_mnt; 136 lockparent = flags & LOCKPARENT; 137 138 /* 139 * Check accessiblity of directory. 140 */ 141 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0) 142 return (error); 143 144 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) && 145 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 146 return (EROFS); 147 148 /* 149 * We now have a segment name to search for, and a directory to search. 150 * 151 * Before tediously performing a linear scan of the directory, 152 * check the name cache to see if the directory/name pair 153 * we are looking for is known already. 154 */ 155 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0) 156 return (error); 157 158 len = cnp->cn_namelen; 159 name = cnp->cn_nameptr; 160 /* 161 * A leading `=' means, we are looking for an associated file 162 */ 163 assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR); 164 if (assoc) { 165 len--; 166 name++; 167 } 168 169 /* 170 * If there is cached information on a previous search of 171 * this directory, pick up where we last left off. 172 * We cache only lookups as these are the most common 173 * and have the greatest payoff. Caching CREATE has little 174 * benefit as it usually must search the entire directory 175 * to determine that the entry does not exist. Caching the 176 * location of the last DELETE or RENAME has not reduced 177 * profiling time and hence has been removed in the interest 178 * of simplicity. 179 */ 180 bmask = imp->im_bmask; 181 if (nameiop != LOOKUP || dp->i_diroff == 0 || 182 dp->i_diroff > dp->i_size) { 183 entryoffsetinblock = 0; 184 dp->i_offset = 0; 185 numdirpasses = 1; 186 } else { 187 dp->i_offset = dp->i_diroff; 188 if ((entryoffsetinblock = dp->i_offset & bmask) && 189 (error = cd9660_bufatoff(dp, (off_t)dp->i_offset, NULL, 190 &bp))) 191 return (error); 192 numdirpasses = 2; 193 iso_nchstats.ncs_2passes++; 194 } 195 endsearch = dp->i_size; 196 197 searchloop: 198 while (dp->i_offset < endsearch) { 199 /* 200 * If offset is on a block boundary, 201 * read the next directory block. 202 * Release previous if it exists. 203 */ 204 if ((dp->i_offset & bmask) == 0) { 205 if (bp != NULL) 206 brelse(bp); 207 error = cd9660_bufatoff(dp, (off_t)dp->i_offset, 208 NULL, &bp); 209 if (error) 210 return (error); 211 entryoffsetinblock = 0; 212 } 213 /* 214 * Get pointer to next entry. 215 */ 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, imp->joliet_level))) { 264 if (isonum_711(ep->flags)&2) 265 ino = isodirino(ep, imp); 266 else 267 ino = dbtob(bp->b_blkno) 268 + entryoffsetinblock; 269 saveoffset = dp->i_offset; 270 } else if (ino) 271 goto foundino; 272 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 273 else if (res < 0) 274 goto notfound; 275 else if (res > 0 && numdirpasses == 2) 276 numdirpasses++; 277 #endif 278 } 279 break; 280 case ISO_FTYPE_RRIP: 281 if (isonum_711(ep->flags)&2) 282 ino = isodirino(ep, imp); 283 else 284 ino = dbtob(bp->b_blkno) + entryoffsetinblock; 285 dp->i_ino = ino; 286 altname = malloc(NAME_MAX, M_TEMP, M_WAITOK); 287 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 288 if (namelen == cnp->cn_namelen 289 && !bcmp(name,altname,namelen)) { 290 free(altname, M_TEMP, 0); 291 goto found; 292 } 293 free(altname, M_TEMP, 0); 294 ino = 0; 295 break; 296 } 297 dp->i_offset += reclen; 298 entryoffsetinblock += reclen; 299 } 300 if (ino) { 301 foundino: 302 dp->i_ino = ino; 303 if (saveoffset != dp->i_offset) { 304 if (lblkno(imp, dp->i_offset) != 305 lblkno(imp, saveoffset)) { 306 if (bp != NULL) 307 brelse(bp); 308 if ((error = cd9660_bufatoff(dp, 309 (off_t)saveoffset, NULL, &bp)) != 0) 310 return (error); 311 } 312 entryoffsetinblock = saveoffset & bmask; 313 ep = (struct iso_directory_record *) 314 ((char *)bp->b_data + entryoffsetinblock); 315 dp->i_offset = saveoffset; 316 } 317 goto found; 318 } 319 notfound: 320 /* 321 * If we started in the middle of the directory and failed 322 * to find our target, we must check the beginning as well. 323 */ 324 if (numdirpasses == 2) { 325 numdirpasses--; 326 dp->i_offset = 0; 327 endsearch = dp->i_diroff; 328 goto searchloop; 329 } 330 if (bp != NULL) 331 brelse(bp); 332 333 /* 334 * Insert name into cache (as non-existent) if appropriate. 335 */ 336 if (cnp->cn_flags & MAKEENTRY) 337 cache_enter(vdp, *vpp, cnp); 338 if (nameiop == CREATE || nameiop == RENAME) 339 return (EJUSTRETURN); 340 return (ENOENT); 341 342 found: 343 if (numdirpasses == 2) 344 iso_nchstats.ncs_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 * If ino is different from dp->i_ino, 376 * it's a relocated directory. 377 */ 378 if (flags & ISDOTDOT) { 379 brelse(bp); 380 VOP_UNLOCK(pdp, p); /* race to get the inode */ 381 cnp->cn_flags |= PDIRUNLOCK; 382 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 383 dp->i_ino != ino, NULL); 384 if (error) { 385 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p) == 0) 386 cnp->cn_flags &= ~PDIRUNLOCK; 387 return (error); 388 } 389 if (lockparent && (flags & ISLASTCN)) { 390 if ((error = vn_lock(pdp, LK_EXCLUSIVE, p))) { 391 vput(tdp); 392 return (error); 393 } 394 cnp->cn_flags &= ~PDIRUNLOCK; 395 } 396 *vpp = tdp; 397 } else if (dp->i_number == dp->i_ino) { 398 brelse(bp); 399 vref(vdp); /* we want ourself, ie "." */ 400 *vpp = vdp; 401 } else { 402 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 403 dp->i_ino != ino, ep); 404 brelse(bp); 405 if (error) 406 return (error); 407 if (!lockparent || !(flags & ISLASTCN)) { 408 VOP_UNLOCK(pdp, p); 409 cnp->cn_flags |= PDIRUNLOCK; 410 } 411 *vpp = tdp; 412 } 413 414 /* 415 * Insert name into cache if appropriate. 416 */ 417 if (cnp->cn_flags & MAKEENTRY) 418 cache_enter(vdp, *vpp, cnp); 419 return (0); 420 } 421 422 /* 423 * Return buffer with the contents of block "offset" from the beginning of 424 * directory "ip". If "res" is non-zero, fill it in with a pointer to the 425 * remaining space in the directory. 426 */ 427 int 428 cd9660_bufatoff(struct iso_node *ip, off_t offset, char **res, 429 struct buf **bpp) 430 { 431 struct iso_mnt *imp; 432 struct buf *bp; 433 daddr_t lbn; 434 int bsize, error; 435 struct vnode *vp = ITOV(ip); 436 437 imp = ip->i_mnt; 438 lbn = lblkno(imp, offset); 439 bsize = blksize(imp, ip, lbn); 440 441 if ((error = bread(vp, lbn, bsize, &bp)) != 0) { 442 brelse(bp); 443 *bpp = NULL; 444 return (error); 445 } 446 if (res) 447 *res = (char *)bp->b_data + blkoff(imp, offset); 448 *bpp = bp; 449 return (0); 450 } 451