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