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 * $Id: cd9660_lookup.c,v 1.4 1994/09/15 19:45:58 bde Exp $ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/namei.h> 47 #include <sys/buf.h> 48 #include <sys/file.h> 49 #include <sys/vnode.h> 50 #include <sys/mount.h> 51 52 #include <isofs/cd9660/iso.h> 53 #include <isofs/cd9660/cd9660_node.h> 54 #include <isofs/cd9660/iso_rrip.h> 55 #include <isofs/cd9660/cd9660_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 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 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(ap) 96 struct vop_lookup_args /* { 97 struct vnode *a_dvp; 98 struct vnode **a_vpp; 99 struct componentname *a_cnp; 100 } */ *ap; 101 { 102 register struct vnode *vdp; /* vnode for directory being searched */ 103 register struct iso_node *dp; /* inode for directory being searched */ 104 register struct iso_mnt *imp; /* file system that directory is in */ 105 struct buf *bp; /* a buffer of directory entries */ 106 struct iso_directory_record *ep = 0;/* the current directory entry */ 107 int entryoffsetinblock; /* offset of ep in bp's buffer */ 108 int saveoffset = 0; /* offset of last directory entry in dir */ 109 int numdirpasses; /* strategy for directory search */ 110 doff_t endsearch; /* offset to end directory search */ 111 struct iso_node *pdp; /* saved dp during symlink work */ 112 struct iso_node *tdp; /* returned by iget */ 113 int lockparent; /* 1 => lockparent flag is set */ 114 int wantparent; /* 1 => wantparent or lockparent flag */ 115 int error; 116 ino_t ino = 0; 117 int reclen; 118 u_short namelen; 119 char altname[NAME_MAX]; 120 int res; 121 int assoc, len; 122 char *name; 123 struct vnode **vpp = ap->a_vpp; 124 struct componentname *cnp = ap->a_cnp; 125 struct ucred *cred = cnp->cn_cred; 126 int flags = cnp->cn_flags; 127 int nameiop = cnp->cn_nameiop; 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 wantparent = flags & (LOCKPARENT|WANTPARENT); 136 137 /* 138 * Check accessiblity of directory. 139 */ 140 if (vdp->v_type != VDIR) 141 return (ENOTDIR); 142 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))) 143 return (error); 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 ((error = cache_lookup(vdp, vpp, cnp))) { 153 int vpid; /* capability number of vnode */ 154 155 if (error == ENOENT) 156 return (error); 157 #ifdef PARANOID 158 if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT)) 159 panic("ufs_lookup: .. through root"); 160 #endif 161 /* 162 * Get the next vnode in the path. 163 * See comment below starting `Step through' for 164 * an explaination of the locking protocol. 165 */ 166 pdp = dp; 167 dp = VTOI(*vpp); 168 vdp = *vpp; 169 vpid = vdp->v_id; 170 if (pdp == dp) { 171 VREF(vdp); 172 error = 0; 173 } else if (flags & ISDOTDOT) { 174 ISO_IUNLOCK(pdp); 175 error = vget(vdp, 1); 176 if (!error && lockparent && (flags & ISLASTCN)) 177 ISO_ILOCK(pdp); 178 } else { 179 error = vget(vdp, 1); 180 if (!lockparent || error || !(flags & ISLASTCN)) 181 ISO_IUNLOCK(pdp); 182 } 183 /* 184 * Check that the capability number did not change 185 * while we were waiting for the lock. 186 */ 187 if (!error) { 188 if (vpid == vdp->v_id) 189 return (0); 190 iso_iput(dp); 191 if (lockparent && pdp != dp && (flags & ISLASTCN)) 192 ISO_IUNLOCK(pdp); 193 } 194 ISO_ILOCK(pdp); 195 dp = pdp; 196 vdp = ITOV(dp); 197 *vpp = NULL; 198 } 199 200 len = cnp->cn_namelen; 201 name = cnp->cn_nameptr; 202 /* 203 * A leading `=' means, we are looking for an associated file 204 */ 205 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR))) 206 { 207 len--; 208 name++; 209 } 210 211 /* 212 * If there is cached information on a previous search of 213 * this directory, pick up where we last left off. 214 * We cache only lookups as these are the most common 215 * and have the greatest payoff. Caching CREATE has little 216 * benefit as it usually must search the entire directory 217 * to determine that the entry does not exist. Caching the 218 * location of the last DELETE or RENAME has not reduced 219 * profiling time and hence has been removed in the interest 220 * of simplicity. 221 */ 222 if (nameiop != LOOKUP || dp->i_diroff == 0 || 223 dp->i_diroff > dp->i_size) { 224 entryoffsetinblock = 0; 225 dp->i_offset = 0; 226 numdirpasses = 1; 227 } else { 228 dp->i_offset = dp->i_diroff; 229 entryoffsetinblock = iso_blkoff(imp, dp->i_offset); 230 if (entryoffsetinblock != 0) { 231 if ((error = iso_blkatoff(dp, dp->i_offset, &bp))) 232 return (error); 233 } 234 numdirpasses = 2; 235 iso_nchstats.ncs_2passes++; 236 } 237 endsearch = roundup(dp->i_size, imp->logical_block_size); 238 239 searchloop: 240 while (dp->i_offset < endsearch) { 241 /* 242 * If offset is on a block boundary, 243 * read the next directory block. 244 * Release previous if it exists. 245 */ 246 if (iso_blkoff(imp, dp->i_offset) == 0) { 247 if (bp != NULL) 248 brelse(bp); 249 if ((error = iso_blkatoff(dp, dp->i_offset, &bp))) 250 return (error); 251 entryoffsetinblock = 0; 252 } 253 /* 254 * Get pointer to next entry. 255 */ 256 ep = (struct iso_directory_record *) 257 (bp->b_un.b_addr + entryoffsetinblock); 258 259 reclen = isonum_711 (ep->length); 260 if (reclen == 0) { 261 /* skip to next block, if any */ 262 dp->i_offset = 263 roundup(dp->i_offset, imp->logical_block_size); 264 continue; 265 } 266 267 if (reclen < ISO_DIRECTORY_RECORD_SIZE) 268 /* illegal entry, stop */ 269 break; 270 271 if (entryoffsetinblock + reclen > imp->logical_block_size) 272 /* entries are not allowed to cross boundaries */ 273 break; 274 275 /* 276 * Check for a name match. 277 */ 278 namelen = isonum_711(ep->name_len); 279 280 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 281 /* illegal entry, stop */ 282 break; 283 284 switch (imp->iso_ftype) { 285 default: 286 if ((!(isonum_711(ep->flags)&4)) == !assoc) { 287 if ((len == 1 288 && *name == '.') 289 || (flags & ISDOTDOT)) { 290 if (namelen == 1 291 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) { 292 /* 293 * Save directory entry's inode number and 294 * reclen in ndp->ni_ufs area, and release 295 * directory buffer. 296 */ 297 isodirino(&dp->i_ino,ep,imp); 298 goto found; 299 } 300 if (namelen != 1 301 || ep->name[0] != 0) 302 goto notfound; 303 } else if (!(res = isofncmp(name,len, 304 ep->name,namelen))) { 305 if (isonum_711(ep->flags)&2) 306 isodirino(&ino,ep,imp); 307 else 308 ino = dbtob(bp->b_blkno) 309 + entryoffsetinblock; 310 saveoffset = dp->i_offset; 311 } else if (ino) 312 goto foundino; 313 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 314 else if (res < 0) 315 goto notfound; 316 else if (res > 0 && numdirpasses == 2) 317 numdirpasses++; 318 #endif 319 } 320 break; 321 case ISO_FTYPE_RRIP: 322 if (isonum_711(ep->flags)&2) 323 isodirino(&ino,ep,imp); 324 else 325 ino = dbtob(bp->b_blkno) + entryoffsetinblock; 326 dp->i_ino = ino; 327 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 328 if (namelen == cnp->cn_namelen 329 && !bcmp(name,altname,namelen)) 330 goto found; 331 ino = 0; 332 break; 333 } 334 dp->i_offset += reclen; 335 entryoffsetinblock += reclen; 336 } 337 if (ino) { 338 foundino: 339 dp->i_ino = ino; 340 if (saveoffset != dp->i_offset) { 341 if (iso_lblkno(imp,dp->i_offset) 342 != iso_lblkno(imp,saveoffset)) { 343 if (bp != NULL) 344 brelse(bp); 345 if ((error = iso_blkatoff(dp, saveoffset, &bp))) 346 return (error); 347 } 348 ep = (struct iso_directory_record *)(bp->b_un.b_addr 349 + iso_blkoff(imp,saveoffset)); 350 dp->i_offset = saveoffset; 351 } 352 goto found; 353 } 354 notfound: 355 /* 356 * If we started in the middle of the directory and failed 357 * to find our target, we must check the beginning as well. 358 */ 359 if (numdirpasses == 2) { 360 numdirpasses--; 361 dp->i_offset = 0; 362 endsearch = dp->i_diroff; 363 goto searchloop; 364 } 365 if (bp != NULL) 366 brelse(bp); 367 /* 368 * Insert name into cache (as non-existent) if appropriate. 369 */ 370 if (cnp->cn_flags & MAKEENTRY) 371 cache_enter(vdp, *vpp, cnp); 372 if (nameiop == CREATE || nameiop == RENAME) 373 return (EJUSTRETURN); 374 return (ENOENT); 375 376 found: 377 if (numdirpasses == 2) 378 iso_nchstats.ncs_pass2++; 379 if (bp != NULL) 380 brelse(bp); 381 382 /* 383 * Found component in pathname. 384 * If the final component of path name, save information 385 * in the cache as to where the entry was found. 386 */ 387 if ((flags & ISLASTCN) && nameiop == LOOKUP) 388 dp->i_diroff = dp->i_offset; 389 390 /* 391 * Step through the translation in the name. We do not `iput' the 392 * directory because we may need it again if a symbolic link 393 * is relative to the current directory. Instead we save it 394 * unlocked as "pdp". We must get the target inode before unlocking 395 * the directory to insure that the inode will not be removed 396 * before we get it. We prevent deadlock by always fetching 397 * inodes from the root, moving down the directory tree. Thus 398 * when following backward pointers ".." we must unlock the 399 * parent directory before getting the requested directory. 400 * There is a potential race condition here if both the current 401 * and parent directories are removed before the `iget' for the 402 * inode associated with ".." returns. We hope that this occurs 403 * infrequently since we cannot avoid this race condition without 404 * implementing a sophisticated deadlock detection algorithm. 405 * Note also that this simple deadlock detection scheme will not 406 * work if the file system has any hard links other than ".." 407 * that point backwards in the directory structure. 408 */ 409 pdp = dp; 410 /* 411 * If ino is different from dp->i_ino, 412 * it's a relocated directory. 413 */ 414 if (flags & ISDOTDOT) { 415 ISO_IUNLOCK(pdp); /* race to get the inode */ 416 if ((error = iso_iget(dp,dp->i_ino, 417 dp->i_ino != ino, 418 &tdp,ep))) { 419 ISO_ILOCK(pdp); 420 return (error); 421 } 422 if (lockparent && (flags & ISLASTCN)) 423 ISO_ILOCK(pdp); 424 *vpp = ITOV(tdp); 425 } else if (dp->i_number == dp->i_ino) { 426 VREF(vdp); /* we want ourself, ie "." */ 427 *vpp = vdp; 428 } else { 429 if ((error = iso_iget(dp,dp->i_ino,dp->i_ino!=ino,&tdp,ep))) 430 return (error); 431 if (!lockparent || !(flags & ISLASTCN)) 432 ISO_IUNLOCK(pdp); 433 *vpp = ITOV(tdp); 434 } 435 436 /* 437 * Insert name into cache if appropriate. 438 */ 439 if (cnp->cn_flags & MAKEENTRY) 440 cache_enter(vdp, *vpp, cnp); 441 return (0); 442 } 443 444 /* 445 * Return buffer with contents of block "offset" 446 * from the beginning of directory "ip". If "res" 447 * is non-zero, fill it in with a pointer to the 448 * remaining space in the directory. 449 */ 450 int 451 iso_blkatoff(ip, offset, bpp) 452 struct iso_node *ip; 453 doff_t offset; 454 struct buf **bpp; 455 { 456 register struct iso_mnt *imp = ip->i_mnt; 457 daddr_t lbn = iso_lblkno(imp,offset); 458 int bsize = iso_blksize(imp,ip,lbn); 459 struct buf *bp; 460 int error; 461 462 if ((error = bread(ITOV(ip),lbn,bsize,NOCRED,&bp))) { 463 brelse(bp); 464 *bpp = 0; 465 return (error); 466 } 467 *bpp = bp; 468 469 return (0); 470 } 471