2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: cd9660_lookup.c,v 1.13 1994/12/24 15:30:03 cgd Exp $ */
28 * Copyright (c) 1989, 1993, 1994
29 * The Regents of the University of California. All rights reserved.
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).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
66 * @(#)cd9660_lookup.c 8.5 (Berkeley) 12/5/94
71 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
75 #include <sys/param.h>
76 #include <sys/vnode.h>
77 #include <sys/mount.h>
78 #include <sys/namei.h>
81 #include <sys/utfconv.h>
82 #include <sys/paths.h>
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>
89 struct nchstats iso_nchstats
;
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).
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.
108 * Overall outline of ufs_lookup:
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
115 * if creating, return locked directory, leaving info on available slots
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
124 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
128 struct vop_lookup_args
/* {
130 struct vnode **a_vpp;
131 struct componentname *a_cnp;
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 */
153 char altname
[ISO_RRIP_NAMEMAX
];
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
;
172 lockparent
= flags
& LOCKPARENT
;
173 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
177 * Check accessiblity of directory.
179 if (vdp
->v_type
!= VDIR
)
181 if ( (error
= VOP_ACCESS(vdp
, VEXEC
, cred
, p
)) )
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.
189 if ((flags
& ISLASTCN
) == 0 &&
190 bcmp(&cnp
->cn_nameptr
[cnp
->cn_namelen
],
191 _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
) - 1) == 0) {
193 cnp
->cn_consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
197 * We now have a segment name to search for, and a directory to search.
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
204 if ((error
= cache_lookup(vdp
, vpp
, cnp
)) && !wantrsrc
) {
205 int vpid
; /* capability number of vnode */
210 if ((vdp
->v_flag
& VROOT
) && (flags
& ISDOTDOT
))
211 panic("cd9660_lookup: .. through root");
214 * Get the next vnode in the path.
215 * See comment below starting `Step through' for
216 * an explaination of the locking protocol.
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
);
231 error
= vget(vdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
232 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
233 VOP_UNLOCK(pdp
, 0, p
);
236 * Check that the capability number did not change
237 * while we were waiting for the lock.
240 if (vpid
== vdp
->v_id
)
243 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
244 VOP_UNLOCK(pdp
, 0, p
);
246 if ( (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
)) )
253 len
= cnp
->cn_namelen
;
254 name
= cnp
->cn_nameptr
;
259 * Decode search name into UCS-2 (Unicode)
261 if ((imp
->iso_ftype
== ISO_FTYPE_JOLIET
) &&
262 !((len
== 1 && *name
== '.') || (flags
& ISDOTDOT
))) {
263 int flags
= UTF_PRECOMPOSED
;
265 if (BYTE_ORDER
!= BIG_ENDIAN
)
266 flags
|= UTF_REVERSE_ENDIAN
;
268 (void) utf8_decodestr(name
, len
, (u_int16_t
*) altname
, &altlen
,
269 sizeof(altname
), 0, flags
);
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
284 bmask
= imp
->im_bmask
;
285 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
286 dp
->i_diroff
> dp
->i_size
) {
287 entryoffsetinblock
= 0;
291 dp
->i_offset
= dp
->i_diroff
;
293 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
294 (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
297 iso_nchstats
.ncs_2passes
++;
299 endsearch
= dp
->i_size
;
302 while (dp
->i_offset
< endsearch
) {
304 * If offset is on a block boundary,
305 * read the next directory block.
306 * Release previous if it exists.
308 if ((dp
->i_offset
& bmask
) == 0) {
311 if ( (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)) )
313 entryoffsetinblock
= 0;
316 * Get pointer to next entry.
318 ep
= (struct iso_directory_record
*)
319 ((char *)bp
->b_data
+ entryoffsetinblock
);
321 reclen
= isonum_711(ep
->length
);
323 /* skip to next block, if any */
325 (dp
->i_offset
& ~bmask
) + imp
->logical_block_size
;
329 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
)
330 /* illegal entry, stop */
333 if (entryoffsetinblock
+ reclen
> imp
->logical_block_size
)
334 /* entries are not allowed to cross boundaries */
337 namelen
= isonum_711(ep
->name_len
);
339 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
+ namelen
)
340 /* illegal entry, stop */
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';
353 * Check for a name match.
355 if (imp
->iso_ftype
== ISO_FTYPE_RRIP
) {
356 if ( isonum_711(ep
->flags
) & directoryBit
)
357 ino
= isodirino(ep
, imp
);
359 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
361 cd9660_rrip_getname(ep
,altname
,&namelen
,&dp
->i_ino
,imp
);
362 if (namelen
== cnp
->cn_namelen
363 && !bcmp(name
,altname
,namelen
))
367 if ((!(isonum_711(ep
->flags
) & associatedBit
)) == !wantrsrc
) {
370 || (flags
& ISDOTDOT
)) {
372 && ep
->name
[0] == ((flags
& ISDOTDOT
) ? 1 : 0)) {
374 * Save directory entry's inode number and
375 * release directory buffer.
377 dp
->i_ino
= isodirino(ep
, imp
);
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
);
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
);
395 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
396 saveoffset
= dp
->i_offset
;
399 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
402 else if (res
> 0 && numdirpasses
== 2)
407 dp
->i_offset
+= reclen
;
408 entryoffsetinblock
+= reclen
;
414 if (saveoffset
!= dp
->i_offset
) {
415 if (lblkno(imp
, dp
->i_offset
) !=
416 lblkno(imp
, saveoffset
)) {
419 if ( (error
= VOP_BLKATOFF(vdp
, (off_t
)saveoffset
, NULL
, &bp
)) )
422 entryoffsetinblock
= saveoffset
& bmask
;
423 ep
= (struct iso_directory_record
*)
424 ((char *)bp
->b_data
+ entryoffsetinblock
);
425 dp
->i_offset
= saveoffset
;
431 * If we started in the middle of the directory and failed
432 * to find our target, we must check the beginning as well.
434 if (numdirpasses
== 2) {
437 endsearch
= dp
->i_diroff
;
444 * Insert name into cache (as non-existent) if appropriate.
446 if ((cnp
->cn_flags
& MAKEENTRY
) && !wantrsrc
)
447 cache_enter(vdp
, *vpp
, cnp
);
448 if (nameiop
== CREATE
|| nameiop
== RENAME
) {
450 * return EROFS (NOT EJUSTRETURN). The caller will then unlock
462 if (numdirpasses
== 2)
463 iso_nchstats
.ncs_pass2
++;
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.
470 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
471 dp
->i_diroff
= dp
->i_offset
;
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.
494 * If ino is different from dp->i_ino,
495 * it's a relocated directory.
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
;
504 VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
507 if (lockparent
&& (flags
& ISLASTCN
) &&
508 (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
))) {
513 } else if (dp
->i_number
== dp
->i_ino
) {
515 VREF(vdp
); /* we want ourself, ie "." */
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
;
529 if (!lockparent
|| !(flags
& ISLASTCN
))
530 VOP_UNLOCK(pdp
, 0, p
);
535 * Insert name into cache if appropriate.
537 if ((cnp
->cn_flags
& MAKEENTRY
) && !wantrsrc
)
538 cache_enter(vdp
, *vpp
, cnp
);
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.
551 struct vop_blkatoff_args
/* {
559 register struct iso_mnt
*imp
;
566 lbn
= lblkno(imp
, ap
->a_offset
);
567 bsize
= blksize(imp
, ip
, lbn
);
569 if ( (error
= bread(ap
->a_vp
, lbn
, bsize
, NOCRED
, &bp
)) ) {
575 *ap
->a_res
= (char *)bp
->b_data
+ blkoff(imp
, ap
->a_offset
);