]> git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_lookup.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_lookup.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* $NetBSD: cd9660_lookup.c,v 1.13 1994/12/24 15:30:03 cgd Exp $ */
23
24 /*-
25 * Copyright (c) 1989, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley
29 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
30 * Support code is derived from software contributed to Berkeley
31 * by Atsushi Murai (amurai@spec.co.jp).
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
62 *
63 * @(#)cd9660_lookup.c 8.5 (Berkeley) 12/5/94
64
65
66
67 * HISTORY
68 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
69
70 */
71
72 #include <sys/param.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/namei.h>
76 #include <sys/buf.h>
77 #include <sys/file.h>
78 #include <sys/utfconv.h>
79 #include <sys/paths.h>
80
81 #include <isofs/cd9660/iso.h>
82 #include <isofs/cd9660/cd9660_node.h>
83 #include <isofs/cd9660/iso_rrip.h>
84 #include <isofs/cd9660/cd9660_rrip.h>
85
86 struct nchstats iso_nchstats;
87
88 /*
89 * Convert a component of a pathname into a pointer to a locked inode.
90 * This is a very central and rather complicated routine.
91 * If the file system is not maintained in a strict tree hierarchy,
92 * this can result in a deadlock situation (see comments in code below).
93 *
94 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
95 * whether the name is to be looked up, created, renamed, or deleted.
96 * When CREATE, RENAME, or DELETE is specified, information usable in
97 * creating, renaming, or deleting a directory entry may be calculated.
98 * If flag has LOCKPARENT or'ed into it and the target of the pathname
99 * exists, lookup returns both the target and its parent directory locked.
100 * When creating or renaming and LOCKPARENT is specified, the target may
101 * not be ".". When deleting and LOCKPARENT is specified, the target may
102 * be "."., but the caller must check to ensure it does an vrele and iput
103 * instead of two iputs.
104 *
105 * Overall outline of ufs_lookup:
106 *
107 * check accessibility of directory
108 * look for name in cache, if found, then if at end of path
109 * and deleting or creating, drop it, else return name
110 * search for name in directory, to found or notfound
111 * notfound:
112 * if creating, return locked directory, leaving info on available slots
113 * else return error
114 * found:
115 * if at end of path and deleting, return information to allow delete
116 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
117 * inode and return info to allow rewrite
118 * if not at end, add name to cache; if at end and neither creating
119 * nor deleting, add name to cache
120 *
121 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
122 */
123 int
124 cd9660_lookup(ap)
125 struct vop_lookup_args /* {
126 struct vnode *a_dvp;
127 struct vnode **a_vpp;
128 struct componentname *a_cnp;
129 } */ *ap;
130 {
131 register struct vnode *vdp; /* vnode for directory being searched */
132 register struct iso_node *dp; /* inode for directory being searched */
133 register struct iso_mnt *imp; /* file system that directory is in */
134 struct buf *bp; /* a buffer of directory entries */
135 struct iso_directory_record *ep = NULL;/* the current directory entry */
136 int entryoffsetinblock; /* offset of ep in bp's buffer */
137 int saveoffset = 0; /* offset of last directory entry in dir */
138 int numdirpasses; /* strategy for directory search */
139 doff_t endsearch; /* offset to end directory search */
140 struct vnode *pdp; /* saved dp during symlink work */
141 struct vnode *tdp; /* returned by cd9660_vget_internal */
142 u_long bmask; /* block offset mask */
143 int lockparent; /* 1 => lockparent flag is set */
144 int wantparent; /* 1 => wantparent or lockparent flag */
145 int wantrsrc; /* 1 => looking for resource fork */
146 int error;
147 ino_t ino = 0;
148 int reclen;
149 u_short namelen;
150 char altname[ISO_RRIP_NAMEMAX];
151 int res;
152 int len;
153 char *name;
154 struct vnode **vpp = ap->a_vpp;
155 struct componentname *cnp = ap->a_cnp;
156 struct ucred *cred = cnp->cn_cred;
157 int flags = cnp->cn_flags;
158 int nameiop = cnp->cn_nameiop;
159 struct proc *p = cnp->cn_proc;
160 int devBlockSize=0;
161 long rsrcsize;
162 size_t altlen;
163
164 bp = NULL;
165 *vpp = NULL;
166 vdp = ap->a_dvp;
167 dp = VTOI(vdp);
168 imp = dp->i_mnt;
169 lockparent = flags & LOCKPARENT;
170 wantparent = flags & (LOCKPARENT|WANTPARENT);
171 wantrsrc = 0;
172
173 /*
174 * Check accessiblity of directory.
175 */
176 if (vdp->v_type != VDIR)
177 return (ENOTDIR);
178 if ( (error = VOP_ACCESS(vdp, VEXEC, cred, p)) )
179 return (error);
180
181 /*
182 * Determine if we're looking for a resource fork
183 * note: this could cause a read off the end of the
184 * component name buffer in some rare cases.
185 */
186 if ((flags & ISLASTCN) == 0 &&
187 bcmp(&cnp->cn_nameptr[cnp->cn_namelen],
188 _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC) - 1) == 0) {
189 flags |= ISLASTCN;
190 cnp->cn_consume = sizeof(_PATH_RSRCFORKSPEC) - 1;
191 wantrsrc = 1;
192 }
193 /*
194 * We now have a segment name to search for, and a directory to search.
195 *
196 * Before tediously performing a linear scan of the directory,
197 * check the name cache to see if the directory/name pair
198 * we are looking for is known already.
199 * Note: resource forks are never in the name cache
200 */
201 if ((error = cache_lookup(vdp, vpp, cnp)) && !wantrsrc) {
202 int vpid; /* capability number of vnode */
203
204 if (error == ENOENT)
205 return (error);
206 #ifdef PARANOID
207 if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT))
208 panic("cd9660_lookup: .. through root");
209 #endif
210 /*
211 * Get the next vnode in the path.
212 * See comment below starting `Step through' for
213 * an explaination of the locking protocol.
214 */
215 pdp = vdp;
216 dp = VTOI(*vpp);
217 vdp = *vpp;
218 vpid = vdp->v_id;
219 if (pdp == vdp) {
220 VREF(vdp);
221 error = 0;
222 } else if (flags & ISDOTDOT) {
223 VOP_UNLOCK(pdp, 0, p);
224 error = vget(vdp, LK_EXCLUSIVE | LK_RETRY, p);
225 if (!error && lockparent && (flags & ISLASTCN))
226 error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p);
227 } else {
228 error = vget(vdp, LK_EXCLUSIVE | LK_RETRY, p);
229 if (!lockparent || error || !(flags & ISLASTCN))
230 VOP_UNLOCK(pdp, 0, p);
231 }
232 /*
233 * Check that the capability number did not change
234 * while we were waiting for the lock.
235 */
236 if (!error) {
237 if (vpid == vdp->v_id)
238 return (0);
239 vput(vdp);
240 if (lockparent && pdp != vdp && (flags & ISLASTCN))
241 VOP_UNLOCK(pdp, 0, p);
242 }
243 if ( (error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p)) )
244 return (error);
245 vdp = pdp;
246 dp = VTOI(pdp);
247 *vpp = NULL;
248 }
249
250 len = cnp->cn_namelen;
251 name = cnp->cn_nameptr;
252 altname[0] = '\0';
253 rsrcsize = 0;
254
255 /*
256 * Decode search name into UCS-2 (Unicode)
257 */
258 if ((imp->iso_ftype == ISO_FTYPE_JOLIET) &&
259 !((len == 1 && *name == '.') || (flags & ISDOTDOT))) {
260 int flags = 0;
261
262 if (BYTE_ORDER != BIG_ENDIAN)
263 flags |= UTF_REVERSE_ENDIAN;
264
265 (void) utf8_decodestr(name, len, (u_int16_t*) altname, &altlen,
266 sizeof(altname), 0, flags);
267 name = altname;
268 len = altlen;
269 }
270 /*
271 * If there is cached information on a previous search of
272 * this directory, pick up where we last left off.
273 * We cache only lookups as these are the most common
274 * and have the greatest payoff. Caching CREATE has little
275 * benefit as it usually must search the entire directory
276 * to determine that the entry does not exist. Caching the
277 * location of the last DELETE or RENAME has not reduced
278 * profiling time and hence has been removed in the interest
279 * of simplicity.
280 */
281 bmask = imp->im_bmask;
282 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
283 dp->i_diroff > dp->i_size) {
284 entryoffsetinblock = 0;
285 dp->i_offset = 0;
286 numdirpasses = 1;
287 } else {
288 dp->i_offset = dp->i_diroff;
289
290 if ((entryoffsetinblock = dp->i_offset & bmask) &&
291 (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
292 return (error);
293 numdirpasses = 2;
294 iso_nchstats.ncs_2passes++;
295 }
296 endsearch = dp->i_size;
297
298 searchloop:
299 while (dp->i_offset < endsearch) {
300 /*
301 * If offset is on a block boundary,
302 * read the next directory block.
303 * Release previous if it exists.
304 */
305 if ((dp->i_offset & bmask) == 0) {
306 if (bp != NULL)
307 brelse(bp);
308 if ( (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)) )
309 return (error);
310 entryoffsetinblock = 0;
311 }
312 /*
313 * Get pointer to next entry.
314 */
315 ep = (struct iso_directory_record *)
316 ((char *)bp->b_data + entryoffsetinblock);
317
318 reclen = isonum_711(ep->length);
319 if (reclen == 0) {
320 /* skip to next block, if any */
321 dp->i_offset =
322 (dp->i_offset & ~bmask) + imp->logical_block_size;
323 continue;
324 }
325
326 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
327 /* illegal entry, stop */
328 break;
329
330 if (entryoffsetinblock + reclen > imp->logical_block_size)
331 /* entries are not allowed to cross boundaries */
332 break;
333
334 namelen = isonum_711(ep->name_len);
335
336 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
337 /* illegal entry, stop */
338 break;
339
340 /* remember the size of resource forks (associated files) */
341 if ((isonum_711(ep->flags) & (directoryBit | associatedBit)) == associatedBit) {
342 if (namelen < sizeof(altname) && ino == 0) {
343 rsrcsize = isonum_733(ep->size);
344 bcopy(ep->name, altname, namelen);
345 altname[namelen] = '\0';
346 altlen = namelen;
347 }
348 }
349 /*
350 * Check for a name match.
351 */
352 if (imp->iso_ftype == ISO_FTYPE_RRIP) {
353 if ( isonum_711(ep->flags) & directoryBit )
354 ino = isodirino(ep, imp);
355 else
356 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
357 dp->i_ino = ino;
358 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
359 if (namelen == cnp->cn_namelen
360 && !bcmp(name,altname,namelen))
361 goto found;
362 ino = 0;
363 } else {
364 if ((!(isonum_711(ep->flags) & associatedBit)) == !wantrsrc) {
365 if ((len == 1
366 && *name == '.')
367 || (flags & ISDOTDOT)) {
368 if (namelen == 1
369 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
370 /*
371 * Save directory entry's inode number and
372 * release directory buffer.
373 */
374 dp->i_ino = isodirino(ep, imp);
375 goto found;
376 }
377 if (namelen != 1
378 || ep->name[0] != 0)
379 goto notfound;
380 } else if (imp->iso_ftype != ISO_FTYPE_JOLIET && !(res = isofncmp(name,len,
381 ep->name,namelen))) {
382 if ( isonum_711(ep->flags) & directoryBit )
383 ino = isodirino(ep, imp);
384 else
385 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
386 saveoffset = dp->i_offset;
387 } else if (imp->iso_ftype == ISO_FTYPE_JOLIET && !(res = ucsfncmp((u_int16_t*)name, len,
388 (u_int16_t*) ep->name, namelen))) {
389 if ( isonum_711(ep->flags) & directoryBit )
390 ino = isodirino(ep, imp);
391 else
392 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
393 saveoffset = dp->i_offset;
394 } else if (ino)
395 goto foundino;
396 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
397 else if (res < 0)
398 goto notfound;
399 else if (res > 0 && numdirpasses == 2)
400 numdirpasses++;
401 #endif
402 }
403 }
404 dp->i_offset += reclen;
405 entryoffsetinblock += reclen;
406 } /* endwhile */
407
408 if (ino) {
409 foundino:
410 dp->i_ino = ino;
411 if (saveoffset != dp->i_offset) {
412 if (lblkno(imp, dp->i_offset) !=
413 lblkno(imp, saveoffset)) {
414 if (bp != NULL)
415 brelse(bp);
416 if ( (error = VOP_BLKATOFF(vdp, (off_t)saveoffset, NULL, &bp)) )
417 return (error);
418 }
419 entryoffsetinblock = saveoffset & bmask;
420 ep = (struct iso_directory_record *)
421 ((char *)bp->b_data + entryoffsetinblock);
422 dp->i_offset = saveoffset;
423 }
424 goto found;
425 }
426 notfound:
427 /*
428 * If we started in the middle of the directory and failed
429 * to find our target, we must check the beginning as well.
430 */
431 if (numdirpasses == 2) {
432 numdirpasses--;
433 dp->i_offset = 0;
434 endsearch = dp->i_diroff;
435 goto searchloop;
436 }
437 if (bp != NULL)
438 brelse(bp);
439
440 /*
441 * Insert name into cache (as non-existent) if appropriate.
442 */
443 if ((cnp->cn_flags & MAKEENTRY) && !wantrsrc)
444 cache_enter(vdp, *vpp, cnp);
445 if (nameiop == CREATE || nameiop == RENAME) {
446 /*
447 * return EROFS (NOT EJUSTRETURN). The caller will then unlock
448 * the parent for us.
449 */
450 return (EROFS);
451 }
452
453 if (wantrsrc)
454 return (ENOTDIR);
455 else
456 return (ENOENT);
457
458 found:
459 if (numdirpasses == 2)
460 iso_nchstats.ncs_pass2++;
461
462 /*
463 * Found component in pathname.
464 * If the final component of path name, save information
465 * in the cache as to where the entry was found.
466 */
467 if ((flags & ISLASTCN) && nameiop == LOOKUP)
468 dp->i_diroff = dp->i_offset;
469
470 /*
471 * Step through the translation in the name. We do not `iput' the
472 * directory because we may need it again if a symbolic link
473 * is relative to the current directory. Instead we save it
474 * unlocked as "pdp". We must get the target inode before unlocking
475 * the directory to insure that the inode will not be removed
476 * before we get it. We prevent deadlock by always fetching
477 * inodes from the root, moving down the directory tree. Thus
478 * when following backward pointers ".." we must unlock the
479 * parent directory before getting the requested directory.
480 * There is a potential race condition here if both the current
481 * and parent directories are removed before the `iget' for the
482 * inode associated with ".." returns. We hope that this occurs
483 * infrequently since we cannot avoid this race condition without
484 * implementing a sophisticated deadlock detection algorithm.
485 * Note also that this simple deadlock detection scheme will not
486 * work if the file system has any hard links other than ".."
487 * that point backwards in the directory structure.
488 */
489 pdp = vdp;
490 /*
491 * If ino is different from dp->i_ino,
492 * it's a relocated directory.
493 */
494 if (flags & ISDOTDOT) {
495 VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
496 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
497 dp->i_ino != ino, ep, p);
498 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
499 brelse(bp);
500 if (error) {
501 VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p);
502 return (error);
503 }
504 if (lockparent && (flags & ISLASTCN) &&
505 (error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p))) {
506 vput(tdp);
507 return (error);
508 }
509 *vpp = tdp;
510 } else if (dp->i_number == dp->i_ino) {
511 brelse(bp);
512 VREF(vdp); /* we want ourself, ie "." */
513 *vpp = vdp;
514 } else {
515 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
516 dp->i_ino != ino, ep, p);
517 /* save parent inode number */
518 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
519 if (!wantrsrc && (tdp->v_type == VREG) && (rsrcsize > 0)) {
520 if (bcmp(ep->name, altname, altlen) == 0)
521 VTOI(tdp)->i_rsrcsize = rsrcsize;
522 }
523 brelse(bp);
524 if (error)
525 return (error);
526 if (!lockparent || !(flags & ISLASTCN))
527 VOP_UNLOCK(pdp, 0, p);
528 *vpp = tdp;
529 }
530
531 /*
532 * Insert name into cache if appropriate.
533 */
534 if ((cnp->cn_flags & MAKEENTRY) && !wantrsrc)
535 cache_enter(vdp, *vpp, cnp);
536
537 return (0);
538 }
539
540
541 /*
542 * Return buffer with the contents of block "offset" from the beginning of
543 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
544 * remaining space in the directory.
545 */
546 int
547 cd9660_blkatoff(ap)
548 struct vop_blkatoff_args /* {
549 struct vnode *a_vp;
550 off_t a_offset;
551 char **a_res;
552 struct buf **a_bpp;
553 } */ *ap;
554 {
555 struct iso_node *ip;
556 register struct iso_mnt *imp;
557 struct buf *bp;
558 daddr_t lbn;
559 int bsize, error;
560
561 ip = VTOI(ap->a_vp);
562 imp = ip->i_mnt;
563 lbn = lblkno(imp, ap->a_offset);
564 bsize = blksize(imp, ip, lbn);
565
566 if ( (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) ) {
567 brelse(bp);
568 *ap->a_bpp = NULL;
569 return (error);
570 }
571 if (ap->a_res)
572 *ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset);
573 *ap->a_bpp = bp;
574
575 return (0);
576 }