2 * Copyright (c) 2000-2003 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
70 #include <sys/param.h>
71 #include <sys/vnode.h>
72 #include <sys/mount.h>
73 #include <sys/namei.h>
76 #include <sys/utfconv.h>
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>
83 struct nchstats iso_nchstats
;
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).
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.
102 * Overall outline of ufs_lookup:
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
109 * if creating, return locked directory, leaving info on available slots
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
118 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
122 struct vop_lookup_args
/* {
124 struct vnode **a_vpp;
125 struct componentname *a_cnp;
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 */
148 char altname
[ISO_RRIP_NAMEMAX
];
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
;
165 lockparent
= flags
& LOCKPARENT
;
166 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
171 * Check accessiblity of directory.
173 if (vdp
->v_type
!= VDIR
)
175 if ( (error
= VOP_ACCESS(vdp
, VEXEC
, cnp
->cn_cred
, p
)) )
179 * We now have a segment name to search for, and a directory to search.
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.
185 if ((error
= cache_lookup(vdp
, vpp
, cnp
))) {
186 int vpid
; /* capability number of vnode */
191 * Get the next vnode in the path.
192 * See comment below starting `Step through' for
193 * an explaination of the locking protocol.
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
);
208 error
= vget(vdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
209 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
210 VOP_UNLOCK(pdp
, 0, p
);
213 * Check that the capability number did not change
214 * while we were waiting for the lock.
217 if (vpid
== vdp
->v_id
)
220 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
221 VOP_UNLOCK(pdp
, 0, p
);
223 if ( (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
)) )
230 len
= cnp
->cn_namelen
;
231 name
= cnp
->cn_nameptr
;
234 * A "._" prefix means, we are looking for an associated file
236 if (imp
->iso_ftype
!= ISO_FTYPE_RRIP
&&
237 *name
== ASSOCCHAR1
&& *(name
+1) == ASSOCCHAR2
) {
243 * Decode search name into UCS-2 (Unicode)
245 if ((imp
->iso_ftype
== ISO_FTYPE_JOLIET
) &&
246 !((len
== 1 && *name
== '.') || (flags
& ISDOTDOT
))) {
247 int flags
= UTF_PRECOMPOSED
;
249 if (BYTE_ORDER
!= BIG_ENDIAN
)
250 flags
|= UTF_REVERSE_ENDIAN
;
252 (void) utf8_decodestr(name
, len
, (u_int16_t
*) altname
, &altlen
,
253 sizeof(altname
), 0, flags
);
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
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;
275 dp
->i_offset
= dp
->i_diroff
;
277 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
278 (error
= VOP_BLKATOFF(vdp
, SECTOFF(imp
, dp
->i_offset
), NULL
, &bp
)))
281 iso_nchstats
.ncs_2passes
++;
283 endsearch
= dp
->i_size
;
286 while (dp
->i_offset
< endsearch
) {
288 * If offset is on a block boundary,
289 * read the next directory block.
290 * Release previous if it exists.
292 if ((dp
->i_offset
& bmask
) == 0) {
295 if ( (error
= VOP_BLKATOFF(vdp
, SECTOFF(imp
,dp
->i_offset
), NULL
, &bp
)) )
297 entryoffsetinblock
= 0;
300 * Get pointer to next entry.
302 ep
= (struct iso_directory_record
*)
303 ((char *)bp
->b_data
+ entryoffsetinblock
);
305 reclen
= isonum_711(ep
->length
);
307 /* skip to next block, if any */
309 (dp
->i_offset
& ~bmask
) + imp
->im_sector_size
;
313 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
) {
314 /* illegal entry, stop */
317 if (entryoffsetinblock
+ reclen
> imp
->im_sector_size
) {
318 /* entries are not allowed to cross sector boundaries */
321 namelen
= isonum_711(ep
->name_len
);
322 isoflags
= isonum_711(ep
->flags
);
324 if (reclen
< ISO_DIRECTORY_RECORD_SIZE
+ namelen
)
325 /* illegal entry, stop */
328 * Check for a name match.
330 if (imp
->iso_ftype
== ISO_FTYPE_RRIP
) {
331 if (isoflags
& directoryBit
)
332 ino
= isodirino(ep
, imp
);
334 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
336 cd9660_rrip_getname(ep
,altname
,&namelen
,&dp
->i_ino
,imp
);
337 if (namelen
== cnp
->cn_namelen
338 && !bcmp(name
,altname
,namelen
))
342 if ((!(isoflags
& associatedBit
)) == !wantassoc
) {
345 || (flags
& ISDOTDOT
)) {
347 && ep
->name
[0] == ((flags
& ISDOTDOT
) ? 1 : 0)) {
349 * Save directory entry's inode number and
350 * release directory buffer.
352 dp
->i_ino
= isodirino(ep
, imp
);
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
);
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
);
370 ino
= (bp
->b_blkno
<< imp
->im_bshift
) + entryoffsetinblock
;
371 saveoffset
= dp
->i_offset
;
374 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
377 else if (res
> 0 && numdirpasses
== 2)
382 dp
->i_offset
+= reclen
;
383 entryoffsetinblock
+= reclen
;
389 if (saveoffset
!= dp
->i_offset
) {
390 if (lblkno(imp
, dp
->i_offset
) !=
391 lblkno(imp
, saveoffset
)) {
394 if ( (error
= VOP_BLKATOFF(vdp
, SECTOFF(imp
, saveoffset
), NULL
, &bp
)) )
397 entryoffsetinblock
= saveoffset
& bmask
;
398 ep
= (struct iso_directory_record
*)
399 ((char *)bp
->b_data
+ entryoffsetinblock
);
400 dp
->i_offset
= saveoffset
;
406 * If we started in the middle of the directory and failed
407 * to find our target, we must check the beginning as well.
409 if (numdirpasses
== 2) {
412 endsearch
= dp
->i_diroff
;
419 * Insert name into cache (as non-existent) if appropriate.
421 if (cnp
->cn_flags
& MAKEENTRY
)
422 cache_enter(vdp
, *vpp
, cnp
);
423 if (nameiop
== CREATE
|| nameiop
== RENAME
) {
425 * return EROFS (NOT EJUSTRETURN). The caller will then unlock
433 if (numdirpasses
== 2)
434 iso_nchstats
.ncs_pass2
++;
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.
441 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
442 dp
->i_diroff
= dp
->i_offset
;
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.
465 * If ino is different from dp->i_ino,
466 * it's a relocated directory.
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
;
475 VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
478 if (lockparent
&& (flags
& ISLASTCN
) &&
479 (error
= VOP_LOCK(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
))) {
484 } else if (dp
->i_number
== dp
->i_ino
) {
486 VREF(vdp
); /* we want ourself, ie "." */
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
;
496 if (!lockparent
|| !(flags
& ISLASTCN
))
497 VOP_UNLOCK(pdp
, 0, p
);
502 * Insert name into cache if appropriate.
504 if (cnp
->cn_flags
& MAKEENTRY
)
505 cache_enter(vdp
, *vpp
, cnp
);
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.
518 struct vop_blkatoff_args
/* {
526 register struct iso_mnt
*imp
;
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
;
540 if ( (error
= bread(ap
->a_vp
, lbn
, bsize
, NOCRED
, &bp
)) ) {
546 *ap
->a_res
= (char *)bp
->b_data
+ blkoff(imp
, ap
->a_offset
);