]> git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_lookup.c
f38eaac9f8a3acb390d107ef6a44553306f73559
[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_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* $NetBSD: cd9660_lookup.c,v 1.13 1994/12/24 15:30:03 cgd Exp $ */
31
32 /*-
33 * Copyright (c) 1989, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley
37 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
38 * Support code is derived from software contributed to Berkeley
39 * by Atsushi Murai (amurai@spec.co.jp).
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 *
69 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
70 *
71 * @(#)cd9660_lookup.c 8.5 (Berkeley) 12/5/94
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
83 #include <isofs/cd9660/iso.h>
84 #include <isofs/cd9660/cd9660_node.h>
85 #include <isofs/cd9660/iso_rrip.h>
86 #include <isofs/cd9660/cd9660_rrip.h>
87
88 struct nchstats iso_nchstats;
89
90 /*
91 * Convert a component of a pathname into a pointer to a locked inode.
92 * This is a very central and rather complicated routine.
93 * If the file system is not maintained in a strict tree hierarchy,
94 * this can result in a deadlock situation (see comments in code below).
95 *
96 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
97 * whether the name is to be looked up, created, renamed, or deleted.
98 * When CREATE, RENAME, or DELETE is specified, information usable in
99 * creating, renaming, or deleting a directory entry may be calculated.
100 * If flag has LOCKPARENT or'ed into it and the target of the pathname
101 * exists, lookup returns both the target and its parent directory locked.
102 * When creating or renaming and LOCKPARENT is specified, the target may
103 * not be ".". When deleting and LOCKPARENT is specified, the target may
104 * be "."., but the caller must check to ensure it does an vrele and iput
105 * instead of two iputs.
106 *
107 * Overall outline of ufs_lookup:
108 *
109 * check accessibility of directory
110 * look for name in cache, if found, then if at end of path
111 * and deleting or creating, drop it, else return name
112 * search for name in directory, to found or notfound
113 * notfound:
114 * if creating, return locked directory, leaving info on available slots
115 * else return error
116 * found:
117 * if at end of path and deleting, return information to allow delete
118 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
119 * inode and return info to allow rewrite
120 * if not at end, add name to cache; if at end and neither creating
121 * nor deleting, add name to cache
122 *
123 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
124 */
125 int
126 cd9660_lookup(struct vnop_lookup_args *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 vfs_context_t ctx = cnp->cn_context;
157 struct proc *p = vfs_context_proc(ctx);
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 * We now have a segment name to search for, and a directory to search.
172 *
173 * Before tediously performing a linear scan of the directory,
174 * check the name cache to see if the directory/name pair
175 * we are looking for is known already.
176 */
177 if ((error = cache_lookup(vdp, vpp, cnp))) {
178 if (error == ENOENT)
179 return (error);
180 return (0);
181 }
182
183 len = cnp->cn_namelen;
184 name = cnp->cn_nameptr;
185 altname[0] = '\0';
186 /*
187 * A "._" prefix means, we are looking for an associated file
188 */
189 if (imp->iso_ftype != ISO_FTYPE_RRIP &&
190 *name == ASSOCCHAR1 && *(name+1) == ASSOCCHAR2) {
191 wantassoc = 1;
192 len -= 2;
193 name += 2;
194 }
195 /*
196 * Decode search name into UCS-2 (Unicode)
197 */
198 if ((imp->iso_ftype == ISO_FTYPE_JOLIET) &&
199 !((len == 1 && *name == '.') || (flags & ISDOTDOT))) {
200 int flags1 = UTF_PRECOMPOSED;
201
202 if (BYTE_ORDER != BIG_ENDIAN)
203 flags1 |= UTF_REVERSE_ENDIAN;
204
205 (void) utf8_decodestr(name, len, (u_int16_t*) altname, &altlen,
206 sizeof(altname), 0, flags1);
207 name = altname;
208 len = altlen;
209 }
210 /*
211 * If there is cached information on a previous search of
212 * this directory, pick up where we last left off.
213 * We cache only lookups as these are the most common
214 * and have the greatest payoff. Caching CREATE has little
215 * benefit as it usually must search the entire directory
216 * to determine that the entry does not exist. Caching the
217 * location of the last DELETE or RENAME has not reduced
218 * profiling time and hence has been removed in the interest
219 * of simplicity.
220 */
221 bmask = imp->im_sector_size - 1;
222 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
223 dp->i_diroff > dp->i_size) {
224 entryoffsetinblock = 0;
225 dp->i_offset = 0;
226 numdirpasses = 1;
227 } else {
228 dp->i_offset = dp->i_diroff;
229
230 if ((entryoffsetinblock = dp->i_offset & bmask) &&
231 (error = cd9660_blkatoff(vdp, SECTOFF(imp, dp->i_offset), NULL, &bp)))
232 return (error);
233 numdirpasses = 2;
234 iso_nchstats.ncs_2passes++;
235 }
236 endsearch = dp->i_size;
237
238 searchloop:
239 while (dp->i_offset < endsearch) {
240 /*
241 * If offset is on a block boundary,
242 * read the next directory block.
243 * Release previous if it exists.
244 */
245 if ((dp->i_offset & bmask) == 0) {
246 if (bp != NULL)
247 buf_brelse(bp);
248 if ( (error = cd9660_blkatoff(vdp, SECTOFF(imp,dp->i_offset), NULL, &bp)) )
249 return (error);
250 entryoffsetinblock = 0;
251 }
252 /*
253 * Get pointer to next entry.
254 */
255 ep = (struct iso_directory_record *)
256 ((char *)buf_dataptr(bp) + entryoffsetinblock);
257
258 reclen = isonum_711(ep->length);
259 if (reclen == 0) {
260 /* skip to next block, if any */
261 dp->i_offset =
262 (dp->i_offset & ~bmask) + imp->im_sector_size;
263 continue;
264 }
265
266 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
267 /* illegal entry, stop */
268 break;
269 }
270 if (entryoffsetinblock + reclen > imp->im_sector_size) {
271 /* entries are not allowed to cross sector boundaries */
272 break;
273 }
274 namelen = isonum_711(ep->name_len);
275 isoflags = isonum_711(ep->flags);
276
277 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
278 /* illegal entry, stop */
279 break;
280 /*
281 * Check for a name match.
282 */
283 if (imp->iso_ftype == ISO_FTYPE_RRIP) {
284 if (isoflags & directoryBit)
285 ino = isodirino(ep, imp);
286 else
287 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
288 dp->i_ino = ino;
289 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
290 if (namelen == cnp->cn_namelen
291 && !bcmp(name,altname,namelen))
292 goto found;
293 ino = 0;
294 } else {
295 if ((!(isoflags & associatedBit)) == !wantassoc) {
296 if ((len == 1
297 && *name == '.')
298 || (flags & ISDOTDOT)) {
299 if (namelen == 1
300 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
301 /*
302 * Save directory entry's inode number and
303 * release directory buffer.
304 */
305 dp->i_ino = isodirino(ep, imp);
306 goto found;
307 }
308 if (namelen != 1
309 || ep->name[0] != 0)
310 goto notfound;
311 } else if (imp->iso_ftype != ISO_FTYPE_JOLIET && !(res = isofncmp(name,len,
312 ep->name,namelen))) {
313 if ( isoflags & directoryBit )
314 ino = isodirino(ep, imp);
315 else
316 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
317 saveoffset = dp->i_offset;
318 } else if (imp->iso_ftype == ISO_FTYPE_JOLIET && !(res = ucsfncmp((u_int16_t*)name, len,
319 (u_int16_t*) ep->name, namelen))) {
320 if ( isoflags & directoryBit )
321 ino = isodirino(ep, imp);
322 else
323 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
324 saveoffset = dp->i_offset;
325 } else if (ino)
326 goto foundino;
327 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
328 else if (res < 0)
329 goto notfound;
330 else if (res > 0 && numdirpasses == 2)
331 numdirpasses++;
332 #endif
333 }
334 }
335 dp->i_offset += reclen;
336 entryoffsetinblock += reclen;
337 } /* endwhile */
338
339 if (ino) {
340 foundino:
341 dp->i_ino = ino;
342 if (saveoffset != dp->i_offset) {
343 if (lblkno(imp, dp->i_offset) !=
344 lblkno(imp, saveoffset)) {
345 if (bp != NULL)
346 buf_brelse(bp);
347 if ( (error = cd9660_blkatoff(vdp, SECTOFF(imp, saveoffset), NULL, &bp)) )
348 return (error);
349 }
350 entryoffsetinblock = saveoffset & bmask;
351 ep = (struct iso_directory_record *)
352 ((char *)buf_dataptr(bp) + entryoffsetinblock);
353 dp->i_offset = saveoffset;
354 }
355 goto found;
356 }
357 notfound:
358 /*
359 * If we started in the middle of the directory and failed
360 * to find our target, we must check the beginning as well.
361 */
362 if (numdirpasses == 2) {
363 numdirpasses--;
364 dp->i_offset = 0;
365 endsearch = dp->i_diroff;
366 goto searchloop;
367 }
368 if (bp != NULL)
369 buf_brelse(bp);
370
371 /*
372 * Insert name into cache (as non-existent) if appropriate.
373 */
374 if (cnp->cn_flags & MAKEENTRY)
375 cache_enter(vdp, *vpp, cnp);
376 return (ENOENT);
377
378 found:
379 if (numdirpasses == 2)
380 iso_nchstats.ncs_pass2++;
381
382 /*
383 * Found component in pathname.
384 * If the final component of path name, save information
385 * in the cache as to where the entry was found.
386 */
387 if ((flags & ISLASTCN) && nameiop == LOOKUP)
388 dp->i_diroff = dp->i_offset;
389
390 /*
391 * Step through the translation in the name. We do not `iput' the
392 * directory because we may need it again if a symbolic link
393 * is relative to the current directory. Instead we save it
394 * unlocked as "pdp". We must get the target inode before unlocking
395 * the directory to insure that the inode will not be removed
396 * before we get it. We prevent deadlock by always fetching
397 * inodes from the root, moving down the directory tree. Thus
398 * when following backward pointers ".." we must unlock the
399 * parent directory before getting the requested directory.
400 * There is a potential race condition here if both the current
401 * and parent directories are removed before the `iget' for the
402 * inode associated with ".." returns. We hope that this occurs
403 * infrequently since we cannot avoid this race condition without
404 * implementing a sophisticated deadlock detection algorithm.
405 * Note also that this simple deadlock detection scheme will not
406 * work if the file system has any hard links other than ".."
407 * that point backwards in the directory structure.
408 */
409 pdp = vdp;
410 /*
411 * If ino is different from dp->i_ino,
412 * it's a relocated directory.
413 */
414 if (flags & ISDOTDOT) {
415 error = cd9660_vget_internal(vnode_mount(vdp), dp->i_ino, &tdp, NULL, NULL,
416 dp->i_ino != ino, ep, p);
417 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
418 buf_brelse(bp);
419
420 *vpp = tdp;
421 } else if (dp->i_number == dp->i_ino) {
422 buf_brelse(bp);
423 vnode_get(vdp); /* we want ourself, ie "." */
424 *vpp = vdp;
425 } else {
426 error = cd9660_vget_internal(vnode_mount(vdp), dp->i_ino, &tdp, vdp, cnp,
427 dp->i_ino != ino, ep, p);
428 /* save parent inode number */
429 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
430 buf_brelse(bp);
431 if (error)
432 return (error);
433 *vpp = tdp;
434 }
435 return (0);
436 }
437
438
439 /*
440 * Return buffer with the contents of block "offset" from the beginning of
441 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
442 * remaining space in the directory.
443 */
444 int
445 cd9660_blkatoff(vnode_t vp, off_t offset, char **res, buf_t *bpp)
446 {
447 struct iso_node *ip;
448 register struct iso_mnt *imp;
449 buf_t bp;
450 daddr_t lbn;
451 int bsize, error;
452
453 ip = VTOI(vp);
454 imp = ip->i_mnt;
455 lbn = lblkno(imp, offset);
456 bsize = blksize(imp, ip, lbn);
457
458 if ((bsize != imp->im_sector_size) &&
459 (offset & (imp->im_sector_size - 1)) == 0) {
460 bsize = imp->im_sector_size;
461 }
462
463 if ( (error = (int)buf_bread(vp, (daddr64_t)((unsigned)lbn), bsize, NOCRED, &bp)) ) {
464 buf_brelse(bp);
465 *bpp = NULL;
466 return (error);
467 }
468 if (res)
469 *res = (char *)buf_dataptr(bp) + blkoff(imp, offset);
470 *bpp = bp;
471
472 return (0);
473 }