2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: cd9660_lookup.c,v 1.13 1994/12/24 15:30:03 cgd Exp $ */
25 * Copyright (c) 1989, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
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).
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
63 * @(#)cd9660_lookup.c 8.5 (Berkeley) 12/5/94
68 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
72 #include <sys/param.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/namei.h>
78 #include <sys/utfconv.h>
79 #include <sys/paths.h>
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>
86 struct nchstats iso_nchstats
;
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).
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.
105 * Overall outline of ufs_lookup:
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
112 * if creating, return locked directory, leaving info on available slots
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
121 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
125 struct vop_lookup_args
/* {
127 struct vnode **a_vpp;
128 struct componentname *a_cnp;
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 */
150 char altname
[ISO_RRIP_NAMEMAX
];
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
;
169 lockparent
= flags
& LOCKPARENT
;
170 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
174 * Check accessiblity of directory.
176 if (vdp
->v_type
!= VDIR
)
178 if ( (error
= VOP_ACCESS(vdp
, VEXEC
, cred
, p
)) )
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.
186 if ((flags
& ISLASTCN
) == 0 &&
187 bcmp(&cnp
->cn_nameptr
[cnp
->cn_namelen
],
188 _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
) - 1) == 0) {
190 cnp
->cn_consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
194 * We now have a segment name to search for, and a directory to search.
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
201 if ((error
= cache_lookup(vdp
, vpp
, cnp
)) && !wantrsrc
) {
202 int vpid
; /* capability number of vnode */
207 if ((vdp
->v_flag
& VROOT
) && (flags
& ISDOTDOT
))
208 panic("cd9660_lookup: .. through root");
211 * Get the next vnode in the path.
212 * See comment below starting `Step through' for
213 * an explaination of the locking protocol.
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
);
228 error
= vget(vdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
229 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
230 VOP_UNLOCK(pdp
, 0, p
);
233 * Check that the capability number did not change
234 * while we were waiting for the lock.
237 if (vpid
== vdp
->v_id
)
240 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
241 VOP_UNLOCK(pdp
, 0, p
);
243 if ( (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
)) )
250 len
= cnp
->cn_namelen
;
251 name
= cnp
->cn_nameptr
;
256 * Decode search name into UCS-2 (Unicode)
258 if ((imp
->iso_ftype
== ISO_FTYPE_JOLIET
) &&
259 !((len
== 1 && *name
== '.') || (flags
& ISDOTDOT
))) {
262 if (BYTE_ORDER
!= BIG_ENDIAN
)
263 flags
|= UTF_REVERSE_ENDIAN
;
265 (void) utf8_decodestr(name
, len
, (u_int16_t
*) altname
, &altlen
,
266 sizeof(altname
), 0, flags
);
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
281 bmask
= imp
->im_bmask
;
282 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
283 dp
->i_diroff
> dp
->i_size
) {
284 entryoffsetinblock
= 0;
288 dp
->i_offset
= dp
->i_diroff
;
290 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
291 (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
294 iso_nchstats
.ncs_2passes
++;
296 endsearch
= dp
->i_size
;
299 while (dp
->i_offset
< endsearch
) {
301 * If offset is on a block boundary,
302 * read the next directory block.
303 * Release previous if it exists.
305 if ((dp
->i_offset
& bmask
) == 0) {
308 if ( (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)) )
310 entryoffsetinblock
= 0;
313 * Get pointer to next entry.
315 ep
= (struct iso_directory_record
*)
316 ((char *)bp
->b_data
+ entryoffsetinblock
);
318 reclen
= isonum_711(ep
->length
);
320 /* skip to next block, if any */
322 (dp
->i_offset
& ~bmask
) + imp
->logical_block_size
;
326 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
)
327 /* illegal entry, stop */
330 if (entryoffsetinblock
+ reclen
> imp
->logical_block_size
)
331 /* entries are not allowed to cross boundaries */
334 namelen
= isonum_711(ep
->name_len
);
336 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
+ namelen
)
337 /* illegal entry, stop */
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';
350 * Check for a name match.
352 if (imp
->iso_ftype
== ISO_FTYPE_RRIP
) {
353 if ( isonum_711(ep
->flags
) & directoryBit
)
354 ino
= isodirino(ep
, imp
);
356 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
358 cd9660_rrip_getname(ep
,altname
,&namelen
,&dp
->i_ino
,imp
);
359 if (namelen
== cnp
->cn_namelen
360 && !bcmp(name
,altname
,namelen
))
364 if ((!(isonum_711(ep
->flags
) & associatedBit
)) == !wantrsrc
) {
367 || (flags
& ISDOTDOT
)) {
369 && ep
->name
[0] == ((flags
& ISDOTDOT
) ? 1 : 0)) {
371 * Save directory entry's inode number and
372 * release directory buffer.
374 dp
->i_ino
= isodirino(ep
, imp
);
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
);
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
);
392 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
393 saveoffset
= dp
->i_offset
;
396 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
399 else if (res
> 0 && numdirpasses
== 2)
404 dp
->i_offset
+= reclen
;
405 entryoffsetinblock
+= reclen
;
411 if (saveoffset
!= dp
->i_offset
) {
412 if (lblkno(imp
, dp
->i_offset
) !=
413 lblkno(imp
, saveoffset
)) {
416 if ( (error
= VOP_BLKATOFF(vdp
, (off_t
)saveoffset
, NULL
, &bp
)) )
419 entryoffsetinblock
= saveoffset
& bmask
;
420 ep
= (struct iso_directory_record
*)
421 ((char *)bp
->b_data
+ entryoffsetinblock
);
422 dp
->i_offset
= saveoffset
;
428 * If we started in the middle of the directory and failed
429 * to find our target, we must check the beginning as well.
431 if (numdirpasses
== 2) {
434 endsearch
= dp
->i_diroff
;
441 * Insert name into cache (as non-existent) if appropriate.
443 if ((cnp
->cn_flags
& MAKEENTRY
) && !wantrsrc
)
444 cache_enter(vdp
, *vpp
, cnp
);
445 if (nameiop
== CREATE
|| nameiop
== RENAME
) {
447 * return EROFS (NOT EJUSTRETURN). The caller will then unlock
459 if (numdirpasses
== 2)
460 iso_nchstats
.ncs_pass2
++;
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.
467 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
468 dp
->i_diroff
= dp
->i_offset
;
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.
491 * If ino is different from dp->i_ino,
492 * it's a relocated directory.
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
;
501 VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
504 if (lockparent
&& (flags
& ISLASTCN
) &&
505 (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
))) {
510 } else if (dp
->i_number
== dp
->i_ino
) {
512 VREF(vdp
); /* we want ourself, ie "." */
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
;
526 if (!lockparent
|| !(flags
& ISLASTCN
))
527 VOP_UNLOCK(pdp
, 0, p
);
532 * Insert name into cache if appropriate.
534 if ((cnp
->cn_flags
& MAKEENTRY
) && !wantrsrc
)
535 cache_enter(vdp
, *vpp
, cnp
);
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.
548 struct vop_blkatoff_args
/* {
556 register struct iso_mnt
*imp
;
563 lbn
= lblkno(imp
, ap
->a_offset
);
564 bsize
= blksize(imp
, ip
, lbn
);
566 if ( (error
= bread(ap
->a_vp
, lbn
, bsize
, NOCRED
, &bp
)) ) {
572 *ap
->a_res
= (char *)bp
->b_data
+ blkoff(imp
, ap
->a_offset
);