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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91 35 * @(#)cd9660_lookup.c 8.2 (Berkeley) 1/23/94 36 * $FreeBSD: src/sys/isofs/cd9660/cd9660_lookup.c,v 1.23.2.2 2001/11/04 06:19:47 dillon Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/proc.h> 42 #include <sys/namei.h> 43 #include <sys/buf.h> 44 #include <sys/vnode.h> 45 #include <sys/mount.h> 46 47 #include "iso.h" 48 #include "cd9660_node.h" 49 #include "iso_rrip.h" 50 51 #include <sys/buf2.h> 52 53 /* 54 * Convert a component of a pathname into a pointer to a locked inode. 55 * This is a very central and rather complicated routine. 56 * If the filesystem is not maintained in a strict tree hierarchy, 57 * this can result in a deadlock situation (see comments in code below). 58 * 59 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 60 * whether the name is to be looked up, created, renamed, or deleted. 61 * When CREATE, RENAME, or DELETE is specified, information usable in 62 * creating, renaming, or deleting a directory entry may be calculated. 63 * If flag has LOCKPARENT or'ed into it and the target of the pathname 64 * exists, lookup returns both the target and its parent directory locked. 65 * When creating or renaming and LOCKPARENT is specified, the target may 66 * not be ".". When deleting and LOCKPARENT is specified, the target may 67 * be "."., but the caller must check to ensure it does an vrele and iput 68 * instead of two iputs. 69 * 70 * Overall outline of ufs_lookup: 71 * 72 * search for name in directory, to found or notfound 73 * notfound: 74 * if creating, return locked directory, leaving info on available slots 75 * else return error 76 * found: 77 * if at end of path and deleting, return information to allow delete 78 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 79 * inode and return info to allow rewrite 80 * if not at end, add name to cache; if at end and neither creating 81 * nor deleting, add name to cache 82 * 83 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 84 * 85 * cd9660_lookup(struct vnode *a_dvp, struct vnode **a_vpp, 86 * struct componentname *a_cnp) 87 */ 88 int 89 cd9660_lookup(struct vop_old_lookup_args *ap) 90 { 91 struct vnode *vdp; /* vnode for directory being searched */ 92 struct iso_node *dp; /* inode for directory being searched */ 93 struct iso_mnt *imp; /* filesystem that directory is in */ 94 struct buf *bp; /* a buffer of directory entries */ 95 struct iso_directory_record *ep = NULL;/* the current directory entry */ 96 int entryoffsetinblock; /* offset of ep in bp's buffer */ 97 int saveoffset = 0; /* offset of last directory entry in dir */ 98 int numdirpasses; /* strategy for directory search */ 99 doff_t endsearch; /* offset to end directory search */ 100 struct vnode *pdp; /* saved dp during symlink work */ 101 struct vnode *tdp; /* returned by cd9660_vget_internal */ 102 u_long bmask; /* block offset mask */ 103 int lockparent; /* 1 => lockparent flag is set */ 104 int error; 105 ino_t ino = 0; 106 int reclen; 107 u_short namelen; 108 int isoflags; 109 char altname[NAME_MAX]; 110 int res; 111 int assoc, len; 112 char *name; 113 struct vnode **vpp = ap->a_vpp; 114 struct componentname *cnp = ap->a_cnp; 115 int flags = cnp->cn_flags; 116 int nameiop = cnp->cn_nameiop; 117 118 bp = NULL; 119 *vpp = NULL; 120 vdp = ap->a_dvp; 121 dp = VTOI(vdp); 122 imp = dp->i_mnt; 123 lockparent = flags & CNP_LOCKPARENT; 124 cnp->cn_flags &= ~CNP_PDIRUNLOCK; 125 126 /* 127 * We now have a segment name to search for, and a directory to search. 128 */ 129 len = cnp->cn_namelen; 130 name = cnp->cn_nameptr; 131 132 /* 133 * A leading `=' means, we are looking for an associated file 134 */ 135 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR))) 136 { 137 len--; 138 name++; 139 } 140 141 /* 142 * If there is cached information on a previous search of 143 * this directory, pick up where we last left off. 144 * We cache only lookups as these are the most common 145 * and have the greatest payoff. Caching CREATE has little 146 * benefit as it usually must search the entire directory 147 * to determine that the entry does not exist. Caching the 148 * location of the last DELETE or RENAME has not reduced 149 * profiling time and hence has been removed in the interest 150 * of simplicity. 151 */ 152 bmask = imp->im_bmask; 153 if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 || 154 dp->i_diroff > dp->i_size) { 155 entryoffsetinblock = 0; 156 dp->i_offset = 0; 157 numdirpasses = 1; 158 } else { 159 dp->i_offset = dp->i_diroff; 160 if ((entryoffsetinblock = dp->i_offset & bmask) && 161 (error = cd9660_devblkatoff(vdp, (off_t)dp->i_offset, NULL, &bp))) 162 return (error); 163 numdirpasses = 2; 164 } 165 endsearch = dp->i_size; 166 167 searchloop: 168 while (dp->i_offset < endsearch) { 169 /* 170 * If offset is on a block boundary, 171 * read the next directory block. 172 * Release previous if it exists. 173 */ 174 if ((dp->i_offset & bmask) == 0) { 175 if (bp != NULL) 176 brelse(bp); 177 if ((error = 178 cd9660_devblkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0) 179 return (error); 180 entryoffsetinblock = 0; 181 } 182 /* 183 * Get pointer to next entry. 184 */ 185 ep = (struct iso_directory_record *) 186 ((char *)bp->b_data + entryoffsetinblock); 187 188 reclen = isonum_711(ep->length); 189 if (reclen == 0) { 190 /* skip to next block, if any */ 191 dp->i_offset = 192 (dp->i_offset & ~bmask) + imp->logical_block_size; 193 continue; 194 } 195 196 if (reclen < ISO_DIRECTORY_RECORD_SIZE) 197 /* illegal entry, stop */ 198 break; 199 200 if (entryoffsetinblock + reclen > imp->logical_block_size) 201 /* entries are not allowed to cross boundaries */ 202 break; 203 204 namelen = isonum_711(ep->name_len); 205 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA? 206 &ep->date[6]: ep->flags); 207 208 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 209 /* illegal entry, stop */ 210 break; 211 212 /* 213 * Check for a name match. 214 */ 215 switch (imp->iso_ftype) { 216 default: 217 if (!(isoflags & 4) == !assoc) { 218 if ((len == 1 219 && *name == '.') 220 || (flags & CNP_ISDOTDOT)) { 221 if (namelen == 1 222 && ep->name[0] == ((flags & CNP_ISDOTDOT) ? 1 : 0)) { 223 /* 224 * Save directory entry's inode number and 225 * release directory buffer. 226 */ 227 dp->i_ino = isodirino(ep, imp); 228 goto found; 229 } 230 if (namelen != 1 231 || ep->name[0] != 0) 232 goto notfound; 233 } else if (!(res = isofncmp(name, len, 234 ep->name, namelen, 235 imp->joliet_level, 236 imp->im_flags, 237 imp->im_d2l, 238 imp->im_l2d))) { 239 if (isoflags & 2) 240 ino = isodirino(ep, imp); 241 else 242 ino = bp->b_bio1.bio_offset 243 + entryoffsetinblock; 244 saveoffset = dp->i_offset; 245 } else if (ino) 246 goto foundino; 247 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 248 else if (res < 0) 249 goto notfound; 250 else if (res > 0 && numdirpasses == 2) 251 numdirpasses++; 252 #endif 253 } 254 break; 255 case ISO_FTYPE_RRIP: 256 if (isonum_711(ep->flags)&2) 257 ino = isodirino(ep, imp); 258 else 259 ino = bp->b_bio1.bio_offset + entryoffsetinblock; 260 dp->i_ino = ino; 261 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 262 if (namelen == cnp->cn_namelen 263 && !bcmp(name,altname,namelen)) 264 goto found; 265 ino = 0; 266 break; 267 } 268 dp->i_offset += reclen; 269 entryoffsetinblock += reclen; 270 } 271 if (ino) { 272 foundino: 273 dp->i_ino = ino; 274 if (saveoffset != dp->i_offset) { 275 if (lblkno(imp, dp->i_offset) != 276 lblkno(imp, saveoffset)) { 277 if (bp != NULL) 278 brelse(bp); 279 if ((error = cd9660_devblkatoff(vdp, 280 (off_t)saveoffset, NULL, &bp)) != 0) 281 return (error); 282 } 283 entryoffsetinblock = saveoffset & bmask; 284 ep = (struct iso_directory_record *) 285 ((char *)bp->b_data + entryoffsetinblock); 286 dp->i_offset = saveoffset; 287 } 288 goto found; 289 } 290 notfound: 291 /* 292 * If we started in the middle of the directory and failed 293 * to find our target, we must check the beginning as well. 294 */ 295 if (numdirpasses == 2) { 296 numdirpasses--; 297 dp->i_offset = 0; 298 endsearch = dp->i_diroff; 299 goto searchloop; 300 } 301 if (bp != NULL) 302 brelse(bp); 303 304 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) 305 return (EROFS); 306 return (ENOENT); 307 308 found: 309 /* 310 * Found component in pathname. 311 * If the final component of path name, save information 312 * in the cache as to where the entry was found. 313 */ 314 if (nameiop == NAMEI_LOOKUP) 315 dp->i_diroff = dp->i_offset; 316 317 /* 318 * Step through the translation in the name. We do not `iput' the 319 * directory because we may need it again if a symbolic link 320 * is relative to the current directory. Instead we save it 321 * unlocked as "pdp". We must get the target inode before unlocking 322 * the directory to insure that the inode will not be removed 323 * before we get it. We prevent deadlock by always fetching 324 * inodes from the root, moving down the directory tree. Thus 325 * when following backward pointers ".." we must unlock the 326 * parent directory before getting the requested directory. 327 * There is a potential race condition here if both the current 328 * and parent directories are removed before the `iget' for the 329 * inode associated with ".." returns. We hope that this occurs 330 * infrequently since we cannot avoid this race condition without 331 * implementing a sophisticated deadlock detection algorithm. 332 * Note also that this simple deadlock detection scheme will not 333 * work if the filesystem has any hard links other than ".." 334 * that point backwards in the directory structure. 335 */ 336 pdp = vdp; 337 338 /* 339 * If ino is different from dp->i_ino, 340 * it's a relocated directory. 341 */ 342 if (flags & CNP_ISDOTDOT) { 343 vn_unlock(pdp); /* race to get the inode */ 344 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 345 dp->i_ino != ino, ep); 346 brelse(bp); 347 if (error) { 348 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY); 349 return (error); 350 } 351 if (lockparent) { 352 error = vn_lock(pdp, LK_EXCLUSIVE | LK_FAILRECLAIM); 353 if (error) { 354 cnp->cn_flags |= CNP_PDIRUNLOCK; 355 vput(tdp); 356 return (error); 357 } 358 } else 359 cnp->cn_flags |= CNP_PDIRUNLOCK; 360 *vpp = tdp; 361 } else if (dp->i_number == dp->i_ino) { 362 brelse(bp); 363 vref(vdp); /* we want ourself, ie "." */ 364 *vpp = vdp; 365 } else { 366 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 367 dp->i_ino != ino, ep); 368 brelse(bp); 369 if (error) 370 return (error); 371 if (!lockparent) { 372 cnp->cn_flags |= CNP_PDIRUNLOCK; 373 vn_unlock(pdp); 374 } 375 *vpp = tdp; 376 } 377 return (0); 378 } 379 380 /* 381 * Return a buffer with the contents of block "offset" from the beginning of 382 * directory "ip". If "res" is non-zero, fill it in with a pointer to the 383 * remaining space in the directory. 384 */ 385 int 386 cd9660_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp) 387 { 388 struct iso_node *ip; 389 struct iso_mnt *imp; 390 struct buf *bp; 391 daddr_t lbn; 392 int bsize, error; 393 394 ip = VTOI(vp); 395 imp = ip->i_mnt; 396 lbn = lblkno(imp, offset); 397 bsize = blksize(imp, ip, lbn); 398 399 if ((error = bread(vp, lblktooff(imp, lbn), bsize, &bp)) != 0) { 400 brelse(bp); 401 *bpp = NULL; 402 return (error); 403 } 404 405 /* 406 * We must BMAP the buffer because the directory code may use 407 * bio_offset to calculate the inode for certain types of directory 408 * entries. We could get away with not doing it before we 409 * VMIO-backed the directories because the buffers would get freed 410 * atomically with the invalidation of their data. But with 411 * VMIO-backed buffers the buffers may be freed and then later 412 * reconstituted - and the reconstituted buffer will have no 413 * knowledge of bio_offset. 414 */ 415 if (bp->b_bio2.bio_offset == NOOFFSET) { 416 error = VOP_BMAP(vp, bp->b_bio1.bio_offset, 417 &bp->b_bio2.bio_offset, NULL, NULL, 418 BUF_CMD_READ); 419 if (error) { 420 bp->b_error = error; 421 bp->b_flags |= B_ERROR; 422 brelse(bp); 423 *bpp = NULL; 424 return (error); 425 } 426 } 427 428 if (res) 429 *res = (char *)bp->b_data + blkoff(imp, offset); 430 *bpp = bp; 431 return (0); 432 } 433 434 435 /* 436 * Return a buffer with the contents of block "offset" from the beginning of 437 * directory "ip". If "res" is non-zero, fill it in with a pointer to the 438 * remaining space in the directory. 439 * 440 * Use the underlying device vnode rather then the passed vnode for the 441 * buffer cache operation. This allows us to access meta-data conveniently 442 * without having to instantiate a VM object for the vnode. 443 * 444 * WARNING! Callers of this routine need to be careful when accessing 445 * the bio_offset. Since this is a device buffer, the device offset will 446 * be in bio1.bio_offset, not bio2.bio_offset. 447 */ 448 int 449 cd9660_devblkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp) 450 { 451 struct iso_node *ip; 452 struct iso_mnt *imp; 453 struct buf *bp; 454 daddr_t lbn; 455 off_t doffset; 456 int bsize, error; 457 458 ip = VTOI(vp); 459 imp = ip->i_mnt; 460 lbn = lblkno(imp, offset); 461 bsize = blksize(imp, ip, lbn); 462 463 error = VOP_BMAP(vp, lblktooff(imp, lbn), &doffset, NULL, NULL, 464 BUF_CMD_READ); 465 if (error) 466 return (error); 467 468 if ((error = bread(imp->im_devvp, doffset, bsize, &bp)) != 0) { 469 brelse(bp); 470 *bpp = NULL; 471 return (error); 472 } 473 if (res) 474 *res = (char *)bp->b_data + blkoff(imp, offset); 475 *bpp = bp; 476 return (0); 477 } 478