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