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