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 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_lookup.c,v 1.25 2008/06/19 23:27:39 dillon Exp $ 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/proc.h> 48 #include <sys/namei.h> 49 #include <sys/buf.h> 50 #include <sys/vnode.h> 51 #include <sys/mount.h> 52 53 #include "iso.h" 54 #include "cd9660_node.h" 55 #include "iso_rrip.h" 56 57 /* 58 * Convert a component of a pathname into a pointer to a locked inode. 59 * This is a very central and rather complicated routine. 60 * If the file system is not maintained in a strict tree hierarchy, 61 * this can result in a deadlock situation (see comments in code below). 62 * 63 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 64 * whether the name is to be looked up, created, renamed, or deleted. 65 * When CREATE, RENAME, or DELETE is specified, information usable in 66 * creating, renaming, or deleting a directory entry may be calculated. 67 * If flag has LOCKPARENT or'ed into it and the target of the pathname 68 * exists, lookup returns both the target and its parent directory locked. 69 * When creating or renaming and LOCKPARENT is specified, the target may 70 * not be ".". When deleting and LOCKPARENT is specified, the target may 71 * be "."., but the caller must check to ensure it does an vrele and iput 72 * instead of two iputs. 73 * 74 * Overall outline of ufs_lookup: 75 * 76 * search for name in directory, to found or notfound 77 * notfound: 78 * if creating, return locked directory, leaving info on available slots 79 * else return error 80 * found: 81 * if at end of path and deleting, return information to allow delete 82 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 83 * inode and return info to allow rewrite 84 * if not at end, add name to cache; if at end and neither creating 85 * nor deleting, add name to cache 86 * 87 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 88 * 89 * cd9660_lookup(struct vnode *a_dvp, struct vnode **a_vpp, 90 * struct componentname *a_cnp) 91 */ 92 int 93 cd9660_lookup(struct vop_old_lookup_args *ap) 94 { 95 struct vnode *vdp; /* vnode for directory being searched */ 96 struct iso_node *dp; /* inode for directory being searched */ 97 struct iso_mnt *imp; /* file system that directory is in */ 98 struct buf *bp; /* a buffer of directory entries */ 99 struct iso_directory_record *ep = 0;/* the current directory entry */ 100 int entryoffsetinblock; /* offset of ep in bp's buffer */ 101 int saveoffset = 0; /* offset of last directory entry in dir */ 102 int numdirpasses; /* strategy for directory search */ 103 doff_t endsearch; /* offset to end directory search */ 104 struct vnode *pdp; /* saved dp during symlink work */ 105 struct vnode *tdp; /* returned by cd9660_vget_internal */ 106 u_long bmask; /* block offset mask */ 107 int lockparent; /* 1 => lockparent flag is set */ 108 int wantparent; /* 1 => wantparent or lockparent flag */ 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 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT); 130 cnp->cn_flags &= ~CNP_PDIRUNLOCK; 131 132 /* 133 * We now have a segment name to search for, and a directory to search. 134 */ 135 136 len = cnp->cn_namelen; 137 name = cnp->cn_nameptr; 138 /* 139 * A leading `=' means, we are looking for an associated file 140 */ 141 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR))) 142 { 143 len--; 144 name++; 145 } 146 147 /* 148 * If there is cached information on a previous search of 149 * this directory, pick up where we last left off. 150 * We cache only lookups as these are the most common 151 * and have the greatest payoff. Caching CREATE has little 152 * benefit as it usually must search the entire directory 153 * to determine that the entry does not exist. Caching the 154 * location of the last DELETE or RENAME has not reduced 155 * profiling time and hence has been removed in the interest 156 * of simplicity. 157 */ 158 bmask = imp->im_bmask; 159 if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 || 160 dp->i_diroff > dp->i_size) { 161 entryoffsetinblock = 0; 162 dp->i_offset = 0; 163 numdirpasses = 1; 164 } else { 165 dp->i_offset = dp->i_diroff; 166 if ((entryoffsetinblock = dp->i_offset & bmask) && 167 (error = cd9660_devblkatoff(vdp, (off_t)dp->i_offset, NULL, &bp))) 168 return (error); 169 numdirpasses = 2; 170 } 171 endsearch = dp->i_size; 172 173 searchloop: 174 while (dp->i_offset < endsearch) { 175 /* 176 * If offset is on a block boundary, 177 * read the next directory block. 178 * Release previous if it exists. 179 */ 180 if ((dp->i_offset & bmask) == 0) { 181 if (bp != NULL) 182 brelse(bp); 183 if ((error = 184 cd9660_devblkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0) 185 return (error); 186 entryoffsetinblock = 0; 187 } 188 /* 189 * Get pointer to next entry. 190 */ 191 ep = (struct iso_directory_record *) 192 ((char *)bp->b_data + entryoffsetinblock); 193 194 reclen = isonum_711(ep->length); 195 if (reclen == 0) { 196 /* skip to next block, if any */ 197 dp->i_offset = 198 (dp->i_offset & ~bmask) + imp->logical_block_size; 199 continue; 200 } 201 202 if (reclen < ISO_DIRECTORY_RECORD_SIZE) 203 /* illegal entry, stop */ 204 break; 205 206 if (entryoffsetinblock + reclen > imp->logical_block_size) 207 /* entries are not allowed to cross boundaries */ 208 break; 209 210 namelen = isonum_711(ep->name_len); 211 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA? 212 &ep->date[6]: ep->flags); 213 214 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen) 215 /* illegal entry, stop */ 216 break; 217 218 /* 219 * Check for a name match. 220 */ 221 switch (imp->iso_ftype) { 222 default: 223 if (!(isoflags & 4) == !assoc) { 224 if ((len == 1 225 && *name == '.') 226 || (flags & CNP_ISDOTDOT)) { 227 if (namelen == 1 228 && ep->name[0] == ((flags & CNP_ISDOTDOT) ? 1 : 0)) { 229 /* 230 * Save directory entry's inode number and 231 * release directory buffer. 232 */ 233 dp->i_ino = isodirino(ep, imp); 234 goto found; 235 } 236 if (namelen != 1 237 || ep->name[0] != 0) 238 goto notfound; 239 } else if (!(res = isofncmp(name, len, 240 ep->name, namelen, 241 imp->joliet_level, 242 imp->im_flags, 243 imp->im_d2l, 244 imp->im_l2d))) { 245 if (isoflags & 2) 246 ino = isodirino(ep, imp); 247 else 248 ino = bp->b_bio1.bio_offset + 249 entryoffsetinblock; 250 saveoffset = dp->i_offset; 251 } else if (ino) 252 goto foundino; 253 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */ 254 else if (res < 0) 255 goto notfound; 256 else if (res > 0 && numdirpasses == 2) 257 numdirpasses++; 258 #endif 259 } 260 break; 261 case ISO_FTYPE_RRIP: 262 if (isonum_711(ep->flags)&2) 263 ino = isodirino(ep, imp); 264 else 265 ino = bp->b_bio1.bio_offset + 266 entryoffsetinblock; 267 dp->i_ino = ino; 268 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp); 269 if (namelen == cnp->cn_namelen 270 && !bcmp(name,altname,namelen)) 271 goto found; 272 ino = 0; 273 break; 274 } 275 dp->i_offset += reclen; 276 entryoffsetinblock += reclen; 277 } 278 if (ino) { 279 foundino: 280 dp->i_ino = ino; 281 if (saveoffset != dp->i_offset) { 282 if (lblkno(imp, dp->i_offset) != 283 lblkno(imp, saveoffset)) { 284 if (bp != NULL) 285 brelse(bp); 286 if ((error = cd9660_devblkatoff(vdp, 287 (off_t)saveoffset, NULL, &bp)) != 0) 288 return (error); 289 } 290 entryoffsetinblock = saveoffset & bmask; 291 ep = (struct iso_directory_record *) 292 ((char *)bp->b_data + entryoffsetinblock); 293 dp->i_offset = saveoffset; 294 } 295 goto found; 296 } 297 notfound: 298 /* 299 * If we started in the middle of the directory and failed 300 * to find our target, we must check the beginning as well. 301 */ 302 if (numdirpasses == 2) { 303 numdirpasses--; 304 dp->i_offset = 0; 305 endsearch = dp->i_diroff; 306 goto searchloop; 307 } 308 if (bp != NULL) 309 brelse(bp); 310 311 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) 312 return (EROFS); 313 return (ENOENT); 314 315 found: 316 /* 317 * Found component in pathname. 318 * If the final component of path name, save information 319 * in the cache as to where the entry was found. 320 */ 321 if (nameiop == NAMEI_LOOKUP) 322 dp->i_diroff = dp->i_offset; 323 324 /* 325 * Step through the translation in the name. We do not `iput' the 326 * directory because we may need it again if a symbolic link 327 * is relative to the current directory. Instead we save it 328 * unlocked as "pdp". We must get the target inode before unlocking 329 * the directory to insure that the inode will not be removed 330 * before we get it. We prevent deadlock by always fetching 331 * inodes from the root, moving down the directory tree. Thus 332 * when following backward pointers ".." we must unlock the 333 * parent directory before getting the requested directory. 334 * There is a potential race condition here if both the current 335 * and parent directories are removed before the `iget' for the 336 * inode associated with ".." returns. We hope that this occurs 337 * infrequently since we cannot avoid this race condition without 338 * implementing a sophisticated deadlock detection algorithm. 339 * Note also that this simple deadlock detection scheme will not 340 * work if the file system has any hard links other than ".." 341 * that point backwards in the directory structure. 342 */ 343 pdp = vdp; 344 /* 345 * If ino is different from dp->i_ino, 346 * it's a relocated directory. 347 */ 348 if (flags & CNP_ISDOTDOT) { 349 vn_unlock(pdp); /* race to get the inode */ 350 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp, 351 dp->i_ino != ino, ep); 352 brelse(bp); 353 if (error) { 354 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY); 355 return (error); 356 } 357 if (lockparent) { 358 if ((error = vn_lock(pdp, LK_EXCLUSIVE)) != 0) { 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