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