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