]> git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_lookup.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_lookup.c
1 /*
2 * Copyright (c) 2000-2003 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 #include <sys/param.h>
71 #include <sys/vnode.h>
72 #include <sys/mount.h>
73 #include <sys/namei.h>
74 #include <sys/buf.h>
75 #include <sys/file.h>
76 #include <sys/utfconv.h>
77
78 #include <isofs/cd9660/iso.h>
79 #include <isofs/cd9660/cd9660_node.h>
80 #include <isofs/cd9660/iso_rrip.h>
81 #include <isofs/cd9660/cd9660_rrip.h>
82
83 struct nchstats iso_nchstats;
84
85 /*
86 * Convert a component of a pathname into a pointer to a locked inode.
87 * This is a very central and rather complicated routine.
88 * If the file system is not maintained in a strict tree hierarchy,
89 * this can result in a deadlock situation (see comments in code below).
90 *
91 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
92 * whether the name is to be looked up, created, renamed, or deleted.
93 * When CREATE, RENAME, or DELETE is specified, information usable in
94 * creating, renaming, or deleting a directory entry may be calculated.
95 * If flag has LOCKPARENT or'ed into it and the target of the pathname
96 * exists, lookup returns both the target and its parent directory locked.
97 * When creating or renaming and LOCKPARENT is specified, the target may
98 * not be ".". When deleting and LOCKPARENT is specified, the target may
99 * be "."., but the caller must check to ensure it does an vrele and iput
100 * instead of two iputs.
101 *
102 * Overall outline of ufs_lookup:
103 *
104 * check accessibility of directory
105 * look for name in cache, if found, then if at end of path
106 * and deleting or creating, drop it, else return name
107 * search for name in directory, to found or notfound
108 * notfound:
109 * if creating, return locked directory, leaving info on available slots
110 * else return error
111 * found:
112 * if at end of path and deleting, return information to allow delete
113 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
114 * inode and return info to allow rewrite
115 * if not at end, add name to cache; if at end and neither creating
116 * nor deleting, add name to cache
117 *
118 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
119 */
120 int
121 cd9660_lookup(ap)
122 struct vop_lookup_args /* {
123 struct vnode *a_dvp;
124 struct vnode **a_vpp;
125 struct componentname *a_cnp;
126 } */ *ap;
127 {
128 register struct vnode *vdp; /* vnode for directory being searched */
129 register struct iso_node *dp; /* inode for directory being searched */
130 register struct iso_mnt *imp; /* file system that directory is in */
131 struct buf *bp; /* a buffer of directory entries */
132 struct iso_directory_record *ep = NULL;/* the current directory entry */
133 int entryoffsetinblock; /* offset of ep in bp's buffer */
134 int saveoffset = 0; /* offset of last directory entry in dir */
135 int numdirpasses; /* strategy for directory search */
136 doff_t endsearch; /* offset to end directory search */
137 struct vnode *pdp; /* saved dp during symlink work */
138 struct vnode *tdp; /* returned by cd9660_vget_internal */
139 u_long bmask; /* block offset mask */
140 int lockparent; /* 1 => lockparent flag is set */
141 int wantparent; /* 1 => wantparent or lockparent flag */
142 int wantassoc;
143 int error;
144 ino_t ino = 0;
145 int reclen;
146 u_short namelen;
147 int isoflags;
148 char altname[ISO_RRIP_NAMEMAX];
149 int res;
150 int len;
151 char *name;
152 struct vnode **vpp = ap->a_vpp;
153 struct componentname *cnp = ap->a_cnp;
154 int flags = cnp->cn_flags;
155 int nameiop = cnp->cn_nameiop;
156 struct proc *p = cnp->cn_proc;
157 int devBlockSize=0;
158 size_t altlen;
159
160 bp = NULL;
161 *vpp = NULL;
162 vdp = ap->a_dvp;
163 dp = VTOI(vdp);
164 imp = dp->i_mnt;
165 lockparent = flags & LOCKPARENT;
166 wantparent = flags & (LOCKPARENT|WANTPARENT);
167 wantassoc = 0;
168
169
170 /*
171 * Check accessiblity of directory.
172 */
173 if (vdp->v_type != VDIR)
174 return (ENOTDIR);
175 if ( (error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred, p)) )
176 return (error);
177
178 /*
179 * We now have a segment name to search for, and a directory to search.
180 *
181 * Before tediously performing a linear scan of the directory,
182 * check the name cache to see if the directory/name pair
183 * we are looking for is known already.
184 */
185 if ((error = cache_lookup(vdp, vpp, cnp))) {
186 int vpid; /* capability number of vnode */
187
188 if (error == ENOENT)
189 return (error);
190 /*
191 * Get the next vnode in the path.
192 * See comment below starting `Step through' for
193 * an explaination of the locking protocol.
194 */
195 pdp = vdp;
196 dp = VTOI(*vpp);
197 vdp = *vpp;
198 vpid = vdp->v_id;
199 if (pdp == vdp) {
200 VREF(vdp);
201 error = 0;
202 } else if (flags & ISDOTDOT) {
203 VOP_UNLOCK(pdp, 0, p);
204 error = vget(vdp, LK_EXCLUSIVE | LK_RETRY, p);
205 if (!error && lockparent && (flags & ISLASTCN))
206 error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p);
207 } else {
208 error = vget(vdp, LK_EXCLUSIVE | LK_RETRY, p);
209 if (!lockparent || error || !(flags & ISLASTCN))
210 VOP_UNLOCK(pdp, 0, p);
211 }
212 /*
213 * Check that the capability number did not change
214 * while we were waiting for the lock.
215 */
216 if (!error) {
217 if (vpid == vdp->v_id)
218 return (0);
219 vput(vdp);
220 if (lockparent && pdp != vdp && (flags & ISLASTCN))
221 VOP_UNLOCK(pdp, 0, p);
222 }
223 if ( (error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p)) )
224 return (error);
225 vdp = pdp;
226 dp = VTOI(pdp);
227 *vpp = NULL;
228 }
229
230 len = cnp->cn_namelen;
231 name = cnp->cn_nameptr;
232 altname[0] = '\0';
233 /*
234 * A "._" prefix means, we are looking for an associated file
235 */
236 if (imp->iso_ftype != ISO_FTYPE_RRIP &&
237 *name == ASSOCCHAR1 && *(name+1) == ASSOCCHAR2) {
238 wantassoc = 1;
239 len -= 2;
240 name += 2;
241 }
242 /*
243 * Decode search name into UCS-2 (Unicode)
244 */
245 if ((imp->iso_ftype == ISO_FTYPE_JOLIET) &&
246 !((len == 1 && *name == '.') || (flags & ISDOTDOT))) {
247 int flags = UTF_PRECOMPOSED;
248
249 if (BYTE_ORDER != BIG_ENDIAN)
250 flags |= UTF_REVERSE_ENDIAN;
251
252 (void) utf8_decodestr(name, len, (u_int16_t*) altname, &altlen,
253 sizeof(altname), 0, flags);
254 name = altname;
255 len = altlen;
256 }
257 /*
258 * If there is cached information on a previous search of
259 * this directory, pick up where we last left off.
260 * We cache only lookups as these are the most common
261 * and have the greatest payoff. Caching CREATE has little
262 * benefit as it usually must search the entire directory
263 * to determine that the entry does not exist. Caching the
264 * location of the last DELETE or RENAME has not reduced
265 * profiling time and hence has been removed in the interest
266 * of simplicity.
267 */
268 bmask = imp->im_sector_size - 1;
269 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
270 dp->i_diroff > dp->i_size) {
271 entryoffsetinblock = 0;
272 dp->i_offset = 0;
273 numdirpasses = 1;
274 } else {
275 dp->i_offset = dp->i_diroff;
276
277 if ((entryoffsetinblock = dp->i_offset & bmask) &&
278 (error = VOP_BLKATOFF(vdp, SECTOFF(imp, dp->i_offset), NULL, &bp)))
279 return (error);
280 numdirpasses = 2;
281 iso_nchstats.ncs_2passes++;
282 }
283 endsearch = dp->i_size;
284
285 searchloop:
286 while (dp->i_offset < endsearch) {
287 /*
288 * If offset is on a block boundary,
289 * read the next directory block.
290 * Release previous if it exists.
291 */
292 if ((dp->i_offset & bmask) == 0) {
293 if (bp != NULL)
294 brelse(bp);
295 if ( (error = VOP_BLKATOFF(vdp, SECTOFF(imp,dp->i_offset), NULL, &bp)) )
296 return (error);
297 entryoffsetinblock = 0;
298 }
299 /*
300 * Get pointer to next entry.
301 */
302 ep = (struct iso_directory_record *)
303 ((char *)bp->b_data + entryoffsetinblock);
304
305 reclen = isonum_711(ep->length);
306 if (reclen == 0) {
307 /* skip to next block, if any */
308 dp->i_offset =
309 (dp->i_offset & ~bmask) + imp->im_sector_size;
310 continue;
311 }
312
313 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
314 /* illegal entry, stop */
315 break;
316 }
317 if (entryoffsetinblock + reclen > imp->im_sector_size) {
318 /* entries are not allowed to cross sector boundaries */
319 break;
320 }
321 namelen = isonum_711(ep->name_len);
322 isoflags = isonum_711(ep->flags);
323
324 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
325 /* illegal entry, stop */
326 break;
327 /*
328 * Check for a name match.
329 */
330 if (imp->iso_ftype == ISO_FTYPE_RRIP) {
331 if (isoflags & directoryBit)
332 ino = isodirino(ep, imp);
333 else
334 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
335 dp->i_ino = ino;
336 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
337 if (namelen == cnp->cn_namelen
338 && !bcmp(name,altname,namelen))
339 goto found;
340 ino = 0;
341 } else {
342 if ((!(isoflags & associatedBit)) == !wantassoc) {
343 if ((len == 1
344 && *name == '.')
345 || (flags & ISDOTDOT)) {
346 if (namelen == 1
347 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
348 /*
349 * Save directory entry's inode number and
350 * release directory buffer.
351 */
352 dp->i_ino = isodirino(ep, imp);
353 goto found;
354 }
355 if (namelen != 1
356 || ep->name[0] != 0)
357 goto notfound;
358 } else if (imp->iso_ftype != ISO_FTYPE_JOLIET && !(res = isofncmp(name,len,
359 ep->name,namelen))) {
360 if ( isoflags & directoryBit )
361 ino = isodirino(ep, imp);
362 else
363 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
364 saveoffset = dp->i_offset;
365 } else if (imp->iso_ftype == ISO_FTYPE_JOLIET && !(res = ucsfncmp((u_int16_t*)name, len,
366 (u_int16_t*) ep->name, namelen))) {
367 if ( isoflags & directoryBit )
368 ino = isodirino(ep, imp);
369 else
370 ino = (bp->b_blkno << imp->im_bshift) + entryoffsetinblock;
371 saveoffset = dp->i_offset;
372 } else if (ino)
373 goto foundino;
374 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
375 else if (res < 0)
376 goto notfound;
377 else if (res > 0 && numdirpasses == 2)
378 numdirpasses++;
379 #endif
380 }
381 }
382 dp->i_offset += reclen;
383 entryoffsetinblock += reclen;
384 } /* endwhile */
385
386 if (ino) {
387 foundino:
388 dp->i_ino = ino;
389 if (saveoffset != dp->i_offset) {
390 if (lblkno(imp, dp->i_offset) !=
391 lblkno(imp, saveoffset)) {
392 if (bp != NULL)
393 brelse(bp);
394 if ( (error = VOP_BLKATOFF(vdp, SECTOFF(imp, saveoffset), NULL, &bp)) )
395 return (error);
396 }
397 entryoffsetinblock = saveoffset & bmask;
398 ep = (struct iso_directory_record *)
399 ((char *)bp->b_data + entryoffsetinblock);
400 dp->i_offset = saveoffset;
401 }
402 goto found;
403 }
404 notfound:
405 /*
406 * If we started in the middle of the directory and failed
407 * to find our target, we must check the beginning as well.
408 */
409 if (numdirpasses == 2) {
410 numdirpasses--;
411 dp->i_offset = 0;
412 endsearch = dp->i_diroff;
413 goto searchloop;
414 }
415 if (bp != NULL)
416 brelse(bp);
417
418 /*
419 * Insert name into cache (as non-existent) if appropriate.
420 */
421 if (cnp->cn_flags & MAKEENTRY)
422 cache_enter(vdp, *vpp, cnp);
423 if (nameiop == CREATE || nameiop == RENAME) {
424 /*
425 * return EROFS (NOT EJUSTRETURN). The caller will then unlock
426 * the parent for us.
427 */
428 return (EROFS);
429 }
430 return (ENOENT);
431
432 found:
433 if (numdirpasses == 2)
434 iso_nchstats.ncs_pass2++;
435
436 /*
437 * Found component in pathname.
438 * If the final component of path name, save information
439 * in the cache as to where the entry was found.
440 */
441 if ((flags & ISLASTCN) && nameiop == LOOKUP)
442 dp->i_diroff = dp->i_offset;
443
444 /*
445 * Step through the translation in the name. We do not `iput' the
446 * directory because we may need it again if a symbolic link
447 * is relative to the current directory. Instead we save it
448 * unlocked as "pdp". We must get the target inode before unlocking
449 * the directory to insure that the inode will not be removed
450 * before we get it. We prevent deadlock by always fetching
451 * inodes from the root, moving down the directory tree. Thus
452 * when following backward pointers ".." we must unlock the
453 * parent directory before getting the requested directory.
454 * There is a potential race condition here if both the current
455 * and parent directories are removed before the `iget' for the
456 * inode associated with ".." returns. We hope that this occurs
457 * infrequently since we cannot avoid this race condition without
458 * implementing a sophisticated deadlock detection algorithm.
459 * Note also that this simple deadlock detection scheme will not
460 * work if the file system has any hard links other than ".."
461 * that point backwards in the directory structure.
462 */
463 pdp = vdp;
464 /*
465 * If ino is different from dp->i_ino,
466 * it's a relocated directory.
467 */
468 if (flags & ISDOTDOT) {
469 VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
470 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
471 dp->i_ino != ino, ep, p);
472 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
473 brelse(bp);
474 if (error) {
475 VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p);
476 return (error);
477 }
478 if (lockparent && (flags & ISLASTCN) &&
479 (error = VOP_LOCK(pdp, LK_EXCLUSIVE | LK_RETRY, p))) {
480 vput(tdp);
481 return (error);
482 }
483 *vpp = tdp;
484 } else if (dp->i_number == dp->i_ino) {
485 brelse(bp);
486 VREF(vdp); /* we want ourself, ie "." */
487 *vpp = vdp;
488 } else {
489 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
490 dp->i_ino != ino, ep, p);
491 /* save parent inode number */
492 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
493 brelse(bp);
494 if (error)
495 return (error);
496 if (!lockparent || !(flags & ISLASTCN))
497 VOP_UNLOCK(pdp, 0, p);
498 *vpp = tdp;
499 }
500
501 /*
502 * Insert name into cache if appropriate.
503 */
504 if (cnp->cn_flags & MAKEENTRY)
505 cache_enter(vdp, *vpp, cnp);
506
507 return (0);
508 }
509
510
511 /*
512 * Return buffer with the contents of block "offset" from the beginning of
513 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
514 * remaining space in the directory.
515 */
516 int
517 cd9660_blkatoff(ap)
518 struct vop_blkatoff_args /* {
519 struct vnode *a_vp;
520 off_t a_offset;
521 char **a_res;
522 struct buf **a_bpp;
523 } */ *ap;
524 {
525 struct iso_node *ip;
526 register struct iso_mnt *imp;
527 struct buf *bp;
528 daddr_t lbn;
529 int bsize, error;
530
531 ip = VTOI(ap->a_vp);
532 imp = ip->i_mnt;
533 lbn = lblkno(imp, ap->a_offset);
534 bsize = blksize(imp, ip, lbn);
535 if ((bsize != imp->im_sector_size) &&
536 (ap->a_offset & (imp->im_sector_size - 1)) == 0) {
537 bsize = imp->im_sector_size;
538 }
539
540 if ( (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) ) {
541 brelse(bp);
542 *ap->a_bpp = NULL;
543 return (error);
544 }
545 if (ap->a_res)
546 *ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset);
547 *ap->a_bpp = bp;
548
549 return (0);
550 }