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 * %sccs.include.redist.c% 11 * 12 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91 13 * 14 * @(#)cd9660_lookup.c 8.2 (Berkeley) 01/23/94 15 */ 16 17 #include <sys/param.h> 18 #include <sys/namei.h> 19 #include <sys/buf.h> 20 #include <sys/file.h> 21 #include <sys/vnode.h> 22 #include <sys/mount.h> 23 24 #include <isofs/cd9660/iso.h> 25 #include <isofs/cd9660/cd9660_node.h> 26 #include <isofs/cd9660/iso_rrip.h> 27 #include <isofs/cd9660/cd9660_rrip.h> 28 29 struct nchstats iso_nchstats; 30 31 /* 32 * Convert a component of a pathname into a pointer to a locked inode. 33 * This is a very central and rather complicated routine. 34 * If the file system is not maintained in a strict tree hierarchy, 35 * this can result in a deadlock situation (see comments in code below). 36 * 37 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 38 * whether the name is to be looked up, created, renamed, or deleted. 39 * When CREATE, RENAME, or DELETE is specified, information usable in 40 * creating, renaming, or deleting a directory entry may be calculated. 41 * If flag has LOCKPARENT or'ed into it and the target of the pathname 42 * exists, lookup returns both the target and its parent directory locked. 43 * When creating or renaming and LOCKPARENT is specified, the target may 44 * not be ".". When deleting and LOCKPARENT is specified, the target may 45 * be "."., but the caller must check to ensure it does an vrele and iput 46 * instead of two iputs. 47 * 48 * Overall outline of ufs_lookup: 49 * 50 * check accessibility of directory 51 * look for name in cache, if found, then if at end of path 52 * and deleting or creating, drop it, else return name 53 * search for name in directory, to found or notfound 54 * notfound: 55 * if creating, return locked directory, leaving info on available slots 56 * else return error 57 * found: 58 * if at end of path and deleting, return information to allow delete 59 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 60 * inode and return info to allow rewrite 61 * if not at end, add name to cache; if at end and neither creating 62 * nor deleting, add name to cache 63 * 64 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 65 */ 66 cd9660_lookup(ap) 67 struct vop_lookup_args /* { 68 struct vnode *a_dvp; 69 struct vnode **a_vpp; 70 struct componentname *a_cnp; 71 } */ *ap; 72 { 73 register struct vnode *vdp; /* vnode for directory being searched */ 74 register struct iso_node *dp; /* inode for directory being searched */ 75 register struct iso_mnt *imp; /* file system that directory is in */ 76 struct buf *bp; /* a buffer of directory entries */ 77 struct iso_directory_record *ep;/* the current directory entry */ 78 int entryoffsetinblock; /* offset of ep in bp's buffer */ 79 int saveoffset; /* offset of last directory entry in dir */ 80 int numdirpasses; /* strategy for directory search */ 81 doff_t endsearch; /* offset to end directory search */ 82 struct iso_node *pdp; /* saved dp during symlink work */ 83 struct iso_node *tdp; /* returned by iget */ 84 int lockparent; /* 1 => lockparent flag is set */ 85 int wantparent; /* 1 => wantparent or lockparent flag */ 86 int error; 87 ino_t ino = 0; 88 int reclen; 89 u_short namelen; 90 char altname[NAME_MAX]; 91 int res; 92 int assoc, len; 93 char *name; 94 struct vnode **vpp = ap->a_vpp; 95 struct componentname *cnp = ap->a_cnp; 96 struct ucred *cred = cnp->cn_cred; 97 int flags = cnp->cn_flags; 98 int nameiop = cnp->cn_nameiop; 99 100 bp = NULL; 101 *vpp = NULL; 102 vdp = ap->a_dvp; 103 dp = VTOI(vdp); 104 imp = dp->i_mnt; 105 lockparent = flags & LOCKPARENT; 106 wantparent = flags & (LOCKPARENT|WANTPARENT); 107 108 /* 109 * Check accessiblity of directory. 110 */ 111 if (vdp->v_type != VDIR) 112 return (ENOTDIR); 113 if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) 114 return (error); 115 116 /* 117 * We now have a segment name to search for, and a directory to search. 118 * 119 * Before tediously performing a linear scan of the directory, 120 * check the name cache to see if the directory/name pair 121 * we are looking for is known already. 122 */ 123 if (error = cache_lookup(vdp, vpp, cnp)) { 124 int vpid; /* capability number of vnode */ 125 126 if (error == ENOENT) 127 return (error); 128 #ifdef PARANOID 129 if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT)) 130 panic("ufs_lookup: .. through root"); 131 #endif 132 /* 133 * Get the next vnode in the path. 134 * See comment below starting `Step through' for 135 * an explaination of the locking protocol. 136 */ 137 pdp = dp; 138 dp = VTOI(*vpp); 139 vdp = *vpp; 140 vpid = vdp->v_id; 141 if (pdp == dp) { 142 VREF(vdp); 143 error = 0; 144 } else if (flags & ISDOTDOT) { 145 ISO_IUNLOCK(pdp); 146 error = vget(vdp, 1); 147 if (!error && lockparent && (flags & ISLASTCN)) 148 ISO_ILOCK(pdp); 149 } else { 150 error = vget(vdp, 1); 151 if (!lockparent || error || !(flags & ISLASTCN)) 152 ISO_IUNLOCK(pdp); 153 } 154 /* 155 * Check that the capability number did not change 156 * while we were waiting for the lock. 157 */ 158 if (!error) { 159 if (vpid == vdp->v_id) 160 return (0); 161 iso_iput(dp); 162 if (lockparent && pdp != dp && (flags & ISLASTCN)) 163 ISO_IUNLOCK(pdp); 164 } 165 ISO_ILOCK(pdp); 166 dp = pdp; 167 vdp = ITOV(dp); 168 *vpp = NULL; 169 } 170 171 len = cnp->cn_namelen; 172 name = cnp->cn_nameptr; 173 /* 174 * A leading `=' means, we are looking for an associated file 175 */ 176 if (assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)) { 177 len--; 178 name++; 179 } 180 181 /* 182 * If there is cached information on a previous search of 183 * this directory, pick up where we last left off. 184 * We cache only lookups as these are the most common 185 * and have the greatest payoff. Caching CREATE has little 186 * benefit as it usually must search the entire directory 187 * to determine that the entry does not exist. Caching the 188 * location of the last DELETE or RENAME has not reduced 189 * profiling time and hence has been removed in the interest 190 * of simplicity. 191 */ 192 if (nameiop != LOOKUP || dp->i_diroff == 0 || 193 dp->i_diroff > dp->i_size) { 194 entryoffsetinblock = 0; 195 dp->i_offset = 0; 196 numdirpasses = 1; 197 } else { 198 dp->i_offset = dp->i_diroff; 199 entryoffsetinblock = iso_blkoff(imp, dp->i_offset); 200 if (entryoffsetinblock != 0) { 201 if (error = iso_blkatoff(dp, dp->i_offset, &bp)) 202 return (error); 203 } 204 numdirpasses = 2; 205 iso_nchstats.ncs_2passes++; 206 } 207 endsearch = roundup(dp->i_size, imp->logical_block_size); 208 209 searchloop: 210 while (dp->i_offset < endsearch) { 211 /* 212 * If offset is on a block boundary, 213 * read the next directory block. 214 * Release previous if it exists. 215 */ 216 if (iso_blkoff(imp, dp->i_offset) == 0) { 217 if (bp != NULL) 218 brelse(bp); 219 if (error = iso_blkatoff(dp, dp->i_offset, &bp)) 220 return (error); 221 entryoffsetinblock = 0; 222 } 223 /* 224 * Get pointer to next entry. 225 */ 226 ep = (struct iso_directory_record *) 227 (bp->b_un.b_addr + entryoffsetinblock); 228 229 reclen = isonum_711 (ep->length); 230 if (reclen == 0) { 231 /* skip to next block, if any */ 232 dp->i_offset = 233 roundup(dp->i_offset, imp->logical_block_size); 234 continue; 235 } 236 237 if (reclen < ISO_DIRECTORY_RECORD_SIZE) 238 /* illegal entry, stop */ 239 break; 240 241 if (entryoffsetinblock + reclen > imp->logical_block_size) 242 /* entries are not allowed to cross boundaries */ 243 break; 244 245 /* 246 * Check for a name match. 247 */ 248 namelen = isonum_711(ep->name_len); 249 250 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 251 /* illegal entry, stop */ 252 break; 253 254 switch (imp->iso_ftype) { 255 default: 256 if ((!(isonum_711(ep->flags)&4)) == !assoc) { 257 if ((len == 1 258 && *name == '.') 259 || (flags & ISDOTDOT)) { 260 if (namelen == 1 261 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) { 262 /* 263 * Save directory entry's inode number and 264 * reclen in ndp->ni_ufs area, and release 265 * directory buffer. 266 */ 267 isodirino(&dp->i_ino,ep,imp); 268 goto found; 269 } 270 if (namelen != 1 271 || ep->name[0] != 0) 272 goto notfound; 273 } else if (!(res = isofncmp(name,len, 274 ep->name,namelen))) { 275 if (isonum_711(ep->flags)&2) 276 isodirino(&ino,ep,imp); 277 else 278 ino = dbtob(bp->b_blkno) 279 + entryoffsetinblock; 280 saveoffset = dp->i_offset; 281 } else if (ino) 282 goto foundino; 283 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 284 else if (res < 0) 285 goto notfound; 286 else if (res > 0 && numdirpasses == 2) 287 numdirpasses++; 288 #endif 289 } 290 break; 291 case ISO_FTYPE_RRIP: 292 if (isonum_711(ep->flags)&2) 293 isodirino(&ino,ep,imp); 294 else 295 ino = dbtob(bp->b_blkno) + entryoffsetinblock; 296 dp->i_ino = ino; 297 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 298 if (namelen == cnp->cn_namelen 299 && !bcmp(name,altname,namelen)) 300 goto found; 301 ino = 0; 302 break; 303 } 304 dp->i_offset += reclen; 305 entryoffsetinblock += reclen; 306 } 307 if (ino) { 308 foundino: 309 dp->i_ino = ino; 310 if (saveoffset != dp->i_offset) { 311 if (iso_lblkno(imp,dp->i_offset) 312 != iso_lblkno(imp,saveoffset)) { 313 if (bp != NULL) 314 brelse(bp); 315 if (error = iso_blkatoff(dp, saveoffset, &bp)) 316 return (error); 317 } 318 ep = (struct iso_directory_record *)(bp->b_un.b_addr 319 + iso_blkoff(imp,saveoffset)); 320 dp->i_offset = saveoffset; 321 } 322 goto found; 323 } 324 notfound: 325 /* 326 * If we started in the middle of the directory and failed 327 * to find our target, we must check the beginning as well. 328 */ 329 if (numdirpasses == 2) { 330 numdirpasses--; 331 dp->i_offset = 0; 332 endsearch = dp->i_diroff; 333 goto searchloop; 334 } 335 if (bp != NULL) 336 brelse(bp); 337 /* 338 * Insert name into cache (as non-existent) if appropriate. 339 */ 340 if (cnp->cn_flags & MAKEENTRY) 341 cache_enter(vdp, *vpp, cnp); 342 if (nameiop == CREATE || nameiop == RENAME) 343 return (EJUSTRETURN); 344 return (ENOENT); 345 346 found: 347 if (numdirpasses == 2) 348 iso_nchstats.ncs_pass2++; 349 if (bp != NULL) 350 brelse(bp); 351 352 /* 353 * Found component in pathname. 354 * If the final component of path name, save information 355 * in the cache as to where the entry was found. 356 */ 357 if ((flags & ISLASTCN) && nameiop == LOOKUP) 358 dp->i_diroff = dp->i_offset; 359 360 /* 361 * Step through the translation in the name. We do not `iput' the 362 * directory because we may need it again if a symbolic link 363 * is relative to the current directory. Instead we save it 364 * unlocked as "pdp". We must get the target inode before unlocking 365 * the directory to insure that the inode will not be removed 366 * before we get it. We prevent deadlock by always fetching 367 * inodes from the root, moving down the directory tree. Thus 368 * when following backward pointers ".." we must unlock the 369 * parent directory before getting the requested directory. 370 * There is a potential race condition here if both the current 371 * and parent directories are removed before the `iget' for the 372 * inode associated with ".." returns. We hope that this occurs 373 * infrequently since we cannot avoid this race condition without 374 * implementing a sophisticated deadlock detection algorithm. 375 * Note also that this simple deadlock detection scheme will not 376 * work if the file system has any hard links other than ".." 377 * that point backwards in the directory structure. 378 */ 379 pdp = dp; 380 /* 381 * If ino is different from dp->i_ino, 382 * it's a relocated directory. 383 */ 384 if (flags & ISDOTDOT) { 385 ISO_IUNLOCK(pdp); /* race to get the inode */ 386 if (error = iso_iget(dp,dp->i_ino, 387 dp->i_ino != ino, 388 &tdp,ep)) { 389 ISO_ILOCK(pdp); 390 return (error); 391 } 392 if (lockparent && (flags & ISLASTCN)) 393 ISO_ILOCK(pdp); 394 *vpp = ITOV(tdp); 395 } else if (dp->i_number == dp->i_ino) { 396 VREF(vdp); /* we want ourself, ie "." */ 397 *vpp = vdp; 398 } else { 399 if (error = iso_iget(dp,dp->i_ino,dp->i_ino!=ino,&tdp,ep)) 400 return (error); 401 if (!lockparent || !(flags & ISLASTCN)) 402 ISO_IUNLOCK(pdp); 403 *vpp = ITOV(tdp); 404 } 405 406 /* 407 * Insert name into cache if appropriate. 408 */ 409 if (cnp->cn_flags & MAKEENTRY) 410 cache_enter(vdp, *vpp, cnp); 411 return (0); 412 } 413 414 /* 415 * Return buffer with contents of block "offset" 416 * from the beginning of directory "ip". If "res" 417 * is non-zero, fill it in with a pointer to the 418 * remaining space in the directory. 419 */ 420 iso_blkatoff(ip, offset, bpp) 421 struct iso_node *ip; 422 doff_t offset; 423 struct buf **bpp; 424 { 425 register struct iso_mnt *imp = ip->i_mnt; 426 daddr_t lbn = iso_lblkno(imp,offset); 427 int bsize = iso_blksize(imp,ip,lbn); 428 struct buf *bp; 429 int error; 430 431 if (error = bread(ITOV(ip),lbn,bsize,NOCRED,&bp)) { 432 brelse(bp); 433 *bpp = 0; 434 return (error); 435 } 436 *bpp = bp; 437 438 return (0); 439 } 440